fix.go 2.0 KB

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