summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/shardsvr_global_index_crud_bulk.js
blob: 2d5976d777b0f6fe7d323642d27295af932b941f (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
644
645
646
647
648
649
/**
 * Tests _shardsvrWriteGlobalIndexKeys, which is sometimes referred to as 'bulk write' as it runs
 * multiple _shardsvrInsertGlobalIndexKey and _shardsvrDeleteGlobalIndexKey statements in bulk.
 *
 * @tags: [
 *     featureFlagGlobalIndexes,
 *     requires_fcv_62,
 *     requires_replication,
 * ]
 */

(function() {
"use strict";

function uuidToString(uuid) {
    const [_, uuidString] = uuid.toString().match(/"((?:\\.|[^"\\])*)"/);
    return uuidString;
}

function entriesInContainer(primary, uuid) {
    return primary.getDB("system")
        .getCollection("globalIndexes." + uuidToString(uuid))
        .find()
        .itcount();
}

// Speed up test time.
const maxNumberOfTxnOpsInSingleOplogEntry = 1000;

const rst = new ReplSetTest({
    nodes: 2,
    nodeOptions: {
        setParameter: {
            maxNumberOfTransactionOperationsInSingleOplogEntry: maxNumberOfTxnOpsInSingleOplogEntry
        }
    }
});
rst.startSet();
rst.initiate();
const primary = rst.getPrimary();
const adminDB = primary.getDB("admin");
const globalIndexUUID = UUID();
const session = primary.startSession();

assert.commandWorked(adminDB.runCommand({_shardsvrCreateGlobalIndex: globalIndexUUID}));
assert.eq(0, entriesInContainer(primary, globalIndexUUID));

// _shardsvrWriteGlobalIndexKeys must run inside a transaction.
assert.commandFailedWithCode(adminDB.runCommand({_shardsvrWriteGlobalIndexKeys: 1, ops: []}),
                             6789500);

// Missing required '_shardsvrWriteGlobalIndexKeys.ops' field.
{
    session.startTransaction();
    assert.commandFailedWithCode(
        session.getDatabase("test").runCommand({_shardsvrWriteGlobalIndexKeys: 1}), 40414);
    session.abortTransaction();
}

// _shardsvrWriteGlobalIndexKeys requires at least one statement in the 'ops' array.
{
    const containerEntriesBefore = entriesInContainer(primary, globalIndexUUID);

    session.startTransaction();
    assert.commandFailedWithCode(
        session.getDatabase("test").runCommand({_shardsvrWriteGlobalIndexKeys: 1, ops: []}),
        6789502);
    session.abortTransaction();
    const containerEntriesAfter = entriesInContainer(primary, globalIndexUUID);

    assert.eq(containerEntriesAfter, containerEntriesBefore);
}

// Only _shardsvrInsertGlobalIndexKey and _shardsvrDeleteGlobalIndexKey commands are allowed.
{
    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand(
                                     {_shardsvrWriteGlobalIndexKeys: 1, ops: [{otherCommand: 1}]}),
                                 6789501);
    session.abortTransaction();
}

// Invalid UUID in _shardsvrWriteGlobalIndexKeys statement.
{
    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [{
            "_shardsvrInsertGlobalIndexKey": "not_a_uuid",
            key: {a: "hola"},
            docKey: {sk: 3, _id: 3}
        }]
    }),
                                 ErrorCodes.InvalidUUID);
    session.abortTransaction();
}

// Unexisting UUID in statement.
{
    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [{"_shardsvrInsertGlobalIndexKey": UUID(), key: {a: "hola"}, docKey: {sk: 3, _id: 3}}]
    }),
                                 6789402);
    session.abortTransaction();
}

// Missing 'key' field in statement.
{
    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [{"_shardsvrInsertGlobalIndexKey": globalIndexUUID, docKey: {sk: 3, _id: 3}}]
    }),
                                 40414);
    session.abortTransaction();
}

// Missing 'docKey' field in statement.
{
    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [{"_shardsvrInsertGlobalIndexKey": globalIndexUUID, key: {a: "hola"}}]
    }),
                                 40414);
    session.abortTransaction();
}

