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
|
/**
* 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 <boost/optional.hpp>
#include <memory>
#include "mongo/bson/bsonobjbuilder.h"
#include "mongo/db/catalog_raii.h"
#include "mongo/db/client.h"
#include "mongo/db/jsobj.h"
#include "mongo/db/namespace_string.h"
#include "mongo/db/s/metadata_manager.h"
#include "mongo/db/s/sharding_state.h"
#include "mongo/db/server_options.h"
#include "mongo/db/service_context.h"
#include "mongo/executor/task_executor.h"
#include "mongo/s/catalog/type_chunk.h"
#include "mongo/s/client/shard_registry.h"
#include "mongo/s/shard_server_test_fixture.h"
#include "mongo/stdx/condition_variable.h"
#include "mongo/unittest/unittest.h"
#include "mongo/util/assert_util.h"
namespace mongo {
namespace {
using unittest::assertGet;
const NamespaceString kNss("TestDB", "TestColl");
const std::string kPattern = "key";
const KeyPattern kShardKeyPattern(BSON(kPattern << 1));
const std::string kThisShard{"thisShard"};
const std::string kOtherShard{"otherShard"};
class MetadataManagerTest : public ShardServerTestFixture {
protected:
void setUp() override {
ShardServerTestFixture::setUp();
_manager = std::make_shared<MetadataManager>(getServiceContext(), kNss, executor().get());
}
/**
* Returns an instance of CollectionMetadata which has no chunks owned by 'thisShard'.
*/
static CollectionMetadata makeEmptyMetadata() {
const OID epoch = OID::gen();
auto rt = RoutingTableHistory::makeNew(
kNss,
UUID::gen(),
kShardKeyPattern,
nullptr,
false,
epoch,
{ChunkType{kNss,
ChunkRange{BSON(kPattern << MINKEY), BSON(kPattern << MAXKEY)},
ChunkVersion(1, 0, epoch),
kOtherShard}});
std::shared_ptr<ChunkManager> cm = std::make_shared<ChunkManager>(rt, boost::none);
return CollectionMetadata(cm, kThisShard);
}
/**
* Returns a new metadata's instance based on the current state by adding a chunk with the
* specified bounds and version. The chunk's version must be higher than that of all chunks
* which are in the input metadata.
*
* It will fassert if the chunk bounds are incorrect or overlap an existing chunk or if the
* chunk version is lower than the maximum one.
*/
static CollectionMetadata cloneMetadataPlusChunk(const ScopedCollectionMetadata& metadata,
const ChunkRange& range) {
const BSONObj& minKey = range.getMin();
const BSONObj& maxKey = range.getMax();
ASSERT(!rangeMapOverlaps(metadata->getChunks(), minKey, maxKey));
auto cm = metadata->getChunkManager();
const auto chunkToSplit = cm->findIntersectingChunkWithSimpleCollation(minKey);
ASSERT_BSONOBJ_GTE(minKey, chunkToSplit.getMin());
ASSERT_BSONOBJ_LT(maxKey, chunkToSplit.getMax());
std::vector<ChunkType> splitChunks;
auto chunkVersion = cm->getVersion();
if (SimpleBSONObjComparator::kInstance.evaluate(chunkToSplit.getMin() < minKey)) {
chunkVersion.incMajor();
splitChunks.emplace_back(
kNss, ChunkRange(chunkToSplit.getMin(), minKey), chunkVersion, kOtherShard);
}
chunkVersion.incMajor();
splitChunks.emplace_back(kNss, ChunkRange(minKey, maxKey), chunkVersion, kThisShard);
chunkVersion.incMajor();
splitChunks.emplace_back(
kNss, ChunkRange(maxKey, chunkToSplit.getMax()), chunkVersion, kOtherShard);
auto rt = cm->getRoutingHistory()->makeUpdated(splitChunks);
return CollectionMetadata(std::make_shared<ChunkManager>(rt, boost::none), kThisShard);
}
static CollectionMetadata cloneMetadataMinusChunk(const ScopedCollectionMetadata& metadata,
const ChunkRange& range) {
const BSONObj& minKey = range.getMin();
const BSONObj& maxKey = range.getMax();
ASSERT(rangeMapOverlaps(metadata->getChunks(), minKey, maxKey));
auto cm = metadata->getChunkManager();
const auto chunkToMoveOut = cm->findIntersectingChunkWithSimpleCollation(minKey);
ASSERT_BSONOBJ_EQ(minKey, chunkToMoveOut.getMin());
ASSERT_BSONOBJ_EQ(maxKey, chunkToMoveOut.getMax());
auto chunkVersion = cm->getVersion();
chunkVersion.incMajor();
auto rt = cm->getRoutingHistory()->makeUpdated(
{ChunkType(kNss, ChunkRange(minKey, maxKey), chunkVersion, kOtherShard)});
return CollectionMetadata(std::make_shared<ChunkManager>(rt, boost::none), kThisShard);
}
std::shared_ptr<MetadataManager> _manager;
};
TEST_F(MetadataManagerTest, InitialMetadataIsUnknown) {
ASSERT(!_manager->getActiveMetadata(_manager, boost::none));
ASSERT(!_manager->getActiveMetadata(_manager, LogicalTime(Timestamp(10))));
ASSERT_EQ(0UL, _manager->numberOfMetadataSnapshots());
ASSERT_EQ(0UL, _manager->numberOfRangesToClean());
ASSERT_EQ(0UL, _manager->numberOfRangesToCleanStillInUse());
}
TEST_F(MetadataManagerTest, MetadataAfterClearIsUnknown) {
_manager->setFilteringMetadata(makeEmptyMetadata());
ASSERT(_manager->getActiveMetadata(_manager, boost::none));
ASSERT(_manager->getActiveMetadata(_manager, LogicalTime(Timestamp(10))));
_manager->clearFilteringMetadata();
ASSERT(!_manager->getActiveMetadata(_manager, boost::none));
ASSERT(!_manager->getActiveMetadata(_manager, LogicalTime(Timestamp(10))));
ASSERT_EQ(0UL, _manager->numberOfMetadataSnapshots());
ASSERT_EQ(0UL, _manager->numberOfRangesToClean());
ASSERT_EQ(0UL, _manager->numberOfRangesToCleanStillInUse());
}
TEST_F(MetadataManagerTest, GetActiveMetadataForUnshardedCollection) {
_manager->setFilteringMetadata(CollectionMetadata());
ASSERT(_manager->getActiveMetadata(_manager, boost::none));
ASSERT(!(*_manager->getActiveMetadata(_manager, boost::none))->isSharded());
ASSERT(_manager->getActiveMetadata(_manager, LogicalTime(Timestamp(10))));
ASSERT(!(*_manager->getActiveMetadata(_manager, LogicalTime(Timestamp(10))))->isSharded());
}
TEST_F(MetadataManagerTest, CleanUpForMigrateIn) {
_manager->setFilteringMetadata(makeEmptyMetadata());
// Sanity checks
ASSERT((*_manager->getActiveMetadata(_manager, boost::none))->isSharded());
ASSERT_EQ(0UL, (*_manager->getActiveMetadata(_manager, boost::none))->getChunks().size());
ChunkRange range1(BSON("key" << 0), BSON("key" << 10));
ChunkRange range2(BSON("key" << 10), BSON("key" << 20));
auto notif1 = _manager->beginReceive(range1);
ASSERT(!notif1.ready());
auto notif2 = _manager->beginReceive(range2);
ASSERT(!notif2.ready());
ASSERT_EQ(2UL, _manager->numberOfRangesToClean());
ASSERT_EQ(0UL, _manager->numberOfRangesToCleanStillInUse());
notif1.abandon();
notif2.abandon();
}
TEST_F(MetadataManagerTest, AddRangeNotificationsBlockAndYield) {
_manager->setFilteringMetadata(makeEmptyMetadata());
ChunkRange cr1(BSON("key" << 0), BSON("key" << 10));
auto notifn1 = _manager->cleanUpRange(cr1, Date_t{});
ASSERT_FALSE(notifn1.ready());
ASSERT_EQ(_manager->numberOfRangesToClean(), 1UL);
auto optNotifn = _manager->trackOrphanedDataCleanup(cr1);
ASSERT_FALSE(notifn1.ready());
ASSERT_FALSE(optNotifn->ready());
ASSERT(notifn1 == *optNotifn);
notifn1.abandon();
optNotifn->abandon();
}
TEST_F(MetadataManagerTest, NotificationBlocksUntilDeletion) {
_manager->setFilteringMetadata(makeEmptyMetadata());
ChunkRange cr1(BSON("key" << 20), BSON("key" << 30));
auto optNotif = _manager->trackOrphanedDataCleanup(cr1);
ASSERT(!optNotif);
{
ASSERT_EQ(_manager->numberOfMetadataSnapshots(), 0UL);
ASSERT_EQ(_manager->numberOfRangesToClean(), 0UL);
auto scm1 = _manager->getActiveMetadata(_manager, boost::none); // and increment refcount
const auto addChunk = [this] {
_manager->setFilteringMetadata(
cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager, boost::none),
{BSON("key" << 0), BSON("key" << 20)}));
};
addChunk(); // push new metadata
auto scm2 = _manager->getActiveMetadata(_manager, boost::none); // and increment refcount
ASSERT_EQ(1ULL, (*scm2)->getChunks().size());
// Simulate drop and recreate
_manager->setFilteringMetadata(makeEmptyMetadata());
addChunk(); // push new metadata
auto scm3 = _manager->getActiveMetadata(_manager, boost::none); // and increment refcount
ASSERT_EQ(1ULL, (*scm3)->getChunks().size());
ASSERT_EQ(_manager->numberOfMetadataSnapshots(), 0UL);
ASSERT_EQ(_manager->numberOfRangesToClean(), 0UL);
optNotif = _manager->cleanUpRange(cr1, Date_t{});
ASSERT(optNotif);
ASSERT_EQ(_manager->numberOfMetadataSnapshots(), 0UL);
ASSERT_EQ(_manager->numberOfRangesToClean(), 1UL);
}
// At this point scm1,2,3 above are destroyed and the refcount of each metadata goes to zero
ASSERT_EQ(_manager->numberOfMetadataSnapshots(), 0UL);
ASSERT_EQ(_manager->numberOfRangesToClean(), 1UL);
ASSERT(!optNotif->ready());
auto optNotif2 = _manager->trackOrphanedDataCleanup(cr1); // now tracking it in _rangesToClean
ASSERT(optNotif2);
ASSERT(!optNotif->ready());
ASSERT(!optNotif2->ready());
ASSERT(*optNotif == *optNotif2);
optNotif->abandon();
optNotif2->abandon();
}
TEST_F(MetadataManagerTest, CleanupNotificationsAreSignaledOnDropAndRecreate) {
const ChunkRange rangeToClean(BSON("key" << 20), BSON("key" << 30));
_manager->setFilteringMetadata(makeEmptyMetadata());
_manager->setFilteringMetadata(
cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager, boost::none),
{BSON("key" << 0), BSON("key" << 20)}));
_manager->setFilteringMetadata(
cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager, boost::none), rangeToClean));
auto cursorOnMovedMetadata = _manager->getActiveMetadata(_manager, boost::none);
_manager->setFilteringMetadata(
cloneMetadataMinusChunk(*_manager->getActiveMetadata(_manager, boost::none), rangeToClean));
auto notif = _manager->cleanUpRange(rangeToClean, Date_t{});
ASSERT(!notif.ready());
auto optNotif = _manager->trackOrphanedDataCleanup(rangeToClean);
ASSERT(optNotif);
ASSERT(!optNotif->ready());
_manager->setFilteringMetadata(makeEmptyMetadata());
ASSERT(notif.ready());
ASSERT(optNotif->ready());
}
TEST_F(MetadataManagerTest, RefreshAfterSuccessfulMigrationSinglePending) {
_manager->setFilteringMetadata(makeEmptyMetadata());
ChunkRange cr1(BSON("key" << 0), BSON("key" << 10));
_manager->setFilteringMetadata(
cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager, boost::none), cr1));
ASSERT_EQ((*_manager->getActiveMetadata(_manager, boost::none))->getChunks().size(), 1UL);
}
TEST_F(MetadataManagerTest, RefreshAfterSuccessfulMigrationMultiplePending) {
_manager->setFilteringMetadata(makeEmptyMetadata());
ChunkRange cr1(BSON("key" << 0), BSON("key" << 10));
ChunkRange cr2(BSON("key" << 30), BSON("key" << 40));
{
_manager->setFilteringMetadata(
cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager, boost::none), cr1));
ASSERT_EQ(_manager->numberOfRangesToClean(), 0UL);
ASSERT_EQ((*_manager->getActiveMetadata(_manager, boost::none))->getChunks().size(), 1UL);
}
{
_manager->setFilteringMetadata(
cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager, boost::none), cr2));
ASSERT_EQ((*_manager->getActiveMetadata(_manager, boost::none))->getChunks().size(), 2UL);
}
}
TEST_F(MetadataManagerTest, RefreshAfterNotYetCompletedMigrationMultiplePending) {
_manager->setFilteringMetadata(makeEmptyMetadata());
ChunkRange cr1(BSON("key" << 0), BSON("key" << 10));
ChunkRange cr2(BSON("key" << 30), BSON("key" << 40));
_manager->setFilteringMetadata(
cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager, boost::none),
{BSON("key" << 50), BSON("key" << 60)}));
ASSERT_EQ((*_manager->getActiveMetadata(_manager, boost::none))->getChunks().size(), 1UL);
}
TEST_F(MetadataManagerTest, BeginReceiveWithOverlappingRange) {
_manager->setFilteringMetadata(makeEmptyMetadata());
ChunkRange cr1(BSON("key" << 0), BSON("key" << 10));
ChunkRange cr2(BSON("key" << 30), BSON("key" << 40));
_manager->setFilteringMetadata(
cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager, boost::none), cr1));
_manager->setFilteringMetadata(
cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager, boost::none), cr2));
ChunkRange crOverlap(BSON("key" << 5), BSON("key" << 35));
}
TEST_F(MetadataManagerTest, RefreshMetadataAfterDropAndRecreate) {
_manager->setFilteringMetadata(makeEmptyMetadata());
_manager->setFilteringMetadata(
cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager, boost::none),
{BSON("key" << 0), BSON("key" << 10)}));
// Now, pretend that the collection was dropped and recreated
_manager->setFilteringMetadata(makeEmptyMetadata());
_manager->setFilteringMetadata(
cloneMetadataPlusChunk(*_manager->getActiveMetadata(_manager, boost::none),
{BSON("key" << 20), BSON("key" << 30)}));
const auto chunks = (*_manager->getActiveMetadata(_manager, boost::none))->getChunks();
ASSERT_EQ(1UL, chunks.size());
const auto chunkEntry = chunks.begin();
ASSERT_BSONOBJ_EQ(BSON("key" << 20), chunkEntry->first);
ASSERT_BSONOBJ_EQ(BSON("key" << 30), chunkEntry->second);
}
// Tests membership functions for _rangesToClean
TEST_F(MetadataManagerTest, RangesToCleanMembership) {
_manager->setFilteringMetadata(makeEmptyMetadata());
ChunkRange cr(BSON("key" << 0), BSON("key" << 10));
ASSERT_EQ(0UL, _manager->numberOfRangesToClean());
auto notifn = _manager->cleanUpRange(cr, Date_t{});
ASSERT(!notifn.ready());
ASSERT_EQ(1UL, _manager->numberOfRangesToClean());
notifn.abandon();
}
TEST_F(MetadataManagerTest, ClearUnneededChunkManagerObjectsLastSnapshotInList) {
_manager->setFilteringMetadata(makeEmptyMetadata());
ChunkRange cr1(BSON("key" << 0), BSON("key" << 10));
ChunkRange cr2(BSON("key" << 30), BSON("key" << 40));
auto scm1 = *_manager->getActiveMetadata(_manager, boost::none);
{
_manager->setFilteringMetadata(cloneMetadataPlusChunk(scm1, cr1));
ASSERT_EQ(_manager->numberOfMetadataSnapshots(), 1UL);
ASSERT_EQ(_manager->numberOfRangesToClean(), 0UL);
auto scm2 = *_manager->getActiveMetadata(_manager, boost::none);
ASSERT_EQ(scm2->getChunks().size(), 1UL);
_manager->setFilteringMetadata(cloneMetadataPlusChunk(scm2, cr2));
ASSERT_EQ(_manager->numberOfMetadataSnapshots(), 2UL);
ASSERT_EQ(_manager->numberOfEmptyMetadataSnapshots(), 0);
}
// The CollectionMetadata in scm2 should be set to boost::none because the object accessing it
// is now out of scope, but that in scm1 should remain
ASSERT_EQ(_manager->numberOfEmptyMetadataSnapshots(), 1);
ASSERT_EQ(_manager->numberOfMetadataSnapshots(), 2UL);
ASSERT_EQ((*_manager->getActiveMetadata(_manager, boost::none))->getChunks().size(), 2UL);
}
TEST_F(MetadataManagerTest, ClearUnneededChunkManagerObjectSnapshotInMiddleOfList) {
_manager->setFilteringMetadata(makeEmptyMetadata());
ChunkRange cr1(BSON("key" << 0), BSON("key" << 10));
ChunkRange cr2(BSON("key" << 30), BSON("key" << 40));
ChunkRange cr3(BSON("key" << 50), BSON("key" << 80));
ChunkRange cr4(BSON("key" << 90), BSON("key" << 100));
auto scm = *_manager->getActiveMetadata(_manager, boost::none);
_manager->setFilteringMetadata(cloneMetadataPlusChunk(scm, cr1));
ASSERT_EQ(_manager->numberOfMetadataSnapshots(), 1UL);
ASSERT_EQ(_manager->numberOfRangesToClean(), 0UL);
auto scm2 = *_manager->getActiveMetadata(_manager, boost::none);
ASSERT_EQ(scm2->getChunks().size(), 1UL);
_manager->setFilteringMetadata(cloneMetadataPlusChunk(scm2, cr2));
{
auto scm3 = *_manager->getActiveMetadata(_manager, boost::none);
ASSERT_EQ(scm3->getChunks().size(), 2UL);
_manager->setFilteringMetadata(cloneMetadataPlusChunk(scm3, cr3));
ASSERT_EQ(_manager->numberOfMetadataSnapshots(), 3UL);
ASSERT_EQ(_manager->numberOfEmptyMetadataSnapshots(), 0);
/**
* The CollectionMetadata object created when creating scm2 above will be set to boost::none
* when we overrwrite scm2 below. The _metadata list will then look like:
* [
* CollectionMetadataTracker{ metadata: xxx, orphans: [], usageCounter: 1},
* CollectionMetadataTracker{ metadata: boost::none, orphans: [], usageCounter: 0},
* CollectionMetadataTracker{ metadata: xxx, orphans: [], usageCounter: 1},
* CollectionMetadataTracker{ metadata: xxx, orphans: [], usageCounter: 1}
* ]
*/
scm2 = *_manager->getActiveMetadata(_manager, boost::none);
ASSERT_EQ(scm2->getChunks().size(), 3UL);
_manager->setFilteringMetadata(cloneMetadataPlusChunk(scm2, cr4));
ASSERT_EQ(_manager->numberOfMetadataSnapshots(), 4UL);
ASSERT_EQ(_manager->numberOfEmptyMetadataSnapshots(), 1);
}
/** The CollectionMetadata in scm3 should be set to boost::none because the object accessing it
* is now out of scope. The _metadata list should look like:
* [
* CollectionMetadataTracker{ metadata: xxx, orphans: [], usageCounter: 1},
* CollectionMetadataTracker{ metadata: boost::none, orphans: [], usageCounter: 0},
* CollectionMetadataTracker{ metadata: boost::none, orphans: [], usageCounter: 0},
* CollectionMetadataTracker{ metadata: xxx, orphans: [], usageCounter: 1}
* ]
*/
ASSERT_EQ(_manager->numberOfMetadataSnapshots(), 4UL);
ASSERT_EQ(_manager->numberOfEmptyMetadataSnapshots(), 2);
}
} // namespace
} // namespace mongo
|