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.

235 lines
6.6 KiB

  1. package shell
  2. import (
  3. "context"
  4. "flag"
  5. "fmt"
  6. "io"
  7. "math"
  8. "github.com/chrislusf/seaweedfs/weed/pb/master_pb"
  9. "github.com/chrislusf/seaweedfs/weed/storage/erasure_coding"
  10. "github.com/chrislusf/seaweedfs/weed/storage/needle"
  11. )
  12. func init() {
  13. commands = append(commands, &commandEcBalance{})
  14. }
  15. type commandEcBalance struct {
  16. }
  17. func (c *commandEcBalance) Name() string {
  18. return "ec.balance"
  19. }
  20. func (c *commandEcBalance) Help() string {
  21. return `balance all ec shards among volume servers
  22. ec.balance [-c EACH_COLLECTION|<collection_name>] [-f] [-dataCenter <data_center>]
  23. Algorithm:
  24. For each type of volume server (different max volume count limit){
  25. for each collection {
  26. balanceEcVolumes()
  27. }
  28. }
  29. func balanceEcVolumes(){
  30. idealWritableVolumes = totalWritableVolumes / numVolumeServers
  31. for {
  32. sort all volume servers ordered by the number of local writable volumes
  33. pick the volume server A with the lowest number of writable volumes x
  34. pick the volume server B with the highest number of writable volumes y
  35. if y > idealWritableVolumes and x +1 <= idealWritableVolumes {
  36. if B has a writable volume id v that A does not have {
  37. move writable volume v from A to B
  38. }
  39. }
  40. }
  41. }
  42. `
  43. }
  44. func (c *commandEcBalance) Do(args []string, commandEnv *commandEnv, writer io.Writer) (err error) {
  45. balanceCommand := flag.NewFlagSet(c.Name(), flag.ContinueOnError)
  46. collection := balanceCommand.String("collection", "EACH_COLLECTION", "collection name, or \"EACH_COLLECTION\" for each collection")
  47. dc := balanceCommand.String("dataCenter", "", "only apply the balancing for this dataCenter")
  48. applyBalancing := balanceCommand.Bool("force", false, "apply the balancing plan")
  49. if err = balanceCommand.Parse(args); err != nil {
  50. return nil
  51. }
  52. var resp *master_pb.VolumeListResponse
  53. ctx := context.Background()
  54. err = commandEnv.masterClient.WithClient(ctx, func(client master_pb.SeaweedClient) error {
  55. resp, err = client.VolumeList(ctx, &master_pb.VolumeListRequest{})
  56. return err
  57. })
  58. if err != nil {
  59. return err
  60. }
  61. typeToNodes := collectVolumeServersByType(resp.TopologyInfo, *dc)
  62. for _, volumeServers := range typeToNodes {
  63. fmt.Printf("balanceEcVolumes servers %d\n", len(volumeServers))
  64. if len(volumeServers) < 2 {
  65. continue
  66. }
  67. if *collection == "EACH_COLLECTION" {
  68. collections, err := ListCollectionNames(commandEnv, false, true)
  69. if err != nil {
  70. return err
  71. }
  72. fmt.Printf("balanceEcVolumes collections %+v\n", len(collections))
  73. for _, c := range collections {
  74. fmt.Printf("balanceEcVolumes collection %+v\n", c)
  75. if err = balanceEcVolumes(commandEnv, c, *applyBalancing); err != nil {
  76. return err
  77. }
  78. }
  79. } else {
  80. if err = balanceEcVolumes(commandEnv, *collection, *applyBalancing); err != nil {
  81. return err
  82. }
  83. }
  84. }
  85. return nil
  86. }
  87. func balanceEcVolumes(commandEnv *commandEnv, collection string, applyBalancing bool) error {
  88. ctx := context.Background()
  89. fmt.Printf("balanceEcVolumes %s\n", collection)
  90. // collect all ec nodes
  91. allEcNodes, totalFreeEcSlots, err := collectEcNodes(ctx, commandEnv)
  92. if err != nil {
  93. return err
  94. }
  95. if totalFreeEcSlots < 1 {
  96. return fmt.Errorf("no free ec shard slots. only %d left", totalFreeEcSlots)
  97. }
  98. // vid => []ecNode
  99. vidLocations := make(map[needle.VolumeId][]*EcNode)
  100. for _, ecNode := range allEcNodes {
  101. for _, shardInfo := range ecNode.info.EcShardInfos {
  102. vidLocations[needle.VolumeId(shardInfo.Id)] = append(vidLocations[needle.VolumeId(shardInfo.Id)], ecNode)
  103. }
  104. }
  105. for vid, locations := range vidLocations {
  106. // collect all ec nodes with at least one free slot
  107. var possibleDestinationEcNodes []*EcNode
  108. for _, ecNode := range allEcNodes {
  109. if ecNode.freeEcSlot > 0 {
  110. possibleDestinationEcNodes = append(possibleDestinationEcNodes, ecNode)
  111. }
  112. }
  113. // calculate average number of shards an ec node should have for one volume
  114. averageShardsPerEcNode := int(math.Ceil(float64(erasure_coding.TotalShardsCount) / float64(len(possibleDestinationEcNodes))))
  115. fmt.Printf("vid %d averageShardsPerEcNode %+v\n", vid, averageShardsPerEcNode)
  116. // check whether this volume has ecNodes that are over average
  117. isOverLimit := false
  118. for _, ecNode := range locations {
  119. shardBits := findEcVolumeShards(ecNode, vid)
  120. if shardBits.ShardIdCount() > averageShardsPerEcNode {
  121. isOverLimit = true
  122. fmt.Printf("vid %d %s has %d shards, isOverLimit %+v\n", vid, ecNode.info.Id, shardBits.ShardIdCount(), isOverLimit)
  123. break
  124. }
  125. }
  126. if isOverLimit {
  127. if err := spreadShardsIntoMoreDataNodes(ctx, commandEnv, averageShardsPerEcNode, collection, vid, locations, possibleDestinationEcNodes, applyBalancing); err != nil {
  128. return err
  129. }
  130. }
  131. }
  132. return nil
  133. }
  134. func spreadShardsIntoMoreDataNodes(ctx context.Context, commandEnv *commandEnv, averageShardsPerEcNode int, collection string, vid needle.VolumeId, existingLocations, possibleDestinationEcNodes []*EcNode, applyBalancing bool) error {
  135. for _, ecNode := range existingLocations {
  136. shardBits := findEcVolumeShards(ecNode, vid)
  137. overLimitCount := shardBits.ShardIdCount() - averageShardsPerEcNode
  138. for _, shardId := range shardBits.ShardIds() {
  139. if overLimitCount <= 0 {
  140. break
  141. }
  142. fmt.Printf("%s has %d overlimit, moving ec shard %d.%d\n", ecNode.info.Id, overLimitCount, vid, shardId)
  143. err := pickOneEcNodeAndMoveOneShard(ctx, commandEnv, averageShardsPerEcNode, ecNode, collection, vid, shardId, possibleDestinationEcNodes, applyBalancing)
  144. if err != nil {
  145. return err
  146. }
  147. overLimitCount--
  148. }
  149. }
  150. return nil
  151. }
  152. func pickOneEcNodeAndMoveOneShard(ctx context.Context, commandEnv *commandEnv, averageShardsPerEcNode int, existingLocation *EcNode, collection string, vid needle.VolumeId, shardId erasure_coding.ShardId, possibleDestinationEcNodes []*EcNode, applyBalancing bool) error {
  153. sortEcNodes(possibleDestinationEcNodes)
  154. for _, destEcNode := range possibleDestinationEcNodes {
  155. if destEcNode.info.Id == existingLocation.info.Id {
  156. continue
  157. }
  158. if destEcNode.freeEcSlot <= 0 {
  159. continue
  160. }
  161. if findEcVolumeShards(destEcNode, vid).ShardIdCount() >= averageShardsPerEcNode {
  162. continue
  163. }
  164. fmt.Printf("%s moves ec shard %d.%d to %s\n", existingLocation.info.Id, vid, shardId, destEcNode.info.Id)
  165. err := moveMountedShardToEcNode(ctx, commandEnv, existingLocation, collection, vid, shardId, destEcNode, applyBalancing)
  166. if err != nil {
  167. return err
  168. }
  169. destEcNode.freeEcSlot--
  170. return nil
  171. }
  172. return nil
  173. }
  174. func findEcVolumeShards(ecNode *EcNode, vid needle.VolumeId) erasure_coding.ShardBits {
  175. for _, shardInfo := range ecNode.info.EcShardInfos {
  176. if needle.VolumeId(shardInfo.Id) == vid {
  177. return erasure_coding.ShardBits(shardInfo.EcIndexBits)
  178. }
  179. }
  180. return 0
  181. }