Skip to content

Commit

Permalink
refactpr
Browse files Browse the repository at this point in the history
  • Loading branch information
empiredan committed Dec 30, 2024
1 parent 0f9e6ff commit 98b60d4
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 64 deletions.
2 changes: 1 addition & 1 deletion idl/duplication.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ struct duplication_sync_response
// per-duplication info and progress of each partition for one or multiple tables.
struct duplication_list_request
{
// The pattern used to match an app name, whose type is specified by `match_type`.
// The pattern is used to match an app name, whose type is specified by `match_type`.
1:string app_name_pattern;
2:utils.pattern_match_type match_type;
}
Expand Down
3 changes: 2 additions & 1 deletion idl/meta_admin.thrift
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ struct configuration_list_apps_request
{
1:dsn.layer2.app_status status = app_status.AS_INVALID;

// The pattern used to match an app name, whose type is specified by `match_type`.
// The pattern is used to match an app name, whose type is specified by `match_type`.
2:optional string app_name_pattern;
3:optional utils.pattern_match_type match_type;
}
Expand All @@ -209,6 +209,7 @@ struct configuration_list_apps_response
1:dsn.error_code err;
2:list<dsn.layer2.app_info> infos;

// Extra message to describe the error.
3:optional string hint_message;
}

Expand Down
15 changes: 8 additions & 7 deletions src/client/replication_ddl_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -218,22 +218,23 @@ dsn::error_code replication_ddl_client::create_app(const std::string &app_name,
const auto &req_status = request_meta_and_wait_response(RPC_CM_CREATE_APP, req, resp);

if (!req_status) {
std::cout << "create app " << app_name
<< " failed: [create] call server error: " << req_status << std::endl;
fmt::println("create app {} failed: [create] call server error: {}", app_name, req_status);
return req_status.code();
}

if (resp.err != dsn::ERR_OK) {
std::cout << "create app " << app_name
<< " failed: [create] received server error: " << resp.err << std::endl;
fmt::println(
"create app {} failed: [create] received server error: {}", app_name, resp.err);
return resp.err;
}

std::cout << "create app " << app_name << " succeed, waiting for app ready" << std::endl;
fmt::println("create app {} succeed, waiting for app ready", app_name);

dsn::error_code error = wait_app_ready(app_name, partition_count, replica_count);
if (error == dsn::ERR_OK)
std::cout << app_name << " is ready now!" << std::endl;
if (error == dsn::ERR_OK) {
fmt::println("{} is ready now!", app_name);
}

return error;
}

Expand Down
51 changes: 22 additions & 29 deletions src/meta/meta_http_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ 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; \
configuration_list_apps_response list_apps_resp; \
do { \
list_apps_req.status = target_status; \
_service->_state->list_apps(list_apps_req, list_apps_resp); \
if (list_apps_resp.err != ERR_OK) { \
http_resp.status_code = http_status_code::kInternalServerError; \
http_resp.body = \
error_s::make(list_apps_resp.err, list_apps_resp.hint_message).description(); \
return; \
} \
} while (false)

