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.

129 lines
4.0 KiB

3 months ago
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/filer_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  7. "github.com/seaweedfs/seaweedfs/weed/util"
  8. "io"
  9. "os"
  10. "strconv"
  11. "strings"
  12. )
  13. func init() {
  14. Commands = append(Commands, &commandFsMetaChangeVolumeId{})
  15. }
  16. type commandFsMetaChangeVolumeId struct {
  17. }
  18. func (c *commandFsMetaChangeVolumeId) Name() string {
  19. return "fs.meta.changeVolumeId"
  20. }
  21. func (c *commandFsMetaChangeVolumeId) Help() string {
  22. return `change volume id in existing metadata.
  23. fs.meta.changeVolumeId -dir=/path/to/a/dir -fromVolumeId=x -toVolumeId=y -force
  24. fs.meta.changeVolumeId -dir=/path/to/a/dir -mapping=/path/to/mapping/file -force
  25. The mapping file should have these lines, each line is: [fromVolumeId]=>[toVolumeId]
  26. e.g.
  27. 1 => 2
  28. 3 => 4
  29. `
  30. }
  31. func (c *commandFsMetaChangeVolumeId) HasTag(CommandTag) bool {
  32. return false
  33. }
  34. func (c *commandFsMetaChangeVolumeId) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  35. fsMetaChangeVolumeIdCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  36. dir := fsMetaChangeVolumeIdCommand.String("dir", "/", "fix all metadata under this folder")
  37. mappingFileName := fsMetaChangeVolumeIdCommand.String("mapping", "", "a file with multiple volume id changes, with each line as x=>y")
  38. fromVolumeId := fsMetaChangeVolumeIdCommand.Uint("fromVolumeId", 0, "change metadata with this volume id")
  39. toVolumeId := fsMetaChangeVolumeIdCommand.Uint("toVolumeId", 0, "change metadata to this volume id")
  40. isForce := fsMetaChangeVolumeIdCommand.Bool("force", false, "applying the metadata changes")
  41. if err = fsMetaChangeVolumeIdCommand.Parse(args); err != nil {
  42. return err
  43. }
  44. // load the mapping
  45. mapping := make(map[needle.VolumeId]needle.VolumeId)
  46. if *mappingFileName != "" {
  47. readMappingFromFile(*mappingFileName, mapping)
  48. } else {
  49. if *fromVolumeId == *toVolumeId {
  50. return fmt.Errorf("no volume id changes")
  51. }
  52. if *fromVolumeId == 0 || *toVolumeId == 0 {
  53. return fmt.Errorf("volume id can not be zero")
  54. }
  55. mapping[needle.VolumeId(*fromVolumeId)] = needle.VolumeId(*toVolumeId)
  56. }
  57. return commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  58. return filer_pb.TraverseBfs(commandEnv, util.FullPath(*dir), func(parentPath util.FullPath, entry *filer_pb.Entry) {
  59. if !entry.IsDirectory {
  60. var hasChanges bool
  61. for _, chunk := range entry.Chunks {
  62. if chunk.IsChunkManifest {
  63. fmt.Printf("Change volume id for large file is not implemented yet: %s/%s\n", parentPath, entry.Name)
  64. return
  65. }
  66. chunkVolumeId := chunk.Fid.VolumeId
  67. if toVolumeId, found := mapping[needle.VolumeId(chunkVolumeId)]; found {
  68. hasChanges = true
  69. chunk.Fid.VolumeId = uint32(toVolumeId)
  70. chunk.FileId = ""
  71. }
  72. }
  73. if hasChanges {
  74. println("Updating", parentPath, entry.Name)
  75. if *isForce {
  76. if updateErr := filer_pb.UpdateEntry(client, &filer_pb.UpdateEntryRequest{
  77. Directory: string(parentPath),
  78. Entry: entry,
  79. }); updateErr != nil {
  80. fmt.Printf("failed to update %s/%s: %v\n", parentPath, entry.Name, updateErr)
  81. }
  82. }
  83. }
  84. }
  85. })
  86. })
  87. }
  88. func readMappingFromFile(filename string, mapping map[needle.VolumeId]needle.VolumeId) error {
  89. mappingFile, openErr := os.Open(filename)
  90. if openErr != nil {
  91. return fmt.Errorf("failed to open file %s: %v", filename, openErr)
  92. }
  93. defer mappingFile.Close()
  94. mappingContent, readErr := io.ReadAll(mappingFile)
  95. if readErr != nil {
  96. return fmt.Errorf("failed to read file %s: %v", filename, readErr)
  97. }
  98. for _, line := range strings.Split(string(mappingContent), "\n") {
  99. parts := strings.Split(line, "=>")
  100. if len(parts) != 2 {
  101. println("unrecognized line:", line)
  102. continue
  103. }
  104. x, errX := strconv.ParseUint(strings.TrimSpace(parts[0]), 10, 64)
  105. if errX != nil {
  106. return errX
  107. }
  108. y, errY := strconv.ParseUint(strings.TrimSpace(parts[1]), 10, 64)
  109. if errY != nil {
  110. return errY
  111. }
  112. mapping[needle.VolumeId(x)] = needle.VolumeId(y)
  113. }
  114. return nil
  115. }