tlog.c 20.8 KB
Newer Older
H
hzcheng 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
 *
 * This program is free software: you can use, redistribute, and/or modify
 * it under the terms of the GNU Affero General Public License, version 3
 * or later ("AGPL"), as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

S
slguan 已提交
16
#define _DEFAULT_SOURCE
S
log  
Shengliang Guan 已提交
17
#include "tlog.h"
H
Hongze Cheng 已提交
18
#include "os.h"
S
Shengliang Guan 已提交
19
#include "tutil.h"
L
Liu Jicong 已提交
20

S
Shengliang Guan 已提交
21 22 23 24
#define LOG_MAX_LINE_SIZE             (1024)
#define LOG_MAX_LINE_BUFFER_SIZE      (LOG_MAX_LINE_SIZE + 3)
#define LOG_MAX_LINE_DUMP_SIZE        (65 * 1024)
#define LOG_MAX_LINE_DUMP_BUFFER_SIZE (LOG_MAX_LINE_DUMP_SIZE + 3)
S
slguan 已提交
25

S
ulog  
Shengliang Guan 已提交
26 27
#define LOG_FILE_NAME_LEN    300
#define LOG_DEFAULT_BUF_SIZE (20 * 1024 * 1024)  // 20MB
H
hzcheng 已提交
28

S
ulog  
Shengliang Guan 已提交
29 30 31 32 33
#define LOG_DEFAULT_INTERVAL 25
#define LOG_INTERVAL_STEP    5
#define LOG_MIN_INTERVAL     5
#define LOG_MAX_INTERVAL     25
#define LOG_MAX_WAIT_MSEC    1000
D
fix bug  
dapan1121 已提交
34

S
slguan 已提交
35
#define LOG_BUF_BUFFER(x) ((x)->buffer)
S
ulog  
Shengliang Guan 已提交
36 37 38 39
#define LOG_BUF_START(x)  ((x)->buffStart)
#define LOG_BUF_END(x)    ((x)->buffEnd)
#define LOG_BUF_SIZE(x)   ((x)->buffSize)
#define LOG_BUF_MUTEX(x)  ((x)->buffMutex)
S
slguan 已提交
40

H
hzcheng 已提交
41
typedef struct {
dengyihao's avatar
dengyihao 已提交
42
  char *        buffer;
43 44 45 46 47 48 49
  int32_t       buffStart;
  int32_t       buffEnd;
  int32_t       buffSize;
  int32_t       minBuffSize;
  TdFilePtr     pFile;
  int32_t       stop;
  TdThread      asyncThread;
wafwerar's avatar
wafwerar 已提交
50
  TdThreadMutex buffMutex;
H
hzcheng 已提交
51 52
} SLogBuff;

S
slguan 已提交
53
typedef struct {
54 55 56 57 58 59 60
  int32_t       fileNum;
  int32_t       maxLines;
  int32_t       lines;
  int32_t       flag;
  int32_t       openInProgress;
  pid_t         pid;
  char          logName[LOG_FILE_NAME_LEN];
dengyihao's avatar
dengyihao 已提交
61
  SLogBuff *    logHandle;
wafwerar's avatar
wafwerar 已提交
62
  TdThreadMutex logMutex;
S
slguan 已提交
63 64
} SLogObj;

S
Shengliang Guan 已提交
65 66
static int8_t  tsLogInited = 0;
static SLogObj tsLogObj = {.fileNum = 1};
S
Shengliang Guan 已提交
67 68
static int64_t tsAsyncLogLostLines = 0;
static int32_t tsWriteInterval = LOG_DEFAULT_INTERVAL;
L
Liu Jicong 已提交
69

S
Shengliang Guan 已提交
70
bool    tsLogEmbedded = 0;
S
Shengliang Guan 已提交
71
bool    tsAsyncLog = true;
S
Shengliang Guan 已提交
72
int32_t tsNumOfLogLines = 10000000;
S
Shengliang Guan 已提交
73
int32_t tsLogKeepDays = 0;
S
Shengliang Guan 已提交
74
LogFp   tsLogFp = NULL;
S
Shengliang Guan 已提交
75 76 77 78
int64_t tsNumOfErrorLogs = 0;
int64_t tsNumOfInfoLogs = 0;
int64_t tsNumOfDebugLogs = 0;
int64_t tsNumOfTraceLogs = 0;
D
fix bug  
dapan1121 已提交
79

S
Shengliang Guan 已提交
80 81 82
// log
int32_t dDebugFlag = 135;
int32_t vDebugFlag = 135;
S
Shengliang Guan 已提交
83
int32_t mDebugFlag = 131;
S
Shengliang Guan 已提交
84 85
int32_t cDebugFlag = 131;
int32_t jniDebugFlag = 131;
S
Shengliang Guan 已提交
86
int32_t tmrDebugFlag = 131;
S
Shengliang Guan 已提交
87
int32_t uDebugFlag = 131;
S
Shengliang Guan 已提交
88 89
int32_t rpcDebugFlag = 131;
int32_t qDebugFlag = 131;
S
Shengliang Guan 已提交
90
int32_t wDebugFlag = 135;
S
Shengliang Guan 已提交
91
int32_t sDebugFlag = 135;
S
Shengliang Guan 已提交
92
int32_t tsdbDebugFlag = 131;
H
Hongze Cheng 已提交
93
int32_t tdbDebugFlag = 131;
L
Liu Jicong 已提交
94
int32_t tqDebugFlag = 135;
S
Shengliang Guan 已提交
95
int32_t fsDebugFlag = 135;
H
Hongze Cheng 已提交
96
int32_t metaDebugFlag = 135;
S
Shengliang Guan 已提交
97
int32_t udfDebugFlag = 135;
98
int32_t smaDebugFlag = 135;
dengyihao's avatar
dengyihao 已提交
99
int32_t idxDebugFlag = 135;
D
dapan1121 已提交
100

