Skip to content

Commit

Permalink
Fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Aiee committed Feb 25, 2022
1 parent 6f990bb commit dd44f0d
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 13 deletions.
28 changes: 22 additions & 6 deletions src/clients/meta/MetaClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2306,15 +2306,15 @@ Status MetaClient::authCheckFromCache(const std::string& account,
//
// Only the clients since v2.6.0 will call verifyVersion(), thus we could determine whether the
// client version is lower than v2.6.0
auto clientAddrIt = clientAddr_.find(clientIp);
if (clientAddrIt == clientAddr_.end()) {
auto clientAddrIt = clientAddrMap_.find(clientIp);
if (clientAddrIt == clientAddrMap_.end()) {
return Status::Error(
folly::sformat("The version of the client sending request from {} is lower than v2.6.0, "
"please update the client.",
clientIp.toString()));
}
// clear the key
clientAddr_.erase(clientAddrIt);
// clientAddrMap_.erase(clientAddrIt);

folly::rcu_reader guard;
const auto& metadata = *metadata_.load();
Expand Down Expand Up @@ -2487,9 +2487,9 @@ folly::Future<StatusOr<bool>> MetaClient::heartbeat() {
}

// TTL for clientAddrMap
// If multiple connections are created but do not authenticate, the clientAddr_ will keep growing.
// This is to clear the clientAddr_ regularly.
clientAddr_.clear();
// If multiple connections are created but do not authenticate, the clientAddrMap_ will keep
// growing. This is to clear the clientAddrMap_ regularly.
clearClientAddrMap();

// info used in the agent, only set once
// TOOD(spw): if we could add data path(disk) dynamicly in the future, it should be
Expand Down Expand Up @@ -3642,5 +3642,21 @@ Status MetaClient::verifyVersion() {
}
return Status::OK();
}

void MetaClient::clearClientAddrMap() {
if (clientAddrMap_.size() == 0) {
return;
}

auto curTimestamp = time::WallClock::fastNowInSec();
for (auto it = clientAddrMap_.cbegin(); it != clientAddrMap_.cend();) {
// The clientAddr is expired
if (it->second < curTimestamp) {
it = clientAddrMap_.erase(it);
} else {
++it;
}
}
}
} // namespace meta
} // namespace nebula
15 changes: 9 additions & 6 deletions src/clients/meta/MetaClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ using FTIndexMap = std::unordered_map<std::string, cpp2::FTIndex>;

using SessionMap = std::unordered_map<SessionID, cpp2::Session>;

using clientAddr = folly::ConcurrentHashMap<HostAddr, bool>;
using clientAddrMap = folly::ConcurrentHashMap<HostAddr, int64_t>;
class MetaChangedListener {
public:
virtual ~MetaChangedListener() = default;
Expand Down Expand Up @@ -645,8 +645,8 @@ class MetaClient {
return options_.localHost_.toString();
}

clientAddr& getClientAddr() {
return clientAddr_;
clientAddrMap& getClientAddrMap() {
return clientAddrMap_;
}

protected:
Expand Down Expand Up @@ -735,6 +735,9 @@ class MetaClient {

Status verifyVersion();

// Removes expired keys in the clientAddrMap_
void clearClientAddrMap();

private:
std::shared_ptr<folly::IOThreadPoolExecutor> ioThreadPool_;
std::shared_ptr<thrift::ThriftClientManager<cpp2::MetaServiceAsyncClient>> clientsMan_;
Expand Down Expand Up @@ -818,14 +821,14 @@ class MetaClient {
// TODO(Aiee) This is a walkround to address the problem that using a lower version(< v2.6.0)
// client to connect with higher version(>= v3.0.0) Nebula service will cause a crash.
//
// The key here is the host of the client that sends the request, and the value indicates whether
// the service allows the connection from that address.
// The key here is the host of the client that sends the request, and the value indicates the
// expiration of the key because we don't want to keep the key forever.
//
// The assumption here is that there is ONLY ONE VERSION of the client in the host.
//
// This map will be updated when verifyVersion() is called. Only the clients since v2.6.0 will
// call verifyVersion(), thus we could determine whether the client version is lower than v2.6.0
clientAddr clientAddr_;
clientAddrMap clientAddrMap_;

// Global service client
ServiceClientsList serviceClientList_;
Expand Down
6 changes: 5 additions & 1 deletion src/graph/service/GraphService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
namespace nebula {
namespace graph {

const int64_t clientAddrTimeout = 60;

Status GraphService::init(std::shared_ptr<folly::IOThreadPoolExecutor> ioExecutor,
const HostAddr& hostAddr) {
auto addrs = network::NetworkUtils::toHosts(FLAGS_meta_server_addrs);
Expand Down Expand Up @@ -249,7 +251,9 @@ folly::Future<cpp2::VerifyClientVersionResp> GraphService::future_verifyClientVe
// The client sent request has a version >= v2.6.0, mark the address as valid
auto* peer = getRequestContext()->getPeerAddress();
auto clientAddr = HostAddr(peer->getAddressStr(), peer->getPort());
metaClient_->getClientAddr().insert(clientAddr, true);

auto ttlTimestamp = time::WallClock::fastNowInSec() + clientAddrTimeout;
metaClient_->getClientAddrMap().insert(clientAddr, ttlTimestamp);
return folly::makeFuture<cpp2::VerifyClientVersionResp>(std::move(resp));
}
} // namespace graph
Expand Down

0 comments on commit dd44f0d

Please sign in to comment.