2023-12-12 04:05:54 +08:00
|
|
|
package pub_balancer
|
2023-09-19 09:47:34 +08:00
|
|
|
|
|
|
|
import (
|
2023-09-25 06:08:44 +08:00
|
|
|
"errors"
|
2023-12-12 04:05:54 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2023-09-19 09:47:34 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
)
|
|
|
|
|
2023-09-25 06:08:44 +08:00
|
|
|
var (
|
|
|
|
ErrNoBroker = errors.New("no broker")
|
|
|
|
)
|
|
|
|
|
2023-12-12 04:05:54 +08:00
|
|
|
func (balancer *Balancer) LookupOrAllocateTopicPartitions(topic *mq_pb.Topic, publish bool, partitionCount int32) (assignments []*mq_pb.BrokerPartitionAssignment, err error) {
|
|
|
|
if partitionCount == 0 {
|
|
|
|
partitionCount = 6
|
|
|
|
}
|
2023-09-19 09:47:34 +08:00
|
|
|
// find existing topic partition assignments
|
2023-12-12 04:05:54 +08:00
|
|
|
for brokerStatsItem := range balancer.Brokers.IterBuffered() {
|
2023-09-19 09:47:34 +08:00
|
|
|
broker, brokerStats := brokerStatsItem.Key, brokerStatsItem.Val
|
2023-12-12 04:05:54 +08:00
|
|
|
for topicPartitionStatsItem := range brokerStats.TopicPartitionStats.IterBuffered() {
|
2023-09-19 09:47:34 +08:00
|
|
|
topicPartitionStat := topicPartitionStatsItem.Val
|
|
|
|
if topicPartitionStat.TopicPartition.Namespace == topic.Namespace &&
|
2023-10-02 16:01:45 +08:00
|
|
|
topicPartitionStat.TopicPartition.Name == topic.Name {
|
2023-09-19 09:47:34 +08:00
|
|
|
assignment := &mq_pb.BrokerPartitionAssignment{
|
|
|
|
Partition: &mq_pb.Partition{
|
|
|
|
RingSize: MaxPartitionCount,
|
|
|
|
RangeStart: topicPartitionStat.RangeStart,
|
|
|
|
RangeStop: topicPartitionStat.RangeStop,
|
|
|
|
},
|
|
|
|
}
|
2023-09-25 14:05:41 +08:00
|
|
|
// TODO fix follower setting
|
|
|
|
assignment.LeaderBroker = broker
|
2023-09-19 09:47:34 +08:00
|
|
|
assignments = append(assignments, assignment)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
if len(assignments) > 0 && len(assignments) == int(partitionCount) || !publish {
|
|
|
|
glog.V(0).Infof("existing topic partitions %d: %v", len(assignments), assignments)
|
2023-09-19 09:47:34 +08:00
|
|
|
return assignments, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// find the topic partitions on the filer
|
|
|
|
// if the topic is not found
|
|
|
|
// if the request is_for_publish
|
|
|
|
// create the topic
|
|
|
|
// if the request is_for_subscribe
|
|
|
|
// return error not found
|
|
|
|
// t := topic.FromPbTopic(request.Topic)
|
2023-12-12 04:05:54 +08:00
|
|
|
if balancer.Brokers.IsEmpty() {
|
2023-09-25 06:08:44 +08:00
|
|
|
return nil, ErrNoBroker
|
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
return allocateTopicPartitions(balancer.Brokers, partitionCount), nil
|
2023-09-19 09:47:34 +08:00
|
|
|
}
|