Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(shell): support resolve IP address in some shell commands #426

Merged
merged 20 commits into from
Nov 19, 2019
2 changes: 1 addition & 1 deletion rdsn
17 changes: 14 additions & 3 deletions src/shell/commands/node_management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// can be found in the LICENSE file in the root directory of this source tree.

#include "shell/commands.h"
#include <dsn/utility/utils.h>

bool query_cluster_info(command_executor *e, shell_context *sc, arguments args)
{
Expand Down Expand Up @@ -132,7 +133,7 @@ bool ls_nodes(command_executor *e, shell_context *sc, arguments args)
std::string node_name = kv.first.to_std_string();
if (resolve_ip) {
// TODO: put hostname_from_ip_port into common utils
node_name = sc->ddl_client->hostname_from_ip_port(node_name.c_str());
dsn::utils::hostname_from_ip_port(node_name.c_str(), &node_name);
}
tmp_map.emplace(kv.first, list_nodes_helper(node_name, status_str));
}
Expand Down Expand Up @@ -401,15 +402,17 @@ bool remote_command(command_executor *e, shell_context *sc, arguments args)
{
static struct option long_options[] = {{"node_type", required_argument, 0, 't'},
{"node_list", required_argument, 0, 'l'},
{"resolve_ip", no_argument, 0, 'r'},
{0, 0, 0, 0}};

std::string type;
std::string nodes;
optind = 0;
bool resolve_ip = false;
while (true) {
int option_index = 0;
int c;
c = getopt_long(args.argc, args.argv, "t:l:", long_options, &option_index);
c = getopt_long(args.argc, args.argv, "t:l:r", long_options, &option_index);
if (c == -1)
break;
switch (c) {
Expand All @@ -419,6 +422,9 @@ bool remote_command(command_executor *e, shell_context *sc, arguments args)
case 'l':
nodes = optarg;
break;
case 'r':
resolve_ip = true;
break;
default:
return false;
}
Expand Down Expand Up @@ -487,7 +493,12 @@ bool remote_command(command_executor *e, shell_context *sc, arguments args)
// TODO (yingchun) output is hard to read, need do some refactor
for (int i = 0; i < node_list.size(); ++i) {
node_desc &n = node_list[i];
fprintf(stderr, "CALL [%s] [%s] ", n.desc.c_str(), n.address.to_string());
std::string hostname;
if (resolve_ip)
dsn::utils::hostname_from_ip_port(n.address.to_string(), &hostname);
Smityz marked this conversation as resolved.
Show resolved Hide resolved
else
hostname = n.address.to_string();
fprintf(stderr, "CALL [%s] [%s] ", n.desc.c_str(), hostname.c_str());
if (results[i].first) {
fprintf(stderr, "succeed: %s\n", results[i].second.c_str());
succeed++;
Expand Down
36 changes: 27 additions & 9 deletions src/shell/commands/table_management.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ bool query_app(command_executor *e, shell_context *sc, arguments args)
std::string app_name = args.argv[1];
std::string out_file;
bool detailed = false;
bool resolve_ip = false;
bool json = false;
bool resolve_ip = false;
Smityz marked this conversation as resolved.
Show resolved Hide resolved

optind = 0;
while (true) {
Expand All @@ -86,12 +86,12 @@ bool query_app(command_executor *e, shell_context *sc, arguments args)
if (c == -1)
break;
switch (c) {
case 'd':
detailed = true;
break;
case 'r':
resolve_ip = true;
break;
case 'd':
Smityz marked this conversation as resolved.
Show resolved Hide resolved
detailed = true;
break;
case 'j':
json = true;
break;
Expand Down Expand Up @@ -121,7 +121,8 @@ bool app_disk(command_executor *e, shell_context *sc, arguments args)
if (args.argc <= 1)
return false;

static struct option long_options[] = {{"detailed", no_argument, 0, 'd'},
static struct option long_options[] = {{"resolve_ip", no_argument, 0, 'r'},
{"detailed", no_argument, 0, 'd'},
{"json", no_argument, 0, 'j'},
{"output", required_argument, 0, 'o'},
{0, 0, 0, 0}};
Expand All @@ -130,18 +131,22 @@ bool app_disk(command_executor *e, shell_context *sc, arguments args)
std::string out_file;
bool detailed = false;
bool json = false;
bool resolve_ip = false;

optind = 0;
while (true) {
int option_index = 0;
int c;
c = getopt_long(args.argc, args.argv, "djo:", long_options, &option_index);
c = getopt_long(args.argc, args.argv, "drjo:", long_options, &option_index);
if (c == -1)
break;
switch (c) {
case 'd':
detailed = true;
break;
case 'r':
resolve_ip = true;
break;
case 'j':
json = true;
break;
Expand All @@ -152,7 +157,6 @@ bool app_disk(command_executor *e, shell_context *sc, arguments args)
return false;
}
}

if (app_name.empty()) {
std::cout << "ERROR: null app name" << std::endl;
return false;
Expand Down Expand Up @@ -184,6 +188,7 @@ bool app_disk(command_executor *e, shell_context *sc, arguments args)
int32_t partition_count = 0;
int32_t max_replica_count = 0;
std::vector<dsn::partition_configuration> partitions;

dsn::error_code err = sc->ddl_client->list_app(app_name, app_id, partition_count, partitions);
if (err != ::dsn::ERR_OK) {
std::cout << "ERROR: list app " << app_name << " failed, error=" << err.to_string()
Expand Down Expand Up @@ -304,7 +309,13 @@ bool app_disk(command_executor *e, shell_context *sc, arguments args)
}
}
std::stringstream oss;
oss << p.primary.to_string() << "(";
std::string hostname;
std::string ip = p.primary.to_string();
if (resolve_ip && dsn::utils::hostname_from_ip_port(ip.c_str(), &hostname)) {
oss << hostname << "(";
} else {
oss << p.primary.to_string() << "(";
};
if (disk_found)
oss << disk_value;
else
Expand Down Expand Up @@ -348,7 +359,14 @@ bool app_disk(command_executor *e, shell_context *sc, arguments args)
count_value = f3->second;
}
}
oss << p.secondaries[j].to_string() << "(";

std::string hostname;
std::string ip = p.secondaries[j].to_string();
if (resolve_ip && dsn::utils::hostname_from_ip_port(ip.c_str(), &hostname)) {
oss << hostname << "(";
} else {
oss << p.secondaries[j].to_string() << "(";
};
if (found)
oss << value;
else
Expand Down
10 changes: 5 additions & 5 deletions src/shell/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static command_executor commands[] = {
{
"app_disk",
"get the disk usage information for some specific app",
"<app_name> [-d|--detailed] [-j|--json] [-o|--output file_name]",
"<app_name> [-d|--detailed] [-r|--resolve_ip] [-j|--json] [-o|--output file_name]",
app_disk,
},
{
Expand Down Expand Up @@ -293,20 +293,20 @@ static command_executor commands[] = {
{
"remote_command",
"send remote command to servers",
"[-t all|meta-server|replica-server] [-l ip:port,ip:port...] "
Smityz marked this conversation as resolved.
Show resolved Hide resolved
"[-r|--resolve_ip] [-l ip:port,ip:port...] "
"<command> [arguments...]",
remote_command,
},
{
"server_info",
"get info of servers",
"[-t all|meta-server|replica-server] [-l ip:port,ip:port...]",
"[-t all|meta-server|replica-server] [-l ip:port,ip:port...] [-r|--resolve_ip]",
server_info,
},
{
"server_stat",
"get stat of servers",
"[-t all|meta-server|replica-server] [-l ip:port,ip:port...]",
"[-t all|meta-server|replica-server] [-l ip:port,ip:port...] [-r|--resolve_ip]",
server_stat,
},
{
Expand All @@ -319,7 +319,7 @@ static command_executor commands[] = {
{
"flush_log",
"flush log of servers",
"[-t all|meta-server|replica-server] [-l ip:port,ip:port...]",
"[-t all|meta-server|replica-server] [-l ip:port,ip:port...][-r|--resolve_ip]",
flush_log,
},
{
Expand Down