-
Notifications
You must be signed in to change notification settings - Fork 1
/
raft.go
64 lines (50 loc) · 1.47 KB
/
raft.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
package raft
import (
"fmt"
"net"
"os"
"path/filepath"
"time"
"github.com/dgraph-io/badger"
"github.com/hashicorp/raft"
raftboltdb "github.com/hashicorp/raft-boltdb"
"../store"
)
func NewRaft(path string) (*raft.Raft, error) {
fsm, err := NewFSM(path, path)
if err != nil {
return nil, fmt.Errorf("Couldn't create new FSM: %v", err)
}
boltDB, err := raftboltdb.NewBoltStore(filepath.Join("./", "raft.db"))
if err != nil {
return nil, fmt.Errorf("Couldn't create new Bolt Store: %v", err)
}
ss, err := raft.NewFileSnapshotStore("./", 1, os.Stderr)
if err != nil {
return nil, fmt.Errorf("Couldn't create new File Snapshot Store: %v", err)
}
addr, err := net.ResolveTCPAddr("tcp", "127.0.0.1:5379")
if err != nil {
return nil, fmt.Errorf("Couldn't create new TCP endpoint: %v", err)
}
transport, err := raft.NewTCPTransport(":15379", addr, 3, 10*time.Second, os.Stderr)
if err != nil {
return nil, fmt.Errorf("Couldn't create new TCP transport: %v", err)
}
defaultRaftCfg := raft.DefaultConfig()
defaultRaftCfg.LocalID = raft.ServerID("local1")
r, err := raft.NewRaft(defaultRaftCfg, fsm, boltDB, boltDB, ss, transport)
if err != nil {
return nil, fmt.Errorf("Couldn't Create new Raft: %v", err)
}
return r, nil
}
func NewFSM(dir, valueDir string) (raft.FSM, error) {
badgerOpt := badger.DefaultOptions("./")
badgerDB, err := badger.Open(badgerOpt)
if err != nil {
return nil, err
}
fsmStore := store.NewBadger(badgerDB)
return fsmStore, nil
}