summaryrefslogtreecommitdiff
path: root/jstests/replsets/rollback_all_op_types.js
blob: 8ffc53f2faf54f824b323c18252eb98c6d4888fc (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
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * Test to ensure that rollback is able to handle every supported operation type correctly.
 *
 * The purpose of this integration test is to check that rollback is able to parse and revert all
 * oplog entry types that would be generated in a real system. It provides a level of assurance at a
 * higher system layer than our unit tests, which can be considerably more "artificial". Unit tests
 * will mock many system components, and sometimes will mock behaviors that don't necessarily match
 * true system behavior i.e. mocking an oplog entry with an incorrect format. So, this integration
 * test provides an additional verification of rollback's correctness within a real replica set.
 */

(function() {
"use strict";

load("jstests/replsets/libs/rollback_test_deluxe.js");

let noOp = () => {};

/**
 * All operation types that are able to be rolled back.
 *
 * Each operation type maps to an array of test objects that contains an 'init' function, an
 * 'op' function, and an optional 'description' field. Some operations depend on the current
 * state of the database, so the 'init' function provides a way to set up the database before an
 * operation is executed. All init functions are executed at the very beginning of the test, as
 * part of CommonOps. Also, to provide isolation between commands, each is given its own
 * database to execute in.
 *
 * Each operation has an array of test objects to allow testing of multiple variations of an
 * operation. Each test case in an array will be executed in isolation.
 *
 * Note: The 'dropDatabase' command is excluded and tested separately. It cannot be tested
 * directly using the RollbackTest fixture, since the command is always up-converted to use
 * majority write concern in 3.6.
 *
 */
let rollbackOps = {
    "insert": [{
        init: (db, collName) => {
            assert.commandWorked(db.createCollection(collName));
        },
        op: (db, collName) => {
            assert.writeOK(db[collName].insert({_id: 0}));
        }
    }],
    "update": [{
        init: (db, collName) => {
            assert.writeOK(db[collName].insert({_id: 0, val: 0}));
        },
        op: (db, collName) => {
            assert.writeOK(db[collName].update({_id: 0}, {val: 1}));
        },
    }],
    "delete": [{
        init: (db, collName) => {
            assert.writeOK(db[collName].insert({_id: 0}));
        },
        op: (db, collName) => {
            assert.writeOK(db[collName].remove({_id: 0}));
        },
    }],
    "create": [{
        init: noOp,
        op: (db, collName) => {
            assert.commandWorked(db.createCollection(collName));
        },
    }],
    "drop": [{
        init: (db, collName) => {
            assert.commandWorked(db.createCollection(collName));
        },
        op: (db, collName) => {
            assert.commandWorked(db.runCommand({drop: collName}));
        },
    }],
    "createIndexes": [{
        init: (db, collName) => {
            assert.commandWorked(db.createCollection(collName));
        },
        op: (db, collName) => {
            assert.commandWorked(db.runCommand({
                createIndexes: collName,
                indexes: [{name: collName + "_index", key: {index_key: 1}}]
            }));
        }
    }],
    "dropIndexes": [
        {
            description: "singleIndex",
            init: (db, collName) => {
                assert.commandWorked(db.runCommand({
                    createIndexes: collName,
                    indexes: [{name: collName + "_index", key: {index_key: 1}}]
                }));
            },
            op: (db, collName) => {
                assert.commandWorked(
                    db.runCommand({dropIndexes: collName, index: collName + "_index"}));
            }
        },
        {
            description: "allIndexes",
            init: (db, collName) => {
                assert.commandWorked(db.runCommand({
                    createIndexes: collName,
                    indexes: [
                        {name: collName + "_index_0", key: {index_key_0: 1}},
                        {name: collName + "_index_1", key: {index_key_1: 1}},
                        {name: collName + "_index_2", key: {index_key_2: 1}}
                    ]
                }));
            },
            op: (db, collName) => {
                assert.commandWorked(db.runCommand({dropIndexes: collName, index: "*"}));
            }
        }
    ],
    "renameCollection": [
        {
            description: "withinSameDatabase",
            init: (db, collName) => {
                assert.commandWorked(db.createCollection(collName + "_source"));
            },
            op: (db, collName) => {
                let nss = db[collName].getFullName();
                assert.commandWorked(
                    db.adminCommand({renameCollection: nss + "_source", to: nss + "_dest"}));
            },
        },
        {
            description: "acrossDatabases",
            init: (db, collName) => {
                assert.commandWorked(db.createCollection(collName));
            },
            op: (db, collName) => {
                let sourceNss = db[collName].getFullName();
                let destNss = db.getName() + "_dest." + collName;
                assert.commandWorked(db.adminCommand({renameCollection: sourceNss, to: destNss}));
            },
        },
        {
            description: "acrossDatabasesDropTarget",
            init: (db, collName) => {
                let dbName = db.getName();
                let destDb = db.getSiblingDB(dbName + "_dest");
                assert.commandWorked(db.createCollection(collName));
                assert.commandWorked(destDb.createCollection(collName));
            },
            op: (db, collName) => {
                let sourceNss = db[collName].getFullName();
                let destNss = db.getName() + "_dest." + collName;
                assert.commandWorked(
                    db.adminCommand({renameCollection: sourceNss, to: destNss, dropTarget: true}));
            },
        },
        {
            description: "dropTarget",
            init: (db, collName) => {
                assert.commandWorked(db.createCollection(collName + "_source"));
                assert.commandWorked(db.createCollection(collName + "_dest"));
            },
            op: (db, collName) => {
                let nss = db[collName].getFullName();
                assert.commandWorked(db.adminCommand(
                    {renameCollection: nss + "_source", to: nss + "_dest", dropTarget: true}));
            },
        }

    ],
    "collMod": [
        {
            description: "allCollectionOptions",
            init: (db, collName) => {
                assert.commandWorked(db.createCollection(collName));
            },
            op: (db, collName) => {
                assert.commandWorked(db.runCommand({
                    collMod: collName,
                    validator: {a: 1},
                    validationLevel: "moderate",
                    validationAction: "warn"
                }));
            }
        },
        {
            description: "validationOptionsWithoutValidator",
            init: (db, collName) => {
                assert.commandWorked(db.createCollection(collName));
            },
            op: (db, collName) => {
                assert.commandWorked(db.runCommand(
                    {collMod: collName, validationLevel: "moderate", validationAction: "warn"}));
            }
        },
        {
            description: "existingValidationOptions",
            init: (db, collName) => {
                assert.commandWorked(db.createCollection(collName));
                assert.commandWorked(db.runCommand(
                    {collMod: collName, validationLevel: "moderate", validationAction: "warn"}));
            },
            op: (db, collName) => {
                assert.commandWorked(db.runCommand({
                    collMod: collName,
                    validator: {a: 1},
                    validationLevel: "moderate",
                    validationAction: "warn"
                }));
            }
        }
    ],
    "convertToCapped": [{
        init: (db, collName) => {
            assert.commandWorked(db.createCollection(collName));
        },
        op: (db, collName) => {
            assert.commandWorked(db.runCommand({convertToCapped: collName, size: 1024}));
        },
    }],
    "applyOps": [
        {
            description: "multipleCRUDOps",
            init: (db, collName) => {
                assert.commandWorked(db.createCollection(collName));
            },
            // In 3.6 only document CRUD operations are grouped into a single applyOps oplog
            // entry.
            op: (db, collName) => {
                let collInfo = db.getCollectionInfos({name: collName})[0];
                let uuid = collInfo.info.uuid;
                let coll = db.getCollection(collName);
                let opsToApply = [
                    {op: "i", ns: coll.getFullName(), ui: uuid, o: {_id: 0}},
                    {
                        op: "u",
                        ns: coll.getFullName(),
                        ui: uuid,
                        o: {_id: 0, val: 1},
                        o2: {_id: 0},
                    },
                    {op: "d", ns: coll.getFullName(), ui: uuid, o: {_id: 0}}
                ];
                assert.commandWorked(db.adminCommand({applyOps: opsToApply}));
            }
        },
        {
            description: "opWithoutUUID",
            init: (db, collName) => {
                assert.commandWorked(db.createCollection(collName));
            },
            // In 3.6 only document CRUD operations are grouped into a single applyOps oplog
            // entry.
            op: (db, collName) => {
                let coll = db.getCollection(collName);
                let opsToApply = [
                    {op: "i", ns: coll.getFullName(), o: {_id: 0}},
                ];
                assert.commandWorked(db.adminCommand({applyOps: opsToApply}));
            }
        }
    ]
};

let testCollName = "test";
let opNames = Object.keys(rollbackOps);

/**
 * Create the test name string given an operation name and the test case index. The test
 * name for the nth test case of an operation called "opName", with description "description",
 * will be "opName_<n>_description".
 */
function opTestNameStr(opName, description, ind) {
    let opVariantName = opName + "_" + ind;
    if (description) {
        opVariantName = opVariantName + "_" + description;
    }
    return opVariantName;
}

/**
 * Operations that will be present on both nodes, before the common point.
 */
let CommonOps = (node) => {
    // Ensure there is at least one common op between nodes.
    node.getDB("commonOp")["test"].insert({_id: "common_op"});

    // Run init functions for each op type. Each is given its own database to run in and a
    // standard collection name to use.
    jsTestLog("Performing init operations for every operation type.");
    opNames.forEach(opName => {
        let opObj = rollbackOps[opName];
        opObj.forEach((opVariantObj, ind) => {
            let opVariantName = opTestNameStr(opName, opVariantObj.description, ind);
            opVariantObj.init(node.getDB(opVariantName), testCollName);
        });
    });
};

/**
 * Operations that will be performed on the rollback node past the common point.
 */
let RollbackOps = (node) => {
    // Returns a new object with any metadata fields from the given command object removed.
    function basicCommandObj(fullCommandObj) {
        let basicCommandObj = {};
        for (let field in fullCommandObj) {
            if (fullCommandObj.hasOwnProperty(field) && !field.startsWith("$")) {
                basicCommandObj[field] = fullCommandObj[field];
            }
        }
        return basicCommandObj;
    }

    // Execute the operation given by 'opFn'. 'opName' is the string identifier of the
    // operation to be executed.
    function executeOp(opName, opFn) {
        // Override 'runCommand' so we can capture the raw command object for each operation
        // and log it, to improve diagnostics.
        const runCommandOriginal = Mongo.prototype.runCommand;
        Mongo.prototype.runCommand = function(dbName, commandObj, options) {
            jsTestLog("Executing command for '" + opName + "' test: \n" +
                      tojson(basicCommandObj(commandObj)));
            return runCommandOriginal.apply(this, arguments);
        };

        opFn(node.getDB(opName), testCollName);

        // Reset runCommand to its normal behavior.
        Mongo.prototype.runCommand = runCommandOriginal;
    }

    jsTestLog("Performing rollback operations for every operation type.");
    opNames.forEach(opName => {
        let opObj = rollbackOps[opName];
        // Execute all test cases for this operation type.
        jsTestLog("Performing '" + opName + "' operations.");
        opObj.forEach((opVariantObj, ind) => {
            let opVariantName = opTestNameStr(opName, opVariantObj.description, ind);
            executeOp(opVariantName, opVariantObj.op);
        });
    });
};

// Set up Rollback Test.
let rollbackTest = new RollbackTestDeluxe();
CommonOps(rollbackTest.getPrimary());

// Perform the operations that will be rolled back.
let rollbackNode = rollbackTest.transitionToRollbackOperations();
RollbackOps(rollbackNode);

// Complete cycle one of rollback. Data consistency is checked automatically after entering
// steady state.
rollbackTest.transitionToSyncSourceOperationsBeforeRollback();
rollbackTest.transitionToSyncSourceOperationsDuringRollback();
rollbackTest.transitionToSteadyStateOperations();

// Again, perform operations that will be rolled back. This time, each node in the replica set
// has assumed a different role and will roll back operations that were applied in a different
// state (e.g. as a SECONDARY as opposed to a PRIMARY).
rollbackNode = rollbackTest.transitionToRollbackOperations();
RollbackOps(rollbackNode);

// Complete cycle two of rollback.
rollbackTest.transitionToSyncSourceOperationsBeforeRollback();
rollbackTest.transitionToSyncSourceOperationsDuringRollback();
rollbackTest.transitionToSteadyStateOperations();

// Perform operations that will be rolled back one more time.
rollbackNode = rollbackTest.transitionToRollbackOperations();
RollbackOps(rollbackNode);

// Complete cycle three of rollback. After this cycle is completed, the replica set returns to
// its original topology.
rollbackTest.transitionToSyncSourceOperationsBeforeRollback();
rollbackTest.transitionToSyncSourceOperationsDuringRollback();
rollbackTest.transitionToSteadyStateOperations();

// Check the replica set.
rollbackTest.stop();
})();