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.

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