clientImpl.c 19.9 KB
Newer Older
1

2
#include "../../libs/scheduler/inc/schedulerInt.h"
3
#include "clientInt.h"
4
#include "clientLog.h"
5 6 7
#include "parser.h"
#include "planner.h"
#include "scheduler.h"
8 9
#include "tdef.h"
#include "tep.h"
10
#include "tglobal.h"
11
#include "tmsgtype.h"
12 13
#include "tnote.h"
#include "tpagedfile.h"
14
#include "tref.h"
X
Xiaoyu Wang 已提交
15 16

#define CHECK_CODE_GOTO(expr, lable) \
H
Haojun Liao 已提交
17 18
  do {                               \
    int32_t code = expr;             \
X
Xiaoyu Wang 已提交
19
    if (TSDB_CODE_SUCCESS != code) { \
H
Haojun Liao 已提交
20 21 22
      terrno = code;                 \
      goto lable;                    \
    }                                \
X
Xiaoyu Wang 已提交
23
  } while (0)
24

25
static int32_t initEpSetFromCfg(const char *firstEp, const char *secondEp, SCorEpSet *pEpSet);
26 27
static SMsgSendInfo* buildConnectMsg(SRequestObj *pRequest);
static void destroySendMsgInfo(SMsgSendInfo* pMsgBody);
H
Haojun Liao 已提交
28

29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
static bool stringLengthCheck(const char* str, size_t maxsize) {
  if (str == NULL) {
    return false;
  }

  size_t len = strlen(str);
  if (len <= 0 || len > maxsize) {
    return false;
  }

  return true;
}

static bool validateUserName(const char* user) {
  return stringLengthCheck(user, TSDB_USER_LEN - 1);
}

static bool validatePassword(const char* passwd) {
47
  return stringLengthCheck(passwd, TSDB_PASSWORD_LEN - 1);
48 49 50 51 52 53
}

static bool validateDbName(const char* db) {
  return stringLengthCheck(db, TSDB_DB_NAME_LEN - 1);
}

54 55 56 57 58 59 60
static char* getClusterKey(const char* user, const char* auth, const char* ip, int32_t port) {
  char key[512] = {0};
  snprintf(key, sizeof(key), "%s:%s:%s:%d", user, auth, ip, port);
  return strdup(key);
}

static STscObj* taosConnectImpl(const char *ip, const char *user, const char *auth, const char *db, uint16_t port, __taos_async_fn_t fp, void *param, SAppInstInfo* pAppInfo);
61
static void  setResSchemaInfo(SReqResultInfo* pResInfo, const SDataBlockSchema* pDataBlockSchema);
62 63

TAOS *taos_connect_internal(const char *ip, const char *user, const char *pass, const char *auth, const char *db, uint16_t port) {
64 65 66 67
  if (taos_init() != TSDB_CODE_SUCCESS) {
    return NULL;
  }

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  if (!validateUserName(user)) {
    terrno = TSDB_CODE_TSC_INVALID_USER_LENGTH;
    return NULL;
  }

  char tmp[TSDB_DB_NAME_LEN] = {0};
  if (db != NULL) {
    if(!validateDbName(db)) {
      terrno = TSDB_CODE_TSC_INVALID_DB_LENGTH;
      return NULL;
    }

    tstrncpy(tmp, db, sizeof(tmp));
    strdequote(tmp);
  }

84
  char secretEncrypt[32] = {0};
85 86 87 88 89 90
  if (auth == NULL) {
    if (!validatePassword(pass)) {
      terrno = TSDB_CODE_TSC_INVALID_PASS_LENGTH;
      return NULL;
    }

91
    taosEncryptPass_c((uint8_t *)pass, strlen(pass), secretEncrypt);
92 93 94 95
  } else {
    tstrncpy(secretEncrypt, auth, tListLen(secretEncrypt));
  }

96
  SCorEpSet epSet = {0};
97 98 99 100 101 102 103 104 105 106 107 108 109 110
  if (ip) {
    if (initEpSetFromCfg(ip, NULL, &epSet) < 0) {
      return NULL;
    }

    if (port) {
      epSet.epSet.port[0] = port;
    }
  } else {
    if (initEpSetFromCfg(tsFirst, tsSecond, &epSet) < 0) {
      return NULL;
    }
  }

111 112
  char* key = getClusterKey(user, secretEncrypt, ip, port);

H
Haojun Liao 已提交
113
  SAppInstInfo** pInst = taosHashGet(appInfo.pInstMap, key, strlen(key));
114
  if (pInst == NULL) {
H
Haojun Liao 已提交
115 116
    SAppInstInfo* p = calloc(1, sizeof(struct SAppInstInfo));
    p->mgmtEp       = epSet;
H
Haojun Liao 已提交
117
    p->pTransporter = openTransporter(user, secretEncrypt, tsNumOfCores);
H
Haojun Liao 已提交
118
    taosHashPut(appInfo.pInstMap, key, strlen(key), &p, POINTER_BYTES);
119

H
Haojun Liao 已提交
120
    pInst = &p;
121 122
  }

H
Haojun Liao 已提交
123 124
  tfree(key);
  return taosConnectImpl(ip, user, &secretEncrypt[0], db, port, NULL, NULL, *pInst);
125 126
}

