/
inflight.go
284 lines (243 loc) · 7.67 KB
/
inflight.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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package rafted
import (
"errors"
"fmt"
ev "github.com/hhkbp2/rafted/event"
ps "github.com/hhkbp2/rafted/persist"
"sync"
)
type CommitCondition interface {
AddVote(ps.MultiAddr) error
IsCommitted() bool
}
type MajorityCommitCondition struct {
VoteStatus map[ps.MultiAddr]bool
VoteCount uint32
MajoritySize uint32
}
func NewMajorityCommitCondition(
addrSlice *ps.ServerAddressSlice) *MajorityCommitCondition {
voteStatus := make(map[ps.MultiAddr]bool)
for _, addr := range addrSlice.AllMultiAddr() {
voteStatus[addr] = false
}
return &MajorityCommitCondition{
VoteStatus: voteStatus,
VoteCount: 0,
MajoritySize: uint32((addrSlice.Len() / 2) + 1),
}
}
func (self *MajorityCommitCondition) AddVote(addr ps.MultiAddr) error {
voteStatus, ok := self.VoteStatus[addr]
if !ok {
return errors.New(fmt.Sprintf("%s not in cluster", addr.String()))
}
if voteStatus {
return errors.New(fmt.Sprintf("%s already voted", addr.String()))
}
self.VoteStatus[addr] = true
self.VoteCount++
return nil
}
func (self *MajorityCommitCondition) IsInCluster(addr ps.MultiAddr) bool {
if _, ok := self.VoteStatus[addr]; ok {
return true
}
return false
}
func (self *MajorityCommitCondition) IsCommitted() bool {
return self.VoteCount >= self.MajoritySize
}
type MemberChangeCommitCondition struct {
OldServersCommitCondition *MajorityCommitCondition
NewServersCommitCondition *MajorityCommitCondition
}
func NewMemberChangeCommitCondition(
conf *ps.Config) *MemberChangeCommitCondition {
return &MemberChangeCommitCondition{
OldServersCommitCondition: NewMajorityCommitCondition(conf.Servers),
NewServersCommitCondition: NewMajorityCommitCondition(conf.NewServers),
}
}
func (self *MemberChangeCommitCondition) AddVote(addr ps.MultiAddr) error {
voted := false
if self.OldServersCommitCondition.IsInCluster(addr) {
if err := self.OldServersCommitCondition.AddVote(addr); err != nil {
return err
}
voted = true
}
if self.NewServersCommitCondition.IsInCluster(addr) {
if err := self.NewServersCommitCondition.AddVote(addr); err != nil {
return err
}
voted = true
}
if voted {
return nil
} else {
return errors.New(
fmt.Sprintf("addr %s not in old/new config", addr.String()))
}
}
func (self *MemberChangeCommitCondition) IsCommitted() bool {
return (self.OldServersCommitCondition.IsCommitted() &&
self.NewServersCommitCondition.IsCommitted())
}
type InflightRequest struct {
LogEntry *ps.LogEntry
ResultChan chan ev.Event
}
type InflightEntry struct {
Request *InflightRequest
Condition CommitCondition
}
func NewInflightEntry(request *InflightRequest) *InflightEntry {
if request.LogEntry.Conf.IsNormalConfig() {
return &InflightEntry{
Request: request,
Condition: NewMajorityCommitCondition(
request.LogEntry.Conf.Servers),
}
}
return &InflightEntry{
Request: request,
Condition: NewMemberChangeCommitCondition(request.LogEntry.Conf),
}
}
type Inflight struct {
MaxIndex uint64
ToCommitEntries []*InflightEntry
CommittedEntries []*InflightEntry
ServerMatchIndexes map[ps.MultiAddr]uint64
sync.Mutex
}
func setupServerMatchIndexes(
conf *ps.Config, prev map[ps.MultiAddr]uint64) map[ps.MultiAddr]uint64 {
serverMatchIndexes := make(map[ps.MultiAddr]uint64)
initIndex := func(m map[ps.MultiAddr]uint64,
addrSlice *ps.ServerAddressSlice) {
for _, addr := range addrSlice.AllMultiAddr() {
serverMatchIndexes[addr] = 0
}
}
if conf.Servers != nil {
initIndex(serverMatchIndexes, conf.Servers)
}
if conf.NewServers != nil {
initIndex(serverMatchIndexes, conf.NewServers)
}
for addr, _ := range serverMatchIndexes {
if matchIndex, ok := prev[addr]; ok {
serverMatchIndexes[addr] = matchIndex
}
}
return serverMatchIndexes
}
func NewInflight(conf *ps.Config) *Inflight {
defaultValue := make(map[ps.MultiAddr]uint64)
matchIndexes := setupServerMatchIndexes(conf, defaultValue)
return &Inflight{
MaxIndex: 0,
ToCommitEntries: make([]*InflightEntry, 0),
CommittedEntries: make([]*InflightEntry, 0),
ServerMatchIndexes: matchIndexes,
}
}
func (self *Inflight) Init() {
self.Lock()
defer self.Unlock()
self.MaxIndex = 0
self.ToCommitEntries = make([]*InflightEntry, 0)
self.CommittedEntries = make([]*InflightEntry, 0)
for k, _ := range self.ServerMatchIndexes {
self.ServerMatchIndexes[k] = 0
}
}
func (self *Inflight) ChangeMember(conf *ps.Config) {
self.Lock()
defer self.Unlock()
newMatchIndexes := setupServerMatchIndexes(conf, self.ServerMatchIndexes)
self.ServerMatchIndexes = newMatchIndexes
}
func (self *Inflight) Add(request *InflightRequest) error {
self.Lock()
defer self.Unlock()
logIndex := request.LogEntry.Index
if logIndex <= self.MaxIndex {
return errors.New("log index should be incremental")
}
self.MaxIndex = logIndex
toCommit := NewInflightEntry(request)
self.ToCommitEntries = append(self.ToCommitEntries, toCommit)
return nil
}
func (self *Inflight) AddAll(toCommits []*InflightEntry) error {
self.Lock()
defer self.Unlock()
if len(toCommits) == 0 {
return errors.New("no inflight request to add")
}
// check the indexes are incremental
index := self.MaxIndex
for _, entry := range toCommits {
if !(index < entry.Request.LogEntry.Index) {
return errors.New("log index should be incremental")
}
index = entry.Request.LogEntry.Index
}
self.MaxIndex = index
self.ToCommitEntries = append(self.ToCommitEntries, toCommits...)
return nil
}
func (self *Inflight) Replicate(
addr ps.MultiAddr, newMatchIndex uint64) (bool, error) {
self.Lock()
defer self.Unlock()
// health check
matchIndex, ok := self.ServerMatchIndexes[addr]
if !ok {
return false, errors.New(fmt.Sprintf("unknown address %#v", addr))
}
if matchIndex >= newMatchIndex {
return false, errors.New(
fmt.Sprintf("invalid new match index %d, not greater than %d",
newMatchIndex, matchIndex))
}
// update vote count for new replicated log
for _, toCommit := range self.ToCommitEntries {
if toCommit.Request.LogEntry.Index > newMatchIndex {
break
}
toCommit.Condition.AddVote(addr)
}
// update match index for the specified server
self.ServerMatchIndexes[addr] = newMatchIndex
// only inflight requests with log index up to(including)
// newMatchIndex are possible to be good to commit
committedIndex := 0
for _, toCommit := range self.ToCommitEntries {
if toCommit.Request.LogEntry.Index > newMatchIndex {
break
}
if !toCommit.Condition.IsCommitted() {
break
}
committedIndex++
}
if committedIndex > 0 {
self.CommittedEntries = append(
self.CommittedEntries, self.ToCommitEntries[:committedIndex]...)
self.ToCommitEntries = self.ToCommitEntries[committedIndex:]
return true, nil
} else {
return false, nil
}
}
func (self *Inflight) GetCommitted() []*InflightEntry {
self.Lock()
defer self.Unlock()
committed := self.CommittedEntries
self.CommittedEntries = make([]*InflightEntry, 0)
return committed
}