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

16
#include "catalog.h"
H
Haojun Liao 已提交
17 18
#include "clientInt.h"
#include "clientLog.h"
dengyihao's avatar
dengyihao 已提交
19 20
#include "functionMgt.h"
#include "os.h"
21
#include "query.h"
X
Xiaoyu Wang 已提交
22
#include "scheduler.h"
H
Haojun Liao 已提交
23 24
#include "tcache.h"
#include "tglobal.h"
dengyihao's avatar
dengyihao 已提交
25
#include "tmsg.h"
H
Haojun Liao 已提交
26 27
#include "tref.h"
#include "trpc.h"
dengyihao's avatar
dengyihao 已提交
28
#include "tsched.h"
H
Haojun Liao 已提交
29 30 31
#include "ttime.h"

#define TSC_VAR_NOT_RELEASE 1
32
#define TSC_VAR_RELEASED    0
H
Haojun Liao 已提交
33

dengyihao's avatar
dengyihao 已提交
34
SAppInfo appInfo;
L
Liu Jicong 已提交
35
int32_t  clientReqRefPool = -1;
dengyihao's avatar
dengyihao 已提交
36
int32_t  clientConnRefPool = -1;
H
Haojun Liao 已提交
37

dengyihao's avatar
dengyihao 已提交
38 39
int32_t timestampDeltaLimit = 900;  // s

L
Liu Jicong 已提交
40 41
static TdThreadOnce tscinit = PTHREAD_ONCE_INIT;
volatile int32_t    tscInitRes = 0;
H
Haojun Liao 已提交
42

dengyihao's avatar
dengyihao 已提交
43
static int32_t registerRequest(SRequestObj *pRequest, STscObj *pTscObj) {
H
Haojun Liao 已提交
44
  // connection has been released already, abort creating request.
H
Haojun Liao 已提交
45
  pRequest->self = taosAddRef(clientReqRefPool, pRequest);
H
Haojun Liao 已提交
46 47 48 49

  int32_t num = atomic_add_fetch_32(&pTscObj->numOfReqs, 1);

  if (pTscObj->pAppInfo) {
D
dapan1121 已提交
50
    SAppClusterSummary *pSummary = &pTscObj->pAppInfo->summary;
H
Haojun Liao 已提交
51

L
Liu Jicong 已提交
52 53
    int32_t total = atomic_add_fetch_64((int64_t *)&pSummary->totalRequests, 1);
    int32_t currentInst = atomic_add_fetch_64((int64_t *)&pSummary->currentRequests, 1);
dengyihao's avatar
dengyihao 已提交
54 55
    tscDebug("0x%" PRIx64 " new Request from connObj:0x%" PRIx64
             ", current:%d, app current:%d, total:%d, reqId:0x%" PRIx64,
D
dapan1121 已提交
56
             pRequest->self, pRequest->pTscObj->id, num, currentInst, total, pRequest->requestId);
H
Haojun Liao 已提交
57
  }
D
dapan1121 已提交
58 59

  return TSDB_CODE_SUCCESS;
H
Haojun Liao 已提交
60 61
}

