summaryrefslogtreecommitdiffstats
path: root/storage/src/tests/distributor/externaloperationhandlertest.cpp
blob: a99567126793cfe55ede3ebb2b75c3b4d4dcee3f (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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.

#include <tests/distributor/distributortestutil.h>
#include <vespa/storage/distributor/externaloperationhandler.h>
#include <vespa/storage/distributor/distributor.h>
#include <vespa/storage/distributor/distributormetricsset.h>
#include <vespa/storageapi/message/persistence.h>
#include <vespa/document/repo/documenttyperepo.h>
#include <vespa/document/update/documentupdate.h>
#include <vespa/document/test/make_document_bucket.h>

using document::test::makeDocumentBucket;

namespace storage::distributor {

class ExternalOperationHandlerTest : public CppUnit::TestFixture,
                                     public DistributorTestUtil
{
    document::TestDocMan _testDocMan;

    CPPUNIT_TEST_SUITE(ExternalOperationHandlerTest);
    CPPUNIT_TEST(testBucketSplitMask);
    CPPUNIT_TEST(testOperationRejectedOnWrongDistribution);
    CPPUNIT_TEST(testOperationRejectedOnPendingWrongDistribution);
    CPPUNIT_TEST(reject_put_if_not_past_safe_time_point);
    CPPUNIT_TEST(reject_remove_if_not_past_safe_time_point);
    CPPUNIT_TEST(reject_update_if_not_past_safe_time_point);
    CPPUNIT_TEST(get_not_rejected_by_unsafe_time_point);
    CPPUNIT_TEST(mutation_not_rejected_when_safe_point_reached);
    CPPUNIT_TEST(reject_put_with_concurrent_mutation_to_same_id);
    CPPUNIT_TEST(do_not_reject_put_operations_to_different_ids);
    CPPUNIT_TEST(reject_remove_with_concurrent_mutation_to_same_id);
    CPPUNIT_TEST(do_not_reject_remove_operations_to_different_ids);
    CPPUNIT_TEST(reject_update_with_concurrent_mutation_to_same_id);
    CPPUNIT_TEST(do_not_reject_update_operations_to_different_ids);
    CPPUNIT_TEST(operation_destruction_allows_new_mutations_for_id);
    CPPUNIT_TEST(concurrent_get_and_mutation_do_not_conflict);
    CPPUNIT_TEST(sequencing_works_across_mutation_types);
    CPPUNIT_TEST(sequencing_can_be_explicitly_config_disabled);
    CPPUNIT_TEST_SUITE_END();

    document::BucketId findNonOwnedUserBucketInState(vespalib::stringref state);
    document::BucketId findOwned1stNotOwned2ndInStates(
            vespalib::stringref state1,
            vespalib::stringref state2);

    std::shared_ptr<api::GetCommand> makeGetCommandForUser(uint64_t id) const;
    std::shared_ptr<api::GetCommand> makeGetCommand(const vespalib::string& id) const;
    std::shared_ptr<api::UpdateCommand> makeUpdateCommand(const vespalib::string& doc_type,
                                                          const vespalib::string& id) const;
    std::shared_ptr<api::UpdateCommand> makeUpdateCommand() const;
    std::shared_ptr<api::PutCommand> makePutCommand(const vespalib::string& doc_type,
                                                    const vespalib::string& id) const;
    std::shared_ptr<api::RemoveCommand> makeRemoveCommand(const vespalib::string& id) const;

    Operation::SP start_operation_verify_not_rejected(std::shared_ptr<api::StorageCommand> cmd);
    void start_operation_verify_rejected(std::shared_ptr<api::StorageCommand> cmd);

    int64_t safe_time_not_reached_metric_count(
            const metrics::LoadMetric<PersistenceOperationMetricSet>& metrics) const {
        return metrics[documentapi::LoadType::DEFAULT].failures
                .safe_time_not_reached.getLongValue("count");
    }

    int64_t safe_time_not_reached_metric_count(const metrics::LoadMetric<UpdateMetricSet>& metrics) const {
        return metrics[documentapi::LoadType::DEFAULT].failures.safe_time_not_reached.getLongValue("count");
    }

    int64_t concurrent_mutatations_metric_count(
            const metrics::LoadMetric<PersistenceOperationMetricSet>& metrics) const {
        return metrics[documentapi::LoadType::DEFAULT].failures
                .concurrent_mutations.getLongValue("count");
    }

    int64_t concurrent_mutatations_metric_count(const metrics::LoadMetric<UpdateMetricSet>& metrics) const {
        return metrics[documentapi::LoadType::DEFAULT].failures.concurrent_mutations.getLongValue("count");
    }

    void set_up_distributor_for_sequencing_test();

    const vespalib::string _dummy_id{"id:foo:testdoctype1::bar"};

protected:
    void testBucketSplitMask();
    void testOperationRejectedOnWrongDistribution();
    void testOperationRejectedOnPendingWrongDistribution();
    void reject_put_if_not_past_safe_time_point();
    void reject_remove_if_not_past_safe_time_point();
    void reject_update_if_not_past_safe_time_point();
    void get_not_rejected_by_unsafe_time_point();
    void mutation_not_rejected_when_safe_point_reached();
    void reject_put_with_concurrent_mutation_to_same_id();
    void do_not_reject_put_operations_to_different_ids();
    void reject_remove_with_concurrent_mutation_to_same_id();
    void do_not_reject_remove_operations_to_different_ids();
    void reject_update_with_concurrent_mutation_to_same_id();
    void do_not_reject_update_operations_to_different_ids();
    void operation_destruction_allows_new_mutations_for_id();
    void concurrent_get_and_mutation_do_not_conflict();
    void sequencing_works_across_mutation_types();
    void sequencing_can_be_explicitly_config_disabled();

    void assert_rejection_due_to_unsafe_time(
            std::shared_ptr<api::StorageCommand> cmd);

    void assert_second_command_rejected_due_to_concurrent_mutation(
            std::shared_ptr<api::StorageCommand> cmd1,
            std::shared_ptr<api::StorageCommand> cmd2,
            const vespalib::string& expected_id_in_message);
    void assert_second_command_not_rejected_due_to_concurrent_mutation(
            std::shared_ptr<api::StorageCommand> cmd1,
            std::shared_ptr<api::StorageCommand> cmd2);

public:
    void tearDown() override {
        close();
    }

};

CPPUNIT_TEST_SUITE_REGISTRATION(ExternalOperationHandlerTest);

using document::DocumentId;

void
ExternalOperationHandlerTest::testBucketSplitMask()
{
    {
        createLinks();
        getDirConfig().getConfig("stor-distributormanager").set("minsplitcount", "16");

        CPPUNIT_ASSERT_EQUAL(document::BucketId(16, 0xffff),
                getExternalOperationHandler().getBucketId(document::DocumentId(
                    vespalib::make_string("userdoc:ns:%d::", 0xffff))
                ).stripUnused());
        CPPUNIT_ASSERT_EQUAL(document::BucketId(16, 0),
                getExternalOperationHandler().getBucketId(document::DocumentId(
                    vespalib::make_string("userdoc:ns:%d::", 0x10000))
                ).stripUnused());
        CPPUNIT_ASSERT_EQUAL(document::BucketId(16, 0xffff),
                getExternalOperationHandler().getBucketId(document::DocumentId(
                    vespalib::make_string("userdoc:ns:%d::", 0xffff))
                ).stripUnused());
        CPPUNIT_ASSERT_EQUAL(document::BucketId(16, 0x100),
                getExternalOperationHandler().getBucketId(document::DocumentId(
                    vespalib::make_string("userdoc:ns:%d::", 0x100))
                ).stripUnused());
        close();
    }
    {
        getDirConfig().getConfig("stor-distributormanager").set("minsplitcount", "20");
        createLinks();
        CPPUNIT_ASSERT_EQUAL(document::BucketId(20, 0x11111),
                getExternalOperationHandler().getBucketId(document::DocumentId(
                    vespalib::make_string("userdoc:ns:%d::", 0x111111))
                ).stripUnused());
        CPPUNIT_ASSERT_EQUAL(document::BucketId(20, 0x22222),
                getExternalOperationHandler().getBucketId(document::DocumentId(
                    vespalib::make_string("userdoc:ns:%d::", 0x222222))
                ).stripUnused());
    }
}

document::BucketId
ExternalOperationHandlerTest::findNonOwnedUserBucketInState(
        vespalib::stringref statestr)
{
    lib::ClusterState state(statestr);
    for (uint64_t i = 1; i < 1000; ++i) {
        document::BucketId bucket(32, i);
        if (!getExternalOperationHandler().ownsBucketInState(state, makeDocumentBucket(bucket))) {
            return bucket;
        }
    }
    throw std::runtime_error("no appropriate bucket found");
}

document::BucketId
ExternalOperationHandlerTest::findOwned1stNotOwned2ndInStates(
        vespalib::stringref statestr1,
        vespalib::stringref statestr2)
{
    lib::ClusterState state1(statestr1);
    lib::ClusterState state2(statestr2);
    for (uint64_t i = 1; i < 1000; ++i) {
        document::BucketId bucket(32, i);
        if (getExternalOperationHandler().ownsBucketInState(state1, makeDocumentBucket(bucket))
            && !getExternalOperationHandler().ownsBucketInState(state2, makeDocumentBucket(bucket)))
        {
            return bucket;
        }
    }
    throw std::runtime_error("no appropriate bucket found");
}

std::shared_ptr<api::GetCommand>
ExternalOperationHandlerTest::makeGetCommand(const vespalib::string& id) const {
    return std::make_shared<api::GetCommand>(makeDocumentBucket(document::BucketId(0)), DocumentId(id), "[all]");
}

std::shared_ptr<api::GetCommand>
ExternalOperationHandlerTest::makeGetCommandForUser(uint64_t id) const {
    DocumentId docId(document::UserDocIdString(vespalib::make_string("userdoc:foo:%lu:bar", id)));
    return std::make_shared<api::GetCommand>(makeDocumentBucket(document::BucketId(0)), docId, "[all]");
}

std::shared_ptr<api::UpdateCommand> ExternalOperationHandlerTest::makeUpdateCommand(
        const vespalib::string& doc_type,
        const vespalib::string& id) const {
    auto update = std::make_shared<document::DocumentUpdate>(
            _testDocMan.getTypeRepo(),
            *_testDocMan.getTypeRepo().getDocumentType(doc_type),
            document::DocumentId(id));
    return std::make_shared<api::UpdateCommand>(
            makeDocumentBucket(document::BucketId(0)), std::move(update), api::Timestamp(0));
}

std::shared_ptr<api::UpdateCommand>
ExternalOperationHandlerTest::makeUpdateCommand() const {
    return makeUpdateCommand("testdoctype1", "id:foo:testdoctype1::baz");
}

std::shared_ptr<api::PutCommand> ExternalOperationHandlerTest::makePutCommand(
        const vespalib::string& doc_type,
        const vespalib::string& id) const {
    auto doc = _testDocMan.createDocument(doc_type, id);
    return std::make_shared<api::PutCommand>(
            makeDocumentBucket(document::BucketId(0)), std::move(doc), api::Timestamp(0));
}

std::shared_ptr<api::RemoveCommand> ExternalOperationHandlerTest::makeRemoveCommand(const vespalib::string& id) const {
    return std::make_shared<api::RemoveCommand>(makeDocumentBucket(document::BucketId(0)), DocumentId(id), api::Timestamp(0));
}

void
ExternalOperationHandlerTest::testOperationRejectedOnWrongDistribution()
{
    createLinks();
    std::string state("distributor:2 storage:2");
    setupDistributor(1, 2, state);

    document::BucketId bucket(findNonOwnedUserBucketInState(state));
    auto cmd = makeGetCommandForUser(bucket.withoutCountBits());

    Operation::SP genOp;
    CPPUNIT_ASSERT(getExternalOperationHandler().handleMessage(cmd, genOp));
    CPPUNIT_ASSERT(!genOp.get());
    CPPUNIT_ASSERT_EQUAL(size_t(1), _sender.replies.size());
    CPPUNIT_ASSERT_EQUAL(
            std::string("ReturnCode(WRONG_DISTRIBUTION, "
                        "distributor:2 storage:2)"),
            _sender.replies[0]->getResult().toString());
}

void
ExternalOperationHandlerTest::testOperationRejectedOnPendingWrongDistribution()
{
    createLinks();
    std::string current("distributor:2 storage:2");
    std::string pending("distributor:3 storage:3");
    setupDistributor(1, 3, current);

    document::BucketId b(findOwned1stNotOwned2ndInStates(current, pending));

    // Trigger pending cluster state
    auto stateCmd = std::make_shared<api::SetSystemStateCommand>(
            lib::ClusterState(pending));
    getBucketDBUpdater().onSetSystemState(stateCmd);

    auto cmd = makeGetCommandForUser(b.withoutCountBits());

    Operation::SP genOp;
    CPPUNIT_ASSERT(getExternalOperationHandler().handleMessage(cmd, genOp));
    CPPUNIT_ASSERT(!genOp.get());
    CPPUNIT_ASSERT_EQUAL(size_t(1), _sender.replies.size());
    // Fail back with _pending_ cluster state so client can start trying
    // correct distributor immediately. If that distributor has not yet
    // completed processing its pending cluster state, it'll return the
    // old (current) cluster state, causing the client to bounce between
    // the two until the pending states have been resolved. This is pretty
    // much inevitable with the current design.
    CPPUNIT_ASSERT_EQUAL(
            std::string("ReturnCode(WRONG_DISTRIBUTION, "
                        "distributor:3 storage:3)"),
            _sender.replies[0]->getResult().toString());
}

using TimePoint = ExternalOperationHandler::TimePoint;
using namespace std::literals::chrono_literals;

void ExternalOperationHandlerTest::assert_rejection_due_to_unsafe_time(
        std::shared_ptr<api::StorageCommand> cmd)
{
    createLinks();
    setupDistributor(1, 2, "distributor:1 storage:1");
    getClock().setAbsoluteTimeInSeconds(9);
    getExternalOperationHandler().rejectFeedBeforeTimeReached(TimePoint(10s));

    Operation::SP generated;
    getExternalOperationHandler().handleMessage(cmd, generated);
    CPPUNIT_ASSERT(generated.get() == nullptr);
    CPPUNIT_ASSERT_EQUAL(size_t(1), _sender.replies.size());
    CPPUNIT_ASSERT_EQUAL(
            std::string("ReturnCode(STALE_TIMESTAMP, "
                        "Operation received at time 9, which is before "
                        "bucket ownership transfer safe time of 10)"),
            _sender.replies[0]->getResult().toString());
}

void ExternalOperationHandlerTest::reject_put_if_not_past_safe_time_point() {
    assert_rejection_due_to_unsafe_time(makePutCommand("foo", "id:foo:testdoctype1::bar"));
    CPPUNIT_ASSERT_EQUAL(int64_t(1), safe_time_not_reached_metric_count(
            getDistributor().getMetrics().puts));
}

void ExternalOperationHandlerTest::reject_remove_if_not_past_safe_time_point() {
    assert_rejection_due_to_unsafe_time(makeRemoveCommand("id:foo:testdoctype1::bar"));
    CPPUNIT_ASSERT_EQUAL(int64_t(1), safe_time_not_reached_metric_count(
            getDistributor().getMetrics().removes));
}

void ExternalOperationHandlerTest::reject_update_if_not_past_safe_time_point() {
    assert_rejection_due_to_unsafe_time(makeUpdateCommand());
    CPPUNIT_ASSERT_EQUAL(int64_t(1), safe_time_not_reached_metric_count(
            getDistributor().getMetrics().updates));
}

void ExternalOperationHandlerTest::get_not_rejected_by_unsafe_time_point() {
    createLinks();
    setupDistributor(1, 2, "distributor:1 storage:1");
    getClock().setAbsoluteTimeInSeconds(9);
    getExternalOperationHandler().rejectFeedBeforeTimeReached(TimePoint(10s));

    Operation::SP generated;
    getExternalOperationHandler().handleMessage(
            makeGetCommandForUser(0), generated);
    CPPUNIT_ASSERT(generated.get() != nullptr);
    CPPUNIT_ASSERT_EQUAL(size_t(0), _sender.replies.size());
    CPPUNIT_ASSERT_EQUAL(int64_t(0), safe_time_not_reached_metric_count(
            getDistributor().getMetrics().gets));
}

void ExternalOperationHandlerTest::mutation_not_rejected_when_safe_point_reached() {
    createLinks();
    setupDistributor(1, 2, "distributor:1 storage:1");
    getClock().setAbsoluteTimeInSeconds(10);
    getExternalOperationHandler().rejectFeedBeforeTimeReached(TimePoint(10s));

    Operation::SP generated;
    DocumentId id("id:foo:testdoctype1::bar");
    getExternalOperationHandler().handleMessage(
            std::make_shared<api::RemoveCommand>(
                makeDocumentBucket(document::BucketId(0)), id, api::Timestamp(0)),
            generated);
    CPPUNIT_ASSERT(generated.get() != nullptr);
    CPPUNIT_ASSERT_EQUAL(size_t(0), _sender.replies.size());
    CPPUNIT_ASSERT_EQUAL(int64_t(0), safe_time_not_reached_metric_count(
            getDistributor().getMetrics().removes));
}

void ExternalOperationHandlerTest::set_up_distributor_for_sequencing_test() {
    createLinks();
    setupDistributor(1, 2, "distributor:1 storage:1");
}

Operation::SP ExternalOperationHandlerTest::start_operation_verify_not_rejected(
        std::shared_ptr<api::StorageCommand> cmd) {
    Operation::SP generated;
    _sender.replies.clear();
    getExternalOperationHandler().handleMessage(cmd, generated);
    CPPUNIT_ASSERT(generated.get() != nullptr);
    CPPUNIT_ASSERT_EQUAL(size_t(0), _sender.replies.size());
    return generated;
}
void ExternalOperationHandlerTest::start_operation_verify_rejected(
        std::shared_ptr<api::StorageCommand> cmd) {
    Operation::SP generated;
    _sender.replies.clear();
    getExternalOperationHandler().handleMessage(cmd, generated);
    CPPUNIT_ASSERT(generated.get() == nullptr);
    CPPUNIT_ASSERT_EQUAL(size_t(1), _sender.replies.size());
}

void ExternalOperationHandlerTest::assert_second_command_rejected_due_to_concurrent_mutation(
        std::shared_ptr<api::StorageCommand> cmd1,
        std::shared_ptr<api::StorageCommand> cmd2,
        const vespalib::string& expected_id_in_message) {
    set_up_distributor_for_sequencing_test();

    // Must hold ref to started operation, or sequencing handle will be released.
    Operation::SP generated1 = start_operation_verify_not_rejected(std::move(cmd1));
    start_operation_verify_rejected(std::move(cmd2));

    // TODO reconsider BUSY return code. Need something transient and non-noisy
    CPPUNIT_ASSERT_EQUAL(
            std::string(vespalib::make_string(
                    "ReturnCode(BUSY, A mutating operation for document "
                    "'%s' is already in progress)", expected_id_in_message.c_str())),
            _sender.replies[0]->getResult().toString());
}

void ExternalOperationHandlerTest::assert_second_command_not_rejected_due_to_concurrent_mutation(
        std::shared_ptr<api::StorageCommand> cmd1,
        std::shared_ptr<api::StorageCommand> cmd2) {
    set_up_distributor_for_sequencing_test();

    Operation::SP generated1 = start_operation_verify_not_rejected(std::move(cmd1));
    start_operation_verify_not_rejected(std::move(cmd2));
}

void ExternalOperationHandlerTest::reject_put_with_concurrent_mutation_to_same_id() {
    assert_second_command_rejected_due_to_concurrent_mutation(
            makePutCommand("testdoctype1", _dummy_id),
            makePutCommand("testdoctype1", _dummy_id), _dummy_id);
    CPPUNIT_ASSERT_EQUAL(int64_t(1), concurrent_mutatations_metric_count(getDistributor().getMetrics().puts));
}

void ExternalOperationHandlerTest::do_not_reject_put_operations_to_different_ids() {
    assert_second_command_not_rejected_due_to_concurrent_mutation(
            makePutCommand("testdoctype1", "id:foo:testdoctype1::baz"),
            makePutCommand("testdoctype1", "id:foo:testdoctype1::foo"));
    CPPUNIT_ASSERT_EQUAL(int64_t(0), concurrent_mutatations_metric_count(getDistributor().getMetrics().puts));
}

void ExternalOperationHandlerTest::reject_remove_with_concurrent_mutation_to_same_id() {
    assert_second_command_rejected_due_to_concurrent_mutation(
            makeRemoveCommand(_dummy_id), makeRemoveCommand(_dummy_id), _dummy_id);
    CPPUNIT_ASSERT_EQUAL(int64_t(1), concurrent_mutatations_metric_count(getDistributor().getMetrics().removes));
}

void ExternalOperationHandlerTest::do_not_reject_remove_operations_to_different_ids() {
    assert_second_command_not_rejected_due_to_concurrent_mutation(
            makeRemoveCommand("id:foo:testdoctype1::baz"),
            makeRemoveCommand("id:foo:testdoctype1::foo"));
    CPPUNIT_ASSERT_EQUAL(int64_t(0), concurrent_mutatations_metric_count(getDistributor().getMetrics().removes));
}

void ExternalOperationHandlerTest::reject_update_with_concurrent_mutation_to_same_id() {
    assert_second_command_rejected_due_to_concurrent_mutation(
            makeUpdateCommand("testdoctype1", _dummy_id),
            makeUpdateCommand("testdoctype1", _dummy_id), _dummy_id);
    CPPUNIT_ASSERT_EQUAL(int64_t(1), concurrent_mutatations_metric_count(getDistributor().getMetrics().updates));
}

void ExternalOperationHandlerTest::do_not_reject_update_operations_to_different_ids() {
    assert_second_command_not_rejected_due_to_concurrent_mutation(
            makeUpdateCommand("testdoctype1", "id:foo:testdoctype1::baz"),
            makeUpdateCommand("testdoctype1", "id:foo:testdoctype1::foo"));
    CPPUNIT_ASSERT_EQUAL(int64_t(0), concurrent_mutatations_metric_count(getDistributor().getMetrics().updates));
}

void ExternalOperationHandlerTest::operation_destruction_allows_new_mutations_for_id() {
    set_up_distributor_for_sequencing_test();

    Operation::SP generated = start_operation_verify_not_rejected(makeRemoveCommand(_dummy_id));

    generated.reset(); // Implicitly release sequencing handle

    start_operation_verify_not_rejected(makeRemoveCommand(_dummy_id));
}

void ExternalOperationHandlerTest::concurrent_get_and_mutation_do_not_conflict() {
    set_up_distributor_for_sequencing_test();

    Operation::SP generated1 = start_operation_verify_not_rejected(makeRemoveCommand(_dummy_id));

    start_operation_verify_not_rejected(makeGetCommand(_dummy_id));
}

void ExternalOperationHandlerTest::sequencing_works_across_mutation_types() {
    set_up_distributor_for_sequencing_test();

    Operation::SP generated = start_operation_verify_not_rejected(makePutCommand("testdoctype1", _dummy_id));
    start_operation_verify_rejected(makeRemoveCommand(_dummy_id));
    start_operation_verify_rejected(makeUpdateCommand("testdoctype1", _dummy_id));
}

void ExternalOperationHandlerTest::sequencing_can_be_explicitly_config_disabled() {
    set_up_distributor_for_sequencing_test();

    // Should be able to modify config after links have been created, i.e. this is a live config.
    getConfig().setSequenceMutatingOperations(false);

    Operation::SP generated = start_operation_verify_not_rejected(makeRemoveCommand(_dummy_id));
    // Sequencing is disabled, so concurrent op is not rejected.
    start_operation_verify_not_rejected(makeRemoveCommand(_dummy_id));
}

// TODO support sequencing of RemoveLocation? It's a mutating operation, but supporting it with
// the current approach is not trivial. A RemoveLocation operation covers the _entire_ bucket
// sub tree under a given location, while the sequencer works on individual GIDs. Mapping the
// former to the latter is not trivial unless we introduce higher level "location" mutation
// pseudo-locks in the sequencer. I.e. if we get a RemoveLocation with id.user==123456, this
// prevents any handles from being acquired to any GID under location BucketId(32, 123456).

}