broker_stats.go 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. package pub_balancer
  2. import (
  3. "fmt"
  4. cmap "github.com/orcaman/concurrent-map/v2"
  5. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/schema_pb"
  8. )
  9. type BrokerStats struct {
  10. TopicPartitionCount int32
  11. PublisherCount int32
  12. SubscriberCount int32
  13. CpuUsagePercent int32
  14. TopicPartitionStats cmap.ConcurrentMap[string, *TopicPartitionStats] // key: topic_partition
  15. Topics []topic.Topic
  16. }
  17. type TopicPartitionStats struct {
  18. topic.TopicPartition
  19. PublisherCount int32
  20. SubscriberCount int32
  21. }
  22. func NewBrokerStats() *BrokerStats {
  23. return &BrokerStats{
  24. TopicPartitionStats: cmap.New[*TopicPartitionStats](),
  25. }
  26. }
  27. func (bs *BrokerStats) String() string {
  28. return fmt.Sprintf("BrokerStats{TopicPartitionCount:%d, Publishers:%d, Subscribers:%d CpuUsagePercent:%d, Stats:%+v}",
  29. bs.TopicPartitionCount, bs.PublisherCount, bs.SubscriberCount, bs.CpuUsagePercent, bs.TopicPartitionStats.Items())
  30. }
  31. func (bs *BrokerStats) UpdateStats(stats *mq_pb.BrokerStats) {
  32. bs.TopicPartitionCount = int32(len(stats.Stats))
  33. bs.CpuUsagePercent = stats.CpuUsagePercent
  34. var publisherCount, subscriberCount int32
  35. currentTopicPartitions := bs.TopicPartitionStats.Items()
  36. for _, topicPartitionStats := range stats.Stats {
  37. tps := &TopicPartitionStats{
  38. TopicPartition: topic.TopicPartition{
  39. Topic: topic.Topic{Namespace: topicPartitionStats.Topic.Namespace, Name: topicPartitionStats.Topic.Name},
  40. Partition: topic.Partition{
  41. RangeStart: topicPartitionStats.Partition.RangeStart,
  42. RangeStop: topicPartitionStats.Partition.RangeStop,
  43. RingSize: topicPartitionStats.Partition.RingSize,
  44. UnixTimeNs: topicPartitionStats.Partition.UnixTimeNs,
  45. },
  46. },
  47. PublisherCount: topicPartitionStats.PublisherCount,
  48. SubscriberCount: topicPartitionStats.SubscriberCount,
  49. }
  50. publisherCount += topicPartitionStats.PublisherCount
  51. subscriberCount += topicPartitionStats.SubscriberCount
  52. key := tps.TopicPartition.TopicPartitionId()
  53. bs.TopicPartitionStats.Set(key, tps)
  54. delete(currentTopicPartitions, key)
  55. }
  56. // remove the topic partitions that are not in the stats
  57. for key := range currentTopicPartitions {
  58. bs.TopicPartitionStats.Remove(key)
  59. }
  60. bs.PublisherCount = publisherCount
  61. bs.SubscriberCount = subscriberCount
  62. }
  63. func (bs *BrokerStats) RegisterAssignment(t *schema_pb.Topic, partition *schema_pb.Partition, isAdd bool) {
  64. tps := &TopicPartitionStats{
  65. TopicPartition: topic.TopicPartition{
  66. Topic: topic.Topic{Namespace: t.Namespace, Name: t.Name},
  67. Partition: topic.Partition{
  68. RangeStart: partition.RangeStart,
  69. RangeStop: partition.RangeStop,
  70. RingSize: partition.RingSize,
  71. UnixTimeNs: partition.UnixTimeNs,
  72. },
  73. },
  74. PublisherCount: 0,
  75. SubscriberCount: 0,
  76. }
  77. key := tps.TopicPartition.TopicPartitionId()
  78. if isAdd {
  79. bs.TopicPartitionStats.SetIfAbsent(key, tps)
  80. } else {
  81. bs.TopicPartitionStats.Remove(key)
  82. }
  83. }