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.

273 lines
9.0 KiB

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