2023-08-29 00:02:12 +08:00
|
|
|
package sub_client
|
|
|
|
|
2024-05-15 14:22:43 +08:00
|
|
|
import (
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
"sync"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ProcessorState struct {
|
2024-05-21 23:42:04 +08:00
|
|
|
stopCh chan struct{}
|
2024-05-15 14:22:43 +08:00
|
|
|
}
|
|
|
|
|
2023-09-05 12:43:50 +08:00
|
|
|
// Subscribe subscribes to a topic's specified partitions.
|
|
|
|
// If a partition is moved to another broker, the subscriber will automatically reconnect to the new broker.
|
2023-08-29 00:02:12 +08:00
|
|
|
|
2023-09-05 12:43:50 +08:00
|
|
|
func (sub *TopicSubscriber) Subscribe() error {
|
2024-05-15 14:22:43 +08:00
|
|
|
|
|
|
|
go sub.startProcessors()
|
|
|
|
|
2023-12-29 03:56:37 +08:00
|
|
|
// loop forever
|
|
|
|
sub.doKeepConnectedToSubCoordinator()
|
|
|
|
|
2023-09-01 15:36:51 +08:00
|
|
|
return nil
|
2023-08-29 00:02:12 +08:00
|
|
|
}
|
2024-05-15 14:22:43 +08:00
|
|
|
|
|
|
|
func (sub *TopicSubscriber) startProcessors() {
|
|
|
|
// listen to the messages from the sub coordinator
|
|
|
|
// start one processor per partition
|
|
|
|
var wg sync.WaitGroup
|
2024-05-21 03:28:01 +08:00
|
|
|
semaphore := make(chan struct{}, sub.SubscriberConfig.MaxPartitionCount)
|
2024-05-15 14:22:43 +08:00
|
|
|
|
2024-05-21 23:05:56 +08:00
|
|
|
for message := range sub.brokerPartitionAssignmentChan {
|
|
|
|
if assigned := message.GetAssignment(); assigned != nil {
|
|
|
|
wg.Add(1)
|
|
|
|
semaphore <- struct{}{}
|
|
|
|
|
|
|
|
topicPartition := topic.FromPbPartition(assigned.PartitionAssignment.Partition)
|
|
|
|
|
|
|
|
// wait until no covering partition is still in progress
|
|
|
|
sub.waitUntilNoOverlappingPartitionInFlight(topicPartition)
|
|
|
|
|
|
|
|
// start a processors
|
2024-05-21 23:42:04 +08:00
|
|
|
stopChan := make(chan struct{})
|
2024-05-21 23:05:56 +08:00
|
|
|
sub.activeProcessorsLock.Lock()
|
2024-05-21 23:42:04 +08:00
|
|
|
sub.activeProcessors[topicPartition] = &ProcessorState{
|
|
|
|
stopCh: stopChan,
|
|
|
|
}
|
2024-05-21 23:05:56 +08:00
|
|
|
sub.activeProcessorsLock.Unlock()
|
|
|
|
|
|
|
|
go func(assigned *mq_pb.BrokerPartitionAssignment, topicPartition topic.Partition) {
|
|
|
|
defer func() {
|
|
|
|
sub.activeProcessorsLock.Lock()
|
|
|
|
delete(sub.activeProcessors, topicPartition)
|
|
|
|
sub.activeProcessorsLock.Unlock()
|
|
|
|
|
|
|
|
<-semaphore
|
|
|
|
wg.Done()
|
|
|
|
}()
|
|
|
|
glog.V(0).Infof("subscriber %s/%s assigned partition %+v at %v", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker)
|
2024-05-21 23:42:04 +08:00
|
|
|
err := sub.onEachPartition(assigned, stopChan)
|
2024-05-21 23:05:56 +08:00
|
|
|
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)
|
|
|
|
} else {
|
|
|
|
glog.V(0).Infof("subscriber %s/%s partition %+v at %v completed", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker)
|
|
|
|
}
|
|
|
|
}(assigned.PartitionAssignment, topicPartition)
|
|
|
|
}
|
|
|
|
if unAssignment := message.GetUnAssignment(); unAssignment != nil {
|
2024-05-21 23:42:04 +08:00
|
|
|
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()
|
2024-05-21 23:05:56 +08:00
|
|
|
}
|
2024-05-15 14:22:43 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sub *TopicSubscriber) waitUntilNoOverlappingPartitionInFlight(topicPartition topic.Partition) {
|
|
|
|
foundOverlapping := true
|
|
|
|
for foundOverlapping {
|
|
|
|
sub.activeProcessorsLock.Lock()
|
|
|
|
foundOverlapping = false
|
2024-05-17 11:28:19 +08:00
|
|
|
var overlappedPartition topic.Partition
|
2024-05-15 14:22:43 +08:00
|
|
|
for partition, _ := range sub.activeProcessors {
|
|
|
|
if partition.Overlaps(topicPartition) {
|
2024-05-17 11:28:19 +08:00
|
|
|
if partition.Equals(topicPartition) {
|
|
|
|
continue
|
|
|
|
}
|
2024-05-15 14:22:43 +08:00
|
|
|
foundOverlapping = true
|
2024-05-17 11:28:19 +08:00
|
|
|
overlappedPartition = partition
|
2024-05-15 14:22:43 +08:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
sub.activeProcessorsLock.Unlock()
|
|
|
|
if foundOverlapping {
|
2024-05-17 11:28:19 +08:00
|
|
|
glog.V(0).Infof("subscriber %s new partition %v waiting for partition %+v to complete", sub.ContentConfig.Topic, topicPartition, overlappedPartition)
|
2024-05-15 14:22:43 +08:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|