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.

268 lines
9.2 KiB

3 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. defer func() {
  73. c.topologyInfo = nil
  74. }()
  75. if err := c.evacuateNormalVolumes(commandEnv, volumeServer, skipNonMoveable, applyChange, writer); err != nil {
  76. return err
  77. }
  78. if err := c.evacuateEcVolumes(commandEnv, volumeServer, skipNonMoveable, applyChange, writer); err != nil {
  79. return err
  80. }
  81. return nil
  82. }
  83. func (c *commandVolumeServerEvacuate) evacuateNormalVolumes(commandEnv *CommandEnv, volumeServer string, skipNonMoveable, applyChange bool, writer io.Writer) error {
  84. // find this volume server
  85. volumeServers := collectVolumeServersByDc(c.topologyInfo, "")
  86. thisNodes, otherNodes := c.nodesOtherThan(volumeServers, volumeServer)
  87. if len(thisNodes) == 0 {
  88. return fmt.Errorf("%s is not found in this cluster", volumeServer)
  89. }
  90. // move away normal volumes
  91. for _, thisNode := range thisNodes {
  92. for _, diskInfo := range thisNode.info.DiskInfos {
  93. if applyChange {
  94. if topologyInfo, _, err := collectTopologyInfo(commandEnv, 0); err != nil {
  95. fmt.Fprintf(writer, "update topologyInfo %v", err)
  96. } else {
  97. _, otherNodesNew := c.nodesOtherThan(
  98. collectVolumeServersByDc(topologyInfo, ""), volumeServer)
  99. if len(otherNodesNew) > 0 {
  100. otherNodes = otherNodesNew
  101. c.topologyInfo = topologyInfo
  102. fmt.Fprintf(writer, "topologyInfo updated %v\n", len(otherNodes))
  103. }
  104. }
  105. }
  106. volumeReplicas, _ := collectVolumeReplicaLocations(c.topologyInfo)
  107. for _, vol := range diskInfo.VolumeInfos {
  108. hasMoved, err := moveAwayOneNormalVolume(commandEnv, volumeReplicas, vol, thisNode, otherNodes, applyChange)
  109. if err != nil {
  110. fmt.Fprintf(writer, "move away volume %d from %s: %v\n", vol.Id, volumeServer, err)
  111. }
  112. if !hasMoved {
  113. if skipNonMoveable {
  114. replicaPlacement, _ := super_block.NewReplicaPlacementFromByte(byte(vol.ReplicaPlacement))
  115. fmt.Fprintf(writer, "skipping non moveable volume %d replication:%s\n", vol.Id, replicaPlacement.String())
  116. } else {
  117. return fmt.Errorf("failed to move volume %d from %s", vol.Id, volumeServer)
  118. }
  119. }
  120. }
  121. }
  122. }
  123. return nil
  124. }
  125. func (c *commandVolumeServerEvacuate) evacuateEcVolumes(commandEnv *CommandEnv, volumeServer string, skipNonMoveable, applyChange bool, writer io.Writer) error {
  126. // find this ec volume server
  127. ecNodes, _ := collectEcVolumeServersByDc(c.topologyInfo, "")
  128. thisNodes, otherNodes := c.ecNodesOtherThan(ecNodes, volumeServer)
  129. if len(thisNodes) == 0 {
  130. return fmt.Errorf("%s is not found in this cluster\n", volumeServer)
  131. }
  132. // move away ec volumes
  133. for _, thisNode := range thisNodes {
  134. for _, diskInfo := range thisNode.info.DiskInfos {
  135. for _, ecShardInfo := range diskInfo.EcShardInfos {
  136. hasMoved, err := c.moveAwayOneEcVolume(commandEnv, ecShardInfo, thisNode, otherNodes, applyChange)
  137. if err != nil {
  138. fmt.Fprintf(writer, "move away volume %d from %s: %v", ecShardInfo.Id, volumeServer, err)
  139. }
  140. if !hasMoved {
  141. if skipNonMoveable {
  142. fmt.Fprintf(writer, "failed to move away ec volume %d from %s\n", ecShardInfo.Id, volumeServer)
  143. } else {
  144. return fmt.Errorf("failed to move away ec volume %d from %s", ecShardInfo.Id, volumeServer)
  145. }
  146. }
  147. }
  148. }
  149. }
  150. return nil
  151. }
  152. func (c *commandVolumeServerEvacuate) moveAwayOneEcVolume(commandEnv *CommandEnv, ecShardInfo *master_pb.VolumeEcShardInformationMessage, thisNode *EcNode, otherNodes []*EcNode, applyChange bool) (hasMoved bool, err error) {
  153. for _, shardId := range erasure_coding.ShardBits(ecShardInfo.EcIndexBits).ShardIds() {
  154. slices.SortFunc(otherNodes, func(a, b *EcNode) bool {
  155. return a.localShardIdCount(ecShardInfo.Id) < b.localShardIdCount(ecShardInfo.Id)
  156. })
  157. for i := 0; i < len(otherNodes); i++ {
  158. emptyNode := otherNodes[i]
  159. collectionPrefix := ""
  160. if ecShardInfo.Collection != "" {
  161. collectionPrefix = ecShardInfo.Collection + "_"
  162. }
  163. fmt.Fprintf(os.Stdout, "moving ec volume %s%d.%d %s => %s\n", collectionPrefix, ecShardInfo.Id, shardId, thisNode.info.Id, emptyNode.info.Id)
  164. err = moveMountedShardToEcNode(commandEnv, thisNode, ecShardInfo.Collection, needle.VolumeId(ecShardInfo.Id), shardId, emptyNode, applyChange)
  165. if err != nil {
  166. return
  167. } else {
  168. hasMoved = true
  169. break
  170. }
  171. }
  172. if !hasMoved {
  173. return
  174. }
  175. }
  176. return
  177. }
  178. func moveAwayOneNormalVolume(commandEnv *CommandEnv, volumeReplicas map[uint32][]*VolumeReplica, vol *master_pb.VolumeInformationMessage, thisNode *Node, otherNodes []*Node, applyChange bool) (hasMoved bool, err error) {
  179. freeVolumeCountfn := capacityByFreeVolumeCount(types.ToDiskType(vol.DiskType))
  180. maxVolumeCountFn := capacityByMaxVolumeCount(types.ToDiskType(vol.DiskType))
  181. for _, n := range otherNodes {
  182. n.selectVolumes(func(v *master_pb.VolumeInformationMessage) bool {
  183. return v.DiskType == vol.DiskType
  184. })
  185. }
  186. // most empty one is in the front
  187. slices.SortFunc(otherNodes, func(a, b *Node) bool {
  188. return a.localVolumeRatio(maxVolumeCountFn) < b.localVolumeRatio(maxVolumeCountFn)
  189. })
  190. for i := 0; i < len(otherNodes); i++ {
  191. emptyNode := otherNodes[i]
  192. if freeVolumeCountfn(emptyNode.info) < 0 {
  193. continue
  194. }
  195. hasMoved, err = maybeMoveOneVolume(commandEnv, volumeReplicas, thisNode, vol, emptyNode, applyChange)
  196. if err != nil {
  197. return
  198. }
  199. if hasMoved {
  200. break
  201. }
  202. }
  203. return
  204. }
  205. func (c *commandVolumeServerEvacuate) nodesOtherThan(volumeServers []*Node, thisServer string) (thisNodes []*Node, otherNodes []*Node) {
  206. for _, node := range volumeServers {
  207. if node.info.Id == thisServer || (*c.volumeRack != "" && node.rack == *c.volumeRack) {
  208. thisNodes = append(thisNodes, node)
  209. continue
  210. }
  211. if *c.volumeRack != "" && *c.volumeRack == node.rack {
  212. continue
  213. }
  214. if *c.targetServer != "" && *c.targetServer != node.info.Id {
  215. continue
  216. }
  217. otherNodes = append(otherNodes, node)
  218. }
  219. return
  220. }
  221. func (c *commandVolumeServerEvacuate) ecNodesOtherThan(volumeServers []*EcNode, thisServer string) (thisNodes []*EcNode, otherNodes []*EcNode) {
  222. for _, node := range volumeServers {
  223. if node.info.Id == thisServer || (*c.volumeRack != "" && string(node.rack) == *c.volumeRack) {
  224. thisNodes = append(thisNodes, node)
  225. continue
  226. }
  227. if *c.volumeRack != "" && *c.volumeRack == string(node.rack) {
  228. continue
  229. }
  230. if *c.targetServer != "" && *c.targetServer != node.info.Id {
  231. continue
  232. }
  233. otherNodes = append(otherNodes, node)
  234. }
  235. return
  236. }