command_volume_server_evacuate.go 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. package shell
  2. import (
  3. "flag"
  4. "fmt"
  5. "io"
  6. "os"
  7. "github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
  8. "github.com/seaweedfs/seaweedfs/weed/storage/erasure_coding"
  9. "github.com/seaweedfs/seaweedfs/weed/storage/needle"
  10. "github.com/seaweedfs/seaweedfs/weed/storage/super_block"
  11. "github.com/seaweedfs/seaweedfs/weed/storage/types"
  12. "golang.org/x/exp/slices"
  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) HasTag(CommandTag) bool {
  38. return false
  39. }
  40. func (c *commandVolumeServerEvacuate) Do(args []string, commandEnv *CommandEnv, writer io.Writer) (err error) {
  41. vsEvacuateCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  42. volumeServer := vsEvacuateCommand.String("node", "", "<host>:<port> of the volume server")
  43. c.volumeRack = vsEvacuateCommand.String("rack", "", "source rack for the volume servers")
  44. c.targetServer = vsEvacuateCommand.String("target", "", "<host>:<port> of target volume")
  45. skipNonMoveable := vsEvacuateCommand.Bool("skipNonMoveable", false, "skip volumes that can not be moved")
  46. applyChange := vsEvacuateCommand.Bool("force", false, "actually apply the changes")
  47. retryCount := vsEvacuateCommand.Int("retry", 0, "how many times to retry")
  48. if err = vsEvacuateCommand.Parse(args); err != nil {
  49. return nil
  50. }
  51. infoAboutSimulationMode(writer, *applyChange, "-force")
  52. if err = commandEnv.confirmIsLocked(args); err != nil && *applyChange {
  53. return
  54. }
  55. if *volumeServer == "" && *c.volumeRack == "" {
  56. return fmt.Errorf("need to specify volume server by -node=<host>:<port> or source rack")
  57. }
  58. for i := 0; i < *retryCount+1; i++ {
  59. if err = c.volumeServerEvacuate(commandEnv, *volumeServer, *skipNonMoveable, *applyChange, writer); err == nil {
  60. return nil
  61. }
  62. }
  63. return
  64. }
  65. func (c *commandVolumeServerEvacuate) volumeServerEvacuate(commandEnv *CommandEnv, volumeServer string, skipNonMoveable, applyChange bool, writer io.Writer) (err error) {
  66. // 1. confirm the volume server is part of the cluster
  67. // 2. collect all other volume servers, sort by empty slots
  68. // 3. move to any other volume server as long as it satisfy the replication requirements
  69. // list all the volumes
  70. // collect topology information
  71. c.topologyInfo, _, err = collectTopologyInfo(commandEnv, 0)
  72. if err != nil {
  73. return err
  74. }
  75. defer func() {
  76. c.topologyInfo = nil
  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\n", 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) int {
  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. freeVolumeCountfn := capacityByFreeVolumeCount(types.ToDiskType(vol.DiskType))
  183. maxVolumeCountFn := capacityByMaxVolumeCount(types.ToDiskType(vol.DiskType))
  184. for _, n := range otherNodes {
  185. n.selectVolumes(func(v *master_pb.VolumeInformationMessage) bool {
  186. return v.DiskType == vol.DiskType
  187. })
  188. }
  189. // most empty one is in the front
  190. slices.SortFunc(otherNodes, func(a, b *Node) int {
  191. return int(a.localVolumeRatio(maxVolumeCountFn) - b.localVolumeRatio(maxVolumeCountFn))
  192. })
  193. for i := 0; i < len(otherNodes); i++ {
  194. emptyNode := otherNodes[i]
  195. if freeVolumeCountfn(emptyNode.info) <= 0 {
  196. continue
  197. }
  198. hasMoved, err = maybeMoveOneVolume(commandEnv, volumeReplicas, thisNode, vol, emptyNode, applyChange)
  199. if err != nil {
  200. return
  201. }
  202. if hasMoved {
  203. break
  204. }
  205. }
  206. return
  207. }
  208. func (c *commandVolumeServerEvacuate) nodesOtherThan(volumeServers []*Node, thisServer string) (thisNodes []*Node, otherNodes []*Node) {
  209. for _, node := range volumeServers {
  210. if node.info.Id == thisServer || (*c.volumeRack != "" && node.rack == *c.volumeRack) {
  211. thisNodes = append(thisNodes, node)
  212. continue
  213. }
  214. if *c.volumeRack != "" && *c.volumeRack == node.rack {
  215. continue
  216. }
  217. if *c.targetServer != "" && *c.targetServer != node.info.Id {
  218. continue
  219. }
  220. otherNodes = append(otherNodes, node)
  221. }
  222. return
  223. }
  224. func (c *commandVolumeServerEvacuate) ecNodesOtherThan(volumeServers []*EcNode, thisServer string) (thisNodes []*EcNode, otherNodes []*EcNode) {
  225. for _, node := range volumeServers {
  226. if node.info.Id == thisServer || (*c.volumeRack != "" && string(node.rack) == *c.volumeRack) {
  227. thisNodes = append(thisNodes, node)
  228. continue
  229. }
  230. if *c.volumeRack != "" && *c.volumeRack == string(node.rack) {
  231. continue
  232. }
  233. if *c.targetServer != "" && *c.targetServer != node.info.Id {
  234. continue
  235. }
  236. otherNodes = append(otherNodes, node)
  237. }
  238. return
  239. }