From 25fdcac30549f4d73608c8a2a5e261e71c538e9f Mon Sep 17 00:00:00 2001 From: Minglei Jin Date: Tue, 21 Jun 2022 17:18:57 +0800 Subject: [PATCH] fix/journal: restore interrupted(abort, power failure, etc.) tdb txn --- source/libs/tdb/src/db/tdbBtree.c | 9 +--- source/libs/tdb/src/db/tdbPager.c | 69 ++++++++++++++++++++++++++++++- source/libs/tdb/src/db/tdbTable.c | 5 +++ source/libs/tdb/src/db/tdbUtil.c | 2 +- source/libs/tdb/src/inc/tdbInt.h | 8 ++++ 5 files changed, 84 insertions(+), 9 deletions(-) diff --git a/source/libs/tdb/src/db/tdbBtree.c b/source/libs/tdb/src/db/tdbBtree.c index 45e71f6c0d..1c924fa636 100644 --- a/source/libs/tdb/src/db/tdbBtree.c +++ b/source/libs/tdb/src/db/tdbBtree.c @@ -56,14 +56,9 @@ typedef struct { } SIntHdr; #pragma pack(pop) -typedef struct { - u8 flags; - SBTree *pBt; -} SBtreeInitPageArg; - static int tdbDefaultKeyCmprFn(const void *pKey1, int keyLen1, const void *pKey2, int keyLen2); static int tdbBtreeOpenImpl(SBTree *pBt); -static int tdbBtreeInitPage(SPage *pPage, void *arg, int init); +//static int tdbBtreeInitPage(SPage *pPage, void *arg, int init); static int tdbBtreeEncodeCell(SPage *pPage, const void *pKey, int kLen, const void *pVal, int vLen, SCell *pCell, int *szCell, TXN *pTxn, SBTree *pBt); static int tdbBtreeDecodeCell(SPage *pPage, const SCell *pCell, SCellDecoder *pDecoder, TXN *pTxn, SBTree *pBt); @@ -348,7 +343,7 @@ static int tdbBtreeOpenImpl(SBTree *pBt) { return 0; } -static int tdbBtreeInitPage(SPage *pPage, void *arg, int init) { +int tdbBtreeInitPage(SPage *pPage, void *arg, int init) { SBTree *pBt; u8 flags; u8 leaf; diff --git a/source/libs/tdb/src/db/tdbPager.c b/source/libs/tdb/src/db/tdbPager.c index a74bb54883..0bf6de5482 100644 --- a/source/libs/tdb/src/db/tdbPager.c +++ b/source/libs/tdb/src/db/tdbPager.c @@ -424,4 +424,71 @@ static int tdbPagerWritePageToDB(SPager *pPager, SPage *pPage) { } return 0; -} \ No newline at end of file +} + +int tdbPagerRestore(SPager *pPager, SBTree *pBt) { + int ret = 0; + SPgno journalSize = 0; + u8 *pageBuf = NULL; + + tdb_fd_t jfd = tdbOsOpen(pPager->jFileName, TDB_O_RDWR, 0755); + if (jfd < 0) { + return 0; + } + + ret = tdbGetFileSize(jfd, pPager->pageSize, &journalSize); + if (ret < 0) { + return -1; + } + + pageBuf = tdbOsCalloc(1, pPager->pageSize); + if (pageBuf == NULL) { + return -1; + } + + TXN txn; + tdbTxnOpen(&txn, 0, tdbDefaultMalloc, tdbDefaultFree, NULL, 0); + SBtreeInitPageArg iArg; + iArg.pBt = pBt; + iArg.flags = 0; + + for (int pgIndex = 0; pgIndex < journalSize; ++pgIndex) { + // read pgno & the page from journal + SPgno pgno; + SPage *pPage; + + int ret = tdbOsRead(jfd, &pgno, sizeof(pgno)); + if (ret < 0) { + return -1; + } + + ret = tdbOsRead(jfd, pageBuf, pPager->pageSize); + if (ret < 0) { + return -1; + } + + ret = tdbPagerFetchPage(pPager, &pgno, &pPage, tdbBtreeInitPage, &iArg, &txn); + if (ret < 0) { + return -1; + } + + // write the page to db + ret = tdbPagerWritePageToDB(pPager, pPage); + if (ret < 0) { + return -1; + } + + tdbPCacheRelease(pPager->pCache, pPage, &txn); + } + + tdbOsFSync(pPager->fd); + + tdbTxnClose(&txn); + + tdbOsFree(pageBuf); + + tdbOsClose(jfd); + tdbOsRemove(pPager->jFileName); + + return 0; +} diff --git a/source/libs/tdb/src/db/tdbTable.c b/source/libs/tdb/src/db/tdbTable.c index 1575f9f206..f1bfb4eabc 100644 --- a/source/libs/tdb/src/db/tdbTable.c +++ b/source/libs/tdb/src/db/tdbTable.c @@ -61,6 +61,11 @@ int tdbTbOpen(const char *tbname, int keyLen, int valLen, tdb_cmpr_fn_t keyCmprF return -1; } + ret = tdbPagerRestore(pPager, pTb->pBt); + if (ret < 0) { + return -1; + } + *ppTb = pTb; return 0; } diff --git a/source/libs/tdb/src/db/tdbUtil.c b/source/libs/tdb/src/db/tdbUtil.c index 4acb83c8e4..9021e08ffe 100644 --- a/source/libs/tdb/src/db/tdbUtil.c +++ b/source/libs/tdb/src/db/tdbUtil.c @@ -64,4 +64,4 @@ int tdbGetFileSize(tdb_fd_t fd, int szPage, SPgno *size) { *size = szBytes / szPage; return 0; -} \ No newline at end of file +} diff --git a/source/libs/tdb/src/inc/tdbInt.h b/source/libs/tdb/src/inc/tdbInt.h index 39b5584fab..c914e098f7 100644 --- a/source/libs/tdb/src/inc/tdbInt.h +++ b/source/libs/tdb/src/inc/tdbInt.h @@ -158,6 +158,13 @@ int tdbBtreeUpsert(SBTree *pBt, const void *pKey, int nKey, const void *pData, i int tdbBtreeGet(SBTree *pBt, const void *pKey, int kLen, void **ppVal, int *vLen); int tdbBtreePGet(SBTree *pBt, const void *pKey, int kLen, void **ppKey, int *pkLen, void **ppVal, int *vLen); +typedef struct { + u8 flags; + SBTree *pBt; +} SBtreeInitPageArg; + +int tdbBtreeInitPage(SPage *pPage, void *arg, int init); + // SBTC int tdbBtcOpen(SBTC *pBtc, SBTree *pBt, TXN *pTxn); int tdbBtcClose(SBTC *pBtc); @@ -185,6 +192,7 @@ int tdbPagerFetchPage(SPager *pPager, SPgno *ppgno, SPage **ppPage, int (*initP TXN *pTxn); void tdbPagerReturnPage(SPager *pPager, SPage *pPage, TXN *pTxn); int tdbPagerAllocPage(SPager *pPager, SPgno *ppgno); +int tdbPagerRestore(SPager *pPager, SBTree *pBt); // tdbPCache.c ==================================== #define TDB_PCACHE_PAGE \