-
Notifications
You must be signed in to change notification settings - Fork 75
/
Copy pathquorum.go
159 lines (141 loc) · 2.86 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
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
package paxi
// 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()]++
}
}
// NACK adds id to quorum nack records
func (q *Quorum) NACK(id ID) {
if !q.nacks[id] {
q.nacks[id] = true
}
}
// 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
}
// Majority quorum satisfied
func (q *Quorum) Majority() bool {
return q.size > config.n/2
}
// FastQuorum from fast paxos
func (q *Quorum) FastQuorum() bool {
return q.size >= config.n*3/4
}
// 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
}
// GridRow == AllZones
func (q *Quorum) GridRow() bool {
return q.AllZones()
}
// GridColumn == all nodes in one zone
func (q *Quorum) GridColumn() bool {
for z, n := range q.zones {
if n == config.npz[z] {
return true
}
}
return false
}
// FGridQ1 is flexible grid quorum for phase 1
func (q *Quorum) FGridQ1(Fz int) bool {
zone := 0
for z, n := range q.zones {
if n > config.npz[z]/2 {
zone++
}
}
return zone >= config.z-Fz
}
// FGridQ2 is flexible grid quorum for phase 2
func (q *Quorum) FGridQ2(Fz int) bool {
zone := 0
for z, n := range q.zones {
if n > config.npz[z]/2 {
zone++
}
}
return zone >= Fz+1
}
/*
// Q1 returns true if config.Quorum type is satisfied
func (q *Quorum) Q1() bool {
switch config.Quorum {
case "majority":
return q.Majority()
case "grid":
return q.GridRow()
case "fgrid":
return q.FGridQ1()
case "group":
return q.ZoneMajority()
case "count":
return q.size >= config.n-config.F
default:
log.Error("Unknown quorum type")
return false
}
}
// Q2 returns true if config.Quorum type is satisfied
func (q *Quorum) Q2() bool {
switch config.Quorum {
case "majority":
return q.Majority()
case "grid":
return q.GridColumn()
case "fgrid":
return q.FGridQ2()
case "group":
return q.ZoneMajority()
case "count":
return q.size > config.F
default:
log.Error("Unknown quorum type")
return false
}
}
*/