2023-12-11 20:05:54 +00:00
|
|
|
package pub_balancer
|
2023-09-16 22:06:16 +00:00
|
|
|
|
|
|
|
import (
|
2023-09-19 01:47:34 +00:00
|
|
|
"fmt"
|
2023-09-16 22:06:16 +00:00
|
|
|
cmap "github.com/orcaman/concurrent-map/v2"
|
2023-09-25 06:05:41 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
2023-09-19 01:47:34 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
)
|
|
|
|
|
2023-09-16 22:06:16 +00:00
|
|
|
type BrokerStats struct {
|
|
|
|
TopicPartitionCount int32
|
|
|
|
ConsumerCount int32
|
|
|
|
CpuUsagePercent int32
|
2023-12-11 20:05:54 +00:00
|
|
|
TopicPartitionStats cmap.ConcurrentMap[string, *TopicPartitionStats] // key: topic_partition
|
|
|
|
Topics []topic.Topic
|
|
|
|
}
|
|
|
|
type TopicPartitionStats struct {
|
|
|
|
topic.TopicPartition
|
|
|
|
ConsumerCount int32
|
|
|
|
IsLeader bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewBrokerStats() *BrokerStats {
|
|
|
|
return &BrokerStats{
|
|
|
|
TopicPartitionStats: cmap.New[*TopicPartitionStats](),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
func (bs *BrokerStats) String() string {
|
|
|
|
return fmt.Sprintf("BrokerStats{TopicPartitionCount:%d, ConsumerCount:%d, CpuUsagePercent:%d, Stats:%+v}",
|
|
|
|
bs.TopicPartitionCount, bs.ConsumerCount, bs.CpuUsagePercent, bs.TopicPartitionStats.Items())
|
2023-09-19 01:47:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BrokerStats) UpdateStats(stats *mq_pb.BrokerStats) {
|
|
|
|
bs.TopicPartitionCount = int32(len(stats.Stats))
|
|
|
|
bs.CpuUsagePercent = stats.CpuUsagePercent
|
|
|
|
|
|
|
|
var consumerCount int32
|
2023-12-11 20:05:54 +00:00
|
|
|
currentTopicPartitions := bs.TopicPartitionStats.Items()
|
2023-09-19 01:47:34 +00:00
|
|
|
for _, topicPartitionStats := range stats.Stats {
|
|
|
|
tps := &TopicPartitionStats{
|
2023-09-25 06:05:41 +00:00
|
|
|
TopicPartition: topic.TopicPartition{
|
2023-10-02 08:01:45 +00:00
|
|
|
Topic: topic.Topic{Namespace: topicPartitionStats.Topic.Namespace, Name: topicPartitionStats.Topic.Name},
|
|
|
|
Partition: topic.Partition{RangeStart: topicPartitionStats.Partition.RangeStart, RangeStop: topicPartitionStats.Partition.RangeStop, RingSize: topicPartitionStats.Partition.RingSize},
|
2023-09-19 01:47:34 +00:00
|
|
|
},
|
|
|
|
ConsumerCount: topicPartitionStats.ConsumerCount,
|
|
|
|
IsLeader: topicPartitionStats.IsLeader,
|
|
|
|
}
|
|
|
|
consumerCount += topicPartitionStats.ConsumerCount
|
|
|
|
key := tps.TopicPartition.String()
|
2023-12-11 20:05:54 +00:00
|
|
|
bs.TopicPartitionStats.Set(key, tps)
|
2023-09-19 01:47:34 +00:00
|
|
|
delete(currentTopicPartitions, key)
|
|
|
|
}
|
|
|
|
// remove the topic partitions that are not in the stats
|
|
|
|
for key := range currentTopicPartitions {
|
2023-12-11 20:05:54 +00:00
|
|
|
bs.TopicPartitionStats.Remove(key)
|
2023-09-19 01:47:34 +00:00
|
|
|
}
|
|
|
|
bs.ConsumerCount = consumerCount
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-09-25 06:05:41 +00:00
|
|
|
func (bs *BrokerStats) RegisterAssignment(t *mq_pb.Topic, partition *mq_pb.Partition) {
|
|
|
|
tps := &TopicPartitionStats{
|
|
|
|
TopicPartition: topic.TopicPartition{
|
2023-10-02 08:01:45 +00:00
|
|
|
Topic: topic.Topic{Namespace: t.Namespace, Name: t.Name},
|
|
|
|
Partition: topic.Partition{RangeStart: partition.RangeStart, RangeStop: partition.RangeStop},
|
2023-09-25 06:05:41 +00:00
|
|
|
},
|
|
|
|
ConsumerCount: 0,
|
|
|
|
IsLeader: true,
|
|
|
|
}
|
|
|
|
key := tps.TopicPartition.String()
|
2023-12-11 20:05:54 +00:00
|
|
|
bs.TopicPartitionStats.Set(key, tps)
|
2023-09-19 01:47:34 +00:00
|
|
|
}
|