// Bulk insert a single entry, then bulk delete it.
{
    const containerEntriesBefore = entriesInContainer(primary, globalIndexUUID);

    session.startTransaction();
    assert.commandWorked(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [{
            _shardsvrInsertGlobalIndexKey: globalIndexUUID,
            key: {myKey: "abc"},
            docKey: {s: 123, _id: 987}
        }]
    }));
    session.commitTransaction();

    const containerEntriesAfterInsert = entriesInContainer(primary, globalIndexUUID);
    assert.eq(containerEntriesAfterInsert, containerEntriesBefore + 1);

    session.startTransaction();
    assert.commandWorked(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [{
            _shardsvrDeleteGlobalIndexKey: globalIndexUUID,
            key: {myKey: "abc"},
            docKey: {s: 123, _id: 987}
        }]
    }));
    session.commitTransaction();

    const containerEntriesAfterDelete = entriesInContainer(primary, globalIndexUUID);
    assert.eq(containerEntriesAfterDelete, containerEntriesBefore);
}

// Update an index key.
{
    const containerEntriesBefore = entriesInContainer(primary, globalIndexUUID);

    session.startTransaction();
    assert.commandWorked(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [{
            _shardsvrInsertGlobalIndexKey: globalIndexUUID,
            key: {myKey: "the quick brown fox"},
            docKey: {myShardKey0: "jumped over", myShardKey1: "the lazy dog", _id: 1234567890}
        }]
    }));
    session.commitTransaction();

    const containerEntriesAfterInsert = entriesInContainer(primary, globalIndexUUID);
    assert.eq(containerEntriesAfterInsert, containerEntriesBefore + 1);

    // We can't reinsert that same key, as that'll trigger a duplicate key error.
    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [{
            _shardsvrInsertGlobalIndexKey: globalIndexUUID,
            key: {myKey: "the quick brown fox"},
            docKey: {myShardKey0: "another", myShardKey1: "value", _id: 0}
        }]
    }),
                                 ErrorCodes.DuplicateKey);
    session.abortTransaction();

    // Update the key.
    session.startTransaction();
    assert.commandWorked(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [
            {
                _shardsvrDeleteGlobalIndexKey: globalIndexUUID,
                key: {myKey: "the quick brown fox"},
                docKey: {myShardKey0: "jumped over", myShardKey1: "the lazy dog", _id: 1234567890}
            },
            {
                _shardsvrInsertGlobalIndexKey: globalIndexUUID,
                key: {myKey: "The Quick Brown Fox"},
                docKey: {myShardKey0: "jumped over", myShardKey1: "the lazy dog", _id: 1234567890}
            }
        ]
    }));
    session.commitTransaction();

    const containerEntriesAfterUpdate = entriesInContainer(primary, globalIndexUUID);
    assert.eq(containerEntriesAfterInsert, containerEntriesAfterUpdate);

    // Verify that the index key update succeeded: we can re-insert the old key, we get a duplicate
    // key error if reinsterting the updated key.
    session.startTransaction();
    assert.commandWorked(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [
            {
                _shardsvrInsertGlobalIndexKey: globalIndexUUID,
                key: {myKey: "the quick brown fox"},
                docKey: {myShardKey0: "another", myShardKey1: "shardKeyValue", _id: 0}
            },
        ]
    }));
    session.commitTransaction();
    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [{
            _shardsvrInsertGlobalIndexKey: globalIndexUUID,
            key: {myKey: "The Quick Brown Fox"},
            docKey: {myShardKey0: "jumped over", myShardKey1: "the lazy dog", _id: 1234567890}
        }]
    }),
                                 ErrorCodes.DuplicateKey);
    session.abortTransaction();
}

