fix.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. fixIgnoreError = cmdFix.Flag.Bool("ignoreError", false, "an optional, if true will be processed despite errors")
  30. )
  31. type VolumeFileScanner4Fix struct {
  32. version needle.Version
  33. nm *needle_map.MemDb
  34. }
  35. func (scanner *VolumeFileScanner4Fix) VisitSuperBlock(superBlock super_block.SuperBlock) error {
  36. scanner.version = superBlock.Version
  37. return nil
  38. }
  39. func (scanner *VolumeFileScanner4Fix) ReadNeedleBody() bool {
  40. return false
  41. }
  42. func (scanner *VolumeFileScanner4Fix) VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error {
  43. 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())
  44. if n.Size.IsValid() {
  45. pe := scanner.nm.Set(n.Id, types.ToOffset(offset), n.Size)
  46. glog.V(2).Infof("saved %d with error %v", n.Size, pe)
  47. } else {
  48. glog.V(2).Infof("skipping deleted file ...")
  49. return scanner.nm.Delete(n.Id)
  50. }
  51. return nil
  52. }
  53. func runFix(cmd *Command, args []string) bool {
  54. for _, arg := range args {
  55. basePath, f := path.Split(util.ResolvePath(arg))
  56. if util.FolderExists(arg) {
  57. basePath = arg
  58. f = ""
  59. }
  60. files := []fs.DirEntry{}
  61. if f == "" {
  62. fileInfo, err := os.ReadDir(basePath)
  63. if err != nil {
  64. fmt.Println(err)
  65. return false
  66. }
  67. files = fileInfo
  68. } else {
  69. fileInfo, err := os.Stat(arg)
  70. if err != nil {
  71. fmt.Println(err)
  72. return false
  73. }
  74. files = []fs.DirEntry{fs.FileInfoToDirEntry(fileInfo)}
  75. }
  76. for _, file := range files {
  77. if !strings.HasSuffix(file.Name(), ".dat") {
  78. continue
  79. }
  80. if *fixVolumeCollection != "" {
  81. if !strings.HasPrefix(file.Name(), *fixVolumeCollection+"_") {
  82. continue
  83. }
  84. }
  85. baseFileName := file.Name()[:len(file.Name())-4]
  86. collection, volumeIdStr := "", baseFileName
  87. if sepIndex := strings.LastIndex(baseFileName, "_"); sepIndex > 0 {
  88. collection = baseFileName[:sepIndex]
  89. volumeIdStr = baseFileName[sepIndex+1:]
  90. }
  91. volumeId, parseErr := strconv.ParseInt(volumeIdStr, 10, 64)
  92. if parseErr != nil {
  93. fmt.Printf("Failed to parse volume id from %s: %v\n", baseFileName, parseErr)
  94. return false
  95. }
  96. if *fixVolumeId != 0 && *fixVolumeId != volumeId {
  97. continue
  98. }
  99. doFixOneVolume(basePath, baseFileName, collection, volumeId)
  100. }
  101. }
  102. return true
  103. }
  104. func doFixOneVolume(basepath string, baseFileName string, collection string, volumeId int64) {
  105. indexFileName := path.Join(basepath, baseFileName+".idx")
  106. nm := needle_map.NewMemDb()
  107. defer nm.Close()
  108. vid := needle.VolumeId(volumeId)
  109. scanner := &VolumeFileScanner4Fix{
  110. nm: nm,
  111. }
  112. if err := storage.ScanVolumeFile(basepath, collection, vid, storage.NeedleMapInMemory, scanner); err != nil {
  113. err := fmt.Errorf("scan .dat File: %v", err)
  114. if *fixIgnoreError {
  115. glog.Error(err)
  116. } else {
  117. glog.Fatal(err)
  118. }
  119. }
  120. if err := nm.SaveToIdx(indexFileName); err != nil {
  121. os.Remove(indexFileName)
  122. err := fmt.Errorf("save to .idx File: %v", err)
  123. if *fixIgnoreError {
  124. glog.Error(err)
  125. } else {
  126. glog.Fatal(err)
  127. }
  128. }
  129. }