-
Notifications
You must be signed in to change notification settings - Fork 635
/
raftstore.go
195 lines (176 loc) · 5 KB
/
raftstore.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
// Copyright 2018 The Chubao Authors.
//
// 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 raftstore
import (
"fmt"
syslog "log"
"os"
"path"
"strconv"
"time"
"github.com/tiglabs/raft"
"github.com/tiglabs/raft/logger"
"github.com/tiglabs/raft/proto"
"github.com/tiglabs/raft/storage/wal"
raftlog "github.com/tiglabs/raft/util/log"
)
// RaftStore defines the interface for the raft store.
type RaftStore interface {
CreatePartition(cfg *PartitionConfig) (Partition, error)
Stop()
RaftConfig() *raft.Config
RaftStatus(raftID uint64) (raftStatus *raft.Status)
NodeManager
RaftServer() *raft.RaftServer
}
type raftStore struct {
nodeID uint64
resolver NodeResolver
raftConfig *raft.Config
raftServer *raft.RaftServer
raftPath string
}
// RaftConfig returns the raft configuration.
func (s *raftStore) RaftConfig() *raft.Config {
return s.raftConfig
}
func (s *raftStore) RaftStatus(raftID uint64) (raftStatus *raft.Status) {
return s.raftServer.Status(raftID)
}
// AddNodeWithPort add a new node with the given port.
func (s *raftStore) AddNodeWithPort(nodeID uint64, addr string, heartbeat int, replicate int) {
s.resolver.AddNodeWithPort(nodeID, addr, heartbeat, replicate)
}
// DeleteNode deletes the node with the given ID in the raft store.
func (s *raftStore) DeleteNode(nodeID uint64) {
s.resolver.DeleteNode(nodeID)
}
// Stop stops the raft store server.
func (s *raftStore) Stop() {
if s.raftServer != nil {
s.raftServer.Stop()
}
}
func newRaftLogger(dir string) {
raftLogPath := path.Join(dir, "logs")
_, err := os.Stat(raftLogPath)
if err != nil {
if pathErr, ok := err.(*os.PathError); ok {
if os.IsNotExist(pathErr) {
os.MkdirAll(raftLogPath, 0755)
}
}
}
raftLog, err := raftlog.NewLog(raftLogPath, "raft", "debug")
if err != nil {
syslog.Println("Fatal: failed to start the baud storage daemon - ", err)
return
}
logger.SetLogger(raftLog)
return
}
// NewRaftStore returns a new raft store instance.
func NewRaftStore(cfg *Config) (mr RaftStore, err error) {
resolver := NewNodeResolver()
newRaftLogger(cfg.RaftPath)
rc := raft.DefaultConfig()
rc.NodeID = cfg.NodeID
rc.LeaseCheck = true
if cfg.HeartbeatPort <= 0 {
cfg.HeartbeatPort = DefaultHeartbeatPort
}
if cfg.ReplicaPort <= 0 {
cfg.ReplicaPort = DefaultReplicaPort
}
if cfg.NumOfLogsToRetain == 0 {
cfg.NumOfLogsToRetain = DefaultNumOfLogsToRetain
}
if cfg.ElectionTick < DefaultElectionTick {
cfg.ElectionTick = DefaultElectionTick
}
if cfg.TickInterval < DefaultTickInterval {
cfg.TickInterval = DefaultTickInterval
}
// if cfg's RecvBufSize bigger than the default 2048,
// use the bigger one.
if cfg.RecvBufSize > rc.ReqBufferSize {
rc.ReqBufferSize = cfg.RecvBufSize
}
rc.HeartbeatAddr = fmt.Sprintf("%s:%d", cfg.IPAddr, cfg.HeartbeatPort)
rc.ReplicateAddr = fmt.Sprintf("%s:%d", cfg.IPAddr, cfg.ReplicaPort)
rc.Resolver = resolver
rc.RetainLogs = cfg.NumOfLogsToRetain
rc.TickInterval = time.Duration(cfg.TickInterval) * time.Millisecond
rc.ElectionTick = cfg.ElectionTick
rs, err := raft.NewRaftServer(rc)
if err != nil {
return
}
mr = &raftStore{
nodeID: cfg.NodeID,
resolver: resolver,
raftConfig: rc,
raftServer: rs,
raftPath: cfg.RaftPath,
}
return
}
func (s *raftStore) RaftServer() *raft.RaftServer {
return s.raftServer
}
// CreatePartition creates a new partition in the raft store.
func (s *raftStore) CreatePartition(cfg *PartitionConfig) (p Partition, err error) {
// Init WaL Storage for this partition.
// Variables:
// wc: WaL Configuration.
// wp: WaL Path.
// ws: WaL Storage.
var walPath string
if cfg.WalPath == "" {
walPath = path.Join(s.raftPath, strconv.FormatUint(cfg.ID, 10))
} else {
walPath = path.Join(cfg.WalPath, "wal_"+strconv.FormatUint(cfg.ID, 10))
}
wc := &wal.Config{}
ws, err := wal.NewStorage(walPath, wc)
if err != nil {
return
}
peers := make([]proto.Peer, 0)
for _, peerAddress := range cfg.Peers {
peers = append(peers, peerAddress.Peer)
s.AddNodeWithPort(
peerAddress.ID,
peerAddress.Address,
peerAddress.HeartbeatPort,
peerAddress.ReplicaPort,
)
}
logger.Info("action[raftstore:CreatePartition] raft config applied [%v] id:%d", cfg.Applied, cfg.ID)
rc := &raft.RaftConfig{
ID: cfg.ID,
Peers: peers,
Leader: cfg.Leader,
Term: cfg.Term,
Storage: ws,
StateMachine: cfg.SM,
Applied: cfg.Applied,
Monitor: newMonitor(),
}
if err = s.raftServer.CreateRaft(rc); err != nil {
return
}
p = newPartition(cfg, s.raftServer, walPath)
return
}