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

ddl: add a switch for alter partition alter placement #21833

Merged
merged 8 commits into from
Dec 18, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
6 changes: 5 additions & 1 deletion ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -2430,7 +2430,11 @@ func (d *ddl) AlterTable(ctx sessionctx.Context, ident ast.Ident, specs []*ast.A
isAlterTable := true
err = d.RenameTable(ctx, ident, newIdent, isAlterTable)
case ast.AlterTableAlterPartition:
err = d.AlterTablePartition(ctx, ident, spec)
if ctx.GetSessionVars().EnableAlterPlacement {
err = d.AlterTablePartition(ctx, ident, spec)
} else {
err = errors.New("alter table partition alter partition is switched off by tidb_enable_alter_placement")
AilinKid marked this conversation as resolved.
Show resolved Hide resolved
}
case ast.AlterTablePartition:
// Prevent silent succeed if user executes ALTER TABLE x PARTITION BY ...
err = errors.New("alter table partition is unsupported")
Expand Down
35 changes: 31 additions & 4 deletions ddl/placement_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ func (s *testDBSuite1) TestAlterTableAlterPartition(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
defer tk.MustExec("drop table if exists t1")

tk.Se.GetSessionVars().EnableAlterPlacement = true
defer func() {
tk.MustExec("drop table if exists t1")
tk.Se.GetSessionVars().EnableAlterPlacement = false
}()

tk.MustExec(`create table t1 (c int)
PARTITION BY RANGE (c) (
Expand Down Expand Up @@ -290,7 +295,11 @@ add placement policy
func (s *testDBSuite1) TestPlacementPolicyCache(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
defer tk.MustExec("drop table if exists t1")
tk.Se.GetSessionVars().EnableAlterPlacement = true
defer func() {
tk.MustExec("drop table if exists t1")
tk.Se.GetSessionVars().EnableAlterPlacement = false
}()

initTable := func() []string {
bundles := make(map[string]*placement.Bundle)
Expand Down Expand Up @@ -333,7 +342,11 @@ func (s *testSerialDBSuite) TestTxnScopeConstraint(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists t1")
defer tk.MustExec("drop table if exists t1")
tk.Se.GetSessionVars().EnableAlterPlacement = true
defer func() {
tk.MustExec("drop table if exists t1")
tk.Se.GetSessionVars().EnableAlterPlacement = false
}()

tk.MustExec(`create table t1 (c int)
PARTITION BY RANGE (c) (
Expand Down Expand Up @@ -474,7 +487,11 @@ func (s *testDBSuite1) TestAbortTxnIfPlacementChanged(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("drop table if exists tp1")
defer tk.MustExec("drop table if exists tp1")
tk.Se.GetSessionVars().EnableAlterPlacement = true
defer func() {
tk.MustExec("drop table if exists tp1")
tk.Se.GetSessionVars().EnableAlterPlacement = false
}()

tk.MustExec(`create table tp1 (c int)
PARTITION BY RANGE (c) (
Expand All @@ -485,6 +502,11 @@ PARTITION BY RANGE (c) (
c.Assert(err, IsNil)
tk1 := testkit.NewTestKitWithSession(c, s.store, se1)
tk1.MustExec("use test")

tk1.Se.GetSessionVars().EnableAlterPlacement = true
defer func() {
tk1.Se.GetSessionVars().EnableAlterPlacement = false
}()
_, err = tk.Exec(`alter table tp1 alter partition p0
add placement policy
constraints='["+ zone = sh "]'
Expand Down Expand Up @@ -529,6 +551,11 @@ func (s *testDBSuite1) TestGlobalTxnState(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

tk.Se.GetSessionVars().EnableAlterPlacement = true
defer func() {
tk.Se.GetSessionVars().EnableAlterPlacement = false
}()

tk.MustExec(`create table t1 (c int)
PARTITION BY RANGE (c) (
PARTITION p0 VALUES LESS THAN (6),
Expand Down
1 change: 1 addition & 0 deletions session/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -2286,6 +2286,7 @@ var builtinGlobalVariable = []string{
variable.TiDBEnableChangeColumnType,
variable.TiDBEnableChangeMultiSchema,
variable.TiDBEnablePointGetCache,
variable.TiDBEnableAlterPlacement,
variable.TiDBEnableAmendPessimisticTxn,
variable.TiDBMemQuotaApplyCache,
variable.TiDBEnableParallelApply,
Expand Down
6 changes: 6 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,9 @@ type SessionVars struct {
// EnablePointGetCache is used to cache value for point get for read only scenario.
EnablePointGetCache bool

// EnableAlterPlacement indicates whether a user can alter table partition placement rules.
EnableAlterPlacement bool

AilinKid marked this conversation as resolved.
Show resolved Hide resolved
// WaitSplitRegionFinish defines the split region behaviour is sync or async.
WaitSplitRegionFinish bool

Expand Down Expand Up @@ -922,6 +925,7 @@ func NewSessionVars() *SessionVars {
EnableChangeColumnType: DefTiDBChangeColumnType,
EnableChangeMultiSchema: DefTiDBChangeMultiSchema,
EnablePointGetCache: DefTiDBPointGetCache,
EnableAlterPlacement: DefTiDBEnableAlterPlacement,
EnableAmendPessimisticTxn: DefTiDBEnableAmendPessimisticTxn,
PartitionPruneMode: *atomic2.NewString(DefTiDBPartitionPruneMode),
TxnScope: config.GetGlobalConfig().TxnScope,
Expand Down Expand Up @@ -1632,6 +1636,8 @@ func (s *SessionVars) SetSystemVar(name string, val string) error {
s.EnableChangeMultiSchema = TiDBOptOn(val)
case TiDBEnablePointGetCache:
s.EnablePointGetCache = TiDBOptOn(val)
case TiDBEnableAlterPlacement:
s.EnableAlterPlacement = TiDBOptOn(val)
case TiDBEnableAmendPessimisticTxn:
s.EnableAmendPessimisticTxn = TiDBOptOn(val)
case TiDBTxnScope:
Expand Down
1 change: 1 addition & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -1047,6 +1047,7 @@ var defaultSysVars = []*SysVar{
{Scope: ScopeGlobal, Name: TiDBEnableChangeColumnType, Value: BoolToOnOff(DefTiDBChangeColumnType), Type: TypeBool},
{Scope: ScopeGlobal, Name: TiDBEnableChangeMultiSchema, Value: BoolToOnOff(DefTiDBChangeMultiSchema), Type: TypeBool},
{Scope: ScopeGlobal, Name: TiDBEnablePointGetCache, Value: BoolToOnOff(DefTiDBPointGetCache), Type: TypeBool},
{Scope: ScopeGlobal, Name: TiDBEnableAlterPlacement, Value: BoolToOnOff(DefTiDBEnableAlterPlacement), Type: TypeBool},
{Scope: ScopeSession, Name: TiDBForcePriority, Value: mysql.Priority2Str[DefTiDBForcePriority]},
{Scope: ScopeSession, Name: TiDBEnableRadixJoin, Value: BoolToOnOff(DefTiDBUseRadixJoin), Type: TypeBool},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBOptJoinReorderThreshold, Value: strconv.Itoa(DefTiDBOptJoinReorderThreshold), Type: TypeUnsigned, MinValue: 0, MaxValue: 63},
Expand Down
4 changes: 4 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -355,6 +355,9 @@ const (
// TiDBEnablePointGetCache is used to control whether to enable the point get cache for special scenario.
TiDBEnablePointGetCache = "tidb_enable_point_get_cache"

// TiDBEnableAlterPlacement is used to control whether to enable alter table partition.
TiDBEnableAlterPlacement = "tidb_enable_alter_placement"

// tidb_max_delta_schema_count defines the max length of deltaSchemaInfos.
// deltaSchemaInfos is a queue that maintains the history of schema changes.
TiDBMaxDeltaSchemaCount = "tidb_max_delta_schema_count"
Expand Down Expand Up @@ -572,6 +575,7 @@ const (
DefTiDBChangeColumnType = false
DefTiDBChangeMultiSchema = false
DefTiDBPointGetCache = false
DefTiDBEnableAlterPlacement = false
DefTiDBHashAggPartialConcurrency = ConcurrencyUnset
DefTiDBHashAggFinalConcurrency = ConcurrencyUnset
DefTiDBWindowConcurrency = ConcurrencyUnset
Expand Down