• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

taosdata / TDengine / #4335

20 Jun 2025 05:45AM UTC coverage: 60.571% (-2.3%) from 62.916%
#4335

push

travis-ci

web-flow
fix: compatibility ci problems. (#31430)

149119 of 315107 branches covered (47.32%)

Branch coverage included in aggregate %.

231167 of 312731 relevant lines covered (73.92%)

6342953.77 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

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

16
#include <ttimer.h>
17
#include "cJSON.h"
18
#include "catalog.h"
19
#include "clientInt.h"
20
#include "clientLog.h"
21
#include "clientMonitor.h"
22
#include "functionMgt.h"
23
#include "os.h"
24
#include "osSleep.h"
25
#include "query.h"
26
#include "qworker.h"
27
#include "scheduler.h"
28
#include "tcache.h"
29
#include "tcompare.h"
30
#include "tconv.h"
31
#include "tglobal.h"
32
#include "thttp.h"
33
#include "tmsg.h"
34
#include "tqueue.h"
35
#include "tref.h"
36
#include "trpc.h"
37
#include "tsched.h"
38
#include "ttime.h"
39
#include "tversion.h"
40

41
#include "cus_name.h"
42

43
#define TSC_VAR_NOT_RELEASE 1
44
#define TSC_VAR_RELEASED    0
45

46
#define ENV_JSON_FALSE_CHECK(c)                     \
47
  do {                                              \
48
    if (!c) {                                       \
49
      tscError("faild to add item to JSON object"); \
50
      code = TSDB_CODE_TSC_FAIL_GENERATE_JSON;      \
51
      goto _end;                                    \
52
    }                                               \
53
  } while (0)
54

55
#define ENV_ERR_RET(c, info)          \
56
  do {                                \
57
    int32_t _code = (c);              \
58
    if (_code != TSDB_CODE_SUCCESS) { \
59
      terrno = _code;                 \
60
      tscInitRes = _code;             \
61
      tscError(info);                 \
62
      return;                         \
63
    }                                 \
64
  } while (0)
65

66
STscDbg   tscDbg = {0};
67
SAppInfo  appInfo;
68
int64_t   lastClusterId = 0;
69
int32_t   clientReqRefPool = -1;
70
int32_t   clientConnRefPool = -1;
71
int32_t   clientStop = -1;
72
SHashObj *pTimezoneMap = NULL;
73

74
int32_t timestampDeltaLimit = 900;  // s
75

76
static TdThreadOnce tscinit = PTHREAD_ONCE_INIT;
77
volatile int32_t    tscInitRes = 0;
78

79
static int32_t registerRequest(SRequestObj *pRequest, STscObj *pTscObj) {
3,007,468✔
80
  int32_t code = TSDB_CODE_SUCCESS;
3,007,468✔
81
  // connection has been released already, abort creating request.
82
  pRequest->self = taosAddRef(clientReqRefPool, pRequest);
3,007,468✔
83
  if (pRequest->self < 0) {
3,007,520!
84
    tscError("failed to add ref to request");
×
85
    code = terrno;
×
86
    return code;
×
87
  }
88

89
  int32_t num = atomic_add_fetch_32(&pTscObj->numOfReqs, 1);
3,007,520✔
90

91
  if (pTscObj->pAppInfo) {
3,007,516!
92
    SAppClusterSummary *pSummary = &pTscObj->pAppInfo->summary;
3,007,517✔
93

94
    int32_t total = atomic_add_fetch_64((int64_t *)&pSummary->totalRequests, 1);
3,007,517✔
95
    int32_t currentInst = atomic_add_fetch_64((int64_t *)&pSummary->currentRequests, 1);
3,007,522✔
96
    tscDebug("req:0x%" PRIx64 ", create request from conn:0x%" PRIx64
3,007,517✔
97
             ", current:%d, app current:%d, total:%d, QID:0x%" PRIx64,
98
             pRequest->self, pRequest->pTscObj->id, num, currentInst, total, pRequest->requestId);
99
  }
100

101
  return code;
3,007,523✔
102
}
103

104
static void concatStrings(SArray *list, char *buf, int size) {
47✔
105
  int len = 0;
47✔
106
  for (int i = 0; i < taosArrayGetSize(list); i++) {
115✔
107
    char *db = taosArrayGet(list, i);
68✔
108
    if (NULL == db) {
68!
109
      tscError("get dbname failed, buf:%s", buf);
×
110
      break;
×
111
    }
112
    char *dot = strchr(db, '.');
68✔
113
    if (dot != NULL) {
68!
114
      db = dot + 1;
68✔
115
    }
116
    if (i != 0) {
68✔
117
      (void)strncat(buf, ",", size - 1 - len);
21✔
118
      len += 1;
21✔
119
    }
120
    int ret = tsnprintf(buf + len, size - len, "%s", db);
68✔
121
    if (ret < 0) {
68!
122
      tscError("snprintf failed, buf:%s, ret:%d", buf, ret);
×
123
      break;
×
124
    }
125
    len += ret;
68✔
126
    if (len >= size) {
68!
127
      tscInfo("dbList is truncated, buf:%s, len:%d", buf, len);
×
128
      break;
×
129
    }
130
  }
131
}
47✔
132
#ifdef USE_REPORT
133
static int32_t generateWriteSlowLog(STscObj *pTscObj, SRequestObj *pRequest, int32_t reqType, int64_t duration) {
47✔
134
  cJSON  *json = cJSON_CreateObject();
47✔
135
  int32_t code = TSDB_CODE_SUCCESS;
47✔
136
  if (json == NULL) {
47!
137
    tscError("failed to create monitor json");
×
138
    return TSDB_CODE_OUT_OF_MEMORY;
×
139
  }
140
  char clusterId[32] = {0};
47✔
141
  if (snprintf(clusterId, sizeof(clusterId), "%" PRId64, pTscObj->pAppInfo->clusterId) < 0) {
47!
142
    tscError("failed to generate clusterId:%" PRId64, pTscObj->pAppInfo->clusterId);
×
143
    code = TSDB_CODE_FAILED;
×
144
    goto _end;
×
145
  }
146

147
  char startTs[32] = {0};
47✔
148
  if (snprintf(startTs, sizeof(startTs), "%" PRId64, pRequest->metric.start / 1000) < 0) {
47!
149
    tscError("failed to generate startTs:%" PRId64, pRequest->metric.start / 1000);
×
150
    code = TSDB_CODE_FAILED;
×
151
    goto _end;
×
152
  }
153

154
  char requestId[32] = {0};
47✔
155
  if (snprintf(requestId, sizeof(requestId), "%" PRIu64, pRequest->requestId) < 0) {
47!
156
    tscError("failed to generate requestId:%" PRIu64, pRequest->requestId);
×
157
    code = TSDB_CODE_FAILED;
×
158
    goto _end;
×
159
  }
160
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "cluster_id", cJSON_CreateString(clusterId)));
47!
161
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "start_ts", cJSON_CreateString(startTs)));
47!
162
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "request_id", cJSON_CreateString(requestId)));
47!
163
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "query_time", cJSON_CreateNumber(duration / 1000)));
47!
164
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "code", cJSON_CreateNumber(pRequest->code)));
47!
165
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "error_info", cJSON_CreateString(tstrerror(pRequest->code))));
47!
166
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "type", cJSON_CreateNumber(reqType)));
47!
167
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(
47!
168
      json, "rows_num", cJSON_CreateNumber(pRequest->body.resInfo.numOfRows + pRequest->body.resInfo.totalRows)));
169
  if (pRequest->sqlstr != NULL &&
47!
170
      strlen(pRequest->sqlstr) > pTscObj->pAppInfo->serverCfg.monitorParas.tsSlowLogMaxLen) {
47!
171
    char tmp = pRequest->sqlstr[pTscObj->pAppInfo->serverCfg.monitorParas.tsSlowLogMaxLen];
×
172
    pRequest->sqlstr[pTscObj->pAppInfo->serverCfg.monitorParas.tsSlowLogMaxLen] = '\0';
×
173
    ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "sql", cJSON_CreateString(pRequest->sqlstr)));
×
174
    pRequest->sqlstr[pTscObj->pAppInfo->serverCfg.monitorParas.tsSlowLogMaxLen] = tmp;
