-
Notifications
You must be signed in to change notification settings - Fork 1
/
fsm.go
133 lines (123 loc) · 2.98 KB
/
fsm.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
package papillon
import (
"io"
)
type FSM interface {
Apply(*LogEntry) interface{}
ReStore(reader io.ReadCloser) error // 从快照恢复,需要自行实现觅等
Snapshot() (FsmSnapshot, error)
}
type FsmSnapshot interface {
Persist(sink SnapshotSink) error
Release()
}
type BatchFSM interface {
FSM
BatchApply([]*LogEntry) []interface{}
}
// runFSM 状态机线程
func (r *Raft) runFSM() {
var (
batchFSM, canBatchApply = r.fsm.(BatchFSM)
configurationStore, canConfigurationStore = r.kvStore.(ConfigurationStorage)
lastAppliedTerm, lastAppliedIdx uint64
)
processConfiguration := func(fu *LogFuture) {
if fu.log.Type != LogCluster || !canConfigurationStore {
return
}
configurationStore.SetConfiguration(fu.log.Index, DecodeCluster(fu.log.Data))
}
setFsmLastApplied := func(log *LogEntry) {
lastAppliedIdx = log.Index
lastAppliedTerm = log.Term
}
for {
select {
case <-r.shutDown.C:
return
case futures := <-r.fsmApplyCh:
switch {
case canBatchApply:
applyBatch(futures, batchFSM, processConfiguration)
if len(futures) > 0 {
setFsmLastApplied(futures[len(futures)-1].log)
}
default:
for _, future := range futures {
applySingle(future, r.fsm, processConfiguration)
setFsmLastApplied(future.log)
}
}
r.readOnly.notify(lastAppliedIdx)
case fu := <-r.fsmRestoreCh:
meta, err := r.recoverSnapshotByID(fu.ID)
fu.responded(nil, err)
if err == nil {
setFsmLastApplied(&LogEntry{Term: meta.Term, Index: meta.Index})
}
r.readOnly.notify(lastAppliedIdx)
case fu := <-r.fsmSnapshotCh:
r.processSnapshot(fu, lastAppliedTerm, lastAppliedIdx)
case fu := <-r.readOnly.request:
if fu.readIndex <= lastAppliedIdx {
fu.responded(lastAppliedIdx, nil)
continue
}
r.readOnly.observe(fu)
}
}
}
func canApply(future *LogFuture) bool {
switch future.log.Type {
case LogCommand:
return true
}
return false
}
func (r *Raft) processSnapshot(fu *fsmSnapshotFuture, lastAppliedTerm, lastAppliedIdx uint64) {
if lastAppliedIdx == 0 {
fu.fail(ErrNothingNewToSnapshot)
return
}
snapshot, err := r.fsm.Snapshot()
if err != nil {
r.logger.Errorf("fsm generate snap shot err :%s", err)
}
fu.responded(&SnapShotFutureResp{
term: lastAppliedTerm,
index: lastAppliedIdx,
fsmSnapshot: snapshot,
}, err)
}
func applySingle(fu *LogFuture, fsm FSM, callback func(fu *LogFuture)) error {
callback(fu)
if !canApply(fu) {
fu.responded(nil, nil)
return nil
}
fu.responded(fsm.Apply(fu.log), nil)
return nil
}
func applyBatch(futures []*LogFuture, batchFSM BatchFSM, callback func(fu *LogFuture)) {
var (
logs []*LogEntry
respFutures []*LogFuture
)
for _, fu := range futures {
callback(fu)
if canApply(fu) {
logs = append(logs, fu.log)
respFutures = append(respFutures, fu)
} else {
fu.success()
}
}
if len(logs) == 0 {
return
}
resp := batchFSM.BatchApply(logs)
for i, fu := range respFutures {
fu.responded(resp[i], nil)
}
}