allocate.go 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  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. )
  8. func allocateTopicPartitions(brokers cmap.ConcurrentMap[string, *BrokerStats], partitionCount int32) (assignments []*mq_pb.BrokerPartitionAssignment) {
  9. // divide the ring into partitions
  10. rangeSize := MaxPartitionCount / partitionCount
  11. for i := int32(0); i < partitionCount; i++ {
  12. assignment := &mq_pb.BrokerPartitionAssignment{
  13. Partition: &mq_pb.Partition{
  14. RingSize: MaxPartitionCount,
  15. RangeStart: int32(i * rangeSize),
  16. RangeStop: int32((i + 1) * rangeSize),
  17. },
  18. }
  19. if i == partitionCount-1 {
  20. assignment.Partition.RangeStop = MaxPartitionCount
  21. }
  22. assignments = append(assignments, assignment)
  23. }
  24. // pick the brokers
  25. pickedBrokers := pickBrokers(brokers, partitionCount)
  26. // assign the partitions to brokers
  27. for i, assignment := range assignments {
  28. assignment.LeaderBroker = pickedBrokers[i]
  29. }
  30. glog.V(0).Infof("allocate topic partitions %d: %v", len(assignments), assignments)
  31. return
  32. }
  33. // for now: randomly pick brokers
  34. // TODO pick brokers based on the broker stats
  35. func pickBrokers(brokers cmap.ConcurrentMap[string, *BrokerStats], count int32) []string {
  36. candidates := make([]string, 0, brokers.Count())
  37. for brokerStatsItem := range brokers.IterBuffered() {
  38. candidates = append(candidates, brokerStatsItem.Key)
  39. }
  40. pickedBrokers := make([]string, 0, count)
  41. for i := int32(0); i < count; i++ {
  42. p := rand.Int() % len(candidates)
  43. if p < 0 {
  44. p = -p
  45. }
  46. pickedBrokers = append(pickedBrokers, candidates[p])
  47. }
  48. return pickedBrokers
  49. }