You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
579 lines
23 KiB
579 lines
23 KiB
2 years ago
|
package msgtransfer
|
||
4 years ago
|
|
||
|
import (
|
||
3 years ago
|
"Open_IM/pkg/common/config"
|
||
|
"Open_IM/pkg/common/constant"
|
||
3 years ago
|
"Open_IM/pkg/common/db"
|
||
3 years ago
|
kfk "Open_IM/pkg/common/kafka"
|
||
|
"Open_IM/pkg/common/log"
|
||
2 years ago
|
pbMsg "Open_IM/pkg/proto/msg"
|
||
3 years ago
|
pbPush "Open_IM/pkg/proto/push"
|
||
3 years ago
|
"Open_IM/pkg/utils"
|
||
4 years ago
|
"context"
|
||
|
"github.com/Shopify/sarama"
|
||
|
"github.com/golang/protobuf/proto"
|
||
3 years ago
|
"hash/crc32"
|
||
4 years ago
|
"strings"
|
||
3 years ago
|
"sync"
|
||
3 years ago
|
"time"
|
||
4 years ago
|
)
|
||
|
|
||
3 years ago
|
type MsgChannelValue struct {
|
||
3 years ago
|
aggregationID string //maybe userID or super groupID
|
||
|
triggerID string
|
||
|
msgList []*pbMsg.MsgDataToMQ
|
||
|
lastSeq uint64
|
||
3 years ago
|
}
|
||
|
type TriggerChannelValue struct {
|
||
|
triggerID string
|
||
|
cmsgList []*sarama.ConsumerMessage
|
||
3 years ago
|
}
|
||
|
type fcb func(cMsg *sarama.ConsumerMessage, msgKey string, sess sarama.ConsumerGroupSession)
|
||
3 years ago
|
type Cmd2Value struct {
|
||
3 years ago
|
Cmd int
|
||
3 years ago
|
Value interface{}
|
||
|
}
|
||
2 years ago
|
type OnlineHistoryRedisConsumerHandler struct {
|
||
3 years ago
|
msgHandle map[string]fcb
|
||
|
historyConsumerGroup *kfk.MConsumerGroup
|
||
3 years ago
|
chArrays [ChannelNum]chan Cmd2Value
|
||
3 years ago
|
msgDistributionCh chan Cmd2Value
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
func (och *OnlineHistoryRedisConsumerHandler) Init(cmdCh chan Cmd2Value) {
|
||
3 years ago
|
och.msgHandle = make(map[string]fcb)
|
||
3 years ago
|
och.msgDistributionCh = make(chan Cmd2Value) //no buffer channel
|
||
|
go och.MessagesDistributionHandle()
|
||
3 years ago
|
for i := 0; i < ChannelNum; i++ {
|
||
3 years ago
|
och.chArrays[i] = make(chan Cmd2Value, 50)
|
||
3 years ago
|
go och.Run(i)
|
||
|
}
|
||
3 years ago
|
if config.Config.ReliableStorage {
|
||
|
och.msgHandle[config.Config.Kafka.Ws2mschat.Topic] = och.handleChatWs2Mongo
|
||
|
} else {
|
||
|
och.msgHandle[config.Config.Kafka.Ws2mschat.Topic] = och.handleChatWs2MongoLowReliability
|
||
3 years ago
|
|
||
3 years ago
|
}
|
||
3 years ago
|
och.historyConsumerGroup = kfk.NewMConsumerGroup(&kfk.MConsumerGroupConfig{KafkaVersion: sarama.V2_0_0_0,
|
||
4 years ago
|
OffsetsInitial: sarama.OffsetNewest, IsReturnErr: false}, []string{config.Config.Kafka.Ws2mschat.Topic},
|
||
2 years ago
|
config.Config.Kafka.Ws2mschat.Addr, config.Config.Kafka.ConsumerGroupID.MsgToRedis)
|
||
3 years ago
|
|
||
3 years ago
|
}
|
||
2 years ago
|
func (och *OnlineHistoryRedisConsumerHandler) Run(channelID int) {
|
||
3 years ago
|
for {
|
||
|
select {
|
||
3 years ago
|
case cmd := <-och.chArrays[channelID]:
|
||
3 years ago
|
switch cmd.Cmd {
|
||
3 years ago
|
case AggregationMessages:
|
||
3 years ago
|
msgChannelValue := cmd.Value.(MsgChannelValue)
|
||
3 years ago
|
msgList := msgChannelValue.msgList
|
||
3 years ago
|
triggerID := msgChannelValue.triggerID
|
||
3 years ago
|
storageMsgList := make([]*pbMsg.MsgDataToMQ, 0, 80)
|
||
3 years ago
|
notStoragePushMsgList := make([]*pbMsg.MsgDataToMQ, 0, 80)
|
||
|
log.Debug(triggerID, "msg arrived channel", "channel id", channelID, msgList, msgChannelValue.aggregationID, len(msgList))
|
||
2 years ago
|
var modifyMsgList []*pbMsg.MsgDataToMQ
|
||
3 years ago
|
for _, v := range msgList {
|
||
3 years ago
|
log.Debug(triggerID, "msg come to storage center", v.String())
|
||
3 years ago
|
isHistory := utils.GetSwitchFromOptions(v.MsgData.Options, constant.IsHistory)
|
||
|
isSenderSync := utils.GetSwitchFromOptions(v.MsgData.Options, constant.IsSenderSync)
|
||
|
if isHistory {
|
||
|
storageMsgList = append(storageMsgList, v)
|
||
3 years ago
|
//log.NewWarn(triggerID, "storageMsgList to mongodb client msgID: ", v.MsgData.ClientMsgID)
|
||
3 years ago
|
} else {
|
||
3 years ago
|
if !(!isSenderSync && msgChannelValue.aggregationID == v.MsgData.SendID) {
|
||
|
notStoragePushMsgList = append(notStoragePushMsgList, v)
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
2 years ago
|
if v.MsgData.ContentType == constant.ReactionMessageModifier || v.MsgData.ContentType == constant.ReactionMessageDeleter {
|
||
|
modifyMsgList = append(modifyMsgList, v)
|
||
|
}
|
||
|
}
|
||
|
if len(modifyMsgList) > 0 {
|
||
|
sendMessageToModifyMQ(msgChannelValue.aggregationID, triggerID, modifyMsgList)
|
||
3 years ago
|
}
|
||
3 years ago
|
//switch msgChannelValue.msg.MsgData.SessionType {
|
||
|
//case constant.SingleChatType:
|
||
|
//case constant.GroupChatType:
|
||
|
//case constant.NotificationChatType:
|
||
|
//default:
|
||
|
// log.NewError(msgFromMQ.OperationID, "SessionType error", msgFromMQ.String())
|
||
|
// return
|
||
|
//}
|
||
3 years ago
|
log.Debug(triggerID, "msg storage length", len(storageMsgList), "push length", len(notStoragePushMsgList))
|
||
2 years ago
|
if len(storageMsgList) > 0 {
|
||
|
err, lastSeq := saveUserChatList(msgChannelValue.aggregationID, storageMsgList, triggerID)
|
||
|
if err != nil {
|
||
|
singleMsgFailedCount += uint64(len(storageMsgList))
|
||
|
log.NewError(triggerID, "single data insert to redis err", err.Error(), storageMsgList)
|
||
|
} else {
|
||
|
singleMsgSuccessCountMutex.Lock()
|
||
|
singleMsgSuccessCount += uint64(len(storageMsgList))
|
||
|
singleMsgSuccessCountMutex.Unlock()
|
||
|
och.SendMessageToMongoCH(msgChannelValue.aggregationID, triggerID, storageMsgList, lastSeq)
|
||
2 years ago
|
|
||
2 years ago
|
for _, v := range storageMsgList {
|
||
|
sendMessageToPushMQ(v, msgChannelValue.aggregationID)
|
||
|
}
|
||
|
for _, x := range notStoragePushMsgList {
|
||
|
sendMessageToPushMQ(x, msgChannelValue.aggregationID)
|
||
|
}
|
||
2 years ago
|
}
|
||
|
|
||
3 years ago
|
} else {
|
||
2 years ago
|
for _, x := range notStoragePushMsgList {
|
||
|
sendMessageToPushMQ(x, msgChannelValue.aggregationID)
|
||
|
}
|
||
3 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
2 years ago
|
|
||
2 years ago
|
func (och *OnlineHistoryRedisConsumerHandler) SendMessageToMongoCH(aggregationID string, triggerID string, messages []*pbMsg.MsgDataToMQ, lastSeq uint64) {
|
||
2 years ago
|
if len(messages) > 0 {
|
||
|
pid, offset, err := producerToMongo.SendMessage(&pbMsg.MsgDataToMongoByMQ{LastSeq: lastSeq, AggregationID: aggregationID, MessageList: messages, TriggerID: triggerID}, aggregationID, triggerID)
|
||
|
if err != nil {
|
||
|
log.Error(triggerID, "kafka send failed", "send data", len(messages), "pid", pid, "offset", offset, "err", err.Error(), "key", aggregationID)
|
||
|
} else {
|
||
|
// log.NewWarn(m.OperationID, "sendMsgToKafka client msgID ", m.MsgData.ClientMsgID)
|
||
|
}
|
||
2 years ago
|
}
|
||
|
//hashCode := getHashCode(aggregationID)
|
||
|
//channelID := hashCode % ChannelNum
|
||
|
//log.Debug(triggerID, "generate channelID", hashCode, channelID, aggregationID)
|
||
|
////go func(cID uint32, userID string, messages []*pbMsg.MsgDataToMQ) {
|
||
|
//och.chMongoArrays[channelID] <- Cmd2Value{Cmd: MongoMessages, Value: MsgChannelValue{aggregationID: aggregationID, msgList: messages, triggerID: triggerID, lastSeq: lastSeq}}
|
||
3 years ago
|
}
|
||
3 years ago
|
|
||
2 years ago
|
//func (och *OnlineHistoryRedisConsumerHandler) MongoMessageRun(channelID int) {
|
||
|
// for {
|
||
|
// select {
|
||
|
// case cmd := <-och.chMongoArrays[channelID]:
|
||
|
// switch cmd.Cmd {
|
||
|
// case MongoMessages:
|
||
|
// msgChannelValue := cmd.Value.(MsgChannelValue)
|
||
|
// msgList := msgChannelValue.msgList
|
||
|
// triggerID := msgChannelValue.triggerID
|
||
|
// aggregationID := msgChannelValue.aggregationID
|
||
|
// lastSeq := msgChannelValue.lastSeq
|
||
|
// err := db.DB.BatchInsertChat2DB(aggregationID, msgList, triggerID, lastSeq)
|
||
|
// if err != nil {
|
||
|
// log.NewError(triggerID, "single data insert to mongo err", err.Error(), msgList)
|
||
|
// }
|
||
|
// for _, v := range msgList {
|
||
|
// if v.MsgData.ContentType == constant.DeleteMessageNotification {
|
||
2 years ago
|
// tips := sdkws.TipsComm{}
|
||
|
// DeleteMessageTips := sdkws.DeleteMessageTips{}
|
||
2 years ago
|
// err := proto.Unmarshal(v.MsgData.Content, &tips)
|
||
|
// if err != nil {
|
||
|
// log.NewError(triggerID, "tips unmarshal err:", err.Error(), v.String())
|
||
|
// continue
|
||
|
// }
|
||
|
// err = proto.Unmarshal(tips.Detail, &DeleteMessageTips)
|
||
|
// if err != nil {
|
||
|
// log.NewError(triggerID, "deleteMessageTips unmarshal err:", err.Error(), v.String())
|
||
|
// continue
|
||
|
// }
|
||
|
// if unexistSeqList, err := db.DB.DelMsgBySeqList(DeleteMessageTips.UserID, DeleteMessageTips.SeqList, v.OperationID); err != nil {
|
||
|
// log.NewError(v.OperationID, utils.GetSelfFuncName(), "DelMsgBySeqList args: ", DeleteMessageTips.UserID, DeleteMessageTips.SeqList, v.OperationID, err.Error(), unexistSeqList)
|
||
|
// }
|
||
|
//
|
||
|
// }
|
||
|
// }
|
||
|
// }
|
||
|
// }
|
||
|
// }
|
||
|
//}
|
||
3 years ago
|
|
||
2 years ago
|
func (och *OnlineHistoryRedisConsumerHandler) MessagesDistributionHandle() {
|
||
3 years ago
|
for {
|
||
3 years ago
|
aggregationMsgs := make(map[string][]*pbMsg.MsgDataToMQ, ChannelNum)
|
||
3 years ago
|
select {
|
||
|
case cmd := <-och.msgDistributionCh:
|
||
|
switch cmd.Cmd {
|
||
|
case ConsumerMsgs:
|
||
|
triggerChannelValue := cmd.Value.(TriggerChannelValue)
|
||
|
triggerID := triggerChannelValue.triggerID
|
||
|
consumerMessages := triggerChannelValue.cmsgList
|
||
|
//Aggregation map[userid]message list
|
||
|
log.Debug(triggerID, "batch messages come to distribution center", len(consumerMessages))
|
||
|
for i := 0; i < len(consumerMessages); i++ {
|
||
|
msgFromMQ := pbMsg.MsgDataToMQ{}
|
||
|
err := proto.Unmarshal(consumerMessages[i].Value, &msgFromMQ)
|
||
|
if err != nil {
|
||
|
log.Error(triggerID, "msg_transfer Unmarshal msg err", "msg", string(consumerMessages[i].Value), "err", err.Error())
|
||
|
return
|
||
|
}
|
||
|
log.Debug(triggerID, "single msg come to distribution center", msgFromMQ.String(), string(consumerMessages[i].Key))
|
||
3 years ago
|
if oldM, ok := aggregationMsgs[string(consumerMessages[i].Key)]; ok {
|
||
3 years ago
|
oldM = append(oldM, &msgFromMQ)
|
||
3 years ago
|
aggregationMsgs[string(consumerMessages[i].Key)] = oldM
|
||
3 years ago
|
} else {
|
||
|
m := make([]*pbMsg.MsgDataToMQ, 0, 100)
|
||
|
m = append(m, &msgFromMQ)
|
||
3 years ago
|
aggregationMsgs[string(consumerMessages[i].Key)] = m
|
||
3 years ago
|
}
|
||
|
}
|
||
3 years ago
|
log.Debug(triggerID, "generate map list users len", len(aggregationMsgs))
|
||
|
for aggregationID, v := range aggregationMsgs {
|
||
3 years ago
|
if len(v) >= 0 {
|
||
3 years ago
|
hashCode := getHashCode(aggregationID)
|
||
3 years ago
|
channelID := hashCode % ChannelNum
|
||
3 years ago
|
log.Debug(triggerID, "generate channelID", hashCode, channelID, aggregationID)
|
||
3 years ago
|
//go func(cID uint32, userID string, messages []*pbMsg.MsgDataToMQ) {
|
||
3 years ago
|
och.chArrays[channelID] <- Cmd2Value{Cmd: AggregationMessages, Value: MsgChannelValue{aggregationID: aggregationID, msgList: v, triggerID: triggerID}}
|
||
3 years ago
|
//}(channelID, userID, v)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
}
|
||
|
|
||
|
}
|
||
2 years ago
|
func (mc *OnlineHistoryRedisConsumerHandler) handleChatWs2Mongo(cMsg *sarama.ConsumerMessage, msgKey string, sess sarama.ConsumerGroupSession) {
|
||
3 years ago
|
msg := cMsg.Value
|
||
3 years ago
|
now := time.Now()
|
||
3 years ago
|
msgFromMQ := pbMsg.MsgDataToMQ{}
|
||
|
err := proto.Unmarshal(msg, &msgFromMQ)
|
||
4 years ago
|
if err != nil {
|
||
3 years ago
|
log.Error("msg_transfer Unmarshal msg err", "", "msg", string(msg), "err", err.Error())
|
||
4 years ago
|
return
|
||
|
}
|
||
3 years ago
|
operationID := msgFromMQ.OperationID
|
||
3 years ago
|
log.NewInfo(operationID, "msg come mongo!!!", "", "msg", string(msg))
|
||
4 years ago
|
//Control whether to store offline messages (mongo)
|
||
3 years ago
|
isHistory := utils.GetSwitchFromOptions(msgFromMQ.MsgData.Options, constant.IsHistory)
|
||
4 years ago
|
//Control whether to store history messages (mysql)
|
||
3 years ago
|
isPersist := utils.GetSwitchFromOptions(msgFromMQ.MsgData.Options, constant.IsPersistent)
|
||
3 years ago
|
isSenderSync := utils.GetSwitchFromOptions(msgFromMQ.MsgData.Options, constant.IsSenderSync)
|
||
3 years ago
|
switch msgFromMQ.MsgData.SessionType {
|
||
3 years ago
|
case constant.SingleChatType:
|
||
3 years ago
|
log.NewDebug(msgFromMQ.OperationID, "msg_transfer msg type = SingleChatType", isHistory, isPersist)
|
||
4 years ago
|
if isHistory {
|
||
3 years ago
|
err := saveUserChat(msgKey, &msgFromMQ)
|
||
|
if err != nil {
|
||
3 years ago
|
singleMsgFailedCount++
|
||
3 years ago
|
log.NewError(operationID, "single data insert to mongo err", err.Error(), msgFromMQ.String())
|
||
|
return
|
||
4 years ago
|
}
|
||
3 years ago
|
singleMsgSuccessCountMutex.Lock()
|
||
3 years ago
|
singleMsgSuccessCount++
|
||
3 years ago
|
singleMsgSuccessCountMutex.Unlock()
|
||
3 years ago
|
log.NewDebug(msgFromMQ.OperationID, "sendMessageToPush cost time ", time.Since(now))
|
||
4 years ago
|
}
|
||
3 years ago
|
if !isSenderSync && msgKey == msgFromMQ.MsgData.SendID {
|
||
|
} else {
|
||
|
go sendMessageToPush(&msgFromMQ, msgKey)
|
||
|
}
|
||
3 years ago
|
log.NewDebug(operationID, "saveSingleMsg cost time ", time.Since(now))
|
||
3 years ago
|
case constant.GroupChatType:
|
||
3 years ago
|
log.NewDebug(msgFromMQ.OperationID, "msg_transfer msg type = GroupChatType", isHistory, isPersist)
|
||
3 years ago
|
if isHistory {
|
||
3 years ago
|
err := saveUserChat(msgFromMQ.MsgData.RecvID, &msgFromMQ)
|
||
3 years ago
|
if err != nil {
|
||
3 years ago
|
log.NewError(operationID, "group data insert to mongo err", msgFromMQ.String(), msgFromMQ.MsgData.RecvID, err.Error())
|
||
3 years ago
|
return
|
||
|
}
|
||
3 years ago
|
groupMsgCount++
|
||
3 years ago
|
}
|
||
3 years ago
|
go sendMessageToPush(&msgFromMQ, msgFromMQ.MsgData.RecvID)
|
||
3 years ago
|
log.NewDebug(operationID, "saveGroupMsg cost time ", time.Since(now))
|
||
|
|
||
3 years ago
|
case constant.NotificationChatType:
|
||
|
log.NewDebug(msgFromMQ.OperationID, "msg_transfer msg type = NotificationChatType", isHistory, isPersist)
|
||
|
if isHistory {
|
||
|
err := saveUserChat(msgKey, &msgFromMQ)
|
||
|
if err != nil {
|
||
|
log.NewError(operationID, "single data insert to mongo err", err.Error(), msgFromMQ.String())
|
||
|
return
|
||
|
}
|
||
3 years ago
|
log.NewDebug(msgFromMQ.OperationID, "sendMessageToPush cost time ", time.Since(now))
|
||
3 years ago
|
}
|
||
|
if !isSenderSync && msgKey == msgFromMQ.MsgData.SendID {
|
||
|
} else {
|
||
|
go sendMessageToPush(&msgFromMQ, msgKey)
|
||
|
}
|
||
3 years ago
|
log.NewDebug(operationID, "saveUserChat cost time ", time.Since(now))
|
||
3 years ago
|
default:
|
||
3 years ago
|
log.NewError(msgFromMQ.OperationID, "SessionType error", msgFromMQ.String())
|
||
3 years ago
|
return
|
||
4 years ago
|
}
|
||
3 years ago
|
sess.MarkMessage(cMsg, "")
|
||
|
log.NewDebug(msgFromMQ.OperationID, "msg_transfer handle topic data to database success...", msgFromMQ.String())
|
||
|
}
|
||
3 years ago
|
|
||
2 years ago
|
func (och *OnlineHistoryRedisConsumerHandler) handleChatWs2MongoLowReliability(cMsg *sarama.ConsumerMessage, msgKey string, sess sarama.ConsumerGroupSession) {
|
||
3 years ago
|
msg := cMsg.Value
|
||
|
msgFromMQ := pbMsg.MsgDataToMQ{}
|
||
|
err := proto.Unmarshal(msg, &msgFromMQ)
|
||
|
if err != nil {
|
||
|
log.Error("msg_transfer Unmarshal msg err", "", "msg", string(msg), "err", err.Error())
|
||
|
return
|
||
|
}
|
||
|
operationID := msgFromMQ.OperationID
|
||
|
log.NewInfo(operationID, "msg come mongo!!!", "", "msg", string(msg))
|
||
|
//Control whether to store offline messages (mongo)
|
||
|
isHistory := utils.GetSwitchFromOptions(msgFromMQ.MsgData.Options, constant.IsHistory)
|
||
|
isSenderSync := utils.GetSwitchFromOptions(msgFromMQ.MsgData.Options, constant.IsSenderSync)
|
||
3 years ago
|
if isHistory {
|
||
|
seq, err := db.DB.IncrUserSeq(msgKey)
|
||
|
if err != nil {
|
||
|
log.NewError(operationID, "data insert to redis err", err.Error(), string(msg))
|
||
|
return
|
||
3 years ago
|
}
|
||
3 years ago
|
sess.MarkMessage(cMsg, "")
|
||
|
msgFromMQ.MsgData.Seq = uint32(seq)
|
||
|
log.Debug(operationID, "send ch msg is ", msgFromMQ.String())
|
||
3 years ago
|
//och.msgCh <- Cmd2Value{Cmd: Msg, Value: MsgChannelValue{msgKey, msgFromMQ}}
|
||
3 years ago
|
//err := saveUserChat(msgKey, &msgFromMQ)
|
||
|
//if err != nil {
|
||
|
// singleMsgFailedCount++
|
||
|
// log.NewError(operationID, "single data insert to mongo err", err.Error(), msgFromMQ.String())
|
||
|
// return
|
||
|
//}
|
||
|
//singleMsgSuccessCountMutex.Lock()
|
||
|
//singleMsgSuccessCount++
|
||
|
//singleMsgSuccessCountMutex.Unlock()
|
||
|
//log.NewDebug(msgFromMQ.OperationID, "sendMessageToPush cost time ", time.Since(now))
|
||
|
} else {
|
||
|
if !(!isSenderSync && msgKey == msgFromMQ.MsgData.SendID) {
|
||
3 years ago
|
go sendMessageToPush(&msgFromMQ, msgKey)
|
||
|
}
|
||
|
}
|
||
4 years ago
|
}
|
||
|
|
||
2 years ago
|
func (OnlineHistoryRedisConsumerHandler) Setup(_ sarama.ConsumerGroupSession) error { return nil }
|
||
|
func (OnlineHistoryRedisConsumerHandler) Cleanup(_ sarama.ConsumerGroupSession) error { return nil }
|
||
3 years ago
|
|
||
2 years ago
|
//func (och *OnlineHistoryRedisConsumerHandler) ConsumeClaim(sess sarama.ConsumerGroupSession,
|
||
3 years ago
|
// claim sarama.ConsumerGroupClaim) error { // a instance in the consumer group
|
||
|
// log.NewDebug("", "online new session msg come", claim.HighWaterMarkOffset(), claim.Topic(), claim.Partition())
|
||
|
// for msg := range claim.Messages() {
|
||
|
// SetOnlineTopicStatus(OnlineTopicBusy)
|
||
|
// //och.TriggerCmd(OnlineTopicBusy)
|
||
|
// log.NewDebug("", "online kafka get info to mongo", "msgTopic", msg.Topic, "msgPartition", msg.Partition, "online", msg.Offset, claim.HighWaterMarkOffset())
|
||
|
// och.msgHandle[msg.Topic](msg, string(msg.Key), sess)
|
||
|
// if claim.HighWaterMarkOffset()-msg.Offset <= 1 {
|
||
|
// log.Debug("", "online msg consume end", claim.HighWaterMarkOffset(), msg.Offset)
|
||
|
// SetOnlineTopicStatus(OnlineTopicVacancy)
|
||
|
// och.TriggerCmd(OnlineTopicVacancy)
|
||
|
// }
|
||
|
// }
|
||
|
// return nil
|
||
|
//}
|
||
|
|
||
2 years ago
|
func (och *OnlineHistoryRedisConsumerHandler) ConsumeClaim(sess sarama.ConsumerGroupSession,
|
||
3 years ago
|
claim sarama.ConsumerGroupClaim) error { // a instance in the consumer group
|
||
3 years ago
|
|
||
3 years ago
|
for {
|
||
|
if sess == nil {
|
||
|
log.NewWarn("", " sess == nil, waiting ")
|
||
|
time.Sleep(100 * time.Millisecond)
|
||
|
} else {
|
||
|
break
|
||
|
}
|
||
3 years ago
|
}
|
||
3 years ago
|
rwLock := new(sync.RWMutex)
|
||
3 years ago
|
log.NewDebug("", "online new session msg come", claim.HighWaterMarkOffset(), claim.Topic(), claim.Partition())
|
||
3 years ago
|
cMsg := make([]*sarama.ConsumerMessage, 0, 1000)
|
||
|
t := time.NewTicker(time.Duration(100) * time.Millisecond)
|
||
3 years ago
|
var triggerID string
|
||
3 years ago
|
go func() {
|
||
3 years ago
|
for {
|
||
|
select {
|
||
|
//case :
|
||
|
// triggerID = utils.OperationIDGenerator()
|
||
|
//
|
||
|
// log.NewDebug(triggerID, "claim.Messages ", msg)
|
||
|
// cMsg = append(cMsg, msg)
|
||
|
// if len(cMsg) >= 1000 {
|
||
|
// ccMsg := make([]*sarama.ConsumerMessage, 0, 1000)
|
||
|
// for _, v := range cMsg {
|
||
|
// ccMsg = append(ccMsg, v)
|
||
|
// }
|
||
|
// log.Debug(triggerID, "length trigger msg consumer start", len(ccMsg))
|
||
|
// och.msgDistributionCh <- Cmd2Value{Cmd: ConsumerMsgs, Value: TriggerChannelValue{
|
||
|
// triggerID: triggerID, cmsgList: ccMsg}}
|
||
|
// sess.MarkMessage(msg, "")
|
||
|
// cMsg = make([]*sarama.ConsumerMessage, 0, 1000)
|
||
|
// log.Debug(triggerID, "length trigger msg consumer end", len(cMsg))
|
||
|
// }
|
||
3 years ago
|
|
||
3 years ago
|
case <-t.C:
|
||
|
if len(cMsg) > 0 {
|
||
|
rwLock.Lock()
|
||
|
ccMsg := make([]*sarama.ConsumerMessage, 0, 1000)
|
||
|
for _, v := range cMsg {
|
||
|
ccMsg = append(ccMsg, v)
|
||
|
}
|
||
|
cMsg = make([]*sarama.ConsumerMessage, 0, 1000)
|
||
|
rwLock.Unlock()
|
||
|
split := 1000
|
||
|
triggerID = utils.OperationIDGenerator()
|
||
2 years ago
|
log.Debug(triggerID, "timer trigger msg consumer start", len(ccMsg))
|
||
3 years ago
|
for i := 0; i < len(ccMsg)/split; i++ {
|
||
|
//log.Debug()
|
||
|
och.msgDistributionCh <- Cmd2Value{Cmd: ConsumerMsgs, Value: TriggerChannelValue{
|
||
|
triggerID: triggerID, cmsgList: ccMsg[i*split : (i+1)*split]}}
|
||
|
}
|
||
|
if (len(ccMsg) % split) > 0 {
|
||
|
och.msgDistributionCh <- Cmd2Value{Cmd: ConsumerMsgs, Value: TriggerChannelValue{
|
||
|
triggerID: triggerID, cmsgList: ccMsg[split*(len(ccMsg)/split):]}}
|
||
|
}
|
||
|
//sess.MarkMessage(ccMsg[len(cMsg)-1], "")
|
||
|
|
||
2 years ago
|
log.Debug(triggerID, "timer trigger msg consumer end", len(cMsg))
|
||
3 years ago
|
}
|
||
3 years ago
|
}
|
||
|
}
|
||
3 years ago
|
|
||
3 years ago
|
}()
|
||
3 years ago
|
for msg := range claim.Messages() {
|
||
|
//msgFromMQ := pbMsg.MsgDataToMQ{}
|
||
|
//err := proto.Unmarshal(msg.Value, &msgFromMQ)
|
||
|
//if err != nil {
|
||
|
// log.Error(triggerID, "msg_transfer Unmarshal msg err", "msg", string(msg.Value), "err", err.Error())
|
||
|
//}
|
||
|
//userID := string(msg.Key)
|
||
|
//hashCode := getHashCode(userID)
|
||
|
//channelID := hashCode % ChannelNum
|
||
|
//log.Debug(triggerID, "generate channelID", hashCode, channelID, userID)
|
||
|
////go func(cID uint32, userID string, messages []*pbMsg.MsgDataToMQ) {
|
||
|
//och.chArrays[channelID] <- Cmd2Value{Cmd: UserMessages, Value: MsgChannelValue{userID: userID, msgList: []*pbMsg.MsgDataToMQ{&msgFromMQ}, triggerID: msgFromMQ.OperationID}}
|
||
|
//sess.MarkMessage(msg, "")
|
||
|
rwLock.Lock()
|
||
2 years ago
|
if len(msg.Value) != 0 {
|
||
|
cMsg = append(cMsg, msg)
|
||
|
}
|
||
3 years ago
|
rwLock.Unlock()
|
||
|
sess.MarkMessage(msg, "")
|
||
|
//och.TriggerCmd(OnlineTopicBusy)
|
||
|
|
||
|
//log.NewDebug("", "online kafka get info to mongo", "msgTopic", msg.Topic, "msgPartition", msg.Partition, "online", msg.Offset, claim.HighWaterMarkOffset())
|
||
|
|
||
|
}
|
||
3 years ago
|
|
||
4 years ago
|
return nil
|
||
|
}
|
||
3 years ago
|
|
||
2 years ago
|
//func (och *OnlineHistoryRedisConsumerHandler) ConsumeClaim(sess sarama.ConsumerGroupSession,
|
||
3 years ago
|
// claim sarama.ConsumerGroupClaim) error { // a instance in the consumer group
|
||
|
//
|
||
|
// for {
|
||
|
// if sess == nil {
|
||
|
// log.NewWarn("", " sess == nil, waiting ")
|
||
|
// time.Sleep(100 * time.Millisecond)
|
||
|
// } else {
|
||
|
// break
|
||
|
// }
|
||
|
// }
|
||
|
//
|
||
|
// log.NewDebug("", "online new session msg come", claim.HighWaterMarkOffset(), claim.Topic(), claim.Partition())
|
||
|
// cMsg := make([]*sarama.ConsumerMessage, 0, 1000)
|
||
|
// t := time.NewTicker(time.Duration(100) * time.Millisecond)
|
||
|
// var triggerID string
|
||
|
// for msg := range claim.Messages() {
|
||
|
// cMsg = append(cMsg, msg)
|
||
|
// //och.TriggerCmd(OnlineTopicBusy)
|
||
|
// select {
|
||
|
// //case :
|
||
|
// // triggerID = utils.OperationIDGenerator()
|
||
|
// //
|
||
|
// // log.NewDebug(triggerID, "claim.Messages ", msg)
|
||
|
// // cMsg = append(cMsg, msg)
|
||
|
// // if len(cMsg) >= 1000 {
|
||
|
// // ccMsg := make([]*sarama.ConsumerMessage, 0, 1000)
|
||
|
// // for _, v := range cMsg {
|
||
|
// // ccMsg = append(ccMsg, v)
|
||
|
// // }
|
||
|
// // log.Debug(triggerID, "length trigger msg consumer start", len(ccMsg))
|
||
|
// // och.msgDistributionCh <- Cmd2Value{Cmd: ConsumerMsgs, Value: TriggerChannelValue{
|
||
|
// // triggerID: triggerID, cmsgList: ccMsg}}
|
||
|
// // sess.MarkMessage(msg, "")
|
||
|
// // cMsg = make([]*sarama.ConsumerMessage, 0, 1000)
|
||
|
// // log.Debug(triggerID, "length trigger msg consumer end", len(cMsg))
|
||
|
// // }
|
||
|
//
|
||
|
// case <-t.C:
|
||
|
// if len(cMsg) > 0 {
|
||
|
// ccMsg := make([]*sarama.ConsumerMessage, 0, 1000)
|
||
|
// for _, v := range cMsg {
|
||
|
// ccMsg = append(ccMsg, v)
|
||
|
// }
|
||
|
// triggerID = utils.OperationIDGenerator()
|
||
|
// log.Debug(triggerID, "timer trigger msg consumer start", len(ccMsg))
|
||
|
// och.msgDistributionCh <- Cmd2Value{Cmd: ConsumerMsgs, Value: TriggerChannelValue{
|
||
|
// triggerID: triggerID, cmsgList: ccMsg}}
|
||
|
// sess.MarkMessage(cMsg[len(cMsg)-1], "")
|
||
|
// cMsg = make([]*sarama.ConsumerMessage, 0, 1000)
|
||
|
// log.Debug(triggerID, "timer trigger msg consumer end", len(cMsg))
|
||
|
// }
|
||
|
// default:
|
||
|
//
|
||
|
// }
|
||
|
// //log.NewDebug("", "online kafka get info to mongo", "msgTopic", msg.Topic, "msgPartition", msg.Partition, "online", msg.Offset, claim.HighWaterMarkOffset())
|
||
|
//
|
||
|
// }
|
||
|
// return nil
|
||
|
//}
|
||
2 years ago
|
|
||
3 years ago
|
func sendMessageToPush(message *pbMsg.MsgDataToMQ, pushToUserID string) {
|
||
3 years ago
|
log.Info(message.OperationID, "msg_transfer send message to push", "message", message.String())
|
||
3 years ago
|
rpcPushMsg := pbPush.PushMsgReq{OperationID: message.OperationID, MsgData: message.MsgData, PushToUserID: pushToUserID}
|
||
|
mqPushMsg := pbMsg.PushMsgDataToMQ{OperationID: message.OperationID, MsgData: message.MsgData, PushToUserID: pushToUserID}
|
||
2 years ago
|
grpcConn := rpc.GetDefaultConn(config.Config.Etcd.EtcdSchema, strings.Join(config.Config.Etcd.EtcdAddr, ","), config.Config.RpcRegisterName.OpenImPushName, message.OperationID)
|
||
2 years ago
|
if grpcConn != nil {
|
||
3 years ago
|
log.Error(rpcPushMsg.OperationID, "rpc dial failed", "push data", rpcPushMsg.String())
|
||
2 years ago
|
pid, offset, err := producer.SendMessage(&mqPushMsg, mqPushMsg.PushToUserID, rpcPushMsg.OperationID)
|
||
4 years ago
|
if err != nil {
|
||
3 years ago
|
log.Error(mqPushMsg.OperationID, "kafka send failed", "send data", message.String(), "pid", pid, "offset", offset, "err", err.Error())
|
||
4 years ago
|
}
|
||
|
return
|
||
|
}
|
||
|
msgClient := pbPush.NewPushMsgServiceClient(grpcConn)
|
||
3 years ago
|
_, err := msgClient.PushMsg(context.Background(), &rpcPushMsg)
|
||
4 years ago
|
if err != nil {
|
||
3 years ago
|
log.Error(rpcPushMsg.OperationID, "rpc send failed", rpcPushMsg.OperationID, "push data", rpcPushMsg.String(), "err", err.Error())
|
||
2 years ago
|
pid, offset, err := producer.SendMessage(&mqPushMsg, mqPushMsg.PushToUserID, rpcPushMsg.OperationID)
|
||
4 years ago
|
if err != nil {
|
||
3 years ago
|
log.Error(message.OperationID, "kafka send failed", mqPushMsg.OperationID, "send data", mqPushMsg.String(), "pid", pid, "offset", offset, "err", err.Error())
|
||
4 years ago
|
}
|
||
|
} else {
|
||
3 years ago
|
log.Info(message.OperationID, "rpc send success", rpcPushMsg.OperationID, "push data", rpcPushMsg.String())
|
||
4 years ago
|
|
||
|
}
|
||
|
}
|
||
3 years ago
|
|
||
2 years ago
|
func sendMessageToPushMQ(message *pbMsg.MsgDataToMQ, pushToUserID string) {
|
||
|
log.Info(message.OperationID, utils.GetSelfFuncName(), "msg ", message.String(), pushToUserID)
|
||
|
rpcPushMsg := pbPush.PushMsgReq{OperationID: message.OperationID, MsgData: message.MsgData, PushToUserID: pushToUserID}
|
||
|
mqPushMsg := pbMsg.PushMsgDataToMQ{OperationID: message.OperationID, MsgData: message.MsgData, PushToUserID: pushToUserID}
|
||
|
pid, offset, err := producer.SendMessage(&mqPushMsg, mqPushMsg.PushToUserID, rpcPushMsg.OperationID)
|
||
|
if err != nil {
|
||
|
log.Error(mqPushMsg.OperationID, "kafka send failed", "send data", message.String(), "pid", pid, "offset", offset, "err", err.Error())
|
||
|
}
|
||
|
return
|
||
|
}
|
||
|
|
||
2 years ago
|
func sendMessageToModifyMQ(aggregationID string, triggerID string, messages []*pbMsg.MsgDataToMQ) {
|
||
|
if len(messages) > 0 {
|
||
|
pid, offset, err := producerToModify.SendMessage(&pbMsg.MsgDataToModifyByMQ{AggregationID: aggregationID, MessageList: messages, TriggerID: triggerID}, aggregationID, triggerID)
|
||
|
if err != nil {
|
||
|
log.Error(triggerID, "kafka send failed", "send data", len(messages), "pid", pid, "offset", offset, "err", err.Error(), "key", aggregationID)
|
||
|
} else {
|
||
|
// log.NewWarn(m.OperationID, "sendMsgToKafka client msgID ", m.MsgData.ClientMsgID)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
// String hashes a string to a unique hashcode.
|
||
|
//
|
||
|
// crc32 returns a uint32, but for our use we need
|
||
|
// and non negative integer. Here we cast to an integer
|
||
|
// and invert it if the result is negative.
|
||
|
func getHashCode(s string) uint32 {
|
||
|
return crc32.ChecksumIEEE([]byte(s))
|
||
|
}
|