-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsm_store.go
342 lines (283 loc) · 7.91 KB
/
fsm_store.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Package store provides a simple distributed key-value store. The keys and
// associated values are changed via distributed consensus, meaning that the
// values are changed only when a majority of nodes in the cluster agree on
// the new value.
//
// Distributed consensus is provided via the Raft algorithm.
package Orangedb
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"net"
"os"
"path/filepath"
"sync"
"time"
"github.com/hashicorp/raft"
"github.com/hashicorp/raft-boltdb"
"github.com/laohanlinux/go-logger/logger"
"github.com/hashicorp/go-msgpack/codec"
)
const (
retainSnapshotCount = 2
raftTimeout = 10 * time.Second
)
// Store is a simple key-value store, where all changes are made via Raft consensus.
//实现了 service.Store interface and raft.FSM interface
type FsmStore struct {
RaftDir string
RaftBind string
mu sync.Mutex
m map[string]string // The key-value store for the system.
Raft *raft.Raft // The consensus mechanism //todo(shili )raft 功能放置的位置不好
BatchSyncCh chan *BatchTxn //该通道用于接收batchTxn,并拷贝到follower
}
// New returns a new Store.
func NewFsmStore() *FsmStore {
return &FsmStore{
m: make(map[string]string),
}
}
//返回raft 服务的结构
func (s *FsmStore) GetRaftSevice() *raft.Raft {
return s.Raft
}
// Open opens the store. If enableSingle is set, and there are no existing peers,
// then this node becomes the first node, and therefore leader, of the cluster.
func (s *FsmStore) Open(enableSingle bool) error {
// Setup Raft configuration.
config := raft.DefaultConfig()
// Check for any existing peers.
peers, err := readPeersJSON(filepath.Join(s.RaftDir, "peers.json"))
if err != nil {
return err
}
// Allow the node to entry single-mode, potentially electing itself, if
// explicitly enabled and there is only 1 node in the cluster already.
if enableSingle && len(peers) <= 1 {
logger.Infof("enabling single-node mode")
config.EnableSingleNode = true
config.DisableBootstrapAfterElect = false
}
// Setup Raft communication.
addr, err := net.ResolveTCPAddr("tcp", s.RaftBind)
if err != nil {
return err
}
transport, err := raft.NewTCPTransport(s.RaftBind, addr, 3, 10 * time.Second, os.Stderr)
if err != nil {
return err
}
// Create peer storage.
peerStore := raft.NewJSONPeers(s.RaftDir, transport)
// Create the snapshot store. This allows the Raft to truncate the log.
snapshots, err := raft.NewFileSnapshotStore(s.RaftDir, retainSnapshotCount, os.Stderr)
if err != nil {
return fmt.Errorf("file snapshot store: %s", err)
}
// Create the log store and stable store.
logStore, err := raftboltdb.NewBoltStore(filepath.Join(s.RaftDir, "raft.db"))
if err != nil {
return fmt.Errorf("new bolt store: %s", err)
}
// Instantiate the Raft systems.
ra, err := raft.NewRaft(config, (*FsmStore)(s), logStore, logStore, snapshots, peerStore, transport)
if err != nil {
return fmt.Errorf("new raft: %s", err)
}
s.Raft = ra
return nil
}
// Get returns the value for the given key. 直接本地设置
func (s *FsmStore) Get(key string) (string, error) {
s.mu.Lock()
logger.Info("Get a value by ", string(key))
defer s.mu.Unlock()
return s.m[key], nil
}
// Set sets the value for the given key. 发送master raft ,然后在进行set
func (s *FsmStore) Set(key, value string) error {
if s.Raft.State() != raft.Leader {
logger.Infof("not leader")
return ErrNotLeader
}
applyCmd := &applyCommand{
Op: "set",
Key: key,
Value: value,
}
bs, err := s.MsgpackEncode(applyCmd);
if nil != err {
logger.Warnf("encode batchTxn fail,err=%v", err)
return err
}
c := &Command{
Type: ApplyCommand,
Data: bs,
}
b, err := s.MsgpackEncode(c);
if err != nil {
return err
}
f := s.Raft.Apply(b, raftTimeout)
return f.Error()
}
func (s *FsmStore) MsgpackEncode(ts interface{}) (bs []byte, err error) {
err = codec.NewEncoderBytes(&bs, &codec.MsgpackHandle{}).Encode(ts)
return
}
// Delete deletes the given key. 发送master raft ,然后在进行删除
func (s *FsmStore) Delete(key string) error {
if s.Raft.State() != raft.Leader {
logger.Infof("not leader")
return ErrNotLeader
}
applyCmd := &applyCommand{
Op: "del",
Key: key,
}
bs, err := s.MsgpackEncode(applyCmd);
if nil != err {
logger.Warnf("encode batchTxn fail,err=%v", err)
return err
}
c := &Command{
Type: ApplyCommand,
Data: bs,
}
b, err := s.MsgpackEncode(c);
if err != nil {
return err
}
f := s.Raft.Apply(b, raftTimeout)
return f.Error()
}
// Join joins a node, located at addr, to this store. The node must be ready to
// respond to Raft communications at that address.
func (s *FsmStore) Join(addr string) error {
logger.Infof("received join request for remote node as %s", addr)
f := s.Raft.AddPeer(addr)
if f.Error() != nil {
return f.Error()
}
logger.Infof("node at %s joined successfully", addr)
return nil
}
func (s *FsmStore) MsgpackDecode(buf []byte, ts interface{}) error {
return codec.NewDecoderBytes(buf, &codec.MsgpackHandle{}).Decode(ts)
}
//raft.FSM接口
// Apply applies a Raft log entry to the key-value store.
func (f *FsmStore) Apply(l *raft.Log) interface{} {
var c Command
if err := f.MsgpackDecode(l.Data, &c); err != nil {
panic(fmt.Sprintf("failed to unmarshal command: %s", err.Error()))
}
switch c.Type {
case ApplyCommand:
var applyCmd applyCommand
if err := f.MsgpackDecode(c.Data, &applyCmd); err != nil {
panic(fmt.Sprintf("failed to unmarshal command: %s", err.Error()))
}
switch applyCmd.Op {
case "set":
logger.Debug("set a key:value ", string(applyCmd.Key), string(applyCmd.Value))
return f.applySet(applyCmd.Key, applyCmd.Value)
case "del":
logger.Debug("delete a key ", string(applyCmd.Key))
return f.applyDelete(applyCmd.Key)
default:
panic(fmt.Sprintf("unrecognized command op: %s", applyCmd.Op))
}
case BatchSubCommand:
var batchCmd batchSubCommand
if err := f.MsgpackDecode(c.Data, &batchCmd); err != nil {
panic(fmt.Sprintf("failed to unmarshal command: %s", err.Error()))
}
f.BatchSyncCh<- batchCmd.BatchTxn //像 reader 传递一个信息
}
return nil
}
//raft.FSM接口
// Snapshot returns a snapshot of the key-value store.
func (f *FsmStore) Snapshot() (raft.FSMSnapshot, error) {
f.mu.Lock()
defer f.mu.Unlock()
// Clone the map.
o := make(map[string]string)
for k, v := range f.m {
o[k] = v
}
return &fsmSnapshot{store: o}, nil
}
//raft.FSM接口
// Restore stores the key-value store to a previous state.
func (f *FsmStore) Restore(rc io.ReadCloser) error {
o := make(map[string]string)
if err := json.NewDecoder(rc).Decode(&o); err != nil {
return err
}
// Set the state from the snapshot, no lock required according to
// Hashicorp docs.
f.m = o
return nil
}
func (f *FsmStore) applySet(key, value string) interface{} {
f.mu.Lock()
defer f.mu.Unlock()
//logger.Info("set a key:value ", string(key),string(value))
f.m[key] = value
return nil
}
func (f *FsmStore) applyDelete(key string) interface{} {
f.mu.Lock()
defer f.mu.Unlock()
//logger.Info("delete a value by ", string(key))
delete(f.m, key)
return nil
}
type fsmSnapshot struct {
store map[string]string
}
func (f *fsmSnapshot) Persist(sink raft.SnapshotSink) error {
err := func() error {
// Encode data.
b, err := json.Marshal(f.store)
if err != nil {
return err
}
// Write data to sink.
if _, err := sink.Write(b); err != nil {
return err
}
// Close the sink.
if err := sink.Close(); err != nil {
return err
}
return nil
}()
if err != nil {
sink.Cancel()
return err
}
return nil
}
func (f *fsmSnapshot) Release() {}
func readPeersJSON(path string) ([]string, error) {
b, err := ioutil.ReadFile(path)
if err != nil && !os.IsNotExist(err) {
return nil, err
}
if len(b) == 0 {
return nil, nil
}
var peers []string
dec := json.NewDecoder(bytes.NewReader(b))
if err := dec.Decode(&peers); err != nil {
return nil, err
}
return peers, nil
}