dengyihao's avatar
dengyihao 已提交
62
static void deregisterRequest(SRequestObj *pRequest) {
D
dapan1121 已提交
63
  const static int64_t SLOW_QUERY_INTERVAL = 3000000L; // todo configurable
H
Haojun Liao 已提交
64 65
  assert(pRequest != NULL);

dengyihao's avatar
dengyihao 已提交
66
  STscObj            *pTscObj = pRequest->pTscObj;
D
dapan1121 已提交
67
  SAppClusterSummary *pActivity = &pTscObj->pAppInfo->summary;
H
Haojun Liao 已提交
68

L
Liu Jicong 已提交
69
  int32_t currentInst = atomic_sub_fetch_64((int64_t *)&pActivity->currentRequests, 1);
H
Haojun Liao 已提交
70 71
  int32_t num = atomic_sub_fetch_32(&pTscObj->numOfReqs, 1);

D
dapan1121 已提交
72
  int64_t duration = taosGetTimestampUs() - pRequest->metric.start;
dengyihao's avatar
dengyihao 已提交
73 74
  tscDebug("0x%" PRIx64 " free Request from connObj: 0x%" PRIx64 ", reqId:0x%" PRIx64 " elapsed:%" PRIu64
           " ms, current:%d, app current:%d",
D
dapan1121 已提交
75
           pRequest->self, pTscObj->id, pRequest->requestId, duration / 1000, num, currentInst);
D
dapan1121 已提交
76 77 78 79 80 81 82 83 84 85 86

  if (QUERY_NODE_VNODE_MODIF_STMT == pRequest->stmtType) {
    atomic_add_fetch_64((int64_t *)&pActivity->insertElapsedTime, duration);
  } else if (QUERY_NODE_SELECT_STMT == pRequest->stmtType) {
    atomic_add_fetch_64((int64_t *)&pActivity->queryElapsedTime, duration);           
  }
  
  if (duration >= SLOW_QUERY_INTERVAL) {
    atomic_add_fetch_64((int64_t *)&pActivity->numOfSlowQueries, 1);
  }
  
D
dapan1121 已提交
87
  releaseTscObj(pTscObj->id);
H
Haojun Liao 已提交
88 89
}

90
// todo close the transporter properly
D
dapan1121 已提交
91 92
void closeTransporter(SAppInstInfo *pAppInfo) {
  if (pAppInfo == NULL || pAppInfo->pTransporter == NULL) {
H
Haojun Liao 已提交
93 94 95
    return;
  }

D
dapan1121 已提交
96 97
  tscDebug("free transporter:%p in app inst %p", pAppInfo->pTransporter, pAppInfo);
  rpcClose(pAppInfo->pTransporter);
H
Haojun Liao 已提交
98 99
}

dengyihao's avatar
dengyihao 已提交
100
static bool clientRpcRfp(int32_t code, tmsg_t msgType) {
D
dapan1121 已提交
101
  if (NEED_REDIRECT_ERROR(code)) {
dengyihao's avatar
dengyihao 已提交
102
    if (msgType == TDMT_SCH_QUERY || msgType == TDMT_SCH_MERGE_QUERY || msgType == TDMT_SCH_FETCH ||
D
dapan1121 已提交
103
        msgType == TDMT_SCH_MERGE_FETCH || msgType == TDMT_SCH_QUERY_HEARTBEAT || msgType == TDMT_SCH_DROP_TASK) {
dengyihao's avatar
dengyihao 已提交
104 105
      return false;
    }
D
dapan1121 已提交
106 107 108 109 110 111
    return true;
  } else {
    return false;
  }
}

H
Haojun Liao 已提交
112
// TODO refactor
dengyihao's avatar
dengyihao 已提交
113
void *openTransporter(const char *user, const char *auth, int32_t numOfThread) {
H
Haojun Liao 已提交
114 115 116 117 118 119
  SRpcInit rpcInit;
  memset(&rpcInit, 0, sizeof(rpcInit));
  rpcInit.localPort = 0;
  rpcInit.label = "TSC";
  rpcInit.numOfThreads = numOfThread;
  rpcInit.cfp = processMsgFromServer;
D
dapan1121 已提交
120
  rpcInit.rfp = clientRpcRfp;
S
Shengliang Guan 已提交
121
  rpcInit.sessions = 1024;
H
Haojun Liao 已提交
122 123
  rpcInit.connType = TAOS_CONN_CLIENT;
  rpcInit.user = (char *)user;
S
Shengliang Guan 已提交
124
  rpcInit.idleTime = tsShellActivityTimer * 1000;
dengyihao's avatar
dengyihao 已提交
125
  void *pDnodeConn = rpcOpen(&rpcInit);
H
Haojun Liao 已提交
126 127 128 129 130 131 132 133
  if (pDnodeConn == NULL) {
    tscError("failed to init connection to server");
    return NULL;
  }

  return pDnodeConn;
}

