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

executor: add retriver for tikv_region_peers (#30657) #32132

Closed
Closed
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
9 changes: 8 additions & 1 deletion executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -1625,6 +1625,14 @@ func (b *executorBuilder) buildMemTable(v *plannercore.PhysicalMemTable) Executo
timeRange: v.QueryTimeRange,
},
}
case strings.ToLower(infoschema.TableTiKVRegionPeers):
return &MemTableReaderExec{
baseExecutor: newBaseExecutor(b.ctx, v.Schema(), v.ID()),
table: v.Table,
retriever: &tikvRegionPeersRetriever{
extractor: v.Extractor.(*plannercore.TikvRegionPeersExtractor),
},
}
case strings.ToLower(infoschema.TableSchemata),
strings.ToLower(infoschema.TableStatistics),
strings.ToLower(infoschema.TableTiDBIndexes),
Expand All @@ -1646,7 +1654,6 @@ func (b *executorBuilder) buildMemTable(v *plannercore.PhysicalMemTable) Executo
strings.ToLower(infoschema.TableProcesslist),
strings.ToLower(infoschema.ClusterTableProcesslist),
strings.ToLower(infoschema.TableTiKVRegionStatus),
strings.ToLower(infoschema.TableTiKVRegionPeers),
strings.ToLower(infoschema.TableTiDBHotRegions),
strings.ToLower(infoschema.TableSessionVar),
strings.ToLower(infoschema.TableConstraints),
Expand Down
59 changes: 0 additions & 59 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,8 +139,6 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex
e.setDataFromUserPrivileges(sctx)
case infoschema.TableTiKVRegionStatus:
err = e.setDataForTiKVRegionStatus(sctx)
case infoschema.TableTiKVRegionPeers:
err = e.setDataForTikVRegionPeers(sctx)
case infoschema.TableTiDBHotRegions:
err = e.setDataForTiDBHotRegions(sctx)
case infoschema.TableConstraints:
Expand Down Expand Up @@ -1494,63 +1492,6 @@ func (e *memtableRetriever) setNewTiKVRegionStatusCol(region *helper.RegionInfo,
e.rows = append(e.rows, row)
}

func (e *memtableRetriever) setDataForTikVRegionPeers(ctx sessionctx.Context) error {
tikvStore, ok := ctx.GetStore().(helper.Storage)
if !ok {
return errors.New("Information about TiKV region status can be gotten only when the storage is TiKV")
}
tikvHelper := &helper.Helper{
Store: tikvStore,
RegionCache: tikvStore.GetRegionCache(),
}
regionsInfo, err := tikvHelper.GetRegionsInfo()
if err != nil {
return err
}
for i := range regionsInfo.Regions {
e.setNewTiKVRegionPeersCols(&regionsInfo.Regions[i])
}
return nil
}

func (e *memtableRetriever) setNewTiKVRegionPeersCols(region *helper.RegionInfo) {
records := make([][]types.Datum, 0, len(region.Peers))
pendingPeerIDSet := set.NewInt64Set()
for _, peer := range region.PendingPeers {
pendingPeerIDSet.Insert(peer.ID)
}
downPeerMap := make(map[int64]int64, len(region.DownPeers))
for _, peerStat := range region.DownPeers {
downPeerMap[peerStat.Peer.ID] = peerStat.DownSec
}
for _, peer := range region.Peers {
row := make([]types.Datum, len(infoschema.TableTiKVRegionPeersCols))
row[0].SetInt64(region.ID)
row[1].SetInt64(peer.ID)
row[2].SetInt64(peer.StoreID)
if peer.IsLearner {
row[3].SetInt64(1)
} else {
row[3].SetInt64(0)
}
if peer.ID == region.Leader.ID {
row[4].SetInt64(1)
} else {
row[4].SetInt64(0)
}
if downSec, ok := downPeerMap[peer.ID]; ok {
row[5].SetString(downPeer, mysql.DefaultCollationName)
row[6].SetInt64(downSec)
} else if pendingPeerIDSet.Exist(peer.ID) {
row[5].SetString(pendingPeer, mysql.DefaultCollationName)
} else {
row[5].SetString(normalPeer, mysql.DefaultCollationName)
}
records = append(records, row)
}
e.rows = append(e.rows, records...)
}

