diff --git a/pkg/task/common_test.go b/pkg/task/common_test.go index 6c4869809..b28809d59 100644 --- a/pkg/task/common_test.go +++ b/pkg/task/common_test.go @@ -5,6 +5,8 @@ package task import ( "fmt" + "github.com/pingcap/tidb/config" + . "github.com/pingcap/check" "github.com/spf13/pflag" ) @@ -37,3 +39,11 @@ func (*testCommonSuite) TestUrlNoQuery(c *C) { c.Assert(field.Key, Equals, flagStorage) c.Assert(field.Interface.(fmt.Stringer).String(), Equals, "s3://some/what") } + +func (s *testCommonSuite) TestTiDBConfigUnchanged(c *C) { + cfg := config.GetGlobalConfig() + restoreConfig := enableTiDBConfig() + c.Assert(cfg, Not(DeepEquals), config.GetGlobalConfig()) + restoreConfig() + c.Assert(cfg, DeepEquals, config.GetGlobalConfig()) +} diff --git a/pkg/task/restore.go b/pkg/task/restore.go index 3cb5e867b..0e57f7a68 100644 --- a/pkg/task/restore.go +++ b/pkg/task/restore.go @@ -154,7 +154,8 @@ func RunRestore(c context.Context, g glue.Glue, cmdName string, cfg *RestoreConf ddlJobs := restore.FilterDDLJobs(client.GetDDLJobs(), tables) // pre-set TiDB config for restore - enableTiDBConfig() + restoreDBConfig := enableTiDBConfig() + defer restoreDBConfig() // execute DDL first err = client.ExecDDLs(ddlJobs) @@ -413,21 +414,23 @@ func RunRestoreTiflashReplica(c context.Context, g glue.Glue, cmdName string, cf return nil } -func enableTiDBConfig() { - // set max-index-length before execute DDLs and create tables - // we set this value to max(3072*4), otherwise we might not restore table - // when upstream and downstream both set this value greater than default(3072) - conf := config.GetGlobalConfig() - conf.MaxIndexLength = config.DefMaxOfMaxIndexLength - log.Warn("set max-index-length to max(3072*4) to skip check index length in DDL") - - // we need set this to true, since all create table DDLs will create with tableInfo - // and we can handle alter drop pk/add pk DDLs with no impact - conf.AlterPrimaryKey = true - - conf.Experimental.AllowsExpressionIndex = true +// enableTiDBConfig tweaks some of configs of TiDB to make the restore progress go well. +// return a function that could restore the config to origin. +func enableTiDBConfig() func() { + restoreConfig := config.RestoreFunc() + config.UpdateGlobal(func(conf *config.Config) { + // set max-index-length before execute DDLs and create tables + // we set this value to max(3072*4), otherwise we might not restore table + // when upstream and downstream both set this value greater than default(3072) + conf.MaxIndexLength = config.DefMaxOfMaxIndexLength + log.Warn("set max-index-length to max(3072*4) to skip check index length in DDL") + + // we need set this to true, since all create table DDLs will create with tableInfo + // and we can handle alter drop pk/add pk DDLs with no impact + conf.AlterPrimaryKey = true + }) - config.StoreGlobalConfig(conf) + return restoreConfig } // restoreTableStream blocks current goroutine and restore a stream of tables, diff --git a/tests/br_db/run.sh b/tests/br_db/run.sh index 4ea4a2d39..de515bd06 100755 --- a/tests/br_db/run.sh +++ b/tests/br_db/run.sh @@ -16,6 +16,7 @@ set -eu DB="$TEST_NAME" +old_conf=$(run_sql "show config where name = 'alter-primary-key'") run_sql "CREATE DATABASE $DB;" run_sql "CREATE TABLE $DB.usertable1 ( \ @@ -34,7 +35,6 @@ run_sql "CREATE TABLE $DB.usertable2 ( \ ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;" run_sql "INSERT INTO $DB.usertable2 VALUES (\"c\", \"d\");" - # backup db echo "backup start..." run_br --pd $PD_ADDR backup db --db "$DB" -s "local://$TEST_DIR/$DB" --ratelimit 5 --concurrency 4 @@ -56,4 +56,7 @@ echo "testing DDL query..." curl 127.0.0.1:10080/ddl/history | grep -E '/\*from\(br\)\*/CREATE TABLE' curl 127.0.0.1:10080/ddl/history | grep -E '/\*from\(br\)\*/CREATE DATABASE' +# test whether we have changed the cluster config. +test "$old_conf" = "$(run_sql "show config where name = 'alter-primary-key'")" + run_sql "DROP DATABASE $DB;" diff --git a/tests/br_full/run.sh b/tests/br_full/run.sh index 6100c980d..6fe00a17d 100755 --- a/tests/br_full/run.sh +++ b/tests/br_full/run.sh @@ -17,6 +17,7 @@ set -eu DB="$TEST_NAME" TABLE="usertable" DB_COUNT=3 +old_conf=$(run_sql "show config where name = 'alter-primary-key'") for i in $(seq $DB_COUNT); do run_sql "CREATE DATABASE $DB${i};" @@ -71,6 +72,9 @@ for ct in lz4 zstd; do fi done +# test whether we have changed the cluster config. +test "$old_conf" = "$(run_sql "show config where name = 'alter-primary-key'")" + for i in $(seq $DB_COUNT); do run_sql "DROP DATABASE $DB${i};" done