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.

90 lines
2.5 KiB

6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
6 years ago
  1. package command
  2. import (
  3. "os"
  4. "path"
  5. "strconv"
  6. "github.com/joeslay/seaweedfs/weed/glog"
  7. "github.com/joeslay/seaweedfs/weed/storage"
  8. "github.com/joeslay/seaweedfs/weed/storage/needle"
  9. "github.com/joeslay/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) 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. }