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: add shell command for query_disk_info #498

Merged
merged 51 commits into from
Mar 24, 2020
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
b29efe8
init
foreverneverer Mar 3, 2020
ae600cd
update
foreverneverer Mar 4, 2020
11e1692
update
foreverneverer Mar 4, 2020
0b8376c
update
foreverneverer Mar 5, 2020
08581c9
update
foreverneverer Mar 9, 2020
3dc8ad9
add test and counter
foreverneverer Mar 12, 2020
3066bd2
fix n/0
foreverneverer Mar 13, 2020
8fca26c
update return
foreverneverer Mar 13, 2020
802e59c
update main
foreverneverer Mar 13, 2020
63740bc
format
foreverneverer Mar 13, 2020
804e065
format
foreverneverer Mar 13, 2020
45dfb29
format
foreverneverer Mar 13, 2020
a954192
format
foreverneverer Mar 13, 2020
d82c99b
Merge branch 'master' into disk_command
Mar 13, 2020
97ba273
fix contain
foreverneverer Mar 16, 2020
b7ea1fa
Merge branch 'disk_command' of github.com:Shuo-Jia/pegasus into disk_…
foreverneverer Mar 16, 2020
47b2151
refactor app_name
foreverneverer Mar 18, 2020
68ffb0f
refactor app_name
foreverneverer Mar 18, 2020
06c2335
refactor app_name
foreverneverer Mar 18, 2020
1c9a98a
refactor app_name
foreverneverer Mar 18, 2020
579eae0
refactor app_name
foreverneverer Mar 18, 2020
484c9e0
refactor app_name
foreverneverer Mar 18, 2020
bbc9514
refactor app_name
foreverneverer Mar 18, 2020
0b9900d
refactor app_name
foreverneverer Mar 18, 2020
2de3902
refactor app_name
foreverneverer Mar 18, 2020
bed06b5
only test
foreverneverer Mar 18, 2020
fc86f83
delete test code
foreverneverer Mar 18, 2020
301bb7c
add json
foreverneverer Mar 18, 2020
8e15f47
add json
foreverneverer Mar 18, 2020
f91175f
add json
foreverneverer Mar 18, 2020
9776f35
add json
foreverneverer Mar 18, 2020
8c6f4f1
add json
foreverneverer Mar 18, 2020
9ac6340
update parse
foreverneverer Mar 19, 2020
fb3164f
modify
foreverneverer Mar 19, 2020
031c6fe
modify
foreverneverer Mar 20, 2020
ad1fb1c
modify
foreverneverer Mar 20, 2020
00ed676
modify
foreverneverer Mar 20, 2020
f73fb92
modify
foreverneverer Mar 23, 2020
cae32cc
add rdsn
foreverneverer Mar 23, 2020
27dfee2
Merge branch 'master' into disk_command
Mar 23, 2020
b1719ac
Merge branch 'master' into disk_command
Mar 23, 2020
b44e631
modify
foreverneverer Mar 23, 2020
c7ecfcc
Merge branch 'disk_command' of github.com:Shuo-Jia/pegasus into disk_…
foreverneverer Mar 23, 2020
24c14a7
modify
foreverneverer Mar 23, 2020
9409437
modify
foreverneverer Mar 23, 2020
a1bf254
modify
foreverneverer Mar 24, 2020
96272cb
modify
foreverneverer Mar 24, 2020
ef247ff
modify
foreverneverer Mar 24, 2020
297af9d
modify
foreverneverer Mar 24, 2020
49a356e
modify
foreverneverer Mar 24, 2020
dc7951b
modify
foreverneverer Mar 24, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/shell/commands.h
Original file line number Diff line number Diff line change
Expand Up @@ -239,3 +239,7 @@ bool remove_dup(command_executor *e, shell_context *sc, arguments args);
bool start_dup(command_executor *e, shell_context *sc, arguments args);

bool pause_dup(command_executor *e, shell_context *sc, arguments args);

// == disk rebalance (see 'commands/disk_rebalance.cpp') == //

bool query_disk_info(command_executor *e, shell_context *sc, arguments args);
169 changes: 169 additions & 0 deletions src/shell/commands/disk_rebalance.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
// Copyright (c) 2017, Xiaomi, Inc. All rights reserved.
// This source code is licensed under the Apache License Version 2.0, which
// can be found in the LICENSE file in the root directory of this source tree.

#include "shell/commands.h"
#include "shell/argh.h"

#include <math.h>
#include <fmt/ostream.h>
#include <dsn/utility/errors.h>
#include <dsn/utility/output_utils.h>
#include <dsn/utility/string_conv.h>
#include <dsn/dist/replication/duplication_common.h>

