forked from fuyao-w/papillon
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi.go
More file actions
351 lines (324 loc) · 8.68 KB
/
api.go
File metadata and controls
351 lines (324 loc) · 8.68 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
package papillon
import (
"errors"
"io"
"time"
)
const (
backoffBaseDuration = time.Millisecond * 10
maxBackoffRounds = 10
)
var (
ErrNotExist = errors.New("not exist")
ErrPipelineReplicationNotSupported = errors.New("pipeline replication not supported")
ErrNotFound = customError{"not found"}
ErrNotLeader = errors.New("not leader")
ErrCantBootstrap = errors.New("bootstrap only works on new clusters")
ErrIllegalConfiguration = errors.New("illegal clusterInfo")
ErrShutDown = errors.New("shut down")
ErrNotStarted = errors.New("not started")
ErrLeadershipTransferInProgress = errors.New("leader ship transfer in progress")
// ErrAbortedByRestore is returned when a leader fails to commit a log
// entry because it's been superseded by a user snapshot restore.
ErrAbortedByRestore = errors.New("snapshot restored while committing log")
ErrEnqueueTimeout = errors.New("timed out enqueuing operation")
ErrTimeout = errors.New("time out")
ErrPipelineShutdown = errors.New("append pipeline closed")
ErrNotVoter = errors.New("not voter")
ErrLeadershipTransferFail = errors.New("not found transfer peer")
ErrLeadershipLost = errors.New("leadership lost")
ErrNothingNewToSnapshot = errors.New("nothing new to snapshot")
ErrEmptyCommit = errors.New("empty commit")
ErrPrevLogNotMatch = errors.New("prev log term not match")
)
func genTimeoutCh(timeout time.Duration) (tm <-chan time.Time) {
if timeout > 0 {
tm = time.After(timeout)
}
return tm
}
type customError struct{ string }
func (e customError) Error() string {
return e.string
}
func (e customError) Is(err error) bool {
if err == nil {
return false
}
return e.Error() == err.Error()
}
func (r *Raft) BootstrapCluster(configuration ClusterInfo) defaultFuture {
future := &bootstrapFuture{
clusterInfo: configuration,
}
future.init()
if r == nil || r.GetState().String() == unknown {
future.fail(ErrNotStarted)
return future
}
select {
case <-r.shutDown.C:
future.fail(ErrShutDown)
case r.commandCh <- &command{enum: commandBootstrap, callback: future}:
}
return future
}
func (r *Raft) LeaderInfo() (ServerID, ServerAddr) {
info := r.leaderInfo.Get()
return info.ID, info.Addr
}
// Apply 向 raft 提交日志
func (r *Raft) Apply(data []byte, timeout time.Duration) ApplyFuture {
return r.apiApplyLog(&LogEntry{Data: data, Type: LogCommand}, timeout)
}
func (r *Raft) apiApplyLog(entry *LogEntry, timeout time.Duration) ApplyFuture {
var (
tm <-chan time.Time
)
if timeout > 0 {
tm = time.After(timeout)
}
var applyFuture = &LogFuture{
log: entry,
}
applyFuture.init()
if r == nil || r.GetState().String() == unknown {
applyFuture.fail(ErrNotStarted)
return applyFuture
}
select {
case <-tm:
return &errFuture[nilRespFuture]{errors.New("apply log time out")}
case <-r.shutDown.C:
return &errFuture[nilRespFuture]{ErrShutDown}
case r.apiLogApplyCh <- applyFuture: //batch apply
case r.commandCh <- &command{enum: commandLogApply, callback: applyFuture}: // 正常提交
}
return applyFuture
}
// VerifyLeader 验证当前节点是否是领导人
func (r *Raft) VerifyLeader() Future[bool] {
vf := &verifyFuture{}
vf.init()
if r == nil || r.GetState().String() == unknown {
vf.fail(ErrNotStarted)
return vf
}
select {
case <-r.shutDown.C:
vf.fail(ErrShutDown)
return &vf.deferResponse
case r.commandCh <- &command{enum: commandVerifyLeader, callback: vf}:
return &vf.deferResponse
}
}
// GetConfiguration 获取集群配置
func (r *Raft) GetConfiguration() ClusterInfo {
return r.configuration.Load()
}
func (r *Raft) requestClusterChange(req clusterChangeRequest, timeout time.Duration) IndexFuture {
var tm <-chan time.Time
if timeout > 0 {
tm = time.After(timeout)
}
var ccf = &clusterChangeFuture{
req: &req,
}
ccf.init()
if r == nil || r.GetState().String() == unknown {
ccf.fail(ErrNotStarted)
return ccf
}
select {
case <-tm:
return &errFuture[nilRespFuture]{err: errors.New("apply log time out")}
case <-r.shutDown.C:
return &errFuture[nilRespFuture]{err: ErrShutDown}
case r.commandCh <- &command{enum: commandClusterChange, callback: ccf}:
return ccf
}
}
func (r *Raft) AddServer(peer ServerInfo, prevIndex uint64, timeout time.Duration) IndexFuture {
return r.requestClusterChange(clusterChangeRequest{
command: addServer,
peer: peer,
pervIndex: prevIndex,
}, timeout)
}
func (r *Raft) RemoveServer(peer ServerInfo, prevIndex uint64, timeout time.Duration) IndexFuture {
return r.requestClusterChange(clusterChangeRequest{
command: removeServer,
peer: peer,
pervIndex: prevIndex,
}, timeout)
}
func (r *Raft) UpdateServer(peer ServerInfo, prevIndex uint64, timeout time.Duration) IndexFuture {
return r.requestClusterChange(clusterChangeRequest{
command: updateServer,
peer: peer,
pervIndex: prevIndex,
}, timeout)
}
func (r *Raft) SnapShot() Future[OpenSnapshot] {
fu := &apiSnapshotFuture{}
fu.init()
if r == nil || r.GetState().String() == unknown {
fu.fail(ErrNotStarted)
return fu
}
select {
case <-r.shutDown.C:
return &errFuture[OpenSnapshot]{ErrShutDown}
case r.apiSnapshotBuildCh <- fu:
return fu
}
}
// StateCh 状态变化的通知
func (r *Raft) StateCh() <-chan *StateChange {
return r.stateChangeCh
}
func (r *Raft) LastContact() time.Time {
return r.lastContact.Load()
}
func (r *Raft) LatestIndex() uint64 {
return r.getLatestIndex()
}
func (r *Raft) LastApplied() uint64 {
return r.getLastApply()
}
func (r *Raft) LeaderTransfer(id ServerID, address ServerAddr, timeout time.Duration) defaultFuture {
tm := genTimeoutCh(timeout)
future := &leadershipTransferFuture{
Peer: ServerInfo{
ID: id,
Addr: address,
},
}
future.init()
if r == nil || r.GetState().String() == unknown {
future.fail(ErrNotStarted)
return future
}
if len(id) > 0 && id == r.localInfo.ID {
future.fail(errors.New("can't transfer to itself"))
return future
}
select {
case r.commandCh <- &command{enum: commandLeadershipTransfer, callback: future}:
return future
case <-r.shutDown.C:
return &errFuture[nilRespFuture]{ErrShutDown}
case <-tm:
return &errFuture[nilRespFuture]{ErrEnqueueTimeout}
}
}
func (r *Raft) ReloadConfig(rc ReloadableConfig) error {
r.confReloadMu.Lock()
defer r.confReloadMu.Unlock()
if r == nil || r.GetState().String() == unknown {
return ErrNotStarted
}
oldConf := *r.Conf()
newConf := rc.apply(oldConf)
ok, hint := ValidateConfig(&newConf)
if !ok {
return errors.New(hint)
}
r.conf.Store(&newConf)
if newConf.HeartbeatTimeout <= oldConf.HeartbeatTimeout {
return nil
}
select {
case <-r.shutDown.C:
return ErrShutDown
case r.commandCh <- &command{enum: commandConfigReload, callback: &newConf}:
}
return nil
}
func (r *Raft) ReStoreSnapshot(meta *SnapshotMeta, reader io.ReadCloser) error {
fu := &userRestoreFuture{
meta: meta,
reader: reader,
}
fu.init()
if r == nil || r.GetState().String() == unknown {
return ErrNotStarted
}
select {
case r.commandCh <- &command{enum: commandSnapshotRestore, callback: fu}:
case <-r.shutDown.C:
return ErrShutDown
}
_, err := fu.Response()
if err != nil {
return err
}
applyFu := r.apiApplyLog(&LogEntry{Type: LogNoop}, 0)
_, err = applyFu.Response()
return err
}
func (r *Raft) ShutDown() defaultFuture {
var resp *shutDownFuture
if r == nil || r.GetState().String() == unknown {
fu := new(defaultDeferResponse)
fu.init()
fu.fail(ErrNotStarted)
return fu
}
r.shutDown.done(func(oldState bool) {
resp = new(shutDownFuture)
if !oldState {
resp.raft = r
}
r.setShutDown()
})
return resp
}
func (r *Raft) RaftStats() Future[map[string]interface{}] {
fu := new(deferResponse[map[string]interface{}])
fu.init()
if r == nil || r.GetState().String() == unknown {
fu.fail(ErrNotStarted)
return fu
}
r.commandCh <- &command{
enum: commandRaftStats,
callback: fu,
}
return fu
}
func (r *Raft) ReadIndex(timeout time.Duration) Future[uint64] {
tm := genTimeoutCh(timeout)
fu := new(deferResponse[uint64])
fu.init()
if r == nil || r.GetState().String() == unknown {
fu.fail(ErrNotStarted)
return fu
}
select {
case <-tm:
fu.fail(ErrTimeout)
case r.commandCh <- &command{
enum: commandReadIndex,
callback: fu,
}:
}
return fu
}
func (r *Raft) Barrier(readIndex uint64, timeout time.Duration) Future[uint64] {
tm := genTimeoutCh(timeout)
fu := &readOnlyFuture{
readIndex: readIndex,
}
fu.init()
if r == nil || r.GetState().String() == unknown {
fu.fail(ErrNotStarted)
return fu
}
select {
case <-tm:
fu.fail(ErrTimeout)
case r.readOnly.request <- fu:
}
return fu
}