Skip to content

Commit

Permalink
refactor and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
empiredan committed Dec 31, 2024
1 parent 664e74c commit 0d40cb3
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 125 deletions.
105 changes: 0 additions & 105 deletions src/client/replication_ddl_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -552,111 +552,6 @@ std::string replication_ddl_client::node_name(const host_port &hp, bool resolve_
return dns_resolver::instance().resolve_address(hp).to_string();
}

dsn::error_code replication_ddl_client::list_nodes(dsn::replication::node_status::type status,
bool detailed,
const std::string &file_name,
bool resolve_ip)
{
std::map<dsn::host_port, dsn::replication::node_status::type> nodes;
auto r = list_nodes(status, nodes);
if (r != dsn::ERR_OK) {
return r;
}

std::map<dsn::host_port, list_nodes_helper> tmp_map;
int alive_node_count = 0;
for (const auto &[hp, type] : nodes) {
if (type == dsn::replication::node_status::NS_ALIVE) {
alive_node_count++;
}
std::string status_str = enum_to_string(type);
status_str = status_str.substr(status_str.find("NS_") + 3);
tmp_map.emplace(hp, list_nodes_helper(node_name(hp, resolve_ip), status_str));
}

if (detailed) {
std::vector<::dsn::app_info> apps;
const auto &result = list_apps(dsn::app_status::AS_AVAILABLE, apps);
if (!result) {
return result.code();
}

for (auto &app : apps) {
int32_t app_id;
int32_t partition_count;
std::vector<partition_configuration> pcs;
r = list_app(app.app_name, app_id, partition_count, pcs);
if (r != dsn::ERR_OK) {
return r;
}

for (const auto &pc : pcs) {
if (pc.hp_primary) {
auto find = tmp_map.find(pc.hp_primary);
if (find != tmp_map.end()) {
find->second.primary_count++;
}
}
for (const auto &secondary : pc.hp_secondaries) {
auto find = tmp_map.find(secondary);
if (find != tmp_map.end()) {
find->second.secondary_count++;
}
}
}
}
}

// print configuration_list_nodes_response
std::streambuf *buf;
std::ofstream of;

if (!file_name.empty()) {
of.open(file_name);
buf = of.rdbuf();
} else {
buf = std::cout.rdbuf();
}
std::ostream out(buf);

dsn::utils::table_printer tp;
tp.add_title("address");
tp.add_column("status");
if (detailed) {
tp.add_column("replica_count");
tp.add_column("primary_count");
tp.add_column("secondary_count");
}
for (auto &kv : tmp_map) {
tp.add_row(kv.second.node_name);
tp.append_data(kv.second.node_status);
if (detailed) {
tp.append_data(kv.second.primary_count + kv.second.secondary_count);
tp.append_data(kv.second.primary_count);
tp.append_data(kv.second.secondary_count);
}
}
tp.output(out);
out << std::endl;

dsn::utils::table_printer tp_count;
tp_count.add_row_name_and_data("total_node_count", nodes.size());
tp_count.add_row_name_and_data("alive_node_count", alive_node_count);
tp_count.add_row_name_and_data("unalive_node_count", nodes.size() - alive_node_count);
tp_count.output(out);
out << std::endl;

return dsn::ERR_OK;
#undef RESOLVE
}

dsn::error_code replication_ddl_client::list_nodes(dsn::replication::node_status::type status,
bool detailed,
const std::string &file_name)
{
return list_nodes(status, detailed, file_name, false);
}

dsn::error_code replication_ddl_client::cluster_name(int64_t timeout_ms, std::string &cluster_name)
{
auto req = std::make_shared<configuration_cluster_info_request>();
Expand Down
39 changes: 30 additions & 9 deletions src/client/replication_ddl_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,18 @@ class replication_ddl_client
error_with<configuration_rename_app_response> rename_app(const std::string &old_app_name,
const std::string &new_app_name);

// Choose tables and list them to a file, with path specified as `output_file`. Once
// `output_file` is empty, tables would be listed to stdout.
//
// Choose tables according to following parameters:
// `show_all`: whether to show all tables, not only the available, but also the ones in
// other status, e.g. the dropped tables.
// `detailed`: whether to show healthy/unhealthy details.
// `json`: whether to output as json format.
// `status`: the status of the tables chosen to be listed. `app_status::AS_INVALID` means
// no restriction.
// `app_name_pattern`: the name pattern of the tables chosen to be listed.
// `match_type`: the type in which the name pattern would be matched.
error_s list_apps(bool show_all,
bool detailed,
bool json,
Expand All @@ -103,28 +115,37 @@ class replication_ddl_client
const std::string &app_name_pattern,
utils::pattern_match_type::type match_type);

// The same as the above, except that there's no restriction on table name; in other
// words, the match type is `PMT_MATCH_ALL`.
error_s list_apps(bool show_all,
bool detailed,
bool json,
const std::string &output_file,
dsn::app_status::type status);

// Create and send request to meta server to get the tables chosen to be listed according
// to the following parameters:
// `status`: the status of the tables chosen to be listed. `app_status::AS_INVALID` means
// no restriction.
// `app_name_pattern`: the name pattern of the tables chosen to be listed.
// `match_type`: the type in which the name pattern would be matched.
//
// `apps` is just the tables chosen to be listed.
error_s list_apps(dsn::app_status::type status,
const std::string &app_name_pattern,
utils::pattern_match_type::type match_type,
std::vector<::dsn::app_info> &apps);

// The same as the above, except that there's no restriction on table name; in other
// words, the match type is `PMT_MATCH_ALL`.
error_s list_apps(dsn::app_status::type status, std::vector<::dsn::app_info> &apps);

dsn::error_code list_nodes(dsn::replication::node_status::type status,
bool detailed,
const std::string &file_name,
bool resolve_ip);

dsn::error_code list_nodes(dsn::replication::node_status::type status,
bool detailed,
const std::string &file_name);

// Create and send request to meta server to get the nodes chosen to be listed according
// to the following parameters:
// `status`: the status of the nodes chosen to be listed. `node_status::NS_INVALID` means
// no restriction.
//
// `nodes` is just the nodes chosen to be listed.
dsn::error_code
list_nodes(dsn::replication::node_status::type status,
std::map<dsn::host_port, dsn::replication::node_status::type> &nodes);
Expand Down
14 changes: 7 additions & 7 deletions src/meta/meta_http_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ struct list_nodes_helper
}
};