X
Xiaoyu Wang 已提交
127 128 129
int32_t buildRequest(STscObj *pTscObj, const char *sql, int sqlLen, SRequestObj** pRequest) {
  *pRequest = createRequest(pTscObj, NULL, NULL, TSDB_SQL_SELECT);
  if (*pRequest == NULL) {
130
    tscError("failed to malloc sqlObj");
X
Xiaoyu Wang 已提交
131
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
132 133
  }

X
Xiaoyu Wang 已提交
134 135 136 137 138
  (*pRequest)->sqlstr = malloc(sqlLen + 1);
  if ((*pRequest)->sqlstr == NULL) {
    tscError("0x%"PRIx64" failed to prepare sql string buffer", (*pRequest)->self);
    (*pRequest)->msgBuf = strdup("failed to prepare sql string buffer");
    return TSDB_CODE_TSC_OUT_OF_MEMORY;
139 140
  }

X
Xiaoyu Wang 已提交
141 142 143
  strntolower((*pRequest)->sqlstr, sql, (int32_t)sqlLen);
  (*pRequest)->sqlstr[sqlLen] = 0;
  (*pRequest)->sqlLen = sqlLen;
144

H
Haojun Liao 已提交
145
  tscDebugL("0x%"PRIx64" SQL: %s, reqId:0x%"PRIx64, (*pRequest)->self, (*pRequest)->sqlstr, (*pRequest)->requestId);
X
Xiaoyu Wang 已提交
146 147
  return TSDB_CODE_SUCCESS;
}
148

X
Xiaoyu Wang 已提交
149
int32_t parseSql(SRequestObj* pRequest, SQueryNode** pQuery) {
H
Haojun Liao 已提交
150 151
  STscObj* pTscObj = pRequest->pTscObj;

X
Xiaoyu Wang 已提交
152
  SParseContext cxt = {
H
Haojun Liao 已提交
153
    .ctx = {.requestId = pRequest->requestId, .acctId = pTscObj->acctId, .db = getConnectionDB(pTscObj), .pTransporter = pTscObj->pTransporter},
154
    .pSql   = pRequest->sqlstr,
X
Xiaoyu Wang 已提交
155
    .sqlLen = pRequest->sqlLen,
156
    .pMsg   = pRequest->msgBuf,
X
Xiaoyu Wang 已提交
157 158
    .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE
  };
H
Haojun Liao 已提交
159

H
Haojun Liao 已提交
160
  cxt.ctx.mgmtEpSet = getEpSet_s(&pTscObj->pAppInfo->mgmtEp);
161
  int32_t code = catalogGetHandle(pTscObj->pAppInfo->clusterId, &cxt.ctx.pCatalog);
H
Haojun Liao 已提交
162 163 164 165 166 167
  if (code != TSDB_CODE_SUCCESS) {
    tfree(cxt.ctx.db);
    return code;
  }

  code = qParseQuerySql(&cxt, pQuery);
168

X
Xiaoyu Wang 已提交
169 170 171
  tfree(cxt.ctx.db);
  return code;
}
H
Haojun Liao 已提交
172

