Skip to content

Database governance built for humans and agents — controlling changes and access across every major database.

refactor(PITR): improve local binlog file operations (#1527)

Yilong Lif3820d9b0dd8
diff --git a/plugin/restore/mysql/mysql.go b/plugin/restore/mysql/mysql.go
index 2bac2070fb..be4b2169e3 100644
--- a/plugin/restore/mysql/mysql.go
+++ b/plugin/restore/mysql/mysql.go
@@ -230,10 +230,7 @@ func getBinlogReplayList(startBinlogInfo api.BinlogInfo, binlogDir string) ([]st
return nil, fmt.Errorf("no binlog files found locally after given start binlog info: %v", startBinlogInfo)
}
- binlogFilesToReplaySorted, err := parseAndSortBinlogFiles(binlogFilesToReplay)
- if err != nil {
- return nil, err
- }
+ binlogFilesToReplaySorted := sortBinlogFiles(binlogFilesToReplay)
if binlogFilesToReplaySorted[0].Seq != startBinlogSeq {
log.Error("The starting binlog file does not exist locally", zap.String("filename", startBinlogInfo.FileName))
@@ -252,23 +249,16 @@ func getBinlogReplayList(startBinlogInfo api.BinlogInfo, binlogDir string) ([]st
return binlogReplayList, nil
}
-// parseAndSortBinlogFiles will parse the binlog file name, and then sort them in ascending order by their numeric extension.
+// sortBinlogFiles will sort binlog files in ascending order by their numeric extension.
// For mysql binlog, after the serial number reaches 999999, the next serial number will not return to 000000, but 1000000,
// so we cannot directly use string to compare lexicographical order.
-func parseAndSortBinlogFiles(binlogFiles []BinlogFile) ([]BinlogFile, error) {
- var ret []BinlogFile
- for _, binlogFile := range binlogFiles {
- seq, err := getBinlogNameSeq(binlogFile.Name)
- if err != nil {
- return nil, fmt.Errorf("failed to parse the binlog file name[%s], error[%w]", binlogFile.Name, err)
- }
- binlogFile.Seq = seq
- ret = append(ret, binlogFile)
- }
- sort.Slice(ret, func(i, j int) bool {
- return ret[i].Seq < ret[j].Seq
+func sortBinlogFiles(binlogFiles []BinlogFile) []BinlogFile {
+ var sorted []BinlogFile
+ sorted = append(sorted, binlogFiles...)
+ sort.Slice(sorted, func(i, j int) bool {
+ return sorted[i].Seq < sorted[j].Seq
})
- return ret, nil
+ return sorted
}
// Locate the binlog event at (filename, position), parse the event and return its timestamp.
@@ -496,6 +486,22 @@ func getPITROldDatabaseName(database string, suffixTs int64) string {
return getSafeName(database, suffix)
}
+func getSortedLocalBinlogFiles(binlogDir string) ([]BinlogFile, error) {
+ binlogFilesInfoLocal, err := ioutil.ReadDir(binlogDir)
+ if err != nil {
+ return nil, err
+ }
+ var binlogFilesLocal []BinlogFile
+ for _, fileInfo := range binlogFilesInfoLocal {
+ binlogFile, err := newBinlogFile(fileInfo.Name(), fileInfo.Size())
+ if err != nil {
+ return nil, err
+ }
+ binlogFilesLocal = append(binlogFilesLocal, binlogFile)
+ }
+ return sortBinlogFiles(binlogFilesLocal), nil
+}
+
func binlogFilesAreContinuous(files []BinlogFile) bool {
for i := 0; i < len(files)-1; i++ {
if files[i].Seq+1 != files[i+1].Seq {
@@ -508,24 +514,28 @@ func binlogFilesAreContinuous(files []BinlogFile) bool {
// FetchArchivedBinlogFiles downloads the missing binlog files from the remote instance to `binlogDir`,
// but exclude latest binlog. We may download the latest binlog only when doing PITR.
func (r *Restore) FetchArchivedBinlogFiles(ctx context.Context) error {
- binlogFilesLocal, err := ioutil.ReadDir(r.binlogDir)
- if err != nil {
- return err
- }
-
+ // Read binlog files list on server.
binlogFilesOnServerSorted, err := r.GetSortedBinlogFilesMetaOnServer(ctx)
if err != nil {
return err
}
-
+ if len(binlogFilesOnServerSorted) == 0 {
+ // No binlog files on server, so there's nothing to download.
+ log.Debug("No binlog file found on server")
+ return nil
+ }
latestBinlogFileOnServer := binlogFilesOnServerSorted[len(binlogFilesOnServerSorted)-1]
log.Debug("Got sorted binlog file list on server", zap.Array("list", ZapBinlogFiles(binlogFilesOnServerSorted)))
+ // Read the local binlog files.
+ binlogFilesLocalSorted, err := getSortedLocalBinlogFiles(r.binlogDir)
+ if err != nil {
+ return fmt.Errorf("failed to read local binlog files, error[%w]", err)
+ }
// build a local file size map from file name to size
localFileMap := make(map[string]int64)
-
- for _, localFile := range binlogFilesLocal {
- localFileMap[localFile.Name()] = localFile.Size()
+ for _, localFile := range binlogFilesLocalSorted {
+ localFileMap[localFile.Name] = localFile.Size
}
todo := make(map[string]bool)
@@ -663,19 +673,20 @@ func (r *Restore) GetSortedBinlogFilesMetaOnServer(ctx context.Context) ([]Binlo
var binlogFiles []BinlogFile
for rows.Next() {
- var binlogFile BinlogFile
+ var name string
+ var size int64
var unused interface{}
- if err := rows.Scan(&binlogFile.Name, &binlogFile.Size, &unused /*Encrypted column*/); err != nil {
+ if err := rows.Scan(&name, &size, &unused /*Encrypted column*/); err != nil {
+ return nil, err
+ }
+ binlogFile, err := newBinlogFile(name, size)
+ if err != nil {
return nil, err
}
binlogFiles = append(binlogFiles, binlogFile)
}
- binlogFiles, err = parseAndSortBinlogFiles(binlogFiles)
- if err != nil {
- return nil, err
- }
- return binlogFiles, nil
+ return sortBinlogFiles(binlogFiles), nil
}
// showLatestBinlogFile returns the metadata of latest binlog
diff --git a/plugin/restore/mysql/mysql_test.go b/plugin/restore/mysql/mysql_test.go
index 7cb77ace6b..f0ba0b5989 100644
--- a/plugin/restore/mysql/mysql_test.go
+++ b/plugin/restore/mysql/mysql_test.go
@@ -142,8 +142,13 @@ func TestGetBinlogFileNameSeqNumber(t *testing.T) {
err: false,
},
{
- name: "binlog.178923",
- expect: 178923,
+ name: "binlog.999999",
+ expect: 999999,
+ err: false,
+ },
+ {
+ name: "binlog.1000000",
+ expect: 1000000,
err: false,
},
{
@@ -394,50 +399,29 @@ func TestGetReplayBinlogPathList(t *testing.T) {
}
}
-func TestParseAndSortBinlogFiles(t *testing.T) {
+func TestSortBinlogFiles(t *testing.T) {
a := require.New(t)
tests := []struct {
binlogFileNames []BinlogFile
expect []BinlogFile
- err bool
}{
// Normal
{
- binlogFileNames: []BinlogFile{{Name: "binlog.000001"}, {Name: "binlog.000002"}, {Name: "binlog.000003"}},
- expect: []BinlogFile{{Name: "binlog.000001", Seq: 1}, {Name: "binlog.000002", Seq: 2}, {Name: "binlog.000003", Seq: 3}},
- err: false,
- },
- // parse error
- {
- binlogFileNames: []BinlogFile{{Name: "binlog.000001"}, {Name: "binlog000002"}},
- expect: []BinlogFile{},
- err: true,
+ binlogFileNames: []BinlogFile{{Seq: 1}, {Seq: 2}, {Seq: 3}},
+ expect: []BinlogFile{{Seq: 1}, {Seq: 2}, {Seq: 3}},
},
// gap
{
- binlogFileNames: []BinlogFile{{Name: "binlog.000001"}, {Name: "binlog.000007"}, {Name: "binlog.000004"}},
- expect: []BinlogFile{{Name: "binlog.000001", Seq: 1}, {Name: "binlog.000004", Seq: 4}, {Name: "binlog.000007", Seq: 7}},
- err: false,
- },
- // 999999
- {
- binlogFileNames: []BinlogFile{{Name: "binlog.999999"}, {Name: "binlog.1000000"}},
- expect: []BinlogFile{{Name: "binlog.999999", Seq: 999999}, {Name: "binlog.1000000", Seq: 1000000}},
- err: false,
+ binlogFileNames: []BinlogFile{{Seq: 1}, {Seq: 7}, {Seq: 4}},
+ expect: []BinlogFile{{Seq: 1}, {Seq: 4}, {Seq: 7}},
},
}
for _, test := range tests {
- items, err := parseAndSortBinlogFiles(test.binlogFileNames)
-
- if test.err {
- a.Error(err)
- } else {
- a.EqualValues(len(items), len(test.expect))
- for idx := range items {
- a.EqualValues(items[idx].Name, test.expect[idx].Name)
- a.EqualValues(items[idx].Seq, test.expect[idx].Seq)
- }
+ sorted := sortBinlogFiles(test.binlogFileNames)
+ a.Equal(len(test.expect), len(sorted))
+ for i := range sorted {
+ a.Equal(sorted[i].Seq, test.expect[i].Seq)
}
}
}