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.

91 lines
2.6 KiB

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