X
Xiaoyu Wang 已提交
173 174 175
int32_t execDdlQuery(SRequestObj* pRequest, SQueryNode* pQuery) {
  SDclStmtInfo* pDcl = (SDclStmtInfo*)pQuery;
  pRequest->type = pDcl->msgType;
H
Haojun Liao 已提交
176
  pRequest->body.requestMsg = (SDataBuf){.pData = pDcl->pMsg, .len = pDcl->msgLen};
X
Xiaoyu Wang 已提交
177

H
Haojun Liao 已提交
178
  STscObj* pTscObj = pRequest->pTscObj;
179
  SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest);
X
Xiaoyu Wang 已提交
180

181
  int64_t transporterId = 0;
182 183 184 185
  if (pDcl->msgType == TDMT_VND_CREATE_TABLE || pDcl->msgType == TDMT_VND_SHOW_TABLES) {
    if (pDcl->msgType == TDMT_VND_SHOW_TABLES) {
      SShowReqInfo* pShowReqInfo = &pRequest->body.showInfo;
      if (pShowReqInfo->pArray == NULL) {
H
Haojun Liao 已提交
186
        pShowReqInfo->currentIndex = 0;  // set the first vnode/ then iterate the next vnode
187 188 189
        pShowReqInfo->pArray = pDcl->pExtension;
      }
    }
190
    asyncSendMsgToServer(pTscObj->pTransporter, &pDcl->epSet, &transporterId, pSendMsg);
X
Xiaoyu Wang 已提交
191
  } else {
192
    SEpSet* pEpSet = &pTscObj->pAppInfo->mgmtEp.epSet;
193
    asyncSendMsgToServer(pTscObj->pTransporter, pEpSet, &transporterId, pSendMsg);
194 195
  }

X
Xiaoyu Wang 已提交
196 197 198
  tsem_wait(&pRequest->body.rspSem);
  return TSDB_CODE_SUCCESS;
}
X
Xiaoyu Wang 已提交
199

200 201
int32_t getPlan(SRequestObj* pRequest, SQueryNode* pQueryNode, SQueryDag** pDag) {
  pRequest->type = pQueryNode->type;
202 203

  SReqResultInfo* pResInfo = &pRequest->body.resInfo;
204
  int32_t code = qCreateQueryDag(pQueryNode, pDag, pRequest->requestId);
205 206 207 208 209
  if (code != 0) {
    return code;
  }

  if (pQueryNode->type == TSDB_SQL_SELECT) {
210 211 212 213 214 215
    SArray* pa = taosArrayGetP((*pDag)->pSubplans, 0);

    SSubplan* pPlan = taosArrayGetP(pa, 0);
    SDataBlockSchema* pDataBlockSchema = &(pPlan->pDataSink->schema);
    setResSchemaInfo(pResInfo, pDataBlockSchema);
    pRequest->type = TDMT_VND_QUERY;
216 217 218
  }

  return code;
X
Xiaoyu Wang 已提交
219 220
}

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
void setResSchemaInfo(SReqResultInfo* pResInfo, const SDataBlockSchema* pDataBlockSchema) {
  assert(pDataBlockSchema != NULL && pDataBlockSchema->numOfCols > 0);

  pResInfo->numOfCols = pDataBlockSchema->numOfCols;
  pResInfo->fields = calloc(pDataBlockSchema->numOfCols, sizeof(pDataBlockSchema->pSchema[0]));

  for (int32_t i = 0; i < pResInfo->numOfCols; ++i) {
    SSchema* pSchema = &pDataBlockSchema->pSchema[i];
    pResInfo->fields[i].bytes = pSchema->bytes;
    pResInfo->fields[i].type  = pSchema->type;
    tstrncpy(pResInfo->fields[i].name, pSchema[i].name, tListLen(pResInfo->fields[i].name));
  }
}

