From 6003e14e736f7c837a0a84058a5037d97ed7cebe Mon Sep 17 00:00:00 2001 From: tzwang Date: Thu, 25 Jul 2024 17:13:10 +0800 Subject: [PATCH] updated deployinstance logics Former-commit-id: 6ce56194e88cb3d4b2a02c249fc8d0de551de99f --- go.mod | 2 +- go.sum | 4 +-- .../handler/ai/trainingtaskstathandler.go | 8 ++--- .../inference/deployinstancestathandler.go | 10 +++--- .../inference/inferencetaskstathandler.go | 13 +++----- internal/logic/ai/trainingtaskstatlogic.go | 21 ++++++++++-- .../inference/deployinstancestatlogic.go | 21 ++++++++++-- .../logic/inference/inferencetaskstatlogic.go | 21 ++++++++++-- .../inference/startdeployinstancelistlogic.go | 10 ++++++ .../inference/stopdeployinstancelogic.go | 9 ++++++ .../service/updater/deployInstance.go | 32 +++++++++++++------ pkg/models/aiinferdeployinstancemodel_gen.go | 1 + 12 files changed, 114 insertions(+), 38 deletions(-) diff --git a/go.mod b/go.mod index 1a7655f6..d5d98134 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( github.com/prometheus/common v0.55.0 github.com/robfig/cron/v3 v3.0.1 github.com/zeromicro/go-zero v1.6.6 - gitlink.org.cn/JointCloud/pcm-ac v0.0.0-20240722032933-7bd6ab00d249 + gitlink.org.cn/JointCloud/pcm-ac v0.0.0-20240725071305-f751eec4dde1 gitlink.org.cn/JointCloud/pcm-modelarts v0.0.0-20240722092017-50d17f36d023 gitlink.org.cn/JointCloud/pcm-octopus v0.0.0-20240718073732-bc5d687f6330 gitlink.org.cn/JointCloud/pcm-openstack v0.0.0-20240403033338-e7edabad4203 diff --git a/go.sum b/go.sum index 950557e5..d8c10e10 100644 --- a/go.sum +++ b/go.sum @@ -471,8 +471,8 @@ github.com/yuin/gopher-lua v1.1.1 h1:kYKnWBjvbNP4XLT3+bPEwAXJx262OhaHDWDVOPjL46M github.com/yuin/gopher-lua v1.1.1/go.mod h1:GBR0iDaNXjAgGg9zfCvksxSRnQx76gclCIb7kdAd1Pw= github.com/zeromicro/go-zero v1.6.6 h1:nZTVYObklHiBdYJ/nPoAZ8kGVAplWSDjT7DGE7ur0uk= github.com/zeromicro/go-zero v1.6.6/go.mod h1:olKf1/hELbSmuIgLgJeoeNVp3tCbLqj6UmO7ATSta4A= -gitlink.org.cn/JointCloud/pcm-ac v0.0.0-20240722032933-7bd6ab00d249 h1:bHJGq5P+8w4fP62PZhIiq/fvOhvDPRtkM4pcmU8OZ1w= -gitlink.org.cn/JointCloud/pcm-ac v0.0.0-20240722032933-7bd6ab00d249/go.mod h1:3eECiw9O2bIFkkePlloKyLNXiqBAhOxNrDoGaaGseGY= +gitlink.org.cn/JointCloud/pcm-ac v0.0.0-20240725071305-f751eec4dde1 h1:DicBXoQiC6mumMBeyqSPNrsjtqJIgk5Pv2hscu2xryw= +gitlink.org.cn/JointCloud/pcm-ac v0.0.0-20240725071305-f751eec4dde1/go.mod h1:3eECiw9O2bIFkkePlloKyLNXiqBAhOxNrDoGaaGseGY= gitlink.org.cn/JointCloud/pcm-modelarts v0.0.0-20240722092017-50d17f36d023 h1:9DNobl0gvRCSXtjyMsfUwq0w0TMvds4rqNRsEqeX4j8= gitlink.org.cn/JointCloud/pcm-modelarts v0.0.0-20240722092017-50d17f36d023/go.mod h1:/eOmBFZKWGoabG3sRVkVvIbLwsd2631k4jkUBR6x1AA= gitlink.org.cn/JointCloud/pcm-octopus v0.0.0-20240718073732-bc5d687f6330 h1:WxPrFSO6LjDCr+k7nmNFlPst8CtoTHQ2iSjv+D2rNnM= diff --git a/internal/handler/ai/trainingtaskstathandler.go b/internal/handler/ai/trainingtaskstathandler.go index 34459d0b..813ed0c1 100644 --- a/internal/handler/ai/trainingtaskstathandler.go +++ b/internal/handler/ai/trainingtaskstathandler.go @@ -1,9 +1,9 @@ package ai import ( + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/ai" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" ) @@ -12,10 +12,6 @@ func TrainingTaskStatHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { l := ai.NewTrainingTaskStatLogic(r.Context(), svcCtx) resp, err := l.TrainingTaskStat() - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) } } diff --git a/internal/handler/inference/deployinstancestathandler.go b/internal/handler/inference/deployinstancestathandler.go index e031c9a7..2427ef60 100644 --- a/internal/handler/inference/deployinstancestathandler.go +++ b/internal/handler/inference/deployinstancestathandler.go @@ -1,6 +1,7 @@ package inference import ( + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" "net/http" "github.com/zeromicro/go-zero/rest/httpx" @@ -13,16 +14,13 @@ func DeployInstanceStatHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.DeployInstanceStatReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := inference.NewDeployInstanceStatLogic(r.Context(), svcCtx) resp, err := l.DeployInstanceStat(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) + } } diff --git a/internal/handler/inference/inferencetaskstathandler.go b/internal/handler/inference/inferencetaskstathandler.go index 8da35222..dda0f173 100644 --- a/internal/handler/inference/inferencetaskstathandler.go +++ b/internal/handler/inference/inferencetaskstathandler.go @@ -1,28 +1,25 @@ package inference import ( - "net/http" - "github.com/zeromicro/go-zero/rest/httpx" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/logic/inference" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/repository/result" + "net/http" ) func InferenceTaskStatHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { var req types.InferenceTaskStatReq if err := httpx.Parse(r, &req); err != nil { - httpx.ErrorCtx(r.Context(), w, err) + result.ParamErrorResult(r, w, err) return } l := inference.NewInferenceTaskStatLogic(r.Context(), svcCtx) resp, err := l.InferenceTaskStat(&req) - if err != nil { - httpx.ErrorCtx(r.Context(), w, err) - } else { - httpx.OkJsonCtx(r.Context(), w, resp) - } + result.HttpResult(r, w, resp, err) + } } diff --git a/internal/logic/ai/trainingtaskstatlogic.go b/internal/logic/ai/trainingtaskstatlogic.go index 1d59186e..c745fb81 100644 --- a/internal/logic/ai/trainingtaskstatlogic.go +++ b/internal/logic/ai/trainingtaskstatlogic.go @@ -2,6 +2,7 @@ package ai import ( "context" + "errors" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" @@ -24,7 +25,23 @@ func NewTrainingTaskStatLogic(ctx context.Context, svcCtx *svc.ServiceContext) * } func (l *TrainingTaskStatLogic) TrainingTaskStat() (resp *types.TrainingTaskStatResp, err error) { - // todo: add your logic here and delete this line + resp = &types.TrainingTaskStatResp{} - return + total, err := l.svcCtx.Scheduler.AiStorages.GetTrainingTaskTotalNum() + if err != nil { + return nil, err + } + + running, err := l.svcCtx.Scheduler.AiStorages.GetTrainingTaskRunningNum() + if err != nil { + return nil, err + } + + if total == 0 { + return nil, errors.New("get statistics failed") + } + + resp.Total = total + resp.Running = running + return resp, nil } diff --git a/internal/logic/inference/deployinstancestatlogic.go b/internal/logic/inference/deployinstancestatlogic.go index e65ed2d0..92043fa6 100644 --- a/internal/logic/inference/deployinstancestatlogic.go +++ b/internal/logic/inference/deployinstancestatlogic.go @@ -2,6 +2,7 @@ package inference import ( "context" + "errors" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" @@ -24,7 +25,23 @@ func NewDeployInstanceStatLogic(ctx context.Context, svcCtx *svc.ServiceContext) } func (l *DeployInstanceStatLogic) DeployInstanceStat(req *types.DeployInstanceStatReq) (resp *types.DeployInstanceStatResp, err error) { - // todo: add your logic here and delete this line + resp = &types.DeployInstanceStatResp{} - return + total, err := l.svcCtx.Scheduler.AiStorages.GetInferDeployInstanceTotalNum() + if err != nil { + return nil, err + } + + running, err := l.svcCtx.Scheduler.AiStorages.GetInferDeployInstanceRunningNum() + if err != nil { + return nil, err + } + + if total == 0 { + return nil, errors.New("get statistics failed") + } + + resp.Total = total + resp.Running = running + return resp, nil } diff --git a/internal/logic/inference/inferencetaskstatlogic.go b/internal/logic/inference/inferencetaskstatlogic.go index 8f7c71f4..b622d6bf 100644 --- a/internal/logic/inference/inferencetaskstatlogic.go +++ b/internal/logic/inference/inferencetaskstatlogic.go @@ -2,6 +2,7 @@ package inference import ( "context" + "errors" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" @@ -24,7 +25,23 @@ func NewInferenceTaskStatLogic(ctx context.Context, svcCtx *svc.ServiceContext) } func (l *InferenceTaskStatLogic) InferenceTaskStat(req *types.InferenceTaskStatReq) (resp *types.InferenceTaskStatResp, err error) { - // todo: add your logic here and delete this line + resp = &types.InferenceTaskStatResp{} - return + total, err := l.svcCtx.Scheduler.AiStorages.GetInferenceTaskTotalNum() + if err != nil { + return nil, err + } + + running, err := l.svcCtx.Scheduler.AiStorages.GetInferenceTaskRunningNum() + if err != nil { + return nil, err + } + + if total == 0 { + return nil, errors.New("get statistics failed") + } + + resp.Total = total + resp.Running = running + return resp, nil } diff --git a/internal/logic/inference/startdeployinstancelistlogic.go b/internal/logic/inference/startdeployinstancelistlogic.go index 272568fc..eb6690c5 100644 --- a/internal/logic/inference/startdeployinstancelistlogic.go +++ b/internal/logic/inference/startdeployinstancelistlogic.go @@ -6,6 +6,7 @@ import ( "github.com/zeromicro/go-zero/core/logx" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" + "strconv" ) type StartDeployInstanceListLogic struct { @@ -25,8 +26,17 @@ func NewStartDeployInstanceListLogic(ctx context.Context, svcCtx *svc.ServiceCon func (l *StartDeployInstanceListLogic) StartDeployInstanceList(req *types.StartDeployInstanceReq) (resp *types.StartDeployInstanceResp, err error) { resp = &types.StartDeployInstanceResp{} success := l.svcCtx.Scheduler.AiService.InferenceAdapterMap[req.AdapterId][req.ClusterId].StartInferDeployInstance(l.ctx, req.InstanceId) + id, err := strconv.ParseInt(req.Id, 10, 64) + ins, err := l.svcCtx.Scheduler.AiStorages.GetInferDeployInstanceById(id) + if err != nil { + return nil, err + } + + l.svcCtx.Scheduler.AiStorages.UpdateInferDeployInstance(ins) + if !success { return nil, errors.New("start instance failed") } + return resp, nil } diff --git a/internal/logic/inference/stopdeployinstancelogic.go b/internal/logic/inference/stopdeployinstancelogic.go index 00c3c128..75587205 100644 --- a/internal/logic/inference/stopdeployinstancelogic.go +++ b/internal/logic/inference/stopdeployinstancelogic.go @@ -6,6 +6,7 @@ import ( "github.com/zeromicro/go-zero/core/logx" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/types" + "strconv" ) type StopDeployInstanceLogic struct { @@ -25,6 +26,14 @@ func NewStopDeployInstanceLogic(ctx context.Context, svcCtx *svc.ServiceContext) func (l *StopDeployInstanceLogic) StopDeployInstance(req *types.StopDeployInstanceReq) (resp *types.StopDeployInstanceResp, err error) { resp = &types.StopDeployInstanceResp{} success := l.svcCtx.Scheduler.AiService.InferenceAdapterMap[req.AdapterId][req.ClusterId].StopInferDeployInstance(l.ctx, req.InstanceId) + id, err := strconv.ParseInt(req.Id, 10, 64) + ins, err := l.svcCtx.Scheduler.AiStorages.GetInferDeployInstanceById(id) + if err != nil { + return nil, err + } + + l.svcCtx.Scheduler.AiStorages.UpdateInferDeployInstance(ins) + if !success { return nil, errors.New("stop instance failed") } diff --git a/internal/scheduler/service/updater/deployInstance.go b/internal/scheduler/service/updater/deployInstance.go index b4b7c74c..42fac905 100644 --- a/internal/scheduler/service/updater/deployInstance.go +++ b/internal/scheduler/service/updater/deployInstance.go @@ -1,16 +1,14 @@ package updater import ( + "gitlink.org.cn/JointCloud/pcm-coordinator/internal/storeLink" "gitlink.org.cn/JointCloud/pcm-coordinator/internal/svc" + "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" "net/http" "strconv" ) -//func UpdateDeployInstanceStatus(svc *svc.ServiceContext, list []*models.AiInferDeployInstance) { -// -//} - func UpdateDeployInstanceStatus(svc *svc.ServiceContext, instance *models.AiInferDeployInstance) { amap, found := svc.Scheduler.AiService.InferenceAdapterMap[strconv.FormatInt(instance.AdapterId, 10)] if !found { @@ -25,13 +23,29 @@ func UpdateDeployInstanceStatus(svc *svc.ServiceContext, instance *models.AiInfe if err != nil { return } - instance.Status = ins.Status + switch instance.ClusterType { + case storeLink.TYPE_OCTOPUS: + switch ins.Status { + case "running": + instance.Status = constants.Running + case "stopped": + instance.Status = constants.Stopped + default: + instance.Status = ins.Status + } + + case storeLink.TYPE_SHUGUANGAI: + switch ins.Status { + case "running": + instance.Status = constants.Running + case "Terminated": + instance.Status = constants.Stopped + default: + instance.Status = ins.Status + } + } err = svc.Scheduler.AiStorages.UpdateInferDeployInstance(instance) if err != nil { return } } - -func UpdateDeployInstanceStatusByClusterId(svc *svc.ServiceContext, list []*models.AiInferDeployInstance) { - -} diff --git a/pkg/models/aiinferdeployinstancemodel_gen.go b/pkg/models/aiinferdeployinstancemodel_gen.go index 00362fe6..cf3fc6e9 100644 --- a/pkg/models/aiinferdeployinstancemodel_gen.go +++ b/pkg/models/aiinferdeployinstancemodel_gen.go @@ -48,6 +48,7 @@ type ( Status string `db:"status"` CreateTime string `db:"create_time"` UpdateTime string `db:"update_time"` + ClusterType string `db:"cluster_type"` } )