fix: add ai Random RandomStrategy

Former-commit-id: 005e2279af52f6e00335612e5677d426df41ea9a
This commit is contained in:
qiwang 2024-05-21 18:56:18 +08:00
parent fa1390fed2
commit 7e8670492f
4 changed files with 77 additions and 57 deletions

View File

@ -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 := &param.Params{Resources: resources}
params := &param.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(&param.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)

View File

@ -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) {

View File

@ -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
}

View File

@ -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 {