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.

230 lines
7.8 KiB

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