summaryrefslogtreecommitdiff
path: root/jstests/replsets/recovery_after_clean_shutdown_but_not_all_writes_in_snapshot.js
blob: 45a005e255e438e3196383cb0692a600e967872e (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
/**
 * Tests that fast metadata counts are correct after replication recovery following a clean
 * shutdown.
 *
 * @tags: [requires_persistence, requires_replication]
 */
(function() {
    "use strict";

    const rst = new ReplSetTest({
        name: "recoveryAfterCleanShutdown",
        nodes: 2,
        nodeOptions:
            {setParameter: {logComponentVerbosity: tojsononeline({storage: {recovery: 2}})}}
    });
    const nodes = rst.startSet();
    rst.initiate();

    const dbName = "recovery_clean_shutdown";
    let primaryDB = rst.getPrimary().getDB(dbName);
    const wMajority = {writeConcern: {w: "majority", wtimeout: ReplSetTest.kDefaultTimeoutMS}};

    // Create a collection that will have all of its writes in the stable checkpoint.
    const collAllStableWrites = "allWritesInStableCheckpoint";
    assert.commandWorked(primaryDB[collAllStableWrites].insert({_id: "dan"}, wMajority));
    assert.commandWorked(primaryDB[collAllStableWrites].insert({_id: "judah"}, wMajority));
    assert.commandWorked(primaryDB[collAllStableWrites].insert({_id: "vessy"}, wMajority));
    assert.commandWorked(primaryDB[collAllStableWrites].insert({_id: "kyle"}, wMajority));

    // Set up a collection with some writes that make it into the stable checkpoint.
    const collSomeStableWrites = "someWritesInStableCheckpoint";
    assert.commandWorked(primaryDB[collSomeStableWrites].insert({_id: "erjon"}, wMajority));
    assert.commandWorked(primaryDB[collSomeStableWrites].insert({_id: "jungsoo"}, wMajority));

    // Set up a collection whose creation is in the stable checkpoint, but will have no stable
    // writes.
    const collNoStableWrites = "noWritesInStableCheckpoint";
    assert.commandWorked(primaryDB[collNoStableWrites].runCommand("create", wMajority));

    // Wait for all oplog entries to enter the stable checkpoint on all secondaries.
    rst.awaitLastOpCommitted();

    // Disable snapshotting on all members of the replica set so that further operations do not
    // enter the majority snapshot.
    nodes.forEach(node => assert.commandWorked(node.adminCommand(
                      {configureFailPoint: "disableSnapshotting", mode: "alwaysOn"})));
    const w1 = {writeConcern: {w: 1, wtimeout: ReplSetTest.kDefaultTimeoutMS}};

    // Set up a collection whose creation is not in the stable checkpoint.
    const collNoStableCreation = "creationNotInStableCheckpoint";
    assert.commandWorked(primaryDB[collNoStableCreation].runCommand("create", w1));

    // Perform writes on collections that replicate to each node but do not enter the majority
    // snapshot. These commands will be replayed during replication recovery during restart.
    [collSomeStableWrites, collNoStableWrites, collNoStableCreation].forEach(
        coll => assert.commandWorked(
            primaryDB[coll].insert({_id: "insertedAfterSnapshottingDisabled"}, w1)));
    rst.awaitReplication();

    jsTestLog("Checking collection counts after snapshotting has been disabled");
    rst.checkCollectionCounts();

    // Perform a clean shutdown and restart. Note that the 'disableSnapshotting' failpoint will be
    // unset on each node following the restart.
    nodes.forEach(node => rst.restart(node));
    rst.awaitNodesAgreeOnPrimary();
    primaryDB = rst.getPrimary().getDB(dbName);

    // Perform a majority write to ensure that both nodes agree on the majority commit point.
    const collCreatedAfterRestart = "createdAfterRestart";
    assert.commandWorked(
        primaryDB[collCreatedAfterRestart].insert({_id: "insertedAfterRestart", wMajority}));

    // Fast metadata count should be correct after restart in the face of a clean shutdown.
    jsTestLog("Checking collection counts after clean restart of all nodes");
    rst.checkCollectionCounts();

    rst.stopSet();
}());