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: use STL facilities instead of 3rd party libraries #4030

Merged
merged 21 commits into from
Mar 16, 2022
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
199 changes: 99 additions & 100 deletions src/clients/meta/MetaClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,106 +674,105 @@ void MetaClient::getResponse(Request req,
folly::RWSpinLock::ReadHolder holder(&hostLock_);
host = toLeader ? leader_ : active_;
}
folly::via(
evb,
[host,
evb,
req = std::move(req),
remoteFunc = std::move(remoteFunc),
respGen = std::move(respGen),
pro = std::move(pro),
toLeader,
retry,
retryLimit,
this]() mutable {
auto client = clientsMan_->client(host, evb, false, FLAGS_meta_client_timeout_ms);
VLOG(1) << "Send request to meta " << host;
remoteFunc(client, req)
.via(evb)
.then([host,
req = std::move(req),
remoteFunc = std::move(remoteFunc),
respGen = std::move(respGen),
pro = std::move(pro),
toLeader,
retry,
retryLimit,
evb,
this](folly::Try<RpcResponse>&& t) mutable {
// exception occurred during RPC
if (t.hasException()) {
stats::StatsManager::addValue(kNumRpcSentToMetadFailed);
if (toLeader) {
updateLeader();
} else {
updateActive();
}
if (retry < retryLimit) {
evb->runAfterDelay(
[req = std::move(req),
remoteFunc = std::move(remoteFunc),
respGen = std::move(respGen),
pro = std::move(pro),
toLeader,
retry,
retryLimit,
this]() mutable {
getResponse(std::move(req),
std::move(remoteFunc),
std::move(respGen),
std::move(pro),
toLeader,
retry + 1,
retryLimit);
},
FLAGS_meta_client_retry_interval_secs * 1000);
return;
} else {
LOG(ERROR) << "Send request to " << host << ", exceed retry limit";
LOG(ERROR) << "RpcResponse exception: " << t.exception().what().c_str();
pro.setValue(
Status::Error("RPC failure in MetaClient: %s", t.exception().what().c_str()));
}
return;
}

auto&& resp = t.value();
auto code = resp.get_code();
if (code == nebula::cpp2::ErrorCode::SUCCEEDED) {
// succeeded
pro.setValue(respGen(std::move(resp)));
return;
} else if (code == nebula::cpp2::ErrorCode::E_LEADER_CHANGED ||
code == nebula::cpp2::ErrorCode::E_MACHINE_NOT_FOUND) {
updateLeader(resp.get_leader());
if (retry < retryLimit) {
evb->runAfterDelay(
[req = std::move(req),
remoteFunc = std::move(remoteFunc),
respGen = std::move(respGen),
pro = std::move(pro),
toLeader,
retry,
retryLimit,
this]() mutable {
getResponse(std::move(req),
std::move(remoteFunc),
std::move(respGen),
std::move(pro),
toLeader,
retry + 1,
retryLimit);
},
FLAGS_meta_client_retry_interval_secs * 1000);
return;
}
} else if (code == nebula::cpp2::ErrorCode::E_CLIENT_SERVER_INCOMPATIBLE) {
pro.setValue(respGen(std::move(resp)));
return;
}
pro.setValue(this->handleResponse(resp));
}); // then
}); // via
folly::via(evb,
[host,
evb,
req = std::move(req),
remoteFunc = std::move(remoteFunc),
respGen = std::move(respGen),
pro = std::move(pro),
toLeader,
retry,
retryLimit,
this]() mutable {
auto client = clientsMan_->client(host, evb, false, FLAGS_meta_client_timeout_ms);
VLOG(1) << "Send request to meta " << host;
remoteFunc(client, req)
.via(evb)
.then([host,
req = std::move(req),
remoteFunc = std::move(remoteFunc),
respGen = std::move(respGen),
pro = std::move(pro),
toLeader,
retry,
retryLimit,
evb,
this](folly::Try<RpcResponse>&& t) mutable {
// exception occurred during RPC
if (t.hasException()) {
stats::StatsManager::addValue(kNumRpcSentToMetadFailed);
if (toLeader) {
updateLeader();
} else {
updateActive();
}
if (retry < retryLimit) {
evb->runAfterDelay(
[req = std::move(req),
remoteFunc = std::move(remoteFunc),
respGen = std::move(respGen),
pro = std::move(pro),
toLeader,
retry,
retryLimit,
this]() mutable {
getResponse(std::move(req),
std::move(remoteFunc),
std::move(respGen),
std::move(pro),
toLeader,
retry + 1,
retryLimit);
},
FLAGS_meta_client_retry_interval_secs * 1000);
return;
} else {
LOG(ERROR) << "Send request to " << host << ", exceed retry limit";
LOG(ERROR) << "RpcResponse exception: " << t.exception().what().c_str();
pro.setValue(Status::Error("RPC failure in MetaClient: %s",
t.exception().what().c_str()));
}
return;
}

auto&& resp = t.value();
auto code = resp.get_code();
if (code == nebula::cpp2::ErrorCode::SUCCEEDED) {
// succeeded
pro.setValue(respGen(std::move(resp)));
return;
} else if (code == nebula::cpp2::ErrorCode::E_LEADER_CHANGED ||
code == nebula::cpp2::ErrorCode::E_MACHINE_NOT_FOUND) {
updateLeader(resp.get_leader());
if (retry < retryLimit) {
evb->runAfterDelay(
[req = std::move(req),
remoteFunc = std::move(remoteFunc),
respGen = std::move(respGen),
pro = std::move(pro),
toLeader,
retry,
retryLimit,
this]() mutable {
getResponse(std::move(req),
std::move(remoteFunc),
std::move(respGen),
std::move(pro),
toLeader,
retry + 1,
retryLimit);
},
FLAGS_meta_client_retry_interval_secs * 1000);
return;
}
} else if (code == nebula::cpp2::ErrorCode::E_CLIENT_SERVER_INCOMPATIBLE) {
pro.setValue(respGen(std::move(resp)));
return;
}
pro.setValue(this->handleResponse(resp));
}); // then
}); // via
}

