diff options
author | Sara Golemon <sara.golemon@mongodb.com> | 2018-02-14 17:27:55 -0500 |
---|---|---|
committer | Max Hirschhorn <max.hirschhorn@mongodb.com> | 2018-02-14 17:27:55 -0500 |
commit | f045ad4878ad035b0f66013367a2d7eb466bd95f (patch) | |
tree | ca162d2df7632b5d5d70bcd82c76bfc54f52ee91 | |
parent | d8ba3098490e517fc8df166c43bc18d45a279dec (diff) | |
download | mongo-f045ad4878ad035b0f66013367a2d7eb466bd95f.tar.gz |
SERVER-30850 Handle replica set connection strings in startParallelShell()
(cherry picked from commit 284d80ba6ee928cdf3d8f23c161a7ee1bf4a8faf)
-rw-r--r-- | jstests/replsets/startParallelShell.js | 33 | ||||
-rw-r--r-- | src/mongo/shell/replsettest.js | 6 | ||||
-rw-r--r-- | src/mongo/shell/servers_misc.js | 20 | ||||
-rw-r--r-- | src/mongo/util/net/hostandport.cpp | 4 |
4 files changed, 55 insertions, 8 deletions
diff --git a/jstests/replsets/startParallelShell.js b/jstests/replsets/startParallelShell.js new file mode 100644 index 00000000000..beca88d19a9 --- /dev/null +++ b/jstests/replsets/startParallelShell.js @@ -0,0 +1,33 @@ +// Test startParallelShell() in a replica set. + +var db; + +(function() { + 'use strict'; + + const setName = 'rs0'; + const replSet = new ReplSetTest({name: setName, nodes: 3}); + const nodes = replSet.nodeList(); + replSet.startSet(); + replSet.initiate(); + + const url = replSet.getURL(); + print("* Connecting to " + url); + const mongo = new Mongo(url); + db = mongo.getDB('admin'); + assert.eq(url, mongo.host, "replSet.getURL() should match active connection string"); + + print("* Starting parallel shell on --host " + db.getMongo().host); + startParallelShell('db.coll0.insert({test: "connString only"});'); + assert.soon(function() { + return db.coll0.find({test: "connString only"}).count() === 1; + }); + + const uri = new MongoURI(url); + const port0 = uri.servers[0].port; + print("* Starting parallel shell w/ --port " + port0); + startParallelShell('db.coll0.insert({test: "explicit port"});', port0); + assert.soon(function() { + return db.coll0.find({test: "explicit port"}).count() === 1; + }); +})(); diff --git a/src/mongo/shell/replsettest.js b/src/mongo/shell/replsettest.js index af7630b8065..8557900e4ad 100644 --- a/src/mongo/shell/replsettest.js +++ b/src/mongo/shell/replsettest.js @@ -427,8 +427,10 @@ var ReplSetTest = function(opts) { var member = {}; member._id = i; - var port = this.ports[i]; - member.host = this.host + ":" + port; + member.host = this.host; + if (!member.host.contains('/')) { + member.host += ":" + this.ports[i]; + } var nodeOpts = this.nodeOptions["n" + i]; if (nodeOpts) { diff --git a/src/mongo/shell/servers_misc.js b/src/mongo/shell/servers_misc.js index fdb49deb76a..f419428e2d0 100644 --- a/src/mongo/shell/servers_misc.js +++ b/src/mongo/shell/servers_misc.js @@ -214,11 +214,21 @@ function startParallelShell(jsCode, port, noConnect) { var args = ["mongo"]; if (typeof db == "object") { - var hostAndPort = db.getMongo().host.split(':'); - var host = hostAndPort[0]; - args.push("--host", host); - if (!port && hostAndPort.length >= 2) { - var port = hostAndPort[1]; + if (!port) { + // If no port override specified, just passthrough connect string. + args.push("--host", db.getMongo().host); + } else { + // Strip port numbers from connect string. + const uri = new MongoURI(db.getMongo().host); + var connString = uri.servers + .map(function(server) { + return server.host; + }) + .join(','); + if (uri.setName.length > 0) { + connString = uri.setName + '/' + connString; + } + args.push("--host", connString); } } if (port) { diff --git a/src/mongo/util/net/hostandport.cpp b/src/mongo/util/net/hostandport.cpp index abd8f2d6ade..4b46938fbdb 100644 --- a/src/mongo/util/net/hostandport.cpp +++ b/src/mongo/util/net/hostandport.cpp @@ -95,7 +95,9 @@ void HostAndPort::append(StringBuilder& ss) const { } else { ss << host(); } - ss << ':' << port(); + if (host().find('/') == std::string::npos) { + ss << ':' << port(); + } } bool HostAndPort::empty() const { |