Multithreaded request aggregator (#4469)

* Run aggregator threaded

* Configurable number of aggregator threads

* Default to max 4 threads

* Added unit test for request_aggregator_threads toml

* Improve the description of request_aggregator_threads

---------

Co-authored-by: Dimitrios Siganos <dimitris@siganos.org>
This commit is contained in:
RickiNano 2024-03-11 13:52:29 +01:00 committed by GitHub
commit d595655392
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 21 additions and 5 deletions

View file

@ -193,6 +193,7 @@ TEST (toml, daemon_config_deserialize_defaults)
ASSERT_EQ (conf.node.work_peers, defaults.node.work_peers); ASSERT_EQ (conf.node.work_peers, defaults.node.work_peers);
ASSERT_EQ (conf.node.work_threads, defaults.node.work_threads); ASSERT_EQ (conf.node.work_threads, defaults.node.work_threads);
ASSERT_EQ (conf.node.max_queued_requests, defaults.node.max_queued_requests); ASSERT_EQ (conf.node.max_queued_requests, defaults.node.max_queued_requests);
ASSERT_EQ (conf.node.request_aggregator_threads, defaults.node.request_aggregator_threads);
ASSERT_EQ (conf.node.max_unchecked_blocks, defaults.node.max_unchecked_blocks); ASSERT_EQ (conf.node.max_unchecked_blocks, defaults.node.max_unchecked_blocks);
ASSERT_EQ (conf.node.backlog_scan_batch_size, defaults.node.backlog_scan_batch_size); ASSERT_EQ (conf.node.backlog_scan_batch_size, defaults.node.backlog_scan_batch_size);
ASSERT_EQ (conf.node.backlog_scan_frequency, defaults.node.backlog_scan_frequency); ASSERT_EQ (conf.node.backlog_scan_frequency, defaults.node.backlog_scan_frequency);
@ -422,6 +423,7 @@ TEST (toml, daemon_config_deserialize_no_defaults)
work_threads = 999 work_threads = 999
max_work_generate_multiplier = 1.0 max_work_generate_multiplier = 1.0
max_queued_requests = 999 max_queued_requests = 999
request_aggregator_threads = 999
max_unchecked_blocks = 999 max_unchecked_blocks = 999
frontiers_confirmation = "always" frontiers_confirmation = "always"
backlog_scan_batch_size = 999 backlog_scan_batch_size = 999
@ -613,6 +615,7 @@ TEST (toml, daemon_config_deserialize_no_defaults)
ASSERT_NE (conf.node.work_peers, defaults.node.work_peers); ASSERT_NE (conf.node.work_peers, defaults.node.work_peers);
ASSERT_NE (conf.node.work_threads, defaults.node.work_threads); ASSERT_NE (conf.node.work_threads, defaults.node.work_threads);
ASSERT_NE (conf.node.max_queued_requests, defaults.node.max_queued_requests); ASSERT_NE (conf.node.max_queued_requests, defaults.node.max_queued_requests);
ASSERT_NE (conf.node.request_aggregator_threads, defaults.node.request_aggregator_threads);
ASSERT_NE (conf.node.backlog_scan_batch_size, defaults.node.backlog_scan_batch_size); ASSERT_NE (conf.node.backlog_scan_batch_size, defaults.node.backlog_scan_batch_size);
ASSERT_NE (conf.node.backlog_scan_frequency, defaults.node.backlog_scan_frequency); ASSERT_NE (conf.node.backlog_scan_frequency, defaults.node.backlog_scan_frequency);

View file

@ -130,6 +130,7 @@ nano::error nano::node_config::serialize_toml (nano::tomlconfig & toml) const
toml.put ("max_work_generate_multiplier", max_work_generate_multiplier, "Maximum allowed difficulty multiplier for work generation.\ntype:double,[1..]"); toml.put ("max_work_generate_multiplier", max_work_generate_multiplier, "Maximum allowed difficulty multiplier for work generation.\ntype:double,[1..]");
toml.put ("frontiers_confirmation", serialize_frontiers_confirmation (frontiers_confirmation), "Mode controlling frontier confirmation rate.\ntype:string,{auto,always,disabled}"); toml.put ("frontiers_confirmation", serialize_frontiers_confirmation (frontiers_confirmation), "Mode controlling frontier confirmation rate.\ntype:string,{auto,always,disabled}");
toml.put ("max_queued_requests", max_queued_requests, "Limit for number of queued confirmation requests for one channel, after which new requests are dropped until the queue drops below this value.\ntype:uint32"); toml.put ("max_queued_requests", max_queued_requests, "Limit for number of queued confirmation requests for one channel, after which new requests are dropped until the queue drops below this value.\ntype:uint32");
toml.put ("request_aggregator_threads", request_aggregator_threads, "Number of threads to dedicate to request aggregator. The default value is the minimum of 4 or the number returned by nano::hardware_concurency(), which is the number of hardware threads or the value of the environment variable NANO_HARDWARE_CONCURRENCY.");
toml.put ("max_unchecked_blocks", max_unchecked_blocks, "Maximum number of unchecked blocks to store in memory. Defaults to 65536. \ntype:uint64,[0..]"); toml.put ("max_unchecked_blocks", max_unchecked_blocks, "Maximum number of unchecked blocks to store in memory. Defaults to 65536. \ntype:uint64,[0..]");
toml.put ("rep_crawler_weight_minimum", rep_crawler_weight_minimum.to_string_dec (), "Rep crawler minimum weight, if this is less than minimum principal weight then this is taken as the minimum weight a rep must have to be tracked. If you want to track all reps set this to 0. If you do not want this to influence anything then set it to max value. This is only useful for debugging or for people who really know what they are doing.\ntype:string,amount,raw"); toml.put ("rep_crawler_weight_minimum", rep_crawler_weight_minimum.to_string_dec (), "Rep crawler minimum weight, if this is less than minimum principal weight then this is taken as the minimum weight a rep must have to be tracked. If you want to track all reps set this to 0. If you do not want this to influence anything then set it to max value. This is only useful for debugging or for people who really know what they are doing.\ntype:string,amount,raw");
toml.put ("backlog_scan_batch_size", backlog_scan_batch_size, "Number of accounts per second to process when doing backlog population scan. Increasing this value will help unconfirmed frontiers get into election prioritization queue faster, however it will also increase resource usage. \ntype:uint"); toml.put ("backlog_scan_batch_size", backlog_scan_batch_size, "Number of accounts per second to process when doing backlog population scan. Increasing this value will help unconfirmed frontiers get into election prioritization queue faster, however it will also increase resource usage. \ntype:uint");
@ -427,6 +428,7 @@ nano::error nano::node_config::deserialize_toml (nano::tomlconfig & toml)
toml.get<double> ("max_work_generate_multiplier", max_work_generate_multiplier); toml.get<double> ("max_work_generate_multiplier", max_work_generate_multiplier);
toml.get<uint32_t> ("max_queued_requests", max_queued_requests); toml.get<uint32_t> ("max_queued_requests", max_queued_requests);
toml.get<uint32_t> ("request_aggregator_threads", request_aggregator_threads);
toml.get<unsigned> ("max_unchecked_blocks", max_unchecked_blocks); toml.get<unsigned> ("max_unchecked_blocks", max_unchecked_blocks);

View file

@ -118,6 +118,7 @@ public:
bool backup_before_upgrade{ false }; bool backup_before_upgrade{ false };
double max_work_generate_multiplier{ 64. }; double max_work_generate_multiplier{ 64. };
uint32_t max_queued_requests{ 512 }; uint32_t max_queued_requests{ 512 };
unsigned request_aggregator_threads{ std::min (nano::hardware_concurrency (), 4u) }; // Max 4 threads if available
unsigned max_unchecked_blocks{ 65536 }; unsigned max_unchecked_blocks{ 65536 };
std::chrono::seconds max_pruning_age{ !network_params.network.is_beta_network () ? std::chrono::seconds (24 * 60 * 60) : std::chrono::seconds (5 * 60) }; // 1 day; 5 minutes for beta network std::chrono::seconds max_pruning_age{ !network_params.network.is_beta_network () ? std::chrono::seconds (24 * 60 * 60) : std::chrono::seconds (5 * 60) }; // 1 day; 5 minutes for beta network
uint64_t max_pruning_depth{ 0 }; uint64_t max_pruning_depth{ 0 };

View file

@ -15,15 +15,20 @@ nano::request_aggregator::request_aggregator (nano::node_config const & config_a
max_delay (config_a.network_params.network.is_dev_network () ? 50 : 300), max_delay (config_a.network_params.network.is_dev_network () ? 50 : 300),
small_delay (config_a.network_params.network.is_dev_network () ? 10 : 50), small_delay (config_a.network_params.network.is_dev_network () ? 10 : 50),
max_channel_requests (config_a.max_queued_requests), max_channel_requests (config_a.max_queued_requests),
request_aggregator_threads (config_a.request_aggregator_threads),
stats (stats_a), stats (stats_a),
local_votes (history_a), local_votes (history_a),
ledger (ledger_a), ledger (ledger_a),
wallets (wallets_a), wallets (wallets_a),
active (active_a), active (active_a),
generator (generator_a), generator (generator_a),
final_generator (final_generator_a), final_generator (final_generator_a)
thread ([this] () { run (); })
{ {
for (auto i = 0; i < request_aggregator_threads; ++i)
{
threads.emplace_back ([this] () { run (); });
}
generator.set_reply_action ([this] (std::shared_ptr<nano::vote> const & vote_a, std::shared_ptr<nano::transport::channel> const & channel_a) { generator.set_reply_action ([this] (std::shared_ptr<nano::vote> const & vote_a, std::shared_ptr<nano::transport::channel> const & channel_a) {
this->reply_action (vote_a, channel_a); this->reply_action (vote_a, channel_a);
}); });
@ -132,11 +137,14 @@ void nano::request_aggregator::stop ()
stopped = true; stopped = true;
} }
condition.notify_all (); condition.notify_all ();
for (auto & thread : threads)
{
if (thread.joinable ()) if (thread.joinable ())
{ {
thread.join (); thread.join ();
} }
} }
}
std::size_t nano::request_aggregator::size () std::size_t nano::request_aggregator::size ()
{ {

View file

@ -13,6 +13,7 @@
#include <condition_variable> #include <condition_variable>
#include <thread> #include <thread>
#include <unordered_map> #include <unordered_map>
#include <vector>
namespace mi = boost::multi_index; namespace mi = boost::multi_index;
@ -74,6 +75,7 @@ public:
std::chrono::milliseconds const max_delay; std::chrono::milliseconds const max_delay;
std::chrono::milliseconds const small_delay; std::chrono::milliseconds const small_delay;
std::size_t const max_channel_requests; std::size_t const max_channel_requests;
std::size_t const request_aggregator_threads;
private: private:
void run (); void run ();
@ -105,7 +107,7 @@ private:
bool started{ false }; bool started{ false };
nano::condition_variable condition; nano::condition_variable condition;
nano::mutex mutex{ mutex_identifier (mutexes::request_aggregator) }; nano::mutex mutex{ mutex_identifier (mutexes::request_aggregator) };
std::thread thread; std::vector<std::thread> threads;
friend std::unique_ptr<container_info_component> collect_container_info (request_aggregator &, std::string const &); friend std::unique_ptr<container_info_component> collect_container_info (request_aggregator &, std::string const &);
}; };