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.

69 lines
2.6 KiB

2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
2 years ago
10 months ago
10 months ago
2 years ago
2 years ago
2 years 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
2 years ago
  1. package sub_client
  2. import (
  3. "context"
  4. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  7. "google.golang.org/grpc"
  8. "sync"
  9. )
  10. type SubscriberConfiguration struct {
  11. ClientId string
  12. ConsumerGroup string
  13. ConsumerGroupInstanceId string
  14. GrpcDialOption grpc.DialOption
  15. MaxPartitionCount int32 // how many partitions to process concurrently
  16. SlidingWindowSize int32 // how many messages to process concurrently per partition
  17. }
  18. func (s *SubscriberConfiguration) String() string {
  19. return "ClientId: " + s.ClientId + ", ConsumerGroup: " + s.ConsumerGroup + ", ConsumerGroupInstanceId: " + s.ConsumerGroupInstanceId
  20. }
  21. type ContentConfiguration struct {
  22. Topic topic.Topic
  23. Filter string
  24. PartitionOffsets []*schema_pb.PartitionOffset
  25. OffsetType schema_pb.OffsetType
  26. OffsetTsNs int64
  27. }
  28. type OnDataMessageFn func(m *mq_pb.SubscribeMessageResponse_Data)
  29. type OnCompletionFunc func()
  30. type TopicSubscriber struct {
  31. ctx context.Context
  32. SubscriberConfig *SubscriberConfiguration
  33. ContentConfig *ContentConfiguration
  34. brokerPartitionAssignmentChan chan *mq_pb.SubscriberToSubCoordinatorResponse
  35. brokerPartitionAssignmentAckChan chan *mq_pb.SubscriberToSubCoordinatorRequest
  36. OnDataMessageFunc OnDataMessageFn
  37. OnCompletionFunc OnCompletionFunc
  38. bootstrapBrokers []string
  39. activeProcessors map[topic.Partition]*ProcessorState
  40. activeProcessorsLock sync.Mutex
  41. PartitionOffsetChan chan KeyedOffset
  42. }
  43. func NewTopicSubscriber(ctx context.Context, bootstrapBrokers []string, subscriber *SubscriberConfiguration, content *ContentConfiguration, partitionOffsetChan chan KeyedOffset) *TopicSubscriber {
  44. return &TopicSubscriber{
  45. ctx: ctx,
  46. SubscriberConfig: subscriber,
  47. ContentConfig: content,
  48. brokerPartitionAssignmentChan: make(chan *mq_pb.SubscriberToSubCoordinatorResponse, 1024),
  49. brokerPartitionAssignmentAckChan: make(chan *mq_pb.SubscriberToSubCoordinatorRequest, 1024),
  50. bootstrapBrokers: bootstrapBrokers,
  51. activeProcessors: make(map[topic.Partition]*ProcessorState),
  52. PartitionOffsetChan: partitionOffsetChan,
  53. }
  54. }
  55. func (sub *TopicSubscriber) SetOnDataMessageFn(fn OnDataMessageFn) {
  56. sub.OnDataMessageFunc = fn
  57. }
  58. func (sub *TopicSubscriber) SetCompletionFunc(onCompletionFn OnCompletionFunc) {
  59. sub.OnCompletionFunc = onCompletionFn
  60. }