-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathzmq_raft_subscriber.js
492 lines (418 loc) · 15.8 KB
/
zmq_raft_subscriber.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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
/*
* Copyright (c) 2016-2023 Rafał Michalski <[email protected]>
*/
"use strict";
/*
client = new ZmqRaftSubscriber('tcp://127.0.0.1:1234')
client.on('data' => (snapshotChunkOrLogEntry) => {
})
client.write(updateRequest)
db.stream.pipe(client).pipe(db.stream);
*/
const isArray = Array.isArray
, isBuffer = Buffer.isBuffer
, now = Date.now;
const assert = require('assert');
const { Duplex } = require('stream');
const { ZMQ_LINGER } = require('zeromq');
const { ZmqSocket } = require('../utils/zmqsocket');
const { assertConstantsDefined, delay, parsePeers } = require('../utils/helpers');
const { bufferToLogEntry, UpdateRequest: { isUpdateRequest } } = require('../common/log_entry');
const { BROADCAST_HEARTBEAT_INTERVAL
, RE_STATUS_SNAPSHOT
} = require('../common/constants');
assertConstantsDefined({
BROADCAST_HEARTBEAT_INTERVAL
, RE_STATUS_SNAPSHOT
}, 'number');
const DEFAULT_BROADCAST_TIMEOUT = BROADCAST_HEARTBEAT_INTERVAL * 2;
const BROADCAST_TIMEOUT_MIN = 100;
const REQUEST_URL_MSG_TYPE = '*';
const requestUrlTypeBuf = Buffer.from(REQUEST_URL_MSG_TYPE);
const ZmqRaftClient = require('../client/zmq_raft_client');
const TimeoutError = ZmqRaftClient.TimeoutError;
const secretBuf$ = Symbol('secretBuf');
const { createFramesProtocol } = require('../protocol');
const stateBroadcastProtocol = createFramesProtocol('StateBroadcast');
const debug = require('debug')('zmq-raft:subscriber');
function MissingEntriesError(message) {
Error.captureStackTrace(this, TimeoutError);
this.name = 'TimeoutError';
this.message = message || 'some entries are missing';
}
MissingEntriesError.prototype = Object.create(Error.prototype);
MissingEntriesError.prototype.constructor = MissingEntriesError;
MissingEntriesError.prototype.isMissingEntries = true;
/**
* A ZmqRaftSubscriber class implements a stream.Duplex interface for reading and updating
* log entries from the ZmqRaft peer cluster with a BroadcastStateMachine via an underlying
* ZmqRaftClient and a zeromq SUB socket.
*
* The messages received by the Readable part of the stream are instances of either
* common.LogEntry or common.SnapshotChunk.
*
* The Writable part of the stream accepts instances of common.LogEntry.UpdateRequest.
*
* @property lastLogIndex {number} - keeps track of the last received log index
* @property lastUpdateLogIndex {number} - keeps track of the last update commit index
**/
class ZmqRaftSubscriber extends Duplex {
/**
* Create ZmqRaftSubscriber
*
* `options` may be one of:
*
* - `url` {String}: A seed url to fetch peer urls from via a Request Config RPC.
* - `urls` {Array}: An array of seed urls to fetch peers from via a Request Config RPC.
* - `peers` {Array}: An array of established zmq raft server descriptors;
* `peers` has precedence over `urls` and if provided the peer list
* is not being fetched via Request Config RPC.
* - `secret` {String|Buffer}: A cluster identifying string which is sent and verified against
* in each message.
* - `timeout` {number}: A time in milliseconds after which we consider a server peer as unresponsive.
* - `sockopts` {Object}: Specify zeromq socket options as an object e.g.: `{ZMQ_IPV4ONLY: true}`.
* - `highwatermark` {number}: A shortcut to specify `ZMQ_SNDHWM` socket option for an underlying
* zeromq DEALER socket; this affects how many messages are queued per server
* so if one of the peers goes down this many messages are possibly lost;
* setting it prevents spamming a peer with expired messages when temporary
* network partition occures (default: 2).
* - `serverElectionGraceDelay` {number}: A delay in milliseconds to wait for the Raft peers
* to elect a new leader before retrying (default: 300).
* - `duplex` {Object}: Super class initialization options
* (stream.Duplex, e.g. writableHighWaterMark, readableHighWaterMark).
* - `readable` {Object}: RequestEntriesStream super class initialization options
* (stream.Readable, e.g. highWaterMark).
* - `lastIndex` {number}: An index of the last entry in the local state machine:
* entries with indexes starting from lastIndex + 1 will be retrieved.
* - `broadcastTimeout` {number}: Milliseconds before the broadcast server is considered unresponsive
* (default: 1 second).
*
* @param {string|Array} [urls]
* @param {Object} [options]
* @return {ZmqRaftSubscriber}
**/
constructor(urls, options) {
if (urls && !isArray(urls) && 'object' === typeof urls) {
options = urls, urls = undefined;
}
options || (options = {});
if (!options && 'object' !== typeof options) {
throw TypeError('ZmqRaftSubscriber: options must be an object');
}
var broadcastTimeoutMs = (options.broadcastTimeout|0) || DEFAULT_BROADCAST_TIMEOUT;
if (broadcastTimeoutMs < BROADCAST_TIMEOUT_MIN) {
broadcastTimeoutMs = BROADCAST_TIMEOUT_MIN;
}
const client = new ZmqRaftClient(urls, Object.assign({lazy: true}, options));
super(Object.assign({readableHighWaterMark: 4}, options.duplex, {objectMode: true}));
this.entriesStreamOptions = Object.assign({highWaterMark: 4}, options.readable, {timeout: 0});
this.broadcastTimeoutMs = broadcastTimeoutMs;
debug('broadcast timeout: %s ms.', broadcastTimeoutMs);
this._read = (/* size */) => {
/* lazy initialize */
delete this._read;
delete this._write;
sub.subscribe(this[secretBuf$]);
this._subscriberTimeout = true;
this._requestPublisherUrl().catch(err => this.emit('error', err));
};
this._write = (updateRequest, encoding, callback) => {
delete this._write;
this._requestPublisherUrl().catch(err => this.emit('error', err));
this._write(updateRequest, encoding, callback);
};
this.client = client;
this[secretBuf$] = Buffer.from(options.secret || '');
this.url = null;
var sub = this.sub = new ZmqSocket('sub');
/* makes sure socket is really closed when close() is called */
sub.setsockopt(ZMQ_LINGER, 0);
this._listener = stateBroadcastProtocol.createSubMessageListener(sub, this._handleBroadcast, this);
var lastIndex = options.lastIndex;
if (lastIndex === undefined) lastIndex = 0;
else if (!Number.isFinite(lastIndex) || lastIndex < 0 || lastIndex % 1 !== 0) {
throw new Error("ZmqRaftSubscriber: lastIndex must be an unsigned integer");
}
this.lastLogIndex = lastIndex;
this.lastUpdateLogIndex = lastIndex;
this.ahead = [];
this.isFresh = null;
this._pendingMissing = null;
this._pendingRpcStream = null;
this._pendingRequestPubUrl = null;
this._subscriberTimeout = undefined;
}
close() {
const sub = this.sub;
if (!sub) return this;
this.sub = null;
clearTimeout(this._subscriberTimeout);
this._subscriberTimeout = undefined;
sub.unsubscribe(this[secretBuf$]);
if (this.url !== null) {
debug('socket.disconnect: %s', this.url)
sub.disconnect(this.url);
}
sub.removeListener('frames', this._listener);
sub.close();
this.client.close();
this.client = null;
debug('subscriber closed');
return this;
}
toString() {
var url = this.url;
return `[object ZmqRaftSubscriber{${(url || '-none-')}}]`;
}
connect(url) {
const sub = this.sub
, old = this.url;
if (isBuffer(url)) url = url.toString();
if (old) {
debug('subscriber.disconnect: %s', old);
sub.disconnect(old);
}
if (url) {
if ('string' !== typeof url) {
throw new TypeError("subscriber.connect: url must be a string or a buffer");
}
this.url = url;
debug('subscriber.connect: %s', url);
sub.connect(url);
}
else {
this.url = null;
}
return this;
}
_requestPublisherUrl() {
if (this._pendingRequestPubUrl !== null) return this._pendingRequestPubUrl;
debug('requesting publisher url');
const client = this.client;
var request = () => {
return client.requestConfig().then(() => {
if (client.leaderId !== null) {
/* we have a leader, ask for state machine publisher url */
return client.request([requestUrlTypeBuf, this[secretBuf$]])
.then(([url]) => {
this.connect(url);
if (url === undefined) {
debug('missing publisher url, retrying request');
return request();
}
else {
this._refreshSubTimeout();
/* success */
this._pendingRequestPubUrl = null;
return this.url;
}
})
.catch(err => {
if (err.isTimeout) {
debug('timeout occured trying to find another server');
client.setLeader(null);
return request();
}
else throw err;
});
}
else {
debug('no leader, trying again later');
return delay(client.serverElectionGraceDelay).then(() => request());
}
});
};
return this._pendingRequestPubUrl = request().catch(err => {
this._pendingRequestPubUrl = null;
throw err;
});
}
_handleBroadcast(args) {
const [secret, term, lastLogIndex] = args.splice(0, 3);
const entries = args;
if (!this[secretBuf$].equals(secret)) {
return this.emit('error', new Error('ZmqRaftSubscriber: broadcast auth fail'));
}
try {
if (!this._appendEntries(entries, lastLogIndex)) {
this._pauseSubscriber();
}
if (!this.isFresh) {
this.isFresh = true;
this.emit('fresh');
}
} catch(err) {
if (err.isMissingEntries) {
debug('broadcast missing: (%s) %s < %s', entries.length, this.lastLogIndex, lastLogIndex);
const ahead = this.ahead;
if (entries.length !== 0) {
ahead.push({lastLogIndex: lastLogIndex, entries: entries});
}
this._requestMissingEntries(lastLogIndex - entries.length - this.lastLogIndex);
}
else throw err;
}
this._refreshSubTimeout();
}
_flushAhead() {
const ahead = this.ahead;
while(ahead.length !== 0) {
const {lastLogIndex, entries} = ahead[0];
try {
if (!this._appendEntries(entries, lastLogIndex)) {
this._pauseSubscriber();
}
ahead.shift();
} catch(err) {
if (err.isMissingEntries) {
/* gap in ahead */
return this._requestMissingEntries(lastLogIndex - entries.length - this.lastLogIndex);
}
else throw err;
}
}
}
/*
throws MissingEntriesError if provided entries can't be applied
returns false when backpressure is needed:
https://nodejs.org/dist/latest-v6.x/docs/api/stream.html#stream_readable_push_chunk_encoding */
_appendEntries(entries, lastLogIndex) {
const entryCount = entries.length;
const gapSize = lastLogIndex - this.lastLogIndex;
if (gapSize > entryCount) {
throw new MissingEntriesError();
}
var res = true;
if (lastLogIndex > this.lastLogIndex) {
const firstLogIndex = lastLogIndex - entryCount + 1;
debug('appending entries: (%s) indexes: %s - %s', entryCount, firstLogIndex, lastLogIndex);
for(let i = entryCount - gapSize; i < entryCount; ++i) {
let entry = bufferToLogEntry(entries[i], firstLogIndex + i);
res = this.push(entry);
}
this.lastLogIndex = lastLogIndex;
}
return res;
}
_pauseSubscriber() {
if (this._subscriberTimeout) {
clearTimeout(this._subscriberTimeout);
debug('backpressured, unsubscribing');
const sub = this.sub;
sub.pause();
sub.unsubscribe(this[secretBuf$]);
this._subscriberTimeout = null;
}
}
_refreshSubTimeout() {
if (this._subscriberTimeout) {
clearTimeout(this._subscriberTimeout);
if (this.sub === null) return;
this._subscriberTimeout = setTimeout(() => {
debug('broadcast timeout');
this.emit('timeout');
this._requestPublisherUrl().catch(err => this.emit('error', err));
}, this.broadcastTimeoutMs);
}
}
_requestMissingEntries(count) {
assert(count > 0);
if (this._pendingMissing !== null) return this._pendingMissing;
if (this._subscriberTimeout === null) return Promise.resolve();
this.isFresh = false;
this.emit('stale', count);
return this._pendingMissing = new Promise((resolve, reject) => {
var options = this.entriesStreamOptions;
options.count = count;
var rpcstream = this.client.requestEntriesStream(this.lastLogIndex, options);
rpcstream.on('data', chunk => {
var logIndex = chunk.logIndex;
if (logIndex > this.lastLogIndex) {
if (!this.push(chunk)) {
rpcstream.pause();
this._pendingRpcStream = rpcstream;
this._pauseSubscriber();
}
if (chunk.isLogEntry || chunk.isLastChunk) {
this.lastLogIndex = logIndex;
}
}
})
.on('end', () => {
this._pendingMissing = this._pendingRpcStream = rpcstream = null;
resolve(this._flushAhead());
})
.once('error', err => {
this._pendingMissing = this._pendingRpcStream = null;
if (err.isOutOfOrder) {
debug(err.message);
resolve(this._flushAhead());
}
else this.emit('error', err);
});
});
}
/* Readable */
_read(/* size */) {
if (this._subscriberTimeout === null) {
debug('resuming streaming - ahead: %s lastLogIndex: %s', this.ahead.length, this.lastLogIndex);
const sub = this.sub;
sub.resume();
sub.subscribe(this[secretBuf$]);
this._subscriberTimeout = true;
this._refreshSubTimeout();
}
if (this._pendingRpcStream) {
debug('resuming missing entries streaming');
this._pendingRpcStream.resume();
this._pendingRpcStream = null;
}
}
/* Writable */
_write(updateRequest, encoding, callback) {
if (!isUpdateRequest(updateRequest)) {
return callback(new TypeError("updateRequest must be a buffer with requestId property"));
}
this.client.requestUpdate(updateRequest.requestId, updateRequest).then(index => {
if (index > this.lastUpdateLogIndex) {
this.lastUpdateLogIndex = index;
}
debug('written index: %s', index);
callback();
}, callback);
}
/* all buffered updates are being sent at once to speed up commit syncing
usually the updates will be accepted in the same order but there is no guarantee
the commit order will be retained once we hit connection problems
if this is an important issue, initialize subscriber with options:
{duplex: {writableHighWaterMark: 1}}
and pay attention to value returned by `writable.write` or use pipe API
*/
_writev(requests, callback) {
var client = this.client
, updateRequest
, promises = [];
try {
for(var i = 0, len = requests.length; i < len; ++i) {
updateRequest = requests[i].chunk;
if (!isUpdateRequest(updateRequest)) {
throw new TypeError("updateRequest must be a buffer with requestId property");
}
let pos = promises.push(
client.requestUpdate(updateRequest.requestId, updateRequest)
.then(index => {
if (index > this.lastUpdateLogIndex) {
this.lastUpdateLogIndex = index;
}
debug('written index: %d (%d of %d)', index, pos, len)
})
);
}
} catch(err) {
promises.push(Promise.reject(err));
}
Promise.all(promises).then(() => callback(), callback);
}
}
ZmqRaftSubscriber.ZmqRaftSubscriber = ZmqRaftSubscriber;
module.exports = exports = ZmqRaftSubscriber;