compact.go 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. package command
  2. import (
  3. "github.com/seaweedfs/seaweedfs/weed/glog"
  4. "github.com/seaweedfs/seaweedfs/weed/storage"
  5. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  6. "github.com/seaweedfs/seaweedfs/weed/util"
  7. )
  8. func init() {
  9. cmdCompact.Run = runCompact // break init cycle
  10. }
  11. var cmdCompact = &Command{
  12. UsageLine: "compact -dir=/tmp -volumeId=234",
  13. Short: "run weed tool compact on volume file",
  14. Long: `Force an compaction to remove deleted files from volume files.
  15. The compacted .dat file is stored as .cpd file.
  16. The compacted .idx file is stored as .cpx file.
  17. For method=0, it compacts based on the .dat file, works if .idx file is corrupted.
  18. For method=1, it compacts based on the .idx file, works if deletion happened but not written to .dat files.
  19. `,
  20. }
  21. var (
  22. compactVolumePath = cmdCompact.Flag.String("dir", ".", "data directory to store files")
  23. compactVolumeCollection = cmdCompact.Flag.String("collection", "", "volume collection name")
  24. compactVolumeId = cmdCompact.Flag.Int("volumeId", -1, "a volume id. The volume should already exist in the dir.")
  25. compactMethod = cmdCompact.Flag.Int("method", 0, "option to choose which compact method. use 0 (default) or 1.")
  26. compactVolumePreallocate = cmdCompact.Flag.Int64("preallocateMB", 0, "preallocate volume disk space")
  27. )
  28. func runCompact(cmd *Command, args []string) bool {
  29. if *compactVolumeId == -1 {
  30. return false
  31. }
  32. preallocate := *compactVolumePreallocate * (1 << 20)
  33. vid := needle.VolumeId(*compactVolumeId)
  34. v, err := storage.NewVolume(util.ResolvePath(*compactVolumePath), util.ResolvePath(*compactVolumePath), *compactVolumeCollection, vid, storage.NeedleMapInMemory, nil, nil, preallocate, 0, 0)
  35. if err != nil {
  36. glog.Fatalf("Load Volume [ERROR] %s\n", err)
  37. }
  38. if *compactMethod == 0 {
  39. if err = v.Compact(preallocate, 0); err != nil {
  40. glog.Fatalf("Compact Volume [ERROR] %s\n", err)
  41. }
  42. } else {
  43. if err = v.Compact2(preallocate, 0, nil); err != nil {
  44. glog.Fatalf("Compact Volume [ERROR] %s\n", err)
  45. }
  46. }
  47. return true
  48. }