D
dapan1121 已提交
134
void destroyAllRequests(SHashObj *pRequests) {
L
Liu Jicong 已提交
135
  void *pIter = taosHashIterate(pRequests, NULL);
D
dapan1121 已提交
136 137 138
  while (pIter != NULL) {
    int64_t *rid = pIter;

D
dapan1121 已提交
139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
    SRequestObj *pRequest = acquireRequest(*rid);
    if (pRequest) {
      destroyRequest(pRequest);
      releaseRequest(*rid);
    }

    pIter = taosHashIterate(pRequests, pIter);
  }
}

void stopAllRequests(SHashObj *pRequests) {
  void *pIter = taosHashIterate(pRequests, NULL);
  while (pIter != NULL) {
    int64_t *rid = pIter;

    SRequestObj *pRequest = acquireRequest(*rid);
    if (pRequest) {
      taos_stop_query(pRequest);
      releaseRequest(*rid);
    }
L
Liu Jicong 已提交
159

D
dapan1121 已提交
160 161 162 163
    pIter = taosHashIterate(pRequests, pIter);
  }
}

dengyihao's avatar
dengyihao 已提交
164
void destroyAppInst(SAppInstInfo *pAppInfo) {
D
dapan1121 已提交
165
  tscDebug("destroy app inst mgr %p", pAppInfo);
D
dapan1121 已提交
166 167

  taosThreadMutexLock(&appInfo.mutex);
dengyihao's avatar
dengyihao 已提交
168

D
dapan1121 已提交
169 170
  hbRemoveAppHbMrg(&pAppInfo->pAppHbMgr);
  taosHashRemove(appInfo.pInstMap, pAppInfo->instKey, strlen(pAppInfo->instKey));
D
dapan1121 已提交
171 172 173

  taosThreadMutexUnlock(&appInfo.mutex);

D
dapan1121 已提交
174 175
  taosMemoryFreeClear(pAppInfo->instKey);
  closeTransporter(pAppInfo);
dengyihao's avatar
dengyihao 已提交
176

D
dapan1121 已提交
177 178
  taosThreadMutexLock(&pAppInfo->qnodeMutex);
  taosArrayDestroy(pAppInfo->pQnodeList);
dengyihao's avatar
dengyihao 已提交
179
  taosThreadMutexUnlock(&pAppInfo->qnodeMutex);
D
dapan1121 已提交
180 181 182 183

  taosMemoryFree(pAppInfo);
}

H
Haojun Liao 已提交
184
void destroyTscObj(void *pObj) {
D
dapan1121 已提交
185 186 187
  if (NULL == pObj) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
188

H
Haojun Liao 已提交
189
  STscObj *pTscObj = pObj;
dengyihao's avatar
dengyihao 已提交
190
  int64_t  tscId = pTscObj->id;
D
dapan1121 已提交
191
  tscTrace("begin to destroy tscObj %" PRIx64 " p:%p", tscId, pTscObj);
H
Haojun Liao 已提交
192

D
dapan1121 已提交
193
  SClientHbKey connKey = {.tscRid = pTscObj->id, .connType = pTscObj->connType};
194
  hbDeregisterConn(pTscObj->pAppInfo->pAppHbMgr, connKey);
195

D
dapan1121 已提交
196
  destroyAllRequests(pTscObj->pRequests);
197
  taosHashCleanup(pTscObj->pRequests);
dengyihao's avatar
dengyihao 已提交
198

D
dapan1121 已提交
199
  schedulerStopQueryHb(pTscObj->pAppInfo->pTransporter);
dengyihao's avatar
dengyihao 已提交
200
  tscDebug("connObj 0x%" PRIx64 " p:%p destroyed, remain inst totalConn:%" PRId64, pTscObj->id, pTscObj,
D
dapan1121 已提交
201 202
           pTscObj->pAppInfo->numOfConns);

203
  int64_t connNum = atomic_sub_fetch_64(&pTscObj->pAppInfo->numOfConns, 1);
D
dapan1121 已提交
204
  if (0 == connNum) {
D
dapan1121 已提交
205
    destroyAppInst(pTscObj->pAppInfo);
D
dapan1121 已提交
206
  }
wafwerar's avatar
wafwerar 已提交
207
  taosThreadMutexDestroy(&pTscObj->mutex);
D
dapan1121 已提交
208
  taosMemoryFree(pTscObj);
D
dapan1121 已提交
209

D
dapan1121 已提交
210
  tscTrace("end to destroy tscObj %" PRIx64 " p:%p", tscId, pTscObj);
H
Haojun Liao 已提交
211 212
}

