fix.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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/types"
  10. )
  11. func init() {
  12. cmdFix.Run = runFix // break init cycle
  13. }
  14. var cmdFix = &Command{
  15. UsageLine: "fix -dir=/tmp -volumeId=234",
  16. Short: "run weed tool fix on index file if corrupted",
  17. Long: `Fix runs the SeaweedFS fix command to re-create the index .idx file.
  18. `,
  19. }
  20. var (
  21. fixVolumePath = cmdFix.Flag.String("dir", ".", "data directory to store files")
  22. fixVolumeCollection = cmdFix.Flag.String("collection", "", "the volume collection name")
  23. fixVolumeId = cmdFix.Flag.Int("volumeId", -1, "a volume id. The volume should already exist in the dir. The volume index file should not exist.")
  24. )
  25. type VolumeFileScanner4Fix struct {
  26. version needle.Version
  27. nm *storage.NeedleMap
  28. }
  29. func (scanner *VolumeFileScanner4Fix) VisitSuperBlock(superBlock storage.SuperBlock) error {
  30. scanner.version = superBlock.Version()
  31. return nil
  32. }
  33. func (scanner *VolumeFileScanner4Fix) ReadNeedleBody() bool {
  34. return false
  35. }
  36. func (scanner *VolumeFileScanner4Fix) VisitNeedle(n *needle.Needle, offset int64, needleHeader, needleBody []byte) error {
  37. glog.V(2).Infof("key %d offset %d size %d disk_size %d gzip %v", n.Id, offset, n.Size, n.DiskSize(scanner.version), n.IsGzipped())
  38. if n.Size > 0 && n.Size != types.TombstoneFileSize {
  39. pe := scanner.nm.Put(n.Id, types.ToOffset(offset), n.Size)
  40. glog.V(2).Infof("saved %d with error %v", n.Size, pe)
  41. } else {
  42. glog.V(2).Infof("skipping deleted file ...")
  43. return scanner.nm.Delete(n.Id, types.ToOffset(offset))
  44. }
  45. return nil
  46. }
  47. func runFix(cmd *Command, args []string) bool {
  48. if *fixVolumeId == -1 {
  49. return false
  50. }
  51. baseFileName := strconv.Itoa(*fixVolumeId)
  52. if *fixVolumeCollection != "" {
  53. baseFileName = *fixVolumeCollection + "_" + baseFileName
  54. }
  55. indexFileName := path.Join(*fixVolumePath, baseFileName+".idx")
  56. indexFile, err := os.OpenFile(indexFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  57. if err != nil {
  58. glog.Fatalf("Create Volume Index [ERROR] %s\n", err)
  59. }
  60. defer indexFile.Close()
  61. nm := storage.NewBtreeNeedleMap(indexFile)
  62. defer nm.Close()
  63. vid := needle.VolumeId(*fixVolumeId)
  64. scanner := &VolumeFileScanner4Fix{
  65. nm: nm,
  66. }
  67. err = storage.ScanVolumeFile(*fixVolumePath, *fixVolumeCollection, vid, storage.NeedleMapInMemory, scanner)
  68. if err != nil {
  69. glog.Fatalf("Export Volume File [ERROR] %s\n", err)
  70. os.Remove(indexFileName)
  71. }
  72. return true
  73. }