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: make aot more stable #4951

Merged
merged 4 commits into from
May 16, 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: 5 additions & 0 deletions server/cluster/cluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -615,6 +615,11 @@ func (c *RaftCluster) GetSuspectRegions() []uint64 {
return c.coordinator.checkers.GetSuspectRegions()
}

// GetHotStat gets hot stat for test.
func (c *RaftCluster) GetHotStat() *statistics.HotStat {
return c.hotStat
}

// RemoveSuspectRegion removes region from suspect list.
func (c *RaftCluster) RemoveSuspectRegion(id uint64) {
c.coordinator.checkers.RemoveSuspectRegion(id)
Expand Down
50 changes: 50 additions & 0 deletions server/statistics/hot_peer_cache_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

. "github.com/pingcap/check"
"github.com/pingcap/kvproto/pkg/metapb"
"github.com/tikv/pd/pkg/movingaverage"
"github.com/tikv/pd/server/core"
)

Expand Down Expand Up @@ -603,3 +604,52 @@ func BenchmarkCheckRegionFlow(b *testing.B) {
}
}
}

type testMovingAverageCase struct {
report []float64
expect []float64
}

func checkMovingAverage(c *C, testCase *testMovingAverageCase) {
interval := 1 * time.Second
tm := movingaverage.NewTimeMedian(DefaultAotSize, DefaultWriteMfSize, interval)
var results []float64
for _, data := range testCase.report {
tm.Add(data, interval)
results = append(results, tm.Get())
}
c.Assert(results, DeepEquals, testCase.expect)
}

//
func (t *testHotPeerCache) TestUnstableData(c *C) {
cases := []*testMovingAverageCase{
{
report: []float64{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
expect: []float64{1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
},
{
report: []float64{0, 0, 0, 0, 0, 1, 0, 0, 0, 0},
expect: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
},
{
report: []float64{0, 0, 0, 0, 0, 1, 1, 0, 0, 0},
expect: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
},
{
report: []float64{0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
expect: []float64{0, 0, 0, 0, 0, 0, 1, 1, 1, 0},
},
{
report: []float64{0, 0, 0, 0, 0, 1, 0, 1, 0, 0},
expect: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
},
{
report: []float64{0, 0, 0, 0, 0, 1, 0, 1, 0, 1},
expect: []float64{0, 0, 0, 0, 0, 0, 0, 0, 0, 1},
},
}
for i := range cases {
checkMovingAverage(c, cases[i])
}
}
2 changes: 1 addition & 1 deletion server/statistics/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const (
// RegionHeartBeatReportInterval is the heartbeat report interval of a region.
RegionHeartBeatReportInterval = 60
// DefaultAotSize is default size of average over time.
DefaultAotSize = 2
DefaultAotSize = 1
// DefaultWriteMfSize is default size of write median filter.
DefaultWriteMfSize = 5
// DefaultReadMfSize is default size of read median filter.
Expand Down
26 changes: 21 additions & 5 deletions tests/pdctl/hot/hot_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (s *hotTestSuite) TestHot(c *C) {
_, err = pdctl.ExecuteCommand(cmd, args...)
c.Assert(err, IsNil)

hotStoreID := uint64(1)
hotStoreID := store1.Id
count := 0
testHot := func(hotRegionID, hotStoreID uint64, hotType string) {
args = []string{"-u", pdAddr, "hot", hotType}
Expand All @@ -141,16 +141,32 @@ func (s *hotTestSuite) TestHot(c *C) {
regionIDCounter++
switch hotType {
case "read":
pdctl.MustPutRegion(c, cluster, hotRegionID, hotStoreID, []byte("b"), []byte("c"), core.SetReadBytes(1000000000), core.SetReportInterval(reportInterval))
loads := []float64{
statistics.RegionReadBytes: float64(1000000000 * reportInterval),
statistics.RegionReadKeys: float64(1000000000 * reportInterval),
statistics.RegionReadQuery: float64(1000000000 * reportInterval),
statistics.RegionWriteBytes: 0,
statistics.RegionWriteKeys: 0,
statistics.RegionWriteQuery: 0,
}
leader := &metapb.Peer{
Id: 100 + regionIDCounter,
StoreId: hotStoreID,
}
peerInfo := core.NewPeerInfo(leader, loads, reportInterval)
region := core.NewRegionInfo(&metapb.Region{
Id: hotRegionID,
}, leader)
rc.GetHotStat().CheckReadAsync(statistics.NewCheckPeerTask(peerInfo, region))
time.Sleep(5000 * time.Millisecond)
if reportInterval >= statistics.RegionHeartBeatReportInterval {
if reportInterval >= statistics.ReadReportInterval {
count++
}
testHot(hotRegionID, hotStoreID, "read")
case "write":
pdctl.MustPutRegion(c, cluster, hotRegionID, hotStoreID, []byte("c"), []byte("d"), core.SetWrittenBytes(1000000000), core.SetReportInterval(reportInterval))
pdctl.MustPutRegion(c, cluster, hotRegionID, hotStoreID, []byte("c"), []byte("d"), core.SetWrittenBytes(1000000000*reportInterval), core.SetReportInterval(reportInterval))
time.Sleep(5000 * time.Millisecond)
if reportInterval >= statistics.RegionHeartBeatReportInterval {
if reportInterval >= statistics.WriteReportInterval {
count++
}
testHot(hotRegionID, hotStoreID, "write")
Expand Down