From f13d685ade22d662070bde942eb094790d4e7b8d Mon Sep 17 00:00:00 2001 From: Blake Oler Date: Thu, 1 Nov 2018 12:14:56 -0400 Subject: SERVER-37624 Allow sessions collection TTL index expiration value to change upon node restart --- src/mongo/db/sessions_collection_standalone.cpp | 25 ++++++++++++++++++++++--- 1 file changed, 22 insertions(+), 3 deletions(-) (limited to 'src/mongo/db/sessions_collection_standalone.cpp') diff --git a/src/mongo/db/sessions_collection_standalone.cpp b/src/mongo/db/sessions_collection_standalone.cpp index 3a45269070e..a29323817bd 100644 --- a/src/mongo/db/sessions_collection_standalone.cpp +++ b/src/mongo/db/sessions_collection_standalone.cpp @@ -47,8 +47,20 @@ BSONObj lsidQuery(const LogicalSessionId& lsid) { } // namespace Status SessionsCollectionStandalone::setupSessionsCollection(OperationContext* opCtx) { + auto existsStatus = checkSessionsCollectionExists(opCtx); + if (existsStatus.isOK()) { + return Status::OK(); + } + DBDirectClient client(opCtx); - auto cmd = generateCreateIndexesCmd(); + BSONObj cmd; + + if (existsStatus.code() == ErrorCodes::IndexOptionsConflict) { + cmd = generateCollModCmd(); + } else { + cmd = generateCreateIndexesCmd(); + } + BSONObj info; if (!client.runCommand(NamespaceString::kLogicalSessionsNamespace.db().toString(), cmd, info)) { return getStatusFromCommandResult(info); @@ -66,15 +78,22 @@ Status SessionsCollectionStandalone::checkSessionsCollectionExists(OperationCont return Status{ErrorCodes::NamespaceNotFound, "config.system.sessions does not exist"}; } - auto indexExists = std::find_if(indexes.begin(), indexes.end(), [](const BSONObj& index) { + auto index = std::find_if(indexes.begin(), indexes.end(), [](const BSONObj& index) { return index.getField("name").String() == kSessionsTTLIndex; }); - if (indexExists == indexes.end()) { + if (index == indexes.end()) { return Status{ErrorCodes::IndexNotFound, "config.system.sessions does not have the required TTL index"}; }; + if (!index->hasField("expireAfterSeconds") || + index->getField("expireAfterSeconds").Int() != (localLogicalSessionTimeoutMinutes * 60)) { + return Status{ + ErrorCodes::IndexOptionsConflict, + "config.system.sessions currently has the incorrect timeout for the TTL index"}; + } + return Status::OK(); } -- cgit v1.2.1