From f6ca8818df48fa4aed7fbb12694ffad470054772 Mon Sep 17 00:00:00 2001 From: Christian Kvalheim Date: Mon, 29 Oct 2012 17:39:14 +0100 Subject: [PATCH] Force correct setting of read_secondary based on the read preference (Issue #741) --- HISTORY | 5 ++- lib/mongodb/connection/repl_set.js | 3 ++ test/auxilliary/replicaset_auth_test.js | 52 ++++++++++++++++++++++++- 3 files changed, 57 insertions(+), 3 deletions(-) diff --git a/HISTORY b/HISTORY index 5faf56714dd..4cf05798fb3 100644 --- a/HISTORY +++ b/HISTORY @@ -1,10 +1,11 @@ 1.1.12 ----------------- -- honor connectTimeoutMS option for replicasets (Issue #750, https://github.com/aheckmann) +- Honor connectTimeoutMS option for replicasets (Issue #750, https://github.com/aheckmann) - Fix ping strategy regression (Issue #738, https://github.com/aheckmann) - Small cleanup of code (Issue #753, https://github.com/sokra/node-mongodb-native) -- fixed index declaration using objects/arrays from other contexts (Issue #755, https://github.com/sokra/node-mongodb-native) +- Fixed index declaration using objects/arrays from other contexts (Issue #755, https://github.com/sokra/node-mongodb-native) - Intermittent (and rare) null callback exception when using ReplicaSets (Issue #752) +- Force correct setting of read_secondary based on the read preference (Issue #741) 1.1.11 2012-10-10 ----------------- diff --git a/lib/mongodb/connection/repl_set.js b/lib/mongodb/connection/repl_set.js index ef714ca67a5..5684e1a36b8 100644 --- a/lib/mongodb/connection/repl_set.js +++ b/lib/mongodb/connection/repl_set.js @@ -106,6 +106,9 @@ var ReplSet = exports.ReplSet = function(servers, options) { this._readPreference = null; } + // Ensure read_secondary is set correctly + this.readSecondary = this._readPreference == ReadPreference.PRIMARY ? false : true; + // Strategy for picking a secondary this.secondaryAcceptableLatencyMS = this.options['secondaryAcceptableLatencyMS'] == null ? 15 : this.options['secondaryAcceptableLatencyMS']; this.strategy = this.options['strategy']; diff --git a/test/auxilliary/replicaset_auth_test.js b/test/auxilliary/replicaset_auth_test.js index 87640ab1c9a..6237a400de1 100644 --- a/test/auxilliary/replicaset_auth_test.js +++ b/test/auxilliary/replicaset_auth_test.js @@ -9,6 +9,7 @@ var testCase = require('nodeunit').testCase, Cursor = mongodb.Cursor, Collection = mongodb.Collection, Server = mongodb.Server, + ReadPreference = mongodb.ReadPreference, ReplSetServers = mongodb.ReplSetServers, ReplicaSetManager = require('../../test/tools/replica_set_manager').ReplicaSetManager, Step = require("step"); @@ -27,7 +28,7 @@ exports.setUp = function(callback) { RS = new ReplicaSetManager({retries:120, auth:true, arbiter_count:0, - secondary_count:1, + secondary_count:2, passive_count:0}); RS.startSet(true, function(err, result) { if(err != null) throw err; @@ -291,6 +292,55 @@ exports.shouldCorrectlyAuthenticateAndEnsureIndex = function(test) { }); } +exports.shouldCorrectlyAuthenticateAndUseReadPreference = function(test) { + var replSet = new ReplSetServers( [ + new Server( RS.host, RS.ports[0], { auto_reconnect: true } ), + new Server( RS.host, RS.ports[1], { auto_reconnect: true } ), + ], + {rs_name:RS.name} + ); + + var db = new Db(MONGODB, replSet, {safe:false, native_parser: false}); + db.open(function(err, db_p) { + test.equal(null, err); + + db_p.addUser('test', 'test', function(err, result) { + test.equal(null, err); + + db_p.authenticate('test', 'test', function(err, replies) { + test.equal(null, err); + + db_p.collection('userconfirm').find({}, {readPreference: ReadPreference.SECONDARY}).toArray(function(err, items) { + console.log("---------------------------------------------------------------") + console.dir(err) + console.dir(items) + + db_p.collection('userconfirm').find({}, {readPreference: ReadPreference.SECONDARY}).toArray(function(err, items) { + console.log("---------------------------------------------------------------") + console.dir(err) + console.dir(items) + + db_p.collection('userconfirm').find({}, {readPreference: ReadPreference.SECONDARY}).toArray(function(err, items) { + console.log("---------------------------------------------------------------") + console.dir(err) + console.dir(items) + + db_p.collection('userconfirm').find({}, {readPreference: ReadPreference.SECONDARY}).toArray(function(err, items) { + console.log("---------------------------------------------------------------") + console.dir(err) + console.dir(items) + test.equal(null, err); + db_p.close(); + test.done(); + }); + }); + }); + }); + }); + }); + }); +} + /** * Retrieve the server information for the current * instance of the db client