clientEnv.c 13.6 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 "os.h"
17
#include "catalog.h"
18
#include "functionMgt.h"
H
Haojun Liao 已提交
19 20
#include "clientInt.h"
#include "clientLog.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 28 29 30
#include "tref.h"
#include "trpc.h"
#include "ttime.h"

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

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

L
Liu Jicong 已提交
37 38
static TdThreadOnce tscinit = PTHREAD_ONCE_INIT;
volatile int32_t    tscInitRes = 0;
H
Haojun Liao 已提交
39

dengyihao's avatar
dengyihao 已提交
40
static void registerRequest(SRequestObj *pRequest) {
D
dapan1121 已提交
41
  STscObj *pTscObj = acquireTscObj(*(int64_t*)pRequest->pTscObj->id);
L
Liu Jicong 已提交
42

H
Haojun Liao 已提交
43 44 45
  assert(pTscObj != NULL);

  // connection has been released already, abort creating request.
H
Haojun Liao 已提交
46
  pRequest->self = taosAddRef(clientReqRefPool, pRequest);
H
Haojun Liao 已提交
47 48 49 50

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

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

L
Liu Jicong 已提交
53 54
    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 已提交
55 56
    tscDebug("0x%" PRIx64 " new Request from connObj:0x%" PRIx64
             ", current:%d, app current:%d, total:%d, reqId:0x%" PRIx64,
D
dapan1121 已提交
57
             pRequest->self, *(int64_t*)pRequest->pTscObj->id, num, currentInst, total, pRequest->requestId);
H
Haojun Liao 已提交
58 59 60
  }
}

dengyihao's avatar
dengyihao 已提交
61
static void deregisterRequest(SRequestObj *pRequest) {
H
Haojun Liao 已提交
62 63
  assert(pRequest != NULL);

L
Liu Jicong 已提交
64
  STscObj          *pTscObj = pRequest->pTscObj;
D
dapan1121 已提交
65
  SAppClusterSummary *pActivity = &pTscObj->pAppInfo->summary;
H
Haojun Liao 已提交
66

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

D
dapan1121 已提交
70
  int64_t duration = taosGetTimestampUs() - pRequest->metric.start;
dengyihao's avatar
dengyihao 已提交
71 72
  tscDebug("0x%" PRIx64 " free Request from connObj: 0x%" PRIx64 ", reqId:0x%" PRIx64 " elapsed:%" PRIu64
           " ms, current:%d, app current:%d",
D
dapan1121 已提交
73 74
           pRequest->self, *(int64_t*)pTscObj->id, pRequest->requestId, duration / 1000, num, currentInst);
  releaseTscObj(*(int64_t*)pTscObj->id);
H
Haojun Liao 已提交
75 76
}

77
// todo close the transporter properly
dengyihao's avatar
dengyihao 已提交
78
void closeTransporter(STscObj *pTscObj) {
H
Haojun Liao 已提交
79
  if (pTscObj == NULL || pTscObj->pAppInfo->pTransporter == NULL) {
H
Haojun Liao 已提交
80 81 82
    return;
  }

D
dapan1121 已提交
83
  tscDebug("free transporter:%p in connObj: 0x%" PRIx64, pTscObj->pAppInfo->pTransporter, *(int64_t*)pTscObj->id);
H
Haojun Liao 已提交
84
  rpcClose(pTscObj->pAppInfo->pTransporter);
H
Haojun Liao 已提交
85 86
}

D
dapan1121 已提交
87 88 89 90 91 92 93 94
static bool clientRpcRfp(int32_t code) {
  if (code == TSDB_CODE_RPC_REDIRECT) {
    return true;
  } else {
    return false;
  }
}

