Merge pull request #26106 from taosdata/fix/TS-4963-3.0

fix: reserve log file name
This commit is contained in:
Hongze Cheng 2024-06-12 15:36:50 +08:00 committed by GitHub
commit c2a80bd1cc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
1 changed files with 27 additions and 12 deletions

View File

@ -66,6 +66,7 @@ typedef struct {
int32_t lines; int32_t lines;
int32_t flag; int32_t flag;
int32_t openInProgress; int32_t openInProgress;
int64_t lastKeepFileSec;
pid_t pid; pid_t pid;
char logName[LOG_FILE_NAME_LEN]; char logName[LOG_FILE_NAME_LEN];
SLogBuff *logHandle; SLogBuff *logHandle;
@ -267,19 +268,33 @@ static void taosUnLockLogFile(TdFilePtr pFile) {
} }
} }
static void taosKeepOldLog(char *oldName) { static void taosReserveOldLog(char *oldName, char *keepName) {
if (tsLogKeepDays == 0) return; if (tsLogKeepDays <= 0) {
keepName[0] = 0;
return;
}
int32_t code = 0;
int64_t fileSec = taosGetTimestampSec(); int64_t fileSec = taosGetTimestampSec();
char fileName[LOG_FILE_NAME_LEN + 20]; if (tsLogObj.lastKeepFileSec < fileSec) {
snprintf(fileName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64, tsLogObj.logName, fileSec); tsLogObj.lastKeepFileSec = fileSec;
} else {
(void)taosRenameFile(oldName, fileName); fileSec = ++tsLogObj.lastKeepFileSec;
}
snprintf(keepName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64, tsLogObj.logName, fileSec);
if ((code = taosRenameFile(oldName, keepName))) {
keepName[0] = 0;
uError("failed to rename file:%s to %s since %s", oldName, keepName, tstrerror(code));
}
}
static void taosKeepOldLog(char *oldName) {
if (oldName[0] != 0) {
char compressFileName[LOG_FILE_NAME_LEN + 20]; char compressFileName[LOG_FILE_NAME_LEN + 20];
snprintf(compressFileName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64 ".gz", tsLogObj.logName, fileSec); snprintf(compressFileName, LOG_FILE_NAME_LEN + 20, "%s.gz", oldName);
if (taosCompressFile(fileName, compressFileName) == 0) { if (taosCompressFile(oldName, compressFileName) == 0) {
(void)taosRemoveFile(fileName); (void)taosRemoveFile(oldName);
}
} }
if (tsLogKeepDays > 0) { if (tsLogKeepDays > 0) {
@ -316,13 +331,13 @@ static OldFileKeeper *taosOpenNewFile() {
tsLogObj.logHandle->pFile = pFile; tsLogObj.logHandle->pFile = pFile;
tsLogObj.lines = 0; tsLogObj.lines = 0;
tsLogObj.openInProgress = 0; tsLogObj.openInProgress = 0;
OldFileKeeper* oldFileKeeper = taosMemoryMalloc(sizeof(OldFileKeeper)); OldFileKeeper *oldFileKeeper = taosMemoryMalloc(sizeof(OldFileKeeper));
if (oldFileKeeper == NULL) { if (oldFileKeeper == NULL) {
uError("create old log keep info faild! mem is not enough."); uError("create old log keep info faild! mem is not enough.");
return NULL; return NULL;
} }
oldFileKeeper->pOldFile = pOldFile; oldFileKeeper->pOldFile = pOldFile;
memcpy(oldFileKeeper->keepName, keepName, LOG_FILE_NAME_LEN + 20); taosReserveOldLog(keepName, oldFileKeeper->keepName);
uInfo(" new log file:%d is opened", tsLogObj.flag); uInfo(" new log file:%d is opened", tsLogObj.flag);
uInfo("=================================="); uInfo("==================================");