-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.go
241 lines (190 loc) · 6.17 KB
/
api.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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
package dragonboatexample
import (
"context"
"sync/atomic"
"github.com/xkeyideal/dragonboat-example/v3/config"
"github.com/xkeyideal/dragonboat-example/v3/gossip"
"github.com/xkeyideal/dragonboat-example/v3/kv"
pb "github.com/xkeyideal/dragonboat-example/v3/pb/api"
"github.com/xkeyideal/dragonboat-example/v3/raft"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
type KVServer struct {
// pb.UnimplementedKVServer
serving int32
kv *kv.KVStorage
}
func NewKVServer(cf string, raftStorage *raft.Storage) *KVServer {
return &KVServer{
serving: config.Abort,
kv: kv.NewKVStorage(cf, raftStorage),
}
}
func (kv *KVServer) ChangeServing(serve int32) {
atomic.StoreInt32(&kv.serving, serve)
}
func (kv *KVServer) ready() bool {
return atomic.LoadInt32(&kv.serving) == config.Serving
}
func (kv *KVServer) Get(ctx context.Context, in *pb.GetRequest) (*pb.GetResponse, error) {
if !kv.ready() {
return nil, status.Error(codes.Aborted, config.ErrSystemAborted.Error())
}
revision, val, err := kv.kv.Get(in.Key, in.Linearizable)
if err != nil {
return nil, status.Error(codes.FailedPrecondition, err.Error())
}
return &pb.GetResponse{
Key: in.Key,
Val: val,
Revision: revision,
}, nil
}
func (kv *KVServer) Put(ctx context.Context, in *pb.PutRequest) (*pb.PutResponse, error) {
if !kv.ready() {
return nil, status.Error(codes.Aborted, config.ErrSystemAborted.Error())
}
revision, err := kv.kv.Put(in.Key, in.Val)
if err != nil {
return nil, status.Error(codes.FailedPrecondition, err.Error())
}
return &pb.PutResponse{
Revision: revision,
}, nil
}
func (kv *KVServer) Del(ctx context.Context, in *pb.DelRequest) (*pb.DelResponse, error) {
if !kv.ready() {
return nil, status.Error(codes.Aborted, config.ErrSystemAborted.Error())
}
revision, val, err := kv.kv.Get(in.Key, false)
if err != nil {
return nil, status.Error(codes.FailedPrecondition, err.Error())
}
err = kv.kv.Del(in.Key)
if err != nil {
return nil, status.Error(codes.FailedPrecondition, err.Error())
}
return &pb.DelResponse{
Key: in.Key,
Val: val,
Revision: revision,
}, nil
}
type RaftControlServer struct {
//pb.UnimplementedRaftControlServer
serving int32
raftStatus int32
raftConfCh chan *pb.RaftReleaseConfig
localIp string
raftDir string
grpcPort uint16
meta *kv.RaftMeta
}
func NewRaftControlServer(cf, localIp, raftDir string, grpcPort uint16,
raftStorage *raft.Storage, raftConfCh chan *pb.RaftReleaseConfig) *RaftControlServer {
return &RaftControlServer{
serving: config.Abort,
raftStatus: 1,
localIp: localIp,
raftConfCh: raftConfCh,
raftDir: raftDir,
grpcPort: grpcPort,
meta: kv.NewRaftMeta(cf, raftStorage),
}
}
func (rc *RaftControlServer) ChangeServing(serve int32) {
atomic.StoreInt32(&rc.serving, serve)
}
func (rc *RaftControlServer) ready() bool {
return atomic.LoadInt32(&rc.serving) == config.Serving
}
func (rc *RaftControlServer) StopRaftStatus() {
atomic.StoreInt32(&rc.raftStatus, 0)
}
// ReleaseRaftConf 前端推送启动配置接口,不需要判断ready
func (rc *RaftControlServer) ReleaseRaftConf(ctx context.Context, in *pb.RaftReleaseConfig) (*pb.WEmpty, error) {
if atomic.LoadInt32(&rc.raftStatus) == 1 {
rc.raftConfCh <- in
atomic.StoreInt32(&rc.raftStatus, 0)
} else {
// 启动后,前端接口再次推送,系统更新metadata
metadata, err := raft.ReadMetadataFromFile(rc.raftDir, rc.grpcPort)
if err != nil {
return &pb.WEmpty{}, nil
}
if metadata.Revision >= in.Revision {
return &pb.WEmpty{}, nil
}
// 如果本地没有metadata则存储入文件, 可以忽略错误
raft.WriteMetadataToFile(rc.raftDir, rc.grpcPort, MetadataByRelease(rc.localIp, in))
}
return &pb.WEmpty{}, nil
}
func (rc *RaftControlServer) ReleaseRaftShard(ctx context.Context, in *pb.RaftShard) (*pb.WEmpty, error) {
if !rc.ready() {
return nil, status.Error(codes.Aborted, config.ErrSystemAborted.Error())
}
shardMap := make(map[uint64][]string)
targetMap := make(map[string]gossip.TargetShardId)
for target, tc := range in.TargetMap {
targetMap[target] = gossip.TargetShardId{
GrpcAddr: tc.GrpcAddr,
ShardIds: tc.ShardIds,
}
for _, shardId := range tc.ShardIds {
shardMap[shardId] = append(shardMap[shardId], target)
}
}
initialMembers := make(map[uint64]map[uint64]string)
for shardId, im := range in.InitialMembers {
initialMembers[shardId] = im.InitialMembers
}
join := make(map[uint64]map[uint64]bool)
for shardId, jn := range in.Join {
join[shardId] = jn.Join
}
clusterMessage := &gossip.RaftShardMessage{
Revision: in.Revision,
Targets: targetMap,
Shards: shardMap,
InitialMembers: initialMembers,
Join: join,
}
rc.meta.UpdateShardMessage(clusterMessage)
return &pb.WEmpty{}, nil
}
func (rc *RaftControlServer) UpdateRaftShardIds(ctx context.Context, in *pb.RaftShardIds) (*pb.WEmpty, error) {
if !rc.ready() {
return nil, status.Error(codes.Aborted, config.ErrSystemAborted.Error())
}
err := rc.meta.UpdateRaftShardIds(in.Add, in.ShardIds)
return &pb.WEmpty{}, err
}
func (rc *RaftControlServer) RaftShardInfo(ctx context.Context, in *pb.WEmpty) (*pb.RaftInfoResponse, error) {
if !rc.ready() {
return nil, status.Error(codes.Aborted, config.ErrSystemAborted.Error())
}
return rc.meta.RaftShardInfo()
}
func (rc *RaftControlServer) AddRaftObserverReplica(ctx context.Context, in *pb.RaftOp) (*pb.WEmpty, error) {
if !rc.ready() {
return nil, status.Error(codes.Aborted, config.ErrSystemAborted.Error())
}
err := rc.meta.AddRaftObserverNode(in.ReplicaId, in.Target, in.ShardIds)
return &pb.WEmpty{}, err
}
func (rc *RaftControlServer) AddRaftReplica(ctx context.Context, in *pb.RaftOp) (*pb.WEmpty, error) {
if !rc.ready() {
return nil, status.Error(codes.Aborted, config.ErrSystemAborted.Error())
}
err := rc.meta.AddRaftNode(in.ReplicaId, in.Target, in.ShardIds)
return &pb.WEmpty{}, err
}
func (rc *RaftControlServer) RemoveRaftReplica(ctx context.Context, in *pb.RaftOp) (*pb.WEmpty, error) {
if !rc.ready() {
return nil, status.Error(codes.Aborted, config.ErrSystemAborted.Error())
}
err := rc.meta.RemoveRaftNode(in.ReplicaId, in.ShardIds)
return &pb.WEmpty{}, err
}