Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix deadlock between Flush and UnregisterDB #349

Merged
merged 5 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions db/db_impl/db_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -690,11 +690,14 @@ Status DBImpl::CloseHelper() {
delete txn_entry.second;
}

mutex_.Unlock();
// We can only access cf_based_write_buffer_manager_ before versions_.reset(),
// after which all cf write buffer managers will be freed.
for (auto m : cf_based_write_buffer_manager_) {
m->UnregisterDB(this);
}
mutex_.Lock();

// versions need to be destroyed before table_cache since it can hold
// references to table_cache.
versions_.reset();
Expand Down
5 changes: 5 additions & 0 deletions memtable/write_buffer_manager.cc
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ void WriteBufferManager::SetFlushSize(size_t new_size) {
}
}

// Must be called without holding db mutex.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment should be placed in header file.

void WriteBufferManager::RegisterColumnFamily(DB* db, ColumnFamilyHandle* cf) {
assert(db != nullptr);
auto sentinel = std::make_shared<WriteBufferSentinel>();
Expand All @@ -83,6 +84,7 @@ void WriteBufferManager::RegisterColumnFamily(DB* db, ColumnFamilyHandle* cf) {
sentinels_.push_back(sentinel);
}

// Must be called without holding db mutex.
void WriteBufferManager::UnregisterDB(DB* db) {
std::lock_guard<std::mutex> lock(sentinels_mu_);
sentinels_.remove_if([=](const std::shared_ptr<WriteBufferSentinel>& s) {
Expand All @@ -91,6 +93,7 @@ void WriteBufferManager::UnregisterDB(DB* db) {
MaybeFlushLocked();
}

// Must be called without holding db mutex.
void WriteBufferManager::UnregisterColumnFamily(ColumnFamilyHandle* cf) {
std::lock_guard<std::mutex> lock(sentinels_mu_);
sentinels_.remove_if([=](const std::shared_ptr<WriteBufferSentinel>& s) {
Expand All @@ -99,6 +102,7 @@ void WriteBufferManager::UnregisterColumnFamily(ColumnFamilyHandle* cf) {
MaybeFlushLocked();
}

// Must be called without holding db mutex.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is wrong.

void WriteBufferManager::ReserveMem(size_t mem) {
size_t local_size = flush_size();
if (cache_res_mgr_ != nullptr) {
Expand Down Expand Up @@ -174,6 +178,7 @@ void WriteBufferManager::FreeMemWithCache(size_t mem) {
#endif // ROCKSDB_LITE
}

// Must be called without holding db mutex.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not entirely correct. The lock of this_db (if not null) must be held.

void WriteBufferManager::MaybeFlushLocked(DB* this_db) {
if (!ShouldFlush()) {
return;
Expand Down
Loading