/
file_wal.go
371 lines (304 loc) · 7.92 KB
/
file_wal.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
package wal
import (
"bytes"
"encoding/binary"
"errors"
"io/ioutil"
"math"
"os"
"sync"
"github.com/PreetamJinka/catena/partition"
"github.com/youtube/vitess/go/cgzip"
)
var (
// Magic sequence to check for valid data.
walMagic = uint32(0x11141993)
errorInvalidWALMagic = errors.New("wal: invalid WAL magic number")
errorInvalidWALFile = errors.New("wal: invalid WAL file")
)
// A FileWAL is a write-ahead log represented by a file on disk.
type FileWAL struct {
f *os.File
lock sync.Mutex
filename string
// lastReadOffset stores end of the last good
// WAL entry. This way we can truncate the WAL
// and keep appending valid data at the end.
lastReadOffset int64
}
// NewFileWAL returns a new on-disk write-ahead log
// with the given file name.
func NewFileWAL(filename string) (*FileWAL, error) {
// Attempt to open WAL file.
f, err := os.OpenFile(filename, os.O_RDWR|os.O_CREATE|os.O_TRUNC|os.O_EXCL, 0666)
if err != nil {
return nil, err
}
return &FileWAL{
f: f,
filename: filename,
}, nil
}
// OpenFileWAL opens a write-ahead long stored at filename.
func OpenFileWAL(filename string) (*FileWAL, error) {
// Attempt to open an existing WAL file.
f, err := os.OpenFile(filename, os.O_RDWR, 0666)
if err != nil {
return nil, err
}
return &FileWAL{
f: f,
filename: filename,
}, nil
}
// Append writes the WALentry to the write-ahead log.
// It returns the number of bytes written and an error.
func (w *FileWAL) Append(entry WALEntry) (int, error) {
// Make sure we have an open WAL.
if w.f == nil {
return 0, errorInvalidWALFile
}
// Buffer writes until the end.
buf := &bytes.Buffer{}
var err error
scratch := [512]byte{}
// Write magic number
scratch[0] = byte(walMagic)
scratch[1] = byte(walMagic >> 8)
scratch[2] = byte(walMagic >> 16)
scratch[3] = byte(walMagic >> 24)
_, err = buf.Write(scratch[:4])
if err != nil {
return 0, err
}
// Write the operation type
err = buf.WriteByte(byte(entry.Operation))
if err != nil {
return 0, err
}
// Write the number of rows
numRows := uint32(len(entry.Rows))
scratch[0] = byte(numRows)
scratch[1] = byte(numRows >> 8)
scratch[2] = byte(numRows >> 16)
scratch[3] = byte(numRows >> 24)
_, err = buf.Write(scratch[:4])
if err != nil {
return 0, err
}
// Write the size of the entry (0 for now)
_, err = buf.Write(scratch[4:8])
if err != nil {
return 0, err
}
gzipWriter, err := cgzip.NewWriterLevel(buf, cgzip.Z_BEST_SPEED)
if err != nil {
return 0, err
}
for _, row := range entry.Rows {
// Write source name length
_, err = gzipWriter.Write([]byte{byte(len(row.Source)), byte(len(row.Metric))})
if err != nil {
return 0, err
}
// Write source and metric names
_, err = gzipWriter.Write([]byte(row.Source + row.Metric))
if err != nil {
return 0, err
}
// Write timestamp and value
scratch[0] = byte(row.Point.Timestamp)
scratch[1] = byte(row.Point.Timestamp >> (8 * 1))
scratch[2] = byte(row.Point.Timestamp >> (8 * 2))
scratch[3] = byte(row.Point.Timestamp >> (8 * 3))
scratch[4] = byte(row.Point.Timestamp >> (8 * 4))
scratch[5] = byte(row.Point.Timestamp >> (8 * 5))
scratch[6] = byte(row.Point.Timestamp >> (8 * 6))
scratch[7] = byte(row.Point.Timestamp >> (8 * 7))
_, err = gzipWriter.Write(scratch[:8])
if err != nil {
return 0, err
}
valueBits := math.Float64bits(row.Point.Value)
scratch[0] = byte(valueBits)
scratch[1] = byte(valueBits >> (8 * 1))
scratch[2] = byte(valueBits >> (8 * 2))
scratch[3] = byte(valueBits >> (8 * 3))
scratch[4] = byte(valueBits >> (8 * 4))
scratch[5] = byte(valueBits >> (8 * 5))
scratch[6] = byte(valueBits >> (8 * 6))
scratch[7] = byte(valueBits >> (8 * 7))
_, err = gzipWriter.Write(scratch[:8])
if err != nil {
return 0, err
}
}
err = gzipWriter.Close()
if err != nil {
return 0, err
}
entrySize := buf.Len() - 13
result := buf.Bytes()
// Write the size of the entry
entrySizeUint32 := uint32(entrySize)
scratch[0] = byte(entrySizeUint32)
scratch[1] = byte(entrySizeUint32 >> 8)
scratch[2] = byte(entrySizeUint32 >> 16)
scratch[3] = byte(entrySizeUint32 >> 24)
copy(result[9:13], scratch[:4])
w.lock.Lock()
// Record the current offset so we can truncate
// later in case something goes wrong.
currentOffset, err := w.f.Seek(0, 1)
if err != nil {
w.lock.Unlock()
return 0, err
}
w.lastReadOffset = currentOffset
// Flush to the file.
n, err := w.f.Write(result)
if err != nil {
w.Truncate()
w.lock.Unlock()
return 0, err
}
w.lock.Unlock()
return n, err
}
// ReadEntry reads a WALEntry from the write-ahead log.
// If a non-nil error is returned, w.Truncate() may
// be called to make the WAL safe for writing.
func (w *FileWAL) ReadEntry() (WALEntry, error) {
w.lock.Lock()
entry := WALEntry{}
var err error
// Make sure we have an open WAL.
if w.f == nil {
w.lock.Unlock()
return entry, errorInvalidWALFile
}
// Read magic value.
magic := uint32(0)
err = binary.Read(w.f, binary.LittleEndian, &magic)
if err != nil {
w.lock.Unlock()
return entry, err
}
if magic != walMagic {
w.lock.Unlock()
return entry, errorInvalidWALMagic
}
// Read the operation type.
err = binary.Read(w.f, binary.LittleEndian, &entry.Operation)
if err != nil {
w.lock.Unlock()
return entry, err
}
// Read the number of rows.
numRows := uint32(0)
err = binary.Read(w.f, binary.LittleEndian, &numRows)
if err != nil {
w.lock.Unlock()
return entry, err
}
// Read the size of the entry.
entrySize := uint32(0)
err = binary.Read(w.f, binary.LittleEndian, &entrySize)
if err != nil {
w.lock.Unlock()
return entry, err
}
entryBytes := make([]byte, int(entrySize))
n, err := w.f.Read(entryBytes)
if err != nil {
w.lock.Unlock()
return entry, err
}
if n != int(entrySize) {
w.lock.Unlock()
return entry, errors.New("wal: did not read full entry")
}
r := bytes.NewReader(entryBytes)
if err != nil {
w.lock.Unlock()
return entry, err
}
gzipReader, err := cgzip.NewReader(r)
if err != nil {
w.lock.Unlock()
return entry, err
}
uncompressed, err := ioutil.ReadAll(gzipReader)
if err != nil {
gzipReader.Close()
w.lock.Unlock()
return entry, err
}
gzipReader.Close()
r = bytes.NewReader(uncompressed)
for i := uint32(0); i < numRows; i++ {
row := partition.Row{}
sourceNameLength, metricNameLength := uint8(0), uint8(0)
// Read the source and metric name lengths.
err = binary.Read(r, binary.LittleEndian, &sourceNameLength)
if err != nil {
w.lock.Unlock()
return entry, err
}
err = binary.Read(r, binary.LittleEndian, &metricNameLength)
if err != nil {
w.lock.Unlock()
return entry, err
}
sourceAndMetricNames := make([]byte, int(sourceNameLength+metricNameLength))
_, err = r.Read(sourceAndMetricNames)
if err != nil {
w.lock.Unlock()
return entry, err
}
row.Source = string(sourceAndMetricNames[:int(sourceNameLength)])
row.Metric = string(sourceAndMetricNames[int(sourceNameLength):])
err = binary.Read(r, binary.LittleEndian, &row.Point)
if err != nil {
w.lock.Unlock()
return entry, err
}
entry.Rows = append(entry.Rows, row)
}
// We've decoded everything fine.
// We now update lastReadOffset to the current offset
// in the file.
currentOffset, err := w.f.Seek(0, 1)
if err != nil {
w.lock.Unlock()
return entry, err
}
w.lastReadOffset = currentOffset
w.lock.Unlock()
return entry, err
}
// Truncate truncates w's backing file to
// lastReadOffset. Truncation ensures that
// new entries can be safely read after
// they are appended.
func (w *FileWAL) Truncate() error {
return w.f.Truncate(w.lastReadOffset)
}
// Close flushes any pending writes and closes the file.
func (w *FileWAL) Close() error {
w.f.Sync()
w.f.Close()
return nil
}
// Destroy closes the FileWAL and removes the
// file on disk.
func (w *FileWAL) Destroy() error {
w.Close()
err := os.Remove(w.filename)
return err
}
func (w *FileWAL) Filename() string {
return w.filename
}
// FileWAL is a WAL
var _ WAL = &FileWAL{}