clientEnv.c 15.8 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
#include "os.h"
20
#include "query.h"
X
Xiaoyu Wang 已提交
21
#include "scheduler.h"
H
Haojun Liao 已提交
22 23
#include "tcache.h"
#include "tglobal.h"
dengyihao's avatar
dengyihao 已提交
24
#include "tmsg.h"
H
Haojun Liao 已提交
25 26 27 28 29
#include "tref.h"
#include "trpc.h"
#include "ttime.h"

#define TSC_VAR_NOT_RELEASE 1
dengyihao's avatar
dengyihao 已提交
30
#define TSC_VAR_RELEASED 0
H
Haojun Liao 已提交
31

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

static pthread_once_t tscinit = PTHREAD_ONCE_INIT;
dengyihao's avatar
dengyihao 已提交
37
volatile int32_t      tscInitRes = 0;
H
Haojun Liao 已提交
38

dengyihao's avatar
dengyihao 已提交
39
static void registerRequest(SRequestObj *pRequest) {
40
  STscObj *pTscObj = (STscObj *)taosAcquireRef(clientConnRefPool, pRequest->pTscObj->id);
H
Haojun Liao 已提交
41 42 43
  assert(pTscObj != NULL);

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

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

  if (pTscObj->pAppInfo) {
49
    SInstanceSummary *pSummary = &pTscObj->pAppInfo->summary;
H
Haojun Liao 已提交
50

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

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

dengyihao's avatar
dengyihao 已提交
62 63
  STscObj *         pTscObj = pRequest->pTscObj;
  SInstanceSummary *pActivity = &pTscObj->pAppInfo->summary;
H
Haojun Liao 已提交
64 65 66 67

  int32_t currentInst = atomic_sub_fetch_32(&pActivity->currentRequests, 1);
  int32_t num = atomic_sub_fetch_32(&pTscObj->numOfReqs, 1);

H
Haojun Liao 已提交
68
  int64_t duration = taosGetTimestampMs() - pRequest->metric.start;
dengyihao's avatar
dengyihao 已提交
69 70 71
  tscDebug("0x%" PRIx64 " free Request from connObj: 0x%" PRIx64 ", reqId:0x%" PRIx64 " elapsed:%" PRIu64
           " ms, current:%d, app current:%d",
           pRequest->self, pTscObj->id, pRequest->requestId, duration, num, currentInst);
72
  taosReleaseRef(clientConnRefPool, pTscObj->id);
H
Haojun Liao 已提交
73 74 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;
  }

dengyihao's avatar
dengyihao 已提交
83
  tscDebug("free transporter:%p in connObj: 0x%" PRIx64, pTscObj->pAppInfo->pTransporter, pTscObj->id);
H
Haojun Liao 已提交
84
  rpcClose(pTscObj->pAppInfo->pTransporter);
H
Haojun Liao 已提交
85 86 87
}

// TODO refactor
dengyihao's avatar
dengyihao 已提交
88
void *openTransporter(const char *user, const char *auth, int32_t numOfThread) {
H
Haojun Liao 已提交
89 90 91 92 93 94
  SRpcInit rpcInit;
  memset(&rpcInit, 0, sizeof(rpcInit));
  rpcInit.localPort = 0;
  rpcInit.label = "TSC";
  rpcInit.numOfThreads = numOfThread;
  rpcInit.cfp = processMsgFromServer;
dengyihao's avatar
dengyihao 已提交
95
  rpcInit.pfp = persistConnForSpecificMsg;
S
Shengliang Guan 已提交
96
  rpcInit.sessions = tsMaxConnections;
H
Haojun Liao 已提交
97 98
  rpcInit.connType = TAOS_CONN_CLIENT;
  rpcInit.user = (char *)user;
S
Shengliang Guan 已提交
99
  rpcInit.idleTime = tsShellActivityTimer * 1000;
H
Haojun Liao 已提交
100
  rpcInit.ckey = "key";
S
Shengliang Guan 已提交
101
  rpcInit.spi = 1;
H
Haojun Liao 已提交
102 103
  rpcInit.secret = (char *)auth;

dengyihao's avatar
dengyihao 已提交
104
  void *pDnodeConn = rpcOpen(&rpcInit);
H
Haojun Liao 已提交
105 106 107 108 109 110 111 112 113 114 115
  if (pDnodeConn == NULL) {
    tscError("failed to init connection to server");
    return NULL;
  }

  return pDnodeConn;
}

