TD-1574
This commit is contained in:
parent
96553e78ce
commit
d627ab2a88
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
|
|
|
@ -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) {
|
||||
|
|
Loading…
Reference in New Issue