summaryrefslogtreecommitdiff
path: root/src/mongo/db/s/collection_metadata_test.cpp
blob: 5bbf258a5abb5b78781bdc937b97ab8e8c40b1b3 (plain)
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
497
498
499
500
501
502
503
504
/**
 *    Copyright (C) 2018-present MongoDB, Inc.
 *
 *    This program is free software: you can redistribute it and/or modify
 *    it under the terms of the Server Side Public License, version 1,
 *    as published by MongoDB, Inc.
 *
 *    This program is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    Server Side Public License for more details.
 *
 *    You should have received a copy of the Server Side Public License
 *    along with this program. If not, see
 *    <http://www.mongodb.com/licensing/server-side-public-license>.
 *
 *    As a special exception, the copyright holders give permission to link the
 *    code of portions of this program with the OpenSSL library under certain
 *    conditions as described in each individual source file and distribute
 *    linked combinations including the program with the OpenSSL library. You
 *    must comply with the Server Side Public License in all respects for
 *    all of the code used other than as permitted herein. If you modify file(s)
 *    with this exception, you may extend this exception to your version of the
 *    file(s), but you are not obligated to do so. If you do not wish to do so,
 *    delete this exception statement from your version. If you delete this
 *    exception statement from all source files in the program, then also delete
 *    it in the license file.
 */

#include "mongo/platform/basic.h"

#include "mongo/base/status.h"
#include "mongo/db/range_arithmetic.h"
#include "mongo/db/s/collection_metadata.h"
#include "mongo/db/s/resharding_util.h"
#include "mongo/s/catalog/type_chunk.h"
#include "mongo/s/chunk_version.h"
#include "mongo/s/sharding_test_fixture_common.h"
#include "mongo/unittest/unittest.h"

