forked from bobg/scp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
quorum.go
179 lines (164 loc) · 5.22 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
package scp
// This file contains functions for finding "blocking sets" and
// "quorums" that satisfy a given predicate.
//
// Each node specifies one or more "quorum slices." Each quorum slice
// is a set of trusted peer nodes. Each quorum slice conceptually
// includes the node itself, though in this implementation that is not
// explicit.
//
// A quorum slice is not necessarily a quorum in itself. A peer in a
// quorum slice may have a dependency on a third-party node, as may
// that node, and so on. A quorum (with respect to a given node) is
// thus the transitive closure over any of its quorum slices. A node
// may have many different quorums, and they may overlap one another.
//
// Every protocol message includes the sending node's set of quorum
// slices. Every node saves the latest message seen from a given
// node. If enough messages have been seen, it is possible for a node
// to know the complete membership of one or more quorums.
//
// A "blocking set" is related to the idea of a quorum, but is
// simpler. It's any set of peers among a node's quorum slices that
// blocks the possibility of a quorum. A blocking set satisfying
// statement X precludes the existence of any quorum satisfying !X. A
// single peer from each of a node's quorum slices is sufficient to
// form a blocking set.
// Checks that at least one node in each quorum slice satisfies pred
// (excluding the slot's node).
func (s *Slot) findBlockingSet(pred predicate) NodeIDSet {
res, _ := s.V.Q.findBlockingSet(s.M, pred)
return res
}
// Finds a quorum in which every node satisfies the given
// predicate. The slot's node itself is presumed to satisfy the
// predicate.
func (s *Slot) findQuorum(pred predicate) NodeIDSet {
res, _ := s.V.Q.findQuorum(s.V.ID, s.M, pred)
return res
}
// Tells whether a statement can be accepted, either because a
// blocking set accepts, or because a quorum votes-or-accepts it. The
// function f should produce an "accepts" predicate when its argument
// is false and a "votes-or-accepts" predicate when its argument is
// true.
func (s *Slot) accept(f func(bool) predicate) NodeIDSet {
// 1. If s's node accepts the statement,
// we're done
// (since it is its own blocking set and,
// more intuitively,
// node N can accept X if N already accepts X).
acceptsPred := f(false)
if s.sent != nil && acceptsPred.test(s.sent) != nil {
return NodeIDSet{s.V.ID}
}
// 2. Look for a blocking set apart from s.V that accepts.
nodeIDs := s.findBlockingSet(acceptsPred)
if len(nodeIDs) > 0 {
return nodeIDs
}
// 3. Look for a quorum that votes-or-accepts.
// The quorum necessarily includes s's node.
votesOrAcceptsPred := f(true)
if s.sent == nil || votesOrAcceptsPred.test(s.sent) == nil {
return nil
}
return s.findQuorum(votesOrAcceptsPred)
}
// Abstract predicate. Concrete types below.
type predicate interface {
// Tests whether a node's latest message satisfies this predicate.
// If it does not, the return value must be nil.
// If it does, the return value should be the predicate,
// or an updated copy of the predicate for use in a subsequent call to test.
// The original predicate should not change, because when findQuorum needs to backtrack,
// it also unwinds to earlier values of the predicate.
test(*Msg) predicate
}
// This is a simple function predicate. It does not change from one
// call to the next.
type fpred func(*Msg) bool
func (f fpred) test(msg *Msg) predicate {
if f(msg) {
return f
}
return nil
}
// This is a predicate that can narrow a set of values as it traverses
// nodes.
type valueSetPred struct {
vals ValueSet
finalVals *ValueSet
testfn func(*Msg, ValueSet) ValueSet
}
func (p *valueSetPred) test(msg *Msg) predicate {
if len(p.vals) == 0 {
return nil
}
nextVals := p.testfn(msg, p.vals)
if len(nextVals) == 0 {
return nil
}
if p.finalVals != nil {
*p.finalVals = nextVals
}
return &valueSetPred{
vals: nextVals,
finalVals: p.finalVals,
testfn: p.testfn,
}
}
// This is a predicate that can narrow a set of ballots as it traverses
// nodes.
type ballotSetPred struct {
ballots BallotSet
finalBallots *BallotSet
testfn func(*Msg, BallotSet) BallotSet
}
func (p *ballotSetPred) test(msg *Msg) predicate {
if len(p.ballots) == 0 {
return nil
}
nextBallots := p.testfn(msg, p.ballots)
if len(nextBallots) == 0 {
return nil
}
if p.finalBallots != nil {
*p.finalBallots = nextBallots
}
return &ballotSetPred{
ballots: nextBallots,
finalBallots: p.finalBallots,
testfn: p.testfn,
}
}
// This is a predicate that can narrow a set of min/max bounds as it
// traverses nodes.
type minMaxPred struct {
min, max int // the current min/max bounds
finalMin, finalMax *int // each call to next updates the min/max bounds these point to
testfn func(msg *Msg, min, max int) (bool, int, int)
}
func (p *minMaxPred) test(msg *Msg) predicate {
if p.min > p.max {
return nil
}
res, min, max := p.testfn(msg, p.min, p.max)
if !res {
return nil
}
nextMin, nextMax := min, max
if p.finalMin != nil {
*p.finalMin = nextMin
}
if p.finalMax != nil {
*p.finalMax = nextMax
}
return &minMaxPred{
min: nextMin,
max: nextMax,
finalMin: p.finalMin,
finalMax: p.finalMax,
testfn: p.testfn,
}
}