Skip to content

Commit 284d80b

Browse files
committed
SERVER-30850 Handle replica set connection strings in startParallelShell()
1 parent 592208b commit 284d80b

File tree

4 files changed

+55
-8
lines changed

4 files changed

+55
-8
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Test startParallelShell() in a replica set.
2+
3+
var db;
4+
5+
(function() {
6+
'use strict';
7+
8+
const setName = 'rs0';
9+
const replSet = new ReplSetTest({name: setName, nodes: 3});
10+
const nodes = replSet.nodeList();
11+
replSet.startSet();
12+
replSet.initiate();
13+
14+
const url = replSet.getURL();
15+
print("* Connecting to " + url);
16+
const mongo = new Mongo(url);
17+
db = mongo.getDB('admin');
18+
assert.eq(url, mongo.host, "replSet.getURL() should match active connection string");
19+
20+
print("* Starting parallel shell on --host " + db.getMongo().host);
21+
startParallelShell('db.coll0.insert({test: "connString only"});');
22+
assert.soon(function() {
23+
return db.coll0.find({test: "connString only"}).count() === 1;
24+
});
25+
26+
const uri = new MongoURI(url);
27+
const port0 = uri.servers[0].port;
28+
print("* Starting parallel shell w/ --port " + port0);
29+
startParallelShell('db.coll0.insert({test: "explicit port"});', port0);
30+
assert.soon(function() {
31+
return db.coll0.find({test: "explicit port"}).count() === 1;
32+
});
33+
})();

src/mongo/shell/replsettest.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -439,8 +439,10 @@ var ReplSetTest = function(opts) {
439439
var member = {};
440440
member._id = i;
441441

442-
var port = this.ports[i];
443-
member.host = this.host + ":" + port;
442+
member.host = this.host;
443+
if (!member.host.contains('/')) {
444+
member.host += ":" + this.ports[i];
445+
}
444446

445447
var nodeOpts = this.nodeOptions["n" + i];
446448
if (nodeOpts) {

src/mongo/shell/servers_misc.js

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -214,11 +214,21 @@ function startParallelShell(jsCode, port, noConnect) {
214214
var args = [shellPath];
215215

216216
if (typeof db == "object") {
217-
var hostAndPort = db.getMongo().host.split(':');
218-
var host = hostAndPort[0];
219-
args.push("--host", host);
220-
if (!port && hostAndPort.length >= 2) {
221-
var port = hostAndPort[1];
217+
if (!port) {
218+
// If no port override specified, just passthrough connect string.
219+
args.push("--host", db.getMongo().host);
220+
} else {
221+
// Strip port numbers from connect string.
222+
const uri = new MongoURI(db.getMongo().host);
223+
var connString = uri.servers
224+
.map(function(server) {
225+
return server.host;
226+
})
227+
.join(',');
228+
if (uri.setName.length > 0) {
229+
connString = uri.setName + '/' + connString;
230+
}
231+
args.push("--host", connString);
222232
}
223233
}
224234
if (port) {

src/mongo/util/net/hostandport.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ void HostAndPort::append(StringBuilder& ss) const {
116116
} else {
117117
ss << host();
118118
}
119-
ss << ':' << port();
119+
if (host().find('/') == std::string::npos) {
120+
ss << ':' << port();
121+
}
120122
}
121123

122124
bool HostAndPort::empty() const {

0 commit comments

Comments
 (0)