Skip to content

Commit

Permalink
executor: fix wrong peer count and improper handling of partitioned t…
Browse files Browse the repository at this point in the history
…able in `TABLE_STORAGE_STATS` table (#42612) (#42865)

close #42611, close #42619
  • Loading branch information
ti-chi-bot authored Aug 14, 2023
1 parent 266b833 commit a0f1d7e
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 17 deletions.
38 changes: 38 additions & 0 deletions executor/infoschema_cluster_table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -326,3 +326,41 @@ func TestTableStorageStats(t *testing.T) {

tk.MustQuery("select count(1) from information_schema.TABLE_STORAGE_STATS where TABLE_SCHEMA = 'mysql'").Check(testkit.Rows(strconv.Itoa(result)))
}

func TestIssue42619(t *testing.T) {
s := createInfosSchemaClusterTableSuite(t)
mockAddr := s.mockAddr
store := &mockStore{
s.store.(helper.Storage),
mockAddr,
}
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t")

tk.MustExec("create table t (a int, b int, index idx(a))")
tk.MustQuery("SELECT TABLE_SCHEMA, TABLE_NAME, PEER_COUNT, REGION_COUNT, EMPTY_REGION_COUNT, TABLE_SIZE, TABLE_KEYS " +
"FROM information_schema.TABLE_STORAGE_STATS " +
"WHERE TABLE_SCHEMA = 'test' and TABLE_NAME='t'").Check(
testkit.Rows("test t 1 1 1 1 1"))

tk.MustExec(
"CREATE TABLE tp (a int(11) DEFAULT NULL,b int(11) DEFAULT NULL,c int(11) DEFAULT NULL," +
"KEY ia(a), KEY ib(b), KEY ic (c))" +
"PARTITION BY RANGE (`a`)" +
"(PARTITION `p0` VALUES LESS THAN (300)," +
"PARTITION `p1` VALUES LESS THAN (600)," +
"PARTITION `p2` VALUES LESS THAN (900)," +
"PARTITION `p3` VALUES LESS THAN (MAXVALUE))")
tk.MustQuery("SELECT TABLE_SCHEMA, TABLE_NAME, PEER_COUNT, REGION_COUNT, EMPTY_REGION_COUNT, TABLE_SIZE, TABLE_KEYS " +
"FROM information_schema.TABLE_STORAGE_STATS " +
"WHERE TABLE_SCHEMA = 'test'").Check(
testkit.Rows(
"test t 1 1 1 1 1",
"test tp 1 1 1 1 1",
"test tp 1 1 1 1 1",
"test tp 1 1 1 1 1",
"test tp 1 1 1 1 1",
"test tp 1 1 1 1 1",
))
}
46 changes: 29 additions & 17 deletions executor/infoschema_reader.go
Original file line number Diff line number Diff line change
Expand Up @@ -2080,25 +2080,37 @@ func (e *tableStorageStatsRetriever) setDataForTableStorageStats(ctx sessionctx.
rows := make([][]types.Datum, 0, 1024)
count := 0
for e.curTable < len(e.initialTables) && count < 1024 {
table := e.initialTables[e.curTable]
tableID := table.ID
err := e.helper.GetPDRegionStats(tableID, &e.stats, false)
if err != nil {
return nil, err
tbl := e.initialTables[e.curTable]
tblIDs := make([]int64, 0, 1)
tblIDs = append(tblIDs, tbl.ID)
if partInfo := tbl.GetPartitionInfo(); partInfo != nil {
for _, partDef := range partInfo.Definitions {
tblIDs = append(tblIDs, partDef.ID)
}
}
peerCount := len(e.stats.StorePeerCount)

record := types.MakeDatums(
table.db, // TABLE_SCHEMA
table.Name.O, // TABLE_NAME
tableID, // TABLE_ID
peerCount, // TABLE_PEER_COUNT
e.stats.Count, // TABLE_REGION_COUNT
e.stats.EmptyCount, // TABLE_EMPTY_REGION_COUNT
e.stats.StorageSize, // TABLE_SIZE
e.stats.StorageKeys, // TABLE_KEYS
)
rows = append(rows, record)
for _, tableID := range tblIDs {
err := e.helper.GetPDRegionStats(tableID, &e.stats, false)
if err != nil {
return nil, err
}
peerCount := 0
for _, cnt := range e.stats.StorePeerCount {
peerCount += cnt
}

record := types.MakeDatums(
tbl.db, // TABLE_SCHEMA
tbl.Name.O, // TABLE_NAME
tableID, // TABLE_ID
peerCount, // TABLE_PEER_COUNT
e.stats.Count, // TABLE_REGION_COUNT
e.stats.EmptyCount, // TABLE_EMPTY_REGION_COUNT
e.stats.StorageSize, // TABLE_SIZE
e.stats.StorageKeys, // TABLE_KEYS
)
rows = append(rows, record)
}
count++
e.curTable++
}
Expand Down

0 comments on commit a0f1d7e

Please sign in to comment.