Skip to content

Commit

Permalink
statistics: fix ToHotPeersStat result when HotPeers is empty (tik…
Browse files Browse the repository at this point in the history
…v#5597) (tikv#5618)

ref tikv#5597, close tikv#5598

Signed-off-by: ti-chi-bot <ti-community-prow-bot@tidb.io>
Signed-off-by: lhy1024 <admin@liudos.us>

Co-authored-by: lhy1024 <admin@liudos.us>
  • Loading branch information
ti-chi-bot and lhy1024 authored Nov 22, 2022
1 parent f929770 commit 24dcebb
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 2 deletions.
7 changes: 5 additions & 2 deletions server/statistics/store_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,14 @@ type StoreLoadDetail struct {
// ToHotPeersStat abstracts load information to HotPeersStat.
func (li *StoreLoadDetail) ToHotPeersStat() *HotPeersStat {
totalLoads := make([]float64, RegionStatCount)
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{
TotalLoads: totalLoads,
StoreByteRate: storeByteRate,
StoreKeyRate: storeKeyRate,
StoreQueryRate: storeQueryRate,
TotalBytesRate: 0.0,
TotalKeysRate: 0.0,
TotalQueryRate: 0.0,
Expand All @@ -57,8 +62,6 @@ func (li *StoreLoadDetail) ToHotPeersStat() *HotPeersStat {

b, k, q := GetRegionStatKind(kind, ByteDim), GetRegionStatKind(kind, KeyDim), GetRegionStatKind(kind, QueryDim)
byteRate, keyRate, queryRate := totalLoads[b], totalLoads[k], totalLoads[q]
storeByteRate, storeKeyRate, storeQueryRate := li.LoadPred.Current.Loads[ByteDim],
li.LoadPred.Current.Loads[KeyDim], li.LoadPred.Current.Loads[QueryDim]

return &HotPeersStat{
TotalLoads: totalLoads,
Expand Down
85 changes: 85 additions & 0 deletions tests/pdctl/hot/hot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"testing"
"time"

"github.com/docker/go-units"
"github.com/gogo/protobuf/proto"
. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
Expand Down Expand Up @@ -362,3 +363,87 @@ func (s *hotTestSuite) TestHistoryHotRegions(c *C) {
c.Assert(e, IsNil)
c.Assert(json.Unmarshal(output, &hotRegions), NotNil)
}

func (s *hotTestSuite) TestHotWithoutHotPeer(c *C) {
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 })
c.Assert(err, IsNil)
err = cluster.RunInitialServers()
c.Assert(err, IsNil)
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())
err = leaderServer.BootstrapCluster()
c.Assert(err, IsNil)
for _, store := range stores {
pdctl.MustPutStore(c, 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)},
})
c.Assert(err, IsNil)
}
}
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{}
c.Assert(err, IsNil)
err = json.Unmarshal(output, &hotRegion)
c.Assert(err, IsNil)
c.Assert(hotRegion.AsPeer[1].Count, Equals, 0)
c.Assert(hotRegion.AsPeer[1].TotalBytesRate, Equals, 0.0)
c.Assert(hotRegion.AsPeer[1].StoreByteRate, Equals, load)
c.Assert(hotRegion.AsLeader[1].Count, Equals, 0)
c.Assert(hotRegion.AsLeader[1].TotalBytesRate, Equals, 0.0)
c.Assert(hotRegion.AsLeader[1].StoreByteRate, Equals, load)
}
{
args := []string{"-u", pdAddr, "hot", "write"}
output, err := pdctl.ExecuteCommand(cmd, args...)
hotRegion := statistics.StoreHotPeersInfos{}
c.Assert(err, IsNil)
err = json.Unmarshal(output, &hotRegion)
c.Assert(err, IsNil)
c.Assert(hotRegion.AsPeer[1].Count, Equals, 0)
c.Assert(hotRegion.AsPeer[1].TotalBytesRate, Equals, 0.0)
c.Assert(hotRegion.AsPeer[1].StoreByteRate, Equals, load)
c.Assert(hotRegion.AsLeader[1].Count, Equals, 0)
c.Assert(hotRegion.AsLeader[1].TotalBytesRate, Equals, 0.0)
c.Assert(hotRegion.AsLeader[1].StoreByteRate, Equals, 0.0) // write leader sum
}
}

0 comments on commit 24dcebb

Please sign in to comment.