summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/indexbg_killop_secondary_success.js
blob: 4b51cbf25ea802b354e4c474b3b3222a475a29ea (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
/**
 * Confirms that aborting a background index builds on a secondary before the primary commits
 * results in a consistent state with no crashing.
 *
 * @tags: [
 *   requires_replication,
 * ]
 */
(function() {
"use strict";

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

// This test triggers an unclean shutdown (an fassert), which may cause inaccurate fast counts.
TestData.skipEnforceFastCountOnValidate = true;

const rst = new ReplSetTest({
    nodes: [
        {},
        {
            // Lower priority than the primary, but allow the secondary to vote and participate in
            // commitQuorum.
            rsConfig: {
                priority: 0,
            },
            slowms: 30000,  // Don't log slow operations on secondary. See SERVER-44821.
        },
        {
            // The arbiter prevents the primary from stepping down due to lack of majority in the
            // case where the secondary is restarting due to the (expected) unclean shutdown. Note
            // that the arbiter doesn't participate in the commitQuorum.
            rsConfig: {
                arbiterOnly: true,
            },
        },
    ]
});
const nodes = rst.startSet();
rst.initiate();

let primary = rst.getPrimary();
let primaryDB = primary.getDB('test');
let primaryColl = primaryDB.getCollection('test');

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

let secondary = rst.getSecondary();
IndexBuildTest.pauseIndexBuilds(secondary);

const gracefulIndexBuildFlag =
    FeatureFlagUtil.isEnabled(primaryDB, "IndexBuildGracefulErrorHandling");

const createIdx = (gracefulIndexBuildFlag)
    ? IndexBuildTest.startIndexBuild(
          primary, primaryColl.getFullName(), {a: 1}, {}, ErrorCodes.IndexBuildAborted)
    : IndexBuildTest.startIndexBuild(primary, primaryColl.getFullName(), {a: 1});

// When the index build starts, find its op id.
let secondaryDB = secondary.getDB(primaryDB.getName());
const opId =
    IndexBuildTest.waitForIndexBuildToScanCollection(secondaryDB, primaryColl.getName(), "a_1");

IndexBuildTest.assertIndexBuildCurrentOpContents(secondaryDB, opId, (op) => {
    jsTestLog('Inspecting db.currentOp() entry for index build: ' + tojson(op));
    assert.eq(primaryColl.getFullName(),
              op.ns,
              'Unexpected ns field value in db.currentOp() result for index build: ' + tojson(op));
});

// Kill the index build on the secondary. With the feature flag enabled, this should signal the
// primary to abort the index build.
assert.commandWorked(secondaryDB.killOp(opId));

if (!gracefulIndexBuildFlag) {
    // We expect this to crash the secondary because this error is not recoverable
    assert.soon(function() {
        return rawMongoProgramOutput().search(/Fatal assertion.*(51101)/) >= 0;
    });

    // After restarting the secondary, expect that the index build completes successfully.
    rst.stop(
        secondary.nodeId, undefined, {forRestart: true, allowedExitCode: MongoRunner.EXIT_ABORT});
    rst.start(secondary.nodeId, undefined, true /* restart */);

} else {
    // Expect the secondary to successfully prevent the primary from committing the index build.
    checkLog.containsJson(secondary, 20655);
}

primary = rst.getPrimary();
rst.awaitSecondaryNodes();
primaryDB = primary.getDB('test');
primaryColl = primaryDB.getCollection('test');

secondary = rst.getSecondary();
secondaryDB = secondary.getDB(primaryDB.getName());
const secondaryColl = secondaryDB.getCollection(primaryColl.getName());

// Wait for the index build to complete on all nodes.
rst.awaitReplication();

// Expect successful createIndex command invocation in parallel shell. A new index should be present
// on the primary and secondary.
createIdx();

if (!gracefulIndexBuildFlag) {
    // Check that index was created despite the attempted killOp().
    IndexBuildTest.assertIndexes(primaryColl, 2, ['_id_', 'a_1']);
    IndexBuildTest.assertIndexes(secondaryColl, 2, ['_id_', 'a_1']);
} else {
    // Check that index was aborted by the killOp().
    IndexBuildTest.assertIndexes(primaryColl, 1, ['_id_']);
    IndexBuildTest.assertIndexes(secondaryColl, 1, ['_id_']);
}

rst.stopSet();
})();