diff options
author | Nathan Louie <nathan.louie@10gen.com> | 2018-06-11 15:25:05 -0400 |
---|---|---|
committer | Nathan Louie <nathan.louie@10gen.com> | 2018-06-18 17:17:35 -0400 |
commit | 2a81c6190985975913fe75dace4fd157b8e82590 (patch) | |
tree | 14cc55da81e9ab4360c8e9b6405ceae6ee62b06d /src | |
parent | 013b82bf5f58bd7de8ae2f4d28d24f82afa22e64 (diff) | |
download | mongo-2a81c6190985975913fe75dace4fd157b8e82590.tar.gz |
SERVER-35149 Track total number of open transactions in ServerTransactionsMetrics
Diffstat (limited to 'src')
-rw-r--r-- | src/mongo/db/server_transactions_metrics.cpp | 12 | ||||
-rw-r--r-- | src/mongo/db/server_transactions_metrics.h | 7 | ||||
-rw-r--r-- | src/mongo/db/session.cpp | 3 | ||||
-rw-r--r-- | src/mongo/db/session_test.cpp | 62 | ||||
-rw-r--r-- | src/mongo/db/transactions_stats.idl | 3 |
5 files changed, 86 insertions, 1 deletions
diff --git a/src/mongo/db/server_transactions_metrics.cpp b/src/mongo/db/server_transactions_metrics.cpp index fe31687f288..07319300f74 100644 --- a/src/mongo/db/server_transactions_metrics.cpp +++ b/src/mongo/db/server_transactions_metrics.cpp @@ -51,6 +51,18 @@ ServerTransactionsMetrics* ServerTransactionsMetrics::get(OperationContext* opCt return get(opCtx->getServiceContext()); } +unsigned long long ServerTransactionsMetrics::getCurrentOpen() const { + return _currentOpen.load(); +} + +void ServerTransactionsMetrics::decrementCurrentOpen() { + _currentOpen.fetchAndSubtract(1); +} + +void ServerTransactionsMetrics::incrementCurrentOpen() { + _currentOpen.fetchAndAdd(1); +} + unsigned long long ServerTransactionsMetrics::getTotalStarted() const { return _totalStarted.load(); } diff --git a/src/mongo/db/server_transactions_metrics.h b/src/mongo/db/server_transactions_metrics.h index a9e2490cf88..22892d37944 100644 --- a/src/mongo/db/server_transactions_metrics.h +++ b/src/mongo/db/server_transactions_metrics.h @@ -46,6 +46,10 @@ public: static ServerTransactionsMetrics* get(ServiceContext* service); static ServerTransactionsMetrics* get(OperationContext* opCtx); + unsigned long long getCurrentOpen() const; + void decrementCurrentOpen(); + void incrementCurrentOpen(); + unsigned long long getTotalStarted() const; void incrementTotalStarted(); @@ -61,6 +65,9 @@ public: void updateStats(TransactionsStats* stats); private: + // The total number of open transactions. + AtomicUInt64 _currentOpen{0}; + // The total number of multi-document transactions started since the last server startup. AtomicUInt64 _totalStarted{0}; diff --git a/src/mongo/db/session.cpp b/src/mongo/db/session.cpp index 4f3afc8b8ae..b3dc45168fd 100644 --- a/src/mongo/db/session.cpp +++ b/src/mongo/db/session.cpp @@ -607,6 +607,7 @@ void Session::_beginOrContinueTxn(WithLock wl, Date_t::fromMillisSinceEpoch(_singleTransactionStats->getStartTime() / 1000) + stdx::chrono::seconds{transactionLifetimeLimitSeconds.load()}; ServerTransactionsMetrics::get(getGlobalServiceContext())->incrementTotalStarted(); + ServerTransactionsMetrics::get(getGlobalServiceContext())->incrementCurrentOpen(); } else { // Execute a retryable write or snapshot read. invariant(startTransaction == boost::none); @@ -914,6 +915,7 @@ void Session::_abortTransaction(WithLock wl) { if (isMultiDocumentTransaction) { _singleTransactionStats->setEndTime(curTimeMicros64()); } + ServerTransactionsMetrics::get(getGlobalServiceContext())->decrementCurrentOpen(); } void Session::_beginOrContinueTxnOnMigration(WithLock wl, TxnNumber txnNumber) { @@ -1049,6 +1051,7 @@ void Session::_commitTransaction(stdx::unique_lock<stdx::mutex> lk, OperationCon if (isMultiDocumentTransaction) { _singleTransactionStats->setEndTime(curTimeMicros64()); } + ServerTransactionsMetrics::get(opCtx)->decrementCurrentOpen(); } BSONObj Session::reportStashedState() const { diff --git a/src/mongo/db/session_test.cpp b/src/mongo/db/session_test.cpp index a29ffb11c69..0104f0d6fa0 100644 --- a/src/mongo/db/session_test.cpp +++ b/src/mongo/db/session_test.cpp @@ -1235,6 +1235,66 @@ TEST_F(SessionTest, IncrementTotalAbortedUponAbort) { ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getTotalAborted(), beforeAbortCount + 1U); } +TEST_F(SessionTest, TrackTotalOpenTransactionsWithAbort) { + const auto sessionId = makeLogicalSessionIdForTest(); + Session session(sessionId); + session.refreshFromStorageIfNeeded(opCtx()); + + unsigned long long beforeTransactionStart = + ServerTransactionsMetrics::get(opCtx())->getCurrentOpen(); + + const TxnNumber txnNum = 1; + opCtx()->setLogicalSessionId(sessionId); + opCtx()->setTxnNumber(txnNum); + + // Tests that starting a transaction increments the open transactions counter by 1. + session.beginOrContinueTxn(opCtx(), txnNum, false, true, "testDB", "insert"); + session.unstashTransactionResources(opCtx(), "insert"); + ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentOpen(), + beforeTransactionStart + 1U); + + // Tests that stashing the transaction resources does not affect the open transactions counter. + { Lock::GlobalLock lk(opCtx(), MODE_IX, Date_t::now(), Lock::InterruptBehavior::kThrow); } + session.stashTransactionResources(opCtx()); + ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentOpen(), + beforeTransactionStart + 1U); + + // Tests that aborting a transaction decrements the open transactions counter by 1. + session.abortArbitraryTransaction(); + ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentOpen(), beforeTransactionStart); +} + +TEST_F(SessionTest, TrackTotalOpenTransactionsWithCommit) { + const auto sessionId = makeLogicalSessionIdForTest(); + Session session(sessionId); + session.refreshFromStorageIfNeeded(opCtx()); + + unsigned long long beforeTransactionStart = + ServerTransactionsMetrics::get(opCtx())->getCurrentOpen(); + + const TxnNumber txnNum = 1; + opCtx()->setLogicalSessionId(sessionId); + opCtx()->setTxnNumber(txnNum); + + // Tests that starting a transaction increments the open transactions counter by 1. + session.beginOrContinueTxn(opCtx(), txnNum, false, true, "testDB", "insert"); + session.unstashTransactionResources(opCtx(), "insert"); + ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentOpen(), + beforeTransactionStart + 1U); + + // Tests that stashing the transaction resources does not affect the open transactions counter. + { Lock::GlobalLock lk(opCtx(), MODE_IX, Date_t::now(), Lock::InterruptBehavior::kThrow); } + session.stashTransactionResources(opCtx()); + ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentOpen(), + beforeTransactionStart + 1U); + + session.unstashTransactionResources(opCtx(), "insert"); + + // Tests that committing a transaction decrements the open transactions counter by 1. + session.commitTransaction(opCtx()); + ASSERT_EQ(ServerTransactionsMetrics::get(opCtx())->getCurrentOpen(), beforeTransactionStart); +} + /** * Test fixture for transactions metrics. */ @@ -1382,4 +1442,4 @@ TEST_F(TransactionsMetricsTest, SingleTransactionStatsDurationShouldKeepIncreasi } } // namespace -} // namespace mongo +} // namespace mongo
\ No newline at end of file diff --git a/src/mongo/db/transactions_stats.idl b/src/mongo/db/transactions_stats.idl index 34b4b954c0f..3987ba0be8e 100644 --- a/src/mongo/db/transactions_stats.idl +++ b/src/mongo/db/transactions_stats.idl @@ -36,6 +36,9 @@ structs: retriedStatementsCount: type: long default: 0 + currentOpen: + type: long + default: 0 totalAborted: type: long default: 0 |