Skip to content

Commit

Permalink
Periodically refresh read transaction inside hinted scheduler (#4325)
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev authored Nov 3, 2023
1 parent d7f2b8e commit 0377c4f
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 5 deletions.
9 changes: 7 additions & 2 deletions nano/node/scheduler/hinted.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,13 +59,15 @@ bool nano::scheduler::hinted::predicate () const
return active.vacancy (nano::election_behavior::hinted) > 0;
}

void nano::scheduler::hinted::activate (const nano::store::transaction & transaction, const nano::block_hash & hash, bool check_dependents)
void nano::scheduler::hinted::activate (const nano::store::read_transaction & transaction, const nano::block_hash & hash, bool check_dependents)
{
std::stack<nano::block_hash> stack;
stack.push (hash);

while (!stack.empty ())
{
transaction.refresh_if_needed ();

const nano::block_hash current_hash = stack.top ();
stack.pop ();

Expand Down Expand Up @@ -115,9 +117,12 @@ void nano::scheduler::hinted::run_iterative ()
const auto minimum_tally = tally_threshold ();
const auto minimum_final_tally = final_tally_threshold ();

// Get the list before db transaction starts to avoid unnecessary slowdowns
auto tops = vote_cache.top (minimum_tally);

auto transaction = node.store.tx_begin_read ();

for (auto const & entry : vote_cache.top (minimum_tally))
for (auto const & entry : tops)
{
if (!predicate ())
{
Expand Down
3 changes: 2 additions & 1 deletion nano/node/scheduler/hinted.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <nano/lib/locks.hpp>
#include <nano/lib/numbers.hpp>
#include <nano/secure/common.hpp>
#include <nano/store/transaction.hpp>

#include <boost/multi_index/hashed_index.hpp>
#include <boost/multi_index/ordered_index.hpp>
Expand Down Expand Up @@ -61,7 +62,7 @@ class hinted final
bool predicate () const;
void run ();
void run_iterative ();
void activate (nano::store::transaction const &, nano::block_hash const & hash, bool check_dependents);
void activate (nano::store::read_transaction const &, nano::block_hash const & hash, bool check_dependents);

nano::uint128_t tally_threshold () const;
nano::uint128_t final_tally_threshold () const;
Expand Down
27 changes: 25 additions & 2 deletions nano/store/transaction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ nano::store::write_transaction_impl::write_transaction_impl (nano::id_dispenser:
nano::store::read_transaction::read_transaction (std::unique_ptr<store::read_transaction_impl> read_transaction_impl) :
impl (std::move (read_transaction_impl))
{
start = std::chrono::steady_clock::now ();
}

void * nano::store::read_transaction::get_handle () const
Expand All @@ -56,6 +57,7 @@ void nano::store::read_transaction::reset () const
void nano::store::read_transaction::renew () const
{
impl->renew ();
start = std::chrono::steady_clock::now ();
}

void nano::store::read_transaction::refresh () const
Expand All @@ -64,6 +66,15 @@ void nano::store::read_transaction::refresh () const
renew ();
}

void nano::store::read_transaction::refresh_if_needed (std::chrono::milliseconds max_age) const
{
auto now = std::chrono::steady_clock::now ();
if (now - start > max_age)
{
refresh ();
}
}

/*
* write_transaction
*/
Expand All @@ -75,6 +86,8 @@ nano::store::write_transaction::write_transaction (std::unique_ptr<store::write_
* For IO threads, we do not want them to block on creating write transactions.
*/
debug_assert (nano::thread_role::get () != nano::thread_role::name::io);

start = std::chrono::steady_clock::now ();
}

void * nano::store::write_transaction::get_handle () const
Expand All @@ -95,12 +108,22 @@ void nano::store::write_transaction::commit ()
void nano::store::write_transaction::renew ()
{
impl->renew ();
start = std::chrono::steady_clock::now ();
}

void nano::store::write_transaction::refresh ()
{
impl->commit ();
impl->renew ();
commit ();
renew ();
}

void nano::store::write_transaction::refresh_if_needed (std::chrono::milliseconds max_age)
{
auto now = std::chrono::steady_clock::now ();
if (now - start > max_age)
{
refresh ();
}
}

bool nano::store::write_transaction::contains (nano::tables table_a) const
Expand Down
4 changes: 4 additions & 0 deletions nano/store/transaction.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,9 +56,11 @@ class read_transaction final : public transaction
void reset () const;
void renew () const;
void refresh () const;
void refresh_if_needed (std::chrono::milliseconds max_age = std::chrono::milliseconds{ 500 }) const;

private:
std::unique_ptr<read_transaction_impl> impl;
mutable std::chrono::steady_clock::time_point start;
};

/**
Expand All @@ -75,9 +77,11 @@ class write_transaction final : public transaction
void commit ();
void renew ();
void refresh ();
void refresh_if_needed (std::chrono::milliseconds max_age = std::chrono::milliseconds{ 500 });
bool contains (nano::tables table_a) const;

private:
std::unique_ptr<write_transaction_impl> impl;
std::chrono::steady_clock::time_point start;
};
} // namespace nano::store

0 comments on commit 0377c4f

Please sign in to comment.