Skip to content
This repository has been archived by the owner on Aug 2, 2022. It is now read-only.

Commit

Permalink
Allows extract block log with filename base
Browse files Browse the repository at this point in the history
  • Loading branch information
huangminghuang committed Feb 15, 2021
1 parent ccdb117 commit 31c489c
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 15 deletions.
17 changes: 9 additions & 8 deletions libraries/chain/block_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -398,9 +398,7 @@ namespace eosio { namespace chain {
block_log_data log_data;
block_log_index log_index;

block_log_bundle(fc::path block_dir) {
block_file_name = block_dir / "blocks.log";
index_file_name = block_dir / "blocks.index";
block_log_bundle(fc::path block_file_name, fc::path index_file_name) {

log_data.open(block_file_name);
log_index.open(index_file_name);
Expand All @@ -413,6 +411,9 @@ namespace eosio { namespace chain {
"${block_file_name} says it has ${log_num_blocks} blocks which disagrees with ${index_num_blocks} indicated by ${index_file_name}",
("block_file_name", block_file_name)("log_num_blocks", log_num_blocks)("index_num_blocks", index_num_blocks)("index_file_name", index_file_name));
}

block_log_bundle(fc::path block_dir) : block_log_bundle(block_dir / "blocks.log", block_dir / "blocks.index") {
}
};

/// Used to traverse the block position (i.e. the last 8 bytes in each block log entry) of the blocks.log file
Expand Down Expand Up @@ -1233,10 +1234,10 @@ namespace eosio { namespace chain {
return std::make_pair(new_block_filename, new_index_filename);
}

void block_log::extract_blocklog(const fc::path& block_dir, const fc::path& dest_dir, uint32_t start_block_num,
void block_log::extract_blocklog(const fc::path& log_filename, const fc::path& index_filename, const fc::path& dest_dir, uint32_t start_block_num,
uint32_t num_blocks) {

block_log_bundle log_bundle(block_dir);
block_log_bundle log_bundle(log_filename, index_filename);

EOS_ASSERT(start_block_num >= log_bundle.log_data.first_block_num(), block_log_exception,
"The first available block is block ${first_block}.", ("first_block", log_bundle.log_data.first_block_num()));
Expand All @@ -1261,10 +1262,10 @@ namespace eosio { namespace chain {
if (!fc::exists(dest_dir))
fc::create_directories(dest_dir);

for (uint32_t i = first_block_num / stride;
i < last_block_num / stride + 1; ++i) {
for (uint32_t i = (first_block_num - 1) / stride;
i < (last_block_num + stride - 1)/stride; ++i) {
uint32_t start_block_num = std::max(i * stride + 1, first_block_num);
uint32_t num_blocks = std::min(stride, last_block_num - start_block_num + 1);
uint32_t num_blocks = std::min( (i+1)*stride, last_block_num) - start_block_num + 1;

auto [new_block_filename, new_index_filename] = blocklog_files(dest_dir, start_block_num, num_blocks);

Expand Down
4 changes: 2 additions & 2 deletions libraries/chain/include/eosio/chain/block_log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ namespace eosio { namespace chain {
*/
static void smoke_test(fc::path block_dir, uint32_t n);

static void extract_blocklog(const fc::path& block_dir, const fc::path& dest_dir, uint32_t start_block,
uint32_t num_blocks);
static void extract_blocklog(const fc::path& log_filename, const fc::path& index_filename,
const fc::path& dest_dir, uint32_t start_block, uint32_t num_blocks);
static void split_blocklog(const fc::path& block_dir, const fc::path& dest_dir, uint32_t stride);

private:
Expand Down
30 changes: 26 additions & 4 deletions programs/eosio-blocklog/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,9 @@ void blocklog::set_program_options(options_description& cli)
cli.add_options()
("blocks-dir", bpo::value<bfs::path>()->default_value("blocks"),
"the location of the blocks directory (absolute path or relative to the current directory)")
("blocks-filebase", bpo::value<bfs::path>()->default_value("blocks"),
"the name of the blocks log/index files without file extension (absolute path or relative to the current directory)."
" This can only used for extract-blocklog.")
("state-history-dir", bpo::value<bfs::path>()->default_value("state-history"),
"the location of the state-history directory (absolute path or relative to the current dir)")
("output-file,o", bpo::value<bfs::path>(),
Expand Down Expand Up @@ -209,7 +212,7 @@ void blocklog::set_program_options(options_description& cli)
"split the block log file based on the stride and store the result in the specified 'output-dir'.")
("extract-blocklog", bpo::bool_switch(&extract_blocklog)->default_value(false),
"Extract blocks from blocks.log and blocks.index and keep the original."
" Must give 'blocks-dir','output-dir', 'first and 'last'.")
" Must give 'blocks-dir' or 'blocks-filebase','output-dir', 'first' and 'last'.")
("help,h", bpo::bool_switch(&help)->default_value(false), "Print this help message and exit.")
;
}
Expand Down Expand Up @@ -296,6 +299,9 @@ int prune_transactions(bfs::path block_dir, bfs::path state_history_dir, uint32_
prune_transactions<state_history_traces_log>("state history traces log", state_history_dir, block_num, ids);
}

inline bfs::path operator+(bfs::path left, bfs::path right){return bfs::path(left)+=right;}


int main(int argc, char** argv) {
std::ios::sync_with_stdio(false); // for potential performance boost for large block log files
options_description cli ("eosio-blocklog command line options");
Expand All @@ -311,8 +317,9 @@ int main(int argc, char** argv) {
return 0;
}

const auto blocks_dir = vmap["blocks-dir"].as<bfs::path>();
if (!block_log::exists(blocks_dir)) {
const auto blocks_dir = vmap["blocks-dir"].as<bfs::path>();

if (!blog.extract_blocklog && !block_log::exists(blocks_dir)) {
std::cerr << "The specified blocks-dir must contain blocks.log and blocks.index files";
return -1;
}
Expand Down Expand Up @@ -377,11 +384,26 @@ int main(int argc, char** argv) {
}

if (blog.extract_blocklog) {

if (blog.first_block == 0 && blog.last_block == std::numeric_limits<uint32_t>::max()) {
std::cerr << "extract_blocklog does nothing unless specify first and/or last block.";
}

bfs::path blocks_filebase = vmap["blocks-filebase"].as<bfs::path>();
if (blocks_filebase.empty() && !blocks_dir.empty()) {
blocks_filebase = blocks_dir / "blocks";
}

bfs::path log_filename = blocks_filebase + ".log";
bfs::path index_filename = blocks_filebase + ".index";

if (!bfs::exists(log_filename) || !bfs::exists(index_filename)){
std::cerr << "Both "<< log_filename << " and " << index_filename << " must exist";
return -1;
}
block_log::extract_blocklog(blocks_dir, output_dir, blog.first_block, blog.last_block-blog.first_block+1);

block_log::extract_blocklog(log_filename, index_filename, output_dir, blog.first_block,
blog.last_block - blog.first_block + 1);
return 0;
}

Expand Down
27 changes: 26 additions & 1 deletion unittests/restart_chain_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ BOOST_AUTO_TEST_CASE(test_split_log) {
BOOST_CHECK( ! chain.control->fetch_block_by_number(160));
}

BOOST_AUTO_TEST_CASE(test_split_log_util) {
BOOST_AUTO_TEST_CASE(test_split_log_util1) {
namespace bfs = boost::filesystem;
fc::temp_directory temp_dir;

Expand Down Expand Up @@ -621,4 +621,29 @@ BOOST_AUTO_TEST_CASE(test_trim_blocklog_front_v3) {
trim_blocklog_front(3);
}

BOOST_AUTO_TEST_CASE(test_split_log_util2) {
namespace bfs = boost::filesystem;


tester chain;
chain.produce_blocks(160);
chain.close();

auto blocks_dir = chain.get_config().blog.log_dir;
auto retained_dir = blocks_dir / "retained";
fc::temp_directory temp_dir;

BOOST_REQUIRE_NO_THROW(block_log::trim_blocklog_front(blocks_dir, temp_dir.path(), 50));
BOOST_REQUIRE_NO_THROW(block_log::trim_blocklog_end(blocks_dir, 150));

BOOST_CHECK_NO_THROW(block_log::split_blocklog(blocks_dir, retained_dir, 50));

BOOST_CHECK(bfs::exists( retained_dir / "blocks-50-50.log" ));
BOOST_CHECK(bfs::exists( retained_dir / "blocks-50-50.index" ));
BOOST_CHECK(bfs::exists( retained_dir / "blocks-51-100.log" ));
BOOST_CHECK(bfs::exists( retained_dir / "blocks-51-100.index" ));
BOOST_CHECK(bfs::exists( retained_dir / "blocks-101-150.log" ));
BOOST_CHECK(bfs::exists( retained_dir / "blocks-101-150.index" ));
}

BOOST_AUTO_TEST_SUITE_END()

0 comments on commit 31c489c

Please sign in to comment.