From fe4165bd99ab86cf4061eb4f3d7b75cc4ad926d1 Mon Sep 17 00:00:00 2001 From: krzysztofpioro Date: Sat, 21 Mar 2020 23:33:13 +0100 Subject: [PATCH] ucp: migrate dataForTiKVRegionStatus --- executor/builder.go | 1 + executor/infoschema_reader.go | 55 ++++++++++++++++++++++++++++ infoschema/tables.go | 68 ++++------------------------------- 3 files changed, 63 insertions(+), 61 deletions(-) diff --git a/executor/builder.go b/executor/builder.go index d500591fe5c79..b6e34614518d9 100644 --- a/executor/builder.go +++ b/executor/builder.go @@ -1437,6 +1437,7 @@ func (b *executorBuilder) buildMemTable(v *plannercore.PhysicalMemTable) Executo strings.ToLower(infoschema.TableCollationCharacterSetApplicability), strings.ToLower(infoschema.TableProcesslist), strings.ToLower(infoschema.ClusterTableProcesslist), + strings.ToLower(infoschema.TableTiKVRegionStatus), strings.ToLower(infoschema.TableTiKVRegionPeers), strings.ToLower(infoschema.TableTiDBHotRegions), strings.ToLower(infoschema.TableSessionVar), diff --git a/executor/infoschema_reader.go b/executor/infoschema_reader.go index 8d0c0f43a8caa..064f28c8eb045 100644 --- a/executor/infoschema_reader.go +++ b/executor/infoschema_reader.go @@ -107,6 +107,8 @@ func (e *memtableRetriever) retrieve(ctx context.Context, sctx sessionctx.Contex err = e.setDataForClusterProcessList(sctx) case infoschema.TableUserPrivileges: e.setDataFromUserPrivileges(sctx) + case infoschema.TableTiKVRegionStatus: + err = e.setDataForTiKVRegionStatus(sctx) case infoschema.TableTiKVRegionPeers: err = e.setDataForTikVRegionPeers(sctx) case infoschema.TableTiDBHotRegions: @@ -1139,6 +1141,59 @@ func keyColumnUsageInTable(schema *model.DBInfo, table *model.TableInfo) [][]typ return rows } +func (e *memtableRetriever) setDataForTiKVRegionStatus(ctx sessionctx.Context) error { + tikvStore, ok := ctx.GetStore().(tikv.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 + } + allSchemas := ctx.GetSessionVars().TxnCtx.InfoSchema.(infoschema.InfoSchema).AllSchemas() + tableInfos := tikvHelper.GetRegionsTableInfo(regionsInfo, allSchemas) + for _, region := range regionsInfo.Regions { + tableList := tableInfos[region.ID] + if len(tableList) == 0 { + e.setNewTiKVRegionStatusCol(®ion, nil) + } + for _, table := range tableList { + e.setNewTiKVRegionStatusCol(®ion, &table) + } + } + return nil +} + +func (e *memtableRetriever) setNewTiKVRegionStatusCol(region *helper.RegionInfo, table *helper.TableInfo) { + row := make([]types.Datum, len(infoschema.TableTiKVRegionStatusCols)) + row[0].SetInt64(region.ID) + row[1].SetString(region.StartKey, mysql.DefaultCollationName) + row[2].SetString(region.EndKey, mysql.DefaultCollationName) + if table != nil { + row[3].SetInt64(table.Table.ID) + row[4].SetString(table.DB.Name.O, mysql.DefaultCollationName) + row[5].SetString(table.Table.Name.O, mysql.DefaultCollationName) + if table.IsIndex { + row[6].SetInt64(1) + row[7].SetInt64(table.Index.ID) + row[8].SetString(table.Index.Name.O, mysql.DefaultCollationName) + } else { + row[6].SetInt64(0) + } + } + row[9].SetInt64(region.Epoch.ConfVer) + row[10].SetInt64(region.Epoch.Version) + row[11].SetInt64(region.WrittenBytes) + row[12].SetInt64(region.ReadBytes) + row[13].SetInt64(region.ApproximateSize) + row[14].SetInt64(region.ApproximateKeys) + e.rows = append(e.rows, row) +} + func (e *memtableRetriever) setDataForTikVRegionPeers(ctx sessionctx.Context) error { tikvStore, ok := ctx.GetStore().(tikv.Storage) if !ok { diff --git a/infoschema/tables.go b/infoschema/tables.go index 0fea9c074dbbb..8300cd461eeb6 100644 --- a/infoschema/tables.go +++ b/infoschema/tables.go @@ -101,8 +101,9 @@ const ( TableTiDBHotRegions = "TIDB_HOT_REGIONS" tableTiKVStoreStatus = "TIKV_STORE_STATUS" // TableAnalyzeStatus is the string constant of Analyze Status - TableAnalyzeStatus = "ANALYZE_STATUS" - tableTiKVRegionStatus = "TIKV_REGION_STATUS" + TableAnalyzeStatus = "ANALYZE_STATUS" + // TableTiKVRegionStatus is the string constant of infoschema table + TableTiKVRegionStatus = "TIKV_REGION_STATUS" // TableTiKVRegionPeers is the string constant of infoschema table TableTiKVRegionPeers = "TIKV_REGION_PEERS" // TableTiDBServersInfo is the string constant of TiDB server information table. @@ -179,7 +180,7 @@ var tableIDMap = map[string]int64{ TableTiDBHotRegions: autoid.InformationSchemaDBID + 36, tableTiKVStoreStatus: autoid.InformationSchemaDBID + 37, TableAnalyzeStatus: autoid.InformationSchemaDBID + 38, - tableTiKVRegionStatus: autoid.InformationSchemaDBID + 39, + TableTiKVRegionStatus: autoid.InformationSchemaDBID + 39, TableTiKVRegionPeers: autoid.InformationSchemaDBID + 40, TableTiDBServersInfo: autoid.InformationSchemaDBID + 41, TableClusterInfo: autoid.InformationSchemaDBID + 42, @@ -786,7 +787,8 @@ var tableAnalyzeStatusCols = []columnInfo{ {name: "STATE", tp: mysql.TypeVarchar, size: 64}, } -var tableTiKVRegionStatusCols = []columnInfo{ +// TableTiKVRegionStatusCols is TiKV region status mem table columns. +var TableTiKVRegionStatusCols = []columnInfo{ {name: "REGION_ID", tp: mysql.TypeLonglong, size: 21}, {name: "START_KEY", tp: mysql.TypeBlob, size: types.UnspecifiedLength}, {name: "END_KEY", tp: mysql.TypeBlob, size: types.UnspecifiedLength}, @@ -1017,60 +1019,6 @@ var tableSequencesCols = []columnInfo{ {name: "COMMENT", tp: mysql.TypeVarchar, size: 64}, } -func dataForTiKVRegionStatus(ctx sessionctx.Context) (records [][]types.Datum, err error) { - tikvStore, ok := ctx.GetStore().(tikv.Storage) - if !ok { - return nil, 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 nil, err - } - allSchemas := ctx.GetSessionVars().TxnCtx.InfoSchema.(InfoSchema).AllSchemas() - tableInfos := tikvHelper.GetRegionsTableInfo(regionsInfo, allSchemas) - for _, region := range regionsInfo.Regions { - tableList := tableInfos[region.ID] - if len(tableList) == 0 { - records = append(records, newTiKVRegionStatusCol(®ion, nil)) - } - for _, table := range tableList { - row := newTiKVRegionStatusCol(®ion, &table) - records = append(records, row) - } - } - return records, nil -} - -func newTiKVRegionStatusCol(region *helper.RegionInfo, table *helper.TableInfo) []types.Datum { - row := make([]types.Datum, len(tableTiKVRegionStatusCols)) - row[0].SetInt64(region.ID) - row[1].SetString(region.StartKey, mysql.DefaultCollationName) - row[2].SetString(region.EndKey, mysql.DefaultCollationName) - if table != nil { - row[3].SetInt64(table.Table.ID) - row[4].SetString(table.DB.Name.O, mysql.DefaultCollationName) - row[5].SetString(table.Table.Name.O, mysql.DefaultCollationName) - if table.IsIndex { - row[6].SetInt64(1) - row[7].SetInt64(table.Index.ID) - row[8].SetString(table.Index.Name.O, mysql.DefaultCollationName) - } else { - row[6].SetInt64(0) - } - } - row[9].SetInt64(region.Epoch.ConfVer) - row[10].SetInt64(region.Epoch.Version) - row[11].SetInt64(region.WrittenBytes) - row[12].SetInt64(region.ReadBytes) - row[13].SetInt64(region.ApproximateSize) - row[14].SetInt64(region.ApproximateKeys) - return row -} - func dataForTiKVStoreStatus(ctx sessionctx.Context) (records [][]types.Datum, err error) { tikvStore, ok := ctx.GetStore().(tikv.Storage) if !ok { @@ -1393,7 +1341,7 @@ var tableNameToColumns = map[string][]columnInfo{ TableTiDBHotRegions: TableTiDBHotRegionsCols, tableTiKVStoreStatus: tableTiKVStoreStatusCols, TableAnalyzeStatus: tableAnalyzeStatusCols, - tableTiKVRegionStatus: tableTiKVRegionStatusCols, + TableTiKVRegionStatus: TableTiKVRegionStatusCols, TableTiKVRegionPeers: TableTiKVRegionPeersCols, TableTiDBServersInfo: tableTiDBServersInfoCols, TableClusterInfo: tableClusterInfoCols, @@ -1468,8 +1416,6 @@ func (it *infoschemaTable) getRows(ctx sessionctx.Context, cols []*table.Column) case tableTableSpaces: case tableTiKVStoreStatus: fullRows, err = dataForTiKVStoreStatus(ctx) - case tableTiKVRegionStatus: - fullRows, err = dataForTiKVRegionStatus(ctx) case tableTiFlashReplica: fullRows = dataForTableTiFlashReplica(ctx, dbs) }