pub_balancer.go 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. package pub_balancer
  2. import (
  3. cmap "github.com/orcaman/concurrent-map/v2"
  4. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  6. )
  7. const (
  8. MaxPartitionCount = 8 * 9 * 5 * 7 //2520
  9. LockBrokerBalancer = "broker_balancer"
  10. )
  11. // PubBalancer collects stats from all brokers.
  12. //
  13. // When publishers wants to create topics, it picks brokers to assign the topic partitions.
  14. // When consumers wants to subscribe topics, it tells which brokers are serving the topic partitions.
  15. //
  16. // When a partition needs to be split or merged, or a partition needs to be moved to another broker,
  17. // the balancer will let the broker tell the consumer instance to stop processing the partition.
  18. // The existing consumer instance will flush the internal state, and then stop processing.
  19. // Then the balancer will tell the brokers to start sending new messages in the new/moved partition to the consumer instances.
  20. //
  21. // Failover to standby consumer instances:
  22. //
  23. // A consumer group can have min and max number of consumer instances.
  24. // For consumer instances joined after the max number, they will be in standby mode.
  25. //
  26. // When a consumer instance is down, the broker will notice this and inform the balancer.
  27. // The balancer will then tell the broker to send the partition to another standby consumer instance.
  28. type PubBalancer struct {
  29. Brokers cmap.ConcurrentMap[string, *BrokerStats] // key: broker address
  30. // Collected from all brokers when they connect to the broker leader
  31. TopicToBrokers cmap.ConcurrentMap[string, *PartitionSlotToBrokerList] // key: topic name
  32. OnPartitionChange func(topic *mq_pb.Topic, assignments []*mq_pb.BrokerPartitionAssignment)
  33. }
  34. func NewPubBalancer() *PubBalancer {
  35. return &PubBalancer{
  36. Brokers: cmap.New[*BrokerStats](),
  37. TopicToBrokers: cmap.New[*PartitionSlotToBrokerList](),
  38. }
  39. }
  40. func (balancer *PubBalancer) AddBroker(broker string) (brokerStats *BrokerStats) {
  41. var found bool
  42. brokerStats, found = balancer.Brokers.Get(broker)
  43. if !found {
  44. brokerStats = NewBrokerStats()
  45. if !balancer.Brokers.SetIfAbsent(broker, brokerStats) {
  46. brokerStats, _ = balancer.Brokers.Get(broker)
  47. }
  48. }
  49. balancer.onPubAddBroker(broker, brokerStats)
  50. return brokerStats
  51. }
  52. func (balancer *PubBalancer) RemoveBroker(broker string, stats *BrokerStats) {
  53. balancer.Brokers.Remove(broker)
  54. // update TopicToBrokers
  55. for _, topic := range stats.Topics {
  56. partitionSlotToBrokerList, found := balancer.TopicToBrokers.Get(topic.String())
  57. if !found {
  58. continue
  59. }
  60. pickedBroker := pickBrokers(balancer.Brokers, 1)
  61. if len(pickedBroker) == 0 {
  62. partitionSlotToBrokerList.RemoveBroker(broker)
  63. } else {
  64. partitionSlotToBrokerList.ReplaceBroker(broker, pickedBroker[0])
  65. }
  66. }
  67. balancer.onPubRemoveBroker(broker, stats)
  68. }
  69. func (balancer *PubBalancer) OnBrokerStatsUpdated(broker string, brokerStats *BrokerStats, receivedStats *mq_pb.BrokerStats) {
  70. brokerStats.UpdateStats(receivedStats)
  71. // update TopicToBrokers
  72. for _, topicPartitionStats := range receivedStats.Stats {
  73. topicKey := topic.FromPbTopic(topicPartitionStats.Topic).String()
  74. partition := topicPartitionStats.Partition
  75. partitionSlotToBrokerList, found := balancer.TopicToBrokers.Get(topicKey)
  76. if !found {
  77. partitionSlotToBrokerList = NewPartitionSlotToBrokerList(MaxPartitionCount)
  78. if !balancer.TopicToBrokers.SetIfAbsent(topicKey, partitionSlotToBrokerList) {
  79. partitionSlotToBrokerList, _ = balancer.TopicToBrokers.Get(topicKey)
  80. }
  81. }
  82. partitionSlotToBrokerList.AddBroker(partition, broker, topicPartitionStats.Follower)
  83. }
  84. }
  85. // OnPubAddBroker is called when a broker is added for a publisher coordinator
  86. func (balancer *PubBalancer) onPubAddBroker(broker string, brokerStats *BrokerStats) {
  87. }
  88. // OnPubRemoveBroker is called when a broker is removed for a publisher coordinator
  89. func (balancer *PubBalancer) onPubRemoveBroker(broker string, brokerStats *BrokerStats) {
  90. }