-
Notifications
You must be signed in to change notification settings - Fork 5
/
quorum.go
74 lines (63 loc) · 1.35 KB
/
quorum.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
package BigBFT
// Quorum records each acknowledgement and check for different types of quorum satisfied
type Quorum struct {
size int
acks map[ID]bool
zones map[int]int
nacks map[ID]bool
}
// NewQuorum returns a new Quorum
func NewQuorum() *Quorum {
q := &Quorum{
size: 0,
acks: make(map[ID]bool),
zones: make(map[int]int),
}
return q
}
// ACK adds id to quorum ack records
func (q *Quorum) ACK(id ID) {
if !q.acks[id] {
q.acks[id] = true
q.size++
q.zones[id.Zone()]++
}
}
// ADD increase ack size by one
func (q *Quorum) ADD() {
q.size++
}
// Size returns current ack size
func (q *Quorum) Size() int {
return q.size
}
// Reset resets the quorum to empty
func (q *Quorum) Reset() {
q.size = 0
q.acks = make(map[ID]bool)
q.zones = make(map[int]int)
q.nacks = make(map[ID]bool)
}
func (q *Quorum) All() bool {
return q.size == config.n
}
func (q *Quorum) Total() int {
return config.Benchmark.Concurrency
}
// Majority quorum satisfied
func (q *Quorum) Majority() bool {
return q.size >= config.n/2
}
// AllZones returns true if there is at one ack from each zone
func (q *Quorum) AllZones() bool {
return len(q.zones) == config.z
}
// ZoneMajority returns true if majority quorum satisfied in any zone
func (q *Quorum) ZoneMajority() bool {
for z, n := range q.zones {
if n > config.npz[z]/2 {
return true
}
}
return false
}