void destroyTscObj(void *pObj) {
  STscObj *pTscObj = pObj;

116 117
  SClientHbKey connKey = {.connId = pTscObj->connId, .hbType = pTscObj->connType};
  hbDeregisterConn(pTscObj->pAppInfo->pAppHbMgr, connKey);
H
Haojun Liao 已提交
118
  atomic_sub_fetch_64(&pTscObj->pAppInfo->numOfConns, 1);
dengyihao's avatar
dengyihao 已提交
119
  tscDebug("connObj 0x%" PRIx64 " destroyed, totalConn:%" PRId64, pTscObj->id, pTscObj->pAppInfo->numOfConns);
H
Haojun Liao 已提交
120 121 122 123
  pthread_mutex_destroy(&pTscObj->mutex);
  tfree(pTscObj);
}

dengyihao's avatar
dengyihao 已提交
124
void *createTscObj(const char *user, const char *auth, const char *db, SAppInstInfo *pAppInfo) {
H
Haojun Liao 已提交
125 126 127 128 129 130 131 132 133 134
  STscObj *pObj = (STscObj *)calloc(1, sizeof(STscObj));
  if (NULL == pObj) {
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }

  pObj->pAppInfo = pAppInfo;
  tstrncpy(pObj->user, user, sizeof(pObj->user));
  memcpy(pObj->pass, auth, TSDB_PASSWORD_LEN);

135 136 137 138
  if (db != NULL) {
    tstrncpy(pObj->db, db, tListLen(pObj->db));
  }

H
Haojun Liao 已提交
139
  pthread_mutex_init(&pObj->mutex, NULL);
140
  pObj->id = taosAddRef(clientConnRefPool, pObj);
H
Haojun Liao 已提交
141

dengyihao's avatar
dengyihao 已提交
142
  tscDebug("connObj created, 0x%" PRIx64, pObj->id);
H
Haojun Liao 已提交
143 144 145
  return pObj;
}

dengyihao's avatar
dengyihao 已提交
146
void *createRequest(STscObj *pObj, __taos_async_fn_t fp, void *param, int32_t type) {
H
Haojun Liao 已提交
147 148 149 150 151 152 153 154
  assert(pObj != NULL);

  SRequestObj *pRequest = (SRequestObj *)calloc(1, sizeof(SRequestObj));
  if (NULL == pRequest) {
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }

dengyihao's avatar
dengyihao 已提交
155
  pRequest->requestId = generateRequestId();
H
Haojun Liao 已提交
156 157
  pRequest->metric.start = taosGetTimestampMs();

dengyihao's avatar
dengyihao 已提交
158 159 160 161
  pRequest->type = type;
  pRequest->pTscObj = pObj;
  pRequest->body.fp = fp;  // not used it yet
  pRequest->msgBuf = calloc(1, ERROR_MSG_BUF_DEFAULT_SIZE);
H
Haojun Liao 已提交
162 163 164 165 166 167
  tsem_init(&pRequest->body.rspSem, 0, 0);

  registerRequest(pRequest);
  return pRequest;
}

dengyihao's avatar
dengyihao 已提交
168
static void doFreeReqResultInfo(SReqResultInfo *pResInfo) {
H
Haojun Liao 已提交
169 170 171 172 173 174 175
  tfree(pResInfo->pRspMsg);
  tfree(pResInfo->length);
  tfree(pResInfo->row);
  tfree(pResInfo->pCol);
  tfree(pResInfo->fields);
}

