Skip to content

Commit

Permalink
Merge pull request #1886 from Expensify/tyler-release-db-on-escalate
Browse files Browse the repository at this point in the history
Release DB handle on follower when escalating command
  • Loading branch information
danieldoglas authored Oct 8, 2024
2 parents 745389d + 89a3505 commit 8bf3a4b
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 2 deletions.
1 change: 1 addition & 0 deletions BedrockServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -953,6 +953,7 @@ void BedrockServer::runCommand(unique_ptr<BedrockCommand>&& _command, bool isBlo
if (!canWriteParallel) {
// Roll back the transaction, it'll get re-run in the sync thread.
core.rollback();
dbScope.release();
auto _clusterMessengerCopy = _clusterMessenger;
if (getState() == SQLiteNodeState::LEADING) {
// Limit the command timeout to 20s to avoid blocking the sync thread long enough to cause the cluster to give up and elect a new leader (causing a fork), which happens
Expand Down
11 changes: 9 additions & 2 deletions sqlitecluster/SQLitePool.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,18 @@ void SQLitePool::returnToPool(size_t index) {
_wait.notify_one();
}

SQLiteScopedHandle::SQLiteScopedHandle(SQLitePool& pool, size_t index) : _pool(pool), _index(index)
SQLiteScopedHandle::SQLiteScopedHandle(SQLitePool& pool, size_t index) : _pool(pool), _index(index), _released(false)
{}

void SQLiteScopedHandle::release() {
if (!_released) {
_pool.returnToPool(_index);
_released = true;
}
}

SQLiteScopedHandle::~SQLiteScopedHandle() {
_pool.returnToPool(_index);
release();
}

SQLite& SQLiteScopedHandle::db() {
Expand Down
2 changes: 2 additions & 0 deletions sqlitecluster/SQLitePool.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,10 @@ class SQLiteScopedHandle {
SQLiteScopedHandle(SQLitePool& pool, size_t index);
~SQLiteScopedHandle();
SQLite& db();
void release();

private:
SQLitePool& _pool;
size_t _index;
bool _released;
};

0 comments on commit 8bf3a4b

Please sign in to comment.