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.

239 lines
6.6 KiB

  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "os"
  8. "sort"
  9. "time"
  10. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  11. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  12. )
  13. func init() {
  14. commands = append(commands, &commandVolumeBalance{})
  15. }
  16. type commandVolumeBalance struct {
  17. }
  18. func (c *commandVolumeBalance) Name() string {
  19. return "volume.balance"
  20. }
  21. func (c *commandVolumeBalance) Help() string {
  22. return `balance all volumes among volume servers
  23. volume.balance [-c collectionName] [-f]
  24. Algorithm:
  25. For each type of volume server (different max volume count limit){
  26. balanceWritableVolumes()
  27. balanceReadOnlyVolumes()
  28. }
  29. func balanceWritableVolumes(){
  30. idealWritableVolumes = totalWritableVolumes / numVolumeServers
  31. for {
  32. sort all volume servers ordered by{
  33. local writable volumes
  34. }
  35. pick the volume server A with the lowest number of writable volumes x
  36. pick the volume server B with the highest number of writable volumes y
  37. if y > idealWritableVolumes and x +1 <= idealWritableVolumes {
  38. if B has a writable volume id v that A does not have {
  39. move writable volume v from A to B
  40. }
  41. }
  42. }
  43. }
  44. func balanceReadOnlyVolumes(){
  45. //similar to balanceWritableVolumes
  46. }
  47. `
  48. }
  49. func (c *commandVolumeBalance) Do(args []string, commandEnv *commandEnv, writer io.Writer) (err error) {
  50. balanceCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  51. collection := balanceCommand.String("c", "ALL", "collection name. use \"ALL\" for all collections")
  52. applyBalancing := balanceCommand.Bool("f", false, "apply the balancing plan.")
  53. if err = balanceCommand.Parse(args); err != nil {
  54. return nil
  55. }
  56. var resp *master_pb.VolumeListResponse
  57. ctx := context.Background()
  58. err = commandEnv.masterClient.WithClient(ctx, func(client master_pb.SeaweedClient) error {
  59. resp, err = client.VolumeList(ctx, &master_pb.VolumeListRequest{})
  60. return err
  61. })
  62. if err != nil {
  63. return err
  64. }
  65. typeToNodes := collectVolumeServersByType(resp.TopologyInfo)
  66. for _, volumeServers := range typeToNodes {
  67. if len(volumeServers) < 2 {
  68. continue
  69. }
  70. if *collection == "ALL" {
  71. collections, err := ListCollectionNames(commandEnv)
  72. if err != nil {
  73. return err
  74. }
  75. for _, c := range collections {
  76. if err = balanceVolumeServers(commandEnv, volumeServers, resp.VolumeSizeLimitMb*1024*1024, c, *applyBalancing); err != nil {
  77. return err
  78. }
  79. }
  80. if err = balanceVolumeServers(commandEnv, volumeServers, resp.VolumeSizeLimitMb*1024*1024, "ALL", *applyBalancing); err != nil {
  81. return err
  82. }
  83. } else {
  84. if err = balanceVolumeServers(commandEnv, volumeServers, resp.VolumeSizeLimitMb*1024*1024, *collection, *applyBalancing); err != nil {
  85. return err
  86. }
  87. }
  88. }
  89. return nil
  90. }
  91. func balanceVolumeServers(commandEnv *commandEnv, dataNodeInfos []*master_pb.DataNodeInfo, volumeSizeLimit uint64, collection string, applyBalancing bool) error {
  92. var nodes []*Node
  93. for _, dn := range dataNodeInfos {
  94. nodes = append(nodes, &Node{
  95. info: dn,
  96. })
  97. }
  98. // balance writable volumes
  99. for _, n := range nodes {
  100. n.selectVolumes(func(v *master_pb.VolumeInformationMessage) bool {
  101. if collection != "ALL" {
  102. if v.Collection != collection {
  103. return false
  104. }
  105. }
  106. return !v.ReadOnly && v.Size < volumeSizeLimit
  107. })
  108. }
  109. if err := balanceSelectedVolume(commandEnv, nodes, sortWritableVolumes, applyBalancing); err != nil {
  110. return err
  111. }
  112. // balance readable volumes
  113. for _, n := range nodes {
  114. n.selectVolumes(func(v *master_pb.VolumeInformationMessage) bool {
  115. if collection != "ALL" {
  116. if v.Collection != collection {
  117. return false
  118. }
  119. }
  120. return v.ReadOnly || v.Size >= volumeSizeLimit
  121. })
  122. }
  123. if err := balanceSelectedVolume(commandEnv, nodes, sortReadOnlyVolumes, applyBalancing); err != nil {
  124. return err
  125. }
  126. return nil
  127. }
  128. func collectVolumeServersByType(t *master_pb.TopologyInfo) (typeToNodes map[uint64][]*master_pb.DataNodeInfo) {
  129. typeToNodes = make(map[uint64][]*master_pb.DataNodeInfo)
  130. for _, dc := range t.DataCenterInfos {
  131. for _, r := range dc.RackInfos {
  132. for _, dn := range r.DataNodeInfos {
  133. typeToNodes[dn.MaxVolumeCount] = append(typeToNodes[dn.MaxVolumeCount], dn)
  134. }
  135. }
  136. }
  137. return
  138. }
  139. type Node struct {
  140. info *master_pb.DataNodeInfo
  141. selectedVolumes map[uint32]*master_pb.VolumeInformationMessage
  142. }
  143. func sortWritableVolumes(volumes []*master_pb.VolumeInformationMessage) {
  144. sort.Slice(volumes, func(i, j int) bool {
  145. return volumes[i].Size < volumes[j].Size
  146. })
  147. }
  148. func sortReadOnlyVolumes(volumes []*master_pb.VolumeInformationMessage) {
  149. sort.Slice(volumes, func(i, j int) bool {
  150. return volumes[i].Id < volumes[j].Id
  151. })
  152. }
  153. func balanceSelectedVolume(commandEnv *commandEnv, nodes []*Node, sortCandidatesFn func(volumes []*master_pb.VolumeInformationMessage), applyBalancing bool) error {
  154. selectedVolumeCount := 0
  155. for _, dn := range nodes {
  156. selectedVolumeCount += len(dn.selectedVolumes)
  157. }
  158. idealSelectedVolumes := selectedVolumeCount / len(nodes)
  159. hasMove := true
  160. for hasMove {
  161. hasMove = false
  162. sort.Slice(nodes, func(i, j int) bool {
  163. return len(nodes[i].selectedVolumes) < len(nodes[j].selectedVolumes)
  164. })
  165. emptyNode, fullNode := nodes[0], nodes[len(nodes)-1]
  166. if len(fullNode.selectedVolumes) > idealSelectedVolumes && len(emptyNode.selectedVolumes)+1 <= idealSelectedVolumes {
  167. // sort the volumes to move
  168. var candidateVolumes []*master_pb.VolumeInformationMessage
  169. for _, v := range fullNode.selectedVolumes {
  170. candidateVolumes = append(candidateVolumes, v)
  171. }
  172. sortCandidatesFn(candidateVolumes)
  173. for _, v := range candidateVolumes {
  174. if _, found := emptyNode.selectedVolumes[v.Id]; !found {
  175. if err := moveVolume(commandEnv, v, fullNode, emptyNode, applyBalancing); err == nil {
  176. delete(fullNode.selectedVolumes, v.Id)
  177. emptyNode.selectedVolumes[v.Id] = v
  178. hasMove = true
  179. break
  180. } else {
  181. return err
  182. }
  183. }
  184. }
  185. }
  186. }
  187. return nil
  188. }
  189. func moveVolume(commandEnv *commandEnv, v *master_pb.VolumeInformationMessage, fullNode *Node, emptyNode *Node, applyBalancing bool) error {
  190. collectionPrefix := v.Collection + "_"
  191. if v.Collection == "" {
  192. collectionPrefix = ""
  193. }
  194. fmt.Fprintf(os.Stdout, "moving volume %s%d %s => %s\n", collectionPrefix, v.Id, fullNode.info.Id, emptyNode.info.Id)
  195. if applyBalancing {
  196. ctx := context.Background()
  197. return LiveMoveVolume(ctx, commandEnv.option.GrpcDialOption, needle.VolumeId(v.Id), fullNode.info.Id, emptyNode.info.Id, 5*time.Second)
  198. }
  199. return nil
  200. }
  201. func (node *Node) selectVolumes(fn func(v *master_pb.VolumeInformationMessage) bool) {
  202. node.selectedVolumes = make(map[uint32]*master_pb.VolumeInformationMessage)
  203. for _, v := range node.info.VolumeInfos {
  204. if fn(v) {
  205. node.selectedVolumes[v.Id] = v
  206. }
  207. }
  208. }