/
service.go
660 lines (609 loc) · 19.1 KB
/
service.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
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
package service
import (
"context"
"encoding/json"
"errors"
"fmt"
"math/rand"
"net"
"net/rpc/jsonrpc"
"strings"
"sync"
"time"
"sorakaq/center/entity"
dao "sorakaq/dao"
"sorakaq/discover"
"sorakaq/message"
recovery "sorakaq/recovery/service"
"sorakaq/util"
"net/rpc"
"github.com/Shopify/sarama"
"github.com/samuel/go-zookeeper/zk"
log "github.com/sirupsen/logrus"
)
type centerService struct {
kafkaConfig *entity.KafkaConfig
kafkaTopics []string
rpcConnPool sync.Map
rpcPort int64
centerIp string
topicWorkerAddressCache sync.Map //topic对应的worker地址缓存
workerAddressCache sync.Map //所有worker地址缓存
retryMsgChan chan message.Request //消息重试通道
}
const (
WORKER_ADDRESS_EXPIRE = 60 //60秒重新取worker address
PULL_OFFLINE_WORKER_MSG_NUM_EVERY_TIME = 500 //每次拉取下线worker消息个数
MAX_REQ_CNT = 3 //每个消息请求worker最大次数
)
type Consumer struct {
ready chan bool
}
type CenterConsumer struct {
}
func (consumer *Consumer) Setup(sarama.ConsumerGroupSession) error {
close(consumer.ready)
return nil
}
func (consumer *Consumer) Cleanup(sarama.ConsumerGroupSession) error {
return nil
}
func (consumer *Consumer) ConsumeClaim(session sarama.ConsumerGroupSession, claim sarama.ConsumerGroupClaim) error {
for message := range claim.Messages() {
log.Infof("Message claimed: key = %s, value = %v, topic = %s, partition = %v, offset = %v", string(message.Key), string(message.Value), message.Topic, message.Partition, message.Offset)
go func(message *sarama.ConsumerMessage) {
err := CenterService.msgCallback(message.Value, false)
if err != nil {
log.Errorf("msg call back err: %v", err)
return
}
session.MarkMessage(message, "")
}(message)
}
return nil
}
func Init(brokerList string, topicPrefix string, centerIp string, rpcPort int64) {
log.Infof("CenterIp : %s, rpcPort: %d", centerIp, rpcPort)
CenterService = NewCenterService(brokerList, topicPrefix, rpcPort, centerIp)
_, err := CenterService.setWorkerAddressCache()
if err != nil {
panic("Set worker address cache err")
}
go CenterService.initRpcServer()
CenterService.watchTopicChange()
CenterService.watchWorkerChange()
rand.Seed(time.Now().UnixNano())
go CenterService.msgRetry()
}
func (this *centerService) initRpcServer() {
rpc.Register(new(CenterConsumer)) // 注册rpc服务
listen, err := net.Listen("tcp", util.IPToWorkerFormat(this.centerIp, this.rpcPort))
if err != nil {
panic(fmt.Sprintf("Init rpc server err: %v, ip: %s, port: %d", err, this.centerIp, this.rpcPort))
}
for {
conn, err := listen.Accept() // 接收客户端连接请求
if err != nil {
continue
}
go func(conn net.Conn) { // 并发处理客户端请求
jsonrpc.ServeConn(conn)
}(conn)
}
}
func NewCenterService(brokerList string, topicPrefix string, rpcPort int64, centerIp string) ICenterService {
config := InitKafkaConf(brokerList, topicPrefix)
topics := make([]string, 0)
return ¢erService{
kafkaConfig: config,
kafkaTopics: topics,
rpcPort: rpcPort,
centerIp: centerIp,
retryMsgChan: make(chan message.Request, 1000),
}
}
func InitKafkaConf(brokerList string, topicPrefix string) *entity.KafkaConfig {
_, err := sarama.ParseKafkaVersion("2.3.0")
if err != nil {
log.Errorf("Error parsing Kafka version: %v", err)
}
kafkaConfig := &entity.KafkaConfig{}
kafkaConfig.Config = sarama.NewConfig()
kafkaConfig.Config.Metadata.RefreshFrequency = 10 * time.Second
kafkaConfig.Config.Consumer.Offsets.Initial = sarama.OffsetOldest
kafkaConfig.Config.Version = sarama.V0_11_0_2
kafkaConfig.Config.Consumer.Return.Errors = true
kafkaConfig.BrokerServers = brokerList
kafkaConfig.TopicPrefix = topicPrefix
return kafkaConfig
}
//启动center
func (this *centerService) Start() {
kafkaTopics, err := this.getAllDelayTopics()
if err != nil {
log.Errorf("Start service err, err: %v", err)
return
}
this.kafkaTopics = kafkaTopics
log.Debugf("Kafka topics: %v", kafkaTopics)
this.createConsumer(kafkaTopics)
}
//创建消费者
func (this *centerService) createConsumer(topics []string) {
for k, _ := range topics {
go this.createConsumerGroup(topics[k : k+1])
}
}
//消费kafka
func (this *centerService) createConsumerGroup(topics []string) {
consumer := Consumer{
ready: make(chan bool, 0),
}
//sarama.Logger = log.New(os.Stdout, "[Sarama] ", log.LstdFlags)
ctx, cancel := context.WithCancel(context.Background())
client, err := sarama.NewConsumerGroup(strings.Split(this.kafkaConfig.BrokerServers, ","), topics[0], this.kafkaConfig.Config)
if err != nil {
log.Panicf("Error creating consumer group client: %v", err)
}
defer client.Close()
go func() {
for err := range client.Errors() {
log.Errorf("Error: %s", err.Error())
}
}()
go func() {
for {
if err := client.Consume(ctx, topics, &consumer); err != nil {
log.Panicf("Error from consumer: %v", err)
}
if ctx.Err() != nil {
log.Errorf("Ctx err: %v", ctx.Err())
return
}
consumer.ready = make(chan bool, 0)
}
}()
select {
case <-ctx.Done():
log.Println("Terminating: context cancelled")
}
<-consumer.ready
cancel()
if err = client.Close(); err != nil {
log.Panicf("Error closing client: %v", err)
}
}
//获取所有延时topic
func (this *centerService) getAllDelayTopics() ([]string, error) {
childrens, err := dao.ZkDao.GetChildrenNode(util.KAFKA_TOPIC_PATH)
if err != nil {
log.Errorf("Get kafka topic err, %v", err)
return nil, err
}
var topics []string
for _, topic := range childrens {
if this.isDelayTopic(topic) {
topics = append(topics, topic)
}
}
log.Infof("Delay topics: %v", topics)
return topics, nil
}
//校验是否是延时topic
func (this *centerService) isDelayTopic(topic string) bool {
if topic == "__consumer_offsets" {
return false
}
if this.kafkaConfig.TopicPrefix == "" || strings.HasPrefix(topic, this.kafkaConfig.TopicPrefix) {
return true
}
return false
}
//同步消息到redis
func (this *centerService) backupMsg(delayMsg *message.DelayMsg) bool {
detail, _ := json.Marshal(delayMsg)
res := recovery.RecoveryService.AddWorkerMsg(delayMsg.WorkerId, delayMsg.MsgId, int64(delayMsg.DelayTime+delayMsg.SendTime), string(detail))
if !res {
log.Errorf("Sync msg to redis err, msg: %s", string(detail))
}
return res
}
//消息发送到worker
func (this *centerService) sendMsg(address string, req *message.Request) error {
connCache, ok := this.rpcConnPool.Load(address)
var client *rpc.Client
if false == ok {
//"127.0.0.1:30000"
var err error
conn, err := net.DialTimeout("tcp", address, 500*time.Millisecond)
if err != nil {
log.WithFields(log.Fields{
"msgId": req.Msg.MsgId,
"address": address,
"err": err,
}).Error("Error get repClient")
return err
}
client = jsonrpc.NewClient(conn)
this.rpcConnPool.Store(address, client)
log.WithFields(log.Fields{
"msgId": req.Msg.MsgId,
"address": address,
}).Info("Add center conn pool")
} else {
client = connCache.(*rpc.Client)
}
log.WithFields(log.Fields{
"msgId": req.Msg.MsgId,
"address": address,
}).Info("Req worker address")
done := make(chan error, 1)
go func() {
var res message.Response
err := client.Call("WorkerConsumer.ConsumerDelayMsg", req, &res)
done <- err
}()
timeout := 5 * time.Second
workers := []string{address}
select {
case <-time.After(timeout):
log.WithFields(log.Fields{
"msgId": req.Msg.MsgId,
"address": address,
"time": timeout,
}).Error("Send msg timeout")
this.closeRpcConn(workers)
return errors.New("Send msg to worker timeout")
case err := <-done:
if err != nil {
this.closeRpcConn(workers)
return err
}
}
return nil
}
//kafka消息回调
func (this *centerService) msgCallback(msg []byte, isDownWorkerMsg bool) error {
log.Debugf("Msg : %v, isDownWorkerMsg: %v", string(msg), isDownWorkerMsg)
request := new(message.Request)
err := json.Unmarshal(msg, &request.Msg)
if err != nil {
log.Errorf("Unmarshal msg err, msg:%s, err:%v", msg, err)
return err
}
if isDownWorkerMsg {
currentTime := uint64(time.Now().UnixNano() / 1e6)
//重新计算延时时间
if request.Msg.DelayTime+request.Msg.SendTime > currentTime {
request.Msg.DelayTime = request.Msg.DelayTime + request.Msg.SendTime - currentTime
} else {
request.Msg.DelayTime = 0
}
}
log.Debugf("Request: %v", request)
return this.msgCallbackWorker(request)
}
func (this *centerService) msgCallbackWorker(request *message.Request) error {
topic := request.Msg.TopicName
excludeWorkerAddress := make([]string, 0)
for reqCnt := 1; reqCnt <= MAX_REQ_CNT; reqCnt++ {
workerAddress, err := this.getOneWorkerAddress(topic, excludeWorkerAddress)
if err != nil {
return err
}
request.Msg.WorkerId = util.GetWorkerIdByWorkerIp(strings.Split(workerAddress, ":")[0])
log.WithFields(log.Fields{
"msgId": request.Msg.MsgId,
"address": workerAddress,
"workerId": request.Msg.WorkerId,
}).Info("Before backup msg ")
if !this.backupMsg(&request.Msg) {
log.WithFields(log.Fields{
"msgId": request.Msg.MsgId,
"address": workerAddress,
"workerId": request.Msg.WorkerId,
}).Errorf("Sync msg to redis err")
go func(request *message.Request) {
this.retryMsgChan <- *request
}(request)
return errors.New(fmt.Sprintf("Sync msg to redis err, msgId: %s", request.Msg.MsgId))
}
log.WithFields(log.Fields{
"msgId": request.Msg.MsgId,
"address": workerAddress,
"workerId": request.Msg.WorkerId,
"reqCnt": reqCnt,
}).Info("Before send msg ")
err = this.sendMsg(workerAddress, request)
if err != nil {
go this.delBackupMsg(request.Msg.WorkerId, request.Msg.MsgId, true)
excludeWorkerAddress = append(excludeWorkerAddress, workerAddress)
continue
}
log.WithFields(log.Fields{
"msgId": request.Msg.MsgId,
"address": workerAddress,
"workerId": request.Msg.WorkerId,
"reqCnt": reqCnt,
}).Info("After send msg ")
return nil
}
return errors.New("Msg call back worker failed, msgId: " + request.Msg.MsgId)
}
//获取worker地址
func (this *centerService) getOneWorkerAddress(topic string, excludeWorkerAddress []string) (string, error) {
workerCache := this.getWorkerAddressCache()
if workerCache == nil || len(workerCache) == 0 {
return "", errors.New("All worker address cache is empty")
}
diffWorkers := util.Difference(workerCache, excludeWorkerAddress)
if len(diffWorkers) == 0 {
diffWorkers = workerCache
}
topicWorkerCache := this.getTopicWorkerAddressCache(topic)
if topicWorkerCache != nil {
return this.getOneActiveWorkerAddress(topicWorkerCache, diffWorkers, topic)
}
workers, err := this.getZKTopicWorkerAddresss(topic)
if err != nil {
log.Warnf("Get zk topic worker address failed, %v", err)
}
var timestamp = time.Now().Unix()
this.topicWorkerAddressCache.Store(topic, map[string]interface{}{
"expire": timestamp + WORKER_ADDRESS_EXPIRE, //更新过期时间
util.WORKER_ADDRESS_CACHE_KEY: workers,
})
log.Infof("Set topic worker address cache, topic: %s, workers:%v", topic, workers)
return this.getOneActiveWorkerAddress(workers, diffWorkers, topic)
}
//获取一个活跃的worker地址
func (this *centerService) getOneActiveWorkerAddress(topicWorkers []string, allWorkers []string, topic string) (string, error) {
if topicWorkers == nil {
log.Warnf("Topic worker address is not exist, topic: %s", topic)
//如果topic分配的worker不存在,在所有活跃的worker中随机取一个
n := rand.Intn(len(allWorkers))
return allWorkers[n], nil
}
activeWorkers := util.Intersect(topicWorkers, allWorkers)
if len(activeWorkers) == 0 {
log.Warnf("Active topic worker address is not exist, topic: %s", topic)
//如果topic分配的活跃worker不存在,在所有活跃的worker中随机取一个
n := rand.Intn(len(allWorkers))
return allWorkers[n], nil
}
n := rand.Intn(len(activeWorkers))
return activeWorkers[n], nil
}
//获取zk上topic绑定的worker地址
func (this *centerService) getZKTopicWorkerAddresss(topic string) ([]string, error) {
path := util.TopicNode(topic)
exists, _, err := dao.ZkDao.Exists(path)
if err != nil {
return nil, err
}
if !exists {
return []string{}, errors.New("Zk node is not exist :" + path)
}
var value []byte
value, _, err = dao.ZkDao.Get(path)
if err != nil {
return []string{}, errors.New(fmt.Sprintf("Get zk node err, err:%v, path:%s", err, path))
}
topicWorkerInfo := new(entity.TopicZkInfo)
err = json.Unmarshal(value, &topicWorkerInfo)
if err != nil {
return []string{}, errors.New(fmt.Sprintf("Unmarshal msg err, msg:%s, err:%v", value, err))
}
return topicWorkerInfo.Workers, nil
}
//监听kafka topic变化
func (this *centerService) watchTopicChange() error {
ech, err := dao.ZkDao.WatchChildNode(util.KAFKA_TOPIC_PATH)
if err != nil {
log.Errorf("Watch children error, %v", err)
return err
}
go this.watchTopicChildChange(ech)
return nil
}
//监听kafka topic子节点变化
func (this *centerService) watchTopicChildChange(watch <-chan zk.Event) error {
event := <-watch
if event.Type == zk.EventNodeChildrenChanged {
newTopics, err := this.getAllDelayTopics()
if err != nil {
log.Errorf("Get kafka topics err, %v", err)
this.watchTopicChange()
return err
}
addTopics := util.Difference(newTopics, this.kafkaTopics)
log.Debugf("AddTopics : %v", addTopics)
if len(addTopics) > 0 {
this.createConsumer(addTopics)
this.kafkaTopics = util.Union(newTopics, this.kafkaTopics)
log.Debugf("Kafka topics update: %v", this.kafkaTopics)
}
this.watchTopicChange()
}
return nil
}
//监听worker变化
func (this *centerService) watchWorkerChange() error {
ech, err := dao.ZkDao.WatchChildNode(util.TmpWorkerNode())
if err != nil {
log.Errorf("Watch children error, %v", err)
return err
}
go this.watchWorkerChildChange(ech)
return nil
}
//监听worker子节点变化
func (this *centerService) watchWorkerChildChange(watch <-chan zk.Event) error {
event := <-watch
if event.Type == zk.EventNodeChildrenChanged {
oldWorkerAddressCache := this.getWorkerAddressCache()
newWorkerAddressCache, err := this.setWorkerAddressCache()
if err != nil {
this.watchWorkerChange()
return err
}
//获取宕机worker消息,重新分发
diffWorkerAddress := util.Difference(oldWorkerAddressCache, newWorkerAddressCache)
log.Infof("Down worker address : %v", diffWorkerAddress)
if len(diffWorkerAddress) == 0 {
this.watchWorkerChange()
return nil
}
go this.closeRpcConn(diffWorkerAddress)
go this.reSendDownWorkerMsg(diffWorkerAddress)
}
this.watchWorkerChange()
return nil
}
//获取宕机worker消息,重新分发
func (this *centerService) reSendDownWorkerMsg(downWorkerAddress []string) {
for _, address := range downWorkerAddress {
lockChildName := "downworker" + address
err := dao.ZkDao.Lock(lockChildName)
if err != nil {
log.Infof("Create lock failed, address: %s, err: %v", address, err)
continue
}
workerId := util.GetWorkerIdByWorkerIp(strings.Split(address, ":")[0])
log.WithFields(log.Fields{
"address": address,
"workerId": workerId,
}).Info("Start pull down worker msg")
this.pullDownWorkerMsgCallback(workerId)
dao.ZkDao.Unlock(lockChildName)
}
}
func (this *centerService) pullDownWorkerMsgCallback(workerId string) {
for {
workerMsgs, err := recovery.RecoveryService.GetWorkerMsgByShard(workerId, 0, PULL_OFFLINE_WORKER_MSG_NUM_EVERY_TIME-1)
if err != nil {
log.Errorf("Get down worker msg failed, workerId : %s, err : %v", workerId, err)
time.Sleep(50 * time.Millisecond)
continue
}
msgCnt := len(workerMsgs)
if msgCnt == 0 {
log.WithFields(log.Fields{
"workerId": workerId,
"msgCnt": msgCnt,
}).Info("Complete pull down worker msg")
break
}
log.WithFields(log.Fields{
"workerId": workerId,
"msgCnt": msgCnt,
}).Info("Pull down worker msg")
var wg sync.WaitGroup
for msgId, msg := range workerMsgs {
if msg == "" {
//删除宕机的worker 备份消息
this.delBackupMsg(workerId, msgId, false)
continue
}
wg.Add(1)
go func(msgId string, workerMsg string) {
callbackRes := this.msgCallback([]byte(workerMsg), true)
if callbackRes == nil {
//删除宕机的worker 备份消息
this.delBackupMsg(workerId, msgId, false)
}
wg.Done()
}(msgId, msg)
}
wg.Wait()
time.Sleep(20 * time.Millisecond)
}
}
//设置worker地址缓存
func (this *centerService) setWorkerAddressCache() ([]string, error) {
childrens, err := this.getAllWorkerAddress()
if err != nil {
return nil, err
}
log.Infof("Set worker address cache, %v", childrens)
this.workerAddressCache.Store(util.WORKER_ADDRESS_CACHE_KEY, childrens)
return childrens, err
}
//获取topic对应的worker地址缓存
func (this *centerService) getTopicWorkerAddressCache(topic string) []string {
topicWorkerMapCache, ok := this.topicWorkerAddressCache.Load(topic)
if ok == false {
log.Infof("Get topic map worker address cache failed, topic :%s", topic)
return nil
}
var timestamp = time.Now().Unix()
log.Debugf("Topic: %s, cacheData: %v, current time: %v", topic, topicWorkerMapCache, timestamp)
if topicWorkerMapCache != nil && topicWorkerMapCache.(map[string]interface{})["expire"].(int64) > timestamp {
return topicWorkerMapCache.(map[string]interface{})[util.WORKER_ADDRESS_CACHE_KEY].([]string)
}
return nil
}
//获取worker地址缓存
func (this *centerService) getWorkerAddressCache() []string {
workerCache, ok := this.workerAddressCache.Load(util.WORKER_ADDRESS_CACHE_KEY)
if ok == false {
log.Errorf("Get worker address cache failed")
return nil
}
return workerCache.([]string)
}
//获取所有的worker地址
func (this *centerService) getAllWorkerAddress() ([]string, error) {
childrens, err := discover.DiscoverClient.DiscoverServices(util.WORKER_SERVICE)
if err != nil {
log.Errorf("Get worker list err, %v", err)
return nil, err
}
return childrens, nil
}
func (this *centerService) closeRpcConn(workers []string) {
for _, address := range workers {
connCache, ok := this.rpcConnPool.Load(address)
if ok {
connCache.(*rpc.Client).Close()
this.rpcConnPool.Delete(address)
}
}
}
func (this *centerService) Stop() {
workerCache := this.getWorkerAddressCache()
if workerCache == nil || len(workerCache) == 0 {
return
}
this.closeRpcConn(workerCache)
}
//延时消息消费
func (this *CenterConsumer) ConsumerDelayMsg(req message.Request, res *message.Response) error {
log.Debugf("Consumer delay msg, req: %v", req)
return CenterService.msgCallbackWorker(&req)
}
func (this *centerService) delBackupMsg(workerId string, msgId string, isDelWorkerIdMap bool) {
res := recovery.RecoveryService.DelWorkerMsg(workerId, msgId, isDelWorkerIdMap)
if !res {
log.WithFields(log.Fields{
"msgId": msgId,
"workerId": workerId,
}).Error("Del worker redis backup msg err")
}
}
//消息重试
func (this *centerService) msgRetry() {
for {
time.Sleep(10 * time.Millisecond)
req, ok := <-this.retryMsgChan
if !ok {
time.Sleep(100 * time.Millisecond)
log.Debugf("Retry msg not exist")
continue
}
log.WithFields(log.Fields{
"msgId": req.Msg.MsgId,
}).Infof("Msg retry")
go this.msgCallbackWorker(&req)
}
}