掃二維碼與項(xiàng)目經(jīng)理溝通
我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流
sqlite 提供了一種 redo log 型事務(wù)實(shí)現(xiàn),支持讀寫的并發(fā),見 write-ahead log(https://sqlite.org/wal.html)。

為定陶等地區(qū)用戶提供了全套網(wǎng)頁設(shè)計(jì)制作服務(wù),及定陶網(wǎng)站建設(shè)行業(yè)解決方案。主營業(yè)務(wù)為成都做網(wǎng)站、網(wǎng)站建設(shè)、外貿(mào)營銷網(wǎng)站建設(shè)、定陶網(wǎng)站設(shè)計(jì),以傳統(tǒng)方式定制建設(shè)網(wǎng)站,并提供域名空間備案等一條龍服務(wù),秉承以專業(yè)、用心的態(tài)度為用戶提供真誠的服務(wù)。我們深信只要達(dá)到每一位用戶的要求,就會得到認(rèn)可,從而選擇與我們長期合作。這樣,我們也可以走得更遠(yuǎn)!
sqlite wal 是一種簡單的 redo log 事務(wù)實(shí)現(xiàn),redo log 概念這里簡述下。數(shù)據(jù)庫事務(wù)需要滿足滿足 acid,其中原子性(a),即一次事務(wù)內(nèi)的多個修改,要么全部提交成功要么全部提交失敗,不存在部分提交到 db 的情況。 redo log 的解決思路是將修改后的日志按序先寫入 log 文件(wal 文件),每個完成的事務(wù)會添加 checksum,可鑒別事務(wù)的完整性。事務(wù)寫入日志文件后,即代表提交成功,讀取時日志和 db 文件合并的結(jié)果構(gòu)成了 db 的完整內(nèi)容。同時定期 checkpoint,同步 wal 中的事務(wù)到 db 文件,使 wal 文件保持在合理的大小。日志文件持久化到磁盤后,已提交成功的事務(wù)按序 checkpoint 執(zhí)行的結(jié)果都是一樣的,不受 crash 和掉電的影響。
sqlite 的 wal 也是這種思路的實(shí)現(xiàn),只是 sqlite 提供的是一種簡化實(shí)現(xiàn),同時只允許一個寫者操作日志文件,日志也是 page 這種物理日志。redo log 還能將 undo log 的隨機(jī)寫轉(zhuǎn)化為順序?qū)懀哂懈叩膶懭胄阅埽?/strong>這里不贅述。
想對 redo log 進(jìn)一步了解,可以參考以下資料:
??https://zhuanlan.zhihu.com/p/35574452??
??https://developer.aliyun.com/article/1009683??
sqlite wal 寫操作不直接寫入 db 主文件,而是寫到“db-wal”文件(以下簡稱'wal'文件)的末尾。讀操作時,將結(jié)合 db 主文件以及 wal 的內(nèi)容返回結(jié)果。wal 模式同時具有簡單的 mvvc 實(shí)現(xiàn),支持文件級別的讀寫并發(fā),提供了相對 delete(rollback) 模式 (undo log 事務(wù)) 更高的并發(fā)性。 具體可看圖加深理解。
下圖中:
關(guān)于寫
關(guān)于讀
關(guān)于 checkpoint:
wal 的實(shí)現(xiàn)大部分代碼集中在 wal.c 中,從 sqlite 的架構(gòu)劃分應(yīng)該主要算是 pager 層的實(shí)現(xiàn)。
https://www.sqlite.org/arch.html。wal 實(shí)現(xiàn)從邏輯上由 3 部分組成:
文件格式定義,官方文檔見:
??https://www.sqlite.org/walformat.html??
??https://www.sqlite.org/fileformat2.html??
這一層細(xì)節(jié)比較多,主要是些二進(jìn)制定義。核心是 wal 格式提供了一種 page 格式的 redo log 組織格式,保證 crash 后 recover 過程滿足一致性。
wal-index 文件(db-shm)只是一種對 wal 文件的快速索引,后文為了省事,也統(tǒng)稱 wal 文件。
即 wal 和 db 文件對外表現(xiàn)為一個統(tǒng)一的文件抽象,并提供文件級別的 mvcc,對 pager 層屏蔽 wal 細(xì)節(jié)。
由于 wal 和 db 一樣都是以 pgno 的方式索引 page,按 pgno 替換就可以構(gòu)造出不同版本的 b 樹,比較簡單。mvcc 主要通過 read lock 的 read mark 實(shí)現(xiàn),前面有介紹過, 后面并發(fā)控制部分會詳細(xì)舉例介紹。
具體實(shí)現(xiàn)可看:
寫入:https://github.com/sqlite/sqlite/blob/version-3.15.2/src/pager.c#L3077
讀取:https://github.com/sqlite/sqlite/blob/version-3.15.2/src/wal.c#L2593
通過文件鎖保證并發(fā)操作不會損壞數(shù)據(jù)庫文件,下一節(jié)詳細(xì)講解。
wal 支持讀讀、讀寫的并發(fā),相比最初的 rollback journal 模式提供了更大的并發(fā)力度。但 wal 實(shí)現(xiàn)的是文件級別的并發(fā),沒有 mysql 表鎖行鎖的概念,一個 db 文件同時的并發(fā)寫事務(wù)同時只能存在一個,不支持寫的同時并發(fā)。checkpoint 也可能會 block 讀寫。
wal 并發(fā)實(shí)現(xiàn)上主要通過文件鎖,和文件級別 mvcc 來實(shí)現(xiàn)文件級別的讀寫并發(fā)。 鎖即下文源碼中的 WAL_CKPT_LOCK,WAL_WRITE_LOCK 和WAL_READ_LOCK,出于簡化問題考慮省略了 WAL_RECOVER_LOCK 等相關(guān)性不大的其他鎖的討論。mvcc 即通過文件多副本和 read mark 實(shí)現(xiàn),后文也會詳細(xì)介紹。
官方介紹:https://www.sqlite.org/walformat.html
可看 2.3.1節(jié) How the various locks are used
也可看下面簡化分析:
數(shù)據(jù)庫的訪問,可以分為 3 類:讀、寫和checkpoint。事務(wù)對鎖的持有不總是在事務(wù)一開始就持有,后文為了簡化分析,會假設(shè)讀寫事務(wù)對鎖的持有在事務(wù)開始時是已知的,并且與事務(wù)同生命周期。實(shí)際在讀事務(wù)某些執(zhí)行路徑上也可能會持有 write lock,這里專注主線邏輯。
這部分可以和源碼分析部分參照起來看,是整個 wal 里面相對復(fù)雜的部分,重點(diǎn),需要來回反復(fù)看。
commit transaction:表示已經(jīng)提交但沒有 checkpoint 的事務(wù),藍(lán)框中表示事務(wù)修改的頁面。
ongoing transition : 表示正在進(jìn)行中的事務(wù),同時也表示一個活躍的數(shù)據(jù)庫連接,藍(lán)線表示 read mark 的位置。
pgx.y: 表示 page 的頁號和版本。
如圖可知:
這部分要和源碼分析結(jié)合,如果此時發(fā)起 checkpoint。
源碼對應(yīng) sqlite 3.15.2,通過直接調(diào)用 checkpoint 觀察整個過程。
??https://github.com/sqlite/sqlite/tree/version-3.15.2/src??
??https://github.com/sqlite/sqlite/blob/version-3.15.2/src/main.c#L2065??
主要是加鎖和一些參數(shù)校驗(yàn)。
??https://github.com/sqlite/sqlite/blob/version-3.15.2/src/main.c#L2146??
ndb 上循環(huán) checkpoint,大多數(shù)時候只有一個 db 文件。
??https://github.com/sqlite/sqlite/blob/version-3.15.2/src/btree.c#L9472??
檢查 btree 是否 locked,也是前置檢查邏輯。
??https://github.com/sqlite/sqlite/blob/version-3.15.2/src/pager.c#L7198??
也是前置的處理邏輯。不過有個和 checkpoint 邏輯有關(guān)的。
/* 只在非SQLITE_CHECKPOINT_PASSIVE模式時設(shè)置xBusyHandler
* 即SQLITE_CHECKPOINT_PASSIVE時如果獲取不到鎖,立即返回,不進(jìn)行等待并retry
*/
if( pPager->pWal ){
rc = sqlite3WalCheckpoint(pPager->pWal, db, eMode,
(eMode==SQLITE_CHECKPOINT_PASSIVE ? 0 : pPager->xBusyHandler),
pPager->pBusyHandlerArg,
pPager->walSyncFlags, pPager->pageSize, (u8 *)pPager->pTmpSpace,
pnLog, pnCkpt
);
}
??https://github.com/sqlite/sqlite/blob/version-3.15.2/src/wal.c#L3192??
int sqlite3WalCheckpoint(
Wal *pWal, /* Wal connection */
int eMode, /* PASSIVE, FULL, RESTART, or TRUNCATE */
int (*xBusy)(void*), /* Function to call when busy */
void *pBusyArg, /* Context argument for xBusyHandler */
int sync_flags, /* Flags to sync db file with (or 0) */
int nBuf, /* Size of temporary buffer */
u8 *zBuf, /* Temporary buffer to use */
int *pnLog, /* OUT: Number of frames in WAL */
int *pnCkpt /* OUT: Number of backfilled frames in WAL */
){
int rc; /* Return code */
int isChanged = 0; /* True if a new wal-index header is loaded */
int eMode2 = eMode; /* Mode to pass to walCheckpoint() */
int (*xBusy2)(void*) = xBusy; /* Busy handler for eMode2 */
assert( pWal->ckptLock==0 );
assert( pWal->writeLock==0 );
/* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
** in the SQLITE_CHECKPOINT_PASSIVE mode. */
assert( eMode!=SQLITE_CHECKPOINT_PASSIVE || xBusy==0 );
if( pWal->readOnly ) return SQLITE_READONLY;
WALTRACE(("WAL%p: checkpoint begins\n", pWal));
/* IMPLEMENTATION-OF: R-62028-47212 All calls obtain an exclusive
** "checkpoint" lock on the database file. */
// 獨(dú)占獲取WAL_CKPT_LOCK鎖
rc = walLockExclusive(pWal, WAL_CKPT_LOCK, 1);
if( rc ){
/* EVIDENCE-OF: R-10421-19736 If any other process is running a
** checkpoint operation at the same time, the lock cannot be obtained and
** SQLITE_BUSY is returned.
** EVIDENCE-OF: R-53820-33897 Even if there is a busy-handler configured,
** it will not be invoked in this case.
*/
testcase( rc==SQLITE_BUSY );
testcase( xBusy!=0 );
return rc;
}
pWal->ckptLock = 1;
/* IMPLEMENTATION-OF: R-59782-36818 The SQLITE_CHECKPOINT_FULL, RESTART and
** TRUNCATE modes also obtain the exclusive "writer" lock on the database
** file.
**
** EVIDENCE-OF: R-60642-04082 If the writer lock cannot be obtained
** immediately, and a busy-handler is configured, it is invoked and the
** writer lock retried until either the busy-handler returns 0 or the
** lock is successfully obtained.
*/
// 非SQLITE_CHECKPOINT_PASSIVE時,獨(dú)占獲取WAL_WRITE_LOCK鎖,并進(jìn)行busy retry
if( eMode!=SQLITE_CHECKPOINT_PASSIVE ){
rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_WRITE_LOCK, 1);
if( rc==SQLITE_OK ){
pWal->writeLock = 1;
}else if( rc==SQLITE_BUSY ){
eMode2 = SQLITE_CHECKPOINT_PASSIVE;
xBusy2 = 0;
rc = SQLITE_OK;
}
}
//如果wal-index顯示db有變化,unfetch db文件,和主線邏輯關(guān)系不大
/* Read the wal-index header. */
if( rc==SQLITE_OK ){
rc = walIndexReadHdr(pWal, &isChanged);
if( isChanged && pWal->pDbFd->pMethods->iVersion>=3 ){
sqlite3OsUnfetch(pWal->pDbFd, 0, 0);
}
}
/* Copy data from the log to the database file. */
if( rc==SQLITE_OK ){
if( pWal->hdr.mxFrame && walPagesize(pWal)!=nBuf ){
rc = SQLITE_CORRUPT_BKPT;
}else{
// checkpoint
rc = walCheckpoint(pWal, eMode2, xBusy2, pBusyArg, sync_flags, zBuf);
}
/* 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);
}
}
// release wal index,非主線邏輯
if( isChanged ){
/* 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.
*/
memset(&pWal->hdr, 0, sizeof(WalIndexHdr));
}
// 釋放鎖,返回
/* Release the locks. */
sqlite3WalEndWriteTransaction(pWal);
walUnlockExclusive(pWal, WAL_CKPT_LOCK, 1);
pWal->ckptLock = 0;
WALTRACE(("WAL%p: checkpoint %s\n", pWal, rc ? "failed" : "ok"));
return (rc==SQLITE_OK && eMode!=eMode2 ? SQLITE_BUSY : rc);
}
??https://github.com/sqlite/sqlite/blob/version-3.15.2/src/wal.c#L1724??
static int walCheckpoint(
Wal *pWal, /* Wal connection */
int eMode, /* One of PASSIVE, FULL or RESTART */
int (*xBusy)(void*), /* Function to call when busy */
void *pBusyArg, /* Context argument for xBusyHandler */
int sync_flags, /* Flags for OsSync() (or 0) */
u8 *zBuf /* Temporary buffer to use */
){
int rc = SQLITE_OK; /* Return code */
int szPage; /* Database page-size */
WalIterator *pIter = 0; /* Wal iterator context */
u32 iDbpage = 0; /* Next database page to write */
u32 iFrame = 0; /* Wal frame containing data for iDbpage */
u32 mxSafeFrame; /* Max frame that can be backfilled */
u32 mxPage; /* Max database page to write */
int i; /* Loop counter */
volatile WalCkptInfo *pInfo; /* The checkpoint status information */
szPage = walPagesize(pWal);
testcase( szPage<=32768 );
testcase( szPage>=65536 );
pInfo = walCkptInfo(pWal);
if( pInfo->nBackfillhdr.mxFrame ){
/* Allocate the iterator */
rc = walIteratorInit(pWal, &pIter);
if( rc!=SQLITE_OK ){
return rc;
}
assert( pIter );
/* EVIDENCE-OF: R-62920-47450 The busy-handler callback is never invoked
** 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.
*/
mxSafeFrame = pWal->hdr.mxFrame;
mxPage = pWal->hdr.nPage;
/* 計(jì)算mxSafeFrame
* 會嘗試獨(dú)占的獲取aReadMark鎖,如果獲取到,則代表原先持有對應(yīng)aReadMark鎖的事務(wù)已經(jīng)結(jié)束。
* 會不斷的用busy rerty邏輯等待對應(yīng)的讀鎖釋放。
* 如果對應(yīng)事物一直沒有釋放aReadMark鎖,最終的 mxSafeFrame = MIN(unfinished_aReadMarks)
*/
for(i=1; i/* Thread-sanitizer reports that the following is an unsafe read,
** as some other thread may be in the process of updating the value
** of the aReadMark[] slot. The assumption here is that if that is
** happening, the other client may only be increasing the value,
** not decreasing it. So assuming either that either the "old" or
** "new" version of the value is read, and not some arbitrary value
** that would never be written by a real client, things are still
** safe. */
u32 y = pInfo->aReadMark[i];
if( mxSafeFrame>y ){
assert( y<=pWal->hdr.mxFrame );
// 嘗試獲取 WAL_READ_LOCK(i)鎖,并進(jìn)行忙等待
rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(i), 1);
if( rc==SQLITE_OK ){
// 成功獲取 WAL_READ_LOCK(i)鎖,設(shè)置為READMARK_NOT_USED;i==1,是個treak,不影響主流程
pInfo->aReadMark[i] = (i==1 ? mxSafeFrame : READMARK_NOT_USED);
walUnlockExclusive(pWal, WAL_READ_LOCK(i), 1);
}else if( rc==SQLITE_BUSY ){
// 一直沒有獲取對應(yīng)WAL_READ_LOCK(i)鎖,設(shè)置mxSafeFrame為y
mxSafeFrame = y;
xBusy = 0;
}else{
goto walcheckpoint_out;
}
}
}
// 開始從wal文件寫回db文件,此時獨(dú)占的持有WAL_READ_LOCK(0)
if( pInfo->nBackfill&& (rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(0),1))==SQLITE_OK
){
i64 nSize; /* Current size of database file */
u32 nBackfill = pInfo->nBackfill;
pInfo->nBackfillAttempted = mxSafeFrame;
/* Sync the WAL to disk */
if( sync_flags ){
rc = sqlite3OsSync(pWal->pWalFd, 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.
*/
if( rc==SQLITE_OK ){
i64 nReq = ((i64)mxPage * szPage);
rc = sqlite3OsFileSize(pWal->pDbFd, &nSize);
if( rc==SQLITE_OK && nSizesqlite3OsFileControlHint(pWal->pDbFd, SQLITE_FCNTL_SIZE_HINT, &nReq);
}
}
// 邏輯比較簡單,遍歷并回寫
/* 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 );
if( iFrame<=nBackfill || iFrame>mxSafeFrame || iDbpage>mxPage ){
continue;
}
iOffset = walFrameOffset(iFrame, szPage) + WAL_FRAME_HDRSIZE;
/* testcase( IS_BIG_INT(iOffset) ); // requires a 4GiB WAL file */
rc = sqlite3OsRead(pWal->pWalFd, zBuf, szPage, iOffset);
if( rc!=SQLITE_OK ) break;
iOffset = (iDbpage-1)*(i64)szPage;
testcase( IS_BIG_INT(iOffset) );
rc = sqlite3OsWrite(pWal->pDbFd, zBuf, szPage, iOffset);
if( rc!=SQLITE_OK ) break;
}
/* If work was actually accomplished... */
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 && sync_flags ){
rc = sqlite3OsSync(pWal->pDbFd, sync_flags);
}
}
if( rc==SQLITE_OK ){
/* 更新nBackfill為已經(jīng)checkpoint的部分
* nBackfill記錄當(dāng)前已經(jīng)checkpoint的部分
*/
pInfo->nBackfill = mxSafeFrame;
}
}
/* Release the reader lock held while backfilling */
// 釋放 WAL_READ_LOCK(0)
walUnlockExclusive(pWal, WAL_READ_LOCK(0), 1);
}
if( rc==SQLITE_BUSY ){
/* Reset the return code so as not to report a checkpoint failure
** just because there are active readers. */
rc = SQLITE_OK;
}
}
/* If this is an SQLITE_CHECKPOINT_RESTART or TRUNCATE operation, and the
** entire wal file has been copied into the database file, then block
** until all readers have finished using the wal file. This ensures that
** the next process to write to the database restarts the wal file.
*/
// 非passive的checkpoint的區(qū)別都在這里
if( rc==SQLITE_OK && eMode!=SQLITE_CHECKPOINT_PASSIVE ){
assert( pWal->writeLock );
if( pInfo->nBackfillhdr.mxFrame ){
// 沒有全部checkpoint
rc = SQLITE_BUSY;
}else if( eMode>=SQLITE_CHECKPOINT_RESTART ){
// RESTART or TRUNCATE
u32 salt1;
sqlite3_randomness(4, &salt1);
assert( pInfo->nBackfill==pWal->hdr.mxFrame );
// 獲取所有讀鎖, 保證下一個事物能夠重新開始restart,即循環(huán)利用wal文件
rc = walBusyLock(pWal, xBusy, pBusyArg, WAL_READ_LOCK(1), WAL_NREADER-1);
if( rc==SQLITE_OK ){
if( eMode==SQLITE_CHECKPOINT_TRUNCATE ){
/* IMPLEMENTATION-OF: R-44699-57140 This mode works the same way as
** SQLITE_CHECKPOINT_RESTART with the addition that it also
** truncates the log file to zero bytes just prior to a
** successful return.
**
** In theory, it might be safe to do this without updating the
** wal-index header in shared memory, as all subsequent reader or
** writer clients should see that the entire log file has been
** checkpointed and behave accordingly. This seems unsafe though,
** as it would leave the system in a state where the contents of
** the wal-index header do not match the contents of the
** file-system. To avoid this, update the wal-index header to
** indicate that the log file contains zero valid frames. */
walRestartHdr(pWal, salt1);
// Truncate wal文件
rc = sqlite3OsTruncate(pWal->pWalFd, 0);
}
walUnlockExclusive(pWal, WAL_READ_LOCK(1), WAL_NREADER-1);
}
}
}
walcheckpoint_out:
walIteratorFree(pIter);
return rc;
}
可看看上面不同操作對鎖的持有情況:
wal 文件提供的操作模型非常簡單,只有在一次完整的 checkpoint 后才會重頭開始循環(huán)利用 wal 文件,如果 checkpoint 一直沒有提交當(dāng)前的 wal 文件中所有更新,會導(dǎo)致 wal 文件無限增大。同時只有在 truncate 模式 checkpoint 才會縮減 wal 文件。
大概有以下原因會導(dǎo)致 wal 不能完全提交,核心都是 checkpoint 競爭不到鎖。
在 checkpoint 中不能如預(yù)料中的獲得鎖,主要有兩種可能:
綜上要解決 wal 無限增大主要有:

我們在微信上24小時期待你的聲音
解答本文疑問/技術(shù)咨詢/運(yùn)營咨詢/技術(shù)建議/互聯(lián)網(wǎng)交流