From 512cd434744d640aa2e0c7043544b20cbfe19750 Mon Sep 17 00:00:00 2001 From: sylzd Date: Wed, 8 Sep 2021 20:04:51 +0800 Subject: [PATCH 01/34] init --- ddl/placement_policy_test.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index 95f23e8e22896..1c6dbb63efeb0 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -17,6 +17,7 @@ package ddl_test import ( "context" "fmt" + "github.com/pingcap/tidb/util/testutil" . "github.com/pingcap/check" "github.com/pingcap/parser/model" @@ -375,3 +376,21 @@ func (s *testDBSuite6) TestCreateTableWithPlacementPolicy(c *C) { tk.MustExec("drop table if exists t") tk.MustExec("drop placement policy if exists x") } + +// TODO: sylzd +func (s *testDBSuite6) TestAlterDBWithPlacementPolicy(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("create placement policy x PRIMARY_REGION=\"cn-east-1\";") + tk.MustExec("ALTER DATABASE test DEFAULT PLACEMENT POLICY=`x`;") + tk.MustExec("create table t(a int);") + + tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|", + "t CREATE TABLE `t` (\n"+ + " `a` int(11) DEFAULT NULL\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ + "/*T![placement] PLACEMENT POLICY=`x` */", + )) + + tk.MustExec(`DROP TABLE IF EXISTS t`) +} From cad76e8037ee8d8ad4fb8e92b10b5316af4b77e1 Mon Sep 17 00:00:00 2001 From: sylzd Date: Mon, 27 Sep 2021 20:19:44 +0800 Subject: [PATCH 02/34] init complete --- br/pkg/restore/log_client.go | 2 +- ddl/ddl_api.go | 57 +++++++++++++++++++++++++++++------- ddl/ddl_worker.go | 3 ++ ddl/ddl_worker_test.go | 1 + ddl/placement_policy_test.go | 3 +- ddl/rollingback.go | 3 +- ddl/schema.go | 29 ++++++++++++++++++ go.mod | 2 +- go.sum | 2 ++ infoschema/builder.go | 19 ++++++++++++ util/admin/admin.go | 4 ++- 11 files changed, 109 insertions(+), 16 deletions(-) diff --git a/br/pkg/restore/log_client.go b/br/pkg/restore/log_client.go index 9ddc2cb0df5a6..95c7cdad35815 100644 --- a/br/pkg/restore/log_client.go +++ b/br/pkg/restore/log_client.go @@ -229,7 +229,7 @@ func (l *LogClient) collectDDLFiles(ctx context.Context) ([]string, error) { func (l *LogClient) isDBRelatedDDL(ddl *cdclog.MessageDDL) bool { switch ddl.Type { - case model.ActionDropSchema, model.ActionCreateSchema, model.ActionModifySchemaCharsetAndCollate: + case model.ActionDropSchema, model.ActionCreateSchema, model.ActionModifySchemaCharsetAndCollate, model.ActionModifySchemaDefaultPlacement: return true } return false diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index dea62f86d36e7..09d830860bdf8 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -156,6 +156,8 @@ func (d *ddl) CreateSchemaWithInfo( func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) (err error) { // Resolve target charset and collation from options. var toCharset, toCollate string + var isAlterCharCol bool + var policyName model.CIStr for _, val := range stmt.Options { switch val.Tp { case ast.DatabaseOptionCharset: @@ -164,6 +166,7 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( } else if toCharset != val.Value { return ErrConflictingDeclarations.GenWithStackByArgs(toCharset, val.Value) } + isAlterCharCol = true case ast.DatabaseOptionCollate: info, err := collate.GetCollationByName(val.Value) if err != nil { @@ -175,33 +178,65 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( return ErrConflictingDeclarations.GenWithStackByArgs(toCharset, info.CharsetName) } toCollate = info.Name - + isAlterCharCol = true + case ast.DatabaseOptionPlacementPrimaryRegion, ast.DatabaseOptionPlacementRegions, ast.DatabaseOptionPlacementFollowerCount, ast.DatabaseOptionPlacementVoterCount, ast.DatabaseOptionPlacementLearnerCount, ast.DatabaseOptionPlacementSchedule, ast.DatabaseOptionPlacementConstraints, ast.DatabaseOptionPlacementLeaderConstraints, ast.DatabaseOptionPlacementLearnerConstraints, ast.DatabaseOptionPlacementFollowerConstraints, ast.DatabaseOptionPlacementVoterConstraints: + //TODO: sylzd + fmt.Println(val) + case ast.DatabaseOptionPlacementPolicy: + policyName = model.NewCIStr(val.Value) } } - if toCollate == "" { - if toCollate, err = charset.GetDefaultCollation(toCharset); err != nil { - return errors.Trace(err) + + // TODO sylzd: 这样有点丑,重新组织下char/colate/placement + if isAlterCharCol{ + if toCollate == "" { + if toCollate, err = charset.GetDefaultCollation(toCharset); err != nil { + return errors.Trace(err) + } } - } - // Check if need to change charset/collation. + // Check if need to change charset/collation. + dbName := model.NewCIStr(stmt.Name) + is := d.GetInfoSchemaWithInterceptor(ctx) + dbInfo, ok := is.SchemaByName(dbName) + if !ok { + return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbName.O) + } + if dbInfo.Charset == toCharset && dbInfo.Collate == toCollate { + return nil + } + // Do the DDL job. + job := &model.Job{ + SchemaID: dbInfo.ID, + SchemaName: dbInfo.Name.L, + Type: model.ActionModifySchemaCharsetAndCollate, + BinlogInfo: &model.HistoryInfo{}, + Args: []interface{}{toCharset, toCollate}, + } + err = d.doDDLJob(ctx, job) + err = d.callHookOnChanged(err) + return errors.Trace(err) + } + // TODO: alter placement + // Do the DDL job. dbName := model.NewCIStr(stmt.Name) is := d.GetInfoSchemaWithInterceptor(ctx) dbInfo, ok := is.SchemaByName(dbName) if !ok { return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbName.O) } - if dbInfo.Charset == toCharset && dbInfo.Collate == toCollate { - return nil + policy, ok := ctx.GetInfoSchema().(infoschema.InfoSchema).PolicyByName(policyName) + if !ok { + return errors.Trace(infoschema.ErrPlacementPolicyNotExists.GenWithStackByArgs(policyName)) } + placementPolicyRef := &model.PolicyRefInfo{ID: policy.ID, Name: policyName} - // Do the DDL job. job := &model.Job{ SchemaID: dbInfo.ID, SchemaName: dbInfo.Name.L, - Type: model.ActionModifySchemaCharsetAndCollate, + Type: model.ActionModifySchemaDefaultPlacement, BinlogInfo: &model.HistoryInfo{}, - Args: []interface{}{toCharset, toCollate}, + Args: []interface{}{placementPolicyRef}, } err = d.doDDLJob(ctx, job) err = d.callHookOnChanged(err) diff --git a/ddl/ddl_worker.go b/ddl/ddl_worker.go index a7c253b5ddbdf..53cf76da88edf 100644 --- a/ddl/ddl_worker.go +++ b/ddl/ddl_worker.go @@ -744,6 +744,9 @@ func (w *worker) runDDLJob(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, ver, err = onModifySchemaCharsetAndCollate(t, job) case model.ActionDropSchema: ver, err = onDropSchema(d, t, job) + case model.ActionModifySchemaDefaultPlacement: + // TODO: sylzd + ver, err = onModifySchemaDefaultPlacement(t, job) case model.ActionCreateTable: ver, err = onCreateTable(d, t, job) case model.ActionRepairTable: diff --git a/ddl/ddl_worker_test.go b/ddl/ddl_worker_test.go index 70e34f92623d7..b1bd1fb1c1ddc 100644 --- a/ddl/ddl_worker_test.go +++ b/ddl/ddl_worker_test.go @@ -623,6 +623,7 @@ func buildCancelJobTests(firstID int64) []testCancelJob { {act: model.ActionTruncateTablePartition, jobIDs: []int64{firstID + 29}, cancelRetErrs: []error{admin.ErrCancelFinishedDDLJob.GenWithStackByArgs(firstID + 29)}, cancelState: model.StatePublic}, {act: model.ActionModifySchemaCharsetAndCollate, jobIDs: []int64{firstID + 31}, cancelRetErrs: noErrs, cancelState: model.StateNone}, {act: model.ActionModifySchemaCharsetAndCollate, jobIDs: []int64{firstID + 32}, cancelRetErrs: []error{admin.ErrCancelFinishedDDLJob.GenWithStackByArgs(firstID + 32)}, cancelState: model.StatePublic}, + // TODO: sylzd model.ActionModifySchemaDefaultPlacement {act: model.ActionAddPrimaryKey, jobIDs: []int64{firstID + 33}, cancelRetErrs: noErrs, cancelState: model.StateDeleteOnly}, {act: model.ActionAddPrimaryKey, jobIDs: []int64{firstID + 34}, cancelRetErrs: noErrs, cancelState: model.StateWriteOnly}, diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index 2be8acf1a5739..8d8153a4720eb 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -378,7 +378,8 @@ func (s *testDBSuite6) TestAlterDBWithPlacementPolicy(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("create placement policy x PRIMARY_REGION=\"cn-east-1\";") - tk.MustExec("ALTER DATABASE test DEFAULT PLACEMENT POLICY=`x`;") + tk.MustExec("ALTER DATABASE test PLACEMENT POLICY=`x`;") +// tk.MustExec("ALTER DATABASE test default PLACEMENT POLICY=`x`;") tk.MustExec("create table t(a int);") tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|", diff --git a/ddl/rollingback.go b/ddl/rollingback.go index fbc48f4f69e5f..f2780f5d4f1e8 100644 --- a/ddl/rollingback.go +++ b/ddl/rollingback.go @@ -476,7 +476,8 @@ func convertJob2RollbackJob(w *worker, d *ddlCtx, t *meta.Meta, job *model.Job) model.ActionModifyTableCharsetAndCollate, model.ActionTruncateTablePartition, model.ActionModifySchemaCharsetAndCollate, model.ActionRepairTable, model.ActionModifyTableAutoIdCache, model.ActionAlterIndexVisibility, - model.ActionExchangeTablePartition: + model.ActionExchangeTablePartition, model.ActionModifySchemaDefaultPlacement: + //TODO: sylzd ver, err = cancelOnlyNotHandledJob(job) default: job.State = model.JobStateCancelled diff --git a/ddl/schema.go b/ddl/schema.go index 490da45784c32..d1c26241af624 100644 --- a/ddl/schema.go +++ b/ddl/schema.go @@ -140,6 +140,35 @@ func onModifySchemaCharsetAndCollate(t *meta.Meta, job *model.Job) (ver int64, _ return ver, nil } +func onModifySchemaDefaultPlacement(t *meta.Meta, job *model.Job) (ver int64, _ error) { + var placementPolicyRef *model.PolicyRefInfo + if err := job.DecodeArgs(&placementPolicyRef); err != nil { + job.State = model.JobStateCancelled + return ver, errors.Trace(err) + } + + dbInfo, err := checkSchemaExistAndCancelNotExistJob(t, job) + if err != nil { + return ver, errors.Trace(err) + } + + if dbInfo.PlacementPolicyRef != nil && (dbInfo.PlacementPolicyRef.ID == placementPolicyRef.ID) { + job.FinishDBJob(model.JobStateDone, model.StatePublic, ver, dbInfo) + return ver, nil + } + + dbInfo.PlacementPolicyRef = placementPolicyRef + + if err = t.UpdateDatabase(dbInfo); err != nil { + return ver, errors.Trace(err) + } + if ver, err = updateSchemaVersion(t, job); err != nil { + return ver, errors.Trace(err) + } + job.FinishDBJob(model.JobStateDone, model.StatePublic, ver, dbInfo) + return ver, nil +} + func onDropSchema(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, _ error) { dbInfo, err := checkSchemaExistAndCancelNotExistJob(t, job) if err != nil { diff --git a/go.mod b/go.mod index 9ac6a9ab5dab2..885c643f8da30 100644 --- a/go.mod +++ b/go.mod @@ -49,7 +49,7 @@ require ( github.com/pingcap/fn v0.0.0-20200306044125-d5540d389059 github.com/pingcap/kvproto v0.0.0-20210806074406-317f69fb54b4 github.com/pingcap/log v0.0.0-20210906054005-afc726e70354 - github.com/pingcap/parser v0.0.0-20210917114242-ac711116bdff + github.com/pingcap/parser v0.0.0-20210927074647-16d022b0dae1 github.com/pingcap/sysutil v0.0.0-20210730114356-fcd8a63f68c5 github.com/pingcap/tidb-tools v5.0.3+incompatible github.com/pingcap/tipb v0.0.0-20210802080519-94b831c6db55 diff --git a/go.sum b/go.sum index 4027ab5b4697c..b7d55ab1da8d3 100644 --- a/go.sum +++ b/go.sum @@ -603,6 +603,8 @@ github.com/pingcap/log v0.0.0-20210906054005-afc726e70354/go.mod h1:DWQW5jICDR7U github.com/pingcap/parser v0.0.0-20210525032559-c37778aff307/go.mod h1:xZC8I7bug4GJ5KtHhgAikjTfU4kBv1Sbo3Pf1MZ6lVw= github.com/pingcap/parser v0.0.0-20210917114242-ac711116bdff h1:LiwvvutmyeSkFkdVM09mH6KK+OeDVJzX7WKy9Lf0ri0= github.com/pingcap/parser v0.0.0-20210917114242-ac711116bdff/go.mod h1:+xcMiiZzdIktT/Nqdfm81dkECJ2EPuoAYywd57py4Pk= +github.com/pingcap/parser v0.0.0-20210927074647-16d022b0dae1 h1:4Rk+ti4Rz+vSRff+oLuMFRBe9HKRdaBA089nr2x4FyI= +github.com/pingcap/parser v0.0.0-20210927074647-16d022b0dae1/go.mod h1:+xcMiiZzdIktT/Nqdfm81dkECJ2EPuoAYywd57py4Pk= github.com/pingcap/sysutil v0.0.0-20200206130906-2bfa6dc40bcd/go.mod h1:EB/852NMQ+aRKioCpToQ94Wl7fktV+FNnxf3CX/TTXI= github.com/pingcap/sysutil v0.0.0-20210315073920-cc0985d983a3/go.mod h1:tckvA041UWP+NqYzrJ3fMgC/Hw9wnmQ/tUkp/JaHly8= github.com/pingcap/sysutil v0.0.0-20210730114356-fcd8a63f68c5 h1:7rvAtZe/ZUzOKzgriNPQoBNvleJXBk4z7L3Z47+tS98= diff --git a/infoschema/builder.go b/infoschema/builder.go index f3c69e3e46761..6e01f920fcc07 100644 --- a/infoschema/builder.go +++ b/infoschema/builder.go @@ -54,6 +54,8 @@ func (b *Builder) ApplyDiff(m *meta.Meta, diff *model.SchemaDiff) ([]int64, erro return b.applyDropSchema(diff.SchemaID), nil case model.ActionModifySchemaCharsetAndCollate: return nil, b.applyModifySchemaCharsetAndCollate(m, diff) + case model.ActionModifySchemaDefaultPlacement: + return nil, b.applyModifySchemaDefaultPlacement(m, diff) case model.ActionCreatePlacementPolicy: return nil, b.applyCreatePolicy(m, diff) case model.ActionDropPlacementPolicy: @@ -315,6 +317,23 @@ func (b *Builder) applyModifySchemaCharsetAndCollate(m *meta.Meta, diff *model.S return nil } +// TODO:sylzd 做啥用 +func (b *Builder) applyModifySchemaDefaultPlacement(m *meta.Meta, diff *model.SchemaDiff) error { + di, err := m.GetDatabase(diff.SchemaID) + if err != nil { + return errors.Trace(err) + } + if di == nil { + // This should never happen. + return ErrDatabaseNotExists.GenWithStackByArgs( + fmt.Sprintf("(Schema ID %d)", diff.SchemaID), + ) + } + newDbInfo := b.copySchemaTables(di.Name.L) + newDbInfo.PlacementPolicyRef = di.PlacementPolicyRef + return nil +} + func (b *Builder) applyDropPolicy(PolicyID int64) []int64 { po, ok := b.is.PolicyByID(PolicyID) if !ok { diff --git a/util/admin/admin.go b/util/admin/admin.go index d1e1ca367cddf..eba7e8e69d87e 100644 --- a/util/admin/admin.go +++ b/util/admin/admin.go @@ -116,7 +116,9 @@ func IsJobRollbackable(job *model.Job) bool { model.ActionTruncateTable, model.ActionAddForeignKey, model.ActionDropForeignKey, model.ActionRenameTable, model.ActionModifyTableCharsetAndCollate, model.ActionTruncateTablePartition, - model.ActionModifySchemaCharsetAndCollate, model.ActionRepairTable, model.ActionModifyTableAutoIdCache: + model.ActionModifySchemaCharsetAndCollate, model.ActionRepairTable, model.ActionModifyTableAutoIdCache, + model.ActionModifySchemaDefaultPlacement: + //TODO: sylzd return job.SchemaState == model.StateNone } return true From 147cf14d2802ba072217bc46f2c59d91b7904868 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 12:27:28 +0800 Subject: [PATCH 03/34] complete policy& fix show database --- ddl/placement_policy_test.go | 33 ++++++++++++++++++++++++++++++--- executor/show.go | 10 +++++----- 2 files changed, 35 insertions(+), 8 deletions(-) diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index 8d8153a4720eb..b45a39e68186f 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -373,23 +373,50 @@ func (s *testDBSuite6) TestCreateTableWithPlacementPolicy(c *C) { tk.MustExec("drop placement policy if exists x") } -// TODO: sylzd func (s *testDBSuite6) TestAlterDBWithPlacementPolicy(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") tk.MustExec("create placement policy x PRIMARY_REGION=\"cn-east-1\";") + tk.MustExec("create placement policy y PRIMARY_REGION=\"cn-east-2\";") + tk.MustExec("ALTER DATABASE test PLACEMENT POLICY=`x`;") -// tk.MustExec("ALTER DATABASE test default PLACEMENT POLICY=`x`;") + + // Test for Show Create Database + tk.MustQuery(`show create database test`).Check(testutil.RowsWithSep("|", + "test CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + "/*T![placement] PLACEMENT POLICY=`x` */", + )) + // Test for Alter Placement Policy affect table created. tk.MustExec("create table t(a int);") + tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|", + "t CREATE TABLE `t` (\n"+ + " `a` int(11) DEFAULT NULL\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ + "/*T![placement] PLACEMENT POLICY=`x` */", + )) + // Test for Alter Default Placement Policy, And will not update the old table options. + tk.MustExec("ALTER DATABASE test DEFAULT PLACEMENT POLICY=`y`;") + tk.MustExec("create table t2(a int);") tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|", "t CREATE TABLE `t` (\n"+ " `a` int(11) DEFAULT NULL\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ "/*T![placement] PLACEMENT POLICY=`x` */", )) + tk.MustQuery(`show create table t2`).Check(testutil.RowsWithSep("|", + "t2 CREATE TABLE `t2` (\n"+ + " `a` int(11) DEFAULT NULL\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ + "/*T![placement] PLACEMENT POLICY=`y` */", + )) + + + tk.MustExec(`DROP TABLE IF EXISTS t, t2`) +} + +func (s *testDBSuite6) TestAlterDBWithDirectPlacement(c *C) { - tk.MustExec(`DROP TABLE IF EXISTS t`) } func (s *testDBSuite6) TestDropPlacementPolicyInUse(c *C) { diff --git a/executor/show.go b/executor/show.go index df77147725cf6..4e87ce4beed27 100644 --- a/executor/show.go +++ b/executor/show.go @@ -1069,7 +1069,7 @@ func ConstructResultOfShowCreateTable(ctx sessionctx.Context, tableInfo *model.T } if tableInfo.PlacementPolicyRef != nil { - fmt.Fprintf(buf, " /*T![placement] PLACEMENT POLICY=`%s` */", tableInfo.PlacementPolicyRef.Name.String()) + fmt.Fprintf(buf, " /*T![placement] PLACEMENT POLICY=%s */", stringutil.Escape(tableInfo.PlacementPolicyRef.Name.String(), sqlMode)) } // add direct placement info here @@ -1329,12 +1329,12 @@ func ConstructResultOfShowCreateDatabase(ctx sessionctx.Context, dbInfo *model.D } // MySQL 5.7 always show the charset info but TiDB may ignore it, which makes a slight difference. We keep this // behavior unchanged because it is trivial enough. - if dbInfo.DirectPlacementOpts != nil { - fmt.Fprintf(buf, " %s", dbInfo.DirectPlacementOpts) - } + if dbInfo.PlacementPolicyRef != nil { - fmt.Fprintf(buf, " PLACEMENT POLICY = %s", stringutil.Escape(dbInfo.PlacementPolicyRef.Name.O, sqlMode)) + fmt.Fprintf(buf, " /*T![placement] PLACEMENT POLICY=%s */", stringutil.Escape(dbInfo.PlacementPolicyRef.Name.String(), sqlMode)) } + // add direct placement info here + appendDirectPlacementInfo(dbInfo.DirectPlacementOpts, buf) return nil } From 232d992d313f6a75b1ccc96ac8f2b09badda8570 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 13:03:47 +0800 Subject: [PATCH 04/34] complete alter db direct placement options --- ddl/ddl_api.go | 36 ++++++++++++++++++++++++++++-------- ddl/ddl_worker.go | 1 - ddl/placement_policy_test.go | 17 +++++++++++++++++ ddl/schema.go | 16 +++++++++++----- infoschema/builder.go | 2 +- 5 files changed, 57 insertions(+), 15 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 09d830860bdf8..4c89471111d3e 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -158,6 +158,9 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( var toCharset, toCollate string var isAlterCharCol bool var policyName model.CIStr + var placementPolicyRef *model.PolicyRefInfo + var directPlacementOpts *model.PlacementSettings + for _, val := range stmt.Options { switch val.Tp { case ast.DatabaseOptionCharset: @@ -180,10 +183,20 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( toCollate = info.Name isAlterCharCol = true case ast.DatabaseOptionPlacementPrimaryRegion, ast.DatabaseOptionPlacementRegions, ast.DatabaseOptionPlacementFollowerCount, ast.DatabaseOptionPlacementVoterCount, ast.DatabaseOptionPlacementLearnerCount, ast.DatabaseOptionPlacementSchedule, ast.DatabaseOptionPlacementConstraints, ast.DatabaseOptionPlacementLeaderConstraints, ast.DatabaseOptionPlacementLearnerConstraints, ast.DatabaseOptionPlacementFollowerConstraints, ast.DatabaseOptionPlacementVoterConstraints: - //TODO: sylzd - fmt.Println(val) + if directPlacementOpts == nil { + directPlacementOpts = &model.PlacementSettings{} + } + err := SetDirectPlacementOpt(directPlacementOpts, ast.PlacementOptionType(val.Tp), val.Value, val.UintValue) + if err != nil { + return err + } case ast.DatabaseOptionPlacementPolicy: policyName = model.NewCIStr(val.Value) + policy, ok := ctx.GetInfoSchema().(infoschema.InfoSchema).PolicyByName(policyName) + if !ok { + return errors.Trace(infoschema.ErrPlacementPolicyNotExists.GenWithStackByArgs(policyName)) + } + placementPolicyRef = &model.PolicyRefInfo{ID: policy.ID, Name: policyName} } } @@ -225,18 +238,25 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( if !ok { return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbName.O) } - policy, ok := ctx.GetInfoSchema().(infoschema.InfoSchema).PolicyByName(policyName) - if !ok { - return errors.Trace(infoschema.ErrPlacementPolicyNotExists.GenWithStackByArgs(policyName)) + + + if directPlacementOpts != nil && placementPolicyRef != nil { + return errors.Trace(ErrPlacementPolicyWithDirectOption.GenWithStackByArgs(placementPolicyRef.Name)) + } + + if directPlacementOpts != nil { + // check the direct placement option compatibility. + if err := checkPolicyValidation(directPlacementOpts); err != nil { + return errors.Trace(err) + } } - placementPolicyRef := &model.PolicyRefInfo{ID: policy.ID, Name: policyName} job := &model.Job{ SchemaID: dbInfo.ID, SchemaName: dbInfo.Name.L, Type: model.ActionModifySchemaDefaultPlacement, BinlogInfo: &model.HistoryInfo{}, - Args: []interface{}{placementPolicyRef}, + Args: []interface{}{placementPolicyRef, directPlacementOpts}, } err = d.doDDLJob(ctx, job) err = d.callHookOnChanged(err) @@ -1848,7 +1868,7 @@ func buildTableInfoWithStmt(ctx sessionctx.Context, s *ast.CreateTableStmt, dbCh } if tbInfo.PlacementPolicyRef == nil && tbInfo.DirectPlacementOpts == nil { - // Set the defaults from Schema. Note: they are mutual exlusive! + // Set the defaults from Schema. Note: they are mutual exclusive! if placementPolicyRef != nil { tbInfo.PlacementPolicyRef = placementPolicyRef } else if directPlacementOpts != nil { diff --git a/ddl/ddl_worker.go b/ddl/ddl_worker.go index 53cf76da88edf..30f650019f1b0 100644 --- a/ddl/ddl_worker.go +++ b/ddl/ddl_worker.go @@ -745,7 +745,6 @@ func (w *worker) runDDLJob(d *ddlCtx, t *meta.Meta, job *model.Job) (ver int64, case model.ActionDropSchema: ver, err = onDropSchema(d, t, job) case model.ActionModifySchemaDefaultPlacement: - // TODO: sylzd ver, err = onModifySchemaDefaultPlacement(t, job) case model.ActionCreateTable: ver, err = onCreateTable(d, t, job) diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index b45a39e68186f..aaf62d871220a 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -416,7 +416,24 @@ func (s *testDBSuite6) TestAlterDBWithPlacementPolicy(c *C) { } func (s *testDBSuite6) TestAlterDBWithDirectPlacement(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("use test") + tk.MustExec("ALTER DATABASE test PRIMARY_REGION=\"se\" FOLLOWERS=2;") + + // Test for Show Create Database + tk.MustQuery(`show create database test`).Check(testutil.RowsWithSep("|", + "test CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", + )) + // Test for Alter Placement Rule affect table created. + tk.MustExec("create table t(a int);") + tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|", + "t CREATE TABLE `t` (\n"+ + " `a` int(11) DEFAULT NULL\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ + "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", + )) } func (s *testDBSuite6) TestDropPlacementPolicyInUse(c *C) { diff --git a/ddl/schema.go b/ddl/schema.go index d1c26241af624..573b62f305ba9 100644 --- a/ddl/schema.go +++ b/ddl/schema.go @@ -142,7 +142,8 @@ func onModifySchemaCharsetAndCollate(t *meta.Meta, job *model.Job) (ver int64, _ func onModifySchemaDefaultPlacement(t *meta.Meta, job *model.Job) (ver int64, _ error) { var placementPolicyRef *model.PolicyRefInfo - if err := job.DecodeArgs(&placementPolicyRef); err != nil { + var directPlacementOpts *model.PlacementSettings + if err := job.DecodeArgs(&placementPolicyRef, &directPlacementOpts); err != nil { job.State = model.JobStateCancelled return ver, errors.Trace(err) } @@ -152,12 +153,17 @@ func onModifySchemaDefaultPlacement(t *meta.Meta, job *model.Job) (ver int64, _ return ver, errors.Trace(err) } - if dbInfo.PlacementPolicyRef != nil && (dbInfo.PlacementPolicyRef.ID == placementPolicyRef.ID) { - job.FinishDBJob(model.JobStateDone, model.StatePublic, ver, dbInfo) - return ver, nil + + if placementPolicyRef != nil { + if dbInfo.PlacementPolicyRef != nil && (dbInfo.PlacementPolicyRef.ID == placementPolicyRef.ID) { + job.FinishDBJob(model.JobStateDone, model.StatePublic, ver, dbInfo) + return ver, nil + } + dbInfo.PlacementPolicyRef = placementPolicyRef + } else if directPlacementOpts != nil { + dbInfo.DirectPlacementOpts = directPlacementOpts } - dbInfo.PlacementPolicyRef = placementPolicyRef if err = t.UpdateDatabase(dbInfo); err != nil { return ver, errors.Trace(err) diff --git a/infoschema/builder.go b/infoschema/builder.go index 6e01f920fcc07..fb5af88a3073b 100644 --- a/infoschema/builder.go +++ b/infoschema/builder.go @@ -317,7 +317,6 @@ func (b *Builder) applyModifySchemaCharsetAndCollate(m *meta.Meta, diff *model.S return nil } -// TODO:sylzd 做啥用 func (b *Builder) applyModifySchemaDefaultPlacement(m *meta.Meta, diff *model.SchemaDiff) error { di, err := m.GetDatabase(diff.SchemaID) if err != nil { @@ -331,6 +330,7 @@ func (b *Builder) applyModifySchemaDefaultPlacement(m *meta.Meta, diff *model.Sc } newDbInfo := b.copySchemaTables(di.Name.L) newDbInfo.PlacementPolicyRef = di.PlacementPolicyRef + newDbInfo.DirectPlacementOpts = di.DirectPlacementOpts return nil } From 56981324ed9e00cd8fafd1923b3206762fb928f6 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 13:14:08 +0800 Subject: [PATCH 05/34] fix lint --- ddl/ddl_api.go | 6 ++---- ddl/ddl_worker_test.go | 5 ++++- ddl/placement_policy_test.go | 1 - ddl/rollingback.go | 2 +- ddl/schema.go | 4 +--- util/admin/admin.go | 2 +- 6 files changed, 9 insertions(+), 11 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 4c89471111d3e..7d92383d46a43 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -201,7 +201,7 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( } // TODO sylzd: 这样有点丑,重新组织下char/colate/placement - if isAlterCharCol{ + if isAlterCharCol { if toCollate == "" { if toCollate, err = charset.GetDefaultCollation(toCharset); err != nil { return errors.Trace(err) @@ -230,8 +230,6 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( err = d.callHookOnChanged(err) return errors.Trace(err) } - // TODO: alter placement - // Do the DDL job. dbName := model.NewCIStr(stmt.Name) is := d.GetInfoSchemaWithInterceptor(ctx) dbInfo, ok := is.SchemaByName(dbName) @@ -239,7 +237,6 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbName.O) } - if directPlacementOpts != nil && placementPolicyRef != nil { return errors.Trace(ErrPlacementPolicyWithDirectOption.GenWithStackByArgs(placementPolicyRef.Name)) } @@ -251,6 +248,7 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( } } + // Do the DDL job. job := &model.Job{ SchemaID: dbInfo.ID, SchemaName: dbInfo.Name.L, diff --git a/ddl/ddl_worker_test.go b/ddl/ddl_worker_test.go index b1bd1fb1c1ddc..ec50513ff122f 100644 --- a/ddl/ddl_worker_test.go +++ b/ddl/ddl_worker_test.go @@ -623,7 +623,6 @@ func buildCancelJobTests(firstID int64) []testCancelJob { {act: model.ActionTruncateTablePartition, jobIDs: []int64{firstID + 29}, cancelRetErrs: []error{admin.ErrCancelFinishedDDLJob.GenWithStackByArgs(firstID + 29)}, cancelState: model.StatePublic}, {act: model.ActionModifySchemaCharsetAndCollate, jobIDs: []int64{firstID + 31}, cancelRetErrs: noErrs, cancelState: model.StateNone}, {act: model.ActionModifySchemaCharsetAndCollate, jobIDs: []int64{firstID + 32}, cancelRetErrs: []error{admin.ErrCancelFinishedDDLJob.GenWithStackByArgs(firstID + 32)}, cancelState: model.StatePublic}, - // TODO: sylzd model.ActionModifySchemaDefaultPlacement {act: model.ActionAddPrimaryKey, jobIDs: []int64{firstID + 33}, cancelRetErrs: noErrs, cancelState: model.StateDeleteOnly}, {act: model.ActionAddPrimaryKey, jobIDs: []int64{firstID + 34}, cancelRetErrs: noErrs, cancelState: model.StateWriteOnly}, @@ -662,6 +661,10 @@ func buildCancelJobTests(firstID int64) []testCancelJob { {act: model.ActionDropIndexes, jobIDs: []int64{firstID + 72}, cancelRetErrs: []error{admin.ErrCannotCancelDDLJob.GenWithStackByArgs(firstID + 72)}, cancelState: model.StateWriteOnly}, {act: model.ActionDropIndexes, jobIDs: []int64{firstID + 73}, cancelRetErrs: []error{admin.ErrCannotCancelDDLJob.GenWithStackByArgs(firstID + 73)}, cancelState: model.StateDeleteOnly}, {act: model.ActionDropIndexes, jobIDs: []int64{firstID + 74}, cancelRetErrs: []error{admin.ErrCannotCancelDDLJob.GenWithStackByArgs(firstID + 74)}, cancelState: model.StateWriteReorganization}, + + // for alter db placement + {act: model.ActionModifySchemaDefaultPlacement, jobIDs: []int64{firstID + 75}, cancelRetErrs: noErrs, cancelState: model.StateNone}, + {act: model.ActionModifySchemaDefaultPlacement, jobIDs: []int64{firstID + 76}, cancelRetErrs: []error{admin.ErrCancelFinishedDDLJob.GenWithStackByArgs(firstID + 76)}, cancelState: model.StatePublic}, } return tests diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index aaf62d871220a..df805ded190df 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -411,7 +411,6 @@ func (s *testDBSuite6) TestAlterDBWithPlacementPolicy(c *C) { "/*T![placement] PLACEMENT POLICY=`y` */", )) - tk.MustExec(`DROP TABLE IF EXISTS t, t2`) } diff --git a/ddl/rollingback.go b/ddl/rollingback.go index f2780f5d4f1e8..4789f36d5e9f3 100644 --- a/ddl/rollingback.go +++ b/ddl/rollingback.go @@ -477,7 +477,7 @@ func convertJob2RollbackJob(w *worker, d *ddlCtx, t *meta.Meta, job *model.Job) model.ActionModifySchemaCharsetAndCollate, model.ActionRepairTable, model.ActionModifyTableAutoIdCache, model.ActionAlterIndexVisibility, model.ActionExchangeTablePartition, model.ActionModifySchemaDefaultPlacement: - //TODO: sylzd + //TODO: sylzd ver, err = cancelOnlyNotHandledJob(job) default: job.State = model.JobStateCancelled diff --git a/ddl/schema.go b/ddl/schema.go index 573b62f305ba9..3389cbd16fd4a 100644 --- a/ddl/schema.go +++ b/ddl/schema.go @@ -153,9 +153,8 @@ func onModifySchemaDefaultPlacement(t *meta.Meta, job *model.Job) (ver int64, _ return ver, errors.Trace(err) } - if placementPolicyRef != nil { - if dbInfo.PlacementPolicyRef != nil && (dbInfo.PlacementPolicyRef.ID == placementPolicyRef.ID) { + if dbInfo.PlacementPolicyRef != nil && (dbInfo.PlacementPolicyRef.ID == placementPolicyRef.ID) { job.FinishDBJob(model.JobStateDone, model.StatePublic, ver, dbInfo) return ver, nil } @@ -164,7 +163,6 @@ func onModifySchemaDefaultPlacement(t *meta.Meta, job *model.Job) (ver int64, _ dbInfo.DirectPlacementOpts = directPlacementOpts } - if err = t.UpdateDatabase(dbInfo); err != nil { return ver, errors.Trace(err) } diff --git a/util/admin/admin.go b/util/admin/admin.go index eba7e8e69d87e..cf8694d56119f 100644 --- a/util/admin/admin.go +++ b/util/admin/admin.go @@ -118,7 +118,7 @@ func IsJobRollbackable(job *model.Job) bool { model.ActionModifyTableCharsetAndCollate, model.ActionTruncateTablePartition, model.ActionModifySchemaCharsetAndCollate, model.ActionRepairTable, model.ActionModifyTableAutoIdCache, model.ActionModifySchemaDefaultPlacement: - //TODO: sylzd + //TODO: sylzd return job.SchemaState == model.StateNone } return true From 2af1ad481438a8e4a11479329f9635f7dfed7420 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 14:52:26 +0800 Subject: [PATCH 06/34] fix conflict test case --- ddl/placement_policy_test.go | 22 +++++++++++++--------- ddl/placement_sql_test.go | 4 ++-- 2 files changed, 15 insertions(+), 11 deletions(-) diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index df805ded190df..49b0dbf8bdd38 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -375,15 +375,17 @@ func (s *testDBSuite6) TestCreateTableWithPlacementPolicy(c *C) { func (s *testDBSuite6) TestAlterDBWithPlacementPolicy(c *C) { tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") + tk.MustExec("drop database if exists TestAlterDB;") + tk.MustExec("create database TestAlterDB;") + tk.MustExec("use TestAlterDB") tk.MustExec("create placement policy x PRIMARY_REGION=\"cn-east-1\";") tk.MustExec("create placement policy y PRIMARY_REGION=\"cn-east-2\";") - tk.MustExec("ALTER DATABASE test PLACEMENT POLICY=`x`;") + tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x`;") // Test for Show Create Database - tk.MustQuery(`show create database test`).Check(testutil.RowsWithSep("|", - "test CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ "/*T![placement] PLACEMENT POLICY=`x` */", )) // Test for Alter Placement Policy affect table created. @@ -396,7 +398,7 @@ func (s *testDBSuite6) TestAlterDBWithPlacementPolicy(c *C) { )) // Test for Alter Default Placement Policy, And will not update the old table options. - tk.MustExec("ALTER DATABASE test DEFAULT PLACEMENT POLICY=`y`;") + tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;") tk.MustExec("create table t2(a int);") tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|", "t CREATE TABLE `t` (\n"+ @@ -416,13 +418,15 @@ func (s *testDBSuite6) TestAlterDBWithPlacementPolicy(c *C) { func (s *testDBSuite6) TestAlterDBWithDirectPlacement(c *C) { tk := testkit.NewTestKit(c, s.store) - tk.MustExec("use test") + tk.MustExec("drop database if exists TestAlterDB;") + tk.MustExec("create database TestAlterDB;") + tk.MustExec("use TestAlterDB") - tk.MustExec("ALTER DATABASE test PRIMARY_REGION=\"se\" FOLLOWERS=2;") + tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2;") // Test for Show Create Database - tk.MustQuery(`show create database test`).Check(testutil.RowsWithSep("|", - "test CREATE DATABASE `test` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", )) // Test for Alter Placement Rule affect table created. diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index 3b45e67ffc2e2..8569160ee6a8d 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -732,14 +732,14 @@ func (s *testDBSuite6) TestCreateSchemaWithPlacement(c *C) { }() tk.MustExec(`CREATE SCHEMA SchemaDirectPlacementTest PRIMARY_REGION='nl' REGIONS = "se,nz" FOLLOWERS=3`) - tk.MustQuery("SHOW CREATE SCHEMA schemadirectplacementtest").Check(testkit.Rows("SchemaDirectPlacementTest CREATE DATABASE `SchemaDirectPlacementTest` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ PRIMARY_REGION=\"nl\" REGIONS=\"se,nz\" FOLLOWERS=3")) + tk.MustQuery("SHOW CREATE SCHEMA schemadirectplacementtest").Check(testkit.Rows("SchemaDirectPlacementTest CREATE DATABASE `SchemaDirectPlacementTest` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ /*T![placement] PRIMARY_REGION=\"nl\" REGIONS=\"se,nz\" FOLLOWERS=3 */")) tk.MustExec(`CREATE PLACEMENT POLICY PolicySchemaTest LEADER_CONSTRAINTS = "[+region=nl]" FOLLOWER_CONSTRAINTS="[+region=se]" FOLLOWERS=4 LEARNER_CONSTRAINTS="[+region=be]" LEARNERS=4`) tk.MustExec(`CREATE PLACEMENT POLICY PolicyTableTest LEADER_CONSTRAINTS = "[+region=tl]" FOLLOWER_CONSTRAINTS="[+region=tf]" FOLLOWERS=2 LEARNER_CONSTRAINTS="[+region=tle]" LEARNERS=1`) tk.MustQuery("SHOW PLACEMENT like 'POLICY %PolicySchemaTest%'").Check(testkit.Rows("POLICY PolicySchemaTest LEADER_CONSTRAINTS=\"[+region=nl]\" FOLLOWERS=4 FOLLOWER_CONSTRAINTS=\"[+region=se]\" LEARNERS=4 LEARNER_CONSTRAINTS=\"[+region=be]\"")) tk.MustQuery("SHOW PLACEMENT like 'POLICY %PolicyTableTest%'").Check(testkit.Rows("POLICY PolicyTableTest LEADER_CONSTRAINTS=\"[+region=tl]\" FOLLOWERS=2 FOLLOWER_CONSTRAINTS=\"[+region=tf]\" LEARNERS=1 LEARNER_CONSTRAINTS=\"[+region=tle]\"")) tk.MustExec("CREATE SCHEMA SchemaPolicyPlacementTest PLACEMENT POLICY = `PolicySchemaTest`") - tk.MustQuery("SHOW CREATE SCHEMA SCHEMAPOLICYPLACEMENTTEST").Check(testkit.Rows("SchemaPolicyPlacementTest CREATE DATABASE `SchemaPolicyPlacementTest` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ PLACEMENT POLICY = `PolicySchemaTest`")) + tk.MustQuery("SHOW CREATE SCHEMA SCHEMAPOLICYPLACEMENTTEST").Check(testkit.Rows("SchemaPolicyPlacementTest CREATE DATABASE `SchemaPolicyPlacementTest` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ /*T![placement] PLACEMENT POLICY = `PolicySchemaTest` */")) tk.MustExec(`CREATE TABLE SchemaDirectPlacementTest.UseSchemaDefault (a int unsigned primary key, b varchar(255))`) tk.MustQuery(`SHOW CREATE TABLE SchemaDirectPlacementTest.UseSchemaDefault`).Check(testkit.Rows( From a30b7de0c4b4523e9315cd648d8fc5edad96ed25 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 15:05:10 +0800 Subject: [PATCH 07/34] fix go.sum --- go.sum | 16 +++++----------- 1 file changed, 5 insertions(+), 11 deletions(-) diff --git a/go.sum b/go.sum index 8bddb4b87ee57..4027ab5b4697c 100644 --- a/go.sum +++ b/go.sum @@ -160,7 +160,6 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= -github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/cznic/golex v0.0.0-20181122101858-9c343928389c/go.mod h1:+bmmJDNmKlhWNG+gwWCkaBoTy39Fs+bzRxVBzoTQbIc= github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 h1:iwZdTE0PVqJCos1vaoKsclOGD3ADKpshg3SRtYBbwso= github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM= @@ -378,9 +377,8 @@ github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7 github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= -github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= -github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 h1:THDBEeQ9xZ8JEaCLyLQqXMMdRqNr0QAUJTIkQAUtFjg= @@ -605,8 +603,6 @@ github.com/pingcap/log v0.0.0-20210906054005-afc726e70354/go.mod h1:DWQW5jICDR7U github.com/pingcap/parser v0.0.0-20210525032559-c37778aff307/go.mod h1:xZC8I7bug4GJ5KtHhgAikjTfU4kBv1Sbo3Pf1MZ6lVw= github.com/pingcap/parser v0.0.0-20210917114242-ac711116bdff h1:LiwvvutmyeSkFkdVM09mH6KK+OeDVJzX7WKy9Lf0ri0= github.com/pingcap/parser v0.0.0-20210917114242-ac711116bdff/go.mod h1:+xcMiiZzdIktT/Nqdfm81dkECJ2EPuoAYywd57py4Pk= -github.com/pingcap/parser v0.0.0-20210927074647-16d022b0dae1 h1:4Rk+ti4Rz+vSRff+oLuMFRBe9HKRdaBA089nr2x4FyI= -github.com/pingcap/parser v0.0.0-20210927074647-16d022b0dae1/go.mod h1:+xcMiiZzdIktT/Nqdfm81dkECJ2EPuoAYywd57py4Pk= github.com/pingcap/sysutil v0.0.0-20200206130906-2bfa6dc40bcd/go.mod h1:EB/852NMQ+aRKioCpToQ94Wl7fktV+FNnxf3CX/TTXI= github.com/pingcap/sysutil v0.0.0-20210315073920-cc0985d983a3/go.mod h1:tckvA041UWP+NqYzrJ3fMgC/Hw9wnmQ/tUkp/JaHly8= github.com/pingcap/sysutil v0.0.0-20210730114356-fcd8a63f68c5 h1:7rvAtZe/ZUzOKzgriNPQoBNvleJXBk4z7L3Z47+tS98= @@ -733,8 +729,8 @@ github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2/go.mod h1:2PfK github.com/tidwall/gjson v1.3.5/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls= github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= -github.com/tikv/client-go/v2 v2.0.0-alpha.0.20210926100628-3cc2459779ca h1:aE/y7lkygl716M6eIos95BKjM1mGew+EoLWRQAXDoAE= -github.com/tikv/client-go/v2 v2.0.0-alpha.0.20210926100628-3cc2459779ca/go.mod h1:KwtZXt0JD+bP9bWW2ka0ir3Wp3oTEfZUTh22bs2sI4o= +github.com/tikv/client-go/v2 v2.0.0-alpha.0.20210918071319-6ca8098ae0fe h1:nOeupIzlqbMP/UswMxfqvz2YftFa2x+N13QN0QpyiZs= +github.com/tikv/client-go/v2 v2.0.0-alpha.0.20210918071319-6ca8098ae0fe/go.mod h1:KwtZXt0JD+bP9bWW2ka0ir3Wp3oTEfZUTh22bs2sI4o= github.com/tikv/pd v1.1.0-beta.0.20210323121136-78679e5e209d/go.mod h1:Jw9KG11C/23Rr7DW4XWQ7H5xOgGZo6DFL1OKAF4+Igw= github.com/tikv/pd v1.1.0-beta.0.20210818082359-acba1da0018d h1:AFm1Dzw+QRUevWRfrFp45CPPkuK/zdSWcfxI10z+WVE= github.com/tikv/pd v1.1.0-beta.0.20210818082359-acba1da0018d/go.mod h1:rammPjeZgpvfrQRPkijcx8tlxF1XM5+m6kRXrkDzCAA= @@ -743,9 +739,8 @@ github.com/tklauser/go-sysconf v0.3.4/go.mod h1:Cl2c8ZRWfHD5IrfHo9VN+FX9kCFjIOyV github.com/tklauser/numcpus v0.2.1 h1:ct88eFm+Q7m2ZfXJdan1xYoXKlmwsfP+k88q05KvlZc= github.com/tklauser/numcpus v0.2.1/go.mod h1:9aU+wOc6WjUIZEwWMP62PL/41d65P+iks1gBkr4QyP8= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 h1:LnC5Kc/wtumK+WB441p7ynQJzVuNRJiqddSIE3IlSEQ= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= -github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966 h1:j6JEOq5QWFker+d7mFQYOhjTZonQ7YkLTHm56dbn+yM= -github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/twmb/murmur3 v1.1.3 h1:D83U0XYKcHRYwYIpBKf3Pks91Z0Byda/9SJ8B6EMRcA= github.com/twmb/murmur3 v1.1.3/go.mod h1:Qq/R7NUyOfr65zD+6Q5IHKsJLwP7exErjN6lyyq3OSQ= github.com/uber/jaeger-client-go v2.22.1+incompatible h1:NHcubEkVbahf9t3p75TOCR83gdUHXjRJvjoBh1yACsM= @@ -803,9 +798,8 @@ go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0= go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg= go.etcd.io/etcd v0.5.0-alpha.5.0.20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg= +go.etcd.io/etcd v0.5.0-alpha.5.0.20200824191128-ae9734ed278b h1:3kC4J3eQF6p1UEfQTkC67eEeb3rTk+shQqdX6tFyq9Q= go.etcd.io/etcd v0.5.0-alpha.5.0.20200824191128-ae9734ed278b/go.mod h1:yVHk9ub3CSBatqGNg7GRmsnfLWtoW60w4eDYfh7vHDg= -go.etcd.io/etcd v0.5.0-alpha.5.0.20210512015243-d19fbe541bf9 h1:MNsY1TIsWLNCMT4DzZjFOxbDKfSoULYP0OFjJ8dSxts= -go.etcd.io/etcd v0.5.0-alpha.5.0.20210512015243-d19fbe541bf9/go.mod h1:q+i20RPAmay+xq8LJ3VMOhXCNk4YCk3V7QP91meFavw= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= From f057d0563dcf3873a122cf56b1da926ed9f6edb4 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 15:07:51 +0800 Subject: [PATCH 08/34] fix go.sum --- go.sum | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/go.sum b/go.sum index 4027ab5b4697c..145d1d5ecbdf5 100644 --- a/go.sum +++ b/go.sum @@ -160,6 +160,7 @@ github.com/cpuguy83/go-md2man/v2 v2.0.0-20190314233015-f79a8a8ca69d/go.mod h1:ma github.com/cpuguy83/go-md2man/v2 v2.0.0/go.mod h1:maD7wRr/U5Z6m/iR4s+kqSMx2CaBsrgA7czyZG/E6dU= github.com/creack/pty v1.1.7/go.mod h1:lj5s0c3V2DBrqTV7llrYr5NG6My20zk30Fl46Y7DoTY= github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= +github.com/creack/pty v1.1.11/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E= github.com/cznic/golex v0.0.0-20181122101858-9c343928389c/go.mod h1:+bmmJDNmKlhWNG+gwWCkaBoTy39Fs+bzRxVBzoTQbIc= github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548 h1:iwZdTE0PVqJCos1vaoKsclOGD3ADKpshg3SRtYBbwso= github.com/cznic/mathutil v0.0.0-20181122101859-297441e03548/go.mod h1:e6NPNENfs9mPDVNRekM7lKScauxd5kXTr1Mfyig6TDM= @@ -377,8 +378,9 @@ github.com/gorilla/mux v1.7.4/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB7 github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI= github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So= github.com/gorilla/websocket v0.0.0-20170926233335-4201258b820c/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= -github.com/gorilla/websocket v1.4.0 h1:WDFjx/TMzVgy9VdMMQi2K2Emtwi2QcUQsztZ/zLaH/Q= github.com/gorilla/websocket v1.4.0/go.mod h1:E7qHFY5m1UJ88s3WnNqhKjPHQ0heANvMoAMk2YaljkQ= +github.com/gorilla/websocket v1.4.2 h1:+/TMaTYc4QFitKJxsQ7Yye35DkWvkdLcvGKqM+x0Ufc= +github.com/gorilla/websocket v1.4.2/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE= github.com/grpc-ecosystem/go-grpc-middleware v1.0.0/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-middleware v1.0.1-0.20190118093823-f849b5445de4/go.mod h1:FiyG127CGDf3tlThmgyCl78X/SZQqEOJBCDaAfeWzPs= github.com/grpc-ecosystem/go-grpc-middleware v1.1.0 h1:THDBEeQ9xZ8JEaCLyLQqXMMdRqNr0QAUJTIkQAUtFjg= @@ -729,8 +731,8 @@ github.com/tiancaiamao/appdash v0.0.0-20181126055449-889f96f722a2/go.mod h1:2PfK github.com/tidwall/gjson v1.3.5/go.mod h1:P256ACg0Mn+j1RXIDXoss50DeIABTYK1PULOJHhxOls= github.com/tidwall/match v1.0.1/go.mod h1:LujAq0jyVjBy028G1WhWfIzbpQfMO8bBZ6Tyb0+pL9E= github.com/tidwall/pretty v1.0.0/go.mod h1:XNkn88O1ChpSDQmQeStsy+sBenx6DDtFZJxhVysOjyk= -github.com/tikv/client-go/v2 v2.0.0-alpha.0.20210918071319-6ca8098ae0fe h1:nOeupIzlqbMP/UswMxfqvz2YftFa2x+N13QN0QpyiZs= -github.com/tikv/client-go/v2 v2.0.0-alpha.0.20210918071319-6ca8098ae0fe/go.mod h1:KwtZXt0JD+bP9bWW2ka0ir3Wp3oTEfZUTh22bs2sI4o= +github.com/tikv/client-go/v2 v2.0.0-alpha.0.20210926100628-3cc2459779ca h1:aE/y7lkygl716M6eIos95BKjM1mGew+EoLWRQAXDoAE= +github.com/tikv/client-go/v2 v2.0.0-alpha.0.20210926100628-3cc2459779ca/go.mod h1:KwtZXt0JD+bP9bWW2ka0ir3Wp3oTEfZUTh22bs2sI4o= github.com/tikv/pd v1.1.0-beta.0.20210323121136-78679e5e209d/go.mod h1:Jw9KG11C/23Rr7DW4XWQ7H5xOgGZo6DFL1OKAF4+Igw= github.com/tikv/pd v1.1.0-beta.0.20210818082359-acba1da0018d h1:AFm1Dzw+QRUevWRfrFp45CPPkuK/zdSWcfxI10z+WVE= github.com/tikv/pd v1.1.0-beta.0.20210818082359-acba1da0018d/go.mod h1:rammPjeZgpvfrQRPkijcx8tlxF1XM5+m6kRXrkDzCAA= @@ -739,8 +741,9 @@ github.com/tklauser/go-sysconf v0.3.4/go.mod h1:Cl2c8ZRWfHD5IrfHo9VN+FX9kCFjIOyV github.com/tklauser/numcpus v0.2.1 h1:ct88eFm+Q7m2ZfXJdan1xYoXKlmwsfP+k88q05KvlZc= github.com/tklauser/numcpus v0.2.1/go.mod h1:9aU+wOc6WjUIZEwWMP62PL/41d65P+iks1gBkr4QyP8= github.com/tmc/grpc-websocket-proxy v0.0.0-20170815181823-89b8d40f7ca8/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= -github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5 h1:LnC5Kc/wtumK+WB441p7ynQJzVuNRJiqddSIE3IlSEQ= github.com/tmc/grpc-websocket-proxy v0.0.0-20190109142713-0ad062ec5ee5/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= +github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966 h1:j6JEOq5QWFker+d7mFQYOhjTZonQ7YkLTHm56dbn+yM= +github.com/tmc/grpc-websocket-proxy v0.0.0-20200427203606-3cfed13b9966/go.mod h1:ncp9v5uamzpCO7NfCPTXjqaC+bZgJeR0sMTm6dMHP7U= github.com/twmb/murmur3 v1.1.3 h1:D83U0XYKcHRYwYIpBKf3Pks91Z0Byda/9SJ8B6EMRcA= github.com/twmb/murmur3 v1.1.3/go.mod h1:Qq/R7NUyOfr65zD+6Q5IHKsJLwP7exErjN6lyyq3OSQ= github.com/uber/jaeger-client-go v2.22.1+incompatible h1:NHcubEkVbahf9t3p75TOCR83gdUHXjRJvjoBh1yACsM= @@ -798,8 +801,9 @@ go.etcd.io/bbolt v1.3.5 h1:XAzx9gjCb0Rxj7EoqcClPD1d5ZBxZJk0jbuoPHenBt0= go.etcd.io/bbolt v1.3.5/go.mod h1:G5EMThwa9y8QZGBClrRx5EY+Yw9kAhnjy3bSjsnlVTQ= go.etcd.io/etcd v0.0.0-20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg= go.etcd.io/etcd v0.5.0-alpha.5.0.20191023171146-3cf2f69b5738/go.mod h1:dnLIgRNXwCJa5e+c6mIZCrds/GIG4ncV9HhK5PX7jPg= -go.etcd.io/etcd v0.5.0-alpha.5.0.20200824191128-ae9734ed278b h1:3kC4J3eQF6p1UEfQTkC67eEeb3rTk+shQqdX6tFyq9Q= go.etcd.io/etcd v0.5.0-alpha.5.0.20200824191128-ae9734ed278b/go.mod h1:yVHk9ub3CSBatqGNg7GRmsnfLWtoW60w4eDYfh7vHDg= +go.etcd.io/etcd v0.5.0-alpha.5.0.20210512015243-d19fbe541bf9 h1:MNsY1TIsWLNCMT4DzZjFOxbDKfSoULYP0OFjJ8dSxts= +go.etcd.io/etcd v0.5.0-alpha.5.0.20210512015243-d19fbe541bf9/go.mod h1:q+i20RPAmay+xq8LJ3VMOhXCNk4YCk3V7QP91meFavw= go.opencensus.io v0.21.0/go.mod h1:mSImk1erAIZhrmZN+AvHh14ztQfjbGwt4TtuofqLduU= go.opencensus.io v0.22.0/go.mod h1:+kGneAE2xo2IficOXnaByMWTGM9T73dGwxeWcUqIpI8= go.opencensus.io v0.22.2/go.mod h1:yxeiOL68Rb0Xd1ddK5vPZ/oVn4vY4Ynel7k9FzqtOIw= From 9160642010dbc2ca5be5e4afa5193d2ff646cacc Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 16:59:40 +0800 Subject: [PATCH 09/34] fix hybrid logic --- ddl/ddl_api.go | 12 +++++++-- ddl/placement_policy_test.go | 47 ++++++++++++++++++++++++++---------- ddl/rollingback.go | 1 - ddl/schema.go | 16 ++++++------ go.sum | 2 ++ util/admin/admin.go | 5 ++-- 6 files changed, 56 insertions(+), 27 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 9f055068cf281..71d2f557249e3 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -237,8 +237,16 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbName.O) } - if directPlacementOpts != nil && placementPolicyRef != nil { - return errors.Trace(ErrPlacementPolicyWithDirectOption.GenWithStackByArgs(placementPolicyRef.Name)) + if (directPlacementOpts != nil && placementPolicyRef != nil) || + (directPlacementOpts != nil && dbInfo.PlacementPolicyRef != nil) || + (placementPolicyRef != nil && dbInfo.DirectPlacementOpts != nil) { + var policyName model.CIStr + if placementPolicyRef != nil { + policyName = placementPolicyRef.Name + } else { + policyName = dbInfo.PlacementPolicyRef.Name + } + return errors.Trace(ErrPlacementPolicyWithDirectOption.GenWithStackByArgs(policyName)) } if directPlacementOpts != nil { diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index 21f7726b8fa84..42d575830272b 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -373,7 +373,7 @@ func (s *testDBSuite6) TestCreateTableWithPlacementPolicy(c *C) { tk.MustExec("drop placement policy if exists x") } -func (s *testDBSuite6) TestAlterDBWithPlacementPolicy(c *C) { +func (s *testDBSuite6) TestAlterDBPlacement(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("drop database if exists TestAlterDB;") tk.MustExec("create database TestAlterDB;") @@ -381,8 +381,8 @@ func (s *testDBSuite6) TestAlterDBWithPlacementPolicy(c *C) { tk.MustExec("create placement policy x PRIMARY_REGION=\"cn-east-1\";") tk.MustExec("create placement policy y PRIMARY_REGION=\"cn-east-2\";") + // Policy Test tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x`;") - // Test for Show Create Database tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ @@ -396,7 +396,6 @@ func (s *testDBSuite6) TestAlterDBWithPlacementPolicy(c *C) { ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ "/*T![placement] PLACEMENT POLICY=`x` */", )) - // Test for Alter Default Placement Policy, And will not update the old table options. tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;") tk.MustExec("create table t2(a int);") @@ -413,30 +412,52 @@ func (s *testDBSuite6) TestAlterDBWithPlacementPolicy(c *C) { "/*T![placement] PLACEMENT POLICY=`y` */", )) - tk.MustExec(`DROP TABLE IF EXISTS t, t2`) -} - -func (s *testDBSuite6) TestAlterDBWithDirectPlacement(c *C) { - tk := testkit.NewTestKit(c, s.store) + // Reset Test tk.MustExec("drop database if exists TestAlterDB;") tk.MustExec("create database TestAlterDB;") tk.MustExec("use TestAlterDB") - + // TODO: need parser changed + //tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=default;") + //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ ", + //)) + //tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY SET DEFAULT") + //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ ", + //)) + //tk.MustExec("ALTER DATABASE TestAlterDB default PLACEMENT POLICY SET DEFAULT") + //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ ", + //)) + + // DirectOption Test tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2;") - // Test for Show Create Database tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", )) // Test for Alter Placement Rule affect table created. - tk.MustExec("create table t(a int);") - tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|", - "t CREATE TABLE `t` (\n"+ + tk.MustExec("create table t3(a int);") + tk.MustQuery(`show create table t3`).Check(testutil.RowsWithSep("|", + "t3 CREATE TABLE `t3` (\n"+ " `a` int(11) DEFAULT NULL\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", )) + // Test for override default option + tk.MustExec("create table t4(a int) PLACEMENT POLICY=\"x\";") + tk.MustQuery(`show create table t4`).Check(testutil.RowsWithSep("|", + "t4 CREATE TABLE `t4` (\n"+ + " `a` int(11) DEFAULT NULL\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ + "/*T![placement] PLACEMENT POLICY=`x` */", + )) + + // Hybrid Test + tk.MustGetErrCode("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;", mysql.ErrPlacementPolicyWithDirectOption) + + tk.MustExec(`DROP DATABASE IF EXISTS TestAlterDB;`) } func (s *testDBSuite6) TestDropPlacementPolicyInUse(c *C) { diff --git a/ddl/rollingback.go b/ddl/rollingback.go index 4789f36d5e9f3..906fa3909f1a3 100644 --- a/ddl/rollingback.go +++ b/ddl/rollingback.go @@ -477,7 +477,6 @@ func convertJob2RollbackJob(w *worker, d *ddlCtx, t *meta.Meta, job *model.Job) model.ActionModifySchemaCharsetAndCollate, model.ActionRepairTable, model.ActionModifyTableAutoIdCache, model.ActionAlterIndexVisibility, model.ActionExchangeTablePartition, model.ActionModifySchemaDefaultPlacement: - //TODO: sylzd ver, err = cancelOnlyNotHandledJob(job) default: job.State = model.JobStateCancelled diff --git a/ddl/schema.go b/ddl/schema.go index 3389cbd16fd4a..2d8428e6d4d65 100644 --- a/ddl/schema.go +++ b/ddl/schema.go @@ -153,16 +153,16 @@ func onModifySchemaDefaultPlacement(t *meta.Meta, job *model.Job) (ver int64, _ return ver, errors.Trace(err) } - if placementPolicyRef != nil { - if dbInfo.PlacementPolicyRef != nil && (dbInfo.PlacementPolicyRef.ID == placementPolicyRef.ID) { - job.FinishDBJob(model.JobStateDone, model.StatePublic, ver, dbInfo) - return ver, nil - } - dbInfo.PlacementPolicyRef = placementPolicyRef - } else if directPlacementOpts != nil { - dbInfo.DirectPlacementOpts = directPlacementOpts + // Notice: dbInfo.DirectPlacementOpts and dbInfo.PlacementPolicyRef can not be both not nil. + if (dbInfo.DirectPlacementOpts == directPlacementOpts) && (dbInfo.PlacementPolicyRef == placementPolicyRef) { + job.FinishDBJob(model.JobStateDone, model.StatePublic, ver, dbInfo) + return ver, nil } + // If placementPolicyRef and directPlacementOpts are both nil, And placement of dbInfo is not nil, it will remove all placement options. + dbInfo.PlacementPolicyRef = placementPolicyRef + dbInfo.DirectPlacementOpts = directPlacementOpts + if err = t.UpdateDatabase(dbInfo); err != nil { return ver, errors.Trace(err) } diff --git a/go.sum b/go.sum index 145d1d5ecbdf5..8bddb4b87ee57 100644 --- a/go.sum +++ b/go.sum @@ -605,6 +605,8 @@ github.com/pingcap/log v0.0.0-20210906054005-afc726e70354/go.mod h1:DWQW5jICDR7U github.com/pingcap/parser v0.0.0-20210525032559-c37778aff307/go.mod h1:xZC8I7bug4GJ5KtHhgAikjTfU4kBv1Sbo3Pf1MZ6lVw= github.com/pingcap/parser v0.0.0-20210917114242-ac711116bdff h1:LiwvvutmyeSkFkdVM09mH6KK+OeDVJzX7WKy9Lf0ri0= github.com/pingcap/parser v0.0.0-20210917114242-ac711116bdff/go.mod h1:+xcMiiZzdIktT/Nqdfm81dkECJ2EPuoAYywd57py4Pk= +github.com/pingcap/parser v0.0.0-20210927074647-16d022b0dae1 h1:4Rk+ti4Rz+vSRff+oLuMFRBe9HKRdaBA089nr2x4FyI= +github.com/pingcap/parser v0.0.0-20210927074647-16d022b0dae1/go.mod h1:+xcMiiZzdIktT/Nqdfm81dkECJ2EPuoAYywd57py4Pk= github.com/pingcap/sysutil v0.0.0-20200206130906-2bfa6dc40bcd/go.mod h1:EB/852NMQ+aRKioCpToQ94Wl7fktV+FNnxf3CX/TTXI= github.com/pingcap/sysutil v0.0.0-20210315073920-cc0985d983a3/go.mod h1:tckvA041UWP+NqYzrJ3fMgC/Hw9wnmQ/tUkp/JaHly8= github.com/pingcap/sysutil v0.0.0-20210730114356-fcd8a63f68c5 h1:7rvAtZe/ZUzOKzgriNPQoBNvleJXBk4z7L3Z47+tS98= diff --git a/util/admin/admin.go b/util/admin/admin.go index cf8694d56119f..e28bc6759c807 100644 --- a/util/admin/admin.go +++ b/util/admin/admin.go @@ -116,9 +116,8 @@ func IsJobRollbackable(job *model.Job) bool { model.ActionTruncateTable, model.ActionAddForeignKey, model.ActionDropForeignKey, model.ActionRenameTable, model.ActionModifyTableCharsetAndCollate, model.ActionTruncateTablePartition, - model.ActionModifySchemaCharsetAndCollate, model.ActionRepairTable, model.ActionModifyTableAutoIdCache, - model.ActionModifySchemaDefaultPlacement: - //TODO: sylzd + model.ActionModifySchemaCharsetAndCollate, model.ActionRepairTable, + model.ActionModifyTableAutoIdCache, model.ActionModifySchemaDefaultPlacement: return job.SchemaState == model.StateNone } return true From 1e4b8e80ecd02f19598af0764d1b66044666b147 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 18:05:19 +0800 Subject: [PATCH 10/34] support set default, bu can split with policy named --- ddl/ddl_api.go | 12 ++++++--- ddl/placement_policy_test.go | 48 ++++++++++++++++++++++++------------ go.mod | 2 ++ 3 files changed, 42 insertions(+), 20 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 71d2f557249e3..ae53ad1121463 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -192,11 +192,15 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( } case ast.DatabaseOptionPlacementPolicy: policyName = model.NewCIStr(val.Value) - policy, ok := ctx.GetInfoSchema().(infoschema.InfoSchema).PolicyByName(policyName) - if !ok { - return errors.Trace(infoschema.ErrPlacementPolicyNotExists.GenWithStackByArgs(policyName)) + if policyName.L == "default" { + placementPolicyRef = nil + } else { + policy, ok := ctx.GetInfoSchema().(infoschema.InfoSchema).PolicyByName(policyName) + if !ok { + return errors.Trace(infoschema.ErrPlacementPolicyNotExists.GenWithStackByArgs(policyName)) + } + placementPolicyRef = &model.PolicyRefInfo{ID: policy.ID, Name: policyName} } - placementPolicyRef = &model.PolicyRefInfo{ID: policy.ID, Name: policyName} } } diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index 42d575830272b..63f69af39a0bf 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -413,22 +413,38 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { )) // Reset Test - tk.MustExec("drop database if exists TestAlterDB;") - tk.MustExec("create database TestAlterDB;") - tk.MustExec("use TestAlterDB") - // TODO: need parser changed - //tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=default;") - //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ ", - //)) - //tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY SET DEFAULT") - //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ ", - //)) - //tk.MustExec("ALTER DATABASE TestAlterDB default PLACEMENT POLICY SET DEFAULT") - //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ ", - //)) + // Test for `=default` + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + "/*T![placement] PLACEMENT POLICY=`y` */", + )) + tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=default;") + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", + )) + // Test for `SET DEFAULT` + tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;") + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + "/*T![placement] PLACEMENT POLICY=`y` */", + )) + tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY SET DEFAULT") + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", + )) + // Test for default `SET default` + tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;") + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + "/*T![placement] PLACEMENT POLICY=`y` */", + )) + tk.MustExec("ALTER DATABASE TestAlterDB default PLACEMENT POLICY SET DEFAULT") + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", + )) + // Test for "=`default`" + // TODO: how can we split “default” (policy name) with default (keyword)? or we just support SET DEFAULT syntax? + // tk.MustGetErrCode("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`default`;", mysql.ErrPlacementPolicyNotExists) // DirectOption Test tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2;") diff --git a/go.mod b/go.mod index b502e979311bf..bc347498eb09b 100644 --- a/go.mod +++ b/go.mod @@ -96,3 +96,5 @@ require ( // cloud.google.com/go/storage will upgrade grpc to v1.40.0 // we need keep the replacement until go.etcd.io supports the higher version of grpc. replace google.golang.org/grpc => google.golang.org/grpc v1.29.1 + +replace github.com/pingcap/parser => /Users/lzd/Dropbox/mi_work/parser From 37cf608ceb86ee9a7126b26b5dd848eeae33833d Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 18:25:59 +0800 Subject: [PATCH 11/34] fix --- ddl/ddl_api.go | 148 +++++++++++++++++++++++++------------------------ 1 file changed, 76 insertions(+), 72 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index ae53ad1121463..7414b8d37a696 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -153,10 +153,79 @@ func (d *ddl) CreateSchemaWithInfo( return errors.Trace(err) } +func (d *ddl) ModifySchemaCharsetAndCollate(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt, toCharset, toCollate string) (err error) { + if toCollate == "" { + if toCollate, err = charset.GetDefaultCollation(toCharset); err != nil { + return errors.Trace(err) + } + } + + // Check if need to change charset/collation. + dbName := model.NewCIStr(stmt.Name) + is := d.GetInfoSchemaWithInterceptor(ctx) + dbInfo, ok := is.SchemaByName(dbName) + if !ok { + return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbName.O) + } + if dbInfo.Charset == toCharset && dbInfo.Collate == toCollate { + return nil + } + // Do the DDL job. + job := &model.Job{ + SchemaID: dbInfo.ID, + SchemaName: dbInfo.Name.L, + Type: model.ActionModifySchemaCharsetAndCollate, + BinlogInfo: &model.HistoryInfo{}, + Args: []interface{}{toCharset, toCollate}, + } + err = d.doDDLJob(ctx, job) + err = d.callHookOnChanged(err) + return errors.Trace(err) +} + +func (d *ddl) ModifySchemaDefaultPlacement(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt, placementPolicyRef *model.PolicyRefInfo, directPlacementOpts *model.PlacementSettings) (err error) { + dbName := model.NewCIStr(stmt.Name) + is := d.GetInfoSchemaWithInterceptor(ctx) + dbInfo, ok := is.SchemaByName(dbName) + if !ok { + return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbName.O) + } + + if (directPlacementOpts != nil && placementPolicyRef != nil) || + (directPlacementOpts != nil && dbInfo.PlacementPolicyRef != nil) || + (placementPolicyRef != nil && dbInfo.DirectPlacementOpts != nil) { + var policyName model.CIStr + if placementPolicyRef != nil { + policyName = placementPolicyRef.Name + } else { + policyName = dbInfo.PlacementPolicyRef.Name + } + return errors.Trace(ErrPlacementPolicyWithDirectOption.GenWithStackByArgs(policyName)) + } + + if directPlacementOpts != nil { + // check the direct placement option compatibility. + if err := checkPolicyValidation(directPlacementOpts); err != nil { + return errors.Trace(err) + } + } + + // Do the DDL job. + job := &model.Job{ + SchemaID: dbInfo.ID, + SchemaName: dbInfo.Name.L, + Type: model.ActionModifySchemaDefaultPlacement, + BinlogInfo: &model.HistoryInfo{}, + Args: []interface{}{placementPolicyRef, directPlacementOpts}, + } + err = d.doDDLJob(ctx, job) + err = d.callHookOnChanged(err) + return errors.Trace(err) +} + func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) (err error) { // Resolve target charset and collation from options. var toCharset, toCollate string - var isAlterCharCol bool var policyName model.CIStr var placementPolicyRef *model.PolicyRefInfo var directPlacementOpts *model.PlacementSettings @@ -169,7 +238,7 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( } else if toCharset != val.Value { return ErrConflictingDeclarations.GenWithStackByArgs(toCharset, val.Value) } - isAlterCharCol = true + err = d.ModifySchemaCharsetAndCollate(ctx, stmt, toCharset, toCollate) case ast.DatabaseOptionCollate: info, err := collate.GetCollationByName(val.Value) if err != nil { @@ -181,15 +250,16 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( return ErrConflictingDeclarations.GenWithStackByArgs(toCharset, info.CharsetName) } toCollate = info.Name - isAlterCharCol = true + err = d.ModifySchemaCharsetAndCollate(ctx, stmt, toCharset, toCollate) case ast.DatabaseOptionPlacementPrimaryRegion, ast.DatabaseOptionPlacementRegions, ast.DatabaseOptionPlacementFollowerCount, ast.DatabaseOptionPlacementVoterCount, ast.DatabaseOptionPlacementLearnerCount, ast.DatabaseOptionPlacementSchedule, ast.DatabaseOptionPlacementConstraints, ast.DatabaseOptionPlacementLeaderConstraints, ast.DatabaseOptionPlacementLearnerConstraints, ast.DatabaseOptionPlacementFollowerConstraints, ast.DatabaseOptionPlacementVoterConstraints: if directPlacementOpts == nil { directPlacementOpts = &model.PlacementSettings{} } - err := SetDirectPlacementOpt(directPlacementOpts, ast.PlacementOptionType(val.Tp), val.Value, val.UintValue) + err = SetDirectPlacementOpt(directPlacementOpts, ast.PlacementOptionType(val.Tp), val.Value, val.UintValue) if err != nil { return err } + err = d.ModifySchemaDefaultPlacement(ctx, stmt, placementPolicyRef, directPlacementOpts) case ast.DatabaseOptionPlacementPolicy: policyName = model.NewCIStr(val.Value) if policyName.L == "default" { @@ -201,76 +271,10 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( } placementPolicyRef = &model.PolicyRefInfo{ID: policy.ID, Name: policyName} } + err = d.ModifySchemaDefaultPlacement(ctx, stmt, placementPolicyRef, directPlacementOpts) } } - - // TODO sylzd: 这样有点丑,重新组织下char/colate/placement - if isAlterCharCol { - if toCollate == "" { - if toCollate, err = charset.GetDefaultCollation(toCharset); err != nil { - return errors.Trace(err) - } - } - - // Check if need to change charset/collation. - dbName := model.NewCIStr(stmt.Name) - is := d.GetInfoSchemaWithInterceptor(ctx) - dbInfo, ok := is.SchemaByName(dbName) - if !ok { - return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbName.O) - } - if dbInfo.Charset == toCharset && dbInfo.Collate == toCollate { - return nil - } - // Do the DDL job. - job := &model.Job{ - SchemaID: dbInfo.ID, - SchemaName: dbInfo.Name.L, - Type: model.ActionModifySchemaCharsetAndCollate, - BinlogInfo: &model.HistoryInfo{}, - Args: []interface{}{toCharset, toCollate}, - } - err = d.doDDLJob(ctx, job) - err = d.callHookOnChanged(err) - return errors.Trace(err) - } - dbName := model.NewCIStr(stmt.Name) - is := d.GetInfoSchemaWithInterceptor(ctx) - dbInfo, ok := is.SchemaByName(dbName) - if !ok { - return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbName.O) - } - - if (directPlacementOpts != nil && placementPolicyRef != nil) || - (directPlacementOpts != nil && dbInfo.PlacementPolicyRef != nil) || - (placementPolicyRef != nil && dbInfo.DirectPlacementOpts != nil) { - var policyName model.CIStr - if placementPolicyRef != nil { - policyName = placementPolicyRef.Name - } else { - policyName = dbInfo.PlacementPolicyRef.Name - } - return errors.Trace(ErrPlacementPolicyWithDirectOption.GenWithStackByArgs(policyName)) - } - - if directPlacementOpts != nil { - // check the direct placement option compatibility. - if err := checkPolicyValidation(directPlacementOpts); err != nil { - return errors.Trace(err) - } - } - - // Do the DDL job. - job := &model.Job{ - SchemaID: dbInfo.ID, - SchemaName: dbInfo.Name.L, - Type: model.ActionModifySchemaDefaultPlacement, - BinlogInfo: &model.HistoryInfo{}, - Args: []interface{}{placementPolicyRef, directPlacementOpts}, - } - err = d.doDDLJob(ctx, job) - err = d.callHookOnChanged(err) - return errors.Trace(err) + return err } func (d *ddl) DropSchema(ctx sessionctx.Context, schema model.CIStr) (err error) { From 7e79db93f6ed90c7917fb668f5e03fd06462764c Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 18:30:38 +0800 Subject: [PATCH 12/34] temp pass CI --- ddl/placement_policy_test.go | 68 +++++++++++++++++++----------------- go.sum | 2 -- 2 files changed, 36 insertions(+), 34 deletions(-) diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index 63f69af39a0bf..2912c234f5c4f 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -413,38 +413,42 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { )) // Reset Test - // Test for `=default` - tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - "/*T![placement] PLACEMENT POLICY=`y` */", - )) - tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=default;") - tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", - )) - // Test for `SET DEFAULT` - tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;") - tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - "/*T![placement] PLACEMENT POLICY=`y` */", - )) - tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY SET DEFAULT") - tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", - )) - // Test for default `SET default` - tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;") - tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - "/*T![placement] PLACEMENT POLICY=`y` */", - )) - tk.MustExec("ALTER DATABASE TestAlterDB default PLACEMENT POLICY SET DEFAULT") - tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", - )) - // Test for "=`default`" - // TODO: how can we split “default” (policy name) with default (keyword)? or we just support SET DEFAULT syntax? - // tk.MustGetErrCode("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`default`;", mysql.ErrPlacementPolicyNotExists) + tk.MustExec("drop database if exists TestAlterDB;") + tk.MustExec("create database TestAlterDB;") + tk.MustExec("use TestAlterDB") + // TODO: waiting for related parser merged + //// Test for `=default` + //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + // "/*T![placement] PLACEMENT POLICY=`y` */", + //)) + //tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=default;") + //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", + //)) + //// Test for `SET DEFAULT` + //tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;") + //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + // "/*T![placement] PLACEMENT POLICY=`y` */", + //)) + //tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY SET DEFAULT") + //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", + //)) + //// Test for default `SET default` + //tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;") + //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + // "/*T![placement] PLACEMENT POLICY=`y` */", + //)) + //tk.MustExec("ALTER DATABASE TestAlterDB default PLACEMENT POLICY SET DEFAULT") + //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", + //)) + //// Test for "=`default`" + //// TODO: how can we split “default” (policy name) with default (keyword)? or we just support SET DEFAULT syntax? + //// tk.MustGetErrCode("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`default`;", mysql.ErrPlacementPolicyNotExists) // DirectOption Test tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2;") diff --git a/go.sum b/go.sum index 8bddb4b87ee57..145d1d5ecbdf5 100644 --- a/go.sum +++ b/go.sum @@ -605,8 +605,6 @@ github.com/pingcap/log v0.0.0-20210906054005-afc726e70354/go.mod h1:DWQW5jICDR7U github.com/pingcap/parser v0.0.0-20210525032559-c37778aff307/go.mod h1:xZC8I7bug4GJ5KtHhgAikjTfU4kBv1Sbo3Pf1MZ6lVw= github.com/pingcap/parser v0.0.0-20210917114242-ac711116bdff h1:LiwvvutmyeSkFkdVM09mH6KK+OeDVJzX7WKy9Lf0ri0= github.com/pingcap/parser v0.0.0-20210917114242-ac711116bdff/go.mod h1:+xcMiiZzdIktT/Nqdfm81dkECJ2EPuoAYywd57py4Pk= -github.com/pingcap/parser v0.0.0-20210927074647-16d022b0dae1 h1:4Rk+ti4Rz+vSRff+oLuMFRBe9HKRdaBA089nr2x4FyI= -github.com/pingcap/parser v0.0.0-20210927074647-16d022b0dae1/go.mod h1:+xcMiiZzdIktT/Nqdfm81dkECJ2EPuoAYywd57py4Pk= github.com/pingcap/sysutil v0.0.0-20200206130906-2bfa6dc40bcd/go.mod h1:EB/852NMQ+aRKioCpToQ94Wl7fktV+FNnxf3CX/TTXI= github.com/pingcap/sysutil v0.0.0-20210315073920-cc0985d983a3/go.mod h1:tckvA041UWP+NqYzrJ3fMgC/Hw9wnmQ/tUkp/JaHly8= github.com/pingcap/sysutil v0.0.0-20210730114356-fcd8a63f68c5 h1:7rvAtZe/ZUzOKzgriNPQoBNvleJXBk4z7L3Z47+tS98= From c67e5036118c415f8feca3a9cd5b55bb65dfd793 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 18:32:39 +0800 Subject: [PATCH 13/34] temp pass CI --- go.mod | 2 -- 1 file changed, 2 deletions(-) diff --git a/go.mod b/go.mod index bc347498eb09b..b502e979311bf 100644 --- a/go.mod +++ b/go.mod @@ -96,5 +96,3 @@ require ( // cloud.google.com/go/storage will upgrade grpc to v1.40.0 // we need keep the replacement until go.etcd.io supports the higher version of grpc. replace google.golang.org/grpc => google.golang.org/grpc v1.29.1 - -replace github.com/pingcap/parser => /Users/lzd/Dropbox/mi_work/parser From 0f300c308a19b06a14417b39489b6a9d39e95c24 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 18:40:22 +0800 Subject: [PATCH 14/34] fix go.sum --- go.sum | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/go.sum b/go.sum index 145d1d5ecbdf5..c6937eac1c259 100644 --- a/go.sum +++ b/go.sum @@ -603,8 +603,8 @@ github.com/pingcap/log v0.0.0-20210625125904-98ed8e2eb1c7/go.mod h1:8AanEdAHATuR github.com/pingcap/log v0.0.0-20210906054005-afc726e70354 h1:SvWCbCPh1YeHd9yQLksvJYAgft6wLTY1aNG81tpyscQ= github.com/pingcap/log v0.0.0-20210906054005-afc726e70354/go.mod h1:DWQW5jICDR7UJh4HtxXSM20Churx4CQL0fwL/SoOSA4= github.com/pingcap/parser v0.0.0-20210525032559-c37778aff307/go.mod h1:xZC8I7bug4GJ5KtHhgAikjTfU4kBv1Sbo3Pf1MZ6lVw= -github.com/pingcap/parser v0.0.0-20210917114242-ac711116bdff h1:LiwvvutmyeSkFkdVM09mH6KK+OeDVJzX7WKy9Lf0ri0= -github.com/pingcap/parser v0.0.0-20210917114242-ac711116bdff/go.mod h1:+xcMiiZzdIktT/Nqdfm81dkECJ2EPuoAYywd57py4Pk= +github.com/pingcap/parser v0.0.0-20210927074647-16d022b0dae1 h1:4Rk+ti4Rz+vSRff+oLuMFRBe9HKRdaBA089nr2x4FyI= +github.com/pingcap/parser v0.0.0-20210927074647-16d022b0dae1/go.mod h1:+xcMiiZzdIktT/Nqdfm81dkECJ2EPuoAYywd57py4Pk= github.com/pingcap/sysutil v0.0.0-20200206130906-2bfa6dc40bcd/go.mod h1:EB/852NMQ+aRKioCpToQ94Wl7fktV+FNnxf3CX/TTXI= github.com/pingcap/sysutil v0.0.0-20210315073920-cc0985d983a3/go.mod h1:tckvA041UWP+NqYzrJ3fMgC/Hw9wnmQ/tUkp/JaHly8= github.com/pingcap/sysutil v0.0.0-20210730114356-fcd8a63f68c5 h1:7rvAtZe/ZUzOKzgriNPQoBNvleJXBk4z7L3Z47+tS98= From 03462d289a82f759f40027736354555f8a4c0109 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 18:55:05 +0800 Subject: [PATCH 15/34] fix --- ddl/ddl_api.go | 6 +++--- ddl/placement_policy_test.go | 33 --------------------------------- 2 files changed, 3 insertions(+), 36 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 7414b8d37a696..beade647be9a7 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -240,9 +240,9 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( } err = d.ModifySchemaCharsetAndCollate(ctx, stmt, toCharset, toCollate) case ast.DatabaseOptionCollate: - info, err := collate.GetCollationByName(val.Value) - if err != nil { - return errors.Trace(err) + info, errGetCollate := collate.GetCollationByName(val.Value) + if errGetCollate != nil { + return errors.Trace(errGetCollate) } if toCharset == "" { toCharset = info.CharsetName diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index 2912c234f5c4f..1e710d737835c 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -416,39 +416,6 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { tk.MustExec("drop database if exists TestAlterDB;") tk.MustExec("create database TestAlterDB;") tk.MustExec("use TestAlterDB") - // TODO: waiting for related parser merged - //// Test for `=default` - //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - // "/*T![placement] PLACEMENT POLICY=`y` */", - //)) - //tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=default;") - //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", - //)) - //// Test for `SET DEFAULT` - //tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;") - //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - // "/*T![placement] PLACEMENT POLICY=`y` */", - //)) - //tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY SET DEFAULT") - //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", - //)) - //// Test for default `SET default` - //tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;") - //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - // "/*T![placement] PLACEMENT POLICY=`y` */", - //)) - //tk.MustExec("ALTER DATABASE TestAlterDB default PLACEMENT POLICY SET DEFAULT") - //tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - // "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */", - //)) - //// Test for "=`default`" - //// TODO: how can we split “default” (policy name) with default (keyword)? or we just support SET DEFAULT syntax? - //// tk.MustGetErrCode("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`default`;", mysql.ErrPlacementPolicyNotExists) // DirectOption Test tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2;") From 3bc1744bf03452969b89de31679443a770060566 Mon Sep 17 00:00:00 2001 From: sylzd Date: Tue, 28 Sep 2021 19:12:47 +0800 Subject: [PATCH 16/34] make lint happy --- ddl/placement_policy_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index 1e710d737835c..0b80ac39aa619 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -17,7 +17,6 @@ package ddl_test import ( "context" "fmt" - "github.com/pingcap/tidb/util/testutil" . "github.com/pingcap/check" "github.com/pingcap/parser/model" @@ -28,6 +27,7 @@ import ( "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/util/testkit" + "github.com/pingcap/tidb/util/testutil" ) func (s *testDBSuite6) TestPlacementPolicy(c *C) { From 92f3cdd37d1c2902f25e839710d64c298632b186 Mon Sep 17 00:00:00 2001 From: sylzd Date: Wed, 29 Sep 2021 09:46:02 +0800 Subject: [PATCH 17/34] fix old test --- ddl/placement_sql_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index 8569160ee6a8d..6194070aa5717 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -739,7 +739,7 @@ func (s *testDBSuite6) TestCreateSchemaWithPlacement(c *C) { tk.MustQuery("SHOW PLACEMENT like 'POLICY %PolicySchemaTest%'").Check(testkit.Rows("POLICY PolicySchemaTest LEADER_CONSTRAINTS=\"[+region=nl]\" FOLLOWERS=4 FOLLOWER_CONSTRAINTS=\"[+region=se]\" LEARNERS=4 LEARNER_CONSTRAINTS=\"[+region=be]\"")) tk.MustQuery("SHOW PLACEMENT like 'POLICY %PolicyTableTest%'").Check(testkit.Rows("POLICY PolicyTableTest LEADER_CONSTRAINTS=\"[+region=tl]\" FOLLOWERS=2 FOLLOWER_CONSTRAINTS=\"[+region=tf]\" LEARNERS=1 LEARNER_CONSTRAINTS=\"[+region=tle]\"")) tk.MustExec("CREATE SCHEMA SchemaPolicyPlacementTest PLACEMENT POLICY = `PolicySchemaTest`") - tk.MustQuery("SHOW CREATE SCHEMA SCHEMAPOLICYPLACEMENTTEST").Check(testkit.Rows("SchemaPolicyPlacementTest CREATE DATABASE `SchemaPolicyPlacementTest` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ /*T![placement] PLACEMENT POLICY = `PolicySchemaTest` */")) + tk.MustQuery("SHOW CREATE SCHEMA SCHEMAPOLICYPLACEMENTTEST").Check(testkit.Rows("SchemaPolicyPlacementTest CREATE DATABASE `SchemaPolicyPlacementTest` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ /*T![placement] PLACEMENT POLICY=`PolicySchemaTest` */")) tk.MustExec(`CREATE TABLE SchemaDirectPlacementTest.UseSchemaDefault (a int unsigned primary key, b varchar(255))`) tk.MustQuery(`SHOW CREATE TABLE SchemaDirectPlacementTest.UseSchemaDefault`).Check(testkit.Rows( From ccb0046cbdeb742d64bb68e1ba56608e10c31633 Mon Sep 17 00:00:00 2001 From: sylzd Date: Wed, 29 Sep 2021 11:46:09 +0800 Subject: [PATCH 18/34] fix unstable test --- ddl/placement_policy_test.go | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index 0b80ac39aa619..c54079fe45e37 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -378,6 +378,8 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { tk.MustExec("drop database if exists TestAlterDB;") tk.MustExec("create database TestAlterDB;") tk.MustExec("use TestAlterDB") + tk.MustExec("drop placement policy if exists x") + tk.MustExec("drop placement policy if exists y") tk.MustExec("create placement policy x PRIMARY_REGION=\"cn-east-1\";") tk.MustExec("create placement policy y PRIMARY_REGION=\"cn-east-2\";") @@ -445,6 +447,8 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { tk.MustGetErrCode("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;", mysql.ErrPlacementPolicyWithDirectOption) tk.MustExec(`DROP DATABASE IF EXISTS TestAlterDB;`) + tk.MustExec("drop placement policy if exists x") + tk.MustExec("drop placement policy if exists y") } func (s *testDBSuite6) TestDropPlacementPolicyInUse(c *C) { From 6a858f7d74b40e589cb77ef8f382a53cf6c8376a Mon Sep 17 00:00:00 2001 From: sylzd Date: Thu, 30 Sep 2021 14:02:53 +0800 Subject: [PATCH 19/34] fix --- ddl/ddl_api.go | 36 +++++++----- ddl/placement_policy_test.go | 79 -------------------------- ddl/placement_sql_test.go | 105 +++++++++++++++++++++++++++++++++++ 3 files changed, 126 insertions(+), 94 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index beade647be9a7..76e99f18d9007 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -191,16 +191,8 @@ func (d *ddl) ModifySchemaDefaultPlacement(ctx sessionctx.Context, stmt *ast.Alt return infoschema.ErrDatabaseNotExists.GenWithStackByArgs(dbName.O) } - if (directPlacementOpts != nil && placementPolicyRef != nil) || - (directPlacementOpts != nil && dbInfo.PlacementPolicyRef != nil) || - (placementPolicyRef != nil && dbInfo.DirectPlacementOpts != nil) { - var policyName model.CIStr - if placementPolicyRef != nil { - policyName = placementPolicyRef.Name - } else { - policyName = dbInfo.PlacementPolicyRef.Name - } - return errors.Trace(ErrPlacementPolicyWithDirectOption.GenWithStackByArgs(policyName)) + if directPlacementOpts != nil && placementPolicyRef != nil { + return errors.Trace(ErrPlacementPolicyWithDirectOption.GenWithStackByArgs(placementPolicyRef.Name)) } if directPlacementOpts != nil { @@ -229,6 +221,8 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( var policyName model.CIStr var placementPolicyRef *model.PolicyRefInfo var directPlacementOpts *model.PlacementSettings + var isAlterCharsetAndCollate bool + var isAlterPlacement bool for _, val := range stmt.Options { switch val.Tp { @@ -238,7 +232,7 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( } else if toCharset != val.Value { return ErrConflictingDeclarations.GenWithStackByArgs(toCharset, val.Value) } - err = d.ModifySchemaCharsetAndCollate(ctx, stmt, toCharset, toCollate) + isAlterCharsetAndCollate = true case ast.DatabaseOptionCollate: info, errGetCollate := collate.GetCollationByName(val.Value) if errGetCollate != nil { @@ -250,7 +244,7 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( return ErrConflictingDeclarations.GenWithStackByArgs(toCharset, info.CharsetName) } toCollate = info.Name - err = d.ModifySchemaCharsetAndCollate(ctx, stmt, toCharset, toCollate) + isAlterCharsetAndCollate = true case ast.DatabaseOptionPlacementPrimaryRegion, ast.DatabaseOptionPlacementRegions, ast.DatabaseOptionPlacementFollowerCount, ast.DatabaseOptionPlacementVoterCount, ast.DatabaseOptionPlacementLearnerCount, ast.DatabaseOptionPlacementSchedule, ast.DatabaseOptionPlacementConstraints, ast.DatabaseOptionPlacementLeaderConstraints, ast.DatabaseOptionPlacementLearnerConstraints, ast.DatabaseOptionPlacementFollowerConstraints, ast.DatabaseOptionPlacementVoterConstraints: if directPlacementOpts == nil { directPlacementOpts = &model.PlacementSettings{} @@ -259,7 +253,7 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( if err != nil { return err } - err = d.ModifySchemaDefaultPlacement(ctx, stmt, placementPolicyRef, directPlacementOpts) + isAlterPlacement = true case ast.DatabaseOptionPlacementPolicy: policyName = model.NewCIStr(val.Value) if policyName.L == "default" { @@ -271,10 +265,22 @@ func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) ( } placementPolicyRef = &model.PolicyRefInfo{ID: policy.ID, Name: policyName} } - err = d.ModifySchemaDefaultPlacement(ctx, stmt, placementPolicyRef, directPlacementOpts) + isAlterPlacement = true } } - return err + + if isAlterCharsetAndCollate { + if err = d.ModifySchemaCharsetAndCollate(ctx, stmt, toCharset, toCollate); err != nil { + return err + } + } + if isAlterPlacement { + if err = d.ModifySchemaDefaultPlacement(ctx, stmt, placementPolicyRef, directPlacementOpts); err != nil { + return err + } + } + + return nil } func (d *ddl) DropSchema(ctx sessionctx.Context, schema model.CIStr) (err error) { diff --git a/ddl/placement_policy_test.go b/ddl/placement_policy_test.go index c54079fe45e37..beaa795532eee 100644 --- a/ddl/placement_policy_test.go +++ b/ddl/placement_policy_test.go @@ -27,7 +27,6 @@ import ( "github.com/pingcap/tidb/meta" "github.com/pingcap/tidb/sessionctx" "github.com/pingcap/tidb/util/testkit" - "github.com/pingcap/tidb/util/testutil" ) func (s *testDBSuite6) TestPlacementPolicy(c *C) { @@ -373,84 +372,6 @@ func (s *testDBSuite6) TestCreateTableWithPlacementPolicy(c *C) { tk.MustExec("drop placement policy if exists x") } -func (s *testDBSuite6) TestAlterDBPlacement(c *C) { - tk := testkit.NewTestKit(c, s.store) - tk.MustExec("drop database if exists TestAlterDB;") - tk.MustExec("create database TestAlterDB;") - tk.MustExec("use TestAlterDB") - tk.MustExec("drop placement policy if exists x") - tk.MustExec("drop placement policy if exists y") - tk.MustExec("create placement policy x PRIMARY_REGION=\"cn-east-1\";") - tk.MustExec("create placement policy y PRIMARY_REGION=\"cn-east-2\";") - - // Policy Test - tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x`;") - // Test for Show Create Database - tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - "/*T![placement] PLACEMENT POLICY=`x` */", - )) - // Test for Alter Placement Policy affect table created. - tk.MustExec("create table t(a int);") - tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|", - "t CREATE TABLE `t` (\n"+ - " `a` int(11) DEFAULT NULL\n"+ - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ - "/*T![placement] PLACEMENT POLICY=`x` */", - )) - // Test for Alter Default Placement Policy, And will not update the old table options. - tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;") - tk.MustExec("create table t2(a int);") - tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|", - "t CREATE TABLE `t` (\n"+ - " `a` int(11) DEFAULT NULL\n"+ - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ - "/*T![placement] PLACEMENT POLICY=`x` */", - )) - tk.MustQuery(`show create table t2`).Check(testutil.RowsWithSep("|", - "t2 CREATE TABLE `t2` (\n"+ - " `a` int(11) DEFAULT NULL\n"+ - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ - "/*T![placement] PLACEMENT POLICY=`y` */", - )) - - // Reset Test - tk.MustExec("drop database if exists TestAlterDB;") - tk.MustExec("create database TestAlterDB;") - tk.MustExec("use TestAlterDB") - - // DirectOption Test - tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2;") - // Test for Show Create Database - tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", - "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", - )) - // Test for Alter Placement Rule affect table created. - tk.MustExec("create table t3(a int);") - tk.MustQuery(`show create table t3`).Check(testutil.RowsWithSep("|", - "t3 CREATE TABLE `t3` (\n"+ - " `a` int(11) DEFAULT NULL\n"+ - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ - "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", - )) - // Test for override default option - tk.MustExec("create table t4(a int) PLACEMENT POLICY=\"x\";") - tk.MustQuery(`show create table t4`).Check(testutil.RowsWithSep("|", - "t4 CREATE TABLE `t4` (\n"+ - " `a` int(11) DEFAULT NULL\n"+ - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ - "/*T![placement] PLACEMENT POLICY=`x` */", - )) - - // Hybrid Test - tk.MustGetErrCode("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;", mysql.ErrPlacementPolicyWithDirectOption) - - tk.MustExec(`DROP DATABASE IF EXISTS TestAlterDB;`) - tk.MustExec("drop placement policy if exists x") - tk.MustExec("drop placement policy if exists y") -} - func (s *testDBSuite6) TestDropPlacementPolicyInUse(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("use test") diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index 6194070aa5717..0ab63eb65fc30 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -16,6 +16,8 @@ package ddl_test import ( "fmt" + mysql "github.com/pingcap/tidb/errno" + "github.com/pingcap/tidb/util/testutil" "sort" . "github.com/pingcap/check" @@ -802,3 +804,106 @@ func (s *testDBSuite6) TestCreateSchemaWithPlacement(c *C) { c.Assert(db.DirectPlacementOpts, IsNil) c.Assert(db.PlacementPolicyRef.Name.O, Equals, "PolicySchemaTest") } + + +func (s *testDBSuite6) TestAlterDBPlacement(c *C) { + tk := testkit.NewTestKit(c, s.store) + tk.MustExec("drop database if exists TestAlterDB;") + tk.MustExec("create database TestAlterDB;") + tk.MustExec("use TestAlterDB") + tk.MustExec("drop placement policy if exists x") + tk.MustExec("drop placement policy if exists y") + tk.MustExec("create placement policy x PRIMARY_REGION=\"cn-east-1\";") + tk.MustExec("create placement policy y PRIMARY_REGION=\"cn-east-2\";") + + // Policy Test + tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x`;") + // Test for Show Create Database + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + "/*T![placement] PLACEMENT POLICY=`x` */", + )) + // Test for Alter Placement Policy affect table created. + tk.MustExec("create table t(a int);") + tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|", + "t CREATE TABLE `t` (\n"+ + " `a` int(11) DEFAULT NULL\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ + "/*T![placement] PLACEMENT POLICY=`x` */", + )) + // Test for Alter Default Placement Policy, And will not update the old table options. + tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;") + tk.MustExec("create table t2(a int);") + tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|", + "t CREATE TABLE `t` (\n"+ + " `a` int(11) DEFAULT NULL\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ + "/*T![placement] PLACEMENT POLICY=`x` */", + )) + tk.MustQuery(`show create table t2`).Check(testutil.RowsWithSep("|", + "t2 CREATE TABLE `t2` (\n"+ + " `a` int(11) DEFAULT NULL\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ + "/*T![placement] PLACEMENT POLICY=`y` */", + )) + + // Reset Test + tk.MustExec("drop database if exists TestAlterDB;") + tk.MustExec("create database TestAlterDB;") + tk.MustExec("use TestAlterDB") + + // DirectOption Test + tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2;") + // Test for Show Create Database + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", + )) + // Test for Alter Placement Rule affect table created. + tk.MustExec("create table t3(a int);") + tk.MustQuery(`show create table t3`).Check(testutil.RowsWithSep("|", + "t3 CREATE TABLE `t3` (\n"+ + " `a` int(11) DEFAULT NULL\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ + "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", + )) + // Test for override default option + tk.MustExec("create table t4(a int) PLACEMENT POLICY=\"x\";") + tk.MustQuery(`show create table t4`).Check(testutil.RowsWithSep("|", + "t4 CREATE TABLE `t4` (\n"+ + " `a` int(11) DEFAULT NULL\n"+ + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ + "/*T![placement] PLACEMENT POLICY=`x` */", + )) + + // Hybrid Test + // Test for alter both policy and direct options. + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", + )) + tk.MustGetErrCode("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x` FOLLOWERS=2;", mysql.ErrPlacementPolicyWithDirectOption) + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", + )) + // Test for change direct options to policy. + tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x`;") + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + "/*T![placement] PLACEMENT POLICY=`x` */", + )) + // Test for change direct options to policy. + tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2;") + tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", + "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ + "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", + )) + + // Test for change policy to direct options. + tk.MustGetErrCode("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y` PRIMARY_REGION=\"se\" FOLLOWERS=2;", mysql.ErrPlacementPolicyWithDirectOption) + + tk.MustExec(`DROP DATABASE IF EXISTS TestAlterDB;`) + tk.MustExec("drop placement policy if exists x") + tk.MustExec("drop placement policy if exists y") +} From 3871a6d2cdb440ef51f7a9432ea84a6479c1dfc9 Mon Sep 17 00:00:00 2001 From: sylzd Date: Thu, 30 Sep 2021 14:08:35 +0800 Subject: [PATCH 20/34] fix --- ddl/placement_sql_test.go | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index 0ab63eb65fc30..b2e6a434f73e9 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -883,6 +883,7 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", )) tk.MustGetErrCode("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x` FOLLOWERS=2;", mysql.ErrPlacementPolicyWithDirectOption) + tk.MustGetErrCode("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y` PRIMARY_REGION=\"se\" FOLLOWERS=2;", mysql.ErrPlacementPolicyWithDirectOption) tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", @@ -893,16 +894,13 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ "/*T![placement] PLACEMENT POLICY=`x` */", )) - // Test for change direct options to policy. + // Test for change policy to direct options. tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2;") tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", )) - // Test for change policy to direct options. - tk.MustGetErrCode("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y` PRIMARY_REGION=\"se\" FOLLOWERS=2;", mysql.ErrPlacementPolicyWithDirectOption) - tk.MustExec(`DROP DATABASE IF EXISTS TestAlterDB;`) tk.MustExec("drop placement policy if exists x") tk.MustExec("drop placement policy if exists y") From dbdd4c54bf65ed9608fa7b516b170740aacf3741 Mon Sep 17 00:00:00 2001 From: sylzd Date: Thu, 30 Sep 2021 16:49:03 +0800 Subject: [PATCH 21/34] good lint --- ddl/placement_sql_test.go | 1 - 1 file changed, 1 deletion(-) diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index b2e6a434f73e9..75d0e1c82d0b0 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -805,7 +805,6 @@ func (s *testDBSuite6) TestCreateSchemaWithPlacement(c *C) { c.Assert(db.PlacementPolicyRef.Name.O, Equals, "PolicySchemaTest") } - func (s *testDBSuite6) TestAlterDBPlacement(c *C) { tk := testkit.NewTestKit(c, s.store) tk.MustExec("drop database if exists TestAlterDB;") From 9bf4db7f6ad29115e3c14f61e507ed8f1f6d4c1c Mon Sep 17 00:00:00 2001 From: sylzd Date: Thu, 30 Sep 2021 16:56:48 +0800 Subject: [PATCH 22/34] happy lint again --- ddl/placement_sql_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index 75d0e1c82d0b0..ea51e32c75e7a 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -17,7 +17,6 @@ package ddl_test import ( "fmt" mysql "github.com/pingcap/tidb/errno" - "github.com/pingcap/tidb/util/testutil" "sort" . "github.com/pingcap/check" @@ -27,6 +26,7 @@ import ( "github.com/pingcap/tidb/ddl/placement" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/table/tables" + "github.com/pingcap/tidb/util/testutil" "github.com/pingcap/tidb/util/testkit" ) From 8287740b6e5022e57d36939025f166905096c080 Mon Sep 17 00:00:00 2001 From: sylzd Date: Thu, 30 Sep 2021 17:06:15 +0800 Subject: [PATCH 23/34] god lint --- ddl/placement_sql_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index ea51e32c75e7a..cfd68d34c3f41 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -26,8 +26,8 @@ import ( "github.com/pingcap/tidb/ddl/placement" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/table/tables" - "github.com/pingcap/tidb/util/testutil" "github.com/pingcap/tidb/util/testkit" + "github.com/pingcap/tidb/util/testutil" ) func (s *testDBSuite6) TestAlterTableAlterPartition(c *C) { From ed87872eb2ba28fd7b38c6b807d47cbea3a03b90 Mon Sep 17 00:00:00 2001 From: sylzd Date: Thu, 30 Sep 2021 17:19:49 +0800 Subject: [PATCH 24/34] legendary lint --- ddl/placement_sql_test.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index cfd68d34c3f41..0a4c0df69d737 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -16,7 +16,6 @@ package ddl_test import ( "fmt" - mysql "github.com/pingcap/tidb/errno" "sort" . "github.com/pingcap/check" @@ -24,6 +23,7 @@ import ( "github.com/pingcap/parser/model" "github.com/pingcap/tidb/ddl" "github.com/pingcap/tidb/ddl/placement" + mysql "github.com/pingcap/tidb/errno" "github.com/pingcap/tidb/session" "github.com/pingcap/tidb/table/tables" "github.com/pingcap/tidb/util/testkit" From 733f75cd7b24435263e02d373e6aa3e248d84bda Mon Sep 17 00:00:00 2001 From: sylzd Date: Fri, 8 Oct 2021 11:47:31 +0800 Subject: [PATCH 25/34] fix test case --- ddl/placement_sql_test.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index 0a4c0df69d737..e31b44b32879e 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -733,8 +733,8 @@ func (s *testDBSuite6) TestCreateSchemaWithPlacement(c *C) { tk.Se.GetSessionVars().EnableAlterPlacement = false }() - tk.MustExec(`CREATE SCHEMA SchemaDirectPlacementTest PRIMARY_REGION='nl' REGIONS = "se,nz" FOLLOWERS=3`) - tk.MustQuery("SHOW CREATE SCHEMA schemadirectplacementtest").Check(testkit.Rows("SchemaDirectPlacementTest CREATE DATABASE `SchemaDirectPlacementTest` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ /*T![placement] PRIMARY_REGION=\"nl\" REGIONS=\"se,nz\" FOLLOWERS=3 */")) + tk.MustExec(`CREATE SCHEMA SchemaDirectPlacementTest PRIMARY_REGION='nl' REGIONS = "se,nz,nl" FOLLOWERS=3`) + tk.MustQuery("SHOW CREATE SCHEMA schemadirectplacementtest").Check(testkit.Rows("SchemaDirectPlacementTest CREATE DATABASE `SchemaDirectPlacementTest` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ /*T![placement] PRIMARY_REGION=\"nl\" REGIONS=\"se,nz,nl\" FOLLOWERS=3 */")) tk.MustExec(`CREATE PLACEMENT POLICY PolicySchemaTest LEADER_CONSTRAINTS = "[+region=nl]" FOLLOWER_CONSTRAINTS="[+region=se]" FOLLOWERS=4 LEARNER_CONSTRAINTS="[+region=be]" LEARNERS=4`) tk.MustExec(`CREATE PLACEMENT POLICY PolicyTableTest LEADER_CONSTRAINTS = "[+region=tl]" FOLLOWER_CONSTRAINTS="[+region=tf]" FOLLOWERS=2 LEARNER_CONSTRAINTS="[+region=tle]" LEARNERS=1`) @@ -749,7 +749,7 @@ func (s *testDBSuite6) TestCreateSchemaWithPlacement(c *C) { " `a` int(10) unsigned NOT NULL,\n" + " `b` varchar(255) DEFAULT NULL,\n" + " PRIMARY KEY (`a`) /*T![clustered_index] CLUSTERED */\n" + - ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![placement] PRIMARY_REGION=\"nl\" REGIONS=\"se,nz\" FOLLOWERS=3 */")) + ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin /*T![placement] PRIMARY_REGION=\"nl\" REGIONS=\"se,nz,nl\" FOLLOWERS=3 */")) tk.MustExec(`CREATE TABLE SchemaDirectPlacementTest.UseDirectPlacement (a int unsigned primary key, b varchar(255)) PRIMARY_REGION="se"`) tk.MustQuery(`SHOW CREATE TABLE SchemaDirectPlacementTest.UseDirectPlacement`).Check(testkit.Rows( "UseDirectPlacement CREATE TABLE `UseDirectPlacement` (\n" + @@ -794,7 +794,7 @@ func (s *testDBSuite6) TestCreateSchemaWithPlacement(c *C) { c.Assert(db.PlacementPolicyRef, IsNil) c.Assert(db.DirectPlacementOpts, NotNil) c.Assert(db.DirectPlacementOpts.PrimaryRegion, Matches, "nl") - c.Assert(db.DirectPlacementOpts.Regions, Matches, "se,nz") + c.Assert(db.DirectPlacementOpts.Regions, Matches, "se,nz,nl") c.Assert(db.DirectPlacementOpts.Followers, Equals, uint64(3)) c.Assert(db.DirectPlacementOpts.Learners, Equals, uint64(0)) From 77c937113dc0693b297ad09144cca5de72a80d20 Mon Sep 17 00:00:00 2001 From: sylzd Date: Fri, 8 Oct 2021 12:09:24 +0800 Subject: [PATCH 26/34] fix test case --- ddl/placement_sql_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index 1f33a2b12a119..bb9fe0d8c4218 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -800,8 +800,8 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { tk.MustExec("use TestAlterDB") tk.MustExec("drop placement policy if exists x") tk.MustExec("drop placement policy if exists y") - tk.MustExec("create placement policy x PRIMARY_REGION=\"cn-east-1\";") - tk.MustExec("create placement policy y PRIMARY_REGION=\"cn-east-2\";") + tk.MustExec("create placement policy x PRIMARY_REGION=\"cn-east-1\", REGIONS=\"cn-east-1\";") + tk.MustExec("create placement policy y PRIMARY_REGION=\"cn-east-2\", REGIONS=\"cn-east-2\";") // Policy Test tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x`;") @@ -840,11 +840,11 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { tk.MustExec("use TestAlterDB") // DirectOption Test - tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2;") + tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2 REGIONS=\"se\";") // Test for Show Create Database tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", + "/*T![placement] PRIMARY_REGION=\"se\" REGIONS=\"se\" FOLLOWERS=2 */", )) // Test for Alter Placement Rule affect table created. tk.MustExec("create table t3(a int);") @@ -852,7 +852,7 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { "t3 CREATE TABLE `t3` (\n"+ " `a` int(11) DEFAULT NULL\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ - "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", + "/*T![placement] PRIMARY_REGION=\"se\" REGIONS=\"se\" FOLLOWERS=2 */", )) // Test for override default option tk.MustExec("create table t4(a int) PLACEMENT POLICY=\"x\";") @@ -867,13 +867,13 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { // Test for alter both policy and direct options. tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", + "/*T![placement] PRIMARY_REGION=\"se\" REGIONS=\"se\" FOLLOWERS=2 */", )) tk.MustGetErrCode("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x` FOLLOWERS=2;", mysql.ErrPlacementPolicyWithDirectOption) tk.MustGetErrCode("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y` PRIMARY_REGION=\"se\" FOLLOWERS=2;", mysql.ErrPlacementPolicyWithDirectOption) tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", + "/*T![placement] PRIMARY_REGION=\"se\" REGIONS=\"se\" FOLLOWERS=2 */", )) // Test for change direct options to policy. tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x`;") @@ -882,10 +882,10 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { "/*T![placement] PLACEMENT POLICY=`x` */", )) // Test for change policy to direct options. - tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2;") + tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2 REGIONS=\"se\" ;") tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - "/*T![placement] PRIMARY_REGION=\"se\" FOLLOWERS=2 */", + "/*T![placement] PRIMARY_REGION=\"se\" REGIONS=\"se\" FOLLOWERS=2 */", )) tk.MustExec(`DROP DATABASE IF EXISTS TestAlterDB;`) From f72686ce1e49f1bcb708f4ea759cefdb83aec737 Mon Sep 17 00:00:00 2001 From: sylzd Date: Fri, 8 Oct 2021 17:13:38 +0800 Subject: [PATCH 27/34] make vars more clear --- ddl/ddl_api.go | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 76e99f18d9007..a82fb2934a4ee 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -217,12 +217,13 @@ func (d *ddl) ModifySchemaDefaultPlacement(ctx sessionctx.Context, stmt *ast.Alt func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) (err error) { // Resolve target charset and collation from options. - var toCharset, toCollate string - var policyName model.CIStr - var placementPolicyRef *model.PolicyRefInfo - var directPlacementOpts *model.PlacementSettings - var isAlterCharsetAndCollate bool - var isAlterPlacement bool + var ( + toCharset, toCollate string + isAlterCharsetAndCollate, isAlterPlacement bool + policyName model.CIStr + placementPolicyRef *model.PolicyRefInfo + directPlacementOpts *model.PlacementSettings + ) for _, val := range stmt.Options { switch val.Tp { From 639aa3f95b98236e8dbff4cf3699b04a8590a2eb Mon Sep 17 00:00:00 2001 From: sylzd Date: Fri, 8 Oct 2021 17:18:16 +0800 Subject: [PATCH 28/34] ditto --- ddl/schema.go | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/ddl/schema.go b/ddl/schema.go index 2d8428e6d4d65..f83375222d263 100644 --- a/ddl/schema.go +++ b/ddl/schema.go @@ -141,8 +141,10 @@ func onModifySchemaCharsetAndCollate(t *meta.Meta, job *model.Job) (ver int64, _ } func onModifySchemaDefaultPlacement(t *meta.Meta, job *model.Job) (ver int64, _ error) { - var placementPolicyRef *model.PolicyRefInfo - var directPlacementOpts *model.PlacementSettings + var ( + placementPolicyRef *model.PolicyRefInfo + directPlacementOpts *model.PlacementSettings + ) if err := job.DecodeArgs(&placementPolicyRef, &directPlacementOpts); err != nil { job.State = model.JobStateCancelled return ver, errors.Trace(err) From f0010ee5b57f3b3d07a05a927250d5f6b467c3e1 Mon Sep 17 00:00:00 2001 From: sylzd Date: Fri, 8 Oct 2021 17:45:44 +0800 Subject: [PATCH 29/34] fix wrong equal --- ddl/schema.go | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/ddl/schema.go b/ddl/schema.go index f83375222d263..23ba7a35ce597 100644 --- a/ddl/schema.go +++ b/ddl/schema.go @@ -155,8 +155,9 @@ func onModifySchemaDefaultPlacement(t *meta.Meta, job *model.Job) (ver int64, _ return ver, errors.Trace(err) } - // Notice: dbInfo.DirectPlacementOpts and dbInfo.PlacementPolicyRef can not be both not nil. - if (dbInfo.DirectPlacementOpts == directPlacementOpts) && (dbInfo.PlacementPolicyRef == placementPolicyRef) { + // Notice: dbInfo.DirectPlacementOpts and dbInfo.PlacementPolicyRef can not be both not nil, which checked before constructing ddl job. + // So that we can just check the two situation that do not need ddl: 1. DB.DP == DDL.DP && nil == nil 2. nil == nil && DB.PP == DDL.PP + if (*dbInfo.DirectPlacementOpts == *directPlacementOpts) && (*dbInfo.PlacementPolicyRef == *placementPolicyRef) { job.FinishDBJob(model.JobStateDone, model.StatePublic, ver, dbInfo) return ver, nil } From 72ac48817273bf39ee135be288c07f5b602d8ae4 Mon Sep 17 00:00:00 2001 From: sylzd Date: Fri, 8 Oct 2021 17:52:15 +0800 Subject: [PATCH 30/34] add test case --- ddl/placement_sql_test.go | 3 +++ 1 file changed, 3 insertions(+) diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index bb9fe0d8c4218..bf4e316e8ffeb 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -804,6 +804,9 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { tk.MustExec("create placement policy y PRIMARY_REGION=\"cn-east-2\", REGIONS=\"cn-east-2\";") // Policy Test + // Test for Non-Exist policy + tk.MustGetErrCode("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`z`;", mysql.ErrPlacementPolicyNotExists) + tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x`;") // Test for Show Create Database tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", From 37cfcb58201433ccc8f46fe6c15457e30b406c5c Mon Sep 17 00:00:00 2001 From: sylzd Date: Fri, 8 Oct 2021 17:59:31 +0800 Subject: [PATCH 31/34] lint --- ddl/ddl_api.go | 8 ++++---- ddl/schema.go | 2 +- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/ddl/ddl_api.go b/ddl/ddl_api.go index 1767986f9e8af..343532badf03d 100644 --- a/ddl/ddl_api.go +++ b/ddl/ddl_api.go @@ -218,11 +218,11 @@ func (d *ddl) ModifySchemaDefaultPlacement(ctx sessionctx.Context, stmt *ast.Alt func (d *ddl) AlterSchema(ctx sessionctx.Context, stmt *ast.AlterDatabaseStmt) (err error) { // Resolve target charset and collation from options. var ( - toCharset, toCollate string + toCharset, toCollate string isAlterCharsetAndCollate, isAlterPlacement bool - policyName model.CIStr - placementPolicyRef *model.PolicyRefInfo - directPlacementOpts *model.PlacementSettings + policyName model.CIStr + placementPolicyRef *model.PolicyRefInfo + directPlacementOpts *model.PlacementSettings ) for _, val := range stmt.Options { diff --git a/ddl/schema.go b/ddl/schema.go index 23ba7a35ce597..7184b426be283 100644 --- a/ddl/schema.go +++ b/ddl/schema.go @@ -142,7 +142,7 @@ func onModifySchemaCharsetAndCollate(t *meta.Meta, job *model.Job) (ver int64, _ func onModifySchemaDefaultPlacement(t *meta.Meta, job *model.Job) (ver int64, _ error) { var ( - placementPolicyRef *model.PolicyRefInfo + placementPolicyRef *model.PolicyRefInfo directPlacementOpts *model.PlacementSettings ) if err := job.DecodeArgs(&placementPolicyRef, &directPlacementOpts); err != nil { From 3dc19b2e97772152177706aacb7f7d8363dfe4d0 Mon Sep 17 00:00:00 2001 From: sylzd Date: Fri, 8 Oct 2021 18:09:47 +0800 Subject: [PATCH 32/34] fix conflicts --- executor/show.go | 14 +++----------- 1 file changed, 3 insertions(+), 11 deletions(-) diff --git a/executor/show.go b/executor/show.go index 120e276c37a75..3989fdc039f2b 100644 --- a/executor/show.go +++ b/executor/show.go @@ -1347,22 +1347,14 @@ func ConstructResultOfShowCreateDatabase(ctx sessionctx.Context, dbInfo *model.D } // MySQL 5.7 always show the charset info but TiDB may ignore it, which makes a slight difference. We keep this // behavior unchanged because it is trivial enough. -<<<<<<< HEAD - if dbInfo.PlacementPolicyRef != nil { - fmt.Fprintf(buf, " /*T![placement] PLACEMENT POLICY=%s */", stringutil.Escape(dbInfo.PlacementPolicyRef.Name.String(), sqlMode)) -======= + // add placement ref info here + fmt.Fprintf(buf, " /*T![placement] PLACEMENT POLICY=%s */", stringutil.Escape(dbInfo.PlacementPolicyRef.Name.O, sqlMode)) + } if dbInfo.DirectPlacementOpts != nil { // add direct placement info here appendDirectPlacementInfo(dbInfo.DirectPlacementOpts, buf) } - if dbInfo.PlacementPolicyRef != nil { - // add placement ref info here - fmt.Fprintf(buf, " /*T![placement] PLACEMENT POLICY=%s */", stringutil.Escape(dbInfo.PlacementPolicyRef.Name.O, sqlMode)) ->>>>>>> 267a3b8162921b88260d4d1b27ef23bc20f5f514 - } - // add direct placement info here - appendDirectPlacementInfo(dbInfo.DirectPlacementOpts, buf) return nil } From bdf86beba590215e9dbaa94865bcae0bed74e439 Mon Sep 17 00:00:00 2001 From: sylzd Date: Fri, 8 Oct 2021 18:14:12 +0800 Subject: [PATCH 33/34] double check policy exits --- ddl/schema.go | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/ddl/schema.go b/ddl/schema.go index 7184b426be283..689261a821e1e 100644 --- a/ddl/schema.go +++ b/ddl/schema.go @@ -154,6 +154,11 @@ func onModifySchemaDefaultPlacement(t *meta.Meta, job *model.Job) (ver int64, _ if err != nil { return ver, errors.Trace(err) } + //Double Check if policy exits while ddl executing + _, err = checkPlacementPolicyExistAndCancelNonExistJob(t, job, placementPolicyRef.ID) + if err != nil { + return ver, errors.Trace(err) + } // Notice: dbInfo.DirectPlacementOpts and dbInfo.PlacementPolicyRef can not be both not nil, which checked before constructing ddl job. // So that we can just check the two situation that do not need ddl: 1. DB.DP == DDL.DP && nil == nil 2. nil == nil && DB.PP == DDL.PP From d881b7236ffa4409fb1788adb9dc7973ebce22e3 Mon Sep 17 00:00:00 2001 From: sylzd Date: Sat, 9 Oct 2021 11:52:40 +0800 Subject: [PATCH 34/34] fix --- ddl/placement_sql_test.go | 38 +++++++++++++++++++------------------- ddl/schema.go | 13 ++++++++----- 2 files changed, 27 insertions(+), 24 deletions(-) diff --git a/ddl/placement_sql_test.go b/ddl/placement_sql_test.go index bf4e316e8ffeb..f4c969319a7e4 100644 --- a/ddl/placement_sql_test.go +++ b/ddl/placement_sql_test.go @@ -798,20 +798,20 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { tk.MustExec("drop database if exists TestAlterDB;") tk.MustExec("create database TestAlterDB;") tk.MustExec("use TestAlterDB") - tk.MustExec("drop placement policy if exists x") - tk.MustExec("drop placement policy if exists y") - tk.MustExec("create placement policy x PRIMARY_REGION=\"cn-east-1\", REGIONS=\"cn-east-1\";") - tk.MustExec("create placement policy y PRIMARY_REGION=\"cn-east-2\", REGIONS=\"cn-east-2\";") + tk.MustExec("drop placement policy if exists alter_x") + tk.MustExec("drop placement policy if exists alter_y") + tk.MustExec("create placement policy alter_x PRIMARY_REGION=\"cn-east-1\", REGIONS=\"cn-east-1\";") + tk.MustExec("create placement policy alter_y PRIMARY_REGION=\"cn-east-2\", REGIONS=\"cn-east-2\";") // Policy Test // Test for Non-Exist policy - tk.MustGetErrCode("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`z`;", mysql.ErrPlacementPolicyNotExists) + tk.MustGetErrCode("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`alter_z`;", mysql.ErrPlacementPolicyNotExists) - tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x`;") + tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`alter_x`;") // Test for Show Create Database tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - "/*T![placement] PLACEMENT POLICY=`x` */", + "/*T![placement] PLACEMENT POLICY=`alter_x` */", )) // Test for Alter Placement Policy affect table created. tk.MustExec("create table t(a int);") @@ -819,22 +819,22 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { "t CREATE TABLE `t` (\n"+ " `a` int(11) DEFAULT NULL\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ - "/*T![placement] PLACEMENT POLICY=`x` */", + "/*T![placement] PLACEMENT POLICY=`alter_x` */", )) // Test for Alter Default Placement Policy, And will not update the old table options. - tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y`;") + tk.MustExec("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`alter_y`;") tk.MustExec("create table t2(a int);") tk.MustQuery(`show create table t`).Check(testutil.RowsWithSep("|", "t CREATE TABLE `t` (\n"+ " `a` int(11) DEFAULT NULL\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ - "/*T![placement] PLACEMENT POLICY=`x` */", + "/*T![placement] PLACEMENT POLICY=`alter_x` */", )) tk.MustQuery(`show create table t2`).Check(testutil.RowsWithSep("|", "t2 CREATE TABLE `t2` (\n"+ " `a` int(11) DEFAULT NULL\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ - "/*T![placement] PLACEMENT POLICY=`y` */", + "/*T![placement] PLACEMENT POLICY=`alter_y` */", )) // Reset Test @@ -858,12 +858,12 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { "/*T![placement] PRIMARY_REGION=\"se\" REGIONS=\"se\" FOLLOWERS=2 */", )) // Test for override default option - tk.MustExec("create table t4(a int) PLACEMENT POLICY=\"x\";") + tk.MustExec("create table t4(a int) PLACEMENT POLICY=\"alter_x\";") tk.MustQuery(`show create table t4`).Check(testutil.RowsWithSep("|", "t4 CREATE TABLE `t4` (\n"+ " `a` int(11) DEFAULT NULL\n"+ ") ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin "+ - "/*T![placement] PLACEMENT POLICY=`x` */", + "/*T![placement] PLACEMENT POLICY=`alter_x` */", )) // Hybrid Test @@ -872,17 +872,17 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ "/*T![placement] PRIMARY_REGION=\"se\" REGIONS=\"se\" FOLLOWERS=2 */", )) - tk.MustGetErrCode("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x` FOLLOWERS=2;", mysql.ErrPlacementPolicyWithDirectOption) - tk.MustGetErrCode("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`y` PRIMARY_REGION=\"se\" FOLLOWERS=2;", mysql.ErrPlacementPolicyWithDirectOption) + tk.MustGetErrCode("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`alter_x` FOLLOWERS=2;", mysql.ErrPlacementPolicyWithDirectOption) + tk.MustGetErrCode("ALTER DATABASE TestAlterDB DEFAULT PLACEMENT POLICY=`alter_y` PRIMARY_REGION=\"se\" FOLLOWERS=2;", mysql.ErrPlacementPolicyWithDirectOption) tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ "/*T![placement] PRIMARY_REGION=\"se\" REGIONS=\"se\" FOLLOWERS=2 */", )) // Test for change direct options to policy. - tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`x`;") + tk.MustExec("ALTER DATABASE TestAlterDB PLACEMENT POLICY=`alter_x`;") tk.MustQuery(`show create database TestAlterDB`).Check(testutil.RowsWithSep("|", "TestAlterDB CREATE DATABASE `TestAlterDB` /*!40100 DEFAULT CHARACTER SET utf8mb4 */ "+ - "/*T![placement] PLACEMENT POLICY=`x` */", + "/*T![placement] PLACEMENT POLICY=`alter_x` */", )) // Test for change policy to direct options. tk.MustExec("ALTER DATABASE TestAlterDB PRIMARY_REGION=\"se\" FOLLOWERS=2 REGIONS=\"se\" ;") @@ -892,6 +892,6 @@ func (s *testDBSuite6) TestAlterDBPlacement(c *C) { )) tk.MustExec(`DROP DATABASE IF EXISTS TestAlterDB;`) - tk.MustExec("drop placement policy if exists x") - tk.MustExec("drop placement policy if exists y") + tk.MustExec("drop placement policy if exists alter_x") + tk.MustExec("drop placement policy if exists alter_y") } diff --git a/ddl/schema.go b/ddl/schema.go index 689261a821e1e..d27a588152264 100644 --- a/ddl/schema.go +++ b/ddl/schema.go @@ -154,15 +154,18 @@ func onModifySchemaDefaultPlacement(t *meta.Meta, job *model.Job) (ver int64, _ if err != nil { return ver, errors.Trace(err) } - //Double Check if policy exits while ddl executing - _, err = checkPlacementPolicyExistAndCancelNonExistJob(t, job, placementPolicyRef.ID) - if err != nil { - return ver, errors.Trace(err) + // Double Check if policy exits while ddl executing + if placementPolicyRef != nil { + _, err = checkPlacementPolicyExistAndCancelNonExistJob(t, job, placementPolicyRef.ID) + if err != nil { + return ver, errors.Trace(err) + } } // Notice: dbInfo.DirectPlacementOpts and dbInfo.PlacementPolicyRef can not be both not nil, which checked before constructing ddl job. // So that we can just check the two situation that do not need ddl: 1. DB.DP == DDL.DP && nil == nil 2. nil == nil && DB.PP == DDL.PP - if (*dbInfo.DirectPlacementOpts == *directPlacementOpts) && (*dbInfo.PlacementPolicyRef == *placementPolicyRef) { + if (directPlacementOpts != nil && dbInfo.DirectPlacementOpts != nil && *dbInfo.DirectPlacementOpts == *directPlacementOpts) || + (placementPolicyRef != nil && dbInfo.PlacementPolicyRef != nil && *dbInfo.PlacementPolicyRef == *placementPolicyRef) { job.FinishDBJob(model.JobStateDone, model.StatePublic, ver, dbInfo) return ver, nil }