@@ -48,68 +48,87 @@ type FSMSnapshot interface {
48
48
// the FSM to block our internal operations.
49
49
func (r * Raft ) runFSM () {
50
50
var lastIndex , lastTerm uint64
51
- for {
52
- select {
53
- case req := <- r .fsmRestoreCh :
54
- // Open the snapshot
55
- meta , source , err := r .snapshots .Open (req .ID )
56
- if err != nil {
57
- req .respond (fmt .Errorf ("failed to open snapshot %v: %v" , req .ID , err ))
58
- continue
59
- }
60
51
61
- // Attempt to restore
52
+ commit := func (req * commitTuple ) {
53
+ // Apply the log if a command
54
+ var resp interface {}
55
+ if req .log .Type == LogCommand {
62
56
start := time .Now ()
63
- if err := r .fsm .Restore (source ); err != nil {
64
- req .respond (fmt .Errorf ("failed to restore snapshot %v: %v" , req .ID , err ))
65
- source .Close ()
66
- continue
67
- }
57
+ resp = r .fsm .Apply (req .log )
58
+ metrics .MeasureSince ([]string {"raft" , "fsm" , "apply" }, start )
59
+ }
60
+
61
+ // Update the indexes
62
+ lastIndex = req .log .Index
63
+ lastTerm = req .log .Term
64
+
65
+ // Invoke the future if given
66
+ if req .future != nil {
67
+ req .future .response = resp
68
+ req .future .respond (nil )
69
+ }
70
+ }
71
+
72
+ restore := func (req * restoreFuture ) {
73
+ // Open the snapshot
74
+ meta , source , err := r .snapshots .Open (req .ID )
75
+ if err != nil {
76
+ req .respond (fmt .Errorf ("failed to open snapshot %v: %v" , req .ID , err ))
77
+ return
78
+ }
79
+
80
+ // Attempt to restore
81
+ start := time .Now ()
82
+ if err := r .fsm .Restore (source ); err != nil {
83
+ req .respond (fmt .Errorf ("failed to restore snapshot %v: %v" , req .ID , err ))
68
84
source .Close ()
69
- metrics .MeasureSince ([]string {"raft" , "fsm" , "restore" }, start )
85
+ return
86
+ }
87
+ source .Close ()
88
+ metrics .MeasureSince ([]string {"raft" , "fsm" , "restore" }, start )
70
89
71
- // Update the last index and term
72
- lastIndex = meta .Index
73
- lastTerm = meta .Term
74
- req .respond (nil )
90
+ // Update the last index and term
91
+ lastIndex = meta .Index
92
+ lastTerm = meta .Term
93
+ req .respond (nil )
94
+ }
75
95
76
- case req := <- r . fsmSnapshotCh :
77
- // Is there something to snapshot?
78
- if lastIndex == 0 {
79
- req .respond (ErrNothingNewToSnapshot )
80
- continue
81
- }
96
+ snapshot := func ( req * reqSnapshotFuture ) {
97
+ // Is there something to snapshot?
98
+ if lastIndex == 0 {
99
+ req .respond (ErrNothingNewToSnapshot )
100
+ return
101
+ }
82
102
83
- // Start a snapshot
84
- start := time .Now ()
85
- snap , err := r .fsm .Snapshot ()
86
- metrics .MeasureSince ([]string {"raft" , "fsm" , "snapshot" }, start )
87
-
88
- // Respond to the request
89
- req .index = lastIndex
90
- req .term = lastTerm
91
- req .snapshot = snap
92
- req .respond (err )
93
-
94
- case commitEntry := <- r .fsmCommitCh :
95
- // Apply the log if a command
96
- var resp interface {}
97
- if commitEntry .log .Type == LogCommand {
98
- start := time .Now ()
99
- resp = r .fsm .Apply (commitEntry .log )
100
- metrics .MeasureSince ([]string {"raft" , "fsm" , "apply" }, start )
101
- }
103
+ // Start a snapshot
104
+ start := time .Now ()
105
+ snap , err := r .fsm .Snapshot ()
106
+ metrics .MeasureSince ([]string {"raft" , "fsm" , "snapshot" }, start )
102
107
103
- // Update the indexes
104
- lastIndex = commitEntry .log .Index
105
- lastTerm = commitEntry .log .Term
108
+ // Respond to the request
109
+ req .index = lastIndex
110
+ req .term = lastTerm
111
+ req .snapshot = snap
112
+ req .respond (err )
113
+ }
114
+
115
+ for {
116
+ select {
117
+ case ptr := <- r .fsmMutateCh :
118
+ switch req := ptr .(type ) {
119
+ case * commitTuple :
120
+ commit (req )
106
121
107
- // Invoke the future if given
108
- if commitEntry .future != nil {
109
- commitEntry .future .response = resp
110
- commitEntry .future .respond (nil )
122
+ case * restoreFuture :
123
+ restore (req )
124
+
125
+ default :
126
+ panic (fmt .Errorf ("bad type passed to fsmMutateCh: %#v" , ptr ))
111
127
}
112
128
129
+ case req := <- r .fsmSnapshotCh :
130
+ snapshot (req )
131
+
113
132
case <- r .shutdownCh :
114
133
return
115
134
}
0 commit comments