2021-02-16 18:47:02 +08:00
|
|
|
package shell
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/pb/master_pb"
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/types"
|
|
|
|
"io"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/chrislusf/seaweedfs/weed/storage/needle"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
Commands = append(Commands, &commandVolumeTierMove{})
|
|
|
|
}
|
|
|
|
|
|
|
|
type commandVolumeTierMove struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeTierMove) Name() string {
|
2021-02-19 19:39:19 +08:00
|
|
|
return "volume.tier.move"
|
2021-02-16 18:47:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeTierMove) Help() string {
|
2021-02-19 19:39:19 +08:00
|
|
|
return `<WIP> change a volume from one disk type to another
|
2021-02-16 18:47:02 +08:00
|
|
|
|
2021-02-22 16:28:42 +08:00
|
|
|
volume.tier.move -fromDiskType=hdd -toDiskType=ssd [-collection=""] [-fullPercent=95] [-quietFor=1h]
|
|
|
|
|
|
|
|
Even if the volume is replicated, only one replica will be changed and the rest replicas will be dropped.
|
|
|
|
So "volume.fix.replication" and "volume.balance" should be followed.
|
2021-02-16 18:47:02 +08:00
|
|
|
|
|
|
|
`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *commandVolumeTierMove) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
|
|
|
|
|
|
|
|
if err = commandEnv.confirmIsLocked(); err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tierCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
|
|
|
|
collection := tierCommand.String("collection", "", "the collection name")
|
|
|
|
fullPercentage := tierCommand.Float64("fullPercent", 95, "the volume reaches the percentage of max volume size")
|
|
|
|
quietPeriod := tierCommand.Duration("quietFor", 24*time.Hour, "select volumes without no writes for this period")
|
|
|
|
source := tierCommand.String("fromDiskType", "", "the source disk type")
|
|
|
|
target := tierCommand.String("toDiskType", "", "the target disk type")
|
|
|
|
if err = tierCommand.Parse(args); err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:28:42 +08:00
|
|
|
fromDiskType := types.ToDiskType(*source)
|
|
|
|
toDiskType := types.ToDiskType(*target)
|
2021-02-16 18:47:02 +08:00
|
|
|
|
2021-02-22 16:28:42 +08:00
|
|
|
if fromDiskType == toDiskType {
|
|
|
|
return fmt.Errorf("source tier %s is the same as target tier %s", fromDiskType, toDiskType)
|
|
|
|
}
|
2021-02-16 18:47:02 +08:00
|
|
|
|
2021-02-22 16:28:42 +08:00
|
|
|
// collect topology information
|
|
|
|
topologyInfo, volumeSizeLimitMb, err := collectTopologyInfo(commandEnv)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-02-16 18:47:02 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// apply to all volumes in the collection
|
2021-02-22 16:28:42 +08:00
|
|
|
volumeIds, err := collectVolumeIdsForTierChange(commandEnv, topologyInfo, volumeSizeLimitMb, fromDiskType, *collection, *fullPercentage, *quietPeriod)
|
2021-02-16 18:47:02 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
fmt.Printf("tier move volumes: %v\n", volumeIds)
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-02-22 16:28:42 +08:00
|
|
|
func collectVolumeIdsForTierChange(commandEnv *CommandEnv, topologyInfo *master_pb.TopologyInfo, volumeSizeLimitMb uint64, sourceTier types.DiskType, selectedCollection string, fullPercentage float64, quietPeriod time.Duration) (vids []needle.VolumeId, err error) {
|
2021-02-16 18:47:02 +08:00
|
|
|
|
|
|
|
quietSeconds := int64(quietPeriod / time.Second)
|
|
|
|
nowUnixSeconds := time.Now().Unix()
|
|
|
|
|
|
|
|
fmt.Printf("collect %s volumes quiet for: %d seconds\n", sourceTier, quietSeconds)
|
|
|
|
|
|
|
|
vidMap := make(map[uint32]bool)
|
2021-02-22 16:28:42 +08:00
|
|
|
eachDataNode(topologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
|
2021-02-16 18:47:02 +08:00
|
|
|
for _, diskInfo := range dn.DiskInfos {
|
|
|
|
for _, v := range diskInfo.VolumeInfos {
|
2021-02-22 16:28:42 +08:00
|
|
|
if v.Collection == selectedCollection && v.ModifiedAtSecond+quietSeconds < nowUnixSeconds && types.ToDiskType(v.DiskType) == sourceTier {
|
|
|
|
if float64(v.Size) > fullPercentage/100*float64(volumeSizeLimitMb)*1024*1024 {
|
2021-02-16 18:47:02 +08:00
|
|
|
vidMap[v.Id] = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
for vid := range vidMap {
|
|
|
|
vids = append(vids, needle.VolumeId(vid))
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|