const (
normalPeer = "NORMAL"
pendingPeer = "PENDING"
Expand Down
131 changes: 131 additions & 0 deletions executor/memtable_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -984,3 +984,134 @@ func (e *hotRegionsHistoryRetriver) getHotRegionRowWithSchemaInfo(

return rows, nil
}

type tikvRegionPeersRetriever struct {
dummyCloser
extractor *plannercore.TikvRegionPeersExtractor
retrieved bool
}

func (e *tikvRegionPeersRetriever) retrieve(ctx context.Context, sctx sessionctx.Context) ([][]types.Datum, error) {
if e.extractor.SkipRequest || e.retrieved {
return nil, nil
}
e.retrieved = true
tikvStore, ok := sctx.GetStore().(helper.Storage)
if !ok {
return nil, errors.New("Information about hot region can be gotten only when the storage is TiKV")
}
tikvHelper := &helper.Helper{
Store: tikvStore,
RegionCache: tikvStore.GetRegionCache(),
}

var regionsInfo, regionsInfoByStoreID []helper.RegionInfo
regionMap := make(map[int64]*helper.RegionInfo)
storeMap := make(map[int64]struct{})

if len(e.extractor.StoreIDs) == 0 && len(e.extractor.RegionIDs) == 0 {
regionsInfo, err := tikvHelper.GetRegionsInfo()
if err != nil {
return nil, err
}
return e.packTiKVRegionPeersRows(regionsInfo.Regions, storeMap)
}

for _, storeID := range e.extractor.StoreIDs {
// if a region_id located in 1, 4, 7 store we will get all of them when request any store_id,
// storeMap is used to filter peers on unexpected stores.
storeMap[int64(storeID)] = struct{}{}
storeRegionsInfo, err := tikvHelper.GetStoreRegionsInfo(storeID)
if err != nil {
return nil, err
}
for i, regionInfo := range storeRegionsInfo.Regions {
// regionMap is used to remove dup regions and record the region in regionsInfoByStoreID.
if _, ok := regionMap[regionInfo.ID]; !ok {
regionsInfoByStoreID = append(regionsInfoByStoreID, regionInfo)
regionMap[regionInfo.ID] = &storeRegionsInfo.Regions[i]
}
}
}

if len(e.extractor.RegionIDs) == 0 {
return e.packTiKVRegionPeersRows(regionsInfoByStoreID, storeMap)
}

for _, regionID := range e.extractor.RegionIDs {
regionInfoByStoreID, ok := regionMap[int64(regionID)]
if !ok {
// if there is storeIDs, target region_id is fetched by storeIDs,
// otherwise we need to fetch it from PD.
if len(e.extractor.StoreIDs) == 0 {
regionInfo, err := tikvHelper.GetRegionInfoByID(regionID)
if err != nil {
return nil, err
}
regionsInfo = append(regionsInfo, *regionInfo)
}
} else {
regionsInfo = append(regionsInfo, *regionInfoByStoreID)
}
}

return e.packTiKVRegionPeersRows(regionsInfo, storeMap)
}

func (e *tikvRegionPeersRetriever) isUnexpectedStoreID(storeID int64, storeMap map[int64]struct{}) bool {
if len(e.extractor.StoreIDs) == 0 {
return false
}
if _, ok := storeMap[storeID]; ok {
return false
}
return true
}

func (e *tikvRegionPeersRetriever) packTiKVRegionPeersRows(
regionsInfo []helper.RegionInfo, storeMap map[int64]struct{}) ([][]types.Datum, error) {
var rows [][]types.Datum
for _, region := range regionsInfo {
records := make([][]types.Datum, 0, len(region.Peers))
pendingPeerIDSet := set.NewInt64Set()
for _, peer := range region.PendingPeers {
pendingPeerIDSet.Insert(peer.ID)
}
downPeerMap := make(map[int64]int64, len(region.DownPeers))
for _, peerStat := range region.DownPeers {
downPeerMap[peerStat.Peer.ID] = peerStat.DownSec
}
for _, peer := range region.Peers {
// isUnexpectedStoreID return true if we should filter this peer.
if e.isUnexpectedStoreID(peer.StoreID, storeMap) {
continue
}

row := make([]types.Datum, len(infoschema.TableTiKVRegionPeersCols))
row[0].SetInt64(region.ID)
row[1].SetInt64(peer.ID)
row[2].SetInt64(peer.StoreID)
if peer.IsLearner {
row[3].SetInt64(1)
} else {
row[3].SetInt64(0)
}
if peer.ID == region.Leader.ID {
row[4].SetInt64(1)
} else {
row[4].SetInt64(0)
}
if downSec, ok := downPeerMap[peer.ID]; ok {
row[5].SetString(downPeer, mysql.DefaultCollationName)
row[6].SetInt64(downSec)
} else if pendingPeerIDSet.Exist(peer.ID) {
row[5].SetString(pendingPeer, mysql.DefaultCollationName)
} else {
row[5].SetString(normalPeer, mysql.DefaultCollationName)
}
records = append(records, row)
}
rows = append(rows, records...)
}
return rows, nil
}
Loading