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
|
/**
* Copyright (C) 2012 10gen Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License, version 3,
* as published by the Free Software Foundation.
*
* 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
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* 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 GNU Affero General 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_LOG_DEFAULT_COMPONENT ::mongo::logger::LogComponent::kSharding
#include "mongo/platform/basic.h"
#include "mongo/db/s/collection_metadata.h"
#include "mongo/bson/util/builder.h"
#include "mongo/s/catalog/type_chunk.h"
#include "mongo/stdx/memory.h"
#include "mongo/util/log.h"
#include "mongo/util/mongoutils/str.h"
namespace mongo {
using std::unique_ptr;
using std::make_pair;
using std::string;
using std::vector;
using str::stream;
CollectionMetadata::CollectionMetadata() = default;
CollectionMetadata::CollectionMetadata(const BSONObj& keyPattern, ChunkVersion collectionVersion)
: _collVersion(collectionVersion),
_shardVersion(ChunkVersion(0, 0, collectionVersion.epoch())),
_keyPattern(keyPattern.getOwned()) {}
CollectionMetadata::~CollectionMetadata() = default;
std::unique_ptr<CollectionMetadata> CollectionMetadata::cloneMigrate(
const ChunkType& chunk, const ChunkVersion& newCollectionVersion) const {
invariant(newCollectionVersion.epoch() == _collVersion.epoch());
invariant(newCollectionVersion > _collVersion);
invariant(rangeMapContains(_chunksMap, chunk.getMin(), chunk.getMax()));
unique_ptr<CollectionMetadata> metadata(stdx::make_unique<CollectionMetadata>());
metadata->_keyPattern = _keyPattern.getOwned();
metadata->fillKeyPatternFields();
metadata->_pendingMap = _pendingMap;
metadata->_chunksMap = _chunksMap;
metadata->_chunksMap.erase(chunk.getMin());
metadata->_shardVersion =
(metadata->_chunksMap.empty() ? ChunkVersion(0, 0, newCollectionVersion.epoch())
: newCollectionVersion);
metadata->_collVersion = newCollectionVersion;
metadata->fillRanges();
invariant(metadata->isValid());
return metadata;
}
unique_ptr<CollectionMetadata> CollectionMetadata::clonePlusChunk(
const BSONObj& minKey, const BSONObj& maxKey, const ChunkVersion& newShardVersion) const {
invariant(newShardVersion.epoch() == _shardVersion.epoch());
invariant(newShardVersion.isSet());
invariant(minKey.woCompare(maxKey) < 0);
invariant(!rangeMapOverlaps(_chunksMap, minKey, maxKey));
unique_ptr<CollectionMetadata> metadata(stdx::make_unique<CollectionMetadata>());
metadata->_keyPattern = _keyPattern.getOwned();
metadata->fillKeyPatternFields();
metadata->_pendingMap = _pendingMap;
metadata->_chunksMap = _chunksMap;
metadata->_chunksMap.insert(make_pair(minKey.getOwned(), maxKey.getOwned()));
metadata->_shardVersion = newShardVersion;
metadata->_collVersion = newShardVersion > _collVersion ? newShardVersion : _collVersion;
metadata->fillRanges();
invariant(metadata->isValid());
return metadata;
}
std::unique_ptr<CollectionMetadata> CollectionMetadata::cloneMinusPending(
const ChunkType& chunk) const {
invariant(rangeMapContains(_pendingMap, chunk.getMin(), chunk.getMax()));
unique_ptr<CollectionMetadata> metadata(stdx::make_unique<CollectionMetadata>());
metadata->_keyPattern = _keyPattern.getOwned();
metadata->fillKeyPatternFields();
metadata->_pendingMap = _pendingMap;
metadata->_pendingMap.erase(chunk.getMin());
metadata->_chunksMap = _chunksMap;
metadata->_rangesMap = _rangesMap;
metadata->_shardVersion = _shardVersion;
metadata->_collVersion = _collVersion;
invariant(metadata->isValid());
return metadata;
}
std::unique_ptr<CollectionMetadata> CollectionMetadata::clonePlusPending(
const ChunkType& chunk) const {
invariant(!rangeMapOverlaps(_chunksMap, chunk.getMin(), chunk.getMax()));
unique_ptr<CollectionMetadata> metadata(stdx::make_unique<CollectionMetadata>());
metadata->_keyPattern = _keyPattern.getOwned();
metadata->fillKeyPatternFields();
metadata->_pendingMap = _pendingMap;
metadata->_chunksMap = _chunksMap;
metadata->_rangesMap = _rangesMap;
metadata->_shardVersion = _shardVersion;
metadata->_collVersion = _collVersion;
// If there are any pending chunks on the interval to be added this is ok, since pending
// chunks aren't officially tracked yet and something may have changed on servers we do not
// see yet.
// We remove any chunks we overlap, the remote request starting a chunk migration must have
// been authoritative.
if (rangeMapOverlaps(_pendingMap, chunk.getMin(), chunk.getMax())) {
RangeVector pendingOverlap;
getRangeMapOverlap(_pendingMap, chunk.getMin(), chunk.getMax(), &pendingOverlap);
warning() << "new pending chunk " << rangeToString(chunk.getMin(), chunk.getMax())
<< " overlaps existing pending chunks " << overlapToString(pendingOverlap)
<< ", a migration may not have completed";
for (RangeVector::iterator it = pendingOverlap.begin(); it != pendingOverlap.end(); ++it) {
metadata->_pendingMap.erase(it->first);
}
}
metadata->_pendingMap.insert(make_pair(chunk.getMin(), chunk.getMax()));
invariant(metadata->isValid());
return metadata;
}
StatusWith<std::unique_ptr<CollectionMetadata>> CollectionMetadata::cloneSplit(
const BSONObj& minKey,
const BSONObj& maxKey,
const std::vector<BSONObj>& splitKeys,
const ChunkVersion& newShardVersion) const {
invariant(newShardVersion.epoch() == _shardVersion.epoch());
invariant(newShardVersion > _shardVersion);
// The version required in both resulting chunks could be simply an increment in the
// minor portion of the current version. However, we are enforcing uniqueness over the
// attributes <ns, version> of the configdb collection 'chunks'. So in practice, a
// migrate somewhere may force this split to pick up a version that has the major
// portion higher than the one that this shard has been using.
//
// TODO drop the uniqueness constraint and tighten the check below so that only the
// minor portion of version changes
// Check that we have the exact chunk that will be subtracted.
if (!rangeMapContains(_chunksMap, minKey, maxKey)) {
stream errMsg;
errMsg << "cannot split chunk " << rangeToString(minKey, maxKey)
<< ", this shard does not contain the chunk";
if (rangeMapOverlaps(_chunksMap, minKey, maxKey)) {
RangeVector overlap;
getRangeMapOverlap(_chunksMap, minKey, maxKey, &overlap);
errMsg << " and it overlaps " << overlapToString(overlap);
}
return {ErrorCodes::IllegalOperation, errMsg};
}
unique_ptr<CollectionMetadata> metadata(stdx::make_unique<CollectionMetadata>());
metadata->_keyPattern = _keyPattern.getOwned();
metadata->fillKeyPatternFields();
metadata->_pendingMap = _pendingMap;
metadata->_chunksMap = _chunksMap;
metadata->_shardVersion = newShardVersion; // will increment 2nd, 3rd,... chunks below
BSONObj startKey = minKey;
for (const auto& split : splitKeys) {
// Check that the split key is valid
if (!rangeContains(minKey, maxKey, split)) {
return {ErrorCodes::IllegalOperation,
stream() << "Cannot split chunk " << rangeToString(minKey, maxKey) << " at key "
<< split};
}
// Check that the split keys are in order
if (split.woCompare(startKey) <= 0) {
// The split keys came in out of order, this probably indicates a bug, so fail the
// operation. Re-iterate splitKeys to build a useful error message including the array
// of splitKeys in the order received.
str::stream errMsg;
errMsg << "Invalid input to splitChunk, split keys must be in order, got: [";
for (auto it2 = splitKeys.cbegin(); it2 != splitKeys.cend(); ++it2) {
if (it2 != splitKeys.begin()) {
errMsg << ", ";
}
errMsg << it2->toString();
}
errMsg << "]";
return {ErrorCodes::IllegalOperation, errMsg};
}
metadata->_chunksMap[startKey] = split.getOwned();
metadata->_chunksMap.insert(make_pair(split.getOwned(), maxKey.getOwned()));
metadata->_shardVersion.incMinor();
startKey = split;
}
metadata->_collVersion =
metadata->_shardVersion > _collVersion ? metadata->_shardVersion : _collVersion;
metadata->fillRanges();
invariant(metadata->isValid());
return std::move(metadata);
}
StatusWith<std::unique_ptr<CollectionMetadata>> CollectionMetadata::cloneMerge(
const BSONObj& minKey, const BSONObj& maxKey, const ChunkVersion& newShardVersion) const {
invariant(newShardVersion.epoch() == _shardVersion.epoch());
invariant(newShardVersion > _shardVersion);
RangeVector overlap;
getRangeMapOverlap(_chunksMap, minKey, maxKey, &overlap);
if (overlap.empty() || overlap.size() == 1) {
return {ErrorCodes::IllegalOperation,
stream() << "cannot merge range " << rangeToString(minKey, maxKey)
<< (overlap.empty() ? ", no chunks found in this range"
: ", only one chunk found in this range")};
}
bool validStartEnd = true;
bool validNoHoles = true;
if (overlap.begin()->first.woCompare(minKey) != 0) {
// First chunk doesn't start with minKey
validStartEnd = false;
} else if (overlap.rbegin()->second.woCompare(maxKey) != 0) {
// Last chunk doesn't end with maxKey
validStartEnd = false;
} else {
// Check that there are no holes
BSONObj prevMaxKey = minKey;
for (RangeVector::iterator it = overlap.begin(); it != overlap.end(); ++it) {
if (it->first.woCompare(prevMaxKey) != 0) {
validNoHoles = false;
break;
}
prevMaxKey = it->second;
}
}
if (!validStartEnd || !validNoHoles) {
return {ErrorCodes::IllegalOperation,
stream() << "cannot merge range " << rangeToString(minKey, maxKey)
<< ", overlapping chunks "
<< overlapToString(overlap)
<< (!validStartEnd ? " do not have the same min and max key"
: " are not all adjacent")};
}
unique_ptr<CollectionMetadata> metadata(stdx::make_unique<CollectionMetadata>());
metadata->_keyPattern = _keyPattern.getOwned();
metadata->fillKeyPatternFields();
metadata->_pendingMap = _pendingMap;
metadata->_chunksMap = _chunksMap;
metadata->_rangesMap = _rangesMap;
metadata->_shardVersion = newShardVersion;
metadata->_collVersion = newShardVersion > _collVersion ? newShardVersion : this->_collVersion;
for (RangeVector::iterator it = overlap.begin(); it != overlap.end(); ++it) {
metadata->_chunksMap.erase(it->first);
}
metadata->_chunksMap.insert(make_pair(minKey, maxKey));
invariant(metadata->isValid());
return std::move(metadata);
}
bool CollectionMetadata::keyBelongsToMe(const BSONObj& key) const {
// For now, collections don't move. So if the collection is not sharded, assume
// the document with the given key can be accessed.
if (_keyPattern.isEmpty()) {
return true;
}
if (_rangesMap.size() <= 0) {
return false;
}
RangeMap::const_iterator it = _rangesMap.upper_bound(key);
if (it != _rangesMap.begin())
it--;
bool good = rangeContains(it->first, it->second, key);
#if 0
// DISABLED because of SERVER-11175 - huge amount of logging
// Logs if the point doesn't belong here.
if ( !good ) {
log() << "bad: " << key << " " << it->first << " " << key.woCompare( it->first ) << " "
<< key.woCompare( it->second );
for ( RangeMap::const_iterator i = _rangesMap.begin(); i != _rangesMap.end(); ++i ) {
log() << "\t" << i->first << "\t" << i->second << "\t";
}
}
#endif
return good;
}
bool CollectionMetadata::keyIsPending(const BSONObj& key) const {
// If we aren't sharded, then the key is never pending (though it belongs-to-me)
if (_keyPattern.isEmpty()) {
return false;
}
if (_pendingMap.size() <= 0) {
return false;
}
RangeMap::const_iterator it = _pendingMap.upper_bound(key);
if (it != _pendingMap.begin())
it--;
bool isPending = rangeContains(it->first, it->second, key);
return isPending;
}
bool CollectionMetadata::getNextChunk(const BSONObj& lookupKey, ChunkType* chunk) const {
RangeMap::const_iterator upperChunkIt = _chunksMap.upper_bound(lookupKey);
RangeMap::const_iterator lowerChunkIt = upperChunkIt;
if (upperChunkIt != _chunksMap.begin()) {
--lowerChunkIt;
} else {
lowerChunkIt = _chunksMap.end();
}
if (lowerChunkIt != _chunksMap.end() && lowerChunkIt->second.woCompare(lookupKey) > 0) {
chunk->setMin(lowerChunkIt->first);
chunk->setMax(lowerChunkIt->second);
return true;
}
if (upperChunkIt != _chunksMap.end()) {
chunk->setMin(upperChunkIt->first);
chunk->setMax(upperChunkIt->second);
return true;
}
return false;
}
bool CollectionMetadata::getDifferentChunk(const BSONObj& chunkMinKey,
ChunkType* differentChunk) const {
RangeMap::const_iterator upperChunkIt = _chunksMap.end();
RangeMap::const_iterator lowerChunkIt = _chunksMap.begin();
while (lowerChunkIt != upperChunkIt) {
if (lowerChunkIt->first.woCompare(chunkMinKey) != 0) {
differentChunk->setMin(lowerChunkIt->first);
differentChunk->setMax(lowerChunkIt->second);
return true;
}
++lowerChunkIt;
}
return false;
}
void CollectionMetadata::toBSONBasic(BSONObjBuilder& bb) const {
_collVersion.addToBSON(bb, "collVersion");
_shardVersion.addToBSON(bb, "shardVersion");
bb.append("keyPattern", _keyPattern);
}
void CollectionMetadata::toBSONChunks(BSONArrayBuilder& bb) const {
if (_chunksMap.empty())
return;
for (RangeMap::const_iterator it = _chunksMap.begin(); it != _chunksMap.end(); ++it) {
BSONArrayBuilder chunkBB(bb.subarrayStart());
chunkBB.append(it->first);
chunkBB.append(it->second);
chunkBB.done();
}
}
void CollectionMetadata::toBSONPending(BSONArrayBuilder& bb) const {
if (_pendingMap.empty())
return;
for (RangeMap::const_iterator it = _pendingMap.begin(); it != _pendingMap.end(); ++it) {
BSONArrayBuilder pendingBB(bb.subarrayStart());
pendingBB.append(it->first);
pendingBB.append(it->second);
pendingBB.done();
}
}
string CollectionMetadata::toStringBasic() const {
return stream() << "collection version: " << _collVersion.toString()
<< ", shard version: " << _shardVersion.toString();
}
bool CollectionMetadata::getNextOrphanRange(const BSONObj& origLookupKey, KeyRange* range) const {
if (_keyPattern.isEmpty())
return false;
BSONObj lookupKey = origLookupKey;
BSONObj maxKey = getMaxKey(); // so we don't keep rebuilding
while (lookupKey.woCompare(maxKey) < 0) {
RangeMap::const_iterator lowerChunkIt = _chunksMap.end();
RangeMap::const_iterator upperChunkIt = _chunksMap.end();
if (!_chunksMap.empty()) {
upperChunkIt = _chunksMap.upper_bound(lookupKey);
lowerChunkIt = upperChunkIt;
if (upperChunkIt != _chunksMap.begin())
--lowerChunkIt;
else
lowerChunkIt = _chunksMap.end();
}
// If we overlap, continue after the overlap
// TODO: Could optimize slightly by finding next non-contiguous chunk
if (lowerChunkIt != _chunksMap.end() && lowerChunkIt->second.woCompare(lookupKey) > 0) {
lookupKey = lowerChunkIt->second;
continue;
}
RangeMap::const_iterator lowerPendingIt = _pendingMap.end();
RangeMap::const_iterator upperPendingIt = _pendingMap.end();
if (!_pendingMap.empty()) {
upperPendingIt = _pendingMap.upper_bound(lookupKey);
lowerPendingIt = upperPendingIt;
if (upperPendingIt != _pendingMap.begin())
--lowerPendingIt;
else
lowerPendingIt = _pendingMap.end();
}
// If we overlap, continue after the overlap
// TODO: Could optimize slightly by finding next non-contiguous chunk
if (lowerPendingIt != _pendingMap.end() &&
lowerPendingIt->second.woCompare(lookupKey) > 0) {
lookupKey = lowerPendingIt->second;
continue;
}
//
// We know that the lookup key is not covered by a chunk or pending range, and where the
// previous chunk and pending chunks are. Now we fill in the bounds as the closest
// bounds of the surrounding ranges in both maps.
//
range->keyPattern = _keyPattern;
range->minKey = getMinKey();
range->maxKey = maxKey;
if (lowerChunkIt != _chunksMap.end() && lowerChunkIt->second.woCompare(range->minKey) > 0) {
range->minKey = lowerChunkIt->second;
}
if (upperChunkIt != _chunksMap.end() && upperChunkIt->first.woCompare(range->maxKey) < 0) {
range->maxKey = upperChunkIt->first;
}
if (lowerPendingIt != _pendingMap.end() &&
lowerPendingIt->second.woCompare(range->minKey) > 0) {
range->minKey = lowerPendingIt->second;
}
if (upperPendingIt != _pendingMap.end() &&
upperPendingIt->first.woCompare(range->maxKey) < 0) {
range->maxKey = upperPendingIt->first;
}
return true;
}
return false;
}
BSONObj CollectionMetadata::getMinKey() const {
BSONObjIterator it(_keyPattern);
BSONObjBuilder minKeyB;
while (it.more())
minKeyB << it.next().fieldName() << MINKEY;
return minKeyB.obj();
}
BSONObj CollectionMetadata::getMaxKey() const {
BSONObjIterator it(_keyPattern);
BSONObjBuilder maxKeyB;
while (it.more())
maxKeyB << it.next().fieldName() << MAXKEY;
return maxKeyB.obj();
}
bool CollectionMetadata::isValid() const {
if (_shardVersion > _collVersion)
return false;
if (_collVersion.majorVersion() == 0)
return false;
if (_collVersion.epoch() != _shardVersion.epoch())
return false;
if (_shardVersion.majorVersion() > 0) {
// Must be chunks
if (_rangesMap.size() == 0 || _chunksMap.size() == 0)
return false;
} else {
// No chunks
if (_shardVersion.minorVersion() > 0)
return false;
if (_rangesMap.size() > 0 || _chunksMap.size() > 0)
return false;
}
return true;
}
bool CollectionMetadata::isValidKey(const BSONObj& key) const {
BSONObjIterator it(_keyPattern);
while (it.more()) {
BSONElement next = it.next();
if (!key.hasField(next.fieldName()))
return false;
}
return key.nFields() == _keyPattern.nFields();
}
void CollectionMetadata::fillRanges() {
if (_chunksMap.empty())
return;
// Load the chunk information, coallesceing their ranges. The version for this shard
// would be the highest version for any of the chunks.
RangeMap::const_iterator it = _chunksMap.begin();
BSONObj min, max;
while (it != _chunksMap.end()) {
BSONObj currMin = it->first;
BSONObj currMax = it->second;
++it;
// coalesce the chunk's bounds in ranges if they are adjacent chunks
if (min.isEmpty()) {
min = currMin;
max = currMax;
continue;
}
if (max == currMin) {
max = currMax;
continue;
}
_rangesMap.insert(make_pair(min, max));
min = currMin;
max = currMax;
}
dassert(!min.isEmpty());
_rangesMap.insert(make_pair(min, max));
}
void CollectionMetadata::fillKeyPatternFields() {
// Parse the shard keys into the states 'keys' and 'keySet' members.
BSONObjIterator patternIter = _keyPattern.begin();
while (patternIter.more()) {
BSONElement current = patternIter.next();
_keyFields.mutableVector().push_back(new FieldRef);
FieldRef* const newFieldRef = _keyFields.mutableVector().back();
newFieldRef->parse(current.fieldNameStringData());
}
}
} // namespace mongo
|