br: lock the call of update stats meta (#47610)

ref pingcap/tidb#47596
This commit is contained in:
Jianjun Liao
2023-10-16 13:09:28 +08:00
committed by GitHub
parent 19470d56f0
commit 476d9b3128
2 changed files with 11 additions and 4 deletions

View File

@ -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))
}
}
@ -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)
}

View File

@ -1758,6 +1758,9 @@ 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
@ -1765,6 +1768,8 @@ func (rc *Client) GoUpdateMetaAndLoadStats(ctx context.Context, inCh <-chan *Cre
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))
@ -1772,6 +1777,8 @@ func (rc *Client) GoUpdateMetaAndLoadStats(ctx context.Context, inCh <-chan *Cre
if err != nil {
log.Error("update stats meta failed", zap.Any("table", tbl.Table), zap.Error(err))
}
updateMetaLock.Unlock()
}
if oldTable.Stats != nil {