Skip to content

Commit

Permalink
chore: prune state_db manually
Browse files Browse the repository at this point in the history
  • Loading branch information
mfrankovi committed Apr 17, 2023
1 parent b58eb48 commit e295ed3
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 28 deletions.
1 change: 1 addition & 0 deletions libraries/cli/include/cli/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class Config {
static constexpr const char* HELP = "help";
static constexpr const char* VERSION = "version";
static constexpr const char* WALLET = "wallet";
static constexpr const char* PRUNE_STATE_DB = "prune-state-db";

static constexpr const char* NODE_COMMAND = "node";
static constexpr const char* ACCOUNT_COMMAND = "account";
Expand Down
4 changes: 4 additions & 0 deletions libraries/cli/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ Config::Config(int argc, const char* argv[]) {
bool destroy_db = false;
bool rebuild_network = false;
bool rebuild_db = false;
bool prune_state_db = false;

bool light_node = false;
bool version = false;
uint64_t rebuild_db_period = 0;
Expand Down Expand Up @@ -129,6 +131,7 @@ Config::Config(int argc, const char* argv[]) {
"Enables Test JsonRPC. Disabled by default");
node_command_options.add_options()(ENABLE_DEBUG, bpo::bool_switch(&enable_debug),
"Enables Debug RPC interface. Disabled by default");
node_command_options.add_options()(PRUNE_STATE_DB, bpo::bool_switch(&prune_state_db), "Prune state_db");

allowed_options.add(main_options);

Expand Down Expand Up @@ -261,6 +264,7 @@ Config::Config(int argc, const char* argv[]) {
}
node_config_.db_config.db_revert_to_period = revert_to_period;
node_config_.db_config.rebuild_db = rebuild_db;
node_config_.db_config.prune_state_db = prune_state_db;
node_config_.db_config.rebuild_db_period = rebuild_db_period;

node_config_.enable_test_rpc = enable_test_rpc;
Expand Down
1 change: 1 addition & 0 deletions libraries/config/include/config/config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ struct DBConfig {
uint32_t db_max_open_files = 0;
PbftPeriod db_revert_to_period = 0;
bool rebuild_db = false;
bool prune_state_db = false;
PbftPeriod rebuild_db_period = 0;
};

Expand Down
12 changes: 5 additions & 7 deletions libraries/config/src/config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,11 @@ FullNodeConfig::FullNodeConfig(const Json::Value &string_or_object, const Json::
}

is_light_node = getConfigDataAsBoolean(root, {"is_light_node"}, true, is_light_node);
if (is_light_node) {
const auto min_light_node_history = (genesis.state.dpos.blocks_per_year * kDefaultLightNodeHistoryDays) / 365;
light_node_history = getConfigDataAsUInt(root, {"light_node_history"}, true, min_light_node_history);
if (light_node_history < min_light_node_history) {
throw ConfigException("Min. required light node history is " + std::to_string(min_light_node_history) +
" blocks (" + std::to_string(kDefaultLightNodeHistoryDays) + " days)");
}
const auto min_light_node_history = (genesis.state.dpos.blocks_per_year * kDefaultLightNodeHistoryDays) / 365;
light_node_history = getConfigDataAsUInt(root, {"light_node_history"}, true, min_light_node_history);
if (light_node_history < min_light_node_history) {
throw ConfigException("Min. required light node history is " + std::to_string(min_light_node_history) +
" blocks (" + std::to_string(kDefaultLightNodeHistoryDays) + " days)");
}

try {
Expand Down
28 changes: 7 additions & 21 deletions libraries/core_libs/consensus/src/final_chain/final_chain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,9 @@ class FinalChainImpl final : public FinalChain {
const bool kLightNode = false;
const uint64_t kLightNodeHistory = 0;
const uint32_t kMaxLevelsPerPeriod;
const uint64_t kLightNodePruneOffset = 0;

// It is not prepared to use more then 1 thread. Examine it if you want to change threads count
boost::asio::thread_pool executor_thread_{1};
boost::asio::thread_pool prune_thread_{1};

std::atomic<uint64_t> num_executed_dag_blk_ = 0;
std::atomic<uint64_t> num_executed_trx_ = 0;
Expand Down Expand Up @@ -61,10 +59,6 @@ class FinalChainImpl final : public FinalChain {
kLightNode(config.is_light_node),
kLightNodeHistory(config.light_node_history),
kMaxLevelsPerPeriod(config.max_levels_per_period),
// This will provide a speific random offset based on node address for each node to prevent all light nodes
// performing prune at the same block height
kLightNodePruneOffset((*reinterpret_cast<uint32_t*>(node_addr.asBytes().data())) %
std::max(config.light_node_history, (uint64_t)1)),
block_headers_cache_(config.final_chain_cache_in_blocks,
[this](uint64_t blk) { return get_block_header(blk); }),
block_hashes_cache_(config.final_chain_cache_in_blocks, [this](uint64_t blk) { return get_block_hash(blk); }),
Expand Down Expand Up @@ -125,12 +119,14 @@ class FinalChainImpl final : public FinalChain {
}

delegation_delay_ = config.genesis.state.dpos.delegation_delay;
if (config.db_config.prune_state_db && last_blk_num.has_value() && *last_blk_num > kLightNodeHistory) {
LOG(log_si_) << "Pruning state db, this might take several minutes";
prune(*last_blk_num - kLightNodeHistory);
LOG(log_si_) << "Pruning state db complete";
}
}

void stop() override {
executor_thread_.join();
prune_thread_.join();
}
void stop() override { executor_thread_.join(); }

std::future<std::shared_ptr<const FinalizationResult>> finalize(
PeriodData&& new_blk, std::vector<h256>&& finalized_dag_blk_hashes,
Expand Down Expand Up @@ -246,13 +242,6 @@ class FinalChainImpl final : public FinalChain {
state_api_.create_snapshot(blk_header->number);
}

if (kLightNode) {
// Actual history size will be between 100% and 105% of light_node_history_ to avoid deleting on every period
if ((((blk_header->number + kLightNodePruneOffset) % (std::max(kLightNodeHistory / 20, (uint64_t)1)) == 0)) &&
blk_header->number > kLightNodeHistory) {
prune(blk_header->number - kLightNodeHistory);
}
}
return result;
}

Expand All @@ -274,10 +263,7 @@ class FinalChainImpl final : public FinalChain {
db_->compactColumn(DB::Columns::final_chain_blk_hash_by_number);
db_->compactColumn(DB::Columns::final_chain_blk_number_by_hash);

boost::asio::post(
prune_thread_,
[this, to_keep = std::move(last_block_to_keep->state_root), to_prune = std::move(state_root_to_prune),
number = last_block_to_keep->number]() { state_api_.prune(to_keep, to_prune, number); });
state_api_.prune(last_block_to_keep->state_root, state_root_to_prune, last_block_to_keep->number);
}
}

Expand Down

0 comments on commit e295ed3

Please sign in to comment.