summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/index_killop.js
blob: 4908ea5ded9201e2588ae608bf9c230abda7d25a (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
/**
 * Confirms that both foreground and background index builds can be aborted using killop.
 */
(function() {
    "use strict";

    const conn = MongoRunner.runMongod({smallfiles: "", nojournal: ""});
    assert.neq(null, conn, "mongod was unable to start up");

    const testDB = conn.getDB("test");
    assert.commandWorked(testDB.dropDatabase());
    testDB.test.insertOne({a: 1});

    // Returns the op id for the running index build, or -1 if there is no current index build.
    function getIndexBuildOpId() {
        const result = testDB.currentOp();
        assert.commandWorked(result);
        let indexBuildOpId = -1;

        result.inprog.forEach(function(op) {
            // Identify the index build as the createIndex command
            // It is assumed that no other clients are concurrently
            // accessing the 'test' database.
            if ((op.op == 'query' || op.op == 'command') && 'createIndexes' in op.command) {
                indexBuildOpId = op.opid;
            }
        });
        return indexBuildOpId;
    }

    // Test that building an index with 'options' can be aborted using killop.
    function testAbortIndexBuild(options) {
        assert.commandWorked(testDB.adminCommand(
            {configureFailPoint: 'hangAfterStartingIndexBuild', mode: 'alwaysOn'}));

        const createIdx = startParallelShell(
            "let coll = db.getSiblingDB('test').test;" +
                "assert.commandWorked(coll.createIndex({ a: 1 }, " + tojson(options) + "));",
            conn.port);

        // When the index build starts, find its op id.
        let opId;
        assert.soon(function() {
            return (opId = getIndexBuildOpId()) != -1;
        }, "Index build operation not found after starting via parallelShell");

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

        // Wait for the index build to stop.
        assert.soon(function() {
            return getIndexBuildOpId() == -1;
        });

        const exitCode = createIdx({checkExitSuccess: false});
        assert.neq(
            0, exitCode, 'expected shell to exit abnormally due to index build being terminated');

        // Check that no new index has been created.  This verifies that the index build was aborted
        // rather than successfully completed.
        assert.eq([{_id: 1}], testDB.test.getIndexKeys());

        assert.commandWorked(
            testDB.adminCommand({configureFailPoint: 'hangAfterStartingIndexBuild', mode: 'off'}));
    }

    testAbortIndexBuild({background: true});
    testAbortIndexBuild({background: false});
})();