-
Notifications
You must be signed in to change notification settings - Fork 4
/
quorum.go
192 lines (172 loc) · 3.48 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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
package PaxiBFT
import "github.com/salemmohammed/PaxiBFT/log"
// 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
ID map[ID]int
n float64
AID []ID
}
// NewQuorum returns a new Quorum
func NewQuorum() *Quorum {
q := &Quorum{
size: 0,
acks: make(map[ID]bool),
zones: make(map[int]int),
ID: make(map[ID]int),
AID: make([]ID,0),
}
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()]++
}
}
// Majority quorum satisfied
func (q *Quorum) Total() int {
return config.n
}
func (q *Quorum) Total1() int {
return config.Benchmark.Concurrency
}
// 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++
}
func (q *Quorum) INC() float64{
q.n++
return q.n
}
// 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
}
// Majority quorum satisfied
func (q *Quorum) PreparedMajority() bool {
return q.size >= (config.n/2) -1
}
// 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
}
// ADD increase ack size by one
func (q *Quorum) AID_ID(id ID) {
for _,v := range q.AID{
if v == id {
return
}
}
q.AID = append(q.AID, id)
log.Debugf("ids %v", q.AID)
}
/*
// 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
}
}
*/