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

Add config option to conditionally enable RocksDB backend #2266

Merged
merged 2 commits into from
Sep 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
32 changes: 32 additions & 0 deletions nano/core_test/block_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,16 @@
#include <nano/node/node.hpp>
#include <nano/secure/versioning.hpp>

#if NANO_ROCKSDB
#include <nano/node/rocksdb/rocksdb.hpp>
#endif

#include <gtest/gtest.h>

#include <fstream>

#include <stdlib.h>

namespace
{
void modify_account_info_to_v13 (nano::mdb_store & store, nano::transaction const & transaction_a, nano::account const & account_a);
Expand Down Expand Up @@ -1840,6 +1846,32 @@ TEST (block_store, reset_renew_existing_transaction)
ASSERT_NE (nullptr, block_existing);
}

TEST (block_store, rocksdb_force_test_env_variable)
{
nano::logger_mt logger;

// Set environment variable
constexpr auto env_var = "TEST_USE_ROCKSDB";
auto value = std::getenv (env_var);

auto store = nano::make_store (logger, nano::unique_path ());

auto mdb_cast = dynamic_cast<nano::mdb_store *> (store.get ());

#if NANO_ROCKSDB
if (value && boost::lexical_cast<int> (value) == 1)
{
ASSERT_NE (boost::polymorphic_downcast<nano::rocksdb_store *> (store.get ()), nullptr);
}
else
{
ASSERT_NE (mdb_cast, nullptr);
}
#else
ASSERT_NE (mdb_cast, nullptr);
#endif
}

namespace
{
// These functions take the latest account_info and create a legacy one so that upgrade tests can be emulated more easily.
Expand Down
4 changes: 4 additions & 0 deletions nano/core_test/core_test_main.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "gtest/gtest.h"

#include <nano/node/common.hpp>
#include <nano/node/logging.hpp>

namespace nano
{
Expand All @@ -13,6 +14,9 @@ GTEST_API_ int main (int argc, char ** argv)
printf ("Running main() from core_test_main.cc\n");
nano::force_nano_test_network ();
nano::node_singleton_memory_pool_purge_guard memory_pool_cleanup_guard;
// Setting up logging so that there aren't any piped to standard output.
nano::logging logging;
logging.init (nano::unique_path ());
testing::InitGoogleTest (&argc, argv);
auto res = RUN_ALL_TESTS ();
nano::cleanup_test_directories_on_exit ();
Expand Down
9 changes: 9 additions & 0 deletions nano/core_test/toml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ TEST (toml, daemon_config_deserialize_defaults)
[node.statistics.log]
[node.statistics.sampling]
[node.websocket]
[node.rocksdb]
[opencl]
[rpc]
[rpc.child_process]
Expand Down Expand Up @@ -235,6 +236,8 @@ TEST (toml, daemon_config_deserialize_defaults)
ASSERT_EQ (conf.node.stat_config.log_headers, defaults.node.stat_config.log_headers);
ASSERT_EQ (conf.node.stat_config.log_counters_filename, defaults.node.stat_config.log_counters_filename);
ASSERT_EQ (conf.node.stat_config.log_samples_filename, defaults.node.stat_config.log_samples_filename);

ASSERT_EQ (conf.node.rocksdb_config.enable, defaults.node.rocksdb_config.enable);
}

TEST (toml, optional_child)
Expand Down Expand Up @@ -466,6 +469,9 @@ TEST (toml, daemon_config_deserialize_no_defaults)
enable = true
port = 999

[node.rocksdb]
enable = true

[opencl]
device = 999
enable = true
Expand Down Expand Up @@ -592,6 +598,8 @@ TEST (toml, daemon_config_deserialize_no_defaults)
ASSERT_NE (conf.node.stat_config.log_headers, defaults.node.stat_config.log_headers);
ASSERT_NE (conf.node.stat_config.log_counters_filename, defaults.node.stat_config.log_counters_filename);
ASSERT_NE (conf.node.stat_config.log_samples_filename, defaults.node.stat_config.log_samples_filename);

ASSERT_NE (conf.node.rocksdb_config.enable, defaults.node.rocksdb_config.enable);
}

