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

taosdata / TDengine / #4548

22 Jul 2025 02:37AM UTC coverage: 54.273% (-3.0%) from 57.287%
#4548

push

travis-ci

GitHub
Merge pull request #32061 from taosdata/new_testcases

132738 of 315239 branches covered (42.11%)

Branch coverage included in aggregate %.

201371 of 300373 relevant lines covered (67.04%)

3475977.14 hits per line

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

50.7
/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) {
612,698✔
80
  int32_t code = TSDB_CODE_SUCCESS;
612,698✔
81
  // connection has been released already, abort creating request.
82
  pRequest->self = taosAddRef(clientReqRefPool, pRequest);
612,698✔
83
  if (pRequest->self < 0) {
612,774!
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);
612,774✔
90

91
  if (pTscObj->pAppInfo) {
612,767!
92
    SAppClusterSummary *pSummary = &pTscObj->pAppInfo->summary;
612,774✔
93

94
    int32_t total = atomic_add_fetch_64((int64_t *)&pSummary->totalRequests, 1);
612,774✔
95
    int32_t currentInst = atomic_add_fetch_64((int64_t *)&pSummary->currentRequests, 1);
612,786✔
96
    tscDebug("req:0x%" PRIx64 ", create request from conn:0x%" PRIx64
612,773✔
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;
612,772✔
102
}
103

104
static void concatStrings(SArray *list, char *buf, int size) {
37✔
105
  int len = 0;
37✔
106
  for (int i = 0; i < taosArrayGetSize(list); i++) {
93✔
107
    char *db = taosArrayGet(list, i);
56✔
108
    if (NULL == db) {
56!
109
      tscError("get dbname failed, buf:%s", buf);
×
110
      break;
×
111
    }
112
    char *dot = strchr(db, '.');
56✔
113
    if (dot != NULL) {
56!
114
      db = dot + 1;
56✔
115
    }
116
    if (i != 0) {
56✔
117
      (void)strncat(buf, ",", size - 1 - len);
19✔
118
      len += 1;
19✔
119
    }
120
    int ret = tsnprintf(buf + len, size - len, "%s", db);
56✔
121
    if (ret < 0) {
56!
122
      tscError("snprintf failed, buf:%s, ret:%d", buf, ret);
×
123
      break;
×
124
    }
125
    len += ret;
56✔
126
    if (len >= size) {
56!
127
      tscInfo("dbList is truncated, buf:%s, len:%d", buf, len);
×
128
      break;
×
129
    }
130
  }
131
}
37✔
132
#ifdef USE_REPORT
133
static int32_t generateWriteSlowLog(STscObj *pTscObj, SRequestObj *pRequest, int32_t reqType, int64_t duration) {
37✔
134
  cJSON  *json = cJSON_CreateObject();
37✔
135
  int32_t code = TSDB_CODE_SUCCESS;
37✔
136
  if (json == NULL) {
37!
137
    tscError("failed to create monitor json");
×
138
    return TSDB_CODE_OUT_OF_MEMORY;
×
139
  }
140
  char clusterId[32] = {0};
37✔
141
  if (snprintf(clusterId, sizeof(clusterId), "%" PRId64, pTscObj->pAppInfo->clusterId) < 0) {
37!
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};
37✔
148
  if (snprintf(startTs, sizeof(startTs), "%" PRId64, pRequest->metric.start / 1000) < 0) {
37!
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};
37✔
155
  if (snprintf(requestId, sizeof(requestId), "%" PRIu64, pRequest->requestId) < 0) {
37!
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)));
37!
161
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "start_ts", cJSON_CreateString(startTs)));
37!
162
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "request_id", cJSON_CreateString(requestId)));
37!
163
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "query_time", cJSON_CreateNumber(duration / 1000)));
37!
164
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "code", cJSON_CreateNumber(pRequest->code)));
37!
165
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "error_info", cJSON_CreateString(tstrerror(pRequest->code))));
37!
166
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "type", cJSON_CreateNumber(reqType)));
37!
167
  ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(
37!
168
      json, "rows_num", cJSON_CreateNumber(pRequest->body.resInfo.numOfRows + pRequest->body.resInfo.totalRows)));
169
  if (pRequest->sqlstr != NULL &&
37!
170
      strlen(pRequest->sqlstr) > pTscObj->pAppInfo->serverCfg.monitorParas.tsSlowLogMaxLen) {
37!
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)));
37!
177
  }
178

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

183
  char pid[32] = {0};
37✔
184
  if (snprintf(pid, sizeof(pid), "%d", appInfo.pid) < 0) {
37!
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)));
37!
191
  if (pRequest->dbList != NULL) {
37!
192
    char dbList[1024] = {0};
37✔
193
    concatStrings(pRequest->dbList, dbList, sizeof(dbList) - 1);
37✔
194
    ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "db", cJSON_CreateString(dbList)));
37!
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);
37✔
202
  if (value == NULL) {
37!
203
    tscError("failed to print json");
×
204
    code = TSDB_CODE_FAILED;
×
205
    goto _end;
×
206
  }
207
  MonitorSlowLogData data = {0};
37✔
208
  data.clusterId = pTscObj->pAppInfo->clusterId;
