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.

cloudScheduler.go 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. "bytes"
  15. "encoding/json"
  16. "errors"
  17. "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/pkg/response"
  18. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models"
  19. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/scheduler/algo"
  20. "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
  21. "gorm.io/gorm"
  22. "io"
  23. "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
  24. "k8s.io/apimachinery/pkg/runtime"
  25. syaml "k8s.io/apimachinery/pkg/runtime/serializer/yaml"
  26. kyaml "k8s.io/apimachinery/pkg/util/yaml"
  27. )
  28. type cloudScheduler struct {
  29. }
  30. func NewCloudScheduler() *cloudScheduler {
  31. return &cloudScheduler{}
  32. }
  33. func (cs *cloudScheduler) pickOptimalStrategy(task *algo.Task, providers ...*algo.Provider) (*algo.Strategy, error) {
  34. //参数为空返回 nil
  35. if len(providers) == 0 || task == nil {
  36. return nil, errors.New("算法获取参数为空")
  37. }
  38. //调度算法
  39. strategy := algo.NewK8sStrategy(task, providers...)
  40. taskResult, err := algo.ScheduleWithFullCollaboration(strategy, strategy.ProviderList)
  41. if err != nil {
  42. return nil, err
  43. }
  44. return taskResult.MaxscoreStrategy, nil
  45. }
  46. func (cs *cloudScheduler) getNewStructForDb(task *response.TaskInfo, participantId int64) (interface{}, error) {
  47. bytes, err := json.Marshal(task.Metadata)
  48. if err != nil {
  49. return nil, err
  50. }
  51. cloud := cs.UnMarshalK8sStruct(string(bytes), task.TaskId)
  52. cloud.Id = utils.GenSnowflakeID()
  53. cloud.YamlString = string(bytes)
  54. cloud.ParticipantId = participantId
  55. return cloud, nil
  56. }
  57. func (cs *cloudScheduler) UnMarshalK8sStruct(yamlString string, taskId int64) models.Cloud {
  58. var cloud models.Cloud
  59. d := kyaml.NewYAMLOrJSONDecoder(bytes.NewBufferString(yamlString), 4096)
  60. var err error
  61. for {
  62. var rawObj runtime.RawExtension
  63. err = d.Decode(&rawObj)
  64. if err == io.EOF {
  65. break
  66. }
  67. if err != nil {
  68. }
  69. obj := &unstructured.Unstructured{}
  70. syaml.NewDecodingSerializer(unstructured.UnstructuredJSONScheme).Decode(rawObj.Raw, nil, obj)
  71. if err != nil {
  72. }
  73. unstructuredMap, err := runtime.DefaultUnstructuredConverter.ToUnstructured(obj)
  74. if err != nil {
  75. }
  76. unstructureObj := &unstructured.Unstructured{Object: unstructuredMap}
  77. cloud = models.Cloud{
  78. TaskId: taskId,
  79. ApiVersion: unstructureObj.GetAPIVersion(),
  80. Name: unstructureObj.GetName(),
  81. Kind: unstructureObj.GetKind(),
  82. Namespace: unstructureObj.GetNamespace(),
  83. Status: "Saved",
  84. }
  85. // 命名空间为空 设置默认值
  86. if len(unstructureObj.GetNamespace()) == 0 {
  87. cloud.Namespace = "default"
  88. }
  89. }
  90. return cloud
  91. }
  92. func (cs *cloudScheduler) genTaskAndProviders(task *response.TaskInfo, dbEngin *gorm.DB) (*algo.Task, []*algo.Provider) {
  93. var proParams []providerParams
  94. sqlstr := "SELECT SUM(a.disk_avail) as disk_avail,SUM(a.mem_avail) as mem_avail,SUM(a.cpu_total * a.cpu_usable) as cpu_avail,participant_id from (SELECT * from sc_node_avail_info where created_time in (SELECT MAX(created_time) as time from sc_node_avail_info where deleted_flag = 0 GROUP BY participant_id,node_name)) a GROUP BY a.participant_id"
  95. dbEngin.Raw(sqlstr).Scan(&proParams)
  96. var providerList []*algo.Provider
  97. for _, p := range proParams {
  98. provider := algo.NewProvider(p.Participant_id, p.Cpu_avail, p.Mem_avail, p.Disk_avail, 0.0, 0.0, 0.0)
  99. providerList = append(providerList, provider)
  100. }
  101. t := algo.NewTask(0, 1, 2, 75120000, 301214500, 1200, 2, 6, 2000)
  102. return t, providerList
  103. }

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.