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.5 KiB

  1. package balancer
  2. import (
  3. cmap "github.com/orcaman/concurrent-map/v2"
  4. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  5. "math/rand"
  6. )
  7. func allocateTopicPartitions(brokers cmap.ConcurrentMap[string, *BrokerStats], partitionCount int32) (assignments []*mq_pb.BrokerPartitionAssignment) {
  8. // divide the ring into partitions
  9. rangeSize := MaxPartitionCount / partitionCount
  10. for i := int32(0); i < partitionCount; i++ {
  11. assignment := &mq_pb.BrokerPartitionAssignment{
  12. Partition: &mq_pb.Partition{
  13. RingSize: MaxPartitionCount,
  14. RangeStart: int32(i * rangeSize),
  15. RangeStop: int32((i + 1) * rangeSize),
  16. },
  17. }
  18. if i == partitionCount-1 {
  19. assignment.Partition.RangeStop = MaxPartitionCount
  20. }
  21. assignments = append(assignments, assignment)
  22. }
  23. // pick the brokers
  24. pickedBrokers := pickBrokers(brokers, partitionCount)
  25. // assign the partitions to brokers
  26. for i, assignment := range assignments {
  27. assignment.LeaderBroker = pickedBrokers[i]
  28. }
  29. return
  30. }
  31. // for now: randomly pick brokers
  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.Int() % len(candidates)
  41. if p < 0 {
  42. p = -p
  43. }
  44. pickedBrokers = append(pickedBrokers, candidates[p])
  45. }
  46. return pickedBrokers
  47. }