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.

133 lines
3.7 KiB

6 years ago
6 years ago
5 years ago
7 years ago
  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 -path=/tmp [-volumeId=234] [-collection=bigData]",
  22. Short: "run weed tool fix to recreate index file(s) if corrupted",
  23. Long: `Fix runs the SeaweedFS fix command on dat files to re-create the index .idx file.
  24. `,
  25. }
  26. var (
  27. fixVolumePath = cmdFix.Flag.String("path", ".", "path to an individual .dat file or a folder of dat files")
  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. )
  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. basePath, f := path.Split(util.ResolvePath(*fixVolumePath))
  55. files := []fs.DirEntry{}
  56. if f == "" {
  57. fileInfo, err := os.ReadDir(basePath + f)
  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. return true
  97. }
  98. func doFixOneVolume(basepath string, baseFileName string, collection string, volumeId int64) {
  99. indexFileName := path.Join(basepath, baseFileName+".idx")
  100. nm := needle_map.NewMemDb()
  101. defer nm.Close()
  102. vid := needle.VolumeId(volumeId)
  103. scanner := &VolumeFileScanner4Fix{
  104. nm: nm,
  105. }
  106. if err := storage.ScanVolumeFile(basepath, collection, vid, storage.NeedleMapInMemory, scanner); err != nil {
  107. glog.Fatalf("scan .dat File: %v", err)
  108. os.Remove(indexFileName)
  109. }
  110. if err := nm.SaveToIdx(indexFileName); err != nil {
  111. glog.Fatalf("save to .idx File: %v", err)
  112. os.Remove(indexFileName)
  113. }
  114. }