command_volume_server_evacuate.go 7.4 KB

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