-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgroup_manager.go
222 lines (194 loc) · 5.99 KB
/
group_manager.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
package raftgrp
import (
"context"
"fmt"
"log"
"path/filepath"
"sync"
etransport "github.com/EricYT/raftgrp/transport"
"github.com/pkg/errors"
"go.etcd.io/etcd/pkg/fileutil"
"go.etcd.io/etcd/raft/raftpb"
"go.uber.org/zap"
)
var (
ErrRaftGroupManagerNotFound error = errors.New("[RaftGroupManager] group not found")
ErrRaftGroupManagerExist error = errors.New("[RaftGroupManager] group already exist")
ErrRaftGroupManagerLaunched error = errors.New("[RaftGroupManager] group already launched")
)
const (
defaultTickMs int = 1000
defaultElectionTicks int = 5
defaultSnapshotCount uint64 = 10
defaultSnapshotCatchUpEntries uint64 = 5
)
type RaftGroupManager struct {
Logger *zap.Logger
mu sync.RWMutex
logdir string
// gRPC address for all raft groups
addr string
groups map[uint64]*RaftGroup
tm *etransport.TransportManager
}
func NewRaftGroupManager(lg *zap.Logger, logdir, addr string) *RaftGroupManager {
if lg == nil {
lg = zap.NewExample()
}
// initialize raft group log directory
if terr := fileutil.TouchDirAll(logdir); terr != nil {
lg.Fatal("[RaftGroupManager] create raft group log directory error",
zap.String("directory", logdir),
zap.Error(terr),
)
}
rm := &RaftGroupManager{
Logger: lg,
logdir: logdir,
addr: addr,
groups: make(map[uint64]*RaftGroup),
}
rm.tm = etransport.NewTransportManager(lg, addr, rm)
return rm
}
func (rm *RaftGroupManager) Start() error {
// FIXME: If we try to reload all raft groups from here.
// It's a heavy operation maybe.
rm.tm.Start()
log.Println("[RaftGroupManager] start 4")
return nil
}
func (rm *RaftGroupManager) Stop() {
rm.tm.Stop()
}
// raft process dispatcher
func (rm *RaftGroupManager) Process(ctx context.Context, gid uint64, m *raftpb.Message) error {
// FIXME: this operation will be bothered by raft group create and restart.
rm.mu.RLock()
g, ok := rm.groups[gid]
rm.mu.RUnlock()
if ok {
return g.Process(ctx, m)
}
return ErrRaftGroupManagerNotFound
}
// snapshot methods
func (rm *RaftGroupManager) UnmarshalSnapshotWriter(ctx context.Context, gid uint64, p []byte) (etransport.SnapshotWriter, error) {
rm.mu.RLock()
g, ok := rm.groups[gid]
rm.mu.RUnlock()
if !ok {
return nil, ErrRaftGroupManagerNotFound
}
return g.usm.UnmarshalSnapshotWriter(p)
}
func (rm *RaftGroupManager) UnmarshalSnapshotParter(ctx context.Context, gid uint64, p []byte) (etransport.SnapshotParter, error) {
rm.mu.RLock()
g, ok := rm.groups[gid]
rm.mu.RUnlock()
if !ok {
return nil, ErrRaftGroupManagerNotFound
}
return g.usm.UnmarshalSnapshotParter(p)
}
func (rm *RaftGroupManager) NewRaftGroup(lg *zap.Logger, gid, id uint64, peers []string, newCluster bool) (*RaftGroup, error) {
rm.mu.Lock()
defer rm.mu.Unlock()
if g, ok := rm.groups[gid]; ok {
return g, ErrRaftGroupManagerExist
}
gdir := logDirByGId(rm.logdir, gid)
if fileutil.Exist(gdir) {
return rm.restartRaftGroup(lg, gid, id)
}
return rm.newRaftGroup(lg, gid, id, peers, newCluster)
}
func (rm *RaftGroupManager) newRaftGroup(lg *zap.Logger, gid, id uint64, peers []string, newCluster bool) (*RaftGroup, error) {
if lg == nil {
lg = rm.Logger
}
// initialize raft group log parent directory
gdir := logDirByGId(rm.logdir, gid)
if terr := fileutil.CreateDirAll(gdir); terr != nil {
lg.Error("[RaftGroupManager] create raft group parent directory failed",
zap.Uint64("gid", gid),
zap.Uint64("id", id),
zap.String("raft-group-parent-directory", gdir),
zap.Error(terr),
)
return nil, errors.Wrap(terr, "[RaftGroupManager] create raft group parent directory error")
}
trans := rm.tm.CreateTransport(lg, gid)
grp, err := NewRaftGroup(GroupConfig{
Logger: lg,
ID: id,
GID: gid,
Peers: ParsePeers(peers),
NewCluster: newCluster,
DataDir: gdir,
TickMs: defaultTickMs,
ElectionTicks: defaultElectionTicks,
PreVote: false,
SnapshotCount: defaultSnapshotCount,
SnapshotCatchUpEntries: defaultSnapshotCatchUpEntries,
}, trans)
if err != nil {
lg.Error("[RaftGroupManager] create raft group failed",
zap.Uint64("gid", gid),
zap.Uint64("id", id),
zap.Error(err),
)
return nil, errors.Wrap(err, "[RaftGroupManager] new raft group error")
}
lg.Info("[RaftGroupManager] create raft group success",
zap.Uint64("group-id", gid),
zap.Uint64("id", id),
)
rm.groups[gid] = grp
return grp, nil
}
func (rm *RaftGroupManager) restartRaftGroup(lg *zap.Logger, gid, id uint64) (*RaftGroup, error) {
if g, ok := rm.groups[gid]; ok {
lg.Error("[RaftGroupManager] restart raft group already launched",
zap.Uint64("raft-group-id", gid),
zap.Uint64("id", id),
zap.Error(ErrRaftGroupManagerLaunched),
)
return g, ErrRaftGroupManagerLaunched
}
trans := rm.tm.CreateTransport(lg, gid)
grp, err := NewRaftGroup(GroupConfig{
Logger: lg,
ID: id,
GID: gid,
DataDir: logDirByGId(rm.logdir, gid),
TickMs: defaultTickMs,
ElectionTicks: defaultElectionTicks,
PreVote: false,
SnapshotCount: defaultSnapshotCount,
SnapshotCatchUpEntries: defaultSnapshotCatchUpEntries,
}, trans)
if err != nil {
lg.Error("[RaftGroupManager] restart raft group failed",
zap.Uint64("gid", gid),
zap.Uint64("id", id),
zap.Error(err),
)
return nil, errors.Wrap(err, "[RaftGroupManager] restart raft group error")
}
lg.Info("[RaftGroupManager] restart raft group success",
zap.Uint64("group-id", gid),
zap.Uint64("id", id),
)
rm.groups[gid] = grp
return grp, nil
}
func (rm *RaftGroupManager) AddPeer(ctx context.Context, gid, peerid uint64, addr string) error {
panic("not implement")
}
func (rm *RaftGroupManager) RemovePeer(ctx context.Context, gid, peerid uint64) error {
panic("not implement")
}
func logDirByGId(parent string, gid uint64) string {
return filepath.Join(parent, fmt.Sprintf("raft-group-%d", gid))
}