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.

130 lines
3.5 KiB

6 years ago
6 years ago
5 years ago
6 years ago
7 years ago
  1. package command
  2. import (
  3. "fmt"
  4. "io/ioutil"
  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 -dir=/tmp -volumeId=234",
  22. Short: "run weed tool fix on index file if corrupted",
  23. Long: `Fix runs the SeaweedFS fix command to re-create the index .idx file.
  24. `,
  25. }
  26. var (
  27. fixVolumePath = cmdFix.Flag.String("dir", ".", "data directory to store files")
  28. fixVolumeCollection = cmdFix.Flag.String("collection", "", "the volume collection name")
  29. fixVolumeId = cmdFix.Flag.Int("volumeId", 0, "an optional volume id.")
  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. dir := util.ResolvePath(*fixVolumePath)
  55. if *fixVolumeId != 0 {
  56. doFixOneVolume(dir, *fixVolumeCollection, needle.VolumeId(*fixVolumeId))
  57. return true
  58. }
  59. files, err := ioutil.ReadDir(dir)
  60. if err != nil {
  61. fmt.Println(err)
  62. return false
  63. }
  64. for _, file := range files {
  65. if !strings.HasSuffix(file.Name(), ".dat") {
  66. continue
  67. }
  68. if *fixVolumeCollection != "" {
  69. if !strings.HasPrefix(file.Name(), *fixVolumeCollection+"_") {
  70. continue
  71. }
  72. }
  73. baseFileName := file.Name()[:len(file.Name())-4]
  74. collection, volumeIdStr := "", baseFileName
  75. if sepIndex := strings.LastIndex(baseFileName, "_"); sepIndex > 0 {
  76. collection = baseFileName[:sepIndex]
  77. volumeIdStr = baseFileName[sepIndex+1:]
  78. }
  79. volumeId, parseErr := strconv.ParseInt(volumeIdStr, 10, 64)
  80. if parseErr != nil {
  81. fmt.Printf("Failed to parse volume id from %s: %v\n", baseFileName, parseErr)
  82. return false
  83. }
  84. doFixOneVolume(dir, collection, needle.VolumeId(volumeId))
  85. }
  86. return true
  87. }
  88. func doFixOneVolume(dir, collection string, volumeId needle.VolumeId) {
  89. baseFileName := strconv.Itoa(int(volumeId))
  90. if collection != "" {
  91. baseFileName = collection + "_" + baseFileName
  92. }
  93. indexFileName := path.Join(dir, baseFileName+".idx")
  94. nm := needle_map.NewMemDb()
  95. defer nm.Close()
  96. vid := needle.VolumeId(*fixVolumeId)
  97. scanner := &VolumeFileScanner4Fix{
  98. nm: nm,
  99. }
  100. if err := storage.ScanVolumeFile(dir, collection, vid, storage.NeedleMapInMemory, scanner); err != nil {
  101. glog.Fatalf("scan .dat File: %v", err)
  102. os.Remove(indexFileName)
  103. }
  104. if err := nm.SaveToIdx(indexFileName); err != nil {
  105. glog.Fatalf("save to .idx File: %v", err)
  106. os.Remove(indexFileName)
  107. }
  108. }