void meta_http_service::get_app_handler(const http_request &req, http_response &resp)
{
std::string app_name;
Expand Down Expand Up @@ -223,18 +237,7 @@ void meta_http_service::list_app_handler(const http_request &req, http_response
return;
}

configuration_list_apps_response response;
configuration_list_apps_request request;
request.status = dsn::app_status::AS_INVALID;

_service->_state->list_apps(request, response);

if (response.err != dsn::ERR_OK) {
resp.body = error_s::make(response.err, response.hint_message).description();
resp.status_code = http_status_code::kInternalServerError;
return;
}
std::vector<::dsn::app_info> &apps = response.infos;
INIT_AND_CALL_LIST_APPS(app_status::AS_INVALID, list_apps_req, list_apps_resp, resp);

// output as json format
std::ostringstream out;
Expand All @@ -252,7 +255,7 @@ void meta_http_service::list_app_handler(const http_request &req, http_response
tp_general.add_column("drop_time");
tp_general.add_column("drop_expire");
tp_general.add_column("envs_count");
for (const auto &app : apps) {
for (const auto &app : list_apps_resp.infos) {
if (app.status != dsn::app_status::AS_AVAILABLE) {
continue;
}
Expand Down Expand Up @@ -308,7 +311,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 : apps) {
for (auto &info : list_apps_resp.infos) {
if (info.status != app_status::AS_AVAILABLE) {
continue;
}
Expand Down Expand Up @@ -400,11 +403,9 @@ void meta_http_service::list_node_handler(const http_request &req, http_response
int unalive_node_count = (_service->_dead_set).size();

if (detailed) {
configuration_list_apps_response response;
configuration_list_apps_request request;
request.status = dsn::app_status::AS_AVAILABLE;
_service->_state->list_apps(request, response);
for (const auto &app : response.infos) {
INIT_AND_CALL_LIST_APPS(app_status::AS_AVAILABLE, list_apps_req, list_apps_resp, resp);

for (const auto &app : list_apps_resp.infos) {
query_cfg_request request_app;
query_cfg_response response_app;
request_app.app_name = app.app_name;
Expand Down Expand Up @@ -520,19 +521,11 @@ void meta_http_service::get_app_envs_handler(const http_request &req, http_respo
}

// get all of the apps
configuration_list_apps_response response;
configuration_list_apps_request request;
request.status = dsn::app_status::AS_AVAILABLE;
_service->_state->list_apps(request, response);
if (response.err != dsn::ERR_OK) {
resp.body = error_s::make(response.err, response.hint_message).description();
resp.status_code = http_status_code::kInternalServerError;
return;
}
INIT_AND_CALL_LIST_APPS(app_status::AS_AVAILABLE, list_apps_req, list_apps_resp, resp);

// using app envs to generate a table_printer
dsn::utils::table_printer tp;
for (auto &app : response.infos) {
for (auto &app : list_apps_resp.infos) {
if (app.app_name == app_name) {
for (auto env : app.envs) {
tp.add_row_name_and_data(env.first, env.second);
Expand Down
36 changes: 11 additions & 25 deletions src/shell/commands/node_management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -454,14 +454,14 @@ bool ls_nodes(command_executor *, shell_context *sc, arguments args)
}
}

dsn::utils::multi_table_printer mtp;
dsn::utils::multi_table_printer multi_printer;
if (!(status.empty() && output_file.empty())) {
dsn::utils::table_printer tp("parameters");
if (!status.empty())
tp.add_row_name_and_data("status", status);
if (!output_file.empty())
tp.add_row_name_and_data("out_file", output_file);
mtp.add(std::move(tp));
multi_printer.add(std::move(tp));
}

::dsn::replication::node_status::type s = ::dsn::replication::node_status::NS_INVALID;
Expand All @@ -477,7 +477,7 @@ bool ls_nodes(command_executor *, shell_context *sc, arguments args)
std::map<dsn::host_port, dsn::replication::node_status::type> status_by_hp;
auto r = sc->ddl_client->list_nodes(s, status_by_hp);
if (r != dsn::ERR_OK) {
std::cout << "list nodes failed, error=" << r << std::endl;
fmt::println("list nodes failed, error={}", r);
return true;
}

Expand All @@ -496,7 +496,7 @@ bool ls_nodes(command_executor *, shell_context *sc, arguments args)
std::vector<::dsn::app_info> apps;
const auto &result = sc->ddl_client->list_apps(dsn::app_status::AS_AVAILABLE, apps);
if (!result) {
std::cout << "list apps failed, error=" << result << std::endl;
fmt::println("list apps failed, error={}", result);
return true;
}

Expand All @@ -506,7 +506,7 @@ bool ls_nodes(command_executor *, shell_context *sc, arguments args)
std::vector<dsn::partition_configuration> pcs;
r = sc->ddl_client->list_app(app.app_name, app_id, partition_count, pcs);
if (r != dsn::ERR_OK) {
std::cout << "list app " << app.app_name << " failed, error=" << r << std::endl;
fmt::println("list app {} failed, error={}", app.app_name, r);
return true;
}

Expand All @@ -530,7 +530,7 @@ bool ls_nodes(command_executor *, shell_context *sc, arguments args)
if (resource_usage) {
std::vector<node_desc> nodes;
if (!fill_nodes(sc, "replica-server", nodes)) {
std::cout << "get replica server node list failed" << std::endl;
fmt::println("get replica server node list failed");
return true;
}

Expand All @@ -553,7 +553,7 @@ bool ls_nodes(command_executor *, shell_context *sc, arguments args)
if (show_qps) {
std::vector<node_desc> nodes;
if (!fill_nodes(sc, "replica-server", nodes)) {
std::cout << "get replica server node list failed" << std::endl;
fmt::println("get replica server node list failed");
return true;
}

Expand Down Expand Up @@ -595,7 +595,7 @@ bool ls_nodes(command_executor *, shell_context *sc, arguments args)
if (show_latency) {
std::vector<node_desc> nodes;
if (!fill_nodes(sc, "replica-server", nodes)) {
std::cout << "get replica server node list failed" << std::endl;
fmt::println("get replica server node list failed");
return true;
}

Expand All @@ -616,19 +616,6 @@ bool ls_nodes(command_executor *, shell_context *sc, arguments args)
}
}

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

// TODO(wangdan): use dsn::utils::output() in output_utils.h instead.
if (!output_file.empty()) {
of.open(output_file);
buf = of.rdbuf();
} else {
buf = std::cout.rdbuf();
}
std::ostream out(buf);

dsn::utils::table_printer tp("details");
tp.add_title("address");
tp.add_column("status");
Expand Down Expand Up @@ -694,16 +681,15 @@ bool ls_nodes(command_executor *, shell_context *sc, arguments args)
tp.append_data(kv.second.multi_put_p99 / 1e6);
}
}
mtp.add(std::move(tp));
multi_printer.add(std::move(tp));

dsn::utils::table_printer tp_count("summary");
tp_count.add_row_name_and_data("total_node_count", status_by_hp.size());
tp_count.add_row_name_and_data("alive_node_count", alive_node_count);
tp_count.add_row_name_and_data("unalive_node_count", status_by_hp.size() - alive_node_count);
mtp.add(std::move(tp_count));
multi_printer.add(std::move(tp_count));

// TODO(wangdan): use dsn::utils::output() in output_utils.h instead.
mtp.output(out, json ? tp_output_format::kJsonPretty : tp_output_format::kTabular);
dsn::utils::output(output_file, json, multi_printer);

return true;
}
Expand Down
2 changes: 1 addition & 1 deletion src/shell/commands/table_management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ bool ls_apps(command_executor *e, shell_context *sc, arguments args)
const auto &result = sc->ddl_client->list_apps(
show_all, detailed, json, output_file, status, app_name_pattern, match_type);
if (!result) {
std::cout << "list apps failed, error=" << result << std::endl;
fmt::println("list apps failed, error={}", result);
}

return true;
Expand Down

0 comments on commit 98b60d4

Please sign in to comment.