summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/readConcern_snapshot.js
blob: b208cf4d71588fa663862389945ade4052fc2f28 (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
// Test parsing of readConcern level 'snapshot'.
// @tags: [requires_replication, uses_transactions]
(function() {
    "use strict";

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

    //
    // Configurations.
    //

    // Transactions should fail on storage engines that do not support them.
    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);
    if (!sessionDb.serverStatus().storageEngine.supportsSnapshotReadConcern ||
        !sessionDb.serverStatus().storageEngine.persistent) {
        // Transactions with readConcern snapshot fail.
        session.startTransaction({readConcern: {level: "snapshot"}});
        assert.commandFailedWithCode(sessionDb.runCommand({find: collName}),
                                     ErrorCodes.IllegalOperation);
        assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                     [ErrorCodes.NoSuchTransaction, ErrorCodes.IllegalOperation]);

        // Transactions without readConcern snapshot fail.
        session.startTransaction();
        assert.commandFailedWithCode(sessionDb.runCommand({find: collName}),
                                     ErrorCodes.IllegalOperation);
        assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                     [ErrorCodes.NoSuchTransaction, ErrorCodes.IllegalOperation]);

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

    // readConcern 'snapshot' is not allowed on a standalone.
    const conn = MongoRunner.runMongod();
    session = conn.startSession({causalConsistency: false});
    sessionDb = session.getDatabase(dbName);
    assert.neq(null, conn, "mongod was unable to start up");
    session.startTransaction({readConcern: {level: "snapshot"}});
    assert.commandFailedWithCode(sessionDb.runCommand({find: collName}),
                                 ErrorCodes.IllegalOperation);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.IllegalOperation);
    session.endSession();
    MongoRunner.stopMongod(conn);

    // readConcern 'snapshot' is allowed on a replica set primary.
    rst = new ReplSetTest({nodes: 2});
    rst.startSet();
    rst.initiate();
    assert.commandWorked(rst.getPrimary().getDB(dbName).runCommand(
        {create: collName, writeConcern: {w: "majority"}}));
    session = rst.getPrimary().getDB(dbName).getMongo().startSession({causalConsistency: false});
    sessionDb = session.getDatabase(dbName);
    session.startTransaction({writeConcern: {w: "majority"}, readConcern: {level: "snapshot"}});
    assert.commandWorked(sessionDb.coll.insert({}));
    assert.commandWorked(sessionDb.runCommand({find: collName}));
    session.commitTransaction();

    // readConcern 'snapshot' is allowed with 'afterClusterTime'.
    session.startTransaction();
    let pingRes = assert.commandWorked(rst.getPrimary().adminCommand({ping: 1}));
    assert(pingRes.hasOwnProperty("$clusterTime"), tojson(pingRes));
    assert(pingRes.$clusterTime.hasOwnProperty("clusterTime"), tojson(pingRes));
    assert.commandWorked(sessionDb.runCommand({
        find: collName,
        readConcern: {level: "snapshot", afterClusterTime: pingRes.$clusterTime.clusterTime}
    }));
    session.commitTransaction();

    // readConcern 'snapshot' is not allowed with 'afterOpTime'.
    session.startTransaction(
        {readConcern: {level: "snapshot", afterOpTime: {ts: Timestamp(1, 2), t: 1}}});
    assert.commandFailedWithCode(sessionDb.runCommand({find: collName}), ErrorCodes.InvalidOptions);
    assert.commandFailedWithCode(session.abortTransaction_forTesting(),
                                 ErrorCodes.NoSuchTransaction);
    session.endSession();

    pingRes = assert.commandWorked(rst.getSecondary().adminCommand({ping: 1}));
    assert(pingRes.hasOwnProperty("$clusterTime"), tojson(pingRes));
    assert(pingRes.$clusterTime.hasOwnProperty("clusterTime"), tojson(pingRes));

    session.startTransaction(
        {readConcern: {level: "snapshot", afterClusterTime: pingRes.$clusterTime.clusterTime}});
    assert.commandWorked(sessionDb.runCommand({find: collName}));
    session.commitTransaction();

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

    //
    // Commands.
    //

    rst = new ReplSetTest({nodes: 1});
    rst.startSet();
    rst.initiate();
    let testDB = rst.getPrimary().getDB(dbName);
    let coll = testDB.coll;
    assert.commandWorked(coll.createIndex({geo: "2d"}));
    assert.commandWorked(testDB.runCommand({
        createIndexes: collName,
        indexes: [{key: {haystack: "geoHaystack", a: 1}, name: "haystack_geo", bucketSize: 1}],
        writeConcern: {w: "majority"}
    }));

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

    // readConcern 'snapshot' is supported by find.
    session.startTransaction({readConcern: {level: "snapshot"}, writeConcern: {w: "majority"}});
    assert.commandWorked(sessionDb.runCommand({find: collName}));

    // readConcern 'snapshot' is supported by aggregate.
    assert.commandWorked(sessionDb.runCommand({aggregate: collName, pipeline: [], cursor: {}}));

    // readConcern 'snapshot' is supported by distinct.
    assert.commandWorked(sessionDb.runCommand({distinct: collName, key: "x"}));

    // readConcern 'snapshot' is supported by geoSearch.
    assert.commandWorked(
        sessionDb.runCommand({geoSearch: collName, near: [0, 0], maxDistance: 1, search: {a: 1}}));

    // readConcern 'snapshot' is not supported by non-CRUD commands.
    assert.commandFailedWithCode(
        sessionDb.runCommand({createIndexes: collName, indexes: [{key: {a: 1}, name: "a_1"}]}),
        ErrorCodes.OperationNotSupportedInTransaction);
    assert.commandWorked(session.abortTransaction_forTesting());
    session.endSession();
    rst.stopSet();
}());