-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraft.go
67 lines (62 loc) · 1.61 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
65
66
67
package flowdb
import (
"github.com/hashicorp/raft"
raftboltdb "github.com/hashicorp/raft-boltdb"
"net"
"os"
"path/filepath"
"strings"
"time"
)
func NewRaft(raftAddr, raftId, raftDir string) (*raft.Raft, *FSM, error) {
config := raft.DefaultConfig()
config.LocalID = raft.ServerID(raftId)
addr, err := net.ResolveTCPAddr("tcp", raftAddr)
if err != nil {
return nil, nil, err
}
transport, err := raft.NewTCPTransport(raftAddr, addr, 2, 5*time.Second, os.Stdout)
if err != nil {
return nil, nil, err
}
snapshots, err := raft.NewFileSnapshotStore(raftDir, 2, os.Stdout)
if err != nil {
return nil, nil, err
}
logStore, err := raftboltdb.NewBoltStore(filepath.Join(raftDir, "raft-log.db"))
if err != nil {
return nil, nil, err
}
stableStore, err := raftboltdb.NewBoltStore(filepath.Join(raftDir, "raft-stable.db"))
if err != nil {
return nil, nil, err
}
fsm := NewFSM()
rf, err := raft.NewRaft(config, fsm, logStore, stableStore, snapshots, transport)
if err != nil {
return nil, nil, err
}
return rf, fsm, nil
}
func Bootstrap(rf *raft.Raft, raftAddr, raftId, raftCluster string) {
servers := rf.GetConfiguration().Configuration().Servers
if len(servers) > 0 {
return
}
peerArr := strings.Split(raftCluster, ",")
if len(peerArr) == 0 {
return
}
var configuration raft.Configuration
for _, peerInfo := range peerArr {
peer := strings.Split(peerInfo, "/")
id := peer[0]
addr := peer[1]
server := raft.Server{
ID: raft.ServerID(id),
Address: raft.ServerAddress(addr),
}
configuration.Servers = append(configuration.Servers, server)
}
rf.BootstrapCluster(configuration)
}