Skip to content

Commit 0d877ee

Browse files
committed
NODE-909 Ensure ensureIndex read is performed against PRIMARY server
1 parent 228bf37 commit 0d877ee

File tree

3 files changed

+75
-5
lines changed

3 files changed

+75
-5
lines changed

lib/db.js

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,8 +1032,6 @@ Db.prototype.createIndex = function(name, fieldOrSpec, options, callback) {
10321032
options = options == null ? {} : options;
10331033
// Shallow clone the options
10341034
options = shallowClone(options);
1035-
// Run only against primary
1036-
options.readPreference = ReadPreference.PRIMARY;
10371035

10381036
// If we have a callback fallback
10391037
if(typeof callback == 'function') return createIndex(self, name, fieldOrSpec, options, callback);
@@ -1048,12 +1046,15 @@ Db.prototype.createIndex = function(name, fieldOrSpec, options, callback) {
10481046

10491047
var createIndex = function(self, name, fieldOrSpec, options, callback) {
10501048
// Get the write concern options
1051-
var finalOptions = writeConcern({}, self, options);
1049+
var finalOptions = writeConcern({}, self, options, { readPreference: ReadPreference.PRIMARY });
10521050
// Ensure we have a callback
10531051
if(finalOptions.writeConcern && typeof callback != 'function') {
10541052
throw MongoError.create({message: "Cannot use a writeConcern without a provided callback", driver:true});
10551053
}
10561054

1055+
// Run only against primary
1056+
options.readPreference = ReadPreference.PRIMARY;
1057+
10571058
// Did the user destroy the topology
10581059
if(self.serverConfig && self.serverConfig.isDestroyed()) return callback(new MongoError('topology was destroyed'));
10591060

@@ -1136,6 +1137,9 @@ var ensureIndex = function(self, name, fieldOrSpec, options, callback) {
11361137
// Did the user destroy the topology
11371138
if(self.serverConfig && self.serverConfig.isDestroyed()) return callback(new MongoError('topology was destroyed'));
11381139

1140+
// Merge primary readPreference
1141+
finalOptions.readPreference = ReadPreference.PRIMARY
1142+
11391143
// Check if the index allready exists
11401144
self.indexInformation(name, finalOptions, function(err, indexInformation) {
11411145
if(err != null && err.code != 26) return handleCallback(callback, err, null);
@@ -1645,7 +1649,6 @@ var indexInformation = function(self, name, options, callback) {
16451649

16461650
// Did the user destroy the topology
16471651
if(self.serverConfig && self.serverConfig.isDestroyed()) return callback(new MongoError('topology was destroyed'));
1648-
16491652
// Process all the results from the index command and collection
16501653
var processResults = function(indexes) {
16511654
// Contains all the information
@@ -1664,7 +1667,7 @@ var indexInformation = function(self, name, options, callback) {
16641667
}
16651668

16661669
// Get the list of indexes of the specified collection
1667-
self.collection(name).listIndexes().toArray(function(err, indexes) {
1670+
self.collection(name).listIndexes(options).toArray(function(err, indexes) {
16681671
if(err) return callback(toError(err));
16691672
if(!Array.isArray(indexes)) return handleCallback(callback, null, []);
16701673
if(full) return handleCallback(callback, null, indexes);

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"dependencies": {
1616
"es6-promise": "3.2.1",
1717
"mongodb-core": "2.1.6",
18+
"mongodb-core": "christkv/mongodb-core#2.0",
1819
"readable-stream": "2.1.5"
1920
},
2021
"devDependencies": {

test/functional/authentication_tests.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,72 @@ exports['Should correctly perform nearest read from secondaries without auth fai
817817
}
818818
}
819819

820+
/**
821+
* @ignore
822+
*/
823+
exports['Should correctly create indexes without hanging when different seedlists'] = {
824+
metadata: { requires: { topology: ['auth'] } },
825+
826+
// The actual test we wish to run
827+
test: function(configuration, test) {
828+
var Db = configuration.require.Db
829+
, Server = configuration.require.Server
830+
, ReadPreference = configuration.require.ReadPreference
831+
, MongoClient = configuration.require.MongoClient
832+
, ReplSet = configuration.require.ReplSet;
833+
834+
setUp(configuration, function(err, replicasetManager) {
835+
var replSet = new ReplSet( [
836+
new Server( 'localhost', 31000),
837+
new Server( 'localhost', 31001)
838+
],
839+
{rs_name: 'rs', poolSize:1}
840+
);
841+
842+
// Connect
843+
new Db('replicaset_test_auth', replSet, {
844+
w: 1, readPreference: ReadPreference.NEAREST
845+
}).open(function(err, db) {
846+
// Add a user
847+
db.admin().addUser("root", "root", {w:3, wtimeout: 25000}, function(err, result) {
848+
test.equal(null, err);
849+
850+
// process.exit(0)
851+
db.close();
852+
853+
MongoClient.connect('mongodb://root:root@localhost:31000,localhost:31001,localhost:31002/admin?replicaSet=rs&readPreference=secondary', function(err, db) {
854+
test.equal(null, err);
855+
856+
// Attempt create index
857+
db.db('replicaset_test_auth')
858+
.collection('createIndexes1')
859+
.ensureIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 }, function(err, r) {
860+
test.equal(null, err);
861+
db.close();
862+
863+
MongoClient.connect('mongodb://root:root@localhost:31002/admin?replicaSet=rs&readPreference=secondary', function(err, db) {
864+
test.equal(null, err);
865+
866+
db.db('replicaset_test_auth')
867+
.collection('createIndexes2')
868+
.ensureIndex({ expiresAt: 1 }, { expireAfterSeconds: 0 }, function(err, r) {
869+
console.dir(err)
870+
test.equal(null, err);
871+
db.close();
872+
873+
replicasetManager.stop().then(function() {
874+
test.done();
875+
});
876+
});
877+
});
878+
});
879+
});
880+
});
881+
});
882+
})
883+
}
884+
}
885+
820886
/**
821887
* @ignore
822888
*/

0 commit comments

Comments
 (0)