int32_t scheduleQuery(SRequestObj* pRequest, SQueryDag* pDag) {
236
  if (TSDB_SQL_INSERT == pRequest->type || TSDB_SQL_CREATE_TABLE == pRequest->type) {
X
Xiaoyu Wang 已提交
237
    SQueryResult res = {.code = 0, .numOfRows = 0, .msgSize = ERROR_MSG_BUF_DEFAULT_SIZE, .msg = pRequest->msgBuf};
238

239
    int32_t code = scheduleExecJob(pRequest->pTscObj->pTransporter, NULL, pDag, &pRequest->body.pQueryJob, &res);
240 241 242
    if (code != TSDB_CODE_SUCCESS) {
      // handle error and retry
    } else {
243 244
      if (pRequest->body.pQueryJob != NULL) {
        scheduleFreeJob(pRequest->body.pQueryJob);
245 246 247
      }
    }

X
Xiaoyu Wang 已提交
248 249
    pRequest->affectedRows = res.numOfRows;
    return res.code;
X
Xiaoyu Wang 已提交
250
  }
251

252
  return scheduleAsyncExecJob(pRequest->pTscObj->pTransporter, NULL, pDag, &pRequest->body.pQueryJob);
X
Xiaoyu Wang 已提交
253
}
X
Xiaoyu Wang 已提交
254

L
Liu Jicong 已提交
255 256 257 258 259 260 261 262 263 264
TAOS_RES *tmq_create_topic(TAOS* taos, const char* name, const char* sql, int sqlLen) {
  STscObj* pTscObj = (STscObj*)taos;
  SRequestObj* pRequest = NULL;
  SQueryNode*  pQuery = NULL;
  SQueryDag*   pDag = NULL;
  char *dagStr = NULL;

  terrno = TSDB_CODE_SUCCESS;

  CHECK_CODE_GOTO(buildRequest(pTscObj, sql, sqlLen, &pRequest), _return);
L
Liu Jicong 已提交
265 266 267

//temporary disabled until planner ready
#if 0
L
Liu Jicong 已提交
268 269 270 271 272 273 274 275 276
  CHECK_CODE_GOTO(parseSql(pRequest, &pQuery), _return);
  //TODO: check sql valid

  CHECK_CODE_GOTO(qCreateQueryDag(pQuery, &pDag), _return);

  dagStr = qDagToString(pDag);
  if(dagStr == NULL) {
    //TODO
  }
L
Liu Jicong 已提交
277
#endif
L
Liu Jicong 已提交
278 279 280 281

  SCMCreateTopicReq req = {
    .name = (char*)name,
    .igExists = 0,
L
Liu Jicong 已提交
282 283 284
    /*.physicalPlan = dagStr,*/
    .physicalPlan = (char*)sql,
    .logicalPlan = "",
L
Liu Jicong 已提交
285 286
  };

L
Liu Jicong 已提交
287 288 289 290 291 292 293 294
  int tlen = tSerializeSCMCreateTopicReq(NULL, &req);
  void* buf = malloc(tlen);
  if(buf == NULL) {
    goto _return;
  }
  void* abuf = buf;
  tSerializeSCMCreateTopicReq(&abuf, &req);
  /*printf("formatted: %s\n", dagStr);*/
L
Liu Jicong 已提交
295 296 297

  pRequest->body.requestMsg = (SDataBuf){ .pData = buf, .len = tlen };

298
  SMsgSendInfo* body = buildMsgInfoImpl(pRequest);
L
Liu Jicong 已提交
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
  SEpSet* pEpSet = &pTscObj->pAppInfo->mgmtEp.epSet;

  int64_t transporterId = 0;
  asyncSendMsgToServer(pTscObj->pTransporter, pEpSet, &transporterId, body);

  tsem_wait(&pRequest->body.rspSem);

_return:
  qDestroyQuery(pQuery);
  qDestroyQueryDag(pDag); 
  destroySendMsgInfo(body);
  if (pRequest != NULL && terrno != TSDB_CODE_SUCCESS) {
    pRequest->code = terrno;
  }
  return pRequest;
}

