fix: add ai Random RandomStrategy
Former-commit-id: 005e2279af52f6e00335612e5677d426df41ea9a
This commit is contained in:
parent
fa1390fed2
commit
7e8670492f
|
@ -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/schedulers/option"
|
||||||
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/service/collector"
|
"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"
|
||||||
"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/api/pkg/response"
|
||||||
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
|
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
|
||||||
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
|
"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
|
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 {
|
/* if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
if len(resources) == 0 {
|
if len(resources) == 0 {
|
||||||
return nil, errors.New("no cluster has resources")
|
return nil, errors.New("no cluster has resources")
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(resources) == 1 {
|
if len(resources) == 1 {
|
||||||
var cluster strategy.AssignedCluster
|
var cluster strategy.AssignedCluster
|
||||||
cluster.ClusterId = resources[0].ClusterId
|
cluster.ClusterId = resources[0].ClusterId
|
||||||
cluster.Replicas = 1
|
cluster.Replicas = 1
|
||||||
return &strategy.SingleAssignment{Cluster: &cluster}, nil
|
return &strategy.SingleAssignment{Cluster: &cluster}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
params := ¶m.Params{Resources: resources}
|
params := ¶m.Params{Resources: resources}*/
|
||||||
|
|
||||||
switch as.option.StrategyName {
|
switch as.option.StrategyName {
|
||||||
case strategy.REPLICATION:
|
/*case strategy.REPLICATION:
|
||||||
var clusterIds []string
|
var clusterIds []string
|
||||||
for _, resource := range resources {
|
for _, resource := range resources {
|
||||||
clusterIds = append(clusterIds, resource.ClusterId)
|
clusterIds = append(clusterIds, resource.ClusterId)
|
||||||
}
|
}
|
||||||
strategy := strategy.NewReplicationStrategy(clusterIds, 1)
|
strategy := strategy.NewReplicationStrategy(clusterIds, 1)
|
||||||
return strategy, nil
|
return strategy, nil*/
|
||||||
case strategy.RESOURCES_PRICING:
|
/*case strategy.RESOURCES_PRICING:
|
||||||
strategy := strategy.NewPricingStrategy(¶m.ResourcePricingParams{Params: params, Replicas: 1})
|
strategy := strategy.NewPricingStrategy(¶m.ResourcePricingParams{Params: params, Replicas: 1})
|
||||||
return strategy, nil
|
return strategy, nil
|
||||||
case strategy.DYNAMIC_RESOURCES:
|
case strategy.DYNAMIC_RESOURCES:
|
||||||
strategy := strategy.NewDynamicResourcesStrategy(params.Resources, as.option, 1)
|
strategy := strategy.NewDynamicResourcesStrategy(params.Resources, as.option, 1)
|
||||||
return strategy, nil
|
return strategy, nil*/
|
||||||
case strategy.STATIC_WEIGHT:
|
case strategy.STATIC_WEIGHT:
|
||||||
//todo resources should match cluster StaticWeightMap
|
//todo resources should match cluster StaticWeightMap
|
||||||
strategy := strategy.NewStaticWeightStrategy(as.option.ClusterToStaticWeight, as.option.Replica)
|
strategy := strategy.NewStaticWeightStrategy(as.option.ClusterToStaticWeight, as.option.Replica)
|
||||||
|
|
|
@ -8,6 +8,7 @@ import (
|
||||||
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/database"
|
"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/schedulers/option"
|
||||||
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/strategy"
|
"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/api/pkg/response"
|
||||||
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
|
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/constants"
|
||||||
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
|
"gitlink.org.cn/JointCloud/pcm-coordinator/pkg/models"
|
||||||
|
@ -25,6 +26,7 @@ type VmScheduler struct {
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
promClient tracker.Prometheus
|
promClient tracker.Prometheus
|
||||||
dbEngin *gorm.DB
|
dbEngin *gorm.DB
|
||||||
|
svcCtx *svc.ServiceContext
|
||||||
}
|
}
|
||||||
|
|
||||||
type VmResult struct {
|
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
|
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) {
|
func (vm *VmScheduler) PickOptimalStrategy() (strategy.Strategy, error) {
|
||||||
if len(vm.option.ClusterIds) == 1 {
|
if len(vm.option.ClusterIds) == 1 {
|
||||||
// TODO database operation Find
|
// TODO database operation Find
|
||||||
|
@ -102,10 +100,6 @@ func (v *VmScheduler) GetNewStructForDb(task *response.TaskInfo, resource string
|
||||||
vm.Status = constants.Saved
|
vm.Status = constants.Saved
|
||||||
vm.ParticipantId = participantId
|
vm.ParticipantId = participantId
|
||||||
return vm, nil
|
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) {
|
func (vm *VmScheduler) genTaskAndProviders() (*providerPricing.Task, []*providerPricing.Provider, error) {
|
||||||
|
|
|
@ -17,7 +17,7 @@ func NewRandomStrategy(clusterIds []string, replicas int32) *RandomStrategy {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *RandomStrategy) Schedule() ([]*AssignedCluster, error) {
|
func (s *RandomStrategy) Schedule() ([]*AssignedCluster, error) {
|
||||||
|
var results []*AssignedCluster
|
||||||
if s.replicas < 1 {
|
if s.replicas < 1 {
|
||||||
return nil, errors.New("replicas must be greater than 0")
|
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")
|
return nil, errors.New("weight must be set")
|
||||||
}
|
}
|
||||||
|
|
||||||
// 创建一个切片来保存每个部分的数量
|
if s.replicas == 1 {
|
||||||
parts := make([]int32, len(s.clusterIds))
|
|
||||||
|
|
||||||
// 首先将每个部分都分配至少一个副本
|
// 创建一个切片来保存每个部分的数量
|
||||||
for i := range parts {
|
parts := make([]int32, len(s.clusterIds))
|
||||||
parts[i] = 1
|
// 剩余要分配的副本数
|
||||||
s.replicas--
|
remaining := s.replicas
|
||||||
}
|
|
||||||
|
|
||||||
// 剩余要分配的副本数
|
// 随机分配剩余的副本
|
||||||
remaining := s.replicas
|
for remaining > 0 {
|
||||||
|
// 随机选择一个部分(索引从0到numParts-1)
|
||||||
|
partIndex := rand.Intn(len(s.clusterIds))
|
||||||
|
|
||||||
// 随机分配剩余的副本
|
// 如果该部分加上一个副本后不会超过总数,则分配一个副本
|
||||||
for remaining > 0 {
|
//if parts[partIndex]+1 <= s.replicas {
|
||||||
// 随机选择一个部分(索引从0到numParts-1)
|
|
||||||
partIndex := rand.Intn(len(s.clusterIds))
|
|
||||||
|
|
||||||
// 如果该部分加上一个副本后不会超过总数,则分配一个副本
|
|
||||||
if parts[partIndex]+1 <= s.replicas {
|
|
||||||
parts[partIndex]++
|
parts[partIndex]++
|
||||||
remaining--
|
remaining--
|
||||||
|
//}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
var results []*AssignedCluster
|
if len(s.clusterIds) == len(parts) {
|
||||||
if len(s.clusterIds) == len(parts) {
|
for i, key := range s.clusterIds {
|
||||||
for i, key := range s.clusterIds {
|
cluster := &AssignedCluster{ClusterId: key, Replicas: parts[i]}
|
||||||
cluster := &AssignedCluster{ClusterId: key, Replicas: parts[i]}
|
results = append(results, cluster)
|
||||||
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
|
return results, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
strategyNames = []string{REPLICATION, RESOURCES_PRICING, STATIC_WEIGHT, DYNAMIC_RESOURCES}
|
strategyNames = []string{REPLICATION, RESOURCES_PRICING, STATIC_WEIGHT, DYNAMIC_RESOURCES, RANDOM}
|
||||||
)
|
)
|
||||||
|
|
||||||
type Strategy interface {
|
type Strategy interface {
|
||||||
|
|
Loading…
Reference in New Issue