Browse Source

client side stop partition subscribing if unassigned

pull/5890/head
chrislu 7 months ago
parent
commit
fa98ecf71e
  1. 9
      weed/mq/client/sub_client/on_each_partition.go
  2. 16
      weed/mq/client/sub_client/subscribe.go

9
weed/mq/client/sub_client/on_each_partition.go

@ -10,7 +10,7 @@ import (
"io" "io"
) )
func (sub *TopicSubscriber) onEachPartition(assigned *mq_pb.BrokerPartitionAssignment) error {
func (sub *TopicSubscriber) onEachPartition(assigned *mq_pb.BrokerPartitionAssignment, stopCh chan struct{}) error {
// connect to the partition broker // connect to the partition broker
return pb.WithBrokerGrpcClient(true, assigned.LeaderBroker, sub.SubscriberConfig.GrpcDialOption, func(client mq_pb.SeaweedMessagingClient) error { return pb.WithBrokerGrpcClient(true, assigned.LeaderBroker, sub.SubscriberConfig.GrpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
@ -61,7 +61,11 @@ func (sub *TopicSubscriber) onEachPartition(assigned *mq_pb.BrokerPartitionAssig
executors := util.NewLimitedConcurrentExecutor(int(perPartitionConcurrency)) executors := util.NewLimitedConcurrentExecutor(int(perPartitionConcurrency))
go func() { go func() {
for ack := range partitionOffsetChan {
for {
select {
case <-stopCh:
break
case ack := <- partitionOffsetChan:
subscribeClient.SendMsg(&mq_pb.SubscribeMessageRequest{ subscribeClient.SendMsg(&mq_pb.SubscribeMessageRequest{
Message: &mq_pb.SubscribeMessageRequest_Ack{ Message: &mq_pb.SubscribeMessageRequest_Ack{
Ack: &mq_pb.SubscribeMessageRequest_AckMessage{ Ack: &mq_pb.SubscribeMessageRequest_AckMessage{
@ -71,6 +75,7 @@ func (sub *TopicSubscriber) onEachPartition(assigned *mq_pb.BrokerPartitionAssig
}, },
}) })
} }
}
subscribeClient.CloseSend() subscribeClient.CloseSend()
}() }()

16
weed/mq/client/sub_client/subscribe.go

@ -9,6 +9,7 @@ import (
) )
type ProcessorState struct { type ProcessorState struct {
stopCh chan struct{}
} }
// Subscribe subscribes to a topic's specified partitions. // Subscribe subscribes to a topic's specified partitions.
@ -41,8 +42,11 @@ func (sub *TopicSubscriber) startProcessors() {
sub.waitUntilNoOverlappingPartitionInFlight(topicPartition) sub.waitUntilNoOverlappingPartitionInFlight(topicPartition)
// start a processors // start a processors
stopChan := make(chan struct{})
sub.activeProcessorsLock.Lock() sub.activeProcessorsLock.Lock()
sub.activeProcessors[topicPartition] = &ProcessorState{}
sub.activeProcessors[topicPartition] = &ProcessorState{
stopCh: stopChan,
}
sub.activeProcessorsLock.Unlock() sub.activeProcessorsLock.Unlock()
go func(assigned *mq_pb.BrokerPartitionAssignment, topicPartition topic.Partition) { go func(assigned *mq_pb.BrokerPartitionAssignment, topicPartition topic.Partition) {
@ -55,7 +59,7 @@ func (sub *TopicSubscriber) startProcessors() {
wg.Done() wg.Done()
}() }()
glog.V(0).Infof("subscriber %s/%s assigned partition %+v at %v", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker) glog.V(0).Infof("subscriber %s/%s assigned partition %+v at %v", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker)
err := sub.onEachPartition(assigned)
err := sub.onEachPartition(assigned, stopChan)
if err != nil { if err != nil {
glog.V(0).Infof("subscriber %s/%s partition %+v at %v: %v", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker, err) glog.V(0).Infof("subscriber %s/%s partition %+v at %v: %v", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker, err)
} else { } else {
@ -64,7 +68,13 @@ func (sub *TopicSubscriber) startProcessors() {
}(assigned.PartitionAssignment, topicPartition) }(assigned.PartitionAssignment, topicPartition)
} }
if unAssignment := message.GetUnAssignment(); unAssignment != nil { if unAssignment := message.GetUnAssignment(); unAssignment != nil {
topicPartition := topic.FromPbPartition(unAssignment.Partition)
sub.activeProcessorsLock.Lock()
if processor, found := sub.activeProcessors[topicPartition]; found {
close(processor.stopCh)
delete(sub.activeProcessors, topicPartition)
}
sub.activeProcessorsLock.Unlock()
} }
} }

Loading…
Cancel
Save