Skip to content
Browse files

Merge branch 'runid_based_prioritization'

  • Loading branch information...
2 parents 9d31a43 + 404aec4 commit 8cc79ec966269411f9ecf9aed562f06fd765fa08 @sreeix committed Mar 13, 2013
Showing with 34 additions and 22 deletions.
  1. +1 −1 config/config.json
  2. +24 −12 lib/redis_proxy.js
  3. +7 −8 lib/server.js
  4. +1 −1 package.json
  5. +1 −0 server.js
View
2 config/config.json
@@ -1,7 +1,7 @@
{
"servers": [{
"host": "localhost"
- ,"port": 6379
+ ,"port": 6399
}
, {
"host": "localhost"
View
36 lib/redis_proxy.js
@@ -23,7 +23,7 @@ var RedisProxy = module.exports = function(o){
this.options = _.defaults(o, {listen_port: 6379, softErrorCount: 5, pool_size: 10, mode: "allToMaster"});
if(o.servers && o.servers.size === 0)
- throw new Error("Expected to have at least one redis to proxy");
+ throw new Error("Expected to have at least one redis to proxy. Can't start");
this.sendCommand = this[this.options.mode];
logger.info("Using the "+ this.options.mode +" mode.");
@@ -38,7 +38,7 @@ var RedisProxy = module.exports = function(o){
}
var onUp = function onUp() {
logger.debug("We have a server that went up");
- if(_.isNull(self._active) && this.client.server_info.role === 'master'){
+ if(!self.active && this.client.server_info.role === 'master'){
self._active = this;
logger.info("setting up the active "+ self._active.options.host + ":" + self._active.options.port);
self.readyup(this);
@@ -70,15 +70,17 @@ RedisProxy.prototype.readyup = function(active){
};
RedisProxy.prototype.nextActive = function() {
- this._active = _.find(this.allServers, function(server) {
- return server.isUp();
- });
+ this._active = _.chain(this.allServers).select(function(server) {
+ return server.isUp() && server.client.server_info["slave-priority"] !== "0";
+ }).sortBy(function (server) {
+ return server.client.server_info["runid"] ; // no slave priority right now, so just use runid
+ }).first().value();
if(this._active){
this.readyup(this.active);
logger.info("Setting up as active "+ this.active.options.host +" : " + this.active.options.port);
} else {
- throw new Error("Expected to have atleast one redis to proxy");
+ logger.error("No redis available");
}
};
@@ -88,17 +90,27 @@ Object.defineProperty(RedisProxy.prototype, 'active', {
// balancing strategies
RedisProxy.prototype.readsToSlaves = function(command, id, callback) {
+ var serverToSend = null;
+ if(!this.active){
+ return callback(new Error("Expected to have atleast one redis to proxy"));
+ }
if(redisCommand.readOnly(command)) {
- logger.info('Read only command sending to the slave');
- (this.nextSlave() || this._active).sendCommand(command, id, callback);
+ logger.info('Read only command');
+ serverToSend = (this.nextSlave() || this.active)
} else {
- logger.info('mutating command sending to the active master');
- this._active.sendCommand(command, id, callback);
+ logger.info('mutating command');
+ serverToSend = this.active;
}
+ logger.info('server:'+ serverToSend.toString());
+ return serverToSend.sendCommand(command, id, callback);
};
RedisProxy.prototype.allToMaster = function(command, id, callback) {
- this._active.sendCommand(command, id, callback);
+ if(this._active){
+ this._active.sendCommand(command, id, callback);
+ }else{
+ return callback(new Error("Expected to have atleast one redis to proxy"));
+ }
};
RedisProxy.prototype.nextSlave = function() {
@@ -108,7 +120,7 @@ RedisProxy.prototype.nextSlave = function() {
};
RedisProxy.prototype.quit = function(id) {
- return this.active.close(id);
+ if(this.active) this.active.close(id);
};
RedisProxy.prototype.watch = function(){
View
15 lib/server.js
@@ -94,7 +94,7 @@ Server.prototype._createConnections = function(){
, startSize: this.options.pool_size
, delayCreation: false
});
-}
+};
Server.prototype.slave = function(server){
var self = this;
@@ -122,7 +122,7 @@ Server.prototype._master = function (){
Server.prototype._incrErrorCount = function(){
this.errorCount++;
if(this.errorCount > this.options.softErrorCount){
- return this.down();
+ this.down();
}
};
@@ -158,15 +158,14 @@ Server.prototype._clearConnections = function clearConnections(){
this.connections.closeAll();
this.connections = null;
};
+Server.prototype.toString = function () {
+ return this.host+":"+ this.port;
+}
Object.defineProperty(Server.prototype, 'host', {
- get: function() {
- return this.options.host;
- }
+ get: function() {return this.options.host;}
});
Object.defineProperty(Server.prototype, 'port', {
- get: function() {
- return this.options.port;
- }
+ get: function() {return this.options.port;}
});
View
2 package.json
@@ -23,7 +23,7 @@
, "hiredis": ""
, "connection_pool": "0.0.2"
, "node-redis-raw": "0.0.2"
- , "redis": "0.7.1"
+ , "redis": "0.8.2"
}
, "devDependencies": {
"mocha": "0.0.8"
View
1 server.js
@@ -30,6 +30,7 @@ var server = net.createServer(function (socket) {
redis_proxy.sendCommand(command, id, function(err, res) {
if(err){
logger.error(err);
+ return socket.write("+ERR "+ err+"\r\n");
}
if(res){
socket.write(res.toString('utf8'));

0 comments on commit 8cc79ec

Please sign in to comment.
Something went wrong with that request. Please try again.