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.

86 lines
2.7 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
11 months ago
11 months ago
11 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. // pick the brokers
  28. pickedBrokers := pickBrokers(brokers, partitionCount)
  29. // assign the partitions to brokers
  30. for i, assignment := range assignments {
  31. assignment.LeaderBroker = pickedBrokers[i]
  32. }
  33. glog.V(0).Infof("allocate topic partitions %d: %v", len(assignments), assignments)
  34. return
  35. }
  36. // for now: randomly pick brokers
  37. // TODO pick brokers based on the broker stats
  38. func pickBrokers(brokers cmap.ConcurrentMap[string, *BrokerStats], count int32) []string {
  39. candidates := make([]string, 0, brokers.Count())
  40. for brokerStatsItem := range brokers.IterBuffered() {
  41. candidates = append(candidates, brokerStatsItem.Key)
  42. }
  43. pickedBrokers := make([]string, 0, count)
  44. for i := int32(0); i < count; i++ {
  45. p := rand.Int() % len(candidates)
  46. if p < 0 {
  47. p = -p
  48. }
  49. pickedBrokers = append(pickedBrokers, candidates[p])
  50. }
  51. return pickedBrokers
  52. }
  53. func EnsureAssignmentsToActiveBrokers(activeBrokers cmap.ConcurrentMap[string, *BrokerStats], assignments []*mq_pb.BrokerPartitionAssignment) (addedAssignments, updatedAssignments []*mq_pb.BrokerPartitionAssignment) {
  54. for _, assignment := range assignments {
  55. if assignment.LeaderBroker == "" {
  56. addedAssignments = append(addedAssignments, assignment)
  57. continue
  58. }
  59. if _, found := activeBrokers.Get(assignment.LeaderBroker); !found {
  60. updatedAssignments = append(updatedAssignments, assignment)
  61. continue
  62. }
  63. }
  64. // pick the brokers with the least number of partitions
  65. if len(addedAssignments) > 0 {
  66. pickedBrokers := pickBrokers(activeBrokers, int32(len(addedAssignments)))
  67. for i, assignment := range addedAssignments {
  68. assignment.LeaderBroker = pickedBrokers[i]
  69. }
  70. }
  71. if len(updatedAssignments) == 0 {
  72. pickedBrokers := pickBrokers(activeBrokers, int32(len(updatedAssignments)))
  73. for i, assignment := range updatedAssignments {
  74. assignment.LeaderBroker = pickedBrokers[i]
  75. }
  76. }
  77. return
  78. }