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

Fix addSessionCount() in multu-thread case #5393

Merged
merged 4 commits into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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