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.

126 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
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. "time"
  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. StartTsNs: time.Now().UnixNano(),
  32. StartType: schema_pb.PartitionOffsetStartType_EARLIEST_IN_MEMORY,
  33. }
  34. }
  35. if err = subscribeClient.Send(&mq_pb.SubscribeMessageRequest{
  36. Message: &mq_pb.SubscribeMessageRequest_Init{
  37. Init: &mq_pb.SubscribeMessageRequest_InitMessage{
  38. ConsumerGroup: sub.SubscriberConfig.ConsumerGroup,
  39. ConsumerId: sub.SubscriberConfig.ConsumerGroupInstanceId,
  40. Topic: sub.ContentConfig.Topic.ToPbTopic(),
  41. PartitionOffset: po,
  42. Filter: sub.ContentConfig.Filter,
  43. FollowerBroker: assigned.FollowerBroker,
  44. SlidingWindowSize: slidingWindowSize,
  45. },
  46. },
  47. }); err != nil {
  48. glog.V(0).Infof("subscriber %s connected to partition %+v at %v: %v", sub.ContentConfig.Topic, assigned.Partition, assigned.LeaderBroker, err)
  49. }
  50. glog.V(0).Infof("subscriber %s connected to partition %+v at %v", sub.ContentConfig.Topic, assigned.Partition, assigned.LeaderBroker)
  51. if sub.OnCompletionFunc != nil {
  52. defer sub.OnCompletionFunc()
  53. }
  54. go func() {
  55. for {
  56. select {
  57. case <-stopCh:
  58. subscribeClient.CloseSend()
  59. return
  60. case ack, ok := <-sub.PartitionOffsetChan:
  61. if !ok {
  62. subscribeClient.CloseSend()
  63. return
  64. }
  65. subscribeClient.SendMsg(&mq_pb.SubscribeMessageRequest{
  66. Message: &mq_pb.SubscribeMessageRequest_Ack{
  67. Ack: &mq_pb.SubscribeMessageRequest_AckMessage{
  68. Key: ack.Key,
  69. Sequence: ack.Offset,
  70. },
  71. },
  72. })
  73. }
  74. }
  75. }()
  76. for {
  77. // glog.V(0).Infof("subscriber %s/%s waiting for message", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup)
  78. resp, err := subscribeClient.Recv()
  79. if err != nil {
  80. return fmt.Errorf("subscribe recv: %v", err)
  81. }
  82. if resp.Message == nil {
  83. glog.V(0).Infof("subscriber %s/%s received nil message", sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup)
  84. continue
  85. }
  86. switch m := resp.Message.(type) {
  87. case *mq_pb.SubscribeMessageResponse_Data:
  88. if m.Data.Ctrl != nil {
  89. glog.V(2).Infof("subscriber %s received control from producer:%s isClose:%v", sub.SubscriberConfig.ConsumerGroup, m.Data.Ctrl.PublisherName, m.Data.Ctrl.IsClose)
  90. continue
  91. }
  92. if len(m.Data.Key) == 0 {
  93. // fmt.Printf("empty key %+v, type %v\n", m, reflect.TypeOf(m))
  94. continue
  95. }
  96. onDataMessageFn(m)
  97. case *mq_pb.SubscribeMessageResponse_Ctrl:
  98. // glog.V(0).Infof("subscriber %s/%s/%s received control %+v", sub.ContentConfig.Namespace, sub.ContentConfig.Topic, sub.SubscriberConfig.ConsumerGroup, m.Ctrl)
  99. if m.Ctrl.IsEndOfStream || m.Ctrl.IsEndOfTopic {
  100. return io.EOF
  101. }
  102. }
  103. }
  104. })
  105. }
  106. func findPartitionOffset(partitionOffsets []*schema_pb.PartitionOffset, partition *schema_pb.Partition) *schema_pb.PartitionOffset {
  107. for _, po := range partitionOffsets {
  108. if po.Partition == partition {
  109. return po
  110. }
  111. }
  112. return nil
  113. }