Skip to content

Commit

Permalink
Offload block activation to background workers
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed May 24, 2024
1 parent b480b82 commit eeebc5e
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 3 deletions.
2 changes: 2 additions & 0 deletions nano/lib/stats_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -380,6 +380,8 @@ enum class detail
// active
insert,
insert_failed,
activate_account,
activate_destination,

// active_elections
started,
Expand Down
3 changes: 3 additions & 0 deletions nano/lib/thread_roles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ std::string nano::thread_role::get_string (nano::thread_role::name role)
case nano::thread_role::name::vote_router:
thread_role_name_string = "Vote router";
break;
case nano::thread_role::name::successor_activation:
thread_role_name_string = "Successor actv";
break;
default:
debug_assert (false && "nano::thread_role::get_string unhandled thread role");
}
Expand Down
1 change: 1 addition & 0 deletions nano/lib/thread_roles.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ enum class name
port_mapping,
stats,
vote_router,
successor_activation,
};

std::string_view to_string (name);
Expand Down
10 changes: 9 additions & 1 deletion nano/node/active_elections.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ nano::active_elections::active_elections (nano::node & node_a, nano::confirming_
block_processor{ block_processor_a },
recently_confirmed{ config.confirmation_cache },
recently_cemented{ config.confirmation_history_size },
activate_workers{ 1, nano::thread_role::name::successor_activation }, // TODO: Make threads configurable
election_time_to_live{ node_a.network_params.network.is_dev_network () ? 0s : 2s }
{
count_by_behavior.fill (0); // Zero initialize array
Expand Down Expand Up @@ -87,6 +88,7 @@ void nano::active_elections::stop ()
stopped = true;
}
condition.notify_all ();
activate_workers.stop ();
nano::join_or_pass (thread);
clear ();
}
Expand Down Expand Up @@ -134,7 +136,9 @@ void nano::active_elections::block_cemented_callback (nano::secure::transaction
// Next-block activations are only done for blocks with previously active elections
if (cemented_bootstrap_count_reached && was_active && !node.flags.disable_activate_successors)
{
activate_successors (transaction, block);
activate_workers.push_task ([this, block = block] () {
activate_successors (node.ledger.tx_begin_read (), block);
});
}
}

Expand Down Expand Up @@ -175,11 +179,13 @@ void nano::active_elections::notify_observers (nano::secure::transaction const &

void nano::active_elections::activate_successors (nano::secure::transaction const & transaction, std::shared_ptr<nano::block> const & block)
{
node.stats.inc (nano::stat::type::active, nano::stat::detail::activate_account);
node.scheduler.priority.activate (transaction, block->account ());

// Start or vote for the next unconfirmed block in the destination account
if (block->is_send () && !block->destination ().is_zero () && block->destination () != block->account ())
{
node.stats.inc (nano::stat::type::active, nano::stat::detail::activate_destination);
node.scheduler.priority.activate (transaction, block->destination ());
}
}
Expand Down Expand Up @@ -566,6 +572,8 @@ std::unique_ptr<nano::container_info_component> nano::collect_container_info (ac
composite->add_component (active_elections.recently_confirmed.collect_container_info ("recently_confirmed"));
composite->add_component (active_elections.recently_cemented.collect_container_info ("recently_cemented"));

composite->add_component (active_elections.activate_workers.collect_container_info ("activate_workers"));

return composite;
}

Expand Down
7 changes: 5 additions & 2 deletions nano/node/active_elections.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <nano/lib/enum_util.hpp>
#include <nano/lib/numbers.hpp>
#include <nano/lib/thread_pool.hpp>
#include <nano/node/election_behavior.hpp>
#include <nano/node/election_insertion_result.hpp>
#include <nano/node/election_status.hpp>
Expand Down Expand Up @@ -157,14 +158,16 @@ class active_elections final
nano::block_processor & block_processor;

public:
recently_confirmed_cache recently_confirmed;
recently_cemented_cache recently_cemented;
nano::recently_confirmed_cache recently_confirmed;
nano::recently_cemented_cache recently_cemented;

// TODO: This mutex is currently public because many tests access it
// TODO: This is bad. Remove the need to explicitly lock this from any code outside of this class
mutable nano::mutex mutex{ mutex_identifier (mutexes::active) };

private:
nano::thread_pool activate_workers;

nano::mutex election_winner_details_mutex{ mutex_identifier (mutexes::election_winner_details) };
std::unordered_map<nano::block_hash, std::shared_ptr<nano::election>> election_winner_details;

Expand Down

0 comments on commit eeebc5e

Please sign in to comment.