-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm.go
72 lines (53 loc) · 1.18 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
package raft
import (
"io"
"github.com/hashicorp/raft"
)
// SharedState is a interface representing
// a log / transaction based state which
// can be shared over multiple nodes.
// The raft protocol ensures that the state
// is equalized on all nodes
type SharedState interface {
// AppendLogMessage decodes the log message
// and applies the represented change to the
// local state
AppendLogMessage([]byte)
// Encode encodes the current state into
// the writer
Encode(io.Writer) error
// Decode decodes a state from the reader
// into the local state
Decode(io.Reader) error
}
type fsm struct {
state SharedState
}
func (f *fsm) Apply(l *raft.Log) interface{} {
f.state.AppendLogMessage(l.Data)
return nil
}
func (f *fsm) Snapshot() (raft.FSMSnapshot, error) {
return f, nil
}
func (f *fsm) Restore(r io.ReadCloser) error {
defer r.Close()
f.state.Decode(r)
return nil
}
func (f *fsm) Persist(sink raft.SnapshotSink) error {
err := func() error {
if err := f.state.Encode(sink); err != nil {
return err
}
if err := sink.Close(); err != nil {
return err
}
return nil
}()
if err != nil {
sink.Cancel()
}
return err
}
func (f *fsm) Release() {}