summaryrefslogtreecommitdiff
path: root/jstests/noPassthrough/max_time_ms.js
blob: a780b21cbbb3e628907358290d841e4d9b625aca (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
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
/**
 * Tests query/command option $maxTimeMS.
 *
 * Creates a sharded cluster.
 * @tags: [
 *   requires_sharding,
 * ]
 */
(function() {
"use strict";

load("jstests/libs/fixture_helpers.js");  // For runCommandOnEachPrimary().

function executeTest(db, isMongos) {
    let cursor;
    let error;

    const admin = db.getSiblingDB("admin");

    function setFailPoint(fpCommand) {
        if (isMongos) {
            FixtureHelpers.runCommandOnEachPrimary({db: admin, cmdObj: fpCommand});
        }

        // Also set it on mongos (or if we're not connected to mongos, the primary).
        assert.commandWorked(admin.runCommand(fpCommand));
    }

    //
    // Simple positive test for query: a ~100 second query with a 100ms time limit should be
    // aborted.
    //
    (function simplePositiveCase() {
        const t = db.max_time_ms_simple_positive;

        assert.commandWorked(t.insert(Array.from({length: 1000}, _ => ({}))));
        cursor = t.find({
            $where: function() {
                sleep(100);
                return true;
            }
        });
        cursor.maxTimeMS(100);
        error = assert.throws(function() {
            cursor.itcount();
        }, [], "expected query to abort due to time limit");
        assert.commandFailedWithCode(error, ErrorCodes.MaxTimeMSExpired);
    })();

    //
    // Simple negative test for query: a ~300ms query with a 10s time limit should not hit the time
    // limit.
    //
    (function simpleNegative() {
        const t = db.max_time_ms_simple_negative;

        assert.commandWorked(t.insert([{}, {}, {}]));
        cursor = t.find({
            $where: function() {
                sleep(100);
                return true;
            }
        });
        cursor.maxTimeMS(10 * 1000);
        assert.doesNotThrow(function() {
            cursor.itcount();
        }, [], "expected query to not hit the time limit");
    })();

    //
    // Simple positive test for getmore:
    // - Issue a find() that returns 2 batches: a fast batch, then a slow batch.
    // - The find() has a 4-second time limit; the first batch should run "instantly", but the
    // second
    //   batch takes ~15 seconds, so the getmore should be aborted.
    //
    (function simplePositiveGetMore() {
        const t = db.max_time_ms_simple_positive_get_more;

        assert.commandWorked(t.insert([{_id: 0}, {_id: 1}, {_id: 2}]));  // fast batch
        assert.commandWorked(t.insert(
            [{_id: 3, slow: true}, {_id: 4, slow: true}, {_id: 5, slow: true}]));  // slow batch
        cursor = t.find({
                      $where: function() {
                          if (this.slow) {
                              sleep(5 * 1000);
                          }
                          return true;
                      }
                  }).sort({_id: 1});
        cursor.batchSize(3);
        cursor.maxTimeMS(4 * 1000);
        assert.doesNotThrow(function() {
            cursor.next();
            cursor.next();
            cursor.next();
        }, [], "expected batch 1 (query) to not hit the time limit");
        error = assert.throws(function() {
            cursor.next();
            cursor.next();
            cursor.next();
        }, [], "expected batch 2 (getmore) to abort due to time limit");
        assert.commandFailedWithCode(error, ErrorCodes.MaxTimeMSExpired);
    })();

    //
    // Simple negative test for getmore:
    // - Issue a find() that returns 2 batches: a fast batch, then a slow batch.
    // - The find() has a 10-second time limit; the first batch should run "instantly", and the
    // second
    //   batch takes only ~2 seconds, so both the query and getmore should not hit the time limit.
    //
    (function simpleNegativeGetMore() {
        const t = db.max_time_ms_simple_negative_get_more;

        assert.commandWorked(t.insert([{_id: 0}, {_id: 1}, {_id: 2}]));              // fast batch
        assert.commandWorked(t.insert([{_id: 3}, {_id: 4}, {_id: 5, slow: true}]));  // slow batch
        cursor = t.find({
                      $where: function() {
                          if (this.slow) {
                              sleep(2 * 1000);
                          }
                          return true;
                      }
                  }).sort({_id: 1});
        cursor.batchSize(3);
        cursor.maxTimeMS(10 * 1000);
        assert.doesNotThrow(function() {
            cursor.next();
            cursor.next();
            cursor.next();
        }, [], "expected batch 1 (query) to not hit the time limit");
        assert.doesNotThrow(function() {
            cursor.next();
            cursor.next();
            cursor.next();
        }, [], "expected batch 2 (getmore) to not hit the time limit");
    })();

    //
    // Many-batch positive test for getmore:
    // - Issue a many-batch find() with a 6-second time limit where the results take 10 seconds to
    //   generate; one of the later getmore ops should be aborted.
    //
    (function manyBatchPositiveGetMore() {
        const t = db.max_time_ms_many_batch_positive_get_more;

        for (let i = 0; i < 5; i++) {
            assert.commandWorked(
                t.insert([{_id: 3 * i}, {_id: (3 * i) + 1}, {_id: (3 * i) + 2, slow: true}]));
        }
        cursor = t.find({
                      $where: function() {
                          if (this.slow) {
                              sleep(2 * 1000);
                          }
                          return true;
                      }
                  }).sort({_id: 1});
        cursor.batchSize(3);
        cursor.maxTimeMS(6 * 1000);
        error = assert.throws(function() {
            cursor.itcount();
        }, [], "expected find() to abort due to time limit");
        assert.commandFailedWithCode(error, ErrorCodes.MaxTimeMSExpired);
    })();

    //
    // Many-batch negative test for getmore:
    // - Issue a many-batch find() with a 20-second time limit where the results take 10 seconds to
    //   generate; the find() should not hit the time limit.
    //
    (function manyBatchNegativeGetMore() {
        const t = db.many_batch_negative_get_more;
        for (var i = 0; i < 5; i++) {
            assert.commandWorked(
                t.insert([{_id: 3 * i}, {_id: (3 * i) + 1}, {_id: (3 * i) + 2, slow: true}]));
        }
        cursor = t.find({
                      $where: function() {
                          if (this.slow) {
                              sleep(2 * 1000);
                          }
                          return true;
                      }
                  }).sort({_id: 1});
        cursor.batchSize(3);
        cursor.maxTimeMS(20 * 1000);
        assert.doesNotThrow(function() {
            // SERVER-40305: Add some additional logging here in case this fails to help us track
            // down why it failed.
            assert.commandWorked(db.adminCommand({setParameter: 1, traceExceptions: 1}));
            cursor.itcount();
            assert.commandWorked(db.adminCommand({setParameter: 1, traceExceptions: 0}));
        }, [], "expected find() to not hit the time limit");
    })();

    //
    // Simple positive test for commands: a ~300ms command with a 100ms time limit should be
    // aborted.
    //
    (function simplePositiveSleepCmd() {
        if (isMongos) {
            // Test-only sleep command is not supported on mongos.
            return;
        }
        assert.commandFailedWithCode(
            db.adminCommand({sleep: 1, millis: 300, maxTimeMS: 100, lock: "none"}),
            ErrorCodes.MaxTimeMSExpired);
    })();

    //
    // Simple negative test for commands: a ~300ms command with a 10s time limit should not hit the
    // time limit.
    //
    (function simpleNegativeSleepCmd() {
        if (isMongos) {
            // Test-only sleep command is not supported on mongos.
            return;
        }
        assert.commandWorked(
            db.adminCommand({sleep: 1, millis: 300, maxTimeMS: 10 * 1000, lock: "none"}));
    })();

    //
    // Tests for input validation.
    //
    (function checkValidation() {
        const t = db.max_time_ms_validation;
        assert.commandWorked(t.insert({}));

        // Verify lower boundary for acceptable input (0 is acceptable, 1 isn't).
        assert.doesNotThrow.automsg(function() {
            t.find().maxTimeMS(0).itcount();
        });
        assert.doesNotThrow.automsg(function() {
            t.find().maxTimeMS(NumberInt(0)).itcount();
        });
        assert.doesNotThrow.automsg(function() {
            t.find().maxTimeMS(NumberLong(0)).itcount();
        });
        assert.commandWorked(db.runCommand({ping: 1, maxTimeMS: 0}));
        assert.commandWorked(db.runCommand({ping: 1, maxTimeMS: NumberInt(0)}));
        assert.commandWorked(db.runCommand({ping: 1, maxTimeMS: NumberLong(0)}));

        assert.throws.automsg(function() {
                         t.find().maxTimeMS(-1).itcount();
                     });
        assert.throws.automsg(function() {
                         t.find().maxTimeMS(NumberInt(-1)).itcount();
                     });
        assert.throws.automsg(function() {
                         t.find().maxTimeMS(NumberLong(-1)).itcount();
                     });
        assert.commandFailedWithCode(db.runCommand({ping: 1, maxTimeMS: -1}), ErrorCodes.BadValue);
        assert.commandFailedWithCode(db.runCommand({ping: 1, maxTimeMS: NumberInt(-1)}),
                                     ErrorCodes.BadValue);
        assert.commandFailedWithCode(db.runCommand({ping: 1, maxTimeMS: NumberLong(-1)}),
                                     ErrorCodes.BadValue);

        // Verify upper boundary for acceptable input (2^31-1 is acceptable, 2^31 isn't).

        var maxValue = Math.pow(2, 31) - 1;

        assert.doesNotThrow.automsg(function() {
            t.find().maxTimeMS(maxValue).itcount();
        });
        assert.doesNotThrow.automsg(function() {
            t.find().maxTimeMS(NumberInt(maxValue)).itcount();
        });
        assert.doesNotThrow.automsg(function() {
            t.find().maxTimeMS(NumberLong(maxValue)).itcount();
        });
        assert.commandWorked(db.runCommand({ping: 1, maxTimeMS: maxValue}));
        assert.commandWorked(db.runCommand({ping: 1, maxTimeMS: NumberInt(maxValue)}));
        assert.commandWorked(db.runCommand({ping: 1, maxTimeMS: NumberLong(maxValue)}));

        assert.throws.automsg(function() {
                         t.find().maxTimeMS(maxValue + 1).itcount();
                     });
        assert.throws.automsg(function() {
                         t.find().maxTimeMS(NumberInt(maxValue + 1)).itcount();
                     });
        assert.throws.automsg(function() {
                         t.find().maxTimeMS(NumberLong(maxValue + 1)).itcount();
                     });
        assert.commandFailedWithCode(db.runCommand({ping: 1, maxTimeMS: maxValue + 1}),
                                     ErrorCodes.BadValue);
        assert.commandFailedWithCode(db.runCommand({ping: 1, maxTimeMS: NumberInt(maxValue + 1)}),
                                     ErrorCodes.BadValue);
        assert.commandFailedWithCode(db.runCommand({ping: 1, maxTimeMS: NumberLong(maxValue + 1)}),
                                     ErrorCodes.BadValue);

        // Verify invalid values are rejected.
        assert.throws.automsg(function() {
                         t.find().maxTimeMS(0.1).itcount();
                     });
        assert.throws.automsg(function() {
                         t.find().maxTimeMS(-0.1).itcount();
                     });
        assert.throws.automsg(function() {
                         t.find().maxTimeMS().itcount();
                     });
        assert.throws.automsg(function() {
                         t.find().maxTimeMS("").itcount();
                     });
        assert.throws.automsg(function() {
                         t.find().maxTimeMS(true).itcount();
                     });
        assert.throws.automsg(function() {
                         t.find().maxTimeMS({}).itcount();
                     });
        assert.commandFailedWithCode(db.runCommand({ping: 1, maxTimeMS: 0.1}), ErrorCodes.BadValue);
        assert.commandFailedWithCode(db.runCommand({ping: 1, maxTimeMS: -0.1}),
                                     ErrorCodes.BadValue);
        assert.commandFailedWithCode(db.runCommand({ping: 1, maxTimeMS: undefined}),
                                     ErrorCodes.BadValue);
        assert.commandFailedWithCode(db.runCommand({ping: 1, maxTimeMS: ""}), ErrorCodes.BadValue);
        assert.commandFailedWithCode(db.runCommand({ping: 1, maxTimeMS: true}),
                                     ErrorCodes.BadValue);
        assert.commandFailedWithCode(db.runCommand({ping: 1, maxTimeMS: {}}), ErrorCodes.BadValue);

        // Verify that the server rejects invalid command argument $maxTimeMS.
        assert.commandFailed(t.getDB().runCommand({ping: 1, $maxTimeMS: 0}),
                             [ErrorCodes.InvalidOptions, 40415]);
    })();

    //
    // Tests for fail points maxTimeAlwaysTimeOut and maxTimeNeverTimeOut.
    //
    (function checkFailPointsWork() {
        // maxTimeAlwaysTimeOut positive test for command.
        const t = db.max_time_ms_always_time_out_fp;
        try {
            assert.commandWorked(
                db.adminCommand({configureFailPoint: "maxTimeAlwaysTimeOut", mode: "alwaysOn"}));
            assert.commandFailedWithCode(db.runCommand({ping: 1, maxTimeMS: 10 * 1000}),
                                         ErrorCodes.MaxTimeMSExpired);
        } finally {
            assert.commandWorked(
                db.adminCommand({configureFailPoint: "maxTimeAlwaysTimeOut", mode: "off"}));
        }

        // maxTimeNeverTimeOut positive test for command. Don't run on mongos because there's no
        // sleep command.
        if (!isMongos) {
            try {
                assert.commandWorked(
                    db.adminCommand({configureFailPoint: "maxTimeNeverTimeOut", mode: "alwaysOn"}));
                assert.commandWorked(
                    db.adminCommand({sleep: 1, millis: 300, maxTimeMS: 100, lock: "none"}));
            } finally {
                assert.commandWorked(
                    db.adminCommand({configureFailPoint: "maxTimeNeverTimeOut", mode: "off"}));
            }
        }

        // maxTimeAlwaysTimeOut positive test for query.
        try {
            setFailPoint({configureFailPoint: "maxTimeAlwaysTimeOut", mode: "alwaysOn"});

            assert.throws(function() {
                t.find().maxTimeMS(10 * 1000).itcount();
            }, [], "expected query to trigger maxTimeAlwaysTimeOut fail point");
        } finally {
            setFailPoint({configureFailPoint: "maxTimeAlwaysTimeOut", mode: "off"});
        }

        // maxTimeNeverTimeOut positive test for query.
        try {
            setFailPoint({configureFailPoint: "maxTimeNeverTimeOut", mode: "alwaysOn"});
            const coll = db.max_time_ms_never_time_out_positive_query;
            assert.commandWorked(coll.insert([{}, {}, {}]));
            cursor = coll.find({
                $where: function() {
                    sleep(100);
                    return true;
                }
            });
            cursor.maxTimeMS(100);
            assert.doesNotThrow(function() {
                cursor.itcount();
            }, [], "expected query to trigger maxTimeNeverTimeOut fail point");
        } finally {
            setFailPoint({configureFailPoint: "maxTimeNeverTimeOut", mode: "off"});
        }
    })();

    // maxTimeAlwaysTimeOut positive test for getmore.
    (function alwaysTimeOutPositiveTest() {
        const coll = db.max_time_ms_always_time_out_positive_get_more;
        assert.commandWorked(coll.insert([{}, {}, {}]));
        cursor = coll.find().maxTimeMS(10 * 1000).batchSize(2);
        assert.doesNotThrow.automsg(function() {
            cursor.next();
            cursor.next();
        });
        try {
            setFailPoint({configureFailPoint: "maxTimeAlwaysTimeOut", mode: "alwaysOn"});
            assert.throws(function() {
                cursor.next();
            }, [], "expected getmore to trigger maxTimeAlwaysTimeOut fail point");
        } finally {
            setFailPoint({configureFailPoint: "maxTimeAlwaysTimeOut", mode: "off"});
        }
    })();

    // maxTimeNeverTimeOut positive test for getmore.
    (function neverTimeOutPositiveTest() {
        const coll = db.max_time_ms_never_time_out_fp;
        assert.commandWorked(coll.insert([{_id: 0}, {_id: 1}, {_id: 2}]));  // fast batch
        assert.commandWorked(coll.insert(
            [{_id: 3, slow: true}, {_id: 4, slow: true}, {_id: 5, slow: true}]));  // slow batch
        cursor = coll.find({
                         $where: function() {
                             if (this.slow) {
                                 sleep(2 * 1000);
                             }
                             return true;
                         }
                     })
                     .sort({_id: 1});
        cursor.batchSize(3);
        cursor.maxTimeMS(2 * 1000);
        assert.doesNotThrow(function() {
            cursor.next();
            cursor.next();
            cursor.next();
        }, [], "expected batch 1 (query) to not hit the time limit");
        try {
            setFailPoint({configureFailPoint: "maxTimeNeverTimeOut", mode: "alwaysOn"});

            assert.doesNotThrow(function() {
                cursor.next();
                cursor.next();
                cursor.next();
            }, [], "expected batch 2 (getmore) to trigger maxTimeNeverTimeOut fail point");
        } finally {
            setFailPoint({configureFailPoint: "maxTimeNeverTimeOut", mode: "off"});
        }
    })();

    //
    // Test that maxTimeMS is accepted by commands that have an option allowlist.
    //
    (function testCommandsWithOptionAllowlist() {
        const t = db.max_time_ms_option_allowlist;
        // The namespace must exist for collMod to work.
        assert.commandWorked(t.insert({x: 1}));

        // "aggregate" command.
        assert.commandWorked(
            t.runCommand("aggregate", {pipeline: [], cursor: {}, maxTimeMS: 60 * 1000}));

        // "collMod" command.
        assert.commandWorked(t.runCommand("collMod", {maxTimeMS: 60 * 1000}));

        // "createIndexes" command.
        assert.commandWorked(t.runCommand(
            "createIndexes", {indexes: [{key: {x: 1}, name: "x_1"}], maxTimeMS: 60 * 1000}));
    })();

    //
    // Test count shell helper (see SERVER-13334).
    //
    (function testCountShellHelper() {
        const t = db.max_time_ms_shell_helper;
        try {
            setFailPoint({configureFailPoint: "maxTimeNeverTimeOut", mode: "alwaysOn"});

            assert.doesNotThrow(function() {
                t.find({}).maxTimeMS(1).count();
            });
        } finally {
            setFailPoint({configureFailPoint: "maxTimeNeverTimeOut", mode: "off"});
        }
    })();
}

const enableTestCmd = {
    setParameter: "enableTestCommands=1"
};
(function runOnStandalone() {
    const conn = MongoRunner.runMongod(enableTestCmd);
    assert.neq(null, conn, "mongod was unable to start up");
    try {
        executeTest(conn.getDB("test"), false);
    } finally {
        MongoRunner.stopMongod(conn);
    }
})();

(function runSharded() {
    const enableTestCmd = {setParameter: "enableTestCommands=1"};
    const st = new ShardingTest(
        {shards: 2, other: {shardOptions: enableTestCmd, mongosOptions: enableTestCmd}});
    try {
        executeTest(st.s.getDB("test"), true);
    } finally {
        st.stop();
    }
})();
})();