dengyihao's avatar
dengyihao 已提交
176
static void doDestroyRequest(void *p) {
H
Haojun Liao 已提交
177
  assert(p != NULL);
dengyihao's avatar
dengyihao 已提交
178
  SRequestObj *pRequest = (SRequestObj *)p;
H
Haojun Liao 已提交
179 180 181 182 183 184 185

  assert(RID_VALID(pRequest->self));

  tfree(pRequest->msgBuf);
  tfree(pRequest->sqlstr);
  tfree(pRequest->pInfo);

H
Haojun Liao 已提交
186
  doFreeReqResultInfo(&pRequest->body.resInfo);
H
Haojun Liao 已提交
187
  qDestroyQueryDag(pRequest->body.pDag);
H
Haojun Liao 已提交
188

H
Haojun Liao 已提交
189 190 191 192
  if (pRequest->body.showInfo.pArray != NULL) {
    taosArrayDestroy(pRequest->body.showInfo.pArray);
  }

H
Haojun Liao 已提交
193 194 195 196
  deregisterRequest(pRequest);
  tfree(pRequest);
}

dengyihao's avatar
dengyihao 已提交
197
void destroyRequest(SRequestObj *pRequest) {
H
Haojun Liao 已提交
198 199 200 201
  if (pRequest == NULL) {
    return;
  }

H
Haojun Liao 已提交
202
  taosReleaseRef(clientReqRefPool, pRequest->self);
H
Haojun Liao 已提交
203 204 205 206 207 208 209 210 211 212 213
}

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;
  srand(taosGetTimestampSec());

  deltaToUtcInitOnce();
S
Shengliang Guan 已提交
214

S
Shengliang Guan 已提交
215
  if (taosCreateLog("taoslog", 10, configDir, NULL, NULL, NULL, 1) != 0) {
S
Shengliang Guan 已提交
216 217 218 219
    tscInitRes = -1;
    return;
  }

S
Shengliang Guan 已提交
220
  if (taosInitCfg(configDir, NULL, NULL, NULL, 1) != 0) {
S
Shengliang Guan 已提交
221 222 223 224
    tscInitRes = -1;
    return;
  }

H
Haojun Liao 已提交
225
  initMsgHandleFp();
226
  initQueryModuleMsgHandle();
H
Haojun Liao 已提交
227

S
Shengliang Guan 已提交
228
  rpcInit();
H
Haojun Liao 已提交
229

D
dapan1121 已提交
230
  SCatalogCfg cfg = {.maxDBCacheNum = 100, .maxTblCacheNum = 100};
231 232
  catalogInit(&cfg);

X
Xiaoyu Wang 已提交
233 234
  SSchedulerCfg scfg = {.maxJobNum = 100};
  schedulerInit(&scfg);
S
Shengliang Guan 已提交
235
  tscDebug("starting to initialize TAOS driver");
H
Haojun Liao 已提交
236 237 238 239 240

  taosSetCoreDump(true);

  initTaskQueue();

241
  clientConnRefPool = taosOpenRef(200, destroyTscObj);
dengyihao's avatar
dengyihao 已提交
242
  clientReqRefPool = taosOpenRef(40960, doDestroyRequest);
H
Haojun Liao 已提交
243 244

  taosGetAppName(appInfo.appName, NULL);
H
Haojun Liao 已提交
245 246
  pthread_mutex_init(&appInfo.mutex, NULL);

dengyihao's avatar
dengyihao 已提交
247
  appInfo.pid = taosGetPId();
H
Haojun Liao 已提交
248
  appInfo.startTime = taosGetTimestampMs();
dengyihao's avatar
dengyihao 已提交
249
  appInfo.pInstMap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK);
H
Haojun Liao 已提交
250 251 252 253 254 255 256 257 258
  tscDebug("client is initialized successfully");
}

int taos_init() {
  pthread_once(&tscinit, taos_init_imp);
  return tscInitRes;
}

