command_volume_vacuum.go 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "io"
  6. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  7. )
  8. func init() {
  9. Commands = append(Commands, &commandVacuum{})
  10. }
  11. type commandVacuum struct {
  12. }
  13. func (c *commandVacuum) Name() string {
  14. return "volume.vacuum"
  15. }
  16. func (c *commandVacuum) Help() string {
  17. return `compact volumes if deleted entries are more than the limit
  18. volume.vacuum [-garbageThreshold=0.3] [-collection=<collection name>] [-volumeId=<volume id>]
  19. `
  20. }
  21. func (c *commandVacuum) HasTag(CommandTag) bool {
  22. return false
  23. }
  24. func (c *commandVacuum) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  25. volumeVacuumCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  26. garbageThreshold := volumeVacuumCommand.Float64("garbageThreshold", 0.3, "vacuum when garbage is more than this limit")
  27. collection := volumeVacuumCommand.String("collection", "", "vacuum this collection")
  28. volumeId := volumeVacuumCommand.Uint("volumeId", 0, "the volume id")
  29. if err = volumeVacuumCommand.Parse(args); err != nil {
  30. return nil
  31. }
  32. if err = commandEnv.confirmIsLocked(args); err != nil {
  33. return
  34. }
  35. err = commandEnv.MasterClient.WithClient(false, func(client master_pb.SeaweedClient) error {
  36. _, err = client.VacuumVolume(context.Background(), &master_pb.VacuumVolumeRequest{
  37. GarbageThreshold: float32(*garbageThreshold),
  38. VolumeId: uint32(*volumeId),
  39. Collection: *collection,
  40. })
  41. return err
  42. })
  43. if err != nil {
  44. return
  45. }
  46. return nil
  47. }