Skip to content

Commit

Permalink
Guard DBImpl::versions_ by mutex_.
Browse files Browse the repository at this point in the history
mutex_ was already acquired before accessing DBImpl::versions_ in all
but one place: DBImpl::GetApproximateSizes. This change requires mutex_
to be held before accessing versions_.

PiperOrigin-RevId: 248390814
  • Loading branch information
cmumford committed May 16, 2019
1 parent 1d0b101 commit c00e177
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 12 deletions.
17 changes: 6 additions & 11 deletions db/db_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -893,10 +893,11 @@ Status DBImpl::DoCompactionWork(CompactionState* compact) {
compact->smallest_snapshot = snapshots_.oldest()->sequence_number();
}

Iterator* input = versions_->MakeInputIterator(compact->compaction);

// Release mutex while we're actually doing the compaction work
mutex_.Unlock();

Iterator* input = versions_->MakeInputIterator(compact->compaction);
input->SeekToFirst();
Status status;
ParsedInternalKey ikey;
Expand Down Expand Up @@ -1433,12 +1434,9 @@ bool DBImpl::GetProperty(const Slice& property, std::string* value) {

void DBImpl::GetApproximateSizes(const Range* range, int n, uint64_t* sizes) {
// TODO(opt): better implementation
Version* v;
{
MutexLock l(&mutex_);
versions_->current()->Ref();
v = versions_->current();
}
MutexLock l(&mutex_);
Version* v = versions_->current();
v->Ref();

for (int i = 0; i < n; i++) {
// Convert user_key into a corresponding internal key.
Expand All @@ -1449,10 +1447,7 @@ void DBImpl::GetApproximateSizes(const Range* range, int n, uint64_t* sizes) {
sizes[i] = (limit >= start ? limit - start : 0);
}

{
MutexLock l(&mutex_);
v->Unref();
}
v->Unref();
}

// Default implementations of convenience methods that subclasses of DB
Expand Down
2 changes: 1 addition & 1 deletion db/db_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ class DBImpl : public DB {

ManualCompaction* manual_compaction_ GUARDED_BY(mutex_);

VersionSet* const versions_;
VersionSet* const versions_ GUARDED_BY(mutex_);

// Have we encountered a background error in paranoid mode?
Status bg_error_ GUARDED_BY(mutex_);
Expand Down

0 comments on commit c00e177

Please sign in to comment.