-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathserver.js
243 lines (217 loc) · 5.67 KB
/
server.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
var P = require('p-promise')
var Leader = require('./leader')
var Candidate = require('./candidate')
var Follower = require('./follower')
var Log = require('./log')
function Server(id, storage, stateMachine) {
this.id = id
this.role = null
this.log = new Log(storage, stateMachine)
this.requests = {}
this.peers = []
this.peerMap = {}
this.countVote = countVote.bind(this)
this.onAppendEntries = onAppendEntries.bind(this)
this.appendEntriesResponse = appendEntriesResponse.bind(this)
this.requestVoteResponse = requestVoteResponse.bind(this)
this.onChangeRole = onChangeRole.bind(this)
this.onExecuted = onExecuted.bind(this)
this.log.on('executed', this.onExecuted)
}
/*/
Changes the role of this server.
name: string - role to switch to
/*/
function onChangeRole(name) {
if (this.role) {
this.role.removeListener('changeRole', this.onChangeRole)
this.role.removeListener('appendEntries', this.onAppendEntries)
console.log('changeRole', this.id, this.role.name, name)
}
switch (name) {
case 'follower':
this.role = new Follower(this.log)
this.role.resetElectionTimeout()
break;
case 'candidate':
this.role = new Candidate(this.log)
this.role.resetElectionTimeout()
this.beginElection()
break;
case 'leader':
this.role = new Leader(
this.log,
this.peers.map(function (p) { return p.id })
)
this.role.noop()
break;
}
this.role.on('changeRole', this.onChangeRole)
this.role.on('appendEntries', this.onAppendEntries)
}
/*/
After an entry has executed, respond to the client if applicable.
index: Number - log index of entry
entry: Object - the requested entry
result: Anything - result of executing the entry on the stateMachine
/*/
function onExecuted(index, entry, result) {
console.log('executed', this.id, index, result)
var request = this.requests[index]
if (!request || !request.callback) { return }
request.callback(null, result)
delete this.requests[index]
}
/*/
Sends an appendEntries RPC to the given peer
peerId: Number - peer to send to
info: Object - an appendEntries request
/*/
function onAppendEntries(peerId, info) {
info.leaderId = this.id
var peer = this.peerMap[peerId]
if (!peer) { return }
// TODO: peer.appendEntries must be able to timeout
peer.appendEntries(info).then(entriesAppended.bind(this, peerId, info))
}
/*/
The response handler for appendEntries. attached in onAppendEntries.
peerId: Number,
request: Object - the full appendEntries 'info' object. see appendEntries,
response: {
term: Number,
success: Boolean
}
/*/
function entriesAppended(peerId, request, response) {
this.role.assertRole(response)
this.role.entriesAppended(peerId, request, response)
}
/*/
Start up as a follower.
peers: [
{
id: Number,
appendEntries: function (info) { return P() },
requestVote: function (info) { return P() }
}
]
/*/
Server.prototype.start = function (peers, role) {
role = role || 'follower'
this.peers = peers
for (var i = 0; i < peers.length; i++) {
var peer = peers[i]
this.peerMap[peer.id] = peer
}
this.log.load()
.then(onChangeRole.bind(this, role))
}
/*/
Broadcast requestVote RPCs to all of our peers
/*/
Server.prototype.beginElection = function () {
this.log.currentTerm++
var info = {
term: this.log.currentTerm,
candidateId: this.id,
lastLogIndex: this.log.lastIndex(),
lastLogTerm: this.log.lastTerm()
}
this.log.requestVote(info) // vote for self
for (var i = 0; i < this.peers.length; i++) {
this.peers[i].requestVote(info).then(this.countVote)
}
}
/*/
The response handler for requestVote
vote: {
id: Number,
term: Number,
success: Boolean (got the vote)
}
/*/
function countVote(vote) {
if (vote.term < this.log.currentTerm) {
return
}
this.role.assertRole(vote)
return this.role.countVote(vote, this.peers.length)
}
/*/
Raft protocol RPC
info: {
term: Number,
candidateId: Number,
lastLogIndex: Number,
lastLogTerm: Number
}
/*/
Server.prototype.requestVote = function (info) {
this.role.assertRole(info)
return this.role.requestVote(info)
.then(this.requestVoteResponse)
}
function requestVoteResponse(voteGranted) {
return { id: this.id, term: this.log.currentTerm, voteGranted: voteGranted }
}
/*/
Raft protocol RPC
info: {
term: Number,
leaderId: Number,
prevLogIndex: Number,
prevLogTerm: Number,
leaderCommit: Number,
entries: {
startIndex: Number,
values: [{ term: Number, op: {} }, ...]
}
}
/*/
Server.prototype.appendEntries = function (info) {
this.role.assertRole(info, 'appendEntries')
return this.role.appendEntries(info)
.then(this.appendEntriesResponse)
}
function appendEntriesResponse(success) {
return { term: this.log.currentTerm, success: success }
}
/*/
Request an entry be applied to the state machine. This is the "public" API.
entry: {
requestId: string
// anything else
}
callback: function (err, result) {}
/*/
Server.prototype.request = function (entry, callback) {
// TODO: look to see if the entry.requestId is "in the system"
var info = {
term: this.log.currentTerm,
leaderId: this.id,
prevLogIndex: this.log.lastIndex(),
prevLogTerm: this.log.lastTerm(),
leaderCommit: this.log.commitIndex,
entries: {
startIndex: this.log.lastIndex() + 1,
values: [{ term: this.log.currentTerm, op: entry }]
}
}
// If I'm not the leader this will return an error
this.role.request(info)
.then(
function () {
// TODO possibly a bloom filter on the entry.requestId
// TODO wrap callback in a "timeout function"
var index = info.entries.startIndex
this.requests[index] = { info: info, callback: callback }
}.bind(this),
function (err) {
callback(err)
}
)
}
Server.prototype.updateConfiguration = function (newConfig, callback) {
}
module.exports = Server