Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 13 additions & 13 deletions lib/mongodb/connection/repl_set/strategies/ping_strategy.js
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ PingStrategy.prototype.checkoutConnection = function(tags, secondaryCandidates)

// Sort by ping time
finalCandidates.sort(function(a, b) {
return a.runtimeStats['pingMs'] > b.runtimeStats['pingMs'];
return a.runtimeStats['pingMs'] - b.runtimeStats['pingMs'];
});

if(0 === finalCandidates.length)
Expand All @@ -129,7 +129,7 @@ PingStrategy.prototype.checkoutConnection = function(tags, secondaryCandidates)

// convert to integer
var lowestPing = lowest.runtimeStats.pingMs | 0;

// determine acceptable latency
var acceptable = lowestPing + this.secondaryAcceptableLatencyMS;

Expand All @@ -141,11 +141,11 @@ PingStrategy.prototype.checkoutConnection = function(tags, secondaryCandidates)
}
}

if(self.logger && self.logger.debug) {
if(self.logger && self.logger.debug) {
self.logger.debug("Ping strategy selection order for tags", tags);
finalCandidates.forEach(function(c) {
self.logger.debug(format("%s:%s = %s ms", c.host, c.port, c.runtimeStats['pingMs']), null);
})
})
}

// If no candidates available return an error
Expand All @@ -154,12 +154,12 @@ PingStrategy.prototype.checkoutConnection = function(tags, secondaryCandidates)

// Ensure no we don't overflow
this.index = this.index % finalCandidates.length
// Pick a random acceptable server
// Pick a random acceptable server
var connection = finalCandidates[this.index].checkoutReader();
// Point to next candidate (round robin style)
this.index = this.index + 1;

if(self.logger && self.logger.debug) {
if(self.logger && self.logger.debug) {
if(connection)
self.logger.debug("picked server %s:%s", connection.socketOptions.host, connection.socketOptions.port);
}
Expand Down Expand Up @@ -252,12 +252,12 @@ PingStrategy.prototype._pingServer = function(callback) {
// Done with the pinging
done();
});
});
});
};
// Ping
_ping(_db, serverInstance);
} else {
var connectTimeoutMS = self.replicaset.options.socketOptions
var connectTimeoutMS = self.replicaset.options.socketOptions
? self.replicaset.options.socketOptions.connectTimeoutMS : 0

// Create a new master connection
Expand All @@ -275,7 +275,7 @@ PingStrategy.prototype._pingServer = function(callback) {
sslPass: self.replicaset.options.sslPass
});

// Create Db instance
// Create Db instance
var _db = new self.Db('local', _server, { safe: true });
_db.on("close", function() {
delete self.dbs[this.serverConfig.host + ":" + this.serverConfig.port];
Expand All @@ -287,7 +287,7 @@ PingStrategy.prototype._pingServer = function(callback) {
return;
}

__db.open(function(err, db) {
__db.open(function(err, db) {
// Emit ping connect
self.replicaset.emit("ping_connect", err, __serverInstance);

Expand All @@ -310,7 +310,7 @@ PingStrategy.prototype._pingServer = function(callback) {
// Execute ping on this connection
__db.executeDbCommand({ping:1}, {failFast:true}, function(err) {
self.replicaset.emit("ping", err, __serverInstance);

if(err) {
delete self.dbs[__db.serverConfig.host + ":" + __db.serverConfig.port];
__db.close();
Expand All @@ -329,7 +329,7 @@ PingStrategy.prototype._pingServer = function(callback) {
__db.close();
return done();
}

// Process the ismaster for the server
if(result && result.documents && self.replicaset.processIsMaster) {
self.replicaset.processIsMaster(__serverInstance, result.documents[0]);
Expand All @@ -339,7 +339,7 @@ PingStrategy.prototype._pingServer = function(callback) {
done();
});
});
});
});
};

// Ping the server
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
var StatisticsStrategy = exports.StatisticsStrategy = function(replicaset) {
this.replicaset = replicaset;
// Logger api
this.Logger = null;
this.Logger = null;
}

// Starts any needed code
Expand Down Expand Up @@ -74,7 +74,7 @@ StatisticsStrategy.prototype.checkoutConnection = function(tags, secondaryCandid
}

finalCandidates.sort(function(a, b) {
return a.runtimeStats.queryStats.sScore > b.runtimeStats.queryStats.sScore;
return a.runtimeStats.queryStats.sScore - b.runtimeStats.queryStats.sScore;
});

// If no candidates available return an error
Expand Down