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.

ai.go 6.3 kB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package participant
  2. import (
  3. "errors"
  4. "fmt"
  5. "github.com/go-resty/resty/v2"
  6. "gitlink.org.cn/JointCloud/pcm-coordinator/internal/scheduler/database"
  7. "net/http"
  8. "sync"
  9. )
  10. const (
  11. // 算法路由
  12. AlgorithmById = "/ai/algorithm/get" //根据Id查询算法列表
  13. AlgorithmsList = "/ai/algorithm/list" //所有算法列表
  14. AlgorithmCreateById = "/ai/algorithm/create" //根据Id创建算法
  15. // 数据集路由
  16. DatasetCreateById = "/ai/dataset/create" //根据Id创建数据集
  17. // 模型相关路由
  18. ModelCreateById = "/ai/model/create" //根据Id创建模型
  19. // 资源相关路由
  20. ResourceSpecList = "/ai/resource/specs" //所有资源列表,根据参数 train or infer 查询资源
  21. ResourceTrainingById = "/ai/resource/train/get" //根据Id查询资源列表
  22. ResourceTrainingList = "/ai/resource/train/list" //所有训练资源列表
  23. // 任务相关路由
  24. TaskCreateTrain = "/ai/task/train"
  25. TaskResultSync = "/ai/task/sync"
  26. TaskLog = "/ai/task/log"
  27. TaskTrainingDetail = "/ai/task/train/detail"
  28. TaskInferenceDetail = "/ai/task/infer/detail"
  29. )
  30. type Ai struct {
  31. store *database.AiStorage
  32. idAddr sync.Map
  33. }
  34. func New(store *database.AiStorage, adapterId string) (*Ai, error) {
  35. if store == nil {
  36. return nil, errors.New("store cannot be nil")
  37. }
  38. a := &Ai{
  39. store: store,
  40. }
  41. css, err := store.GetClustersByAdapterId(adapterId)
  42. if err != nil {
  43. return nil, fmt.Errorf("failed to get clusters: %w", err)
  44. }
  45. for _, info := range css.List {
  46. a.idAddr.Store(info.Id, info.Server)
  47. }
  48. InitClient()
  49. return a, nil
  50. }
  51. func (a *Ai) UpdateAddr(id string, addr string) {
  52. a.idAddr.Store(id, addr)
  53. }
  54. func (a *Ai) GetServerAddrById(id string) (string, bool) {
  55. val, ok := a.idAddr.Load(id)
  56. if !ok {
  57. return "", false
  58. }
  59. addr, ok := val.(string)
  60. if !ok {
  61. return "", false
  62. }
  63. return addr, true
  64. }
  65. func (a *Ai) AlgorithmById(platformId string) (resp *Resp, err error) {
  66. addr, ok := a.GetServerAddrById(platformId)
  67. if !ok {
  68. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  69. }
  70. respErr := &RespErr{}
  71. _, err = Request(addr+AlgorithmById, http.MethodGet, func(req *resty.Request) {
  72. req.SetQueryParams(map[string]string{
  73. "pfId": platformId,
  74. }).SetError(&respErr).SetResult(&resp)
  75. })
  76. if err != nil {
  77. return nil, err
  78. }
  79. return
  80. }
  81. func (a *Ai) AlgorithmCreateById(platformId string, param *CreateParam) (resp *Resp, err error) {
  82. addr, ok := a.GetServerAddrById(platformId)
  83. if !ok {
  84. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  85. }
  86. respErr := &RespErr{}
  87. _, err = Request(addr+AlgorithmCreateById, http.MethodPost, func(req *resty.Request) {
  88. req.SetQueryParams(map[string]string{
  89. "pfId": platformId,
  90. }).SetBody(param).SetError(&respErr).SetResult(&resp)
  91. })
  92. if err != nil {
  93. return nil, err
  94. }
  95. return
  96. }
  97. func (a *Ai) DatasetCreateById(platformId string, param *CreateParam) (resp *Resp, err error) {
  98. addr, ok := a.GetServerAddrById(platformId)
  99. if !ok {
  100. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  101. }
  102. respErr := &RespErr{}
  103. _, err = Request(addr+DatasetCreateById, http.MethodPost, func(req *resty.Request) {
  104. req.SetQueryParams(map[string]string{
  105. "pfId": platformId,
  106. }).SetBody(param).SetError(&respErr).SetResult(&resp)
  107. })
  108. if err != nil {
  109. return nil, err
  110. }
  111. return
  112. }
  113. func (a *Ai) ModelCreateById(platformId string, param *CreateParam) (resp *Resp, err error) {
  114. addr, ok := a.GetServerAddrById(platformId)
  115. if !ok {
  116. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  117. }
  118. respErr := &RespErr{}
  119. _, err = Request(addr+ModelCreateById, http.MethodPost, func(req *resty.Request) {
  120. req.SetQueryParams(map[string]string{
  121. "pfId": platformId,
  122. }).SetBody(param).SetError(&respErr).SetResult(&resp)
  123. })
  124. if err != nil {
  125. return nil, err
  126. }
  127. return
  128. }
  129. func (a *Ai) TaskCreateTrain(platformId string, param *TaskCreateParam) (resp *Resp, err error) {
  130. addr, ok := a.GetServerAddrById(platformId)
  131. if !ok {
  132. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  133. }
  134. respErr := &RespErr{}
  135. _, err = Request(addr+TaskCreateTrain, http.MethodPost, func(req *resty.Request) {
  136. req.SetQueryParams(map[string]string{
  137. "pfId": platformId,
  138. }).SetBody(param).SetError(&respErr).SetResult(&resp)
  139. })
  140. if err != nil {
  141. return nil, err
  142. }
  143. return
  144. }
  145. func (a *Ai) TaskResultSync(platformId string, param *TaskResultSyncParam) (resp *Resp, err error) {
  146. addr, ok := a.GetServerAddrById(platformId)
  147. if !ok {
  148. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  149. }
  150. respErr := &RespErr{}
  151. _, err = Request(addr+TaskResultSync, http.MethodPost, func(req *resty.Request) {
  152. req.SetQueryParams(map[string]string{
  153. "pfId": platformId,
  154. }).SetBody(param).SetError(&respErr).SetResult(&resp)
  155. })
  156. if err != nil {
  157. return nil, err
  158. }
  159. return
  160. }
  161. func (a *Ai) TaskLog(platformId string, taskId string) (resp *Resp, err error) {
  162. addr, ok := a.GetServerAddrById(platformId)
  163. if !ok {
  164. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  165. }
  166. respErr := &RespErr{}
  167. _, err = Request(addr+TaskLog, http.MethodGet, func(req *resty.Request) {
  168. req.SetQueryParams(map[string]string{
  169. "pfId": platformId,
  170. "taskId": taskId,
  171. }).SetError(&respErr).SetResult(&resp)
  172. })
  173. if err != nil {
  174. return nil, err
  175. }
  176. return
  177. }
  178. func (a *Ai) TaskTrainingDetail(platformId string, taskId string) (resp *Resp, err error) {
  179. addr, ok := a.GetServerAddrById(platformId)
  180. if !ok {
  181. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  182. }
  183. respErr := &RespErr{}
  184. _, err = Request(addr+TaskTrainingDetail, http.MethodGet, func(req *resty.Request) {
  185. req.SetQueryParams(map[string]string{
  186. "pfId": platformId,
  187. "taskId": taskId,
  188. }).SetError(&respErr).SetResult(&resp)
  189. })
  190. if err != nil {
  191. return nil, err
  192. }
  193. return
  194. }
  195. func (a *Ai) TaskInferenceDetail(platformId string, taskId string) (resp *Resp, err error) {
  196. addr, ok := a.GetServerAddrById(platformId)
  197. if !ok {
  198. return nil, fmt.Errorf("clusterId not found: %s", platformId)
  199. }
  200. respErr := &RespErr{}
  201. _, err = Request(addr+TaskInferenceDetail, http.MethodGet, func(req *resty.Request) {
  202. req.SetQueryParams(map[string]string{
  203. "pfId": platformId,
  204. "taskId": taskId,
  205. }).SetError(&respErr).SetResult(&resp)
  206. })
  207. if err != nil {
  208. return nil, err
  209. }
  210. return
  211. }

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.