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

taosdata / TDengine / #4886

16 Dec 2025 01:13AM UTC coverage: 65.292% (+0.03%) from 65.258%
#4886

push

travis-ci

web-flow
fix: compile error (#33938)

178718 of 273721 relevant lines covered (65.29%)

103311111.65 hits per line

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

54.84
/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
#include "clientSession.h"
43

44
#define TSC_VAR_NOT_RELEASE 1
45
#define TSC_VAR_RELEASED    0
46

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

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

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

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

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

88
  int32_t num = atomic_add_fetch_32(&pTscObj->numOfReqs, 1);
635,260,199✔
89

90
  if (pTscObj->pAppInfo) {
635,267,238✔
91
    SAppClusterSummary *pSummary = &pTscObj->pAppInfo->summary;
635,260,323✔
92

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

100
  return code;
635,261,695✔
101
}
102

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

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

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

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

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

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

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

216
_end:
×
217
  cJSON_Delete(json);
×
218
  return code;
×
219
}
220
#endif
221
static bool checkSlowLogExceptDb(SRequestObj *pRequest, char *exceptDb) {
328,391✔
222
  if (pRequest->pDb != NULL) {
328,391✔
223
    return strcmp(pRequest->pDb, exceptDb) != 0;
197,159✔
224
  }
225

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

243
static void deregisterRequest(SRequestObj *pRequest) {
635,142,067✔
244
  if (pRequest == NULL) {
635,142,067✔
245
    tscError("pRequest == NULL");
×
246
    return;
×
247
  }
248

249
  STscObj            *pTscObj = pRequest->pTscObj;
635,142,067✔
250
  SAppClusterSummary *pActivity = &pTscObj->pAppInfo->summary;
635,141,849✔
251

252
  int32_t currentInst = atomic_sub_fetch_64((int64_t *)&pActivity->currentRequests, 1);
635,142,383✔
253
  int32_t num = atomic_sub_fetch_32(&pTscObj->numOfReqs, 1);
635,142,642✔
254
  int32_t reqType = SLOW_LOG_TYPE_OTHERS;
635,142,642✔
255

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

261
  if (TSDB_CODE_SUCCESS == nodesSimAcquireAllocator(pRequest->allocatorRefId)) {
635,142,354✔
262
    if ((pRequest->pQuery && pRequest->pQuery->pRoot && QUERY_NODE_VNODE_MODIFY_STMT == pRequest->pQuery->pRoot->type &&
635,140,728✔
263
         (0 == ((SVnodeModifyOpStmt *)pRequest->pQuery->pRoot)->sqlNodeType)) ||
511,083,218✔
264
        QUERY_NODE_VNODE_MODIFY_STMT == pRequest->stmtType) {
190,266,623✔
265
      tscDebug("req:0x%" PRIx64 ", insert duration:%" PRId64 "us, parseCost:%" PRId64 "us, ctgCost:%" PRId64
511,722,438✔
266
               "us, analyseCost:%" PRId64 "us, planCost:%" PRId64 "us, exec:%" PRId64 "us, QID:0x%" PRIx64,
267
               pRequest->self, duration, pRequest->metric.parseCostUs, pRequest->metric.ctgCostUs,
268
               pRequest->metric.analyseCostUs, pRequest->metric.planCostUs, pRequest->metric.execCostUs,
269
               pRequest->requestId);
270
      (void)atomic_add_fetch_64((int64_t *)&pActivity->insertElapsedTime, duration);
511,722,438✔
271
      reqType = SLOW_LOG_TYPE_INSERT;
511,725,302✔
272
    } else if (QUERY_NODE_SELECT_STMT == pRequest->stmtType) {
123,417,340✔
273
      tscDebug("req:0x%" PRIx64 ", query duration:%" PRId64 "us, parseCost:%" PRId64 "us, ctgCost:%" PRId64
85,222,562✔
274
               "us, analyseCost:%" PRId64 "us, planCost:%" PRId64 "us, exec:%" PRId64 "us, QID:0x%" PRIx64,
275
               pRequest->self, duration, pRequest->metric.parseCostUs, pRequest->metric.ctgCostUs,
276
               pRequest->metric.analyseCostUs, pRequest->metric.planCostUs, pRequest->metric.execCostUs,
277
               pRequest->requestId);
278

279
      (void)atomic_add_fetch_64((int64_t *)&pActivity->queryElapsedTime, duration);
85,222,562✔
280
      reqType = SLOW_LOG_TYPE_QUERY;
85,222,562✔
281
    }
282

283
    if (TSDB_CODE_SUCCESS != nodesSimReleaseAllocator(pRequest->allocatorRefId)) {
635,142,642✔
284
      tscError("failed to release allocator");
×
285
    }
286
  }
287

288
#ifdef USE_REPORT
289
  if (pTscObj->pAppInfo->serverCfg.monitorParas.tsEnableMonitor) {
635,143,559✔
290
    if (QUERY_NODE_VNODE_MODIFY_STMT == pRequest->stmtType || QUERY_NODE_INSERT_STMT == pRequest->stmtType) {
381,245✔
291
      sqlReqLog(pTscObj->id, pRequest->killed, pRequest->code, MONITORSQLTYPEINSERT);
359,482✔
292
    } else if (QUERY_NODE_SELECT_STMT == pRequest->stmtType) {
21,763✔
293
      sqlReqLog(pTscObj->id, pRequest->killed, pRequest->code, MONITORSQLTYPESELECT);
6,488✔
294
    } else if (QUERY_NODE_DELETE_STMT == pRequest->stmtType) {
15,275✔
295
      sqlReqLog(pTscObj->id, pRequest->killed, pRequest->code, MONITORSQLTYPEDELETE);
×
296
    }
297
  }
298

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

316
  releaseTscObj(pTscObj->id);
635,140,940✔
317
}
318

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

325
  tscDebug("free transporter:%p in app inst %p", pAppInfo->pTransporter, pAppInfo);
1,244,205✔
326
  rpcClose(pAppInfo->pTransporter);
1,244,205✔
327
}
328

329
static bool clientRpcRfp(int32_t code, tmsg_t msgType) {
14,670,275✔
330
  if (NEED_REDIRECT_ERROR(code)) {
14,670,275✔
331
    if (msgType == TDMT_SCH_QUERY || msgType == TDMT_SCH_MERGE_QUERY || msgType == TDMT_SCH_FETCH ||
11,452,723✔
332
        msgType == TDMT_SCH_MERGE_FETCH || msgType == TDMT_SCH_QUERY_HEARTBEAT || msgType == TDMT_SCH_DROP_TASK ||
11,452,723✔
333
        msgType == TDMT_SCH_TASK_NOTIFY) {
334
      return false;
×
335
    }
336
    return true;
11,452,723✔
337
  } else if (code == TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY || code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE ||
3,217,552✔
338
             code == TSDB_CODE_SYN_WRITE_STALL || code == TSDB_CODE_SYN_PROPOSE_NOT_READY ||
3,217,584✔
339
             code == TSDB_CODE_SYN_RESTORING) {
340
    tscDebug("client msg type %s should retry since %s", TMSG_INFO(msgType), tstrerror(code));
×
341
    return true;
×
342
  } else {
343
    return false;
3,217,552✔
344
  }
345
}
346

