summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/killop.js
blob: db9e73ee4b474331ae1e2d751b05cd496c497d85 (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
// Confirms basic killOp execution via mongod and mongos.
// @tags: [
//   requires_replication,
//   requires_sharding,
// ]

(function() {
"use strict";

const dbName = "killop";
const collName = "test";

// 'conn' is a connection to either a mongod when testing a replicaset or a mongos when testing
// a sharded cluster. 'shardConn' is a connection to the mongod we enable failpoints on.
function runTest(conn, shardConn) {
    const db = conn.getDB(dbName);

    assert.commandWorked(
        shardConn.adminCommand({setParameter: 1, internalQueryExecYieldIterations: 1}));
    assert.commandWorked(
        shardConn.adminCommand({"configureFailPoint": "setYieldAllLocksHang", "mode": "alwaysOn"}));

    const findComment = "unique_find_comment";
    const queryToKill = "assert.commandWorked(db.getSiblingDB('" + dbName +
        "').runCommand({find: '" + collName + "', filter: {x: 1}, comment: '" + findComment +
        "'}));";
    const awaitShell = startParallelShell(queryToKill, conn.port);
    let opId;

    const curOpFilter = {
        "ns": dbName + "." + collName,
        "command.comment": findComment,
    };
    assert.soon(
        function() {
            const result = db.currentOp(curOpFilter);
            assert.commandWorked(result);
            if (result.inprog.length === 1 && result.inprog[0].numYields > 0) {
                opId = result.inprog[0].opid;
                return true;
            }

            return false;
        },
        function() {
            return "Failed to find operation in currentOp() output: " +
                tojson(db.currentOp({"ns": dbName + "." + collName}));
        });

    assert.commandWorked(db.killOp(opId));

    let result = db.currentOp(curOpFilter);
    assert.commandWorked(result);
    assert(result.inprog.length === 1, tojson(db.currentOp()));
    assert(result.inprog[0].hasOwnProperty("killPending"));
    assert.eq(true, result.inprog[0].killPending);

    assert.commandWorked(
        shardConn.adminCommand({"configureFailPoint": "setYieldAllLocksHang", "mode": "off"}));

    const exitCode = awaitShell({checkExitSuccess: false});
    assert.neq(0, exitCode, "Expected shell to exit with failure due to operation kill");

    result = db.currentOp(curOpFilter);
    assert.commandWorked(result);
    assert(result.inprog.length === 0, tojson(db.currentOp()));
}

const st = new ShardingTest({shards: 1, rs: {nodes: 1}, mongos: 1});
const shardConn = st.rs0.getPrimary();

// Create the unsharded collection.
assert.commandWorked(st.s.getDB(dbName).getCollection(collName).insert({x: 1}));

// Test killOp against mongod.
runTest(shardConn, shardConn);

// Test killOp against mongos.
runTest(st.s, shardConn);

st.stop();
})();