From 54bf196b5ff31aa91659b1e93ce572b051eec18f Mon Sep 17 00:00:00 2001 From: qiwang <1364512070@qq.com> Date: Thu, 27 Jun 2024 09:52:35 +0800 Subject: [PATCH 01/11] fix: update task_vm 0626 Former-commit-id: 2dede3eca7afac77f9f2d77c0d30548a94cd4754 --- api/client/types.go | 2 +- pkg/models/taskvmmodel_gen.go | 7 ++++--- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/api/client/types.go b/api/client/types.go index 2aa28e22..bf5c676d 100644 --- a/api/client/types.go +++ b/api/client/types.go @@ -175,7 +175,7 @@ type VmInfo struct { //DeletedAt string `json:"deletedAt,omitempty"` VmName string `json:"vmName,omitempty"` Replicas int64 `json:"replicas,omitempty"` - //ServerId string `json:"serverId,omitempty"` + ServerId string `json:"serverId,omitempty"` } type ResourceStats struct { diff --git a/pkg/models/taskvmmodel_gen.go b/pkg/models/taskvmmodel_gen.go index 697ffbe9..74f6d4e6 100644 --- a/pkg/models/taskvmmodel_gen.go +++ b/pkg/models/taskvmmodel_gen.go @@ -55,6 +55,7 @@ type ( DeletedAt string `db:"deleted_at"` // 删除时间 VmName string `db:"vm_name"` // 虚拟机名称 Replicas int64 `db:"replicas"` // 副本数 + ServerId string `db:"server_id"` // 虚拟机id } ) @@ -93,14 +94,14 @@ func (m *defaultTaskVmModel) FindOne(ctx context.Context, id int64) (*TaskVm, er } func (m *defaultTaskVmModel) Insert(ctx context.Context, data *TaskVm) (sql.Result, error) { - query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, taskVmRowsExpectAutoSet) - ret, err := m.conn.ExecCtx(ctx, query, data.TaskId, data.Name, data.AdapterId, data.AdapterName, data.ClusterId, data.ClusterName, data.FlavorRef, data.ImageRef, data.Status, data.Platform, data.MinCount, data.Uuid, data.StartTime, data.RunningTime, data.Result, data.Remark, data.DeletedAt, data.VmName, data.Replicas) + query := fmt.Sprintf("insert into %s (%s) values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)", m.table, taskVmRowsExpectAutoSet) + ret, err := m.conn.ExecCtx(ctx, query, data.TaskId, data.Name, data.AdapterId, data.AdapterName, data.ClusterId, data.ClusterName, data.FlavorRef, data.ImageRef, data.Status, data.Platform, data.MinCount, data.Uuid, data.StartTime, data.RunningTime, data.Result, data.Remark, data.DeletedAt, data.VmName, data.Replicas, data.ServerId) return ret, err } func (m *defaultTaskVmModel) Update(ctx context.Context, data *TaskVm) error { query := fmt.Sprintf("update %s set %s where `id` = ?", m.table, taskVmRowsWithPlaceHolder) - _, err := m.conn.ExecCtx(ctx, query, data.TaskId, data.Name, data.AdapterId, data.AdapterName, data.ClusterId, data.ClusterName, data.FlavorRef, data.ImageRef, data.Status, data.Platform, data.MinCount, data.Uuid, data.StartTime, data.RunningTime, data.Result, data.Remark, data.DeletedAt, data.VmName, data.Replicas, data.Id) + _, err := m.conn.ExecCtx(ctx, query, data.TaskId, data.Name, data.AdapterId, data.AdapterName, data.ClusterId, data.ClusterName, data.FlavorRef, data.ImageRef, data.Status, data.Platform, data.MinCount, data.Uuid, data.StartTime, data.RunningTime, data.Result, data.Remark, data.DeletedAt, data.VmName, data.Replicas, data.ServerId, data.Id) return err } From ee87491df44eb930e621a21b8e2bb48ccd87538e Mon Sep 17 00:00:00 2001 From: tzwang Date: Thu, 27 Jun 2024 11:20:05 +0800 Subject: [PATCH 02/11] fix imageinfer api bug Former-commit-id: 4efe95aa95c442f0ff0ae32756ea1e418d6be349 --- api/internal/logic/inference/imageinferencelogic.go | 2 +- api/internal/scheduler/service/aiService.go | 11 ++++++----- 2 files changed, 7 insertions(+), 6 deletions(-) diff --git a/api/internal/logic/inference/imageinferencelogic.go b/api/internal/logic/inference/imageinferencelogic.go index f10a3c53..e37265ab 100644 --- a/api/internal/logic/inference/imageinferencelogic.go +++ b/api/internal/logic/inference/imageinferencelogic.go @@ -115,7 +115,7 @@ func (l *ImageInferenceLogic) ImageInfer(r *http.Request, req *types.ImageInfere l.svcCtx.Scheduler.AiStorages.AddNoticeInfo(opt.AdapterId, adapterName, "", "", opt.TaskName, "create", "任务创建中") - go l.svcCtx.Scheduler.AiService.ImageInfer(opt, id, adapterName, clusters, ts, l.ctx) + go l.svcCtx.Scheduler.AiService.ImageInfer(opt, id, adapterName, clusters, ts) return resp, nil } diff --git a/api/internal/scheduler/service/aiService.go b/api/internal/scheduler/service/aiService.go index 1e1d9df0..6d07a8e7 100644 --- a/api/internal/scheduler/service/aiService.go +++ b/api/internal/scheduler/service/aiService.go @@ -1,8 +1,7 @@ package service import ( - "context" - "fmt" + "github.com/zeromicro/go-zero/core/logx" "github.com/zeromicro/go-zero/zrpc" hpcacclient "gitlink.org.cn/JointCloud/pcm-ac/hpcacclient" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/config" @@ -17,6 +16,7 @@ import ( "gitlink.org.cn/JointCloud/pcm-modelarts/client/imagesservice" "gitlink.org.cn/JointCloud/pcm-modelarts/client/modelartsservice" "gitlink.org.cn/JointCloud/pcm-octopus/octopusclient" + "net/http" "strconv" "sync" ) @@ -91,13 +91,14 @@ func InitAiClusterMap(conf *config.Config, clusters []types.ClusterInfo) (map[st return executorMap, collectorMap } -func (as *AiService) ImageInfer(opt *option.InferOption, id int64, adapterName string, clusters []*strategy.AssignedCluster, ts []*inference.ImageFile, ctx context.Context) { +func (as *AiService) ImageInfer(opt *option.InferOption, id int64, adapterName string, clusters []*strategy.AssignedCluster, ts []*inference.ImageFile) { - res, err := inference.Infer(opt, id, adapterName, clusters, ts, as.AiCollectorAdapterMap, as.Storage, ctx) + r := http.Request{} + _, err := inference.Infer(opt, id, adapterName, clusters, ts, as.AiCollectorAdapterMap, as.Storage, r.Context()) if err != nil { + logx.Errorf(err.Error()) return } - fmt.Println(res) } //func (a *AiService) AddCluster() error { From e5582bbbcb34b709fd4b122bcc0bbb8f5b8a668f Mon Sep 17 00:00:00 2001 From: tzwang Date: Thu, 27 Jun 2024 15:44:58 +0800 Subject: [PATCH 03/11] fix staticweight bug Former-commit-id: fe3017c8e0ca945d333009e9447c7b07ac8c1f4c --- .../weightDistributing/weightDistributing.go | 5 +++++ .../scheduler/service/inference/imageInfer.go | 17 ++++++++++++++--- 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/api/internal/scheduler/algorithm/weightDistributing/weightDistributing.go b/api/internal/scheduler/algorithm/weightDistributing/weightDistributing.go index 83c15723..b1fb7d80 100644 --- a/api/internal/scheduler/algorithm/weightDistributing/weightDistributing.go +++ b/api/internal/scheduler/algorithm/weightDistributing/weightDistributing.go @@ -11,6 +11,7 @@ type Weight struct { } func DistributeReplicas(weights []*Weight, replicas int32) { + var weightSum int32 weightSum = 0 for _, w := range weights { @@ -59,6 +60,10 @@ func DistributeReplicas(weights []*Weight, replicas int32) { weightRatio[maxIdx]-- rest-- } else { + if weights[minIdx].Replica == 0 { + weightRatio[minIdx]++ + continue + } weights[minIdx].Replica-- weightRatio[minIdx]++ rest++ diff --git a/api/internal/scheduler/service/inference/imageInfer.go b/api/internal/scheduler/service/inference/imageInfer.go index 732072b2..2cf1e713 100644 --- a/api/internal/scheduler/service/inference/imageInfer.go +++ b/api/internal/scheduler/service/inference/imageInfer.go @@ -63,18 +63,23 @@ func Infer(opt *option.InferOption, id int64, adapterName string, clusters []*st } } + var mutex sync.Mutex + errMap := make(map[string]string) for _, cluster := range clusters { wg.Add(1) c := cluster go func() { imageUrls, err := collectorMap[c.ClusterId].GetInferUrl(ctx, opt) - for i, _ := range imageUrls { - imageUrls[i].Url = imageUrls[i].Url + storeLink.FORWARD_SLASH + "image" - } if err != nil { + mutex.Lock() + errMap[c.ClusterId] = err.Error() + mutex.Unlock() wg.Done() return } + for i, _ := range imageUrls { + imageUrls[i].Url = imageUrls[i].Url + storeLink.FORWARD_SLASH + "image" + } clusterName, _ := storage.GetClusterNameById(c.ClusterId) s := struct { @@ -111,6 +116,9 @@ func Infer(opt *option.InferOption, id int64, adapterName string, clusters []*st for _, t := range aiTaskList { t.Status = constants.Failed t.EndTime = time.Now().Format(time.RFC3339) + if _, ok := errMap[strconv.Itoa(int(t.ClusterId))]; ok { + t.Msg = errMap[strconv.Itoa(int(t.ClusterId))] + } err := storage.UpdateAiTask(t) if err != nil { logx.Errorf(err.Error()) @@ -142,6 +150,9 @@ func Infer(opt *option.InferOption, id int64, adapterName string, clusters []*st if ac.ClusterId == strconv.Itoa(int(t.ClusterId)) { t.Status = constants.Failed t.EndTime = time.Now().Format(time.RFC3339) + if _, ok := errMap[strconv.Itoa(int(t.ClusterId))]; ok { + t.Msg = errMap[strconv.Itoa(int(t.ClusterId))] + } err := storage.UpdateAiTask(t) if err != nil { logx.Errorf(err.Error()) From 79b51345941bea250322b98e0e618e3b4a483744 Mon Sep 17 00:00:00 2001 From: jagger Date: Thu, 27 Jun 2024 15:54:21 +0800 Subject: [PATCH 04/11] fix bug Signed-off-by: jagger Former-commit-id: 37b5cbe848e7641c1666514b03a63a79d9b98036 --- api/desc/ai/pcm-ai.api | 5 +- api/internal/handler/ai/proxyapihandler.go | 2 +- api/internal/logic/ai/proxyapilogic.go | 84 +++++++++++----------- api/internal/types/types.go | 6 +- 4 files changed, 53 insertions(+), 44 deletions(-) diff --git a/api/desc/ai/pcm-ai.api b/api/desc/ai/pcm-ai.api index fefc7ea3..8d2ccfe3 100644 --- a/api/desc/ai/pcm-ai.api +++ b/api/desc/ai/pcm-ai.api @@ -1822,8 +1822,11 @@ service AICore-api { type ( ChatReq{ - ApiUrl string `json:"apiUrl,optional"` + ApiUrl string `json:"apiUrl"` Method string `json:"method,optional"` ReqData map[string]interface{} `json:"reqData"` } + ChatResult{ + Resuluts string `json:"results,optional"` + } ) \ No newline at end of file diff --git a/api/internal/handler/ai/proxyapihandler.go b/api/internal/handler/ai/proxyapihandler.go index cbb732e2..b86ce9d2 100644 --- a/api/internal/handler/ai/proxyapihandler.go +++ b/api/internal/handler/ai/proxyapihandler.go @@ -18,7 +18,7 @@ func ProxyApiHandler(svcCtx *svc.ServiceContext) http.HandlerFunc { } l := ai.NewProxyApiLogic(r.Context(), svcCtx) - resp, err := l.ProxyApi(&req, w) + resp, err := l.ProxyApi(&req) result.HttpResult(r, w, resp, err) } } diff --git a/api/internal/logic/ai/proxyapilogic.go b/api/internal/logic/ai/proxyapilogic.go index 1e7235b0..adec665f 100644 --- a/api/internal/logic/ai/proxyapilogic.go +++ b/api/internal/logic/ai/proxyapilogic.go @@ -5,8 +5,8 @@ import ( "context" "crypto/tls" "encoding/json" - "fmt" - tool "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils" + "github.com/go-resty/resty/v2" + "github.com/pkg/errors" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/utils/hws" "net/http" @@ -30,21 +30,28 @@ func NewProxyApiLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProxyApi } } -type ChatResult struct { - Results string `json:"results"` -} +const ( + XProjectID = "d18190e28e3f45a281ef0b0696ec9d52" + XStage = "RELEASE" + ContentType = "application/json" +) -type ResponseData struct { - Results string `json:"results"` -} - -func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq, w http.ResponseWriter) (resp *types.CommonResp, err error) { +func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq) (resp *types.ChatResult, err error) { + logx.Infof("【开始处理请求,目标URL: %s】", req.ApiUrl) jsonBytes, err := json.Marshal(&req.ReqData) - // 调用第三方接口的 POST 方法 - thirdReq, err := http.NewRequest("POST", req.ApiUrl, bytes.NewBuffer(jsonBytes)) if err != nil { - return + logx.Errorf("【序列化请求数据失败: %v】", err) + return nil, errors.New("请求数据序列化失败") + } + + resp = &types.ChatResult{} + + // 构建 HTTP 请求 + request, err := http.NewRequest("POST", req.ApiUrl, bytes.NewBuffer(jsonBytes)) + if err != nil { + logx.Errorf("【构建 HTTP 请求失败: %v】", err) + return nil, errors.New("网络错误,请稍后重试") } signer := &hws.Signer{ @@ -52,38 +59,33 @@ func (l *ProxyApiLogic) ProxyApi(req *types.ChatReq, w http.ResponseWriter) (res Secret: "JWXCE9qcYbc7RjpSRIWt4WgG3ZKF6Q4lPzkJReX9", } - if err := signer.Sign(thirdReq); err != nil { - return nil, err + if err := signer.Sign(request); err != nil { + logx.Errorf("【接口签名错误: %v】", err) + return nil, errors.New("网络错误,请稍后重试") } - // 设置client信任所有证书 - tr := &http.Transport{ - TLSClientConfig: &tls.Config{InsecureSkipVerify: true}, - } - client := &http.Client{ - Transport: tr, + client := resty.New().SetTLSClientConfig(&tls.Config{InsecureSkipVerify: true}) + + response, err := client.R(). + SetHeader("X-Project-Id", XProjectID). + SetHeader("x-stage", XStage). + SetHeader("Content-Type", ContentType). + SetHeader("Authorization", request.Header.Get(hws.HeaderXAuthorization)). + SetHeader("X-Sdk-Date", request.Header.Get(hws.HeaderXDateTime)). + SetBody(jsonBytes). + SetResult(&resp). + Post(req.ApiUrl) + + if err != nil { + logx.Errorf("【远程调用接口URL:%s, 返回错误: %s】", req.ApiUrl, err.Error()) + return nil, errors.New("网络错误,请稍后重试") } - thirdReq.Header.Set("X-Project-Id", "d18190e28e3f45a281ef0b0696ec9d52") - thirdReq.Header.Set("x-stage", "RELEASE") - thirdReq.Header.Set("Authorization", thirdReq.Header.Get(hws.HeaderXAuthorization)) - thirdReq.Header.Set("X-Sdk-Date", thirdReq.Header.Get(hws.HeaderXDateTime)) - thirdReq.Header.Set("Content-Type", "application/json") - - thirdResp, err := client.Do(thirdReq) - - defer thirdReq.Body.Close() - var responseData ResponseData - decoder := json.NewDecoder(thirdResp.Body) - if err := decoder.Decode(&responseData); err != nil { - fmt.Println("Error decoding response:", err) + if response.StatusCode() != 200 { + logx.Errorf("【远程调用接口URL:%s, 返回错误: %s】", req.ApiUrl, response.Body()) + return nil, errors.New("网络错误,请稍后重试") } - chatResult := &ChatResult{} - tool.Convert(responseData, &chatResult) - return &types.CommonResp{ - Code: thirdResp.StatusCode, - Msg: "success", - Data: chatResult, - }, nil + logx.Infof("【请求处理成功,目标URL: %s】", req.ApiUrl) + return resp, nil } diff --git a/api/internal/types/types.go b/api/internal/types/types.go index ac7263a2..fb682586 100644 --- a/api/internal/types/types.go +++ b/api/internal/types/types.go @@ -2877,11 +2877,15 @@ type AiTask struct { } type ChatReq struct { - ApiUrl string `json:"apiUrl,optional"` + ApiUrl string `json:"apiUrl"` Method string `json:"method,optional"` ReqData map[string]interface{} `json:"reqData"` } +type ChatResult struct { + Resuluts string `json:"results,optional"` +} + type StorageScreenReq struct { } From f823538feb7cc366ce05e9ab4e10122a7ba36374 Mon Sep 17 00:00:00 2001 From: jagger Date: Thu, 27 Jun 2024 16:52:25 +0800 Subject: [PATCH 05/11] fix bug Signed-off-by: jagger Former-commit-id: 9770d848e79315c3461471299a68c2c9008ae045 --- .devops/api.yml | 19 +++---------------- 1 file changed, 3 insertions(+), 16 deletions(-) diff --git a/.devops/api.yml b/.devops/api.yml index ecf4f08a..59444726 100644 --- a/.devops/api.yml +++ b/.devops/api.yml @@ -51,8 +51,8 @@ workflow: input: docker_username: ((dev.docker_user)) docker_password: ((dev.docker_password)) - image_name: '"registry.cn-hangzhou.aliyuncs.com/jcce/pcm-coordinator-api"' - image_tag: git_clone_0.commit_time + image_name: '"registry.cn-hangzhou.aliyuncs.com/jcce/pcm-core-api"' + image_tag: 'latest' registry_address: '"registry.cn-hangzhou.aliyuncs.com"' docker_file: git_clone_0.git_path + '/api/Dockerfile' docker_build_path: git_clone_0.git_path @@ -65,26 +65,13 @@ workflow: - ref: end name: 结束 task: end - needs: - - kubectl_deploy_0 - - ref: kubectl_deploy_0 - name: kubectl部署资源 - task: kubectl_deploy@1.1.0 - input: - command: '"apply"' - resource_file_path: git_clone_0.git_path + '/api' - certificate_authority_data: ((dev.k8s_cad)) - server: '"https://119.45.100.73:6443"' - client_certificate_data: ((dev.k8s_ccd)) - client_key_data: ((dev.k8s_ckd)) - hosts: '""' needs: - docker_image_build_0 - ref: shell_0 name: shell image: docker.jianmuhub.com/library/debian:buster-slim env: - IMAGE_NAME: '"registry.cn-hangzhou.aliyuncs.com/jcce/pcm-coordinator-api"' + IMAGE_NAME: '"registry.cn-hangzhou.aliyuncs.com/jcce/pcm-core-api"' IMAGE_TAG: git_clone_0.commit_time SECRET_NAME: global.secret_name PROJECT_NAME: global.project_name From 785ce4f22c098df39bcc203285435a9d4b73985f Mon Sep 17 00:00:00 2001 From: jagger Date: Thu, 27 Jun 2024 17:07:03 +0800 Subject: [PATCH 06/11] fix bug Signed-off-by: jagger Former-commit-id: ea26e0ca40fe911aec7bee7f9c9d9f61227bf52d --- .devops/api.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.devops/api.yml b/.devops/api.yml index 59444726..1ecd581d 100644 --- a/.devops/api.yml +++ b/.devops/api.yml @@ -52,7 +52,7 @@ workflow: docker_username: ((dev.docker_user)) docker_password: ((dev.docker_password)) image_name: '"registry.cn-hangzhou.aliyuncs.com/jcce/pcm-core-api"' - image_tag: 'latest' + image_tag: '"latest"' registry_address: '"registry.cn-hangzhou.aliyuncs.com"' docker_file: git_clone_0.git_path + '/api/Dockerfile' docker_build_path: git_clone_0.git_path From 20f538da5bd6a406fcb4f869c88c59a6ae732ffd Mon Sep 17 00:00:00 2001 From: jagger Date: Thu, 27 Jun 2024 17:26:51 +0800 Subject: [PATCH 07/11] fix bug Signed-off-by: jagger Former-commit-id: e6b870d4a7defcf0bad01f00ec96eb7ace9d2be3 --- api/Dockerfile | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/api/Dockerfile b/api/Dockerfile index a27e861c..2eeb683f 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -1,4 +1,4 @@ -FROM golang:1.22.4-alpine3.20 AS builder +FROM registry.cn-hangzhou.aliyuncs.com/jcce-images/golang:1.22.4-alpine3.20 AS builder WORKDIR /app @@ -9,7 +9,7 @@ RUN go env -w GO111MODULE=on \ && go env -w CGO_ENABLED=0 RUN go build -o pcm-coordinator-api /app/api/pcm.go -FROM alpine:3.20 +FROM registry.cn-hangzhou.aliyuncs.com/jcce-images/alpine:3.20 WORKDIR /app From 605745d0fe69f9ca95d6930d11ef8b34e3637320 Mon Sep 17 00:00:00 2001 From: jagger Date: Thu, 27 Jun 2024 17:33:10 +0800 Subject: [PATCH 08/11] fix bug Signed-off-by: jagger Former-commit-id: 588fc8dca58443e84711d7ff4719198b3f7a4e94 --- api/Dockerfile | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/api/Dockerfile b/api/Dockerfile index 2eeb683f..afd6ce83 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -6,7 +6,8 @@ COPY . . RUN go env -w GO111MODULE=on \ && go env -w GOPROXY=https://goproxy.cn,direct \ -&& go env -w CGO_ENABLED=0 +&& go env -w CGO_ENABLED=0 \ +&& go mod download RUN go build -o pcm-coordinator-api /app/api/pcm.go FROM registry.cn-hangzhou.aliyuncs.com/jcce-images/alpine:3.20 From 0b23594787f73a549d590915d7bf7211aee85190 Mon Sep 17 00:00:00 2001 From: jagger Date: Thu, 27 Jun 2024 17:42:24 +0800 Subject: [PATCH 09/11] fix bug Signed-off-by: jagger Former-commit-id: e171caddd12aa9e69293ac0b900262fe95e1b657 --- api/Dockerfile | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/api/Dockerfile b/api/Dockerfile index afd6ce83..9fb6df85 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -7,8 +7,7 @@ COPY . . RUN go env -w GO111MODULE=on \ && go env -w GOPROXY=https://goproxy.cn,direct \ && go env -w CGO_ENABLED=0 \ -&& go mod download -RUN go build -o pcm-coordinator-api /app/api/pcm.go +&& go build -o pcm-coordinator-api /app/api/pcm.go FROM registry.cn-hangzhou.aliyuncs.com/jcce-images/alpine:3.20 From 0cc7c186c5c4807af79efe42c660a0fdcfd7d28a Mon Sep 17 00:00:00 2001 From: tzwang Date: Thu, 27 Jun 2024 17:44:54 +0800 Subject: [PATCH 10/11] fix staticweight bug Former-commit-id: 982092d0ae6466d1be3a5ee27bfa80aab0e812bc --- .../algorithm/weightDistributing/weightDistributing.go | 8 +++++++- api/internal/scheduler/strategy/staticWeight.go | 5 ++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/api/internal/scheduler/algorithm/weightDistributing/weightDistributing.go b/api/internal/scheduler/algorithm/weightDistributing/weightDistributing.go index b1fb7d80..ad592dc4 100644 --- a/api/internal/scheduler/algorithm/weightDistributing/weightDistributing.go +++ b/api/internal/scheduler/algorithm/weightDistributing/weightDistributing.go @@ -1,6 +1,7 @@ package weightDistributing import ( + "errors" "math" ) @@ -10,7 +11,7 @@ type Weight struct { Replica int32 } -func DistributeReplicas(weights []*Weight, replicas int32) { +func DistributeReplicas(weights []*Weight, replicas int32) error { var weightSum int32 weightSum = 0 @@ -18,6 +19,10 @@ func DistributeReplicas(weights []*Weight, replicas int32) { weightSum += w.Weight } + if weightSum == 0 { + return errors.New("static weights are empty") + } + weightRatio := make([]float64, len(weights)) for i, w := range weights { weightRatio[i] = float64(w.Weight) / float64(weightSum) @@ -69,4 +74,5 @@ func DistributeReplicas(weights []*Weight, replicas int32) { rest++ } } + return nil } diff --git a/api/internal/scheduler/strategy/staticWeight.go b/api/internal/scheduler/strategy/staticWeight.go index 8b3108e9..f106b608 100644 --- a/api/internal/scheduler/strategy/staticWeight.go +++ b/api/internal/scheduler/strategy/staticWeight.go @@ -35,7 +35,10 @@ func (s *StaticWeightStrategy) Schedule() ([]*AssignedCluster, error) { weights = append(weights, weight) } - weightDistributing.DistributeReplicas(weights, s.replicas) + err := weightDistributing.DistributeReplicas(weights, s.replicas) + if err != nil { + return nil, err + } var results []*AssignedCluster for _, weight := range weights { From 5ba09371c1cd4ed07a4d9e6ab0b41110612ec44e Mon Sep 17 00:00:00 2001 From: devad Date: Fri, 28 Jun 2024 00:48:52 +0000 Subject: [PATCH 11/11] fix bugs Former-commit-id: 4e5d6953ebc35d50323397c3da0b3bfefc206b6c --- api/Dockerfile | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/api/Dockerfile b/api/Dockerfile index 9fb6df85..80416fda 100644 --- a/api/Dockerfile +++ b/api/Dockerfile @@ -2,14 +2,14 @@ FROM registry.cn-hangzhou.aliyuncs.com/jcce-images/golang:1.22.4-alpine3.20 AS b WORKDIR /app +LABEL stage=gobuilder +ENV CGO_ENABLED 0 +ENV GOARCH amd64 +ENV GOPROXY https://goproxy.cn,direct + COPY . . - -RUN go env -w GO111MODULE=on \ -&& go env -w GOPROXY=https://goproxy.cn,direct \ -&& go env -w CGO_ENABLED=0 \ -&& go build -o pcm-coordinator-api /app/api/pcm.go - -FROM registry.cn-hangzhou.aliyuncs.com/jcce-images/alpine:3.20 +COPY api/etc/ /app/ +RUN go mod download && go build -o pcm-coordinator-api /app/api/pcm.go WORKDIR /app