213
void *createTscObj(const char *user, const char *auth, const char *db, int32_t connType, SAppInstInfo *pAppInfo) {
wafwerar's avatar
wafwerar 已提交
214
  STscObj *pObj = (STscObj *)taosMemoryCalloc(1, sizeof(STscObj));
H
Haojun Liao 已提交
215 216 217 218 219
  if (NULL == pObj) {
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }

D
dapan1121 已提交
220 221 222 223 224 225
  pObj->pRequests = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK);
  if (NULL == pObj->pRequests) {
    taosMemoryFree(pObj);
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }
L
Liu Jicong 已提交
226

227
  pObj->connType = connType;
H
Haojun Liao 已提交
228 229 230 231
  pObj->pAppInfo = pAppInfo;
  tstrncpy(pObj->user, user, sizeof(pObj->user));
  memcpy(pObj->pass, auth, TSDB_PASSWORD_LEN);

232 233 234 235
  if (db != NULL) {
    tstrncpy(pObj->db, db, tListLen(pObj->db));
  }

wafwerar's avatar
wafwerar 已提交
236
  taosThreadMutexInit(&pObj->mutex, NULL);
D
dapan1121 已提交
237
  pObj->id = taosAddRef(clientConnRefPool, pObj);
238
  pObj->schemalessType = 1;
H
Haojun Liao 已提交
239

D
dapan1121 已提交
240 241
  atomic_add_fetch_64(&pObj->pAppInfo->numOfConns, 1);

D
dapan1121 已提交
242
  tscDebug("connObj created, 0x%" PRIx64 ",p:%p", pObj->id, pObj);
H
Haojun Liao 已提交
243 244 245
  return pObj;
}

L
Liu Jicong 已提交
246
STscObj *acquireTscObj(int64_t rid) { return (STscObj *)taosAcquireRef(clientConnRefPool, rid); }
D
dapan1121 已提交
247

L
Liu Jicong 已提交
248
int32_t releaseTscObj(int64_t rid) { return taosReleaseRef(clientConnRefPool, rid); }
D
dapan1121 已提交
249

