-
Notifications
You must be signed in to change notification settings - Fork 542
/
remote.go
198 lines (174 loc) · 3.96 KB
/
remote.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
// Copyright 2015 The etcd Authors
//
// 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.
//
//
//
// Copyright 2017-2019 Lei Ni (nilei81@gmail.com)
//
// 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.
//
//
// remote.go is used for tracking the state of remote raft node. it is derived
// from etcd raft's flow control code.
//
package raft
import (
"fmt"
)
type remoteStateType uint64
const (
remoteRetry remoteStateType = iota
remoteWait
remoteReplicate
remoteSnapshot
)
var remoteNames = []string{
"Retry",
"Wait",
"Replicate",
"Snapshot",
}
func (r remoteStateType) String() string {
return remoteNames[uint64(r)]
}
type remote struct {
// called matchIndex/nextIndex in the etcd raft paper
match uint64
next uint64
snapshotIndex uint64
state remoteStateType
active bool
}
func (r *remote) String() string {
return fmt.Sprintf("match:%d,next:%d,state:%s,si:%d",
r.match, r.next, r.state, r.snapshotIndex)
}
func (r *remote) reset() {
r.snapshotIndex = 0
}
func (r *remote) becomeRetry() {
if r.state == remoteSnapshot {
r.next = max(r.match+1, r.snapshotIndex+1)
} else {
r.next = r.match + 1
}
r.reset()
r.state = remoteRetry
}
func (r *remote) retryToWait() {
if r.state == remoteRetry {
r.state = remoteWait
}
}
func (r *remote) waitToRetry() {
if r.state == remoteWait {
r.state = remoteRetry
}
}
func (r *remote) becomeWait() {
r.becomeRetry()
r.retryToWait()
}
func (r *remote) becomeReplicate() {
r.next = r.match + 1
r.reset()
r.state = remoteReplicate
}
func (r *remote) becomeSnapshot(index uint64) {
r.reset()
r.snapshotIndex = index
r.state = remoteSnapshot
}
func (r *remote) clearPendingSnapshot() {
r.snapshotIndex = 0
}
func (r *remote) tryUpdate(index uint64) bool {
if r.next < index+1 {
r.next = index + 1
}
if r.match < index {
r.waitToRetry()
r.match = index
return true
}
return false
}
func (r *remote) progress(lastIndex uint64) {
if r.state == remoteReplicate {
r.next = lastIndex + 1
} else if r.state == remoteRetry {
r.retryToWait()
} else {
panic("unexpected remote state")
}
}
func (r *remote) respondedTo() {
if r.state == remoteRetry {
r.becomeReplicate()
} else if r.state == remoteSnapshot {
if r.match >= r.snapshotIndex {
r.becomeRetry()
}
}
}
func (r *remote) decreaseTo(rejected uint64, last uint64) bool {
if r.state == remoteReplicate {
if rejected <= r.match {
// stale msg
return false
}
r.next = r.match + 1
return true
}
if r.next-1 != rejected {
// stale
return false
}
r.waitToRetry()
r.next = max(1, min(rejected, last+1))
return true
}
func (r *remote) isPaused() bool {
switch r.state {
case remoteRetry:
return false
case remoteWait:
return true
case remoteReplicate:
return false
case remoteSnapshot:
return true
default:
panic("unexpected remote state")
}
}
func (r *remote) isActive() bool {
return r.active
}
func (r *remote) setActive() {
r.active = true
}
func (r *remote) setNotActive() {
r.active = false
}