- /*
-
- Copyright (c) [2023] [pcm]
- [pcm-coordinator] is licensed under Mulan PSL v2.
- You can use this software according to the terms and conditions of the Mulan PSL v2.
- You may obtain a copy of Mulan PSL v2 at:
- http://license.coscl.org.cn/MulanPSL2
- THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
- EITHER EXPaRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
- MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
- See the Mulan PSL v2 for more details.
-
- */
-
- package schedulers
-
- import (
- "errors"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/scheduler"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/scheduler/algorithm/providerPricing"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/scheduler/entity"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/scheduler/schedulers/option"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/scheduler/service/collector"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/internal/scheduler/strategy"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/api/pkg/response"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/models"
- "gitlink.org.cn/jcce-pcm/pcm-coordinator/pkg/utils"
- )
-
- type AiScheduler struct {
- yamlString string
- task *response.TaskInfo
- *scheduler.Scheduler
- option option.AiOption
- }
-
- func NewAiScheduler(val string, scheduler *scheduler.Scheduler) (*AiScheduler, error) {
- return &AiScheduler{yamlString: val, Scheduler: scheduler}, nil
- }
-
- func (as *AiScheduler) GetNewStructForDb(task *response.TaskInfo, resource string, participantId int64) (interface{}, error) {
- ai := models.Ai{
- ParticipantId: participantId,
- TaskId: task.TaskId,
- Status: "Saved",
- YamlString: as.yamlString,
- }
- utils.Convert(task.Metadata, &ai)
- return ai, nil
- }
-
- func (as *AiScheduler) PickOptimalStrategy() (strategy.Strategy, error) {
- resources, err := as.findClustersWithResource()
- if err != nil {
- return nil, err
- }
-
- if len(resources) < 2 /*|| as.task */ {
- var pros []entity.Participant
- for _, resource := range resources {
- pros = append(pros, entity.Participant{
- Participant_id: resource.ParticipantId,
- Name: resource.Name,
- })
- }
- strategy := strategy.NewReplicationStrategy(nil, 0)
- return strategy, nil
- }
-
- task, providerList := as.genTaskAndProviders()
- if err != nil {
- return nil, nil
- }
- strategy := strategy.NewPricingStrategy(task, providerList...)
- return strategy, nil
- }
-
- func (as *AiScheduler) genTaskAndProviders() (*providerPricing.Task, []*providerPricing.Provider) {
-
- return nil, nil
- }
-
- func (as *AiScheduler) AssignTask(clusters []*strategy.AssignedCluster) error {
- if clusters == nil {
- return errors.New("clusters is nil")
- }
-
- executorMap := *as.AiExecutor
- for _, cluster := range clusters {
- _, err := executorMap[cluster.Name].Execute(option.AiOption{})
- if err != nil {
- // TODO: database operation
- }
- // TODO: database operation
- }
-
- return nil
- }
-
- func (as *AiScheduler) findClustersWithResource() ([]*collector.ResourceSpecs, error) {
- var resourceSpecs []*collector.ResourceSpecs
- for _, resourceCollector := range *as.ResourceCollector {
- spec, err := resourceCollector.GetResourceSpecs()
- if err != nil {
- continue
- }
- resourceSpecs = append(resourceSpecs, spec)
- }
- if len(resourceSpecs) == 0 {
- return nil, errors.New("no resource found")
- }
- return resourceSpecs, nil
- }
|