-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprophet_transport.go
407 lines (349 loc) · 8.77 KB
/
prophet_transport.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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
package prophet
import (
"encoding/json"
"fmt"
"time"
"github.com/fagongzi/goetty"
)
const (
typeResourceHeartbeatReq byte = iota
typeResourceHeartbeatRsp
typeContainerHeartbeatReq
typeContainerHeartbeatRsp
typeGetContainerReq
typeGetContainerRsp
typeAllocIDReq
typeAllocIDRsp
typeAskSplitReq
typeAskSplitRsp
typeErrorRsp
typeInitWatcher
typeEventNotify
)
// ResourceHeartbeatReq resource hb msg
type ResourceHeartbeatReq struct {
Resource Resource `json:"-"`
Data []byte `json:"data"`
LeaderPeer *Peer `json:"leaderPeer"`
DownPeers []*PeerStats `json:"downPeers"`
PendingPeers []*Peer `json:"pendingPeers"`
ContainerID uint64 `json:"cid"`
}
// Init init
func (req *ResourceHeartbeatReq) Init(adapter Adapter) error {
req.Resource = adapter.NewResource()
err := req.Resource.Unmarshal(req.Data)
if err != nil {
return err
}
return nil
}
// Prepare prepare
func (req *ResourceHeartbeatReq) Prepare() error {
data, err := req.Resource.Marshal()
if err != nil {
return err
}
req.Data = data
return nil
}
type resourceHeartbeatRsp struct {
ResourceID uint64 `json:"resourceID"`
NewLeader *Peer `json:"newLeader"`
Peer *Peer `json:"peer"`
ChangeType ChangePeerType `json:"changeType"`
ContainerID uint64 `json:"containerID"`
}
type getContainerReq struct {
ID uint64 `json:"id"`
}
type getContainerRsp struct {
Container Container `json:"container"`
}
func newChangeLeaderRsp(resourceID uint64, newLeader *Peer) *resourceHeartbeatRsp {
return &resourceHeartbeatRsp{
ResourceID: resourceID,
NewLeader: newLeader,
}
}
func newChangePeerRsp(resourceID uint64, peer *Peer, changeType ChangePeerType) *resourceHeartbeatRsp {
return &resourceHeartbeatRsp{
ResourceID: resourceID,
Peer: peer,
ChangeType: changeType,
}
}
func newScalePeerRsp(resourceID uint64, containerID uint64) *resourceHeartbeatRsp {
return &resourceHeartbeatRsp{
ResourceID: resourceID,
ContainerID: containerID,
ChangeType: ScalePeer,
}
}
// ContainerHeartbeatReq container hb msg
type ContainerHeartbeatReq struct {
Data []byte `json:"data"`
StorageCapacity uint64 `json:"storageCapacity"`
StorageAvailable uint64 `json:"storageAvailable"`
LeaderCount uint64 `json:"leaderCount"`
ReplicaCount uint64 `json:"replicaCount"`
SendingSnapCount uint64 `json:"sendingSnapCount"`
ReceivingSnapCount uint64 `json:"receivingSnapCount"`
ApplyingSnapCount uint64 `json:"applyingSnapCount"`
Busy bool `json:"busy"` // stop replica balance
Block bool `json:"block"` // stop leader balance
Container Container `json:"-"`
}
// Init init
func (req *ContainerHeartbeatReq) Init(adapter Adapter) error {
req.Container = adapter.NewContainer()
err := req.Container.Unmarshal(req.Data)
if err != nil {
return err
}
return nil
}
// Prepare prepare
func (req *ContainerHeartbeatReq) Prepare() error {
data, err := req.Container.Marshal()
if err != nil {
return err
}
req.Data = data
return nil
}
type containerHeartbeatRsp struct {
Status string `json:"status"`
}
func newContainerHeartbeatRsp() Serializable {
rsp := &containerHeartbeatRsp{}
rsp.Status = "OK"
return rsp
}
type errorRsp struct {
Err string `json:"err"`
}
func newErrorRsp(err error) Serializable {
rsp := &errorRsp{}
rsp.Err = err.Error()
return rsp
}
type allocIDReq struct {
}
type allocIDRsp struct {
ID uint64 `json:"id,omitempty"`
Err error `json:"err,omitempty"`
}
type askSplitReq struct {
Resource Resource `json:"-"`
Data []byte `json:"data,omitempty"`
}
// Init init
func (req *askSplitReq) Init(adapter Adapter) error {
if len(req.Data) > 0 {
req.Resource = adapter.NewResource()
err := req.Resource.Unmarshal(req.Data)
if err != nil {
return err
}
}
return nil
}
// Prepare prepare
func (req *askSplitReq) Prepare() error {
if req.Resource != nil {
data, err := req.Resource.Marshal()
if err != nil {
return err
}
req.Data = data
}
return nil
}
type askSplitRsp struct {
Err error `json:"err,omitempty"`
NewID uint64 `json:"newID,omitempty"`
NewPeerIDs []uint64 `json:"peerIDs,omitempty"`
}
// codec format: length(4bytes) + msgType(1bytes) + msg(length bytes)
type codec struct {
adapter Adapter
}
func (c *codec) Encode(data interface{}, out *goetty.ByteBuf) error {
var target Serializable
var t byte
if msg, ok := data.(*ResourceHeartbeatReq); ok {
target = msg
t = typeResourceHeartbeatReq
} else if msg, ok := data.(*resourceHeartbeatRsp); ok {
target = msg
t = typeResourceHeartbeatRsp
} else if msg, ok := data.(*ContainerHeartbeatReq); ok {
target = msg
t = typeContainerHeartbeatReq
} else if msg, ok := data.(*containerHeartbeatRsp); ok {
target = msg
t = typeContainerHeartbeatRsp
} else if msg, ok := data.(*allocIDReq); ok {
target = msg
t = typeAllocIDReq
} else if msg, ok := data.(*allocIDRsp); ok {
target = msg
t = typeAllocIDRsp
} else if msg, ok := data.(*askSplitReq); ok {
target = msg
t = typeAskSplitReq
} else if msg, ok := data.(*askSplitRsp); ok {
target = msg
t = typeAskSplitRsp
} else if msg, ok := data.(*errorRsp); ok {
target = msg
t = typeErrorRsp
} else if msg, ok := data.(*InitWatcher); ok {
target = msg
t = typeInitWatcher
} else if msg, ok := data.(*EventNotify); ok {
target = msg
t = typeEventNotify
} else {
return fmt.Errorf("not support msg: %+v", data)
}
if codecS, ok := target.(codecSerializable); ok {
err := codecS.Prepare()
if err != nil {
return err
}
}
value, err := json.Marshal(target)
if err != nil {
return err
}
out.WriteByte(t)
out.Write(value)
return nil
}
func (c *codec) Decode(in *goetty.ByteBuf) (bool, interface{}, error) {
value := in.GetMarkedRemindData()
in.MarkedBytesReaded()
var msg Serializable
t := value[0]
switch t {
case typeResourceHeartbeatReq:
msg = &ResourceHeartbeatReq{}
break
case typeResourceHeartbeatRsp:
msg = &resourceHeartbeatRsp{}
break
case typeContainerHeartbeatReq:
msg = &ContainerHeartbeatReq{}
break
case typeContainerHeartbeatRsp:
msg = &containerHeartbeatRsp{}
break
case typeAllocIDReq:
msg = &allocIDReq{}
break
case typeAllocIDRsp:
msg = &allocIDRsp{}
break
case typeAskSplitReq:
msg = &askSplitReq{}
break
case typeAskSplitRsp:
msg = &askSplitRsp{}
break
case typeErrorRsp:
msg = &errorRsp{}
break
case typeInitWatcher:
msg = &InitWatcher{}
break
case typeEventNotify:
msg = &EventNotify{}
break
default:
return false, nil, fmt.Errorf("unknown msg type")
}
err := json.Unmarshal(value[1:], msg)
if err != nil {
return false, nil, err
}
if codecS, ok := msg.(codecSerializable); ok {
err = codecS.Init(c.adapter)
if err != nil {
return false, nil, err
}
}
return true, msg, nil
}
func (p *defaultProphet) startListen() {
go func() {
err := p.tcpL.Start(p.doConnection)
if err != nil {
log.Fatalf("rpc listen at %s failed, errors:\n%+v",
p.node.Addr,
err)
}
}()
<-p.tcpL.Started()
log.Infof("start rpc listen at %s", p.node.Addr)
}
func (p *defaultProphet) doConnection(conn goetty.IOSession) error {
if p.wn != nil {
defer p.wn.clearWatcher(conn)
}
for {
value, err := conn.Read()
if err != nil {
return err
}
if !p.isLeader() {
conn.WriteAndFlush(newErrorRsp(errMaybeNotLeader))
continue
}
if msg, ok := value.(*ResourceHeartbeatReq); ok {
rsp, err := p.handleResourceHeartbeat(msg)
if err != nil {
conn.WriteAndFlush(newErrorRsp(err))
continue
}
conn.WriteAndFlush(rsp)
} else if msg, ok := value.(*ContainerHeartbeatReq); ok {
p.handleContainerHeartbeat(msg)
conn.WriteAndFlush(newContainerHeartbeatRsp())
} else if msg, ok := value.(*allocIDReq); ok {
conn.WriteAndFlush(p.handleAllocID(msg))
} else if msg, ok := value.(*askSplitReq); ok {
conn.WriteAndFlush(p.handleAskSplit(msg))
} else if msg, ok := value.(*InitWatcher); ok {
p.wn.onInitWatcher(msg, conn)
}
}
}
func (p *defaultProphet) getLeaderClient() goetty.IOSession {
for {
if p.leader == nil {
p.maybeLoadLeader()
}
if p.leader != nil {
conn, err := p.createLeaderClient(p.leader.Addr)
if err == nil {
log.Infof("create leader connection to %+v", p.leader)
return conn
}
log.Errorf("create leader connection failed, errors: %+v", err)
}
time.Sleep(time.Second)
}
}
func (p *defaultProphet) createLeaderClient(leader string) (goetty.IOSession, error) {
conn := goetty.NewConnector(leader,
goetty.WithClientDecoder(goetty.NewIntLengthFieldBasedDecoder(p.bizCodec)),
goetty.WithClientEncoder(goetty.NewIntLengthFieldBasedEncoder(p.bizCodec)),
goetty.WithClientConnectTimeout(time.Second*10))
_, err := conn.Connect()
if err != nil {
return nil, err
}
return conn, nil
}