From 8e6bbe1c62c84cb8cd4c69a3a06dfa437daa3bf0 Mon Sep 17 00:00:00 2001 From: SpadeA-Tang Date: Sat, 7 Oct 2023 17:39:26 +0800 Subject: [PATCH 1/5] i Signed-off-by: SpadeA-Tang --- db/db_impl/db_impl.cc | 3 +++ memtable/write_buffer_manager.cc | 5 +++++ 2 files changed, 8 insertions(+) diff --git a/db/db_impl/db_impl.cc b/db/db_impl/db_impl.cc index 4089c0acb9a..4c0798531e7 100644 --- a/db/db_impl/db_impl.cc +++ b/db/db_impl/db_impl.cc @@ -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(); diff --git a/memtable/write_buffer_manager.cc b/memtable/write_buffer_manager.cc index 6a7e199b33c..9ccf11d40be 100644 --- a/memtable/write_buffer_manager.cc +++ b/memtable/write_buffer_manager.cc @@ -73,6 +73,7 @@ void WriteBufferManager::SetFlushSize(size_t new_size) { } } +// Must be called without holding db mutex. void WriteBufferManager::RegisterColumnFamily(DB* db, ColumnFamilyHandle* cf) { assert(db != nullptr); auto sentinel = std::make_shared(); @@ -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 lock(sentinels_mu_); sentinels_.remove_if([=](const std::shared_ptr& s) { @@ -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 lock(sentinels_mu_); sentinels_.remove_if([=](const std::shared_ptr& s) { @@ -99,6 +102,7 @@ void WriteBufferManager::UnregisterColumnFamily(ColumnFamilyHandle* cf) { MaybeFlushLocked(); } +// Must be called without holding db mutex. void WriteBufferManager::ReserveMem(size_t mem) { size_t local_size = flush_size(); if (cache_res_mgr_ != nullptr) { @@ -174,6 +178,7 @@ void WriteBufferManager::FreeMemWithCache(size_t mem) { #endif // ROCKSDB_LITE } +// Must be called without holding db mutex. void WriteBufferManager::MaybeFlushLocked(DB* this_db) { if (!ShouldFlush()) { return; From 8de1463699d23b3c300849987295467c85f040ed Mon Sep 17 00:00:00 2001 From: SpadeA-Tang Date: Mon, 9 Oct 2023 10:25:24 +0800 Subject: [PATCH 2/5] update comment Signed-off-by: SpadeA-Tang --- include/rocksdb/write_buffer_manager.h | 2 ++ memtable/write_buffer_manager.cc | 4 ---- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/include/rocksdb/write_buffer_manager.h b/include/rocksdb/write_buffer_manager.h index f2b93d8aa55..43fdb113b3e 100644 --- a/include/rocksdb/write_buffer_manager.h +++ b/include/rocksdb/write_buffer_manager.h @@ -141,6 +141,8 @@ class WriteBufferManager final { } } + // Must ensure that the mutex of all dbs except this_db are not held. If this_db + // is not nullptr, the mutex of it must be held. void MaybeFlushLocked(DB* this_db = nullptr); // Returns true if total memory usage exceeded buffer_size. diff --git a/memtable/write_buffer_manager.cc b/memtable/write_buffer_manager.cc index 9ccf11d40be..f87edee4c10 100644 --- a/memtable/write_buffer_manager.cc +++ b/memtable/write_buffer_manager.cc @@ -84,7 +84,6 @@ 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 lock(sentinels_mu_); sentinels_.remove_if([=](const std::shared_ptr& s) { @@ -93,7 +92,6 @@ void WriteBufferManager::UnregisterDB(DB* db) { MaybeFlushLocked(); } -// Must be called without holding db mutex. void WriteBufferManager::UnregisterColumnFamily(ColumnFamilyHandle* cf) { std::lock_guard lock(sentinels_mu_); sentinels_.remove_if([=](const std::shared_ptr& s) { @@ -102,7 +100,6 @@ void WriteBufferManager::UnregisterColumnFamily(ColumnFamilyHandle* cf) { MaybeFlushLocked(); } -// Must be called without holding db mutex. void WriteBufferManager::ReserveMem(size_t mem) { size_t local_size = flush_size(); if (cache_res_mgr_ != nullptr) { @@ -115,7 +112,6 @@ void WriteBufferManager::ReserveMem(size_t mem) { } } -// Should only be called from write thread void WriteBufferManager::ReserveMemWithCache(size_t mem) { #ifndef ROCKSDB_LITE assert(cache_res_mgr_ != nullptr); From 0b3a2322998c0abe4916383d912a9f8814b29f37 Mon Sep 17 00:00:00 2001 From: SpadeA-Tang Date: Mon, 9 Oct 2023 10:27:27 +0800 Subject: [PATCH 3/5] u Signed-off-by: SpadeA-Tang --- memtable/write_buffer_manager.cc | 2 -- 1 file changed, 2 deletions(-) diff --git a/memtable/write_buffer_manager.cc b/memtable/write_buffer_manager.cc index f87edee4c10..f87091016d6 100644 --- a/memtable/write_buffer_manager.cc +++ b/memtable/write_buffer_manager.cc @@ -73,7 +73,6 @@ void WriteBufferManager::SetFlushSize(size_t new_size) { } } -// Must be called without holding db mutex. void WriteBufferManager::RegisterColumnFamily(DB* db, ColumnFamilyHandle* cf) { assert(db != nullptr); auto sentinel = std::make_shared(); @@ -174,7 +173,6 @@ void WriteBufferManager::FreeMemWithCache(size_t mem) { #endif // ROCKSDB_LITE } -// Must be called without holding db mutex. void WriteBufferManager::MaybeFlushLocked(DB* this_db) { if (!ShouldFlush()) { return; From 3e6c116588969932e44cf06b2b4e9f2a5593f8d0 Mon Sep 17 00:00:00 2001 From: SpadeA-Tang Date: Mon, 9 Oct 2023 10:29:02 +0800 Subject: [PATCH 4/5] U Signed-off-by: SpadeA-Tang --- memtable/write_buffer_manager.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/memtable/write_buffer_manager.cc b/memtable/write_buffer_manager.cc index f87091016d6..6a7e199b33c 100644 --- a/memtable/write_buffer_manager.cc +++ b/memtable/write_buffer_manager.cc @@ -111,6 +111,7 @@ void WriteBufferManager::ReserveMem(size_t mem) { } } +// Should only be called from write thread void WriteBufferManager::ReserveMemWithCache(size_t mem) { #ifndef ROCKSDB_LITE assert(cache_res_mgr_ != nullptr); From d6508d7914ae59292983afb17b31fc92d59bba9e Mon Sep 17 00:00:00 2001 From: SpadeA-Tang Date: Mon, 9 Oct 2023 10:39:31 +0800 Subject: [PATCH 5/5] format Signed-off-by: SpadeA-Tang --- include/rocksdb/write_buffer_manager.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/include/rocksdb/write_buffer_manager.h b/include/rocksdb/write_buffer_manager.h index 43fdb113b3e..444e383080d 100644 --- a/include/rocksdb/write_buffer_manager.h +++ b/include/rocksdb/write_buffer_manager.h @@ -141,8 +141,8 @@ class WriteBufferManager final { } } - // Must ensure that the mutex of all dbs except this_db are not held. If this_db - // is not nullptr, the mutex of it must be held. + // Must ensure that the mutex of all dbs except this_db are not held. If + // this_db is not nullptr, the mutex of it must be held. void MaybeFlushLocked(DB* this_db = nullptr); // Returns true if total memory usage exceeded buffer_size.