D
fix bug  
dapan1121 已提交
101
int64_t dbgEmptyW = 0;
D
fix bug  
dapan1121 已提交
102 103 104
int64_t dbgWN = 0;
int64_t dbgSmallWN = 0;
int64_t dbgBigWN = 0;
D
fix bug  
dapan1121 已提交
105 106
int64_t dbgWSize = 0;

dengyihao's avatar
dengyihao 已提交
107
static void *    taosAsyncOutputLog(void *param);
108
static int32_t   taosPushLogBuffer(SLogBuff *pLogBuf, const char *msg, int32_t msgLen);
S
slguan 已提交
109
static SLogBuff *taosLogBuffNew(int32_t bufSize);
110
static void      taosCloseLogByFd(TdFilePtr pFile);
S
slguan 已提交
111
static int32_t   taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum);
S
Shengliang Guan 已提交
112
static int32_t   taosCompressFile(char *srcFileName, char *destFileName);
S
slguan 已提交
113 114

static int32_t taosStartLog() {
wafwerar's avatar
wafwerar 已提交
115 116 117
  TdThreadAttr threadAttr;
  taosThreadAttrInit(&threadAttr);
  if (taosThreadCreate(&(tsLogObj.logHandle->asyncThread), &threadAttr, taosAsyncOutputLog, tsLogObj.logHandle) != 0) {
H
hzcheng 已提交
118 119
    return -1;
  }
wafwerar's avatar
wafwerar 已提交
120
  taosThreadAttrDestroy(&threadAttr);
H
hzcheng 已提交
121 122 123
  return 0;
}

S
Shengliang Guan 已提交
124
int32_t taosInitLog(const char *logName, int32_t maxFiles) {
S
Shengliang Guan 已提交
125
  if (atomic_val_compare_exchange_8(&tsLogInited, 0, 1) != 0) return 0;
S
osenv  
Shengliang Guan 已提交
126
  osUpdate();
S
Shengliang Guan 已提交
127

S
Shengliang Guan 已提交
128
  char fullName[PATH_MAX] = {0};
S
os env  
Shengliang Guan 已提交
129
  snprintf(fullName, PATH_MAX, "%s" TD_DIRSEP "%s", tsLogDir, logName);
S
Shengliang Guan 已提交
130

S
ulog  
Shengliang Guan 已提交
131
  tsLogObj.logHandle = taosLogBuffNew(LOG_DEFAULT_BUF_SIZE);
S
slguan 已提交
132
  if (tsLogObj.logHandle == NULL) return -1;
S
Shengliang Guan 已提交
133
  if (taosOpenLogFile(fullName, tsNumOfLogLines, maxFiles) < 0) return -1;
H
hzcheng 已提交
134 135 136 137
  if (taosStartLog() < 0) return -1;
  return 0;
}

S
slguan 已提交
138 139 140 141
static void taosStopLog() {
  if (tsLogObj.logHandle) {
    tsLogObj.logHandle->stop = 1;
  }
H
hzcheng 已提交
142 143
}

S
slguan 已提交
144
void taosCloseLog() {
145 146 147 148
  if (tsLogObj.logHandle != NULL) {
    taosStopLog();
    if (tsLogObj.logHandle != NULL && taosCheckPthreadValid(tsLogObj.logHandle->asyncThread)) {
      taosThreadJoin(tsLogObj.logHandle->asyncThread, NULL);
149
      taosThreadClear(&tsLogObj.logHandle->asyncThread);
150 151 152 153 154 155 156 157 158 159 160
    }
    tsLogInited = 0;

    taosThreadMutexDestroy(&tsLogObj.logHandle->buffMutex);
    taosCloseFile(&tsLogObj.logHandle->pFile);
    taosMemoryFreeClear(tsLogObj.logHandle->buffer);
    memset(&tsLogObj.logHandle->buffer, 0, sizeof(tsLogObj.logHandle->buffer));
    taosThreadMutexDestroy(&tsLogObj.logMutex);
    taosMemoryFreeClear(tsLogObj.logHandle);
    memset(&tsLogObj.logHandle, 0, sizeof(tsLogObj.logHandle));
    tsLogObj.logHandle = NULL;
S
slguan 已提交
161
  }
H
hzcheng 已提交
162 163
}

164 165
static bool taosLockLogFile(TdFilePtr pFile) {
  if (pFile == NULL) return false;
H
hzcheng 已提交
166

S
slguan 已提交
167
  if (tsLogObj.fileNum > 1) {
168
    int32_t ret = taosLockFile(pFile);
H
hzcheng 已提交
169 170 171 172 173 174 175 176
    if (ret == 0) {
      return true;
    }
  }

  return false;
}

177 178
static void taosUnLockLogFile(TdFilePtr pFile) {
  if (pFile == NULL) return;
H
hzcheng 已提交
179

S
slguan 已提交
180
  if (tsLogObj.fileNum > 1) {
181
    taosUnLockFile(pFile);
H
hzcheng 已提交
182 183 184
  }
}

