summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/drop_ready_index_while_building_an_index.js
blob: 6a1218c1c7d8db7ba62e1cc68e6794a3cf5ae536 (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
/**
 * Tests that the dropIndexes command can drop ready indexes while there are index builds
 * in-progress.
 *
 * @tags: [requires_replication]
 */
(function() {
"use strict";

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

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

const primary = rst.getPrimary();
const secondary = rst.getSecondary();

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

const db = primary.getDB(dbName);
const coll = db.getCollection(collName);
const secondaryDB = secondary.getDB(dbName);
const secondaryColl = secondaryDB.getCollection(collName);

assert.commandWorked(db.createCollection(collName));

for (let i = 0; i < 5; i++) {
    assert.commandWorked(coll.insert({x: i, y: i}));
}

assert.commandWorked(coll.createIndex({x: 1}, {name: "x_1"}));

IndexBuildTest.pauseIndexBuilds(primary);
IndexBuildTest.pauseIndexBuilds(secondary);

const awaitIndexBuild =
    IndexBuildTest.startIndexBuild(db.getMongo(), coll.getFullName(), {y: 1}, {name: "y_1"});
IndexBuildTest.waitForIndexBuildToScanCollection(db, collName, "y_1");
IndexBuildTest.waitForIndexBuildToScanCollection(secondaryDB, collName, "y_1");

IndexBuildTest.assertIndexes(
    coll, /*numIndexes=*/ 3, /*readyIndexes=*/["_id_", "x_1"], /*notReadyIndexes=*/["y_1"]);
// However unlikely, a secondary being in the scan collection phase does not guarantee the oplog
// applier considers the 'startIndexBuild' applied, and as such an attempt to listIndexes might
// fail.
IndexBuildTest.assertIndexesSoon(secondaryColl,
                                 /*numIndexes=*/ 3,
                                 /*readyIndexes=*/["_id_", "x_1"],
                                 /*notReadyIndexes=*/["y_1"]);

// Drop the ready index while another index build is in-progress.
assert.commandWorked(coll.dropIndex("x_1"));
rst.awaitReplication();

IndexBuildTest.assertIndexes(
    coll, /*numIndexes=*/ 2, /*readyIndexes=*/["_id_"], /*notReadyIndexes=*/["y_1"]);
IndexBuildTest.assertIndexes(
    secondaryColl, /*numIndexes=*/ 2, /*readyIndexes=*/["_id_"], /*notReadyIndexes=*/["y_1"]);

IndexBuildTest.resumeIndexBuilds(primary);
IndexBuildTest.resumeIndexBuilds(secondary);

awaitIndexBuild();
rst.awaitReplication();

IndexBuildTest.assertIndexes(
    coll, /*numIndexes=*/ 2, /*readyIndexes=*/["_id_", "y_1"], /*notReadyIndexes=*/[]);
IndexBuildTest.assertIndexes(
    secondaryColl, /*numIndexes=*/ 2, /*readyIndexes=*/["_id_", "y_1"], /*notReadyIndexes=*/[]);

rst.stopSet();
}());