X
Xiaoyu Wang 已提交
316 317 318 319 320 321
TAOS_RES *taos_query_l(TAOS *taos, const char *sql, int sqlLen) {
  STscObj *pTscObj = (STscObj *)taos;
  if (sqlLen > (size_t) tsMaxSQLStringLen) {
    tscError("sql string exceeds max length:%d", tsMaxSQLStringLen);
    terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT;
    return NULL;
322 323
  }

X
Xiaoyu Wang 已提交
324 325
  nPrintTsc("%s", sql)

H
Haojun Liao 已提交
326 327 328
  SRequestObj *pRequest = NULL;
  SQueryNode  *pQuery   = NULL;
  SQueryDag   *pDag     = NULL;
X
Xiaoyu Wang 已提交
329

X
Xiaoyu Wang 已提交
330
  terrno = TSDB_CODE_SUCCESS;
X
Xiaoyu Wang 已提交
331 332
  CHECK_CODE_GOTO(buildRequest(pTscObj, sql, sqlLen, &pRequest), _return);
  CHECK_CODE_GOTO(parseSql(pRequest, &pQuery), _return);
H
Haojun Liao 已提交
333

X
Xiaoyu Wang 已提交
334 335
  if (qIsDdlQuery(pQuery)) {
    CHECK_CODE_GOTO(execDdlQuery(pRequest, pQuery), _return);
H
Haojun Liao 已提交
336
  } else {
X
Xiaoyu Wang 已提交
337
    CHECK_CODE_GOTO(getPlan(pRequest, pQuery, &pDag), _return);
338
    CHECK_CODE_GOTO(scheduleQuery(pRequest, pDag), _return);
X
Xiaoyu Wang 已提交
339
    pRequest->code = terrno;
X
Xiaoyu Wang 已提交
340 341 342
  }

_return:
L
Liu Jicong 已提交
343
  qDestroyQuery(pQuery);
X
Xiaoyu Wang 已提交
344
  qDestroyQueryDag(pDag);
X
Xiaoyu Wang 已提交
345
  if (NULL != pRequest && TSDB_CODE_SUCCESS != terrno) {
X
Xiaoyu Wang 已提交
346 347
    pRequest->code = terrno;
  }
348

349 350 351
  return pRequest;
}

352
int initEpSetFromCfg(const char *firstEp, const char *secondEp, SCorEpSet *pEpSet) {
353 354
  pEpSet->version = 0;

H
Haojun Liao 已提交
355
  // init mnode ip set
356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
  SEpSet *mgmtEpSet   = &(pEpSet->epSet);
  mgmtEpSet->numOfEps = 0;
  mgmtEpSet->inUse    = 0;

  if (firstEp && firstEp[0] != 0) {
    if (strlen(firstEp) >= TSDB_EP_LEN) {
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
      return -1;
    }

    taosGetFqdnPortFromEp(firstEp, mgmtEpSet->fqdn[0], &(mgmtEpSet->port[0]));
    mgmtEpSet->numOfEps++;
  }

  if (secondEp && secondEp[0] != 0) {
    if (strlen(secondEp) >= TSDB_EP_LEN) {
      terrno = TSDB_CODE_TSC_INVALID_FQDN;
      return -1;
    }

    taosGetFqdnPortFromEp(secondEp, mgmtEpSet->fqdn[mgmtEpSet->numOfEps], &(mgmtEpSet->port[mgmtEpSet->numOfEps]));
    mgmtEpSet->numOfEps++;
  }

  if (mgmtEpSet->numOfEps == 0) {
    terrno = TSDB_CODE_TSC_INVALID_FQDN;
    return -1;
  }

  return 0;
}

388
STscObj* taosConnectImpl(const char *ip, const char *user, const char *auth, const char *db, uint16_t port, __taos_async_fn_t fp, void *param, SAppInstInfo* pAppInfo) {
389
  STscObj *pTscObj = createTscObj(user, auth, db, pAppInfo);
390 391 392 393 394
  if (NULL == pTscObj) {
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return pTscObj;
  }

H
Hongze Cheng 已提交
395
  SRequestObj *pRequest = createRequest(pTscObj, fp, param, TDMT_MND_CONNECT);
396 397 398
  if (pRequest == NULL) {
    destroyTscObj(pTscObj);
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
399 400 401
    return NULL;
  }

402
  SMsgSendInfo* body = buildConnectMsg(pRequest);
403 404

  int64_t transporterId = 0;
H
Haojun Liao 已提交
405
  asyncSendMsgToServer(pTscObj->pTransporter, &pTscObj->pAppInfo->mgmtEp.epSet, &transporterId, body);
406 407 408 409 410 411 412 413 414 415

  tsem_wait(&pRequest->body.rspSem);
  if (pRequest->code != TSDB_CODE_SUCCESS) {
    const char *errorMsg = (pRequest->code == TSDB_CODE_RPC_FQDN_ERROR) ? taos_errstr(pRequest) : tstrerror(terrno);
    printf("failed to connect to server, reason: %s\n\n", errorMsg);

    destroyRequest(pRequest);
    taos_close(pTscObj);
    pTscObj = NULL;
  } else {
416
    tscDebug("0x%"PRIx64" connection is opening, connId:%d, dnodeConn:%p, reqId:0x%"PRIx64, pTscObj->id, pTscObj->connId, pTscObj->pTransporter, pRequest->requestId);
417 418 419 420 421 422
    destroyRequest(pRequest);
  }

  return pTscObj;
}

