Skip to content

Commit

Permalink
Add vote_cache stats
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Nov 7, 2023
1 parent 344efdc commit 6c643f0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 15 deletions.
34 changes: 23 additions & 11 deletions nano/core_test/vote_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ nano::keypair create_rep (nano::uint128_t weight)

TEST (vote_cache, construction)
{
nano::test::system system;
nano::vote_cache_config cfg;
nano::vote_cache vote_cache{ cfg };
nano::vote_cache vote_cache{ cfg, system.stats };
ASSERT_EQ (0, vote_cache.size ());
ASSERT_TRUE (vote_cache.empty ());
auto hash1 = nano::test::random_hash ();
Expand All @@ -49,8 +50,9 @@ TEST (vote_cache, construction)
*/
TEST (vote_cache, insert_one_hash)
{
nano::test::system system;
nano::vote_cache_config cfg;
nano::vote_cache vote_cache{ cfg };
nano::vote_cache vote_cache{ cfg, system.stats };
vote_cache.rep_weight_query = rep_weight_query ();
auto rep1 = create_rep (7);
auto hash1 = nano::test::random_hash ();
Expand Down Expand Up @@ -79,8 +81,9 @@ TEST (vote_cache, insert_one_hash)
*/
TEST (vote_cache, insert_one_hash_many_votes)
{
nano::test::system system;
nano::vote_cache_config cfg;
nano::vote_cache vote_cache{ cfg };
nano::vote_cache vote_cache{ cfg, system.stats };
vote_cache.rep_weight_query = rep_weight_query ();
auto hash1 = nano::test::random_hash ();
auto rep1 = create_rep (7);
Expand Down Expand Up @@ -114,8 +117,9 @@ TEST (vote_cache, insert_one_hash_many_votes)
*/
TEST (vote_cache, insert_many_hashes_many_votes)
{
nano::test::system system;
nano::vote_cache_config cfg;
nano::vote_cache vote_cache{ cfg };
nano::vote_cache vote_cache{ cfg, system.stats };
vote_cache.rep_weight_query = rep_weight_query ();
// There will be 3 random hashes to vote for
auto hash1 = nano::test::random_hash ();
Expand Down Expand Up @@ -194,8 +198,9 @@ TEST (vote_cache, insert_many_hashes_many_votes)
*/
TEST (vote_cache, insert_duplicate)
{
nano::test::system system;
nano::vote_cache_config cfg;
nano::vote_cache vote_cache{ cfg };
nano::vote_cache vote_cache{ cfg, system.stats };
vote_cache.rep_weight_query = rep_weight_query ();
auto hash1 = nano::test::random_hash ();
auto rep1 = create_rep (9);
Expand All @@ -211,8 +216,9 @@ TEST (vote_cache, insert_duplicate)
*/
TEST (vote_cache, insert_newer)
{
nano::test::system system;
nano::vote_cache_config cfg;
nano::vote_cache vote_cache{ cfg };
nano::vote_cache vote_cache{ cfg, system.stats };
vote_cache.rep_weight_query = rep_weight_query ();
auto hash1 = nano::test::random_hash ();
auto rep1 = create_rep (9);
Expand All @@ -236,8 +242,9 @@ TEST (vote_cache, insert_newer)
*/
TEST (vote_cache, insert_older)
{
nano::test::system system;
nano::vote_cache_config cfg;
nano::vote_cache vote_cache{ cfg };
nano::vote_cache vote_cache{ cfg, system.stats };
vote_cache.rep_weight_query = rep_weight_query ();
auto hash1 = nano::test::random_hash ();
auto rep1 = create_rep (9);
Expand All @@ -259,8 +266,9 @@ TEST (vote_cache, insert_older)
*/
TEST (vote_cache, erase)
{
nano::test::system system;
nano::vote_cache_config cfg;
nano::vote_cache vote_cache{ cfg };
nano::vote_cache vote_cache{ cfg, system.stats };
vote_cache.rep_weight_query = rep_weight_query ();
auto hash1 = nano::test::random_hash ();
auto hash2 = nano::test::random_hash ();
Expand Down Expand Up @@ -298,10 +306,11 @@ TEST (vote_cache, erase)
*/
TEST (vote_cache, overfill)
{
nano::test::system system;
// Create a vote cache with max size set to 1024
nano::vote_cache_config cfg;
cfg.max_size = 1024;
nano::vote_cache vote_cache{ cfg };
nano::vote_cache vote_cache{ cfg, system.stats };
vote_cache.rep_weight_query = rep_weight_query ();
const int count = 16 * 1024;
for (int n = 0; n < count; ++n)
Expand All @@ -324,8 +333,9 @@ TEST (vote_cache, overfill)
*/
TEST (vote_cache, overfill_entry)
{
nano::test::system system;
nano::vote_cache_config cfg;
nano::vote_cache vote_cache{ cfg };
nano::vote_cache vote_cache{ cfg, system.stats };
vote_cache.rep_weight_query = rep_weight_query ();
const int count = 1024;
auto hash1 = nano::test::random_hash ();
Expand All @@ -336,4 +346,6 @@ TEST (vote_cache, overfill_entry)
vote_cache.vote (vote1->hashes.front (), vote1);
}
ASSERT_EQ (1, vote_cache.size ());
}
}

TEST (vote_cache,
2 changes: 2 additions & 0 deletions nano/lib/stats_enums.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,8 @@ enum class detail : uint8_t
update,
request,
broadcast,
cleanup,
top,

// processing queue
queue,
Expand Down
2 changes: 1 addition & 1 deletion nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ nano::node::node (boost::asio::io_context & io_ctx_a, std::filesystem::path cons
history{ config.network_params.voting },
vote_uniquer (block_uniquer),
confirmation_height_processor (ledger, write_database_queue, config.conf_height_processor_batch_min_time, config.logging, logger, node_initialized_latch, flags.confirmation_height_processor_mode),
vote_cache{ config.vote_cache },
vote_cache{ config.vote_cache, stats },
generator{ config, ledger, wallets, vote_processor, history, network, stats, /* non-final */ false },
final_generator{ config, ledger, wallets, vote_processor, history, network, stats, /* final */ true },
active (*this, confirmation_height_processor),
Expand Down
11 changes: 10 additions & 1 deletion nano/node/vote_cache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ std::chrono::steady_clock::time_point nano::vote_cache::entry::last_vote () cons

nano::vote_cache::vote_cache (vote_cache_config const & config_a, nano::stats & stats_a) :
config{ config_a },
stats{ stats_a },
cleanup_interval{ config_a.age_cutoff / 2 }
{
}
Expand All @@ -128,12 +129,16 @@ void nano::vote_cache::vote (const nano::block_hash & hash, const std::shared_pt
auto & cache_by_hash = cache.get<tag_hash> ();
if (auto existing = cache_by_hash.find (hash); existing != cache_by_hash.end ())
{
stats.inc (nano::stat::type::vote_cache, nano::stat::detail::update);

cache_by_hash.modify (existing, [this, &representative, &timestamp, &rep_weight] (entry & ent) {
ent.vote (representative, timestamp, rep_weight, config.max_voters);
});
}
else
{
stats.inc (nano::stat::type::vote_cache, nano::stat::detail::insert);

entry cache_entry{ hash };
cache_entry.vote (representative, timestamp, rep_weight, config.max_voters);

Expand Down Expand Up @@ -187,6 +192,8 @@ bool nano::vote_cache::erase (const nano::block_hash & hash)

std::vector<nano::vote_cache::top_entry> nano::vote_cache::top (const nano::uint128_t & min_tally)
{
stats.inc (nano::stat::type::vote_cache, nano::stat::detail::top);

std::vector<top_entry> results;
{
nano::lock_guard<nano::mutex> lock{ mutex };
Expand Down Expand Up @@ -225,6 +232,8 @@ void nano::vote_cache::cleanup ()
{
debug_assert (!mutex.try_lock ());

stats.inc (nano::stat::type::vote_cache, nano::stat::detail::cleanup);

auto const cutoff = std::chrono::steady_clock::now () - config.age_cutoff;

auto it = cache.begin ();
Expand All @@ -241,7 +250,7 @@ void nano::vote_cache::cleanup ()
}
}

std::unique_ptr<nano::container_info_component> nano::vote_cache::collect_container_info (const std::string & name)
std::unique_ptr<nano::container_info_component> nano::vote_cache::collect_container_info (const std::string & name) const
{
auto composite = std::make_unique<container_info_composite> (name);
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "cache", size (), sizeof (ordered_cache::value_type) }));
Expand Down
5 changes: 3 additions & 2 deletions nano/node/vote_cache.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ class vote_cache final
};

public:
explicit vote_cache (vote_cache_config const &);
explicit vote_cache (vote_cache_config const &, nano::stats &);

/**
* Adds a new vote to cache
Expand Down Expand Up @@ -126,7 +126,7 @@ class vote_cache final
std::vector<top_entry> top (nano::uint128_t const & min_tally);

public: // Container info
std::unique_ptr<nano::container_info_component> collect_container_info (std::string const & name);
std::unique_ptr<nano::container_info_component> collect_container_info (std::string const & name) const;

public:
/**
Expand All @@ -136,6 +136,7 @@ class vote_cache final

private: // Dependencies
vote_cache_config const & config;
nano::stats & stats;

private:
void cleanup ();
Expand Down

0 comments on commit 6c643f0

Please sign in to comment.