S
TD-1263  
Shengliang Guan 已提交
185
static void taosKeepOldLog(char *oldName) {
S
TD-1574  
Shengliang Guan 已提交
186
  if (tsLogKeepDays == 0) return;
S
TD-1263  
Shengliang Guan 已提交
187

S
TD-1263  
Shengliang Guan 已提交
188
  int64_t fileSec = taosGetTimestampSec();
S
TD-1263  
Shengliang Guan 已提交
189
  char    fileName[LOG_FILE_NAME_LEN + 20];
S
TD-1263  
Shengliang Guan 已提交
190
  snprintf(fileName, LOG_FILE_NAME_LEN + 20, "%s.%" PRId64, tsLogObj.logName, fileSec);
S
TD-1263  
Shengliang Guan 已提交
191

S
Shengliang Guan 已提交
192
  taosRenameFile(oldName, fileName);
S
TD-1574  
Shengliang Guan 已提交
193 194 195 196
  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) {
197
      (void)taosRemoveFile(fileName);
S
TD-1574  
Shengliang Guan 已提交
198 199 200
    }
  }

S
os env  
Shengliang Guan 已提交
201
  taosRemoveOldFiles(tsLogDir, TABS(tsLogKeepDays));
S
TD-1263  
Shengliang Guan 已提交
202 203
}

