local_manager.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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. }