37✔
209
  data.type = SLOW_LOG_WRITE;
37✔
210
  data.data = value;
37✔
211
  code = monitorPutData2MonitorQueue(data);
37✔
212
  if (TSDB_CODE_SUCCESS != code) {
37!
213
    taosMemoryFree(value);
×
214
    goto _end;
×
215
  }
216

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

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

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

250
  STscObj            *pTscObj = pRequest->pTscObj;
612,232✔
251
  SAppClusterSummary *pActivity = &pTscObj->pAppInfo->summary;
612,232✔
252

253
  int32_t currentInst = atomic_sub_fetch_64((int64_t *)&pActivity->currentRequests, 1);
612,232✔
254
  int32_t num = atomic_sub_fetch_32(&pTscObj->numOfReqs, 1);
612,300✔
255
  int32_t reqType = SLOW_LOG_TYPE_OTHERS;
612,292✔
256

257
  int64_t duration = taosGetTimestampUs() - pRequest->metric.start;
612,242✔
258
  tscDebug("req:0x%" PRIx64 ", free from conn:0x%" PRIx64 ", QID:0x%" PRIx64
612,242✔
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)) {
612,242!
263
    if ((pRequest->pQuery && pRequest->pQuery->pRoot && QUERY_NODE_VNODE_MODIFY_STMT == pRequest->pQuery->pRoot->type &&
612,281!
264
         (0 == ((SVnodeModifyOpStmt *)pRequest->pQuery->pRoot)->sqlNodeType)) ||
549,370✔
265
        QUERY_NODE_VNODE_MODIFY_STMT == pRequest->stmtType) {
96,593✔
266
      tscDebug("req:0x%" PRIx64 ", insert duration:%" PRId64 "us, parseCost:%" PRId64 "us, ctgCost:%" PRId64
550,037✔
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);
550,037✔
272
      reqType = SLOW_LOG_TYPE_INSERT;
550,085✔
273
    } else if (QUERY_NODE_SELECT_STMT == pRequest->stmtType) {
62,244✔
274
      tscDebug("req:0x%" PRIx64 ", query duration:%" PRId64 "us, parseCost:%" PRId64 "us, ctgCost:%" PRId64
35,106✔
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);
35,106✔
281
      reqType = SLOW_LOG_TYPE_QUERY;
35,099✔
282
    }
283

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

289
#ifdef USE_REPORT
290
  if (pTscObj->pAppInfo->serverCfg.monitorParas.tsEnableMonitor) {
612,283✔
291
    if (QUERY_NODE_VNODE_MODIFY_STMT == pRequest->stmtType || QUERY_NODE_INSERT_STMT == pRequest->stmtType) {
502,081✔
292
      sqlReqLog(pTscObj->id, pRequest->killed, pRequest->code, MONITORSQLTYPEINSERT);
470,890✔
293
    } else if (QUERY_NODE_SELECT_STMT == pRequest->stmtType) {
31,191✔
294
      sqlReqLog(pTscObj->id, pRequest->killed, pRequest->code, MONITORSQLTYPESELECT);
18,686✔
295
    } else if (QUERY_NODE_DELETE_STMT == pRequest->stmtType) {
12,505✔
296
      sqlReqLog(pTscObj->id, pRequest->killed, pRequest->code, MONITORSQLTYPEDELETE);
21✔
297
    }
298
  }
299

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

317
  releaseTscObj(pTscObj->id);
612,283✔
318
}
319

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

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

330
static bool clientRpcRfp(int32_t code, tmsg_t msgType) {
61,319✔
331
  if (NEED_REDIRECT_ERROR(code)) {
61,319!
332
    if (msgType == TDMT_SCH_QUERY || msgType == TDMT_SCH_MERGE_QUERY || msgType == TDMT_SCH_FETCH ||
58,124!
333
        msgType == TDMT_SCH_MERGE_FETCH || msgType == TDMT_SCH_QUERY_HEARTBEAT || msgType == TDMT_SCH_DROP_TASK ||
58,126!
334
        msgType == TDMT_SCH_TASK_NOTIFY) {
335
      return false;
×
336
    }
337
    return true;
58,126✔
338
  } else if (code == TSDB_CODE_UTIL_QUEUE_OUT_OF_MEMORY || code == TSDB_CODE_OUT_OF_RPC_MEMORY_QUEUE) {
3,195!
339
    tscDebug("client msg type %s should retry since %s", TMSG_INFO(msgType), tstrerror(code));
1!
340
    return true;
×
341
  } else {
342
    return false;
3,194✔
343
  }
344
}
345

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

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

370
  rpcInit.retryMinInterval = tsRedirectPeriod;
2,407✔
371
  rpcInit.retryStepFactor = tsRedirectFactor;
2,407✔
372
  rpcInit.retryMaxInterval = tsRedirectMaxPeriod;
2,407✔
373
  rpcInit.retryMaxTimeout = tsMaxRetryWaitTime;
2,407✔
374

375
  int32_t connLimitNum = tsNumOfRpcSessions / (tsNumOfRpcThreads * 3);
2,407✔
376
  connLimitNum = TMAX(connLimitNum, 10);
