pcm-coordinator/api/internal/scheduler/strategy/staticWeight.go

51 lines
1.2 KiB
Go

package strategy
import (
"errors"
"gitlink.org.cn/JointCloud/pcm-coordinator/api/internal/scheduler/algorithm/weightDistributing"
)
type StaticWeightStrategy struct {
staticWeightMap map[string]int32
replicas int32
}
func NewStaticWeightStrategy(staticWeightMap map[string]int32, replicas int32) *StaticWeightStrategy {
return &StaticWeightStrategy{staticWeightMap: staticWeightMap,
replicas: replicas,
}
}
func (s *StaticWeightStrategy) Schedule() ([]*AssignedCluster, error) {
if s.replicas < 1 {
return nil, errors.New("replicas must be greater than 0")
}
if len(s.staticWeightMap) == 0 || s.staticWeightMap == nil {
return nil, errors.New("weight must be set")
}
weights := make([]*weightDistributing.Weight, 0)
for k, v := range s.staticWeightMap {
weight := &weightDistributing.Weight{
Id: k,
Weight: v,
}
weights = append(weights, weight)
}
err := weightDistributing.DistributeReplicas(weights, s.replicas)
if err != nil {
return nil, err
}
var results []*AssignedCluster
for _, weight := range weights {
cluster := &AssignedCluster{ClusterId: weight.Id, Replicas: weight.Replica}
results = append(results, cluster)
}
return results, nil
}