From 885447fedb8f08e1a4d2d7175aa1b0b4c7290e8c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Piotr=20W=C3=B3jcik?= <3044353+pwojcikdev@users.noreply.github.com> Date: Tue, 15 Oct 2024 13:03:38 +0200 Subject: [PATCH] Use channel as telemetry data key (#4697) * Use channel as telemetry data key * Erase dead channels --- nano/lib/stats_enums.hpp | 1 + nano/node/telemetry.cpp | 19 ++++++++++--------- nano/node/telemetry.hpp | 15 +++++++++++---- 3 files changed, 22 insertions(+), 13 deletions(-) diff --git a/nano/lib/stats_enums.hpp b/nano/lib/stats_enums.hpp index 6bf38cd5ef..ee9d8799dc 100644 --- a/nano/lib/stats_enums.hpp +++ b/nano/lib/stats_enums.hpp @@ -369,6 +369,7 @@ enum class detail failed_send_telemetry_req, empty_payload, cleanup_outdated, + erase_stale, // vote generator generator_broadcasts, diff --git a/nano/node/telemetry.cpp b/nano/node/telemetry.cpp index 1ae2a59be0..d7c659dcee 100644 --- a/nano/node/telemetry.cpp +++ b/nano/node/telemetry.cpp @@ -96,14 +96,11 @@ void nano::telemetry::process (const nano::telemetry_ack & telemetry, const std: nano::unique_lock lock{ mutex }; - const auto endpoint = channel->get_endpoint (); - - if (auto it = telemetries.get ().find (endpoint); it != telemetries.get ().end ()) + if (auto it = telemetries.get ().find (channel); it != telemetries.get ().end ()) { stats.inc (nano::stat::type::telemetry, nano::stat::detail::update); - telemetries.get ().modify (it, [&telemetry, &endpoint] (auto & entry) { - debug_assert (entry.endpoint == endpoint); + telemetries.get ().modify (it, [&telemetry, &channel] (auto & entry) { entry.data = telemetry.data; entry.last_updated = std::chrono::steady_clock::now (); }); @@ -111,7 +108,7 @@ void nano::telemetry::process (const nano::telemetry_ack & telemetry, const std: else { stats.inc (nano::stat::type::telemetry, nano::stat::detail::insert); - telemetries.get ().insert ({ endpoint, telemetry.data, std::chrono::steady_clock::now (), channel }); + telemetries.get ().insert ({ channel, telemetry.data, std::chrono::steady_clock::now () }); if (telemetries.size () > max_size) { @@ -247,10 +244,14 @@ void nano::telemetry::cleanup () // Remove if telemetry data is stale if (!check_timeout (entry)) { - stats.inc (nano::stat::type::telemetry, nano::stat::detail::cleanup_outdated); + stats.inc (nano::stat::type::telemetry, nano::stat::detail::erase_stale); + return true; // Erase + } + if (!entry.channel->alive ()) + { + stats.inc (nano::stat::type::telemetry, nano::stat::detail::erase_dead); return true; // Erase } - return false; // Do not erase }); } @@ -283,7 +284,7 @@ std::unordered_map nano::telemetry::get_al { if (check_timeout (entry)) { - result[entry.endpoint] = entry.data; + result[entry.endpoint ()] = entry.data; } } return result; diff --git a/nano/node/telemetry.hpp b/nano/node/telemetry.hpp index 6262cb25e2..7a615e7e2a 100644 --- a/nano/node/telemetry.hpp +++ b/nano/node/telemetry.hpp @@ -86,10 +86,14 @@ class telemetry private: struct entry { - nano::endpoint endpoint; + std::shared_ptr channel; nano::telemetry_data data; std::chrono::steady_clock::time_point last_updated; - std::shared_ptr channel; + + nano::endpoint endpoint () const + { + return channel->get_endpoint (); + } }; private: @@ -110,13 +114,16 @@ class telemetry private: // clang-format off class tag_sequenced {}; + class tag_channel {}; class tag_endpoint {}; using ordered_telemetries = boost::multi_index_container>, - mi::hashed_unique, - mi::member> + mi::ordered_unique, + mi::member, &entry::channel>>, + mi::hashed_non_unique, + mi::const_mem_fun> >>; // clang-format on