diff --git a/server/cluster/cluster_test.go b/server/cluster/cluster_test.go index 17a34c997f2..bb5e5013416 100644 --- a/server/cluster/cluster_test.go +++ b/server/cluster/cluster_test.go @@ -155,8 +155,7 @@ func TestStoreHeartbeat(t *testing.T) { re.Nil(cluster.HandleStoreHeartbeat(hotHeartBeat)) time.Sleep(20 * time.Millisecond) storeStats = cluster.hotStat.RegionStats(statistics.Read, 1) - re.Len(storeStats[1], 1) - re.Equal(uint64(1), storeStats[1][0].RegionID) + re.Len(storeStats[1], 0) storeStats = cluster.hotStat.RegionStats(statistics.Read, 3) re.Empty(storeStats[1]) // after 2 hot heartbeats, wo can find region 1 peer again @@ -593,7 +592,7 @@ func TestRegionHeartbeatHotStat(t *testing.T) { EndKey: []byte{byte(1 + 1)}, RegionEpoch: &metapb.RegionEpoch{ConfVer: 2, Version: 2}, } - region := core.NewRegionInfo(regionMeta, leader, core.WithInterval(&pdpb.TimeInterval{StartTimestamp: 0, EndTimestamp: 10}), + region := core.NewRegionInfo(regionMeta, leader, core.WithInterval(&pdpb.TimeInterval{StartTimestamp: 0, EndTimestamp: statistics.RegionHeartBeatReportInterval}), core.SetWrittenBytes(30000*10), core.SetWrittenKeys(300000*10)) err = cluster.processRegionHeartbeat(region) diff --git a/server/statistics/hot_peer.go b/server/statistics/hot_peer.go index 83077d24f54..13c84ee2516 100644 --- a/server/statistics/hot_peer.go +++ b/server/statistics/hot_peer.go @@ -154,7 +154,7 @@ func (stat *HotPeerStat) GetThresholds() []float64 { return stat.thresholds } -// Clone clones the HotPeerStat +// Clone clones the HotPeerStat. func (stat *HotPeerStat) Clone() *HotPeerStat { ret := *stat ret.Loads = make([]float64, DimLen) @@ -204,3 +204,10 @@ func (stat *HotPeerStat) GetStores() []uint64 { func (stat *HotPeerStat) IsLearner() bool { return stat.isLearner } + +func (stat *HotPeerStat) defaultAntiCount() int { + if stat.Kind == Read { + return hotRegionAntiCount * (RegionHeartBeatReportInterval / StoreHeartBeatReportInterval) + } + return hotRegionAntiCount +} diff --git a/server/statistics/hot_peer_cache.go b/server/statistics/hot_peer_cache.go index 5ddacd289cd..c34d67baecf 100644 --- a/server/statistics/hot_peer_cache.go +++ b/server/statistics/hot_peer_cache.go @@ -98,7 +98,7 @@ func (f *hotPeerCache) RegionStats(minHotDegree int) map[uint64][]*HotPeerStat { values := peers.GetAll() stat := make([]*HotPeerStat, 0, len(values)) for _, v := range values { - if peer := v.(*HotPeerStat); peer.HotDegree >= minHotDegree && !peer.inCold { + if peer := v.(*HotPeerStat); peer.HotDegree >= minHotDegree && !peer.inCold && peer.AntiCount == peer.defaultAntiCount() { stat = append(stat, peer) } } @@ -255,8 +255,8 @@ func (f *hotPeerCache) checkColdPeer(storeID uint64, reportRegions map[uint64]*c StoreID: storeID, RegionID: regionID, Kind: f.kind, - // use oldItem.thresholds to make the newItem won't affect the threshold - Loads: oldItem.thresholds, + // use 0 to make the cold newItem won't affect the loads. + Loads: make([]float64, len(oldItem.Loads)), LastUpdateTime: time.Now(), isLeader: oldItem.isLeader, isLearner: oldItem.isLearner, @@ -539,20 +539,18 @@ func coldItem(newItem, oldItem *HotPeerStat) { func hotItem(newItem, oldItem *HotPeerStat) { newItem.HotDegree = oldItem.HotDegree + 1 - newItem.AntiCount = hotRegionAntiCount - newItem.allowInherited = true - if newItem.Kind == Read { - newItem.AntiCount = hotRegionAntiCount * (RegionHeartBeatReportInterval / StoreHeartBeatReportInterval) + if oldItem.AntiCount < oldItem.defaultAntiCount() { + newItem.AntiCount = oldItem.AntiCount + 1 + } else { + newItem.AntiCount = oldItem.AntiCount } + newItem.allowInherited = true } func initItem(item *HotPeerStat) { item.HotDegree = 1 - item.AntiCount = hotRegionAntiCount + item.AntiCount = item.defaultAntiCount() item.allowInherited = true - if item.Kind == Read { - item.AntiCount = hotRegionAntiCount * (RegionHeartBeatReportInterval / StoreHeartBeatReportInterval) - } } func inheritItem(newItem, oldItem *HotPeerStat) { diff --git a/server/statistics/hot_peer_cache_test.go b/server/statistics/hot_peer_cache_test.go index 1b762b8567b..e96800b3481 100644 --- a/server/statistics/hot_peer_cache_test.go +++ b/server/statistics/hot_peer_cache_test.go @@ -331,6 +331,7 @@ func TestUpdateHotPeerStat(t *testing.T) { re.Equal(0, newItem.AntiCount) // sum of interval is larger than report interval, and hot oldItem = newItem + oldItem.AntiCount = oldItem.defaultAntiCount() newItem = cache.updateHotPeerStat(nil, newItem, oldItem, []float64{60.0, 60.0, 60.0}, 4*time.Second) re.Equal(1, newItem.HotDegree) re.Equal(2*m, newItem.AntiCount)