Skip to content

Commit

Permalink
statistics: fix anticount and cold peer statistics (tikv#5479)
Browse files Browse the repository at this point in the history
close tikv#5478

Signed-off-by: lhy1024 <[email protected]>

Co-authored-by: 混沌DM <[email protected]>
  • Loading branch information
lhy1024 and HunDunDM committed Sep 13, 2022
1 parent ea25ac1 commit b95e5f8
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 15 deletions.
5 changes: 2 additions & 3 deletions server/cluster/cluster_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
9 changes: 8 additions & 1 deletion server/statistics/hot_peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
}
20 changes: 9 additions & 11 deletions server/statistics/hot_peer_cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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) {
Expand Down
1 change: 1 addition & 0 deletions server/statistics/hot_peer_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit b95e5f8

Please sign in to comment.