-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathcluster_configuration.js
324 lines (291 loc) · 7.75 KB
/
cluster_configuration.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
/*
* Copyright (c) 2016-2017 Rafał Michalski <[email protected]>
*/
"use strict";
const isArray = Array.isArray;
const { mergeMaps, majorityOf, parsePeers } = require('../utils/helpers');
/**
* ClusterConfiguration is a class to control peer membership, transition of peer membership and voting.
*
* new ClusterConfiguration(myPeerId, [[]]|{old:[], new:[]})
*
* @property {string} peerId
*
* @property {boolean} isTransitional
* @property {boolean} isSoleMaster
*
* @property {Map} ocluster
* @property {Map} ncluster
*
* @property {number} majority
* @property {number} ncMajority
*
* @property {Set} voted
* @property {Set} ncVoted
* @property {number} votes
* @property {number} ncVotes
*
* @property {Map} peers (a single/joint config peers map without myPeerId) - safe to be shared
*
* @property {Array} configAry -> (a single/joint config peers array)
**/
class ClusterConfiguration {
/**
* creates a new ClusterConfiguration instance
*
* cfg may be an array of peer descriptors or object with properties "old" and "new"
* containing arrays with old and new peer descriptors
*
* @param {string} myPeerId
* @param {Array|Object} cfg
* @return {ClusterConfiguration}
**/
constructor(myPeerId, cfg) {
this.peerId = myPeerId;
this.peers = new Map();
this.voted = new Set();
this.votes = 0;
this.ncVoted = new Set();
this.ncVotes = 0;
this.replace(cfg);
}
/**
* checks if peerId is a member of the cluster
*
* @param {string} peerId
* @return {boolean}
**/
isMember(peerId) {
return this.ocluster.has(peerId) || this.ncluster.has(peerId);
}
/**
* gets url of peerId or undefined
*
* @param {string} peerId
* @return {string|undefined}
**/
getUrl(peerId) {
return this.ocluster.get(peerId) || this.ncluster.get(peerId);
}
/**
* @property {boolean} isTransitional
**/
get isTransitional() {
return this.ncMajority !== 0;
}
/**
* @property {boolean} isSoleMaster
**/
get isSoleMaster() {
return this.peers.size === 0;
}
/**
* converts ClusterConfiguration instance to an array or plain object
*
* @return {Array|Object}
**/
serialize() {
var ocluster = Array.from(this.ocluster)
, ncluster = this.ncluster;
return ncluster.size === 0 ? ocluster
: {old: ocluster, new: Array.from(ncluster)};
}
/**
* converts ClusterConfiguration new cluster configuration to an array
*
* @return {Array}
**/
serializeNC() {
return Array.from(this.ncluster);
}
/**
* replaces current config with the new config
*
* cfg may be an array of peer descriptors or object with properties "old" and "new"
* containing arrays with old and new peer descriptors
*
* @param {Array|Object} cfg
**/
replace(cfg) {
if (isArray(cfg)) {
this.ocluster = parsePeers(cfg);
this.ncluster = new Map();
this.majority = majorityOf(this.ocluster.size);
this.ncMajority = 0;
}
else if (cfg === null || 'object' !== typeof cfg || !isArray(cfg.old) || !isArray(cfg.new)) {
throw new TypeError('ClusterConfiguration: argument parsing error');
}
else {
this.ocluster = parsePeers(cfg.old);
this.ncluster = parsePeers(cfg.new, this.ocluster);
this.majority = majorityOf(this.ocluster.size);
this.ncMajority = majorityOf(this.ncluster.size);
}
this._updatePeers();
}
/**
* joins current config with the new config returning transitional config
*
* returns null if join was impossible (current config was transitional)
*
* @param {Array} cfg
* @return {Object|null}
**/
join(cfg) {
if (this.ncMajority !== 0) return null;
var ocluster = this.ocluster
, ncluster = Array.from(parsePeers(cfg, ocluster));
ocluster = Array.from(ocluster);
if (ocluster.length === ncluster.length) {
if (ncluster.every((pair, idx) => ocluster[idx][0] === pair[0])) {
throw new Error("no change in cluster membership");
}
}
return {old: ocluster, new: ncluster};
}
/**
* clear voting metadata
**/
votingStart() {
this.voted.clear();
this.ncVoted.clear();
this.votes = 0;
this.ncVotes = 0;
}
/**
* add vote from a peer
*
* @param {string} peerId
* @param {boolean} voteGranted
**/
vote(peerId, voteGranted) {
var voted = this.voted;
if (this.ocluster.has(peerId)) {
if (!voted.has(peerId)) {
voted.add(peerId);
if (voteGranted) ++this.votes;
}
}
if (this.ncluster.has(peerId)) {
voted = this.ncVoted;
if (!voted.has(peerId)) {
voted.add(peerId);
if (voteGranted) ++this.ncVotes;
}
}
}
/**
* has majority already voted
*
* @return {boolean}
**/
majorityHasVoted() {
return this.voted.size >= this.majority && this.ncVoted.size >= this.ncMajority;
}
/**
* has voting been won
*
* @return {boolean}
**/
hasWonVoting() {
return this.votes >= this.majority && this.ncVotes >= this.ncMajority;
}
/**
* is majority of peers' match index larger or equal to logIndex
*
* @param {number} logIndex
* @param {Map} matchIndex peerId -> matchIndex
* @return {boolean}
**/
majorityHasLogIndex(logIndex, matchIndex) {
var majority = this.majority
, ncMajority = this.ncMajority
, ocluster = this.ocluster
, ncluster = this.ncluster
, peerId = this.peerId;
/* assuming me has logIndex in log */
if (ocluster.has(peerId)) --majority;
if (ncluster.has(peerId)) --ncMajority;
if (majority <= 0 && ncMajority <= 0) return true;
for(peerId of this.peers.keys()) {
if (matchIndex.get(peerId) >= logIndex) {
if (ocluster.has(peerId)) --majority;
if (ncluster.has(peerId)) --ncMajority;
if (majority <= 0 && ncMajority <= 0) return true;
}
}
return false;
}
/**
* create map from current other peers using factory function
*
* @param {Function} factory
* @return {Map}
**/
createOtherPeersMap(factory) {
return this.updateOtherPeersMap(new Map(), factory);
}
/**
* remove keys not found in other peers, reorder map content and fill missing keys with value
*
* @param {Map} map
* @param {*} value
* @return {Map}
**/
resetOtherPeersMap(map, value) {
var peerId
, peers = this.peers;
for(peerId of peers.keys()) {
let upd = map.get(peerId);
if (!map.delete(peerId)) upd = value;
map.set(peerId, upd);
}
for(peerId of map.keys()) {
if (!peers.has(peerId)) map.delete(peerId);
}
return map;
}
/**
* update map content with current other peers
*
* if factory is null or not provided it will only delete elements and not create new ones
*
* @param {Map} map
* @param {Function|null} factory (peerUrl, peerId) => {*}
* @param {Function} [destructor] (value, peerId) => {void}
* @return {Map}
**/
updateOtherPeersMap(map, factory, destructor) {
var peers = this.peers
, peerId;
for(peerId of map.keys()) {
if (!peers.has(peerId)) {
destructor && destructor(map.get(peerId), peerId);
map.delete(peerId);
}
}
if ('function' === typeof factory) {
for(peerId of peers.keys()) {
if (!map.has(peerId)) {
map.set(peerId, factory(peers.get(peerId), peerId));
}
}
}
return map;
}
/**
* updates peers
*
* used internally
**/
_updatePeers() {
var peers = this.peers;
peers.clear();
mergeMaps(peers, this.ocluster, this.ncluster);
this.configAry = Array.from(peers);
peers.delete(this.peerId);
}
}
ClusterConfiguration.prototype.toJSON = ClusterConfiguration.prototype.serialize;
exports = module.exports = ClusterConfiguration;