Skip to content

Commit

Permalink
cherry pick #2864 to release-4.0 (#2875)
Browse files Browse the repository at this point in the history
Signed-off-by: ti-srebot <ti-srebot@pingcap.com>

Co-authored-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
ti-srebot and rleungx authored Sep 1, 2020
1 parent 928c257 commit 6403e70
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 41 deletions.
25 changes: 10 additions & 15 deletions server/core/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,7 @@ import (
const (
// Interval to save store meta (including heartbeat ts) to etcd.
storePersistInterval = 5 * time.Minute
lowSpaceThreshold = 100 * (1 << 10) // 100 GB
highSpaceThreshold = 300 * (1 << 10) // 300 GB
mb = 1 << 20 // megabyte
mb = 1 << 20 // megabyte
)

// StoreInfo contains information about a store.
Expand Down Expand Up @@ -303,6 +301,7 @@ func (s *StoreInfo) RegionScore(highSpaceRatio, lowSpaceRatio float64, delta int
var amplification float64
available := float64(s.GetAvailable()) / mb
used := float64(s.GetUsedSize()) / mb
capacity := float64(s.GetCapacity()) / mb

if s.GetRegionSize() == 0 || used == 0 {
amplification = 1
Expand All @@ -312,9 +311,9 @@ func (s *StoreInfo) RegionScore(highSpaceRatio, lowSpaceRatio float64, delta int
}

// highSpaceBound is the lower bound of the high space stage.
highSpaceBound := s.GetSpaceThreshold(highSpaceRatio, highSpaceThreshold)
highSpaceBound := (1 - highSpaceRatio) * capacity
// lowSpaceBound is the upper bound of the low space stage.
lowSpaceBound := s.GetSpaceThreshold(lowSpaceRatio, lowSpaceThreshold)
lowSpaceBound := (1 - lowSpaceRatio) * capacity
if available-float64(delta)/amplification >= highSpaceBound {
score = float64(s.GetRegionSize() + delta)
} else if available-float64(delta)/amplification <= lowSpaceBound {
Expand Down Expand Up @@ -347,21 +346,17 @@ func (s *StoreInfo) StorageSize() uint64 {
return s.GetUsedSize()
}

// GetSpaceThreshold returns the threshold of low/high space in MB.
func (s *StoreInfo) GetSpaceThreshold(spaceRatio, spaceThreshold float64) float64 {
var min float64 = spaceThreshold
capacity := float64(s.GetCapacity()) / mb
space := capacity * (1.0 - spaceRatio)
if min > space {
min = space
// AvailableRatio is store's freeSpace/capacity.
func (s *StoreInfo) AvailableRatio() float64 {
if s.GetCapacity() == 0 {
return 0
}
return min
return float64(s.GetAvailable()) / float64(s.GetCapacity())
}

// IsLowSpace checks if the store is lack of space.
func (s *StoreInfo) IsLowSpace(lowSpaceRatio float64) bool {
available := float64(s.GetAvailable()) / mb
return s.GetStoreStats() != nil && available <= s.GetSpaceThreshold(lowSpaceRatio, lowSpaceThreshold)
return s.GetStoreStats() != nil && s.AvailableRatio() < 1-lowSpaceRatio
}

// ResourceCount returns count of leader/region in the store.
Expand Down
26 changes: 0 additions & 26 deletions server/core/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
package core

import (
"fmt"
"math"
"sync"
"time"
Expand Down Expand Up @@ -100,31 +99,6 @@ var _ = Suite(&testStoreSuite{})

type testStoreSuite struct{}

func (s *testStoreSuite) TestLowSpaceThreshold(c *C) {
stats := &pdpb.StoreStats{}
stats.Capacity = 10 * (1 << 40) // 10 TB
stats.Available = 1 * (1 << 40) // 1 TB

store := NewStoreInfo(
&metapb.Store{Id: 1},
SetStoreStats(stats),
)
threshold := store.GetSpaceThreshold(0.8, lowSpaceThreshold)
c.Assert(threshold, Equals, float64(lowSpaceThreshold))
c.Assert(store.IsLowSpace(0.8), Equals, false)
stats = &pdpb.StoreStats{}
stats.Capacity = 100 * (1 << 20) // 100 MB
stats.Available = 10 * (1 << 20) // 10 MB

store = NewStoreInfo(
&metapb.Store{Id: 1},
SetStoreStats(stats),
)
threshold = store.GetSpaceThreshold(0.8, lowSpaceThreshold)
c.Assert(fmt.Sprintf("%.2f", threshold), Equals, fmt.Sprintf("%.2f", 100*0.2))
c.Assert(store.IsLowSpace(0.8), Equals, true)
}

func (s *testStoreSuite) TestRegionScore(c *C) {
stats := &pdpb.StoreStats{}
stats.Capacity = 512 * (1 << 20) // 512 MB
Expand Down

0 comments on commit 6403e70

Please sign in to comment.