Skip to content

Commit 3cb5420

Browse files
committed
refactor(remote_commands): init
1 parent 7d581a4 commit 3cb5420

File tree

6 files changed

+75
-61
lines changed

6 files changed

+75
-61
lines changed

src/runtime/service_engine.cpp

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
#include "utils/filesystem.h"
4545
#include "utils/fmt_logging.h"
4646
#include "utils/join_point.h"
47+
#include "utils/string_conv.h"
4748
#include "utils/strings.h"
4849

4950
using namespace dsn::utils;
@@ -144,14 +145,11 @@ error_code service_node::start()
144145
return err;
145146
}
146147

147-
void service_node::get_runtime_info(const std::string &indent,
148-
const std::vector<std::string> &args,
149-
/*out*/ std::stringstream &ss)
148+
std::string service_node::get_runtime_info(const std::string &indent,
149+
const std::vector<std::string> &args)
150150
{
151-
ss << indent << full_name() << ":" << std::endl;
152-
153-
std::string indent2 = indent + "\t";
154-
_computation->get_runtime_info(indent2, args, ss);
151+
return fmt::format(
152+
"{}{}:\n{}", indent, full_name(), _computation->get_runtime_info(indent + "\t", args));
155153
}
156154

157155
void service_node::get_queue_info(
@@ -243,26 +241,32 @@ void service_engine::start_node(service_app_spec &app_spec)
243241

244242
std::string service_engine::get_runtime_info(const std::vector<std::string> &args)
245243
{
246-
std::stringstream ss;
247-
if (args.size() == 0) {
248-
ss << "" << service_engine::instance()._nodes_by_app_id.size()
249-
<< " nodes available:" << std::endl;
250-
for (auto &kv : service_engine::instance()._nodes_by_app_id) {
251-
ss << "\t" << kv.second->id() << "." << kv.second->full_name() << std::endl;
252-
}
253-
} else {
254-
std::string indent = "";
255-
int id = atoi(args[0].c_str());
256-
auto it = service_engine::instance()._nodes_by_app_id.find(id);
257-
if (it != service_engine::instance()._nodes_by_app_id.end()) {
258-
auto args2 = args;
259-
args2.erase(args2.begin());
260-
it->second->get_runtime_info(indent, args2, ss);
261-
} else {
262-
ss << "cannot find node with given app id";
244+
// Overview.
245+
if (args.empty()) {
246+
auto result = fmt::format("{} nodes available:\n",
247+
service_engine::instance()._nodes_by_app_id.size());
248+
for (const auto &nodes_by_app_id : service_engine::instance()._nodes_by_app_id) {
249+
result = fmt::format("{}\t{}.{}\n",
250+
result,
251+
nodes_by_app_id.second->id(),
252+
nodes_by_app_id.second->full_name());
263253
}
254+
return result;
264255
}
265-
return ss.str();
256+
257+
int id;
258+
if (!dsn::buf2int32(args[0], id)) {
259+
return {"ERR: invalid argument, only one integer argument is acceptable"};
260+
}
261+
262+
const auto &it = service_engine::instance()._nodes_by_app_id.find(id);
263+
if (it == service_engine::instance()._nodes_by_app_id.end()) {
264+
return fmt::format("ERR: cannot find node with given app id({})", id);
265+
}
266+
267+
auto tmp_args = args;
268+
tmp_args.erase(tmp_args.begin());
269+
return it->second->get_runtime_info("", tmp_args);
266270
}
267271

268272
std::string service_engine::get_queue_info(const std::vector<std::string> &args)

src/runtime/service_engine.h

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,7 @@ class service_node
6060
rpc_engine *rpc() const { return _rpc.get(); }
6161
task_engine *computation() const { return _computation.get(); }
6262

63-
void get_runtime_info(const std::string &indent,
64-
const std::vector<std::string> &args,
65-
/*out*/ std::stringstream &ss);
63+
std::string get_runtime_info(const std::string &indent, const std::vector<std::string> &args);
6664
void get_queue_info(/*out*/ std::stringstream &ss);
6765

6866
dsn::error_code start();

src/runtime/task/task_engine.cpp

Lines changed: 41 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -184,28 +184,40 @@ bool task_worker_pool::shared_same_worker_with_current_task(task *tsk) const
184184
}
185185
}
186186

187-
void task_worker_pool::get_runtime_info(const std::string &indent,
188-
const std::vector<std::string> &args,
189-
/*out*/ std::stringstream &ss)
187+
std::string task_worker_pool::get_runtime_info(const std::string &indent,
188+
const std::vector<std::string> &args)
190189
{
191-
std::string indent2 = indent + "\t";
192-
ss << indent << "contains " << _workers.size() << " threads with " << _queues.size()
193-
<< " queues" << std::endl;
194-
195-
for (auto &q : _queues) {
196-
if (q) {
197-
ss << indent2 << q->get_name() << " now has " << q->count() << " pending tasks"
198-
<< std::endl;
190+
const auto indent2 = fmt::format("{}\t", indent);
191+
192+
auto out = fmt::memory_buffer();
193+
fmt::format_to(std::back_inserter(out),
194+
"{}contains {} threads with {} queues",
195+
indent,
196+
_workers.size(),
197+
_queues.size());
198+
for (const auto &queue : _queues) {
199+
if (queue) {
200+
fmt::format_to(std::back_inserter(out),
201+
"{}{} now has {} pending tasks\n",
202+
indent2,
203+
queue->get_name(),
204+
queue->count());
199205
}
200206
}
201207

202-
for (auto &wk : _workers) {
203-
if (wk) {
204-
ss << indent2 << wk->index() << " (TID = " << wk->native_tid()
205-
<< ") attached with queue " << wk->queue()->get_name() << std::endl;
208+
for (const auto &worker : _workers) {
209+
if (worker) {
210+
fmt::format_to(std::back_inserter(out),
211+
"{}{} (TID = {}) attached with queue {}\n",
212+
indent2,
213+
worker->index(),
214+
worker->native_tid(),
215+
worker->queue()->get_name());
206216
}
207217
}
218+
return fmt::to_string(out);
208219
}
220+
209221
void task_worker_pool::get_queue_info(/*out*/ std::stringstream &ss)
210222
{
211223
ss << "[";
@@ -279,17 +291,23 @@ volatile int *task_engine::get_task_queue_virtual_length_ptr(dsn::task_code code
279291
return pl->queues()[idx]->get_virtual_length_ptr();
280292
}
281293

282-
void task_engine::get_runtime_info(const std::string &indent,
283-
const std::vector<std::string> &args,
284-
/*out*/ std::stringstream &ss)
294+
std::string task_engine::get_runtime_info(const std::string &indent,
295+
const std::vector<std::string> &args)
285296
{
286-
std::string indent2 = indent + "\t";
287-
for (auto &p : _pools) {
288-
if (p) {
289-
ss << indent << p->spec().pool_code.to_string() << std::endl;
290-
p->get_runtime_info(indent2, args, ss);
297+
const auto indent2 = fmt::format("{}\t", indent);
298+
299+
auto out = fmt::memory_buffer();
300+
fmt::format_to(std::back_inserter(out), indent2);
301+
for (const auto &pool : _pools) {
302+
if (pool) {
303+
fmt::format_to(std::back_inserter(out),
304+
"{}{}\n{}",
305+
indent,
306+
pool->spec().pool_code,
307+
pool->get_runtime_info(indent2, args));
291308
}
292309
}
310+
return fmt::to_string(out);
293311
}
294312

295313
void task_engine::get_queue_info(/*out*/ std::stringstream &ss)

src/runtime/task/task_engine.h

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -72,9 +72,7 @@ class task_worker_pool
7272
bool shared_same_worker_with_current_task(task *task) const;
7373
task_engine *engine() const { return _owner; }
7474
service_node *node() const { return _node; }
75-
void get_runtime_info(const std::string &indent,
76-
const std::vector<std::string> &args,
77-
/*out*/ std::stringstream &ss);
75+
std::string get_runtime_info(const std::string &indent, const std::vector<std::string> &args);
7876
void get_queue_info(/*out*/ std::stringstream &ss);
7977
std::vector<task_queue *> &queues() { return _queues; }
8078
std::vector<task_worker *> &workers() { return _workers; }
@@ -118,9 +116,7 @@ class task_engine
118116
volatile int *get_task_queue_virtual_length_ptr(dsn::task_code code, int hash);
119117

120118
service_node *node() const { return _node; }
121-
void get_runtime_info(const std::string &indent,
122-
const std::vector<std::string> &args,
123-
/*out*/ std::stringstream &ss);
119+
std::string get_runtime_info(const std::string &indent, const std::vector<std::string> &args);
124120
void get_queue_info(/*out*/ std::stringstream &ss);
125121

126122
private:

src/runtime/test/task_engine.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,7 @@ TEST(core, task_engine)
5858

5959
ASSERT_TRUE(engine->is_started());
6060
std::vector<std::string> args;
61-
std::stringstream oss;
62-
engine->get_runtime_info(" ", args, oss);
63-
printf("%s\n", oss.str().c_str());
61+
fmt::print(stdout, "{}\n", engine->get_runtime_info(" ", args));
6462

6563
std::vector<task_worker_pool *> &pools = engine->pools();
6664
for (size_t i = 0; i < pools.size(); ++i) {

src/shell/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ static command_executor commands[] = {
333333
"remote_command",
334334
"send remote command to servers",
335335
"[-t all|meta-server|replica-server] [-r|--resolve_ip] [-l ip:port,ip:port...]"
336-
"<command> [arguments...]",
336+
" <command> [arguments...]",
337337
remote_command,
338338
},
339339
{

0 commit comments

Comments
 (0)