2,407✔
377
  connLimitNum = TMIN(connLimitNum, 1000);
2,407✔
378
  rpcInit.connLimitNum = connLimitNum;
2,407✔
379
  rpcInit.shareConnLimit = tsShareConnLimit;
2,407✔
380
  rpcInit.timeToGetConn = tsTimeToGetAvailableConn;
2,407✔
381
  rpcInit.startReadTimer = 1;
2,407✔
382
  rpcInit.readTimeout = tsReadTimeout;
2,407✔
383
  rpcInit.ipv6 = tsEnableIpv6;
2,407✔
384

385
  int32_t code = taosVersionStrToInt(td_version, &rpcInit.compatibilityVer);
2,407✔
386
  if (TSDB_CODE_SUCCESS != code) {
2,407!
387
    tscError("invalid version string.");
×
388
    return code;
×
389
  }
390

391
  tscInfo("rpc max retry timeout %" PRId64 "", rpcInit.retryMaxTimeout);
2,407!
392
  *pDnodeConn = rpcOpen(&rpcInit);
2,407✔
393
  if (*pDnodeConn == NULL) {
2,407!
394
    tscError("failed to init connection to server since %s", tstrerror(terrno));
×
395
    code = terrno;
×
396
  }
397

398
  return code;
2,407✔
399
}
400

401
void destroyAllRequests(SHashObj *pRequests) {
8,379✔
402
  void *pIter = taosHashIterate(pRequests, NULL);
8,379✔
403
  while (pIter != NULL) {
8,379!
404
    int64_t *rid = pIter;
×
405

406
    SRequestObj *pRequest = acquireRequest(*rid);
×
407
    if (pRequest) {
×
408
      destroyRequest(pRequest);
×
409
      (void)releaseRequest(*rid);  // ignore error
×
410
    }
411

412
    pIter = taosHashIterate(pRequests, pIter);
×
413
  }
414
}
8,379✔
415

416
void stopAllRequests(SHashObj *pRequests) {
×
417
  void *pIter = taosHashIterate(pRequests, NULL);
×
418
  while (pIter != NULL) {
×
419
    int64_t *rid = pIter;
×
420

421
    SRequestObj *pRequest = acquireRequest(*rid);
×
422
    if (pRequest) {
×
423
      taos_stop_query(pRequest);
×
424
      (void)releaseRequest(*rid);  // ignore error
×
425
    }
426

427
    pIter = taosHashIterate(pRequests, pIter);
×
428
  }
429
}
×
430

