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.2 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. "github.com/pkg/errors"
  16. "github.com/zeromicro/go-zero/core/logx"
  17. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/pkg/response"
  18. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/scheduler/algo"
  19. tool "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
  20. "gitlink.org.cn/jcce-pcm/pcm-coordinator/rpc/client/participantservice"
  21. "gorm.io/gorm"
  22. "strconv"
  23. "strings"
  24. )
  25. type scheduler struct {
  26. task *response.TaskInfo
  27. participantIds []int64
  28. scheduleService scheduleService
  29. dbEngin *gorm.DB
  30. result []string //pID:子任务yamlstring 键值对
  31. participantRpc participantservice.ParticipantService
  32. }
  33. func NewScheduler(scheduleService scheduleService, val string, dbEngin *gorm.DB, participantRpc participantservice.ParticipantService) (*scheduler, error) {
  34. var task *response.TaskInfo
  35. err := json.Unmarshal([]byte(val), &task)
  36. if err != nil {
  37. return nil, errors.New("create scheduler failed : " + err.Error())
  38. }
  39. return &scheduler{task: task, scheduleService: scheduleService, dbEngin: dbEngin, participantRpc: participantRpc}, nil
  40. }
  41. func (s *scheduler) SpecifyClusters() {
  42. // 如果已指定集群名,通过数据库查询后返回p端ip列表
  43. if len(s.task.Clusters) != 0 {
  44. s.dbEngin.Raw("select id from sc_participant_phy_info where `name` in (?)", s.task.Clusters).Scan(&s.participantIds)
  45. return
  46. }
  47. }
  48. func (s *scheduler) SpecifyNsID() {
  49. // 未指定集群名,只指定nsID
  50. if len(s.task.Clusters) == 0 {
  51. if len(s.task.NsID) != 0 {
  52. var clusters string
  53. s.dbEngin.Raw("select clusters from sc_tenant_info where `tenant_name` = ?", s.task.NsID).Scan(&clusters)
  54. clusterArr := strings.Split(clusters, ",")
  55. s.dbEngin.Raw("select id from sc_participant_phy_info where `name` in (?)", clusterArr).Scan(&s.participantIds)
  56. }
  57. } else {
  58. return
  59. }
  60. }
  61. func (s *scheduler) MatchLabels() {
  62. var ids []int64
  63. count := 0
  64. // 集群和nsID都未指定,则通过标签匹配
  65. if len(s.task.Clusters) == 0 && len(s.task.NsID) == 0 {
  66. //如果集群列表或nsID均未指定
  67. for key := range s.task.MatchLabels {
  68. var participantIds []int64
  69. s.dbEngin.Raw("select participant_id from sc_participant_label_info where `key` = ? and value = ?", key, s.task.MatchLabels[key]).Scan(&participantIds)
  70. if count == 0 {
  71. ids = participantIds
  72. }
  73. ids = intersect(ids, participantIds)
  74. count++
  75. }
  76. s.participantIds = ids
  77. } else {
  78. return
  79. }
  80. }
  81. // TempAssign todo 屏蔽原调度算法
  82. func (s *scheduler) TempAssign() error {
  83. //需要判断task中的资源类型,针对metadata中的多个kind做不同处理
  84. //输入副本数和集群列表,最终结果输出为pID对应副本数量列表,针对多个kind需要做拆分和重新拼接组合
  85. var resources []interface{}
  86. tool.Convert(s.task.Metadata, &resources)
  87. for index := range resources {
  88. //如果是Deployment,需要对副本数做分发
  89. if resources[index].(map[string]interface{})["kind"].(string) == "Deployment" || resources[index].(map[string]interface{})["kind"].(string) == "Replicaset" ||
  90. resources[index].(map[string]interface{})["kind"].(string) == "StatefulSet" {
  91. resources[index].(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 _, participantId := range s.participantIds {
  139. var resources []interface{}
  140. tool.Convert(s.task.Metadata, &resources)
  141. for _, resource := range resources {
  142. structForDb, err := s.scheduleService.getNewStructForDb(s.task, resource, participantId)
  143. if err != nil {
  144. return err
  145. }
  146. tx := s.dbEngin.Create(structForDb)
  147. if tx.Error != nil {
  148. logx.Error(tx.Error)
  149. return tx.Error
  150. }
  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.