#define INIT_AND_CALL_LIST_APPS(target_status, list_apps_req, list_apps_resp, http_resp) \
configuration_list_apps_request list_apps_req; \
#define INIT_AND_CALL_LIST_APPS(target_status, list_apps_resp, http_resp) \
configuration_list_apps_response list_apps_resp; \
do { \
configuration_list_apps_request list_apps_req; \
list_apps_req.status = target_status; \
_service->_state->list_apps(list_apps_req, list_apps_resp); \
if (list_apps_resp.err != ERR_OK) { \
Expand Down Expand Up @@ -238,7 +238,7 @@ void meta_http_service::list_app_handler(const http_request &req, http_response
return;
}

INIT_AND_CALL_LIST_APPS(app_status::AS_INVALID, list_apps_req, list_apps_resp, resp);
INIT_AND_CALL_LIST_APPS(app_status::AS_INVALID, list_apps_resp, resp);

// output as json format
std::ostringstream out;
Expand Down Expand Up @@ -312,7 +312,7 @@ void meta_http_service::list_app_handler(const http_request &req, http_response
tp_health.add_column("unhealthy");
tp_health.add_column("write_unhealthy");
tp_health.add_column("read_unhealthy");
for (auto &info : list_apps_resp.infos) {
for (const auto &info : list_apps_resp.infos) {
if (info.status != app_status::AS_AVAILABLE) {
continue;
}
Expand Down Expand Up @@ -405,7 +405,7 @@ void meta_http_service::list_node_handler(const http_request &req, http_response
size_t unalive_node_count = (_service->_dead_set).size();

if (detailed) {
INIT_AND_CALL_LIST_APPS(app_status::AS_AVAILABLE, list_apps_req, list_apps_resp, resp);
INIT_AND_CALL_LIST_APPS(app_status::AS_AVAILABLE, list_apps_resp, resp);

for (const auto &app : list_apps_resp.infos) {
query_cfg_request request_app;
Expand Down Expand Up @@ -523,11 +523,11 @@ void meta_http_service::get_app_envs_handler(const http_request &req, http_respo
}

// get all of the apps
INIT_AND_CALL_LIST_APPS(app_status::AS_AVAILABLE, list_apps_req, list_apps_resp, resp);
INIT_AND_CALL_LIST_APPS(app_status::AS_AVAILABLE, list_apps_resp, resp);

// using app envs to generate a table_printer
dsn::utils::table_printer tp;
for (auto &app : list_apps_resp.infos) {
for (const auto &app : list_apps_resp.infos) {
if (app.app_name == app_name) {
for (const auto &env : app.envs) {
tp.add_row_name_and_data(env.first, env.second);
Expand Down
7 changes: 7 additions & 0 deletions src/meta/server_state.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1624,6 +1624,7 @@ void server_state::list_apps(const configuration_list_apps_request &request,
zauto_read_lock l(_lock);

for (const auto &[_, app] : _all_apps) {
// Any table chosen to be listed must match the pattern, if any.
if (request.__isset.app_name_pattern && request.__isset.match_type) {
const auto &err =
utils::pattern_match(app->app_name, request.app_name_pattern, request.match_type);
Expand All @@ -1648,6 +1649,12 @@ void server_state::list_apps(const configuration_list_apps_request &request,
}
}

// Only in the following two cases would a table be chosen to be listed, according to
// the requested status:
//
// * `app_status::AS_INVALID` means no filter, in other words, any table with any status
// could be chosen;
// * or, current status of a table is the same as the requested status.
if (request.status != app_status::AS_INVALID && request.status != app->status) {
continue;
}
Expand Down
16 changes: 12 additions & 4 deletions src/utils/output_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -227,8 +227,8 @@ class multi_table_printer
std::vector<table_printer> _tps;
};

// Used as a general interface for printer to output to a file, typically `table_printer`
// and `multi_table_printer`.
// Used as a general interface for printer to output to a file in json or tabular format,
// typically `table_printer` and `multi_table_printer`.
template <typename Printer>
void output(const std::string &file_path, bool json, const Printer &printer)
{
Expand All @@ -249,8 +249,16 @@ void output(const std::string &file_path, bool json, const Printer &printer)
: table_printer::output_format::kTabular);
}

// Used as a general interface for printer to output to stdout, typically `table_printer`
// and `multi_table_printer`.
// Used as a general interface for printer to output to a file in tabular format, typically
// `table_printer` and `multi_table_printer`.
template <typename Printer>
void output(const std::string &file_path, const Printer &printer)
{
output(file_path, false, printer);
}

// Used as a general interface for printer to output to stdout in json or tabular format,
// typically `table_printer` and `multi_table_printer`.
template <typename Printer>
void output(bool json, const Printer &printer)
{
Expand Down

0 comments on commit 0d40cb3

Please sign in to comment.