forked from JointCloud/pcm-coordinator
84 lines
2.0 KiB
Go
84 lines
2.0 KiB
Go
package scheduler
|
|
|
|
import (
|
|
"encoding/json"
|
|
"github.com/pkg/errors"
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/algo"
|
|
"gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/types"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type scheduler struct {
|
|
task *types.TaskInfo
|
|
participantIds []int64
|
|
scheduleService scheduleService
|
|
}
|
|
|
|
func NewScheduler(scheduleService scheduleService, val string) (*scheduler, error) {
|
|
var task *types.TaskInfo
|
|
err := json.Unmarshal([]byte(val), &task)
|
|
if err != nil {
|
|
return nil, errors.New("create scheduler failed : " + err.Error())
|
|
}
|
|
return &scheduler{task: task, scheduleService: scheduleService}, nil
|
|
}
|
|
|
|
func (s *scheduler) MatchLabels(dbEngin *gorm.DB) {
|
|
// 已指定 ParticipantId
|
|
if s.task.ParticipantId != 0 {
|
|
return
|
|
}
|
|
|
|
var ids []int64
|
|
count := 0
|
|
for key := range s.task.MatchLabels {
|
|
var participantIds []int64
|
|
dbEngin.Raw("select participant_id from sc_participant_label_info where `key` = ? and value = ?", key, s.task.MatchLabels[key]).Scan(&participantIds)
|
|
if count == 0 {
|
|
ids = participantIds
|
|
}
|
|
//if len(participantId) == 0 || len(ids) == 0 {
|
|
// return nil, nil
|
|
//}
|
|
ids = intersect(ids, participantIds)
|
|
count++
|
|
}
|
|
s.participantIds = micsSlice(ids, 1)
|
|
}
|
|
|
|
func (s *scheduler) AssignAndSchedule() error {
|
|
task, providerList := s.genTaskAndProviders()
|
|
|
|
strategy, err := s.scheduleService.pickOptimalStrategy(task, providerList...)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
if strategy == nil {
|
|
s.task.ParticipantId = s.participantIds[0]
|
|
}
|
|
|
|
return nil
|
|
}
|
|
|
|
func (s *scheduler) SaveToDb(dbEngin *gorm.DB) error {
|
|
if s.task.ParticipantId == 0 {
|
|
return errors.New("participantId 为空")
|
|
}
|
|
structForDb, err := s.scheduleService.getNewStructForDb(s.task, s.task.ParticipantId)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tx := dbEngin.Create(structForDb)
|
|
if tx.Error != nil {
|
|
logx.Error(tx.Error)
|
|
return tx.Error
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (s *scheduler) genTaskAndProviders() (*algo.Task, []*algo.Provider) {
|
|
return nil, nil
|
|
}
|