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.

140 lines
4.1 KiB

10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
1 week ago
10 months ago
10 months ago
10 months ago
7 months ago
1 week ago
10 months ago
  1. package sub_client
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "github.com/seaweedfs/seaweedfs/weed/glog"
  7. "github.com/seaweedfs/seaweedfs/weed/pb"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  10. "io"
  11. )
  12. type KeyedOffset struct {
  13. Key []byte
  14. Offset int64
  15. }
  16. func (sub *TopicSubscriber) onEachPartition(assigned *mq_pb.BrokerPartitionAssignment, stopCh chan struct{}, onDataMessageFn OnDataMessageFn) error {
  17. // connect to the partition broker
  18. return pb.WithBrokerGrpcClient(true, assigned.LeaderBroker, sub.SubscriberConfig.GrpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
  19. subscribeClient, err := client.SubscribeMessage(context.Background())
  20. if err != nil {
  21. return fmt.Errorf("create subscribe client: %v", err)
  22. }
  23. slidingWindowSize := sub.SubscriberConfig.SlidingWindowSize
  24. if slidingWindowSize <= 0 {
  25. slidingWindowSize = 1
  26. }
  27. po := findPartitionOffset(sub.ContentConfig.PartitionOffsets, assigned.Partition)
  28. if po == nil {
  29. po = &schema_pb.PartitionOffset{
  30. Partition: assigned.Partition,
  31. StartType: schema_pb.PartitionOffsetStartType_EARLIEST_IN_MEMORY,
  32. }
  33. }
  34. if err = subscribeClient.Send(&mq_pb.SubscribeMessageRequest{
  35. Message: &mq_pb.SubscribeMessageRequest_Init{
  36. Init: &mq_pb.SubscribeMessageRequest_InitMessage{
  37. ConsumerGroup: sub.SubscriberConfig.ConsumerGroup,
  38. ConsumerId: sub.SubscriberConfig.ConsumerGroupInstanceId,
  39. Topic: sub.ContentConfig.Topic.ToPbTopic(),
  40. PartitionOffset: po,
  41. Filter: sub.ContentConfig.Filter,
  42. FollowerBroker: assigned.FollowerBroker,
  43. SlidingWindowSize: slidingWindowSize,
  44. },
  45. },
  46. }); err != nil {
  47. glog.V(0).Infof("subscriber %s connected to partition %+v at %v: %v", sub.ContentConfig.Topic, assigned.Partition, assigned.LeaderBroker, err)
  48. }
  49. glog.V(0).Infof("subscriber %s connected to partition %+v at %v", sub.ContentConfig.Topic, assigned.Partition, assigned.LeaderBroker)
  50. if sub.OnCompletionFunc != nil {
  51. defer sub.OnCompletionFunc()
  52. }
  53. go func() {
  54. for {
  55. select {
  56. case <-sub.ctx.Done():
  57. subscribeClient.CloseSend()
  58. return
  59. case <-stopCh:
  60. subscribeClient.CloseSend()
  61. return
  62. case ack, ok := <-sub.PartitionOffsetChan:
  63. if !ok {
  64. subscribeClient.CloseSend()
  65. return
  66. }
  67. subscribeClient.SendMsg(&mq_pb.SubscribeMessageRequest{
  68. Message: &mq_pb.SubscribeMessageRequest_Ack{
  69. Ack: &mq_pb.SubscribeMessageRequest_AckMessage{
  70. Key: ack.Key,
  71. Sequence: ack.Offset,
  72. },
  73. },
  74. })
  75. }
  76. }
  77. }()
  78. for {
  79. // glog.V(0).Infof("subscriber %s/%s waiting for message", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup)
  80. resp, err := subscribeClient.Recv()
  81. if err != nil {
  82. if errors.Is(err, io.EOF) {
  83. return nil
  84. }
  85. return fmt.Errorf("subscribe recv: %v", err)
  86. }
  87. if resp.Message == nil {
  88. glog.V(0).Infof("subscriber %s/%s received nil message", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup)
  89. continue
  90. }
  91. select {
  92. case <-sub.ctx.Done():
  93. return nil
  94. case <-stopCh:
  95. return nil
  96. default:
  97. }
  98. switch m := resp.Message.(type) {
  99. case *mq_pb.SubscribeMessageResponse_Data:
  100. if m.Data.Ctrl != nil {
  101. glog.V(2).Infof("subscriber %s received control from producer:%s isClose:%v", sub.SubscriberConfig.ConsumerGroup, m.Data.Ctrl.PublisherName, m.Data.Ctrl.IsClose)
  102. continue
  103. }
  104. if len(m.Data.Key) == 0 {
  105. // fmt.Printf("empty key %+v, type %v\n", m, reflect.TypeOf(m))
  106. continue
  107. }
  108. onDataMessageFn(m)
  109. case *mq_pb.SubscribeMessageResponse_Ctrl:
  110. // glog.V(0).Infof("subscriber %s/%s/%s received control %+v", sub.ContentConfig.Namespace, sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, m.Ctrl)
  111. if m.Ctrl.IsEndOfStream || m.Ctrl.IsEndOfTopic {
  112. return io.EOF
  113. }
  114. }
  115. }
  116. })
  117. }
  118. func findPartitionOffset(partitionOffsets []*schema_pb.PartitionOffset, partition *schema_pb.Partition) *schema_pb.PartitionOffset {
  119. for _, po := range partitionOffsets {
  120. if po.Partition == partition {
  121. return po
  122. }
  123. }
  124. return nil
  125. }