summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/currentop_query.js
blob: 5de8ce8abaf6372a8962f710980c73e5fb0a3528 (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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
/**
 * Confirms inclusion of query, command object and planSummary in currentOp() for CRUD operations.
 * This test should not be run in the parallel suite as it sets fail points.
 */
(function() {
    "use strict";

    /**
     * @param {Object} params - Configuration options for the test.
     * @param {string} params.readMode - The read mode to use for the parallel shell. This allows
     * testing currentOp() output for both OP_QUERY and OP_GET_MORE queries, as well as "find" and
     * "getMore" commands.
     */
    function runTest(params) {
        var conn = MongoRunner.runMongod({smallfiles: "", nojournal: ""});
        assert.neq(null, conn, "mongod was unable to start up");

        var testDB = conn.getDB("test");
        assert.commandWorked(testDB.dropDatabase());

        var coll = testDB.currentop_query;

        // Force yield to occur on every PlanExecutor iteration.
        assert.commandWorked(
            testDB.adminCommand({setParameter: 1, internalQueryExecYieldIterations: 1}));

        /**
         * Captures currentOp() for a given test command/operation and confirms that namespace,
         * operation type and planSummary are correct.
         *
         *  @param {Object} testObj - Contains test arguments.
         *  @param {function} testObj.test - A function that runs the desired test op/cmd.
         *  @param {string} testObj.planSummary - A string containing the expected planSummary.
         *  @param {Object} testObj.currentOpFilter - A filter to be used to narrow currentOp()
         *  output to only the relevant operation or command.
         *  @param {string} [testObj.command] - The command to test against. Will look for this to
         *  be a key in the currentOp().query object.
         *  @param {string} [testObj.operation] - The operation to test against. Will look for this
         *  to be the value of the currentOp().op field.
         */
        function confirmCurrentOpContents(testObj) {
            // Force queries to hang on yield to allow for currentOp capture.
            assert.commandWorked(testDB.adminCommand(
                {configureFailPoint: "setYieldAllLocksHang", mode: "alwaysOn"}));

            // Set shell read mode for the parallel shell test.
            TestData.shellReadMode = params.readMode;
            TestData.currentOpTest = testObj.test;
            testObj.test = function() {
                db.getMongo().forceReadMode(TestData.shellReadMode);
                TestData.currentOpTest();
            };

            // Run query.
            var awaitShell = startParallelShell(testObj.test, conn.port);

            // Capture currentOp record for the query and confirm that the 'query' and 'planSummary'
            // fields contain the content expected. We are indirectly testing the 'ns' field as well
            // with the currentOp query argument.
            assert.soon(
                function() {
                    testObj.currentOpFilter.ns = coll.getFullName();
                    testObj.currentOpFilter.planSummary = testObj.planSummary;
                    if (testObj.hasOwnProperty("command")) {
                        testObj.currentOpFilter["query." + testObj.command] = {$exists: true};
                    } else if (testObj.hasOwnProperty("operation")) {
                        testObj.currentOpFilter.op = testObj.operation;
                    }

                    var result = testDB.currentOp(testObj.currentOpFilter);
                    assert.commandWorked(result);

                    if (result.inprog.length === 1) {
                        assert.eq(result.inprog[0].appName, "MongoDB Shell", tojson(result));

                        return true;
                    }

                    return false;
                },
                function() {
                    return "Failed to find operation from " + tojson(testObj) +
                        " in currentOp() output: " + tojson(testDB.currentOp());
                });

            // Allow the query to complete.
            assert.commandWorked(
                testDB.adminCommand({configureFailPoint: "setYieldAllLocksHang", mode: "off"}));

            awaitShell();
            delete TestData.currentOpTest;
            delete TestData.shellReadMode;
        }

        //
        // Confirm currentOp content for commands defined in 'testList'.
        //
        var testList = [
            {
              test: function() {
                  assert.eq(db.currentop_query
                                .aggregate([{$match: {a: 1, $comment: "currentop_query"}}], {
                                    collation: {locale: "fr"},
                                    hint: {_id: 1},
                                    comment: "currentop_query_2"
                                })
                                .itcount(),
                            1);
              },
              command: "aggregate",
              planSummary: "IXSCAN { _id: 1 }",
              currentOpFilter: {
                  "query.pipeline.0.$match.$comment": "currentop_query",
                  "query.comment": "currentop_query_2",
                  "query.collation": {locale: "fr"},
                  "query.hint": {_id: 1}
              }
            },
            {
              test: function() {
                  assert.eq(db.currentop_query.find({a: 1, $comment: "currentop_query"})
                                .collation({locale: "fr"})
                                .count(),
                            1);
              },
              command: "count",
              planSummary: "COLLSCAN",
              currentOpFilter:
                  {"query.query.$comment": "currentop_query", "query.collation": {locale: "fr"}}
            },
            {
              test: function() {
                  assert.eq(
                      db.currentop_query.distinct(
                          "a", {a: 1, $comment: "currentop_query"}, {collation: {locale: "fr"}}),
                      [1]);
              },
              command: "distinct",
              planSummary: "COLLSCAN",
              currentOpFilter:
                  {"query.query.$comment": "currentop_query", "query.collation": {locale: "fr"}}
            },
            {
              test: function() {
                  assert.eq(db.currentop_query.find({a: 1}).comment("currentop_query").itcount(),
                            1);
              },
              command: "find",
              planSummary: "COLLSCAN",
              currentOpFilter: {"query.comment": "currentop_query"}
            },
            {
              test: function() {
                  assert.eq(db.currentop_query.findAndModify({
                      query: {a: 1, $comment: "currentop_query"},
                      update: {$inc: {b: 1}},
                      collation: {locale: "fr"}
                  }),
                            {"_id": 1, "a": 1});
              },
              command: "findandmodify",
              planSummary: "COLLSCAN",
              currentOpFilter:
                  {"query.query.$comment": "currentop_query", "query.collation": {locale: "fr"}}
            },
            {
              test: function() {
                  assert.eq(db.currentop_query.group({
                      key: {a: 1},
                      cond: {a: 1, $comment: "currentop_query"},
                      reduce: function() {},
                      initial: {},
                      collation: {locale: "fr"}
                  }),
                            [{"a": 1}]);
              },
              command: "group",
              planSummary: "COLLSCAN",
              currentOpFilter: {
                  "query.group.cond.$comment": "currentop_query",
                  "query.group.collation": {locale: "fr"}
              }
            },
            {
              test: function() {
                  assert.commandWorked(db.currentop_query.mapReduce(
                      function() {
                          emit(this.a, this.b);
                      },
                      function(a, b) {
                          return Array.sum(b);
                      },
                      {
                        query: {a: 1, $comment: "currentop_query"},
                        out: {inline: 1},
                        collation: {locale: "fr"}
                      }));
              },
              command: "mapreduce",
              planSummary: "COLLSCAN",
              currentOpFilter:
                  {"query.query.$comment": "currentop_query", "query.collation": {locale: "fr"}}
            },
            {
              test: function() {
                  assert.writeOK(db.currentop_query.remove({a: 2, $comment: "currentop_query"},
                                                           {collation: {locale: "fr"}}));
              },
              operation: "remove",
              planSummary: "COLLSCAN",
              currentOpFilter: {"query.$comment": "currentop_query", "collation": {locale: "fr"}}
            },
            {
              test: function() {
                  assert.writeOK(db.currentop_query.update({a: 1, $comment: "currentop_query"},
                                                           {$inc: {b: 1}},
                                                           {collation: {locale: "fr"}}));
              },
              operation: "update",
              planSummary: "COLLSCAN",
              currentOpFilter: {"query.$comment": "currentop_query", "collation": {locale: "fr"}}
            }
        ];

        coll.drop();
        var i;
        for (i = 0; i < 5; ++i) {
            assert.writeOK(coll.insert({_id: i, a: i}));
        }

        testList.forEach(confirmCurrentOpContents);

        //
        // Confirm currentOp contains collation for find command.
        //
        if (params.readMode === "commands") {
            confirmCurrentOpContents({
                test: function() {
                    assert.eq(db.currentop_query.find({a: 1})
                                  .comment("currentop_query")
                                  .collation({locale: "fr"})
                                  .itcount(),
                              1);
                },
                command: "find",
                planSummary: "COLLSCAN",
                currentOpFilter:
                    {"query.comment": "currentop_query", "query.collation": {locale: "fr"}}
            });
        }

        //
        // Confirm currentOp content for geoNear.
        //
        coll.drop();
        for (i = 0; i < 10; ++i) {
            assert.writeOK(coll.insert({a: i, loc: {type: "Point", coordinates: [i, i]}}));
        }
        assert.commandWorked(coll.createIndex({loc: "2dsphere"}));

        confirmCurrentOpContents({
            test: function() {
                assert.commandWorked(db.runCommand({
                    geoNear: "currentop_query",
                    near: {type: "Point", coordinates: [1, 1]},
                    spherical: true,
                    query: {$comment: "currentop_query"},
                    collation: {locale: "fr"}
                }));
            },
            command: "geoNear",
            planSummary: "GEO_NEAR_2DSPHERE { loc: \"2dsphere\" }",
            currentOpFilter:
                {"query.query.$comment": "currentop_query", "query.collation": {locale: "fr"}}
        });

        //
        // Confirm currentOp content for getMore. This case tests command and legacy getMore with an
        // originating find command.
        //
        coll.drop();
        for (i = 0; i < 10; ++i) {
            assert.writeOK(coll.insert({a: i}));
        }

        var cmdRes = testDB.runCommand(
            {find: "currentop_query", filter: {$comment: "currentop_query"}, batchSize: 0});
        assert.commandWorked(cmdRes);

        TestData.commandResult = cmdRes;

        var filter = {
            "query.getMore": TestData.commandResult.cursor.id,
            "originatingCommand.filter.$comment": "currentop_query"
        };

        confirmCurrentOpContents({
            test: function() {
                var cursor = new DBCommandCursor(db.getMongo(), TestData.commandResult, 5);
                assert.eq(cursor.itcount(), 10);
            },
            planSummary: "COLLSCAN",
            currentOpFilter: filter
        });

        delete TestData.commandResult;

        //
        // Confirm that currentOp displays upconverted getMore and originatingCommand in the case of
        // a legacy query.
        //
        if (params.readMode === "legacy") {
            let filter = {
                "query.getMore": {$gt: 0},
                "query.collection": "currentop_query",
                "query.batchSize": 2,
                originatingCommand: {
                    find: "currentop_query",
                    filter: {},
                    ntoreturn: 2,
                    comment: "currentop_query"
                }
            };

            confirmCurrentOpContents({
                test: function() {
                    // Temporarily disable hanging yields so that we can iterate the first batch.
                    assert.commandWorked(
                        db.adminCommand({configureFailPoint: "setYieldAllLocksHang", mode: "off"}));

                    let cursor =
                        db.currentop_query.find({}).comment("currentop_query").batchSize(2);

                    // Exhaust the current batch so that the next request will force a getMore.
                    while (cursor.objsLeftInBatch() > 0) {
                        cursor.next();
                    }

                    // Set yields to hang so that we can check currentOp output.
                    assert.commandWorked(db.adminCommand(
                        {configureFailPoint: "setYieldAllLocksHang", mode: "alwaysOn"}));

                    assert.eq(cursor.itcount(), 8);
                },
                operation: "getmore",
                planSummary: "COLLSCAN",
                currentOpFilter: filter
            });
        }

        //
        // Confirm 512 byte size limit for currentOp query field.
        //
        coll.drop();
        assert.writeOK(coll.insert({a: 1}));

        // When the currentOp command serializes the query object as a string, individual string
        // values inside it are truncated at 150 characters. To test "total length" truncation we
        // need to pass multiple values, each smaller than 150 bytes.
        TestData.queryFilter = {
            "1": "1".repeat(100),
            "2": "2".repeat(100),
            "3": "3".repeat(100),
            "4": "4".repeat(100),
            "5": "5".repeat(100),
            "6": "6".repeat(100),
        };

        var truncatedQueryString = "{ find: \"currentop_query\", filter: { " +
            "1: \"1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111\", " +
            "2: \"2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222\", " +
            "3: \"3333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333333\", " +
            "4: \"4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444\", " +
            "5: \"5555555555555555555555555555555555555555...";

        confirmCurrentOpContents({
            test: function() {
                assert.eq(db.currentop_query.find(TestData.queryFilter)
                              .comment("currentop_query")
                              .itcount(),
                          0);
            },
            planSummary: "COLLSCAN",
            currentOpFilter:
                {"query.$truncated": truncatedQueryString, "query.comment": "currentop_query"}
        });

        // Verify that an originatingCommand truncated by currentOp appears as { $truncated:
        // <string>, comment: <string> }.
        cmdRes = testDB.runCommand({
            find: "currentop_query",
            filter: TestData.queryFilter,
            comment: "currentop_query",
            batchSize: 0
        });
        assert.commandWorked(cmdRes);

        TestData.commandResult = cmdRes;

        filter = {
            "query.getMore": TestData.commandResult.cursor.id,
            "originatingCommand.$truncated": truncatedQueryString,
            "originatingCommand.comment": "currentop_query"
        };

        confirmCurrentOpContents({
            test: function() {
                var cursor = new DBCommandCursor(db.getMongo(), TestData.commandResult, 5);
                assert.eq(cursor.itcount(), 0);
            },
            planSummary: "COLLSCAN",
            currentOpFilter: filter
        });

        delete TestData.commandResult;

        // Verify that an aggregation truncated by currentOp appears as { $truncated: <string>,
        // comment: <string> } when a comment parameter is present.
        truncatedQueryString = "{ aggregate: \"currentop_query\", pipeline: [ { $match: { 1: " +
            "\"111111111111111111111111111111111111111111111111111111111111111111111111111111" +
            "1111111111111111111111\", 2: \"2222222222222222222222222222222222222222222222222" +
            "222222222222222222222222222222222222222222222222222\", 3: \"33333333333333333333" +
            "33333333333333333333333333333333333333333333333333333333333333333333333333333333" +
            "\", 4: \"44444444444444444444444444444444444444444444444444444444444444444444444" +
            "44444444444444444444444444444\", 5: \"555555555555555555555...";

        confirmCurrentOpContents({
            test: function() {
                assert.eq(
                    db.currentop_query
                        .aggregate([{$match: TestData.queryFilter}], {comment: "currentop_query"})
                        .itcount(),
                    0);
            },
            planSummary: "COLLSCAN",
            currentOpFilter:
                {"query.$truncated": truncatedQueryString, "query.comment": "currentop_query"}
        });

        delete TestData.queryFilter;
    }

    runTest({readMode: "commands"});
    runTest({readMode: "legacy"});
})();