bool query_disk_info(command_executor *e, shell_context *sc, arguments args)
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
{
// disk_info [-n|--node node_address] [-a|--app app_replica_count]

argh::parser cmd(args.argc, args.argv);
if (cmd.pos_args().size() > 3) {
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
fmt::print(stderr, "too many params\n");
return false;
}

bool query_one_node = cmd[{"-n", "--node"}];
bool query_app_replica_count = cmd[{"-a", "--app_replica"}];

std::map<dsn::rpc_address, dsn::replication::node_status::type> nodes;
auto error = sc->ddl_client->list_nodes(::dsn::replication::node_status::NS_INVALID, nodes);
if (error != dsn::ERR_OK) {
std::cout << "list nodes failed, error=" << error.to_string() << std::endl;
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
return false;
}

std::vector<dsn::rpc_address> targets;
if (query_one_node) {
if (!cmd(1)) {
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
fmt::print(stderr, "missing param <node_address>\n");
return false;
}
std::string node_address = cmd(1).str();

for (auto &node : nodes) {
if (node.first.to_std_string() == node_address) {
targets.emplace_back(node.first);
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
}
}

if (targets.empty()) {
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
fmt::print(stderr, "please input valid target node_address!\n");
return false;
}
} else {
if (query_app_replica_count) {
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
fmt::print(stderr, "please input node_address when query app replica count!\n");
return false;
}
for (const auto &node : nodes) {
targets.emplace_back(node.first);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same?

foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
}
}

int app_id = 0;
if (query_app_replica_count) {
if (cmd(2)) {
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
app_id = stoi(cmd(2).str());
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
}
}

const auto &err_resps = sc->ddl_client->query_disk_info(targets, app_id);
acelyc111 marked this conversation as resolved.
Show resolved Hide resolved

dsn::utils::table_printer node_printer;
node_printer.add_title("node");
node_printer.add_column("total_capacity(MB)");
node_printer.add_column("avalable_capacity(MB)");
node_printer.add_column("avalable_ratio(%)");
node_printer.add_column("capacity_balance");

int total_capacity_ratio = 0;
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
for (const auto &err_resp : err_resps) {
dsn::error_s err = err_resp.second.get_error();
if (err.is_ok()) {
err = dsn::error_s::make(err_resp.second.get_value().err);
}
if (!err.is_ok()) {
fmt::print(stderr,
"disk of node[{}] info skiped because request failed, error={}\n",
err_resp.first.to_std_string(),
err.description());
if (query_one_node) {
return false;
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
const auto &resp = err_resp.second.get_value();
total_capacity_ratio =
resp.total_capacity_mb == 0
? 0
: std::round((double)resp.total_available_mb * 100 / resp.total_capacity_mb);
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved

int temp = 0;
for (const auto &disk_info : resp.disk_infos) {
int disk_available_ratio = disk_info.disk_capacity_mb == 0
? 0
: std::round((double)disk_info.disk_available_mb *
100 / disk_info.disk_capacity_mb);
temp += pow((disk_available_ratio - total_capacity_ratio), 2);
}

int capacity_balance = sqrt(temp);

node_printer.add_row(err_resp.first.to_std_string());
node_printer.append_data(resp.total_capacity_mb);
node_printer.append_data(resp.total_available_mb);
node_printer.append_data(total_capacity_ratio);
node_printer.append_data(capacity_balance);
}
}
node_printer.output(std::cout);
std::cout << std::endl;

if (query_one_node) {
const auto &err_resp = err_resps.begin();
dsn::utils::table_printer disk_printer;
disk_printer.add_title("disk");
const auto &resp = err_resp->second.get_value();
if (query_app_replica_count) {
disk_printer.add_column("primary_count");
disk_printer.add_column("secondary_count");
disk_printer.add_column("replica_count");

for (const auto &disk_info : resp.disk_infos) {
int primary_count = 0;
int secondary_count = 0;
for (const auto &replica_count : disk_info.holding_primary_replica_counts) {
primary_count += replica_count.second;
}

for (const auto &replica_count : disk_info.holding_secondary_replica_counts) {
secondary_count += replica_count.second;
}
disk_printer.add_row(disk_info.tag);
disk_printer.append_data(primary_count);
disk_printer.append_data(secondary_count);
disk_printer.append_data(primary_count + secondary_count);
}

foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
} else {
disk_printer.add_column("total_capacity(MB)");
disk_printer.add_column("avalable_capacity(MB)");
disk_printer.add_column("avalable_ratio(%)");
disk_printer.add_column("disk_density");

for (const auto &disk_info : resp.disk_infos) {
int disk_available_ratio = disk_info.disk_capacity_mb == 0
? 0
: std::round((double)disk_info.disk_available_mb *
100 / disk_info.disk_capacity_mb);
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
int disk_density = disk_available_ratio - total_capacity_ratio;
disk_printer.add_row(disk_info.tag);
disk_printer.append_data(disk_info.disk_capacity_mb);
disk_printer.append_data(disk_info.disk_available_mb);
disk_printer.append_data(disk_available_ratio);
disk_printer.append_data(disk_density);
}
}
disk_printer.output(std::cout);
}
return true;
}
4 changes: 4 additions & 0 deletions src/shell/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -437,6 +437,10 @@ static command_executor commands[] = {
{"remove_dup", "remove duplication", "<app_name> <dup_id>", remove_dup},
{"start_dup", "start duplication", "<app_name> <dup_id>", start_dup},
{"pause_dup", "pause duplication", "<app_name> <dup_id>", pause_dup},
{"disk_info",
"query node disk info",
"[-n|--node node_address] [-a|--app app_replica_count]",
foreverneverer marked this conversation as resolved.
Show resolved Hide resolved
query_disk_info},
{
"exit", "exit shell", "", exit_shell,
},
Expand Down