command_fs_meta_change_volume_id.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  32. fsMetaChangeVolumeIdCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  33. dir := fsMetaChangeVolumeIdCommand.String("dir", "/", "fix all metadata under this folder")
  34. mappingFileName := fsMetaChangeVolumeIdCommand.String("mapping", "", "a file with multiple volume id changes, with each line as x=>y")
  35. fromVolumeId := fsMetaChangeVolumeIdCommand.Uint("fromVolumeId", 0, "change metadata with this volume id")
  36. toVolumeId := fsMetaChangeVolumeIdCommand.Uint("toVolumeId", 0, "change metadata to this volume id")
  37. isForce := fsMetaChangeVolumeIdCommand.Bool("force", false, "applying the metadata changes")
  38. if err = fsMetaChangeVolumeIdCommand.Parse(args); err != nil {
  39. return err
  40. }
  41. // load the mapping
  42. mapping := make(map[needle.VolumeId]needle.VolumeId)
  43. if *mappingFileName != "" {
  44. readMappingFromFile(*mappingFileName, mapping)
  45. } else {
  46. if *fromVolumeId == *toVolumeId {
  47. return fmt.Errorf("no volume id changes")
  48. }
  49. if *fromVolumeId == 0 || *toVolumeId == 0 {
  50. return fmt.Errorf("volume id can not be zero")
  51. }
  52. mapping[needle.VolumeId(*fromVolumeId)] = needle.VolumeId(*toVolumeId)
  53. }
  54. return commandEnv.WithFilerClient(false, func(client filer_pb.SeaweedFilerClient) error {
  55. return filer_pb.TraverseBfs(commandEnv, util.FullPath(*dir), func(parentPath util.FullPath, entry *filer_pb.Entry) {
  56. if !entry.IsDirectory {
  57. var hasChanges bool
  58. for _, chunk := range entry.Chunks {
  59. if chunk.IsChunkManifest {
  60. fmt.Printf("Change volume id for large file is not implemented yet: %s/%s\n", parentPath, entry.Name)
  61. return
  62. }
  63. chunkVolumeId := chunk.Fid.VolumeId
  64. if toVolumeId, found := mapping[needle.VolumeId(chunkVolumeId)]; found {
  65. hasChanges = true
  66. chunk.Fid.VolumeId = uint32(toVolumeId)
  67. chunk.FileId = ""
  68. }
  69. }
  70. if hasChanges {
  71. println("Updating", parentPath, entry.Name)
  72. if *isForce {
  73. if updateErr := filer_pb.UpdateEntry(client, &filer_pb.UpdateEntryRequest{
  74. Directory: string(parentPath),
  75. Entry: entry,
  76. }); updateErr != nil {
  77. fmt.Printf("failed to update %s/%s: %v\n", parentPath, entry.Name, updateErr)
  78. }
  79. }
  80. }
  81. }
  82. })
  83. })
  84. }
  85. func readMappingFromFile(filename string, mapping map[needle.VolumeId]needle.VolumeId) error {
  86. mappingFile, openErr := os.Open(filename)
  87. if openErr != nil {
  88. return fmt.Errorf("failed to open file %s: %v", filename, openErr)
  89. }
  90. defer mappingFile.Close()
  91. mappingContent, readErr := io.ReadAll(mappingFile)
  92. if readErr != nil {
  93. return fmt.Errorf("failed to read file %s: %v", filename, readErr)
  94. }
  95. for _, line := range strings.Split(string(mappingContent), "\n") {
  96. parts := strings.Split(line, "=>")
  97. if len(parts) != 2 {
  98. println("unrecognized line:", line)
  99. continue
  100. }
  101. x, errX := strconv.ParseUint(strings.TrimSpace(parts[0]), 10, 64)
  102. if errX != nil {
  103. return errX
  104. }
  105. y, errY := strconv.ParseUint(strings.TrimSpace(parts[1]), 10, 64)
  106. if errY != nil {
  107. return errY
  108. }
  109. mapping[needle.VolumeId(x)] = needle.VolumeId(y)
  110. }
  111. return nil
  112. }