Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

restore: remove restore meta sql in create table #27199

Merged
merged 14 commits into from
Aug 30, 2021
Merged
50 changes: 5 additions & 45 deletions br/pkg/restore/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ func (db *DB) CreateTable(ctx context.Context, table *metautil.Table) error {
return errors.Trace(err)
}

var restoreMetaSQL string
if table.Info.IsSequence() {
setValFormat := fmt.Sprintf("do setval(%s.%s, %%d);",
utils.EncloseName(table.DB.Name.O),
Expand Down Expand Up @@ -149,55 +148,16 @@ func (db *DB) CreateTable(ctx context.Context, table *metautil.Table) error {
return errors.Trace(err)
}
}
restoreMetaSQL = fmt.Sprintf(setValFormat, table.Info.AutoIncID)
err = db.se.Execute(ctx, restoreMetaSQL)
} else {
var alterAutoIncIDFormat string
switch {
case table.Info.IsView():
return nil
default:
alterAutoIncIDFormat = "alter table %s.%s auto_increment = %d;"
}
restoreMetaSQL = fmt.Sprintf(
alterAutoIncIDFormat,
utils.EncloseName(table.DB.Name.O),
utils.EncloseName(table.Info.Name.O),
table.Info.AutoIncID)
if utils.NeedAutoID(table.Info) {
err = db.se.Execute(ctx, restoreMetaSQL)
}
}

if err != nil {
log.Error("restore meta sql failed",
zap.String("query", restoreMetaSQL),
zap.Stringer("db", table.DB.Name),
zap.Stringer("table", table.Info.Name),
zap.Error(err))
return errors.Trace(err)
}
if table.Info.PKIsHandle && table.Info.ContainsAutoRandomBits() {
// this table has auto random id, we need rebase it

// we can't merge two alter query, because
// it will cause Error: [ddl:8200]Unsupported multi schema change
alterAutoRandIDSQL := fmt.Sprintf(
"alter table %s.%s auto_random_base = %d",
utils.EncloseName(table.DB.Name.O),
utils.EncloseName(table.Info.Name.O),
table.Info.AutoRandID)

err = db.se.Execute(ctx, alterAutoRandIDSQL)
if err != nil {
log.Error("alter AutoRandID failed",
zap.String("query", alterAutoRandIDSQL),
restoreMetaSQL := fmt.Sprintf(setValFormat, table.Info.AutoIncID)
if err = db.se.Execute(ctx, restoreMetaSQL); err != nil {
log.Error("restore meta sql failed",
zap.String("query", restoreMetaSQL),
zap.Stringer("db", table.DB.Name),
zap.Stringer("table", table.Info.Name),
zap.Error(err))
return errors.Trace(err)
}
}

return errors.Trace(err)
}

Expand Down
24 changes: 22 additions & 2 deletions br/tests/br_views_and_sequences/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@
set -eu
DB="$TEST_NAME"

trim_sql_result() {
tail -n1 | sed 's/[^0-9]//g'
}

run_sql "create schema $DB;"
run_sql "create view $DB.view_1 as select 331 as m;"
run_sql "create view $DB.view_2 as select * from $DB.view_1;"
Expand All @@ -29,6 +33,13 @@ run_sql "create view $DB.view_3 as select m from $DB.table_1 union select a * b
run_sql "drop view $DB.view_1;"
run_sql "create view $DB.view_1 as select 133 as m;"

run_sql "create table $DB.auto_inc (n int primary key AUTO_INCREMENT);"
run_sql "insert into $DB.auto_inc values (), (), (), (), ();"
last_id=$(run_sql "select n from $DB.auto_inc order by n desc limit 1" | trim_sql_result)

run_sql "create table $DB.auto_rnd (n BIGINT primary key AUTO_RANDOM(8));"
last_rnd_id=$(run_sql "insert into $DB.auto_rnd values (), (), (), (), ();select last_insert_id();" | trim_sql_result )
YuJuncen marked this conversation as resolved.
Show resolved Hide resolved

echo "backup start..."
run_br backup db --db "$DB" -s "local://$TEST_DIR/$DB" --pd $PD_ADDR

Expand All @@ -39,11 +50,20 @@ run_br restore db --db $DB -s "local://$TEST_DIR/$DB" --pd $PD_ADDR

set -x

views_count=$(run_sql "select count(*) c, sum(m) s from $DB.view_3;" | tail -2 | paste -sd ';')
views_count=$(run_sql "select count(*) c, sum(m) s from $DB.view_3;" | tail -2 | paste -sd ';' -)
[ "$views_count" = 'c: 8;s: 181' ]

run_sql "insert into $DB.table_2 (c) values (33);"
seq_val=$(run_sql "select a >= 8 and b >= 4 as g from $DB.table_2 where c = 33;" | tail -1)
[ "$seq_val" = 'g: 1' ]

run_sql "drop schema $DB"
run_sql "insert into $DB.auto_inc values ();"
last_id_after_restore=$(run_sql "select n from $DB.auto_inc order by n desc limit 1;" | trim_sql_result)
[ $last_id_after_restore -gt $last_id ]
rnd_last_id_after_restore=$(run_sql "insert into $DB.auto_rnd values ();select last_insert_id();" | trim_sql_result )
[ $(( rnd_last_id_after_restore & 0xff )) -gt $(( last_rnd_id & 0xff )) ]
YuJuncen marked this conversation as resolved.
Show resolved Hide resolved
rnd_count_after_restore=$(run_sql "select count(*) from $DB.auto_rnd;" | trim_sql_result )
[ $rnd_count_after_restore -gt 5 ]


run_sql "drop schema $DB"