summaryrefslogtreecommitdiff
path: root/jstests/concurrency/fsm_workloads/internal_transactions_unsharded.js
blob: e4136b121374f2928328f2d1e356c4fa1905608e (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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
'use strict';

/**
 * Runs insert, update, delete and findAndModify commands in internal transactions using all the
 * available client session settings. This workload works on both standalone replica sets and
 * sharded clusters since by default the FSM runner shards every collection used by a workload that
 * runs against a sharded cluster using the shard key {_id: hashed}. However, the workload is only
 * run on standalone replica sets since there is already a sharded workload for this
 * (internal_transactions_sharded.js) that sets up its own range-sharded collection.
 *
 * @tags: [
 *  requires_fcv_60,
 *  uses_transactions,
 *  assumes_unsharded_collection
 * ]
 */
load('jstests/concurrency/fsm_libs/extend_workload.js');
load('jstests/concurrency/fsm_workload_helpers/auto_retry_transaction.js');
load("jstests/libs/override_methods/retry_writes_at_least_once.js");

// This workload involves running commands outside a session.
TestData.disableImplicitSessions = true;

if ($config === undefined) {
    // There is no workload to extend. Define a noop base workload to make the 'extendWorkload' call
    // below still work.
    $config = {
        threadCount: 1,
        iterations: 1,
        startState: "init",
        data: {},
        states: {init: function(db, collName) {}},
        transitions: {init: {init: 1}},
        setup: function(db, collName) {},
        teardown: function(db, collName) {},
    };
}

var $config = extendWorkload($config, function($config, $super) {
    $config.threadCount = 5;
    $config.iterations = 50;

    $config.data.executionContextTypes =
        {kNoClientSession: 1, kClientSession: 2, kClientRetryableWrite: 3, kClientTransaction: 4};
    $config.data.imageTypes = {kPreImage: 1, kPostImage: 2};

    // The number of documents assigned to a thread when the workload starts.
    $config.data.partitionSize = 200;
    // The batch size for the find command used for looking up the documents assigned to a thread.
    // Use a large batch size so that a getMore command is never needed since getMore is not
    // retryable after network errors.
    $config.data.batchSizeForDocsLookUp = 1000;
    // The counter values for the documents assigned to a thread. The map is populated during
    // the init state and is updated after every write in the other states. Used to verify that
    // updates aren't double applied.
    $config.data.expectedCounters = {};
    // Keep track of the documents that a thread has started writing to but does not know if the
    // write has succeeded, for example, because the write is interrupted. The key of the inner map
    // is the document id, and the value is command name for the write against that document (i.e.
    // "insert", "update", "delete" or "findAndModify").
    $config.data.dirtyDocs = {
        [$config.data.executionContextTypes.kNoClientSession]: {},
        [$config.data.executionContextTypes.kClientSession]: {},
        [$config.data.executionContextTypes.kClientRetryableWrite]: {},
        [$config.data.executionContextTypes.kClientTransaction]: {},
    };
    $config.data.expectDirtyDocs = {
        [$config.data.executionContextTypes.kNoClientSession]: false,
        [$config.data.executionContextTypes.kClientSession]: false,
        [$config.data.executionContextTypes.kClientRetryableWrite]: false,
        [$config.data.executionContextTypes.kClientTransaction]: false,
    };

    // This workload sets the 'storeFindAndModifyImagesInSideCollection' parameter to a random bool
    // during setup() and restores the original value during teardown().
    $config.data.originalStoreFindAndModifyImagesInSideCollection = {};

    // This workload supports setting the 'transactionLifetimeLimitSeconds' to 45 seconds
    // (configurable) during setup() and restoring the original value during teardown().
    $config.data.lowerTransactionLifetimeLimitSeconds = false;
    $config.data.transactionLifetimeLimitSeconds = 45;
    $config.data.originalTransactionLifetimeLimitSeconds = {};

    // Determine if this workload needs to use causally consistent sessions.
    $config.data.shouldUseCausalConsistency = (() => {
        if (TestData.runningWithCausalConsistency !== undefined) {
            // Use the causal consistency setting on TestData.
            return TestData.runningWithCausalConsistency;
        }
        if (TestData.runningWithShardStepdowns) {
            // Use causal consistency since it is running with stepdown/kill/terminate and "read
            // your own writes" against the primary is only guaranteed outside a casually consistent
            // session when the primary is stable.
            return true;
        }
        // Don't use causal consistency in other cases since it should not be necessary.
        return false;
    })();

    /**
     * Returns a random boolean.
     */
    $config.data.generateRandomBool = function generateRandomBool() {
        return Math.random() > 0.5;
    };

    /**
     * Returns a random integer between min (inclusive) and max (inclusive).
     */
    $config.data.generateRandomInt = function generateRandomInt(min, max) {
        return Math.floor(Math.random() * (max - min + 1)) + min;
    };

    $config.data.generateRandomExecutionContext = function generateRandomExecutionContext() {
        if (this.shouldUseCausalConsistency) {
            // Exclude kNoClientSession since a (casually consistent) session is required.
            return this.generateRandomInt(2, 4);
        }
        return this.generateRandomInt(1, 4);
    };

    $config.data.generateRandomImageType = function generateRandomImageType() {
        return this.generateRandomInt(1, 2);
    };

    $config.data.startSessions = function startSessions(db) {
        this.mongo = db.getMongo();
        this.nonRetryableWriteSession = this.mongo.startSession(
            {causalConsistency: this.shouldUseCausalConsistency, retryWrites: false});
        this.retryableWriteSession = this.mongo.startSession(
            {causalConsistency: this.shouldUseCausalConsistency, retryWrites: true});
        this.sessions = [this.nonRetryableWriteSession, this.retryableWriteSession];

        print(`Started a non-retryable write session ${
            tojsononeline(this.nonRetryableWriteSession.getSessionId())}`);
        print(`Started a retryable write session ${
            tojsononeline(this.retryableWriteSession.getSessionId())}`);
    };

    $config.data.getInternalTransactionDB = function getDB(executionCtxType, dbName) {
        switch (executionCtxType) {
            case this.executionContextTypes.kNoClientSession:
                return this.mongo.getDB(dbName);
            case this.executionContextTypes.kClientSession:
                return this.nonRetryableWriteSession.getDatabase(dbName);
            case this.executionContextTypes.kClientRetryableWrite:
                return this.retryableWriteSession.getDatabase(dbName);
            case this.executionContextTypes.kClientTransaction:
                return this.generateRandomBool() ? this.nonRetryableWriteSession.getDatabase(dbName)
                                                 : this.retryableWriteSession.getDatabase(dbName);
            default:
                throw Error("Unknown execution context");
        }
    };

    $config.data.getCollectionForDocumentChecks = function getCollectionForDocumentChecks(
        defaultDb, txnDb, collName) {
        assert.eq(defaultDb.getMongo().host, txnDb.getMongo().host);
        return txnDb.getCollection(collName);
    };

    $config.data.getMaxClusterTime = function getMaxClusterTime(sessions) {
        let maxClusterTime = new Timestamp(1, 0);
        for (let session of sessions) {
            if (session.getClusterTime() === undefined) {
                continue;
            }
            const clusterTime = session.getClusterTime().clusterTime;
            if (clusterTime > maxClusterTime) {
                maxClusterTime = clusterTime;
            }
        }
        return maxClusterTime;
    };

    const insertOpFieldName = "insertOp";
    const updateOpFieldName = "updateOp";
    const findAndModifyOpFieldName = "findAndModifyOp";

    $config.data.isDirtyDocument = function isDirtyDocument(doc) {
        for (const executionCtxType in this.dirtyDocs) {
            if (doc._id in this.dirtyDocs[executionCtxType]) {
                return true;
            }
        }
        return false;
    };

    $config.data.getQueryForDocument = function getQueryForDocument(doc) {
        return {_id: doc._id, tid: this.tid};
    };

    /**
     * Returns true if 'res' contains an acceptable error for the aggregate command used to look up
     * a random document.
     */
    $config.data.isAcceptableAggregateCmdError = function isAcceptableAggregateCmdError(res) {
        // The aggregate command is expected to involve running getMore commands which are not
        // retryable after network errors.
        return TestData.runningWithShardStepdowns && res &&
            (res.code == ErrorCodes.QueryPlanKilled);
    };

    $config.data.getRandomDocument = function getRandomDocument(db, collName) {
        const aggregateCmdObj = {
            aggregate: collName,
            cursor: {},
            pipeline: [{$match: {tid: this.tid}}, {$sample: {size: 1}}],
        };
        // Use linearizable read concern to guarantee any subsequent transaction snapshot will
        // include the found document. Skip if the test has a default read concern or requires
        // casual consistency because in both cases the default read concern should provide this
        // guarantee already.
        if (!TestData.defaultReadConcernLevel &&
            !db.getSession().getOptions().isCausalConsistency()) {
            aggregateCmdObj.readConcern = {level: "linearizable"};
        }

        let numTries = 0;
        const numDocs = Object.keys(this.expectedCounters).length;
        while (numTries < numDocs) {
            print("Finding a random document " +
                  tojsononeline({aggregateCmdObj, numTries, numDocs}));
            let aggRes;
            assert.soon(() => {
                try {
                    aggRes = db.runCommand(aggregateCmdObj);
                    assert.commandWorked(aggRes);
                    return true;
                } catch (e) {
                    if (this.isAcceptableAggregateCmdError(aggRes)) {
                        return false;
                    }
                    throw e;
                }
            });
            const doc = aggRes.cursor.firstBatch[0];
            print("Found a random document " +
                  tojsononeline({doc, isDirty: this.isDirtyDocument(doc)}));
            if (!this.isDirtyDocument(doc)) {
                return doc;
            }
            numTries++;
        }
        throw Error("Could not find a clean document");
    };

    $config.data.generateRandomDocument = function generateRandomDocument(tid) {
        return {_id: UUID(), tid: tid, counter: 0};
    };

    $config.data.generateRandomInsert = function generateRandomInsert(db, collName) {
        const docToInsert = this.generateRandomDocument(this.tid);
        docToInsert[insertOpFieldName] = 0;

        const cmdObj = {insert: collName, documents: [docToInsert]};
        const checkResponseFunc = (res) => {
            assert.eq(res.n, 1, res);
        };
        const checkDocsFunc = (collection) => {
            assert.eq(collection.findOne({_id: docToInsert._id}), docToInsert);
            this.expectedCounters[docToInsert._id] = docToInsert.counter;
        };
        const docId = docToInsert._id;

        return {cmdObj, checkResponseFunc, checkDocsFunc, docId};
    };

    $config.data.generateRandomUpdate = function generateRandomUpdate(db, collName) {
        const docToUpdate = this.getRandomDocument(db, collName);
        assert.neq(docToUpdate, null);

        const updatedDoc = Object.assign({}, docToUpdate);
        updatedDoc[updateOpFieldName] = docToUpdate.hasOwnProperty(updateOpFieldName)
            ? (docToUpdate[updateOpFieldName] + 1)
            : 0;
        updatedDoc.counter += 1;

        const update = {
            $set: {[updateOpFieldName]: updatedDoc[updateOpFieldName]},
            $inc: {counter: 1}
        };

        const cmdObj = {
            update: collName,
            updates: [{q: this.getQueryForDocument(docToUpdate), u: update}]
        };
        const checkResponseFunc = (res) => {
            assert.eq(res.n, 1, res);
            assert.eq(res.nModified, 1, res);
        };
        const checkDocsFunc = (collection) => {
            assert.isnull(collection.findOne(docToUpdate));
            assert.eq(collection.findOne(this.getQueryForDocument(docToUpdate)), updatedDoc);
            this.expectedCounters[docToUpdate._id] = updatedDoc.counter;
        };
        const docId = docToUpdate._id;

        return {cmdObj, checkResponseFunc, checkDocsFunc, docId};
    };

    $config.data.generateRandomDelete = function generateRandomDelete(db, collName) {
        const docToDelete = this.getRandomDocument(db, collName);
        assert.neq(docToDelete, null);

        const cmdObj = {
            delete: collName,
            deletes: [{q: this.getQueryForDocument(docToDelete), limit: 1}]
        };
        const checkResponseFunc = (res) => {
            assert.eq(res.n, 1, res);
        };
        const checkDocsFunc = (collection) => {
            assert.isnull(collection.findOne(docToDelete));
            delete this.expectedCounters[docToDelete._id];
        };
        const docId = docToDelete._id;

        return {cmdObj, checkResponseFunc, checkDocsFunc, docId};
    };

    $config.data.generateRandomFindAndModify = function generateRandomFindAndModify(db, collName) {
        const isUpsert = this.generateRandomBool();
        const imageType = this.generateRandomImageType();

        const docToUpdate =
            isUpsert ? this.generateRandomDocument(this.tid) : this.getRandomDocument(db, collName);

        const updatedDoc = Object.assign({}, docToUpdate);
        updatedDoc[findAndModifyOpFieldName] = docToUpdate.hasOwnProperty(findAndModifyOpFieldName)
            ? (docToUpdate[findAndModifyOpFieldName] + 1)
            : 0;
        updatedDoc.counter += 1;

        const update = {
            $set: {[findAndModifyOpFieldName]: updatedDoc[findAndModifyOpFieldName]},
            $inc: {counter: 1}
        };

        const cmdObj = {
            findAndModify: collName,
            query: this.getQueryForDocument(docToUpdate),
            update: update
        };
        cmdObj.upsert = isUpsert;
        if (imageType == this.imageTypes.kPostImage) {
            cmdObj.new = true;
        }
        const checkResponseFunc = (res) => {
            assert.eq(res.lastErrorObject.n, 1, res);
            if (isUpsert) {
                assert.eq(res.lastErrorObject.updatedExisting, false, res);
                assert.eq(res.lastErrorObject.upserted, updatedDoc._id, res);
            } else {
                assert.eq(res.lastErrorObject.updatedExisting, true, res);
                assert.eq(res.value,
                          imageType == this.imageTypes.kPreImage ? docToUpdate : updatedDoc,
                          res);
            }
        };
        const checkDocsFunc = (collection) => {
            assert.isnull(collection.findOne(docToUpdate));
            assert.neq(collection.findOne(updatedDoc), null);
            this.expectedCounters[docToUpdate._id] = updatedDoc.counter;
        };
        const docId = docToUpdate._id;

        return {cmdObj, checkResponseFunc, checkDocsFunc, docId};
    };

    /**
     * Returns true if 'res' contains an acceptable retry error for a retryable write command.
     */
    $config.data.isAcceptableRetryError = function isAcceptableRetryError(res) {
        // This workload does not involve data placement changes so retries should always succeed.
        // Workloads that extend this workload should override this method accordingly.
        return false;
    };

    /**
     * Runs the command specified by 'crudOp.cmdObj' inside an internal transaction using the
     * specified client 'executionCtxType'.
     */
    $config.data.runInternalTransaction = function runInternalTransaction(
        defaultDb, collName, executionCtxType, crudOp) {
        // The testInternalTransactions command below runs with the session setting defined by
        // 'executionCtxType'.
        fsm.forceRunningOutsideTransaction(this);

        // Add an insert command to each transaction so that when this workload is running on a
        // sharded cluster there can be a mix of single-shard and cross-shard transactions.
        const insertOp = this.generateRandomInsert(defaultDb, collName);

        if (executionCtxType == this.executionContextTypes.kClientRetryableWrite) {
            crudOp.cmdObj.stmtId = NumberInt(1);
            insertOp.cmdObj.stmtId = NumberInt(-1);
        }
        const internalTxnTestCmdObj = {
            testInternalTransactions: 1,
            commandInfos: [
                {dbName: defaultDb.getName(), command: crudOp.cmdObj},
                {dbName: defaultDb.getName(), command: insertOp.cmdObj}
            ],
        };
        if (this.useClusterClient) {
            internalTxnTestCmdObj.useClusterClient = true;
        }

        print(`Running an internal transaction using a test command ${
            tojsononeline(internalTxnTestCmdObj)}: ${tojsononeline({executionCtxType})}`);
        const txnDb = this.getInternalTransactionDB(executionCtxType, defaultDb.getName());

        const runFunc = () => {
            let res;
            try {
                res = txnDb.adminCommand(internalTxnTestCmdObj);
                print(`Response: ${tojsononeline(res)}`);
                assert.commandWorked(res);
            } catch (e) {
                if ((executionCtxType == this.executionContextTypes.kClientRetryableWrite) &&
                    this.isAcceptableRetryError(res, executionCtxType)) {
                    print("Ignoring retry error for retryable write: " + tojsononeline(res));
                    return;
                }
                throw e;
            }

            // Check responses.
            res.responses.forEach(innerRes => {
                assert.commandWorked(innerRes);
            });
            if (executionCtxType == this.executionContextTypes.kClientRetryableWrite) {
                // If the command was retried, 'responses' would only contain the response for
                // 'crudOp.cmdObj'.
                assert.lte(res.responses.length, 2);
            } else {
                assert.eq(res.responses.length, 2);
            }
            const crudRes = res.responses[0];
            crudOp.checkResponseFunc(crudRes);
            if (res.responses.length == 2) {
                const insertRes = res.responses[1];
                insertOp.checkResponseFunc(insertRes);
            }
        };

        print("Starting internal transaction");
        this.dirtyDocs[executionCtxType][crudOp.docId] = Object.keys(crudOp.cmdObj)[0];
        this.dirtyDocs[executionCtxType][insertOp.docId] = "insert";

        if (executionCtxType == this.executionContextTypes.kClientTransaction) {
            withTxnAndAutoRetry(
                txnDb.getSession(), runFunc, {retryOnKilledSession: this.retryOnKilledSession});
        } else {
            runFunc();
        }

        // Check documents.
        const collection = this.getCollectionForDocumentChecks(defaultDb, txnDb, collName);
        crudOp.checkDocsFunc(collection);
        insertOp.checkDocsFunc(collection);

        delete this.dirtyDocs[executionCtxType][crudOp.docId];
        delete this.dirtyDocs[executionCtxType][insertOp.docId];
        print("Finished internal transaction");
    };

    $config.data.insertInitialDocuments = function insertInitialDocuments(db, collName, tid) {
        let bulk = db.getCollection(collName).initializeUnorderedBulkOp();
        for (let i = 0; i < this.partitionSize; ++i) {
            const doc = this.generateRandomDocument(tid);
            bulk.insert(doc);
        }
        assert.commandWorked(bulk.execute());
    };

    $config.data.overrideStoreFindAndModifyImagesInSideCollection =
        function overrideStoreFindAndModifyImagesInSideCollection(cluster) {
        // Store the findAndModify images in the oplog half of the time.
        const enableFindAndModifyImageCollection = this.generateRandomBool();
        cluster.executeOnMongodNodes((db) => {
            const res = assert.commandWorked(db.adminCommand({
                setParameter: 1,
                storeFindAndModifyImagesInSideCollection: enableFindAndModifyImageCollection
            }));
            this.originalStoreFindAndModifyImagesInSideCollection[db.getMongo().host] = res.was;
        });
    };

    $config.data.restoreStoreFindAndModifyImagesInSideCollection =
        function restoreStoreFindAndModifyImagesInSideCollection(cluster) {
        cluster.executeOnMongodNodes((db) => {
            assert.commandWorked(db.adminCommand({
                setParameter: 1,
                storeFindAndModifyImagesInSideCollection:
                    this.originalStoreFindAndModifyImagesInSideCollection[db.getMongo().host]
            }));
        });
    };

    $config.data.overrideTransactionLifetimeLimit = function overrideTransactionLifetimeLimit(
        cluster) {
        cluster.executeOnMongodNodes((db) => {
            const res = assert.commandWorked(db.adminCommand({
                setParameter: 1,
                transactionLifetimeLimitSeconds: this.transactionLifetimeLimitSeconds
            }));
            this.originalTransactionLifetimeLimitSeconds[db.getMongo().host] = res.was;
        });
    };

    $config.data.restoreTransactionLifetimeLimit = function restoreTransactionLifetimeLimit(
        cluster) {
        cluster.executeOnMongodNodes((db) => {
            assert.commandWorked(db.adminCommand({
                setParameter: 1,
                transactionLifetimeLimitSeconds:
                    this.originalTransactionLifetimeLimitSeconds[db.getMongo().host]
            }));
        });
    };

    $config.setup = function setup(db, collName, cluster) {
        assert.commandWorked(db.createCollection(collName, {writeConcern: {w: "majority"}}));
        if (this.insertInitialDocsOnSetUp) {
            // There isn't a way to determine what the thread ids are in setup phase so just assume
            // that they are [0, 1, ..., this.threadCount-1].
            for (let tid = 0; tid < this.threadCount; ++tid) {
                this.insertInitialDocuments(db, collName, tid);
            }
        }
        this.overrideStoreFindAndModifyImagesInSideCollection(cluster);
        if (this.lowerTransactionLifetimeLimitSeconds) {
            this.overrideTransactionLifetimeLimit(cluster);
        }
    };

    $config.teardown = function teardown(db, collName, cluster) {
        this.restoreStoreFindAndModifyImagesInSideCollection(cluster);
        if (this.lowerTransactionLifetimeLimitSeconds) {
            this.restoreTransactionLifetimeLimit(cluster);
        }
    };

    /**
     * Starts a retryable-write session and non-retryable write session, inserts the documents for
     * this thread, and populates the 'expectedCounters' map.
     */
    $config.states.init = function init(db, collName) {
        $super.states.init.apply(this, arguments);

        if (!this.insertInitialDocsOnSetUp) {
            this.insertInitialDocuments(db, collName, this.tid);
        }

        const docs = assert
                         .commandWorked(db.runCommand({
                             find: collName,
                             filter: {tid: this.tid},
                             batchSize: this.batchSizeForDocsLookUp,
                         }))
                         .cursor.firstBatch;
        assert.eq(docs.length, this.partitionSize);
        docs.forEach(doc => {
            this.expectedCounters[doc._id] = doc.counter;
        });

        this.startSessions(db);
    };

    $config.states.internalTransactionForInsert = function internalTransactionForInsert(db,
                                                                                        collName) {
        print("Starting internalTransactionForInsert");
        const executionCtxType = this.generateRandomExecutionContext();
        const insertOp = this.generateRandomInsert(db, collName);
        this.runInternalTransaction(db, collName, executionCtxType, insertOp);
        print("Finished internalTransactionForInsert");
    };

    $config.states.internalTransactionForUpdate = function internalTransactionForUpdate(db,
                                                                                        collName) {
        print("Starting internalTransactionForUpdate");
        const executionCtxType = this.generateRandomExecutionContext();
        const updateOp = this.generateRandomUpdate(db, collName);
        this.runInternalTransaction(db, collName, executionCtxType, updateOp);
        print("Finished internalTransactionForUpdate");
    };

    $config.states.internalTransactionForDelete = function internalTransactionForDelete(db,
                                                                                        collName) {
        print("Starting internalTransactionForDelete");
        const executionCtxType = this.generateRandomExecutionContext();
        const deleteOp = this.generateRandomDelete(db, collName);
        this.runInternalTransaction(db, collName, executionCtxType, deleteOp);
        print("Finished internalTransactionForDelete");
    };

    $config.states.internalTransactionForFindAndModify =
        function internalTransactionForFindAndModify(db, collName) {
        print("Starting internalTransactionForFindAndModify");
        const executionCtxType = this.generateRandomExecutionContext();
        const findAndModifyOp = this.generateRandomFindAndModify(db, collName);
        this.runInternalTransaction(db, collName, executionCtxType, findAndModifyOp);
        print("Finished internalTransactionForFindAndModify");
    };

    /**
     * Asserts that the counter values for all documents assigned to this thread match their
     * expected values.
     */
    $config.states.verifyDocuments = function verifyDocuments(db, collName) {
        print("Starting verifyDocuments");

        for (const executionCtxType in this.expectDirtyDocs) {
            const numDirtyDocs = Object.keys(this.dirtyDocs[executionCtxType]).length;
            print(`Dirty documents: ${tojsononeline({
                executionCtxType,
                count: numDirtyDocs,
                docs: this.dirtyDocs[executionCtxType]
            })}`);
            if (!this.expectDirtyDocs[executionCtxType]) {
                assert.eq(0,
                          numDirtyDocs,
                          () => `expected to find no dirty documents for ${
                              tojsononeline({executionCtxType})} but found ${numDirtyDocs}`);
            }
        }

        // The read below should not be done inside a transaction (and use readConcern level
        // "snapshot").
        fsm.forceRunningOutsideTransaction(this);

        const numDocsExpected = Object.keys(this.expectedCounters).length;
        const findCmdObj = {
            find: collName,
            filter: {tid: this.tid},
            batchSize: this.batchSizeForDocsLookUp,
        };
        if (this.shouldUseCausalConsistency) {
            findCmdObj.readConcern = {afterClusterTime: this.getMaxClusterTime(this.sessions)};
            if (TestData.runningWithShardStepdowns) {
                findCmdObj.readConcern.level = "majority";
            }
        }
        const docs = assert.commandWorked(db.runCommand(findCmdObj)).cursor.firstBatch;
        print("verifyDocuments " +
              tojsononeline(
                  {findCmdObj, numDocsFound: docs.length, numDocsExpected: numDocsExpected}));

        docs.forEach(doc => {
            if (this.isDirtyDocument(doc)) {
                return;
            }
            assert(doc._id in this.expectedCounters, tojson(doc));
            const expectedCounter = this.expectedCounters[doc._id];
            assert.eq(expectedCounter, doc.counter, () => {
                return 'unexpected counter value, doc: ' + tojson(doc);
            });
        });

        print("Finished verifyDocuments");
    };

    if ($config.passConnectionCache) {
        // If 'passConnectionCache' is true, every state function must accept 3 parameters: db,
        // collName and connCache. This workload does not set 'passConnectionCache' since it doesn't
        // use 'connCache' but it may extend a sharding workload that uses it.
        const originalInit = $config.states.init;
        $config.states.init = function(db, collName, connCache) {
            originalInit.call(this, db, collName);
        };

        const originalInternalTransactionForInsert = $config.states.internalTransactionForInsert;
        $config.states.internalTransactionForInsert = function(db, collName, connCache) {
            originalInternalTransactionForInsert.call(this, db, collName);
        };

        const originalInternalTransactionForUpdate = $config.states.internalTransactionForUpdate;
        $config.states.internalTransactionForUpdate = function(db, collName, connCache) {
            originalInternalTransactionForUpdate.call(this, db, collName);
        };

        const originalInternalTransactionForDelete = $config.states.internalTransactionForDelete;
        $config.states.internalTransactionForDelete = function(db, collName, connCache) {
            originalInternalTransactionForDelete.call(this, db, collName);
        };

        const originalInternalTransactionForFindAndModify =
            $config.states.internalTransactionForFindAndModify;
        $config.states.internalTransactionForFindAndModify = function(db, collName, connCache) {
            originalInternalTransactionForFindAndModify.call(this, db, collName);
        };

        const originalVerifyDocuments = $config.states.verifyDocuments;
        $config.states.verifyDocuments = function(db, collName, connCache) {
            originalVerifyDocuments.call(this, db, collName);
        };
    }

    $config.transitions = {
        init: {
            internalTransactionForInsert: 0.25,
            internalTransactionForUpdate: 0.25,
            internalTransactionForDelete: 0.25,
            internalTransactionForFindAndModify: 0.25,
        },
        internalTransactionForInsert: {
            internalTransactionForInsert: 0.2,
            internalTransactionForUpdate: 0.2,
            internalTransactionForDelete: 0.2,
            internalTransactionForFindAndModify: 0.2,
            verifyDocuments: 0.2
        },
        internalTransactionForUpdate: {
            internalTransactionForInsert: 0.2,
            internalTransactionForUpdate: 0.2,
            internalTransactionForDelete: 0.2,
            internalTransactionForFindAndModify: 0.2,
            verifyDocuments: 0.2
        },
        internalTransactionForDelete: {
            internalTransactionForInsert: 0.2,
            internalTransactionForUpdate: 0.2,
            internalTransactionForDelete: 0.2,
            internalTransactionForFindAndModify: 0.2,
            verifyDocuments: 0.2
        },
        internalTransactionForFindAndModify: {
            internalTransactionForInsert: 0.2,
            internalTransactionForUpdate: 0.2,
            internalTransactionForDelete: 0.2,
            internalTransactionForFindAndModify: 0.2,
            verifyDocuments: 0.2
        },
        verifyDocuments: {
            internalTransactionForInsert: 0.25,
            internalTransactionForUpdate: 0.25,
            internalTransactionForDelete: 0.25,
            internalTransactionForFindAndModify: 0.25,
        }
    };

    return $config;
});