Skip to content

Commit

Permalink
compaction: avoid arbitrary grandparent overlap
Browse files Browse the repository at this point in the history
If the compaction output splitters suggest a user key less than the next user
key, use the the splitters' suggestion as the split key. This avoids arbitrary
overlap with grandparents if the next key is far beyond the grandparent limit.
  • Loading branch information
jbowens committed Jan 31, 2022
1 parent b55361d commit 3ea1111
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 28 deletions.
30 changes: 10 additions & 20 deletions compaction.go
Original file line number Diff line number Diff line change
Expand Up @@ -2330,28 +2330,18 @@ func (d *DB) runCompaction(

// A splitter requested a split, and we're ready to finish the output.
// We need to choose the key at which to split any pending range
// tombstones.
var splitKey []byte
switch {
case key != nil:
// We hit the size, grandparent, or L0 limit for the sstable.
// The next key either has a greater user key than the previous
// key, or if not, the previous key must not have had a zero
// sequence number.

// TODO(jackson): If we hit the grandparent limit, the next
// grandparent's smallest key may be less than the current key.
// Splitting at the current key will cause this output to overlap
// a potentially unbounded number of grandparents.
// tombstones. There are two options:
// 1. splitterSuggestion — The key suggested by the splitter. This key
// is guaranteed to be greater than the last key written to the
// current output.
// 2. key.UserKey — the first key of the next sstable output. This user
// key is also guaranteed to be greater than the last user key
// written to the current output (see userKeyChangeSplitter).
// Use whichever is smaller.
splitKey := splitterSuggestion
if key != nil && (splitKey == nil || c.cmp(splitKey, key.UserKey) > 0) {
splitKey = key.UserKey
case key == nil:
// NB: Because of the userKeyChangeSplitter, `splitterSuggestion`
// must be > any key previously added to the current output sstable.
splitKey = splitterSuggestion
default:
return nil, nil, errors.New("pebble: not reached")
}

if err := finishOutput(splitKey); err != nil {
return nil, pendingOutputs, err
}
Expand Down
8 changes: 4 additions & 4 deletions testdata/manual_compaction
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ L3
compact a-e L1
----
2:
000009:[a#3,SET-g#72057594037927935,RANGEDEL]
000010:[h#3,SET-h#3,SET]
000009:[a#3,SET-c#72057594037927935,RANGEDEL]
000010:[c#2,RANGEDEL-h#3,SET]
3:
000006:[a#0,SET-b#0,SET]
000007:[c#0,SET-d#0,SET]
Expand Down Expand Up @@ -284,8 +284,8 @@ L3
compact a-c L1
----
2:
000009:[a#3,RANGEDEL-c#72057594037927935,RANGEDEL]
000010:[c#2,SET-c#2,SET]
000009:[a#3,RANGEDEL-b#72057594037927935,RANGEDEL]
000010:[b#3,RANGEDEL-c#2,SET]
3:
000006:[b#2,SET-b#2,SET]
000007:[b#1,SET-b#1,SET]
Expand Down
8 changes: 4 additions & 4 deletions testdata/manual_compaction_set_with_del
Original file line number Diff line number Diff line change
Expand Up @@ -180,8 +180,8 @@ L3
compact a-e L1
----
2:
000009:[a#3,SETWITHDEL-g#72057594037927935,RANGEDEL]
000010:[h#3,SET-h#3,SET]
000009:[a#3,SETWITHDEL-c#72057594037927935,RANGEDEL]
000010:[c#2,RANGEDEL-h#3,SET]
3:
000006:[a#0,SET-b#0,SET]
000007:[c#0,SET-d#0,SET]
Expand Down Expand Up @@ -284,8 +284,8 @@ L3
compact a-c L1
----
2:
000009:[a#3,RANGEDEL-c#72057594037927935,RANGEDEL]
000010:[c#2,SET-c#2,SET]
000009:[a#3,RANGEDEL-b#72057594037927935,RANGEDEL]
000010:[b#3,RANGEDEL-c#2,SET]
3:
000006:[b#2,SET-b#2,SET]
000007:[b#1,SET-b#1,SET]
Expand Down

0 comments on commit 3ea1111

Please sign in to comment.