Skip to content

Commit

Permalink
scheduler: add check to prohibit returning inf during spread boost ca…
Browse files Browse the repository at this point in the history
…lculation
  • Loading branch information
nickethier committed May 15, 2019
1 parent 315beb6 commit ea843a5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 1 deletion.
6 changes: 5 additions & 1 deletion scheduler/spread.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ func evenSpreadScoreBoost(pset *propertySet, option *structs.Node) float64 {
for _, value := range combinedUseMap {
if minCount == 0 || value < minCount {
minCount = value
}

if maxCount == 0 || value > maxCount {
maxCount = value
}
Expand All @@ -214,7 +214,11 @@ func evenSpreadScoreBoost(pset *propertySet, option *structs.Node) float64 {
} else if minCount == maxCount {
// Maximum possible penalty when the distribution is even
return -1.0
} else if minCount == 0 {
// Maximum possible boost
return 1.0
}

// Penalty based on delta from max value
delta := int(maxCount - minCount)
deltaBoost = float64(delta) / float64(minCount)
Expand Down
24 changes: 24 additions & 0 deletions scheduler/spread_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package scheduler

import (
"math"
"testing"

"fmt"
Expand Down Expand Up @@ -311,6 +312,7 @@ func TestSpreadIterator_EvenSpread(t *testing.T) {
}
for _, rn := range out {
require.Equal(t, fmt.Sprintf("%.3f", expectedScores[rn.Node.Datacenter]), fmt.Sprintf("%.3f", rn.FinalScore))

}

// Update the plan to add allocs to nodes in dc1
Expand Down Expand Up @@ -543,3 +545,25 @@ func TestSpreadIterator_MaxPenalty(t *testing.T) {
}

}

func Test_evenSpreadScoreBoost(t *testing.T) {
pset := &propertySet{
existingValues: map[string]uint64{},
proposedValues: map[string]uint64{
"dc2": 1,
"dc1": 1,
"dc3": 1,
},
clearedValues: map[string]uint64{
"dc2": 1,
"dc3": 1,
},
targetAttribute: "${node.datacenter}",
}

opt := &structs.Node{
Datacenter: "dc2",
}
boost := evenSpreadScoreBoost(pset, opt)
require.False(t, math.IsInf(boost, 1))
}

0 comments on commit ea843a5

Please sign in to comment.