Skip to content

Commit

Permalink
Removing rocksdb write lock mutexes as store::write_queue serializes …
Browse files Browse the repository at this point in the history
…write transactions.
  • Loading branch information
clemahieu committed Sep 27, 2024
1 parent 076df48 commit 68d47e3
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 62 deletions.
31 changes: 2 additions & 29 deletions nano/store/rocksdb/rocksdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ());
Expand Down Expand Up @@ -422,24 +417,10 @@ std::vector<rocksdb::ColumnFamilyDescriptor> nano::store::rocksdb::component::cr
return column_families;
}

nano::store::write_transaction nano::store::rocksdb::component::tx_begin_write (std::vector<nano::tables> const & tables_requiring_locks_a, std::vector<nano::tables> const & tables_no_locks_a)
nano::store::write_transaction nano::store::rocksdb::component::tx_begin_write (std::vector<nano::tables> const &, std::vector<nano::tables> const &)
{
std::unique_ptr<nano::store::rocksdb::write_transaction_impl> 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<nano::store::rocksdb::write_transaction_impl> (transaction_db, all_tables (), tables_no_locks_a, write_lock_mutexes);
}
else
{
txn = std::make_unique<nano::store::rocksdb::write_transaction_impl> (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<nano::store::rocksdb::write_transaction_impl> (transaction_db) };
}

nano::store::read_transaction nano::store::rocksdb::component::tx_begin_read () const
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 0 additions & 2 deletions nano/store/rocksdb/rocksdb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ class component : public nano::store::component
::rocksdb::TransactionDB * transaction_db = nullptr;
std::unique_ptr<::rocksdb::DB> db;
std::vector<std::unique_ptr<::rocksdb::ColumnFamilyHandle>> handles;
std::unordered_map<nano::tables, nano::mutex> write_lock_mutexes;
nano::rocksdb_config rocksdb_config;
unsigned const max_block_write_batch_num_m;

Expand Down Expand Up @@ -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;
Expand Down
27 changes: 3 additions & 24 deletions nano/store/rocksdb/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<nano::tables> const & tables_requiring_locks_a, std::vector<nano::tables> const & tables_no_locks_a, std::unordered_map<nano::tables, nano::mutex> & 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);
Expand All @@ -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 ()
Expand All @@ -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;
}
8 changes: 1 addition & 7 deletions nano/store/rocksdb/transaction_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<nano::tables> const & tables_requiring_locks_a, std::vector<nano::tables> const & tables_no_locks_a, std::unordered_map<nano::tables, nano::mutex> & mutexes_a);
write_transaction_impl (::rocksdb::TransactionDB * db_a);
~write_transaction_impl ();
void commit () override;
void renew () override;
Expand All @@ -36,12 +36,6 @@ class write_transaction_impl final : public store::write_transaction_impl
private:
::rocksdb::Transaction * txn;
::rocksdb::TransactionDB * db;
std::vector<nano::tables> tables_requiring_locks;
std::vector<nano::tables> tables_no_locks;
std::unordered_map<nano::tables, nano::mutex> & mutexes;
bool active{ true };

void lock ();
void unlock ();
};
}

0 comments on commit 68d47e3

Please sign in to comment.