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
|
|
|
|
ConsumerCount int32
|
|
|
|
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
|
|
|
|
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 09:47:34 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (bs *BrokerStats) UpdateStats(stats *mq_pb.BrokerStats) {
|
|
|
|
bs.TopicPartitionCount = int32(len(stats.Stats))
|
|
|
|
bs.CpuUsagePercent = stats.CpuUsagePercent
|
|
|
|
|
|
|
|
var consumerCount 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
|
|
|
},
|
|
|
|
ConsumerCount: topicPartitionStats.ConsumerCount,
|
|
|
|
IsLeader: topicPartitionStats.IsLeader,
|
|
|
|
}
|
|
|
|
consumerCount += topicPartitionStats.ConsumerCount
|
|
|
|
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
|
|
|
}
|
|
|
|
bs.ConsumerCount = consumerCount
|
|
|
|
|
|
|
|
}
|
|
|
|
|
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
|
|
|
},
|
|
|
|
ConsumerCount: 0,
|
|
|
|
IsLeader: true,
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|