-
Notifications
You must be signed in to change notification settings - Fork 2
/
statemachine.go
108 lines (97 loc) · 3.59 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
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
// Copyright 2017,2018 Lei Ni ([email protected]).
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package main
import (
"encoding/binary"
"fmt"
"io"
"io/ioutil"
"github.com/lni/dragonboat/statemachine"
)
// ExampleStateMachine 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 ExampleStateMachine struct {
ClusterID uint64
NodeID uint64
Count uint64
}
// NewExampleStateMachine creates and return a new ExampleStateMachine object.
func NewExampleStateMachine(clusterID uint64,
nodeID uint64) statemachine.IStateMachine {
return &ExampleStateMachine{
ClusterID: clusterID,
NodeID: nodeID,
Count: 0,
}
}
// Lookup performs local lookup on the ExampleStateMachine instance. In this example,
// we always return the Count value as a little endian binary encoded byte
// slice.
func (s *ExampleStateMachine) Lookup(query []byte) []byte {
result := make([]byte, 8)
binary.LittleEndian.PutUint64(result, s.Count)
return result
}
// Update updates the object using the specified committed raft entry.
func (s *ExampleStateMachine) Update(data []byte) uint64 {
// 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 ExampleStateMachine.Update(), msg: %s, count:%d\n",
string(data), s.Count)
return uint64(len(data))
}
// SaveSnapshot saves the current IStateMachine state into a snapshot using the
// specified io.Writer object.
func (s *ExampleStateMachine) SaveSnapshot(w io.Writer,
fc statemachine.ISnapshotFileCollection,
done <-chan struct{}) (uint64, 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)
if err != nil {
return 0, err
}
return uint64(len(data)), nil
}
// RecoverFromSnapshot recovers the state using the provided snapshot.
func (s *ExampleStateMachine) RecoverFromSnapshot(r io.Reader,
files []statemachine.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 := ioutil.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 *ExampleStateMachine) Close() {}
// GetHash returns a uint64 representing the current object state.
func (s *ExampleStateMachine) GetHash() uint64 {
// the only state we have is that Count variable. that uint64 value pretty much
// represents the state of this IStateMachine
return s.Count
}