Skip to content

Commit

Permalink
Merge pull request #2029 from AntelopeIO/GH-2021-snapshot-ram-5.0
Browse files Browse the repository at this point in the history
[5.0] P2P: Pause net_plugin during snapshot write
  • Loading branch information
heifner authored Jan 4, 2024
2 parents 31f7bc4 + 8fe4f3c commit 04774eb
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
11 changes: 10 additions & 1 deletion libraries/chain/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ struct controller_impl {
named_thread_pool<chain> thread_pool;
deep_mind_handler* deep_mind_logger = nullptr;
bool okay_to_print_integrity_hash_on_stop = false;
std::atomic<bool> writing_snapshot = false;

thread_local static platform_timer timer; // a copy for main thread and each read-only thread
#if defined(EOSIO_EOS_VM_RUNTIME_ENABLED) || defined(EOSIO_EOS_VM_JIT_RUNTIME_ENABLED)
Expand Down Expand Up @@ -3246,7 +3247,15 @@ fc::sha256 controller::calculate_integrity_hash() { try {

void controller::write_snapshot( const snapshot_writer_ptr& snapshot ) {
EOS_ASSERT( !my->pending, block_validate_exception, "cannot take a consistent snapshot with a pending block" );
return my->add_to_snapshot(snapshot);
my->writing_snapshot.store(true, std::memory_order_release);
fc::scoped_exit<std::function<void()>> e = [&] {
my->writing_snapshot.store(false, std::memory_order_release);
};
my->add_to_snapshot(snapshot);
}

bool controller::is_writing_snapshot() const {
return my->writing_snapshot.load(std::memory_order_acquire);
}

int64_t controller::set_proposed_producers( vector<producer_authority> producers ) {
Expand Down
2 changes: 2 additions & 0 deletions libraries/chain/include/eosio/chain/controller.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,8 @@ namespace eosio { namespace chain {

fc::sha256 calculate_integrity_hash();
void write_snapshot( const snapshot_writer_ptr& snapshot );
// thread-safe
bool is_writing_snapshot()const;

bool sender_avoids_whitelist_blacklist_enforcement( account_name sender )const;
void check_actor_list( const flat_set<account_name>& actors )const;
Expand Down
10 changes: 10 additions & 0 deletions plugins/net_plugin/net_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -537,6 +537,14 @@ namespace eosio {
void start_expire_timer();
void start_monitors();

// we currently pause on snapshot generation
void wait_if_paused() const {
controller& cc = chain_plug->chain();
while (cc.is_writing_snapshot()) {
std::this_thread::sleep_for(std::chrono::milliseconds(10));
}
}

void expire();
/** \name Peer Timestamps
* Time message handling
Expand Down Expand Up @@ -2897,6 +2905,8 @@ namespace eosio {
return;
}

my_impl->wait_if_paused();

boost::asio::async_read( *socket,
pending_message_buffer.get_buffer_sequence_for_boost_async_read(), completion_handler,
boost::asio::bind_executor( strand,
Expand Down

0 comments on commit 04774eb

Please sign in to comment.