int taos_options_imp(TSDB_OPTION option, const char *str) {
S
Shengliang Guan 已提交
259
#if 0  
H
Haojun Liao 已提交
260 261 262 263 264 265 266 267 268 269 270 271
  SGlobalCfg *cfg = NULL;

  switch (option) {
    case TSDB_OPTION_CONFIGDIR:
      cfg = taosGetConfigOption("configDir");
      assert(cfg != NULL);

      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
        tstrncpy(configDir, str, TSDB_FILENAME_LEN);
        cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
        tscInfo("set config file directory:%s", str);
      } else {
dengyihao's avatar
dengyihao 已提交
272 273
        tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, str,
                tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr);
H
Haojun Liao 已提交
274 275 276 277 278 279 280 281 282 283 284 285 286 287
      }
      break;

    case TSDB_OPTION_SHELL_ACTIVITY_TIMER:
      cfg = taosGetConfigOption("shellActivityTimer");
      assert(cfg != NULL);

      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
        tsShellActivityTimer = atoi(str);
        if (tsShellActivityTimer < 1) tsShellActivityTimer = 1;
        if (tsShellActivityTimer > 3600) tsShellActivityTimer = 3600;
        cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
        tscInfo("set shellActivityTimer:%d", tsShellActivityTimer);
      } else {
dengyihao's avatar
dengyihao 已提交
288 289
        tscWarn("config option:%s, input value:%s, is configured by %s, use %d", cfg->option, str,
                tsCfgStatusStr[cfg->cfgStatus], *(int32_t *)cfg->ptr);
H
Haojun Liao 已提交
290 291 292 293 294 295 296 297
      }
      break;

    case TSDB_OPTION_LOCALE: {  // set locale
      cfg = taosGetConfigOption("locale");
      assert(cfg != NULL);

      size_t len = strlen(str);
S
Shengliang Guan 已提交
298
      if (len == 0 || len > TD_LOCALE_LEN) {
H
Haojun Liao 已提交
299 300 301 302 303 304 305
        tscInfo("Invalid locale:%s, use default", str);
        return -1;
      }

      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
        char sep = '.';

dengyihao's avatar
dengyihao 已提交
306 307
        if (strlen(tsLocale) == 0) {  // locale does not set yet
          char *defaultLocale = setlocale(LC_CTYPE, "");
H
Haojun Liao 已提交
308 309 310 311 312 313 314 315

          // The locale of the current OS does not be set correctly, so the default locale cannot be acquired.
          // The launch of current system will abort soon.
          if (defaultLocale == NULL) {
            tscError("failed to get default locale, please set the correct locale in current OS");
            return -1;
          }

S
Shengliang Guan 已提交
316
          tstrncpy(tsLocale, defaultLocale, TD_LOCALE_LEN);
H
Haojun Liao 已提交
317 318 319 320 321
        }

        // set the user specified locale
        char *locale = setlocale(LC_CTYPE, str);

dengyihao's avatar
dengyihao 已提交
322
        if (locale != NULL) {  // failed to set the user specified locale
H
Haojun Liao 已提交
323 324
          tscInfo("locale set, prev locale:%s, new locale:%s", tsLocale, locale);
          cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
dengyihao's avatar
dengyihao 已提交
325
        } else {  // set the user specified locale failed, use default LC_CTYPE as current locale
H
Haojun Liao 已提交
326 327 328 329
          locale = setlocale(LC_CTYPE, tsLocale);
          tscInfo("failed to set locale:%s, current locale:%s", str, tsLocale);
        }

S
Shengliang Guan 已提交
330
        tstrncpy(tsLocale, locale, TD_LOCALE_LEN);
H
Haojun Liao 已提交
331 332 333 334 335 336 337 338 339 340 341 342 343 344

        char *charset = strrchr(tsLocale, sep);
        if (charset != NULL) {
          charset += 1;

          charset = taosCharsetReplace(charset);

          if (taosValidateEncodec(charset)) {
            if (strlen(tsCharset) == 0) {
              tscInfo("charset set:%s", charset);
            } else {
              tscInfo("charset changed from %s to %s", tsCharset, charset);
            }

S
Shengliang Guan 已提交
345
            tstrncpy(tsCharset, charset, TD_LOCALE_LEN);
H
Haojun Liao 已提交
346 347 348 349 350 351 352
            cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;

          } else {
            tscInfo("charset:%s is not valid in locale, charset remains:%s", charset, tsCharset);
          }

          free(charset);
dengyihao's avatar
dengyihao 已提交
353
        } else {  // it may be windows system
H
Haojun Liao 已提交
354 355 356
          tscInfo("charset remains:%s", tsCharset);
        }
      } else {
dengyihao's avatar
dengyihao 已提交
357 358
        tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, str,
                tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr);
