enh: log rotate

This commit is contained in:
kailixu 2024-11-30 23:33:09 +08:00
parent 9de36b0a19
commit 01f10980b3
1 changed files with 11 additions and 11 deletions

View File

@ -1078,9 +1078,9 @@ static void taosWriteLog(SLogBuff *pLogBuf) {
pLogBuf->writeInterval = 0; pLogBuf->writeInterval = 0;
} }
#define LOG_ROTATE_INTERVAL 30 #define LOG_ROTATE_INTERVAL 1800 // seconds
#ifndef LOG_ROTATE_BOOT #ifndef LOG_ROTATE_BOOT
#define LOG_ROTATE_BOOT 3 #define LOG_ROTATE_BOOT 180 // seconds
#endif #endif
static void *taosLogRotateFunc(void *param) { static void *taosLogRotateFunc(void *param) {
setThreadName("logRotate"); setThreadName("logRotate");
@ -1132,7 +1132,7 @@ static void *taosLogRotateFunc(void *param) {
int64_t elapseSec = taosGetTimestampMs() / 1000 - mtime; int64_t elapseSec = taosGetTimestampMs() / 1000 - mtime;
if (elapseSec < 86400) { if (elapseSec < 7200) {
continue; continue;
} }
@ -1167,7 +1167,7 @@ static void *taosAsyncOutputLog(void *param) {
int32_t count = 0; int32_t count = 0;
int32_t updateCron = 0; int32_t updateCron = 0;
int32_t writeInterval = 0; int32_t writeInterval = 0;
int32_t lastCheckMin = taosGetTimestampMs() / 60000 - (LOG_ROTATE_INTERVAL - LOG_ROTATE_BOOT); int64_t lastCheckSec = taosGetTimestampMs() / 1000 - (LOG_ROTATE_INTERVAL - LOG_ROTATE_BOOT);
while (1) { while (1) {
if (pSlowBuf) { if (pSlowBuf) {
@ -1195,22 +1195,22 @@ static void *taosAsyncOutputLog(void *param) {
} }
// process the log rotation every LOG_ROTATE_INTERVAL minutes // process the log rotation every LOG_ROTATE_INTERVAL minutes
int32_t curMin = taosGetTimestampMs() / 60000; int64_t curSec = taosGetTimestampMs() / 1000;
if (curMin >= lastCheckMin) { if (curSec >= lastCheckSec) {
if ((curMin - lastCheckMin) >= LOG_ROTATE_INTERVAL) { if ((curSec - lastCheckSec) >= LOG_ROTATE_INTERVAL) {
TdThread thread; TdThread thread;
TdThreadAttr attr; TdThreadAttr attr;
(void)taosThreadAttrInit(&attr); (void)taosThreadAttrInit(&attr);
(void)taosThreadAttrSetDetachState(&attr, PTHREAD_CREATE_DETACHED); (void)taosThreadAttrSetDetachState(&attr, PTHREAD_CREATE_DETACHED);
if (taosThreadCreate(&thread, &attr, taosLogRotateFunc, tsLogObj.logHandle) == 0) { if (taosThreadCreate(&thread, &attr, taosLogRotateFunc, tsLogObj.logHandle) == 0) {
lastCheckMin = curMin; lastCheckSec = curSec;
} else { } else {
uWarn("failed to create thread to process log buffer"); uWarn("failed to create thread to process log rotation");
} }
(void)taosThreadAttrDestroy(&attr); (void)taosThreadAttrDestroy(&attr);
} }
} else if (curMin < lastCheckMin) { } else if (curSec < lastCheckSec) {
lastCheckMin = curMin; lastCheckSec = curSec;
} }
} }