namespace mongo {
namespace {

using unittest::assertGet;

const NamespaceString kNss("test.foo");
const ShardId kThisShard("thisShard");
const ShardId kOtherShard("otherShard");

CollectionMetadata makeCollectionMetadataImpl(
    const KeyPattern& shardKeyPattern,
    const std::vector<std::pair<BSONObj, BSONObj>>& thisShardsChunks,
    bool staleChunkManager,
    UUID uuid = UUID::gen(),
    boost::optional<TypeCollectionReshardingFields> reshardingFields = boost::none) {

    const OID epoch = OID::gen();

    const Timestamp kRouting(100, 0);
    const Timestamp kChunkManager(staleChunkManager ? 99 : 100, 0);

    std::vector<ChunkType> allChunks;
    auto nextMinKey = shardKeyPattern.globalMin();
    ChunkVersion version{1, 0, epoch, boost::none /* timestamp */};
    for (const auto& myNextChunk : thisShardsChunks) {
        if (SimpleBSONObjComparator::kInstance.evaluate(nextMinKey < myNextChunk.first)) {
            // Need to add a chunk to the other shard from nextMinKey to myNextChunk.first.
            allChunks.emplace_back(
                kNss, ChunkRange{nextMinKey, myNextChunk.first}, version, kOtherShard);
            allChunks.back().setHistory({ChunkHistory(kRouting, kOtherShard)});
            version.incMajor();
        }
        allChunks.emplace_back(
            kNss, ChunkRange{myNextChunk.first, myNextChunk.second}, version, kThisShard);
        allChunks.back().setHistory({ChunkHistory(kRouting, kThisShard)});
        version.incMajor();
        nextMinKey = myNextChunk.second;
    }

    if (SimpleBSONObjComparator::kInstance.evaluate(nextMinKey < shardKeyPattern.globalMax())) {
        allChunks.emplace_back(
            kNss, ChunkRange{nextMinKey, shardKeyPattern.globalMax()}, version, kOtherShard);
        allChunks.back().setHistory({ChunkHistory(kRouting, kOtherShard)});
    }

    return CollectionMetadata(
        ChunkManager(kThisShard,
                     DatabaseVersion(uuid),
                     ShardingTestFixtureCommon::makeStandaloneRoutingTableHistory(
                         RoutingTableHistory::makeNew(kNss,
                                                      uuid,
                                                      shardKeyPattern,
                                                      nullptr,
                                                      false,
                                                      epoch,
                                                      boost::none /* timestamp */,
                                                      std::move(reshardingFields),
                                                      true,
                                                      allChunks)),
                     kChunkManager),
        kThisShard);
}

struct ConstructedRangeMap : public RangeMap {
    ConstructedRangeMap()
        : RangeMap(SimpleBSONObjComparator::kInstance.makeBSONObjIndexedMap<BSONObj>()) {}
};

class NoChunkFixture : public unittest::Test {
protected:
    CollectionMetadata makeCollectionMetadata(
        UUID existingUuid = UUID::gen(),
        UUID reshardingUuid = UUID::gen(),
        CoordinatorStateEnum state = CoordinatorStateEnum::kInitializing) const {

        TypeCollectionReshardingFields reshardingFields{reshardingUuid};
        reshardingFields.setState(state);

        if (state == CoordinatorStateEnum::kDecisionPersisted) {
            TypeCollectionRecipientFields recipientFields{
                {kThisShard, kOtherShard}, existingUuid, kNss};
            reshardingFields.setRecipientFields(std::move(recipientFields));
        } else if (state == CoordinatorStateEnum::kBlockingWrites) {
            TypeCollectionDonorFields donorFields{
                constructTemporaryReshardingNss(kNss.db(), existingUuid),
                KeyPattern{BSON("newKey" << 1)},
                {kThisShard, kOtherShard}};
            reshardingFields.setDonorFields(std::move(donorFields));
        }

        auto metadataUuid = (state >= CoordinatorStateEnum::kDecisionPersisted &&
                             state != CoordinatorStateEnum::kError)
            ? reshardingUuid
            : existingUuid;

        return makeCollectionMetadataImpl(
            KeyPattern(BSON("a" << 1)), {}, false, metadataUuid, reshardingFields);
    }
};

TEST_F(NoChunkFixture, KeyBelongsToMe) {
    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSON("a" << MINKEY)));
    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSON("a" << 10)));
    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSON("a" << MINKEY)));

    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSONObj()));
}

TEST_F(NoChunkFixture, IsValidKey) {
    ASSERT(makeCollectionMetadata().isValidKey(BSON("a"
                                                    << "abcde")));
    ASSERT(makeCollectionMetadata().isValidKey(BSON("a" << 3)));
    ASSERT(!makeCollectionMetadata().isValidKey(BSON("a"
                                                     << "abcde"
                                                     << "b" << 1)));
    ASSERT(!makeCollectionMetadata().isValidKey(BSON("c"
                                                     << "abcde")));
}

TEST_F(NoChunkFixture, GetNextChunk) {
    ChunkType nextChunk;
    ASSERT(
        !makeCollectionMetadata().getNextChunk(makeCollectionMetadata().getMinKey(), &nextChunk));
}

TEST_F(NoChunkFixture, RangeOverlapsChunk) {
    ASSERT(!makeCollectionMetadata().rangeOverlapsChunk(
        ChunkRange{BSON("a" << 100), BSON("a" << 200)}));
}

TEST_F(NoChunkFixture, OrphanedDataRangeBegin) {
    auto metadata(makeCollectionMetadata());

    ConstructedRangeMap pending;
    BSONObj lookupKey = metadata.getMinKey();
    auto keyRange = metadata.getNextOrphanRange(pending, lookupKey);
    ASSERT(keyRange);

    ASSERT(keyRange->getMin().woCompare(metadata.getMinKey()) == 0);
    ASSERT(keyRange->getMax().woCompare(metadata.getMaxKey()) == 0);

    // Make sure we don't have any more ranges
    ASSERT(!metadata.getNextOrphanRange(pending, keyRange->getMax()));
}

