Skip to content

Commit

Permalink
No need for lock
Browse files Browse the repository at this point in the history
  • Loading branch information
johnmlee101 committed Dec 10, 2024
1 parent 5211d06 commit a7df55e
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 16 deletions.
17 changes: 4 additions & 13 deletions sqlitecluster/SQLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -790,7 +791,7 @@ int SQLite::commit(const string& description, function<void()>* preCheckpointCal
_mutexLocked = false;
_queryCache.clear();

_sharedData.decrementOpenTransactions();
_sharedData.openTransactionCount--;

if (preCheckpointCallback != nullptr) {
(*preCheckpointCallback)();
Expand Down Expand Up @@ -847,7 +848,7 @@ void SQLite::rollback() {
_rollbackElapsed += STimeNow() - before;
}

_sharedData.decrementOpenTransactions();
_sharedData.openTransactionCount--;

// Finally done with this.
_insideTransaction = false;
Expand Down Expand Up @@ -1208,16 +1209,6 @@ void SQLite::SharedData::commitTransactionInfo(uint64_t commitID) {
_committedTransactions.insert(_preparedTransactions.extract(commitID));
}

void SQLite::SharedData::incrementOpenTransactions() {
lock_guard<decltype(_internalStateMutex)> lock(_internalStateMutex);
openTransactionCount++;
}

void SQLite::SharedData::decrementOpenTransactions() {
lock_guard<decltype(_internalStateMutex)> lock(_internalStateMutex);
openTransactionCount--;
}

map<uint64_t, tuple<string, string, uint64_t>> SQLite::SharedData::popCommittedTransactions() {
lock_guard<decltype(_internalStateMutex)> lock(_internalStateMutex);
decltype(_committedTransactions) result;
Expand Down
4 changes: 1 addition & 3 deletions sqlitecluster/SQLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,9 +314,7 @@ class SQLite {
// If set to false, this prevents any thread from being able to commit to the DB.
atomic<bool> _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<int64_t> openTransactionCount;

SPerformanceTimer _commitLockTimer;
Expand Down

0 comments on commit a7df55e

Please sign in to comment.