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.

93 lines
2.8 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. }
  11. // Subscribe subscribes to a topic's specified partitions.
  12. // If a partition is moved to another broker, the subscriber will automatically reconnect to the new broker.
  13. func (sub *TopicSubscriber) Subscribe() error {
  14. go sub.startProcessors()
  15. // loop forever
  16. sub.doKeepConnectedToSubCoordinator()
  17. return nil
  18. }
  19. func (sub *TopicSubscriber) startProcessors() {
  20. // listen to the messages from the sub coordinator
  21. // start one processor per partition
  22. var wg sync.WaitGroup
  23. semaphore := make(chan struct{}, sub.ProcessorConfig.ConcurrentPartitionLimit)
  24. for assigned := range sub.brokerPartitionAssignmentChan {
  25. wg.Add(1)
  26. semaphore <- struct{}{}
  27. topicPartition := topic.FromPbPartition(assigned.Partition)
  28. // wait until no covering partition is still in progress
  29. sub.waitUntilNoOverlappingPartitionInFlight(topicPartition)
  30. // start a processors
  31. sub.activeProcessorsLock.Lock()
  32. sub.activeProcessors[topicPartition] = &ProcessorState{}
  33. sub.activeProcessorsLock.Unlock()
  34. go func(assigned *mq_pb.BrokerPartitionAssignment, topicPartition topic.Partition) {
  35. defer func() {
  36. sub.activeProcessorsLock.Lock()
  37. delete(sub.activeProcessors, topicPartition)
  38. sub.activeProcessorsLock.Unlock()
  39. <-semaphore
  40. wg.Done()
  41. }()
  42. glog.V(0).Infof("subscriber %s/%s assigned partition %+v at %v", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker)
  43. err := sub.onEachPartition(assigned)
  44. if err != nil {
  45. glog.V(0).Infof("subscriber %s/%s partition %+v at %v: %v", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker, err)
  46. } else {
  47. glog.V(0).Infof("subscriber %s/%s partition %+v at %v completed", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, assigned.Partition, assigned.LeaderBroker)
  48. }
  49. }(assigned, topicPartition)
  50. }
  51. wg.Wait()
  52. }
  53. func (sub *TopicSubscriber) waitUntilNoOverlappingPartitionInFlight(topicPartition topic.Partition) {
  54. foundOverlapping := true
  55. for foundOverlapping {
  56. sub.activeProcessorsLock.Lock()
  57. foundOverlapping = false
  58. var overlappedPartition topic.Partition
  59. for partition, _ := range sub.activeProcessors {
  60. if partition.Overlaps(topicPartition) {
  61. if partition.Equals(topicPartition) {
  62. continue
  63. }
  64. foundOverlapping = true
  65. overlappedPartition = partition
  66. break
  67. }
  68. }
  69. sub.activeProcessorsLock.Unlock()
  70. if foundOverlapping {
  71. glog.V(0).Infof("subscriber %s new partition %v waiting for partition %+v to complete", sub.ContentConfig.Topic, topicPartition, overlappedPartition)
  72. time.Sleep(1 * time.Second)
  73. }
  74. }
  75. }