pull/232/head
Gordon 3 years ago
parent 3bc2e97e8b
commit 35958ca3a2

@ -10,8 +10,8 @@ import (
"sync" "sync"
) )
const OnlineTopicBusy = "Busy" const OnlineTopicBusy = 1
const OnlineTopicVacancy = "Vacancy" const OnlineTopicVacancy = 0
var ( var (
persistentCH PersistentConsumerHandler persistentCH PersistentConsumerHandler
@ -19,7 +19,7 @@ var (
offlineHistoryCH OfflineHistoryConsumerHandler offlineHistoryCH OfflineHistoryConsumerHandler
producer *kafka.Producer producer *kafka.Producer
cmdCh chan Cmd2Value cmdCh chan Cmd2Value
onlineTopicStatus string onlineTopicStatus int
w *sync.Mutex w *sync.Mutex
singleMsgSuccessCount uint64 singleMsgSuccessCount uint64
groupMsgCount uint64 groupMsgCount uint64
@ -31,6 +31,7 @@ func Init() {
w = new(sync.Mutex) w = new(sync.Mutex)
persistentCH.Init() persistentCH.Init()
historyCH.Init(cmdCh) historyCH.Init(cmdCh)
onlineTopicStatus = OnlineTopicVacancy
log.Debug("come msg transfer ts", config.Config.Kafka.ConsumerGroupID.MsgToMongoOffline, config.Config.Kafka.Ws2mschatOffline.Topic) log.Debug("come msg transfer ts", config.Config.Kafka.ConsumerGroupID.MsgToMongoOffline, config.Config.Kafka.Ws2mschatOffline.Topic)
offlineHistoryCH.Init(cmdCh) offlineHistoryCH.Init(cmdCh)
statistics.NewStatistics(&singleMsgSuccessCount, config.Config.ModuleName.MsgTransferName, fmt.Sprintf("%d second singleMsgCount insert to mongo", constant.StatisticsTimeInterval), constant.StatisticsTimeInterval) statistics.NewStatistics(&singleMsgSuccessCount, config.Config.ModuleName.MsgTransferName, fmt.Sprintf("%d second singleMsgCount insert to mongo", constant.StatisticsTimeInterval), constant.StatisticsTimeInterval)
@ -47,12 +48,12 @@ func Run() {
go historyCH.historyConsumerGroup.RegisterHandleAndConsumer(&historyCH) go historyCH.historyConsumerGroup.RegisterHandleAndConsumer(&historyCH)
go offlineHistoryCH.historyConsumerGroup.RegisterHandleAndConsumer(&offlineHistoryCH) go offlineHistoryCH.historyConsumerGroup.RegisterHandleAndConsumer(&offlineHistoryCH)
} }
func SetOnlineTopicStatus(status string) { func SetOnlineTopicStatus(status int) {
w.Lock() w.Lock()
defer w.Unlock() defer w.Unlock()
onlineTopicStatus = status onlineTopicStatus = status
} }
func GetOnlineTopicStatus() string { func GetOnlineTopicStatus() int {
w.Lock() w.Lock()
defer w.Unlock() defer w.Unlock()
return onlineTopicStatus return onlineTopicStatus

@ -103,23 +103,20 @@ func (mc *OfflineHistoryConsumerHandler) ConsumeClaim(sess sarama.ConsumerGroupS
// log.NewDebug("", "kafka get info to delay mongo", "msgTopic", msg.Topic, "msgPartition", msg.Partition, "offline") // log.NewDebug("", "kafka get info to delay mongo", "msgTopic", msg.Topic, "msgPartition", msg.Partition, "offline")
// //mc.msgHandle[msg.Topic](msg.Value, string(msg.Key)) // //mc.msgHandle[msg.Topic](msg.Value, string(msg.Key))
//} //}
cmd := Cmd2Value{}
repeat:
select {
case cmd = <-mc.cmdCh:
case <-time.After(time.Millisecond * time.Duration(1)):
goto repeat
}
if cmd.Cmd == OnlineTopicVacancy {
for msg := range claim.Messages() { for msg := range claim.Messages() {
if GetOnlineTopicStatus() == OnlineTopicVacancy { if GetOnlineTopicStatus() == OnlineTopicVacancy {
log.NewDebug("", "kafka get info to mongo", "msgTopic", msg.Topic, "msgPartition", msg.Partition, "msg", string(msg.Value)) log.NewDebug("", "kafka get info to mongo", "msgTopic", msg.Topic, "msgPartition", msg.Partition, "msg", string(msg.Value))
mc.msgHandle[msg.Topic](msg.Value, string(msg.Key)) mc.msgHandle[msg.Topic](msg.Value, string(msg.Key))
sess.MarkMessage(msg, "") sess.MarkMessage(msg, "")
} else { } else {
goto repeat select {
case <-mc.cmdCh:
case <-time.After(time.Millisecond * time.Duration(100)):
} }
mc.msgHandle[msg.Topic](msg.Value, string(msg.Key))
sess.MarkMessage(msg, "")
} }
} }
return nil return nil
} }

@ -19,7 +19,7 @@ import (
type fcb func(msg []byte, msgKey string) type fcb func(msg []byte, msgKey string)
type Cmd2Value struct { type Cmd2Value struct {
Cmd string Cmd int
Value interface{} Value interface{}
} }
type OnlineHistoryConsumerHandler struct { type OnlineHistoryConsumerHandler struct {
@ -37,7 +37,7 @@ func (och *OnlineHistoryConsumerHandler) Init(cmdCh chan Cmd2Value) {
config.Config.Kafka.Ws2mschat.Addr, config.Config.Kafka.ConsumerGroupID.MsgToMongo) config.Config.Kafka.Ws2mschat.Addr, config.Config.Kafka.ConsumerGroupID.MsgToMongo)
} }
func (och *OnlineHistoryConsumerHandler) TriggerCmd(status string) { func (och *OnlineHistoryConsumerHandler) TriggerCmd(status int) {
operationID := utils.OperationIDGenerator() operationID := utils.OperationIDGenerator()
for { for {
err := sendCmd(och.cmdCh, Cmd2Value{Cmd: status, Value: ""}, 1) err := sendCmd(och.cmdCh, Cmd2Value{Cmd: status, Value: ""}, 1)
@ -135,15 +135,15 @@ func (och *OnlineHistoryConsumerHandler) ConsumeClaim(sess sarama.ConsumerGroupS
claim sarama.ConsumerGroupClaim) error { // a instance in the consumer group claim sarama.ConsumerGroupClaim) error { // a instance in the consumer group
log.NewDebug("", "online new session msg come", claim.HighWaterMarkOffset(), claim.Topic(), claim.Partition()) log.NewDebug("", "online new session msg come", claim.HighWaterMarkOffset(), claim.Topic(), claim.Partition())
for msg := range claim.Messages() { for msg := range claim.Messages() {
och.TriggerCmd(OnlineTopicBusy)
SetOnlineTopicStatus(OnlineTopicBusy) SetOnlineTopicStatus(OnlineTopicBusy)
log.NewDebug("", "kafka get info to mongo", "msgTopic", msg.Topic, "msgPartition", msg.Partition, "online") //och.TriggerCmd(OnlineTopicBusy)
log.NewDebug("", "kafka get info to mongo", "msgTopic", msg.Topic, "msgPartition", msg.Partition, "online", msg.Offset, claim.HighWaterMarkOffset())
och.msgHandle[msg.Topic](msg.Value, string(msg.Key)) och.msgHandle[msg.Topic](msg.Value, string(msg.Key))
sess.MarkMessage(msg, "") sess.MarkMessage(msg, "")
if claim.HighWaterMarkOffset()-msg.Offset <= 1 { if claim.HighWaterMarkOffset()-msg.Offset <= 1 {
log.Debug("", "online msg consume end", claim.HighWaterMarkOffset(), msg.Offset) log.Debug("", "online msg consume end", claim.HighWaterMarkOffset(), msg.Offset)
och.TriggerCmd(OnlineTopicVacancy)
SetOnlineTopicStatus(OnlineTopicVacancy) SetOnlineTopicStatus(OnlineTopicVacancy)
och.TriggerCmd(OnlineTopicVacancy)
} }
} }
return nil return nil

Loading…
Cancel
Save