Skip to content

Commit ba4eb8f

Browse files
crazycs520winkyao
authored andcommitted
*: add region read/write bytes info and approximate size/keys in show table regions result. (#11847)
1 parent 8c5f4c7 commit ba4eb8f

File tree

6 files changed

+70
-16
lines changed

6 files changed

+70
-16
lines changed

executor/executor_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -4145,7 +4145,7 @@ func (s *testSuite) TestShowTableRegion(c *C) {
41454145
// 4 regions to store record data.
41464146
// 1 region to store index data.
41474147
c.Assert(len(rows), Equals, 5)
4148-
c.Assert(len(rows[0]), Equals, 7)
4148+
c.Assert(len(rows[0]), Equals, 11)
41494149
tbl := testGetTableByName(c, tk.Se, "test", "t_regions")
41504150
// Check the region start key.
41514151
c.Assert(rows[0][1], Equals, fmt.Sprintf("t_%d_r", tbl.Meta().ID))

executor/show.go

+5
Original file line numberDiff line numberDiff line change
@@ -1288,5 +1288,10 @@ func (e *ShowExec) fillRegionsToChunk(regions []regionMeta) {
12881288
} else {
12891289
e.result.AppendInt64(6, 0)
12901290
}
1291+
1292+
e.result.AppendInt64(7, regions[i].writtenBytes)
1293+
e.result.AppendInt64(8, regions[i].readBytes)
1294+
e.result.AppendInt64(9, regions[i].approximateSize)
1295+
e.result.AppendInt64(10, regions[i].approximateKeys)
12911296
}
12921297
}

executor/split.go