TEST_F(NoChunkFixture, OrphanedDataRangeMiddle) {
    auto metadata(makeCollectionMetadata());

    ConstructedRangeMap pending;
    BSONObj lookupKey = BSON("a" << 20);
    auto keyRange = metadata.getNextOrphanRange(pending, lookupKey);
    ASSERT(keyRange);

    ASSERT(keyRange->getMin().woCompare(metadata.getMinKey()) == 0);
    ASSERT(keyRange->getMax().woCompare(metadata.getMaxKey()) == 0);

    // Make sure we don't have any more ranges
    ASSERT(!metadata.getNextOrphanRange(pending, keyRange->getMax()));
}

TEST_F(NoChunkFixture, OrphanedDataRangeEnd) {
    auto metadata(makeCollectionMetadata());

    ConstructedRangeMap pending;
    ASSERT(!metadata.getNextOrphanRange(pending, metadata.getMaxKey()));
}

TEST_F(NoChunkFixture, DisallowWritesInDecisionPersistedWithOrigUUID) {
    UUID originalUUID = UUID::gen();
    UUID reshardingUUID = UUID::gen();

    auto metadata = makeCollectionMetadata(
        originalUUID, reshardingUUID, CoordinatorStateEnum::kDecisionPersisted);

    // Writes should be disallowed if the collection metadata's UUID matches the original
    // collection's UUID.
    ASSERT(metadata.disallowWritesForResharding(originalUUID));
}

TEST_F(NoChunkFixture, AllowWritesInDecisionPersistedWithReshardingUUID) {
    UUID originalUUID = UUID::gen();
    UUID reshardingUUID = UUID::gen();

    auto metadata = makeCollectionMetadata(
        originalUUID, reshardingUUID, CoordinatorStateEnum::kDecisionPersisted);

    // Writes should NOT be disallowed when the UUID matches the temp collection's
    // UUID.
    ASSERT(!metadata.disallowWritesForResharding(reshardingUUID));
}

TEST_F(NoChunkFixture, DisallowWritesInDecisionPersistedThrows) {
    UUID originalUUID = UUID::gen();
    UUID reshardingUUID = UUID::gen();
    UUID rogueUUID = UUID::gen();

    auto metadata = makeCollectionMetadata(
        originalUUID, reshardingUUID, CoordinatorStateEnum::kDecisionPersisted);

    // If the collection's UUID matches neither the original UUID nor the resharding UUID,
    // expect an exception.
    ASSERT_THROWS_CODE(metadata.disallowWritesForResharding(rogueUUID),
                       AssertionException,
                       ErrorCodes::InvalidUUID);
}

TEST_F(NoChunkFixture, DisallowWritesInApplyingWithOrigUUID) {
    UUID originalUUID = UUID::gen();
    UUID reshardingUUID = UUID::gen();

    auto metadata =
        makeCollectionMetadata(originalUUID, reshardingUUID, CoordinatorStateEnum::kApplying);

    // Writes should NOT be disallowed if the coordinator state is applying.
    ASSERT(!metadata.disallowWritesForResharding(originalUUID));
}

TEST_F(NoChunkFixture, DisallowWritesInBlockingWritesWithOrigUUID) {
    UUID originalUUID = UUID::gen();
    UUID reshardingUUID = UUID::gen();

    auto metadata =
        makeCollectionMetadata(originalUUID, reshardingUUID, CoordinatorStateEnum::kBlockingWrites);

    // Writes should be disallowed if the coordinator state is blocking-writes.
    ASSERT(metadata.disallowWritesForResharding(originalUUID));
}

TEST_F(NoChunkFixture, DisallowWritesInErrorWithOrigUUID) {
    UUID originalUUID = UUID::gen();
    UUID reshardingUUID = UUID::gen();

    auto metadata =
        makeCollectionMetadata(originalUUID, reshardingUUID, CoordinatorStateEnum::kError);

    // Writes should NOT be disallowed if the coordinator state is error.
    ASSERT(!metadata.disallowWritesForResharding(originalUUID));
}

