summaryrefslogtreecommitdiff
path: root/jstests/sharding/index1.js
blob: 57092b3a5660bd29d785a6b20fd65808b0e2ded2 (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
// SERVER-2326 - make sure that sharding only works with unique indices
(function() {

var s = new ShardingTest({ name: "shard_index", shards: 2, mongos: 1 });

// Regenerate fully because of SERVER-2782
for ( var i = 0; i < 22; i++ ) {
        
    var coll = s.admin._mongo.getDB( "test" ).getCollection( "foo" + i )
    coll.drop()

    var bulk = coll.initializeUnorderedBulkOp();
    for ( var j = 0; j < 300; j++ ) {
        bulk.insert({ num: j, x: 1 });
    }
    assert.writeOK(bulk.execute());

    if (i == 0) {
        s.adminCommand({ enablesharding: "" + coll._db });
        s.ensurePrimaryShard(coll.getDB().getName(), 'shard0001');
    }

    print("\n\n\n\n\nTest # " + i)
    
    if ( i == 0 ) {

        // Unique index exists, but not the right one.
        coll.ensureIndex( { num : 1 }, { unique : true } )
        coll.ensureIndex( { x : 1 } )

        passed = false
        try {
            s.adminCommand( { shardcollection : "" + coll, key : { x : 1 } } )
            passed = true
        } catch (e) {
            print( e )
        }
        assert( !passed, "Should not shard collection when another unique index exists!")

    }
    if ( i == 1 ) {
        
        // Unique index exists as prefix, also index exists
        coll.ensureIndex( { x : 1 } )
        coll.ensureIndex( { x : 1, num : 1 }, { unique : true } )
        
        try{
            s.adminCommand({ shardcollection : "" + coll, key : { x : 1 } })
        }
        catch(e){
            print(e)
            assert( false, "Should be able to shard non-unique index without unique option.")
        }
        
    }
    if ( i == 2 ) {
        // Non-unique index exists as prefix, also index exists.  No unique index.
        coll.ensureIndex( { x : 1 } )
        coll.ensureIndex( { x : 1, num : 1 } )

        passed = false;
        try{
            s.adminCommand({ shardcollection : "" + coll, key : { x : 1 } })
            passed = true;

        }
        catch( e ){
            print(e)
            assert( !passed, "Should be able to shard collection with no unique index if unique not specified.")
        }
    }
    if ( i == 3 ) {

        // Unique index exists as prefix, also unique index exists
        coll.ensureIndex( { num : 1 }, { unique : true })
        coll.ensureIndex( { num : 1 , x : 1 }, { unique : true } )

        try{
            s.adminCommand({ shardcollection : "" + coll, key : { num : 1 }, unique : true })
        }
        catch( e ){
            print(e)
            assert( false, "Should be able to shard collection with unique prefix index.")
        }

    }
    if ( i == 4 ) {

        // Unique index exists as id, also unique prefix index exists
        coll.ensureIndex( { _id : 1, num : 1 }, { unique : true } )

        try{
            s.adminCommand({ shardcollection : "" + coll, key : { _id : 1 }, unique : true })
        }
        catch( e ){
            print(e)
            assert( false, "Should be able to shard collection with unique id index.")
        }
        
    }
    if ( i == 5 ) {

        // Unique index exists as id, also unique prefix index exists
        coll.ensureIndex( { _id : 1, num : 1 }, { unique : true } )

        try{
            s.adminCommand({ shardcollection : "" + coll, key : { _id : 1, num : 1 }, unique : true })
        }
        catch( e ){
            print(e)
            assert( false, "Should be able to shard collection with unique combination id index.")
        }
        
    }
    if ( i == 6 ) {

        coll.remove({})
        
        // Unique index does not exist, also unique prefix index exists
        coll.ensureIndex( { num : 1, _id : 1 }, { unique : true } )

        try{
            s.adminCommand({ shardcollection : "" + coll, key : { num : 1 }, unique : true })
        }
        catch( e ){
            print(e)
            assert( false, "Should be able to shard collection with no unique index but with a unique prefix index.")
        }
        
        printjson( coll.getIndexes() )
        
        // Make sure the index created is unique!
        assert.eq( 1, coll.getIndexes().filter( function(z) { return friendlyEqual( z.key, { num : 1 } ) && z.unique; } ).length );
        
    }
    if ( i == 7 ) {
        coll.remove({})

        // No index exists

        try{
            assert.eq( coll.find().itcount(), 0 )
            s.adminCommand({ shardcollection : "" + coll, key : { num : 1 } })
        }
        catch( e ){
            print(e)
            assert( false, "Should be able to shard collection with no index on shard key.")
        }
    }
    if ( i == 8 ) {
        coll.remove({})

        // No index exists

        passed = false
        try{
            assert.eq( coll.find().itcount(), 0 )
            s.adminCommand({ shardcollection : "" + coll, key : { num : 1 }, unique : true })
            passed = true
        }
        catch( e ){
            print(e)
        }
        assert( passed, "Should be able to shard collection with unique flag but with no unique index on shard key, if coll empty.")
        
        printjson( coll.getIndexes() )
        
        // Make sure the index created is unique!
        assert.eq( 1, coll.getIndexes().filter( function(z) { return friendlyEqual( z.key, { num : 1 } ) && z.unique; } ).length );
    }
    if ( i == 9 ) {

        // Unique index exists on a different field as well
        coll.ensureIndex( { num : 1 }, { unique : true } )
        coll.ensureIndex( { x : 1 } )

        passed = false
        try {
            s.adminCommand( { shardcollection : "" + coll, key : { x : 1 } } )
            passed = true
        } catch (e) {
            print( e )
        }
        assert( !passed, "Should not shard collection when another unique index exists!" )
    }
    if ( i == 10 ){

        //try sharding non-empty collection without any index
        passed = false;
        try{
            s.adminCommand( { shardcollection : "" + coll, key : { num : 1 } } );
            passed = true;
        }
        catch( e ){
            print(e);
        }
        assert( !passed , "Should not be able to shard without index");

        //now add containing index and try sharding by prefix
        coll.ensureIndex( {num : 1, x : 1} );

        try{
            s.adminCommand( { shardcollection : "" + coll, key : { num : 1 } } );
            passed = true;
        }
        catch( e ){
            print(e);
        }
        assert( passed , "Should be able to shard collection with prefix of existing index");

        printjson( coll.getIndexes() );

        //make sure no extra index is created
        assert.eq( 2, coll.getIndexes().length );
    }
    if ( i == 11 ){
        coll.remove({})

        //empty collection with useful index. check new index not created
        coll.ensureIndex( {num : 1, x : 1} );

        try{
            s.adminCommand( { shardcollection : "" + coll, key : { num : 1 } } );
            passed = true;
        }
        catch( e ){
            print(e);
        }
        assert( passed , "Should be able to shard collection with prefix of existing index");

        printjson( coll.getIndexes() );

        //make sure no extra index is created
        assert.eq( 2, coll.getIndexes().length );
    }
    if ( i == 12 ){

        //check multikey values for x make index unusable for shard key
        coll.save({num : 100 , x : [2,3] });
        coll.ensureIndex( {num : 1, x : 1} );

        passed = false;
        try{
            s.adminCommand( { shardcollection : "" + coll, key : { num : 1 } } );
            passed = true;
        }
        catch( e ){
            print(e);
        }
        assert( !passed , "Should not be able to shard collection with mulikey index");
    }
    if ( i == 13 ){

        coll.save({ num : [100, 200], x : 10});
        coll.ensureIndex( { num : 1, x : 1} );

        passed = false;
        try{
            s.adminCommand( { shardcollection : "" + coll, key : { num : 1 } } );
            passed = true;
        }
        catch( e ){
            print(e);
        }
        assert( !passed , "Should not be able to shard collection with mulikey index");

    }
    if ( i == 14 ){

        coll.save({ num : 100, x : 10, y : [1,2]});
        coll.ensureIndex( { num : 1, x : 1, y : 1} );

        passed = false;
        try{
            s.adminCommand( { shardcollection : "" + coll, key : { num : 1 } } );
            passed = true;
        }
        catch( e ){
            print(e);
        }
        assert( !passed , "Should not be able to shard collection with mulikey index");

    }
    if ( i == 15 ) {

        // try sharding with a hashed index
        coll.ensureIndex( { num : "hashed"} );

        try{
            s.adminCommand( { shardcollection : "" + coll, key : { num : "hashed" } } );
        }
        catch( e ){
            print(e);
            assert( false, "Should be able to shard collection with hashed index.");
        }
    }
    if ( i == 16 ) {

        // create hashed index, but try to declare it unique when sharding
        coll.ensureIndex( { num : "hashed"} )

        passed = false;
        try{
            s.adminCommand({ shardcollection : "" + coll, key : { num : "hashed" }, unique : true});
            passed = true;
        }
        catch( e ){
            print(e);
        }
        assert( !passed , "Should not be able to declare hashed shard key unique.");

    }
    if ( i == 17 ) {

        // create hashed index, but unrelated unique index present
        coll.ensureIndex( { x : "hashed" } );
        coll.ensureIndex( { num : 1 }, { unique : true} );

        passed = false;
        try {
            s.adminCommand( { shardcollection : "" + coll, key : { x : "hashed" } } );
            passed = true;
        }
        catch (e) {
            print( e );
        }
        assert( !passed, "Should not be able to shard on hashed index with another unique index" );

    }
    if ( i == 18 ) {

        // create hashed index, and a regular unique index exists on same field
        coll.ensureIndex( { num : "hashed" } );
        coll.ensureIndex( { num : 1 }, { unique : true } );

        try{
            s.adminCommand({ shardcollection : "" + coll, key : { num : "hashed" } } );
        }
        catch( e ){
            print(e);
            assert( false, "Should be able to shard coll with hashed and regular unique index");
        }
    }
    if ( i == 19 ) {
        // Create sparse index.
        coll.ensureIndex( { x : 1 }, { sparse : true } );

        passed = false;
        try {
            s.adminCommand( { shardcollection : "" + coll, key : { x : 1 } } );
            passed = true;
        }
        catch ( e ) {
            print( e );
        }
        assert( !passed, "Should not be able to shard coll with sparse index" );
    }
    if ( i == 20 ) {
        // Create partial index.
        coll.ensureIndex( { x : 1 }, { filter: { num : { $gt : 1 } } } );

        passed = false;
        try {
            s.adminCommand( { shardcollection : "" + coll, key : { x : 1 } } );
            passed = true;
        }
        catch ( e ) {
            print( e );
        }
        assert( !passed, "Should not be able to shard coll with partial index" );
    }
    if ( i == 21 ) {
        // Ensure that a collection with a normal index and a partial index can be sharded, where
        // both are prefixed by the shard key.

        coll.ensureIndex( { x : 1, num : 1 }, { filter: { num : { $gt : 1 } } } );
        coll.ensureIndex( { x : 1, num : -1 } );

        try {
            s.adminCommand( { shardcollection : "" + coll, key : { x : 1 } } );
        }
        catch ( e ) {
            print( e );
            assert( false, "Should be able to shard coll with regular and partial index");
        }
    }
}

s.stop();

})();