summaryrefslogtreecommitdiff
path: root/jstests/sharding/sharding_index_catalog_API.js
blob: 02f549311f9bd4060b438c154a61b25c33d8ac0a (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
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
/**
 * Tests that the global indexes API correctly creates and drops an index from the catalog.
 *
 * @tags: [
 *   multiversion_incompatible,
 *   featureFlagGlobalIndexesShardingCatalog,
 * ]
 */

(function() {
'use strict';

function registerIndex(rs, nss, pattern, name, uuid) {
    rs.getPrimary().adminCommand({
        _shardsvrRegisterIndex: nss,
        keyPattern: pattern,
        options: {global: true},
        name: name,
        collectionUUID: uuid,
        indexCollectionUUID: UUID(),
        lastmod: Timestamp(0, 0), /* Placeholder value, a cluster time will be set by the API. */
        writeConcern: {w: 'majority'}
    });
}

function unregisterIndex(rs, nss, name, uuid) {
    rs.getPrimary().adminCommand({
        _shardsvrUnregisterIndex: nss,
        name: name,
        collectionUUID: uuid,
        lastmod: Timestamp(0, 0), /* Placeholder value, a cluster time will be set by the API. */
        writeConcern: {w: 'majority'}
    });
}

const st = new ShardingTest({mongos: 1, shards: {rs0: {nodes: 3}, rs1: {nodes: 3}}});

const shard0 = st.shard0.shardName;
const shard1 = st.shard1.shardName;
const dbName = 'foo';
const collectionName = 'test';
const collection2Name = 'test2';
const collection3Name = 'test3';
const collection4Name = 'test4';
const collection5Name = 'test5';
const collection6Name = 'test6';
const nss = dbName + '.' + collectionName;
const nss2 = dbName + '.' + collection2Name;
const nss3 = dbName + '.' + collection3Name;
const nss4 = dbName + '.' + collection4Name;
const nss5 = dbName + '.' + collection5Name;
const nss6 = dbName + '.' + collection6Name;
const index1Pattern = {
    x: 1
};
const index1Name = 'x_1';
const index2Pattern = {
    y: 1
};
const index2Name = 'y_1';
const index3Pattern = {
    z: 1
};
const index3Name = 'z_1';
const index4Pattern = {
    w: 1
};
const index4Name = 'w_1';
const configsvrIndexCatalog = 'config.csrs.indexes';
const configsvrCollectionCatalog = 'config.collections';
const shardIndexCatalog = 'config.shard.indexes';
const shardCollectionCatalog = 'config.shard.collections';

st.s.adminCommand({enableSharding: dbName, primaryShard: shard0});
assert.commandWorked(st.s.adminCommand({shardCollection: nss, key: {_id: 1}}));
const collectionUUID = st.s.getCollection('config.collections').findOne({_id: nss}).uuid;

registerIndex(st.rs0, nss, index1Pattern, index1Name, collectionUUID);

jsTestLog(
    "Check that we created the index on the config server and on shard0, which is the only shard with data.");
assert.eq(1, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));
assert.eq(1, st.configRS.getPrimary().getCollection(configsvrCollectionCatalog).countDocuments({
    uuid: collectionUUID,
    indexVersion: {$exists: true}
}));
assert.eq(1, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));
assert.eq(1, st.rs0.getPrimary().getCollection(shardCollectionCatalog).countDocuments({
    uuid: collectionUUID,
    indexVersion: {$exists: true}
}));
assert.eq(1, st.rs0.getSecondary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));
assert.eq(1, st.rs0.getSecondary().getCollection(shardCollectionCatalog).countDocuments({
    uuid: collectionUUID,
    indexVersion: {$exists: true}
}));
assert.eq(0, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));

