From b2d68b717a14c93c219b4548e29118bc0ed4a8f2 Mon Sep 17 00:00:00 2001 From: nebula-bots <88429921+nebula-bots@users.noreply.github.com> Date: Mon, 13 Mar 2023 15:58:22 +0800 Subject: [PATCH] Fix `addSessionCount()` in multu-thread case (#2483) * Fix GraphSessionManager::addSessionCount() in multu-thread case * Fix compilation --------- Co-authored-by: Yichen Wang <18348405+Aiee@users.noreply.github.com> Co-authored-by: Sophie <84560950+Sophie-Xie@users.noreply.github.com> --- src/graph/session/GraphSessionManager.cpp | 29 +++++++---------------- src/graph/session/GraphSessionManager.h | 4 ++-- 2 files changed, 10 insertions(+), 23 deletions(-) diff --git a/src/graph/session/GraphSessionManager.cpp b/src/graph/session/GraphSessionManager.cpp index e11fbc57d03..fbd7d02fd43 100644 --- a/src/graph/session/GraphSessionManager.cpp +++ b/src/graph/session/GraphSessionManager.cpp @@ -84,10 +84,7 @@ folly::Future>> GraphSessionManager::fin return Status::Error("Insert session to local cache failed."); } std::string key = session.get_user_name() + session.get_client_ip(); - bool addResp = addSessionCount(key); - if (!addResp) { - return Status::Error("Insert userIpSessionCount to local cache failed."); - } + addSessionCount(key); // update the space info to sessionPtr if (!spaceName.empty()) { @@ -148,10 +145,7 @@ folly::Future>> GraphSessionManager::cre return Status::Error("Insert session to local cache failed."); } std::string sessionKey = userName + clientIp; - bool addResp = addSessionCount(sessionKey); - if (!addResp) { - return Status::Error("Insert userIpSessionCount to local cache failed."); - } + addSessionCount(sessionKey); updateSessionInfo(sessionPtr.get()); return sessionPtr; } @@ -337,10 +331,7 @@ Status GraphSessionManager::init() { if (!ret.second) { return Status::Error("Insert session to local cache failed."); } - bool addResp = addSessionCount(key); - if (!addResp) { - return Status::Error("Insert userIpSessionCount to local cache failed."); - } + addSessionCount(key); updateSessionInfo(sessionPtr.get()); loadSessionCount++; } @@ -371,29 +362,25 @@ void GraphSessionManager::removeSessionFromLocalCache(const std::vectorsecond.get()->fetch_add(1); } else { - auto ret1 = userIpSessionCount_.emplace(key, std::make_shared()); - if (!ret1.second) { - return false; - } + userIpSessionCount_.insert_or_assign(key, std::make_shared()); } - return true; } -bool GraphSessionManager::subSessionCount(std::string& key) { +void GraphSessionManager::subSessionCount(std::string& key) { auto countFindPtr = userIpSessionCount_.find(key); if (countFindPtr == userIpSessionCount_.end()) { - return false; + VLOG(1) << "Session count not found for key: " << key; + return; } auto count = countFindPtr->second.get()->fetch_sub(1); if (count <= 0) { userIpSessionCount_.erase(countFindPtr); } - return true; } } // namespace graph } // namespace nebula diff --git a/src/graph/session/GraphSessionManager.h b/src/graph/session/GraphSessionManager.h index 749e1a06cec..cc13f1a956e 100644 --- a/src/graph/session/GraphSessionManager.h +++ b/src/graph/session/GraphSessionManager.h @@ -114,10 +114,10 @@ class GraphSessionManager final : public SessionManager { void updateSessionInfo(ClientSession* session); // add sessionCount - bool addSessionCount(std::string& key); + void addSessionCount(std::string& key); // sub sessionCount - bool subSessionCount(std::string& key); + void subSessionCount(std::string& key); }; } // namespace graph