nhancdt2602 commented on code in PR #3557:
URL: https://github.com/apache/kvrocks/pull/3557#discussion_r3577068596
##########
src/server/server.cc:
##########
@@ -1392,11 +1389,82 @@ int64_t Server::GetLastBgsaveTime() {
return last_bgsave_timestamp_secs_ == -1 ? start_time_secs_ :
last_bgsave_timestamp_secs_;
}
-Server::InfoEntries Server::GetStatsInfo() {
+void Server::initCommandStats(Stats *stats) {
+ auto commands = redis::CommandTable::GetOriginal();
+ for (const auto &iter : *commands) {
+ stats->commands_stats[iter.first].calls = 0;
+ stats->commands_stats[iter.first].latency = 0;
+
+ if (stats->bucket_boundaries.size() > 0) {
+ // NB: Extra index for the last bucket (Inf)
+ for (std::size_t i{0}; i <= stats->bucket_boundaries.size(); ++i) {
+
stats->commands_histogram[iter.first].buckets.push_back(std::make_unique<std::atomic<uint64_t>>(0));
+ }
+ stats->commands_histogram[iter.first].calls = 0;
+ stats->commands_histogram[iter.first].sum = 0;
+ }
+ }
+}
+
+std::shared_ptr<Stats> Server::GetOrCreateNamespaceStats(const std::string
&ns) {
+ {
+ std::shared_lock<std::shared_mutex> lock(ns_stats_mu_);
+ if (auto it = ns_stats_.find(ns); it != ns_stats_.end()) {
+ return it->second;
+ }
+ }
+
+ std::unique_lock<std::shared_mutex> lock(ns_stats_mu_);
+ if (auto it = ns_stats_.find(ns); it != ns_stats_.end()) {
+ return it->second;
+ }
+ auto ns_stats =
std::make_shared<Stats>(config_->histogram_bucket_boundaries);
+ initCommandStats(ns_stats.get());
+ ns_stats_[ns] = ns_stats;
+ return ns_stats;
+}
+
+std::shared_ptr<Stats> Server::AggregateNamespaceStats() {
+ auto agg = std::make_shared<Stats>(config_->histogram_bucket_boundaries);
+ initCommandStats(agg.get());
+
+ std::shared_lock<std::shared_mutex> lock(ns_stats_mu_);
+ for (const auto &[ns, ns_stats] : ns_stats_) {
+ agg->total_calls.fetch_add(ns_stats->total_calls.load(),
std::memory_order_relaxed);
+ for (const auto &[cmd, stat] : ns_stats->commands_stats) {
+ agg->commands_stats[cmd].calls.fetch_add(stat.calls.load(),
std::memory_order_relaxed);
+ agg->commands_stats[cmd].latency.fetch_add(stat.latency.load(),
std::memory_order_relaxed);
+ }
+ for (const auto &[cmd, hist] : ns_stats->commands_histogram) {
+ auto &agg_hist = agg->commands_histogram[cmd];
+ agg_hist.calls.fetch_add(hist.calls.load(), std::memory_order_relaxed);
+ agg_hist.sum.fetch_add(hist.sum.load(), std::memory_order_relaxed);
+ for (std::size_t i = 0; i < hist.buckets.size(); ++i) {
+ agg_hist.buckets[i]->fetch_add(hist.buckets[i]->load(),
std::memory_order_relaxed);
+ }
+ }
+ }
+ return agg;
+}
+
+void Server::ClearNamespaceStats(const std::string &ns) {
+ std::unique_lock<std::shared_mutex> lock(ns_stats_mu_);
+ ns_stats_.erase(ns);
+}
+
+Server::InfoEntries Server::GetStatsInfo(const std::string &ns) {
+ // Command stats are per namespace; the admin/default namespace sees the
aggregate across all of them.
+ auto cmd_stats_ptr = ns == kDefaultNamespace ? AggregateNamespaceStats() :
GetOrCreateNamespaceStats(ns);
+ const Stats &cmd_stats = *cmd_stats_ptr;
+
Server::InfoEntries entries;
entries.emplace_back("total_connections_received", total_clients_.load());
- entries.emplace_back("total_commands_processed", stats.total_calls.load());
- entries.emplace_back("instantaneous_ops_per_sec",
stats.GetInstantaneousMetric(STATS_METRIC_COMMAND));
+ entries.emplace_back("total_commands_processed",
cmd_stats.total_calls.load());
+ // Per-namespace ops/sec comes from the namespace's own sampled metric; the
admin/default view uses
+ // the global metric, which the sampler feeds with the sum across all
namespaces.
+ auto ops_per_sec = ns == kDefaultNamespace ?
stats.GetInstantaneousMetric(STATS_METRIC_COMMAND)
+ :
cmd_stats.GetInstantaneousMetric(STATS_METRIC_COMMAND);
+ entries.emplace_back("instantaneous_ops_per_sec", ops_per_sec);
Review Comment:
As the aggregation logic is fast, I believe we can ignore the concern here.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]