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.

117 lines
3.5 KiB

1 year ago
9 months 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
9 months ago
9 months 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 year ago
1 year ago
10 months ago
1 year ago
9 months ago
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. package topic
  2. import (
  3. cmap "github.com/orcaman/concurrent-map/v2"
  4. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  5. "github.com/shirou/gopsutil/v3/cpu"
  6. "time"
  7. )
  8. // LocalTopicManager manages topics on local broker
  9. type LocalTopicManager struct {
  10. topics cmap.ConcurrentMap[string, *LocalTopic]
  11. }
  12. // NewLocalTopicManager creates a new LocalTopicManager
  13. func NewLocalTopicManager() *LocalTopicManager {
  14. return &LocalTopicManager{
  15. topics: cmap.New[*LocalTopic](),
  16. }
  17. }
  18. // AddLocalPartition adds a topic to the local topic manager
  19. func (manager *LocalTopicManager) AddLocalPartition(topic Topic, localPartition *LocalPartition) {
  20. localTopic, ok := manager.topics.Get(topic.String())
  21. if !ok {
  22. localTopic = NewLocalTopic(topic)
  23. }
  24. if !manager.topics.SetIfAbsent(topic.String(), localTopic) {
  25. localTopic, _ = manager.topics.Get(topic.String())
  26. }
  27. if localTopic.findPartition(localPartition.Partition) != nil {
  28. return
  29. }
  30. localTopic.Partitions = append(localTopic.Partitions, localPartition)
  31. }
  32. // GetLocalPartition gets a topic from the local topic manager
  33. func (manager *LocalTopicManager) GetLocalPartition(topic Topic, partition Partition) *LocalPartition {
  34. localTopic, ok := manager.topics.Get(topic.String())
  35. if !ok {
  36. return nil
  37. }
  38. return localTopic.findPartition(partition)
  39. }
  40. // RemoveTopic removes a topic from the local topic manager
  41. func (manager *LocalTopicManager) RemoveTopic(topic Topic) {
  42. manager.topics.Remove(topic.String())
  43. }
  44. func (manager *LocalTopicManager) RemoveLocalPartition(topic Topic, partition Partition) (removed bool) {
  45. localTopic, ok := manager.topics.Get(topic.String())
  46. if !ok {
  47. return false
  48. }
  49. return localTopic.removePartition(partition)
  50. }
  51. func (manager *LocalTopicManager) ClosePublishers(topic Topic, unixTsNs int64) (removed bool) {
  52. localTopic, ok := manager.topics.Get(topic.String())
  53. if !ok {
  54. return false
  55. }
  56. return localTopic.closePartitionPublishers(unixTsNs)
  57. }
  58. func (manager *LocalTopicManager) CloseSubscribers(topic Topic, unixTsNs int64) (removed bool) {
  59. localTopic, ok := manager.topics.Get(topic.String())
  60. if !ok {
  61. return false
  62. }
  63. return localTopic.closePartitionSubscribers(unixTsNs)
  64. }
  65. func (manager *LocalTopicManager) CollectStats(duration time.Duration) *mq_pb.BrokerStats {
  66. stats := &mq_pb.BrokerStats{
  67. Stats: make(map[string]*mq_pb.TopicPartitionStats),
  68. }
  69. // collect current broker's cpu usage
  70. // this needs to be in front, so the following stats can be more accurate
  71. usages, err := cpu.Percent(duration, false)
  72. if err == nil && len(usages) > 0 {
  73. stats.CpuUsagePercent = int32(usages[0])
  74. }
  75. // collect current broker's topics and partitions
  76. manager.topics.IterCb(func(topic string, localTopic *LocalTopic) {
  77. for _, localPartition := range localTopic.Partitions {
  78. topicPartition := &TopicPartition{
  79. Topic: Topic{Namespace: localTopic.Namespace, Name: localTopic.Name},
  80. Partition: localPartition.Partition,
  81. }
  82. stats.Stats[topicPartition.String()] = &mq_pb.TopicPartitionStats{
  83. Topic: &mq_pb.Topic{
  84. Namespace: string(localTopic.Namespace),
  85. Name: localTopic.Name,
  86. },
  87. Partition: localPartition.Partition.ToPbPartition(),
  88. PublisherCount: int32(localPartition.Publishers.Size()),
  89. SubscriberCount: int32(localPartition.Subscribers.Size()),
  90. }
  91. // fmt.Printf("collect topic %+v partition %+v\n", topicPartition, localPartition.Partition)
  92. }
  93. })
  94. return stats
  95. }
  96. func (manager *LocalTopicManager) WaitUntilNoPublishers(topic Topic) {
  97. localTopic, ok := manager.topics.Get(topic.String())
  98. if !ok {
  99. return
  100. }
  101. localTopic.WaitUntilNoPublishers()
  102. }