/
vlog.go
516 lines (428 loc) · 12.2 KB
/
vlog.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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Copyright 2019 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.
package storage
import (
"bytes"
"encoding/binary"
"fmt"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/pingcap/errors"
"github.com/pingcap/log"
"go.uber.org/zap"
pb "github.com/pingcap/tipb/go-binlog"
)
const (
// we finalize the curFile when size >= finalizeFileSizeAtClose when closing the vlog, we don't need to scan the too big curFile to recover the maxTS of the file when reopen the vlog
// TODO we can always finalize the curFile when close Storage, and truncate the footer when open if we want to continue writing this file, so no need to scan the file to get the info in footer
finalizeFileSizeAtClose = 50 * 1024 // 50K
fileExt = ".vlog"
)
// Options is the config options of Append and vlog
type Options struct {
ValueLogFileSize int64
Sync bool
KVChanCapacity int
SlowWriteThreshold float64
StopWriteAtAvailableSpace uint64
KVConfig *KVConfig
}
// DefaultOptions return the default options
func DefaultOptions() *Options {
return &Options{
ValueLogFileSize: 500 * (1 << 20),
Sync: true,
KVChanCapacity: chanCapacity,
SlowWriteThreshold: slowWriteThreshold,
}
}
// WithKVConfig set the Config
func (o *Options) WithKVConfig(kvConfig *KVConfig) *Options {
o.KVConfig = kvConfig
return o
}
// WithStopWriteAtAvailableSpace set the Config
func (o *Options) WithStopWriteAtAvailableSpace(bytes uint64) *Options {
o.StopWriteAtAvailableSpace = bytes
return o
}
// WithSlowWriteThreshold set the Config
func (o *Options) WithSlowWriteThreshold(threshold float64) *Options {
o.SlowWriteThreshold = threshold
return o
}
// WithValueLogFileSize set the ValueLogFileSize
func (o *Options) WithValueLogFileSize(size int64) *Options {
o.ValueLogFileSize = size
return o
}
// WithKVChanCapacity set the ChanCapacity
func (o *Options) WithKVChanCapacity(capacity int) *Options {
o.KVChanCapacity = capacity
return o
}
// WithSync set the Sync
func (o *Options) WithSync(sync bool) *Options {
o.Sync = sync
return o
}
type request struct {
startTS int64
commitTS int64
tp pb.BinlogType
payload []byte
valuePointer valuePointer
wg sync.WaitGroup
err error
}
func (r *request) ts() int64 {
if r.tp == pb.BinlogType_Prewrite {
return r.startTS
}
return r.commitTS
}
func (r *request) String() string {
return fmt.Sprintf("{ts: %d, payload len: %d, valuePointer: %+v}", r.ts(), len(r.payload), r.valuePointer)
}
type batchRequest []*request
// String implements fmt.Stringer
func (b *batchRequest) String() string {
s := new(strings.Builder)
s.WriteString("{")
for _, r := range []*request(*b) {
s.WriteString(r.String())
s.WriteByte(',')
}
s.WriteString("}")
return s.String()
}
type valuePointer struct {
Fid uint32
Offset int64
}
func (vp valuePointer) less(other valuePointer) bool {
if vp.Fid != other.Fid {
return vp.Fid < other.Fid
}
return vp.Offset < other.Offset
}
// MarshalBinary never return not nil err now
func (vp *valuePointer) MarshalBinary() (data []byte, err error) {
data = make([]byte, 12)
binary.LittleEndian.PutUint32(data, vp.Fid)
binary.LittleEndian.PutUint64(data[4:], uint64(vp.Offset))
return
}
// UnmarshalBinary implement encoding.BinaryMarshal
func (vp *valuePointer) UnmarshalBinary(data []byte) error {
if len(data) < 12 {
return errors.New("not enough data")
}
vp.Fid = binary.LittleEndian.Uint32(data)
vp.Offset = int64(binary.LittleEndian.Uint64(data[4:]))
return nil
}
type valueLog struct {
buf *bytes.Buffer // buf to write to the current log file
dirPath string
sync bool
maxFid uint32
filesLock sync.RWMutex
gcLock sync.Mutex
filesMap map[uint32]*logFile
opt *Options
}
func newValueLog(valueDir string, options *Options) (*valueLog, error) {
vlog := new(valueLog)
if err := vlog.open(valueDir, options); err != nil {
return nil, errors.Trace(err)
}
return vlog, nil
}
func (vlog *valueLog) filePath(fid uint32) string {
return filepath.Join(vlog.dirPath, fmt.Sprintf("%06d%s", fid, fileExt))
}
func (vlog *valueLog) getFileRLocked(fid uint32) (*logFile, error) {
vlog.filesLock.RLock()
defer vlog.filesLock.RUnlock()
ret, ok := vlog.filesMap[fid]
if !ok {
return nil, errors.NotFoundf("not found fid: %d", fid)
}
ret.lock.RLock()
return ret, nil
}
func (vlog *valueLog) open(path string, opt *Options) error {
if opt == nil {
opt = DefaultOptions()
}
vlog.dirPath = path
vlog.sync = opt.Sync
vlog.opt = opt
vlog.buf = new(bytes.Buffer)
vlog.filesMap = make(map[uint32]*logFile)
if err := vlog.openOrCreateFiles(); err != nil {
return errors.Annotatef(err, "unable to open value log")
}
return nil
}
func (vlog *valueLog) openOrCreateFiles() error {
files, err := os.ReadDir(vlog.dirPath)
if err != nil {
return errors.Annotatef(err, "error while read dir: %s", vlog.dirPath)
}
// open all files at start, or we can lazily open it to quick start time
// the vlog file name <fid>.vlog will be like "000001.vlog"
for _, file := range files {
if file.IsDir() {
continue
}
fName := file.Name()
if !strings.HasSuffix(fName, fileExt) {
continue
}
fid64, err := strconv.ParseUint(strings.TrimSuffix(fName, fileExt), 10, 32)
if err != nil {
return errors.Annotatef(err, "parse file %s err", fName)
}
fid := uint32(fid64)
logFile, err := newLogFile(fid, vlog.filePath(fid))
if err != nil {
return errors.Annotatef(err, "error open file %s", fName)
}
vlog.filesMap[fid] = logFile
if fid > vlog.maxFid {
vlog.maxFid = fid
}
}
// no files, then create the first file with fid = 0
if len(vlog.filesMap) == 0 {
_, err := vlog.createLogFile(0)
if err != nil {
return errors.Annotatef(err, "error create first file")
}
} else {
// maxFid is the file we will append record to, check if we need to create a new one
curFile := vlog.filesMap[vlog.maxFid]
if curFile.end {
_, err := vlog.createLogFile(atomic.AddUint32(&vlog.maxFid, 1))
if err != nil {
return errors.Annotatef(err, "error create new file")
}
}
}
return nil
}
func (vlog *valueLog) createLogFile(fid uint32) (*logFile, error) {
path := vlog.filePath(fid)
logFile, err := newLogFile(fid, path)
if err != nil {
return nil, errors.Annotate(err, "unable to create log file")
}
vlog.filesLock.Lock()
vlog.filesMap[fid] = logFile
vlog.filesLock.Unlock()
return logFile, nil
}
func (vlog *valueLog) close() error {
vlog.filesLock.Lock()
defer vlog.filesLock.Unlock()
var err error
curFile := vlog.filesMap[vlog.maxFid]
// finalize the curFile when it's tool big, so when restart, we don't need to scan the too big curFile to recover the maxTS of the file
if curFile.GetWriteOffset() >= finalizeFileSizeAtClose {
err = curFile.finalize()
if err != nil {
return errors.Annotatef(err, "finalize file %s failed", curFile.path)
}
}
for _, logFile := range vlog.filesMap {
err = logFile.close()
if err != nil {
return errors.Annotatef(err, "close %s failed", logFile.path)
}
}
return nil
}
func (vlog *valueLog) readValue(vp valuePointer) ([]byte, error) {
logFile, err := vlog.getFileRLocked(vp.Fid)
if err != nil {
return nil, errors.Annotatef(err, "get file(id: %d) failed", vp.Fid)
}
defer logFile.lock.RUnlock()
record, err := logFile.readRecord(vp.Offset)
if err != nil {
return nil, errors.Annotatef(err, "read record at %+v failed", vp)
}
return record.payload, nil
}
// write is thread-unsafe by design and should not be called concurrently.
func (vlog *valueLog) write(reqs []*request) error {
vlog.filesLock.RLock()
curFile := vlog.filesMap[vlog.maxFid]
vlog.filesLock.RUnlock()
var bufReqs []*request
vlog.buf.Reset()
rotate := func() error {
err := curFile.finalize()
if err != nil {
return errors.Annotatef(err, "finalize file %s failed", curFile.path)
}
id := atomic.AddUint32(&vlog.maxFid, 1)
curFile, err = vlog.createLogFile(id)
if err != nil {
return errors.Annotatef(err, "create file id %d failed", id)
}
return nil
}
toDisk := func() error {
writeT0 := time.Now()
err := curFile.Write(vlog.buf.Bytes(), vlog.sync)
writeBinlogTimeHistogram.WithLabelValues("to_disk").Observe(time.Since(writeT0).Seconds())
if err != nil {
return errors.Trace(err)
}
for _, req := range bufReqs {
curFile.updateMaxTS(req.ts())
}
vlog.buf.Reset()
bufReqs = bufReqs[:0]
// rotate file
if curFile.GetWriteOffset() > vlog.opt.ValueLogFileSize {
rotateT0 := time.Now()
err := rotate()
writeBinlogTimeHistogram.WithLabelValues("rotate").Observe(time.Since(rotateT0).Seconds())
if err != nil {
return err
}
}
return nil
}
for _, req := range reqs {
req.valuePointer.Fid = curFile.fid
req.valuePointer.Offset = curFile.GetWriteOffset() + int64(vlog.buf.Len())
_, err := encodeRecord(vlog.buf, req.payload)
if err != nil {
return errors.Trace(err)
}
bufReqs = append(bufReqs, req)
writeNow := curFile.GetWriteOffset()+int64(vlog.buf.Len()) > vlog.opt.ValueLogFileSize
if writeNow {
if err := toDisk(); err != nil {
return errors.Annotate(err, "write to disk failed")
}
}
}
return toDisk()
}
// sortedFids returns the file id sorted
func (vlog *valueLog) sortedFids() []uint32 {
ret := make([]uint32, 0, len(vlog.filesMap))
for fid := range vlog.filesMap {
ret = append(ret, fid)
}
sort.Slice(ret, func(i, j int) bool {
return ret[i] < ret[j]
})
return ret
}
func (vlog *valueLog) scanRequests(start valuePointer, fn func(*request) error) error {
return vlog.scan(start, func(vp valuePointer, record *Record) error {
binlog := new(pb.Binlog)
err := binlog.Unmarshal(record.payload)
if err != nil {
return errors.Trace(err)
}
// skip the wrongly write binlog by pump client previous
if binlog.StartTs == 0 && binlog.CommitTs == 0 {
log.Info("skip empty binlog")
return nil
}
rq := request{
startTS: binlog.StartTs,
commitTS: binlog.CommitTs,
tp: binlog.Tp,
valuePointer: vp,
}
return fn(&rq)
})
}
// scan visits binlogs in order starting from the specified position.
// There are two limitations to the usage of scan:
// 1. Binlogs added in new logFiles after scan starts are not visible, so don't assume
// that every single binlog added would be visited
// 2. If GC is running concurrently, logFiles may be closed and deleted, thus breaking the scanning.
func (vlog *valueLog) scan(start valuePointer, fn func(vp valuePointer, record *Record) error) error {
vlog.gcLock.Lock()
defer vlog.gcLock.Unlock()
vlog.filesLock.RLock()
fids := vlog.sortedFids()
var lfs []*logFile
for _, fid := range fids {
if fid >= start.Fid {
lf := vlog.filesMap[fid]
lfs = append(lfs, lf)
}
}
vlog.filesLock.RUnlock()
for _, lf := range lfs {
var startOffset int64
if lf.fid == start.Fid {
startOffset = start.Offset
}
err := lf.scan(startOffset, fn)
if err != nil {
return errors.Trace(err)
}
}
return nil
}
// delete data <= gcTS
func (vlog *valueLog) gcTS(gcTS int64) {
log.Info("GC vlog", zap.Int64("ts", gcTS))
vlog.gcLock.Lock()
defer vlog.gcLock.Unlock()
vlog.filesLock.Lock()
var toDeleteFiles []*logFile
for _, logFile := range vlog.filesMap {
if logFile.fid == vlog.maxFid {
continue
}
if logFile.maxTS <= gcTS {
toDeleteFiles = append(toDeleteFiles, logFile)
}
}
for _, logFile := range toDeleteFiles {
delete(vlog.filesMap, logFile.fid)
}
vlog.filesLock.Unlock()
for _, logFile := range toDeleteFiles {
logFile.lock.Lock()
err := logFile.close()
if err != nil {
log.Error("close file failed", zap.String("path", logFile.path), zap.Error(err))
}
err = os.Remove(logFile.path)
if err != nil {
log.Error("remove file failed", zap.String("path", logFile.path), zap.Error(err))
}
log.Info("remove file", zap.String("path", logFile.path))
logFile.lock.Unlock()
}
}