250
void *createRequest(uint64_t connId, int32_t type) {
wafwerar's avatar
wafwerar 已提交
251
  SRequestObj *pRequest = (SRequestObj *)taosMemoryCalloc(1, sizeof(SRequestObj));
H
Haojun Liao 已提交
252 253 254 255 256
  if (NULL == pRequest) {
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }

dengyihao's avatar
dengyihao 已提交
257
  STscObj *pTscObj = acquireTscObj(connId);
258
  if (pTscObj == NULL) {
259
    taosMemoryFree(pRequest);
260 261 262 263
    terrno = TSDB_CODE_TSC_DISCONNECTED;
    return NULL;
  }

L
Liu Jicong 已提交
264
  pRequest->resType = RES_TYPE__QUERY;
dengyihao's avatar
dengyihao 已提交
265
  pRequest->requestId = generateRequestId();
D
dapan1121 已提交
266
  pRequest->metric.start = taosGetTimestampUs();
H
Haojun Liao 已提交
267

268
  pRequest->body.resInfo.convertUcs4 = true;  // convert ucs4 by default
dengyihao's avatar
dengyihao 已提交
269
  pRequest->type = type;
270 271 272 273

  pRequest->pDb = getDbOfConnection(pTscObj);
  pRequest->pTscObj = pTscObj;

wafwerar's avatar
wafwerar 已提交
274
  pRequest->msgBuf = taosMemoryCalloc(1, ERROR_MSG_BUF_DEFAULT_SIZE);
D
stmt  
dapan1121 已提交
275
  pRequest->msgBufLen = ERROR_MSG_BUF_DEFAULT_SIZE;
H
Haojun Liao 已提交
276 277
  tsem_init(&pRequest->body.rspSem, 0, 0);

278
  if (registerRequest(pRequest, pTscObj)) {
D
dapan1121 已提交
279 280 281
    doDestroyRequest(pRequest);
    return NULL;
  }
L
Liu Jicong 已提交
282

H
Haojun Liao 已提交
283 284 285
  return pRequest;
}

L
Liu Jicong 已提交
286
void doFreeReqResultInfo(SReqResultInfo *pResInfo) {
wafwerar's avatar
wafwerar 已提交
287 288 289 290 291
  taosMemoryFreeClear(pResInfo->pRspMsg);
  taosMemoryFreeClear(pResInfo->length);
  taosMemoryFreeClear(pResInfo->row);
  taosMemoryFreeClear(pResInfo->pCol);
  taosMemoryFreeClear(pResInfo->fields);
292
  taosMemoryFreeClear(pResInfo->userFields);
wmmhello's avatar
wmmhello 已提交
293
  taosMemoryFreeClear(pResInfo->convertJson);
294 295 296

  if (pResInfo->convertBuf != NULL) {
    for (int32_t i = 0; i < pResInfo->numOfCols; ++i) {
wafwerar's avatar
wafwerar 已提交
297
      taosMemoryFreeClear(pResInfo->convertBuf[i]);
298
    }
wafwerar's avatar
wafwerar 已提交
299
    taosMemoryFreeClear(pResInfo->convertBuf);
300
  }
H
Haojun Liao 已提交
301 302
}

D
dapan1121 已提交
303 304 305 306 307 308
SRequestObj *acquireRequest(int64_t rid) { return (SRequestObj *)taosAcquireRef(clientReqRefPool, rid); }

int32_t releaseRequest(int64_t rid) { return taosReleaseRef(clientReqRefPool, rid); }

int32_t removeRequest(int64_t rid) { return taosRemoveRef(clientReqRefPool, rid); }

D
dapan1121 已提交
309
void doDestroyRequest(void *p) {
D
dapan1121 已提交
310 311 312
  if (NULL == p) {
    return;
  }
dengyihao's avatar
dengyihao 已提交
313

dengyihao's avatar
dengyihao 已提交
314
  SRequestObj *pRequest = (SRequestObj *)p;
dengyihao's avatar
dengyihao 已提交
315

D
dapan1121 已提交
316
  int64_t reqId = pRequest->self;
D
dapan1121 已提交
317
  tscTrace("begin to destroy request %" PRIx64 " p:%p", reqId, pRequest);
dengyihao's avatar
dengyihao 已提交
318

D
dapan1121 已提交
319
  taosHashRemove(pRequest->pTscObj->pRequests, &pRequest->self, sizeof(pRequest->self));
L
Liu Jicong 已提交
320

D
dapan1121 已提交
321
  schedulerFreeJob(&pRequest->body.queryJob, 0);
D
dapan1121 已提交
322

wafwerar's avatar
wafwerar 已提交
323 324 325
  taosMemoryFreeClear(pRequest->msgBuf);
  taosMemoryFreeClear(pRequest->sqlstr);
  taosMemoryFreeClear(pRequest->pDb);
H
Haojun Liao 已提交
326

H
Haojun Liao 已提交
327
  doFreeReqResultInfo(&pRequest->body.resInfo);
H
Haojun Liao 已提交
328

X
Xiaoyu Wang 已提交
329 330
  taosArrayDestroy(pRequest->tableList);
  taosArrayDestroy(pRequest->dbList);
D
dapan1121 已提交
331
  taosArrayDestroy(pRequest->targetTableList);
X
Xiaoyu Wang 已提交
332

D
dapan1121 已提交
333
  destroyQueryExecRes(&pRequest->body.resInfo.execRes);
D
dapan1121 已提交
334

D
dapan1121 已提交
335 336 337
  if (pRequest->self) {
    deregisterRequest(pRequest);
  }
D
dapan1121 已提交
338

H
Haojun Liao 已提交
339 340 341 342
  if (pRequest->syncQuery) {
    taosMemoryFree(pRequest->body.param);
  }

343
  taosMemoryFree(pRequest);
D
dapan1121 已提交
344
  tscTrace("end to destroy request %" PRIx64 " p:%p", reqId, pRequest);
H
Haojun Liao 已提交
345 346
}