// A duplicate key error rolls back the whole bulk write.
{
    const containerEntriesBefore = entriesInContainer(primary, globalIndexUUID);

    session.startTransaction();
    assert.commandWorked(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [{
            _shardsvrInsertGlobalIndexKey: globalIndexUUID,
            key: {myKey: "testDupes"},
            docKey: {myShardKey0: "test", _id: "dupes"}
        }]
    }));
    session.commitTransaction();

    const containerEntriesAfterInsert = entriesInContainer(primary, globalIndexUUID);
    assert.eq(containerEntriesAfterInsert, containerEntriesBefore + 1);

    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [
            {
                _shardsvrInsertGlobalIndexKey: globalIndexUUID,
                key: {myKey: "new entry"},
                docKey: {myShardKey0: "new", _id: "entry"}
            },
            {
                _shardsvrInsertGlobalIndexKey: globalIndexUUID,
                key: {myKey: "testDupes"},
                docKey: {myShardKey0: "test", _id: "dupes"}
            }
        ]
    }),
                                 ErrorCodes.DuplicateKey);
    session.abortTransaction();

    const containerEntriesAfterDupKeyError = entriesInContainer(primary, globalIndexUUID);
    assert.eq(containerEntriesAfterInsert, containerEntriesAfterDupKeyError);
}

// A KeyNotFound error rolls back the whole bulk write.
{
    const containerEntriesBefore = entriesInContainer(primary, globalIndexUUID);

    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [
            {
                _shardsvrInsertGlobalIndexKey: globalIndexUUID,
                key: {myKey: "key that will be rolled back"},
                docKey: {myShardKey0: "key", _id: "roll back"}
            },
            {
                _shardsvrDeleteGlobalIndexKey: globalIndexUUID,
                key: {myKey: "key that doesn't exist"},
                docKey: {myShardKey0: "does not", _id: "exist"}
            }
        ]
    }),
                                 ErrorCodes.KeyNotFound);
    session.abortTransaction();

    const containerEntriesAfter = entriesInContainer(primary, globalIndexUUID);
    assert.eq(containerEntriesBefore, containerEntriesAfter);
}

// A KeyNotFound error on the docKey rolls back the whole bulk write.
{
    const containerEntriesBefore = entriesInContainer(primary, globalIndexUUID);

    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [
            {
                _shardsvrInsertGlobalIndexKey: globalIndexUUID,
                key: {myKey: "key that will be rolled back"},
                docKey: {myShardKey0: "key", _id: "roll back"}
            },
            {
                _shardsvrDeleteGlobalIndexKey: globalIndexUUID,
                key: {myKey: "key that will be rolled back"},
                docKey: {myShardKey0: "docKey", _id: "doesn't match"}
            }
        ]
    }),
                                 ErrorCodes.KeyNotFound);
    session.abortTransaction();

    const containerEntriesAfter = entriesInContainer(primary, globalIndexUUID);
    assert.eq(containerEntriesBefore, containerEntriesAfter);
}

// Insert 2k index entries in bulk.
{
    const containerEntriesBefore = entriesInContainer(primary, globalIndexUUID);
    let ops = [];
    for (let i = 0; i < 2000; i++) {
        ops.push(
            {_shardsvrInsertGlobalIndexKey: globalIndexUUID, key: {a: i}, docKey: {sk: i, _id: i}});
    }

    session.startTransaction();
    assert.commandWorked(
        session.getDatabase("test").runCommand({_shardsvrWriteGlobalIndexKeys: 1, ops: ops}));
    session.commitTransaction();

    const containerEntriesAfter = entriesInContainer(primary, globalIndexUUID);
    assert.eq(containerEntriesAfter, containerEntriesBefore + 2000);
}

// Delete the 2k index entries above in bulk.
{
    const containerEntriesBefore = entriesInContainer(primary, globalIndexUUID);

    let ops = [];
    for (let i = 0; i < 2000; i++) {
        ops.push(
            {_shardsvrDeleteGlobalIndexKey: globalIndexUUID, key: {a: i}, docKey: {sk: i, _id: i}});
    }

    session.startTransaction();
    assert.commandWorked(
        session.getDatabase("test").runCommand({_shardsvrWriteGlobalIndexKeys: 1, ops: ops}));
    session.commitTransaction();

    const containerEntriesAfter = entriesInContainer(primary, globalIndexUUID);
    assert.eq(containerEntriesAfter, containerEntriesBefore - 2000);
}

