Merge pull request 'add RandomStrategy' (#198) from master-wq into master

Former-commit-id: cb88296195cc15024fe0dcb9c4198692ead368c4
This commit is contained in:
qiwang 2024-05-21 19:03:20 +08:00
commit 1f28682e6c
3 changed files with 54 additions and 33 deletions

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 {