Skip to content

Commit f6ca881

Browse files
committed
Force correct setting of read_secondary based on the read preference (Issue #741)
1 parent 87b1287 commit f6ca881

File tree

3 files changed

+57
-3
lines changed

3 files changed

+57
-3
lines changed

HISTORY

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
1.1.12
22
-----------------
3-
- honor connectTimeoutMS option for replicasets (Issue #750, https://github.com/aheckmann)
3+
- Honor connectTimeoutMS option for replicasets (Issue #750, https://github.com/aheckmann)
44
- Fix ping strategy regression (Issue #738, https://github.com/aheckmann)
55
- Small cleanup of code (Issue #753, https://github.com/sokra/node-mongodb-native)
6-
- fixed index declaration using objects/arrays from other contexts (Issue #755, https://github.com/sokra/node-mongodb-native)
6+
- Fixed index declaration using objects/arrays from other contexts (Issue #755, https://github.com/sokra/node-mongodb-native)
77
- Intermittent (and rare) null callback exception when using ReplicaSets (Issue #752)
8+
- Force correct setting of read_secondary based on the read preference (Issue #741)
89

910
1.1.11 2012-10-10
1011
-----------------

lib/mongodb/connection/repl_set.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,9 @@ var ReplSet = exports.ReplSet = function(servers, options) {
106106
this._readPreference = null;
107107
}
108108

109+
// Ensure read_secondary is set correctly
110+
this.readSecondary = this._readPreference == ReadPreference.PRIMARY ? false : true;
111+
109112
// Strategy for picking a secondary
110113
this.secondaryAcceptableLatencyMS = this.options['secondaryAcceptableLatencyMS'] == null ? 15 : this.options['secondaryAcceptableLatencyMS'];
111114
this.strategy = this.options['strategy'];

test/auxilliary/replicaset_auth_test.js

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ var testCase = require('nodeunit').testCase,
99
Cursor = mongodb.Cursor,
1010
Collection = mongodb.Collection,
1111
Server = mongodb.Server,
12+
ReadPreference = mongodb.ReadPreference,
1213
ReplSetServers = mongodb.ReplSetServers,
1314
ReplicaSetManager = require('../../test/tools/replica_set_manager').ReplicaSetManager,
1415
Step = require("step");
@@ -27,7 +28,7 @@ exports.setUp = function(callback) {
2728
RS = new ReplicaSetManager({retries:120,
2829
auth:true,
2930
arbiter_count:0,
30-
secondary_count:1,
31+
secondary_count:2,
3132
passive_count:0});
3233
RS.startSet(true, function(err, result) {
3334
if(err != null) throw err;
@@ -291,6 +292,55 @@ exports.shouldCorrectlyAuthenticateAndEnsureIndex = function(test) {
291292
});
292293
}
293294

295+
exports.shouldCorrectlyAuthenticateAndUseReadPreference = function(test) {
296+
var replSet = new ReplSetServers( [
297+
new Server( RS.host, RS.ports[0], { auto_reconnect: true } ),
298+
new Server( RS.host, RS.ports[1], { auto_reconnect: true } ),
299+
],
300+
{rs_name:RS.name}
301+
);
302+
303+
var db = new Db(MONGODB, replSet, {safe:false, native_parser: false});
304+
db.open(function(err, db_p) {
305+
test.equal(null, err);
306+
307+
db_p.addUser('test', 'test', function(err, result) {
308+
test.equal(null, err);
309+
310+
db_p.authenticate('test', 'test', function(err, replies) {
311+
test.equal(null, err);
312+
313+
db_p.collection('userconfirm').find({}, {readPreference: ReadPreference.SECONDARY}).toArray(function(err, items) {
314+
console.log("---------------------------------------------------------------")
315+
console.dir(err)
316+
console.dir(items)
317+
318+
db_p.collection('userconfirm').find({}, {readPreference: ReadPreference.SECONDARY}).toArray(function(err, items) {
319+
console.log("---------------------------------------------------------------")
320+
console.dir(err)
321+
console.dir(items)
322+
323+
db_p.collection('userconfirm').find({}, {readPreference: ReadPreference.SECONDARY}).toArray(function(err, items) {
324+
console.log("---------------------------------------------------------------")
325+
console.dir(err)
326+
console.dir(items)
327+
328+
db_p.collection('userconfirm').find({}, {readPreference: ReadPreference.SECONDARY}).toArray(function(err, items) {
329+
console.log("---------------------------------------------------------------")
330+
console.dir(err)
331+
console.dir(items)
332+
test.equal(null, err);
333+
db_p.close();
334+
test.done();
335+
});
336+
});
337+
});
338+
});
339+
});
340+
});
341+
});
342+
}
343+
294344
/**
295345
* Retrieve the server information for the current
296346
* instance of the db client

0 commit comments

Comments
 (0)