2023-12-11 20:05:54 +00:00
|
|
|
package pub_balancer
|
2023-09-19 01:47:34 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
cmap "github.com/orcaman/concurrent-map/v2"
|
2023-12-11 20:05:54 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2023-09-19 01:47:34 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
2023-09-24 22:08:44 +00:00
|
|
|
"math/rand"
|
2024-01-11 06:36:17 +00:00
|
|
|
"time"
|
2023-09-19 01:47:34 +00:00
|
|
|
)
|
|
|
|
|
2024-01-16 16:40:23 +00:00
|
|
|
func AllocateTopicPartitions(brokers cmap.ConcurrentMap[string, *BrokerStats], partitionCount int32) (assignments []*mq_pb.BrokerPartitionAssignment) {
|
2023-09-24 22:08:44 +00:00
|
|
|
// divide the ring into partitions
|
2024-01-11 06:36:17 +00:00
|
|
|
now := time.Now().UnixNano()
|
2023-09-24 22:08:44 +00:00
|
|
|
rangeSize := MaxPartitionCount / partitionCount
|
2023-09-25 04:19:51 +00:00
|
|
|
for i := int32(0); i < partitionCount; i++ {
|
2023-09-24 22:08:44 +00:00
|
|
|
assignment := &mq_pb.BrokerPartitionAssignment{
|
2023-09-19 01:47:34 +00:00
|
|
|
Partition: &mq_pb.Partition{
|
|
|
|
RingSize: MaxPartitionCount,
|
2023-09-24 22:08:44 +00:00
|
|
|
RangeStart: int32(i * rangeSize),
|
|
|
|
RangeStop: int32((i + 1) * rangeSize),
|
2024-01-11 06:36:17 +00:00
|
|
|
UnixTimeNs: now,
|
2023-09-19 01:47:34 +00:00
|
|
|
},
|
2023-09-24 22:08:44 +00:00
|
|
|
}
|
|
|
|
if i == partitionCount-1 {
|
|
|
|
assignment.Partition.RangeStop = MaxPartitionCount
|
|
|
|
}
|
|
|
|
assignments = append(assignments, assignment)
|
2023-09-19 01:47:34 +00:00
|
|
|
}
|
2023-09-24 22:08:44 +00:00
|
|
|
|
|
|
|
// pick the brokers
|
|
|
|
pickedBrokers := pickBrokers(brokers, partitionCount)
|
|
|
|
|
|
|
|
// assign the partitions to brokers
|
|
|
|
for i, assignment := range assignments {
|
|
|
|
assignment.LeaderBroker = pickedBrokers[i]
|
|
|
|
}
|
2023-12-11 20:05:54 +00:00
|
|
|
glog.V(0).Infof("allocate topic partitions %d: %v", len(assignments), assignments)
|
2023-09-24 22:08:44 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// for now: randomly pick brokers
|
|
|
|
// TODO pick brokers based on the broker stats
|
2023-09-25 04:19:51 +00:00
|
|
|
func pickBrokers(brokers cmap.ConcurrentMap[string, *BrokerStats], count int32) []string {
|
2023-09-24 22:08:44 +00:00
|
|
|
candidates := make([]string, 0, brokers.Count())
|
|
|
|
for brokerStatsItem := range brokers.IterBuffered() {
|
|
|
|
candidates = append(candidates, brokerStatsItem.Key)
|
|
|
|
}
|
|
|
|
pickedBrokers := make([]string, 0, count)
|
2023-09-25 04:19:51 +00:00
|
|
|
for i := int32(0); i < count; i++ {
|
2023-09-24 22:08:44 +00:00
|
|
|
p := rand.Int() % len(candidates)
|
|
|
|
if p < 0 {
|
|
|
|
p = -p
|
|
|
|
}
|
|
|
|
pickedBrokers = append(pickedBrokers, candidates[p])
|
|
|
|
}
|
|
|
|
return pickedBrokers
|
2023-09-19 01:47:34 +00:00
|
|
|
}
|