Skip to content

Commit

Permalink
EXPECTED MIN MAX WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
pwojcikdev committed Mar 5, 2024
1 parent 2829339 commit 66451de
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
6 changes: 3 additions & 3 deletions nano/lib/stats.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ auto nano::stats::count (stat::type type, stat::detail detail, stat::dir dir) co
return 0;
}

void nano::stats::sample (stat::sample sample, nano::stats::sampler_value_t value)
void nano::stats::sample (stat::sample sample, std::pair<sampler_value_t, sampler_value_t> expected_min_max, nano::stats::sampler_value_t value)
{
// Updates need to happen while holding the mutex
auto update_sampler = [this] (nano::stats::sampler_key key, auto && updater) {
auto update_sampler = [this, expected_min_max] (nano::stats::sampler_key key, auto && updater) {
// This is a two-step process to avoid exclusively locking the mutex in the common case
{
std::shared_lock lock{ mutex };
Expand All @@ -145,7 +145,7 @@ void nano::stats::sample (stat::sample sample, nano::stats::sampler_value_t valu
std::unique_lock lock{ mutex };

// Insertions will be ignored if the key already exists
auto [it, inserted] = samplers.emplace (key, std::make_unique<sampler_entry> (config.max_samples));
auto [it, inserted] = samplers.emplace (key, std::make_unique<sampler_entry> (config.max_samples, expected_min_max));
updater (*it->second);
}
};
Expand Down
9 changes: 6 additions & 3 deletions nano/lib/stats.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class stats final
counter_value_t count (stat::type type, stat::detail detail, stat::dir dir = stat::dir::in) const;

/** Adds a sample to the given sampler */
void sample (stat::sample sample, sampler_value_t value);
void sample (stat::sample sample, std::pair<sampler_value_t, sampler_value_t> expected_min_max, sampler_value_t value);

/** Returns a potentially empty list of the last N samples, where N is determined by the 'max_samples' configuration. Samples are reset after each lookup. */
std::vector<sampler_value_t> samples (stat::sample sample);
Expand Down Expand Up @@ -174,8 +174,11 @@ class stats final
class sampler_entry
{
public:
explicit sampler_entry (size_t max_samples) :
samples{ max_samples } {};
std::pair<sampler_value_t, sampler_value_t> const expected_min_max;

sampler_entry (size_t max_samples, std::pair<sampler_value_t, sampler_value_t> expected_min_max) :
samples{ max_samples },
expected_min_max{ expected_min_max } {};

// Prevent copying
sampler_entry (sampler_entry const &) = delete;
Expand Down
2 changes: 1 addition & 1 deletion nano/node/active_transactions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ void nano::active_transactions::cleanup_election (nano::unique_lock<nano::mutex>

lock_a.unlock ();

node.stats.sample (nano::stat::sample::active_election_duration, election->duration ().count ());
node.stats.sample (nano::stat::sample::active_election_duration, { 0, 1000 * 60 * 10 /* 0-10 minutes range */ }, election->duration ().count ());

vacancy_update ();

Expand Down
2 changes: 1 addition & 1 deletion nano/node/bootstrap_ascending/service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -362,7 +362,7 @@ void nano::bootstrap_ascending::service::process (nano::asc_pull_ack const & mes
tags_by_id.erase (iterator);

stats.inc (nano::stat::type::bootstrap_ascending, nano::stat::detail::reply);
stats.sample (nano::stat::sample::bootstrap_tag_duration, nano::milliseconds_since_epoch () - tag.time);
stats.sample (nano::stat::sample::bootstrap_tag_duration, { 0, config.bootstrap_ascending.timeout }, nano::milliseconds_since_epoch () - tag.time);

scoring.received_message (channel);

Expand Down
13 changes: 7 additions & 6 deletions nano/rpc_test/rpc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5827,13 +5827,13 @@ TEST (rpc, stats_counters)
auto node = add_ipc_enabled_node (system);
auto const rpc_ctx = add_rpc (system, node);

node->stats.sample (nano::stat::sample::active_election_duration, 1);
node->stats.sample (nano::stat::sample::active_election_duration, 2);
node->stats.sample (nano::stat::sample::active_election_duration, 3);
node->stats.sample (nano::stat::sample::active_election_duration, 4);
node->stats.sample (nano::stat::sample::active_election_duration, { 0, 10 }, 1);
node->stats.sample (nano::stat::sample::active_election_duration, { 0, 10 }, 2);
node->stats.sample (nano::stat::sample::active_election_duration, { 0, 10 }, 3);
node->stats.sample (nano::stat::sample::active_election_duration, { 0, 10 }, 4);

node->stats.sample (nano::stat::sample::bootstrap_tag_duration, 5);
node->stats.sample (nano::stat::sample::bootstrap_tag_duration, 5);
node->stats.sample (nano::stat::sample::bootstrap_tag_duration, { 0, 10 }, 5);
node->stats.sample (nano::stat::sample::bootstrap_tag_duration, { 0, 10 }, 5);

boost::property_tree::ptree request;
request.put ("action", "stats");
Expand All @@ -5847,6 +5847,7 @@ TEST (rpc, stats_counters)
std::cout << ss.str () << std::endl;
}

// TODO: Assert the response
// ASSERT_EQ (response.get_child ("node").get_child ("vote_uniquer").get_child ("cache").get<std::string> ("count"), "1");
}

Expand Down

0 comments on commit 66451de

Please sign in to comment.