From 96645a18904b8de3460c21ab35a7e9046df773e8 Mon Sep 17 00:00:00 2001 From: kennytm Date: Thu, 23 Apr 2020 02:52:00 +0800 Subject: [PATCH 1/8] tests: update the download URL of nightly TiDB --- tests/download_tools.sh | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/tests/download_tools.sh b/tests/download_tools.sh index e0689dd61..fb8a71604 100755 --- a/tests/download_tools.sh +++ b/tests/download_tools.sh @@ -27,19 +27,18 @@ fi MISSING_TIDB_COMPONENTS= for COMPONENT in tidb-server pd-server tikv-server pd-ctl; do if [ ! -e "$BIN/$COMPONENT" ]; then - MISSING_TIDB_COMPONENTS="$MISSING_TIDB_COMPONENTS tidb-latest-linux-amd64/bin/$COMPONENT" + MISSING_TIDB_COMPONENTS="$MISSING_TIDB_COMPONENTS tidb-nightly-linux-amd64/bin/$COMPONENT" fi done if [ -n "$MISSING_TIDB_COMPONENTS" ]; then echo "Downloading latest TiDB bundle..." - # TODO: the url is going to change from 'latest' to 'nightly' someday. - curl -L -f -o "$BIN/tidb.tar.gz" "https://download.pingcap.org/tidb-latest-linux-amd64.tar.gz" + curl -L -f -o "$BIN/tidb.tar.gz" "https://download.pingcap.org/tidb-nightly-linux-amd64.tar.gz" tar -x -f "$BIN/tidb.tar.gz" -C "$BIN/" $MISSING_TIDB_COMPONENTS rm "$BIN/tidb.tar.gz" - mv "$BIN"/tidb-latest-linux-amd64/bin/* "$BIN/" - rmdir "$BIN/tidb-latest-linux-amd64/bin" - rmdir "$BIN/tidb-latest-linux-amd64" + mv "$BIN"/tidb-nightly-linux-amd64/bin/* "$BIN/" + rmdir "$BIN/tidb-nightly-linux-amd64/bin" + rmdir "$BIN/tidb-nightly-linux-amd64" fi if [ ! -e "$BIN/go-ycsb" ]; then From b50047653ce500f5bca86f33080c6fffd368aecb Mon Sep 17 00:00:00 2001 From: kennytm Date: Thu, 23 Apr 2020 04:00:16 +0800 Subject: [PATCH 2/8] backup,restore: support adjusting sequence value --- pkg/backup/client.go | 11 ++++++++++- pkg/restore/db.go | 12 +++++++++++- 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/pkg/backup/client.go b/pkg/backup/client.go index 1b016ba8b..d11172802 100644 --- a/pkg/backup/client.go +++ b/pkg/backup/client.go @@ -215,13 +215,22 @@ func BuildBackupRangeAndSchema( var dbData []byte idAlloc := autoid.NewAllocator(storage, dbInfo.ID, false, autoid.RowIDAllocType) + seqAlloc := autoid.NewAllocator(storage, dbInfo.ID, false, autoid.SequenceType) for _, tableInfo := range dbInfo.Tables { if !tableFilter.Match(&filter.Table{Schema: dbInfo.Name.L, Name: tableInfo.Name.L}) { // Skip tables other than the given table. continue } - globalAutoID, err := idAlloc.NextGlobalAutoID(tableInfo.ID) + var globalAutoID int64 + switch { + case tableInfo.IsSequence(): + globalAutoID, err = seqAlloc.NextGlobalAutoID(tableInfo.ID) + case tableInfo.IsView(): + // no auto ID for views. + default: + globalAutoID, err = idAlloc.NextGlobalAutoID(tableInfo.ID) + } if err != nil { return nil, nil, errors.Trace(err) } diff --git a/pkg/restore/db.go b/pkg/restore/db.go index 4ff1b5ea2..078afc3dc 100644 --- a/pkg/restore/db.go +++ b/pkg/restore/db.go @@ -106,8 +106,18 @@ func (db *DB) CreateTable(ctx context.Context, table *utils.Table) error { zap.Error(err)) return errors.Trace(err) } + + var alterAutoIncIDFormat string + switch { + case table.Info.IsSequence(): + alterAutoIncIDFormat = "do setval(%s.%s, %d);" + case table.Info.IsView(): + return nil + default: + alterAutoIncIDFormat = "alter table %s.%s auto_increment = %d;" + } alterAutoIncIDSQL := fmt.Sprintf( - "alter table %s.%s auto_increment = %d", + alterAutoIncIDFormat, utils.EncloseName(table.Db.Name.O), utils.EncloseName(table.Info.Name.O), table.Info.AutoIncID) From 44610192b5073aada55ad6c15dba7f32b4383771 Mon Sep 17 00:00:00 2001 From: kennytm Date: Thu, 23 Apr 2020 05:14:57 +0800 Subject: [PATCH 3/8] tests: added test case for views and sequences --- tests/br_views_and_sequences/run.sh | 47 +++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100755 tests/br_views_and_sequences/run.sh diff --git a/tests/br_views_and_sequences/run.sh b/tests/br_views_and_sequences/run.sh new file mode 100755 index 000000000..7261898a2 --- /dev/null +++ b/tests/br_views_and_sequences/run.sh @@ -0,0 +1,47 @@ +#!/bin/sh +# +# Copyright 2020 PingCAP, Inc. +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# See the License for the specific language governing permissions and +# limitations under the License. + +set -eu +DB="$TEST_NAME" + +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;" +run_sql "create sequence $DB.seq_1 nocache cycle maxvalue 40;" +run_sql "create table $DB.table_1 (m int primary key default next value for $DB.seq_1, b int);" +run_sql "insert into $DB.table_1 (b) values (8), (12), (16), (20);" +run_sql "create sequence $DB.seq_2;" +run_sql "create table $DB.table_2 (a int default next value for $DB.seq_1, b int default next value for $DB.seq_2, c int);" +run_sql "insert into $DB.table_2 (c) values (24), (28), (32);" +run_sql "create view $DB.view_3 as select m from $DB.table_1 union select a * b as m from $DB.table_2 union select m from $DB.view_2;" +run_sql "drop view $DB.view_1;" +run_sql "create view $DB.view_1 as select 133 as m;" + +echo "backup start..." +run_br backup db --db "$DB" -s "local://$TEST_DIR/$DB" --pd $PD_ADDR + +run_sql "drop schema $DB;" + +echo "restore start..." +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 = 'c: 8;s: 181' ] + +run_sql "insert into $DB.table_2 (c) values (33);" +seq_val=$(run_sql "select a, b >= 4 as bg from $DB.table_2 where c = 33;" | tail -2 | paste -sd ';') +[ $seq_val = 'a: 8;bg: 1' ] From 57232f3eaaec7d6f994bbd03f9c8b70cf27713d1 Mon Sep 17 00:00:00 2001 From: kennytm Date: Thu, 23 Apr 2020 15:26:14 +0800 Subject: [PATCH 4/8] tests: fix and relax test --- tests/br_views_and_sequences/run.sh | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/br_views_and_sequences/run.sh b/tests/br_views_and_sequences/run.sh index 7261898a2..345d3aa15 100755 --- a/tests/br_views_and_sequences/run.sh +++ b/tests/br_views_and_sequences/run.sh @@ -40,8 +40,8 @@ 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 = 'c: 8;s: 181' ] +[ "$views_count" = 'c: 8;s: 181' ] run_sql "insert into $DB.table_2 (c) values (33);" -seq_val=$(run_sql "select a, b >= 4 as bg from $DB.table_2 where c = 33;" | tail -2 | paste -sd ';') -[ $seq_val = 'a: 8;bg: 1' ] +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' ] From b7efca489d6b9af17e5f51c74a81b028cdc8aa1c Mon Sep 17 00:00:00 2001 From: kennytm Date: Wed, 6 May 2020 01:42:46 +0800 Subject: [PATCH 5/8] backup: fix gofmt --- pkg/backup/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pkg/backup/client.go b/pkg/backup/client.go index bebb1d3e4..53b385d3d 100644 --- a/pkg/backup/client.go +++ b/pkg/backup/client.go @@ -215,7 +215,7 @@ func BuildBackupRangeAndSchema( var dbData []byte idAlloc := autoid.NewAllocator(storage, dbInfo.ID, false, autoid.RowIDAllocType) - seqAlloc := autoid.NewAllocator(storage, dbInfo.ID, false, autoid.SequenceType) + seqAlloc := autoid.NewAllocator(storage, dbInfo.ID, false, autoid.SequenceType) randAlloc := autoid.NewAllocator(storage, dbInfo.ID, false, autoid.AutoRandomType) for _, tableInfo := range dbInfo.Tables { From fc27535f626f361618ad9c75a99ff31deb001259 Mon Sep 17 00:00:00 2001 From: luancheng Date: Fri, 22 May 2020 20:00:45 +0800 Subject: [PATCH 6/8] add backupsquence config && trigger sequence cycle round --- pkg/backup/client.go | 5 ++++ pkg/backup/schema_test.go | 10 +++---- pkg/restore/db.go | 55 +++++++++++++++++++++++++++------------ pkg/task/backup.go | 11 +++++++- 4 files changed, 59 insertions(+), 22 deletions(-) diff --git a/pkg/backup/client.go b/pkg/backup/client.go index c0651d7a7..e88e06470 100644 --- a/pkg/backup/client.go +++ b/pkg/backup/client.go @@ -212,6 +212,7 @@ func BuildBackupRangeAndSchema( storage kv.Storage, tableFilter *filter.Filter, backupTS uint64, + backupSequence bool, ) ([]rtree.Range, *Schemas, error) { info, err := dom.GetSnapshotInfoSchema(backupTS) if err != nil { @@ -239,6 +240,10 @@ func BuildBackupRangeAndSchema( var globalAutoID int64 switch { case tableInfo.IsSequence(): + if !backupSequence { + // don't backup sequence + continue + } globalAutoID, err = seqAlloc.NextGlobalAutoID(tableInfo.ID) case tableInfo.IsView(): // no auto ID for views. diff --git a/pkg/backup/schema_test.go b/pkg/backup/schema_test.go index 06584a6b5..a9defdf5b 100644 --- a/pkg/backup/schema_test.go +++ b/pkg/backup/schema_test.go @@ -62,7 +62,7 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) { }) c.Assert(err, IsNil) _, backupSchemas, err := backup.BuildBackupRangeAndSchema( - s.mock.Domain, s.mock.Storage, testFilter, math.MaxUint64) + s.mock.Domain, s.mock.Storage, testFilter, math.MaxUint64, false) c.Assert(err, IsNil) c.Assert(backupSchemas, IsNil) @@ -72,7 +72,7 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) { }) c.Assert(err, IsNil) _, backupSchemas, err = backup.BuildBackupRangeAndSchema( - s.mock.Domain, s.mock.Storage, fooFilter, math.MaxUint64) + s.mock.Domain, s.mock.Storage, fooFilter, math.MaxUint64, false) c.Assert(err, IsNil) c.Assert(backupSchemas, IsNil) @@ -80,7 +80,7 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) { noFilter, err := filter.New(false, &filter.Rules{}) c.Assert(err, IsNil) _, backupSchemas, err = backup.BuildBackupRangeAndSchema( - s.mock.Domain, s.mock.Storage, noFilter, math.MaxUint64) + s.mock.Domain, s.mock.Storage, noFilter, math.MaxUint64, false) c.Assert(err, IsNil) c.Assert(backupSchemas, IsNil) @@ -90,7 +90,7 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) { tk.MustExec("insert into t1 values (10);") _, backupSchemas, err = backup.BuildBackupRangeAndSchema( - s.mock.Domain, s.mock.Storage, testFilter, math.MaxUint64) + s.mock.Domain, s.mock.Storage, testFilter, math.MaxUint64, false) c.Assert(err, IsNil) c.Assert(backupSchemas.Len(), Equals, 1) updateCh := new(simpleProgress) @@ -110,7 +110,7 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) { tk.MustExec("insert into t2 values (11);") _, backupSchemas, err = backup.BuildBackupRangeAndSchema( - s.mock.Domain, s.mock.Storage, noFilter, math.MaxUint64) + s.mock.Domain, s.mock.Storage, noFilter, math.MaxUint64, false) c.Assert(err, IsNil) c.Assert(backupSchemas.Len(), Equals, 2) updateCh.reset() diff --git a/pkg/restore/db.go b/pkg/restore/db.go index 9d345f8eb..d2d8d1ddc 100644 --- a/pkg/restore/db.go +++ b/pkg/restore/db.go @@ -107,25 +107,48 @@ func (db *DB) CreateTable(ctx context.Context, table *utils.Table) error { return errors.Trace(err) } - var alterAutoIncIDFormat string - switch { - case table.Info.IsSequence(): - alterAutoIncIDFormat = "do setval(%s.%s, %d);" - case table.Info.IsView(): - return nil - default: - alterAutoIncIDFormat = "alter table %s.%s auto_increment = %d;" + var restoreMetaSQL string + if table.Info.IsSequence() { + increment := table.Info.Sequence.Increment + // TiDB sequence's behaviour is designed to keep the same pace + // among all nodes within the same cluster. so we need restore round. + // Here is a hack way to trigger sequence cycle round > 0 according to + // https://github.com/pingcap/br/pull/242#issuecomment-631307978 + // TODO use sql to set cycle round + setValFormat := fmt.Sprintf("do setval(%s.%s, %%d);", + utils.EncloseName(table.Db.Name.O), + utils.EncloseName(table.Info.Name.O)) + nextSeqSQL := fmt.Sprintf("do nextval(%s.%s);", + utils.EncloseName(table.Db.Name.O), + utils.EncloseName(table.Info.Name.O)) + + if increment < 0 { + restoreMetaSQL += fmt.Sprintf(setValFormat, table.Info.Sequence.MinValue) + } else { + restoreMetaSQL += fmt.Sprintf(setValFormat, table.Info.Sequence.MaxValue) + } + // trigger cycle round > 0 + restoreMetaSQL += nextSeqSQL + restoreMetaSQL += fmt.Sprintf(setValFormat, table.Info.AutoIncID) + } 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) } - alterAutoIncIDSQL := fmt.Sprintf( - alterAutoIncIDFormat, - utils.EncloseName(table.Db.Name.O), - utils.EncloseName(table.Info.Name.O), - table.Info.AutoIncID) - err = db.se.Execute(ctx, alterAutoIncIDSQL) + err = db.se.Execute(ctx, restoreMetaSQL) if err != nil { - log.Error("alter AutoIncID failed", - zap.String("query", alterAutoIncIDSQL), + 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)) diff --git a/pkg/task/backup.go b/pkg/task/backup.go index 9d055bc5c..b5fb75232 100644 --- a/pkg/task/backup.go +++ b/pkg/task/backup.go @@ -34,6 +34,7 @@ const ( flagLastBackupTS = "lastbackupts" flagGCTTL = "gcttl" + flagBackupSequence = "backupsequence" defaultBackupConcurrency = 4 ) @@ -46,6 +47,7 @@ type BackupConfig struct { BackupTS uint64 `json:"backup-ts" toml:"backup-ts"` LastBackupTS uint64 `json:"last-backup-ts" toml:"last-backup-ts"` GCTTL int64 `json:"gc-ttl" toml:"gc-ttl"` + BackupSequence bool `json:"backup-sequence" toml:"backup-sequence"` } // DefineBackupFlags defines common flags for the backup command. @@ -60,6 +62,7 @@ func DefineBackupFlags(flags *pflag.FlagSet) { flags.String(flagBackupTS, "", "the backup ts support TSO or datetime,"+ " e.g. '400036290571534337', '2018-05-11 01:42:23'") flags.Int64(flagGCTTL, backup.DefaultBRGCSafePointTTL, "the TTL (in seconds) that PD holds for BR's GC safepoint") + flags.Bool(flagBackupSequence, false, "whether backup sequence or not") } // ParseFromFlags parses the backup-related flags from the flag set. @@ -90,6 +93,12 @@ func (cfg *BackupConfig) ParseFromFlags(flags *pflag.FlagSet) error { } cfg.GCTTL = gcTTL + backupSequence, err := flags.GetBool(flagBackupSequence) + if err != nil { + return errors.Trace(err) + } + cfg.BackupSequence = backupSequence + if err = cfg.Config.ParseFromFlags(flags); err != nil { return errors.Trace(err) } @@ -135,7 +144,7 @@ func RunBackup(c context.Context, g glue.Glue, cmdName string, cfg *BackupConfig g.Record("BackupTS", backupTS) ranges, backupSchemas, err := backup.BuildBackupRangeAndSchema( - mgr.GetDomain(), mgr.GetTiKV(), tableFilter, backupTS) + mgr.GetDomain(), mgr.GetTiKV(), tableFilter, backupTS, cfg.BackupSequence) if err != nil { return err } From 7146861d24e1e1b9ba5c8df7cd877e41a8431ca2 Mon Sep 17 00:00:00 2001 From: luancheng Date: Mon, 25 May 2020 12:38:33 +0800 Subject: [PATCH 7/8] remove flag backupseqence --- pkg/backup/client.go | 5 ----- pkg/backup/schema_test.go | 10 +++++----- pkg/task/backup.go | 11 +---------- 3 files changed, 6 insertions(+), 20 deletions(-) diff --git a/pkg/backup/client.go b/pkg/backup/client.go index e88e06470..c0651d7a7 100644 --- a/pkg/backup/client.go +++ b/pkg/backup/client.go @@ -212,7 +212,6 @@ func BuildBackupRangeAndSchema( storage kv.Storage, tableFilter *filter.Filter, backupTS uint64, - backupSequence bool, ) ([]rtree.Range, *Schemas, error) { info, err := dom.GetSnapshotInfoSchema(backupTS) if err != nil { @@ -240,10 +239,6 @@ func BuildBackupRangeAndSchema( var globalAutoID int64 switch { case tableInfo.IsSequence(): - if !backupSequence { - // don't backup sequence - continue - } globalAutoID, err = seqAlloc.NextGlobalAutoID(tableInfo.ID) case tableInfo.IsView(): // no auto ID for views. diff --git a/pkg/backup/schema_test.go b/pkg/backup/schema_test.go index a9defdf5b..06584a6b5 100644 --- a/pkg/backup/schema_test.go +++ b/pkg/backup/schema_test.go @@ -62,7 +62,7 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) { }) c.Assert(err, IsNil) _, backupSchemas, err := backup.BuildBackupRangeAndSchema( - s.mock.Domain, s.mock.Storage, testFilter, math.MaxUint64, false) + s.mock.Domain, s.mock.Storage, testFilter, math.MaxUint64) c.Assert(err, IsNil) c.Assert(backupSchemas, IsNil) @@ -72,7 +72,7 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) { }) c.Assert(err, IsNil) _, backupSchemas, err = backup.BuildBackupRangeAndSchema( - s.mock.Domain, s.mock.Storage, fooFilter, math.MaxUint64, false) + s.mock.Domain, s.mock.Storage, fooFilter, math.MaxUint64) c.Assert(err, IsNil) c.Assert(backupSchemas, IsNil) @@ -80,7 +80,7 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) { noFilter, err := filter.New(false, &filter.Rules{}) c.Assert(err, IsNil) _, backupSchemas, err = backup.BuildBackupRangeAndSchema( - s.mock.Domain, s.mock.Storage, noFilter, math.MaxUint64, false) + s.mock.Domain, s.mock.Storage, noFilter, math.MaxUint64) c.Assert(err, IsNil) c.Assert(backupSchemas, IsNil) @@ -90,7 +90,7 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) { tk.MustExec("insert into t1 values (10);") _, backupSchemas, err = backup.BuildBackupRangeAndSchema( - s.mock.Domain, s.mock.Storage, testFilter, math.MaxUint64, false) + s.mock.Domain, s.mock.Storage, testFilter, math.MaxUint64) c.Assert(err, IsNil) c.Assert(backupSchemas.Len(), Equals, 1) updateCh := new(simpleProgress) @@ -110,7 +110,7 @@ func (s *testBackupSchemaSuite) TestBuildBackupRangeAndSchema(c *C) { tk.MustExec("insert into t2 values (11);") _, backupSchemas, err = backup.BuildBackupRangeAndSchema( - s.mock.Domain, s.mock.Storage, noFilter, math.MaxUint64, false) + s.mock.Domain, s.mock.Storage, noFilter, math.MaxUint64) c.Assert(err, IsNil) c.Assert(backupSchemas.Len(), Equals, 2) updateCh.reset() diff --git a/pkg/task/backup.go b/pkg/task/backup.go index b5fb75232..9d055bc5c 100644 --- a/pkg/task/backup.go +++ b/pkg/task/backup.go @@ -34,7 +34,6 @@ const ( flagLastBackupTS = "lastbackupts" flagGCTTL = "gcttl" - flagBackupSequence = "backupsequence" defaultBackupConcurrency = 4 ) @@ -47,7 +46,6 @@ type BackupConfig struct { BackupTS uint64 `json:"backup-ts" toml:"backup-ts"` LastBackupTS uint64 `json:"last-backup-ts" toml:"last-backup-ts"` GCTTL int64 `json:"gc-ttl" toml:"gc-ttl"` - BackupSequence bool `json:"backup-sequence" toml:"backup-sequence"` } // DefineBackupFlags defines common flags for the backup command. @@ -62,7 +60,6 @@ func DefineBackupFlags(flags *pflag.FlagSet) { flags.String(flagBackupTS, "", "the backup ts support TSO or datetime,"+ " e.g. '400036290571534337', '2018-05-11 01:42:23'") flags.Int64(flagGCTTL, backup.DefaultBRGCSafePointTTL, "the TTL (in seconds) that PD holds for BR's GC safepoint") - flags.Bool(flagBackupSequence, false, "whether backup sequence or not") } // ParseFromFlags parses the backup-related flags from the flag set. @@ -93,12 +90,6 @@ func (cfg *BackupConfig) ParseFromFlags(flags *pflag.FlagSet) error { } cfg.GCTTL = gcTTL - backupSequence, err := flags.GetBool(flagBackupSequence) - if err != nil { - return errors.Trace(err) - } - cfg.BackupSequence = backupSequence - if err = cfg.Config.ParseFromFlags(flags); err != nil { return errors.Trace(err) } @@ -144,7 +135,7 @@ func RunBackup(c context.Context, g glue.Glue, cmdName string, cfg *BackupConfig g.Record("BackupTS", backupTS) ranges, backupSchemas, err := backup.BuildBackupRangeAndSchema( - mgr.GetDomain(), mgr.GetTiKV(), tableFilter, backupTS, cfg.BackupSequence) + mgr.GetDomain(), mgr.GetTiKV(), tableFilter, backupTS) if err != nil { return err } From ca4b3ffd935856f194774c07e2d75516b5bf2fa9 Mon Sep 17 00:00:00 2001 From: luancheng Date: Mon, 25 May 2020 12:46:18 +0800 Subject: [PATCH 8/8] address comment --- pkg/restore/db.go | 35 +++++++++++++++++++---------------- 1 file changed, 19 insertions(+), 16 deletions(-) diff --git a/pkg/restore/db.go b/pkg/restore/db.go index d2d8d1ddc..70a250a15 100644 --- a/pkg/restore/db.go +++ b/pkg/restore/db.go @@ -109,27 +109,30 @@ func (db *DB) CreateTable(ctx context.Context, table *utils.Table) error { var restoreMetaSQL string if table.Info.IsSequence() { - increment := table.Info.Sequence.Increment - // TiDB sequence's behaviour is designed to keep the same pace - // among all nodes within the same cluster. so we need restore round. - // Here is a hack way to trigger sequence cycle round > 0 according to - // https://github.com/pingcap/br/pull/242#issuecomment-631307978 - // TODO use sql to set cycle round setValFormat := fmt.Sprintf("do setval(%s.%s, %%d);", utils.EncloseName(table.Db.Name.O), utils.EncloseName(table.Info.Name.O)) - nextSeqSQL := fmt.Sprintf("do nextval(%s.%s);", - utils.EncloseName(table.Db.Name.O), - utils.EncloseName(table.Info.Name.O)) - - if increment < 0 { - restoreMetaSQL += fmt.Sprintf(setValFormat, table.Info.Sequence.MinValue) + if table.Info.Sequence.Cycle { + increment := table.Info.Sequence.Increment + // TiDB sequence's behaviour is designed to keep the same pace + // among all nodes within the same cluster. so we need restore round. + // Here is a hack way to trigger sequence cycle round > 0 according to + // https://github.com/pingcap/br/pull/242#issuecomment-631307978 + // TODO use sql to set cycle round + nextSeqSQL := fmt.Sprintf("do nextval(%s.%s);", + utils.EncloseName(table.Db.Name.O), + utils.EncloseName(table.Info.Name.O)) + if increment < 0 { + restoreMetaSQL += fmt.Sprintf(setValFormat, table.Info.Sequence.MinValue) + } else { + restoreMetaSQL += fmt.Sprintf(setValFormat, table.Info.Sequence.MaxValue) + } + // trigger cycle round > 0 + restoreMetaSQL += nextSeqSQL + restoreMetaSQL += fmt.Sprintf(setValFormat, table.Info.AutoIncID) } else { - restoreMetaSQL += fmt.Sprintf(setValFormat, table.Info.Sequence.MaxValue) + restoreMetaSQL = fmt.Sprintf(setValFormat, table.Info.AutoIncID) } - // trigger cycle round > 0 - restoreMetaSQL += nextSeqSQL - restoreMetaSQL += fmt.Sprintf(setValFormat, table.Info.AutoIncID) } else { var alterAutoIncIDFormat string switch {