Skip to content
This repository has been archived by the owner on Jul 24, 2024. It is now read-only.

br: add lock file #358

Merged
merged 4 commits into from
Jun 17, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions pkg/backup/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,13 @@ func (bc *Client) GetTS(ctx context.Context, duration time.Duration, ts uint64)
return backupTS, nil
}

// SetLockFile set write lock file.
func (bc *Client) SetLockFile(ctx context.Context) error {
return bc.storage.Write(ctx, utils.LockFile,
[]byte("DO NOT DELETE\n"+
"This file exists to remind other backup jobs won't use this path"))
}

// SetGCTTL set gcTTL for client.
func (bc *Client) SetGCTTL(ttl int64) {
bc.gcTTL = ttl
Expand All @@ -147,6 +154,13 @@ func (bc *Client) SetStorage(ctx context.Context, backend *kvproto.StorageBacken
if exist {
return errors.New("backup meta exists, may be some backup files in the path already")
}
exist, err = bc.storage.FileExists(ctx, utils.LockFile)
if err != nil {
return errors.Annotatef(err, "error occurred when checking %s file", utils.LockFile)
}
if exist {
return errors.New("backup lock exists, may be some backup files in the path already")
}
bc.backend = backend
return nil
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/task/backup.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,10 @@ func RunBackup(c context.Context, g glue.Glue, cmdName string, cfg *BackupConfig
if err = client.SetStorage(ctx, u, cfg.SendCreds); err != nil {
return err
}
err = client.SetLockFile(ctx)
if err != nil {
return err
}
client.SetGCTTL(cfg.GCTTL)

backupTS, err := client.GetTS(ctx, cfg.TimeAgo, cfg.BackupTS)
Expand Down
2 changes: 2 additions & 0 deletions pkg/utils/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import (
)

const (
// LockFile represents file name,
LockFile = "backup.lock"
// MetaFile represents file name
MetaFile = "backupmeta"
// MetaJSONFile represents backup meta json file name
Expand Down
32 changes: 29 additions & 3 deletions tests/br_other/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ run_sql "CREATE DATABASE $DB;"

run_sql "CREATE TABLE $DB.usertable1 ( \
YCSB_KEY varchar(64) NOT NULL, \
FIELD0 varchar(1) DEFAULT NULL, \
FIELD0 varchar(10) DEFAULT NULL, \
PRIMARY KEY (YCSB_KEY) \
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_bin;"

run_sql "INSERT INTO $DB.usertable1 VALUES (\"a\", \"b\");"
run_sql "INSERT INTO $DB.usertable1 VALUES (\"aa\", \"b\");"
for i in `seq 1 100`
do
run_sql "INSERT INTO $DB.usertable1 VALUES (\"a$i\", \"bbbbbbbbbb\");"
done

# backup full
echo "backup start..."
Expand All @@ -52,6 +54,30 @@ if [ "$corrupted" -ne "1" ];then
exit 1
fi

# backup full with ratelimit = 1 to make sure this backup task won't finish quickly
echo "backup start to test lock file"
run_br --pd $PD_ADDR backup full -s "local://$TEST_DIR/$DB/lock" --ratelimit 1 --ratelimit-unit 1 --concurrency 4 > /dev/null 2>&1 &
# record last backup pid
_pid=$!

backup_fail=0
echo "another backup start expect to fail due to last backup add a lockfile"
run_br --pd $PD_ADDR backup full -s "local://$TEST_DIR/$DB/lock" --concurrency 4 || backup_fail=1
if [ "$backup_fail" -ne "1" ];then
echo "TEST: [$TEST_NAME] test backup lock file failed!"
exit 1
fi

if ps -p $_pid > /dev/null
then
echo "$_pid is running"
# kill last backup progress
kill -9 $_pid
else
echo "TEST: [$TEST_NAME] test backup lock file failed! the last backup finished"
exit 1
fi

run_sql "DROP DATABASE $DB;"

# Test version
Expand Down