H
Haojun Liao 已提交
95
// TODO refactor
dengyihao's avatar
dengyihao 已提交
96
void *openTransporter(const char *user, const char *auth, int32_t numOfThread) {
H
Haojun Liao 已提交
97 98 99 100 101 102
  SRpcInit rpcInit;
  memset(&rpcInit, 0, sizeof(rpcInit));
  rpcInit.localPort = 0;
  rpcInit.label = "TSC";
  rpcInit.numOfThreads = numOfThread;
  rpcInit.cfp = processMsgFromServer;
D
dapan1121 已提交
103
  rpcInit.rfp = clientRpcRfp;
S
Shengliang Guan 已提交
104
  rpcInit.sessions = 1024;
H
Haojun Liao 已提交
105 106
  rpcInit.connType = TAOS_CONN_CLIENT;
  rpcInit.user = (char *)user;
S
Shengliang Guan 已提交
107
  rpcInit.idleTime = tsShellActivityTimer * 1000;
dengyihao's avatar
dengyihao 已提交
108
  void *pDnodeConn = rpcOpen(&rpcInit);
H
Haojun Liao 已提交
109 110 111 112 113 114 115 116
  if (pDnodeConn == NULL) {
    tscError("failed to init connection to server");
    return NULL;
  }

  return pDnodeConn;
}

D
dapan1121 已提交
117
void closeAllRequests(SHashObj *pRequests) {
L
Liu Jicong 已提交
118
  void *pIter = taosHashIterate(pRequests, NULL);
D
dapan1121 已提交
119 120 121
  while (pIter != NULL) {
    int64_t *rid = pIter;

L
Liu Jicong 已提交
122 123
    releaseRequest(*rid);

D
dapan1121 已提交
124 125 126 127
    pIter = taosHashIterate(pRequests, pIter);
  }
}

H
Haojun Liao 已提交
128 129 130
void destroyTscObj(void *pObj) {
  STscObj *pTscObj = pObj;

D
dapan1121 已提交
131
  SClientHbKey connKey = {.tscRid = *(int64_t*)pTscObj->id, .connType = pTscObj->connType};
132
  hbDeregisterConn(pTscObj->pAppInfo->pAppHbMgr, connKey);
D
dapan1121 已提交
133
  int64_t connNum = atomic_sub_fetch_64(&pTscObj->pAppInfo->numOfConns, 1);
D
dapan1121 已提交
134
  closeAllRequests(pTscObj->pRequests);
D
dapan1121 已提交
135 136
  schedulerStopQueryHb(pTscObj->pAppInfo->pTransporter);
  if (0 == connNum) {
D
dapan1121 已提交
137 138
    // TODO 
    //closeTransporter(pTscObj);
D
dapan1121 已提交
139
  }
D
dapan1121 已提交
140
  tscDebug("connObj 0x%" PRIx64 " destroyed, totalConn:%" PRId64, *(int64_t*)pTscObj->id, pTscObj->pAppInfo->numOfConns);
wafwerar's avatar
wafwerar 已提交
141
  taosThreadMutexDestroy(&pTscObj->mutex);
wafwerar's avatar
wafwerar 已提交
142
  taosMemoryFreeClear(pTscObj);
H
Haojun Liao 已提交
143 144
}

145
void *createTscObj(const char *user, const char *auth, const char *db, int32_t connType, SAppInstInfo *pAppInfo) {
wafwerar's avatar
wafwerar 已提交
146
  STscObj *pObj = (STscObj *)taosMemoryCalloc(1, sizeof(STscObj));
H
Haojun Liao 已提交
147 148 149 150 151
  if (NULL == pObj) {
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }

D
dapan1121 已提交
152 153 154 155 156 157
  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 已提交
158

159
  pObj->connType = connType;
H
Haojun Liao 已提交
160 161 162 163
  pObj->pAppInfo = pAppInfo;
  tstrncpy(pObj->user, user, sizeof(pObj->user));
  memcpy(pObj->pass, auth, TSDB_PASSWORD_LEN);

164 165 166 167
  if (db != NULL) {
    tstrncpy(pObj->db, db, tListLen(pObj->db));
  }

wafwerar's avatar
wafwerar 已提交
168
  taosThreadMutexInit(&pObj->mutex, NULL);
D
dapan1121 已提交
169 170
  pObj->id = taosMemoryMalloc(sizeof(int64_t));
  *(int64_t*)pObj->id = taosAddRef(clientConnRefPool, pObj);
171
  pObj->schemalessType = 1;
H
Haojun Liao 已提交
172

D
dapan1121 已提交
173
  tscDebug("connObj created, 0x%" PRIx64, *(int64_t*)pObj->id);
H
Haojun Liao 已提交
174 175 176
  return pObj;
}

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

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

