-
Notifications
You must be signed in to change notification settings - Fork 5
/
broadcast_state_machine.js
227 lines (183 loc) · 6.46 KB
/
broadcast_state_machine.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
/*
* Copyright (c) 2016-2018 Rafał Michalski <[email protected]>
*/
"use strict";
const MAX_SAFE_INTEGER = Number.MAX_SAFE_INTEGER;
const path = require('path');
const { ZMQ_LINGER } = require('zeromq');
const { assertConstantsDefined
, createOptionsFactory
, validateIntegerOption } = require('../utils/helpers');
const { ZmqSocket } = require('../utils/zmqsocket');
const { FSM_LEADER, BROADCAST_HEARTBEAT_INTERVAL } = require('../common/constants');
assertConstantsDefined({
BROADCAST_HEARTBEAT_INTERVAL
}, 'number');
assertConstantsDefined({
FSM_LEADER
}, 'symbol');
const synchronize = require('../utils/synchronize');
const { createFramesProtocol } = require('../protocol');
const stateBroadcastProtocol = createFramesProtocol('StateBroadcast');
const FilePersistence = require('../common/file_persistence');
const REQUEST_URL_MATCH = ('*').charCodeAt(0);
const update$ = Symbol.for('update');
const debug = require('debug')('zmq-raft:broadcast-sm');
const createBroadcastSmOptions = createOptionsFactory({
secret: ''
, bindUrl: null
, lastApplied: 0
, broadcastHeartbeatInterval: BROADCAST_HEARTBEAT_INTERVAL
});
/**
* Implements the FilePersistence for state machine data.
*
* The properties being stored:
*
* - `lastApplied` {number} - a last applied log index to the state machine.
**/
class BroadcastStateMachine extends FilePersistence {
/**
* Creates new instance
*
* `filename` should contain path to a filename in some existing directory.
* `url` should contain a zmq url for a ZMQ_PUB socket.
*
* NOTE: for servers behind NAT `url` must be the PUBLIC address
* (visible from outside world) in this instance pass socket
* bind address in `options.bindUrl`.
*
* `options` may be one of:
*
* - `lastApplied` {number}: An initial persistent lastApplied value (default: 0).
* - `secret` {string}: A string to emit as first frame.
* - `bindUrl` {string}: An optional url address to bind ZMQ_PUB socket to.
* - `broadcastHeartbeatInterval` {number}: A broadcast heartbeat interval in milliseconds (default: 500).
*
* A new file will be created if `filename` does not exist.
*
* @param {string} filename
* @param {string} url
* @param {Object} options
* @return this
**/
constructor(filename, url, options) {
options = createBroadcastSmOptions(options);
super(filename, {lastApplied: validateIntegerOption(options, 'lastApplied', 0)});
this.url = url;
this.bindUrl = options.bindUrl || url;
debug('url: %s', this.url);
this.broadcastHeartbeatInterval = validateIntegerOption(options, 'broadcastHeartbeatInterval', 50);
debug('heartbeat interval: %s ms', this.broadcastHeartbeatInterval);
this._urlBuf = Buffer.from(url);
this._secretBuf = Buffer.from(options.secret || '');
this._broadcastInterval = null;
var fan = this._fan = new ZmqSocket('pub');
this._fan.setsockopt(ZMQ_LINGER, 2000);
/* StateMachineBase api */
this.on('raft-state', (state, term) => {
if (state === FSM_LEADER) {
debug('start heartbeats at term %s with last applied: %s', term, this.lastApplied);
this._refreshBroadcaster(term);
}
else if (this._broadcastInterval !== null) {
debug('stop heartbeats at term %s with last applied: %s', term, this.lastApplied);
this._clearBroadcaster();
}
});
this.on('client-request', (reply, msgType) => {
if (msgType.length === 1 && msgType[0] === REQUEST_URL_MATCH) {
reply(this._urlBuf);
}
});
}
/* FilePersistence api */
[Symbol.for('init')]() {
return new Promise((resolve, reject) => {
this._fan.bind(this.bindUrl, err => {
if (err) return reject(err);
debug('ready at: %s', this.bindUrl);
this.send = stateBroadcastProtocol.createSendFunctionFor(this._fan);
resolve();
});
});
}
[Symbol.for('apply')]({lastApplied}) {
if (lastApplied !== undefined) this.lastApplied = lastApplied;
}
[Symbol.for('validate')]({lastApplied}, withAllProperties) {
var data = {};
if (lastApplied !== undefined) data.lastApplied = validateLastApplied(lastApplied);
else if (withAllProperties) {
data.lastApplied = this.lastApplied;
}
return data;
}
close() {
clearInterval(this._broadcastInterval);
this._broadcastInterval = null;
var fan = this._fan;
this._fan = null;
return synchronize(this, () => {
if (fan) {
debug('closing');
this.send = null;
fan.close();
}
return this[Symbol.for('close')]();
});
}
/* StateMachineBase api */
applyEntries(entries, nextIndex, currentTerm, snapshot) {
return synchronize(this, () => {
const lastApplied = this.lastApplied;
if (nextIndex <= lastApplied
|| (snapshot && nextIndex !== snapshot.logIndex + 1)
|| (!snapshot && nextIndex !== lastApplied + 1)) {
throw new Error("BroadcastStateMachine: trying to apply entries out of order");
}
const numEntries = entries.length;
const lastIndex = nextIndex + numEntries - 1;
if (lastIndex === lastApplied) return lastApplied;
this._pauseBroadcaster();
return this[update$]({lastApplied: lastIndex}).then(() => {
if (this.isLeader) {
debug('broadcasting entries: %s at currentTerm: %s with nextIndex %s', numEntries, currentTerm, nextIndex);
this.send([this._secretBuf, currentTerm, lastIndex].concat(entries));
this._refreshBroadcaster(currentTerm);
}
return lastIndex;
});
});
}
/* own api */
get isLeader() {
return this._broadcastInterval !== null;
}
_refreshBroadcaster(term) {
if (this._broadcastInterval !== null) {
clearInterval(this._broadcastInterval);
}
this._broadcastInterval = setInterval(() => {
this.send([this._secretBuf, term, this.lastApplied]);
}, this.broadcastHeartbeatInterval);
}
_pauseBroadcaster() {
if (this._broadcastInterval !== null) {
clearInterval(this._broadcastInterval);
}
}
_clearBroadcaster() {
if (this._broadcastInterval !== null) {
clearInterval(this._broadcastInterval);
this._broadcastInterval = null;
}
}
}
module.exports = BroadcastStateMachine;
function validateLastApplied(lastApplied) {
if ('number' !== typeof lastApplied ||
lastApplied < 0 ||
lastApplied > MAX_SAFE_INTEGER) throw new Error("BroadcastStateMachine: invalid lastApplied");
return lastApplied;
}