@@ -45,6 +45,7 @@ type StoreInfo struct {
4545func NewStoreInfo (store * metapb.Store ) * StoreInfo {
4646 return & StoreInfo {
4747 Store : store ,
48+ Stats : & pdpb.StoreStats {},
4849 LeaderWeight : 1.0 ,
4950 RegionWeight : 1.0 ,
5051 }
@@ -103,15 +104,46 @@ func (s *StoreInfo) DownTime() time.Duration {
103104}
104105
105106const minWeight = 1e-6
107+ const maxScore = 1024 * 1024 * 1024
106108
107- // LeaderScore returns the store's leader score: leaderCount / leaderWeight.
108- func (s * StoreInfo ) LeaderScore () float64 {
109- return float64 (s .LeaderSize ) / math .Max (s .LeaderWeight , minWeight )
109+ // LeaderScore returns the store's leader score: leaderSize / leaderWeight.
110+ func (s * StoreInfo ) LeaderScore (delta int64 ) float64 {
111+ return float64 (s .LeaderSize + delta ) / math .Max (s .LeaderWeight , minWeight )
110112}
111113
112- // RegionScore returns the store's region score: regionSize / regionWeight.
113- func (s * StoreInfo ) RegionScore () float64 {
114- return float64 (s .RegionSize ) / math .Max (s .RegionWeight , minWeight )
114+ // RegionScore returns the store's region score.
115+ func (s * StoreInfo ) RegionScore (highSpaceRatio , lowSpaceRatio float64 , delta int64 ) float64 {
116+ if s .RegionSize == 0 {
117+ return float64 (delta )
118+ }
119+
120+ capacity := float64 (s .Stats .GetCapacity ()) / (1 << 20 )
121+ available := float64 (s .Stats .GetAvailable ()) / (1 << 20 )
122+
123+ var score float64
124+
125+ // because of rocksdb compression, region size is larger than actual used size
126+ amplification := float64 (s .RegionSize ) / (float64 (s .Stats .GetUsedSize ()) / (1 << 20 ))
127+
128+ if available - float64 (delta )/ amplification >= (1 - highSpaceRatio )* capacity {
129+ score = float64 (s .RegionSize + delta )
130+ } else if available - float64 (delta )/ amplification <= (1 - lowSpaceRatio )* capacity {
131+ score = maxScore - (available - float64 (delta )/ amplification )
132+ } else {
133+ // to make the score function continuous, we use linear function y = k * x + b as transition period
134+ // from above we know that there are two points must on the function image
135+ // p1(highSpaceRatio*capacity*amplification, highSpaceRatio*capacity*amplification) and
136+ // p2(lowSpaceRatio*capacity*amplification, maxScore-(1-lowSpaceRatio)*capacity)
137+ // so k = (y2 - y1) / (x2 - x1)
138+ x1 , y1 := highSpaceRatio * capacity * amplification , highSpaceRatio * capacity * amplification
139+ x2 , y2 := lowSpaceRatio * capacity * amplification , maxScore - (1 - lowSpaceRatio )* capacity
140+
141+ k := (y2 - y1 ) / (x2 - x1 )
142+ b := y1 - k * x1
143+ score = k * float64 (s .RegionSize + delta ) + b
144+ }
145+
146+ return score / math .Max (s .RegionWeight , minWeight )
115147}
116148
117149// StorageSize returns store's used storage size reported from tikv.
@@ -127,11 +159,9 @@ func (s *StoreInfo) AvailableRatio() float64 {
127159 return float64 (s .Stats .GetAvailable ()) / float64 (s .Stats .GetCapacity ())
128160}
129161
130- const storeLowSpaceThreshold = 0.2
131-
132162// IsLowSpace checks if the store is lack of space.
133- func (s * StoreInfo ) IsLowSpace () bool {
134- return s .Stats != nil && s .AvailableRatio () < storeLowSpaceThreshold
163+ func (s * StoreInfo ) IsLowSpace (lowSpaceRatio float64 ) bool {
164+ return s .Stats != nil && s .AvailableRatio () < 1 - lowSpaceRatio
135165}
136166
137167// ResourceCount reutrns count of leader/region in the store.
@@ -159,12 +189,12 @@ func (s *StoreInfo) ResourceSize(kind ResourceKind) int64 {
159189}
160190
161191// ResourceScore reutrns score of leader/region in the store.
162- func (s * StoreInfo ) ResourceScore (kind ResourceKind ) float64 {
192+ func (s * StoreInfo ) ResourceScore (kind ResourceKind , highSpaceRatio , lowSpaceRatio float64 , delta int64 ) float64 {
163193 switch kind {
164194 case LeaderKind :
165- return s .LeaderScore ()
195+ return s .LeaderScore (delta )
166196 case RegionKind :
167- return s .RegionScore ()
197+ return s .RegionScore (highSpaceRatio , lowSpaceRatio , delta )
168198 default :
169199 return 0
170200 }
0 commit comments