// Insert and delete the same 2k entries in the same bulk.
{
    const containerEntriesBefore = entriesInContainer(primary, globalIndexUUID);
    let ops = [];
    for (let i = 0; i < 2000; i++) {
        ops.push(
            {_shardsvrInsertGlobalIndexKey: globalIndexUUID, key: {a: i}, docKey: {sk: i, _id: i}});
        ops.push(
            {_shardsvrDeleteGlobalIndexKey: globalIndexUUID, key: {a: i}, docKey: {sk: i, _id: i}});
    }

    session.startTransaction();
    assert.commandWorked(
        session.getDatabase("test").runCommand({_shardsvrWriteGlobalIndexKeys: 1, ops: ops}));
    session.commitTransaction();

    const containerEntriesAfter = entriesInContainer(primary, globalIndexUUID);
    assert.eq(containerEntriesAfter, containerEntriesBefore);
}

// Insert and delete keys on multiple global index containers.
{
    const otherGlobalIndexUUID = UUID();
    assert.commandWorked(adminDB.runCommand({_shardsvrCreateGlobalIndex: otherGlobalIndexUUID}));
    assert.eq(0, entriesInContainer(primary, otherGlobalIndexUUID));

    const containerEntriesBefore = entriesInContainer(primary, globalIndexUUID);
    const otherContainerEntriesBefore = entriesInContainer(primary, otherGlobalIndexUUID);

    session.startTransaction();
    assert.commandWorked(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [
            {
                _shardsvrInsertGlobalIndexKey: globalIndexUUID,
                key: {k0: "first"},
                docKey: {sk0: "first", _id: "first"}
            },
            {
                _shardsvrInsertGlobalIndexKey: otherGlobalIndexUUID,
                key: {k0: "firstOnSecondContainer"},
                docKey: {sk0: "firstOnSecondContainer", _id: "firstOnSecondContainer"}
            },
            {
                _shardsvrInsertGlobalIndexKey: otherGlobalIndexUUID,
                key: {k0: "secondOnSecondContainer"},
                docKey: {sk0: "secondOnSecondContainer", _id: "secondOnSecondContainer"}
            },
            {
                _shardsvrDeleteGlobalIndexKey: globalIndexUUID,
                key: {k0: "first"},
                docKey: {sk0: "first", _id: "first"}
            },
            {
                _shardsvrInsertGlobalIndexKey: globalIndexUUID,
                key: {k0: "firstUpdated"},
                docKey: {sk0: "first", _id: "first"}
            },
            {
                _shardsvrDeleteGlobalIndexKey: otherGlobalIndexUUID,
                key: {k0: "firstOnSecondContainer"},
                docKey: {sk0: "firstOnSecondContainer", _id: "firstOnSecondContainer"}
            },
        ]
    }));
    session.commitTransaction();

    const containerEntriesAfter = entriesInContainer(primary, globalIndexUUID);
    const otherContainerEntriesAfter = entriesInContainer(primary, otherGlobalIndexUUID);
    assert.eq(containerEntriesAfter, containerEntriesBefore + 1);
    assert.eq(otherContainerEntriesAfter, otherContainerEntriesBefore + 1);

    // Verify that that the expected index keys are in place by fetching by document key, and by
    // triggering DuplicateKey error on inserting the same index key.
    assert.eq(1,
              primary.getDB("system")
                  .getCollection("globalIndexes." + uuidToString(globalIndexUUID))
                  .find({_id: {sk0: "first", _id: "first"}})
                  .itcount());
    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand({
        _shardsvrInsertGlobalIndexKey: globalIndexUUID,
        key: {k0: "firstUpdated"},
        docKey: {sk0: "doesn't", _id: "matter"}
    }),
                                 ErrorCodes.DuplicateKey);
    session.abortTransaction();
    assert.eq(1,
              primary.getDB("system")
                  .getCollection("globalIndexes." + uuidToString(otherGlobalIndexUUID))
                  .find({_id: {sk0: "secondOnSecondContainer", _id: "secondOnSecondContainer"}})
                  .itcount());
    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand({
        _shardsvrInsertGlobalIndexKey: otherGlobalIndexUUID,
        key: {k0: "secondOnSecondContainer"},
        docKey: {sk0: "doesn't", _id: "matter"}
    }),
                                 ErrorCodes.DuplicateKey);
    session.abortTransaction();
}

