2023-12-12 04:05:54 +08:00
|
|
|
package broker
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
2024-01-09 15:27:02 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/glog"
|
2023-12-12 04:05:54 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
|
2024-04-13 13:29:53 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/schema"
|
2023-12-12 04:05:54 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/mq/topic"
|
2024-02-06 08:46:40 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
2023-12-12 04:05:54 +08:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
)
|
|
|
|
|
|
|
|
// ConfigureTopic Runs on any broker, but proxied to the balancer if not the balancer
|
|
|
|
// It generates an assignments based on existing allocations,
|
|
|
|
// and then assign the partitions to the brokers.
|
|
|
|
func (b *MessageQueueBroker) ConfigureTopic(ctx context.Context, request *mq_pb.ConfigureTopicRequest) (resp *mq_pb.ConfigureTopicResponse, err error) {
|
2024-02-06 08:46:40 +08:00
|
|
|
if !b.isLockOwner() {
|
|
|
|
proxyErr := b.withBrokerClient(false, pb.ServerAddress(b.lockAsBalancer.LockOwner()), func(client mq_pb.SeaweedMessagingClient) error {
|
2023-12-12 04:05:54 +08:00
|
|
|
resp, err = client.ConfigureTopic(ctx, request)
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if proxyErr != nil {
|
|
|
|
return nil, proxyErr
|
|
|
|
}
|
|
|
|
return resp, err
|
|
|
|
}
|
|
|
|
|
2024-04-13 13:29:53 +08:00
|
|
|
// validate the schema
|
|
|
|
if request.RecordType != nil {
|
|
|
|
if _, err = schema.NewSchema(request.RecordType); err != nil {
|
|
|
|
return nil, status.Errorf(codes.InvalidArgument, "invalid record type %+v: %v", request.RecordType, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-01-22 16:51:31 +08:00
|
|
|
t := topic.FromPbTopic(request.Topic)
|
2024-02-06 08:46:40 +08:00
|
|
|
var readErr, assignErr error
|
2024-05-14 23:50:17 +08:00
|
|
|
resp, readErr = b.fca.ReadTopicConfFromFiler(t)
|
2024-01-29 06:27:52 +08:00
|
|
|
if readErr != nil {
|
2024-02-06 08:46:40 +08:00
|
|
|
glog.V(0).Infof("read topic %s conf: %v", request.Topic, readErr)
|
|
|
|
}
|
|
|
|
|
|
|
|
if resp != nil {
|
|
|
|
assignErr = b.ensureTopicActiveAssignments(t, resp)
|
2024-01-23 03:04:47 +08:00
|
|
|
// no need to assign directly.
|
|
|
|
// The added or updated assignees will read from filer directly.
|
|
|
|
// The gone assignees will die by themselves.
|
2024-01-22 16:51:31 +08:00
|
|
|
}
|
2024-02-06 08:46:40 +08:00
|
|
|
|
|
|
|
if readErr == nil && assignErr == nil && len(resp.BrokerPartitionAssignments) == int(request.PartitionCount) {
|
2024-01-22 16:51:31 +08:00
|
|
|
glog.V(0).Infof("existing topic partitions %d: %+v", len(resp.BrokerPartitionAssignments), resp.BrokerPartitionAssignments)
|
2024-02-06 08:46:40 +08:00
|
|
|
return
|
|
|
|
}
|
2024-01-17 01:30:46 +08:00
|
|
|
|
2024-02-06 08:46:40 +08:00
|
|
|
if resp != nil && len(resp.BrokerPartitionAssignments) > 0 {
|
|
|
|
if cancelErr := b.assignTopicPartitionsToBrokers(ctx, request.Topic, resp.BrokerPartitionAssignments, false); cancelErr != nil {
|
|
|
|
glog.V(1).Infof("cancel old topic %s partitions assignments %v : %v", request.Topic, resp.BrokerPartitionAssignments, cancelErr)
|
2024-01-17 01:30:46 +08:00
|
|
|
}
|
2024-02-06 08:46:40 +08:00
|
|
|
}
|
|
|
|
resp = &mq_pb.ConfigureTopicResponse{}
|
|
|
|
if b.Balancer.Brokers.IsEmpty() {
|
|
|
|
return nil, status.Errorf(codes.Unavailable, pub_balancer.ErrNoBroker.Error())
|
|
|
|
}
|
|
|
|
resp.BrokerPartitionAssignments = pub_balancer.AllocateTopicPartitions(b.Balancer.Brokers, request.PartitionCount)
|
2024-04-13 13:29:53 +08:00
|
|
|
resp.RecordType = request.RecordType
|
2024-01-17 01:30:46 +08:00
|
|
|
|
2024-02-06 08:46:40 +08:00
|
|
|
// save the topic configuration on filer
|
2024-05-14 23:50:17 +08:00
|
|
|
if err := b.fca.SaveTopicConfToFiler(request.Topic, resp); err != nil {
|
2024-02-06 08:46:40 +08:00
|
|
|
return nil, fmt.Errorf("configure topic: %v", err)
|
2024-01-17 01:30:46 +08:00
|
|
|
}
|
2023-12-12 04:05:54 +08:00
|
|
|
|
2024-02-06 08:46:40 +08:00
|
|
|
b.Balancer.OnPartitionChange(request.Topic, resp.BrokerPartitionAssignments)
|
|
|
|
|
2024-01-23 01:00:22 +08:00
|
|
|
glog.V(0).Infof("ConfigureTopic: topic %s partition assignments: %v", request.Topic, resp.BrokerPartitionAssignments)
|
|
|
|
|
|
|
|
return resp, err
|
|
|
|
}
|