summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/index_build_yield_prepare_conflicts.js
blob: 2881ae145315e1d6af7b280e7bcf915d1db4a360 (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
/*
 * This test ensures it is not possible for the collection scan phase of an index build to encounter
 * a prepare conflict after yielding. See SERVER-44577.
 *
 * @tags: [
 *   requires_replication,
 *   uses_transactions,
 *   uses_prepare_transaction,
 * ]
 */
load("jstests/core/txns/libs/prepare_helpers.js");  // For PrepareHelpers.
load("jstests/noPassthrough/libs/index_build.js");  // For IndexBuildTest
load("jstests/libs/fail_point_util.js");

(function() {

"use strict";

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

const dbName = jsTestName();
const collName = "coll";

const primary = rst.getPrimary();
const primaryDB = primary.getDB(dbName);
const primaryAdmin = primary.getDB('admin');
const primaryColl = primaryDB[collName];
const collNss = primaryColl.getFullName();

for (var i = 0; i < 3; i++) {
    assert.commandWorked(primaryColl.insert({_id: i, x: i}));
}
rst.awaitReplication();

// Make the index build collection scan yield often.
assert.commandWorked(primary.adminCommand({setParameter: 1, internalQueryExecYieldIterations: 2}));

jsTestLog("Enable setYieldAllLocksHang fail point");
let res = assert.commandWorked(primaryAdmin.runCommand(
    {configureFailPoint: "setYieldAllLocksHang", data: {namespace: collNss}, mode: "alwaysOn"}));
let timesEntered = res.count;

jsTestLog("Create index");
const awaitIndex = IndexBuildTest.startIndexBuild(primary, primaryColl.getFullName(), {x: 1});

// Wait until index build (collection scan phase) yields.
jsTestLog("Wait for the index build to yield and hang");
assert.commandWorked(primaryAdmin.runCommand({
    waitForFailPoint: "setYieldAllLocksHang",
    timesEntered: timesEntered + 1,
    maxTimeMS: kDefaultWaitForFailPointTimeout
}));

jsTestLog("Start a txn");
const session = primary.startSession();
const sessionDB = session.getDatabase(dbName);
const sessionColl = sessionDB.getCollection(collName);
session.startTransaction();
assert.commandWorked(sessionColl.insert({_id: 20}));

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

// This will make the hybrid build previously started resume.
assert.commandWorked(
    primary.adminCommand({configureFailPoint: "setYieldAllLocksHang", mode: "off"}));

jsTestLog("Wait for index build to complete collection scanning phase");
checkLog.containsJson(primary, 20391);

session.abortTransaction_forTesting();
awaitIndex();

rst.stopSet();
})();