/**
 * Fixture with single chunk containing:
 * [10->20)
 */
class SingleChunkFixture : public unittest::Test {
protected:
    CollectionMetadata makeCollectionMetadata() const {
        return makeCollectionMetadataImpl(
            KeyPattern(BSON("a" << 1)), {std::make_pair(BSON("a" << 10), BSON("a" << 20))}, false);
    }
};

TEST_F(SingleChunkFixture, KeyBelongsToMe) {
    ASSERT(makeCollectionMetadata().keyBelongsToMe(BSON("a" << 10)));
    ASSERT(makeCollectionMetadata().keyBelongsToMe(BSON("a" << 15)));
    ASSERT(makeCollectionMetadata().keyBelongsToMe(BSON("a" << 19)));

    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSON("a" << 0)));
    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSON("a" << 9)));
    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSON("a" << 20)));
    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSON("a" << 1234)));
    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSON("a" << MINKEY)));
    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSON("a" << MAXKEY)));

    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSONObj()));
}

TEST_F(SingleChunkFixture, GetNextChunk) {
    ChunkType nextChunk;
    ASSERT(makeCollectionMetadata().getNextChunk(makeCollectionMetadata().getMinKey(), &nextChunk));
    ASSERT_EQUALS(0, nextChunk.getMin().woCompare(BSON("a" << 10)));
    ASSERT_EQUALS(0, nextChunk.getMax().woCompare(BSON("a" << 20)));
}

TEST_F(SingleChunkFixture, GetNextChunkShouldFindNothing) {
    ChunkType nextChunk;
    ASSERT(
        !makeCollectionMetadata().getNextChunk(makeCollectionMetadata().getMaxKey(), &nextChunk));
}

TEST_F(SingleChunkFixture, RangeOverlapsChunk) {
    ASSERT(
        !makeCollectionMetadata().rangeOverlapsChunk(ChunkRange{BSON("a" << 20), BSON("a" << 30)}));
    ASSERT(!makeCollectionMetadata().rangeOverlapsChunk(
        ChunkRange{BSON("a" << 100), BSON("a" << 200)}));
    ASSERT(
        !makeCollectionMetadata().rangeOverlapsChunk(ChunkRange{BSON("a" << 0), BSON("a" << 10)}));
    ASSERT(
        makeCollectionMetadata().rangeOverlapsChunk(ChunkRange{BSON("a" << 11), BSON("a" << 19)}));
    ASSERT(
        makeCollectionMetadata().rangeOverlapsChunk(ChunkRange{BSON("a" << 19), BSON("a" << 20)}));
}

TEST_F(SingleChunkFixture, ChunkOrphanedDataRanges) {
    ConstructedRangeMap pending;
    auto keyRange =
        makeCollectionMetadata().getNextOrphanRange(pending, makeCollectionMetadata().getMinKey());
    ASSERT(keyRange);

    ASSERT(keyRange->getMin().woCompare(makeCollectionMetadata().getMinKey()) == 0);
    ASSERT(keyRange->getMax().woCompare(BSON("a" << 10)) == 0);

    keyRange = makeCollectionMetadata().getNextOrphanRange(pending, keyRange->getMax());
    ASSERT(keyRange);
    ASSERT(keyRange->getMin().woCompare(BSON("a" << 20)) == 0);
    ASSERT(keyRange->getMax().woCompare(makeCollectionMetadata().getMaxKey()) == 0);

    ASSERT(!makeCollectionMetadata().getNextOrphanRange(pending, keyRange->getMax()));
}

/**
 * Fixture with single chunk containing:
 * [(min, min)->(max, max))
 */
