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.

85 lines
2.4 KiB

1 year ago
Merge accumulated changes related to message queue (#5098) * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * tracking topic=>broker * merge * comment
1 year ago
1 year ago
Merge accumulated changes related to message queue (#5098) * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * tracking topic=>broker * merge * comment
1 year ago
1 year ago
  1. package sub_client
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  7. "sync"
  8. )
  9. func (sub *TopicSubscriber) doProcess() error {
  10. var wg sync.WaitGroup
  11. for _, brokerPartitionAssignment := range sub.brokerPartitionAssignments {
  12. brokerAddress := brokerPartitionAssignment.LeaderBroker
  13. grpcConnection, err := pb.GrpcDial(context.Background(), brokerAddress, true, sub.SubscriberConfig.GrpcDialOption)
  14. if err != nil {
  15. return fmt.Errorf("dial broker %s: %v", brokerAddress, err)
  16. }
  17. brokerClient := mq_pb.NewSeaweedMessagingClient(grpcConnection)
  18. subscribeClient, err := brokerClient.Subscribe(context.Background(), &mq_pb.SubscribeRequest{
  19. Message: &mq_pb.SubscribeRequest_Init{
  20. Init: &mq_pb.SubscribeRequest_InitMessage{
  21. ConsumerGroup: sub.SubscriberConfig.GroupId,
  22. ConsumerId: sub.SubscriberConfig.GroupInstanceId,
  23. Topic: &mq_pb.Topic{
  24. Namespace: sub.ContentConfig.Namespace,
  25. Name: sub.ContentConfig.Topic,
  26. },
  27. Partition: &mq_pb.Partition{
  28. RingSize: brokerPartitionAssignment.Partition.RingSize,
  29. RangeStart: brokerPartitionAssignment.Partition.RangeStart,
  30. RangeStop: brokerPartitionAssignment.Partition.RangeStop,
  31. },
  32. Filter: sub.ContentConfig.Filter,
  33. Offset: &mq_pb.SubscribeRequest_InitMessage_StartTimestampNs{
  34. StartTimestampNs: sub.alreadyProcessedTsNs,
  35. },
  36. },
  37. },
  38. })
  39. if err != nil {
  40. return fmt.Errorf("create subscribe client: %v", err)
  41. }
  42. wg.Add(1)
  43. go func() {
  44. defer wg.Done()
  45. if sub.OnCompletionFunc != nil {
  46. defer sub.OnCompletionFunc()
  47. }
  48. defer func() {
  49. subscribeClient.SendMsg(&mq_pb.SubscribeRequest{
  50. Message: &mq_pb.SubscribeRequest_Ack{
  51. Ack: &mq_pb.SubscribeRequest_AckMessage{
  52. Sequence: 0,
  53. },
  54. },
  55. })
  56. subscribeClient.CloseSend()
  57. }()
  58. for {
  59. resp, err := subscribeClient.Recv()
  60. if err != nil {
  61. fmt.Printf("subscribe error: %v\n", err)
  62. return
  63. }
  64. if resp.Message == nil {
  65. continue
  66. }
  67. switch m := resp.Message.(type) {
  68. case *mq_pb.SubscribeResponse_Data:
  69. if !sub.OnEachMessageFunc(m.Data.Key, m.Data.Value) {
  70. return
  71. }
  72. sub.alreadyProcessedTsNs = m.Data.TsNs
  73. case *mq_pb.SubscribeResponse_Ctrl:
  74. if m.Ctrl.IsEndOfStream || m.Ctrl.IsEndOfTopic {
  75. return
  76. }
  77. }
  78. }
  79. }()
  80. }
  81. wg.Wait()
  82. return nil
  83. }