347
// start timer for particular msgType
348
static bool clientRpcTfp(int32_t code, tmsg_t msgType) {
×
349
  if (msgType == TDMT_VND_SUBMIT || msgType == TDMT_VND_CREATE_TABLE) {
×
350
    return true;
×
351
  }
352
  return false;
×
353
}
354

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

371
  rpcInit.retryMinInterval = tsRedirectPeriod;
1,244,240✔
372
  rpcInit.retryStepFactor = tsRedirectFactor;
1,244,240✔
373
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
1,244,240✔
374
  rpcInit.retryMaxTimeout = tsMaxRetryWaitTime;
1,244,240✔
375

376
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
1,244,240✔
377
  connLimitNum = TMAX(connLimitNum, 10);
1,244,240✔
378
  connLimitNum = TMIN(connLimitNum, 1000);
1,244,240✔
379
  rpcInit.connLimitNum = connLimitNum;
1,244,240✔
380
  rpcInit.shareConnLimit = tsShareConnLimit;
1,244,240✔
381
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
1,244,240✔
382
  rpcInit.startReadTimer = 1;
1,244,240✔
383
  rpcInit.readTimeout = tsReadTimeout;
1,244,240✔
384
  rpcInit.ipv6 = tsEnableIpv6;
1,244,240✔
385
  rpcInit.enableSSL = tsEnableTLS;
1,244,240✔
386
  rpcInit.enableSasl = tsEnableSasl;
1,244,240✔
387

388
  memcpy(rpcInit.caPath, tsTLSCaPath, strlen(tsTLSCaPath));
1,244,240✔
389
  memcpy(rpcInit.certPath, tsTLSSvrCertPath, strlen(tsTLSSvrCertPath));
1,244,240✔
390
  memcpy(rpcInit.keyPath, tsTLSSvrKeyPath, strlen(tsTLSSvrKeyPath));
1,244,240✔
391
  memcpy(rpcInit.cliCertPath, tsTLSCliCertPath, strlen(tsTLSCliCertPath));
1,244,240✔
392
  memcpy(rpcInit.cliKeyPath, tsTLSCliKeyPath, strlen(tsTLSCliKeyPath));
1,244,240✔
393

394
  int32_t code = taosVersionStrToInt(td_version, &rpcInit.compatibilityVer);
1,244,240✔
395
  if (TSDB_CODE_SUCCESS != code) {
1,244,240✔
396
    tscError("invalid version string.");
×
397
    return code;
×
398
  }
399

400
  tscInfo("rpc max retry timeout %" PRId64 "", rpcInit.retryMaxTimeout);
1,244,240✔
401
  *pDnodeConn = rpcOpen(&rpcInit);
1,244,240✔
402
  if (*pDnodeConn == NULL) {
1,244,240✔
403
    tscError("failed to init connection to server since %s", tstrerror(terrno));
35✔
404
    code = terrno;
35✔
405
  }
406

407
  return code;
1,244,240✔
408
}
409

410
void destroyAllRequests(SHashObj *pRequests) {
2,541,228✔
411
  void *pIter = taosHashIterate(pRequests, NULL);
2,541,228✔
412
  while (pIter != NULL) {
2,541,228✔
413
    int64_t *rid = pIter;
×
414

415
    SRequestObj *pRequest = acquireRequest(*rid);
×
416
    if (pRequest) {
×
417
      destroyRequest(pRequest);
×
418
      (void)releaseRequest(*rid);  // ignore error
×
419
    }
420

421
    pIter = taosHashIterate(pRequests, pIter);
×
422
  }
423
}
2,541,228✔
424

425
void stopAllRequests(SHashObj *pRequests) {
259✔
426
  void *pIter = taosHashIterate(pRequests, NULL);
259✔
427
  while (pIter != NULL) {
259✔
428
    int64_t *rid = pIter;
×
429

430
    SRequestObj *pRequest = acquireRequest(*rid);
×
431
    if (pRequest) {
×
432
      taos_stop_query(pRequest);
×
433
      (void)releaseRequest(*rid);  // ignore error
×
434
    }
435

436
    pIter = taosHashIterate(pRequests, pIter);
×
437
  }
438
}
259✔
439

