command_mq_topic_truncate.go 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "strings"
  8. "github.com/seaweedfs/seaweedfs/weed/mq/topic"
  9. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  10. "github.com/seaweedfs/seaweedfs/weed/util"
  11. )
  12. func init() {
  13. Commands = append(Commands, &commandMqTopicTruncate{})
  14. }
  15. type commandMqTopicTruncate struct {
  16. }
  17. func (c *commandMqTopicTruncate) Name() string {
  18. return "mq.topic.truncate"
  19. }
  20. func (c *commandMqTopicTruncate) Help() string {
  21. return `clear all data from a topic while preserving topic structure
  22. Example:
  23. mq.topic.truncate -namespace <namespace> -topic <topic_name>
  24. This command removes all log files and parquet files from all partitions
  25. of the specified topic, while keeping the topic configuration intact.
  26. `
  27. }
  28. func (c *commandMqTopicTruncate) HasTag(CommandTag) bool {
  29. return false
  30. }
  31. func (c *commandMqTopicTruncate) Do(args []string, commandEnv *CommandEnv, writer io.Writer) error {
  32. // parse parameters
  33. mqCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  34. namespace := mqCommand.String("namespace", "", "namespace name")
  35. topicName := mqCommand.String("topic", "", "topic name")
  36. if err := mqCommand.Parse(args); err != nil {
  37. return err
  38. }
  39. if *namespace == "" {
  40. return fmt.Errorf("namespace is required")
  41. }
  42. if *topicName == "" {
  43. return fmt.Errorf("topic name is required")
  44. }
  45. // Verify topic exists by trying to read its configuration
  46. t := topic.NewTopic(*namespace, *topicName)
  47. err := commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  48. _, err := t.ReadConfFile(client)
  49. if err != nil {
  50. return fmt.Errorf("topic %s.%s does not exist or cannot be read: %v", *namespace, *topicName, err)
  51. }
  52. return nil
  53. })
  54. if err != nil {
  55. return err
  56. }
  57. fmt.Fprintf(writer, "Truncating topic %s.%s...\n", *namespace, *topicName)
  58. // Discover and clear all partitions using centralized logic
  59. partitions, err := t.DiscoverPartitions(context.Background(), commandEnv)
  60. if err != nil {
  61. return fmt.Errorf("failed to discover topic partitions: %v", err)
  62. }
  63. if len(partitions) == 0 {
  64. fmt.Fprintf(writer, "No partitions found for topic %s.%s\n", *namespace, *topicName)
  65. return nil
  66. }
  67. fmt.Fprintf(writer, "Found %d partitions, clearing data...\n", len(partitions))
  68. // Clear data from each partition
  69. totalFilesDeleted := 0
  70. for _, partitionPath := range partitions {
  71. filesDeleted, err := c.clearPartitionData(commandEnv, partitionPath, writer)
  72. if err != nil {
  73. fmt.Fprintf(writer, "Warning: failed to clear partition %s: %v\n", partitionPath, err)
  74. continue
  75. }
  76. totalFilesDeleted += filesDeleted
  77. fmt.Fprintf(writer, "Cleared partition: %s (%d files)\n", partitionPath, filesDeleted)
  78. }
  79. fmt.Fprintf(writer, "Successfully truncated topic %s.%s - deleted %d files from %d partitions\n",
  80. *namespace, *topicName, totalFilesDeleted, len(partitions))
  81. return nil
  82. }
  83. // clearPartitionData deletes all data files (log files, parquet files) from a partition directory
  84. // Returns the number of files deleted
  85. func (c *commandMqTopicTruncate) clearPartitionData(commandEnv *CommandEnv, partitionPath string, writer io.Writer) (int, error) {
  86. filesDeleted := 0
  87. err := filer_pb.ReadDirAllEntries(context.Background(), commandEnv, util.FullPath(partitionPath), "", func(entry *filer_pb.Entry, isLast bool) error {
  88. if entry.IsDirectory {
  89. return nil // Skip subdirectories
  90. }
  91. fileName := entry.Name
  92. // Preserve configuration files
  93. if strings.HasSuffix(fileName, ".conf") ||
  94. strings.HasSuffix(fileName, ".config") ||
  95. fileName == "topic.conf" ||
  96. fileName == "partition.conf" {
  97. fmt.Fprintf(writer, " Preserving config file: %s\n", fileName)
  98. return nil
  99. }
  100. // Delete all data files (log files, parquet files, offset files, etc.)
  101. deleteErr := filer_pb.Remove(context.Background(), commandEnv, partitionPath, fileName, false, true, true, false, nil)
  102. if deleteErr != nil {
  103. fmt.Fprintf(writer, " Warning: failed to delete %s/%s: %v\n", partitionPath, fileName, deleteErr)
  104. // Continue with other files rather than failing entirely
  105. } else {
  106. fmt.Fprintf(writer, " Deleted: %s\n", fileName)
  107. filesDeleted++
  108. }
  109. return nil
  110. })
  111. return filesDeleted, err
  112. }