Skip to content

Commit

Permalink
feat: catch error msgs in ns/tablet client (#3587)
Browse files Browse the repository at this point in the history
  • Loading branch information
vagetablechicken authored Nov 16, 2023
1 parent a966e66 commit 84a1ffc
Show file tree
Hide file tree
Showing 14 changed files with 224 additions and 234 deletions.
3 changes: 2 additions & 1 deletion docs/zh/maintain/openmldb_ops.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,9 @@ python tools/openmldb_ops.py --openmldb_bin_path=./bin/openmldb --zk_cluster=0.0
python tools/openmldb_ops.py --openmldb_bin_path=./bin/openmldb --zk_cluster=0.0.0.0:2181 --zk_root_path=/openmldb --cmd=recoverdata
```

注:理论上openmldb_ops不要求版本匹配,高版本openmldb_ops可以操作低版本的openmldb集群
运行结果可以只关注是否存在ERROR级日志,如果存在,请保留完整的日志记录,便于技术人员查找问题

### 系统要求
- 要求python2.7及以上版本
- 理论上openmldb_ops不要求与OpenMLDB集群的版本匹配,高版本openmldb_ops可以操作低版本的OpenMLDB集群。
- `showopstatus``showtablestatus`需要`prettytable`依赖
3 changes: 3 additions & 0 deletions src/base/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

#include <string>

#include "absl/strings/str_cat.h"

#include "base/slice.h"
#include "version.h" // NOLINT

Expand Down Expand Up @@ -189,6 +191,7 @@ struct Status {
inline bool OK() const { return code == ReturnCode::kOk; }
inline const std::string& GetMsg() const { return msg; }
inline int GetCode() const { return code; }
inline std::string ToString() const { return absl::StrCat("ReturnCode[", code, "]", msg); }
int code;
std::string msg;
};
Expand Down
97 changes: 45 additions & 52 deletions src/client/ns_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -221,17 +221,16 @@ base::Status NsClient::ShowOPStatus(const std::string& name, uint32_t pid,
return {base::ReturnCode::kError, response->msg()};
}

bool NsClient::CancelOP(uint64_t op_id, std::string& msg) {
base::Status NsClient::CancelOP(uint64_t op_id) {
::openmldb::nameserver::CancelOPRequest request;
::openmldb::nameserver::GeneralResponse response;
request.set_op_id(op_id);
bool ok = client_.SendRequest(&::openmldb::nameserver::NameServer_Stub::CancelOP, &request, &response,
FLAGS_request_timeout_ms, 1);
msg = response.msg();
if (ok && response.code() == 0) {
return true;
auto st = client_.SendRequestSt(&::openmldb::nameserver::NameServer_Stub::CancelOP, &request, &response,
FLAGS_request_timeout_ms, 1);
if (st.OK()) {
return {response.code(), response.msg()};
}
return false;
return st;
}

bool NsClient::AddTableField(const std::string& table_name, const ::openmldb::common::ColumnDesc& column_desc,
Expand Down Expand Up @@ -342,10 +341,10 @@ bool NsClient::SetSdkEndpoint(const std::string& server_name, const std::string&
return false;
}

bool NsClient::AddReplica(const std::string& name, const std::set<uint32_t>& pid_set, const std::string& endpoint,
std::string& msg) {
base::Status NsClient::AddReplica(const std::string& name, const std::set<uint32_t>& pid_set,
const std::string& endpoint) {
if (pid_set.empty()) {
return false;
return {base::ReturnCode::kError, "arg pid set is empty"};
}
::openmldb::nameserver::AddReplicaNSRequest request;
::openmldb::nameserver::GeneralResponse response;
Expand All @@ -358,13 +357,12 @@ bool NsClient::AddReplica(const std::string& name, const std::set<uint32_t>& pid
request.add_pid_group(pid);
}
}
bool ok = client_.SendRequest(&::openmldb::nameserver::NameServer_Stub::AddReplicaNS, &request, &response,
FLAGS_request_timeout_ms, 1);
msg = response.msg();
if (ok && response.code() == 0) {
return true;
auto st = client_.SendRequestSt(&::openmldb::nameserver::NameServer_Stub::AddReplicaNS, &request, &response,
FLAGS_request_timeout_ms, 1);
if (st.OK()) {
return {response.code(), response.msg()};
}
return false;
return st;
}

bool NsClient::AddReplicaNS(const std::string& name, const std::vector<std::string>& endpoint_vec, uint32_t pid,
Expand Down Expand Up @@ -393,10 +391,10 @@ bool NsClient::AddReplicaNS(const std::string& name, const std::vector<std::stri
return false;
}

bool NsClient::DelReplica(const std::string& name, const std::set<uint32_t>& pid_set, const std::string& endpoint,
std::string& msg) {
base::Status NsClient::DelReplica(const std::string& name, const std::set<uint32_t>& pid_set,
const std::string& endpoint) {
if (pid_set.empty()) {
return false;
return {base::ReturnCode::kError, "arg pid set is empty"};
}
::openmldb::nameserver::DelReplicaNSRequest request;
::openmldb::nameserver::GeneralResponse response;
Expand All @@ -409,13 +407,12 @@ bool NsClient::DelReplica(const std::string& name, const std::set<uint32_t>& pid
request.add_pid_group(pid);
}
}
bool ok = client_.SendRequest(&::openmldb::nameserver::NameServer_Stub::DelReplicaNS, &request, &response,
FLAGS_request_timeout_ms, 1);
msg = response.msg();
if (ok && response.code() == 0) {
return true;
auto st = client_.SendRequestSt(&::openmldb::nameserver::NameServer_Stub::DelReplicaNS, &request, &response,
FLAGS_request_timeout_ms, 1);
if (st.OK()) {
return {response.code(), response.msg()};
}
return false;
return st;
}

bool NsClient::ConfSet(const std::string& key, const std::string& value, std::string& msg) {
Expand Down Expand Up @@ -458,7 +455,7 @@ bool NsClient::ConfGet(const std::string& key, std::map<std::string, std::string
return false;
}

bool NsClient::ChangeLeader(const std::string& name, uint32_t pid, std::string& candidate_leader, std::string& msg) {
base::Status NsClient::ChangeLeader(const std::string& name, uint32_t pid, std::string& candidate_leader) {
::openmldb::nameserver::ChangeLeaderRequest request;
::openmldb::nameserver::GeneralResponse response;
request.set_name(name);
Expand All @@ -467,13 +464,12 @@ bool NsClient::ChangeLeader(const std::string& name, uint32_t pid, std::string&
request.set_candidate_leader(candidate_leader);
}
request.set_db(GetDb());
bool ok = client_.SendRequest(&::openmldb::nameserver::NameServer_Stub::ChangeLeader, &request, &response,
auto st = client_.SendRequestSt(&::openmldb::nameserver::NameServer_Stub::ChangeLeader, &request, &response,
FLAGS_request_timeout_ms, 1);
msg = response.msg();
if (ok && response.code() == 0) {
return true;
if (st.OK()) {
return {response.code(), response.msg()};
}
return false;
return st;
}

bool NsClient::OfflineEndpoint(const std::string& endpoint, uint32_t concurrency, std::string& msg) {
Expand All @@ -492,8 +488,8 @@ bool NsClient::OfflineEndpoint(const std::string& endpoint, uint32_t concurrency
return false;
}

bool NsClient::Migrate(const std::string& src_endpoint, const std::string& name, const std::set<uint32_t>& pid_set,
const std::string& des_endpoint, std::string& msg) {
base::Status NsClient::Migrate(const std::string& src_endpoint, const std::string& name,
const std::set<uint32_t>& pid_set, const std::string& des_endpoint) {
::openmldb::nameserver::MigrateRequest request;
::openmldb::nameserver::GeneralResponse response;
request.set_src_endpoint(src_endpoint);
Expand All @@ -503,13 +499,12 @@ bool NsClient::Migrate(const std::string& src_endpoint, const std::string& name,
for (auto pid : pid_set) {
request.add_pid(pid);
}
bool ok = client_.SendRequest(&::openmldb::nameserver::NameServer_Stub::Migrate, &request, &response,
auto st = client_.SendRequestSt(&::openmldb::nameserver::NameServer_Stub::Migrate, &request, &response,
FLAGS_request_timeout_ms, 1);
msg = response.msg();
if (ok && response.code() == 0) {
return true;
if (st.OK()) {
return {response.code(), response.msg()};
}
return false;
return st;
}

bool NsClient::RecoverEndpoint(const std::string& endpoint, bool need_restore, uint32_t concurrency, std::string& msg) {
Expand All @@ -529,20 +524,19 @@ bool NsClient::RecoverEndpoint(const std::string& endpoint, bool need_restore, u
return false;
}

bool NsClient::RecoverTable(const std::string& name, uint32_t pid, const std::string& endpoint, std::string& msg) {
base::Status NsClient::RecoverTable(const std::string& name, uint32_t pid, const std::string& endpoint) {
::openmldb::nameserver::RecoverTableRequest request;
::openmldb::nameserver::GeneralResponse response;
request.set_name(name);
request.set_pid(pid);
request.set_endpoint(endpoint);
request.set_db(GetDb());
bool ok = client_.SendRequest(&::openmldb::nameserver::NameServer_Stub::RecoverTable, &request, &response,
auto st = client_.SendRequestSt(&::openmldb::nameserver::NameServer_Stub::RecoverTable, &request, &response,
FLAGS_request_timeout_ms, 1);
msg = response.msg();
if (ok && response.code() == 0) {
return true;
if (st.OK()) {
return {response.code(), response.msg()};
}
return false;
return st;
}

bool NsClient::ConnectZK(std::string& msg) {
Expand Down Expand Up @@ -603,8 +597,8 @@ bool NsClient::GetTablePartition(const std::string& name, uint32_t pid,
return false;
}

bool NsClient::UpdateTableAliveStatus(const std::string& endpoint, std::string& name, uint32_t pid, bool is_alive,
std::string& msg) {
base::Status NsClient::UpdateTableAliveStatus(const std::string& endpoint, const std::string& name, uint32_t pid,
bool is_alive) {
::openmldb::nameserver::UpdateTableAliveRequest request;
::openmldb::nameserver::GeneralResponse response;
request.set_endpoint(endpoint);
Expand All @@ -614,13 +608,12 @@ bool NsClient::UpdateTableAliveStatus(const std::string& endpoint, std::string&
if (pid < UINT32_MAX) {
request.set_pid(pid);
}
bool ok = client_.SendRequest(&::openmldb::nameserver::NameServer_Stub::UpdateTableAliveStatus, &request, &response,
FLAGS_request_timeout_ms, 1);
msg = response.msg();
if (ok && response.code() == 0) {
return true;
auto st = client_.SendRequestSt(&::openmldb::nameserver::NameServer_Stub::UpdateTableAliveStatus, &request,
&response, FLAGS_request_timeout_ms, 1);
if (st.OK()) {
return {response.code(), response.msg()};
}
return false;
return st;
}

bool NsClient::UpdateTTL(const std::string& name, const ::openmldb::type::TTLType& type, uint64_t abs_ttl,
Expand Down
28 changes: 11 additions & 17 deletions src/client/ns_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -94,12 +94,11 @@ class NsClient : public Client {
bool MakeSnapshot(const std::string& name, const std::string& db, uint32_t pid, uint64_t end_offset,
std::string& msg); // NOLINT

base::Status ShowOPStatus(const std::string& name, uint32_t pid,
nameserver::ShowOPStatusResponse* response);
base::Status ShowOPStatus(const std::string& name, uint32_t pid, nameserver::ShowOPStatusResponse* response);

base::Status ShowOPStatus(uint64_t op_id, ::openmldb::nameserver::ShowOPStatusResponse* response);

bool CancelOP(uint64_t op_id, std::string& msg); // NOLINT
base::Status CancelOP(uint64_t op_id);

bool AddTableField(const std::string& table_name, const ::openmldb::common::ColumnDesc& column_desc,
std::string& msg); // NOLINT
Expand Down Expand Up @@ -146,35 +145,32 @@ class NsClient : public Client {
const ::openmldb::nameserver::ZoneInfo& zone_info,
std::string& msg); // NOLINT

bool AddReplica(const std::string& name, const std::set<uint32_t>& pid_set, const std::string& endpoint,
std::string& msg); // NOLINT
base::Status AddReplica(const std::string& name, const std::set<uint32_t>& pid_set, const std::string& endpoint);

bool AddReplicaNS(const std::string& name, const std::vector<std::string>& endpoint_vec, uint32_t pid,
const ::openmldb::nameserver::ZoneInfo& zone_info, const ::openmldb::api::TaskInfo& task_info);

bool DelReplica(const std::string& name, const std::set<uint32_t>& pid_set, const std::string& endpoint,
std::string& msg); // NOLINT
base::Status DelReplica(const std::string& name, const std::set<uint32_t>& pid_set, const std::string& endpoint);

bool ConfSet(const std::string& key, const std::string& value,
std::string& msg); // NOLINT

bool ConfGet(const std::string& key, std::map<std::string, std::string>& conf_map, // NOLINT
std::string& msg); // NOLINT

bool ChangeLeader(const std::string& name, uint32_t pid,
std::string& candidate_leader, // NOLINT
std::string& msg); // NOLINT
base::Status ChangeLeader(const std::string& name, uint32_t pid,
std::string& candidate_leader); // NOLINT

bool OfflineEndpoint(const std::string& endpoint, uint32_t concurrency,
std::string& msg); // NOLINT

bool Migrate(const std::string& src_endpoint, const std::string& name, const std::set<uint32_t>& pid_set,
const std::string& des_endpoint, std::string& msg); // NOLINT
base::Status Migrate(const std::string& src_endpoint, const std::string& name, const std::set<uint32_t>& pid_set,
const std::string& des_endpoint);

bool RecoverEndpoint(const std::string& endpoint, bool need_restore, uint32_t concurrency,
std::string& msg); // NOLINT

bool RecoverTable(const std::string& name, uint32_t pid, const std::string& endpoint, std::string& msg); // NOLINT
base::Status RecoverTable(const std::string& name, uint32_t pid, const std::string& endpoint);

bool ConnectZK(std::string& msg); // NOLINT

Expand All @@ -187,10 +183,8 @@ class NsClient : public Client {
::openmldb::nameserver::TablePartition& table_partition, // NOLINT
std::string& msg); // NOLINT

bool UpdateTableAliveStatus(const std::string& endpoint,
std::string& name, // NOLINT
uint32_t pid, bool is_alive,
std::string& msg); // NOLINT
base::Status UpdateTableAliveStatus(const std::string& endpoint, const std::string& name, uint32_t pid,
bool is_alive);

bool UpdateTTL(const std::string& name, const ::openmldb::type::TTLType& type, uint64_t abs_ttl, uint64_t lat_ttl,
const std::string& ts_name, std::string& msg); // NOLINT
Expand Down
Loading

0 comments on commit 84a1ffc

Please sign in to comment.