balance_action.go 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. package pub_balancer
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  7. "google.golang.org/grpc"
  8. )
  9. // Balancer <= PublisherToPubBalancer() <= Broker <=> Publish()
  10. // ExecuteBalanceActionMove from Balancer => AssignTopicPartitions() => Broker => Publish()
  11. func (balancer *Balancer) ExecuteBalanceActionMove(move *BalanceActionMove, grpcDialOption grpc.DialOption) error {
  12. if _, found := balancer.Brokers.Get(move.SourceBroker); !found {
  13. return fmt.Errorf("source broker %s not found", move.SourceBroker)
  14. }
  15. if _, found := balancer.Brokers.Get(move.TargetBroker); !found {
  16. return fmt.Errorf("target broker %s not found", move.TargetBroker)
  17. }
  18. err := pb.WithBrokerGrpcClient(false, move.TargetBroker, grpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
  19. _, err := client.AssignTopicPartitions(context.Background(), &mq_pb.AssignTopicPartitionsRequest{
  20. Topic: move.TopicPartition.Topic.ToPbTopic(),
  21. BrokerPartitionAssignments: []*mq_pb.BrokerPartitionAssignment{
  22. {
  23. Partition: move.TopicPartition.ToPbPartition(),
  24. },
  25. },
  26. IsLeader: true,
  27. IsDraining: false,
  28. })
  29. return err
  30. })
  31. if err != nil {
  32. return fmt.Errorf("assign topic partition %v to %s: %v", move.TopicPartition, move.TargetBroker, err)
  33. }
  34. err = pb.WithBrokerGrpcClient(false, move.SourceBroker, grpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
  35. _, err := client.AssignTopicPartitions(context.Background(), &mq_pb.AssignTopicPartitionsRequest{
  36. Topic: move.TopicPartition.Topic.ToPbTopic(),
  37. BrokerPartitionAssignments: []*mq_pb.BrokerPartitionAssignment{
  38. {
  39. Partition: move.TopicPartition.ToPbPartition(),
  40. },
  41. },
  42. IsLeader: true,
  43. IsDraining: true,
  44. })
  45. return err
  46. })
  47. if err != nil {
  48. return fmt.Errorf("assign topic partition %v to %s: %v", move.TopicPartition, move.SourceBroker, err)
  49. }
  50. return nil
  51. }