command_mq_topic_configure.go 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. package shell
  2. import (
  3. "context"
  4. "encoding/json"
  5. "flag"
  6. "fmt"
  7. "github.com/seaweedfs/seaweedfs/weed/pb"
  8. "github.com/seaweedfs/seaweedfs/weed/pb/mq_pb"
  9. "io"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandMqTopicConfigure{})
  13. }
  14. type commandMqTopicConfigure struct {
  15. }
  16. func (c *commandMqTopicConfigure) Name() string {
  17. return "mq.topic.configure"
  18. }
  19. func (c *commandMqTopicConfigure) Help() string {
  20. return `configure a topic with a given name
  21. Example:
  22. mq.topic.configure -namespace <namespace> -topic <topic_name> -partition_count <partition_count>
  23. `
  24. }
  25. func (c *commandMqTopicConfigure) HasTag(CommandTag) bool {
  26. return false
  27. }
  28. func (c *commandMqTopicConfigure) Do(args []string, commandEnv *CommandEnv, writer io.Writer) error {
  29. // parse parameters
  30. mqCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  31. namespace := mqCommand.String("namespace", "", "namespace name")
  32. topicName := mqCommand.String("topic", "", "topic name")
  33. partitionCount := mqCommand.Int("partitionCount", 6, "partition count")
  34. if err := mqCommand.Parse(args); err != nil {
  35. return err
  36. }
  37. // find the broker balancer
  38. brokerBalancer, err := findBrokerBalancer(commandEnv)
  39. if err != nil {
  40. return err
  41. }
  42. fmt.Fprintf(writer, "current balancer: %s\n", brokerBalancer)
  43. // create topic
  44. return pb.WithBrokerGrpcClient(false, brokerBalancer, commandEnv.option.GrpcDialOption, func(client mq_pb.SeaweedMessagingClient) error {
  45. resp, err := client.ConfigureTopic(context.Background(), &mq_pb.ConfigureTopicRequest{
  46. Topic: &mq_pb.Topic{
  47. Namespace: *namespace,
  48. Name: *topicName,
  49. },
  50. PartitionCount: int32(*partitionCount),
  51. })
  52. if err != nil {
  53. return err
  54. }
  55. output, _ := json.MarshalIndent(resp, "", " ")
  56. fmt.Fprintf(writer, "response:\n%+v\n", string(output))
  57. return nil
  58. })
  59. }