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
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
|
/**
* 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.
*/
#define MONGO_LOGV2_DEFAULT_COMPONENT ::mongo::logv2::LogComponent::kDefault
#include "mongo/platform/basic.h"
#include "mongo/db/concurrency/locker_noop.h"
#include "mongo/db/pipeline/aggregation_request_helper.h"
#include "mongo/s/catalog/type_chunk.h"
#include "mongo/s/catalog/type_collection.h"
#include "mongo/s/catalog/type_database.h"
#include "mongo/s/catalog_cache.h"
#include "mongo/s/catalog_cache_test_fixture.h"
#include "mongo/s/database_version.h"
#include "mongo/unittest/death_test.h"
namespace mongo {
namespace {
using executor::RemoteCommandRequest;
using unittest::assertGet;
const NamespaceString kNss("TestDB", "TestColl");
class CatalogCacheRefreshTest : public CatalogCacheTestFixture {
protected:
void setUp() override {
CatalogCacheTestFixture::setUp();
setupNShards(2);
}
void expectGetDatabase() {
expectFindSendBSONObjVector(kConfigHostAndPort, [&]() {
DatabaseType db(
kNss.db().toString(), {"0"}, true, DatabaseVersion(UUID::gen(), Timestamp(1, 1)));
return std::vector<BSONObj>{db.toBSON()};
}());
}
void expectGetCollection(OID epoch,
Timestamp timestamp,
const ShardKeyPattern& shardKeyPattern) {
expectFindSendBSONObjVector(kConfigHostAndPort, [&]() {
return std::vector<BSONObj>{
getDefaultCollectionType(epoch, timestamp, shardKeyPattern).toBSON()};
}());
}
void expectCollectionAndChunksAggregationWithReshardingFields(
OID epoch,
Timestamp timestamp,
const ShardKeyPattern& shardKeyPattern,
UUID reshardingUUID,
const std::vector<ChunkType>& chunks) {
expectFindSendBSONObjVector(kConfigHostAndPort, [&]() {
auto collType = getDefaultCollectionType(epoch, timestamp, shardKeyPattern);
TypeCollectionReshardingFields reshardingFields;
reshardingFields.setReshardingUUID(reshardingUUID);
collType.setReshardingFields(std::move(reshardingFields));
std::vector<BSONObj> aggResult{collType.toBSON()};
std::transform(
chunks.begin(), chunks.end(), std::back_inserter(aggResult), [](const auto& chunk) {
return BSON("chunks" << chunk.toConfigBSON());
});
return aggResult;
}());
}
CollectionType getDefaultCollectionType(OID epoch,
Timestamp timestamp,
const ShardKeyPattern& shardKeyPattern) {
CollectionType collType(kNss, epoch, timestamp, Date_t::now(), UUID::gen());
collType.setKeyPattern(shardKeyPattern.toBSON());
collType.setUnique(false);
return collType;
}
};
TEST_F(CatalogCacheRefreshTest, FullLoad) {
const OID epoch = OID::gen();
const Timestamp timestamp(1);
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
const UUID reshardingUUID = UUID::gen();
auto future = scheduleRoutingInfoUnforcedRefresh(kNss);
expectGetDatabase();
ChunkVersion version(1, 0, epoch, timestamp);
ChunkType chunk1(reshardingUUID,
{shardKeyPattern.getKeyPattern().globalMin(), BSON("_id" << -100)},
version,
{"0"});
chunk1.setName(OID::gen());
version.incMinor();
ChunkType chunk2(reshardingUUID, {BSON("_id" << -100), BSON("_id" << 0)}, version, {"1"});
chunk2.setName(OID::gen());
version.incMinor();
ChunkType chunk3(reshardingUUID, {BSON("_id" << 0), BSON("_id" << 100)}, version, {"0"});
chunk3.setName(OID::gen());
version.incMinor();
ChunkType chunk4(reshardingUUID,
{BSON("_id" << 100), shardKeyPattern.getKeyPattern().globalMax()},
version,
{"1"});
chunk4.setName(OID::gen());
version.incMinor();
expectCollectionAndChunksAggregationWithReshardingFields(
epoch, timestamp, shardKeyPattern, reshardingUUID, {chunk1, chunk2, chunk3, chunk4});
auto cm = *future.default_timed_get();
ASSERT(cm.isSharded());
ASSERT_EQ(4, cm.numChunks());
ASSERT_EQ(reshardingUUID, cm.getReshardingFields()->getReshardingUUID());
}
TEST_F(CatalogCacheRefreshTest, NoLoadIfShardNotMarkedStaleInOperationContext) {
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
auto initialRoutingInfo(
makeChunkManager(kNss, shardKeyPattern, nullptr, true, {BSON("_id" << 0)}));
ASSERT_EQ(2, initialRoutingInfo.numChunks());
auto futureNoRefresh = scheduleRoutingInfoUnforcedRefresh(kNss);
auto cm = *futureNoRefresh.default_timed_get();
ASSERT(cm.isSharded());
ASSERT_EQ(2, cm.numChunks());
}
class MockLockerAlwaysReportsToBeLocked : public LockerNoop {
public:
using LockerNoop::LockerNoop;
bool isLocked() const final {
return true;
}
};
DEATH_TEST_F(CatalogCacheRefreshTest, ShouldFailToRefreshWhenLocksAreHeld, "Invariant") {
operationContext()->setLockState(std::make_unique<MockLockerAlwaysReportsToBeLocked>());
scheduleRoutingInfoUnforcedRefresh(kNss);
}
TEST_F(CatalogCacheRefreshTest, DatabaseNotFound) {
auto future = scheduleRoutingInfoUnforcedRefresh(kNss);
// Return an empty database (need to return it twice because for missing databases, the
// CatalogClient tries twice)
expectFindSendBSONObjVector(kConfigHostAndPort, {});
expectFindSendBSONObjVector(kConfigHostAndPort, {});
try {
auto cm = *future.default_timed_get();
FAIL(str::stream() << "Returning no database did not fail and returned " << cm.toString());
} catch (const DBException& ex) {
ASSERT_EQ(ErrorCodes::NamespaceNotFound, ex.code());
}
}
TEST_F(CatalogCacheRefreshTest, DatabaseBSONCorrupted) {
auto future = scheduleRoutingInfoUnforcedRefresh(kNss);
// Return a corrupted database entry
expectFindSendBSONObjVector(kConfigHostAndPort,
{BSON(
"BadValue"
<< "This value should not be in a database config document")});
try {
auto cm = *future.default_timed_get();
FAIL(str::stream() << "Returning corrupted database entry did not fail and returned "
<< cm.toString());
} catch (const DBException& ex) {
ASSERT_EQ(ErrorCodes::NoSuchKey, ex.code());
}
}
TEST_F(CatalogCacheRefreshTest, CollectionNotFound) {
auto future = scheduleRoutingInfoUnforcedRefresh(kNss);
expectGetDatabase();
// Return an empty collection
expectFindSendBSONObjVector(kConfigHostAndPort, {});
auto cm = *future.default_timed_get();
ASSERT(!cm.isSharded());
ASSERT_EQ(ShardId{"0"}, cm.dbPrimary());
}
TEST_F(CatalogCacheRefreshTest, CollectionBSONCorrupted) {
auto future = scheduleRoutingInfoUnforcedRefresh(kNss);
expectGetDatabase();
// Return a corrupted collection entry
expectFindSendBSONObjVector(
kConfigHostAndPort,
{BSON("BadValue"
<< "This value should not be in a collection config document")});
try {
auto cm = *future.default_timed_get();
FAIL(str::stream() << "Returning corrupted collection entry did not fail and returned "
<< cm.toString());
} catch (const DBException& ex) {
constexpr int kParseError = 40414;
ASSERT_EQ(ErrorCodes::Error(kParseError), ex.code());
}
}
TEST_F(CatalogCacheRefreshTest, FullLoadNoChunksFound) {
const OID epoch = OID::gen();
const Timestamp timestamp(1);
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
auto future = scheduleRoutingInfoUnforcedRefresh(kNss);
expectGetDatabase();
// Return no chunks three times, which is how frequently the catalog cache retries
expectFindSendBSONObjVector(kConfigHostAndPort, [&] {
const auto coll = getDefaultCollectionType(epoch, timestamp, shardKeyPattern);
return std::vector<BSONObj>{coll.toBSON()};
}());
expectFindSendBSONObjVector(kConfigHostAndPort, [&] {
const auto coll = getDefaultCollectionType(epoch, timestamp, shardKeyPattern);
return std::vector<BSONObj>{coll.toBSON()};
}());
expectFindSendBSONObjVector(kConfigHostAndPort, [&] {
const auto coll = getDefaultCollectionType(epoch, timestamp, shardKeyPattern);
return std::vector<BSONObj>{coll.toBSON()};
}());
try {
auto cm = *future.default_timed_get();
FAIL(str::stream() << "Returning no chunks for collection did not fail and returned "
<< cm.toString());
} catch (const DBException& ex) {
ASSERT_EQ(ErrorCodes::ConflictingOperationInProgress, ex.code());
}
}
TEST_F(CatalogCacheRefreshTest, IncrementalLoadNoChunksFound) {
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
auto initialRoutingInfo(makeChunkManager(kNss, shardKeyPattern, nullptr, true, {}));
const OID epoch = initialRoutingInfo.getVersion().epoch();
const Timestamp timestamp = initialRoutingInfo.getVersion().getTimestamp();
ASSERT_EQ(1, initialRoutingInfo.numChunks());
auto future = scheduleRoutingInfoForcedRefresh(kNss);
// Return no chunks three times, which is how frequently the catalog cache retries
expectFindSendBSONObjVector(kConfigHostAndPort, [&] {
const auto coll = getDefaultCollectionType(epoch, timestamp, shardKeyPattern);
return std::vector<BSONObj>{coll.toBSON()};
}());
expectFindSendBSONObjVector(kConfigHostAndPort, [&] {
const auto coll = getDefaultCollectionType(epoch, timestamp, shardKeyPattern);
return std::vector<BSONObj>{coll.toBSON()};
}());
expectFindSendBSONObjVector(kConfigHostAndPort, [&] {
const auto coll = getDefaultCollectionType(epoch, timestamp, shardKeyPattern);
return std::vector<BSONObj>{coll.toBSON()};
}());
try {
auto cm = *future.default_timed_get();
FAIL(str::stream() << "Returning no chunks for collection did not fail and returned "
<< cm.toString());
} catch (const DBException& ex) {
ASSERT_EQ(ErrorCodes::ConflictingOperationInProgress, ex.code());
}
}
TEST_F(CatalogCacheRefreshTest, ChunksBSONCorrupted) {
const OID epoch = OID::gen();
const Timestamp timestamp(1);
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
auto future = scheduleRoutingInfoUnforcedRefresh(kNss);
expectGetDatabase();
// Return no chunks three times, which is how frequently the catalog cache retries
expectFindSendBSONObjVector(kConfigHostAndPort, [&] {
const auto coll = getDefaultCollectionType(epoch, timestamp, shardKeyPattern);
const auto chunk1 =
ChunkType(coll.getUuid(),
{shardKeyPattern.getKeyPattern().globalMin(), BSON("_id" << 0)},
ChunkVersion(1, 0, epoch, Timestamp(1, 1)),
{"0"});
return std::vector<BSONObj>{/* collection */
coll.toBSON(),
/* chunks */
coll.toBSON().addFields(
BSON("chunks" << chunk1.toConfigBSON())),
BSON("BadValue"
<< "This value should not be in a chunk config document")};
}());
try {
auto cm = *future.default_timed_get();
FAIL(str::stream() << "Returning no chunks for collection did not fail and returned "
<< cm.toString());
} catch (const DBException& ex) {
ASSERT_EQ(ErrorCodes::NoSuchKey, ex.code());
}
}
TEST_F(CatalogCacheRefreshTest, FullLoadMissingChunkWithLowestVersion) {
const OID epoch = OID::gen();
const UUID uuid = UUID::gen();
const Timestamp timestamp(1, 1);
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
auto future = scheduleRoutingInfoUnforcedRefresh(kNss);
expectGetDatabase();
const auto incompleteChunks = [&]() {
ChunkVersion version(1, 0, epoch, timestamp);
// Chunk from (MinKey, -100) is missing (as if someone is dropping the collection
// concurrently) and has the lowest version.
version.incMinor();
ChunkType chunk2(uuid, {BSON("_id" << -100), BSON("_id" << 0)}, version, {"1"});
chunk2.setName(OID::gen());
version.incMinor();
ChunkType chunk3(uuid, {BSON("_id" << 0), BSON("_id" << 100)}, version, {"0"});
chunk3.setName(OID::gen());
version.incMinor();
ChunkType chunk4(uuid,
{BSON("_id" << 100), shardKeyPattern.getKeyPattern().globalMax()},
version,
{"1"});
chunk4.setName(OID::gen());
version.incMinor();
return std::vector<ChunkType>{chunk2, chunk3, chunk4};
}();
// Return incomplete set of chunks three times, which is how frequently the catalog cache
// retries
expectCollectionAndChunksAggregation(
kNss, epoch, timestamp, uuid, shardKeyPattern, incompleteChunks);
expectCollectionAndChunksAggregation(
kNss, epoch, timestamp, uuid, shardKeyPattern, incompleteChunks);
expectCollectionAndChunksAggregation(
kNss, epoch, timestamp, uuid, shardKeyPattern, incompleteChunks);
try {
auto cm = *future.default_timed_get();
FAIL(
str::stream() << "Returning incomplete chunks for collection did not fail and returned "
<< cm.toString());
} catch (const DBException& ex) {
ASSERT_EQ(ErrorCodes::ConflictingOperationInProgress, ex.code());
}
}
TEST_F(CatalogCacheRefreshTest, FullLoadMissingChunkWithHighestVersion) {
const OID epoch = OID::gen();
const UUID uuid = UUID::gen();
const Timestamp timestamp(1, 1);
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
auto future = scheduleRoutingInfoUnforcedRefresh(kNss);
expectGetDatabase();
const auto incompleteChunks = [&]() {
ChunkVersion version(1, 0, epoch, timestamp);
// Chunk from (MinKey, -100) is missing (as if someone is dropping the collection
// concurrently) and has the higest version.
version.incMinor();
ChunkType chunk2(uuid, {BSON("_id" << -100), BSON("_id" << 0)}, version, {"1"});
chunk2.setName(OID::gen());
version.incMinor();
ChunkType chunk3(uuid, {BSON("_id" << 0), BSON("_id" << 100)}, version, {"0"});
chunk3.setName(OID::gen());
version.incMinor();
ChunkType chunk4(uuid,
{BSON("_id" << 100), shardKeyPattern.getKeyPattern().globalMax()},
version,
{"1"});
chunk4.setName(OID::gen());
version.incMinor();
return std::vector<ChunkType>{chunk2, chunk3, chunk4};
}();
// Return incomplete set of chunks three times, which is how frequently the catalog cache
// retries
expectCollectionAndChunksAggregation(
kNss, epoch, timestamp, uuid, shardKeyPattern, incompleteChunks);
expectCollectionAndChunksAggregation(
kNss, epoch, timestamp, uuid, shardKeyPattern, incompleteChunks);
expectCollectionAndChunksAggregation(
kNss, epoch, timestamp, uuid, shardKeyPattern, incompleteChunks);
try {
auto cm = *future.default_timed_get();
FAIL(
str::stream() << "Returning incomplete chunks for collection did not fail and returned "
<< cm.toString());
} catch (const DBException& ex) {
ASSERT_EQ(ErrorCodes::ConflictingOperationInProgress, ex.code());
}
}
TEST_F(CatalogCacheRefreshTest, IncrementalLoadMissingChunkWithLowestVersion) {
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
auto initialRoutingInfo(makeChunkManager(kNss, shardKeyPattern, nullptr, true, {}));
const OID epoch = initialRoutingInfo.getVersion().epoch();
const UUID uuid = *initialRoutingInfo.getUUID();
const auto timestamp = initialRoutingInfo.getVersion().getTimestamp();
ASSERT_EQ(1, initialRoutingInfo.numChunks());
auto future = scheduleRoutingInfoIncrementalRefresh(kNss);
const auto incompleteChunks = [&]() {
ChunkVersion version(1, 0, epoch, timestamp);
// Chunk from (MinKey, -100) is missing (as if someone is dropping the collection
// concurrently) and has the lowest version.
version.incMinor();
ChunkType chunk2(uuid, {BSON("_id" << -100), BSON("_id" << 0)}, version, {"1"});
chunk2.setName(OID::gen());
version.incMinor();
ChunkType chunk3(uuid, {BSON("_id" << 0), BSON("_id" << 100)}, version, {"0"});
chunk3.setName(OID::gen());
version.incMinor();
ChunkType chunk4(uuid,
{BSON("_id" << 100), shardKeyPattern.getKeyPattern().globalMax()},
version,
{"1"});
chunk4.setName(OID::gen());
version.incMinor();
return std::vector<ChunkType>{chunk2, chunk3, chunk4};
}();
// Return incomplete set of chunks three times, which is how frequently the catalog cache
// retries
expectCollectionAndChunksAggregation(
kNss, epoch, timestamp, UUID::gen(), shardKeyPattern, incompleteChunks);
expectCollectionAndChunksAggregation(
kNss, epoch, timestamp, UUID::gen(), shardKeyPattern, incompleteChunks);
expectCollectionAndChunksAggregation(
kNss, epoch, timestamp, UUID::gen(), shardKeyPattern, incompleteChunks);
try {
auto cm = *future.default_timed_get();
FAIL(
str::stream() << "Returning incomplete chunks for collection did not fail and returned "
<< cm.toString());
} catch (const DBException& ex) {
ASSERT_EQ(ErrorCodes::ConflictingOperationInProgress, ex.code());
}
}
TEST_F(CatalogCacheRefreshTest, IncrementalLoadMissingChunkWithHighestVersion) {
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
auto initialRoutingInfo(makeChunkManager(kNss, shardKeyPattern, nullptr, true, {}));
const OID epoch = initialRoutingInfo.getVersion().epoch();
const UUID uuid = *initialRoutingInfo.getUUID();
const auto timestamp = initialRoutingInfo.getVersion().getTimestamp();
ASSERT_EQ(1, initialRoutingInfo.numChunks());
auto future = scheduleRoutingInfoIncrementalRefresh(kNss);
const auto incompleteChunks = [&]() {
ChunkVersion version(1, 0, epoch, timestamp);
// Chunk from (MinKey, -100) is missing (as if someone is dropping the collection
// concurrently) and has the higest version.
ChunkType chunk2(uuid, {BSON("_id" << -100), BSON("_id" << 0)}, version, {"1"});
chunk2.setName(OID::gen());
version.incMinor();
ChunkType chunk3(uuid, {BSON("_id" << 0), BSON("_id" << 100)}, version, {"0"});
chunk3.setName(OID::gen());
version.incMinor();
ChunkType chunk4(uuid,
{BSON("_id" << 100), shardKeyPattern.getKeyPattern().globalMax()},
version,
{"1"});
chunk4.setName(OID::gen());
version.incMinor();
return std::vector<ChunkType>{chunk2, chunk3, chunk4};
}();
// Return incomplete set of chunks three times, which is how frequently the catalog cache
// retries
expectCollectionAndChunksAggregation(
kNss, epoch, timestamp, UUID::gen(), shardKeyPattern, incompleteChunks);
expectCollectionAndChunksAggregation(
kNss, epoch, timestamp, UUID::gen(), shardKeyPattern, incompleteChunks);
expectCollectionAndChunksAggregation(
kNss, epoch, timestamp, UUID::gen(), shardKeyPattern, incompleteChunks);
try {
auto cm = *future.default_timed_get();
FAIL(
str::stream() << "Returning incomplete chunks for collection did not fail and returned "
<< cm.toString());
} catch (const DBException& ex) {
ASSERT_EQ(ErrorCodes::ConflictingOperationInProgress, ex.code());
}
}
TEST_F(CatalogCacheRefreshTest, ChunkEpochChangeDuringIncrementalLoadRecoveryAfterRetry) {
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
auto initialRoutingInfo(makeChunkManager(kNss, shardKeyPattern, nullptr, true, {}));
ASSERT_EQ(1, initialRoutingInfo.numChunks());
setupNShards(2);
auto future = scheduleRoutingInfoIncrementalRefresh(kNss);
ChunkVersion oldVersion = initialRoutingInfo.getVersion();
const OID newEpoch = OID::gen();
const Timestamp newTimestamp = Timestamp(2);
// On the first attempt, return set of chunks, one of which has different epoch. This simulates
// the situation where a collection existed with epoch0, we started a refresh for that
// collection, the cursor yielded and while it yielded another node dropped the collection and
// recreated it with different epoch and chunks.
onFindCommand([&](const RemoteCommandRequest& request) {
const auto opMsg = OpMsgRequest::fromDBAndBody(request.dbname, request.cmdObj);
const auto aggRequest = unittest::assertGet(
aggregation_request_helper::parseFromBSONForTests(kNss, opMsg.body));
const auto& pipeline = aggRequest.getPipeline();
ASSERT_BSONOBJ_EQ(
pipeline[1]["$unionWith"]["pipeline"].Array()[1]["$match"]["lastmodEpoch"].Obj(),
BSON("$eq" << oldVersion.epoch()));
ASSERT_BSONOBJ_EQ(
pipeline[2]["$unionWith"]["pipeline"].Array()[1]["$match"]["lastmodEpoch"].Obj(),
BSON("$ne" << oldVersion.epoch()));
const auto coll = getDefaultCollectionType(
oldVersion.epoch(), oldVersion.getTimestamp(), shardKeyPattern);
const auto collBSON = coll.toBSON();
oldVersion.incMajor();
ChunkType chunk1(coll.getUuid(),
{shardKeyPattern.getKeyPattern().globalMin(), BSON("_id" << 0)},
oldVersion,
{"0"});
chunk1.setName(OID::gen());
// "Yield" happens here with drop and recreate in between. This is the "last" chunk from the
// recreated collection.
ChunkType chunk3(coll.getUuid(),
{BSON("_id" << 100), shardKeyPattern.getKeyPattern().globalMax()},
ChunkVersion(5, 2, newEpoch, newTimestamp),
{"1"});
chunk3.setName(OID::gen());
const auto chunk1BSON = BSON("chunks" << chunk1.toConfigBSON());
const auto chunk3BSON = BSON("chunks" << chunk3.toConfigBSON());
return std::vector<BSONObj>{collBSON, chunk1BSON, chunk3BSON};
});
// On the second retry attempt, return the correct set of chunks from the recreated collection
ChunkVersion newVersion(5, 0, newEpoch, newTimestamp);
onFindCommand([&](const RemoteCommandRequest& request) {
const auto opMsg = OpMsgRequest::fromDBAndBody(request.dbname, request.cmdObj);
const auto aggRequest = unittest::assertGet(
aggregation_request_helper::parseFromBSONForTests(kNss, opMsg.body));
const auto& pipeline = aggRequest.getPipeline();
ASSERT_BSONOBJ_EQ(
pipeline[1]["$unionWith"]["pipeline"].Array()[1]["$match"]["lastmodEpoch"].Obj(),
BSON("$eq" << oldVersion.epoch()));
ASSERT_BSONOBJ_EQ(
pipeline[2]["$unionWith"]["pipeline"].Array()[1]["$match"]["lastmodEpoch"].Obj(),
BSON("$ne" << oldVersion.epoch()));
const auto coll = getDefaultCollectionType(newEpoch, newTimestamp, shardKeyPattern);
const auto collBSON =
getDefaultCollectionType(newEpoch, newTimestamp, shardKeyPattern).toBSON();
ChunkType chunk1(coll.getUuid(),
{shardKeyPattern.getKeyPattern().globalMin(), BSON("_id" << 0)},
newVersion,
{"0"});
chunk1.setName(OID::gen());
newVersion.incMinor();
ChunkType chunk2(coll.getUuid(), {BSON("_id" << 0), BSON("_id" << 100)}, newVersion, {"0"});
chunk2.setName(OID::gen());
newVersion.incMinor();
ChunkType chunk3(coll.getUuid(),
{BSON("_id" << 100), shardKeyPattern.getKeyPattern().globalMax()},
newVersion,
{"1"});
chunk3.setName(OID::gen());
const auto chunk1BSON = BSON("chunks" << chunk1.toConfigBSON());
const auto chunk2BSON = BSON("chunks" << chunk2.toConfigBSON());
const auto chunk3BSON = BSON("chunks" << chunk3.toConfigBSON());
return std::vector<BSONObj>{collBSON, chunk1BSON, chunk2BSON, chunk3BSON};
});
auto cm = *future.default_timed_get();
ASSERT(cm.isSharded());
ASSERT_EQ(3, cm.numChunks());
ASSERT_EQ(newVersion, cm.getVersion());
ASSERT_EQ(ChunkVersion(5, 1, newVersion.epoch(), newVersion.getTimestamp()),
cm.getVersion({"0"}));
ASSERT_EQ(ChunkVersion(5, 2, newVersion.epoch(), newVersion.getTimestamp()),
cm.getVersion({"1"}));
}
TEST_F(CatalogCacheRefreshTest, IncrementalLoadAfterCollectionEpochChange) {
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
auto initialRoutingInfo(makeChunkManager(kNss, shardKeyPattern, nullptr, true, {}));
ASSERT_EQ(1, initialRoutingInfo.numChunks());
setupNShards(2);
auto future = scheduleRoutingInfoIncrementalRefresh(kNss);
ChunkVersion oldVersion = initialRoutingInfo.getVersion();
ChunkVersion newVersion(1, 0, OID::gen(), Timestamp(2));
const UUID uuid = *initialRoutingInfo.getUUID();
// Return collection with a different epoch and a set of chunks, which represent a split
onFindCommand([&](const RemoteCommandRequest& request) {
const auto opMsg = OpMsgRequest::fromDBAndBody(request.dbname, request.cmdObj);
const auto aggRequest = unittest::assertGet(
aggregation_request_helper::parseFromBSONForTests(kNss, opMsg.body));
const auto& pipeline = aggRequest.getPipeline();
ASSERT_BSONOBJ_EQ(
pipeline[1]["$unionWith"]["pipeline"].Array()[1]["$match"]["lastmodEpoch"].Obj(),
BSON("$eq" << oldVersion.epoch()));
ASSERT_BSONOBJ_EQ(
pipeline[2]["$unionWith"]["pipeline"].Array()[1]["$match"]["lastmodEpoch"].Obj(),
BSON("$ne" << oldVersion.epoch()));
const auto collBSON =
getDefaultCollectionType(newVersion.epoch(), newVersion.getTimestamp(), shardKeyPattern)
.toBSON();
ChunkType chunk1(uuid,
{shardKeyPattern.getKeyPattern().globalMin(), BSON("_id" << 0)},
newVersion,
{"0"});
chunk1.setName(OID::gen());
newVersion.incMinor();
ChunkType chunk2(uuid,
{BSON("_id" << 0), shardKeyPattern.getKeyPattern().globalMax()},
newVersion,
{"1"});
chunk2.setName(OID::gen());
const auto chunk1BSON = BSON("chunks" << chunk1.toConfigBSON());
const auto chunk2BSON = BSON("chunks" << chunk2.toConfigBSON());
return std::vector<BSONObj>{collBSON, chunk1BSON, chunk2BSON};
});
auto cm = *future.default_timed_get();
ASSERT(cm.isSharded());
ASSERT_EQ(2, cm.numChunks());
ASSERT_EQ(newVersion, cm.getVersion());
ASSERT_EQ(ChunkVersion(1, 0, newVersion.epoch(), newVersion.getTimestamp()),
cm.getVersion({"0"}));
ASSERT_EQ(ChunkVersion(1, 1, newVersion.epoch(), newVersion.getTimestamp()),
cm.getVersion({"1"}));
}
TEST_F(CatalogCacheRefreshTest, IncrementalLoadAfterSplit) {
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
auto initialRoutingInfo(makeChunkManager(kNss, shardKeyPattern, nullptr, true, {}));
ASSERT_EQ(1, initialRoutingInfo.numChunks());
ChunkVersion version = initialRoutingInfo.getVersion();
auto future = scheduleRoutingInfoIncrementalRefresh(kNss);
// Return set of chunks, which represent a split
onFindCommand([&](const RemoteCommandRequest& request) {
const auto opMsg = OpMsgRequest::fromDBAndBody(request.dbname, request.cmdObj);
const auto aggRequest = unittest::assertGet(
aggregation_request_helper::parseFromBSONForTests(kNss, opMsg.body));
const auto& pipeline = aggRequest.getPipeline();
ASSERT_BSONOBJ_EQ(
pipeline[1]["$unionWith"]["pipeline"]
.Array()[2]["$lookup"]["pipeline"]
.Array()[1]["$match"]["lastmod"]
.Obj(),
BSON("$gte" << Timestamp(version.majorVersion(), version.minorVersion())));
ASSERT_BSONOBJ_EQ(
pipeline[1]["$unionWith"]["pipeline"].Array()[1]["$match"]["lastmodEpoch"].Obj(),
BSON("$eq" << version.epoch()));
const auto coll =
getDefaultCollectionType(version.epoch(), version.getTimestamp(), shardKeyPattern);
const auto collBSON = coll.toBSON();
version.incMajor();
ChunkType chunk1(coll.getUuid(),
{shardKeyPattern.getKeyPattern().globalMin(), BSON("_id" << 0)},
version,
{"0"});
chunk1.setName(OID::gen());
version.incMinor();
ChunkType chunk2(coll.getUuid(),
{BSON("_id" << 0), shardKeyPattern.getKeyPattern().globalMax()},
version,
{"0"});
chunk2.setName(OID::gen());
const auto chunk1BSON = BSON("chunks" << chunk1.toConfigBSON());
const auto chunk2BSON = BSON("chunks" << chunk2.toConfigBSON());
return std::vector<BSONObj>{collBSON, chunk1BSON, chunk2BSON};
});
auto cm = *future.default_timed_get();
ASSERT(cm.isSharded());
ASSERT_EQ(2, cm.numChunks());
ASSERT_EQ(version, cm.getVersion());
ASSERT_EQ(version, cm.getVersion({"0"}));
ASSERT_EQ(ChunkVersion(0, 0, version.epoch(), version.getTimestamp()), cm.getVersion({"1"}));
}
TEST_F(CatalogCacheRefreshTest, IncrementalLoadAfterMoveWithReshardingFieldsAdded) {
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
const UUID reshardingUUID = UUID::gen();
auto initialRoutingInfo(
makeChunkManager(kNss, shardKeyPattern, nullptr, true, {BSON("_id" << 0)}));
ASSERT_EQ(2, initialRoutingInfo.numChunks());
ASSERT(boost::none == initialRoutingInfo.getReshardingFields());
ChunkVersion version = initialRoutingInfo.getVersion();
const UUID uuid = *initialRoutingInfo.getUUID();
auto future = scheduleRoutingInfoIncrementalRefresh(kNss);
ChunkVersion expectedDestShardVersion;
// Return set of chunks, which represent a move
version.incMajor();
expectedDestShardVersion = version;
ChunkType chunk1(
uuid, {shardKeyPattern.getKeyPattern().globalMin(), BSON("_id" << 0)}, version, {"1"});
chunk1.setName(OID::gen());
version.incMinor();
ChunkType chunk2(
uuid, {BSON("_id" << 0), shardKeyPattern.getKeyPattern().globalMax()}, version, {"0"});
chunk2.setName(OID::gen());
expectCollectionAndChunksAggregationWithReshardingFields(
version.epoch(), version.getTimestamp(), shardKeyPattern, reshardingUUID, {chunk1, chunk2});
auto cm = *future.default_timed_get();
ASSERT(cm.isSharded());
ASSERT_EQ(2, cm.numChunks());
ASSERT_EQ(reshardingUUID, cm.getReshardingFields()->getReshardingUUID());
ASSERT_EQ(version, cm.getVersion());
ASSERT_EQ(version, cm.getVersion({"0"}));
ASSERT_EQ(expectedDestShardVersion, cm.getVersion({"1"}));
}
TEST_F(CatalogCacheRefreshTest, IncrementalLoadAfterMoveLastChunkWithReshardingFieldsRemoved) {
const ShardKeyPattern shardKeyPattern(BSON("_id" << 1));
const UUID reshardingUUID = UUID::gen();
TypeCollectionReshardingFields reshardingFields;
reshardingFields.setReshardingUUID(reshardingUUID);
auto initialRoutingInfo(
makeChunkManager(kNss, shardKeyPattern, nullptr, true, {}, reshardingFields));
ASSERT_EQ(1, initialRoutingInfo.numChunks());
ASSERT_EQ(reshardingUUID, initialRoutingInfo.getReshardingFields()->getReshardingUUID());
setupNShards(2);
ChunkVersion version = initialRoutingInfo.getVersion();
auto future = scheduleRoutingInfoIncrementalRefresh(kNss);
// The collection type won't have resharding fields this time.
// Return set of chunks, which represent a move
version.incMajor();
ChunkType chunk1(
*initialRoutingInfo.getUUID(),
{shardKeyPattern.getKeyPattern().globalMin(), shardKeyPattern.getKeyPattern().globalMax()},
version,
{"1"});
chunk1.setName(OID::gen());
expectCollectionAndChunksAggregation(
kNss, version.epoch(), version.getTimestamp(), UUID::gen(), shardKeyPattern, {chunk1});
auto cm = *future.default_timed_get();
ASSERT(cm.isSharded());
ASSERT_EQ(1, cm.numChunks());
ASSERT_EQ(version, cm.getVersion());
ASSERT_EQ(ChunkVersion(0, 0, version.epoch(), version.getTimestamp()), cm.getVersion({"0"}));
ASSERT_EQ(version, cm.getVersion({"1"}));
ASSERT(boost::none == cm.getReshardingFields());
}
} // namespace
} // namespace mongo
|