Skip to content

Commit

Permalink
Vote generator session for batch insertions (#2702)
Browse files Browse the repository at this point in the history
* Vote generator session for batch insertions
Restoring removed in PR #2688
* Make vote_generator_session single threaded
* Vote generator session test
  • Loading branch information
SergiySW committed Apr 9, 2020
1 parent 17c0a74 commit 60a4c2f
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 4 deletions.
26 changes: 26 additions & 0 deletions nano/core_test/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -878,3 +878,29 @@ TEST (active_multiplier, normalization)
ASSERT_NEAR (16.0, norm_multiplier15, 1e-10);
ASSERT_NEAR (nano::denormalized_multiplier (norm_multiplier15, node.network_params.network.publish_thresholds.epoch_2_receive), multiplier15, 1e-10);
}

namespace nano
{
TEST (active_transactions, vote_generator_session)
{
nano::system system (1);
auto node (system.nodes[0]);
system.wallet (0)->insert_adhoc (nano::test_genesis_key.prv);
nano::vote_generator_session generator_session (node->active.generator);
boost::thread thread ([node, &generator_session]() {
nano::thread_role::set (nano::thread_role::name::request_loop);
for (unsigned i = 0; i < 100; ++i)
{
generator_session.add (nano::block_hash (i));
}
ASSERT_EQ (0, node->stats.count (nano::stat::type::vote, nano::stat::detail::vote_indeterminate));
generator_session.flush ();
});
thread.join ();
system.deadline_set (5s);
while (node->stats.count (nano::stat::type::vote, nano::stat::detail::vote_indeterminate) < (100 / nano::network::confirm_ack_hashes_max))
{
ASSERT_NO_ERROR (system.poll (5ms));
}
}
}
4 changes: 3 additions & 1 deletion nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,7 @@ void nano::active_transactions::request_confirm (nano::unique_lock<std::mutex> &
nano::confirmation_solicitor solicitor (node.network, node.network_params.network);
solicitor.prepare (node.rep_crawler.principal_representatives (std::numeric_limits<size_t>::max ()));

nano::vote_generator_session generator_session (generator);
auto & sorted_roots_l (roots.get<tag_difficulty> ());
auto const election_ttl_cutoff_l (std::chrono::steady_clock::now () - election_time_to_live);
bool const check_all_elections_l (std::chrono::steady_clock::now () - last_check_all_elections > check_all_elections_period);
Expand All @@ -229,7 +230,7 @@ void nano::active_transactions::request_confirm (nano::unique_lock<std::mutex> &

if (!election_l->prioritized () && unconfirmed_count_l < prioritized_cutoff)
{
election_l->prioritize_election ();
election_l->prioritize_election (generator_session);
}

unconfirmed_count_l += !confirmed_l;
Expand All @@ -246,6 +247,7 @@ void nano::active_transactions::request_confirm (nano::unique_lock<std::mutex> &
}
lock_a.unlock ();
solicitor.flush ();
generator_session.flush ();
lock_a.lock ();

// This is updated after the loop to ensure slow machines don't do the full check often
Expand Down
1 change: 1 addition & 0 deletions nano/node/active_transactions.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,7 @@ class active_transactions final
friend class confirmation_height_prioritize_frontiers_Test;
friend class confirmation_height_prioritize_frontiers_overwrite_Test;
friend class active_transactions_confirmation_consistency_Test;
friend class active_transactions_vote_generator_session_Test;
friend class node_vote_by_hash_bundle_Test;
friend std::unique_ptr<container_info_component> collect_container_info (active_transactions &, const std::string &);
};
Expand Down
4 changes: 2 additions & 2 deletions nano/node/election.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -584,12 +584,12 @@ bool nano::election::prioritized () const
return prioritized_m;
}

void nano::election::prioritize_election ()
void nano::election::prioritize_election (nano::vote_generator_session & generator_session_a)
{
debug_assert (!node.active.mutex.try_lock ());
debug_assert (!prioritized_m);
prioritized_m = true;
generate_votes (status.winner->hash ());
generator_session_a.add (status.winner->hash ());
}

void nano::election::generate_votes (nano::block_hash const & hash_a)
Expand Down
3 changes: 2 additions & 1 deletion nano/node/election.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ namespace nano
class channel;
class confirmation_solicitor;
class node;
class vote_generator_session;
class vote_info final
{
public:
Expand Down Expand Up @@ -83,7 +84,7 @@ class election final : public std::enable_shared_from_this<nano::election>
void adjust_dependent_difficulty ();
void insert_inactive_votes_cache (nano::block_hash const &);
bool prioritized () const;
void prioritize_election ();
void prioritize_election (nano::vote_generator_session &);
// Erase all blocks from active and, if not confirmed, clear digests from network filters
void cleanup ();

Expand Down
28 changes: 28 additions & 0 deletions nano/node/voting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,17 @@ void nano::vote_generator::add (nano::block_hash const & hash_a)
}
}

void nano::vote_generator::add (std::vector<nano::block_hash> const & hashes_a)
{
nano::unique_lock<std::mutex> lock (mutex);
hashes.insert (hashes.end (), hashes_a.begin (), hashes_a.end ());
if (hashes.size () >= nano::network::confirm_ack_hashes_max)
{
lock.unlock ();
condition.notify_all ();
}
}

void nano::vote_generator::stop ()
{
nano::unique_lock<std::mutex> lock (mutex);
Expand Down Expand Up @@ -102,6 +113,23 @@ void nano::vote_generator::run ()
}
}

nano::vote_generator_session::vote_generator_session (nano::vote_generator & vote_generator_a) :
generator (vote_generator_a)
{
}

void nano::vote_generator_session::add (nano::block_hash const & hash_a)
{
debug_assert (nano::thread_role::get () == nano::thread_role::name::request_loop);
hashes.push_back (hash_a);
}

void nano::vote_generator_session::flush ()
{
debug_assert (nano::thread_role::get () == nano::thread_role::name::request_loop);
generator.add (hashes);
}

nano::votes_cache::votes_cache (nano::wallets & wallets_a) :
wallets (wallets_a)
{
Expand Down
13 changes: 13 additions & 0 deletions nano/node/voting.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class vote_generator final
public:
vote_generator (nano::node_config & config_a, nano::block_store & store_a, nano::wallets & wallets_a, nano::vote_processor & vote_processor_a, nano::votes_cache & votes_cache_a, nano::network & network_a);
void add (nano::block_hash const &);
void add (std::vector<nano::block_hash> const &);
void stop ();

private:
Expand All @@ -53,6 +54,18 @@ class vote_generator final
friend std::unique_ptr<container_info_component> collect_container_info (vote_generator & vote_generator, const std::string & name);
};

class vote_generator_session final
{
public:
vote_generator_session (vote_generator & vote_generator_a);
void add (nano::block_hash const &);
void flush ();

private:
nano::vote_generator & generator;
std::vector<nano::block_hash> hashes;
};

std::unique_ptr<container_info_component> collect_container_info (vote_generator & vote_generator, const std::string & name);
class cached_votes final
{
Expand Down

0 comments on commit 60a4c2f

Please sign in to comment.