summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/indexbg_restart_secondary_noretry.js
blob: 97bcaff5412f720044041863c7adaad831fa9eb8 (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
/**
 * Starts a replica set, builds an index in background.  Kills the secondary with a failpoint once
 * the index build starts.  It should *not* build an index on the secondary on restart due to
 * `--noIndexBuildRetry` option being supplied.
 */

// @tags: [requires_persistence, requires_journaling, requires_replication]
(function() {
    'use strict';

    // Assert that running `mongod` with `--noIndexBuildRetry` and `--replSet` does not startup.
    {
        // If code breaks the incompatibility between `--noIndexBuildRetry` and `--replSet`, using
        // `notAStorageEngine` will cause a failure later in execution that returns a different
        // exit code (100).
        var process = MongoRunner.runMongod({
            noIndexBuildRetry: "",
            replSet: "rs0",
            storageEngine: "notAStorageEngine",
            waitForConnect: false
        });
        var exitCode = waitProgram(process.pid);
        assert.eq(1, exitCode);
    }

    // Skip db hash check because secondary will have different number of indexes due to the
    // --noIndexBuildRetry command line option.
    TestData.skipCheckDBHashes = true;

    // Set up replica set.
    var replTest = new ReplSetTest({name: 'bgIndexNoRetry', nodes: 3});
    var nodenames = replTest.nodeList();

    var nodes = replTest.startSet();
    replTest.initiate({
        "_id": "bgIndexNoRetry",
        "members": [
            {"_id": 0, "host": nodenames[0]},
            {"_id": 1, "host": nodenames[1]},
            {"_id": 2, "host": nodenames[2], arbiterOnly: true}
        ]
    });

    var master = replTest.getPrimary();
    var second = replTest.getSecondary();

    var masterDB = master.getDB('bgIndexNoRetrySec');
    var secondDB = second.getDB('bgIndexNoRetrySec');

    var collectionName = 'jstests_bgsec';

    var size = 100;

    var masterColl = masterDB.getCollection(collectionName);
    var bulk = masterColl.initializeUnorderedBulkOp();
    for (var i = 0; i < size; ++i) {
        bulk.insert({i: i});
    }
    assert.writeOK(bulk.execute({j: true}));
    assert.eq(size, masterColl.count(), 'unexpected number of documents after bulk insert.');

    // Make sure the documents get replicated to the secondary.
    replTest.awaitReplication();

    assert.commandWorked(secondDB.adminCommand(
        {configureFailPoint: 'hangAfterStartingIndexBuildUnlocked', mode: 'alwaysOn'}));
    masterColl.createIndex({i: 1}, {background: true});
    masterDB.getLastError(2);
    assert.eq(2, masterColl.getIndexes().length);

    // Kill -9 and restart the secondary, after making sure all writes are durable.
    // Waiting for durable is important for both (A) the record that we started the index build so
    // it is rebuild on restart, and (B) the update to minvalid to show that we've already applied
    // the oplog entry so it isn't replayed. If (A) is present without (B), then there are two ways
    // that the index can be rebuilt on startup and this test is only for the one triggered by (A).
    secondDB.adminCommand({fsync: 1});
    replTest.stop(second, 9, {allowedExitCode: MongoRunner.EXIT_SIGKILL});
    replTest.start(
        second, {"noReplSet": true, "noIndexBuildRetry": ""}, /*restart*/ true, /*wait=*/false);

    // Make sure secondary comes back.
    assert.soon(function() {
        try {
            secondDB.isMaster();  // trigger a reconnect if needed
            return true;
        } catch (e) {
            return false;
        }
    }, "secondary didn't restart", 60000, 1000);

    var secondaryColl = secondDB.getCollection(collectionName);

    assert.neq(2, secondaryColl.getIndexes().length);
    replTest.stopSet();
}());