From 96c59cc7462f0c57313cd08ac074ff3e67b27d54 Mon Sep 17 00:00:00 2001 From: xiao-77 Date: Wed, 11 Sep 2024 13:47:43 +0800 Subject: [PATCH] reduce mutex lock usage time --- include/util/tutil.h | 14 +++++++------- source/libs/wal/src/walRead.c | 20 +++++++++++++------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/include/util/tutil.h b/include/util/tutil.h index 5fa06b7e61..6a8f58e360 100644 --- a/include/util/tutil.h +++ b/include/util/tutil.h @@ -176,13 +176,13 @@ static FORCE_INLINE int32_t taosGetTbHashVal(const char *tbname, int32_t tblen, } \ } while (0) -#define TAOS_CHECK_RETURN_WITH_MUTEX(CMD, MUTEX) \ - do { \ - int32_t __c = (CMD); \ - if (__c != TSDB_CODE_SUCCESS) { \ - taosThreadMutexUnlock(MUTEX); \ - TAOS_RETURN(__c); \ - } \ +#define TAOS_CHECK_RETURN_WITH_FREE(CMD, PTR) \ + do { \ + int32_t __c = (CMD); \ + if (__c != TSDB_CODE_SUCCESS) { \ + taosMemoryFree(PTR); \ + TAOS_RETURN(__c); \ + } \ } while (0) #define TAOS_CHECK_GOTO(CMD, LINO, LABEL) \ diff --git a/source/libs/wal/src/walRead.c b/source/libs/wal/src/walRead.c index 5568270efe..1e9fcc70aa 100644 --- a/source/libs/wal/src/walRead.c +++ b/source/libs/wal/src/walRead.c @@ -202,22 +202,28 @@ static int32_t walReadSeekVerImpl(SWalReader *pReader, int64_t ver) { SWalFileInfo tmpInfo; tmpInfo.firstVer = ver; taosThreadMutexLock(&pWal->mutex); - SWalFileInfo *pRet = taosArraySearch(pWal->fileInfoSet, &tmpInfo, compareWalFileInfo, TD_LE); - if (pRet == NULL) { + SWalFileInfo *gloablPRet = taosArraySearch(pWal->fileInfoSet, &tmpInfo, compareWalFileInfo, TD_LE); + if (gloablPRet == NULL) { wError("failed to find WAL log file with ver:%" PRId64, ver); taosThreadMutexUnlock(&pWal->mutex); TAOS_RETURN(TSDB_CODE_WAL_INVALID_VER); } - + SWalFileInfo *pRet = taosMemoryMalloc(sizeof(SWalFileInfo)); + if (pRet == NULL) { + wError("failed to allocate memory for localRet"); + taosThreadMutexUnlock(&pWal->mutex); + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); + } + TAOS_MEMCPY(pRet, gloablPRet, sizeof(SWalFileInfo)); + taosThreadMutexUnlock(&pWal->mutex); if (pReader->curFileFirstVer != pRet->firstVer) { // error code was set inner - TAOS_CHECK_RETURN_WITH_MUTEX(walReadChangeFile(pReader, pRet->firstVer), &pWal->mutex); + TAOS_CHECK_RETURN_WITH_FREE(walReadChangeFile(pReader, pRet->firstVer), pRet); } // error code was set inner - TAOS_CHECK_RETURN_WITH_MUTEX(walReadSeekFilePos(pReader, pRet->firstVer, ver), &pWal->mutex); - - taosThreadMutexUnlock(&pWal->mutex); + TAOS_CHECK_RETURN_WITH_FREE(walReadSeekFilePos(pReader, pRet->firstVer, ver), pRet); + taosMemoryFree(pRet); wDebug("vgId:%d, wal version reset from %" PRId64 " to %" PRId64, pReader->pWal->cfg.vgId, pReader->curVersion, ver); pReader->curVersion = ver;