dengyihao's avatar
dengyihao 已提交
347
void destroyRequest(SRequestObj *pRequest) {
H
Haojun Liao 已提交
348 349 350 351
  if (pRequest == NULL) {
    return;
  }

D
dapan1121 已提交
352
  taos_stop_query(pRequest);
D
dapan1121 已提交
353
  removeRequest(pRequest->self);
H
Haojun Liao 已提交
354 355 356 357 358 359 360
}

void taos_init_imp(void) {
  // In the APIs of other program language, taos_cleanup is not available yet.
  // So, to make sure taos_cleanup will be invoked to clean up the allocated resource to suppress the valgrind warning.
  atexit(taos_cleanup);
  errno = TSDB_CODE_SUCCESS;
wafwerar's avatar
wafwerar 已提交
361
  taosSeedRand(taosGetTimestampSec());
H
Haojun Liao 已提交
362 363

  deltaToUtcInitOnce();
S
Shengliang Guan 已提交
364

wafwerar's avatar
wafwerar 已提交
365
  if (taosCreateLog("taoslog", 10, configDir, NULL, NULL, NULL, NULL, 1) != 0) {
S
Shengliang Guan 已提交
366 367 368 369
    tscInitRes = -1;
    return;
  }

wafwerar's avatar
wafwerar 已提交
370
  if (taosInitCfg(configDir, NULL, NULL, NULL, NULL, 1) != 0) {
S
Shengliang Guan 已提交
371 372 373 374
    tscInitRes = -1;
    return;
  }

375
  initQueryModuleMsgHandle();
H
Haojun Liao 已提交
376

D
dapan1121 已提交
377 378
  taosConvInit();
  
S
Shengliang Guan 已提交
379
  rpcInit();
H
Haojun Liao 已提交
380

D
dapan1121 已提交
381
  SCatalogCfg cfg = {.maxDBCacheNum = 100, .maxTblCacheNum = 100};
382 383
  catalogInit(&cfg);

D
dapan1121 已提交
384
  schedulerInit();
S
Shengliang Guan 已提交
385
  tscDebug("starting to initialize TAOS driver");
H
Haojun Liao 已提交
386 387 388 389

  taosSetCoreDump(true);

  initTaskQueue();
390
  fmFuncMgtInit();
H
Haojun Liao 已提交
391

392
  clientConnRefPool = taosOpenRef(200, destroyTscObj);
dengyihao's avatar
dengyihao 已提交
393
  clientReqRefPool = taosOpenRef(40960, doDestroyRequest);
H
Haojun Liao 已提交
394

dengyihao's avatar
dengyihao 已提交
395
  // transDestroyBuffer(&conn->readBuf);
H
Haojun Liao 已提交
396
  taosGetAppName(appInfo.appName, NULL);
wafwerar's avatar
wafwerar 已提交
397
  taosThreadMutexInit(&appInfo.mutex, NULL);
H
Haojun Liao 已提交
398

dengyihao's avatar
dengyihao 已提交
399
  appInfo.pid = taosGetPId();
H
Haojun Liao 已提交
400
  appInfo.startTime = taosGetTimestampMs();
dengyihao's avatar
dengyihao 已提交
401
  appInfo.pInstMap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
H
Haojun Liao 已提交
402 403 404 405
  tscDebug("client is initialized successfully");
}