440
void destroyAppInst(void *info) {
1,244,205✔
441
  SAppInstInfo *pAppInfo = *(SAppInstInfo **)info;
1,244,205✔
442
  tscInfo("destroy app inst mgr %p", pAppInfo);
1,244,205✔
443

444
  int32_t code = taosThreadMutexLock(&appInfo.mutex);
1,244,205✔
445
  if (TSDB_CODE_SUCCESS != code) {
1,244,205✔
446
    tscError("failed to lock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
447
  }
448

449
  hbRemoveAppHbMrg(&pAppInfo->pAppHbMgr);
1,244,205✔
450

451
  code = taosThreadMutexUnlock(&appInfo.mutex);
1,244,205✔
452
  if (TSDB_CODE_SUCCESS != code) {
1,244,205✔
453
    tscError("failed to unlock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
454
  }
455

456
  taosMemoryFreeClear(pAppInfo->instKey);
1,244,205✔
457
  closeTransporter(pAppInfo);
1,244,205✔
458

459
  code = taosThreadMutexLock(&pAppInfo->qnodeMutex);
1,244,205✔
460
  if (TSDB_CODE_SUCCESS != code) {
1,244,205✔
461
    tscError("failed to lock qnode mutex, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
462
  }
463

464
  taosArrayDestroy(pAppInfo->pQnodeList);
1,244,205✔
465
  code = taosThreadMutexUnlock(&pAppInfo->qnodeMutex);
1,244,205✔
466
  if (TSDB_CODE_SUCCESS != code) {
1,244,205✔
467
    tscError("failed to unlock qnode mutex, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
468
  }
469

470
  taosMemoryFree(pAppInfo);
1,244,205✔
471
}
1,244,205✔
472

473
//  tscObj 1--->conn1
474
/// tscObj 2-->conn1
475
//  tscObj 3-->conn1
476

477
void destroyTscObj(void *pObj) {
2,541,228✔
478
  if (NULL == pObj) {
2,541,228✔
479
    return;
×
480
  }
481

482
  STscObj *pTscObj = pObj;
2,541,228✔
483
  int64_t  tscId = pTscObj->id;
2,541,228✔
484
  tscTrace("conn:%" PRIx64 ", begin destroy, p:%p", tscId, pTscObj);
2,541,228✔
485

486
  SClientHbKey connKey = {.tscRid = pTscObj->id, .connType = pTscObj->connType};
2,541,228✔
487
  hbDeregisterConn(pTscObj, connKey);
2,541,228✔
488

489
  destroyAllRequests(pTscObj->pRequests);
2,541,228✔
490
  taosHashCleanup(pTscObj->pRequests);
2,541,228✔
491

492
  schedulerStopQueryHb(pTscObj->pAppInfo->pTransporter);
2,541,228✔
493
  tscDebug("conn:0x%" PRIx64 ", p:%p destroyed, remain inst totalConn:%" PRId64, pTscObj->id, pTscObj,
2,541,228✔
494
           pTscObj->pAppInfo->numOfConns);
495

496
  // In any cases, we should not free app inst here. Or an race condition rises.
497
  /*int64_t connNum = */ (void)atomic_sub_fetch_64(&pTscObj->pAppInfo->numOfConns, 1);
2,541,228✔
498

499
  (void)taosThreadMutexDestroy(&pTscObj->mutex);
2,541,228✔
500
  taosMemoryFree(pTscObj);
2,541,228✔
501

502
  tscTrace("conn:0x%" PRIx64 ", end destroy, p:%p", tscId, pTscObj);
2,541,228✔
503
}
504

505
int32_t createTscObj(const char *user, const char *auth, const char *db, int32_t connType, SAppInstInfo *pAppInfo,
2,687,330✔
506
                     STscObj **pObj) {
507
  *pObj = (STscObj *)taosMemoryCalloc(1, sizeof(STscObj));
2,687,330✔
508
  if (NULL == *pObj) {
2,687,330✔
509
    return terrno;
×
510
  }
511

512
  (*pObj)->pRequests = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
2,687,330✔
513
  if (NULL == (*pObj)->pRequests) {
2,687,079✔
514
    taosMemoryFree(*pObj);
×
515
    return terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
516
  }
517

518
  (*pObj)->connType = connType;
2,687,079✔
519
  (*pObj)->pAppInfo = pAppInfo;
2,687,330✔
520
  (*pObj)->appHbMgrIdx = pAppInfo->pAppHbMgr->idx;
2,687,079✔
521
  tstrncpy((*pObj)->user, user, sizeof((*pObj)->user));
2,686,576✔
522
  (void)memcpy((*pObj)->pass, auth, TSDB_PASSWORD_LEN);
2,686,827✔
523

524
  if (db != NULL) {
2,687,079✔
525
    tstrncpy((*pObj)->db, db, tListLen((*pObj)->db));
2,687,330✔
526
  }
527

528
  TSC_ERR_RET(taosThreadMutexInit(&(*pObj)->mutex, NULL));
2,687,079✔
529

530
  int32_t code = TSDB_CODE_SUCCESS;
2,686,576✔
531

532
  (*pObj)->id = taosAddRef(clientConnRefPool, *pObj);
2,686,576✔
533
  if ((*pObj)->id < 0) {
2,687,079✔
534
    tscError("failed to add object to clientConnRefPool");
×
535
    code = terrno;
×
536
    taosMemoryFree(*pObj);
×
537
    return code;
×
538
  }
539

540
  (void)atomic_add_fetch_64(&(*pObj)->pAppInfo->numOfConns, 1);
2,687,079✔
541

542
  tscInfo("conn:0x%" PRIx64 ", created, p:%p", (*pObj)->id, *pObj);
2,687,330✔
543
  return code;
2,687,330✔
544
}
545

546
STscObj *acquireTscObj(int64_t rid) { return (STscObj *)taosAcquireRef(clientConnRefPool, rid); }
1,338,066,289✔
547

548
void releaseTscObj(int64_t rid) {
1,335,869,538✔
549
  int32_t code = taosReleaseRef(clientConnRefPool, rid);
1,335,869,538✔
550
  if (TSDB_CODE_SUCCESS != code) {
1,335,873,603✔
551
    tscWarn("failed to release TscObj, code:%s", tstrerror(code));
×
552
  }
553
}
1,335,873,603✔
554

555
int32_t createRequest(uint64_t connId, int32_t type, int64_t reqid, SRequestObj **pRequest) {
635,257,343✔
556
  int32_t code = TSDB_CODE_SUCCESS;
635,257,343✔
557
  *pRequest = (SRequestObj *)taosMemoryCalloc(1, sizeof(SRequestObj));
635,257,343✔
558
  if (NULL == *pRequest) {
635,250,190✔
559
    return terrno;
×
560
  }
561

562
  STscObj *pTscObj = acquireTscObj(connId);
635,250,892✔
563
  if (pTscObj == NULL) {
635,268,721✔
564
    TSC_ERR_JRET(TSDB_CODE_TSC_DISCONNECTED);
×
565
  }
566
  SSyncQueryParam *interParam = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
635,268,721✔
567
  if (interParam == NULL) {
635,262,221✔
568
    releaseTscObj(connId);
×
569
    TSC_ERR_JRET(terrno);
×
570
  }
571
  TSC_ERR_JRET(tsem_init(&interParam->sem, 0, 0));
635,262,221✔
572
  interParam->pRequest = *pRequest;
635,265,162✔
573
  (*pRequest)->body.interParam = interParam;
635,263,645✔
574

575
  (*pRequest)->resType = RES_TYPE__QUERY;
635,267,364✔
576
  (*pRequest)->requestId = reqid == 0 ? generateRequestId() : reqid;
635,265,207✔
577
  (*pRequest)->metric.start = taosGetTimestampUs();
1,267,330,658✔
578

579
  (*pRequest)->body.resInfo.convertUcs4 = true;  // convert ucs4 by default
635,261,255✔
580
  (*pRequest)->body.resInfo.charsetCxt = pTscObj->optionInfo.charsetCxt;
635,267,281✔
581
  (*pRequest)->type = type;
635,267,193✔
582
  (*pRequest)->allocatorRefId = -1;
635,268,197✔
583

584
  (*pRequest)->pDb = getDbOfConnection(pTscObj);
635,267,165✔
585
  if (NULL == (*pRequest)->pDb) {
635,261,908✔
586
    TSC_ERR_JRET(terrno);
64,591,260✔
587
  }
588
  (*pRequest)->pTscObj = pTscObj;
635,263,451✔
589
  (*pRequest)->inCallback = false;
635,262,999✔
590
  (*pRequest)->msgBuf = taosMemoryCalloc(1, ERROR_MSG_BUF_DEFAULT_SIZE);
635,256,724✔
591
  if (NULL == (*pRequest)->msgBuf) {
635,251,611✔
592
    code = terrno;
×
593
    goto _return;
×
594
  }
595
  (*pRequest)->msgBufLen = ERROR_MSG_BUF_DEFAULT_SIZE;
635,254,557✔
596
  TSC_ERR_JRET(tsem_init(&(*pRequest)->body.rspSem, 0, 0));
635,260,592✔
597
  TSC_ERR_JRET(registerRequest(*pRequest, pTscObj));
635,258,077✔
598

599
  return TSDB_CODE_SUCCESS;
635,262,292✔
600
_return:
×
601
  if ((*pRequest)->pTscObj) {
×
602
    doDestroyRequest(*pRequest);
×
603
  } else {
604
    taosMemoryFree(*pRequest);
×
605
  }
606
  return code;
×
607
}
608

609
void doFreeReqResultInfo(SReqResultInfo *pResInfo) {
667,689,615✔
610
  taosMemoryFreeClear(pResInfo->pRspMsg);
667,689,615✔
611
  taosMemoryFreeClear(pResInfo->length);
667,689,954✔
612
  taosMemoryFreeClear(pResInfo->row);
667,689,466✔
613
  taosMemoryFreeClear(pResInfo->pCol);
667,689,708✔
614
  taosMemoryFreeClear(pResInfo->fields);
667,685,946✔
615
  taosMemoryFreeClear(pResInfo->userFields);
667,689,844✔
616
  taosMemoryFreeClear(pResInfo->convertJson);
667,686,430✔
617
  taosMemoryFreeClear(pResInfo->decompBuf);
667,691,136✔
618

619
  if (pResInfo->convertBuf != NULL) {
667,688,425✔
620
    for (int32_t i = 0; i < pResInfo->numOfCols; ++i) {
563,391,683✔
621
      taosMemoryFreeClear(pResInfo->convertBuf[i]);
461,922,779✔
622
    }
623
    taosMemoryFreeClear(pResInfo->convertBuf);
101,470,797✔
624
  }
625
}
667,689,372✔
626

627
SRequestObj *acquireRequest(int64_t rid) { return (SRequestObj *)taosAcquireRef(clientReqRefPool, rid); }
2,147,483,647✔
628

629
int32_t releaseRequest(int64_t rid) { return taosReleaseRef(clientReqRefPool, rid); }
2,147,483,647✔
630

631
int32_t removeRequest(int64_t rid) { return taosRemoveRef(clientReqRefPool, rid); }
635,135,411✔
632

633
/// return the most previous req ref id
634
int64_t removeFromMostPrevReq(SRequestObj *pRequest) {
635,134,578✔
635
  int64_t      mostPrevReqRefId = pRequest->self;
635,134,578✔
636
  SRequestObj *pTmp = pRequest;
635,137,707✔
637
  while (pTmp->relation.prevRefId) {
635,137,707✔
638
    pTmp = acquireRequest(pTmp->relation.prevRefId);
×
639
    if (pTmp) {
×
640
      mostPrevReqRefId = pTmp->self;
×
641
      (void)releaseRequest(mostPrevReqRefId);  // ignore error
×
642
    } else {
643
      break;
×
644
    }
645
  }
646
  (void)removeRequest(mostPrevReqRefId);  // ignore error
635,137,627✔
647
  return mostPrevReqRefId;
635,140,044✔
648
}
649

650
void destroyNextReq(int64_t nextRefId) {
635,131,395✔
651
  if (nextRefId) {
635,131,395✔
652
    SRequestObj *pObj = acquireRequest(nextRefId);
×
653
    if (pObj) {
×
654
      (void)releaseRequest(nextRefId);  // ignore error
×
655
      (void)releaseRequest(nextRefId);  // ignore error
×
656
    }
657
  }
658
}
635,131,395✔
659

660
void destroySubRequests(SRequestObj *pRequest) {
×
661
  int32_t      reqIdx = -1;
×
662
  SRequestObj *pReqList[16] = {NULL};
×
663
  uint64_t     tmpRefId = 0;
×
664

665
  if (pRequest->relation.userRefId && pRequest->relation.userRefId != pRequest->self) {
×
666
    return;
×
667
  }
668

669
  SRequestObj *pTmp = pRequest;
×
670
  while (pTmp->relation.prevRefId) {
×
671
    tmpRefId = pTmp->relation.prevRefId;
×
672
    pTmp = acquireRequest(tmpRefId);
×
673
    if (pTmp) {
×
674
      pReqList[++reqIdx] = pTmp;
×
675
      (void)releaseRequest(tmpRefId);  // ignore error
×
676
    } else {
677
      tscError("prev req ref 0x%" PRIx64 " is not there", tmpRefId);
×
678
      break;
×
679
    }
680
  }
681

682
  for (int32_t i = reqIdx; i >= 0; i--) {
×
683
    (void)removeRequest(pReqList[i]->self);  // ignore error
×
684
  }
685

686
  tmpRefId = pRequest->relation.nextRefId;
×
687
  while (tmpRefId) {
×
688
    pTmp = acquireRequest(tmpRefId);
×
689
    if (pTmp) {
×
690
      tmpRefId = pTmp->relation.nextRefId;
×
691
      (void)removeRequest(pTmp->self);   // ignore error
×
692
      (void)releaseRequest(pTmp->self);  // ignore error
×
693
    } else {
694
      tscError("next req ref 0x%" PRIx64 " is not there", tmpRefId);
×
695
      break;
×
696
    }
697
  }
698
}
699

700
void doDestroyRequest(void *p) {
635,136,520✔
701
  if (NULL == p) {
635,136,520✔
702
    return;
×
703
  }
704

705
  SRequestObj *pRequest = (SRequestObj *)p;
635,136,520✔
706

707
  uint64_t reqId = pRequest->requestId;
635,136,520✔
708
  tscTrace("QID:0x%" PRIx64 ", begin destroy request, res:%p", reqId, pRequest);
635,133,610✔
709

710
  int64_t nextReqRefId = pRequest->relation.nextRefId;
635,133,610✔
711

712
  int32_t code = taosHashRemove(pRequest->pTscObj->pRequests, &pRequest->self, sizeof(pRequest->self));
635,136,485✔
713
  if (TSDB_CODE_SUCCESS != code) {
635,137,077✔
714
    tscDebug("failed to remove request from hash since %s", tstrerror(code));
2,687,330✔
715
  }
716
  schedulerFreeJob(&pRequest->body.queryJob, 0);
635,137,077✔
717

718
  destorySqlCallbackWrapper(pRequest->pWrapper);
635,139,738✔
719

720
  taosMemoryFreeClear(pRequest->msgBuf);
635,132,391✔
721

722
  doFreeReqResultInfo(&pRequest->body.resInfo);
635,128,027✔
723
  if (TSDB_CODE_SUCCESS != tsem_destroy(&pRequest->body.rspSem)) {
635,128,700✔
724
    tscError("failed to destroy semaphore");
×
725
  }
726

727
  SSessParam para = {.type = SESSION_MAX_CONCURRENCY, .value = -1};
635,132,061✔
728
  code = tscUpdateSessMgtMetric(pRequest->pTscObj, &para);
635,131,241✔
729

730
  taosArrayDestroy(pRequest->tableList);
635,142,642✔
731
  taosArrayDestroy(pRequest->targetTableList);
635,142,642✔
732
  destroyQueryExecRes(&pRequest->body.resInfo.execRes);
635,142,642✔
733

734
  if (pRequest->self) {
635,142,354✔
735
    deregisterRequest(pRequest);
635,142,354✔
736
  }
737

738
  taosMemoryFreeClear(pRequest->pDb);
635,141,494✔
739
  taosArrayDestroy(pRequest->dbList);
635,136,272✔
740
  if (pRequest->body.interParam) {
635,141,301✔
741
    if (TSDB_CODE_SUCCESS != tsem_destroy(&((SSyncQueryParam *)pRequest->body.interParam)->sem)) {
635,138,991✔
742
      tscError("failed to destroy semaphore in pRequest");
×
743
    }
744
  }
745
  taosMemoryFree(pRequest->body.interParam);
635,142,628✔
746

747
  qDestroyQuery(pRequest->pQuery);
635,137,884✔
748
  nodesDestroyAllocator(pRequest->allocatorRefId);
635,136,651✔
749

750
  taosMemoryFreeClear(pRequest->effectiveUser);
635,135,016✔
751
  taosMemoryFreeClear(pRequest->sqlstr);
635,131,510✔
752
  taosMemoryFree(pRequest);
635,135,996✔
753
  tscTrace("QID:0x%" PRIx64 ", end destroy request, res:%p", reqId, pRequest);
635,132,723✔
754
  destroyNextReq(nextReqRefId);
635,132,723✔
755
}
756

757
void destroyRequest(SRequestObj *pRequest) {
635,138,424✔
758
  if (pRequest == NULL) return;
635,138,424✔
759

760
  taos_stop_query(pRequest);
635,138,424✔
761
  (void)removeFromMostPrevReq(pRequest);
635,137,502✔
762
}
763

764
void taosStopQueryImpl(SRequestObj *pRequest) {
635,136,651✔
765
  pRequest->killed = true;
635,136,651✔
766

767
  // It is not a query, no need to stop.
768
  if (NULL == pRequest->pQuery || QUERY_EXEC_MODE_SCHEDULE != pRequest->pQuery->execMode) {
635,139,473✔
769
    tscDebug("QID:0x%" PRIx64 ", no need to be killed since not query", pRequest->requestId);
43,871,906✔
770
    return;
43,873,648✔
771
  }
772

773
  schedulerFreeJob(&pRequest->body.queryJob, TSDB_CODE_TSC_QUERY_KILLED);
591,265,939✔
774
  tscDebug("QID:0x%" PRIx64 ", killed", pRequest->requestId);
591,264,450✔
775
}
776

777
void stopAllQueries(SRequestObj *pRequest) {
635,129,718✔
778
  int32_t      reqIdx = -1;
635,129,718✔
779
  SRequestObj *pReqList[16] = {NULL};
635,129,718✔
780
  uint64_t     tmpRefId = 0;
635,131,094✔
781

782
  if (pRequest->relation.userRefId && pRequest->relation.userRefId != pRequest->self) {
635,131,094✔
783
    return;
×
784
  }
785

786
  SRequestObj *pTmp = pRequest;
635,134,297✔
787
  while (pTmp->relation.prevRefId) {
635,134,297✔
788
    tmpRefId = pTmp->relation.prevRefId;
×
789
    pTmp = acquireRequest(tmpRefId);
×
790
    if (pTmp) {
×
791
      pReqList[++reqIdx] = pTmp;
×
792
    } else {
793
      tscError("prev req ref 0x%" PRIx64 " is not there", tmpRefId);
×
794
      break;
×
795
    }
796
  }
797

798
  for (int32_t i = reqIdx; i >= 0; i--) {
635,134,252✔
799
    taosStopQueryImpl(pReqList[i]);
×
800
    (void)releaseRequest(pReqList[i]->self);  // ignore error
×
801
  }
802

803
  taosStopQueryImpl(pRequest);
635,134,252✔
804

805
  tmpRefId = pRequest->relation.nextRefId;
635,138,348✔
806
  while (tmpRefId) {
635,135,240✔
807
    pTmp = acquireRequest(tmpRefId);
×
808
    if (pTmp) {
×
809
      tmpRefId = pTmp->relation.nextRefId;
×
810
      taosStopQueryImpl(pTmp);
×
811
      (void)releaseRequest(pTmp->self);  // ignore error
×
812
    } else {
813
      tscError("next req ref 0x%" PRIx64 " is not there", tmpRefId);
×
814
      break;
×
815
    }
816
  }
817
}
818
#ifdef USE_REPORT
819
void crashReportThreadFuncUnexpectedStopped(void) { atomic_store_32(&clientStop, -1); }
×
820

821
static void *tscCrashReportThreadFp(void *param) {
×
822
  int32_t code = 0;
×
823
  setThreadName("client-crashReport");
×
824
  char filepath[PATH_MAX] = {0};
×
825
  (void)snprintf(filepath, sizeof(filepath), "%s%s.taosCrashLog", tsLogDir, TD_DIRSEP);
×
826
  char     *pMsg = NULL;
×
827
  int64_t   msgLen = 0;
×
828
  TdFilePtr pFile = NULL;
×
829
  bool      truncateFile = false;
×
830
  int32_t   sleepTime = 200;
×
831
  int32_t   reportPeriodNum = 3600 * 1000 / sleepTime;
×
832
  int32_t   loopTimes = reportPeriodNum;
×
833

834
#ifdef WINDOWS
835
  if (taosCheckCurrentInDll()) {
836
    atexit(crashReportThreadFuncUnexpectedStopped);
837
  }
838
#endif
839

840
  if (-1 != atomic_val_compare_exchange_32(&clientStop, -1, 0)) {
×
841
    return NULL;
×
842
  }
843
  STelemAddrMgmt mgt;
×
844
  code = taosTelemetryMgtInit(&mgt, tsTelemServer);
×
845
  if (code) {
×
846
    tscError("failed to init telemetry management, code:%s", tstrerror(code));
×
847
    return NULL;
×
848
  }
849

850
  code = initCrashLogWriter();
×
851
  if (code) {
×
852
    tscError("failed to init crash log writer, code:%s", tstrerror(code));
×
853
    return NULL;
×
854
  }
855

856
  while (1) {
857
    checkAndPrepareCrashInfo();
×
858
    if (clientStop > 0 && reportThreadSetQuit()) break;
×
859
    if (loopTimes++ < reportPeriodNum) {
×
860
      if (loopTimes < 0) loopTimes = reportPeriodNum;
×
861
      taosMsleep(sleepTime);
×
862
      continue;
×
863
    }
864

865
    taosReadCrashInfo(filepath, &pMsg, &msgLen, &pFile);
×
866
    if (pMsg && msgLen > 0) {
×
867
      if (taosSendTelemReport(&mgt, tsClientCrashReportUri, tsTelemPort, pMsg, msgLen, HTTP_FLAT) != 0) {
×
868
        tscError("failed to send crash report");
×
869
        if (pFile) {
×
870
          taosReleaseCrashLogFile(pFile, false);
×
871
          pFile = NULL;
×
872

873
          taosMsleep(sleepTime);
×
874
          loopTimes = 0;
×
875
          continue;
×
876
        }
877
      } else {
878
        tscInfo("succeed to send crash report");
×
879
        truncateFile = true;
×
880
      }
881
    } else {
882
      tscInfo("no crash info was found");
×
883
    }
884

885
    taosMemoryFree(pMsg);
×
886

887
    if (pMsg && msgLen > 0) {
×
888
      pMsg = NULL;
×
889
      continue;
×
890
    }
891

892
    if (pFile) {
×
893
      taosReleaseCrashLogFile(pFile, truncateFile);
×
894
      pFile = NULL;
×
895
      truncateFile = false;
×
896
    }
897

898
    taosMsleep(sleepTime);
×
899
    loopTimes = 0;
×
900
  }
901
  taosTelemetryDestroy(&mgt);
×
902

903
  clientStop = -2;
×
904
  return NULL;
×
905
}
906

907
int32_t tscCrashReportInit() {
1,188,043✔
908
  if (!tsEnableCrashReport) {
1,188,043✔
909
    return TSDB_CODE_SUCCESS;
1,188,043✔
910
  }
911
  int32_t      code = TSDB_CODE_SUCCESS;
×
912
  TdThreadAttr thAttr;
×
913
  TSC_ERR_JRET(taosThreadAttrInit(&thAttr));
×
914
  TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE));
×
915
  TdThread crashReportThread;
×
916
  if (taosThreadCreate(&crashReportThread, &thAttr, tscCrashReportThreadFp, NULL) != 0) {
×
917
    tscError("failed to create crashReport thread since %s", strerror(ERRNO));
×
918
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
919
    TSC_ERR_RET(terrno);
×
920
  }
921

922
  (void)taosThreadAttrDestroy(&thAttr);
×
923
_return:
×
924
  if (code) {
×
925
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
926
    TSC_ERR_RET(terrno);
×
927
  }
928

929
  return code;
×
930
}
931

932
void tscStopCrashReport() {
1,188,303✔
933
  if (!tsEnableCrashReport) {
1,188,303✔
934
    return;
1,188,303✔
935
  }
936

937
  if (atomic_val_compare_exchange_32(&clientStop, 0, 1)) {
×
938
    tscDebug("crash report thread already stopped");
×
939
    return;
×
940
  }
941

942
  while (atomic_load_32(&clientStop) > 0) {
×
943
    taosMsleep(100);
×
944
  }
945
}
946

947
void taos_write_crashinfo(int signum, void *sigInfo, void *context) {
×
948
  writeCrashLogToFile(signum, sigInfo, CUS_PROMPT, lastClusterId, appInfo.startTime);
×
949
}
×
950
#endif
951

952
#ifdef TAOSD_INTEGRATED
953
typedef struct {
954
  TdThread pid;
955
  int32_t  stat;  // < 0: start failed, 0: init(not start), 1: start successfully
956
} SDaemonObj;
957

958
extern int  dmStartDaemon(int argc, char const *argv[]);
959
extern void dmStopDaemon();
960

961
SDaemonObj daemonObj = {0};
962

963
typedef struct {
964
  int32_t argc;
965
  char  **argv;
966
} SExecArgs;
967

968
static void *dmStartDaemonFunc(void *param) {
969
  int32_t    code = 0;
970
  SExecArgs *pArgs = (SExecArgs *)param;
971
  int32_t    argc = pArgs->argc;
972
  char     **argv = pArgs->argv;
973

974
  code = dmStartDaemon(argc, (const char **)argv);
975
  if (code != 0) {
976
    printf("failed to start taosd since %s\r\n", tstrerror(code));
977
    goto _exit;
978
  }
979

980
_exit:
981
  if (code != 0) {
982
    atomic_store_32(&daemonObj.stat, code);
983
  }
984
  return NULL;
985
}
986

987
static int32_t shellStartDaemon(int argc, char *argv[]) {
988
  int32_t    code = 0, lino = 0;
989
  SExecArgs *pArgs = NULL;
990
  int64_t    startMs = taosGetTimestampMs(), endMs = startMs;
991

992
  TdThreadAttr thAttr;
993
  (void)taosThreadAttrInit(&thAttr);
994
  (void)taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE);
995
#ifdef TD_COMPACT_OS
996
  (void)taosThreadAttrSetStackSize(&thAttr, STACK_SIZE_SMALL);
997
#endif
998
  pArgs = (SExecArgs *)taosMemoryCalloc(1, sizeof(SExecArgs));
999
  if (pArgs == NULL) {
1000
    code = terrno;
1001
    TAOS_CHECK_EXIT(code);
1002
  }
1003
  pArgs->argc = argc;
1004
  pArgs->argv = argv;
1005

1006
#ifndef TD_AS_LIB
1007
  tsLogEmbedded = 1;
1008
#endif
1009

1010
  TAOS_CHECK_EXIT(taosThreadCreate(&daemonObj.pid, &thAttr, dmStartDaemonFunc, pArgs));
1011

1012
  while (true) {
1013
    if (atomic_load_64(&tsDndStart)) {
1014
      atomic_store_32(&daemonObj.stat, 1);
1015
      break;
1016
    }
1017
    int32_t daemonstat = atomic_load_32(&daemonObj.stat);
1018
    if (daemonstat < 0) {
1019
      code = daemonstat;
1020
      TAOS_CHECK_EXIT(code);
1021
    }
1022

1023
    if (daemonstat > 1) {
1024
      code = TSDB_CODE_APP_ERROR;
1025
      TAOS_CHECK_EXIT(code);
1026
    }
1027
    taosMsleep(1000);
1028
  }
1029

1030
_exit:
1031
  endMs = taosGetTimestampMs();
1032
  (void)taosThreadAttrDestroy(&thAttr);
1033
  taosMemoryFreeClear(pArgs);
1034
  if (code) {
1035
    printf("\r\n The daemon start failed at line %d since %s, cost %" PRIi64 " ms\r\n", lino, tstrerror(code),
1036
           endMs - startMs);
1037
  } else {
1038
    printf("\r\n The daemon started successfully, cost %" PRIi64 " ms\r\n", endMs - startMs);
1039
  }
1040
#ifndef TD_AS_LIB
1041
  tsLogEmbedded = 0;
1042
#endif
1043
  return code;
1044
}
1045

1046
void shellStopDaemon() {
1047
#ifndef TD_AS_LIB
1048
  tsLogEmbedded = 1;
1049
#endif
1050
  dmStopDaemon();
1051
  if (taosCheckPthreadValid(daemonObj.pid)) {
1052
    (void)taosThreadJoin(daemonObj.pid, NULL);
1053
    taosThreadClear(&daemonObj.pid);
1054
  }
1055
}
1056
#endif
1057

1058
void taos_init_imp(void) {
1,188,303✔
1059
#if defined(LINUX)
1060
  if (tscDbg.memEnable) {
1,188,303✔
1061
    int32_t code = taosMemoryDbgInit();
×
1062
    if (code) {
×
1063
      (void)printf("failed to init memory dbg, error:%s\n", tstrerror(code));
×
1064
    } else {
1065
      tsAsyncLog = false;
×
1066
      (void)printf("memory dbg enabled\n");
×
1067
    }
1068
  }
1069
#endif
1070

1071
  // In the APIs of other program language, taos_cleanup is not available yet.
1072
  // So, to make sure taos_cleanup will be invoked to clean up the allocated resource to suppress the valgrind warning.
1073
  (void)atexit(taos_cleanup);
1,188,303✔
1074
  SET_ERRNO(TSDB_CODE_SUCCESS);
1,188,303✔
1075
  terrno = TSDB_CODE_SUCCESS;
1,188,303✔
1076
  taosSeedRand(taosGetTimestampSec());
1,188,303✔
1077

1078
  appInfo.pid = taosGetPId();
1,188,303✔
1079
  appInfo.startTime = taosGetTimestampMs();
1,188,303✔
1080
  appInfo.pInstMap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
1,188,303✔
1081
  appInfo.pInstMapByClusterId =
1,188,303✔
1082
      taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK);
1,188,303✔
1083
  if (NULL == appInfo.pInstMap || NULL == appInfo.pInstMapByClusterId) {
1,188,303✔
1084
    (void)printf("failed to allocate memory when init appInfo\n");
×
1085
    tscInitRes = terrno;
×
1086
    return;
×
1087
  }
1088
  taosHashSetFreeFp(appInfo.pInstMap, destroyAppInst);
1,188,303✔
1089

1090
  const char *logName = CUS_PROMPT "log";
1,188,303✔
1091
  ENV_ERR_RET(taosInitLogOutput(&logName), "failed to init log output");
1,188,303✔
1092
  if (taosCreateLog(logName, 10, configDir, NULL, NULL, NULL, NULL, 1) != 0) {
1,188,303✔
1093
    (void)printf(" WARING: Create %s failed:%s. configDir=%s\n", logName, strerror(ERRNO), configDir);
260✔
1094
    tscInitRes = terrno;
260✔
1095
    return;
260✔
1096
  }
1097

1098
#ifdef TAOSD_INTEGRATED
1099
  ENV_ERR_RET(taosInitCfg(configDir, NULL, NULL, NULL, NULL, 0), "failed to init cfg");
1100
#else
1101
  ENV_ERR_RET(taosInitCfg(configDir, NULL, NULL, NULL, NULL, 1), "failed to init cfg");
1,188,043✔
1102
#endif
1103

1104
  initQueryModuleMsgHandle();
1,188,043✔
1105
#ifndef DISALLOW_NCHAR_WITHOUT_ICONV
1106
  if ((tsCharsetCxt = taosConvInit(tsCharset)) == NULL) {
1,188,043✔
1107
    tscInitRes = terrno;
×
1108
    tscError("failed to init conv");
×
1109
    return;
×
1110
  }
1111
#endif
1112
#if !defined(WINDOWS) && !defined(TD_ASTRA)
1113
  ENV_ERR_RET(tzInit(), "failed to init timezone");
1,188,043✔
1114
#endif
1115
  ENV_ERR_RET(monitorInit(), "failed to init monitor");
1,188,043✔
1116
  ENV_ERR_RET(rpcInit(), "failed to init rpc");
1,188,043✔
1117

1118
  if (InitRegexCache() != 0) {
1,188,043✔
1119
    tscInitRes = terrno;
×
1120
    (void)printf("failed to init regex cache\n");
×
1121
    return;
×
1122
  }
1123

1124
  tscInfo("starting to initialize TAOS driver");
1,188,043✔
1125

1126
  SCatalogCfg cfg = {.maxDBCacheNum = 100, .maxTblCacheNum = 100};
1,188,043✔
1127
  ENV_ERR_RET(catalogInit(&cfg), "failed to init catalog");
1,188,043✔
1128
  ENV_ERR_RET(schedulerInit(), "failed to init scheduler");
1,188,043✔
1129
  ENV_ERR_RET(initClientId(), "failed to init clientId");
1,188,043✔
1130

1131
  ENV_ERR_RET(initTaskQueue(), "failed to init task queue");
1,188,043✔
1132
  ENV_ERR_RET(fmFuncMgtInit(), "failed to init funcMgt");
1,188,043✔
1133
  ENV_ERR_RET(nodesInitAllocatorSet(), "failed to init allocator set");
1,188,043✔
1134

1135
  clientConnRefPool = taosOpenRef(200, destroyTscObj);
1,188,043✔
1136
  clientReqRefPool = taosOpenRef(40960, doDestroyRequest);
1,188,043✔
1137

1138
  ENV_ERR_RET(taosGetAppName(appInfo.appName, NULL), "failed to get app name");
1,188,043✔
1139
  ENV_ERR_RET(taosThreadMutexInit(&appInfo.mutex, NULL), "failed to init thread mutex");
1,188,043✔
1140
#ifdef USE_REPORT
1141
  ENV_ERR_RET(tscCrashReportInit(), "failed to init crash report");
1,188,043✔
1142
#endif
1143
  ENV_ERR_RET(qInitKeywordsTable(), "failed to init parser keywords table");
1,188,043✔
1144
#ifdef TAOSD_INTEGRATED
1145
  ENV_ERR_RET(shellStartDaemon(0, NULL), "failed to start taosd daemon");
1146
#endif
1147

1148
  ENV_ERR_RET(sessMgtInit(), "failed to init session management");
1,188,043✔
1149
  
1150
  tscInfo("TAOS driver is initialized successfully");
1,188,043✔
1151
}
1152

