summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/readConcern_atClusterTime.js
blob: 222d7b31ec8b9759c23268cf5ba9688d921b31c8 (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
// Test parsing of readConcern option 'atClusterTime'.
//
// Only run this test with the WiredTiger storage engine, since we expect other storage engines to
// return early because they do not support snapshot read concern.
// @tags: [requires_wiredtiger, uses_transactions, uses_atclustertime]

function _getClusterTime(rst) {
    const pingRes = assert.commandWorked(rst.getPrimary().adminCommand({ping: 1}));
    assert(pingRes.hasOwnProperty("$clusterTime"), tojson(pingRes));
    assert(pingRes.$clusterTime.hasOwnProperty("clusterTime"), tojson(pingRes));
    return pingRes.$clusterTime.clusterTime;
}

(function() {
    "use strict";

    // Skip this test if running with --nojournal and WiredTiger.
    if (jsTest.options().noJournal &&
        (!jsTest.options().storageEngine || jsTest.options().storageEngine === "wiredTiger")) {
        print("Skipping test because running WiredTiger without journaling isn't a valid" +
              " replica set configuration");
        return;
    }

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

    const rst = new ReplSetTest({nodes: 1});
    rst.startSet();
    rst.initiate();
    const testDB = rst.getPrimary().getDB(dbName);

    if (!testDB.serverStatus().storageEngine.supportsSnapshotReadConcern) {
        rst.stopSet();
        return;
    }

    const session = testDB.getMongo().startSession({causalConsistency: false});
    const sessionDb = session.getDatabase(dbName);

    const clusterTime = _getClusterTime(rst);

    // 'atClusterTime' can be used with readConcern level 'snapshot'.
    session.startTransaction({readConcern: {level: "snapshot", atClusterTime: clusterTime}});
    assert.commandWorked(sessionDb.runCommand({find: collName}));
    assert.commandWorked(session.commitTransaction_forTesting());

    // 'atClusterTime' cannot be greater than the current cluster time.
    const futureClusterTime = new Timestamp(clusterTime.getTime() + 1000, 1);
    session.startTransaction({readConcern: {level: "snapshot", atClusterTime: futureClusterTime}});
    assert.commandFailedWithCode(sessionDb.runCommand({find: collName}), ErrorCodes.InvalidOptions);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    // 'atClusterTime' must have type Timestamp.
    session.startTransaction({readConcern: {level: "snapshot", atClusterTime: "bad"}});
    assert.commandFailedWithCode(sessionDb.runCommand({find: collName}), ErrorCodes.TypeMismatch);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    // 'atClusterTime' cannot be used with readConcern level 'majority'.
    session.startTransaction({readConcern: {level: "majority", atClusterTime: clusterTime}});
    assert.commandFailedWithCode(sessionDb.runCommand({find: collName}), ErrorCodes.InvalidOptions);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    // 'atClusterTime' cannot be used with readConcern level 'local'.
    session.startTransaction({readConcern: {level: "local", atClusterTime: clusterTime}});
    assert.commandFailedWithCode(sessionDb.runCommand({find: collName}), ErrorCodes.InvalidOptions);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    // 'atClusterTime' cannot be used with readConcern level 'available'.
    session.startTransaction({readConcern: {level: "available", atClusterTime: clusterTime}});
    assert.commandFailedWithCode(sessionDb.runCommand({find: collName}), ErrorCodes.InvalidOptions);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    // 'atClusterTime' cannot be used with readConcern level 'linearizable'.
    session.startTransaction({readConcern: {level: "linearizable", atClusterTime: clusterTime}});
    assert.commandFailedWithCode(sessionDb.runCommand({find: collName}), ErrorCodes.InvalidOptions);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    // 'atClusterTime' cannot be used without readConcern level (level is 'local' by default).
    session.startTransaction({readConcern: {atClusterTime: clusterTime}});
    assert.commandFailedWithCode(sessionDb.runCommand({find: collName}), ErrorCodes.InvalidOptions);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    // 'atClusterTime' cannot be used with 'afterOpTime'.
    session.startTransaction({
        readConcern: {
            level: "snapshot",
            atClusterTime: clusterTime,
            afterOpTime: {ts: Timestamp(1, 2), t: 1}
        }
    });
    assert.commandFailedWithCode(sessionDb.runCommand({find: collName}), ErrorCodes.InvalidOptions);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

    // 'atClusterTime' cannot be used outside of a session.
    assert.commandFailedWithCode(
        testDB.runCommand(
            {find: collName, readConcern: {level: "snapshot", atClusterTime: clusterTime}}),
        ErrorCodes.InvalidOptions);

    // 'atClusterTime' cannot be used with 'afterClusterTime'.
    session.startTransaction({
        readConcern:
            {level: "snapshot", atClusterTime: clusterTime, afterClusterTime: clusterTime}
    });
    assert.commandFailedWithCode(sessionDb.runCommand({find: collName}), ErrorCodes.InvalidOptions);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);

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

    // readConcern with 'atClusterTime' should succeed regardless of value of 'enableTestCommands'.
    {
        jsTest.setOption('enableTestCommands', false);
        let rst = new ReplSetTest({nodes: 1});
        rst.startSet();
        rst.initiate();
        let session =
            rst.getPrimary().getDB(dbName).getMongo().startSession({causalConsistency: false});
        let sessionDb = session.getDatabase(dbName);
        session.startTransaction(
            {readConcern: {level: "snapshot", atClusterTime: _getClusterTime(rst)}});
        assert.commandWorked(sessionDb.runCommand({find: collName}));
        assert.commandWorked(session.commitTransaction_forTesting());
        session.endSession();
        rst.stopSet();

        jsTest.setOption('enableTestCommands', true);
        rst = new ReplSetTest({nodes: 1});
        rst.startSet();
        rst.initiate();
        session =
            rst.getPrimary().getDB(dbName).getMongo().startSession({causalConsistency: false});
        sessionDb = session.getDatabase(dbName);
        session.startTransaction(
            {readConcern: {level: "snapshot", atClusterTime: _getClusterTime(rst)}});
        assert.commandWorked(sessionDb.runCommand({find: collName}));
        assert.commandWorked(session.commitTransaction_forTesting());
        session.endSession();
        rst.stopSet();
    }

    // readConcern with 'atClusterTime' is not allowed when enableMajorityReadConcern=false.
    {
        let rst = new ReplSetTest({nodes: [{"enableMajorityReadConcern": "false"}]});
        rst.startSet();
        rst.initiate();
        let session =
            rst.getPrimary().getDB(dbName).getMongo().startSession({causalConsistency: false});
        let sessionDb = session.getDatabase(dbName);
        session.startTransaction(
            {readConcern: {level: "snapshot", atClusterTime: _getClusterTime(rst)}});
        assert.commandFailedWithCode(sessionDb.runCommand({find: collName}),
                                     ErrorCodes.InvalidOptions);
        assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                     ErrorCodes.NoSuchTransaction);
        session.endSession();
        rst.stopSet();
    }

}());