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.

265 lines
9.1 KiB

4 years ago
4 years ago
2 years ago
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  6. "github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
  7. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  8. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  10. "golang.org/x/exp/slices"
  11. "io"
  12. "os"
  13. )
  14. func init() {
  15. Commands = append(Commands, &commandVolumeServerEvacuate{})
  16. }
  17. type commandVolumeServerEvacuate struct {
  18. topologyInfo *master_pb.TopologyInfo
  19. targetServer *string
  20. volumeRack *string
  21. }
  22. func (c *commandVolumeServerEvacuate) Name() string {
  23. return "volumeServer.evacuate"
  24. }
  25. func (c *commandVolumeServerEvacuate) Help() string {
  26. return `move out all data on a volume server
  27. volumeServer.evacuate -node <host:port>
  28. This command moves all data away from the volume server.
  29. The volumes on the volume servers will be redistributed.
  30. Usually this is used to prepare to shutdown or upgrade the volume server.
  31. Sometimes a volume can not be moved because there are no
  32. good destination to meet the replication requirement.
  33. E.g. a volume replication 001 in a cluster with 2 volume servers can not be moved.
  34. You can use "-skipNonMoveable" to move the rest volumes.
  35. `
  36. }
  37. func (c *commandVolumeServerEvacuate) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  38. vsEvacuateCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  39. volumeServer := vsEvacuateCommand.String("node", "", "<host>:<port> of the volume server")
  40. c.volumeRack = vsEvacuateCommand.String("rack", "", "source rack for the volume servers")
  41. c.targetServer = vsEvacuateCommand.String("target", "", "<host>:<port> of target volume")
  42. skipNonMoveable := vsEvacuateCommand.Bool("skipNonMoveable", false, "skip volumes that can not be moved")
  43. applyChange := vsEvacuateCommand.Bool("force", false, "actually apply the changes")
  44. retryCount := vsEvacuateCommand.Int("retry", 0, "how many times to retry")
  45. if err = vsEvacuateCommand.Parse(args); err != nil {
  46. return nil
  47. }
  48. infoAboutSimulationMode(writer, *applyChange, "-force")
  49. if err = commandEnv.confirmIsLocked(args); err != nil && *applyChange {
  50. return
  51. }
  52. if *volumeServer == "" && *c.volumeRack == "" {
  53. return fmt.Errorf("need to specify volume server by -node=<host>:<port> or source rack")
  54. }
  55. for i := 0; i < *retryCount+1; i++ {
  56. if err = c.volumeServerEvacuate(commandEnv, *volumeServer, *skipNonMoveable, *applyChange, writer); err == nil {
  57. return nil
  58. }
  59. }
  60. return
  61. }
  62. func (c *commandVolumeServerEvacuate) volumeServerEvacuate(commandEnv *CommandEnv, volumeServer string, skipNonMoveable, applyChange bool, writer io.Writer) (err error) {
  63. // 1. confirm the volume server is part of the cluster
  64. // 2. collect all other volume servers, sort by empty slots
  65. // 3. move to any other volume server as long as it satisfy the replication requirements
  66. // list all the volumes
  67. // collect topology information
  68. c.topologyInfo, _, err = collectTopologyInfo(commandEnv, 0)
  69. if err != nil {
  70. return err
  71. }
  72. if err := c.evacuateNormalVolumes(commandEnv, volumeServer, skipNonMoveable, applyChange, writer); err != nil {
  73. return err
  74. }
  75. if err := c.evacuateEcVolumes(commandEnv, volumeServer, skipNonMoveable, applyChange, writer); err != nil {
  76. return err
  77. }
  78. return nil
  79. }
  80. func (c *commandVolumeServerEvacuate) evacuateNormalVolumes(commandEnv *CommandEnv, volumeServer string, skipNonMoveable, applyChange bool, writer io.Writer) error {
  81. // find this volume server
  82. volumeServers := collectVolumeServersByDc(c.topologyInfo, "")
  83. thisNodes, otherNodes := c.nodesOtherThan(volumeServers, volumeServer)
  84. if len(thisNodes) == 0 {
  85. return fmt.Errorf("%s is not found in this cluster", volumeServer)
  86. }
  87. // move away normal volumes
  88. for _, thisNode := range thisNodes {
  89. for _, diskInfo := range thisNode.info.DiskInfos {
  90. if applyChange {
  91. if topologyInfo, _, err := collectTopologyInfo(commandEnv, 0); err != nil {
  92. fmt.Fprintf(writer, "update topologyInfo %v", err)
  93. } else {
  94. _, otherNodesNew := c.nodesOtherThan(
  95. collectVolumeServersByDc(topologyInfo, ""), volumeServer)
  96. if len(otherNodesNew) > 0 {
  97. otherNodes = otherNodesNew
  98. c.topologyInfo = topologyInfo
  99. fmt.Fprintf(writer, "topologyInfo updated %v\n", len(otherNodes))
  100. }
  101. }
  102. }
  103. volumeReplicas, _ := collectVolumeReplicaLocations(c.topologyInfo)
  104. for _, vol := range diskInfo.VolumeInfos {
  105. hasMoved, err := moveAwayOneNormalVolume(commandEnv, volumeReplicas, vol, thisNode, otherNodes, applyChange)
  106. if err != nil {
  107. fmt.Fprintf(writer, "move away volume %d from %s: %v\n", vol.Id, volumeServer, err)
  108. }
  109. if !hasMoved {
  110. if skipNonMoveable {
  111. replicaPlacement, _ := super_block.NewReplicaPlacementFromByte(byte(vol.ReplicaPlacement))
  112. fmt.Fprintf(writer, "skipping non moveable volume %d replication:%s\n", vol.Id, replicaPlacement.String())
  113. } else {
  114. return fmt.Errorf("failed to move volume %d from %s", vol.Id, volumeServer)
  115. }
  116. }
  117. }
  118. }
  119. }
  120. return nil
  121. }
  122. func (c *commandVolumeServerEvacuate) evacuateEcVolumes(commandEnv *CommandEnv, volumeServer string, skipNonMoveable, applyChange bool, writer io.Writer) error {
  123. // find this ec volume server
  124. ecNodes, _ := collectEcVolumeServersByDc(c.topologyInfo, "")
  125. thisNodes, otherNodes := c.ecNodesOtherThan(ecNodes, volumeServer)
  126. if len(thisNodes) == 0 {
  127. return fmt.Errorf("%s is not found in this cluster\n", volumeServer)
  128. }
  129. // move away ec volumes
  130. for _, thisNode := range thisNodes {
  131. for _, diskInfo := range thisNode.info.DiskInfos {
  132. for _, ecShardInfo := range diskInfo.EcShardInfos {
  133. hasMoved, err := c.moveAwayOneEcVolume(commandEnv, ecShardInfo, thisNode, otherNodes, applyChange)
  134. if err != nil {
  135. fmt.Fprintf(writer, "move away volume %d from %s: %v", ecShardInfo.Id, volumeServer, err)
  136. }
  137. if !hasMoved {
  138. if skipNonMoveable {
  139. fmt.Fprintf(writer, "failed to move away ec volume %d from %s\n", ecShardInfo.Id, volumeServer)
  140. } else {
  141. return fmt.Errorf("failed to move away ec volume %d from %s", ecShardInfo.Id, volumeServer)
  142. }
  143. }
  144. }
  145. }
  146. }
  147. return nil
  148. }
  149. func (c *commandVolumeServerEvacuate) moveAwayOneEcVolume(commandEnv *CommandEnv, ecShardInfo *master_pb.VolumeEcShardInformationMessage, thisNode *EcNode, otherNodes []*EcNode, applyChange bool) (hasMoved bool, err error) {
  150. for _, shardId := range erasure_coding.ShardBits(ecShardInfo.EcIndexBits).ShardIds() {
  151. slices.SortFunc(otherNodes, func(a, b *EcNode) bool {
  152. return a.localShardIdCount(ecShardInfo.Id) < b.localShardIdCount(ecShardInfo.Id)
  153. })
  154. for i := 0; i < len(otherNodes); i++ {
  155. emptyNode := otherNodes[i]
  156. collectionPrefix := ""
  157. if ecShardInfo.Collection != "" {
  158. collectionPrefix = ecShardInfo.Collection + "_"
  159. }
  160. fmt.Fprintf(os.Stdout, "moving ec volume %s%d.%d %s => %s\n", collectionPrefix, ecShardInfo.Id, shardId, thisNode.info.Id, emptyNode.info.Id)
  161. err = moveMountedShardToEcNode(commandEnv, thisNode, ecShardInfo.Collection, needle.VolumeId(ecShardInfo.Id), shardId, emptyNode, applyChange)
  162. if err != nil {
  163. return
  164. } else {
  165. hasMoved = true
  166. break
  167. }
  168. }
  169. if !hasMoved {
  170. return
  171. }
  172. }
  173. return
  174. }
  175. func moveAwayOneNormalVolume(commandEnv *CommandEnv, volumeReplicas map[uint32][]*VolumeReplica, vol *master_pb.VolumeInformationMessage, thisNode *Node, otherNodes []*Node, applyChange bool) (hasMoved bool, err error) {
  176. freeVolumeCountfn := capacityByFreeVolumeCount(types.ToDiskType(vol.DiskType))
  177. maxVolumeCountFn := capacityByMaxVolumeCount(types.ToDiskType(vol.DiskType))
  178. for _, n := range otherNodes {
  179. n.selectVolumes(func(v *master_pb.VolumeInformationMessage) bool {
  180. return v.DiskType == vol.DiskType
  181. })
  182. }
  183. // most empty one is in the front
  184. slices.SortFunc(otherNodes, func(a, b *Node) bool {
  185. return a.localVolumeRatio(maxVolumeCountFn) < b.localVolumeRatio(maxVolumeCountFn)
  186. })
  187. for i := 0; i < len(otherNodes); i++ {
  188. emptyNode := otherNodes[i]
  189. if freeVolumeCountfn(emptyNode.info) < 0 {
  190. continue
  191. }
  192. hasMoved, err = maybeMoveOneVolume(commandEnv, volumeReplicas, thisNode, vol, emptyNode, applyChange)
  193. if err != nil {
  194. return
  195. }
  196. if hasMoved {
  197. break
  198. }
  199. }
  200. return
  201. }
  202. func (c *commandVolumeServerEvacuate) nodesOtherThan(volumeServers []*Node, thisServer string) (thisNodes []*Node, otherNodes []*Node) {
  203. for _, node := range volumeServers {
  204. if node.info.Id == thisServer || (*c.volumeRack != "" && node.rack == *c.volumeRack) {
  205. thisNodes = append(thisNodes, node)
  206. continue
  207. }
  208. if *c.volumeRack != "" && *c.volumeRack == node.rack {
  209. continue
  210. }
  211. if *c.targetServer != "" && *c.targetServer != node.info.Id {
  212. continue
  213. }
  214. otherNodes = append(otherNodes, node)
  215. }
  216. return
  217. }
  218. func (c *commandVolumeServerEvacuate) ecNodesOtherThan(volumeServers []*EcNode, thisServer string) (thisNodes []*EcNode, otherNodes []*EcNode) {
  219. for _, node := range volumeServers {
  220. if node.info.Id == thisServer || (*c.volumeRack != "" && string(node.rack) == *c.volumeRack) {
  221. thisNodes = append(thisNodes, node)
  222. continue
  223. }
  224. if *c.volumeRack != "" && *c.volumeRack == string(node.rack) {
  225. continue
  226. }
  227. if *c.targetServer != "" && *c.targetServer != node.info.Id {
  228. continue
  229. }
  230. otherNodes = append(otherNodes, node)
  231. }
  232. return
  233. }