Skip to content

Commit

Permalink
Improved readability of block log artifacts creation messages
Browse files Browse the repository at this point in the history
  • Loading branch information
Lucius authored and vogel76 committed Jan 8, 2025
1 parent 0db3ce1 commit 3c68b28
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 13 deletions.
8 changes: 6 additions & 2 deletions libraries/chain/block_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ namespace hive { namespace chain {
close();

my->block_file = file;
bool file_existed = fc::exists(file);

{
const fc::path parent_path = my->block_file.parent_path();
Expand All @@ -215,7 +216,7 @@ namespace hive { namespace chain {
if (read_only)
{
flags = O_RDONLY | O_CLOEXEC;
if(not fc::exists(file) && write_fallback)
if(not file_existed && write_fallback)
{
int temp_flags = flags | O_CREAT;
int temp_fd = ::open(file_str.c_str(), temp_flags, 0644);
Expand Down Expand Up @@ -283,7 +284,10 @@ namespace hive { namespace chain {
}

if (auto_open_artifacts)
my->_artifacts = block_log_artifacts::open(file, *this, read_only, write_fallback, false, theApp, thread_pool);
my->_artifacts = block_log_artifacts::open(file, *this, read_only, write_fallback,
false /*full_match_verification*/,
not file_existed /*log_file_created*/,
theApp, thread_pool);
}

void block_log::close()
Expand Down
35 changes: 27 additions & 8 deletions libraries/chain/block_log_artifacts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,10 @@ class block_log_artifacts::impl final

impl( appbase::application& app );

void open(const fc::path& block_log_file_path, const block_log& source_block_provider, const bool read_only, const bool write_fallback, const bool full_match_verification, hive::chain::blockchain_worker_thread_pool& thread_pool);
void open(const fc::path& block_log_file_path, const block_log& source_block_provider,
const bool read_only, const bool write_fallback, const bool full_match_verification,
const bool new_block_log_created,
hive::chain::blockchain_worker_thread_pool& thread_pool);

fc::path get_artifacts_file() const
{
Expand Down Expand Up @@ -318,7 +321,10 @@ block_log_artifacts::impl::impl( appbase::application& app ): theApp( app )

}

void block_log_artifacts::impl::open(const fc::path& block_log_file_path, const block_log& source_block_provider, const bool read_only, const bool write_fallback, const bool full_match_verification, hive::chain::blockchain_worker_thread_pool& thread_pool)
void block_log_artifacts::impl::open(const fc::path& block_log_file_path,
const block_log& source_block_provider, const bool read_only, const bool write_fallback,
const bool full_match_verification, const bool new_block_log_created,
hive::chain::blockchain_worker_thread_pool& thread_pool)
{
try {
set_block_num_to_file_pos_offset(
Expand All @@ -339,17 +345,21 @@ void block_log_artifacts::impl::open(const fc::path& block_log_file_path, const
_storage_fd = ::open(file_str.c_str(), O_RDONLY | O_CLOEXEC, 0644);

auto open_writeable_fallback = [&](bool close_fd, const char* msg){
ilog(msg, (_artifact_file_name));
if(msg != nullptr)
ilog(msg, (_artifact_file_name));
if (close_fd)
close();
open(block_log_file_path, source_block_provider, false /*read_only*/, false /*write_fallback*/, full_match_verification, thread_pool);
open(block_log_file_path, source_block_provider, false /*read_only*/,
false /*write_fallback*/, full_match_verification, new_block_log_created, thread_pool);
};

if (_storage_fd == -1)
{
if (errno == ENOENT && write_fallback)
{
open_writeable_fallback(false/*close_fd*/, "Missing artifacts file ${_artifact_file_name}. Trying creation in read write mode...");
// To avoid confusion warn about missing artifacts only when block log existed earlier.
open_writeable_fallback(false/*close_fd*/,
new_block_log_created ? nullptr : "Missing artifacts file ${_artifact_file_name}. Trying creation in read write mode...");
return;
}

Expand Down Expand Up @@ -416,7 +426,10 @@ void block_log_artifacts::impl::open(const fc::path& block_log_file_path, const
{
if (errno == ENOENT)
{
wlog("Could not find artifacts file in ${_artifact_file_name}. Creating new artifacts file ...", (_artifact_file_name));
// To avoid confusion warn about missing artifacts only when block log existed earlier.
if( not new_block_log_created )
wlog("Could not find artifacts file in ${_artifact_file_name}. Creating new artifacts file ...", (_artifact_file_name));

_storage_fd = ::open(_artifact_file_name.generic_string().c_str(), O_RDWR | O_CREAT | O_CLOEXEC, 0644);

if (_storage_fd == -1)
Expand Down Expand Up @@ -918,10 +931,16 @@ block_log_artifacts::~block_log_artifacts()
_impl->close();
}

block_log_artifacts::block_log_artifacts_ptr_t block_log_artifacts::open(const fc::path& block_log_file_path, const block_log& source_block_provider, const bool read_only, const bool write_fallback, const bool full_match_verification, appbase::application& app, hive::chain::blockchain_worker_thread_pool& thread_pool)
block_log_artifacts::block_log_artifacts_ptr_t block_log_artifacts::open(
const fc::path& block_log_file_path, const block_log& source_block_provider,
const bool read_only, const bool write_fallback, const bool full_match_verification,
const bool new_block_log_created, appbase::application& app,
hive::chain::blockchain_worker_thread_pool& thread_pool)
{
block_log_artifacts_ptr_t block_artifacts(new block_log_artifacts( app ));
block_artifacts->_impl->open(block_log_file_path, source_block_provider, read_only, write_fallback, full_match_verification, thread_pool );
block_artifacts->_impl->open(block_log_file_path, source_block_provider, read_only,
write_fallback, full_match_verification, new_block_log_created,
thread_pool );
return block_artifacts;
}

Expand Down
9 changes: 8 additions & 1 deletion libraries/chain/include/hive/chain/block_log_artifacts.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,11 +69,18 @@ class block_log_artifacts final
* \param read_only - determines if artifacts file are open in read_only mode.
* \param write_fallback - whether to try create/overwrite artifacts file when opening in read only mode failed.
* \param full_match_verification - if true, all artifacts will be checked if they match block_log. Otherwise only small amount of artifacts will be checked if they match block_log.
* \param new_block_log_created - whether corresponding block log file has just been created (and is empty).
* Built instance of `block_log_artifacts` will be automaticaly closed before destruction.
*
* Function throws on any error f.e. related to IO.
*/
static block_log_artifacts_ptr_t open(const fc::path& block_log_file_path, const block_log& source_block_provider, const bool read_only, const bool write_fallback, const bool full_match_verification, appbase::application& app, hive::chain::blockchain_worker_thread_pool& thread_pool);
static block_log_artifacts_ptr_t open(const fc::path& block_log_file_path,
const block_log& source_block_provider,
const bool read_only, const bool write_fallback,
const bool full_match_verification,
const bool new_block_log_created,
appbase::application& app,
hive::chain::blockchain_worker_thread_pool& thread_pool);

fc::path get_artifacts_file() const;

Expand Down
5 changes: 4 additions & 1 deletion programs/util/block_log_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,12 @@ using namespace hive::chain;
void generate_artifacts(const fc::path& block_log_path, appbase::application& app, hive::chain::blockchain_worker_thread_pool& thread_pool)
{
block_log bl( app );
bool block_log_existed = fc::exists(block_log_path);

bl.open(block_log_path, thread_pool, true, false);
auto bla = block_log_artifacts::open(block_log_path, bl, false, false, false, app, thread_pool);
auto bla = block_log_artifacts::open(block_log_path, bl, false /*read_only*/,
false /*write_fallback*/, false /*full_match_verification*/,
not block_log_existed /*new_block_log_created*/, app, thread_pool);
bla.reset();
ilog("open and generation finished...");
}
Expand Down
6 changes: 5 additions & 1 deletion programs/util/block_log_util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,12 +768,16 @@ bool get_block_artifacts(const fc::path &block_log_path, const int32_t first_blo
try
{
hive::chain::block_log block_log(app);
bool block_log_existed = fc::exists(block_log_path);
block_log.open(block_log_path, thread_pool, true, false);
FC_ASSERT(block_log.head(), "Cannot operate on empty block_log");
if (full_match_verification)
ilog("Opening artifacts file with full artifacts match verification ...");

hive::chain::block_log_artifacts::block_log_artifacts_ptr_t artifacts = hive::chain::block_log_artifacts::block_log_artifacts::open(block_log_path, block_log, true, false, full_match_verification, app, thread_pool);
hive::chain::block_log_artifacts::block_log_artifacts_ptr_t artifacts =
hive::chain::block_log_artifacts::block_log_artifacts::open(block_log_path, block_log,
true /*read_only*/, false /*write_fallback*/, full_match_verification,
not block_log_existed /*new_block_log_created*/, app, thread_pool);

if (full_match_verification)
ilog("Artifacts file match verification done");
Expand Down

0 comments on commit 3c68b28

Please sign in to comment.