summaryrefslogtreecommitdiff
path: root/jstests/replsets/speculative_majority_supported_commands.js
blob: 26fd40aa244526ed8aca19a5ccd1c6f0bebe19fe (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
/**
 * Verify that speculative majority is only allowed on supported commands.
 *
 * Currently, only change stream aggregation commands and the 'find' command with the
 * 'allowSpeculativeMajorityRead' flag are permitted.
 *
 * @tags: [uses_speculative_majority]
 */
(function() {
    "use strict";

    let name = "speculative_majority_supported_commands";
    let replTest =
        new ReplSetTest({name: name, nodes: 1, nodeOptions: {enableMajorityReadConcern: 'false'}});
    replTest.startSet();
    replTest.initiate();

    let dbName = name;
    let collName = "coll";

    let primary = replTest.getPrimary();
    let primaryDB = primary.getDB(dbName);

    // Create a collection.
    assert.commandWorked(primaryDB[collName].insert({_id: 0}, {writeConcern: {w: "majority"}}));

    /**
     * Allowed commands.
     */

    // Change stream aggregation is allowed.
    let res = primaryDB.runCommand({
        aggregate: collName,
        pipeline: [{$changeStream: {}}],
        cursor: {},
        readConcern: {level: "majority"}
    });
    assert.commandWorked(res);

    // Find query with speculative flag is allowed.
    res = primaryDB.runCommand(
        {find: collName, readConcern: {level: "majority"}, allowSpeculativeMajorityRead: true});
    assert.commandWorked(res);

    /**
     * Disallowed commands.
     */

    // A non change stream aggregation is not allowed.
    res = primaryDB.runCommand({
        aggregate: collName,
        pipeline: [{$project: {}}],
        cursor: {},
        readConcern: {level: "majority"}
    });
    assert.commandFailedWithCode(res, ErrorCodes.ReadConcernMajorityNotEnabled);

    // The 'find' command without requisite flag is unsupported.
    res = primaryDB.runCommand({find: collName, readConcern: {level: "majority"}});
    assert.commandFailedWithCode(res, ErrorCodes.ReadConcernMajorityNotEnabled);

    res = primaryDB.runCommand(
        {find: collName, readConcern: {level: "majority"}, allowSpeculativeMajorityRead: false});
    assert.commandFailedWithCode(res, ErrorCodes.ReadConcernMajorityNotEnabled);

    // Another basic read command. We don't exhaustively check all commands.
    res = primaryDB.runCommand({count: collName, readConcern: {level: "majority"}});
    assert.commandFailedWithCode(res, ErrorCodes.ReadConcernMajorityNotEnabled);

    // Speculative flag is only allowed on find commands.
    res = primaryDB.runCommand(
        {count: collName, readConcern: {level: "majority"}, allowSpeculativeMajorityRead: true});
    assert.commandFailedWithCode(res, ErrorCodes.ReadConcernMajorityNotEnabled);

    replTest.stopSet();
})();