Skip to content

Commit

Permalink
Use steady clock in ascending bootstrap
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Nov 13, 2023
1 parent 7ef1427 commit 79ffebc
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 20 deletions.
5 changes: 0 additions & 5 deletions nano/lib/timer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,4 @@ inline seconds_t seconds_since_epoch ()
{
return std::chrono::duration_cast<std::chrono::seconds> (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;
}
}
22 changes: 16 additions & 6 deletions nano/node/bootstrap/bootstrap_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 ();
}
Expand All @@ -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 ();
}
Expand All @@ -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"))
{
Expand All @@ -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);
Expand Down
6 changes: 3 additions & 3 deletions nano/node/bootstrap/bootstrap_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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;
};
Expand Down
5 changes: 3 additions & 2 deletions nano/node/bootstrap_ascending/account_sets.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<tag_account> ().find (account);
if (iter != priorities.get<tag_account> ().end ())
Expand All @@ -132,7 +132,8 @@ bool nano::bootstrap_ascending::account_sets::check_timestamp (const nano::accou
auto iter = priorities.get<tag_account> ().find (account);
if (iter != priorities.get<tag_account> ().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;
}
Expand Down
2 changes: 1 addition & 1 deletion nano/node/bootstrap_ascending/account_sets.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
10 changes: 8 additions & 2 deletions nano/node/bootstrap_ascending/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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<tag_sequenced> ();
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 ();
Expand Down
2 changes: 1 addition & 1 deletion nano/node/bootstrap_ascending/service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 };
};

Expand Down

0 comments on commit 79ffebc

Please sign in to comment.