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.

162 lines
4.9 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
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
11 months ago
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
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
9 months ago
10 months ago
11 months ago
10 months ago
10 months ago
10 months ago
9 months ago
10 months ago
10 months ago
10 months ago
10 months ago
10 months ago
  1. package pub_balancer
  2. import (
  3. cmap "github.com/orcaman/concurrent-map/v2"
  4. "github.com/seaweedfs/seaweedfs/weed/glog"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  6. "math/rand"
  7. "time"
  8. )
  9. func AllocateTopicPartitions(brokers cmap.ConcurrentMap[string, *BrokerStats], partitionCount int32) (assignments []*mq_pb.BrokerPartitionAssignment) {
  10. // divide the ring into partitions
  11. now := time.Now().UnixNano()
  12. rangeSize := MaxPartitionCount / partitionCount
  13. for i := int32(0); i < partitionCount; i++ {
  14. assignment := &mq_pb.BrokerPartitionAssignment{
  15. Partition: &mq_pb.Partition{
  16. RingSize: MaxPartitionCount,
  17. RangeStart: int32(i * rangeSize),
  18. RangeStop: int32((i + 1) * rangeSize),
  19. UnixTimeNs: now,
  20. },
  21. }
  22. if i == partitionCount-1 {
  23. assignment.Partition.RangeStop = MaxPartitionCount
  24. }
  25. assignments = append(assignments, assignment)
  26. }
  27. EnsureAssignmentsToActiveBrokers(brokers, 1, assignments)
  28. glog.V(0).Infof("allocate topic partitions %d: %v", len(assignments), assignments)
  29. return
  30. }
  31. // randomly pick n brokers, which may contain duplicates
  32. // TODO pick brokers based on the broker stats
  33. func pickBrokers(brokers cmap.ConcurrentMap[string, *BrokerStats], count int32) []string {
  34. candidates := make([]string, 0, brokers.Count())
  35. for brokerStatsItem := range brokers.IterBuffered() {
  36. candidates = append(candidates, brokerStatsItem.Key)
  37. }
  38. pickedBrokers := make([]string, 0, count)
  39. for i := int32(0); i < count; i++ {
  40. p := rand.Intn(len(candidates))
  41. pickedBrokers = append(pickedBrokers, candidates[p])
  42. }
  43. return pickedBrokers
  44. }
  45. // reservoir sampling select N brokers from the active brokers, with exclusion of the excluded brokers
  46. func pickBrokersExcluded(brokers []string, count int, excludedLeadBroker string, excludedBrokers []string) []string {
  47. // convert the excluded brokers to a map
  48. excludedBrokerMap := make(map[string]bool)
  49. for _, broker := range excludedBrokers {
  50. excludedBrokerMap[broker] = true
  51. }
  52. if excludedLeadBroker != "" {
  53. excludedBrokerMap[excludedLeadBroker] = true
  54. }
  55. pickedBrokers := make([]string, 0, count)
  56. for i, broker := range brokers {
  57. if _, found := excludedBrokerMap[broker]; found {
  58. continue
  59. }
  60. if len(pickedBrokers) < count {
  61. pickedBrokers = append(pickedBrokers, broker)
  62. } else {
  63. j := rand.Intn(i + 1)
  64. if j < count {
  65. pickedBrokers[j] = broker
  66. }
  67. }
  68. }
  69. // shuffle the picked brokers
  70. count = len(pickedBrokers)
  71. for i := 0; i < count; i++ {
  72. j := rand.Intn(count)
  73. pickedBrokers[i], pickedBrokers[j] = pickedBrokers[j], pickedBrokers[i]
  74. }
  75. return pickedBrokers
  76. }
  77. // EnsureAssignmentsToActiveBrokers ensures the assignments are assigned to active brokers
  78. func EnsureAssignmentsToActiveBrokers(activeBrokers cmap.ConcurrentMap[string, *BrokerStats], followerCount int, assignments []*mq_pb.BrokerPartitionAssignment) (hasChanges bool) {
  79. glog.V(0).Infof("EnsureAssignmentsToActiveBrokers: activeBrokers: %v, followerCount: %d, assignments: %v", activeBrokers.Count(), followerCount, assignments)
  80. candidates := make([]string, 0, activeBrokers.Count())
  81. for brokerStatsItem := range activeBrokers.IterBuffered() {
  82. candidates = append(candidates, brokerStatsItem.Key)
  83. }
  84. for _, assignment := range assignments {
  85. // count how many brokers are needed
  86. count := 0
  87. if assignment.LeaderBroker == "" {
  88. count++
  89. } else if _, found := activeBrokers.Get(assignment.LeaderBroker); !found {
  90. assignment.LeaderBroker = ""
  91. count++
  92. }
  93. for i := 0; i < followerCount; i++ {
  94. if i >= len(assignment.FollowerBrokers) {
  95. count++
  96. continue
  97. }
  98. if assignment.FollowerBrokers[i] == "" {
  99. count++
  100. } else if _, found := activeBrokers.Get(assignment.FollowerBrokers[i]); !found {
  101. assignment.FollowerBrokers[i] = ""
  102. count++
  103. }
  104. }
  105. if count > 0 {
  106. pickedBrokers := pickBrokersExcluded(candidates, count, assignment.LeaderBroker, assignment.FollowerBrokers)
  107. i := 0
  108. if assignment.LeaderBroker == "" {
  109. if i < len(pickedBrokers) {
  110. assignment.LeaderBroker = pickedBrokers[i]
  111. i++
  112. hasChanges = true
  113. }
  114. }
  115. hasEmptyFollowers := false
  116. j := 0
  117. for ; j < len(assignment.FollowerBrokers); j++ {
  118. if assignment.FollowerBrokers[j] == "" {
  119. hasChanges = true
  120. if i < len(pickedBrokers) {
  121. assignment.FollowerBrokers[j] = pickedBrokers[i]
  122. i++
  123. } else {
  124. hasEmptyFollowers = true
  125. }
  126. }
  127. }
  128. if hasEmptyFollowers {
  129. var followerBrokers []string
  130. for _, follower := range assignment.FollowerBrokers {
  131. if follower != "" {
  132. followerBrokers = append(followerBrokers, follower)
  133. }
  134. }
  135. assignment.FollowerBrokers = followerBrokers
  136. }
  137. if i < len(pickedBrokers) {
  138. assignment.FollowerBrokers = append(assignment.FollowerBrokers, pickedBrokers[i:]...)
  139. hasChanges = true
  140. }
  141. }
  142. }
  143. glog.V(0).Infof("EnsureAssignmentsToActiveBrokers: activeBrokers: %v, followerCount: %d, assignments: %v hasChanges: %v", activeBrokers.Count(), followerCount, assignments, hasChanges)
  144. return
  145. }