@nsshunt/stsappframework 3.3.3 → 3.3.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -0,0 +1,444 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
26
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.GrpcServer = exports.raftState = void 0;
|
|
30
|
+
/* eslint @typescript-eslint/no-unused-vars: 0, @typescript-eslint/no-explicit-any: 0 */ // --> OFF
|
|
31
|
+
const uuid_1 = require("uuid");
|
|
32
|
+
const node_crypto_1 = __importDefault(require("node:crypto"));
|
|
33
|
+
const grpc = __importStar(require("@grpc/grpc-js"));
|
|
34
|
+
const protoLoader = __importStar(require("@grpc/proto-loader"));
|
|
35
|
+
const chalk_1 = __importDefault(require("chalk"));
|
|
36
|
+
const tiny_emitter_1 = require("tiny-emitter");
|
|
37
|
+
var raftState;
|
|
38
|
+
(function (raftState) {
|
|
39
|
+
raftState["idle"] = "idle";
|
|
40
|
+
raftState["follower"] = "follower";
|
|
41
|
+
raftState["candidate"] = "candidate";
|
|
42
|
+
raftState["leader"] = "leader";
|
|
43
|
+
})(raftState || (exports.raftState = raftState = {}));
|
|
44
|
+
class GrpcServer extends tiny_emitter_1.TinyEmitter {
|
|
45
|
+
#options;
|
|
46
|
+
#PROTO_PATH = __dirname + '/../grpc.proto';
|
|
47
|
+
#serverId = (0, uuid_1.v4)();
|
|
48
|
+
#currentLeaderId = null;
|
|
49
|
+
#currentTerm = 0;
|
|
50
|
+
#state = raftState.idle;
|
|
51
|
+
#serverClients = {};
|
|
52
|
+
#electionTimeoutMin = 500; // 3000
|
|
53
|
+
#electionTimeoutMax = 1000; // 3500
|
|
54
|
+
#heartBeatTimeoutVal = 100; // 2000
|
|
55
|
+
#electionTimeout = null;
|
|
56
|
+
#heartBeatTimeout = null;
|
|
57
|
+
#VotesMade = {};
|
|
58
|
+
#VotesReceived = {};
|
|
59
|
+
#leaderWorkerTimeout = null;
|
|
60
|
+
#packageDefinition;
|
|
61
|
+
#raft;
|
|
62
|
+
constructor(options) {
|
|
63
|
+
super();
|
|
64
|
+
this.#options = options;
|
|
65
|
+
if (this.#options.electionTimeoutMin) {
|
|
66
|
+
this.#electionTimeoutMin = this.#options.electionTimeoutMin;
|
|
67
|
+
}
|
|
68
|
+
if (this.#options.electionTimeoutMax) {
|
|
69
|
+
this.#electionTimeoutMax = this.#options.electionTimeoutMax;
|
|
70
|
+
}
|
|
71
|
+
if (this.#options.heartBeatTimeoutVal) {
|
|
72
|
+
this.#heartBeatTimeoutVal = this.#options.heartBeatTimeoutVal;
|
|
73
|
+
}
|
|
74
|
+
this.#packageDefinition = protoLoader.loadSync(this.#PROTO_PATH, { keepCase: true,
|
|
75
|
+
longs: String,
|
|
76
|
+
enums: String,
|
|
77
|
+
defaults: true,
|
|
78
|
+
oneofs: true
|
|
79
|
+
});
|
|
80
|
+
this.#raft = grpc.loadPackageDefinition(this.#packageDefinition).raft;
|
|
81
|
+
}
|
|
82
|
+
#GetClient = (serverIndex) => {
|
|
83
|
+
const server = this.#options.servers[serverIndex];
|
|
84
|
+
if (!this.#serverClients[serverIndex]) {
|
|
85
|
+
console.log(`GetClient for server on port: [${server.port}]`);
|
|
86
|
+
this.#serverClients[serverIndex] = new this.#raft.Raft(`${server.address}:${server.port}`, grpc.credentials.createInsecure());
|
|
87
|
+
}
|
|
88
|
+
return this.#serverClients[serverIndex];
|
|
89
|
+
/*
|
|
90
|
+
const options = {
|
|
91
|
+
//'grpc.primary_user_agent': 'my-custom-client/1.0',
|
|
92
|
+
'grpc.max_receive_message_length': -1, // No limit on receive message size
|
|
93
|
+
'grpc.enable_retries': true, // Enable retries for idempotent calls
|
|
94
|
+
'grpc.keepalive_time_ms': 120000, // Keepalive pings every 2 minutes
|
|
95
|
+
'verbose_trace': true
|
|
96
|
+
};
|
|
97
|
+
*/
|
|
98
|
+
};
|
|
99
|
+
#_AppendEntries = (AppendEntriesRequest) => {
|
|
100
|
+
//LogMessage(`_AppendEntries: [${JSON.stringify(AppendEntriesRequest)}]`);
|
|
101
|
+
const { term, leaderId, prevLogIndex, prevLogTerm, entries, leaderCommit } = AppendEntriesRequest;
|
|
102
|
+
if (term >= this.#currentTerm) {
|
|
103
|
+
if (this.#state.localeCompare(raftState.candidate) === 0) {
|
|
104
|
+
this.#LogMessage(chalk_1.default.rgb(252, 186, 3)(`New Leader has been elected! (while I am candidate) Term: [${term}], Leader ID: [${leaderId}]`));
|
|
105
|
+
this.#currentLeaderId = leaderId;
|
|
106
|
+
this.#ChangeState(raftState.follower);
|
|
107
|
+
this.emit('NewLeader', leaderId);
|
|
108
|
+
}
|
|
109
|
+
else if (this.#state.localeCompare(raftState.leader) === 0) {
|
|
110
|
+
this.#CancelLeaderWork();
|
|
111
|
+
this.emit('CancelLeader', leaderId);
|
|
112
|
+
this.#LogMessage(chalk_1.default.magenta(`--- *** ---`));
|
|
113
|
+
this.#LogMessage(chalk_1.default.magenta(`Leader Demoted. Term: [${term}], Leader ID: [${leaderId}]`));
|
|
114
|
+
this.#LogMessage(chalk_1.default.magenta(`New Leader has been elected! (while I am Leader) Term: [${term}], Leader ID: [${leaderId}]`));
|
|
115
|
+
this.#LogMessage(chalk_1.default.magenta(`--- *** ---`));
|
|
116
|
+
this.#currentLeaderId = leaderId;
|
|
117
|
+
this.#CancelHeartBeat();
|
|
118
|
+
this.#ChangeState(raftState.follower);
|
|
119
|
+
this.emit('NewLeader', leaderId);
|
|
120
|
+
}
|
|
121
|
+
else if (leaderId.localeCompare(this.#currentLeaderId) !== 0) {
|
|
122
|
+
this.#LogMessage(chalk_1.default.rgb(3, 211, 252)(`New Leader has been elected! (while I am follower) Term: [${term}], Leader ID: [${leaderId}]`));
|
|
123
|
+
this.#currentLeaderId = leaderId;
|
|
124
|
+
this.#ChangeState(raftState.follower);
|
|
125
|
+
this.emit('NewLeader', leaderId);
|
|
126
|
+
}
|
|
127
|
+
else {
|
|
128
|
+
//LogMessage(`[${serverId}] Received Heartbeat. Term: [${term}], Leader ID: [${leaderId}]`)
|
|
129
|
+
}
|
|
130
|
+
this.#ChangeTerm(term);
|
|
131
|
+
this.#StartElectionTimeout();
|
|
132
|
+
}
|
|
133
|
+
const AppendEntriesResponse = {
|
|
134
|
+
term: this.#currentTerm,
|
|
135
|
+
success: true
|
|
136
|
+
};
|
|
137
|
+
return AppendEntriesResponse;
|
|
138
|
+
};
|
|
139
|
+
#CancelLeaderWork = () => {
|
|
140
|
+
if (this.#leaderWorkerTimeout) {
|
|
141
|
+
this.#LogMessage(chalk_1.default.magenta(`Cancel Leader Work`));
|
|
142
|
+
clearTimeout(this.#leaderWorkerTimeout);
|
|
143
|
+
this.#leaderWorkerTimeout = null;
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
#StartLeaderWork = () => {
|
|
147
|
+
this.#LogMessage(chalk_1.default.magenta(`Start Leader Work`));
|
|
148
|
+
this.#CancelLeaderWork();
|
|
149
|
+
const leaderWorker = () => {
|
|
150
|
+
this.#LogMessage(chalk_1.default.rgb(50, 100, 20)(` .. doing Leader Work ...`));
|
|
151
|
+
this.#leaderWorkerTimeout = setTimeout(leaderWorker, 500);
|
|
152
|
+
};
|
|
153
|
+
leaderWorker();
|
|
154
|
+
};
|
|
155
|
+
#_AppendEntriesPreFlight = (AppendEntriesRequest) => {
|
|
156
|
+
this.#LogMessage(`_AppendEntriesPreFlight: [${JSON.stringify(AppendEntriesRequest)}]`);
|
|
157
|
+
const AppendEntriesResponse = {
|
|
158
|
+
term: this.#currentTerm,
|
|
159
|
+
success: true
|
|
160
|
+
};
|
|
161
|
+
return AppendEntriesResponse;
|
|
162
|
+
};
|
|
163
|
+
#_RequestVote = (RequestVoteRequest) => {
|
|
164
|
+
this.#LogMessage(`_RequestVote: [${JSON.stringify(RequestVoteRequest)}]`);
|
|
165
|
+
const { term, candidateId } = RequestVoteRequest;
|
|
166
|
+
if (this.#VotesMade[term]) {
|
|
167
|
+
this.#LogMessage(`Server: [${this.#serverId}] I have already voted this term. Term: [${term}]. I voted for CandidateId: [${this.#VotesMade[term]}] `);
|
|
168
|
+
const RequestVoteResponse = {
|
|
169
|
+
term: this.#currentTerm,
|
|
170
|
+
voteGranted: false
|
|
171
|
+
};
|
|
172
|
+
return RequestVoteResponse;
|
|
173
|
+
}
|
|
174
|
+
else if (term < this.#currentTerm) {
|
|
175
|
+
this.#LogMessage(`Server: [${this.#serverId}] Provided term is lower than my current term. CandidateId: [${candidateId}], Candidate Provided term: [${term}], My current Term: [${this.#currentTerm}]`);
|
|
176
|
+
const RequestVoteResponse = {
|
|
177
|
+
term: this.#currentTerm,
|
|
178
|
+
voteGranted: false
|
|
179
|
+
};
|
|
180
|
+
// I still record the fact that I have voted for this term, my answer was false
|
|
181
|
+
this.#VotesMade[term] = candidateId;
|
|
182
|
+
return RequestVoteResponse;
|
|
183
|
+
}
|
|
184
|
+
else {
|
|
185
|
+
this.#LogMessage(`Server: [${this.#serverId}] Vote Yes for term: [${term}], candidateId: [${candidateId}]`);
|
|
186
|
+
//@@ ???? why here - its a bottom of main loop anyway
|
|
187
|
+
this.#StartElectionTimeout();
|
|
188
|
+
const RequestVoteResponse = {
|
|
189
|
+
term: this.#currentTerm,
|
|
190
|
+
voteGranted: true
|
|
191
|
+
};
|
|
192
|
+
this.#VotesMade[term] = candidateId;
|
|
193
|
+
return RequestVoteResponse;
|
|
194
|
+
}
|
|
195
|
+
};
|
|
196
|
+
#_InstallSnapshot = (InstallSnapshotRequest) => {
|
|
197
|
+
this.#LogMessage(`_InstallSnapshot: [${JSON.stringify(InstallSnapshotRequest)}]`);
|
|
198
|
+
const InstallSnapshotResponse = {
|
|
199
|
+
term: 1
|
|
200
|
+
};
|
|
201
|
+
return InstallSnapshotResponse;
|
|
202
|
+
};
|
|
203
|
+
#AppendEntries = (call, callback) => {
|
|
204
|
+
callback(null, this.#_AppendEntries(call.request));
|
|
205
|
+
};
|
|
206
|
+
#RequestVote = (call, callback) => {
|
|
207
|
+
callback(null, this.#_RequestVote(call.request));
|
|
208
|
+
};
|
|
209
|
+
#InstallSnapshot = (call, callback) => {
|
|
210
|
+
callback(null, this.#_InstallSnapshot(call.request));
|
|
211
|
+
};
|
|
212
|
+
#AppendEntriesPreFlight = (call, callback) => {
|
|
213
|
+
callback(null, this.#_AppendEntriesPreFlight(call.request));
|
|
214
|
+
};
|
|
215
|
+
#GetServer = () => {
|
|
216
|
+
const server = new grpc.Server();
|
|
217
|
+
server.addService(this.#raft.Raft.service, {
|
|
218
|
+
AppendEntries: this.#AppendEntries,
|
|
219
|
+
RequestVote: this.#RequestVote,
|
|
220
|
+
InstallSnapshot: this.#InstallSnapshot,
|
|
221
|
+
AppendEntriesPreFlight: this.#AppendEntriesPreFlight
|
|
222
|
+
});
|
|
223
|
+
return server;
|
|
224
|
+
};
|
|
225
|
+
#LogError = (context, error) => {
|
|
226
|
+
//console.error(chalk.red(error));
|
|
227
|
+
};
|
|
228
|
+
#LogMessage = (message) => {
|
|
229
|
+
const useMessage = `[${this.#serverId}] [${this.#state}] [${this.#currentTerm}] [${this.#currentLeaderId}]: ${message}`;
|
|
230
|
+
if (this.#state.localeCompare(raftState.leader) === 0) {
|
|
231
|
+
console.log(chalk_1.default.green(useMessage));
|
|
232
|
+
}
|
|
233
|
+
else if (this.#state.localeCompare(raftState.candidate) === 0) {
|
|
234
|
+
console.log(chalk_1.default.yellow(useMessage));
|
|
235
|
+
}
|
|
236
|
+
else {
|
|
237
|
+
console.log(chalk_1.default.cyan(useMessage));
|
|
238
|
+
}
|
|
239
|
+
};
|
|
240
|
+
#ChangeState = (newState) => {
|
|
241
|
+
if (newState.localeCompare(this.#state) !== 0) {
|
|
242
|
+
this.#LogMessage(`ChangeState: Current State: [${this.#state}], New State: [${newState}]`);
|
|
243
|
+
this.#state = newState;
|
|
244
|
+
}
|
|
245
|
+
};
|
|
246
|
+
#ChangeTerm = (newTerm) => {
|
|
247
|
+
if (newTerm !== this.#currentTerm) {
|
|
248
|
+
this.#LogMessage(`ChangeTerm: Current Term: [${this.#currentTerm}], New Term: [${newTerm}]`);
|
|
249
|
+
this.#currentTerm = newTerm;
|
|
250
|
+
}
|
|
251
|
+
};
|
|
252
|
+
#StartElection = () => {
|
|
253
|
+
this.#LogMessage(`StartElection`);
|
|
254
|
+
this.#ChangeState(raftState.candidate);
|
|
255
|
+
this.#ChangeTerm(parseInt(this.#currentTerm) + 1);
|
|
256
|
+
this.#VotesReceived[this.#currentTerm] = {
|
|
257
|
+
[this.#serverId]: true
|
|
258
|
+
};
|
|
259
|
+
// Request votes from other servers
|
|
260
|
+
const RequestVoteRequest = {
|
|
261
|
+
term: this.#currentTerm,
|
|
262
|
+
candidateId: this.#serverId,
|
|
263
|
+
lastLogIndex: 0,
|
|
264
|
+
lastLogTerm: 0
|
|
265
|
+
};
|
|
266
|
+
const RequestVoteResponseCallBack = (clientId, RequestVoteResponse) => {
|
|
267
|
+
if (this.#state.localeCompare(raftState.candidate) !== 0) {
|
|
268
|
+
this.#LogMessage(`RequestVoteResponseCallBack(): No longer candidate. Ignoreing response.`);
|
|
269
|
+
return;
|
|
270
|
+
}
|
|
271
|
+
this.#LogMessage(RequestVoteResponse);
|
|
272
|
+
const { term, voteGranted } = RequestVoteResponse;
|
|
273
|
+
if (term > this.#currentTerm) {
|
|
274
|
+
this.#LogMessage(`RequestVoteResponseCallBack(): Response term greater than my current term.`);
|
|
275
|
+
//ChangeTerm(term);
|
|
276
|
+
this.#ChangeState(raftState.follower);
|
|
277
|
+
this.#StartElectionTimeout();
|
|
278
|
+
return;
|
|
279
|
+
}
|
|
280
|
+
this.#VotesReceived[this.#currentTerm][clientId] = voteGranted;
|
|
281
|
+
this.#LogMessage(`VotesReceived: [${JSON.stringify(this.#VotesReceived[this.#currentTerm])}]`);
|
|
282
|
+
// Check majority
|
|
283
|
+
let voteCount = 0;
|
|
284
|
+
Object.values(this.#VotesReceived[this.#currentTerm]).forEach((v) => {
|
|
285
|
+
if (v === true) {
|
|
286
|
+
voteCount++;
|
|
287
|
+
}
|
|
288
|
+
});
|
|
289
|
+
let majority = this.#options.servers.length;
|
|
290
|
+
if ((majority % 2) !== 0) {
|
|
291
|
+
majority = Math.floor(majority / 2) + 1;
|
|
292
|
+
}
|
|
293
|
+
if (voteCount >= majority) {
|
|
294
|
+
this.#LogMessage(chalk_1.default.bold(`---------[ Leader Elected ]---------`));
|
|
295
|
+
this.#currentLeaderId = this.#serverId;
|
|
296
|
+
this.#CancelElectionTimeout();
|
|
297
|
+
this.#ChangeState(raftState.leader);
|
|
298
|
+
this.emit('NewLeader', this.#serverId);
|
|
299
|
+
this.#SendHeartBeat();
|
|
300
|
+
this.#StartHeartBeat();
|
|
301
|
+
this.#StartLeaderWork();
|
|
302
|
+
}
|
|
303
|
+
};
|
|
304
|
+
// Start at +1 (i.e. not me)
|
|
305
|
+
for (let i = 1; i < this.#options.servers.length; i++) {
|
|
306
|
+
const server = this.#options.servers[i];
|
|
307
|
+
this.#GetClient(i).RequestVote(RequestVoteRequest, (error, RequestVoteResponse) => {
|
|
308
|
+
if (error) {
|
|
309
|
+
this.#LogError(`${server.name}.RequestVote`, error);
|
|
310
|
+
return;
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
RequestVoteResponseCallBack(server.name, RequestVoteResponse);
|
|
314
|
+
}
|
|
315
|
+
});
|
|
316
|
+
}
|
|
317
|
+
this.#StartElectionTimeout();
|
|
318
|
+
};
|
|
319
|
+
#CancelElectionTimeout = () => {
|
|
320
|
+
if (this.#electionTimeout) {
|
|
321
|
+
clearTimeout(this.#electionTimeout);
|
|
322
|
+
}
|
|
323
|
+
};
|
|
324
|
+
#StartElectionTimeout = () => {
|
|
325
|
+
if (this.#electionTimeout) {
|
|
326
|
+
clearTimeout(this.#electionTimeout);
|
|
327
|
+
}
|
|
328
|
+
this.#electionTimeout = setTimeout(() => {
|
|
329
|
+
this.#LogMessage(`StartElectionTimeout triggered`);
|
|
330
|
+
this.#StartElection();
|
|
331
|
+
}, node_crypto_1.default.randomInt(this.#electionTimeoutMin, this.#electionTimeoutMax));
|
|
332
|
+
};
|
|
333
|
+
#SendHeartBeat = () => {
|
|
334
|
+
const AppendEntriesRequest = {
|
|
335
|
+
term: this.#currentTerm,
|
|
336
|
+
leaderId: this.#serverId,
|
|
337
|
+
prevLogIndex: 0,
|
|
338
|
+
prevLogTerm: 0,
|
|
339
|
+
entries: [],
|
|
340
|
+
leaderCommit: 0
|
|
341
|
+
};
|
|
342
|
+
// Start at +1 (i.e. not me)
|
|
343
|
+
for (let i = 1; i < this.#options.servers.length; i++) {
|
|
344
|
+
this.#GetClient(i).AppendEntries(AppendEntriesRequest, (error, AppendEntriesResponse) => {
|
|
345
|
+
if (error) {
|
|
346
|
+
this.#LogError('AppendEntriesResponseCallBack', error);
|
|
347
|
+
return;
|
|
348
|
+
}
|
|
349
|
+
else {
|
|
350
|
+
//LogMessage(`AppendEntriesResponseCallBack(): Response: [${JSON.stringify(AppendEntriesResponse)}]`);
|
|
351
|
+
}
|
|
352
|
+
});
|
|
353
|
+
}
|
|
354
|
+
};
|
|
355
|
+
#CancelHeartBeat = () => {
|
|
356
|
+
if (this.#heartBeatTimeout) {
|
|
357
|
+
clearTimeout(this.#heartBeatTimeout);
|
|
358
|
+
}
|
|
359
|
+
};
|
|
360
|
+
#StartHeartBeat = () => {
|
|
361
|
+
if (this.#heartBeatTimeout) {
|
|
362
|
+
clearTimeout(this.#heartBeatTimeout);
|
|
363
|
+
}
|
|
364
|
+
this.#heartBeatTimeout = setTimeout(() => {
|
|
365
|
+
//LogMessage(`StartHeartBeat triggered: [${new Date()}]`)
|
|
366
|
+
this.#SendHeartBeat();
|
|
367
|
+
this.#StartHeartBeat();
|
|
368
|
+
}, this.#heartBeatTimeoutVal);
|
|
369
|
+
};
|
|
370
|
+
#StartRaftServer = (port) => {
|
|
371
|
+
if (this.#state.localeCompare(raftState.idle) === 0) {
|
|
372
|
+
this.#LogMessage(`STS Raft Server Starting`);
|
|
373
|
+
setTimeout(this.#StartElectionTimeout, 0);
|
|
374
|
+
}
|
|
375
|
+
};
|
|
376
|
+
/*
|
|
377
|
+
#SendSnapshot() {
|
|
378
|
+
const InstallSnapshotRequest = {
|
|
379
|
+
term: this.#currentTerm,
|
|
380
|
+
leaderId: this.#serverId,
|
|
381
|
+
lastIncludedIndex : 0,
|
|
382
|
+
lastIncludedTerm : 0,
|
|
383
|
+
data: null,
|
|
384
|
+
done : true
|
|
385
|
+
};
|
|
386
|
+
const InstallSnapshotResponseCallBack = (error: any, InstallSnapshotResponse : any) => {
|
|
387
|
+
if (error) {
|
|
388
|
+
this.#LogError('AppendEntriesResponseCallBack', error);
|
|
389
|
+
return;
|
|
390
|
+
} else {
|
|
391
|
+
//LogMessage(`AppendEntriesResponseCallBack(): Response: [${JSON.stringify(AppendEntriesResponse)}]`);
|
|
392
|
+
}
|
|
393
|
+
};
|
|
394
|
+
|
|
395
|
+
// Start at +1 (i.e. not me)
|
|
396
|
+
for (let i=1; i < this.#options.servers.length; i++) {
|
|
397
|
+
this.#GetClient(i).InstallSnapshot(InstallSnapshotRequest, InstallSnapshotResponseCallBack);
|
|
398
|
+
}
|
|
399
|
+
}
|
|
400
|
+
*/
|
|
401
|
+
#SendPreFlight = () => {
|
|
402
|
+
const AppendEntriesRequest = {
|
|
403
|
+
term: this.#currentTerm,
|
|
404
|
+
leaderId: this.#serverId,
|
|
405
|
+
prevLogIndex: 0,
|
|
406
|
+
prevLogTerm: 0,
|
|
407
|
+
entries: [],
|
|
408
|
+
leaderCommit: 0
|
|
409
|
+
};
|
|
410
|
+
const AppendEntriesPreFlightResponseCallBack = (error, AppendEntriesResponse) => {
|
|
411
|
+
if (error) {
|
|
412
|
+
this.#LogError('AppendEntriesResponseCallBack', error);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
else {
|
|
416
|
+
this.#LogMessage(`AppendEntriesPreFlightResponseCallBack(): Response: [${JSON.stringify(AppendEntriesResponse)}]`);
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
this.#LogMessage(`SendPreFlight - self`);
|
|
420
|
+
this.#GetClient(0).AppendEntriesPreFlight(AppendEntriesRequest, AppendEntriesPreFlightResponseCallBack);
|
|
421
|
+
};
|
|
422
|
+
StartServer = () => {
|
|
423
|
+
// If this is run as a script, start a server on an unused port
|
|
424
|
+
const routeServer = this.#GetServer();
|
|
425
|
+
routeServer.bindAsync(`0.0.0.0:${this.#options.listenPort}`, grpc.ServerCredentials.createInsecure(), (error, port) => {
|
|
426
|
+
//routeServer.start();
|
|
427
|
+
this.#LogMessage(`Server Started, Port: [${port}]`);
|
|
428
|
+
const deadline = new Date();
|
|
429
|
+
deadline.setSeconds(deadline.getSeconds() + 5); // Set a 5-second timeout
|
|
430
|
+
this.#GetClient(0).waitForReady(deadline, (err) => {
|
|
431
|
+
if (err) {
|
|
432
|
+
console.error('Self Client not ready:', err);
|
|
433
|
+
}
|
|
434
|
+
else {
|
|
435
|
+
console.log('Self Client ready!');
|
|
436
|
+
this.#SendPreFlight();
|
|
437
|
+
setTimeout(() => this.#StartRaftServer(port), 5000);
|
|
438
|
+
}
|
|
439
|
+
});
|
|
440
|
+
});
|
|
441
|
+
};
|
|
442
|
+
}
|
|
443
|
+
exports.GrpcServer = GrpcServer;
|
|
444
|
+
//# sourceMappingURL=grpcServer.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grpcServer.js","sourceRoot":"","sources":["../../src/grpc/grpcServer.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,yFAAyF,CAAE,UAAU;AACrG,+BAAoC;AAEpC,8DAAiC;AAEjC,oDAAsC;AAEtC,gEAAkD;AAGlD,kDAA0B;AAG1B,+CAA2C;AAE3C,IAAY,SAKX;AALD,WAAY,SAAS;IACjB,0BAAa,CAAA;IACb,kCAAqB,CAAA;IACrB,oCAAuB,CAAA;IACvB,8BAAiB,CAAA;AACrB,CAAC,EALW,SAAS,yBAAT,SAAS,QAKpB;AAuBD,MAAa,UAAW,SAAQ,0BAAW;IACvC,QAAQ,CAAqB;IAC7B,WAAW,GAAG,SAAS,GAAG,gBAAgB,CAAC;IAC3C,SAAS,GAAW,IAAA,SAAM,GAAE,CAAC;IAC7B,gBAAgB,GAAkB,IAAI,CAAC;IACvC,YAAY,GAAW,CAAC,CAAC;IACzB,MAAM,GAAc,SAAS,CAAC,IAAI,CAAC;IACnC,cAAc,GAAwB,EAAG,CAAC;IAE1C,mBAAmB,GAAW,GAAG,CAAC,CAAC,OAAO;IAC1C,mBA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AAC,WAAW,IAAI,CAAC,QAAQ,CAAC,UAAU,EAAE,EAAE,IAAI,CAAC,iBAAiB,CAAC,cAAc,EAAE,EAAE,CAAC,KAAmB,EAAE,IAAY,EAAE,EAAE;YACxI,wBAAwB;YACxB,IAAI,CAAC,WAAW,CAAC,0BAA0B,IAAI,GAAG,CAAC,CAAC;YAEpD,MAAM,QAAQ,GAAG,IAAI,IAAI,EAAE,CAAC;YAC5B,QAAQ,CAAC,UAAU,CAAC,QAAQ,CAAC,UAAU,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,yBAAyB;YAEzE,IAAI,CAAC,UAAU,CAAC,CAAC,CAAC,CAAC,YAAY,CAAC,QAAQ,EAAE,CAAC,GAAQ,EAAE,EAAE;gBACnD,IAAI,GAAG,EAAE,CAAC;oBACN,OAAO,CAAC,KAAK,CAAC,wBAAwB,EAAE,GAAG,CAAC,CAAC;gBACjD,CAAC;qBAAM,CAAC;oBACJ,OAAO,CAAC,GAAG,CAAC,oBAAoB,CAAC,CAAC;oBAClC,IAAI,CAAC,cAAc,EAAE,CAAC;oBACtB,UAAU,CAAC,GAAG,EAAE,CAAC,IAAI,CAAC,gBAAgB,CAAC,IAAI,CAAC,EAAE,IAAI,CAAC,CAAC;gBACxD,CAAC;YACL,CAAC,CAAC,CAAC;QACP,CAAC,CAAC,CAAC;IACP,CAAC,CAAA;CACJ;AA7bD,gCA6bC"}
|
package/package.json
CHANGED
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import { TinyEmitter } from "tiny-emitter";
|
|
2
|
+
export declare enum raftState {
|
|
3
|
+
idle = "idle",
|
|
4
|
+
follower = "follower",
|
|
5
|
+
candidate = "candidate",
|
|
6
|
+
leader = "leader"
|
|
7
|
+
}
|
|
8
|
+
export interface IClusterServerDetails {
|
|
9
|
+
id: string;
|
|
10
|
+
uri: string;
|
|
11
|
+
grpcClient: any;
|
|
12
|
+
}
|
|
13
|
+
export interface IGrpcServerConfig {
|
|
14
|
+
port: number;
|
|
15
|
+
address: string;
|
|
16
|
+
name: string;
|
|
17
|
+
id: string;
|
|
18
|
+
}
|
|
19
|
+
export interface IGrpcServerOptions {
|
|
20
|
+
listenPort: number;
|
|
21
|
+
servers: IGrpcServerConfig[];
|
|
22
|
+
electionTimeoutMin?: number;
|
|
23
|
+
electionTimeoutMax?: number;
|
|
24
|
+
heartBeatTimeoutVal?: number;
|
|
25
|
+
}
|
|
26
|
+
export declare class GrpcServer extends TinyEmitter {
|
|
27
|
+
#private;
|
|
28
|
+
constructor(options: IGrpcServerOptions);
|
|
29
|
+
StartServer: () => void;
|
|
30
|
+
}
|
|
31
|
+
//# sourceMappingURL=grpcServer.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"grpcServer.d.ts","sourceRoot":"","sources":["../../src/grpc/grpcServer.ts"],"names":[],"mappings":"AAaA,OAAO,EAAE,WAAW,EAAE,MAAM,cAAc,CAAC;AAE3C,oBAAY,SAAS;IACjB,IAAI,SAAS;IACb,QAAQ,aAAa;IACrB,SAAS,cAAc;IACvB,MAAM,WAAW;CACpB;AAED,MAAM,WAAW,qBAAqB;IAClC,EAAE,EAAE,MAAM,CAAA;IACV,GAAG,EAAE,MAAM,CAAA;IACX,UAAU,EAAE,GAAG,CAAC;CACnB;AAED,MAAM,WAAW,iBAAiB;IAC9B,IAAI,EAAE,MAAM,CAAA;IACZ,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,EAAE,MAAM,CAAA;IACZ,EAAE,EAAE,MAAM,CAAA;CACb;AAED,MAAM,WAAW,kBAAkB;IAC/B,UAAU,EAAE,MAAM,CAAA;IAClB,OAAO,EAAE,iBAAiB,EAAE,CAAA;IAC5B,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,kBAAkB,CAAC,EAAE,MAAM,CAAA;IAC3B,mBAAmB,CAAC,EAAE,MAAM,CAAA;CAC/B;AAED,qBAAa,UAAW,SAAQ,WAAW;;gBAsB3B,OAAO,EAAE,kBAAkB;IAkZvC,WAAW,aAoBV;CACJ"}
|