Skip to content

Commit

Permalink
refactor and add comments
Browse files Browse the repository at this point in the history
  • Loading branch information
empiredan committed Jan 2, 2025
1 parent 0d40cb3 commit 48121a9
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 15 deletions.
26 changes: 13 additions & 13 deletions src/client/replication_ddl_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -215,11 +215,11 @@ dsn::error_code replication_ddl_client::create_app(const std::string &app_name,
req->options.is_stateful = !is_stateless;

dsn::replication::configuration_create_app_response resp;
const auto &req_status = request_meta_and_wait_response(RPC_CM_CREATE_APP, req, resp);
const auto &req_result = request_meta_and_wait_response(RPC_CM_CREATE_APP, req, resp);

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

if (resp.err != dsn::ERR_OK) {
Expand Down Expand Up @@ -248,10 +248,10 @@ dsn::error_code replication_ddl_client::drop_app(const std::string &app_name, in
req->options.__set_reserve_seconds(reserve_seconds);

dsn::replication::configuration_drop_app_response resp;
const auto &req_status = request_meta_and_wait_response(RPC_CM_DROP_APP, req, resp);
const auto &req_result = request_meta_and_wait_response(RPC_CM_DROP_APP, req, resp);

if (!req_status) {
return req_status.code();
if (!req_result) {
return req_result.code();
}

if (resp.err != dsn::ERR_OK) {
Expand All @@ -270,10 +270,10 @@ dsn::error_code replication_ddl_client::recall_app(int32_t app_id, const std::st
req->new_app_name = new_app_name;

dsn::replication::configuration_recall_app_response resp;
const auto &req_status = request_meta_and_wait_response(RPC_CM_RECALL_APP, req, resp);
const auto &req_result = request_meta_and_wait_response(RPC_CM_RECALL_APP, req, resp);

if (!req_status) {
return req_status.code();
if (!req_result) {
return req_result.code();
}

if (resp.err != dsn::ERR_OK) {
Expand All @@ -298,9 +298,9 @@ error_s replication_ddl_client::list_apps(dsn::app_status::type status,
req->__set_match_type(match_type);

dsn::replication::configuration_list_apps_response resp;
const auto &req_status = request_meta_and_wait_response(RPC_CM_LIST_APPS, req, resp);
if (!req_status) {
return req_status;
const auto &req_result = request_meta_and_wait_response(RPC_CM_LIST_APPS, req, resp);
if (!req_result) {
return req_result;
}

if (resp.err != dsn::ERR_OK) {
Expand Down
15 changes: 15 additions & 0 deletions src/client/replication_ddl_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -324,6 +324,11 @@ class replication_ddl_client
dsn::message_ex *request,
dsn::message_ex *resp);

// Send RPC request `req` with `code` to meta server:
// * `timeout_milliseconds`: timeout for the request;
// * `reply_thread_hash`: thread hash for the RPC response task.
//
// Return the RPC response task.
template <typename TRequest>
rpc_response_task_ptr request_meta(const dsn::task_code &code,
std::shared_ptr<TRequest> &req,
Expand All @@ -349,6 +354,7 @@ class replication_ddl_client
return task;
}

// The same as the above, except that the thread hash for the RPC response task is set to 0.
template <typename TRequest>
rpc_response_task_ptr request_meta(const dsn::task_code &code,
std::shared_ptr<TRequest> &req,
Expand All @@ -357,6 +363,7 @@ class replication_ddl_client
return request_meta(code, req, timeout_milliseconds, 0);
}

// The same as the above, except that the timeout for the RPC request is set to 0.
template <typename TRequest>
rpc_response_task_ptr request_meta(const dsn::task_code &code, std::shared_ptr<TRequest> &req)
{
Expand All @@ -368,6 +375,12 @@ class replication_ddl_client
return err == dsn::ERR_BUSY_CREATING || err == dsn::ERR_BUSY_DROPPING;
}

// The same as `request_meta()`, except that it would retry multiple times as configured
// once failed or busy, and return error status for the response task. If succeed, `resp`
// would be set as the response.
//
// NOTE: the returned error is just for the RPC request; please also check the possible
// error status in `resp` if the RPC request succeeds.
template <typename TRequest, typename TResponse>
error_s request_meta_and_wait_response(const dsn::task_code &code,
std::shared_ptr<TRequest> &req,
Expand Down Expand Up @@ -428,6 +441,7 @@ class replication_ddl_client
return error_s::ok();
}

// The same as the above, except that the thread hash for the RPC response task is set to 0.
template <typename TRequest, typename TResponse>
error_s request_meta_and_wait_response(const dsn::task_code &code,
std::shared_ptr<TRequest> &req,
Expand All @@ -437,6 +451,7 @@ class replication_ddl_client
return request_meta_and_wait_response(code, req, resp, timeout_milliseconds, 0);
}

// The same as the above, except that the timeout for the RPC request is set to 0.
template <typename TRequest, typename TResponse>
error_s request_meta_and_wait_response(const dsn::task_code &code,
std::shared_ptr<TRequest> &req,
Expand Down
4 changes: 2 additions & 2 deletions src/client/test/ddl_client_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ TEST(DDLClientTest, RetryMetaRequest)
resp.err = ERR_UNKNOWN;

auto start_ms = dsn_now_ms();
const auto &req_status =
const auto &req_result =
ddl_client->request_meta_and_wait_response(RPC_CM_CREATE_APP, req, resp);
uint64_t duration_ms = dsn_now_ms() - start_ms;

Expand All @@ -156,7 +156,7 @@ TEST(DDLClientTest, RetryMetaRequest)
EXPECT_LE(test.expected_sleep_ms, duration_ms);

// Check if final send error is matched.
EXPECT_EQ(test.final_send_error, req_status.code());
EXPECT_EQ(test.final_send_error, req_result.code());

// Check if final response error is matched.
EXPECT_EQ(test.final_resp_error, resp.err);
Expand Down

0 comments on commit 48121a9

Please sign in to comment.