1153
int taos_init() {
1,817,335✔
1154
  (void)taosThreadOnce(&tscinit, taos_init_imp);
1,817,335✔
1155
  return tscInitRes;
1,817,404✔
1156
}
1157

1158
const char *getCfgName(TSDB_OPTION option) {
1,665✔
1159
  const char *name = NULL;
1,665✔
1160

1161
  switch (option) {
1,665✔
1162
    case TSDB_OPTION_SHELL_ACTIVITY_TIMER:
565✔
1163
      name = "shellActivityTimer";
565✔
1164
      break;
565✔
1165
    case TSDB_OPTION_LOCALE:
×
1166
      name = "locale";
×
1167
      break;
×
1168
    case TSDB_OPTION_CHARSET:
×
1169
      name = "charset";
×
1170
      break;
×
1171
    case TSDB_OPTION_TIMEZONE:
1,100✔
1172
      name = "timezone";
1,100✔
1173
      break;
1,100✔
1174
    case TSDB_OPTION_USE_ADAPTER:
×
1175
      name = "useAdapter";
×
1176
      break;
×
1177
    default:
×
1178
      break;
×
1179
  }
1180

1181
  return name;
1,665✔
1182
}
1183

1184
int taos_options_imp(TSDB_OPTION option, const char *str) {
527,533✔
1185
  if (option == TSDB_OPTION_CONFIGDIR) {
527,533✔
1186
#ifndef WINDOWS
1187
    char newstr[PATH_MAX];
520,184✔
1188
    int  len = strlen(str);
525,868✔
1189
    if (len > 1 && str[0] != '"' && str[0] != '\'') {
525,868✔
1190
      if (len + 2 >= PATH_MAX) {
525,635✔
1191
        tscError("Too long path %s", str);
×
1192
        return -1;
×
1193
      }
1194
      newstr[0] = '"';
525,635✔
1195
      (void)memcpy(newstr + 1, str, len);
525,635✔
1196
      newstr[len + 1] = '"';
525,635✔
1197
      newstr[len + 2] = '\0';
525,635✔
1198
      str = newstr;
525,635✔
1199
    }
1200
#endif
1201
    tstrncpy(configDir, str, PATH_MAX);
525,868✔
1202
    tscInfo("set cfg:%s to %s", configDir, str);
525,868✔
1203
    return 0;
525,868✔
1204
  }
1205

1206
  // initialize global config
1207
  if (taos_init() != 0) {
1,665✔
1208
    return -1;
×
1209
  }
1210

1211
  SConfig     *pCfg = taosGetCfg();
1,665✔
1212
  SConfigItem *pItem = NULL;
1,665✔
1213
  const char  *name = getCfgName(option);
1,665✔
1214

1215
  if (name == NULL) {
1,665✔
1216
    tscError("Invalid option %d", option);
×
1217
    return -1;
×
1218
  }
1219

1220
  pItem = cfgGetItem(pCfg, name);
1,665✔
1221
  if (pItem == NULL) {
1,665✔
1222
    tscError("Invalid option %d", option);
×
1223
    return -1;
×
1224
  }
1225

1226
  int code = cfgSetItem(pCfg, name, str, CFG_STYPE_TAOS_OPTIONS, true);
1,665✔
1227
  if (code != 0) {
1,665✔
1228
    tscError("failed to set cfg:%s to %s since %s", name, str, terrstr());
565✔
1229
  } else {
1230
    tscInfo("set cfg:%s to %s", name, str);
1,100✔
1231
    if (TSDB_OPTION_SHELL_ACTIVITY_TIMER == option || TSDB_OPTION_USE_ADAPTER == option) {
1,100✔
1232
      code = taosCfgDynamicOptions(pCfg, name, false);
×
1233
    }
1234
  }
1235

1236
  return code;
1,665✔
1237
}
1238

