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.

192 lines
6.0 KiB

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
  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. break
  104. }
  105. // mark all replicas as read only
  106. if err = markVolumeReadonly(commandEnv.option.GrpcDialOption, vid, locations); err != nil {
  107. return fmt.Errorf("mark volume %d as readonly on %s: %v", vid, locations[0].Url, err)
  108. }
  109. if err = LiveMoveVolume(commandEnv.option.GrpcDialOption, vid, sourceVolumeServer, dst.dataNode.Id, 5*time.Second, toDiskType.ReadableString()); err != nil {
  110. return fmt.Errorf("move volume %d %s => %s : %v", vid, locations[0].Url, dst.dataNode.Id, err)
  111. }
  112. // remove the remaining replicas
  113. for _, loc := range locations {
  114. if loc.Url != dst.dataNode.Id {
  115. if err = deleteVolume(commandEnv.option.GrpcDialOption, vid, loc.Url); err != nil {
  116. if !strings.Contains(err.Error(), "not found") {
  117. fmt.Fprintf(writer, "failed to delete volume %d on %s: %v\n", vid, loc.Url, err)
  118. }
  119. }
  120. }
  121. }
  122. }
  123. }
  124. if !hasFoundTarget {
  125. fmt.Fprintf(writer, "can not find disk type %s for volume %d\n", toDiskType.ReadableString(), vid)
  126. }
  127. return nil
  128. }
  129. 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) {
  130. quietSeconds := int64(quietPeriod / time.Second)
  131. nowUnixSeconds := time.Now().Unix()
  132. fmt.Printf("collect %s volumes quiet for: %d seconds\n", sourceTier, quietSeconds)
  133. vidMap := make(map[uint32]bool)
  134. eachDataNode(topologyInfo, func(dc string, rack RackId, dn *master_pb.DataNodeInfo) {
  135. for _, diskInfo := range dn.DiskInfos {
  136. for _, v := range diskInfo.VolumeInfos {
  137. // check collection name pattern
  138. if collectionPattern != "" {
  139. matched, err := filepath.Match(collectionPattern, v.Collection)
  140. if err != nil {
  141. return
  142. }
  143. if !matched {
  144. continue
  145. }
  146. }
  147. if v.ModifiedAtSecond+quietSeconds < nowUnixSeconds && types.ToDiskType(v.DiskType) == sourceTier {
  148. if float64(v.Size) > fullPercentage/100*float64(volumeSizeLimitMb)*1024*1024 {
  149. vidMap[v.Id] = true
  150. }
  151. }
  152. }
  153. }
  154. })
  155. for vid := range vidMap {
  156. vids = append(vids, needle.VolumeId(vid))
  157. }
  158. return
  159. }