423 424 425 426 427 428 429
static SMsgSendInfo* buildConnectMsg(SRequestObj *pRequest) {
  SMsgSendInfo *pMsgSendInfo = calloc(1, sizeof(SMsgSendInfo));
  if (pMsgSendInfo == NULL) {
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
    return NULL;
  }

H
Haojun Liao 已提交
430
  pMsgSendInfo->msgType         = TDMT_MND_CONNECT;
S
Shengliang Guan 已提交
431
  pMsgSendInfo->msgInfo.len     = sizeof(SConnectReq);
432 433
  pMsgSendInfo->requestObjRefId = pRequest->self;
  pMsgSendInfo->requestId       = pRequest->requestId;
D
catalog  
dapan1121 已提交
434
  pMsgSendInfo->fp              = handleRequestRspFp[TMSG_INDEX(pMsgSendInfo->msgType)];
435
  pMsgSendInfo->param           = pRequest;
436

S
Shengliang Guan 已提交
437
  SConnectReq *pConnect = calloc(1, sizeof(SConnectReq));
438
  if (pConnect == NULL) {
439
    tfree(pMsgSendInfo);
440
    terrno = TSDB_CODE_TSC_OUT_OF_MEMORY;
441
    return NULL;
442 443
  }

444 445
  STscObj *pObj = pRequest->pTscObj;

446
  char* db = getConnectionDB(pObj);
447
  tstrncpy(pConnect->db, db, sizeof(pConnect->db));
448
  tfree(db);
449

450 451 452
  pConnect->pid = htonl(appInfo.pid);
  pConnect->startTime = htobe64(appInfo.startTime);
  tstrncpy(pConnect->app, appInfo.appName, tListLen(pConnect->app));
453

454 455
  pMsgSendInfo->msgInfo.pData = pConnect;
  return pMsgSendInfo;
456 457
}

458
static void destroySendMsgInfo(SMsgSendInfo* pMsgBody) {
H
Haojun Liao 已提交
459
  assert(pMsgBody != NULL);
460 461
  tfree(pMsgBody->msgInfo.pData);
  tfree(pMsgBody);
H
Haojun Liao 已提交
462 463
}

464
void processMsgFromServer(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) {
465 466
  SMsgSendInfo *pSendInfo = (SMsgSendInfo *) pMsg->ahandle;
  assert(pMsg->ahandle != NULL);
467

468
  if (pSendInfo->requestObjRefId != 0) {
H
Haojun Liao 已提交
469
    SRequestObj *pRequest = (SRequestObj *)taosAcquireRef(clientReqRefPool, pSendInfo->requestObjRefId);
470
    assert(pRequest->self == pSendInfo->requestObjRefId);
471

472 473
    pRequest->metric.rsp = taosGetTimestampMs();
    pRequest->code = pMsg->code;
474

475 476 477 478 479
    STscObj *pTscObj = pRequest->pTscObj;
    if (pEpSet) {
      if (!isEpsetEqual(&pTscObj->pAppInfo->mgmtEp.epSet, pEpSet)) {
        updateEpSet_s(&pTscObj->pAppInfo->mgmtEp, pEpSet);
      }
480 481
    }

482
    /*
483 484
   * There is not response callback function for submit response.
   * The actual inserted number of points is the first number.
485
     */
486
    int32_t elapsed = pRequest->metric.rsp - pRequest->metric.start;
487
    if (pMsg->code == TSDB_CODE_SUCCESS) {
H
Haojun Liao 已提交
488
      tscDebug("0x%" PRIx64 " message:%s, code:%s rspLen:%d, elapsed:%d ms, reqId:0x%"PRIx64, pRequest->self,
489
          TMSG_INFO(pMsg->msgType), tstrerror(pMsg->code), pMsg->contLen, elapsed, pRequest->requestId);
490
    } else {
H
Haojun Liao 已提交
491
      tscError("0x%" PRIx64 " SQL cmd:%s, code:%s rspLen:%d, elapsed time:%d ms, reqId:0x%"PRIx64, pRequest->self,
492
          TMSG_INFO(pMsg->msgType), tstrerror(pMsg->code), pMsg->contLen, elapsed, pRequest->requestId);
493
    }
494

H
Haojun Liao 已提交
495
    taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId);
496 497
  }

