From 055cc1e431811d6e87eea672acd2e96e95138e07 Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 4 Oct 2017 20:57:14 +0000 Subject: [PATCH 01/51] Add experimental mode that uses two wal files. Activated using "PRAGMA journal_mode = wal2". FossilOrigin-Name: e2fc5c814cf6862d536aacb9eca66ecd31ba7e3e3033fa4c5564d533f4a18dfc --- manifest | 41 +- manifest.uuid | 2 +- src/btree.c | 10 +- src/os_unix.c | 4 +- src/pager.c | 47 +- src/pager.h | 19 +- src/pragma.c | 3 +- src/vdbe.c | 62 +- src/wal.c | 1567 +++++++++++++++++++++++++++------------- src/wal.h | 4 +- test/permutations.test | 17 + test/savepoint.test | 1 + test/savepoint6.test | 17 +- test/tester.tcl | 24 +- test/waltwo2.test | 127 ++++ 15 files changed, 1363 insertions(+), 582 deletions(-) create mode 100644 test/waltwo2.test diff --git a/manifest b/manifest index 1ea0bfb8d7..f6dbc867fd 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Updates\sto\srequirements\smarks. -D 2017-10-04T18:26:44.610 +C Add\sexperimental\smode\sthat\suses\stwo\swal\sfiles.\sActivated\susing\s"PRAGMA\njournal_mode\s=\swal2". +D 2017-10-04T20:57:14.949 F Makefile.in 4bc36d913c2e3e2d326d588d72f618ac9788b2fd4b7efda61102611a6495c3ff F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 6033b51b6aea702ea059f6ab2d47b1d3cef648695f787247dd4fb395fe60673f @@ -401,7 +401,7 @@ F src/auth.c 6277d63837357549fe14e723490d6dc1a38768d71c795c5eb5c0f8a99f918f73 F src/backup.c faf17e60b43233c214aae6a8179d24503a61e83b F src/bitvec.c 17ea48eff8ba979f1f5b04cc484c7bb2be632f33 F src/btmutex.c 0e9ce2d56159b89b9bc8e197e023ee11e39ff8ca -F src/btree.c cc88a7fca7287dfc004921bb5e2764893dfe4f6dd33be3570126b3fc37932600 +F src/btree.c c7d94975b015058e6a68278e53d841585552bbc541c1b31b6e024eb9fd888746 F src/btree.h 32ef5d3f25dc70ef1ee9cecf84a023c21378f06a57cd701d2e866e141b150f09 F src/btreeInt.h 55b702efce17e5d1941865464227d3802cfc9c7c832fac81d4c94dced47a71fc F src/build.c e71e96a67daf3d1dd23188423e66cd6af38017e2ec73fead5d2b57da2d3c7e16 @@ -442,16 +442,16 @@ F src/os.c 93e0979b9b55df29c0c4923f73b48e9d3fe728f01dd8ed4f6a9d2f1d79779bc8 F src/os.h 8e976e59eb4ca1c0fca6d35ee803e38951cb0343 F src/os_common.h b2f4707a603e36811d9b1a13278bffd757857b85 F src/os_setup.h 0dbaea40a7d36bf311613d31342e0b99e2536586 -F src/os_unix.c 3984fc069df59e26f000e30609611cecdb4e93293e6ee52313a473a7e874af1b +F src/os_unix.c 1f9c3e771557edd248e1fcec0818739826b2a3121b609a6e3372b670236d085a F src/os_win.c 6892c3ff23b7886577e47f13d827ca220c0831bae3ce00eea8c258352692f8c6 F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a -F src/pager.c 2aa56a99bb13128d9102e84c7a9f835e546cbb58f0861d481bc3db32973b1628 -F src/pager.h 581698f2177e8bd4008fe4760898ce20b6133d1df22139b9101b5155f900df7a +F src/pager.c bc2d2b6784cc2b5103e07aff13b012dab30c7efa0abb9bc2bb949ec45cba4326 +F src/pager.h e11e516208a460bea1b95fe3da697642306e7f350d5f96d2c1d21231ee4d2bf2 F src/parse.y 52ef3cecd0934e9da4a45b585883a03243ad615d338ad94f44501a05891dcdfa F src/pcache.c 4bada070456980c3c1f16d58ec2e64e389ad77b935e3d77e0c96e7bbd397289c F src/pcache.h 072f94d29281cffd99e46c1539849f248c4b56ae7684c1f36626797fee375170 F src/pcache1.c 716975564c15eb6679e97f734cec1bfd6c16ac3d4010f05f1f8e509fc7d19880 -F src/pragma.c d04725ac25387d9638919e197fb009f378e13af7bf899516979e54b3164e3602 +F src/pragma.c 49a04b2ec3199b7967c19b3239182d9c4e860726909ed80b7a3e21fe5bb9e6c4 F src/pragma.h bb83728944b42f6d409c77f5838a8edbdb0fe83046c5496ffc9602b40340a324 F src/prepare.c 9a141a1b02dca53beaa9771699d390aafcac01f5d1f1c0ae6e23ded8dcdb709a F src/printf.c 40aee47ae9be4bd3dbdc8968bd07fddc027be8edec8daddf24d3391d36698a1c @@ -526,7 +526,7 @@ F src/update.c 5404be9e840717323a69209190cdbc9d0d34adaedaaf1d1a1069babf2c4171c0 F src/utf.c 810fbfebe12359f10bc2a011520a6e10879ab2a163bcb26c74768eab82ea62a5 F src/util.c 5168013cfd937a695d23cce8c67cb07a3dda242d4cb812530ba1148b88e0f159 F src/vacuum.c 90839322fd5f00df9617eb21b68beda9b6e2a2937576b0d65985e4aeb1c53739 -F src/vdbe.c 176c0897af0aedecd3abc9afaf7fa80eaa7cf5eaf62583de256a9961df474373 +F src/vdbe.c c3c3fe95d98df8757570fb265236cfdf31536d1025bf37c232237d1e422ae39b F src/vdbe.h d50cadf12bcf9fb99117ef392ce1ea283aa429270481426b6e8b0280c101fd97 F src/vdbeInt.h 1fe00770144c12c4913128f35262d11527ef3284561baaab59b947a41c08d0d9 F src/vdbeapi.c 9c670ca0dcc1cd86373aa353b747b26fe531ca5cd4331690c611d1f03842e2a1 @@ -537,8 +537,8 @@ F src/vdbesort.c 731a09e5cb9e96b70c394c1b7cf3860fbe84acca7682e178615eb941a3a0ef2 F src/vdbetrace.c 48e11ebe040c6b41d146abed2602e3d00d621d7ebe4eb29b0a0f1617fd3c2f6c F src/vtab.c 0e4885495172e1bdf54b12cce23b395ac74ef5729031f15e1bc1e3e6b360ed1a F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 839db09792fead5052bb35e533fa485e134913d547d05b5f42e537b73e63f07a -F src/wal.h 8de5d2d3de0956d6f6cb48c83a4012d5f227b8fe940f3a349a4b7e85ebcb492a +F src/wal.c 5ca528539a69edd6333dcab1d49e89d4f98efb6a23f0fda85bc52c4ec313db49 +F src/wal.h b6063e6be1b03389372f3f32240e99b8ab92c32cdd05aa0e31b30a21e4e41654 F src/walker.c 3ccfa8637f95355bff61144e01a615b8ef26f79c312880848da73f03367da1e6 F src/where.c 049522adcf5426f1a8c3ed07be15e1ffa3266afd34e8e7bee64b63e2fbfad0b5 F src/whereInt.h 82c04c5075308abbac59180c8bad5ecb45b07453981f60a53f3c7dee21e1e971 @@ -1097,7 +1097,7 @@ F test/parser1.test 391b9bf9a229547a129c61ac345ed1a6f5eb1854 F test/pcache.test c8acbedd3b6fd0f9a7ca887a83b11d24a007972b F test/pcache2.test af7f3deb1a819f77a6d0d81534e97d1cf62cd442 F test/percentile.test 4243af26b8f3f4555abe166f723715a1f74c77ff -F test/permutations.test d911c9ba49088d22054a05dc73743f677872a92ac89288bcdeafa0ebf3f9c531 +F test/permutations.test 5fe80f417441ed6a69acd77f5190305dbf255cc2be214f8e36df4715c4f63f08 F test/pragma.test c31b5e98998c160a4c85b1e04f590655c67f2daa7f73854640cd120610e3ac15 F test/pragma2.test e5d5c176360c321344249354c0c16aec46214c9f F test/pragma3.test 14c12bc5352b1e100e0b6b44f371053a81ccf8ed @@ -1142,11 +1142,11 @@ F test/rowvalue9.test d8dd2c6ecac432dadaa79e41dc2434f007be1b6b F test/rowvaluefault.test 7cd9ccc6c2fbdd881672984087aad0491bb75504 F test/rtree.test 0c8d9dd458d6824e59683c19ab2ffa9ef946f798 F test/run-wordcount.sh 891e89c4c2d16e629cd45951d4ed899ad12afc09 -F test/savepoint.test 1f8a6b1aea9a0d05837adc463d4bf47bd9d0f1c842f1c2a9caccd639baf34bf9 +F test/savepoint.test 69c56b891ce0ff28f1376b5516bf2b6a8b39d0430433216bfc496e72103baaa5 F test/savepoint2.test 9b8543940572a2f01a18298c3135ad0c9f4f67d7 F test/savepoint4.test c8f8159ade6d2acd9128be61e1230f1c1edc6cc0 F test/savepoint5.test 0735db177e0ebbaedc39812c8d065075d563c4fd -F test/savepoint6.test f41279c5e137139fa5c21485773332c7adb98cd7 +F test/savepoint6.test 48a645a7bb3a59a6fcf06a7364cfe5b655c336760de39068f7c241b0fc80d963 F test/savepoint7.test cde525ea3075283eb950cdcdefe23ead4f700daa F test/savepointfault.test f044eac64b59f09746c7020ee261734de82bf9b2 F test/scanstatus.test 5253c219e331318a437f436268e0e82345700285 @@ -1261,7 +1261,7 @@ F test/temptable.test d2c9b87a54147161bcd1822e30c1d1cd891e5b30 F test/temptable2.test cd396beb41117a5302fff61767c35fa4270a0d5e F test/temptable3.test d11a0974e52b347e45ee54ef1923c91ed91e4637 F test/temptrigger.test 38f0ca479b1822d3117069e014daabcaacefffcc -F test/tester.tcl 9948bd856ce8a1c127f2f7900365387a42a917ce0dc87185bdd128fa5b11aff2 +F test/tester.tcl 71d30287dd22aae0eb9b07e62574336ae6a57e7a50e5dab320a604cfcca4b173 F test/thread001.test 9f22fd3525a307ff42a326b6bc7b0465be1745a5 F test/thread002.test e630504f8a06c00bf8bbe68528774dd96aeb2e58 F test/thread003.test ee4c9efc3b86a6a2767516a37bd64251272560a7 @@ -1520,6 +1520,7 @@ F test/walro.test 4ab7ac01b77c2f894235c699d59e3e3c7f15a160 F test/walshared.test 0befc811dcf0b287efae21612304d15576e35417 F test/walslow.test c05c68d4dc2700a982f89133ce103a1a84cc285f F test/walthread.test de8dbaf6d9e41481c460ba31ca61e163d7348f8e +F test/waltwo2.test 6e4d36500a20ff2d19761cf0e9a5d178e83d1798feda157ebc0681e01a35e56e F test/where.test f0c325563acde44f2c4ea6ba348e9e29f7121757 F test/where2.test 478d2170637b9211f593120648858593bf2445a1 F test/where3.test 54cdeb02157acc979de41530b804ae7b09552bf1 @@ -1655,7 +1656,11 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 7c69f8f1089c3e3843fbf7ec37a897c849a3df822a4ce3b4fcde586adf991a3f -R 61b14913c42f4f709d3e3a3845316f97 -U drh -Z 37903880ad327c432f24949839963d82 +P 40964a4ef7565ea0ddf452f48cb22373d068528e07d40eefc008f2231c969422 +R 07b0a272ae21af8e4cb674b6ac44fa89 +T *branch * wal2 +T *sym-wal2 * +T +closed f04ded1d9b40d54463162264e37e6d92411d09427eea592ef05681035e2f2e64 +T -sym-trunk * +U dan +Z 07422baaaff21d5f15df6853d7e92b06 diff --git a/manifest.uuid b/manifest.uuid index c2c8cc6040..092af1e9bc 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -40964a4ef7565ea0ddf452f48cb22373d068528e07d40eefc008f2231c969422 \ No newline at end of file +e2fc5c814cf6862d536aacb9eca66ecd31ba7e3e3033fa4c5564d533f4a18dfc \ No newline at end of file diff --git a/src/btree.c b/src/btree.c index cadd711e45..b221b7cf9c 100644 --- a/src/btree.c +++ b/src/btree.c @@ -2993,10 +2993,10 @@ static int lockBtree(BtShared *pBt){ goto page1_init_failed; } #else - if( page1[18]>2 ){ + if( page1[18]>3 ){ pBt->btsFlags |= BTS_READ_ONLY; } - if( page1[19]>2 ){ + if( page1[19]>3 ){ goto page1_init_failed; } @@ -3008,9 +3008,9 @@ static int lockBtree(BtShared *pBt){ ** may not be the latest version - there may be a newer one in the log ** file. */ - if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){ + if( page1[19]>=2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){ int isOpen = 0; - rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen); + rc = sqlite3PagerOpenWal(pBt->pPager, (page1[19]==3), &isOpen); if( rc!=SQLITE_OK ){ goto page1_init_failed; }else{ @@ -9866,7 +9866,7 @@ int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){ BtShared *pBt = pBtree->pBt; int rc; /* Return code */ - assert( iVersion==1 || iVersion==2 ); + assert( iVersion==1 || iVersion==2 || iVersion==3 ); /* If setting the version fields to 1, do not automatically open the ** WAL connection, even if the version fields are currently set to 2. diff --git a/src/os_unix.c b/src/os_unix.c index 4445104dd6..19ac47263f 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -4163,7 +4163,7 @@ static int unixShmSystemLock( assert( sqlite3_mutex_held(pShmNode->mutex) || pShmNode->nRef==0 ); /* Shared locks never span more than one byte */ - assert( n==1 || lockType!=F_RDLCK ); + /* assert( n==1 || lockType!=F_RDLCK ); */ /* Locks are within range */ assert( n>=1 && n<=SQLITE_SHM_NLOCK ); @@ -4609,7 +4609,7 @@ static int unixShmLock( || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); - assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); + /* assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); */ assert( pShmNode->h>=0 || pDbFd->pInode->bProcessLock==1 ); assert( pShmNode->h<0 || pDbFd->pInode->bProcessLock==0 ); diff --git a/src/pager.c b/src/pager.c index a43614cdb4..5a2b01f77a 100644 --- a/src/pager.c +++ b/src/pager.c @@ -810,20 +810,6 @@ static const unsigned char aJournalMagic[] = { */ #define PAGER_MAX_PGNO 2147483647 -/* -** The argument to this macro is a file descriptor (type sqlite3_file*). -** Return 0 if it is not open, or non-zero (but not 1) if it is. -** -** This is so that expressions can be written as: -** -** if( isOpen(pPager->jfd) ){ ... -** -** instead of -** -** if( pPager->jfd->pMethods ){ ... -*/ -#define isOpen(pFd) ((pFd)->pMethods!=0) - /* ** Return true if this pager uses a write-ahead log to read page pgno. ** Return false if the pager reads pgno directly from the database. @@ -944,6 +930,7 @@ static int assert_pager_state(Pager *p){ assert( isOpen(p->jfd) || p->journalMode==PAGER_JOURNALMODE_OFF || p->journalMode==PAGER_JOURNALMODE_WAL + || p->journalMode==PAGER_JOURNALMODE_WAL2 ); } assert( pPager->dbOrigSize==pPager->dbFileSize ); @@ -958,6 +945,7 @@ static int assert_pager_state(Pager *p){ assert( isOpen(p->jfd) || p->journalMode==PAGER_JOURNALMODE_OFF || p->journalMode==PAGER_JOURNALMODE_WAL + || p->journalMode==PAGER_JOURNALMODE_WAL2 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC) ); assert( pPager->dbOrigSize<=pPager->dbHintSize ); @@ -970,6 +958,7 @@ static int assert_pager_state(Pager *p){ assert( isOpen(p->jfd) || p->journalMode==PAGER_JOURNALMODE_OFF || p->journalMode==PAGER_JOURNALMODE_WAL + || p->journalMode==PAGER_JOURNALMODE_WAL2 || (sqlite3OsDeviceCharacteristics(p->fd)&SQLITE_IOCAP_BATCH_ATOMIC) ); break; @@ -2063,7 +2052,7 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ } pPager->journalOff = 0; }else if( pPager->journalMode==PAGER_JOURNALMODE_PERSIST - || (pPager->exclusiveMode && pPager->journalMode!=PAGER_JOURNALMODE_WAL) + || (pPager->exclusiveMode && pPager->journalModetempFile); pPager->journalOff = 0; @@ -2077,7 +2066,8 @@ static int pager_end_transaction(Pager *pPager, int hasMaster, int bCommit){ assert( sqlite3JournalIsInMemory(pPager->jfd)==0 ); assert( pPager->journalMode==PAGER_JOURNALMODE_DELETE || pPager->journalMode==PAGER_JOURNALMODE_MEMORY - || pPager->journalMode==PAGER_JOURNALMODE_WAL + || pPager->journalMode==PAGER_JOURNALMODE_WAL + || pPager->journalMode==PAGER_JOURNALMODE_WAL2 ); sqlite3OsClose(pPager->jfd); if( bDelete ){ @@ -3344,9 +3334,9 @@ static int pagerOpenWalIfPresent(Pager *pPager){ rc = sqlite3OsDelete(pPager->pVfs, pPager->zWal, 0); }else{ testcase( sqlite3PcachePagecount(pPager->pPCache)==0 ); - rc = sqlite3PagerOpenWal(pPager, 0); + rc = sqlite3PagerOpenWal(pPager, 0, 0); } - }else if( pPager->journalMode==PAGER_JOURNALMODE_WAL ){ + }else if( pPager->journalMode>=PAGER_JOURNALMODE_WAL ){ pPager->journalMode = PAGER_JOURNALMODE_DELETE; } } @@ -7245,6 +7235,7 @@ int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){ || eMode==PAGER_JOURNALMODE_PERSIST || eMode==PAGER_JOURNALMODE_OFF || eMode==PAGER_JOURNALMODE_WAL + || eMode==PAGER_JOURNALMODE_WAL2 || eMode==PAGER_JOURNALMODE_MEMORY ); /* This routine is only called from the OP_JournalMode opcode, and @@ -7279,9 +7270,12 @@ int sqlite3PagerSetJournalMode(Pager *pPager, int eMode){ assert( (PAGER_JOURNALMODE_MEMORY & 5)==4 ); assert( (PAGER_JOURNALMODE_OFF & 5)==0 ); assert( (PAGER_JOURNALMODE_WAL & 5)==5 ); + assert( (PAGER_JOURNALMODE_WAL2 & 5)==4 ); assert( isOpen(pPager->fd) || pPager->exclusiveMode ); - if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 ){ + if( !pPager->exclusiveMode && (eOld & 5)==1 && (eMode & 1)==0 + && eMode!=PAGER_JOURNALMODE_WAL2 /* TODO: fix this if possible */ + ){ /* In this case we would like to delete the journal file. If it is ** not possible, then that is not a problem. Deleting the journal file @@ -7443,7 +7437,7 @@ static int pagerExclusiveLock(Pager *pPager){ ** lock on the database file and use heap-memory to store the wal-index ** in. Otherwise, use the normal shared-memory. */ -static int pagerOpenWal(Pager *pPager){ +static int pagerOpenWal(Pager *pPager, int bWal2){ int rc = SQLITE_OK; assert( pPager->pWal==0 && pPager->tempFile==0 ); @@ -7464,7 +7458,7 @@ static int pagerOpenWal(Pager *pPager){ if( rc==SQLITE_OK ){ rc = sqlite3WalOpen(pPager->pVfs, pPager->fd, pPager->zWal, pPager->exclusiveMode, - pPager->journalSizeLimit, &pPager->pWal + pPager->journalSizeLimit, bWal2, &pPager->pWal ); } pagerFixMaplimit(pPager); @@ -7490,6 +7484,7 @@ static int pagerOpenWal(Pager *pPager){ */ int sqlite3PagerOpenWal( Pager *pPager, /* Pager object */ + int bWal2, /* Open in wal2 mode if not already open */ int *pbOpen /* OUT: Set to true if call is a no-op */ ){ int rc = SQLITE_OK; /* Return code */ @@ -7506,9 +7501,9 @@ int sqlite3PagerOpenWal( /* Close any rollback journal previously open */ sqlite3OsClose(pPager->jfd); - rc = pagerOpenWal(pPager); + rc = pagerOpenWal(pPager, bWal2); if( rc==SQLITE_OK ){ - pPager->journalMode = PAGER_JOURNALMODE_WAL; + pPager->journalMode = bWal2?PAGER_JOURNALMODE_WAL2:PAGER_JOURNALMODE_WAL; pPager->eState = PAGER_OPEN; } }else{ @@ -7530,7 +7525,9 @@ int sqlite3PagerOpenWal( int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){ int rc = SQLITE_OK; - assert( pPager->journalMode==PAGER_JOURNALMODE_WAL ); + assert( pPager->journalMode==PAGER_JOURNALMODE_WAL + || pPager->journalMode==PAGER_JOURNALMODE_WAL2 + ); /* If the log file is not already open, but does exist in the file-system, ** it may need to be checkpointed before the connection can switch to @@ -7545,7 +7542,7 @@ int sqlite3PagerCloseWal(Pager *pPager, sqlite3 *db){ ); } if( rc==SQLITE_OK && logexists ){ - rc = pagerOpenWal(pPager); + rc = pagerOpenWal(pPager, 0); } } diff --git a/src/pager.h b/src/pager.h index 126267bcc8..399fcd4de3 100644 --- a/src/pager.h +++ b/src/pager.h @@ -81,6 +81,23 @@ typedef struct PgHdr DbPage; #define PAGER_JOURNALMODE_TRUNCATE 3 /* Commit by truncating journal */ #define PAGER_JOURNALMODE_MEMORY 4 /* In-memory journal file */ #define PAGER_JOURNALMODE_WAL 5 /* Use write-ahead logging */ +#define PAGER_JOURNALMODE_WAL2 6 /* Use write-ahead logging mode 2 */ + +#define isWalMode(x) ((x)==PAGER_JOURNALMODE_WAL || (x)==PAGER_JOURNALMODE_WAL2) + +/* +** The argument to this macro is a file descriptor (type sqlite3_file*). +** Return 0 if it is not open, or non-zero (but not 1) if it is. +** +** This is so that expressions can be written as: +** +** if( isOpen(pPager->jfd) ){ ... +** +** instead of +** +** if( pPager->jfd->pMethods ){ ... +*/ +#define isOpen(pFd) ((pFd)->pMethods!=0) /* ** Flags that make up the mask passed to sqlite3PagerGet(). @@ -177,7 +194,7 @@ int sqlite3PagerSharedLock(Pager *pPager); int sqlite3PagerCheckpoint(Pager *pPager, sqlite3*, int, int*, int*); int sqlite3PagerWalSupported(Pager *pPager); int sqlite3PagerWalCallback(Pager *pPager); - int sqlite3PagerOpenWal(Pager *pPager, int *pisOpen); + int sqlite3PagerOpenWal(Pager *pPager, int, int *pisOpen); int sqlite3PagerCloseWal(Pager *pPager, sqlite3*); # ifdef SQLITE_DIRECT_OVERFLOW_READ int sqlite3PagerUseWal(Pager *pPager, Pgno); diff --git a/src/pragma.c b/src/pragma.c index 918b1d8131..b78940c971 100644 --- a/src/pragma.c +++ b/src/pragma.c @@ -260,7 +260,7 @@ const char *sqlite3JournalModename(int eMode){ static char * const azModeName[] = { "delete", "persist", "off", "truncate", "memory" #ifndef SQLITE_OMIT_WAL - , "wal" + , "wal", "wal2" #endif }; assert( PAGER_JOURNALMODE_DELETE==0 ); @@ -269,6 +269,7 @@ const char *sqlite3JournalModename(int eMode){ assert( PAGER_JOURNALMODE_TRUNCATE==3 ); assert( PAGER_JOURNALMODE_MEMORY==4 ); assert( PAGER_JOURNALMODE_WAL==5 ); + assert( PAGER_JOURNALMODE_WAL2==6 ); assert( eMode>=0 && eMode<=ArraySize(azModeName) ); if( eMode==ArraySize(azModeName) ) return 0; diff --git a/src/vdbe.c b/src/vdbe.c index 9687170bec..c7b5cf74c4 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -6325,6 +6325,7 @@ case OP_JournalMode: { /* out2 */ || eNew==PAGER_JOURNALMODE_OFF || eNew==PAGER_JOURNALMODE_MEMORY || eNew==PAGER_JOURNALMODE_WAL + || eNew==PAGER_JOURNALMODE_WAL2 || eNew==PAGER_JOURNALMODE_QUERY ); assert( pOp->p1>=0 && pOp->p1nDb ); @@ -6342,16 +6343,25 @@ case OP_JournalMode: { /* out2 */ /* Do not allow a transition to journal_mode=WAL for a database ** in temporary storage or if the VFS does not support shared memory */ - if( eNew==PAGER_JOURNALMODE_WAL + if( isWalMode(eNew) && (sqlite3Strlen30(zFilename)==0 /* Temp file */ || !sqlite3PagerWalSupported(pPager)) /* No shared-memory support */ ){ eNew = eOld; } - if( (eNew!=eOld) - && (eOld==PAGER_JOURNALMODE_WAL || eNew==PAGER_JOURNALMODE_WAL) - ){ + if( eNew!=eOld && (isWalMode(eNew) || isWalMode(eOld)) ){ + + /* Prevent changing directly to wal2 from wal mode. And vice versa. */ + if( isWalMode(eNew) && isWalMode(eOld) ){ + rc = SQLITE_ERROR; + sqlite3VdbeError(p, "cannot change from %s to %s mode", + sqlite3JournalModename(eOld), sqlite3JournalModename(eNew) + ); + goto abort_due_to_error; + } + + /* Prevent switching into or out of wal/wal2 mode mid-transaction */ if( !db->autoCommit || db->nVdbeRead>1 ){ rc = SQLITE_ERROR; sqlite3VdbeError(p, @@ -6359,31 +6369,33 @@ case OP_JournalMode: { /* out2 */ (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") ); goto abort_due_to_error; - }else{ + } - if( eOld==PAGER_JOURNALMODE_WAL ){ - /* If leaving WAL mode, close the log file. If successful, the call - ** to PagerCloseWal() checkpoints and deletes the write-ahead-log - ** file. An EXCLUSIVE lock may still be held on the database file - ** after a successful return. - */ - rc = sqlite3PagerCloseWal(pPager, db); - if( rc==SQLITE_OK ){ - sqlite3PagerSetJournalMode(pPager, eNew); - } - }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ - /* Cannot transition directly from MEMORY to WAL. Use mode OFF - ** as an intermediate */ - sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); - } - - /* Open a transaction on the database file. Regardless of the journal - ** mode, this transaction always uses a rollback journal. + if( isWalMode(eOld) ){ + /* If leaving WAL mode, close the log file. If successful, the call + ** to PagerCloseWal() checkpoints and deletes the write-ahead-log + ** file. An EXCLUSIVE lock may still be held on the database file + ** after a successful return. */ - assert( sqlite3BtreeIsInTrans(pBt)==0 ); + rc = sqlite3PagerCloseWal(pPager, db); if( rc==SQLITE_OK ){ - rc = sqlite3BtreeSetVersion(pBt, (eNew==PAGER_JOURNALMODE_WAL ? 2 : 1)); + sqlite3PagerSetJournalMode(pPager, eNew); } + }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ + /* Cannot transition directly from MEMORY to WAL. Use mode OFF + ** as an intermediate */ + sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); + } + + /* Open a transaction on the database file. Regardless of the journal + ** mode, this transaction always uses a rollback journal. + */ + assert( sqlite3BtreeIsInTrans(pBt)==0 ); + if( rc==SQLITE_OK ){ + /* 1==rollback, 2==wal, 3==wal2 */ + rc = sqlite3BtreeSetVersion(pBt, + 1 + isWalMode(eNew) + (eNew==PAGER_JOURNALMODE_WAL2) + ); } } #endif /* ifndef SQLITE_OMIT_WAL */ diff --git a/src/wal.c b/src/wal.c index 9930b84421..36c04289a5 100644 --- a/src/wal.c +++ b/src/wal.c @@ -255,20 +255,19 @@ int sqlite3WalTrace = 0; #endif /* -** The maximum (and only) versions of the wal and wal-index formats -** that may be interpreted by this version of SQLite. +** Both the wal-file and the wal-index contain version fields +** indicating the current version of the system. If a client +** reads the header of a wal file (as part of recovery), or the +** wal-index (as part of opening a read transaction) and (a) the +** header checksum is correct but (b) the version field is not +** recognized, the operation fails with SQLITE_CANTOPEN. ** -** If a client begins recovering a WAL file and finds that (a) the checksum -** values in the wal-header are correct and (b) the version field is not -** WAL_MAX_VERSION, recovery fails and SQLite returns SQLITE_CANTOPEN. -** -** Similarly, if a client successfully reads a wal-index header (i.e. the -** checksum test is successful) and finds that the version field is not -** WALINDEX_MAX_VERSION, then no read-transaction is opened and SQLite -** returns SQLITE_CANTOPEN. +** Currently, clients support both version-1 ("journal_mode=wal") and +** version-2 ("journal_mode=wal2"). Legacy clients may support version-1 +** only. */ -#define WAL_MAX_VERSION 3007000 -#define WALINDEX_MAX_VERSION 3007000 +#define WAL_VERSION1 3007000 /* For "journal_mode=wal" */ +#define WAL_VERSION2 3021000 /* For "journal_mode=wal2" */ /* ** Indices of various locking bytes. WAL_NREADER is the number @@ -282,6 +281,35 @@ int sqlite3WalTrace = 0; #define WAL_READ_LOCK(I) (3+(I)) #define WAL_NREADER (SQLITE_SHM_NLOCK-3) +/* +** Values that may be stored in Wal.readLock in wal2 mode. +** +** In wal mode, the Wal.readLock member is set to -1 when no read-lock +** is held, or else is the index of the read-mark on which a lock is +** held. +** +** In wal2 mode, Wal.readLock must be set to one of the following values. +** A value of -1 still indicates that no read-lock is held, but the other +** values are symbolic. See the implementation of walLockReader() for +** details of how the symbols map to OS level locks. +*/ +#define WAL_LOCK_NONE -1 +#define WAL_LOCK_PART1 1 +#define WAL_LOCK_PART1_FULL2 2 +#define WAL_LOCK_PART2 3 +#define WAL_LOCK_PART2_FULL1 4 + +/* +** This constant is used in wal2 mode only. +** +** In wal2 mode, when committing a transaction, if the current wal file +** is sufficiently large and there are no conflicting locks held, the +** writer writes the new transaction into the start of the other wal +** file. Usually, "sufficiently large" is defined by the value configured +** using "PRAGMA journal_size_limit". However, if no such value has been +** configured, sufficiently large defaults to WAL_DEFAULT_WALSIZE frames. +*/ +#define WAL_DEFAULT_WALSIZE 1000 /* Object declarations */ typedef struct WalIndexHdr WalIndexHdr; @@ -301,21 +329,64 @@ typedef struct WalCkptInfo WalCkptInfo; ** The szPage value can be any power of 2 between 512 and 32768, inclusive. ** Or it can be 1 to represent a 65536-byte page. The latter case was ** added in 3.7.1 when support for 64K pages was added. +** +** WAL2 mode notes: Member variable mxFrame2 is only used in wal2 mode +** (when iVersion is set to WAL_VERSION2). The lower 31 bits store +** the maximum frame number in file *-wal2. The most significant bit +** is a flag - set if clients are currently appending to *-wal2, clear +** otherwise. */ struct WalIndexHdr { u32 iVersion; /* Wal-index version */ - u32 unused; /* Unused (padding) field */ + u32 mxFrame2; /* See "WAL2 mode notes" above */ u32 iChange; /* Counter incremented each transaction */ u8 isInit; /* 1 when initialized */ u8 bigEndCksum; /* True if checksums in WAL are big-endian */ u16 szPage; /* Database page size in bytes. 1==64K */ - u32 mxFrame; /* Index of last valid frame in the WAL */ + u32 mxFrame; /* Index of last valid frame in each WAL */ u32 nPage; /* Size of database in pages */ u32 aFrameCksum[2]; /* Checksum of last frame in log */ u32 aSalt[2]; /* Two salt values copied from WAL header */ u32 aCksum[2]; /* Checksum over all prior fields */ }; +/* +** The following macros and functions are get/set methods for the maximum +** frame numbers and current wal file values stored in the WalIndexHdr +** structure. These are helpful because of the unorthodox way in which +** the values are stored in wal2 mode (see above). They are equivalent +** to functions with the following signatures. +** +** u32 walidxGetMxFrame(WalIndexHdr*, int iWal); // get mxFrame +** void walidxSetMxFrame(WalIndexHdr*, int iWal, u32 val); // set mxFrame +** int walidxGetFile(WalIndexHdr*) // get file +** void walidxSetFile(WalIndexHdr*, int val); // set file +*/ +#define walidxGetMxFrame(pHdr, iWal) \ + ((iWal) ? ((pHdr)->mxFrame2 & 0x7FFFFFF) : (pHdr)->mxFrame) + +static void walidxSetMxFrame(WalIndexHdr *pHdr, int iWal, u32 mxFrame){ + if( iWal ){ + pHdr->mxFrame2 = (pHdr->mxFrame2 & 0x80000000) | mxFrame; + }else{ + pHdr->mxFrame = mxFrame; + } + assert( walidxGetMxFrame(pHdr, iWal)==mxFrame ); +} + +#define walidxGetFile(pHdr) ((pHdr)->mxFrame2 >> 31) + +#define walidxSetFile(pHdr, iWal) ( \ + (pHdr)->mxFrame2 = ((pHdr)->mxFrame2 & 0x7FFFFFFF) | ((iWal)<<31) \ +) + +/* +** Argument is a pointer to a Wal structure. Return true if the current +** cache of the wal-index header indicates "journal_mode=wal2" mode, or +** false otherwise. +*/ +#define isWalMode2(pWal) ((pWal)->hdr.iVersion==WAL_VERSION2) + /* ** A copy of the following object occurs in the wal-index immediately ** following the second copy of the WalIndexHdr. This object stores @@ -427,7 +498,7 @@ struct WalCkptInfo { struct Wal { sqlite3_vfs *pVfs; /* The VFS used to create pDbFd */ sqlite3_file *pDbFd; /* File handle for the database file */ - sqlite3_file *pWalFd; /* File handle for WAL file */ + sqlite3_file *apWalFd[2]; /* File handle for "*-wal" and "*-wal2" */ u32 iCallback; /* Value to pass to log callback (or 0) */ i64 mxWalSize; /* Truncate WAL to this size upon reset */ int nWiData; /* Size of array apWiData */ @@ -447,6 +518,7 @@ struct Wal { u32 minFrame; /* Ignore wal frames before this one */ u32 iReCksum; /* On commit, recalculate checksums from here */ const char *zWalName; /* Name of WAL file */ + char *zWalName2; /* Name of second WAL file */ u32 nCkpt; /* Checkpoint sequence counter in the wal-header */ #ifdef SQLITE_DEBUG u8 lockError; /* True if a locking error has occurred */ @@ -454,6 +526,7 @@ struct Wal { #ifdef SQLITE_ENABLE_SNAPSHOT WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */ #endif + int bWal2; /* bWal2 flag passed to WalOpen() */ }; /* @@ -667,7 +740,7 @@ static void walIndexWriteHdr(Wal *pWal){ assert( pWal->writeLock ); pWal->hdr.isInit = 1; - pWal->hdr.iVersion = WALINDEX_MAX_VERSION; + assert( pWal->hdr.iVersion==WAL_VERSION1||pWal->hdr.iVersion==WAL_VERSION2 ); walChecksumBytes(1, (u8*)&pWal->hdr, nCksum, 0, pWal->hdr.aCksum); memcpy((void*)&aHdr[1], (const void*)&pWal->hdr, sizeof(WalIndexHdr)); walShmBarrier(pWal); @@ -745,7 +818,7 @@ static int walDecodeFrame( } /* A frame is only valid if a checksum of the WAL header, - ** all prior frams, the first 16 bytes of this frame-header, + ** all prior frames, the first 16 bytes of this frame-header, ** and the frame-data matches the checksum in the last 8 ** bytes of this frame-header. */ @@ -831,6 +904,36 @@ static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){ walLockName(lockIdx), n)); } +/* +** This function is used to take and release read-locks in wal2 mode. +** +** Use of WAL_READ_LOCK(x) slots for (1<=x<=4). +** +** 1) Partial read of *-wal-1 (blocks checkpointer from checkpointing) +** 2) Full read of *-wal-2 (blocks writer from writing) +** 3) Partial read of *-wal-2 (blocks checkpointer from checkpointing) +** 4) Full read of *-wal-1 (blocks writer from writing) +*/ +static int walLockReader(Wal *pWal, int eLock, int bLock){ + int i; /* Index of first readmark to lock */ + int n; /* Number of readmarks to lock */ + + assert( pWal->hdr.iVersion==WAL_VERSION2 ); + if( pWal->exclusiveMode ) return SQLITE_OK; + + switch( eLock ){ + case WAL_LOCK_PART1 : i = 1; n = 1; break; + case WAL_LOCK_PART1_FULL2: i = 1; n = 2; break; + case WAL_LOCK_PART2 : i = 3; n = 1; break; + case WAL_LOCK_PART2_FULL1: i = 3; n = 2; break; + default: assert( !"cannot happen" ); + } + + return sqlite3OsShmLock(pWal->pDbFd, WAL_READ_LOCK(i), n, + SQLITE_SHM_SHARED | (bLock ? SQLITE_SHM_LOCK : SQLITE_SHM_UNLOCK) + ); +} + /* ** Compute a hash on a page number. The resulting hash value must land ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances @@ -891,6 +994,43 @@ static int walHashGet( return rc; } +static u32 walExternalEncode(int iWal, u32 iFrame){ + u32 iRet; + if( iWal ){ + iRet = HASHTABLE_NPAGE_ONE + iFrame; + iRet += ((iFrame-1) / HASHTABLE_NPAGE) * HASHTABLE_NPAGE; + }else{ + iRet = iFrame; + iFrame += HASHTABLE_NPAGE - HASHTABLE_NPAGE_ONE; + iRet += ((iFrame-1) / HASHTABLE_NPAGE) * HASHTABLE_NPAGE; + } + return iRet; +} + +/* +** Parameter iExternal is an external frame identifier. This function +** transforms it to a wal file number (0 or 1) and frame number within +** this wal file (reported via output parameter *piRead). +*/ +static int walExternalDecode(u32 iExternal, u32 *piRead){ + int iHash = (iExternal+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1)/HASHTABLE_NPAGE; + + if( 0==(iHash & 0x01) ){ + /* A frame in wal file 0 */ + *piRead = (iExternal <= HASHTABLE_NPAGE_ONE) ? iExternal : + iExternal - (iHash/2) * HASHTABLE_NPAGE; + return 0; + } + if( iHash==0 ){ + *piRead = iExternal; + return 0; + }else{ + *piRead = iExternal - HASHTABLE_NPAGE_ONE - ((iHash-1)/2) * HASHTABLE_NPAGE; + } + + return (iHash % 2); +} + /* ** Return the number of the wal-index page that contains the hash-table ** and page-number array that contain entries corresponding to WAL frame @@ -908,6 +1048,22 @@ static int walFramePage(u32 iFrame){ return iHash; } +/* +** Return the index of the hash-table corresponding to frame iFrame of wal +** file iWal. +*/ +static int walFramePage2(int iWal, u32 iFrame){ + int iRet; + assert( iWal==0 || iWal==1 ); + assert( iFrame>0 ); + if( iWal==0 ){ + iRet = 2*((iFrame+HASHTABLE_NPAGE-HASHTABLE_NPAGE_ONE-1)/HASHTABLE_NPAGE); + }else{ + iRet = 1 + 2 * ((iFrame-1) / HASHTABLE_NPAGE); + } + return iRet; +} + /* ** Return the page number associated with frame iFrame in this WAL. */ @@ -919,6 +1075,10 @@ static u32 walFramePgno(Wal *pWal, u32 iFrame){ return pWal->apWiData[iHash][(iFrame-1-HASHTABLE_NPAGE_ONE)%HASHTABLE_NPAGE]; } +static u32 walFramePgno2(Wal *pWal, int iWal, u32 iFrame){ + return walFramePgno(pWal, walExternalEncode(iWal, iFrame)); +} + /* ** Remove entries from the hash table that point to WAL slots greater ** than pWal->hdr.mxFrame. @@ -938,26 +1098,36 @@ static void walCleanupHash(Wal *pWal){ int iLimit = 0; /* Zero values greater than this */ int nByte; /* Number of bytes to zero in aPgno[] */ int i; /* Used to iterate through aHash[] */ + int iWal = walidxGetFile(&pWal->hdr); + u32 mxFrame = walidxGetMxFrame(&pWal->hdr, iWal); + + u32 iExternal; + if( isWalMode2(pWal) ){ + iExternal = walExternalEncode(iWal, mxFrame); + }else{ + assert( iWal==0 ); + iExternal = mxFrame; + } assert( pWal->writeLock ); - testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE-1 ); - testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE ); - testcase( pWal->hdr.mxFrame==HASHTABLE_NPAGE_ONE+1 ); + testcase( mxFrame==HASHTABLE_NPAGE_ONE-1 ); + testcase( mxFrame==HASHTABLE_NPAGE_ONE ); + testcase( mxFrame==HASHTABLE_NPAGE_ONE+1 ); - if( pWal->hdr.mxFrame==0 ) return; + if( mxFrame==0 ) return; /* Obtain pointers to the hash-table and page-number array containing ** the entry that corresponds to frame pWal->hdr.mxFrame. It is guaranteed ** that the page said hash-table and array reside on is already mapped. */ - assert( pWal->nWiData>walFramePage(pWal->hdr.mxFrame) ); - assert( pWal->apWiData[walFramePage(pWal->hdr.mxFrame)] ); - walHashGet(pWal, walFramePage(pWal->hdr.mxFrame), &aHash, &aPgno, &iZero); + assert( pWal->nWiData>walFramePage(iExternal) ); + assert( pWal->apWiData[walFramePage(iExternal)] ); + walHashGet(pWal, walFramePage(iExternal), &aHash, &aPgno, &iZero); /* Zero all hash-table entries that correspond to frame numbers greater ** than pWal->hdr.mxFrame. */ - iLimit = pWal->hdr.mxFrame - iZero; + iLimit = iExternal - iZero; assert( iLimit>0 ); for(i=0; iiLimit ){ @@ -966,8 +1136,7 @@ static void walCleanupHash(Wal *pWal){ } /* Zero the entries in the aPgno array that correspond to frames with - ** frame numbers greater than pWal->hdr.mxFrame. - */ + ** frame numbers greater than pWal->hdr.mxFrame. */ nByte = (int)((char *)aHash - (char *)&aPgno[iLimit+1]); memset((void *)&aPgno[iLimit+1], 0, nByte); @@ -988,18 +1157,25 @@ static void walCleanupHash(Wal *pWal){ #endif /* SQLITE_ENABLE_EXPENSIVE_ASSERT */ } - /* ** Set an entry in the wal-index that will map database page number ** pPage into WAL frame iFrame. */ -static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ +static int walIndexAppend(Wal *pWal, int iWal, u32 iFrame, u32 iPage){ int rc; /* Return code */ u32 iZero = 0; /* One less than frame number of aPgno[1] */ volatile u32 *aPgno = 0; /* Page number array */ volatile ht_slot *aHash = 0; /* Hash table */ + u32 iExternal; + + if( isWalMode2(pWal) ){ + iExternal = walExternalEncode(iWal, iFrame); + }else{ + assert( iWal==0 ); + iExternal = iFrame; + } - rc = walHashGet(pWal, walFramePage(iFrame), &aHash, &aPgno, &iZero); + rc = walHashGet(pWal, walFramePage(iExternal), &aHash, &aPgno, &iZero); /* Assuming the wal-index file was successfully mapped, populate the ** page number array and hash table entry. @@ -1009,7 +1185,7 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ int idx; /* Value to write to hash-table slot */ int nCollide; /* Number of hash collisions */ - idx = iFrame - iZero; + idx = iExternal - iZero; assert( idx <= HASHTABLE_NSLOT/2 + 1 ); /* If this is the first entry to be added to this hash-table, zero the @@ -1071,6 +1247,133 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ return rc; } +/* +** Recover a single wal file - *-wal if iWal==0, or *-wal2 if iWal==1. +*/ +static int walIndexRecoverOne(Wal *pWal, int iWal, u32 *pnCkpt, int *pbZero){ + i64 nSize; /* Size of log file */ + u32 aFrameCksum[2] = {0, 0}; + int rc; + sqlite3_file *pWalFd = pWal->apWalFd[iWal]; + + assert( iWal==0 || iWal==1 ); + + memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); + sqlite3_randomness(8, pWal->hdr.aSalt); + + rc = sqlite3OsFileSize(pWalFd, &nSize); + if( rc==SQLITE_OK ){ + if( nSize>WAL_HDRSIZE ){ + u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */ + u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */ + int szFrame; /* Number of bytes in buffer aFrame[] */ + u8 *aData; /* Pointer to data part of aFrame buffer */ + int iFrame; /* Index of last frame read */ + i64 iOffset; /* Next offset to read from log file */ + int szPage; /* Page size according to the log */ + u32 magic; /* Magic value read from WAL header */ + u32 version; /* Magic value read from WAL header */ + int isValid; /* True if this frame is valid */ + + /* Read in the WAL header. */ + rc = sqlite3OsRead(pWalFd, aBuf, WAL_HDRSIZE, 0); + if( rc!=SQLITE_OK ){ + return rc; + } + + /* If the database page size is not a power of two, or is greater than + ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid + ** data. Similarly, if the 'magic' value is invalid, ignore the whole + ** WAL file. + */ + magic = sqlite3Get4byte(&aBuf[0]); + szPage = sqlite3Get4byte(&aBuf[8]); + if( (magic&0xFFFFFFFE)!=WAL_MAGIC + || szPage&(szPage-1) + || szPage>SQLITE_MAX_PAGE_SIZE + || szPage<512 + ){ + return SQLITE_OK; + } + pWal->hdr.bigEndCksum = (u8)(magic&0x00000001); + pWal->szPage = szPage; + + /* Verify that the WAL header checksum is correct */ + walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, + aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum + ); + if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24]) + || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28]) + ){ + return SQLITE_OK; + } + + memcpy(&pWal->hdr.aSalt, &aBuf[16], 8); + *pnCkpt = sqlite3Get4byte(&aBuf[12]); + + /* Verify that the version number on the WAL format is one that + ** are able to understand */ + version = sqlite3Get4byte(&aBuf[4]); + if( version!=WAL_VERSION1 && version!=WAL_VERSION2 ){ + return SQLITE_CANTOPEN_BKPT; + } + pWal->hdr.iVersion = version; + + /* Malloc a buffer to read frames into. */ + szFrame = szPage + WAL_FRAME_HDRSIZE; + aFrame = (u8 *)sqlite3_malloc64(szFrame); + if( !aFrame ){ + return SQLITE_NOMEM_BKPT; + } + aData = &aFrame[WAL_FRAME_HDRSIZE]; + + /* Read all frames from the log file. */ + iFrame = 0; + for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){ + u32 pgno; /* Database page number for frame */ + u32 nTruncate; /* dbsize field from frame header */ + + /* Read and decode the next log frame. */ + iFrame++; + rc = sqlite3OsRead(pWalFd, aFrame, szFrame, iOffset); + if( rc!=SQLITE_OK ) break; + isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame); + if( !isValid ) break; + rc = walIndexAppend(pWal, iWal, iFrame, pgno); + if( rc!=SQLITE_OK ) break; + + /* If nTruncate is non-zero, this is a commit record. */ + if( nTruncate ){ + pWal->hdr.mxFrame = iFrame; + pWal->hdr.nPage = nTruncate; + pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16)); + testcase( szPage<=32768 ); + testcase( szPage>=65536 ); + aFrameCksum[0] = pWal->hdr.aFrameCksum[0]; + aFrameCksum[1] = pWal->hdr.aFrameCksum[1]; + } + } + + sqlite3_free(aFrame); + }else if( pbZero && nSize==0 ){ + *pbZero = 1; + } + } + + pWal->hdr.aFrameCksum[0] = aFrameCksum[0]; + pWal->hdr.aFrameCksum[1] = aFrameCksum[1]; + + return rc; +} + +static int walOpenWal2(Wal *pWal){ + int rc = SQLITE_OK; + if( !isOpen(pWal->apWalFd[1]) ){ + int f = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL); + rc = sqlite3OsOpen(pWal->pVfs, pWal->zWalName2, pWal->apWalFd[1], f, &f); + } + return rc; +} /* ** Recover the wal-index by reading the write-ahead log file. @@ -1084,10 +1387,12 @@ static int walIndexAppend(Wal *pWal, u32 iFrame, u32 iPage){ */ static int walIndexRecover(Wal *pWal){ int rc; /* Return Code */ - i64 nSize; /* Size of log file */ - u32 aFrameCksum[2] = {0, 0}; int iLock; /* Lock offset to lock for checkpoint */ int nLock; /* Number of locks to hold */ + u32 nCkpt1 = 0xFFFFFFFF; + u32 nCkpt2 = 0xFFFFFFFF; + int bZero = 0; + WalIndexHdr hdr; /* Obtain an exclusive lock on all byte in the locking range not already ** locked by the caller. The caller is guaranteed to have locked the @@ -1107,147 +1412,116 @@ static int walIndexRecover(Wal *pWal){ } WALTRACE(("WAL%p: recovery begin...\n", pWal)); - memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); - - rc = sqlite3OsFileSize(pWal->pWalFd, &nSize); - if( rc!=SQLITE_OK ){ - goto recovery_error; - } - - if( nSize>WAL_HDRSIZE ){ - u8 aBuf[WAL_HDRSIZE]; /* Buffer to load WAL header into */ - u8 *aFrame = 0; /* Malloc'd buffer to load entire frame */ - int szFrame; /* Number of bytes in buffer aFrame[] */ - u8 *aData; /* Pointer to data part of aFrame buffer */ - int iFrame; /* Index of last frame read */ - i64 iOffset; /* Next offset to read from log file */ - int szPage; /* Page size according to the log */ - u32 magic; /* Magic value read from WAL header */ - u32 version; /* Magic value read from WAL header */ - int isValid; /* True if this frame is valid */ - - /* Read in the WAL header. */ - rc = sqlite3OsRead(pWal->pWalFd, aBuf, WAL_HDRSIZE, 0); - if( rc!=SQLITE_OK ){ - goto recovery_error; - } - - /* If the database page size is not a power of two, or is greater than - ** SQLITE_MAX_PAGE_SIZE, conclude that the WAL file contains no valid - ** data. Similarly, if the 'magic' value is invalid, ignore the whole - ** WAL file. - */ - magic = sqlite3Get4byte(&aBuf[0]); - szPage = sqlite3Get4byte(&aBuf[8]); - if( (magic&0xFFFFFFFE)!=WAL_MAGIC - || szPage&(szPage-1) - || szPage>SQLITE_MAX_PAGE_SIZE - || szPage<512 - ){ - goto finished; - } - pWal->hdr.bigEndCksum = (u8)(magic&0x00000001); - pWal->szPage = szPage; - pWal->nCkpt = sqlite3Get4byte(&aBuf[12]); - memcpy(&pWal->hdr.aSalt, &aBuf[16], 8); - - /* Verify that the WAL header checksum is correct */ - walChecksumBytes(pWal->hdr.bigEndCksum==SQLITE_BIGENDIAN, - aBuf, WAL_HDRSIZE-2*4, 0, pWal->hdr.aFrameCksum - ); - if( pWal->hdr.aFrameCksum[0]!=sqlite3Get4byte(&aBuf[24]) - || pWal->hdr.aFrameCksum[1]!=sqlite3Get4byte(&aBuf[28]) - ){ - goto finished; + /* Recover the *-wal file. If a valid version-1 header is recovered + ** from it, do not open the *-wal2 file. Even if it exists. + ** + ** Otherwise, if the *-wal2 file exists or if the "wal2" flag was + ** specified when sqlite3WalOpen() was called, open and recover + ** the *-wal2 file. Except, if the *-wal file was zero bytes in size, + ** truncate the *-wal2 to zero bytes in size. + ** + ** After this block has run, if the *-wal2 file is open the system + ** starts up in VERSION2 mode. In this case pWal->hdr contains the + ** wal-index header considering only *-wal2. Stack variable hdr + ** contains the wal-index header considering only *-wal. The hash + ** tables are populated for both. + ** + ** Or, if the *-wal2 file is not open, start up in VERSION1 mode. + ** pWal->hdr is already populated. + */ + rc = walIndexRecoverOne(pWal, 0, &nCkpt1, &bZero); + assert( pWal->hdr.iVersion==0 + || pWal->hdr.iVersion==WAL_VERSION1 + || pWal->hdr.iVersion==WAL_VERSION2 + ); + if( rc==SQLITE_OK && pWal->hdr.iVersion!=WAL_VERSION1 ){ + int bOpen = 1; + sqlite3_vfs *pVfs = pWal->pVfs; + if( pWal->hdr.iVersion==0 && pWal->bWal2==0 ){ + rc = sqlite3OsAccess(pVfs, pWal->zWalName2, SQLITE_ACCESS_EXISTS, &bOpen); } - - /* Verify that the version number on the WAL format is one that - ** are able to understand */ - version = sqlite3Get4byte(&aBuf[4]); - if( version!=WAL_MAX_VERSION ){ - rc = SQLITE_CANTOPEN_BKPT; - goto finished; + if( rc==SQLITE_OK && bOpen ){ + rc = walOpenWal2(pWal); + if( rc==SQLITE_OK ){ + hdr = pWal->hdr; + rc = walIndexRecoverOne(pWal, 1, &nCkpt2, 0); + } } + } - /* Malloc a buffer to read frames into. */ - szFrame = szPage + WAL_FRAME_HDRSIZE; - aFrame = (u8 *)sqlite3_malloc64(szFrame); - if( !aFrame ){ - rc = SQLITE_NOMEM_BKPT; - goto recovery_error; - } - aData = &aFrame[WAL_FRAME_HDRSIZE]; + if( rc==SQLITE_OK ){ + volatile WalCkptInfo *pInfo; - /* Read all frames from the log file. */ - iFrame = 0; - for(iOffset=WAL_HDRSIZE; (iOffset+szFrame)<=nSize; iOffset+=szFrame){ - u32 pgno; /* Database page number for frame */ - u32 nTruncate; /* dbsize field from frame header */ + if( isOpen(pWal->apWalFd[1]) ){ + /* The case where *-wal2 may follow *-wal */ + if( nCkpt2<=0x0F && nCkpt2==nCkpt1+1 ){ + if( sqlite3Get4byte((u8*)(&pWal->hdr.aSalt[0]))==hdr.aFrameCksum[0] + && sqlite3Get4byte((u8*)(&pWal->hdr.aSalt[1]))==hdr.aFrameCksum[1] + ){ + walidxSetFile(&pWal->hdr, 1); + walidxSetMxFrame(&pWal->hdr, 1, pWal->hdr.mxFrame); + walidxSetMxFrame(&pWal->hdr, 0, hdr.mxFrame); + }else{ + pWal->hdr = hdr; + } + }else - /* Read and decode the next log frame. */ - iFrame++; - rc = sqlite3OsRead(pWal->pWalFd, aFrame, szFrame, iOffset); - if( rc!=SQLITE_OK ) break; - isValid = walDecodeFrame(pWal, &pgno, &nTruncate, aData, aFrame); - if( !isValid ) break; - rc = walIndexAppend(pWal, iFrame, pgno); - if( rc!=SQLITE_OK ) break; + /* When *-wal may follow *-wal2 */ + if( (nCkpt2==0x0F && nCkpt1==0) || (nCkpt2<0x0F && nCkpt2==nCkpt1-1) ){ + if( sqlite3Get4byte((u8*)(&hdr.aSalt[0]))==pWal->hdr.aFrameCksum[0] + && sqlite3Get4byte((u8*)(&hdr.aSalt[1]))==pWal->hdr.aFrameCksum[1] + ){ + SWAP(WalIndexHdr, pWal->hdr, hdr); + walidxSetMxFrame(&pWal->hdr, 1, hdr.mxFrame); + } + }else - /* If nTruncate is non-zero, this is a commit record. */ - if( nTruncate ){ - pWal->hdr.mxFrame = iFrame; - pWal->hdr.nPage = nTruncate; - pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16)); - testcase( szPage<=32768 ); - testcase( szPage>=65536 ); - aFrameCksum[0] = pWal->hdr.aFrameCksum[0]; - aFrameCksum[1] = pWal->hdr.aFrameCksum[1]; + /* Fallback */ + if( nCkpt1<=nCkpt2 ){ + pWal->hdr = hdr; + }else{ + walidxSetFile(&pWal->hdr, 1); } + pWal->hdr.iVersion = WAL_VERSION2; + }else{ + pWal->hdr.iVersion = WAL_VERSION1; } - sqlite3_free(aFrame); - } - -finished: - if( rc==SQLITE_OK ){ - volatile WalCkptInfo *pInfo; - int i; - pWal->hdr.aFrameCksum[0] = aFrameCksum[0]; - pWal->hdr.aFrameCksum[1] = aFrameCksum[1]; walIndexWriteHdr(pWal); /* Reset the checkpoint-header. This is safe because this thread is ** currently holding locks that exclude all other readers, writers and - ** checkpointers. - */ + ** checkpointers. */ pInfo = walCkptInfo(pWal); - pInfo->nBackfill = 0; - pInfo->nBackfillAttempted = pWal->hdr.mxFrame; - pInfo->aReadMark[0] = 0; - for(i=1; iaReadMark[i] = READMARK_NOT_USED; - if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame; + memset((void*)pInfo, 0, sizeof(WalCkptInfo)); + if( 0==isWalMode2(pWal) ){ + int i; + pInfo->nBackfillAttempted = pWal->hdr.mxFrame; + pInfo->aReadMark[0] = 0; + for(i=1; iaReadMark[i] = READMARK_NOT_USED; + if( pWal->hdr.mxFrame ) pInfo->aReadMark[1] = pWal->hdr.mxFrame; + } /* If more than one frame was recovered from the log file, report an ** event via sqlite3_log(). This is to help with identifying performance ** problems caused by applications routinely shutting down without - ** checkpointing the log file. - */ + ** checkpointing the log file. */ if( pWal->hdr.nPage ){ sqlite3_log(SQLITE_NOTICE_RECOVER_WAL, - "recovered %d frames from WAL file %s", - pWal->hdr.mxFrame, pWal->zWalName + "recovered (%d,%d) frames from WAL files %s[2] (%s mode)", + walidxGetMxFrame(&pWal->hdr, 0), walidxGetMxFrame(&pWal->hdr, 1), + pWal->zWalName, isWalMode2(pWal) ? "wal2" : "wal" ); } } -recovery_error: WALTRACE(("WAL%p: recovery %s\n", pWal, rc ? "failed" : "ok")); walUnlockExclusive(pWal, iLock, nLock); return rc; } /* -** Close an open wal-index. +** Close an open wal-index and wal files. */ static void walIndexClose(Wal *pWal, int isDelete){ if( pWal->exclusiveMode==WAL_HEAPMEMORY_MODE ){ @@ -1259,6 +1533,8 @@ static void walIndexClose(Wal *pWal, int isDelete){ }else{ sqlite3OsShmUnmap(pWal->pDbFd, isDelete); } + sqlite3OsClose(pWal->apWalFd[0]); + sqlite3OsClose(pWal->apWalFd[1]); } /* @@ -1282,11 +1558,14 @@ int sqlite3WalOpen( const char *zWalName, /* Name of the WAL file */ int bNoShm, /* True to run in heap-memory mode */ i64 mxWalSize, /* Truncate WAL to this size on reset */ + int bWal2, /* True to open in wal2 mode */ Wal **ppWal /* OUT: Allocated Wal handle */ ){ int rc; /* Return Code */ Wal *pRet; /* Object to allocate and return */ int flags; /* Flags passed to OsOpen() */ + int nWalName; /* Length of zWalName in bytes */ + int nByte; /* Bytes of space to allocate */ assert( zWalName && zWalName[0] ); assert( pDbFd ); @@ -1306,34 +1585,42 @@ int sqlite3WalOpen( assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET ); #endif + nWalName = sqlite3Strlen30(zWalName); + nByte = sizeof(Wal) + pVfs->szOsFile*2 + nWalName+2; /* Allocate an instance of struct Wal to return. */ *ppWal = 0; - pRet = (Wal*)sqlite3MallocZero(sizeof(Wal) + pVfs->szOsFile); + pRet = (Wal*)sqlite3MallocZero(nByte); if( !pRet ){ return SQLITE_NOMEM_BKPT; } pRet->pVfs = pVfs; - pRet->pWalFd = (sqlite3_file *)&pRet[1]; + pRet->apWalFd[0] = (sqlite3_file*)((char*)pRet+sizeof(Wal)); + pRet->apWalFd[1] = (sqlite3_file*)((char*)pRet+sizeof(Wal)+pVfs->szOsFile); pRet->pDbFd = pDbFd; - pRet->readLock = -1; + pRet->readLock = WAL_LOCK_NONE; pRet->mxWalSize = mxWalSize; pRet->zWalName = zWalName; pRet->syncHeader = 1; pRet->padToSectorBoundary = 1; pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE); + pRet->bWal2 = bWal2; + + pRet->zWalName2 = (char*)pRet + sizeof(Wal) + 2*pVfs->szOsFile; + memcpy(pRet->zWalName2, zWalName, nWalName); + pRet->zWalName2[nWalName] = '2'; + pRet->zWalName2[nWalName+1] = '\0'; - /* Open file handle on the write-ahead log file. */ + /* Open a file handle on the first write-ahead log file. */ flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL); - rc = sqlite3OsOpen(pVfs, zWalName, pRet->pWalFd, flags, &flags); + rc = sqlite3OsOpen(pVfs, zWalName, pRet->apWalFd[0], flags, &flags); if( rc==SQLITE_OK && flags&SQLITE_OPEN_READONLY ){ pRet->readOnly = WAL_RDONLY; } if( rc!=SQLITE_OK ){ walIndexClose(pRet, 0); - sqlite3OsClose(pRet->pWalFd); sqlite3_free(pRet); }else{ int iDC = sqlite3OsDeviceCharacteristics(pDbFd); @@ -1546,30 +1833,40 @@ static void walIteratorFree(WalIterator *p){ ** pages in the WAL in ascending order. The caller must hold the checkpoint ** lock. ** -** On success, make *pp point to the newly allocated WalInterator object -** return SQLITE_OK. Otherwise, return an error code. If this routine -** returns an error, the value of *pp is undefined. +** On success, make *pp point to the newly allocated WalIterator object +** and return SQLITE_OK. Otherwise, return an error code. If this routine +** returns an error, the final value of *pp is undefined. ** ** The calling routine should invoke walIteratorFree() to destroy the ** WalIterator object when it has finished with it. */ -static int walIteratorInit(Wal *pWal, WalIterator **pp){ +static int walIteratorInit(Wal *pWal, int iWal, WalIterator **pp){ WalIterator *p; /* Return value */ int nSegment; /* Number of segments to merge */ u32 iLast; /* Last frame in log */ int nByte; /* Number of bytes to allocate */ int i; /* Iterator variable */ + int iLastSeg; /* Last hash table to iterate though */ ht_slot *aTmp; /* Temp space used by merge-sort */ int rc = SQLITE_OK; /* Return Code */ + int iMode = isWalMode2(pWal) ? 2 : 1; + + assert( isWalMode2(pWal) || iWal==0 ); /* This routine only runs while holding the checkpoint lock. And ** it only runs if there is actually content in the log (mxFrame>0). */ - assert( pWal->ckptLock && pWal->hdr.mxFrame>0 ); - iLast = pWal->hdr.mxFrame; + iLast = walidxGetMxFrame(&pWal->hdr, iWal); + assert( pWal->ckptLock && iLast>0 ); + + if( iMode==2 ){ + iLastSeg = walFramePage2(iWal, iLast); + }else{ + iLastSeg = walFramePage(iLast); + } + nSegment = 1 + (iLastSeg/iMode); /* Allocate space for the WalIterator object. */ - nSegment = walFramePage(iLast) + 1; nByte = sizeof(WalIterator) + (nSegment-1)*sizeof(struct WalSegment) + iLast*sizeof(ht_slot); @@ -1590,19 +1887,28 @@ static int walIteratorInit(Wal *pWal, WalIterator **pp){ rc = SQLITE_NOMEM_BKPT; } - for(i=0; rc==SQLITE_OK && i=2 ); + }else{ + iZero = iExtZero; + } aPgno++; - if( (i+1)==nSegment ){ + if( i==iLastSeg ){ nEntry = (int)(iLast - iZero); }else{ nEntry = (int)((u32*)aHash - (u32*)aPgno); @@ -1614,10 +1920,10 @@ static int walIteratorInit(Wal *pWal, WalIterator **pp){ aIndex[j] = (ht_slot)j; } walMergesort((u32 *)aPgno, aTmp, aIndex, &nEntry); - p->aSegment[i].iZero = iZero; - p->aSegment[i].nEntry = nEntry; - p->aSegment[i].aIndex = aIndex; - p->aSegment[i].aPgno = (u32 *)aPgno; + p->aSegment[i/iMode].iZero = iZero; + p->aSegment[i/iMode].nEntry = nEntry; + p->aSegment[i/iMode].aIndex = aIndex; + p->aSegment[i/iMode].aPgno = (u32 *)aPgno; } } sqlite3_free(aTmp); @@ -1678,6 +1984,7 @@ static void walRestartHdr(Wal *pWal, u32 salt1){ volatile WalCkptInfo *pInfo = walCkptInfo(pWal); int i; /* Loop counter */ u32 *aSalt = pWal->hdr.aSalt; /* Big-endian salt values */ + assert( isWalMode2(pWal)==0 ); pWal->nCkpt++; pWal->hdr.mxFrame = 0; sqlite3Put4byte((u8*)&aSalt[0], 1 + sqlite3Get4byte((u8*)&aSalt[0])); @@ -1739,15 +2046,30 @@ static int walCheckpoint( u32 mxPage; /* Max database page to write */ int i; /* Loop counter */ volatile WalCkptInfo *pInfo; /* The checkpoint status information */ + int bWal2 = isWalMode2(pWal); /* True for wal2 connections */ + int iCkpt = bWal2 ? !walidxGetFile(&pWal->hdr) : 0; + mxSafeFrame = walidxGetMxFrame(&pWal->hdr, iCkpt); szPage = walPagesize(pWal); testcase( szPage<=32768 ); testcase( szPage>=65536 ); pInfo = walCkptInfo(pWal); - if( pInfo->nBackfillhdr.mxFrame ){ + if( (bWal2==1 && pInfo->nBackfill==0 && mxSafeFrame) + || (bWal2==0 && pInfo->nBackfillapWalFd[iCkpt]; + mxPage = pWal->hdr.nPage; + + /* If this is a wal2 system, check for a reader holding a lock + ** preventing this checkpoint operation. If one is found, return + ** early. */ + if( bWal2 ){ + rc = walLockExclusive(pWal, WAL_READ_LOCK(1 + iCkpt*2), 1); + if( rc!=SQLITE_OK ) return rc; + } /* Allocate the iterator */ - rc = walIteratorInit(pWal, &pIter); + rc = walIteratorInit(pWal, iCkpt, &pIter); if( rc!=SQLITE_OK ){ return rc; } @@ -1757,52 +2079,53 @@ static int walCheckpoint( ** in the SQLITE_CHECKPOINT_PASSIVE mode. */ assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 ); - /* Compute in mxSafeFrame the index of the last frame of the WAL that is - ** safe to write into the database. Frames beyond mxSafeFrame might - ** overwrite database pages that are in use by active readers and thus - ** cannot be backfilled from the WAL. + + /* If this is a wal system (not wal2), compute in mxSafeFrame the index + ** of the last frame of the WAL that is safe to write into the database. + ** Frames beyond mxSafeFrame might overwrite database pages that are in + ** use by active readers and thus cannot be backfilled from the WAL. */ - mxSafeFrame = pWal->hdr.mxFrame; - mxPage = pWal->hdr.nPage; - for(i=1; iaReadMark[i]; - if( mxSafeFrame>y ){ - assert( y<=pWal->hdr.mxFrame ); - rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1); - if( rc==SQLITE_OK ){ - pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED); - walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); - }else if( rc==SQLITE_BUSY ){ - mxSafeFrame = y; - xBusy = 0; - }else{ - goto walcheckpoint_out; + if( bWal2==0 ){ + for(i=1; iaReadMark[i]; + if( mxSafeFrame>y ){ + assert( y<=pWal->hdr.mxFrame ); + rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1); + if( rc==SQLITE_OK ){ + pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED); + walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); + }else if( rc==SQLITE_BUSY ){ + mxSafeFrame = y; + xBusy = 0; + }else{ + goto walcheckpoint_out; + } } } } - if( pInfo->nBackfillnBackfillnBackfill; + assert( bWal2==0 || nBackfill==0 ); pInfo->nBackfillAttempted = mxSafeFrame; - /* Sync the WAL to disk */ - rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)); + /* Sync the wal file being checkpointed to disk */ + rc = sqlite3OsSync(pWalFd, CKPT_SYNC_FLAGS(sync_flags)); /* If the database may grow as a result of this checkpoint, hint - ** about the eventual size of the db file to the VFS layer. - */ + ** about the eventual size of the db file to the VFS layer. */ if( rc==SQLITE_OK ){ i64 nReq = ((i64)mxPage * szPage); rc = sqlite3OsFileSize(pWal->pDbFd, &nSize); @@ -1811,21 +2134,27 @@ static int walCheckpoint( } } - /* Iterate through the contents of the WAL, copying data to the db file */ while( rc==SQLITE_OK && 0==walIteratorNext(pIter, &iDbpage, &iFrame) ){ i64 iOffset; - assert( walFramePgno(pWal, iFrame)==iDbpage ); + + assert( bWal2==1 || walFramePgno(pWal, iFrame)==iDbpage ); + assert( bWal2==0 || walFramePgno2(pWal, iCkpt, iFrame)==iDbpage ); + if( db->u1.isInterrupted ){ rc = db->mallocFailed ? SQLITE_NOMEM_BKPT : SQLITE_INTERRUPT; break; } if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){ + assert( bWal2==0 || iDbpage>mxPage ); continue; } iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE; + WALTRACE(("WAL%p: checkpoint frame %d of wal %d to db page %d\n", + pWal, (int)iFrame, iCkpt, (int)iDbpage + )); /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */ - rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset); + rc = sqlite3OsRead(pWalFd, zBuf, szPage, iOffset); if( rc!=SQLITE_OK ) break; iOffset = (iDbpage-1)*(i64)szPage; testcase( IS_BIG_INT(iOffset) ); @@ -1833,23 +2162,22 @@ static int walCheckpoint( if( rc!=SQLITE_OK ) break; } - /* If work was actually accomplished... */ + /* Truncate the db file, sync the wal file and set the WalCkptInfo + ** flag to indicate that it has been checkpointed. */ + if( !bWal2 && rc==SQLITE_OK && mxSafeFrame==walIndexHdr(pWal)->mxFrame ){ + i64 szDb = pWal->hdr.nPage*(i64)szPage; + testcase( IS_BIG_INT(szDb) ); + rc = sqlite3OsTruncate(pWal->pDbFd, szDb); + } if( rc==SQLITE_OK ){ - if( mxSafeFrame==walIndexHdr(pWal)->mxFrame ){ - i64 szDb = pWal->hdr.nPage*(i64)szPage; - testcase( IS_BIG_INT(szDb) ); - rc = sqlite3OsTruncate(pWal->pDbFd, szDb); - if( rc==SQLITE_OK ){ - rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags)); - } - } - if( rc==SQLITE_OK ){ - pInfo->nBackfill = mxSafeFrame; - } + rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags)); + } + if( rc==SQLITE_OK ){ + pInfo->nBackfill = bWal2 ? 1 : mxSafeFrame; } /* Release the reader lock held while backfilling */ - walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1); + walUnlockExclusive(pWal, WAL_READ_LOCK(bWal2 ? 1 + iCkpt*2 : 0), 1); } if( rc==SQLITE_BUSY ){ @@ -1864,7 +2192,7 @@ static int walCheckpoint( ** until all readers have finished using the wal file. This ensures that ** the next process to write to the database restarts the wal file. */ - if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){ + if( bWal2==0 && rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){ assert( pWal->writeLock ); if( pInfo->nBackfillhdr.mxFrame ){ rc = SQLITE_BUSY; @@ -1889,7 +2217,7 @@ static int walCheckpoint( ** file-system. To avoid this, update the wal-index header to ** indicate that the log file contains zero valid frames. */ walRestartHdr(pWal, salt1); - rc = sqlite3OsTruncate(pWal->pWalFd, 0); + rc = sqlite3OsTruncate(pWal->apWalFd[0], 0); } walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1); } @@ -1906,16 +2234,18 @@ static int walCheckpoint( ** it to exactly nMax bytes. If an error occurs while doing so, ignore it. */ static void walLimitSize(Wal *pWal, i64 nMax){ - i64 sz; - int rx; - sqlite3BeginBenignMalloc(); - rx = sqlite3OsFileSize(pWal->pWalFd, &sz); - if( rx==SQLITE_OK && (sz > nMax ) ){ - rx = sqlite3OsTruncate(pWal->pWalFd, nMax); - } - sqlite3EndBenignMalloc(); - if( rx ){ - sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName); + if( isWalMode2(pWal)==0 ){ + i64 sz; + int rx; + sqlite3BeginBenignMalloc(); + rx = sqlite3OsFileSize(pWal->apWalFd[0], &sz); + if( rx==SQLITE_OK && (sz > nMax ) ){ + rx = sqlite3OsTruncate(pWal->apWalFd[0], nMax); + } + sqlite3EndBenignMalloc(); + if( rx ){ + sqlite3_log(rx, "cannot limit WAL size: %s", pWal->zWalName); + } } } @@ -1944,39 +2274,50 @@ int sqlite3WalClose( if( zBuf!=0 && SQLITE_OK==(rc = sqlite3OsLock(pWal->pDbFd, SQLITE_LOCK_EXCLUSIVE)) ){ + int i; if( pWal->exclusiveMode==WAL_NORMAL_MODE ){ pWal->exclusiveMode = WAL_EXCLUSIVE_MODE; } - rc = sqlite3WalCheckpoint(pWal, db, - SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0 - ); - if( rc==SQLITE_OK ){ - int bPersist = -1; - sqlite3OsFileControlHint( - pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist + for(i=0; rc==SQLITE_OK && i<2; i++){ + rc = sqlite3WalCheckpoint(pWal, db, + SQLITE_CHECKPOINT_PASSIVE, 0, 0, sync_flags, nBuf, zBuf, 0, 0 ); - if( bPersist!=1 ){ - /* Try to delete the WAL file if the checkpoint completed and - ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal - ** mode (!bPersist) */ - isDelete = 1; - }else if( pWal->mxWalSize>=0 ){ - /* Try to truncate the WAL file to zero bytes if the checkpoint - ** completed and fsynced (rc==SQLITE_OK) and we are in persistent - ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a - ** non-negative value (pWal->mxWalSize>=0). Note that we truncate - ** to zero bytes as truncating to the journal_size_limit might - ** leave a corrupt WAL file on disk. */ - walLimitSize(pWal, 0); + if( rc==SQLITE_OK ){ + int bPersist = -1; + sqlite3OsFileControlHint( + pWal->pDbFd, SQLITE_FCNTL_PERSIST_WAL, &bPersist + ); + if( bPersist!=1 ){ + /* Try to delete the WAL file if the checkpoint completed and + ** fsyned (rc==SQLITE_OK) and if we are not in persistent-wal + ** mode (!bPersist) */ + isDelete = 1; + }else if( pWal->mxWalSize>=0 ){ + /* Try to truncate the WAL file to zero bytes if the checkpoint + ** completed and fsynced (rc==SQLITE_OK) and we are in persistent + ** WAL mode (bPersist) and if the PRAGMA journal_size_limit is a + ** non-negative value (pWal->mxWalSize>=0). Note that we truncate + ** to zero bytes as truncating to the journal_size_limit might + ** leave a corrupt WAL file on disk. */ + walLimitSize(pWal, 0); + } } + + if( isWalMode2(pWal)==0 ) break; + + walCkptInfo(pWal)->nBackfill = 0; + walidxSetFile(&pWal->hdr, !walidxGetFile(&pWal->hdr)); + pWal->writeLock = 1; + walIndexWriteHdr(pWal); + pWal->writeLock = 0; } } walIndexClose(pWal, isDelete); - sqlite3OsClose(pWal->pWalFd); if( isDelete ){ sqlite3BeginBenignMalloc(); sqlite3OsDelete(pWal->pVfs, pWal->zWalName, 0); + sqlite3OsDelete(pWal->pVfs, pWal->zWalName2, 0); sqlite3EndBenignMalloc(); } WALTRACE(("WAL%p: closed\n", pWal)); @@ -2115,7 +2456,9 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){ ** sure the wal-index was not constructed with some future format that ** this version of SQLite cannot understand. */ - if( badHdr==0 && pWal->hdr.iVersion!=WALINDEX_MAX_VERSION ){ + if( badHdr==0 + && pWal->hdr.iVersion!=WAL_VERSION1 && pWal->hdr.iVersion!=WAL_VERSION2 + ){ rc = SQLITE_CANTOPEN_BKPT; } @@ -2180,13 +2523,9 @@ static int walIndexReadHdr(Wal *pWal, int *pChanged){ */ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ volatile WalCkptInfo *pInfo; /* Checkpoint information in wal-index */ - u32 mxReadMark; /* Largest aReadMark[] value */ - int mxI; /* Index of largest aReadMark[] value */ - int i; /* Loop counter */ int rc = SQLITE_OK; /* Return code */ - u32 mxFrame; /* Wal frame to lock to */ - assert( pWal->readLock<0 ); /* Not currently locked */ + assert( pWal->readLock==WAL_LOCK_NONE ); /* Not currently locked */ /* Take steps to avoid spinning forever if there is a protocol error. ** @@ -2248,131 +2587,156 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ } pInfo = walCkptInfo(pWal); - if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame + if( isWalMode2(pWal) ){ + int eLock = 1 + (walidxGetFile(&pWal->hdr)*2); + if( pInfo->nBackfill==0 ){ + eLock += walidxGetMxFrame(&pWal->hdr, !walidxGetFile(&pWal->hdr))>0; + } + rc = walLockReader(pWal, eLock, 1); + if( rc!=SQLITE_OK ){ + return rc; + } + + walShmBarrier(pWal); + if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){ + walLockReader(pWal, eLock, 0); + return WAL_RETRY; + }else{ + pWal->readLock = eLock; + } + assert( pWal->minFrame==0 && walFramePage(pWal->minFrame)==0 ); + }else{ + u32 mxReadMark; /* Largest aReadMark[] value */ + int mxI; /* Index of largest aReadMark[] value */ + int i; /* Loop counter */ + u32 mxFrame; /* Wal frame to lock to */ + + if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame #ifdef SQLITE_ENABLE_SNAPSHOT - && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0 - || 0==memcmp(&pWal->hdr, pWal->pSnapshot, sizeof(WalIndexHdr))) + && (pWal->pSnapshot==0 || pWal->hdr.mxFrame==0 + || 0==memcmp(&pWal->hdr, pWal->pSnapshot, sizeof(WalIndexHdr))) #endif - ){ - /* The WAL has been completely backfilled (or it is empty). - ** and can be safely ignored. - */ - rc = walLockShared(pWal, WAL_READ_LOCK(0)); - walShmBarrier(pWal); - if( rc==SQLITE_OK ){ - if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){ - /* It is not safe to allow the reader to continue here if frames - ** may have been appended to the log before READ_LOCK(0) was obtained. - ** When holding READ_LOCK(0), the reader ignores the entire log file, - ** which implies that the database file contains a trustworthy - ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from - ** happening, this is usually correct. - ** - ** However, if frames have been appended to the log (or if the log - ** is wrapped and written for that matter) before the READ_LOCK(0) - ** is obtained, that is not necessarily true. A checkpointer may - ** have started to backfill the appended frames but crashed before - ** it finished. Leaving a corrupt image in the database file. - */ - walUnlockShared(pWal, WAL_READ_LOCK(0)); - return WAL_RETRY; + ){ + /* The WAL has been completely backfilled (or it is empty). + ** and can be safely ignored. + */ + rc = walLockShared(pWal, WAL_READ_LOCK(0)); + walShmBarrier(pWal); + if( rc==SQLITE_OK ){ + if( memcmp((void*)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){ + /* It is not safe to allow the reader to continue here if frames + ** may have been appended to the log before READ_LOCK(0) was obtained. + ** When holding READ_LOCK(0), the reader ignores the entire log file, + ** which implies that the database file contains a trustworthy + ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from + ** happening, this is usually correct. + ** + ** However, if frames have been appended to the log (or if the log + ** is wrapped and written for that matter) before the READ_LOCK(0) + ** is obtained, that is not necessarily true. A checkpointer may + ** have started to backfill the appended frames but crashed before + ** it finished. Leaving a corrupt image in the database file. + */ + walUnlockShared(pWal, WAL_READ_LOCK(0)); + return WAL_RETRY; + } + pWal->readLock = 0; + return SQLITE_OK; + }else if( rc!=SQLITE_BUSY ){ + return rc; } - pWal->readLock = 0; - return SQLITE_OK; - }else if( rc!=SQLITE_BUSY ){ - return rc; } - } - /* If we get this far, it means that the reader will want to use - ** the WAL to get at content from recent commits. The job now is - ** to select one of the aReadMark[] entries that is closest to - ** but not exceeding pWal->hdr.mxFrame and lock that entry. - */ - mxReadMark = 0; - mxI = 0; - mxFrame = pWal->hdr.mxFrame; + /* If we get this far, it means that the reader will want to use + ** the WAL to get at content from recent commits. The job now is + ** to select one of the aReadMark[] entries that is closest to + ** but not exceeding pWal->hdr.mxFrame and lock that entry. + */ + mxReadMark = 0; + mxI = 0; + mxFrame = pWal->hdr.mxFrame; #ifdef SQLITE_ENABLE_SNAPSHOT - if( pWal->pSnapshot && pWal->pSnapshot->mxFramepSnapshot->mxFrame; - } -#endif - for(i=1; iaReadMark[i]; - if( mxReadMark<=thisMark && thisMark<=mxFrame ){ - assert( thisMark!=READMARK_NOT_USED ); - mxReadMark = thisMark; - mxI = i; + if( pWal->pSnapshot && pWal->pSnapshot->mxFramepSnapshot->mxFrame; } - } - if( (pWal->readOnly & WAL_SHM_RDONLY)==0 - && (mxReadMarkaReadMark[i] = mxFrame; + u32 thisMark = pInfo->aReadMark[i]; + if( mxReadMark<=thisMark && thisMark<=mxFrame ){ + assert( thisMark!=READMARK_NOT_USED ); + mxReadMark = thisMark; mxI = i; - walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); - break; - }else if( rc!=SQLITE_BUSY ){ - return rc; } } - } - if( mxI==0 ){ - assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); - return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK; - } + if( (pWal->readOnly & WAL_SHM_RDONLY)==0 + && (mxReadMarkaReadMark[i] = mxFrame; + mxI = i; + walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); + break; + }else if( rc!=SQLITE_BUSY ){ + return rc; + } + } + } + if( mxI==0 ){ + assert( rc==SQLITE_BUSY || (pWal->readOnly & WAL_SHM_RDONLY)!=0 ); + return rc==SQLITE_BUSY ? WAL_RETRY : SQLITE_READONLY_CANTLOCK; + } - rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); - if( rc ){ - return rc==SQLITE_BUSY ? WAL_RETRY : rc; - } - /* Now that the read-lock has been obtained, check that neither the - ** value in the aReadMark[] array or the contents of the wal-index - ** header have changed. - ** - ** It is necessary to check that the wal-index header did not change - ** between the time it was read and when the shared-lock was obtained - ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility - ** that the log file may have been wrapped by a writer, or that frames - ** that occur later in the log than pWal->hdr.mxFrame may have been - ** copied into the database by a checkpointer. If either of these things - ** happened, then reading the database with the current value of - ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry - ** instead. - ** - ** Before checking that the live wal-index header has not changed - ** since it was read, set Wal.minFrame to the first frame in the wal - ** file that has not yet been checkpointed. This client will not need - ** to read any frames earlier than minFrame from the wal file - they - ** can be safely read directly from the database file. - ** - ** Because a ShmBarrier() call is made between taking the copy of - ** nBackfill and checking that the wal-header in shared-memory still - ** matches the one cached in pWal->hdr, it is guaranteed that the - ** checkpointer that set nBackfill was not working with a wal-index - ** header newer than that cached in pWal->hdr. If it were, that could - ** cause a problem. The checkpointer could omit to checkpoint - ** a version of page X that lies before pWal->minFrame (call that version - ** A) on the basis that there is a newer version (version B) of the same - ** page later in the wal file. But if version B happens to like past - ** frame pWal->hdr.mxFrame - then the client would incorrectly assume - ** that it can read version A from the database file. However, since - ** we can guarantee that the checkpointer that set nBackfill could not - ** see any pages past pWal->hdr.mxFrame, this problem does not come up. - */ - pWal->minFrame = pInfo->nBackfill+1; - walShmBarrier(pWal); - if( pInfo->aReadMark[mxI]!=mxReadMark - || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) - ){ - walUnlockShared(pWal, WAL_READ_LOCK(mxI)); - return WAL_RETRY; - }else{ - assert( mxReadMark<=pWal->hdr.mxFrame ); - pWal->readLock = (i16)mxI; + rc = walLockShared(pWal, WAL_READ_LOCK(mxI)); + if( rc ){ + return rc==SQLITE_BUSY ? WAL_RETRY : rc; + } + /* Now that the read-lock has been obtained, check that neither the + ** value in the aReadMark[] array or the contents of the wal-index + ** header have changed. + ** + ** It is necessary to check that the wal-index header did not change + ** between the time it was read and when the shared-lock was obtained + ** on WAL_READ_LOCK(mxI) was obtained to account for the possibility + ** that the log file may have been wrapped by a writer, or that frames + ** that occur later in the log than pWal->hdr.mxFrame may have been + ** copied into the database by a checkpointer. If either of these things + ** happened, then reading the database with the current value of + ** pWal->hdr.mxFrame risks reading a corrupted snapshot. So, retry + ** instead. + ** + ** Before checking that the live wal-index header has not changed + ** since it was read, set Wal.minFrame to the first frame in the wal + ** file that has not yet been checkpointed. This client will not need + ** to read any frames earlier than minFrame from the wal file - they + ** can be safely read directly from the database file. + ** + ** Because a ShmBarrier() call is made between taking the copy of + ** nBackfill and checking that the wal-header in shared-memory still + ** matches the one cached in pWal->hdr, it is guaranteed that the + ** checkpointer that set nBackfill was not working with a wal-index + ** header newer than that cached in pWal->hdr. If it were, that could + ** cause a problem. The checkpointer could omit to checkpoint + ** a version of page X that lies before pWal->minFrame (call that version + ** A) on the basis that there is a newer version (version B) of the same + ** page later in the wal file. But if version B happens to like past + ** frame pWal->hdr.mxFrame - then the client would incorrectly assume + ** that it can read version A from the database file. However, since + ** we can guarantee that the checkpointer that set nBackfill could not + ** see any pages past pWal->hdr.mxFrame, this problem does not come up. + */ + pWal->minFrame = pInfo->nBackfill+1; + walShmBarrier(pWal); + if( pInfo->aReadMark[mxI]!=mxReadMark + || memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) + ){ + walUnlockShared(pWal, WAL_READ_LOCK(mxI)); + return WAL_RETRY; + }else{ + assert( mxReadMark<=pWal->hdr.mxFrame ); + pWal->readLock = (i16)mxI; + } } return rc; } @@ -2488,6 +2852,10 @@ int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ testcase( (rc&0xff)==SQLITE_IOERR ); testcase( rc==SQLITE_PROTOCOL ); testcase( rc==SQLITE_OK ); + + if( rc==SQLITE_OK && pWal->hdr.iVersion==WAL_VERSION2 ){ + rc = walOpenWal2(pWal); + } #ifdef SQLITE_ENABLE_SNAPSHOT if( rc==SQLITE_OK ){ @@ -2561,12 +2929,75 @@ int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ */ void sqlite3WalEndReadTransaction(Wal *pWal){ sqlite3WalEndWriteTransaction(pWal); - if( pWal->readLock>=0 ){ - walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); - pWal->readLock = -1; + if( pWal->readLock!=WAL_LOCK_NONE ){ + if( isWalMode2(pWal) ){ + (void)walLockReader(pWal, pWal->readLock, 0); + }else{ + walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); + } + pWal->readLock = WAL_LOCK_NONE; } } +/* Search hash table iHash for an entry matching page number +** pgno. Each call to this function searches a single hash table +** (each hash table indexes up to HASHTABLE_NPAGE frames). +** +** This code might run concurrently to the code in walIndexAppend() +** that adds entries to the wal-index (and possibly to this hash +** table). This means the value just read from the hash +** slot (aHash[iKey]) may have been added before or after the +** current read transaction was opened. Values added after the +** read transaction was opened may have been written incorrectly - +** i.e. these slots may contain garbage data. However, we assume +** that any slots written before the current read transaction was +** opened remain unmodified. +** +** For the reasons above, the if(...) condition featured in the inner +** loop of the following block is more stringent that would be required +** if we had exclusive access to the hash-table: +** +** (aPgno[iFrame]==pgno): +** This condition filters out normal hash-table collisions. +** +** (iFrame<=iLast): +** This condition filters out entries that were added to the hash +** table after the current read-transaction had started. +*/ +static int walSearchHash( + Wal *pWal, + u32 iLast, + int iHash, + Pgno pgno, + u32 *piRead +){ + volatile ht_slot *aHash; /* Pointer to hash table */ + volatile u32 *aPgno; /* Pointer to array of page numbers */ + u32 iZero; /* Frame number corresponding to aPgno[0] */ + int iKey; /* Hash slot index */ + int nCollide; /* Number of hash collisions remaining */ + int rc; /* Error code */ + + rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero); + if( rc!=SQLITE_OK ){ + return rc; + } + nCollide = HASHTABLE_NSLOT; + for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){ + u32 iFrame = aHash[iKey] + iZero; + if( iFrame<=iLast && iFrame>=pWal->minFrame && aPgno[aHash[iKey]]==pgno ){ + assert( iFrame>*piRead || CORRUPT_DB ); + *piRead = iFrame; + } + if( (nCollide--)==0 ){ + return SQLITE_CORRUPT_BKPT; + } + } + + return SQLITE_OK; +} + + /* ** Search the wal file for page pgno. If found, set *piRead to the frame that ** contains the page. Otherwise, if pgno is not in the wal file, set *piRead @@ -2580,80 +3011,74 @@ int sqlite3WalFindFrame( Pgno pgno, /* Database page number to read data for */ u32 *piRead /* OUT: Frame number (or zero) */ ){ + int bWal2 = isWalMode2(pWal); + int iApp = walidxGetFile(&pWal->hdr); + int rc = SQLITE_OK; u32 iRead = 0; /* If !=0, WAL frame to return data from */ - u32 iLast = pWal->hdr.mxFrame; /* Last page in WAL for this reader */ + u32 iLast; /* Last frame in wal file */ int iHash; /* Used to loop through N hash tables */ - int iMinHash; /* This routine is only be called from within a read transaction. */ - assert( pWal->readLock>=0 || pWal->lockError ); + assert( pWal->readLock!=WAL_LOCK_NONE ); - /* If the "last page" field of the wal-index header snapshot is 0, then - ** no data will be read from the wal under any circumstances. Return early - ** in this case as an optimization. Likewise, if pWal->readLock==0, - ** then the WAL is ignored by the reader so return early, as if the - ** WAL were empty. - */ - if( iLast==0 || pWal->readLock==0 ){ - *piRead = 0; - return SQLITE_OK; + /* If this is a wal2 system, the client must have a partial-wal lock + ** on wal file iApp. Or if it is a wal system, iApp==0 must be true. */ + assert( bWal2==0 || iApp==1 + || pWal->readLock==WAL_LOCK_PART1 || pWal->readLock==WAL_LOCK_PART1_FULL2 + ); + assert( bWal2==0 || iApp==0 + || pWal->readLock==WAL_LOCK_PART2 || pWal->readLock==WAL_LOCK_PART2_FULL1 + ); + assert( bWal2 || iApp==0 ); + + /* Search the wal file that the client holds a partial lock on first */ + iLast = walidxGetMxFrame(&pWal->hdr, iApp); + if( iLast ){ + u32 iExternal = bWal2 ? walExternalEncode(iApp, iLast) : iLast; + int iMinHash = walFramePage(pWal->minFrame); + for(iHash=walFramePage(iExternal); + iHash>=iMinHash && iRead==0; + iHash-=(1+bWal2) + ){ + rc = walSearchHash(pWal, iExternal, iHash, pgno, &iRead); + if( rc!=SQLITE_OK ) break; + } } - /* Search the hash table or tables for an entry matching page number - ** pgno. Each iteration of the following for() loop searches one - ** hash table (each hash table indexes up to HASHTABLE_NPAGE frames). - ** - ** This code might run concurrently to the code in walIndexAppend() - ** that adds entries to the wal-index (and possibly to this hash - ** table). This means the value just read from the hash - ** slot (aHash[iKey]) may have been added before or after the - ** current read transaction was opened. Values added after the - ** read transaction was opened may have been written incorrectly - - ** i.e. these slots may contain garbage data. However, we assume - ** that any slots written before the current read transaction was - ** opened remain unmodified. - ** - ** For the reasons above, the if(...) condition featured in the inner - ** loop of the following block is more stringent that would be required - ** if we had exclusive access to the hash-table: - ** - ** (aPgno[iFrame]==pgno): - ** This condition filters out normal hash-table collisions. - ** - ** (iFrame<=iLast): - ** This condition filters out entries that were added to the hash - ** table after the current read-transaction had started. - */ - iMinHash = walFramePage(pWal->minFrame); - for(iHash=walFramePage(iLast); iHash>=iMinHash && iRead==0; iHash--){ - volatile ht_slot *aHash; /* Pointer to hash table */ - volatile u32 *aPgno; /* Pointer to array of page numbers */ - u32 iZero; /* Frame number corresponding to aPgno[0] */ - int iKey; /* Hash slot index */ - int nCollide; /* Number of hash collisions remaining */ - int rc; /* Error code */ - - rc = walHashGet(pWal, iHash, &aHash, &aPgno, &iZero); - if( rc!=SQLITE_OK ){ - return rc; - } - nCollide = HASHTABLE_NSLOT; - for(iKey=walHash(pgno); aHash[iKey]; iKey=walNextHash(iKey)){ - u32 iFrame = aHash[iKey] + iZero; - if( iFrame<=iLast && iFrame>=pWal->minFrame && aPgno[aHash[iKey]]==pgno ){ - assert( iFrame>iRead || CORRUPT_DB ); - iRead = iFrame; - } - if( (nCollide--)==0 ){ - return SQLITE_CORRUPT_BKPT; + /* If the requested page was not found, no error has occured, and + ** the client holds a full-wal lock on the other wal file, search it + ** too. */ + if( rc==SQLITE_OK && bWal2 && iRead==0 && ( + pWal->readLock==WAL_LOCK_PART1_FULL2 + || pWal->readLock==WAL_LOCK_PART2_FULL1 + )){ + iLast = walidxGetMxFrame(&pWal->hdr, !iApp); + if( iLast ){ + u32 iExternal = walExternalEncode(!iApp, iLast); + for(iHash=walFramePage2(!iApp, iLast); iHash>=0 && iRead==0; iHash -= 2){ + rc = walSearchHash(pWal, iExternal, iHash, pgno, &iRead); + if( rc!=SQLITE_OK ) break; } } } +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) + if( iRead ){ + u32 iFrame; + int iWal = walExternalDecode(iRead, &iFrame); + WALTRACE(("WAL%p: page %d @ frame %d wal %d\n",pWal,(int)pgno,iFrame,iWal)); + }else{ + WALTRACE(("WAL%p: page %d not found\n", pWal, (int)pgno)); + } +#endif + #ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT /* If expensive assert() statements are available, do a linear search ** of the wal-index file content. Make sure the results agree with the - ** result obtained using the hash indexes above. */ + ** result obtained using the hash indexes above. + ** + ** TODO: This is broken for wal2. + */ { u32 iRead2 = 0; u32 iTest; @@ -2679,26 +3104,40 @@ int sqlite3WalFindFrame( */ int sqlite3WalReadFrame( Wal *pWal, /* WAL handle */ - u32 iRead, /* Frame to read */ + u32 iExternal, /* Frame to read */ int nOut, /* Size of buffer pOut in bytes */ u8 *pOut /* Buffer to write page data to */ ){ int sz; + int iWal = 0; + u32 iRead; i64 iOffset; + + /* Figure out the page size */ sz = pWal->hdr.szPage; sz = (sz&0xfe00) + ((sz&0x0001)<<16); testcase( sz<=32768 ); testcase( sz>=65536 ); + + if( isWalMode2(pWal) ){ + /* Figure out which of the two wal files, and the frame within, that + ** iExternal refers to. */ + iWal = walExternalDecode(iExternal, &iRead); + }else{ + iRead = iExternal; + } + + WALTRACE(("WAL%p: reading frame %d wal %d\n", pWal, iRead, iWal)); iOffset = walFrameOffset(iRead, sz) + WAL_FRAME_HDRSIZE; /* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL */ - return sqlite3OsRead(pWal->pWalFd, pOut, (nOut>sz ? sz : nOut), iOffset); + return sqlite3OsRead(pWal->apWalFd[iWal], pOut, (nOut>sz?sz:nOut), iOffset); } /* ** Return the size of the database in pages (or zero, if unknown). */ Pgno sqlite3WalDbsize(Wal *pWal){ - if( pWal && ALWAYS(pWal->readLock>=0) ){ + if( pWal && ALWAYS(pWal->readLock!=WAL_LOCK_NONE) ){ return pWal->hdr.nPage; } return 0; @@ -2723,7 +3162,7 @@ int sqlite3WalBeginWriteTransaction(Wal *pWal){ /* Cannot start a write transaction without first holding a read ** transaction. */ - assert( pWal->readLock>=0 ); + assert( pWal->readLock!=WAL_LOCK_NONE ); assert( pWal->writeLock==0 && pWal->iReCksum==0 ); if( pWal->readOnly ){ @@ -2781,18 +3220,21 @@ int sqlite3WalEndWriteTransaction(Wal *pWal){ int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){ int rc = SQLITE_OK; if( ALWAYS(pWal->writeLock) ){ - Pgno iMax = pWal->hdr.mxFrame; + int iWal = walidxGetFile(&pWal->hdr); + Pgno iMax = walidxGetMxFrame(&pWal->hdr, iWal); + Pgno iNew; Pgno iFrame; - + + assert( isWalMode2(pWal) || iWal==0 ); + /* Restore the clients cache of the wal-index header to the state it ** was in before the client began writing to the database. */ memcpy(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr)); + assert( walidxGetFile(&pWal->hdr)==iWal ); + iNew = walidxGetMxFrame(&pWal->hdr, walidxGetFile(&pWal->hdr)); - for(iFrame=pWal->hdr.mxFrame+1; - ALWAYS(rc==SQLITE_OK) && iFrame<=iMax; - iFrame++ - ){ + for(iFrame=iNew+1; ALWAYS(rc==SQLITE_OK) && iFrame<=iMax; iFrame++){ /* This call cannot fail. Unless the page for which the page number ** is passed as the second argument is (a) in the cache and ** (b) has an outstanding reference, then xUndo is either a no-op @@ -2804,10 +3246,16 @@ int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){ ** page 1 is never written to the log until the transaction is ** committed. As a result, the call to xUndo may not fail. */ - assert( walFramePgno(pWal, iFrame)!=1 ); - rc = xUndo(pUndoCtx, walFramePgno(pWal, iFrame)); + Pgno pgno; + if( isWalMode2(pWal) ){ + pgno = walFramePgno2(pWal, iWal, iFrame); + }else{ + pgno = walFramePgno(pWal, iFrame); + } + assert( pgno!=1 ); + rc = xUndo(pUndoCtx, pgno); } - if( iMax!=pWal->hdr.mxFrame ) walCleanupHash(pWal); + if( iMax!=iNew ) walCleanupHash(pWal); } return rc; } @@ -2819,11 +3267,13 @@ int sqlite3WalUndo(Wal *pWal, int (*xUndo)(void *, Pgno), void *pUndoCtx){ ** point in the event of a savepoint rollback (via WalSavepointUndo()). */ void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){ + int iWal = walidxGetFile(&pWal->hdr); assert( pWal->writeLock ); - aWalData[0] = pWal->hdr.mxFrame; + assert( isWalMode2(pWal) || iWal==0 ); + aWalData[0] = walidxGetMxFrame(&pWal->hdr, iWal); aWalData[1] = pWal->hdr.aFrameCksum[0]; aWalData[2] = pWal->hdr.aFrameCksum[1]; - aWalData[3] = pWal->nCkpt; + aWalData[3] = isWalMode2(pWal) ? iWal : pWal->nCkpt; } /* @@ -2834,21 +3284,24 @@ void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){ */ int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){ int rc = SQLITE_OK; + int iWal = walidxGetFile(&pWal->hdr); + int iCmp = isWalMode2(pWal) ? iWal : pWal->nCkpt; assert( pWal->writeLock ); - assert( aWalData[3]!=pWal->nCkpt || aWalData[0]<=pWal->hdr.mxFrame ); + assert( isWalMode2(pWal) || iWal==0 ); + assert( aWalData[3]!=iCmp || aWalData[0]<=walidxGetMxFrame(&pWal->hdr,iWal) ); - if( aWalData[3]!=pWal->nCkpt ){ + if( aWalData[3]!=iCmp ){ /* This savepoint was opened immediately after the write-transaction ** was started. Right after that, the writer decided to wrap around ** to the start of the log. Update the savepoint values to match. */ aWalData[0] = 0; - aWalData[3] = pWal->nCkpt; + aWalData[3] = iCmp; } - if( aWalData[0]hdr.mxFrame ){ - pWal->hdr.mxFrame = aWalData[0]; + if( aWalData[0]hdr, iWal) ){ + walidxSetMxFrame(&pWal->hdr, iWal, aWalData[0]); pWal->hdr.aFrameCksum[0] = aWalData[1]; pWal->hdr.aFrameCksum[1] = aWalData[2]; walCleanupHash(pWal); @@ -2857,23 +3310,80 @@ int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){ return rc; } +/* +** This function is used in wal2 mode. +** +** This function is called when writer pWal is just about to start +** writing out frames. The "other" wal file (wal file !pWal->hdr.iAppend) +** has been fully checkpointed. This function returns SQLITE_OK if there +** are no readers preventing the writer from switching to the other wal +** file. Or SQLITE_BUSY if there are. +*/ +static int walRestartOk(Wal *pWal){ + int rc; /* Return code */ + int iApp = walidxGetFile(&pWal->hdr); /* Current WAL file */ + + /* No reader can be doing a "partial" read of wal file !iApp - in that + ** case it would not have been possible to checkpoint the file. So + ** it is only necessary to test for "full" readers. See the comment + ** above walLockReader() function for exactly what this means in terms + ** of locks. */ + int i = (iApp==0) ? 2 : 4; + + rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); + if( rc==SQLITE_OK ){ + walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); + } + return rc; +} + /* ** This function is called just before writing a set of frames to the log ** file (see sqlite3WalFrames()). It checks to see if, instead of appending -** to the current log file, it is possible to overwrite the start of the -** existing log file with the new frames (i.e. "reset" the log). If so, -** it sets pWal->hdr.mxFrame to 0. Otherwise, pWal->hdr.mxFrame is left -** unchanged. +** to the current log file, it is possible and desirable to switch to the +** other log file and write the new transaction to the start of it. +** If so, the wal-index header is updated accordingly - both in heap memory +** and in the *-shm file. ** ** SQLITE_OK is returned if no error is encountered (regardless of whether -** or not pWal->hdr.mxFrame is modified). An SQLite error code is returned +** or not the wal-index header is modified). An SQLite error code is returned ** if an error occurs. */ static int walRestartLog(Wal *pWal){ int rc = SQLITE_OK; - int cnt; - if( pWal->readLock==0 ){ + if( isWalMode2(pWal) ){ + int iApp = walidxGetFile(&pWal->hdr); + int nWalSize = WAL_DEFAULT_WALSIZE; + if( pWal->mxWalSize>0 ){ + nWalSize = (pWal->mxWalSize-WAL_HDRSIZE+pWal->szPage+WAL_FRAME_HDRSIZE-1) + / (pWal->szPage+WAL_FRAME_HDRSIZE); + nWalSize = MAX(nWalSize, 1); + } + + if( walidxGetMxFrame(&pWal->hdr, iApp)>=nWalSize ){ + volatile WalCkptInfo *pInfo = walCkptInfo(pWal); + if( walidxGetMxFrame(&pWal->hdr, !iApp)==0 || pInfo->nBackfill ){ + rc = walRestartOk(pWal); + if( rc==SQLITE_OK ){ + iApp = !iApp; + pWal->nCkpt++; + walidxSetFile(&pWal->hdr, iApp); + walidxSetMxFrame(&pWal->hdr, iApp, 0); + sqlite3Put4byte((u8*)&pWal->hdr.aSalt[0], pWal->hdr.aFrameCksum[0]); + sqlite3Put4byte((u8*)&pWal->hdr.aSalt[1], pWal->hdr.aFrameCksum[1]); + walIndexWriteHdr(pWal); + pInfo->nBackfill = 0; + walLockReader(pWal, pWal->readLock, 0); + pWal->readLock = iApp ? WAL_LOCK_PART2_FULL1 : WAL_LOCK_PART1_FULL2; + rc = walLockReader(pWal, pWal->readLock, 1); + }else if( rc==SQLITE_BUSY ){ + rc = SQLITE_OK; + } + } + } + }else if( pWal->readLock==0 ){ + int cnt; volatile WalCkptInfo *pInfo = walCkptInfo(pWal); assert( pInfo->nBackfill==pWal->hdr.mxFrame ); if( pInfo->nBackfill>0 ){ @@ -2897,7 +3407,7 @@ static int walRestartLog(Wal *pWal){ } } walUnlockShared(pWal, WAL_READ_LOCK(0)); - pWal->readLock = -1; + pWal->readLock = WAL_LOCK_NONE; cnt = 0; do{ int notUsed; @@ -2908,6 +3418,7 @@ static int walRestartLog(Wal *pWal){ testcase( rc==SQLITE_PROTOCOL ); testcase( rc==SQLITE_OK ); } + return rc; } @@ -2966,6 +3477,18 @@ static int walWriteOneFrame( int rc; /* Result code from subfunctions */ void *pData; /* Data actually written */ u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-header in */ + +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) + { + int iWal = walidxGetFile(&p->pWal->hdr); + int iFrame = 1 + (iOffset / (WAL_FRAME_HDRSIZE + p->pWal->szPage)); + assert( p->pWal->apWalFd[iWal]==p->pFd ); + WALTRACE(("WAL%p: page %d written to frame %d of wal %d\n", + p->pWal, (int)pPage->pgno, iFrame, iWal + )); + } +#endif + #if defined(SQLITE_HAS_CODEC) if( (pData = sqlite3PagerCodec(pPage))==0 ) return SQLITE_NOMEM_BKPT; #else @@ -2988,13 +3511,15 @@ static int walWriteOneFrame( ** SQLITE_OK is returned if successful, or an SQLite error code otherwise. */ static int walRewriteChecksums(Wal *pWal, u32 iLast){ - const int szPage = pWal->szPage;/* Database page size */ int rc = SQLITE_OK; /* Return code */ + const int szPage = pWal->szPage;/* Database page size */ u8 *aBuf; /* Buffer to load data from wal file into */ u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */ u32 iRead; /* Next frame to read from wal file */ i64 iCksumOff; + assert( isWalMode2(pWal)==0 ); + aBuf = sqlite3_malloc(szPage + WAL_FRAME_HDRSIZE); if( aBuf==0 ) return SQLITE_NOMEM_BKPT; @@ -3009,7 +3534,7 @@ static int walRewriteChecksums(Wal *pWal, u32 iLast){ }else{ iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16; } - rc = sqlite3OsRead(pWal->pWalFd, aBuf, sizeof(u32)*2, iCksumOff); + rc = sqlite3OsRead(pWal->apWalFd[0], aBuf, sizeof(u32)*2, iCksumOff); pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf); pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]); @@ -3017,14 +3542,14 @@ static int walRewriteChecksums(Wal *pWal, u32 iLast){ pWal->iReCksum = 0; for(; rc==SQLITE_OK && iRead<=iLast; iRead++){ i64 iOff = walFrameOffset(iRead, szPage); - rc = sqlite3OsRead(pWal->pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff); + rc = sqlite3OsRead(pWal->apWalFd[0], aBuf, szPage+WAL_FRAME_HDRSIZE, iOff); if( rc==SQLITE_OK ){ u32 iPgno, nDbSize; iPgno = sqlite3Get4byte(aBuf); nDbSize = sqlite3Get4byte(&aBuf[4]); walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE], aFrame); - rc = sqlite3OsWrite(pWal->pWalFd, aFrame, sizeof(aFrame), iOff); + rc = sqlite3OsWrite(pWal->apWalFd[0], aFrame, sizeof(aFrame), iOff); } } @@ -3054,6 +3579,7 @@ int sqlite3WalFrames( WalWriter w; /* The writer */ u32 iFirst = 0; /* First frame that may be overwritten */ WalIndexHdr *pLive; /* Pointer to shared header */ + int iApp; assert( pList ); assert( pWal->writeLock ); @@ -3062,22 +3588,17 @@ int sqlite3WalFrames( ** nTruncate==0 then this frame set does not complete the transaction. */ assert( (isCommit!=0)==(nTruncate!=0) ); -#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) - { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){} - WALTRACE(("WAL%p: frame write begin. %d frames. mxFrame=%d. %s\n", - pWal, cnt, pWal->hdr.mxFrame, isCommit ? "Commit" : "Spill")); - } -#endif - pLive = (WalIndexHdr*)walIndexHdr(pWal); if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){ - iFirst = pLive->mxFrame+1; + if( isWalMode2(pWal)==0 ){ + iFirst = pLive->mxFrame+1; + } } /* See if it is possible to write these frames into the start of the ** log file, instead of appending to it at pWal->hdr.mxFrame. */ - if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){ + else if( SQLITE_OK!=(rc = walRestartLog(pWal)) ){ return rc; } @@ -3085,28 +3606,50 @@ int sqlite3WalFrames( ** header to the start of the WAL file. See comments at the top of ** this source file for a description of the WAL header format. */ - iFrame = pWal->hdr.mxFrame; + iApp = walidxGetFile(&pWal->hdr); + iFrame = walidxGetMxFrame(&pWal->hdr, iApp); + assert( iApp==0 || isWalMode2(pWal) ); + +#if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) + { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){} + WALTRACE(("WAL%p: frame write begin. %d frames. iWal=%d. mxFrame=%d. %s\n", + pWal, cnt, iApp, iFrame, isCommit ? "Commit" : "Spill")); + } +#endif + if( iFrame==0 ){ + u32 iCkpt = 0; u8 aWalHdr[WAL_HDRSIZE]; /* Buffer to assemble wal-header in */ u32 aCksum[2]; /* Checksum for wal-header */ sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN)); - sqlite3Put4byte(&aWalHdr[4], WAL_MAX_VERSION); + sqlite3Put4byte(&aWalHdr[4], pWal->hdr.iVersion); sqlite3Put4byte(&aWalHdr[8], szPage); - sqlite3Put4byte(&aWalHdr[12], pWal->nCkpt); - if( pWal->nCkpt==0 ) sqlite3_randomness(8, pWal->hdr.aSalt); + if( isWalMode2(pWal) ){ + if( walidxGetMxFrame(&pWal->hdr, !iApp)>0 ){ + u8 aPrev[4]; + rc = sqlite3OsRead(pWal->apWalFd[!iApp], aPrev, 4, 12); + if( rc!=SQLITE_OK ){ + return rc; + } + iCkpt = (sqlite3Get4byte(aPrev) + 1) & 0x0F; + } + }else{ + iCkpt = pWal->nCkpt; + } + sqlite3Put4byte(&aWalHdr[12], iCkpt); memcpy(&aWalHdr[16], pWal->hdr.aSalt, 8); walChecksumBytes(1, aWalHdr, WAL_HDRSIZE-2*4, 0, aCksum); sqlite3Put4byte(&aWalHdr[24], aCksum[0]); sqlite3Put4byte(&aWalHdr[28], aCksum[1]); - + pWal->szPage = szPage; pWal->hdr.bigEndCksum = SQLITE_BIGENDIAN; pWal->hdr.aFrameCksum[0] = aCksum[0]; pWal->hdr.aFrameCksum[1] = aCksum[1]; pWal->truncateOnCommit = 1; - rc = sqlite3OsWrite(pWal->pWalFd, aWalHdr, sizeof(aWalHdr), 0); + rc = sqlite3OsWrite(pWal->apWalFd[iApp], aWalHdr, sizeof(aWalHdr), 0); WALTRACE(("WAL%p: wal-header write %s\n", pWal, rc ? "failed" : "ok")); if( rc!=SQLITE_OK ){ return rc; @@ -3120,7 +3663,7 @@ int sqlite3WalFrames( ** https://sqlite.org/src/info/ff5be73dee */ if( pWal->syncHeader ){ - rc = sqlite3OsSync(pWal->pWalFd, CKPT_SYNC_FLAGS(sync_flags)); + rc = sqlite3OsSync(pWal->apWalFd[iApp], CKPT_SYNC_FLAGS(sync_flags)); if( rc ) return rc; } } @@ -3128,7 +3671,7 @@ int sqlite3WalFrames( /* Setup information needed to write frames into the WAL */ w.pWal = pWal; - w.pFd = pWal->pWalFd; + w.pFd = pWal->apWalFd[iApp]; w.iSyncPoint = 0; w.syncFlags = sync_flags; w.szPage = szPage; @@ -3158,7 +3701,7 @@ int sqlite3WalFrames( #else pData = p->pData; #endif - rc = sqlite3OsWrite(pWal->pWalFd, pData, szPage, iOff); + rc = sqlite3OsWrite(pWal->apWalFd[iApp], pData, szPage, iOff); if( rc ) return rc; p->flags &= ~PGHDR_WAL_APPEND; continue; @@ -3198,7 +3741,7 @@ int sqlite3WalFrames( if( isCommit && WAL_SYNC_FLAGS(sync_flags)!=0 ){ int bSync = 1; if( pWal->padToSectorBoundary ){ - int sectorSize = sqlite3SectorSize(pWal->pWalFd); + int sectorSize = sqlite3SectorSize(w.pFd); w.iSyncPoint = ((iOffset+sectorSize-1)/sectorSize)*sectorSize; bSync = (w.iSyncPoint==iOffset); testcase( bSync ); @@ -3233,16 +3776,16 @@ int sqlite3WalFrames( ** guarantees that there are no other writers, and no data that may ** be in use by existing readers is being overwritten. */ - iFrame = pWal->hdr.mxFrame; + iFrame = walidxGetMxFrame(&pWal->hdr, iApp); for(p=pList; p && rc==SQLITE_OK; p=p->pDirty){ if( (p->flags & PGHDR_WAL_APPEND)==0 ) continue; iFrame++; - rc = walIndexAppend(pWal, iFrame, p->pgno); + rc = walIndexAppend(pWal, iApp, iFrame, p->pgno); } while( rc==SQLITE_OK && nExtra>0 ){ iFrame++; nExtra--; - rc = walIndexAppend(pWal, iFrame, pLast->pgno); + rc = walIndexAppend(pWal, iApp, iFrame, pLast->pgno); } if( rc==SQLITE_OK ){ @@ -3250,7 +3793,7 @@ int sqlite3WalFrames( pWal->hdr.szPage = (u16)((szPage&0xff00) | (szPage>>16)); testcase( szPage<=32768 ); testcase( szPage>=65536 ); - pWal->hdr.mxFrame = iFrame; + walidxSetMxFrame(&pWal->hdr, iApp, iFrame); if( isCommit ){ pWal->hdr.iChange++; pWal->hdr.nPage = nTruncate; @@ -3258,7 +3801,17 @@ int sqlite3WalFrames( /* If this is a commit, update the wal-index header too. */ if( isCommit ){ walIndexWriteHdr(pWal); - pWal->iCallback = iFrame; + if( isWalMode2(pWal) ){ + int iOther = !walidxGetFile(&pWal->hdr); + if( walidxGetMxFrame(&pWal->hdr, iOther) + && !walCkptInfo(pWal)->nBackfill + ){ + pWal->iCallback = walidxGetMxFrame(&pWal->hdr, 0); + pWal->iCallback += walidxGetMxFrame(&pWal->hdr, 1); + } + }else{ + pWal->iCallback = iFrame; + } } } @@ -3350,7 +3903,9 @@ int sqlite3WalCheckpoint( /* Copy data from the log to the database file. */ if( rc==SQLITE_OK ){ - if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){ + if( (walPagesize(pWal)!=nBuf) + && (walidxGetMxFrame(&pWal->hdr, 0) || walidxGetMxFrame(&pWal->hdr, 1)) + ){ rc = SQLITE_CORRUPT_BKPT; }else{ rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf); @@ -3358,8 +3913,20 @@ int sqlite3WalCheckpoint( /* If no error occurred, set the output variables. */ if( rc==SQLITE_OK || rc==SQLITE_BUSY ){ - if( pnLog ) *pnLog = (int)pWal->hdr.mxFrame; - if( pnCkpt ) *pnCkpt = (int)(walCkptInfo(pWal)->nBackfill); + if( pnLog ){ + *pnLog = walidxGetMxFrame(&pWal->hdr,0)+walidxGetMxFrame(&pWal->hdr,1); + } + if( pnCkpt ){ + if( isWalMode2(pWal) ){ + if( (int)(walCkptInfo(pWal)->nBackfill) ){ + *pnCkpt = walidxGetMxFrame(&pWal->hdr, !walidxGetFile(&pWal->hdr)); + }else{ + *pnCkpt = 0; + } + }else{ + *pnCkpt = walCkptInfo(pWal)->nBackfill; + } + } } } @@ -3421,6 +3988,7 @@ int sqlite3WalCallback(Wal *pWal){ */ int sqlite3WalExclusiveMode(Wal *pWal, int op){ int rc; + assert( pWal->writeLock==0 ); assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 ); @@ -3430,13 +3998,18 @@ int sqlite3WalExclusiveMode(Wal *pWal, int op){ ** locks are taken in this case). Nor should the pager attempt to ** upgrade to exclusive-mode following such an error. */ - assert( pWal->readLock>=0 || pWal->lockError ); - assert( pWal->readLock>=0 || (op<=0 && pWal->exclusiveMode==0) ); + assert( pWal->readLock!=WAL_LOCK_NONE || pWal->lockError ); + assert( pWal->readLock!=WAL_LOCK_NONE || (op<=0 && pWal->exclusiveMode==0) ); if( op==0 ){ if( pWal->exclusiveMode ){ pWal->exclusiveMode = 0; - if( walLockShared(pWal, WAL_READ_LOCK(pWal->readLock))!=SQLITE_OK ){ + if( isWalMode2(pWal) ){ + rc = walLockReader(pWal, pWal->readLock, 1); + }else{ + rc = walLockShared(pWal, WAL_READ_LOCK(pWal->readLock)); + } + if( rc==SQLITE_OK ){ pWal->exclusiveMode = 1; } rc = pWal->exclusiveMode==0; @@ -3447,7 +4020,11 @@ int sqlite3WalExclusiveMode(Wal *pWal, int op){ }else if( op>0 ){ assert( pWal->exclusiveMode==0 ); assert( pWal->readLock>=0 ); - walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); + if( isWalMode2(pWal) ){ + walLockReader(pWal, pWal->readLock, 0); + }else{ + walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); + } pWal->exclusiveMode = 1; rc = 1; }else{ @@ -3531,7 +4108,7 @@ int sqlite3WalFramesize(Wal *pWal){ /* Return the sqlite3_file object for the WAL file */ sqlite3_file *sqlite3WalFile(Wal *pWal){ - return pWal->pWalFd; + return pWal->apWalFd[0]; } #endif /* #ifndef SQLITE_OMIT_WAL */ diff --git a/src/wal.h b/src/wal.h index d97300a684..29ddd7eb0b 100644 --- a/src/wal.h +++ b/src/wal.h @@ -26,7 +26,7 @@ #define CKPT_SYNC_FLAGS(X) (((X)>>2)&0x03) #ifdef SQLITE_OMIT_WAL -# define sqlite3WalOpen(x,y,z) 0 +# define sqlite3WalOpen(w,x,y,z) 0 # define sqlite3WalLimit(x,y) # define sqlite3WalClose(v,w,x,y,z) 0 # define sqlite3WalBeginReadTransaction(y,z) 0 @@ -55,7 +55,7 @@ typedef struct Wal Wal; /* Open and close a connection to a write-ahead log. */ -int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *, int, i64, Wal**); +int sqlite3WalOpen(sqlite3_vfs*, sqlite3_file*, const char *,int,i64,int,Wal**); int sqlite3WalClose(Wal *pWal, sqlite3*, int sync_flags, int, u8 *); /* Set the limiting size of a WAL file. */ diff --git a/test/permutations.test b/test/permutations.test index 5afc51cb7d..7fbf98d412 100644 --- a/test/permutations.test +++ b/test/permutations.test @@ -1019,6 +1019,23 @@ test_suite "wal" -description { fts3c.test fts3d.test fts3e.test fts3query.test } +test_suite "wal2" -description { + Run tests with journal_mode=WAL2 +} -initialize { + set ::G(savepoint6_iterations) 100 +} -shutdown { + unset -nocomplain ::G(savepoint6_iterations) +} -files { + savepoint.test savepoint2.test savepoint6.test + trans.test avtrans.test + + fts3aa.test fts3ab.test fts3ac.test fts3ad.test + fts3ae.test fts3af.test fts3ag.test fts3ah.test + fts3ai.test fts3aj.test fts3ak.test fts3al.test + fts3am.test fts3an.test fts3ao.test fts3b.test + fts3c.test fts3d.test fts3e.test fts3query.test +} + test_suite "rtree" -description { All R-tree related tests. Provides coverage of source file rtree.c. } -files [glob -nocomplain $::testdir/../ext/rtree/*.test] diff --git a/test/savepoint.test b/test/savepoint.test index eed8a9e702..f196f8d2fc 100644 --- a/test/savepoint.test +++ b/test/savepoint.test @@ -28,6 +28,7 @@ do_test savepoint-1.1 { RELEASE sp1; } } {} +wal_check_journal_mode savepoint-1.1 do_test savepoint-1.2 { execsql { SAVEPOINT sp1; diff --git a/test/savepoint6.test b/test/savepoint6.test index b1d0d46f5c..6b41ef2da9 100644 --- a/test/savepoint6.test +++ b/test/savepoint6.test @@ -15,6 +15,10 @@ set testdir [file dirname $argv0] source $testdir/tester.tcl proc sql {zSql} { + if {0 && $::debug_op} { + puts stderr "$zSql ;" + flush stderr + } uplevel db eval [list $zSql] #puts stderr "$zSql ;" } @@ -67,11 +71,13 @@ proc x_to_y {x} { # delete_rows XVALUES # proc savepoint {zName} { + if {$::debug_op} { puts stderr "savepoint $zName" ; flush stderr } catch { sql "SAVEPOINT $zName" } lappend ::lSavepoint [list $zName [array get ::aEntry]] } proc rollback {zName} { + if {$::debug_op} { puts stderr "rollback $zName" ; flush stderr } catch { sql "ROLLBACK TO $zName" } for {set i [expr {[llength $::lSavepoint]-1}]} {$i>=0} {incr i -1} { set zSavepoint [lindex $::lSavepoint $i 0] @@ -89,6 +95,7 @@ proc rollback {zName} { } proc release {zName} { + if {$::debug_op} { puts stderr "release $zName" ; flush stderr } catch { sql "RELEASE $zName" } for {set i [expr {[llength $::lSavepoint]-1}]} {$i>=0} {incr i -1} { set zSavepoint [lindex $::lSavepoint $i 0] @@ -104,6 +111,7 @@ proc release {zName} { } proc insert_rows {lX} { + if {$::debug_op} { puts stderr "insert_rows $lX" ; flush stderr } foreach x $lX { set y [x_to_y $x] @@ -116,6 +124,7 @@ proc insert_rows {lX} { } proc delete_rows {lX} { + if {$::debug_op} { puts stderr "delete_rows $lX" ; flush stderr } foreach x $lX { # Update database [db] sql "DELETE FROM t1 WHERE x = $x" @@ -164,6 +173,11 @@ proc random_integers {nRes nRange} { } #------------------------------------------------------------------------- +set ::debug_op 0 +proc debug_ops {} { + set ::debug_op 1 +} + proc database_op {} { set i [expr int(rand()*2)] if {$i==0} { @@ -185,9 +199,6 @@ proc savepoint_op {} { set C [lindex $cmds [expr int(rand()*6)]] set N [lindex $names [expr int(rand()*5)]] - #puts stderr " $C $N ; " - #flush stderr - $C $N return ok } diff --git a/test/tester.tcl b/test/tester.tcl index 10a20a47d6..4294b00786 100644 --- a/test/tester.tcl +++ b/test/tester.tcl @@ -586,6 +586,7 @@ proc reset_db {} { forcedelete test.db forcedelete test.db-journal forcedelete test.db-wal + forcedelete test.db-wal2 sqlite3 db ./test.db set ::DB [sqlite3_connection_pointer db] if {[info exists ::SETUP_SQL]} { @@ -2053,17 +2054,32 @@ proc drop_all_indexes {{db db}} { # Returns true if this test should be run in WAL mode. False otherwise. # proc wal_is_wal_mode {} { - expr {[permutation] eq "wal"} + if {[permutation] eq "wal"} { return 1 } + if {[permutation] eq "wal2"} { return 2 } + return 0 } proc wal_set_journal_mode {{db db}} { - if { [wal_is_wal_mode] } { - $db eval "PRAGMA journal_mode = WAL" + switch -- [wal_is_wal_mode] { + 0 { + } + + 1 { + $db eval "PRAGMA journal_mode = WAL" + } + + 2 { + $db eval "PRAGMA journal_mode = WAL2" + } } } proc wal_check_journal_mode {testname {db db}} { if { [wal_is_wal_mode] } { $db eval { SELECT * FROM sqlite_master } - do_test $testname [list $db eval "PRAGMA main.journal_mode"] {wal} + set expected "wal" + if {[wal_is_wal_mode]==2} { + set expected "wal2" + } + do_test $testname [list $db eval "PRAGMA main.journal_mode"] $expected } } diff --git a/test/waltwo2.test b/test/waltwo2.test new file mode 100644 index 0000000000..0ab5636a06 --- /dev/null +++ b/test/waltwo2.test @@ -0,0 +1,127 @@ +# 2017 September 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL2" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl + +set testprefix walsimple +ifcapable !wal {finish_test ; return } + +db close +foreach f [glob -nocomplain test.db*] { forcedelete $f } +sqlite3 db test.db + +do_execsql_test 1.0 { + CREATE TABLE t1(x, y); + PRAGMA journal_mode = wal2; +} {wal2} + +do_execsql_test 1.1 { + SELECT * FROM t1; +} {} + +do_execsql_test 1.2 { + INSERT INTO t1 VALUES(1, 2); +} {} + +do_execsql_test 1.3 { + SELECT * FROM t1; +} {1 2} + +do_test 1.4 { + sqlite3 db2 test.db + execsql { SELECT * FROM t1 } db2 +} {1 2} + +do_test 1.5 { + lsort [glob test.db*] +} {test.db test.db-shm test.db-wal test.db-wal2} + +do_test 1.6 { + db close + db2 close + sqlite3 db test.db + execsql { SELECT * FROM t1 } +} {1 2} + +do_execsql_test 1.7 { + PRAGMA journal_size_limit = 4000; + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t1 VALUES(5, 6); + INSERT INTO t1 VALUES(7, 8); + INSERT INTO t1 VALUES(9, 10); + INSERT INTO t1 VALUES(11, 12); + INSERT INTO t1 VALUES(13, 14); + INSERT INTO t1 VALUES(15, 16); + INSERT INTO t1 VALUES(17, 18); + SELECT * FROM t1; +} {4000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18} + +do_test 1.8 { + sqlite3 db2 test.db + execsql { SELECT * FROM t1 } db2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18} + +do_test 1.9 { + db close + db2 close + lsort [glob test.db*] +} {test.db} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + CREATE INDEX i1 ON t1(b, c); + PRAGMA journal_mode = wal2; + PRAGMA journal_size_limit = 4000; +} {wal2 4000} + +proc wal_hook {DB nm nFrame} { + $DB eval { PRAGMA wal_checkpoint } +} +db wal_hook [list wal_hook db] + + +foreach js {4000 8000 12000} { + foreach NROW [list 100 200 300 400 500 600 1000] { + do_test 2.$js.$NROW.1 { + db eval "DELETE FROM t1" + db eval "PRAGMA journal_size_limit = $js" + set nTotal 0 + for {set i 0} {$i < $NROW} {incr i} { + db eval { INSERT INTO t1 VALUES($i, $i, randomblob(abs(random()%50))) } + incr nTotal $i + } + set {} {} + } {} + + do_test 2.$js.$NROW.2 { + sqlite3 db2 test.db + db2 eval { + PRAGMA integrity_check; + SELECT count(*), sum(b) FROM t1; + } + } [list ok $NROW $nTotal] + + db2 close + } +} + +finish_test + From 3400e78f385c21d66ee3107bd78b856285a71b6e Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 5 Oct 2017 18:14:46 +0000 Subject: [PATCH 02/51] Fix test case failures on this branch. FossilOrigin-Name: 16decc13af908087fb8aa34eeccf43e8da1b8f2e4b808028986d1ef08134c72c --- manifest | 22 +++++++++------------- manifest.uuid | 2 +- src/wal.c | 41 +++++++++++++++++++++++++---------------- test/corruptA.test | 2 +- test/rdonly.test | 6 +++--- test/uri.test | 4 ++-- 6 files changed, 41 insertions(+), 36 deletions(-) diff --git a/manifest b/manifest index f6dbc867fd..e9519e9d3f 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\sexperimental\smode\sthat\suses\stwo\swal\sfiles.\sActivated\susing\s"PRAGMA\njournal_mode\s=\swal2". -D 2017-10-04T20:57:14.949 +C Fix\stest\scase\sfailures\son\sthis\sbranch. +D 2017-10-05T18:14:46.713 F Makefile.in 4bc36d913c2e3e2d326d588d72f618ac9788b2fd4b7efda61102611a6495c3ff F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 6033b51b6aea702ea059f6ab2d47b1d3cef648695f787247dd4fb395fe60673f @@ -537,7 +537,7 @@ F src/vdbesort.c 731a09e5cb9e96b70c394c1b7cf3860fbe84acca7682e178615eb941a3a0ef2 F src/vdbetrace.c 48e11ebe040c6b41d146abed2602e3d00d621d7ebe4eb29b0a0f1617fd3c2f6c F src/vtab.c 0e4885495172e1bdf54b12cce23b395ac74ef5729031f15e1bc1e3e6b360ed1a F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 5ca528539a69edd6333dcab1d49e89d4f98efb6a23f0fda85bc52c4ec313db49 +F src/wal.c 3455865cef56441a90c3688b65e091df3343e18c36bd4ad9da1154ecb22f2216 F src/wal.h b6063e6be1b03389372f3f32240e99b8ab92c32cdd05aa0e31b30a21e4e41654 F src/walker.c 3ccfa8637f95355bff61144e01a615b8ef26f79c312880848da73f03367da1e6 F src/where.c 049522adcf5426f1a8c3ed07be15e1ffa3266afd34e8e7bee64b63e2fbfad0b5 @@ -675,7 +675,7 @@ F test/corrupt6.test fc6a891716139665dae0073b6945e3670bf92568 F test/corrupt7.test b036f94bda4b0b23a2919bf717046ce9ecca4543 F test/corrupt8.test 2399dfe40d2c0c63af86706e30f3e6302a8d0516 F test/corrupt9.test 730a3db08d4ab9aa43392ea30d9c2b4879cbff85 -F test/corruptA.test 112f4b2ae0b95ebf3ea63718642fb969a93acea557ace3a307234d19c245989b +F test/corruptA.test 56e8f321adaf3411960e9d2c7136669d8e1a91cbde6cf401ea84e6d6c7ccbe10 F test/corruptB.test 73a8d6c0b9833697ecf16b63e3c5c05c945b5dec F test/corruptC.test 138ecb02188ed1a719b533d4a139568204039f72f00e07a8d30d920bd83122db F test/corruptD.test b3c205fac7952b1de645ce44bb02335cd9e3e040 @@ -1118,7 +1118,7 @@ F test/quote.test 215897dbe8de1a6f701265836d6601cc6ed103e6 F test/randexpr1.tcl 40dec52119ed3a2b8b2a773bce24b63a3a746459 F test/randexpr1.test eda062a97e60f9c38ae8d806b03b0ddf23d796df F test/rbu.test 168573d353cd0fd10196b87b0caa322c144ef736 -F test/rdonly.test 64e2696c322e3538df0b1ed624e21f9a23ed9ff8 +F test/rdonly.test 21e99ee237265d0cf95a0c84b50c784e834acaa4ef05d92a27b262626a656682 F test/regexp1.test 497ea812f264d12b6198d6e50a76be4a1973a9d8 F test/regexp2.test 40e894223b3d6672655481493f1be12012f2b33c F test/reindex.test 44edd3966b474468b823d481eafef0c305022254 @@ -1456,7 +1456,7 @@ F test/unixexcl.test d936ba2b06794018e136418addd59a2354eeae97 F test/unordered.test ca7adce0419e4ca0c50f039885e76ed2c531eda8 F test/update.test 6c68446b8a0a33d522a7c72b320934596a2d7d32 F test/update2.test fffc92e72ae568fe048588762e650cd8ccbd8c8b6e4fe9099231766bfe4b51de -F test/uri.test 3481026f00ade6dfe8adb7acb6e1e47b04369568 +F test/uri.test a2becabcb9fe25d08d1ae49c0788f4a75dda97bfe4c8641c2d04e224faf7a6e2 F test/uri2.test 9d3ba7a53ee167572d53a298ee4a5d38ec4a8fb7 F test/userauth01.test e740a2697a7b40d7c5003a7d7edaee16acd349a9 F test/utf16align.test 54cd35a27c005a9b6e7815d887718780b6a462ae @@ -1656,11 +1656,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 40964a4ef7565ea0ddf452f48cb22373d068528e07d40eefc008f2231c969422 -R 07b0a272ae21af8e4cb674b6ac44fa89 -T *branch * wal2 -T *sym-wal2 * -T +closed f04ded1d9b40d54463162264e37e6d92411d09427eea592ef05681035e2f2e64 -T -sym-trunk * +P e2fc5c814cf6862d536aacb9eca66ecd31ba7e3e3033fa4c5564d533f4a18dfc +R 69b71d3158015f2955b522d7c6fc4b0d U dan -Z 07422baaaff21d5f15df6853d7e92b06 +Z 23af8b038261b8f3163841b50db40cad diff --git a/manifest.uuid b/manifest.uuid index 092af1e9bc..2d0fb63ef8 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -e2fc5c814cf6862d536aacb9eca66ecd31ba7e3e3033fa4c5564d533f4a18dfc \ No newline at end of file +16decc13af908087fb8aa34eeccf43e8da1b8f2e4b808028986d1ef08134c72c \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 36c04289a5..db4fca8b94 100644 --- a/src/wal.c +++ b/src/wal.c @@ -1507,11 +1507,18 @@ static int walIndexRecover(Wal *pWal){ ** problems caused by applications routinely shutting down without ** checkpointing the log file. */ if( pWal->hdr.nPage ){ - sqlite3_log(SQLITE_NOTICE_RECOVER_WAL, - "recovered (%d,%d) frames from WAL files %s[2] (%s mode)", - walidxGetMxFrame(&pWal->hdr, 0), walidxGetMxFrame(&pWal->hdr, 1), - pWal->zWalName, isWalMode2(pWal) ? "wal2" : "wal" - ); + if( isWalMode2(pWal) ){ + sqlite3_log(SQLITE_NOTICE_RECOVER_WAL, + "recovered (%d,%d) frames from WAL files %s[2] (%s mode)", + walidxGetMxFrame(&pWal->hdr, 0), walidxGetMxFrame(&pWal->hdr, 1), + pWal->zWalName, isWalMode2(pWal) ? "wal2" : "wal" + ); + }else{ + sqlite3_log(SQLITE_NOTICE_RECOVER_WAL, + "recovered %d frames from WAL file %s", + pWal->hdr.mxFrame, pWal->zWalName + ); + } } } @@ -2164,13 +2171,15 @@ static int walCheckpoint( /* Truncate the db file, sync the wal file and set the WalCkptInfo ** flag to indicate that it has been checkpointed. */ - if( !bWal2 && rc==SQLITE_OK && mxSafeFrame==walIndexHdr(pWal)->mxFrame ){ - i64 szDb = pWal->hdr.nPage*(i64)szPage; - testcase( IS_BIG_INT(szDb) ); - rc = sqlite3OsTruncate(pWal->pDbFd, szDb); - } - if( rc==SQLITE_OK ){ - rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags)); + if( rc==SQLITE_OK && (bWal2 || mxSafeFrame==walIndexHdr(pWal)->mxFrame) ){ + if( !bWal2 ){ + i64 szDb = pWal->hdr.nPage*(i64)szPage; + testcase( IS_BIG_INT(szDb) ); + rc = sqlite3OsTruncate(pWal->pDbFd, szDb); + } + if( rc==SQLITE_OK ){ + rc = sqlite3OsSync(pWal->pDbFd, CKPT_SYNC_FLAGS(sync_flags)); + } } if( rc==SQLITE_OK ){ pInfo->nBackfill = bWal2 ? 1 : mxSafeFrame; @@ -4003,14 +4012,14 @@ int sqlite3WalExclusiveMode(Wal *pWal, int op){ if( op==0 ){ if( pWal->exclusiveMode ){ - pWal->exclusiveMode = 0; + pWal->exclusiveMode = WAL_NORMAL_MODE; if( isWalMode2(pWal) ){ rc = walLockReader(pWal, pWal->readLock, 1); }else{ rc = walLockShared(pWal, WAL_READ_LOCK(pWal->readLock)); } - if( rc==SQLITE_OK ){ - pWal->exclusiveMode = 1; + if( rc!=SQLITE_OK ){ + pWal->exclusiveMode = WAL_EXCLUSIVE_MODE; } rc = pWal->exclusiveMode==0; }else{ @@ -4025,7 +4034,7 @@ int sqlite3WalExclusiveMode(Wal *pWal, int op){ }else{ walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); } - pWal->exclusiveMode = 1; + pWal->exclusiveMode = WAL_EXCLUSIVE_MODE; rc = 1; }else{ rc = pWal->exclusiveMode==0; diff --git a/test/corruptA.test b/test/corruptA.test index 12d918615f..653b88b693 100644 --- a/test/corruptA.test +++ b/test/corruptA.test @@ -47,7 +47,7 @@ db close forcecopy test.db test.db-template set unreadable_version 02 -ifcapable wal { set unreadable_version 03 } +ifcapable wal { set unreadable_version 04 } do_test corruptA-2.1 { forcecopy test.db-template test.db hexio_write test.db 19 $unreadable_version ;# the read format number diff --git a/test/rdonly.test b/test/rdonly.test index 404c613b21..bdc70cdd70 100644 --- a/test/rdonly.test +++ b/test/rdonly.test @@ -41,7 +41,7 @@ do_test rdonly-1.1.1 { sqlite3_db_readonly db main } {0} -# Changes the write version from 1 to 3. Verify that the database +# Changes the write version from 1 to 4. Verify that the database # can be read but not written. # do_test rdonly-1.2 { @@ -49,7 +49,7 @@ do_test rdonly-1.2 { hexio_get_int [hexio_read test.db 18 1] } 1 do_test rdonly-1.3 { - hexio_write test.db 18 03 + hexio_write test.db 18 04 sqlite3 db test.db execsql { SELECT * FROM t1; @@ -83,7 +83,7 @@ do_test rdonly-1.5 { # the database is read-only until after it is locked. # set ro_version 02 -ifcapable wal { set ro_version 03 } +ifcapable wal { set ro_version 04 } do_test rdonly-1.6 { hexio_write test.db 18 $ro_version ; # write-version hexio_write test.db 24 11223344 ; # change-counter diff --git a/test/uri.test b/test/uri.test index 0931a25e51..b47f5fb05c 100644 --- a/test/uri.test +++ b/test/uri.test @@ -280,11 +280,11 @@ ifcapable wal { INSERT INTO t2 VALUES('x', 'y'); } lsort [array names ::T1] - } {test.db1 test.db1-journal test.db1-wal} + } {test.db1 test.db1-journal test.db1-wal test.db1-wal2} do_test 5.1.2 { lsort [array names ::T2] - } {test.db2 test.db2-journal test.db2-wal} + } {test.db2 test.db2-journal test.db2-wal test.db2-wal2} db close tvfs1 delete From 24f1b25c7b7b3b73033693c42bd9af956aaf3263 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 6 Oct 2017 13:43:42 +0000 Subject: [PATCH 03/51] Fix frame overwriting in wal2 mode. FossilOrigin-Name: a4b02bc9388226da21b3837a20c6c7eb0d13854dde62b7136e04f4978528dc71 --- manifest | 13 +++---- manifest.uuid | 2 +- src/wal.c | 67 ++++++++++++++++++++---------------- test/wal2rewrite.test | 80 +++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 126 insertions(+), 36 deletions(-) create mode 100644 test/wal2rewrite.test diff --git a/manifest b/manifest index e9519e9d3f..ebdae660fb 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\stest\scase\sfailures\son\sthis\sbranch. -D 2017-10-05T18:14:46.713 +C Fix\sframe\soverwriting\sin\swal2\smode. +D 2017-10-06T13:43:42.775 F Makefile.in 4bc36d913c2e3e2d326d588d72f618ac9788b2fd4b7efda61102611a6495c3ff F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 6033b51b6aea702ea059f6ab2d47b1d3cef648695f787247dd4fb395fe60673f @@ -537,7 +537,7 @@ F src/vdbesort.c 731a09e5cb9e96b70c394c1b7cf3860fbe84acca7682e178615eb941a3a0ef2 F src/vdbetrace.c 48e11ebe040c6b41d146abed2602e3d00d621d7ebe4eb29b0a0f1617fd3c2f6c F src/vtab.c 0e4885495172e1bdf54b12cce23b395ac74ef5729031f15e1bc1e3e6b360ed1a F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 3455865cef56441a90c3688b65e091df3343e18c36bd4ad9da1154ecb22f2216 +F src/wal.c 1fdb379a36877a21fa97220bbe075957c024a6d06716db692e1d35145738a5a1 F src/wal.h b6063e6be1b03389372f3f32240e99b8ab92c32cdd05aa0e31b30a21e4e41654 F src/walker.c 3ccfa8637f95355bff61144e01a615b8ef26f79c312880848da73f03367da1e6 F src/where.c 049522adcf5426f1a8c3ed07be15e1ffa3266afd34e8e7bee64b63e2fbfad0b5 @@ -1492,6 +1492,7 @@ F test/vtab_err.test 0d4d8eb4def1d053ac7c5050df3024fd47a3fbd8 F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad F test/wal.test 613efec03e517e1775d86b993a54877d2e29a477 F test/wal2.test 6ac39b94a284ebac6efb6be93b0cdfe73ee6083f129555e3144d8a615e9900ef +F test/wal2rewrite.test 467a66722b3e68b2c5c50f98fb62491c5f5e3f54b1d5728c7f8f1d5afe4976fe F test/wal3.test 2a93004bc0fb2b5c29888964024695bade278ab2 F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c F test/wal5.test 9c11da7aeccd83a46d79a556ad11a18d3cb15aa9 @@ -1656,7 +1657,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P e2fc5c814cf6862d536aacb9eca66ecd31ba7e3e3033fa4c5564d533f4a18dfc -R 69b71d3158015f2955b522d7c6fc4b0d +P 16decc13af908087fb8aa34eeccf43e8da1b8f2e4b808028986d1ef08134c72c +R 756fa931da3c4e04a97cc0362e6290b7 U dan -Z 23af8b038261b8f3163841b50db40cad +Z 4ba52753cca3b375d5de2490acf53ff4 diff --git a/manifest.uuid b/manifest.uuid index 2d0fb63ef8..d9d56c6538 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -16decc13af908087fb8aa34eeccf43e8da1b8f2e4b808028986d1ef08134c72c \ No newline at end of file +a4b02bc9388226da21b3837a20c6c7eb0d13854dde62b7136e04f4978528dc71 \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index db4fca8b94..54486f9969 100644 --- a/src/wal.c +++ b/src/wal.c @@ -3006,6 +3006,30 @@ static int walSearchHash( return SQLITE_OK; } +static int walSearchWal( + Wal *pWal, + int iWal, + Pgno pgno, + u32 *piRead +){ + int rc = SQLITE_OK; + int bWal2 = isWalMode2(pWal); + u32 iLast = walidxGetMxFrame(&pWal->hdr, iWal); + if( iLast ){ + int iHash; + int iMinHash = walFramePage(pWal->minFrame); + u32 iExternal = bWal2 ? walExternalEncode(iWal, iLast) : iLast; + assert( bWal2==0 || pWal->minFrame==0 ); + for(iHash=walFramePage(iExternal); + iHash>=iMinHash && *piRead==0; + iHash-=(1+bWal2) + ){ + rc = walSearchHash(pWal, iExternal, iHash, pgno, piRead); + if( rc!=SQLITE_OK ) break; + } + } + return rc; +} /* ** Search the wal file for page pgno. If found, set *piRead to the frame that @@ -3024,8 +3048,6 @@ int sqlite3WalFindFrame( int iApp = walidxGetFile(&pWal->hdr); int rc = SQLITE_OK; u32 iRead = 0; /* If !=0, WAL frame to return data from */ - u32 iLast; /* Last frame in wal file */ - int iHash; /* Used to loop through N hash tables */ /* This routine is only be called from within a read transaction. */ assert( pWal->readLock!=WAL_LOCK_NONE ); @@ -3041,18 +3063,8 @@ int sqlite3WalFindFrame( assert( bWal2 || iApp==0 ); /* Search the wal file that the client holds a partial lock on first */ - iLast = walidxGetMxFrame(&pWal->hdr, iApp); - if( iLast ){ - u32 iExternal = bWal2 ? walExternalEncode(iApp, iLast) : iLast; - int iMinHash = walFramePage(pWal->minFrame); - for(iHash=walFramePage(iExternal); - iHash>=iMinHash && iRead==0; - iHash-=(1+bWal2) - ){ - rc = walSearchHash(pWal, iExternal, iHash, pgno, &iRead); - if( rc!=SQLITE_OK ) break; - } - } + + rc = walSearchWal(pWal, iApp, pgno, &iRead); /* If the requested page was not found, no error has occured, and ** the client holds a full-wal lock on the other wal file, search it @@ -3061,14 +3073,7 @@ int sqlite3WalFindFrame( pWal->readLock==WAL_LOCK_PART1_FULL2 || pWal->readLock==WAL_LOCK_PART2_FULL1 )){ - iLast = walidxGetMxFrame(&pWal->hdr, !iApp); - if( iLast ){ - u32 iExternal = walExternalEncode(!iApp, iLast); - for(iHash=walFramePage2(!iApp, iLast); iHash>=0 && iRead==0; iHash -= 2){ - rc = walSearchHash(pWal, iExternal, iHash, pgno, &iRead); - if( rc!=SQLITE_OK ) break; - } - } + rc = walSearchWal(pWal, !iApp, pgno, &iRead); } #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) @@ -3526,6 +3531,7 @@ static int walRewriteChecksums(Wal *pWal, u32 iLast){ u8 aFrame[WAL_FRAME_HDRSIZE]; /* Buffer to assemble frame-headers in */ u32 iRead; /* Next frame to read from wal file */ i64 iCksumOff; + sqlite3_file *pWalFd = pWal->apWalFd[walidxGetFile(&pWal->hdr)]; assert( isWalMode2(pWal)==0 ); @@ -3589,6 +3595,7 @@ int sqlite3WalFrames( u32 iFirst = 0; /* First frame that may be overwritten */ WalIndexHdr *pLive; /* Pointer to shared header */ int iApp; + int bWal2 = isWalMode2(pWal); assert( pList ); assert( pWal->writeLock ); @@ -3599,9 +3606,8 @@ int sqlite3WalFrames( pLive = (WalIndexHdr*)walIndexHdr(pWal); if( memcmp(&pWal->hdr, (void *)pLive, sizeof(WalIndexHdr))!=0 ){ - if( isWalMode2(pWal)==0 ){ - iFirst = pLive->mxFrame+1; - } + /* if( isWalMode2(pWal)==0 ) */ + iFirst = walidxGetMxFrame(pLive, walidxGetFile(pLive))+1; } /* See if it is possible to write these frames into the start of the @@ -3617,7 +3623,7 @@ int sqlite3WalFrames( */ iApp = walidxGetFile(&pWal->hdr); iFrame = walidxGetMxFrame(&pWal->hdr, iApp); - assert( iApp==0 || isWalMode2(pWal) ); + assert( iApp==0 || bWal2 ); #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) { int cnt; for(cnt=0, p=pList; p; p=p->pDirty, cnt++){} @@ -3634,7 +3640,7 @@ int sqlite3WalFrames( sqlite3Put4byte(&aWalHdr[0], (WAL_MAGIC | SQLITE_BIGENDIAN)); sqlite3Put4byte(&aWalHdr[4], pWal->hdr.iVersion); sqlite3Put4byte(&aWalHdr[8], szPage); - if( isWalMode2(pWal) ){ + if( bWal2 ){ if( walidxGetMxFrame(&pWal->hdr, !iApp)>0 ){ u8 aPrev[4]; rc = sqlite3OsRead(pWal->apWalFd[!iApp], aPrev, 4, 12); @@ -3697,8 +3703,11 @@ int sqlite3WalFrames( ** checksums must be recomputed when the transaction is committed. */ if( iFirst && (p->pDirty || isCommit==0) ){ u32 iWrite = 0; - VVA_ONLY(rc =) sqlite3WalFindFrame(pWal, p->pgno, &iWrite); + VVA_ONLY(rc =) walSearchWal(pWal, iApp, p->pgno, &iWrite); assert( rc==SQLITE_OK || iWrite==0 ); + if( iWrite && bWal2 ){ + walExternalDecode(iWrite, &iWrite); + } if( iWrite>=iFirst ){ i64 iOff = walFrameOffset(iWrite, szPage) + WAL_FRAME_HDRSIZE; void *pData; @@ -3810,7 +3819,7 @@ int sqlite3WalFrames( /* If this is a commit, update the wal-index header too. */ if( isCommit ){ walIndexWriteHdr(pWal); - if( isWalMode2(pWal) ){ + if( bWal2 ){ int iOther = !walidxGetFile(&pWal->hdr); if( walidxGetMxFrame(&pWal->hdr, iOther) && !walCkptInfo(pWal)->nBackfill diff --git a/test/wal2rewrite.test b/test/wal2rewrite.test new file mode 100644 index 0000000000..dea4f8c4f5 --- /dev/null +++ b/test/wal2rewrite.test @@ -0,0 +1,80 @@ +# 2017 September 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL2" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl + +set testprefix wal2rewrite +ifcapable !wal {finish_test ; return } + +proc filesize {filename} { + if {[file exists $filename]} { + return [file size $filename] + } + return 0 +} + +foreach {tn jrnlmode} { + 1 wal + 2 wal2 +} { + reset_db + execsql "PRAGMA journal_mode = $jrnlmode" + do_execsql_test $tn.1 { + PRAGMA journal_size_limit = 10000; + PRAGMA cache_size = 5; + PRAGMA wal_autocheckpoint = 10; + + CREATE TABLE t1(a INTEGER PRIMARY KEY, b INTEGER, c BLOB); + CREATE INDEX t1b ON t1(b); + CREATE INDEX t1c ON t1(c); + + WITH s(i) AS ( + SELECT 1 UNION SELECT i+1 FROM s WHERE i<10 + ) + INSERT INTO t1 SELECT i, i, randomblob(800) FROM s; + } {10000 10} + + for {set i 0} {$i < 4} {incr i} { + do_execsql_test $tn.$i.1 { + UPDATE t1 SET c=randomblob(800) WHERE (b%10)==5 AND ($i%2) + } + do_execsql_test $tn.$i.2 { + BEGIN; + UPDATE t1 SET b=b+10, c=randomblob(800); + UPDATE t1 SET b=b+10, c=randomblob(800); + UPDATE t1 SET b=b+10, c=randomblob(800); + UPDATE t1 SET b=b+10, c=randomblob(800); + UPDATE t1 SET b=b+10, c=randomblob(800); + UPDATE t1 SET b=b+10, c=randomblob(800); + UPDATE t1 SET b=b+10, c=randomblob(800); + UPDATE t1 SET b=b+10, c=randomblob(800); + UPDATE t1 SET b=b+10, c=randomblob(800); + UPDATE t1 SET b=b+10, c=randomblob(800); + } + execsql COMMIT + + do_test $tn.$i.3 { expr [filesize test.db-wal] < 100000 } 1 + do_test $tn.$i.4 { expr [filesize test.db-wal2] < 100000 } 1 + } + +} + + + +finish_test From bfcf8d6d675492c3bec7201781c7529284cebe3a Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 6 Oct 2017 14:08:39 +0000 Subject: [PATCH 04/51] Fix a bug in recovering wal2 mode databases introduced by the previous commit. FossilOrigin-Name: 9e1502e1b650217efc361732a3dfe686caa2e6352d040c73865f1faf09bf4591 --- manifest | 14 +++++++------- manifest.uuid | 2 +- src/wal.c | 6 +++--- test/wal2rewrite.test | 14 +++++++++++++- 4 files changed, 24 insertions(+), 12 deletions(-) diff --git a/manifest b/manifest index ebdae660fb..2eb85d5859 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sframe\soverwriting\sin\swal2\smode. -D 2017-10-06T13:43:42.775 +C Fix\sa\sbug\sin\srecovering\swal2\smode\sdatabases\sintroduced\sby\sthe\sprevious\scommit. +D 2017-10-06T14:08:39.369 F Makefile.in 4bc36d913c2e3e2d326d588d72f618ac9788b2fd4b7efda61102611a6495c3ff F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 6033b51b6aea702ea059f6ab2d47b1d3cef648695f787247dd4fb395fe60673f @@ -537,7 +537,7 @@ F src/vdbesort.c 731a09e5cb9e96b70c394c1b7cf3860fbe84acca7682e178615eb941a3a0ef2 F src/vdbetrace.c 48e11ebe040c6b41d146abed2602e3d00d621d7ebe4eb29b0a0f1617fd3c2f6c F src/vtab.c 0e4885495172e1bdf54b12cce23b395ac74ef5729031f15e1bc1e3e6b360ed1a F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 1fdb379a36877a21fa97220bbe075957c024a6d06716db692e1d35145738a5a1 +F src/wal.c 287feccc5c6e886a5d747f5555382280e85a24904d0378940ef3869eb4273571 F src/wal.h b6063e6be1b03389372f3f32240e99b8ab92c32cdd05aa0e31b30a21e4e41654 F src/walker.c 3ccfa8637f95355bff61144e01a615b8ef26f79c312880848da73f03367da1e6 F src/where.c 049522adcf5426f1a8c3ed07be15e1ffa3266afd34e8e7bee64b63e2fbfad0b5 @@ -1492,7 +1492,7 @@ F test/vtab_err.test 0d4d8eb4def1d053ac7c5050df3024fd47a3fbd8 F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad F test/wal.test 613efec03e517e1775d86b993a54877d2e29a477 F test/wal2.test 6ac39b94a284ebac6efb6be93b0cdfe73ee6083f129555e3144d8a615e9900ef -F test/wal2rewrite.test 467a66722b3e68b2c5c50f98fb62491c5f5e3f54b1d5728c7f8f1d5afe4976fe +F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal3.test 2a93004bc0fb2b5c29888964024695bade278ab2 F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c F test/wal5.test 9c11da7aeccd83a46d79a556ad11a18d3cb15aa9 @@ -1657,7 +1657,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 16decc13af908087fb8aa34eeccf43e8da1b8f2e4b808028986d1ef08134c72c -R 756fa931da3c4e04a97cc0362e6290b7 +P a4b02bc9388226da21b3837a20c6c7eb0d13854dde62b7136e04f4978528dc71 +R 1b4fe3016ef19de5fbb230932d1cf085 U dan -Z 4ba52753cca3b375d5de2490acf53ff4 +Z bb0f71cc9a0d45c0ef66e315563912f3 diff --git a/manifest.uuid b/manifest.uuid index d9d56c6538..9ce9b5d7db 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -a4b02bc9388226da21b3837a20c6c7eb0d13854dde62b7136e04f4978528dc71 \ No newline at end of file +9e1502e1b650217efc361732a3dfe686caa2e6352d040c73865f1faf09bf4591 \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 54486f9969..2cc21039da 100644 --- a/src/wal.c +++ b/src/wal.c @@ -3549,7 +3549,7 @@ static int walRewriteChecksums(Wal *pWal, u32 iLast){ }else{ iCksumOff = walFrameOffset(pWal->iReCksum-1, szPage) + 16; } - rc = sqlite3OsRead(pWal->apWalFd[0], aBuf, sizeof(u32)*2, iCksumOff); + rc = sqlite3OsRead(pWalFd, aBuf, sizeof(u32)*2, iCksumOff); pWal->hdr.aFrameCksum[0] = sqlite3Get4byte(aBuf); pWal->hdr.aFrameCksum[1] = sqlite3Get4byte(&aBuf[sizeof(u32)]); @@ -3557,14 +3557,14 @@ static int walRewriteChecksums(Wal *pWal, u32 iLast){ pWal->iReCksum = 0; for(; rc==SQLITE_OK && iRead<=iLast; iRead++){ i64 iOff = walFrameOffset(iRead, szPage); - rc = sqlite3OsRead(pWal->apWalFd[0], aBuf, szPage+WAL_FRAME_HDRSIZE, iOff); + rc = sqlite3OsRead(pWalFd, aBuf, szPage+WAL_FRAME_HDRSIZE, iOff); if( rc==SQLITE_OK ){ u32 iPgno, nDbSize; iPgno = sqlite3Get4byte(aBuf); nDbSize = sqlite3Get4byte(&aBuf[4]); walEncodeFrame(pWal, iPgno, nDbSize, &aBuf[WAL_FRAME_HDRSIZE], aFrame); - rc = sqlite3OsWrite(pWal->apWalFd[0], aFrame, sizeof(aFrame), iOff); + rc = sqlite3OsWrite(pWalFd, aFrame, sizeof(aFrame), iOff); } } diff --git a/test/wal2rewrite.test b/test/wal2rewrite.test index dea4f8c4f5..7e3b7b17b5 100644 --- a/test/wal2rewrite.test +++ b/test/wal2rewrite.test @@ -71,8 +71,20 @@ foreach {tn jrnlmode} { do_test $tn.$i.3 { expr [filesize test.db-wal] < 100000 } 1 do_test $tn.$i.4 { expr [filesize test.db-wal2] < 100000 } 1 - } + set sum [db eval {SELECT sum(b), md5sum(c) FROM t1}] + + do_test $tn.$i.5 { + foreach f [glob -nocomplain test.db2*] {forcedelete $f} + foreach f [glob -nocomplain test.db*] { + forcecopy $f [string map {test.db test.db2} $f] + } + + sqlite3 db2 test.db2 + db2 eval {SELECT sum(b), md5sum(c) FROM t1} + } $sum + db2 close + } } From 5d122d308fd7439a5af69fcb297a303e4c6d892d Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 7 Oct 2017 13:37:04 +0000 Subject: [PATCH 05/51] Check in test file wal2simple.test. FossilOrigin-Name: 8932b2f1d7e6a26221ea3dea01000832b2d1eb17ac0b70ef6028f9286ae450a3 --- manifest | 12 +-- manifest.uuid | 2 +- test/wal2simple.test | 233 +++++++++++++++++++++++++++++++++++++++++++ test/waltwo2.test | 127 ----------------------- 4 files changed, 240 insertions(+), 134 deletions(-) create mode 100644 test/wal2simple.test delete mode 100644 test/waltwo2.test diff --git a/manifest b/manifest index 71878d3e69..5356b92b47 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\slatest\strunk\schanges\sinto\sthis\sbranch. -D 2017-10-06T14:25:25.410 +C Check\sin\stest\sfile\swal2simple.test. +D 2017-10-07T13:37:04.814 F Makefile.in 4bc36d913c2e3e2d326d588d72f618ac9788b2fd4b7efda61102611a6495c3ff F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 6033b51b6aea702ea059f6ab2d47b1d3cef648695f787247dd4fb395fe60673f @@ -1493,6 +1493,7 @@ F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad F test/wal.test 613efec03e517e1775d86b993a54877d2e29a477 F test/wal2.test 6ac39b94a284ebac6efb6be93b0cdfe73ee6083f129555e3144d8a615e9900ef F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c +F test/wal2simple.test 7ea5b2bfedace701a6415d27ecac7bcd852381435b6f3d71bb90eda0db11b2b9 F test/wal3.test 2a93004bc0fb2b5c29888964024695bade278ab2 F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c F test/wal5.test 9c11da7aeccd83a46d79a556ad11a18d3cb15aa9 @@ -1521,7 +1522,6 @@ F test/walro.test 4ab7ac01b77c2f894235c699d59e3e3c7f15a160 F test/walshared.test 0befc811dcf0b287efae21612304d15576e35417 F test/walslow.test c05c68d4dc2700a982f89133ce103a1a84cc285f F test/walthread.test de8dbaf6d9e41481c460ba31ca61e163d7348f8e -F test/waltwo2.test 6e4d36500a20ff2d19761cf0e9a5d178e83d1798feda157ebc0681e01a35e56e F test/where.test f0c325563acde44f2c4ea6ba348e9e29f7121757 F test/where2.test 478d2170637b9211f593120648858593bf2445a1 F test/where3.test 54cdeb02157acc979de41530b804ae7b09552bf1 @@ -1657,7 +1657,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 9e1502e1b650217efc361732a3dfe686caa2e6352d040c73865f1faf09bf4591 8ca0fa8dfe6a66aea7fc63f15e6f704cb190aa0760a3fec2db5f6bad3861a135 -R 8c191757c669656683c7e7d83493c86d +P 7e43517861d4ecfa86766a16a2c721377b75da78771d5ba18870dcb9626a8dce +R efd9b7af2079a6949a0d309021efe433 U dan -Z b9f86bb91f1032f49d3cb4702444cf0c +Z 3fd52f62c898d986eaf1742529c99b05 diff --git a/manifest.uuid b/manifest.uuid index f045e6d6c2..1c4e653c83 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -7e43517861d4ecfa86766a16a2c721377b75da78771d5ba18870dcb9626a8dce \ No newline at end of file +8932b2f1d7e6a26221ea3dea01000832b2d1eb17ac0b70ef6028f9286ae450a3 \ No newline at end of file diff --git a/test/wal2simple.test b/test/wal2simple.test new file mode 100644 index 0000000000..ea69e67b8c --- /dev/null +++ b/test/wal2simple.test @@ -0,0 +1,233 @@ +# 2017 September 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL2" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl + +set testprefix wal2simple +ifcapable !wal {finish_test ; return } + +#------------------------------------------------------------------------- +# The following tests verify that a client can switch in and out of wal +# and wal2 mode. But that it is not possible to change directly from wal +# to wal2, or from wal2 to wal mode. +# +do_execsql_test 1.1.0 { + PRAGMA journal_mode = wal2 +} {wal2} +execsql { SELECT * FROM sqlite_master} +do_execsql_test 1.x { + PRAGMA journal_mode; + PRAGMA main.journal_mode; +} {wal2 wal2} +db close +do_test 1.1.1 { file size test.db } {1024} +do_test 1.1.2 { hexio_read test.db 18 2 } 0303 + +sqlite3 db test.db +do_execsql_test 1.2.0 { + SELECT * FROM sqlite_master; + PRAGMA journal_mode = delete; +} {delete} +db close +do_test 1.2.1 { file size test.db } {1024} +do_test 1.2.2 { hexio_read test.db 18 2 } 0101 + +sqlite3 db test.db +do_execsql_test 1.3.0 { + SELECT * FROM sqlite_master; + PRAGMA journal_mode = wal; +} {wal} +db close +do_test 1.3.1 { file size test.db } {1024} +do_test 1.3.2 { hexio_read test.db 18 2 } 0202 + +sqlite3 db test.db +do_catchsql_test 1.4.0 { + PRAGMA journal_mode = wal2; +} {1 {cannot change from wal to wal2 mode}} +do_execsql_test 1.4.1 { + PRAGMA journal_mode = wal; + PRAGMA journal_mode = delete; + PRAGMA journal_mode = wal2; + PRAGMA journal_mode = wal2; +} {wal delete wal2 wal2} +do_catchsql_test 1.4.2 { + PRAGMA journal_mode = wal; +} {1 {cannot change from wal2 to wal mode}} +db close +do_test 1.4.3 { hexio_read test.db 18 2 } 0303 + +#------------------------------------------------------------------------- +# Test that recovery in wal2 mode works. +# +forcedelete test.db test.db-wal test.db-wal2 +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b); + PRAGMA journal_mode = wal2; + PRAGMA journal_size_limit = 5000; +} {wal2 5000} + +proc wal_hook {DB nm nFrame} { $DB eval { PRAGMA wal_checkpoint } } +db wal_hook {wal_hook db} + +for {set i 1} {$i <= 200} {incr i} { + execsql { INSERT INTO t1 VALUES(NULL, randomblob(100)) } + set res [db eval { SELECT sum(a), md5sum(b) FROM t1 }] + + do_test 2.1.$i { + foreach f [glob -nocomplain test.db2*] { forcedelete $f } + forcecopy test.db test.db2 + forcecopy test.db-wal test.db2-wal + forcecopy test.db-wal2 test.db2-wal2 + + sqlite3 db2 test.db2 + db2 eval { SELECT sum(a), md5sum(b) FROM t1 } + } $res + + db2 close +} + +#------------------------------------------------------------------------- + +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(x BLOB, y INTEGER PRIMARY KEY); + CREATE INDEX i1 ON t1(x); + PRAGMA cache_size = 5; + PRAGMA journal_mode = wal2; +} {wal2} + +do_test 3.1 { + execsql BEGIN + for {set i 1} {$i < 1000} {incr i} { + execsql { INSERT INTO t1 VALUES(randomblob(800), $i) } + } + execsql COMMIT +} {} + +do_execsql_test 3.2 { + PRAGMA integrity_check; +} {ok} + +#------------------------------------------------------------------------- +catch { db close } +foreach f [glob -nocomplain test.db*] { forcedelete $f } +reset_db +do_execsql_test 4.0 { + CREATE TABLE t1(x, y); + PRAGMA journal_mode = wal2; +} {wal2} + +do_execsql_test 4.1 { + SELECT * FROM t1; +} {} + +do_execsql_test 4.2 { + INSERT INTO t1 VALUES(1, 2); +} {} + +do_execsql_test 4.3 { + SELECT * FROM t1; +} {1 2} + +do_test 4.4 { + sqlite3 db2 test.db + execsql { SELECT * FROM t1 } db2 +} {1 2} + +do_test 4.5 { + lsort [glob test.db*] +} {test.db test.db-shm test.db-wal test.db-wal2} + +do_test 4.6 { + db close + db2 close + sqlite3 db test.db + execsql { SELECT * FROM t1 } +} {1 2} + +do_execsql_test 4.7 { + PRAGMA journal_size_limit = 4000; + INSERT INTO t1 VALUES(3, 4); + INSERT INTO t1 VALUES(5, 6); + INSERT INTO t1 VALUES(7, 8); + INSERT INTO t1 VALUES(9, 10); + INSERT INTO t1 VALUES(11, 12); + INSERT INTO t1 VALUES(13, 14); + INSERT INTO t1 VALUES(15, 16); + INSERT INTO t1 VALUES(17, 18); + SELECT * FROM t1; +} {4000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18} + +do_test 4.8 { + sqlite3 db2 test.db + execsql { SELECT * FROM t1 } db2 +} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18} + +do_test 4.9 { + db close + db2 close + lsort [glob test.db*] +} {test.db} + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 5.0 { + CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); + CREATE INDEX i1 ON t1(b, c); + PRAGMA journal_mode = wal2; + PRAGMA journal_size_limit = 4000; +} {wal2 4000} + +proc wal_hook {DB nm nFrame} { + $DB eval { PRAGMA wal_checkpoint } +} +db wal_hook [list wal_hook db] + + +foreach js {4000 8000 12000} { + foreach NROW [list 100 200 300 400 500 600 1000] { + do_test 5.$js.$NROW.1 { + db eval "DELETE FROM t1" + db eval "PRAGMA journal_size_limit = $js" + set nTotal 0 + for {set i 0} {$i < $NROW} {incr i} { + db eval { INSERT INTO t1 VALUES($i, $i, randomblob(abs(random()%50))) } + incr nTotal $i + } + set {} {} + } {} + + do_test 5.$js.$NROW.2 { + sqlite3 db2 test.db + db2 eval { + PRAGMA integrity_check; + SELECT count(*), sum(b) FROM t1; + } + } [list ok $NROW $nTotal] + + db2 close + } +} + + + + +finish_test diff --git a/test/waltwo2.test b/test/waltwo2.test deleted file mode 100644 index 0ab5636a06..0000000000 --- a/test/waltwo2.test +++ /dev/null @@ -1,127 +0,0 @@ -# 2017 September 19 -# -# The author disclaims copyright to this source code. In place of -# a legal notice, here is a blessing: -# -# May you do good and not evil. -# May you find forgiveness for yourself and forgive others. -# May you share freely, never taking more than you give. -# -#*********************************************************************** -# This file implements regression tests for SQLite library. The -# focus of this file is testing the operation of the library in -# "PRAGMA journal_mode=WAL2" mode. -# - -set testdir [file dirname $argv0] -source $testdir/tester.tcl -source $testdir/lock_common.tcl -source $testdir/malloc_common.tcl -source $testdir/wal_common.tcl - -set testprefix walsimple -ifcapable !wal {finish_test ; return } - -db close -foreach f [glob -nocomplain test.db*] { forcedelete $f } -sqlite3 db test.db - -do_execsql_test 1.0 { - CREATE TABLE t1(x, y); - PRAGMA journal_mode = wal2; -} {wal2} - -do_execsql_test 1.1 { - SELECT * FROM t1; -} {} - -do_execsql_test 1.2 { - INSERT INTO t1 VALUES(1, 2); -} {} - -do_execsql_test 1.3 { - SELECT * FROM t1; -} {1 2} - -do_test 1.4 { - sqlite3 db2 test.db - execsql { SELECT * FROM t1 } db2 -} {1 2} - -do_test 1.5 { - lsort [glob test.db*] -} {test.db test.db-shm test.db-wal test.db-wal2} - -do_test 1.6 { - db close - db2 close - sqlite3 db test.db - execsql { SELECT * FROM t1 } -} {1 2} - -do_execsql_test 1.7 { - PRAGMA journal_size_limit = 4000; - INSERT INTO t1 VALUES(3, 4); - INSERT INTO t1 VALUES(5, 6); - INSERT INTO t1 VALUES(7, 8); - INSERT INTO t1 VALUES(9, 10); - INSERT INTO t1 VALUES(11, 12); - INSERT INTO t1 VALUES(13, 14); - INSERT INTO t1 VALUES(15, 16); - INSERT INTO t1 VALUES(17, 18); - SELECT * FROM t1; -} {4000 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18} - -do_test 1.8 { - sqlite3 db2 test.db - execsql { SELECT * FROM t1 } db2 -} {1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18} - -do_test 1.9 { - db close - db2 close - lsort [glob test.db*] -} {test.db} - -#------------------------------------------------------------------------- -reset_db -do_execsql_test 2.0 { - CREATE TABLE t1(a INTEGER PRIMARY KEY, b, c); - CREATE INDEX i1 ON t1(b, c); - PRAGMA journal_mode = wal2; - PRAGMA journal_size_limit = 4000; -} {wal2 4000} - -proc wal_hook {DB nm nFrame} { - $DB eval { PRAGMA wal_checkpoint } -} -db wal_hook [list wal_hook db] - - -foreach js {4000 8000 12000} { - foreach NROW [list 100 200 300 400 500 600 1000] { - do_test 2.$js.$NROW.1 { - db eval "DELETE FROM t1" - db eval "PRAGMA journal_size_limit = $js" - set nTotal 0 - for {set i 0} {$i < $NROW} {incr i} { - db eval { INSERT INTO t1 VALUES($i, $i, randomblob(abs(random()%50))) } - incr nTotal $i - } - set {} {} - } {} - - do_test 2.$js.$NROW.2 { - sqlite3 db2 test.db - db2 eval { - PRAGMA integrity_check; - SELECT count(*), sum(b) FROM t1; - } - } [list ok $NROW $nTotal] - - db2 close - } -} - -finish_test - From 50182fa846d66e413e303e2545885ac12667b598 Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 7 Oct 2017 19:55:37 +0000 Subject: [PATCH 06/51] Ignore the *-wal2 file if the *-wal file is zero bytes in size. FossilOrigin-Name: f7360fad51f224f347bb7d263eb89056b27461c278309e00e575a0e8898c9f40 --- manifest | 14 +++++------ manifest.uuid | 2 +- src/wal.c | 20 +++++++++++++++ test/wal2simple.test | 58 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 86 insertions(+), 8 deletions(-) diff --git a/manifest b/manifest index 5356b92b47..aa48e86c68 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Check\sin\stest\sfile\swal2simple.test. -D 2017-10-07T13:37:04.814 +C Ignore\sthe\s*-wal2\sfile\sif\sthe\s*-wal\sfile\sis\szero\sbytes\sin\ssize. +D 2017-10-07T19:55:37.322 F Makefile.in 4bc36d913c2e3e2d326d588d72f618ac9788b2fd4b7efda61102611a6495c3ff F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 6033b51b6aea702ea059f6ab2d47b1d3cef648695f787247dd4fb395fe60673f @@ -537,7 +537,7 @@ F src/vdbesort.c 731a09e5cb9e96b70c394c1b7cf3860fbe84acca7682e178615eb941a3a0ef2 F src/vdbetrace.c 48e11ebe040c6b41d146abed2602e3d00d621d7ebe4eb29b0a0f1617fd3c2f6c F src/vtab.c 0e4885495172e1bdf54b12cce23b395ac74ef5729031f15e1bc1e3e6b360ed1a F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 287feccc5c6e886a5d747f5555382280e85a24904d0378940ef3869eb4273571 +F src/wal.c 11314f64edbb7613e80290830a7379ff083b511bd1ee97846518c3102ebf5c4f F src/wal.h b6063e6be1b03389372f3f32240e99b8ab92c32cdd05aa0e31b30a21e4e41654 F src/walker.c 3ccfa8637f95355bff61144e01a615b8ef26f79c312880848da73f03367da1e6 F src/where.c 049522adcf5426f1a8c3ed07be15e1ffa3266afd34e8e7bee64b63e2fbfad0b5 @@ -1493,7 +1493,7 @@ F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad F test/wal.test 613efec03e517e1775d86b993a54877d2e29a477 F test/wal2.test 6ac39b94a284ebac6efb6be93b0cdfe73ee6083f129555e3144d8a615e9900ef F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c -F test/wal2simple.test 7ea5b2bfedace701a6415d27ecac7bcd852381435b6f3d71bb90eda0db11b2b9 +F test/wal2simple.test 8719413446ca97ca88507c5b79f139b631faa5b4e177b7424008b26fc153da30 F test/wal3.test 2a93004bc0fb2b5c29888964024695bade278ab2 F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c F test/wal5.test 9c11da7aeccd83a46d79a556ad11a18d3cb15aa9 @@ -1657,7 +1657,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 7e43517861d4ecfa86766a16a2c721377b75da78771d5ba18870dcb9626a8dce -R efd9b7af2079a6949a0d309021efe433 +P 8932b2f1d7e6a26221ea3dea01000832b2d1eb17ac0b70ef6028f9286ae450a3 +R c46e3ca31dc60584535891a8280b1e6f U dan -Z 3fd52f62c898d986eaf1742529c99b05 +Z fe4e7b40b37235ec7703f9add7af6779 diff --git a/manifest.uuid b/manifest.uuid index 1c4e653c83..bd32a9a8c9 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -8932b2f1d7e6a26221ea3dea01000832b2d1eb17ac0b70ef6028f9286ae450a3 \ No newline at end of file +f7360fad51f224f347bb7d263eb89056b27461c278309e00e575a0e8898c9f40 \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 2cc21039da..277f674bbb 100644 --- a/src/wal.c +++ b/src/wal.c @@ -1375,6 +1375,21 @@ static int walOpenWal2(Wal *pWal){ return rc; } +static int walTruncateWal2(Wal *pWal){ + int bIs; + int rc; + assert( !isOpen(pWal->apWalFd[1]) ); + rc = sqlite3OsAccess(pWal->pVfs, pWal->zWalName2, SQLITE_ACCESS_EXISTS, &bIs); + if( rc==SQLITE_OK && bIs ){ + rc = walOpenWal2(pWal); + if( rc==SQLITE_OK ){ + rc = sqlite3OsTruncate(pWal->apWalFd[1], 0); + sqlite3OsClose(pWal->apWalFd[1]); + } + } + return rc; +} + /* ** Recover the wal-index by reading the write-ahead log file. ** @@ -1434,6 +1449,9 @@ static int walIndexRecover(Wal *pWal){ || pWal->hdr.iVersion==WAL_VERSION1 || pWal->hdr.iVersion==WAL_VERSION2 ); + if( rc==SQLITE_OK && bZero ){ + rc = walTruncateWal2(pWal); + } if( rc==SQLITE_OK && pWal->hdr.iVersion!=WAL_VERSION1 ){ int bOpen = 1; sqlite3_vfs *pVfs = pWal->pVfs; @@ -1481,6 +1499,8 @@ static int walIndexRecover(Wal *pWal){ pWal->hdr = hdr; }else{ walidxSetFile(&pWal->hdr, 1); + walidxSetMxFrame(&pWal->hdr, 1, pWal->hdr.mxFrame); + walidxSetMxFrame(&pWal->hdr, 0, 0); } pWal->hdr.iVersion = WAL_VERSION2; }else{ diff --git a/test/wal2simple.test b/test/wal2simple.test index ea69e67b8c..2498c5500f 100644 --- a/test/wal2simple.test +++ b/test/wal2simple.test @@ -228,6 +228,64 @@ foreach js {4000 8000 12000} { } +#------------------------------------------------------------------------- +reset_db +do_execsql_test 6.0 { + CREATE TABLE tx(x); + PRAGMA journal_mode = wal2; + PRAGMA journal_size_limit = 3500; +} {wal2 3500} + +do_test 6.1 { + for {set i 0} {$i < 10} {incr i} { + execsql "CREATE TABLE t$i (x);" + } +} {} + +puts "[file size test.db-wal] [file size test.db-wal2]" + +do_test 6.2.1 { + foreach f [glob -nocomplain test.db2*] { forcedelete $f } + forcecopy test.db-wal2 test.db2-wal2 + sqlite3 db2 test.db2 + db2 eval { SELECT * FROM sqlite_master } +} {} +do_test 6.2.2 { + db2 eval { + PRAGMA journal_mode = wal2; + SELECT * FROM sqlite_master; + } +} {wal2} + +do_test 6.3.1 { + db2 close + foreach f [glob -nocomplain test.db2*] { forcedelete $f } + forcecopy test.db-wal2 test.db2-wal2 + forcecopy test.db test.db2 + sqlite3 db2 test.db2 + db2 eval { SELECT * FROM sqlite_master } +} {table tx tx 2 {CREATE TABLE tx(x)}} +do_test 6.3.2 { + db2 eval { + PRAGMA journal_mode = wal2; + SELECT * FROM sqlite_master; + } +} {wal2 table tx tx 2 {CREATE TABLE tx(x)}} +do_test 6.4.1 { + db2 close + foreach f [glob -nocomplain test.db2*] { forcedelete $f } + forcecopy test.db-wal2 test.db2-wal2 + forcecopy test.db-wal test.db2-wal + sqlite3 db2 test.db2 + db2 eval { SELECT * FROM sqlite_master } +} {} +do_test 6.4.2 { + db2 eval { + PRAGMA journal_mode = wal2; + SELECT * FROM sqlite_master; + } +} {wal2} +db2 close finish_test From 40927fd61e4db1149e31a65d4ca05d64bb6212dc Mon Sep 17 00:00:00 2001 From: dan Date: Mon, 9 Oct 2017 19:49:08 +0000 Subject: [PATCH 07/51] Add a header comment to wal.c describing the differences between wal and wal2 mode. FossilOrigin-Name: 9c80cd202f1c966929b279e18f19c663912686fcf92f03b85a02b9c7e55a0fc6 --- manifest | 12 ++-- manifest.uuid | 2 +- src/wal.c | 168 +++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 173 insertions(+), 9 deletions(-) diff --git a/manifest b/manifest index aa48e86c68..0f5352281d 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Ignore\sthe\s*-wal2\sfile\sif\sthe\s*-wal\sfile\sis\szero\sbytes\sin\ssize. -D 2017-10-07T19:55:37.322 +C Add\sa\sheader\scomment\sto\swal.c\sdescribing\sthe\sdifferences\sbetween\swal\sand\swal2\nmode. +D 2017-10-09T19:49:08.605 F Makefile.in 4bc36d913c2e3e2d326d588d72f618ac9788b2fd4b7efda61102611a6495c3ff F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 6033b51b6aea702ea059f6ab2d47b1d3cef648695f787247dd4fb395fe60673f @@ -537,7 +537,7 @@ F src/vdbesort.c 731a09e5cb9e96b70c394c1b7cf3860fbe84acca7682e178615eb941a3a0ef2 F src/vdbetrace.c 48e11ebe040c6b41d146abed2602e3d00d621d7ebe4eb29b0a0f1617fd3c2f6c F src/vtab.c 0e4885495172e1bdf54b12cce23b395ac74ef5729031f15e1bc1e3e6b360ed1a F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 11314f64edbb7613e80290830a7379ff083b511bd1ee97846518c3102ebf5c4f +F src/wal.c c025455c9d6cf48ca55bd894be4a37a160565de9c845510a048fb9113761b2f4 F src/wal.h b6063e6be1b03389372f3f32240e99b8ab92c32cdd05aa0e31b30a21e4e41654 F src/walker.c 3ccfa8637f95355bff61144e01a615b8ef26f79c312880848da73f03367da1e6 F src/where.c 049522adcf5426f1a8c3ed07be15e1ffa3266afd34e8e7bee64b63e2fbfad0b5 @@ -1657,7 +1657,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 8932b2f1d7e6a26221ea3dea01000832b2d1eb17ac0b70ef6028f9286ae450a3 -R c46e3ca31dc60584535891a8280b1e6f +P f7360fad51f224f347bb7d263eb89056b27461c278309e00e575a0e8898c9f40 +R 3eebf17818b82033242954f80ad24b93 U dan -Z fe4e7b40b37235ec7703f9add7af6779 +Z fe65619fadae77c6923d24a16aa4aa37 diff --git a/manifest.uuid b/manifest.uuid index bd32a9a8c9..f96be1474c 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -f7360fad51f224f347bb7d263eb89056b27461c278309e00e575a0e8898c9f40 \ No newline at end of file +9c80cd202f1c966929b279e18f19c663912686fcf92f03b85a02b9c7e55a0fc6 \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 277f674bbb..836e876c5c 100644 --- a/src/wal.c +++ b/src/wal.c @@ -101,7 +101,7 @@ ** ** To read a page from the database (call it page number P), a reader ** first checks the WAL to see if it contains page P. If so, then the -** last valid instance of page P that is a followed by a commit frame +** last valid instance of page P that is followed by a commit frame ** or is a commit frame itself becomes the value read. If the WAL ** contains no copies of page P that are valid and which are a commit ** frame or are followed by a commit frame, then page P is read from @@ -229,7 +229,7 @@ ** and to the wal-index) might be using a different value K1, where K1>K0. ** Both readers can use the same hash table and mapping section to get ** the correct result. There may be entries in the hash table with -** K>K0 but to the first reader, those entries will appear to be unused +** K>K0, but to the first reader those entries will appear to be unused ** slots in the hash table and so the first reader will get an answer as ** if no values greater than K0 had ever been inserted into the hash table ** in the first place - which is what reader one wants. Meanwhile, the @@ -240,6 +240,166 @@ ** that correspond to frames greater than the new K value are removed ** from the hash table at this point. */ + +/* +** WAL2 NOTES +** +** This file also contains the implementation of "wal2" mode - activated +** using "PRAGMA journal_mode = wal2". Wal2 mode is very similar to wal +** mode, except that it uses two wal files instead of one. Under some +** circumstances, wal2 mode provides more concurrency than legacy wal +** mode. +** +** THE PROBLEM WAL2 SOLVES: +** +** In legacy wal mode, if a writer wishes to write to the database while +** a checkpoint is ongoing, it may append frames to the existing wal file. +** This means that after the checkpoint has finished, the wal file consists +** of a large block of checkpointed frames, followed by a block of +** uncheckpointed frames. In a deployment that features a high volume of +** write traffic, this may mean that the wal file is never completely +** checkpointed. And so grows indefinitely. +** +** An alternative is to use "PRAGMA wal_checkpoint=RESTART" or similar to +** force a complete checkpoint of the wal file. But this must: +** +** 1) Wait on all existing readers to finish, +** 2) Wait on any existing writer, and then block all new writers, +** 3) Do the checkpoint, +** 4) Wait on any new readers that started during steps 2 and 3. Writers +** are still blocked during this step. +** +** This means that in order to avoid the wal file growing indefinitely +** in a busy system, writers must periodically pause to allow a checkpoint +** to complete. In a system with long running readers, such pauses may be +** for a non-trivial amount of time. +** +** OVERVIEW OF SOLUTION +** +** Wal2 mode uses two wal files. After writers have grown the first wal +** file to a pre-configured size, they begin appending transactions to +** the second wal file. Once all existing readers are reading snapshots +** new enough to include the entire first wal file, a checkpointer can +** checkpoint it. +** +** Meanwhile, writers are writing transactions to the second wal file. +** Once that wal file has grown larger than the pre-configured size, each +** new writer checks if: +** +** * the first wal file has been checkpointed, and if so, if +** * there are no readers still reading from the first wal file (once +** it has been checkpointed, new readers read only from the second +** wal file). +** +** If both these conditions are true, the writer may switch back to the +** first wal file. Eventually, a checkpointer can checkpoint the second +** wal file, and so on. +** +** The wal file that writers are currently appending to (the one they +** don't have to check the above two criteria before writing to) is called +** the "current" wal file. +** +** The first wal file takes the same name as the wal file in legacy wal +** mode systems - "-wal". The second is named "-wal2". +** +** WAL FILE FORMAT +** +** The file format used for each wal file in wal2 mode is the same as for +** legacy wal mode. Except, the file format field is set to 3021000 +** instead of 3007000. +** +** WAL-INDEX FORMAT +** +** The wal-index format is also very similar. Even though there are two +** wal files, there is still a single wal-index shared-memory area (*-shm +** file with the default unix or win32 VFS). The wal-index header is the +** same size, with the following exceptions it has the same format: +** +** * The version field is set to 3021000 instead of 3007000. +** +** * An unused 32-bit field in the legacy wal-index header is +** now used to store (a) a single bit indicating which of the +** two wal files writers should append to and (b) the number +** of frames in the second wal file (31 bits). +** +** The first hash table in the wal-index contains entries corresponding +** to the first HASHTABLE_NPAGE_ONE frames stored in the first wal file. +** The second hash table in the wal-index contains entries indexing the +** first HASHTABLE_NPAGE frames in the second wal file. The third hash +** table contains the next HASHTABLE_NPAGE frames in the first wal file, +** and so on. +** +** LOCKS +** +** Read-locks are simpler than for legacy wal mode. There are no locking +** slots that contain frame numbers. Instead, there are four distinct +** combinations of read locks a reader may hold: +** +** WAL_LOCK_PART1: "part" lock on first wal, none of second. +** WAL_LOCK_PART1_FULL2: "part" lock on first wal, "full" of second. +** WAL_LOCK_PART2: no lock on first wal, "part" lock on second. +** WAL_LOCK_PART2_FULL1: "full" lock on first wal, "part" lock on second. +** +** When a reader reads the wal-index header as part of opening a read +** transaction, it takes a "part" lock on the current wal file. "Part" +** because the wal file may grow while the read transaction is active, in +** which case the reader would be reading only part of the wal file. +** A part lock prevents a checkpointer from checkpointing the wal file +** on which it is held. +** +** If there is data in the non-current wal file that has not been +** checkpointed, the reader takes a "full" lock on that wal file. A +** "full" lock indicates that the reader is using the entire wal file. +** A full lock prevents a writer from overwriting the wal file on which +** it is held, but does not prevent a checkpointer from checkpointing +** it. +** +** There is still a single WRITER and a single CHECKPOINTER lock. The +** recovery procedure still takes the same exclusive lock on the entire +** range of SQLITE_SHM_NLOCK shm-locks. This works because the read-locks +** above use four of the six read-locking slots used by legacy wal mode. +** See the header comment for function walLockReader() for details. +** +** STARTUP/RECOVERY +** +** The read and write version fields of the database header in a wal2 +** database are set to 0x03, instead of 0x02 as in legacy wal mode. +** +** The wal file format used in wal2 mode is the same as the format used +** in legacy wal mode. However, in order to support recovery, there are two +** differences in the way wal file header fields are populated, as follows: +** +** * When the first wal file is first created, the "nCkpt" field in +** the wal file header is set to 0. Thereafter, each time the writer +** switches wal file, it sets the nCkpt field in the new wal file +** header to ((nCkpt0 + 1) & 0x0F), where nCkpt0 is the value in +** the previous wal file header. This means that the first wal file +** always has an even value in the nCkpt field, and the second wal +** file always has an odd value. +** +** * When a writer switches wal file, it sets the salt values in the +** new wal file to a copy of the checksum for the final frame in +** the previous wal file. +** +** Recovery proceeds as follows: +** +** 1. Each wal file is recovered separately. Except, if the first wal +** file does not exist or is zero bytes in size, the second wal file +** is truncated to zero bytes before it is "recovered". +** +** 2. If both wal files contain valid headers, then the nCkpt fields +** are compared to see which of the two wal files is older. If the +** salt keys in the second wal file match the final frame checksum +** in the older wal file, then both wal files are used. Otherwise, +** the newer wal file is ignored. +** +** 3. Or, if only one or neither of the wal files has a valid header, +** then only a single or no wal files are recovered into the +** reconstructed wal-index. +** +** Refer to header comments for walIndexRecover() for further details. +*/ + #ifndef SQLITE_OMIT_WAL #include "wal.h" @@ -1491,6 +1651,10 @@ static int walIndexRecover(Wal *pWal){ ){ SWAP(WalIndexHdr, pWal->hdr, hdr); walidxSetMxFrame(&pWal->hdr, 1, hdr.mxFrame); + }else{ + walidxSetFile(&pWal->hdr, 1); + walidxSetMxFrame(&pWal->hdr, 1, pWal->hdr.mxFrame); + walidxSetMxFrame(&pWal->hdr, 0, 0); } }else From 6cbf8e173f69cda68209f879661803b37606fa53 Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 10 Oct 2017 20:11:10 +0000 Subject: [PATCH 08/51] Add new extension "bgckpt" in ext/misc/bgckpt.c. For experimenting with running wal2 mode checkpoints in a background thread. FossilOrigin-Name: 63955442304052f5adddd05ccaeebe87ddc5e25af695702793518f015b4f0a87 --- ext/misc/bgckpt.c | 238 ++++++++++++++++++++++++++++++++++++++++++++++ main.mk | 1 + manifest | 15 +-- manifest.uuid | 2 +- src/tclsqlite.c | 2 + 5 files changed, 250 insertions(+), 8 deletions(-) create mode 100644 ext/misc/bgckpt.c diff --git a/ext/misc/bgckpt.c b/ext/misc/bgckpt.c new file mode 100644 index 0000000000..ca8f3edf01 --- /dev/null +++ b/ext/misc/bgckpt.c @@ -0,0 +1,238 @@ +/* +** 2017-10-11 +** +** The author disclaims copyright to this source code. In place of +** a legal notice, here is a blessing: +** +** May you do good and not evil. +** May you find forgiveness for yourself and forgive others. +** May you share freely, never taking more than you give. +** +****************************************************************************** +** +*/ + +#if !defined(SQLITE_TEST) || defined(SQLITE_OS_UNIX) + +#include "sqlite3.h" +#include +#include + +/* +** API declarations. +*/ +typedef struct Checkpointer Checkpointer; +int sqlite3_bgckpt_create(const char *zFilename, Checkpointer **pp); +int sqlite3_bgckpt_checkpoint(Checkpointer *p, int bBlock); +void sqlite3_bgckpt_destroy(Checkpointer *p); + + +struct Checkpointer { + sqlite3 *db; /* Database handle */ + + pthread_t thread; /* Background thread */ + pthread_mutex_t mutex; + pthread_cond_t cond; + + int rc; /* Error from "PRAGMA wal_checkpoint" */ + int bCkpt; /* True if checkpoint requested */ + int bExit; /* True if exit requested */ +}; + +static void *bgckptThreadMain(void *pCtx){ + int rc = SQLITE_OK; + Checkpointer *p = (Checkpointer*)pCtx; + + while( rc==SQLITE_OK ){ + int bExit; + + pthread_mutex_lock(&p->mutex); + if( p->bCkpt==0 && p->bExit==0 ){ + pthread_cond_wait(&p->cond, &p->mutex); + } + p->bCkpt = 0; + bExit = p->bExit; + pthread_mutex_unlock(&p->mutex); + + if( bExit ) break; + rc = sqlite3_exec(p->db, "PRAGMA wal_checkpoint", 0, 0, 0); + if( rc==SQLITE_BUSY ){ + rc = SQLITE_OK; + } + } + + pthread_mutex_lock(&p->mutex); + p->rc = rc; + pthread_mutex_unlock(&p->mutex); + return 0; +} + +void sqlite3_bgckpt_destroy(Checkpointer *p){ + if( p ){ + void *ret = 0; + + /* Signal the background thread to exit */ + pthread_mutex_lock(&p->mutex); + p->bExit = 1; + pthread_cond_broadcast(&p->cond); + pthread_mutex_unlock(&p->mutex); + + pthread_join(p->thread, &ret); + sqlite3_close(p->db); + sqlite3_free(p); + } +} + + +int sqlite3_bgckpt_create(const char *zFilename, Checkpointer **pp){ + Checkpointer *pNew = 0; + int rc; + + pNew = (Checkpointer*)sqlite3_malloc(sizeof(Checkpointer)); + if( pNew==0 ){ + rc = SQLITE_NOMEM; + }else{ + memset(pNew, 0, sizeof(Checkpointer)); + rc = sqlite3_open(zFilename, &pNew->db); + } + + if( rc==SQLITE_OK ){ + pthread_mutex_init(&pNew->mutex, 0); + pthread_cond_init(&pNew->cond, 0); + pthread_create(&pNew->thread, 0, bgckptThreadMain, (void*)pNew); + } + + if( rc!=SQLITE_OK ){ + sqlite3_bgckpt_destroy(pNew); + pNew = 0; + } + *pp = pNew; + return rc; +} + +int sqlite3_bgckpt_checkpoint(Checkpointer *p, int bBlock){ + int rc; + pthread_mutex_lock(&p->mutex); + rc = p->rc; + if( rc==SQLITE_OK ){ + p->bCkpt = 1; + pthread_cond_broadcast(&p->cond); + } + pthread_mutex_unlock(&p->mutex); + return rc; +} + +#ifdef SQLITE_TEST + +#if defined(INCLUDE_SQLITE_TCL_H) +# include "sqlite_tcl.h" +#else +# include "tcl.h" +# ifndef SQLITE_TCLAPI +# define SQLITE_TCLAPI +# endif +#endif + +const char *sqlite3ErrName(int rc); + +static void SQLITE_TCLAPI bgckpt_del(void * clientData){ + Checkpointer *pCkpt = (Checkpointer*)clientData; + sqlite3_bgckpt_destroy(pCkpt); +} + +/* +** Tclcmd: $ckpt SUBCMD ... +*/ +static int SQLITE_TCLAPI bgckpt_obj_cmd( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + Checkpointer *pCkpt = (Checkpointer*)clientData; + const char *aCmd[] = { "checkpoint", "destroy", 0 }; + int iCmd; + + if( objc<2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "SUBCMD ..."); + return TCL_ERROR; + } + + if( Tcl_GetIndexFromObj(interp, objv[1], aCmd, "sub-command", 0, &iCmd) ){ + return TCL_ERROR; + } + + switch( iCmd ){ + case 0: { + int rc; + int bBlock = 0; + + if( objc>3 ){ + Tcl_WrongNumArgs(interp, 2, objv, "?BLOCKING?"); + return TCL_ERROR; + } + if( objc==3 && Tcl_GetBooleanFromObj(interp, objv[2], &bBlock) ){ + return TCL_ERROR; + } + + rc = sqlite3_bgckpt_checkpoint(pCkpt, bBlock); + if( rc!=SQLITE_OK ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_ERROR; + } + break; + } + + case 1: { + Tcl_DeleteCommand(interp, Tcl_GetString(objv[0])); + break; + } + } + + return TCL_OK; +} + +/* +** Tclcmd: bgckpt CMDNAME FILENAME +*/ +static int SQLITE_TCLAPI bgckpt_cmd( + void * clientData, + Tcl_Interp *interp, + int objc, + Tcl_Obj *CONST objv[] +){ + const char *zCmd; + const char *zFilename; + int rc; + Checkpointer *pCkpt; + + if( objc!=3 ){ + Tcl_WrongNumArgs(interp, 1, objv, "CMDNAME FILENAME"); + return TCL_ERROR; + } + zCmd = Tcl_GetString(objv[1]); + zFilename = Tcl_GetString(objv[2]); + + rc = sqlite3_bgckpt_create(zFilename, &pCkpt); + if( rc!=SQLITE_OK ){ + Tcl_SetObjResult(interp, Tcl_NewStringObj(sqlite3ErrName(rc), -1)); + return TCL_ERROR; + } + + Tcl_CreateObjCommand(interp, zCmd, bgckpt_obj_cmd, (void*)pCkpt, bgckpt_del); + Tcl_SetObjResult(interp, objv[1]); + return TCL_OK; +} + +int Bgckpt_Init(Tcl_Interp *interp){ + Tcl_CreateObjCommand(interp, "bgckpt", bgckpt_cmd, 0, 0); + return TCL_OK; +} +#endif /* SQLITE_TEST */ + +#else +int Bgckpt_Init(Tcl_Interp *interp){ + return TCL_OK; +} +#endif + diff --git a/main.mk b/main.mk index 7da6db15f1..e34c88181b 100644 --- a/main.mk +++ b/main.mk @@ -327,6 +327,7 @@ TESTSRC = \ # TESTSRC += \ $(TOP)/ext/misc/amatch.c \ + $(TOP)/ext/misc/bgckpt.c \ $(TOP)/ext/misc/carray.c \ $(TOP)/ext/misc/closure.c \ $(TOP)/ext/misc/csv.c \ diff --git a/manifest b/manifest index 8bc427df49..709f88d499 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\slatest\strunk\schanges\swith\sthis\sbranch. -D 2017-10-09T19:50:09.728 +C Add\snew\sextension\s"bgckpt"\sin\sext/misc/bgckpt.c.\sFor\sexperimenting\swith\nrunning\swal2\smode\scheckpoints\sin\sa\sbackground\sthread. +D 2017-10-10T20:11:10.809 F Makefile.in 4bc36d913c2e3e2d326d588d72f618ac9788b2fd4b7efda61102611a6495c3ff F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 F Makefile.msc 6033b51b6aea702ea059f6ab2d47b1d3cef648695f787247dd4fb395fe60673f @@ -258,6 +258,7 @@ F ext/lsm1/test/lsm1_simple.test ca949efefa102f4644231dcd9291d8cda7699a4ce1006b2 F ext/misc/README.md 8e008c8d2b02e09096b31dfba033253ac27c6c06a18aa5826e299fa7601d90b2 F ext/misc/amatch.c 6db4607cb17c54b853a2d7c7c36046d004853f65b9b733e6f019d543d5dfae87 F ext/misc/anycollseq.c 5ffdfde9829eeac52219136ad6aa7cd9a4edb3b15f4f2532de52f4a22525eddb +F ext/misc/bgckpt.c 1e0178c1d9f44d44f45c731ebff45854194ca59a41d33f078f10a3a714bf4532 F ext/misc/carray.c ed96c218ea940b85c9a274c4d9c59fe9491c299147a38a8bba537687bd6c6005 F ext/misc/closure.c 0d2a038df8fbae7f19de42e7c7d71f2e4dc88704 F ext/misc/completion.c 52c3f01523e3e387eb321b4739a89d1fe47cbe6025aa1f2d8d3685e9e365df0f @@ -382,7 +383,7 @@ F ext/userauth/userauth.c 3410be31283abba70255d71fd24734e017a4497f F install-sh 9d4de14ab9fb0facae2f48780b874848cbf2f895 x F ltmain.sh 3ff0879076df340d2e23ae905484d8c15d5fdea8 F magic.txt 8273bf49ba3b0c8559cb2774495390c31fd61c60 -F main.mk d0145f02deb67d65c4822225847cba112c237cdb62f4905eeb4b648e82bfc222 +F main.mk 8dcd78e5977c6e220ec0ab9a00aa2469ca38380d1553cb3eff18469a28e04654 F mkso.sh fd21c06b063bb16a5d25deea1752c2da6ac3ed83 F mptest/config01.test 3c6adcbc50b991866855f1977ff172eb6d901271 F mptest/config02.test 4415dfe36c48785f751e16e32c20b077c28ae504 @@ -468,7 +469,7 @@ F src/sqliteInt.h c07bc88eca1f59ce73e1f486187d0df4effe67c4579e112dfdd91c159e5c05 F src/sqliteLimit.h 1513bfb7b20378aa0041e7022d04acb73525de35b80b252f1b83fedb4de6a76b F src/status.c 9737ed017279a9e0c5da748701c3c7bf1e8ae0dae459aad20dd64fcff97a7e35 F src/table.c b46ad567748f24a326d9de40e5b9659f96ffff34 -F src/tclsqlite.c 487951d81f9704800fd9f0ffdaa2f935a83ccb6be3575c2c4ef83e4789b4c828 +F src/tclsqlite.c 37be19e8ea1b4ec1e632d86272ada77bbb93f20b033df258ad3ed63dc4d40fa4 F src/test1.c 8ef15f7a357f85dfc41c6c748ce9c947b4f676e01bb5ae6a45bee4923dff8b51 F src/test2.c 3efb99ab7f1fc8d154933e02ae1378bac9637da5 F src/test3.c b8434949dfb8aff8dfa082c8b592109e77844c2135ed3c492113839b6956255b @@ -1658,7 +1659,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 9c80cd202f1c966929b279e18f19c663912686fcf92f03b85a02b9c7e55a0fc6 5594a121bf132a98d0ecb4cf86d9f2681925c9416206096bf11c9370a5dae22f -R 23a883a99c179b06ab669012d3314db6 +P d218d815f89cb1368fdb5e3f774b7adaaf02560a367ba0f3e54987e08dd6241a +R 7d8085c250f90df872ed6ab275626e25 U dan -Z c8f907ef387c1600d03d31327cf9ed02 +Z bca32e00ecb94225e888df6bb3b83e85 diff --git a/manifest.uuid b/manifest.uuid index 7c4ede0c0c..91bc8d5bae 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -d218d815f89cb1368fdb5e3f774b7adaaf02560a367ba0f3e54987e08dd6241a \ No newline at end of file +63955442304052f5adddd05ccaeebe87ddc5e25af695702793518f015b4f0a87 \ No newline at end of file diff --git a/src/tclsqlite.c b/src/tclsqlite.c index 1b9f91405e..29fe8f7953 100644 --- a/src/tclsqlite.c +++ b/src/tclsqlite.c @@ -4183,6 +4183,7 @@ static void init_all(Tcl_Interp *interp){ extern int Fts5tcl_Init(Tcl_Interp *); extern int SqliteRbu_Init(Tcl_Interp*); extern int Sqlitetesttcl_Init(Tcl_Interp*); + extern int Bgckpt_Init(Tcl_Interp*); #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) extern int Sqlitetestfts3_Init(Tcl_Interp *interp); #endif @@ -4231,6 +4232,7 @@ static void init_all(Tcl_Interp *interp){ Fts5tcl_Init(interp); SqliteRbu_Init(interp); Sqlitetesttcl_Init(interp); + Bgckpt_Init(interp); #if defined(SQLITE_ENABLE_FTS3) || defined(SQLITE_ENABLE_FTS4) Sqlitetestfts3_Init(interp); From 816fc3595655e0f7f543c2d1749f86548ebaabf1 Mon Sep 17 00:00:00 2001 From: dan Date: Mon, 3 Dec 2018 18:13:46 +0000 Subject: [PATCH 09/51] Increase a timeout in test file walprotocol2.test. To account for unix builds without HAVE_USLEEP. FossilOrigin-Name: 480be916c840e9e28010c22cf29a8396502c9e6387f31f750260c6290f58e9a1 --- manifest | 12 ++++++------ manifest.uuid | 2 +- test/walprotocol2.test | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/manifest b/manifest index 1531e9dadf..adbd28acea 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Sync\sthis\sbranch\swith\sthe\slatest\strunk. -D 2018-12-01T20:14:06.719 +C Increase\sa\stimeout\sin\stest\sfile\swalprotocol2.test.\sTo\saccount\sfor\sunix\sbuilds\nwithout\sHAVE_USLEEP. +D 2018-12-03T18:13:46.107 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in a050c8670ea0d7b37b2192306cbb50d392acd9902b84e9b56f3444d006f97a6c @@ -1619,7 +1619,7 @@ F test/walnoshm.test 84ca10c544632a756467336b7c3b864d493ee496 F test/waloverwrite.test dad2f26567f1b45174e54fbf9a8dc1cb876a7f03 F test/walpersist.test 8c6b7e3ec1ba91b5e4dc4e0921d6d3f87cd356a6 F test/walprotocol.test a112aba0b79e3adeaa485fed09484b32c654e97df58e454aa8489ac2cd57bf84 -F test/walprotocol2.test 7d3b6b4bf0b12f8007121b1e6ef714bc99101fb3b48e46371df1db868eebc131 +F test/walprotocol2.test 7e4bedd5ee83607e2928ac438bf7332a396b980d3e02aa0746509ce11ad1f13c F test/walro.test cb438d05ba0d191f10b688e39c4f0cd5b71569a1d1f4440e5bdf3c6880e08c20 F test/walro2.test 0e79dd15cbdb4f482c01ea248373669c732414a726b357d04846a816afafb768 F test/walrofault.test c70cb6e308c443867701856cce92ad8288cd99488fa52afab77cca6cfd51af68 @@ -1782,7 +1782,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 63955442304052f5adddd05ccaeebe87ddc5e25af695702793518f015b4f0a87 bf8c1b2b7a5960c282e543b9c293686dccff272512d08865f4600fb58238b4f9 -R 434605294285e3da7c3ee68026e7ade7 +P 7a44fa5a350a3f19b8e9f5196d22535788885f8c0e849572202bf64a055ddc2d +R 8dcc78dd7464cc5597695c90813a01e4 U dan -Z 6329688d1e7210750fc075a6d45758a7 +Z 7d2b6bedf7bcc33a553c67266fb7675b diff --git a/manifest.uuid b/manifest.uuid index bb767038c1..2987cabc9a 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -7a44fa5a350a3f19b8e9f5196d22535788885f8c0e849572202bf64a055ddc2d \ No newline at end of file +480be916c840e9e28010c22cf29a8396502c9e6387f31f750260c6290f58e9a1 \ No newline at end of file diff --git a/test/walprotocol2.test b/test/walprotocol2.test index 0792c9aae0..389ccb5528 100644 --- a/test/walprotocol2.test +++ b/test/walprotocol2.test @@ -85,7 +85,7 @@ proc lock_callback {method filename handle lock} { db2 eval { INSERT INTO x VALUES('x') } } } -db timeout 10 +db timeout 1100 do_catchsql_test 2.4 { BEGIN EXCLUSIVE; } {0 {}} From 6456b3929c58287efdd0851b3ef14e935c78ade1 Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 5 Dec 2018 16:31:02 +0000 Subject: [PATCH 10/51] Fixes for snapshots API on this branch. Also ensure that the snapshots API cannot be used with wal2 mode databases (for now anyhow). FossilOrigin-Name: 19c61ab79458936ff4dfca46cf4d1fb1ab16d7bdb5024f502eb4339ec4eef32c --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/wal.c | 13 ++++++++++++- 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/manifest b/manifest index adbd28acea..5cf7e8b10d 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Increase\sa\stimeout\sin\stest\sfile\swalprotocol2.test.\sTo\saccount\sfor\sunix\sbuilds\nwithout\sHAVE_USLEEP. -D 2018-12-03T18:13:46.107 +C Fixes\sfor\ssnapshots\sAPI\son\sthis\sbranch.\sAlso\sensure\sthat\sthe\ssnapshots\sAPI\ncannot\sbe\sused\swith\swal2\smode\sdatabases\s(for\snow\sanyhow). +D 2018-12-05T16:31:02.698 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in a050c8670ea0d7b37b2192306cbb50d392acd9902b84e9b56f3444d006f97a6c @@ -589,7 +589,7 @@ F src/vdbesort.c 90aad5a92608f2dd771c96749beabdb562c9d881131a860a7a5bccf66dc3be7 F src/vdbetrace.c 79d6dbbc479267b255a7de8080eee6e729928a0ef93ed9b0bfa5618875b48392 F src/vtab.c 70188a745dc4e57d26e942681ff4b2912b7c8249ad5de3f60f0677b4337bcfaa F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c e5b19ec1ce95882a2ae610d2657b441b079734427757c1a4d069f5f57bb7e9a2 +F src/wal.c 2b9b1bc7eeb819b46b0abe0bf4d9c48a13df0989814bb213da1b480d4b1c8107 F src/wal.h fc6113057f2950fc14631176b748293e216fb385ea8df665e6d259e37f8f7d21 F src/walker.c fb94aadc9099ff9c6506d0a8b88d51266005bcaa265403f3d7caf732a562eb66 F src/where.c 3818e8a736a05d2cb194e64399af707e367fbcc5c251d785804d02eaf121288e @@ -1782,7 +1782,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 7a44fa5a350a3f19b8e9f5196d22535788885f8c0e849572202bf64a055ddc2d -R 8dcc78dd7464cc5597695c90813a01e4 +P 480be916c840e9e28010c22cf29a8396502c9e6387f31f750260c6290f58e9a1 +R 869a87f8972d12d94814188a57f87377 U dan -Z 7d2b6bedf7bcc33a553c67266fb7675b +Z 200899075d870fbfb9b7c809d447f2d7 diff --git a/manifest.uuid b/manifest.uuid index 2987cabc9a..0754a48ec2 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -480be916c840e9e28010c22cf29a8396502c9e6387f31f750260c6290f58e9a1 \ No newline at end of file +19c61ab79458936ff4dfca46cf4d1fb1ab16d7bdb5024f502eb4339ec4eef32c \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 42001bf2d2..97bb781d79 100644 --- a/src/wal.c +++ b/src/wal.c @@ -3234,6 +3234,9 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ int sqlite3WalSnapshotRecover(Wal *pWal){ int rc; + /* Snapshots may not be used with wal2 mode databases. */ + if( isWalMode2(pWal) ) return SQLITE_ERROR; + assert( pWal->readLock>=0 ); rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1); if( rc==SQLITE_OK ){ @@ -3308,6 +3311,7 @@ int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ #ifdef SQLITE_ENABLE_SNAPSHOT int bChanged = 0; WalIndexHdr *pSnapshot = pWal->pSnapshot; + if( pSnapshot && isWalMode2(pWal) ) return SQLITE_ERROR; if( pSnapshot && memcmp(pSnapshot, &pWal->hdr, sizeof(WalIndexHdr))!=0 ){ bChanged = 1; } @@ -4534,9 +4538,12 @@ int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){ WalIndexHdr *pRet; static const u32 aZero[4] = { 0, 0, 0, 0 }; + /* Snapshots may not be used with wal2 mode databases. */ + if( isWalMode2(pWal) ) return SQLITE_ERROR; + assert( pWal->readLock>=0 && pWal->writeLock==0 ); - if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){ + if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,8)==0 ){ *ppSnapshot = 0; return SQLITE_ERROR; } @@ -4587,6 +4594,10 @@ int sqlite3_snapshot_cmp(sqlite3_snapshot *p1, sqlite3_snapshot *p2){ */ int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){ int rc; + + /* Snapshots may not be used with wal2 mode databases. */ + if( isWalMode2(pWal) ) return SQLITE_ERROR; + rc = walLockShared(pWal, WAL_CKPT_LOCK); if( rc==SQLITE_OK ){ WalIndexHdr *pNew = (WalIndexHdr*)pSnapshot; From e05f9225831a41d64127e88ebc2add79eb002b8a Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 5 Dec 2018 17:14:03 +0000 Subject: [PATCH 11/51] Fix a problem causing "PRAGMA journal_mode" to report the wrong journal mode (wal instead of wal2) under some circumstances. FossilOrigin-Name: 1d8d4f689653ce80157740e339f7f1b42479bf90d82b176d8202d0a49f428398 --- manifest | 16 ++++++++-------- manifest.uuid | 2 +- src/pager.c | 4 ++++ src/wal.c | 8 ++++++++ src/wal.h | 4 ++++ 5 files changed, 25 insertions(+), 9 deletions(-) diff --git a/manifest b/manifest index 5cf7e8b10d..f47ab23d2f 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fixes\sfor\ssnapshots\sAPI\son\sthis\sbranch.\sAlso\sensure\sthat\sthe\ssnapshots\sAPI\ncannot\sbe\sused\swith\swal2\smode\sdatabases\s(for\snow\sanyhow). -D 2018-12-05T16:31:02.698 +C Fix\sa\sproblem\scausing\s"PRAGMA\sjournal_mode"\sto\sreport\sthe\swrong\sjournal\smode\n(wal\sinstead\sof\swal2)\sunder\ssome\scircumstances. +D 2018-12-05T17:14:03.651 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in a050c8670ea0d7b37b2192306cbb50d392acd9902b84e9b56f3444d006f97a6c @@ -494,7 +494,7 @@ F src/os_setup.h 0dbaea40a7d36bf311613d31342e0b99e2536586 F src/os_unix.c 711480e9152f221098ec2b0d4ef94dc798f08af649c34f5cd4dc2bbf40c4f556 F src/os_win.c 85d9e532d0444ab6c16d7431490c2e279e282aa0917b0e988996b1ae0de5c5a0 F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a -F src/pager.c dd88ccf7fa519d6e325a115af28cd528713ea4d33a510d6f202386df002f2e74 +F src/pager.c dc6d0acb3f89b4dcb801c86a0e722c1bde116e058c7216ff74341812a38bbdd9 F src/pager.h 3abf6d65199fd0680b26a047c6167a96a4d6ead7535e02522b79f0fb27a3edec F src/parse.y 6840fe7c0b5eb4dd25ee5d075213bc8255ed4c0678d71bfb6744d0520d91c179 F src/pcache.c 696a01f1a6370c1b50a09c15972bc3bee3333f8fcd1f2da8e9a76b1b062c59ee @@ -589,8 +589,8 @@ F src/vdbesort.c 90aad5a92608f2dd771c96749beabdb562c9d881131a860a7a5bccf66dc3be7 F src/vdbetrace.c 79d6dbbc479267b255a7de8080eee6e729928a0ef93ed9b0bfa5618875b48392 F src/vtab.c 70188a745dc4e57d26e942681ff4b2912b7c8249ad5de3f60f0677b4337bcfaa F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 2b9b1bc7eeb819b46b0abe0bf4d9c48a13df0989814bb213da1b480d4b1c8107 -F src/wal.h fc6113057f2950fc14631176b748293e216fb385ea8df665e6d259e37f8f7d21 +F src/wal.c c01f62932ae8458e77aed8846e819e167a52224c0a13f972c9e7d90354205911 +F src/wal.h d2a69695c84137f76e19a247a342cb02ab0131001b6f58153d94b71195bbd84d F src/walker.c fb94aadc9099ff9c6506d0a8b88d51266005bcaa265403f3d7caf732a562eb66 F src/where.c 3818e8a736a05d2cb194e64399af707e367fbcc5c251d785804d02eaf121288e F src/whereInt.h f125f29fca80890768e0b2caa14f95db74b2dacd3a122a168f97aa7b64d6968f @@ -1782,7 +1782,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 480be916c840e9e28010c22cf29a8396502c9e6387f31f750260c6290f58e9a1 -R 869a87f8972d12d94814188a57f87377 +P 19c61ab79458936ff4dfca46cf4d1fb1ab16d7bdb5024f502eb4339ec4eef32c +R b8125f586e18293d9635394478d15d26 U dan -Z 200899075d870fbfb9b7c809d447f2d7 +Z 00401c2c33715960f0bf723cc059ce5c diff --git a/manifest.uuid b/manifest.uuid index 0754a48ec2..ebdf4ef09b 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -19c61ab79458936ff4dfca46cf4d1fb1ab16d7bdb5024f502eb4339ec4eef32c \ No newline at end of file +1d8d4f689653ce80157740e339f7f1b42479bf90d82b176d8202d0a49f428398 \ No newline at end of file diff --git a/src/pager.c b/src/pager.c index 1dbbd4c631..72abf2f94f 100644 --- a/src/pager.c +++ b/src/pager.c @@ -3258,6 +3258,10 @@ static int pagerBeginReadTransaction(Pager *pPager){ if( rc!=SQLITE_OK || changed ){ pager_reset(pPager); if( USEFETCH(pPager) ) sqlite3OsUnfetch(pPager->fd, 0, 0); + assert( pPager->journalMode==PAGER_JOURNALMODE_WAL + || pPager->journalMode==PAGER_JOURNALMODE_WAL2 + ); + pPager->journalMode = sqlite3WalJournalMode(pPager->pWal); } return rc; diff --git a/src/wal.c b/src/wal.c index 97bb781d79..ee5d2e1779 100644 --- a/src/wal.c +++ b/src/wal.c @@ -4641,4 +4641,12 @@ sqlite3_file *sqlite3WalFile(Wal *pWal){ return pWal->apWalFd[0]; } +/* +** Return the journal mode used by this Wal object. +*/ +int sqlite3WalJournalMode(Wal *pWal){ + assert( pWal ); + return (isWalMode2(pWal) ? PAGER_JOURNALMODE_WAL2 : PAGER_JOURNALMODE_WAL); +} + #endif /* #ifndef SQLITE_OMIT_WAL */ diff --git a/src/wal.h b/src/wal.h index 7017e908ed..ccc2888f97 100644 --- a/src/wal.h +++ b/src/wal.h @@ -45,6 +45,7 @@ # define sqlite3WalFramesize(z) 0 # define sqlite3WalFindFrame(x,y,z) 0 # define sqlite3WalFile(x) 0 +# define sqlite3WalJournalMode(x) 0 #else #define WAL_SAVEPOINT_NDATA 4 @@ -146,5 +147,8 @@ int sqlite3WalFramesize(Wal *pWal); /* Return the sqlite3_file object for the WAL file */ sqlite3_file *sqlite3WalFile(Wal *pWal); +/* Return the journal mode (WAL or WAL2) used by this Wal object. */ +int sqlite3WalJournalMode(Wal *pWal); + #endif /* ifndef SQLITE_OMIT_WAL */ #endif /* SQLITE_WAL_H */ From c30d437376a6ac366319ec01716700bc45414d9d Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 5 Dec 2018 17:31:24 +0000 Subject: [PATCH 12/51] Fix a test script problem on this branch. FossilOrigin-Name: 285d1c5904dd607457c6b5ff7be3d3191a90ca8b1fb0837327663c9910f978ac --- manifest | 12 ++++++------ manifest.uuid | 2 +- test/savepoint.test | 3 ++- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/manifest b/manifest index f47ab23d2f..5b4bda667c 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sa\sproblem\scausing\s"PRAGMA\sjournal_mode"\sto\sreport\sthe\swrong\sjournal\smode\n(wal\sinstead\sof\swal2)\sunder\ssome\scircumstances. -D 2018-12-05T17:14:03.651 +C Fix\sa\stest\sscript\sproblem\son\sthis\sbranch. +D 2018-12-05T17:31:24.866 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in a050c8670ea0d7b37b2192306cbb50d392acd9902b84e9b56f3444d006f97a6c @@ -1227,7 +1227,7 @@ F test/rowvalue9.test d8dd2c6ecac432dadaa79e41dc2434f007be1b6b F test/rowvaluefault.test 7cd9ccc6c2fbdd881672984087aad0491bb75504 F test/rtree.test 0c8d9dd458d6824e59683c19ab2ffa9ef946f798 F test/run-wordcount.sh 891e89c4c2d16e629cd45951d4ed899ad12afc09 -F test/savepoint.test 69c56b891ce0ff28f1376b5516bf2b6a8b39d0430433216bfc496e72103baaa5 +F test/savepoint.test ea1a6c08454e1a4edd973589bcf1fca83928ed09c48858fde1e8653b6daab278 F test/savepoint2.test 9b8543940572a2f01a18298c3135ad0c9f4f67d7 F test/savepoint4.test c8f8159ade6d2acd9128be61e1230f1c1edc6cc0 F test/savepoint5.test 0735db177e0ebbaedc39812c8d065075d563c4fd @@ -1782,7 +1782,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 19c61ab79458936ff4dfca46cf4d1fb1ab16d7bdb5024f502eb4339ec4eef32c -R b8125f586e18293d9635394478d15d26 +P 1d8d4f689653ce80157740e339f7f1b42479bf90d82b176d8202d0a49f428398 +R ea40d6e9e89bd0f6f2c98f8376b01df3 U dan -Z 00401c2c33715960f0bf723cc059ce5c +Z 1339c3788ed308ea20667b613925c238 diff --git a/manifest.uuid b/manifest.uuid index ebdf4ef09b..defc4724b4 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -1d8d4f689653ce80157740e339f7f1b42479bf90d82b176d8202d0a49f428398 \ No newline at end of file +285d1c5904dd607457c6b5ff7be3d3191a90ca8b1fb0837327663c9910f978ac \ No newline at end of file diff --git a/test/savepoint.test b/test/savepoint.test index f196f8d2fc..16155547a3 100644 --- a/test/savepoint.test +++ b/test/savepoint.test @@ -806,7 +806,8 @@ do_test savepoint-11.6 { integrity_check savepoint-11.7 do_test savepoint-11.8 { execsql { ROLLBACK } - execsql { PRAGMA wal_checkpoint } + db close + sqlite3 db test.db file size test.db } {8192} From d597b966d9ffe95daa6a7143091d87916c6225f1 Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 6 Dec 2018 16:54:44 +0000 Subject: [PATCH 13/51] Add test file wal2snapshot.test that should have been added as part of an earlier commit. FossilOrigin-Name: f6baa7e1163ed5f61375b0554337030fec23e8a9f60c6412e1b5d626961e93f9 --- manifest | 11 +++--- manifest.uuid | 2 +- test/wal2snapshot.test | 84 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 91 insertions(+), 6 deletions(-) create mode 100644 test/wal2snapshot.test diff --git a/manifest b/manifest index 5b4bda667c..07d0505ca6 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sa\stest\sscript\sproblem\son\sthis\sbranch. -D 2018-12-05T17:31:24.866 +C Add\stest\sfile\swal2snapshot.test\sthat\sshould\shave\sbeen\sadded\sas\spart\sof\san\nearlier\scommit. +D 2018-12-06T16:54:44.030 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in a050c8670ea0d7b37b2192306cbb50d392acd9902b84e9b56f3444d006f97a6c @@ -1595,6 +1595,7 @@ F test/wal.test 613efec03e517e1775d86b993a54877d2e29a477 F test/wal2.test 155b9efa999bdb38ce1cd729b9a4fcdbffd6b88be27f039bad1d2929d287d918 F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal2simple.test 8c9dfb8f1bca01a0deb57f7074cdb83865c2292e89b13f7a51a1c160dca3f5f4 +F test/wal2snapshot.test 95a919e1c73dee0e0212d10931d03cc1116f68a0ff603163e551aaa5ac7025c1 F test/wal3.test 2a93004bc0fb2b5c29888964024695bade278ab2 F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c F test/wal5.test 9c11da7aeccd83a46d79a556ad11a18d3cb15aa9 @@ -1782,7 +1783,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 1d8d4f689653ce80157740e339f7f1b42479bf90d82b176d8202d0a49f428398 -R ea40d6e9e89bd0f6f2c98f8376b01df3 +P 285d1c5904dd607457c6b5ff7be3d3191a90ca8b1fb0837327663c9910f978ac +R c6176f33f0706e46f32e37a55f5b7c74 U dan -Z 1339c3788ed308ea20667b613925c238 +Z dba2fba0086f2ef35808e528fd424f75 diff --git a/manifest.uuid b/manifest.uuid index defc4724b4..60435bf318 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -285d1c5904dd607457c6b5ff7be3d3191a90ca8b1fb0837327663c9910f978ac \ No newline at end of file +f6baa7e1163ed5f61375b0554337030fec23e8a9f60c6412e1b5d626961e93f9 \ No newline at end of file diff --git a/test/wal2snapshot.test b/test/wal2snapshot.test new file mode 100644 index 0000000000..9f461db7d6 --- /dev/null +++ b/test/wal2snapshot.test @@ -0,0 +1,84 @@ +# 2018 December 5 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL2" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl + +set testprefix wal2snapshot +ifcapable !wal {finish_test ; return } +ifcapable !snapshot {finish_test; return} + +foreach {tn mode} {1 wal 2 wal2} { + reset_db + do_execsql_test $tn.1 "PRAGMA journal_mode = $mode" $mode + + do_execsql_test $tn.2 { + CREATE TABLE t1(a, b); + INSERT INTO t1 VALUES(1, 2); + INSERT INTO t1 VALUES(3, 4); + BEGIN; + } + + # Check that sqlite3_snapshot_get() is an error for a wal2 db. + # + if {$tn==1} { + do_test 1.3 { + set S [sqlite3_snapshot_get db main] + sqlite3_snapshot_free $S + } {} + } else { + do_test 2.3 { + list [catch { sqlite3_snapshot_get db main } msg] $msg + } {1 SQLITE_ERROR} + } + + # Check that sqlite3_snapshot_recover() is an error for a wal2 db. + # + do_execsql_test $tn.4 COMMIT + if {$tn==1} { + do_test 1.5 { + sqlite3_snapshot_recover db main + } {} + } else { + do_test 2.5 { + list [catch { sqlite3_snapshot_recover db main } msg] $msg + } {1 SQLITE_ERROR} + } + + # Check that sqlite3_snapshot_open() is an error for a wal2 db. + # + if {$tn==1} { + do_test 1.6 { + execsql BEGIN + set SNAPSHOT [sqlite3_snapshot_get_blob db main] + sqlite3_snapshot_open_blob db main $SNAPSHOT + execsql COMMIT + } {} + } else { + do_test 2.6 { + execsql BEGIN + set res [ + list [catch { sqlite3_snapshot_open_blob db main $SNAPSHOT } msg] $msg + ] + execsql COMMIT + set res + } {1 SQLITE_ERROR} + } +} + + +finish_test + + From f17e510ee599c7f5521ab8f2ea49563b0010c17e Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 11 Dec 2018 17:56:23 +0000 Subject: [PATCH 14/51] Change the way wal2 locks work to ensure a reader only ever has to lock a single slot. FossilOrigin-Name: 18b2c23ac53d985ccc5798ea2d92fb75644b857c373fb490e0d04d5d0194a3d5 --- manifest | 14 +-- manifest.uuid | 2 +- src/wal.c | 205 ++++++++++++++++++++++--------------------- test/wal2simple.test | 2 - 4 files changed, 115 insertions(+), 108 deletions(-) diff --git a/manifest b/manifest index 1e2d309518..b182fb73af 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\slatest\strunk\schanges\sinto\sthis\sbranch. -D 2018-12-11T13:44:59.901 +C Change\sthe\sway\swal2\slocks\swork\sto\sensure\sa\sreader\sonly\sever\shas\sto\slock\sa\nsingle\sslot. +D 2018-12-11T17:56:23.255 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in 68d0ba0f0b533d5bc84c78c13a6ce84ee81183a67014caa47a969e67f028fa1c @@ -590,7 +590,7 @@ F src/vdbesort.c 90aad5a92608f2dd771c96749beabdb562c9d881131a860a7a5bccf66dc3be7 F src/vdbetrace.c 79d6dbbc479267b255a7de8080eee6e729928a0ef93ed9b0bfa5618875b48392 F src/vtab.c 70188a745dc4e57d26e942681ff4b2912b7c8249ad5de3f60f0677b4337bcfaa F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c c01f62932ae8458e77aed8846e819e167a52224c0a13f972c9e7d90354205911 +F src/wal.c 22850fcd2efa9b13e237f6097f931fe7dbd9e50f401a659288d70ae3951396b8 F src/wal.h d2a69695c84137f76e19a247a342cb02ab0131001b6f58153d94b71195bbd84d F src/walker.c fb94aadc9099ff9c6506d0a8b88d51266005bcaa265403f3d7caf732a562eb66 F src/where.c 3818e8a736a05d2cb194e64399af707e367fbcc5c251d785804d02eaf121288e @@ -1597,7 +1597,7 @@ F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad F test/wal.test 613efec03e517e1775d86b993a54877d2e29a477 F test/wal2.test 155b9efa999bdb38ce1cd729b9a4fcdbffd6b88be27f039bad1d2929d287d918 F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c -F test/wal2simple.test 8c9dfb8f1bca01a0deb57f7074cdb83865c2292e89b13f7a51a1c160dca3f5f4 +F test/wal2simple.test ce30899ab9c396d0d91905f401bb9a591f12bdf0b6b6bd5d1a62b6f560eac6e3 F test/wal2snapshot.test 95a919e1c73dee0e0212d10931d03cc1116f68a0ff603163e551aaa5ac7025c1 F test/wal3.test 2a93004bc0fb2b5c29888964024695bade278ab2 F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c @@ -1787,7 +1787,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P f6baa7e1163ed5f61375b0554337030fec23e8a9f60c6412e1b5d626961e93f9 d1db8d5894450b24bb0335983503d9bbf6cc48a0ae4b83291283fb2d32b6b25b -R 01aa34cbfc00cc4424502da843942732 +P d8dd98a39ea061dea264a7d81153f7b1be2f81b554b30d0ce289897c802209bd +R 7649521e36559a5f6fcaed801161c713 U dan -Z d3ebc7a4959beab3fb6b05d6d91630aa +Z 45541997f2aea4861e4215e9e1638ee2 diff --git a/manifest.uuid b/manifest.uuid index 6216c305ae..5aa435a1c6 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -d8dd98a39ea061dea264a7d81153f7b1be2f81b554b30d0ce289897c802209bd \ No newline at end of file +18b2c23ac53d985ccc5798ea2d92fb75644b857c373fb490e0d04d5d0194a3d5 \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index ee5d2e1779..634de7ec6f 100644 --- a/src/wal.c +++ b/src/wal.c @@ -362,7 +362,6 @@ ** recovery procedure still takes the same exclusive lock on the entire ** range of SQLITE_SHM_NLOCK shm-locks. This works because the read-locks ** above use four of the six read-locking slots used by legacy wal mode. -** See the header comment for function walLockReader() for details. ** ** STARTUP/RECOVERY ** @@ -473,16 +472,20 @@ int sqlite3WalTrace = 0; ** is held, or else is the index of the read-mark on which a lock is ** held. ** -** In wal2 mode, Wal.readLock must be set to one of the following values. -** A value of -1 still indicates that no read-lock is held, but the other -** values are symbolic. See the implementation of walLockReader() for -** details of how the symbols map to OS level locks. +** In wal2 mode, a value of -1 still indicates that no read-lock is held. +** And a non-zero value still represents the index of the read-mark on +** which a lock is held. There are two differences: +** +** 1. wal2 mode never uses read-mark 0. +** +** 2. locks on each read-mark have a different interpretation, as +** indicated by the symbolic names below. */ #define WAL_LOCK_NONE -1 #define WAL_LOCK_PART1 1 #define WAL_LOCK_PART1_FULL2 2 -#define WAL_LOCK_PART2 3 -#define WAL_LOCK_PART2_FULL1 4 +#define WAL_LOCK_PART2_FULL1 3 +#define WAL_LOCK_PART2 4 /* ** This constant is used in wal2 mode only. @@ -1111,36 +1114,6 @@ static void walUnlockExclusive(Wal *pWal, int lockIdx, int n){ walLockName(lockIdx), n)); } -/* -** This function is used to take and release read-locks in wal2 mode. -** -** Use of WAL_READ_LOCK(x) slots for (1<=x<=4). -** -** 1) Partial read of *-wal-1 (blocks checkpointer from checkpointing) -** 2) Full read of *-wal-2 (blocks writer from writing) -** 3) Partial read of *-wal-2 (blocks checkpointer from checkpointing) -** 4) Full read of *-wal-1 (blocks writer from writing) -*/ -static int walLockReader(Wal *pWal, int eLock, int bLock){ - int i; /* Index of first readmark to lock */ - int n; /* Number of readmarks to lock */ - - assert( pWal->hdr.iVersion==WAL_VERSION2 ); - if( pWal->exclusiveMode ) return SQLITE_OK; - - switch( eLock ){ - case WAL_LOCK_PART1 : i = 1; n = 1; break; - case WAL_LOCK_PART1_FULL2: i = 1; n = 2; break; - case WAL_LOCK_PART2 : i = 3; n = 1; break; - case WAL_LOCK_PART2_FULL1: i = 3; n = 2; break; - default: assert( !"cannot happen" ); - } - - return sqlite3OsShmLock(pWal->pDbFd, WAL_READ_LOCK(i), n, - SQLITE_SHM_SHARED | (bLock ? SQLITE_SHM_LOCK : SQLITE_SHM_UNLOCK) - ); -} - /* ** Compute a hash on a page number. The resulting hash value must land ** between 0 and (HASHTABLE_NSLOT-1). The walHashNext() function advances @@ -2251,6 +2224,68 @@ static void walRestartHdr(Wal *pWal, u32 salt1){ assert( pInfo->aReadMark[0]==0 ); } +/* +** This function is used in wal2 mode. +** +** This function is called when writer pWal is just about to start +** writing out frames. Parameter iApp is the current wal file. The "other" wal +** file (wal file !iApp) has been fully checkpointed. This function returns +** SQLITE_OK if there are no readers preventing the writer from switching to +** the other wal file. Or SQLITE_BUSY if there are. +*/ +static int wal2RestartOk(Wal *pWal, int iApp){ + /* The other wal file (wal file !iApp) can be overwritten if there + ** are no readers reading from it - no "full" or "partial" locks. + ** Technically speaking it is not possible for any reader to hold + ** a "part" lock, as this would have prevented the file from being + ** checkpointed. But checking anyway doesn't hurt. The following + ** is equivalent to: + ** + ** if( iApp==0 ) eLock = WAL_LOCK_PART1_FULL2; + ** if( iApp==1 ) eLock = WAL_LOCK_PART1; + */ + int eLock = 1 + (iApp==0); + + assert( WAL_LOCK_PART1==1 ); + assert( WAL_LOCK_PART1_FULL2==2 ); + assert( WAL_LOCK_PART2_FULL1==3 ); + assert( WAL_LOCK_PART2==4 ); + + assert( iApp!=0 || eLock==WAL_LOCK_PART1_FULL2 ); + assert( iApp!=1 || eLock==WAL_LOCK_PART1 ); + + return walLockExclusive(pWal, WAL_READ_LOCK(eLock), 3); +} +static void wal2RestartFinished(Wal *pWal, int iApp){ + walUnlockExclusive(pWal, WAL_READ_LOCK(1 + (iApp==0)), 3); +} + +/* +** This function is used in wal2 mode. +** +** This function is called when a checkpointer wishes to checkpoint wal +** file iCkpt. It takes the required lock and, if successful, returns +** SQLITE_OK. Otherwise, an SQLite error code (e.g. SQLITE_BUSY). If this +** function returns SQLITE_OK, it is the responsibility of the caller +** to invoke wal2CheckpointFinished() to release the lock. +*/ +static int wal2CheckpointOk(Wal *pWal, int iCkpt){ + int eLock = 1 + (iCkpt*2); + + assert( WAL_LOCK_PART1==1 ); + assert( WAL_LOCK_PART1_FULL2==2 ); + assert( WAL_LOCK_PART2_FULL1==3 ); + assert( WAL_LOCK_PART2==4 ); + + assert( iCkpt!=0 || eLock==WAL_LOCK_PART1 ); + assert( iCkpt!=1 || eLock==WAL_LOCK_PART2_FULL1 ); + + return walLockExclusive(pWal, WAL_READ_LOCK(eLock), 2); +} +static void wal2CheckpointFinished(Wal *pWal, int iCkpt){ + walUnlockExclusive(pWal, WAL_READ_LOCK(1 + (iCkpt*2)), 2); +} + /* ** Copy as much content as we can from the WAL back into the database file ** in response to an sqlite3_wal_checkpoint() request or the equivalent. @@ -2318,7 +2353,7 @@ static int walCheckpoint( ** preventing this checkpoint operation. If one is found, return ** early. */ if( bWal2 ){ - rc = walLockExclusive(pWal, WAL_READ_LOCK(1 + iCkpt*2), 1); + rc = wal2CheckpointOk(pWal, iCkpt); if( rc!=SQLITE_OK ) return rc; } @@ -2367,9 +2402,9 @@ static int walCheckpoint( assert( rc==SQLITE_OK || pIter==0 ); } - if( pIter - && (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK - ){ + if( pIter && (bWal2 + || (rc = walBusyLock(pWal, xBusy, pBusyArg,WAL_READ_LOCK(0),1))==SQLITE_OK + )){ u32 nBackfill = pInfo->nBackfill; assert( bWal2==0 || nBackfill==0 ); @@ -2434,7 +2469,9 @@ static int walCheckpoint( } /* Release the reader lock held while backfilling */ - walUnlockExclusive(pWal, WAL_READ_LOCK(bWal2 ? 1 + iCkpt*2 : 0), 1); + if( bWal2==0 ){ + walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1); + } } if( rc==SQLITE_BUSY ){ @@ -2442,6 +2479,7 @@ static int walCheckpoint( ** just because there are active readers. */ rc = SQLITE_OK; } + if( bWal2 ) wal2CheckpointFinished(pWal, iCkpt); } /* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the @@ -3060,18 +3098,27 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ assert( pWal->apWiData[0]!=0 ); pInfo = walCkptInfo(pWal); if( isWalMode2(pWal) ){ - int eLock = 1 + (walidxGetFile(&pWal->hdr)*2); - if( pInfo->nBackfill==0 ){ - eLock += walidxGetMxFrame(&pWal->hdr, !walidxGetFile(&pWal->hdr))>0; - } - rc = walLockReader(pWal, eLock, 1); + /* This connection needs a "part" lock on the current wal file and, + ** unless pInfo->nBackfill is set to indicate that it has already been + ** checkpointed, a "full" lock on the other wal file. */ + int iWal = walidxGetFile(&pWal->hdr); + int nBackfill = pInfo->nBackfill || walidxGetMxFrame(&pWal->hdr, !iWal)==0; + int eLock = 1 + (iWal*2) + (nBackfill==iWal); + + assert( nBackfill==0 || nBackfill==1 ); + assert( iWal==0 || iWal==1 ); + assert( iWal!=0 || nBackfill!=1 || eLock==WAL_LOCK_PART1 ); + assert( iWal!=0 || nBackfill!=0 || eLock==WAL_LOCK_PART1_FULL2 ); + assert( iWal!=1 || nBackfill!=1 || eLock==WAL_LOCK_PART2 ); + assert( iWal!=1 || nBackfill!=0 || eLock==WAL_LOCK_PART2_FULL1 ); + + rc = walLockShared(pWal, WAL_READ_LOCK(eLock)); if( rc!=SQLITE_OK ){ return rc; } - walShmBarrier(pWal); if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){ - walLockReader(pWal, eLock, 0); + walUnlockShared(pWal, WAL_READ_LOCK(eLock)); return WAL_RETRY; }else{ pWal->readLock = eLock; @@ -3403,11 +3450,7 @@ int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ void sqlite3WalEndReadTransaction(Wal *pWal){ sqlite3WalEndWriteTransaction(pWal); if( pWal->readLock!=WAL_LOCK_NONE ){ - if( isWalMode2(pWal) ){ - (void)walLockReader(pWal, pWal->readLock, 0); - }else{ - walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); - } + walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); pWal->readLock = WAL_LOCK_NONE; } } @@ -3794,33 +3837,6 @@ int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){ return rc; } -/* -** This function is used in wal2 mode. -** -** This function is called when writer pWal is just about to start -** writing out frames. The "other" wal file (wal file !pWal->hdr.iAppend) -** has been fully checkpointed. This function returns SQLITE_OK if there -** are no readers preventing the writer from switching to the other wal -** file. Or SQLITE_BUSY if there are. -*/ -static int walRestartOk(Wal *pWal){ - int rc; /* Return code */ - int iApp = walidxGetFile(&pWal->hdr); /* Current WAL file */ - - /* No reader can be doing a "partial" read of wal file !iApp - in that - ** case it would not have been possible to checkpoint the file. So - ** it is only necessary to test for "full" readers. See the comment - ** above walLockReader() function for exactly what this means in terms - ** of locks. */ - int i = (iApp==0) ? 2 : 4; - - rc = walLockExclusive(pWal, WAL_READ_LOCK(i), 1); - if( rc==SQLITE_OK ){ - walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1); - } - return rc; -} - /* ** This function is called just before writing a set of frames to the log ** file (see sqlite3WalFrames()). It checks to see if, instead of appending @@ -3848,19 +3864,20 @@ static int walRestartLog(Wal *pWal){ if( walidxGetMxFrame(&pWal->hdr, iApp)>=nWalSize ){ volatile WalCkptInfo *pInfo = walCkptInfo(pWal); if( walidxGetMxFrame(&pWal->hdr, !iApp)==0 || pInfo->nBackfill ){ - rc = walRestartOk(pWal); + rc = wal2RestartOk(pWal, iApp); if( rc==SQLITE_OK ){ - iApp = !iApp; + int iNew = !iApp; pWal->nCkpt++; - walidxSetFile(&pWal->hdr, iApp); - walidxSetMxFrame(&pWal->hdr, iApp, 0); + walidxSetFile(&pWal->hdr, iNew); + walidxSetMxFrame(&pWal->hdr, iNew, 0); sqlite3Put4byte((u8*)&pWal->hdr.aSalt[0], pWal->hdr.aFrameCksum[0]); sqlite3Put4byte((u8*)&pWal->hdr.aSalt[1], pWal->hdr.aFrameCksum[1]); walIndexWriteHdr(pWal); pInfo->nBackfill = 0; - walLockReader(pWal, pWal->readLock, 0); - pWal->readLock = iApp ? WAL_LOCK_PART2_FULL1 : WAL_LOCK_PART1_FULL2; - rc = walLockReader(pWal, pWal->readLock, 1); + wal2RestartFinished(pWal, iApp); + walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); + pWal->readLock = iNew ? WAL_LOCK_PART2_FULL1 : WAL_LOCK_PART1_FULL2; + rc = walLockShared(pWal, WAL_READ_LOCK(pWal->readLock)); }else if( rc==SQLITE_BUSY ){ rc = SQLITE_OK; } @@ -4490,11 +4507,7 @@ int sqlite3WalExclusiveMode(Wal *pWal, int op){ if( op==0 ){ if( pWal->exclusiveMode ){ pWal->exclusiveMode = WAL_NORMAL_MODE; - if( isWalMode2(pWal) ){ - rc = walLockReader(pWal, pWal->readLock, 1); - }else{ - rc = walLockShared(pWal, WAL_READ_LOCK(pWal->readLock)); - } + rc = walLockShared(pWal, WAL_READ_LOCK(pWal->readLock)); if( rc!=SQLITE_OK ){ pWal->exclusiveMode = WAL_EXCLUSIVE_MODE; } @@ -4506,11 +4519,7 @@ int sqlite3WalExclusiveMode(Wal *pWal, int op){ }else if( op>0 ){ assert( pWal->exclusiveMode==WAL_NORMAL_MODE ); assert( pWal->readLock>=0 ); - if( isWalMode2(pWal) ){ - walLockReader(pWal, pWal->readLock, 0); - }else{ - walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); - } + walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); pWal->exclusiveMode = WAL_EXCLUSIVE_MODE; rc = 1; }else{ diff --git a/test/wal2simple.test b/test/wal2simple.test index becbb232fc..28e13bf162 100644 --- a/test/wal2simple.test +++ b/test/wal2simple.test @@ -243,8 +243,6 @@ do_test 6.1 { } } {} -puts "[file size test.db-wal] [file size test.db-wal2]" - do_test 6.2.1 { foreach f [glob -nocomplain test.db2*] { forcedelete $f } forcecopy test.db-wal2 test.db2-wal2 From 2c4328ed870ce007ffb2181393cfc999d618406c Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 12 Dec 2018 19:04:19 +0000 Subject: [PATCH 15/51] Add tests to ensure that each of the 4 wal read-locks does what it is supposed to. FossilOrigin-Name: 4d5779f31d4931edb8bb8952d8886625ead5e51f0c308e5763e519427f6609e1 --- manifest | 12 +-- manifest.uuid | 2 +- test/wal2simple.test | 186 ++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 192 insertions(+), 8 deletions(-) diff --git a/manifest b/manifest index b182fb73af..4cb34c27b4 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Change\sthe\sway\swal2\slocks\swork\sto\sensure\sa\sreader\sonly\sever\shas\sto\slock\sa\nsingle\sslot. -D 2018-12-11T17:56:23.255 +C Add\stests\sto\sensure\sthat\seach\sof\sthe\s4\swal\sread-locks\sdoes\swhat\sit\sis\ssupposed\sto. +D 2018-12-12T19:04:19.425 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in 68d0ba0f0b533d5bc84c78c13a6ce84ee81183a67014caa47a969e67f028fa1c @@ -1597,7 +1597,7 @@ F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad F test/wal.test 613efec03e517e1775d86b993a54877d2e29a477 F test/wal2.test 155b9efa999bdb38ce1cd729b9a4fcdbffd6b88be27f039bad1d2929d287d918 F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c -F test/wal2simple.test ce30899ab9c396d0d91905f401bb9a591f12bdf0b6b6bd5d1a62b6f560eac6e3 +F test/wal2simple.test 96206c98bf64ab20ec00a1c0f6c709e258b98b39f2149889361f31966ce5a703 F test/wal2snapshot.test 95a919e1c73dee0e0212d10931d03cc1116f68a0ff603163e551aaa5ac7025c1 F test/wal3.test 2a93004bc0fb2b5c29888964024695bade278ab2 F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c @@ -1787,7 +1787,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P d8dd98a39ea061dea264a7d81153f7b1be2f81b554b30d0ce289897c802209bd -R 7649521e36559a5f6fcaed801161c713 +P 18b2c23ac53d985ccc5798ea2d92fb75644b857c373fb490e0d04d5d0194a3d5 +R 234c1705f2bf2a9abb44249d0ee7aea0 U dan -Z 45541997f2aea4861e4215e9e1638ee2 +Z d3854fa2ddea650fc625b1a32d28af78 diff --git a/manifest.uuid b/manifest.uuid index 5aa435a1c6..60eb3eb58e 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -18b2c23ac53d985ccc5798ea2d92fb75644b857c373fb490e0d04d5d0194a3d5 \ No newline at end of file +4d5779f31d4931edb8bb8952d8886625ead5e51f0c308e5763e519427f6609e1 \ No newline at end of file diff --git a/test/wal2simple.test b/test/wal2simple.test index 28e13bf162..3d35b026c6 100644 --- a/test/wal2simple.test +++ b/test/wal2simple.test @@ -114,7 +114,6 @@ do_execsql_test 3.0 { PRAGMA journal_mode = wal2; } {wal2} -breakpoint do_test 3.1 { execsql BEGIN for {set i 1} {$i < 1000} {incr i} { @@ -287,4 +286,189 @@ do_test 6.4.2 { } {wal2} db2 close +#------------------------------------------------------------------------- +reset_db +sqlite3 db2 test.db +do_execsql_test 7.0 { + PRAGMA journal_size_limit = 10000; + PRAGMA journal_mode = wal2; + PRAGMA wal_autocheckpoint = 0; + BEGIN; + CREATE TABLE t1(a); + INSERT INTO t1 VALUES( randomblob(8000) ); + COMMIT; +} {10000 wal2 0} + +do_test 7.1 { + list [file size test.db-wal] [file size test.db-wal2] +} {9464 0} + +# Connection db2 is holding a PART1 lock. +# +# 7.2.2: Test that the PART1 does not prevent db from switching to the +# other wal file. +# +# 7.2.3: Test that the PART1 does prevent a checkpoint of test.db-wal. +# +# 7.2.4: Test that after the PART1 is released the checkpoint is possible. +# +do_test 7.2.1 { + execsql { + BEGIN; + SELECT count(*) FROM t1; + } db2 +} {1} +do_test 7.2.2 { + execsql { + INSERT INTO t1 VALUES( randomblob(800) ); + INSERT INTO t1 VALUES( randomblob(800) ); + } + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {13656 3176 1024} +do_test 7.2.3 { + execsql { PRAGMA wal_checkpoint } + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {13656 3176 1024} +do_test 7.2.4 { + execsql { END } db2 + execsql { PRAGMA wal_checkpoint } + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {13656 3176 11264} + +# Connection db2 is holding a PART2_FULL1 lock. +# +# 7.3.2: Test that the lock does not prevent checkpointing. +# +# 7.3.3: Test that the lock does prevent the writer from overwriting +# test.db-wal. +# +# 7.3.4: Test that after the PART2_FULL1 is released the writer can +# switch wal files and overwrite test.db-wal +# +db close +db2 close +sqlite3 db test.db +sqlite3 db2 test.db +do_test 7.3.1 { + execsql { + PRAGMA wal_autocheckpoint = 0; + PRAGMA journal_size_limit = 10000; + INSERT INTO t1 VALUES(randomblob(10000)); + INSERT INTO t1 VALUES(randomblob(500)); + } + execsql { + BEGIN; + SELECT count(*) FROM t1; + } db2 + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {12608 3176 11264} +do_test 7.3.2 { + execsql { PRAGMA wal_checkpoint } + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {12608 3176 21504} +do_test 7.3.3 { + execsql { + INSERT INTO t1 VALUES(randomblob(10000)); + INSERT INTO t1 VALUES(randomblob(500)); + } + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {12608 18896 21504} +do_test 7.3.4 { + execsql END db2 + execsql { INSERT INTO t1 VALUES(randomblob(5000)); } + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {12608 18896 21504} + +# Connection db2 is holding a PART2 lock. +# +# 7.4.2: Test that the lock does not prevent writer switching to test.db-wal. +# +# 7.3.3: Test that the lock does prevent checkpointing of test.db-wal2. +# +# 7.3.4: Test that after the PART2 is released test.db-wal2 can be +# checkpointed. +# +db close +db2 close +sqlite3 db test.db +sqlite3 db2 test.db +do_test 7.4.1 { + execsql { + PRAGMA wal_autocheckpoint = 0; + PRAGMA journal_size_limit = 10000; + INSERT INTO t1 VALUES(randomblob(10000)); + INSERT INTO t1 VALUES(randomblob(10000)); + PRAGMA wal_checkpoint; + } + execsql { + BEGIN; + SELECT count(*) FROM t1; + } db2 + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {12608 12608 44032} +do_test 7.4.2 { + execsql { + INSERT INTO t1 VALUES(randomblob(5000)); + } + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {12608 12608 44032} +do_test 7.4.3 { + execsql { PRAGMA wal_checkpoint } + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {12608 12608 44032} +do_test 7.4.4 { + execsql END db2 + execsql { PRAGMA wal_checkpoint } + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {12608 12608 54272} + +# Connection db2 is holding a PART1_FULL2 lock. +# +# 7.5.2: Test that the lock does not prevent a checkpoint of test.db-wal2. +# +# 7.5.3: Test that the lock does prevent the writer from overwriting +# test.db-wal2. +# +# 7.5.4: Test that after the PART1_FULL2 lock is released, the writer +# can switch to test.db-wal2. +# +db close +db2 close +sqlite3 db test.db +sqlite3 db2 test.db +do_test 7.5.1 { + execsql { + PRAGMA wal_autocheckpoint = 0; + PRAGMA journal_size_limit = 10000; + INSERT INTO t1 VALUES(randomblob(10000)); + INSERT INTO t1 VALUES(randomblob(10000)); + PRAGMA wal_checkpoint; + INSERT INTO t1 VALUES(randomblob(5000)); + } + execsql { + BEGIN; + SELECT count(*) FROM t1; + } db2 + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {12608 12608 64512} +do_test 7.5.2 { + execsql { PRAGMA wal_checkpoint } + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {12608 12608 75776} +do_test 7.5.3.1 { + execsql { INSERT INTO t1 VALUES(randomblob(5000)) } + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {14704 12608 75776} +do_test 7.5.3.2 { + execsql { INSERT INTO t1 VALUES(randomblob(5000)) } + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {22040 12608 75776} +do_test 7.5.4 { + execsql END db2 + execsql { INSERT INTO t1 VALUES(randomblob(5000)) } + list [file size test.db-wal] [file size test.db-wal2] [file size test.db] +} {22040 12608 75776} + + finish_test + From eeb778d506e429549090fd78b75282b579908e8a Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 12 Dec 2018 20:39:38 +0000 Subject: [PATCH 16/51] Add new test file wal2big.test. FossilOrigin-Name: e3e50bcdab8c91e003942d84430b3e580e034141236d19dda0e8af4ecf0e085b --- manifest | 13 ++++++----- manifest.uuid | 2 +- src/wal.c | 9 ++------ test/wal2big.test | 59 +++++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 69 insertions(+), 14 deletions(-) create mode 100644 test/wal2big.test diff --git a/manifest b/manifest index 4cb34c27b4..8249da62fe 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\stests\sto\sensure\sthat\seach\sof\sthe\s4\swal\sread-locks\sdoes\swhat\sit\sis\ssupposed\sto. -D 2018-12-12T19:04:19.425 +C Add\snew\stest\sfile\swal2big.test. +D 2018-12-12T20:39:38.782 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in 68d0ba0f0b533d5bc84c78c13a6ce84ee81183a67014caa47a969e67f028fa1c @@ -590,7 +590,7 @@ F src/vdbesort.c 90aad5a92608f2dd771c96749beabdb562c9d881131a860a7a5bccf66dc3be7 F src/vdbetrace.c 79d6dbbc479267b255a7de8080eee6e729928a0ef93ed9b0bfa5618875b48392 F src/vtab.c 70188a745dc4e57d26e942681ff4b2912b7c8249ad5de3f60f0677b4337bcfaa F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 22850fcd2efa9b13e237f6097f931fe7dbd9e50f401a659288d70ae3951396b8 +F src/wal.c 7106d08dce79c9c5338c2b194139c418f21cf30dc2440145ce788893dd9b49cc F src/wal.h d2a69695c84137f76e19a247a342cb02ab0131001b6f58153d94b71195bbd84d F src/walker.c fb94aadc9099ff9c6506d0a8b88d51266005bcaa265403f3d7caf732a562eb66 F src/where.c 3818e8a736a05d2cb194e64399af707e367fbcc5c251d785804d02eaf121288e @@ -1596,6 +1596,7 @@ F test/vtab_err.test dcc8b7b9cb67522b3fe7a272c73856829dae4ab7fdb30399aea1b6981bd F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad F test/wal.test 613efec03e517e1775d86b993a54877d2e29a477 F test/wal2.test 155b9efa999bdb38ce1cd729b9a4fcdbffd6b88be27f039bad1d2929d287d918 +F test/wal2big.test b6e78336b7aa517bf81953e118d73cd405549fb95159fab7d2b67a8eb5990ade F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal2simple.test 96206c98bf64ab20ec00a1c0f6c709e258b98b39f2149889361f31966ce5a703 F test/wal2snapshot.test 95a919e1c73dee0e0212d10931d03cc1116f68a0ff603163e551aaa5ac7025c1 @@ -1787,7 +1788,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 18b2c23ac53d985ccc5798ea2d92fb75644b857c373fb490e0d04d5d0194a3d5 -R 234c1705f2bf2a9abb44249d0ee7aea0 +P 4d5779f31d4931edb8bb8952d8886625ead5e51f0c308e5763e519427f6609e1 +R 4985894a481d77e7477a50c3c152405c U dan -Z d3854fa2ddea650fc625b1a32d28af78 +Z 72ed7e3951a42a191c8ff32f1f42f36f diff --git a/manifest.uuid b/manifest.uuid index 60eb3eb58e..65c0861b34 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -4d5779f31d4931edb8bb8952d8886625ead5e51f0c308e5763e519427f6609e1 \ No newline at end of file +e3e50bcdab8c91e003942d84430b3e580e034141236d19dda0e8af4ecf0e085b \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 634de7ec6f..4cc37d0700 100644 --- a/src/wal.c +++ b/src/wal.c @@ -1204,14 +1204,9 @@ static int walExternalDecode(u32 iExternal, u32 *piRead){ iExternal - (iHash/2) * HASHTABLE_NPAGE; return 0; } - if( iHash==0 ){ - *piRead = iExternal; - return 0; - }else{ - *piRead = iExternal - HASHTABLE_NPAGE_ONE - ((iHash-1)/2) * HASHTABLE_NPAGE; - } - return (iHash % 2); + *piRead = iExternal - HASHTABLE_NPAGE_ONE - ((iHash-1)/2) * HASHTABLE_NPAGE; + return 1; } /* diff --git a/test/wal2big.test b/test/wal2big.test new file mode 100644 index 0000000000..2758108479 --- /dev/null +++ b/test/wal2big.test @@ -0,0 +1,59 @@ +# 2017 September 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL2" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl + +set testprefix wal2big +ifcapable !wal {finish_test ; return } + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1b ON t1(b); + CREATE INDEX t1c ON t1(c); + PRAGMA journal_mode = wal2; + PRAGMA journal_size_limit = 10000000; + + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<200000 + ) + INSERT INTO t1 SELECT random(), random(), random() FROM s; +} {wal2 10000000} + +do_execsql_test 1.1 { + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s WHERE i<200000 + ) + INSERT INTO t1 SELECT random(), random(), random() FROM s; +} + +do_test 1.1 { + list [expr [file size test.db-wal]>10000000] \ + [expr [file size test.db-wal2]>10000000] +} {1 1} + +do_test 1.2 { + sqlite3 db2 test.db + execsql { + SELECT count(*) FROM t1; + PRAGMA integrity_check; + } db2 +} {400000 ok} + +finish_test From 597bf19266ad8b148d8ff9d88b99a9fdf032f9d7 Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 13 Dec 2018 16:26:43 +0000 Subject: [PATCH 17/51] Add tests cases for recovery in wal2 mode. FossilOrigin-Name: 34f56f8a4239781071edf6317723d0a7333249551c2c1ce7ed39fc116d53d780 --- manifest | 15 ++-- manifest.uuid | 2 +- src/wal.c | 8 +- test/wal2big.test | 16 +++- test/wal2recover.test | 167 ++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 194 insertions(+), 14 deletions(-) create mode 100644 test/wal2recover.test diff --git a/manifest b/manifest index 8249da62fe..06ffd7fd7e 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\snew\stest\sfile\swal2big.test. -D 2018-12-12T20:39:38.782 +C Add\stests\scases\sfor\srecovery\sin\swal2\smode. +D 2018-12-13T16:26:43.514 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in 68d0ba0f0b533d5bc84c78c13a6ce84ee81183a67014caa47a969e67f028fa1c @@ -590,7 +590,7 @@ F src/vdbesort.c 90aad5a92608f2dd771c96749beabdb562c9d881131a860a7a5bccf66dc3be7 F src/vdbetrace.c 79d6dbbc479267b255a7de8080eee6e729928a0ef93ed9b0bfa5618875b48392 F src/vtab.c 70188a745dc4e57d26e942681ff4b2912b7c8249ad5de3f60f0677b4337bcfaa F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 7106d08dce79c9c5338c2b194139c418f21cf30dc2440145ce788893dd9b49cc +F src/wal.c 93389c399264ca3e0728b7bc2565b4ee6a9099b78929e633ffad3bcd493e415e F src/wal.h d2a69695c84137f76e19a247a342cb02ab0131001b6f58153d94b71195bbd84d F src/walker.c fb94aadc9099ff9c6506d0a8b88d51266005bcaa265403f3d7caf732a562eb66 F src/where.c 3818e8a736a05d2cb194e64399af707e367fbcc5c251d785804d02eaf121288e @@ -1596,7 +1596,8 @@ F test/vtab_err.test dcc8b7b9cb67522b3fe7a272c73856829dae4ab7fdb30399aea1b6981bd F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad F test/wal.test 613efec03e517e1775d86b993a54877d2e29a477 F test/wal2.test 155b9efa999bdb38ce1cd729b9a4fcdbffd6b88be27f039bad1d2929d287d918 -F test/wal2big.test b6e78336b7aa517bf81953e118d73cd405549fb95159fab7d2b67a8eb5990ade +F test/wal2big.test 0b4ec526f9ca4bbabc355042c38045ae2e253fb46eb327bb7693d0122bc6968b +F test/wal2recover.test 4f14625c65cdb6132e061f85d2d441f8bb96aea4b20d4261bcf14041d8e1a735 F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal2simple.test 96206c98bf64ab20ec00a1c0f6c709e258b98b39f2149889361f31966ce5a703 F test/wal2snapshot.test 95a919e1c73dee0e0212d10931d03cc1116f68a0ff603163e551aaa5ac7025c1 @@ -1788,7 +1789,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 4d5779f31d4931edb8bb8952d8886625ead5e51f0c308e5763e519427f6609e1 -R 4985894a481d77e7477a50c3c152405c +P e3e50bcdab8c91e003942d84430b3e580e034141236d19dda0e8af4ecf0e085b +R e3b17b10c3f7dc5dae3bc5cc7a744091 U dan -Z 72ed7e3951a42a191c8ff32f1f42f36f +Z 6979d16521822ff458ef9e063c382318 diff --git a/manifest.uuid b/manifest.uuid index 65c0861b34..edb729f46a 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -e3e50bcdab8c91e003942d84430b3e580e034141236d19dda0e8af4ecf0e085b \ No newline at end of file +34f56f8a4239781071edf6317723d0a7333249551c2c1ce7ed39fc116d53d780 \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 4cc37d0700..7d334ed50b 100644 --- a/src/wal.c +++ b/src/wal.c @@ -1656,8 +1656,8 @@ static int walIndexRecover(Wal *pWal){ /* The case where *-wal2 may follow *-wal */ if( nCkpt2<=0x0F && nCkpt2==nCkpt1+1 ){ if( sqlite3Get4byte((u8*)(&pWal->hdr.aSalt[0]))==hdr.aFrameCksum[0] - && sqlite3Get4byte((u8*)(&pWal->hdr.aSalt[1]))==hdr.aFrameCksum[1] - ){ + && sqlite3Get4byte((u8*)(&pWal->hdr.aSalt[1]))==hdr.aFrameCksum[1] + ){ walidxSetFile(&pWal->hdr, 1); walidxSetMxFrame(&pWal->hdr, 1, pWal->hdr.mxFrame); walidxSetMxFrame(&pWal->hdr, 0, hdr.mxFrame); @@ -1715,9 +1715,9 @@ static int walIndexRecover(Wal *pWal){ if( pWal->hdr.nPage ){ if( isWalMode2(pWal) ){ sqlite3_log(SQLITE_NOTICE_RECOVER_WAL, - "recovered (%d,%d) frames from WAL files %s[2] (%s mode)", + "recovered (%d,%d) frames from WAL files %s[2] (wal2 mode)", walidxGetMxFrame(&pWal->hdr, 0), walidxGetMxFrame(&pWal->hdr, 1), - pWal->zWalName, isWalMode2(pWal) ? "wal2" : "wal" + pWal->zWalName ); }else{ sqlite3_log(SQLITE_NOTICE_RECOVER_WAL, diff --git a/test/wal2big.test b/test/wal2big.test index 2758108479..53552e3639 100644 --- a/test/wal2big.test +++ b/test/wal2big.test @@ -43,12 +43,12 @@ do_execsql_test 1.1 { INSERT INTO t1 SELECT random(), random(), random() FROM s; } -do_test 1.1 { +do_test 1.2 { list [expr [file size test.db-wal]>10000000] \ [expr [file size test.db-wal2]>10000000] } {1 1} -do_test 1.2 { +do_test 1.3 { sqlite3 db2 test.db execsql { SELECT count(*) FROM t1; @@ -56,4 +56,16 @@ do_test 1.2 { } db2 } {400000 ok} +do_test 1.4 { + db2 close + forcecopy test.db test.db2 + forcecopy test.db-wal test.db2-wal + forcecopy test.db-wal2 test.db2-wal2 + sqlite3 db2 test.db2 + execsql { + SELECT count(*) FROM t1; + PRAGMA integrity_check; + } +} {400000 ok} + finish_test diff --git a/test/wal2recover.test b/test/wal2recover.test new file mode 100644 index 0000000000..fecd85829c --- /dev/null +++ b/test/wal2recover.test @@ -0,0 +1,167 @@ +# 2018 December 13 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL2" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl + +set testprefix wal2recover +ifcapable !wal {finish_test ; return } + +proc db_copy {from to} { + forcecopy $from $to + forcecopy ${from}-wal ${to}-wal + forcecopy ${from}-wal2 ${to}-wal2 +} + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1b ON t1(b); + CREATE INDEX t1c ON t1(c); + PRAGMA journal_mode = wal2; + PRAGMA journal_size_limit = 15000; + PRAGMA wal_autocheckpoint = 0; +} {wal2 15000 0} + +do_test 1.1 { + for {set i 1} {$i <= 1000} {incr i} { + execsql { INSERT INTO t1 VALUES(random(), random(), random()) } + db_copy test.db test.db2 + sqlite3 db2 test.db + set res [execsql { + SELECT count(*) FROM t1; + PRAGMA integrity_check; + } db2] + db2 close + if {$res != [list $i ok]} { + error "failure on iteration $i" + } + } + set {} {} +} {} + +#-------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(x UNIQUE); + CREATE TABLE t2(x UNIQUE); + PRAGMA journal_mode = wal2; + PRAGMA journal_size_limit = 10000; + PRAGMA wal_autocheckpoint = 0; + BEGIN; + INSERT INTO t1 VALUES(randomblob(4000)); + INSERT INTO t1 VALUES(randomblob(4000)); + INSERT INTO t1 VALUES(randomblob(4000)); + COMMIT; + BEGIN; + INSERT INTO t2 VALUES(randomblob(4000)); + INSERT INTO t2 VALUES(randomblob(4000)); + INSERT INTO t2 VALUES(randomblob(4000)); + COMMIT; +} {wal2 10000 0} +do_test 2.0.1 { + list [file size test.db] [file size test.db-wal] [file size test.db-wal2] +} {5120 28328 28328} + +# Test recovery with both wal files intact. +# +do_test 2.1 { + db_copy test.db test.db2 + sqlite3 db2 test.db2 + execsql { + SELECT count(*) FROM t1; + SELECT count(*) FROM t2; + PRAGMA integrity_check; + } db2 +} {3 3 ok} + +do_test 2.2 { + db2 close + db_copy test.db test.db2 + hexio_write test.db2-wal 16 12345678 + sqlite3 db2 test.db2 + execsql { + SELECT count(*) FROM t1; + SELECT count(*) FROM t2; + } db2 +} {0 3} + +do_test 2.3 { + db2 close + db_copy test.db test.db2 + hexio_write test.db2-wal2 16 12345678 + sqlite3 db2 test.db2 + execsql { + SELECT count(*) FROM t1; + SELECT count(*) FROM t2; + PRAGMA integrity_check; + } db2 +} {3 0 ok} + +do_test 2.4 { + db2 close + db_copy test.db test.db2 + forcecopy test.db-wal test.db2-wal2 + sqlite3 db2 test.db2 + execsql { + SELECT count(*) FROM t1; + SELECT count(*) FROM t2; + PRAGMA integrity_check; + } db2 +} {3 0 ok} + +do_test 2.5 { + db2 close + db_copy test.db test.db2 + forcecopy test.db-wal test.db2-wal2 + forcecopy test.db-wal2 test.db2-wal + sqlite3 db2 test.db2 + execsql { + SELECT count(*) FROM t1; + SELECT count(*) FROM t2; + PRAGMA integrity_check; + } db2 +} {3 3 ok} + +do_test 2.6 { + db2 close + db_copy test.db test.db2 + forcecopy test.db-wal test.db2-wal2 + close [open test.db-wal w] + sqlite3 db2 test.db2 + execsql { + SELECT count(*) FROM t1; + SELECT count(*) FROM t2; + PRAGMA integrity_check; + } db2 +} {3 0 ok} + +do_test 2.7 { + db2 close + db_copy test.db test.db2 + forcedelete test.db2-wal + sqlite3 db2 test.db2 + execsql { + SELECT count(*) FROM t1; + SELECT count(*) FROM t2; + PRAGMA integrity_check; + } db2 +} {0 0 ok} + +finish_test + From e698c1f79ea8ab498fa863f7dab2bbc91946de6c Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 14 Dec 2018 19:53:39 +0000 Subject: [PATCH 18/51] Add further tests for wal2 mode. FossilOrigin-Name: 54e628f902a5508badc8941ceda1bec12fe8f58969c91c670a37888ef2681880 --- manifest | 17 +++---- manifest.uuid | 2 +- src/wal.c | 2 +- test/permutations.test | 14 ++++-- test/wal2recover.test | 99 +++++++++++++++++++++++++++++++++++++++++ test/wal2savepoint.test | 60 +++++++++++++++++++++++++ 6 files changed, 180 insertions(+), 14 deletions(-) create mode 100644 test/wal2savepoint.test diff --git a/manifest b/manifest index 06ffd7fd7e..88e15baab9 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\stests\scases\sfor\srecovery\sin\swal2\smode. -D 2018-12-13T16:26:43.514 +C Add\sfurther\stests\sfor\swal2\smode. +D 2018-12-14T19:53:39.654 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in 68d0ba0f0b533d5bc84c78c13a6ce84ee81183a67014caa47a969e67f028fa1c @@ -590,7 +590,7 @@ F src/vdbesort.c 90aad5a92608f2dd771c96749beabdb562c9d881131a860a7a5bccf66dc3be7 F src/vdbetrace.c 79d6dbbc479267b255a7de8080eee6e729928a0ef93ed9b0bfa5618875b48392 F src/vtab.c 70188a745dc4e57d26e942681ff4b2912b7c8249ad5de3f60f0677b4337bcfaa F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 93389c399264ca3e0728b7bc2565b4ee6a9099b78929e633ffad3bcd493e415e +F src/wal.c 1deb1d320b5de56407172ac21577d2bbf5c6ea52dcc0f5315a8d926d4839146e F src/wal.h d2a69695c84137f76e19a247a342cb02ab0131001b6f58153d94b71195bbd84d F src/walker.c fb94aadc9099ff9c6506d0a8b88d51266005bcaa265403f3d7caf732a562eb66 F src/where.c 3818e8a736a05d2cb194e64399af707e367fbcc5c251d785804d02eaf121288e @@ -1182,7 +1182,7 @@ F test/parser1.test 6ccdf5e459a5dc4673d3273dc311a7e9742ca952dd0551a6a6320d27035c F test/pcache.test c8acbedd3b6fd0f9a7ca887a83b11d24a007972b F test/pcache2.test af7f3deb1a819f77a6d0d81534e97d1cf62cd442 F test/percentile.test 4243af26b8f3f4555abe166f723715a1f74c77ff -F test/permutations.test 57c9f829d72c91c89ae08febe205b73ec22751f476b4dafeafb614dfd263d74c +F test/permutations.test f97a5613430a62ef5fb5aafeb37d4e857dea44e47c31064f154c7049c25b292b F test/pg_common.tcl 301ac19c1a52fd55166d26db929b3b89165c634d52b5f8ad76ea8cb06960db30 F test/pragma.test c267bf02742c823a191960895b3d52933cebd7beee26757d1ed694f213fcd867 F test/pragma2.test e5d5c176360c321344249354c0c16aec46214c9f @@ -1597,8 +1597,9 @@ F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad F test/wal.test 613efec03e517e1775d86b993a54877d2e29a477 F test/wal2.test 155b9efa999bdb38ce1cd729b9a4fcdbffd6b88be27f039bad1d2929d287d918 F test/wal2big.test 0b4ec526f9ca4bbabc355042c38045ae2e253fb46eb327bb7693d0122bc6968b -F test/wal2recover.test 4f14625c65cdb6132e061f85d2d441f8bb96aea4b20d4261bcf14041d8e1a735 +F test/wal2recover.test 64155a1f13ac434ebb374f109b9daa2b52c3ca69db9199be0bcbabb9727cae4e F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c +F test/wal2savepoint.test 29725383e7eb8b78fed46333d69dc45b4ed321db43ecc81766aadea991874bef F test/wal2simple.test 96206c98bf64ab20ec00a1c0f6c709e258b98b39f2149889361f31966ce5a703 F test/wal2snapshot.test 95a919e1c73dee0e0212d10931d03cc1116f68a0ff603163e551aaa5ac7025c1 F test/wal3.test 2a93004bc0fb2b5c29888964024695bade278ab2 @@ -1789,7 +1790,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P e3e50bcdab8c91e003942d84430b3e580e034141236d19dda0e8af4ecf0e085b -R e3b17b10c3f7dc5dae3bc5cc7a744091 +P 34f56f8a4239781071edf6317723d0a7333249551c2c1ce7ed39fc116d53d780 +R cabac54018fcd32b9b9c7e5679bc38f9 U dan -Z 6979d16521822ff458ef9e063c382318 +Z be9d783248b67e630efcf341b29cd59e diff --git a/manifest.uuid b/manifest.uuid index edb729f46a..0fc73c3fc5 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -34f56f8a4239781071edf6317723d0a7333249551c2c1ce7ed39fc116d53d780 \ No newline at end of file +54e628f902a5508badc8941ceda1bec12fe8f58969c91c670a37888ef2681880 \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 7d334ed50b..4ce5a67585 100644 --- a/src/wal.c +++ b/src/wal.c @@ -1532,7 +1532,7 @@ static int walIndexRecoverOne(Wal *pWal, int iWal, u32 *pnCkpt, int *pbZero){ } sqlite3_free(aFrame); - }else if( pbZero && nSize==0 ){ + }else if( pbZero ){ *pbZero = 1; } } diff --git a/test/permutations.test b/test/permutations.test index 7e8f38ed1e..75f6ecbc02 100644 --- a/test/permutations.test +++ b/test/permutations.test @@ -430,10 +430,16 @@ lappend ::testsuitelist xxx test_suite "coverage-wal" -description { Coverage tests for file wal.c. } -files { - wal.test wal2.test wal3.test walmode.test - walbak.test walhook.test walcrash2.test walcksum.test - walfault.test walbig.test walnoshm.test - wal5.test +wal2big.test wal2recover.test wal2rewrite.test +wal2simple.test wal2snapshot.test wal2.test +wal3.test wal4.test wal5.test +wal64k.test wal6.test wal7.test wal8.test wal9.test +walbak.test walbig.test walblock.test walcksum.test +walfault.test walhook.test walmode.test walnoshm.test +waloverwrite.test walpersist.test walprotocol2.test +walprotocol.test walro2.test walrofault.test walro.test +walshared.test walslow.test wal.test +wal2savepoint.test } test_suite "coverage-pager" -description { diff --git a/test/wal2recover.test b/test/wal2recover.test index fecd85829c..e8cecc1bf7 100644 --- a/test/wal2recover.test +++ b/test/wal2recover.test @@ -163,5 +163,104 @@ do_test 2.7 { } db2 } {0 0 ok} +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 3.0 { + CREATE TABLE t1(a TEXT, b TEXT, c TEXT); + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1b ON t1(b); + CREATE INDEX t1c ON t1(c); + PRAGMA journal_mode = wal2; + PRAGMA journal_size_limit = 10000; + PRAGMA wal_autocheckpoint = 0; + PRAGMA cache_size = 5; +} {wal2 10000 0} + +do_execsql_test 3.1 { + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s where i < 200) + INSERT INTO t1 SELECT i, i, i FROM s; + + INSERT INTO t1 VALUES(201, 201, 201); +} {} + +do_test 3.2 { + list [file size test.db] [file size test.db-wal] [file size test.db-wal2] +} {5120 15752 4224} + +do_test 3.3 { + forcecopy test.db test.db2 + forcecopy test.db-wal test.db2-wal + forcecopy test.db-wal2 test.db2-wal2 + sqlite3 db2 test.db2 + execsql { + PRAGMA journal_size_limit = 10000; + PRAGMA wal_autocheckpoint = 0; + PRAGMA cache_size = 5; + BEGIN; + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s where i < 200) + INSERT INTO t1 SELECT i, i, i FROM s; + } db2 + list [file size test.db2] [file size test.db2-wal] [file size test.db2-wal2] +} {5120 15752 23088} + +do_test 3.4 { + set fd [open test.db2-shm] + fconfigure $fd -encoding binary -translation binary + set data [read $fd] + close $fd + + set fd [open test.db-shm w] + fconfigure $fd -encoding binary -translation binary + puts -nonewline $fd $data + close $fd + + execsql { + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s where i < 10) + INSERT INTO t1 SELECT i, i, i FROM s; + SELECT count(*) FROM t1; + PRAGMA integrity_check; + } +} {211 ok} + +do_test 3.5 { + list [file size test.db] [file size test.db-wal] [file size test.db-wal2] +} {5120 15752 18896} + +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 4.0 { + PRAGMA journal_mode = wal2; + CREATE TABLE xyz(x, y, z); + INSERT INTO xyz VALUES('x', 'y', 'z'); +} {wal2} +db close +do_test 4.1 { + close [open test.db-wal w] + file mkdir test.db-wal2 + sqlite3 db test.db + catchsql { SELECT * FROM xyz } +} {1 {unable to open database file}} +db close +file delete test.db-wal2 + +do_test 4.2 { + sqlite3 db test.db + execsql { + INSERT INTO xyz VALUES('a', 'b', 'c'); + } + forcecopy test.db test.db2 + forcecopy test.db-wal test.db2-wal + forcedelete test.db2-wal2 + file mkdir test.db2-wal2 + sqlite3 db2 test.db2 + catchsql { SELECT * FROM xyz } db2 +} {1 {unable to open database file}} +db2 close +file delete test.db2-wal2 + + + finish_test diff --git a/test/wal2savepoint.test b/test/wal2savepoint.test new file mode 100644 index 0000000000..c875af88c4 --- /dev/null +++ b/test/wal2savepoint.test @@ -0,0 +1,60 @@ +# 2018 December 13 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL2" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl + +set testprefix wal2savepoint +ifcapable !wal {finish_test ; return } + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + CREATE INDEX t1a ON t1(a); + CREATE INDEX t1b ON t1(b); + CREATE INDEX t1c ON t1(c); + PRAGMA journal_mode = wal2; + PRAGMA journal_size_limit = 15000; + PRAGMA wal_autocheckpoint = 0; + PRAGMA cache_size = 5; +} {wal2 15000 0} + +do_execsql_test 1.1 { + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s where i < 200) + INSERT INTO t1 SELECT random(), random(), random() FROM s; +} {} + +do_test 1.2 { + list [file size test.db] [file size test.db-wal] [file size test.db-wal2] +} {5120 23088 0} + +do_execsql_test 1.3 { + BEGIN; + SAVEPOINT abc; + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s where i < 100) + INSERT INTO t1 SELECT random(), random(), random() FROM s; + ROLLBACK TO abc; + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s where i < 10) + INSERT INTO t1 SELECT random(), random(), random() FROM s; + COMMIT; + SELECT count(*) FROM t1; + PRAGMA integrity_check; +} {210 ok} + + +finish_test + From 93543215e03af2d0c3b0b85712a7a1b6e4381e4b Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 15 Dec 2018 20:20:13 +0000 Subject: [PATCH 19/51] Further test cases for wal2 mode. FossilOrigin-Name: 9cb5f8dab685f5ea36ad142cfa588dee82e87f3a89e8dcf84e0ee124bb29bc7f --- manifest | 20 ++-- manifest.uuid | 2 +- src/test_hexio.c | 22 +++- src/wal.c | 2 +- test/permutations.test | 2 +- test/wal2lock.test | 106 +++++++++++++++++++ test/wal2recover.test | 34 ------ test/wal2recover2.test | 227 +++++++++++++++++++++++++++++++++++++++++ 8 files changed, 364 insertions(+), 51 deletions(-) create mode 100644 test/wal2lock.test create mode 100644 test/wal2recover2.test diff --git a/manifest b/manifest index 88e15baab9..3d69ddd17c 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\sfurther\stests\sfor\swal2\smode. -D 2018-12-14T19:53:39.654 +C Further\stest\scases\sfor\swal2\smode. +D 2018-12-15T20:20:13.338 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in 68d0ba0f0b533d5bc84c78c13a6ce84ee81183a67014caa47a969e67f028fa1c @@ -539,7 +539,7 @@ F src/test_demovfs.c a0c3bdd45ed044115c2c9f7779e56eafff18741e F src/test_devsym.c 1960abbb234b97e9b920f07e99503fc04b443f62bbc3c6ff2c2cea2133e3b8a2 F src/test_fs.c 35a2f7dd8a915900873386331386d9ba1ae1b5026d74fd20c2807bc76221f291 F src/test_func.c d12d805953bcb3bb19f71d29cdc93383b7b7a3369504d2b7e398a1bd77376294 -F src/test_hexio.c 1d4469ca61ab202a1fcec6543f584d2407205e8d +F src/test_hexio.c 1a70ba996c8c8c028469b428ceca0d2c9c84fae68589984022ed3fc6e706eaf7 F src/test_init.c 4413c211a94b62157ca4c145b3f27c497f03c664 F src/test_intarray.c 39b4181662a0f33a427748d87218e7578d913e683dc27eab7098bb41617cac71 F src/test_intarray.h d57ae92f420cda25e22790dac474d60961bd0c500cbaa3338a05152d4a669ef7 @@ -590,7 +590,7 @@ F src/vdbesort.c 90aad5a92608f2dd771c96749beabdb562c9d881131a860a7a5bccf66dc3be7 F src/vdbetrace.c 79d6dbbc479267b255a7de8080eee6e729928a0ef93ed9b0bfa5618875b48392 F src/vtab.c 70188a745dc4e57d26e942681ff4b2912b7c8249ad5de3f60f0677b4337bcfaa F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 1deb1d320b5de56407172ac21577d2bbf5c6ea52dcc0f5315a8d926d4839146e +F src/wal.c beca1487f7cecd1d6a965ba899a5a6b8868b3eb80580d33b2a73f8e37ab4901b F src/wal.h d2a69695c84137f76e19a247a342cb02ab0131001b6f58153d94b71195bbd84d F src/walker.c fb94aadc9099ff9c6506d0a8b88d51266005bcaa265403f3d7caf732a562eb66 F src/where.c 3818e8a736a05d2cb194e64399af707e367fbcc5c251d785804d02eaf121288e @@ -1182,7 +1182,7 @@ F test/parser1.test 6ccdf5e459a5dc4673d3273dc311a7e9742ca952dd0551a6a6320d27035c F test/pcache.test c8acbedd3b6fd0f9a7ca887a83b11d24a007972b F test/pcache2.test af7f3deb1a819f77a6d0d81534e97d1cf62cd442 F test/percentile.test 4243af26b8f3f4555abe166f723715a1f74c77ff -F test/permutations.test f97a5613430a62ef5fb5aafeb37d4e857dea44e47c31064f154c7049c25b292b +F test/permutations.test c7090c0136224e2ca400f1e0a085dad13a4f40a03387881e26f1a4cd1344d557 F test/pg_common.tcl 301ac19c1a52fd55166d26db929b3b89165c634d52b5f8ad76ea8cb06960db30 F test/pragma.test c267bf02742c823a191960895b3d52933cebd7beee26757d1ed694f213fcd867 F test/pragma2.test e5d5c176360c321344249354c0c16aec46214c9f @@ -1597,7 +1597,9 @@ F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad F test/wal.test 613efec03e517e1775d86b993a54877d2e29a477 F test/wal2.test 155b9efa999bdb38ce1cd729b9a4fcdbffd6b88be27f039bad1d2929d287d918 F test/wal2big.test 0b4ec526f9ca4bbabc355042c38045ae2e253fb46eb327bb7693d0122bc6968b -F test/wal2recover.test 64155a1f13ac434ebb374f109b9daa2b52c3ca69db9199be0bcbabb9727cae4e +F test/wal2lock.test 0ef98d72dc6bcf7711dedd684760488400d9a9a6eec0dc5d3822060437793552 +F test/wal2recover.test da390d40c955a74b8a049d41248f92f928a5a5ee6bbcca33cb665c95b736c11c +F test/wal2recover2.test 98749381c2e61574e181a2e288295a3bf93d12f57769db50ecea7fc211d9ad5c F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal2savepoint.test 29725383e7eb8b78fed46333d69dc45b4ed321db43ecc81766aadea991874bef F test/wal2simple.test 96206c98bf64ab20ec00a1c0f6c709e258b98b39f2149889361f31966ce5a703 @@ -1790,7 +1792,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 34f56f8a4239781071edf6317723d0a7333249551c2c1ce7ed39fc116d53d780 -R cabac54018fcd32b9b9c7e5679bc38f9 +P 54e628f902a5508badc8941ceda1bec12fe8f58969c91c670a37888ef2681880 +R e2c9e8287795c59cafc4b6dc4a5ee2a2 U dan -Z be9d783248b67e630efcf341b29cd59e +Z 964094b4a0f5d261b4d47067580a5ce8 diff --git a/manifest.uuid b/manifest.uuid index 0fc73c3fc5..c6c9f8cdaa 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -54e628f902a5508badc8941ceda1bec12fe8f58969c91c670a37888ef2681880 \ No newline at end of file +9cb5f8dab685f5ea36ad142cfa588dee82e87f3a89e8dcf84e0ee124bb29bc7f \ No newline at end of file diff --git a/src/test_hexio.c b/src/test_hexio.c index 7b62ea08bc..1ffedec7f0 100644 --- a/src/test_hexio.c +++ b/src/test_hexio.c @@ -190,7 +190,7 @@ static int SQLITE_TCLAPI hexio_write( } /* -** USAGE: hexio_get_int HEXDATA +** USAGE: hexio_get_int [-littleendian] HEXDATA ** ** Interpret the HEXDATA argument as a big-endian integer. Return ** the value of that integer. HEXDATA can contain between 2 and 8 @@ -207,12 +207,20 @@ static int SQLITE_TCLAPI hexio_get_int( const unsigned char *zIn; unsigned char *aOut; unsigned char aNum[4]; + int bLittle = 0; - if( objc!=2 ){ - Tcl_WrongNumArgs(interp, 1, objv, "HEXDATA"); + if( objc==3 ){ + int n; + char *z = Tcl_GetStringFromObj(objv[1], &n); + if( n>=2 && n<=13 && memcmp(z, "-littleendian", n)==0 ){ + bLittle = 1; + } + } + if( (objc-bLittle)!=2 ){ + Tcl_WrongNumArgs(interp, 1, objv, "[-littleendian] HEXDATA"); return TCL_ERROR; } - zIn = (const unsigned char *)Tcl_GetStringFromObj(objv[1], &nIn); + zIn = (const unsigned char *)Tcl_GetStringFromObj(objv[1+bLittle], &nIn); aOut = sqlite3_malloc( nIn/2 ); if( aOut==0 ){ return TCL_ERROR; @@ -225,7 +233,11 @@ static int SQLITE_TCLAPI hexio_get_int( memcpy(&aNum[4-nOut], aOut, nOut); } sqlite3_free(aOut); - val = (aNum[0]<<24) | (aNum[1]<<16) | (aNum[2]<<8) | aNum[3]; + if( bLittle ){ + val = (aNum[3]<<24) | (aNum[2]<<16) | (aNum[1]<<8) | aNum[0]; + }else{ + val = (aNum[0]<<24) | (aNum[1]<<16) | (aNum[2]<<8) | aNum[3]; + } Tcl_SetObjResult(interp, Tcl_NewIntObj(val)); return TCL_OK; } diff --git a/src/wal.c b/src/wal.c index 4ce5a67585..6200d59c9a 100644 --- a/src/wal.c +++ b/src/wal.c @@ -3109,7 +3109,7 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int cnt){ rc = walLockShared(pWal, WAL_READ_LOCK(eLock)); if( rc!=SQLITE_OK ){ - return rc; + return (rc==SQLITE_BUSY ? WAL_RETRY : rc); } walShmBarrier(pWal); if( memcmp((void *)walIndexHdr(pWal), &pWal->hdr, sizeof(WalIndexHdr)) ){ diff --git a/test/permutations.test b/test/permutations.test index 75f6ecbc02..4530baf88b 100644 --- a/test/permutations.test +++ b/test/permutations.test @@ -439,7 +439,7 @@ walfault.test walhook.test walmode.test walnoshm.test waloverwrite.test walpersist.test walprotocol2.test walprotocol.test walro2.test walrofault.test walro.test walshared.test walslow.test wal.test -wal2savepoint.test +wal2savepoint.test wal2lock.test wal2recover2.test } test_suite "coverage-pager" -description { diff --git a/test/wal2lock.test b/test/wal2lock.test new file mode 100644 index 0000000000..f86cf87925 --- /dev/null +++ b/test/wal2lock.test @@ -0,0 +1,106 @@ +# 2018 December 15 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL2" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl + +set testprefix wal2lock +ifcapable !wal {finish_test ; return } + +db close +testvfs tvfs +sqlite3 db test.db -vfs tvfs + +do_execsql_test 1.0 { + PRAGMA journal_mode = wal2; + CREATE TABLE y1(y, yy); + CREATE INDEX y1y ON y1(y); + CREATE INDEX y1yy ON y1(yy); + INSERT INTO y1 VALUES(1, 2), (3, 4), (5, 6); +} {wal2} + +tvfs script vfs_callback +tvfs filter xShmLock + +set ::lock [list] +proc vfs_callback {func file name lock} { + lappend ::lock $lock + return SQLITE_OK +} + +do_execsql_test 1.1.1 { + SELECT * FROM y1 +} {1 2 3 4 5 6} +do_test 1.1.2 { + set ::lock +} {{4 1 lock shared} {4 1 unlock shared}} + +set ::bFirst 1 +proc vfs_callback {func file name lock} { + if {$::bFirst} { + set ::bFirst 0 + return SQLITE_BUSY + } + return SQLITE_OK +} +do_execsql_test 1.2 { + SELECT * FROM y1 +} {1 2 3 4 5 6} + +set ::bFirst 1 +proc vfs_callback {func file name lock} { + if {$::bFirst} { + set ::bFirst 0 + return SQLITE_IOERR + } + return SQLITE_OK +} +do_catchsql_test 1.3 { + SELECT * FROM y1 +} {1 {disk I/O error}} + +puts "# Warning: This next test case causes SQLite to call xSleep(1) 100 times." +puts "# Normally this equates to a delay of roughly 10 seconds, but if SQLite" +puts "# is built on unix without HAVE_USLEEP defined, it may be much longer." +proc vfs_callback {func file name lock} { return SQLITE_BUSY } +do_catchsql_test 1.4 { + SELECT * FROM y1 +} {1 {locking protocol}} +proc vfs_callback {func file name lock} { return SQLITE_OK } + +sqlite3 db2 test.db -vfs tvfs +set ::bFirst 1 + +proc vfs_callback {func file name lock} { + if {$::bFirst} { + set ::bFirst 0 + db2 eval { INSERT INTO y1 VALUES(7, 8) } + } +} + +do_execsql_test 1.5.1 { + SELECT * FROM y1 +} {1 2 3 4 5 6 7 8} +do_execsql_test 1.5.2 { + SELECT * FROM y1 +} {1 2 3 4 5 6 7 8} + +db close +db2 close +tvfs delete +finish_test diff --git a/test/wal2recover.test b/test/wal2recover.test index e8cecc1bf7..8fd08b8905 100644 --- a/test/wal2recover.test +++ b/test/wal2recover.test @@ -227,40 +227,6 @@ do_test 3.5 { list [file size test.db] [file size test.db-wal] [file size test.db-wal2] } {5120 15752 18896} -#------------------------------------------------------------------------- -# -reset_db -do_execsql_test 4.0 { - PRAGMA journal_mode = wal2; - CREATE TABLE xyz(x, y, z); - INSERT INTO xyz VALUES('x', 'y', 'z'); -} {wal2} -db close -do_test 4.1 { - close [open test.db-wal w] - file mkdir test.db-wal2 - sqlite3 db test.db - catchsql { SELECT * FROM xyz } -} {1 {unable to open database file}} -db close -file delete test.db-wal2 - -do_test 4.2 { - sqlite3 db test.db - execsql { - INSERT INTO xyz VALUES('a', 'b', 'c'); - } - forcecopy test.db test.db2 - forcecopy test.db-wal test.db2-wal - forcedelete test.db2-wal2 - file mkdir test.db2-wal2 - sqlite3 db2 test.db2 - catchsql { SELECT * FROM xyz } db2 -} {1 {unable to open database file}} -db2 close -file delete test.db2-wal2 - - finish_test diff --git a/test/wal2recover2.test b/test/wal2recover2.test new file mode 100644 index 0000000000..11804c34a3 --- /dev/null +++ b/test/wal2recover2.test @@ -0,0 +1,227 @@ +# 2018 December 13 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL2" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl + +set testprefix wal2recover2 +ifcapable !wal {finish_test ; return } + +do_execsql_test 1.0 { + CREATE TABLE t1(x); + CREATE TABLE t2(x); + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1500 ) + INSERT INTO t1 SELECT i FROM s; + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1500 ) + INSERT INTO t2 SELECT i FROM s; + + PRAGMA journal_mode = wal2; + PRAGMA journal_size_limit = 10000; +} {wal2 10000} + +set ::L 1125750 +set ::M 1126500 +set ::H 1127250 + +do_execsql_test 1.1 { + UPDATE t1 SET x=x+1; + UPDATE t2 SET x=x+1 WHERE rowid<=750; + + SELECT sum(x) FROM t1; + SELECT sum(x) FROM t2; +} [list $H $M] + +do_test 1.2 { + list [file size test.db] [file size test.db-wal] [file size test.db-wal2] +} {31744 14704 7368} + +proc cksum {zIn data} { + if {[string length $zIn]==0} { + set s0 0 + set s1 0 + } else { + set s0 [hexio_get_int [string range $zIn 0 7]] + set s1 [hexio_get_int [string range $zIn 8 15]] + } + set n [expr [string length $data] / 8] + + for {set i 0} {$i < $n} {incr i 2} { + set x0 [hexio_get_int -l [string range $data [expr $i*8] [expr $i*8+7]]] + set x1 [hexio_get_int -l [string range $data [expr $i*8+8] [expr $i*8+8+7]]] + + set s0 [expr ($s0 + $x0 + $s1) & 0xFFFFFFFF] + set s1 [expr ($s1 + $x1 + $s0) & 0xFFFFFFFF] + } + + return "[hexio_render_int32 $s0][hexio_render_int32 $s1]" +} + +proc fix_wal_cksums {file} { + # Fix the checksum on the wal header. + set data [hexio_read $file 0 32] + set cksum [cksum {} [string range $data 0 47]] + set salt [hexio_read $file 16 8] + hexio_write $file 24 $cksum + + # Fix the checksums for all pages in the wal file. + set pgsz [hexio_get_int [hexio_read $file 8 4]] + set sz [file size $file] + for {set off 32} {$off < $sz} {incr off [expr $pgsz+24]} { + set e [hexio_read $file $off 8] + set cksum [cksum $cksum $e] + + set p [hexio_read $file [expr $off+24] $pgsz] + set cksum [cksum $cksum $p] + + hexio_write $file [expr $off+8] $salt + hexio_write $file [expr $off+16] $cksum + } +} + +proc wal_incr_hdrfield {file field} { + switch -- $field { + nCkpt { set offset 12 } + salt0 { set offset 16 } + salt1 { set offset 20 } + default { + error "unknown field $field - should be \"nCkpt\", \"salt0\" or \"salt1\"" + } + } + + # Increment the value in the wal header. + set v [hexio_get_int [hexio_read $file $offset 4]] + incr v + hexio_write $file $offset [hexio_render_int32 $v] + + # Fix various checksums + fix_wal_cksums $file +} + +proc wal_set_nckpt {file val} { + # Increment the value in the wal header. + hexio_write $file 12 [hexio_render_int32 $val] + + # Fix various checksums + fix_wal_cksums $file +} + +proc wal_set_follow {file prevfile} { + set pgsz [hexio_get_int [hexio_read $prevfile 8 4]] + set sz [file size $prevfile] + set cksum [hexio_read $prevfile [expr $sz-$pgsz-8] 8] + + hexio_write $file 16 $cksum + fix_wal_cksums $file +} + +foreach {tn file field} { + 1 test.db2-wal salt0 + 2 test.db2-wal salt1 + 3 test.db2-wal nCkpt + 4 test.db2-wal2 salt0 + 5 test.db2-wal2 salt1 + 6 test.db2-wal2 nCkpt +} { + do_test 1.3.$tn { + forcecopy test.db test.db2 + forcecopy test.db-wal test.db2-wal + forcecopy test.db-wal2 test.db2-wal2 + wal_incr_hdrfield $file $field + sqlite3 db2 test.db2 + execsql { + SELECT sum(x) FROM t1; + SELECT sum(x) FROM t2; + } db2 + } [list $H $L] + db2 close +} + +do_test 1.4 { + forcecopy test.db test.db2 + forcecopy test.db-wal2 test.db2-wal + forcedelete test.db2-wal2 + sqlite3 db2 test.db2 + execsql { + SELECT sum(x) FROM t1; + SELECT sum(x) FROM t2; + } db2 +} [list $L $M] + +do_test 1.5 { + forcecopy test.db test.db2 + forcecopy test.db-wal2 test.db2-wal + forcecopy test.db-wal test.db2-wal2 + sqlite3 db2 test.db2 + execsql { + SELECT sum(x) FROM t1; + SELECT sum(x) FROM t2; + } db2 +} [list $H $M] + +foreach {tn file field} { + 1 test.db2-wal salt0 + 2 test.db2-wal salt1 + 3 test.db2-wal2 salt0 + 4 test.db2-wal2 salt1 +} { + do_test 1.6.$tn { + forcecopy test.db test.db2 + forcecopy test.db-wal2 test.db2-wal + forcecopy test.db-wal test.db2-wal2 + wal_incr_hdrfield $file $field + breakpoint + sqlite3 db2 test.db2 + execsql { + SELECT sum(x) FROM t1; + SELECT sum(x) FROM t2; + } db2 + } [list $H $L] + db2 close +} + +foreach {tn nCkpt1 nCkpt2 res} [list \ + 1 2 1 "$H $M" \ + 2 2 2 "$L $M" \ + 3 3 1 "$H $L" \ + 4 15 14 "$H $M" \ + 5 0 15 "$H $M" \ + 6 1 15 "$L $M" \ +] { + do_test 1.7.$tn { + forcecopy test.db test.db2 + forcecopy test.db-wal2 test.db2-wal + forcecopy test.db-wal test.db2-wal2 + + wal_set_nckpt test.db2-wal2 $nCkpt2 + wal_set_nckpt test.db2-wal $nCkpt1 + wal_set_follow test.db2-wal test.db2-wal2 + + if {$tn==1} breakpoint + + sqlite3 db2 test.db2 + execsql { + SELECT sum(x) FROM t1; + SELECT sum(x) FROM t2; + } db2 + } $res + db2 close +} + + +finish_test + From c5140b39ceaeba325c38bcd8ab70816cef78de5b Mon Sep 17 00:00:00 2001 From: dan Date: Mon, 17 Dec 2018 15:22:24 +0000 Subject: [PATCH 20/51] Add extra test case to wal2recover.test. FossilOrigin-Name: 1a5aa73435b11fff0567672dc921d34aad57adf0381ad430e1389f39a6f025b7 --- manifest | 12 ++++++------ manifest.uuid | 2 +- test/wal2recover.test | 33 +++++++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 7 deletions(-) diff --git a/manifest b/manifest index 3d69ddd17c..6011543e8e 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Further\stest\scases\sfor\swal2\smode. -D 2018-12-15T20:20:13.338 +C Add\sextra\stest\scase\sto\swal2recover.test. +D 2018-12-17T15:22:24.652 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in 68d0ba0f0b533d5bc84c78c13a6ce84ee81183a67014caa47a969e67f028fa1c @@ -1598,7 +1598,7 @@ F test/wal.test 613efec03e517e1775d86b993a54877d2e29a477 F test/wal2.test 155b9efa999bdb38ce1cd729b9a4fcdbffd6b88be27f039bad1d2929d287d918 F test/wal2big.test 0b4ec526f9ca4bbabc355042c38045ae2e253fb46eb327bb7693d0122bc6968b F test/wal2lock.test 0ef98d72dc6bcf7711dedd684760488400d9a9a6eec0dc5d3822060437793552 -F test/wal2recover.test da390d40c955a74b8a049d41248f92f928a5a5ee6bbcca33cb665c95b736c11c +F test/wal2recover.test ba8f4bc9397c838734619f9e759bd98b00e355347b3cf80a2e677610d231d5d8 F test/wal2recover2.test 98749381c2e61574e181a2e288295a3bf93d12f57769db50ecea7fc211d9ad5c F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal2savepoint.test 29725383e7eb8b78fed46333d69dc45b4ed321db43ecc81766aadea991874bef @@ -1792,7 +1792,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 54e628f902a5508badc8941ceda1bec12fe8f58969c91c670a37888ef2681880 -R e2c9e8287795c59cafc4b6dc4a5ee2a2 +P 9cb5f8dab685f5ea36ad142cfa588dee82e87f3a89e8dcf84e0ee124bb29bc7f +R 126e20a307613344c2c21be5333f4cc9 U dan -Z 964094b4a0f5d261b4d47067580a5ce8 +Z 0b7e3c508818c5bb27b7cd0635e26287 diff --git a/manifest.uuid b/manifest.uuid index c6c9f8cdaa..61b581122e 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -9cb5f8dab685f5ea36ad142cfa588dee82e87f3a89e8dcf84e0ee124bb29bc7f \ No newline at end of file +1a5aa73435b11fff0567672dc921d34aad57adf0381ad430e1389f39a6f025b7 \ No newline at end of file diff --git a/test/wal2recover.test b/test/wal2recover.test index 8fd08b8905..23e3cebda2 100644 --- a/test/wal2recover.test +++ b/test/wal2recover.test @@ -227,6 +227,39 @@ do_test 3.5 { list [file size test.db] [file size test.db-wal] [file size test.db-wal2] } {5120 15752 18896} +#------------------------------------------------------------------------- +# +reset_db +do_execsql_test 4.0 { + PRAGMA journal_mode = wal2; + CREATE TABLE xyz(x, y, z); + INSERT INTO xyz VALUES('x', 'y', 'z'); +} {wal2} +db close +do_test 4.1 { + close [open test.db-wal w] + file mkdir test.db-wal2 + sqlite3 db test.db + catchsql { SELECT * FROM xyz } +} {1 {unable to open database file}} +db close +file delete test.db-wal2 + +do_test 4.2 { + sqlite3 db test.db + execsql { + INSERT INTO xyz VALUES('a', 'b', 'c'); + } + forcecopy test.db test.db2 + forcecopy test.db-wal test.db2-wal + forcedelete test.db2-wal2 + file mkdir test.db2-wal2 + sqlite3 db2 test.db2 + catchsql { SELECT * FROM xyz } db2 +} {1 {unable to open database file}} +db2 close +file delete test.db2-wal2 + finish_test From 665f3d209b29f9d57679938c971b99a3bae5cdf0 Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 22 Dec 2018 15:50:00 +0000 Subject: [PATCH 21/51] Enhance the wal2 header comment in wal.c to explain how the wal-hook is invoked in wal2 mode. FossilOrigin-Name: 118aa7e32a94ad971a955ab60db5bfc5b6241f93734a41ba83ab72ea71bc9aaa --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/wal.c | 25 +++++++++++++++++++++++++ 3 files changed, 32 insertions(+), 7 deletions(-) diff --git a/manifest b/manifest index 6011543e8e..4a2e015de6 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Add\sextra\stest\scase\sto\swal2recover.test. -D 2018-12-17T15:22:24.652 +C Enhance\sthe\swal2\sheader\scomment\sin\swal.c\sto\sexplain\show\sthe\swal-hook\sis\ninvoked\sin\swal2\smode. +D 2018-12-22T15:50:00.221 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in 68d0ba0f0b533d5bc84c78c13a6ce84ee81183a67014caa47a969e67f028fa1c @@ -590,7 +590,7 @@ F src/vdbesort.c 90aad5a92608f2dd771c96749beabdb562c9d881131a860a7a5bccf66dc3be7 F src/vdbetrace.c 79d6dbbc479267b255a7de8080eee6e729928a0ef93ed9b0bfa5618875b48392 F src/vtab.c 70188a745dc4e57d26e942681ff4b2912b7c8249ad5de3f60f0677b4337bcfaa F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c beca1487f7cecd1d6a965ba899a5a6b8868b3eb80580d33b2a73f8e37ab4901b +F src/wal.c 93dc58d21bc6e1d08edfa42ba03e07f9652cd4c17c4205c20528e84e3b26c892 F src/wal.h d2a69695c84137f76e19a247a342cb02ab0131001b6f58153d94b71195bbd84d F src/walker.c fb94aadc9099ff9c6506d0a8b88d51266005bcaa265403f3d7caf732a562eb66 F src/where.c 3818e8a736a05d2cb194e64399af707e367fbcc5c251d785804d02eaf121288e @@ -1792,7 +1792,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 9cb5f8dab685f5ea36ad142cfa588dee82e87f3a89e8dcf84e0ee124bb29bc7f -R 126e20a307613344c2c21be5333f4cc9 +P 1a5aa73435b11fff0567672dc921d34aad57adf0381ad430e1389f39a6f025b7 +R ae7a76a21801add0bf32d05f9dbcc082 U dan -Z 0b7e3c508818c5bb27b7cd0635e26287 +Z e66e7dac085eccd9e1ba0c66084130c4 diff --git a/manifest.uuid b/manifest.uuid index 61b581122e..d16063f1f2 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -1a5aa73435b11fff0567672dc921d34aad57adf0381ad430e1389f39a6f025b7 \ No newline at end of file +118aa7e32a94ad971a955ab60db5bfc5b6241f93734a41ba83ab72ea71bc9aaa \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 6200d59c9a..3e9b32525b 100644 --- a/src/wal.c +++ b/src/wal.c @@ -306,6 +306,31 @@ ** The first wal file takes the same name as the wal file in legacy wal ** mode systems - "-wal". The second is named "-wal2". ** +** CHECKPOINTS +** +** The "pre-configured size" mentioned above is the value set by +** "PRAGMA journal_size_limit". Or, if journal_size_limit is not set, +** 1000 pages. +** +** There is only a single type of checkpoint in wal2 mode (no "truncate", +** "restart" etc.), and it always checkpoints the entire contents of a single +** wal file. A wal file cannot be checkpointed until after a writer has written +** the first transaction into the other wal file and all readers are reading a +** snapshot that includes at least one transaction from the other wal file. +** +** The wal-hook, if one is registered, is invoked after a write-transaction +** is committed, just as it is in legacy wal mode. The integer parameter +** passed to the wal-hook is the total number of uncheckpointed frames in both +** wal files. Except, the parameter is set to zero if there is no frames +** that may be checkpointed. This happens in two scenarios: +** +** 1. The "other" wal file (the one that the writer did not just append to) +** is completely empty, or +** +** 2. The "other" wal file (the one that the writer did not just append to) +** has already been checkpointed. +** +** ** WAL FILE FORMAT ** ** The file format used for each wal file in wal2 mode is the same as for From 0f297b6e508eab1d3bf6b3b75956a87dbbf6e227 Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 26 Dec 2018 20:42:32 +0000 Subject: [PATCH 22/51] Improve testing of the wal.c changes on this branch. FossilOrigin-Name: 63483e22c775183e01924b5cb3d0f3655b63c3cdd06faf26cacd4d0913c0055c --- manifest | 17 +++++++------- manifest.uuid | 2 +- src/wal.c | 9 ++++--- test/permutations.test | 1 + test/wal2fault.test | 52 +++++++++++++++++++++++++++++++++++++++++ test/wal2savepoint.test | 13 +++++++++++ 6 files changed, 80 insertions(+), 14 deletions(-) create mode 100644 test/wal2fault.test diff --git a/manifest b/manifest index 3a78a08258..310e343517 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\slatest\strunk\schanges\swith\sthis\sbranch. -D 2018-12-26T19:10:59.224 +C Improve\stesting\sof\sthe\swal.c\schanges\son\sthis\sbranch. +D 2018-12-26T20:42:32.396 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in d8b254f8bb81bab43c340d70d17dc3babab40fcc8a348c8255881f780a45fee6 @@ -593,7 +593,7 @@ F src/vdbesort.c 90aad5a92608f2dd771c96749beabdb562c9d881131a860a7a5bccf66dc3be7 F src/vdbetrace.c 79d6dbbc479267b255a7de8080eee6e729928a0ef93ed9b0bfa5618875b48392 F src/vtab.c 70188a745dc4e57d26e942681ff4b2912b7c8249ad5de3f60f0677b4337bcfaa F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 328101a4c383b0a8564b03aa9c3f79f9b3b30c1959e4a46f49dd0e62a0a61088 +F src/wal.c 9b0402f6e126d13dc324588d0e033c274264dae6972a3d4de0fb0813fa91a840 F src/wal.h d2a69695c84137f76e19a247a342cb02ab0131001b6f58153d94b71195bbd84d F src/walker.c fb94aadc9099ff9c6506d0a8b88d51266005bcaa265403f3d7caf732a562eb66 F src/where.c 3818e8a736a05d2cb194e64399af707e367fbcc5c251d785804d02eaf121288e @@ -1188,7 +1188,7 @@ F test/parser1.test 6ccdf5e459a5dc4673d3273dc311a7e9742ca952dd0551a6a6320d27035c F test/pcache.test c8acbedd3b6fd0f9a7ca887a83b11d24a007972b F test/pcache2.test af7f3deb1a819f77a6d0d81534e97d1cf62cd442 F test/percentile.test 4243af26b8f3f4555abe166f723715a1f74c77ff -F test/permutations.test 928386eb05969cc0effc7be7cfe86ea44ee3fee05a1f9ea75387732475a3fadf +F test/permutations.test 027d8460ba7a2e7b1332388c81fc896f713e1bba57c7157a8278b6bdfd321111 F test/pg_common.tcl 301ac19c1a52fd55166d26db929b3b89165c634d52b5f8ad76ea8cb06960db30 F test/pragma.test c267bf02742c823a191960895b3d52933cebd7beee26757d1ed694f213fcd867 F test/pragma2.test e5d5c176360c321344249354c0c16aec46214c9f @@ -1604,11 +1604,12 @@ F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad F test/wal.test cdf0ca6cc0447520d19ef1c83287824ebeb3e82d75af856511ba96841a79fc9b F test/wal2.test 155b9efa999bdb38ce1cd729b9a4fcdbffd6b88be27f039bad1d2929d287d918 F test/wal2big.test 0b4ec526f9ca4bbabc355042c38045ae2e253fb46eb327bb7693d0122bc6968b +F test/wal2fault.test 2e8e60cacd5bcd451618aeffd05f676894d17202d3e2986e288d36e2c5993249 F test/wal2lock.test 0ef98d72dc6bcf7711dedd684760488400d9a9a6eec0dc5d3822060437793552 F test/wal2recover.test ba8f4bc9397c838734619f9e759bd98b00e355347b3cf80a2e677610d231d5d8 F test/wal2recover2.test 98749381c2e61574e181a2e288295a3bf93d12f57769db50ecea7fc211d9ad5c F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c -F test/wal2savepoint.test 29725383e7eb8b78fed46333d69dc45b4ed321db43ecc81766aadea991874bef +F test/wal2savepoint.test 2c82bd6a6ee5066c156040d2e9c2415646fcf96116ae7ad127eaf0c0b4a85f22 F test/wal2simple.test 96206c98bf64ab20ec00a1c0f6c709e258b98b39f2149889361f31966ce5a703 F test/wal2snapshot.test 95a919e1c73dee0e0212d10931d03cc1116f68a0ff603163e551aaa5ac7025c1 F test/wal3.test 2a93004bc0fb2b5c29888964024695bade278ab2 @@ -1803,7 +1804,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 118aa7e32a94ad971a955ab60db5bfc5b6241f93734a41ba83ab72ea71bc9aaa 6821c61f1d71be2be7f867e59fd94582a1eaf45a4d1fb88be995807a77cc22ea -R 9446c2d66d137856eca38f10c8a3c210 +P 404f9d99d325ddf82fde6d957860d30b2fb67f232c0878a3cf6df9596541f16e +R 26605d7d9d3118326625f742a3f1a756 U dan -Z 215adea076b7047172a8d8ead4404cfa +Z b2f5d8c03c1979ab0271df68769d4d7b diff --git a/manifest.uuid b/manifest.uuid index b3e91ceedc..457fa69d0a 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -404f9d99d325ddf82fde6d957860d30b2fb67f232c0878a3cf6df9596541f16e \ No newline at end of file +63483e22c775183e01924b5cb3d0f3655b63c3cdd06faf26cacd4d0913c0055c \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 44a4671a4d..678c6bb95b 100644 --- a/src/wal.c +++ b/src/wal.c @@ -576,7 +576,7 @@ struct WalIndexHdr { ** void walidxSetFile(WalIndexHdr*, int val); // set file */ #define walidxGetMxFrame(pHdr, iWal) \ - ((iWal) ? ((pHdr)->mxFrame2 & 0x7FFFFFF) : (pHdr)->mxFrame) + ((iWal) ? ((pHdr)->mxFrame2 & 0x7FFFFFFF) : (pHdr)->mxFrame) static void walidxSetMxFrame(WalIndexHdr *pHdr, int iWal, u32 mxFrame){ if( iWal ){ @@ -4427,10 +4427,9 @@ int sqlite3WalCheckpoint( /* Copy data from the log to the database file. */ if( rc==SQLITE_OK ){ + int iCkpt = walidxGetFile(&pWal->hdr); - if( (walPagesize(pWal)!=nBuf) - && (walidxGetMxFrame(&pWal->hdr, 0) || walidxGetMxFrame(&pWal->hdr, 1)) - ){ + if( (walPagesize(pWal)!=nBuf) && walidxGetMxFrame(&pWal->hdr, iCkpt) ){ rc = SQLITE_CORRUPT_BKPT; }else{ rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf); @@ -4444,7 +4443,7 @@ int sqlite3WalCheckpoint( if( pnCkpt ){ if( isWalMode2(pWal) ){ if( (int)(walCkptInfo(pWal)->nBackfill) ){ - *pnCkpt = walidxGetMxFrame(&pWal->hdr, !walidxGetFile(&pWal->hdr)); + *pnCkpt = walidxGetMxFrame(&pWal->hdr, iCkpt); }else{ *pnCkpt = 0; } diff --git a/test/permutations.test b/test/permutations.test index f75bad6c7b..5dc203d80c 100644 --- a/test/permutations.test +++ b/test/permutations.test @@ -444,6 +444,7 @@ test_suite "coverage-wal" -description { snapshot2.test snapshot3.test snapshot4.test snapshot_fault.test snapshot.test snapshot_up.test walcrash2.test walcrash3.test walcrash4.test walcrash.test + wal2fault.test } test_suite "coverage-pager" -description { diff --git a/test/wal2fault.test b/test/wal2fault.test new file mode 100644 index 0000000000..7067e45529 --- /dev/null +++ b/test/wal2fault.test @@ -0,0 +1,52 @@ +# 2010 May 03 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/malloc_common.tcl +source $testdir/lock_common.tcl + +ifcapable !wal {finish_test ; return } +set testprefix wal2fault + +do_execsql_test 1.0 { + CREATE TABLE t1(x,y); + PRAGMA journal_mode = wal2; + WITH s(i) AS ( SELECT 100 UNION ALL SELECT i-1 FROM s WHERE (i-1)>0 ) + INSERT INTO t1 SELECT i, randomblob(i) FROM s; + WITH s(i) AS ( SELECT 100 UNION ALL SELECT i-1 FROM s WHERE (i-1)>0 ) + INSERT INTO t1 SELECT i, randomblob(i) FROM s; +} {wal2} + +do_test 1.1 { + expr [file size test.db-wal]>10000 +} {1} +faultsim_save_and_close + +do_faultsim_test 1 -prep { + faultsim_restore_and_reopen + execsql { + PRAGMA journal_size_limit = 10000; + SELECT count(*) FROM sqlite_master; + } +} -body { + execsql { + INSERT INTO t1 VALUES(1, 2); + } +} -test { + faultsim_test_result {0 {}} +} + +finish_test diff --git a/test/wal2savepoint.test b/test/wal2savepoint.test index c875af88c4..f46ff92280 100644 --- a/test/wal2savepoint.test +++ b/test/wal2savepoint.test @@ -55,6 +55,19 @@ do_execsql_test 1.3 { PRAGMA integrity_check; } {210 ok} +do_execsql_test 1.4 { + BEGIN; + SAVEPOINT abc; + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s where i < 100) + INSERT INTO t1 SELECT random(), random(), random() FROM s; + ROLLBACK TO abc; + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s where i < 10) + INSERT INTO t1 SELECT random(), random(), random() FROM s; + COMMIT; + SELECT count(*) FROM t1; + PRAGMA integrity_check; +} {220 ok} + finish_test From ce909d1aae0d861e0597f3ed486e6f318de726d2 Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 27 Dec 2018 16:49:33 +0000 Subject: [PATCH 23/51] Increase test coverage of wal.c provided by permutation "coverage-wal" on this branch. FossilOrigin-Name: 2f7f893a702728745445f6ef5f914c1157058a8fbdfd1a58cfb8906e5566729d --- manifest | 18 +++++++++--------- manifest.uuid | 2 +- src/wal.c | 7 +++++-- test/wal.test | 12 ++++++------ test/wal2recover2.test | 43 ++++++++++++++++++++++++++++++++++++++++-- test/wal2snapshot.test | 11 ++++++++++- 6 files changed, 72 insertions(+), 21 deletions(-) diff --git a/manifest b/manifest index 310e343517..80a2f721d2 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Improve\stesting\sof\sthe\swal.c\schanges\son\sthis\sbranch. -D 2018-12-26T20:42:32.396 +C Increase\stest\scoverage\sof\swal.c\sprovided\sby\spermutation\s"coverage-wal"\son\sthis\nbranch. +D 2018-12-27T16:49:33.954 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in d8b254f8bb81bab43c340d70d17dc3babab40fcc8a348c8255881f780a45fee6 @@ -593,7 +593,7 @@ F src/vdbesort.c 90aad5a92608f2dd771c96749beabdb562c9d881131a860a7a5bccf66dc3be7 F src/vdbetrace.c 79d6dbbc479267b255a7de8080eee6e729928a0ef93ed9b0bfa5618875b48392 F src/vtab.c 70188a745dc4e57d26e942681ff4b2912b7c8249ad5de3f60f0677b4337bcfaa F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 9b0402f6e126d13dc324588d0e033c274264dae6972a3d4de0fb0813fa91a840 +F src/wal.c 8bfa18a1c5e04a21a8cb142cdb7f8236ad32300a2298565b6fe1cdd15beee3a6 F src/wal.h d2a69695c84137f76e19a247a342cb02ab0131001b6f58153d94b71195bbd84d F src/walker.c fb94aadc9099ff9c6506d0a8b88d51266005bcaa265403f3d7caf732a562eb66 F src/where.c 3818e8a736a05d2cb194e64399af707e367fbcc5c251d785804d02eaf121288e @@ -1601,17 +1601,17 @@ F test/vtabJ.test d7b73675708cf63cfcb9d443bb451fc01a028347275b7311e51f9fdf3ca675 F test/vtab_alter.test 736e66fb5ec7b4fee58229aa3ada2f27ec58bc58c00edae4836890c3784c6783 F test/vtab_err.test dcc8b7b9cb67522b3fe7a272c73856829dae4ab7fdb30399aea1b6981bda2b65 F test/vtab_shared.test 5253bff2355a9a3f014c15337da7e177ab0ef8ad -F test/wal.test cdf0ca6cc0447520d19ef1c83287824ebeb3e82d75af856511ba96841a79fc9b +F test/wal.test a233cd4733928a5aaef24738727c4094d561ea68089f93cccd1f6e9dec610212 F test/wal2.test 155b9efa999bdb38ce1cd729b9a4fcdbffd6b88be27f039bad1d2929d287d918 F test/wal2big.test 0b4ec526f9ca4bbabc355042c38045ae2e253fb46eb327bb7693d0122bc6968b F test/wal2fault.test 2e8e60cacd5bcd451618aeffd05f676894d17202d3e2986e288d36e2c5993249 F test/wal2lock.test 0ef98d72dc6bcf7711dedd684760488400d9a9a6eec0dc5d3822060437793552 F test/wal2recover.test ba8f4bc9397c838734619f9e759bd98b00e355347b3cf80a2e677610d231d5d8 -F test/wal2recover2.test 98749381c2e61574e181a2e288295a3bf93d12f57769db50ecea7fc211d9ad5c +F test/wal2recover2.test d473784bc72ee70438d1aa1cbe449fb322d7e52e8eaa256135fb3182c66b1c78 F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal2savepoint.test 2c82bd6a6ee5066c156040d2e9c2415646fcf96116ae7ad127eaf0c0b4a85f22 F test/wal2simple.test 96206c98bf64ab20ec00a1c0f6c709e258b98b39f2149889361f31966ce5a703 -F test/wal2snapshot.test 95a919e1c73dee0e0212d10931d03cc1116f68a0ff603163e551aaa5ac7025c1 +F test/wal2snapshot.test 7a5f4629a3c43a43c3440b8b2ea9f07de91a46b0b9eea5f08f62b5bf5b6468df F test/wal3.test 2a93004bc0fb2b5c29888964024695bade278ab2 F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c F test/wal5.test 9c11da7aeccd83a46d79a556ad11a18d3cb15aa9 @@ -1804,7 +1804,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 404f9d99d325ddf82fde6d957860d30b2fb67f232c0878a3cf6df9596541f16e -R 26605d7d9d3118326625f742a3f1a756 +P 63483e22c775183e01924b5cb3d0f3655b63c3cdd06faf26cacd4d0913c0055c +R 44506fb1f03e87f3f3981d2947902fad U dan -Z b2f5d8c03c1979ab0271df68769d4d7b +Z 537fbe586afdb7bdeed308e7bd56f47e diff --git a/manifest.uuid b/manifest.uuid index 457fa69d0a..f8fc8d6c56 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -63483e22c775183e01924b5cb3d0f3655b63c3cdd06faf26cacd4d0913c0055c \ No newline at end of file +2f7f893a702728745445f6ef5f914c1157058a8fbdfd1a58cfb8906e5566729d \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 678c6bb95b..b90ba4df1a 100644 --- a/src/wal.c +++ b/src/wal.c @@ -3885,7 +3885,8 @@ static int walRestartLog(Wal *pWal){ if( walidxGetMxFrame(&pWal->hdr, iApp)>=nWalSize ){ volatile WalCkptInfo *pInfo = walCkptInfo(pWal); - if( walidxGetMxFrame(&pWal->hdr, !iApp)==0 || pInfo->nBackfill ){ + u32 mxFrame = walidxGetMxFrame(&pWal->hdr, !iApp); + if( mxFrame==0 || pInfo->nBackfill ){ rc = wal2RestartOk(pWal, iApp); if( rc==SQLITE_OK ){ int iNew = !iApp; @@ -4429,7 +4430,9 @@ int sqlite3WalCheckpoint( if( rc==SQLITE_OK ){ int iCkpt = walidxGetFile(&pWal->hdr); - if( (walPagesize(pWal)!=nBuf) && walidxGetMxFrame(&pWal->hdr, iCkpt) ){ + if( (walPagesize(pWal)!=nBuf) + && ((pWal->hdr.mxFrame2 & 0x7FFFFFFF) || pWal->hdr.mxFrame) + ){ rc = SQLITE_CORRUPT_BKPT; }else{ rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags, zBuf); diff --git a/test/wal.test b/test/wal.test index a003b6ad20..f545b580a3 100644 --- a/test/wal.test +++ b/test/wal.test @@ -1174,7 +1174,7 @@ foreach {tn pgsz works} { 9 32768 1 10 65536 1 11 131072 0 - 11 1016 0 + 12 1016 0 } { if {$::SQLITE_MAX_PAGE_SIZE < $pgsz} { @@ -1184,14 +1184,14 @@ foreach {tn pgsz works} { for {set pg 1} {$pg <= 3} {incr pg} { forcecopy testX.db test.db forcedelete test.db-wal - + # Check that the database now exists and consists of three pages. And # that there is no associated wal file. # do_test wal-18.2.$tn.$pg.1 { file exists test.db-wal } 0 do_test wal-18.2.$tn.$pg.2 { file exists test.db } 1 do_test wal-18.2.$tn.$pg.3 { file size test.db } [expr 1024*3] - + do_test wal-18.2.$tn.$pg.4 { # Create a wal file that contains a single frame (database page @@ -1223,16 +1223,16 @@ foreach {tn pgsz works} { puts -nonewline $fd $framehdr puts -nonewline $fd $framebody close $fd - + file size test.db-wal } [wal_file_size 1 $pgsz] - + do_test wal-18.2.$tn.$pg.5 { sqlite3 db test.db set rc [catch { db one {PRAGMA integrity_check} } msg] expr { $rc!=0 || $msg!="ok" } } $works - + db close } } diff --git a/test/wal2recover2.test b/test/wal2recover2.test index 11804c34a3..3665e78b20 100644 --- a/test/wal2recover2.test +++ b/test/wal2recover2.test @@ -184,7 +184,6 @@ foreach {tn file field} { forcecopy test.db-wal2 test.db2-wal forcecopy test.db-wal test.db2-wal2 wal_incr_hdrfield $file $field - breakpoint sqlite3 db2 test.db2 execsql { SELECT sum(x) FROM t1; @@ -211,7 +210,6 @@ foreach {tn nCkpt1 nCkpt2 res} [list \ wal_set_nckpt test.db2-wal $nCkpt1 wal_set_follow test.db2-wal test.db2-wal2 - if {$tn==1} breakpoint sqlite3 db2 test.db2 execsql { @@ -222,6 +220,47 @@ foreach {tn nCkpt1 nCkpt2 res} [list \ db2 close } +#------------------------------------------------------------------------- +reset_db +do_execsql_test 1.8.1 { + PRAGMA autovacuum = 0; + PRAGMA page_size = 4096; + CREATE TABLE t1(x); + CREATE TABLE t2(x); + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1500 ) + INSERT INTO t1 SELECT i FROM s; + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1500 ) + INSERT INTO t2 SELECT i FROM s; + + PRAGMA journal_mode = wal2; + PRAGMA journal_size_limit = 10000; + + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1500 ) + INSERT INTO t2 SELECT i FROM s; +} {wal2 10000} + +do_test 1.8.2 { + list [file size test.db-wal] [file size test.db-wal2] +} {24752 0} + +do_execsql_test 1.8.3 { PRAGMA user_version = 123 } +do_test 1.8.4 { + list [file size test.db-wal] [file size test.db-wal2] +} {24752 4152} + +do_test 1.8.5 { + hexio_write test.db-wal2 [expr 56+16] 0400 + fix_wal_cksums test.db-wal2 +} {} + +do_test 1.8.6 { + forcecopy test.db test.db2 + forcecopy test.db-wal test.db2-wal + forcecopy test.db-wal2 test.db2-wal2 + sqlite3 db2 test.db2 + catchsql { SELECT * FROM sqlite_master } db2 +} {1 {database disk image is malformed}} +db2 close finish_test diff --git a/test/wal2snapshot.test b/test/wal2snapshot.test index 9f461db7d6..fbc676b3da 100644 --- a/test/wal2snapshot.test +++ b/test/wal2snapshot.test @@ -67,7 +67,7 @@ foreach {tn mode} {1 wal 2 wal2} { execsql COMMIT } {} } else { - do_test 2.6 { + do_test 2.6.1 { execsql BEGIN set res [ list [catch { sqlite3_snapshot_open_blob db main $SNAPSHOT } msg] $msg @@ -75,6 +75,15 @@ foreach {tn mode} {1 wal 2 wal2} { execsql COMMIT set res } {1 SQLITE_ERROR} + do_test 2.6.2 { + execsql BEGIN + execsql {SELECT * FROM sqlite_master} + set res [ + list [catch { sqlite3_snapshot_open_blob db main $SNAPSHOT } msg] $msg + ] + execsql COMMIT + set res + } {1 SQLITE_ERROR} } } From d445ed094a97768bfe3144f797a22c4116b5ecdc Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 28 Dec 2018 16:20:53 +0000 Subject: [PATCH 24/51] Add new test file wal2rollback.test to this branch. FossilOrigin-Name: 85a376fc6c77117b96814df800b3d68a441d69602ffc2eb8cd7dba29f25d69aa --- manifest | 11 ++++---- manifest.uuid | 2 +- test/wal2rollback.test | 62 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 69 insertions(+), 6 deletions(-) create mode 100644 test/wal2rollback.test diff --git a/manifest b/manifest index 80a2f721d2..36037e002a 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Increase\stest\scoverage\sof\swal.c\sprovided\sby\spermutation\s"coverage-wal"\son\sthis\nbranch. -D 2018-12-27T16:49:33.954 +C Add\snew\stest\sfile\swal2rollback.test\sto\sthis\sbranch. +D 2018-12-28T16:20:53.421 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in d8b254f8bb81bab43c340d70d17dc3babab40fcc8a348c8255881f780a45fee6 @@ -1609,6 +1609,7 @@ F test/wal2lock.test 0ef98d72dc6bcf7711dedd684760488400d9a9a6eec0dc5d38220604377 F test/wal2recover.test ba8f4bc9397c838734619f9e759bd98b00e355347b3cf80a2e677610d231d5d8 F test/wal2recover2.test d473784bc72ee70438d1aa1cbe449fb322d7e52e8eaa256135fb3182c66b1c78 F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c +F test/wal2rollback.test 23adc4a099b23f6aaea8b04fdca1c35861d887dd80f8be7da2d5273eb777e428 F test/wal2savepoint.test 2c82bd6a6ee5066c156040d2e9c2415646fcf96116ae7ad127eaf0c0b4a85f22 F test/wal2simple.test 96206c98bf64ab20ec00a1c0f6c709e258b98b39f2149889361f31966ce5a703 F test/wal2snapshot.test 7a5f4629a3c43a43c3440b8b2ea9f07de91a46b0b9eea5f08f62b5bf5b6468df @@ -1804,7 +1805,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 63483e22c775183e01924b5cb3d0f3655b63c3cdd06faf26cacd4d0913c0055c -R 44506fb1f03e87f3f3981d2947902fad +P 2f7f893a702728745445f6ef5f914c1157058a8fbdfd1a58cfb8906e5566729d +R 8f41f427e8d991f531c57ad5df021eef U dan -Z 537fbe586afdb7bdeed308e7bd56f47e +Z 5c736bd8817b6ee9ab94ee65b14c8406 diff --git a/manifest.uuid b/manifest.uuid index f8fc8d6c56..77bae9be40 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -2f7f893a702728745445f6ef5f914c1157058a8fbdfd1a58cfb8906e5566729d \ No newline at end of file +85a376fc6c77117b96814df800b3d68a441d69602ffc2eb8cd7dba29f25d69aa \ No newline at end of file diff --git a/test/wal2rollback.test b/test/wal2rollback.test new file mode 100644 index 0000000000..91842ee220 --- /dev/null +++ b/test/wal2rollback.test @@ -0,0 +1,62 @@ +# 2017 September 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL2" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl + +set testprefix wal2rollback +ifcapable !wal {finish_test ; return } + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + CREATE TABLE t2(a, b, c); + CREATE INDEX i1 ON t1(a); + CREATE INDEX i2 ON t1(b); + PRAGMA journal_mode = wal2; + PRAGMA cache_size = 5; + PRAGMA journal_size_limit = 10000; + WITH s(i) AS ( + SELECT 1 UNION ALL SELECT i+1 FROM s LIMIT 1000 + ) + INSERT INTO t1 SELECT i, i, randomblob(200) FROM s; +} {wal2 10000} + +do_test 1.1 { + expr [file size test.db-wal] > 10000 +} 1 + +do_test 1.2 { + execsql { + BEGIN; + UPDATE t1 SET b=b+1; + INSERT INTO t2 VALUES(1,2,3); + } + expr [file size test.db-wal2] > 10000 +} {1} + +breakpoint +do_execsql_test 1.3 { + ROLLBACK; + SELECT * FROM t2; + SELECT count(*) FROM t1 WHERE a=b; + PRAGMA integrity_check; +} {1000 ok} + + + +finish_test From 3eeda95d36f313059b7b9d423a83c1758f67997f Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 2 Jan 2019 17:00:00 +0000 Subject: [PATCH 25/51] Reinstate assert() statements in os_unix.c that were removed to allow wal-mode SHARED locks to be taken over more than one locking slot (this branch no longer does that, so the assert() statements can go back in). FossilOrigin-Name: 8445fb6d5dae98a0129514a10a15f08412bac5a1d8114b12e592c744ea2bc4c9 --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/os_unix.c | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/manifest b/manifest index b5dbfe2e3f..575be8a9db 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\slatest\strunk\schanges\sinto\sthis\sbranch. -D 2019-01-02T16:15:02.479 +C Reinstate\sassert()\sstatements\sin\sos_unix.c\sthat\swere\sremoved\sto\sallow\swal-mode\nSHARED\slocks\sto\sbe\staken\sover\smore\sthan\sone\slocking\sslot\s(this\sbranch\sno\nlonger\sdoes\sthat,\sso\sthe\sassert()\sstatements\scan\sgo\sback\sin). +D 2019-01-02T17:00:00.197 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in d8b254f8bb81bab43c340d70d17dc3babab40fcc8a348c8255881f780a45fee6 @@ -495,7 +495,7 @@ F src/os.c 8aeb0b0f40f8f5b0da03fe49706695adaf42d2f516ab95abc72e86c245e119de F src/os.h 48388821692e87da174ea198bf96b1b2d9d83be5dfc908f673ee21fafbe0d432 F src/os_common.h b2f4707a603e36811d9b1a13278bffd757857b85 F src/os_setup.h 0dbaea40a7d36bf311613d31342e0b99e2536586 -F src/os_unix.c 711480e9152f221098ec2b0d4ef94dc798f08af649c34f5cd4dc2bbf40c4f556 +F src/os_unix.c f6e91b8fd82af7afbfd073c4974ad6cdb8e62d9f65ceddb45167835a0567fdc0 F src/os_win.c 85d9e532d0444ab6c16d7431490c2e279e282aa0917b0e988996b1ae0de5c5a0 F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a F src/pager.c dc6d0acb3f89b4dcb801c86a0e722c1bde116e058c7216ff74341812a38bbdd9 @@ -1806,7 +1806,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 85a376fc6c77117b96814df800b3d68a441d69602ffc2eb8cd7dba29f25d69aa 0465d2fc0d3f8beaa1b6b0bd7bd51d69a524f30d889c9402e7d02cc06164a310 -R 8ab95e1c9057b1fa69e559d1716438ba +P 87ef68f9174b5e21fa2fb4f2fa23e0ecd6a0ea854422c8aa74cd7306a1ed8427 +R 81c151bc78b917b5a505ba7d0041b59f U dan -Z c36d42812d8f14aa5f845217b379c180 +Z 69b1e094c88bc1f9442e8f44aee16f43 diff --git a/manifest.uuid b/manifest.uuid index dc67799ac4..56ed701164 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -87ef68f9174b5e21fa2fb4f2fa23e0ecd6a0ea854422c8aa74cd7306a1ed8427 \ No newline at end of file +8445fb6d5dae98a0129514a10a15f08412bac5a1d8114b12e592c744ea2bc4c9 \ No newline at end of file diff --git a/src/os_unix.c b/src/os_unix.c index 82a19bf7e4..f20763e5b0 100644 --- a/src/os_unix.c +++ b/src/os_unix.c @@ -4291,7 +4291,7 @@ static int unixShmSystemLock( assert( pShmNode->nRef>0 || unixMutexHeld() ); /* Shared locks never span more than one byte */ - /* assert( n==1 || lockType!=F_RDLCK ); */ + assert( n==1 || lockType!=F_RDLCK ); /* Locks are within range */ assert( n>=1 && n<=SQLITE_SHM_NLOCK ); @@ -4794,7 +4794,7 @@ static int unixShmLock( || flags==(SQLITE_SHM_LOCK | SQLITE_SHM_EXCLUSIVE) || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_SHARED) || flags==(SQLITE_SHM_UNLOCK | SQLITE_SHM_EXCLUSIVE) ); - /* assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); */ + assert( n==1 || (flags & SQLITE_SHM_EXCLUSIVE)!=0 ); assert( pShmNode->hShm>=0 || pDbFd->pInode->bProcessLock==1 ); assert( pShmNode->hShm<0 || pDbFd->pInode->bProcessLock==0 ); From f552a2374008e72ec6ebd62bf060ce5655b67148 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 11 Jan 2019 14:58:03 +0000 Subject: [PATCH 26/51] Add new file doc/wal2.md to this branch. FossilOrigin-Name: b495dce153f7f886f3dba09593f29ce2375718bf6508f2cfffd1af8071a995ae --- doc/wal2.md | 98 +++++++++++++++++++++++++++++++++++++++++++++++++++ manifest | 11 +++--- manifest.uuid | 2 +- 3 files changed, 105 insertions(+), 6 deletions(-) create mode 100644 doc/wal2.md diff --git a/doc/wal2.md b/doc/wal2.md new file mode 100644 index 0000000000..5afd8ae606 --- /dev/null +++ b/doc/wal2.md @@ -0,0 +1,98 @@ + +Wal2 Mode Notes +=============== + +## Activating/Deactivating Wal2 Mode + +"Wal2" mode is very similar to "wal" mode. To change a database to wal2 mode, +use the command: + +> + PRAGMA journal_mode = wal2; + +It is not possible to change a database directly from "wal" mode to "wal2" +mode. Instead, it must first be changed to rollback mode. So, to change a wal +mode database to wal2 mode, the following two commands may be used: + +> + PRAGMA journal_mode = delete; + PRAGMA journal_mode = wal2; + +A database in wal2 mode may only be accessed by versions of SQLite compiled +from this branch. Attempting to use any other version of SQLite results in an +SQLITE_NOTADB error. A wal2 mode database may be changed back to rollback mode +(making it accessible by all versions of SQLite) using: + +> + PRAGMA journal_mode = delete; + +## The Advantage of Wal2 Mode + +In legacy wal mode, when a writer writes data to the database, it doesn't +modify the database file directly. Instead, it appends new data to the +"<database>-wal" file. Readers read data from both the original database +file and the "<database>-wal" file. At some point, data is copied from the +"<database>-wal" file into the database file, after which the wal file can +be deleted or overwritten. Copying data from the wal file into the database +file is called a "checkpoint", and may be done explictly (either by "PRAGMA +wal_checkpoint" or sqlite3_wal_checkpoint_v2()), or +automatically (by configuring "PRAGMA wal_autocheckpoint" - this is the +default). + +Checkpointers do not block writers, and writers do not block checkpointers. +However, if a writer writes to the database while a checkpoint is ongoing, +then the new data is appended to the end of the wal file. This means that, +even following the checkpoint, the wal file cannot be overwritten or deleted, +and so all subsequent transactions must also be appended to the wal file. The +work of the checkpointer is not wasted - SQLite remembers which parts of the +wal file have already been copied into the db file so that the next checkpoint +does not have to do so again - but it does mean that the wal file may grow +indefinitely if the checkpointer never gets a chance to finish without a +writer appending to the wal file. There are also circumstances in which +long-running readers may prevent a checkpointer from checkpointing the entire +wal file - also causing the wal file to grow indefinitely in a busy system. + +Wal2 mode does not have this problem. In wal2 mode, wal files do not grow +indefinitely even if the checkpointer never has a chance to finish +uninterrupted. + +In wal2 mode, the system uses two wal files instead of one. The files are named +"<database>-wal" and "<database>-wal2", where "<database>" is of +course the name of the database file. When data is written to the database, the +writer begins by appending the new data to the first wal file. Once the first +wal file has grown large enough, writers switch to appending data to the second +wal file. At this point the first wal file can be checkpointed (after which it +can be overwritten). Then, once the second wal file has grown large enough and +the first wal file has been checkpointed, writers switch back to the first wal +file. And so on. + +## Application Programming + +From the point of view of the user, the main differences between wal and +wal2 mode are to do with checkpointing: + + * In wal mode, a checkpoint may be attempted at any time. In wal2 + mode, the checkpointer has to wait until writers have switched + to the "other" wal file before a checkpoint can take place. + + * In wal mode, the wal-hook (callback registered using + sqlite3_wal_hook()) is invoked after a transaction is committed + with the total number of pages in the wal file as an argument. In wal2 + mode, the argument is either the total number of uncheckpointed pages in + both wal files, or - if the "other" wal file is empty or already + checkpointed - 0. + +Clients are recommended to use the same strategies for checkpointing wal2 mode +databases as for wal databases - by registering a wal-hook using +sqlite3_wal_hook() and attempting a checkpoint when the parameter +exceeds a certain threshold. + +However, it should be noted that although the wal-hook is invoked after each +transaction is committed to disk and database locks released, it is still +invoked from within the sqlite3_step() call used to execute the "COMMIT" +command. In BEGIN CONCURRENT systems, where the "COMMIT" is often protected by +an application mutex, this may reduce concurrency. In such systems, instead of +executing a checkpoint from within the wal-hook, a thread might defer this +action until after the application mutex has been released. + + diff --git a/manifest b/manifest index 575be8a9db..6af6923c47 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Reinstate\sassert()\sstatements\sin\sos_unix.c\sthat\swere\sremoved\sto\sallow\swal-mode\nSHARED\slocks\sto\sbe\staken\sover\smore\sthan\sone\slocking\sslot\s(this\sbranch\sno\nlonger\sdoes\sthat,\sso\sthe\sassert()\sstatements\scan\sgo\sback\sin). -D 2019-01-02T17:00:00.197 +C Add\snew\sfile\sdoc/wal2.md\sto\sthis\sbranch. +D 2019-01-11T14:58:03.764 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F Makefile.in d8b254f8bb81bab43c340d70d17dc3babab40fcc8a348c8255881f780a45fee6 @@ -40,6 +40,7 @@ F doc/F2FS.txt c1d4a0ae9711cfe0e1d8b019d154f1c29e0d3abfe820787ba1e9ed7691160fcd F doc/lemon.html 24956ab2995e55fe171e55bdd04f22b553957dc8bb43501dbb9311e30187e0d3 F doc/pager-invariants.txt 27fed9a70ddad2088750c4a2b493b63853da2710 F doc/vfs-shm.txt e101f27ea02a8387ce46a05be2b1a902a021d37a +F doc/wal2.md a807405a05e19a4945c5905a9ffa0fe45b8560dd7572461192501f565c19cdb5 F ext/README.md fd5f78013b0a2bc6f0067afb19e6ad040e89a10179b4f6f03eee58fac5f169bd F ext/async/README.txt e12275968f6fde133a80e04387d0e839b0c51f91 F ext/async/sqlite3async.c 0f3070cc3f5ede78f2b9361fb3b629ce200d7d74 @@ -1806,7 +1807,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 87ef68f9174b5e21fa2fb4f2fa23e0ecd6a0ea854422c8aa74cd7306a1ed8427 -R 81c151bc78b917b5a505ba7d0041b59f +P 8445fb6d5dae98a0129514a10a15f08412bac5a1d8114b12e592c744ea2bc4c9 +R 4b991801ac5f384be1f414fb71a05937 U dan -Z 69b1e094c88bc1f9442e8f44aee16f43 +Z 736399187a1b654bff250254a2a37532 diff --git a/manifest.uuid b/manifest.uuid index 56ed701164..1695a54411 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -8445fb6d5dae98a0129514a10a15f08412bac5a1d8114b12e592c744ea2bc4c9 \ No newline at end of file +b495dce153f7f886f3dba09593f29ce2375718bf6508f2cfffd1af8071a995ae \ No newline at end of file From 281ee2f754073c6ad6c7b09bd2d251f6e5d83caa Mon Sep 17 00:00:00 2001 From: drh Date: Fri, 8 Mar 2019 15:39:24 +0000 Subject: [PATCH 27/51] Add the bgckpt.c extension to Makefile.in and Makefile.msc. FossilOrigin-Name: 3712d625d625f962c627c74222467ad4858be46feda570449f9cb640549c9ddd --- Makefile.in | 1 + Makefile.msc | 1 + manifest | 16 ++++++++-------- manifest.uuid | 2 +- 4 files changed, 11 insertions(+), 9 deletions(-) diff --git a/Makefile.in b/Makefile.in index 3d9a566505..3619393f1f 100644 --- a/Makefile.in +++ b/Makefile.in @@ -437,6 +437,7 @@ TESTSRC += \ $(TOP)/ext/expert/sqlite3expert.c \ $(TOP)/ext/expert/test_expert.c \ $(TOP)/ext/misc/amatch.c \ + $(TOP)/ext/misc/bgckpt.c \ $(TOP)/ext/misc/carray.c \ $(TOP)/ext/misc/closure.c \ $(TOP)/ext/misc/csv.c \ diff --git a/Makefile.msc b/Makefile.msc index 179215ecf4..a414b73e27 100644 --- a/Makefile.msc +++ b/Makefile.msc @@ -1512,6 +1512,7 @@ TESTEXT = \ $(TOP)\ext\expert\sqlite3expert.c \ $(TOP)\ext\expert\test_expert.c \ $(TOP)\ext\misc\amatch.c \ + $(TOP)\ext\misc\bgckpt.c \ $(TOP)\ext\misc\carray.c \ $(TOP)\ext\misc\closure.c \ $(TOP)\ext\misc\csv.c \ diff --git a/manifest b/manifest index 6af6923c47..22a5c5e789 100644 --- a/manifest +++ b/manifest @@ -1,10 +1,10 @@ -C Add\snew\sfile\sdoc/wal2.md\sto\sthis\sbranch. -D 2019-01-11T14:58:03.764 +C Add\sthe\sbgckpt.c\sextension\sto\sMakefile.in\sand\sMakefile.msc. +D 2019-03-08T15:39:24.036 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea -F Makefile.in d8b254f8bb81bab43c340d70d17dc3babab40fcc8a348c8255881f780a45fee6 +F Makefile.in 903e48404f86dea5701c47f326933e4c846f68f35b22ecd349bddba80bbf8a94 F Makefile.linux-gcc 7bc79876b875010e8c8f9502eb935ca92aa3c434 -F Makefile.msc 3c4c7e94419ff28cb68850188c9d153b343aed4c5ebed5965426232ed67ff9d9 +F Makefile.msc 366f2446655bc49eabcfb1a042e1e3d317fd05441f36ab9ddc0ad16e46de2388 F README.md 377233394b905d3b2e2b33741289e093bc93f2e7adbe00923b2c5958c9a9edee F VERSION 453e2f4529ca208196d5567db28d549d7151f79efd33f6e6cfe6e613e583a0be F aclocal.m4 a5c22d164aff7ed549d53a90fa56d56955281f50 @@ -1807,7 +1807,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 8445fb6d5dae98a0129514a10a15f08412bac5a1d8114b12e592c744ea2bc4c9 -R 4b991801ac5f384be1f414fb71a05937 -U dan -Z 736399187a1b654bff250254a2a37532 +P b495dce153f7f886f3dba09593f29ce2375718bf6508f2cfffd1af8071a995ae +R 097e453c6dbf0709087c24fac1c6c3b8 +U drh +Z 49a629fd05fa20141267e72d5e7eee4a diff --git a/manifest.uuid b/manifest.uuid index 1695a54411..c6932ae30e 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -b495dce153f7f886f3dba09593f29ce2375718bf6508f2cfffd1af8071a995ae \ No newline at end of file +3712d625d625f962c627c74222467ad4858be46feda570449f9cb640549c9ddd \ No newline at end of file From 9bf3da81a0ddd287e4128dc9a7742647d33a9de4 Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 19 Nov 2020 16:08:08 +0000 Subject: [PATCH 28/51] Fix things so that sqlite3_database_file_object() works with wal2 filenames. FossilOrigin-Name: c2426ae8a80d61e1ede2d065c10145ba31d4ac615803f48b7903f5f7daaa6f89 --- manifest | 14 +++++++------- manifest.uuid | 2 +- src/pager.c | 3 +++ src/wal.c | 10 ++-------- 4 files changed, 13 insertions(+), 16 deletions(-) diff --git a/manifest b/manifest index 89f4cd0170..34d30c5eb2 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\slatest\strunk\schanges\sinto\sthis\sbranch. -D 2020-08-11T11:34:00.038 +C Fix\sthings\sso\sthat\ssqlite3_database_file_object()\sworks\swith\swal2\sfilenames. +D 2020-11-19T16:08:08.155 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -524,7 +524,7 @@ F src/os_setup.h 0dbaea40a7d36bf311613d31342e0b99e2536586 F src/os_unix.c 9b1b860163fd2d4d7679b5260d384d1a9f88ef917a90f28963eca8acd472d8c8 F src/os_win.c a2149ff0a85c1c3f9cc102a46c673ce87e992396ba3411bfb53db66813b32f1d F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a -F src/pager.c 35fcb2d9400404d0d8f719461b411076bcd5597574b7a238f3a04f959842a88d +F src/pager.c aafa89d881c1d56b2dc212f99a6527ca3d4f7105a01e755871657981fc411005 F src/pager.h 9d38ac4b9f0e1361f7ddb6aa0675a54f5e4f085703108d3aafe7868872113f92 F src/parse.y 5bdb760a29c0b25caf7e80e82210b81cd2ea3066d5199ca29e6eac40b34bc184 F src/pcache.c 385ff064bca69789d199a98e2169445dc16e4291fa807babd61d4890c3b34177 @@ -621,7 +621,7 @@ F src/vdbetrace.c fa3bf238002f0bbbdfb66cc8afb0cea284ff9f148d6439bc1f6f2b4c3b7143 F src/vdbevtab.c f99b275366c5fc5e2d99f734729880994ab9500bdafde7fae3b02d562b9d323c F src/vtab.c 5f5fc793092f53bbdfde296c50f563fb7bda58cf48e9cf6a8bdfbc5abd409845 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c dafdd709d1e01f77719a983f5b3071ccd82808d59a044f8ce48cd0bfa1c1fea5 +F src/wal.c 055328936bf2519e235adcb65b3e8d416f0835ac81d577d08fd34cd29432af88 F src/wal.h d01234e828943e002040c22a7e017642962f9fd9b2dc142fa599769ae4e459e9 F src/walker.c 3df26a33dc4f54e8771600fb7fdebe1ece0896c2ad68c30ab40b017aa4395049 F src/where.c 2ea911238674e9baaeddf105dddabed92692a01996073c4d4983f9a7efe481f9 @@ -1891,7 +1891,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 0c0d0a77bc8fa68ee584ca6d7a0408dcd00734a9b6b69d127ee8477c230a749c 5bbd4bddd3b9fa64d134ed62bce3eb4a09456bf24dec2474b5d764a3a3775964 -R 19eea2388f833c601e8554bc39b427c6 +P 91262e665755a1430f3b667867b9c4fa0cc3aa0388c21e8ba436485beefa9f4a +R 526f45cc6b30de9a0c02f320de6299a2 U dan -Z e3452c6286d56d7ce7f02a59585758e0 +Z 12962dd7c3e8bd2cbe9af67559b28702 diff --git a/manifest.uuid b/manifest.uuid index 7bc87f2c5c..add304a4f1 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -91262e665755a1430f3b667867b9c4fa0cc3aa0388c21e8ba436485beefa9f4a \ No newline at end of file +c2426ae8a80d61e1ede2d065c10145ba31d4ac615803f48b7903f5f7daaa6f89 \ No newline at end of file diff --git a/src/pager.c b/src/pager.c index 359c801870..9f837f6156 100644 --- a/src/pager.c +++ b/src/pager.c @@ -4782,6 +4782,7 @@ int sqlite3PagerOpen( nPathname + 8 + 1 + /* Journal filename */ #ifndef SQLITE_OMIT_WAL nPathname + 4 + 1 + /* WAL filename */ + nPathname + 5 + 1 + /* Second WAL filename */ #endif 3 /* Terminator */ ); @@ -4834,6 +4835,8 @@ int sqlite3PagerOpen( sqlite3FileSuffix3(zFilename, pPager->zWal); pPtr = (u8*)(pPager->zWal + sqlite3Strlen30(pPager->zWal)+1); #endif + memcpy(pPtr, zPathname, nPathname); pPtr += nPathname; + memcpy(pPtr, "-wal2", 5); pPtr += 5 + 1; }else{ pPager->zWal = 0; } diff --git a/src/wal.c b/src/wal.c index 622264a15f..47124992a8 100644 --- a/src/wal.c +++ b/src/wal.c @@ -1887,7 +1887,6 @@ int sqlite3WalOpen( int rc; /* Return Code */ Wal *pRet; /* Object to allocate and return */ int flags; /* Flags passed to OsOpen() */ - int nWalName; /* Length of zWalName in bytes */ int nByte; /* Bytes of space to allocate */ assert( zWalName && zWalName[0] ); @@ -1908,8 +1907,7 @@ int sqlite3WalOpen( assert( UNIX_SHM_BASE==WALINDEX_LOCK_OFFSET ); #endif - nWalName = sqlite3Strlen30(zWalName); - nByte = sizeof(Wal) + pVfs->szOsFile*2 + nWalName+2; + nByte = sizeof(Wal) + pVfs->szOsFile*2; /* Allocate an instance of struct Wal to return. */ *ppWal = 0; @@ -1929,11 +1927,7 @@ int sqlite3WalOpen( pRet->padToSectorBoundary = 1; pRet->exclusiveMode = (bNoShm ? WAL_HEAPMEMORY_MODE: WAL_NORMAL_MODE); pRet->bWal2 = bWal2; - - pRet->zWalName2 = (char*)pRet + sizeof(Wal) + 2*pVfs->szOsFile; - memcpy(pRet->zWalName2, zWalName, nWalName); - pRet->zWalName2[nWalName] = '2'; - pRet->zWalName2[nWalName+1] = '\0'; + pRet->zWalName2 = &zWalName[sqlite3Strlen30(zWalName)+1]; /* Open a file handle on the first write-ahead log file. */ flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_WAL); From 50c9522c24382cd76cb7d6737368c35a88d7e4f0 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 20 Nov 2020 11:17:48 +0000 Subject: [PATCH 29/51] Declare a variable "const" in order to avoid compiler warnings. FossilOrigin-Name: 4f5481bf291c39e228dd9c67f504dfe70879617b7266245d6236cd1d2d99274c --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/wal.c | 2 +- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/manifest b/manifest index 34d30c5eb2..5e5b81e19c 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sthings\sso\sthat\ssqlite3_database_file_object()\sworks\swith\swal2\sfilenames. -D 2020-11-19T16:08:08.155 +C Declare\sa\svariable\s"const"\sin\sorder\sto\savoid\scompiler\swarnings. +D 2020-11-20T11:17:48.207 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -621,7 +621,7 @@ F src/vdbetrace.c fa3bf238002f0bbbdfb66cc8afb0cea284ff9f148d6439bc1f6f2b4c3b7143 F src/vdbevtab.c f99b275366c5fc5e2d99f734729880994ab9500bdafde7fae3b02d562b9d323c F src/vtab.c 5f5fc793092f53bbdfde296c50f563fb7bda58cf48e9cf6a8bdfbc5abd409845 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 055328936bf2519e235adcb65b3e8d416f0835ac81d577d08fd34cd29432af88 +F src/wal.c 5f03d6aa3c213b0369cef9dadbf6a36b3d769899aa8afbc9d926c4c9c72e09e5 F src/wal.h d01234e828943e002040c22a7e017642962f9fd9b2dc142fa599769ae4e459e9 F src/walker.c 3df26a33dc4f54e8771600fb7fdebe1ece0896c2ad68c30ab40b017aa4395049 F src/where.c 2ea911238674e9baaeddf105dddabed92692a01996073c4d4983f9a7efe481f9 @@ -1891,7 +1891,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 91262e665755a1430f3b667867b9c4fa0cc3aa0388c21e8ba436485beefa9f4a -R 526f45cc6b30de9a0c02f320de6299a2 +P c2426ae8a80d61e1ede2d065c10145ba31d4ac615803f48b7903f5f7daaa6f89 +R bbd5698d99ebe7b0ec2ea77bc05f1921 U dan -Z 12962dd7c3e8bd2cbe9af67559b28702 +Z 3d5cfe3d18a8d38ec6e1998df13cb1b1 diff --git a/manifest.uuid b/manifest.uuid index add304a4f1..9a42e4bfc6 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -c2426ae8a80d61e1ede2d065c10145ba31d4ac615803f48b7903f5f7daaa6f89 \ No newline at end of file +4f5481bf291c39e228dd9c67f504dfe70879617b7266245d6236cd1d2d99274c \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 47124992a8..ba24591988 100644 --- a/src/wal.c +++ b/src/wal.c @@ -719,7 +719,7 @@ struct Wal { u32 minFrame; /* Ignore wal frames before this one */ u32 iReCksum; /* On commit, recalculate checksums from here */ const char *zWalName; /* Name of WAL file */ - char *zWalName2; /* Name of second WAL file */ + const char *zWalName2; /* Name of second WAL file */ u32 nCkpt; /* Checkpoint sequence counter in the wal-header */ #ifdef SQLITE_DEBUG u8 lockError; /* True if a locking error has occurred */ From 348093c7197ba505f8d65832aa318505a08bcdd8 Mon Sep 17 00:00:00 2001 From: dan Date: Wed, 15 Dec 2021 12:51:02 +0000 Subject: [PATCH 30/51] Fix a problem causing one wal file to be deleted without being checkpointed on close in cases where the last connection to close the db does so with an out of date wal-index header in memory. FossilOrigin-Name: b0a70a2356c44d65c54c6d9bdf05972071462e72c28d6c6e593147ffa3d27ef2 --- manifest | 17 +++++---- manifest.uuid | 2 +- src/wal.c | 10 ++++- test/wal2openclose.test | 81 +++++++++++++++++++++++++++++++++++++++++ test/wal2simple.test | 26 ++++++------- 5 files changed, 112 insertions(+), 24 deletions(-) create mode 100644 test/wal2openclose.test diff --git a/manifest b/manifest index 7588404f42..3a8447f4d3 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\sversion\s3.37.0\sinto\sthe\swal2\sbranch. -D 2021-11-27T15:00:54.793 +C Fix\sa\sproblem\scausing\sone\swal\sfile\sto\sbe\sdeleted\swithout\sbeing\scheckpointed\son\sclose\sin\scases\swhere\sthe\slast\sconnection\sto\sclose\sthe\sdb\sdoes\sso\swith\san\sout\sof\sdate\swal-index\sheader\sin\smemory. +D 2021-12-15T12:51:02.972 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -636,7 +636,7 @@ F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf8 F src/vdbevtab.c f99b275366c5fc5e2d99f734729880994ab9500bdafde7fae3b02d562b9d323c F src/vtab.c 9d5c3f49d3a6959b6eef287bb8fa773563102a80a835c3314c57144412709e78 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c ebd6d4c0e47a752a4f38fa486bf7243d2af1a8323b5a98c4a1dacdc590d87716 +F src/wal.c c373a0c800337f512672bc0365bc95a915df43d2e45f1427f1a02bf5e98edcee F src/wal.h d01234e828943e002040c22a7e017642962f9fd9b2dc142fa599769ae4e459e9 F src/walker.c f890a3298418d7cba3b69b8803594fdc484ea241206a8dfa99db6dd36f8cbb3b F src/where.c de0d4ff409c7b62a8803f9f267cc2c7fedddbc00de9ab7b5382c507383c18665 @@ -1726,12 +1726,13 @@ F test/wal2.test 31f6e2c404b9f2cdf9ca19b105a1742fdc19653c2c936da39e3658c61752404 F test/wal2big.test 0b4ec526f9ca4bbabc355042c38045ae2e253fb46eb327bb7693d0122bc6968b F test/wal2fault.test 2e8e60cacd5bcd451618aeffd05f676894d17202d3e2986e288d36e2c5993249 F test/wal2lock.test 0ef98d72dc6bcf7711dedd684760488400d9a9a6eec0dc5d3822060437793552 +F test/wal2openclose.test 2b26be723ea7f4263c8d5d70b37efd1c359561a0526e39466c45fe8e6478daee F test/wal2recover.test ba8f4bc9397c838734619f9e759bd98b00e355347b3cf80a2e677610d231d5d8 F test/wal2recover2.test 0c46afc759e4392a3c12fba17432b880c93a13bf4246d1be5101b00bae4c5f01 F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal2rollback.test 23adc4a099b23f6aaea8b04fdca1c35861d887dd80f8be7da2d5273eb777e428 F test/wal2savepoint.test 2c82bd6a6ee5066c156040d2e9c2415646fcf96116ae7ad127eaf0c0b4a85f22 -F test/wal2simple.test 4a9bfc34dc888e75acb4e7bdce0bf15e960cbee02f185c07a396b35c49d4431d +F test/wal2simple.test 320a08927f307d0ead26fa3fcef4e3f64279f49be9504f918cb33294f75aeec8 F test/wal2snapshot.test 7a5f4629a3c43a43c3440b8b2ea9f07de91a46b0b9eea5f08f62b5bf5b6468df F test/wal3.test 2a93004bc0fb2b5c29888964024695bade278ab2 F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c @@ -1945,7 +1946,7 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 135133b7a65c652e95b0642bc2358425af72e46b1b6b64d256547b5e5e8f7e41 bd41822c7424d393a30e92ff6cb254d25c26769889c1499a18a0b9339f5d6c8a -R 201b14339f58661bd2f967a1bd6734d2 -U drh -Z 71b7d59afa12a68b678449df3fa88a69 +P 3425af625bac6c89950a50c2a22c4b7f66b0080811fe16ec8879158420641d5c +R 3f40ea32fb804a93691154c1384222dd +U dan +Z 459a6eb607efc418f73088930cf8495d diff --git a/manifest.uuid b/manifest.uuid index 62aa877b1a..038301ccda 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -3425af625bac6c89950a50c2a22c4b7f66b0080811fe16ec8879158420641d5c \ No newline at end of file +b0a70a2356c44d65c54c6d9bdf05972071462e72c28d6c6e593147ffa3d27ef2 \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 23e1a98065..d9b71117bd 100644 --- a/src/wal.c +++ b/src/wal.c @@ -794,6 +794,7 @@ struct Wal { #ifdef SQLITE_ENABLE_SNAPSHOT WalIndexHdr *pSnapshot; /* Start transaction here if not NULL */ #endif + int bClosing; /* Set to true at start of sqlite3WalClose() */ int bWal2; /* bWal2 flag passed to WalOpen() */ #ifdef SQLITE_ENABLE_SETLK_TIMEOUT sqlite3 *db; @@ -2850,6 +2851,7 @@ int sqlite3WalClose( int rc = SQLITE_OK; if( pWal ){ int isDelete = 0; /* True to unlink wal and wal-index files */ + pWal->bClosing = 1; /* If an EXCLUSIVE lock can be obtained on the database file (using the ** ordinary, rollback-mode locking methods, this guarantees that the @@ -4770,13 +4772,17 @@ int sqlite3WalCheckpoint( } } - if( isChanged ){ + if( isChanged && pWal->bClosing==0 ){ /* If a new wal-index header was loaded before the checkpoint was ** performed, then the pager-cache associated with pWal is now ** out of date. So zero the cached wal-index header to ensure that ** next time the pager opens a snapshot on this database it knows that ** the cache needs to be reset. - */ + ** + ** Except, do not do this if the wal is being closed. In this case + ** the caller needs the wal-index header to check if the database is + ** in wal2 mode and the "other" wal file also needs to be checkpointed. + ** Besides, the pager cache will not be used again in this case. */ memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); } diff --git a/test/wal2openclose.test b/test/wal2openclose.test new file mode 100644 index 0000000000..66e5875813 --- /dev/null +++ b/test/wal2openclose.test @@ -0,0 +1,81 @@ +# 2017 September 19 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL2" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl + +set testprefix wal2openclose +ifcapable !wal {finish_test ; return } + +do_execsql_test 1.0 { + CREATE TABLE t1(a, b, c); + PRAGMA journal_mode = wal2; + PRAGMA wal_autocheckpoint = 0; + PRAGMA journal_size_limit = 75000; +} {wal2 0 75000} + +do_test 1.1 { + for {set ii 1} {$ii <= 200} {incr ii} { + execsql { + INSERT INTO t1 VALUES($ii, $ii, $ii); + } + } + expr ([file size test.db-wal2] - 75000) > 30000 +} {1} + +do_test 1.2 { + db close + list [file exists test.db-wal] [file exists test.db-wal2] +} {0 0} + +sqlite3 db test.db +do_execsql_test 1.3 { + SELECT sum(c) FROM t1 +} {20100} +db close + +#------------------------------------------------------------------------- +reset_db +do_execsql_test 2.0 { + CREATE TABLE t1(a, b, c); + PRAGMA journal_mode = wal2; + INSERT INTO t1 VALUES(1, 2, 3); +} {wal2} +db_save_and_close + +db_restore_and_reopen +do_execsql_test 2.1 { + SELECT * FROM t1; +} {1 2 3} + +do_test 2.2 { + sqlite3 db2 test.db + db2 eval {INSERT INTO t1 VALUES(4, 5, 6)} + db2 close +} {} + +breakpoint +db close +sqlite3 db test.db +do_execsql_test 2.2 { + SELECT * FROM t1; +} {1 2 3 4 5 6} + + + +finish_test diff --git a/test/wal2simple.test b/test/wal2simple.test index e17fb66ec9..e63f4e1759 100644 --- a/test/wal2simple.test +++ b/test/wal2simple.test @@ -361,23 +361,23 @@ do_test 7.3.1 { SELECT count(*) FROM t1; } db2 list [file size test.db-wal] [file size test.db-wal2] [file size test.db] -} {12608 3176 11264} +} {12608 3176 12288} do_test 7.3.2 { execsql { PRAGMA wal_checkpoint } list [file size test.db-wal] [file size test.db-wal2] [file size test.db] -} {12608 3176 21504} +} {12608 3176 22528} do_test 7.3.3 { execsql { INSERT INTO t1 VALUES(randomblob(10000)); INSERT INTO t1 VALUES(randomblob(500)); } list [file size test.db-wal] [file size test.db-wal2] [file size test.db] -} {12608 18896 21504} +} {12608 18896 22528} do_test 7.3.4 { execsql END db2 execsql { INSERT INTO t1 VALUES(randomblob(5000)); } list [file size test.db-wal] [file size test.db-wal2] [file size test.db] -} {12608 18896 21504} +} {12608 18896 22528} # Connection db2 is holding a PART2 lock. # @@ -406,22 +406,22 @@ do_test 7.4.1 { SELECT count(*) FROM t1; } db2 list [file size test.db-wal] [file size test.db-wal2] [file size test.db] -} {12608 12608 44032} +} {12608 12608 50176} do_test 7.4.2 { execsql { INSERT INTO t1 VALUES(randomblob(5000)); } list [file size test.db-wal] [file size test.db-wal2] [file size test.db] -} {12608 12608 44032} +} {12608 12608 50176} do_test 7.4.3 { execsql { PRAGMA wal_checkpoint } list [file size test.db-wal] [file size test.db-wal2] [file size test.db] -} {12608 12608 44032} +} {12608 12608 50176} do_test 7.4.4 { execsql END db2 execsql { PRAGMA wal_checkpoint } list [file size test.db-wal] [file size test.db-wal2] [file size test.db] -} {12608 12608 54272} +} {12608 12608 60416} # Connection db2 is holding a PART1_FULL2 lock. # @@ -451,24 +451,24 @@ do_test 7.5.1 { SELECT count(*) FROM t1; } db2 list [file size test.db-wal] [file size test.db-wal2] [file size test.db] -} {12608 12608 64512} +} {12608 12608 76800} do_test 7.5.2 { execsql { PRAGMA wal_checkpoint } list [file size test.db-wal] [file size test.db-wal2] [file size test.db] -} {12608 12608 75776} +} {12608 12608 87040} do_test 7.5.3.1 { execsql { INSERT INTO t1 VALUES(randomblob(5000)) } list [file size test.db-wal] [file size test.db-wal2] [file size test.db] -} {14704 12608 75776} +} {14704 12608 87040} do_test 7.5.3.2 { execsql { INSERT INTO t1 VALUES(randomblob(5000)) } list [file size test.db-wal] [file size test.db-wal2] [file size test.db] -} {22040 12608 75776} +} {22040 12608 87040} do_test 7.5.4 { execsql END db2 execsql { INSERT INTO t1 VALUES(randomblob(5000)) } list [file size test.db-wal] [file size test.db-wal2] [file size test.db] -} {22040 12608 75776} +} {22040 12608 87040} finish_test From 86b7bb29daf7e86599b946b86aa57ea6e86b3e05 Mon Sep 17 00:00:00 2001 From: dan Date: Mon, 27 Jun 2022 21:42:45 +0000 Subject: [PATCH 31/51] Fix a problem causing false corruption reports following recovery of a *-wal file that contains one or more transactions and a *-wal2 file that contains a valid header but no valid transactions. FossilOrigin-Name: f6eafb65a43c650b065abe4a59e329c977cab1856c72dc9162046576d7bbfc8a --- manifest | 15 ++++++------ manifest.uuid | 2 +- src/wal.c | 6 +++-- test/wal2recover3.test | 52 ++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 65 insertions(+), 10 deletions(-) create mode 100644 test/wal2recover3.test diff --git a/manifest b/manifest index 3b4e819139..8cd56d9d96 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\sversion\s3.39.0\sinto\sthe\swal2\sbranch. -D 2022-06-25T16:53:36.816 +C Fix\sa\sproblem\scausing\sfalse\scorruption\sreports\sfollowing\srecovery\sof\sa\s*-wal\sfile\sthat\scontains\sone\sor\smore\stransactions\sand\sa\s*-wal2\sfile\sthat\scontains\sa\svalid\sheader\sbut\sno\svalid\stransactions. +D 2022-06-27T21:42:45.196 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -656,7 +656,7 @@ F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf8 F src/vdbevtab.c f99b275366c5fc5e2d99f734729880994ab9500bdafde7fae3b02d562b9d323c F src/vtab.c 3d72c780d1ea08906a198e4f033921a658a54590e3ed72c544995d84f3f9464a F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c a9cc35881efbb97e568b654dffa2716cf4c5d3fd15489980ab96e48fc5bc60a7 +F src/wal.c f5fcbb7c5692690d877fb65f97f392cfd6a568e56c765c3f3f18bb9ed82409ed F src/wal.h d01234e828943e002040c22a7e017642962f9fd9b2dc142fa599769ae4e459e9 F src/walker.c f890a3298418d7cba3b69b8803594fdc484ea241206a8dfa99db6dd36f8cbb3b F src/where.c 9a44063e60d8f42dd9dc8147b8e8dcfc315bbd13e25c395211292c36d828c869 @@ -1773,6 +1773,7 @@ F test/wal2lock.test 0ef98d72dc6bcf7711dedd684760488400d9a9a6eec0dc5d38220604377 F test/wal2openclose.test 2b26be723ea7f4263c8d5d70b37efd1c359561a0526e39466c45fe8e6478daee F test/wal2recover.test ba8f4bc9397c838734619f9e759bd98b00e355347b3cf80a2e677610d231d5d8 F test/wal2recover2.test 0c46afc759e4392a3c12fba17432b880c93a13bf4246d1be5101b00bae4c5f01 +F test/wal2recover3.test 4a91689e165a38bc401736e6518188c2b0ff4fa1566d1810b8867536db128177 F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal2rollback.test 23adc4a099b23f6aaea8b04fdca1c35861d887dd80f8be7da2d5273eb777e428 F test/wal2savepoint.test 2c82bd6a6ee5066c156040d2e9c2415646fcf96116ae7ad127eaf0c0b4a85f22 @@ -1991,8 +1992,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 5a5e6e368f5cc8fe7cf29696a07942101f5c114084d4117acf277bb3ad8555ac 14e166f40dbfa6e055543f8301525f2ca2e96a02a57269818b9e69e162e98918 -R e5fc911f0a86748145e1da0886462030 -U drh -Z 1da384b15e9d7950d5559005e2984b62 +P ad3a7005e704711fb9de9e96883db3aed82c4f132a66cb74daf67c2527d79f73 +R 5eb4043659412b932d516effbdde5301 +U dan +Z 7526c786ad8a0ce3d92bc4d2c949f076 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 5360f63fdd..b34953287d 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -ad3a7005e704711fb9de9e96883db3aed82c4f132a66cb74daf67c2527d79f73 \ No newline at end of file +f6eafb65a43c650b065abe4a59e329c977cab1856c72dc9162046576d7bbfc8a \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index f8fb749c85..14c330a85c 100644 --- a/src/wal.c +++ b/src/wal.c @@ -1827,7 +1827,8 @@ static int walIndexRecover(Wal *pWal){ if( isOpen(pWal->apWalFd[1]) ){ /* The case where *-wal2 may follow *-wal */ if( nCkpt2<=0x0F && nCkpt2==nCkpt1+1 ){ - if( sqlite3Get4byte((u8*)(&pWal->hdr.aSalt[0]))==hdr.aFrameCksum[0] + if( pWal->hdr.mxFrame + && sqlite3Get4byte((u8*)(&pWal->hdr.aSalt[0]))==hdr.aFrameCksum[0] && sqlite3Get4byte((u8*)(&pWal->hdr.aSalt[1]))==hdr.aFrameCksum[1] ){ walidxSetFile(&pWal->hdr, 1); @@ -1840,7 +1841,8 @@ static int walIndexRecover(Wal *pWal){ /* When *-wal may follow *-wal2 */ if( (nCkpt2==0x0F && nCkpt1==0) || (nCkpt2<0x0F && nCkpt2==nCkpt1-1) ){ - if( sqlite3Get4byte((u8*)(&hdr.aSalt[0]))==pWal->hdr.aFrameCksum[0] + if( hdr.mxFrame + && sqlite3Get4byte((u8*)(&hdr.aSalt[0]))==pWal->hdr.aFrameCksum[0] && sqlite3Get4byte((u8*)(&hdr.aSalt[1]))==pWal->hdr.aFrameCksum[1] ){ SWAP(WalIndexHdr, pWal->hdr, hdr); diff --git a/test/wal2recover3.test b/test/wal2recover3.test new file mode 100644 index 0000000000..eab3011341 --- /dev/null +++ b/test/wal2recover3.test @@ -0,0 +1,52 @@ +# 2022 June 28 +# +# The author disclaims copyright to this source code. In place of +# a legal notice, here is a blessing: +# +# May you do good and not evil. +# May you find forgiveness for yourself and forgive others. +# May you share freely, never taking more than you give. +# +#*********************************************************************** +# This file implements regression tests for SQLite library. The +# focus of this file is testing the operation of the library in +# "PRAGMA journal_mode=WAL2" mode. +# + +set testdir [file dirname $argv0] +source $testdir/tester.tcl +source $testdir/lock_common.tcl +source $testdir/malloc_common.tcl +source $testdir/wal_common.tcl + +set testprefix wal2recover3 +ifcapable !wal {finish_test ; return } + +do_execsql_test 1.0 { + CREATE TABLE t1(x); + CREATE TABLE t2(x); + PRAGMA journal_mode = wal2; + PRAGMA wal_autocheckpoint = 0; + PRAGMA journal_size_limit = 10000; +} {wal2 0 10000} + +do_execsql_test 1.1 { + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1500 ) + INSERT INTO t1 SELECT i FROM s; + WITH s(i) AS ( VALUES(1) UNION ALL SELECT i+1 FROM s WHERE i<1500 ) + INSERT INTO t2 SELECT i FROM s; +} + +db_save_and_close +set fd [open sv_test.db-wal2 r+] +seek $fd 4000 +puts -nonewline $fd 0 +close $fd + +db_restore_and_reopen +do_execsql_test 1.2 { + SELECT sql FROM sqlite_schema; +} {{CREATE TABLE t1(x)} {CREATE TABLE t2(x)}} + +finish_test + From 70cfee98e540c51e89ba6d12a7e11c9dfb8a09ab Mon Sep 17 00:00:00 2001 From: drh <> Date: Mon, 6 Feb 2023 19:00:53 +0000 Subject: [PATCH 32/51] Disable an invalid assert(). Add the "WAL2" compile-time property so that test scripts can more easily determine that they are dealing with a WAL2-capable build. FossilOrigin-Name: e93113259e05fb6b81ad1b4a460bd54168ff5c372b643d4845c1b1c87b7db204 --- manifest | 16 ++++++++-------- manifest.uuid | 2 +- src/ctime.c | 1 + src/wal.c | 2 +- tool/mkctimec.tcl | 2 ++ 5 files changed, 13 insertions(+), 10 deletions(-) diff --git a/manifest b/manifest index 9d10006bf2..05a28527d9 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\sthe\slatest\strunk\senhancements\sinto\sthe\swal2\sbranch. -D 2023-02-06T17:48:49.853 +C Disable\san\sinvalid\sassert().\s\sAdd\sthe\s"WAL2"\scompile-time\sproperty\sso\sthat\ntest\sscripts\scan\smore\seasily\sdetermine\sthat\sthey\sare\sdealing\swith\sa\nWAL2-capable\sbuild. +D 2023-02-06T19:00:53.294 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -568,7 +568,7 @@ F src/btreeInt.h 06bb2c1a07172d5a1cd27a2a5d617b93b1e976c5873709c31964786f86365a6 F src/build.c c55ab6d1b089ceef57160e840f05f692955ac90944c3d04fcf01d97fd7bfd08d F src/callback.c 4cd7225b26a97f7de5fee5ae10464bed5a78f2adefe19534cc2095b3a8ca484a F src/complete.c a3634ab1e687055cd002e11b8f43eb75c17da23e -F src/ctime.c 20507cc0b0a6c19cd882fcd0eaeda32ae6a4229fb4b024cfdf3183043d9b703d +F src/ctime.c f439c21d439aaf4706950a2597474f1a796b557a0750666308202c0f601ef0fd F src/date.c 94ce83b4cd848a387680a5f920c9018c16655db778c4d36525af0a0f34679ac5 F src/dbpage.c d47549716549311f79dc39fe5c8fb19390a6eb2c960f8e37c89a9c4de0c1052e F src/dbstat.c ec92074baa61d883de58c945162d9e666c13cd7cf3a23bc38b4d1c4d0b2c2bef @@ -705,7 +705,7 @@ F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf8 F src/vdbevtab.c aae4bd769410eb7e1d02c42613eec961d514459b1c3c1c63cfc84e92a137daac F src/vtab.c a39f6ed161f16a84ff445af9dba4776b2cf0898be08a33bee8e1128a66c0074b F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c f5fcbb7c5692690d877fb65f97f392cfd6a568e56c765c3f3f18bb9ed82409ed +F src/wal.c b433980d0e97fa3469cb46136fd59194df638c684afc299b662b1114f3495af6 F src/wal.h d01234e828943e002040c22a7e017642962f9fd9b2dc142fa599769ae4e459e9 F src/walker.c f890a3298418d7cba3b69b8803594fdc484ea241206a8dfa99db6dd36f8cbb3b F src/where.c 3bbca705410258969d6d2e0c54b468de9674445d41b67714bb4fc3f1edef41d2 @@ -1984,7 +1984,7 @@ F tool/max-limits.c cbb635fbb37ae4d05f240bfb5b5270bb63c54439 F tool/merge-test.tcl de76b62f2de2a92d4c1ca4f976bce0aea6899e0229e250479b229b2a1914b176 F tool/mkautoconfamal.sh f62353eb6c06ab264da027fd4507d09914433dbdcab9cb011cdc18016f1ab3b8 F tool/mkccode.tcl 86463e68ce9c15d3041610fedd285ce32a5cf7a58fc88b3202b8b76837650dbe x -F tool/mkctimec.tcl c185cf1bdcd3d9bd3c06f77a2fd2df8a4a0d07266f992ecda75286965ba3574c x +F tool/mkctimec.tcl 82e75faafe0625a9fedd3817a22bd9ac337b5ab8ba5486a9a113afce4ed1f67b x F tool/mkkeywordhash.c 35bfc41adacc4aa6ef6fca7fd0c63e0ec0534b78daf4d0cfdebe398216bbffc3 F tool/mkmsvcmin.tcl 6ecab9fe22c2c8de4d82d4c46797bda3d2deac8e763885f5a38d0c44a895ab33 F tool/mkopcodec.tcl 33d20791e191df43209b77d37f0ff0904620b28465cca6990cf8d60da61a07ef @@ -2059,8 +2059,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P c9b0180203378da26dc958c183ebb59274537e9fee3b346933b1e0844ed68a4a 5dde07a91dcf99b9c9a418b4e2178f66eef4cffd4799538a419674314a7530f9 -R 84cde0279afd88fb09c07f8fe9ea2f8c +P 44f8f33d4ab159476800142e763dfc6beb2c2c1d857c8eebb286498dec4004fd +R 4a29884f29ee8d2f8e34960946ff4a8b U drh -Z 5942c82874e68fc52050e3aca6e113bd +Z 5908cdab3a7ef3394b937ba8240e9eba # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 22a89d0b04..a1815f8b78 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -44f8f33d4ab159476800142e763dfc6beb2c2c1d857c8eebb286498dec4004fd \ No newline at end of file +e93113259e05fb6b81ad1b4a460bd54168ff5c372b643d4845c1b1c87b7db204 \ No newline at end of file diff --git a/src/ctime.c b/src/ctime.c index c853e5ba46..75dbd5c0b1 100644 --- a/src/ctime.c +++ b/src/ctime.c @@ -770,6 +770,7 @@ static const char * const sqlite3azCompileOpt[] = { #ifdef SQLITE_VDBE_COVERAGE "VDBE_COVERAGE", #endif +"WAL2", #ifdef SQLITE_WIN32_MALLOC "WIN32_MALLOC", #endif diff --git a/src/wal.c b/src/wal.c index 14c330a85c..113c07c3d2 100644 --- a/src/wal.c +++ b/src/wal.c @@ -3929,7 +3929,7 @@ int sqlite3WalFindFrame( } #endif -#ifdef SQLITE_ENABLE_EXPENSIVE_ASSERT +#if defined(SQLITE_ENABLE_EXPENSIVE_ASSERT) && /*TODO*/ 0 /* If expensive assert() statements are available, do a linear search ** of the wal-index file content. Make sure the results agree with the ** result obtained using the hash indexes above. diff --git a/tool/mkctimec.tcl b/tool/mkctimec.tcl index 0438b11b35..3920f50792 100755 --- a/tool/mkctimec.tcl +++ b/tool/mkctimec.tcl @@ -379,6 +379,8 @@ set options(THREADSAFE) { #endif } +set options(WAL2) { "WAL2", } + proc trim_name {in} { set ret $in if {[string range $in 0 6]=="SQLITE_"} { From faff28a47840f9b95c7565ca5a328cbfef02634d Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 7 Feb 2023 10:58:39 +0000 Subject: [PATCH 33/51] Fix an assert() in sqlite3WalFindFrame() so that it matches the equivalent assert() on trunk. FossilOrigin-Name: f5bf91d9bf3910ab3dcf44c138084dfc26fb123edb8934bfdfcdd5c4afb67f2d --- manifest | 14 +++++++------- manifest.uuid | 2 +- src/wal.c | 2 +- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/manifest b/manifest index 05a28527d9..fec72887fe 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Disable\san\sinvalid\sassert().\s\sAdd\sthe\s"WAL2"\scompile-time\sproperty\sso\sthat\ntest\sscripts\scan\smore\seasily\sdetermine\sthat\sthey\sare\sdealing\swith\sa\nWAL2-capable\sbuild. -D 2023-02-06T19:00:53.294 +C Fix\san\sassert()\sin\ssqlite3WalFindFrame()\sso\sthat\sit\smatches\sthe\sequivalent\sassert()\son\strunk. +D 2023-02-07T10:58:39.543 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -705,7 +705,7 @@ F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf8 F src/vdbevtab.c aae4bd769410eb7e1d02c42613eec961d514459b1c3c1c63cfc84e92a137daac F src/vtab.c a39f6ed161f16a84ff445af9dba4776b2cf0898be08a33bee8e1128a66c0074b F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c b433980d0e97fa3469cb46136fd59194df638c684afc299b662b1114f3495af6 +F src/wal.c d9645dd2b606b5df93cc21b072cabab30ec9a20de826a6059cc08c73d889dd25 F src/wal.h d01234e828943e002040c22a7e017642962f9fd9b2dc142fa599769ae4e459e9 F src/walker.c f890a3298418d7cba3b69b8803594fdc484ea241206a8dfa99db6dd36f8cbb3b F src/where.c 3bbca705410258969d6d2e0c54b468de9674445d41b67714bb4fc3f1edef41d2 @@ -2059,8 +2059,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 44f8f33d4ab159476800142e763dfc6beb2c2c1d857c8eebb286498dec4004fd -R 4a29884f29ee8d2f8e34960946ff4a8b -U drh -Z 5908cdab3a7ef3394b937ba8240e9eba +P e93113259e05fb6b81ad1b4a460bd54168ff5c372b643d4845c1b1c87b7db204 +R 0cf0e867c1d072053e4612b80f259abc +U dan +Z 5cf3b3810b537b2d719a6a64e959fd37 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index a1815f8b78..56dbbc51f5 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -e93113259e05fb6b81ad1b4a460bd54168ff5c372b643d4845c1b1c87b7db204 \ No newline at end of file +f5bf91d9bf3910ab3dcf44c138084dfc26fb123edb8934bfdfcdd5c4afb67f2d \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 113c07c3d2..1dd95d69ec 100644 --- a/src/wal.c +++ b/src/wal.c @@ -3888,7 +3888,7 @@ int sqlite3WalFindFrame( /* This routine is only be called from within a read transaction. Or, ** sometimes, as part of a rollback that occurs after an error reaquiring ** a read-lock in walRestartLog(). */ - assert( pWal->readLock!=WAL_LOCK_NONE || pWal->writeLock ); + assert( pWal->readLock!=WAL_LOCK_NONE || pWal->lockError ); /* If this is a wal2 system, the client must have a partial-wal lock ** on wal file iApp. Or if it is a wal system, iApp==0 must be true. */ From e3d452a826f9010bbe8d14c301855ee073747fbd Mon Sep 17 00:00:00 2001 From: dan Date: Tue, 7 Feb 2023 11:19:20 +0000 Subject: [PATCH 34/51] Fix a dropped error code in wal.c. FossilOrigin-Name: 2537db6f6b9df00913b57e2ad0e4deef2040ce47b28f9a9939f42c0443d70ac9 --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/wal.c | 1 + 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/manifest b/manifest index fec72887fe..540b816de6 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\san\sassert()\sin\ssqlite3WalFindFrame()\sso\sthat\sit\smatches\sthe\sequivalent\sassert()\son\strunk. -D 2023-02-07T10:58:39.543 +C Fix\sa\sdropped\serror\scode\sin\swal.c. +D 2023-02-07T11:19:20.199 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -705,7 +705,7 @@ F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf8 F src/vdbevtab.c aae4bd769410eb7e1d02c42613eec961d514459b1c3c1c63cfc84e92a137daac F src/vtab.c a39f6ed161f16a84ff445af9dba4776b2cf0898be08a33bee8e1128a66c0074b F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c d9645dd2b606b5df93cc21b072cabab30ec9a20de826a6059cc08c73d889dd25 +F src/wal.c 610c58e925fb98902b6939af9c3c8b1d3ab1cfbc5dd1e19db540c4619171cd26 F src/wal.h d01234e828943e002040c22a7e017642962f9fd9b2dc142fa599769ae4e459e9 F src/walker.c f890a3298418d7cba3b69b8803594fdc484ea241206a8dfa99db6dd36f8cbb3b F src/where.c 3bbca705410258969d6d2e0c54b468de9674445d41b67714bb4fc3f1edef41d2 @@ -2059,8 +2059,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P e93113259e05fb6b81ad1b4a460bd54168ff5c372b643d4845c1b1c87b7db204 -R 0cf0e867c1d072053e4612b80f259abc +P f5bf91d9bf3910ab3dcf44c138084dfc26fb123edb8934bfdfcdd5c4afb67f2d +R f9886324061163f3fe48f6338ffe4fff U dan -Z 5cf3b3810b537b2d719a6a64e959fd37 +Z 88e782989721bd110052c1e06e2b2f03 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 56dbbc51f5..59e7028c8f 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -f5bf91d9bf3910ab3dcf44c138084dfc26fb123edb8934bfdfcdd5c4afb67f2d \ No newline at end of file +2537db6f6b9df00913b57e2ad0e4deef2040ce47b28f9a9939f42c0443d70ac9 \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index 1dd95d69ec..ee227ea3ff 100644 --- a/src/wal.c +++ b/src/wal.c @@ -3918,6 +3918,7 @@ int sqlite3WalFindFrame( )){ rc = walSearchWal(pWal, !iApp, pgno, &iRead); } + if( rc!=SQLITE_OK ) return rc; #if defined(SQLITE_TEST) && defined(SQLITE_DEBUG) if( iRead ){ From 5b39dd45e8b40715ff81ecd36ac0cb72e6088931 Mon Sep 17 00:00:00 2001 From: drh <> Date: Sat, 13 May 2023 15:00:47 +0000 Subject: [PATCH 35/51] Merge the latest 3.42.0 beta changes into the wal2 branch. FossilOrigin-Name: 771361e19e5b7f944cd2cfecb064b39175b892148fa877b763170744bca0b0cf --- manifest | 10 +++++----- manifest.uuid | 2 +- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/manifest b/manifest index fd0000778c..faaff78773 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Update\sthis\sbranch\swith\slatest\schanges\sfrom\strunk. -D 2023-05-04T14:31:17.113 +C Merge\sthe\slatest\s3.42.0\sbeta\schanges\sinto\sthe\swal2\sbranch. +D 2023-05-13T15:00:47.430 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -2082,8 +2082,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 0215dcc76d7b3a1dad10b8e6fd8b54f2ab5006a156db5cee992ab2dac98d9284 d3370d59cffb7ab982d6c620c93d22aa6a9dc786e1c4af95ca8d45ff0b9b7d6f +P 49777032f29517d23c8c7483536f8ba828e7000dc303415da6881cc458620be2 R 4c52475278c5ccef59c6ec833b8f6544 -U dan -Z f87e8c5c1721799925f2f360f33a9b2f +U drh +Z a8e26bfa96688ebb06d100956a914e43 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 2838b6d8b0..dbcd97d845 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -49777032f29517d23c8c7483536f8ba828e7000dc303415da6881cc458620be2 \ No newline at end of file +771361e19e5b7f944cd2cfecb064b39175b892148fa877b763170744bca0b0cf \ No newline at end of file From 1e6ee1929ca8497eb5ee6aab1b22626e28549ac5 Mon Sep 17 00:00:00 2001 From: dan Date: Thu, 10 Aug 2023 17:04:29 +0000 Subject: [PATCH 36/51] Mark test script wal2big.test as "TESTRUNNER: slow". FossilOrigin-Name: ae7f3bcb5babfa50084f5b4d3c5663489dbf306440706152642b7f0fb47fa1f5 --- manifest | 12 ++++++------ manifest.uuid | 2 +- test/wal2big.test | 2 ++ 3 files changed, 9 insertions(+), 7 deletions(-) diff --git a/manifest b/manifest index 0863026130..7f9b3beb95 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\slatest\strunk\schanges\sinto\sthis\sbranch. -D 2023-08-10T17:03:33.071 +C Mark\stest\sscript\swal2big.test\sas\s"TESTRUNNER:\sslow". +D 2023-08-10T17:04:29.698 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -1832,7 +1832,7 @@ F test/vtabdrop.test 65d4cf6722972e5499bdaf0c0d70ee3b8133944a4e4bc31862563f32a7e F test/vtabrhs1.test 9b5ecbc74a689500c33a4b2b36761f9bcc22fcc4e3f9d21066ee0c9c74cf5f6c F test/wal.test 07d61ee522bc203235a7979645d6c5d84f86882afad5b678532ee37b5b85b133 F test/wal2.test 31f6e2c404b9f2cdf9ca19b105a1742fdc19653c2c936da39e3658c617524046 -F test/wal2big.test 0b4ec526f9ca4bbabc355042c38045ae2e253fb46eb327bb7693d0122bc6968b +F test/wal2big.test 829141cbecdda4329db8fa38705424c4a73db72a06b9540b06811a825d330409 F test/wal2fault.test 2e8e60cacd5bcd451618aeffd05f676894d17202d3e2986e288d36e2c5993249 F test/wal2lock.test 0ef98d72dc6bcf7711dedd684760488400d9a9a6eec0dc5d3822060437793552 F test/wal2openclose.test 2b26be723ea7f4263c8d5d70b37efd1c359561a0526e39466c45fe8e6478daee @@ -2064,8 +2064,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P e6c066cae4ed5d698820c0c1ea8840773146e9d1a665551d8d51bb9df121cdb7 aa6de539c09faa320b68c63659e602107145c4263fa680d5b40fe4d7d7ac4534 -R f99f5fc942bd03ff500daadad5540cf1 +P 72f7807b34eee1fcfd94f72d27535e5273ed1f7ff3d464179a774b7c7241f5f5 +R 26af008ae6d036cbeb33f554ac503b68 U dan -Z ffac72e58a992aabc015a8c1dc573638 +Z 4fd2c88d6b68e9363568fe5cf8860077 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index b87b04233d..8aceda7a74 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -72f7807b34eee1fcfd94f72d27535e5273ed1f7ff3d464179a774b7c7241f5f5 \ No newline at end of file +ae7f3bcb5babfa50084f5b4d3c5663489dbf306440706152642b7f0fb47fa1f5 \ No newline at end of file diff --git a/test/wal2big.test b/test/wal2big.test index 53552e3639..77daab5af6 100644 --- a/test/wal2big.test +++ b/test/wal2big.test @@ -8,6 +8,8 @@ # May you share freely, never taking more than you give. # #*********************************************************************** +# TESTRUNNER: slow +# # This file implements regression tests for SQLite library. The # focus of this file is testing the operation of the library in # "PRAGMA journal_mode=WAL2" mode. From 7a1cc96197f3a3ffe5f5cc0b59f2cfdf6ad0d6a3 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 11 Aug 2023 11:23:35 +0000 Subject: [PATCH 37/51] Update test scripts specific to this branch so that they work on windows. FossilOrigin-Name: f0ca13edad058628e454cc895faac5751de77115c5fea62e98c29d9fdb6b0559 --- manifest | 14 ++++++------ manifest.uuid | 2 +- test/wal2recover.test | 49 ++++++++++++++++++++++++------------------ test/wal2recover2.test | 2 ++ 4 files changed, 38 insertions(+), 29 deletions(-) diff --git a/manifest b/manifest index 7f9b3beb95..770090f7c6 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Mark\stest\sscript\swal2big.test\sas\s"TESTRUNNER:\sslow". -D 2023-08-10T17:04:29.698 +C Update\stest\sscripts\sspecific\sto\sthis\sbranch\sso\sthat\sthey\swork\son\swindows. +D 2023-08-11T11:23:35.025 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -1836,8 +1836,8 @@ F test/wal2big.test 829141cbecdda4329db8fa38705424c4a73db72a06b9540b06811a825d33 F test/wal2fault.test 2e8e60cacd5bcd451618aeffd05f676894d17202d3e2986e288d36e2c5993249 F test/wal2lock.test 0ef98d72dc6bcf7711dedd684760488400d9a9a6eec0dc5d3822060437793552 F test/wal2openclose.test 2b26be723ea7f4263c8d5d70b37efd1c359561a0526e39466c45fe8e6478daee -F test/wal2recover.test ba8f4bc9397c838734619f9e759bd98b00e355347b3cf80a2e677610d231d5d8 -F test/wal2recover2.test 0c46afc759e4392a3c12fba17432b880c93a13bf4246d1be5101b00bae4c5f01 +F test/wal2recover.test 28b3fa0acb4e8725211a02803cd51116cd048841a876224f244af78039ba34fa +F test/wal2recover2.test af7e3bc08690918257da1bb37c94029a780bb17a65113c11b1a8bc9498fe82af F test/wal2recover3.test 4a91689e165a38bc401736e6518188c2b0ff4fa1566d1810b8867536db128177 F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal2rollback.test 23adc4a099b23f6aaea8b04fdca1c35861d887dd80f8be7da2d5273eb777e428 @@ -2064,8 +2064,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 72f7807b34eee1fcfd94f72d27535e5273ed1f7ff3d464179a774b7c7241f5f5 -R 26af008ae6d036cbeb33f554ac503b68 +P ae7f3bcb5babfa50084f5b4d3c5663489dbf306440706152642b7f0fb47fa1f5 +R 205333c8221bc744810ff95e8d8d4646 U dan -Z 4fd2c88d6b68e9363568fe5cf8860077 +Z 92ca64233ed526e0d8ac0035acb73143 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 8aceda7a74..7dfdbaa151 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -ae7f3bcb5babfa50084f5b4d3c5663489dbf306440706152642b7f0fb47fa1f5 \ No newline at end of file +f0ca13edad058628e454cc895faac5751de77115c5fea62e98c29d9fdb6b0559 \ No newline at end of file diff --git a/test/wal2recover.test b/test/wal2recover.test index 23e3cebda2..ef327f7f62 100644 --- a/test/wal2recover.test +++ b/test/wal2recover.test @@ -162,6 +162,7 @@ do_test 2.7 { PRAGMA integrity_check; } db2 } {0 0 ok} +db2 close #------------------------------------------------------------------------- # @@ -204,28 +205,33 @@ do_test 3.3 { list [file size test.db2] [file size test.db2-wal] [file size test.db2-wal2] } {5120 15752 23088} -do_test 3.4 { - set fd [open test.db2-shm] - fconfigure $fd -encoding binary -translation binary - set data [read $fd] - close $fd - - set fd [open test.db-shm w] - fconfigure $fd -encoding binary -translation binary - puts -nonewline $fd $data - close $fd - - execsql { - WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s where i < 10) - INSERT INTO t1 SELECT i, i, i FROM s; - SELECT count(*) FROM t1; - PRAGMA integrity_check; - } -} {211 ok} -do_test 3.5 { - list [file size test.db] [file size test.db-wal] [file size test.db-wal2] -} {5120 15752 18896} +if {$tcl_platform(platform)!="windows"} { + # These cannot be run under windows, as the *-shm file may not be read + # while it is locked by the database connection. + do_test 3.4 { + set fd [open test.db2-shm] + fconfigure $fd -encoding binary -translation binary + set data [read $fd] + close $fd + + set fd [open test.db-shm w] + fconfigure $fd -encoding binary -translation binary + puts -nonewline $fd $data + close $fd + + execsql { + WITH s(i) AS ( SELECT 1 UNION ALL SELECT i+1 FROM s where i < 10) + INSERT INTO t1 SELECT i, i, i FROM s; + SELECT count(*) FROM t1; + PRAGMA integrity_check; + } + } {211 ok} + + do_test 3.5 { + list [file size test.db] [file size test.db-wal] [file size test.db-wal2] + } {5120 15752 18896} +} #------------------------------------------------------------------------- # @@ -244,6 +250,7 @@ do_test 4.1 { } {1 {unable to open database file}} db close file delete test.db-wal2 +db2 close do_test 4.2 { sqlite3 db test.db diff --git a/test/wal2recover2.test b/test/wal2recover2.test index c3cadf8825..60ecfc28bf 100644 --- a/test/wal2recover2.test +++ b/test/wal2recover2.test @@ -163,6 +163,7 @@ do_test 1.4 { } [list $L $M] do_test 1.5 { + db2 close forcecopy test.db test.db2 forcecopy test.db-wal2 test.db2-wal forcecopy test.db-wal test.db2-wal2 @@ -173,6 +174,7 @@ do_test 1.5 { } db2 } [list $H $M] +db2 close foreach {tn file field} { 1 test.db2-wal salt0 2 test.db2-wal salt1 From 5267068d3dff99dc7b28241541cbf6a435558151 Mon Sep 17 00:00:00 2001 From: drh <> Date: Tue, 29 Aug 2023 20:19:42 +0000 Subject: [PATCH 38/51] (no comment) FossilOrigin-Name: 9e927eed2d9e379dd79ef82aa1e41102463c5654b8f4e343bb88bb0daf55c689 --- manifest | 13 +++++++------ manifest.uuid | 2 +- test/wal2recover2.test | 8 +++++++- 3 files changed, 15 insertions(+), 8 deletions(-) diff --git a/manifest b/manifest index c286f20053..807a581d93 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Version\s3.43.0\sfor\sthe\swal2\sbranch. -D 2023-08-24T13:19:33.412 +C (no\scomment) +D 2023-08-29T20:19:42.109 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -1877,7 +1877,7 @@ F test/wal2fault.test 2e8e60cacd5bcd451618aeffd05f676894d17202d3e2986e288d36e2c5 F test/wal2lock.test 0ef98d72dc6bcf7711dedd684760488400d9a9a6eec0dc5d3822060437793552 F test/wal2openclose.test 2b26be723ea7f4263c8d5d70b37efd1c359561a0526e39466c45fe8e6478daee F test/wal2recover.test 28b3fa0acb4e8725211a02803cd51116cd048841a876224f244af78039ba34fa -F test/wal2recover2.test fbbbf251639ab22eba0f0f09721a6824fadfb8f0214afdece2c5cf704e7fbc43 +F test/wal2recover2.test 3fe5d5932b16a3a38304a85bafed1f965faef40c230fd450838a0502e61343bb F test/wal2recover3.test 4a91689e165a38bc401736e6518188c2b0ff4fa1566d1810b8867536db128177 F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal2rollback.test 23adc4a099b23f6aaea8b04fdca1c35861d887dd80f8be7da2d5273eb777e428 @@ -2106,8 +2106,9 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 135bf72c6b5f436b11463dd40b3a73bcc610bf9c2dcbe6646dbedd0d9325f6a9 0f80b798b3f4b81a7bb4233c58294edd0f1156f36b6ecf5ab8e83631d468778c -R 33a2875f37184973dbcbb44062d51184 +P b5970fc4599d4fe81312e049420724b14e3d2c1de04769844fb1ab8d953170cd +Q +81361769e17b3d1589c5d0f9991a139963c6633d26bc72f189a69e0391d8f52f +R 8f5a9b50c5e3fa2227873acfebc2f61e U drh -Z dd8dd44d4178e619a8d6fbb3f97be763 +Z 403917b750ada434e8ac9e682d39ef1a # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index b7ae64ec23..48bc6826c4 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -b5970fc4599d4fe81312e049420724b14e3d2c1de04769844fb1ab8d953170cd \ No newline at end of file +9e927eed2d9e379dd79ef82aa1e41102463c5654b8f4e343bb88bb0daf55c689 \ No newline at end of file diff --git a/test/wal2recover2.test b/test/wal2recover2.test index 9b8058c1d7..c4da38e3a0 100644 --- a/test/wal2recover2.test +++ b/test/wal2recover2.test @@ -255,13 +255,19 @@ do_test 1.8.5 { fix_wal_cksums test.db-wal2 } {} +ifcapable oversize_cell_check { + set msg {database disk image is malformed} +} else { + set msg {malformed database schema (?)} +} + do_test 1.8.6 { forcecopy test.db test.db2 forcecopy test.db-wal test.db2-wal forcecopy test.db-wal2 test.db2-wal2 sqlite3 db2 test.db2 catchsql { SELECT * FROM sqlite_master } db2 -} {1 {database disk image is malformed}} +} [list 1 $msg] db2 close finish_test From 1fd73f2693582f16cb510f9004355d4ad7dd344c Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 8 Sep 2023 16:05:09 +0000 Subject: [PATCH 39/51] Fix an apparently harmless buffer-overrun that could occur on this branch when committing multi-file transactions in wal2 mode. FossilOrigin-Name: dde9fcbde16bf71c4ab708e2db769b9cefacd0d7bce8223a46bf1264ae7ddecd --- manifest | 15 +++++++-------- manifest.uuid | 2 +- src/vdbeaux.c | 3 ++- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/manifest b/manifest index 807a581d93..cb37978fbc 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C (no\scomment) -D 2023-08-29T20:19:42.109 +C Fix\san\sapparently\sharmless\sbuffer-overrun\sthat\scould\soccur\son\sthis\sbranch\swhen\scommitting\smulti-file\stransactions\sin\swal2\smode. +D 2023-09-08T16:05:09.694 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -760,7 +760,7 @@ F src/vdbe.c 11f1a00443991bd12bd0c97050b2614f7fd3e82b9cc0ffdb13d14d424d733e77 F src/vdbe.h 41485521f68e9437fdb7ec4a90f9d86ab294e9bb8281e33b235915e29122cfc0 F src/vdbeInt.h 949669dfd8a41550d27dcb905b494f2ccde9a2e6c1b0b04daa1227e2e74c2b2c F src/vdbeapi.c 37341acd781fda162e8cf4d9fc2eaea2febad3b365877a9d7233b8c6d0960d85 -F src/vdbeaux.c e3aa5c46827cd95e0fc4d0f302fa3e901ab5f07258fdbb42709eeef40f63018d +F src/vdbeaux.c ad53a8294930a1bf305e6cdedd8366c119f54c3986450c432be655b5c63fea65 F src/vdbeblob.c 2516697b3ee8154eb8915f29466fb5d4f1ae39ee8b755ea909cefaf57ec5e2ce F src/vdbemem.c 317b9f48708139db6239ade40c7980b4bc8233168383690d588dad6d8437f722 F src/vdbesort.c 0d40dca073c94e158ead752ef4225f4fee22dee84145e8c00ca2309afb489015 @@ -2106,9 +2106,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P b5970fc4599d4fe81312e049420724b14e3d2c1de04769844fb1ab8d953170cd -Q +81361769e17b3d1589c5d0f9991a139963c6633d26bc72f189a69e0391d8f52f -R 8f5a9b50c5e3fa2227873acfebc2f61e -U drh -Z 403917b750ada434e8ac9e682d39ef1a +P 9e927eed2d9e379dd79ef82aa1e41102463c5654b8f4e343bb88bb0daf55c689 +R 67cfe449cf756201852d8fa054eb97c8 +U dan +Z d3e8074aed95fbe7ee6ba6918a5a4be2 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 48bc6826c4..5c31a22276 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -9e927eed2d9e379dd79ef82aa1e41102463c5654b8f4e343bb88bb0daf55c689 \ No newline at end of file +dde9fcbde16bf71c4ab708e2db769b9cefacd0d7bce8223a46bf1264ae7ddecd \ No newline at end of file diff --git a/src/vdbeaux.c b/src/vdbeaux.c index 225c8d12c9..1fac0d38cc 100644 --- a/src/vdbeaux.c +++ b/src/vdbeaux.c @@ -2912,7 +2912,8 @@ static int vdbeCommit(sqlite3 *db, Vdbe *p){ /* OFF */ 0, /* TRUNCATE */ 1, /* MEMORY */ 0, - /* WAL */ 0 + /* WAL */ 0, + /* WAL2 */ 0 }; Pager *pPager; /* Pager associated with pBt */ needXcommit = 1; From 9d086cc56267bdb4e1d902cf35929a3281e08049 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 8 Sep 2023 16:49:45 +0000 Subject: [PATCH 40/51] Fix a sanitizer runtime error in wal2 code (that (1 << 31) cannot be represented as an int). FossilOrigin-Name: 79a920c036c91bbae737e4f4c5310052d26407fe7d23bcf95a2aa79ea3773a0f --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/wal.c | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/manifest b/manifest index cb37978fbc..6249d1698f 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\san\sapparently\sharmless\sbuffer-overrun\sthat\scould\soccur\son\sthis\sbranch\swhen\scommitting\smulti-file\stransactions\sin\swal2\smode. -D 2023-09-08T16:05:09.694 +C Fix\sa\ssanitizer\sruntime\serror\sin\swal2\scode\s(that\s(1\s<<\s31)\scannot\sbe\srepresented\sas\san\sint). +D 2023-09-08T16:49:45.275 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -768,7 +768,7 @@ F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf8 F src/vdbevtab.c 57fa8f56478e5b5cb558cb425e7878515e0a105c54f96f1d1bbf4b9433529254 F src/vtab.c 1ecf8c3745d29275688d583e12822fa984d421e0286b5ef50c137bc3bf6d7a64 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 7b887817194adfdf627df2c403a73c83fab833551009b876d3bcd7f0323859ed +F src/wal.c ffbca820a2ee9827269488cf8e1356ae66ad1e65da636da4eb4f3fcdf9d6ddd5 F src/wal.h 97b8a9903387401377b59507e86b93a148ef1ad4e5ce0f23659a12dcdce56af2 F src/walker.c 7c7ea0115345851c3da4e04e2e239a29983b61fb5b038b94eede6aba462640e2 F src/where.c b8917792f1e0dbfa28fb29e6cd3d560060d69667be0ba4c491cbc772363264f5 @@ -2106,8 +2106,8 @@ F vsixtest/vsixtest.tcl 6a9a6ab600c25a91a7acc6293828957a386a8a93 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 9e927eed2d9e379dd79ef82aa1e41102463c5654b8f4e343bb88bb0daf55c689 -R 67cfe449cf756201852d8fa054eb97c8 +P dde9fcbde16bf71c4ab708e2db769b9cefacd0d7bce8223a46bf1264ae7ddecd +R 65c1ace262788f5f1a8abc9e996b5c61 U dan -Z d3e8074aed95fbe7ee6ba6918a5a4be2 +Z c09d772004a60e1edd32f9310b5e97ea # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 5c31a22276..c43c13c196 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -dde9fcbde16bf71c4ab708e2db769b9cefacd0d7bce8223a46bf1264ae7ddecd \ No newline at end of file +79a920c036c91bbae737e4f4c5310052d26407fe7d23bcf95a2aa79ea3773a0f \ No newline at end of file diff --git a/src/wal.c b/src/wal.c index a3d8dbb97a..95b70cec77 100644 --- a/src/wal.c +++ b/src/wal.c @@ -581,8 +581,8 @@ static void walidxSetMxFrame(WalIndexHdr *pHdr, int iWal, u32 mxFrame){ #define walidxGetFile(pHdr) ((pHdr)->mxFrame2 >> 31) -#define walidxSetFile(pHdr, iWal) ( \ - (pHdr)->mxFrame2 = ((pHdr)->mxFrame2 & 0x7FFFFFFF) | ((iWal)<<31) \ +#define walidxSetFile(pHdr, iWal) ( \ + (pHdr)->mxFrame2 = ((pHdr)->mxFrame2 & 0x7FFFFFFF) | (((u32)(iWal))<<31) \ ) /* From 742f74ec19f11ec24d96c0aa1eb1e5e1b2df1eb2 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 9 Aug 2024 10:56:11 +0000 Subject: [PATCH 41/51] Fix some signed/unsigned compiler warnings on this branch. FossilOrigin-Name: 681d228023efb902b9039acf33600dd91f158e622d4dc0b434ed74f39ce0070a --- manifest | 21 +++++++++++---------- manifest.uuid | 2 +- src/wal.c | 8 ++++---- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/manifest b/manifest index 802e0d33f4..0acb61543e 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\sthe\slatest\strunk\senhancements\sinto\sthe\swal2\sbranch. -D 2024-08-01T00:38:59.125 +C Fix\ssome\ssigned/unsigned\scompiler\swarnings\son\sthis\sbranch. +D 2024-08-09T10:56:11.486 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -617,10 +617,10 @@ F ext/wasm/api/post-js-footer.js cd0a8ec768501d9bd45d325ab0442037fb0e33d1f3b4f08 F ext/wasm/api/post-js-header.js 04dc12c3edd666b64a1b4ef3b6690c88dcc653f26451fd4734472d8e29c1c122 F ext/wasm/api/pre-js.c-pp.js ad906703f7429590f2fbf5e6498513bf727a1a4f0ebfa057afb08161d7511219 F ext/wasm/api/sqlite3-api-cleanup.js d235ad237df6954145404305040991c72ef8b1881715d2a650dda7b3c2576d0e -F ext/wasm/api/sqlite3-api-glue.c-pp.js 54b32b5321105a72d6f3d3e8b77f28f162d0367b08c63184263d3f85f3d7dbed w ext/wasm/api/sqlite3-api-glue.js -F ext/wasm/api/sqlite3-api-oo1.c-pp.js f3a8e2004c6625d17946c11f2fb32008be78bc5207bf746fc77d59848813225f w ext/wasm/api/sqlite3-api-oo1.js +F ext/wasm/api/sqlite3-api-glue.c-pp.js 54b32b5321105a72d6f3d3e8b77f28f162d0367b08c63184263d3f85f3d7dbed +F ext/wasm/api/sqlite3-api-oo1.c-pp.js f3a8e2004c6625d17946c11f2fb32008be78bc5207bf746fc77d59848813225f F ext/wasm/api/sqlite3-api-prologue.js 6f1257e04885632ed9f44d43aba200b86e0bc16709ffdba29abbbeb1bc8e8b76 -F ext/wasm/api/sqlite3-api-worker1.c-pp.js 5cc22a3c0d52828cb32aad8691488719f47d27567e63e8bc8b832d74371c352d w ext/wasm/api/sqlite3-api-worker1.js +F ext/wasm/api/sqlite3-api-worker1.c-pp.js 5cc22a3c0d52828cb32aad8691488719f47d27567e63e8bc8b832d74371c352d F ext/wasm/api/sqlite3-license-version-header.js 0c807a421f0187e778dc1078f10d2994b915123c1223fe752b60afdcd1263f89 F ext/wasm/api/sqlite3-opfs-async-proxy.js e8f1df56e97a29004a95a2eddd26778f52c33b3e797d32d4b1b668a38e6493dd F ext/wasm/api/sqlite3-vfs-helper.c-pp.js 3f828cc66758acb40e9c5b4dcfd87fd478a14c8fb7f0630264e6c7fa0e57515d @@ -844,7 +844,7 @@ F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf8 F src/vdbevtab.c fc46b9cbd759dc013f0b3724549cc0d71379183c667df3a5988f7e2f1bd485f3 F src/vtab.c 5fb499d20494b7eecaadb7584634af9afcb374cb0524912b475fcb1712458a1b F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 2586d8f3503ed7a59cb3becb380bd90a5ca218c2d331d3d33e3bed58d26aa1ae +F src/wal.c 7fe7aa272006e5070b31109757bb45d9e387d4df0c58a7ee56614764aaa39651 F src/wal.h 97b8a9903387401377b59507e86b93a148ef1ad4e5ce0f23659a12dcdce56af2 F src/walker.c 7c7ea0115345851c3da4e04e2e239a29983b61fb5b038b94eede6aba462640e2 F src/where.c d87a4160e26a7a96a2f7ca283b147b1b283b54ba545c46acb14cfcc6ec37ae9e @@ -2214,8 +2214,9 @@ F vsixtest/vsixtest.tcl 6195aba1f12a5e10efc2b8c0009532167be5e301abe5b31385638080 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P fdc0e1480a941d494e556331544cb40b2a997e8709e2c7fab1bed572007eb88d ea9d88f9ca3399bca83bf03893689a927b73e481604b94527e42de43f103eb46 -R e1014a2acf54aea568d91af6ca83770a -U drh -Z bd8b7db6c09919a8f9573093758c93b8 +P eac582056fcf6210bf90576cce30c087503b864dc8d558f2998f04b3d1bfb569 +Q +d615d5291871ba120d916c4722e6067e083f394c6a7f398059442b36c4500718 +R b7562e9c3a0decaefc4ce79c3da81d61 +U dan +Z 2f95d8eb17e333c43b562f53602b1258 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 92881fe0e2..3d0bdfa188 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -eac582056fcf6210bf90576cce30c087503b864dc8d558f2998f04b3d1bfb569 +681d228023efb902b9039acf33600dd91f158e622d4dc0b434ed74f39ce0070a diff --git a/src/wal.c b/src/wal.c index 17d63439ad..a5625cb837 100644 --- a/src/wal.c +++ b/src/wal.c @@ -579,7 +579,7 @@ static void walidxSetMxFrame(WalIndexHdr *pHdr, int iWal, u32 mxFrame){ assert( walidxGetMxFrame(pHdr, iWal)==mxFrame ); } -#define walidxGetFile(pHdr) ((pHdr)->mxFrame2 >> 31) +#define walidxGetFile(pHdr) (int)((pHdr)->mxFrame2 >> 31) #define walidxSetFile(pHdr, iWal) ( \ (pHdr)->mxFrame2 = ((pHdr)->mxFrame2 & 0x7FFFFFFF) | (((u32)(iWal))<<31) \ @@ -4533,7 +4533,7 @@ void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){ aWalData[0] = walidxGetMxFrame(&pWal->hdr, iWal); aWalData[1] = pWal->hdr.aFrameCksum[0]; aWalData[2] = pWal->hdr.aFrameCksum[1]; - aWalData[3] = isWalMode2(pWal) ? iWal : pWal->nCkpt; + aWalData[3] = isWalMode2(pWal) ? (u32)iWal : pWal->nCkpt; } /* @@ -4545,7 +4545,7 @@ void sqlite3WalSavepoint(Wal *pWal, u32 *aWalData){ int sqlite3WalSavepointUndo(Wal *pWal, u32 *aWalData){ int rc = SQLITE_OK; int iWal = walidxGetFile(&pWal->hdr); - int iCmp = isWalMode2(pWal) ? iWal : pWal->nCkpt; + u32 iCmp = isWalMode2(pWal) ? (u32)iWal : pWal->nCkpt; assert( pWal->writeLock ); assert( isWalMode2(pWal) || iWal==0 ); @@ -4590,7 +4590,7 @@ static int walRestartLog(Wal *pWal){ if( isWalMode2(pWal) ){ int iApp = walidxGetFile(&pWal->hdr); - int nWalSize = WAL_DEFAULT_WALSIZE; + u32 nWalSize = WAL_DEFAULT_WALSIZE; if( pWal->mxWalSize>0 ){ nWalSize = (pWal->mxWalSize-WAL_HDRSIZE+pWal->szPage+WAL_FRAME_HDRSIZE-1) / (pWal->szPage+WAL_FRAME_HDRSIZE); From aa4530485931d15913856002dedc101502f59695 Mon Sep 17 00:00:00 2001 From: dan Date: Mon, 30 Sep 2024 17:53:47 +0000 Subject: [PATCH 42/51] Fix a usan warning in test_hexio.c on this branch. FossilOrigin-Name: 59fba0288d8fb68f9b6e4936c09ae9169bc002bfe6548354504563a674000c20 --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/test_hexio.c | 4 ++-- 3 files changed, 9 insertions(+), 9 deletions(-) diff --git a/manifest b/manifest index a905d8f55b..1fde4e10a9 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\sthe\slatest\strunk\senhancements,\sincluding\sfix\sfor\sassert()\sfailure\sin\sfts5,\sinto\sthe\swal2\sbranch. -D 2024-09-30T17:32:08.647 +C Fix\sa\susan\swarning\sin\stest_hexio.c\son\sthis\sbranch. +D 2024-09-30T17:53:47.829 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md df5091916dbb40e6e9686186587125e1b2ff51f022cc334e886c19a0e9982724 @@ -800,7 +800,7 @@ F src/test_demovfs.c 3efa2adf4f21e10d95521721687d5ca047aea91fa62dd8cc22ac9e5a9c9 F src/test_devsym.c 649434ed34d0b03fbd5a6b42df80f0f9a7e53f94dd1710aad5dd8831e91c4e86 F src/test_fs.c c411c40baba679536fc34e2679349f59d8225570aed3488b5b3ef1908525a3d5 F src/test_func.c 8c0e89192f70fac307822d1ac2911ee51751288780b3db0c5ab5ca75fa0fe851 -F src/test_hexio.c 35704e7db83fe1a0e1a014bacb17de97d7d1e89af3356a9da04e37cdaa087c09 +F src/test_hexio.c 7449504e4bde876ba91b202617a9228c7c8c2e7bd8b957302f3803ac0e9e353c F src/test_init.c 17313332d58e90defc527129d5eda4a08bd6b6e8de7207a231523c8d98fb445e F src/test_intarray.c e4216aadee9df2de7d1aee7e70f6b22c80ee79ece72a63d57105db74217639e5 F src/test_intarray.h 6c3534641108cd1bea517a8e117dcba237081310a29a4c35bd2190caa8972293 @@ -2227,8 +2227,8 @@ F vsixtest/vsixtest.tcl 6195aba1f12a5e10efc2b8c0009532167be5e301abe5b31385638080 F vsixtest/vsixtest.vcxproj.data 2ed517e100c66dc455b492e1a33350c1b20fbcdc F vsixtest/vsixtest.vcxproj.filters 37e51ffedcdb064aad6ff33b6148725226cd608e F vsixtest/vsixtest_TemporaryKey.pfx e5b1b036facdb453873e7084e1cae9102ccc67a0 -P 0f9e2fceecdb7fad0a2528a4244a786d17274d11b83f336d6012420675a2e4cd 350c6e75ce3c1e81458d1baa73045df489284206e8b279ab3c2f5e3d011c262a -R cb26b1a66669f9ba73a72b529dcc5094 +P ab6356181274016571124989bc10e16256666567bb284612a0a689cf18a50adb +R b05282f64f82f52e60568844e5597fe7 U dan -Z 6f705badf675793272d3710bf9fc6490 +Z 7723f16e0d8ec4b8fecc8e3015d59289 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index acd7fc61f6..91c0e60e6f 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -ab6356181274016571124989bc10e16256666567bb284612a0a689cf18a50adb +59fba0288d8fb68f9b6e4936c09ae9169bc002bfe6548354504563a674000c20 diff --git a/src/test_hexio.c b/src/test_hexio.c index 47c9d8fd2b..1a21e89aa0 100644 --- a/src/test_hexio.c +++ b/src/test_hexio.c @@ -232,9 +232,9 @@ static int SQLITE_TCLAPI hexio_get_int( } sqlite3_free(aOut); if( bLittle ){ - val = (aNum[3]<<24) | (aNum[2]<<16) | (aNum[1]<<8) | aNum[0]; + val = (int)((u32)aNum[3]<<24) | (aNum[2]<<16) | (aNum[1]<<8) | aNum[0]; }else{ - val = (aNum[0]<<24) | (aNum[1]<<16) | (aNum[2]<<8) | aNum[3]; + val = (int)((u32)aNum[0]<<24) | (aNum[1]<<16) | (aNum[2]<<8) | aNum[3]; } Tcl_SetObjResult(interp, Tcl_NewIntObj(val)); return TCL_OK; From ea98a50afecf1f9add70f1a2a17d2e0457615962 Mon Sep 17 00:00:00 2001 From: drh <> Date: Mon, 18 Nov 2024 18:03:27 +0000 Subject: [PATCH 43/51] Attempt to reduce divergence from trunk. FossilOrigin-Name: ffe9f9ccd72f022a8388bd6426abf4b9f00d52a18206d8c3345d9dd47d4ddd71 --- ...NS.fiddle => EXPORTED_FUNCTIONS.fiddle.in} | 11 +- ext/wasm/EXPORTED_FUNCTIONS.sqlite3-core | 155 ++++++++++++++++++ ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api | 72 -------- ext/wasm/api/sqlite3-worker1-promiser.c-pp.js | 135 ++++++++++++--- manifest | 26 +-- manifest.uuid | 2 +- src/parse.y | 11 +- test/corruptN.test | 9 - test/wal_common.tcl | 40 +++++ 9 files changed, 334 insertions(+), 127 deletions(-) rename ext/wasm/{EXPORTED_FUNCTIONS.fiddle => EXPORTED_FUNCTIONS.fiddle.in} (61%) create mode 100644 ext/wasm/EXPORTED_FUNCTIONS.sqlite3-core delete mode 100644 ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api diff --git a/ext/wasm/EXPORTED_FUNCTIONS.fiddle b/ext/wasm/EXPORTED_FUNCTIONS.fiddle.in similarity index 61% rename from ext/wasm/EXPORTED_FUNCTIONS.fiddle rename to ext/wasm/EXPORTED_FUNCTIONS.fiddle.in index b96ce4e67c..103704df10 100644 --- a/ext/wasm/EXPORTED_FUNCTIONS.fiddle +++ b/ext/wasm/EXPORTED_FUNCTIONS.fiddle.in @@ -1,7 +1,10 @@ -_fiddle_exec -_fiddle_interrupt -_fiddle_experiment -_fiddle_the_db _fiddle_db_arg _fiddle_db_filename +_fiddle_exec +_fiddle_experiment +_fiddle_interrupt +_fiddle_main _fiddle_reset_db +_fiddle_db_handle +_fiddle_db_vfs +_fiddle_export_db diff --git a/ext/wasm/EXPORTED_FUNCTIONS.sqlite3-core b/ext/wasm/EXPORTED_FUNCTIONS.sqlite3-core new file mode 100644 index 0000000000..2578002ce4 --- /dev/null +++ b/ext/wasm/EXPORTED_FUNCTIONS.sqlite3-core @@ -0,0 +1,155 @@ +_malloc +_free +_realloc +_sqlite3_aggregate_context +_sqlite3_auto_extension +_sqlite3_bind_blob +_sqlite3_bind_double +_sqlite3_bind_int +_sqlite3_bind_int64 +_sqlite3_bind_null +_sqlite3_bind_parameter_count +_sqlite3_bind_parameter_index +_sqlite3_bind_parameter_name +_sqlite3_bind_pointer +_sqlite3_bind_text +_sqlite3_busy_handler +_sqlite3_busy_timeout +_sqlite3_cancel_auto_extension +_sqlite3_changes +_sqlite3_changes64 +_sqlite3_clear_bindings +_sqlite3_close_v2 +_sqlite3_collation_needed +_sqlite3_column_blob +_sqlite3_column_bytes +_sqlite3_column_count +_sqlite3_column_decltype +_sqlite3_column_double +_sqlite3_column_int +_sqlite3_column_int64 +_sqlite3_column_name +_sqlite3_column_text +_sqlite3_column_type +_sqlite3_column_value +_sqlite3_commit_hook +_sqlite3_compileoption_get +_sqlite3_compileoption_used +_sqlite3_complete +_sqlite3_context_db_handle +_sqlite3_create_collation +_sqlite3_create_collation_v2 +_sqlite3_create_function +_sqlite3_create_function_v2 +_sqlite3_data_count +_sqlite3_db_filename +_sqlite3_db_handle +_sqlite3_db_name +_sqlite3_db_readonly +_sqlite3_db_status +_sqlite3_deserialize +_sqlite3_errcode +_sqlite3_errmsg +_sqlite3_error_offset +_sqlite3_errstr +_sqlite3_exec +_sqlite3_expanded_sql +_sqlite3_extended_errcode +_sqlite3_extended_result_codes +_sqlite3_file_control +_sqlite3_finalize +_sqlite3_free +_sqlite3_get_auxdata +_sqlite3_get_autocommit +_sqlite3_initialize +_sqlite3_interrupt +_sqlite3_is_interrupted +_sqlite3_keyword_count +_sqlite3_keyword_name +_sqlite3_keyword_check +_sqlite3_last_insert_rowid +_sqlite3_libversion +_sqlite3_libversion_number +_sqlite3_limit +_sqlite3_malloc +_sqlite3_malloc64 +_sqlite3_msize +_sqlite3_open +_sqlite3_open_v2 +_sqlite3_overload_function +_sqlite3_prepare_v2 +_sqlite3_prepare_v3 +_sqlite3_randomness +_sqlite3_realloc +_sqlite3_realloc64 +_sqlite3_reset +_sqlite3_reset_auto_extension +_sqlite3_result_blob +_sqlite3_result_double +_sqlite3_result_error +_sqlite3_result_error_code +_sqlite3_result_error_nomem +_sqlite3_result_error_toobig +_sqlite3_result_int +_sqlite3_result_int64 +_sqlite3_result_null +_sqlite3_result_pointer +_sqlite3_result_subtype +_sqlite3_result_text +_sqlite3_result_zeroblob +_sqlite3_result_zeroblob64 +_sqlite3_rollback_hook +_sqlite3_serialize +_sqlite3_set_auxdata +_sqlite3_set_last_insert_rowid +_sqlite3_shutdown +_sqlite3_sourceid +_sqlite3_sql +_sqlite3_status +_sqlite3_status64 +_sqlite3_step +_sqlite3_stmt_busy +_sqlite3_stmt_explain +_sqlite3_stmt_isexplain +_sqlite3_stmt_readonly +_sqlite3_stmt_status +_sqlite3_strglob +_sqlite3_stricmp +_sqlite3_strlike +_sqlite3_strnicmp +_sqlite3_table_column_metadata +_sqlite3_total_changes +_sqlite3_total_changes64 +_sqlite3_trace_v2 +_sqlite3_txn_state +_sqlite3_update_hook +_sqlite3_uri_boolean +_sqlite3_uri_int64 +_sqlite3_uri_key +_sqlite3_uri_parameter +_sqlite3_user_data +_sqlite3_value_blob +_sqlite3_value_bytes +_sqlite3_value_double +_sqlite3_value_dup +_sqlite3_value_free +_sqlite3_value_frombind +_sqlite3_value_int +_sqlite3_value_int64 +_sqlite3_value_nochange +_sqlite3_value_numeric_type +_sqlite3_value_pointer +_sqlite3_value_subtype +_sqlite3_value_text +_sqlite3_value_type +_sqlite3_vfs_find +_sqlite3_vfs_register +_sqlite3_vfs_unregister +_sqlite3_vtab_collation +_sqlite3_vtab_distinct +_sqlite3_vtab_in +_sqlite3_vtab_in_first +_sqlite3_vtab_in_next +_sqlite3_vtab_nochange +_sqlite3_vtab_on_conflict +_sqlite3_vtab_rhs_value diff --git a/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api deleted file mode 100644 index 8f103c7c0b..0000000000 --- a/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api +++ /dev/null @@ -1,72 +0,0 @@ -_sqlite3_bind_blob -_sqlite3_bind_double -_sqlite3_bind_int -_sqlite3_bind_int64 -_sqlite3_bind_null -_sqlite3_bind_parameter_count -_sqlite3_bind_parameter_index -_sqlite3_bind_text -_sqlite3_changes -_sqlite3_changes64 -_sqlite3_clear_bindings -_sqlite3_close_v2 -_sqlite3_column_blob -_sqlite3_column_bytes -_sqlite3_column_count -_sqlite3_column_count -_sqlite3_column_double -_sqlite3_column_int -_sqlite3_column_int64 -_sqlite3_column_name -_sqlite3_column_text -_sqlite3_column_type -_sqlite3_compileoption_get -_sqlite3_compileoption_used -_sqlite3_create_function_v2 -_sqlite3_data_count -_sqlite3_db_filename -_sqlite3_db_name -_sqlite3_errmsg -_sqlite3_error_offset -_sqlite3_errstr -_sqlite3_exec -_sqlite3_expanded_sql -_sqlite3_extended_errcode -_sqlite3_extended_result_codes -_sqlite3_finalize -_sqlite3_initialize -_sqlite3_interrupt -_sqlite3_libversion -_sqlite3_libversion_number -_sqlite3_open -_sqlite3_open_v2 -_sqlite3_prepare_v2 -_sqlite3_prepare_v3 -_sqlite3_reset -_sqlite3_result_blob -_sqlite3_result_double -_sqlite3_result_error -_sqlite3_result_error_code -_sqlite3_result_error_nomem -_sqlite3_result_error_toobig -_sqlite3_result_int -_sqlite3_result_null -_sqlite3_result_text -_sqlite3_sourceid -_sqlite3_sql -_sqlite3_step -_sqlite3_strglob -_sqlite3_strlike -_sqlite3_total_changes -_sqlite3_total_changes64 -_sqlite3_value_blob -_sqlite3_value_bytes -_sqlite3_value_double -_sqlite3_value_text -_sqlite3_value_type -_sqlite3_vfs_find -_sqlite3_vfs_register -_sqlite3_wasm_db_error -_sqlite3_wasm_enum_json -_malloc -_free diff --git a/ext/wasm/api/sqlite3-worker1-promiser.c-pp.js b/ext/wasm/api/sqlite3-worker1-promiser.c-pp.js index 1689d34802..55e497ead5 100644 --- a/ext/wasm/api/sqlite3-worker1-promiser.c-pp.js +++ b/ext/wasm/api/sqlite3-worker1-promiser.c-pp.js @@ -1,3 +1,4 @@ +//#ifnot omit-oo1 /* 2022-08-24 @@ -41,9 +42,13 @@ - `onready` (optional, but...): this callback is called with no arguments when the worker fires its initial 'sqlite3-api'/'worker1-ready' message, which it does when - sqlite3.initWorker1API() completes its initialization. This is - the simplest way to tell the worker to kick off work at the - earliest opportunity. + sqlite3.initWorker1API() completes its initialization. This is the + simplest way to tell the worker to kick off work at the earliest + opportunity, and the only way to know when the worker module has + completed loading. The irony of using a callback for this, instead + of returning a promise from sqlite3Worker1Promiser() is not lost on + the developers: see sqlite3Worker1Promiser.v2() which uses a + Promise instead. - `onunhandled` (optional): a callback which gets passed the message event object for any worker.onmessage() events which @@ -114,7 +119,7 @@ by all client code except that which tests this API. The `row` property contains the row result in the form implied by the `rowMode` option (defaulting to `'array'`). The `rowNumber` is a - 1-based integer value incremented by 1 on each call into th + 1-based integer value incremented by 1 on each call into the callback. At the end of the result set, the same event is fired with @@ -122,8 +127,17 @@ the end of the result set has been reached. Note that the rows arrive via worker-posted messages, with all the implications of that. + + Notable shortcomings: + + - This API was not designed with ES6 modules in mind. Neither Firefox + nor Safari support, as of March 2023, the {type:"module"} flag to the + Worker constructor, so that particular usage is not something we're going + to target for the time being: + + https://developer.mozilla.org/en-US/docs/Web/API/Worker/Worker */ -self.sqlite3Worker1Promiser = function callee(config = callee.defaultConfig){ +globalThis.sqlite3Worker1Promiser = function callee(config = callee.defaultConfig){ // Inspired by: https://stackoverflow.com/a/52439530 if(1===arguments.length && 'function'===typeof arguments[0]){ const f = config; @@ -146,6 +160,7 @@ self.sqlite3Worker1Promiser = function callee(config = callee.defaultConfig){ if(!config.worker) config.worker = callee.defaultConfig.worker; if('function'===typeof config.worker) config.worker = config.worker(); let dbId; + let promiserFunc; config.worker.onmessage = function(ev){ ev = ev.data; debug('worker1.onmessage',ev); @@ -153,14 +168,14 @@ self.sqlite3Worker1Promiser = function callee(config = callee.defaultConfig){ if(!msgHandler){ if(ev && 'sqlite3-api'===ev.type && 'worker1-ready'===ev.result) { /*fired one time when the Worker1 API initializes*/ - if(config.onready) config.onready(); + if(config.onready) config.onready(promiserFunc); return; } msgHandler = handlerMap[ev.type] /* check for exec per-row callback */; if(msgHandler && msgHandler.onrow){ msgHandler.onrow(ev); return; - } + } if(config.onunhandled) config.onunhandled(arguments[0]); else err("sqlite3Worker1Promiser() unhandled worker message:",ev); return; @@ -182,19 +197,19 @@ self.sqlite3Worker1Promiser = function callee(config = callee.defaultConfig){ try {msgHandler.resolve(ev)} catch(e){msgHandler.reject(e)} }/*worker.onmessage()*/; - return function(/*(msgType, msgArgs) || (msgEnvelope)*/){ + return promiserFunc = function(/*(msgType, msgArgs) || (msgEnvelope)*/){ let msg; if(1===arguments.length){ msg = arguments[0]; }else if(2===arguments.length){ - msg = { - type: arguments[0], - args: arguments[1] - }; + msg = Object.create(null); + msg.type = arguments[0]; + msg.args = arguments[1]; + msg.dbId = msg.args.dbId; }else{ - toss("Invalid arugments for sqlite3Worker1Promiser()-created factory."); + toss("Invalid arguments for sqlite3Worker1Promiser()-created factory."); } - if(!msg.dbId) msg.dbId = dbId; + if(!msg.dbId && msg.type!=='open') msg.dbId = dbId; msg.messageId = genMsgId(msg); msg.departureTime = performance.now(); const proxy = Object.create(null); @@ -236,28 +251,96 @@ self.sqlite3Worker1Promiser = function callee(config = callee.defaultConfig){ return p; }; }/*sqlite3Worker1Promiser()*/; -self.sqlite3Worker1Promiser.defaultConfig = { + +globalThis.sqlite3Worker1Promiser.defaultConfig = { worker: function(){ -//#if target=es6-bundler-friendly - return new Worker("sqlite3-worker1.js"); +//#if target=es6-module + return new Worker(new URL("sqlite3-worker1-bundler-friendly.mjs", import.meta.url),{ + type: 'module' + }); //#else let theJs = "sqlite3-worker1.js"; if(this.currentScript){ const src = this.currentScript.src.split('/'); src.pop(); theJs = src.join('/')+'/' + theJs; - //console.warn("promiser currentScript, theJs =",this.currentScript,theJs); - }else{ - //console.warn("promiser self.location =",self.location); - const urlParams = new URL(self.location.href).searchParams; + //sqlite3.config.warn("promiser currentScript, theJs =",this.currentScript,theJs); + }else if(globalThis.location){ + //sqlite3.config.warn("promiser globalThis.location =",globalThis.location); + const urlParams = new URL(globalThis.location.href).searchParams; if(urlParams.has('sqlite3.dir')){ theJs = urlParams.get('sqlite3.dir') + '/' + theJs; } } - return new Worker(theJs + self.location.search); + return new Worker(theJs + globalThis.location.search); +//#endif + } +//#ifnot target=es6-module + .bind({ + currentScript: globalThis?.document?.currentScript + }) //#endif - }.bind({ - currentScript: self?.document?.currentScript - }), + , onerror: (...args)=>console.error('worker1 promiser error',...args) -}; +}/*defaultConfig*/; + +/** + sqlite3Worker1Promiser.v2(), added in 3.46, works identically to + sqlite3Worker1Promiser() except that it returns a Promise instead + of relying an an onready callback in the config object. The Promise + resolves to the same factory function which + sqlite3Worker1Promiser() returns. + + If config is-a function or is an object which contains an onready + function, that function is replaced by a proxy which will resolve + after calling the original function and will reject if that + function throws. +*/ +sqlite3Worker1Promiser.v2 = function(config){ + let oldFunc; + if( 'function' == typeof config ){ + oldFunc = config; + config = {}; + }else if('function'===typeof config?.onready){ + oldFunc = config.onready; + delete config.onready; + } + const promiseProxy = Object.create(null); + config = Object.assign((config || Object.create(null)),{ + onready: async function(func){ + try { + if( oldFunc ) await oldFunc(func); + promiseProxy.resolve(func); + } + catch(e){promiseProxy.reject(e)} + } + }); + const p = new Promise(function(resolve,reject){ + promiseProxy.resolve = resolve; + promiseProxy.reject = reject; + }); + try{ + this.original(config); + }catch(e){ + promiseProxy.reject(e); + } + return p; +}.bind({ + /* We do this because clients are + recommended to delete globalThis.sqlite3Worker1Promiser. */ + original: sqlite3Worker1Promiser +}); + +//#if target=es6-module +/** + When built as a module, we export sqlite3Worker1Promiser.v2() + instead of sqlite3Worker1Promise() because (A) its interface is more + conventional for ESM usage and (B) the ESM option export option for + this API did not exist until v2 was created, so there's no backwards + incompatibility. +*/ +export default sqlite3Worker1Promiser.v2; +//#endif /* target=es6-module */ +//#else +/* Built with the omit-oo1 flag. */ +//#endif ifnot omit-oo1 diff --git a/manifest b/manifest index 7034208174..32b665d139 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\sthe\slatest\strunk\schanges\sinto\sthe\swal2\sbranch. -D 2024-11-18T15:30:58.804 +C Attempt\sto\sreduce\sdivergence\sfrom\strunk. +D 2024-11-18T18:03:27.562 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md e108e1e69ae8e8a59e93c455654b8ac9356a11720d3345df2a4743e9590fb20d @@ -49,7 +49,7 @@ F autosetup/cc-shared.tcl 4f024e94a47f427ba61de1739f6381ef0080210f9fae89112d5c1d F autosetup/cc.tcl c0fcc50ca91deff8741e449ddad05bcd08268bc31177e613a6343bbd1fd3e45f F autosetup/jimsh0.c d40e381ea4526a067590e7b91bd4b2efa6d4980d286f908054c647b3df4aee14 F autosetup/pkg-config.tcl 4e635bf39022ff65e0d5434339dd41503ea48fc53822c9c5bde88b02d3d952ba -F autosetup/proj.tcl 96fe16b87c9feb9c1cf2682280f678c659bc52c09fca5de02afc2f7ec5bfb154 w autosetup/hwaci-common.tcl +F autosetup/proj.tcl 96fe16b87c9feb9c1cf2682280f678c659bc52c09fca5de02afc2f7ec5bfb154 F autosetup/system.tcl 51d4be76cd9a9074704b584e5c9cbba616202c8468cf9ba8a4f8294a7ab1dba9 F configure 9a00b21dfd13757bbfb8d89b30660a89ec1f8f3a79402b8f9f9b6fc475c3303a x F contrib/sqlitecon.tcl 210a913ad63f9f991070821e599d600bd913e0ad @@ -615,7 +615,8 @@ F ext/session/sessionwor.test 6fd9a2256442cebde5b2284936ae9e0d54bde692d0f5fd009e F ext/session/sqlite3session.c 3d0a7f0f7a1c946e01818c716a55a40ae30542a29a9045cb05daf7fb658cdafa F ext/session/sqlite3session.h 683ccbf16e2c2521661fc4c1cf918ce57002039efbcabcd8097fa4bca569104b F ext/session/test_session.c aa29abdcc9011ac02f4fa38e8ede226106eaeee7c3ea7d8b2b999a124e0c368c -F ext/wasm/EXPORTED_FUNCTIONS.fiddle 7fb73f7150ab79d83bb45a67d257553c905c78cd3d693101699243f36c5ae6c3 +F ext/wasm/EXPORTED_FUNCTIONS.fiddle.in 27450c8b8c70875a260aca55435ec927068b34cef801a96205adb81bdcefc65c w ext/wasm/EXPORTED_FUNCTION.fiddle.in +F ext/wasm/EXPORTED_FUNCTIONS.sqlite3-core 2bcbbfe3b95c043ed6037e2708a2ee078d212dd1612c364f93588d8dc97300fe F ext/wasm/GNUmakefile 311aa0d5edc7006409962cc77cc26560d92f9be69c2c4302e8bbc68189fd02db F ext/wasm/README-dist.txt 6382cb9548076fca472fb3330bbdba3a55c1ea0b180ff9253f084f07ff383576 F ext/wasm/README.md a8a2962c3aebdf8d2104a9102e336c5554e78fc6072746e5daf9c61514e7d193 @@ -624,7 +625,6 @@ F ext/wasm/SQLTester/SQLTester.mjs 66e1adc3d79467b68e3e40614fd42c1a577c7e219ec09 F ext/wasm/SQLTester/SQLTester.run.mjs 57f2adb33f43f2784abbf8026c1bfd049d8013af1998e7dcb8b50c89ffc332e0 F ext/wasm/SQLTester/index.html 64f3435084c7d6139b08d1f2a713828a73f68de2ae6a3112cbb5980d991ba06f F ext/wasm/SQLTester/touint8array.c 2d5ece04ec1393a6a60c4bf96385bda5e1a10ad49f3038b96460fc5e5aa7e536 -F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-api c5eaceabb9e759aaae7d3101a4a3e542f96ab2c99d89a80ce20ec18c23115f33 F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-extras fe40d6d758646e38f8b15f709044951e10884214f5453d35502100179c388c13 F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-see fb29e62082a658f0d81102488414d422c393c4b20cc2f685b216bc566237957b F ext/wasm/api/EXPORTED_RUNTIME_METHODS.sqlite3-api 1ec3c73e7d66e95529c3c64ac3de2470b0e9e7fbf7a5b41261c367cf4f1b7287 @@ -646,7 +646,7 @@ F ext/wasm/api/sqlite3-vfs-opfs-sahpool.c-pp.js bb5e96cd0fd6e1e54538256433f1c60a F ext/wasm/api/sqlite3-vfs-opfs.c-pp.js 9b86ca2d8276cf919fbc9ba2a10e9786033b64f92c2db844d951804dee6c4b4e F ext/wasm/api/sqlite3-vtab-helper.c-pp.js e809739d71e8b35dfe1b55d24d91f02d04239e6aef7ca1ea92a15a29e704f616 F ext/wasm/api/sqlite3-wasm.c 83f5e9f998e9fa4261eb84e9f092210e3ffe03895119f5ded0429eb34ab9d2be -F ext/wasm/api/sqlite3-worker1-promiser.c-pp.js f10c3ecd9df06f6320073c2ce230a7ed7c56034d8b88c1e57095f2a97faf423a +F ext/wasm/api/sqlite3-worker1-promiser.c-pp.js 46f303ba8ddd1b2f0a391798837beddfa72e8c897038c8047eda49ce7d5ed46b F ext/wasm/api/sqlite3-worker1.c-pp.js 5e8706c2c4af2a57fbcdc02f4e7ef79869971bc21bb8ede777687786ce1c92d5 F ext/wasm/batch-runner-sahpool.html e9a38fdeb36a13eac7b50241dfe7ae066fe3f51f5c0b0151e7baee5fce0d07a7 F ext/wasm/batch-runner-sahpool.js 54a3ac228e6c4703fe72fb65c897e19156263a51fe9b7e21d2834a45e876aabd @@ -765,7 +765,7 @@ F src/os_win.c db4baa8f62bbfe3967c71b008cea31a8f2ff337c1667ff4d8a677e697315ff0d F src/os_win.h 7b073010f1451abe501be30d12f6bc599824944a F src/pager.c 2f89b83e0a85635ebae908325029a21bd6cdcc0c604e9289354215fb6c3e4ec3 F src/pager.h fc0c95d27f2aefffcb39b4373b3557e89e1b4b9a36b183cc04d6f22002bf0ad2 -F src/parse.y 8ec56598aa0df92428627502267d0d1c9778cc27308f8ffd31dfb2d017a8755f +F src/parse.y dcf45a81b61223ac93e61fdfe9b22d635dd371c446e8222634d90aa37e25e5f6 F src/pcache.c 588cc3c5ccaaadde689ed35ce5c5c891a1f7b1f4d1f56f6cf0143b74d8ee6484 F src/pcache.h 1497ce1b823cf00094bb0cf3bac37b345937e6f910890c626b16512316d3abf5 F src/pcache1.c 49516ad7718a3626f28f710fa7448ef1fce3c07fd169acbb4817341950264319 @@ -1043,7 +1043,7 @@ F test/corruptJ.test 4d5ccc4bf959464229a836d60142831ef76a5aa4 F test/corruptK.test ac13504593d89d69690d45479547616ed12644d42b5cb7eeb2e759a76fc23dcb F test/corruptL.test 652fc8ac0763a6fd3eb28b951d481924167b2d9936083bcc68253b2274a0c8fe F test/corruptM.test 7d574320e08c1b36caa3e47262061f186367d593a7e305d35f15289cc2c3e067 -F test/corruptN.test 40bc47aee4af9aadff902be43f14d69dc17b3731448dad6c7cc722da913f1455 +F test/corruptN.test a034bb217bebd8d007625dfb078e76ec3d53515052dbceb68bd47b2c27674d5c F test/cost.test cc434a026b1e9d0d98137a147e24e5daf1b1ad09e9ff7da63b34c83ddd136d92 F test/count.test cd4bd531066e8d77ef8fe1e3fc8253d042072e117ccab214b290cf83f1602249 F test/countofview.test 4088e461a10ee33e69803c177a69aa1d7bba81a9ffc2df66d76465a22ca7fdfc @@ -2009,7 +2009,7 @@ F test/wal64k.test 2a525c0f45d709bae3765c71045ccec5df7d100ccbd3a7860fdba46c9addb F test/wal7.test 2ae8f427d240099cc4b2dfef63cff44e2a68a1bd F test/wal8.test d9df3fba4caad5854ed69ed673c68482514203c8 F test/wal9.test 378e76a9ad09cd9bee06c172ad3547b0129a6750 -F test/wal_common.tcl 4589f701d5527ace2eba43823c96c2177e1f9dd2a6098256ee2203a0a313c13a +F test/wal_common.tcl 204d1721ac13c5e0c7fae6380315b5ab7f4e8423f580d826c5e9df1995cb018d F test/walbak.test 018d4e5a3d45c6298d11b99f09a8ef6876527946 F test/walbig.test f437473a16cfb314867c6b5d1dbcd519e73e3434 F test/walblock.test be48f3a75eff0b4456209f26b3ce186c2015497d @@ -2203,7 +2203,7 @@ F tool/stack_usage.tcl f8e71b92cdb099a147dad572375595eae55eca43 F tool/stripccomments.c 20b8aabc4694d0d4af5566e42da1f1a03aff057689370326e9269a9ddcffdc37 F tool/symbols-mingw.sh 4dbcea7e74768305384c9fd2ed2b41bbf9f0414d F tool/symbols.sh 1612bd947750e21e7b47befad5f6b3825b06cce0705441f903bf35ced65ae9b9 -F tool/tclConfigShToAutoDef.sh 44ec55046d86a3febb2cb3e099399b41794e80e9cd138eee7b9b016f819e882b x tool/tclConfigShToTcl.sh +F tool/tclConfigShToAutoDef.sh 44ec55046d86a3febb2cb3e099399b41794e80e9cd138eee7b9b016f819e882b x F tool/tclConfigShToMake.sh 7c065d81c2d178e15e45a77372c6e5a38b5a1b08755301cd6f20a3a862db7312 x F tool/varint.c 5d94cb5003db9dbbcbcc5df08d66f16071aee003 F tool/vdbe-compress.tcl fa2f37ab39b2a0087fafb6a7f3ce19503e25e624ffa8ed9951717ab72920c088 @@ -2212,8 +2212,8 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh 49a486c5069de041aedcbde4de178293e0463ae9918ecad7539eedf0ec77a139 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 4ddea432bb19abf33a8a202a1a6d512d008e810e98a5ff19e2d699ce40da2e9a e1330ecf170436678c5c235557fb95b8acbf4fad62583edba4af03aff49afbc5 -R ae4103aa11685c042bb48b9ec2472609 +P d19aeff609ae7a19e90cef1181706cee81d9e5ba8475f0f03ddb1ee600b90415 +R 6dec105d14c1dc647248e32a689305b3 U drh -Z 077b0a02a7a2dc128b626abf88e131d3 +Z e4cd7e2e7de43b8796514668764d2dd3 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 2e4b84b07a..2dea9c6e54 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -d19aeff609ae7a19e90cef1181706cee81d9e5ba8475f0f03ddb1ee600b90415 +ffe9f9ccd72f022a8388bd6426abf4b9f00d52a18206d8c3345d9dd47d4ddd71 diff --git a/src/parse.y b/src/parse.y index 8fdea9bfa3..b8d904d12c 100644 --- a/src/parse.y +++ b/src/parse.y @@ -43,7 +43,7 @@ %syntax_error { UNUSED_PARAMETER(yymajor); /* Silence some compiler warnings */ if( TOKEN.z[0] ){ - sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &TOKEN); + parserSyntaxError(pParse, &TOKEN); }else{ sqlite3ErrorMsg(pParse, "incomplete input"); } @@ -111,6 +111,13 @@ struct TrigEvent { int a; IdList * b; }; struct FrameBound { int eType; Expr *pExpr; }; +/* +** Generate a syntax error +*/ +static void parserSyntaxError(Parse *pParse, Token *p){ + sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", p); +} + /* ** Disable lookaside memory allocation for objects that might be ** shared across database connections. @@ -1156,7 +1163,7 @@ expr(A) ::= VARIABLE(X). { Token t = X; /*A-overwrites-X*/ assert( t.n>=2 ); if( pParse->nested==0 ){ - sqlite3ErrorMsg(pParse, "near \"%T\": syntax error", &t); + parserSyntaxError(pParse, &t); A = 0; }else{ A = sqlite3PExpr(pParse, TK_REGISTER, 0, 0); diff --git a/test/corruptN.test b/test/corruptN.test index 8108609c09..2297991aba 100644 --- a/test/corruptN.test +++ b/test/corruptN.test @@ -141,15 +141,6 @@ do_test 2.0 { | end c-b92b.txt.db }]} {} -# This test only works with the legacy RC4 PRNG -if 0 { - prng_seed 0 db - do_catchsql_test 2.1 { - SELECT count(*) FROM sqlite_schema; - WITH RECURSIVE c(x) AS (VALUES(1) UNION ALL SELECT x+1 FROM c WHERE x<1000) - INSERT INTO t1(a) SELECT randomblob(null) FROM c; - } {1 {database disk image is malformed}} -} reset_db if {![info exists ::G(perm:presql)]} { diff --git a/test/wal_common.tcl b/test/wal_common.tcl index cdba53b5b2..d31131aa02 100644 --- a/test/wal_common.tcl +++ b/test/wal_common.tcl @@ -87,3 +87,43 @@ proc wal_fix_walindex_cksum {hdrvar} { lset hdr 10 $c1 lset hdr 11 $c2 } + +# This command assumes that $file is the name of a database file opened +# in wal mode using a [testvfs] VFS. It returns a list of the 12 32-bit +# integers that make up the wal-index-header for the named file. +# +proc set_tvfs_hdr {file args} { + + # Set $nHdr to the number of bytes in the wal-index header: + set nHdr 48 + set nInt [expr {$nHdr/4}] + + if {[llength $args]>2} { + error {wrong # args: should be "set_tvfs_hdr fileName ?val1? ?val2?"} + } + + set blob [tvfs shm $file] + if {$::tcl_platform(byteOrder)=="bigEndian"} {set fmt I} {set fmt i} + + if {[llength $args]} { + set ia [lindex $args 0] + set ib $ia + if {[llength $args]==2} { + set ib [lindex $args 1] + } + binary scan $blob a[expr $nHdr*2]a* dummy tail + set blob [binary format ${fmt}${nInt}${fmt}${nInt}a* $ia $ib $tail] + tvfs shm $file $blob + } + + binary scan $blob ${fmt}${nInt} ints + return $ints +} + +proc incr_tvfs_hdr {file idx incrval} { + set ints [set_tvfs_hdr $file] + set v [lindex $ints $idx] + incr v $incrval + lset ints $idx $v + set_tvfs_hdr $file $ints +} From 7204db1f61d017152f4f288d61934bea091715f1 Mon Sep 17 00:00:00 2001 From: dan Date: Fri, 13 Dec 2024 20:18:29 +0000 Subject: [PATCH 44/51] Fix a problem with the output of "PRAGMA wal_checkpoint" introduced long ago by [63483e22c775183e]. FossilOrigin-Name: c5f7bba8683a9aa503750abb7fd937566983dc1ca08361eca8b9b185294b909a --- manifest | 16 ++++++++-------- manifest.uuid | 2 +- src/wal.c | 4 +--- test/wal2simple.test | 16 ++++++++++++++++ 4 files changed, 26 insertions(+), 12 deletions(-) diff --git a/manifest b/manifest index 7c531c605e..ed4b67285c 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Update\sthe\swal2\sbranch\swith\sthe\slatest\strunk\senhancements\sand\sfixes. -D 2024-12-07T23:24:17.824 +C Fix\sa\sproblem\swith\sthe\soutput\sof\s"PRAGMA\swal_checkpoint"\sintroduced\slong\sago\sby\s[63483e22c775183e]. +D 2024-12-13T20:18:29.915 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md e108e1e69ae8e8a59e93c455654b8ac9356a11720d3345df2a4743e9590fb20d @@ -860,7 +860,7 @@ F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf8 F src/vdbevtab.c fc46b9cbd759dc013f0b3724549cc0d71379183c667df3a5988f7e2f1bd485f3 F src/vtab.c 316cd48e9320660db3047cd306cd056e4361180cebb4d0f10a39244e10c11422 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 70d127be90923ec33cc53f7206d947173f1e4e4a98b34115d92861e1584e36ef +F src/wal.c c80108214438d8112963ac4a28e2ddfb92fc993b33bf25fdc77440e30ff6c268 F src/wal.h 97b8a9903387401377b59507e86b93a148ef1ad4e5ce0f23659a12dcdce56af2 F src/walker.c d5006d6b005e4ea7302ad390957a8d41ed83faa177e412f89bc5600a7462a014 F src/where.c b34adb09fdb2a4f57a0bafd4194b501047ef383976fef9f0ac3f395f4881694d @@ -2003,7 +2003,7 @@ F test/wal2recover3.test 4a91689e165a38bc401736e6518188c2b0ff4fa1566d1810b886753 F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal2rollback.test 23adc4a099b23f6aaea8b04fdca1c35861d887dd80f8be7da2d5273eb777e428 F test/wal2savepoint.test c380512ec1f97012cfc753e718bf3df38f51e7c319110e1cae25094a33b371c4 -F test/wal2simple.test 320a08927f307d0ead26fa3fcef4e3f64279f49be9504f918cb33294f75aeec8 +F test/wal2simple.test 2aaf8d81e889035e6021369bec45758f89ac3a13ae7bcefc111ea1aabe3f9cfd F test/wal2snapshot.test 7a5f4629a3c43a43c3440b8b2ea9f07de91a46b0b9eea5f08f62b5bf5b6468df F test/wal3.test 5de023bb862fd1eb9d2ad26fa8d9c43abb5370582e5b08b2ae0d6f93661bc310 F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c @@ -2216,8 +2216,8 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh 49a486c5069de041aedcbde4de178293e0463ae9918ecad7539eedf0ec77a139 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 9591ab7c6b9bf70d33607c3dc8e319906d537c13c8e6b853b294e941581bcfe1 fc6904a508eb732b1cb5cc12321a0d637d97e1e066a022a2c93cb50595f3a86a -R 716accb182792985409c3e9a8fb0b506 -U drh -Z 936f7b3be32146f67ad3db2a80519bfd +P d3ce95f7e0335b8522a1805ab602b64856ced3007c507ba42039c6183ef9ac3d +R ee2cfb61cc5fc78106af5b84f79ac024 +U dan +Z 2ab193829137f4720a5ca440c0e08d93 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 32b54b3113..4d9fe6678e 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -d3ce95f7e0335b8522a1805ab602b64856ced3007c507ba42039c6183ef9ac3d +c5f7bba8683a9aa503750abb7fd937566983dc1ca08361eca8b9b185294b909a diff --git a/src/wal.c b/src/wal.c index 4bae7e1527..5f43689f8f 100644 --- a/src/wal.c +++ b/src/wal.c @@ -5175,8 +5175,6 @@ int sqlite3WalCheckpoint( /* Copy data from the log to the database file. */ if( rc==SQLITE_OK ){ - int iCkpt = walidxGetFile(&pWal->hdr); - if( (walPagesize(pWal)!=nBuf) && ((pWal->hdr.mxFrame2 & 0x7FFFFFFF) || pWal->hdr.mxFrame) ){ @@ -5195,7 +5193,7 @@ int sqlite3WalCheckpoint( if( pnCkpt ){ if( isWalMode2(pWal) ){ if( (int)(walCkptInfo(pWal)->nBackfill) ){ - *pnCkpt = walidxGetMxFrame(&pWal->hdr, iCkpt); + *pnCkpt = walidxGetMxFrame(&pWal->hdr,!walidxGetFile(&pWal->hdr)); }else{ *pnCkpt = 0; } diff --git a/test/wal2simple.test b/test/wal2simple.test index e63f4e1759..3ccd4b91a4 100644 --- a/test/wal2simple.test +++ b/test/wal2simple.test @@ -470,6 +470,22 @@ do_test 7.5.4 { list [file size test.db-wal] [file size test.db-wal2] [file size test.db] } {22040 12608 87040} +#------------------------------------------------------------------------- +reset_db +do_execsql_test 9.0 { + PRAGMA journal_size_limit = 10000; + PRAGMA journal_mode = wal2; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); +} {10000 wal2} + +do_execsql_test 9.1 { + PRAGMA wal_checkpoint; +} {0 50 13} + finish_test From 52660d4d37ea9d530a3867f12240e0fc1349904f Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 14 Dec 2024 16:41:39 +0000 Subject: [PATCH 45/51] Do not attempt to switch wal files when writing if the current transaction read from both wal files. This fix is already on branch 'bedrock'. FossilOrigin-Name: 693cc8e8db6c00249211bdfc21be2bff169ca6b0c4560522dd404615e8ba3b76 --- manifest | 14 +++++------ manifest.uuid | 2 +- src/wal.c | 56 +++++++++++++++++++++++++++----------------- test/wal2simple.test | 27 ++++++++++++++++++++- 4 files changed, 69 insertions(+), 30 deletions(-) diff --git a/manifest b/manifest index ed4b67285c..658424d0a6 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sa\sproblem\swith\sthe\soutput\sof\s"PRAGMA\swal_checkpoint"\sintroduced\slong\sago\sby\s[63483e22c775183e]. -D 2024-12-13T20:18:29.915 +C Do\snot\sattempt\sto\sswitch\swal\sfiles\swhen\swriting\sif\sthe\scurrent\stransaction\sread\sfrom\sboth\swal\sfiles.\sThis\sfix\sis\salready\son\sbranch\s'bedrock'. +D 2024-12-14T16:41:39.812 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md e108e1e69ae8e8a59e93c455654b8ac9356a11720d3345df2a4743e9590fb20d @@ -860,7 +860,7 @@ F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf8 F src/vdbevtab.c fc46b9cbd759dc013f0b3724549cc0d71379183c667df3a5988f7e2f1bd485f3 F src/vtab.c 316cd48e9320660db3047cd306cd056e4361180cebb4d0f10a39244e10c11422 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c c80108214438d8112963ac4a28e2ddfb92fc993b33bf25fdc77440e30ff6c268 +F src/wal.c 42f0823a65218643d213633b52e2a80ca70b193f6f7802c6edb040448860e9e8 F src/wal.h 97b8a9903387401377b59507e86b93a148ef1ad4e5ce0f23659a12dcdce56af2 F src/walker.c d5006d6b005e4ea7302ad390957a8d41ed83faa177e412f89bc5600a7462a014 F src/where.c b34adb09fdb2a4f57a0bafd4194b501047ef383976fef9f0ac3f395f4881694d @@ -2003,7 +2003,7 @@ F test/wal2recover3.test 4a91689e165a38bc401736e6518188c2b0ff4fa1566d1810b886753 F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal2rollback.test 23adc4a099b23f6aaea8b04fdca1c35861d887dd80f8be7da2d5273eb777e428 F test/wal2savepoint.test c380512ec1f97012cfc753e718bf3df38f51e7c319110e1cae25094a33b371c4 -F test/wal2simple.test 2aaf8d81e889035e6021369bec45758f89ac3a13ae7bcefc111ea1aabe3f9cfd +F test/wal2simple.test 471f6d404c19b94968ef4c74566914e544d31c5fa0afc8f1b31af117d7014a8b F test/wal2snapshot.test 7a5f4629a3c43a43c3440b8b2ea9f07de91a46b0b9eea5f08f62b5bf5b6468df F test/wal3.test 5de023bb862fd1eb9d2ad26fa8d9c43abb5370582e5b08b2ae0d6f93661bc310 F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c @@ -2216,8 +2216,8 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh 49a486c5069de041aedcbde4de178293e0463ae9918ecad7539eedf0ec77a139 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P d3ce95f7e0335b8522a1805ab602b64856ced3007c507ba42039c6183ef9ac3d -R ee2cfb61cc5fc78106af5b84f79ac024 +P c5f7bba8683a9aa503750abb7fd937566983dc1ca08361eca8b9b185294b909a +R 9126283d88fd2ef1f08bb80d8fab9c01 U dan -Z 2ab193829137f4720a5ca440c0e08d93 +Z 875bffc535ab8b30264831dbcb04504f # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 4d9fe6678e..6c7c0600cb 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -c5f7bba8683a9aa503750abb7fd937566983dc1ca08361eca8b9b185294b909a +693cc8e8db6c00249211bdfc21be2bff169ca6b0c4560522dd404615e8ba3b76 diff --git a/src/wal.c b/src/wal.c index 5f43689f8f..987eed318d 100644 --- a/src/wal.c +++ b/src/wal.c @@ -4593,32 +4593,46 @@ static int walRestartLog(Wal *pWal){ int iApp = walidxGetFile(&pWal->hdr); u32 nWalSize = WAL_DEFAULT_WALSIZE; if( pWal->mxWalSize>0 ){ + /* mxWalSize is in bytes. Convert this to a number of frames. */ nWalSize = (pWal->mxWalSize-WAL_HDRSIZE+pWal->szPage+WAL_FRAME_HDRSIZE-1) / (pWal->szPage+WAL_FRAME_HDRSIZE); nWalSize = MAX(nWalSize, 1); } - if( walidxGetMxFrame(&pWal->hdr, iApp)>=nWalSize ){ - volatile WalCkptInfo *pInfo = walCkptInfo(pWal); - u32 mxFrame = walidxGetMxFrame(&pWal->hdr, !iApp); - if( mxFrame==0 || pInfo->nBackfill ){ - rc = wal2RestartOk(pWal, iApp); - if( rc==SQLITE_OK ){ - int iNew = !iApp; - pWal->nCkpt++; - walidxSetFile(&pWal->hdr, iNew); - walidxSetMxFrame(&pWal->hdr, iNew, 0); - sqlite3Put4byte((u8*)&pWal->hdr.aSalt[0], pWal->hdr.aFrameCksum[0]); - sqlite3Put4byte((u8*)&pWal->hdr.aSalt[1], pWal->hdr.aFrameCksum[1]); - walIndexWriteHdr(pWal); - pInfo->nBackfill = 0; - wal2RestartFinished(pWal, iApp); - walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); - pWal->readLock = iNew ? WAL_LOCK_PART2_FULL1 : WAL_LOCK_PART1_FULL2; - rc = walLockShared(pWal, WAL_READ_LOCK(pWal->readLock)); - }else if( rc==SQLITE_BUSY ){ - rc = SQLITE_OK; - } + assert( iApp==0 || pWal->readLock==WAL_LOCK_PART2 + || pWal->readLock==WAL_LOCK_PART2_FULL1 ); + assert( iApp==1 || pWal->readLock==WAL_LOCK_PART1 + || pWal->readLock==WAL_LOCK_PART1_FULL2 ); + + /* Switch to wal file !iApp if + ** + ** (a) Wal file iApp (the current wal file) contains >= nWalSize frames. + ** (b) This client is not reading from wal file !iApp. + ** (c) No other client is reading from wal file !iApp. + ** + ** Condition (b) guarantees that wal file !iApp is either empty or + ** completely checkpointed. + */ + if( (pWal->readLock==WAL_LOCK_PART1 || pWal->readLock==WAL_LOCK_PART2) + && walidxGetMxFrame(&pWal->hdr, iApp)>=nWalSize + ){ + rc = wal2RestartOk(pWal, iApp); + if( rc==SQLITE_OK ){ + volatile WalCkptInfo *pInfo = walCkptInfo(pWal); + int iNew = !iApp; + pWal->nCkpt++; + walidxSetFile(&pWal->hdr, iNew); + walidxSetMxFrame(&pWal->hdr, iNew, 0); + sqlite3Put4byte((u8*)&pWal->hdr.aSalt[0], pWal->hdr.aFrameCksum[0]); + sqlite3Put4byte((u8*)&pWal->hdr.aSalt[1], pWal->hdr.aFrameCksum[1]); + walIndexWriteHdr(pWal); + pInfo->nBackfill = 0; + wal2RestartFinished(pWal, iApp); + walUnlockShared(pWal, WAL_READ_LOCK(pWal->readLock)); + pWal->readLock = iNew ? WAL_LOCK_PART2_FULL1 : WAL_LOCK_PART1_FULL2; + rc = walLockShared(pWal, WAL_READ_LOCK(pWal->readLock)); + }else if( rc==SQLITE_BUSY ){ + rc = SQLITE_OK; } } }else if( pWal->readLock==0 ){ diff --git a/test/wal2simple.test b/test/wal2simple.test index 3ccd4b91a4..3d50d3e950 100644 --- a/test/wal2simple.test +++ b/test/wal2simple.test @@ -470,6 +470,32 @@ do_test 7.5.4 { list [file size test.db-wal] [file size test.db-wal2] [file size test.db] } {22040 12608 87040} +#------------------------------------------------------------------------- +reset_db +do_execsql_test 8.0 { + PRAGMA journal_size_limit = 10000; + PRAGMA journal_mode = wal2; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); + BEGIN; + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); +} {10000 wal2} + +sqlite3 db2 test.db +do_execsql_test -db db2 8.1 { + PRAGMA wal_checkpoint; +} {0 50 13} + +do_execsql_test 8.2 { + COMMIT; +} + +db2 close + + #------------------------------------------------------------------------- reset_db do_execsql_test 9.0 { @@ -486,6 +512,5 @@ do_execsql_test 9.1 { PRAGMA wal_checkpoint; } {0 50 13} - finish_test From 3d6fbe469b55644e1829a40b69a3f7a365578503 Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 14 Dec 2024 16:50:29 +0000 Subject: [PATCH 46/51] Fix a problem with non-PASSIVE wal2 checkpoints sometimes taking the WRITER lock. FossilOrigin-Name: 11d7865a56c1fbb978e3ba70ddc03c2a16080e2992c580e00e4c6c1fa47c043c --- manifest | 15 ++++++++------- manifest.uuid | 2 +- src/wal.c | 2 +- test/wal2simple.test | 27 +++++++++++++++++++++++++++ 4 files changed, 37 insertions(+), 9 deletions(-) diff --git a/manifest b/manifest index 658424d0a6..cf2fce9a5f 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Do\snot\sattempt\sto\sswitch\swal\sfiles\swhen\swriting\sif\sthe\scurrent\stransaction\sread\sfrom\sboth\swal\sfiles.\sThis\sfix\sis\salready\son\sbranch\s'bedrock'. -D 2024-12-14T16:41:39.812 +C Fix\sa\sproblem\swith\snon-PASSIVE\swal2\scheckpoints\ssometimes\staking\sthe\sWRITER\slock. +D 2024-12-14T16:50:29.177 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md e108e1e69ae8e8a59e93c455654b8ac9356a11720d3345df2a4743e9590fb20d @@ -860,7 +860,7 @@ F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf8 F src/vdbevtab.c fc46b9cbd759dc013f0b3724549cc0d71379183c667df3a5988f7e2f1bd485f3 F src/vtab.c 316cd48e9320660db3047cd306cd056e4361180cebb4d0f10a39244e10c11422 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 42f0823a65218643d213633b52e2a80ca70b193f6f7802c6edb040448860e9e8 +F src/wal.c 0d024a770c3f52baab91427a3a1b89cecfaefb492964da1a8e83f94187287f46 F src/wal.h 97b8a9903387401377b59507e86b93a148ef1ad4e5ce0f23659a12dcdce56af2 F src/walker.c d5006d6b005e4ea7302ad390957a8d41ed83faa177e412f89bc5600a7462a014 F src/where.c b34adb09fdb2a4f57a0bafd4194b501047ef383976fef9f0ac3f395f4881694d @@ -2003,7 +2003,7 @@ F test/wal2recover3.test 4a91689e165a38bc401736e6518188c2b0ff4fa1566d1810b886753 F test/wal2rewrite.test 6ca6f631ffcf871240beab5f02608913fd075c6d0d31310b026c8383c65c9f9c F test/wal2rollback.test 23adc4a099b23f6aaea8b04fdca1c35861d887dd80f8be7da2d5273eb777e428 F test/wal2savepoint.test c380512ec1f97012cfc753e718bf3df38f51e7c319110e1cae25094a33b371c4 -F test/wal2simple.test 471f6d404c19b94968ef4c74566914e544d31c5fa0afc8f1b31af117d7014a8b +F test/wal2simple.test d2d94fe1f30700c9f30e46c16f1c3d7d245b861a1be2bfe5ead701484c6dd38e F test/wal2snapshot.test 7a5f4629a3c43a43c3440b8b2ea9f07de91a46b0b9eea5f08f62b5bf5b6468df F test/wal3.test 5de023bb862fd1eb9d2ad26fa8d9c43abb5370582e5b08b2ae0d6f93661bc310 F test/wal4.test 4744e155cd6299c6bd99d3eab1c82f77db9cdb3c @@ -2216,8 +2216,9 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh 49a486c5069de041aedcbde4de178293e0463ae9918ecad7539eedf0ec77a139 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P c5f7bba8683a9aa503750abb7fd937566983dc1ca08361eca8b9b185294b909a -R 9126283d88fd2ef1f08bb80d8fab9c01 +P 693cc8e8db6c00249211bdfc21be2bff169ca6b0c4560522dd404615e8ba3b76 +Q +1a59cae3c31aea25cef3705cce2477e26515a0463cf9094bd29951899b758767 +R 5649ce79a264a23dcf9673f867edeff1 U dan -Z 875bffc535ab8b30264831dbcb04504f +Z 9ac7b2b52092771e4fcd759caab1f3c0 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 6c7c0600cb..c3512e7ef3 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -693cc8e8db6c00249211bdfc21be2bff169ca6b0c4560522dd404615e8ba3b76 +11d7865a56c1fbb978e3ba70ddc03c2a16080e2992c580e00e4c6c1fa47c043c diff --git a/src/wal.c b/src/wal.c index 987eed318d..ca53cf9c62 100644 --- a/src/wal.c +++ b/src/wal.c @@ -5158,7 +5158,7 @@ int sqlite3WalCheckpoint( ** writer lock retried until either the busy-handler returns 0 or the ** lock is successfully obtained. */ - if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){ + if( eMode!=SQLITE_CHECKPOINT_PASSIVE && isWalMode2(pWal)==0 ){ rc = walBusyLock(pWal, xBusy2, pBusyArg, WAL_WRITE_LOCK, 1); if( rc==SQLITE_OK ){ pWal->writeLock = 1; diff --git a/test/wal2simple.test b/test/wal2simple.test index 3d50d3e950..1a8c1e63bd 100644 --- a/test/wal2simple.test +++ b/test/wal2simple.test @@ -512,5 +512,32 @@ do_execsql_test 9.1 { PRAGMA wal_checkpoint; } {0 50 13} + +#------------------------------------------------------------------------- +# Check that it is possible to do a non-PASSIVE checkpoint on a wal2 +# db without blocking writers. +# +reset_db +do_execsql_test 10.0 { + PRAGMA journal_size_limit = 10000; + PRAGMA journal_mode = wal2; + CREATE TABLE t1(x); + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); + BEGIN; + INSERT INTO t1 VALUES( hex( randomblob(5000) ) ); +} {10000 wal2} + +sqlite3 db2 test.db +do_execsql_test -db db2 10.1 { + PRAGMA wal_checkpoint = FULL; +} {0 50 13} + +do_execsql_test 10.2 { + COMMIT; +} + finish_test From 686166eb32a9b4fbfdcf95d21b18bcb3bad349f9 Mon Sep 17 00:00:00 2001 From: dan Date: Sat, 14 Dec 2024 17:19:06 +0000 Subject: [PATCH 47/51] Update to walRestartLog() function to make it shorter easier to merge with branch "bedrock". FossilOrigin-Name: 88d7fb2aef882fc41a4f9c1bdd2f0162289e90d62df50af1b0eb6994c5c94c28 --- manifest | 13 ++++++------- manifest.uuid | 2 +- src/wal.c | 4 +++- 3 files changed, 10 insertions(+), 9 deletions(-) diff --git a/manifest b/manifest index cf2fce9a5f..1c4be400ec 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Fix\sa\sproblem\swith\snon-PASSIVE\swal2\scheckpoints\ssometimes\staking\sthe\sWRITER\slock. -D 2024-12-14T16:50:29.177 +C Update\sto\swalRestartLog()\sfunction\sto\smake\sit\sshorter\seasier\sto\smerge\swith\sbranch\s"bedrock". +D 2024-12-14T17:19:06.037 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md e108e1e69ae8e8a59e93c455654b8ac9356a11720d3345df2a4743e9590fb20d @@ -860,7 +860,7 @@ F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf8 F src/vdbevtab.c fc46b9cbd759dc013f0b3724549cc0d71379183c667df3a5988f7e2f1bd485f3 F src/vtab.c 316cd48e9320660db3047cd306cd056e4361180cebb4d0f10a39244e10c11422 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 0d024a770c3f52baab91427a3a1b89cecfaefb492964da1a8e83f94187287f46 +F src/wal.c 2ec54ef10bb354fc61424c4b65f407e795abe07059001d0d1c5f99be5bb8b7bf F src/wal.h 97b8a9903387401377b59507e86b93a148ef1ad4e5ce0f23659a12dcdce56af2 F src/walker.c d5006d6b005e4ea7302ad390957a8d41ed83faa177e412f89bc5600a7462a014 F src/where.c b34adb09fdb2a4f57a0bafd4194b501047ef383976fef9f0ac3f395f4881694d @@ -2216,9 +2216,8 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh 49a486c5069de041aedcbde4de178293e0463ae9918ecad7539eedf0ec77a139 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 693cc8e8db6c00249211bdfc21be2bff169ca6b0c4560522dd404615e8ba3b76 -Q +1a59cae3c31aea25cef3705cce2477e26515a0463cf9094bd29951899b758767 -R 5649ce79a264a23dcf9673f867edeff1 +P 11d7865a56c1fbb978e3ba70ddc03c2a16080e2992c580e00e4c6c1fa47c043c +R 4b3e1430220bd396f41d373fd3638959 U dan -Z 9ac7b2b52092771e4fcd759caab1f3c0 +Z 80dc095041f96e4782d4286427d7b68b # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index c3512e7ef3..50f2864eed 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -11d7865a56c1fbb978e3ba70ddc03c2a16080e2992c580e00e4c6c1fa47c043c +88d7fb2aef882fc41a4f9c1bdd2f0162289e90d62df50af1b0eb6994c5c94c28 diff --git a/src/wal.c b/src/wal.c index ca53cf9c62..ddb0b9dd62 100644 --- a/src/wal.c +++ b/src/wal.c @@ -4613,7 +4613,9 @@ static int walRestartLog(Wal *pWal){ ** Condition (b) guarantees that wal file !iApp is either empty or ** completely checkpointed. */ - if( (pWal->readLock==WAL_LOCK_PART1 || pWal->readLock==WAL_LOCK_PART2) + assert( (0*3)+1==WAL_LOCK_PART1 ); /* iApp==0 -> require WAL_LOCK_PART1 */ + assert( (1*3)+1==WAL_LOCK_PART2 ); /* iApp==1 -> require WAL_LOCK_PART2 */ + if( pWal->readLock==(iApp*3)+1 && walidxGetMxFrame(&pWal->hdr, iApp)>=nWalSize ){ rc = wal2RestartOk(pWal, iApp); From 0524562918fce9484bd950c337c955bd85bc8d10 Mon Sep 17 00:00:00 2001 From: drh <> Date: Mon, 16 Dec 2024 13:05:47 +0000 Subject: [PATCH 48/51] Synchronize the WASM code with trunk. FossilOrigin-Name: 3619baa19e8e7617a7c5918073a826af1cc4d5dc9e9f27b4e4187e988b534d7f --- .../{ => api}/EXPORTED_FUNCTIONS.sqlite3-core | 0 ext/wasm/fiddle/{fiddle.html => index.html} | 31 +++++++++++++++---- manifest | 14 ++++----- manifest.uuid | 2 +- 4 files changed, 33 insertions(+), 14 deletions(-) rename ext/wasm/{ => api}/EXPORTED_FUNCTIONS.sqlite3-core (100%) rename ext/wasm/fiddle/{fiddle.html => index.html} (90%) diff --git a/ext/wasm/EXPORTED_FUNCTIONS.sqlite3-core b/ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-core similarity index 100% rename from ext/wasm/EXPORTED_FUNCTIONS.sqlite3-core rename to ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-core diff --git a/ext/wasm/fiddle/fiddle.html b/ext/wasm/fiddle/index.html similarity index 90% rename from ext/wasm/fiddle/fiddle.html rename to ext/wasm/fiddle/index.html index 3570b283e3..f779749319 100644 --- a/ext/wasm/fiddle/fiddle.html +++ b/ext/wasm/fiddle/index.html @@ -9,7 +9,6 @@ two lines and ensure that these files are on the web server. --> - @@ -273,11 +297,6 @@ - diff --git a/manifest b/manifest index 90c315b1ed..502c9092d5 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\sall\sthe\slatest\strunk\senhancements\sand\sfixes\sinto\sthe\swal2\sbranch. -D 2024-12-16T12:41:33.006 +C Synchronize\sthe\sWASM\scode\swith\strunk. +D 2024-12-16T13:05:47.681 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md e108e1e69ae8e8a59e93c455654b8ac9356a11720d3345df2a4743e9590fb20d @@ -619,7 +619,6 @@ F ext/session/sqlite3session.c 3d0a7f0f7a1c946e01818c716a55a40ae30542a29a9045cb0 F ext/session/sqlite3session.h 683ccbf16e2c2521661fc4c1cf918ce57002039efbcabcd8097fa4bca569104b F ext/session/test_session.c aa29abdcc9011ac02f4fa38e8ede226106eaeee7c3ea7d8b2b999a124e0c368c F ext/wasm/EXPORTED_FUNCTIONS.fiddle.in 27450c8b8c70875a260aca55435ec927068b34cef801a96205adb81bdcefc65c -F ext/wasm/EXPORTED_FUNCTIONS.sqlite3-core 2bcbbfe3b95c043ed6037e2708a2ee078d212dd1612c364f93588d8dc97300fe F ext/wasm/GNUmakefile 311aa0d5edc7006409962cc77cc26560d92f9be69c2c4302e8bbc68189fd02db F ext/wasm/README-dist.txt 6382cb9548076fca472fb3330bbdba3a55c1ea0b180ff9253f084f07ff383576 F ext/wasm/README.md a8a2962c3aebdf8d2104a9102e336c5554e78fc6072746e5daf9c61514e7d193 @@ -628,6 +627,7 @@ F ext/wasm/SQLTester/SQLTester.mjs 66e1adc3d79467b68e3e40614fd42c1a577c7e219ec09 F ext/wasm/SQLTester/SQLTester.run.mjs 57f2adb33f43f2784abbf8026c1bfd049d8013af1998e7dcb8b50c89ffc332e0 F ext/wasm/SQLTester/index.html 64f3435084c7d6139b08d1f2a713828a73f68de2ae6a3112cbb5980d991ba06f F ext/wasm/SQLTester/touint8array.c 2d5ece04ec1393a6a60c4bf96385bda5e1a10ad49f3038b96460fc5e5aa7e536 +F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-core 2bcbbfe3b95c043ed6037e2708a2ee078d212dd1612c364f93588d8dc97300fe F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-extras fe40d6d758646e38f8b15f709044951e10884214f5453d35502100179c388c13 F ext/wasm/api/EXPORTED_FUNCTIONS.sqlite3-see fb29e62082a658f0d81102488414d422c393c4b20cc2f685b216bc566237957b F ext/wasm/api/EXPORTED_RUNTIME_METHODS.sqlite3-api 1ec3c73e7d66e95529c3c64ac3de2470b0e9e7fbf7a5b41261c367cf4f1b7287 @@ -673,8 +673,8 @@ F ext/wasm/dist.make 653e212c1e84aa3be168d62a10616ccea45ee9585b0192745d2706707a5 F ext/wasm/example_extra_init.c 2347cd69d19d839ef4e5e77b7855103a7fe3ef2af86f2e8c95839afd8b05862f F ext/wasm/fiddle.make d4969f0322a582c57a22ce3541f10a5b09a609d14eab32891f613f43b3c14d8b F ext/wasm/fiddle/fiddle-worker.js 850e66fce39b89d59e161d1abac43a181a4caa89ddeea162765d660277cd84ce -F ext/wasm/fiddle/fiddle.html 550c5aafce40bd218de9bf26192749f69f9b10bc379423ecd2e162bcef885c08 F ext/wasm/fiddle/fiddle.js b444a5646a9aac9f3fc06c53d78af5e1912eb235d69a8e6010723e4eb0e9d4a1 +F ext/wasm/fiddle/index.html c79b1741cbeba78f88af0a84cf5ec7de87a909a6a8d10a369b1f4824c66c2088 F ext/wasm/index-dist.html 564b5ec5669676482c5a25dea9e721d8eafed426ecb155f93d29aeff8507511f F ext/wasm/index.html e4bbffdb3d40eff12b3f9c7abedef91787e2935620b7f8d40f2c774b80ad8fa9 F ext/wasm/jaccwabyt/jaccwabyt.js 1264710db3cfbcb6887d95665b7aeba60c1126eaef789ca4cf1a4a17d5bc7f54 @@ -2216,8 +2216,8 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh 49a486c5069de041aedcbde4de178293e0463ae9918ecad7539eedf0ec77a139 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 88d7fb2aef882fc41a4f9c1bdd2f0162289e90d62df50af1b0eb6994c5c94c28 ae580443d210811c12209866112fc8b0b83281e24945504b748a17d93ad84062 -R a2d4860e2c5c02aaae3c6d8ecfa2a1f3 +P 011ce4ae693fb91340c014b28b0197b9f26e3a5614e301d8c6c4e1967c943441 +R b4df0a1efe11f3e544155b403f6ca5be U drh -Z bec2890f29726a611ccbb7e41228d315 +Z de3301ad9dcffc005aaffcf58de933f1 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index d9562a9170..89a04fe5d5 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -011ce4ae693fb91340c014b28b0197b9f26e3a5614e301d8c6c4e1967c943441 +3619baa19e8e7617a7c5918073a826af1cc4d5dc9e9f27b4e4187e988b534d7f From 6b716403f950b88cec23fef6e190c8f32e59c0ad Mon Sep 17 00:00:00 2001 From: drh <> Date: Mon, 16 Dec 2024 17:46:21 +0000 Subject: [PATCH 49/51] Reduce divergence of wal2 from trunk. FossilOrigin-Name: 90d744d67446a270f7547910531e76593dee82ce537e75c11af0d94eca8404cf --- manifest | 14 +++++++------- manifest.uuid | 2 +- src/vdbe.c | 48 ++++++++++++++++++++++++------------------------ src/wal.c | 14 +++++--------- 4 files changed, 37 insertions(+), 41 deletions(-) diff --git a/manifest b/manifest index 2a87516bc5..38c7fc1be5 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Sync\swith\strunk. -D 2024-12-16T13:32:27.273 +C Reduce\sdivergence\sof\swal2\sfrom\strunk. +D 2024-12-16T17:46:21.982 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md e108e1e69ae8e8a59e93c455654b8ac9356a11720d3345df2a4743e9590fb20d @@ -848,7 +848,7 @@ F src/upsert.c 215328c3f91623c520ec8672c44323553f12caeb4f01b1090ebdca99fdf7b4f1 F src/utf.c 8b29d9a5956569ea2700f869669b8ef67a9662ee5e724ff77ab3c387e27094ba F src/util.c e5f6a5eeaa26b69054a43bbd0048cfe3d2851f6961052b35aed8f695df922850 F src/vacuum.c b763b6457bd058d2072ef9364832351fd8d11e8abf70cbb349657360f7d55c40 -F src/vdbe.c f14ac99e5ff68a244d3eeac2081ff5201105f4b3d97099177da0d65924e304c1 +F src/vdbe.c b2d91fce8b2d357b69d3c4aa9d9175736132a1db52baa091ee8cc54f0926120a F src/vdbe.h 9676348d342bd04e21e384c63b57224171ce84fac77853357334ef94c4d33cf4 F src/vdbeInt.h bf294a0c8fc4cc80779e74b04b8bd82c6e1197b3137cefe0b16cdf002fc7dfd6 F src/vdbeapi.c 38c252a202d70b56cfb734460bc888ddbd581afec1a10cd4d6c894c9e0b5baea @@ -860,7 +860,7 @@ F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf8 F src/vdbevtab.c fc46b9cbd759dc013f0b3724549cc0d71379183c667df3a5988f7e2f1bd485f3 F src/vtab.c 316cd48e9320660db3047cd306cd056e4361180cebb4d0f10a39244e10c11422 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c 2ec54ef10bb354fc61424c4b65f407e795abe07059001d0d1c5f99be5bb8b7bf +F src/wal.c f5588f5e2d04afcf49add28b866cc76e4af2e2d8d2531486ac98f502bf8ca93c F src/wal.h 97b8a9903387401377b59507e86b93a148ef1ad4e5ce0f23659a12dcdce56af2 F src/walker.c d5006d6b005e4ea7302ad390957a8d41ed83faa177e412f89bc5600a7462a014 F src/where.c 9ad3dea8003a8913da6a4ca8322e2fe30773f46e88a0d4fbf9db13bdb999efa2 @@ -2216,8 +2216,8 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh 49a486c5069de041aedcbde4de178293e0463ae9918ecad7539eedf0ec77a139 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P 3619baa19e8e7617a7c5918073a826af1cc4d5dc9e9f27b4e4187e988b534d7f 4fab85b3d0d1cad2d185f2456de7ddf281badf8561cc051e10e16655441dcc84 -R b4df0a1efe11f3e544155b403f6ca5be +P eab619453abf6979962a68d298c44553ef0d9eb18112e61420d4de8a9c16f620 +R 80e0eedebdef6156cabc3ad9fa1485f5 U drh -Z a3ec2e08a3dfb16a51c5778333a7d435 +Z d17a5f7761993d7328d68cbb53bd5978 # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 31d69dde16..0dad6e95c8 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -eab619453abf6979962a68d298c44553ef0d9eb18112e61420d4de8a9c16f620 +90d744d67446a270f7547910531e76593dee82ce537e75c11af0d94eca8404cf diff --git a/src/vdbe.c b/src/vdbe.c index 71a68fb4c1..0590f2a61b 100644 --- a/src/vdbe.c +++ b/src/vdbe.c @@ -7959,34 +7959,34 @@ case OP_JournalMode: { /* out2 */ (eNew==PAGER_JOURNALMODE_WAL ? "into" : "out of") ); goto abort_due_to_error; - } + }else{ + + if( isWalMode(eOld) ){ + /* If leaving WAL mode, close the log file. If successful, the call + ** to PagerCloseWal() checkpoints and deletes the write-ahead-log + ** file. An EXCLUSIVE lock may still be held on the database file + ** after a successful return. + */ + rc = sqlite3PagerCloseWal(pPager, db); + if( rc==SQLITE_OK ){ + sqlite3PagerSetJournalMode(pPager, eNew); + } + }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ + /* Cannot transition directly from MEMORY to WAL. Use mode OFF + ** as an intermediate */ + sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); + } - if( isWalMode(eOld) ){ - /* If leaving WAL mode, close the log file. If successful, the call - ** to PagerCloseWal() checkpoints and deletes the write-ahead-log - ** file. An EXCLUSIVE lock may still be held on the database file - ** after a successful return. + /* Open a transaction on the database file. Regardless of the journal + ** mode, this transaction always uses a rollback journal. */ - rc = sqlite3PagerCloseWal(pPager, db); + assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE ); if( rc==SQLITE_OK ){ - sqlite3PagerSetJournalMode(pPager, eNew); + /* 1==rollback, 2==wal, 3==wal2 */ + rc = sqlite3BtreeSetVersion(pBt, + 1 + isWalMode(eNew) + (eNew==PAGER_JOURNALMODE_WAL2) + ); } - }else if( eOld==PAGER_JOURNALMODE_MEMORY ){ - /* Cannot transition directly from MEMORY to WAL. Use mode OFF - ** as an intermediate */ - sqlite3PagerSetJournalMode(pPager, PAGER_JOURNALMODE_OFF); - } - - /* Open a transaction on the database file. Regardless of the journal - ** mode, this transaction always uses a rollback journal. - */ - assert( sqlite3BtreeTxnState(pBt)!=SQLITE_TXN_WRITE ); - if( rc==SQLITE_OK ){ - /* 1==rollback, 2==wal, 3==wal2 */ - rc = sqlite3BtreeSetVersion(pBt, - 1 + isWalMode(eNew) + (eNew==PAGER_JOURNALMODE_WAL2) - ); - } } #endif /* ifndef SQLITE_OMIT_WAL */ diff --git a/src/wal.c b/src/wal.c index ddb0b9dd62..87344947e4 100644 --- a/src/wal.c +++ b/src/wal.c @@ -882,7 +882,6 @@ struct WalIterator { sizeof(ht_slot)*HASHTABLE_NSLOT + HASHTABLE_NPAGE*sizeof(u32) \ ) - /* ** Structured Exception Handling (SEH) is a Windows-specific technique ** for catching exceptions raised while accessing memory-mapped files. @@ -989,6 +988,7 @@ static void sehInjectFault(Wal *pWal){ # define SEH_SET_ON_ERROR(X,Y) #endif /* ifdef SQLITE_USE_SEH */ + /* ** Obtain a pointer to the iPage'th page of the wal-index. The wal-index ** is broken into pages of WALINDEX_PGSZ bytes. Wal-index pages are @@ -4128,7 +4128,6 @@ int sqlite3WalBeginReadTransaction(Wal *pWal, int *pChanged){ return rc; } - /* ** Finish with a read transaction. All this does is release the ** read-lock. @@ -4438,14 +4437,13 @@ int sqlite3WalBeginWriteTransaction(Wal *pWal){ if( memcmp(&pWal->hdr, (void *)walIndexHdr(pWal), sizeof(WalIndexHdr))!=0 ){ rc = SQLITE_BUSY_SNAPSHOT; } - } + } SEH_EXCEPT( rc = SQLITE_IOERR_IN_PAGE; ) if( rc!=SQLITE_OK ){ walUnlockExclusive(pWal, WAL_WRITE_LOCK, 1); pWal->writeLock = 0; } - return rc; } @@ -5072,7 +5070,6 @@ static int walFrames( return rc; } - /* ** Write a set of frames to the log. The caller must hold the write-lock ** on the log file (obtained using sqlite3WalBeginWriteTransaction()). @@ -5198,7 +5195,7 @@ int sqlite3WalCheckpoint( }else{ rc = walCheckpoint(pWal, db, eMode2, xBusy2, pBusyArg, sync_flags,zBuf); } - + /* If no error occurred, set the output variables. */ if( rc==SQLITE_OK || rc==SQLITE_BUSY ){ if( pnLog ){ @@ -5292,7 +5289,6 @@ int sqlite3WalCallback(Wal *pWal){ */ int sqlite3WalExclusiveMode(Wal *pWal, int op){ int rc; - assert( pWal->writeLock==0 ); assert( pWal->exclusiveMode!=WAL_HEAPMEMORY_MODE || op==-1 ); @@ -5355,7 +5351,7 @@ int sqlite3WalSnapshotGet(Wal *pWal, sqlite3_snapshot **ppSnapshot){ assert( pWal->readLock>=0 && pWal->writeLock==0 ); - if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,8)==0 ){ + if( memcmp(&pWal->hdr.aFrameCksum[0],aZero,16)==0 ){ *ppSnapshot = 0; return SQLITE_ERROR; } @@ -5438,7 +5434,7 @@ int sqlite3WalSnapshotCheck(Wal *pWal, sqlite3_snapshot *pSnapshot){ } } } - SEH_EXCEPT( rc = walHandleException(pWal) ); + SEH_EXCEPT( rc = walHandleException(pWal); ) return rc; } From 5cf971bdc7cf1cadbef011d250e2f421cdbd609d Mon Sep 17 00:00:00 2001 From: drh <> Date: Mon, 16 Dec 2024 18:13:47 +0000 Subject: [PATCH 50/51] Further reduction in divergence from trunk. FossilOrigin-Name: e4406a6e2660f1f42614d0034d024cc206109ac71e9f8b289aeed2eba20be8e9 --- manifest | 12 ++++++------ manifest.uuid | 2 +- src/wal.c | 19 +++++++++---------- 3 files changed, 16 insertions(+), 17 deletions(-) diff --git a/manifest b/manifest index 38c7fc1be5..286528c5d1 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Reduce\sdivergence\sof\swal2\sfrom\strunk. -D 2024-12-16T17:46:21.982 +C Further\sreduction\sin\sdivergence\sfrom\strunk. +D 2024-12-16T18:13:47.246 F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea F LICENSE.md e108e1e69ae8e8a59e93c455654b8ac9356a11720d3345df2a4743e9590fb20d @@ -860,7 +860,7 @@ F src/vdbetrace.c fe0bc29ebd4e02c8bc5c1945f1d2e6be5927ec12c06d89b03ef2a4def34bf8 F src/vdbevtab.c fc46b9cbd759dc013f0b3724549cc0d71379183c667df3a5988f7e2f1bd485f3 F src/vtab.c 316cd48e9320660db3047cd306cd056e4361180cebb4d0f10a39244e10c11422 F src/vxworks.h d2988f4e5a61a4dfe82c6524dd3d6e4f2ce3cdb9 -F src/wal.c f5588f5e2d04afcf49add28b866cc76e4af2e2d8d2531486ac98f502bf8ca93c +F src/wal.c 3ff22607c591f208bf81b5ea2160e5715d12bbb5b762394f79b2bd13d3f7568b F src/wal.h 97b8a9903387401377b59507e86b93a148ef1ad4e5ce0f23659a12dcdce56af2 F src/walker.c d5006d6b005e4ea7302ad390957a8d41ed83faa177e412f89bc5600a7462a014 F src/where.c 9ad3dea8003a8913da6a4ca8322e2fe30773f46e88a0d4fbf9db13bdb999efa2 @@ -2216,8 +2216,8 @@ F tool/version-info.c 3b36468a90faf1bbd59c65fd0eb66522d9f941eedd364fabccd7227350 F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee87c1b31a7 F tool/warnings.sh 49a486c5069de041aedcbde4de178293e0463ae9918ecad7539eedf0ec77a139 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f -P eab619453abf6979962a68d298c44553ef0d9eb18112e61420d4de8a9c16f620 -R 80e0eedebdef6156cabc3ad9fa1485f5 +P 90d744d67446a270f7547910531e76593dee82ce537e75c11af0d94eca8404cf +R a440747468661c427837f99ee412b9ae U drh -Z d17a5f7761993d7328d68cbb53bd5978 +Z 0ce636ddd1ec473c89130c2c2fee8b2e # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index 0dad6e95c8..82603d2e32 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -90d744d67446a270f7547910531e76593dee82ce537e75c11af0d94eca8404cf +e4406a6e2660f1f42614d0034d024cc206109ac71e9f8b289aeed2eba20be8e9 diff --git a/src/wal.c b/src/wal.c index 87344947e4..891552b2d1 100644 --- a/src/wal.c +++ b/src/wal.c @@ -3746,15 +3746,16 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){ pWal->readLock = eLock; } assert( pWal->minFrame==0 && walFramePage(pWal->minFrame)==0 ); - }else{ + }else + { u32 mxReadMark; /* Largest aReadMark[] value */ int mxI; /* Index of largest aReadMark[] value */ int i; /* Loop counter */ u32 mxFrame; /* Wal frame to lock to */ if( !useWal && pInfo->nBackfill==pWal->hdr.mxFrame - #ifdef SQLITE_ENABLE_SNAPSHOT +#ifdef SQLITE_ENABLE_SNAPSHOT && ((pWal->bGetSnapshot==0 && pWal->pSnapshot==0) || pWal->hdr.mxFrame==0) - #endif +#endif ){ /* The WAL has been completely backfilled (or it is empty). ** and can be safely ignored. @@ -3770,7 +3771,7 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){ ** snapshot. Since holding READ_LOCK(0) prevents a checkpoint from ** happening, this is usually correct. ** - ** However, if frames have been appended to the log (or if the log + ** However, if frames have been appended to the log (or if the log ** is wrapped and written for that matter) before the READ_LOCK(0) ** is obtained, that is not necessarily true. A checkpointer may ** have started to backfill the appended frames but crashed before @@ -3794,11 +3795,11 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){ mxReadMark = 0; mxI = 0; mxFrame = pWal->hdr.mxFrame; - #ifdef SQLITE_ENABLE_SNAPSHOT +#ifdef SQLITE_ENABLE_SNAPSHOT if( pWal->pSnapshot && pWal->pSnapshot->mxFramepSnapshot->mxFrame; } - #endif +#endif for(i=1; iaReadMark+i); SEH_INJECT_FAULT; if( mxReadMark<=thisMark && thisMark<=mxFrame ){ @@ -3841,7 +3842,6 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){ #endif assert((rc&0xFF)!=SQLITE_BUSY||rc==SQLITE_BUSY||rc==SQLITE_BUSY_TIMEOUT); return (rc&0xFF)==SQLITE_BUSY ? WAL_RETRY : rc; - } /* Now that the read-lock has been obtained, check that neither the ** value in the aReadMark[] array or the contents of the wal-index @@ -3863,9 +3863,9 @@ static int walTryBeginRead(Wal *pWal, int *pChanged, int useWal, int *pCnt){ ** to read any frames earlier than minFrame from the wal file - they ** can be safely read directly from the database file. ** - ** Because a ShmBarrier() call is made between taking the copy of + ** Because a ShmBarrier() call is made between taking the copy of ** nBackfill and checking that the wal-header in shared-memory still - ** matches the one cached in pWal->hdr, it is guaranteed that the + ** matches the one cached in pWal->hdr, it is guaranteed that the ** checkpointer that set nBackfill was not working with a wal-index ** header newer than that cached in pWal->hdr. If it were, that could ** cause a problem. The checkpointer could omit to checkpoint @@ -4671,7 +4671,6 @@ static int walRestartLog(Wal *pWal){ testcase( rc==SQLITE_PROTOCOL ); testcase( rc==SQLITE_OK ); } - return rc; } From db3f0dfac02327756d80380225fbb010a19735cd Mon Sep 17 00:00:00 2001 From: dan Date: Mon, 22 Jun 2026 16:43:20 +0000 Subject: [PATCH 51/51] Fix a problem with readonly_shm=1 connections on this branch causing SQLITE_PROTOCOL to be returned in some cases. FossilOrigin-Name: 0c3a346d659fe041f3a8bccf357a2b435e49fbfdf34de4c55b1435b1db1e21c2 --- manifest | 18 +++++++++--------- manifest.uuid | 2 +- src/wal.c | 10 +++++++--- test/walro.test | 45 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+), 13 deletions(-) diff --git a/manifest b/manifest index b7c354049a..a90df36329 100644 --- a/manifest +++ b/manifest @@ -1,5 +1,5 @@ -C Merge\sthe\slatest\strunk\senhancements\sand\sfixes\sinto\sthe\swal2\sbranch. -D 2026-05-29T18:08:48.599 +C Fix\sa\sproblem\swith\sreadonly_shm=1\sconnections\son\sthis\sbranch\scausing\sSQLITE_PROTOCOL\sto\sbe\sreturned\sin\ssome\scases. +D 2026-06-22T16:43:20.559 F .fossil-settings/binary-glob 61195414528fb3ea9693577e1980230d78a1f8b0a54c78cf1b9b24d0a409ed6a x F .fossil-settings/empty-dirs dbb81e8fc0401ac46a1491ab34a7f2c7c0452f2f06b54ebb845d024ca8283ef1 F .fossil-settings/ignore-glob 35175cdfcf539b2318cb04a9901442804be81cd677d8b889fcc9149c21f239ea @@ -376,7 +376,7 @@ F ext/misc/compress.c 8191118b9b73e7796c961790db62d35d9b0fb724b045e005a5713dc9e0 F ext/misc/csv.c 5e9d4dd749e762c144104c0f01db5bf4458735b19081ebe481a64e589a66687a F ext/misc/dbdump.c 678f1b9ae2317b4473f65d03132a2482c3f4b08920799ed80feedd2941a06680 F ext/misc/decimal.c 432e5b03a0e2a68a1846a9852a565a1b546ca9b295deda834e4653f0f5577daa -F ext/misc/diskused.c 56433ee0d1ae21975c549e873d99d07b30b1b7fee9cd58b103797d7c7a2eeab6 w ext/misc/analyze.c +F ext/misc/diskused.c 56433ee0d1ae21975c549e873d99d07b30b1b7fee9cd58b103797d7c7a2eeab6 F ext/misc/eval.c 04bc9aada78c888394204b4ed996ab834b99726fb59603b0ee3ed6e049755dc1 F ext/misc/explain.c 9670c8ff7b255eea7845abc5123a4958e74016c16990b10497e56380f91704b9 F ext/misc/fileio.c 936c0a7b3382a047d833ad33f62ba59a3847b79ea745bf529797cd344966fbb0 @@ -819,7 +819,7 @@ F src/vdbetrace.c 49e689f751505839742f4a243a1a566e57d5c9eaf0d33bbaa26e2de3febf7b F src/vdbevtab.c fc46b9cbd759dc013f0b3724549cc0d71379183c667df3a5988f7e2f1bd485f3 F src/vtab.c 5437ce986db2f70e639ce8a3fe68dcdfe64b0f1abb14eaebecdabd5e0766cc68 F src/vxworks.h 9d18819c5235b49c2340a8a4d48195ec5d5afb637b152406de95a9436beeaeab -F src/wal.c 6fdaac2cb9a53f47052138a41ed7d102c701c6f98fba87826511c47ad31bf563 +F src/wal.c 610467d13cef79cccb9d6eddfe2d83956cc68be1f000b30cb78e42ece924e40c F src/wal.h 97b8a9903387401377b59507e86b93a148ef1ad4e5ce0f23659a12dcdce56af2 F src/walker.c d5006d6b005e4ea7302ad390957a8d41ed83faa177e412f89bc5600a7462a014 F src/where.c 33e4a6558ee69f33d6a4e7069e3a40a55959d14e5653a9a83926e70305d471f3 @@ -2036,7 +2036,7 @@ F test/walpersist.test 8d78a1ec91299163451417b451a2bac3481f8eb9f455b1ca507a6625c F test/walprotocol.test 1b3f922125e341703f6e946d77fdc564d38fb3e07a9385cfdc6c99cac1ecf878 F test/walprotocol2.test 7e4bedd5ee83607e2928ac438bf7332a396b980d3e02aa0746509ce11ad1f13c F test/walrestart.test 5168c0c2414d1971d8dec949c1070a0144cf15402361ba0d0e6a8054f5598a64 -F test/walro.test 78a84bc0fdae1385c06b017215c426b6845734d6a5a3ac75c918dd9b801b1b9d +F test/walro.test 1f011c21e00596bf7135ef1a8b781ffa96e5a22eabe2ae80ab84cf67b12c577e F test/walro2.test 33955a6fd874dd9724005e17f77fef89d334b3171454a1256fe4941a96766cdc F test/walrofault.test c70cb6e308c443867701856cce92ad8288cd99488fa52afab77cca6cfd51af68 F test/walseh1.test bae700eb99519b6d5cd3f893c04759accc5a59c391d4189fe4dd6995a533442b @@ -2222,8 +2222,8 @@ F tool/warnings-clang.sh bbf6a1e685e534c92ec2bfba5b1745f34fb6f0bc2a362850723a9ee F tool/warnings.sh a554d13f6e5cf3760f041b87939e3d616ec6961859c3245e8ef701d1eafc2ca2 F tool/win/sqlite.vsix deb315d026cc8400325c5863eef847784a219a2f F tool/winmain.c 00c8fb88e365c9017db14c73d3c78af62194d9644feaf60e220ab0f411f3604c -P 28d1d29ad465b150ea705af8352c0e6599b3f42c15474b2ae89e8a2c2b9b097c c7839f7a1749b38f8a36c06d93a7b59095e91ce753e8dd020de273ca8f73a239 -R da9eb2c8c6fc545fa76a8916647efb64 -U drh -Z 81655c913c3efc0b9200f9ede46569c5 +P d8cf1a62aff38de008f9b826c4846b15e37c89dc0f8bb412b79349da4d1580b0 +R 2289bb0b48ffe72c90b31d57a179970c +U dan +Z d1fff2703bb94dcbe876b7cf9d75943d # Remove this line to create a well-formed Fossil manifest. diff --git a/manifest.uuid b/manifest.uuid index f668496b3a..1961b90dd7 100644 --- a/manifest.uuid +++ b/manifest.uuid @@ -1 +1 @@ -d8cf1a62aff38de008f9b826c4846b15e37c89dc0f8bb412b79349da4d1580b0 +0c3a346d659fe041f3a8bccf357a2b435e49fbfdf34de4c55b1435b1db1e21c2 diff --git a/src/wal.c b/src/wal.c index 940ae7bd59..20f82117fe 100644 --- a/src/wal.c +++ b/src/wal.c @@ -1285,6 +1285,12 @@ static int walDecodeFrame( return 0; } + /* Need a valid page size + */ + if( !pWal->szPage ){ + return 0; + } + /* A frame is only valid if a checksum of the WAL header, ** all prior frames, the first 16 bytes of this frame-header, ** and the frame-data matches the checksum in the last 8 @@ -1713,8 +1719,6 @@ static int walIndexRecoverOne(Wal *pWal, int iWal, u32 *pnCkpt, int *pbZero){ assert( iWal==0 || iWal==1 ); memset(&pWal->hdr, 0, sizeof(WalIndexHdr)); - sqlite3_randomness(8, pWal->hdr.aSalt); - rc = sqlite3OsFileSize(pWalFd, &nSize); if( rc==SQLITE_OK ){ if( nSize>WAL_HDRSIZE ){ @@ -3510,7 +3514,7 @@ static int walBeginShmUnreliable(Wal *pWal, int *pChanged){ /* Allocate a buffer to read frames into */ assert( (pWal->szPage & (pWal->szPage-1))==0 ); - assert( pWal->szPage>=512 && pWal->szPage<=65536 ); + assert( (pWal->szPage>=512 && pWal->szPage<=65536) || pWal->szPage==0 ); szFrame = pWal->szPage + WAL_FRAME_HDRSIZE; aFrame = (u8 *)sqlite3_malloc64(szFrame); if( aFrame==0 ){ diff --git a/test/walro.test b/test/walro.test index a39b844d9d..80e2b8d02c 100644 --- a/test/walro.test +++ b/test/walro.test @@ -297,4 +297,49 @@ do_multiclient_test tn { } {} } +#------------------------------------------------------------------------- + +proc write_hex_file {fname hex} { + set fd [open $fname wb] + fconfigure $fd -translation binary -encoding binary + puts -nonewline $fd [decode_hexdb $hex] + close $fd +} + +write_hex_file test.db { +| size 8192 pagesize 4096 filename badwal.db +| page 1 offset 0 +| 0: 53 51 4c 69 74 65 20 66 6f 72 6d 61 74 20 33 00 SQLite format 3. +| 16: 10 00 01 01 00 40 20 20 00 00 00 02 00 00 00 02 .....@ ........ +| 32: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 04 ................ +| 48: 00 00 00 00 00 00 00 00 00 00 00 01 00 00 00 00 ................ +| 80: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 02 ................ +| 96: 00 2e 76 89 0d 00 00 00 01 0f c3 00 0f c3 00 00 ..v............. +| 4032: 00 00 00 3b 01 06 17 0f 0f 01 67 74 61 62 6c 65 ...;......gtable +| 4048: 74 74 02 43 52 45 41 54 45 20 54 41 42 4c 45 20 tt.CREATE TABLE +| 4064: 74 28 78 20 49 4e 54 45 47 45 52 20 50 52 49 4d t(x INTEGER PRIM +| 4080: 41 52 59 20 4b 45 59 2c 20 79 20 54 45 58 54 29 ARY KEY, y TEXT) +} + +write_hex_file test.db-wal { +| size 56 pagesize 1024 filename badwal.db-wal +| page 1 offset 0 +| 0: 37 7f 06 82 00 2d e2 18 00 00 00 00 00 00 00 00 7....-.......... +| 32: 00 00 00 01 00 00 00 00 00 00 00 00 00 00 00 00 ................ +| end badwal.db-wal +} + +write_hex_file test.db-shm { +| size 32 pagesize 1024 filename badwal.db-wal +| page 1 offset 0 +| 0: 53 48 4d 2d 55 4e 55 53 45 44 2d 49 4e 2d 52 45 SHM-UNUSED-IN-RE +| 16: 41 44 4f 4e 4c 59 2d 43 41 4e 54 49 4e 49 54 0a ADONLY-CANTINIT. +| end badwal.db-shm +} + +sqlite3 db file:test.db?mode=ro&readonly_shm=1 -uri 1 +do_execsql_test 3.0 { + SELECT count(*) FROM sqlite_schema +} {1} + finish_test