/** There should be no required values **/
Expand All @@ -610,6 +618,7 @@ TEST (toml, daemon_config_no_required)
[node.statistics.log]
[node.statistics.sampling]
[node.websocket]
[node.rocksdb]
[opencl]
[rpc]
[rpc.child_process]
Expand Down
2 changes: 2 additions & 0 deletions nano/lib/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ add_library (nano_lib
numbers.cpp
rep_weights.hpp
rep_weights.cpp
rocksdbconfig.hpp
rocksdbconfig.cpp
rpc_handler_interface.hpp
rpcconfig.hpp
rpcconfig.cpp
Expand Down
14 changes: 14 additions & 0 deletions nano/lib/rocksdbconfig.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#include <nano/lib/rocksdbconfig.hpp>
#include <nano/lib/tomlconfig.hpp>

nano::error nano::rocksdb_config::serialize_toml (nano::tomlconfig & toml) const
{
toml.put ("enable", enable, "Whether to use the RocksDB backend for the ledger database\ntype:bool");
return toml.get_error ();
}

nano::error nano::rocksdb_config::deserialize_toml (nano::tomlconfig & toml)
{
toml.get_optional<bool> ("enable", enable);
return toml.get_error ();
}
18 changes: 18 additions & 0 deletions nano/lib/rocksdbconfig.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <nano/lib/errors.hpp>

namespace nano
{
class tomlconfig;

/** Configuration options for RocksDB */
class rocksdb_config final
{
public:
nano::error serialize_toml (nano::tomlconfig & toml_a) const;
nano::error deserialize_toml (nano::tomlconfig & toml_a);

bool enable{ false };
};
}
3 changes: 2 additions & 1 deletion nano/lib/walletconfig.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <nano/crypto_lib/random_pool.hpp>
#include <nano/lib/tomlconfig.hpp>
#include <nano/lib/walletconfig.hpp>

Expand All @@ -7,7 +8,7 @@ nano::wallet_config::wallet_config ()
assert (!wallet.is_zero ());
}

