From 18d124a410563c80bac0dd87bc7aa87968dc756a Mon Sep 17 00:00:00 2001 From: 3pointer Date: Mon, 29 Nov 2021 15:58:26 +0800 Subject: [PATCH 1/2] br: add more precise check for lock file --- br/pkg/backup/client.go | 32 +++++++++++++++++++++++++------- br/pkg/backup/client_test.go | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+), 7 deletions(-) diff --git a/br/pkg/backup/client.go b/br/pkg/backup/client.go index 79edc3403be68..fa6f660358a63 100644 --- a/br/pkg/backup/client.go +++ b/br/pkg/backup/client.go @@ -180,14 +180,9 @@ func (bc *Client) SetStorage(ctx context.Context, backend *backuppb.StorageBacke "there may be some backup files in the path already, "+ "please specify a correct backup directory!", bc.storage.URI()+"/"+metautil.MetaFile) } - exist, err = bc.storage.FileExists(ctx, metautil.LockFile) + err = CheckBackupStorageIsLocked(ctx, bc.storage) if err != nil { - return errors.Annotatef(err, "error occurred when checking %s file", metautil.LockFile) - } - if exist { - return errors.Annotatef(berrors.ErrInvalidArgument, "backup lock file exists in %v, "+ - "there may be some backup files in the path already, "+ - "please specify a correct backup directory!", bc.storage.URI()+"/"+metautil.LockFile) + return err } bc.backend = backend return nil @@ -198,6 +193,29 @@ func (bc *Client) GetClusterID() uint64 { return bc.clusterID } +// CheckBackupStorageIsLocked checks whether backups is locked. +// which means we found other backup progress already write +// some data files into the same backup directory or cloud prefix. +func CheckBackupStorageIsLocked(ctx context.Context, s storage.ExternalStorage) error { + exist, err := s.FileExists(ctx, metautil.LockFile) + if err != nil { + return errors.Annotatef(err, "error occurred when checking %s file", metautil.LockFile) + } + if exist { + err = s.WalkDir(ctx, &storage.WalkOption{}, func(path string, size int64) error { + // should return error to break the walkDir when found lock file and other .sst files. + if strings.HasSuffix(path, ".sst") { + return errors.Annotatef(berrors.ErrInvalidArgument, "backup lock file and sst file exist in %v, "+ + "there are some backup files in the path already, "+ + "please specify a correct backup directory!", s.URI()+"/"+metautil.LockFile) + } + return nil + }) + return err + } + return nil +} + // BuildTableRanges returns the key ranges encompassing the entire table, // and its partitions if exists. func BuildTableRanges(tbl *model.TableInfo) ([]kv.KeyRange, error) { diff --git a/br/pkg/backup/client_test.go b/br/pkg/backup/client_test.go index 3c3688f79bc9f..e46d832bae3ee 100644 --- a/br/pkg/backup/client_test.go +++ b/br/pkg/backup/client_test.go @@ -70,6 +70,13 @@ func (r *testBackup) SetUpSuite(c *C) { } +func (r *testBackup) resetStorage(c *C) { + var err error + base := c.MkDir() + r.storage, err = storage.NewLocalStorage(base) + c.Assert(err, IsNil) +} + func (r *testBackup) TestGetTS(c *C) { var ( err error @@ -335,3 +342,30 @@ func (r *testBackup) TestskipUnsupportedDDLJob(c *C) { c.Assert(err, IsNil) c.Assert(len(allDDLJobs), Equals, 8) } + +func (r *testBackup) TestCheckBackupIsLocked(c *C) { + ctx := context.Background() + + r.resetStorage(c) + // check passed with an empty storage + err := backup.CheckBackupStorageIsLocked(ctx, r.storage) + c.Assert(err, IsNil) + + // check passed with only a lock file + err = r.storage.WriteFile(ctx, metautil.LockFile, nil) + c.Assert(err, IsNil) + err = backup.CheckBackupStorageIsLocked(ctx, r.storage) + c.Assert(err, IsNil) + + // check passed with a lock file and other non-sst files. + err = r.storage.WriteFile(ctx, "1.txt", nil) + c.Assert(err, IsNil) + err = backup.CheckBackupStorageIsLocked(ctx, r.storage) + c.Assert(err, IsNil) + + // check failed + err = r.storage.WriteFile(ctx, "1.sst", nil) + c.Assert(err, IsNil) + err = backup.CheckBackupStorageIsLocked(ctx, r.storage) + c.Assert(err, ErrorMatches, "backup lock file and sst file exist in(.+)") +} From 0000cd32f526ee4e6aeea05c9ecc808e0725d046 Mon Sep 17 00:00:00 2001 From: 3pointer Date: Mon, 29 Nov 2021 17:34:49 +0800 Subject: [PATCH 2/2] fmt --- br/pkg/backup/client.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/br/pkg/backup/client.go b/br/pkg/backup/client.go index fa6f660358a63..0c8183f7e0e47 100644 --- a/br/pkg/backup/client.go +++ b/br/pkg/backup/client.go @@ -202,7 +202,7 @@ func CheckBackupStorageIsLocked(ctx context.Context, s storage.ExternalStorage) return errors.Annotatef(err, "error occurred when checking %s file", metautil.LockFile) } if exist { - err = s.WalkDir(ctx, &storage.WalkOption{}, func(path string, size int64) error { + err = s.WalkDir(ctx, &storage.WalkOption{}, func(path string, size int64) error { // should return error to break the walkDir when found lock file and other .sst files. if strings.HasSuffix(path, ".sst") { return errors.Annotatef(berrors.ErrInvalidArgument, "backup lock file and sst file exist in %v, "+