summaryrefslogtreecommitdiff
path: root/jstests/replsets/index_restart_secondary.js
blob: d785a394ee39f5cd4a45fb3461916ec1d2f807f3 (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
/**
 * Starts a replica set with arbiter, build an index
 * restart secondary once it starts building index,
 * index build restarts after secondary restarts
 */

var replTest = new ReplSetTest({
    name: 'fgIndex',
    nodes: 3,
    oplogSize: 100,  // This test inserts enough data to wrap the default 40MB oplog.
});

var nodes = replTest.nodeList();

// We need an arbiter to ensure that the primary doesn't step down when we restart the secondary
var conns = replTest.startSet();

// don't run on 32-bit builders since they are slow and single core, which leads to heartbeats
// failing and loss of primary during the bulk write
if (conns[0].getDB('test').serverBuildInfo().bits !== 32) {
    replTest.initiate({
        "_id": "fgIndex",
        "members": [
            {"_id": 0, "host": nodes[0]},
            {"_id": 1, "host": nodes[1]},
            {"_id": 2, "host": nodes[2], "arbiterOnly": true}
        ]
    });

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

    var secondId = replTest.getNodeId(second);

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

    var size = 500000;

    jsTest.log("creating test data " + size + " documents");
    var bulk = masterDB.jstests_fgsec.initializeUnorderedBulkOp();
    for (var i = 0; i < size; ++i) {
        bulk.insert({i: i});
    }
    assert.commandWorked(bulk.execute({w: "majority"}));

    jsTest.log("Creating index");
    masterDB.jstests_fgsec.ensureIndex({i: 1});
    assert.eq(2, masterDB.jstests_fgsec.getIndexes().length);

    // Wait for the secondary to get the index entry
    assert.soon(function() {
        return 2 == secondDB.jstests_fgsec.getIndexes().length;
    }, "index not created on secondary (prior to restart)", 800000, 50);

    jsTest.log("Index created on secondary");

    // restart secondary and reconnect
    jsTest.log("Restarting secondary");
    replTest.restart(secondId, {}, /*wait=*/true);

    // 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", 30000, 1000);

    assert.soon(function() {
        return 2 == secondDB.jstests_fgsec.getIndexes().length;
    }, "Index build not resumed after restart", 30000, 50);

    jsTest.log("index-restart-secondary.js complete");
}

replTest.stopSet();