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

Commit

Permalink
Merge pull request #7172 from EOSIO/create-snapshot-in-irreversible-mode
Browse files Browse the repository at this point in the history
immediately return created snapshot in irreversible mode
  • Loading branch information
arhag committed Apr 20, 2019
2 parents 060e8f1 + aa6f647 commit 715af8f
Showing 1 changed file with 49 additions and 26 deletions.
75 changes: 49 additions & 26 deletions plugins/producer_plugin/producer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1023,15 +1023,59 @@ void producer_plugin::create_snapshot(producer_plugin::next_function<producer_pl
chain::controller& chain = my->chain_plug->chain();

auto head_id = chain.head_block_id();
std::string snapshot_path = (pending_snapshot::get_final_path(head_id, my->_snapshots_dir)).generic_string();
const auto& snapshot_path = pending_snapshot::get_final_path(head_id, my->_snapshots_dir);
const auto& temp_path = pending_snapshot::get_temp_path(head_id, my->_snapshots_dir);

// maintain legacy exception if the snapshot exists
if( fc::is_regular_file(snapshot_path) ) {
auto ex = snapshot_exists_exception( FC_LOG_MESSAGE( error, "snapshot named ${name} already exists", ("name", snapshot_path) ) );
auto ex = snapshot_exists_exception( FC_LOG_MESSAGE( error, "snapshot named ${name} already exists", ("name", snapshot_path.generic_string()) ) );
next(ex.dynamic_copy_exception());
return;
}

auto write_snapshot = [&]( const bfs::path& p ) -> void {
auto reschedule = fc::make_scoped_exit([this](){
my->schedule_production_loop();
});

if (chain.is_building_block()) {
// abort the pending block
chain.abort_block();
} else {
reschedule.cancel();
}

bfs::create_directory( p.parent_path() );

// create the snapshot
auto snap_out = std::ofstream(p.generic_string(), (std::ios::out | std::ios::binary));
auto writer = std::make_shared<ostream_snapshot_writer>(snap_out);
chain.write_snapshot(writer);
writer->finalize();
snap_out.flush();
snap_out.close();
};

// If in irreversible mode, create snapshot and return path to snapshot immediately.
if( chain.get_read_mode() == db_read_mode::IRREVERSIBLE ) {
try {
write_snapshot( temp_path );

boost::system::error_code ec;
bfs::rename(temp_path, snapshot_path, ec);
EOS_ASSERT(!ec, snapshot_finalization_exception,
"Unable to finalize valid snapshot of block number ${bn}: [code: ${ec}] ${message}",
("bn", chain.head_block_num())
("ec", ec.value())
("message", ec.message()));

next( producer_plugin::snapshot_information{head_id, snapshot_path.generic_string()} );
} CATCH_AND_CALL (next);
return;
}

// Otherwise, the result will be returned when the snapshot becomes irreversible.

// determine if this snapshot is already in-flight
auto& pending_by_id = my->_pending_snapshot_index.get<by_id>();
auto existing = pending_by_id.find(head_id);
Expand All @@ -1044,31 +1088,10 @@ void producer_plugin::create_snapshot(producer_plugin::next_function<producer_pl
};
});
} else {
// write a new temp snapshot
std::string temp_path = (pending_snapshot::get_temp_path(head_id, my->_snapshots_dir)).generic_string();
std::string pending_path = (pending_snapshot::get_pending_path(head_id, my->_snapshots_dir)).generic_string();
std::string final_path = (pending_snapshot::get_final_path(head_id, my->_snapshots_dir)).generic_string();
bool written = false;
const auto& pending_path = pending_snapshot::get_pending_path(head_id, my->_snapshots_dir);

try {
auto reschedule = fc::make_scoped_exit([this](){
my->schedule_production_loop();
});

if (chain.is_building_block()) {
// abort the pending block
chain.abort_block();
} else {
reschedule.cancel();
}

// create a new pending snapshot
auto snap_out = std::ofstream(temp_path, (std::ios::out | std::ios::binary));
auto writer = std::make_shared<ostream_snapshot_writer>(snap_out);
chain.write_snapshot(writer);
writer->finalize();
snap_out.flush();
snap_out.close();
write_snapshot( temp_path ); // create a new pending snapshot

boost::system::error_code ec;
bfs::rename(temp_path, pending_path, ec);
Expand All @@ -1078,7 +1101,7 @@ void producer_plugin::create_snapshot(producer_plugin::next_function<producer_pl
("ec", ec.value())
("message", ec.message()));

my->_pending_snapshot_index.emplace(head_id, next, pending_path, final_path);
my->_pending_snapshot_index.emplace(head_id, next, pending_path.generic_string(), snapshot_path.generic_string());
} CATCH_AND_CALL (next);
}
}
Expand Down

0 comments on commit 715af8f

Please sign in to comment.