From 8664d5991e328be9f33e3f879221548b6210dd32 Mon Sep 17 00:00:00 2001 From: Leavrth Date: Fri, 13 Oct 2023 14:20:11 +0800 Subject: [PATCH 1/2] lock the call of update stats meta Signed-off-by: Leavrth --- br/pkg/restore/client.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/br/pkg/restore/client.go b/br/pkg/restore/client.go index a2462c80b341c..12bf237a0108d 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 { From 0399cc26939e46ba6ad52b172e0beab8b5cc5da8 Mon Sep 17 00:00:00 2001 From: Leavrth Date: Fri, 13 Oct 2023 17:04:31 +0800 Subject: [PATCH 2/2] use snapshot to dump stats Signed-off-by: Leavrth --- br/pkg/backup/schema.go | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/br/pkg/backup/schema.go b/br/pkg/backup/schema.go index 8b6949cd484a1..9b0bd724d3cc0 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) }