command_volume_delete_empty.go 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. package shell
  2. import (
  3. "flag"
  4. "github.com/seaweedfs/seaweedfs/weed/pb"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  7. "io"
  8. "log"
  9. "time"
  10. )
  11. func init() {
  12. Commands = append(Commands, &commandVolumeDeleteEmpty{})
  13. }
  14. type commandVolumeDeleteEmpty struct {
  15. }
  16. func (c *commandVolumeDeleteEmpty) Name() string {
  17. return "volume.deleteEmpty"
  18. }
  19. func (c *commandVolumeDeleteEmpty) Help() string {
  20. return `delete empty volumes from all volume servers
  21. volume.deleteEmpty -quietFor=24h -force
  22. This command deletes all empty volumes from one volume server.
  23. `
  24. }
  25. func (c *commandVolumeDeleteEmpty) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  26. volDeleteCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  27. quietPeriod := volDeleteCommand.Duration("quietFor", 24*time.Hour, "select empty volumes with no recent writes, avoid newly created ones")
  28. applyBalancing := volDeleteCommand.Bool("force", false, "apply to delete empty volumes")
  29. if err = volDeleteCommand.Parse(args); err != nil {
  30. return nil
  31. }
  32. infoAboutSimulationMode(writer, *applyBalancing, "-force")
  33. if err = commandEnv.confirmIsLocked(args); err != nil {
  34. return
  35. }
  36. // collect topology information
  37. topologyInfo, _, err := collectTopologyInfo(commandEnv, 0)
  38. if err != nil {
  39. return err
  40. }
  41. quietSeconds := int64(*quietPeriod / time.Second)
  42. nowUnixSeconds := time.Now().Unix()
  43. eachDataNode(topologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  44. for _, diskInfo := range dn.DiskInfos {
  45. for _, v := range diskInfo.VolumeInfos {
  46. if v.Size <= 8 && v.ModifiedAtSecond+quietSeconds < nowUnixSeconds {
  47. if *applyBalancing {
  48. log.Printf("deleting empty volume %d from %s", v.Id, dn.Id)
  49. if deleteErr := deleteVolume(commandEnv.option.GrpcDialOption, needle.VolumeId(v.Id), pb.NewServerAddressFromDataNode(dn)); deleteErr != nil {
  50. err = deleteErr
  51. }
  52. continue
  53. } else {
  54. log.Printf("empty volume %d from %s", v.Id, dn.Id)
  55. }
  56. }
  57. }
  58. }
  59. })
  60. return
  61. }