1239
/**
1240
 * The request id is an unsigned integer format of 64bit.
1241
 *+------------+-----+-----------+---------------+
1242
 *| uid|localIp| PId | timestamp | serial number |
1243
 *+------------+-----+-----------+---------------+
1244
 *| 12bit      |12bit|24bit      |16bit          |
1245
 *+------------+-----+-----------+---------------+
1246
 * @return
1247
 */
1248
uint64_t generateRequestId() {
658,578,241✔
1249
  static uint32_t hashId = 0;
1250
  static int32_t  requestSerialId = 0;
1251

1252
  if (hashId == 0) {
658,578,241✔
1253
    int32_t code = taosGetSystemUUIDU32(&hashId);
1,186,783✔
1254
    if (code != TSDB_CODE_SUCCESS) {
1,186,783✔
1255
      tscError("Failed to get the system uid to generated request id, reason:%s. use ip address instead",
×
1256
               tstrerror(code));
1257
    }
1258
  }
1259

1260
  uint64_t id = 0;
658,574,104✔
1261

1262
  while (true) {
×
1263
    int64_t  ts = taosGetTimestampMs();
658,581,429✔
1264
    uint64_t pid = taosGetPId();
658,581,429✔
1265
    uint32_t val = atomic_add_fetch_32(&requestSerialId, 1);
658,573,345✔
1266
    if (val >= 0xFFFF) atomic_store_32(&requestSerialId, 0);
658,578,040✔
1267

1268
    id = (((uint64_t)(hashId & 0x0FFF)) << 52) | ((pid & 0x0FFF) << 40) | ((ts & 0xFFFFFF) << 16) | (val & 0xFFFF);
658,580,467✔
1269
    if (id) {
658,580,467✔
1270
      break;
658,580,467✔
1271
    }
1272
  }
1273
  return id;
658,580,467✔
1274
}
1275