class SingleChunkMinMaxCompoundKeyFixture : public unittest::Test {
protected:
    CollectionMetadata makeCollectionMetadata() const {
        const KeyPattern shardKeyPattern(BSON("a" << 1 << "b" << 1));
        return makeCollectionMetadataImpl(
            shardKeyPattern,
            {std::make_pair(shardKeyPattern.globalMin(), shardKeyPattern.globalMax())},
            false);
    }
};

TEST_F(SingleChunkMinMaxCompoundKeyFixture, KeyBelongsToMe) {
    ASSERT(makeCollectionMetadata().keyBelongsToMe(BSON("a" << MINKEY << "b" << MINKEY)));
    ASSERT(makeCollectionMetadata().keyBelongsToMe(BSON("a" << MINKEY << "b" << 10)));
    ASSERT(makeCollectionMetadata().keyBelongsToMe(BSON("a" << 10 << "b" << 20)));

    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSON("a" << MAXKEY << "b" << MAXKEY)));

    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSONObj()));
}

/**
 * Fixture with chunks:
 * [(10, 0)->(20, 0)), [(30, 0)->(40, 0))
 */
class TwoChunksWithGapCompoundKeyFixture : public unittest::Test {
protected:
    CollectionMetadata makeCollectionMetadata() const {
        return makeCollectionMetadataImpl(
            KeyPattern(BSON("a" << 1 << "b" << 1)),
            {std::make_pair(BSON("a" << 10 << "b" << 0), BSON("a" << 20 << "b" << 0)),
             std::make_pair(BSON("a" << 30 << "b" << 0), BSON("a" << 40 << "b" << 0))},
            false);
    }
};

TEST_F(TwoChunksWithGapCompoundKeyFixture, ChunkGapOrphanedDataRanges) {
    ConstructedRangeMap pending;

    auto keyRange =
        makeCollectionMetadata().getNextOrphanRange(pending, makeCollectionMetadata().getMinKey());
    ASSERT(keyRange);
    ASSERT(keyRange->getMin().woCompare(makeCollectionMetadata().getMinKey()) == 0);
    ASSERT(keyRange->getMax().woCompare(BSON("a" << 10 << "b" << 0)) == 0);

    keyRange = makeCollectionMetadata().getNextOrphanRange(pending, keyRange->getMax());
    ASSERT(keyRange);
    ASSERT(keyRange->getMin().woCompare(BSON("a" << 20 << "b" << 0)) == 0);
    ASSERT(keyRange->getMax().woCompare(BSON("a" << 30 << "b" << 0)) == 0);

    keyRange = makeCollectionMetadata().getNextOrphanRange(pending, keyRange->getMax());
    ASSERT(keyRange);
    ASSERT(keyRange->getMin().woCompare(BSON("a" << 40 << "b" << 0)) == 0);
    ASSERT(keyRange->getMax().woCompare(makeCollectionMetadata().getMaxKey()) == 0);

    ASSERT(!makeCollectionMetadata().getNextOrphanRange(pending, keyRange->getMax()));
}

/**
 * Fixture with chunk containing:
 * [min->10) , [10->20) , <gap> , [30->max)
 */
class ThreeChunkWithRangeGapFixture : public unittest::Test {
protected:
    CollectionMetadata makeCollectionMetadata() const {
        return makeCollectionMetadataImpl(KeyPattern(BSON("a" << 1)),
                                          {std::make_pair(BSON("a" << MINKEY), BSON("a" << 10)),
                                           std::make_pair(BSON("a" << 10), BSON("a" << 20)),
                                           std::make_pair(BSON("a" << 30), BSON("a" << MAXKEY))},
                                          false);
    }
};

