-
Notifications
You must be signed in to change notification settings - Fork 0
/
statemachine.go
79 lines (71 loc) · 2.63 KB
/
statemachine.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
package dragonboat
import (
"encoding/binary"
"fmt"
sm "github.com/lni/dragonboat/v4/statemachine"
"io"
)
// StateMachine is the IStateMachine implementation used in the example
// for handling all inputs not ends with "?".
// See https://github.com/lni/dragonboat/blob/master/statemachine/rsm.go for
// more details of the IStateMachine interface.
type StateMachine struct {
ShardID uint64
ReplicaID uint64
Count uint64
}
// NewStateMachine creates and return a new StateMachine object.
func NewStateMachine(shardID uint64, replicaID uint64) sm.IStateMachine {
return &StateMachine{
ShardID: shardID,
ReplicaID: replicaID,
Count: 0,
}
}
// Lookup performs local lookup on the StateMachine instance. In this example,
// we always return the Count value as a little endian binary encoded byte
// slice.
func (s *StateMachine) Lookup(query interface{}) (interface{}, error) {
result := make([]byte, 8)
binary.LittleEndian.PutUint64(result, s.Count)
return result, nil
}
// Update updates the object using the specified committed raft entry.
func (s *StateMachine) Update(e sm.Entry) (sm.Result, error) {
// in this example, we print out the following message for each
// incoming update request. we also increase the counter by one to remember
// how many updates we have applied
s.Count++
fmt.Printf("from StateMachine.Update(), msg: %s, count:%d\n",
string(e.Cmd), s.Count)
return sm.Result{Value: uint64(len(e.Cmd))}, nil
}
// SaveSnapshot saves the current IStateMachine state into a snapshot using the
// specified io.Writer object.
func (s *StateMachine) SaveSnapshot(w io.Writer,
fc sm.ISnapshotFileCollection, done <-chan struct{}) error {
// as shown above, the only state that can be saved is the Count variable
// there is no external file in this IStateMachine example, we thus leave
// the fc untouched
data := make([]byte, 8)
binary.LittleEndian.PutUint64(data, s.Count)
_, err := w.Write(data)
return err
}
// RecoverFromSnapshot recovers the state using the provided snapshot.
func (s *StateMachine) RecoverFromSnapshot(r io.Reader,
files []sm.SnapshotFile, done <-chan struct{}) error {
// restore the Count variable, that is the only state we maintain in this
// example, the input files is expected to be empty
data, err := io.ReadAll(r)
if err != nil {
return err
}
v := binary.LittleEndian.Uint64(data)
s.Count = v
return nil
}
// Close closes the IStateMachine instance. There is nothing for us to cleanup
// or release as this is a pure in memory data store. Note that the Close
// method is not guaranteed to be called as node can crash at any time.
func (s *StateMachine) Close() error { return nil }