fix staticweight bug

Former-commit-id: 982092d0ae6466d1be3a5ee27bfa80aab0e812bc
This commit is contained in:
tzwang 2024-06-27 17:44:54 +08:00
parent f823538feb
commit 0cc7c186c5
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 {