+46-10
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import (
2929
"github.com/pingcap/parser/mysql"
3030
"github.com/pingcap/tidb/kv"
3131
"github.com/pingcap/tidb/sessionctx"
32+
"github.com/pingcap/tidb/store/helper"
3233
"github.com/pingcap/tidb/store/tikv"
3334
"github.com/pingcap/tidb/table/tables"
3435
"github.com/pingcap/tidb/tablecodec"
@@ -439,12 +440,16 @@ func (e *SplitTableRegionExec) getSplitTableKeys() ([][]byte, error) {
439440

440441
// RegionMeta contains a region's peer detail
441442
type regionMeta struct {
442-
region *metapb.Region
443-
leaderID uint64
444-
storeID uint64 // storeID is the store ID of the leader region.
445-
start string
446-
end string
447-
scattering bool
443+
region *metapb.Region
444+
leaderID uint64
445+
storeID uint64 // storeID is the store ID of the leader region.
446+
start string
447+
end string
448+
scattering bool
449+
writtenBytes int64
450+
readBytes int64
451+
approximateSize int64
452+
approximateKeys int64
448453
}
449454

450455
func getPhysicalTableRegions(physicalTableID int64, tableInfo *model.TableInfo, tikvStore tikv.Storage, s kv.SplitableStore, uniqueRegionMap map[uint64]struct{}) ([]regionMeta, error) {
@@ -460,7 +465,7 @@ func getPhysicalTableRegions(physicalTableID int64, tableInfo *model.TableInfo,
460465
}
461466
recordPrefix := tablecodec.GenTableRecordPrefix(physicalTableID)
462467
tablePrefix := tablecodec.GenTablePrefix(physicalTableID)
463-
recordRegions, err := getRegionMeta(recordRegionMetas, uniqueRegionMap, tablePrefix, recordPrefix, nil, physicalTableID, 0)
468+
recordRegions, err := getRegionMeta(tikvStore, recordRegionMetas, uniqueRegionMap, tablePrefix, recordPrefix, nil, physicalTableID, 0)
464469
if err != nil {
465470
return nil, err
466471
}
@@ -477,7 +482,7 @@ func getPhysicalTableRegions(physicalTableID int64, tableInfo *model.TableInfo,
477482
return nil, err
478483
}
479484
indexPrefix := tablecodec.EncodeTableIndexPrefix(physicalTableID, index.ID)
480-
indexRegions, err := getRegionMeta(regionMetas, uniqueRegionMap, tablePrefix, recordPrefix, indexPrefix, physicalTableID, index.ID)
485+
indexRegions, err := getRegionMeta(tikvStore, regionMetas, uniqueRegionMap, tablePrefix, recordPrefix, indexPrefix, physicalTableID, index.ID)
481486
if err != nil {
482487
return nil, err
483488
}
@@ -504,7 +509,7 @@ func getPhysicalIndexRegions(physicalTableID int64, indexInfo *model.IndexInfo,
504509
recordPrefix := tablecodec.GenTableRecordPrefix(physicalTableID)
505510
tablePrefix := tablecodec.GenTablePrefix(physicalTableID)
506511
indexPrefix := tablecodec.EncodeTableIndexPrefix(physicalTableID, indexInfo.ID)
507-
indexRegions, err := getRegionMeta(regions, uniqueRegionMap, tablePrefix, recordPrefix, indexPrefix, physicalTableID, indexInfo.ID)
512+
indexRegions, err := getRegionMeta(tikvStore, regions, uniqueRegionMap, tablePrefix, recordPrefix, indexPrefix, physicalTableID, indexInfo.ID)
508513
if err != nil {
509514
return nil, err
510515
}
@@ -585,7 +590,7 @@ func (d *regionKeyDecoder) decodeRegionKey(key []byte) string {
585590
return fmt.Sprintf("%x", key)
586591
}
587592

588-
func getRegionMeta(regionMetas []*tikv.Region, uniqueRegionMap map[uint64]struct{}, tablePrefix, recordPrefix, indexPrefix []byte, physicalTableID, indexID int64) ([]regionMeta, error) {
593+
func getRegionMeta(tikvStore tikv.Storage, regionMetas []*tikv.Region, uniqueRegionMap map[uint64]struct{}, tablePrefix, recordPrefix, indexPrefix []byte, physicalTableID, indexID int64) ([]regionMeta, error) {
589594
regions := make([]regionMeta, 0, len(regionMetas))
590595
for _, r := range regionMetas {
591596
if _, ok := uniqueRegionMap[r.GetID()]; ok {
@@ -598,6 +603,37 @@ func getRegionMeta(regionMetas []*tikv.Region, uniqueRegionMap map[uint64]struct
598603
storeID: r.GetLeaderStoreID(),
599604
})
600605
}
606+
regions, err := getRegionInfo(tikvStore, regions)
607+
if err != nil {
608+
return regions, err
609+
}
601610
decodeRegionsKey(regions, tablePrefix, recordPrefix, indexPrefix, physicalTableID, indexID)
602611
return regions, nil
603612
}
613+
614+
func getRegionInfo(store tikv.Storage, regions []regionMeta) ([]regionMeta, error) {
615+
// check pd server exists.
616+
etcd, ok := store.(tikv.EtcdBackend)
617+
if !ok {
618+
return regions, nil
619+
}
620+
pdHosts := etcd.EtcdAddrs()
621+
if len(pdHosts) == 0 {
622+
return regions, nil
623+
}
624+
tikvHelper := &helper.Helper{
625+
Store: store,
626+
RegionCache: store.GetRegionCache(),
627+
}
628+
for i := range regions {
629+
regionInfo, err := tikvHelper.GetRegionInfoByID(regions[i].region.Id)
630+
if err != nil {
631+
return nil, err
632+
}
633+
regions[i].writtenBytes = regionInfo.WrittenBytes
634+
regions[i].readBytes = regionInfo.ReadBytes
635+
regions[i].approximateSize = regionInfo.ApproximateSize
636+
regions[i].approximateKeys = regionInfo.ApproximateKeys
637+
}
638+
return regions, nil
639+
}

planner/core/planbuilder.go

+5-1
Original file line numberDiff line numberDiff line change
@@ -1230,14 +1230,18 @@ func buildShowDDLJobsFields() *expression.Schema {
12301230
}
12311231

12321232
func buildTableRegionsSchema() *expression.Schema {
1233-
schema := expression.NewSchema(make([]*expression.Column, 0, 10)...)
1233+
schema := expression.NewSchema(make([]*expression.Column, 0, 11)...)
12341234
schema.Append(buildColumn("", "REGION_ID", mysql.TypeLonglong, 4))
12351235
schema.Append(buildColumn("", "START_KEY", mysql.TypeVarchar, 64))
12361236
schema.Append(buildColumn("", "END_KEY", mysql.TypeVarchar, 64))
12371237
schema.Append(buildColumn("", "LEADER_ID", mysql.TypeLonglong, 4))
12381238
schema.Append(buildColumn("", "LEADER_STORE_ID", mysql.TypeLonglong, 4))
12391239
schema.Append(buildColumn("", "PEERS", mysql.TypeVarchar, 64))
12401240
schema.Append(buildColumn("", "SCATTERING", mysql.TypeTiny, 1))
1241+
schema.Append(buildColumn("", "WRITTEN_BYTES", mysql.TypeLonglong, 4))
1242+
schema.Append(buildColumn("", "READ_BYTES", mysql.TypeLonglong, 4))
1243+
schema.Append(buildColumn("", "APPROXIMATE_SIZE(MB)", mysql.TypeLonglong, 4))
1244+
schema.Append(buildColumn("", "APPROXIMATE_KEYS", mysql.TypeLonglong, 4))
12411245
return schema
12421246
}
12431247

store/helper/helper.go

+8
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"math"
2323
"net/http"
2424
"sort"
25+
"strconv"
2526
"strings"
2627
"time"
2728

@@ -580,6 +581,13 @@ func (h *Helper) GetRegionsInfo() (*RegionsInfo, error) {
580581
return &regionsInfo, err
581582
}
582583

584+
// GetRegionInfoByID gets the region information of the region ID by using PD's api.
585+
func (h *Helper) GetRegionInfoByID(regionID uint64) (*RegionInfo, error) {
586+
var regionInfo RegionInfo
587+
err := h.requestPD("GET", pdapi.RegionByID+strconv.FormatUint(regionID, 10), nil, &regionInfo)
588+
return &regionInfo, err
589+
}
590+
583591
// request PD API, decode the response body into res
584592
func (h *Helper) requestPD(method, uri string, body io.Reader, res interface{}) error {
585593
etcd, ok := h.Store.(tikv.EtcdBackend)

util/pdapi/const.go

+5-4
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@ package pdapi
1515

1616
// The following constants are the APIs of PD server.
1717
const (
18-
HotRead = "/pd/api/v1/hotspot/regions/read"
19-
HotWrite = "/pd/api/v1/hotspot/regions/write"
20-
Regions = "/pd/api/v1/regions"
21-
Stores = "/pd/api/v1/stores"
18+
HotRead = "/pd/api/v1/hotspot/regions/read"
19+
HotWrite = "/pd/api/v1/hotspot/regions/write"
20+
Regions = "/pd/api/v1/regions"
21+
RegionByID = "/pd/api/v1//region/id/"
22+
Stores = "/pd/api/v1/stores"
2223
)

0 commit comments

Comments
 (0)