From ecd8db02004d73c4b65ebb772a6c882b5cb98223 Mon Sep 17 00:00:00 2001 From: Haobo Xu Date: Mon, 25 Mar 2013 18:01:47 -0700 Subject: [PATCH] [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 --- db/db_impl.cc | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/db/db_impl.cc b/db/db_impl.cc index 9d68f7010..167941375 100644 --- a/db/db_impl.cc +++ b/db/db_impl.cc @@ -1876,11 +1876,12 @@ Status DBImpl::Get(const ReadOptions& options, const Slice& key, std::string* value) { Status s; - MutexLock l(&mutex_); + std::unique_ptr sw = stats::StartStopWatch(env_, options_.statistics, DB_GET); SequenceNumber snapshot; + MutexLock l(&mutex_); if (options.snapshot != nullptr) { snapshot = reinterpret_cast(options.snapshot)->number_; } else { @@ -1894,24 +1895,22 @@ Status DBImpl::Get(const ReadOptions& options, imm.RefAll(); current->Ref(); + // Unlock while reading from files and memtables + + mutex_.Unlock(); bool have_stat_update = false; Version::GetStats stats; - - // Unlock while reading from files and memtables - { - mutex_.Unlock(); - // First look in the memtable, then in the immutable memtable (if any). - LookupKey lkey(key, snapshot); - if (mem->Get(lkey, value, &s)) { - // Done - } else if (imm.Get(lkey, value, &s)) { - // Done - } else { - s = current->Get(options, lkey, value, &stats); - have_stat_update = true; - } - mutex_.Lock(); + // First look in the memtable, then in the immutable memtable (if any). + LookupKey lkey(key, snapshot); + if (mem->Get(lkey, value, &s)) { + // Done + } else if (imm.Get(lkey, value, &s)) { + // Done + } else { + s = current->Get(options, lkey, value, &stats); + have_stat_update = true; } + mutex_.Lock(); if (!options_.disable_seek_compaction && have_stat_update && current->UpdateStats(stats)) { @@ -1961,10 +1960,10 @@ Status DBImpl::Write(const WriteOptions& options, WriteBatch* my_batch) { w.disableWAL = options.disableWAL; w.done = false; - MutexLock l(&mutex_); std::unique_ptr sw = stats::StartStopWatch(env_, options_.statistics, DB_WRITE); + MutexLock l(&mutex_); writers_.push_back(&w); while (!w.done && &w != writers_.front()) { w.cv.Wait();