×
175
  } else {
176
    ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "sql", cJSON_CreateString(pRequest->sqlstr)));
47!
177
  }
178

179
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "user", cJSON_CreateString(pTscObj->user)));
47!
180
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "process_name", cJSON_CreateString(appInfo.appName)));
47!
181
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "ip", cJSON_CreateString(tsLocalFqdn)));
47!
182

183
  char pid[32] = {0};
47✔
184
  if (snprintf(pid, sizeof(pid), "%d", appInfo.pid) < 0) {
47!
185
    tscError("failed to generate pid:%d", appInfo.pid);
×
186
    code = TSDB_CODE_FAILED;
×
187
    goto _end;
×
188
  }
189

190
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "process_id", cJSON_CreateString(pid)));
47!
191
  if (pRequest->dbList != NULL) {
47!
192
    char dbList[1024] = {0};
47✔
193
    concatStrings(pRequest->dbList, dbList, sizeof(dbList) - 1);
47✔
194
    ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "db", cJSON_CreateString(dbList)));
47!
195
  } else if (pRequest->pDb != NULL) {
×
196
    ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "db", cJSON_CreateString(pRequest->pDb)));
×
197
  } else {
198
    ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "db", cJSON_CreateString("")));
×
199
  }
200

201
  char *value = cJSON_PrintUnformatted(json);
47✔
202
  if (value == NULL) {
47!
203
    tscError("failed to print json");
×
204
    code = TSDB_CODE_FAILED;
×
205
    goto _end;
×
206
  }
207
  MonitorSlowLogData data = {0};
47✔
208
  data.clusterId = pTscObj->pAppInfo->clusterId;
47✔
209
  data.type = SLOW_LOG_WRITE;
47✔
210
  data.data = value;
47✔
211
  code = monitorPutData2MonitorQueue(data);
47✔
212
  if (TSDB_CODE_SUCCESS != code) {
47!
213
    taosMemoryFree(value);
×
214
    goto _end;
×
215
  }
216

217
_end:
47✔
218
  cJSON_Delete(json);
47✔
219
  return code;
47✔
220
}
221
#endif
222
static bool checkSlowLogExceptDb(SRequestObj *pRequest, char *exceptDb) {
1,006✔
223
  if (pRequest->pDb != NULL) {
1,006✔
224
    return strcmp(pRequest->pDb, exceptDb) != 0;
674✔
225
  }
226

227
  for (int i = 0; i < taosArrayGetSize(pRequest->dbList); i++) {
446✔
228
    char *db = taosArrayGet(pRequest->dbList, i);
114✔
229
    if (NULL == db) {
114!
230
      tscError("get dbname failed, exceptDb:%s", exceptDb);
×
231
      return false;
×
232
    }
233
    char *dot = strchr(db, '.');
114✔
234
    if (dot != NULL) {
114!
235
      db = dot + 1;
114✔
236
    }
237
    if (strcmp(db, exceptDb) == 0) {
114!
238
      return false;
×
239
    }
240
  }
241
  return true;
332✔
242
}
243

244
static void deregisterRequest(SRequestObj *pRequest) {
3,006,882✔
245
  if (pRequest == NULL) {
3,006,882!
246
    tscError("pRequest == NULL");
×
247
    return;
×
248
  }
249

250
  STscObj            *pTscObj = pRequest->pTscObj;
3,006,882✔
251
  SAppClusterSummary *pActivity = &pTscObj->pAppInfo->summary;
3,006,882✔
252

253
  int32_t currentInst = atomic_sub_fetch_64((int64_t *)&pActivity->currentRequests, 1);
3,006,882✔
254
  int32_t num = atomic_sub_fetch_32(&pTscObj->numOfReqs, 1);
3,006,929✔
255
  int32_t reqType = SLOW_LOG_TYPE_OTHERS;
3,006,921✔
256

257
  int64_t duration = taosGetTimestampUs() - pRequest->metric.start;
3,006,866✔
258
  tscDebug("req:0x%" PRIx64 ", free from conn:0x%" PRIx64 ", QID:0x%" PRIx64
3,006,866✔
259
           ", elapsed:%.2f ms, current:%d, app current:%d",
260
           pRequest->self, pTscObj->id, pRequest->requestId, duration / 1000.0, num, currentInst);
261

262
  if (TSDB_CODE_SUCCESS == nodesSimAcquireAllocator(pRequest->allocatorRefId)) {
3,006,866!
263
    if ((pRequest->pQuery && pRequest->pQuery->pRoot && QUERY_NODE_VNODE_MODIFY_STMT == pRequest->pQuery->pRoot->type &&
3,006,889!
264
         (0 == ((SVnodeModifyOpStmt *)pRequest->pQuery->pRoot)->sqlNodeType)) ||
1,867,585✔
265
        QUERY_NODE_VNODE_MODIFY_STMT == pRequest->stmtType) {
1,234,696✔
266
      tscDebug("req:0x%" PRIx64 ", insert duration:%" PRId64 "us, parseCost:%" PRId64 "us, ctgCost:%" PRId64
1,907,893✔
267
               "us, analyseCost:%" PRId64 "us, planCost:%" PRId64 "us, exec:%" PRId64 "us, QID:0x%" PRIx64,
268
               pRequest->self, duration, pRequest->metric.parseCostUs, pRequest->metric.ctgCostUs,
269
               pRequest->metric.analyseCostUs, pRequest->metric.planCostUs, pRequest->metric.execCostUs,
270
               pRequest->requestId);
271
      (void)atomic_add_fetch_64((int64_t *)&pActivity->insertElapsedTime, duration);
1,907,893✔
272
      reqType = SLOW_LOG_TYPE_INSERT;
1,907,936✔
273
    } else if (QUERY_NODE_SELECT_STMT == pRequest->stmtType) {
1,098,996✔
274
      tscDebug("req:0x%" PRIx64 ", query duration:%" PRId64 "us, parseCost:%" PRId64 "us, ctgCost:%" PRId64
795,544✔
275
               "us, analyseCost:%" PRId64 "us, planCost:%" PRId64 "us, exec:%" PRId64 "us, QID:0x%" PRIx64,
276
               pRequest->self, duration, pRequest->metric.parseCostUs, pRequest->metric.ctgCostUs,
277
               pRequest->metric.analyseCostUs, pRequest->metric.planCostUs, pRequest->metric.execCostUs,
278
               pRequest->requestId);
279

280
      (void)atomic_add_fetch_64((int64_t *)&pActivity->queryElapsedTime, duration);
795,544✔
281
      reqType = SLOW_LOG_TYPE_QUERY;
795,543✔
282
    }
283

284
    if (TSDB_CODE_SUCCESS != nodesSimReleaseAllocator(pRequest->allocatorRefId)) {
3,006,931!
285
      tscError("failed to release allocator");
×
286
    }
287
  }
288

289
#ifdef USE_REPORT
290
  if (pTscObj->pAppInfo->serverCfg.monitorParas.tsEnableMonitor) {
3,006,895✔
291
    if (QUERY_NODE_VNODE_MODIFY_STMT == pRequest->stmtType || QUERY_NODE_INSERT_STMT == pRequest->stmtType) {
502,084✔
292
      sqlReqLog(pTscObj->id, pRequest->killed, pRequest->code, MONITORSQLTYPEINSERT);
464,600✔
293
    } else if (QUERY_NODE_SELECT_STMT == pRequest->stmtType) {
37,484✔
294
      sqlReqLog(pTscObj->id, pRequest->killed, pRequest->code, MONITORSQLTYPESELECT);
23,497✔
295
    } else if (QUERY_NODE_DELETE_STMT == pRequest->stmtType) {
13,987✔
296
      sqlReqLog(pTscObj->id, pRequest->killed, pRequest->code, MONITORSQLTYPEDELETE);
129✔
297
    }
298
  }
299

300
  if ((duration >= pTscObj->pAppInfo->serverCfg.monitorParas.tsSlowLogThreshold * 1000000UL) &&
3,007,902!
301
      checkSlowLogExceptDb(pRequest, pTscObj->pAppInfo->serverCfg.monitorParas.tsSlowLogExceptDb)) {
1,006✔
302
    (void)atomic_add_fetch_64((int64_t *)&pActivity->numOfSlowQueries, 1);
1,006✔
303
    if (pTscObj->pAppInfo->serverCfg.monitorParas.tsSlowLogScope & reqType) {
1,006✔
304
      taosPrintSlowLog("PID:%d, connId:%u, QID:0x%" PRIx64 ", Start:%" PRId64 "us, Duration:%" PRId64 "us, SQL:%s",
321✔
305
                       taosGetPId(), pTscObj->connId, pRequest->requestId, pRequest->metric.start, duration,
306
                       pRequest->sqlstr);
307
      if (pTscObj->pAppInfo->serverCfg.monitorParas.tsEnableMonitor) {
321✔
308
        slowQueryLog(pTscObj->id, pRequest->killed, pRequest->code, duration);
47✔
309
        if (TSDB_CODE_SUCCESS != generateWriteSlowLog(pTscObj, pRequest, reqType, duration)) {
47!
310
          tscError("failed to generate write slow log");
×
311
        }
312
      }
313
    }
314
  }
315
#endif
316

317
  releaseTscObj(pTscObj->id);
3,006,896✔
318
}
319

