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.

k8sStrategy.go 8.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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 algo
  13. import (
  14. "errors"
  15. "gonum.org/v1/gonum/mat"
  16. "math"
  17. )
  18. type k8sStrategy struct {
  19. ProviderList []*Provider
  20. Task *Task
  21. StrategyList []*Strategy
  22. }
  23. func NewK8sStrategy(task *Task, providers ...*Provider) *k8sStrategy {
  24. var providerList []*Provider
  25. var res [][]int
  26. for _, p := range providers {
  27. p.GenMaxResourceNum(task)
  28. providerList = append(providerList, p)
  29. }
  30. back_trace_task(task.Replicas, 0, providerList, 0, &res, 0)
  31. var strategyList []*Strategy
  32. for _, r := range res {
  33. var path []int
  34. var pathlist [][]int
  35. var resourcePerProvider []int
  36. for j, p := range providerList {
  37. if r[j] > p.MaxReplicas {
  38. resourcePerProvider = append(resourcePerProvider, p.MaxReplicas)
  39. } else {
  40. resourcePerProvider = append(resourcePerProvider, r[j])
  41. }
  42. }
  43. back_trace_resource(resourcePerProvider, 0, path, &pathlist)
  44. strategy := NewStrategy()
  45. strategy.Tasksolution = r
  46. strategy.Resourcesolution = pathlist
  47. strategyList = append(strategyList, strategy)
  48. }
  49. return &k8sStrategy{ProviderList: providerList, Task: task, StrategyList: strategyList}
  50. }
  51. func (ps *k8sStrategy) computeMaxScore() (*Task, error) {
  52. maxStrategy := NewStrategy()
  53. var maxprofit float64
  54. //先计算出最大的利润值
  55. for _, strategy := range ps.StrategyList {
  56. for _, resourceSolu := range strategy.Resourcesolution {
  57. profit := computeProfit(ps.Task, strategy.Tasksolution, resourceSolu, ps.ProviderList)
  58. if profit > maxprofit {
  59. maxprofit = profit
  60. }
  61. }
  62. }
  63. for _, strategy := range ps.StrategyList {
  64. for _, resourceSolu := range strategy.Resourcesolution {
  65. profit := computeProfit(ps.Task, strategy.Tasksolution, resourceSolu, ps.ProviderList)
  66. highDegree := computeHighDegree(ps.Task, resourceSolu, ps.ProviderList)
  67. valueSum := profit/maxprofit + highDegree
  68. //将每个确定任务分配策略的最高的策略得分存储到里面
  69. if valueSum > maxStrategy.ValueSum {
  70. strategy.Profit = profit
  71. strategy.HighDegree = highDegree
  72. }
  73. if valueSum > maxStrategy.ValueSum {
  74. maxStrategy.ValueSum = valueSum
  75. maxStrategy.Tasksolution = strategy.Tasksolution
  76. newResourceSolu := [][]int{}
  77. newResourceSolu = append(newResourceSolu, resourceSolu)
  78. maxStrategy.Resourcesolution = newResourceSolu
  79. maxStrategy.Profit = profit
  80. maxStrategy.HighDegree = highDegree
  81. }
  82. }
  83. }
  84. if len(ps.ProviderList) == 0 {
  85. return nil, errors.New("empty providers")
  86. }
  87. ps.Task.MaxscoreStrategy = maxStrategy // 记录该任务的最终分配策略
  88. return ps.Task, nil
  89. }
  90. func computeProfit(task *Task, tasksolution []int, resourcesolution []int, providerList []*Provider) float64 {
  91. var timeexecution int //记录任务的实际最大执行时间
  92. var costSum float64 //该任务在多个云厂商所需支付的成本总价
  93. for i, provider := range providerList {
  94. //如果该厂商分的任务为0,则直接跳过该厂商,循环到下一厂商
  95. if tasksolution[i] == 0 {
  96. continue
  97. }
  98. //先计算下该云厂商的执行时间ddl,并替换任务的最大执行时间,向上取整
  99. t := math.Ceil(float64(tasksolution[i])/float64(resourcesolution[i])) * float64(task.Time)
  100. if int(t) > timeexecution {
  101. timeexecution = int(t)
  102. }
  103. //计算前几份资源多执行任务
  104. forOneMoreTaskNUm := tasksolution[i] % resourcesolution[i]
  105. for j := 0; j < resourcesolution[i]; j++ {
  106. if j < forOneMoreTaskNUm {
  107. t = math.Ceil(float64(tasksolution[i])/float64(resourcesolution[i])) * float64(task.Time)
  108. } else {
  109. t = math.Floor(float64(tasksolution[i])/float64(resourcesolution[i])) * float64(task.Time)
  110. }
  111. //如果这份资源分的的任务数
  112. cost := (provider.CpuCost*task.Cpu + provider.MemCost*task.Mem + provider.DiskCost*task.Disk) * t * (math.Pow(float64(j+1), math.Log2(provider.LearnIndex)))
  113. costSum += cost
  114. }
  115. }
  116. //计算用户的支付价格pay
  117. pay := task.Pr
  118. if timeexecution == task.Time { //没有排队等待,且只有一个副本直接执行或者多个副本完全并行执行
  119. if pay < costSum {
  120. pay = costSum
  121. }
  122. } else if timeexecution >= task.T0 && timeexecution <= task.T1 { //有排队时间或者任务存在串行执行
  123. if task.T1 == task.T0 { //仅有一个副本,时间中有排队时间
  124. e := math.Exp(float64(-task.B) * float64(timeexecution-task.T1))
  125. pay = (1 - 1/(1+e)) * task.Pr
  126. } else { //多个副本
  127. e := math.Exp(float64(-task.B) * float64(timeexecution-task.T1) / float64(task.T1-task.T0))
  128. pay = (1 - 1/(1+e)) * task.Pr
  129. }
  130. if pay < costSum {
  131. pay = costSum
  132. }
  133. } else { //超出用户满意度的完全串行时间
  134. pay = 1 / 2 * task.Pr
  135. if pay < costSum {
  136. pay = costSum
  137. }
  138. }
  139. profitSum := pay - costSum
  140. return profitSum
  141. }
  142. func computeHighDegree(task *Task, resourcesolution []int, providerList []*Provider) float64 {
  143. var highDegreeSum float64
  144. // 依次计算每个云厂商的资源可用度
  145. for i, provider := range providerList {
  146. // 定义两个四维向量
  147. // 未来任务资源需求比例
  148. futureDemand := mat.NewVecDense(3, []float64{1, 1, 1})
  149. // 定义假设按此方案分配后的剩余资源可用量,时间虽然有差异,但是先按那个时刻算吧,这里可能还要改一下
  150. nowLeft_cpu := provider.CpuAvail - task.Cpu*float64(resourcesolution[i])
  151. nowLeft_mem := provider.MemAvail - task.Mem*float64(resourcesolution[i])
  152. nowLeft_disk := provider.DiskAvail - task.Disk*float64(resourcesolution[i])
  153. nowLeft := mat.NewVecDense(3, []float64{nowLeft_cpu, nowLeft_mem, nowLeft_disk})
  154. // 使用余弦相似度计算两个比值的相近度
  155. // 计算向量的内积
  156. dot_product := mat.Dot(futureDemand, nowLeft)
  157. // 计算向量的模长
  158. magnitude1 := mat.Norm(futureDemand, 2)
  159. magnitude2 := mat.Norm(nowLeft, 2)
  160. // 计算余弦相似度
  161. //临时处理被除数为0的特殊情况
  162. var cosineSimilarity = 0.0
  163. if magnitude1 != 0 && magnitude2 != 0 {
  164. cosineSimilarity = dot_product / (magnitude1 * magnitude2)
  165. }
  166. highDegreeSum += cosineSimilarity
  167. }
  168. return highDegreeSum / float64(len(providerList))
  169. }
  170. func back_trace_task(ReplicaNum int, DoneReplicasNum int, providerList []*Provider, staclu int, res *[][]int, sum int) {
  171. //var count int = 0
  172. pnum := len(providerList)
  173. //所有的任务数都已经进行分配
  174. if DoneReplicasNum == ReplicaNum {
  175. var a []int
  176. for i := 0; i < pnum; i++ {
  177. a = append(a, providerList[i].CurReplicas)
  178. }
  179. *res = append(*res, a)
  180. //(*res)[0] = append((*res)[0], a)
  181. //count += 1
  182. return
  183. }
  184. //遍历完所有的云厂商序号
  185. if staclu >= pnum {
  186. return
  187. }
  188. if providerList[staclu].CurReplicas < providerList[staclu].MaxTaskCanRun {
  189. providerList[staclu].CurReplicas += 1
  190. back_trace_task(ReplicaNum, DoneReplicasNum+1, providerList, staclu, res, sum)
  191. providerList[staclu].CurReplicas -= 1
  192. back_trace_task(ReplicaNum, DoneReplicasNum, providerList, staclu+1, res, sum)
  193. } else {
  194. back_trace_task(ReplicaNum, DoneReplicasNum, providerList, staclu+1, res, sum)
  195. }
  196. }
  197. func back_trace_resource(list []int, i int, path []int, pathlist *[][]int) {
  198. if i == len(list) {
  199. var pathCopy = make([]int, len(path))
  200. copy(pathCopy, path)
  201. *pathlist = append(*pathlist, pathCopy)
  202. return
  203. }
  204. if list[i] == 0 {
  205. path = append(path, 0)
  206. back_trace_resource(list, i+1, path, pathlist)
  207. path = path[:len(path)-1]
  208. } else {
  209. for j := 1; j < list[i]+1; j++ {
  210. path = append(path, j)
  211. back_trace_resource(list, i+1, path, pathlist)
  212. path = path[:len(path)-1]
  213. }
  214. }
  215. }

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.