summaryrefslogtreecommitdiff
path: root/jstests/core/mr_killop.js
blob: 8ed4d1cc680334c00c533c155b1ee4ee301226ca (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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
// Test killop applied to m/r operations and child ops of m/r operations.
// The test runs commands that are not allowed with security token: killOp, mapreduce.
// @tags: [
//   not_allowed_with_security_token,
//   # mapReduce does not support afterClusterTime.
//   does_not_support_causal_consistency,
//   does_not_support_stepdowns,
//   uses_map_reduce_with_temp_collections,
//   uses_multiple_connections,
//   uses_parallel_shell,
//   no_selinux,
// ]
(function() {
"use strict";
const source = db.jstests_mr_killop;
source.drop();
const out = db.jstests_mr_killop_out;
out.drop();
assert.commandWorked(db.adminCommand({configureFailPoint: "mr_killop_test_fp", mode: "alwaysOn"}));

/** @return op code for map reduce op created by spawned shell. */
function getOpCode() {
    const inProg = db.currentOp().inprog;

    function isMapReduce(op) {
        if (!op.command) {
            return false;
        }

        const cmdBody = op.command;
        if (cmdBody.$truncated) {
            const stringifiedCmd = cmdBody.$truncated;
            return (stringifiedCmd.search('mapreduce') >= 0 ||
                    stringifiedCmd.search('aggregate') >= 0) &&
                stringifiedCmd.search(source.getName()) >= 0;
        }

        return (cmdBody.mapreduce && cmdBody.mapreduce == source.getName()) ||
            (cmdBody.isMapReduceCommand && cmdBody.aggregate == source.getName());
    }

    for (let i in inProg) {
        const o = inProg[i];
        // Identify a map/reduce operation by its collection, whether or not it is currently active.
        if ((o.active || o.waitingForLock) && isMapReduce(o))
            return o.opid;
    }
    return -1;
}

/**
 * Run one mapReduce with the specified parameters in a parallel shell. Kill the map reduce op and
 * wait for the map reduce op to terminate.
 */
function runTest(map, reduce, finalize, scope, wait) {
    source.drop();
    out.drop();
    // Ensure we have 2 documents for the reduce to run.
    assert.commandWorked(source.insert({a: 1}));
    assert.commandWorked(source.insert({a: 1}));

    const spec = {mapreduce: source.getName(), out: out.getName(), map: map, reduce: reduce};
    if (finalize) {
        spec["finalize"] = finalize;
    }
    if (scope) {
        spec["scope"] = scope;
    }

    // Windows shell strips all double quotes from command line, so use single quotes.
    const stringifiedSpec = tojson(spec).toString().replace(/\n/g, ' ').replace(/\"/g, "\'");

    // The assert below won't be caught by this test script, but it will cause error messages to be
    // printed.
    const awaitShell =
        startParallelShell("assert.commandWorked( db.runCommand( " + stringifiedSpec + " ) );");

    if (wait) {
        sleep(20);
    }

    let opCode = null;
    assert.soon(function() {
        opCode = getOpCode();
        return opCode != -1;
    });

    db.killOp(opCode);

    // When the map reduce op is killed, the spawned shell will exit
    const exitCode = awaitShell({checkExitSuccess: false});
    assert.neq(0,
               exitCode,
               "expected shell to exit abnormally due to map-reduce execution being terminated");
    assert.eq(-1, getOpCode());
}

/** Test using wait and non wait modes. */
function runTests(map, reduce, finalize, scope) {
    runTest(map, reduce, finalize, scope, false);
    runTest(map, reduce, finalize, scope, true);
}

/** Test looping in map function. */
function runMapTests(loop) {
    // Without scope.
    runTests(
        loop,  // map
        function(k, v) {
            return v[0];
        },     // reduce
        null,  // finalize
        null   // scope
    );

    // With scope.
    runTests(
        function() {
            loop();
        },  // map
        function(k, v) {
            return v[0];
        },            // reduce
        null,         // finalize
        {loop: loop}  // scope
    );
}

/** Test looping in reduce function. */
function runReduceTests(loop) {
    // Without scope.
    runTests(
        function() {
            emit(this.a, 1);
        },     // map
        loop,  // reduce
        null,  // finalize
        null   // scope
    );

    // With scope.
    runTests(
        function() {
            emit(this.a, 1);
        },  // map
        function() {
            loop();
        },            // reduce
        null,         // finalize
        {loop: loop}  // scope
    );
}

/** Test looping in finalize function. */
function runFinalizeTests(loop) {
    // Without scope.
    runTests(
        function() {
            emit(this.a, 1);
        },  // map
        function(k, v) {
            return v[0];
        },     // reduce
        loop,  // finalize
        null   // scope
    );

    // With scope.
    runTests(
        function() {
            emit(this.a, 1);
        },  // map
        function(k, v) {
            return v[0];
        },  // reduce
        function(a, b) {
            loop();
        },            // finalize
        {loop: loop}  // scope
    );
}

const loop = function() {
    while (1) {
        sleep(10);
    }
};
runMapTests(loop, false);
runReduceTests(loop, false);
runFinalizeTests(loop, false);
db.adminCommand({configureFailPoint: "mr_killop_test_fp", mode: "off"});
}());