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.

aiStorage.go 10 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334
  1. package database
  2. import (
  3. "github.com/zeromicro/go-zero/core/logx"
  4. clientCore "gitlink.org.cn/JointCloud/pcm-coordinator/api/client"
  5. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/schedulers/option"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/types"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/tracker"
  10. "gorm.io/gorm"
  11. "strconv"
  12. "time"
  13. )
  14. type AiStorage struct {
  15. DbEngin *gorm.DB
  16. }
  17. func (s *AiStorage) GetParticipants() (*types.ClusterListResp, error) {
  18. var resp types.ClusterListResp
  19. tx := s.DbEngin.Raw("select * from t_cluster where `deleted_at` IS NULL ORDER BY create_time Desc").Scan(&resp.List)
  20. if tx.Error != nil {
  21. logx.Errorf(tx.Error.Error())
  22. return nil, tx.Error
  23. }
  24. return &resp, nil
  25. }
  26. func (s *AiStorage) GetClustersByAdapterId(id string) (*types.ClusterListResp, error) {
  27. var resp types.ClusterListResp
  28. tx := s.DbEngin.Raw("select * from t_cluster where `deleted_at` IS NULL and `adapter_id` = ? ORDER BY create_time Desc", id).Scan(&resp.List)
  29. if tx.Error != nil {
  30. logx.Errorf(tx.Error.Error())
  31. return nil, tx.Error
  32. }
  33. return &resp, nil
  34. }
  35. func (s *AiStorage) GetClusterNameById(id string) (string, error) {
  36. var name string
  37. tx := s.DbEngin.Raw("select `description` from t_cluster where `id` = ?", id).Scan(&name)
  38. if tx.Error != nil {
  39. logx.Errorf(tx.Error.Error())
  40. return "", tx.Error
  41. }
  42. return name, nil
  43. }
  44. func (s *AiStorage) GetAdapterNameById(id string) (string, error) {
  45. var name string
  46. tx := s.DbEngin.Raw("select `name` from t_adapter where `id` = ?", id).Scan(&name)
  47. if tx.Error != nil {
  48. logx.Errorf(tx.Error.Error())
  49. return "", tx.Error
  50. }
  51. return name, nil
  52. }
  53. func (s *AiStorage) GetAdapterIdsByType(adapterType string) ([]string, error) {
  54. var list []types.AdapterInfo
  55. var ids []string
  56. db := s.DbEngin.Model(&types.AdapterInfo{}).Table("t_adapter")
  57. db = db.Where("type = ?", adapterType)
  58. err := db.Order("create_time desc").Find(&list).Error
  59. if err != nil {
  60. return nil, err
  61. }
  62. for _, info := range list {
  63. ids = append(ids, info.Id)
  64. }
  65. return ids, nil
  66. }
  67. func (s *AiStorage) GetAdaptersByType(adapterType string) ([]*types.AdapterInfo, error) {
  68. var list []*types.AdapterInfo
  69. db := s.DbEngin.Model(&types.AdapterInfo{}).Table("t_adapter")
  70. db = db.Where("type = ?", adapterType)
  71. err := db.Order("create_time desc").Find(&list).Error
  72. if err != nil {
  73. return nil, err
  74. }
  75. return list, nil
  76. }
  77. func (s *AiStorage) GetAiTasksByAdapterId(adapterId string) ([]*models.TaskAi, error) {
  78. var resp []*models.TaskAi
  79. db := s.DbEngin.Model(&models.TaskAi{}).Table("task_ai")
  80. db = db.Where("adapter_id = ?", adapterId)
  81. err := db.Order("commit_time desc").Find(&resp).Error
  82. if err != nil {
  83. return nil, err
  84. }
  85. return resp, nil
  86. }
  87. func (s *AiStorage) SaveTask(name string, strategyCode int64, synergyStatus int64, aiType string) (int64, error) {
  88. // 构建主任务结构体
  89. taskModel := models.Task{
  90. Status: constants.Saved,
  91. Description: "ai task",
  92. Name: name,
  93. SynergyStatus: synergyStatus,
  94. Strategy: strategyCode,
  95. AdapterTypeDict: "1",
  96. TaskTypeDict: aiType,
  97. CommitTime: time.Now(),
  98. }
  99. // 保存任务数据到数据库
  100. tx := s.DbEngin.Create(&taskModel)
  101. if tx.Error != nil {
  102. return 0, tx.Error
  103. }
  104. return taskModel.Id, nil
  105. }
  106. func (s *AiStorage) SaveAiTask(taskId int64, opt option.Option, adapterName string, clusterId string, clusterName string, jobId string, status string, msg string) error {
  107. var aiOpt *option.AiOption
  108. switch (opt).(type) {
  109. case *option.AiOption:
  110. aiOpt = (opt).(*option.AiOption)
  111. case *option.InferOption:
  112. inferOpt := (opt).(*option.InferOption)
  113. aiOpt = &option.AiOption{}
  114. aiOpt.TaskName = inferOpt.TaskName
  115. aiOpt.Replica = inferOpt.Replica
  116. aiOpt.AdapterId = inferOpt.AdapterId
  117. aiOpt.TaskType = inferOpt.ModelType
  118. aiOpt.StrategyName = inferOpt.Strategy
  119. }
  120. // 构建主任务结构体
  121. aId, err := strconv.ParseInt(aiOpt.AdapterId, 10, 64)
  122. if err != nil {
  123. return err
  124. }
  125. cId, err := strconv.ParseInt(clusterId, 10, 64)
  126. if err != nil {
  127. return err
  128. }
  129. aiTaskModel := models.TaskAi{
  130. TaskId: taskId,
  131. AdapterId: aId,
  132. AdapterName: adapterName,
  133. ClusterId: cId,
  134. ClusterName: clusterName,
  135. Name: aiOpt.TaskName,
  136. Replica: int64(aiOpt.Replica),
  137. JobId: jobId,
  138. TaskType: aiOpt.TaskType,
  139. Strategy: aiOpt.StrategyName,
  140. Status: status,
  141. Msg: msg,
  142. Card: aiOpt.ComputeCard,
  143. CommitTime: time.Now(),
  144. }
  145. // 保存任务数据到数据库
  146. tx := s.DbEngin.Create(&aiTaskModel)
  147. if tx.Error != nil {
  148. return tx.Error
  149. }
  150. return nil
  151. }
  152. func (s *AiStorage) SaveClusterTaskQueue(adapterId string, clusterId string, queueNum int64) error {
  153. aId, err := strconv.ParseInt(adapterId, 10, 64)
  154. if err != nil {
  155. return err
  156. }
  157. cId, err := strconv.ParseInt(clusterId, 10, 64)
  158. if err != nil {
  159. return err
  160. }
  161. taskQueue := models.TClusterTaskQueue{
  162. AdapterId: aId,
  163. ClusterId: cId,
  164. QueueNum: queueNum,
  165. }
  166. tx := s.DbEngin.Create(&taskQueue)
  167. if tx.Error != nil {
  168. return tx.Error
  169. }
  170. return nil
  171. }
  172. func (s *AiStorage) GetClusterTaskQueues(adapterId string, clusterId string) ([]*models.TClusterTaskQueue, error) {
  173. var taskQueues []*models.TClusterTaskQueue
  174. tx := s.DbEngin.Raw("select * from t_cluster_task_queue where `adapter_id` = ? and `cluster_id` = ?", adapterId, clusterId).Scan(&taskQueues)
  175. if tx.Error != nil {
  176. logx.Errorf(tx.Error.Error())
  177. return nil, tx.Error
  178. }
  179. return taskQueues, nil
  180. }
  181. func (s *AiStorage) GetAiTaskIdByClusterIdAndTaskId(clusterId string, taskId string) (string, error) {
  182. var aiTask models.TaskAi
  183. tx := s.DbEngin.Raw("select * from task_ai where `cluster_id` = ? and `task_id` = ?", clusterId, taskId).Scan(&aiTask)
  184. if tx.Error != nil {
  185. logx.Errorf(tx.Error.Error())
  186. return "", tx.Error
  187. }
  188. return aiTask.JobId, nil
  189. }
  190. func (s *AiStorage) GetClusterResourcesById(clusterId string) (*models.TClusterResource, error) {
  191. var clusterResource models.TClusterResource
  192. tx := s.DbEngin.Raw("select * from t_cluster_resource where `cluster_id` = ?", clusterId).Scan(&clusterResource)
  193. if tx.Error != nil {
  194. logx.Errorf(tx.Error.Error())
  195. return nil, tx.Error
  196. }
  197. return &clusterResource, nil
  198. }
  199. func (s *AiStorage) SaveClusterResources(adapterId string, clusterId string, clusterName string, clusterType int64, cpuAvail float64, cpuTotal float64,
  200. memAvail float64, memTotal float64, diskAvail float64, diskTotal float64, gpuAvail float64, gpuTotal float64, cardTotal int64, topsTotal float64) error {
  201. cId, err := strconv.ParseInt(clusterId, 10, 64)
  202. if err != nil {
  203. return err
  204. }
  205. aId, err := strconv.ParseInt(adapterId, 10, 64)
  206. if err != nil {
  207. return err
  208. }
  209. clusterResource := models.TClusterResource{
  210. AdapterId: aId,
  211. ClusterId: cId,
  212. ClusterName: clusterName,
  213. ClusterType: clusterType,
  214. CpuAvail: cpuAvail,
  215. CpuTotal: cpuTotal,
  216. MemAvail: memAvail,
  217. MemTotal: memTotal,
  218. DiskAvail: diskAvail,
  219. DiskTotal: diskTotal,
  220. GpuAvail: gpuAvail,
  221. GpuTotal: gpuTotal,
  222. CardTotal: cardTotal,
  223. CardTopsTotal: topsTotal,
  224. }
  225. tx := s.DbEngin.Create(&clusterResource)
  226. if tx.Error != nil {
  227. return tx.Error
  228. }
  229. // prometheus
  230. param := tracker.ClusterLoadRecord{
  231. AdapterId: aId,
  232. ClusterName: clusterName,
  233. CpuAvail: cpuAvail,
  234. CpuTotal: cpuTotal,
  235. CpuUtilisation: clusterResource.CpuAvail / clusterResource.CpuTotal,
  236. MemoryAvail: memAvail,
  237. MemoryTotal: memTotal,
  238. MemoryUtilisation: clusterResource.MemAvail / clusterResource.MemTotal,
  239. DiskAvail: diskAvail,
  240. DiskTotal: diskTotal,
  241. DiskUtilisation: clusterResource.DiskAvail / clusterResource.DiskTotal,
  242. }
  243. tracker.SyncClusterLoad(param)
  244. return nil
  245. }
  246. func (s *AiStorage) UpdateClusterResources(clusterResource *models.TClusterResource) error {
  247. tx := s.DbEngin.Where("cluster_id = ?", clusterResource.ClusterId).Updates(clusterResource)
  248. if tx.Error != nil {
  249. return tx.Error
  250. }
  251. // prometheus
  252. param := tracker.ClusterLoadRecord{
  253. AdapterId: clusterResource.AdapterId,
  254. ClusterName: clusterResource.ClusterName,
  255. CpuAvail: clusterResource.CpuAvail,
  256. CpuTotal: clusterResource.CpuTotal,
  257. CpuUtilisation: clusterResource.CpuAvail / clusterResource.CpuTotal,
  258. MemoryAvail: clusterResource.MemAvail,
  259. MemoryTotal: clusterResource.MemTotal,
  260. MemoryUtilisation: clusterResource.MemAvail / clusterResource.MemTotal,
  261. DiskAvail: clusterResource.DiskAvail,
  262. DiskTotal: clusterResource.DiskTotal,
  263. DiskUtilisation: clusterResource.DiskAvail / clusterResource.DiskTotal,
  264. }
  265. tracker.SyncClusterLoad(param)
  266. return nil
  267. }
  268. func (s *AiStorage) UpdateAiTask(task *models.TaskAi) error {
  269. tx := s.DbEngin.Updates(task)
  270. if tx.Error != nil {
  271. return tx.Error
  272. }
  273. return nil
  274. }
  275. func (s *AiStorage) GetStrategyCode(name string) (int64, error) {
  276. var strategy int64
  277. sqlStr := `select t_dict_item.item_value
  278. from t_dict
  279. left join t_dict_item on t_dict.id = t_dict_item.dict_id
  280. where item_text = ?
  281. and t_dict.dict_code = 'schedule_Strategy'`
  282. //查询调度策略
  283. err := s.DbEngin.Raw(sqlStr, name).Scan(&strategy).Error
  284. if err != nil {
  285. return strategy, nil
  286. }
  287. return strategy, nil
  288. }
  289. func (s *AiStorage) AddNoticeInfo(adapterId string, adapterName string, clusterId string, clusterName string, taskName string, noticeType string, incident string) {
  290. aId, err := strconv.ParseInt(adapterId, 10, 64)
  291. if err != nil {
  292. return
  293. }
  294. cId, err := strconv.ParseInt(clusterId, 10, 64)
  295. if err != nil {
  296. return
  297. }
  298. noticeInfo := clientCore.NoticeInfo{
  299. AdapterId: aId,
  300. AdapterName: adapterName,
  301. ClusterId: cId,
  302. ClusterName: clusterName,
  303. NoticeType: noticeType,
  304. TaskName: taskName,
  305. Incident: incident,
  306. CreatedTime: time.Now(),
  307. }
  308. result := s.DbEngin.Table("t_notice").Create(&noticeInfo)
  309. if result.Error != nil {
  310. logx.Errorf("Task creation failure, err: %v", result.Error)
  311. }
  312. }

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.