S
slguan 已提交
204
static void *taosThreadToOpenNewFile(void *param) {
S
TD-1263  
Shengliang Guan 已提交
205 206
  char keepName[LOG_FILE_NAME_LEN + 20];
  sprintf(keepName, "%s.%d", tsLogObj.logName, tsLogObj.flag);
H
hzcheng 已提交
207

S
slguan 已提交
208 209
  tsLogObj.flag ^= 1;
  tsLogObj.lines = 0;
S
TD-1263  
Shengliang Guan 已提交
210
  char name[LOG_FILE_NAME_LEN + 20];
S
slguan 已提交
211
  sprintf(name, "%s.%d", tsLogObj.logName, tsLogObj.flag);
H
hzcheng 已提交
212

S
Shengliang Guan 已提交
213
  taosUmaskFile(0);
H
hzcheng 已提交
214

215
  TdFilePtr pFile = taosOpenFile(name, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
216
  if (pFile == NULL) {
217 218
    tsLogObj.openInProgress = 0;
    tsLogObj.lines = tsLogObj.maxLines - 1000;
219
    uError("open new log file fail! reason:%s, reuse lastlog", strerror(errno));
H
Hui Li 已提交
220 221
    return NULL;
  }
S
TD-1263  
Shengliang Guan 已提交
222

223 224
  taosLockLogFile(pFile);
  (void)taosLSeekFile(pFile, 0, SEEK_SET);
H
hzcheng 已提交
225

226 227
  TdFilePtr pOldFile = tsLogObj.logHandle->pFile;
  tsLogObj.logHandle->pFile = pFile;
S
slguan 已提交
228 229
  tsLogObj.lines = 0;
  tsLogObj.openInProgress = 0;
wafwerar's avatar
wafwerar 已提交
230
  taosSsleep(20);
231
  taosCloseLogByFd(pOldFile);
L
Liu Jicong 已提交
232

S
TD-1263  
Shengliang Guan 已提交
233 234
  uInfo("   new log file:%d is opened", tsLogObj.flag);
  uInfo("==================================");
S
TD-1263  
Shengliang Guan 已提交
235 236
  taosKeepOldLog(keepName);

H
hzcheng 已提交
237 238 239
  return NULL;
}

S
slguan 已提交
240
static int32_t taosOpenNewLogFile() {
wafwerar's avatar
wafwerar 已提交
241
  taosThreadMutexLock(&tsLogObj.logMutex);
H
hzcheng 已提交
242

S
slguan 已提交
243 244
  if (tsLogObj.lines > tsLogObj.maxLines && tsLogObj.openInProgress == 0) {
    tsLogObj.openInProgress = 1;
H
hzcheng 已提交
245

246
    uInfo("open new log file ......");
247
    TdThread     thread;
wafwerar's avatar
wafwerar 已提交
248 249 250
    TdThreadAttr attr;
    taosThreadAttrInit(&attr);
    taosThreadAttrSetDetachState(&attr, PTHREAD_CREATE_DETACHED);
H
hzcheng 已提交
251

wafwerar's avatar
wafwerar 已提交
252 253
    taosThreadCreate(&thread, &attr, taosThreadToOpenNewFile, NULL);
    taosThreadAttrDestroy(&attr);
H
hzcheng 已提交
254 255
  }

wafwerar's avatar
wafwerar 已提交
256
  taosThreadMutexUnlock(&tsLogObj.logMutex);
H
hzcheng 已提交
257 258 259 260

  return 0;
}

S
slguan 已提交
261 262 263
void taosResetLog() {
  char lastName[LOG_FILE_NAME_LEN + 20];
  sprintf(lastName, "%s.%d", tsLogObj.logName, tsLogObj.flag);
H
hzcheng 已提交
264 265

  // force create a new log file
S
slguan 已提交
266
  tsLogObj.lines = tsLogObj.maxLines + 10;
H
hzcheng 已提交
267 268

  taosOpenNewLogFile();
269
  (void)taosRemoveFile(lastName);
H
hzcheng 已提交
270

271 272
  uInfo("==================================");
  uInfo("   reset log file ");
H
hzcheng 已提交
273 274
}

S
slguan 已提交
275
static bool taosCheckFileIsOpen(char *logFileName) {
276 277
  TdFilePtr pFile = taosOpenFile(logFileName, TD_FILE_WRITE);
  if (pFile == NULL) {
278 279 280 281 282 283
    if (errno == ENOENT) {
      return false;
    } else {
      printf("\nfailed to open log file:%s, reason:%s\n", logFileName, strerror(errno));
      return true;
    }
H
hzcheng 已提交
284 285
  }

286 287 288
  if (taosLockLogFile(pFile)) {
    taosUnLockLogFile(pFile);
    taosCloseFile(&pFile);
H
hzcheng 已提交
289 290
    return false;
  } else {
291
    taosCloseFile(&pFile);
H
hzcheng 已提交
292 293 294 295
    return true;
  }
}

S
slguan 已提交
296 297 298
static void taosGetLogFileName(char *fn) {
  if (tsLogObj.fileNum > 1) {
    for (int32_t i = 0; i < tsLogObj.fileNum; i++) {
H
hzcheng 已提交
299 300 301 302 303 304 305 306 307
      char fileName[LOG_FILE_NAME_LEN];

      sprintf(fileName, "%s%d.0", fn, i);
      bool file1open = taosCheckFileIsOpen(fileName);

      sprintf(fileName, "%s%d.1", fn, i);
      bool file2open = taosCheckFileIsOpen(fileName);

      if (!file1open && !file2open) {
S
slguan 已提交
308
        sprintf(tsLogObj.logName, "%s%d", fn, i);
H
hzcheng 已提交
309 310 311 312 313
        return;
      }
    }
  }

H
Hui Li 已提交
314 315 316
  if (strlen(fn) < LOG_FILE_NAME_LEN) {
    strcpy(tsLogObj.logName, fn);
  }
H
hzcheng 已提交
317 318
}

S
slguan 已提交
319
static int32_t taosOpenLogFile(char *fn, int32_t maxLines, int32_t maxFileNum) {
S
slguan 已提交
320 321
#ifdef WINDOWS
  /*
L
Liu Jicong 已提交
322 323 324
   * always set maxFileNum to 1
   * means client log filename is unique in windows
   */
S
slguan 已提交
325 326 327
  maxFileNum = 1;
#endif

S
Shengliang Guan 已提交
328 329 330
  char    name[LOG_FILE_NAME_LEN + 50] = "\0";
  int32_t logstat0_mtime, logstat1_mtime;
  int32_t size;
H
hzcheng 已提交
331

S
slguan 已提交
332 333
  tsLogObj.maxLines = maxLines;
  tsLogObj.fileNum = maxFileNum;
H
hzcheng 已提交
334 335
  taosGetLogFileName(fn);

H
Hui Li 已提交
336 337 338 339
  if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) {
    strcpy(name, fn);
    strcat(name, ".0");
  }
S
Shengliang Guan 已提交
340
  bool log0Exist = taosStatFile(name, NULL, &logstat0_mtime) >= 0;
H
hzcheng 已提交
341

S
TD-1263  
Shengliang Guan 已提交
342 343 344 345
  if (strlen(fn) < LOG_FILE_NAME_LEN + 50 - 2) {
    strcpy(name, fn);
    strcat(name, ".1");
  }
S
Shengliang Guan 已提交
346 347
  bool log1Exist = taosStatFile(name, NULL, &logstat1_mtime) >= 0;

H
hzcheng 已提交
348
  // if none of the log files exist, open 0, if both exists, open the old one
S
TD-1263  
Shengliang Guan 已提交
349 350 351
  if (!log0Exist && !log1Exist) {
    tsLogObj.flag = 0;
  } else if (!log1Exist) {
S
slguan 已提交
352
    tsLogObj.flag = 0;
S
TD-1263  
Shengliang Guan 已提交
353 354
  } else if (!log0Exist) {
    tsLogObj.flag = 1;
H
hzcheng 已提交
355
  } else {
S
Shengliang Guan 已提交
356
    tsLogObj.flag = (logstat0_mtime > logstat1_mtime) ? 0 : 1;
H
hzcheng 已提交
357 358
  }

H
Hui Li 已提交
359 360
  char fileName[LOG_FILE_NAME_LEN + 50] = "\0";
  sprintf(fileName, "%s.%d", tsLogObj.logName, tsLogObj.flag);
wafwerar's avatar
wafwerar 已提交
361
  taosThreadMutexInit(&tsLogObj.logMutex, NULL);
H
hzcheng 已提交
362

S
Shengliang Guan 已提交
363
  taosUmaskFile(0);
364
  tsLogObj.logHandle->pFile = taosOpenFile(fileName, TD_FILE_CREATE | TD_FILE_WRITE);
H
hzcheng 已提交
365

366
  if (tsLogObj.logHandle->pFile == NULL) {
H
Hui Li 已提交
367
    printf("\nfailed to open log file:%s, reason:%s\n", fileName, strerror(errno));
H
hzcheng 已提交
368 369
    return -1;
  }
370
  taosLockLogFile(tsLogObj.logHandle->pFile);
H
hzcheng 已提交
371 372

  // only an estimate for number of lines
S
Shengliang Guan 已提交
373
  int64_t filesize = 0;
374
  if (taosFStatFile(tsLogObj.logHandle->pFile, &filesize, NULL) < 0) {
H
Hui Li 已提交
375
    printf("\nfailed to fstat log file:%s, reason:%s\n", fileName, strerror(errno));
H
Hui Li 已提交
376 377
    return -1;
  }
S
Shengliang Guan 已提交
378
  size = (int32_t)filesize;
S
slguan 已提交
379
  tsLogObj.lines = size / 60;
H
hzcheng 已提交
380

381
  taosLSeekFile(tsLogObj.logHandle->pFile, 0, SEEK_END);
H
hzcheng 已提交
382 383

  sprintf(name, "==================================================\n");
384
  taosWriteFile(tsLogObj.logHandle->pFile, name, (uint32_t)strlen(name));
H
hzcheng 已提交
385
  sprintf(name, "                new log file                      \n");
386
  taosWriteFile(tsLogObj.logHandle->pFile, name, (uint32_t)strlen(name));
H
hzcheng 已提交
387
  sprintf(name, "==================================================\n");
388
  taosWriteFile(tsLogObj.logHandle->pFile, name, (uint32_t)strlen(name));
H
hzcheng 已提交
389 390 391 392

  return 0;
}

