diff options
-rw-r--r-- | jstests/noPassthrough/explain_max_time_ms.js | 71 | ||||
-rw-r--r-- | src/mongo/shell/explain_query.js | 5 | ||||
-rw-r--r-- | src/mongo/shell/explainable.js | 19 |
3 files changed, 92 insertions, 3 deletions
diff --git a/jstests/noPassthrough/explain_max_time_ms.js b/jstests/noPassthrough/explain_max_time_ms.js new file mode 100644 index 00000000000..f9cc72d2da8 --- /dev/null +++ b/jstests/noPassthrough/explain_max_time_ms.js @@ -0,0 +1,71 @@ +/** + * Tests the explain command with the maxTimeMS option. + */ +(function() { + "use strict"; + + const standalone = MongoRunner.runMongod(); + assert.neq(null, standalone, "mongod was unable to start up"); + + const dbName = "test"; + const db = standalone.getDB(dbName); + const collName = "explain_max_time_ms"; + const coll = db.getCollection(collName); + + const destCollName = "explain_max_time_ms_dest"; + const mapFn = function() { + emit(this.i, this.j); + }; + const reduceFn = function(key, values) { + return Array.sum(values); + }; + + coll.drop(); + assert.commandWorked(db.createCollection(collName)); + + assert.writeOK(coll.insert({i: 1, j: 1})); + assert.writeOK(coll.insert({i: 2, j: 1})); + assert.writeOK(coll.insert({i: 2, j: 2})); + + // Set fail point to make sure operations with "maxTimeMS" set will time out. + assert.commandWorked( + db.adminCommand({configureFailPoint: "maxTimeAlwaysTimeOut", mode: "alwaysOn"})); + + for (let verbosity of["executionStats", "allPlansExecution"]) { + // Expect explain to time out if "maxTimeMS" is set on the aggregate command. + assert.commandFailedWithCode(assert.throws(function() { + coll.explain(verbosity).aggregate([{$match: {i: 1}}], {maxTimeMS: 1}); + }), + ErrorCodes.ExceededTimeLimit); + // Expect explain to time out if "maxTimeMS" is set on the count command. + assert.commandFailedWithCode(assert.throws(function() { + coll.explain(verbosity).count({i: 1}, {maxTimeMS: 1}); + }), + ErrorCodes.ExceededTimeLimit); + // Expect explain to time out if "maxTimeMS" is set on the distinct command. + assert.commandFailedWithCode(assert.throws(function() { + coll.explain(verbosity).distinct("i", {}, {maxTimeMS: 1}); + }), + ErrorCodes.ExceededTimeLimit); + // Expect explain to time out if "maxTimeMS" is set on the find command. + assert.commandFailedWithCode(assert.throws(function() { + coll.find().maxTimeMS(1).explain(verbosity); + }), + ErrorCodes.ExceededTimeLimit); + assert.commandFailedWithCode(assert.throws(function() { + coll.explain(verbosity).find().maxTimeMS(1).finish(); + }), + ErrorCodes.ExceededTimeLimit); + // Expect explain to time out if "maxTimeMS" is set on the findAndModify command. + assert.commandFailedWithCode(assert.throws(function() { + coll.explain(verbosity).findAndModify({update: {$inc: {j: 1}}, maxTimeMS: 1}); + }), + ErrorCodes.ExceededTimeLimit); + } + + // Disable fail point. + assert.commandWorked( + db.adminCommand({configureFailPoint: "maxTimeAlwaysTimeOut", mode: "off"})); + + MongoRunner.stopMongod(standalone); +})(); diff --git a/src/mongo/shell/explain_query.js b/src/mongo/shell/explain_query.js index 0670734891e..ddf97698984 100644 --- a/src/mongo/shell/explain_query.js +++ b/src/mongo/shell/explain_query.js @@ -152,6 +152,11 @@ var DBExplainQuery = (function() { var explainCmd = {explain: innerCmd}; explainCmd["verbosity"] = this._verbosity; + // If "maxTimeMS" is set on innerCmd, it needs to be propagated to the top-level + // of explainCmd so that it has the intended effect. + if (innerCmd.hasOwnProperty("maxTimeMS")) { + explainCmd.maxTimeMS = innerCmd.maxTimeMS; + } var explainDb = this._query._db; diff --git a/src/mongo/shell/explainable.js b/src/mongo/shell/explainable.js index da130b9b13c..ba69f77ce63 100644 --- a/src/mongo/shell/explainable.js +++ b/src/mongo/shell/explainable.js @@ -28,6 +28,16 @@ var Explainable = (function() { return explainResult; }; + var buildExplainCmd = function(innerCmd, verbosity) { + var explainCmd = {"explain": innerCmd, "verbosity": verbosity}; + // If "maxTimeMS" is set on innerCmd, it needs to be propagated to the top-level + // of explainCmd so that it has the intended effect. + if (innerCmd.hasOwnProperty("maxTimeMS")) { + explainCmd.maxTimeMS = innerCmd.maxTimeMS; + } + return explainCmd; + }; + function constructor(collection, verbosity) { // // Private vars. @@ -110,7 +120,7 @@ var Explainable = (function() { let aggCmd = Object.extend( {"aggregate": this._collection.getName(), "pipeline": pipeline}, extraOpts); - let explainCmd = {"explain": aggCmd, "verbosity": this._verbosity}; + let explainCmd = buildExplainCmd(aggCmd, this._verbosity); let explainResult = this._collection.runReadCommand(explainCmd); return throwOrReturn(explainResult); } @@ -133,7 +143,7 @@ var Explainable = (function() { this.findAndModify = function(params) { var famCmd = Object.extend({"findAndModify": this._collection.getName()}, params); - var explainCmd = {"explain": famCmd, "verbosity": this._verbosity}; + var explainCmd = buildExplainCmd(famCmd, this._verbosity); var explainResult = this._collection.runReadCommand(explainCmd); return throwOrReturn(explainResult); }; @@ -156,8 +166,11 @@ var Explainable = (function() { if (options && options.hasOwnProperty("collation")) { distinctCmd.collation = options.collation; } + if (options && options.hasOwnProperty("maxTimeMS")) { + distinctCmd.maxTimeMS = options.maxTimeMS; + } - var explainCmd = {explain: distinctCmd, verbosity: this._verbosity}; + var explainCmd = buildExplainCmd(distinctCmd, this._verbosity); var explainResult = this._collection.runReadCommand(explainCmd); return throwOrReturn(explainResult); }; |