431
void destroyAppInst(void *info) {
2,407✔
432
  SAppInstInfo *pAppInfo = *(SAppInstInfo **)info;
2,407✔
433
  tscInfo("destroy app inst mgr %p", pAppInfo);
2,407!
434

435
  int32_t code = taosThreadMutexLock(&appInfo.mutex);
2,407✔
436
  if (TSDB_CODE_SUCCESS != code) {
2,407!
437
    tscError("failed to lock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
438
  }
439

440
  hbRemoveAppHbMrg(&pAppInfo->pAppHbMgr);
2,407✔
441

442
  code = taosThreadMutexUnlock(&appInfo.mutex);
2,407✔
443
  if (TSDB_CODE_SUCCESS != code) {
2,407!
444
    tscError("failed to unlock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
445
  }
446

447
  taosMemoryFreeClear(pAppInfo->instKey);
2,407!
448
  closeTransporter(pAppInfo);
2,407✔
449

450
  code = taosThreadMutexLock(&pAppInfo->qnodeMutex);
2,407✔
451
  if (TSDB_CODE_SUCCESS != code) {
2,407!
452
    tscError("failed to lock qnode mutex, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
453
  }
454

455
  taosArrayDestroy(pAppInfo->pQnodeList);
2,407✔
456
  code = taosThreadMutexUnlock(&pAppInfo->qnodeMutex);
2,407✔
457
  if (TSDB_CODE_SUCCESS != code) {
2,407!
458
    tscError("failed to unlock qnode mutex, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code)));
×
459
  }
460

461
  taosMemoryFree(pAppInfo);
2,407!
462
}
2,407✔
463

464
void destroyTscObj(void *pObj) {
8,379✔
465
  if (NULL == pObj) {
8,379!
466
    return;
×
467
  }
468

469
  STscObj *pTscObj = pObj;
8,379✔
470
  int64_t  tscId = pTscObj->id;
8,379✔
471
  tscTrace("conn:%" PRIx64 ", begin destroy, p:%p", tscId, pTscObj);
8,379!
472

473
  SClientHbKey connKey = {.tscRid = pTscObj->id, .connType = pTscObj->connType};
8,379✔
474
  hbDeregisterConn(pTscObj, connKey);
8,379✔
475

476
  destroyAllRequests(pTscObj->pRequests);
8,379✔
477
  taosHashCleanup(pTscObj->pRequests);
8,379✔
478

479
  schedulerStopQueryHb(pTscObj->pAppInfo->pTransporter);
8,379✔
480
  tscDebug("conn:0x%" PRIx64 ", p:%p destroyed, remain inst totalConn:%" PRId64, pTscObj->id, pTscObj,
8,379✔
481
           pTscObj->pAppInfo->numOfConns);
482

483
  // In any cases, we should not free app inst here. Or an race condition rises.
484
  /*int64_t connNum = */ (void)atomic_sub_fetch_64(&pTscObj->pAppInfo->numOfConns, 1);
8,379✔
485

486
  (void)taosThreadMutexDestroy(&pTscObj->mutex);
8,379✔
487
  taosMemoryFree(pTscObj);
8,379!
488

489
  tscTrace("conn:0x%" PRIx64 ", end destroy, p:%p", tscId, pTscObj);
8,379!
490
}
491

492
int32_t createTscObj(const char *user, const char *auth, const char *db, int32_t connType, SAppInstInfo *pAppInfo,
9,089✔
493
                     STscObj **pObj) {
494
  *pObj = (STscObj *)taosMemoryCalloc(1, sizeof(STscObj));
9,089!
495
  if (NULL == *pObj) {
9,089!
496
    return terrno;
×
497
  }
498

499
  (*pObj)->pRequests = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
9,089✔
500
  if (NULL == (*pObj)->pRequests) {
9,089!
501
    taosMemoryFree(*pObj);
×
502
    return terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY;
×
503
  }
504

505
  (*pObj)->connType = connType;
9,089✔
506
  (*pObj)->pAppInfo = pAppInfo;
9,089✔
507
  (*pObj)->appHbMgrIdx = pAppInfo->pAppHbMgr->idx;
9,089✔
508
  tstrncpy((*pObj)->user, user, sizeof((*pObj)->user));
9,089✔
509
  (void)memcpy((*pObj)->pass, auth, TSDB_PASSWORD_LEN);
9,089✔
510

511
  if (db != NULL) {
9,089!
512
    tstrncpy((*pObj)->db, db, tListLen((*pObj)->db));
9,089✔
513
  }
514

515
  TSC_ERR_RET(taosThreadMutexInit(&(*pObj)->mutex, NULL));
9,089!
516

517
  int32_t code = TSDB_CODE_SUCCESS;
9,089✔
518

519
  (*pObj)->id = taosAddRef(clientConnRefPool, *pObj);
9,089✔
520
  if ((*pObj)->id < 0) {
9,089!
521
    tscError("failed to add object to clientConnRefPool");
×
522
    code = terrno;
×
523
    taosMemoryFree(*pObj);
×
524
    return code;
×
525
  }
526

527
  (void)atomic_add_fetch_64(&(*pObj)->pAppInfo->numOfConns, 1);
9,089✔
528

529
  tscInfo("conn:0x%" PRIx64 ", created, p:%p", (*pObj)->id, *pObj);
9,089!
530
  return code;
9,089✔
531
}
532

533
STscObj *acquireTscObj(int64_t rid) { return (STscObj *)taosAcquireRef(clientConnRefPool, rid); }
1,176,111✔
534

535
void releaseTscObj(int64_t rid) {
1,173,481✔
536
  int32_t code = taosReleaseRef(clientConnRefPool, rid);
1,173,481✔
537
  if (TSDB_CODE_SUCCESS != code) {
1,173,607!
538
    tscWarn("failed to release TscObj, code:%s", tstrerror(code));
×
539
  }
540
}
1,173,607✔
541

542
int32_t createRequest(uint64_t connId, int32_t type, int64_t reqid, SRequestObj **pRequest) {
612,688✔
543
  int32_t code = TSDB_CODE_SUCCESS;
612,688✔
544
  *pRequest = (SRequestObj *)taosMemoryCalloc(1, sizeof(SRequestObj));
612,688!
545
  if (NULL == *pRequest) {
612,621!
546
    return terrno;
×
547
  }
548

549
  STscObj *pTscObj = acquireTscObj(connId);
612,621✔
550
  if (pTscObj == NULL) {
612,780!
551
    TSC_ERR_JRET(TSDB_CODE_TSC_DISCONNECTED);
×
552
  }
553
  SSyncQueryParam *interParam = taosMemoryCalloc(1, sizeof(SSyncQueryParam));
612,780!
554
  if (interParam == NULL) {
612,731!
555
    releaseTscObj(connId);
×
556
    TSC_ERR_JRET(terrno);
×
557
  }
558
  TSC_ERR_JRET(tsem_init(&interParam->sem, 0, 0));
612,731!
559
  interParam->pRequest = *pRequest;
612,753✔
560
  (*pRequest)->body.interParam = interParam;
612,753✔
561

562
  (*pRequest)->resType = RES_TYPE__QUERY;
612,753✔
563
  (*pRequest)->requestId = reqid == 0 ? generateRequestId() : reqid;
612,753!
564
  (*pRequest)->metric.start = taosGetTimestampUs();
612,743✔
565

566
  (*pRequest)->body.resInfo.convertUcs4 = true;  // convert ucs4 by default
612,755✔
567
  (*pRequest)->body.resInfo.charsetCxt = pTscObj->optionInfo.charsetCxt;
612,755✔
568
  (*pRequest)->type = type;
612,755✔
569
  (*pRequest)->allocatorRefId = -1;
612,755✔
570

571
  (*pRequest)->pDb = getDbOfConnection(pTscObj);
612,755✔
572
  if (NULL == (*pRequest)->pDb) {
612,713✔
573
    TSC_ERR_JRET(terrno);
111,697!
574
  }
575
  (*pRequest)->pTscObj = pTscObj;
612,713✔
576
  (*pRequest)->inCallback = false;
612,713✔
577
  (*pRequest)->msgBuf = taosMemoryCalloc(1, ERROR_MSG_BUF_DEFAULT_SIZE);
612,713!
578
  if (NULL == (*pRequest)->msgBuf) {
612,598!
579
    code = terrno;
×
580
    goto _return;
×
581
  }
582
  (*pRequest)->msgBufLen = ERROR_MSG_BUF_DEFAULT_SIZE;
612,598✔
583
  TSC_ERR_JRET(tsem_init(&(*pRequest)->body.rspSem, 0, 0));
612,598!
584
  TSC_ERR_JRET(registerRequest(*pRequest, pTscObj));
612,644✔
585

586
  return TSDB_CODE_SUCCESS;
612,772✔
587
_return:
×
588
  if ((*pRequest)->pTscObj) {
×
589
    doDestroyRequest(*pRequest);
×
590
  } else {
591
    taosMemoryFree(*pRequest);
×
592
  }
593
  return code;
×
594
}
595

596
void doFreeReqResultInfo(SReqResultInfo *pResInfo) {
615,644✔
597
  taosMemoryFreeClear(pResInfo->pRspMsg);
615,644!
598
  taosMemoryFreeClear(pResInfo->length);
615,566!
599
  taosMemoryFreeClear(pResInfo->row);
615,582!
600
  taosMemoryFreeClear(pResInfo->pCol);
615,562!
601
  taosMemoryFreeClear(pResInfo->fields);
615,562!
602
  taosMemoryFreeClear(pResInfo->userFields);
615,541!
603
  taosMemoryFreeClear(pResInfo->convertJson);
615,568!
604
  taosMemoryFreeClear(pResInfo->decompBuf);
615,568!
605

606
  if (pResInfo->convertBuf != NULL) {
615,568✔
607
    for (int32_t i = 0; i < pResInfo->numOfCols; ++i) {
184,109✔
608
      taosMemoryFreeClear(pResInfo->convertBuf[i]);
147,919!
609
    }
610
    taosMemoryFreeClear(pResInfo->convertBuf);
36,190!
611
  }
612
}
615,518✔
613

614
SRequestObj *acquireRequest(int64_t rid) { return (SRequestObj *)taosAcquireRef(clientReqRefPool, rid); }
2,691,942✔
615

616
int32_t releaseRequest(int64_t rid) { return taosReleaseRef(clientReqRefPool, rid); }
2,693,621✔
617

618
int32_t removeRequest(int64_t rid) { return taosRemoveRef(clientReqRefPool, rid); }
612,274✔
619

620
/// return the most previous req ref id
621
int64_t removeFromMostPrevReq(SRequestObj *pRequest) {
612,284✔
622
  int64_t      mostPrevReqRefId = pRequest->self;
612,284✔
623
  SRequestObj *pTmp = pRequest;
612,284✔
624
  while (pTmp->relation.prevRefId) {
612,284!
625
    pTmp = acquireRequest(pTmp->relation.prevRefId);
×
626
    if (pTmp) {
×
627
      mostPrevReqRefId = pTmp->self;
×
628
      (void)releaseRequest(mostPrevReqRefId);  // ignore error
×
629
    } else {
630
      break;
×
631
    }
632
  }
633
  (void)removeRequest(mostPrevReqRefId);  // ignore error
612,284✔
634
  return mostPrevReqRefId;
612,305✔
635
}
636

637
void destroyNextReq(int64_t nextRefId) {
612,240✔
638
  if (nextRefId) {
612,240!
639
    SRequestObj *pObj = acquireRequest(nextRefId);
×
640
    if (pObj) {
×
641
      (void)releaseRequest(nextRefId);  // ignore error
×
642
      (void)releaseRequest(nextRefId);  // ignore error
×
643
    }
644
  }
645
}
612,240✔
646

647
void destroySubRequests(SRequestObj *pRequest) {
×
648
  int32_t      reqIdx = -1;
×
649
  SRequestObj *pReqList[16] = {NULL};
×
650
  uint64_t     tmpRefId = 0;
×
651

652
  if (pRequest->relation.userRefId && pRequest->relation.userRefId != pRequest->self) {
×
653
    return;
×
654
  }
655

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

669
  for (int32_t i = reqIdx; i >= 0; i--) {
×
670
    (void)removeRequest(pReqList[i]->self);  // ignore error
×
671
  }
672

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

687
void doDestroyRequest(void *p) {
612,247✔
688
  if (NULL == p) {
612,247!
689
    return;
×
690
  }
691

692
  SRequestObj *pRequest = (SRequestObj *)p;
612,247✔
693

694
  uint64_t reqId = pRequest->requestId;
612,247✔
695
  tscTrace("QID:0x%" PRIx64 ", begin destroy request, res:%p", reqId, pRequest);
612,247!
696

697
  int64_t nextReqRefId = pRequest->relation.nextRefId;
612,247✔
698

699
  int32_t code = taosHashRemove(pRequest->pTscObj->pRequests, &pRequest->self, sizeof(pRequest->self));
612,247✔
700
  if (TSDB_CODE_SUCCESS != code) {
612,302✔
701
    tscDebug("failed to remove request from hash since %s", tstrerror(code));
9,088✔
702
  }
703
  schedulerFreeJob(&pRequest->body.queryJob, 0);
612,302✔
704

705
  destorySqlCallbackWrapper(pRequest->pWrapper);
612,292✔
706

707
  taosMemoryFreeClear(pRequest->msgBuf);
612,264!
708

709
  doFreeReqResultInfo(&pRequest->body.resInfo);
612,164✔
710
  if (TSDB_CODE_SUCCESS != tsem_destroy(&pRequest->body.rspSem)) {
612,182!
711
    tscError("failed to destroy semaphore");
×
712
  }
713

714
  taosArrayDestroy(pRequest->tableList);
612,235✔
715
  taosArrayDestroy(pRequest->targetTableList);
612,182✔
716
  destroyQueryExecRes(&pRequest->body.resInfo.execRes);
612,265✔
717

718
  if (pRequest->self) {
612,248!
719
    deregisterRequest(pRequest);
612,255✔
720
  }
721

722
  taosMemoryFreeClear(pRequest->pDb);
612,273!
723
  taosArrayDestroy(pRequest->dbList);
612,161✔
724
  if (pRequest->body.interParam) {
612,216!
725
    if (TSDB_CODE_SUCCESS != tsem_destroy(&((SSyncQueryParam *)pRequest->body.interParam)->sem)) {
612,265!
726
      tscError("failed to destroy semaphore in pRequest");
×
727
    }
728
  }
729
  taosMemoryFree(pRequest->body.interParam);
612,218!
730

731
  qDestroyQuery(pRequest->pQuery);
612,210✔
732
  nodesDestroyAllocator(pRequest->allocatorRefId);
612,240✔
733

734
  taosMemoryFreeClear(pRequest->effectiveUser);
612,282!
735
  taosMemoryFreeClear(pRequest->sqlstr);
612,282!
736
  taosMemoryFree(pRequest);
612,257!
737
  tscTrace("QID:0x%" PRIx64 ", end destroy request, res:%p", reqId, pRequest);
612,248!
738
  destroyNextReq(nextReqRefId);
612,248✔
739
}
740

741
void destroyRequest(SRequestObj *pRequest) {
612,302✔
742
  if (pRequest == NULL) return;
612,302!
743

744
  taos_stop_query(pRequest);
612,302✔
745
  (void)removeFromMostPrevReq(pRequest);
612,314✔
746
}
747

748
void taosStopQueryImpl(SRequestObj *pRequest) {
612,297✔
749
  pRequest->killed = true;
612,297✔
750

751
  // It is not a query, no need to stop.
752
  if (NULL == pRequest->pQuery || QUERY_EXEC_MODE_SCHEDULE != pRequest->pQuery->execMode) {
612,297✔
753
    tscDebug("QID:0x%" PRIx64 ", no need to be killed since not query", pRequest->requestId);
28,785✔
754
    return;
28,820✔
755
  }
756

757
  schedulerFreeJob(&pRequest->body.queryJob, TSDB_CODE_TSC_QUERY_KILLED);
583,512✔
758
  tscDebug("QID:0x%" PRIx64 ", killed", pRequest->requestId);
583,490✔
759
}
760

761
void stopAllQueries(SRequestObj *pRequest) {
612,294✔
762
  int32_t      reqIdx = -1;
612,294✔
763
  SRequestObj *pReqList[16] = {NULL};
612,294✔
764
  uint64_t     tmpRefId = 0;
612,294✔
765

766
  if (pRequest->relation.userRefId && pRequest->relation.userRefId != pRequest->self) {
612,294!
767
    return;
×
768
  }
769

770
  SRequestObj *pTmp = pRequest;
612,294✔
771
  while (pTmp->relation.prevRefId) {
612,294!
772
    tmpRefId = pTmp->relation.prevRefId;
×
773
    pTmp = acquireRequest(tmpRefId);
×
774
    if (pTmp) {
×
775
      pReqList[++reqIdx] = pTmp;
×
776
    } else {
777
      tscError("prev req ref 0x%" PRIx64 " is not there", tmpRefId);
×
778
      break;
×
779
    }
780
  }
781

782
  for (int32_t i = reqIdx; i >= 0; i--) {
612,304!
783
    taosStopQueryImpl(pReqList[i]);
×
784
    (void)releaseRequest(pReqList[i]->self);  // ignore error
×
785
  }
786

787
  taosStopQueryImpl(pRequest);
612,304✔
788

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

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

818
#ifdef WINDOWS
819
  if (taosCheckCurrentInDll()) {
820
    atexit(crashReportThreadFuncUnexpectedStopped);
821
  }
822
#endif
823

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

834
  code = initCrashLogWriter();
×
835
  if (code) {
×
836
    tscError("failed to init crash log writer, code:%s", tstrerror(code));
×
837
    return NULL;
×
838
  }
839

840
  while (1) {
841
    checkAndPrepareCrashInfo();
×
842
    if (clientStop > 0 && reportThreadSetQuit()) break;
×
843
    if (loopTimes++ < reportPeriodNum) {
×
844
      if (loopTimes < 0) loopTimes = reportPeriodNum;
×
845
      taosMsleep(sleepTime);
×
846
      continue;
×
847
    }
848

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

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

869
    taosMemoryFree(pMsg);
×
870

871
    if (pMsg && msgLen > 0) {
×
872
      pMsg = NULL;
×
873
      continue;
×
874
    }
875

876
    if (pFile) {
×
877
      taosReleaseCrashLogFile(pFile, truncateFile);
×
878
      pFile = NULL;
×
879
      truncateFile = false;
×
880
    }
881

882
    taosMsleep(sleepTime);
×
883
    loopTimes = 0;
×
884
  }
885
  taosTelemetryDestroy(&mgt);
×
886

887
  clientStop = -2;
×
888
  return NULL;
×
889
}
890

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

906
  (void)taosThreadAttrDestroy(&thAttr);
×
907
_return:
×
908
  if (code) {
×
909
    terrno = TAOS_SYSTEM_ERROR(ERRNO);
×
910
    TSC_ERR_RET(terrno);
×
911
  }
912

913
  return code;
×
914
}
915

916
void tscStopCrashReport() {
2,379✔
917
  if (!tsEnableCrashReport) {
2,379!
918
    return;
2,379✔
919
  }
920

921
  if (atomic_val_compare_exchange_32(&clientStop, 0, 1)) {
×
922
    tscDebug("crash report thread already stopped");
×
923
    return;
×
924
  }
925

926
  while (atomic_load_32(&clientStop) > 0) {
×
927
    taosMsleep(100);
×
928
  }
929
}
930

931
void taos_write_crashinfo(int signum, void *sigInfo, void *context) {
×
932
  writeCrashLogToFile(signum, sigInfo, CUS_PROMPT, lastClusterId, appInfo.startTime);
×
933
}
×
934
#endif
935

936
#ifdef TAOSD_INTEGRATED
937
typedef struct {
938
  TdThread pid;
939
  int32_t  stat;  // < 0: start failed, 0: init(not start), 1: start successfully
940
} SDaemonObj;
941

942
extern int  dmStartDaemon(int argc, char const *argv[]);
943
extern void dmStopDaemon();
944

945
SDaemonObj daemonObj = {0};
946

947
typedef struct {
948
  int32_t argc;
949
  char  **argv;
950
} SExecArgs;
951

952
static void *dmStartDaemonFunc(void *param) {
953
  int32_t    code = 0;
954
  SExecArgs *pArgs = (SExecArgs *)param;
955
  int32_t    argc = pArgs->argc;
956
  char     **argv = pArgs->argv;
957

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

964
_exit:
965
  if (code != 0) {
966
    atomic_store_32(&daemonObj.stat, code);
967
  }
968
  return NULL;
969
}
970

971
static int32_t shellStartDaemon(int argc, char *argv[]) {
972
  int32_t    code = 0, lino = 0;
973
  SExecArgs *pArgs = NULL;
974
  int64_t    startMs = taosGetTimestampMs(), endMs = startMs;
975

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

990
#ifndef TD_AS_LIB
991
  tsLogEmbedded = 1;
992
#endif
993

994
  TAOS_CHECK_EXIT(taosThreadCreate(&daemonObj.pid, &thAttr, dmStartDaemonFunc, pArgs));
995

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

1007
    if (daemonstat > 1) {
1008
      code = TSDB_CODE_APP_ERROR;
1009
      TAOS_CHECK_EXIT(code);
1010
    }
1011
    taosMsleep(1000);
1012
  }
1013

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

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

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

1055
  // In the APIs of other program language, taos_cleanup is not available yet.
1056
  // So, to make sure taos_cleanup will be invoked to clean up the allocated resource to suppress the valgrind warning.
1057
  (void)atexit(taos_cleanup);
2,379✔
1058
  SET_ERRNO(TSDB_CODE_SUCCESS);
2,379✔
1059
  terrno = TSDB_CODE_SUCCESS;
2,379✔
1060
  taosSeedRand(taosGetTimestampSec());
2,379✔
1061

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

1074
  const char *logName = CUS_PROMPT "log";
2,379✔
1075
  ENV_ERR_RET(taosInitLogOutput(&logName), "failed to init log output");
2,379!
1076
  if (taosCreateLog(logName, 10, configDir, NULL, NULL, NULL, NULL, 1) != 0) {
2,379✔
1077
    (void)printf(" WARING: Create %s failed:%s. configDir=%s\n", logName, strerror(ERRNO), configDir);
1✔
1078
    tscInitRes = terrno;
1✔
1079
    return;
1✔
1080
  }
1081

1082
#ifdef TAOSD_INTEGRATED
1083
  ENV_ERR_RET(taosInitCfg(configDir, NULL, NULL, NULL, NULL, 0), "failed to init cfg");
1084
#else
1085
  ENV_ERR_RET(taosInitCfg(configDir, NULL, NULL, NULL, NULL, 1), "failed to init cfg");
2,378!
1086
#endif
1087

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

1102
  if (InitRegexCache() != 0) {
2,378!
1103
    tscInitRes = terrno;
×
1104
    (void)printf("failed to init regex cache\n");
×
1105
    return;
×
1106
  }
1107

1108
  tscInfo("starting to initialize TAOS driver");
2,378!
1109

1110
  SCatalogCfg cfg = {.maxDBCacheNum = 100, .maxTblCacheNum = 100};
2,378✔
1111
  ENV_ERR_RET(catalogInit(&cfg), "failed to init catalog");
2,378!
1112
  ENV_ERR_RET(schedulerInit(), "failed to init scheduler");
2,378!
1113
  ENV_ERR_RET(initClientId(), "failed to init clientId");
2,378!
1114

1115
  ENV_ERR_RET(initTaskQueue(), "failed to init task queue");
2,378!
1116
  ENV_ERR_RET(fmFuncMgtInit(), "failed to init funcMgt");
2,378!
1117
  ENV_ERR_RET(nodesInitAllocatorSet(), "failed to init allocator set");
2,378!
1118

1119
  clientConnRefPool = taosOpenRef(200, destroyTscObj);
2,378✔
1120
  clientReqRefPool = taosOpenRef(40960, doDestroyRequest);
2,378✔
1121

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

1134
int taos_init() {
3,754✔
1135
  (void)taosThreadOnce(&tscinit, taos_init_imp);
3,754✔
1136
  return tscInitRes;
3,756✔
1137
}
1138

1139
const char *getCfgName(TSDB_OPTION option) {
531✔
1140
  const char *name = NULL;
531✔
1141

1142
  switch (option) {
531!
1143
    case TSDB_OPTION_SHELL_ACTIVITY_TIMER:
×
1144
      name = "shellActivityTimer";
×
1145
      break;
×
1146
    case TSDB_OPTION_LOCALE:
×
1147
      name = "locale";
×
1148
      break;
×
1149
    case TSDB_OPTION_CHARSET:
×
1150
      name = "charset";
×
1151
      break;
×
1152
    case TSDB_OPTION_TIMEZONE:
9✔
1153
      name = "timezone";
9✔
1154
      break;
9✔
1155
    case TSDB_OPTION_USE_ADAPTER:
×
1156
      name = "useAdapter";
×
1157
      break;
×
1158
    default:
522✔
1159
      break;
522✔
1160
  }
1161

1162
  return name;
531✔
1163
}
1164

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

1187
  // initialize global config
1188
  if (taos_init() != 0) {
531!
1189
    return -1;
×
1190
  }
1191

1192
  SConfig     *pCfg = taosGetCfg();
531✔
1193
  SConfigItem *pItem = NULL;
531✔
1194
  const char  *name = getCfgName(option);
531✔
1195

1196
  if (name == NULL) {
531✔
1197
    tscError("Invalid option %d", option);
522!
1198
    return -1;
522✔
1199
  }
1200

1201
  pItem = cfgGetItem(pCfg, name);
9✔
1202
  if (pItem == NULL) {
9!
1203
    tscError("Invalid option %d", option);
×
1204
    return -1;
×
1205
  }
1206

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

1217
  return code;
9✔
1218
}
1219

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

1233
  if (hashId == 0) {
674,461✔
1234
    int32_t code = taosGetSystemUUIDU32(&hashId);
2,375✔
1235
    if (code != TSDB_CODE_SUCCESS) {
2,375!
1236
      tscError("Failed to get the system uid to generated request id, reason:%s. use ip address instead",
×
1237
               tstrerror(code));
1238
    }
1239
  }
1240

1241
  uint64_t id = 0;
674,428✔
1242

1243
  while (true) {
×
1244
    int64_t  ts = taosGetTimestampMs();
674,437✔
1245
    uint64_t pid = taosGetPId();
674,437✔
1246
    uint32_t val = atomic_add_fetch_32(&requestSerialId, 1);
674,457✔
1247
    if (val >= 0xFFFF) atomic_store_32(&requestSerialId, 0);
674,484!
1248

1249
    id = (((uint64_t)(hashId & 0x0FFF)) << 52) | ((pid & 0x0FFF) << 40) | ((ts & 0xFFFFFF) << 16) | (val & 0xFFFF);
674,495✔
1250
    if (id) {
674,495!
1251
      break;
674,495✔
1252
    }
1253
  }
1254
  return id;
674,495✔
1255
}
1256

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

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

1282
  if(size >= 1000) {
1283
    ret.retCode = SET_CONF_RET_ERR_TOO_LONG;
1284
    tstrncpy(ret.retMsg, "json object size is too long", RET_MSG_LENGTH);
1285
    return ret;
1286
  }
1287

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

1314
setConfRet taos_set_config(const char *config){
1315
  taosThreadMutexLock(&setConfMutex);
1316
  setConfRet ret = taos_set_config_imp(config);
1317
  taosThreadMutexUnlock(&setConfMutex);
1318
  return ret;
1319
}
1320
#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