Skip to content

Commit

Permalink
Optimize unnecessary db lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed May 22, 2024
1 parent 2dd6605 commit 28dd829
Show file tree
Hide file tree
Showing 6 changed files with 53 additions and 31 deletions.
1 change: 1 addition & 0 deletions nano/lib/stats_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,7 @@ enum class detail
// backlog
activated,
activate_failed,
activate_skip,

// active
insert,
Expand Down
8 changes: 5 additions & 3 deletions nano/node/backlog_population.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@
#include <nano/store/component.hpp>
#include <nano/store/confirmation_height.hpp>

nano::backlog_population::backlog_population (const config & config_a, nano::ledger & ledger, nano::stats & stats_a) :
nano::backlog_population::backlog_population (const config & config_a, nano::scheduler::component & schedulers, nano::ledger & ledger, nano::stats & stats_a) :
config_m{ config_a },
schedulers{ schedulers },
ledger{ ledger },
stats{ stats_a }
{
Expand Down Expand Up @@ -119,8 +120,6 @@ void nano::backlog_population::populate_backlog (nano::unique_lock<nano::mutex>

void nano::backlog_population::activate (secure::transaction const & transaction, nano::account const & account)
{
debug_assert (!activate_callback.empty ());

auto const maybe_account_info = ledger.store.account.get (transaction, account);
if (!maybe_account_info)
{
Expand All @@ -137,5 +136,8 @@ void nano::backlog_population::activate (secure::transaction const & transaction
stats.inc (nano::stat::type::backlog, nano::stat::detail::activated);

activate_callback.notify (transaction, account);

schedulers.optimistic.activate (account, account_info, conf_info);
schedulers.priority.activate (transaction, account, account_info, conf_info);
}
}
4 changes: 3 additions & 1 deletion nano/node/backlog_population.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/lib/observer_set.hpp>
#include <nano/node/scheduler/component.hpp>
#include <nano/secure/common.hpp>

#include <condition_variable>
Expand Down Expand Up @@ -34,7 +35,7 @@ class backlog_population final
unsigned frequency;
};

backlog_population (const config &, ledger &, nano::stats &);
backlog_population (const config &, nano::scheduler::component &, nano::ledger &, nano::stats &);
~backlog_population ();

void start ();
Expand All @@ -54,6 +55,7 @@ class backlog_population final
callback_t activate_callback;

private: // Dependencies
nano::scheduler::component & schedulers;
nano::ledger & ledger;
nano::stats & stats;

Expand Down
10 changes: 5 additions & 5 deletions nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,7 @@ nano::node::node (std::shared_ptr<boost::asio::io_context> io_ctx_a, std::filesy
aggregator_impl{ std::make_unique<nano::request_aggregator> (config.request_aggregator, *this, stats, generator, final_generator, history, ledger, wallets, vote_router) },
aggregator{ *aggregator_impl },
wallets (wallets_store.init_error (), *this),
backlog{ nano::backlog_population_config (config), ledger, stats },
backlog{ nano::backlog_population_config (config), scheduler, ledger, stats },
ascendboot{ config, block_processor, ledger, network, stats },
websocket{ config.websocket_config, observers, wallets, ledger, io_ctx, logger },
epoch_upgrader{ *this, ledger, store, network_params, logger },
Expand All @@ -232,10 +232,10 @@ nano::node::node (std::shared_ptr<boost::asio::io_context> io_ctx_a, std::filesy
return ledger.weight (rep);
};

backlog.activate_callback.add ([this] (secure::transaction const & transaction, nano::account const & account) {
scheduler.priority.activate (transaction, account);
scheduler.optimistic.activate (transaction, account);
});
// backlog.activate_callback.add ([this] (secure::transaction const & transaction, nano::account const & account) {
// scheduler.priority.activate (transaction, account);
// scheduler.optimistic.activate (transaction, account);
// });

vote_router.vote_processed.add ([this] (std::shared_ptr<nano::vote> const & vote, nano::vote_source source, std::unordered_map<nano::block_hash, nano::vote_code> const & results) {
if (source != nano::vote_source::cache)
Expand Down
57 changes: 35 additions & 22 deletions nano/node/scheduler/priority.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,33 +57,46 @@ bool nano::scheduler::priority::activate (secure::transaction const & transactio
node.store.confirmation_height.get (transaction, account, conf_info);
if (conf_info.height < info->block_count)
{
debug_assert (conf_info.frontier != info->head);
auto hash = conf_info.height == 0 ? info->open_block : node.ledger.any.block_successor (transaction, conf_info.frontier).value_or (0);
auto block = node.ledger.any.block_get (transaction, hash);
debug_assert (block != nullptr);
if (node.ledger.dependents_confirmed (transaction, *block))
{
auto const balance = node.ledger.any.block_balance (transaction, hash).value ();
auto const previous_balance = node.ledger.any.block_balance (transaction, conf_info.frontier).value_or (0);
auto const balance_priority = std::max (balance, previous_balance);
return activate (transaction, account, *info, conf_info);
}
}

node.stats.inc (nano::stat::type::election_scheduler, nano::stat::detail::activated);
node.logger.trace (nano::log::type::election_scheduler, nano::log::detail::block_activated,
nano::log::arg{ "account", account.to_account () }, // TODO: Convert to lazy eval
nano::log::arg{ "block", block },
nano::log::arg{ "time", info->modified },
nano::log::arg{ "priority", balance_priority });
stats.inc (nano::stat::type::election_scheduler, nano::stat::detail::activate_skip);
return false; // Not activated
}

{
nano::lock_guard<nano::mutex> lock{ mutex };
buckets->push (info->modified, block, balance_priority);
}
notify ();
}
bool nano::scheduler::priority::activate (secure::transaction const & transaction, nano::account const & account, nano::account_info const & info, nano::confirmation_height_info const & conf_info)
{
debug_assert (conf_info.frontier != info.head);

auto hash = conf_info.height == 0 ? info.open_block : node.ledger.any.block_successor (transaction, conf_info.frontier).value_or (0);
auto block = node.ledger.any.block_get (transaction, hash);
release_assert (block != nullptr);

if (node.ledger.dependents_confirmed (transaction, *block))
{
auto const balance = node.ledger.any.block_balance (transaction, hash).value ();
auto const previous_balance = node.ledger.any.block_balance (transaction, conf_info.frontier).value_or (0);
auto const balance_priority = std::max (balance, previous_balance);

node.stats.inc (nano::stat::type::election_scheduler, nano::stat::detail::activated);
node.logger.trace (nano::log::type::election_scheduler, nano::log::detail::block_activated,
nano::log::arg{ "account", account.to_account () }, // TODO: Convert to lazy eval
nano::log::arg{ "block", block },
nano::log::arg{ "time", info.modified },
nano::log::arg{ "priority", balance_priority });

{
nano::lock_guard<nano::mutex> lock{ mutex };
buckets->push (info.modified, block, balance_priority);
}
notify ();

return true; // Activated
}

return true; // Activated
stats.inc (nano::stat::type::election_scheduler, nano::stat::detail::activate_failed);
return false; // Not activated
}

void nano::scheduler::priority::notify ()
Expand Down
4 changes: 4 additions & 0 deletions nano/node/scheduler/priority.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

namespace nano
{
class account_info;
class confirmation_height_info;
class block;
class container_info_component;
class node;
Expand Down Expand Up @@ -48,6 +50,8 @@ class priority final
* @return true if account was activated
*/
bool activate (secure::transaction const &, nano::account const &);
bool activate (secure::transaction const &, nano::account const &, nano::account_info const &, nano::confirmation_height_info const &);

void notify ();
std::size_t size () const;
bool empty () const;
Expand Down

0 comments on commit 28dd829

Please sign in to comment.