diff options
author | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2017-10-16 15:29:41 -0400 |
---|---|---|
committer | Kaloian Manassiev <kaloian.manassiev@mongodb.com> | 2017-10-18 08:14:38 -0400 |
commit | c3f581ee05ca1252aa2f42bbf16a913f9f819e1d (patch) | |
tree | d27f776e475be72bac358d64a35ab9f65c616098 /src/mongo/db | |
parent | 689e308e910b1c689246da34ac85ca0198f68e4a (diff) | |
download | mongo-c3f581ee05ca1252aa2f42bbf16a913f9f819e1d.tar.gz |
SERVER-31342 Do not remove checked-out sessions from the cache on invalidation
Diffstat (limited to 'src/mongo/db')
-rw-r--r-- | src/mongo/db/session_catalog.cpp | 27 | ||||
-rw-r--r-- | src/mongo/db/session_catalog.h | 20 |
2 files changed, 23 insertions, 24 deletions
diff --git a/src/mongo/db/session_catalog.cpp b/src/mongo/db/session_catalog.cpp index 8f73410fe1c..30b1ddfeb8a 100644 --- a/src/mongo/db/session_catalog.cpp +++ b/src/mongo/db/session_catalog.cpp @@ -67,7 +67,13 @@ const auto operationSessionDecoration = SessionCatalog::SessionCatalog(ServiceContext* serviceContext) : _serviceContext(serviceContext) {} -SessionCatalog::~SessionCatalog() = default; +SessionCatalog::~SessionCatalog() { + stdx::lock_guard<stdx::mutex> lg(_mutex); + for (const auto& entry : _txnTable) { + auto& sri = entry.second; + invariant(!sri->checkedOut); + } +} void SessionCatalog::create(ServiceContext* service) { auto& sessionTransactionTable = sessionTransactionTableDecoration(service); @@ -145,12 +151,12 @@ ScopedCheckedOutSession SessionCatalog::checkOutSession(OperationContext* opCtx) auto sri = _getOrCreateSessionRuntimeInfo(opCtx, lsid, ul); - // Wait until the session is no longer in use + // Wait until the session is no longer checked out opCtx->waitForConditionOrInterrupt( - sri->availableCondVar, ul, [&sri]() { return sri->state != SessionRuntimeInfo::kInUse; }); + sri->availableCondVar, ul, [&sri]() { return !sri->checkedOut; }); - invariant(sri->state == SessionRuntimeInfo::kAvailable); - sri->state = SessionRuntimeInfo::kInUse; + invariant(!sri->checkedOut); + sri->checkedOut = true; return ScopedCheckedOutSession(opCtx, ScopedSession(std::move(sri))); } @@ -183,7 +189,12 @@ void SessionCatalog::invalidateSessions(OperationContext* opCtx, const auto invalidateSessionFn = [&](WithLock, SessionRuntimeInfoMap::iterator it) { auto& sri = it->second; sri->txnState.invalidate(); - _txnTable.erase(it); + + // We cannot remove checked-out sessions from the cache, because operations expect to find + // them there to check back in + if (!sri->checkedOut) { + _txnTable.erase(it); + } }; stdx::lock_guard<stdx::mutex> lg(_mutex); @@ -223,9 +234,9 @@ void SessionCatalog::_releaseSession(const LogicalSessionId& lsid) { invariant(it != _txnTable.end()); auto& sri = it->second; - invariant(sri->state == SessionRuntimeInfo::kInUse); + invariant(sri->checkedOut); - sri->state = SessionRuntimeInfo::kAvailable; + sri->checkedOut = false; sri->availableCondVar.notify_one(); } diff --git a/src/mongo/db/session_catalog.h b/src/mongo/db/session_catalog.h index e0432c439f5..6aa4e4d0442 100644 --- a/src/mongo/db/session_catalog.h +++ b/src/mongo/db/session_catalog.h @@ -127,22 +127,10 @@ private: struct SessionRuntimeInfo { SessionRuntimeInfo(LogicalSessionId lsid) : txnState(std::move(lsid)) {} - enum State { - // Session can be checked out - kAvailable, - - // Session is in use by another operation and the caller must wait to check it out - kInUse, - - // Session is at the end of its lifetime and is in a state where its persistent - // information is being cleaned up. Sessions in this state can never be checked out. The - // reason to put the session in this state is to allow for cleanup to happen - // asynchronous and while not holding locks. - kInCleanup - }; - - // Current state of the runtime info for a session - State state{kAvailable}; + // Current check-out state of the session. If set to false, the session can be checked out. + // If set to true, the session is in use by another operation and the caller must wait to + // check it out. + bool checkedOut{false}; // Signaled when the state becomes available. Uses the transaction table's mutex to protect // the state transitions. |