blob: 5eba50344488687ece881b2afee87f68dfb4558c [file] [log] [blame]
Abhilash S.L3b494632019-07-16 15:51:09 +05301// Copyright 2019 The etcd Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15package quorum
16
17import (
18 "fmt"
19 "math"
20 "sort"
21 "strings"
22)
23
24// MajorityConfig is a set of IDs that uses majority quorums to make decisions.
25type MajorityConfig map[uint64]struct{}
26
27func (c MajorityConfig) String() string {
28 sl := make([]uint64, 0, len(c))
29 for id := range c {
30 sl = append(sl, id)
31 }
32 sort.Slice(sl, func(i, j int) bool { return sl[i] < sl[j] })
33 var buf strings.Builder
34 buf.WriteByte('(')
35 for i := range sl {
36 if i > 0 {
37 buf.WriteByte(' ')
38 }
39 fmt.Fprint(&buf, sl[i])
40 }
41 buf.WriteByte(')')
42 return buf.String()
43}
44
45// Describe returns a (multi-line) representation of the commit indexes for the
46// given lookuper.
47func (c MajorityConfig) Describe(l AckedIndexer) string {
48 if len(c) == 0 {
49 return "<empty majority quorum>"
50 }
51 type tup struct {
52 id uint64
53 idx Index
54 ok bool // idx found?
55 bar int // length of bar displayed for this tup
56 }
57
58 // Below, populate .bar so that the i-th largest commit index has bar i (we
59 // plot this as sort of a progress bar). The actual code is a bit more
60 // complicated and also makes sure that equal index => equal bar.
61
62 n := len(c)
63 info := make([]tup, 0, n)
64 for id := range c {
65 idx, ok := l.AckedIndex(id)
66 info = append(info, tup{id: id, idx: idx, ok: ok})
67 }
68
69 // Sort by index
70 sort.Slice(info, func(i, j int) bool {
71 if info[i].idx == info[j].idx {
72 return info[i].id < info[j].id
73 }
74 return info[i].idx < info[j].idx
75 })
76
77 // Populate .bar.
78 for i := range info {
79 if i > 0 && info[i-1].idx < info[i].idx {
80 info[i].bar = i
81 }
82 }
83
84 // Sort by ID.
85 sort.Slice(info, func(i, j int) bool {
86 return info[i].id < info[j].id
87 })
88
89 var buf strings.Builder
90
91 // Print.
92 fmt.Fprint(&buf, strings.Repeat(" ", n)+" idx\n")
93 for i := range info {
94 bar := info[i].bar
95 if !info[i].ok {
96 fmt.Fprint(&buf, "?"+strings.Repeat(" ", n))
97 } else {
98 fmt.Fprint(&buf, strings.Repeat("x", bar)+">"+strings.Repeat(" ", n-bar))
99 }
100 fmt.Fprintf(&buf, " %5d (id=%d)\n", info[i].idx, info[i].id)
101 }
102 return buf.String()
103}
104
105type uint64Slice []uint64
106
107func insertionSort(sl uint64Slice) {
108 a, b := 0, len(sl)
109 for i := a + 1; i < b; i++ {
110 for j := i; j > a && sl[j] < sl[j-1]; j-- {
111 sl[j], sl[j-1] = sl[j-1], sl[j]
112 }
113 }
114}
115
116// CommittedIndex computes the committed index from those supplied via the
117// provided AckedIndexer (for the active config).
118func (c MajorityConfig) CommittedIndex(l AckedIndexer) Index {
119 n := len(c)
120 if n == 0 {
121 // This plays well with joint quorums which, when one half is the zero
122 // MajorityConfig, should behave like the other half.
123 return math.MaxUint64
124 }
125
126 // Use an on-stack slice to collect the committed indexes when n <= 7
127 // (otherwise we alloc). The alternative is to stash a slice on
128 // MajorityConfig, but this impairs usability (as is, MajorityConfig is just
129 // a map, and that's nice). The assumption is that running with a
130 // replication factor of >7 is rare, and in cases in which it happens
131 // performance is a lesser concern (additionally the performance
132 // implications of an allocation here are far from drastic).
133 var stk [7]uint64
134 srt := uint64Slice(stk[:])
135
136 if cap(srt) < n {
137 srt = make([]uint64, n)
138 }
139 srt = srt[:n]
140
141 {
142 // Fill the slice with the indexes observed. Any unused slots will be
143 // left as zero; these correspond to voters that may report in, but
144 // haven't yet. We fill from the right (since the zeroes will end up on
145 // the left after sorting below anyway).
146 i := n - 1
147 for id := range c {
148 if idx, ok := l.AckedIndex(id); ok {
149 srt[i] = uint64(idx)
150 i--
151 }
152 }
153 }
154
155 // Sort by index. Use a bespoke algorithm (copied from the stdlib's sort
156 // package) to keep srt on the stack.
157 insertionSort(srt)
158
159 // The smallest index into the array for which the value is acked by a
160 // quorum. In other words, from the end of the slice, move n/2+1 to the
161 // left (accounting for zero-indexing).
162 pos := n - (n/2 + 1)
163 return Index(srt[pos])
164}
165
166// VoteResult takes a mapping of voters to yes/no (true/false) votes and returns
167// a result indicating whether the vote is pending (i.e. neither a quorum of
168// yes/no has been reached), won (a quorum of yes has been reached), or lost (a
169// quorum of no has been reached).
170func (c MajorityConfig) VoteResult(votes map[uint64]bool) VoteResult {
171 if len(c) == 0 {
172 // By convention, the elections on an empty config win. This comes in
173 // handy with joint quorums because it'll make a half-populated joint
174 // quorum behave like a majority quorum.
175 return VoteWon
176 }
177
178 ny := [2]int{} // vote counts for no and yes, respectively
179
180 var missing int
181 for id := range c {
182 v, ok := votes[id]
183 if !ok {
184 missing++
185 continue
186 }
187 if v {
188 ny[1]++
189 } else {
190 ny[0]++
191 }
192 }
193
194 q := len(c)/2 + 1
195 if ny[1] >= q {
196 return VoteWon
197 }
198 if ny[1]+missing >= q {
199 return VotePending
200 }
201 return VoteLost
202}