jsTestLog("Ensure we committed in the right collection.");
if (TestData.configShard) {
    // The config server is shard0 in config shard mode, so it should have both collections.
    assert.eq(1, st.configRS.getPrimary().getCollection(shardIndexCatalog).countDocuments({
        collectionUUID: collectionUUID,
        name: index1Name
    }));
    assert.eq(1, st.rs0.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
        collectionUUID: collectionUUID,
        name: index1Name
    }));
} else {
    assert.eq(0, st.configRS.getPrimary().getCollection(shardIndexCatalog).countDocuments({
        collectionUUID: collectionUUID,
        name: index1Name
    }));
    assert.eq(0, st.rs0.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
        collectionUUID: collectionUUID,
        name: index1Name
    }));
}
assert.eq(0, st.rs1.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));

st.s.adminCommand({split: nss, middle: {_id: 0}});
st.s.adminCommand({moveChunk: nss, find: {_id: 0}, to: shard1});

jsTestLog("Verify indexes are copied to the new shard after a migration.");
assert.eq(1, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));
assert.eq(1, st.rs1.getSecondary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));

jsTestLog("AND the index version.");
const indexVersionRS0 = st.rs0.getPrimary()
                            .getDB('config')
                            .shard.collections.findOne({uuid: collectionUUID})
                            .indexVersion;
const indexVersionRS1 = st.rs1.getPrimary()
                            .getDB('config')
                            .shard.collections.findOne({uuid: collectionUUID})
                            .indexVersion;
assert.eq(indexVersionRS0, indexVersionRS1);

registerIndex(st.rs0, nss, index2Pattern, index2Name, collectionUUID);

jsTestLog(
    "Check that we created the index on the config server and on both shards because there is data everywhere.");
assert.eq(1, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));
assert.eq(1, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));
assert.eq(1, st.rs0.getSecondary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));
assert.eq(1, st.rs0.getSecondary().getCollection(shardCollectionCatalog).countDocuments({
    uuid: collectionUUID,
    indexVersion: {$exists: true}
}));
assert.eq(1, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));
assert.eq(1, st.rs1.getSecondary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));

jsTestLog("Check we didn't commit in a wrong collection.");
if (TestData.configShard) {
    // The config server is shard0 in config shard mode, so it should have both collections.
    assert.eq(1, st.configRS.getPrimary().getCollection(shardIndexCatalog).countDocuments({
        collectionUUID: collectionUUID,
        name: index2Name
    }));
    assert.eq(1, st.rs0.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
        collectionUUID: collectionUUID,
        name: index2Name
    }));
} else {
    assert.eq(0, st.configRS.getPrimary().getCollection(shardIndexCatalog).countDocuments({
        collectionUUID: collectionUUID,
        name: index2Name
    }));
    assert.eq(0, st.rs0.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
        collectionUUID: collectionUUID,
        name: index2Name
    }));
}
assert.eq(0, st.rs1.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));

jsTestLog("Drop index test.");
unregisterIndex(st.rs0, nss, index2Name, collectionUUID);

assert.eq(0, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));
assert.eq(0, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));
assert.eq(0, st.rs0.getSecondary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));
assert.eq(0, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));
assert.eq(0, st.rs1.getSecondary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));

jsTestLog(
    "Check global index consolidation. Case 1: 1 leftover index dropped. Initial state: there must be only one index in the shards.");
assert.eq(1, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));
assert.eq(1, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));
assert.eq(1, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));
assert.eq(1, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));

st.s.adminCommand({moveChunk: nss, find: {_id: 0}, to: shard0});
unregisterIndex(st.rs0, nss, index1Name, collectionUUID);

jsTestLog("Check that there is leftover data.");
assert.eq(0, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));
assert.eq(1, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));
assert.eq(1, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));

jsTestLog("Consolidation of indexes for case 1.");
st.s.adminCommand({moveChunk: nss, find: {_id: 0}, to: shard1});
assert.eq(0, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));
assert.eq(0, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));

jsTestLog("Case 2: 1 leftover index dropped, and another index created. Add one index.");
registerIndex(st.rs0, nss, index1Pattern, index1Name, collectionUUID);

