diff --git a/api/internal/scheduler/schedulers/aiScheduler.go b/api/internal/scheduler/schedulers/aiScheduler.go index 50cf27ca..5c8daacc 100644 --- a/api/internal/scheduler/schedulers/aiScheduler.go +++ b/api/internal/scheduler/schedulers/aiScheduler.go @@ -107,6 +107,8 @@ func (as *AiScheduler) PickOptimalStrategy() (strategy.Strategy, error) { //todo resources should match cluster StaticWeightMap strategy := strategy.NewStaticWeightStrategy(as.option.ClusterToStaticWeight, as.option.Replica) return strategy, nil + case strategy.RANDOM: + } return nil, errors.New("no strategy has been chosen") diff --git a/api/internal/scheduler/strategy/random.go b/api/internal/scheduler/strategy/random.go new file mode 100644 index 00000000..b030bafd --- /dev/null +++ b/api/internal/scheduler/strategy/random.go @@ -0,0 +1,10 @@ +package strategy + +/*type RandomStrategy struct { + clusterIds []string + replicas int32 +} + +func NewRandomStrategy(clusterIds []string, replicas int32) *NewRandomStrategy { + return &RandomStrategy{clusterIds: clusterIds, replicas: replicas} +}*/ diff --git a/api/internal/scheduler/strategy/strategy.go b/api/internal/scheduler/strategy/strategy.go index 59d6cb17..91cd4d4d 100644 --- a/api/internal/scheduler/strategy/strategy.go +++ b/api/internal/scheduler/strategy/strategy.go @@ -5,6 +5,7 @@ const ( RESOURCES_PRICING = "resourcesPricing" STATIC_WEIGHT = "staticWeight" DYNAMIC_RESOURCES = "dynamicResources" + RANDOM = "random" DATA_LOCALITY = "dataLocality" //感知数据位置,数据调度和计算调度协同,近数据调度 ENERGY_CONSUMPTION = "energyConsumption" //根据各集群总体能耗水平调度作业,优先选择能耗低的集群调度作业 ) diff --git a/api/internal/scheduler/strategy/test/strategy_test.go b/api/internal/scheduler/strategy/test/strategy_test.go index 376e93c9..8802ffa2 100644 --- a/api/internal/scheduler/strategy/test/strategy_test.go +++ b/api/internal/scheduler/strategy/test/strategy_test.go @@ -5,7 +5,9 @@ import ( "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/entity" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/service/collector" "gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/strategy" + "math/rand" "testing" + "time" ) func TestReplication(t *testing.T) { @@ -106,3 +108,144 @@ func TestStaticWeight(t *testing.T) { }) } } + +func TestRandom(t *testing.T) { + // 使用当前时间作为随机数种子,确保每次程序运行产生的随机数序列都不同 + rand.Seed(time.Now().UnixNano()) + + /*randomNum := randInt(1, 100) + fmt.Println("Random number:", randomNum)*/ + total := 5 // 假设总数是5 + first, second := splitIntoTwoRandomParts(total) + fmt.Printf("第一部分的数量: %d, 第二部分的数量: %d\n", first, second) +} + +// randInt 生成一个指定范围内的随机整数,包括min但不包括max +func randInt(min, max int) int { + return min + rand.Intn(max-min) +} + +func splitIntoTwoRandomParts(total int) (int, int) { + if total < 2 { + // 如果总数小于2,则无法分成两部分 + return 0, 0 + } + // 生成一个随机数作为第一部分的数量(范围在[1, total-1]之间) + firstPart := rand.Intn(total-1) + 1 + // 第二部分的数量就是总数减去第一部分的数量 + secondPart := total - firstPart + return firstPart, secondPart +} + +func splitIntoRandomParts(total int) (int, int) { + if total < 2 { + // 如果总数小于2,则无法分成两部分 + return 0, 0 + } + // 生成一个随机数作为第一部分的数量(范围在[1, total-1]之间) + firstPart := rand.Intn(total-1) + 1 + // 第二部分的数量就是总数减去第一部分的数量 + secondPart := total - firstPart + return firstPart, secondPart +} + +func TestRandoms(t *testing.T) { + // 使用当前时间作为随机数种子,确保每次程序运行产生的随机数序列都不同 + rand.Seed(time.Now().UnixNano()) + + /*randomNum := randInt(1, 100) + fmt.Println("Random number:", randomNum)*/ + total := 10 // 假设总数是5 + parts := splitRandomParts(total) + fmt.Println("分配结果:", parts) +} + +// splitIntoRandomParts 将总数total随机分成多个部分,并返回这些部分的切片 +func splitRandomParts(total int) []int { + if total < 2 { + // 如果总数小于2,则无法分成多个部分 + return []int{total} + } + + // 创建一个切片来保存每个部分的数量 + var parts []int + + // 剩余要分配的副本数 + remaining := total + + // 随机决定要分成的部分数量(至少2个部分) + numParts := rand.Intn(total-1) + 2 + + // 确保每个部分至少获得1个副本 + for i := 0; i < numParts-1; i++ { + // 生成一个随机数(1到剩余副本数之间) + // 为了避免最后一个部分太小,我们可能需要调整随机数范围 + minPartSize := 1 + if remaining <= numParts-i { + // 如果剩余副本数不足以让每个部分都至少获得1个,则调整最小部分大小 + minPartSize = remaining / (numParts - i) + if remaining%(numParts-i) > 0 { + minPartSize++ + } + } + // 生成一个大于等于minPartSize且小于等于remaining的随机数 + partSize := minPartSize + rand.Intn(remaining-minPartSize+1) + parts = append(parts, partSize) + remaining -= partSize + } + + // 最后一个部分的数量就是剩余的副本数 + parts = append(parts, remaining) + + return parts +} + +func TestNumRandom(t *testing.T) { + total := 10 // 假设副本数是10 + numParts := 3 // 假设要分成5个集群 + + parts, err := splitIntoParts(total, numParts) + if err != nil { + fmt.Println("Error:", err) + return + } + fmt.Println("分配结果:", parts) +} + +// splitIntoParts 将总数total随机分成numParts个部分,并返回这些部分的切片 +func splitIntoParts(total int, numParts int) ([]int, error) { + if total < 1 || numParts < 1 { + // 总数或部分数量不能小于1 + return nil, fmt.Errorf("total and numParts must be greater than 0") + } + if numParts > total { + // 部分数量不能大于总数 + return nil, fmt.Errorf("numParts cannot be greater than total") + } + + // 创建一个切片来保存每个部分的数量 + parts := make([]int, numParts) + + // 首先将每个部分都分配至少一个副本 + for i := range parts { + parts[i] = 1 + total-- + } + + // 剩余要分配的副本数 + remaining := total + + // 随机分配剩余的副本 + for remaining > 0 { + // 随机选择一个部分(索引从0到numParts-1) + partIndex := rand.Intn(numParts) + + // 如果该部分加上一个副本后不会超过总数,则分配一个副本 + if parts[partIndex]+1 <= total { + parts[partIndex]++ + remaining-- + } + } + + return parts, nil +}