-
Notifications
You must be signed in to change notification settings - Fork 2
/
changeconfig.go
270 lines (249 loc) · 6.66 KB
/
changeconfig.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
// Copyright 2019 Santhosh Kumar Tekuri
//
// 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"
"time"
)
func (l *leader) onChangeConfig(t changeConfig) {
if !l.configs.IsCommitted() {
t.reply(InProgressError("configChange"))
return
}
// see https://groups.google.com/forum/#!msg/raft-dev/t4xj6dJTP6E/d2D9LrWRza8J
if l.commitIndex < l.startIndex {
t.reply(ErrNotCommitReady)
return
}
if t.newConf.Index != l.configs.Latest.Index {
t.reply(ErrStaleConfig)
return
}
if err := t.newConf.validate(); err != nil {
t.reply(err)
return
}
// ensure that except action, address nothing is modified
for id, n := range l.configs.Latest.Nodes {
nn, ok := t.newConf.Nodes[id]
if !ok {
t.reply(fmt.Errorf("raft.changeConfig: node %d is removed", id))
return
}
if n.Voter != nn.Voter {
t.reply(fmt.Errorf("raft.changeConfig: node %d voting right changed", id))
return
}
}
for id, n := range t.newConf.Nodes {
if _, ok := l.configs.Latest.Nodes[id]; !ok {
if n.Voter {
t.reply(fmt.Errorf("raft.changeConfig: new node %d must be nonvoter", id))
return
}
}
}
// ensure that new cluster will have at least one voter
var voter uint64
for id, n := range t.newConf.Nodes {
if n.Voter && n.Action == None {
voter = id
}
}
if voter == 0 {
t.reply(fmt.Errorf("raft.changeConfig: at least one voter must remain in cluster"))
return
}
l.checkConfigActions(t.task, t.newConf)
if l.configs.IsCommitted() {
if trace {
println(l, "no configActions changed")
}
// no actions changed, so commit as it is
l.doChangeConfig(t.task, t.newConf)
}
}
func (l *leader) doChangeConfig(t *task, config Config) {
l.storeEntry(&newEntry{
entry: config.encode(),
task: t,
})
}
// called from leader.storeEntry, if lastLogIndex is updated
func (l *leader) beginFinishedRounds() {
for id, repl := range l.repls {
r := repl.status.round
if r != nil && r.finished() {
r.begin(l.lastLogIndex)
if trace {
println(l, id, "started:", r)
}
}
}
}
// checkConfigActions finishes any postponed promotions
//
// this is called:
// - from leader.init
// - from leader.changeConfig
// - from leader.setCommitIndex, if config is committed
// - from leader.onTransferTimeout
func (l *leader) checkConfigActions(t *task, config Config) {
// do actions on self if any
n := config.Nodes[l.nid]
if l.canChangeConfig() && n.Action != None {
if trace {
println(l, n.ID, "started", n.Action)
}
if tracer.configActionStarted != nil {
tracer.configActionStarted(l.Raft, n.ID, n.Action)
}
switch n.Action {
case Demote:
config = config.clone()
n.Voter = false
if n.Action == Demote {
n.Action = None
}
config.Nodes[n.ID] = n
case Remove, ForceRemove:
config = config.clone()
delete(config.Nodes, l.nid)
default:
panic(unreachable())
}
l.doChangeConfig(t, config)
}
for _, repl := range l.repls {
l.checkConfigAction(t, config, &repl.status)
}
}
// checks whether round is completed, if so
// promotes if threshold is satisfied.
//
// - from leader.checkReplUpdates, if repl.matchIndex is updated
// - from leader.checkConfigActions
func (l *leader) checkConfigAction(t *task, config Config, status *replicationStatus) {
n := config.Nodes[status.id]
action := n.nextAction()
if action == None {
return
}
// start or stop rounds
if action != Promote {
status.round = nil
} else if status.round == nil {
// start first round
status.round = new(round)
status.round.begin(l.lastLogIndex)
if trace {
println(l, status.id, "started:", status.round)
}
}
// finish round if completed, start new round if necessary
if status.round != nil {
r := status.round
if !r.finished() && status.matchIndex >= r.LastIndex {
r.finish()
if trace {
println(l, status.id, "finished:", r)
}
l.logger.Info("nonVoter", status.id, "completed round", r.Ordinal, "in", r.Duration(), ", its lastIndex:", r.LastIndex)
if tracer.roundCompleted != nil {
tracer.roundCompleted(l.Raft, status.id, *r)
}
}
if !r.finished() {
return
}
hasNewEntries := l.lastLogIndex > status.matchIndex
if hasNewEntries && r.Duration() > l.promoteThreshold {
r.begin(l.lastLogIndex)
if trace {
println(l, status.id, "started:", r)
}
return
}
}
if !l.canChangeConfig() {
if trace {
println(l, status.id, "cannot", action, "now")
}
return
}
// perform configAction
switch action {
case Promote:
l.logger.Info("promoting nonvoter ", n.ID, ", after", status.round.Ordinal, "round(s)")
config = config.clone()
n.Voter, n.Action = true, None
config.Nodes[n.ID] = n
case Remove:
if status.matchIndex >= l.configs.Latest.Index {
l.logger.Info("removing nonvoter", n.ID)
config = config.clone()
delete(config.Nodes, n.ID)
} else {
return
}
case ForceRemove:
l.logger.Info("force removing node", n.ID)
config = config.clone()
delete(config.Nodes, n.ID)
case Demote:
l.logger.Info("demoting voter", n.ID)
config = config.clone()
n.Voter = false
if n.Action == Demote {
n.Action = None
}
config.Nodes[n.ID] = n
}
if trace {
println(l, status.id, "started", action)
}
if tracer.configActionStarted != nil {
tracer.configActionStarted(l.Raft, n.ID, action)
}
l.doChangeConfig(t, config)
}
func (l *leader) canChangeConfig() bool {
return l.configs.IsCommitted() && !l.transfer.inProgress()
}
func (l *leader) onWaitForStableConfig(t waitForStableConfig) {
if l.configs.IsStable() {
t.reply(l.configs.Latest)
return
}
l.waitStable = append(l.waitStable, t)
}
// round ------------------------------------------------
type round struct {
Ordinal uint64
Start time.Time
End time.Time
LastIndex uint64
}
func (r *round) begin(lastIndex uint64) {
r.Ordinal, r.Start, r.LastIndex = r.Ordinal+1, time.Now(), lastIndex
}
func (r *round) finish() { r.End = time.Now() }
func (r *round) finished() bool { return !r.End.IsZero() }
func (r round) Duration() time.Duration { return r.End.Sub(r.Start) }
func (r round) String() string {
if r.finished() {
return fmt.Sprintf("round{#%d %s lastIndex: %d}", r.Ordinal, r.Duration(), r.LastIndex)
}
return fmt.Sprintf("round{#%d lastIndex: %d}", r.Ordinal, r.LastIndex)
}