Skip to content

Commit

Permalink
Added database_backend config setting
Browse files Browse the repository at this point in the history
  • Loading branch information
RickiNano committed Jan 19, 2025
1 parent 2bd47b6 commit d7b055c
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 3 deletions.
3 changes: 1 addition & 2 deletions nano/lib/rocksdbconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

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");
toml.put ("io_threads", io_threads, "Number of threads to use with the background compaction and flushing.\ntype:uint32");
toml.put ("read_cache", read_cache, "Amount of megabytes per table allocated to read cache. Valid range is 1 - 1024. Default is 32.\nCarefully monitor memory usage if non-default values are used\ntype:long");
toml.put ("write_cache", write_cache, "Total amount of megabytes allocated to write cache. Valid range is 1 - 256. Default is 64.\nCarefully monitor memory usage if non-default values are used\ntype:long");
Expand All @@ -14,7 +13,7 @@ nano::error nano::rocksdb_config::serialize_toml (nano::tomlconfig & toml) const

nano::error nano::rocksdb_config::deserialize_toml (nano::tomlconfig & toml)
{
toml.get_optional<bool> ("enable", enable);
toml.get_optional<bool> ("enable", enable); // TODO: This setting can be removed in future versions
toml.get_optional<unsigned> ("io_threads", io_threads);
toml.get_optional<long> ("read_cache", read_cache);
toml.get_optional<long> ("write_cache", write_cache);
Expand Down
9 changes: 8 additions & 1 deletion nano/node/make_store.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,17 @@

std::unique_ptr<nano::store::component> nano::make_store (nano::logger & logger, std::filesystem::path const & path, nano::ledger_constants & constants, bool read_only, bool add_db_postfix, nano::node_config const & node_config)
{
if (node_config.rocksdb_config.enable)
if (node_config.database_backend == nano::database_backend::rocksdb)
{
return std::make_unique<nano::store::rocksdb::component> (logger, add_db_postfix ? path / "rocksdb" : path, constants, node_config.rocksdb_config, read_only);
}
if (node_config.rocksdb_config.enable && node_config.database_backend == nano::database_backend::lmdb)
{
// rocksdb.enable is true in config, but database_backend is set to LMDB in config
logger.critical (nano::log::type::config, "Legacy RocksDb setting detected in config file.");
logger.info (nano::log::type::config, "Edit node_config.toml and use 'database_backend' in the node section to re-enable RocksDb");
std::exit (EXIT_FAILURE);
}

return std::make_unique<nano::store::lmdb::component> (logger, add_db_postfix ? path / "data.ldb" : path, constants, node_config.diagnostics_config.txn_tracking, node_config.block_processor_batch_max_time, node_config.lmdb_config, node_config.backup_before_upgrade);
}
31 changes: 31 additions & 0 deletions nano/node/nodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,8 @@ nano::error nano::node_config::deserialize_toml (nano::tomlconfig & toml)
toml.get<bool> ("allow_local_peers", allow_local_peers);
toml.get<unsigned> (signature_checker_threads_key, signature_checker_threads);

database_backend = get_database_backend (toml);

if (toml.has_key ("lmdb"))
{
auto lmdb_config_l (toml.get_required_child ("lmdb"));
Expand Down Expand Up @@ -659,6 +661,35 @@ void nano::node_config::deserialize_address (std::string const & entry_a, std::v
}
}

std::string nano::node_config::serialize_database_backend (nano::database_backend mode_a) const
{
switch (mode_a)
{
case nano::database_backend::rocksdb:
return "rocksdb";
case nano::database_backend::lmdb:
return "lmdb";
}
debug_assert (false);
}

nano::database_backend nano::node_config::get_database_backend (nano::tomlconfig & toml)
{
if (toml.has_key ("database_backend"))
{
auto backend_string (toml.get<std::string> ("database_backend"));
if (backend_string == "rocksdb")
return database_backend::rocksdb;
if (backend_string == "lmdb")
return database_backend::lmdb;

toml.get_error ().set ("Unknown database_backend type: " + backend_string);
return nano::database_backend::lmdb;
}
// Default to LMDB
return nano::database_backend::lmdb;
}

nano::account nano::node_config::random_representative () const
{
debug_assert (!preconfigured_representatives.empty ());
Expand Down
9 changes: 9 additions & 0 deletions nano/node/nodeconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,12 @@ namespace nano
{
class tomlconfig;

enum class database_backend
{
lmdb,
rocksdb
};

/**
* Node configuration
*/
Expand Down Expand Up @@ -133,6 +139,7 @@ class node_config
uint64_t max_pruning_depth{ 0 };
nano::rocksdb_config rocksdb_config;
nano::lmdb_config lmdb_config;
nano::database_backend database_backend{ std::string (std::getenv ("BACKEND") ? std::getenv ("BACKEND") : "") == "rocksdb" ? nano::database_backend::rocksdb : nano::database_backend::lmdb };
bool enable_upnp{ true };
std::size_t max_ledger_notifications{ 8 };

Expand All @@ -156,6 +163,8 @@ class node_config
public:
/** Entry is ignored if it cannot be parsed as a valid address:port */
void deserialize_address (std::string const &, std::vector<std::pair<std::string, uint16_t>> &) const;
std::string serialize_database_backend (nano::database_backend) const;
nano::database_backend get_database_backend (nano::tomlconfig & toml);

private:
static std::optional<unsigned> env_io_threads ();
Expand Down

0 comments on commit d7b055c

Please sign in to comment.