-
Notifications
You must be signed in to change notification settings - Fork 1
/
valuelog.go
690 lines (557 loc) · 14.6 KB
/
valuelog.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
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
package golbat
import (
"bytes"
"encoding/binary"
"fmt"
"hash/crc32"
"io/ioutil"
"math"
"os"
"path/filepath"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"github.com/neotse/golbat/internel"
"github.com/pkg/errors"
)
const ValueFileExt = ".vlog"
const maxVlogFileSize = math.MaxUint32
type valueLog struct {
dirPath string
// guards our view of which files exist, which to be deleted, how many active iterators
filesLock sync.RWMutex
filesMap map[uint32]*logFile
maxFid uint32
filesToBeDeleted []uint32
// A refcount of iterators -- when this hits zero, we can delete the filesToBeDeleted.
numActiveIterators int32
writableLogOffset uint32 // read by read, written by write. Must access via atomics.
numEntriesWritten uint32
valueLogFileSize uint32
valueLogMaxEntries uint32
valueThreshold uint32
garbageCh chan struct{}
discard *discard
logger internel.Logger
}
func OpenValueLog(option Options) (*valueLog, error) {
v := &valueLog{
dirPath: option.ValueLogDir,
valueLogFileSize: uint32(option.ValueLogFileSize),
valueLogMaxEntries: uint32(option.ValueLogMaxEntries),
valueThreshold: uint32(option.ValueThreshold),
garbageCh: make(chan struct{}, 1),
logger: option.Logger,
}
if err := v.loadFiles(); err != nil {
return nil, err
}
d, err := NewDiscard(option)
if err != nil {
return nil, err
}
v.discard = d
// first open
if len(v.filesMap) == 0 {
_, err := v.createValueLogFile()
if err != nil {
return nil, Wrapf(err, "Create a new log file failed while open value log")
}
return v, nil
}
// filter out the deleting log file
fids := v.sortAndFilterFids()
for _, fid := range fids {
lf, ok := v.filesMap[fid]
AssertTrue(ok)
if err := lf.Open(valueLogFilePath(v.dirPath, fid), os.O_RDWR,
2*option.ValueLogFileSize); err != nil {
return nil, Wrapf(err, "Open existing file: %s", lf.path)
}
if lf.size == 0 && fid != v.maxFid {
v.logger.Infof("Deleting empty file: %s", lf.path)
if err := lf.Delete(); err != nil {
return nil, Wrapf(err, "While deleting a empty log file: %s", lf.path)
}
delete(v.filesMap, fid)
}
}
last, ok := v.filesMap[v.maxFid]
AssertTrue(ok)
offset, err := last.iterate(0, func(_ *entry, vp valPtr) error {
return nil
})
if err != nil {
return nil, Wrapf(err, "While iteratring the last value log file: %s", last.path)
}
// the last value log file is not empty, truncate it and create a new one.
if offset > 0 {
if err := last.Truncate(int64(offset)); err != nil {
return nil, Wrapf(err, "While truncating the last value log file: %s", last.path)
}
if _, err := v.createValueLogFile(); err != nil {
return nil, Wrapf(err, "While creating a new last value log file")
}
}
// the last value log file is empty, reuse it.
return v, nil
}
func (v *valueLog) Write(batch *writeBatchInternel) error {
if err := v.validate(batch); err != nil {
return err
}
v.filesLock.RLock()
maxFid := v.maxFid
curLogFile := v.filesMap[maxFid]
v.filesLock.RUnlock()
defer func() {
if batch.sync {
if err := curLogFile.Sync(); err != nil {
v.logger.Errorf("Error Sync value log(%s): %+v", curLogFile.path, err)
}
}
}()
// clear
batch.ptrs = batch.ptrs[:0]
written := 0
// write every entry
buf := &bytes.Buffer{}
for _, e := range batch.entries {
if e.checkWithThreshold(v.valueThreshold) {
batch.ptrs = append(batch.ptrs, valPtr{})
continue
}
p := valPtr{}
p.fid = curLogFile.fid
p.offset = v.writeOffset()
plen, err := curLogFile.EncodeEntryTo(e, buf)
if err != nil {
return err
}
// atomic update the offset, allow the concurrently write entry to the same log file
atomic.AddUint32(&v.writableLogOffset, plen)
curLogFile.WriteEntryFrom(buf)
p.len = plen
batch.ptrs = append(batch.ptrs, p)
written++
}
v.numEntriesWritten += uint32(written)
// flush
if _, err := v.flush(curLogFile); err != nil {
return err
}
return nil
}
func (v *valueLog) Read(option *ReadOptions, vp valPtr) (*entry, error) {
lf, err := v.getLogFile(vp)
if err != nil {
return nil, err
}
defer lf.lock.RUnlock()
buf, err := lf.readWithValPtr(vp)
if err != nil {
return nil, err
}
if option.VerifyCheckSum {
hash := crc32.New(internel.CastagnoliCrcTable)
if _, err := hash.Write(buf[:len(buf)-crc32.Size]); err != nil {
return nil, Wrapf(err, "Failed compute the crc32 of value ptr: %+v", vp)
}
checksum := buf[len(buf)-crc32.Size:]
if hash.Sum32() != binary.BigEndian.Uint32(checksum) {
return nil, Wrapf(ErrChecksumMismatch, "Corrupted value at: %+v", vp)
}
}
var h header
hlen := h.Decode(buf)
kv := buf[hlen:]
if len(kv) < int(h.klen+h.vlen) {
return nil, errors.Errorf("Invalid value ptr: %+v, need len: %d, but has len: %d",
vp, h.klen+h.vlen, len(kv))
}
return &entry{
key: kv[:h.klen],
value: kv[h.klen : h.klen+h.vlen],
offset: vp.offset,
hlen: uint16(hlen),
rtype: h.recordType,
}, nil
}
func (v *valueLog) Close() error {
if v == nil {
return nil
}
v.logger.Debugf("Closing value log and stopping garbage collection.")
var err error
for id, lf := range v.filesMap {
lf.lock.Lock() // We won’t release the lock.
offset := int64(-1)
if id == v.maxFid {
offset = int64(v.writeOffset())
}
if terr := lf.CloseWithTruncate(offset); terr != nil && err == nil {
err = terr
}
}
if terr := v.discard.Close(); err == nil && terr != nil {
err = terr
}
return err
}
// sync function syncs content of latest value log file to disk.
func (v *valueLog) Sync() error {
v.filesLock.RLock()
maxFid := v.maxFid
currLogFile := v.filesMap[maxFid]
if currLogFile == nil {
v.filesLock.RUnlock()
return nil
}
currLogFile.lock.RLock()
v.filesLock.RUnlock()
err := currLogFile.Sync()
currLogFile.lock.RUnlock()
return err
}
func (v *valueLog) writeOffset() uint32 {
return atomic.LoadUint32(&v.writableLogOffset)
}
func (v *valueLog) createValueLogFile() (*logFile, error) {
fid := v.maxFid + 1
fpath := valueLogFilePath(v.dirPath, fid)
lf := &logFile{
fid: fid,
path: fpath,
pos: 0,
}
err := lf.Open(fpath, os.O_RDWR|os.O_CREATE|os.O_EXCL, 2*int(v.valueLogFileSize))
if err != nil {
return nil, err
}
v.filesLock.Lock()
defer v.filesLock.Unlock()
v.filesMap[fid] = lf
v.maxFid = fid
atomic.StoreUint32(&v.writableLogOffset, 0)
v.numEntriesWritten = 0
return lf, nil
}
func (v *valueLog) deleteValueLogFile(lf *logFile) error {
if lf == nil {
return nil
}
lf.lock.Lock()
defer lf.lock.Unlock()
return lf.Delete()
}
func (v *valueLog) loadFiles() error {
v.filesMap = make(map[uint32]*logFile)
files, err := ioutil.ReadDir(v.dirPath)
if err != nil {
Wrapf(err, "Unable to open value log dir. Path: %s", v.dirPath)
}
// some file may duplicated, so we need a Set to found these.
exist := make(map[uint64]struct{})
for _, file := range files {
fname := file.Name()
if !strings.HasSuffix(fname, ValueFileExt) {
continue
}
fid, err := getFileIdFromName(fname)
if err != nil {
return Wrapf(err, "Unable to parse value log file id. File: %s", fname)
}
if _, ok := exist[fid]; ok {
return Wrapf(err, "Duplicate file found. Please delete one manually. File: %s", fname)
}
exist[fid] = struct{}{}
lf := &logFile{
fid: uint32(fid),
path: filepath.Join(v.dirPath, fname),
}
v.filesMap[lf.fid] = lf
if v.maxFid < lf.fid {
v.maxFid = lf.fid
}
}
return nil
}
func (v *valueLog) sortAndFilterFids() []uint32 {
deleted := make(map[uint32]struct{})
for _, fid := range v.filesToBeDeleted {
deleted[fid] = struct{}{}
}
res := make([]uint32, 0, len(v.filesMap))
for fid := range v.filesMap {
if _, ok := deleted[fid]; !ok {
res = append(res, fid)
}
}
sort.Slice(res, func(i, j int) bool {
return res[i] < res[j]
})
return res
}
func (v *valueLog) validate(batch *writeBatchInternel) error {
offset := uint64(v.writeOffset())
estimatedVlogOffset := batch.ApproximateSize() + offset
if estimatedVlogOffset > uint64(maxVlogFileSize) {
return errors.Errorf("Batch size offset %d is bigger than maximum offset %d",
estimatedVlogOffset, maxVlogFileSize)
}
return nil
}
func (v *valueLog) flush(lf *logFile) (*logFile, error) {
if v.writeOffset() <= v.valueLogFileSize && v.numEntriesWritten <= v.valueLogMaxEntries {
return lf, nil
}
if err := lf.Flush(v.writeOffset()); err != nil {
return nil, err
}
newlf, err := v.createValueLogFile()
if err != nil {
return nil, err
}
return newlf, nil
}
func (v *valueLog) getLogFile(vp valPtr) (*logFile, error) {
v.filesLock.RLock()
defer v.filesLock.RUnlock()
lf, ok := v.filesMap[vp.fid]
if !ok {
return nil, errors.Errorf("File id: %d not found.", vp.fid)
}
maxFid := v.maxFid
if vp.fid == maxFid {
offset := v.writeOffset()
if vp.offset >= offset {
return nil, errors.Errorf(
"Invalid read offset: %d greater than current offset: %d", vp.offset, offset)
}
}
// may be the gc goroutine read this file concurrently
lf.lock.RLock()
return lf, nil
}
func (v *valueLog) updateDiscard(stats map[uint32]uint64) {
for fid, count := range stats {
v.discard.Update(fid, count)
}
}
func (v *valueLog) runGC(discardRatio float64, db DB) error {
select {
case v.garbageCh <- struct{}{}:
defer func() {
<-v.garbageCh
}()
lf := v.selectGCFile(discardRatio)
if lf == nil {
return ErrNoRewrite
}
if err := v.gcLogFile(lf, db); err != nil {
return err
}
v.discard.Reset(lf.fid)
return nil
default:
return ErrRejected
}
}
func (v *valueLog) waitGC(closer *internel.Closer) {
defer closer.Done()
<-closer.HasBeenClosed()
// Block any GC in progress to finish, and don't allow any more writes to runGC by filling up
// the channel of size 1.
v.garbageCh <- struct{}{}
}
var writeOption = &WriteOptions{Sync: true}
func (v *valueLog) selectGCFile(ratio float64) *logFile {
v.filesLock.RLock()
defer v.filesLock.RUnlock()
var fid uint32
var count uint64
var lf *logFile
for {
fid, count = v.discard.Max()
if fid == 0 {
v.logger.Debugf("No file need to garbage collection.")
return nil
}
logFile, ok := v.filesMap[fid]
if !ok {
v.discard.Reset(fid)
} else {
lf = logFile
break
}
}
f, err := lf.Fd.Stat()
if err != nil {
v.logger.Errorf("Unable to get stats for value log fid: %d err: %+v", f, err)
return nil
}
// the max discard count of the selected file doesn't reach the ratio
if thresold := ratio * float64(f.Size()); thresold > float64(count) {
v.logger.Debugf("Discard: %d less than threshold: %.0f for file: %s",
count, thresold, f.Name())
return nil
}
maxFid := atomic.LoadUint32(&v.maxFid)
if fid < maxFid {
v.logger.Infof("Found value log max discard fid: %d discard: %d", maxFid, count)
return lf
}
return nil
}
func (v *valueLog) gcLogFile(lf *logFile, db DB) error {
v.filesLock.RLock()
for _, fid := range v.filesToBeDeleted {
if lf.fid == fid {
v.filesLock.Unlock()
return errors.Errorf("The value log has been marked for deletion. fid: %d", fid)
}
}
maxFid := v.maxFid
if lf.fid >= maxFid {
return errors.Errorf("The value log id equal or greater than maxFid. fid: %d, maxFid: %d",
lf.fid, maxFid)
}
v.filesLock.RUnlock()
v.logger.Infof("Gc value log: %d, path:%d", lf.fid, lf.path)
option := db.GetOption()
wb := NewWriteBatch(db)
var size uint32
var count, moved int
ropt := &ReadOptions{VerifyCheckSum: true}
filter := func(e *entry, _ valPtr) error {
count++
if count%100000 == 0 {
v.logger.Debugf("Processing entry %d", count)
}
ev, err := db.GetExtend(ropt, e.key)
if err != nil {
return err
}
// Version not found. Discard.
if ev.version != ParseVersion(e.key) {
return nil
}
// lsm store the key with value. Discard.
if ev.Meta&ValPtr == 0 {
return nil
}
// entry has been deleted. Discard.
if ev.Meta&Delete == Delete {
return nil
}
// Value isn't in the lsm, but still present in value log.
if len(ev.Value) == 0 {
return errors.Errorf("Empty value: %+v", ev)
}
var vp valPtr
vp.Decode(ev.Value)
if vp.fid == lf.fid && vp.offset == e.offset {
moved++
ne := entry{}
// deep copy
ne.key = append(ne.key, e.key...)
ne.value = append(ne.value, e.value...)
ne.rtype = e.rtype &^ ValPtr
es := ne.estimateSize(uint32(option.ValueThreshold)) + uint32(len(ne.value))
if wb.Count()+1 >= option.maxBatchCount || size+es >= uint32(option.maxBatchSize) {
if err := db.Write(writeOption, wb); err != nil {
return err
}
wb.Clear()
}
wb.Put(e.key, e.value)
size += es
}
return nil
}
_, err := lf.iterate(0, filter)
if err != nil {
return err
}
if err := db.Write(writeOption, wb); err != nil {
return err
}
v.logger.Infof("Total entries: %d. Moved: %d", count, moved)
v.logger.Infof("Removing file: %d, path: %s", lf.fid, lf.path)
canDeleteLogFile := false
v.filesLock.Lock()
if _, ok := v.filesMap[lf.fid]; !ok {
v.filesLock.Unlock()
return errors.Errorf("Not found the file need to delete: %d", lf.fid)
}
if v.iteratorCount() == 0 {
delete(v.filesMap, lf.fid)
canDeleteLogFile = true
} else {
v.filesToBeDeleted = append(v.filesToBeDeleted, lf.fid)
}
v.filesLock.Unlock()
if canDeleteLogFile {
if err := v.deleteValueLogFile(lf); err != nil {
return err
}
}
return nil
}
func (v *valueLog) iteratorCount() int {
return int(atomic.LoadInt32(&v.numActiveIterators))
}
func (v *valueLog) incrIteratorCount() {
atomic.AddInt32(&v.numActiveIterators, 1)
}
func (v *valueLog) decrIteratorCount() error {
num := atomic.AddInt32(&v.numActiveIterators, -1)
if num != 0 {
return nil
}
v.filesLock.Lock()
lfs := make([]*logFile, 0, len(v.filesToBeDeleted))
for _, id := range v.filesToBeDeleted {
lfs = append(lfs, v.filesMap[id])
delete(v.filesMap, id)
}
v.filesToBeDeleted = nil
v.filesLock.Unlock()
for _, lf := range lfs {
if err := v.deleteValueLogFile(lf); err != nil {
return err
}
}
return nil
}
func (v *valueLog) dropAll() (int, error) {
var count int
deleteAll := func() error {
v.filesLock.Lock()
defer v.filesLock.Unlock()
for _, lf := range v.filesMap {
if err := v.deleteValueLogFile(lf); err != nil {
return err
}
count++
}
v.filesMap = make(map[uint32]*logFile)
v.maxFid = 0
return nil
}
if err := deleteAll(); err != nil {
return count, err
}
if _, err := v.createValueLogFile(); err != nil {
return count, err
}
return count, nil
}
func valueLogFilePath(dir string, fid uint32) string {
return filepath.Join(dir, fmt.Sprintf("%06d%s", fid, ValueFileExt))
}
func getFileIdFromName(fileName string) (uint64, error) {
return strconv.ParseUint(fileName[:len(fileName)-len(ValueFileExt)], 10, 32)
}