4747#include " utils/blob.h"
4848#include " utils/error_code.h"
4949#include " utils/errors.h"
50+ #include " utils/math.h"
5051#include " utils/metrics.h"
5152#include " utils/output_utils.h"
5253#include " utils/ports.h"
@@ -98,11 +99,13 @@ dsn::metric_filters resource_usage_filters()
9899{
99100 dsn::metric_filters filters;
100101 filters.with_metric_fields = {dsn::kMetricNameField , dsn::kMetricSingleValueField };
101- filters.entity_types = {" server" , " replica" };
102+ filters.entity_types = {" server" , " replica" , " disk " };
102103 filters.entity_metrics = {" resident_mem_usage_mb" ,
103104 " rdb_block_cache_mem_usage_bytes" ,
104105 " rdb_memtable_mem_usage_bytes" ,
105- " rdb_index_and_filter_blocks_mem_usage_bytes" };
106+ " rdb_index_and_filter_blocks_mem_usage_bytes" ,
107+ " disk_capacity_total_mb" ,
108+ " disk_capacity_avail_mb" };
106109 return filters;
107110}
108111
@@ -117,24 +120,48 @@ dsn::error_s parse_resource_usage(const std::string &json_string, list_nodes_hel
117120 return FMT_ERR (dsn::ERR_INVALID_DATA, " invalid json string" );
118121 }
119122
123+ int64_t total_capacity_mb = 0 ;
124+ int64_t total_available_mb = 0 ;
125+ stat.disk_available_min_ratio = 100 ;
120126 for (const auto &entity : query_snapshot.entities ) {
121- for ( const auto &m : entity.metrics ) {
122- if (entity. type == " server " ) {
127+ if ( entity.type == " server " ) {
128+ for ( const auto &m : entity. metrics ) {
123129 if (m.name == " resident_mem_usage_mb" ) {
124130 stat.memused_res_mb += m.value ;
125131 } else if (m.name == " rdb_block_cache_mem_usage_bytes" ) {
126132 stat.block_cache_bytes += m.value ;
127133 }
128- } else if (entity.type == " replica" ) {
134+ }
135+ } else if (entity.type == " replica" ) {
136+ for (const auto &m : entity.metrics ) {
129137 if (m.name == " rdb_memtable_mem_usage_bytes" ) {
130138 stat.mem_tbl_bytes += m.value ;
131139 } else if (m.name == " rdb_index_and_filter_blocks_mem_usage_bytes" ) {
132140 stat.mem_idx_bytes += m.value ;
133141 }
134142 }
143+ } else if (entity.type == " disk" ) {
144+ int64_t capacity_mb = 0 ;
145+ int64_t available_mb = 0 ;
146+ for (const auto &m : entity.metrics ) {
147+ if (m.name == " disk_capacity_total_mb" ) {
148+ total_capacity_mb += m.value ;
149+ capacity_mb = m.value ;
150+ } else if (m.name == " disk_capacity_avail_mb" ) {
151+ total_available_mb += m.value ;
152+ available_mb = m.value ;
153+ }
154+ }
155+
156+ const auto available_ratio = dsn::utils::calc_percentage (available_mb, capacity_mb);
157+ stat.disk_available_min_ratio =
158+ std::min (stat.disk_available_min_ratio , available_ratio);
135159 }
136160 }
137161
162+ stat.disk_available_total_ratio =
163+ dsn::utils::calc_percentage (total_available_mb, total_capacity_mb);
164+
138165 return dsn::error_s::ok ();
139166}
140167
@@ -281,10 +308,6 @@ bool ls_nodes(command_executor *e, shell_context *sc, arguments args)
281308
282309 const auto &results = get_metrics (nodes, resource_usage_filters ().to_query_string ());
283310
284- // TODO(wangdan): following replica-level and disk-level metrics would be replaced:
285- // "replica*eon.replica_stub*disk.available.total.ratio"
286- // "replica*eon.replica_stub*disk.available.min.ratio"
287-
288311 for (size_t i = 0 ; i < nodes.size (); ++i) {
289312 auto tmp_it = tmp_map.find (nodes[i].address );
290313 if (tmp_it == tmp_map.end ()) {
@@ -312,15 +335,6 @@ bool ls_nodes(command_executor *e, shell_context *sc, arguments args)
312335 << " failed: " << res << std::endl;
313336 return true ;
314337 }
315-
316- // TODO(wangdan): after migrated to new metrics, remove following code:
317- dsn::perf_counter_info info;
318- for (dsn::perf_counter_metric &m : info.counters ) {
319- if (m.name .find (" disk.available.total.ratio" ) != std::string::npos)
320- stat.disk_available_total_ratio += m.value ;
321- else if (m.name .find (" disk.available.min.ratio" ) != std::string::npos)
322- stat.disk_available_min_ratio += m.value ;
323- }
324338 }
325339 }
326340
0 commit comments