From ad1c474dc6286bda868f83eecc174a910a61f06a Mon Sep 17 00:00:00 2001 From: factosea <285808407@qq.com> Date: Wed, 3 Apr 2024 15:29:08 +0800 Subject: [PATCH 1/2] fix: logFileHandle NULL --- source/util/src/tlog.c | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 16ca1b64c3..7802b77e6a 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -242,6 +242,7 @@ void taosCloseLog() { taosMemoryFreeClear(tsLogObj.logHandle->buffer); taosThreadMutexDestroy(&tsLogObj.logMutex); taosMemoryFreeClear(tsLogObj.logHandle); + tsLogObj.logHandle = NULL; } } @@ -347,10 +348,11 @@ void taosResetLog() { // force create a new log file tsLogObj.lines = tsNumOfLogLines + 10; - taosOpenNewLogFile(); - - uInfo("=================================="); - uInfo(" reset log file "); + if (tsLogObj.logHandle) { + taosOpenNewLogFile(); + uInfo("=================================="); + uInfo(" reset log file "); + } } static bool taosCheckFileIsOpen(char *logFileName) { From a969142c37935fc1615a0c6769da38a61e414a61 Mon Sep 17 00:00:00 2001 From: factosea <285808407@qq.com> Date: Wed, 10 Apr 2024 10:21:54 +0800 Subject: [PATCH 2/2] fix: create log file thread --- source/util/src/tlog.c | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/source/util/src/tlog.c b/source/util/src/tlog.c index 7802b77e6a..0c09174970 100644 --- a/source/util/src/tlog.c +++ b/source/util/src/tlog.c @@ -286,8 +286,11 @@ static void taosKeepOldLog(char *oldName) { taosRemoveOldFiles(tsLogDir, tsLogKeepDays); } } - -static void *taosThreadToOpenNewFile(void *param) { +typedef struct { + TdFilePtr pOldFile; + char keepName[LOG_FILE_NAME_LEN + 20]; +} OldFileKeeper; +static OldFileKeeper *taosOpenNewFile() { char keepName[LOG_FILE_NAME_LEN + 20]; sprintf(keepName, "%s.%d", tsLogObj.logName, tsLogObj.flag); @@ -313,13 +316,26 @@ static void *taosThreadToOpenNewFile(void *param) { tsLogObj.logHandle->pFile = pFile; tsLogObj.lines = 0; tsLogObj.openInProgress = 0; - taosSsleep(20); - taosCloseLogByFd(pOldFile); + OldFileKeeper* oldFileKeeper = taosMemoryMalloc(sizeof(OldFileKeeper)); + if (oldFileKeeper == NULL) { + uError("create old log keep info faild! mem is not enough."); + return NULL; + } + oldFileKeeper->pOldFile = pOldFile; + memcpy(oldFileKeeper->keepName, keepName, LOG_FILE_NAME_LEN + 20); uInfo(" new log file:%d is opened", tsLogObj.flag); uInfo("=================================="); - taosKeepOldLog(keepName); + return oldFileKeeper; +} +static void *taosThreadToCloseOldFile(void* param) { + if(!param) return NULL; + OldFileKeeper* oldFileKeeper = (OldFileKeeper*)param; + taosSsleep(20); + taosCloseLogByFd(oldFileKeeper->pOldFile); + taosKeepOldLog(oldFileKeeper->keepName); + taosMemoryFree(oldFileKeeper); return NULL; } @@ -335,7 +351,8 @@ static int32_t taosOpenNewLogFile() { taosThreadAttrInit(&attr); taosThreadAttrSetDetachState(&attr, PTHREAD_CREATE_DETACHED); - taosThreadCreate(&thread, &attr, taosThreadToOpenNewFile, NULL); + OldFileKeeper* oldFileKeeper = taosOpenNewFile(); + taosThreadCreate(&thread, &attr, taosThreadToCloseOldFile, oldFileKeeper); taosThreadAttrDestroy(&attr); }