/
wal_show_handler.go
211 lines (183 loc) · 7.55 KB
/
wal_show_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
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
package postgres
import (
"sort"
"github.com/wal-g/wal-g/internal"
"github.com/johnalotoski/storages/storage"
"github.com/wal-g/tracelog"
"github.com/wal-g/wal-g/utility"
)
const (
TimelineOkStatus = "OK"
TimelineLostSegmentStatus = "LOST_SEGMENTS"
)
// TimelineInfo contains information about some timeline in storage
type TimelineInfo struct {
ID uint32 `json:"id"`
ParentID uint32 `json:"parent_id"`
SwitchPointLsn uint64 `json:"switch_point_lsn"`
StartSegment string `json:"start_segment"`
EndSegment string `json:"end_segment"`
SegmentsCount int `json:"segments_count"`
MissingSegments []string `json:"missing_segments"`
Backups []*BackupDetail `json:"backups,omitempty"`
SegmentRangeSize uint64 `json:"segment_range_size"`
Status string `json:"status"`
}
func NewTimelineInfo(walSegments *WalSegmentsSequence, historyRecords []*TimelineHistoryRecord) (*TimelineInfo, error) {
timelineInfo := &TimelineInfo{
ID: walSegments.timelineID,
StartSegment: walSegments.minSegmentNo.getFilename(walSegments.timelineID),
EndSegment: walSegments.maxSegmentNo.getFilename(walSegments.timelineID),
SegmentsCount: len(walSegments.walSegmentNumbers),
SegmentRangeSize: uint64(walSegments.maxSegmentNo-walSegments.minSegmentNo) + 1,
Status: TimelineOkStatus,
}
missingSegments, err := walSegments.FindMissingSegments()
if err != nil {
return nil, err
}
timelineInfo.MissingSegments = make([]string, 0, len(missingSegments))
for _, segment := range missingSegments {
timelineInfo.MissingSegments = append(timelineInfo.MissingSegments, segment.GetFileName())
}
if len(timelineInfo.MissingSegments) > 0 {
timelineInfo.Status = TimelineLostSegmentStatus
}
// set parent timeline id and timeline switch LSN if have .history record available
if len(historyRecords) > 0 {
switchHistoryRecord := historyRecords[len(historyRecords)-1]
timelineInfo.ParentID = switchHistoryRecord.timeline
timelineInfo.SwitchPointLsn = switchHistoryRecord.lsn
}
return timelineInfo, nil
}
// WalSegmentsSequence represents some collection of wal segments with the same timeline
type WalSegmentsSequence struct {
timelineID uint32
walSegmentNumbers map[WalSegmentNo]bool
minSegmentNo WalSegmentNo
maxSegmentNo WalSegmentNo
}
func NewSegmentsSequence(id uint32, segmentNo WalSegmentNo) *WalSegmentsSequence {
walSegmentNumbers := make(map[WalSegmentNo]bool)
walSegmentNumbers[segmentNo] = true
return &WalSegmentsSequence{
timelineID: id,
walSegmentNumbers: walSegmentNumbers,
minSegmentNo: segmentNo,
maxSegmentNo: segmentNo,
}
}
// AddWalSegmentNo adds the provided segment number to collection
func (seq *WalSegmentsSequence) AddWalSegmentNo(number WalSegmentNo) {
seq.walSegmentNumbers[number] = true
if seq.minSegmentNo > number {
seq.minSegmentNo = number
}
if seq.maxSegmentNo < number {
seq.maxSegmentNo = number
}
}
// FindMissingSegments finds missing segments in range [minSegmentNo, maxSegmentNo]
func (seq *WalSegmentsSequence) FindMissingSegments() ([]WalSegmentDescription, error) {
startWalSegment := WalSegmentDescription{Number: seq.maxSegmentNo, Timeline: seq.timelineID}
walSegments := make(map[WalSegmentDescription]bool, len(seq.walSegmentNumbers))
for number := range seq.walSegmentNumbers {
segment := WalSegmentDescription{Number: number, Timeline: seq.timelineID}
walSegments[segment] = true
}
// create WAL segment runner to run on single timeline
walSegmentRunner := NewWalSegmentRunner(startWalSegment, walSegments, seq.minSegmentNo, nil)
walSegmentScanner := NewWalSegmentScanner(walSegmentRunner)
err := walSegmentScanner.Scan(SegmentScanConfig{
UnlimitedScan: true,
MissingSegmentStatus: Lost,
})
if err != nil {
return nil, err
}
return walSegmentScanner.GetMissingSegmentsDescriptions(), nil
}
// HandleWalShow gets the list of files inside WAL folder, detects the available WAL segments,
// groups WAL segments by the timeline and shows detailed info about each timeline stored in storage
func HandleWalShow(rootFolder storage.Folder, showBackups bool, outputWriter WalShowOutputWriter) {
walFolder := rootFolder.GetSubFolder(utility.WalPath)
filenames, err := getFolderFilenames(walFolder)
tracelog.ErrorLogger.FatalfOnError("Failed to get the WAL folder filenames %v\n", err)
walSegments := getSegmentsFromFiles(filenames)
segmentsByTimelines := groupSegmentsByTimelines(walSegments)
timelineInfos := make([]*TimelineInfo, 0, len(segmentsByTimelines))
for _, segmentsSequence := range segmentsByTimelines {
historyRecords, err := getTimeLineHistoryRecords(segmentsSequence.timelineID, walFolder)
if err != nil {
if _, ok := err.(HistoryFileNotFoundError); !ok {
tracelog.ErrorLogger.Fatalf("Error while loading .history file %v\n", err)
}
}
info, err := NewTimelineInfo(segmentsSequence, historyRecords)
tracelog.ErrorLogger.FatalfOnError("Error while creating TimeLineInfo %v\n", err)
timelineInfos = append(timelineInfos, info)
}
if showBackups {
timelineInfos, err = addBackupsInfo(timelineInfos, rootFolder)
tracelog.ErrorLogger.FatalfOnError("Failed to add backups info: %v\n", err)
}
// order timelines by ID
sort.Slice(timelineInfos, func(i, j int) bool {
return timelineInfos[i].ID < timelineInfos[j].ID
})
err = outputWriter.Write(timelineInfos)
tracelog.ErrorLogger.FatalfOnError("Error writing output: %v\n", err)
}
func groupSegmentsByTimelines(segments map[WalSegmentDescription]bool) map[uint32]*WalSegmentsSequence {
segmentsByTimelines := make(map[uint32]*WalSegmentsSequence)
for segment := range segments {
if timelineInfo, ok := segmentsByTimelines[segment.Timeline]; ok {
timelineInfo.AddWalSegmentNo(segment.Number)
continue
}
segmentsByTimelines[segment.Timeline] = NewSegmentsSequence(segment.Timeline, segment.Number)
}
return segmentsByTimelines
}
// addBackupsInfo adds info about available backups for each timeline
func addBackupsInfo(timelineInfos []*TimelineInfo, rootFolder storage.Folder) ([]*TimelineInfo, error) {
backups, err := internal.GetBackups(rootFolder.GetSubFolder(utility.BaseBackupPath))
if err != nil {
if _, ok := err.(internal.NoBackupsFoundError); ok {
tracelog.InfoLogger.Println("No backups found in storage.")
return timelineInfos, nil
}
return nil, err
}
backupDetails, err := GetBackupsDetails(rootFolder.GetSubFolder(utility.BaseBackupPath), backups)
if err != nil {
return nil, err
}
for _, info := range timelineInfos {
info.Backups, err = getBackupsInRange(info.StartSegment, info.EndSegment, info.ID, backupDetails)
if err != nil {
return nil, err
}
}
return timelineInfos, nil
}
// getBackupsInRange returns backups taken in range [startSegmentName, endSegmentName]
func getBackupsInRange(startSegmentName, endSegmentName string, timeline uint32,
backups []BackupDetail) ([]*BackupDetail, error) {
filteredBackups := make([]*BackupDetail, 0)
for idx := range backups {
backup := &backups[idx]
backupTimeline, _, err := ParseWALFilename(backup.WalFileName)
if err != nil {
return nil, err
}
startSegment := newWalSegmentNo(backup.StartLsn).getFilename(backupTimeline)
endSegment := newWalSegmentNo(backup.FinishLsn).getFilename(backupTimeline)
// there we compare segments lexicographically, maybe it is wrong...
if timeline == backupTimeline && startSegment >= startSegmentName && endSegment <= endSegmentName {
filteredBackups = append(filteredBackups, backup)
}
}
return filteredBackups, nil
}