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.

89 lines
2.5 KiB

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