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.

deployinstancelistlogic.go 3.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package inference
  2. import (
  3. "context"
  4. "errors"
  5. "github.com/zeromicro/go-zero/core/logx"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/common"
  7. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/service/utils/status"
  8. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
  10. "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
  11. "time"
  12. )
  13. type DeployInstanceListLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewDeployInstanceListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *DeployInstanceListLogic {
  19. return &DeployInstanceListLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. }
  24. }
  25. func (l *DeployInstanceListLogic) DeployInstanceList(req *types.DeployInstanceListReq) (resp *types.DeployInstanceListResp, err error) {
  26. limit := req.PageSize
  27. offset := req.PageSize * (req.PageNum - 1)
  28. resp = &types.DeployInstanceListResp{}
  29. var tasklist []*models.AiDeployInstanceTask
  30. tx := l.svcCtx.DbEngin.Raw("select * from ai_deploy_instance_task").Scan(&tasklist)
  31. if tx.Error != nil {
  32. logx.Errorf(tx.Error.Error())
  33. return nil, tx.Error
  34. }
  35. if len(tasklist) == 0 {
  36. resp.List = nil
  37. resp.PageSize = req.PageSize
  38. resp.PageNum = req.PageNum
  39. resp.Total = 0
  40. return
  41. }
  42. //count total
  43. var total int64
  44. err = tx.Count(&total).Error
  45. tx.Limit(limit).Offset(offset)
  46. if err != nil {
  47. return resp, err
  48. }
  49. err = tx.Order("create_time desc").Find(&tasklist).Error
  50. if err != nil {
  51. return nil, errors.New(err.Error())
  52. }
  53. deployTasks, err := l.GenerateDeployTasks(tasklist)
  54. if err != nil {
  55. return nil, errors.New(err.Error())
  56. }
  57. slices := make([][]*models.AiInferDeployInstance, len(deployTasks))
  58. for i := 0; i < len(deployTasks); i++ {
  59. slices[i] = deployTasks[i].Instances
  60. }
  61. list := common.ConcatMultipleSlices(slices)
  62. if len(list) != 0 {
  63. go status.UpdateDeployInstanceStatusBatch(l.svcCtx, list, true)
  64. ins := list[0]
  65. for i := range list {
  66. uTime, _ := time.Parse(time.RFC3339, ins.UpdateTime)
  67. latest, _ := time.Parse(time.RFC3339, list[i].UpdateTime)
  68. if latest.After(uTime) {
  69. ins = list[i]
  70. }
  71. }
  72. go status.UpdateDeployInstanceStatus(l.svcCtx, ins, true, nil)
  73. go status.UpdateDeployTaskStatus(l.svcCtx)
  74. }
  75. resp.List = &deployTasks
  76. resp.PageSize = req.PageSize
  77. resp.PageNum = req.PageNum
  78. resp.Total = total
  79. return
  80. }
  81. func (l *DeployInstanceListLogic) GenerateDeployTasks(tasklist []*models.AiDeployInstanceTask) ([]*DeployTask, error) {
  82. var tasks []*DeployTask
  83. for _, t := range tasklist {
  84. list, err := l.svcCtx.Scheduler.AiStorages.GetInstanceListByDeployTaskId(t.Id)
  85. if err != nil {
  86. logx.Errorf("db GetInstanceListByDeployTaskId error")
  87. return nil, errors.New(err.Error())
  88. }
  89. if len(list) == 0 {
  90. err := l.svcCtx.Scheduler.AiStorages.DeleteDeployTaskById(t.Id)
  91. if err != nil {
  92. logx.Errorf("db DeleteByDeployTaskId error")
  93. return nil, errors.New(err.Error())
  94. }
  95. continue
  96. }
  97. deployTask := &DeployTask{
  98. Id: t.Id,
  99. Name: t.Name,
  100. Desc: t.Desc,
  101. Instances: list,
  102. }
  103. tasks = append(tasks, deployTask)
  104. }
  105. return tasks, nil
  106. }
  107. type DeployTask struct {
  108. Id int64 `json:"id,string"`
  109. Name string `json:"name"`
  110. Desc string `json:"desc"`
  111. Instances []*models.AiInferDeployInstance `json:"instances,string"`
  112. }

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.