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.

75 lines
2.2 KiB

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/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. func runFix(cmd *Command, args []string) bool {
  25. if *fixVolumeId == -1 {
  26. return false
  27. }
  28. baseFileName := strconv.Itoa(*fixVolumeId)
  29. if *fixVolumeCollection != "" {
  30. baseFileName = *fixVolumeCollection + "_" + baseFileName
  31. }
  32. indexFileName := path.Join(*fixVolumePath, baseFileName+".idx")
  33. indexFile, err := os.OpenFile(indexFileName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0644)
  34. if err != nil {
  35. glog.Fatalf("Create Volume Index [ERROR] %s\n", err)
  36. }
  37. defer indexFile.Close()
  38. nm := storage.NewBtreeNeedleMap(indexFile)
  39. defer nm.Close()
  40. var version storage.Version
  41. vid := storage.VolumeId(*fixVolumeId)
  42. err = storage.ScanVolumeFile(*fixVolumePath, *fixVolumeCollection, vid,
  43. storage.NeedleMapInMemory,
  44. func(superBlock storage.SuperBlock) error {
  45. version = superBlock.Version()
  46. return nil
  47. }, false, func(n *storage.Needle, offset int64) error {
  48. glog.V(2).Infof("key %d offset %d size %d disk_size %d gzip %v", n.Id, offset, n.Size, n.DiskSize(version), n.IsGzipped())
  49. if n.Size > 0 {
  50. pe := nm.Put(n.Id, types.Offset(offset/types.NeedlePaddingSize), n.Size)
  51. glog.V(2).Infof("saved %d with error %v", n.Size, pe)
  52. } else {
  53. glog.V(2).Infof("skipping deleted file ...")
  54. return nm.Delete(n.Id, types.Offset(offset/types.NeedlePaddingSize))
  55. }
  56. return nil
  57. })
  58. if err != nil {
  59. glog.Fatalf("Export Volume File [ERROR] %s\n", err)
  60. os.Remove(indexFileName)
  61. }
  62. return true
  63. }