jsTestLog("Move only chunk to shard0, leaving 'garbage'.");
st.s.adminCommand({moveChunk: nss, find: {_id: 0}, to: shard0});
assert.eq(1, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));

jsTestLog("Drop and create another index.");

unregisterIndex(st.rs0, nss, index1Name, collectionUUID);
registerIndex(st.rs0, nss, index2Pattern, index2Name, collectionUUID);

jsTestLog("We'll find leftover index info.");
assert.eq(1, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));
assert.eq(0, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));
assert.eq(1, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));
assert.eq(0, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));

jsTestLog("Moving the chunk back, this will consolidate the indexes.");
st.s.adminCommand({moveChunk: nss, find: {_id: 0}, to: shard1});
assert.eq(1, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));
assert.eq(0, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));
assert.eq(1, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index2Name
}));
assert.eq(0, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index1Name
}));

jsTestLog("Case 3: Multi-index consolidation test. Create index1 again.");
registerIndex(st.rs0, nss, index1Pattern, index1Name, collectionUUID);

jsTestLog("Move the chunk back and clear the indexes.");
st.s.adminCommand({moveChunk: nss, find: {_id: 0}, to: shard0});

unregisterIndex(st.rs0, nss, index1Name, collectionUUID);
unregisterIndex(st.rs0, nss, index2Name, collectionUUID);

jsTestLog("Check for leftover data.");
assert.eq(0, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));
assert.eq(2, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));

jsTestLog("Create the new indexes.");
registerIndex(st.rs0, nss, index3Pattern, index3Name, collectionUUID);
registerIndex(st.rs0, nss, index4Pattern, index4Name, collectionUUID);

jsTestLog("Move chunk, it should consolidate the indexes.");
st.s.adminCommand({moveChunk: nss, find: {_id: 0}, to: shard1});
assert.eq(2, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));
assert.eq(2, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));
assert.eq(1, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index3Name
}));
assert.eq(1, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index4Name
}));
assert.eq(1, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index3Name
}));
assert.eq(1, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index4Name
}));

jsTestLog(
    "Rename test. Have one sharded collection with indexVersion 1. Have a second collection with indexVersion 2. Rename A to B, indexVersion should be bumped.");
assert.commandWorked(st.s.adminCommand({shardCollection: nss2, key: {_id: 1}}));
const collection2UUID = st.s.getCollection('config.collections').findOne({_id: nss2}).uuid;

registerIndex(st.rs0, nss2, index1Pattern, index1Name, collection2UUID);
registerIndex(st.rs0, nss2, index2Pattern, index2Name, collection2UUID);

assert.eq(2, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection2UUID
}));
assert.eq(1, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection2UUID,
    name: index1Name
}));
assert.eq(1, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection2UUID,
    name: index2Name
}));

jsTestLog(
    "Check correct index metadata after rename. Case 1: rename existing sharded collection with destination sharded collection. Snapshot indexVersions for future checks.");
const indexVersionNss = st.configRS.getPrimary()
                            .getCollection(configsvrCollectionCatalog)
                            .findOne({_id: nss})
                            .indexVersion;
const indexVersionNss2 = st.configRS.getPrimary()
                             .getCollection(configsvrCollectionCatalog)
                             .findOne({_id: nss2})
                             .indexVersion;
jsTestLog(
    "This is the case where the bump makes sense, nss has different indexes than nss2, so, after the rename, we should keep the original indexes of nss.");
assert(timestampCmp(indexVersionNss, indexVersionNss2));

assert.commandWorked(
    st.s.getDB(dbName).adminCommand({renameCollection: nss, to: nss2, dropTarget: true}));

