You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

52 lines
2.0 KiB

  1. package broker
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/glog"
  6. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  7. "github.com/seaweedfs/seaweedfs/weed/pb"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  9. )
  10. // AssignTopicPartitions Runs on the assigned broker, to execute the topic partition assignment
  11. func (b *MessageQueueBroker) AssignTopicPartitions(c context.Context, request *mq_pb.AssignTopicPartitionsRequest) (*mq_pb.AssignTopicPartitionsResponse, error) {
  12. ret := &mq_pb.AssignTopicPartitionsResponse{}
  13. self := pb.ServerAddress(fmt.Sprintf("%s:%d", b.option.Ip, b.option.Port))
  14. // drain existing topic partition subscriptions
  15. for _, assignment := range request.BrokerPartitionAssignments {
  16. t := topic.FromPbTopic(request.Topic)
  17. partition := topic.FromPbPartition(assignment.Partition)
  18. b.accessLock.Lock()
  19. if request.IsDraining {
  20. // TODO drain existing topic partition subscriptions
  21. b.localTopicManager.RemoveTopicPartition(t, partition)
  22. } else {
  23. var localPartition *topic.LocalPartition
  24. if localPartition = b.localTopicManager.GetTopicPartition(t, partition); localPartition == nil {
  25. localPartition = topic.FromPbBrokerPartitionAssignment(self, partition, assignment, b.genLogFlushFunc(t, assignment.Partition), b.genLogOnDiskReadFunc(t, assignment.Partition))
  26. b.localTopicManager.AddTopicPartition(t, localPartition)
  27. }
  28. }
  29. b.accessLock.Unlock()
  30. }
  31. // if is leader, notify the followers to drain existing topic partition subscriptions
  32. if request.IsLeader {
  33. for _, brokerPartition := range request.BrokerPartitionAssignments {
  34. for _, follower := range brokerPartition.FollowerBrokers {
  35. err := pb.WithBrokerGrpcClient(false, follower, b.grpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
  36. _, err := client.AssignTopicPartitions(context.Background(), request)
  37. return err
  38. })
  39. if err != nil {
  40. return ret, err
  41. }
  42. }
  43. }
  44. }
  45. glog.V(0).Infof("AssignTopicPartitions: topic %s partition assignments: %v", request.Topic, request.BrokerPartitionAssignments)
  46. return ret, nil
  47. }