// Insert and delete keys in bulk while inserting a document to a collection in the same multi-doc
// transaction.
{
    const containerEntriesBefore = entriesInContainer(primary, globalIndexUUID);

    session.startTransaction();
    assert.commandWorked(session.getDatabase("test").runCommand({
        _shardsvrWriteGlobalIndexKeys: 1,
        ops: [
            {
                _shardsvrInsertGlobalIndexKey: globalIndexUUID,
                key: {k0: "globalIndexKey"},
                docKey: {sk0: "globalIndexKey", _id: "globalIndexKey"}
            },
            {
                _shardsvrInsertGlobalIndexKey: globalIndexUUID,
                key: {k0: "globalIndexKey2"},
                docKey: {sk0: "globalIndexKey2", _id: "globalIndexKey"}
            },
        ]
    }));
    assert.commandWorked(session.getDatabase("test").getCollection("c").insertOne({x: "data"}));
    session.commitTransaction();

    const containerEntriesAfter = entriesInContainer(primary, globalIndexUUID);
    assert.eq(containerEntriesAfter, containerEntriesBefore + 2);

    // Verify that that the expected index keys are in place by fetching by document key, and by
    // triggering DuplicateKey error on inserting the same index key.
    assert.eq(1,
              primary.getDB("system")
                  .getCollection("globalIndexes." + uuidToString(globalIndexUUID))
                  .find({_id: {sk0: "globalIndexKey", _id: "globalIndexKey"}})
                  .itcount());
    assert.eq(1,
              primary.getDB("system")
                  .getCollection("globalIndexes." + uuidToString(globalIndexUUID))
                  .find({_id: {sk0: "globalIndexKey2", _id: "globalIndexKey"}})
                  .itcount());
    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand({
        _shardsvrInsertGlobalIndexKey: globalIndexUUID,
        key: {k0: "globalIndexKey"},
        docKey: {sk0: "doesn't", _id: "matter"}
    }),
                                 ErrorCodes.DuplicateKey);
    session.abortTransaction();
    session.startTransaction();
    assert.commandFailedWithCode(session.getDatabase("test").runCommand({
        _shardsvrInsertGlobalIndexKey: globalIndexUUID,
        key: {k0: "globalIndexKey2"},
        docKey: {sk0: "doesn't", _id: "matter"}
    }),
                                 ErrorCodes.DuplicateKey);
    session.abortTransaction();
    assert.eq(1, primary.getDB("test").getCollection("c").find({x: "data"}).itcount());
}

// A bulk write broken down into multiple applyOps.
{
    const uuid = UUID();
    // Generate three applyOps entries, the last one only containing a single statement.
    const stmts = 2 * maxNumberOfTxnOpsInSingleOplogEntry + 1;
    assert.commandWorked(adminDB.runCommand({_shardsvrCreateGlobalIndex: uuid}));
    assert.eq(0, entriesInContainer(primary, uuid));

    let ops = [];
    for (let i = 0; i < stmts; i++) {
        ops.push({_shardsvrInsertGlobalIndexKey: uuid, key: {a: i}, docKey: {sk: i, _id: i}});
    }
    session.startTransaction();
    assert.commandWorked(
        session.getDatabase("test").runCommand({_shardsvrWriteGlobalIndexKeys: 1, ops: ops}));
    session.commitTransaction();

    // The global index container consists of 'stmts' entries.
    assert.eq(stmts, entriesInContainer(primary, uuid));

    // The transaction is split into three applyOps entries, with 'stmts' entries in total.
    const applyOpsSummary =
        adminDB.getSiblingDB('local')
            .oplog.rs
            .aggregate([
                {$match: {ns: "admin.$cmd", 'o.applyOps.op': 'xi', 'o.applyOps.ui': uuid}},
                {$project: {count: {$size: '$o.applyOps'}}},
            ])
            .toArray();
    assert.eq(3, applyOpsSummary.length);
    assert.eq(maxNumberOfTxnOpsInSingleOplogEntry, applyOpsSummary[0]["count"]);
    assert.eq(maxNumberOfTxnOpsInSingleOplogEntry, applyOpsSummary[1]["count"]);
    assert.eq(1, applyOpsSummary[2]["count"]);
    assert.eq(
        stmts,
        applyOpsSummary[0]["count"] + applyOpsSummary[1]["count"] + applyOpsSummary[2]["count"]);
}

