diff options
author | samantharitter <samantha.ritter@10gen.com> | 2017-08-14 16:38:25 -0400 |
---|---|---|
committer | samantharitter <samantha.ritter@10gen.com> | 2017-08-15 18:34:23 -0400 |
commit | ea31111dc95eb309269545348c34791b472f6c25 (patch) | |
tree | 64fa8062170370939c10a2e21412ad5cbd65a48e /src/mongo/db/logical_session_cache.cpp | |
parent | aa1c211dc382fa9fefc8709b9fbd9ae81befc88a (diff) | |
download | mongo-ea31111dc95eb309269545348c34791b472f6c25.tar.gz |
SERVER-28335 Implement refreshSessions and refreshSessionsInternal commands
Diffstat (limited to 'src/mongo/db/logical_session_cache.cpp')
-rw-r--r-- | src/mongo/db/logical_session_cache.cpp | 50 |
1 files changed, 33 insertions, 17 deletions
diff --git a/src/mongo/db/logical_session_cache.cpp b/src/mongo/db/logical_session_cache.cpp index fb3518ada63..b45eedaa4cc 100644 --- a/src/mongo/db/logical_session_cache.cpp +++ b/src/mongo/db/logical_session_cache.cpp @@ -130,35 +130,51 @@ Status LogicalSessionCache::promote(LogicalSessionId lsid) { return {ErrorCodes::NoSuchSession, "no matching session record found in the cache"}; } - // Do not use records if they have expired. - auto time = now(); - if (_isDead(it->second, time)) { - return {ErrorCodes::NoSuchSession, "no matching session record found in the cache"}; - } - // Update the last use time before returning. - it->second.setLastUse(time); + it->second.setLastUse(now()); return Status::OK(); } Status LogicalSessionCache::startSession(OperationContext* opCtx, LogicalSessionRecord record) { // Add the new record to our local cache. We will insert it into the sessions collection - // the next time _refresh is called. - - // If we get a conflict here, then an interloper may have ended this session - // and then created a new one with the same id. In this case, return a failure. - auto oldRecord = _addToCache(record); - if (oldRecord) { - if (*oldRecord != record) { - if (!_isDead(*oldRecord, now())) { - return {ErrorCodes::DuplicateSession, "session with this id already exists"}; - } + // the next time _refresh is called. If there is already a record in the cache for this + // session, we'll just write over it with our newer, more recent one. + _addToCache(record); + return Status::OK(); +} + +Status LogicalSessionCache::refreshSessions(OperationContext* opCtx, + const RefreshSessionsCmdFromClient& cmd) { + // Update the timestamps of all these records in our cache. + auto sessions = makeLogicalSessionIds(cmd.getRefreshSessions(), opCtx); + for (auto& lsid : sessions) { + if (!promote(lsid).isOK()) { + // This is a new record, insert it. + _addToCache(makeLogicalSessionRecord(opCtx, lsid, now())); } } return Status::OK(); } +Status LogicalSessionCache::refreshSessions(OperationContext* opCtx, + const RefreshSessionsCmdFromClusterMember& cmd) { + LogicalSessionRecordSet toRefresh{}; + + // Update the timestamps of all these records in our cache. + auto records = cmd.getRefreshSessionsInternal(); + for (auto& record : records) { + if (!promote(record.getId()).isOK()) { + // This is a new record, insert it. + _addToCache(record); + } + toRefresh.insert(record); + } + + // Write to the sessions collection now. + return _sessionsColl->refreshSessions(opCtx, toRefresh, now()); +} + void LogicalSessionCache::refreshNow(Client* client) { return _refresh(client); } |