From 7e8670492fb5bcfa7d72f868a67e15466e3f8607 Mon Sep 17 00:00:00 2001 From: qiwang <1364512070@qq.com> Date: Tue, 21 May 2024 18:56:18 +0800 Subject: [PATCH] fix: add ai Random RandomStrategy Former-commit-id: 005e2279af52f6e00335612e5677d426df41ea9a --- .../scheduler/schedulers/aiScheduler.go | 47 ++++++------ .../scheduler/schedulers/vmScheduler.go | 10 +-- api/internal/scheduler/strategy/random.go | 75 +++++++++++++------ api/internal/scheduler/strategy/strategy.go | 2 +- 4 files changed, 77 insertions(+), 57 deletions(-) diff --git a/api/internal/scheduler/schedulers/aiScheduler.go b/api/internal/scheduler/schedulers/aiScheduler.go index 79e11a0d..5a98a8d6 100644 --- a/api/internal/scheduler/schedulers/aiScheduler.go +++ b/api/internal/scheduler/schedulers/aiScheduler.go @@ -26,7 +26,6 @@ import ( "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/schedulers/option" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/service/collector" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/strategy" - "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/strategy/param" "gitlink.org.cn/JointCloud/pcm-coordinator/api/pkg/response" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" @@ -90,38 +89,38 @@ func (as *AiScheduler) PickOptimalStrategy() (strategy.Strategy, error) { return &strategy.SingleAssignment{Cluster: &strategy.AssignedCluster{ClusterId: as.option.ClusterIds[0], Replicas: 1}}, nil } - resources, err := as.findClustersWithResources() + //resources, err := as.findClustersWithResources() - if err != nil { - return nil, err - } - if len(resources) == 0 { - return nil, errors.New("no cluster has resources") - } + /* if err != nil { + return nil, err + } + if len(resources) == 0 { + return nil, errors.New("no cluster has resources") + } - if len(resources) == 1 { - var cluster strategy.AssignedCluster - cluster.ClusterId = resources[0].ClusterId - cluster.Replicas = 1 - return &strategy.SingleAssignment{Cluster: &cluster}, nil - } + if len(resources) == 1 { + var cluster strategy.AssignedCluster + cluster.ClusterId = resources[0].ClusterId + cluster.Replicas = 1 + return &strategy.SingleAssignment{Cluster: &cluster}, nil + } - params := ¶m.Params{Resources: resources} + params := ¶m.Params{Resources: resources}*/ switch as.option.StrategyName { - case strategy.REPLICATION: - var clusterIds []string - for _, resource := range resources { - clusterIds = append(clusterIds, resource.ClusterId) - } - strategy := strategy.NewReplicationStrategy(clusterIds, 1) - return strategy, nil - case strategy.RESOURCES_PRICING: + /*case strategy.REPLICATION: + var clusterIds []string + for _, resource := range resources { + clusterIds = append(clusterIds, resource.ClusterId) + } + strategy := strategy.NewReplicationStrategy(clusterIds, 1) + return strategy, nil*/ + /*case strategy.RESOURCES_PRICING: strategy := strategy.NewPricingStrategy(¶m.ResourcePricingParams{Params: params, Replicas: 1}) return strategy, nil case strategy.DYNAMIC_RESOURCES: strategy := strategy.NewDynamicResourcesStrategy(params.Resources, as.option, 1) - return strategy, nil + return strategy, nil*/ case strategy.STATIC_WEIGHT: //todo resources should match cluster StaticWeightMap strategy := strategy.NewStaticWeightStrategy(as.option.ClusterToStaticWeight, as.option.Replica) diff --git a/api/internal/scheduler/schedulers/vmScheduler.go b/api/internal/scheduler/schedulers/vmScheduler.go index 89d52a4d..d4c3de4e 100644 --- a/api/internal/scheduler/schedulers/vmScheduler.go +++ b/api/internal/scheduler/schedulers/vmScheduler.go @@ -8,6 +8,7 @@ import ( "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/database" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/schedulers/option" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/strategy" + "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/svc" "gitlink.org.cn/JointCloud/pcm-coordinator/api/pkg/response" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants" "gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models" @@ -25,6 +26,7 @@ type VmScheduler struct { ctx context.Context promClient tracker.Prometheus dbEngin *gorm.DB + svcCtx *svc.ServiceContext } type VmResult struct { @@ -40,10 +42,6 @@ func NewVmScheduler(ctx context.Context, val string, scheduler *scheduler.Schedu return &VmScheduler{ctx: ctx, yamlString: val, Scheduler: scheduler, option: option, dbEngin: dbEngin, promClient: promClient}, nil } -/*func NewCloudScheduler(ctx context.Context, val string, scheduler *scheduler.Scheduler, option *option.CloudOption, dbEngin *gorm.DB, promClient tracker.Prometheus) (*CloudScheduler, error) { - return &CloudScheduler{ctx: ctx, yamlString: val, Scheduler: scheduler, option: option, dbEngin: dbEngin, promClient: promClient}, nil -}*/ - func (vm *VmScheduler) PickOptimalStrategy() (strategy.Strategy, error) { if len(vm.option.ClusterIds) == 1 { // TODO database operation Find @@ -102,10 +100,6 @@ func (v *VmScheduler) GetNewStructForDb(task *response.TaskInfo, resource string vm.Status = constants.Saved vm.ParticipantId = participantId return vm, nil - //vm.YamlString =v.yamlString - /* vm. = utils.GenSnowflakeID() - vm.NsID = task.NsID - vm.ParticipantId = participantId*/ } func (vm *VmScheduler) genTaskAndProviders() (*providerPricing.Task, []*providerPricing.Provider, error) { diff --git a/api/internal/scheduler/strategy/random.go b/api/internal/scheduler/strategy/random.go index 315c0b3f..09e21a0d 100644 --- a/api/internal/scheduler/strategy/random.go +++ b/api/internal/scheduler/strategy/random.go @@ -17,7 +17,7 @@ func NewRandomStrategy(clusterIds []string, replicas int32) *RandomStrategy { } func (s *RandomStrategy) Schedule() ([]*AssignedCluster, error) { - + var results []*AssignedCluster if s.replicas < 1 { return nil, errors.New("replicas must be greater than 0") } @@ -30,37 +30,64 @@ func (s *RandomStrategy) Schedule() ([]*AssignedCluster, error) { return nil, errors.New("weight must be set") } - // 创建一个切片来保存每个部分的数量 - parts := make([]int32, len(s.clusterIds)) + if s.replicas == 1 { - // 首先将每个部分都分配至少一个副本 - for i := range parts { - parts[i] = 1 - s.replicas-- - } + // 创建一个切片来保存每个部分的数量 + parts := make([]int32, len(s.clusterIds)) + // 剩余要分配的副本数 + remaining := s.replicas - // 剩余要分配的副本数 - remaining := s.replicas + // 随机分配剩余的副本 + for remaining > 0 { + // 随机选择一个部分(索引从0到numParts-1) + partIndex := rand.Intn(len(s.clusterIds)) - // 随机分配剩余的副本 - for remaining > 0 { - // 随机选择一个部分(索引从0到numParts-1) - partIndex := rand.Intn(len(s.clusterIds)) - - // 如果该部分加上一个副本后不会超过总数,则分配一个副本 - if parts[partIndex]+1 <= s.replicas { + // 如果该部分加上一个副本后不会超过总数,则分配一个副本 + //if parts[partIndex]+1 <= s.replicas { parts[partIndex]++ remaining-- + //} } - } - var results []*AssignedCluster - if len(s.clusterIds) == len(parts) { - for i, key := range s.clusterIds { - cluster := &AssignedCluster{ClusterId: key, Replicas: parts[i]} - results = append(results, cluster) + if len(s.clusterIds) == len(parts) { + for i, key := range s.clusterIds { + cluster := &AssignedCluster{ClusterId: key, Replicas: parts[i]} + results = append(results, cluster) + } } + + } else { + // 创建一个切片来保存每个部分的数量 + parts := make([]int32, len(s.clusterIds)) + + // 首先将每个部分都分配至少一个副本 + for i := range parts { + parts[i] = 1 + s.replicas-- + } + + // 剩余要分配的副本数 + remaining := s.replicas + + // 随机分配剩余的副本 + for remaining > 0 { + // 随机选择一个部分(索引从0到numParts-1) + partIndex := rand.Intn(len(s.clusterIds)) + + // 如果该部分加上一个副本后不会超过总数,则分配一个副本 + //if parts[partIndex]+1 <= s.replicas { + parts[partIndex]++ + remaining-- + //} + } + + if len(s.clusterIds) == len(parts) { + for i, key := range s.clusterIds { + cluster := &AssignedCluster{ClusterId: key, Replicas: parts[i]} + results = append(results, cluster) + } + } + } return results, nil - } diff --git a/api/internal/scheduler/strategy/strategy.go b/api/internal/scheduler/strategy/strategy.go index 91cd4d4d..85a242f3 100644 --- a/api/internal/scheduler/strategy/strategy.go +++ b/api/internal/scheduler/strategy/strategy.go @@ -11,7 +11,7 @@ const ( ) var ( - strategyNames = []string{REPLICATION, RESOURCES_PRICING, STATIC_WEIGHT, DYNAMIC_RESOURCES} + strategyNames = []string{REPLICATION, RESOURCES_PRICING, STATIC_WEIGHT, DYNAMIC_RESOURCES, RANDOM} ) type Strategy interface {