From a7df55eafb3a04e9f9fbf14defbfea249247fe1f Mon Sep 17 00:00:00 2001 From: John Lee Date: Tue, 10 Dec 2024 13:58:47 -0500 Subject: [PATCH] No need for lock --- sqlitecluster/SQLite.cpp | 17 ++++------------- sqlitecluster/SQLite.h | 4 +--- 2 files changed, 5 insertions(+), 16 deletions(-) diff --git a/sqlitecluster/SQLite.cpp b/sqlitecluster/SQLite.cpp index fcac40b00..e0ded6539 100644 --- a/sqlitecluster/SQLite.cpp +++ b/sqlitecluster/SQLite.cpp @@ -405,7 +405,8 @@ bool SQLite::beginTransaction(TRANSACTION_TYPE type) { uint64_t before = STimeNow(); _insideTransaction = !SQuery(_db, "starting db transaction", "BEGIN CONCURRENT"); - _sharedData.incrementOpenTransactions(); + // We actively track transaction counts incrementing and decrementing to log the number of active open transactions at any given moment. + _sharedData.openTransactionCount++; // Because some other thread could commit once we've run `BEGIN CONCURRENT`, this value can be slightly behind // where we're actually able to start such that we know we shouldn't get a conflict if this commits successfully on @@ -790,7 +791,7 @@ int SQLite::commit(const string& description, function* preCheckpointCal _mutexLocked = false; _queryCache.clear(); - _sharedData.decrementOpenTransactions(); + _sharedData.openTransactionCount--; if (preCheckpointCallback != nullptr) { (*preCheckpointCallback)(); @@ -847,7 +848,7 @@ void SQLite::rollback() { _rollbackElapsed += STimeNow() - before; } - _sharedData.decrementOpenTransactions(); + _sharedData.openTransactionCount--; // Finally done with this. _insideTransaction = false; @@ -1208,16 +1209,6 @@ void SQLite::SharedData::commitTransactionInfo(uint64_t commitID) { _committedTransactions.insert(_preparedTransactions.extract(commitID)); } -void SQLite::SharedData::incrementOpenTransactions() { - lock_guard lock(_internalStateMutex); - openTransactionCount++; -} - -void SQLite::SharedData::decrementOpenTransactions() { - lock_guard lock(_internalStateMutex); - openTransactionCount--; -} - map> SQLite::SharedData::popCommittedTransactions() { lock_guard lock(_internalStateMutex); decltype(_committedTransactions) result; diff --git a/sqlitecluster/SQLite.h b/sqlitecluster/SQLite.h index c33a3c63f..f88b9d3c7 100644 --- a/sqlitecluster/SQLite.h +++ b/sqlitecluster/SQLite.h @@ -314,9 +314,7 @@ class SQLite { // If set to false, this prevents any thread from being able to commit to the DB. atomic _commitEnabled; - // These blocks are to monitor the number of open transactions on the whole server. - void incrementOpenTransactions(); - void decrementOpenTransactions(); + // This variable is used to monitor the number of open transactions on the whole server. atomic openTransactionCount; SPerformanceTimer _commitLockTimer;