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.

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