From 476d9b3128cd090bf1dbbbbe83ddf8c0316a64b5 Mon Sep 17 00:00:00 2001 From: Jianjun Liao <36503113+Leavrth@users.noreply.github.com> Date: Mon, 16 Oct 2023 13:09:28 +0800 Subject: [PATCH] br: lock the call of update stats meta (#47610) ref pingcap/tidb#47596 --- br/pkg/backup/schema.go | 8 ++++---- br/pkg/restore/client.go | 7 +++++++ 2 files changed, 11 insertions(+), 4 deletions(-) diff --git a/br/pkg/backup/schema.go b/br/pkg/backup/schema.go index eb9f910cb0b2a..259b891dfce9a 100644 --- a/br/pkg/backup/schema.go +++ b/br/pkg/backup/schema.go @@ -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) } diff --git a/br/pkg/restore/client.go b/br/pkg/restore/client.go index c79b45673e42a..a8e8d6a5135ea 100644 --- a/br/pkg/restore/client.go +++ b/br/pkg/restore/client.go @@ -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 {