Skip to content

Commit

Permalink
feat(new_metrics): support server_stat command showing some importa…
Browse files Browse the repository at this point in the history
…nt server-level metrics (part 1) (apache#2085)

As the 1st part that support `server_stat` command, both built-in metrics, the usage
of virtual and physical memory would be shown.
  • Loading branch information
empiredan authored Aug 23, 2024
1 parent 5b9f46a commit c99cfd6
Show file tree
Hide file tree
Showing 10 changed files with 364 additions and 80 deletions.
2 changes: 1 addition & 1 deletion .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# https://releases.llvm.org/14.0.0/tools/clang/tools/extra/docs/clang-tidy/index.html

CheckOptions: []
Checks: 'abseil-*,boost-*,bugprone-*,cert-*,clang-analyzer-*,concurrency-*,cppcoreguidelines-*,darwin-*,fuchsia-*,google-*,hicpp-*,linuxkernel-*,llvm-*,misc-*,modernize-*,performance-*,portability-*,readability-*,-cppcoreguidelines-pro-type-union-access,-llvm-include-order,-modernize-use-trailing-return-type,-cppcoreguidelines-avoid-non-const-global-variables,-fuchsia-statically-constructed-objects,-fuchsia-overloaded-operator,-bugprone-easily-swappable-parameters,-cppcoreguidelines-non-private-member-variables-in-classes,-misc-non-private-member-variables-in-classes,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-hicpp-no-array-decay,-hicpp-named-parameter,-readability-named-parameter,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-readability-function-cognitive-complexity,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-hicpp-avoid-c-arrays,-modernize-avoid-c-arrays,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-type-const-cast,-readability-identifier-length,-fuchsia-default-arguments-calls,-google-readability-avoid-underscore-in-googletest-name,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers'
Checks: 'abseil-*,boost-*,bugprone-*,cert-*,clang-analyzer-*,concurrency-*,cppcoreguidelines-*,darwin-*,fuchsia-*,google-*,hicpp-*,linuxkernel-*,llvm-*,misc-*,modernize-*,performance-*,portability-*,readability-*,-cppcoreguidelines-pro-type-union-access,-llvm-include-order,-misc-definitions-in-headers,-modernize-use-trailing-return-type,-cppcoreguidelines-macro-usage,-modernize-replace-disallow-copy-and-assign-macro,-bugprone-macro-parentheses,-cppcoreguidelines-avoid-non-const-global-variables,-fuchsia-statically-constructed-objects,-fuchsia-overloaded-operator,-bugprone-easily-swappable-parameters,-cppcoreguidelines-non-private-member-variables-in-classes,-misc-non-private-member-variables-in-classes,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-hicpp-no-array-decay,-hicpp-named-parameter,-readability-named-parameter,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-readability-function-cognitive-complexity,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-hicpp-avoid-c-arrays,-modernize-avoid-c-arrays,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-type-const-cast,-concurrency-mt-unsafe,-readability-identifier-length,-fuchsia-default-arguments-calls,-google-readability-avoid-underscore-in-googletest-name,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers'
ExtraArgs:
ExtraArgsBefore: []
FormatStyle: none
Expand Down
3 changes: 2 additions & 1 deletion .github/workflows/module_labeler_conf.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
# under the License.
---
permissions:
contents: write
contents: 'write'
pull-requests: 'write'

github:
- .github/**/*
Expand Down
2 changes: 1 addition & 1 deletion build_tools/clang_tidy.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ def tidy_on_path(path):
"clang-tidy",
"-p0",
"-path", BUILD_PATH,
"-checks=-cppcoreguidelines-pro-type-union-access,-llvm-include-order,-modernize-use-trailing-return-type,-cppcoreguidelines-avoid-non-const-global-variables,-fuchsia-statically-constructed-objects,-fuchsia-overloaded-operator,-bugprone-easily-swappable-parameters,-cppcoreguidelines-non-private-member-variables-in-classes,-misc-non-private-member-variables-in-classes,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-hicpp-no-array-decay,-hicpp-named-parameter,-readability-named-parameter,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-readability-function-cognitive-complexity,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-hicpp-avoid-c-arrays,-modernize-avoid-c-arrays,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-type-const-cast,-readability-identifier-length,-fuchsia-default-arguments-calls,-google-readability-avoid-underscore-in-googletest-name,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers",
"-checks=-cppcoreguidelines-pro-type-union-access,-llvm-include-order,-misc-definitions-in-headers,-modernize-use-trailing-return-type,-cppcoreguidelines-macro-usage,-modernize-replace-disallow-copy-and-assign-macro,-bugprone-macro-parentheses,-cppcoreguidelines-avoid-non-const-global-variables,-fuchsia-statically-constructed-objects,-fuchsia-overloaded-operator,-bugprone-easily-swappable-parameters,-cppcoreguidelines-non-private-member-variables-in-classes,-misc-non-private-member-variables-in-classes,-cppcoreguidelines-pro-bounds-array-to-pointer-decay,-hicpp-no-array-decay,-hicpp-named-parameter,-readability-named-parameter,-cppcoreguidelines-pro-bounds-pointer-arithmetic,-readability-function-cognitive-complexity,-cert-err58-cpp,-cppcoreguidelines-avoid-c-arrays,-hicpp-avoid-c-arrays,-modernize-avoid-c-arrays,-cppcoreguidelines-owning-memory,-cppcoreguidelines-pro-type-const-cast,-concurrency-mt-unsafe,-readability-identifier-length,-fuchsia-default-arguments-calls,-google-readability-avoid-underscore-in-googletest-name,-cppcoreguidelines-avoid-magic-numbers,-readability-magic-numbers",
"-extra-arg=-language=c++",
"-extra-arg=-std=c++17",
"-extra-arg=-Ithirdparty/output/include"]
Expand Down
102 changes: 82 additions & 20 deletions src/shell/command_helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -709,30 +709,67 @@ inline std::vector<dsn::http_result> get_metrics(const std::vector<node_desc> &n
return results;
}