181
void *createRequest(STscObj *pObj, int32_t type) {
H
Haojun Liao 已提交
182 183
  assert(pObj != NULL);

wafwerar's avatar
wafwerar 已提交
184
  SRequestObj *pRequest = (SRequestObj *)taosMemoryCalloc(1, sizeof(SRequestObj));
H
Haojun Liao 已提交
185 186 187 188 189
  if (NULL == pRequest) {
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }

L
Liu Jicong 已提交
190
  pRequest->resType = RES_TYPE__QUERY;
X
Xiaoyu Wang 已提交
191
  pRequest->pDb = getDbOfConnection(pObj);
dengyihao's avatar
dengyihao 已提交
192
  pRequest->requestId = generateRequestId();
D
dapan1121 已提交
193
  pRequest->metric.start = taosGetTimestampUs();
H
Haojun Liao 已提交
194

195 196
  pRequest->body.resInfo.convertUcs4 = true;  // convert ucs4 by default

dengyihao's avatar
dengyihao 已提交
197 198
  pRequest->type = type;
  pRequest->pTscObj = pObj;
wafwerar's avatar
wafwerar 已提交
199
  pRequest->msgBuf = taosMemoryCalloc(1, ERROR_MSG_BUF_DEFAULT_SIZE);
D
stmt  
dapan1121 已提交
200
  pRequest->msgBufLen = ERROR_MSG_BUF_DEFAULT_SIZE;
H
Haojun Liao 已提交
201 202 203
  tsem_init(&pRequest->body.rspSem, 0, 0);

  registerRequest(pRequest);
L
Liu Jicong 已提交
204

H
Haojun Liao 已提交
205 206 207
  return pRequest;
}

L
Liu Jicong 已提交
208
void doFreeReqResultInfo(SReqResultInfo *pResInfo) {
wafwerar's avatar
wafwerar 已提交
209 210 211 212 213
  taosMemoryFreeClear(pResInfo->pRspMsg);
  taosMemoryFreeClear(pResInfo->length);
  taosMemoryFreeClear(pResInfo->row);
  taosMemoryFreeClear(pResInfo->pCol);
  taosMemoryFreeClear(pResInfo->fields);
214
  taosMemoryFreeClear(pResInfo->userFields);
215 216 217

  if (pResInfo->convertBuf != NULL) {
    for (int32_t i = 0; i < pResInfo->numOfCols; ++i) {
wafwerar's avatar
wafwerar 已提交
218
      taosMemoryFreeClear(pResInfo->convertBuf[i]);
219
    }
wafwerar's avatar
wafwerar 已提交
220
    taosMemoryFreeClear(pResInfo->convertBuf);
221
  }
H
Haojun Liao 已提交
222 223
}

dengyihao's avatar
dengyihao 已提交
224
static void doDestroyRequest(void *p) {
H
Haojun Liao 已提交
225
  assert(p != NULL);
dengyihao's avatar
dengyihao 已提交
226
  SRequestObj *pRequest = (SRequestObj *)p;
H
Haojun Liao 已提交
227 228 229

  assert(RID_VALID(pRequest->self));

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

D
dapan1121 已提交
232
  if (pRequest->body.queryJob != 0) {
D
dapan1121 已提交
233
    schedulerFreeJob(pRequest->body.queryJob, 0);
D
dapan1121 已提交
234 235
  }

wafwerar's avatar
wafwerar 已提交
236 237 238
  taosMemoryFreeClear(pRequest->msgBuf);
  taosMemoryFreeClear(pRequest->sqlstr);
  taosMemoryFreeClear(pRequest->pDb);
H
Haojun Liao 已提交
239

H
Haojun Liao 已提交
240
  doFreeReqResultInfo(&pRequest->body.resInfo);
X
Xiaoyu Wang 已提交
241
  qDestroyQueryPlan(pRequest->body.pDag);
H
Haojun Liao 已提交
242

X
Xiaoyu Wang 已提交
243 244 245
  taosArrayDestroy(pRequest->tableList);
  taosArrayDestroy(pRequest->dbList);

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

H
Haojun Liao 已提交
248
  deregisterRequest(pRequest);
wafwerar's avatar
wafwerar 已提交
249
  taosMemoryFreeClear(pRequest);
H
Haojun Liao 已提交
250 251
}

