From 7d961a7fec175fa81aaae5a2459ca748e11dfa71 Mon Sep 17 00:00:00 2001 From: xiao-77 Date: Wed, 27 Nov 2024 10:15:50 +0800 Subject: [PATCH] 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