-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproposer.go
101 lines (90 loc) · 1.97 KB
/
proposer.go
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
package paxos
import "fmt"
type proposer struct {
id int
pv int
pn int
receives chan message
acceptors []chan message
learners []chan message
}
// NewProposer makes a new proposer component with identifier id, initial
// proposal value v, receival channel receives, and channels to the other
// components through acceptors and learners.
func NewProposer(id, v int,
receives chan message,
acceptors, learners []chan message) *proposer {
p := new(proposer)
p.id = id
p.pv = v
p.pn = 0
p.receives = receives
p.acceptors = acceptors
p.learners = learners
return p
}
// Run starts the proposer's Paxos algorithm.
func (p *proposer) Run() {
fmt.Printf("Proposer %v: started\n", p.id)
decided := false
for !decided {
// PHASE 1: Prepare-Promise
p.prepare()
responded := make(map[int]bool)
max := p.pn
for len(responded) < len(p.acceptors)/2+1 {
msg := <-p.receives
switch msg.t {
case Promise:
responded[msg.from] = true
if msg.pn > max {
p.pv = msg.pv
max = msg.pn
}
default:
}
}
// PHASE 2: Accept-Accepted
p.accept()
responded = make(map[int]bool)
max = p.pn
for len(responded) < len(p.acceptors)/2+1 {
msg := <-p.receives
switch msg.t {
case Accepted:
responded[msg.from] = true
if msg.pn > max {
max = msg.pn
}
default:
}
}
if p.pn == max {
break
}
p.pn = max
}
// Success: Chosen value
p.chosen()
}
func (p *proposer) prepare() {
p.pn++
msg := NewPrepareMessage(p.id, p.pn)
fmt.Printf("Proposer %v: sending Prepare\n", p.id)
broadcast(p.acceptors, msg)
}
func (p *proposer) accept() {
msg := NewAcceptMessage(p.id, p.pn, p.pv)
fmt.Printf("Proposer %v: sending Accept\n", p.id)
broadcast(p.acceptors, msg)
}
func (p *proposer) chosen() {
msg := NewChosenMessage(p.id, p.pv)
fmt.Printf("Proposer %v: sending Chosen\n", p.id)
broadcast(p.learners, msg)
}
func broadcast(peers []chan message, msg message) {
for _, peer := range peers {
peer <- msg
}
}