/
stream_fetch_handler.go
114 lines (92 loc) · 2.85 KB
/
stream_fetch_handler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
package mysql
import (
"github.com/tinsane/storages/storage"
"github.com/wal-g/wal-g/internal"
"github.com/wal-g/wal-g/utility"
"os"
"path"
"path/filepath"
"strings"
"time"
)
type BinlogFetchSettings struct{}
func (settings BinlogFetchSettings) GetEndTsEnv() string {
return BinlogEndTsSetting
}
func (settings BinlogFetchSettings) GetDstEnv() string {
return BinlogDstSetting
}
func (settings BinlogFetchSettings) GetLogFolderPath() string {
return BinlogPath
}
type BinlogFetchHandlers struct {
dstPathFolder string
endTS *time.Time
}
func (handlers BinlogFetchHandlers) HandleAbortFetch(logFilePath string) error {
return os.Remove(logFilePath)
}
func (handlers BinlogFetchHandlers) DownloadLogTo(logFolder storage.Folder, logName string, dstLogFilePath string) error {
return internal.DownloadWALFileTo(logFolder, logName, dstLogFilePath)
}
func (handlers BinlogFetchHandlers) GetLogFilePath(pathToLog string) (string, error) {
return path.Join(handlers.dstPathFolder, pathToLog), nil
}
func (handlers BinlogFetchHandlers) ShouldBeAborted(pathToLog string) (bool, error) {
timestamp, err := parseFromBinlog(pathToLog)
if err != nil {
return false, err
}
return binlogIsTooOld(timestamp, handlers.endTS), nil
}
func FetchLogs(folder storage.Folder, backup *internal.Backup) error {
settings := BinlogFetchSettings{}
endTS, dstFolder, err := internal.GetOperationLogsSettings(settings)
if err != nil {
return err
}
backupUploadTime, err := getBackupUploadTime(folder, backup)
if err != nil {
return err
}
handlers := BinlogFetchHandlers{dstPathFolder: dstFolder, endTS: endTS}
fetchedBinlogs, err := internal.FetchLogs(folder, backupUploadTime, nil, settings, handlers)
if err != nil {
return err
}
return createIndexFile(dstFolder, fetchedBinlogs)
}
func getBackupUploadTime(folder storage.Folder, backup *internal.Backup) (time.Time, error) {
var streamSentinel StreamSentinelDto
err := internal.FetchStreamSentinel(backup, &streamSentinel)
if err != nil {
return time.Time{}, err
}
binlogs, _, err := folder.GetSubFolder(BinlogPath).ListFolder()
if err != nil {
return time.Time{}, err
}
var backupUploadTime time.Time
for _, binlog := range binlogs {
if strings.HasPrefix(binlog.GetName(), streamSentinel.BinLogStart) {
backupUploadTime = binlog.GetLastModified()
}
}
return backupUploadTime, nil
}
func binlogIsTooOld(binlogTimestamp time.Time, endTS *time.Time) bool {
return endTS != nil && binlogTimestamp.After(*endTS)
}
func createIndexFile(dstFolder string, fetchedBinlogs []storage.Object) error {
indexFile, err := os.Create(filepath.Join(dstFolder, "binlogs_order"))
if err != nil {
return err
}
for _, binlogObject := range fetchedBinlogs {
_, err = indexFile.WriteString(utility.TrimFileExtension(binlogObject.GetName()) + "\n")
if err != nil {
return err
}
}
return indexFile.Close()
}