Skip to content

Commit

Permalink
Merge pull request #1409 from Expensify/main
Browse files Browse the repository at this point in the history
Update expensify_prod branch
  • Loading branch information
chiragsalian authored Dec 13, 2022
2 parents 6e63b34 + 7fb3eba commit 91b5349
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 7 deletions.
27 changes: 20 additions & 7 deletions sqlitecluster/SQLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ SQLite::~SQLite() {
}

bool SQLite::beginTransaction(TRANSACTION_TYPE type) {
_currentTransactionType = type;
if (type == TRANSACTION_TYPE::EXCLUSIVE) {
if (isSyncThread) {
// Blocking the sync thread has catastrophic results (forking) and so we either get this quickly, or we fail the transaction.
Expand Down Expand Up @@ -655,6 +656,21 @@ int SQLite::commit(const string& description, function<void()>* preCheckpointCal
_uncommittedQuery.clear();
_sharedData._commitLockTimer.stop();
_sharedData.commitLock.unlock();
if (_currentTransactionType == TRANSACTION_TYPE::EXCLUSIVE) {
// This is is basically duplicate code from below, but this is a quick fix tosee if this helps, we can de-dupe later if this works. It's only 6 lines.
_sharedData.checkpointInProgress.test_and_set();
auto start = STimeNow();
int framesCheckpointed = 0;

// SQLITE_CHECKPOINT_TRUNCATE is the "most aggressive" checkpoint setting. It clears the entire file and resets its size to 0.
sqlite3_wal_checkpoint_v2(_db, 0, SQLITE_CHECKPOINT_TRUNCATE, NULL, &framesCheckpointed);
auto end = STimeNow();
SINFO("Checkpointed " << framesCheckpointed << " (total) frames of " << _sharedData.outstandingFramesToCheckpoint << " in " << (end - start) << "us. EXCLUSIVE mode.");

// It might not actually be 0, but we'll just let sqlite tell us what it is next time _walHookCallback runs.
_sharedData.outstandingFramesToCheckpoint = 0;
_sharedData.checkpointInProgress.clear();
}
_mutexLocked = false;
_queryCache.clear();

Expand All @@ -663,16 +679,13 @@ int SQLite::commit(const string& description, function<void()>* preCheckpointCal
}

// If we are the first to set it (i.e., test_and_set returned `false` as the previous value), we'll start a checkpoint.
if (!_sharedData.checkpointInProgress.test_and_set()) {
size_t outstandingFrames = _sharedData.outstandingFramesToCheckpoint;
if (outstandingFrames) {
if ((_currentTransactionType != TRANSACTION_TYPE::EXCLUSIVE) && (!_sharedData.checkpointInProgress.test_and_set())) {
if (_sharedData.outstandingFramesToCheckpoint) {
auto start = STimeNow();
int totalFrames = 0;
int framesCheckpointed = 0;
sqlite3_wal_checkpoint_v2(_db, 0, SQLITE_CHECKPOINT_PASSIVE, &totalFrames, &framesCheckpointed);
sqlite3_wal_checkpoint_v2(_db, 0, SQLITE_CHECKPOINT_PASSIVE, NULL, &framesCheckpointed);
auto end = STimeNow();
SINFO("Checkpoint finished with " << (totalFrames - framesCheckpointed) << " frames remaining to checkpoint in " << (end - start) << "us. WAL hook had reported "
<< outstandingFrames << " frames to checkpoint.");
SINFO("Checkpointed " << framesCheckpointed << " (total) frames of " << _sharedData.outstandingFramesToCheckpoint << " in " << (end - start) << "us.");

// It might not actually be 0, but we'll just let sqlite tell us what it is next time _walHookCallback runs.
_sharedData.outstandingFramesToCheckpoint = 0;
Expand Down
3 changes: 3 additions & 0 deletions sqlitecluster/SQLite.h
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,9 @@ class SQLite {
// True when we have a transaction in progress.
bool _insideTransaction = false;

// Keep track of what type of transaction we've started, we want to do complete checkpoints at the end of exclusive transactions.
TRANSACTION_TYPE _currentTransactionType;

// The new query and new hash to add to the journal for a transaction that's nearing completion, before we commit
// it.
string _uncommittedQuery;
Expand Down

0 comments on commit 91b5349

Please sign in to comment.