320
// todo close the transporter properly
321
void closeTransporter(SAppInstInfo *pAppInfo) {
16,781✔
322
  if (pAppInfo == NULL || pAppInfo->pTransporter == NULL) {
16,781!
323
    return;
×
324
  }
325

326
  tscDebug("free transporter:%p in app inst %p", pAppInfo->pTransporter, pAppInfo);
16,781✔
327
  rpcClose(pAppInfo->pTransporter);
16,781✔
328
}
329

330
static bool clientRpcRfp(int32_t code, tmsg_t msgType) {
121,140✔
331
  if (NEED_REDIRECT_ERROR(code)) {
121,140!
332
    if (msgType == TDMT_SCH_QUERY || msgType == TDMT_SCH_MERGE_QUERY || msgType == TDMT_SCH_FETCH ||
107,836!
333
        msgType == TDMT_SCH_MERGE_FETCH || msgType == TDMT_SCH_QUERY_HEARTBEAT || msgType == TDMT_SCH_DROP_TASK ||
107,839!
334
        msgType == TDMT_SCH_TASK_NOTIFY) {
335
      return false;
×
336
    }
337
    return true;
107,839✔
338
  } else {
339
    return false;
13,304✔
340
  }
341
}
342

343
// start timer for particular msgType
344
static bool clientRpcTfp(int32_t code, tmsg_t msgType) {
×
345
  if (msgType == TDMT_VND_SUBMIT || msgType == TDMT_VND_CREATE_TABLE) {
×
346
    return true;
×
347
  }
348
  return false;
×
349
}
350

351
// TODO refactor
352
int32_t openTransporter(const char *user, const char *auth, int32_t numOfThread, void **pDnodeConn) {
16,781✔
353
  SRpcInit rpcInit;
354
  (void)memset(&rpcInit, 0, sizeof(rpcInit));
16,781✔
355
  rpcInit.localPort = 0;
16,781✔
356
  rpcInit.label = "TSC";
16,781✔
357
  rpcInit.numOfThreads = tsNumOfRpcThreads;
16,781✔
358
  rpcInit.cfp = processMsgFromServer;
16,781✔
359
  rpcInit.rfp = clientRpcRfp;
16,781✔
360
  rpcInit.sessions = 1024;
16,781✔
361
  rpcInit.connType = TAOS_CONN_CLIENT;
16,781✔
362
  rpcInit.user = (char *)user;
16,781✔
363
  rpcInit.idleTime = tsShellActivityTimer * 1000;
16,781✔
364
  rpcInit.compressSize = tsCompressMsgSize;
16,781✔
365
  rpcInit.dfp = destroyAhandle;
16,781✔
366

367
  rpcInit.retryMinInterval = tsRedirectPeriod;
16,781✔
368
  rpcInit.retryStepFactor = tsRedirectFactor;
16,781✔
369
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
16,781✔
370
  rpcInit.retryMaxTimeout = tsMaxRetryWaitTime;
16,781✔
371

372
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
16,781✔
373
  connLimitNum = TMAX(connLimitNum, 10);
16,781✔
374
  connLimitNum = TMIN(connLimitNum, 1000);
16,781✔
375
  rpcInit.connLimitNum = connLimitNum;
16,781✔
376
  rpcInit.shareConnLimit = tsShareConnLimit;
16,781✔
377
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
16,781✔
378
  rpcInit.startReadTimer = 1;
16,781✔
379
  rpcInit.readTimeout = tsReadTimeout;
16,781✔
380
  rpcInit.ipv6 = tsEnableIpv6;
16,781✔
381

382
  int32_t code = taosVersionStrToInt(td_version, &rpcInit.compatibilityVer);
16,781✔
383
  if (TSDB_CODE_SUCCESS != code) {
16,781!
384
    tscError("invalid version string.");
×
385
    return code;
×
386
  }
387

388
  *pDnodeConn = rpcOpen(&rpcInit);
16,781✔
389
  if (*pDnodeConn == NULL) {
16,781!
390
    tscError("failed to init connection to server since %s", tstrerror(terrno));
×
391
    code = terrno;
×
392
  }
393

394
  return code;
16,781✔
395
}
396

397
void destroyAllRequests(SHashObj *pRequests) {
21,406✔
398
  void *pIter = taosHashIterate(pRequests, NULL);
21,406✔
399
  while (pIter != NULL) {
21,406!
400
    int64_t *rid = pIter;
×
401

402
    SRequestObj *pRequest = acquireRequest(*rid);
×
403
    if (pRequest) {
×
404
      destroyRequest(pRequest);
×
405
      (void)releaseRequest(*rid);  // ignore error
×
406
    }
407

408
    pIter = taosHashIterate(pRequests, pIter);
×
409
  }
410
}
21,406✔
411

412
void stopAllRequests(SHashObj *pRequests) {
5✔
413
  void *pIter = taosHashIterate(pRequests, NULL);
5✔
414
  while (pIter != NULL) {
5!
415
    int64_t *rid = pIter;
×
416

417
    SRequestObj *pRequest = acquireRequest(*rid);
×
418
    if (pRequest) {
×
419
      taos_stop_query(pRequest);
×
420
      (void)releaseRequest(*rid);  // ignore error
×
421
    }
422

423
    pIter = taosHashIterate(pRequests, pIter);
×
424
  }
425
}
5✔
426