393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412
static void taosUpdateLogNums(ELogLevel level) {
  switch (level) {
    case DEBUG_ERROR:
      atomic_add_fetch_64(&tsNumOfErrorLogs, 1);
      break;
    case DEBUG_INFO:
      atomic_add_fetch_64(&tsNumOfInfoLogs, 1);
      break;
    case DEBUG_DEBUG:
      atomic_add_fetch_64(&tsNumOfDebugLogs, 1);
      break;
    case DEBUG_DUMP:
    case DEBUG_TRACE:
      atomic_add_fetch_64(&tsNumOfTraceLogs, 1);
      break;
    default:
      break;
  }
}

S
Shengliang Guan 已提交
413
static inline int32_t taosBuildLogHead(char *buffer, const char *flags) {
H
hzcheng 已提交
414 415 416
  struct tm      Tm, *ptm;
  struct timeval timeSecs;

S
Shengliang Guan 已提交
417
  taosGetTimeOfDay(&timeSecs);
S
Shengliang Guan 已提交
418
  time_t curTime = timeSecs.tv_sec;
wafwerar's avatar
wafwerar 已提交
419
  ptm = taosLocalTime(&curTime, &Tm);
S
slguan 已提交
420

S
Shengliang Guan 已提交
421 422 423
  return sprintf(buffer, "%02d/%02d %02d:%02d:%02d.%06d %08" PRId64 " %s", ptm->tm_mon + 1, ptm->tm_mday, ptm->tm_hour,
                 ptm->tm_min, ptm->tm_sec, (int32_t)timeSecs.tv_usec, taosGetSelfPthreadId(), flags);
}
H
hzcheng 已提交
424

S
Shengliang Guan 已提交
425
static inline void taosPrintLogImp(ELogLevel level, int32_t dflag, const char *buffer, int32_t len) {
426
  if ((dflag & DEBUG_FILE) && tsLogObj.logHandle && tsLogObj.logHandle->pFile != NULL) {
427
    taosUpdateLogNums(level);
H
hzcheng 已提交
428
    if (tsAsyncLog) {
S
slguan 已提交
429
      taosPushLogBuffer(tsLogObj.logHandle, buffer, len);
H
hzcheng 已提交
430
    } else {
431
      taosWriteFile(tsLogObj.logHandle->pFile, buffer, len);
H
hzcheng 已提交
432 433
    }

S
slguan 已提交
434 435
    if (tsLogObj.maxLines > 0) {
      atomic_add_fetch_32(&tsLogObj.lines, 1);
S
Shengliang Guan 已提交
436 437 438
      if ((tsLogObj.lines > tsLogObj.maxLines) && (tsLogObj.openInProgress == 0)) {
        taosOpenNewLogFile();
      }
H
hzcheng 已提交
439 440 441
    }
  }

442 443 444
  if (dflag & DEBUG_SCREEN) {
    write(1, buffer, (uint32_t)len);
  }
H
hzcheng 已提交
445 446
}

S
Shengliang Guan 已提交
447
void taosPrintLog(const char *flags, ELogLevel level, int32_t dflag, const char *format, ...) {
S
os env  
Shengliang Guan 已提交
448
  if (!osLogSpaceAvailable()) return;
S
Shengliang Guan 已提交
449
  if (!(dflag & DEBUG_FILE) && !(dflag & DEBUG_SCREEN)) return;
S
slguan 已提交
450

S
Shengliang Guan 已提交
451 452
  char    buffer[LOG_MAX_LINE_BUFFER_SIZE];
  int32_t len = taosBuildLogHead(buffer, flags);
H
hzcheng 已提交
453

S
Shengliang Guan 已提交
454 455
  va_list argpointer;
  va_start(argpointer, format);
S
Shengliang Guan 已提交
456
  int32_t writeLen = len + vsnprintf(buffer + len, LOG_MAX_LINE_BUFFER_SIZE - len, format, argpointer);
S
Shengliang Guan 已提交
457
  va_end(argpointer);
H
hzcheng 已提交
458

S
Shengliang Guan 已提交
459 460 461
  if (writeLen > LOG_MAX_LINE_SIZE) writeLen = LOG_MAX_LINE_SIZE;
  buffer[writeLen++] = '\n';
  buffer[writeLen] = 0;
H
hzcheng 已提交
462

S
Shengliang Guan 已提交
463 464 465 466 467 468
  taosPrintLogImp(level, dflag, buffer, writeLen);

  if (tsLogFp && level <= DEBUG_INFO) {
    buffer[writeLen - 1] = 0;
    (*tsLogFp)(taosGetTimestampMs(), level, buffer + len);
  }
H
hzcheng 已提交
469 470
}

