You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

leastLoadFirst.go 2.6 kB

11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
11 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. package strategy
  2. import (
  3. "errors"
  4. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/common"
  5. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/service/collector"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/storeLink"
  7. "strings"
  8. )
  9. const (
  10. RUNNING_TASK_WEIGHT = 300
  11. TASK_PREDICTED_WEIGHT = 0.03
  12. UNLIMITED_PREDICTED_NUM = 10000
  13. )
  14. type LeastLoadFirst struct {
  15. replicas int32
  16. clusters []*CLusterLoad
  17. }
  18. type CLusterLoad struct {
  19. ClusterId string
  20. TaskRunningNum int64
  21. TaskPredictedNum int64
  22. }
  23. func NewLeastLoadFirst(replicas int32, resources []*collector.ResourceSpec) *LeastLoadFirst {
  24. var clusters []*CLusterLoad
  25. for _, resource := range resources {
  26. if resource.ClusterId == "" {
  27. continue
  28. }
  29. cluster := &CLusterLoad{
  30. ClusterId: resource.ClusterId,
  31. }
  32. var balance float64
  33. var rate float64
  34. for _, res := range resource.Resources {
  35. r, ok := res.(*collector.ClusterResource)
  36. if !ok {
  37. continue
  38. }
  39. switch r.Resource.Type {
  40. case storeLink.RUNNINGTASK:
  41. num, ok := r.Resource.Total.Value.(int64)
  42. if !ok {
  43. continue
  44. }
  45. cluster.TaskRunningNum = num
  46. case strings.ToUpper(storeLink.BALANCE):
  47. b, ok := r.Resource.Total.Value.(float64)
  48. if !ok {
  49. continue
  50. }
  51. balance = b
  52. case strings.ToUpper(storeLink.RATE):
  53. rt, ok := r.Resource.Total.Value.(float64)
  54. if !ok {
  55. continue
  56. }
  57. rate = rt
  58. default:
  59. continue
  60. }
  61. }
  62. if balance == 0 || rate == 0 {
  63. continue
  64. }
  65. var predictedNum int64
  66. if balance == -1 {
  67. // unlimited
  68. predictedNum = UNLIMITED_PREDICTED_NUM
  69. } else {
  70. predictedNum = int64(common.RoundFloat(balance/rate, 0))
  71. }
  72. cluster.TaskPredictedNum = predictedNum
  73. clusters = append(clusters, cluster)
  74. }
  75. return &LeastLoadFirst{replicas: replicas, clusters: clusters}
  76. }
  77. func (l *LeastLoadFirst) Schedule() ([]*AssignedCluster, error) {
  78. if l.replicas < 1 {
  79. return nil, errors.New("replicas must be greater than 0")
  80. }
  81. swMap := make(map[string]int32, 0)
  82. for _, cluster := range l.clusters {
  83. var runningTaskWeight int64
  84. if cluster.TaskRunningNum != 0 {
  85. runningTaskWeight = RUNNING_TASK_WEIGHT / cluster.TaskRunningNum
  86. } else {
  87. runningTaskWeight = RUNNING_TASK_WEIGHT * 2
  88. }
  89. weight := runningTaskWeight + int64(float64(cluster.TaskPredictedNum)*TASK_PREDICTED_WEIGHT)
  90. swMap[cluster.ClusterId] = int32(weight)
  91. }
  92. sw := NewStaticWeightStrategy(swMap, l.replicas)
  93. clusters, err := sw.Schedule()
  94. if err != nil {
  95. return nil, err
  96. }
  97. return clusters, nil
  98. }

PCM is positioned as Software stack over Cloud, aiming to build the standards and ecology of heterogeneous cloud collaboration for JCC in a non intrusive and autonomous peer-to-peer manner.