summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/index_killop_after_stepdown.js
blob: 8a4f20987ca8f25b8f23105aaeeefa137103f7d1 (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
/**
 * Tests a race condition between a user interrupting an index build and the node stepping-down. The
 * nature of this problem is that the stepping-down node is not able to replicate an abortIndexBuild
 * oplog entry after the user kills the operation. The old primary will rely on the new primary to
 * replicate a commitIndexBuild oplog entry after the takeover.
 *
 * @tags: [
 *   requires_replication,
 * ]
 */
(function() {
"use strict";

load('jstests/noPassthrough/libs/index_build.js');
load("jstests/libs/fail_point_util.js");

const rst = new ReplSetTest({
    nodes: [
        {},
        {},
    ]
});
rst.startSet();
rst.initiate();

const primary = rst.getPrimary();
const testDB = primary.getDB('test');
const coll = testDB.getCollection('test');

assert.commandWorked(coll.insert({a: 1}));

let res = assert.commandWorked(primary.adminCommand(
    {configureFailPoint: 'hangBeforeIndexBuildAbortOnInterrupt', mode: 'alwaysOn'}));
const hangBeforeAbortFailpointTimesEntered = res.count;

IndexBuildTest.pauseIndexBuilds(primary);
const createIdx = IndexBuildTest.startIndexBuild(
    primary, coll.getFullName(), {a: 1}, {}, [ErrorCodes.Interrupted]);

// When the index build starts, find its op id. This will be the op id of the client connection, not
// the thread pool task managed by IndexBuildsCoordinatorMongod.
const filter = {
    "desc": {$regex: /conn.*/}
};
const opId = IndexBuildTest.waitForIndexBuildToStart(testDB, coll.getName(), 'a_1', filter);

// Kill the index build.
assert.commandWorked(testDB.killOp(opId));

// Wait for the command thread to observe the killOp.
assert.commandWorked(primary.adminCommand({
    waitForFailPoint: "hangBeforeIndexBuildAbortOnInterrupt",
    timesEntered: hangBeforeAbortFailpointTimesEntered + 1,
    maxTimeMS: kDefaultWaitForFailPointTimeout
}));

// Step down the primary, preventing the index build from generating an abort oplog entry.
assert.commandWorked(testDB.adminCommand({replSetStepDown: 30, force: true}));

// Let the command thread try to abort the index build.
assert.commandWorked(primary.adminCommand(
    {configureFailPoint: 'hangBeforeIndexBuildAbortOnInterrupt', mode: 'off'}));

// Unable to abort index build because we are not primary.
checkLog.containsJson(primary, 20449);

createIdx();

// Let the index build continue running.
IndexBuildTest.resumeIndexBuilds(primary);

// Wait for the index build to stop.
IndexBuildTest.waitForIndexBuildToStop(testDB);

// With two phase index builds, a stepdown will not abort the index build, which should complete
// after a new node becomes primary.
rst.awaitReplication();

// The old primary, now secondary, should process the commitIndexBuild oplog entry.

const secondaryColl = rst.getSecondary().getCollection(coll.getFullName());
IndexBuildTest.assertIndexes(coll, 2, ['_id_', 'a_1'], [], {includeBuildUUIDs: true});
IndexBuildTest.assertIndexes(secondaryColl, 2, ['_id_', 'a_1'], [], {includeBuildUUIDs: true});

rst.stopSet();
})();