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.

resourcePricing.go 5.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /*
  2. Copyright (c) [2023] [pcm]
  3. [pcm-coordinator] is licensed under Mulan PSL v2.
  4. You can use this software according to the terms and conditions of the Mulan PSL v2.
  5. You may obtain a copy of Mulan PSL v2 at:
  6. http://license.coscl.org.cn/MulanPSL2
  7. THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
  8. EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
  9. MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
  10. See the Mulan PSL v2 for more details.
  11. */
  12. package strategy
  13. import (
  14. "errors"
  15. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/algorithm/providerPricing"
  16. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/strategy/param"
  17. "strconv"
  18. )
  19. type PricingStrategy struct {
  20. ProviderList []*providerPricing.Provider
  21. Task *providerPricing.Task
  22. StrategyList []*providerPricing.Strategy
  23. }
  24. func NewPricingStrategy(params *param.ResourcePricingParams) *PricingStrategy {
  25. providers := params.GetProviders()
  26. task := params.GetTask()
  27. var providerList []*providerPricing.Provider
  28. var res [][]int
  29. for _, p := range providers {
  30. p.GenMaxResourceNum(task)
  31. providerList = append(providerList, p)
  32. }
  33. providerPricing.Back_trace_task(task.Replicas, 0, providerList, 0, &res, 0)
  34. var strategyList []*providerPricing.Strategy
  35. for _, r := range res {
  36. var path []int
  37. var pathlist [][]int
  38. var resourcePerProvider []int
  39. for j, p := range providerList {
  40. if r[j] > p.MaxReplicas {
  41. resourcePerProvider = append(resourcePerProvider, p.MaxReplicas)
  42. } else {
  43. resourcePerProvider = append(resourcePerProvider, r[j])
  44. }
  45. }
  46. providerPricing.Back_trace_resource(resourcePerProvider, 0, path, &pathlist)
  47. strategy := providerPricing.NewStrategy()
  48. strategy.Tasksolution = r
  49. strategy.Resourcesolution = pathlist
  50. strategyList = append(strategyList, strategy)
  51. }
  52. return &PricingStrategy{ProviderList: providerList, Task: task, StrategyList: strategyList}
  53. }
  54. func (ps *PricingStrategy) computeMaxScore() (*providerPricing.Task, error) {
  55. maxStrategy := providerPricing.NewStrategy()
  56. var maxprofit float64
  57. //先计算出最大的利润值
  58. for _, strategy := range ps.StrategyList {
  59. for _, resourceSolu := range strategy.Resourcesolution {
  60. profit := providerPricing.ComputeProfit(ps.Task, strategy.Tasksolution, resourceSolu, ps.ProviderList)
  61. if profit > maxprofit {
  62. maxprofit = profit
  63. }
  64. }
  65. }
  66. for _, strategy := range ps.StrategyList {
  67. for _, resourceSolu := range strategy.Resourcesolution {
  68. profit := providerPricing.ComputeProfit(ps.Task, strategy.Tasksolution, resourceSolu, ps.ProviderList)
  69. highDegree := providerPricing.ComputeHighDegree(ps.Task, resourceSolu, ps.ProviderList)
  70. valueSum := profit/maxprofit + highDegree
  71. //将每个确定任务分配策略的最高的策略得分存储到里面
  72. if valueSum > maxStrategy.ValueSum {
  73. strategy.Profit = profit
  74. strategy.HighDegree = highDegree
  75. }
  76. if valueSum > maxStrategy.ValueSum {
  77. maxStrategy.ValueSum = valueSum
  78. maxStrategy.Tasksolution = strategy.Tasksolution
  79. newResourceSolu := [][]int{}
  80. newResourceSolu = append(newResourceSolu, resourceSolu)
  81. maxStrategy.Resourcesolution = newResourceSolu
  82. maxStrategy.Profit = profit
  83. maxStrategy.HighDegree = highDegree
  84. }
  85. }
  86. }
  87. if len(ps.ProviderList) == 0 {
  88. return nil, errors.New("empty providers")
  89. }
  90. ps.Task.MaxscoreStrategy = maxStrategy // 记录该任务的最终分配策略
  91. return ps.Task, nil
  92. }
  93. func (ps *PricingStrategy) produceMaxScoreStrategy() (*providerPricing.Strategy, error) {
  94. task, err := ps.computeMaxScore()
  95. if err != nil {
  96. return nil, err
  97. }
  98. //计算任务i的resourcePerTask属性
  99. for i, _ := range ps.ProviderList {
  100. tasksolu := task.MaxscoreStrategy.Tasksolution[i] // 第j个提供商分到的任务数
  101. resourcesolu := task.MaxscoreStrategy.Resourcesolution[0][i] // 第j个提供商分到的资源数
  102. // 在第j个云提供商处声明一个长度为资源数的链表
  103. resourcePerTaskPerProviders := make([]int, resourcesolu)
  104. if tasksolu > 0 {
  105. for tasksolu > 0 {
  106. for j := 0; j < resourcesolu; j++ {
  107. resourcePerTaskPerProviders[j] += 1
  108. tasksolu -= 1
  109. }
  110. }
  111. } else if tasksolu == 0 {
  112. resourcePerTaskPerProviders = []int{0}
  113. }
  114. task.ResourcePerTask = append(task.ResourcePerTask, resourcePerTaskPerProviders)
  115. }
  116. return task.MaxscoreStrategy, nil
  117. }
  118. func (ps *PricingStrategy) Schedule() ([]*AssignedCluster, error) {
  119. strategy, err := ps.produceMaxScoreStrategy()
  120. if err != nil {
  121. return nil, err
  122. }
  123. if len(strategy.Tasksolution) == 0 {
  124. return nil, errors.New("调度失败, 未能获取调度结果")
  125. }
  126. var results []*AssignedCluster
  127. for i, e := range strategy.Tasksolution {
  128. if e == 0 {
  129. continue
  130. }
  131. cluster := &AssignedCluster{ClusterId: strconv.FormatInt(ps.ProviderList[i].Pid, 10), Replicas: int32(e)}
  132. results = append(results, cluster)
  133. }
  134. if len(results) == 0 {
  135. return nil, errors.New("可用集群为空")
  136. }
  137. return results, nil
  138. }

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.