Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

statistics: fix anticount and cold peer statistics #5479

Merged
merged 7 commits into from
Sep 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -185,7 +185,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, RegionStatCount)
Expand Down Expand Up @@ -235,3 +235,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 @@ -89,7 +89,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 @@ -228,8 +228,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 @@ -511,20 +511,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