-
Notifications
You must be signed in to change notification settings - Fork 24
/
index.js
389 lines (312 loc) · 12 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/**
Redis Sentinel client, add-on for node_redis client.
See readme for details/usage.
*/
var RedisSingleClient = require('redis'),
events = require('events'),
util = require('util'),
reply_to_object = require('redis/lib/util.js').reply_to_object,
to_array = require('redis/lib/to_array.js'),
commands = RedisSingleClient.commands;
/*
options includes:
- host
- port
- masterOptions
- masterName
- logger (e.g. winston)
- debug (boolean)
*/
function RedisSentinelClient(options) {
// RedisClient takes (stream,options). we don't want stream, make sure only one.
if (arguments.length > 1) {
throw new Error("Sentinel client takes only options to initialize");
}
// make this an EventEmitter (also below)
events.EventEmitter.call(this);
var self = this;
this.options = options = options || {};
this.options.masterName = this.options.masterName || 'mymaster';
self.emitMasterErrors = false;
// no socket support for now (b/c need multiple connections).
if (options.port == null || options.host == null) {
throw new Error("Sentinel client needs a host and port");
}
// if debugging is enabled for sentinel client, enable master client's too.
// (standard client just uses console.log, not the 'logger' passed here.)
if (options.debug) {
RedisSingleClient.debug_mode = true;
}
var masterOptions = options.masterOptions || {};
masterOptions.disable_flush = true; // Disables flush_and_error, to preserve queue
// if master & slaves need a password to authenticate,
// pass it in as 'master_auth_pass'.
// (corresponds w/ 'auth_pass' for normal client,
// but differentiating b/c we're not authenticating to the *sentinel*, rather to the master/slaves.)
// by setting it to 'auth_pass' on master client, it should authenticate to the master (& slaves on failover).
// note, sentinel daemon's conf needs to know this same password too/separately.
masterOptions.auth_pass = options.master_auth_pass || null;
// this client will always be connected to the active master.
// using 9999 as initial; expected to fail; is replaced & re-connected to real port later.
self.activeMasterClient = new RedisSingleClient.createClient(9999, '127.0.0.1', masterOptions);
// pass up errors
// @todo emit a separate 'master error' event?
self.activeMasterClient.on('error', function(error){
if (self.emitMasterErrors) {
error.message = self.myName + " master error: " + error.message;
self.onError.call(self, error);
}
});
// pass up messages
self.activeMasterClient.on('message', function(channel, message){
self.emit('message', channel, message);
});
// pass these through
['unsubscribe','end', 'reconnecting'].forEach(function(staticProp){
// @todo rewrite this to use `apply`
self.activeMasterClient.on(staticProp, function(a, b, c, d){
self.emit(staticProp, a, b, c, d);
});
});
// used for logging & errors
this.myName = 'sentinel-' + this.options.host + ':' + this.options.port + '-' + this.options.masterName;
/*
what a failover looks like:
- master fires ECONNREFUSED errors a few times
- sentinel listener gets:
+sdown
+odown
+failover-triggered
+failover-state-wait-start
+failover-state-select-slave
+selected-slave
+failover-state-send-slaveof-noone
+failover-state-wait-promotion
+promoted-slave
+failover-state-reconf-slaves
+slave-reconf-sent
+slave-reconf-inprog
+slave-reconf-done
+failover-end
+switch-master
(see docs @ http://redis.io/topics/sentinel)
note, these messages don't specify WHICH master is down.
so if a sentinel is listening to multiple masters, and we have a RedisSentinelClient
for each sentinel:master relationship, every client will be notified of every master's failovers.
But that's fine, b/c reconnect() checks if it actually changed, and does nothing if not.
*/
// one client to query ('talker'), one client to subscribe ('listener').
// these are standard redis clients.
// talker is used by reconnect() below
this.sentinelTalker = new RedisSingleClient.createClient(options.port, options.host);
this.sentinelTalker.on('connect', function(){
self.debug('connected to sentinel talker');
});
this.sentinelTalker.on('error', function(error){
error.message = self.myName + " talker error: " + error.message;
self.onError.call(self, error);
});
this.sentinelTalker.on('end', function(){
self.debug('sentinel talker disconnected');
// @todo emit something?
// @todo does it automatically reconnect? (supposed to)
});
var sentinelListener = new RedisSingleClient.createClient(options.port, options.host);
sentinelListener.on('connect', function(){
self.debug('connected to sentinel listener');
});
sentinelListener.on('error', function(error){
error.message = self.myName + " listener error: " + error.message;
self.onError(error);
});
sentinelListener.on('end', function(){
self.debug('sentinel listener disconnected');
// @todo emit something?
});
// Connect on load
this.reconnect();
// Subscribe to all messages
sentinelListener.psubscribe('*');
sentinelListener.on('pmessage', function(channel, msg) {
self.debug('sentinel message', msg);
// pass up, in case app wants to respond
self.emit('sentinel message', msg);
switch(msg) {
case '+sdown':
self.debug('Down detected');
self.emit('down-start');
self.emitMasterErrors = false;
break;
case '+failover-triggered':
self.debug('Failover detected');
self.emit('failover-start');
break;
case '+failover-end':
self.debug('Reconnect triggered by ' + msg);
self.emit('failover-end');
self.reconnect();
break;
}
});
}
util.inherits(RedisSentinelClient, events.EventEmitter);
// [re]connect activeMasterClient to the master.
// destroys the previous connection and replaces w/ a new one,
// (transparently to the user)
// but only if host+port have changed.
RedisSentinelClient.prototype.reconnect = function reconnect(onReconnectCallback) {
var self = this;
self.debug('reconnecting');
self.sentinelTalker.send_command("SENTINEL", ["get-master-addr-by-name", self.options.masterName], function(error, newMaster) {
if (error) {
error.message = self.myName + " Error getting master: " + error.message;
self.onError(error);
return;
}
try {
newMaster = {
host: newMaster[0],
port: newMaster[1]
};
self.debug('new master info', newMaster);
if (!newMaster.host || !newMaster.port) throw new Error("Missing host or port");
}
catch(error) {
error.message = self.myName + ' Unable to reconnect master: ' + error.message;
self.onError(error);
}
if (self.activeMasterClient &&
newMaster.host === self.activeMasterClient.host &&
newMaster.port === self.activeMasterClient.port) {
self.debug('Master has not changed, nothing to do');
if (typeof onReconnectCallback === 'function') onReconnectCallback();
return;
}
self.debug("Changing master from " +
(self.activeMasterClient ? self.activeMasterClient.host + ":" + self.activeMasterClient.port : "[none]") +
" to " + newMaster.host + ":" + newMaster.port);
// reconfigure it
self.activeMasterClient.host = newMaster.host;
self.activeMasterClient.port = newMaster.port;
// reconnect it.
// (if using auth, should re-auth automatically too.)
self.activeMasterClient.forceReconnectionAttempt();
// @todo use no_ready_check = true? then change this 'ready' to 'connect'
// backwards-compat. w/RedisClient
self.activeMasterClient.on('connect', function(){
self.emit('connect');
self.emitMasterErrors = true;
});
self.activeMasterClient.once('ready', function(){
self.debug('New master is ready');
// anything outside holding a ref to activeMasterClient needs to listen to this,
// and refresh its reference. pass the new master so it's easier.
self.emit('reconnected', self.activeMasterClient);
// backwards-compat. w/RedisClient
self.emit('ready');
if (typeof onReconnectCallback === 'function') onReconnectCallback();
});
});
};
//
// pass thru all client commands from RedisSentinelClient to activeMasterClient
//
RedisSentinelClient.prototype.send_command = function (command, args, callback) {
// this ref needs to be totally atomic
var client = this.activeMasterClient;
return client.send_command.apply(client, arguments);
};
// adapted from index.js for RedisClient
commands.forEach(function (command) {
RedisSentinelClient.prototype[command.toUpperCase()] =
RedisSentinelClient.prototype[command] = function (args, callback) {
var sentinel = this;
sentinel.debug('command', command, args);
if (Array.isArray(args) && typeof callback === "function") {
return sentinel.send_command(command, args, callback);
} else {
return sentinel.send_command(command, to_array(arguments));
}
};
});
// automagically handle multi & exec?
// (tests will tell...)
// this multi is on the master client, so don't hold onto it too long!
var Multi = RedisSingleClient.Multi;
// @todo make a SentinelMulti that queues within the sentinel client?
// would need to handle all Multi.prototype methods, etc.
// for now let multi's queue die if the master dies.
RedisSentinelClient.prototype.multi =
RedisSentinelClient.prototype.MULTI = function (args) {
return new Multi(this.activeMasterClient, args);
};
['hmget', 'hmset', 'done'].forEach(function(staticProp){
RedisSentinelClient.prototype[staticProp] =
RedisSentinelClient.prototype[staticProp.toUpperCase()] = function(){
var client = this.activeMasterClient;
return client[staticProp].apply(client, arguments);
};
});
// helper to get client.
// (even tho activeMasterClient is public, this is clearer)
RedisSentinelClient.prototype.getMaster = function getMaster() {
return this.activeMasterClient;
};
// get static values from client, also pass-thru
// not all of them... @review!
[ 'connection_id', 'ready', 'connected', 'connections', 'commands_sent', 'connect_timeout',
'monitoring', 'closing', 'server_info',
'stream' /* ?? */
].forEach(function(staticProp){
RedisSentinelClient.prototype.__defineGetter__(staticProp, function(){
if (this.activeMasterClient !== null) {
return this.activeMasterClient[staticProp];
} else {
return null;
}
});
// might as well have a setter too...?
RedisSentinelClient.prototype.__defineSetter__(staticProp, function(newVal){
return this.activeMasterClient[staticProp] = newVal;
});
});
// log(type, msgs...)
// type is 'info', 'error', 'debug' etc
RedisSentinelClient.prototype.log = function log() {
var logger = this.options.logger || console;
var args = Array.prototype.slice.call(arguments);
logger.log.apply(logger, args);
};
// debug(msgs...)
RedisSentinelClient.prototype.debug = function debug() {
if (this.options.debug) {
var args = ['debug'].concat(Array.prototype.slice.call(arguments));
this.log.apply(this, args);
}
};
RedisSentinelClient.prototype.onError = function(error) {
// redundant? debug instead?
this.log('error', error);
this.emit('error', error);
};
exports.RedisSentinelClient = RedisSentinelClient;
// called by RedisClient::createClient() when options.sentinel===true
// similar args for backwards compat,
// but does not support sockets (see above)
exports.createClient = function (port, host, options) {
// allow the arg structure of RedisClient, but collapse into options for constructor.
//
// note, no default_host or default_port,
// see http://redis.io/topics/sentinel.
// also no net_client or allowNoSocket, see above.
// this could be a problem w/ backwards compatibility.
if (arguments.length === 1) {
options = arguments[0] || {};
}
else {
options = options || {};
options.port = port;
options.host = host;
}
return new RedisSentinelClient(options);
};