int taos_init() {
wafwerar's avatar
wafwerar 已提交
406
  taosThreadOnce(&tscinit, taos_init_imp);
H
Haojun Liao 已提交
407 408 409 410
  return tscInitRes;
}

int taos_options_imp(TSDB_OPTION option, const char *str) {
S
Shengliang Guan 已提交
411 412 413 414 415 416 417 418
  if (option != TSDB_OPTION_CONFIGDIR) {
    taos_init();  // initialize global config
  } else {
    tstrncpy(configDir, str, PATH_MAX);
    tscInfo("set cfg:%s to %s", configDir, str);
    return 0;
  }

dengyihao's avatar
dengyihao 已提交
419
  SConfig     *pCfg = taosGetCfg();
S
Shengliang Guan 已提交
420
  SConfigItem *pItem = NULL;
H
Haojun Liao 已提交
421 422 423

  switch (option) {
    case TSDB_OPTION_CONFIGDIR:
S
Shengliang Guan 已提交
424
      pItem = cfgGetItem(pCfg, "configDir");
H
Haojun Liao 已提交
425 426
      break;
    case TSDB_OPTION_SHELL_ACTIVITY_TIMER:
S
Shengliang Guan 已提交
427
      pItem = cfgGetItem(pCfg, "shellActivityTimer");
H
Haojun Liao 已提交
428
      break;
S
Shengliang Guan 已提交
429 430
    case TSDB_OPTION_LOCALE:
      pItem = cfgGetItem(pCfg, "locale");
H
Haojun Liao 已提交
431
      break;
S
Shengliang Guan 已提交
432 433
    case TSDB_OPTION_CHARSET:
      pItem = cfgGetItem(pCfg, "charset");
H
Haojun Liao 已提交
434 435
      break;
    case TSDB_OPTION_TIMEZONE:
S
Shengliang Guan 已提交
436
      pItem = cfgGetItem(pCfg, "timezone");
H
Haojun Liao 已提交
437 438
      break;
    default:
S
Shengliang Guan 已提交
439 440 441 442 443 444 445 446 447 448 449 450 451
      break;
  }

  if (pItem == NULL) {
    tscError("Invalid option %d", option);
    return -1;
  }

  int code = cfgSetItem(pCfg, pItem->name, str, CFG_STYPE_TAOS_OPTIONS);
  if (code != 0) {
    tscError("failed to set cfg:%s to %s since %s", pItem->name, str, terrstr());
  } else {
    tscInfo("set cfg:%s to %s", pItem->name, str);
H
Haojun Liao 已提交
452
  }
S
Shengliang Guan 已提交
453 454

  return code;
H
Haojun Liao 已提交
455 456
}

457 458 459 460 461
/**
 * The request id is an unsigned integer format of 64bit.
 *+------------+-----+-----------+---------------+
 *| uid|localIp| PId | timestamp | serial number |
 *+------------+-----+-----------+---------------+
462
 *| 12bit      |12bit|24bit      |16bit          |
463 464 465 466
 *+------------+-----+-----------+---------------+
 * @return
 */
