Skip to content

Commit

Permalink
statistics: fix ToHotPeersStat result when HotPeers is empty (#5597)
Browse files Browse the repository at this point in the history
close #5598

Signed-off-by: lhy1024 <admin@liudos.us>

Co-authored-by: ShuNing <nolouch@gmail.com>
  • Loading branch information
lhy1024 and nolouch authored Oct 21, 2022
1 parent 1a485f7 commit 8ed1660
Show file tree
Hide file tree
Showing 2 changed files with 87 additions and 5 deletions.
10 changes: 5 additions & 5 deletions server/statistics/store_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,13 @@ type StoreLoadDetail struct {

// ToHotPeersStat abstracts load information to HotPeersStat.
func (li *StoreLoadDetail) ToHotPeersStat() *HotPeersStat {
storeByteRate, storeKeyRate, storeQueryRate := li.LoadPred.Current.Loads[ByteDim],
li.LoadPred.Current.Loads[KeyDim], li.LoadPred.Current.Loads[QueryDim]
if len(li.HotPeers) == 0 {
return &HotPeersStat{
StoreByteRate: 0.0,
StoreKeyRate: 0.0,
StoreQueryRate: 0.0,
StoreByteRate: storeByteRate,
StoreKeyRate: storeKeyRate,
StoreQueryRate: storeQueryRate,
TotalBytesRate: 0.0,
TotalKeysRate: 0.0,
TotalQueryRate: 0.0,
Expand All @@ -51,8 +53,6 @@ func (li *StoreLoadDetail) ToHotPeersStat() *HotPeersStat {
queryRate += peer.Loads[QueryDim]
}
}
storeByteRate, storeKeyRate, storeQueryRate := li.LoadPred.Current.Loads[ByteDim],
li.LoadPred.Current.Loads[KeyDim], li.LoadPred.Current.Loads[QueryDim]

return &HotPeersStat{
TotalBytesRate: byteRate,
Expand Down
82 changes: 82 additions & 0 deletions tests/pdctl/hot/hot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -358,3 +358,85 @@ func TestHistoryHotRegions(t *testing.T) {
re.NoError(err)
re.Error(json.Unmarshal(output, &hotRegions))
}

func TestHotWithoutHotPeer(t *testing.T) {
re := require.New(t)
statistics.Denoising = false
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
cluster, err := tests.NewTestCluster(ctx, 1, func(cfg *config.Config, serverName string) { cfg.Schedule.HotRegionCacheHitsThreshold = 0 })
re.NoError(err)
err = cluster.RunInitialServers()
re.NoError(err)
cluster.WaitLeader()
pdAddr := cluster.GetConfig().GetClientURL()
cmd := pdctlCmd.GetRootCmd()

stores := []*metapb.Store{
{
Id: 1,
State: metapb.StoreState_Up,
LastHeartbeat: time.Now().UnixNano(),
},
{
Id: 2,
State: metapb.StoreState_Up,
LastHeartbeat: time.Now().UnixNano(),
},
}

leaderServer := cluster.GetServer(cluster.GetLeader())
re.NoError(leaderServer.BootstrapCluster())
for _, store := range stores {
pdctl.MustPutStore(re, leaderServer.GetServer(), store)
}
timestamp := uint64(time.Now().UnixNano())
load := 1024.0
for _, store := range stores {
for i := 0; i < 5; i++ {
err := leaderServer.GetServer().GetRaftCluster().HandleStoreHeartbeat(&pdpb.StoreStats{
StoreId: store.Id,
BytesRead: uint64(load * statistics.StoreHeartBeatReportInterval),
KeysRead: uint64(load * statistics.StoreHeartBeatReportInterval),
BytesWritten: uint64(load * statistics.StoreHeartBeatReportInterval),
KeysWritten: uint64(load * statistics.StoreHeartBeatReportInterval),
Capacity: 1000 * units.MiB,
Available: 1000 * units.MiB,
Interval: &pdpb.TimeInterval{
StartTimestamp: timestamp + uint64(i*statistics.StoreHeartBeatReportInterval),
EndTimestamp: timestamp + uint64((i+1)*statistics.StoreHeartBeatReportInterval)},
})
re.NoError(err)
}
}
defer cluster.Destroy()

// wait hot scheduler starts
time.Sleep(5000 * time.Millisecond)
{
args := []string{"-u", pdAddr, "hot", "read"}
output, err := pdctl.ExecuteCommand(cmd, args...)
hotRegion := statistics.StoreHotPeersInfos{}
re.NoError(err)
re.NoError(json.Unmarshal(output, &hotRegion))
re.Equal(hotRegion.AsPeer[1].Count, 0)
re.Equal(0.0, hotRegion.AsPeer[1].TotalBytesRate)
re.Equal(load, hotRegion.AsPeer[1].StoreByteRate)
re.Equal(hotRegion.AsLeader[1].Count, 0)
re.Equal(0.0, hotRegion.AsLeader[1].TotalBytesRate)
re.Equal(load, hotRegion.AsLeader[1].StoreByteRate)
}
{
args := []string{"-u", pdAddr, "hot", "write"}
output, err := pdctl.ExecuteCommand(cmd, args...)
hotRegion := statistics.StoreHotPeersInfos{}
re.NoError(err)
re.NoError(json.Unmarshal(output, &hotRegion))
re.Equal(hotRegion.AsPeer[1].Count, 0)
re.Equal(0.0, hotRegion.AsPeer[1].TotalBytesRate)
re.Equal(load, hotRegion.AsPeer[1].StoreByteRate)
re.Equal(hotRegion.AsLeader[1].Count, 0)
re.Equal(0.0, hotRegion.AsLeader[1].TotalBytesRate)
re.Equal(0.0, hotRegion.AsLeader[1].StoreByteRate) // write leader sum
}
}

0 comments on commit 8ed1660

Please sign in to comment.