forked from dgraph-io/badger
-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
compaction skip non-overlapping bottom tables #116
Merged
Merged
Changes from 2 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f018d29
compaction skip non-overlapping bottom tables
coocood 7aefd8c
do not move down if there are skipped tables.
coocood 8db6398
fix fillBottomTables
coocood a7b5d3c
use y.CompareKeysWithVer
coocood 5a873e0
do not build empty table and limit the minimum size of skipped table
coocood 38a93da
add assert in replaceTables
coocood File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -371,6 +371,7 @@ func (lc *levelsController) compactBuildTables(level int, cd compactDef, | |
filter = lc.kv.opt.CompactionFilterFactory(level+1, cd.smallest(), cd.biggest()) | ||
guards = filter.Guards() | ||
} | ||
skippedTbls := cd.skippedTbls | ||
|
||
var lastKey, skipKey []byte | ||
var builder *table.Builder | ||
|
@@ -410,6 +411,13 @@ func (lc *levelsController) compactBuildTables(level int, cd compactDef, | |
// Only break if we are on a different key, and have reached capacity. We want | ||
// to ensure that all versions of the key are stored in the same sstable, and | ||
// not divided across multiple tables at the same level. | ||
if len(skippedTbls) > 0 { | ||
skipped := skippedTbls[0] | ||
if bytes.Compare(key, skipped.Biggest()) > 0 { | ||
skippedTbls = skippedTbls[1:] | ||
break | ||
} | ||
} | ||
if shouldFinishFile(key, lastKey, guard, int64(builder.EstimateSize()), lc.kv.opt.MaxTableSize) { | ||
break | ||
} | ||
|
@@ -491,10 +499,7 @@ func (lc *levelsController) compactBuildTables(level int, cd compactDef, | |
log.Error(err) | ||
return | ||
} | ||
|
||
sort.Slice(newTables, func(i, j int) bool { | ||
return y.CompareKeysWithVer(newTables[i].Biggest(), newTables[j].Biggest()) < 0 | ||
}) | ||
sortTables(newTables) | ||
lc.kv.vlog.updateGCStats(discardStats.discardSpaces) | ||
log.Infof("Discard stats: %v", discardStats) | ||
assertTablesOrder(newTables) | ||
|
@@ -522,6 +527,8 @@ type compactDef struct { | |
top []*table.Table | ||
bot []*table.Table | ||
|
||
skippedTbls []*table.Table | ||
|
||
thisRange keyRange | ||
nextRange keyRange | ||
|
||
|
@@ -610,8 +617,7 @@ func (lc *levelsController) fillTablesL0(cd *compactDef) bool { | |
|
||
kr := getKeyRange(cd.top) | ||
left, right := cd.nextLevel.overlappingTables(levelHandlerRLocked{}, kr) | ||
cd.bot = make([]*table.Table, right-left) | ||
copy(cd.bot, cd.nextLevel.tables[left:right]) | ||
lc.fillBottomTables(cd, cd.nextLevel.tables[left:right]) | ||
|
||
if len(cd.bot) == 0 { // the bottom-most level | ||
cd.nextRange = kr | ||
|
@@ -626,6 +632,26 @@ func (lc *levelsController) fillTablesL0(cd *compactDef) bool { | |
return true | ||
} | ||
|
||
func (lc *levelsController) fillBottomTables(cd *compactDef, overlappingTables []*table.Table) { | ||
for _, t := range overlappingTables { | ||
// If none of the top tables contains the range in an overlapping bottom table, | ||
// we can skip it during compaction to reduce write amplification. | ||
var added bool | ||
for _, topTbl := range cd.top { | ||
iter := topTbl.NewIteratorNoRef(false) | ||
iter.Seek(t.Smallest()) | ||
if iter.Valid() && bytes.Compare(iter.Key(), t.Biggest()) < 0 { | ||
cd.bot = append(cd.bot, t) | ||
added = true | ||
break | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. return here and remove the the added boolean flag check. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only one bottom table will be added if we return here. |
||
} | ||
} | ||
if !added { | ||
cd.skippedTbls = append(cd.skippedTbls, t) | ||
} | ||
} | ||
} | ||
|
||
func (lc *levelsController) fillTables(cd *compactDef) bool { | ||
cd.lockLevels() | ||
defer cd.unlockLevels() | ||
|
@@ -656,9 +682,7 @@ func (lc *levelsController) fillTables(cd *compactDef) bool { | |
} | ||
cd.top = []*table.Table{t} | ||
left, right := cd.nextLevel.overlappingTables(levelHandlerRLocked{}, cd.thisRange) | ||
|
||
cd.bot = make([]*table.Table, right-left) | ||
copy(cd.bot, cd.nextLevel.tables[left:right]) | ||
lc.fillBottomTables(cd, cd.nextLevel.tables[left:right]) | ||
|
||
if len(cd.bot) == 0 { | ||
cd.bot = []*table.Table{} | ||
|
@@ -780,7 +804,7 @@ func (lc *levelsController) runCompactDef(l int, cd compactDef, limiter *rate.Li | |
|
||
var newTables []*table.Table | ||
var changeSet protos.ManifestChangeSet | ||
if l > 0 && len(cd.bot) == 0 { | ||
if l > 0 && len(cd.bot) == 0 && len(cd.skippedTbls) == 0 { | ||
// skip level 0, since it may has many table overlap with each other | ||
newTables = cd.top | ||
changeSet = protos.ManifestChangeSet{Changes: []*protos.ManifestChange{ | ||
|
@@ -803,7 +827,7 @@ func (lc *levelsController) runCompactDef(l int, cd compactDef, limiter *rate.Li | |
|
||
// See comment earlier in this function about the ordering of these ops, and the order in which | ||
// we access levels when reading. | ||
if err := nextLevel.replaceTables(newTables); err != nil { | ||
if err := nextLevel.replaceTables(newTables, cd.skippedTbls); err != nil { | ||
return err | ||
} | ||
if err := thisLevel.deleteTables(cd.top); err != nil { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you keep assertTablesOrder to make sure assertion is always true?