-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathelection.go
52 lines (45 loc) · 1.43 KB
/
election.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
package raft
//===========================================================================
// Election Helpers
//===========================================================================
// NewElection creates an election for the specified peers, defaulting the
// votes to false until otherwise updated.
func NewElection(peers ...string) *Election {
votes := new(Election)
votes.ballots = make(map[string]bool, len(peers))
for _, name := range peers {
votes.ballots[name] = false
}
return votes
}
// Election objects keep track of the outcome of a single leader election by
// mapping remote peers to the votes they've provided. Uses simple majority
// to determine if an election has passed or failed.
type Election struct {
ballots map[string]bool // The votes cast during the election (yes/no)
}
// Vote records the vote for the given Replica, identified by name.
func (e *Election) Vote(name string, vote bool) {
if _, ok := e.ballots[name]; !ok {
return
}
e.ballots[name] = vote
}
// Majority computes how many nodes are needed for an election to pass.
func (e *Election) Majority() int {
return (len(e.ballots) / 2) + 1
}
// Votes sums the number of Ballots equal to true
func (e *Election) Votes() int {
count := 0
for _, ballot := range e.ballots {
if ballot {
count++
}
}
return count
}
// Passed returns true if the number of True votes is a majority.
func (e *Election) Passed() bool {
return e.Votes() >= e.Majority()
}