dengyihao's avatar
dengyihao 已提交
252
void destroyRequest(SRequestObj *pRequest) {
H
Haojun Liao 已提交
253 254 255 256
  if (pRequest == NULL) {
    return;
  }

D
dapan1121 已提交
257
  taosRemoveRef(clientReqRefPool, pRequest->self);
H
Haojun Liao 已提交
258 259
}

L
Liu Jicong 已提交
260
SRequestObj *acquireRequest(int64_t rid) { return (SRequestObj *)taosAcquireRef(clientReqRefPool, rid); }
D
dapan1121 已提交
261

L
Liu Jicong 已提交
262
int32_t releaseRequest(int64_t rid) { return taosReleaseRef(clientReqRefPool, rid); }
D
dapan1121 已提交
263

H
Haojun Liao 已提交
264 265 266 267 268 269
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 已提交
270
  taosSeedRand(taosGetTimestampSec());
H
Haojun Liao 已提交
271 272

  deltaToUtcInitOnce();
S
Shengliang Guan 已提交
273

wafwerar's avatar
wafwerar 已提交
274
  if (taosCreateLog("taoslog", 10, configDir, NULL, NULL, NULL, NULL, 1) != 0) {
S
Shengliang Guan 已提交
275 276 277 278
    tscInitRes = -1;
    return;
  }

wafwerar's avatar
wafwerar 已提交
279
  if (taosInitCfg(configDir, NULL, NULL, NULL, NULL, 1) != 0) {
S
Shengliang Guan 已提交
280 281 282 283
    tscInitRes = -1;
    return;
  }

284
  initQueryModuleMsgHandle();
H
Haojun Liao 已提交
285

S
Shengliang Guan 已提交
286
  rpcInit();
H
Haojun Liao 已提交
287

D
dapan1121 已提交
288
  SCatalogCfg cfg = {.maxDBCacheNum = 100, .maxTblCacheNum = 100};
289 290
  catalogInit(&cfg);

X
Xiaoyu Wang 已提交
291 292
  SSchedulerCfg scfg = {.maxJobNum = 100};
  schedulerInit(&scfg);
S
Shengliang Guan 已提交
293
  tscDebug("starting to initialize TAOS driver");
H
Haojun Liao 已提交
294 295 296 297

  taosSetCoreDump(true);

  initTaskQueue();
298
  fmFuncMgtInit();
H
Haojun Liao 已提交
299

300
  clientConnRefPool = taosOpenRef(200, destroyTscObj);
dengyihao's avatar
dengyihao 已提交
301
  clientReqRefPool = taosOpenRef(40960, doDestroyRequest);
H
Haojun Liao 已提交
302

dengyihao's avatar
dengyihao 已提交
303
  // transDestroyBuffer(&conn->readBuf);
H
Haojun Liao 已提交
304
  taosGetAppName(appInfo.appName, NULL);
wafwerar's avatar
wafwerar 已提交
305
  taosThreadMutexInit(&appInfo.mutex, NULL);
H
Haojun Liao 已提交
306

dengyihao's avatar
dengyihao 已提交
307
  appInfo.pid = taosGetPId();
H
Haojun Liao 已提交
308
  appInfo.startTime = taosGetTimestampMs();
