You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
1.8 KiB

Merge accumulated changes related to message queue (#5098) * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * balance partitions on brokers * prepare topic partition first and then publish, move partition * purge unused APIs * clean up * adjust logs * add BalanceTopics() grpc API * configure topic * configure topic command * refactor * repair missing partitions * sequence of operations to ensure ordering * proto to close publishers and consumers * rename file * topic partition versioned by unixTimeNs * create local topic partition * close publishers * randomize the client name * wait until no publishers * logs * close stop publisher channel * send last ack * comments * comment * comments * support list of brokers * add cli options * Update .gitignore * logs * return io.eof directly * refactor * optionally create topic * refactoring * detect consumer disconnection * sub client wait for more messages * subscribe by time stamp * rename * rename to sub_balancer * rename * adjust comments * rename * fix compilation * rename * rename * SubscriberToSubCoordinator * sticky rebalance * go fmt * add tests * tracking topic=>broker * merge * comment
1 year ago
  1. package pub_balancer
  2. import (
  3. cmap "github.com/orcaman/concurrent-map/v2"
  4. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  5. "math/rand"
  6. )
  7. func BalanceTopicPartitionOnBrokers(brokers cmap.ConcurrentMap[string, *BrokerStats]) BalanceAction {
  8. // 1. calculate the average number of partitions per broker
  9. var totalPartitionCount int32
  10. var totalBrokerCount int32
  11. for brokerStats := range brokers.IterBuffered() {
  12. totalBrokerCount++
  13. totalPartitionCount += brokerStats.Val.TopicPartitionCount
  14. }
  15. averagePartitionCountPerBroker := totalPartitionCount / totalBrokerCount
  16. minPartitionCountPerBroker := averagePartitionCountPerBroker
  17. maxPartitionCountPerBroker := averagePartitionCountPerBroker
  18. var sourceBroker, targetBroker string
  19. var candidatePartition *topic.TopicPartition
  20. for brokerStats := range brokers.IterBuffered() {
  21. if minPartitionCountPerBroker > brokerStats.Val.TopicPartitionCount {
  22. minPartitionCountPerBroker = brokerStats.Val.TopicPartitionCount
  23. targetBroker = brokerStats.Key
  24. }
  25. if maxPartitionCountPerBroker < brokerStats.Val.TopicPartitionCount {
  26. maxPartitionCountPerBroker = brokerStats.Val.TopicPartitionCount
  27. sourceBroker = brokerStats.Key
  28. // select a random partition from the source broker
  29. randomePartitionIndex := rand.Intn(int(brokerStats.Val.TopicPartitionCount))
  30. index := 0
  31. for topicPartitionStats := range brokerStats.Val.TopicPartitionStats.IterBuffered() {
  32. if index == randomePartitionIndex {
  33. candidatePartition = &topicPartitionStats.Val.TopicPartition
  34. break
  35. } else {
  36. index++
  37. }
  38. }
  39. }
  40. }
  41. if minPartitionCountPerBroker >= maxPartitionCountPerBroker-1 {
  42. return nil
  43. }
  44. // 2. move the partitions from the source broker to the target broker
  45. return &BalanceActionMove{
  46. TopicPartition: *candidatePartition,
  47. SourceBroker: sourceBroker,
  48. TargetBroker: targetBroker,
  49. }
  50. }