Skip to content

Commit

Permalink
storage: update capacity score to contain range counts
Browse files Browse the repository at this point in the history
This gets rid of the wired 1/(1+rangecount) that was there before. If a
more complex scoring is required, it can be added then.

Part of cockroachdb#10275.
  • Loading branch information
BramGruneir committed Dec 16, 2016
1 parent 43ca188 commit ceca0e2
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 58 deletions.
26 changes: 11 additions & 15 deletions pkg/storage/rule_solver.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,19 @@ type candidate struct {
store roachpb.StoreDescriptor
valid bool
constraintScore float64
capacityScore float64
capacityScore int
}

func (c candidate) String() string {
return fmt.Sprintf("s%d, valid:%t, con:%.2f, bal:%.2f",
return fmt.Sprintf("s%d, valid:%t, con:%.2f, cap:%d",
c.store.StoreID, c.valid, c.constraintScore, c.capacityScore)
}

// less first compares valid, then constraint scores, then capacity
// less first compares valid, then constraint scores, then capacity scores.
// - A valid candidate is always greater than a non-valid.
// - A higher constraint score is always greater, regardless of capacity
// scores.
// - A lower capacity score is greater.
func (c candidate) less(o candidate) bool {
if !o.valid {
return false
Expand All @@ -50,7 +53,7 @@ func (c candidate) less(o candidate) bool {
if c.constraintScore != o.constraintScore {
return c.constraintScore < o.constraintScore
}
return c.capacityScore < o.capacityScore
return c.capacityScore > o.capacityScore
}

type candidateList []candidate
Expand Down Expand Up @@ -225,7 +228,7 @@ func allocateCandidates(
store: s,
valid: true,
constraintScore: constraintScore,
capacityScore: capacityScore(s),
capacityScore: int(s.Capacity.RangeCount),
})
}
if deterministic {
Expand Down Expand Up @@ -270,7 +273,7 @@ func removeCandidates(
store: s,
valid: true,
constraintScore: constraintScore,
capacityScore: capacityScore(s),
capacityScore: int(s.Capacity.RangeCount),
})
}
if deterministic {
Expand Down Expand Up @@ -325,7 +328,7 @@ func rebalanceCandidates(
store: s,
valid: true,
constraintScore: constraintScore,
capacityScore: capacityScore(s),
capacityScore: int(s.Capacity.RangeCount),
})
} else {
if !constraintsOk || !maxCapacityOK || !rebalanceToConvergesOnMean(sl, s) {
Expand All @@ -343,7 +346,7 @@ func rebalanceCandidates(
store: s,
valid: true,
constraintScore: constraintScore,
capacityScore: capacityScore(s),
capacityScore: int(s.Capacity.RangeCount),
})
}
}
Expand Down Expand Up @@ -450,13 +453,6 @@ func diversityRemovalScore(
return maxScore
}

// capacityScore returns a score between 0 and 1 that is inversely proportional
// to the number of ranges on the store such that the most empty store will have
// the highest scores.
func capacityScore(store roachpb.StoreDescriptor) float64 {
return 1.0 / float64(store.Capacity.RangeCount+1)
}

