summaryrefslogtreecommitdiff
path: root/jstests/replsets/initiate_emrc_false.js
blob: 23990ec704c5fc427964b6115bb957c350d9f1fe (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
/*
 * Test that there is no side effects of failing replSetInitiate with eMRC=false, SERVER-58780.
 * @tags: [requires_persistence, multiversion_incompatible]
 */

(function() {
"use strict";

load("jstests/libs/storage_helpers.js");  // getOldestRequiredTimestampForCrashRecovery()

function runTest({cleanShutdown}) {
    const rst = new ReplSetTest({
        name: jsTestName(),
        nodes: 2,
        nodeOptions: {
            enableMajorityReadConcern: "false",
        }
    });
    const nodes = rst.startSet();

    // Run replSetInitiate with an invalid config and expect it to fail.
    jsTestLog("Running invalid replSetInitiate");
    assert.commandFailedWithCode(nodes[0].adminCommand({replSetInitiate: {foo: "bar"}}),
                                 ErrorCodes.InvalidReplicaSetConfig);
    assert.commandFailedWithCode(nodes[1].adminCommand({replSetInitiate: {foo: "bar"}}),
                                 ErrorCodes.InvalidReplicaSetConfig);

    rst.initiateWithHighElectionTimeout();

    // Do some dummy writes.
    const primary = rst.getPrimary();
    const testDB = primary.getDB("test");
    assert.commandWorked(testDB.runCommand({
        insert: "coll",
        documents: [...Array(100).keys()].map(x => ({_id: x})),
        writeConcern: {w: "majority"}
    }));
    rst.awaitSecondaryNodes();

    const secondary = rst.getSecondary();

    // Wait until the secondary has an initial checkpoint.
    assert.soon(() => {
        const oldestRequiredTimestampForCrashRecovery =
            getOldestRequiredTimestampForCrashRecovery(secondary.getDB("admin"));
        jsTestLog("Secondary oldestRequiredTimestampForCrashRecovery: " +
                  tojson(oldestRequiredTimestampForCrashRecovery));
        return oldestRequiredTimestampForCrashRecovery &&
            timestampCmp(oldestRequiredTimestampForCrashRecovery, Timestamp(0, 0)) > 0;
    }, "Timeout waiting for the initial checkpoint", ReplSetTest.kDefaultTimeoutMS, 1000);

    if (cleanShutdown) {
        jsTestLog("Restarting secondary node from clean shutdown");
        rst.stop(secondary, 0, undefined, {forRestart: true});
    } else {
        jsTestLog("Restarting secondary node from unclean shutdown");
        rst.stop(secondary, 9, {allowedExitCode: MongoRunner.EXIT_SIGKILL}, {forRestart: true});
    }

    // Restarting from shutdown shouldn't need to go through initial sync again. Set a failpoint to
    // hang initial sync so that if the node decides to do initial sync again, the test will fail.
    rst.start(secondary,
              {
                  setParameter: {
                      'failpoint.initialSyncHangAfterGettingBeginFetchingTimestamp':
                          tojson({mode: 'alwaysOn'}),
                  }
              },
              true /* restart */);

    rst.awaitSecondaryNodes();

    rst.stopSet();
}

runTest({cleanShutdown: true});
runTest({cleanShutdown: false});
})();