Skip to content

Commit

Permalink
*: Add tidb_enable_direct_placement to default forbid direct placem…
Browse files Browse the repository at this point in the history
…ent (#31623)
  • Loading branch information
lcwangchao authored Jan 13, 2022
1 parent 1c7d3c7 commit 794e838
Show file tree
Hide file tree
Showing 11 changed files with 85 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ddl/ddl_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,10 @@ func checkAndNormalizePlacement(ctx sessionctx.Context, placementPolicyRef *mode
}

if directPlacementOpts != nil {
if !ctx.GetSessionVars().EnableDirectPlacement {
return nil, nil, errors.New("Direct placement is disabled")
}

// check the direct placement option compatibility.
if err := checkPolicyValidation(directPlacementOpts); err != nil {
return nil, nil, errors.Trace(err)
Expand Down
21 changes: 21 additions & 0 deletions ddl/placement_policy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,8 @@ func (s *testDBSuite6) TestPlacementPolicy(c *C) {
func (s *testDBSuite6) TestPlacementFollowers(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")

defer tk.MustExec("drop table if exists t1")
defer tk.MustExec("drop placement policy if exists x")

Expand Down Expand Up @@ -459,6 +461,7 @@ func (s *testDBSuite6) TestCreateTableWithPlacementPolicy(c *C) {
clearAllBundles(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("drop table if exists t,t_range_p,t_hash_p,t_list_p")

// Direct placement option: special constraints may be incompatible with common constraint.
Expand Down Expand Up @@ -920,6 +923,7 @@ func (s *testDBSuite6) TestPolicyCacheAndPolicyDependency(c *C) {
func (s *testDBSuite6) TestAlterTablePartitionWithPlacementPolicy(c *C) {
clearAllBundles(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@tidb_enable_direct_placement=1")
defer func() {
tk.MustExec("drop table if exists t1")
tk.MustExec("drop placement policy if exists x")
Expand Down Expand Up @@ -1046,6 +1050,8 @@ func (s *testDBSuite6) TestPolicyInheritance(c *C) {
clearAllBundles(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")

tk.MustExec("drop table if exists t, t0")
tk.MustExec("drop placement policy if exists x")

Expand Down Expand Up @@ -1119,6 +1125,7 @@ func (s *testDBSuite6) TestDatabasePlacement(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("drop database if exists db2")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("set @@tidb_enable_direct_placement=1")

tk.MustExec("create placement policy p1 primary_region='r1' regions='r1'")
defer tk.MustExec("drop placement policy p1")
Expand Down Expand Up @@ -1190,6 +1197,7 @@ func (s *testDBSuite6) TestDropDatabaseGCPlacement(c *C) {
tk.MustExec("drop database if exists db2")

tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")

tk.MustExec("create table t (id int) primary_region='r0' regions='r0'")
defer tk.MustExec("drop table if exists t")
Expand Down Expand Up @@ -1237,6 +1245,7 @@ func (s *testDBSuite6) TestDropTableGCPlacement(c *C) {

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")

tk.MustExec("create table t0 (id int)")
defer tk.MustExec("drop table if exists t0")
Expand Down Expand Up @@ -1274,6 +1283,7 @@ func (s *testDBSuite6) TestAlterTablePlacement(c *C) {
clearAllBundles(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("drop table if exists tp")
tk.MustExec("drop placement policy if exists p1")

Expand Down Expand Up @@ -1373,6 +1383,7 @@ func (s *testDBSuite6) TestDropTablePartitionGCPlacement(c *C) {

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")

tk.MustExec("create table t0 (id int)")
defer tk.MustExec("drop table if exists t0")
Expand Down Expand Up @@ -1423,6 +1434,7 @@ func (s *testDBSuite6) TestAlterTablePartitionPlacement(c *C) {
clearAllBundles(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("drop table if exists tp")
tk.MustExec("drop placement policy if exists p0")
tk.MustExec("drop placement policy if exists p1")
Expand Down Expand Up @@ -1539,6 +1551,7 @@ func (s *testDBSuite6) TestAddPartitionWithPlacement(c *C) {
clearAllBundles(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("drop table if exists tp")
tk.MustExec("drop placement policy if exists p1")

Expand Down Expand Up @@ -1613,6 +1626,8 @@ func (s *testDBSuite6) TestAddPartitionWithPlacement(c *C) {
func (s *testDBSuite6) TestTruncateTableWithPlacement(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")

tk.MustExec("drop table if exists t1, tp")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("drop placement policy if exists p2")
Expand Down Expand Up @@ -1696,6 +1711,7 @@ func (s *testDBSuite6) TestTruncateTableGCWithPlacement(c *C) {

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")

tk.MustExec("create table t0 (id int)")
defer tk.MustExec("drop table if exists t0")
Expand Down Expand Up @@ -1745,6 +1761,8 @@ func (s *testDBSuite6) TestTruncateTableGCWithPlacement(c *C) {
func (s *testDBSuite6) TestTruncateTablePartitionWithPlacement(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")

tk.MustExec("drop table if exists t1, tp")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("drop placement policy if exists p2")
Expand Down Expand Up @@ -1821,6 +1839,7 @@ func (s *testDBSuite6) TestTruncatePartitionGCWithPlacement(c *C) {

tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")

tk.MustExec("create table t0 (id int)")
defer tk.MustExec("drop table if exists t0")
Expand Down Expand Up @@ -1871,6 +1890,7 @@ func (s *testDBSuite6) TestExchangePartitionWithPlacement(c *C) {
clearAllBundles(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@tidb_enable_exchange_partition=1")
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("use test")
tk.MustExec("drop table if exists t1, t2, tp")
tk.MustExec("drop placement policy if exists p1")
Expand Down Expand Up @@ -2034,6 +2054,7 @@ func (s *testDBSuite6) TestPDFail(c *C) {
clearAllBundles(c)
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("drop table if exists t1, t2, tp")

Expand Down
37 changes: 37 additions & 0 deletions ddl/placement_sql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,7 @@ PARTITION BY RANGE (c) (

func (s *testDBSuite6) TestCreateSchemaWithPlacement(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("drop schema if exists SchemaDirectPlacementTest")
tk.MustExec("drop schema if exists SchemaPolicyPlacementTest")
defer func() {
Expand Down Expand Up @@ -329,6 +330,7 @@ func (s *testDBSuite6) TestCreateSchemaWithPlacement(c *C) {

func (s *testDBSuite6) TestAlterDBPlacement(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("drop database if exists TestAlterDB;")
tk.MustExec("create database TestAlterDB;")
tk.MustExec("use TestAlterDB")
Expand Down Expand Up @@ -715,6 +717,7 @@ func (s *testDBSuite6) TestPlacementTiflashCheck(c *C) {
}()

tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("drop table if exists tp")

Expand Down Expand Up @@ -804,3 +807,37 @@ func (s *testDBSuite6) TestPlacementTiflashCheck(c *C) {
"(PARTITION `p0` VALUES LESS THAN (100) /*T![placement] PRIMARY_REGION=\"r3\" REGIONS=\"r3\" */,\n" +
" PARTITION `p1` VALUES LESS THAN (1000))"))
}

func (s *testDBSuite6) TestEnableDirectPlacement(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")

tk.MustExec("drop database if exists db1")
tk.MustExec("drop table if exists tp")
tk.MustExec("drop placement policy if exists p1")
defer func() {
tk.MustExec("drop database if exists db1")
tk.MustExec("drop table if exists tp")
tk.MustExec("drop placement policy if exists p1")
}()
tk.MustExec("create placement policy p1 primary_region='r1' regions='r1,r2'")
tk.MustExec(`CREATE TABLE tp (id INT) placement policy p1 PARTITION BY RANGE (id) (
PARTITION p0 VALUES LESS THAN (100) placement policy p1,
PARTITION p1 VALUES LESS THAN (1000)
)`)
tk.MustExec("create database db1 placement policy p1")

errorSQLs := []string{
"create database db2 followers=1",
"alter database db1 followers=1",
"create table t(a int) followers=1",
"alter table tp followers=1",
"alter table tp partition p0 followers=1",
"alter table tp add partition(partition p2 values less than(10000) followers=1)",
}

for _, sql := range errorSQLs {
err := tk.ExecToErr(sql)
c.Assert(err.Error(), Equals, "Direct placement is disabled")
}
}
1 change: 1 addition & 0 deletions ddl/serial_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -533,6 +533,7 @@ func (s *testSerialSuite) TestCreateTableWithLikeAtTemporaryMode(c *C) {

// Test create table like at temporary mode.
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("drop table if exists temporary_table;")
tk.MustExec("create global temporary table temporary_table (a int, b int,index(a)) on commit delete rows")
tk.MustExec("drop table if exists temporary_table_t1;")
Expand Down
1 change: 1 addition & 0 deletions executor/executor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -593,6 +593,7 @@ func (s *testSuiteP2) TestAdminShowDDLJobs(c *C) {

func (s *testSuiteP2) TestAdminShowDDLJobsInfo(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("create database if not exists test_admin_show_ddl_jobs")
defer tk.MustExec("drop database if exists test_admin_show_ddl_jobs")
tk.MustExec("use test_admin_show_ddl_jobs")
Expand Down
9 changes: 9 additions & 0 deletions executor/show_placement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ import (
func (s *testSuite5) TestShowPlacement(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")

tk.MustExec("drop table if exists t1, t2, t3, t4, db2.t2")
tk.MustExec("drop database if exists db2")
tk.MustExec("drop database if exists db3")
Expand Down Expand Up @@ -117,6 +119,7 @@ func (s *testSuite5) TestShowPlacement(c *C) {
func (s *testSuite5) TestShowPlacementPrivilege(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("drop table if exists t1,t2,t3, db2.t1, db2.t3")
tk.MustExec("drop database if exists db2")
tk.MustExec("drop database if exists db3")
Expand Down Expand Up @@ -184,6 +187,8 @@ func (s *testSuite5) TestShowPlacementPrivilege(c *C) {
func (s *testSuite5) TestShowPlacementForDB(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")

tk.MustExec("drop database if exists db2")
tk.MustExec("drop placement policy if exists p1")

Expand Down Expand Up @@ -214,6 +219,7 @@ func (s *testSuite5) TestShowPlacementForDB(c *C) {
func (s *testSuite5) TestShowPlacementForTableAndPartition(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("drop table if exists t1,t2,t3,t4,db2.t1")
tk.MustExec("drop database if exists db2")
Expand Down Expand Up @@ -298,6 +304,8 @@ func (s *testSuite5) TestShowPlacementForTableAndPartition(c *C) {
func (s *testSuite5) TestShowPlacementForDBPrivilege(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")

tk.MustExec("drop table if exists db2.t1")
tk.MustExec("drop database if exists db2")
tk.MustExec("drop user if exists user1")
Expand Down Expand Up @@ -364,6 +372,7 @@ func (s *testSuite5) TestShowPlacementForDBPrivilege(c *C) {
func (s *testSuite5) TestShowPlacementForTableAndPartitionPrivilege(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")
tk.MustExec("drop placement policy if exists p1")
tk.MustExec("drop table if exists t1,t2,t3,t4,db2.t1")
tk.MustExec("drop database if exists db2")
Expand Down
1 change: 1 addition & 0 deletions executor/show_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1006,6 +1006,7 @@ func (s *testSuite5) TestShowCreateTable(c *C) {
func (s *testSuite5) TestShowCreateTablePlacement(c *C) {
tk := testkit.NewTestKit(c, s.store)
tk.MustExec("use test")
tk.MustExec("set @@tidb_enable_direct_placement=1")
defer tk.MustExec(`DROP TABLE IF EXISTS t`)

// case for direct opts
Expand Down
1 change: 1 addition & 0 deletions privilege/privileges/privileges_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2612,6 +2612,7 @@ func TestInformationSchemaPlacmentRulesPrivileges(t *testing.T) {
defer clean()

tk := testkit.NewTestKit(t, store)
tk.MustExec("set @@tidb_enable_direct_placement=1")

defer func() {
require.True(t, tk.Session().Auth(&auth.UserIdentity{
Expand Down
3 changes: 3 additions & 0 deletions sessionctx/variable/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -748,6 +748,9 @@ type SessionVars struct {
// ignore: Ignore all placement settings in ddl operations
PlacementMode string

// EnableDirectPlacement is used to control whether the direct placement should be enabled
EnableDirectPlacement bool

// WaitSplitRegionFinish defines the split region behaviour is sync or async.
WaitSplitRegionFinish bool

Expand Down
4 changes: 4 additions & 0 deletions sessionctx/variable/sysvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -849,6 +849,10 @@ var defaultSysVars = []*SysVar{
s.EnablePointGetCache = TiDBOptOn(val)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBEnableDirectPlacement, Hidden: true, Value: BoolToOnOff(false), Type: TypeBool, SetSession: func(s *SessionVars, val string) error {
s.EnableDirectPlacement = TiDBOptOn(val)
return nil
}},
{Scope: ScopeGlobal | ScopeSession, Name: TiDBPlacementMode, Value: DefTiDBPlacementMode, Type: TypeEnum, PossibleValues: []string{PlacementModeStrict, PlacementModeIgnore}, SetSession: func(s *SessionVars, val string) error {
s.PlacementMode = val
return nil
Expand Down
3 changes: 3 additions & 0 deletions sessionctx/variable/tidb_vars.go
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,9 @@ const (
// TiDBPlacementMode is used to control the mode for placement
TiDBPlacementMode = "tidb_placement_mode"

// TiDBEnableDirectPlacement is used to control whether the direct placement should be enabled
TiDBEnableDirectPlacement = "tidb_enable_direct_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

0 comments on commit 794e838

Please sign in to comment.