diff options
Diffstat (limited to 'src/mongo')
-rw-r--r-- | src/mongo/client/mongo_uri.cpp | 16 | ||||
-rw-r--r-- | src/mongo/client/mongo_uri.h | 18 | ||||
-rw-r--r-- | src/mongo/scripting/mozjs/internedstring.defs | 1 | ||||
-rw-r--r-- | src/mongo/scripting/mozjs/mongo.cpp | 9 | ||||
-rw-r--r-- | src/mongo/shell/dbshell.cpp | 9 | ||||
-rw-r--r-- | src/mongo/shell/mongo.js | 6 |
6 files changed, 50 insertions, 9 deletions
diff --git a/src/mongo/client/mongo_uri.cpp b/src/mongo/client/mongo_uri.cpp index 820a11da1dc..ed11ee2728f 100644 --- a/src/mongo/client/mongo_uri.cpp +++ b/src/mongo/client/mongo_uri.cpp @@ -436,9 +436,23 @@ MongoURI MongoURI::parseImpl(const std::string& url) { str::stream() << "appname cannot exceed 128 characters: " << optIter->second); } + boost::optional<bool> retryWrites = boost::none; + optIter = options.find("retryWrites"); + if (optIter != end(options)) { + if (optIter->second == "true") { + retryWrites.reset(true); + } else if (optIter->second == "false") { + retryWrites.reset(false); + } else { + uasserted(ErrorCodes::FailedToParse, + str::stream() << "retryWrites must be either \"true\" or \"false\""); + } + } + ConnectionString cs( setName.empty() ? ConnectionString::MASTER : ConnectionString::SET, servers, setName); - return MongoURI(std::move(cs), username, password, database, std::move(options)); + return MongoURI( + std::move(cs), username, password, database, std::move(retryWrites), std::move(options)); } StatusWith<MongoURI> MongoURI::parse(const std::string& url) try { diff --git a/src/mongo/client/mongo_uri.h b/src/mongo/client/mongo_uri.h index b5cf3ecf15c..1c56a4e6579 100644 --- a/src/mongo/client/mongo_uri.h +++ b/src/mongo/client/mongo_uri.h @@ -82,7 +82,7 @@ StatusWith<std::string> uriDecode(StringData str); * 2. The 'fragment' field, as defined by section 3.5 is not permitted. * * For a complete list of URI string options, see - * https://wiki.mongodb.com/display/DH/Connection+String+Format + * https://docs.mongodb.com/manual/reference/connection-string/#connection-string-options * * Examples: * @@ -145,15 +145,24 @@ public: return _connectString.getServers(); } + const boost::optional<std::string> getAppName() const; + boost::optional<bool> getRetryWrites() const { + return _retryWrites; + } + // If you are trying to clone a URI (including its options/auth information) for a single // server (say a member of a replica-set), you can pass in its HostAndPort information to // get a new URI with the same info, except type() will be MASTER and getServers() will // be the single host you pass in. MongoURI cloneURIForServer(HostAndPort hostAndPort) const { - return MongoURI( - ConnectionString(std::move(hostAndPort)), _user, _password, _database, _options); + return MongoURI(ConnectionString(std::move(hostAndPort)), + _user, + _password, + _database, + _retryWrites, + _options); } ConnectionString::ConnectionType type() const { @@ -173,11 +182,13 @@ private: const std::string& user, const std::string& password, const std::string& database, + boost::optional<bool> retryWrites, OptionsMap options) : _connectString(std::move(connectString)), _user(user), _password(password), _database(database), + _retryWrites(std::move(retryWrites)), _options(std::move(options)) {} BSONObj _makeAuthObjFromOptions(int maxWireVersion) const; @@ -188,6 +199,7 @@ private: std::string _user; std::string _password; std::string _database; + boost::optional<bool> _retryWrites; OptionsMap _options; }; diff --git a/src/mongo/scripting/mozjs/internedstring.defs b/src/mongo/scripting/mozjs/internedstring.defs index 3735c576b97..89900e5c35c 100644 --- a/src/mongo/scripting/mozjs/internedstring.defs +++ b/src/mongo/scripting/mozjs/internedstring.defs @@ -49,6 +49,7 @@ MONGO_MOZJS_INTERNED_STRING(prototype, "prototype") MONGO_MOZJS_INTERNED_STRING(_query, "_query") MONGO_MOZJS_INTERNED_STRING(readOnly, "readOnly") MONGO_MOZJS_INTERNED_STRING(reason, "reason") +MONGO_MOZJS_INTERNED_STRING(_retryWrites, "_retryWrites") MONGO_MOZJS_INTERNED_STRING(_ro, "_ro") MONGO_MOZJS_INTERNED_STRING(scope, "scope") MONGO_MOZJS_INTERNED_STRING(servers, "servers") diff --git a/src/mongo/scripting/mozjs/mongo.cpp b/src/mongo/scripting/mozjs/mongo.cpp index c28f9e124eb..47bb122cb7f 100644 --- a/src/mongo/scripting/mozjs/mongo.cpp +++ b/src/mongo/scripting/mozjs/mongo.cpp @@ -720,6 +720,15 @@ void MongoExternalInfo::construct(JSContext* cx, JS::CallArgs args) { auto defaultDB = cs.getDatabase() == "" ? "test" : cs.getDatabase(); o.setString(InternedString::defaultDB, defaultDB); + // Adds a property to the Mongo connection object. + boost::optional<bool> retryWrites = cs.getRetryWrites(); + // If retryWrites is not explicitly set in uri, sessions created on this connection default to + // the global retryWrites value. This is checked in sessions.js by using the injected + // _shouldRetryWrites() function, which returns true if the --retryWrites flag was passed. + if (retryWrites) { + o.setBoolean(InternedString::_retryWrites, retryWrites.get()); + } + args.rval().setObjectOrNull(thisv); } diff --git a/src/mongo/shell/dbshell.cpp b/src/mongo/shell/dbshell.cpp index 4dfb85d835a..83e07975a68 100644 --- a/src/mongo/shell/dbshell.cpp +++ b/src/mongo/shell/dbshell.cpp @@ -784,10 +784,11 @@ int _main(int argc, char* argv[], char** envp) { << getURIFromArgs(processedURI, shellGlobalParams.dbhost, shellGlobalParams.port) << "\");"; - if (shellGlobalParams.shouldRetryWrites) { - // If the user specified --retryWrites to the mongo shell, then we replace the global - // `db` object with a DB object that has retryable writes enabled. - ss << "db = db.getMongo().startSession({retryWrites: true}).getDatabase(db.getName());"; + if (shellGlobalParams.shouldRetryWrites || parsedURI.getRetryWrites()) { + // If the --retryWrites cmdline argument or retryWrites URI param was specified, then + // replace the global `db` object with a DB object started in a session. The resulting + // Mongo connection checks its _retryWrites property. + ss << "db = db.getMongo().startSession().getDatabase(db.getName());"; } mongo::shell_utils::_dbConnect = ss.str(); diff --git a/src/mongo/shell/mongo.js b/src/mongo/shell/mongo.js index 2a477659eb8..e789e284795 100644 --- a/src/mongo/shell/mongo.js +++ b/src/mongo/shell/mongo.js @@ -417,7 +417,11 @@ Mongo.prototype.getClusterTime = function() { return this._clusterTime; }; -Mongo.prototype.startSession = function startSession(options) { +Mongo.prototype.startSession = function startSession(options = {}) { + // Set retryWrites if not already set on options. + if (!options.hasOwnProperty("retryWrites") && this.hasOwnProperty("_retryWrites")) { + options.retryWrites = this._retryWrites; + } return new DriverSession(this, options); }; |