diff --git a/src/common/src/tglobal.c b/src/common/src/tglobal.c index a75e712f387cd684486147449adcdfe357a54d67..2630e3c468bd43a4c23b2f391e8e25254cbe8672 100644 --- a/src/common/src/tglobal.c +++ b/src/common/src/tglobal.c @@ -1014,7 +1014,7 @@ static void doInitGlobalConfig(void) { cfg.ptr = &tsLogKeepDays; cfg.valType = TAOS_CFG_VTYPE_INT32; cfg.cfgType = TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_LOG | TSDB_CFG_CTYPE_B_CLIENT; - cfg.minValue = 0; + cfg.minValue = -365000; cfg.maxValue = 365000; cfg.ptrLength = 0; cfg.unitType = TAOS_CFG_UTYPE_NONE; diff --git a/src/os/inc/osDir.h b/src/os/inc/osDir.h index 17683743e3ee7ed11350c02e504cce9f379b73d9..4a522dadb590bdeebe130850ad4bd73cfc37bb8d 100644 --- a/src/os/inc/osDir.h +++ b/src/os/inc/osDir.h @@ -25,6 +25,7 @@ void taosRemoveDir(char *rootDir); int taosMkDir(const char *pathname, mode_t mode); void taosRename(char* oldName, char *newName); void taosRemoveOldLogFiles(char *rootDir, int32_t keepDays); +int32_t taosCompressFile(char *srcFileName, char *destFileName); #ifdef __cplusplus } diff --git a/src/os/src/detail/CMakeLists.txt b/src/os/src/detail/CMakeLists.txt index 9f710e3ddffbe9342857cc3e92348f70c586f86e..afb89354536f03d4a467fa16e20a7fe25794ade2 100644 --- a/src/os/src/detail/CMakeLists.txt +++ b/src/os/src/detail/CMakeLists.txt @@ -2,6 +2,7 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) PROJECT(TDengine) INCLUDE_DIRECTORIES(.) +INCLUDE_DIRECTORIES(${TD_COMMUNITY_DIR}/deps/zlib-1.2.11/inc) AUX_SOURCE_DIRECTORY(. SRC) SET_SOURCE_FILES_PROPERTIES(osSysinfo.c PROPERTIES COMPILE_FLAGS -w) SET_SOURCE_FILES_PROPERTIES(osCoredump.c PROPERTIES COMPILE_FLAGS -w) diff --git a/src/os/src/detail/osDir.c b/src/os/src/detail/osDir.c index 93651c78ef98cfbcb8bbdbb575b3fb952acab229..bdb840ce010e63fcd48389b9db4294e2d58e6b36 100644 --- a/src/os/src/detail/osDir.c +++ b/src/os/src/detail/osDir.c @@ -17,6 +17,9 @@ #include "os.h" #include "tglobal.h" #include "tulog.h" +#include "zlib.h" + +#define COMPRESS_STEP_SIZE 163840 void taosRemoveDir(char *rootDir) { DIR *dir = opendir(rootDir); @@ -73,11 +76,11 @@ void taosRemoveOldLogFiles(char *rootDir, int32_t keepDays) { if (de->d_type & DT_DIR) { continue; } else { - // struct stat fState; - // if (stat(fname, &fState) < 0) { - // continue; - // } int32_t len = (int32_t)strlen(filename); + if (len > 3 && strcmp(filename + len - 3, ".gz") == 0) { + len -= 3; + } + int64_t fileSec = 0; for (int i = len - 1; i >= 0; i--) { if (filename[i] == '.') { @@ -100,3 +103,45 @@ void taosRemoveOldLogFiles(char *rootDir, int32_t keepDays) { closedir(dir); rmdir(rootDir); } + +int32_t taosCompressFile(char *srcFileName, char *destFileName) { + int32_t ret = 0; + int32_t len = 0; + char * data = malloc(COMPRESS_STEP_SIZE); + FILE * srcFp = NULL; + gzFile dstFp = NULL; + + srcFp = fopen(srcFileName, "r"); + if (srcFp == NULL) { + ret = -1; + goto cmp_end; + } + + int32_t fd = open(destFileName, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); + if (fd < 0) { + ret = -2; + goto cmp_end; + } + + dstFp = gzdopen(fd, "wb6f"); + if (dstFp == NULL) { + ret = -3; + goto cmp_end; + } + + while (!feof(srcFp)) { + len = (uLong)fread(data, 1, COMPRESS_STEP_SIZE, srcFp); + gzwrite(dstFp, data, len); + } + +cmp_end: + if (srcFp) { + fclose(srcFp); + } + if (dstFp) { + gzclose(dstFp); + } + free(data); + + return ret; +} diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index e63a085cc8d9e81d68e770df64f6c0d6c43e1807..89c8e3dc39211eca1b6c877f7789ad4313917ea2 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -3,7 +3,7 @@ PROJECT(TDengine) AUX_SOURCE_DIRECTORY(src SRC) ADD_LIBRARY(tutil ${SRC}) -TARGET_LINK_LIBRARIES(tutil pthread osdetail lz4) +TARGET_LINK_LIBRARIES(tutil pthread osdetail lz4 z) IF (TD_LINUX) TARGET_LINK_LIBRARIES(tutil m rt) diff --git a/src/util/src/tlog.c b/src/util/src/tlog.c index e5afe1b68e789e4c5500aad21ea3f074302bc36f..09b0933fd6e32e9b65c8c7acbb81fbfe7d5c005b 100644 --- a/src/util/src/tlog.c +++ b/src/util/src/tlog.c @@ -139,14 +139,22 @@ static void taosUnLockFile(int32_t fd) { } static void taosKeepOldLog(char *oldName) { - if (tsLogKeepDays <= 0) return; + if (tsLogKeepDays == 0) return; int64_t fileSec = taosGetTimestampSec(); char fileName[LOG_FILE_NAME_LEN + 20]; snprintf(fileName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64, tsLogObj.logName, fileSec); taosRename(oldName, fileName); - taosRemoveOldLogFiles(tsLogDir, tsLogKeepDays); + if (tsLogKeepDays < 0) { + char compressFileName[LOG_FILE_NAME_LEN + 20]; + snprintf(compressFileName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64 ".gz", tsLogObj.logName, fileSec); + if (taosCompressFile(fileName, compressFileName) == 0) { + (void)remove(fileName); + } + } + + taosRemoveOldLogFiles(tsLogDir, ABS(tsLogKeepDays)); } static void *taosThreadToOpenNewFile(void *param) {