Skip to content

Commit

Permalink
Merge pull request #905 from AntelopeIO/single_timer
Browse files Browse the repository at this point in the history
[4.0] Use a single timer for read and write windows
  • Loading branch information
linh2931 authored Mar 27, 2023
2 parents 8de2986 + d73bf1b commit fed6d11
Showing 1 changed file with 8 additions and 12 deletions.
20 changes: 8 additions & 12 deletions plugins/producer_plugin/producer_plugin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -313,8 +313,7 @@ class producer_plugin_impl : public std::enable_shared_from_this<producer_plugin
producer_plugin_impl(boost::asio::io_service& io)
:_timer(io)
,_transaction_ack_channel(app().get_channel<compat::channels::transaction_ack>())
,_ro_write_window_timer(io)
,_ro_read_window_timer(io)
,_ro_timer(io)
{
}

Expand Down Expand Up @@ -521,8 +520,7 @@ class producer_plugin_impl : public std::enable_shared_from_this<producer_plugin
fc::microseconds _ro_read_window_effective_time_us{ 0 }; // calculated during option initialization
std::atomic<int64_t> _ro_all_threads_exec_time_us; // total time spent by all threads executing transactions. use atomic for simplicity and performance
fc::time_point _ro_read_window_start_time;
boost::asio::deadline_timer _ro_write_window_timer;
boost::asio::deadline_timer _ro_read_window_timer;
boost::asio::deadline_timer _ro_timer;
fc::microseconds _ro_max_trx_time_us{ 0 }; // calculated during option initialization
ro_trx_queue_t _ro_trx_queue;
std::atomic<uint32_t> _ro_num_active_trx_exec_tasks{ 0 };
Expand Down Expand Up @@ -2878,8 +2876,7 @@ void producer_plugin_impl::switch_to_write_window() {
}

EOS_ASSERT(_ro_num_active_trx_exec_tasks.load() == 0 && _ro_trx_exec_tasks_fut.empty(), producer_exception, "no read-only tasks should be running before switching to write window");
_ro_read_window_timer.cancel();
_ro_write_window_timer.cancel();
_ro_timer.cancel();

start_write_window();
}
Expand All @@ -2893,8 +2890,8 @@ void producer_plugin_impl::start_write_window() {
_idle_trx_time = fc::time_point::now();

auto expire_time = boost::posix_time::microseconds(_ro_write_window_time_us.count());
_ro_write_window_timer.expires_from_now( expire_time );
_ro_write_window_timer.async_wait( app().executor().wrap( // stay on app thread
_ro_timer.expires_from_now( expire_time );
_ro_timer.async_wait( app().executor().wrap( // stay on app thread
priority::high,
exec_queue::read_only_trx_safe, // placed in read_only_trx_safe queue so it is ensured to be executed in either window
[weak_this = weak_from_this()]( const boost::system::error_code& ec ) {
Expand All @@ -2910,8 +2907,7 @@ void producer_plugin_impl::switch_to_read_window() {
EOS_ASSERT(app().executor().is_write_window(), producer_exception, "expected to be in write window");
EOS_ASSERT(_ro_num_active_trx_exec_tasks.load() == 0 && _ro_trx_exec_tasks_fut.empty(), producer_exception, "_ro_trx_exec_tasks_fut expected to be empty" );

_ro_write_window_timer.cancel();
_ro_read_window_timer.cancel();
_ro_timer.cancel();
_time_tracker.add_idle_time( fc::time_point::now() - _idle_trx_time );

// we are in write window, so no read-only trx threads are processing transactions.
Expand All @@ -2938,8 +2934,8 @@ void producer_plugin_impl::switch_to_read_window() {
}

auto expire_time = boost::posix_time::microseconds(_ro_read_window_time_us.count());
_ro_read_window_timer.expires_from_now( expire_time );
_ro_read_window_timer.async_wait( app().executor().wrap( // stay on app thread
_ro_timer.expires_from_now( expire_time );
_ro_timer.async_wait( app().executor().wrap( // stay on app thread
priority::high,
exec_queue::read_only_trx_safe,
[weak_this = weak_from_this()]( const boost::system::error_code& ec ) {
Expand Down

0 comments on commit fed6d11

Please sign in to comment.