From aa1a5700fba114815a42aa2b486b7ff18db6b170 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 7 Dec 2020 23:52:07 +0800 Subject: [PATCH 1/4] TD-2371 --- src/client/src/tscAsync.c | 2 +- src/client/src/tscSql.c | 2 +- src/client/src/tscSystem.c | 7 - src/dnode/src/dnodeMain.c | 2 + src/kit/shell/src/shellLinux.c | 2 +- src/plugins/http/src/httpSql.c | 4 +- src/plugins/http/src/httpSystem.c | 4 - src/util/inc/tnote.h | 53 ++-- src/util/src/tnote.c | 395 ++++++++++++++---------------- 9 files changed, 219 insertions(+), 252 deletions(-) diff --git a/src/client/src/tscAsync.c b/src/client/src/tscAsync.c index 3ff8a68d8f..6b2722b43f 100644 --- a/src/client/src/tscAsync.c +++ b/src/client/src/tscAsync.c @@ -96,7 +96,7 @@ void taos_query_a(TAOS *taos, const char *sqlstr, __async_cb_func_t fp, void *pa return; } - taosNotePrintTsc(sqlstr); + nPrintTsc(sqlstr); SSqlObj *pSql = (SSqlObj *)calloc(1, sizeof(SSqlObj)); if (pSql == NULL) { diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index fae5b5856f..8e165241f0 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -344,7 +344,7 @@ TAOS_RES* taos_query_c(TAOS *taos, const char *sqlstr, uint32_t sqlLen, TAOS_RES return NULL; } - taosNotePrintTsc(sqlstr); + nPrintTsc(sqlstr); SSqlObj* pSql = calloc(1, sizeof(SSqlObj)); if (pSql == NULL) { diff --git a/src/client/src/tscSystem.c b/src/client/src/tscSystem.c index 03b6ac8404..132f8158fc 100644 --- a/src/client/src/tscSystem.c +++ b/src/client/src/tscSystem.c @@ -41,7 +41,6 @@ int tscRefId = -1; int tscNumOfThreads; static pthread_once_t tscinit = PTHREAD_ONCE_INIT; -void taosInitNote(int numOfNoteLines, int maxNotes, char* lable); //void tscUpdateEpSet(void *ahandle, SRpcEpSet *pEpSet); void tscCheckDiskUsage(void *UNUSED_PARAM(para), void* UNUSED_PARAM(param)) { @@ -78,7 +77,6 @@ int32_t tscInitRpc(const char *user, const char *secretEncrypt, void **pDnodeCon return 0; } - void taos_init_imp(void) { char temp[128] = {0}; @@ -111,11 +109,6 @@ void taos_init_imp(void) { } taosSetCoreDump(); - - if (tsTscEnableRecordSql != 0) { - taosInitNote(tsNumOfLogLines / 10, 1, (char*)"tsc_note"); - } - tscInitMsgsFp(); int queueSize = tsMaxConnections*2; diff --git a/src/dnode/src/dnodeMain.c b/src/dnode/src/dnodeMain.c index 502c410ce3..79efe5aa0e 100644 --- a/src/dnode/src/dnodeMain.c +++ b/src/dnode/src/dnodeMain.c @@ -16,6 +16,7 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taos.h" +#include "tnote.h" #include "tconfig.h" #include "tfile.h" #include "twal.h" @@ -98,6 +99,7 @@ int32_t dnodeInitSystem() { taosInitGlobalCfg(); taosReadGlobalLogCfg(); taosSetCoreDump(); + taosInitNotes(); signal(SIGPIPE, SIG_IGN); if (dnodeCreateDir(tsLogDir) < 0) { diff --git a/src/kit/shell/src/shellLinux.c b/src/kit/shell/src/shellLinux.c index f896253fb4..6f4ee3fc50 100644 --- a/src/kit/shell/src/shellLinux.c +++ b/src/kit/shell/src/shellLinux.c @@ -46,7 +46,7 @@ static struct argp_option options[] = { {"thread", 'T', "THREADNUM", 0, "Number of threads when using multi-thread to import data."}, {"database", 'd', "DATABASE", 0, "Database to use when connecting to the server."}, {"timezone", 't', "TIMEZONE", 0, "Time zone of the shell, default is local."}, - {"netrole", 'n', "NETROLE", 0, "Net role when network connectivity test, default is NULL, options: client|server|rpc|startup."}, + {"netrole", 'n', "NETROLE", 0, "Net role when network connectivity test, default is startup, options: client|server|rpc|startup."}, {"pktlen", 'l', "PKTLEN", 0, "Packet length used for net test, default is 1000 bytes."}, {0}}; diff --git a/src/plugins/http/src/httpSql.c b/src/plugins/http/src/httpSql.c index 564f555c40..3e517c6fa6 100644 --- a/src/plugins/http/src/httpSql.c +++ b/src/plugins/http/src/httpSql.c @@ -181,7 +181,7 @@ void httpProcessMultiSql(HttpContext *pContext) { char *sql = httpGetCmdsString(pContext, cmd->sql); httpTraceL("context:%p, fd:%d, user:%s, process pos:%d, start query, sql:%s", pContext, pContext->fd, pContext->user, multiCmds->pos, sql); - taosNotePrintHttp(sql); + nPrintHttp(sql); taos_query_a(pContext->session->taos, sql, httpProcessMultiSqlCallBack, (void *)pContext); } @@ -329,7 +329,7 @@ void httpProcessSingleSqlCmd(HttpContext *pContext) { } httpTraceL("context:%p, fd:%d, user:%s, start query, sql:%s", pContext, pContext->fd, pContext->user, sql); - taosNotePrintHttp(sql); + nPrintHttp(sql); taos_query_a(pSession->taos, sql, httpProcessSingleSqlCallBack, (void *)pContext); } diff --git a/src/plugins/http/src/httpSystem.c b/src/plugins/http/src/httpSystem.c index 3b8858b62e..34a70a658b 100644 --- a/src/plugins/http/src/httpSystem.c +++ b/src/plugins/http/src/httpSystem.c @@ -37,7 +37,6 @@ void opInitHandle(HttpServer* pServer) {} #endif HttpServer tsHttpServer; -void taosInitNote(int32_t numOfNoteLines, int32_t maxNotes, char* lable); int32_t httpInitSystem() { strcpy(tsHttpServer.label, "rest"); @@ -48,9 +47,6 @@ int32_t httpInitSystem() { pthread_mutex_init(&tsHttpServer.serverMutex, NULL); - if (tsHttpEnableRecordSql != 0) { - taosInitNote(tsNumOfLogLines / 10, 1, (char*)"http_note"); - } restInitHandle(&tsHttpServer); adminInitHandle(&tsHttpServer); gcInitHandle(&tsHttpServer); diff --git a/src/util/inc/tnote.h b/src/util/inc/tnote.h index 552224abf0..b28a53fc69 100644 --- a/src/util/inc/tnote.h +++ b/src/util/inc/tnote.h @@ -20,41 +20,38 @@ extern "C" { #endif -#include "os.h" -#include "tutil.h" -#include "tglobal.h" - #define MAX_NOTE_LINE_SIZE 66000 #define NOTE_FILE_NAME_LEN 300 - -typedef struct _taosNoteInfo { - int taosNoteFileNum ; - int taosNoteMaxLines; - int taosNoteLines; - char taosNoteName[NOTE_FILE_NAME_LEN]; - int taosNoteFlag; - int taosNoteFd; - int taosNoteOpenInProgress; - pthread_mutex_t taosNoteMutex; -}taosNoteInfo; - -void taosNotePrint(taosNoteInfo * pNote, const char * const format, ...); -extern taosNoteInfo m_HttpNote; -extern taosNoteInfo m_TscNote; +typedef struct { + int32_t fileNum; + int32_t maxLines; + int32_t lines; + int32_t flag; + int32_t fd; + int32_t openInProgress; + char name[NOTE_FILE_NAME_LEN]; + pthread_mutex_t mutex; +} SNoteObj; -extern int tsHttpEnableRecordSql; -extern int tsTscEnableRecordSql; +extern SNoteObj tsHttpNote; +extern SNoteObj tsTscNote; +extern SNoteObj tsErrorNote; -#define taosNotePrintHttp(...) \ +void taosInitNotes(); +void taosNotePrint(SNoteObj* pNote, const char* const format, ...); + +#define nPrintHttp(...) \ if (tsHttpEnableRecordSql) { \ - taosNotePrint(&m_HttpNote, __VA_ARGS__); \ + taosNotePrint(&tsHttpNote, __VA_ARGS__); \ } - -#define taosNotePrintTsc(...) \ - if (tsTscEnableRecordSql) { \ - taosNotePrint(&m_TscNote, __VA_ARGS__); \ - } + +#define nPrintTsc(...) \ + if (tsTscEnableRecordSql) { \ + taosNotePrint(&tsTscNote, __VA_ARGS__); \ + } + +#define nError(...) taosNotePrint(&tsErrorNote, __VA_ARGS__); #ifdef __cplusplus } diff --git a/src/util/src/tnote.c b/src/util/src/tnote.c index 9536f6fb70..04dd58afce 100644 --- a/src/util/src/tnote.c +++ b/src/util/src/tnote.c @@ -13,277 +13,256 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "os.h" +#include "tutil.h" +#include "tglobal.h" #include "tnote.h" -taosNoteInfo m_HttpNote; -taosNoteInfo m_TscNote; +SNoteObj tsHttpNote; +SNoteObj tsTscNote; +SNoteObj tsErrorNote; -int taosOpenNoteWithMaxLines(char *fn, int maxLines, int maxNoteNum, taosNoteInfo * pNote); +static int32_t taosOpenNoteWithMaxLines(char *fn, int32_t maxLines, int32_t maxNoteNum, SNoteObj * pNote); +static void taosCloseNoteByFd(int32_t oldFd, SNoteObj * pNote); -void taosInitNote(int numOfNoteLines, int maxNotes, char* lable) -{ - taosNoteInfo * pNote = NULL; - char temp[128] = { 0 }; +void taosInitNote(int32_t numOfLines, int32_t maxNotes, SNoteObj *pNote, char *name) { + memset(pNote, 0, sizeof(SNoteObj)); + pNote->fileNum = 1; + pNote->fd = -1; - if (strcasecmp(lable, "http_note") == 0) { - pNote = &m_HttpNote; - sprintf(temp, "%s/httpnote", tsLogDir); - } else if (strcasecmp(lable, "tsc_note") == 0) { - pNote = &m_TscNote; - sprintf(temp, "%s/tscnote-%d", tsLogDir, getpid()); - } else { - return; - } + if (taosOpenNoteWithMaxLines(name, numOfLines, maxNotes, pNote) < 0) { + fprintf(stderr, "failed to init note file\n"); + } - memset(pNote, 0, sizeof(taosNoteInfo)); - pNote->taosNoteFileNum = 1; - //pNote->taosNoteMaxLines = 0; - //pNote->taosNoteLines = 0; - //pNote->taosNoteFlag = 0; - pNote->taosNoteFd = -1; - //pNote->taosNoteOpenInProgress = 0; - - if (taosOpenNoteWithMaxLines(temp, numOfNoteLines, maxNotes, pNote) < 0) - fprintf(stderr, "failed to init note file\n"); - - taosNotePrint(pNote, "=================================================="); - taosNotePrint(pNote, "=================== new note ==================="); - taosNotePrint(pNote, "=================================================="); + taosNotePrint(pNote, "=================================================="); + taosNotePrint(pNote, "=================== new note ==================="); + taosNotePrint(pNote, "=================================================="); } -void taosCloseNoteByFd(int oldFd, taosNoteInfo * pNote); -bool taosLockNote(int fd, taosNoteInfo * pNote) -{ - if (fd < 0) return false; +void taosInitNotes() { + char name[TSDB_FILENAME_LEN * 2] = {0}; - if (pNote->taosNoteFileNum > 1) { - int ret = (int)(flock(fd, LOCK_EX | LOCK_NB)); - if (ret == 0) { - return true; - } - } + if (tsTscEnableRecordSql) { + snprintf(name, TSDB_FILENAME_LEN * 2, "%s/tscsql-%d", tsLogDir, taosGetPId()); + taosInitNote(tsNumOfLogLines, 1, &tsTscNote, name); + } - return false; + if (tsHttpEnableRecordSql) { + snprintf(name, TSDB_FILENAME_LEN * 2, "%s/httpsql", tsLogDir); + taosInitNote(tsNumOfLogLines, 1, &tsHttpNote, name); + } + + if (tscEmbedded == 0) { + snprintf(name, TSDB_FILENAME_LEN * 2, "%s/note", tsLogDir); + taosInitNote(tsNumOfLogLines, 1, &tsErrorNote, name); + } } -void taosUnLockNote(int fd, taosNoteInfo * pNote) -{ - if (fd < 0) return; +bool taosLockNote(int32_t fd, SNoteObj *pNote) { + if (fd < 0) return false; - if (pNote->taosNoteFileNum > 1) { - flock(fd, LOCK_UN | LOCK_NB); + if (pNote->fileNum > 1) { + int32_t ret = (int32_t)(flock(fd, LOCK_EX | LOCK_NB)); + if (ret == 0) { + return true; } + } + + return false; } -void *taosThreadToOpenNewNote(void *param) -{ - char name[NOTE_FILE_NAME_LEN * 2]; - taosNoteInfo * pNote = (taosNoteInfo *)param; +void taosUnLockNote(int32_t fd, SNoteObj *pNote) { + if (fd < 0) return; - pNote->taosNoteFlag ^= 1; - pNote->taosNoteLines = 0; - sprintf(name, "%s.%d", pNote->taosNoteName, pNote->taosNoteFlag); + if (pNote->fileNum > 1) { + flock(fd, LOCK_UN | LOCK_NB); + } +} - umask(0); +void *taosThreadToOpenNewNote(void *param) { + char name[NOTE_FILE_NAME_LEN * 2]; + SNoteObj *pNote = (SNoteObj *)param; - int fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); - if (fd < 0) { - return NULL; - } + pNote->flag ^= 1; + pNote->lines = 0; + sprintf(name, "%s.%d", pNote->name, pNote->flag); - taosLockNote(fd, pNote); - (void)lseek(fd, 0, SEEK_SET); + umask(0); - int oldFd = pNote->taosNoteFd; - pNote->taosNoteFd = fd; - pNote->taosNoteLines = 0; - pNote->taosNoteOpenInProgress = 0; - taosNotePrint(pNote, "=============== new note is opened ============="); - - taosCloseNoteByFd(oldFd, pNote); + int32_t fd = open(name, O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU | S_IRWXG | S_IRWXO); + if (fd < 0) { return NULL; + } + + taosLockNote(fd, pNote); + (void)lseek(fd, 0, SEEK_SET); + + int32_t oldFd = pNote->fd; + pNote->fd = fd; + pNote->lines = 0; + pNote->openInProgress = 0; + taosNotePrint(pNote, "=============== new note is opened ============="); + + taosCloseNoteByFd(oldFd, pNote); + return NULL; } -int taosOpenNewNote(taosNoteInfo * pNote) -{ - pthread_mutex_lock(&pNote->taosNoteMutex); +int32_t taosOpenNewNote(SNoteObj *pNote) { + pthread_mutex_lock(&pNote->mutex); - if (pNote->taosNoteLines > pNote->taosNoteMaxLines && pNote->taosNoteOpenInProgress == 0) { - pNote->taosNoteOpenInProgress = 1; + if (pNote->lines > pNote->maxLines && pNote->openInProgress == 0) { + pNote->openInProgress = 1; - taosNotePrint(pNote, "=============== open new note =================="); - pthread_t pattern; - pthread_attr_t attr; - pthread_attr_init(&attr); - pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + taosNotePrint(pNote, "=============== open new note =================="); + pthread_t pattern; + pthread_attr_t attr; + pthread_attr_init(&attr); + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); - pthread_create(&pattern, &attr, taosThreadToOpenNewNote, (void*)pNote); - pthread_attr_destroy(&attr); - } + pthread_create(&pattern, &attr, taosThreadToOpenNewNote, (void *)pNote); + pthread_attr_destroy(&attr); + } - pthread_mutex_unlock(&pNote->taosNoteMutex); + pthread_mutex_unlock(&pNote->mutex); - return pNote->taosNoteFd; + return pNote->fd; } -bool taosCheckNoteIsOpen(char *noteName, taosNoteInfo * pNote) -{ - /* - int exist = access(noteName, F_OK); - if (exist != 0) { - return false; - } - */ +bool taosCheckNoteIsOpen(char *noteName, SNoteObj *pNote) { + int32_t fd = open(noteName, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); + if (fd < 0) { + fprintf(stderr, "failed to open note:%s reason:%s\n", noteName, strerror(errno)); + return true; + } - int fd = open(noteName, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); - if (fd < 0) { - fprintf(stderr, "failed to open note:%s reason:%s\n", noteName, strerror(errno)); - return true; - } - - if (taosLockNote(fd, pNote)) { - taosUnLockNote(fd, pNote); - close(fd); - return false; - } - else { - close(fd); - return true; - } + if (taosLockNote(fd, pNote)) { + taosUnLockNote(fd, pNote); + close(fd); + return false; + } else { + close(fd); + return true; + } } -void taosGetNoteName(char *fn, taosNoteInfo * pNote) -{ - if (pNote->taosNoteFileNum > 1) { - for (int i = 0; i < pNote->taosNoteFileNum; i++) { - char fileName[NOTE_FILE_NAME_LEN]; +void taosGetNoteName(char *fn, SNoteObj *pNote) { + if (pNote->fileNum > 1) { + for (int32_t i = 0; i < pNote->fileNum; i++) { + char fileName[NOTE_FILE_NAME_LEN]; - sprintf(fileName, "%s%d.0", fn, i); - bool file1open = taosCheckNoteIsOpen(fileName, pNote); + sprintf(fileName, "%s%d.0", fn, i); + bool file1open = taosCheckNoteIsOpen(fileName, pNote); - sprintf(fileName, "%s%d.1", fn, i); - bool file2open = taosCheckNoteIsOpen(fileName, pNote); + sprintf(fileName, "%s%d.1", fn, i); + bool file2open = taosCheckNoteIsOpen(fileName, pNote); - if (!file1open && !file2open) { - sprintf(pNote->taosNoteName, "%s%d", fn, i); - return; - } - } + if (!file1open && !file2open) { + sprintf(pNote->name, "%s%d", fn, i); + return; + } } + } - if (strlen(fn) < NOTE_FILE_NAME_LEN) { - strcpy(pNote->taosNoteName, fn); - } + if (strlen(fn) < NOTE_FILE_NAME_LEN) { + strcpy(pNote->name, fn); + } } -int taosOpenNoteWithMaxLines(char *fn, int maxLines, int maxNoteNum, taosNoteInfo * pNote) -{ - char name[NOTE_FILE_NAME_LEN * 2] = "\0"; - struct stat notestat0, notestat1; - int size; +int32_t taosOpenNoteWithMaxLines(char *fn, int32_t maxLines, int32_t maxNoteNum, SNoteObj *pNote) { + char name[NOTE_FILE_NAME_LEN * 2] = "\0"; + int32_t size; + struct stat notestat0, notestat1; - pNote->taosNoteMaxLines = maxLines; - pNote->taosNoteFileNum = maxNoteNum; - taosGetNoteName(fn, pNote); + pNote->maxLines = maxLines; + pNote->fileNum = maxNoteNum; + taosGetNoteName(fn, pNote); - if (strlen(fn) > NOTE_FILE_NAME_LEN * 2 - 2) { - fprintf(stderr, "the len of file name overflow:%s\n", fn); - return -1; - } + if (strlen(fn) > NOTE_FILE_NAME_LEN * 2 - 2) { + fprintf(stderr, "the len of file name overflow:%s\n", fn); + return -1; + } + strcpy(name, fn); + strcat(name, ".0"); + + // if none of the note files exist, open 0, if both exists, open the old one + if (stat(name, ¬estat0) < 0) { + pNote->flag = 0; + } else { strcpy(name, fn); - strcat(name, ".0"); - - // if none of the note files exist, open 0, if both exists, open the old one - if (stat(name, ¬estat0) < 0) { - pNote->taosNoteFlag = 0; + strcat(name, ".1"); + if (stat(name, ¬estat1) < 0) { + pNote->flag = 1; } else { - strcpy(name, fn); - strcat(name, ".1"); - if (stat(name, ¬estat1) < 0) { - pNote->taosNoteFlag = 1; - } - else { - pNote->taosNoteFlag = (notestat0.st_mtime > notestat1.st_mtime) ? 0 : 1; - } + pNote->flag = (notestat0.st_mtime > notestat1.st_mtime) ? 0 : 1; } + } - char noteName[NOTE_FILE_NAME_LEN * 2] = "\0"; - sprintf(noteName, "%s.%d", pNote->taosNoteName, pNote->taosNoteFlag); - pthread_mutex_init(&pNote->taosNoteMutex, NULL); + char noteName[NOTE_FILE_NAME_LEN * 2] = "\0"; + sprintf(noteName, "%s.%d", pNote->name, pNote->flag); + pthread_mutex_init(&pNote->mutex, NULL); - umask(0); - pNote->taosNoteFd = open(noteName, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); + umask(0); + pNote->fd = open(noteName, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); - if (pNote->taosNoteFd < 0) { - fprintf(stderr, "failed to open note file:%s reason:%s\n", noteName, strerror(errno)); - return -1; - } - taosLockNote(pNote->taosNoteFd, pNote); + if (pNote->fd < 0) { + fprintf(stderr, "failed to open note file:%s reason:%s\n", noteName, strerror(errno)); + return -1; + } + taosLockNote(pNote->fd, pNote); - // only an estimate for number of lines - struct stat filestat; - if (fstat(pNote->taosNoteFd, &filestat) < 0) { - fprintf(stderr, "failed to fstat note file:%s reason:%s\n", noteName, strerror(errno)); - return -1; - } - size = (int)filestat.st_size; - pNote->taosNoteLines = size / 60; + // only an estimate for number of lines + struct stat filestat; + if (fstat(pNote->fd, &filestat) < 0) { + fprintf(stderr, "failed to fstat note file:%s reason:%s\n", noteName, strerror(errno)); + return -1; + } + size = (int32_t)filestat.st_size; + pNote->lines = size / 60; - lseek(pNote->taosNoteFd, 0, SEEK_END); + lseek(pNote->fd, 0, SEEK_END); - return 0; + return 0; } -void taosNotePrint(taosNoteInfo * pNote, const char * const format, ...) -{ - va_list argpointer; - char buffer[MAX_NOTE_LINE_SIZE+2]; - int len; - struct tm Tm, *ptm; - struct timeval timeSecs; - time_t curTime; +void taosNotePrint(SNoteObj *pNote, const char *const format, ...) { + va_list argpointer; + char buffer[MAX_NOTE_LINE_SIZE + 2]; + int32_t len; + struct tm Tm, *ptm; + struct timeval timeSecs; + time_t curTime; - gettimeofday(&timeSecs, NULL); - curTime = timeSecs.tv_sec; - ptm = localtime_r(&curTime, &Tm); -#ifndef LINUX - len = sprintf(buffer, "%02d/%02d %02d:%02d:%02d.%06d 0x%lld ", ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, - ptm->tm_min, ptm->tm_sec, (int)timeSecs.tv_usec, taosGetPthreadId()); -#else - len = sprintf(buffer, "%02d/%02d %02d:%02d:%02d.%06d %lx ", ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, ptm->tm_min, - ptm->tm_sec, (int)timeSecs.tv_usec, (unsigned long int)pthread_self()); -#endif - va_start(argpointer, format); - len += vsnprintf(buffer + len, MAX_NOTE_LINE_SIZE - len, format, argpointer); - va_end(argpointer); + gettimeofday(&timeSecs, NULL); + curTime = timeSecs.tv_sec; + ptm = localtime_r(&curTime, &Tm); + len = sprintf(buffer, "%02d/%02d %02d:%02d:%02d.%06d 0x%08" PRIx64 " ", ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour, + ptm->tm_min, ptm->tm_sec, (int32_t)timeSecs.tv_usec, taosGetPthreadId()); + va_start(argpointer, format); + len += vsnprintf(buffer + len, MAX_NOTE_LINE_SIZE - len, format, argpointer); + va_end(argpointer); - if (len >= MAX_NOTE_LINE_SIZE) len = MAX_NOTE_LINE_SIZE - 2; + if (len >= MAX_NOTE_LINE_SIZE) len = MAX_NOTE_LINE_SIZE - 2; - buffer[len++] = '\n'; - buffer[len] = 0; + buffer[len++] = '\n'; + buffer[len] = 0; - if (pNote->taosNoteFd >= 0) { - taosWrite(pNote->taosNoteFd, buffer, (unsigned int)len); + if (pNote->fd >= 0) { + taosWrite(pNote->fd, buffer, len); - if (pNote->taosNoteMaxLines > 0) { - pNote->taosNoteLines++; - if ((pNote->taosNoteLines > pNote->taosNoteMaxLines) && (pNote->taosNoteOpenInProgress == 0)) - taosOpenNewNote(pNote); - } + if (pNote->maxLines > 0) { + pNote->lines++; + if ((pNote->lines > pNote->maxLines) && (pNote->openInProgress == 0)) taosOpenNewNote(pNote); } + } } -void taosCloseNote(taosNoteInfo * pNote) -{ - taosCloseNoteByFd(pNote->taosNoteFd, pNote); -} +void taosCloseNote(SNoteObj *pNote) { taosCloseNoteByFd(pNote->fd, pNote); } -void taosCloseNoteByFd(int fd, taosNoteInfo * pNote) -{ - if (fd >= 0) { - taosUnLockNote(fd, pNote); - close(fd); - } +void taosCloseNoteByFd(int32_t fd, SNoteObj *pNote) { + if (fd >= 0) { + taosUnLockNote(fd, pNote); + close(fd); + } } From aa3df5f7fac7976f1adf267977bce3d8062944d9 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 8 Dec 2020 10:28:13 +0800 Subject: [PATCH 2/4] TD-2371 --- src/util/src/tlog.c | 1 - src/util/src/tnote.c | 39 ++++++++++++++++++++------------------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/src/util/src/tlog.c b/src/util/src/tlog.c index ad3a922304..0ba5e49c6e 100644 --- a/src/util/src/tlog.c +++ b/src/util/src/tlog.c @@ -287,7 +287,6 @@ static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) { tsLogObj.fileNum = maxFileNum; taosGetLogFileName(fn); - if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) { strcpy(name, fn); strcat(name, ".0"); diff --git a/src/util/src/tnote.c b/src/util/src/tnote.c index 04dd58afce..3338d8f301 100644 --- a/src/util/src/tnote.c +++ b/src/util/src/tnote.c @@ -53,7 +53,7 @@ void taosInitNotes() { taosInitNote(tsNumOfLogLines, 1, &tsHttpNote, name); } - if (tscEmbedded == 0) { + if (tscEmbedded == 1) { snprintf(name, TSDB_FILENAME_LEN * 2, "%s/note", tsLogDir); taosInitNote(tsNumOfLogLines, 1, &tsErrorNote, name); } @@ -170,36 +170,37 @@ void taosGetNoteName(char *fn, SNoteObj *pNote) { } int32_t taosOpenNoteWithMaxLines(char *fn, int32_t maxLines, int32_t maxNoteNum, SNoteObj *pNote) { - char name[NOTE_FILE_NAME_LEN * 2] = "\0"; + char name[NOTE_FILE_NAME_LEN * 2] = {0}; int32_t size; - struct stat notestat0, notestat1; + struct stat logstat0, logstat1; pNote->maxLines = maxLines; pNote->fileNum = maxNoteNum; taosGetNoteName(fn, pNote); - if (strlen(fn) > NOTE_FILE_NAME_LEN * 2 - 2) { - fprintf(stderr, "the len of file name overflow:%s\n", fn); - return -1; + if (strlen(fn) < NOTE_FILE_NAME_LEN + 50 - 2) { + strcpy(name, fn); + strcat(name, ".0"); } + bool log0Exist = stat(name, &logstat0) >= 0; - strcpy(name, fn); - strcat(name, ".0"); - - // if none of the note files exist, open 0, if both exists, open the old one - if (stat(name, ¬estat0) < 0) { - pNote->flag = 0; - } else { + if (strlen(fn) < NOTE_FILE_NAME_LEN + 50 - 2) { strcpy(name, fn); strcat(name, ".1"); - if (stat(name, ¬estat1) < 0) { - pNote->flag = 1; - } else { - pNote->flag = (notestat0.st_mtime > notestat1.st_mtime) ? 0 : 1; - } + } + bool log1Exist = stat(name, &logstat1) >= 0; + + if (!log0Exist && !log1Exist) { + pNote->flag = 0; + } else if (!log1Exist) { + pNote->flag = 0; + } else if (!log0Exist) { + pNote->flag = 1; + } else { + pNote->flag = (logstat0.st_mtime > logstat1.st_mtime) ? 0 : 1; } - char noteName[NOTE_FILE_NAME_LEN * 2] = "\0"; + char noteName[NOTE_FILE_NAME_LEN * 2] = {0}; sprintf(noteName, "%s.%d", pNote->name, pNote->flag); pthread_mutex_init(&pNote->mutex, NULL); From c3a7a9576daf9e2e1421d3054c743cdba58ead87 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 8 Dec 2020 10:56:50 +0800 Subject: [PATCH 3/4] TD-2371 --- src/util/inc/tnote.h | 5 +++-- src/util/src/tlog.c | 2 ++ src/util/src/tnote.c | 49 +++++++++++++++++++++++--------------------- 3 files changed, 31 insertions(+), 25 deletions(-) diff --git a/src/util/inc/tnote.h b/src/util/inc/tnote.h index b28a53fc69..b4c0c7501b 100644 --- a/src/util/inc/tnote.h +++ b/src/util/inc/tnote.h @@ -36,10 +36,11 @@ typedef struct { extern SNoteObj tsHttpNote; extern SNoteObj tsTscNote; -extern SNoteObj tsErrorNote; +extern SNoteObj tsInfoNote; void taosInitNotes(); void taosNotePrint(SNoteObj* pNote, const char* const format, ...); +void taosNotePrintBuffer(SNoteObj *pNote, char *buffer, int32_t len); #define nPrintHttp(...) \ if (tsHttpEnableRecordSql) { \ @@ -51,7 +52,7 @@ void taosNotePrint(SNoteObj* pNote, const char* const format, ...); taosNotePrint(&tsTscNote, __VA_ARGS__); \ } -#define nError(...) taosNotePrint(&tsErrorNote, __VA_ARGS__); +#define nInfo(buffer, len) taosNotePrintBuffer(&tsInfoNote, buffer, len); #ifdef __cplusplus } diff --git a/src/util/src/tlog.c b/src/util/src/tlog.c index 0ba5e49c6e..c0f89e8465 100644 --- a/src/util/src/tlog.c +++ b/src/util/src/tlog.c @@ -17,6 +17,7 @@ #include "os.h" #include "tulog.h" #include "tlog.h" +#include "tnote.h" #include "tutil.h" #define MAX_LOGLINE_SIZE (1000) @@ -400,6 +401,7 @@ void taosPrintLog(const char *flags, int32_t dflag, const char *format, ...) { } if (dflag & DEBUG_SCREEN) taosWrite(1, buffer, (uint32_t)len); + if (dflag == 255) nInfo(buffer, len); } void taosDumpData(unsigned char *msg, int32_t len) { diff --git a/src/util/src/tnote.c b/src/util/src/tnote.c index 3338d8f301..82ce2c314d 100644 --- a/src/util/src/tnote.c +++ b/src/util/src/tnote.c @@ -21,12 +21,12 @@ SNoteObj tsHttpNote; SNoteObj tsTscNote; -SNoteObj tsErrorNote; +SNoteObj tsInfoNote; -static int32_t taosOpenNoteWithMaxLines(char *fn, int32_t maxLines, int32_t maxNoteNum, SNoteObj * pNote); -static void taosCloseNoteByFd(int32_t oldFd, SNoteObj * pNote); +static int32_t taosOpenNoteWithMaxLines(char *fn, int32_t maxLines, int32_t maxNoteNum, SNoteObj *pNote); +static void taosCloseNoteByFd(int32_t oldFd, SNoteObj *pNote); -void taosInitNote(int32_t numOfLines, int32_t maxNotes, SNoteObj *pNote, char *name) { +static void taosInitNote(int32_t numOfLines, int32_t maxNotes, SNoteObj *pNote, char *name) { memset(pNote, 0, sizeof(SNoteObj)); pNote->fileNum = 1; pNote->fd = -1; @@ -54,12 +54,12 @@ void taosInitNotes() { } if (tscEmbedded == 1) { - snprintf(name, TSDB_FILENAME_LEN * 2, "%s/note", tsLogDir); - taosInitNote(tsNumOfLogLines, 1, &tsErrorNote, name); + snprintf(name, TSDB_FILENAME_LEN * 2, "%s/taosinfo", tsLogDir); + taosInitNote(tsNumOfLogLines, 1, &tsInfoNote, name); } } -bool taosLockNote(int32_t fd, SNoteObj *pNote) { +static bool taosLockNote(int32_t fd, SNoteObj *pNote) { if (fd < 0) return false; if (pNote->fileNum > 1) { @@ -72,7 +72,7 @@ bool taosLockNote(int32_t fd, SNoteObj *pNote) { return false; } -void taosUnLockNote(int32_t fd, SNoteObj *pNote) { +static void taosUnLockNote(int32_t fd, SNoteObj *pNote) { if (fd < 0) return; if (pNote->fileNum > 1) { @@ -80,7 +80,7 @@ void taosUnLockNote(int32_t fd, SNoteObj *pNote) { } } -void *taosThreadToOpenNewNote(void *param) { +static void *taosThreadToOpenNewNote(void *param) { char name[NOTE_FILE_NAME_LEN * 2]; SNoteObj *pNote = (SNoteObj *)param; @@ -108,7 +108,7 @@ void *taosThreadToOpenNewNote(void *param) { return NULL; } -int32_t taosOpenNewNote(SNoteObj *pNote) { +static int32_t taosOpenNewNote(SNoteObj *pNote) { pthread_mutex_lock(&pNote->mutex); if (pNote->lines > pNote->maxLines && pNote->openInProgress == 0) { @@ -129,7 +129,7 @@ int32_t taosOpenNewNote(SNoteObj *pNote) { return pNote->fd; } -bool taosCheckNoteIsOpen(char *noteName, SNoteObj *pNote) { +static bool taosCheckNoteIsOpen(char *noteName, SNoteObj *pNote) { int32_t fd = open(noteName, O_WRONLY | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO); if (fd < 0) { fprintf(stderr, "failed to open note:%s reason:%s\n", noteName, strerror(errno)); @@ -146,7 +146,7 @@ bool taosCheckNoteIsOpen(char *noteName, SNoteObj *pNote) { } } -void taosGetNoteName(char *fn, SNoteObj *pNote) { +static void taosGetNoteName(char *fn, SNoteObj *pNote) { if (pNote->fileNum > 1) { for (int32_t i = 0; i < pNote->fileNum; i++) { char fileName[NOTE_FILE_NAME_LEN]; @@ -169,7 +169,7 @@ void taosGetNoteName(char *fn, SNoteObj *pNote) { } } -int32_t taosOpenNoteWithMaxLines(char *fn, int32_t maxLines, int32_t maxNoteNum, SNoteObj *pNote) { +static int32_t taosOpenNoteWithMaxLines(char *fn, int32_t maxLines, int32_t maxNoteNum, SNoteObj *pNote) { char name[NOTE_FILE_NAME_LEN * 2] = {0}; int32_t size; struct stat logstat0, logstat1; @@ -227,6 +227,16 @@ int32_t taosOpenNoteWithMaxLines(char *fn, int32_t maxLines, int32_t maxNoteNum, return 0; } +void taosNotePrintBuffer(SNoteObj *pNote, char *buffer, int32_t len) { + if (pNote->fd < 0) return; + taosWrite(pNote->fd, buffer, len); + + if (pNote->maxLines > 0) { + pNote->lines++; + if ((pNote->lines > pNote->maxLines) && (pNote->openInProgress == 0)) taosOpenNewNote(pNote); + } +} + void taosNotePrint(SNoteObj *pNote, const char *const format, ...) { va_list argpointer; char buffer[MAX_NOTE_LINE_SIZE + 2]; @@ -249,19 +259,12 @@ void taosNotePrint(SNoteObj *pNote, const char *const format, ...) { buffer[len++] = '\n'; buffer[len] = 0; - if (pNote->fd >= 0) { - taosWrite(pNote->fd, buffer, len); - - if (pNote->maxLines > 0) { - pNote->lines++; - if ((pNote->lines > pNote->maxLines) && (pNote->openInProgress == 0)) taosOpenNewNote(pNote); - } - } + taosNotePrintBuffer(pNote, buffer, len); } -void taosCloseNote(SNoteObj *pNote) { taosCloseNoteByFd(pNote->fd, pNote); } +// static void taosCloseNote(SNoteObj *pNote) { taosCloseNoteByFd(pNote->fd, pNote); } -void taosCloseNoteByFd(int32_t fd, SNoteObj *pNote) { +static void taosCloseNoteByFd(int32_t fd, SNoteObj *pNote) { if (fd >= 0) { taosUnLockNote(fd, pNote); close(fd); From b21f78d3206fdd72746ff255802adabc8c0bb26e Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 8 Dec 2020 11:10:04 +0800 Subject: [PATCH 4/4] TD-2371 --- src/client/src/tscSystem.c | 2 ++ src/util/inc/tnote.h | 5 ++++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/src/client/src/tscSystem.c b/src/client/src/tscSystem.c index 132f8158fc..1eddeacc65 100644 --- a/src/client/src/tscSystem.c +++ b/src/client/src/tscSystem.c @@ -17,6 +17,7 @@ #include "taosmsg.h" #include "tref.h" #include "trpc.h" +#include "tnote.h" #include "tsystem.h" #include "ttimer.h" #include "tutil.h" @@ -102,6 +103,7 @@ void taos_init_imp(void) { taosReadGlobalCfg(); taosCheckGlobalCfg(); + taosInitNotes(); rpcInit(); tscDebug("starting to initialize TAOS client ..."); diff --git a/src/util/inc/tnote.h b/src/util/inc/tnote.h index b4c0c7501b..7511b61f41 100644 --- a/src/util/inc/tnote.h +++ b/src/util/inc/tnote.h @@ -52,7 +52,10 @@ void taosNotePrintBuffer(SNoteObj *pNote, char *buffer, int32_t len); taosNotePrint(&tsTscNote, __VA_ARGS__); \ } -#define nInfo(buffer, len) taosNotePrintBuffer(&tsInfoNote, buffer, len); +#define nInfo(buffer, len) \ + if (tscEmbedded == 1) { \ + taosNotePrintBuffer(&tsInfoNote, buffer, len); \ + } #ifdef __cplusplus }