Skip to content

Commit 647faa6

Browse files
committed
db: find strict overlap with excise span
`Version.Overlaps()` returns all "indirect" overlaps in L0. This is not what we want when we look for files to excise. This change replaces `Overlaps()` with using `LevelSlice.Overlaps()` on each sublevel/level.
1 parent e5ebbdf commit 647faa6

File tree

5 files changed

+32
-27
lines changed

5 files changed

+32
-27
lines changed

compaction.go

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1348,23 +1348,21 @@ func (d *DB) runIngestFlush(c *compaction) (*manifest.VersionEdit, error) {
13481348
}
13491349
if ingestFlushable.exciseSpan.Valid() {
13501350
// Iterate through all levels and find files that intersect with exciseSpan.
1351-
for l := range c.version.Levels {
1352-
overlaps := c.version.Overlaps(l, base.UserKeyBoundsEndExclusive(ingestFlushable.exciseSpan.Start, ingestFlushable.exciseSpan.End))
1353-
for m := range overlaps.All() {
1354-
newFiles, err := d.excise(context.TODO(), ingestFlushable.exciseSpan.UserKeyBounds(), m, ve, l)
1351+
for layer, ls := range c.version.AllLevelsAndSublevels() {
1352+
for m := range ls.Overlaps(d.cmp, ingestFlushable.exciseSpan.UserKeyBounds()).All() {
1353+
newFiles, err := d.excise(context.TODO(), ingestFlushable.exciseSpan.UserKeyBounds(), m, ve, layer.Level())
13551354
if err != nil {
13561355
return nil, err
13571356
}
13581357

13591358
if _, ok := ve.DeletedTables[deletedFileEntry{
1360-
Level: l,
1359+
Level: layer.Level(),
13611360
FileNum: m.FileNum,
13621361
}]; !ok {
1363-
// We did not excise this file.
1364-
continue
1362+
panic("did not excise file that overlaps with the excise span")
13651363
}
13661364
replacedFiles[m.FileNum] = newFiles
1367-
updateLevelMetricsOnExcise(m, l, newFiles)
1365+
updateLevelMetricsOnExcise(m, layer.Level(), newFiles)
13681366
}
13691367
}
13701368
}

download.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -248,13 +248,14 @@ func (d *DB) newDownloadSpanTask(vers *version, sp DownloadSpan) (_ *downloadSpa
248248
// [sp.StartKey, sp.EndKey). Expand the bounds to the left so that we
249249
// include the start keys of any external sstables that overlap with
250250
// sp.StartKey.
251-
vers.IterAllLevelsAndSublevels(func(iter manifest.LevelIterator, level manifest.Layer) {
251+
for _, ls := range vers.AllLevelsAndSublevels() {
252+
iter := ls.Iter()
252253
if f := iter.SeekGE(d.cmp, sp.StartKey); f != nil &&
253254
objstorage.IsExternalTable(d.objProvider, f.FileBacking.DiskFileNum) &&
254255
d.cmp(f.Smallest.UserKey, bounds.Start) < 0 {
255256
bounds.Start = f.Smallest.UserKey
256257
}
257-
})
258+
}
258259
startCursor := downloadCursor{
259260
level: 0,
260261
key: bounds.Start,

ingest.go

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2102,22 +2102,21 @@ func (d *DB) ingestApply(
21022102
// see if any new compactions are conflicting with our chosen target levels
21032103
// for files, and if they are, we should signal those compactions to error
21042104
// out.
2105-
for level := range current.Levels {
2106-
for m := range current.Overlaps(level, exciseSpan.UserKeyBounds()).All() {
2107-
newFiles, err := d.excise(ctx, exciseSpan.UserKeyBounds(), m, ve, level)
2105+
for layer, ls := range current.AllLevelsAndSublevels() {
2106+
for m := range ls.Overlaps(d.cmp, exciseSpan.UserKeyBounds()).All() {
2107+
newFiles, err := d.excise(ctx, exciseSpan.UserKeyBounds(), m, ve, layer.Level())
21082108
if err != nil {
21092109
return nil, err
21102110
}
21112111

21122112
if _, ok := ve.DeletedTables[deletedFileEntry{
2113-
Level: level,
2113+
Level: layer.Level(),
21142114
FileNum: m.FileNum,
21152115
}]; !ok {
2116-
// We did not excise this file.
2117-
continue
2116+
panic("did not excise file that overlaps with the excise span")
21182117
}
21192118
replacedFiles[m.FileNum] = newFiles
2120-
updateLevelMetricsOnExcise(m, level, newFiles)
2119+
updateLevelMetricsOnExcise(m, layer.Level(), newFiles)
21212120
}
21222121
}
21232122
}

ingest_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -892,14 +892,15 @@ func TestExcise(t *testing.T) {
892892
d.mu.Unlock()
893893
current := d.mu.versions.currentVersion()
894894

895-
current.IterAllLevelsAndSublevels(func(iter manifest.LevelIterator, l manifest.Layer) {
895+
for l, ls := range current.AllLevelsAndSublevels() {
896+
iter := ls.Iter()
896897
for m := iter.SeekGE(d.cmp, exciseSpan.Start); m != nil && d.cmp(m.Smallest.UserKey, exciseSpan.End) < 0; m = iter.Next() {
897898
_, err := d.excise(context.Background(), exciseSpan.UserKeyBounds(), m, ve, l.Level())
898899
if err != nil {
899900
td.Fatalf(t, "error when excising %s: %s", m.FileNum, err.Error())
900901
}
901902
}
902-
})
903+
}
903904

904905
d.mu.Lock()
905906
d.mu.versions.logUnlock()

internal/manifest/version.go

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1653,14 +1653,20 @@ func (v *Version) Overlaps(level int, bounds base.UserKeyBounds) LevelSlice {
16531653
return v.Levels[level].Slice().Overlaps(v.cmp.Compare, bounds)
16541654
}
16551655

1656-
// IterAllLevelsAndSublevels calls fn with an iterator for each L0 sublevel
1657-
// (from top to bottom), then once for each level below L0.
1658-
func (v *Version) IterAllLevelsAndSublevels(fn func(it LevelIterator, level Layer)) {
1659-
for sublevel := len(v.L0SublevelFiles) - 1; sublevel >= 0; sublevel-- {
1660-
fn(v.L0SublevelFiles[sublevel].Iter(), L0Sublevel(sublevel))
1661-
}
1662-
for level := 1; level < NumLevels; level++ {
1663-
fn(v.Levels[level].Iter(), Level(level))
1656+
// AllLevelsAndSublevels returns an iterator that produces a Layer, LevelSlice
1657+
// pair for each L0 sublevel (from top to bottom) and each level below L0.
1658+
func (v *Version) AllLevelsAndSublevels() iter.Seq2[Layer, LevelSlice] {
1659+
return func(yield func(Layer, LevelSlice) bool) {
1660+
for sublevel := len(v.L0SublevelFiles) - 1; sublevel >= 0; sublevel-- {
1661+
if !yield(L0Sublevel(sublevel), v.L0SublevelFiles[sublevel]) {
1662+
return
1663+
}
1664+
}
1665+
for level := 1; level < NumLevels; level++ {
1666+
if !yield(Level(level), v.Levels[level].Slice()) {
1667+
return
1668+
}
1669+
}
16641670
}
16651671
}
16661672

0 commit comments

Comments
 (0)