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

*: add show table regions syntax #10612

Merged
merged 30 commits into from
Jul 10, 2019
Merged
Show file tree
Hide file tree
Changes from 25 commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
3a74339
*: add split table status syntax
crazycs520 Jun 10, 2019
385881c
refactor code and support partition table
crazycs520 Jun 11, 2019
1020fd7
refine region key decode
crazycs520 Jun 11, 2019
8225be8
Merge branch 'master' of https://github.com/pingcap/tidb into split_s…
crazycs520 Jun 11, 2019
70eb948
refine decode region key
crazycs520 Jun 11, 2019
754dc59
change syntax to 'SHOW TABLE table_name [INDEX] [index_name] REGIONS'
crazycs520 Jun 11, 2019
32512b5
fix ci
crazycs520 Jun 11, 2019
f369225
Merge branch 'master' of https://github.com/pingcap/tidb into split_s…
crazycs520 Jun 11, 2019
368551e
fix test
crazycs520 Jun 11, 2019
382bbee
refactor code
crazycs520 Jun 12, 2019
5a935ac
refactor code
crazycs520 Jun 12, 2019
c437714
add test
crazycs520 Jun 12, 2019
7f9ff7e
Merge branch 'master' of https://github.com/pingcap/tidb into split_s…
crazycs520 Jun 13, 2019
78c3ccb
load region after split regions
crazycs520 Jun 14, 2019
cf6a7a8
load region from pd when show table regions
crazycs520 Jun 14, 2019
786139e
add more test
crazycs520 Jun 14, 2019
99479f7
fix ci
crazycs520 Jun 14, 2019
be50ce8
fix test
crazycs520 Jun 14, 2019
2e0576b
address comment
crazycs520 Jun 18, 2019
04c7ae2
address comment
crazycs520 Jun 24, 2019
1385689
Merge branch 'master' of https://github.com/pingcap/tidb into split_s…
crazycs520 Jun 24, 2019
8e6af81
address comment
crazycs520 Jun 24, 2019
db5d28d
Address comment
crazycs520 Jul 2, 2019
9f5e69b
add show table region test for partition table
crazycs520 Jul 2, 2019
6ffdada
add leader store id
crazycs520 Jul 3, 2019
3f5e11b
update parser go.mod
crazycs520 Jul 4, 2019
caa5f10
fix ci check
crazycs520 Jul 4, 2019
cb88721
Merge branch 'master' into split_status
crazycs520 Jul 4, 2019
70b11f3
Merge branch 'master' of https://github.com/pingcap/tidb into split_s…
crazycs520 Jul 10, 2019
bf77cdb
Merge branch 'master' of https://github.com/pingcap/tidb into split_s…
crazycs520 Jul 10, 2019
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
1 change: 1 addition & 0 deletions executor/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ func (b *executorBuilder) buildShow(v *plannercore.Show) Executor {
DBName: model.NewCIStr(v.DBName),
Table: v.Table,
Column: v.Column,
IndexName: v.IndexName,
User: v.User,
Roles: v.Roles,
IfNotExists: v.IfNotExists,
Expand Down
117 changes: 117 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3997,6 +3997,123 @@ func (s *testSuite) TestSplitRegion(c *C) {
tk.MustExec(`split table t by (0),(1000),(1000000)`)
}

func (s *testSuite) TestShowTableRegion(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t_regions1, t_regions")
tk.MustExec("create table t_regions1 (a int key, b int, index idx(b))")
tk.MustExec("create table t_regions (a int key, b int, index idx(b))")

// Test show table regions.
tk.MustExec(`split table t_regions1 by (0)`)
tk.MustExec(`split table t_regions between (-10000) and (10000) regions 4;`)
re := tk.MustQuery("show table t_regions regions")
rows := re.Rows()
// Table t_regions should have 4 regions now.
c.Assert(len(rows), Equals, 4)
c.Assert(len(rows[0]), Equals, 7)
tbl1 := testGetTableByName(c, tk.Se, "test", "t_regions1")
tbl := testGetTableByName(c, tk.Se, "test", "t_regions")
// Check the region start key.
c.Assert(rows[0][1], Matches, fmt.Sprintf("t_%d_.*", tbl1.Meta().ID))
c.Assert(rows[1][1], Equals, fmt.Sprintf("t_%d_r_-5000", tbl.Meta().ID))
c.Assert(rows[2][1], Equals, fmt.Sprintf("t_%d_r_0", tbl.Meta().ID))
c.Assert(rows[3][1], Equals, fmt.Sprintf("t_%d_r_5000", tbl.Meta().ID))

// Test show table index regions.
tk.MustExec(`split table t_regions index idx between (-1000) and (1000) regions 4;`)
re = tk.MustQuery("show table t_regions index idx regions")
rows = re.Rows()
// The index `idx` of table t_regions should have 4 regions now.
c.Assert(len(rows), Equals, 4)
// Check the region start key.
c.Assert(rows[0][1], Equals, fmt.Sprintf("t_%d_i_1_", tbl.Meta().ID))
c.Assert(rows[1][1], Matches, fmt.Sprintf("t_%d_i_1_.*", tbl.Meta().ID))
c.Assert(rows[2][1], Matches, fmt.Sprintf("t_%d_i_1_.*", tbl.Meta().ID))
c.Assert(rows[3][1], Matches, fmt.Sprintf("t_%d_i_1_.*", tbl.Meta().ID))

re = tk.MustQuery("show table t_regions regions")
rows = re.Rows()
// The index `idx` of table t_regions should have 4 regions now.
c.Assert(len(rows), Equals, 7)
// Check the region start key.
c.Assert(rows[0][1], Matches, fmt.Sprintf("t_%d_i_1_.*", tbl.Meta().ID))
c.Assert(rows[1][1], Equals, fmt.Sprintf("t_%d_r_-5000", tbl.Meta().ID))
c.Assert(rows[2][1], Equals, fmt.Sprintf("t_%d_r_0", tbl.Meta().ID))
c.Assert(rows[3][1], Equals, fmt.Sprintf("t_%d_r_5000", tbl.Meta().ID))
c.Assert(rows[4][1], Matches, fmt.Sprintf("t_%d_i_1_.*", tbl.Meta().ID))
c.Assert(rows[5][1], Matches, fmt.Sprintf("t_%d_i_1_.*", tbl.Meta().ID))
c.Assert(rows[6][1], Matches, fmt.Sprintf("t_%d_i_1_.*", tbl.Meta().ID))

// Test unsigned primary key and wait scatter finish.
tk.MustExec("drop table if exists t_regions")
tk.MustExec("create table t_regions (a int unsigned key, b int, index idx(b))")

// Test show table regions.
tk.MustExec(`set @@session.tidb_wait_split_region_finish=1;`)
tk.MustExec(`split table t_regions between (0) and (10000) regions 4;`)
re = tk.MustQuery("show table t_regions regions")
rows = re.Rows()
// Table t_regions should have 4 regions now.
c.Assert(len(rows), Equals, 4)
tbl = testGetTableByName(c, tk.Se, "test", "t_regions")
// Check the region start key.
c.Assert(rows[0][1], Matches, "t_.*")
c.Assert(rows[1][1], Equals, fmt.Sprintf("t_%d_r_2500", tbl.Meta().ID))
c.Assert(rows[2][1], Equals, fmt.Sprintf("t_%d_r_5000", tbl.Meta().ID))
c.Assert(rows[3][1], Equals, fmt.Sprintf("t_%d_r_7500", tbl.Meta().ID))

// Test show table index regions.
tk.MustExec(`split table t_regions index idx between (0) and (1000) regions 4;`)
re = tk.MustQuery("show table t_regions index idx regions")
rows = re.Rows()
// The index `idx` of table t_regions should have 4 regions now.
c.Assert(len(rows), Equals, 4)
// Check the region start key.
c.Assert(rows[0][1], Equals, fmt.Sprintf("t_%d_i_1_", tbl.Meta().ID))
c.Assert(rows[1][1], Matches, fmt.Sprintf("t_%d_i_1_.*", tbl.Meta().ID))
c.Assert(rows[2][1], Matches, fmt.Sprintf("t_%d_i_1_.*", tbl.Meta().ID))
c.Assert(rows[3][1], Matches, fmt.Sprintf("t_%d_i_1_.*", tbl.Meta().ID))

// Test show table regions for partition table when disable split region when create table.
atomic.StoreUint32(&ddl.EnableSplitTableRegion, 0)
tk.MustExec("drop table if exists partition_t;")
tk.MustExec("set @@session.tidb_enable_table_partition = '1';")
tk.MustExec("create table partition_t (a int, b int,index(a)) partition by hash (a) partitions 3")
re = tk.MustQuery("show table partition_t regions")
rows = re.Rows()
// Table t_regions should have 4 regions now.
c.Assert(len(rows), Equals, 1)
tbl = testGetTableByName(c, tk.Se, "test", "partition_t")
partitionDef := tbl.Meta().GetPartitionInfo().Definitions
c.Assert(rows[0][1], Matches, "t_.*")

// Test show table regions for partition table when enable split region when create table.
atomic.StoreUint32(&ddl.EnableSplitTableRegion, 1)
tk.MustExec("drop table if exists partition_t;")
tk.MustExec("create table partition_t (a int, b int,index(a)) partition by hash (a) partitions 3")
re = tk.MustQuery("show table partition_t regions")
rows = re.Rows()
// Table t_regions should have 4 regions now.
c.Assert(len(rows), Equals, 3)
tbl = testGetTableByName(c, tk.Se, "test", "partition_t")
partitionDef = tbl.Meta().GetPartitionInfo().Definitions
c.Assert(rows[0][1], Matches, fmt.Sprintf("t_%d_.*", partitionDef[0].ID))
c.Assert(rows[1][1], Matches, fmt.Sprintf("t_%d_.*", partitionDef[1].ID))
c.Assert(rows[2][1], Matches, fmt.Sprintf("t_%d_.*", partitionDef[2].ID))
atomic.StoreUint32(&ddl.EnableSplitTableRegion, 0)
}

func testGetTableByName(c *C, ctx sessionctx.Context, db, table string) table.Table {
dom := domain.GetDomain(ctx)
// Make sure the table schema is the new schema.
err := dom.Reload()
c.Assert(err, IsNil)
tbl, err := dom.InfoSchema().TableByName(model.NewCIStr(db), model.NewCIStr(table))
c.Assert(err, IsNil)
return tbl
}

func (s *testSuite) TestIssue10435(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
Expand Down
109 changes: 109 additions & 0 deletions executor/show.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ import (
"github.com/pingcap/tidb/privilege"
"github.com/pingcap/tidb/sessionctx/stmtctx"
"github.com/pingcap/tidb/sessionctx/variable"
"github.com/pingcap/tidb/store/tikv"
"github.com/pingcap/tidb/table"
"github.com/pingcap/tidb/types"
"github.com/pingcap/tidb/types/json"
Expand All @@ -60,6 +61,7 @@ type ShowExec struct {
DBName model.CIStr
Table *ast.TableName // Used for showing columns.
Column *ast.ColumnName // Used for `desc table column`.
IndexName model.CIStr // Used for show table regions.
Flag int // Some flag parsed from sql, such as FULL.
Full bool
User *auth.UserIdentity // Used for show grants.
Expand Down Expand Up @@ -178,6 +180,8 @@ func (e *ShowExec) fetchAll() error {
case ast.ShowAnalyzeStatus:
e.fetchShowAnalyzeStatus()
return nil
case ast.ShowRegions:
return e.fetchShowTableRegions()
}
return nil
}
Expand Down Expand Up @@ -1163,3 +1167,108 @@ func (e *ShowExec) appendRow(row []interface{}) {
}
}
}

func (e *ShowExec) fetchShowTableRegions() error {
store := e.ctx.GetStore()
tikvStore, ok := store.(tikv.Storage)
if !ok {
return nil
}
splitStore, ok := store.(splitableStore)
if !ok {
return nil
}

tb, err := e.getTable()
if err != nil {
return errors.Trace(err)
}

// Get table regions from from pd, not from regionCache, because the region cache maybe outdated.
var regions []regionMeta
if len(e.IndexName.L) != 0 {
indexInfo := tb.Meta().FindIndexByName(e.IndexName.L)
if indexInfo == nil {
return plannercore.ErrKeyDoesNotExist.GenWithStackByArgs(e.IndexName, tb.Meta().Name)
}
regions, err = getTableIndexRegions(tb, indexInfo, tikvStore, splitStore)
} else {
regions, err = getTableRegions(tb, tikvStore, splitStore)
}

if err != nil {
return err
}
e.fillRegionsToChunk(regions)
return nil
}

func getTableRegions(tb table.Table, tikvStore tikv.Storage, splitStore splitableStore) ([]regionMeta, error) {
if info := tb.Meta().GetPartitionInfo(); info != nil {
return getPartitionTableRegions(info, tb.(table.PartitionedTable), tikvStore, splitStore)
}
return getPhysicalTableRegions(tb.Meta().ID, tb.Meta(), tikvStore, splitStore, nil)
}

func getTableIndexRegions(tb table.Table, indexInfo *model.IndexInfo, tikvStore tikv.Storage, splitStore splitableStore) ([]regionMeta, error) {
if info := tb.Meta().GetPartitionInfo(); info != nil {
return getPartitionIndexRegions(info, tb.(table.PartitionedTable), indexInfo, tikvStore, splitStore)
}
return getPhysicalIndexRegions(tb.Meta().ID, indexInfo, tikvStore, splitStore, nil)
}

func getPartitionTableRegions(info *model.PartitionInfo, tbl table.PartitionedTable, tikvStore tikv.Storage, splitStore splitableStore) ([]regionMeta, error) {
tiancaiamao marked this conversation as resolved.
Show resolved Hide resolved
regions := make([]regionMeta, 0, len(info.Definitions))
uniqueRegionMap := make(map[uint64]struct{})
for _, def := range info.Definitions {
pid := def.ID
partition := tbl.GetPartition(pid)
partition.GetPhysicalID()
partitionRegions, err := getPhysicalTableRegions(partition.GetPhysicalID(), tbl.Meta(), tikvStore, splitStore, uniqueRegionMap)
if err != nil {
return nil, err
}
regions = append(regions, partitionRegions...)
}
return regions, nil
}

func getPartitionIndexRegions(info *model.PartitionInfo, tbl table.PartitionedTable, indexInfo *model.IndexInfo, tikvStore tikv.Storage, splitStore splitableStore) ([]regionMeta, error) {
var regions []regionMeta
uniqueRegionMap := make(map[uint64]struct{})
for _, def := range info.Definitions {
pid := def.ID
partition := tbl.GetPartition(pid)
partition.GetPhysicalID()
partitionRegions, err := getPhysicalIndexRegions(partition.GetPhysicalID(), indexInfo, tikvStore, splitStore, uniqueRegionMap)
if err != nil {
return nil, err
}
regions = append(regions, partitionRegions...)
}
return regions, nil
}

func (e *ShowExec) fillRegionsToChunk(regions []regionMeta) {
for i := range regions {
e.result.AppendUint64(0, regions[i].region.Id)
e.result.AppendString(1, regions[i].start)
e.result.AppendString(2, regions[i].end)
e.result.AppendUint64(3, regions[i].leaderID)
e.result.AppendUint64(4, regions[i].storeID)

peers := ""
for i, peer := range regions[i].region.Peers {
if i > 0 {
peers += ", "
}
peers += strconv.FormatUint(peer.Id, 10)
}
e.result.AppendString(5, peers)
if regions[i].scattering {
e.result.AppendInt64(6, 1)
} else {
e.result.AppendInt64(6, 0)
}
}
}
Loading