command_volume_tier_move.go 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  6. "github.com/chrislusf/seaweedfs/weed/storage/types"
  7. "github.com/chrislusf/seaweedfs/weed/wdclient"
  8. "io"
  9. "path/filepath"
  10. "strings"
  11. "time"
  12. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  13. )
  14. func init() {
  15. Commands = append(Commands, &commandVolumeTierMove{})
  16. }
  17. type commandVolumeTierMove struct {
  18. }
  19. func (c *commandVolumeTierMove) Name() string {
  20. return "volume.tier.move"
  21. }
  22. func (c *commandVolumeTierMove) Help() string {
  23. return `change a volume from one disk type to another
  24. volume.tier.move -fromDiskType=hdd -toDiskType=ssd [-collectionPattern=""] [-fullPercent=95] [-quietFor=1h]
  25. Even if the volume is replicated, only one replica will be changed and the rest replicas will be dropped.
  26. So "volume.fix.replication" and "volume.balance" should be followed.
  27. `
  28. }
  29. func (c *commandVolumeTierMove) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  30. if err = commandEnv.confirmIsLocked(); err != nil {
  31. return
  32. }
  33. tierCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  34. collectionPattern := tierCommand.String("collectionPattern", "", "match with wildcard characters '*' and '?'")
  35. fullPercentage := tierCommand.Float64("fullPercent", 95, "the volume reaches the percentage of max volume size")
  36. quietPeriod := tierCommand.Duration("quietFor", 24*time.Hour, "select volumes without no writes for this period")
  37. source := tierCommand.String("fromDiskType", "", "the source disk type")
  38. target := tierCommand.String("toDiskType", "", "the target disk type")
  39. applyChange := tierCommand.Bool("force", false, "actually apply the changes")
  40. if err = tierCommand.Parse(args); err != nil {
  41. return nil
  42. }
  43. fromDiskType := types.ToDiskType(*source)
  44. toDiskType := types.ToDiskType(*target)
  45. if fromDiskType == toDiskType {
  46. return fmt.Errorf("source tier %s is the same as target tier %s", fromDiskType, toDiskType)
  47. }
  48. // collect topology information
  49. topologyInfo, volumeSizeLimitMb, err := collectTopologyInfo(commandEnv)
  50. if err != nil {
  51. return err
  52. }
  53. // collect all volumes that should change
  54. volumeIds, err := collectVolumeIdsForTierChange(commandEnv, topologyInfo, volumeSizeLimitMb, fromDiskType, *collectionPattern, *fullPercentage, *quietPeriod)
  55. if err != nil {
  56. return err
  57. }
  58. fmt.Printf("tier move volumes: %v\n", volumeIds)
  59. _, allLocations := collectVolumeReplicaLocations(topologyInfo)
  60. for _, vid := range volumeIds {
  61. if err = doVolumeTierMove(commandEnv, writer, vid, toDiskType, allLocations, *applyChange); err != nil {
  62. fmt.Printf("tier move volume %d: %v\n", vid, err)
  63. }
  64. }
  65. return nil
  66. }
  67. func isOneOf(server string, locations []wdclient.Location) bool {
  68. for _, loc := range locations {
  69. if server == loc.Url {
  70. return true
  71. }
  72. }
  73. return false
  74. }
  75. func doVolumeTierMove(commandEnv *CommandEnv, writer io.Writer, vid needle.VolumeId, toDiskType types.DiskType, allLocations []location, applyChanges bool) (err error) {
  76. // find volume location
  77. locations, found := commandEnv.MasterClient.GetLocations(uint32(vid))
  78. if !found {
  79. return fmt.Errorf("volume %d not found", vid)
  80. }
  81. // find one server with the most empty volume slots with target disk type
  82. hasFoundTarget := false
  83. keepDataNodesSorted(allLocations, toDiskType)
  84. fn := capacityByFreeVolumeCount(toDiskType)
  85. for _, dst := range allLocations {
  86. if fn(dst.dataNode) > 0 && !hasFoundTarget {
  87. // ask the volume server to replicate the volume
  88. if isOneOf(dst.dataNode.Id, locations) {
  89. continue
  90. }
  91. sourceVolumeServer := ""
  92. for _, loc := range locations {
  93. if loc.Url != dst.dataNode.Id {
  94. sourceVolumeServer = loc.Url
  95. }
  96. }
  97. if sourceVolumeServer == "" {
  98. continue
  99. }
  100. fmt.Fprintf(writer, "moving volume %d from %s to %s with disk type %s ...\n", vid, sourceVolumeServer, dst.dataNode.Id, toDiskType.ReadableString())
  101. hasFoundTarget = true
  102. if !applyChanges {
  103. // adjust volume count
  104. dst.dataNode.DiskInfos[string(toDiskType)].VolumeCount++
  105. break
  106. }
  107. // mark all replicas as read only
  108. if err = markVolumeReadonly(commandEnv.option.GrpcDialOption, vid, locations); err != nil {
  109. return fmt.Errorf("mark volume %d as readonly on %s: %v", vid, locations[0].Url, err)
  110. }
  111. if err = LiveMoveVolume(commandEnv.option.GrpcDialOption, vid, sourceVolumeServer, dst.dataNode.Id, 5*time.Second, toDiskType.ReadableString()); err != nil {
  112. return fmt.Errorf("move volume %d %s => %s : %v", vid, locations[0].Url, dst.dataNode.Id, err)
  113. }
  114. // adjust volume count
  115. dst.dataNode.DiskInfos[string(toDiskType)].VolumeCount++
  116. // remove the remaining replicas
  117. for _, loc := range locations {
  118. if loc.Url != dst.dataNode.Id {
  119. if err = deleteVolume(commandEnv.option.GrpcDialOption, vid, loc.Url); err != nil {
  120. if !strings.Contains(err.Error(), "not found") {
  121. fmt.Fprintf(writer, "failed to delete volume %d on %s: %v\n", vid, loc.Url, err)
  122. }
  123. }
  124. }
  125. }
  126. }
  127. }
  128. if !hasFoundTarget {
  129. fmt.Fprintf(writer, "can not find disk type %s for volume %d\n", toDiskType.ReadableString(), vid)
  130. }
  131. return nil
  132. }
  133. func collectVolumeIdsForTierChange(commandEnv *CommandEnv, topologyInfo *master_pb.TopologyInfo, volumeSizeLimitMb uint64, sourceTier types.DiskType, collectionPattern string, fullPercentage float64, quietPeriod time.Duration) (vids []needle.VolumeId, err error) {
  134. quietSeconds := int64(quietPeriod / time.Second)
  135. nowUnixSeconds := time.Now().Unix()
  136. fmt.Printf("collect %s volumes quiet for: %d seconds\n", sourceTier, quietSeconds)
  137. vidMap := make(map[uint32]bool)
  138. eachDataNode(topologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  139. for _, diskInfo := range dn.DiskInfos {
  140. for _, v := range diskInfo.VolumeInfos {
  141. // check collection name pattern
  142. if collectionPattern != "" {
  143. matched, err := filepath.Match(collectionPattern, v.Collection)
  144. if err != nil {
  145. return
  146. }
  147. if !matched {
  148. continue
  149. }
  150. }
  151. if v.ModifiedAtSecond+quietSeconds < nowUnixSeconds && types.ToDiskType(v.DiskType) == sourceTier {
  152. if float64(v.Size) > fullPercentage/100*float64(volumeSizeLimitMb)*1024*1024 {
  153. vidMap[v.Id] = true
  154. }
  155. }
  156. }
  157. }
  158. })
  159. for vid := range vidMap {
  160. vids = append(vids, needle.VolumeId(vid))
  161. }
  162. return
  163. }