forked from dgraph-io/badger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value.go
937 lines (815 loc) · 22.7 KB
/
value.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
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
/*
* Copyright 2017 Dgraph Labs, Inc. and Contributors
*
* 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,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package badger
import (
"bufio"
"bytes"
"encoding/binary"
"fmt"
"hash/crc32"
"io"
"io/ioutil"
"math/rand"
"os"
"sort"
"strconv"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/dgraph-io/badger/y"
"github.com/pkg/errors"
"golang.org/x/net/trace"
)
// Values have their first byte being byteData or byteDelete. This helps us distinguish between
// a key that has never been seen and a key that has been explicitly deleted.
const (
BitDelete byte = 1 // Set if the key has been deleted.
BitValuePointer byte = 2 // Set if the value is NOT stored directly next to key.
BitUnused byte = 4
BitSetIfAbsent byte = 8 // Set if the key is set using SetIfAbsent.
M int64 = 1 << 20
)
// ErrCorrupt is returned when a value log file is corrupted.
var ErrCorrupt = errors.New("Unable to find log. Potential data corruption")
// ErrCasMismatch is returned when a CompareAndSet operation has failed due
// to a counter mismatch.
var ErrCasMismatch = errors.New("CompareAndSet failed due to counter mismatch")
// ErrKeyExists is returned by SetIfAbsent metadata bit is set, but the
// key already exists in the store.
var ErrKeyExists = errors.New("SetIfAbsent failed since key already exists")
const (
maxKeySize = 1 << 20
maxValueSize = 1 << 30
)
type logFile struct {
path string
// This is a lock on the file descriptor's value and the file's existence. Use shared
// ownership when reading/writing the file, use exclusive ownership to open/close the
// descriptor or remove the file.
fdLock sync.RWMutex
fd *os.File
fid uint32
offset uint32
size uint32
}
// openReadOnly assumes that we have a write lock on logFile.
func (lf *logFile) openReadOnly() error {
var err error
lf.fd, err = os.OpenFile(lf.path, os.O_RDONLY, 0666)
if err != nil {
return errors.Wrapf(err, "Unable to open %q as RDONLY.", lf.path)
}
fi, err := lf.fd.Stat()
if err != nil {
return errors.Wrapf(err, "Unable to check stat for %q", lf.path)
}
lf.size = uint32(fi.Size())
return nil
}
// lf must be RLocked (or exclusively locked) if you call this.
func (lf *logFile) read(buf []byte, offset int64) error {
nbr, err := lf.fd.ReadAt(buf, offset)
y.NumReads.Add(1)
y.NumBytesRead.Add(int64(nbr))
return err
}
func (lf *logFile) doneWriting() error {
// Sync before acquiring lock. (We call this from write() and thus know we have shared access
// to the fd.)
if err := lf.fd.Sync(); err != nil {
return errors.Wrapf(err, "Unable to sync value log: %q", lf.path)
}
// Close and reopen the file read-only. Acquire lock because fd will become invalid for a bit.
// Acquiring the lock is bad because, while we don't hold the lock for a long time, it forces
// one batch of readers wait for the preceding batch of readers to finish.
//
// If there's a benefit to reopening the file read-only, it might be on Windows. I don't know
// what the benefit is. Consider keeping the file read-write, or use fcntl to change permissions.
lf.fdLock.Lock()
defer lf.fdLock.Unlock()
if err := lf.fd.Close(); err != nil {
return errors.Wrapf(err, "Unable to close value log: %q", lf.path)
}
return lf.openReadOnly()
}
// You must hold lf.lock to sync()
func (lf *logFile) sync() error {
return lf.fd.Sync()
}
var errStop = errors.New("Stop iteration")
type logEntry func(e Entry, vp valuePointer) error
// iterate iterates over log file. It doesn't not allocate new memory for every kv pair.
// Therefore, the kv pair is only valid for the duration of fn call.
func (lf *logFile) iterate(offset uint32, fn logEntry) error {
_, err := lf.fd.Seek(int64(offset), io.SeekStart)
if err != nil {
return y.Wrap(err)
}
reader := bufio.NewReader(lf.fd)
var hbuf [headerBufSize]byte
var h header
k := make([]byte, 1<<10)
v := make([]byte, 1<<20)
truncate := false
recordOffset := offset
for {
hash := crc32.New(y.CastagnoliCrcTable)
tee := io.TeeReader(reader, hash)
if _, err = io.ReadFull(tee, hbuf[:]); err != nil {
if err == io.EOF {
break
} else if err == io.ErrUnexpectedEOF {
truncate = true
break
}
return err
}
var e Entry
e.offset = recordOffset
h.Decode(hbuf[:])
if h.klen > maxKeySize || h.vlen > maxValueSize {
truncate = true
break
}
vl := int(h.vlen)
if cap(v) < vl {
v = make([]byte, 2*vl)
}
kl := int(h.klen)
if cap(k) < kl {
k = make([]byte, 2*kl)
}
e.Key = k[:kl]
e.Value = v[:vl]
if _, err = io.ReadFull(tee, e.Key); err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
truncate = true
break
}
return err
}
e.Meta = h.meta
e.UserMeta = h.userMeta
e.casCounter = h.casCounter
e.CASCounterCheck = h.casCounterCheck
if _, err = io.ReadFull(tee, e.Value); err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
truncate = true
break
}
return err
}
var crcBuf [4]byte
if _, err = io.ReadFull(reader, crcBuf[:]); err != nil {
if err == io.EOF || err == io.ErrUnexpectedEOF {
truncate = true
break
}
return err
}
crc := binary.BigEndian.Uint32(crcBuf[:])
if crc != hash.Sum32() {
truncate = true
break
}
var vp valuePointer
vp.Len = headerBufSize + h.klen + h.vlen + uint32(len(crcBuf))
recordOffset += vp.Len
vp.Offset = e.offset
vp.Fid = lf.fid
if err := fn(e, vp); err != nil {
if err == errStop {
break
}
return y.Wrap(err)
}
}
if truncate {
if err := lf.fd.Truncate(int64(recordOffset)); err != nil {
return err
}
}
return nil
}
func (vlog *valueLog) rewrite(f *logFile) error {
maxFid := atomic.LoadUint32(&vlog.maxFid)
y.AssertTruef(uint32(f.fid) < maxFid, "fid to move: %d. Current max fid: %d", f.fid, maxFid)
elog := trace.NewEventLog("badger", "vlog-rewrite")
defer elog.Finish()
elog.Printf("Rewriting fid: %d", f.fid)
wb := make([]*Entry, 0, 1000)
var size int64
y.AssertTrue(vlog.kv != nil)
var count int
fe := func(e Entry) error {
count++
if count%10000 == 0 {
elog.Printf("Processing entry %d", count)
}
vs, err := vlog.kv.get(e.Key)
if err != nil {
return err
}
if (vs.Meta & BitDelete) > 0 {
return nil
}
if (vs.Meta & BitValuePointer) == 0 {
return nil
}
// Value is still present in value log.
if len(vs.Value) == 0 {
return errors.Errorf("Empty value: %+v", vs)
}
var vp valuePointer
vp.Decode(vs.Value)
if vp.Fid > f.fid {
return nil
}
if vp.Offset > e.offset {
return nil
}
if vp.Fid == f.fid && vp.Offset == e.offset {
// This new entry only contains the key, and a pointer to the value.
ne := new(Entry)
if e.Meta == BitSetIfAbsent {
// If we rewrite this entry without removing BitSetIfAbsent, lsm would see that
// the key is already present, which would be this same entry and won't update
// the vptr to point to the new file.
e.Meta = 0
}
y.AssertTruef(e.Meta == 0, "Got meta: 0")
ne.Meta = e.Meta
ne.UserMeta = e.UserMeta
ne.Key = make([]byte, len(e.Key))
copy(ne.Key, e.Key)
ne.Value = make([]byte, len(e.Value))
copy(ne.Value, e.Value)
ne.CASCounterCheck = vs.CASCounter // CAS counter check. Do not rewrite if key has a newer value.
wb = append(wb, ne)
size += int64(vlog.opt.estimateSize(ne))
if size >= 64*M {
elog.Printf("request has %d entries, size %d", len(wb), size)
if err := vlog.kv.BatchSet(wb); err != nil {
return err
}
size = 0
wb = wb[:0]
}
} else {
// This can now happen because we can move some entries forward, but then not write
// them to LSM tree due to CAS check failure.
}
return nil
}
err := f.iterate(0, func(e Entry, vp valuePointer) error {
return fe(e)
})
if err != nil {
return err
}
if len(wb) > 0 {
elog.Printf("request has %d entries, size %d", len(wb), size)
if err := vlog.kv.BatchSet(wb); err != nil {
return err
}
}
elog.Printf("Processed %d entries in total", count)
elog.Printf("Removing fid: %d", f.fid)
// Entries written to LSM. Remove the older file now.
{
vlog.filesLock.Lock()
// Just a sanity-check.
if _, ok := vlog.filesMap[f.fid]; !ok {
vlog.filesLock.Unlock()
return errors.Errorf("Unable to find fid: %d", f.fid)
}
delete(vlog.filesMap, f.fid)
vlog.filesLock.Unlock()
}
// Exclusively lock the file so that there are no readers before closing/destroying it
f.fdLock.Lock()
rem := vlog.fpath(f.fid)
f.fd.Close() // close file previous to remove it
f.fdLock.Unlock()
elog.Printf("Removing %s", rem)
return os.Remove(rem)
}
// Entry provides Key, Value and if required, CASCounterCheck to kv.BatchSet() API.
// If CASCounterCheck is provided, it would be compared against the current casCounter
// assigned to this key-value. Set be done on this key only if the counters match.
type Entry struct {
Key []byte
Meta byte
UserMeta byte
Value []byte
CASCounterCheck uint64 // If nonzero, we will check if existing casCounter matches.
Error error // Error if any.
// Fields maintained internally.
offset uint32
casCounter uint64
}
// Encodes e to buf. Returns number of bytes written.
func encodeEntry(e *Entry, buf *bytes.Buffer) (int, error) {
var h header
h.klen = uint32(len(e.Key))
h.vlen = uint32(len(e.Value))
h.meta = e.Meta
h.userMeta = e.UserMeta
h.casCounter = e.casCounter
h.casCounterCheck = e.CASCounterCheck
var headerEnc [headerBufSize]byte
h.Encode(headerEnc[:])
hash := crc32.New(y.CastagnoliCrcTable)
buf.Write(headerEnc[:])
hash.Write(headerEnc[:])
buf.Write(e.Key)
hash.Write(e.Key)
buf.Write(e.Value)
hash.Write(e.Value)
var crcBuf [4]byte
binary.BigEndian.PutUint32(crcBuf[:], hash.Sum32())
buf.Write(crcBuf[:])
return len(headerEnc) + len(e.Key) + len(e.Value) + len(crcBuf), nil
}
func (e Entry) print(prefix string) {
fmt.Printf("%s Key: %s Meta: %d UserMeta: %d Offset: %d len(val)=%d cas=%d check=%d\n",
prefix, e.Key, e.Meta, e.UserMeta, e.offset, len(e.Value), e.casCounter, e.CASCounterCheck)
}
type header struct {
klen uint32
vlen uint32
meta byte
userMeta byte
casCounter uint64
casCounterCheck uint64
}
const (
headerBufSize = 26
)
func (h header) Encode(out []byte) {
y.AssertTrue(len(out) >= headerBufSize)
binary.BigEndian.PutUint32(out[0:4], h.klen)
binary.BigEndian.PutUint32(out[4:8], h.vlen)
out[8] = h.meta
out[9] = h.userMeta
binary.BigEndian.PutUint64(out[10:18], h.casCounter)
binary.BigEndian.PutUint64(out[18:26], h.casCounterCheck)
}
// Decodes h from buf.
func (h *header) Decode(buf []byte) {
h.klen = binary.BigEndian.Uint32(buf[0:4])
h.vlen = binary.BigEndian.Uint32(buf[4:8])
h.meta = buf[8]
h.userMeta = buf[9]
h.casCounter = binary.BigEndian.Uint64(buf[10:18])
h.casCounterCheck = binary.BigEndian.Uint64(buf[18:26])
}
type valuePointer struct {
Fid uint32
Len uint32
Offset uint32
}
func (p valuePointer) Less(o valuePointer) bool {
if p.Fid != o.Fid {
return p.Fid < o.Fid
}
if p.Offset != o.Offset {
return p.Offset < o.Offset
}
return p.Len < o.Len
}
func (p valuePointer) IsZero() bool {
return p.Fid == 0 && p.Offset == 0 && p.Len == 0
}
const valuePointerEncodedSize = 12
// Encode encodes Pointer into byte buffer.
func (p valuePointer) Encode(b []byte) []byte {
binary.BigEndian.PutUint32(b[:4], p.Fid)
binary.BigEndian.PutUint32(b[4:8], p.Len)
binary.BigEndian.PutUint32(b[8:valuePointerEncodedSize], p.Offset)
return b[:valuePointerEncodedSize]
}
func (p *valuePointer) Decode(b []byte) {
p.Fid = binary.BigEndian.Uint32(b[:4])
p.Len = binary.BigEndian.Uint32(b[4:8])
p.Offset = binary.BigEndian.Uint32(b[8:valuePointerEncodedSize])
}
type valueLog struct {
buf bytes.Buffer
dirPath string
elog trace.EventLog
// guards our view of which files exist
filesLock sync.RWMutex
filesMap map[uint32]*logFile
kv *KV
maxFid uint32
offset uint32
opt Options
}
func vlogFilePath(dirPath string, fid uint32) string {
return fmt.Sprintf("%s%s%09d.vlog", dirPath, string(os.PathSeparator), fid)
}
func (vlog *valueLog) fpath(fid uint32) string {
return vlogFilePath(vlog.dirPath, fid)
}
func (vlog *valueLog) openOrCreateFiles() error {
files, err := ioutil.ReadDir(vlog.dirPath)
if err != nil {
return errors.Wrapf(err, "Error while opening value log")
}
found := make(map[uint64]struct{})
var maxFid uint32 // Beware len(files) == 0 case, this starts at 0.
for _, file := range files {
if !strings.HasSuffix(file.Name(), ".vlog") {
continue
}
fsz := len(file.Name())
fid, err := strconv.ParseUint(file.Name()[:fsz-5], 10, 32)
if err != nil {
return errors.Wrapf(err, "Error while parsing value log id for file: %q", file.Name())
}
if _, ok := found[fid]; ok {
return errors.Errorf("Found the same value log file twice: %d", fid)
}
found[fid] = struct{}{}
lf := &logFile{fid: uint32(fid), path: vlog.fpath(uint32(fid))}
vlog.filesMap[uint32(fid)] = lf
if uint32(fid) > maxFid {
maxFid = uint32(fid)
}
}
vlog.maxFid = uint32(maxFid)
// Open all previous log files as read only. Open the last log file
// as read write.
for fid, lf := range vlog.filesMap {
if fid == maxFid {
lf.fd, err = y.OpenExistingSyncedFile(vlog.fpath(fid), vlog.opt.SyncWrites)
if err != nil {
return errors.Wrapf(err, "Unable to open value log file as RDWR")
}
} else {
if err := lf.openReadOnly(); err != nil {
return err
}
}
}
// If no files are found, then create a new file.
if len(vlog.filesMap) == 0 {
// We already set vlog.maxFid above
_, err := vlog.createVlogFile(0)
if err != nil {
return err
}
}
return nil
}
func (vlog *valueLog) createVlogFile(fid uint32) (*logFile, error) {
path := vlog.fpath(fid)
lf := &logFile{fid: fid, offset: 0, path: path}
var err error
lf.fd, err = y.CreateSyncedFile(path, vlog.opt.SyncWrites)
if err != nil {
return nil, errors.Wrapf(err, "Unable to create value log file")
}
err = syncDir(vlog.dirPath)
if err != nil {
return nil, errors.Wrapf(err, "Unable to sync value log file dir")
}
vlog.filesLock.Lock()
vlog.filesMap[fid] = lf
vlog.filesLock.Unlock()
return lf, nil
}
func (vlog *valueLog) Open(kv *KV, opt *Options) error {
vlog.dirPath = opt.ValueDir
vlog.opt = *opt
vlog.kv = kv
vlog.filesMap = make(map[uint32]*logFile)
if err := vlog.openOrCreateFiles(); err != nil {
return errors.Wrapf(err, "Unable to open value log")
}
vlog.elog = trace.NewEventLog("Badger", "Valuelog")
return nil
}
func (vlog *valueLog) Close() error {
vlog.elog.Printf("Stopping garbage collection of values.")
defer vlog.elog.Finish()
var err error
for _, f := range vlog.filesMap {
if closeErr := f.fd.Close(); closeErr != nil && err == nil {
err = closeErr
}
}
return err
}
// sortedFids returns the file id's, sorted. Assumes we have shared access to filesMap.
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
}
// Replay replays the value log. The kv provided is only valid for the lifetime of function call.
func (vlog *valueLog) Replay(ptr valuePointer, fn logEntry) error {
fid := ptr.Fid
offset := ptr.Offset + ptr.Len
vlog.elog.Printf("Seeking at value pointer: %+v\n", ptr)
fids := vlog.sortedFids()
for _, id := range fids {
if id < fid {
continue
}
of := offset
if id > fid {
of = 0
}
f := vlog.filesMap[id]
err := f.iterate(of, fn)
if err != nil {
return errors.Wrapf(err, "Unable to replay value log: %q", f.path)
}
}
// Seek to the end to start writing.
var err error
last := vlog.filesMap[vlog.maxFid]
lastOffset, err := last.fd.Seek(0, io.SeekEnd)
last.offset = uint32(lastOffset)
return errors.Wrapf(err, "Unable to seek to end of value log: %q", last.path)
}
type request struct {
// Input values
Entries []*Entry
// Output values and wait group stuff below
Ptrs []valuePointer
Wg sync.WaitGroup
Err error
}
// sync is thread-unsafe and should not be called concurrently with write.
func (vlog *valueLog) sync() error {
if vlog.opt.SyncWrites {
return nil
}
vlog.filesLock.RLock()
if len(vlog.filesMap) == 0 {
vlog.filesLock.RUnlock()
return nil
}
curlf := vlog.filesMap[vlog.maxFid]
curlf.fdLock.RLock()
vlog.filesLock.RUnlock()
dirSyncCh := make(chan error)
go func() { dirSyncCh <- syncDir(vlog.opt.ValueDir) }()
err := curlf.sync()
curlf.fdLock.RUnlock()
dirSyncErr := <-dirSyncCh
if err != nil {
err = dirSyncErr
}
return err
}
// write is thread-unsafe by design and should not be called concurrently.
func (vlog *valueLog) write(reqs []*request) error {
vlog.filesLock.RLock()
curlf := vlog.filesMap[vlog.maxFid]
vlog.filesLock.RUnlock()
toDisk := func() error {
if vlog.buf.Len() == 0 {
return nil
}
vlog.elog.Printf("Flushing %d blocks of total size: %d", len(reqs), vlog.buf.Len())
n, err := curlf.fd.Write(vlog.buf.Bytes())
if err != nil {
return errors.Wrapf(err, "Unable to write to value log file: %q", curlf.path)
}
y.NumWrites.Add(1)
y.NumBytesWritten.Add(int64(n))
vlog.elog.Printf("Done")
curlf.offset += uint32(n)
vlog.buf.Reset()
if curlf.offset > uint32(vlog.opt.ValueLogFileSize) {
var err error
if err = curlf.doneWriting(); err != nil {
return err
}
newid := atomic.AddUint32(&vlog.maxFid, 1)
y.AssertTruef(newid < 1<<16, "newid will overflow uint16: %v", newid)
newlf, err := vlog.createVlogFile(newid)
if err != nil {
return err
}
curlf = newlf
}
return nil
}
for i := range reqs {
b := reqs[i]
b.Ptrs = b.Ptrs[:0]
for j := range b.Entries {
e := b.Entries[j]
var p valuePointer
if !vlog.opt.SyncWrites && len(e.Value) < vlog.opt.ValueThreshold {
// No need to write to value log.
b.Ptrs = append(b.Ptrs, p)
continue
}
p.Fid = curlf.fid
p.Offset = curlf.offset + uint32(vlog.buf.Len()) // Use the offset including buffer length so far.
plen, err := encodeEntry(e, &vlog.buf) // Now encode the entry into buffer.
if err != nil {
return err
}
p.Len = uint32(plen)
b.Ptrs = append(b.Ptrs, p)
if p.Offset > uint32(vlog.opt.ValueLogFileSize) {
if err := toDisk(); err != nil {
return err
}
}
}
}
return toDisk()
// Acquire mutex locks around this manipulation, so that the reads don't try to use
// an invalid file descriptor.
}
// Gets the logFile and acquires an RLock() on it too. You must call RUnlock on the file (if
// non-nil).
func (vlog *valueLog) getFileRLocked(fid uint32) (*logFile, error) {
vlog.filesLock.RLock()
defer vlog.filesLock.RUnlock()
ret, ok := vlog.filesMap[fid]
if !ok {
return nil, ErrCorrupt
}
ret.fdLock.RLock()
return ret, nil
}
// Read reads the value log at a given location.
func (vlog *valueLog) Read(p valuePointer, s *y.Slice) (e Entry, err error) {
lf, err := vlog.getFileRLocked(p.Fid)
if err != nil {
return e, err
}
defer lf.fdLock.RUnlock()
if s == nil {
s = new(y.Slice)
}
buf := s.Resize(int(p.Len))
if err := lf.read(buf, int64(p.Offset)); err != nil {
return e, err
}
var h header
h.Decode(buf)
n := uint32(headerBufSize)
e.Key = buf[n : n+h.klen]
n += h.klen
e.Meta = h.meta
e.UserMeta = h.userMeta
e.casCounter = h.casCounter
e.CASCounterCheck = h.casCounterCheck
e.Value = buf[n : n+h.vlen]
return e, nil
}
func (vlog *valueLog) runGCInLoop(lc *y.LevelCloser) {
defer lc.Done()
if vlog.opt.ValueGCThreshold == 0.0 {
return
}
tick := time.NewTicker(vlog.opt.ValueGCRunInterval)
for {
select {
case <-lc.HasBeenClosed():
return
case <-tick.C:
vlog.doRunGC()
}
}
}
func (vlog *valueLog) pickLog() *logFile {
vlog.filesLock.RLock()
defer vlog.filesLock.RUnlock()
if len(vlog.filesMap) <= 1 {
return nil
}
fids := vlog.sortedFids()
// This file shouldn't be being written to.
idx := rand.Intn(len(fids))
if idx > 0 {
idx = rand.Intn(idx) // Another level of rand to favor smaller fids.
}
return vlog.filesMap[fids[idx]]
}
func (vlog *valueLog) doRunGC() error {
lf := vlog.pickLog()
if lf == nil {
return nil
}
type reason struct {
total float64
keep float64
discard float64
}
var r reason
var window = 100.0
count := 0
// Pick a random start point for the log.
skipFirstM := float64(rand.Intn(int(vlog.opt.ValueLogFileSize/M))) - window
var skipped float64
start := time.Now()
y.AssertTrue(vlog.kv != nil)
err := lf.iterate(0, func(e Entry, vp valuePointer) error {
esz := float64(vp.Len) / (1 << 20) // in MBs. +4 for the CAS stuff.
skipped += esz
if skipped < skipFirstM {
return nil
}
count++
if count%100 == 0 {
time.Sleep(time.Millisecond)
}
r.total += esz
if r.total > window {
return errStop
}
if time.Since(start) > 10*time.Second {
return errStop
}
vs, err := vlog.kv.get(e.Key)
if err != nil {
return err
}
if (vs.Meta & BitDelete) > 0 {
// Key has been deleted. Discard.
r.discard += esz
return nil
}
if (vs.Meta & BitValuePointer) == 0 {
// Value is stored alongside key. Discard.
r.discard += esz
return nil
}
// Value is still present in value log.
y.AssertTrue(len(vs.Value) > 0)
vp.Decode(vs.Value)
if vp.Fid > lf.fid {
// Value is present in a later log. Discard.
r.discard += esz
return nil
}
if vp.Offset > e.offset {
// Value is present in a later offset, but in the same log.
r.discard += esz
return nil
}
if vp.Fid == lf.fid && vp.Offset == e.offset {
// This is still the active entry. This would need to be rewritten.
r.keep += esz
} else {
vlog.elog.Printf("Reason=%+v\n", r)
ne, err := vlog.Read(vp, nil)
if err != nil {
return errStop
}
ne.offset = vp.Offset
if ne.casCounter == e.casCounter {
ne.print("Latest Entry in LSM")
e.print("Latest Entry in Log")
return errors.Errorf(
"This shouldn't happen. Latest Pointer:%+v. Meta:%v.", vp, vs.Meta)
}
}
return nil
})
if err != nil {
vlog.elog.Errorf("Error while iterating for RunGC: %v", err)
return err
}
vlog.elog.Printf("Fid: %d Data status=%+v\n", lf.fid, r)
if r.total < 10.0 || r.discard < vlog.opt.ValueGCThreshold*r.total {
vlog.elog.Printf("Skipping GC on fid: %d\n\n", lf.fid)
return nil
}
vlog.elog.Printf("REWRITING VLOG %d\n", lf.fid)
if err = vlog.rewrite(lf); err != nil {
return err
}
vlog.elog.Printf("Done rewriting.")
return nil
}