From b86bd44f0c533c908013db2a48dfd8b8ddeb3230 Mon Sep 17 00:00:00 2001 From: tzwang Date: Wed, 23 Aug 2023 16:57:52 +0800 Subject: [PATCH] =?UTF-8?q?=E8=B0=83=E5=BA=A6=E7=BB=93=E6=9E=84=E4=BF=AE?= =?UTF-8?q?=E6=94=B93?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Former-commit-id: 8242a201cd8b7a4a7e9fc7320288f8585907bb6a --- .../algo/{pcmStrategy.go => k8sStrategy.go} | 8 ++-- api/internal/pkg/scheduler/aiScheduler.go | 5 +++ api/internal/pkg/scheduler/cloudScheduler.go | 10 +++++ api/internal/pkg/scheduler/commonScheduler.go | 37 ++++++++++--------- api/internal/pkg/scheduler/hpcScheduler.go | 5 +++ api/internal/pkg/scheduler/scheduler.go | 6 ++- 6 files changed, 48 insertions(+), 23 deletions(-) rename api/internal/algo/{pcmStrategy.go => k8sStrategy.go} (97%) diff --git a/api/internal/algo/pcmStrategy.go b/api/internal/algo/k8sStrategy.go similarity index 97% rename from api/internal/algo/pcmStrategy.go rename to api/internal/algo/k8sStrategy.go index d4396e61..71efefc7 100644 --- a/api/internal/algo/pcmStrategy.go +++ b/api/internal/algo/k8sStrategy.go @@ -6,13 +6,13 @@ import ( "math" ) -type pcmStrategy struct { +type k8sStrategy struct { ProviderList []*Provider Task *Task StrategyList []*Strategy } -func NewPcmStrategy(task *Task, providers ...*Provider) *pcmStrategy { +func NewK8sStrategy(task *Task, providers ...*Provider) *k8sStrategy { var providerList []*Provider var res [][]int @@ -43,10 +43,10 @@ func NewPcmStrategy(task *Task, providers ...*Provider) *pcmStrategy { strategyList = append(strategyList, strategy) } - return &pcmStrategy{ProviderList: providerList, Task: task, StrategyList: strategyList} + return &k8sStrategy{ProviderList: providerList, Task: task, StrategyList: strategyList} } -func (ps pcmStrategy) computeMaxScore() (*Task, error) { +func (ps k8sStrategy) computeMaxScore() (*Task, error) { maxStrategy := NewStrategy() var maxprofit float64 diff --git a/api/internal/pkg/scheduler/aiScheduler.go b/api/internal/pkg/scheduler/aiScheduler.go index bbd817f5..2b181662 100644 --- a/api/internal/pkg/scheduler/aiScheduler.go +++ b/api/internal/pkg/scheduler/aiScheduler.go @@ -1,6 +1,7 @@ package scheduler import ( + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/algo" "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" "gitlink.org.cn/jcce-pcm/pcm-coordinator/model" "gitlink.org.cn/jcce-pcm/utils/tool" @@ -24,3 +25,7 @@ func (cs aiScheduler) getNewStructForDb(task *types.TaskInfo, participantIds []i tool.Convert(task.Metadata, &ai) return ai, nil } + +func (cs aiScheduler) pickOptimalStrategy(task *algo.Task, providers ...*algo.Provider) (*algo.Task, error) { + return nil, nil +} diff --git a/api/internal/pkg/scheduler/cloudScheduler.go b/api/internal/pkg/scheduler/cloudScheduler.go index 81477de2..4b2c208d 100644 --- a/api/internal/pkg/scheduler/cloudScheduler.go +++ b/api/internal/pkg/scheduler/cloudScheduler.go @@ -3,6 +3,7 @@ package scheduler import ( "bytes" "encoding/json" + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/algo" "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" "gitlink.org.cn/jcce-pcm/pcm-coordinator/model" "io" @@ -19,6 +20,15 @@ func NewCloudScheduler() *cloudScheduler { return &cloudScheduler{} } +func (cs cloudScheduler) pickOptimalStrategy(task *algo.Task, providers ...*algo.Provider) (*algo.Task, error) { + strategy := algo.NewK8sStrategy(task, providers...) + taskResult, err := algo.ScheduleWithFullCollaboration(strategy, strategy.ProviderList) + if err != nil { + return nil, err + } + return taskResult, nil +} + func (cs cloudScheduler) getNewStructForDb(task *types.TaskInfo, participantIds []int64) (interface{}, error) { bytes, err := json.Marshal(task.Metadata) if err != nil { diff --git a/api/internal/pkg/scheduler/commonScheduler.go b/api/internal/pkg/scheduler/commonScheduler.go index 0da9022e..30126a34 100644 --- a/api/internal/pkg/scheduler/commonScheduler.go +++ b/api/internal/pkg/scheduler/commonScheduler.go @@ -1,33 +1,34 @@ package scheduler import ( + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/algo" "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" - "gorm.io/gorm" "math/rand" "time" ) type scheduleService interface { getNewStructForDb(task *types.TaskInfo, participantIds []int64) (interface{}, error) + pickOptimalStrategy(task *algo.Task, providers ...*algo.Provider) (*algo.Task, error) } -func MatchLabels(dbEngin *gorm.DB, task *types.TaskInfo) ([]int64, error) { - var ids []int64 - count := 0 - for key := range task.MatchLabels { - var participantId []int64 - dbEngin.Raw("select participant_id from sc_participant_label_info where `key` = ? and value = ?", key, task.MatchLabels[key]).Scan(&participantId) - if count == 0 { - ids = participantId - } - if len(participantId) == 0 || len(ids) == 0 { - return nil, nil - } - ids = intersect(ids, participantId) - count++ - } - return micsSlice(ids, 1), nil -} +//func MatchLabels(dbEngin *gorm.DB, task *types.TaskInfo) ([]int64, error) { +// var ids []int64 +// count := 0 +// for key := range task.MatchLabels { +// var participantId []int64 +// dbEngin.Raw("select participant_id from sc_participant_label_info where `key` = ? and value = ?", key, task.MatchLabels[key]).Scan(&participantId) +// if count == 0 { +// ids = participantId +// } +// if len(participantId) == 0 || len(ids) == 0 { +// return nil, nil +// } +// ids = intersect(ids, participantId) +// count++ +// } +// return micsSlice(ids, 1), nil +//} // 求交集 func intersect(slice1, slice2 []int64) []int64 { diff --git a/api/internal/pkg/scheduler/hpcScheduler.go b/api/internal/pkg/scheduler/hpcScheduler.go index c1dba3cd..15443e0d 100644 --- a/api/internal/pkg/scheduler/hpcScheduler.go +++ b/api/internal/pkg/scheduler/hpcScheduler.go @@ -1,6 +1,7 @@ package scheduler import ( + "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/algo" "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types" "gitlink.org.cn/jcce-pcm/pcm-coordinator/model" "gitlink.org.cn/jcce-pcm/utils/tool" @@ -24,3 +25,7 @@ func (h hpcScheduler) getNewStructForDb(task *types.TaskInfo, participantIds []i tool.Convert(task.Metadata, &hpc) return hpc, nil } + +func (cs hpcScheduler) pickOptimalStrategy(task *algo.Task, providers ...*algo.Provider) (*algo.Task, error) { + return nil, nil +} diff --git a/api/internal/pkg/scheduler/scheduler.go b/api/internal/pkg/scheduler/scheduler.go index 4c953239..17ede9ec 100644 --- a/api/internal/pkg/scheduler/scheduler.go +++ b/api/internal/pkg/scheduler/scheduler.go @@ -47,11 +47,15 @@ func (s scheduler) MatchLabels(dbEngin *gorm.DB) { s.participantIds = micsSlice(ids, 1) } +func (s scheduler) AssignAndSchedule() { + +} + func (s scheduler) SaveToDb(dbEngin *gorm.DB) error { if len(s.participantIds) == 0 { return errors.New("participantIds 为空") } - structForDb, err := s.scheduleService().getNewStructForDb(s.task, s.participantIds) + structForDb, err := s.scheduleService.getNewStructForDb(s.task, s.participantIds) if err != nil { return err }