471
void taosPrintLongString(const char *flags, ELogLevel level, int32_t dflag, const char *format, ...) {
S
os env  
Shengliang Guan 已提交
472
  if (!osLogSpaceAvailable()) return;
S
Shengliang Guan 已提交
473
  if (!(dflag & DEBUG_FILE) && !(dflag & DEBUG_SCREEN)) return;
S
slguan 已提交
474

S
Shengliang Guan 已提交
475 476
  char    buffer[LOG_MAX_LINE_DUMP_BUFFER_SIZE];
  int32_t len = taosBuildLogHead(buffer, flags);
H
hzcheng 已提交
477

S
Shengliang Guan 已提交
478
  va_list argpointer;
H
hzcheng 已提交
479
  va_start(argpointer, format);
S
Shengliang Guan 已提交
480
  len += vsnprintf(buffer + len, LOG_MAX_LINE_DUMP_BUFFER_SIZE - len, format, argpointer);
H
hzcheng 已提交
481 482
  va_end(argpointer);

S
Shengliang Guan 已提交
483
  if (len > LOG_MAX_LINE_DUMP_SIZE) len = LOG_MAX_LINE_DUMP_SIZE;
H
hzcheng 已提交
484 485 486
  buffer[len++] = '\n';
  buffer[len] = 0;

S
Shengliang Guan 已提交
487 488
  taosPrintLogImp(level, dflag, buffer, len);
}
L
Liu Jicong 已提交
489

S
Shengliang Guan 已提交
490 491 492
void taosDumpData(unsigned char *msg, int32_t len) {
  if (!osLogSpaceAvailable()) return;
  taosUpdateLogNums(DEBUG_DUMP);
H
hzcheng 已提交
493

494
  char    temp[256] = {0};
S
Shengliang Guan 已提交
495 496 497 498 499 500 501 502 503 504 505
  int32_t i, pos = 0, c = 0;

  for (i = 0; i < len; ++i) {
    sprintf(temp + pos, "%02x ", msg[i]);
    c++;
    pos += 3;
    if (c >= 16) {
      temp[pos++] = '\n';
      taosWriteFile(tsLogObj.logHandle->pFile, temp, (uint32_t)pos);
      c = 0;
      pos = 0;
H
hzcheng 已提交
506 507 508
    }
  }

S
Shengliang Guan 已提交
509 510 511
  temp[pos++] = '\n';

  taosWriteFile(tsLogObj.logHandle->pFile, temp, (uint32_t)pos);
H
hzcheng 已提交
512 513
}

514 515 516 517
static void taosCloseLogByFd(TdFilePtr pFile) {
  if (pFile != NULL) {
    taosUnLockLogFile(pFile);
    taosCloseFile(&pFile);
H
hzcheng 已提交
518 519 520
  }
}

S
slguan 已提交
521
static SLogBuff *taosLogBuffNew(int32_t bufSize) {
522
  SLogBuff *pLogBuf = NULL;
H
hzcheng 已提交
523

524 525
  pLogBuf = taosMemoryCalloc(1, sizeof(SLogBuff));
  if (pLogBuf == NULL) return NULL;
H
hzcheng 已提交
526

527 528
  LOG_BUF_BUFFER(pLogBuf) = taosMemoryMalloc(bufSize);
  if (LOG_BUF_BUFFER(pLogBuf) == NULL) goto _err;
H
hzcheng 已提交
529

530 531 532 533
  LOG_BUF_START(pLogBuf) = LOG_BUF_END(pLogBuf) = 0;
  LOG_BUF_SIZE(pLogBuf) = bufSize;
  pLogBuf->minBuffSize = bufSize / 10;
  pLogBuf->stop = 0;
H
hzcheng 已提交
534

535 536
  if (taosThreadMutexInit(&LOG_BUF_MUTEX(pLogBuf), NULL) < 0) goto _err;
  // tsem_init(&(pLogBuf->buffNotEmpty), 0, 0);
H
hzcheng 已提交
537

538
  return pLogBuf;
H
hzcheng 已提交
539 540

_err:
541 542
  taosMemoryFreeClear(LOG_BUF_BUFFER(pLogBuf));
  taosMemoryFreeClear(pLogBuf);
H
hzcheng 已提交
543 544 545
  return NULL;
}

546
static void taosCopyLogBuffer(SLogBuff *pLogBuf, int32_t start, int32_t end, const char *msg, int32_t msgLen) {
D
fix bug  
dapan1121 已提交
547
  if (start > end) {
548
    memcpy(LOG_BUF_BUFFER(pLogBuf) + end, msg, msgLen);
D
fix bug  
dapan1121 已提交
549
  } else {
550 551 552
    if (LOG_BUF_SIZE(pLogBuf) - end < msgLen) {
      memcpy(LOG_BUF_BUFFER(pLogBuf) + end, msg, LOG_BUF_SIZE(pLogBuf) - end);
      memcpy(LOG_BUF_BUFFER(pLogBuf), msg + LOG_BUF_SIZE(pLogBuf) - end, msgLen - LOG_BUF_SIZE(pLogBuf) + end);
D
fix bug  
dapan1121 已提交
553
    } else {
554
      memcpy(LOG_BUF_BUFFER(pLogBuf) + end, msg, msgLen);
D
fix bug  
dapan1121 已提交
555 556
    }
  }
557
  LOG_BUF_END(pLogBuf) = (LOG_BUF_END(pLogBuf) + msgLen) % LOG_BUF_SIZE(pLogBuf);
D
fix bug  
dapan1121 已提交
558 559
}

