broker_grpc_lookup.go 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. package broker
  2. import (
  3. "context"
  4. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  5. "google.golang.org/grpc/codes"
  6. "google.golang.org/grpc/status"
  7. )
  8. // FindTopicBrokers returns the brokers that are serving the topic
  9. //
  10. // 1. lock the topic
  11. //
  12. // 2. find the topic partitions on the filer
  13. // 2.1 if the topic is not found, return error
  14. // 2.2 if the request is_for_publish, create the topic
  15. // 2.2.1 if the request is_for_subscribe, return error not found
  16. // 2.2.2 if the request is_for_publish, create the topic
  17. // 2.2 if the topic is found, return the brokers
  18. //
  19. // 3. unlock the topic
  20. func (broker *MessageQueueBroker) LookupTopicBrokers(ctx context.Context, request *mq_pb.LookupTopicBrokersRequest) (resp *mq_pb.LookupTopicBrokersResponse, err error) {
  21. if broker.currentBalancer == "" {
  22. return nil, status.Errorf(codes.Unavailable, "no balancer")
  23. }
  24. if !broker.lockAsBalancer.IsLocked() {
  25. proxyErr := broker.withBrokerClient(false, broker.currentBalancer, func(client mq_pb.SeaweedMessagingClient) error {
  26. resp, err = client.LookupTopicBrokers(ctx, request)
  27. return nil
  28. })
  29. if proxyErr != nil {
  30. return nil, proxyErr
  31. }
  32. return resp, err
  33. }
  34. ret := &mq_pb.LookupTopicBrokersResponse{}
  35. ret.Topic = request.Topic
  36. ret.BrokerPartitionAssignments, err = broker.Balancer.LookupOrAllocateTopicPartitions(ret.Topic, request.IsForPublish, 6)
  37. return ret, err
  38. }
  39. // CheckTopicPartitionsStatus check the topic partitions on the broker
  40. func (broker *MessageQueueBroker) CheckTopicPartitionsStatus(c context.Context, request *mq_pb.CheckTopicPartitionsStatusRequest) (*mq_pb.CheckTopicPartitionsStatusResponse, error) {
  41. ret := &mq_pb.CheckTopicPartitionsStatusResponse{}
  42. return ret, nil
  43. }
  44. func (broker *MessageQueueBroker) ListTopics(ctx context.Context, request *mq_pb.ListTopicsRequest) (resp *mq_pb.ListTopicsResponse, err error) {
  45. if broker.currentBalancer == "" {
  46. return nil, status.Errorf(codes.Unavailable, "no balancer")
  47. }
  48. if !broker.lockAsBalancer.IsLocked() {
  49. proxyErr := broker.withBrokerClient(false, broker.currentBalancer, func(client mq_pb.SeaweedMessagingClient) error {
  50. resp, err = client.ListTopics(ctx, request)
  51. return nil
  52. })
  53. if proxyErr != nil {
  54. return nil, proxyErr
  55. }
  56. return resp, err
  57. }
  58. ret := &mq_pb.ListTopicsResponse{}
  59. knownTopics := make(map[*mq_pb.Topic]struct{})
  60. for brokerStatsItem := range broker.Balancer.Brokers.IterBuffered() {
  61. _, brokerStats := brokerStatsItem.Key, brokerStatsItem.Val
  62. for topicPartitionStatsItem := range brokerStats.Stats.IterBuffered() {
  63. topicPartitionStat := topicPartitionStatsItem.Val
  64. topic := &mq_pb.Topic{
  65. Namespace: topicPartitionStat.TopicPartition.Namespace,
  66. Name: topicPartitionStat.TopicPartition.Topic,
  67. }
  68. if _, found := knownTopics[topic]; found {
  69. continue
  70. }
  71. ret.Topics = append(ret.Topics, topic)
  72. }
  73. }
  74. return ret, nil
  75. }