summaryrefslogtreecommitdiff
path: root/jstests/noPassthroughWithMongod/indexbg_restart_secondary.js
blob: 446502905cb74a6ec9bfc6e16693e7885f9f91ab (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
/**
 * Starts a replica set with arbiter, builds an index in background.
 * Kills the secondary once the index build starts with a failpoint.
 * The index build should resume when the secondary is restarted.
 */

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

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

// We need an arbiter to ensure that the primary doesn't step down
// when we restart the secondary.
replTest.startSet();
replTest.initiate({
    "_id": "bgIndex",
    "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 masterDB = master.getDB('bgIndexSec');
var secondDB = second.getDB('bgIndexSec');

var collectionName = 'jstests_bgsec';

var coll = masterDB.getCollection(collectionName);

var size = 100;

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

// Make sure the documents make it to the secondary.
replTest.awaitReplication();

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

    // Make sure all writes are durable on the secondary so that we can restart it knowing that
    // the index build will be found on startup.
    // 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});
} finally {
    assert.commandWorked(secondDB.adminCommand(
        {configureFailPoint: 'leaveIndexBuildUnfinishedForShutdown', mode: 'off'}));
}

MongoRunner.stopMongod(second);
replTest.start(second, {}, /*restart=*/true, /*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.getCollection(collectionName).getIndexes().length;
}, "Index build not resumed after restart", 30000, 50);
replTest.stopSet();
}());