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.

221 lines
7.2 KiB

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