Skip to content

Commit

Permalink
Improve collect_container_info ()
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Feb 25, 2024
1 parent beb4c5b commit 638b18e
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 22 deletions.
2 changes: 1 addition & 1 deletion nano/node/node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ std::unique_ptr<nano::container_info_component> nano::collect_container_info (no
composite->add_component (collect_container_info (node.observers, "observers"));
composite->add_component (collect_container_info (node.wallets, "wallets"));
composite->add_component (collect_container_info (node.vote_processor, "vote_processor"));
composite->add_component (collect_container_info (node.rep_crawler, "rep_crawler"));
composite->add_component (node.rep_crawler.collect_container_info ("rep_crawler"));
composite->add_component (node.block_processor.collect_container_info ("block_processor"));
composite->add_component (collect_container_info (node.online_reps, "online_reps"));
composite->add_component (collect_container_info (node.history, "history"));
Expand Down
26 changes: 11 additions & 15 deletions nano/node/repcrawler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -440,13 +440,23 @@ std::vector<nano::representative> nano::rep_crawler::principal_representatives (
return representatives (count, node.minimum_principal_weight (), minimum_protocol_version);
}

/** Total number of representatives */
std::size_t nano::rep_crawler::representative_count ()
{
nano::lock_guard<nano::mutex> lock{ mutex };
return reps.size ();
}

std::unique_ptr<nano::container_info_component> nano::rep_crawler::collect_container_info (const std::string & name)
{
nano::lock_guard<nano::mutex> guard{ mutex };

auto composite = std::make_unique<container_info_composite> (name);
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "reps", reps.size (), sizeof (decltype (reps)::value_type) }));
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "queries", queries.size (), sizeof (decltype (queries)::value_type) }));
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "responses", responses.size (), sizeof (decltype (responses)::value_type) }));
return composite;
}

// Only for tests
void nano::rep_crawler::force_add_rep (const nano::account & account, const std::shared_ptr<nano::transport::channel> & channel)
{
Expand All @@ -471,20 +481,6 @@ void nano::rep_crawler::force_query (const nano::block_hash & hash, const std::s
queries.emplace (query_entry{ hash, channel });
}

std::unique_ptr<nano::container_info_component> nano::collect_container_info (rep_crawler & rep_crawler, std::string const & name)
{
std::size_t count;
{
nano::lock_guard<nano::mutex> guard{ rep_crawler.mutex };
count = rep_crawler.queries.size ();
}

auto const sizeof_element = sizeof (decltype (rep_crawler.queries)::value_type);
auto composite = std::make_unique<container_info_composite> (name);
composite->add_component (std::make_unique<container_info_leaf> (container_info{ "queries", count, sizeof_element }));
return composite;
}

/*
* rep_crawler_config
*/
Expand Down
9 changes: 3 additions & 6 deletions nano/node/repcrawler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ class rep_crawler_config final
*/
class rep_crawler final
{
friend std::unique_ptr<container_info_component> collect_container_info (rep_crawler & rep_crawler, std::string const & name);

public:
rep_crawler (rep_crawler_config const &, nano::node &);
~rep_crawler ();
Expand Down Expand Up @@ -88,9 +86,10 @@ class rep_crawler final
std::vector<representative> principal_representatives (std::size_t count = std::numeric_limits<std::size_t>::max (), std::optional<decltype (nano::network_constants::protocol_version)> const & minimum_protocol_version = {});

/** Total number of representatives */

std::size_t representative_count ();

std::unique_ptr<container_info_component> collect_container_info (std::string const & name);

private: // Dependencies
rep_crawler_config const & config;
nano::node & node;
Expand All @@ -111,7 +110,7 @@ class rep_crawler final
/** Returns a list of endpoints to crawl. The total weight is passed in to avoid computing it twice. */
std::vector<std::shared_ptr<nano::transport::channel>> prepare_crawl_targets (bool sufficient_weight) const;
std::optional<hash_root_t> prepare_query_target ();
bool track_rep_request (hash_root_t hash_root, std::shared_ptr<nano::transport::channel> const & channel_a);
bool track_rep_request (hash_root_t hash_root, std::shared_ptr<nano::transport::channel> const & channel);

private:
/**
Expand Down Expand Up @@ -190,6 +189,4 @@ class rep_crawler final
void force_process (std::shared_ptr<nano::vote> const & vote, std::shared_ptr<nano::transport::channel> const & channel);
void force_query (nano::block_hash const & hash, std::shared_ptr<nano::transport::channel> const & channel);
};

std::unique_ptr<container_info_component> collect_container_info (rep_crawler & rep_crawler, std::string const & name);
}

0 comments on commit 638b18e

Please sign in to comment.