1276
#if 0
1277
#include "cJSON.h"
1278
static setConfRet taos_set_config_imp(const char *config){
1279
  setConfRet ret = {SET_CONF_RET_SUCC, {0}};
1280
  static bool setConfFlag = false;
1281
  if (setConfFlag) {
1282
    ret.retCode = SET_CONF_RET_ERR_ONLY_ONCE;
1283
    tstrncpy(ret.retMsg, "configuration can only set once", RET_MSG_LENGTH);
1284
    return ret;
1285
  }
1286
  taosInitGlobalCfg();
1287
  cJSON *root = cJSON_Parse(config);
1288
  if (root == NULL){
1289
    ret.retCode = SET_CONF_RET_ERR_JSON_PARSE;
1290
    tstrncpy(ret.retMsg, "parse json error", RET_MSG_LENGTH);
1291
    return ret;
1292
  }
1293

1294
  int size = cJSON_GetArraySize(root);
1295
  if(!cJSON_IsObject(root) || size == 0) {
1296
    ret.retCode = SET_CONF_RET_ERR_JSON_INVALID;
1297
    tstrncpy(ret.retMsg, "json content is invalid, must be not empty object", RET_MSG_LENGTH);
1298
    return ret;
1299
  }
1300

1301
  if(size >= 1000) {
1302
    ret.retCode = SET_CONF_RET_ERR_TOO_LONG;
1303
    tstrncpy(ret.retMsg, "json object size is too long", RET_MSG_LENGTH);
1304
    return ret;
1305
  }
1306

1307
  for(int i = 0; i < size; i++){
1308
    cJSON *item = cJSON_GetArrayItem(root, i);
1309
    if(!item) {
1310
      ret.retCode = SET_CONF_RET_ERR_INNER;
1311
      tstrncpy(ret.retMsg, "inner error", RET_MSG_LENGTH);
1312
      return ret;
1313
    }
1314
    if(!taosReadConfigOption(item->string, item->valuestring, NULL, NULL, TAOS_CFG_CSTATUS_OPTION, TSDB_CFG_CTYPE_B_CLIENT)){
1315
      ret.retCode = SET_CONF_RET_ERR_PART;
1316
      if (strlen(ret.retMsg) == 0){
1317
        snprintf(ret.retMsg, RET_MSG_LENGTH, "part error|%s", item->string);
1318
      }else{
1319
        int tmp = RET_MSG_LENGTH - 1 - (int)strlen(ret.retMsg);
1320
        size_t leftSize = tmp >= 0 ? tmp : 0;
1321
        strncat(ret.retMsg, "|",  leftSize);
1322
        tmp = RET_MSG_LENGTH - 1 - (int)strlen(ret.retMsg);
1323
        leftSize = tmp >= 0 ? tmp : 0;
1324
        strncat(ret.retMsg, item->string, leftSize);
1325
      }
1326
    }
1327
  }
1328
  cJSON_Delete(root);
1329
  setConfFlag = true;
1330
  return ret;
1331
}
1332

1333
setConfRet taos_set_config(const char *config){
1334
  taosThreadMutexLock(&setConfMutex);
1335
  setConfRet ret = taos_set_config_imp(config);
1336
  taosThreadMutexUnlock(&setConfMutex);
1337
  return ret;
1338
}
1339
#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