From 1733d5a43f95b9e48b0b198d44bc0a34ad0e55ed Mon Sep 17 00:00:00 2001 From: yarkin Date: Thu, 28 Mar 2024 13:45:49 +0800 Subject: [PATCH 1/2] Always set geometry parameters when initializing mdbx envs. --- silkworm/node/db/mdbx.cpp | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/silkworm/node/db/mdbx.cpp b/silkworm/node/db/mdbx.cpp index 81582cca..514ea0e5 100644 --- a/silkworm/node/db/mdbx.cpp +++ b/silkworm/node/db/mdbx.cpp @@ -131,14 +131,13 @@ ::mdbx::env_managed open_env(const EnvConfig& config) { } ::mdbx::env_managed::create_parameters cp{}; // Default create parameters - if (!config.shared) { - auto max_map_size = static_cast(config.in_memory ? 128_Mebi : config.max_size); - auto growth_size = static_cast(config.in_memory ? 8_Mebi : config.growth_size); - cp.geometry.make_dynamic(::mdbx::env::geometry::default_value, max_map_size); - cp.geometry.growth_step = growth_size; - if (!db_ondisk_file_size) - cp.geometry.pagesize = static_cast(config.page_size); - } + // We will set create_parameters even when opening existing db so that we can update max_map_size if config changed. + auto max_map_size = static_cast(config.in_memory ? 128_Mebi : config.max_size); + auto growth_size = static_cast(config.in_memory ? 8_Mebi : config.growth_size); + cp.geometry.make_dynamic(::mdbx::env::geometry::default_value, max_map_size); + cp.geometry.growth_step = growth_size; + if (!db_ondisk_file_size) + cp.geometry.pagesize = static_cast(config.page_size); using OP = ::mdbx::env::operate_parameters; OP op{}; // Operational parameters @@ -157,7 +156,8 @@ ::mdbx::env_managed open_env(const EnvConfig& config) { " db has " + human_size(db_page_size)); } - if (!config.shared) { + // Those settings should only need to be set once during creation. + if (config.create) { // C++ bindings don't have setoptions ::mdbx::error::success_or_throw(::mdbx_env_set_option(ret, MDBX_opt_rp_augment_limit, 32_Mebi)); if (!config.readonly) { From ffe51c8e4382a62a697cb11ef566f76444d934c4 Mon Sep 17 00:00:00 2001 From: yarkin Date: Thu, 28 Mar 2024 13:46:55 +0800 Subject: [PATCH 2/2] Open db as readonly for rpc and passing in max_readers options. --- silkworm/silkrpc/daemon.cpp | 3 ++- silkworm/silkrpc/settings.hpp | 1 + 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/silkworm/silkrpc/daemon.cpp b/silkworm/silkrpc/daemon.cpp index e4721fc0..5f6d85b1 100644 --- a/silkworm/silkrpc/daemon.cpp +++ b/silkworm/silkrpc/daemon.cpp @@ -194,9 +194,10 @@ Daemon::Daemon(DaemonSettings settings, std::shared_ptr chain chaindata_env_ = std::make_shared(); silkworm::db::EnvConfig db_config{ .path = settings_.datadir->string() + kChaindataRelativePath, + .readonly = true, .in_memory = true, .shared = true, - .max_readers = kDatabaseMaxReaders}; + .max_readers = settings.max_readers ? *settings.max_readers : kDatabaseMaxReaders}; *chaindata_env_ = silkworm::db::open_env(db_config); } else if (chaindata_env) { // Use the existing chaindata environment diff --git a/silkworm/silkrpc/settings.hpp b/silkworm/silkrpc/settings.hpp index 9cbbbf22..bcde9071 100644 --- a/silkworm/silkrpc/settings.hpp +++ b/silkworm/silkrpc/settings.hpp @@ -37,6 +37,7 @@ struct DaemonSettings { std::optional jwt_secret_file; bool skip_protocol_check{false}; uint64_t rpc_quirk_flag{0}; + std::optional max_readers; }; } // namespace silkworm::rpc