aboutsummaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/check_condition_test.cpp
blob: 617401dd271c792566aa40bf2dfd8f45c4b61496 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
#include <vespa/document/bucket/bucket.h>
#include <vespa/document/bucket/fixed_bucket_spaces.h>
#include <vespa/document/base/documentid.h>
#include <vespa/document/fieldset/fieldsets.h>
#include <vespa/documentapi/messagebus/messages/testandsetcondition.h>
#include <vespa/storage/distributor/node_supported_features.h>
#include <vespa/storage/distributor/operations/cancel_scope.h>
#include <vespa/storage/distributor/operations/external/check_condition.h>
#include <vespa/storage/distributor/persistence_operation_metric_set.h>
#include <vespa/storageapi/message/persistence.h>
#include <tests/distributor/distributor_stripe_test_util.h>
#include <gtest/gtest.h>
#include <gmock/gmock.h>

using namespace ::testing;

namespace storage::distributor {

using namespace document;
using documentapi::TestAndSetCondition;

class CheckConditionTest
    : public Test,
      public DistributorStripeTestUtil
{
public:
    DocumentId                    _doc_id{"id:foo:testdoctype1:n=1234:bar"};
    BucketId                      _bucket_id{16, 1234};
    TestAndSetCondition           _tas_cond{"foo or bar"};
    PersistenceOperationMetricSet _metrics{"dummy_metrics", nullptr};
    uint32_t                      _trace_level{5};

    CheckConditionTest();
    ~CheckConditionTest() override;

    void SetUp() override {
        createLinks();
        // By default, set up 2 nodes {0, 1} with mutually out of sync replica state
        // and with both reporting that they support condition probing.
        setup_stripe(2, 2, "version:1 storage:2 distributor:1");
        config_enable_condition_probing(true);
        tag_content_node_supports_condition_probing(0, true);
        tag_content_node_supports_condition_probing(1, true);
        addNodesToBucketDB(_bucket_id, "0=10/20/30/t,1=40/50/60");
    };

    void TearDown() override {
        close();
    }

    std::shared_ptr<CheckCondition> create_check_condition() {
        auto& bucket_space = getDistributorBucketSpace();
        auto doc_bucket    = BucketIdFactory{}.getBucketId(_doc_id);
        auto bucket        = Bucket(FixedBucketSpaces::default_space(), _bucket_id);
        assert(_bucket_id.contains(doc_bucket));
        return CheckCondition::create_if_inconsistent_replicas(bucket, bucket_space, _doc_id, _tas_cond,
                                                               node_context(), operation_context(), _metrics,
                                                               _trace_level);
    }

    std::shared_ptr<api::GetCommand> sent_get_command(size_t idx) {
        return sent_command<api::GetCommand>(idx);
    }

    std::shared_ptr<api::PutCommand> sent_put_command(size_t idx) {
        return sent_command<api::PutCommand>(idx);
    }

    static std::shared_ptr<api::GetReply> make_reply(const api::GetCommand& cmd, api::Timestamp ts,
                                                     bool is_tombstone, bool condition_matched)
    {
        return std::make_shared<api::GetReply>(cmd, std::shared_ptr<document::Document>(), ts,
                                               false, is_tombstone, condition_matched);
    }

    std::shared_ptr<api::GetReply> make_matched_reply(size_t cmd_idx, api::Timestamp ts = 1000) {
        return make_reply(*sent_get_command(cmd_idx), ts, false, true);
    }

    std::shared_ptr<api::GetReply> make_mismatched_reply(size_t cmd_idx, api::Timestamp ts = 1000) {
        return make_reply(*sent_get_command(cmd_idx), ts, false, false);
    }

    std::shared_ptr<api::GetReply> make_not_found_non_tombstone_reply(size_t cmd_idx) {
        return make_reply(*sent_get_command(cmd_idx), 0, false, false);
    }

    std::shared_ptr<api::GetReply> make_tombstone_reply(size_t cmd_idx, api::Timestamp ts = 1000) {
        return make_reply(*sent_get_command(cmd_idx), ts, true, false);
    }

    std::shared_ptr<api::GetReply> make_trace_reply(size_t cmd_idx, api::Timestamp ts, std::string trace_message) {
        auto reply = make_reply(*sent_get_command(cmd_idx), ts, true, false);
        MBUS_TRACE(reply->getTrace(), _trace_level, trace_message);
        return reply;
    }

    std::shared_ptr<api::GetReply> make_failed_reply(size_t cmd_idx) {
        auto reply = make_reply(*sent_get_command(cmd_idx), 0, false, false);
        reply->setResult(api::ReturnCode(api::ReturnCode::ABORTED, "did a bork"));
        return reply;
    }

    void test_cond_with_2_gets_sent(const std::function<void(CheckCondition&)>& reply_invoker,
                                    const std::function<void(const CheckCondition::Outcome&)>& outcome_checker)
    {
        auto cond = create_check_condition();
        ASSERT_TRUE(cond);
        cond->start_and_send(_sender);
        ASSERT_EQ("Get => 0,Get => 1", _sender.getCommands(true));
        reply_invoker(*cond);
        auto& outcome = cond->maybe_outcome();
        ASSERT_TRUE(outcome);
        outcome_checker(*outcome);
    }
};

CheckConditionTest::CheckConditionTest() = default;
CheckConditionTest::~CheckConditionTest() = default;

TEST_F(CheckConditionTest, no_checker_returned_when_config_disabled) {
    config_enable_condition_probing(false);
    auto cond = create_check_condition();
    EXPECT_FALSE(cond);
}

TEST_F(CheckConditionTest, no_checker_returned_when_probing_not_supported_on_at_least_one_node) {
    tag_content_node_supports_condition_probing(1, false);
    auto cond = create_check_condition();
    EXPECT_FALSE(cond);
}

TEST_F(CheckConditionTest, no_checker_returned_when_bucket_replicas_are_consistent) {
    addNodesToBucketDB(_bucket_id, "0=10/20/30/t,1=10/20/30");
    auto cond = create_check_condition();
    EXPECT_FALSE(cond);
}

TEST_F(CheckConditionTest, no_checker_returned_when_empty_replica_set) {
    removeFromBucketDB(_bucket_id);
    auto cond = create_check_condition();
    EXPECT_FALSE(cond);
}

TEST_F(CheckConditionTest, starting_sends_condition_probe_gets) {
    auto cond = create_check_condition();
    ASSERT_TRUE(cond);
    EXPECT_FALSE(cond->maybe_outcome());
    // Nothing should be sent prior to start_and_send()
    ASSERT_EQ("", _sender.getCommands(true));
    // We don't test too much of the Get functionality, as that's already covered by GetOperation tests.
    // But we test the main binding glue between the two components.
    cond->start_and_send(_sender);
    EXPECT_FALSE(cond->maybe_outcome());
    ASSERT_EQ("Get => 0,Get => 1", _sender.getCommands(true));
    auto cmd = sent_get_command(0);
    EXPECT_EQ(cmd->getDocumentId(), _doc_id);
    EXPECT_EQ(cmd->condition(), _tas_cond);
    EXPECT_EQ(cmd->getFieldSet(), NoFields::NAME);
    EXPECT_EQ(cmd->internal_read_consistency(), api::InternalReadConsistency::Strong);
    EXPECT_EQ(cmd->getTrace().getLevel(), _trace_level);
}

TEST_F(CheckConditionTest, condition_matching_completes_check_with_match_outcome) {
    test_cond_with_2_gets_sent([&](auto& cond) {
        cond.handle_reply(_sender, make_matched_reply(0));
        cond.handle_reply(_sender, make_matched_reply(1));
    }, [&](auto& outcome) {
        EXPECT_TRUE(outcome.matched_condition());
        EXPECT_FALSE(outcome.not_found());
        EXPECT_FALSE(outcome.failed());
    });
}

TEST_F(CheckConditionTest, newest_document_version_is_authoritative_for_condition_match) {
    test_cond_with_2_gets_sent([&](auto& cond) {
        cond.handle_reply(_sender, make_matched_reply(0, 1001));
        cond.handle_reply(_sender, make_mismatched_reply(1, 1000));
    }, [&](auto& outcome) {
        EXPECT_TRUE(outcome.matched_condition());
        EXPECT_FALSE(outcome.not_found());
        EXPECT_FALSE(outcome.failed());
    });
}

TEST_F(CheckConditionTest, condition_mismatching_completes_check_with_mismatch_outcome) {
    test_cond_with_2_gets_sent([&](auto& cond) {
        cond.handle_reply(_sender, make_matched_reply(0, 1000));
        cond.handle_reply(_sender, make_mismatched_reply(1, 1001));
    }, [&](auto& outcome) {
        EXPECT_FALSE(outcome.matched_condition());
        EXPECT_FALSE(outcome.not_found());
        EXPECT_FALSE(outcome.failed());
    });
}

TEST_F(CheckConditionTest, not_found_non_tombstone_completes_check_with_not_found_outcome) {
    test_cond_with_2_gets_sent([&](auto& cond) {
        cond.handle_reply(_sender, make_not_found_non_tombstone_reply(0));
        cond.handle_reply(_sender, make_not_found_non_tombstone_reply(1));
    }, [&](auto& outcome) {
        EXPECT_FALSE(outcome.matched_condition());
        EXPECT_TRUE(outcome.not_found());
        EXPECT_FALSE(outcome.failed());
    });
}

TEST_F(CheckConditionTest, not_found_with_tombstone_completes_check_with_not_found_outcome) {
    test_cond_with_2_gets_sent([&](auto& cond) {
        cond.handle_reply(_sender, make_matched_reply(0, 1000));
        cond.handle_reply(_sender, make_tombstone_reply(1, 1001));
    }, [&](auto& outcome) {
        EXPECT_FALSE(outcome.matched_condition());
        EXPECT_TRUE(outcome.not_found());
        EXPECT_FALSE(outcome.failed());
    });
}

TEST_F(CheckConditionTest, failed_gets_completes_check_with_error_outcome) {
    test_cond_with_2_gets_sent([&](auto& cond) {
        cond.handle_reply(_sender, make_matched_reply(0));
        cond.handle_reply(_sender, make_failed_reply(1));
    }, [&](auto& outcome) {
        EXPECT_FALSE(outcome.matched_condition());
        EXPECT_FALSE(outcome.not_found());
        EXPECT_TRUE(outcome.failed());
    });
}

TEST_F(CheckConditionTest, check_fails_if_condition_explicitly_cancelled) {
    test_cond_with_2_gets_sent([&](auto& cond) {
        cond.handle_reply(_sender, make_matched_reply(0));
        cond.cancel(_sender, CancelScope::of_fully_cancelled());
        cond.handle_reply(_sender, make_matched_reply(1));
    }, [&](auto& outcome) {
        EXPECT_FALSE(outcome.matched_condition());
        EXPECT_FALSE(outcome.not_found());
        EXPECT_TRUE(outcome.failed());
        EXPECT_EQ(outcome.error_code().getResult(), api::ReturnCode::ABORTED);
    });
}

// TODO deprecate in favor of cancelling
TEST_F(CheckConditionTest, check_fails_if_replica_set_changed_between_start_and_completion) {
    test_cond_with_2_gets_sent([&](auto& cond) {
        cond.handle_reply(_sender, make_matched_reply(0));
        // Simulate node 0 going down, with new cluster state version push and implicit DB removal
        enable_cluster_state("version:2 storage:1 distributor:1");
        addNodesToBucketDB(_bucket_id, "1=10/20/30");
        cond.handle_reply(_sender, make_matched_reply(1));
    }, [&](auto& outcome) {
        EXPECT_FALSE(outcome.matched_condition());
        EXPECT_FALSE(outcome.not_found());
        EXPECT_TRUE(outcome.failed());
        EXPECT_EQ(outcome.error_code().getResult(), api::ReturnCode::BUCKET_NOT_FOUND);
    });
}

// TODO deprecate in favor of cancelling
TEST_F(CheckConditionTest, check_fails_if_bucket_ownership_changed_between_start_and_completion_pending_transition_case) {
    test_cond_with_2_gets_sent([&](auto& cond) {
        cond.handle_reply(_sender, make_matched_reply(0));
        simulate_set_pending_cluster_state("version:2 storage:1 distributor:1 .0.s:d"); // technically, no distributors own anything
        cond.handle_reply(_sender, make_matched_reply(1));
    }, [&](auto& outcome) {
        EXPECT_FALSE(outcome.matched_condition());
        EXPECT_FALSE(outcome.not_found());
        EXPECT_TRUE(outcome.failed());
        EXPECT_EQ(outcome.error_code().getResult(), api::ReturnCode::BUCKET_NOT_FOUND);
    });
}

// TODO deprecate in favor of cancelling
TEST_F(CheckConditionTest, check_fails_if_bucket_ownership_changed_between_start_and_completion_completed_transition_case) {
    test_cond_with_2_gets_sent([&](auto& cond) {
        cond.handle_reply(_sender, make_matched_reply(0));
        enable_cluster_state("version:2 storage:1 distributor:1 .0.s:d"); // technically, no distributors own anything
        cond.handle_reply(_sender, make_matched_reply(1));
    }, [&](auto& outcome) {
        EXPECT_FALSE(outcome.matched_condition());
        EXPECT_FALSE(outcome.not_found());
        EXPECT_TRUE(outcome.failed());
        EXPECT_EQ(outcome.error_code().getResult(), api::ReturnCode::BUCKET_NOT_FOUND);
    });
}

TEST_F(CheckConditionTest, nested_get_traces_are_propagated_to_outcome) {
    test_cond_with_2_gets_sent([&](auto& cond) {
        cond.handle_reply(_sender, make_trace_reply(0, 100, "hello"));
        cond.handle_reply(_sender, make_trace_reply(1, 200, "world"));
    }, [&](auto& outcome) {
        auto trace_str = outcome.trace().toString();
        EXPECT_THAT(trace_str, HasSubstr("hello"));
        EXPECT_THAT(trace_str, HasSubstr("world"));
    });
}

TEST_F(CheckConditionTest, condition_evaluation_increments_probe_latency_metrics) {
    getClock().setAbsoluteTimeInSeconds(1);
    EXPECT_EQ(_metrics.latency.getLongValue("count"), 0);
    EXPECT_EQ(_metrics.ok.getLongValue("last"), 0);
    test_cond_with_2_gets_sent([&](auto& cond) {
        cond.handle_reply(_sender, make_matched_reply(0));
        getClock().setAbsoluteTimeInSeconds(3);
        cond.handle_reply(_sender, make_matched_reply(1));
    }, [&](auto& outcome) noexcept {
        (void)outcome;
    });
    EXPECT_EQ(_metrics.latency.getLongValue("count"), 1);
    EXPECT_EQ(_metrics.ok.getLongValue("last"), 1);
    EXPECT_DOUBLE_EQ(_metrics.latency.getLast(), 2'000.0); // in millis
}

}