diff options
author | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2019-05-05 10:35:37 -0400 |
---|---|---|
committer | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2019-05-07 15:28:19 -0400 |
commit | 74d624eb406121e530330d0920cebc61d9aeae22 (patch) | |
tree | 590104daffe70a234e44ff4ee1cbd627b503d603 | |
parent | ec6f8b802592cb88f7b2c312f368acfa47a82643 (diff) | |
download | mongo-74d624eb406121e530330d0920cebc61d9aeae22.tar.gz |
SERVER-37837 Use unique_ptr instead of shared_ptr for the SessionCatalog map
-rw-r--r-- | src/mongo/db/session_catalog.cpp | 21 | ||||
-rw-r--r-- | src/mongo/db/session_catalog.h | 41 |
2 files changed, 32 insertions, 30 deletions
diff --git a/src/mongo/db/session_catalog.cpp b/src/mongo/db/session_catalog.cpp index a82af347a04..2e0d6de0953 100644 --- a/src/mongo/db/session_catalog.cpp +++ b/src/mongo/db/session_catalog.cpp @@ -107,8 +107,9 @@ SessionCatalog::SessionToKill SessionCatalog::checkOutSessionForKill(OperationCo invariant(ObservableSession(ul, sri->session)._killed()); // Wait until the session is no longer checked out - opCtx->waitForConditionOrInterrupt(sri->availableCondVar, ul, [&ul, &sri]() { - return !ObservableSession(ul, sri->session).currentOperation(); + opCtx->waitForConditionOrInterrupt(sri->availableCondVar, ul, [&ul, &sri] { + ObservableSession osession(ul, sri->session); + return !osession.currentOperation(); }); sri->session._checkoutOpCtx = opCtx; @@ -146,23 +147,23 @@ SessionCatalog::KillToken SessionCatalog::killSession(const LogicalSessionId& ls return ObservableSession(lg, sri->session).kill(); } -std::shared_ptr<SessionCatalog::SessionRuntimeInfo> SessionCatalog::_getOrCreateSessionRuntimeInfo( +SessionCatalog::SessionRuntimeInfo* SessionCatalog::_getOrCreateSessionRuntimeInfo( WithLock, OperationContext* opCtx, const LogicalSessionId& lsid) { auto it = _sessions.find(lsid); if (it == _sessions.end()) { - it = _sessions.emplace(lsid, std::make_shared<SessionRuntimeInfo>(lsid)).first; + it = _sessions.emplace(lsid, std::make_unique<SessionRuntimeInfo>(lsid)).first; } - return it->second; + return it->second.get(); } -void SessionCatalog::_releaseSession(std::shared_ptr<SessionCatalog::SessionRuntimeInfo> sri, - boost::optional<SessionCatalog::KillToken> killToken) { +void SessionCatalog::_releaseSession(SessionRuntimeInfo* sri, + boost::optional<KillToken> killToken) { stdx::lock_guard<stdx::mutex> lg(_mutex); // Make sure we have exactly the same session on the map and that it is still associated with an // operation context (meaning checked-out) - invariant(_sessions[sri->session.getSessionId()] == sri); + invariant(_sessions[sri->session.getSessionId()].get() == sri); invariant(sri->session._checkoutOpCtx); sri->session._checkoutOpCtx = nullptr; sri->availableCondVar.notify_all(); @@ -173,10 +174,6 @@ void SessionCatalog::_releaseSession(std::shared_ptr<SessionCatalog::SessionRunt } } -OperationContext* ObservableSession::currentOperation() const { - return _session->_checkoutOpCtx; -} - SessionCatalog::KillToken ObservableSession::kill(ErrorCodes::Error reason) const { const bool firstKiller = (0 == _session->_killsRequested); ++_session->_killsRequested; diff --git a/src/mongo/db/session_catalog.h b/src/mongo/db/session_catalog.h index ca020d378d6..c88ed62eb96 100644 --- a/src/mongo/db/session_catalog.h +++ b/src/mongo/db/session_catalog.h @@ -122,26 +122,29 @@ private: stdx::condition_variable availableCondVar; }; + /** + * Blocking method, which checks-out the session set on 'opCtx'. + */ ScopedCheckedOutSession _checkOutSession(OperationContext* opCtx); /** - * May release and re-acquire it zero or more times before returning. The returned - * 'SessionRuntimeInfo' is guaranteed to be linked on the catalog's _txnTable as long as the - * lock is held. + * Creates or returns the session runtime info for 'lsid' from the '_sessions' map. The returned + * pointer is guaranteed to be linked on the map for as long as the mutex is held. */ - std::shared_ptr<SessionRuntimeInfo> _getOrCreateSessionRuntimeInfo( - WithLock, OperationContext* opCtx, const LogicalSessionId& lsid); + SessionRuntimeInfo* _getOrCreateSessionRuntimeInfo(WithLock, + OperationContext* opCtx, + const LogicalSessionId& lsid); /** * Makes a session, previously checked out through 'checkoutSession', available again. */ - void _releaseSession(std::shared_ptr<SessionRuntimeInfo> sri, - boost::optional<KillToken> killToken); + void _releaseSession(SessionRuntimeInfo* sri, boost::optional<KillToken> killToken); + // Protects the state below stdx::mutex _mutex; // Owns the Session objects for all current Sessions. - LogicalSessionIdMap<std::shared_ptr<SessionRuntimeInfo>> _sessions; + LogicalSessionIdMap<std::unique_ptr<SessionRuntimeInfo>> _sessions; }; /** @@ -151,18 +154,22 @@ private: class SessionCatalog::ScopedCheckedOutSession { public: ScopedCheckedOutSession(SessionCatalog& catalog, - std::shared_ptr<SessionCatalog::SessionRuntimeInfo> sri, + SessionCatalog::SessionRuntimeInfo* sri, boost::optional<SessionCatalog::KillToken> killToken) - : _catalog(catalog), _sri(std::move(sri)), _killToken(std::move(killToken)) {} + : _catalog(catalog), _sri(sri), _killToken(std::move(killToken)) {} + + ScopedCheckedOutSession(ScopedCheckedOutSession&& other) + : _catalog(other._catalog), _sri(other._sri), _killToken(std::move(other._killToken)) { + other._sri = nullptr; + } - ScopedCheckedOutSession(ScopedCheckedOutSession&&) = default; ScopedCheckedOutSession& operator=(ScopedCheckedOutSession&&) = delete; ScopedCheckedOutSession(const ScopedCheckedOutSession&) = delete; ScopedCheckedOutSession& operator=(ScopedCheckedOutSession&) = delete; ~ScopedCheckedOutSession() { if (_sri) { - _catalog._releaseSession(std::move(_sri), std::move(_killToken)); + _catalog._releaseSession(_sri, std::move(_killToken)); } } @@ -178,15 +185,11 @@ public: return *get(); } - operator bool() const { - return bool(_sri); - } - private: // The owning session catalog into which the session should be checked back SessionCatalog& _catalog; - std::shared_ptr<SessionCatalog::SessionRuntimeInfo> _sri; + SessionCatalog::SessionRuntimeInfo* _sri; boost::optional<SessionCatalog::KillToken> _killToken; }; @@ -242,7 +245,9 @@ public: * Returns a pointer to the current operation running on this Session, or nullptr if there is no * operation currently running on this Session. */ - OperationContext* currentOperation() const; + OperationContext* currentOperation() const { + return _session->_checkoutOpCtx; + } /** * Increments the number of "killers" for this session and returns a 'kill token' to to be |