command_volume_server_evacuate.go 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. sort.Slice(otherNodes, func(i, j int) bool {
  149. return otherNodes[i].localVolumeRatio(fn) > otherNodes[j].localVolumeRatio(fn)
  150. })
  151. for i := 0; i < len(otherNodes); i++ {
  152. emptyNode := otherNodes[i]
  153. hasMoved, err = maybeMoveOneVolume(commandEnv, volumeReplicas, thisNode, vol, emptyNode, applyChange)
  154. if err != nil {
  155. return
  156. }
  157. if hasMoved {
  158. break
  159. }
  160. }
  161. return
  162. }
  163. func nodesOtherThan(volumeServers []*Node, thisServer string) (thisNode *Node, otherNodes []*Node) {
  164. for _, node := range volumeServers {
  165. if node.info.Id == thisServer {
  166. thisNode = node
  167. continue
  168. }
  169. otherNodes = append(otherNodes, node)
  170. }
  171. return
  172. }
  173. func ecNodesOtherThan(volumeServers []*EcNode, thisServer string) (thisNode *EcNode, otherNodes []*EcNode) {
  174. for _, node := range volumeServers {
  175. if node.info.Id == thisServer {
  176. thisNode = node
  177. continue
  178. }
  179. otherNodes = append(otherNodes, node)
  180. }
  181. return
  182. }