H
Haojun Liao 已提交
359 360 361 362 363 364 365 366 367 368
      }
      break;
    }

    case TSDB_OPTION_CHARSET: {
      /* set charset will override the value of charset, assigned during system locale changed */
      cfg = taosGetConfigOption("charset");
      assert(cfg != NULL);

      size_t len = strlen(str);
S
Shengliang Guan 已提交
369
      if (len == 0 || len > TD_LOCALE_LEN) {
H
Haojun Liao 已提交
370 371 372 373 374 375 376 377 378 379 380 381
        tscInfo("failed to set charset:%s", str);
        return -1;
      }

      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
        if (taosValidateEncodec(str)) {
          if (strlen(tsCharset) == 0) {
            tscInfo("charset is set:%s", str);
          } else {
            tscInfo("charset changed from %s to %s", tsCharset, str);
          }

S
Shengliang Guan 已提交
382
          tstrncpy(tsCharset, str, TD_LOCALE_LEN);
H
Haojun Liao 已提交
383 384 385 386 387
          cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
        } else {
          tscInfo("charset:%s not valid", str);
        }
      } else {
dengyihao's avatar
dengyihao 已提交
388 389
        tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, str,
                tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr);
H
Haojun Liao 已提交
390 391 392 393 394 395 396 397 398 399
      }

      break;
    }

    case TSDB_OPTION_TIMEZONE:
      cfg = taosGetConfigOption("timezone");
      assert(cfg != NULL);

      if (cfg->cfgStatus <= TAOS_CFG_CSTATUS_OPTION) {
S
Shengliang Guan 已提交
400
        tstrncpy(tsTimezone, str, TD_TIMEZONE_LEN);
H
Haojun Liao 已提交
401 402 403 404
        tsSetTimeZone();
        cfg->cfgStatus = TAOS_CFG_CSTATUS_OPTION;
        tscDebug("timezone set:%s, input:%s by taos_options", tsTimezone, str);
      } else {
dengyihao's avatar
dengyihao 已提交
405 406
        tscWarn("config option:%s, input value:%s, is configured by %s, use %s", cfg->option, str,
                tsCfgStatusStr[cfg->cfgStatus], (char *)cfg->ptr);
H
Haojun Liao 已提交
407 408 409 410 411 412 413 414
      }
      break;

    default:
      // TODO return the correct error code to client in the format for taos_errstr()
      tscError("Invalid option %d", option);
      return -1;
  }
S
Shengliang Guan 已提交
415
#endif
H
Haojun Liao 已提交
416 417 418
  return 0;
}

419 420 421 422 423
/**
 * The request id is an unsigned integer format of 64bit.
 *+------------+-----+-----------+---------------+
 *| uid|localIp| PId | timestamp | serial number |
 *+------------+-----+-----------+---------------+
424
 *| 12bit      |12bit|24bit      |16bit          |
425 426 427 428
 *+------------+-----+-----------+---------------+
 * @return
 */
uint64_t generateRequestId() {
H
Haojun Liao 已提交
429
  static uint64_t hashId = 0;
dengyihao's avatar
dengyihao 已提交
430
  static int32_t  requestSerialId = 0;
H
Haojun Liao 已提交
431 432 433 434 435 436 437 438 439 440 441

  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));
    }
442 443
  }

dengyihao's avatar
dengyihao 已提交
444 445 446
  int64_t  ts = taosGetTimestampMs();
  uint64_t pid = taosGetPId();
  int32_t  val = atomic_add_fetch_32(&requestSerialId, 1);
447

448
  uint64_t id = ((hashId & 0x0FFF) << 52) | ((pid & 0x0FFF) << 40) | ((ts & 0xFFFFFF) << 16) | (val & 0xFFFF);
449 450 451
  return id;
}

H
Haojun Liao 已提交
452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514
#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){
  pthread_mutex_lock(&setConfMutex);
  setConfRet ret = taos_set_config_imp(config);
  pthread_mutex_unlock(&setConfMutex);
  return ret;
}
515
#endif