summaryrefslogtreecommitdiff
path: root/dbtests/d_chunk_manager_tests.cpp
blob: bcfe9fa9ec1608d03a2b8d2027b3fb45e23a0250 (plain)
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
//@file d_chunk_manager_tests.cpp : s/d_chunk_manager.{h,cpp} tests

/**
*    Copyright (C) 2010 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 "dbtests.h"

#include "../s/d_chunk_manager.h"

namespace {

    class BasicTests {
    public:
        void run() {
            BSONObj collection = BSON( "_id"     << "test.foo" <<
                                       "dropped" << false <<
                                       "key"     << BSON( "a" << 1 ) <<
                                       "unique"  << false );

            // single-chunk collection
            BSONArray chunks = BSON_ARRAY( BSON( "_id" << "test.foo-a_MinKey" <<
                                                 "ns"  << "test.foo" <<
                                                 "min" << BSON( "a" << MINKEY ) <<
                                                 "max" << BSON( "a" << MAXKEY ) ) );

            ShardChunkManager s ( collection , chunks );

            BSONObj k1 = BSON( "a" << MINKEY );
            ASSERT( s.belongsToMe( k1 ) );
            BSONObj k2 = BSON( "a" << MAXKEY );
            ASSERT( ! s.belongsToMe( k2 ) );
            BSONObj k3 = BSON( "a" << 1 << "b" << 2 );
            ASSERT( s.belongsToMe( k3 ) );
        }
    };

    class BasicCompoundTests {
    public:
        void run() {
            BSONObj collection = BSON( "_id"     << "test.foo" <<
                                       "dropped" << false <<
                                       "key"     << BSON( "a" << 1 << "b" << 1) <<
                                       "unique"  << false );

            // single-chunk collection
            BSONArray chunks = BSON_ARRAY( BSON( "_id" << "test.foo-a_MinKeyb_MinKey" <<
                                                 "ns"  << "test.foo" <<
                                                 "min" << BSON( "a" << MINKEY << "b" << MINKEY ) <<
                                                 "max" << BSON( "a" << MAXKEY << "b" << MAXKEY ) ) );

            ShardChunkManager s ( collection , chunks );

            BSONObj k1 = BSON( "a" << MINKEY << "b" << MINKEY );
            ASSERT( s.belongsToMe( k1 ) );
            BSONObj k2 = BSON( "a" << MAXKEY << "b" << MAXKEY );
            ASSERT( ! s.belongsToMe( k2 ) );
            BSONObj k3 = BSON( "a" << MINKEY << "b" << 10 );
            ASSERT( s.belongsToMe( k3 ) );
            BSONObj k4 = BSON( "a" << 10 << "b" << 20 );
            ASSERT( s.belongsToMe( k4 ) );
        }
    };

    class RangeTests {
    public:
        void run() {
            BSONObj collection = BSON( "_id"     << "x.y" <<
                                       "dropped" << false <<
                                       "key"     << BSON( "a" << 1 ) <<
                                       "unique"  << false );

            // 3-chunk collection, 2 of them being contiguous
            // [min->10) , [10->20) , <gap> , [30->max)
            BSONArray chunks = BSON_ARRAY( BSON( "_id" << "x.y-a_MinKey" <<
                                                 "ns"  << "x.y" <<
                                                 "min" << BSON( "a" << MINKEY ) <<
                                                 "max" << BSON( "a" << 10 ) ) <<
                                           BSON( "_id" << "x.y-a_10" <<
                                                 "ns"  << "x.y" <<
                                                 "min" << BSON( "a" << 10 ) <<
                                                 "max" << BSON( "a" << 20 ) ) <<
                                           BSON( "_id" << "x.y-a_30" <<
                                                 "ns"  << "x.y" <<
                                                 "min" << BSON( "a" << 30 ) <<
                                                 "max" << BSON( "a" << MAXKEY ) ) );

            ShardChunkManager s ( collection , chunks );

            BSONObj k1 = BSON( "a" << 5 );
            ASSERT( s.belongsToMe( k1 ) );
            BSONObj k2 = BSON( "a" << 10 );
            ASSERT( s.belongsToMe( k2 ) );
            BSONObj k3 = BSON( "a" << 25 );
            ASSERT( ! s.belongsToMe( k3 ) );
            BSONObj k4 = BSON( "a" << 30 );
            ASSERT( s.belongsToMe( k4 ) );
            BSONObj k5 = BSON( "a" << 40 );
            ASSERT( s.belongsToMe( k5 ) );
        }
    };

    class GetNextTests {
    public:
        void run() {

            BSONObj collection = BSON( "_id"     << "x.y" <<
                                       "dropped" << false <<
                                       "key"     << BSON( "a" << 1 ) <<
                                       "unique"  << false );
            // empty collection
            BSONArray chunks1 = BSONArray();
            ShardChunkManager s1( collection , chunks1 );

            BSONObj empty;
            BSONObj arbitraryKey = BSON( "a" << 10 );
            BSONObj foundMin, foundMax;

            ASSERT( s1.getNextChunk( empty , &foundMin , &foundMax ) );
            ASSERT( foundMin.isEmpty() );
            ASSERT( foundMax.isEmpty() );
            ASSERT( s1.getNextChunk( arbitraryKey , &foundMin , &foundMax ) );
            ASSERT( foundMin.isEmpty() );
            ASSERT( foundMax.isEmpty() );

            // single-chunk collection
            // [10->20]
            BSONObj key_a10 = BSON( "a" << 10 );
            BSONObj key_a20 = BSON( "a" << 20 );
            BSONArray chunks2 = BSON_ARRAY( BSON( "_id" << "x.y-a_10" <<
                                                  "ns"  << "x.y" <<
                                                  "min" << key_a10 <<
                                                  "max" << key_a20 ) );
            ShardChunkManager s2( collection , chunks2 );
            ASSERT( s2.getNextChunk( empty , &foundMin , &foundMax ) );
            ASSERT( foundMin.woCompare( key_a10 ) == 0 );
            ASSERT( foundMax.woCompare( key_a20 ) == 0 );

            // 3-chunk collection, 2 of them being contiguous
            // [min->10) , [10->20) , <gap> , [30->max)
            BSONObj key_a30 = BSON( "a" << 30 );
            BSONObj key_min = BSON( "a" << MINKEY );
            BSONObj key_max = BSON( "a" << MAXKEY );
            BSONArray chunks3 = BSON_ARRAY( BSON( "_id" << "x.y-a_MinKey" <<
                                                  "ns"  << "x.y" <<
                                                  "min" << key_min <<
                                                  "max" << key_a10  ) <<
                                            BSON( "_id" << "x.y-a_10" <<
                                                  "ns"  << "x.y" <<
                                                  "min" << key_a10  <<
                                                  "max" << key_a20  ) <<
                                            BSON( "_id" << "x.y-a_30" <<
                                                  "ns"  << "x.y" <<
                                                  "min" << key_a30  <<
                                                  "max" << key_max  ) );
            ShardChunkManager s3( collection , chunks3 );
            ASSERT( ! s3.getNextChunk( empty , &foundMin , &foundMax ) ); // not eof
            ASSERT( foundMin.woCompare( key_min ) == 0 );
            ASSERT( foundMax.woCompare( key_a10 ) == 0 );
            ASSERT( ! s3.getNextChunk( key_a10 , &foundMin , &foundMax ) );
            ASSERT( foundMin.woCompare( key_a30 ) == 0 );
            ASSERT( foundMax.woCompare( key_max ) == 0 );
            ASSERT( s3.getNextChunk( key_a30 , &foundMin , &foundMax ) );
        }
    };

    class DeletedTests {
    public:
        void run() {
            BSONObj collection = BSON( "_id"     << "test.foo" <<
                                       "dropped" << "true" );

            BSONArray chunks = BSONArray();

            ASSERT_EXCEPTION( ShardChunkManager s ( collection , chunks ) , UserException );
        }
    };

    class ClonePlusTests {
    public:
        void run() {
            BSONObj collection = BSON( "_id"     << "test.foo" <<
                                       "dropped" << false <<
                                       "key"     << BSON( "a" << 1 << "b" << 1 ) <<
                                       "unique"  << false );
            // 1-chunk collection
            // [10,0-20,0)
            BSONArray chunks = BSON_ARRAY( BSON( "_id" << "test.foo-a_MinKey" <<
                                                 "ns"  << "test.foo" <<
                                                 "min" << BSON( "a" << 10 << "b" << 0 ) <<
                                                 "max" << BSON( "a" << 20 << "b" << 0 ) ) );

            ShardChunkManager s ( collection , chunks );

            // new chunk [20,0-30,0)
            BSONObj min = BSON( "a" << 20 << "b" << 0 );
            BSONObj max = BSON( "a" << 30 << "b" << 0 );
            ShardChunkManagerPtr cloned( s.clonePlus( min , max , 1 /* TODO test version */ ) );

            BSONObj k1 = BSON( "a" << 5 << "b" << 0 );
            ASSERT( ! cloned->belongsToMe( k1 ) );
            BSONObj k2 = BSON( "a" << 20 << "b" << 0 );
            ASSERT( cloned->belongsToMe( k2 ) );
            BSONObj k3 = BSON( "a" << 25 << "b" << 0 );
            ASSERT( cloned->belongsToMe( k3 ) );
            BSONObj k4 = BSON( "a" << 30 << "b" << 0 );
            ASSERT( ! cloned->belongsToMe( k4 ) );
        }
    };

    class ClonePlusExceptionTests {
    public:
        void run() {
            BSONObj collection = BSON( "_id"     << "test.foo" <<
                                       "dropped" << false <<
                                       "key"     << BSON( "a" << 1 << "b" << 1 ) <<
                                       "unique"  << false );
            // 1-chunk collection
            // [10,0-20,0)
            BSONArray chunks = BSON_ARRAY( BSON( "_id" << "test.foo-a_MinKey" <<
                                                 "ns"  << "test.foo" <<
                                                 "min" << BSON( "a" << 10 << "b" << 0 ) <<
                                                 "max" << BSON( "a" << 20 << "b" << 0 ) ) );

            ShardChunkManager s ( collection , chunks );

            // [15,0-25,0) overlaps [10,0-20,0)
            BSONObj min = BSON( "a" << 15 << "b" << 0 );
            BSONObj max = BSON( "a" << 25 << "b" << 0 );
            ASSERT_EXCEPTION( s.clonePlus ( min , max , 1 /* TODO test version */ ) , UserException );
        }
    };

    class CloneMinusTests {
    public:
        void run() {
            BSONObj collection = BSON( "_id"     << "x.y" <<
                                       "dropped" << false <<
                                       "key"     << BSON( "a" << 1 << "b" << 1 ) <<
                                       "unique"  << false );

            // 2-chunk collection
            // [10,0->20,0) , <gap> , [30,0->40,0)
            BSONArray chunks = BSON_ARRAY( BSON( "_id" << "x.y-a_10b_0" <<
                                                 "ns"  << "x.y" <<
                                                 "min" << BSON( "a" << 10 << "b" << 0 ) <<
                                                 "max" << BSON( "a" << 20 << "b" << 0 ) ) <<
                                           BSON( "_id" << "x.y-a_30b_0" <<
                                                 "ns"  << "x.y" <<
                                                 "min" << BSON( "a" << 30 << "b" << 0 ) <<
                                                 "max" << BSON( "a" << 40 << "b" << 0 ) ) );

            ShardChunkManager s ( collection , chunks );

            // deleting chunk [10,0-20,0)
            BSONObj min = BSON( "a" << 10 << "b" << 0 );
            BSONObj max = BSON( "a" << 20 << "b" << 0 );
            ShardChunkManagerPtr cloned( s.cloneMinus( min , max , 1 /* TODO test version */ ) );

            BSONObj k1 = BSON( "a" << 5 << "b" << 0 );
            ASSERT( ! cloned->belongsToMe( k1 ) );
            BSONObj k2 = BSON( "a" << 15 << "b" << 0 );
            ASSERT( ! cloned->belongsToMe( k2 ) );
            BSONObj k3 = BSON( "a" << 30 << "b" << 0 );
            ASSERT( cloned->belongsToMe( k3 ) );
            BSONObj k4 = BSON( "a" << 35 << "b" << 0 );
            ASSERT( cloned->belongsToMe( k4 ) );
            BSONObj k5 = BSON( "a" << 40 << "b" << 0 );
            ASSERT( ! cloned->belongsToMe( k5 ) );
        }
    };

    class CloneMinusExceptionTests {
    public:
        void run() {
            BSONObj collection = BSON( "_id"     << "x.y" <<
                                       "dropped" << false <<
                                       "key"     << BSON( "a" << 1 << "b" << 1 ) <<
                                       "unique"  << false );

            // 2-chunk collection
            // [10,0->20,0) , <gap> , [30,0->40,0)
            BSONArray chunks = BSON_ARRAY( BSON( "_id" << "x.y-a_10b_0" <<
                                                 "ns"  << "x.y" <<
                                                 "min" << BSON( "a" << 10 << "b" << 0 ) <<
                                                 "max" << BSON( "a" << 20 << "b" << 0 ) ) <<
                                           BSON( "_id" << "x.y-a_30b_0" <<
                                                 "ns"  << "x.y" <<
                                                 "min" << BSON( "a" << 30 << "b" << 0 ) <<
                                                 "max" << BSON( "a" << 40 << "b" << 0 ) ) );

            ShardChunkManager s ( collection , chunks );

            // deleting non-existing chunk [25,0-28,0)
            BSONObj min1 = BSON( "a" << 25 << "b" << 0 );
            BSONObj max1 = BSON( "a" << 28 << "b" << 0 );
            ASSERT_EXCEPTION( s.cloneMinus( min1 , max1 , 1 /* TODO test version */ ) , UserException );


            // deletin an overlapping range (not exactly a chunk) [15,0-25,0)
            BSONObj min2 = BSON( "a" << 15 << "b" << 0 );
            BSONObj max2 = BSON( "a" << 25 << "b" << 0 );
            ASSERT_EXCEPTION( s.cloneMinus( min2 , max2 , 1 /* TODO test version */ ) , UserException );
        }
    };

    class CloneSplitTests {
    public:
        void run() {
            BSONObj collection = BSON( "_id"     << "test.foo" <<
                                       "dropped" << false <<
                                       "key"     << BSON( "a" << 1 << "b" << 1 ) <<
                                       "unique"  << false );
            // 1-chunk collection
            // [10,0-20,0)
            BSONObj min = BSON( "a" << 10 << "b" << 0 );
            BSONObj max = BSON( "a" << 20 << "b" << 0 );
            BSONArray chunks = BSON_ARRAY( BSON( "_id" << "test.foo-a_MinKey"
                                                 << "ns"  << "test.foo"
                                                 << "min" << min
                                                 << "max" << max ) );

            ShardChunkManager s ( collection , chunks );

            BSONObj split1 = BSON( "a" << 15 << "b" << 0 );
            BSONObj split2 = BSON( "a" << 18 << "b" << 0 );
            vector<BSONObj> splitKeys;
            splitKeys.push_back( split1 );
            splitKeys.push_back( split2 );
            ShardChunkVersion version( 1 , 99 ); // first chunk 1|99 , second 1|100
            ShardChunkManagerPtr cloned( s.cloneSplit( min , max , splitKeys , version ) );

            version.incMinor(); /* second chunk 1|100, first split point */
            version.incMinor(); /* third chunk 1|101, second split point */
            ASSERT_EQUALS( cloned->getVersion() , version /* 1|101 */ );
            ASSERT_EQUALS( s.getNumChunks() , 1u );
            ASSERT_EQUALS( cloned->getNumChunks() , 3u );
            ASSERT( cloned->belongsToMe( min ) );
            ASSERT( cloned->belongsToMe( split1 ) );
            ASSERT( cloned->belongsToMe( split2 ) );
            ASSERT( ! cloned->belongsToMe( max ) );
        }
    };

    class CloneSplitExceptionTests {
    public:
        void run() {
            BSONObj collection = BSON( "_id"     << "test.foo" <<
                                       "dropped" << false <<
                                       "key"     << BSON( "a" << 1 << "b" << 1 ) <<
                                       "unique"  << false );
            // 1-chunk collection
            // [10,0-20,0)
            BSONObj min = BSON( "a" << 10 << "b" << 0 );
            BSONObj max = BSON( "a" << 20 << "b" << 0 );
            BSONArray chunks = BSON_ARRAY( BSON( "_id" << "test.foo-a_MinKey"
                                                 << "ns"  << "test.foo"
                                                 << "min" << min
                                                 << "max" << max ) );

            ShardChunkManager s ( collection , chunks );

            BSONObj badSplit = BSON( "a" << 5 << "b" << 0 );
            vector<BSONObj> splitKeys;
            splitKeys.push_back( badSplit );
            ASSERT_EXCEPTION( s.cloneSplit( min , max , splitKeys , ShardChunkVersion( 1 ) ) , UserException );

            BSONObj badMax = BSON( "a" << 25 << "b" << 0 );
            BSONObj split = BSON( "a" << 15 << "b" << 0 );
            splitKeys.clear();
            splitKeys.push_back( split );
            ASSERT_EXCEPTION( s.cloneSplit( min , badMax, splitKeys , ShardChunkVersion( 1 ) ) , UserException );
        }
    };

    class EmptyShardTests {
    public:
        void run() {
            BSONObj collection = BSON( "_id"     << "test.foo" <<
                                       "dropped" << false <<
                                       "key"     << BSON( "a" << 1 ) <<
                                       "unique"  << false );

            // no chunks on this shard
            BSONArray chunks;

            // shard can have zero chunks for an existing collection
            // version should be 0, though
            ShardChunkManager s( collection , chunks );
            ASSERT_EQUALS( s.getVersion() , ShardChunkVersion( 0 ) );
            ASSERT_EQUALS( s.getNumChunks() , 0u );
        }
    };

    class LastChunkTests {
    public:
        void run() {
            BSONObj collection = BSON( "_id"     << "test.foo" <<
                                       "dropped" << false <<
                                       "key"     << BSON( "a" << 1 ) <<
                                       "unique"  << false  );

            // 1-chunk collection
            // [10->20)
            BSONArray chunks = BSON_ARRAY( BSON( "_id" << "test.foo-a_10" <<
                                                 "ns"  << "test.foo" <<
                                                 "min" << BSON( "a" << 10 ) <<
                                                 "max" << BSON( "a" << 20 ) ) );

            ShardChunkManager s( collection , chunks );
            BSONObj min = BSON( "a" << 10 );
            BSONObj max = BSON( "a" << 20 );

            // if we remove the only chunk, the only version accepted is 0
            ShardChunkVersion nonZero = 99;
            ASSERT_EXCEPTION( s.cloneMinus( min , max , nonZero ) , UserException );
            ShardChunkManagerPtr empty( s.cloneMinus( min , max , 0 ) );
            ASSERT_EQUALS( empty->getVersion() , ShardChunkVersion( 0 ) );
            ASSERT_EQUALS( empty->getNumChunks() , 0u );
            BSONObj k = BSON( "a" << 15 << "b" << 0 );
            ASSERT( ! empty->belongsToMe( k ) );

            // we can add a chunk to an empty manager
            // version should be provided
            ASSERT_EXCEPTION( empty->clonePlus( min , max , 0 ) , UserException );
            ShardChunkManagerPtr cloned( empty->clonePlus( min , max , nonZero ) );
            ASSERT_EQUALS( cloned->getVersion(), nonZero );
            ASSERT_EQUALS( cloned->getNumChunks() , 1u );
            ASSERT( cloned->belongsToMe( k ) );
        }
    };

    class ShardChunkManagerSuite : public Suite {
    public:
        ShardChunkManagerSuite() : Suite ( "shard_chunk_manager" ) {}

        void setupTests() {
            add< BasicTests >();
            add< BasicCompoundTests >();
            add< RangeTests >();
            add< GetNextTests >();
            add< DeletedTests >();
            add< ClonePlusTests >();
            add< ClonePlusExceptionTests >();
            add< CloneMinusTests >();
            add< CloneMinusExceptionTests >();
            add< CloneSplitTests >();
            add< CloneSplitExceptionTests >();
            add< EmptyShardTests >();
            add< LastChunkTests >();
        }
    } shardChunkManagerSuite;

}  // anonymous namespace