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.

116 lines
3.5 KiB

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