nano::error nano::wallet_config::parse (std::string wallet_a, std::string account_a)
nano::error nano::wallet_config::parse (std::string const & wallet_a, std::string const & account_a)
{
nano::error error;
if (wallet.decode_hex (wallet_a))
Expand Down
4 changes: 1 addition & 3 deletions nano/lib/walletconfig.hpp
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#pragma once

#include <nano/crypto_lib/random_pool.hpp>
#include <nano/lib/errors.hpp>
#include <nano/lib/numbers.hpp>

#include <string>

namespace nano
{
class jsonconfig;
class tomlconfig;

/** Configuration options for the Qt wallet */
Expand All @@ -17,7 +15,7 @@ class wallet_config final
public:
wallet_config ();
/** Update this instance by parsing the given wallet and account */
nano::error parse (std::string wallet_a, std::string account_a);
nano::error parse (std::string const & wallet_a, std::string const & account_a);
nano::error serialize_toml (nano::tomlconfig & toml_a) const;
nano::error deserialize_toml (nano::tomlconfig & toml_a);
nano::uint256_union wallet;
Expand Down
4 changes: 3 additions & 1 deletion nano/nano_node/daemon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,12 @@ void nano_daemon::daemon::run (boost::filesystem::path const & data_path, nano::
auto node (std::make_shared<nano::node> (io_ctx, data_path, alarm, config.node, opencl_work, flags));
if (!node->init_error ())
{
auto database_backend = dynamic_cast<nano::mdb_store *> (node->store_impl.get ()) ? "LMDB" : "RocksDB";
auto network_label = node->network_params.network.get_current_network_as_string ();
std::cout << "Network: " << network_label << ", version: " << NANO_VERSION_STRING << "\n"
<< "Path: " << node->application_path.string () << "\n"
<< "Build Info: " << BUILD_INFO << std::endl;
<< "Build Info: " << BUILD_INFO << "\n"
<< "Database backend: " << database_backend << std::endl;

node->start ();
nano::ipc::ipc_server ipc_server (*node, config.rpc);
Expand Down
35 changes: 31 additions & 4 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ alarm (alarm_a),
work (work_a),
distributed_work (*this),
logger (config_a.logging.min_time_between_log_output),
store_impl (nano::make_store (logger, application_path_a, flags.read_only, true, config_a.diagnostics_config.txn_tracking, config_a.block_processor_batch_max_time, config_a.lmdb_max_dbs, flags.sideband_batch_size, config_a.backup_before_upgrade)),
store_impl (nano::make_store (logger, application_path_a, flags.read_only, true, config_a.diagnostics_config.txn_tracking, config_a.block_processor_batch_max_time, config_a.lmdb_max_dbs, flags.sideband_batch_size, config_a.backup_before_upgrade, config_a.rocksdb_config.enable)),
store (*store_impl),
wallets_store_impl (std::make_unique<nano::mdb_wallets_store> (application_path_a / "wallets.ldb", config_a.lmdb_max_dbs)),
wallets_store (*wallets_store_impl),
Expand Down Expand Up @@ -1350,11 +1350,38 @@ nano::node_flags const & nano::inactive_node_flag_defaults ()
return node_flags;
}

std::unique_ptr<nano::block_store> nano::make_store (nano::logger_mt & logger, boost::filesystem::path const & path, bool read_only, bool add_db_postfix, nano::txn_tracking_config const & txn_tracking_config_a, std::chrono::milliseconds block_processor_batch_max_time_a, int lmdb_max_dbs, size_t batch_size, bool backup_before_upgrade)
std::unique_ptr<nano::block_store> nano::make_store (nano::logger_mt & logger, boost::filesystem::path const & path, bool read_only, bool add_db_postfix, nano::txn_tracking_config const & txn_tracking_config_a, std::chrono::milliseconds block_processor_batch_max_time_a, int lmdb_max_dbs, size_t batch_size, bool backup_before_upgrade, bool use_rocksdb_backend)
{
#if NANO_ROCKSDB
return std::make_unique<nano::rocksdb_store> (logger, add_db_postfix ? path / "rocksdb" : path, read_only);
auto make_rocksdb = [&logger, add_db_postfix, &path, read_only]() {
return std::make_unique<nano::rocksdb_store> (logger, add_db_postfix ? path / "rocksdb" : path, read_only);
};
#endif

if (use_rocksdb_backend)
{
#if NANO_ROCKSDB
return make_rocksdb ();
#else
return std::make_unique<nano::mdb_store> (logger, add_db_postfix ? path / "data.ldb" : path, txn_tracking_config_a, block_processor_batch_max_time_a, lmdb_max_dbs, batch_size, backup_before_upgrade);
// Can only use the rocksdb_store if the node has been build with rocksdb support
release_assert (false);
return nullptr;
#endif
}
else
{
#if NANO_ROCKSDB
/** To use RocksDB in tests make sure the node is built with the cmake variable -DNANO_ROCKSDB=ON and the environment variable TEST_USE_ROCKSDB=1 is set */
static nano::network_constants network_constants;
if (auto use_rocksdb_str = std::getenv ("TEST_USE_ROCKSDB") && network_constants.is_test_network ())
{
if (boost::lexical_cast<int> (use_rocksdb_str) == 1)
{
return make_rocksdb ();
}
}
#endif
}

return std::make_unique<nano::mdb_store> (logger, add_db_postfix ? path / "data.ldb" : path, txn_tracking_config_a, block_processor_batch_max_time_a, lmdb_max_dbs, batch_size, backup_before_upgrade);
}
11 changes: 11 additions & 0 deletions nano/node/nodeconfig.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <nano/crypto_lib/random_pool.hpp>
#include <nano/lib/config.hpp>
#include <nano/lib/jsonconfig.hpp>
#include <nano/lib/rocksdbconfig.hpp>
#include <nano/lib/rpcconfig.hpp>
#include <nano/lib/tomlconfig.hpp>
#include <nano/node/nodeconfig.hpp>
Expand Down Expand Up @@ -144,6 +145,10 @@ nano::error nano::node_config::serialize_toml (nano::tomlconfig & toml) const
stat_config.serialize_toml (stat_l);
toml.put_child ("statistics", stat_l);

nano::tomlconfig rocksdb_l;
rocksdb_config.serialize_toml (rocksdb_l);
toml.put_child ("rocksdb", rocksdb_l);

return toml.get_error ();
}

Expand Down Expand Up @@ -189,6 +194,12 @@ nano::error nano::node_config::deserialize_toml (nano::tomlconfig & toml)
stat_config.deserialize_toml (stat_config_l);
}

if (toml.has_key ("rocksdb"))
{
auto rocksdb_config_l (toml.get_required_child ("rocksdb"));
rocksdb_config.deserialize_toml (rocksdb_config_l);
}

if (toml.has_key ("work_peers"))
{
work_peers.clear ();
Expand Down
3 changes: 3 additions & 0 deletions nano/node/nodeconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <nano/lib/errors.hpp>
#include <nano/lib/jsonconfig.hpp>
#include <nano/lib/numbers.hpp>
#include <nano/lib/rocksdbconfig.hpp>
#include <nano/lib/stats.hpp>
#include <nano/node/ipcconfig.hpp>
#include <nano/node/logging.hpp>
Expand Down Expand Up @@ -93,6 +94,8 @@ class node_config
std::chrono::seconds work_watcher_period{ std::chrono::seconds (5) };
double max_work_generate_multiplier{ 64. };
uint64_t max_work_generate_difficulty{ nano::network_constants::publish_full_threshold };
nano::rocksdb_config rocksdb_config;

nano::frontiers_confirmation_mode frontiers_confirmation{ nano::frontiers_confirmation_mode::automatic };
std::string serialize_frontiers_confirmation (nano::frontiers_confirmation_mode) const;
nano::frontiers_confirmation_mode deserialize_frontiers_confirmation (std::string const &);
Expand Down
2 changes: 1 addition & 1 deletion nano/secure/blockstore.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -695,7 +695,7 @@ class block_store
virtual nano::read_transaction tx_begin_read () = 0;
};

std::unique_ptr<nano::block_store> make_store (nano::logger_mt & logger, boost::filesystem::path const & path, bool open_read_only = false, bool add_db_postfix = false, nano::txn_tracking_config const & txn_tracking_config_a = nano::txn_tracking_config{}, std::chrono::milliseconds block_processor_batch_max_time_a = std::chrono::milliseconds (5000), int lmdb_max_dbs = 128, size_t batch_size = 512, bool backup_before_upgrade = false);
std::unique_ptr<nano::block_store> make_store (nano::logger_mt & logger, boost::filesystem::path const & path, bool open_read_only = false, bool add_db_postfix = false, nano::txn_tracking_config const & txn_tracking_config_a = nano::txn_tracking_config{}, std::chrono::milliseconds block_processor_batch_max_time_a = std::chrono::milliseconds (5000), int lmdb_max_dbs = 128, size_t batch_size = 512, bool backup_before_upgrade = false, bool rocksdb_backend = false);
}

namespace std
Expand Down