// Adapt the result returned by `get_metrics` into the structure that could be processed by
// `remote_command`.
template <typename... Args>
inline dsn::error_s process_get_metrics_result(const dsn::http_result &result,
const node_desc &node,
const char *what,
Args &&...args)
{
if (dsn_unlikely(!result.error())) {
return FMT_ERR(result.error().code(),
"ERROR: query {} metrics from node {} failed, msg={}",
fmt::format(what, std::forward<Args>(args)...),
node.hp,
result.error());
}

if (dsn_unlikely(result.status() != dsn::http_status_code::kOk)) {
return FMT_ERR(dsn::ERR_HTTP_ERROR,
"ERROR: query {} metrics from node {} failed, http_status={}, msg={}",
fmt::format(what, std::forward<Args>(args)...),
node.hp,
dsn::get_http_status_message(result.status()),
result.body());
}

return dsn::error_s::ok();
}

#define RETURN_SHELL_IF_GET_METRICS_FAILED(result, node, what, ...) \
do { \
if (dsn_unlikely(!result.error())) { \
std::cout << "ERROR: send http request to query " << fmt::format(what, ##__VA_ARGS__) \
<< " metrics from node " << node.hp << " failed: " << result.error() \
<< std::endl; \
return true; \
} \
if (dsn_unlikely(result.status() != dsn::http_status_code::kOk)) { \
std::cout << "ERROR: send http request to query " << what << " metrics from node " \
<< node.hp << " failed: " << dsn::get_http_status_message(result.status()) \
<< std::endl \
<< result.body() << std::endl; \
const auto &res = process_get_metrics_result(result, node, what, ##__VA_ARGS__); \
if (dsn_unlikely(!res)) { \
fmt::println(res.description()); \
return true; \
} \
} while (0)

// Adapt the result of some parsing operations on the metrics returned by `get_metrics` into the
// structure that could be processed by `remote_command`.
template <typename... Args>
inline dsn::error_s process_parse_metrics_result(const dsn::error_s &result,
const node_desc &node,
const char *what,
Args &&...args)
{
if (dsn_unlikely(!result)) {
return FMT_ERR(result.code(),
"ERROR: {} metrics response from node {} failed, msg={}",
fmt::format(what, std::forward<Args>(args)...),
node.hp,
result);
}

return dsn::error_s::ok();
}

#define RETURN_SHELL_IF_PARSE_METRICS_FAILED(expr, node, what, ...) \
do { \
const auto &res = (expr); \
const auto &res = process_parse_metrics_result(expr, node, what, ##__VA_ARGS__); \
if (dsn_unlikely(!res)) { \
std::cout << "ERROR: parse " << fmt::format(what, ##__VA_ARGS__) \
<< " metrics response from node " << node.hp << " failed: " << res \
<< std::endl; \
fmt::println(res.description()); \
return true; \
} \
} while (0)
Expand Down Expand Up @@ -832,37 +869,59 @@ class aggregate_stats_calcs
}

// Create the aggregations as needed.
DEF_CALC_CREATOR(assignments)
DEF_CALC_CREATOR(sums)
DEF_CALC_CREATOR(increases)
DEF_CALC_CREATOR(rates)

#undef DEF_CALC_CREATOR

#define CALC_ASSIGNMENT_STATS(entities) \
do { \
if (_assignments) { \
RETURN_NOT_OK(_assignments->assign(entities)); \
} \
} while (0)

#define CALC_ACCUM_STATS(entities) \
do { \
if (_sums) { \
RETURN_NOT_OK(_sums->add_assign(entities)); \
} \
} while (0)

// Perform the chosen accum aggregations on the fetched metrics.
// Perform the chosen aggregations (both assignment and accum) on the fetched metrics.
dsn::error_s aggregate_metrics(const std::string &json_string)
{
DESERIALIZE_METRIC_QUERY_BRIEF_SNAPSHOT(value, json_string, query_snapshot);

return aggregate_metrics(query_snapshot);
}

dsn::error_s aggregate_metrics(const dsn::metric_query_brief_value_snapshot &query_snapshot)
{
CALC_ASSIGNMENT_STATS(query_snapshot.entities);
CALC_ACCUM_STATS(query_snapshot.entities);

return dsn::error_s::ok();
}

// Perform all of the chosen aggregations (both accum and delta) on the fetched metrics.
// Perform the chosen aggregations (assignement, accum, delta and rate) on the fetched metrics.
dsn::error_s aggregate_metrics(const std::string &json_string_start,
const std::string &json_string_end)
{
DESERIALIZE_METRIC_QUERY_BRIEF_2_SAMPLES(
json_string_start, json_string_end, query_snapshot_start, query_snapshot_end);

// Apply ending sample to the accum aggregations.
return aggregate_metrics(query_snapshot_start, query_snapshot_end);
}

dsn::error_s
aggregate_metrics(const dsn::metric_query_brief_value_snapshot &query_snapshot_start,
const dsn::metric_query_brief_value_snapshot &query_snapshot_end)
{
// Apply ending sample to the assignment and accum aggregations.
CALC_ASSIGNMENT_STATS(query_snapshot_end.entities);
CALC_ACCUM_STATS(query_snapshot_end.entities);

const std::array deltas_list = {&_increases, &_rates};
Expand All @@ -884,9 +943,12 @@ class aggregate_stats_calcs

#undef CALC_ACCUM_STATS

#undef CALC_ASSIGNMENT_STATS

private:
DISALLOW_COPY_AND_ASSIGN(aggregate_stats_calcs);

std::unique_ptr<aggregate_stats> _assignments;
std::unique_ptr<aggregate_stats> _sums;
std::unique_ptr<aggregate_stats> _increases;
std::unique_ptr<aggregate_stats> _rates;
Expand Down Expand Up @@ -1940,7 +2002,7 @@ get_table_stats(shell_context *sc, uint32_t sample_interval_ms, std::vector<row_
RETURN_SHELL_IF_PARSE_METRICS_FAILED(
calcs->aggregate_metrics(results_start[i].body(), results_end[i].body()),
nodes[i],
"row data requests");
"aggregate row data requests");
}

return true;
Expand Down Expand Up @@ -1990,7 +2052,7 @@ inline bool get_partition_stats(shell_context *sc,
RETURN_SHELL_IF_PARSE_METRICS_FAILED(
calcs->aggregate_metrics(results_start[i].body(), results_end[i].body()),
nodes[i],
"row data requests for table(id={})",
"aggregate row data requests for table(id={})",
table_id);
}

Expand Down
3 changes: 1 addition & 2 deletions src/shell/commands/data_operations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,6 @@
#include "utils/blob.h"
#include "utils/defer.h"
#include "utils/error_code.h"
#include "utils/errors.h"
#include "utils/flags.h"
#include "utils/fmt_logging.h"
#include "utils/metrics.h"
Expand Down Expand Up @@ -2294,7 +2293,7 @@ bool get_rdb_estimated_keys_stats(shell_context *sc,
create_rdb_estimated_keys_stats_calcs(table_id, pcs, nodes[i].hp, "replica", rows);
RETURN_SHELL_IF_PARSE_METRICS_FAILED(calcs->aggregate_metrics(results[i].body()),
nodes[i],
"rdb_estimated_keys for table(id={})",
"aggregate rdb_estimated_keys for table(id={})",
table_id);
}

Expand Down
Loading

0 comments on commit c99cfd6

Please sign in to comment.