Skip to content

Commit

Permalink
Beeter error handling for replicaset when wrong replicaset name provided
Browse files Browse the repository at this point in the history
  • Loading branch information
christkv committed Jun 29, 2011
1 parent 8479366 commit 4664ee1
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 12 deletions.
5 changes: 5 additions & 0 deletions HISTORY
@@ -1,3 +1,8 @@

* Fixes for reconnect logic for server object (replays auth correctly)
* More testcases for auth
* Fixes in error handling for replicaset

0.9.6-1 2011-06-25
* Fixes for test to run properly using c++ bson parser
* Fixes for dbref in native parser (correctly handles ref without db component)
Expand Down
1 change: 0 additions & 1 deletion examples/replSetServersSimple.js
Expand Up @@ -8,7 +8,6 @@ var Db = require('../lib/mongodb').Db,
DbCommand = require('../lib/mongodb/commands/db_command').DbCommand,
Connection = require('../lib/mongodb').Connection,
Server = require('../lib/mongodb').Server,
ServerCluster = require('../lib/mongodb').ServerCluster,
// BSON = require('../lib/mongodb').BSONPure;
ReplSetServers = require('../lib/mongodb').ReplSetServers,
CheckMaster = require('../lib/mongodb').CheckMaster,
Expand Down
11 changes: 8 additions & 3 deletions lib/mongodb/connections/repl_set_servers.js
Expand Up @@ -132,6 +132,7 @@ ReplSetServers.prototype.connect = function(parent, callback) {
server.connection.on("connect", function() {
// Create a callback function for a given connection
var connectCallback = function(err, reply) {
if(replSetSelf.otherErrors.length > 0) return;
// Update number of connected servers, ensure we update before any possible errors
numberOfConnectedServers = numberOfConnectedServers + 1;

Expand Down Expand Up @@ -174,11 +175,15 @@ ReplSetServers.prototype.connect = function(parent, callback) {

if(replSetSelf.replicaSet != node["setName"]) {
// Add other error to the list of errors
replSetSelf.otherErrors.push(new Error("configured mongodb replicaset does not match provided replicaset [" + node["setName"] + "] != [" + replSetSelf.replicaSet + "]"));
var errorMessage = new Error("configured mongodb replicaset does not match provided replicaset [" + node["setName"] + "] != [" + replSetSelf.replicaSet + "]");
replSetSelf.otherErrors.push(errorMessage);
// Close all servers and return an error
for(var i = 0; i < serverConnections.length; i++) {
serverConnections[i].close();
}

// Return the error message
return callback(errorMessage);
}
}

Expand All @@ -203,7 +208,7 @@ ReplSetServers.prototype.connect = function(parent, callback) {
}

// emit a message saying we got a master and are ready to go and change state to reflect it
if(numberOfConnectedServers == serverConnections.length && (parent.state == 'notConnected')) {
if(numberOfConnectedServers >= serverConnections.length && (parent.state == 'notConnected')) {
parent.isInitializing = false;
// If we have no master connection
if(replSetSelf.otherErrors.length > 0) {
Expand All @@ -224,7 +229,7 @@ ReplSetServers.prototype.connect = function(parent, callback) {

// We had some errored out servers, does not matter as long as we have a master server
// we can write to.
if ((numberOfConnectedServers + numberOfErrorServers) == serverConnections.length) {
if((numberOfConnectedServers + numberOfErrorServers) >= serverConnections.length) {
parent.isInitializing = false;
// If we have no master connection
if(replSetSelf.otherErrors.length > 0) {
Expand Down
12 changes: 6 additions & 6 deletions test/replicaset/connect_test.js
Expand Up @@ -68,7 +68,7 @@ module.exports = testCase({
],
{rs_name:RS.name + "-wrong"}
);

var db = new Db('integration_test_', replSet);
db.open(function(err, p_db) {
test.notEqual(null, err);
Expand Down Expand Up @@ -96,7 +96,7 @@ module.exports = testCase({
// debug("=========================================== shouldConnectWithThirdNodeKilled")
RS.getNodeFromPort(RS.ports[2], function(err, node) {
if(err != null) debug("shouldConnectWithThirdNodeKilled :: " + inspect(err));

RS.kill(node, function(err, result) {
if(err != null) debug("shouldConnectWithThirdNodeKilled :: " + inspect(err));
// Replica configuration
Expand Down Expand Up @@ -212,14 +212,14 @@ module.exports = testCase({
// Test primary
RS.primary(function(err, primary) {
if(err != null) debug("shouldCorrectlyConnect :: " + inspect(err));

test.notEqual(null, primary);
test.equal(primary, p_db.serverConfig.primary.host + ":" + p_db.serverConfig.primary.port);

// Perform tests
RS.secondaries(function(err, items) {
if(err != null) debug("shouldCorrectlyConnect :: " + inspect(err));

// Test if we have the right secondaries
test.deepEqual(items.sort(), p_db.serverConfig.secondaries.map(function(item) {
return item.host + ":" + item.port;
Expand All @@ -228,7 +228,7 @@ module.exports = testCase({
// Test if we have the right arbiters
RS.arbiters(function(err, items) {
if(err != null) debug("shouldCorrectlyConnect :: " + inspect(err));

test.deepEqual(items.sort(), p_db.serverConfig.arbiters.map(function(item) {
return item.host + ":" + item.port;
}).sort());
Expand All @@ -237,7 +237,7 @@ module.exports = testCase({
var db2 = new Db('integration_test_', replSet );
db2.open(function(err, p_db2) {
if(err != null) debug("shouldCorrectlyConnect :: " + inspect(err));

test.equal(true, p_db2.serverConfig.isConnected());

// Close top instance
Expand Down
4 changes: 2 additions & 2 deletions tools/test_all.js
Expand Up @@ -72,7 +72,7 @@ exec('rm -rf ./output', function(err, stdout, stderr) {
Step(
// Start the single server
function startSingleServer() {
serverManager.start(true, this);
serverManager.start(true, {purgedirectories:true}, this);
},
// Run all the integration tests using the pure js bson parser
function runPureJS() {
Expand Down Expand Up @@ -101,7 +101,7 @@ exec('rm -rf ./output', function(err, stdout, stderr) {
// Execute without replicaset tests
Step(
function startSingleServer() {
serverManager.start(true, this);
serverManager.start(true, {purgedirectories:true}, this);
},
function runPureJS() {
options.suffix = 'pure';
Expand Down

0 comments on commit 4664ee1

Please sign in to comment.