// maxCapacityCheck returns true if the store has room for a new replica.
func maxCapacityCheck(store roachpb.StoreDescriptor) bool {
return store.Capacity.FractionUsed() < maxFractionUsedThreshold
Expand Down
76 changes: 33 additions & 43 deletions pkg/storage/rule_solver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ func TestCandidateSelection(t *testing.T) {
StoreID: roachpb.StoreID(i + idShift),
},
constraintScore: float64(score.constraint),
capacityScore: float64(score.capacity),
capacityScore: score.capacity,
valid: true,
})
}
Expand Down Expand Up @@ -121,18 +121,18 @@ func TestCandidateSelection(t *testing.T) {
bad: scoreTuple{0, 0},
},
{
candidates: []scoreTuple{{0, 1}, {0, 0}},
best: []scoreTuple{{0, 1}, {0, 0}},
worst: []scoreTuple{{0, 1}, {0, 0}},
good: scoreTuple{0, 1},
bad: scoreTuple{0, 0},
candidates: []scoreTuple{{0, 0}, {0, 1}},
best: []scoreTuple{{0, 0}, {0, 1}},
worst: []scoreTuple{{0, 0}, {0, 1}},
good: scoreTuple{0, 0},
bad: scoreTuple{0, 1},
},
{
candidates: []scoreTuple{{0, 2}, {0, 1}, {0, 0}},
best: []scoreTuple{{0, 2}, {0, 1}, {0, 0}},
worst: []scoreTuple{{0, 2}, {0, 1}, {0, 0}},
candidates: []scoreTuple{{0, 0}, {0, 1}, {0, 2}},
best: []scoreTuple{{0, 0}, {0, 1}, {0, 2}},
worst: []scoreTuple{{0, 0}, {0, 1}, {0, 2}},
good: scoreTuple{0, 1},
bad: scoreTuple{0, 0},
bad: scoreTuple{0, 2},
},
{
candidates: []scoreTuple{{1, 0}, {0, 1}},
Expand All @@ -142,25 +142,25 @@ func TestCandidateSelection(t *testing.T) {
bad: scoreTuple{0, 1},
},
{
candidates: []scoreTuple{{1, 0}, {0, 2}, {0, 1}},
candidates: []scoreTuple{{1, 0}, {0, 1}, {0, 2}},
best: []scoreTuple{{1, 0}},
worst: []scoreTuple{{0, 2}, {0, 1}},
worst: []scoreTuple{{0, 1}, {0, 2}},
good: scoreTuple{1, 0},
bad: scoreTuple{0, 1},
bad: scoreTuple{0, 2},
},
{
candidates: []scoreTuple{{1, 1}, {1, 0}, {0, 2}},
best: []scoreTuple{{1, 1}, {1, 0}},
candidates: []scoreTuple{{1, 0}, {1, 1}, {0, 2}},
best: []scoreTuple{{1, 0}, {1, 1}},
worst: []scoreTuple{{0, 2}},
good: scoreTuple{1, 1},
good: scoreTuple{1, 0},
bad: scoreTuple{0, 2},
},
{
candidates: []scoreTuple{{1, 1}, {1, 0}, {0, 3}, {0, 2}},
best: []scoreTuple{{1, 1}, {1, 0}},
worst: []scoreTuple{{0, 3}, {0, 2}},
good: scoreTuple{1, 1},
bad: scoreTuple{0, 2},
candidates: []scoreTuple{{1, 0}, {1, 1}, {0, 2}, {0, 3}},
best: []scoreTuple{{1, 0}, {1, 1}},
worst: []scoreTuple{{0, 2}, {0, 3}},
good: scoreTuple{1, 0},
bad: scoreTuple{0, 3},
},
}

Expand Down Expand Up @@ -228,57 +228,57 @@ func TestBetterThan(t *testing.T) {
{
valid: true,
constraintScore: 1,
capacityScore: 1,
capacityScore: 0,
},
{
valid: true,
constraintScore: 1,
capacityScore: 1,
capacityScore: 0,
},
{
valid: true,
constraintScore: 1,
capacityScore: 0,
capacityScore: 1,
},
{
valid: true,
constraintScore: 1,
capacityScore: 0,
capacityScore: 1,
},
{
valid: true,
constraintScore: 0,
capacityScore: 1,
capacityScore: 0,
},
{
valid: true,
constraintScore: 0,
capacityScore: 1,
capacityScore: 0,
},
{
valid: true,
constraintScore: 0,
capacityScore: 0,
capacityScore: 1,
},
{
valid: true,
constraintScore: 0,
capacityScore: 0,
capacityScore: 1,
},
{
valid: false,
constraintScore: 1,
capacityScore: 0.5,
capacityScore: 0,
},
{
valid: false,
constraintScore: 0,
capacityScore: 0.5,
capacityScore: 0,
},
{
valid: false,
constraintScore: 0,
capacityScore: 0,
capacityScore: 1,
},
}

Expand Down Expand Up @@ -631,8 +631,7 @@ func TestDiversityRemovalScore(t *testing.T) {
}
}

// TestCapacityScore tests both capacityScore and maxCapacityCheck.
func TestCapacityScore(t *testing.T) {
func TestMaxCapacity(t *testing.T) {
defer leaktest.AfterTest(t)()

expectedCheck := map[roachpb.StoreID]bool{
Expand All @@ -641,17 +640,8 @@ func TestCapacityScore(t *testing.T) {
testStoreUSb: true,
testStoreEurope: true,
}
expectedScore := map[roachpb.StoreID]float64{
testStoreUSa15: 1.0 / 100.0,
testStoreUSa1: 1.0,
testStoreUSb: 1.0 / 51.0,
testStoreEurope: 1.0 / 41.0,
}

for _, s := range testStores {
if e, a := expectedScore[s.StoreID], capacityScore(s); e != a {
t.Errorf("store %d expected capacity score: %.2f, actual %.2f", s.StoreID, e, a)
}
if e, a := expectedCheck[s.StoreID], maxCapacityCheck(s); e != a {
t.Errorf("store %d expected max capacity check: %t, actual %t", s.StoreID, e, a)
}
Expand Down

0 comments on commit ceca0e2

Please sign in to comment.