release db mutex when calling ApproximateSize (#4630)

Summary:
`GenSubcompactionBoundaries` calls `VersionSet::ApproximateSize` which gets BlockBasedTableReader for every file and seeks in its index block to find `key`'s offset. If the table or index block aren't in memory already, this involves I/O. This can be improved by releasing DB mutex when calling ApproximateSize.
Pull Request resolved: https://github.com/facebook/rocksdb/pull/4630

Differential Revision: D13052653

Pulled By: miasantreble

fbshipit-source-id: cae31d46d10d0860fa8a26b8d5154b2d17d1685f
main
Zhongyi Xie 6 years ago committed by Facebook Github Bot
parent b82e57d425
commit d8df169b84
  1. 11
      db/compaction_job.cc

@ -511,7 +511,10 @@ void CompactionJob::GenSubcompactionBoundaries() {
// size of data covered by keys in that range // size of data covered by keys in that range
uint64_t sum = 0; uint64_t sum = 0;
std::vector<RangeWithSize> ranges; std::vector<RangeWithSize> ranges;
auto* v = cfd->current(); // Get input version from CompactionState since it's already referenced
// earlier in SetInputVersioCompaction::SetInputVersion and will not change
// when db_mutex_ is released below
auto* v = compact_->compaction->input_version();
for (auto it = bounds.begin();;) { for (auto it = bounds.begin();;) {
const Slice a = *it; const Slice a = *it;
it++; it++;
@ -521,7 +524,13 @@ void CompactionJob::GenSubcompactionBoundaries() {
} }
const Slice b = *it; const Slice b = *it;
// ApproximateSize could potentially create table reader iterator to seek
// to the index block and may incur I/O cost in the process. Unlock db
// mutex to reduce contention
db_mutex_->Unlock();
uint64_t size = versions_->ApproximateSize(v, a, b, start_lvl, out_lvl + 1); uint64_t size = versions_->ApproximateSize(v, a, b, start_lvl, out_lvl + 1);
db_mutex_->Lock();
ranges.emplace_back(a, b, size); ranges.emplace_back(a, b, size);
sum += size; sum += size;
} }

Loading…
Cancel
Save