From d06502a0ca64c0ed878b3692a129ec228aeb44dc Mon Sep 17 00:00:00 2001 From: JaySon-Huang Date: Thu, 28 May 2020 11:46:06 +0800 Subject: [PATCH 1/2] Avoid bug for store.used_size == 0 Signed-off-by: JaySon-Huang --- server/core/store.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/server/core/store.go b/server/core/store.go index ef54bbd8c85..48b986ef9f8 100644 --- a/server/core/store.go +++ b/server/core/store.go @@ -304,7 +304,7 @@ func (s *StoreInfo) RegionScore(highSpaceRatio, lowSpaceRatio float64, delta int available := float64(s.GetAvailable()) / mb used := float64(s.GetUsedSize()) / mb - if s.GetRegionSize() == 0 { + if s.GetRegionSize() == 0 || used == 0 { amplification = 1 } else { // because of rocksdb compression, region size is larger than actual used size From d69a9a99a20220d8ff100bef63b2d4526f69f9ea Mon Sep 17 00:00:00 2001 From: JaySon-Huang Date: Thu, 28 May 2020 17:57:55 +0800 Subject: [PATCH 2/2] Add test case Signed-off-by: JaySon-Huang --- server/core/store_test.go | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/server/core/store_test.go b/server/core/store_test.go index 928466eb6f4..4f790aea1ae 100644 --- a/server/core/store_test.go +++ b/server/core/store_test.go @@ -15,6 +15,7 @@ package core import ( "fmt" + "math" "sync" "time" @@ -123,3 +124,19 @@ func (s *testStoreSuite) TestLowSpaceThreshold(c *C) { 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 + stats.Available = 100 * (1 << 20) // 100 MB + stats.UsedSize = 0 + + store := NewStoreInfo( + &metapb.Store{Id: 1}, + SetStoreStats(stats), + SetRegionSize(1), + ) + score := store.RegionScore(0.7, 0.9, 0) + // Region score should never be NaN, or /store API would fail. + c.Assert(math.IsNaN(score), Equals, false) +}