dengyihao's avatar
dengyihao 已提交
309
  appInfo.pInstMap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
H
Haojun Liao 已提交
310 311 312 313
  tscDebug("client is initialized successfully");
}

int taos_init() {
wafwerar's avatar
wafwerar 已提交
314
  taosThreadOnce(&tscinit, taos_init_imp);
H
Haojun Liao 已提交
315 316 317 318
  return tscInitRes;
}

int taos_options_imp(TSDB_OPTION option, const char *str) {
S
Shengliang Guan 已提交
319 320 321 322 323 324 325 326
  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;
  }

L
Liu Jicong 已提交
327
  SConfig     *pCfg = taosGetCfg();
S
Shengliang Guan 已提交
328
  SConfigItem *pItem = NULL;
H
Haojun Liao 已提交
329 330 331

  switch (option) {
    case TSDB_OPTION_CONFIGDIR:
S
Shengliang Guan 已提交
332
      pItem = cfgGetItem(pCfg, "configDir");
H
Haojun Liao 已提交
333 334
      break;
    case TSDB_OPTION_SHELL_ACTIVITY_TIMER:
S
Shengliang Guan 已提交
335
      pItem = cfgGetItem(pCfg, "shellActivityTimer");
H
Haojun Liao 已提交
336
      break;
S
Shengliang Guan 已提交
337 338
    case TSDB_OPTION_LOCALE:
      pItem = cfgGetItem(pCfg, "locale");
H
Haojun Liao 已提交
339
      break;
S
Shengliang Guan 已提交
340 341
    case TSDB_OPTION_CHARSET:
      pItem = cfgGetItem(pCfg, "charset");
H
Haojun Liao 已提交
342 343
      break;
    case TSDB_OPTION_TIMEZONE:
S
Shengliang Guan 已提交
344
      pItem = cfgGetItem(pCfg, "timezone");
H
Haojun Liao 已提交
345 346
      break;
    default:
S
Shengliang Guan 已提交
347 348 349 350 351 352 353 354 355 356 357 358 359
      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 已提交
360
  }
S
Shengliang Guan 已提交
361 362

  return code;
H
Haojun Liao 已提交
363 364
}

365 366 367 368 369
/**
 * The request id is an unsigned integer format of 64bit.
 *+------------+-----+-----------+---------------+
 *| uid|localIp| PId | timestamp | serial number |
 *+------------+-----+-----------+---------------+
370
 *| 12bit      |12bit|24bit      |16bit          |
371 372 373 374
 *+------------+-----+-----------+---------------+
 * @return
 */
uint64_t generateRequestId() {
H
Haojun Liao 已提交
375
  static uint64_t hashId = 0;
dengyihao's avatar
dengyihao 已提交
376
  static int32_t  requestSerialId = 0;
H
Haojun Liao 已提交
377 378 379 380 381 382 383 384 385 386 387

  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));
    }
388 389
  }

D
dapan1121 已提交
390
  uint64_t id = 0;
L
Liu Jicong 已提交
391

D
dapan1121 已提交
392 393 394 395
  while (true) {
    int64_t  ts = taosGetTimestampMs();
    uint64_t pid = taosGetPId();
    int32_t  val = atomic_add_fetch_32(&requestSerialId, 1);
396

D
dapan1121 已提交
397 398 399 400 401
    id = ((hashId & 0x0FFF) << 52) | ((pid & 0x0FFF) << 40) | ((ts & 0xFFFFFF) << 16) | (val & 0xFFFF);
    if (id) {
      break;
    }
  }
402 403 404
  return id;
}

H
Haojun Liao 已提交
405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462
#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 已提交
463
  taosThreadMutexLock(&setConfMutex);
H
Haojun Liao 已提交
464
  setConfRet ret = taos_set_config_imp(config);
wafwerar's avatar
wafwerar 已提交
465
  taosThreadMutexUnlock(&setConfMutex);
H
Haojun Liao 已提交
466 467
  return ret;
}
468
#endif