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.

147 lines
4.1 KiB

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