-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmain.go
174 lines (145 loc) · 3.86 KB
/
main.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
package main
import (
"errors"
"flag"
"fmt"
"log"
"net"
"os"
"path/filepath"
"raft-redis-cluster/raft"
"raft-redis-cluster/store"
"raft-redis-cluster/transport"
"strings"
"time"
hraft "github.com/hashicorp/raft"
raftboltdb "github.com/hashicorp/raft-boltdb"
)
// initialPeersList nodeID->address mapping
type initialPeersList []Peer
type Peer struct {
NodeID string
RaftAddr string
RedisAddr string
}
// Set initialPeersList
// nodeID=raftAddress|RedisAddress,nodeID=raftAddress|RedisAddress...
func (i *initialPeersList) Set(value string) error {
node := strings.Split(value, ",")
for _, n := range node {
nodes := strings.Split(n, "=")
if len(nodes) != 2 {
return errors.New("invalid peer format. expected nodeID=raftAddress|RedisAddress")
}
address := strings.Split(nodes[1], "|")
if len(address) != 2 {
fmt.Println(address)
return errors.New("invalid peer format. expected nodeID=raftAddress|RedisAddress")
}
*i = append(*i, Peer{
NodeID: nodes[0],
RaftAddr: address[0],
RedisAddr: address[1],
})
}
return nil
}
func (i *initialPeersList) String() string {
return fmt.Sprintf("%v", *i)
}
var (
raftAddr = flag.String("address", "localhost:50051", "TCP host+port for this raft node")
redisAddr = flag.String("redis_address", "localhost:6379", "TCP host+port for redis")
serverID = flag.String("server_id", "", "Node id used by Raft")
dataDir = flag.String("data_dir", "", "Raft data dir")
initialPeers = initialPeersList{}
)
func init() {
flag.Var(&initialPeers, "initial_peers", "Initial peers for the Raft cluster")
flag.Parse()
validateFlags()
}
func validateFlags() {
if *serverID == "" {
log.Fatalf("flag --server_id is required")
}
if *raftAddr == "" {
log.Fatalf("flag --address is required")
}
if *redisAddr == "" {
log.Fatalf("flag --redis_address is required")
}
if *dataDir == "" {
log.Fatalf("flag --data_dir is required")
}
}
func main() {
datastore := store.NewMemoryStore()
st := raft.NewStateMachine(datastore)
r, sdb, err := NewRaft(*dataDir, *serverID, *raftAddr, st, initialPeers)
if err != nil {
log.Fatalln(err)
}
redis := transport.NewRedis(hraft.ServerID(*serverID), r, datastore, sdb)
err = redis.Serve(*redisAddr)
if err != nil {
log.Fatalln(err)
}
}
// snapshotRetainCount スナップショットの保持数
const snapshotRetainCount = 2
func NewRaft(baseDir string, id string, address string, fsm hraft.FSM, nodes initialPeersList) (*hraft.Raft, hraft.StableStore, error) {
c := hraft.DefaultConfig()
c.LocalID = hraft.ServerID(id)
ldb, err := raftboltdb.NewBoltStore(filepath.Join(baseDir, "logs.dat"))
if err != nil {
return nil, nil, err
}
sdb, err := raftboltdb.NewBoltStore(filepath.Join(baseDir, "stable.dat"))
if err != nil {
return nil, nil, err
}
fss, err := hraft.NewFileSnapshotStore(baseDir, snapshotRetainCount, os.Stderr)
if err != nil {
return nil, nil, err
}
tcpAddr, err := net.ResolveTCPAddr("tcp", address)
if err != nil {
return nil, nil, err
}
tm, err := hraft.NewTCPTransport(address, tcpAddr, 10, time.Second*10, os.Stderr)
if err != nil {
return nil, nil, err
}
r, err := hraft.NewRaft(c, fsm, ldb, sdb, fss, tm)
if err != nil {
return nil, nil, err
}
cfg := hraft.Configuration{
Servers: []hraft.Server{
{
Suffrage: hraft.Voter,
ID: hraft.ServerID(id),
Address: hraft.ServerAddress(address),
},
},
}
// 自分以外の initialPeers を追加
for _, peer := range nodes {
sid := hraft.ServerID(peer.NodeID)
cfg.Servers = append(cfg.Servers, hraft.Server{
Suffrage: hraft.Voter,
ID: sid,
Address: hraft.ServerAddress(peer.RaftAddr),
})
err := store.SetRedisAddrByNodeID(sdb, sid, peer.RedisAddr)
if err != nil {
return nil, nil, err
}
}
f := r.BootstrapCluster(cfg)
if err := f.Error(); err != nil {
return nil, nil, err
}
return r, sdb, nil
}