From 19dff5bac736b65a2bdbac0c936dad37b859734a Mon Sep 17 00:00:00 2001 From: Hillium Date: Tue, 14 Jul 2020 17:39:38 +0800 Subject: [PATCH 1/4] restore: don't restore auto id if the table doesn't has it. --- pkg/restore/db.go | 12 +++++++----- pkg/utils/schema.go | 7 +++++++ 2 files changed, 14 insertions(+), 5 deletions(-) diff --git a/pkg/restore/db.go b/pkg/restore/db.go index dc862d53e..3d0e1eae6 100644 --- a/pkg/restore/db.go +++ b/pkg/restore/db.go @@ -158,11 +158,13 @@ func (db *DB) CreateTable(ctx context.Context, table *utils.Table) error { 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 table.NeedRebaseAutoID() { + restoreMetaSQL = fmt.Sprintf( + alterAutoIncIDFormat, + utils.EncloseName(table.Db.Name.O), + utils.EncloseName(table.Info.Name.O), + table.Info.AutoIncID) + } } err = db.se.Execute(ctx, restoreMetaSQL) diff --git a/pkg/utils/schema.go b/pkg/utils/schema.go index 739ce5c47..d8bf14f0c 100644 --- a/pkg/utils/schema.go +++ b/pkg/utils/schema.go @@ -40,6 +40,13 @@ func (tbl *Table) NoChecksum() bool { return tbl.Crc64Xor == 0 && tbl.TotalKvs == 0 && tbl.TotalBytes == 0 } +func (tbl *Table) NeedRebaseAutoID() bool { + tblInfo := tbl.Info + hasRowID := !tblInfo.PKIsHandle && !tblInfo.IsCommonHandle + hasAutoIncID := tblInfo.GetAutoIncrementColInfo() != nil + return hasRowID || hasAutoIncID +} + // Database wraps the schema and tables of a database. type Database struct { Info *model.DBInfo From 8d62e4ddb903f7c9486bc63e304d38967c0bfbac Mon Sep 17 00:00:00 2001 From: Hillium Date: Tue, 14 Jul 2020 17:48:16 +0800 Subject: [PATCH 2/4] restore: fix some bugs --- pkg/restore/db.go | 13 +++++++------ pkg/utils/schema.go | 1 + 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/pkg/restore/db.go b/pkg/restore/db.go index 3d0e1eae6..18aa82544 100644 --- a/pkg/restore/db.go +++ b/pkg/restore/db.go @@ -150,6 +150,7 @@ func (db *DB) CreateTable(ctx context.Context, table *utils.Table) error { } } restoreMetaSQL = fmt.Sprintf(setValFormat, table.Info.AutoIncID) + err = db.se.Execute(ctx, restoreMetaSQL) } else { var alterAutoIncIDFormat string switch { @@ -158,16 +159,16 @@ func (db *DB) CreateTable(ctx context.Context, table *utils.Table) error { 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 table.NeedRebaseAutoID() { - restoreMetaSQL = fmt.Sprintf( - alterAutoIncIDFormat, - utils.EncloseName(table.Db.Name.O), - utils.EncloseName(table.Info.Name.O), - table.Info.AutoIncID) + err = db.se.Execute(ctx, restoreMetaSQL) } } - err = db.se.Execute(ctx, restoreMetaSQL) if err != nil { log.Error("restore meta sql failed", zap.String("query", restoreMetaSQL), diff --git a/pkg/utils/schema.go b/pkg/utils/schema.go index d8bf14f0c..8abbcd910 100644 --- a/pkg/utils/schema.go +++ b/pkg/utils/schema.go @@ -40,6 +40,7 @@ func (tbl *Table) NoChecksum() bool { return tbl.Crc64Xor == 0 && tbl.TotalKvs == 0 && tbl.TotalBytes == 0 } +// NeedRebaseAutoID checks whether the table need to rebase its autoid. func (tbl *Table) NeedRebaseAutoID() bool { tblInfo := tbl.Info hasRowID := !tblInfo.PKIsHandle && !tblInfo.IsCommonHandle From d4826fa76b2c556c493efe74743844d358f70657 Mon Sep 17 00:00:00 2001 From: Hillium Date: Tue, 14 Jul 2020 18:02:25 +0800 Subject: [PATCH 3/4] backup: don't alloc auto ID if the table doesn't need it --- pkg/backup/client.go | 4 ++-- pkg/utils/schema.go | 10 +++++++--- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/pkg/backup/client.go b/pkg/backup/client.go index a86801434..456affd19 100644 --- a/pkg/backup/client.go +++ b/pkg/backup/client.go @@ -268,8 +268,8 @@ func BuildBackupRangeAndSchema( switch { case tableInfo.IsSequence(): globalAutoID, err = seqAlloc.NextGlobalAutoID(tableInfo.ID) - case tableInfo.IsView(): - // no auto ID for views. + case tableInfo.IsView() || !utils.NeedAutoID(tableInfo): + // no auto ID for views or table without either rowID nor auto_increment ID. default: globalAutoID, err = idAlloc.NextGlobalAutoID(tableInfo.ID) } diff --git a/pkg/utils/schema.go b/pkg/utils/schema.go index 8abbcd910..068077739 100644 --- a/pkg/utils/schema.go +++ b/pkg/utils/schema.go @@ -40,14 +40,18 @@ func (tbl *Table) NoChecksum() bool { return tbl.Crc64Xor == 0 && tbl.TotalKvs == 0 && tbl.TotalBytes == 0 } -// NeedRebaseAutoID checks whether the table need to rebase its autoid. -func (tbl *Table) NeedRebaseAutoID() bool { - tblInfo := tbl.Info +// NeedAutoID checks whether the table needs backing up with an autoid. +func NeedAutoID(tblInfo *model.TableInfo) bool { hasRowID := !tblInfo.PKIsHandle && !tblInfo.IsCommonHandle hasAutoIncID := tblInfo.GetAutoIncrementColInfo() != nil return hasRowID || hasAutoIncID } +// NeedRebaseAutoID checks whether the table need to rebase its autoid. +func (tbl *Table) NeedRebaseAutoID() bool { + return NeedAutoID(tbl.Info) +} + // Database wraps the schema and tables of a database. type Database struct { Info *model.DBInfo From 5eb08f6ec0df60d57b3313561328a17c6c35aac7 Mon Sep 17 00:00:00 2001 From: Hillium Date: Tue, 14 Jul 2020 19:25:04 +0800 Subject: [PATCH 4/4] restore: remove `table.NeedRebaseAutoID` --- pkg/restore/db.go | 2 +- pkg/utils/schema.go | 5 ----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/pkg/restore/db.go b/pkg/restore/db.go index 18aa82544..9c414e9f6 100644 --- a/pkg/restore/db.go +++ b/pkg/restore/db.go @@ -164,7 +164,7 @@ func (db *DB) CreateTable(ctx context.Context, table *utils.Table) error { utils.EncloseName(table.Db.Name.O), utils.EncloseName(table.Info.Name.O), table.Info.AutoIncID) - if table.NeedRebaseAutoID() { + if utils.NeedAutoID(table.Info) { err = db.se.Execute(ctx, restoreMetaSQL) } } diff --git a/pkg/utils/schema.go b/pkg/utils/schema.go index 068077739..418a767c9 100644 --- a/pkg/utils/schema.go +++ b/pkg/utils/schema.go @@ -47,11 +47,6 @@ func NeedAutoID(tblInfo *model.TableInfo) bool { return hasRowID || hasAutoIncID } -// NeedRebaseAutoID checks whether the table need to rebase its autoid. -func (tbl *Table) NeedRebaseAutoID() bool { - return NeedAutoID(tbl.Info) -} - // Database wraps the schema and tables of a database. type Database struct { Info *model.DBInfo