Skip to content

Commit cc7ecd8

Browse files
committed
NOT providing replSet name in MongoClient connection URI will force single server connection. Fixes issue where it was impossible to directly connect to a replicaset member server.
1 parent abad397 commit cc7ecd8

File tree

7 files changed

+69
-12
lines changed

7 files changed

+69
-12
lines changed

HISTORY.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
-----------------
33
* Propagate timeout event correctly to db instances.
44
* Application Monitoring API (APM) implemented.
5+
* NOT providing replSet name in MongoClient connection URI will force single server connection. Fixes issue where it was impossible to directly connect to a replicaset member server.
56

67
2.0.42 08-18-2015
78
-----------------

lib/db.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -659,7 +659,6 @@ var evaluate = function(self, code, parameters, options, callback) {
659659
* @param {boolean} [options.nolock=false] Tell MongoDB not to block on the evaulation of the javascript.
660660
* @param {Db~resultCallback} [callback] The results callback
661661
* @return {Promise} returns Promise if no callback passed
662-
* @deprecated MongoDB 3.2 and higher no longer support eval.
663662
*/
664663
Db.prototype.eval = function(code, parameters, options, callback) {
665664
var self = this;

lib/mongo_client.js

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -240,8 +240,6 @@ var connect = function(url, options, callback) {
240240
? new Server(object.servers[i].domain_socket, _server_options)
241241
: new Server(object.servers[i].host, object.servers[i].port, _server_options);
242242

243-
var setName;
244-
245243
var connectFunction = function(__server) {
246244
// Attempt connect
247245
new Db(object.dbName, __server, {w:1, native_parser:false}).open(function(err, db) {
@@ -257,7 +255,6 @@ var connect = function(url, options, callback) {
257255
// Check what type of server we have
258256
if(isMasterDoc.setName) {
259257
totalNumberOfMongodServers++;
260-
setName = isMasterDoc.setName;
261258
}
262259

263260
if(isMasterDoc.msg && isMasterDoc.msg == "isdbgrid") totalNumberOfMongosServers++;
@@ -288,13 +285,16 @@ var connect = function(url, options, callback) {
288285
&& totalNumberOfMongosServers == 0
289286
&& object.servers.length == 1
290287
&& (!object.rs_options.replicaSet || !object.rs_options.rs_name)) {
288+
291289
var obj = object.servers[0];
292290
serverConfig = obj.domain_socket ?
293291
new Server(obj.domain_socket, object.server_options)
294292
: new Server(obj.host, obj.port, object.server_options);
293+
295294
} else if(totalNumberOfMongodServers > 0
296295
|| totalNumberOfMongosServers > 0
297296
|| object.rs_options.replicaSet || object.rs_options.rs_name) {
297+
298298
var finalServers = object.servers
299299
.filter(function(serverObj) {
300300
return errorServers[serverObj.host + ":" + serverObj.port] == null;
@@ -309,10 +309,19 @@ var connect = function(url, options, callback) {
309309
// Set up the final configuration
310310
if(totalNumberOfMongodServers > 0) {
311311
try {
312-
if (totalNumberOfMongodServers == 1) {
313-
object.rs_options.replicaSet = object.rs_options.replicaSet || setName;
312+
313+
// If no replicaset name was provided, we wish to perform a
314+
// direct connection
315+
if(totalNumberOfMongodServers == 1
316+
&& (!object.rs_options.replicaSet || !object.rs_options.rs_name)) {
317+
serverConfig = finalServers[0];
318+
} else if(totalNumberOfMongodServers == 1) {
319+
object.rs_options.replicaSet = object.rs_options.replicaSet || object.rs_options.rs_name;
320+
serverConfig = new ReplSet(finalServers, object.rs_options);
321+
} else {
322+
serverConfig = new ReplSet(finalServers, object.rs_options);
314323
}
315-
serverConfig = new ReplSet(finalServers, object.rs_options);
324+
316325
} catch(err) {
317326
return callback(err, null);
318327
}

lib/server.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -278,6 +278,7 @@ Server.prototype.connect = function(db, _options, callback) {
278278
try {
279279
callback(null, self);
280280
} catch(err) {
281+
console.log(err.stack)
281282
process.nextTick(function() { throw err; })
282283
}
283284
}

test/functional/logger_tests.js

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,3 +76,47 @@ exports['Should not fail with undefined id'] = {
7676
});
7777
}
7878
}
79+
80+
/**
81+
* Should No fail with undefined id
82+
* @ignore
83+
*/
84+
exports['Should correctly log cursor'] = {
85+
metadata: { requires: { topology: ['single'] } },
86+
87+
// The actual test we wish to run
88+
test: function(configuration, test) {
89+
var MongoClient = configuration.require.MongoClient
90+
, Logger = configuration.require.Logger;
91+
92+
MongoClient.connect('mongodb://localhost:27017/test', {}, function(err, db) {
93+
test.equal(null, err);
94+
95+
// Status
96+
var logged = false;
97+
98+
// Set the current logger
99+
Logger.setCurrentLogger(function() {
100+
test.ok(msg != null);
101+
test.equal('debug', context.type);
102+
test.equal('Db', context.className);
103+
logged = true;
104+
});
105+
106+
// Set the filter
107+
Logger.setLevel('debug');
108+
Logger.filter('class', ['Cursor']);
109+
110+
// perform any operation that gets logged
111+
db.collection('logging').find().toArray(function(err, d) {
112+
test.equal(null, err);
113+
test.ok(logged);
114+
115+
// Clean up
116+
Logger.reset();
117+
db.close();
118+
test.done();
119+
});
120+
});
121+
}
122+
}

test/functional/replset_connection_tests.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -949,20 +949,22 @@ exports['Should Correctly remove server going into recovery mode'] = {
949949
/**
950950
* @ignore
951951
*/
952-
exports['Should not give an error when using a single server seed and no setName'] = {
952+
exports['Should return single server direct connection when replicaSet not provided'] = {
953953
metadata: { requires: { topology: 'replicaset' } },
954954

955955
// The actual test we wish to run
956956
test: function(configuration, test) {
957957
var mongo = configuration.require
958-
, MongoClient = mongo.MongoClient;
958+
, MongoClient = mongo.MongoClient
959+
, Server = mongo.Server;
959960

960961
var url = f("mongodb://localhost:%s/%s"
961962
, configuration.port
962963
, "integration_test_");
963964

964965
MongoClient.connect(url, function(err, db) {
965966
test.equal(null, err);
967+
test.ok(db.serverConfig instanceof Server);
966968

967969
restartAndDone(configuration, test);
968970
});

test/functional/replset_operations_tests.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ exports['Should fail due to w:5 and wtimeout:1 with ordered batch api'] = {
3737
// Create url
3838
var url = format("mongodb://%s,%s/%s?replicaSet=%s&readPreference=%s"
3939
, format("%s:%s", configuration.host, configuration.port)
40-
, format("%s:%s", configuration.host, configuration.host + 1)
40+
, format("%s:%s", configuration.host, configuration.port + 1)
4141
, "integration_test_"
4242
, configuration.replicasetName
4343
, "primary");
@@ -83,6 +83,7 @@ exports['Should fail due to w:5 and wtimeout:1 with ordered batch api'] = {
8383
}
8484

8585
MongoClient.connect(url, function(err, db) {
86+
console.dir(err)
8687
test.equal(null, err);
8788

8889
executeTests(db, function() {
@@ -357,7 +358,7 @@ exports['Should Correctly group using replicaset'] = {
357358
// Create url
358359
var url = format("mongodb://%s,%s/%s?replicaSet=%s&readPreference=%s"
359360
, format("%s:%s", configuration.host, configuration.port)
360-
, format("%s:%s", configuration.host, configuration.host + 1)
361+
, format("%s:%s", configuration.host, configuration.port + 1)
361362
, "integration_test_"
362363
, configuration.replicasetName
363364
, "primary");
@@ -402,7 +403,7 @@ exports['Should fail to do map reduce to out collection'] = {
402403
// Create url
403404
var url = format("mongodb://%s,%s/%s?replicaSet=%s&readPreference=%s"
404405
, format("%s:%s", configuration.host, configuration.port)
405-
, format("%s:%s", configuration.host, configuration.host + 1)
406+
, format("%s:%s", configuration.host, configuration.port + 1)
406407
, "integration_test_"
407408
, configuration.replicasetName
408409
, "primary");

0 commit comments

Comments
 (0)