Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multithreaded request aggregator #4469

Merged
merged 5 commits into from
Mar 11, 2024
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions nano/core_test/toml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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_threads, defaults.node.work_threads);
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.backlog_scan_batch_size, defaults.node.backlog_scan_batch_size);
ASSERT_EQ (conf.node.backlog_scan_frequency, defaults.node.backlog_scan_frequency);
Expand Down Expand Up @@ -422,6 +423,7 @@ TEST (toml, daemon_config_deserialize_no_defaults)
work_threads = 999
max_work_generate_multiplier = 1.0
max_queued_requests = 999
request_aggregator_threads = 999
max_unchecked_blocks = 999
frontiers_confirmation = "always"
backlog_scan_batch_size = 999
Expand Down Expand Up @@ -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_threads, defaults.node.work_threads);
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_frequency, defaults.node.backlog_scan_frequency);

Expand Down
2 changes: 2 additions & 0 deletions nano/node/nodeconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,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 ("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 ("request_aggregator_threads", request_aggregator_threads, "Number of threads dedicated to the request aggregator. Defaults to use 4 threads if available, otherwise uses all cpu threads available");
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 ("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");
Expand Down Expand Up @@ -416,6 +417,7 @@ nano::error nano::node_config::deserialize_toml (nano::tomlconfig & toml)
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> ("request_aggregator_threads", request_aggregator_threads);

toml.get<unsigned> ("max_unchecked_blocks", max_unchecked_blocks);

Expand Down
1 change: 1 addition & 0 deletions nano/node/nodeconfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ class node_config
bool backup_before_upgrade{ false };
double max_work_generate_multiplier{ 64. };
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 };
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 };
Expand Down
16 changes: 12 additions & 4 deletions nano/node/request_aggregator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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),
small_delay (config_a.network_params.network.is_dev_network () ? 10 : 50),
max_channel_requests (config_a.max_queued_requests),
request_aggregator_threads (config_a.request_aggregator_threads),
stats (stats_a),
local_votes (history_a),
ledger (ledger_a),
wallets (wallets_a),
active (active_a),
generator (generator_a),
final_generator (final_generator_a),
thread ([this] () { run (); })
final_generator (final_generator_a)
{
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) {
this->reply_action (vote_a, channel_a);
});
Expand Down Expand Up @@ -132,9 +137,12 @@ void nano::request_aggregator::stop ()
stopped = true;
}
condition.notify_all ();
if (thread.joinable ())
for (auto & thread : threads)
{
thread.join ();
if (thread.joinable ())
{
thread.join ();
}
}
}

Expand Down
4 changes: 3 additions & 1 deletion nano/node/request_aggregator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <condition_variable>
#include <thread>
#include <unordered_map>
#include <vector>

namespace mi = boost::multi_index;

Expand Down Expand Up @@ -74,6 +75,7 @@ class request_aggregator final
std::chrono::milliseconds const max_delay;
std::chrono::milliseconds const small_delay;
std::size_t const max_channel_requests;
std::size_t const request_aggregator_threads;

private:
void run ();
Expand Down Expand Up @@ -105,7 +107,7 @@ class request_aggregator final
bool started{ false };
nano::condition_variable condition;
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 &);
};
Expand Down
Loading