uint64_t generateRequestId() {
H
Haojun Liao 已提交
467
  static uint64_t hashId = 0;
dengyihao's avatar
dengyihao 已提交
468
  static int32_t  requestSerialId = 0;
H
Haojun Liao 已提交
469 470 471 472 473 474 475 476 477 478 479

  if (hashId == 0) {
    char    uid[64] = {0};
    int32_t code = taosGetSystemUUID(uid, tListLen(uid));
    if (code != TSDB_CODE_SUCCESS) {
      tscError("Failed to get the system uid to generated request id, reason:%s. use ip address instead",
               tstrerror(TAOS_SYSTEM_ERROR(errno)));

    } else {
      hashId = MurmurHash3_32(uid, strlen(uid));
    }
480 481
  }

D
dapan1121 已提交
482
  uint64_t id = 0;
L
Liu Jicong 已提交
483

D
dapan1121 已提交
484 485 486 487
  while (true) {
    int64_t  ts = taosGetTimestampMs();
    uint64_t pid = taosGetPId();
    int32_t  val = atomic_add_fetch_32(&requestSerialId, 1);
488

D
dapan1121 已提交
489 490 491 492 493
    id = ((hashId & 0x0FFF) << 52) | ((pid & 0x0FFF) << 40) | ((ts & 0xFFFFFF) << 16) | (val & 0xFFFF);
    if (id) {
      break;
    }
  }
494 495 496
  return id;
}

H
Haojun Liao 已提交
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554
#if 0
#include "cJSON.h"
static setConfRet taos_set_config_imp(const char *config){
  setConfRet ret = {SET_CONF_RET_SUCC, {0}};
  static bool setConfFlag = false;
  if (setConfFlag) {
    ret.retCode = SET_CONF_RET_ERR_ONLY_ONCE;
    strcpy(ret.retMsg, "configuration can only set once");
    return ret;
  }
  taosInitGlobalCfg();
  cJSON *root = cJSON_Parse(config);
  if (root == NULL){
    ret.retCode = SET_CONF_RET_ERR_JSON_PARSE;
    strcpy(ret.retMsg, "parse json error");
    return ret;
  }

  int size = cJSON_GetArraySize(root);
  if(!cJSON_IsObject(root) || size == 0) {
    ret.retCode = SET_CONF_RET_ERR_JSON_INVALID;
    strcpy(ret.retMsg, "json content is invalid, must be not empty object");
    return ret;
  }

  if(size >= 1000) {
    ret.retCode = SET_CONF_RET_ERR_TOO_LONG;
    strcpy(ret.retMsg, "json object size is too long");
    return ret;
  }

  for(int i = 0; i < size; i++){
    cJSON *item = cJSON_GetArrayItem(root, i);
    if(!item) {
      ret.retCode = SET_CONF_RET_ERR_INNER;
      strcpy(ret.retMsg, "inner error");
      return ret;
    }
    if(!taosReadConfigOption(item->string, item->valuestring, NULL, NULL, TAOS_CFG_CSTATUS_OPTION, TSDB_CFG_CTYPE_B_CLIENT)){
      ret.retCode = SET_CONF_RET_ERR_PART;
      if (strlen(ret.retMsg) == 0){
        snprintf(ret.retMsg, RET_MSG_LENGTH, "part error|%s", item->string);
      }else{
        int tmp = RET_MSG_LENGTH - 1 - (int)strlen(ret.retMsg);
        size_t leftSize = tmp >= 0 ? tmp : 0;
        strncat(ret.retMsg, "|",  leftSize);
        tmp = RET_MSG_LENGTH - 1 - (int)strlen(ret.retMsg);
        leftSize = tmp >= 0 ? tmp : 0;
        strncat(ret.retMsg, item->string, leftSize);
      }
    }
  }
  cJSON_Delete(root);
  setConfFlag = true;
  return ret;
}

setConfRet taos_set_config(const char *config){
wafwerar's avatar
wafwerar 已提交
555
  taosThreadMutexLock(&setConfMutex);
H
Haojun Liao 已提交
556
  setConfRet ret = taos_set_config_imp(config);
wafwerar's avatar
wafwerar 已提交
557
  taosThreadMutexUnlock(&setConfMutex);
H
Haojun Liao 已提交
558 559
  return ret;
}
560
#endif