Skip to content

Commit

Permalink
Squash merge PR nanocurrency#4671: Use RocksDB TransactionDB rather t…
Browse files Browse the repository at this point in the history
…han OptimisticTransactionDB
  • Loading branch information
gr0vity committed Jul 7, 2024
1 parent 0776b9f commit ebf31fa
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 29 deletions.
12 changes: 6 additions & 6 deletions nano/store/rocksdb/rocksdb.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,10 +193,10 @@ void nano::store::rocksdb::component::open (bool & error_a, std::filesystem::pat
}
else
{
s = ::rocksdb::OptimisticTransactionDB::Open (options_a, path_a.string (), column_families, &handles_l, &optimistic_db);
if (optimistic_db)
s = ::rocksdb::TransactionDB::Open (options_a, ::rocksdb::TransactionDBOptions{}, path_a.string (), column_families, &handles_l, &transaction_db);
if (transaction_db)
{
db.reset (optimistic_db);
db.reset (transaction_db);
}
}

Expand Down Expand Up @@ -489,15 +489,15 @@ std::vector<rocksdb::ColumnFamilyDescriptor> nano::store::rocksdb::component::cr
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)
{
std::unique_ptr<nano::store::rocksdb::write_transaction_impl> txn;
release_assert (optimistic_db != nullptr);
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> (optimistic_db, all_tables (), tables_no_locks_a, write_lock_mutexes);
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> (optimistic_db, tables_requiring_locks_a, tables_no_locks_a, write_lock_mutexes);
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
Expand Down
5 changes: 2 additions & 3 deletions nano/store/rocksdb/rocksdb.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
#include <rocksdb/options.h>
#include <rocksdb/slice.h>
#include <rocksdb/table.h>
#include <rocksdb/utilities/optimistic_transaction_db.h>
#include <rocksdb/utilities/transaction_db.h>

namespace nano
{
Expand Down Expand Up @@ -105,8 +105,7 @@ class component : public nano::store::component
bool error{ false };
nano::logger & logger;
nano::ledger_constants & constants;
// Optimistic transactions are used in write mode
::rocksdb::OptimisticTransactionDB * optimistic_db = nullptr;
::rocksdb::TransactionDB * transaction_db = nullptr;
std::unique_ptr<::rocksdb::DB> db;
std::vector<std::unique_ptr<::rocksdb::ColumnFamilyHandle>> handles;
std::shared_ptr<::rocksdb::TableFactory> small_table_factory;
Expand Down
21 changes: 4 additions & 17 deletions nano/store/rocksdb/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@ void * nano::store::rocksdb::read_transaction_impl::get_handle () const
return (void *)&options;
}

nano::store::rocksdb::write_transaction_impl::write_transaction_impl (::rocksdb::OptimisticTransactionDB * 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) :
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)
{
lock ();
::rocksdb::OptimisticTransactionOptions txn_options;
::rocksdb::TransactionOptions txn_options;
txn_options.set_snapshot = true;
txn = db->BeginTransaction (::rocksdb::WriteOptions (), txn_options);
}
Expand All @@ -56,27 +56,14 @@ void nano::store::rocksdb::write_transaction_impl::commit ()
if (active)
{
auto status = txn->Commit ();

// If there are no available memtables try again a few more times
constexpr auto num_attempts = 10;
auto attempt_num = 0;
while (status.IsTryAgain () && attempt_num < num_attempts)
{
status = txn->Commit ();
++attempt_num;
}

if (!status.ok ())
{
release_assert (false && "Unable to write to the RocksDB database", status.ToString ());
}
release_assert (status.ok () && "Unable to write to the RocksDB database", status.ToString ());
active = false;
}
}

void nano::store::rocksdb::write_transaction_impl::renew ()
{
::rocksdb::OptimisticTransactionOptions txn_options;
::rocksdb::TransactionOptions txn_options;
txn_options.set_snapshot = true;
db->BeginTransaction (::rocksdb::WriteOptions (), txn_options, txn);
active = true;
Expand Down
6 changes: 3 additions & 3 deletions nano/store/rocksdb/transaction_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@

#include <rocksdb/db.h>
#include <rocksdb/options.h>
#include <rocksdb/utilities/optimistic_transaction_db.h>
#include <rocksdb/utilities/transaction.h>
#include <rocksdb/utilities/transaction_db.h>

namespace nano::store::rocksdb
{
Expand All @@ -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::OptimisticTransactionDB * 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, 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 ();
void commit () override;
void renew () override;
Expand All @@ -35,7 +35,7 @@ class write_transaction_impl final : public store::write_transaction_impl

private:
::rocksdb::Transaction * txn;
::rocksdb::OptimisticTransactionDB * db;
::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;
Expand Down

0 comments on commit ebf31fa

Please sign in to comment.