Skip to content

Commit ea0198f

Browse files
committed
Create log::Writer out of DB Mutex
Summary: Our measurement shows that sometimes new log::Write's constructor can take hundreds of milliseconds. It's unclear why but just simply move it out of DB mutex. Test Plan: make all check Reviewers: haobo, ljin, igor Reviewed By: haobo CC: nkg-, yhchiang, leveldb Differential Revision: https://reviews.facebook.net/D17487
1 parent c90d446 commit ea0198f

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

db/db_impl.cc

+13-3
Original file line numberDiff line numberDiff line change
@@ -3703,7 +3703,10 @@ Status DBImpl::Write(const WriteOptions& options, WriteBatch* my_batch) {
37033703

37043704
// May temporarily unlock and wait.
37053705
SuperVersion* superversion_to_free = nullptr;
3706-
Status status = MakeRoomForWrite(my_batch == nullptr, &superversion_to_free);
3706+
log::Writer* old_log = nullptr;
3707+
Status status = MakeRoomForWrite(my_batch == nullptr,
3708+
&superversion_to_free,
3709+
&old_log);
37073710
uint64_t last_sequence = versions_->LastSequence();
37083711
Writer* last_writer = &w;
37093712
if (status.ok() && my_batch != nullptr) { // nullptr batch is for compactions
@@ -3804,6 +3807,7 @@ Status DBImpl::Write(const WriteOptions& options, WriteBatch* my_batch) {
38043807
writers_.front()->cv.Signal();
38053808
}
38063809
mutex_.Unlock();
3810+
delete old_log;
38073811
delete superversion_to_free;
38083812
BumpPerfTime(&perf_context.write_pre_and_post_process_time,
38093813
&pre_post_process_timer);
@@ -3893,7 +3897,8 @@ uint64_t DBImpl::SlowdownAmount(int n, double bottom, double top) {
38933897
// REQUIRES: mutex_ is held
38943898
// REQUIRES: this thread is currently at the front of the writer queue
38953899
Status DBImpl::MakeRoomForWrite(bool force,
3896-
SuperVersion** superversion_to_free) {
3900+
SuperVersion** superversion_to_free,
3901+
log::Writer** old_log) {
38973902
mutex_.AssertHeld();
38983903
assert(!writers_.empty());
38993904
bool allow_delay = !force;
@@ -4015,6 +4020,7 @@ Status DBImpl::MakeRoomForWrite(bool force,
40154020

40164021
} else {
40174022
unique_ptr<WritableFile> lfile;
4023+
log::Writer* new_log = nullptr;
40184024
MemTable* new_mem = nullptr;
40194025

40204026
// Attempt to switch to a new memtable and trigger flush of old.
@@ -4032,6 +4038,7 @@ Status DBImpl::MakeRoomForWrite(bool force,
40324038
// Our final size should be less than write_buffer_size
40334039
// (compression, etc) but err on the side of caution.
40344040
lfile->SetPreallocationBlockSize(1.1 * options_.write_buffer_size);
4041+
new_log = new log::Writer(std::move(lfile));
40354042
new_mem = new MemTable(internal_comparator_, options_);
40364043
new_superversion = new SuperVersion();
40374044
}
@@ -4044,10 +4051,13 @@ Status DBImpl::MakeRoomForWrite(bool force,
40444051
// Avoid chewing through file number space in a tight loop.
40454052
versions_->ReuseFileNumber(new_log_number);
40464053
assert (!new_mem);
4054+
assert(new_log == nullptr);
40474055
break;
40484056
}
40494057
logfile_number_ = new_log_number;
4050-
log_.reset(new log::Writer(std::move(lfile)));
4058+
assert(new_log != nullptr);
4059+
*old_log = log_.release();
4060+
log_.reset(new_log);
40514061
mem_->SetNextLogNumber(logfile_number_);
40524062
imm_.Add(mem_);
40534063
if (force) {

db/db_impl.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -338,9 +338,12 @@ class DBImpl : public DB {
338338
uint64_t SlowdownAmount(int n, double bottom, double top);
339339
// MakeRoomForWrite will return superversion_to_free through an arugment,
340340
// which the caller needs to delete. We do it because caller can delete
341-
// the superversion outside of mutex
341+
// the superversion outside of mutex.
342+
// old_log if not nullptr is the old log writer that should be safely
343+
// closed whenever DB mutex is released.
342344
Status MakeRoomForWrite(bool force /* compact even if there is room? */,
343-
SuperVersion** superversion_to_free);
345+
SuperVersion** superversion_to_free,
346+
log::Writer** old_log);
344347
void BuildBatchGroup(Writer** last_writer,
345348
autovector<WriteBatch*>* write_batch_group);
346349

0 commit comments

Comments
 (0)