TEST_F(ThreeChunkWithRangeGapFixture, GetNextChunkMatch) {
    auto metadata(makeCollectionMetadata());

    ChunkType chunk;

    ASSERT(metadata.getNextChunk(BSON("a" << MINKEY), &chunk));
    ASSERT_BSONOBJ_EQ(metadata.getMinKey(), chunk.getMin());
    ASSERT_BSONOBJ_EQ(BSON("a" << MINKEY), chunk.getMin());
    ASSERT_BSONOBJ_EQ(BSON("a" << 10), chunk.getMax());

    ASSERT(metadata.getNextChunk(BSON("a" << 10), &chunk));
    ASSERT_BSONOBJ_EQ(BSON("a" << 10), chunk.getMin());
    ASSERT_BSONOBJ_EQ(BSON("a" << 20), chunk.getMax());

    ASSERT(metadata.getNextChunk(BSON("a" << 30), &chunk));
    ASSERT_BSONOBJ_EQ(BSON("a" << 30), chunk.getMin());
    ASSERT_BSONOBJ_EQ(metadata.getMaxKey(), chunk.getMax());
    ASSERT_BSONOBJ_EQ(BSON("a" << MAXKEY), chunk.getMax());
}

TEST_F(ThreeChunkWithRangeGapFixture, KeyBelongsToMe) {
    ASSERT(makeCollectionMetadata().keyBelongsToMe(BSON("a" << 5)));
    ASSERT(makeCollectionMetadata().keyBelongsToMe(BSON("a" << 10)));
    ASSERT(makeCollectionMetadata().keyBelongsToMe(BSON("a" << 30)));
    ASSERT(makeCollectionMetadata().keyBelongsToMe(BSON("a" << 40)));

    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSON("a" << 20)));
    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSON("a" << 25)));
    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSON("a" << MAXKEY)));

    ASSERT(!makeCollectionMetadata().keyBelongsToMe(BSONObj()));
}

TEST_F(ThreeChunkWithRangeGapFixture, GetNextChunkFromBeginning) {
    ChunkType nextChunk;
    ASSERT(makeCollectionMetadata().getNextChunk(makeCollectionMetadata().getMinKey(), &nextChunk));
    ASSERT_EQUALS(0, nextChunk.getMin().woCompare(BSON("a" << MINKEY)));
    ASSERT_EQUALS(0, nextChunk.getMax().woCompare(BSON("a" << 10)));
}

TEST_F(ThreeChunkWithRangeGapFixture, GetNextChunkFromMiddle) {
    ChunkType nextChunk;
    ASSERT(makeCollectionMetadata().getNextChunk(BSON("a" << 20), &nextChunk));
    ASSERT_EQUALS(0, nextChunk.getMin().woCompare(BSON("a" << 30)));
    ASSERT_EQUALS(0, nextChunk.getMax().woCompare(BSON("a" << MAXKEY)));
}

TEST_F(ThreeChunkWithRangeGapFixture, GetNextChunkFromLast) {
    ChunkType nextChunk;
    ASSERT(makeCollectionMetadata().getNextChunk(BSON("a" << 30), &nextChunk));
    ASSERT_EQUALS(0, nextChunk.getMin().woCompare(BSON("a" << 30)));
    ASSERT_EQUALS(0, nextChunk.getMax().woCompare(BSON("a" << MAXKEY)));
}

/**
 * Fixture with single chunk containing:
 * [10->20)
 */
class StaleChunkFixture : public unittest::Test {
protected:
    CollectionMetadata makeCollectionMetadata() const {
        return makeCollectionMetadataImpl(
            KeyPattern(BSON("a" << 1)), {std::make_pair(BSON("a" << 10), BSON("a" << 20))}, true);
    }
};

TEST_F(StaleChunkFixture, KeyBelongsToMe) {
    ASSERT_THROWS_CODE(makeCollectionMetadata().keyBelongsToMe(BSON("a" << 10)),
                       AssertionException,
                       ErrorCodes::StaleChunkHistory);

    ASSERT_THROWS_CODE(makeCollectionMetadata().keyBelongsToMe(BSON("a" << 0)),
                       AssertionException,
                       ErrorCodes::StaleChunkHistory);
}

}  // namespace
}  // namespace mongo