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.

115 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
4 months 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. localTopic.addPartition(localPartition)
  28. }
  29. // GetLocalPartition gets a topic from the local topic manager
  30. func (manager *LocalTopicManager) GetLocalPartition(topic Topic, partition Partition) *LocalPartition {
  31. localTopic, ok := manager.topics.Get(topic.String())
  32. if !ok {
  33. return nil
  34. }
  35. return localTopic.findPartition(partition)
  36. }
  37. // RemoveTopic removes a topic from the local topic manager
  38. func (manager *LocalTopicManager) RemoveTopic(topic Topic) {
  39. manager.topics.Remove(topic.String())
  40. }
  41. func (manager *LocalTopicManager) RemoveLocalPartition(topic Topic, partition Partition) (removed bool) {
  42. localTopic, ok := manager.topics.Get(topic.String())
  43. if !ok {
  44. return false
  45. }
  46. return localTopic.removePartition(partition)
  47. }
  48. func (manager *LocalTopicManager) ClosePublishers(topic Topic, unixTsNs int64) (removed bool) {
  49. localTopic, ok := manager.topics.Get(topic.String())
  50. if !ok {
  51. return false
  52. }
  53. return localTopic.closePartitionPublishers(unixTsNs)
  54. }
  55. func (manager *LocalTopicManager) CloseSubscribers(topic Topic, unixTsNs int64) (removed bool) {
  56. localTopic, ok := manager.topics.Get(topic.String())
  57. if !ok {
  58. return false
  59. }
  60. return localTopic.closePartitionSubscribers(unixTsNs)
  61. }
  62. func (manager *LocalTopicManager) CollectStats(duration time.Duration) *mq_pb.BrokerStats {
  63. stats := &mq_pb.BrokerStats{
  64. Stats: make(map[string]*mq_pb.TopicPartitionStats),
  65. }
  66. // collect current broker's cpu usage
  67. // this needs to be in front, so the following stats can be more accurate
  68. usages, err := cpu.Percent(duration, false)
  69. if err == nil && len(usages) > 0 {
  70. stats.CpuUsagePercent = int32(usages[0])
  71. }
  72. // collect current broker's topics and partitions
  73. manager.topics.IterCb(func(topic string, localTopic *LocalTopic) {
  74. for _, localPartition := range localTopic.Partitions {
  75. topicPartition := &TopicPartition{
  76. Topic: Topic{Namespace: localTopic.Namespace, Name: localTopic.Name},
  77. Partition: localPartition.Partition,
  78. }
  79. stats.Stats[topicPartition.String()] = &mq_pb.TopicPartitionStats{
  80. Topic: &mq_pb.Topic{
  81. Namespace: string(localTopic.Namespace),
  82. Name: localTopic.Name,
  83. },
  84. Partition: localPartition.Partition.ToPbPartition(),
  85. PublisherCount: int32(localPartition.Publishers.Size()),
  86. SubscriberCount: int32(localPartition.Subscribers.Size()),
  87. Follower: localPartition.Follower,
  88. }
  89. // fmt.Printf("collect topic %+v partition %+v\n", topicPartition, localPartition.Partition)
  90. }
  91. })
  92. return stats
  93. }
  94. func (manager *LocalTopicManager) WaitUntilNoPublishers(topic Topic) {
  95. localTopic, ok := manager.topics.Get(topic.String())
  96. if !ok {
  97. return
  98. }
  99. localTopic.WaitUntilNoPublishers()
  100. }