forked from wal-g/wal-g
/
timeline_check_runner.go
108 lines (92 loc) · 3.2 KB
/
timeline_check_runner.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
package postgres
import (
"bytes"
"fmt"
"io"
"github.com/T0n0T/wal-g/utility"
"github.com/wal-g/tracelog"
)
type TimelineCheckDetails struct {
CurrentTimelineID uint32 `json:"current_timeline_id"`
HighestStorageTimelineID uint32 `json:"highest_storage_timeline_id"`
}
func (details TimelineCheckDetails) NewPlainTextReader() (io.Reader, error) {
var outputBuffer bytes.Buffer
outputBuffer.WriteString(fmt.Sprintf("Highest timeline found in storage: %d\n",
details.HighestStorageTimelineID))
outputBuffer.WriteString(fmt.Sprintf("Current cluster timeline: %d\n",
details.CurrentTimelineID))
return &outputBuffer, nil
}
// TimelineCheckRunner is used to verify that the current timeline
// is the highest among the storage timelines
type TimelineCheckRunner struct {
currentTimeline uint32
walFolderFilenames []string
}
func (check TimelineCheckRunner) Name() string {
return "TimelineCheck"
}
func NewTimelineCheckRunner(walFolderFilenames []string,
currentSegment WalSegmentDescription) (TimelineCheckRunner, error) {
return TimelineCheckRunner{currentTimeline: currentSegment.Timeline, walFolderFilenames: walFolderFilenames}, nil
}
func (check TimelineCheckRunner) Run() (WalVerifyCheckResult, error) {
highestTimeline := tryFindHighestTimelineID(check.walFolderFilenames)
return newTimelineCheckResult(check.currentTimeline, highestTimeline), nil
}
func (check TimelineCheckRunner) Type() WalVerifyCheckType {
return WalVerifyTimelineCheck
}
// newTimelineCheckResult check produces the WalVerifyCheckResult with status:
// StatusOk if current timeline id matches the highest timeline id found in storage
// StatusWarning if could not determine if current timeline matches the highest in storage
// StatusFailure if current timeline is not equal to the highest timeline id found in storage
func newTimelineCheckResult(currentTimeline, highestTimeline uint32) WalVerifyCheckResult {
result := WalVerifyCheckResult{
Status: StatusWarning,
Details: TimelineCheckDetails{
CurrentTimelineID: currentTimeline,
HighestStorageTimelineID: highestTimeline,
},
}
if highestTimeline > 0 {
if currentTimeline == highestTimeline {
result.Status = StatusOk
} else {
result.Status = StatusFailure
}
}
return result
}
// TODO: Unit tests
func tryFindHighestTimelineID(filenames []string) (highestTimelineID uint32) {
for _, name := range filenames {
fileTimeline, ok := tryParseTimelineID(name)
if !ok {
tracelog.WarningLogger.Printf(
"Could not parse the timeline Id from %s. Skipping...",
name)
continue
}
if highestTimelineID < fileTimeline {
highestTimelineID = fileTimeline
}
}
return highestTimelineID
}
func tryParseTimelineID(fileName string) (timelineID uint32, success bool) {
// try to parse timeline id from WAL segment file
baseName := utility.TrimFileExtension(fileName)
fileTimeline, _, err := ParseWALFilename(baseName)
if err == nil {
return fileTimeline, true
}
// try to parse timeline id from .history file
matchResult := timelineHistoryFileRegexp.FindStringSubmatch(baseName)
if matchResult == nil || len(matchResult) < 2 {
return 0, false
}
fileTimeline, err = ParseTimelineFromString(matchResult[1])
return fileTimeline, err == nil
}