forked from cockroachdb/pebble
-
Notifications
You must be signed in to change notification settings - Fork 0
/
compaction.go
1350 lines (1228 loc) · 41.3 KB
/
compaction.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
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright 2013 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 pebble
import (
"bytes"
"context"
"errors"
"fmt"
"math"
"os"
"sort"
"sync/atomic"
"time"
"unsafe"
"github.com/petermattis/pebble/internal/base"
"github.com/petermattis/pebble/internal/rangedel"
"github.com/petermattis/pebble/sstable"
"github.com/petermattis/pebble/vfs"
)
var errEmptyTable = errors.New("pebble: empty table")
// expandedCompactionByteSizeLimit is the maximum number of bytes in all
// compacted files. We avoid expanding the lower level file set of a compaction
// if it would make the total compaction cover more than this many bytes.
func expandedCompactionByteSizeLimit(opts *Options, level int) uint64 {
return uint64(25 * opts.Level(level).TargetFileSize)
}
// maxGrandparentOverlapBytes is the maximum bytes of overlap with level+2
// before we stop building a single file in a level to level+1 compaction.
func maxGrandparentOverlapBytes(opts *Options, level int) uint64 {
return uint64(10 * opts.Level(level).TargetFileSize)
}
// compaction is a table compaction from one level to the next, starting from a
// given version.
type compaction struct {
cmp Compare
version *version
// startLevel is the level that is being compacted. Inputs from startLevel
// and outputLevel will be merged to produce a set of outputLevel files.
startLevel int
// outputLevel is the level that files are being produced in. outputLevel is
// equal to startLevel+1 except when startLevel is 0 in which case it is
// equal to compactionPicker.baseLevel.
outputLevel int
// maxOutputFileSize is the maximum size of an individual table created
// during compaction.
maxOutputFileSize uint64
// maxOverlapBytes is the maximum number of bytes of overlap allowed for a
// single output table with the tables in the grandparent level.
maxOverlapBytes uint64
// maxExpandedBytes is the maximum size of an expanded compaction. If growing
// a compaction results in a larger size, the original compaction is used
// instead.
maxExpandedBytes uint64
// disableRangeTombstoneElision disables elision of range tombstones. Used by
// tests to allow range tombstones to be added to tables where they would
// otherwise be elided.
disableRangeTombstoneElision bool
// flushing contains the flushables (aka memtables) that are being flushed.
flushing []flushable
// bytesIterated contains the number of bytes that have been flushed/compacted.
bytesIterated uint64
// inputs are the tables to be compacted.
inputs [2][]fileMetadata
// grandparents are the tables in level+2 that overlap with the files being
// compacted. Used to determine output table boundaries.
grandparents []fileMetadata
overlappedBytes uint64 // bytes of overlap with grandparent tables
seenKey bool // some output key has been seen
}
func newCompaction(opts *Options, cur *version, startLevel, baseLevel int) *compaction {
if startLevel > 0 && startLevel < baseLevel {
panic(fmt.Sprintf("invalid compaction: start level %d should be empty (base level %d)",
startLevel, baseLevel))
}
outputLevel := startLevel + 1
if startLevel == 0 {
outputLevel = baseLevel
}
if outputLevel >= numLevels-1 {
outputLevel = numLevels - 1
}
// Output level is in the range [baseLevel,numLevels]. For the purpose of
// determining the target output file size, overlap bytes, and expanded
// bytes, we want to adjust the range to [1,numLevels].
adjustedOutputLevel := 1 + outputLevel - baseLevel
return &compaction{
cmp: opts.Comparer.Compare,
version: cur,
startLevel: startLevel,
outputLevel: outputLevel,
maxOutputFileSize: uint64(opts.Level(adjustedOutputLevel).TargetFileSize),
maxOverlapBytes: maxGrandparentOverlapBytes(opts, adjustedOutputLevel),
maxExpandedBytes: expandedCompactionByteSizeLimit(opts, adjustedOutputLevel),
}
}
func newFlush(opts *Options, cur *version, baseLevel int, flushing []flushable) *compaction {
c := &compaction{
cmp: opts.Comparer.Compare,
version: cur,
startLevel: -1,
outputLevel: 0,
maxOutputFileSize: math.MaxUint64,
maxOverlapBytes: math.MaxUint64,
maxExpandedBytes: math.MaxUint64,
flushing: flushing,
}
// TODO(peter): When we allow flushing to create multiple tables we'll want
// to choose sstable boundaries based on the grandparents. But for now we
// want to create a single table during flushing so this is all commented
// out.
if false {
c.maxOutputFileSize = uint64(opts.Level(0).TargetFileSize)
c.maxOverlapBytes = maxGrandparentOverlapBytes(opts, 0)
c.maxExpandedBytes = expandedCompactionByteSizeLimit(opts, 0)
var smallest InternalKey
var largest InternalKey
smallestSet, largestSet := false, false
updatePointBounds := func(iter internalIterator) {
if key, _ := iter.First(); key != nil {
if !smallestSet ||
base.InternalCompare(c.cmp, smallest, *key) > 0 {
smallestSet = true
smallest = key.Clone()
}
}
if key, _ := iter.Last(); key != nil {
if !largestSet ||
base.InternalCompare(c.cmp, largest, *key) < 0 {
largestSet = true
largest = key.Clone()
}
}
}
updateRangeBounds := func(iter internalIterator) {
if key, _ := iter.First(); key != nil {
if !smallestSet ||
base.InternalCompare(c.cmp, smallest, *key) > 0 {
smallestSet = true
smallest = key.Clone()
}
}
}
for i := range flushing {
f := flushing[i]
updatePointBounds(f.newIter(nil))
if rangeDelIter := f.newRangeDelIter(nil); rangeDelIter != nil {
updateRangeBounds(rangeDelIter)
}
}
c.grandparents = c.version.overlaps(baseLevel, c.cmp, smallest.UserKey, largest.UserKey)
}
return c
}
// setupOtherInputs fills in the rest of the compaction inputs, regardless of
// whether the compaction was automatically scheduled or user initiated.
func (c *compaction) setupOtherInputs() {
c.inputs[0] = c.expandInputs(c.inputs[0])
smallest0, largest0 := ikeyRange(c.cmp, c.inputs[0], nil)
c.inputs[1] = c.version.overlaps(c.outputLevel, c.cmp, smallest0.UserKey, largest0.UserKey)
smallest01, largest01 := ikeyRange(c.cmp, c.inputs[0], c.inputs[1])
// Grow the inputs if it doesn't affect the number of level+1 files.
if c.grow(smallest01, largest01) {
smallest01, largest01 = ikeyRange(c.cmp, c.inputs[0], c.inputs[1])
}
// Compute the set of outputLevel+1 files that overlap this compaction.
if c.outputLevel+1 < numLevels {
c.grandparents = c.version.overlaps(c.outputLevel+1, c.cmp, smallest01.UserKey, largest01.UserKey)
}
}
// expandInputs expands the files in inputs[0] in order to maintain the
// invariant that the versions of keys at level+1 are older than the versions
// of keys at level. This is achieved by adding tables to the right of the
// current input tables such that the rightmost table has a "clean cut". A
// clean cut is either a change in user keys, or
func (c *compaction) expandInputs(inputs []fileMetadata) []fileMetadata {
if c.startLevel == 0 {
// We already call version.overlaps for L0 and that call guarantees that we
// get a "clean cut".
return inputs
}
files := c.version.files[c.startLevel]
// Pointer arithmetic to figure out the index if inputs[0] with
// files[0]. This requires that the inputs slice is a sub-slice of
// files. This is true for non-L0 files returned from version.overlaps.
if uintptr(unsafe.Pointer(&inputs[0])) < uintptr(unsafe.Pointer(&files[0])) {
panic("pebble: invalid input slice")
}
start := int((uintptr(unsafe.Pointer(&inputs[0])) -
uintptr(unsafe.Pointer(&files[0]))) / unsafe.Sizeof(inputs[0]))
if start >= len(files) {
panic("pebble: invalid input slice")
}
end := start + len(inputs)
for ; end < len(files); end++ {
cur := &files[end-1]
next := &files[end]
if c.cmp(cur.largest.UserKey, next.smallest.UserKey) < 0 {
break
}
if cur.largest.Trailer == InternalKeyRangeDeleteSentinel {
// The range deletion sentinel key is set for the largest key in a table
// when a range deletion tombstone straddles a table. It isn't necessary
// to include the next table in the compaction as cur.largest.UserKey
// does not actually exist in the table.
break
}
// cur.largest.UserKey == next.largest.UserKey, so we need to include next
// in the compaction.
}
return files[start:end]
}
// grow grows the number of inputs at c.level without changing the number of
// c.level+1 files in the compaction, and returns whether the inputs grew. sm
// and la are the smallest and largest InternalKeys in all of the inputs.
func (c *compaction) grow(sm, la InternalKey) bool {
if len(c.inputs[1]) == 0 {
return false
}
grow0 := c.version.overlaps(c.startLevel, c.cmp, sm.UserKey, la.UserKey)
grow0 = c.expandInputs(grow0)
if len(grow0) <= len(c.inputs[0]) {
return false
}
if totalSize(grow0)+totalSize(c.inputs[1]) >= c.maxExpandedBytes {
return false
}
sm1, la1 := ikeyRange(c.cmp, grow0, nil)
grow1 := c.version.overlaps(c.outputLevel, c.cmp, sm1.UserKey, la1.UserKey)
if len(grow1) != len(c.inputs[1]) {
return false
}
c.inputs[0] = grow0
c.inputs[1] = grow1
return true
}
func (c *compaction) trivialMove() bool {
if len(c.flushing) != 0 {
return false
}
// Check for a trivial move of one table from one level to the next. We avoid
// such a move if there is lots of overlapping grandparent data. Otherwise,
// the move could create a parent file that will require a very expensive
// merge later on.
if len(c.inputs[0]) == 1 && len(c.inputs[1]) == 0 &&
totalSize(c.grandparents) <= c.maxOverlapBytes {
return true
}
return false
}
// shouldStopBefore returns true if the output to the current table should be
// finished and a new table started before adding the specified key. This is
// done in order to prevent a table at level N from overlapping too much data
// at level N+1. We want to avoid such large overlaps because they translate
// into large compactions. The current heuristic stops output of a table if the
// addition of another key would cause the table to overlap more than 10x the
// target file size at level N. See maxGrandparentOverlapBytes.
//
// TODO(peter): Stopping compaction output in the middle of a user-key creates
// 2 sstables that need to be compacted together as an "atomic compaction
// unit". This is unfortunate as it removes the benefit of stopping output to
// an sstable in order to prevent a large compaction with the next level. Seems
// better to adjust shouldStopBefore to not stop output in the middle of a
// user-key. Perhaps this isn't a problem if the compaction picking heuristics
// always pick the right (older) sibling for compaction first.
func (c *compaction) shouldStopBefore(key InternalKey) bool {
for len(c.grandparents) > 0 {
g := &c.grandparents[0]
if base.InternalCompare(c.cmp, key, g.largest) <= 0 {
break
}
if c.seenKey {
c.overlappedBytes += g.size
}
c.grandparents = c.grandparents[1:]
}
c.seenKey = true
if c.overlappedBytes > c.maxOverlapBytes {
c.overlappedBytes = 0
return true
}
return false
}
// allowZeroSeqNum returns true if seqnum's can be zeroed if there are no
// snapshots requiring them to be kept. It performs this determination by
// looking for an sstable which overlaps the bounds of the compaction at a
// lower level in the LSM.
func (c *compaction) allowZeroSeqNum(iter internalIterator) bool {
if len(c.flushing) != 0 {
if len(c.version.files[0]) > 0 {
// We can only allow zeroing of seqnum for L0 tables if no other L0 tables
// exist. Otherwise we may violate the invariant that L0 tables are ordered
// by increasing seqnum. This could be relaxed with a bit more intelligence
// in how a new L0 table is merged into the existing set of L0 tables.
return false
}
lower, _ := iter.First()
upper, _ := iter.Last()
if lower == nil || upper == nil {
return false
}
return c.elideRangeTombstone(lower.UserKey, upper.UserKey)
}
var lower, upper []byte
for i := range c.inputs {
files := c.inputs[i]
for j := range files {
f := &files[j]
if lower == nil || c.cmp(lower, f.smallest.UserKey) > 0 {
lower = f.smallest.UserKey
}
if upper == nil || c.cmp(upper, f.largest.UserKey) < 0 {
upper = f.largest.UserKey
}
}
}
// [lower,upper] now cover the bounds of the compaction inputs. Check to see
// if those bounds overlap an sstable at a lower level.
return c.elideRangeTombstone(lower, upper)
}
// elideTombstone returns true if it is ok to elide a tombstone for the
// specified key. A return value of true guarantees that there are no key/value
// pairs at c.level+2 or higher that possibly contain the specified user key.
func (c *compaction) elideTombstone(key []byte) bool {
if len(c.flushing) != 0 {
return false
}
level := c.outputLevel + 1
if c.outputLevel == 0 {
// Level 0 can contain overlapping sstables so we need to check it for
// overlaps.
level = 0
}
// TODO(peter): this can be faster if key is always increasing between
// successive elideTombstones calls and we can keep some state in between
// calls.
for ; level < numLevels; level++ {
for _, f := range c.version.files[level] {
if c.cmp(key, f.largest.UserKey) <= 0 {
if c.cmp(key, f.smallest.UserKey) >= 0 {
return false
}
// For levels below level 0, the files within a level are in
// increasing ikey order, so we can break early.
break
}
}
}
return true
}
// elideRangeTombstone returns true if it is ok to elide the specified range
// tombstone. A return value of true guarantees that there are no key/value
// pairs at c.outputLevel+1 or higher that possibly overlap the specified
// tombstone.
func (c *compaction) elideRangeTombstone(start, end []byte) bool {
if c.disableRangeTombstoneElision {
return false
}
level := c.outputLevel + 1
if c.outputLevel == 0 {
// Level 0 can contain overlapping sstables so we need to check it for
// overlaps.
level = 0
}
for ; level < numLevels; level++ {
overlaps := c.version.overlaps(level, c.cmp, start, end)
if len(overlaps) > 0 {
return false
}
}
return true
}
// atomicUnitBounds returns the bounds of the atomic compaction unit containing
// the specified sstable (identified by a pointer to its fileMetadata).
func (c *compaction) atomicUnitBounds(f *fileMetadata) (lower, upper []byte) {
for i := range c.inputs {
files := c.inputs[i]
for j := range files {
if f == &files[j] {
lowerBound := f.smallest.UserKey
for k := j; k > 0; k-- {
cur := &files[k]
prev := &files[k-1]
if c.cmp(prev.largest.UserKey, cur.smallest.UserKey) < 0 {
break
}
if prev.largest.Trailer == InternalKeyRangeDeleteSentinel {
// The range deletion sentinel key is set for the largest key in a
// table when a range deletion tombstone straddles a table. It
// isn't necessary to include the next table in the atomic
// compaction unit as cur.largest.UserKey does not actually exist
// in the table.
break
}
lowerBound = prev.smallest.UserKey
}
upperBound := f.largest.UserKey
for k := j + 1; k < len(files); k++ {
cur := &files[k-1]
next := &files[k]
if c.cmp(cur.largest.UserKey, next.smallest.UserKey) < 0 {
break
}
if cur.largest.Trailer == InternalKeyRangeDeleteSentinel {
// The range deletion sentinel key is set for the largest key in a
// table when a range deletion tombstone straddles a table. It
// isn't necessary to include the next table in the atomic
// compaction unit as cur.largest.UserKey does not actually exist
// in the table.
break
}
// cur.largest.UserKey == next.largest.UserKey, so next is part of
// the atomic compaction unit.
upperBound = next.largest.UserKey
}
return lowerBound, upperBound
}
}
}
return nil, nil
}
// newInputIter returns an iterator over all the input tables in a compaction.
func (c *compaction) newInputIter(
newIters tableNewIters,
) (_ internalIterator, retErr error) {
if len(c.flushing) != 0 {
if len(c.flushing) == 1 {
f := c.flushing[0]
iter := f.newFlushIter(nil, &c.bytesIterated)
if rangeDelIter := f.newRangeDelIter(nil); rangeDelIter != nil {
return newMergingIter(c.cmp, iter, rangeDelIter), nil
}
return iter, nil
}
iters := make([]internalIterator, 0, 2*len(c.flushing))
for i := range c.flushing {
f := c.flushing[i]
iters = append(iters, f.newFlushIter(nil, &c.bytesIterated))
rangeDelIter := f.newRangeDelIter(nil)
if rangeDelIter != nil {
iters = append(iters, rangeDelIter)
}
}
return newMergingIter(c.cmp, iters...), nil
}
iters := make([]internalIterator, 0, 2*len(c.inputs[0])+1)
defer func() {
if retErr != nil {
for _, iter := range iters {
if iter != nil {
iter.Close()
}
}
}
}()
// In normal operation, levelIter iterates over the point operations in a
// level, and initializes a rangeDelIter pointer for the range deletions in
// each table. During compaction, we want to iterate over the merged view of
// point operations and range deletions. In order to do this we create two
// levelIters per level, one which iterates over the point operations, and
// one which iterates over the range deletions. These two iterators are
// combined with a mergingIter.
newRangeDelIter := func(
f *fileMetadata, _ *IterOptions, bytesIterated *uint64,
) (internalIterator, internalIterator, error) {
iter, rangeDelIter, err := newIters(f, nil /* iter options */, &c.bytesIterated)
if err == nil {
// TODO(peter): It is mildly wasteful to open the point iterator only to
// immediately close it. One way to solve this would be to add new
// methods to tableCache for creating point and range-deletion iterators
// independently. We'd only want to use those methods here,
// though. Doesn't seem worth the hassle in the near term.
if err = iter.Close(); err != nil {
rangeDelIter.Close()
rangeDelIter = nil
}
}
if rangeDelIter != nil {
// Truncate the range tombstones returned by the iterator to the upper
// bound of the atomic compaction unit.
lowerBound, upperBound := c.atomicUnitBounds(f)
if lowerBound != nil || upperBound != nil {
rangeDelIter = rangedel.Truncate(c.cmp, rangeDelIter, lowerBound, upperBound)
}
}
return rangeDelIter, nil, err
}
if c.startLevel != 0 {
iters = append(iters, newLevelIter(nil, c.cmp, newIters, c.inputs[0], &c.bytesIterated))
iters = append(iters, newLevelIter(nil, c.cmp, newRangeDelIter, c.inputs[0], &c.bytesIterated))
} else {
for i := range c.inputs[0] {
f := &c.inputs[0][i]
iter, rangeDelIter, err := newIters(f, nil /* iter options */, &c.bytesIterated)
if err != nil {
return nil, fmt.Errorf("pebble: could not open table %d: %v", f.fileNum, err)
}
iters = append(iters, iter)
if rangeDelIter != nil {
iters = append(iters, rangeDelIter)
}
}
}
iters = append(iters, newLevelIter(nil, c.cmp, newIters, c.inputs[1], &c.bytesIterated))
iters = append(iters, newLevelIter(nil, c.cmp, newRangeDelIter, c.inputs[1], &c.bytesIterated))
return newMergingIter(c.cmp, iters...), nil
}
func (c *compaction) String() string {
if len(c.flushing) != 0 {
return "flush\n"
}
var buf bytes.Buffer
for i := range c.inputs {
level := c.startLevel
if i == 1 {
level = c.outputLevel
}
fmt.Fprintf(&buf, "%d:", level)
for _, f := range c.inputs[i] {
fmt.Fprintf(&buf, " %d:%s-%s", f.fileNum, f.smallest, f.largest)
}
fmt.Fprintf(&buf, "\n")
}
return buf.String()
}
type manualCompaction struct {
level int
outputLevel int
done chan error
start InternalKey
end InternalKey
}
// maybeScheduleFlush schedules a flush if necessary.
//
// d.mu must be held when calling this.
func (d *DB) maybeScheduleFlush() {
if d.mu.compact.flushing || atomic.LoadInt32(&d.closed) != 0 {
return
}
if len(d.mu.mem.queue) <= 1 {
return
}
if !d.mu.mem.queue[0].readyForFlush() {
return
}
d.mu.compact.flushing = true
go d.flush()
}
func (d *DB) flush() {
d.mu.Lock()
defer d.mu.Unlock()
if err := d.flush1(); err != nil {
// TODO(peter): count consecutive flush errors and backoff.
if d.opts.EventListener.BackgroundError != nil {
d.opts.EventListener.BackgroundError(err)
}
}
d.mu.compact.flushing = false
// More flush work may have arrived while we were flushing, so schedule
// another flush if needed.
d.maybeScheduleFlush()
// The flush may have produced too many files in a level, so schedule a
// compaction if needed.
d.maybeScheduleCompaction()
d.mu.compact.cond.Broadcast()
}
// flush runs a compaction that copies the immutable memtables from memory to
// disk.
//
// d.mu must be held when calling this, but the mutex may be dropped and
// re-acquired during the course of this method.
func (d *DB) flush1() error {
var n int
for ; n < len(d.mu.mem.queue)-1; n++ {
if !d.mu.mem.queue[n].readyForFlush() {
break
}
}
if n == 0 {
// None of the immutable memtables are ready for flushing.
return nil
}
c := newFlush(d.opts, d.mu.versions.currentVersion(),
d.mu.versions.picker.baseLevel, d.mu.mem.queue[:n])
jobID := d.mu.nextJobID
d.mu.nextJobID++
if d.opts.EventListener.FlushBegin != nil {
d.opts.EventListener.FlushBegin(FlushInfo{
JobID: jobID,
})
}
ve, pendingOutputs, err := d.runCompaction(c)
if d.opts.EventListener.FlushEnd != nil {
info := FlushInfo{
JobID: jobID,
Err: err,
}
if err == nil {
for i := range ve.newFiles {
e := &ve.newFiles[i]
info.Output = append(info.Output, e.meta.tableInfo(d.dirname))
}
if len(ve.newFiles) == 0 {
info.Err = errEmptyTable
}
}
d.opts.EventListener.FlushEnd(info)
}
if err != nil {
return err
}
// The flush succeeded or it produced an empty sstable. In either case we
// want to bump the log number.
ve.logNumber, _ = d.mu.mem.queue[n].logInfo()
metrics := ve.metrics[0]
for i := 0; i < n; i++ {
_, size := d.mu.mem.queue[i].logInfo()
metrics.BytesIn += size
}
err = d.mu.versions.logAndApply(jobID, ve, d.dataDir)
for _, fileNum := range pendingOutputs {
if _, ok := d.mu.compact.pendingOutputs[fileNum]; !ok {
panic("pebble: expected pending output not present")
}
delete(d.mu.compact.pendingOutputs, fileNum)
}
if err != nil {
return err
}
// Refresh bytes flushed count.
atomic.StoreUint64(&d.bytesFlushed, 0)
flushed := d.mu.mem.queue[:n]
d.mu.mem.queue = d.mu.mem.queue[n:]
d.updateReadStateLocked()
d.deleteObsoleteFiles(jobID)
// Mark all the memtables we flushed as flushed. Note that we do this last so
// that a synchronous call to DB.Flush() will not return until the deletion
// of obsolete files from this job have completed. This makes testing easier
// and provides similar behavior to manual compactions where the compaction
// is not marked as completed until the deletion of obsolete files job has
// completed.
for i := range flushed {
close(flushed[i].flushed())
}
return nil
}
// maybeScheduleCompaction schedules a compaction if necessary.
//
// d.mu must be held when calling this.
func (d *DB) maybeScheduleCompaction() {
if d.mu.compact.compacting || atomic.LoadInt32(&d.closed) != 0 {
return
}
if len(d.mu.compact.manual) > 0 {
d.mu.compact.compacting = true
go d.compact()
return
}
if !d.mu.versions.picker.compactionNeeded() {
// There is no work to be done.
return
}
d.mu.compact.compacting = true
go d.compact()
}
// compact runs one compaction and maybe schedules another call to compact.
func (d *DB) compact() {
d.mu.Lock()
defer d.mu.Unlock()
if err := d.compact1(); err != nil {
// TODO(peter): count consecutive compaction errors and backoff.
if d.opts.EventListener.BackgroundError != nil {
d.opts.EventListener.BackgroundError(err)
}
}
d.mu.compact.compacting = false
// The previous compaction may have produced too many files in a
// level, so reschedule another compaction if needed.
d.maybeScheduleCompaction()
d.mu.compact.cond.Broadcast()
}
// compact1 runs one compaction.
//
// d.mu must be held when calling this, but the mutex may be dropped and
// re-acquired during the course of this method.
func (d *DB) compact1() (err error) {
var c *compaction
if len(d.mu.compact.manual) > 0 {
manual := d.mu.compact.manual[0]
d.mu.compact.manual = d.mu.compact.manual[1:]
c = d.mu.versions.picker.pickManual(d.opts, manual)
defer func() {
manual.done <- err
}()
} else {
c = d.mu.versions.picker.pickAuto(d.opts)
}
if c == nil {
return nil
}
jobID := d.mu.nextJobID
d.mu.nextJobID++
info := CompactionInfo{
JobID: jobID,
}
if d.opts.EventListener.CompactionBegin != nil || d.opts.EventListener.CompactionEnd != nil {
info.Input.Level = c.startLevel
info.Output.Level = c.outputLevel
for i := range c.inputs {
for j := range c.inputs[i] {
m := &c.inputs[i][j]
info.Input.Tables[i] = append(info.Input.Tables[i], m.tableInfo(d.dirname))
}
}
}
if d.opts.EventListener.CompactionBegin != nil {
d.opts.EventListener.CompactionBegin(info)
}
ve, pendingOutputs, err := d.runCompaction(c)
if d.opts.EventListener.CompactionEnd != nil {
info.Err = err
if err == nil {
for i := range ve.newFiles {
e := &ve.newFiles[i]
info.Output.Tables = append(info.Output.Tables, e.meta.tableInfo(d.dirname))
}
}
d.opts.EventListener.CompactionEnd(info)
}
if err != nil {
return err
}
err = d.mu.versions.logAndApply(jobID, ve, d.dataDir)
for _, fileNum := range pendingOutputs {
if _, ok := d.mu.compact.pendingOutputs[fileNum]; !ok {
panic("pebble: expected pending output not present")
}
delete(d.mu.compact.pendingOutputs, fileNum)
}
if err != nil {
return err
}
d.updateReadStateLocked()
d.deleteObsoleteFiles(jobID)
return nil
}
// runCompactions runs a compaction that produces new on-disk tables from
// memtables or old on-disk tables.
//
// d.mu must be held when calling this, but the mutex may be dropped and
// re-acquired during the course of this method.
func (d *DB) runCompaction(c *compaction) (
ve *versionEdit, pendingOutputs []uint64, retErr error,
) {
// Check for a trivial move of one table from one level to the next. We avoid
// such a move if there is lots of overlapping grandparent data. Otherwise,
// the move could create a parent file that will require a very expensive
// merge later on.
if c.trivialMove() {
meta := &c.inputs[0][0]
return &versionEdit{
deletedFiles: map[deletedFileEntry]bool{
deletedFileEntry{level: c.startLevel, fileNum: meta.fileNum}: true,
},
newFiles: []newFileEntry{
{level: c.outputLevel, meta: *meta},
},
metrics: map[int]*LevelMetrics{
c.outputLevel: &LevelMetrics{
BytesMoved: meta.size,
},
},
}, nil, nil
}
defer func() {
if retErr != nil {
for _, fileNum := range pendingOutputs {
delete(d.mu.compact.pendingOutputs, fileNum)
}
pendingOutputs = nil
}
}()
snapshots := d.mu.snapshots.toSlice()
// Release the d.mu lock while doing I/O.
// Note the unusual order: Unlock and then Lock.
d.mu.Unlock()
defer d.mu.Lock()
iiter, err := c.newInputIter(d.newIters)
if err != nil {
return nil, pendingOutputs, err
}
iter := newCompactionIter(c.cmp, d.merge, iiter, snapshots,
c.allowZeroSeqNum(iiter), c.elideTombstone, c.elideRangeTombstone)
var (
filenames []string
tw *sstable.Writer
)
defer func() {
if iter != nil {
retErr = firstError(retErr, iter.Close())
}
if tw != nil {
retErr = firstError(retErr, tw.Close())
}
if retErr != nil {
for _, filename := range filenames {
d.opts.FS.Remove(filename)
}
}
}()
metrics := &LevelMetrics{
BytesIn: totalSize(c.inputs[0]),
BytesRead: totalSize(c.inputs[1]),
}
metrics.BytesRead += metrics.BytesIn
ve = &versionEdit{
deletedFiles: map[deletedFileEntry]bool{},
metrics: map[int]*LevelMetrics{
c.outputLevel: metrics,
},
}
newOutput := func() error {
d.mu.Lock()
fileNum := d.mu.versions.nextFileNum()
d.mu.compact.pendingOutputs[fileNum] = struct{}{}
pendingOutputs = append(pendingOutputs, fileNum)
d.mu.Unlock()
filename := dbFilename(d.dirname, fileTypeTable, fileNum)
file, err := d.opts.FS.Create(filename)
if err != nil {
return err
}
file = vfs.NewSyncingFile(file, vfs.SyncingFileOptions{
BytesPerSync: d.opts.BytesPerSync,
})
filenames = append(filenames, filename)
tw = sstable.NewWriter(file, d.opts, d.opts.Level(c.outputLevel))
ve.newFiles = append(ve.newFiles, newFileEntry{
level: c.outputLevel,
meta: fileMetadata{
fileNum: fileNum,
},
})
return nil
}
finishOutput := func(key InternalKey) error {
// NB: clone the key because the data can be held on to by the call to
// compactionIter.Tombstones via rangedel.Fragmenter.FlushTo.
key = key.Clone()
for _, v := range iter.Tombstones(key.UserKey) {
if tw == nil {
if err := newOutput(); err != nil {
return err
}
}
if err := tw.Add(v.Start, v.End); err != nil {
return err
}
}
if tw == nil {
return nil
}
if err := tw.Close(); err != nil {
tw = nil
return err
}
writerMeta, err := tw.Metadata()
if err != nil {
tw = nil
return err
}
tw = nil
meta := &ve.newFiles[len(ve.newFiles)-1].meta
meta.size = writerMeta.Size
meta.smallestSeqNum = writerMeta.SmallestSeqNum
meta.largestSeqNum = writerMeta.LargestSeqNum
metrics.BytesWritten += meta.size
// The handling of range boundaries is a bit complicated.
if n := len(ve.newFiles); n > 1 {
// This is not the first output. Bound the smallest range key by the
// previous tables largest key.
prevMeta := &ve.newFiles[n-2].meta
if writerMeta.SmallestRange.UserKey != nil &&
d.cmp(writerMeta.SmallestRange.UserKey, prevMeta.largest.UserKey) <= 0 {
// The range boundary user key is less than or equal to the previous
// table's largest key. We need the tables to be key-space partitioned,
// so force the boundary to a key that we know is larger than the
// previous key.
//
// We use seqnum zero since seqnums are in descending order, and our
// goal is to ensure this forged key does not overlap with the previous
// file. `InternalKeyRangeDeleteSentinel` is actually the first key
// kind as key kinds are also in descending order. But, this is OK
// because choosing seqnum zero is already enough to prevent overlap
// (the previous file could not end with a key at seqnum zero if this
// file had a tombstone extending into it).
writerMeta.SmallestRange = base.MakeInternalKey(
prevMeta.largest.UserKey, 0, InternalKeyKindRangeDelete)
}
}
if key.UserKey != nil && writerMeta.LargestRange.UserKey != nil {
if d.cmp(writerMeta.LargestRange.UserKey, key.UserKey) >= 0 {
writerMeta.LargestRange = key
writerMeta.LargestRange.Trailer = InternalKeyRangeDeleteSentinel
}
}
meta.smallest = writerMeta.Smallest(d.cmp)
meta.largest = writerMeta.Largest(d.cmp)
return nil
}