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.

98 lines
3.0 KiB

6 years ago
6 years ago
5 years ago
7 years ago
7 years ago
6 years ago
7 years ago
  1. package command
  2. import (
  3. "os"
  4. "path"
  5. "strconv"
  6. "github.com/chrislusf/seaweedfs/weed/glog"
  7. "github.com/chrislusf/seaweedfs/weed/storage"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. "github.com/chrislusf/seaweedfs/weed/storage/needle_map"
  10. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  11. "github.com/chrislusf/seaweedfs/weed/storage/types"
  12. "github.com/chrislusf/seaweedfs/weed/util"
  13. )
  14. func init() {
  15. cmdFix.Run = runFix // break init cycle
  16. }
  17. var cmdFix = &Command{
  18. UsageLine: "fix -dir=/tmp -volumeId=234",
  19. Short: "run weed tool fix on index file if corrupted",
  20. Long: `Fix runs the SeaweedFS fix command to re-create the index .idx file.
  21. `,
  22. }
  23. var (
  24. fixVolumePath = cmdFix.Flag.String("dir", ".", "data directory to store files")
  25. fixVolumeCollection = cmdFix.Flag.String("collection", "", "the volume collection name")
  26. fixVolumeId = cmdFix.Flag.Int("volumeId", -1, "a volume id. The volume should already exist in the dir. The volume index file should not exist.")
  27. fixIncludeDeleted = cmdFix.Flag.Bool("includeDeleted", false, "include deleted entries in the index file")
  28. )
  29. type VolumeFileScanner4Fix struct {
  30. version needle.Version
  31. nm *needle_map.MemDb
  32. }
  33. func (scanner *VolumeFileScanner4Fix) VisitSuperBlock(superBlock super_block.SuperBlock) error {
  34. scanner.version = superBlock.Version
  35. return nil
  36. }
  37. func (scanner *VolumeFileScanner4Fix) ReadNeedleBody() bool {
  38. return false
  39. }
  40. func (scanner *VolumeFileScanner4Fix) VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error {
  41. 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())
  42. if n.Size.IsValid() {
  43. pe := scanner.nm.Set(n.Id, types.ToOffset(offset), n.Size)
  44. glog.V(2).Infof("saved %s %d bytes with error %v", n.Id.String(), n.Size, pe)
  45. } else {
  46. if val, found := scanner.nm.Get(n.Id); *fixIncludeDeleted && found && val.Size > 0 {
  47. pe := scanner.nm.Set(n.Id, val.Offset, -val.Size)
  48. glog.V(2).Infof("update deleted %s %d bytes with error %v", n.Id.String(), -val.Size, pe)
  49. return nil
  50. }
  51. glog.V(1).Infof("skipping deleted file %s size %d ...", n.Id.String(), n.Size)
  52. return scanner.nm.Delete(n.Id)
  53. }
  54. return nil
  55. }
  56. func runFix(cmd *Command, args []string) bool {
  57. if *fixVolumeId == -1 {
  58. return false
  59. }
  60. baseFileName := strconv.Itoa(*fixVolumeId)
  61. if *fixVolumeCollection != "" {
  62. baseFileName = *fixVolumeCollection + "_" + baseFileName
  63. }
  64. indexFileName := path.Join(util.ResolvePath(*fixVolumePath), baseFileName+".idx")
  65. nm := needle_map.NewMemDb()
  66. defer nm.Close()
  67. vid := needle.VolumeId(*fixVolumeId)
  68. scanner := &VolumeFileScanner4Fix{
  69. nm: nm,
  70. }
  71. if err := storage.ScanVolumeFile(util.ResolvePath(*fixVolumePath), *fixVolumeCollection, vid, storage.NeedleMapInMemory, scanner); err != nil {
  72. glog.Fatalf("scan .dat File: %v", err)
  73. os.Remove(indexFileName)
  74. }
  75. if err := nm.SaveToIdx(indexFileName, *fixIncludeDeleted); err != nil {
  76. glog.Fatalf("save to .idx File: %v", err)
  77. os.Remove(indexFileName)
  78. }
  79. return true
  80. }