Skip to content

Commit

Permalink
Support for “preferredSlave” when connecting to a Sentinel group for
Browse files Browse the repository at this point in the history
local reads in a distributed environment. Value can be an array of
objects with required properties “ip” and “port” and option property of
“prio” indicating the priority, defaults to 1, lower numbers preferred
first. If the value is a function the available slaves are passed into
it, expecting a single slave as the response. If no preferred slave is
found a random one is returned from those available.

Updated tests to return bad values in SENTINEL slaves command but still
select the correct one based on the preferredSlave result.
  • Loading branch information
doublesharp committed Sep 24, 2016
1 parent b216e4e commit 8dfbe32
Show file tree
Hide file tree
Showing 2 changed files with 127 additions and 4 deletions.
60 changes: 57 additions & 3 deletions lib/connectors/sentinel_connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ var Redis;
function SentinelConnector(options) {
Connector.call(this, options);
if (this.options.sentinels.length === 0) {
throw new Error('Requires at least on sentinel to connect to.');
throw new Error('Requires at least one sentinel to connect to.');
}
if (!this.options.name) {
throw new Error('Requires the name of master.');
Expand Down Expand Up @@ -99,7 +99,7 @@ SentinelConnector.prototype.updateSentinels = function (client, callback) {
var sentinel = utils.packObject(result[i]);
var flags = sentinel.flags ? sentinel.flags.split(',') : [];
if (flags.indexOf('disconnected') === -1 && sentinel.ip && sentinel.port) {
var endpoint = { host: sentinel.ip, port: parseInt(sentinel.port) };
var endpoint = { host: sentinel.ip, port: parseInt(sentinel.port, 10) };
var isDuplicate = _this.sentinels.some(function (o) {
return o.host === endpoint.host && o.port === endpoint.port;
});
Expand Down Expand Up @@ -133,6 +133,7 @@ SentinelConnector.prototype.resolveMaster = function (client, callback) {
};

SentinelConnector.prototype.resolveSlave = function (client, callback) {
var _this = this;
client.sentinel('slaves', this.options.name, function (err, result) {
client.disconnect();
if (err) {
Expand All @@ -147,7 +148,60 @@ SentinelConnector.prototype.resolveSlave = function (client, callback) {
availableSlaves.push(slave);
}
}
selectedSlave = _.sample(availableSlaves);
// allow the options to prefer particular slave(s)
if (_this.options.preferredSlaves) {
var preferredSlaves = _this.options.preferredSlaves;
switch (typeof preferredSlaves) {
case 'function':
// use function from options to filter preferred slave
selectedSlave = _this.options.preferredSlaves(availableSlaves);
break;
case 'object':
if (!Array.isArray(preferredSlaves)) {
preferredSlaves = [preferredSlaves];
} else {
// sort by priority
preferredSlaves.sort(function (a, b) {
// default the priority to 1
if (!a.prio) {
a.prio = 1;
}
if (!b.prio) {
b.prio = 1;
}

// lowest priority first
if (a.prio < b.prio) {
return -1;
}
if (a.prio > b.prio) {
return 1;
}
return 0;
});
}
// loop over preferred slaves and return the first match
for (var p = 0; p < preferredSlaves.length; p++) {
for (var a = 0; a < availableSlaves.length; a++) {
if (availableSlaves[a].ip === preferredSlaves[p].ip) {
if (availableSlaves[a].port === preferredSlaves[p].port) {
selectedSlave = availableSlaves[a];
break;
}
}
}
if (selectedSlave) {
break;
}
}
// if none of the preferred slaves are available, a random available slave is returned
break;
}
}
if (!selectedSlave) {
// get a random available slave
selectedSlave = _.sample(availableSlaves);
}
}
callback(null, selectedSlave ? { host: selectedSlave.ip, port: selectedSlave.port } : null);
});
Expand Down
71 changes: 70 additions & 1 deletion test/functional/sentinel.js
Original file line number Diff line number Diff line change
Expand Up @@ -231,7 +231,76 @@ describe('sentinel', function () {
{ host: '127.0.0.1', port: '27379' }
],
name: 'master',
role: 'slave'
role: 'slave',
preferredSlaves: [{ ip: '127.0.0.1', port: '17381', prio: 10 }]
});
});

it('should connect to the slave successfully based on preferred slave priority', function (done) {
var sentinel = new MockServer(27379, function (argv) {
if (argv[0] === 'sentinel' && argv[1] === 'slaves' && argv[2] === 'master') {
return [
['ip', '127.0.0.1', 'port', '44444', 'flags', 'slave'],
['ip', '127.0.0.1', 'port', '17381', 'flags', 'slave'],
['ip', '127.0.0.1', 'port', '55555', 'flags', 'slave']
];
}
});
var slave = new MockServer(17381);
slave.on('connect', function () {
redis.disconnect();
sentinel.disconnect(function () {
slave.disconnect(done);
});
});

var redis = new Redis({
sentinels: [
{ host: '127.0.0.1', port: '27379' }
],
name: 'master',
role: 'slave',
// for code coverage (sorting, etc), use multiple valid values that resolve to prio 1
preferredSlaves: [,
{ ip: '127.0.0.1', port: '11111', prio: 100 },
{ ip: '127.0.0.1', port: '17381', prio: 1 },
{ ip: '127.0.0.1', port: '22222', prio: 100 },
{ ip: '127.0.0.1', port: '17381' },
{ ip: '127.0.0.1', port: '17381' }
]
});
});

it('should connect to the slave successfully based on preferred slave filter function', function (done) {
var sentinel = new MockServer(27379, function (argv) {
if (argv[0] === 'sentinel' && argv[1] === 'slaves' && argv[2] === 'master') {
return [['ip', '127.0.0.1', 'port', '17381', 'flags', 'slave']];
}
});
// only one running slave, which we will prefer
var slave = new MockServer(17381);
slave.on('connect', function () {
redis.disconnect();
sentinel.disconnect(function () {
slave.disconnect(done);
});
});

var redis = new Redis({
sentinels: [
{ host: '127.0.0.1', port: '27379' }
],
name: 'master',
role: 'slave',
preferredSlaves: function(slaves){
for (var i = 0; i < slaves.length; i++){
var slave = slaves[i];
if (slave.ip == '127.0.0.1' && slave.port =='17381'){
return slave;
}
}
return false;
}
});
});

Expand Down

0 comments on commit 8dfbe32

Please sign in to comment.