-
Notifications
You must be signed in to change notification settings - Fork 128
/
Copy pathsubscriber.js
774 lines (682 loc) · 20.4 KB
/
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
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
/*
* Kuzzle, a backend software, self-hostable and ready to use
* to power modern apps
*
* Copyright 2015-2022 Kuzzle
* mailto: support AT kuzzle.io
* website: http://kuzzle.io
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
"use strict";
const { Subscriber } = require("zeromq");
const protobuf = require("protobufjs");
const Long = require("long");
const debug = require("../util/debug")("kuzzle:cluster:sync");
const DocumentNotification = require("../core/realtime/notification/document");
const UserNotification = require("../core/realtime/notification/user");
const { has } = require("../util/safeObject");
const { fromKoncordeIndex } = require("../util/koncordeCompat");
/* eslint-disable sort-keys */
const stateEnum = Object.freeze({
BUFFERING: 1,
SANE: 2,
MISSING_HEARTBEAT: 3,
EVICTED: 4,
});
/* eslint-enable sort-keys */
// Handles messages received from other nodes
class ClusterSubscriber {
/**
* @constructor
* @param {ClusterNode} localNode
* @param {string} remoteNodeId - Remote node unique ID
* @param {string} remoteNodeIP - address of the distant node
*/
constructor(localNode, remoteNodeId, remoteNodeIP) {
// not to be confused with remote nodes
this.localNode = localNode;
this.remoteNodeIP = remoteNodeIP;
this.remoteNodeAddress = `tcp://${remoteNodeIP}:${this.localNode.config.ports.sync}`;
this.remoteNodeId = remoteNodeId;
// Used in debug mode when the node might be slower
this.remoteNodeEvictionPrevented = false;
this.socket = null;
this.protoroot = null;
// keeps track of received message ids from the remote node
this.lastMessageId = new Long(0, 0, true);
// A subscriber starts in the BUFFERING state, meaning it stores incoming
// sync messages without processing them, until it enters the SANE state
// This delays applying sync messages while the local node initializes
this.state = stateEnum.BUFFERING;
this.buffer = [];
// keeps track of the remote node heartbeats, and evicts it if no
// heartbeats have been received after some time
this.heartbeatTimer = null;
this.lastHeartbeat = Date.now();
this.heartbeatDelay = this.localNode.heartbeatDelay * 1.5;
// Messages handlers (quick translation between a topic name and its
// associated handler)
this.handlers = Object.freeze({
AddCollection: this.handleCollectionAddition,
AddIndex: this.handleIndexAddition,
ClusterWideEvent: this.handleClusterWideEvent,
DocumentNotification: this.handleDocumentNotification,
DumpRequest: this.handleDumpRequest,
Heartbeat: this.handleHeartbeat,
InvalidateProfile: this.handleProfileInvalidation,
InvalidateRole: this.handleRoleInvalidation,
NewAuthStrategy: this.handleNewAuthStrategy,
NewRealtimeRoom: this.handleNewRealtimeRoom,
NodeEvicted: this.handleNodeEviction,
NodePreventEviction: this.handleNodePreventEviction,
NodeShutdown: this.handleNodeShutdown,
RefreshIndexCache: this.handleRefreshIndexCache,
RefreshValidators: this.handleRefreshValidators,
RemoveAuthStrategy: this.handleAuthStrategyRemoval,
RemoveCollection: this.handleCollectionRemoval,
RemoveIndexes: this.handleIndexesRemoval,
RemoveRealtimeRoom: this.handleRealtimeRoomRemoval,
ResetSecurity: this.handleResetSecurity,
Shutdown: this.handleShutdown,
Subscription: this.handleSubscription,
Unsubscription: this.handleUnsubscription,
UserNotification: this.handleUserNotification,
});
}
/**
* Initializes this class, establishes a connection to the remote node and
* starts listening to it.
*/
async init() {
this.protoroot = await protobuf.load(`${__dirname}/protobuf/sync.proto`);
this.socket = new Subscriber();
this.socket.connect(this.remoteNodeAddress);
this.socket.subscribe();
this.heartbeatTimer = setInterval(
() => this.checkHeartbeat(),
this.heartbeatDelay
);
this.listen();
}
/**
* Starts subscribing to other nodes
* Do NOT wait this method: it's an infinite loop, it's not meant to ever
* return (unless the remote node has been evicted)
*/
async listen() {
while (this.state !== stateEnum.EVICTED) {
let topic;
let data;
try {
[topic, data] = await this.socket.receive();
} catch (e) {
if (this.state !== stateEnum.EVICTED) {
await this.evictNode({
broadcast: true,
reason: e.message,
});
}
return;
}
// do nothing is the node was evicted even if we already check
// for this in processData()
if (this.state === stateEnum.EVICTED) {
return;
}
if (this.state !== stateEnum.BUFFERING) {
await this.processData(topic.toString(), data);
} else {
this.buffer.push([topic.toString(), data]);
}
}
}
/**
* Plays all buffered sync messages, and switches this subscriber state from
* BUFFERING to SANE.
*
* @param {Long} lastMessageId
* @return {void}
*/
async sync(lastMessageId) {
this.lastMessageId = lastMessageId;
// copy the buffer for processing: new sync messages might be buffered
// while we apply older messages with async functions
while (this.buffer.length > 0) {
const _buffer = this.buffer;
this.buffer = [];
for (let i = 0; i < _buffer.length; i++) {
await this.processData(_buffer[i][0], _buffer[i][1]);
}
}
this.state = stateEnum.SANE;
}
/**
* Decodes an incoming message, and dispatches it.
* The topic name must match a protobuf message.
*
* /!\ This method MUST NEVER THROW
* It's awaited by this.listen(), which cannot be awaited (infinite loop),
* and it also cannot afford to attach a rejection handler everytime a message
* is received to prevent clogging the event loop with unnecessary promises
*
* @param {string} topic
* @param {Buffer} data
* @return {void}
*/
async processData(topic, data) {
if (this.state === stateEnum.EVICTED) {
return;
}
const decoder = this.protoroot.lookup(topic);
if (decoder === null) {
await this.evictNode({
broadcast: true,
reason: `received an invalid message from ${this.remoteNodeId} (unknown topic "${topic}")`,
});
return;
}
const message = decoder.toObject(decoder.decode(data));
if (!(await this.validateMessage(message))) {
return;
}
try {
// If we are receiving messages from a node,
// it means the node is alive so it should counts as an heartbeat
this.handleHeartbeat();
await this.handlers[topic].call(this, message);
} catch (e) {
this.localNode.evictSelf(
`Unable to process sync message (topic: ${topic}, message: ${JSON.stringify(
message
)}`,
e
);
}
}
async handleNodePreventEviction(message) {
this.remoteNodeEvictionPrevented = message.evictionPrevented;
}
/**
* Handles a heartbeat from the remote node
*
* @return {void}
*/
handleHeartbeat() {
this.lastHeartbeat = Date.now();
}
/**
* Handles a node eviction message.
*
* @param {Object} message - decoded NodeEvicted protobuf message
* @return {void}
*/
async handleNodeEviction(message) {
if (message.nodeId === this.localNode.nodeId) {
global.kuzzle.log.error(
`[CLUSTER] Node evicted by ${message.evictor}. Reason: ${message.reason}`
);
global.kuzzle.shutdown();
return;
}
await this.localNode.evictNode(message.nodeId, {
broadcast: false,
reason: message.reason,
});
}
/**
* Handles a node shutdown.
*
* @param {Object} message - decoded NodeShutdown protobuf message
* @return {void}
*/
async handleNodeShutdown(message) {
await this.localNode.evictNode(message.nodeId, {
broadcast: false,
reason: "Node is shutting down",
});
}
/**
* Handles messages about realtime room creations
*
* @param {Object} message - decoded NewRealtimeRoom protobuf message
* @return {void}
*/
handleNewRealtimeRoom(message) {
const { id, index, filter, messageId } = message;
const icpair = fromKoncordeIndex(index);
debug(
"New realtime room created by node %s (message: %d, room: %s, index: %s, collection: %s)",
this.remoteNodeId,
messageId,
id,
icpair.index,
icpair.collection
);
this.localNode.fullState.addRealtimeRoom(
id,
icpair.index,
icpair.collection,
JSON.parse(filter),
{
messageId,
nodeId: this.remoteNodeId,
subscribers: 0,
}
);
}
/**
* Handles messages about realtime subscriptions
*
* @param {Object} message - decoded Subscription protobuf message
* @return {void}
*/
handleSubscription(message) {
debug(
"New realtime subscription received from node %s (message: %d, room: %s)",
this.remoteNodeId,
message.messageId,
message.roomId
);
this.localNode.fullState.addRealtimeSubscription(
message.roomId,
this.remoteNodeId,
message.messageId
);
}
/**
* Handles messages about realtime room removal
*
* @param {Object} message - decoded RemoveRealtimeRoom protobuf message
* @return {void}
*/
handleRealtimeRoomRemoval(message) {
debug(
"Realtime room removal received from node %s (message: %d, room: %s)",
this.remoteNodeId,
message.messageId,
message.roomId
);
this.localNode.fullState.removeRealtimeRoom(
message.roomId,
this.remoteNodeId
);
}
/**
* Handles messages about user unsubscriptions
*
* @param {Object} message - decoded Unscription protobuf message
* @return {void}
*/
handleUnsubscription(message) {
debug(
"Realtime unsubscription received from node %s (message: %d, room: %s)",
this.remoteNodeId,
message.messageId,
message.roomId
);
this.localNode.fullState.removeRealtimeSubscription(
message.roomId,
this.remoteNodeId,
message.messageId
);
}
/**
* Handles messages about cluster-wide events
*
* @param {Object} message - decoded ClusterWideEvent protobuf message
* @return {void}
*/
handleClusterWideEvent(message) {
const payload = JSON.parse(message.payload);
this.localNode.eventEmitter.emit(message.event, payload);
}
/**
* Handles messages about document notifications
*
* @param {Object} message - decoded DocumentNotification protobuf message
* @return {void}
*/
async handleDocumentNotification(message) {
const notification = new DocumentNotification({
action: message.action,
collection: message.collection,
controller: message.controller,
index: message.index,
node: this.remoteNodeId,
protocol: message.protocol,
requestId: message.requestId,
result: JSON.parse(message.result),
scope: message.scope,
status: message.status,
timestamp: message.timestamp.toNumber(),
volatile: JSON.parse(message.volatile),
});
return global.kuzzle.ask(
"core:realtime:document:dispatch",
message.rooms,
notification
);
}
/**
* Handles messages about user notifications
*
* @param {Object} message - decoded UserNotification protobuf message
* @return {void}
*/
async handleUserNotification(message) {
const notification = new UserNotification({
action: message.action,
collection: message.collection,
controller: message.controller,
index: message.index,
node: this.remoteNodeId,
protocol: message.protocol,
result: JSON.parse(message.result),
status: message.status,
timestamp: message.timestamp.toNumber(),
user: message.user,
volatile: JSON.parse(message.volatile),
});
return global.kuzzle.ask(
"core:realtime:user:sendMessage",
message.room,
notification
);
}
/**
* Handles messages about new authentication strategies
*
* @param {Object} message - decoded NewAuthStrategy protobuf message
* @return {void}
*/
handleNewAuthStrategy(message) {
const { pluginName, strategy, strategyName } = message;
debug(
"New authentication strategy added by node %s (plugin: %s, strategy: %s)",
this.remoteNodeId,
pluginName,
strategyName
);
this.localNode.fullState.addAuthStrategy(message);
global.kuzzle.pluginsManager.registerStrategy(
pluginName,
strategyName,
strategy
);
}
/**
* Handles messages about authentication strategy removals
*
* @param {Object} message - decoded RemoveAuthStrategy protobuf message
* @return {void}
*/
handleAuthStrategyRemoval(message) {
const { pluginName, strategyName } = message;
debug(
"Authentication strategy removed by node %s (plugin: %s, strategy: %s)",
this.remoteNodeId,
pluginName,
strategyName
);
global.kuzzle.pluginsManager.unregisterStrategy(pluginName, strategyName);
this.localNode.fullState.removeAuthStrategy(strategyName);
}
/**
* Handles messages about security resets
*
* @return {void}
*/
async handleResetSecurity() {
debug("Security reset received from node %s", this.remoteNodeId);
await global.kuzzle.ask("core:security:profile:invalidate");
await global.kuzzle.ask("core:security:role:invalidate");
}
/**
* Handles a cross-nodes dump request
*
* @param {Object} message - decoded DumpRequest protobuf message
* @return {void}
*/
handleDumpRequest(message) {
debug("Dump generation request received from node %s", this.remoteNodeId);
global.kuzzle.dump(message.suffix);
}
/**
* Handles cluster-wide shutdown
* @return {void}
*/
handleShutdown() {
debug(
"Cluster-wide shutdown request received from node %s",
this.remoteNodeId
);
global.kuzzle.log.error(
`[CLUSTER] Cluster wide shutdown from ${this.remoteNodeId}`
);
global.kuzzle.shutdown();
}
/**
* Handles changes on document validators
*
* @return {void}
*/
async handleRefreshValidators() {
debug(
"Validators changed notification received from node %s",
this.remoteNodeId
);
await global.kuzzle.validation.curateSpecification();
}
/**
* Handles manual refresh of the index cache
*/
async handleRefreshIndexCache() {
debug(
"Index cache manually refresh received from node %s",
this.remoteNodeId
);
await global.kuzzle.ask("core:storage:public:cache:refresh", {
from: "cluster",
});
}
/**
* Invalidates a profile to force reloading it from the storage space
*
* @param {Object} message - decoded DumpRequest protobuf message
* @return {void}
*/
async handleProfileInvalidation(message) {
debug(
"Profile invalidation request received from node %s (profile: %s)",
this.remoteNodeId,
message.profileId
);
await global.kuzzle.ask(
"core:security:profile:invalidate",
message.profileId
);
}
/**
* Invalidates a role to force reloading it from the storage space
*
* @param {Object} message - decoded DumpRequest protobuf message
* @return {void}
*/
async handleRoleInvalidation(message) {
debug(
"Role invalidation request received from node %s (role: %s)",
this.remoteNodeId,
message.roleId
);
await global.kuzzle.ask("core:security:role:invalidate", message.roleId);
}
/**
* Adds a new index to the index cache
*
* @param {Object} message - decoded IndexCacheAdd protobuf message
* @return {void}
*/
async handleIndexAddition(message) {
const { index, scope } = message;
debug(
"New index added by node %s (scope: %s, index: %s)",
this.remoteNodeId,
scope,
index
);
await global.kuzzle.ask(`core:storage:${scope}:cache:addIndex`, index);
}
/**
* Removes indexes from the index cache
*
* @param {Object} message - decoded IndexCacheAdd protobuf message
* @return {void}
*/
async handleIndexesRemoval(message) {
const { indexes, scope } = message;
debug(
"Indexes removed by node %s (scope: %s, indexes: %s)",
this.remoteNodeId,
scope,
indexes
);
await global.kuzzle.ask(
`core:storage:${scope}:cache:removeIndexes`,
indexes
);
}
/**
* Adds a new collection to the index cache
*
* @param {Object} message - decoded IndexCacheAdd protobuf message
* @return {void}
*/
async handleCollectionAddition(message) {
const { collection, index, scope } = message;
debug(
"New collection added by node %s (scope: %s, index: %s, collection: %s)",
this.remoteNodeId,
scope,
index,
collection
);
await global.kuzzle.ask(
`core:storage:${scope}:cache:addCollection`,
index,
collection
);
}
/**
* Removes a collection from the index cache
*
* @param {Object} message - decoded IndexCacheAdd protobuf message
* @return {void}
*/
async handleCollectionRemoval(message) {
const { collection, index, scope } = message;
debug(
"Indexes removed by node %s (scope: %s, index: %s, collection: %s)",
this.remoteNodeId,
scope,
index,
collection
);
await global.kuzzle.ask(
`core:storage:${scope}:cache:removeCollection`,
index,
collection
);
}
/**
* Checks that we did receive a heartbeat from the remote node
* If a heartbeat is missing, we allow 1 heartbeat round for the remote node
* to recover, otherwise we evict it from the cluster.
*/
async checkHeartbeat() {
if (this.remoteNodeEvictionPrevented) {
// Fake the heartbeat while the node eviction prevention is enabled
// otherwise when the node eviction prevention is disabled
// the node will be evicted if it did not send a heartbeat before disabling the protection.
this.lastHeartbeat = Date.now();
return;
}
if (this.state === stateEnum.EVICTED) {
return;
}
const now = Date.now();
if (now - this.lastHeartbeat > this.heartbeatDelay) {
if (this.state === stateEnum.MISSING_HEARTBEAT) {
await this.evictNode({
broadcast: true,
reason: "heartbeat timeout",
});
} else {
this.state = stateEnum.MISSING_HEARTBEAT;
}
} else {
this.state = stateEnum.SANE;
}
}
/**
* Disconnects from the remote node, and frees all allocated resources.
*/
dispose() {
if (this.state === stateEnum.EVICTED) {
return;
}
this.state = stateEnum.EVICTED;
this.socket.close();
this.socket = null;
clearInterval(this.heartbeatTimer);
}
/**
* Checks that the received message is the one we expect.
* @param {Object} message - decoded protobuf message
* @return {boolean} false: the message must be discarded, true otherwise
*/
async validateMessage(message) {
if (!has(message, "messageId")) {
global.kuzzle.log.warn(
`Invalid message received from node ${this.remoteNodeId}. Evicting it.`
);
await this.evictNode({
broadcast: true,
reason: 'invalid message received (missing "messageId" field)',
});
return false;
}
if (
this.state === stateEnum.BUFFERING &&
this.lastMessageId.greaterThanOrEqual(message.messageId)
) {
return false;
}
this.lastMessageId = this.lastMessageId.add(1);
if (this.lastMessageId.notEquals(message.messageId)) {
await this.localNode.evictSelf(
`Node out-of-sync: ${
message.messageId - this.lastMessageId - 1
} messages lost from node ${this.remoteNodeId}`
);
return false;
}
return true;
}
async evictNode({ broadcast, reason }) {
this.state = stateEnum.EVICTED;
await this.localNode.evictNode(this.remoteNodeId, { broadcast, reason });
}
}
ClusterSubscriber.stateEnum = stateEnum;
module.exports = ClusterSubscriber;