Skip to content

Commit

Permalink
br: pre allocate table id at first (#51737) (#52153)
Browse files Browse the repository at this point in the history
close #51736
  • Loading branch information
ti-chi-bot committed May 23, 2024
1 parent c149ae8 commit 001c1ff
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 6 deletions.
9 changes: 3 additions & 6 deletions br/pkg/restore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -377,7 +377,9 @@ func (rc *Client) InitCheckpointMetadataForLogRestore(ctx context.Context, taskN
return gcRatio, nil
}

func (rc *Client) allocTableIDs(ctx context.Context, tables []*metautil.Table) error {
// AllocTableIDs would pre-allocate the table's origin ID if exists, so that the TiKV doesn't need to rewrite the key in
// the download stage.
func (rc *Client) AllocTableIDs(ctx context.Context, tables []*metautil.Table) error {
rc.preallocedTableIDs = tidalloc.New(tables)
ctx = kv.WithInternalSourceType(ctx, kv.InternalTxnBR)
err := kv.RunInNewTxn(ctx, rc.GetDomain().Store(), true, func(_ context.Context, txn kv.Transaction) error {
Expand Down Expand Up @@ -955,11 +957,6 @@ func (rc *Client) GoCreateTables(
}
outCh := make(chan CreatedTable, len(tables))
rater := logutil.TraceRateOver(logutil.MetricTableCreatedCounter)
if err := rc.allocTableIDs(ctx, tables); err != nil {
errCh <- err
close(outCh)
return outCh
}

var err error

Expand Down
6 changes: 6 additions & 0 deletions br/pkg/task/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -882,6 +882,12 @@ func runRestore(c context.Context, g glue.Glue, cmdName string, cfg *RestoreConf
}
}

// preallocate the table id, because any ddl job or database creation also allocates the global ID
err = client.AllocTableIDs(ctx, tables)
if err != nil {
return errors.Trace(err)
}

// execute DDL first
err = client.ExecDDLs(ctx, ddlJobs)
if err != nil {
Expand Down

0 comments on commit 001c1ff

Please sign in to comment.