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
|
// sharding.cpp : some unit tests for sharding internals
/**
* Copyright (C) 2009 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/>.
*/
#include "pch.h"
#include "mongo/dbtests/dbtests.h"
#include "mongo/client/dbclientmockcursor.h"
#include "mongo/client/parallel.h"
#include "mongo/s/chunk_diff.h"
#include "mongo/s/chunk_version.h"
#include "mongo/s/type_chunk.h"
namespace ShardingTests {
namespace serverandquerytests {
class test1 {
public:
void run() {
ServerAndQuery a( "foo:1" , BSON( "a" << GT << 0 << LTE << 100 ) );
ServerAndQuery b( "foo:1" , BSON( "a" << GT << 200 << LTE << 1000 ) );
ASSERT( a < b );
ASSERT( ! ( b < a ) );
set<ServerAndQuery> s;
s.insert( a );
s.insert( b );
ASSERT_EQUALS( (unsigned int)2 , s.size() );
}
};
}
static int rand( int max = -1 ){
static unsigned seed = 1337;
#if !defined(_WIN32)
int r = rand_r( &seed ) ;
#else
int r = ::rand(); // seed not used in this case
#endif
// Modding is bad, but don't really care in this case
return max > 0 ? r % max : r;
}
//
// Sets up a basic environment for loading chunks to/from the direct database connection
// Redirects connections to the direct database for the duration of the test.
//
class ChunkManagerTest : public ConnectionString::ConnectionHook {
public:
class CustomDirectClient : public DBDirectClient {
public:
virtual ConnectionString::ConnectionType type() const {
return ConnectionString::CUSTOM;
}
};
CustomDirectClient _client;
Shard _shard;
ChunkManagerTest() {
DBException::traceExceptions = true;
// Make all connections redirect to the direct client
ConnectionString::setConnectionHook( this );
// Create the default config database before querying, necessary for direct connections
client().dropDatabase( "config" );
client().insert( "config.test", BSON( "hello" << "world" ) );
client().dropCollection( "config.test" );
client().dropDatabase( nsGetDB( collName() ) );
client().insert( collName(), BSON( "hello" << "world" ) );
client().dropCollection( collName() );
// Since we've redirected the conns, the host doesn't matter here so long as it's
// prefixed with a "$"
_shard = Shard( "shard0000", "$hostFooBar:27017" );
// Need to run this to ensure the shard is in the global lookup table
_shard.setAddress( _shard.getAddress() );
}
virtual ~ChunkManagerTest() {
// Reset the redirection
ConnectionString::setConnectionHook( NULL );
}
string collName(){ return "foo.bar"; }
Shard& shard(){ return _shard; }
DBDirectClient& client(){
return _client;
}
virtual DBClientBase* connect( const ConnectionString& connStr,
string& errmsg,
double socketTimeout )
{
// Note - must be new, since it gets owned elsewhere
return new CustomDirectClient();
}
};
//
// Tests creating a new chunk manager and creating the default chunks
//
class ChunkManagerCreateBasicTest : public ChunkManagerTest {
public:
void run(){
ChunkManager manager( collName(), ShardKeyPattern( BSON( "_id" << 1 ) ), false );
manager.createFirstChunks( shard().getConnString(), shard(), NULL, NULL );
BSONObj firstChunk = client().findOne(ChunkType::ConfigNS, BSONObj()).getOwned();
ASSERT(firstChunk[ChunkType::min()].Obj()[ "_id" ].type() == MinKey );
ASSERT(firstChunk[ChunkType::max()].Obj()[ "_id" ].type() == MaxKey );
ChunkVersion version = ChunkVersion::fromBSON(firstChunk,
ChunkType::DEPRECATED_lastmod());
ASSERT( version.majorVersion() == 1 );
ASSERT( version.minorVersion() == 0 );
ASSERT( version.isEpochSet() );
}
};
//
// Tests creating a new chunk manager with random split points. Creating chunks on multiple shards is not
// tested here since there are unresolved race conditions there and probably should be avoided if at all
// possible.
//
class ChunkManagerCreateFullTest : public ChunkManagerTest {
public:
static const int numSplitPoints = 100;
void genRandomSplitPoints( vector<int>* splitPoints ){
for( int i = 0; i < numSplitPoints; i++ ){
splitPoints->push_back( rand( numSplitPoints * 10 ) );
}
}
void genRandomSplitKeys( const string& keyName, vector<BSONObj>* splitKeys ){
vector<int> splitPoints;
genRandomSplitPoints( &splitPoints );
for( vector<int>::iterator it = splitPoints.begin(); it != splitPoints.end(); ++it ){
splitKeys->push_back( BSON( keyName << *it ) );
}
}
// Uses a chunk manager to create chunks
void createChunks( const string& keyName ){
vector<BSONObj> splitKeys;
genRandomSplitKeys( keyName, &splitKeys );
ChunkManager manager( collName(), ShardKeyPattern( BSON( keyName << 1 ) ), false );
manager.createFirstChunks( shard().getConnString(), shard(), &splitKeys, NULL );
}
void run(){
string keyName = "_id";
createChunks( keyName );
auto_ptr<DBClientCursor> cursor =
client().query(ChunkType::ConfigNS, QUERY(ChunkType::ns(collName())));
set<int> minorVersions;
OID epoch;
// Check that all chunks were created with version 1|x with consistent epoch and unique minor versions
while( cursor->more() ){
BSONObj chunk = cursor->next();
ChunkVersion version = ChunkVersion::fromBSON(chunk,
ChunkType::DEPRECATED_lastmod());
ASSERT( version.majorVersion() == 1 );
ASSERT( version.isEpochSet() );
if( ! epoch.isSet() ) epoch = version.epoch();
ASSERT( version.epoch() == epoch );
ASSERT( minorVersions.find( version.minorVersion() ) == minorVersions.end() );
minorVersions.insert( version.minorVersion() );
ASSERT(chunk[ChunkType::shard()].String() == shard().getName());
}
}
};
//
// Tests that chunks are loaded correctly from the db with no a-priori info and also that they can be reloaded
// on top of an old chunk manager with changes.
//
class ChunkManagerLoadBasicTest : public ChunkManagerCreateFullTest {
public:
void run(){
string keyName = "_id";
createChunks( keyName );
int numChunks = static_cast<int>(client().count(ChunkType::ConfigNS,
BSON(ChunkType::ns(collName()))));
BSONObj firstChunk = client().findOne(ChunkType::ConfigNS, BSONObj()).getOwned();
ChunkVersion version = ChunkVersion::fromBSON(firstChunk,
ChunkType::DEPRECATED_lastmod());
// Make manager load existing chunks
ChunkManagerPtr manager( new ChunkManager( collName(), ShardKeyPattern( BSON( "_id" << 1 ) ), false ) );
((ChunkManager*) manager.get())->loadExistingRanges( shard().getConnString() );
ASSERT( manager->getVersion().epoch() == version.epoch() );
ASSERT( manager->getVersion().minorVersion() == ( numChunks - 1 ) );
ASSERT( static_cast<int>( manager->getChunkMap().size() ) == numChunks );
// Modify chunks collection
BSONObjBuilder b;
ChunkVersion laterVersion = ChunkVersion( 2, 1, version.epoch() );
laterVersion.addToBSON(b, ChunkType::DEPRECATED_lastmod());
client().update(ChunkType::ConfigNS, BSONObj(), BSON( "$set" << b.obj()));
// Make new manager load chunk diff
ChunkManager newManager( manager );
newManager.loadExistingRanges( shard().getConnString() );
ASSERT( newManager.getVersion().toLong() == laterVersion.toLong() );
ASSERT( newManager.getVersion().epoch() == laterVersion.epoch() );
ASSERT( static_cast<int>( newManager.getChunkMap().size() ) == numChunks );
}
};
class ChunkDiffUnitTest {
public:
bool _inverse;
typedef map<BSONObj, BSONObj, BSONObjCmp> RangeMap;
typedef map<string, ChunkVersion> VersionMap;
ChunkDiffUnitTest( bool inverse ) : _inverse( inverse ) {}
// The default pass-through adapter for using config diffs
class DefaultDiffAdapter : public ConfigDiffTracker<BSONObj,string> {
public:
DefaultDiffAdapter() {}
virtual ~DefaultDiffAdapter() {}
virtual bool isTracked( const BSONObj& chunkDoc ) const { return true; }
virtual BSONObj maxFrom( const BSONObj& max ) const { return max; }
virtual pair<BSONObj,BSONObj> rangeFor( const BSONObj& chunkDoc, const BSONObj& min, const BSONObj& max ) const {
return make_pair( min, max );
}
virtual string shardFor( const string& name ) const { return name; }
virtual string nameFrom( const string& shard ) const { return shard; }
};
// Inverts the storage order for chunks from min to max
class InverseDiffAdapter : public DefaultDiffAdapter {
public:
InverseDiffAdapter() {}
virtual ~InverseDiffAdapter() {}
// Disable
virtual BSONObj maxFrom( const BSONObj& max ) const { ASSERT( false ); return max; }
virtual BSONObj minFrom( const BSONObj& min ) const { return min; }
virtual bool isMinKeyIndexed() const { return false; }
virtual pair<BSONObj,BSONObj> rangeFor( const BSONObj& chunkDoc, const BSONObj& min, const BSONObj& max ) const {
return make_pair( max, min );
}
};
// Allow validating with and without ranges (b/c our splits won't actually be updated by the diffs)
void validate( BSONArray chunks, ChunkVersion maxVersion, const VersionMap& maxShardVersions ){
validate( chunks, NULL, maxVersion, maxShardVersions );
}
void validate( BSONArray chunks, const RangeMap& ranges, ChunkVersion maxVersion, const VersionMap& maxShardVersions ){
validate( chunks, (RangeMap*)&ranges, maxVersion, maxShardVersions );
}
// Validates that the ranges and versions are valid given the chunks
void validate( const BSONArray& chunks, RangeMap* ranges, ChunkVersion maxVersion, const VersionMap& maxShardVersions ){
BSONObjIterator it( chunks );
int chunkCount = 0;
ChunkVersion foundMaxVersion;
VersionMap foundMaxShardVersions;
//
// Validate that all the chunks are there and collect versions
//
while( it.more() ){
BSONObj chunkDoc = it.next().Obj();
chunkCount++;
if( ranges != NULL ){
// log() << "Validating chunk " << chunkDoc << " size : " << ranges->size() << " vs " << chunkCount << endl;
RangeMap::iterator chunkRange = ranges->find( _inverse ? chunkDoc["max"].Obj() : chunkDoc["min"].Obj() );
ASSERT( chunkRange != ranges->end() );
ASSERT( chunkRange->second.woCompare( _inverse ? chunkDoc["min"].Obj() : chunkDoc["max"].Obj() ) == 0 );
}
ChunkVersion version =
ChunkVersion::fromBSON(chunkDoc[ChunkType::DEPRECATED_lastmod()]);
if( version > foundMaxVersion ) foundMaxVersion = version;
ChunkVersion shardMaxVersion =
foundMaxShardVersions[chunkDoc[ChunkType::shard()].String()];
if( version > shardMaxVersion ) {
foundMaxShardVersions[chunkDoc[ChunkType::shard()].String() ] = version;
}
}
// Make sure all chunks are accounted for
if( ranges != NULL ) ASSERT( chunkCount == (int) ranges->size() );
// log() << "Validating that all shard versions are up to date..." << endl;
// Validate that all the versions are the same
ASSERT( foundMaxVersion.isEquivalentTo( maxVersion ) );
for( VersionMap::iterator it = foundMaxShardVersions.begin(); it != foundMaxShardVersions.end(); it++ ){
ChunkVersion foundVersion = it->second;
VersionMap::const_iterator maxIt = maxShardVersions.find( it->first );
ASSERT( maxIt != maxShardVersions.end() );
ASSERT( foundVersion.isEquivalentTo( maxIt->second ) );
}
// Make sure all shards are accounted for
ASSERT( foundMaxShardVersions.size() == maxShardVersions.size() );
}
void run() {
int numShards = 10;
int numInitialChunks = 5;
int maxChunks = 100000; // Needed to not overflow the BSONArray's max bytes
int keySize = 2;
BSONArrayBuilder chunksB;
BSONObj lastSplitPt;
ChunkVersion version( 1, 0, OID() );
//
// Generate numChunks with a given key size over numShards
// All chunks have double key values, so we can split them a bunch
//
for( int i = -1; i < numInitialChunks; i++ ){
BSONObjBuilder splitPtB;
for( int k = 0; k < keySize; k++ ){
string field = string( "k" ) + string( 1, (char)('0' + k) );
if( i < 0 )
splitPtB.appendMinKey( field );
else if( i < numInitialChunks - 1 )
splitPtB.append( field, (double)i );
else
splitPtB.appendMaxKey( field );
}
BSONObj splitPt = splitPtB.obj();
if( i >= 0 ){
BSONObjBuilder chunkB;
chunkB.append(ChunkType::min(), lastSplitPt );
chunkB.append(ChunkType::max(), splitPt );
int shardNum = rand( numShards );
chunkB.append(ChunkType::shard(),
"shard" + string( 1, (char)('A' + shardNum) ) );
rand( 2 ) ? version.incMajor() : version.incMinor();
version.addToBSON(chunkB, ChunkType::DEPRECATED_lastmod());
chunksB.append( chunkB.obj() );
}
lastSplitPt = splitPt;
}
BSONArray chunks = chunksB.arr();
// log() << "Chunks generated : " << chunks << endl;
DBClientMockCursor chunksCursor( chunks );
// Setup the empty ranges and versions first
RangeMap ranges;
ChunkVersion maxVersion = ChunkVersion( 0, 0, OID() );
VersionMap maxShardVersions;
// Create a differ which will track our progress
boost::shared_ptr< DefaultDiffAdapter > differ( _inverse ? new InverseDiffAdapter() : new DefaultDiffAdapter() );
differ->attach( "test", ranges, maxVersion, maxShardVersions );
// Validate initial load
differ->calculateConfigDiff( chunksCursor );
validate( chunks, ranges, maxVersion, maxShardVersions );
// Generate a lot of diffs, and keep validating that updating from the diffs always
// gives us the right ranges and versions
int numDiffs = 135; // Makes about 100000 chunks overall
int numChunks = numInitialChunks;
for( int i = 0; i < numDiffs; i++ ){
// log() << "Generating new diff... " << i << endl;
BSONArrayBuilder diffsB;
BSONArrayBuilder newChunksB;
BSONObjIterator chunksIt( chunks );
while( chunksIt.more() ){
BSONObj chunk = chunksIt.next().Obj();
int randChoice = rand( 10 );
if( randChoice < 2 && numChunks < maxChunks ){
// Simulate a split
// log() << " ...starting a split with chunk " << chunk << endl;
BSONObjBuilder leftB;
BSONObjBuilder rightB;
BSONObjBuilder midB;
for( int k = 0; k < keySize; k++ ){
string field = string( "k" ) + string( 1, (char)('0' + k) );
BSONType maxType = chunk[ChunkType::max()].Obj()[field].type();
double max = maxType == NumberDouble ? chunk["max"].Obj()[field].Number() : 0.0;
BSONType minType = chunk[ChunkType::min()].Obj()[field].type();
double min = minType == NumberDouble ?
chunk[ChunkType::min()].Obj()[field].Number() :
0.0;
if( minType == MinKey ){
midB.append( field, max - 1.0 );
}
else if( maxType == MaxKey ){
midB.append( field, min + 1.0 );
}
else {
midB.append( field, ( max + min ) / 2.0 );
}
}
BSONObj midPt = midB.obj();
// Only happens if we can't split the min chunk
if( midPt.isEmpty() ) continue;
leftB.append( chunk[ChunkType::min()] );
leftB.append(ChunkType::max(), midPt );
rightB.append(ChunkType::min(), midPt );
rightB.append(chunk[ChunkType::max()] );
leftB.append(chunk[ChunkType::shard()] );
rightB.append(chunk[ChunkType::shard()] );
version.incMajor();
version._minor = 0;
version.addToBSON(leftB, ChunkType::DEPRECATED_lastmod());
version.incMinor();
version.addToBSON(rightB, ChunkType::DEPRECATED_lastmod());
BSONObj left = leftB.obj();
BSONObj right = rightB.obj();
// log() << " ... split into " << left << " and " << right << endl;
newChunksB.append( left );
newChunksB.append( right );
diffsB.append( right );
diffsB.append( left );
numChunks++;
}
else if( randChoice < 4 && chunksIt.more() ){
// Simulate a migrate
// log() << " ...starting a migrate with chunk " << chunk << endl;
BSONObj prevShardChunk;
while( chunksIt.more() ){
prevShardChunk = chunksIt.next().Obj();
if( prevShardChunk[ChunkType::shard()].String() ==
chunk[ChunkType::shard()].String() ) break;
// log() << "... appending chunk from diff shard: " << prevShardChunk << endl;
newChunksB.append( prevShardChunk );
prevShardChunk = BSONObj();
}
// We need to move between different shards, hence the weirdness in logic here
if( ! prevShardChunk.isEmpty() ){
BSONObjBuilder newShardB;
BSONObjBuilder prevShardB;
newShardB.append(chunk[ChunkType::min()]);
newShardB.append(chunk[ChunkType::max()]);
prevShardB.append(prevShardChunk[ChunkType::min()]);
prevShardB.append(prevShardChunk[ChunkType::max()]);
int shardNum = rand( numShards );
newShardB.append(ChunkType::shard(),
"shard" + string( 1, (char)('A' + shardNum)));
prevShardB.append(prevShardChunk[ChunkType::shard()]);
version.incMajor();
version._minor = 0;
version.addToBSON(newShardB, ChunkType::DEPRECATED_lastmod());
version.incMinor();
version.addToBSON(prevShardB, ChunkType::DEPRECATED_lastmod());
BSONObj newShard = newShardB.obj();
BSONObj prevShard = prevShardB.obj();
// log() << " ... migrated to " << newShard << " and updated " << prevShard << endl;
newChunksB.append( newShard );
newChunksB.append( prevShard );
diffsB.append( newShard );
diffsB.append( prevShard );
}
else{
// log() << "... appending chunk, no more left: " << chunk << endl;
newChunksB.append( chunk );
}
}
else{
// log() << "Appending chunk : " << chunk << endl;
newChunksB.append( chunk );
}
}
BSONArray diffs = diffsB.arr();
chunks = newChunksB.arr();
// log() << "Diffs generated : " << diffs << endl;
// log() << "All chunks : " << chunks << endl;
// Rarely entirely clear out our data
if( rand( 10 ) < 1 ){
diffs = chunks;
ranges.clear();
maxVersion = ChunkVersion( 0, 0, OID() );
maxShardVersions.clear();
}
// log() << "Total number of chunks : " << numChunks << " iteration " << i << endl;
DBClientMockCursor diffCursor( diffs );
differ->calculateConfigDiff( diffCursor );
validate( chunks, ranges, maxVersion, maxShardVersions );
}
}
};
class ChunkDiffUnitTestNormal : public ChunkDiffUnitTest {
public:
ChunkDiffUnitTestNormal() : ChunkDiffUnitTest( false ) {}
};
class ChunkDiffUnitTestInverse : public ChunkDiffUnitTest {
public:
ChunkDiffUnitTestInverse() : ChunkDiffUnitTest( true ) {}
};
class All : public Suite {
public:
All() : Suite( "sharding" ) {
}
void setupTests() {
add< serverandquerytests::test1 >();
add< ChunkManagerCreateBasicTest >();
add< ChunkManagerCreateFullTest >();
add< ChunkManagerLoadBasicTest >();
add< ChunkDiffUnitTestNormal >();
add< ChunkDiffUnitTestInverse >();
}
} myall;
}
|