From 79ffebc615fc04de115d7b9a578d3432c3847312 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20Wo=CC=81jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 14 Nov 2023 00:31:58 +0100 Subject: [PATCH] Use steady clock in ascending bootstrap --- nano/lib/timer.hpp | 5 ----- nano/node/bootstrap/bootstrap_config.cpp | 22 ++++++++++++++----- nano/node/bootstrap/bootstrap_config.hpp | 6 ++--- .../node/bootstrap_ascending/account_sets.cpp | 5 +++-- .../node/bootstrap_ascending/account_sets.hpp | 2 +- nano/node/bootstrap_ascending/service.cpp | 10 +++++++-- nano/node/bootstrap_ascending/service.hpp | 2 +- 7 files changed, 32 insertions(+), 20 deletions(-) diff --git a/nano/lib/timer.hpp b/nano/lib/timer.hpp index 166a212e9f..2af742e339 100644 --- a/nano/lib/timer.hpp +++ b/nano/lib/timer.hpp @@ -111,9 +111,4 @@ inline seconds_t seconds_since_epoch () { return std::chrono::duration_cast (std::chrono::system_clock::now ().time_since_epoch ()).count (); } - -inline nano::millis_t time_difference (nano::millis_t start, nano::millis_t end) -{ - return end > start ? (end - start) : 0; -} } \ No newline at end of file diff --git a/nano/node/bootstrap/bootstrap_config.cpp b/nano/node/bootstrap/bootstrap_config.cpp index 819cd64b54..6e3913eeea 100644 --- a/nano/node/bootstrap/bootstrap_config.cpp +++ b/nano/node/bootstrap/bootstrap_config.cpp @@ -9,7 +9,10 @@ nano::error nano::account_sets_config::deserialize (nano::tomlconfig & toml) toml.get ("consideration_count", consideration_count); toml.get ("priorities_max", priorities_max); toml.get ("blocking_max", blocking_max); - toml.get ("cooldown", cooldown); + + auto cooldown_l = cooldown.count (); + toml.get ("cooldown", cooldown_l); + cooldown = std::chrono::milliseconds{ cooldown_l }; return toml.get_error (); } @@ -19,7 +22,7 @@ nano::error nano::account_sets_config::serialize (nano::tomlconfig & toml) const toml.put ("consideration_count", consideration_count, "Limit the number of account candidates to consider and also the number of iterations.\ntype:uint64"); toml.put ("priorities_max", priorities_max, "Cutoff size limit for the priority list.\ntype:uint64"); toml.put ("blocking_max", blocking_max, "Cutoff size limit for the blocked accounts from the priority list.\ntype:uint64"); - toml.put ("cooldown", cooldown, "Waiting time for an account to become available.\ntype:milliseconds"); + toml.put ("cooldown", cooldown.count (), "Waiting time for an account to become available.\ntype:milliseconds"); return toml.get_error (); } @@ -32,9 +35,16 @@ nano::error nano::bootstrap_ascending_config::deserialize (nano::tomlconfig & to toml.get ("requests_limit", requests_limit); toml.get ("database_requests_limit", database_requests_limit); toml.get ("pull_count", pull_count); - toml.get ("timeout", timeout); + + auto timeout_l = timeout.count (); + toml.get ("timeout", timeout_l); + timeout = std::chrono::milliseconds{ timeout_l }; + toml.get ("throttle_coefficient", throttle_coefficient); - toml.get ("throttle_wait", throttle_wait); + + auto throttle_wait_l = throttle_wait.count (); + toml.get ("throttle_wait", throttle_wait_l); + throttle_wait = std::chrono::milliseconds{ throttle_wait_l }; if (toml.has_key ("account_sets")) { @@ -50,9 +60,9 @@ nano::error nano::bootstrap_ascending_config::serialize (nano::tomlconfig & toml toml.put ("requests_limit", requests_limit, "Request limit to ascending bootstrap after which requests will be dropped.\nNote: changing to unlimited (0) is not recommended.\ntype:uint64"); toml.put ("database_requests_limit", database_requests_limit, "Request limit for accounts from database after which requests will be dropped.\nNote: changing to unlimited (0) is not recommended as this operation competes for resources on querying the database.\ntype:uint64"); toml.put ("pull_count", pull_count, "Number of requested blocks for ascending bootstrap request.\ntype:uint64"); - toml.put ("timeout", timeout, "Timeout in milliseconds for incoming ascending bootstrap messages to be processed.\ntype:milliseconds"); + toml.put ("timeout", timeout.count (), "Timeout in milliseconds for incoming ascending bootstrap messages to be processed.\ntype:milliseconds"); toml.put ("throttle_coefficient", throttle_coefficient, "Scales the number of samples to track for bootstrap throttling.\ntype:uint64"); - toml.put ("throttle_wait", throttle_wait, "Length of time to wait between requests when throttled.\ntype:milliseconds"); + toml.put ("throttle_wait", throttle_wait.count (), "Length of time to wait between requests when throttled.\ntype:milliseconds"); nano::tomlconfig account_sets_l; account_sets.serialize (account_sets_l); diff --git a/nano/node/bootstrap/bootstrap_config.hpp b/nano/node/bootstrap/bootstrap_config.hpp index 89e7f27fb1..d322691dad 100644 --- a/nano/node/bootstrap/bootstrap_config.hpp +++ b/nano/node/bootstrap/bootstrap_config.hpp @@ -17,7 +17,7 @@ class account_sets_config final std::size_t consideration_count{ 4 }; std::size_t priorities_max{ 256 * 1024 }; std::size_t blocking_max{ 256 * 1024 }; - nano::millis_t cooldown{ 1000 * 3 }; + std::chrono::milliseconds cooldown{ 1000 * 3 }; }; class bootstrap_ascending_config final @@ -30,9 +30,9 @@ class bootstrap_ascending_config final std::size_t requests_limit{ 64 }; std::size_t database_requests_limit{ 1024 }; std::size_t pull_count{ nano::bootstrap_server::max_blocks }; - nano::millis_t timeout{ 1000 * 3 }; + std::chrono::milliseconds timeout{ 1000 * 3 }; std::size_t throttle_coefficient{ 16 }; - nano::millis_t throttle_wait{ 100 }; + std::chrono::milliseconds throttle_wait{ 100 }; nano::account_sets_config account_sets; }; diff --git a/nano/node/bootstrap_ascending/account_sets.cpp b/nano/node/bootstrap_ascending/account_sets.cpp index 1348c5bebc..6b1752bad8 100644 --- a/nano/node/bootstrap_ascending/account_sets.cpp +++ b/nano/node/bootstrap_ascending/account_sets.cpp @@ -116,7 +116,7 @@ void nano::bootstrap_ascending::account_sets::unblock (nano::account const & acc void nano::bootstrap_ascending::account_sets::timestamp (const nano::account & account, bool reset) { - const nano::millis_t tstamp = reset ? 0 : nano::milliseconds_since_epoch (); + const std::chrono::steady_clock::time_point tstamp = reset ? std::chrono::steady_clock::time_point{} : std::chrono::steady_clock::now (); auto iter = priorities.get ().find (account); if (iter != priorities.get ().end ()) @@ -132,7 +132,8 @@ bool nano::bootstrap_ascending::account_sets::check_timestamp (const nano::accou auto iter = priorities.get ().find (account); if (iter != priorities.get ().end ()) { - if (nano::milliseconds_since_epoch () - iter->timestamp < config.cooldown) + // Not cooled down yet + if (iter->timestamp + config.cooldown > std::chrono::steady_clock::now ()) { return false; } diff --git a/nano/node/bootstrap_ascending/account_sets.hpp b/nano/node/bootstrap_ascending/account_sets.hpp index 04a60440da..f4f6d094bc 100644 --- a/nano/node/bootstrap_ascending/account_sets.hpp +++ b/nano/node/bootstrap_ascending/account_sets.hpp @@ -71,7 +71,7 @@ class account_sets { nano::account account{ 0 }; float priority{ 0 }; - nano::millis_t timestamp{ 0 }; + std::chrono::steady_clock::time_point timestamp{}; nano::bootstrap_ascending::id_t id{ 0 }; // Uniformly distributed, used for random querying priority_entry (nano::account account, float priority); diff --git a/nano/node/bootstrap_ascending/service.cpp b/nano/node/bootstrap_ascending/service.cpp index 36a8f25c86..f3a91472c9 100644 --- a/nano/node/bootstrap_ascending/service.cpp +++ b/nano/node/bootstrap_ascending/service.cpp @@ -264,7 +264,7 @@ bool nano::bootstrap_ascending::service::request (nano::account & account, std:: async_tag tag{}; tag.id = nano::bootstrap_ascending::generate_id (); tag.account = account; - tag.time = nano::milliseconds_since_epoch (); + tag.time = std::chrono::steady_clock::now (); // Check if the account picked has blocks, if it does, start the pull from the highest block auto info = ledger.store.account.get (ledger.store.tx_begin_read (), account); @@ -341,8 +341,14 @@ void nano::bootstrap_ascending::service::run_timeouts () scoring.sync (network.list ()); scoring.timeout (); throttle.resize (compute_throttle_size ()); + auto & tags_by_order = tags.get (); - while (!tags_by_order.empty () && nano::time_difference (tags_by_order.front ().time, nano::milliseconds_since_epoch ()) > config.bootstrap_ascending.timeout) + + auto const now = std::chrono::steady_clock::now (); + auto timed_out = [&] (auto const & tag) { + return tag.time + config.bootstrap_ascending.timeout < now; + }; + while (!tags_by_order.empty () && timed_out (*tags_by_order.begin ())) { auto tag = tags_by_order.front (); tags_by_order.pop_front (); diff --git a/nano/node/bootstrap_ascending/service.hpp b/nano/node/bootstrap_ascending/service.hpp index 43f2d2ae73..686003915b 100644 --- a/nano/node/bootstrap_ascending/service.hpp +++ b/nano/node/bootstrap_ascending/service.hpp @@ -78,7 +78,7 @@ class service query_type type{ query_type::invalid }; nano::bootstrap_ascending::id_t id{ 0 }; nano::hash_or_account start{ 0 }; - nano::millis_t time{ 0 }; + std::chrono::steady_clock::time_point time{}; nano::account account{ 0 }; };