jsTestLog("Check CSRS metadata.");
assert.eq(2, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
}));
assert.eq(0, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collection2UUID,
}));
assert.eq(1, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index3Name
}));
assert.eq(1, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index4Name
}));
jsTestLog("Check RS0 metadata.");
assert.eq(2, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));
assert.eq(0, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection2UUID
}));
assert.eq(1, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index3Name
}));
assert.eq(1, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index4Name
}));
jsTestLog("Check RS1 metadata.");
assert.eq(2, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));
assert.eq(0, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection2UUID
}));
assert.eq(1, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index3Name
}));
assert.eq(1, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
    name: index4Name
}));
const indexVersionAfterFirstRename = st.configRS.getPrimary()
                                         .getCollection(configsvrCollectionCatalog)
                                         .findOne({_id: nss2})
                                         .indexVersion;
assert(timestampCmp(indexVersionNss, indexVersionAfterFirstRename) < 0);
assert(timestampCmp(indexVersionNss2, indexVersionAfterFirstRename) < 0);

st.s.getCollection(nss3).insert({x: 1});

jsTestLog("Case 2: Rename from unsharded collection to sharded collection clears the indexes");

assert.commandWorked(
    st.s.getDB(dbName).adminCommand({renameCollection: nss3, to: nss2, dropTarget: true}));

assert.eq(0, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));
assert.eq(0, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collectionUUID
}));
assert.eq(0, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collectionUUID,
}));

assert.commandWorked(st.s.getDB(dbName).runCommand({drop: collection2Name}));

jsTestLog(
    "Case 3: Rename from sharded collection without indexes to sharded collection with indexes clears the indexVersion");

assert.commandWorked(st.s.adminCommand({shardCollection: nss2, key: {_id: 1}}));
assert.commandWorked(st.s.adminCommand({shardCollection: nss3, key: {_id: 1}}));

let collection3UUID = st.s.getCollection('config.collections').findOne({_id: nss3}).uuid;

registerIndex(st.rs0, nss3, index1Pattern, index1Name, collection3UUID);

assert.commandWorked(
    st.s.getDB(dbName).adminCommand({renameCollection: nss2, to: nss3, dropTarget: true}));

let nss3Metadata =
    st.configRS.getPrimary().getCollection(configsvrCollectionCatalog).findOne({_id: nss3});
assert(!nss3Metadata.indexVersion);

jsTestLog("Drop collection test. Create a sharded collection and some indexes");
assert.commandWorked(st.s.adminCommand({shardCollection: nss4, key: {_id: 1}}));
const collection4UUID = st.s.getCollection('config.collections').findOne({_id: nss4}).uuid;

registerIndex(st.rs0, nss4, index1Pattern, index1Name, collection4UUID);
registerIndex(st.rs0, nss4, index2Pattern, index2Name, collection4UUID);

assert.eq(2, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection4UUID
}));
assert.eq(2, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collection4UUID,
}));
jsTestLog("Drop collection, should eliminate indexes everywhere.");
assert.commandWorked(st.s.getDB(dbName).runCommand({drop: collection4Name}));
assert.eq(0, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection4UUID
}));
assert.eq(0, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collection4UUID,
}));

jsTestLog("Drop database test. Create a sharded collection and some indexes");
assert.commandWorked(st.s.adminCommand({shardCollection: nss5, key: {_id: 1}}));

const collection5UUID = st.s.getCollection('config.collections').findOne({_id: nss5}).uuid;

registerIndex(st.rs0, nss5, index3Pattern, index3Name, collection5UUID);
registerIndex(st.rs0, nss5, index4Pattern, index4Name, collection5UUID);

assert.eq(2, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection5UUID
}));
assert.eq(2, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collection5UUID,
}));
jsTestLog("Drop database, should eliminate indexes everywhere.");
assert.commandWorked(st.s.getDB(dbName).dropDatabase());
assert.eq(0, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection5UUID
}));
assert.eq(0, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collection5UUID,
}));

