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.3 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
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 = 200
  11. TASK_PREDICTED_WEIGHT = 0.03
  12. )
  13. type LeastLoadFirst struct {
  14. replicas int32
  15. clusters []*CLusterLoad
  16. }
  17. type CLusterLoad struct {
  18. ClusterId string
  19. TaskRunningNum int64
  20. TaskPredictedNum int64
  21. }
  22. func NewLeastLoadFirst(replicas int32, resources []*collector.ResourceSpec) *LeastLoadFirst {
  23. var clusters []*CLusterLoad
  24. for _, resource := range resources {
  25. if resource.ClusterId == "" {
  26. continue
  27. }
  28. cluster := &CLusterLoad{
  29. ClusterId: resource.ClusterId,
  30. }
  31. var balance float64
  32. var rate float64
  33. for _, res := range resource.Resources {
  34. r, ok := res.(*collector.Usage)
  35. if !ok {
  36. continue
  37. }
  38. switch r.Type {
  39. case storeLink.RUNNINGTASK:
  40. num, ok := r.Total.Value.(int64)
  41. if !ok {
  42. continue
  43. }
  44. cluster.TaskRunningNum = num
  45. case strings.ToUpper(storeLink.BALANCE):
  46. b, ok := r.Total.Value.(float64)
  47. if !ok {
  48. continue
  49. }
  50. balance = b
  51. case strings.ToUpper(storeLink.RATE):
  52. rt, ok := r.Total.Value.(float64)
  53. if !ok {
  54. continue
  55. }
  56. rate = rt
  57. default:
  58. continue
  59. }
  60. }
  61. if balance == 0 || rate == 0 {
  62. continue
  63. }
  64. predictedNum := int64(common.RoundFloat(balance/rate, 0))
  65. cluster.TaskPredictedNum = predictedNum
  66. clusters = append(clusters, cluster)
  67. }
  68. return &LeastLoadFirst{replicas: replicas, clusters: clusters}
  69. }
  70. func (l *LeastLoadFirst) Schedule() ([]*AssignedCluster, error) {
  71. if l.replicas < 1 {
  72. return nil, errors.New("replicas must be greater than 0")
  73. }
  74. swMap := make(map[string]int32, 0)
  75. for _, cluster := range l.clusters {
  76. var runningTaskWeight int64
  77. if cluster.TaskRunningNum != 0 {
  78. runningTaskWeight = RUNNING_TASK_WEIGHT / cluster.TaskRunningNum
  79. }
  80. weight := runningTaskWeight + int64(float64(cluster.TaskPredictedNum)*TASK_PREDICTED_WEIGHT)
  81. swMap[cluster.ClusterId] = int32(weight)
  82. }
  83. sw := NewStaticWeightStrategy(swMap, l.replicas)
  84. clusters, err := sw.Schedule()
  85. if err != nil {
  86. return nil, err
  87. }
  88. return clusters, nil
  89. }

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.