2023-12-12 04:05:54 +08:00
|
|
|
package pub_balancer
|
|
|
|
|
|
|
|
import (
|
|
|
|
cmap "github.com/orcaman/concurrent-map/v2"
|
2023-12-29 12:35:15 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
2023-12-12 04:05:54 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
MaxPartitionCount = 8 * 9 * 5 * 7 //2520
|
|
|
|
LockBrokerBalancer = "broker_balancer"
|
|
|
|
)
|
|
|
|
|
2024-05-22 00:56:30 +08:00
|
|
|
// PubBalancer collects stats from all brokers.
|
2023-12-12 04:05:54 +08:00
|
|
|
//
|
|
|
|
// When publishers wants to create topics, it picks brokers to assign the topic partitions.
|
|
|
|
// When consumers wants to subscribe topics, it tells which brokers are serving the topic partitions.
|
|
|
|
//
|
|
|
|
// When a partition needs to be split or merged, or a partition needs to be moved to another broker,
|
|
|
|
// the balancer will let the broker tell the consumer instance to stop processing the partition.
|
|
|
|
// The existing consumer instance will flush the internal state, and then stop processing.
|
|
|
|
// Then the balancer will tell the brokers to start sending new messages in the new/moved partition to the consumer instances.
|
|
|
|
//
|
|
|
|
// Failover to standby consumer instances:
|
|
|
|
//
|
|
|
|
// A consumer group can have min and max number of consumer instances.
|
|
|
|
// For consumer instances joined after the max number, they will be in standby mode.
|
|
|
|
//
|
|
|
|
// When a consumer instance is down, the broker will notice this and inform the balancer.
|
|
|
|
// The balancer will then tell the broker to send the partition to another standby consumer instance.
|
2024-05-22 00:56:30 +08:00
|
|
|
type PubBalancer struct {
|
2023-12-12 04:05:54 +08:00
|
|
|
Brokers cmap.ConcurrentMap[string, *BrokerStats] // key: broker address
|
|
|
|
// Collected from all brokers when they connect to the broker leader
|
2024-03-01 01:38:52 +08:00
|
|
|
TopicToBrokers cmap.ConcurrentMap[string, *PartitionSlotToBrokerList] // key: topic name
|
2024-01-04 05:30:30 +08:00
|
|
|
OnPartitionChange func(topic *mq_pb.Topic, assignments []*mq_pb.BrokerPartitionAssignment)
|
2024-03-01 01:38:52 +08:00
|
|
|
OnAddBroker func(broker string, brokerStats *BrokerStats)
|
|
|
|
OnRemoveBroker func(broker string, brokerStats *BrokerStats)
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
2023-12-23 03:33:50 +08:00
|
|
|
|
2024-05-22 01:02:07 +08:00
|
|
|
func NewPubBalancer() *PubBalancer {
|
2024-05-22 00:56:30 +08:00
|
|
|
return &PubBalancer{
|
2023-12-23 03:33:50 +08:00
|
|
|
Brokers: cmap.New[*BrokerStats](),
|
2023-12-12 04:05:54 +08:00
|
|
|
TopicToBrokers: cmap.New[*PartitionSlotToBrokerList](),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-05-22 00:56:30 +08:00
|
|
|
func (balancer *PubBalancer) AddBroker(broker string) (brokerStats *BrokerStats) {
|
2023-12-12 04:05:54 +08:00
|
|
|
var found bool
|
|
|
|
brokerStats, found = balancer.Brokers.Get(broker)
|
|
|
|
if !found {
|
|
|
|
brokerStats = NewBrokerStats()
|
|
|
|
if !balancer.Brokers.SetIfAbsent(broker, brokerStats) {
|
|
|
|
brokerStats, _ = balancer.Brokers.Get(broker)
|
|
|
|
}
|
|
|
|
}
|
2024-01-21 04:16:40 +08:00
|
|
|
balancer.onPubAddBroker(broker, brokerStats)
|
2024-01-21 03:41:11 +08:00
|
|
|
balancer.OnAddBroker(broker, brokerStats)
|
2023-12-12 04:05:54 +08:00
|
|
|
return brokerStats
|
|
|
|
}
|
|
|
|
|
2024-05-22 00:56:30 +08:00
|
|
|
func (balancer *PubBalancer) RemoveBroker(broker string, stats *BrokerStats) {
|
2023-12-12 04:05:54 +08:00
|
|
|
balancer.Brokers.Remove(broker)
|
|
|
|
|
|
|
|
// update TopicToBrokers
|
|
|
|
for _, topic := range stats.Topics {
|
|
|
|
partitionSlotToBrokerList, found := balancer.TopicToBrokers.Get(topic.String())
|
|
|
|
if !found {
|
|
|
|
continue
|
|
|
|
}
|
2024-01-21 17:24:12 +08:00
|
|
|
pickedBroker := pickBrokers(balancer.Brokers, 1)
|
|
|
|
if len(pickedBroker) == 0 {
|
|
|
|
partitionSlotToBrokerList.RemoveBroker(broker)
|
|
|
|
} else {
|
|
|
|
partitionSlotToBrokerList.ReplaceBroker(broker, pickedBroker[0])
|
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
2024-01-21 04:16:40 +08:00
|
|
|
balancer.onPubRemoveBroker(broker, stats)
|
2024-01-21 03:41:11 +08:00
|
|
|
balancer.OnRemoveBroker(broker, stats)
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
|
2024-05-22 00:56:30 +08:00
|
|
|
func (balancer *PubBalancer) OnBrokerStatsUpdated(broker string, brokerStats *BrokerStats, receivedStats *mq_pb.BrokerStats) {
|
2023-12-12 04:05:54 +08:00
|
|
|
brokerStats.UpdateStats(receivedStats)
|
|
|
|
|
|
|
|
// update TopicToBrokers
|
|
|
|
for _, topicPartitionStats := range receivedStats.Stats {
|
2023-12-29 12:35:15 +08:00
|
|
|
topicKey := topic.FromPbTopic(topicPartitionStats.Topic).String()
|
2023-12-12 04:05:54 +08:00
|
|
|
partition := topicPartitionStats.Partition
|
2023-12-29 12:35:15 +08:00
|
|
|
partitionSlotToBrokerList, found := balancer.TopicToBrokers.Get(topicKey)
|
2023-12-12 04:05:54 +08:00
|
|
|
if !found {
|
|
|
|
partitionSlotToBrokerList = NewPartitionSlotToBrokerList(MaxPartitionCount)
|
2023-12-29 12:35:15 +08:00
|
|
|
if !balancer.TopicToBrokers.SetIfAbsent(topicKey, partitionSlotToBrokerList) {
|
|
|
|
partitionSlotToBrokerList, _ = balancer.TopicToBrokers.Get(topicKey)
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
}
|
2024-05-19 15:46:12 +08:00
|
|
|
partitionSlotToBrokerList.AddBroker(partition, broker, topicPartitionStats.Follower)
|
2023-12-12 04:05:54 +08:00
|
|
|
}
|
|
|
|
}
|
2024-01-21 04:16:40 +08:00
|
|
|
|
|
|
|
// OnPubAddBroker is called when a broker is added for a publisher coordinator
|
2024-05-22 00:56:30 +08:00
|
|
|
func (balancer *PubBalancer) onPubAddBroker(broker string, brokerStats *BrokerStats) {
|
2024-01-21 04:16:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// OnPubRemoveBroker is called when a broker is removed for a publisher coordinator
|
2024-05-22 00:56:30 +08:00
|
|
|
func (balancer *PubBalancer) onPubRemoveBroker(broker string, brokerStats *BrokerStats) {
|
2024-01-21 04:16:40 +08:00
|
|
|
}
|