-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol_node.go
More file actions
494 lines (396 loc) · 11 KB
/
Copy pathprotocol_node.go
File metadata and controls
494 lines (396 loc) · 11 KB
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
package paxos
import "time"
type node struct {
id int
clusterMembers []int
// normally, they say nodes are either proposers or acceptors or learners
// that doesn't mean we have different types of nodes necessarily, we can have a single type of nodes that can play all roles, they're more like hats, not strictly roles
// ---- ACCEPTOR state ----
promisedBallot Ballot // highest ballot promised
acceptedProposal *proposal
// ---- PROPOSER state (optional) ----
round *proposerRound
lastCounter int
retryConfig retryConfig
// ---- LEARNER state (...roughly) ----
chosenValue *string
// -- reusable output buffers --
actions []nodeAction
traces []TraceEvent
}
// nodeEvents are triggers that happen to a node, coming from the outside world: proposalTriggered, messageReceived, retryTimerFired...
// the main takeaway is the signature of:
// func (n *node) Handle(event nodeEvent) nodeOutput
//
// that is, based on nodeEvent(s), we will (potentially) decide to take nodeAction(s) or record traceEvetn(s)
// example: based on a messageReceived that is a Prepare request from a proposer, the node might want to perform the action that will sendTo a promise to a proposer
// the caller of node.Handle(event) is responsible for interacting with the outside world and performing the node's decided next actions. I added this abstraction / decoupling layer because I needed to have both:
// 1- a normal in-memory, non-deteministic simulation run time
// 2- a deterministic scheduler for testing (testing this with timers and goroutiens is just ugly)
// but also it turned out to be more elegant!
// tldr: protocol_node.go is the protocol logic, with a state-machine-like shape
type nodeEvent interface {
isNodeEvent()
}
// NOTE: the algorithm doesn't really specify what triggers proposals
// which signals that liveness is really achieved via external
// assumptions (e.g. there already exists a proposal...) unlike safety which _is_ guaranteed by the protocol under its failure modes (assuming it's correctly implemented)
type proposalTriggered struct {
value string
}
func (proposalTriggered) isNodeEvent() {}
type messageReceived struct {
from int
msg Message
}
func (messageReceived) isNodeEvent() {}
type retryTimerFired struct {
ballot Ballot
}
func (retryTimerFired) isNodeEvent() {}
type nodeAction interface {
isNodeAction()
}
type nodeOutput struct {
actions []nodeAction
traces []TraceEvent
}
type sendMessage struct {
to int
msg Message
}
func (sendMessage) isNodeAction() {}
type resetRetryTimer struct {
ballot Ballot
after time.Duration
}
func (resetRetryTimer) isNodeAction() {}
type stopRetryTimer struct{}
func (stopRetryTimer) isNodeAction() {}
type TraceEvent interface {
isTraceEvent()
}
type ValueLearned struct {
Value string
}
func (ValueLearned) isTraceEvent() {}
type RoundStarted struct {
Ballot Ballot
Value string
}
func (RoundStarted) isTraceEvent() {}
type RoundRetried struct {
Ballot Ballot
Attempt int
NextAfter time.Duration
}
func (RoundRetried) isTraceEvent() {}
type RoundPreempted struct {
Current Ballot
Higher Ballot
}
func (RoundPreempted) isTraceEvent() {}
type RoundAbandoned struct {
Ballot Ballot
}
func (RoundAbandoned) isTraceEvent() {}
type PromiseQuorumReached struct {
Ballot Ballot
Value string
CarriedForward bool
}
func (PromiseQuorumReached) isTraceEvent() {}
type proposerRound struct {
promisers map[int]struct{}
accepters map[int]struct{}
highestAccepted *proposal
ballot Ballot
value string
retry retryState
}
type retryConfig struct {
baseTimeout time.Duration
maxDelay time.Duration
maxRetries int
}
type retryState struct {
currentTimeout time.Duration
retryCount int
}
func newNode(id int, clusterMembers []int, retryConfig retryConfig) *node {
return &node{
id: id,
clusterMembers: append([]int(nil), clusterMembers...),
retryConfig: retryConfig,
}
}
func (n *node) Handle(event nodeEvent) nodeOutput {
n.actions = n.actions[:0]
n.traces = n.traces[:0]
switch event := event.(type) {
case proposalTriggered:
n.startProposal(event.value, nil)
case messageReceived:
n.handleMessage(event.from, event.msg)
case retryTimerFired:
n.handleRetryTimer(event.ballot)
}
actions := append([]nodeAction(nil), n.actions...)
traces := append([]TraceEvent(nil), n.traces...)
n.actions = n.actions[:0]
n.traces = n.traces[:0]
return nodeOutput{
actions: actions,
traces: traces,
}
}
func (n *node) handleMessage(from int, msg Message) {
switch msg := msg.(type) {
case Prepare:
n.handlePrepare(from, msg)
case Promise:
n.handlePromise(from, msg)
case Accept:
n.handleAccept(from, msg)
case Accepted:
n.handleAccepted(from, msg)
case Reject:
n.handleReject(msg)
case Decided:
n.handleDecided(msg)
}
}
func (n *node) sendTo(to int, msg Message) {
n.actions = append(n.actions, sendMessage{to: to, msg: msg})
}
func (n *node) sendToAll(msg Message) {
for _, id := range n.clusterMembers {
if id != n.id {
n.sendTo(id, msg)
}
}
}
func (n *node) clusterQuorum() int {
return len(n.clusterMembers)/2 + 1
}
func (n *node) resetRetryTimer(ballot Ballot, after time.Duration) {
n.actions = append(n.actions, resetRetryTimer{ballot: ballot, after: after})
}
func (n *node) stopRetryTimer() {
n.actions = append(n.actions, stopRetryTimer{})
}
func (n *node) recordTrace(event TraceEvent) {
n.traces = append(n.traces, event)
}
func (n *node) startProposal(value string, above *Ballot) {
newBallot := n.startProposalRound(value, above)
n.resetRetryTimer(newBallot, n.round.retry.currentTimeout)
}
func (n *node) startProposalRound(value string, above *Ballot) Ballot {
// we need the ballot to be higher than any we've seen, which leads to this dance:
updatedCounter := n.lastCounter + 1
if above != nil && above.Counter+1 > updatedCounter {
updatedCounter = above.Counter + 1
}
if n.promisedBallot.Counter+1 > updatedCounter {
updatedCounter = n.promisedBallot.Counter + 1
}
n.lastCounter = updatedCounter
newBallot := Ballot{
Counter: updatedCounter,
ProposerID: n.id,
}
n.round = &proposerRound{
promisers: make(map[int]struct{}),
accepters: make(map[int]struct{}),
ballot: newBallot,
value: value,
retry: retryState{currentTimeout: n.retryConfig.baseTimeout},
}
n.recordTrace(RoundStarted{Ballot: newBallot, Value: value})
n.promisedBallot = newBallot
n.round.promisers[n.id] = struct{}{}
if n.acceptedProposal != nil {
n.round.highestAccepted = n.acceptedProposal
}
n.sendToAll(Prepare{
from: n.id,
proposedBallot: newBallot,
})
return newBallot
}
func (n *node) resetRetryBackoff() {
state := n.round.retry
cfg := n.retryConfig
state.currentTimeout = cfg.baseTimeout
state.retryCount = 0
n.round.retry = state
n.resetRetryTimer(n.round.ballot, state.currentTimeout)
}
func (n *node) handleRetryTimer(ballot Ballot) {
if n.round == nil || ballot.cmp(n.round.ballot) != 0 {
return
}
state := n.round.retry
cfg := n.retryConfig
if state.retryCount == cfg.maxRetries {
n.recordTrace(RoundAbandoned{Ballot: n.round.ballot})
n.round = nil
n.stopRetryTimer()
return
}
state.currentTimeout *= 2
if state.currentTimeout > cfg.maxDelay {
state.currentTimeout = cfg.maxDelay
}
state.retryCount++
n.round.retry = state
n.recordTrace(RoundRetried{
Ballot: n.round.ballot,
Attempt: state.retryCount,
NextAfter: state.currentTimeout,
})
n.resetRetryTimer(n.round.ballot, state.currentTimeout)
if len(n.round.promisers) < n.clusterQuorum() {
n.sendToAll(Prepare{
from: n.id,
proposedBallot: n.round.ballot,
})
} else {
n.sendToAll(Accept{
from: n.id,
proposedBallot: n.round.ballot,
proposedValue: n.round.value,
})
}
}
func (n *node) handlePrepare(from int, msg Prepare) {
if msg.proposedBallot.cmp(n.promisedBallot) >= 0 {
n.promisedBallot = msg.proposedBallot
n.sendTo(from, Promise{
from: n.id,
promisedBallot: msg.proposedBallot,
acceptedProposal: n.acceptedProposal,
})
return
}
n.sendTo(from, Reject{
from: n.id,
promisedBallot: n.promisedBallot,
})
}
func (n *node) handlePromise(from int, msg Promise) {
round := n.round
if round == nil {
return
}
c := msg.promisedBallot.cmp(round.ballot)
// c > 0: should arrive as a Reject, not a Promise
// c < 0: stale, ignore
if c != 0 {
return
}
if _, ok := round.promisers[from]; ok {
// be careful not to reset the timer for duplicate promises, that's fake liveness / progress
return
}
n.resetRetryBackoff()
round.promisers[from] = struct{}{}
if msg.acceptedProposal != nil {
incoming := msg.acceptedProposal
if round.highestAccepted == nil || round.highestAccepted.ballot.cmp(incoming.ballot) < 0 {
round.highestAccepted = incoming
}
}
if len(round.promisers) == n.clusterQuorum() {
// any acceptor already accepted a value? we must propose it then
carriedForward := false
if round.highestAccepted != nil {
round.value = round.highestAccepted.value
carriedForward = true
}
if round.ballot.cmp(n.promisedBallot) >= 0 {
n.acceptedProposal = &proposal{
ballot: round.ballot,
value: round.value,
}
round.accepters[n.id] = struct{}{}
}
n.recordTrace(PromiseQuorumReached{
Ballot: round.ballot,
Value: round.value,
CarriedForward: carriedForward,
})
n.sendToAll(Accept{
from: n.id,
proposedBallot: round.ballot,
proposedValue: round.value,
})
}
}
func (n *node) handleAccept(from int, msg Accept) {
if msg.proposedBallot.cmp(n.promisedBallot) >= 0 {
n.acceptedProposal = &proposal{
ballot: msg.proposedBallot,
value: msg.proposedValue,
}
n.promisedBallot = msg.proposedBallot
n.sendTo(from, Accepted{
from: n.id,
promisedBallot: n.promisedBallot,
acceptedProposal: n.acceptedProposal,
})
return
}
n.sendTo(from, Reject{
from: n.id,
promisedBallot: n.promisedBallot,
})
}
func (n *node) handleAccepted(from int, msg Accepted) {
round := n.round
if round == nil {
return
}
if msg.promisedBallot.cmp(round.ballot) != 0 {
return
}
if _, ok := round.accepters[from]; ok {
return
}
n.resetRetryBackoff()
round.accepters[from] = struct{}{}
if len(round.accepters) == n.clusterQuorum() {
n.sendToAll(Decided{
value: round.value,
})
n.chosenValue = &round.value
n.recordTrace(ValueLearned{Value: round.value})
n.round = nil
n.stopRetryTimer()
}
}
func (n *node) handleReject(msg Reject) {
round := n.round
if round == nil {
return
}
if msg.promisedBallot.cmp(round.ballot) <= 0 {
return
}
n.recordTrace(RoundPreempted{Current: round.ballot, Higher: msg.promisedBallot})
n.startProposal(round.value, &msg.promisedBallot)
}
func (n *node) handleDecided(msg Decided) {
if n.chosenValue != nil && *n.chosenValue != msg.value {
panic("already chose a different value...")
}
if n.round != nil {
n.round = nil
n.stopRetryTimer()
}
if n.chosenValue == nil {
n.chosenValue = &msg.value
n.recordTrace(ValueLearned{Value: msg.value})
}
}