From c3fa89acd333790d361468a45e140fa197cf5b02 Mon Sep 17 00:00:00 2001 From: Artur Melanchyk Date: Mon, 25 Mar 2024 05:51:46 +0100 Subject: [PATCH] br: use atomic for failedFilesCount (#52046) --- br/pkg/checkpoint/checkpoint.go | 16 ++++------------ 1 file changed, 4 insertions(+), 12 deletions(-) diff --git a/br/pkg/checkpoint/checkpoint.go b/br/pkg/checkpoint/checkpoint.go index a7707afda2e04..c4a0092c51cbd 100644 --- a/br/pkg/checkpoint/checkpoint.go +++ b/br/pkg/checkpoint/checkpoint.go @@ -24,6 +24,7 @@ import ( "math/rand" "strings" "sync" + "sync/atomic" "time" "github.com/pingcap/errors" @@ -846,13 +847,8 @@ func removeCheckpointData(ctx context.Context, s storage.ExternalStorage, subDir zap.Int64("remove-size", removeSize), ) - maxFailedFilesNum := 16 - failedFilesCount := struct { - lock sync.Mutex - count int - }{ - count: 0, - } + maxFailedFilesNum := int64(16) + var failedFilesCount atomic.Int64 pool := utils.NewWorkerPool(4, "checkpoint remove worker") eg, gCtx := errgroup.WithContext(ctx) for _, filename := range removedFileNames { @@ -860,13 +856,9 @@ func removeCheckpointData(ctx context.Context, s storage.ExternalStorage, subDir pool.ApplyOnErrorGroup(eg, func() error { if err := s.DeleteFile(gCtx, name); err != nil { log.Warn("failed to remove the file", zap.String("filename", name), zap.Error(err)) - failedFilesCount.lock.Lock() - failedFilesCount.count += 1 - if failedFilesCount.count >= maxFailedFilesNum { - failedFilesCount.lock.Unlock() + if failedFilesCount.Add(1) >= maxFailedFilesNum { return errors.Annotate(err, "failed to delete too many files") } - failedFilesCount.lock.Unlock() } return nil })