-
Notifications
You must be signed in to change notification settings - Fork 471
/
reader.go
316 lines (287 loc) · 10.5 KB
/
reader.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
// Copyright 2024 The LevelDB-Go and Pebble Authors. All rights reserved. Use
// of this source code is governed by a BSD-style license that can be found in
// the LICENSE file.
package wal
import (
"bytes"
"cmp"
"fmt"
"io"
"slices"
"strings"
"github.com/cockroachdb/errors"
"github.com/cockroachdb/pebble/batchrepr"
"github.com/cockroachdb/pebble/internal/base"
"github.com/cockroachdb/pebble/record"
"github.com/cockroachdb/pebble/vfs"
)
// A LogicalLog identifies a logical WAL and its consituent segment files.
type LogicalLog struct {
Num NumWAL
// segments contains the list of the consistuent physical segment files that
// make up the single logical WAL file. segments is ordered by increasing
// logIndex.
segments []segment
}
// A segment represents an individual physical file that makes up a contiguous
// segment of a logical WAL. If a failover occurred during a WAL's lifetime, a
// WAL may be composed of multiple segments.
type segment struct {
logNameIndex logNameIndex
dir Dir
}
// String implements fmt.Stringer.
func (s segment) String() string {
return fmt.Sprintf("(%s,%s)", s.dir.Dirname, s.logNameIndex)
}
// NumSegments returns the number of consitutent physical log files that make up
// thie log.
func (ll LogicalLog) NumSegments() int { return len(ll.segments) }
// SegmentLocation returns the FS and path for the i-th physical segment file.
func (ll LogicalLog) SegmentLocation(i int) (vfs.FS, string) {
s := ll.segments[i]
path := s.dir.FS.PathJoin(s.dir.Dirname, makeLogFilename(ll.Num, s.logNameIndex))
return s.dir.FS, path
}
// PhysicalSize stats each of the log's physical files, summing their sizes.
func (ll LogicalLog) PhysicalSize() (uint64, error) {
var size uint64
for i := range ll.segments {
fs, path := ll.SegmentLocation(i)
stat, err := fs.Stat(path)
if err != nil {
return 0, err
}
size += uint64(stat.Size())
}
return size, nil
}
// OpenForRead a logical WAL for reading.
func (ll LogicalLog) OpenForRead() Reader {
return newVirtualWALReader(ll)
}
// String implements fmt.Stringer.
func (ll LogicalLog) String() string {
var sb strings.Builder
sb.WriteString(base.DiskFileNum(ll.Num).String())
sb.WriteString(": {")
for i := range ll.segments {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(ll.segments[i].String())
}
sb.WriteString("}")
return sb.String()
}
// Scan finds all log files in the provided directories. It returns an
// ordered list of WALs in increasing NumWAL order.
func Scan(dirs ...Dir) (Logs, error) {
var wals []LogicalLog
for _, d := range dirs {
ls, err := d.FS.List(d.Dirname)
if err != nil {
return nil, errors.Wrapf(err, "reading %q", d.Dirname)
}
for _, name := range ls {
dfn, li, ok := parseLogFilename(name)
if !ok {
continue
}
// Have we seen this logical log number yet?
i, found := slices.BinarySearchFunc(wals, dfn, func(lw LogicalLog, n NumWAL) int {
return cmp.Compare(lw.Num, n)
})
if !found {
wals = slices.Insert(wals, i, LogicalLog{Num: dfn, segments: make([]segment, 0, 1)})
}
// Ensure we haven't seen this log index yet, and find where it
// slots within this log's segments.
j, found := slices.BinarySearchFunc(wals[i].segments, li, func(s segment, li logNameIndex) int {
return cmp.Compare(s.logNameIndex, li)
})
if found {
return nil, errors.Errorf("wal: duplicate logIndex=%s for WAL %s in %s and %s",
li, dfn, d.Dirname, wals[i].segments[j].dir.Dirname)
}
wals[i].segments = slices.Insert(wals[i].segments, j, segment{logNameIndex: li, dir: d})
}
}
return wals, nil
}
// Logs holds a collection of WAL files.
type Logs []LogicalLog
// Get retrieves the WAL with the given number if present. The second return
// value indicates whether or not the WAL was found.
func (l Logs) Get(num NumWAL) (LogicalLog, bool) {
i, found := slices.BinarySearchFunc(l, num, func(lw LogicalLog, n NumWAL) int {
return cmp.Compare(lw.Num, n)
})
if !found {
return LogicalLog{}, false
}
return l[i], true
}
func newVirtualWALReader(wal LogicalLog) *virtualWALReader {
return &virtualWALReader{
LogicalLog: wal,
currIndex: -1,
}
}
// A virtualWALReader takes an ordered sequence of physical WAL files
// ("segments") and implements the wal.Reader interface, providing a merged view
// of the WAL's logical contents. It's responsible for filtering duplicate
// records which may be shared by the tail of a segment file and the head of its
// successor.
type virtualWALReader struct {
// VirtualWAL metadata.
LogicalLog
// State pertaining to the current position of the reader within the virtual
// WAL and its constituent physical files.
currIndex int
currFile vfs.File
currReader *record.Reader
// off describes the current Offset within the WAL.
off Offset
// lastSeqNum is the sequence number of the batch contained within the last
// record returned to the user. A virtual WAL may be split across a sequence
// of several physical WAL files. The tail of one physical WAL may be
// duplicated within the head of the next physical WAL file. We use
// contained batches' sequence numbers to deduplicate. This lastSeqNum field
// should monotonically increase as we iterate over the WAL files. If we
// ever observe a batch encoding a sequence number <= lastSeqNum, we must
// have already returned the batch and should skip it.
lastSeqNum uint64
// recordBuf is a buffer used to hold the latest record read from a physical
// file, and then returned to the user. A pointer to this buffer is returned
// directly to the caller of NextRecord.
recordBuf bytes.Buffer
}
// *virtualWALReader implements wal.Reader.
var _ Reader = (*virtualWALReader)(nil)
// NextRecord returns a reader for the next record. It returns io.EOF if there
// are no more records. The reader returned becomes stale after the next
// NextRecord call, and should no longer be used.
func (r *virtualWALReader) NextRecord() (io.Reader, Offset, error) {
r.recordBuf.Reset()
// On the first call, we need to open the first file.
if r.currIndex < 0 {
err := r.nextFile()
if err != nil {
return nil, Offset{}, err
}
}
for {
// Update our current physical offset to match the current file offset.
r.off.Physical = r.currReader.Offset()
// Obtain a Reader for the next record within this log file.
rec, err := r.currReader.Next()
if errors.Is(err, io.EOF) {
// This file is exhausted; continue to the next.
err := r.nextFile()
if err != nil {
return nil, r.off, err
}
continue
}
// Copy the record into a buffer. This ensures we read its entirety so
// that NextRecord returns the next record, even if the caller never
// exhausts the previous record's Reader. The record.Reader requires the
// record to be exhausted to read all of the record's chunks before
// attempting to read the next record. Buffering also also allows us to
// easily read the header of the batch down below for deduplication.
if err == nil {
_, err = io.Copy(&r.recordBuf, rec)
}
// The record may be malformed. This is expected during a WAL failover,
// because the tail of a WAL may be only partially written or otherwise
// unclean because of WAL recycling and the inability to write the EOF
// trailer record. If this isn't the last file, we silently ignore the
// invalid record at the tail and proceed to the next file. If it is
// the last file, bubble the error up and let the client decide what to
// do with it. If the virtual WAL is the most recent WAL, Open may also
// decide to ignore it because it's consistent with an incomplete
// in-flight write at the time of process exit/crash. See #453.
if record.IsInvalidRecord(err) && r.currIndex < len(r.segments)-1 {
if err := r.nextFile(); err != nil {
return nil, r.off, err
}
continue
} else if err != nil {
return nil, r.off, err
}
// We may observe repeat records between the physical files that make up
// a virtual WAL because inflight writes to a file on a stalled disk may
// or may not end up completing. WAL records always contain encoded
// batches, and batches that contain data can be uniquely identifed by
// sequence number.
//
// Parse the batch header.
h, ok := batchrepr.ReadHeader(r.recordBuf.Bytes())
if !ok {
// Failed to read the batch header because the record was smaller
// than the length of a batch header. This is unexpected. The record
// envelope successfully decoded and the checkums of the individual
// record fragment(s) validated, so the writer truly wrote an
// invalid batch. During Open WAL recovery treats this as
// corruption. We could return the record to the caller, allowing
// the caller to interpret it as corruption, but it seems safer to
// be explicit and surface the corruption error here.
return nil, r.off, base.CorruptionErrorf("pebble: corrupt log file logNum=%d, logNameIndex=%s: invalid batch",
r.Num, errors.Safe(r.segments[r.currIndex].logNameIndex))
}
// There's a subtlety necessitated by LogData operations. A LogData
// applied to a batch results in data appended to the WAL in a batch
// format, but the data is never applied to the memtable or LSM. A batch
// only containing LogData will repeat a sequence number. We skip these
// batches because they're not relevant for recovery and we do not want
// to mistakenly deduplicate the batch containing KVs at the same
// sequence number. We can differentiate LogData-only batches through
// their batch headers: they'll encode a count of zero.
if h.Count == 0 {
r.recordBuf.Reset()
continue
}
// If we've already observed a sequence number >= this batch's sequence
// number, we must've already returned this record to the client. Skip
// it.
if h.SeqNum <= r.lastSeqNum {
r.recordBuf.Reset()
continue
}
r.lastSeqNum = h.SeqNum
return &r.recordBuf, r.off, nil
}
}
// Close closes the reader, releasing open resources.
func (r *virtualWALReader) Close() error {
if r.currFile != nil {
if err := r.currFile.Close(); err != nil {
return err
}
}
return nil
}
// nextFile advances the internal state to the next physical segment file.
func (r *virtualWALReader) nextFile() error {
if r.currFile != nil {
err := r.currFile.Close()
r.currFile = nil
if err != nil {
return err
}
}
r.currIndex++
if r.currIndex >= len(r.segments) {
return io.EOF
}
fs, path := r.LogicalLog.SegmentLocation(r.currIndex)
r.off.PhysicalFile = path
r.off.Physical = 0
var err error
if r.currFile, err = fs.Open(path); err != nil {
return errors.Wrapf(err, "opening WAL file segment %q", path)
}
r.currReader = record.NewReader(r.currFile, base.DiskFileNum(r.Num))
return nil
}