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.

112 lines
3.5 KiB

8 months ago
  1. package sub_client
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/pb"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/util"
  9. "io"
  10. )
  11. func (sub *TopicSubscriber) onEachPartition(assigned *mq_pb.BrokerPartitionAssignment) error {
  12. // connect to the partition broker
  13. return pb.WithBrokerGrpcClient(true, assigned.LeaderBroker, sub.SubscriberConfig.GrpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
  14. subscribeClient, err := client.SubscribeMessage(context.Background())
  15. if err != nil {
  16. return fmt.Errorf("create subscribe client: %v", err)
  17. }
  18. perPartitionConcurrency := sub.SubscriberConfig.PerPartitionConcurrency
  19. if perPartitionConcurrency <= 0 {
  20. perPartitionConcurrency = 1
  21. }
  22. if err = subscribeClient.Send(&mq_pb.SubscribeMessageRequest{
  23. Message: &mq_pb.SubscribeMessageRequest_Init{
  24. Init: &mq_pb.SubscribeMessageRequest_InitMessage{
  25. ConsumerGroup: sub.SubscriberConfig.ConsumerGroup,
  26. ConsumerId: sub.SubscriberConfig.ConsumerGroupInstanceId,
  27. Topic: sub.ContentConfig.Topic.ToPbTopic(),
  28. PartitionOffset: &mq_pb.PartitionOffset{
  29. Partition: assigned.Partition,
  30. StartType: mq_pb.PartitionOffsetStartType_EARLIEST_IN_MEMORY,
  31. },
  32. Filter: sub.ContentConfig.Filter,
  33. FollowerBroker: assigned.FollowerBroker,
  34. Concurrency: perPartitionConcurrency,
  35. },
  36. },
  37. }); err != nil {
  38. glog.V(0).Infof("subscriber %s connected to partition %+v at %v: %v", sub.ContentConfig.Topic, assigned.Partition, assigned.LeaderBroker, err)
  39. }
  40. glog.V(0).Infof("subscriber %s connected to partition %+v at %v", sub.ContentConfig.Topic, assigned.Partition, assigned.LeaderBroker)
  41. if sub.OnCompletionFunc != nil {
  42. defer sub.OnCompletionFunc()
  43. }
  44. type KeyedOffset struct {
  45. Key []byte
  46. Offset int64
  47. }
  48. partitionOffsetChan := make(chan KeyedOffset, 1024)
  49. defer func() {
  50. close(partitionOffsetChan)
  51. }()
  52. executors := util.NewLimitedConcurrentExecutor(int(perPartitionConcurrency))
  53. go func() {
  54. for ack := range partitionOffsetChan {
  55. subscribeClient.SendMsg(&mq_pb.SubscribeMessageRequest{
  56. Message: &mq_pb.SubscribeMessageRequest_Ack{
  57. Ack: &mq_pb.SubscribeMessageRequest_AckMessage{
  58. Key: ack.Key,
  59. Sequence: ack.Offset,
  60. },
  61. },
  62. })
  63. }
  64. subscribeClient.CloseSend()
  65. }()
  66. var lastErr error
  67. for lastErr == nil {
  68. // glog.V(0).Infof("subscriber %s/%s/%s waiting for message", sub.ContentConfig.Namespace, sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup)
  69. resp, err := subscribeClient.Recv()
  70. if err != nil {
  71. return fmt.Errorf("subscribe recv: %v", err)
  72. }
  73. if resp.Message == nil {
  74. glog.V(0).Infof("subscriber %s/%s received nil message", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup)
  75. continue
  76. }
  77. switch m := resp.Message.(type) {
  78. case *mq_pb.SubscribeMessageResponse_Data:
  79. executors.Execute(func() {
  80. processErr := sub.OnEachMessageFunc(m.Data.Key, m.Data.Value)
  81. if processErr == nil {
  82. partitionOffsetChan <- KeyedOffset{
  83. Key: m.Data.Key,
  84. Offset: m.Data.TsNs,
  85. }
  86. } else {
  87. lastErr = processErr
  88. }
  89. })
  90. case *mq_pb.SubscribeMessageResponse_Ctrl:
  91. // glog.V(0).Infof("subscriber %s/%s/%s received control %+v", sub.ContentConfig.Namespace, sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, m.Ctrl)
  92. if m.Ctrl.IsEndOfStream || m.Ctrl.IsEndOfTopic {
  93. return io.EOF
  94. }
  95. }
  96. }
  97. return lastErr
  98. })
  99. }