summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/index_abort_stepdown_prepare.js
blob: 061ac4055b35516e0a9f38989dcd4f0f2499d264 (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
/**
 * Tests that we don't hit 3 way deadlock between a interrupted index build, prepared txn and step
 * down thread.
 *
 * This test creates a scenario where:
 * 1) Starts an index build.
 * 2) Transaction gets prepared and holds the collection lock in MODE_IX.
 * 3) A dropIndexes command attempts to abort the createIndexes command. The abort command holds the
 *    RSTL lock in MODE_IX and tries to acquire a MODE_X collection lock, but blocks on the prepared
 *    transaction.
 * 4) Step down enqueues RSTL in MODE_X and waits for aborting thread to release RSTL lock.
 * 5) The aborting thread gets interrupted by step down, step down completes, and the index build
 *    eventually completes on the new primary.
 *
 * @tags: [
 *   uses_prepare_transaction,
 *   uses_transactions,
 * ]
 */
load('jstests/noPassthrough/libs/index_build.js');
load("jstests/replsets/rslib.js");
load("jstests/core/txns/libs/prepare_helpers.js");
load("jstests/libs/fail_point_util.js");

(function() {

"use strict";

const dbName = "test";
const collName = "coll";

const rst = new ReplSetTest({nodes: 1});
rst.startSet();
rst.initiate();

const primary = rst.getPrimary();

const primaryDB = primary.getDB(dbName);
const primaryColl = primaryDB[collName];

TestData.dbName = dbName;
TestData.collName = collName;

jsTestLog("Do a document write");
assert.commandWorked(primaryColl.insert({_id: 1, a: 1}));

// Enable fail point which makes index build hang in an interruptible state.
const failPoint = "hangAfterIndexBuildDumpsInsertsFromBulk";
let res =
    assert.commandWorked(primary.adminCommand({configureFailPoint: failPoint, mode: "alwaysOn"}));
let timesEntered = res.count;

const indexName = "a_1";
const createIndex = IndexBuildTest.startIndexBuild(primary,
                                                   primaryColl.getFullName(),
                                                   {a: 1},
                                                   {name: indexName},
                                                   ErrorCodes.InterruptedDueToReplStateChange);
jsTestLog("Waiting for index build to hit failpoint");
assert.commandWorked(primary.adminCommand({
    waitForFailPoint: failPoint,
    timesEntered: timesEntered + 1,
    maxTimeMS: kDefaultWaitForFailPointTimeout
}));

jsTestLog("Start txn");
const session = primary.startSession();
const sessionDB = session.getDatabase(dbName);
const sessionColl = sessionDB.getCollection(collName);
session.startTransaction();
assert.commandWorked(sessionColl.insert({x: 1}, {$set: {y: 1}}));

jsTestLog("Prepare txn");
PrepareHelpers.prepareTransaction(session);

// Attempt to abort the index build. It will hang holding the RSTL while waiting for the collection
// X lock.
const abortIndexThread = startParallelShell(() => {
    jsTestLog("Attempting to abort the index build");
    assert.commandFailedWithCode(db.getSiblingDB('test').coll.dropIndex("a_1"),
                                 ErrorCodes.InterruptedDueToReplStateChange);
}, primary.port);
checkLog.containsJson(primary, 4656010);

// Stepdown should interrupt the dropIndexes operation and cause it to drop its queued lock.
jsTestLog("Stepping down the primary");
assert.commandWorked(primaryDB.adminCommand({"replSetStepDown": 5 * 60, "force": true}));

// Unblock the index build and wait for threads to join. The stepdown should succeed in unblocking
// the abort. In the case of single-phase index builds, the abort will succeed after the stepdown.
assert.commandWorked(primary.adminCommand({configureFailPoint: failPoint, mode: "off"}));
abortIndexThread();
createIndex();

jsTestLog("Waiting for node to become secondary");
waitForState(primary, ReplSetTest.State.SECONDARY);
// Allow the primary to be re-elected, and wait for it.

assert.commandWorked(primary.adminCommand({replSetFreeze: 0}));
rst.getPrimary();

jsTestLog("Abort txn");
assert.commandWorked(session.abortTransaction_forTesting());

jsTestLog("Waiting for index build to complete");
IndexBuildTest.waitForIndexBuildToStop(primaryDB, primaryColl.getName(), indexName);

IndexBuildTest.assertIndexes(primaryColl, 2, ["_id_", indexName]);

rst.stopSet();
})();