[RocksDB] Minimize Mutex protected code section in the critical path

Summary: rocksdb uses a single global lock to protect in memory metadata. We should minimize the mutex protected code section to increase the effective parallelism of the program. See https://our.intern.facebook.com/intern/tasks/?t=2218928

Test Plan:
make check
db_bench

Reviewers: dhruba, heyongqiang

CC: zshao, leveldb

Differential Revision: https://reviews.facebook.net/D9705
main
Haobo Xu 12 years ago
parent a8bf8fe504
commit ecd8db0200
  1. 13
      db/db_impl.cc

@ -1876,11 +1876,12 @@ Status DBImpl::Get(const ReadOptions& options,
const Slice& key, const Slice& key,
std::string* value) { std::string* value) {
Status s; Status s;
MutexLock l(&mutex_);
std::unique_ptr<StopWatch> sw = stats::StartStopWatch(env_, std::unique_ptr<StopWatch> sw = stats::StartStopWatch(env_,
options_.statistics, options_.statistics,
DB_GET); DB_GET);
SequenceNumber snapshot; SequenceNumber snapshot;
MutexLock l(&mutex_);
if (options.snapshot != nullptr) { if (options.snapshot != nullptr) {
snapshot = reinterpret_cast<const SnapshotImpl*>(options.snapshot)->number_; snapshot = reinterpret_cast<const SnapshotImpl*>(options.snapshot)->number_;
} else { } else {
@ -1894,12 +1895,11 @@ Status DBImpl::Get(const ReadOptions& options,
imm.RefAll(); imm.RefAll();
current->Ref(); current->Ref();
bool have_stat_update = false;
Version::GetStats stats;
// Unlock while reading from files and memtables // Unlock while reading from files and memtables
{
mutex_.Unlock(); mutex_.Unlock();
bool have_stat_update = false;
Version::GetStats stats;
// First look in the memtable, then in the immutable memtable (if any). // First look in the memtable, then in the immutable memtable (if any).
LookupKey lkey(key, snapshot); LookupKey lkey(key, snapshot);
if (mem->Get(lkey, value, &s)) { if (mem->Get(lkey, value, &s)) {
@ -1911,7 +1911,6 @@ Status DBImpl::Get(const ReadOptions& options,
have_stat_update = true; have_stat_update = true;
} }
mutex_.Lock(); mutex_.Lock();
}
if (!options_.disable_seek_compaction && if (!options_.disable_seek_compaction &&
have_stat_update && current->UpdateStats(stats)) { have_stat_update && current->UpdateStats(stats)) {
@ -1961,10 +1960,10 @@ Status DBImpl::Write(const WriteOptions& options, WriteBatch* my_batch) {
w.disableWAL = options.disableWAL; w.disableWAL = options.disableWAL;
w.done = false; w.done = false;
MutexLock l(&mutex_);
std::unique_ptr<StopWatch> sw = stats::StartStopWatch(env_, std::unique_ptr<StopWatch> sw = stats::StartStopWatch(env_,
options_.statistics, options_.statistics,
DB_WRITE); DB_WRITE);
MutexLock l(&mutex_);
writers_.push_back(&w); writers_.push_back(&w);
while (!w.done && &w != writers_.front()) { while (!w.done && &w != writers_.front()) {
w.cv.Wait(); w.cv.Wait();

Loading…
Cancel
Save