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.0 KiB

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