Merge pull request 'fix staticweight bugs' (#256) from tzwang/pcm-coordinator:master into master

Former-commit-id: 7ccd2fcf78a4e019a62ac658f0a4ed2cdafda8cb
This commit is contained in:
tzwang 2024-06-27 17:45:52 +08:00
commit 55d7906115
2 changed files with 11 additions and 2 deletions

View File

@ -1,6 +1,7 @@
package weightDistributing
import (
"errors"
"math"
)
@ -10,7 +11,7 @@ type Weight struct {
Replica int32
}
func DistributeReplicas(weights []*Weight, replicas int32) {
func DistributeReplicas(weights []*Weight, replicas int32) error {
var weightSum int32
weightSum = 0
@ -18,6 +19,10 @@ func DistributeReplicas(weights []*Weight, replicas int32) {
weightSum += w.Weight
}
if weightSum == 0 {
return errors.New("static weights are empty")
}
weightRatio := make([]float64, len(weights))
for i, w := range weights {
weightRatio[i] = float64(w.Weight) / float64(weightSum)
@ -69,4 +74,5 @@ func DistributeReplicas(weights []*Weight, replicas int32) {
rest++
}
}
return nil
}

View File

@ -35,7 +35,10 @@ func (s *StaticWeightStrategy) Schedule() ([]*AssignedCluster, error) {
weights = append(weights, weight)
}
weightDistributing.DistributeReplicas(weights, s.replicas)
err := weightDistributing.DistributeReplicas(weights, s.replicas)
if err != nil {
return nil, err
}
var results []*AssignedCluster
for _, weight := range weights {