// Global index CRUD ops generate the same oplog entry, regardless of whether the transaction
// statements are bulked together. Also validates the oplog entry.
{
    {
        const containerEntriesBefore = entriesInContainer(primary, globalIndexUUID);
        session.startTransaction();
        assert.commandWorked(session.getDatabase("test").runCommand({
            _shardsvrWriteGlobalIndexKeys: 1,
            ops: [
                {
                    _shardsvrInsertGlobalIndexKey: globalIndexUUID,
                    key: {myKey: "insertAndRemove"},
                    docKey: {shardKey: "insert", _id: "andRemove"}
                },
                {
                    _shardsvrDeleteGlobalIndexKey: globalIndexUUID,
                    key: {myKey: "insertAndRemove"},
                    docKey: {shardKey: "insert", _id: "andRemove"}
                }
            ]
        }));
        session.commitTransaction();
        const containerEntriesAfter = entriesInContainer(primary, globalIndexUUID);
        assert.eq(containerEntriesAfter, containerEntriesBefore);
    }
    {
        const containerEntriesBefore = entriesInContainer(primary, globalIndexUUID);
        session.startTransaction();
        assert.commandWorked(session.getDatabase("test").runCommand({
            _shardsvrInsertGlobalIndexKey: globalIndexUUID,
            key: {myKey: "insertAndRemove"},
            docKey: {shardKey: "insert", _id: "andRemove"}
        }));
        assert.commandWorked(session.getDatabase("test").runCommand({
            _shardsvrDeleteGlobalIndexKey: globalIndexUUID,
            key: {myKey: "insertAndRemove"},
            docKey: {shardKey: "insert", _id: "andRemove"}
        }));
        session.commitTransaction();
        const containerEntriesAfter = entriesInContainer(primary, globalIndexUUID);
        assert.eq(containerEntriesAfter, containerEntriesBefore);
    }

    const oplogEntries =
        adminDB.getSiblingDB('local')
            .oplog.rs
            .find({ns: "admin.$cmd", 'o.applyOps.op': 'xi', 'o.applyOps.ui': globalIndexUUID})
            .sort({$natural: -1})
            .limit(2)
            .toArray();

    let oplogEntryBulk = oplogEntries[0];
    let oplogEntryPlain = oplogEntries[1];
    assert.neq(oplogEntryBulk["txnNumber"], oplogEntryPlain["txnNumber"]);
    assert.neq(oplogEntryBulk["ts"], oplogEntryPlain["ts"]);
    assert.neq(oplogEntryBulk["wall"], oplogEntryPlain["wall"]);
    delete oplogEntryBulk.ts;
    delete oplogEntryPlain.ts;
    delete oplogEntryBulk.txnNumber;
    delete oplogEntryPlain.txnNumber;
    delete oplogEntryBulk.wall;
    delete oplogEntryPlain.wall;
    assert.docEq(oplogEntryBulk, oplogEntryPlain);
    assert.eq(oplogEntryBulk["o"]["applyOps"][0]["op"], "xi");
    assert.docEq(oplogEntryBulk["o"]["applyOps"][0]["o"]["key"], {myKey: "insertAndRemove"});
    assert.docEq(oplogEntryBulk["o"]["applyOps"][0]["o"]["docKey"],
                 {shardKey: "insert", _id: "andRemove"});
    assert.eq(oplogEntryBulk["o"]["applyOps"][1]["op"], "xd");
    assert.docEq(oplogEntryBulk["o"]["applyOps"][1]["o"]["key"], {myKey: "insertAndRemove"});
    assert.docEq(oplogEntryBulk["o"]["applyOps"][1]["o"]["docKey"],
                 {shardKey: "insert", _id: "andRemove"});
}

session.endSession();
rst.stopSet();
})();