Skip to content

Commit

Permalink
Merge pull request #1952 from Expensify/main
Browse files Browse the repository at this point in the history
Update expensify_prod branch
  • Loading branch information
chiragsalian authored Nov 11, 2024
2 parents be01efe + 0de2ad3 commit 2606f79
Show file tree
Hide file tree
Showing 3 changed files with 95 additions and 36 deletions.
105 changes: 79 additions & 26 deletions libstuff/sqlite3.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
** separate file. This file contains only code for the core SQLite library.
**
** The content in this amalgamation comes from Fossil check-in
** 492a0a85438586a8ccd9629239304f4df3f2.
** 3a45387a09398db8efdb2b6ecd773fa30970.
*/
#define SQLITE_CORE 1
#define SQLITE_AMALGAMATION 1
Expand Down Expand Up @@ -465,7 +465,7 @@ extern "C" {
*/
#define SQLITE_VERSION "3.47.0"
#define SQLITE_VERSION_NUMBER 3047000
#define SQLITE_SOURCE_ID "2024-11-01 15:25:34 492a0a85438586a8ccd9629239304f4df3f2c6391ec05dd9837b7a553313d632"
#define SQLITE_SOURCE_ID "2024-11-08 17:09:34 3a45387a09398db8efdb2b6ecd773fa30970e61e5242823e04a2e968d1c93c10"

/*
** CAPI3REF: Run-Time Library Version Numbers
Expand Down Expand Up @@ -17449,33 +17449,41 @@ SQLITE_PRIVATE int sqlite3CursorRangeHintExprCheck(Walker *pWalker, Expr *pExpr)
#define COMMIT_TIME_RELOCATE1COUNT 8
#define COMMIT_TIME_RELOCATE2COUNT 9

#define COMMIT_TIME_AFTER_FIXUNLOCKED 10
#define COMMIT_TIME_RELOCATE2_READUS 10
#define COMMIT_TIME_RELOCATE2_READCOUNT 11
#define COMMIT_TIME_RELOCATE2_ALLOCATEUS 12
#define COMMIT_TIME_RELOCATE2_RELOCATEUS 13

#define COMMIT_TIME_BEFORE_WALFRAMES 11
#define COMMIT_TIME_AFTER_CHANGECOUNTER 13
#define COMMIT_TIME_AFTER_RESTARTLOG 14
#define COMMIT_TIME_AFTER_WRITEHDR 15
#define COMMIT_TIME_AFTER_FIXUNLOCKED 14

#define COMMIT_TIME_OSWRITE 16
#define COMMIT_TIME_BEFORE_WALFRAMES 15
#define COMMIT_TIME_AFTER_CHANGECOUNTER 16
#define COMMIT_TIME_AFTER_RESTARTLOG 17
#define COMMIT_TIME_AFTER_WRITEHDR 18

#define COMMIT_TIME_AFTER_WRITEFRAMES 17
#define COMMIT_TIME_OSWRITE 19

#define COMMIT_TIME_BEFORE_WALINDEX 18
#define COMMIT_TIME_AFTER_WALINDEX 19
#define COMMIT_TIME_AFTER_WALINDEXHDR 20
#define COMMIT_TIME_AFTER_WRITEFRAMES 20

#define COMMIT_TIME_WALFRAMESFLAGS 21
#define COMMIT_TIME_NFRAME 21
#define COMMIT_TIME_HASHMAPUS 22

#define COMMIT_TIME_AFTER_WALFRAMES 22
#define COMMIT_TIME_BEFORE_WALINDEX 23
#define COMMIT_TIME_AFTER_WALINDEX 24
#define COMMIT_TIME_AFTER_WALINDEXHDR 25

#define COMMIT_TIME_BEFORE_PHASETWO 23
#define COMMIT_TIME_AFTER_PHASETWO 24
#define COMMIT_TIME_WALFRAMESFLAGS 26

#define COMMIT_TIME_AFTER_VDBECOMMIT 25
#define COMMIT_TIME_AFTER_HALT 26
#define COMMIT_TIME_FINISH 27
#define COMMIT_TIME_AFTER_WALFRAMES 27

#define COMMIT_TIME_N 28
#define COMMIT_TIME_BEFORE_PHASETWO 28
#define COMMIT_TIME_AFTER_PHASETWO 29

#define COMMIT_TIME_AFTER_VDBECOMMIT 30
#define COMMIT_TIME_AFTER_HALT 31
#define COMMIT_TIME_FINISH 32

#define COMMIT_TIME_N 33

/* #define COMMIT_TIME_TIMEOUT (2*1000*1000) */
#define COMMIT_TIME_TIMEOUT (10*1000) /* 10ms threshold */
Expand Down Expand Up @@ -60957,6 +60965,7 @@ static int pager_playback(Pager *pPager, int isHot){
static int readDbPage(PgHdr *pPg){
Pager *pPager = pPg->pPager; /* Pager object associated with page pPg */
int rc = SQLITE_OK; /* Return code */
u64 t1 = 0;

#ifndef SQLITE_OMIT_WAL
u32 iFrame = 0; /* Frame of WAL containing pgno */
Expand All @@ -60968,6 +60977,9 @@ static int readDbPage(PgHdr *pPg){
rc = sqlite3WalFindFrame(pPager->pWal, pPg->pgno, &iFrame);
if( rc ) return rc;
}
if( pPager->aCommitTime ){
t1 = sqlite3STimeNow();
}
if( iFrame ){
rc = sqlite3WalReadFrame(pPager->pWal, iFrame,pPager->pageSize,pPg->pData);
}else
Expand All @@ -60979,6 +60991,10 @@ static int readDbPage(PgHdr *pPg){
rc = SQLITE_OK;
}
}
if( pPager->aCommitTime ){
pPager->aCommitTime[COMMIT_TIME_RELOCATE2_READUS] += (sqlite3STimeNow() - t1);
pPager->aCommitTime[COMMIT_TIME_RELOCATE2_READCOUNT]++;
}

if( pPg->pgno==1 ){
if( rc ){
Expand Down Expand Up @@ -66966,7 +66982,13 @@ static int walIndexPage(
){
SEH_INJECT_FAULT;
if( pWal->nWiData<=iPage || (*ppPage = pWal->apWiData[iPage])==0 ){
return walIndexPageRealloc(pWal, iPage, ppPage);
int rc;
u64 t1;
if( pWal->aCommitTime ) t1 = sqlite3STimeNow();
rc = walIndexPageRealloc(pWal, iPage, ppPage);
if( pWal->aCommitTime ){
pWal->aCommitTime[COMMIT_TIME_HASHMAPUS] += sqlite3STimeNow() - t1;
}
}
return SQLITE_OK;
}
Expand Down Expand Up @@ -71082,6 +71104,7 @@ static int walFrames(
WalIndexHdr *pLive; /* Pointer to shared header */
int iApp;
int bWal2 = isWalMode2(pWal);
int nFrame = 0;

int logFlags = 0;

Expand Down Expand Up @@ -71297,13 +71320,15 @@ static int walFrames(
if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue;
iFrame++;
rc = walIndexAppend(pWal, iApp, iFrame, p->pgno);
nFrame++;
}
assert( pLast!=0 || nExtra==0 );
while( rc==SQLITE_OK && nExtra>0 ){
iFrame++;
nExtra--;
rc = walIndexAppend(pWal, iApp, iFrame, pLast->pgno);
}
if( pWal->aCommitTime ) pWal->aCommitTime[COMMIT_TIME_NFRAME] = nFrame;

sqlite3CommitTimeSet(pWal->aCommitTime, COMMIT_TIME_AFTER_WALINDEX);

Expand Down Expand Up @@ -77391,27 +77416,46 @@ static int btreeRelocateRange(

if( pEntry->eType==PTRMAP_FREEPAGE ){
Pgno dummy;
u64 t1;
if( pBt->aCommitTime ) t1 = sqlite3STimeNow();
rc = allocateBtreePage(pBt, &pFree, &dummy, iPg, BTALLOC_EXACT);
if( pBt->aCommitTime ){
pBt->aCommitTime[COMMIT_TIME_RELOCATE2_ALLOCATEUS] += (sqlite3STimeNow() - t1);
}
if( pFree ){
assert( sqlite3PagerPageRefcount(pFree->pDbPage)==1 );
sqlite3PcacheDrop(pFree->pDbPage);
}
assert( rc!=SQLITE_OK || dummy==iPg );
}else if( pnCurrent ){
u64 t1;
btreeGetPage(pBt, iPg, &pPg, 0);
assert( sqlite3PagerIswriteable(pPg->pDbPage) );
assert( sqlite3PagerPageRefcount(pPg->pDbPage)==1 );
iNew = ++(*pnCurrent);
if( iNew==PENDING_BYTE_PAGE(pBt) ) iNew = ++(*pnCurrent);
if( pBt->aCommitTime ) t1 = sqlite3STimeNow();
rc = relocatePage(pBt, pPg, pEntry->eType, pEntry->parent, iNew, 1);
if( pBt->aCommitTime ){
pBt->aCommitTime[COMMIT_TIME_RELOCATE2_RELOCATEUS] += (sqlite3STimeNow() - t1);
}
releasePageNotNull(pPg);
}else{
u64 t1;
if( pBt->aCommitTime ) t1 = sqlite3STimeNow();
rc = allocateBtreePage(pBt, &pFree, &iNew, iFirst-1, BTALLOC_LE);
if( pBt->aCommitTime ){
pBt->aCommitTime[COMMIT_TIME_RELOCATE2_ALLOCATEUS] += (sqlite3STimeNow() - t1);
}
assert( rc!=SQLITE_OK || iNew<iFirst );
if( rc==SQLITE_OK ){
releasePage(pFree);
btreeGetPage(pBt, iPg, &pPg, 0);
if( pBt->aCommitTime ) t1 = sqlite3STimeNow();
rc = relocatePage(pBt, pPg, pEntry->eType, pEntry->parent,iNew,1);
if( pBt->aCommitTime ){
pBt->aCommitTime[COMMIT_TIME_RELOCATE2_RELOCATEUS] += (sqlite3STimeNow() - t1);
}
releasePage(pPg);
}
}
Expand Down Expand Up @@ -77514,7 +77558,9 @@ static int btreeFixUnlocked(Btree *p){
sqlite3CommitTimeSet(
p->pBt->aCommitTime, COMMIT_TIME_START_RELOCATE2
);
sqlite3PagerSetCommitTime(pBt->pPager, pBt->aCommitTime);
rc = btreeRelocateRange(pBt, nFin+1, nCurrent, 0);
sqlite3PagerSetCommitTime(pBt->pPager, 0);
}

put4byte(&p1[28], nFin);
Expand Down Expand Up @@ -93269,10 +93315,17 @@ SQLITE_PRIVATE void sqlite3CommitTimeLog(u64 *aCommit){
if( ii==COMMIT_TIME_RELOCATE1COUNT
|| ii==COMMIT_TIME_RELOCATE2COUNT
|| ii==COMMIT_TIME_OTHERWRITERS
|| ii==COMMIT_TIME_NFRAME
|| ii==COMMIT_TIME_RELOCATE2_READCOUNT
){
iVal = (int)aCommit[ii];
zHash = "#";
}else if( ii==COMMIT_TIME_OSWRITE ){
}else if( ii==COMMIT_TIME_OSWRITE
|| ii==COMMIT_TIME_RELOCATE2_READUS
|| ii==COMMIT_TIME_RELOCATE2_ALLOCATEUS
|| ii==COMMIT_TIME_RELOCATE2_RELOCATEUS
|| ii==COMMIT_TIME_HASHMAPUS
){
iVal = (int)aCommit[ii];
zU = "us";
}else if( ii==COMMIT_TIME_WALFRAMESFLAGS ){
Expand All @@ -93283,7 +93336,7 @@ SQLITE_PRIVATE void sqlite3CommitTimeLog(u64 *aCommit){
}
zStr = sqlite3_mprintf("%z%s%s%d%s", zStr, (zStr?", ":""),zHash,iVal,zU);
}
sqlite3_log(SQLITE_WARNING, "slow commit (v=8): (%s)", zStr);
sqlite3_log(SQLITE_WARNING, "slow commit (v=9): (%s)", zStr);
sqlite3_free(zStr);
}
}
Expand Down Expand Up @@ -93311,7 +93364,7 @@ SQLITE_PRIVATE void sqlite3PrepareTimeLog(const char *zSql, int nSql, u64 *aPrep
}
if( nByte<0 ){ nByte = sqlite3Strlen30(zSql); }
sqlite3_log(SQLITE_WARNING,
"slow prepare (v=8): (%s) [%.*s]", zStr, nByte, zSql
"slow prepare (v=9): (%s) [%.*s]", zStr, nByte, zSql
);
sqlite3_free(zStr);
}
Expand All @@ -93327,7 +93380,7 @@ SQLITE_PRIVATE void sqlite3SchemaTimeLog(u64 *aSchema){
(aSchema[ii]==0 ? 0 : (int)(aSchema[ii] - i1))
);
}
sqlite3_log(SQLITE_WARNING, "slow schema (v=8): (%s)", zStr);
sqlite3_log(SQLITE_WARNING, "slow schema (v=9): (%s)", zStr);
sqlite3_free(zStr);
}
}
Expand Down Expand Up @@ -257705,7 +257758,7 @@ static void fts5SourceIdFunc(
){
assert( nArg==0 );
UNUSED_PARAM2(nArg, apUnused);
sqlite3_result_text(pCtx, "fts5: 2024-11-01 15:25:34 492a0a85438586a8ccd9629239304f4df3f2c6391ec05dd9837b7a553313d632", -1, SQLITE_TRANSIENT);
sqlite3_result_text(pCtx, "fts5: 2024-11-08 17:09:34 3a45387a09398db8efdb2b6ecd773fa30970e61e5242823e04a2e968d1c93c10", -1, SQLITE_TRANSIENT);
}

/*
Expand Down
2 changes: 1 addition & 1 deletion libstuff/sqlite3.h
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ extern "C" {
*/
#define SQLITE_VERSION "3.47.0"
#define SQLITE_VERSION_NUMBER 3047000
#define SQLITE_SOURCE_ID "2024-11-01 15:25:34 492a0a85438586a8ccd9629239304f4df3f2c6391ec05dd9837b7a553313d632"
#define SQLITE_SOURCE_ID "2024-11-08 17:09:34 3a45387a09398db8efdb2b6ecd773fa30970e61e5242823e04a2e968d1c93c10"

/*
** CAPI3REF: Run-Time Library Version Numbers
Expand Down
24 changes: 15 additions & 9 deletions sqlitecluster/SQLite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -386,16 +386,22 @@ bool SQLite::beginTransaction(TRANSACTION_TYPE type) {
_mutexLocked = true;
}

if (_insideTransaction || !_uncommittedHash.empty() || !_uncommittedQuery.empty()) {
// The most likely case for hitting this is that we forgot to roll back a transaction when we were finished with it
// during the last use of this DB handle. In that case, `_insideTransaction` is likely true, and possibly
// _uncommittedHash or _uncommittedQuery is set. Rollback should put this DB handle back into a usable state,
// but it breaks the current transaction on this handle. We throw and fail the one transaction and hopefully have
// fixed the handle for the next use.
// The most likely case for hitting this is that we forgot to roll back a transaction when we were finished with it
// during the last use of this DB handle. In that case, `_insideTransaction` is likely true, and possibly
// _uncommittedHash or _uncommittedQuery is set. Rollback should put this DB handle back into a usable state,
// but it breaks the current transaction on this handle. We throw and fail the one transaction and hopefully have
// fixed the handle for the next use
if (_insideTransaction) {
rollback();
STHROW("Attempted to begin transaction while in invalid state: already inside transaction");
}
if (!_uncommittedHash.empty()) {
rollback();
STHROW("Attempted to begin transaction while in invalid state: _uncommittedHash not empty");
}
if (!_uncommittedQuery.empty()) {
rollback();
STHROW("Attempted to begin transaction while in invalid state. _insideTransaction="s +
(_insideTransaction ? "true" : "false") + ", _uncommittedHash='" + _uncommittedHash +
", _uncommittedQuery empty? " + (_uncommittedQuery.empty() ? "true" : "false"));
STHROW("Attempted to begin transaction while in invalid state: _uncommittedQuery not empty");
}

// Reset before the query, as it's possible the query sets these.
Expand Down

0 comments on commit 2606f79

Please sign in to comment.