diff --git a/src/ledger/ledger.cpp b/src/ledger/ledger.cpp index 0a5159b2..5632a5a9 100644 --- a/src/ledger/ledger.cpp +++ b/src/ledger/ledger.cpp @@ -257,19 +257,17 @@ namespace ledger // Remove old shards if this is not a full history node. if (conf::cfg.node.history == conf::HISTORY::CUSTOM) { - const std::string shard_dir_path = ledger_fs.physical_path(hpfs::RW_SESSION_NAME, shard_parent_dir); - std::list shards = util::fetch_dir_entries(shard_dir_path); - for (const std::string shard : shards) + for (int i = led_shard_no - 1; i >= 0; i--) { - uint64_t primary_shard_seq_no; - util::stoull(shard, primary_shard_seq_no); - if (primary_shard_seq_no < led_shard_no) + const std::string shard_path = std::string(ledger_fs.physical_path(hpfs::RW_SESSION_NAME, shard_parent_dir)).append("/").append(std::to_string(i)); + // Break the loop if there's no corresponding shard. + // There cannot be shards which is less than this shard no since shards are continous. + if (!util::is_dir_exists(shard_path)) + break; + else if (util::remove_directory_recursively(shard_path) == -1) { - const std::string shard_path = std::string(shard_dir_path).append("/").append(shard); - if (util::is_dir_exists(shard_path) && util::remove_directory_recursively(shard_path) == -1) - { - LOG_ERROR << errno << ": Error deleting shard: " << shard; - } + LOG_ERROR << errno << ": Error deleting shard: " << i; + break; } } } @@ -431,7 +429,7 @@ namespace ledger if (last_primary_shard_id.seq_no == 0 && last_primary_shard_id.hash == util::h32_empty) { - // This is the genesis ledger. + // This is the genesis ledger. ctx.set_lcl_id(p2p::sequence_hash{0, util::h32_empty}); return 0; } @@ -503,7 +501,6 @@ namespace ledger return 0; } - /** * Update max_shard.seq_no meta file with the given latest shard sequence number which can be used to identify last shard * sequence number in startup. diff --git a/src/ledger/ledger_mount.cpp b/src/ledger/ledger_mount.cpp index fbc4a478..1c8b108e 100644 --- a/src/ledger/ledger_mount.cpp +++ b/src/ledger/ledger_mount.cpp @@ -12,18 +12,38 @@ namespace ledger // Add ledger fs preparation logic here. p2p::sequence_hash last_primary_shard_id; p2p::sequence_hash last_blob_shard_id; - constexpr const char *session_name = "ro_ledger_prepare_fs"; - if (start_ro_session(session_name, true) == -1 || - get_last_shard_info(session_name, last_primary_shard_id, PRIMARY_DIR) == -1 || - get_last_ledger_and_update_context(session_name, last_primary_shard_id) == -1 || - get_last_shard_info(session_name, last_blob_shard_id, BLOB_DIR) == -1 || - stop_ro_session(session_name) == -1) + if (acquire_rw_session() == -1) + { + LOG_ERROR << "Failed to acquire rw session at mount " << mount_dir << "."; + return -1; + } + + if (get_last_shard_info(hpfs::RW_SESSION_NAME, last_primary_shard_id, PRIMARY_DIR) == -1 || + get_last_ledger_and_update_context(hpfs::RW_SESSION_NAME, last_primary_shard_id) == -1 || + get_last_shard_info(hpfs::RW_SESSION_NAME, last_blob_shard_id, BLOB_DIR) == -1) { LOG_ERROR << "Failed to prepare initial fs at mount " << mount_dir << "."; return -1; } + if (conf::cfg.node.history == conf::HISTORY::CUSTOM) + { + //Remove old primary shards that exceeds max shard range. + if (last_primary_shard_id.seq_no >= conf::cfg.node.history_config.max_primary_shards) + remove_old_shards(last_primary_shard_id.seq_no - conf::cfg.node.history_config.max_primary_shards + 1, PRIMARY_DIR); + + //Remove old blob shards that exceeds max shard range. + if (last_blob_shard_id.seq_no >= conf::cfg.node.history_config.max_blob_shards) + remove_old_shards(last_blob_shard_id.seq_no - conf::cfg.node.history_config.max_blob_shards + 1, BLOB_DIR); + } + + if (release_rw_session() == -1) + { + LOG_ERROR << "Failed to release rw session at mount " << mount_dir << "."; + return -1; + } + LOG_INFO << "Initial primary: " << last_primary_shard_id << " | blob: " << last_blob_shard_id; // Update last shard hash and shard number tracker. diff --git a/src/ledger/ledger_sync.cpp b/src/ledger/ledger_sync.cpp index 1933b9ea..7d4ba900 100644 --- a/src/ledger/ledger_sync.cpp +++ b/src/ledger/ledger_sync.cpp @@ -54,6 +54,11 @@ namespace ledger LOG_ERROR << "Error updating max shard meta file in primary shard sync."; return; } + + // If existing max shard is older than the max we can keep. Then delete all the existing shards. + if (conf::cfg.node.history == conf::HISTORY::CUSTOM && synced_shard_seq_no - last_primary_shard_seq_no >= conf::cfg.node.history_config.max_primary_shards) + remove_old_shards(last_primary_shard_seq_no + 1, PRIMARY_DIR); + const p2p::sequence_hash updated_primary_shard_id{synced_shard_seq_no, synced_target.hash}; if (get_last_ledger_and_update_context(hpfs::RW_SESSION_NAME, updated_primary_shard_id) == -1) { @@ -104,6 +109,10 @@ namespace ledger return; } + // If existing max shard is older than the max we can keep. Then delete all the existing shards. + if (conf::cfg.node.history == conf::HISTORY::CUSTOM && synced_shard_seq_no - last_blob_shard_seq_no >= conf::cfg.node.history_config.max_blob_shards) + remove_old_shards(last_blob_shard_seq_no + 1, BLOB_DIR); + last_blob_shard_seq_no = synced_shard_seq_no; ctx.set_last_blob_shard_id(p2p::sequence_hash{synced_shard_seq_no, synced_target.hash}); }