Skip to content

Commit

Permalink
infoschema: fix inaccurate statistics of TIDB_HOT_REGIONS (#21278) (#…
Browse files Browse the repository at this point in the history
…21319)

Signed-off-by: Ryan Leung <rleungx@gmail.com>
  • Loading branch information
ti-srebot authored Dec 11, 2020
1 parent 251478a commit dbe814c
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 5 deletions.
1 change: 1 addition & 0 deletions store/helper/helper.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,7 @@ type HotTableIndex struct {
func (h *Helper) FetchRegionTableIndex(metrics map[uint64]RegionMetric, allSchemas []*model.DBInfo) ([]HotTableIndex, error) {
hotTables := make([]HotTableIndex, 0, len(metrics))
for regionID, regionMetric := range metrics {
regionMetric := regionMetric
t := HotTableIndex{RegionID: regionID, RegionMetric: &regionMetric}
region, err := h.RegionCache.LocateRegionByID(tikv.NewBackoffer(context.Background(), 500), regionID)
if err != nil {
Expand Down
25 changes: 21 additions & 4 deletions store/helper/helper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"github.com/gorilla/mux"
. "github.com/pingcap/check"
"github.com/pingcap/log"
"github.com/pingcap/parser/model"
"github.com/pingcap/tidb/store/helper"
"github.com/pingcap/tidb/store/mockstore"
"github.com/pingcap/tidb/store/mockstore/mocktikv"
Expand Down Expand Up @@ -63,8 +64,13 @@ func (s *mockStore) TLSConfig() *tls.Config {
func (s *HelperTestSuite) SetUpSuite(c *C) {
go s.mockPDHTTPServer(c)
time.Sleep(100 * time.Millisecond)
mvccStore := mocktikv.MustNewMVCCStore()
mockTikvStore, err := mockstore.NewMockTikvStore(mockstore.WithMVCCStore(mvccStore))
mockTikvStore, err := mockstore.NewMockTikvStore(
mockstore.WithCluster(func() *mocktikv.Cluster {
cluster := mocktikv.NewCluster()
mocktikv.BootstrapWithMultiRegions(cluster, []byte("x"))
return cluster
}()),
)
s.store = &mockStore{
mockTikvStore.(tikv.Storage),
[]string{"127.0.0.1:10100/"},
Expand All @@ -79,7 +85,13 @@ func (s *HelperTestSuite) TestHotRegion(c *C) {
}
regionMetric, err := helper.FetchHotRegion(pdapi.HotRead)
c.Assert(err, IsNil, Commentf("err: %+v", err))
c.Assert(fmt.Sprintf("%v", regionMetric), Equals, "map[1:{100 1 0}]")
dbInfo := &model.DBInfo{
Name: model.NewCIStr("test"),
}
c.Assert(fmt.Sprintf("%v", regionMetric), Equals, "map[3:{100 1 0} 4:{200 2 0}]")
res, err := helper.FetchRegionTableIndex(regionMetric, []*model.DBInfo{dbInfo})
c.Assert(err, IsNil, Commentf("err: %+v", err))
c.Assert(res[0].RegionMetric, Not(Equals), res[1].RegionMetric)
}

func (s *HelperTestSuite) TestTiKVRegionsInfo(c *C) {
Expand Down Expand Up @@ -123,9 +135,14 @@ func (s *HelperTestSuite) mockHotRegionResponse(w http.ResponseWriter, req *http
RegionsStat: []helper.RegionStat{
{
FlowBytes: 100,
RegionID: 1,
RegionID: 3,
HotDegree: 1,
},
{
FlowBytes: 200,
RegionID: 4,
HotDegree: 2,
},
},
}
resp := helper.StoreHotRegionInfos{
Expand Down
2 changes: 1 addition & 1 deletion store/tikv/test_util.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package tikv
import (
"github.com/google/uuid"
"github.com/pingcap/errors"
"github.com/pingcap/pd/client"
pd "github.com/pingcap/pd/client"
"github.com/pingcap/tidb/kv"
)

Expand Down

0 comments on commit dbe814c

Please sign in to comment.