560
static int32_t taosPushLogBuffer(SLogBuff *pLogBuf, const char *msg, int32_t msgLen) {
L
Liu Jicong 已提交
561 562 563
  int32_t        start = 0;
  int32_t        end = 0;
  int32_t        remainSize = 0;
D
fix bug  
dapan1121 已提交
564
  static int64_t lostLine = 0;
L
Liu Jicong 已提交
565 566
  char           tmpBuf[40] = {0};
  int32_t        tmpBufLen = 0;
H
hzcheng 已提交
567

568
  if (pLogBuf == NULL || pLogBuf->stop) return -1;
H
hzcheng 已提交
569

570 571 572
  taosThreadMutexLock(&LOG_BUF_MUTEX(pLogBuf));
  start = LOG_BUF_START(pLogBuf);
  end = LOG_BUF_END(pLogBuf);
H
hzcheng 已提交
573

574
  remainSize = (start > end) ? (start - end - 1) : (start + LOG_BUF_SIZE(pLogBuf) - end - 1);
H
hzcheng 已提交
575

D
fix bug  
dapan1121 已提交
576
  if (lostLine > 0) {
L
Liu Jicong 已提交
577
    sprintf(tmpBuf, "...Lost %" PRId64 " lines here...\n", lostLine);
D
dapan1121 已提交
578
    tmpBufLen = (int32_t)strlen(tmpBuf);
D
fix bug  
dapan1121 已提交
579 580 581 582
  }

  if (remainSize <= msgLen || ((lostLine > 0) && (remainSize <= (msgLen + tmpBufLen)))) {
    lostLine++;
S
Shengliang Guan 已提交
583
    tsAsyncLogLostLines++;
584
    taosThreadMutexUnlock(&LOG_BUF_MUTEX(pLogBuf));
H
hzcheng 已提交
585 586 587
    return -1;
  }

D
fix bug  
dapan1121 已提交
588
  if (lostLine > 0) {
589
    taosCopyLogBuffer(pLogBuf, start, end, tmpBuf, tmpBufLen);
D
fix bug  
dapan1121 已提交
590
    lostLine = 0;
H
hzcheng 已提交
591
  }
D
fix bug  
dapan1121 已提交
592

593
  taosCopyLogBuffer(pLogBuf, LOG_BUF_START(pLogBuf), LOG_BUF_END(pLogBuf), msg, msgLen);
H
hzcheng 已提交
594

L
Liu Jicong 已提交
595
  // int32_t w = atomic_sub_fetch_32(&waitLock, 1);
D
fix bug  
dapan1121 已提交
596
  /*
597 598
  if (w <= 0 || ((remainSize - msgLen - tmpBufLen) < (LOG_BUF_SIZE(pLogBuf) * 4 /5))) {
    tsem_post(&(pLogBuf->buffNotEmpty));
D
fix bug  
dapan1121 已提交
599 600 601
    dbgPostN++;
  } else {
    dbgNoPostN++;
D
fix bug  
dapan1121 已提交
602
  }
D
fix bug  
dapan1121 已提交
603
  */
H
hzcheng 已提交
604

605
  taosThreadMutexUnlock(&LOG_BUF_MUTEX(pLogBuf));
H
hzcheng 已提交
606 607 608 609

  return 0;
}

610
static int32_t taosGetLogRemainSize(SLogBuff *pLogBuf, int32_t start, int32_t end) {
D
fix bug  
dapan1121 已提交
611
  int32_t rSize = end - start;
H
hzcheng 已提交
612

613
  return rSize >= 0 ? rSize : LOG_BUF_SIZE(pLogBuf) + rSize;
D
fix bug  
dapan1121 已提交
614
}
H
hzcheng 已提交
615

616
static void taosWriteLog(SLogBuff *pLogBuf) {
D
fix bu  
dapan1121 已提交
617
  static int32_t lastDuration = 0;
L
Liu Jicong 已提交
618 619 620
  int32_t        remainChecked = 0;
  int32_t        start, end, pollSize;

D
fix bug  
dapan1121 已提交
621
  do {
D
fix bu  
dapan1121 已提交
622
    if (remainChecked == 0) {
623 624
      start = LOG_BUF_START(pLogBuf);
      end = LOG_BUF_END(pLogBuf);
D
fix bu  
dapan1121 已提交
625 626 627

      if (start == end) {
        dbgEmptyW++;
S
Shengliang Guan 已提交
628
        tsWriteInterval = LOG_MAX_INTERVAL;
D
fix bu  
dapan1121 已提交
629 630
        return;
      }
H
hzcheng 已提交
631

632 633
      pollSize = taosGetLogRemainSize(pLogBuf, start, end);
      if (pollSize < pLogBuf->minBuffSize) {
S
Shengliang Guan 已提交
634
        lastDuration += tsWriteInterval;
D
fix bug  
dapan1121 已提交
635
        if (lastDuration < LOG_MAX_WAIT_MSEC) {
D
fix bu  
dapan1121 已提交
636 637 638
          break;
        }
      }
D
fix bug  
dapan1121 已提交
639

D
fix bu  
dapan1121 已提交
640 641 642 643
      lastDuration = 0;
    }

    if (start < end) {
644
      taosWriteFile(pLogBuf->pFile, LOG_BUF_BUFFER(pLogBuf) + start, pollSize);
H
hzcheng 已提交
645
    } else {
646 647
      int32_t tsize = LOG_BUF_SIZE(pLogBuf) - start;
      taosWriteFile(pLogBuf->pFile, LOG_BUF_BUFFER(pLogBuf) + start, tsize);
D
fix bug  
dapan1121 已提交
648

649
      taosWriteFile(pLogBuf->pFile, LOG_BUF_BUFFER(pLogBuf), end);
H
hzcheng 已提交
650
    }
D
fix bug  
dapan1121 已提交
651 652

    dbgWN++;
L
Liu Jicong 已提交
653 654
    dbgWSize += pollSize;

655
    if (pollSize < pLogBuf->minBuffSize) {
D
fix bug  
dapan1121 已提交
656
      dbgSmallWN++;
S
Shengliang Guan 已提交
657 658
      if (tsWriteInterval < LOG_MAX_INTERVAL) {
        tsWriteInterval += LOG_INTERVAL_STEP;
D
fix bug  
dapan1121 已提交
659
      }
660
    } else if (pollSize > LOG_BUF_SIZE(pLogBuf) / 3) {
D
fix bug  
dapan1121 已提交
661
      dbgBigWN++;
S
Shengliang Guan 已提交
662
      tsWriteInterval = LOG_MIN_INTERVAL;
663
    } else if (pollSize > LOG_BUF_SIZE(pLogBuf) / 4) {
S
Shengliang Guan 已提交
664 665
      if (tsWriteInterval > LOG_MIN_INTERVAL) {
        tsWriteInterval -= LOG_INTERVAL_STEP;
D
fix bug  
dapan1121 已提交
666
      }
D
fix bug  
dapan1121 已提交
667 668
    }

669
    LOG_BUF_START(pLogBuf) = (LOG_BUF_START(pLogBuf) + pollSize) % LOG_BUF_SIZE(pLogBuf);
D
fix bug  
dapan1121 已提交
670

671 672
    start = LOG_BUF_START(pLogBuf);
    end = LOG_BUF_END(pLogBuf);
D
fix bu  
dapan1121 已提交
673

674 675
    pollSize = taosGetLogRemainSize(pLogBuf, start, end);
    if (pollSize < pLogBuf->minBuffSize) {
D
fix bug  
dapan1121 已提交
676 677 678
      break;
    }

S
Shengliang Guan 已提交
679
    tsWriteInterval = LOG_MIN_INTERVAL;
D
fix bu  
dapan1121 已提交
680 681

    remainChecked = 1;
L
Liu Jicong 已提交
682
  } while (1);
H
hzcheng 已提交
683 684
}

