-
Notifications
You must be signed in to change notification settings - Fork 1
/
client.go
538 lines (450 loc) · 11.2 KB
/
client.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
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
package prophet
import (
"context"
"errors"
"sync"
"sync/atomic"
"time"
"github.com/deepfabric/prophet/metadata"
"github.com/deepfabric/prophet/pb/rpcpb"
"github.com/deepfabric/prophet/util"
"github.com/fagongzi/goetty"
)
var (
// ErrClosed error of client closed
ErrClosed = errors.New("client is closed")
// ErrTimeout timeout
ErrTimeout = errors.New("rpc timeout")
)
var (
stateRunning = int32(0)
stateStopped = int32(1)
)
// Client prophet client
type Client interface {
Close() error
AllocID() (uint64, error)
GetContainer(containerID uint64) (metadata.Container, error)
ResourceHeartbeat(meta metadata.Resource, hb rpcpb.ResourceHeartbeatReq) error
ContainerHeartbeat(hb rpcpb.ContainerHeartbeatReq) (rpcpb.ContainerHeartbeatRsp, error)
AskSplit(res metadata.Resource) (rpcpb.SplitID, error)
ReportSplit(left, right metadata.Resource) error
AskBatchSplit(res metadata.Resource, count uint32) ([]rpcpb.SplitID, error)
ReportBatchSplit(results ...metadata.Resource) error
NewWatcher(flag uint32) (Watcher, error)
GetResourceHeartbeatRspNotifier() (chan rpcpb.ResourceHeartbeatRsp, error)
}
type asyncClient struct {
opts *options
adapter metadata.Adapter
state int32
id uint64
contexts sync.Map // id -> request
leaderConn goetty.IOSession
currentLeader string
ctx context.Context
cancel context.CancelFunc
resetReadC chan struct{}
resetLeaderConnC chan string
writeC chan *ctx
resourceHeartbeatRspC chan rpcpb.ResourceHeartbeatRsp
allocIDC chan uint64
}
// NewClient create a prophet client
func NewClient(adapter metadata.Adapter, opts ...Option) Client {
c := &asyncClient{
opts: &options{},
adapter: adapter,
leaderConn: createConn(),
resetReadC: make(chan struct{}, 1),
resetLeaderConnC: make(chan string, 1),
writeC: make(chan *ctx, 128),
resourceHeartbeatRspC: make(chan rpcpb.ResourceHeartbeatRsp, 128),
allocIDC: make(chan uint64, 128),
}
for _, opt := range opts {
opt(c.opts)
}
c.opts.adjust()
c.ctx, c.cancel = context.WithCancel(context.Background())
c.start()
return c
}
func (c *asyncClient) Close() error {
if atomic.CompareAndSwapInt32(&c.state, stateRunning, stateStopped) {
c.doClose()
}
return nil
}
func (c *asyncClient) AllocID() (uint64, error) {
if !c.running() {
return 0, ErrClosed
}
req := &rpcpb.Request{}
req.Type = rpcpb.TypeAllocIDReq
c.asyncDo(req, func(resp *rpcpb.Response, err error) {
if err != nil {
util.GetLogger().Errorf("alloc id failed with %+v", err)
} else {
c.allocIDC <- resp.AllocID.ID
}
})
select {
case id, ok := <-c.allocIDC:
if !ok {
return 0, ErrClosed
}
return id, nil
case <-time.After(c.opts.rpcTimeout):
return 0, ErrTimeout
}
}
func (c *asyncClient) ResourceHeartbeat(meta metadata.Resource, hb rpcpb.ResourceHeartbeatReq) error {
if !c.running() {
return ErrClosed
}
data, err := meta.Marshal()
if err != nil {
return err
}
hb.Resource = data
req := &rpcpb.Request{}
req.Type = rpcpb.TypeResourceHeartbeatReq
req.ResourceHeartbeat = hb
c.asyncDo(req, func(resp *rpcpb.Response, err error) {
if err != nil {
util.GetLogger().Errorf("resource %+v heartbeat failed with %+v",
meta,
err)
} else {
c.resourceHeartbeatRspC <- resp.ResourceHeartbeat
}
})
return nil
}
func (c *asyncClient) ContainerHeartbeat(hb rpcpb.ContainerHeartbeatReq) (rpcpb.ContainerHeartbeatRsp, error) {
if !c.running() {
return rpcpb.ContainerHeartbeatRsp{}, ErrClosed
}
req := &rpcpb.Request{}
req.Type = rpcpb.TypeContainerHeartbeatReq
req.ContainerHeartbeat = hb
resp, err := c.syncDo(req)
if err != nil {
util.GetLogger().Errorf("container %d heartbeat failed with %+v",
hb.Stats.ContainerID,
err)
return rpcpb.ContainerHeartbeatRsp{}, err
}
return resp.ContainerHeartbeat, nil
}
func (c *asyncClient) GetContainer(containerID uint64) (metadata.Container, error) {
if !c.running() {
return nil, ErrClosed
}
req := &rpcpb.Request{}
req.Type = rpcpb.TypeGetContainerReq
req.GetContainer.ID = containerID
resp, err := c.syncDo(req)
if err != nil {
return nil, err
}
meta := c.adapter.NewContainer()
err = meta.Unmarshal(resp.GetContainer.Data)
if err != nil {
return nil, err
}
return meta, nil
}
func (c *asyncClient) AskSplit(res metadata.Resource) (rpcpb.SplitID, error) {
if !c.running() {
return rpcpb.SplitID{}, ErrClosed
}
data, err := res.Marshal()
if err != nil {
return rpcpb.SplitID{}, err
}
req := &rpcpb.Request{}
req.Type = rpcpb.TypeAskSplitReq
req.AskSplit.Data = data
resp, err := c.syncDo(req)
if err != nil {
return rpcpb.SplitID{}, err
}
return resp.AskSplit.SplitID, nil
}
func (c *asyncClient) ReportSplit(left, right metadata.Resource) error {
if !c.running() {
return ErrClosed
}
leftData, err := left.Marshal()
if err != nil {
return err
}
rightData, err := right.Marshal()
if err != nil {
return err
}
req := &rpcpb.Request{}
req.Type = rpcpb.TypeReportSplitReq
req.ReportSplit.Left = leftData
req.ReportSplit.Right = rightData
_, err = c.syncDo(req)
if err != nil {
return err
}
return nil
}
func (c *asyncClient) AskBatchSplit(res metadata.Resource, count uint32) ([]rpcpb.SplitID, error) {
if !c.running() {
return nil, ErrClosed
}
data, err := res.Marshal()
if err != nil {
return nil, err
}
req := &rpcpb.Request{}
req.Type = rpcpb.TypeAskBatchSplitReq
req.AskBatchSplit.Data = data
req.AskBatchSplit.Count = count
resp, err := c.syncDo(req)
if err != nil {
return nil, err
}
return resp.AskBatchSplit.SplitIDs, nil
}
func (c *asyncClient) ReportBatchSplit(results ...metadata.Resource) error {
if !c.running() {
return ErrClosed
}
var resources [][]byte
for _, res := range results {
v, err := res.Marshal()
if err != nil {
return err
}
resources = append(resources, v)
}
req := &rpcpb.Request{}
req.Type = rpcpb.TypeBatchReportSplitReq
req.BatchReportSplit.Resources = resources
_, err := c.syncDo(req)
if err != nil {
return err
}
return nil
}
func (c *asyncClient) NewWatcher(flag uint32) (Watcher, error) {
if !c.running() {
return nil, ErrClosed
}
return newWatcher(flag, c), nil
}
func (c *asyncClient) GetResourceHeartbeatRspNotifier() (chan rpcpb.ResourceHeartbeatRsp, error) {
if !c.running() {
return nil, ErrClosed
}
return c.resourceHeartbeatRspC, nil
}
func (c *asyncClient) doClose() {
c.cancel()
close(c.resourceHeartbeatRspC)
close(c.resetLeaderConnC)
close(c.writeC)
close(c.allocIDC)
c.leaderConn.Close()
}
func (c *asyncClient) start() {
err := c.initLeaderConn(c.leaderConn, "", time.Minute)
if err != nil {
util.GetLogger().Fatalf("connect to leader failed after 1 minute, error %+v", err)
}
go c.writeLoop()
go c.readLoop()
util.GetLogger().Infof("client started")
}
func (c *asyncClient) running() bool {
return atomic.LoadInt32(&c.state) == stateRunning
}
func (c *asyncClient) syncDo(req *rpcpb.Request) (*rpcpb.Response, error) {
ctx := newSyncCtx(req)
c.do(ctx)
ctx.wait()
if ctx.err != nil {
return nil, ctx.err
}
if ctx.resp.Error != "" {
return nil, errors.New(ctx.resp.Error)
}
return ctx.resp, nil
}
func (c *asyncClient) asyncDo(req *rpcpb.Request, cb func(*rpcpb.Response, error)) {
c.do(newAsyncCtx(req, cb))
}
func (c *asyncClient) do(ctx *ctx) {
ctx.req.ID = c.nextID()
c.contexts.Store(ctx.req.ID, ctx)
util.GetLogger().Debugf("before write %+v", ctx.req)
c.writeC <- ctx
util.GetLogger().Debugf("after write %+v", ctx.req)
util.DefaultTimeoutWheel().Schedule(c.opts.rpcTimeout, c.timeout, ctx.req.ID)
}
func (c *asyncClient) timeout(arg interface{}) {
if v, ok := c.contexts.Load(arg); ok {
c.contexts.Delete(arg)
v.(*ctx).done(nil, ErrTimeout)
c.scheduleResetLeaderConn("")
}
}
func (c *asyncClient) scheduleResetLeaderConn(newLeader string) {
if atomic.LoadInt32(&c.state) != stateRunning {
return
}
select {
case c.resetLeaderConnC <- newLeader:
default:
}
}
func (c *asyncClient) nextID() uint64 {
return atomic.AddUint64(&c.id, 1)
}
func (c *asyncClient) writeLoop() {
for {
select {
case <-c.ctx.Done():
return
case ctx, ok := <-c.writeC:
if ok {
c.doWrite(ctx)
}
case newLeader, ok := <-c.resetLeaderConnC:
if ok {
if err := c.resetLeaderConn(newLeader); err != nil {
util.GetLogger().Errorf("reset leader connection failed with %+v",
newLeader,
err)
}
}
}
}
}
func (c *asyncClient) readLoop() {
for {
select {
case <-c.ctx.Done():
return
case _, ok := <-c.resetReadC:
if ok {
util.GetLogger().Infof("client read loop started")
for {
msg, err := c.leaderConn.Read()
if err != nil {
util.GetLogger().Errorf("read resp from leader %s failed with %+v",
c.currentLeader,
err)
c.scheduleResetLeaderConn("")
break
}
util.GetLogger().Debugf("read msg %+v",
msg)
resp := msg.(*rpcpb.Response)
if v, ok := c.contexts.Load(resp.ID); ok {
if resp.Error != "" && util.IsNotLeaderError(resp.Error) {
c.scheduleResetLeaderConn(resp.Leader)
c.writeC <- v.(*ctx)
break
}
c.contexts.Delete(resp.ID)
v.(*ctx).done(resp, nil)
}
}
}
}
}
}
func (c *asyncClient) doWrite(ctx *ctx) {
err := c.leaderConn.Write(ctx.req)
if err != nil {
util.GetLogger().Errorf("write %+v failed with %+v",
ctx.req, err)
}
}
func (c *asyncClient) resetLeaderConn(newLeader string) error {
if c.currentLeader == newLeader {
util.GetLogger().Infof("skip reset leader, current leader is %s", newLeader)
return nil
}
c.leaderConn.Close()
return c.initLeaderConn(c.leaderConn, newLeader, c.opts.rpcTimeout)
}
func (c *asyncClient) initLeaderConn(conn goetty.IOSession, newLeader string, timeout time.Duration) error {
addr := newLeader
for {
select {
case <-time.After(timeout):
return ErrTimeout
default:
if addr == "" {
leader := c.opts.leaderGetter()
if leader != nil {
addr = leader.Addr
}
}
if addr != "" {
util.GetLogger().Infof("start init connection to leader %+v", addr)
conn.Close()
ok, err := conn.Connect(addr, c.opts.rpcTimeout)
if err == nil && ok {
c.currentLeader = addr
c.resetReadC <- struct{}{}
util.GetLogger().Infof("connected to leader %s", addr)
return nil
}
util.GetLogger().Errorf("init leader conn failed with %+v, retry later", err)
}
}
time.Sleep(time.Millisecond * 200)
// to prevent the given leader from being unreachable,
// use LeaderGetter for the next retry
addr = ""
}
}
type ctx struct {
state uint64
req *rpcpb.Request
resp *rpcpb.Response
err error
c chan struct{}
cb func(resp *rpcpb.Response, err error)
sync bool
}
func newSyncCtx(req *rpcpb.Request) *ctx {
return &ctx{
req: req,
c: make(chan struct{}),
sync: true,
}
}
func newAsyncCtx(req *rpcpb.Request, cb func(resp *rpcpb.Response, err error)) *ctx {
return &ctx{
req: req,
cb: cb,
sync: false,
}
}
func (c *ctx) done(resp *rpcpb.Response, err error) {
if atomic.CompareAndSwapUint64(&c.state, 0, 1) {
if c.sync {
c.resp = resp
c.err = err
close(c.c)
} else {
c.cb(resp, err)
}
}
}
func (c *ctx) wait() {
if c.sync {
<-c.c
}
}