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 score when region size is zero #899

Merged
merged 4 commits into from Jan 4, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 7 additions & 4 deletions server/core/store.go
Expand Up @@ -14,6 +14,7 @@
package core

import (
"math"
"strings"
"time"

Expand Down Expand Up @@ -105,18 +106,20 @@ const minWeight = 1e-6

// LeaderScore returns the store's leader score: leaderCount / leaderWeight.
func (s *StoreInfo) LeaderScore() float64 {
size := math.Max(1, float64(s.LeaderSize))
if s.LeaderWeight <= 0 {
return float64(s.LeaderSize) / minWeight
return size / minWeight
}
return float64(s.LeaderSize) / s.LeaderWeight
return size / s.LeaderWeight
}

// RegionScore returns the store's region score: regionSize / regionWeight.
func (s *StoreInfo) RegionScore() float64 {
size := math.Max(1, float64(s.RegionSize))
if s.RegionWeight <= 0 {
return float64(s.RegionSize) / minWeight
return size / minWeight
}
return float64(s.RegionSize) / s.RegionWeight
return size / s.RegionWeight
}

// StorageSize returns store's used storage size reported from tikv.
Expand Down
4 changes: 2 additions & 2 deletions server/schedulers/balance_test.go
Expand Up @@ -170,9 +170,9 @@ func (s *testBalanceLeaderSchedulerSuite) TestBalanceLimit(c *C) {
c.Check(s.schedule(nil), IsNil)

// Stores: 1 2 3 4
// Leaders: 10 0 0 0
// Leaders: 16 0 0 0
// Region1: L F F F
s.tc.updateLeaderCount(1, 10)
s.tc.updateLeaderCount(1, 16)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why change to 16?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note that: shouldBalance() uses math.Min(sourceSizeDiff, targetSizeDiff) >= float64(region.ApproximateSize)*tolerantRatio to decide whether to balance.

Before when size is zero, score is zero. for this case,
// Stores: 1 2 3 4
// Score: 100 0 0 0
avgScore is 25, and sizediff satisfy the formula above just enough

After when size is zero, score is 1, so for this case
// Stores: 1 2 3 4
// Score: 100 1 1 1
avgScore is 25.75, and sizediff doesn't satisfy the formula above

so enlarge the leader count

c.Check(s.schedule(nil), NotNil)

// Stores: 1 2 3 4
Expand Down