command_volume_server_evacuate.go 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214
  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  7. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  8. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  9. "github.com/chrislusf/seaweedfs/weed/storage/super_block"
  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. var resp *master_pb.VolumeListResponse
  56. err = commandEnv.MasterClient.WithClient(func(client master_pb.SeaweedClient) error {
  57. resp, err = client.VolumeList(context.Background(), &master_pb.VolumeListRequest{})
  58. return err
  59. })
  60. if err != nil {
  61. return err
  62. }
  63. if err := evacuateNormalVolumes(commandEnv, resp, volumeServer, skipNonMoveable, applyChange, writer); err != nil {
  64. return err
  65. }
  66. if err := evacuateEcVolumes(commandEnv, resp, volumeServer, skipNonMoveable, applyChange, writer); err != nil {
  67. return err
  68. }
  69. return nil
  70. }
  71. func evacuateNormalVolumes(commandEnv *CommandEnv, resp *master_pb.VolumeListResponse, volumeServer string, skipNonMoveable, applyChange bool, writer io.Writer) error {
  72. // find this volume server
  73. volumeServers := collectVolumeServersByDc(resp.TopologyInfo, "")
  74. thisNode, otherNodes := nodesOtherThan(volumeServers, volumeServer)
  75. if thisNode == nil {
  76. return fmt.Errorf("%s is not found in this cluster", volumeServer)
  77. }
  78. // move away normal volumes
  79. volumeReplicas, _ := collectVolumeReplicaLocations(resp)
  80. for _, vol := range thisNode.info.VolumeInfos {
  81. hasMoved, err := moveAwayOneNormalVolume(commandEnv, volumeReplicas, vol, thisNode, otherNodes, applyChange)
  82. if err != nil {
  83. return fmt.Errorf("move away volume %d from %s: %v", vol.Id, volumeServer, err)
  84. }
  85. if !hasMoved {
  86. if skipNonMoveable {
  87. replicaPlacement, _ := super_block.NewReplicaPlacementFromByte(byte(vol.ReplicaPlacement))
  88. fmt.Fprintf(writer, "skipping non moveable volume %d replication:%s\n", vol.Id, replicaPlacement.String())
  89. } else {
  90. return fmt.Errorf("failed to move volume %d from %s", vol.Id, volumeServer)
  91. }
  92. }
  93. }
  94. return nil
  95. }
  96. func evacuateEcVolumes(commandEnv *CommandEnv, resp *master_pb.VolumeListResponse, volumeServer string, skipNonMoveable, applyChange bool, writer io.Writer) error {
  97. // find this ec volume server
  98. ecNodes, _ := collectEcVolumeServersByDc(resp.TopologyInfo, "")
  99. thisNode, otherNodes := ecNodesOtherThan(ecNodes, volumeServer)
  100. if thisNode == nil {
  101. return fmt.Errorf("%s is not found in this cluster\n", volumeServer)
  102. }
  103. // move away ec volumes
  104. for _, ecShardInfo := range thisNode.info.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. return nil
  118. }
  119. func moveAwayOneEcVolume(commandEnv *CommandEnv, ecShardInfo *master_pb.VolumeEcShardInformationMessage, thisNode *EcNode, otherNodes []*EcNode, applyChange bool) (hasMoved bool, err error) {
  120. for _, shardId := range erasure_coding.ShardBits(ecShardInfo.EcIndexBits).ShardIds() {
  121. sort.Slice(otherNodes, func(i, j int) bool {
  122. return otherNodes[i].localShardIdCount(ecShardInfo.Id) < otherNodes[j].localShardIdCount(ecShardInfo.Id)
  123. })
  124. for i := 0; i < len(otherNodes); i++ {
  125. emptyNode := otherNodes[i]
  126. collectionPrefix := ""
  127. if ecShardInfo.Collection != "" {
  128. collectionPrefix = ecShardInfo.Collection + "_"
  129. }
  130. fmt.Fprintf(os.Stdout, "moving ec volume %s%d.%d %s => %s\n", collectionPrefix, ecShardInfo.Id, shardId, thisNode.info.Id, emptyNode.info.Id)
  131. err = moveMountedShardToEcNode(commandEnv, thisNode, ecShardInfo.Collection, needle.VolumeId(ecShardInfo.Id), shardId, emptyNode, applyChange)
  132. if err != nil {
  133. return
  134. } else {
  135. hasMoved = true
  136. break
  137. }
  138. }
  139. if !hasMoved {
  140. return
  141. }
  142. }
  143. return
  144. }
  145. func moveAwayOneNormalVolume(commandEnv *CommandEnv, volumeReplicas map[uint32][]*VolumeReplica, vol *master_pb.VolumeInformationMessage, thisNode *Node, otherNodes []*Node, applyChange bool) (hasMoved bool, err error) {
  146. sort.Slice(otherNodes, func(i, j int) bool {
  147. return otherNodes[i].localVolumeRatio() < otherNodes[j].localVolumeRatio()
  148. })
  149. for i := 0; i < len(otherNodes); i++ {
  150. emptyNode := otherNodes[i]
  151. hasMoved, err = maybeMoveOneVolume(commandEnv, volumeReplicas, thisNode, vol, emptyNode, applyChange)
  152. if err != nil {
  153. return
  154. }
  155. if hasMoved {
  156. break
  157. }
  158. }
  159. return
  160. }
  161. func nodesOtherThan(volumeServers []*Node, thisServer string) (thisNode *Node, otherNodes []*Node) {
  162. for _, node := range volumeServers {
  163. if node.info.Id == thisServer {
  164. thisNode = node
  165. continue
  166. }
  167. otherNodes = append(otherNodes, node)
  168. }
  169. return
  170. }
  171. func ecNodesOtherThan(volumeServers []*EcNode, thisServer string) (thisNode *EcNode, otherNodes []*EcNode) {
  172. for _, node := range volumeServers {
  173. if node.info.Id == thisServer {
  174. thisNode = node
  175. continue
  176. }
  177. otherNodes = append(otherNodes, node)
  178. }
  179. return
  180. }