498 499 500 501 502 503 504 505 506 507
  SDataBuf buf = {.len = pMsg->contLen, .pData = NULL};

  if (pMsg->contLen > 0) {
    buf.pData = calloc(1, pMsg->contLen);
    if (buf.pData == NULL) {
      terrno = TSDB_CODE_OUT_OF_MEMORY;
      pMsg->code = TSDB_CODE_OUT_OF_MEMORY;
    } else {
      memcpy(buf.pData, pMsg->pCont, pMsg->contLen);
    }
508 509
  }

510
  pSendInfo->fp(pSendInfo->param, &buf, pMsg->code);
511
  rpcFreeCont(pMsg->pCont);
512
  destroySendMsgInfo(pSendInfo);
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

TAOS *taos_connect_auth(const char *ip, const char *user, const char *auth, const char *db, uint16_t port) {
  tscDebug("try to connect to %s:%u by auth, user:%s db:%s", ip, port, user, db);
  if (user == NULL) {
    user = TSDB_DEFAULT_USER;
  }

  if (auth == NULL) {
    tscError("No auth info is given, failed to connect to server");
    return NULL;
  }

  return taos_connect_internal(ip, user, NULL, auth, db, port);
}

TAOS *taos_connect_l(const char *ip, int ipLen, const char *user, int userLen, const char *pass, int passLen, const char *db, int dbLen, uint16_t port) {
  char ipStr[TSDB_EP_LEN]      = {0};
  char dbStr[TSDB_DB_NAME_LEN] = {0};
  char userStr[TSDB_USER_LEN]  = {0};
  char passStr[TSDB_PASSWORD_LEN]   = {0};

  strncpy(ipStr,   ip,   MIN(TSDB_EP_LEN - 1, ipLen));
  strncpy(userStr, user, MIN(TSDB_USER_LEN - 1, userLen));
  strncpy(passStr, pass, MIN(TSDB_PASSWORD_LEN - 1, passLen));
  strncpy(dbStr,   db,   MIN(TSDB_DB_NAME_LEN - 1, dbLen));
  return taos_connect(ipStr, userStr, passStr, dbStr, port);
540 541 542 543
}

