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.

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