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.

74 lines
1.9 KiB

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