[TD-13756]<fix>: file close memory error.
This commit is contained in:
parent
3ac1708a25
commit
70170d95d4
|
@ -38,7 +38,7 @@
|
|||
#define LOG_BUF_MUTEX(x) ((x)->buffMutex)
|
||||
|
||||
typedef struct {
|
||||
char buffer[LOG_DEFAULT_BUF_SIZE];
|
||||
char *buffer;
|
||||
int32_t buffStart;
|
||||
int32_t buffEnd;
|
||||
int32_t buffSize;
|
||||
|
@ -58,7 +58,7 @@ typedef struct {
|
|||
int32_t openInProgress;
|
||||
pid_t pid;
|
||||
char logName[LOG_FILE_NAME_LEN];
|
||||
SLogBuff logHandle;
|
||||
SLogBuff *logHandle;
|
||||
TdThreadMutex logMutex;
|
||||
} SLogObj;
|
||||
|
||||
|
@ -101,16 +101,16 @@ int64_t dbgWSize = 0;
|
|||
|
||||
static void *taosAsyncOutputLog(void *param);
|
||||
static int32_t taosPushLogBuffer(SLogBuff *tLogBuff, const char *msg, int32_t msgLen);
|
||||
static SLogBuff *taosLogBuffNew(SLogBuff *tLogBuff);
|
||||
static SLogBuff *taosLogBuffNew(int32_t bufSize);
|
||||
static void taosCloseLogByFd(TdFilePtr pFile);
|
||||
static void taosDestroyLog();
|
||||
static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum);
|
||||
static void taosCloseLogFile(void);
|
||||
static int32_t taosCompressFile(char *srcFileName, char *destFileName);
|
||||
|
||||
static int32_t taosStartLog() {
|
||||
TdThreadAttr threadAttr;
|
||||
taosThreadAttrInit(&threadAttr);
|
||||
if (taosThreadCreate(&(tsLogObj.logHandle.asyncThread), &threadAttr, taosAsyncOutputLog, &tsLogObj.logHandle) != 0) {
|
||||
if (taosThreadCreate(&(tsLogObj.logHandle->asyncThread), &threadAttr, taosAsyncOutputLog, tsLogObj.logHandle) != 0) {
|
||||
return -1;
|
||||
}
|
||||
taosThreadAttrDestroy(&threadAttr);
|
||||
|
@ -124,21 +124,24 @@ int32_t taosInitLog(const char *logName, int32_t maxFiles) {
|
|||
char fullName[PATH_MAX] = {0};
|
||||
snprintf(fullName, PATH_MAX, "%s" TD_DIRSEP "%s", tsLogDir, logName);
|
||||
|
||||
taosLogBuffNew(&tsLogObj.logHandle);
|
||||
tsLogObj.logHandle = taosLogBuffNew(LOG_DEFAULT_BUF_SIZE);
|
||||
if (tsLogObj.logHandle == NULL) return -1;
|
||||
if (taosOpenLogFile(fullName, tsNumOfLogLines, maxFiles) < 0) return -1;
|
||||
atexit(taosCloseLogFile);
|
||||
// atexit(taosDestroyLog);
|
||||
if (taosStartLog() < 0) return -1;
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void taosStopLog() {
|
||||
tsLogObj.logHandle.stop = 1;
|
||||
if (tsLogObj.logHandle) {
|
||||
tsLogObj.logHandle->stop = 1;
|
||||
}
|
||||
}
|
||||
|
||||
void taosCloseLog() {
|
||||
taosStopLog();
|
||||
if (taosCheckPthreadValid(tsLogObj.logHandle.asyncThread)) {
|
||||
taosThreadJoin(tsLogObj.logHandle.asyncThread, NULL);
|
||||
if (taosCheckPthreadValid(tsLogObj.logHandle->asyncThread)) {
|
||||
taosThreadJoin(tsLogObj.logHandle->asyncThread, NULL);
|
||||
}
|
||||
tsLogInited = 0;
|
||||
// In case that other threads still use log resources causing invalid write in valgrind
|
||||
|
@ -147,6 +150,14 @@ void taosCloseLog() {
|
|||
// taosCloseLog();
|
||||
}
|
||||
|
||||
static void taosDestroyLog() {
|
||||
if(tsLogObj.logHandle != NULL) {
|
||||
taosCloseFile(&tsLogObj.logHandle->pFile);
|
||||
taosMemoryFreeClear(LOG_BUF_BUFFER(tsLogObj.logHandle));
|
||||
taosMemoryFreeClear(tsLogObj.logHandle);
|
||||
}
|
||||
}
|
||||
|
||||
static bool taosLockLogFile(TdFilePtr pFile) {
|
||||
if (pFile == NULL) return false;
|
||||
|
||||
|
@ -209,8 +220,8 @@ static void *taosThreadToOpenNewFile(void *param) {
|
|||
taosLockLogFile(pFile);
|
||||
(void)taosLSeekFile(pFile, 0, SEEK_SET);
|
||||
|
||||
TdFilePtr pOldFile = tsLogObj.logHandle.pFile;
|
||||
tsLogObj.logHandle.pFile = pFile;
|
||||
TdFilePtr pOldFile = tsLogObj.logHandle->pFile;
|
||||
tsLogObj.logHandle->pFile = pFile;
|
||||
tsLogObj.lines = 0;
|
||||
tsLogObj.openInProgress = 0;
|
||||
taosCloseLogByFd(pOldFile);
|
||||
|
@ -346,39 +357,35 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) {
|
|||
taosThreadMutexInit(&tsLogObj.logMutex, NULL);
|
||||
|
||||
taosUmaskFile(0);
|
||||
tsLogObj.logHandle.pFile = taosOpenFile(fileName, TD_FILE_CTEATE | TD_FILE_WRITE);
|
||||
tsLogObj.logHandle->pFile = taosOpenFile(fileName, TD_FILE_CTEATE | TD_FILE_WRITE);
|
||||
|
||||
if (tsLogObj.logHandle.pFile == NULL) {
|
||||
if (tsLogObj.logHandle->pFile == NULL) {
|
||||
printf("\nfailed to open log file:%s, reason:%s\n", fileName, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
taosLockLogFile(tsLogObj.logHandle.pFile);
|
||||
taosLockLogFile(tsLogObj.logHandle->pFile);
|
||||
|
||||
// only an estimate for number of lines
|
||||
int64_t filesize = 0;
|
||||
if (taosFStatFile(tsLogObj.logHandle.pFile, &filesize, NULL) < 0) {
|
||||
if (taosFStatFile(tsLogObj.logHandle->pFile, &filesize, NULL) < 0) {
|
||||
printf("\nfailed to fstat log file:%s, reason:%s\n", fileName, strerror(errno));
|
||||
return -1;
|
||||
}
|
||||
size = (int32_t)filesize;
|
||||
tsLogObj.lines = size / 60;
|
||||
|
||||
taosLSeekFile(tsLogObj.logHandle.pFile, 0, SEEK_END);
|
||||
taosLSeekFile(tsLogObj.logHandle->pFile, 0, SEEK_END);
|
||||
|
||||
sprintf(name, "==================================================\n");
|
||||
taosWriteFile(tsLogObj.logHandle.pFile, name, (uint32_t)strlen(name));
|
||||
taosWriteFile(tsLogObj.logHandle->pFile, name, (uint32_t)strlen(name));
|
||||
sprintf(name, " new log file \n");
|
||||
taosWriteFile(tsLogObj.logHandle.pFile, name, (uint32_t)strlen(name));
|
||||
taosWriteFile(tsLogObj.logHandle->pFile, name, (uint32_t)strlen(name));
|
||||
sprintf(name, "==================================================\n");
|
||||
taosWriteFile(tsLogObj.logHandle.pFile, name, (uint32_t)strlen(name));
|
||||
taosWriteFile(tsLogObj.logHandle->pFile, name, (uint32_t)strlen(name));
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static void taosCloseLogFile(void) {
|
||||
taosCloseFile(&tsLogObj.logHandle.pFile);
|
||||
}
|
||||
|
||||
static void taosUpdateLogNums(ELogLevel level) {
|
||||
switch (level) {
|
||||
case DEBUG_ERROR:
|
||||
|
@ -412,12 +419,12 @@ static inline int32_t taosBuildLogHead(char *buffer, const char *flags) {
|
|||
}
|
||||
|
||||
static inline void taosPrintLogImp(ELogLevel level, int32_t dflag, const char *buffer, int32_t len) {
|
||||
if ((dflag & DEBUG_FILE) && tsLogObj.logHandle.pFile != NULL) {
|
||||
if ((dflag & DEBUG_FILE) && tsLogObj.logHandle && tsLogObj.logHandle->pFile != NULL) {
|
||||
taosUpdateLogNums(level);
|
||||
if (tsAsyncLog) {
|
||||
taosPushLogBuffer(&tsLogObj.logHandle, buffer, len);
|
||||
taosPushLogBuffer(tsLogObj.logHandle, buffer, len);
|
||||
} else {
|
||||
taosWriteFile(tsLogObj.logHandle.pFile, buffer, len);
|
||||
taosWriteFile(tsLogObj.logHandle->pFile, buffer, len);
|
||||
}
|
||||
|
||||
if (tsLogObj.maxLines > 0) {
|
||||
|
@ -489,7 +496,7 @@ void taosDumpData(unsigned char *msg, int32_t len) {
|
|||
pos += 3;
|
||||
if (c >= 16) {
|
||||
temp[pos++] = '\n';
|
||||
taosWriteFile(tsLogObj.logHandle.pFile, temp, (uint32_t)pos);
|
||||
taosWriteFile(tsLogObj.logHandle->pFile, temp, (uint32_t)pos);
|
||||
c = 0;
|
||||
pos = 0;
|
||||
}
|
||||
|
@ -497,7 +504,7 @@ void taosDumpData(unsigned char *msg, int32_t len) {
|
|||
|
||||
temp[pos++] = '\n';
|
||||
|
||||
taosWriteFile(tsLogObj.logHandle.pFile, temp, (uint32_t)pos);
|
||||
taosWriteFile(tsLogObj.logHandle->pFile, temp, (uint32_t)pos);
|
||||
}
|
||||
|
||||
static void taosCloseLogByFd(TdFilePtr pFile) {
|
||||
|
@ -507,10 +514,18 @@ static void taosCloseLogByFd(TdFilePtr pFile) {
|
|||
}
|
||||
}
|
||||
|
||||
static SLogBuff *taosLogBuffNew(SLogBuff *tLogBuff) {
|
||||
static SLogBuff *taosLogBuffNew(int32_t bufSize) {
|
||||
SLogBuff *tLogBuff = NULL;
|
||||
|
||||
tLogBuff = taosMemoryCalloc(1, sizeof(SLogBuff));
|
||||
if (tLogBuff == NULL) return NULL;
|
||||
|
||||
LOG_BUF_BUFFER(tLogBuff) = taosMemoryMalloc(bufSize);
|
||||
if (LOG_BUF_BUFFER(tLogBuff) == NULL) goto _err;
|
||||
|
||||
LOG_BUF_START(tLogBuff) = LOG_BUF_END(tLogBuff) = 0;
|
||||
LOG_BUF_SIZE(tLogBuff) = LOG_DEFAULT_BUF_SIZE;
|
||||
tLogBuff->minBuffSize = LOG_DEFAULT_BUF_SIZE / 10;
|
||||
LOG_BUF_SIZE(tLogBuff) = bufSize;
|
||||
tLogBuff->minBuffSize = bufSize / 10;
|
||||
tLogBuff->stop = 0;
|
||||
|
||||
if (taosThreadMutexInit(&LOG_BUF_MUTEX(tLogBuff), NULL) < 0) goto _err;
|
||||
|
@ -519,6 +534,8 @@ static SLogBuff *taosLogBuffNew(SLogBuff *tLogBuff) {
|
|||
return tLogBuff;
|
||||
|
||||
_err:
|
||||
taosMemoryFreeClear(LOG_BUF_BUFFER(tLogBuff));
|
||||
taosMemoryFreeClear(tLogBuff);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue