summaryrefslogtreecommitdiff
path: root/jstests/sharding/commands_that_write_accept_wc_configRS.js
blob: c8dd99fb06dfce2f9f6e6add7f4c045147ebe1b2 (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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
load('jstests/libs/write_concern_util.js');
load('jstests/multiVersion/libs/auth_helpers.js');

/**
 * This file tests that commands that do writes accept a write concern in a sharded cluster. This
 * test defines various database commands and what they expect to be true before and after the fact.
 * It then runs the commands with various invalid writeConcerns and valid writeConcerns and
 * ensures that they succeed and fail appropriately. For the valid writeConcerns, the test stops
 * replication between nodes to make sure the write concern is actually being waited for. This only
 * tests commands that get sent to config servers and must have w: majority specified. If these
 * commands fail, they should return an actual error, not just a writeConcernError.
 */

(function() {
    "use strict";
    var st = new ShardingTest({
        shards: {
            rs0: {nodes: 3, settings: {chainingAllowed: false}},
            rs1: {nodes: 3, settings: {chainingAllowed: false}}
        },
        configReplSetTestOptions: {settings: {chainingAllowed: false}},
        mongos: 1,
        other: {mongosOptions: {noAutoSplit: ""}}
    });

    var mongos = st.s;
    var dbName = "wc-test-configRS";
    var db = mongos.getDB(dbName);
    var adminDB = mongos.getDB('admin');
    // A database connection on a local shard, rather than through the mongos.
    var localDB = st.shard0.getDB('localWCTest');
    var collName = 'leaves';
    var coll = db[collName];
    var counter = 0;

    function dropTestData() {
        db.dropUser('username');
        db.dropUser('user1');
        localDB.dropUser('user2');
        assert(!db.auth("username", "password"), "auth should have failed");
        getNewDB();
    }

    // We get new databases because we do not want to reuse dropped databases that may be in a
    // bad state. This test calls dropDatabase when config server secondary nodes are down, so the
    // command fails after only the database metadata is dropped from the config servers, but the
    // data on the shards still remains. This makes future operations, such as moveChunk, fail.
    function getNewDB() {
        db = mongos.getDB(dbName + counter);
        counter++;
        coll = db[collName];
    }

    var commands = [];

    commands.push({
        req: {authSchemaUpgrade: 1},
        setupFunc: function() {
            shardCollectionWithChunks(st, coll);
            adminDB.system.version.update(
                {_id: "authSchema"}, {"currentVersion": 3}, {upsert: true});
            localDB.getSiblingDB('admin')
                .system.version.update({_id: "authSchema"}, {"currentVersion": 3}, {upsert: true});

            db.createUser({user: 'user1', pwd: 'pass', roles: jsTest.basicUserRoles});
            assert(db.auth({mechanism: 'MONGODB-CR', user: 'user1', pwd: 'pass'}));
            db.logout();

            localDB.createUser({user: 'user2', pwd: 'pass', roles: jsTest.basicUserRoles});
            assert(localDB.auth({mechanism: 'MONGODB-CR', user: 'user2', pwd: 'pass'}));
            localDB.logout();
        },
        confirmFunc: function() {
            // All users should only have SCRAM credentials.
            verifyUserDoc(db, 'user1', false, true);
            verifyUserDoc(localDB, 'user2', false, true);

            // After authSchemaUpgrade MONGODB-CR no longer works.
            verifyAuth(db, 'user1', 'pass', false, true);
            verifyAuth(localDB, 'user2', 'pass', false, true);
        },
        requiresMajority: true,
        runsOnShards: true,
        failsOnShards: false,
        admin: true
    });

    // Drop an unsharded database.
    commands.push({
        req: {dropDatabase: 1},
        setupFunc: function() {
            coll.insert({type: 'oak'});
            db.pine_needles.insert({type: 'pine'});
        },
        confirmFunc: function() {
            assert.eq(coll.find().itcount(), 0);
            assert.eq(db.pine_needles.find().itcount(), 0);
        },
        requiresMajority: false,
        runsOnShards: true,
        failsOnShards: true,
        admin: false
    });

    // Drop a sharded database.
    commands.push({
        req: {dropDatabase: 1},
        setupFunc: function() {
            shardCollectionWithChunks(st, coll);
            coll.insert({type: 'oak', x: 11});
            db.pine_needles.insert({type: 'pine'});
        },
        confirmFunc: function() {
            assert.eq(coll.find().itcount(), 0);
            assert.eq(db.pine_needles.find().itcount(), 0);
        },
        requiresMajority: false,
        runsOnShards: true,
        failsOnShards: true,
        admin: false
    });

    commands.push({
        req: {createUser: 'username', pwd: 'password', roles: jsTest.basicUserRoles},
        setupFunc: function() {},
        confirmFunc: function() {
            assert(db.auth("username", "password"), "auth failed");
        },
        requiresMajority: true,
        runsOnShards: false,
        failsOnShards: false,
        admin: false
    });

    commands.push({
        req: {updateUser: 'username', pwd: 'password2', roles: jsTest.basicUserRoles},
        setupFunc: function() {
            db.runCommand({createUser: 'username', pwd: 'password', roles: jsTest.basicUserRoles});
        },
        confirmFunc: function() {
            assert(!db.auth("username", "password"), "auth should have failed");
            assert(db.auth("username", "password2"), "auth failed");
        },
        requiresMajority: true,
        runsOnShards: false,
        admin: false
    });

    commands.push({
        req: {dropUser: 'tempUser'},
        setupFunc: function() {
            db.runCommand({createUser: 'tempUser', pwd: 'password', roles: jsTest.basicUserRoles});
            assert(db.auth("tempUser", "password"), "auth failed");
        },
        confirmFunc: function() {
            assert(!db.auth("tempUser", "password"), "auth should have failed");
        },
        requiresMajority: true,
        runsOnShards: false,
        failsOnShards: false,
        admin: false
    });

    // Sharded dropCollection should return a normal error.
    commands.push({
        req: {drop: collName},
        setupFunc: function() {
            shardCollectionWithChunks(st, coll);
        },
        confirmFunc: function() {
            assert.eq(coll.count(), 0);
        },
        requiresMajority: false,
        runsOnShards: true,
        failsOnShards: true,
        admin: false
    });

    // Config server commands require w: majority writeConcerns.
    var invalidWriteConcerns = [{w: 'invalid'}, {w: 2}];

    function testInvalidWriteConcern(wc, cmd) {
        if (wc.w === 2 && !cmd.requiresMajority) {
            return;
        }
        cmd.req.writeConcern = wc;
        jsTest.log("Testing " + tojson(cmd.req));

        dropTestData();
        cmd.setupFunc();
        var res = runCommandCheckAdmin(db, cmd);
        assert.commandFailed(res);
        assert(!res.writeConcernError,
               'bad writeConcern on config server had writeConcernError. ' +
                   tojson(res.writeConcernError));
    }

    function runCommandFailOnShardsPassOnConfigs(cmd) {
        var req = cmd.req;
        var res;
        // This command is run on the shards in addition to the config servers.
        if (cmd.runsOnShards) {
            if (cmd.failsOnShards) {
                // This command fails when there is a writeConcernError on the shards.
                req.writeConcern.wtimeout = 3000;
                res = runCommandCheckAdmin(db, cmd);
                restartReplicationOnAllShards(st);
                assert.commandFailed(res);
                assert(
                    !res.writeConcernError,
                    'command on config servers with a paused replicaset had writeConcernError: ' +
                        tojson(res));
            } else {
                // This command passes and returns a writeConcernError when there is a
                // writeConcernError on the shards.
                req.writeConcern.wtimeout = 3000;
                res = runCommandCheckAdmin(db, cmd);
                restartReplicationOnAllShards(st);
                assert.commandWorked(res);
                cmd.confirmFunc();
                assertWriteConcernError(res);
            }
        } else {
            // This command is only run on the config servers and so should pass when shards are
            // not replicating.
            res = runCommandCheckAdmin(db, cmd);
            restartReplicationOnAllShards(st);
            assert.commandWorked(res);
            cmd.confirmFunc();
            assert(!res.writeConcernError,
                   'command on config servers with a paused replicaset had writeConcernError: ' +
                       tojson(res));
        }
    }

    function testMajorityWriteConcern(cmd) {
        var req = cmd.req;
        var setupFunc = cmd.setupFunc;
        var confirmFunc = cmd.confirmFunc;

        req.writeConcern = {
            w: 'majority',
            wtimeout: 25000
        };
        jsTest.log("Testing " + tojson(req));

        dropTestData();
        setupFunc();

        // Command with a full cluster should succeed.
        var res = runCommandCheckAdmin(db, cmd);
        assert.commandWorked(res);
        assert(!res.writeConcernError,
               'command on a full cluster had writeConcernError: ' + tojson(res));
        confirmFunc();

        dropTestData();
        setupFunc();
        // Stop replication at all shard secondaries.
        stopReplicationOnSecondariesOfAllShards(st);

        // Command is running on full config server replica set but a majority of a shard's
        // nodes are down.
        runCommandFailOnShardsPassOnConfigs(cmd);

        dropTestData();
        setupFunc();
        // Stop replication at all config server secondaries and all shard secondaries.
        stopReplicationOnSecondariesOfAllShards(st);
        st.configRS.awaitReplication(30000);
        stopReplicationOnSecondaries(st.configRS);

        // Command should fail after two config servers are not replicating.
        req.writeConcern.wtimeout = 3000;
        res = runCommandCheckAdmin(db, cmd);
        restartReplicationOnAllShards(st);
        assert.commandFailed(res);
        assert(!res.writeConcernError,
               'command on config servers with a paused replicaset had writeConcernError: ' +
                   tojson(res));
    }

    commands.forEach(function(cmd) {
        invalidWriteConcerns.forEach(function(wc) {
            testInvalidWriteConcern(wc, cmd);
        });
        testMajorityWriteConcern(cmd);
    });

})();