Skip to content

Commit

Permalink
backup: PITR did not work sometimes
Browse files Browse the repository at this point in the history
A bug in RestoreManager.FindNearestDump caused the bug.
Fixes #563.
  • Loading branch information
ymmt2005 committed Sep 6, 2023
1 parent 720adf8 commit 5b0ca60
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 7 deletions.
19 changes: 13 additions & 6 deletions backup/restore.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,6 @@ func (rm *RestoreManager) Restore(ctx context.Context) error {
if err != nil {
return fmt.Errorf("failed to list object keys: %w", err)
}
sort.Strings(keys)

dumpKey, binlogKey, backupTime := rm.FindNearestDump(keys)
if dumpKey == "" {
Expand Down Expand Up @@ -195,16 +194,19 @@ func (rm *RestoreManager) Restore(ctx context.Context) error {
return nil
}

// FindNearestDump finds the nearest dump file and binlog file to the restore point.
// `keys` are object keys for the restoring instance. They need not be sorted.
func (rm *RestoreManager) FindNearestDump(keys []string) (string, string, time.Time) {
sort.Strings(keys)

var nearest time.Time
var nearestDump, nearestBinlog string

for _, key := range keys {
if strings.HasSuffix(key, constants.BinlogFilename) {
nearestBinlog = key
continue
}
if !strings.HasSuffix(key, constants.DumpFilename) {
isBinlog := strings.HasSuffix(key, constants.BinlogFilename)
isDump := strings.HasSuffix(key, constants.DumpFilename)
if !isBinlog && !isDump {
rm.log.Info("skipping garbage", "key", key)
continue
}

Expand All @@ -217,6 +219,11 @@ func (rm *RestoreManager) FindNearestDump(keys []string) (string, string, time.T
break
}

if isBinlog {
nearestBinlog = key
continue
}

nearestDump = key
nearest = bkt
if path.Dir(nearestDump) != path.Dir(nearestBinlog) {
Expand Down
12 changes: 11 additions & 1 deletion backup/restore_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backup

import (
"sort"
"testing"
"time"

Expand All @@ -15,7 +16,12 @@ func TestFindNearestDump(t *testing.T) {
"moco/test/test/garbage",
"moco/test/test/20210526000000/dump.tar", // invalid
"moco/test/test/20210526-000000/dump.tar",
"moco/test/test/20210527-000000/dump.tar",
"moco/test/test/20210527-000000/binlog.tar.zst",
"moco/test/test/20210528-000000/dump.tar",
"moco/test/test/20210528-000000/binlog.tar.zst",
}
sort.Strings(keys)

testCases := []struct {
name string
Expand All @@ -25,7 +31,7 @@ func TestFindNearestDump(t *testing.T) {
expectBinlog string
expectTime time.Time
}{
{"latest", time.Date(2021, time.May, 26, 0, 0, 0, 0, time.UTC),
{"exact", time.Date(2021, time.May, 26, 0, 0, 0, 0, time.UTC),
"moco/test/test/20210526-000000/dump.tar", "", time.Date(2021, time.May, 26, 0, 0, 0, 0, time.UTC)},
{"up-to-date", time.Date(2021, time.May, 26, 1, 0, 0, 0, time.UTC),
"moco/test/test/20210526-000000/dump.tar", "", time.Date(2021, time.May, 26, 0, 0, 0, 0, time.UTC)},
Expand All @@ -35,6 +41,10 @@ func TestFindNearestDump(t *testing.T) {
"moco/test/test/20210525-112233/dump.tar", "moco/test/test/20210525-112233/binlog.tar.zst",
time.Date(2021, time.May, 25, 11, 22, 33, 0, time.UTC)},
{"not-found", time.Date(2021, time.May, 24, 0, 0, 0, 0, time.UTC), "", "", time.Time{}},
{"#563", time.Date(2021, time.May, 27, 12, 0, 0, 0, time.UTC),
"moco/test/test/20210527-000000/dump.tar", "moco/test/test/20210527-000000/binlog.tar.zst",
time.Date(2021, time.May, 27, 0, 0, 0, 0, time.UTC),
},
}

for _, tc := range testCases {
Expand Down

0 comments on commit 5b0ca60

Please sign in to comment.