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.

71 lines
2.0 KiB

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