fix.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. package command
  2. import (
  3. "fmt"
  4. "io/fs"
  5. "os"
  6. "path"
  7. "strconv"
  8. "strings"
  9. "github.com/chrislusf/seaweedfs/weed/glog"
  10. "github.com/chrislusf/seaweedfs/weed/storage"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. "github.com/chrislusf/seaweedfs/weed/storage/needle_map"
  13. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  14. "github.com/chrislusf/seaweedfs/weed/storage/types"
  15. "github.com/chrislusf/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. files := []fs.DirEntry{}
  56. if f == "" {
  57. fileInfo, err := os.ReadDir(basePath)
  58. if err != nil {
  59. fmt.Println(err)
  60. return false
  61. }
  62. files = fileInfo
  63. } else {
  64. fileInfo, err := os.Stat(basePath + f)
  65. if err != nil {
  66. fmt.Println(err)
  67. return false
  68. }
  69. files = []fs.DirEntry{fs.FileInfoToDirEntry(fileInfo)}
  70. }
  71. for _, file := range files {
  72. if !strings.HasSuffix(file.Name(), ".dat") {
  73. continue
  74. }
  75. if *fixVolumeCollection != "" {
  76. if !strings.HasPrefix(file.Name(), *fixVolumeCollection+"_") {
  77. continue
  78. }
  79. }
  80. baseFileName := file.Name()[:len(file.Name())-4]
  81. collection, volumeIdStr := "", baseFileName
  82. if sepIndex := strings.LastIndex(baseFileName, "_"); sepIndex > 0 {
  83. collection = baseFileName[:sepIndex]
  84. volumeIdStr = baseFileName[sepIndex+1:]
  85. }
  86. volumeId, parseErr := strconv.ParseInt(volumeIdStr, 10, 64)
  87. if parseErr != nil {
  88. fmt.Printf("Failed to parse volume id from %s: %v\n", baseFileName, parseErr)
  89. return false
  90. }
  91. if *fixVolumeId != 0 && *fixVolumeId != volumeId {
  92. continue
  93. }
  94. doFixOneVolume(basePath, baseFileName, collection, volumeId)
  95. }
  96. }
  97. return true
  98. }
  99. func doFixOneVolume(basepath string, baseFileName string, collection string, volumeId int64) {
  100. indexFileName := path.Join(basepath, baseFileName+".idx")
  101. nm := needle_map.NewMemDb()
  102. defer nm.Close()
  103. vid := needle.VolumeId(volumeId)
  104. scanner := &VolumeFileScanner4Fix{
  105. nm: nm,
  106. }
  107. if err := storage.ScanVolumeFile(basepath, collection, vid, storage.NeedleMapInMemory, scanner); err != nil {
  108. glog.Fatalf("scan .dat File: %v", err)
  109. os.Remove(indexFileName)
  110. }
  111. if err := nm.SaveToIdx(indexFileName); err != nil {
  112. glog.Fatalf("save to .idx File: %v", err)
  113. os.Remove(indexFileName)
  114. }
  115. }