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

refactor: Refactor the method 'check_leader' make it return a enum class type #1406

Merged
merged 1 commit into from
Mar 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
17 changes: 9 additions & 8 deletions src/meta/meta_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -540,27 +540,28 @@ void meta_service::register_rpc_handlers()
&meta_service::on_set_max_replica_count);
}

int meta_service::check_leader(dsn::message_ex *req, dsn::rpc_address *forward_address)
meta_leader_state meta_service::check_leader(dsn::message_ex *req,
dsn::rpc_address *forward_address)
{
dsn::rpc_address leader;
if (!_failure_detector->get_leader(&leader)) {
if (!req->header->context.u.is_forward_supported) {
if (forward_address != nullptr)
*forward_address = leader;
return -1;
return meta_leader_state::kNotLeaderAndCannotForwardRpc;
}

LOG_DEBUG("leader address: {}", leader);
if (!leader.is_invalid()) {
dsn_rpc_forward(req, leader);
return 0;
return meta_leader_state::kNotLeaderAndCanForwardRpc;
} else {
if (forward_address != nullptr)
forward_address->set_invalid();
return -1;
return meta_leader_state::kNotLeaderAndCannotForwardRpc;
}
}
return 1;
return meta_leader_state::kIsLeader;
}

// table operations
Expand Down Expand Up @@ -813,13 +814,13 @@ void meta_service::on_start_recovery(configuration_recovery_rpc rpc)
{
configuration_recovery_response &response = rpc.response();
LOG_INFO("got start recovery request, start to do recovery");
int result = check_leader(rpc, nullptr);
auto result = check_leader(rpc, nullptr);
// request has been forwarded to others
if (result == 0) {
if (result == meta_leader_state::kNotLeaderAndCanForwardRpc) {
return;
}

if (result == -1) {
if (result == meta_leader_state::kNotLeaderAndCannotForwardRpc) {
response.err = ERR_FORWARD_TO_OTHERS;
} else {
zauto_write_lock l(_meta_lock);
Expand Down
46 changes: 25 additions & 21 deletions src/meta/meta_service.h
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,14 @@ ENUM_REG(meta_op_status::RESTORE)
ENUM_REG(meta_op_status::MANUAL_COMPACT)
ENUM_END(meta_op_status)

// The leader status of meta server
enum class meta_leader_state : int
{
kIsLeader, // the meta is leader
kNotLeaderAndCanForwardRpc, // meta isn't leader, and rpc-msg can forward to others
kNotLeaderAndCannotForwardRpc, // meta isn't leader, and rpc-msg can't forward to others
};

class meta_service : public serverlet<meta_service>
{
public:
Expand Down Expand Up @@ -278,15 +286,11 @@ class meta_service : public serverlet<meta_service>
void on_get_max_replica_count(configuration_get_max_replica_count_rpc rpc);
void on_set_max_replica_count(configuration_set_max_replica_count_rpc rpc);

// common routines
// ret:
// 1. the meta is leader
// 0. meta isn't leader, and rpc-msg can forward to others
// -1. meta isn't leader, and rpc-msg can't forward to others
// if return -1 and `forward_address' != nullptr, then return leader by `forward_address'.
int check_leader(dsn::message_ex *req, dsn::rpc_address *forward_address);
// if return 'kNotLeaderAndCannotForwardRpc' and 'forward_address' != nullptr, then return
// leader by 'forward_address'.
meta_leader_state check_leader(dsn::message_ex *req, dsn::rpc_address *forward_address);
template <typename TRpcHolder>
int check_leader(TRpcHolder rpc, /*out*/ rpc_address *forward_address);
meta_leader_state check_leader(TRpcHolder rpc, /*out*/ rpc_address *forward_address);
// ret:
// false: check failed
// true: check succeed
Expand Down Expand Up @@ -368,27 +372,27 @@ class meta_service : public serverlet<meta_service>
};

template <typename TRpcHolder>
int meta_service::check_leader(TRpcHolder rpc, rpc_address *forward_address)
meta_leader_state meta_service::check_leader(TRpcHolder rpc, rpc_address *forward_address)
{
dsn::rpc_address leader;
if (!_failure_detector->get_leader(&leader)) {
if (!rpc.dsn_request()->header->context.u.is_forward_supported) {
if (forward_address != nullptr)
*forward_address = leader;
return -1;
return meta_leader_state::kNotLeaderAndCannotForwardRpc;
}

LOG_DEBUG("leader address: {}", leader);
if (!leader.is_invalid()) {
rpc.forward(leader);
return 0;
return meta_leader_state::kNotLeaderAndCanForwardRpc;
} else {
if (forward_address != nullptr)
forward_address->set_invalid();
return -1;
return meta_leader_state::kNotLeaderAndCannotForwardRpc;
}
}
return 1;
return meta_leader_state::kIsLeader;
}

template <typename TRpcHolder>
Expand All @@ -400,11 +404,11 @@ bool meta_service::check_status(TRpcHolder rpc, rpc_address *forward_address)
return false;
}

int result = check_leader(rpc, forward_address);
if (result == 0)
auto result = check_leader(rpc, forward_address);
if (result == meta_leader_state::kNotLeaderAndCanForwardRpc)
return false;
if (result == -1 || !_started) {
if (result == -1) {
if (result == meta_leader_state::kNotLeaderAndCannotForwardRpc || !_started) {
if (result == meta_leader_state::kNotLeaderAndCannotForwardRpc) {
rpc.response().err = ERR_FORWARD_TO_OTHERS;
} else if (_recovering) {
rpc.response().err = ERR_UNDER_RECOVERY;
Expand All @@ -428,12 +432,12 @@ bool meta_service::check_status_with_msg(message_ex *req, TRespType &response_st
return false;
}

int result = check_leader(req, nullptr);
if (result == 0) {
auto result = check_leader(req, nullptr);
if (result == meta_leader_state::kNotLeaderAndCanForwardRpc) {
return false;
}
if (result == -1 || !_started) {
if (result == -1) {
if (result == meta_leader_state::kNotLeaderAndCannotForwardRpc || !_started) {
if (result == meta_leader_state::kNotLeaderAndCannotForwardRpc) {
response_struct.err = ERR_FORWARD_TO_OTHERS;
} else if (_recovering) {
response_struct.err = ERR_UNDER_RECOVERY;
Expand Down