Skip to content

Commit

Permalink
br: lock the call of update stats meta (#47610)
Browse files Browse the repository at this point in the history
ref #47596
  • Loading branch information
Leavrth authored Oct 16, 2023
1 parent 19470d5 commit 476d9b3
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
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

0 comments on commit 476d9b3

Please sign in to comment.