summaryrefslogtreecommitdiff
path: root/jstests/sharding/after_cluster_time.js
blob: 690c1f8b50b851d067fa3b651183ecb4f27d3689 (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
/**
 * Tests readConcern: afterClusterTime behavior in a sharded cluster.
 * @tags: [requires_majority_read_concern]
 */
(function() {
"use strict";

function assertAfterClusterTimeReadFailsWithCode(db, readConcernObj, errorCode) {
    return assert.commandFailedWithCode(
        db.runCommand({find: "foo", readConcern: readConcernObj}),
        errorCode,
        "expected command with read concern options: " + tojson(readConcernObj) + " to fail");
}

function assertAfterClusterTimeReadSucceeds(db, readConcernObj) {
    return assert.commandWorked(
        db.runCommand({find: "foo", readConcern: readConcernObj}),
        "expected command with read concern options: " + tojson(readConcernObj) + " to succeed");
}

const rst = new ReplSetTest({
    nodes: 1,
    nodeOptions: {
        enableMajorityReadConcern: "",
        shardsvr: "",
    }
});

rst.startSet();
rst.initiate();

// Start the sharding test and add the majority read concern enabled replica set.
const st = new ShardingTest({manualAddShard: true});
assert.commandWorked(st.s.adminCommand({addShard: rst.getURL()}));

const testDB = st.s.getDB("test");

// Insert some data to find later.
assert.commandWorked(
    testDB.runCommand({insert: "foo", documents: [{_id: 1, x: 1}], writeConcern: {w: "majority"}}));

// Test the afterClusterTime API without causal consistency enabled on the mongo connection.

assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "linearizable", afterClusterTime: Timestamp(1, 1)}, ErrorCodes.InvalidOptions);

// Reads with afterClusterTime require a non-zero timestamp.
assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "local", afterClusterTime: {}}, ErrorCodes.TypeMismatch);

assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "local", afterClusterTime: 10}, ErrorCodes.TypeMismatch);

assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "local", afterClusterTime: Timestamp()}, ErrorCodes.InvalidOptions);

assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "local", afterClusterTime: Timestamp(0, 0)}, ErrorCodes.InvalidOptions);

// Reads with proper afterClusterTime arguments return committed data after the given time.
// Reads with afterClusterTime require a non-zero timestamp.
assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "majority", afterClusterTime: {}}, ErrorCodes.TypeMismatch);

assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "majority", afterClusterTime: 10}, ErrorCodes.TypeMismatch);

assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "majority", afterClusterTime: Timestamp()}, ErrorCodes.InvalidOptions);

assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "majority", afterClusterTime: Timestamp(0, 0)}, ErrorCodes.InvalidOptions);

// Reads with proper afterClusterTime arguments return committed data after the given time.
let testReadOwnWrite = function(readConcern) {
    let res = assert.commandWorked(testDB.runCommand(
        {find: "foo", readConcern: {level: readConcern, afterClusterTime: Timestamp(1, 1)}}));

    assert.eq(res.cursor.firstBatch,
              [{_id: 1, x: 1}],
              "expected afterClusterTime read to return the committed document");

    // Test the afterClusterTime API with causal consistency enabled on the mongo connection.
    testDB.getMongo().setCausalConsistency(true);

    // With causal consistency enabled, the shell sets read concern to level "majority" if it is
    // not specified.
    assertAfterClusterTimeReadSucceeds(testDB, {afterClusterTime: Timestamp(1, 1)});
    testDB.getMongo().setCausalConsistency(false);
};

testReadOwnWrite("local");
testReadOwnWrite("majority");

// Read concern levels other than majority are still not accepted.
assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "linearizable", afterClusterTime: Timestamp(1, 1)}, ErrorCodes.InvalidOptions);

// Reads with afterClusterTime still require a non-zero timestamp.
assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "majority", afterClusterTime: {}}, ErrorCodes.TypeMismatch);

assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "majority", afterClusterTime: 10}, ErrorCodes.TypeMismatch);

assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "majority", afterClusterTime: Timestamp()}, ErrorCodes.InvalidOptions);

assertAfterClusterTimeReadFailsWithCode(
    testDB, {level: "majority", afterClusterTime: Timestamp(0, 0)}, ErrorCodes.InvalidOptions);

rst.stopSet();
st.stop();
})();