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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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/rpc/client/participantservice"
  19. "gorm.io/gorm"
  20. "sigs.k8s.io/yaml"
  21. "strings"
  22. )
  23. type scheduler struct {
  24. task *response.TaskInfo
  25. participantIds []int64
  26. scheduleService scheduleService
  27. dbEngin *gorm.DB
  28. result []string //pID:子任务yamlstring 键值对
  29. participantRpc participantservice.ParticipantService
  30. }
  31. func NewScheduler(scheduleService scheduleService, val string, dbEngin *gorm.DB, participantRpc participantservice.ParticipantService) (*scheduler, error) {
  32. var task *response.TaskInfo
  33. err := json.Unmarshal([]byte(val), &task)
  34. if err != nil {
  35. return nil, errors.New("create scheduler failed : " + err.Error())
  36. }
  37. return &scheduler{task: task, scheduleService: scheduleService, dbEngin: dbEngin, participantRpc: participantRpc}, nil
  38. }
  39. func (s *scheduler) SpecifyClusters() {
  40. // 如果已指定集群名,通过数据库查询后返回p端ip列表
  41. if len(s.task.Clusters) != 0 {
  42. s.dbEngin.Raw("select id from sc_participant_phy_info where `name` in (?)", s.task.Clusters).Scan(&s.participantIds)
  43. return
  44. }
  45. }
  46. func (s *scheduler) SpecifyNsID() {
  47. // 未指定集群名,只指定nsID
  48. if len(s.task.Clusters) == 0 {
  49. if len(s.task.NsID) != 0 {
  50. var clusters string
  51. s.dbEngin.Raw("select clusters from sc_tenant_info where `tenant_name` = ?", s.task.NsID).Scan(&clusters)
  52. clusterArr := strings.Split(clusters, ",")
  53. s.dbEngin.Raw("select id from sc_participant_phy_info where `name` in (?)", clusterArr).Scan(&s.participantIds)
  54. }
  55. } else {
  56. return
  57. }
  58. }
  59. func (s *scheduler) MatchLabels() {
  60. var ids []int64
  61. count := 0
  62. // 集群和nsID都未指定,则通过标签匹配
  63. if len(s.task.Clusters) == 0 && len(s.task.NsID) == 0 {
  64. //如果集群列表或nsID均未指定
  65. for key := range s.task.MatchLabels {
  66. var participantIds []int64
  67. s.dbEngin.Raw("select participant_id from sc_participant_label_info where `key` = ? and value = ?", key, s.task.MatchLabels[key]).Scan(&participantIds)
  68. if count == 0 {
  69. ids = participantIds
  70. }
  71. ids = intersect(ids, participantIds)
  72. count++
  73. }
  74. s.participantIds = ids
  75. } else {
  76. return
  77. }
  78. }
  79. // TempAssign todo 屏蔽原调度算法
  80. func (s *scheduler) TempAssign() error {
  81. //需要判断task中的资源类型,针对metadata中的多个kind做不同处理
  82. //输入副本数和集群列表,最终结果输出为pID对应副本数量列表,针对多个kind需要做拆分和重新拼接组合
  83. var meData []string
  84. for _, yamlString := range s.task.Metadata {
  85. var data map[string]interface{}
  86. err := yaml.Unmarshal([]byte(yamlString), &data)
  87. if err != nil {
  88. }
  89. jsonData, err := json.Marshal(data)
  90. if err != nil {
  91. }
  92. meData = append(meData, string(jsonData))
  93. }
  94. s.task.Metadata = meData
  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. strategy, err := s.scheduleService.pickOptimalStrategy()
  116. if err != nil {
  117. return err
  118. }
  119. _, err = strategy.Schedule()
  120. if err != nil {
  121. return err
  122. }
  123. //集群数量不满足,指定到标签匹配后第一个集群
  124. //if len(providerList) < 2 {
  125. // s.task.ParticipantId = s.participantIds[0]
  126. // return nil
  127. //}
  128. return nil
  129. }
  130. func (s *scheduler) SaveToDb() error {
  131. for _, participantId := range s.participantIds {
  132. for _, resource := range s.task.Metadata {
  133. structForDb, err := s.scheduleService.getNewStructForDb(s.task, resource, participantId)
  134. if err != nil {
  135. return err
  136. }
  137. tx := s.dbEngin.Create(structForDb)
  138. if tx.Error != nil {
  139. logx.Error(tx.Error)
  140. return tx.Error
  141. }
  142. }
  143. }
  144. return nil
  145. }

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.