Skip to content

Commit

Permalink
ddl: split partition region when add a new partition (#16537) (#17668)
Browse files Browse the repository at this point in the history
  • Loading branch information
tiancaiamao authored Jun 5, 2020
1 parent e5f723c commit 9b915a7
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 1 deletion.
43 changes: 42 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -1451,7 +1451,6 @@ func (d *ddl) CreateTable(ctx sessionctx.Context, s *ast.CreateTableStmt) (err e
go preSplit()
}
}

if tbInfo.AutoIncID > 1 {
// Default tableAutoIncID base is 0.
// If the first ID is expected to greater than 1, we need to do rebase.
Expand Down Expand Up @@ -1495,6 +1494,37 @@ func (d *ddl) RecoverTable(ctx sessionctx.Context, tbInfo *model.TableInfo, sche
return errors.Trace(err)
}

// preSplitAndScatter performs pre-split and scatter of the table's regions.
func (d *ddl) preSplitAndScatter(ctx sessionctx.Context, tbInfo *model.TableInfo, pi *model.PartitionInfo) {
sp, ok := d.store.(kv.SplitableStore)
if !ok || atomic.LoadUint32(&EnableSplitTableRegion) == 0 {
return
}
var (
preSplit func()
scatterRegion bool
)
val, err := variable.GetGlobalSystemVar(ctx.GetSessionVars(), variable.TiDBScatterRegion)
if err != nil {
logutil.Logger(context.Background()).Warn("[ddl] won't scatter region", zap.Error(err))
} else {
scatterRegion = variable.TiDBOptOn(val)
}
if pi == nil {
pi = tbInfo.GetPartitionInfo()
}
if pi != nil {
preSplit = func() { splitPartitionTableRegion(sp, pi, scatterRegion) }
} else {
preSplit = func() { splitTableRegion(sp, tbInfo, scatterRegion) }
}
if scatterRegion {
preSplit()
} else {
go preSplit()
}
}

func (d *ddl) CreateView(ctx sessionctx.Context, s *ast.CreateViewStmt) (err error) {
ident := ast.Ident{Name: s.ViewName.Name, Schema: s.ViewName.Schema}
is := d.GetInfoSchemaWithInterceptor(ctx)
Expand Down Expand Up @@ -2238,6 +2268,9 @@ func (d *ddl) AddTablePartitions(ctx sessionctx.Context, ident ast.Ident, spec *
}

err = d.doDDLJob(ctx, job)
if err == nil {
d.preSplitAndScatter(ctx, meta, partInfo)
}
err = d.callHookOnChanged(err)
return errors.Trace(err)
}
Expand Down Expand Up @@ -3153,6 +3186,14 @@ func (d *ddl) TruncateTable(ctx sessionctx.Context, ti ast.Ident) error {
}
return errors.Trace(err)
}

oldTblInfo := tb.Meta()
if oldTblInfo.PreSplitRegions > 0 {
if _, tb, err := d.getSchemaAndTableByIdent(ctx, ti); err == nil {
d.preSplitAndScatter(ctx, tb.Meta(), tb.Meta().GetPartitionInfo())
}
}

if !config.TableLockEnabled() {
return nil
}
Expand Down
18 changes: 18 additions & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4122,6 +4122,24 @@ func (s *testSuite) TestShowTableRegion(c *C) {
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))

// Test split partition region when add new partition.
tk.MustExec("drop table if exists partition_t;")
tk.MustExec(`create table partition_t (a int, b int,index(a)) PARTITION BY RANGE (a) (
PARTITION p0 VALUES LESS THAN (10),
PARTITION p1 VALUES LESS THAN (20),
PARTITION p2 VALUES LESS THAN (30));`)
tk.MustExec(`alter table partition_t add partition ( partition p3 values less than (40), partition p4 values less than (50) );`)
re = tk.MustQuery("show table partition_t regions")
rows = re.Rows()
c.Assert(len(rows), Equals, 5)
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))
c.Assert(rows[3][1], Matches, fmt.Sprintf("t_%d_.*", partitionDef[3].ID))
c.Assert(rows[4][1], Matches, fmt.Sprintf("t_%d_.*", partitionDef[4].ID))

// Test pre-split table region when create table.
tk.MustExec("drop table if exists t_pre")
tk.MustExec("create table t_pre (a int, b int) shard_row_id_bits = 2 pre_split_regions=2;")
Expand Down

0 comments on commit 9b915a7

Please sign in to comment.