2021-08-01 22:33:45 +00:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
2022-07-29 07:17:28 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
|
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/needle"
|
2023-06-14 21:39:58 +00:00
|
|
|
"github.com/seaweedfs/seaweedfs/weed/storage/super_block"
|
2021-08-01 22:33:45 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Commands = append(Commands, &commandVolumeDeleteEmpty{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type commandVolumeDeleteEmpty struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeDeleteEmpty) Name() string {
|
|
|
|
return "volume.deleteEmpty"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeDeleteEmpty) Help() string {
|
|
|
|
return `delete empty volumes from all volume servers
|
|
|
|
|
2021-08-02 02:37:20 +00:00
|
|
|
volume.deleteEmpty -quietFor=24h -force
|
2021-08-01 22:33:45 +00:00
|
|
|
|
|
|
|
This command deletes all empty volumes from one volume server.
|
|
|
|
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeDeleteEmpty) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
|
|
|
|
|
|
|
volDeleteCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
quietPeriod := volDeleteCommand.Duration("quietFor", 24*time.Hour, "select empty volumes with no recent writes, avoid newly created ones")
|
2021-08-02 02:37:20 +00:00
|
|
|
applyBalancing := volDeleteCommand.Bool("force", false, "apply to delete empty volumes")
|
2021-08-01 22:33:45 +00:00
|
|
|
if err = volDeleteCommand.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
2022-05-31 21:48:46 +00:00
|
|
|
infoAboutSimulationMode(writer, *applyBalancing, "-force")
|
2021-08-01 22:33:45 +00:00
|
|
|
|
2021-12-10 21:24:38 +00:00
|
|
|
if err = commandEnv.confirmIsLocked(args); err != nil {
|
2021-09-14 05:13:34 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-01 22:33:45 +00:00
|
|
|
// collect topology information
|
2022-02-08 08:53:55 +00:00
|
|
|
topologyInfo, _, err := collectTopologyInfo(commandEnv, 0)
|
2021-08-01 22:33:45 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
quietSeconds := int64(*quietPeriod / time.Second)
|
|
|
|
nowUnixSeconds := time.Now().Unix()
|
|
|
|
|
|
|
|
eachDataNode(topologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
|
|
|
|
for _, diskInfo := range dn.DiskInfos {
|
|
|
|
for _, v := range diskInfo.VolumeInfos {
|
2023-06-14 21:39:58 +00:00
|
|
|
if v.Size <= super_block.SuperBlockSize && v.ModifiedAtSecond > 0 && v.ModifiedAtSecond+quietSeconds < nowUnixSeconds {
|
2021-08-02 02:37:20 +00:00
|
|
|
if *applyBalancing {
|
|
|
|
log.Printf("deleting empty volume %d from %s", v.Id, dn.Id)
|
2023-06-12 17:42:44 +00:00
|
|
|
if deleteErr := deleteVolume(commandEnv.option.GrpcDialOption, needle.VolumeId(v.Id),
|
|
|
|
pb.NewServerAddressFromDataNode(dn), true); deleteErr != nil {
|
2021-08-02 02:37:20 +00:00
|
|
|
err = deleteErr
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
} else {
|
|
|
|
log.Printf("empty volume %d from %s", v.Id, dn.Id)
|
2021-08-01 22:33:45 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|