Skip to content

Commit 6ddcc99

Browse files
doublesharpluin
authored andcommitted
feat: Sentinel preferredSlaves option (#370)
* Support for “preferredSlave” when connecting to a Sentinel group for 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. * Update README with details on the `preferredSlaves` option.
1 parent 8718567 commit 6ddcc99

File tree

3 files changed

+163
-4
lines changed

3 files changed

+163
-4
lines changed

README.md

+37-1
Original file line numberDiff line numberDiff line change
@@ -608,10 +608,46 @@ The arguments passed to the constructor are different from the ones you use to c
608608

609609
* `name` identifies a group of Redis instances composed of a master and one or more slaves (`mymaster` in the example);
610610
* `sentinels` are a list of sentinels to connect to. The list does not need to enumerate all your sentinel instances, but a few so that if one is down the client will try the next one.
611+
* `role` (optional) with a value of `slave` will return a random slave from the Sentinel group.
612+
* `preferredSlaves` (optional) can be used to prefer a particular slave or set of slaves based on priority. It accepts a function or array.
611613

612614
ioredis **guarantees** that the node you connected to is always a master even after a failover. When a failover happens, instead of trying to reconnect to the failed node (which will be demoted to slave when it's available again), ioredis will ask sentinels for the new master node and connect to it. All commands sent during the failover are queued and will be executed when the new connection is established so that none of the commands will be lost.
613615

614-
It's possible to connect to a slave instead of a master by specifying the option `role` with the value of `slave`, and ioredis will try to connect to a random slave of the specified master, with the guarantee that the connected node is always a slave. If the current node is promoted to master due to a failover, ioredis will disconnect from it and ask the sentinels for another slave node to connect to.
616+
It's possible to connect to a slave instead of a master by specifying the option `role` with the value of `slave` and ioredis will try to connect to a random slave of the specified master, with the guarantee that the connected node is always a slave. If the current node is promoted to master due to a failover, ioredis will disconnect from it and ask the sentinels for another slave node to connect to.
617+
618+
If you specify the option `preferredSlaves` along with `role: 'slave'` ioredis will attempt to use this value when selecting the slave from the pool of available slaves. The value of `preferredSlaves` should either be a function that accepts an array of avaiable slaves and returns a single result, or an array of slave values priorities by the lowest `prio` value first with a default value of `1`.
619+
620+
```javascript
621+
// available slaves format
622+
var availableSlaves = [{ ip: '127.0.0.1', port: '31231', flags: 'slave' }];
623+
624+
// preferredSlaves array format
625+
var preferredSlaves = [
626+
{ ip: '127.0.0.1', port: '31231', prio: 1 },
627+
{ ip: '127.0.0.1', port: '31232', prio: 2 }
628+
];
629+
630+
// preferredSlaves function format
631+
preferredSlaves = function(availableSlaves) {
632+
for (var i = 0; i < availableSlaves.length; i++) {
633+
var slave = availableSlaves[i];
634+
if (slave.ip === '127.0.0.1') {
635+
if (slave.port === '31234') {
636+
return slave;
637+
}
638+
}
639+
}
640+
// if no preferred slaves are available a random one is used
641+
return false;
642+
};
643+
644+
var redis = new Redis({
645+
sentinels: [{ host: '127.0.0.1', port: 26379 }, { host: '127.0.0.1', port: 26380 }],
646+
name: 'mymaster',
647+
role: 'slave',
648+
preferredSlaves: preferredSlaves
649+
});
650+
```
615651

616652
Besides the `retryStrategy` option, there's also a `sentinelRetryStrategy` in Sentinel mode which will be invoked when all the sentinel nodes are unreachable during connecting. If `sentinelRetryStrategy` returns a valid delay time, ioredis will try to reconnect from scratch. The default value of `sentinelRetryStrategy` is:
617653

lib/connectors/sentinel_connector.js

+56-2
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ var Redis;
1111
function SentinelConnector(options) {
1212
Connector.call(this, options);
1313
if (this.options.sentinels.length === 0) {
14-
throw new Error('Requires at least on sentinel to connect to.');
14+
throw new Error('Requires at least one sentinel to connect to.');
1515
}
1616
if (!this.options.name) {
1717
throw new Error('Requires the name of master.');
@@ -131,6 +131,7 @@ SentinelConnector.prototype.resolveMaster = function (client, callback) {
131131
};
132132

133133
SentinelConnector.prototype.resolveSlave = function (client, callback) {
134+
var _this = this;
134135
client.sentinel('slaves', this.options.name, function (err, result) {
135136
client.disconnect();
136137
if (err) {
@@ -145,7 +146,60 @@ SentinelConnector.prototype.resolveSlave = function (client, callback) {
145146
availableSlaves.push(slave);
146147
}
147148
}
148-
selectedSlave = _.sample(availableSlaves);
149+
// allow the options to prefer particular slave(s)
150+
if (_this.options.preferredSlaves) {
151+
var preferredSlaves = _this.options.preferredSlaves;
152+
switch (typeof preferredSlaves) {
153+
case 'function':
154+
// use function from options to filter preferred slave
155+
selectedSlave = _this.options.preferredSlaves(availableSlaves);
156+
break;
157+
case 'object':
158+
if (!Array.isArray(preferredSlaves)) {
159+
preferredSlaves = [preferredSlaves];
160+
} else {
161+
// sort by priority
162+
preferredSlaves.sort(function (a, b) {
163+
// default the priority to 1
164+
if (!a.prio) {
165+
a.prio = 1;
166+
}
167+
if (!b.prio) {
168+
b.prio = 1;
169+
}
170+
171+
// lowest priority first
172+
if (a.prio < b.prio) {
173+
return -1;
174+
}
175+
if (a.prio > b.prio) {
176+
return 1;
177+
}
178+
return 0;
179+
});
180+
}
181+
// loop over preferred slaves and return the first match
182+
for (var p = 0; p < preferredSlaves.length; p++) {
183+
for (var a = 0; a < availableSlaves.length; a++) {
184+
if (availableSlaves[a].ip === preferredSlaves[p].ip) {
185+
if (availableSlaves[a].port === preferredSlaves[p].port) {
186+
selectedSlave = availableSlaves[a];
187+
break;
188+
}
189+
}
190+
}
191+
if (selectedSlave) {
192+
break;
193+
}
194+
}
195+
// if none of the preferred slaves are available, a random available slave is returned
196+
break;
197+
}
198+
}
199+
if (!selectedSlave) {
200+
// get a random available slave
201+
selectedSlave = _.sample(availableSlaves);
202+
}
149203
}
150204
callback(null, selectedSlave ? { host: selectedSlave.ip, port: selectedSlave.port } : null);
151205
});

test/functional/sentinel.js

+70-1
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,76 @@ describe('sentinel', function () {
231231
{ host: '127.0.0.1', port: '27379' }
232232
],
233233
name: 'master',
234-
role: 'slave'
234+
role: 'slave',
235+
preferredSlaves: [{ ip: '127.0.0.1', port: '17381', prio: 10 }]
236+
});
237+
});
238+
239+
it('should connect to the slave successfully based on preferred slave priority', function (done) {
240+
var sentinel = new MockServer(27379, function (argv) {
241+
if (argv[0] === 'sentinel' && argv[1] === 'slaves' && argv[2] === 'master') {
242+
return [
243+
['ip', '127.0.0.1', 'port', '44444', 'flags', 'slave'],
244+
['ip', '127.0.0.1', 'port', '17381', 'flags', 'slave'],
245+
['ip', '127.0.0.1', 'port', '55555', 'flags', 'slave']
246+
];
247+
}
248+
});
249+
var slave = new MockServer(17381);
250+
slave.on('connect', function () {
251+
redis.disconnect();
252+
sentinel.disconnect(function () {
253+
slave.disconnect(done);
254+
});
255+
});
256+
257+
var redis = new Redis({
258+
sentinels: [
259+
{ host: '127.0.0.1', port: '27379' }
260+
],
261+
name: 'master',
262+
role: 'slave',
263+
// for code coverage (sorting, etc), use multiple valid values that resolve to prio 1
264+
preferredSlaves: [,
265+
{ ip: '127.0.0.1', port: '11111', prio: 100 },
266+
{ ip: '127.0.0.1', port: '17381', prio: 1 },
267+
{ ip: '127.0.0.1', port: '22222', prio: 100 },
268+
{ ip: '127.0.0.1', port: '17381' },
269+
{ ip: '127.0.0.1', port: '17381' }
270+
]
271+
});
272+
});
273+
274+
it('should connect to the slave successfully based on preferred slave filter function', function (done) {
275+
var sentinel = new MockServer(27379, function (argv) {
276+
if (argv[0] === 'sentinel' && argv[1] === 'slaves' && argv[2] === 'master') {
277+
return [['ip', '127.0.0.1', 'port', '17381', 'flags', 'slave']];
278+
}
279+
});
280+
// only one running slave, which we will prefer
281+
var slave = new MockServer(17381);
282+
slave.on('connect', function () {
283+
redis.disconnect();
284+
sentinel.disconnect(function () {
285+
slave.disconnect(done);
286+
});
287+
});
288+
289+
var redis = new Redis({
290+
sentinels: [
291+
{ host: '127.0.0.1', port: '27379' }
292+
],
293+
name: 'master',
294+
role: 'slave',
295+
preferredSlaves: function(slaves){
296+
for (var i = 0; i < slaves.length; i++){
297+
var slave = slaves[i];
298+
if (slave.ip == '127.0.0.1' && slave.port =='17381'){
299+
return slave;
300+
}
301+
}
302+
return false;
303+
}
235304
});
236305
});
237306

0 commit comments

Comments
 (0)