Skip to content

Commit

Permalink
refactor: make command manager decoupled from dsn_runtime (#522)
Browse files Browse the repository at this point in the history
  • Loading branch information
zhao liwei authored Jul 2, 2020
1 parent a53199d commit 43c5646
Show file tree
Hide file tree
Showing 13 changed files with 246 additions and 251 deletions.
4 changes: 0 additions & 4 deletions include/dsn/tool-api/command_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ class command_manager : public ::dsn::utils::singleton<command_manager>
const std::string &help_long,
command_handler handler);

dsn_handle_t register_app_command(const std::vector<std::string> &commands,
const std::string &help_one_line,
const std::string &help_long,
command_handler handler);
void deregister_command(dsn_handle_t handle);

bool run_command(const std::string &cmd,
Expand Down
2 changes: 0 additions & 2 deletions include/dsn/tool-api/global_config.h
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,6 @@ struct service_spec
std::string lock_nr_factory_name;
std::string rwlock_nr_factory_name;
std::string semaphore_factory_name;
std::string nfs_factory_name;
std::string logging_factory_name;

network_client_configs network_default_client_cfs; // default network configed by tools
Expand Down Expand Up @@ -193,7 +192,6 @@ CONFIG_FLD_STRING(lock_factory_name, "", "recursive exclusive lock provider")
CONFIG_FLD_STRING(lock_nr_factory_name, "", "non-recurisve exclusive lock provider")
CONFIG_FLD_STRING(rwlock_nr_factory_name, "", "non-recurisve rwlock provider")
CONFIG_FLD_STRING(semaphore_factory_name, "", "semaphore provider")
CONFIG_FLD_STRING(nfs_factory_name, "", "nfs provider")
CONFIG_FLD_STRING(logging_factory_name, "", "logging provider")
CONFIG_END

Expand Down
16 changes: 0 additions & 16 deletions src/core/core/command_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,26 +29,10 @@
#include <sstream>

#include <dsn/utility/utils.h>
#include <dsn/cpp/service_app.h>
#include <dsn/tool-api/command_manager.h>

namespace dsn {

dsn_handle_t command_manager::register_app_command(const std::vector<std::string> &commands,
const std::string &help_one_line,
const std::string &help_long,
command_handler handler)
{
std::string app_tag = std::string(service_app::current_service_app_info().full_name) + ".";
std::vector<std::string> commands_with_app_tag;
commands_with_app_tag.reserve(commands.size());
for (const std::string &c : commands) {
commands_with_app_tag.push_back(app_tag + c);
}
return register_command(
commands_with_app_tag, app_tag + help_one_line, app_tag + help_long, handler);
}

dsn_handle_t command_manager::register_command(const std::vector<std::string> &commands,
const std::string &help_one_line,
const std::string &help_long,
Expand Down
3 changes: 0 additions & 3 deletions src/core/tools/common/nativerun.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,9 +93,6 @@ void nativerun::install(service_spec &spec)
if (spec.semaphore_factory_name == "")
spec.semaphore_factory_name = ("dsn::tools::std_semaphore_provider");

if (spec.nfs_factory_name == "")
spec.nfs_factory_name = "dsn::service::nfs_node_simple";

for (auto it = spec.threadpool_specs.begin(); it != spec.threadpool_specs.end(); ++it) {
threadpool_spec &tspec = *it;

Expand Down
3 changes: 0 additions & 3 deletions src/core/tools/simulator/simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ void simulator::install(service_spec &spec)
if (spec.semaphore_factory_name == "")
spec.semaphore_factory_name = ("dsn::tools::sim_semaphore_provider");

if (spec.nfs_factory_name == "")
spec.nfs_factory_name = "dsn::service::nfs_node_simple";

for (auto it = spec.threadpool_specs.begin(); it != spec.threadpool_specs.end(); ++it) {
threadpool_spec &tspec = *it;

Expand Down
13 changes: 8 additions & 5 deletions src/dist/failure_detector/failure_detector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,14 @@ failure_detector::failure_detector()

void failure_detector::register_ctrl_commands()
{
_get_allow_list = dsn::command_manager::instance().register_app_command(
{"fd.allow_list"},
"fd.allow_list",
"show allow list of replica",
[this](const std::vector<std::string> &args) { return get_allow_list(args); });
static std::once_flag flag;
std::call_once(flag, [&]() {
_get_allow_list = dsn::command_manager::instance().register_command(
{"fd.allow_list"},
"fd.allow_list",
"show allow list of failure detector",
[this](const std::vector<std::string> &args) { return get_allow_list(args); });
});
}

void failure_detector::unregister_ctrl_commands() { UNREGISTER_VALID_HANDLER(_get_allow_list); }
Expand Down
65 changes: 34 additions & 31 deletions src/dist/nfs/nfs_client_impl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,6 @@ nfs_client_impl::nfs_client_impl()
dassert(max_copy_rate_bytes > FLAGS_nfs_copy_block_bytes,
"max_copy_rate_bytes should be greater than nfs_copy_block_bytes");
_copy_token_bucket.reset(new TokenBucket(max_copy_rate_bytes, 1.5 * max_copy_rate_bytes));
FLAGS_max_copy_rate_megabytes = FLAGS_max_copy_rate_megabytes;

register_cli_commands();
}
Expand Down Expand Up @@ -557,40 +556,44 @@ void nfs_client_impl::handle_completion(const user_request_ptr &req, error_code

void nfs_client_impl::register_cli_commands()
{
dsn::command_manager::instance().register_app_command(
{"nfs.max_copy_rate_megabytes"},
"nfs.max_copy_rate_megabytes [num | DEFAULT]",
"control the max rate(MB/s) to copy file from remote node",
[this](const std::vector<std::string> &args) {
std::string result("OK");

if (args.empty()) {
return std::to_string(FLAGS_max_copy_rate_megabytes);
}

if (args[0] == "DEFAULT") {
uint32_t max_copy_rate_bytes = FLAGS_max_copy_rate_megabytes << 20;
_copy_token_bucket->reset(max_copy_rate_bytes, 1.5 * max_copy_rate_bytes);
FLAGS_max_copy_rate_megabytes = FLAGS_max_copy_rate_megabytes;
return result;
}
static std::once_flag flag;
std::call_once(flag, [&]() {
dsn::command_manager::instance().register_command(
{"nfs.max_copy_rate_megabytes"},
"nfs.max_copy_rate_megabytes [num | DEFAULT]",
"control the max rate(MB/s) to copy file from remote node",
[this](const std::vector<std::string> &args) {
std::string result("OK");

if (args.empty()) {
return std::to_string(FLAGS_max_copy_rate_megabytes);
}

int32_t max_copy_rate_megabytes = 0;
if (!dsn::buf2int32(args[0], max_copy_rate_megabytes) || max_copy_rate_megabytes <= 0) {
return std::string("ERR: invalid arguments");
}
if (args[0] == "DEFAULT") {
uint32_t max_copy_rate_bytes = FLAGS_max_copy_rate_megabytes << 20;
_copy_token_bucket->reset(max_copy_rate_bytes, 1.5 * max_copy_rate_bytes);
return result;
}

int32_t max_copy_rate_megabytes = 0;
if (!dsn::buf2int32(args[0], max_copy_rate_megabytes) ||
max_copy_rate_megabytes <= 0) {
return std::string("ERR: invalid arguments");
}

uint32_t max_copy_rate_bytes = max_copy_rate_megabytes << 20;
if (max_copy_rate_bytes <= FLAGS_nfs_copy_block_bytes) {
result = std::string("ERR: max_copy_rate_bytes(max_copy_rate_megabytes << 20) "
"should be greater than nfs_copy_block_bytes:")
.append(std::to_string(FLAGS_nfs_copy_block_bytes));
uint32_t max_copy_rate_bytes = max_copy_rate_megabytes << 20;
if (max_copy_rate_bytes <= FLAGS_nfs_copy_block_bytes) {
result = std::string("ERR: max_copy_rate_bytes(max_copy_rate_megabytes << 20) "
"should be greater than nfs_copy_block_bytes:")
.append(std::to_string(FLAGS_nfs_copy_block_bytes));
return result;
}
_copy_token_bucket->reset(max_copy_rate_bytes, 1.5 * max_copy_rate_bytes);
FLAGS_max_copy_rate_megabytes = max_copy_rate_megabytes;
return result;
}
_copy_token_bucket->reset(max_copy_rate_bytes, 1.5 * max_copy_rate_bytes);
FLAGS_max_copy_rate_megabytes = max_copy_rate_megabytes;
return result;
});
});
});
}
} // namespace service
} // namespace dsn
Loading

0 comments on commit 43c5646

Please sign in to comment.