Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix edge case with spread scoring #5713

Merged
merged 3 commits into from
May 15, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions scheduler/spread.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,12 @@ 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 {
// Current attribute count is equal to min and both are zero. This means no allocations
// were placed for this attribute value yet. Should get the maximum possible boost.
return 1.0
}

// Penalty based on delta from max value
delta := int(maxCount - minCount)
deltaBoost = float64(delta) / float64(minCount)
Expand Down
25 changes: 25 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,26 @@ 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))
require.Equal(t, 1.0, boost)
}