Skip to content

Commit

Permalink
update name
Browse files Browse the repository at this point in the history
  • Loading branch information
3pointer committed Oct 25, 2021
1 parent 5e5979e commit 9aac1db
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
16 changes: 8 additions & 8 deletions br/pkg/restore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -401,12 +401,12 @@ func (rc *Client) createTable(
dom *domain.Domain,
table *metautil.Table,
newTS uint64,
uniqueMap map[UniqueName]bool,
ddlTables map[UniqueTableName]bool,
) (CreatedTable, error) {
if rc.IsSkipCreateSQL() {
log.Info("skip create table and alter autoIncID", zap.Stringer("table", table.Info.Name))
} else {
err := db.CreateTable(ctx, table, uniqueMap)
err := db.CreateTable(ctx, table, ddlTables)
if err != nil {
return CreatedTable{}, errors.Trace(err)
}
Expand Down Expand Up @@ -445,7 +445,7 @@ func (rc *Client) GoCreateTables(
// Could we have a smaller size of tables?
log.Info("start create tables")

uniqueMap := rc.DDLJobsMap()
ddlTables := rc.DDLJobsMap()
if span := opentracing.SpanFromContext(ctx); span != nil && span.Tracer() != nil {
span1 := span.Tracer().StartSpan("Client.GoCreateTables", opentracing.ChildOf(span.Context()))
defer span1.Finish()
Expand All @@ -459,7 +459,7 @@ func (rc *Client) GoCreateTables(
return c.Err()
default:
}
rt, err := rc.createTable(c, db, dom, t, newTS, uniqueMap)
rt, err := rc.createTable(c, db, dom, t, newTS, ddlTables)
if err != nil {
log.Error("create table failed",
zap.Error(err),
Expand Down Expand Up @@ -1059,18 +1059,18 @@ func (rc *Client) IsSkipCreateSQL() bool {
return rc.noSchema
}

// DDLJobsMap returns a map[UniqueName]bool about < db table, hasCreate/hasTruncate DDL >.
// DDLJobsMap returns a map[UniqueTableName]bool about < db table, hasCreate/hasTruncate DDL >.
// if we execute some DDLs before create table.
// we may get two situation that need to rebase auto increment/random id.
// 1. truncate table: truncate will generate new id cache.
// 2. create table/create and rename table: the first create table will lock down the id cache.
// because we cannot create onExistReplace table.
// so the final create DDL with the correct auto increment/random id won't be executed.
func (rc *Client) DDLJobsMap() map[UniqueName]bool {
m := make(map[UniqueName]bool)
func (rc *Client) DDLJobsMap() map[UniqueTableName]bool {
m := make(map[UniqueTableName]bool)
for _, job := range rc.ddlJobs {
if job.Type == model.ActionTruncateTable || job.Type == model.ActionCreateTable {
m[UniqueName{job.SchemaName, job.BinlogInfo.TableInfo.Name.String()}] = true
m[UniqueTableName{job.SchemaName, job.BinlogInfo.TableInfo.Name.String()}] = true
}
}
return m
Expand Down
12 changes: 6 additions & 6 deletions br/pkg/restore/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ type DB struct {
se glue.Session
}

type UniqueName struct {
type UniqueTableName struct {
DB string
Table string
}
Expand Down Expand Up @@ -102,7 +102,7 @@ func (db *DB) CreateDatabase(ctx context.Context, schema *model.DBInfo) error {
}

// CreateTable executes a CREATE TABLE SQL.
func (db *DB) CreateTable(ctx context.Context, table *metautil.Table, uniqueMap map[UniqueName]bool) error {
func (db *DB) CreateTable(ctx context.Context, table *metautil.Table, ddlTables map[UniqueTableName]bool) error {
err := db.se.CreateTable(ctx, table.DB.Name, table.Info)
if err != nil {
log.Error("create table failed",
Expand Down Expand Up @@ -165,7 +165,7 @@ func (db *DB) CreateTable(ctx context.Context, table *metautil.Table, uniqueMap
utils.EncloseName(table.Info.Name.O),
table.Info.AutoIncID)
}
if uniqueMap[UniqueName{table.DB.Name.String(), table.Info.Name.String()}] {
if ddlTables[UniqueTableName{table.DB.Name.String(), table.Info.Name.String()}] {
err = db.se.Execute(ctx, restoreMetaSQL)
if err != nil {
log.Error("restore meta sql failed",
Expand Down Expand Up @@ -232,12 +232,12 @@ func FilterDDLJobs(allDDLJobs []*model.Job, tables []*metautil.Table) (ddlJobs [
for _, table := range tables {
tableIDs := make(map[int64]bool)
tableIDs[table.Info.ID] = true
tableNames := make(map[UniqueName]bool)
name := UniqueName{table.DB.Name.String(), table.Info.Name.String()}
tableNames := make(map[UniqueTableName]bool)
name := UniqueTableName{table.DB.Name.String(), table.Info.Name.String()}
tableNames[name] = true
for _, job := range allDDLJobs {
if job.BinlogInfo.TableInfo != nil {
name = UniqueName{job.SchemaName, job.BinlogInfo.TableInfo.Name.String()}
name = UniqueTableName{job.SchemaName, job.BinlogInfo.TableInfo.Name.String()}
if tableIDs[job.TableID] || tableNames[name] {
ddlJobs = append(ddlJobs, job)
tableIDs[job.TableID] = true
Expand Down
4 changes: 2 additions & 2 deletions br/pkg/restore/db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (s *testRestoreSchemaSuite) TestRestoreAutoIncID(c *C) {
table.DB.Collate = "utf8mb4_bin"
err = db.CreateDatabase(context.Background(), table.DB)
c.Assert(err, IsNil, Commentf("Error create empty charset db: %s %s", err, s.mock.DSN))
uniqueMap := make(map[restore.UniqueName]bool)
uniqueMap := make(map[restore.UniqueTableName]bool)
err = db.CreateTable(context.Background(), &table, uniqueMap)
c.Assert(err, IsNil, Commentf("Error create table: %s %s", err, s.mock.DSN))

Expand All @@ -117,7 +117,7 @@ func (s *testRestoreSchemaSuite) TestRestoreAutoIncID(c *C) {

// try again, success because we use alter sql in unique map.
table.Info.AutoIncID = globalAutoID + 300
uniqueMap[restore.UniqueName{"test", "\"t\""}] = true
uniqueMap[restore.UniqueTableName{"test", "\"t\""}] = true
err = db.CreateTable(context.Background(), &table, uniqueMap)
// Check if AutoIncID is altered to globalAutoID + 300.
autoIncID, err = strconv.ParseUint(tk.MustQuery("admin show `\"t\"` next_row_id").Rows()[0][3].(string), 10, 64)
Expand Down

0 comments on commit 9aac1db

Please sign in to comment.