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.

task_impl.go 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. package client
  2. import (
  3. "github.com/zeromicro/go-zero/core/logx"
  4. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
  5. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils"
  7. "gorm.io/gorm"
  8. "log"
  9. "strings"
  10. "sync"
  11. )
  12. type task struct {
  13. sync.RWMutex
  14. client *client
  15. options *TaskOptions
  16. log log.Logger
  17. }
  18. func newTask(client *client, options *TaskOptions) (*task, error) {
  19. task := &task{
  20. RWMutex: sync.RWMutex{},
  21. client: client,
  22. options: options,
  23. log: log.Logger{},
  24. }
  25. return task, nil
  26. }
  27. func (t task) PullTaskInfo(pullTaskInfoReq PullTaskInfoReq) (*PullTaskInfoResp, error) {
  28. result := PullTaskInfoResp{}
  29. // 查询p端类型
  30. var kind int32
  31. t.client.DbEngin.Raw("select type as kind from `t_adapter` where id = ?", pullTaskInfoReq.AdapterId).Scan(&kind)
  32. // 查询云智超中的数据列表
  33. switch kind {
  34. case 2:
  35. var hpcModelList []models.TaskHpc
  36. findModelList(pullTaskInfoReq.AdapterId, t.client.DbEngin, &hpcModelList)
  37. utils.Convert(hpcModelList, &result.HpcInfoList)
  38. case 0:
  39. var cloudModelList []models.Cloud
  40. findModelList(pullTaskInfoReq.AdapterId, t.client.DbEngin, &cloudModelList)
  41. utils.Convert(cloudModelList, &result.CloudInfoList)
  42. case 1:
  43. var aiModelList []models.Ai
  44. findModelList(pullTaskInfoReq.AdapterId, t.client.DbEngin, &aiModelList)
  45. utils.Convert(aiModelList, &result.AiInfoList)
  46. }
  47. return &result, nil
  48. }
  49. func (t task) PushTaskInfo(pushTaskInfoReq PushTaskInfoReq) (*PushTaskInfoResp, error) {
  50. // 查询p端类型
  51. var kind int32
  52. t.client.DbEngin.Raw("select type as kind from t_adapter where id = ?", pushTaskInfoReq.AdapterId).Scan(&kind)
  53. switch kind {
  54. case 0:
  55. for _, cloudInfo := range pushTaskInfoReq.CloudInfoList {
  56. t.client.DbEngin.Exec("update cloud set status = ?,start_time = ?,result = ? where participant_id = ? and id = ?",
  57. cloudInfo.Status, cloudInfo.StartTime, cloudInfo.Result, pushTaskInfoReq.AdapterId, cloudInfo.Id)
  58. syncTask(t.client.DbEngin, cloudInfo.TaskId)
  59. }
  60. case 2:
  61. for _, hpcInfo := range pushTaskInfoReq.HpcInfoList {
  62. t.client.DbEngin.Exec("update task_hpc set status = ?,start_time = ?,job_id = ? where cluster_id = ? and task_id = ? and name = ?",
  63. hpcInfo.Status, hpcInfo.StartTime, hpcInfo.RunningTime, hpcInfo.JobId, pushTaskInfoReq.AdapterId, hpcInfo.TaskId, hpcInfo.Name)
  64. syncTask(t.client.DbEngin, hpcInfo.TaskId)
  65. }
  66. case 1:
  67. for _, aiInfo := range pushTaskInfoReq.AiInfoList {
  68. t.client.DbEngin.Exec("update ai set status = ?,start_time = ?,project_id = ?,job_id = ? where participant_id = ? and task_id = ? and name = ?",
  69. aiInfo.Status, aiInfo.StartTime, aiInfo.ProjectId, aiInfo.JobId, pushTaskInfoReq.AdapterId, aiInfo.TaskId, aiInfo.Name)
  70. syncTask(t.client.DbEngin, aiInfo.TaskId)
  71. }
  72. }
  73. return &PushTaskInfoResp{}, nil
  74. }
  75. func (t task) PushResourceInfo(pushResourceInfoReq PushResourceInfoReq) {
  76. //TODO implement me
  77. panic("implement me")
  78. }
  79. func findModelList(participantId int64, dbEngin *gorm.DB, data interface{}) error {
  80. tx := dbEngin.Where("cluster_id = (select id from t_cluster where adapter_id = ?) AND status NOT IN ?", participantId, []string{"Deleted", "Succeeded", "Completed", "Failed"}).Find(data)
  81. if tx.Error != nil {
  82. return tx.Error
  83. }
  84. return nil
  85. }
  86. func syncTask(gorm *gorm.DB, taskId int64) {
  87. var allStatus string
  88. tx := gorm.Raw("SELECT CONCAT_WS(',',GROUP_CONCAT(DISTINCT h.status) ,GROUP_CONCAT(DISTINCT a.status) ,GROUP_CONCAT(DISTINCT c.status))as status from task t left join hpc h on t.id = h.task_id left join cloud c on t.id = c.task_id left join ai a on t.id = a.task_id where t.id = ?", taskId).Scan(&allStatus)
  89. if tx.Error != nil {
  90. logx.Error(tx.Error)
  91. }
  92. // 子状态统一则修改主任务状态
  93. statusArray := strings.Split(allStatus, ",")
  94. if len(removeRepeatedElement(statusArray)) == 1 {
  95. updateTask(gorm, taskId, statusArray[0])
  96. }
  97. // 子任务包含失败状态 主任务则失败
  98. if strings.Contains(allStatus, constants.Failed) {
  99. updateTask(gorm, taskId, constants.Failed)
  100. }
  101. if strings.Contains(allStatus, constants.Running) {
  102. updateTask(gorm, taskId, constants.Running)
  103. }
  104. }
  105. func updateTask(gorm *gorm.DB, taskId int64, status string) {
  106. var task models.Task
  107. gorm.Where("id = ? ", taskId).Find(&task)
  108. if task.Status != status {
  109. task.Status = status
  110. gorm.Updates(&task)
  111. }
  112. }
  113. func removeRepeatedElement(arr []string) (newArr []string) {
  114. newArr = make([]string, 0)
  115. for i := 0; i < len(arr); i++ {
  116. repeat := false
  117. for j := i + 1; j < len(arr); j++ {
  118. if arr[i] == arr[j] {
  119. repeat = true
  120. break
  121. }
  122. }
  123. if !repeat {
  124. newArr = append(newArr, arr[i])
  125. }
  126. }
  127. return
  128. }

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.