2023-09-24 22:26:49 +00:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2023-09-25 06:05:41 +00:00
|
|
|
"fmt"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/balancer"
|
2023-09-24 22:26:49 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
2023-09-25 04:19:51 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
2023-09-24 22:26:49 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
2023-09-26 22:17:33 +00:00
|
|
|
// ConfigureTopic Runs on any broker, but proxied to the balancer if not the balancer
|
|
|
|
func (broker *MessageQueueBroker) ConfigureTopic(ctx context.Context, request *mq_pb.ConfigureTopicRequest) (resp *mq_pb.ConfigureTopicResponse, err error) {
|
2023-09-24 22:26:49 +00:00
|
|
|
if broker.currentBalancer == "" {
|
|
|
|
return nil, status.Errorf(codes.Unavailable, "no balancer")
|
|
|
|
}
|
|
|
|
if !broker.lockAsBalancer.IsLocked() {
|
|
|
|
proxyErr := broker.withBrokerClient(false, broker.currentBalancer, func(client mq_pb.SeaweedMessagingClient) error {
|
2023-09-26 22:17:33 +00:00
|
|
|
resp, err = client.ConfigureTopic(ctx, request)
|
2023-09-24 22:26:49 +00:00
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if proxyErr != nil {
|
|
|
|
return nil, proxyErr
|
|
|
|
}
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2023-09-26 22:17:33 +00:00
|
|
|
ret := &mq_pb.ConfigureTopicResponse{}
|
2023-09-24 22:26:49 +00:00
|
|
|
ret.BrokerPartitionAssignments, err = broker.Balancer.LookupOrAllocateTopicPartitions(request.Topic, true, request.PartitionCount)
|
|
|
|
|
2023-09-25 04:19:51 +00:00
|
|
|
for _, bpa := range ret.BrokerPartitionAssignments {
|
2023-09-25 06:05:41 +00:00
|
|
|
// fmt.Printf("create topic %s on %s\n", request.Topic, bpa.LeaderBroker)
|
2023-09-25 04:19:51 +00:00
|
|
|
if doCreateErr := broker.withBrokerClient(false, pb.ServerAddress(bpa.LeaderBroker), func(client mq_pb.SeaweedMessagingClient) error {
|
2023-09-26 22:17:33 +00:00
|
|
|
_, doCreateErr := client.DoConfigureTopic(ctx, &mq_pb.DoConfigureTopicRequest{
|
2023-09-25 04:19:51 +00:00
|
|
|
Topic: request.Topic,
|
|
|
|
Partition: bpa.Partition,
|
|
|
|
})
|
2023-09-25 06:05:41 +00:00
|
|
|
if doCreateErr != nil {
|
|
|
|
return fmt.Errorf("do create topic %s on %s: %v", request.Topic, bpa.LeaderBroker, doCreateErr)
|
|
|
|
}
|
|
|
|
brokerStats, found := broker.Balancer.Brokers.Get(bpa.LeaderBroker)
|
|
|
|
if !found {
|
|
|
|
brokerStats = balancer.NewBrokerStats()
|
2023-09-25 06:34:31 +00:00
|
|
|
if !broker.Balancer.Brokers.SetIfAbsent(bpa.LeaderBroker, brokerStats) {
|
|
|
|
brokerStats, _ = broker.Balancer.Brokers.Get(bpa.LeaderBroker)
|
|
|
|
}
|
2023-09-25 06:05:41 +00:00
|
|
|
}
|
|
|
|
brokerStats.RegisterAssignment(request.Topic, bpa.Partition)
|
|
|
|
return nil
|
2023-09-25 04:19:51 +00:00
|
|
|
}); doCreateErr != nil {
|
|
|
|
return nil, doCreateErr
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 06:34:31 +00:00
|
|
|
// TODO revert if some error happens in the middle of the assignments
|
|
|
|
|
2023-09-24 22:26:49 +00:00
|
|
|
return ret, err
|
|
|
|
}
|
|
|
|
|
2023-09-26 22:17:33 +00:00
|
|
|
func (broker *MessageQueueBroker) DoConfigureTopic(ctx context.Context, req *mq_pb.DoConfigureTopicRequest) (resp *mq_pb.DoConfigureTopicResponse, err error) {
|
|
|
|
ret := &mq_pb.DoConfigureTopicResponse{}
|
2023-09-24 22:26:49 +00:00
|
|
|
t, p := topic.FromPbTopic(req.Topic), topic.FromPbPartition(req.Partition)
|
|
|
|
localTopicPartition := broker.localTopicManager.GetTopicPartition(t, p)
|
|
|
|
if localTopicPartition == nil {
|
|
|
|
localTopicPartition = topic.NewLocalPartition(t, p, true, nil)
|
|
|
|
broker.localTopicManager.AddTopicPartition(t, localTopicPartition)
|
|
|
|
}
|
|
|
|
|
|
|
|
return ret, err
|
|
|
|
}
|