summaryrefslogtreecommitdiff
path: root/jstests/sharding/configsvr_metadata_commands_require_majority_write_concern.js
blob: f2f7dd5e53db9b7aab760bbb87718e5fe11b0a14 (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
/**
 * Checks that issuing a metadata command 1) through mongos or 2) directly against the config server
 * with a non-majority writeConcern fails.
 */
(function() {
    'use strict';

    const dbName = "test";
    const collName = "foo";
    const ns = dbName + "." + collName;
    const newShardName = "newShard";

    const unacceptableWriteConcerns = [
        {writeConcern: {w: 0}},
        {writeConcern: {w: 1}},
        {writeConcern: {w: 2}},
        {writeConcern: {w: 3}},
        // TODO: should metadata commands allow j: false? can CSRS have an in-memory storage engine?
        // writeConcern{w: "majority", j: "false"}},
    ];

    const acceptableWriteConcerns = [
        {},
        {writeConcern: {w: "majority"}},
        {writeConcern: {w: "majority", j: true}},
        {writeConcern: {w: "majority", j: true, wtimeout: 15000}},
    ];

    const setupFuncs = {
        noop: function() {},
        createDatabase: function() {
            // A database is implicitly created when a collection within it is created.
            assert.commandWorked(st.s.getDB(dbName).runCommand({create: collName}));
        },
        enableSharding: function() {
            assert.commandWorked(st.s.adminCommand({enableSharding: dbName}));
        },
        addShard: function() {
            assert.commandWorked(st.s.adminCommand({addShard: newShard.name, name: newShardName}));
        },
    };

    const cleanupFuncs = {
        noop: function() {},
        dropDatabase: function() {
            assert.commandWorked(st.s.getDB(dbName).runCommand({dropDatabase: 1}));
        },
        removeShardIfExists: function() {
            var res = st.s.adminCommand({removeShard: newShardName});
            if (!res.ok && res.code == ErrorCodes.ShardNotFound) {
                return;
            }
            assert.commandWorked(res);
            assert.eq('started', res.state);
            res = st.s.adminCommand({removeShard: newShardName});
            assert.commandWorked(res);
            assert.eq('completed', res.state);
        },
    };

    function checkCommand(conn, command, setupFunc, cleanupFunc) {
        unacceptableWriteConcerns.forEach(function(writeConcern) {
            jsTest.log("testing " + tojson(command) + " with writeConcern " + tojson(writeConcern) +
                       " against " + conn + ", expecting the command to fail");
            setupFunc();
            let commandWithWriteConcern = {};
            Object.assign(commandWithWriteConcern, command, writeConcern);
            assert.commandFailedWithCode(conn.adminCommand(commandWithWriteConcern),
                                         ErrorCodes.InvalidOptions);
            cleanupFunc();
        });

        acceptableWriteConcerns.forEach(function(writeConcern) {
            jsTest.log("testing " + tojson(command) + " with writeConcern " + tojson(writeConcern) +
                       " against " + conn + ", expecting the command to succeed");
            setupFunc();
            let commandWithWriteConcern = {};
            Object.assign(commandWithWriteConcern, command, writeConcern);
            assert.commandWorked(conn.adminCommand(commandWithWriteConcern));
            cleanupFunc();
        });
    }

    var st = new ShardingTest({shards: 1});

    // enableSharding

    checkCommand(st.s, {enableSharding: dbName}, setupFuncs.noop, cleanupFuncs.dropDatabase);

    checkCommand(st.configRS.getPrimary(),
                 {_configsvrEnableSharding: dbName},
                 setupFuncs.noop,
                 cleanupFuncs.dropDatabase);

    // movePrimary

    checkCommand(st.s,
                 {movePrimary: dbName, to: st.shard0.name},
                 setupFuncs.createDatabase,
                 cleanupFuncs.dropDatabase);

    checkCommand(st.configRS.getPrimary(),
                 {_configsvrMovePrimary: dbName, to: st.shard0.name},
                 setupFuncs.createDatabase,
                 cleanupFuncs.dropDatabase);

    // shardCollection

    checkCommand(st.s,
                 {shardCollection: ns, key: {_id: 1}},
                 setupFuncs.enableSharding,
                 cleanupFuncs.dropDatabase);

    checkCommand(st.configRS.getPrimary(),
                 {_configsvrShardCollection: ns, key: {_id: 1}},
                 setupFuncs.enableSharding,
                 cleanupFuncs.dropDatabase);

    // createDatabase

    // Don't check createDatabase against mongos: there is no createDatabase command exposed on
    // mongos; a database is created implicitly when a collection in it is created.

    checkCommand(st.configRS.getPrimary(),
                 {_configsvrCreateDatabase: dbName, to: st.shard0.name},
                 setupFuncs.noop,
                 cleanupFuncs.dropDatabase);

    // addShard

    var newShard = MongoRunner.runMongod({shardsvr: ""});

    checkCommand(st.s,
                 {addShard: newShard.name, name: newShardName},
                 setupFuncs.noop,
                 cleanupFuncs.removeShardIfExists);

    checkCommand(st.configRS.getPrimary(),
                 {_configsvrAddShard: newShard.name, name: newShardName},
                 setupFuncs.noop,
                 cleanupFuncs.removeShardIfExists);

    // removeShard

    checkCommand(st.s, {removeShard: newShardName}, setupFuncs.addShard, cleanupFuncs.noop);

    checkCommand(st.configRS.getPrimary(),
                 {_configsvrRemoveShard: newShardName},
                 setupFuncs.addShard,
                 cleanupFuncs.noop);

    st.stop();
})();