summaryrefslogtreecommitdiff
path: root/jstests/replsets/oplog_replay_on_startup_with_bad_op.js
blob: cf7911c248ef9f5ba3f466b8460a727153ec19ac (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
// 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. If the op fails to apply, the
// server must fail to start up.
//
// @tags: [requires_persistence]
(function() {
"use strict";

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

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

var conn = rst.getPrimary();  // Waits for PRIMARY state.

// Wait for the commit point to reach the top of the oplog so that the stableTS can advance.
assert.soon(function() {
    const optimes = assert.commandWorked(conn.adminCommand({replSetGetStatus: 1})).optimes;
    return (0 == rs.compareOpTimes(optimes.lastCommittedOpTime, optimes.appliedOpTime));
});

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 lastOplogDoc = conn.getCollection('local.oplog.rs').find().sort({$natural: -1}).limit(1)[0];
var lastTs = lastOplogDoc.ts;
var newTs = Timestamp(lastTs.t + 1, 1);
var term = lastOplogDoc.t;

assert.commandWorked(oplog.insert({
    ts: newTs,
    t: term,
    h: 1,
    op: 'c',
    ns: 'somedb.$cmd',
    o: {thereIsNoCommandWithThisName: 1},
}));

var injectedMinValidDoc = {
    _id: ObjectId(),

    // appliedThrough
    begin: {
        ts: lastTs,
        t: term,
    },

    // minvalid:
    t: term,
    ts: newTs,
};

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

assert.throws(() => rst.restart(0));  // Restart in replSet mode again.

// fassert() calls std::abort(), which returns a different exit code for Windows vs. other
// platforms.
const exitCode = _isWindows() ? MongoRunner.EXIT_ABRUPT : MongoRunner.EXIT_ABORT;
rst.stop(0, undefined, {allowedExitCode: exitCode});
rst.stopSet();
})();