提交 f65e3609 编写于 作者: S Shengliang Guan

TD-1263

上级 37922f29
......@@ -1013,18 +1013,18 @@ static void doInitGlobalConfig(void) {
cfg.ptr = &tsNumOfLogLines;
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 = 10000;
cfg.minValue = 1000;
cfg.maxValue = 2000000000;
cfg.ptrLength = 0;
cfg.unitType = TAOS_CFG_UTYPE_NONE;
taosInitConfigOption(cfg);
cfg.option = "logKeepDays";
cfg.ptr = &tsNumOfLogLines;
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.maxValue = 3650;
cfg.maxValue = 36500;
cfg.ptrLength = 0;
cfg.unitType = TAOS_CFG_UTYPE_NONE;
taosInitConfigOption(cfg);
......
......@@ -54,9 +54,9 @@ void taosRename(char* oldName, char *newName) {
// if newName in not empty, rename return fail.
// the newName must be empty or does not exist
if (rename(oldName, newName)) {
uError("%s is modify to %s fail, reason:%s", oldName, newName, strerror(errno));
uError("failed to rename file %s to %s, reason:%s", oldName, newName, strerror(errno));
} else {
uInfo("%s is modify to %s success!", oldName, newName);
uInfo("successfully to rename file %s to %s", oldName, newName);
}
}
......@@ -64,7 +64,7 @@ void taosRemoveOldLogFiles(char *rootDir, int32_t keepDays) {
DIR *dir = opendir(rootDir);
if (dir == NULL) return;
int64_t ms = taosGetTimestampMs();
int64_t sec = taosGetTimestampSec();
struct dirent *de = NULL;
while ((de = readdir(dir)) != NULL) {
......@@ -80,20 +80,19 @@ void taosRemoveOldLogFiles(char *rootDir, int32_t keepDays) {
// continue;
// }
int32_t len = strlen(filename);
int64_t fileMs = 0;
for (int i = len - 1; i >= 0; ++i) {
int64_t fileSec = 0;
for (int i = len - 1; i >= 0; i--) {
if (filename[i] == '.') {
fileMs = atoll(filename + i + 1);
fileSec = atoll(filename + i + 1);
break;
}
}
if (fileMs <= 0) continue;
int32_t days = (fileMs - ms) / 86400 + 1;
if (fileSec <= 100) continue;
int32_t days = ABS(sec - fileSec) / 86400 + 1;
if (days > keepDays) {
(void)remove(filename);
uInfo("file:%s is removed, days:%d keepDays:%d", filename, days, keepDays);
} else {
uTrace("file:%s won't be removed, days:%d keepDays:%d", filename, days, keepDays);
}
......
......@@ -79,6 +79,7 @@ static int32_t taosPushLogBuffer(SLogBuff *tLogBuff, char *msg, int32_t msgLen
static SLogBuff *taosLogBuffNew(int32_t bufSize);
static void taosCloseLogByFd(int32_t oldFd);
static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum);
extern void taosPrintGlobalCfg();
static int32_t taosStartLog() {
pthread_attr_t threadAttr;
......@@ -140,11 +141,10 @@ static void taosUnLockFile(int32_t fd) {
static void taosKeepOldLog(char *oldName) {
if (tsLogKeepDays <= 0) return;
int64_t ms = taosGetTimestampMs();
int64_t fileSec = taosGetTimestampSec();
char fileName[LOG_FILE_NAME_LEN + 20];
snprintf(fileName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64, tsLogObj.logName, ms);
snprintf(fileName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64, tsLogObj.logName, fileSec);
uInfo("rename log file %s to %s", oldName, fileName);
taosRename(oldName, fileName);
taosRemoveOldLogFiles(tsLogDir, tsLogKeepDays);
}
......@@ -174,8 +174,10 @@ static void *taosThreadToOpenNewFile(void *param) {
tsLogObj.lines = 0;
tsLogObj.openInProgress = 0;
taosCloseLogByFd(oldFd);
uInfo("new log file is opened!!!");
uInfo(" new log file:%d is opened", tsLogObj.flag);
uInfo("==================================");
taosPrintGlobalCfg();
taosKeepOldLog(keepName);
return NULL;
......@@ -282,20 +284,23 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) {
strcat(name, ".0");
}
if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) {
strcpy(name, fn);
strcat(name, ".1");
}
bool log0Exist = stat(name, &logstat0) >= 0;
bool log1Exist = stat(name, &logstat1) >= 0;
// if none of the log files exist, open 0, if both exists, open the old one
if (stat(name, &logstat0) < 0) {
if (!log0Exist && !log1Exist) {
tsLogObj.flag = 0;
} else if (!log1Exist) {
tsLogObj.flag = 0;
} else if (!log0Exist) {
tsLogObj.flag = 1;
} else {
if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) {
strcpy(name, fn);
strcat(name, ".1");
}
if (stat(name, &logstat1) < 0) {
tsLogObj.flag = 1;
} else {
tsLogObj.flag = (logstat0.st_mtime > logstat1.st_mtime) ? 0 : 1;
}
tsLogObj.flag = (logstat0.st_mtime > logstat1.st_mtime) ? 0 : 1;
}
char fileName[LOG_FILE_NAME_LEN + 50] = "\0";
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册