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

br: lock the call of update stats meta #47610

Merged
merged 2 commits into from
Oct 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions br/pkg/backup/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func (ss *Schemas) BackupSchemas(
}
}
if statsHandle != nil {
if err := schema.dumpStatsToJSON(statsHandle); err != nil {
if err := schema.dumpStatsToJSON(statsHandle, backupTS); err != nil {
logger.Error("dump table stats failed", logutil.ShortError(err))
}
}
Expand Down Expand Up @@ -209,9 +209,9 @@ func (s *schemaInfo) calculateChecksum(
return nil
}

func (s *schemaInfo) dumpStatsToJSON(statsHandle *handle.Handle) error {
jsonTable, err := statsHandle.DumpStatsToJSON(
s.dbInfo.Name.String(), s.tableInfo, nil, true)
func (s *schemaInfo) dumpStatsToJSON(statsHandle *handle.Handle, backupTS uint64) error {
jsonTable, err := statsHandle.DumpStatsToJSONBySnapshot(
s.dbInfo.Name.String(), s.tableInfo, backupTS, true)
if err != nil {
return errors.Trace(err)
}
Expand Down
7 changes: 7 additions & 0 deletions br/pkg/restore/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -1758,20 +1758,27 @@ func (rc *Client) GoUpdateMetaAndLoadStats(ctx context.Context, inCh <-chan *Cre
log.Info("Start to update meta then load stats")
outCh := DefaultOutputTableChan()
workers := utils.NewWorkerPool(16, "UpdateStats")
// The rc.db is not thread safe
var updateMetaLock sync.Mutex

go concurrentHandleTablesCh(ctx, inCh, outCh, errCh, workers, func(c context.Context, tbl *CreatedTable) error {
oldTable := tbl.OldTable
// Not need to return err when failed because of update analysis-meta
restoreTS, err := rc.GetTSWithRetry(ctx)
if err != nil {
log.Error("getTS failed", zap.Error(err))
} else {
updateMetaLock.Lock()

log.Info("start update metas",
zap.Stringer("table", oldTable.Info.Name),
zap.Stringer("db", oldTable.DB.Name))
err = rc.db.UpdateStatsMeta(ctx, tbl.Table.ID, restoreTS, oldTable.TotalKvs)
if err != nil {
log.Error("update stats meta failed", zap.Any("table", tbl.Table), zap.Error(err))
}

updateMetaLock.Unlock()
}

if oldTable.Stats != nil {
Expand Down
Loading