command_mq_topic_list.go 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. package shell
  2. import (
  3. "context"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/mq/pub_balancer"
  6. "github.com/seaweedfs/seaweedfs/weed/pb"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  9. "io"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandMqTopicList{})
  13. }
  14. type commandMqTopicList struct {
  15. }
  16. func (c *commandMqTopicList) Name() string {
  17. return "mq.topic.list"
  18. }
  19. func (c *commandMqTopicList) Help() string {
  20. return `print out all topics`
  21. }
  22. func (c *commandMqTopicList) HasTag(CommandTag) bool {
  23. return false
  24. }
  25. func (c *commandMqTopicList) Do(args []string, commandEnv *CommandEnv, writer io.Writer) error {
  26. brokerBalancer, err := findBrokerBalancer(commandEnv)
  27. if err != nil {
  28. return err
  29. }
  30. fmt.Fprintf(writer, "current balancer: %s\n", brokerBalancer)
  31. return pb.WithBrokerGrpcClient(false, brokerBalancer, commandEnv.option.GrpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
  32. resp, err := client.ListTopics(context.Background(), &mq_pb.ListTopicsRequest{})
  33. if err != nil {
  34. return err
  35. }
  36. if len(resp.Topics) == 0 {
  37. fmt.Fprintf(writer, "no topics found\n")
  38. return nil
  39. }
  40. for _, topic := range resp.Topics {
  41. fmt.Fprintf(writer, " %+v\n", topic)
  42. }
  43. return nil
  44. })
  45. }
  46. func findBrokerBalancer(commandEnv *CommandEnv) (brokerBalancer string, err error) {
  47. err = commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  48. resp, err := client.FindLockOwner(context.Background(), &filer_pb.FindLockOwnerRequest{
  49. Name: pub_balancer.LockBrokerBalancer,
  50. })
  51. if err != nil {
  52. return fmt.Errorf("FindLockOwner: %v", err)
  53. }
  54. brokerBalancer = resp.Owner
  55. return nil
  56. })
  57. return
  58. }