From 7d961a7fec175fa81aaae5a2459ca748e11dfa71 Mon Sep 17 00:00:00 2001 From: xiao-77 Date: Wed, 27 Nov 2024 10:15:50 +0800 Subject: [PATCH 1/4] Enh close wal module open file. --- source/libs/wal/src/walMeta.c | 11 ++--- source/libs/wal/src/walWrite.c | 75 ++++++++++++++-------------------- 2 files changed, 36 insertions(+), 50 deletions(-) diff --git a/source/libs/wal/src/walMeta.c b/source/libs/wal/src/walMeta.c index ce2b9218b5..0000647a61 100644 --- a/source/libs/wal/src/walMeta.c +++ b/source/libs/wal/src/walMeta.c @@ -371,7 +371,7 @@ static int32_t walLogEntriesComplete(const SWal* pWal) { } static int32_t walTrimIdxFile(SWal* pWal, int32_t fileIdx) { - int32_t code = 0; + int32_t code = TSDB_CODE_SUCCESS; SWalFileInfo* pFileInfo = taosArrayGet(pWal->fileInfoSet, fileIdx); if (!pFileInfo) { TAOS_RETURN(TSDB_CODE_FAILED); @@ -384,7 +384,7 @@ static int32_t walTrimIdxFile(SWal* pWal, int32_t fileIdx) { if (taosStatFile(fnameStr, &fileSize, NULL, NULL) != 0) { wError("vgId:%d, failed to stat file due to %s. file:%s", pWal->cfg.vgId, strerror(errno), fnameStr); code = terrno; - TAOS_RETURN(code); + goto _exit; } int64_t records = TMAX(0, pFileInfo->lastVer - pFileInfo->firstVer + 1); int64_t lastEndOffset = records * sizeof(SWalIdxEntry); @@ -395,7 +395,7 @@ static int32_t walTrimIdxFile(SWal* pWal, int32_t fileIdx) { TdFilePtr pFile = taosOpenFile(fnameStr, TD_FILE_READ | TD_FILE_WRITE); if (pFile == NULL) { - TAOS_RETURN(terrno); + goto _exit; } wInfo("vgId:%d, trim idx file. file: %s, size: %" PRId64 ", offset: %" PRId64, pWal->cfg.vgId, fnameStr, fileSize, @@ -404,10 +404,11 @@ static int32_t walTrimIdxFile(SWal* pWal, int32_t fileIdx) { code = taosFtruncateFile(pFile, lastEndOffset); if (code < 0) { wError("vgId:%d, failed to truncate file due to %s. file:%s", pWal->cfg.vgId, strerror(errno), fnameStr); - TAOS_RETURN(code); + goto _exit; } - (void)taosCloseFile(&pFile); +_exit: + (void)taosCloseFile(&pFile); TAOS_RETURN(TSDB_CODE_SUCCESS); } diff --git a/source/libs/wal/src/walWrite.c b/source/libs/wal/src/walWrite.c index 66ead2fd26..6fe750d143 100644 --- a/source/libs/wal/src/walWrite.c +++ b/source/libs/wal/src/walWrite.c @@ -160,12 +160,12 @@ static int64_t walChangeWrite(SWal *pWal, int64_t ver) { int32_t walRollback(SWal *pWal, int64_t ver) { TAOS_UNUSED(taosThreadRwlockWrlock(&pWal->mutex)); wInfo("vgId:%d, wal rollback for version %" PRId64, pWal->cfg.vgId, ver); + int32_t code = 0; int64_t ret; char fnameStr[WAL_FILE_LEN]; if (ver > pWal->vers.lastVer || ver <= pWal->vers.commitVer || ver <= pWal->vers.snapshotVer) { - TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - - TAOS_RETURN(TSDB_CODE_WAL_INVALID_VER); + code = TSDB_CODE_WAL_INVALID_VER; + goto _exit; } // find correct file @@ -173,9 +173,8 @@ int32_t walRollback(SWal *pWal, int64_t ver) { // change current files ret = walChangeWrite(pWal, ver); if (ret < 0) { - TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - - TAOS_RETURN(terrno); + code = terrno; + goto _exit; } // delete files in descending order @@ -200,23 +199,20 @@ int32_t walRollback(SWal *pWal, int64_t ver) { TAOS_UNUSED(taosCloseFile(&pWal->pIdxFile)); TdFilePtr pIdxFile = taosOpenFile(fnameStr, TD_FILE_WRITE | TD_FILE_READ | TD_FILE_APPEND); if (pIdxFile == NULL) { - TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - - TAOS_RETURN(terrno); + code = terrno; + goto _exit; } int64_t idxOff = walGetVerIdxOffset(pWal, ver); ret = taosLSeekFile(pIdxFile, idxOff, SEEK_SET); if (ret < 0) { - TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - - TAOS_RETURN(terrno); + code = terrno; + goto _exit; } // read idx file and get log file pos SWalIdxEntry entry; if (taosReadFile(pIdxFile, &entry, sizeof(SWalIdxEntry)) != sizeof(SWalIdxEntry)) { - TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - - TAOS_RETURN(terrno); + code = terrno; + goto _exit; } walBuildLogName(pWal, walGetCurFileFirstVer(pWal), fnameStr); @@ -225,70 +221,58 @@ int32_t walRollback(SWal *pWal, int64_t ver) { wDebug("vgId:%d, wal truncate file %s", pWal->cfg.vgId, fnameStr); if (pLogFile == NULL) { // TODO - TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - - TAOS_RETURN(terrno); + code = terrno; + goto _exit; } ret = taosLSeekFile(pLogFile, entry.offset, SEEK_SET); if (ret < 0) { // TODO - TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - - TAOS_RETURN(terrno); + code = terrno; + goto _exit; } // validate offset SWalCkHead head; int64_t size = taosReadFile(pLogFile, &head, sizeof(SWalCkHead)); if (size != sizeof(SWalCkHead)) { - TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - - TAOS_RETURN(terrno); + code = terrno; + goto _exit; } - int32_t code = walValidHeadCksum(&head); + code = walValidHeadCksum(&head); if (code != 0) { - TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - - TAOS_RETURN(TSDB_CODE_WAL_FILE_CORRUPTED); + code = TSDB_CODE_WAL_FILE_CORRUPTED; + goto _exit; } if (head.head.version != ver) { - TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - - TAOS_RETURN(TSDB_CODE_WAL_FILE_CORRUPTED); + code = TSDB_CODE_WAL_FILE_CORRUPTED; + goto _exit; } // truncate old files code = taosFtruncateFile(pLogFile, entry.offset); if (code < 0) { - TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - - TAOS_RETURN(code); + goto _exit; } code = taosFtruncateFile(pIdxFile, idxOff); if (code < 0) { - TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - - TAOS_RETURN(code); + goto _exit; } pWal->vers.lastVer = ver - 1; ((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->lastVer = ver - 1; ((SWalFileInfo *)taosArrayGetLast(pWal->fileInfoSet))->fileSize = entry.offset; - TAOS_UNUSED(taosCloseFile(&pIdxFile)); - TAOS_UNUSED(taosCloseFile(&pLogFile)); - code = walSaveMeta(pWal); if (code < 0) { wError("vgId:%d, failed to save meta since %s", pWal->cfg.vgId, terrstr()); - TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - - TAOS_RETURN(code); + goto _exit; } - // unlock +_exit: + TAOS_UNUSED(taosCloseFile(&pIdxFile)); + TAOS_UNUSED(taosCloseFile(&pLogFile)); TAOS_UNUSED(taosThreadRwlockUnlock(&pWal->mutex)); - TAOS_RETURN(TSDB_CODE_SUCCESS); + TAOS_RETURN(code); } static int32_t walRollImpl(SWal *pWal) { @@ -718,6 +702,7 @@ static int32_t walInitWriteFile(SWal *pWal) { walBuildLogName(pWal, fileFirstVer, fnameStr); pLogTFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); if (pLogTFile == NULL) { + taosCloseFile(&pIdxTFile); TAOS_RETURN(terrno); } // switch file From d609d144be9743aa506133853241cd29e474f7ad Mon Sep 17 00:00:00 2001 From: xiao-77 Date: Wed, 27 Nov 2024 13:46:55 +0800 Subject: [PATCH 2/4] Fix ci crash. --- source/libs/wal/src/walWrite.c | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/source/libs/wal/src/walWrite.c b/source/libs/wal/src/walWrite.c index 6fe750d143..69e70a0f47 100644 --- a/source/libs/wal/src/walWrite.c +++ b/source/libs/wal/src/walWrite.c @@ -160,9 +160,10 @@ static int64_t walChangeWrite(SWal *pWal, int64_t ver) { int32_t walRollback(SWal *pWal, int64_t ver) { TAOS_UNUSED(taosThreadRwlockWrlock(&pWal->mutex)); wInfo("vgId:%d, wal rollback for version %" PRId64, pWal->cfg.vgId, ver); - int32_t code = 0; - int64_t ret; - char fnameStr[WAL_FILE_LEN]; + int32_t code = 0; + int64_t ret; + char fnameStr[WAL_FILE_LEN]; + TdFilePtr pIdxFile = NULL, pLogFile = NULL; if (ver > pWal->vers.lastVer || ver <= pWal->vers.commitVer || ver <= pWal->vers.snapshotVer) { code = TSDB_CODE_WAL_INVALID_VER; goto _exit; @@ -196,8 +197,7 @@ int32_t walRollback(SWal *pWal, int64_t ver) { } walBuildIdxName(pWal, walGetCurFileFirstVer(pWal), fnameStr); - TAOS_UNUSED(taosCloseFile(&pWal->pIdxFile)); - TdFilePtr pIdxFile = taosOpenFile(fnameStr, TD_FILE_WRITE | TD_FILE_READ | TD_FILE_APPEND); + pIdxFile = taosOpenFile(fnameStr, TD_FILE_WRITE | TD_FILE_READ | TD_FILE_APPEND); if (pIdxFile == NULL) { code = terrno; goto _exit; @@ -216,8 +216,7 @@ int32_t walRollback(SWal *pWal, int64_t ver) { } walBuildLogName(pWal, walGetCurFileFirstVer(pWal), fnameStr); - TAOS_UNUSED(taosCloseFile(&pWal->pLogFile)); - TdFilePtr pLogFile = taosOpenFile(fnameStr, TD_FILE_WRITE | TD_FILE_READ | TD_FILE_APPEND); + pLogFile = taosOpenFile(fnameStr, TD_FILE_WRITE | TD_FILE_READ | TD_FILE_APPEND); wDebug("vgId:%d, wal truncate file %s", pWal->cfg.vgId, fnameStr); if (pLogFile == NULL) { // TODO @@ -702,7 +701,7 @@ static int32_t walInitWriteFile(SWal *pWal) { walBuildLogName(pWal, fileFirstVer, fnameStr); pLogTFile = taosOpenFile(fnameStr, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_APPEND); if (pLogTFile == NULL) { - taosCloseFile(&pIdxTFile); + TAOS_UNUSED(taosCloseFile(&pIdxTFile)); TAOS_RETURN(terrno); } // switch file From 1eb005a9f09ed2afd45a0b8fee729941a0c20a29 Mon Sep 17 00:00:00 2001 From: xiao-77 Date: Wed, 27 Nov 2024 16:27:00 +0800 Subject: [PATCH 3/4] Fix error code return. --- source/libs/wal/src/walMeta.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/libs/wal/src/walMeta.c b/source/libs/wal/src/walMeta.c index 0000647a61..43688347db 100644 --- a/source/libs/wal/src/walMeta.c +++ b/source/libs/wal/src/walMeta.c @@ -395,6 +395,7 @@ static int32_t walTrimIdxFile(SWal* pWal, int32_t fileIdx) { TdFilePtr pFile = taosOpenFile(fnameStr, TD_FILE_READ | TD_FILE_WRITE); if (pFile == NULL) { + code = terrno; goto _exit; } @@ -409,7 +410,7 @@ static int32_t walTrimIdxFile(SWal* pWal, int32_t fileIdx) { _exit: (void)taosCloseFile(&pFile); - TAOS_RETURN(TSDB_CODE_SUCCESS); + TAOS_RETURN(code); } static void printFileSet(int32_t vgId, SArray* fileSet, const char* str) { From 4f2faff91daa893d0f6505a782eff14fe123161f Mon Sep 17 00:00:00 2001 From: xiao-77 Date: Thu, 28 Nov 2024 11:20:22 +0800 Subject: [PATCH 4/4] Fix maybe close undefine fd ptr. --- source/libs/wal/src/walMeta.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/source/libs/wal/src/walMeta.c b/source/libs/wal/src/walMeta.c index 43688347db..b40a9eeefe 100644 --- a/source/libs/wal/src/walMeta.c +++ b/source/libs/wal/src/walMeta.c @@ -372,6 +372,7 @@ static int32_t walLogEntriesComplete(const SWal* pWal) { static int32_t walTrimIdxFile(SWal* pWal, int32_t fileIdx) { int32_t code = TSDB_CODE_SUCCESS; + TdFilePtr pFile = NULL; SWalFileInfo* pFileInfo = taosArrayGet(pWal->fileInfoSet, fileIdx); if (!pFileInfo) { TAOS_RETURN(TSDB_CODE_FAILED); @@ -393,7 +394,7 @@ static int32_t walTrimIdxFile(SWal* pWal, int32_t fileIdx) { TAOS_RETURN(TSDB_CODE_SUCCESS); } - TdFilePtr pFile = taosOpenFile(fnameStr, TD_FILE_READ | TD_FILE_WRITE); + pFile = taosOpenFile(fnameStr, TD_FILE_READ | TD_FILE_WRITE); if (pFile == NULL) { code = terrno; goto _exit;