summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/afterClusterTime_committed_reads.js
blob: 0f9bc1480c783f9c2e943e5246cfab14698e5a7c (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
// Test that causally consistent majority-committed read-only transactions will wait for the
// majority commit point to move past 'afterClusterTime' before they can commit.
// @tags: [uses_transactions]
(function() {
    "use strict";

    load("jstests/libs/write_concern_util.js");  // For stopReplicationOnSecondaries.

    const dbName = "test";
    const collName = "coll";

    const rst = new ReplSetTest({nodes: 2});
    rst.startSet();
    rst.initiate();

    const session =
        rst.getPrimary().getDB(dbName).getMongo().startSession({causalConsistency: false});
    const primaryDB = session.getDatabase(dbName);

    let txnNumber = 0;

    function testReadConcernLevel(level) {
        // Stop replication.
        stopReplicationOnSecondaries(rst);

        // Perform a write and get its op time.
        const res = assert.commandWorked(primaryDB.runCommand({insert: collName, documents: [{}]}));
        assert(res.hasOwnProperty("opTime"), tojson(res));
        assert(res.opTime.hasOwnProperty("ts"), tojson(res));
        const clusterTime = res.opTime.ts;

        // A majority-committed read-only transaction on the primary after the new cluster time
        // should time out at commit time waiting for the cluster time to be majority committed.
        assert.commandWorked(primaryDB.runCommand({
            find: collName,
            txnNumber: NumberLong(++txnNumber),
            startTransaction: true,
            autocommit: false,
            readConcern: {level: level, afterClusterTime: clusterTime}
        }));
        assert.commandFailedWithCode(primaryDB.adminCommand({
            commitTransaction: 1,
            txnNumber: NumberLong(txnNumber),
            autocommit: false,
            writeConcern: {w: "majority"},
            maxTimeMS: 1000
        }),
                                     ErrorCodes.ExceededTimeLimit);

        // Restart replication.
        restartReplicationOnSecondaries(rst);

        // A majority-committed read-only transaction on the primary after the new cluster time now
        // succeeds.
        assert.commandWorked(primaryDB.runCommand({
            find: collName,
            txnNumber: NumberLong(++txnNumber),
            startTransaction: true,
            autocommit: false,
            readConcern: {level: level, afterClusterTime: clusterTime}
        }));
        assert.commandWorked(primaryDB.adminCommand({
            commitTransaction: 1,
            txnNumber: NumberLong(txnNumber),
            autocommit: false,
            writeConcern: {w: "majority"}
        }));
    }

    testReadConcernLevel("majority");
    testReadConcernLevel("snapshot");

    rst.stopSet();
}());