427
void destroyAppInst(void *info) {
16,781✔
428
  SAppInstInfo *pAppInfo = *(SAppInstInfo **)info;
16,781✔
429
  tscInfo("destroy app inst mgr %p", pAppInfo);
16,781!
430

431
  int32_t code = taosThreadMutexLock(&appInfo.mutex);
16,781✔
432
  if (TSDB_CODE_SUCCESS != code) {
16,781!
433
    tscError("failed to lock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
434
  }
435

436
  hbRemoveAppHbMrg(&pAppInfo->pAppHbMgr);
16,781✔
437

438
  code = taosThreadMutexUnlock(&appInfo.mutex);
16,781✔
439
  if (TSDB_CODE_SUCCESS != code) {
16,781!
440
    tscError("failed to unlock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
441
  }
442

443
  taosMemoryFreeClear(pAppInfo->instKey);
16,781!
444
  closeTransporter(pAppInfo);
16,781✔
445

446
  code = taosThreadMutexLock(&pAppInfo->qnodeMutex);
16,781✔
447
  if (TSDB_CODE_SUCCESS != code) {
16,781!
448
    tscError("failed to lock qnode mutex, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
449
  }
450

451
  taosArrayDestroy(pAppInfo->pQnodeList);
16,781✔
452
  code = taosThreadMutexUnlock(&pAppInfo->qnodeMutex);
16,781✔
453
  if (TSDB_CODE_SUCCESS != code) {
16,781!
454
    tscError("failed to unlock qnode mutex, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
455
  }
456

457
  taosMemoryFree(pAppInfo);
16,781!
458
}
16,781✔
459

460
void destroyTscObj(void *pObj) {
21,406✔
461
  if (NULL == pObj) {
21,406!
462
    return;
×
463
  }
464

465
  STscObj *pTscObj = pObj;
21,406✔
466
  int64_t  tscId = pTscObj->id;
21,406✔
467
  tscTrace("conn:%" PRIx64 ", begin destroy, p:%p", tscId, pTscObj);
21,406✔
468

469
  SClientHbKey connKey = {.tscRid = pTscObj->id, .connType = pTscObj->connType};
21,406✔
470
  hbDeregisterConn(pTscObj, connKey);
21,406✔
471

472
  destroyAllRequests(pTscObj->pRequests);
21,406✔
473
  taosHashCleanup(pTscObj->pRequests);
21,406✔
474

475
  schedulerStopQueryHb(pTscObj->pAppInfo->pTransporter);
21,406✔
476
  tscDebug("conn:0x%" PRIx64 ", p:%p destroyed, remain inst totalConn:%" PRId64, pTscObj->id, pTscObj,
21,406✔
477
           pTscObj->pAppInfo->numOfConns);
478

479
  // In any cases, we should not free app inst here. Or an race condition rises.
480
  /*int64_t connNum = */ (void)atomic_sub_fetch_64(&pTscObj->pAppInfo->numOfConns, 1);
21,406✔
481

482
  (void)taosThreadMutexDestroy(&pTscObj->mutex);
21,406✔
483
  taosMemoryFree(pTscObj);
21,406!
484

485
  tscTrace("conn:0x%" PRIx64 ", end destroy, p:%p", tscId, pTscObj);
21,406✔
486
}
487

488
int32_t createTscObj(const char *user, const char *auth, const char *db, int32_t connType, SAppInstInfo *pAppInfo,
22,032✔
489
                     STscObj **pObj) {
490
  *pObj = (STscObj *)taosMemoryCalloc(1, sizeof(STscObj));
22,032!
491
  if (NULL == *pObj) {
22,032!
492
    return terrno;
×
493
  }
494

495
  (*pObj)->pRequests = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
22,032✔
496
  if (NULL == (*pObj)->pRequests) {
22,030!
497
    taosMemoryFree(*pObj);
×
498
    return terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
499
  }
500

501
  (*pObj)->connType = connType;
22,030✔
502
  (*pObj)->pAppInfo = pAppInfo;
22,030✔
503
  (*pObj)->appHbMgrIdx = pAppInfo->pAppHbMgr->idx;
22,030✔
504
  tstrncpy((*pObj)->user, user, sizeof((*pObj)->user));
22,030✔
505
  (void)memcpy((*pObj)->pass, auth, TSDB_PASSWORD_LEN);
22,030✔
506

507
  if (db != NULL) {
22,030!
508
    tstrncpy((*pObj)->db, db, tListLen((*pObj)->db));
22,031✔
509
  }
510

511
  TSC_ERR_RET(taosThreadMutexInit(&(*pObj)->mutex, NULL));
22,030!
512

513
  int32_t code = TSDB_CODE_SUCCESS;
22,032✔
514

515
  (*pObj)->id = taosAddRef(clientConnRefPool, *pObj);
22,032✔
516
  if ((*pObj)->id < 0) {
22,031!
517
    tscError("failed to add object to clientConnRefPool");
×
518
    code = terrno;
×
519
    taosMemoryFree(*pObj);
×
520
    return code;
×
521
  }
522

523
  (void)atomic_add_fetch_64(&(*pObj)->pAppInfo->numOfConns, 1);
22,031✔
524

525
  tscInfo("conn:0x%" PRIx64 ", created, p:%p", (*pObj)->id, *pObj);
22,032!
526
  return code;
22,032✔
527
}
528

529
STscObj *acquireTscObj(int64_t rid) { return (STscObj *)taosAcquireRef(clientConnRefPool, rid); }
3,778,985✔
530

531
void releaseTscObj(int64_t rid) {
3,773,789✔
532
  int32_t code = taosReleaseRef(clientConnRefPool, rid);
3,773,789✔
533
  if (TSDB_CODE_SUCCESS != code) {
3,773,874!
534
    tscWarn("failed to release TscObj, code:%s", tstrerror(code));
×
535
  }
536
}
3,773,874✔
537

538
int32_t createRequest(uint64_t connId, int32_t type, int64_t reqid, SRequestObj **pRequest) {
3,007,441✔
539
  int32_t code = TSDB_CODE_SUCCESS;
3,007,441✔
540
  *pRequest = (SRequestObj *)taosMemoryCalloc(1, sizeof(SRequestObj));
3,007,441!
541
  if (NULL == *pRequest) {
3,007,501!
542
    return terrno;
×
543
  }
544

545
  STscObj *pTscObj = acquireTscObj(connId);
3,007,501✔
546
  if (pTscObj == NULL) {
3,007,525!
547
    TSC_ERR_JRET(TSDB_CODE_TSC_DISCONNECTED);
×
548
  }
549
  SSyncQueryParam *interParam = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
3,007,525!
550
  if (interParam == NULL) {
3,007,516!
551
    releaseTscObj(connId);
×
552
    TSC_ERR_JRET(terrno);
×
553
  }
554
  TSC_ERR_JRET(tsem_init(&interParam->sem, 0, 0));
3,007,516!
555
  interParam->pRequest = *pRequest;
3,007,505✔
556
  (*pRequest)->body.interParam = interParam;
3,007,505✔
557

558
  (*pRequest)->resType = RES_TYPE__QUERY;
3,007,505✔
559
  (*pRequest)->requestId = reqid == 0 ? generateRequestId() : reqid;
3,007,505!
560
  (*pRequest)->metric.start = taosGetTimestampUs();
3,007,498✔
561

562
  (*pRequest)->body.resInfo.convertUcs4 = true;  // convert ucs4 by default
3,007,518✔
563
  (*pRequest)->body.resInfo.charsetCxt = pTscObj->optionInfo.charsetCxt;
3,007,518✔
564
  (*pRequest)->type = type;
3,007,518✔
565
  (*pRequest)->allocatorRefId = -1;
3,007,518✔
566

567
  (*pRequest)->pDb = getDbOfConnection(pTscObj);
3,007,518✔
568
  if (NULL == (*pRequest)->pDb) {
3,007,508✔
569
    TSC_ERR_JRET(terrno);
441,475!
570
  }
571
  (*pRequest)->pTscObj = pTscObj;
3,007,510✔
572
  (*pRequest)->inCallback = false;
3,007,510✔
573
  (*pRequest)->msgBuf = taosMemoryCalloc(1, ERROR_MSG_BUF_DEFAULT_SIZE);
3,007,510!
574
  if (NULL == (*pRequest)->msgBuf) {
3,007,497!
575
    code = terrno;
×
576
    goto _return;
×
577
  }
578
  (*pRequest)->msgBufLen = ERROR_MSG_BUF_DEFAULT_SIZE;
3,007,497✔
579
  TSC_ERR_JRET(tsem_init(&(*pRequest)->body.rspSem, 0, 0));
3,007,497!
580
  TSC_ERR_JRET(registerRequest(*pRequest, pTscObj));
3,007,509!
581

582
  return TSDB_CODE_SUCCESS;
3,007,515✔
583
_return:
×
584
  if ((*pRequest)->pTscObj) {
×
585
    doDestroyRequest(*pRequest);
×
586
  } else {
587
    taosMemoryFree(*pRequest);
×
588
  }
589
  return code;
×
590
}
591

592
void doFreeReqResultInfo(SReqResultInfo *pResInfo) {
3,384,644✔
593
  taosMemoryFreeClear(pResInfo->pRspMsg);
3,384,644!
594
  taosMemoryFreeClear(pResInfo->length);
3,384,644!
595
  taosMemoryFreeClear(pResInfo->row);
3,384,647!
596
  taosMemoryFreeClear(pResInfo->pCol);
3,384,650!
597
  taosMemoryFreeClear(pResInfo->fields);
3,384,655!
598
  taosMemoryFreeClear(pResInfo->userFields);
3,384,651!
599
  taosMemoryFreeClear(pResInfo->convertJson);
3,384,650!
600
  taosMemoryFreeClear(pResInfo->decompBuf);
3,384,650!
601

602
  if (pResInfo->convertBuf != NULL) {
3,384,650✔
603
    for (int32_t i = 0; i < pResInfo->numOfCols; ++i) {
5,453,817✔
604
      taosMemoryFreeClear(pResInfo->convertBuf[i]);
4,335,948!
605
    }
606
    taosMemoryFreeClear(pResInfo->convertBuf);
1,117,869!
607
  }
608
}
3,384,655✔
609

610
SRequestObj *acquireRequest(int64_t rid) { return (SRequestObj *)taosAcquireRef(clientReqRefPool, rid); }
16,405,272✔
611

612
int32_t releaseRequest(int64_t rid) { return taosReleaseRef(clientReqRefPool, rid); }
16,430,611✔
613

614
int32_t removeRequest(int64_t rid) { return taosRemoveRef(clientReqRefPool, rid); }
3,006,445✔
615

616
/// return the most previous req ref id
617
int64_t removeFromMostPrevReq(SRequestObj *pRequest) {
3,006,456✔
618
  int64_t      mostPrevReqRefId = pRequest->self;
3,006,456✔
619
  SRequestObj *pTmp = pRequest;
3,006,456✔
620
  while (pTmp->relation.prevRefId) {
3,006,884✔
621
    pTmp = acquireRequest(pTmp->relation.prevRefId);
428✔
622
    if (pTmp) {
428!
623
      mostPrevReqRefId = pTmp->self;
428✔
624
      (void)releaseRequest(mostPrevReqRefId);  // ignore error
428✔
625
    } else {
626
      break;
×
627
    }
628
  }
629
  (void)removeRequest(mostPrevReqRefId);  // ignore error
3,006,456✔
630
  return mostPrevReqRefId;
3,006,500✔
631
}
632

633
void destroyNextReq(int64_t nextRefId) {
3,006,876✔
634
  if (nextRefId) {
3,006,876✔
635
    SRequestObj *pObj = acquireRequest(nextRefId);
450✔
636
    if (pObj) {
450!
637
      (void)releaseRequest(nextRefId);  // ignore error
450✔
638
      (void)releaseRequest(nextRefId);  // ignore error
450✔
639
    }
640
  }
641
}
3,006,876✔
642

643
void destroySubRequests(SRequestObj *pRequest) {
×
644
  int32_t      reqIdx = -1;
×
645
  SRequestObj *pReqList[16] = {NULL};
×
646
  uint64_t     tmpRefId = 0;
×
647

648
  if (pRequest->relation.userRefId && pRequest->relation.userRefId != pRequest->self) {
×
649
    return;
×
650
  }
651

652
  SRequestObj *pTmp = pRequest;
×
653
  while (pTmp->relation.prevRefId) {
×
654
    tmpRefId = pTmp->relation.prevRefId;
×
655
    pTmp = acquireRequest(tmpRefId);
×
656
    if (pTmp) {
×
657
      pReqList[++reqIdx] = pTmp;
×
658
      (void)releaseRequest(tmpRefId);  // ignore error
×
659
    } else {
660
      tscError("prev req ref 0x%" PRIx64 " is not there", tmpRefId);
×
661
      break;
×
662
    }
663
  }
664

665
  for (int32_t i = reqIdx; i >= 0; i--) {
×
666
    (void)removeRequest(pReqList[i]->self);  // ignore error
×
667
  }
668

669
  tmpRefId = pRequest->relation.nextRefId;
×
670
  while (tmpRefId) {
×
671
    pTmp = acquireRequest(tmpRefId);
×
672
    if (pTmp) {
×
673
      tmpRefId = pTmp->relation.nextRefId;
×
674
      (void)removeRequest(pTmp->self);   // ignore error
×
675
      (void)releaseRequest(pTmp->self);  // ignore error
×
676
    } else {
677
      tscError("next req ref 0x%" PRIx64 " is not there", tmpRefId);
×
678
      break;
×
679
    }
680
  }
681
}
682

683
void doDestroyRequest(void *p) {
3,006,917✔
684
  if (NULL == p) {
3,006,917!
685
    return;
×
686
  }
687

688
  SRequestObj *pRequest = (SRequestObj *)p;
3,006,917✔
689

690
  uint64_t reqId = pRequest->requestId;
3,006,917✔
691
  tscTrace("QID:0x%" PRIx64 ", begin destroy request, res:%p", reqId, pRequest);
3,006,917✔
692

693
  int64_t nextReqRefId = pRequest->relation.nextRefId;
3,006,917✔
694

695
  int32_t code = taosHashRemove(pRequest->pTscObj->pRequests, &pRequest->self, sizeof(pRequest->self));
3,006,917✔
696
  if (TSDB_CODE_SUCCESS != code) {
3,006,918✔
697
    tscDebug("failed to remove request from hash since %s", tstrerror(code));
22,032✔
698
  }
699
  schedulerFreeJob(&pRequest->body.queryJob, 0);
3,006,918✔
700

701
  destorySqlCallbackWrapper(pRequest->pWrapper);
3,006,917✔
702

703
  taosMemoryFreeClear(pRequest->msgBuf);
3,006,881!
704

705
  doFreeReqResultInfo(&pRequest->body.resInfo);
3,006,899✔
706
  if (TSDB_CODE_SUCCESS != tsem_destroy(&pRequest->body.rspSem)) {
3,006,850!
707
    tscError("failed to destroy semaphore");
×
708
  }
709

710
  taosArrayDestroy(pRequest->tableList);
3,006,905✔
711
  taosArrayDestroy(pRequest->targetTableList);
3,006,872✔
712
  destroyQueryExecRes(&pRequest->body.resInfo.execRes);
3,006,908✔
713

714
  if (pRequest->self) {
3,006,920!
715
    deregisterRequest(pRequest);
3,006,922✔
716
  }
717

718
  taosMemoryFreeClear(pRequest->pDb);
3,006,905!
719
  taosArrayDestroy(pRequest->dbList);
3,006,906✔
720
  if (pRequest->body.interParam) {
3,006,916!
721
    if (TSDB_CODE_SUCCESS != tsem_destroy(&((SSyncQueryParam *)pRequest->body.interParam)->sem)) {
3,006,923!
722
      tscError("failed to destroy semaphore in pRequest");
×
723
    }
724
  }
725
  taosMemoryFree(pRequest->body.interParam);
3,006,914!
726

727
  qDestroyQuery(pRequest->pQuery);
3,006,912✔
728
  nodesDestroyAllocator(pRequest->allocatorRefId);
3,006,907✔
729

730
  taosMemoryFreeClear(pRequest->effectiveUser);
3,006,887!
731
  taosMemoryFreeClear(pRequest->sqlstr);
3,006,887!
732
  taosMemoryFree(pRequest);
3,006,914!
733
  tscTrace("QID:0x%" PRIx64 ", end destroy request, res:%p", reqId, pRequest);
3,006,921✔
734
  destroyNextReq(nextReqRefId);
3,006,921✔
735
}
736

737
void destroyRequest(SRequestObj *pRequest) {
3,006,439✔
738
  if (pRequest == NULL) return;
3,006,439!
739

740
  taos_stop_query(pRequest);
3,006,439✔
741
  (void)removeFromMostPrevReq(pRequest);
3,006,462✔
742
}
743

744
void taosStopQueryImpl(SRequestObj *pRequest) {
3,006,869✔
745
  pRequest->killed = true;
3,006,869✔
746

747
  // It is not a query, no need to stop.
748
  if (NULL == pRequest->pQuery || QUERY_EXEC_MODE_SCHEDULE != pRequest->pQuery->execMode) {
3,006,869✔
749
    tscDebug("QID:0x%" PRIx64 ", no need to be killed since not query", pRequest->requestId);
358,319✔
750
    return;
358,332✔
751
  }
752

753
  schedulerFreeJob(&pRequest->body.queryJob, TSDB_CODE_TSC_QUERY_KILLED);
2,648,550✔
754
  tscDebug("QID:0x%" PRIx64 ", killed", pRequest->requestId);
2,648,551✔
755
}
756

757
void stopAllQueries(SRequestObj *pRequest) {
3,006,439✔
758
  int32_t      reqIdx = -1;
3,006,439✔
759
  SRequestObj *pReqList[16] = {NULL};
3,006,439✔
760
  uint64_t     tmpRefId = 0;
3,006,439✔
761

762
  if (pRequest->relation.userRefId && pRequest->relation.userRefId != pRequest->self) {
3,006,439!
763
    return;
×
764
  }
765

766
  SRequestObj *pTmp = pRequest;
3,006,439✔
767
  while (pTmp->relation.prevRefId) {
3,006,867✔
768
    tmpRefId = pTmp->relation.prevRefId;
428✔
769
    pTmp = acquireRequest(tmpRefId);
428✔
770
    if (pTmp) {
428!
771
      pReqList[++reqIdx] = pTmp;
428✔
772
    } else {
773
      tscError("prev req ref 0x%" PRIx64 " is not there", tmpRefId);
×
774
      break;
×
775
    }
776
  }
777

778
  for (int32_t i = reqIdx; i >= 0; i--) {
3,006,891✔
779
    taosStopQueryImpl(pReqList[i]);
428✔
780
    (void)releaseRequest(pReqList[i]->self);  // ignore error
428✔
781
  }
782

783
  taosStopQueryImpl(pRequest);
3,006,463✔
784

785
  tmpRefId = pRequest->relation.nextRefId;
3,006,458✔
786
  while (tmpRefId) {
3,006,456!
787
    pTmp = acquireRequest(tmpRefId);
×
788
    if (pTmp) {
×
789
      tmpRefId = pTmp->relation.nextRefId;
×
790
      taosStopQueryImpl(pTmp);
×
791
      (void)releaseRequest(pTmp->self);  // ignore error
×
792
    } else {
793
      tscError("next req ref 0x%" PRIx64 " is not there", tmpRefId);
×
794
      break;
×
795
    }
796
  }
797
}
798
#ifdef USE_REPORT
799
void crashReportThreadFuncUnexpectedStopped(void) { atomic_store_32(&clientStop, -1); }
×
800

801
static void *tscCrashReportThreadFp(void *param) {
×
802
  int32_t code = 0;
×
803
  setThreadName("client-crashReport");
×
804
  char filepath[PATH_MAX] = {0};
×
805
  (void)snprintf(filepath, sizeof(filepath), "%s%s.taosCrashLog", tsLogDir, TD_DIRSEP);
×
806
  char     *pMsg = NULL;
×
807
  int64_t   msgLen = 0;
×
808
  TdFilePtr pFile = NULL;
×
809
  bool      truncateFile = false;
×
810
  int32_t   sleepTime = 200;
×
811
  int32_t   reportPeriodNum = 3600 * 1000 / sleepTime;
×
812
  int32_t   loopTimes = reportPeriodNum;
×
813

814
#ifdef WINDOWS
815
  if (taosCheckCurrentInDll()) {
816
    atexit(crashReportThreadFuncUnexpectedStopped);
817
  }
818
#endif
819

820
  if (-1 != atomic_val_compare_exchange_32(&clientStop, -1, 0)) {
×
821
    return NULL;
×
822
  }
823
  STelemAddrMgmt mgt;
824
  code = taosTelemetryMgtInit(&mgt, tsTelemServer);
×
825
  if (code) {
×
826
    tscError("failed to init telemetry management, code:%s", tstrerror(code));
×
827
    return NULL;
×
828
  }
829

830
  code = initCrashLogWriter();
×
831
  if (code) {
×
832
    tscError("failed to init crash log writer, code:%s", tstrerror(code));
×
833
    return NULL;
×
834
  }
835

836
  while (1) {
837
    checkAndPrepareCrashInfo();
×
838
    if (clientStop > 0 && reportThreadSetQuit()) break;
×
839
    if (loopTimes++ < reportPeriodNum) {
×
840
      if (loopTimes < 0) loopTimes = reportPeriodNum;
×
841
      taosMsleep(sleepTime);
×
842
      continue;
×
843
    }
844

845
    taosReadCrashInfo(filepath, &pMsg, &msgLen, &pFile);
×
846
    if (pMsg && msgLen > 0) {
×
847
      if (taosSendTelemReport(&mgt, tsClientCrashReportUri, tsTelemPort, pMsg, msgLen, HTTP_FLAT) != 0) {
×
848
        tscError("failed to send crash report");
×
849
        if (pFile) {
×
850
          taosReleaseCrashLogFile(pFile, false);
×
851
          pFile = NULL;
×
852

853
          taosMsleep(sleepTime);
×
854
          loopTimes = 0;
×
855
          continue;
×
856
        }
857
      } else {
858
        tscInfo("succeed to send crash report");
×
859
        truncateFile = true;
×
860
      }
861
    } else {
862
      tscInfo("no crash info was found");
×
863
    }
864

865
    taosMemoryFree(pMsg);
×
866

867
    if (pMsg && msgLen > 0) {
×
868
      pMsg = NULL;
×
869
      continue;
×
870
    }
871

872
    if (pFile) {
×
873
      taosReleaseCrashLogFile(pFile, truncateFile);
×
874
      pFile = NULL;
×
875
      truncateFile = false;
×
876
    }
877

878
    taosMsleep(sleepTime);
×
879
    loopTimes = 0;
×
880
  }
881
  taosTelemetryDestroy(&mgt);
×
882

883
  clientStop = -2;
×
884
  return NULL;
×
885
}
886

887
int32_t tscCrashReportInit() {
16,532✔
888
  if (!tsEnableCrashReport) {
16,532!
889
    return TSDB_CODE_SUCCESS;
16,532✔
890
  }
891
  int32_t      code = TSDB_CODE_SUCCESS;
×
892
  TdThreadAttr thAttr;
893
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
×
894
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
×
895
  TdThread crashReportThread;
896
  if (taosThreadCreate(&crashReportThread, &thAttr, tscCrashReportThreadFp, NULL) != 0) {
×
897
    tscError("failed to create crashReport thread since %s", strerror(ERRNO));
×
898
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
899
    TSC_ERR_RET(terrno);
×
900
  }
901

902
  (void)taosThreadAttrDestroy(&thAttr);
×
903
_return:
×
904
  if (code) {
×
905
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
906
    TSC_ERR_RET(terrno);
×
907
  }
908

909
  return code;
×
910
}
911

912
void tscStopCrashReport() {
16,532✔
913
  if (!tsEnableCrashReport) {
16,532!
914
    return;
16,532✔
915
  }
916

917
  if (atomic_val_compare_exchange_32(&clientStop, 0, 1)) {
×
918
    tscDebug("crash report thread already stopped");
×
919
    return;
×
920
  }
921

922
  while (atomic_load_32(&clientStop) > 0) {
×
923
    taosMsleep(100);
×
924
  }
925
}
926

927
void taos_write_crashinfo(int signum, void *sigInfo, void *context) {
×
928
  writeCrashLogToFile(signum, sigInfo, CUS_PROMPT, lastClusterId, appInfo.startTime);
×
929
}
×
930
#endif
931

932
#ifdef TAOSD_INTEGRATED
933
typedef struct {
934
  TdThread pid;
935
  int32_t  stat;  // < 0: start failed, 0: init(not start), 1: start successfully
936
} SDaemonObj;
937

938
extern int  dmStartDaemon(int argc, char const *argv[]);
939
extern void dmStopDaemon();
940

941
SDaemonObj daemonObj = {0};
942

943
typedef struct {
944
  int32_t argc;
945
  char  **argv;
946
} SExecArgs;
947

948
static void *dmStartDaemonFunc(void *param) {
949
  int32_t    code = 0;
950
  SExecArgs *pArgs = (SExecArgs *)param;
951
  int32_t    argc = pArgs->argc;
952
  char     **argv = pArgs->argv;
953

954
  code = dmStartDaemon(argc, (const char **)argv);
955
  if (code != 0) {
956
    printf("failed to start taosd since %s\r\n", tstrerror(code));
957
    goto _exit;
958
  }
959

960
_exit:
961
  if (code != 0) {
962
    atomic_store_32(&daemonObj.stat, code);
963
  }
964
  return NULL;
965
}
966

967
static int32_t shellStartDaemon(int argc, char *argv[]) {
968
  int32_t    code = 0, lino = 0;
969
  SExecArgs *pArgs = NULL;
970
  int64_t    startMs = taosGetTimestampMs(), endMs = startMs;
971

972
  TdThreadAttr thAttr;
973
  (void)taosThreadAttrInit(&thAttr);
974
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
975
#ifdef TD_COMPACT_OS
976
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
977
#endif
978
  pArgs = (SExecArgs *)taosMemoryCalloc(1, sizeof(SExecArgs));
979
  if (pArgs == NULL) {
980
    code = terrno;
981
    TAOS_CHECK_EXIT(code);
982
  }
983
  pArgs->argc = argc;
984
  pArgs->argv = argv;
985

986
#ifndef TD_AS_LIB
987
  tsLogEmbedded = 1;
988
#endif
989

990
  TAOS_CHECK_EXIT(taosThreadCreate(&daemonObj.pid, &thAttr, dmStartDaemonFunc, pArgs));
991

992
  while (true) {
993
    if (atomic_load_64(&tsDndStart)) {
994
      atomic_store_32(&daemonObj.stat, 1);
995
      break;
996
    }
997
    int32_t daemonstat = atomic_load_32(&daemonObj.stat);
998
    if (daemonstat < 0) {
999
      code = daemonstat;
1000
      TAOS_CHECK_EXIT(code);
1001
    }
1002

1003
    if (daemonstat > 1) {
1004
      code = TSDB_CODE_APP_ERROR;
1005
      TAOS_CHECK_EXIT(code);
1006
    }
1007
    taosMsleep(1000);
1008
  }
1009

1010
_exit:
1011
  endMs = taosGetTimestampMs();
1012
  (void)taosThreadAttrDestroy(&thAttr);
1013
  taosMemoryFreeClear(pArgs);
1014
  if (code) {
1015
    printf("\r\n The daemon start failed at line %d since %s, cost %" PRIi64 " ms\r\n", lino, tstrerror(code),
1016
           endMs - startMs);
1017
  } else {
1018
    printf("\r\n The daemon started successfully, cost %" PRIi64 " ms\r\n", endMs - startMs);
1019
  }
1020
#ifndef TD_AS_LIB
1021
  tsLogEmbedded = 0;
1022
#endif
1023
  return code;
1024
}
1025

1026
void shellStopDaemon() {
1027
#ifndef TD_AS_LIB
1028
  tsLogEmbedded = 1;
1029
#endif
1030
  dmStopDaemon();
1031
  if (taosCheckPthreadValid(daemonObj.pid)) {
1032
    (void)taosThreadJoin(daemonObj.pid, NULL);
1033
    taosThreadClear(&daemonObj.pid);
1034
  }
1035
}
1036
#endif
1037

1038
void taos_init_imp(void) {
16,532✔
1039
#if defined(LINUX)
1040
  if (tscDbg.memEnable) {
16,532!
1041
    int32_t code = taosMemoryDbgInit();
×
1042
    if (code) {
×
1043
      (void)printf("failed to init memory dbg, error:%s\n", tstrerror(code));
×
1044
    } else {
1045
      tsAsyncLog = false;
×
1046
      (void)printf("memory dbg enabled\n");
×
1047
    }
1048
  }
1049
#endif
1050

1051
  // In the APIs of other program language, taos_cleanup is not available yet.
1052
  // So, to make sure taos_cleanup will be invoked to clean up the allocated resource to suppress the valgrind warning.
1053
  (void)atexit(taos_cleanup);
16,532✔
1054
  SET_ERRNO(TSDB_CODE_SUCCESS);
16,532✔
1055
  terrno = TSDB_CODE_SUCCESS;
16,532✔
1056
  taosSeedRand(taosGetTimestampSec());
16,532✔
1057

1058
  appInfo.pid = taosGetPId();
16,532✔
1059
  appInfo.startTime = taosGetTimestampMs();
16,532✔
1060
  appInfo.pInstMap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
16,532✔
1061
  appInfo.pInstMapByClusterId =
16,532✔
1062
      taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
16,532✔
1063
  if (NULL == appInfo.pInstMap || NULL == appInfo.pInstMapByClusterId) {
16,532!
1064
    (void)printf("failed to allocate memory when init appInfo\n");
×
1065
    tscInitRes = terrno;
×
1066
    return;
×
1067
  }
1068
  taosHashSetFreeFp(appInfo.pInstMap, destroyAppInst);
16,532✔
1069

1070
  const char *logName = CUS_PROMPT "log";
16,532✔
1071
  ENV_ERR_RET(taosInitLogOutput(&logName), "failed to init log output");
16,532!
1072
  if (taosCreateLog(logName, 10, configDir, NULL, NULL, NULL, NULL, 1) != 0) {
16,532!
1073
    (void)printf(" WARING: Create %s failed:%s. configDir=%s\n", logName, strerror(ERRNO), configDir);
×
1074
    tscInitRes = terrno;
×
1075
    return;
×
1076
  }
1077

1078
#ifdef TAOSD_INTEGRATED
1079
  ENV_ERR_RET(taosInitCfg(configDir, NULL, NULL, NULL, NULL, 0), "failed to init cfg");
1080
#else
1081
  ENV_ERR_RET(taosInitCfg(configDir, NULL, NULL, NULL, NULL, 1), "failed to init cfg");
16,532!
1082
#endif
1083

1084
  initQueryModuleMsgHandle();
16,532✔
1085
#ifndef DISALLOW_NCHAR_WITHOUT_ICONV
1086
  if ((tsCharsetCxt = taosConvInit(tsCharset)) == NULL) {
16,532!
1087
    tscInitRes = terrno;
×
1088
    tscError("failed to init conv");
×
1089
    return;
×
1090
  }
1091
#endif
1092
#if !defined(WINDOWS) && !defined(TD_ASTRA)
1093
  ENV_ERR_RET(tzInit(), "failed to init timezone");
16,532!
1094
#endif
1095
  ENV_ERR_RET(monitorInit(), "failed to init monitor");
16,532!
1096
  ENV_ERR_RET(rpcInit(), "failed to init rpc");
16,532!
1097

1098
  if (InitRegexCache() != 0) {
16,532!
1099
    tscInitRes = terrno;
×
1100
    (void)printf("failed to init regex cache\n");
×
1101
    return;
×
1102
  }
1103

1104
  tscInfo("starting to initialize TAOS driver");
16,532!
1105

1106
  SCatalogCfg cfg = {.maxDBCacheNum = 100, .maxTblCacheNum = 100};
16,532✔
1107
  ENV_ERR_RET(catalogInit(&cfg), "failed to init catalog");
16,532!
1108
  ENV_ERR_RET(schedulerInit(), "failed to init scheduler");
16,532!
1109
  ENV_ERR_RET(initClientId(), "failed to init clientId");
16,532!
1110

1111
  ENV_ERR_RET(initTaskQueue(), "failed to init task queue");
16,532!
1112
  ENV_ERR_RET(fmFuncMgtInit(), "failed to init funcMgt");
16,532!
1113
  ENV_ERR_RET(nodesInitAllocatorSet(), "failed to init allocator set");
16,532!
1114

1115
  clientConnRefPool = taosOpenRef(200, destroyTscObj);
16,532✔
1116
  clientReqRefPool = taosOpenRef(40960, doDestroyRequest);
16,532✔
1117

1118
  ENV_ERR_RET(taosGetAppName(appInfo.appName, NULL), "failed to get app name");
16,532!
1119
  ENV_ERR_RET(taosThreadMutexInit(&appInfo.mutex, NULL), "failed to init thread mutex");
16,532!
1120
#ifdef USE_REPORT
1121
  ENV_ERR_RET(tscCrashReportInit(), "failed to init crash report");
16,532!
1122
#endif
1123
  ENV_ERR_RET(qInitKeywordsTable(), "failed to init parser keywords table");
16,532!
1124
#ifdef TAOSD_INTEGRATED
1125
  ENV_ERR_RET(shellStartDaemon(0, NULL), "failed to start taosd daemon");
1126
#endif
1127
  tscInfo("TAOS driver is initialized successfully");
16,532!
1128
}
1129

1130
int taos_init() {
21,175✔
1131
  (void)taosThreadOnce(&tscinit, taos_init_imp);
21,175✔
1132
  return tscInitRes;
21,176✔
1133
}
1134

1135
const char *getCfgName(TSDB_OPTION option) {
546✔
1136
  const char *name = NULL;
546✔
1137

1138
  switch (option) {
546!
1139
    case TSDB_OPTION_SHELL_ACTIVITY_TIMER:
×
1140
      name = "shellActivityTimer";
×
1141
      break;
×
1142
    case TSDB_OPTION_LOCALE:
×
1143
      name = "locale";
×
1144
      break;
×
1145
    case TSDB_OPTION_CHARSET:
×
1146
      name = "charset";
×
1147
      break;
×
1148
    case TSDB_OPTION_TIMEZONE:
24✔
1149
      name = "timezone";
24✔
1150
      break;
24✔
1151
    case TSDB_OPTION_USE_ADAPTER:
×
1152
      name = "useAdapter";
×
1153
      break;
×
1154
    default:
522✔
1155
      break;
522✔
1156
  }
1157

1158
  return name;
546✔
1159
}
1160

1161
int taos_options_imp(TSDB_OPTION option, const char *str) {
2,696✔
1162
  if (option == TSDB_OPTION_CONFIGDIR) {
2,696✔
1163
#ifndef WINDOWS
1164
    char newstr[PATH_MAX];
1165
    int  len = strlen(str);
2,150✔
1166
    if (len > 1 && str[0] != '"' && str[0] != '\'') {
2,150!
1167
      if (len + 2 >= PATH_MAX) {
2,145!
1168
        tscError("Too long path %s", str);
×
1169
        return -1;
×
1170
      }
1171
      newstr[0] = '"';
2,145✔
1172
      (void)memcpy(newstr + 1, str, len);
2,145✔
1173
      newstr[len + 1] = '"';
2,145✔
1174
      newstr[len + 2] = '\0';
2,145✔
1175
      str = newstr;
2,145✔
1176
    }
1177
#endif
1178
    tstrncpy(configDir, str, PATH_MAX);
2,150✔
1179
    tscInfo("set cfg:%s to %s", configDir, str);
2,150!
1180
    return 0;
2,150✔
1181
  }
1182

1183
  // initialize global config
1184
  if (taos_init() != 0) {
546!
1185
    return -1;
×
1186
  }
1187

1188
  SConfig     *pCfg = taosGetCfg();
546✔
1189
  SConfigItem *pItem = NULL;
546✔
1190
  const char  *name = getCfgName(option);
546✔
1191

1192
  if (name == NULL) {
546✔
1193
    tscError("Invalid option %d", option);
522!
1194
    return -1;
522✔
1195
  }
1196

1197
  pItem = cfgGetItem(pCfg, name);
24✔
1198
  if (pItem == NULL) {
24!
1199
    tscError("Invalid option %d", option);
×
1200
    return -1;
×
1201
  }
1202

1203
  int code = cfgSetItem(pCfg, name, str, CFG_STYPE_TAOS_OPTIONS, true);
24✔
1204
  if (code != 0) {
24!
1205
    tscError("failed to set cfg:%s to %s since %s", name, str, terrstr());
×
1206
  } else {
1207
    tscInfo("set cfg:%s to %s", name, str);
24!
1208
    if (TSDB_OPTION_SHELL_ACTIVITY_TIMER == option || TSDB_OPTION_USE_ADAPTER == option) {
24!
1209
      code = taosCfgDynamicOptions(pCfg, name, false);
×
1210
    }
1211
  }
1212

1213
  return code;
24✔
1214
}
1215

1216
/**
1217
 * The request id is an unsigned integer format of 64bit.
1218
 *+------------+-----+-----------+---------------+
1219
 *| uid|localIp| PId | timestamp | serial number |
1220
 *+------------+-----+-----------+---------------+
1221
 *| 12bit      |12bit|24bit      |16bit          |
1222
 *+------------+-----+-----------+---------------+
1223
 * @return
1224
 */
1225
uint64_t generateRequestId() {
3,167,927✔
1226
  static uint32_t hashId = 0;
1227
  static int32_t  requestSerialId = 0;
1228

1229
  if (hashId == 0) {
3,167,927✔
1230
    int32_t code = taosGetSystemUUIDU32(&hashId);
16,525✔
1231
    if (code != TSDB_CODE_SUCCESS) {
16,525!
1232
      tscError("Failed to get the system uid to generated request id, reason:%s. use ip address instead",
×
1233
               tstrerror(code));
1234
    }
1235
  }
1236

1237
  uint64_t id = 0;
3,167,935✔
1238

1239
  while (true) {
×
1240
    int64_t  ts = taosGetTimestampMs();
3,167,938✔
1241
    uint64_t pid = taosGetPId();
3,167,938✔
1242
    uint32_t val = atomic_add_fetch_32(&requestSerialId, 1);
3,167,944✔
1243
    if (val >= 0xFFFF) atomic_store_32(&requestSerialId, 0);
3,167,976✔
1244

1245
    id = (((uint64_t)(hashId & 0x0FFF)) << 52) | ((pid & 0x0FFF) << 40) | ((ts & 0xFFFFFF) << 16) | (val & 0xFFFF);
3,167,975✔
1246
    if (id) {
3,167,975!
1247
      break;
3,167,975✔
1248
    }
1249
  }
1250
  return id;
3,167,975✔
1251
}
1252

1253
#if 0
1254
#include "cJSON.h"
1255
static setConfRet taos_set_config_imp(const char *config){
1256
  setConfRet ret = {SET_CONF_RET_SUCC, {0}};
1257
  static bool setConfFlag = false;
1258
  if (setConfFlag) {
1259
    ret.retCode = SET_CONF_RET_ERR_ONLY_ONCE;
1260
    tstrncpy(ret.retMsg, "configuration can only set once", RET_MSG_LENGTH);
1261
    return ret;
1262
  }
1263
  taosInitGlobalCfg();
1264
  cJSON *root = cJSON_Parse(config);
1265
  if (root == NULL){
1266
    ret.retCode = SET_CONF_RET_ERR_JSON_PARSE;
1267
    tstrncpy(ret.retMsg, "parse json error", RET_MSG_LENGTH);
1268
    return ret;
1269
  }
1270

1271
  int size = cJSON_GetArraySize(root);
1272
  if(!cJSON_IsObject(root) || size == 0) {
1273
    ret.retCode = SET_CONF_RET_ERR_JSON_INVALID;
1274
    tstrncpy(ret.retMsg, "json content is invalid, must be not empty object", RET_MSG_LENGTH);
1275
    return ret;
1276
  }
1277

1278
  if(size >= 1000) {
1279
    ret.retCode = SET_CONF_RET_ERR_TOO_LONG;
1280
    tstrncpy(ret.retMsg, "json object size is too long", RET_MSG_LENGTH);
1281
    return ret;
1282
  }
1283

1284
  for(int i = 0; i < size; i++){
1285
    cJSON *item = cJSON_GetArrayItem(root, i);
1286
    if(!item) {
1287
      ret.retCode = SET_CONF_RET_ERR_INNER;
1288
      tstrncpy(ret.retMsg, "inner error", RET_MSG_LENGTH);
1289
      return ret;
1290
    }
1291
    if(!taosReadConfigOption(item->string, item->valuestring, NULL, NULL, TAOS_CFG_CSTATUS_OPTION, TSDB_CFG_CTYPE_B_CLIENT)){
1292
      ret.retCode = SET_CONF_RET_ERR_PART;
1293
      if (strlen(ret.retMsg) == 0){
1294
        snprintf(ret.retMsg, RET_MSG_LENGTH, "part error|%s", item->string);
1295
      }else{
1296
        int tmp = RET_MSG_LENGTH - 1 - (int)strlen(ret.retMsg);
1297
        size_t leftSize = tmp >= 0 ? tmp : 0;
1298
        strncat(ret.retMsg, "|",  leftSize);
1299
        tmp = RET_MSG_LENGTH - 1 - (int)strlen(ret.retMsg);
1300
        leftSize = tmp >= 0 ? tmp : 0;
1301
        strncat(ret.retMsg, item->string, leftSize);
1302
      }
1303
    }
1304
  }
1305
  cJSON_Delete(root);
1306
  setConfFlag = true;
1307
  return ret;
1308
}
1309

1310
setConfRet taos_set_config(const char *config){
1311
  taosThreadMutexLock(&setConfMutex);
1312
  setConfRet ret = taos_set_config_imp(config);
1313
  taosThreadMutexUnlock(&setConfMutex);
1314
  return ret;
1315
}
1316
#endif
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc