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.

82 lines
2.2 KiB

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