void* doFetchRow(SRequestObj* pRequest) {
  assert(pRequest != NULL);
544
  SReqResultInfo* pResultInfo = &pRequest->body.resInfo;
545

H
Haojun Liao 已提交
546
  if (pResultInfo->pData == NULL || pResultInfo->current >= pResultInfo->numOfRows) {
547 548
    if (pRequest->type == TDMT_VND_QUERY) {
      pRequest->type = TDMT_VND_FETCH;
D
dapan1121 已提交
549
      scheduleFetchRows(pRequest->body.pQueryJob, (void **)&pRequest->body.resInfo.pData);
D
dapan1121 已提交
550 551 552 553 554 555 556
      
      pResultInfo->current = 0;
      if (pResultInfo->numOfRows <= pResultInfo->current) {
        return NULL;
      }

      goto _return;
557
    } else if (pRequest->type == TDMT_MND_SHOW) {
H
Haojun Liao 已提交
558 559 560
      pRequest->type = TDMT_MND_SHOW_RETRIEVE;
    } else if (pRequest->type == TDMT_VND_SHOW_TABLES) {
      pRequest->type = TDMT_VND_SHOW_TABLES_FETCH;
H
Haojun Liao 已提交
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583
    } else if (pRequest->type == TDMT_VND_SHOW_TABLES_FETCH) {
      pRequest->type = TDMT_VND_SHOW_TABLES;
      SShowReqInfo* pShowReqInfo = &pRequest->body.showInfo;
      pShowReqInfo->currentIndex += 1;
      if (pShowReqInfo->currentIndex >= taosArrayGetSize(pShowReqInfo->pArray)) {
        return NULL;
      }

      SVgroupInfo* pVgroupInfo = taosArrayGet(pShowReqInfo->pArray, pShowReqInfo->currentIndex);
      SVShowTablesReq* pShowReq = calloc(1, sizeof(SVShowTablesReq));
      pShowReq->head.vgId = htonl(pVgroupInfo->vgId);

      pRequest->body.requestMsg.len = sizeof(SVShowTablesReq);
      pRequest->body.requestMsg.pData = pShowReq;

      SMsgSendInfo* body = buildMsgInfoImpl(pRequest);

      int64_t  transporterId = 0;
      STscObj *pTscObj = pRequest->pTscObj;
      asyncSendMsgToServer(pTscObj->pTransporter, &pTscObj->pAppInfo->mgmtEp.epSet, &transporterId, body);
      tsem_wait(&pRequest->body.rspSem);

      pRequest->type = TDMT_VND_SHOW_TABLES_FETCH;
H
Haojun Liao 已提交
584
    }
H
Haojun Liao 已提交
585

586
    SMsgSendInfo* body = buildMsgInfoImpl(pRequest);
H
Haojun Liao 已提交
587

588 589
    int64_t  transporterId = 0;
    STscObj *pTscObj = pRequest->pTscObj;
H
Haojun Liao 已提交
590
    asyncSendMsgToServer(pTscObj->pTransporter, &pTscObj->pAppInfo->mgmtEp.epSet, &transporterId, body);
H
Haojun Liao 已提交
591 592 593 594 595 596 597 598 599

    tsem_wait(&pRequest->body.rspSem);

    pResultInfo->current = 0;
    if (pResultInfo->numOfRows <= pResultInfo->current) {
      return NULL;
    }
  }

D
dapan1121 已提交
600 601
_return:

H
Haojun Liao 已提交
602 603 604 605 606
  for(int32_t i = 0; i < pResultInfo->numOfCols; ++i) {
    pResultInfo->row[i] = pResultInfo->pCol[i] + pResultInfo->fields[i].bytes * pResultInfo->current;
    if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) {
      pResultInfo->length[i] = varDataLen(pResultInfo->row[i]);
      pResultInfo->row[i] = varDataVal(pResultInfo->row[i]);
607
    }
H
Haojun Liao 已提交
608 609 610 611 612 613
  }

  pResultInfo->current += 1;
  return pResultInfo->row;
}

614
void setResultDataPtr(SReqResultInfo* pResultInfo, TAOS_FIELD* pFields, int32_t numOfCols, int32_t numOfRows) {
H
Haojun Liao 已提交
615 616 617 618 619 620 621 622
  assert(numOfCols > 0 && pFields != NULL && pResultInfo != NULL);
  if (numOfRows == 0) {
    return;
  }

  int32_t offset = 0;
  for (int32_t i = 0; i < numOfCols; ++i) {
    pResultInfo->length[i] = pResultInfo->fields[i].bytes;
H
Haojun Liao 已提交
623
    pResultInfo->row[i]    = (char*) (pResultInfo->pData + offset * pResultInfo->numOfRows);
H
Haojun Liao 已提交
624 625
    pResultInfo->pCol[i]   = pResultInfo->row[i];
    offset += pResultInfo->fields[i].bytes;
626
  }
S
Shengliang Guan 已提交
627 628
}

629 630 631 632 633
char* getConnectionDB(STscObj* pObj) {
  char *p = NULL;
  pthread_mutex_lock(&pObj->mutex);
  p = strndup(pObj->db, tListLen(pObj->db));
  pthread_mutex_unlock(&pObj->mutex);
S
Shengliang Guan 已提交
634

635 636 637 638 639 640 641 642 643
  return p;
}

void setConnectionDB(STscObj* pTscObj, const char* db) {
  assert(db != NULL && pTscObj != NULL);
  pthread_mutex_lock(&pTscObj->mutex);
  tstrncpy(pTscObj->db, db, tListLen(pTscObj->db));
  pthread_mutex_unlock(&pTscObj->mutex);
}
S
Shengliang Guan 已提交
644