Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

restore: restore tidb config after restoring #509

Merged
merged 8 commits into from
Sep 17, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions pkg/task/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ package task

import (
"fmt"
"reflect"

"github.com/pingcap/tidb/config"

. "github.com/pingcap/check"
"github.com/spf13/pflag"
Expand Down Expand Up @@ -37,3 +40,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(reflect.DeepEqual(cfg, config.GetGlobalConfig()), IsFalse)
YuJuncen marked this conversation as resolved.
Show resolved Hide resolved
restoreConfig()
c.Assert(reflect.DeepEqual(cfg, config.GetGlobalConfig()), IsTrue)
YuJuncen marked this conversation as resolved.
Show resolved Hide resolved
}
32 changes: 18 additions & 14 deletions pkg/task/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -413,19 +414,22 @@ 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

config.StoreGlobalConfig(conf)
// 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
})
return restoreConfig
}

// restoreTableStream blocks current goroutine and restore a stream of tables,
Expand Down
5 changes: 4 additions & 1 deletion tests/br_db/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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 ( \
Expand All @@ -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
Expand All @@ -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;"
4 changes: 4 additions & 0 deletions tests/br_full/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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};"
Expand Down Expand Up @@ -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