fix.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. package command
  2. import (
  3. "fmt"
  4. "io/fs"
  5. "os"
  6. "path"
  7. "strconv"
  8. "strings"
  9. "github.com/seaweedfs/seaweedfs/weed/glog"
  10. "github.com/seaweedfs/seaweedfs/weed/storage"
  11. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  12. "github.com/seaweedfs/seaweedfs/weed/storage/needle_map"
  13. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  14. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  15. "github.com/seaweedfs/seaweedfs/weed/util"
  16. )
  17. func init() {
  18. cmdFix.Run = runFix // break init cycle
  19. }
  20. var cmdFix = &Command{
  21. UsageLine: "fix [-volumeId=234] [-collection=bigData] /tmp",
  22. Short: "run weed tool fix on files or whole folders to recreate index file(s) if corrupted",
  23. Long: `Fix runs the SeaweedFS fix command on dat files or whole folders to re-create the index .idx file.
  24. `,
  25. }
  26. var (
  27. fixVolumeCollection = cmdFix.Flag.String("collection", "", "an optional volume collection name, if specified only it will be processed")
  28. fixVolumeId = cmdFix.Flag.Int64("volumeId", 0, "an optional volume id, if not 0 (default) only it will be processed")
  29. )
  30. type VolumeFileScanner4Fix struct {
  31. version needle.Version
  32. nm *needle_map.MemDb
  33. }
  34. func (scanner *VolumeFileScanner4Fix) VisitSuperBlock(superBlock super_block.SuperBlock) error {
  35. scanner.version = superBlock.Version
  36. return nil
  37. }
  38. func (scanner *VolumeFileScanner4Fix) ReadNeedleBody() bool {
  39. return false
  40. }
  41. func (scanner *VolumeFileScanner4Fix) VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error {
  42. glog.V(2).Infof("key %d offset %d size %d disk_size %d compressed %v", n.Id, offset, n.Size, n.DiskSize(scanner.version), n.IsCompressed())
  43. if n.Size.IsValid() {
  44. pe := scanner.nm.Set(n.Id, types.ToOffset(offset), n.Size)
  45. glog.V(2).Infof("saved %d with error %v", n.Size, pe)
  46. } else {
  47. glog.V(2).Infof("skipping deleted file ...")
  48. return scanner.nm.Delete(n.Id)
  49. }
  50. return nil
  51. }
  52. func runFix(cmd *Command, args []string) bool {
  53. for _, arg := range args {
  54. basePath, f := path.Split(util.ResolvePath(arg))
  55. if util.FolderExists(arg) {
  56. basePath = arg
  57. f = ""
  58. }
  59. files := []fs.DirEntry{}
  60. if f == "" {
  61. fileInfo, err := os.ReadDir(basePath)
  62. if err != nil {
  63. fmt.Println(err)
  64. return false
  65. }
  66. files = fileInfo
  67. } else {
  68. fileInfo, err := os.Stat(arg)
  69. if err != nil {
  70. fmt.Println(err)
  71. return false
  72. }
  73. files = []fs.DirEntry{fs.FileInfoToDirEntry(fileInfo)}
  74. }
  75. for _, file := range files {
  76. if !strings.HasSuffix(file.Name(), ".dat") {
  77. continue
  78. }
  79. if *fixVolumeCollection != "" {
  80. if !strings.HasPrefix(file.Name(), *fixVolumeCollection+"_") {
  81. continue
  82. }
  83. }
  84. baseFileName := file.Name()[:len(file.Name())-4]
  85. collection, volumeIdStr := "", baseFileName
  86. if sepIndex := strings.LastIndex(baseFileName, "_"); sepIndex > 0 {
  87. collection = baseFileName[:sepIndex]
  88. volumeIdStr = baseFileName[sepIndex+1:]
  89. }
  90. volumeId, parseErr := strconv.ParseInt(volumeIdStr, 10, 64)
  91. if parseErr != nil {
  92. fmt.Printf("Failed to parse volume id from %s: %v\n", baseFileName, parseErr)
  93. return false
  94. }
  95. if *fixVolumeId != 0 && *fixVolumeId != volumeId {
  96. continue
  97. }
  98. doFixOneVolume(basePath, baseFileName, collection, volumeId)
  99. }
  100. }
  101. return true
  102. }
  103. func doFixOneVolume(basepath string, baseFileName string, collection string, volumeId int64) {
  104. indexFileName := path.Join(basepath, baseFileName+".idx")
  105. nm := needle_map.NewMemDb()
  106. defer nm.Close()
  107. vid := needle.VolumeId(volumeId)
  108. scanner := &VolumeFileScanner4Fix{
  109. nm: nm,
  110. }
  111. if err := storage.ScanVolumeFile(basepath, collection, vid, storage.NeedleMapInMemory, scanner); err != nil {
  112. glog.Fatalf("scan .dat File: %v", err)
  113. }
  114. if err := nm.SaveToIdx(indexFileName); err != nil {
  115. os.Remove(indexFileName)
  116. glog.Fatalf("save to .idx File: %v", err)
  117. }
  118. }