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.

aiScheduler.go 6.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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 schedulers
  13. import (
  14. "context"
  15. "encoding/json"
  16. "errors"
  17. "fmt"
  18. "gitlink.org.cn/JointCloud/pcm-ac/hpcAC"
  19. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler"
  20. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/schedulers/option"
  21. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/service/collector"
  22. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/strategy"
  23. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/strategy/param"
  24. "gitlink.org.cn/JointCloud/pcm-coordinator/api/pkg/response"
  25. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  26. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  27. "gitlink.org.cn/JointCloud/pcm-octopus/octopus"
  28. "sync"
  29. )
  30. type AiScheduler struct {
  31. yamlString string
  32. task *response.TaskInfo
  33. *scheduler.Scheduler
  34. option *option.AiOption
  35. ctx context.Context
  36. }
  37. type AiResult struct {
  38. TaskId string
  39. ClusterId string
  40. Replica int32
  41. Msg string
  42. }
  43. func NewAiScheduler(ctx context.Context, val string, scheduler *scheduler.Scheduler, option *option.AiOption) (*AiScheduler, error) {
  44. return &AiScheduler{ctx: ctx, yamlString: val, Scheduler: scheduler, option: option}, nil
  45. }
  46. func (as *AiScheduler) GetNewStructForDb(task *response.TaskInfo, resource string, participantId int64) (interface{}, error) {
  47. ai := models.Ai{
  48. ParticipantId: participantId,
  49. TaskId: task.TaskId,
  50. Status: "Saved",
  51. YamlString: as.yamlString,
  52. }
  53. utils.Convert(task.Metadata, &ai)
  54. return ai, nil
  55. }
  56. func (as *AiScheduler) PickOptimalStrategy() (strategy.Strategy, error) {
  57. if as.option.AiClusterId != "" {
  58. // TODO database operation Find
  59. return &strategy.SingleAssignment{Cluster: &strategy.AssignedCluster{ClusterId: "", Replicas: 1}}, nil
  60. }
  61. resources, err := as.findClustersWithResources()
  62. if err != nil {
  63. return nil, err
  64. }
  65. if len(resources) == 0 {
  66. return nil, errors.New("no cluster has resources")
  67. }
  68. if len(resources) == 1 {
  69. var cluster strategy.AssignedCluster
  70. cluster.ClusterId = resources[0].ClusterId
  71. cluster.Replicas = 1
  72. return &strategy.SingleAssignment{Cluster: &cluster}, nil
  73. }
  74. params := &param.Params{Resources: resources}
  75. switch as.option.StrategyName {
  76. case strategy.REPLICATION:
  77. var clusterIds []string
  78. for _, resource := range resources {
  79. clusterIds = append(clusterIds, resource.ClusterId)
  80. }
  81. strategy := strategy.NewReplicationStrategy(clusterIds, 1)
  82. return strategy, nil
  83. case strategy.RESOURCES_PRICING:
  84. strategy := strategy.NewPricingStrategy(&param.ResourcePricingParams{Params: params, Replicas: 1})
  85. return strategy, nil
  86. case strategy.DYNAMIC_RESOURCES:
  87. strategy := strategy.NewDynamicResourcesStrategy(params.Resources, as.option, 1)
  88. return strategy, nil
  89. case strategy.STATIC_WEIGHT:
  90. //todo resources should match cluster StaticWeightMap
  91. strategy := strategy.NewStaticWeightStrategy(as.option.ClusterToStaticWeight, 1)
  92. return strategy, nil
  93. }
  94. return nil, errors.New("no strategy has been chosen")
  95. }
  96. func (as *AiScheduler) AssignTask(clusters []*strategy.AssignedCluster) (interface{}, error) {
  97. if clusters == nil {
  98. return nil, errors.New("clusters is nil")
  99. }
  100. for i := len(clusters) - 1; i >= 0; i-- {
  101. if clusters[i].Replicas == 0 {
  102. clusters = append(clusters[:i], clusters[i+1:]...)
  103. }
  104. }
  105. if len(clusters) == 0 {
  106. return nil, errors.New("clusters is nil")
  107. }
  108. var wg sync.WaitGroup
  109. var results []*AiResult
  110. var errs []interface{}
  111. var ch = make(chan *AiResult, len(clusters))
  112. var errCh = make(chan interface{}, len(clusters))
  113. executorMap := *as.AiExecutor
  114. for _, cluster := range clusters {
  115. c := cluster
  116. wg.Add(1)
  117. go func() {
  118. opt, _ := cloneAiOption(as.option)
  119. resp, err := executorMap[c.ClusterId].Execute(as.ctx, opt)
  120. if err != nil {
  121. e := struct {
  122. err error
  123. clusterId string
  124. }{
  125. err: err,
  126. clusterId: c.ClusterId,
  127. }
  128. errCh <- e
  129. wg.Done()
  130. return
  131. }
  132. result, _ := convertType(resp)
  133. result.Replica = c.Replicas
  134. result.ClusterId = c.ClusterId
  135. ch <- result
  136. wg.Done()
  137. }()
  138. }
  139. wg.Wait()
  140. close(ch)
  141. close(errCh)
  142. for e := range errCh {
  143. errs = append(errs, e)
  144. }
  145. if len(errs) != len(clusters) {
  146. return nil, errors.New("submit task failed")
  147. }
  148. if len(errs) != 0 {
  149. var msg string
  150. for _, err := range errs {
  151. e := (err).(struct {
  152. err error
  153. clusterId string
  154. })
  155. msg += fmt.Sprintf("clusterId: %v , error: %v \n", e.clusterId, e.err.Error())
  156. }
  157. return nil, errors.New(msg)
  158. }
  159. for s := range ch {
  160. // TODO: database operation
  161. results = append(results, s)
  162. }
  163. return results, nil
  164. }
  165. func (as *AiScheduler) findClustersWithResources() ([]*collector.ResourceStats, error) {
  166. var wg sync.WaitGroup
  167. var ch = make(chan *collector.ResourceStats, len(*as.ResourceCollector))
  168. var errCh = make(chan error, len(*as.ResourceCollector))
  169. var resourceSpecs []*collector.ResourceStats
  170. var errs []error
  171. for _, resourceCollector := range *as.ResourceCollector {
  172. wg.Add(1)
  173. rc := resourceCollector
  174. go func() {
  175. spec, err := rc.GetResourceStats(as.ctx)
  176. if err != nil {
  177. errCh <- err
  178. wg.Done()
  179. return
  180. }
  181. ch <- spec
  182. wg.Done()
  183. }()
  184. }
  185. wg.Wait()
  186. close(ch)
  187. close(errCh)
  188. for s := range ch {
  189. resourceSpecs = append(resourceSpecs, s)
  190. }
  191. for e := range errCh {
  192. errs = append(errs, e)
  193. }
  194. if len(errs) != 0 {
  195. return nil, errors.New("get resources failed")
  196. }
  197. if len(resourceSpecs) == 0 {
  198. return nil, errors.New("no resource found")
  199. }
  200. return resourceSpecs, nil
  201. }
  202. func convertType(in interface{}) (*AiResult, error) {
  203. var result AiResult
  204. switch (in).(type) {
  205. case *hpcAC.SubmitTaskAiResp:
  206. resp := (in).(*hpcAC.SubmitTaskAiResp)
  207. if resp.Code == "0" {
  208. result.TaskId = resp.Data
  209. } else {
  210. result.Msg = resp.Msg
  211. }
  212. return &result, nil
  213. case *octopus.CreateTrainJobResp:
  214. resp := (in).(*octopus.CreateTrainJobResp)
  215. if resp.Success {
  216. result.TaskId = resp.Payload.JobId
  217. } else {
  218. result.Msg = resp.Error.Message
  219. }
  220. return &result, nil
  221. default:
  222. return nil, errors.New("ai task response failed")
  223. }
  224. }
  225. func cloneAiOption(opt *option.AiOption) (*option.AiOption, error) {
  226. origJSON, err := json.Marshal(opt)
  227. if err != nil {
  228. return nil, err
  229. }
  230. clone := option.AiOption{}
  231. if err = json.Unmarshal(origJSON, &clone); err != nil {
  232. return nil, err
  233. }
  234. return &clone, nil
  235. }

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.