std::vector<SpaceIdName> MetaClient::toSpaceIdName(const std::vector<cpp2::IdName>& tIdNames) {
Expand Down
6 changes: 3 additions & 3 deletions src/clients/storage/InternalStorageClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ ::nebula::cpp2::ErrorCode getErrorCode(T& tryResp) {

void InternalStorageClient::chainUpdateEdge(cpp2::UpdateEdgeRequest& reversedRequest,
TermID termOfSrc,
folly::Optional<int64_t> optVersion,
std::optional<int64_t> optVersion,
folly::Promise<::nebula::cpp2::ErrorCode>&& p,
folly::EventBase* evb) {
auto spaceId = reversedRequest.get_space_id();
Expand Down Expand Up @@ -83,7 +83,7 @@ void InternalStorageClient::chainUpdateEdge(cpp2::UpdateEdgeRequest& reversedReq

void InternalStorageClient::chainAddEdges(cpp2::AddEdgesRequest& directReq,
TermID termId,
folly::Optional<int64_t> optVersion,
std::optional<int64_t> optVersion,
folly::Promise<nebula::cpp2::ErrorCode>&& p,
folly::EventBase* evb) {
auto spaceId = directReq.get_space_id();
Expand Down Expand Up @@ -120,7 +120,7 @@ void InternalStorageClient::chainAddEdges(cpp2::AddEdgesRequest& directReq,

cpp2::ChainAddEdgesRequest InternalStorageClient::makeChainAddReq(const cpp2::AddEdgesRequest& req,
TermID termId,
folly::Optional<int64_t> ver) {
std::optional<int64_t> ver) {
cpp2::ChainAddEdgesRequest ret;
ret.space_id_ref() = req.get_space_id();
ret.parts_ref() = req.get_parts();
Expand Down
6 changes: 3 additions & 3 deletions src/clients/storage/InternalStorageClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ class InternalStorageClient

virtual void chainUpdateEdge(cpp2::UpdateEdgeRequest& reversedRequest,
TermID termOfSrc,
folly::Optional<int64_t> optVersion,
std::optional<int64_t> optVersion,
folly::Promise<::nebula::cpp2::ErrorCode>&& p,
folly::EventBase* evb = nullptr);

virtual void chainAddEdges(cpp2::AddEdgesRequest& req,
TermID termId,
folly::Optional<int64_t> optVersion,
std::optional<int64_t> optVersion,
folly::Promise<::nebula::cpp2::ErrorCode>&& p,
folly::EventBase* evb = nullptr);

Expand All @@ -55,7 +55,7 @@ class InternalStorageClient
private:
cpp2::ChainAddEdgesRequest makeChainAddReq(const cpp2::AddEdgesRequest& req,
TermID termId,
folly::Optional<int64_t> optVersion);
std::optional<int64_t> optVersion);
};

} // namespace storage
Expand Down
12 changes: 6 additions & 6 deletions src/codec/NebulaCodecImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,17 +27,17 @@ std::string NebulaCodecImpl::encode(std::vector<Value> values,
RowWriter writer(schema);
for (auto& value : values) {
if (value.type() == typeid(int32_t)) {
writer << boost::any_cast<int32_t>(value);
writer << std::any_cast<int32_t>(value);
} else if (value.type() == typeid(int64_t)) {
writer << boost::any_cast<int64_t>(value);
writer << std::any_cast<int64_t>(value);
} else if (value.type() == typeid(std::string)) {
writer << boost::any_cast<std::string>(value);
writer << std::any_cast<std::string>(value);
} else if (value.type() == typeid(double)) {
writer << boost::any_cast<double>(value);
writer << std::any_cast<double>(value);
} else if (value.type() == typeid(float)) {
writer << boost::any_cast<float>(value);
writer << std::any_cast<float>(value);
} else if (value.type() == typeid(bool)) {
writer << boost::any_cast<bool>(value);
writer << std::any_cast<bool>(value);
} else {
LOG(ERROR) << "Value Type :" << value.type().name() << std::endl;
}
Expand Down
2 changes: 1 addition & 1 deletion src/codec/include/NebulaCodec.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ namespace nebula {

class NebulaCodec {
public:
typedef boost::any Value;
using Value = std::any;

virtual ~NebulaCodec() = default;

Expand Down
28 changes: 14 additions & 14 deletions src/codec/test/NebulaCodecTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@
#include <gtest/gtest.h>

#include "codec/NebulaCodecImpl.h"
#include "codec/RowReaderWrapper"
#include "codec/RowReaderWrapper.h"
#include "codec/test/SchemaWriter.h"
#include "common/base/Base.h"

namespace nebula {

TEST(NebulaCodec, encode) {
std::vector<boost::any> v;
std::vector<std::any> v;
v.emplace_back(1);
v.emplace_back(false);
v.emplace_back(3.14F);
v.emplace_back(3.14);
v.emplace_back(std::string("hi"));

EXPECT_EQ(boost::any_cast<int>(v[0]), 1);
EXPECT_EQ(boost::any_cast<bool>(v[1]), false);
EXPECT_EQ(boost::any_cast<float>(v[2]), 3.14F);
EXPECT_EQ(boost::any_cast<double>(v[3]), 3.14);
EXPECT_EQ(boost::any_cast<std::string>(v[4]), "hi");
EXPECT_EQ(std::any_cast<int>(v[0]), 1);
EXPECT_EQ(std::any_cast<bool>(v[1]), false);
EXPECT_EQ(std::any_cast<float>(v[2]), 3.14F);
EXPECT_EQ(std::any_cast<double>(v[3]), 3.14);
EXPECT_EQ(std::any_cast<std::string>(v[4]), "hi");

SchemaWriter schemaWriter;
schemaWriter.appendCol("i_field", cpp2::SupportedType::INT);
Expand Down Expand Up @@ -81,7 +81,7 @@ TEST(NebulaCodec, encode) {
EXPECT_EQ("hi", sVal.toString());

// check empty values
std::vector<boost::any> emptyV;
std::vector<std::any> emptyV;
std::string emptyEncoded = codec.encode(emptyV);

SchemaWriter emptyWriter;
Expand Down Expand Up @@ -133,12 +133,12 @@ TEST(NebulaCodec, decode) {
NebulaCodecImpl codec;
auto result = codec.decode(encoded, schema);

EXPECT_TRUE(boost::any_cast<bool>(result.value()["b_field"]));
EXPECT_EQ(boost::any_cast<int>(result.value()["i_field"]), 64);
EXPECT_EQ(boost::any_cast<int64_t>(result.value()["v_field"]), 0x1122334455667788L);
EXPECT_EQ(boost::any_cast<float>(result.value()["f_field"]), 3.14F);
EXPECT_EQ(boost::any_cast<double>(result.value()["d_field"]), 2.718);
EXPECT_EQ(boost::any_cast<std::string>(result.value()["s_field"]), "Hello World!");
EXPECT_TRUE(std::any_cast<bool>(result.value()["b_field"]));
EXPECT_EQ(std::any_cast<int>(result.value()["i_field"]), 64);
EXPECT_EQ(std::any_cast<int64_t>(result.value()["v_field"]), 0x1122334455667788L);
EXPECT_EQ(std::any_cast<float>(result.value()["f_field"]), 3.14F);
EXPECT_EQ(std::any_cast<double>(result.value()["d_field"]), 2.718);
EXPECT_EQ(std::any_cast<std::string>(result.value()["s_field"]), "Hello World!");

// check empty encoded string
auto empty_encoded = codec.decode("", schema);
Expand Down
6 changes: 3 additions & 3 deletions src/common/base/Base.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,8 @@
#include <sys/types.h>
#include <unistd.h>

#include <any>
#include <atomic>
#include <boost/any.hpp>
#include <boost/variant.hpp>
#include <cassert>
#include <cerrno>
#include <chrono>
Expand Down Expand Up @@ -47,6 +46,7 @@
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <variant>
#include <vector>

#if defined(__clang__) && defined(__aarch64__)
Expand Down Expand Up @@ -107,7 +107,7 @@

namespace nebula {

using VariantType = boost::variant<int64_t, double, bool, std::string>;
using VariantType = std::variant<int64_t, double, bool, std::string>;

#ifndef VAR_INT64
#define VAR_INT64 0
Expand Down
Loading