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
|
/**
* Copyright (C) 2020-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/db/catalog/create_collection.h"
#include "mongo/db/catalog_raii.h"
#include "mongo/db/db_raii.h"
#include "mongo/db/dbdirectclient.h"
#include "mongo/db/global_settings.h"
#include "mongo/db/persistent_task_store.h"
#include "mongo/db/repl/wait_for_majority_service.h"
#include "mongo/db/s/collection_sharding_runtime.h"
#include "mongo/db/s/operation_sharding_state.h"
#include "mongo/db/s/range_deletion_task_gen.h"
#include "mongo/db/s/shard_filtering_metadata_refresh.h"
#include "mongo/db/s/shard_server_test_fixture.h"
#include "mongo/db/s/sharding_state.h"
#include "mongo/db/vector_clock.h"
#include "mongo/s/catalog/sharding_catalog_client_mock.h"
#include "mongo/s/catalog_cache_loader_mock.h"
#include "mongo/util/fail_point.h"
namespace mongo {
namespace {
const NamespaceString kTestNss("TestDB", "TestColl");
const std::string kShardKey = "_id";
const BSONObj kShardKeyPattern = BSON(kShardKey << 1);
class CollectionShardingRuntimeTest : public ShardServerTestFixture {
protected:
static CollectionMetadata makeShardedMetadata(OperationContext* opCtx,
UUID uuid = UUID::gen()) {
const OID epoch = OID::gen();
const Timestamp timestamp(1, 1);
auto range = ChunkRange(BSON(kShardKey << MINKEY), BSON(kShardKey << MAXKEY));
auto chunk = ChunkType(
uuid, std::move(range), ChunkVersion({epoch, timestamp}, {1, 0}), ShardId("other"));
ChunkManager cm(ShardId("0"),
DatabaseVersion(UUID::gen(), timestamp),
makeStandaloneRoutingTableHistory(
RoutingTableHistory::makeNew(kTestNss,
uuid,
kShardKeyPattern,
nullptr,
false,
epoch,
timestamp,
boost::none /* timeseriesFields */,
boost::none,
boost::none /* chunkSizeBytes */,
true,
{std::move(chunk)})),
boost::none);
return CollectionMetadata(std::move(cm), ShardId("0"));
}
};
TEST_F(CollectionShardingRuntimeTest,
GetCollectionDescriptionThrowsStaleConfigBeforeSetFilteringMetadataIsCalledAndNoOSSSet) {
OperationContext* opCtx = operationContext();
CollectionShardingRuntime csr(getServiceContext(), kTestNss, executor());
ASSERT_FALSE(csr.getCollectionDescription(opCtx).isSharded());
auto metadata = makeShardedMetadata(opCtx);
ScopedSetShardRole scopedSetShardRole{
opCtx, kTestNss, metadata.getShardVersion(), boost::none /* databaseVersion */};
ASSERT_THROWS_CODE(csr.getCollectionDescription(opCtx), DBException, ErrorCodes::StaleConfig);
}
TEST_F(
CollectionShardingRuntimeTest,
GetCollectionDescriptionReturnsUnshardedAfterSetFilteringMetadataIsCalledWithUnshardedMetadata) {
CollectionShardingRuntime csr(getServiceContext(), kTestNss, executor());
csr.setFilteringMetadata(operationContext(), CollectionMetadata());
ASSERT_FALSE(csr.getCollectionDescription(operationContext()).isSharded());
}
TEST_F(CollectionShardingRuntimeTest,
GetCollectionDescriptionReturnsShardedAfterSetFilteringMetadataIsCalledWithShardedMetadata) {
CollectionShardingRuntime csr(getServiceContext(), kTestNss, executor());
OperationContext* opCtx = operationContext();
auto metadata = makeShardedMetadata(opCtx);
csr.setFilteringMetadata(opCtx, metadata);
ScopedSetShardRole scopedSetShardRole{
opCtx, kTestNss, metadata.getShardVersion(), boost::none /* databaseVersion */};
ASSERT_TRUE(csr.getCollectionDescription(opCtx).isSharded());
}
TEST_F(CollectionShardingRuntimeTest,
GetCurrentMetadataIfKnownReturnsNoneBeforeSetFilteringMetadataIsCalled) {
CollectionShardingRuntime csr(getServiceContext(), kTestNss, executor());
ASSERT_FALSE(csr.getCurrentMetadataIfKnown());
}
TEST_F(
CollectionShardingRuntimeTest,
GetCurrentMetadataIfKnownReturnsUnshardedAfterSetFilteringMetadataIsCalledWithUnshardedMetadata) {
CollectionShardingRuntime csr(getServiceContext(), kTestNss, executor());
csr.setFilteringMetadata(operationContext(), CollectionMetadata());
const auto optCurrMetadata = csr.getCurrentMetadataIfKnown();
ASSERT_TRUE(optCurrMetadata);
ASSERT_FALSE(optCurrMetadata->isSharded());
ASSERT_EQ(optCurrMetadata->getShardVersion(), ChunkVersion::UNSHARDED());
}
TEST_F(
CollectionShardingRuntimeTest,
GetCurrentMetadataIfKnownReturnsShardedAfterSetFilteringMetadataIsCalledWithShardedMetadata) {
CollectionShardingRuntime csr(getServiceContext(), kTestNss, executor());
OperationContext* opCtx = operationContext();
auto metadata = makeShardedMetadata(opCtx);
csr.setFilteringMetadata(opCtx, metadata);
const auto optCurrMetadata = csr.getCurrentMetadataIfKnown();
ASSERT_TRUE(optCurrMetadata);
ASSERT_TRUE(optCurrMetadata->isSharded());
ASSERT_EQ(optCurrMetadata->getShardVersion(), metadata.getShardVersion());
}
TEST_F(CollectionShardingRuntimeTest,
GetCurrentMetadataIfKnownReturnsNoneAfterClearFilteringMetadataIsCalled) {
CollectionShardingRuntime csr(getServiceContext(), kTestNss, executor());
OperationContext* opCtx = operationContext();
csr.setFilteringMetadata(opCtx, makeShardedMetadata(opCtx));
csr.clearFilteringMetadata(opCtx);
ASSERT_FALSE(csr.getCurrentMetadataIfKnown());
}
TEST_F(CollectionShardingRuntimeTest, SetFilteringMetadataWithSameUUIDKeepsSameMetadataManager) {
CollectionShardingRuntime csr(getServiceContext(), kTestNss, executor());
ASSERT_EQ(csr.getNumMetadataManagerChanges_forTest(), 0);
OperationContext* opCtx = operationContext();
auto metadata = makeShardedMetadata(opCtx);
csr.setFilteringMetadata(opCtx, metadata);
// Should create a new MetadataManager object, bumping the count to 1.
ASSERT_EQ(csr.getNumMetadataManagerChanges_forTest(), 1);
// Set it again.
csr.setFilteringMetadata(opCtx, metadata);
// Should not have reset metadata, so the counter should still be 1.
ASSERT_EQ(csr.getNumMetadataManagerChanges_forTest(), 1);
}
TEST_F(CollectionShardingRuntimeTest,
SetFilteringMetadataWithDifferentUUIDReplacesPreviousMetadataManager) {
CollectionShardingRuntime csr(getServiceContext(), kTestNss, executor());
OperationContext* opCtx = operationContext();
auto metadata = makeShardedMetadata(opCtx);
csr.setFilteringMetadata(opCtx, metadata);
ScopedSetShardRole scopedSetShardRole{
opCtx, kTestNss, metadata.getShardVersion(), boost::none /* databaseVersion */};
ASSERT_EQ(csr.getNumMetadataManagerChanges_forTest(), 1);
// Set it again with a different metadata object (UUID is generated randomly in
// makeShardedMetadata()).
auto newMetadata = makeShardedMetadata(opCtx);
csr.setFilteringMetadata(opCtx, newMetadata);
ASSERT_EQ(csr.getNumMetadataManagerChanges_forTest(), 2);
ASSERT(
csr.getCollectionDescription(opCtx).uuidMatches(newMetadata.getChunkManager()->getUUID()));
}
TEST_F(CollectionShardingRuntimeTest, ReturnUnshardedMetadataInServerlessMode) {
const NamespaceString testNss("TestDBForServerless", "TestColl");
OperationContext* opCtx = operationContext();
// Enable serverless mode in global settings.
repl::ReplSettings severlessRs;
severlessRs.setServerlessMode();
repl::ReplSettings originalRs = getGlobalReplSettings();
setGlobalReplSettings(severlessRs);
ASSERT_TRUE(getGlobalReplSettings().isServerless());
// Enable sharding state and set shard version on the OSS for testNss.
ScopedSetShardRole scopedSetShardRole1{
opCtx,
testNss,
ChunkVersion::UNSHARDED(), /* shardVersion */
boost::none /* databaseVersion */
};
CollectionShardingRuntime csr(getServiceContext(), testNss, executor());
auto collectionFilter = csr.getOwnershipFilter(
opCtx, CollectionShardingRuntime::OrphanCleanupPolicy::kAllowOrphanCleanup, true);
ASSERT_FALSE(collectionFilter.isSharded());
ASSERT_FALSE(csr.getCurrentMetadataIfKnown()->isSharded());
ASSERT_FALSE(csr.getCollectionDescription(opCtx).isSharded());
// Enable sharding state and set shard version on the OSS for logical session nss.
ScopedSetShardRole scopedSetShardRole2{
opCtx,
NamespaceString::kLogicalSessionsNamespace,
ChunkVersion({OID::gen(), Timestamp(1, 1)}, {1, 0}), /* shardVersion */
boost::none /* databaseVersion */
};
CollectionShardingRuntime csrLogicalSession(
getServiceContext(), NamespaceString::kLogicalSessionsNamespace, executor());
ASSERT(csrLogicalSession.getCurrentMetadataIfKnown() == boost::none);
ASSERT_THROWS_CODE(
csrLogicalSession.getCollectionDescription(opCtx), DBException, ErrorCodes::StaleConfig);
ASSERT_THROWS_CODE(
csrLogicalSession.getOwnershipFilter(
opCtx, CollectionShardingRuntime::OrphanCleanupPolicy::kAllowOrphanCleanup, true),
DBException,
ErrorCodes::StaleConfig);
// Reset the global settings.
setGlobalReplSettings(originalRs);
}
class CollectionShardingRuntimeTestWithMockedLoader : public ShardServerTestFixture {
public:
const NamespaceString kNss{"test.foo"};
const UUID kCollUUID = UUID::gen();
const std::string kShardKey = "x";
const HostAndPort kConfigHostAndPort{"DummyConfig", 12345};
const std::vector<ShardType> kShardList = {ShardType("shard0", "Host0:12345")};
void setUp() override {
// Don't call ShardServerTestFixture::setUp so we can install a mock catalog cache
// loader.
ShardingMongodTestFixture::setUp();
replicationCoordinator()->alwaysAllowWrites(true);
serverGlobalParams.clusterRole = ClusterRole::ShardServer;
_clusterId = OID::gen();
ShardingState::get(getServiceContext())
->setInitialized(kShardList[0].getName(), _clusterId);
auto mockLoader = std::make_unique<CatalogCacheLoaderMock>();
_mockCatalogCacheLoader = mockLoader.get();
CatalogCacheLoader::set(getServiceContext(), std::move(mockLoader));
uassertStatusOK(
initializeGlobalShardingStateForMongodForTest(ConnectionString(kConfigHostAndPort)));
configTargeterMock()->setFindHostReturnValue(kConfigHostAndPort);
WaitForMajorityService::get(getServiceContext()).startup(getServiceContext());
for (const auto& shard : kShardList) {
std::unique_ptr<RemoteCommandTargeterMock> targeter(
std::make_unique<RemoteCommandTargeterMock>());
HostAndPort host(shard.getHost());
targeter->setConnectionStringReturnValue(ConnectionString(host));
targeter->setFindHostReturnValue(host);
targeterFactory()->addTargeterToReturn(ConnectionString(host), std::move(targeter));
}
}
void tearDown() override {
WaitForMajorityService::get(getServiceContext()).shutDown();
ShardServerTestFixture::tearDown();
}
class StaticCatalogClient final : public ShardingCatalogClientMock {
public:
StaticCatalogClient(std::vector<ShardType> shards) : _shards(std::move(shards)) {}
StatusWith<repl::OpTimeWith<std::vector<ShardType>>> getAllShards(
OperationContext* opCtx, repl::ReadConcernLevel readConcern) override {
return repl::OpTimeWith<std::vector<ShardType>>(_shards);
}
std::vector<CollectionType> getCollections(
OperationContext* opCtx,
StringData dbName,
repl::ReadConcernLevel readConcernLevel) override {
return _colls;
}
void setCollections(std::vector<CollectionType> colls) {
_colls = std::move(colls);
}
private:
const std::vector<ShardType> _shards;
std::vector<CollectionType> _colls;
};
std::unique_ptr<ShardingCatalogClient> makeShardingCatalogClient() override {
return std::make_unique<StaticCatalogClient>(kShardList);
}
CollectionType createCollection(const OID& epoch, const Timestamp& timestamp) {
CollectionType res(kNss, epoch, timestamp, Date_t::now(), kCollUUID, BSON(kShardKey << 1));
res.setAllowMigrations(false);
return res;
}
std::vector<ChunkType> createChunks(const OID& epoch,
const UUID& uuid,
const Timestamp& timestamp) {
auto range1 = ChunkRange(BSON(kShardKey << MINKEY), BSON(kShardKey << 5));
ChunkType chunk1(
uuid, range1, ChunkVersion({epoch, timestamp}, {1, 0}), kShardList[0].getName());
auto range2 = ChunkRange(BSON(kShardKey << 5), BSON(kShardKey << MAXKEY));
ChunkType chunk2(
uuid, range2, ChunkVersion({epoch, timestamp}, {1, 1}), kShardList[0].getName());
return {chunk1, chunk2};
}
protected:
CatalogCacheLoaderMock* _mockCatalogCacheLoader;
};
/**
* Fixture for when range deletion functionality is required in CollectionShardingRuntime tests.
*/
class CollectionShardingRuntimeWithRangeDeleterTest : public CollectionShardingRuntimeTest {
public:
void setUp() override {
CollectionShardingRuntimeTest::setUp();
WaitForMajorityService::get(getServiceContext()).startup(getServiceContext());
// Set up replication coordinator to be primary and have no replication delay.
auto replCoord = std::make_unique<repl::ReplicationCoordinatorMock>(getServiceContext());
replCoord->setCanAcceptNonLocalWrites(true);
std::ignore = replCoord->setFollowerMode(repl::MemberState::RS_PRIMARY);
// Make waitForWriteConcern return immediately.
replCoord->setAwaitReplicationReturnValueFunction([this](OperationContext* opCtx,
const repl::OpTime& opTime) {
return repl::ReplicationCoordinator::StatusAndDuration(Status::OK(), Milliseconds(0));
});
repl::ReplicationCoordinator::set(getServiceContext(), std::move(replCoord));
{
OperationShardingState::ScopedAllowImplicitCollectionCreate_UNSAFE
unsafeCreateCollection(operationContext());
uassertStatusOK(createCollection(
operationContext(), kTestNss.db().toString(), BSON("create" << kTestNss.coll())));
}
AutoGetCollection autoColl(operationContext(), kTestNss, MODE_IX);
_uuid = autoColl.getCollection()->uuid();
}
void tearDown() override {
DBDirectClient client(operationContext());
client.dropCollection(kTestNss.ns());
WaitForMajorityService::get(getServiceContext()).shutDown();
CollectionShardingRuntimeTest::tearDown();
}
// Creates the CSR if it does not exist and stashes it in the CollectionShardingStateMap. This
// is required for waitForClean tests which use CollectionShardingRuntime::get().
CollectionShardingRuntime& csr() {
AutoGetCollection autoColl(operationContext(), kTestNss, MODE_IX);
auto* css = CollectionShardingState::get(operationContext(), kTestNss);
return *checked_cast<CollectionShardingRuntime*>(css);
}
const UUID& uuid() const {
return _uuid;
}
private:
UUID _uuid{UUID::gen()};
};
// The 'pending' field must not be set in order for a range deletion task to succeed, but the
// ShardServerOpObserver will submit the task for deletion upon seeing an insert without the
// 'pending' field. The tests call removeDocumentsFromRange directly, so we want to avoid having
// the op observer also submit the task. The ShardServerOpObserver will ignore replacement
// updates on the range deletions namespace though, so we can get around the issue by inserting
// the task with the 'pending' field set, and then remove the field using a replacement update
// after.
RangeDeletionTask insertRangeDeletionTask(OperationContext* opCtx,
const NamespaceString& nss,
const UUID& uuid,
const ChunkRange& range,
int64_t numOrphans) {
PersistentTaskStore<RangeDeletionTask> store(NamespaceString::kRangeDeletionNamespace);
auto migrationId = UUID::gen();
RangeDeletionTask t(migrationId, nss, uuid, ShardId("donor"), range, CleanWhenEnum::kDelayed);
t.setPending(true);
t.setNumOrphanDocs(numOrphans);
const auto currentTime = VectorClock::get(opCtx)->getTime();
t.setTimestamp(currentTime.clusterTime().asTimestamp());
store.add(opCtx, t);
auto query = BSON(RangeDeletionTask::kIdFieldName << migrationId);
t.setPending(boost::none);
auto update = t.toBSON();
store.update(opCtx, query, update);
return t;
}
TEST_F(CollectionShardingRuntimeWithRangeDeleterTest,
WaitForCleanReturnsErrorIfMetadataManagerDoesNotExist) {
auto status = CollectionShardingRuntime::waitForClean(
operationContext(),
kTestNss,
uuid(),
ChunkRange(BSON(kShardKey << MINKEY), BSON(kShardKey << MAXKEY)),
Date_t::max());
ASSERT_EQ(status.code(), ErrorCodes::ConflictingOperationInProgress);
}
TEST_F(CollectionShardingRuntimeWithRangeDeleterTest,
WaitForCleanReturnsErrorIfCollectionUUIDDoesNotMatchFilteringMetadata) {
OperationContext* opCtx = operationContext();
auto metadata = makeShardedMetadata(opCtx, uuid());
csr().setFilteringMetadata(opCtx, metadata);
auto randomUuid = UUID::gen();
auto status = CollectionShardingRuntime::waitForClean(
opCtx,
kTestNss,
randomUuid,
ChunkRange(BSON(kShardKey << MINKEY), BSON(kShardKey << MAXKEY)),
Date_t::max());
ASSERT_EQ(status.code(), ErrorCodes::ConflictingOperationInProgress);
}
TEST_F(CollectionShardingRuntimeWithRangeDeleterTest,
WaitForCleanReturnsOKIfNoDeletionsAreScheduled) {
OperationContext* opCtx = operationContext();
auto metadata = makeShardedMetadata(opCtx, uuid());
csr().setFilteringMetadata(opCtx, metadata);
auto status = CollectionShardingRuntime::waitForClean(
opCtx,
kTestNss,
uuid(),
ChunkRange(BSON(kShardKey << MINKEY), BSON(kShardKey << MAXKEY)),
Date_t::max());
ASSERT_OK(status);
}
TEST_F(CollectionShardingRuntimeWithRangeDeleterTest,
WaitForCleanBlocksBehindOneScheduledDeletion) {
// Enable fail point to suspendRangeDeletion.
globalFailPointRegistry().find("suspendRangeDeletion")->setMode(FailPoint::alwaysOn);
OperationContext* opCtx = operationContext();
auto metadata = makeShardedMetadata(opCtx, uuid());
csr().setFilteringMetadata(opCtx, metadata);
const ChunkRange range = ChunkRange(BSON(kShardKey << MINKEY), BSON(kShardKey << MAXKEY));
const auto task = insertRangeDeletionTask(opCtx, kTestNss, uuid(), range, 0);
auto cleanupComplete =
csr().cleanUpRange(range, task.getId(), CollectionShardingRuntime::CleanWhen::kNow);
opCtx->setDeadlineAfterNowBy(Milliseconds(100), ErrorCodes::MaxTimeMSExpired);
auto status =
CollectionShardingRuntime::waitForClean(opCtx, kTestNss, uuid(), range, Date_t::max());
ASSERT_EQ(status.code(), ErrorCodes::MaxTimeMSExpired);
globalFailPointRegistry().find("suspendRangeDeletion")->setMode(FailPoint::off);
cleanupComplete.get();
}
TEST_F(CollectionShardingRuntimeWithRangeDeleterTest,
WaitForCleanBlocksBehindAllScheduledDeletions) {
OperationContext* opCtx = operationContext();
auto metadata = makeShardedMetadata(opCtx, uuid());
csr().setFilteringMetadata(opCtx, metadata);
const auto middleKey = 5;
const ChunkRange range1 = ChunkRange(BSON(kShardKey << MINKEY), BSON(kShardKey << middleKey));
const auto task1 = insertRangeDeletionTask(opCtx, kTestNss, uuid(), range1, 0);
const ChunkRange range2 = ChunkRange(BSON(kShardKey << middleKey), BSON(kShardKey << MAXKEY));
const auto task2 = insertRangeDeletionTask(opCtx, kTestNss, uuid(), range2, 0);
auto cleanupCompleteFirst =
csr().cleanUpRange(range1, task1.getId(), CollectionShardingRuntime::CleanWhen::kNow);
auto cleanupCompleteSecond =
csr().cleanUpRange(range2, task2.getId(), CollectionShardingRuntime::CleanWhen::kNow);
auto status = CollectionShardingRuntime::waitForClean(
opCtx,
kTestNss,
uuid(),
ChunkRange(BSON(kShardKey << MINKEY), BSON(kShardKey << MAXKEY)),
Date_t::max());
// waitForClean should block until both cleanup tasks have run. This is a best-effort check,
// since even if it did not block, it is possible that the cleanup tasks could complete before
// reaching these lines.
ASSERT(cleanupCompleteFirst.isReady());
ASSERT(cleanupCompleteSecond.isReady());
ASSERT_OK(status);
}
TEST_F(CollectionShardingRuntimeWithRangeDeleterTest,
WaitForCleanReturnsOKAfterSuccessfulDeletion) {
OperationContext* opCtx = operationContext();
auto metadata = makeShardedMetadata(opCtx, uuid());
csr().setFilteringMetadata(opCtx, metadata);
const ChunkRange range = ChunkRange(BSON(kShardKey << MINKEY), BSON(kShardKey << MAXKEY));
const auto task = insertRangeDeletionTask(opCtx, kTestNss, uuid(), range, 0);
auto cleanupComplete =
csr().cleanUpRange(range, task.getId(), CollectionShardingRuntime::CleanWhen::kNow);
auto status =
CollectionShardingRuntime::waitForClean(opCtx, kTestNss, uuid(), range, Date_t::max());
ASSERT_OK(status);
ASSERT(cleanupComplete.isReady());
}
TEST_F(CollectionShardingRuntimeWithRangeDeleterTest,
WaitForCleanCorrectEvenAfterClearFollowedBySetFilteringMetadata) {
globalFailPointRegistry().find("suspendRangeDeletion")->setMode(FailPoint::alwaysOn);
ScopeGuard resetFailPoint(
[=] { globalFailPointRegistry().find("suspendRangeDeletion")->setMode(FailPoint::off); });
OperationContext* opCtx = operationContext();
auto metadata = makeShardedMetadata(opCtx, uuid());
csr().setFilteringMetadata(opCtx, metadata);
const ChunkRange range = ChunkRange(BSON(kShardKey << MINKEY), BSON(kShardKey << MAXKEY));
const auto task = insertRangeDeletionTask(opCtx, kTestNss, uuid(), range, 0);
// Schedule range deletion that will hang due to `suspendRangeDeletion` failpoint
auto cleanupComplete =
csr().cleanUpRange(range, task.getId(), CollectionShardingRuntime::CleanWhen::kNow);
// Clear and set again filtering metadata
csr().clearFilteringMetadata(opCtx);
csr().setFilteringMetadata(opCtx, metadata);
auto waitForCleanUp = [&](Date_t timeout) {
return CollectionShardingRuntime::waitForClean(opCtx, kTestNss, uuid(), range, timeout);
};
// Check that the hanging range deletion is still tracked even following a clear of the metadata
auto status = waitForCleanUp(Date_t::now() + Milliseconds(100));
ASSERT_NOT_OK(status);
ASSERT(!cleanupComplete.isReady());
globalFailPointRegistry().find("suspendRangeDeletion")->setMode(FailPoint::off);
resetFailPoint.dismiss();
// Check that the range deletion is not tracked anymore after it succeeds
status = waitForCleanUp(Date_t::max());
ASSERT_OK(status);
ASSERT(cleanupComplete.isReady());
}
} // namespace
} // namespace mongo
|