-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraft.mjs
175 lines (158 loc) · 4.57 KB
/
raft.mjs
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
import { nodeId } from "./nodeid.mjs";
import fetch from "node-fetch";
import { promiseTimeout } from "./timeoutPromise.mjs";
const HEARTBEAT = 120;
const electionTimeout = () => timing(150, 300);
const blue = (str) => `\x1b[34m${str}\x1b[89m\x1b[0m`;
const orange = (str) => `\x1b[93m${str}\x1b[39m\x1b[0m`;
const green = (str) => `\x1b[32m${str}\x1b[89m\x1b[0m`;
const q = async (url) =>
await result(
await fetch(url).catch((err) => {
return { status: 500, error: err.code, ok: false };
})
);
const result = async (res) =>
res.ok
? { status: res.status, data: await res.json() }
: { status: res.status, error: res.error };
const timing = (min, max) => Math.floor(Math.random() * (max - min) + min);
const askForVote = (term) => async (node) => {
const id = nodeId(node);
// console.log(blue(`asking for vote on node [${id}](${node})`));
const { error, data } = await promiseTimeout(
HEARTBEAT,
q(`${node}/vote/${term}/${id}`)
).catch((error) => ({ error }));
if (error) {
return { vote: false, id, term };
}
// console.log(green(`result of asking for vote: ${JSON.stringify(data)}`));
// console.log(id, !error);
const { result: vote } = data;
return { vote, id, term };
};
// [object] => boolean
const countMajorityVotes = (total, o) => {
const _votes = o
.filter(({ vote }) => vote)
.map(({ id }) => id)
.reduce((c, p) => [p, ...c], []);
const votes = _votes.reduce((a, c) => a + 1, 0);
const majority = votes / total >= 0.5;
// console.log("counting", _votes, _votes.length);
// console.log(votes, total, votes / total, majority);
return majority;
};
const noSelf = (id) => (node) => nodeId(node) !== id;
export const Raft = (url, nodes) => {
const Id = nodeId(url);
// const Ids = nodes
// .map((node) => ({ [nodeId(node)]: node }))
// .reduce((a, c) => ({ ...a, ...c }), {});
const States = ["Follower", "Candidate", "Leader"];
let electionCountdown;
let termsVotes = {};
let term = 0;
let state = 0;
let heardFromLeader = false;
let heartbeatInterval;
const askForVotes = async (term) => nodes.map(askForVote(term));
// append entries
const heartbeat = async () => {
console.log("\x1Bc");
console.log(`${Id}: 💓 [${term}]`);
const others = nodes.filter(noSelf(Id));
return await Promise.all(
others.map((node) =>
q(`${node}/append/${term}/${Id}`).catch(console.error)
)
);
};
const candidate = async () => {
state = 1;
let _term = term;
const majority = countMajorityVotes(
nodes.length,
await Promise.all(await askForVotes(_term))
);
if (majority) {
// console.log("majority found for term", _term, Id);
if (_term === term) {
state = 2;
}
} else {
// console.log("not voted in");
state = 0;
}
return majority;
};
const vote = (term, id) => {
// console.debug("voting in progress", term, termsVotes);
resetElectionTimer();
console.log("vote: ", !termsVotes[term], termsVotes);
if (!termsVotes[term]) {
// console.log(`Y ${term} ${id}`);
// note that you voted for this term
termsVotes[term] = true;
return true;
} else {
// console.log(`N ${term} ${id}`);
// do not vote
return false;
}
};
const election = async () => {
if (!heardFromLeader && state === 0) {
term += 1;
console.debug(
orange(
"leader election timeout exceeded, candidating now for term",
term
)
);
const win = await candidate(term);
if (win) {
// console.log("🍾", win);
heartbeatInterval = setInterval(heartbeat, HEARTBEAT);
clearInterval(electionCountdown);
}
}
};
const resetElectionTimer = () => {
if (electionCountdown) {
clearInterval(electionCountdown);
}
electionCountdown = setInterval(election, electionTimeout());
};
return {
init: () => {
console.log(`started raft with id: [${Id}](${url})`);
resetElectionTimer();
setInterval(() => {
// console.log("!");
heardFromLeader = false;
}, electionTimeout());
},
reset: (_term) => {
heardFromLeader = true;
if (_term > term) {
term = _term;
}
resetElectionTimer();
},
voteReset: (_term) => {
state = 0;
clearInterval(heartbeatInterval);
if (_term > term) {
term = _term;
}
resetElectionTimer();
},
state: () => ({
state: States[state],
term,
}),
wouldVoteinTerm: (term, id) => vote(term, id),
};
};