summaryrefslogtreecommitdiff
path: root/jstests/replsets/oplog_replay_on_startup.js
blob: 248d04aa1a31da199522029c1cb81878c4e1be49 (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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
// SERVER-7200 On startup, replica set nodes delete oplog state past the oplog delete point and
// apply any remaining unapplied ops before coming up as a secondary.
//
// @tags: [requires_persistence]
(function() {
    "use strict";

    var ns = "test.coll";

    var rst = new ReplSetTest({
        nodes: 1,
    });

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

    var conn = rst.getPrimary();  // Waits for PRIMARY state.
    var nojournal = Array.contains(conn.adminCommand({getCmdLineOpts: 1}).argv, '--nojournal');
    var storageEngine = jsTest.options().storageEngine;
    var term = conn.getCollection('local.oplog.rs').find().sort({$natural: -1}).limit(1).next().t;
    if (typeof(term) == 'undefined') {
        term = -1;  // Use a dummy term for PV0.
    }

    function runTest({
        oplogEntries,
        collectionContents,
        deletePoint,
        begin,
        minValid,
        expectedState,
        expectedApplied,
    }) {
        if (nojournal && (storageEngine === 'mmapv1') && expectedState === 'FATAL') {
            // We can't test fatal states on mmap without a journal because it won't be able
            // to start up again.
            return;
        }

        if (term != -1) {
            term++;  // Each test gets a new term on PV1 to ensure OpTimes always move forward.
        }

        conn = rst.restart(0, {noReplSet: true});  // Restart as a standalone node.
        assert.neq(null, conn, "failed to restart");
        var oplog = conn.getCollection('local.oplog.rs');
        var minValidColl = conn.getCollection('local.replset.minvalid');
        var coll = conn.getCollection(ns);

        // Reset state to empty.
        assert.commandWorked(oplog.runCommand('emptycapped'));
        coll.drop();
        assert.commandWorked(coll.runCommand('create'));

        var ts = (num) => num === null ? Timestamp() : Timestamp(1000, num);

        oplogEntries.forEach((num) => {
            assert.writeOK(oplog.insert({
                ts: ts(num),
                t: term,
                h: 1,
                op: 'i',
                ns: ns,
                o: {_id: num},
            }));
        });

        collectionContents.forEach((num) => {
            assert.writeOK(coll.insert({_id: num}));
        });

        var injectedMinValidDoc = {
            _id: ObjectId(),

            // appliedThrough
            begin: {
                ts: ts(begin),
                t: term,
            },

            oplogDeleteFromPoint: ts(deletePoint),

            // minvalid:
            t: term,
            ts: ts(minValid),
        };

        // This weird mechanism is the only way to bypass mongod's attempt to fill in null
        // Timestamps.
        assert.writeOK(minValidColl.remove({}));
        assert.writeOK(minValidColl.update({}, {$set: injectedMinValidDoc}, {upsert: true}));
        assert.eq(minValidColl.findOne(),
                  injectedMinValidDoc,
                  "If the Timestamps differ, the server may be filling in the null timestamps");

        try {
            conn = rst.restart(0);  // Restart in replSet mode again.
        } catch (e) {
            assert.eq(expectedState, 'FATAL', 'node failed to restart: ' + e);
            return;
        }

        // Wait for the node to go to SECONDARY if it is able.
        assert.soon(
            () =>
                conn.adminCommand('serverStatus').metrics.repl.apply.attemptsToBecomeSecondary > 0,
            () => conn.adminCommand('serverStatus').metrics.repl.apply.attemptsToBecomeSecondary);

        var isMaster = conn.adminCommand('ismaster');
        switch (expectedState) {
            case 'SECONDARY':
                // Primary is also acceptable since once a node becomes secondary, it will try to
                // become primary if it is eligible and has enough votes (which this node does).
                // This is supposed to test that we reach secondary, not that we stay there.
                assert(isMaster.ismaster || isMaster.secondary,
                       'not PRIMARY or SECONDARY: ' + tojson(isMaster));
                break;

            case 'RECOVERING':
                assert(!isMaster.ismaster && !isMaster.secondary,
                       'not in RECOVERING: ' + tojson(isMaster));

                // Restart as a standalone node again so we can read from the collection.
                conn = rst.restart(0, {noReplSet: true});
                break;

            case 'FATAL':
                doassert("server startup didn't fail when it should have");
                break;

            default:
                doassert(`expectedState ${expectedState} is not supported`);
        }

        // Ensure the oplog has the entries it should have and none that it shouldn't.
        assert.eq(conn.getCollection('local.oplog.rs')
                      .find({ns: ns, op: 'i'})
                      .sort({$natural: 1})
                      .map((op) => op.o._id),
                  expectedApplied);

        // Ensure that all ops that should have been applied were.
        conn.setSlaveOk(true);
        assert.eq(conn.getCollection(ns).find().sort({_id: 1}).map((obj) => obj._id),
                  expectedApplied);
    }

    //
    // Normal 3.4 cases
    //

    runTest({
        oplogEntries: [1, 2, 3],
        collectionContents: [1, 2, 3],
        deletePoint: null,
        begin: null,
        minValid: null,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3],
    });

    runTest({
        oplogEntries: [1, 2, 3],
        collectionContents: [1, 2, 3],
        deletePoint: null,
        begin: null,
        minValid: 2,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3],
    });

    runTest({
        oplogEntries: [1, 2, 3],
        collectionContents: [1, 2, 3],
        deletePoint: null,
        begin: null,
        minValid: 3,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3],
    });

    runTest({
        oplogEntries: [1, 2, 3],
        collectionContents: [1, 2, 3],
        deletePoint: null,
        begin: 3,
        minValid: 3,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3],
    });

    runTest({
        oplogEntries: [1, 2, 3],
        collectionContents: [1, 2, 3],
        deletePoint: 4,
        begin: 3,
        minValid: 3,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3],
    });

    runTest({
        oplogEntries: [1, 2, 3, 4, 5, 6],
        collectionContents: [1, 2, 3],
        deletePoint: 4,
        begin: 3,
        minValid: 3,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3],
    });

    runTest({
        oplogEntries: [1, 2, 3, /*4,*/ 5, 6],
        collectionContents: [1, 2, 3],
        deletePoint: 4,
        begin: 3,
        minValid: 3,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3],
    });

    runTest({
        oplogEntries: [1, 2, 3, 4, 5, 6],
        collectionContents: [1, 2, 3],
        deletePoint: null,
        begin: 3,
        minValid: 3,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3, 4, 5, 6],
    });

    runTest({
        oplogEntries: [1, 2, 3, 4, 5, 6],
        collectionContents: [1, 2, 3],
        deletePoint: null,
        begin: 3,
        minValid: 6,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3, 4, 5, 6],
    });

    //
    // 3.2 -> 3.4 upgrade cases
    //

    runTest({
        oplogEntries: [1, 2, 3],
        collectionContents: [1, 2, 3],
        deletePoint: null,
        begin: 3,
        minValid: 6,
        expectedState: 'RECOVERING',
        expectedApplied: [1, 2, 3],
    });

    runTest({
        oplogEntries: [1, 2, 3, 4, 5],
        collectionContents: [1, 2, 3],
        deletePoint: null,
        begin: 3,
        minValid: 6,
        expectedState: 'RECOVERING',
        expectedApplied: [1, 2, 3, 4, 5],
    });

    runTest({
        oplogEntries: [1, 2, 3, 4, 5],
        collectionContents: [1, 2, 3, 4, 5],
        deletePoint: null,
        begin: null,
        minValid: 6,
        expectedState: 'RECOVERING',
        expectedApplied: [1, 2, 3, 4, 5],
    });

    //
    // 3.4 -> 3.2 -> 3.4 downgrade/reupgrade cases
    //

    runTest({
        oplogEntries: [1, 2, 3],
        collectionContents: [1, 2, 3],
        deletePoint: 4,
        begin: 3,
        minValid: 6,
        expectedState: 'RECOVERING',
        expectedApplied: [1, 2, 3],
    });

    runTest({
        oplogEntries: [1, 2, 3, 4, 5],
        collectionContents: [1, 2, 3],
        deletePoint: 4,
        begin: 3,
        minValid: 6,
        expectedState: 'RECOVERING',
        expectedApplied: [1, 2, 3],
    });

    runTest({
        oplogEntries: [1, 2, 3, /*4,*/ 5, 6],
        collectionContents: [1, 2, 3],
        deletePoint: 4,
        begin: 3,
        minValid: 6,
        expectedState: 'RECOVERING',
        expectedApplied: [1, 2, 3],
    });

    runTest({
        oplogEntries: [1, 2, 3],
        collectionContents: [1, 2, 3],
        deletePoint: 2,
        begin: null,
        minValid: 3,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3],
    });

    runTest({
        oplogEntries: [1, 2, 3],
        collectionContents: [1, 2, 3],
        deletePoint: 2,
        begin: 3,
        minValid: 6,
        expectedState: 'RECOVERING',
        expectedApplied: [1, 2, 3],
    });

    runTest({
        oplogEntries: [1, 2, 3, 4, 5],
        collectionContents: [1, 2, 3],
        deletePoint: 2,
        begin: 3,
        minValid: 6,
        expectedState: 'RECOVERING',
        expectedApplied: [1, 2, 3, 4, 5],
    });

    runTest({
        oplogEntries: [1, 2, 3, 4, 5, 6],
        collectionContents: [1, 2, 3],
        deletePoint: 2,
        begin: 3,
        minValid: 6,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3, 4, 5, 6],
    });

    //
    // These states should be impossible to get into.
    //

    runTest({
        oplogEntries: [1, 2, 3],
        collectionContents: [1, 2, 3, 4],
        deletePoint: null,
        begin: 4,
        minValid: null,  // doesn't matter.
        expectedState: 'FATAL',
    });

    runTest({
        oplogEntries: [4, 5, 6],
        collectionContents: [1, 2],
        deletePoint: 2,
        begin: 3,
        minValid: null,  // doesn't matter.
        expectedState: 'FATAL',
    });

    runTest({
        oplogEntries: [4, 5, 6],
        collectionContents: [1, 2],
        deletePoint: null,
        begin: 3,
        minValid: null,  // doesn't matter.
        expectedState: 'FATAL',
    });

    runTest({
        oplogEntries: [1, 2, 3, 4, 5, 6],
        collectionContents: [1, 2, 3],
        deletePoint: 2,
        begin: 3,
        minValid: 3,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3, 4, 5, 6],
    });

    runTest({
        oplogEntries: [1, 2, 3, 4, 5, 6],
        collectionContents: [1, 2, 3, 4, 5],
        deletePoint: null,
        begin: 5,
        minValid: 3,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3, 4, 5, 6],
    });

    runTest({
        oplogEntries: [1, 2, 3, 4, 5, 6],
        collectionContents: [1, 2, 3, 4, 5],
        deletePoint: null,
        begin: 5,
        minValid: null,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3, 4, 5, 6],
    });

    runTest({
        oplogEntries: [1, 2, 3, 4, 5],
        collectionContents: [1],
        deletePoint: 4,
        begin: 1,
        minValid: 3,
        expectedState: 'SECONDARY',
        expectedApplied: [1, 2, 3],
    });

    rst.stopSet();
})();