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.

107 lines
3.4 KiB

1 year ago
1 year ago
  1. package sub_client
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/glog"
  4. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  6. "sync"
  7. "time"
  8. )
  9. type ProcessorState struct {
  10. stopCh chan struct{}
  11. }
  12. // Subscribe subscribes to a topic's specified partitions.
  13. // If a partition is moved to another broker, the subscriber will automatically reconnect to the new broker.
  14. func (sub *TopicSubscriber) Subscribe() error {
  15. go sub.startProcessors()
  16. // loop forever
  17. sub.doKeepConnectedToSubCoordinator()
  18. return nil
  19. }
  20. func (sub *TopicSubscriber) startProcessors() {
  21. // listen to the messages from the sub coordinator
  22. // start one processor per partition
  23. var wg sync.WaitGroup
  24. semaphore := make(chan struct{}, sub.SubscriberConfig.MaxPartitionCount)
  25. for message := range sub.brokerPartitionAssignmentChan {
  26. if assigned := message.GetAssignment(); assigned != nil {
  27. wg.Add(1)
  28. semaphore <- struct{}{}
  29. topicPartition := topic.FromPbPartition(assigned.PartitionAssignment.Partition)
  30. // wait until no covering partition is still in progress
  31. sub.waitUntilNoOverlappingPartitionInFlight(topicPartition)
  32. // start a processors
  33. stopChan := make(chan struct{})
  34. sub.activeProcessorsLock.Lock()
  35. sub.activeProcessors[topicPartition] = &ProcessorState{
  36. stopCh: stopChan,
  37. }
  38. sub.activeProcessorsLock.Unlock()
  39. go func(assigned *mq_pb.BrokerPartitionAssignment, topicPartition topic.Partition) {
  40. defer func() {
  41. sub.activeProcessorsLock.Lock()
  42. delete(sub.activeProcessors, topicPartition)
  43. sub.activeProcessorsLock.Unlock()
  44. <-semaphore
  45. wg.Done()
  46. }()
  47. glog.V(0).Infof("subscriber %s/%s assigned partition %+v at %v", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker)
  48. err := sub.onEachPartition(assigned, stopChan)
  49. if err != nil {
  50. glog.V(0).Infof("subscriber %s/%s partition %+v at %v: %v", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker, err)
  51. } else {
  52. glog.V(0).Infof("subscriber %s/%s partition %+v at %v completed", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker)
  53. }
  54. }(assigned.PartitionAssignment, topicPartition)
  55. }
  56. if unAssignment := message.GetUnAssignment(); unAssignment != nil {
  57. topicPartition := topic.FromPbPartition(unAssignment.Partition)
  58. sub.activeProcessorsLock.Lock()
  59. if processor, found := sub.activeProcessors[topicPartition]; found {
  60. close(processor.stopCh)
  61. delete(sub.activeProcessors, topicPartition)
  62. }
  63. sub.activeProcessorsLock.Unlock()
  64. }
  65. }
  66. wg.Wait()
  67. }
  68. func (sub *TopicSubscriber) waitUntilNoOverlappingPartitionInFlight(topicPartition topic.Partition) {
  69. foundOverlapping := true
  70. for foundOverlapping {
  71. sub.activeProcessorsLock.Lock()
  72. foundOverlapping = false
  73. var overlappedPartition topic.Partition
  74. for partition, _ := range sub.activeProcessors {
  75. if partition.Overlaps(topicPartition) {
  76. if partition.Equals(topicPartition) {
  77. continue
  78. }
  79. foundOverlapping = true
  80. overlappedPartition = partition
  81. break
  82. }
  83. }
  84. sub.activeProcessorsLock.Unlock()
  85. if foundOverlapping {
  86. glog.V(0).Infof("subscriber %s new partition %v waiting for partition %+v to complete", sub.ContentConfig.Topic, topicPartition, overlappedPartition)
  87. time.Sleep(1 * time.Second)
  88. }
  89. }
  90. }