-
Notifications
You must be signed in to change notification settings - Fork 4
/
qset.go
219 lines (196 loc) · 5.45 KB
/
qset.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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
package scp
import (
"bytes"
"fmt"
"math/big"
)
type (
// QSet is a compact representation for a set of quorum slices.
// A quorum slice is any T items from M,
// where 0 < T <= len(M).
// An item in M is either a node or a nested QSet.
// If the latter,
// any of the recursively defined subslices count as one "item" here.
QSet struct {
T int `json:"threshold"`
M []QSetMember `json:"members"`
}
// QSetMember is a member of a QSet.
// It's either a node ID or a nested QSet.
// Exactly one of its fields is non-nil.
QSetMember struct {
N *NodeID `json:"node_id,omitempty"`
Q *QSet `json:"qset,omitempty"`
}
)
// Checks that at least one node in each quorum slice satisfies pred
// (excluding the slot's node).
//
// Works by finding len(q.M)-q.T+1 members for which pred is true
func (q QSet) findBlockingSet(msgs map[NodeID]*Msg, pred predicate) (NodeIDSet, predicate) {
return findBlockingSetHelper(len(q.M)-q.T+1, q.M, msgs, pred, nil)
}
func findBlockingSetHelper(needed int, members []QSetMember, msgs map[NodeID]*Msg, pred predicate, sofar NodeIDSet) (NodeIDSet, predicate) {
if needed == 0 {
return sofar, pred
}
if needed > len(members) {
return nil, pred
}
m0 := members[0]
switch {
case m0.N != nil:
if msg, ok := msgs[*m0.N]; ok {
if nextPred := pred.test(msg); nextPred != nil {
return findBlockingSetHelper(needed-1, members[1:], msgs, nextPred, sofar.Add(*m0.N))
}
}
case m0.Q != nil:
sofar2, pred2 := findBlockingSetHelper(len(m0.Q.M)-m0.Q.T+1, m0.Q.M, msgs, pred, sofar)
if len(sofar2) > 0 {
return findBlockingSetHelper(needed-1, members[1:], msgs, pred2, sofar2)
}
}
return findBlockingSetHelper(needed, members[1:], msgs, pred, sofar)
}
// Finds a quorum in which every node satisfies the given
// predicate. The slot's node itself is presumed to satisfy the
// predicate.
func (q QSet) findQuorum(nodeID NodeID, m map[NodeID]*Msg, pred predicate) (NodeIDSet, predicate) {
return findQuorumHelper(q.T, q.M, m, pred, NodeIDSet{nodeID})
}
func findQuorumHelper(threshold int, members []QSetMember, msgs map[NodeID]*Msg, pred predicate, sofar NodeIDSet) (NodeIDSet, predicate) {
if threshold == 0 {
return sofar, pred
}
if threshold > len(members) {
return nil, pred
}
m0 := members[0]
switch {
case m0.N != nil:
if sofar.Contains(*m0.N) {
return findQuorumHelper(threshold-1, members[1:], msgs, pred, sofar)
}
if msg, ok := msgs[*m0.N]; ok {
if nextPred := pred.test(msg); nextPred != nil {
sofar2, pred2 := findQuorumHelper(msg.Q.T, msg.Q.M, msgs, nextPred, sofar.Add(*m0.N))
if len(sofar2) > 0 {
return findQuorumHelper(threshold-1, members[1:], msgs, pred2, sofar2)
}
}
}
case m0.Q != nil:
sofar2, pred2 := findQuorumHelper(m0.Q.T, m0.Q.M, msgs, pred, sofar)
if len(sofar2) > 0 {
return findQuorumHelper(threshold-1, members[1:], msgs, pred2, sofar2)
}
}
return findQuorumHelper(threshold, members[1:], msgs, pred, sofar)
}
// Function weight returns the fraction of q's quorum slices in which id appears.
// Return value is the fraction and
// (as an optimization)
// a bool indicating whether it's exactly 1.
func (q QSet) weight(id NodeID) (float64, bool) {
num, denom := q.NodeFrac(id)
if num == denom {
return 1.0, true
}
return float64(num) / float64(denom), false
}
// Slices calls f once for each slice represented by q.
// It continues until all slices have been generated or f returns false to terminate early.
func (q QSet) Slices(f func(NodeIDSet) bool) {
slicesHelper(q.T, q.M, f, nil, 0)
}
func slicesHelper(t int, members []QSetMember, f func(NodeIDSet) bool, sofar NodeIDSet, depth int) (out bool) {
if t == 0 {
return f(sofar)
}
if t > len(members) {
return true
}
m0 := members[0]
switch {
case m0.N != nil:
if !slicesHelper(t-1, members[1:], f, append(sofar, *m0.N), depth+1) {
return false
}
case m0.Q != nil:
ok := slicesHelper(
m0.Q.T,
m0.Q.M,
func(slice NodeIDSet) bool {
return slicesHelper(t-1, members[1:], f, sofar.Union(slice), depth+1)
},
sofar,
depth+1,
)
if !ok {
return false
}
}
return slicesHelper(t, members[1:], f, sofar, depth+1)
}
// Nodes returns a flattened set of all nodes contained in q and its nested QSets.
func (q QSet) Nodes() NodeIDSet {
var result NodeIDSet
for _, m := range q.M {
switch {
case m.N != nil:
result = result.Add(*m.N)
case m.Q != nil:
result = result.Union(m.Q.Nodes())
}
}
return result
}
func (q QSet) NumSlices() *big.Int {
result := new(big.Int)
result.Binomial(int64(len(q.M)), int64(q.T))
for _, m := range q.M {
if m.Q != nil {
result.Mul(result, m.Q.NumSlices())
}
}
return result
}
// NodeFrac gives the fraction of slices in q containing the given node.
// It assumes that id appears in at most one QSet
// (either the top level one or a single reachable nested one)
// and then only once in that QSet.
func (q QSet) NodeFrac(id NodeID) (num, denom int) {
for _, m := range q.M {
switch {
case m.N != nil:
if *m.N == id {
return q.T, len(q.M)
}
case m.Q != nil:
num2, denom2 := m.Q.NodeFrac(id)
if num2 > 0 {
return q.T * num2, len(q.M) * denom2
}
}
}
return 0, 1
}
func (m QSetMember) String() string {
switch {
case m.N != nil:
return fmt.Sprintf("N:%s", *m.N)
case m.Q != nil:
b := new(bytes.Buffer)
fmt.Fprintf(b, "Q:{T=%d [", m.Q.T)
for i, mm := range m.Q.M {
if i > 0 {
b.WriteByte(' ')
}
b.WriteString(mm.String())
}
fmt.Fprint(b, "]}")
return b.String()
}
return ""
}