Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bugfix rebalance is not paused after suspend and rebalance not trigge… #852

Merged
merged 1 commit into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 13 additions & 3 deletions consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,8 +221,8 @@ type PullRequest struct {
}

func (pr *PullRequest) String() string {
return fmt.Sprintf("[ConsumerGroup: %s, Topic: %s, MessageQueue: %d]",
pr.consumerGroup, pr.mq.Topic, pr.mq.QueueId)
return fmt.Sprintf("[ConsumerGroup: %s, Topic: %s, MessageQueue: brokerName=%s, queueId=%d, nextOffset=%d]",
pr.consumerGroup, pr.mq.Topic, pr.mq.BrokerName, pr.mq.QueueId, pr.nextOffset)
}

type defaultConsumer struct {
Expand Down Expand Up @@ -357,6 +357,16 @@ func (dc *defaultConsumer) isSubscribeTopicNeedUpdate(topic string) bool {
return !exist
}

func (dc *defaultConsumer) doBalanceIfNotPaused() {
if dc.pause {
rlog.Info("[BALANCE-SKIP] since consumer paused", map[string]interface{}{
rlog.LogKeyConsumerGroup: dc.consumerGroup,
})
return
}
dc.doBalance()
}

func (dc *defaultConsumer) doBalance() {
dc.subscriptionDataTable.Range(func(key, value interface{}) bool {
topic := key.(string)
Expand Down Expand Up @@ -674,7 +684,7 @@ func (dc *defaultConsumer) updateProcessQueueTable(topic string, mqs []*primitiv
if dc.removeUnnecessaryMessageQueue(&mq, pq) {
dc.processQueueTable.Delete(key)
changed = true
rlog.Debug("remove unnecessary mq because pull was paused, prepare to fix it", map[string]interface{}{
rlog.Debug("remove unnecessary mq because pull was expired, prepare to fix it", map[string]interface{}{
rlog.LogKeyConsumerGroup: dc.consumerGroup,
rlog.LogKeyMessageQueue: mq.String(),
})
Expand Down
25 changes: 14 additions & 11 deletions consumer/push_consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,10 @@ func (pc *pushConsumer) Rebalance() {
pc.defaultConsumer.doBalance()
}

func (pc *pushConsumer) RebalanceIfNotPaused() {
pc.defaultConsumer.doBalanceIfNotPaused()
}

func (pc *pushConsumer) PersistConsumerOffset() error {
return pc.defaultConsumer.persistConsumerOffset()
}
Expand Down Expand Up @@ -877,31 +881,30 @@ func (pc *pushConsumer) ResetOffset(topic string, table map[primitive.MessageQue
pc.suspend()
defer pc.resume()

mqs := make([]*primitive.MessageQueue, 0)
copyPc := sync.Map{}
pc.processQueueTable.Range(func(key, value interface{}) bool {
mq := key.(primitive.MessageQueue)
pq := value.(*processQueue)
if _, ok := table[mq]; ok && mq.Topic == topic {
pq.WithDropped(true)
pq.clear()
}
mqs = append(mqs, &mq)
copyPc.Store(&mq, pq)
return true
})
time.Sleep(10 * time.Second)
v, exist := pc.topicSubscribeInfoTable.Load(topic)
if !exist {
return
}
queuesOfTopic := v.([]*primitive.MessageQueue)
for _, k := range queuesOfTopic {
if _, ok := table[*k]; ok {
pc.storage.update(k, table[*k], false)
v, exist := pc.processQueueTable.Load(k)
for _, mq := range mqs {
if _, ok := table[*mq]; ok {
pc.storage.update(mq, table[*mq], false)
v, exist := copyPc.Load(mq)
if !exist {
continue
}
pq := v.(*processQueue)
pc.removeUnnecessaryMessageQueue(k, pq)
pc.processQueueTable.Delete(k)
pc.removeUnnecessaryMessageQueue(mq, pq)
pc.processQueueTable.Delete(mq)
}
}
}
Expand Down
15 changes: 13 additions & 2 deletions internal/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ type InnerConsumer interface {
IsSubscribeTopicNeedUpdate(topic string) bool
SubscriptionDataList() []*SubscriptionData
Rebalance()
RebalanceIfNotPaused()
IsUnitMode() bool
GetConsumerRunningInfo(stack bool) *ConsumerRunningInfo
ConsumeMessageDirectly(msg *primitive.MessageExt, brokerName string) *ConsumeMessageDirectlyResult
Expand Down Expand Up @@ -223,7 +224,7 @@ func GetOrNewRocketMQClient(option ClientOptions, callbackCh chan interface{}) R
rlog.Info("receive broker's notification to consumer group", map[string]interface{}{
rlog.LogKeyConsumerGroup: req.ExtFields["consumerGroup"],
})
client.RebalanceImmediately()
client.RebalanceIfNotPaused()
return nil
})
client.remoteClient.RegisterRequestFunc(ReqCheckTransactionState, func(req *remote.RemotingCommand, addr net.Addr) *remote.RemotingCommand {
Expand Down Expand Up @@ -492,7 +493,7 @@ func (c *rmqClient) Start() {
for {
select {
case <-ticker.C:
c.RebalanceImmediately()
c.RebalanceIfNotPaused()
case <-c.done:
rlog.Info("The RMQClient stopping do rebalance", map[string]interface{}{
"clientID": c.ClientID(),
Expand Down Expand Up @@ -820,6 +821,16 @@ func (c *rmqClient) RebalanceImmediately() {
})
}

func (c *rmqClient) RebalanceIfNotPaused() {
c.rbMutex.Lock()
defer c.rbMutex.Unlock()
c.consumerMap.Range(func(key, value interface{}) bool {
consumer := value.(InnerConsumer)
consumer.RebalanceIfNotPaused()
return true
})
}

func (c *rmqClient) UpdatePublishInfo(topic string, data *TopicRouteData, changed bool) {
if data == nil {
return
Expand Down