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 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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/JointCloud/pcm-coordinator/api/internal/scheduler/common"
  18. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/database"
  19. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/service"
  20. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/strategy"
  21. "gitlink.org.cn/JointCloud/pcm-coordinator/api/pkg/response"
  22. "gitlink.org.cn/JointCloud/pcm-coordinator/rpc/client/participantservice"
  23. "gorm.io/gorm"
  24. "sigs.k8s.io/yaml"
  25. "strings"
  26. "sync"
  27. )
  28. type Scheduler struct {
  29. task *response.TaskInfo
  30. participantIds []int64
  31. subSchedule SubSchedule
  32. dbEngin *gorm.DB
  33. result []string //pID:子任务yamlstring 键值对
  34. participantRpc participantservice.ParticipantService
  35. AiStorages *database.AiStorage
  36. AiService *service.AiService
  37. mu sync.RWMutex
  38. }
  39. type SubSchedule interface {
  40. GetNewStructForDb(task *response.TaskInfo, resource string, participantId int64) (interface{}, error)
  41. PickOptimalStrategy() (strategy.Strategy, error)
  42. AssignTask(clusters []*strategy.AssignedCluster) (interface{}, error)
  43. }
  44. func NewScheduler(subSchedule SubSchedule, val string, dbEngin *gorm.DB, participantRpc participantservice.ParticipantService) (*Scheduler, error) {
  45. var task *response.TaskInfo
  46. err := json.Unmarshal([]byte(val), &task)
  47. if err != nil {
  48. return nil, errors.New("create scheduler failed : " + err.Error())
  49. }
  50. return &Scheduler{task: task, subSchedule: subSchedule, dbEngin: dbEngin, participantRpc: participantRpc}, nil
  51. }
  52. func NewSchdlr(aiService *service.AiService, storages *database.AiStorage) *Scheduler {
  53. return &Scheduler{AiService: aiService, AiStorages: storages}
  54. }
  55. func (s *Scheduler) SpecifyClusters() {
  56. // 如果已指定集群名,通过数据库查询后返回p端ip列表
  57. if len(s.task.Clusters) != 0 {
  58. s.dbEngin.Raw("select id from sc_participant_phy_info where `name` in (?)", s.task.Clusters).Scan(&s.participantIds)
  59. return
  60. }
  61. }
  62. func (s *Scheduler) SpecifyNsID() {
  63. // 未指定集群名,只指定nsID
  64. if len(s.task.Clusters) == 0 {
  65. if len(s.task.NsID) != 0 {
  66. var clusters string
  67. s.dbEngin.Raw("select clusters from sc_tenant_info where `tenant_name` = ?", s.task.NsID).Scan(&clusters)
  68. clusterArr := strings.Split(clusters, ",")
  69. s.dbEngin.Raw("select id from sc_participant_phy_info where `name` in (?)", clusterArr).Scan(&s.participantIds)
  70. }
  71. } else {
  72. return
  73. }
  74. }
  75. func (s *Scheduler) MatchLabels() {
  76. var ids []int64
  77. count := 0
  78. // 集群和nsID都未指定,则通过标签匹配
  79. if len(s.task.Clusters) == 0 && len(s.task.NsID) == 0 {
  80. //如果集群列表或nsID均未指定
  81. for key := range s.task.MatchLabels {
  82. var participantIds []int64
  83. s.dbEngin.Raw("select participant_id from sc_participant_label_info where `key` = ? and value = ?", key, s.task.MatchLabels[key]).Scan(&participantIds)
  84. if count == 0 {
  85. ids = participantIds
  86. }
  87. ids = common.Intersect(ids, participantIds)
  88. count++
  89. }
  90. s.participantIds = ids
  91. } else {
  92. return
  93. }
  94. }
  95. // TempAssign todo 屏蔽原调度算法
  96. func (s *Scheduler) TempAssign() error {
  97. //需要判断task中的资源类型,针对metadata中的多个kind做不同处理
  98. //输入副本数和集群列表,最终结果输出为pID对应副本数量列表,针对多个kind需要做拆分和重新拼接组合
  99. var meData []string
  100. for _, yamlString := range s.task.Metadata {
  101. var data map[string]interface{}
  102. err := yaml.Unmarshal([]byte(yamlString), &data)
  103. if err != nil {
  104. }
  105. jsonData, err := json.Marshal(data)
  106. if err != nil {
  107. }
  108. meData = append(meData, string(jsonData))
  109. }
  110. s.task.Metadata = meData
  111. return nil
  112. }
  113. func (s *Scheduler) AssignAndSchedule(ss SubSchedule) (interface{}, error) {
  114. //// 已指定 ParticipantId
  115. //if s.task.ParticipantId != 0 {
  116. // return nil
  117. //}
  118. //// 标签匹配以及后,未找到ParticipantIds
  119. //if len(s.participantIds) == 0 {
  120. // return errors.New("未找到匹配的ParticipantIds")
  121. //}
  122. //
  123. //// 指定或者标签匹配的结果只有一个集群,给任务信息指定
  124. //if len(s.participantIds) == 1 {
  125. // s.task.ParticipantId = s.participantIds[0]
  126. // //replicas := s.task.Metadata.(map[string]interface{})["spec"].(map[string]interface{})["replicas"].(float64)
  127. // //result := make(map[int64]string)
  128. // //result[s.participantIds[0]] = strconv.FormatFloat(replicas, 'f', 2, 64)
  129. // //s.result = result
  130. //
  131. // return nil
  132. //}
  133. strategy, err := ss.PickOptimalStrategy()
  134. if err != nil {
  135. return nil, err
  136. }
  137. clusters, err := strategy.Schedule()
  138. if err != nil {
  139. return nil, err
  140. }
  141. //集群数量不满足,指定到标签匹配后第一个集群
  142. //if len(providerList) < 2 {
  143. // s.task.ParticipantId = s.participantIds[0]
  144. // return nil
  145. //}
  146. resp, err := ss.AssignTask(clusters)
  147. if err != nil {
  148. return nil, err
  149. }
  150. return resp, nil
  151. }
  152. func (s *Scheduler) SaveToDb() error {
  153. for _, participantId := range s.participantIds {
  154. for _, resource := range s.task.Metadata {
  155. structForDb, err := s.subSchedule.GetNewStructForDb(s.task, resource, participantId)
  156. if err != nil {
  157. return err
  158. }
  159. tx := s.dbEngin.Create(structForDb)
  160. if tx.Error != nil {
  161. logx.Error(tx.Error)
  162. return tx.Error
  163. }
  164. }
  165. }
  166. return nil
  167. }

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.