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.

75 lines
2.0 KiB

  1. package shell
  2. import (
  3. "flag"
  4. "github.com/chrislusf/seaweedfs/weed/pb"
  5. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  6. "github.com/chrislusf/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. if err = commandEnv.confirmIsLocked(); err != nil {
  33. return
  34. }
  35. // collect topology information
  36. topologyInfo, _, err := collectTopologyInfo(commandEnv)
  37. if err != nil {
  38. return err
  39. }
  40. quietSeconds := int64(*quietPeriod / time.Second)
  41. nowUnixSeconds := time.Now().Unix()
  42. eachDataNode(topologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  43. for _, diskInfo := range dn.DiskInfos {
  44. for _, v := range diskInfo.VolumeInfos {
  45. if v.Size <= 8 && v.ModifiedAtSecond+quietSeconds < nowUnixSeconds {
  46. if *applyBalancing {
  47. log.Printf("deleting empty volume %d from %s", v.Id, dn.Id)
  48. if deleteErr := deleteVolume(commandEnv.option.GrpcDialOption, needle.VolumeId(v.Id), pb.NewServerAddressFromDataNode(dn)); deleteErr != nil {
  49. err = deleteErr
  50. }
  51. continue
  52. } else {
  53. log.Printf("empty volume %d from %s", v.Id, dn.Id)
  54. }
  55. }
  56. }
  57. }
  58. })
  59. return
  60. }