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.

78 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) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  27. volDeleteCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  28. quietPeriod := volDeleteCommand.Duration("quietFor", 24*time.Hour, "select empty volumes with no recent writes, avoid newly created ones")
  29. applyBalancing := volDeleteCommand.Bool("force", false, "apply to delete empty volumes")
  30. if err = volDeleteCommand.Parse(args); err != nil {
  31. return nil
  32. }
  33. infoAboutSimulationMode(writer, *applyBalancing, "-force")
  34. if err = commandEnv.confirmIsLocked(args); err != nil {
  35. return
  36. }
  37. // collect topology information
  38. topologyInfo, _, err := collectTopologyInfo(commandEnv, 0)
  39. if err != nil {
  40. return err
  41. }
  42. quietSeconds := int64(*quietPeriod / time.Second)
  43. nowUnixSeconds := time.Now().Unix()
  44. eachDataNode(topologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  45. for _, diskInfo := range dn.DiskInfos {
  46. for _, v := range diskInfo.VolumeInfos {
  47. if v.Size <= super_block.SuperBlockSize && v.ModifiedAtSecond > 0 && v.ModifiedAtSecond+quietSeconds < nowUnixSeconds {
  48. if *applyBalancing {
  49. log.Printf("deleting empty volume %d from %s", v.Id, dn.Id)
  50. if deleteErr := deleteVolume(commandEnv.option.GrpcDialOption, needle.VolumeId(v.Id),
  51. pb.NewServerAddressFromDataNode(dn), true); deleteErr != nil {
  52. err = deleteErr
  53. }
  54. continue
  55. } else {
  56. log.Printf("empty volume %d from %s", v.Id, dn.Id)
  57. }
  58. }
  59. }
  60. }
  61. })
  62. return
  63. }