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.

createdeploytasklogic.go 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. package inference
  2. import (
  3. "context"
  4. "errors"
  5. "fmt"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/schedulers/option"
  7. "strconv"
  8. "sync"
  9. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc"
  10. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types"
  11. "github.com/zeromicro/go-zero/core/logx"
  12. )
  13. type CreateDeployTaskLogic struct {
  14. logx.Logger
  15. ctx context.Context
  16. svcCtx *svc.ServiceContext
  17. }
  18. func NewCreateDeployTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateDeployTaskLogic {
  19. return &CreateDeployTaskLogic{
  20. Logger: logx.WithContext(ctx),
  21. ctx: ctx,
  22. svcCtx: svcCtx,
  23. }
  24. }
  25. func (l *CreateDeployTaskLogic) CreateDeployTask(req *types.CreateDeployTaskReq) (resp *types.CreateDeployTaskResp, err error) {
  26. resp = &types.CreateDeployTaskResp{}
  27. if len(req.AdapterClusterMap) == 0 {
  28. return nil, errors.New("adapters are empty")
  29. }
  30. opt := &option.InferOption{
  31. TaskName: req.TaskName,
  32. ModelType: req.ModelType,
  33. ModelName: req.ModelName,
  34. Cmd: "",
  35. }
  36. duplicated, err := l.svcCtx.Scheduler.AiStorages.IsDeployTaskNameDuplicated(req.TaskName)
  37. if err != nil {
  38. return nil, err
  39. }
  40. if duplicated {
  41. return nil, errors.New("TaskName already exists")
  42. }
  43. taskId, err := l.svcCtx.Scheduler.AiStorages.SaveInferDeployTask(req.TaskName, 0, req.ModelName, req.ModelType, req.TaskDesc)
  44. if err != nil {
  45. return nil, err
  46. }
  47. var clusterlen int
  48. for _, c := range req.AdapterClusterMap {
  49. clusterlen += len(c)
  50. }
  51. var errCh = make(chan interface{}, clusterlen)
  52. var errs []interface{}
  53. buf := make(chan bool, 2)
  54. var wg sync.WaitGroup
  55. for aid, v := range req.AdapterClusterMap {
  56. for _, c := range v {
  57. wg.Add(1)
  58. cid := c
  59. buf <- true
  60. go func() {
  61. err = l.createDeployInstance(taskId, aid, cid, opt)
  62. if err != nil {
  63. e := struct {
  64. err error
  65. clusterId string
  66. }{
  67. err: err,
  68. clusterId: cid,
  69. }
  70. errCh <- e
  71. wg.Done()
  72. <-buf
  73. return
  74. }
  75. wg.Done()
  76. <-buf
  77. }()
  78. }
  79. }
  80. wg.Wait()
  81. close(errCh)
  82. for e := range errCh {
  83. errs = append(errs, e)
  84. }
  85. if len(errs) != 0 {
  86. var msg string
  87. for _, err := range errs {
  88. e := (err).(struct {
  89. err error
  90. clusterId string
  91. })
  92. clusterName, err := l.svcCtx.Scheduler.AiStorages.GetClusterNameById(e.clusterId)
  93. if err != nil {
  94. clusterName = e.clusterId
  95. }
  96. msg += fmt.Sprintf("CreateInstance Failed # clusterName: %v, error: %v \n", clusterName, e.err.Error())
  97. }
  98. return nil, errors.New(msg)
  99. }
  100. return
  101. }
  102. func (l *CreateDeployTaskLogic) createDeployInstance(taskId int64, adapterId string, clusterId string, opt *option.InferOption) error {
  103. cmap, found := l.svcCtx.Scheduler.AiService.InferenceAdapterMap[adapterId]
  104. if !found {
  105. return errors.New("adapterId not exist: " + adapterId)
  106. }
  107. iCluster, found := cmap[clusterId]
  108. if !found {
  109. return errors.New("clusterId not exist: " + clusterId)
  110. }
  111. insId, err := iCluster.CreateInferDeployInstance(l.ctx, opt)
  112. if err != nil {
  113. return err
  114. }
  115. aid, err := strconv.ParseInt(adapterId, 10, 64)
  116. if err != nil {
  117. return err
  118. }
  119. cid, err := strconv.ParseInt(clusterId, 10, 64)
  120. if err != nil {
  121. return err
  122. }
  123. adapterName, err := l.svcCtx.Scheduler.AiStorages.GetAdapterNameById(adapterId)
  124. if err != nil {
  125. return err
  126. }
  127. clusterName, err := l.svcCtx.Scheduler.AiStorages.GetClusterNameById(clusterId)
  128. if err != nil {
  129. return err
  130. }
  131. ins, err := iCluster.GetInferDeployInstance(l.ctx, insId)
  132. if err != nil {
  133. return err
  134. }
  135. _, err = l.svcCtx.Scheduler.AiStorages.SaveInferDeployInstance(taskId, ins.InstanceId, ins.InstanceName, aid, adapterName, cid, clusterName, ins.ModelName, ins.ModelType, ins.InferCard, ins.ClusterType)
  136. if err != nil {
  137. return err
  138. }
  139. return nil
  140. }

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.