summaryrefslogtreecommitdiff
path: root/src/mongo/db/session
diff options
context:
space:
mode:
authorBenety Goh <benety@mongodb.com>2022-08-23 12:06:13 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2022-08-23 17:17:05 +0000
commit3ecc949d233e878d1b9c2a0aee98eb99a044d260 (patch)
tree89f04129fb07ce156ceef8582daa163b9a813564 /src/mongo/db/session
parent29b2944a389c1937c294fd061f54526a14f49498 (diff)
downloadmongo-3ecc949d233e878d1b9c2a0aee98eb99a044d260.tar.gz
SERVER-68215 make MongoDSessionCatalog a decoration on ServiceContext
Diffstat (limited to 'src/mongo/db/session')
-rw-r--r--src/mongo/db/session/session_catalog_mongod.cpp24
-rw-r--r--src/mongo/db/session/session_catalog_mongod.h36
-rw-r--r--src/mongo/db/session/session_catalog_mongod_test.cpp8
3 files changed, 53 insertions, 15 deletions
diff --git a/src/mongo/db/session/session_catalog_mongod.cpp b/src/mongo/db/session/session_catalog_mongod.cpp
index 1d5bc3df966..82198fdf50c 100644
--- a/src/mongo/db/session/session_catalog_mongod.cpp
+++ b/src/mongo/db/session/session_catalog_mongod.cpp
@@ -28,10 +28,10 @@
*/
-#include "mongo/platform/basic.h"
-
#include "mongo/db/session/session_catalog_mongod.h"
+#include <memory>
+
#include "mongo/bson/bsonmisc.h"
#include "mongo/db/catalog_raii.h"
#include "mongo/db/client.h"
@@ -60,6 +60,9 @@
namespace mongo {
namespace {
+const auto getMongoDSessionCatalog =
+ ServiceContext::declareDecoration<std::unique_ptr<MongoDSessionCatalog>>();
+
struct SessionTasksExecutor {
SessionTasksExecutor()
: threadPool([] {
@@ -477,6 +480,21 @@ void abortInProgressTransactions(OperationContext* opCtx) {
const std::string MongoDSessionCatalog::kConfigTxnsPartialIndexName = "parent_lsid";
+MongoDSessionCatalog* MongoDSessionCatalog::get(OperationContext* opCtx) {
+ return get(opCtx->getServiceContext());
+}
+
+MongoDSessionCatalog* MongoDSessionCatalog::get(ServiceContext* service) {
+ const auto& sessionCatalog = getMongoDSessionCatalog(service);
+ invariant(sessionCatalog);
+ return sessionCatalog.get();
+}
+
+void MongoDSessionCatalog::set(ServiceContext* service,
+ std::unique_ptr<MongoDSessionCatalog> sessionCatalog) {
+ getMongoDSessionCatalog(service) = std::move(sessionCatalog);
+}
+
BSONObj MongoDSessionCatalog::getConfigTxnPartialIndexSpec() {
NewIndexSpec index;
index.setV(int(IndexDescriptor::kLatestIndexVersion));
@@ -490,6 +508,8 @@ BSONObj MongoDSessionCatalog::getConfigTxnPartialIndexSpec() {
return index.toBSON();
}
+MongoDSessionCatalog::MongoDSessionCatalog() {}
+
void MongoDSessionCatalog::onStepUp(OperationContext* opCtx) {
// Invalidate sessions that could have a retryable write on it, so that we can refresh from disk
// in case the in-memory state was out of sync.
diff --git a/src/mongo/db/session/session_catalog_mongod.h b/src/mongo/db/session/session_catalog_mongod.h
index a2de7228bec..7b65df78193 100644
--- a/src/mongo/db/session/session_catalog_mongod.h
+++ b/src/mongo/db/session/session_catalog_mongod.h
@@ -36,7 +36,22 @@ namespace mongo {
class SessionsCollection;
class MongoDSessionCatalog {
+ MongoDSessionCatalog(const MongoDSessionCatalog&) = delete;
+ MongoDSessionCatalog& operator=(const MongoDSessionCatalog&) = delete;
+
public:
+ /**
+ * Retrieves the mongod session transaction table associated with the service or operation
+ * context.
+ */
+ static MongoDSessionCatalog* get(OperationContext* opCtx);
+ static MongoDSessionCatalog* get(ServiceContext* service);
+
+ /**
+ * Sets the mongod session transaction table associated with the service or operation context.
+ */
+ static void set(ServiceContext* service, std::unique_ptr<MongoDSessionCatalog> sessionCatalog);
+
static const std::string kConfigTxnsPartialIndexName;
// The max batch size is chosen so that a single batch won't exceed the 16MB BSON object size
@@ -49,12 +64,14 @@ public:
*/
static BSONObj getConfigTxnPartialIndexSpec();
+ MongoDSessionCatalog();
+
/**
* Invoked when the node enters the primary state. Ensures that the transactions collection is
* created. Throws on severe exceptions due to which it is not safe to continue the step-up
* process.
*/
- static void onStepUp(OperationContext* opCtx);
+ void onStepUp(OperationContext* opCtx);
/**
* Fetches the UUID of the transaction table, or an empty optional if the collection does not
@@ -62,21 +79,20 @@ public:
*
* Required for rollback via refetch.
*/
- static boost::optional<UUID> getTransactionTableUUID(OperationContext* opCtx);
+ boost::optional<UUID> getTransactionTableUUID(OperationContext* opCtx);
/**
* Callback to be invoked in response to insert/update/delete of 'config.transactions' in order
* to notify the session catalog that the on-disk contents are out of sync with the in-memory
* state. The 'singleSessionDoc' must contain the _id of the session which was updated.
*/
- static void observeDirectWriteToConfigTransactions(OperationContext* opCtx,
- BSONObj singleSessionDoc);
+ void observeDirectWriteToConfigTransactions(OperationContext* opCtx, BSONObj singleSessionDoc);
/**
* Callback to be invoked when the contents of 'config.transactions' are out of sync with that
* in the in-memory catalog, such as when rollback happens or drop of 'config.transactions'.
*/
- static void invalidateAllSessions(OperationContext* opCtx);
+ void invalidateAllSessions(OperationContext* opCtx);
/**
* Locates session entries from the in-memory catalog and in 'config.transactions' which have
@@ -84,15 +100,15 @@ public:
*
* Returns the number of sessions, which were reaped from the persisted store on disk.
*/
- static int reapSessionsOlderThan(OperationContext* opCtx,
- SessionsCollection& sessionsCollection,
- Date_t possiblyExpired);
+ int reapSessionsOlderThan(OperationContext* opCtx,
+ SessionsCollection& sessionsCollection,
+ Date_t possiblyExpired);
/**
* Deletes the given session ids from config.transactions and config.image_collection.
*/
- static int removeSessionsTransactionRecords(OperationContext* opCtx,
- const std::vector<LogicalSessionId>& lsidsToRemove);
+ int removeSessionsTransactionRecords(OperationContext* opCtx,
+ const std::vector<LogicalSessionId>& lsidsToRemove);
};
/**
diff --git a/src/mongo/db/session/session_catalog_mongod_test.cpp b/src/mongo/db/session/session_catalog_mongod_test.cpp
index 29acb444f56..5e988142e97 100644
--- a/src/mongo/db/session/session_catalog_mongod_test.cpp
+++ b/src/mongo/db/session/session_catalog_mongod_test.cpp
@@ -84,9 +84,11 @@ TEST_F(MongoDSessionCatalogTest, ReapSomeExpiredSomeNot) {
_collectionMock->add(LogicalSessionRecord(makeLogicalSessionIdForTest(), clock()->now()));
_collectionMock->add(LogicalSessionRecord(makeLogicalSessionIdForTest(), clock()->now()));
- ASSERT_EQ(2,
- MongoDSessionCatalog::reapSessionsOlderThan(
- _opCtx, *_collection, clock()->now() - Minutes{30}));
+ auto mongoDSessionCatalog = MongoDSessionCatalog{};
+ auto numReaped = mongoDSessionCatalog.reapSessionsOlderThan(
+ _opCtx, *_collection, clock()->now() - Minutes{30});
+
+ ASSERT_EQ(2, numReaped);
}
} // namespace