mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-12-26 02:37:55 +08:00
580940bf82
* 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
55 lines
1.6 KiB
Go
55 lines
1.6 KiB
Go
package pub_balancer
|
|
|
|
import (
|
|
cmap "github.com/orcaman/concurrent-map/v2"
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
"math/rand"
|
|
)
|
|
|
|
func allocateTopicPartitions(brokers cmap.ConcurrentMap[string, *BrokerStats], partitionCount int32) (assignments []*mq_pb.BrokerPartitionAssignment) {
|
|
// divide the ring into partitions
|
|
rangeSize := MaxPartitionCount / partitionCount
|
|
for i := int32(0); i < partitionCount; i++ {
|
|
assignment := &mq_pb.BrokerPartitionAssignment{
|
|
Partition: &mq_pb.Partition{
|
|
RingSize: MaxPartitionCount,
|
|
RangeStart: int32(i * rangeSize),
|
|
RangeStop: int32((i + 1) * rangeSize),
|
|
},
|
|
}
|
|
if i == partitionCount-1 {
|
|
assignment.Partition.RangeStop = MaxPartitionCount
|
|
}
|
|
assignments = append(assignments, assignment)
|
|
}
|
|
|
|
// pick the brokers
|
|
pickedBrokers := pickBrokers(brokers, partitionCount)
|
|
|
|
// assign the partitions to brokers
|
|
for i, assignment := range assignments {
|
|
assignment.LeaderBroker = pickedBrokers[i]
|
|
}
|
|
glog.V(0).Infof("allocate topic partitions %d: %v", len(assignments), assignments)
|
|
return
|
|
}
|
|
|
|
// for now: randomly pick brokers
|
|
// TODO pick brokers based on the broker stats
|
|
func pickBrokers(brokers cmap.ConcurrentMap[string, *BrokerStats], count int32) []string {
|
|
candidates := make([]string, 0, brokers.Count())
|
|
for brokerStatsItem := range brokers.IterBuffered() {
|
|
candidates = append(candidates, brokerStatsItem.Key)
|
|
}
|
|
pickedBrokers := make([]string, 0, count)
|
|
for i := int32(0); i < count; i++ {
|
|
p := rand.Int() % len(candidates)
|
|
if p < 0 {
|
|
p = -p
|
|
}
|
|
pickedBrokers = append(pickedBrokers, candidates[p])
|
|
}
|
|
return pickedBrokers
|
|
}
|