2023-12-12 04:05:54 +08:00
|
|
|
package pub_balancer
|
2023-09-17 06:06:16 +08:00
|
|
|
|
|
|
|
import (
|
2023-09-19 09:47:34 +08:00
|
|
|
"fmt"
|
2023-09-17 06:06:16 +08:00
|
|
|
cmap "github.com/orcaman/concurrent-map/v2"
|
2023-09-25 14:05:41 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
2023-09-19 09:47:34 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
)
|
|
|
|
|
2023-09-17 06:06:16 +08:00
|
|
|
type BrokerStats struct {
|
|
|
|
TopicPartitionCount int32
|
2024-03-28 14:42:19 +08:00
|
|
|
PublisherCount int32
|
|
|
|
SubscriberCount int32
|
2023-09-17 06:06:16 +08:00
|
|
|
CpuUsagePercent int32
|
2023-12-12 04:05:54 +08:00
|
|
|
TopicPartitionStats cmap.ConcurrentMap[string, *TopicPartitionStats] // key: topic_partition
|
|
|
|
Topics []topic.Topic
|
|
|
|
}
|
|
|
|
type TopicPartitionStats struct {
|
|
|
|
topic.TopicPartition
|
2024-03-28 14:42:19 +08:00
|
|
|
PublisherCount int32
|
|
|
|
SubscriberCount int32
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewBrokerStats() *BrokerStats {
|
|
|
|
return &BrokerStats{
|
|
|
|
TopicPartitionStats: cmap.New[*TopicPartitionStats](),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (bs *BrokerStats) String() string {
|
2024-03-28 14:42:19 +08:00
|
|
|
return fmt.Sprintf("BrokerStats{TopicPartitionCount:%d, Publishers:%d, Subscribers:%d CpuUsagePercent:%d, Stats:%+v}",
|
|
|
|
bs.TopicPartitionCount, bs.PublisherCount, bs.SubscriberCount, bs.CpuUsagePercent, bs.TopicPartitionStats.Items())
|
2023-09-19 09:47:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BrokerStats) UpdateStats(stats *mq_pb.BrokerStats) {
|
|
|
|
bs.TopicPartitionCount = int32(len(stats.Stats))
|
|
|
|
bs.CpuUsagePercent = stats.CpuUsagePercent
|
|
|
|
|
2024-03-28 14:42:19 +08:00
|
|
|
var publisherCount, subscriberCount int32
|
2023-12-12 04:05:54 +08:00
|
|
|
currentTopicPartitions := bs.TopicPartitionStats.Items()
|
2023-09-19 09:47:34 +08:00
|
|
|
for _, topicPartitionStats := range stats.Stats {
|
|
|
|
tps := &TopicPartitionStats{
|
2023-09-25 14:05:41 +08:00
|
|
|
TopicPartition: topic.TopicPartition{
|
2024-03-01 01:38:52 +08:00
|
|
|
Topic: topic.Topic{Namespace: topicPartitionStats.Topic.Namespace, Name: topicPartitionStats.Topic.Name},
|
2024-01-11 14:36:17 +08:00
|
|
|
Partition: topic.Partition{
|
|
|
|
RangeStart: topicPartitionStats.Partition.RangeStart,
|
2024-03-01 01:38:52 +08:00
|
|
|
RangeStop: topicPartitionStats.Partition.RangeStop,
|
|
|
|
RingSize: topicPartitionStats.Partition.RingSize,
|
2024-01-11 14:36:17 +08:00
|
|
|
UnixTimeNs: topicPartitionStats.Partition.UnixTimeNs,
|
|
|
|
},
|
2023-09-19 09:47:34 +08:00
|
|
|
},
|
2024-03-28 14:42:19 +08:00
|
|
|
PublisherCount: topicPartitionStats.PublisherCount,
|
|
|
|
SubscriberCount: topicPartitionStats.SubscriberCount,
|
2023-09-19 09:47:34 +08:00
|
|
|
}
|
2024-03-28 14:42:19 +08:00
|
|
|
publisherCount += topicPartitionStats.PublisherCount
|
|
|
|
subscriberCount += topicPartitionStats.SubscriberCount
|
2023-09-19 09:47:34 +08:00
|
|
|
key := tps.TopicPartition.String()
|
2023-12-12 04:05:54 +08:00
|
|
|
bs.TopicPartitionStats.Set(key, tps)
|
2023-09-19 09:47:34 +08:00
|
|
|
delete(currentTopicPartitions, key)
|
|
|
|
}
|
|
|
|
// remove the topic partitions that are not in the stats
|
|
|
|
for key := range currentTopicPartitions {
|
2023-12-12 04:05:54 +08:00
|
|
|
bs.TopicPartitionStats.Remove(key)
|
2023-09-19 09:47:34 +08:00
|
|
|
}
|
2024-03-28 14:42:19 +08:00
|
|
|
bs.PublisherCount = publisherCount
|
|
|
|
bs.SubscriberCount = subscriberCount
|
2023-09-19 09:47:34 +08:00
|
|
|
}
|
|
|
|
|
2024-01-23 02:47:39 +08:00
|
|
|
func (bs *BrokerStats) RegisterAssignment(t *mq_pb.Topic, partition *mq_pb.Partition, isAdd bool) {
|
2023-09-25 14:05:41 +08:00
|
|
|
tps := &TopicPartitionStats{
|
|
|
|
TopicPartition: topic.TopicPartition{
|
2024-03-01 01:38:52 +08:00
|
|
|
Topic: topic.Topic{Namespace: t.Namespace, Name: t.Name},
|
2024-01-11 14:36:17 +08:00
|
|
|
Partition: topic.Partition{
|
|
|
|
RangeStart: partition.RangeStart,
|
2024-03-01 01:38:52 +08:00
|
|
|
RangeStop: partition.RangeStop,
|
|
|
|
RingSize: partition.RingSize,
|
2024-01-11 14:36:17 +08:00
|
|
|
UnixTimeNs: partition.UnixTimeNs,
|
|
|
|
},
|
2023-09-25 14:05:41 +08:00
|
|
|
},
|
2024-03-28 14:42:19 +08:00
|
|
|
PublisherCount: 0,
|
|
|
|
SubscriberCount: 0,
|
2023-09-25 14:05:41 +08:00
|
|
|
}
|
|
|
|
key := tps.TopicPartition.String()
|
2024-01-23 02:47:39 +08:00
|
|
|
if isAdd {
|
|
|
|
bs.TopicPartitionStats.SetIfAbsent(key, tps)
|
|
|
|
} else {
|
|
|
|
bs.TopicPartitionStats.Remove(key)
|
|
|
|
}
|
2023-09-19 09:47:34 +08:00
|
|
|
}
|