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.

scheduler.go 6.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  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 scheduler
  13. import (
  14. "encoding/json"
  15. "fmt"
  16. "github.com/pkg/errors"
  17. "github.com/zeromicro/go-zero/core/logx"
  18. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/pkg/response"
  19. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/scheduler/algo"
  20. tool "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
  21. "gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/client/participantservice"
  22. "gorm.io/gorm"
  23. "strconv"
  24. "strings"
  25. )
  26. type scheduler struct {
  27. task *response.TaskInfo
  28. participantIds []int64
  29. scheduleService scheduleService
  30. dbEngin *gorm.DB
  31. result map[int64]string //pID:子任务yamlstring 键值对
  32. participantRpc participantservice.ParticipantService
  33. }
  34. func NewScheduler(scheduleService scheduleService, val string, dbEngin *gorm.DB, participantRpc participantservice.ParticipantService) (*scheduler, error) {
  35. var task *response.TaskInfo
  36. err := json.Unmarshal([]byte(val), &task)
  37. if err != nil {
  38. return nil, errors.New("create scheduler failed : " + err.Error())
  39. }
  40. return &scheduler{task: task, scheduleService: scheduleService, dbEngin: dbEngin, participantRpc: participantRpc, result: make(map[int64]string, 0)}, nil
  41. }
  42. func (s *scheduler) SpecifyClusters() {
  43. // 如果已指定集群名,通过数据库查询后返回p端ip列表
  44. if len(s.task.Clusters) != 0 {
  45. s.dbEngin.Raw("select id from sc_participant_phy_info where `name` in (?)", s.task.Clusters).Scan(&s.participantIds)
  46. return
  47. }
  48. }
  49. func (s *scheduler) SpecifyNsID() {
  50. // 未指定集群名,只指定nsID
  51. if len(s.task.Clusters) == 0 {
  52. if len(s.task.NsID) != 0 {
  53. var clusters string
  54. s.dbEngin.Raw("select clusters from sc_tenant_info where `tenant_name` = ?", s.task.NsID).Scan(&clusters)
  55. clusterArr := strings.Split(clusters, ",")
  56. s.dbEngin.Raw("select id from sc_participant_phy_info where `name` in (?)", clusterArr).Scan(&s.participantIds)
  57. }
  58. } else {
  59. return
  60. }
  61. }
  62. func (s *scheduler) MatchLabels() {
  63. var ids []int64
  64. count := 0
  65. // 集群和nsID都未指定,则通过标签匹配
  66. if len(s.task.Clusters) == 0 && len(s.task.NsID) == 0 {
  67. //如果集群列表或nsID均未指定
  68. for key := range s.task.MatchLabels {
  69. var participantIds []int64
  70. s.dbEngin.Raw("select participant_id from sc_participant_label_info where `key` = ? and value = ?", key, s.task.MatchLabels[key]).Scan(&participantIds)
  71. if count == 0 {
  72. ids = participantIds
  73. }
  74. ids = intersect(ids, participantIds)
  75. count++
  76. }
  77. s.participantIds = ids
  78. } else {
  79. return
  80. }
  81. }
  82. // TempAssign todo 屏蔽原调度算法
  83. func (s *scheduler) TempAssign() error {
  84. //需要判断task中的资源类型,针对metadata中的多个kind做不同处理
  85. //输入副本数和集群列表,最终结果输出为pID对应副本数量列表,针对多个kind需要做拆分和重新拼接组合
  86. var resources []interface{}
  87. tool.Convert(s.task.Metadata, &resources)
  88. for _, resource := range resources {
  89. //如果是Deployment,需要对副本数做分发
  90. if resource.(map[string]interface{})["kind"].(string) == "Deployment" || resource.(map[string]interface{})["kind"].(string) == "Replicaset" {
  91. resource.(map[string]interface{})["spec"].(map[string]interface{})["replicas"] = s.task.Replicas
  92. }
  93. }
  94. s.task.Metadata = resources
  95. return nil
  96. }
  97. func (s *scheduler) AssignAndSchedule() error {
  98. // 已指定 ParticipantId
  99. if s.task.ParticipantId != 0 {
  100. return nil
  101. }
  102. // 标签匹配以及后,未找到ParticipantIds
  103. if len(s.participantIds) == 0 {
  104. return errors.New("未找到匹配的ParticipantIds")
  105. }
  106. // 指定或者标签匹配的结果只有一个集群,给任务信息指定
  107. if len(s.participantIds) == 1 {
  108. s.task.ParticipantId = s.participantIds[0]
  109. replicas := s.task.Metadata.(map[string]interface{})["spec"].(map[string]interface{})["replicas"].(float64)
  110. result := make(map[int64]string)
  111. result[s.participantIds[0]] = strconv.FormatFloat(replicas, 'f', 2, 64)
  112. s.result = result
  113. return nil
  114. }
  115. //生成算法所需参数
  116. task, providerList, err := s.obtainParamsforStrategy()
  117. if err != nil {
  118. return err
  119. }
  120. //集群数量不满足,指定到标签匹配后第一个集群
  121. if len(providerList) < 2 {
  122. s.task.ParticipantId = s.participantIds[0]
  123. return nil
  124. }
  125. //调度算法
  126. strategy, err := s.scheduleService.pickOptimalStrategy(task, providerList...)
  127. if err != nil {
  128. return err
  129. }
  130. //调度结果
  131. err = s.assignReplicasToResult(strategy, providerList)
  132. if err != nil {
  133. return err
  134. }
  135. return nil
  136. }
  137. func (s *scheduler) SaveToDb() error {
  138. for key, value := range s.result {
  139. num, err := strconv.Atoi(value)
  140. if err != nil {
  141. fmt.Println("转换失败:", err)
  142. }
  143. structForDb, err := s.scheduleService.getNewStructForDb(s.task, int64(key), int32(num))
  144. if err != nil {
  145. return err
  146. }
  147. tx := s.dbEngin.Create(structForDb)
  148. if tx.Error != nil {
  149. logx.Error(tx.Error)
  150. return tx.Error
  151. }
  152. }
  153. return nil
  154. }
  155. func (s *scheduler) obtainParamsforStrategy() (*algo.Task, []*algo.Provider, error) {
  156. task, providerList := s.scheduleService.genTaskAndProviders(s.task, s.dbEngin)
  157. if len(providerList) == 0 {
  158. return nil, nil, errors.New("获取集群失败")
  159. }
  160. return task, providerList, nil
  161. }
  162. func (s *scheduler) assignReplicasToResult(strategy *algo.Strategy, providerList []*algo.Provider) error {
  163. if len(strategy.Tasksolution) == 0 {
  164. return errors.New("调度失败, 未能获取调度结果")
  165. }
  166. for i, e := range strategy.Tasksolution {
  167. if e == 0 {
  168. continue
  169. }
  170. s.result[providerList[i].Pid] = string(e)
  171. }
  172. if len(s.result) == 0 {
  173. return errors.New("可用集群为空")
  174. }
  175. return nil
  176. }

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.