From 68d47e308e7173c035c1d47155e04b433dec6045 Mon Sep 17 00:00:00 2001 From: Colin LeMahieu Date: Fri, 27 Sep 2024 11:57:13 +0100 Subject: [PATCH] Removing rocksdb write lock mutexes as store::write_queue serializes write transactions. --- nano/store/rocksdb/rocksdb.cpp | 31 ++----------------------- nano/store/rocksdb/rocksdb.hpp | 2 -- nano/store/rocksdb/transaction.cpp | 27 +++------------------ nano/store/rocksdb/transaction_impl.hpp | 8 +------ 4 files changed, 6 insertions(+), 62 deletions(-) diff --git a/nano/store/rocksdb/rocksdb.cpp b/nano/store/rocksdb/rocksdb.cpp index c16c6f57dd..d17cbe28e9 100644 --- a/nano/store/rocksdb/rocksdb.cpp +++ b/nano/store/rocksdb/rocksdb.cpp @@ -117,11 +117,6 @@ nano::store::rocksdb::component::component (nano::logger & logger_a, std::filesy db.reset (nullptr); } - if (!open_read_only_a) - { - construct_column_family_mutexes (); - } - if (is_fully_upgraded) { open (error, path_a, open_read_only_a, options, create_column_families ()); @@ -422,24 +417,10 @@ std::vector nano::store::rocksdb::component::cr return column_families; } -nano::store::write_transaction nano::store::rocksdb::component::tx_begin_write (std::vector const & tables_requiring_locks_a, std::vector const & tables_no_locks_a) +nano::store::write_transaction nano::store::rocksdb::component::tx_begin_write (std::vector const &, std::vector const &) { - std::unique_ptr txn; release_assert (db != nullptr); - if (tables_requiring_locks_a.empty () && tables_no_locks_a.empty ()) - { - // Use all tables if none are specified - txn = std::make_unique (transaction_db, all_tables (), tables_no_locks_a, write_lock_mutexes); - } - else - { - txn = std::make_unique (transaction_db, tables_requiring_locks_a, tables_no_locks_a, write_lock_mutexes); - } - - // Tables must be kept in alphabetical order. These can be used for mutex locking, so order is important to prevent deadlocking - debug_assert (std::is_sorted (tables_requiring_locks_a.begin (), tables_requiring_locks_a.end ())); - - return store::write_transaction{ std::move (txn) }; + return store::write_transaction{ std::make_unique (transaction_db) }; } nano::store::read_transaction nano::store::rocksdb::component::tx_begin_read () const @@ -743,14 +724,6 @@ int nano::store::rocksdb::component::clear (::rocksdb::ColumnFamilyHandle * colu return status.code (); } -void nano::store::rocksdb::component::construct_column_family_mutexes () -{ - for (auto table : all_tables ()) - { - write_lock_mutexes.emplace (std::piecewise_construct, std::forward_as_tuple (table), std::forward_as_tuple ()); - } -} - rocksdb::Options nano::store::rocksdb::component::get_db_options () { ::rocksdb::Options db_options; diff --git a/nano/store/rocksdb/rocksdb.hpp b/nano/store/rocksdb/rocksdb.hpp index fe197536e3..643a67a672 100644 --- a/nano/store/rocksdb/rocksdb.hpp +++ b/nano/store/rocksdb/rocksdb.hpp @@ -108,7 +108,6 @@ class component : public nano::store::component ::rocksdb::TransactionDB * transaction_db = nullptr; std::unique_ptr<::rocksdb::DB> db; std::vector> handles; - std::unordered_map write_lock_mutexes; nano::rocksdb_config rocksdb_config; unsigned const max_block_write_batch_num_m; @@ -152,7 +151,6 @@ class component : public nano::store::component void upgrade_v22_to_v23 (store::write_transaction &); void upgrade_v23_to_v24 (store::write_transaction &); - void construct_column_family_mutexes (); ::rocksdb::Options get_db_options (); ::rocksdb::BlockBasedTableOptions get_table_options () const; ::rocksdb::ColumnFamilyOptions get_cf_options (std::string const & cf_name_a) const; diff --git a/nano/store/rocksdb/transaction.cpp b/nano/store/rocksdb/transaction.cpp index abccc8bf6f..5642029cc4 100644 --- a/nano/store/rocksdb/transaction.cpp +++ b/nano/store/rocksdb/transaction.cpp @@ -32,13 +32,9 @@ void * nano::store::rocksdb::read_transaction_impl::get_handle () const return (void *)&options; } -nano::store::rocksdb::write_transaction_impl::write_transaction_impl (::rocksdb::TransactionDB * db_a, std::vector const & tables_requiring_locks_a, std::vector const & tables_no_locks_a, std::unordered_map & mutexes_a) : - db (db_a), - tables_requiring_locks (tables_requiring_locks_a), - tables_no_locks (tables_no_locks_a), - mutexes (mutexes_a) +nano::store::rocksdb::write_transaction_impl::write_transaction_impl (::rocksdb::TransactionDB * db_a) : + db (db_a) { - lock (); ::rocksdb::TransactionOptions txn_options; txn_options.set_snapshot = true; txn = db->BeginTransaction (::rocksdb::WriteOptions (), txn_options); @@ -48,7 +44,6 @@ nano::store::rocksdb::write_transaction_impl::~write_transaction_impl () { commit (); delete txn; - unlock (); } void nano::store::rocksdb::write_transaction_impl::commit () @@ -74,23 +69,7 @@ void * nano::store::rocksdb::write_transaction_impl::get_handle () const return txn; } -void nano::store::rocksdb::write_transaction_impl::lock () -{ - for (auto table : tables_requiring_locks) - { - mutexes.at (table).lock (); - } -} - -void nano::store::rocksdb::write_transaction_impl::unlock () -{ - for (auto table : tables_requiring_locks) - { - mutexes.at (table).unlock (); - } -} - bool nano::store::rocksdb::write_transaction_impl::contains (nano::tables table_a) const { - return (std::find (tables_requiring_locks.begin (), tables_requiring_locks.end (), table_a) != tables_requiring_locks.end ()) || (std::find (tables_no_locks.begin (), tables_no_locks.end (), table_a) != tables_no_locks.end ()); + return true; } diff --git a/nano/store/rocksdb/transaction_impl.hpp b/nano/store/rocksdb/transaction_impl.hpp index ab9429fcd8..9ccbedc184 100644 --- a/nano/store/rocksdb/transaction_impl.hpp +++ b/nano/store/rocksdb/transaction_impl.hpp @@ -26,7 +26,7 @@ class read_transaction_impl final : public store::read_transaction_impl class write_transaction_impl final : public store::write_transaction_impl { public: - write_transaction_impl (::rocksdb::TransactionDB * db_a, std::vector const & tables_requiring_locks_a, std::vector const & tables_no_locks_a, std::unordered_map & mutexes_a); + write_transaction_impl (::rocksdb::TransactionDB * db_a); ~write_transaction_impl (); void commit () override; void renew () override; @@ -36,12 +36,6 @@ class write_transaction_impl final : public store::write_transaction_impl private: ::rocksdb::Transaction * txn; ::rocksdb::TransactionDB * db; - std::vector tables_requiring_locks; - std::vector tables_no_locks; - std::unordered_map & mutexes; bool active{ true }; - - void lock (); - void unlock (); }; }