generated from B1NARY-GR0UP/.github
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprogress.go
204 lines (180 loc) · 4.24 KB
/
progress.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
// Copyright 2024 BINARY Members
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package raft
import (
"fmt"
"reflect"
"slices"
"strings"
rt "github.com/B1NARY-GR0UP/raft/raftthrift"
)
// ClusterConfig is Config (trk.Config)
type ClusterConfig struct {
Voters JointConfig
AutoLeave bool
}
func (c *ClusterConfig) String() string {
var sb strings.Builder
_, _ = fmt.Fprintf(&sb, "voters=%s", c.Voters)
if c.AutoLeave {
sb.WriteString(" autoLeave")
}
return sb.String()
}
func (c *ClusterConfig) clone() ClusterConfig {
clone := func(m map[int64]struct{}) map[int64]struct{} {
if m == nil {
return nil
}
res := make(map[int64]struct{}, len(m))
for k := range m {
res[k] = struct{}{}
}
return res
}
return ClusterConfig{
Voters: JointConfig{clone(c.Voters[0]), clone(c.Voters[1])},
}
}
type progressMap map[int64]*Progress
func (p progressMap) clone() map[int64]Progress {
m := make(map[int64]Progress)
for k, v := range p {
m[k] = *v
}
return m
}
type tracker struct {
ClusterConfig
votes map[int64]bool
// Volatile state on leaders
// raft log progress
progress progressMap
}
func newTracker() *tracker {
return &tracker{
ClusterConfig: ClusterConfig{
Voters: JointConfig{
MajorityConfig{},
nil, // populated when used
},
AutoLeave: false,
},
votes: make(map[int64]bool),
progress: make(progressMap),
}
}
func (t *tracker) voterIDs() []int64 {
m := t.Voters.IDs()
ids := make([]int64, 0, len(m))
for id := range m {
ids = append(ids, id)
}
slices.Sort(ids)
return ids
}
// visitProgress visit and apply closure to every progress
func (t *tracker) visitProgress(f func(id int64, p *Progress)) {
n := len(t.progress)
// most clusters are smaller than or equal to 7
var sli [7]int64
var ids []int64
// avoid dynamic allocate
if n > len(sli) {
ids = make([]int64, n)
} else {
ids = sli[:n]
}
for id := range t.progress {
n--
ids[n] = id
}
// insertionSort works well on small data set
slices.Sort(ids)
for _, id := range ids {
f(id, t.progress[id])
}
}
func (t *tracker) visitVoters(f func(id int64)) {
// REPLACE:
// ids := t.voters.ids()
// insertionSort(ids)
ids := t.Voters.sortedIDs()
for _, id := range ids {
f(id)
}
}
func (t *tracker) resetVotes() {
clear(t.votes)
}
func (t *tracker) resetProgress() {
clear(t.progress)
}
func (t *tracker) recordVote(id int64, voteGranted bool) {
if _, ok := t.votes[id]; !ok {
t.votes[id] = voteGranted
}
}
func (t *tracker) tallyVotes() (granted, rejected int64, _ VoteResult) {
for _, vote := range t.votes {
if vote {
granted++
} else {
rejected++
}
}
return granted, rejected, t.Voters.VoteResult(t.votes)
}
// TODO: PLAN B >> raft-go's style
func (t *tracker) commitIndex() int64 {
return t.Voters.commitIndex(t.progress)
}
func (t *tracker) ConfState() rt.ConfState {
return rt.ConfState{
Voters: incoming(t.Voters).Slice(),
VotersOutgoing: outgoing(t.Voters).Slice(),
AutoLeave: t.AutoLeave,
}
}
func assertConfStateEqual(a, b rt.ConfState) {
if IsConfStateEqual(a, b) {
return
}
panic("confstate not equal")
}
func IsConfStateEqual(a, b rt.ConfState) bool {
s := func(sli *[]int64) {
// use deep copy avoid effect to original object
*sli = append([]int64(nil), *sli...)
slices.Sort(*sli)
}
// use pointer to affect a and b, otherwise fields of a, b will not be sorted
for _, csp := range []*rt.ConfState{&a, &b} {
s(&csp.Voters)
s(&csp.VotersOutgoing)
}
return reflect.DeepEqual(a, b)
}
type Progress struct {
NextIndex int64
MatchIndex int64
}
func (p *Progress) decreaseNextIndex(v int64) {
p.NextIndex -= v
}
// update matchIndex and nextIndex
func (p *Progress) update(idx int64) {
p.MatchIndex = max(p.MatchIndex, idx)
p.NextIndex = max(p.NextIndex, idx+1)
}