Skip to content

Commit

Permalink
Fix addSessionCount() in multu-thread case (vesoft-inc#2483)
Browse files Browse the repository at this point in the history
* Fix GraphSessionManager::addSessionCount() in multu-thread case

* Fix compilation

---------

Co-authored-by: Yichen Wang <[email protected]>
Co-authored-by: Sophie <[email protected]>
  • Loading branch information
3 people authored Mar 13, 2023
1 parent b7b9c0f commit b2d68b7
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 23 deletions.
29 changes: 8 additions & 21 deletions src/graph/session/GraphSessionManager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,7 @@ folly::Future<StatusOr<std::shared_ptr<ClientSession>>> 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()) {
Expand Down Expand Up @@ -148,10 +145,7 @@ folly::Future<StatusOr<std::shared_ptr<ClientSession>>> 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;
}
Expand Down Expand Up @@ -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++;
}
Expand Down Expand Up @@ -371,29 +362,25 @@ void GraphSessionManager::removeSessionFromLocalCache(const std::vector<SessionI
}
}

bool GraphSessionManager::addSessionCount(std::string& key) {
void GraphSessionManager::addSessionCount(std::string& key) {
auto countFindPtr = userIpSessionCount_.find(key);
if (countFindPtr != userIpSessionCount_.end()) {
countFindPtr->second.get()->fetch_add(1);
} else {
auto ret1 = userIpSessionCount_.emplace(key, std::make_shared<SessionCount>());
if (!ret1.second) {
return false;
}
userIpSessionCount_.insert_or_assign(key, std::make_shared<SessionCount>());
}
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
4 changes: 2 additions & 2 deletions src/graph/session/GraphSessionManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,10 +114,10 @@ class GraphSessionManager final : public SessionManager<ClientSession> {
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
Expand Down

0 comments on commit b2d68b7

Please sign in to comment.