-
Notifications
You must be signed in to change notification settings - Fork 0
/
paxos.go
58 lines (47 loc) · 1.3 KB
/
paxos.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
package paxos
type network struct {
proposers []*proposer
acceptors []*acceptor
learners []*learner
}
// NewNetwork creates a Paxos network with nProposers proposers (attempting to
// initially propose the values respectively in vs), nAcceptors acceptors
// and nLearners learners.
func NewNetwork(nProposers, nAcceptors, nLearners int, vs []int) *network {
cProposers := makeChannels(nProposers)
cAcceptors := makeChannels(nAcceptors)
cLearners := makeChannels(nLearners)
n := new(network)
n.proposers = make([]*proposer, nProposers)
n.acceptors = make([]*acceptor, nAcceptors)
n.learners = make([]*learner, nLearners)
for i := range n.proposers {
n.proposers[i] = NewProposer(i, vs[i], cProposers[i], cAcceptors, cLearners)
}
for i := range n.acceptors {
n.acceptors[i] = NewAcceptor(i, cAcceptors[i], cProposers)
}
for i := range n.learners {
n.learners[i] = NewLearner(i, cLearners[i])
}
return n
}
func makeChannels(n int) []chan message {
chans := make([]chan message, n)
for i := range chans {
chans[i] = make(chan message, 1024)
}
return chans
}
// Starts a goroutine for all the components in the network.
func (n *network) Start() {
for _, l := range n.learners {
go l.Run()
}
for _, a := range n.acceptors {
go a.Run()
}
for _, p := range n.proposers {
go p.Run()
}
}