S
slguan 已提交
685
static void *taosAsyncOutputLog(void *param) {
686
  SLogBuff *pLogBuf = (SLogBuff *)param;
H
Haojun Liao 已提交
687
  setThreadName("log");
L
Liu Jicong 已提交
688

H
hzcheng 已提交
689
  while (1) {
S
Shengliang Guan 已提交
690
    taosMsleep(tsWriteInterval);
H
hzcheng 已提交
691 692

    // Polling the buffer
693
    taosWriteLog(pLogBuf);
H
hzcheng 已提交
694

695
    if (pLogBuf->stop) break;
H
hzcheng 已提交
696 697 698 699
  }

  return NULL;
}
S
Shengliang Guan 已提交
700 701 702 703 704

int32_t taosCompressFile(char *srcFileName, char *destFileName) {
  int32_t compressSize = 163840;
  int32_t ret = 0;
  int32_t len = 0;
dengyihao's avatar
dengyihao 已提交
705
  char *  data = taosMemoryMalloc(compressSize);
S
ulog  
Shengliang Guan 已提交
706
  //  gzFile  dstFp = NULL;
S
Shengliang Guan 已提交
707

708 709 710
  // srcFp = fopen(srcFileName, "r");
  TdFilePtr pSrcFile = taosOpenFile(srcFileName, TD_FILE_READ);
  if (pSrcFile == NULL) {
S
Shengliang Guan 已提交
711 712 713 714
    ret = -1;
    goto cmp_end;
  }

715
  TdFilePtr pFile = taosOpenFile(destFileName, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
716
  if (pFile == NULL) {
S
Shengliang Guan 已提交
717 718 719 720
    ret = -2;
    goto cmp_end;
  }

S
ulog  
Shengliang Guan 已提交
721 722 723 724 725 726 727 728 729 730 731
  //  dstFp = gzdopen(fd, "wb6f");
  //  if (dstFp == NULL) {
  //    ret = -3;
  //    close(fd);
  //    goto cmp_end;
  //  }
  //
  //  while (!feof(srcFp)) {
  //    len = (int32_t)fread(data, 1, compressSize, srcFp);
  //    (void)gzwrite(dstFp, data, len);
  //  }
S
Shengliang Guan 已提交
732 733

cmp_end:
734 735
  if (pSrcFile) {
    taosCloseFile(&pSrcFile);
S
Shengliang Guan 已提交
736
  }
S
ulog  
Shengliang Guan 已提交
737 738 739
  //  if (dstFp) {
  //    gzclose(dstFp);
  //  }
wafwerar's avatar
wafwerar 已提交
740
  taosMemoryFree(data);
S
Shengliang Guan 已提交
741 742 743 744

  return ret;
}

S
Shengliang Guan 已提交
745
void taosSetAllDebugFlag(int32_t flag) {
S
Shengliang Guan 已提交
746
  if (flag <= 0) return;
S
Shengliang Guan 已提交
747

S
Shengliang Guan 已提交
748 749
  uDebugFlag = flag;
  rpcDebugFlag = flag;
S
Shengliang Guan 已提交
750
  jniDebugFlag = flag;
S
Shengliang Guan 已提交
751
  qDebugFlag = flag;
S
Shengliang Guan 已提交
752 753 754 755
  cDebugFlag = flag;
  dDebugFlag = flag;
  vDebugFlag = flag;
  mDebugFlag = flag;
S
Shengliang Guan 已提交
756 757 758 759 760
  wDebugFlag = flag;
  sDebugFlag = flag;
  tsdbDebugFlag = flag;
  tqDebugFlag = flag;
  fsDebugFlag = flag;
S
Shengliang Guan 已提交
761
  udfDebugFlag = flag;
762
  smaDebugFlag = flag;
dengyihao's avatar
dengyihao 已提交
763
  idxDebugFlag = flag;
S
Shengliang Guan 已提交
764
  uInfo("all debug flag are set to %d", flag);
L
temp  
Liu Jicong 已提交
765
}