jsTestLog("Resharding Case 1: happy path.");
st.s.adminCommand({enableSharding: dbName, primaryShard: shard0});
assert.eq(0, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({}));
assert.eq(0, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({}));
assert.commandWorked(st.s.adminCommand({shardCollection: nss6, key: {_id: 1}}));

assert.commandWorked(st.s.adminCommand({split: nss6, middle: {_id: 0}}));
assert.commandWorked(st.s.adminCommand({moveChunk: nss6, find: {_id: 0}, to: shard1}));

const collection6UUID = st.s.getCollection(configsvrCollectionCatalog).findOne({_id: nss6}).uuid;
registerIndex(st.rs0, nss6, index1Pattern, index1Name, collection6UUID);
registerIndex(st.rs0, nss6, index2Pattern, index2Name, collection6UUID);

const beforeReshardingIndexVersion =
    st.s.getCollection(configsvrCollectionCatalog).findOne({_id: nss6}).indexVersion;

assert.commandWorked(st.s.getDB(dbName).runCommand(
    {createIndexes: collection6Name, indexes: [{key: {x: 1}, name: 'x_1'}]}));

jsTestLog("Give enough cardinality for two chunks to resharding.");
assert.commandWorked(st.s.getCollection(nss6).insert({x: 0}));
assert.commandWorked(st.s.getCollection(nss6).insert({x: 1}));

assert.commandWorked(st.s.adminCommand({reshardCollection: nss6, key: {x: 1}}));
const collection6AfterResharding =
    st.s.getCollection(configsvrCollectionCatalog).findOne({_id: nss6});

assert.gte(collection6AfterResharding.indexVersion, beforeReshardingIndexVersion);
assert.eq(
    st.rs0.getPrimary().getCollection(shardCollectionCatalog).findOne({_id: nss6}).indexVersion,
    collection6AfterResharding.indexVersion);
assert.eq(
    st.rs1.getPrimary().getCollection(shardCollectionCatalog).findOne({_id: nss6}).indexVersion,
    collection6AfterResharding.indexVersion);

assert.eq(0, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collection6UUID
}));
assert.eq(2, st.configRS.getPrimary().getCollection(configsvrIndexCatalog).countDocuments({
    collectionUUID: collection6AfterResharding.uuid
}));

assert.eq(0, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection6UUID
}));

assert.eq(2, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection6AfterResharding.uuid
}));

assert.eq(0, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection6UUID
}));

assert.eq(2, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection6AfterResharding.uuid
}));

jsTestLog("Case 2: donor shard different to destination shard. First move chunks to " + shard1 +
          ".");

let coll6Chunks =
    st.s.getCollection('config.chunks').find({uuid: collection6AfterResharding.uuid}).toArray();
let newChunks = [];

coll6Chunks.forEach((chunk) => {
    assert.commandWorked(st.s.adminCommand({moveChunk: nss6, find: chunk.min, to: shard1}));
    newChunks.push({min: {y: chunk.min.x}, max: {y: chunk.max.x}, recipientShardId: shard0});
});

assert.eq(2, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection6AfterResharding.uuid
}));

assert.commandWorked(st.s.getDB(dbName).runCommand(
    {createIndexes: collection6Name, indexes: [{key: {y: 1}, name: 'y_1'}]}));
assert.commandWorked(st.s.getCollection(nss6).insert({y: 0}));
assert.commandWorked(st.s.getCollection(nss6).insert({y: 1}));

assert.commandWorked(
    st.s.adminCommand({reshardCollection: nss6, key: {y: 1}, _presetReshardedChunks: newChunks}));

const collection6AfterSecondResharding =
    st.s.getCollection(configsvrCollectionCatalog).findOne({_id: nss6});

assert.eq(0, st.rs1.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection6AfterSecondResharding.uuid
}));

assert.eq(2, st.rs0.getPrimary().getCollection(shardIndexCatalog).countDocuments({
    collectionUUID: collection6AfterSecondResharding.uuid
}));

st.stop();
})();