diff options
author | David Storch <david.storch@10gen.com> | 2015-09-28 09:51:50 -0400 |
---|---|---|
committer | David Storch <david.storch@10gen.com> | 2015-09-30 13:25:15 -0400 |
commit | df2a33f9974950ebe484edfa29171cec53071b0a (patch) | |
tree | a257aa80208cece12cecee6c4cff9dbc3ec0993f /src/mongo/shell/mongo.js | |
parent | 17a3bd06f4bdb3b7d761ea059b996004a8b6ae7f (diff) | |
download | mongo-df2a33f9974950ebe484edfa29171cec53071b0a.tar.gz |
SERVER-20067 SERVER-19572 set readMode in shell based on wire version
"compatibility" readMode now causes the shell to detect whether or not
to use find/getMore commands based on wire version. "commands" mode
forces use of the find/getMore commands, and "legacy" mode forces uses
of legacy OP_QUERY/OP_GET_MORE reads.
Also configures tests to force use of "commands" or "legacy" mode as
appropriate.
Diffstat (limited to 'src/mongo/shell/mongo.js')
-rw-r--r-- | src/mongo/shell/mongo.js | 54 |
1 files changed, 44 insertions, 10 deletions
diff --git a/src/mongo/shell/mongo.js b/src/mongo/shell/mongo.js index fb6e63583a3..a57a8e1e8dc 100644 --- a/src/mongo/shell/mongo.js +++ b/src/mongo/shell/mongo.js @@ -297,27 +297,61 @@ Mongo.prototype.useReadCommands = function() { } /** - * Get the readMode string (either "commands" for find/getMore commands or "compatibility" for - * OP_QUERY find and OP_GET_MORE). + * For testing, forces the shell to use the readMode specified in 'mode'. Must be either "commands" + * (use the find/getMore commands), "legacy" (use legacy OP_QUERY/OP_GET_MORE wire protocol reads), + * or "compatibility" (auto-detect mode based on wire version). + */ +Mongo.prototype.forceReadMode = function(mode) { + if (mode !== "commands" && mode !== "compatibility" && mode !== "legacy") { + throw new Error("Mode must be one of {commands, compatibility, legacy}, but got: " + mode); + } + + this._readMode = mode; +}; + +/** + * Get the readMode string (either "commands" for find/getMore commands, "legacy" for OP_QUERY find + * and OP_GET_MORE, or "compatibility" for detecting based on wire version). */ Mongo.prototype.readMode = function() { - if ("_readMode" in this) { + if ("_readMode" in this && this._readMode !== "compatibility") { // We already have determined our read mode. Just return it. return this._readMode; } - // Determine read mode based on shell params. - // - // TODO: Detect what to use based on wire protocol version. - if (_readMode) { + // Get the readMode from the shell params. + if (typeof _readMode === "function") { this._readMode = _readMode(); } - else { - this._readMode = "compatibility"; + + // If we're in compatibility mode, determine whether the server supports the find/getMore + // commands. If it does, use commands mode. If not, degrade to legacy mode. + if (this._readMode === "compatibility") { + try { + var isMaster = this.getDB("admin").runCommand({isMaster: 1}); + var hasReadCommands = (isMaster.ok && 'minWireVersion' in isMaster && + 'maxWireVersion' in isMaster && + isMaster.minWireVersion <= 4 && + 4 <= isMaster.maxWireVersion); + + if (hasReadCommands) { + this._readMode = "commands"; + } + else { + print("Cannot use 'commands' readMode, degrading to 'legacy' mode"); + this._readMode = "legacy"; + } + } + catch (e) { + // We failed trying to determine whether the remote node supports the find/getMore + // commands. In this case, we keep _readMode as "compatibility" and the shell should + // issue legacy reads. Next time around we will issue another isMaster to try to + // determine the readMode decisively. + } } return this._readMode; -} +}; // // Write Concern can be set at the connection level, and is used for all write operations unless |