tscAsync.c 17.4 KB
Newer Older
H
hzcheng 已提交
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"
H
hzcheng 已提交
17 18 19 20 21 22 23 24

#include "tlog.h"
#include "trpc.h"
#include "tscProfile.h"
#include "tscSecondaryMerge.h"
#include "tscUtil.h"
#include "tsclient.h"
#include "tsocket.h"
H
hjxilinx 已提交
25
#include "tscSQLParser.h"
H
hzcheng 已提交
26
#include "tutil.h"
L
lihui 已提交
27
#include "tnote.h"
H
hzcheng 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42

void tscProcessFetchRow(SSchedMsg *pMsg);
void tscProcessAsyncRetrieve(void *param, TAOS_RES *tres, int numOfRows);
static void tscProcessAsyncRetrieveNextVnode(void *param, TAOS_RES *tres, int numOfRows);
static void tscProcessAsyncContinueRetrieve(void *param, TAOS_RES *tres, int numOfRows);

static void tscProcessAsyncRetrieveImpl(void *param, TAOS_RES *tres, int numOfRows, void (*fp)());

/*
 * proxy function to perform sequentially query&retrieve operation.
 * If sql queries upon metric and two-stage merge procedure is not needed,
 * it will sequentially query&retrieve data for all vnodes in pCmd->pMetricMeta
 */
static void tscProcessAsyncFetchRowsProxy(void *param, TAOS_RES *tres, int numOfRows);

H
hjxilinx 已提交
43
// TODO return the correct error code to client in tscQueueAsyncError
S
slguan 已提交
44
void taos_query_a(TAOS *taos, const char *sqlstr, void (*fp)(void *, TAOS_RES *, int), void *param) {
H
hzcheng 已提交
45 46 47 48 49 50 51 52
  STscObj *pObj = (STscObj *)taos;
  if (pObj == NULL || pObj->signature != pObj) {
    tscError("bug!!! pObj:%p", pObj);
    globalCode = TSDB_CODE_DISCONNECTED;
    tscQueueAsyncError(fp, param);
    return;
  }

53
  int32_t sqlLen = strlen(sqlstr);
H
hjxilinx 已提交
54
  if (sqlLen > tsMaxSQLStringLen) {
55 56 57
    tscError("sql string too long");
    tscQueueAsyncError(fp, param);
    return;
H
hjxilinx 已提交
58
  }
L
lihui 已提交
59 60

  taosNotePrintTsc(sqlstr);
61

H
hjxilinx 已提交
62
  SSqlObj *pSql = (SSqlObj *)calloc(1, sizeof(SSqlObj));
H
hzcheng 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75 76
  if (pSql == NULL) {
    tscError("failed to malloc sqlObj");
    tscQueueAsyncError(fp, param);
    return;
  }

  SSqlCmd *pCmd = &pSql->cmd;
  SSqlRes *pRes = &pSql->res;

  pSql->signature = pSql;
  pSql->pTscObj = pObj;
  pSql->fp = fp;
  pSql->param = param;

S
slguan 已提交
77 78 79
  if (TSDB_CODE_SUCCESS != tscAllocPayload(pCmd, TSDB_DEFAULT_PAYLOAD_SIZE)) {
    tscError("failed to malloc payload");
    tfree(pSql);
H
hzcheng 已提交
80 81 82 83
    tscQueueAsyncError(fp, param);
    return;
  }

S
slguan 已提交
84

H
hzcheng 已提交
85 86 87 88
  pSql->sqlstr = malloc(sqlLen + 1);
  if (pSql->sqlstr == NULL) {
    tscError("%p failed to malloc sql string buffer", pSql);
    tscQueueAsyncError(fp, param);
89 90
    free(pCmd->payload);
    free(pSql);
H
hzcheng 已提交
91 92 93 94 95 96
    return;
  }

  pRes->qhandle = 0;
  pRes->numOfRows = 1;

S
slguan 已提交
97
  strtolower(pSql->sqlstr, sqlstr);
H
hzcheng 已提交
98 99
  tscTrace("%p Async SQL: %s, pObj:%p", pSql, pSql->sqlstr, pObj);

100
  int32_t code = tsParseSql(pSql, true);
H
hzcheng 已提交
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
  if (code == TSDB_CODE_ACTION_IN_PROGRESS) return;

  if (code != TSDB_CODE_SUCCESS) {
    pSql->res.code = (uint8_t)code;
    tscQueueAsyncRes(pSql);
    return;
  }

  tscDoQuery(pSql);
}

static void tscProcessAsyncFetchRowsProxy(void *param, TAOS_RES *tres, int numOfRows) {
  if (tres == NULL) {
    return;
  }

  SSqlObj *pSql = (SSqlObj *)tres;
  SSqlRes *pRes = &pSql->res;
  SSqlCmd *pCmd = &pSql->cmd;

121 122
  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0);
  
H
hzcheng 已提交
123
  // sequentially retrieve data from remain vnodes first, query vnode specified by vnodeIdx
124
  if (numOfRows == 0 && tscProjectionQueryOnMetric(pCmd, 0)) {
H
hzcheng 已提交
125
    // vnode is denoted by vnodeIdx, continue to query vnode specified by vnodeIdx
126
    SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
H
hjxilinx 已提交
127
    assert(pMeterMetaInfo->vnodeIndex >= 0);
H
hzcheng 已提交
128 129

    /* reach the maximum number of output rows, abort */
130
    if (pCmd->globalLimit > 0 && pRes->numOfTotal >= pCmd->globalLimit) {
H
hzcheng 已提交
131 132 133 134 135
      (*pSql->fetchFp)(param, tres, 0);
      return;
    }

    /* update the limit value according to current retrieval results */
136 137 138
    SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0);
    pQueryInfo->limit.limit = pCmd->globalLimit - pRes->numOfTotal;
    pQueryInfo->limit.offset = pRes->offset;
H
hzcheng 已提交
139

H
hjxilinx 已提交
140 141
    if ((++(pMeterMetaInfo->vnodeIndex)) < pMeterMetaInfo->pMetricMeta->numOfVnodes) {
      tscTrace("%p retrieve data from next vnode:%d", pSql, pMeterMetaInfo->vnodeIndex);
H
hzcheng 已提交
142

S
slguan 已提交
143
      pSql->cmd.command = TSDB_SQL_SELECT;  // reset flag to launch query first.
H
hzcheng 已提交
144

S
slguan 已提交
145
      tscResetForNextRetrieve(pRes);
H
hzcheng 已提交
146 147 148 149
      pSql->fp = tscProcessAsyncRetrieveNextVnode;
      tscProcessSql(pSql);
      return;
    }
150
  } else { // localreducer has handle this situation
H
hzcheng 已提交
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221
    if (pCmd->command != TSDB_SQL_RETRIEVE_METRIC) {
      pRes->numOfTotal += pRes->numOfRows;
    }
  }

  (*pSql->fetchFp)(param, tres, numOfRows);
}

// actual continue retrieve function with user-specified callback function
static void tscProcessAsyncRetrieveImpl(void *param, TAOS_RES *tres, int numOfRows, void (*fp)()) {
  SSqlObj *pSql = (SSqlObj *)tres;
  if (pSql == NULL) {  // error
    tscError("sql object is NULL");
    return;
  }

  SSqlCmd *pCmd = &pSql->cmd;
  SSqlRes *pRes = &pSql->res;

  if (pRes->qhandle == 0 || numOfRows != 0) {
    if (pRes->qhandle == 0) {
      tscError("qhandle is NULL");
    } else {
      pRes->code = numOfRows;
    }

    tscQueueAsyncError(pSql->fetchFp, param);
    return;
  }

  pSql->fp = fp;
  if (pCmd->command != TSDB_SQL_RETRIEVE_METRIC && pCmd->command < TSDB_SQL_LOCAL) {
    pCmd->command = (pCmd->command > TSDB_SQL_MGMT) ? TSDB_SQL_RETRIEVE : TSDB_SQL_FETCH;
  }
  tscProcessSql(pSql);
}

/*
 * retrieve callback for fetch rows proxy. It serves as the callback function of querying vnode
 */
static void tscProcessAsyncRetrieveNextVnode(void *param, TAOS_RES *tres, int numOfRows) {
  tscProcessAsyncRetrieveImpl(param, tres, numOfRows, tscProcessAsyncFetchRowsProxy);
}

static void tscProcessAsyncContinueRetrieve(void *param, TAOS_RES *tres, int numOfRows) {
  tscProcessAsyncRetrieveImpl(param, tres, numOfRows, tscProcessAsyncRetrieve);
}

void taos_fetch_rows_a(TAOS_RES *taosa, void (*fp)(void *, TAOS_RES *, int), void *param) {
  SSqlObj *pSql = (SSqlObj *)taosa;
  if (pSql == NULL || pSql->signature != pSql) {
    tscError("sql object is NULL");
    globalCode = TSDB_CODE_DISCONNECTED;
    tscQueueAsyncError(fp, param);
    return;
  }

  SSqlRes *pRes = &pSql->res;
  SSqlCmd *pCmd = &pSql->cmd;

  if (pRes->qhandle == 0) {
    tscError("qhandle is NULL");
    tscQueueAsyncError(fp, param);
    return;
  }

  // user-defined callback function is stored in fetchFp
  pSql->fetchFp = fp;
  pSql->fp = tscProcessAsyncFetchRowsProxy;

  pSql->param = param;
S
slguan 已提交
222
  tscResetForNextRetrieve(pRes);
H
hzcheng 已提交
223 224 225 226

  if (pCmd->command != TSDB_SQL_RETRIEVE_METRIC && pCmd->command < TSDB_SQL_LOCAL) {
    pCmd->command = (pCmd->command > TSDB_SQL_MGMT) ? TSDB_SQL_RETRIEVE : TSDB_SQL_FETCH;
  }
S
slguan 已提交
227

H
hzcheng 已提交
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
  tscProcessSql(pSql);
}

void taos_fetch_row_a(TAOS_RES *taosa, void (*fp)(void *, TAOS_RES *, TAOS_ROW), void *param) {
  SSqlObj *pSql = (SSqlObj *)taosa;
  if (pSql == NULL || pSql->signature != pSql) {
    tscError("sql object is NULL");
    globalCode = TSDB_CODE_DISCONNECTED;
    tscQueueAsyncError(fp, param);
    return;
  }

  SSqlRes *pRes = &pSql->res;
  SSqlCmd *pCmd = &pSql->cmd;

  if (pRes->qhandle == 0) {
    tscError("qhandle is NULL");
    tscQueueAsyncError(fp, param);
    return;
  }

  pSql->fetchFp = fp;
  pSql->param = param;

  if (pRes->row >= pRes->numOfRows) {
S
slguan 已提交
253
    tscResetForNextRetrieve(pRes);
H
hzcheng 已提交
254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    pSql->fp = tscProcessAsyncRetrieve;
    pCmd->command = (pCmd->command > TSDB_SQL_MGMT) ? TSDB_SQL_RETRIEVE : TSDB_SQL_FETCH;
    tscProcessSql(pSql);
  } else {
    SSchedMsg schedMsg;
    schedMsg.fp = tscProcessFetchRow;
    schedMsg.ahandle = pSql;
    schedMsg.thandle = pRes->tsrow;
    schedMsg.msg = NULL;
    taosScheduleTask(tscQhandle, &schedMsg);
  }
}

void tscProcessAsyncRetrieve(void *param, TAOS_RES *tres, int numOfRows) {
  SSqlObj *pSql = (SSqlObj *)tres;
  SSqlRes *pRes = &pSql->res;
  SSqlCmd *pCmd = &pSql->cmd;

272
  SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(pCmd, 0);
H
hzcheng 已提交
273 274
  if (numOfRows == 0) {
    // sequentially retrieve data from remain vnodes.
275
    if (tscProjectionQueryOnMetric(pCmd, 0)) {
H
hzcheng 已提交
276 277 278
      /*
       * vnode is denoted by vnodeIdx, continue to query vnode specified by vnodeIdx till all vnode have been retrieved
       */
279
      SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0);
H
hjxilinx 已提交
280
      assert(pMeterMetaInfo->vnodeIndex >= 0);
H
hzcheng 已提交
281 282

      /* reach the maximum number of output rows, abort */
283
      if (pCmd->globalLimit > 0 && pRes->numOfTotal >= pCmd->globalLimit) {
H
hzcheng 已提交
284 285 286 287 288
        (*pSql->fetchFp)(pSql->param, pSql, NULL);
        return;
      }

      /* update the limit value according to current retrieval results */
289 290
      SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0);
      pQueryInfo->limit.limit = pCmd->globalLimit - pRes->numOfTotal;
H
hzcheng 已提交
291

H
hjxilinx 已提交
292
      if ((++pMeterMetaInfo->vnodeIndex) <= pMeterMetaInfo->pMetricMeta->numOfVnodes) {
S
slguan 已提交
293
        pSql->cmd.command = TSDB_SQL_SELECT;  // reset flag to launch query first.
H
hzcheng 已提交
294

S
slguan 已提交
295
        tscResetForNextRetrieve(pRes);
H
hzcheng 已提交
296 297 298 299 300 301 302 303 304
        pSql->fp = tscProcessAsyncContinueRetrieve;
        tscProcessSql(pSql);
        return;
      }
    } else {
      (*pSql->fetchFp)(pSql->param, pSql, NULL);
    }
  } else {
    for (int i = 0; i < pCmd->numOfCols; ++i)
305
      pRes->tsrow[i] = TSC_GET_RESPTR_BASE(pRes, pQueryInfo, i, pQueryInfo->order) + pRes->bytes[i] * pRes->row;
H
hzcheng 已提交
306 307 308 309 310 311 312 313 314 315
    pRes->row++;

    (*pSql->fetchFp)(pSql->param, pSql, pSql->res.tsrow);
  }
}

void tscProcessFetchRow(SSchedMsg *pMsg) {
  SSqlObj *pSql = (SSqlObj *)pMsg->ahandle;
  SSqlRes *pRes = &pSql->res;
  SSqlCmd *pCmd = &pSql->cmd;
316 317 318 319
  assert(0);
  
  SQueryInfo *pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0);
  assert(pCmd->numOfCols == pQueryInfo->fieldsInfo.numOfOutputCols);
H
hzcheng 已提交
320 321

  for (int i = 0; i < pCmd->numOfCols; ++i)
322
    pRes->tsrow[i] = TSC_GET_RESPTR_BASE(pRes, pQueryInfo, i, pQueryInfo->order) + pRes->bytes[i] * pRes->row;
H
hzcheng 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416
  pRes->row++;

  (*pSql->fetchFp)(pSql->param, pSql, pRes->tsrow);
}

void tscProcessAsyncRes(SSchedMsg *pMsg) {
  SSqlObj *pSql = (SSqlObj *)pMsg->ahandle;
  STscObj *pTscObj = pSql->pTscObj;
  SSqlCmd *pCmd = &pSql->cmd;
  SSqlRes *pRes = &pSql->res;

  void *taosres = pSql;

  // pCmd may be released, so cache pCmd->command
  int cmd = pCmd->command;
  int code = pRes->code ? -pRes->code : pRes->numOfRows;

  if ((tscKeepConn[cmd] == 0 || (code != TSDB_CODE_SUCCESS && code != TSDB_CODE_ACTION_IN_PROGRESS)) &&
      pSql->pStream == NULL) {
    if (pSql->thandle) taosAddConnIntoCache(tscConnCache, pSql->thandle, pSql->ip, pSql->vnode, pTscObj->user);

    pSql->thandle = NULL;
  }

  // in case of async insert, restore the user specified callback function
  bool shouldFree = tscShouldFreeAsyncSqlObj(pSql);

  if (cmd == TSDB_SQL_INSERT) {
    assert(pSql->fp != NULL);
    pSql->fp = pSql->fetchFp;
  }

  (*pSql->fp)(pSql->param, taosres, code);

  if (shouldFree) {
    tscFreeSqlObj(pSql);
    tscTrace("%p Async sql is automatically freed in async res", pSql);
  }
}

void tscProcessAsyncError(SSchedMsg *pMsg) {
  void (*fp)() = pMsg->ahandle;

  (*fp)(pMsg->thandle, NULL, -1);
}

void tscQueueAsyncError(void(*fp), void *param) {
  SSchedMsg schedMsg;
  schedMsg.fp = tscProcessAsyncError;
  schedMsg.ahandle = fp;
  schedMsg.thandle = param;
  schedMsg.msg = NULL;
  taosScheduleTask(tscQhandle, &schedMsg);
}

void tscQueueAsyncRes(SSqlObj *pSql) {
  if (pSql == NULL || pSql->signature != pSql) {
    tscTrace("%p SqlObj is freed, not add into queue async res", pSql);
    return;
  } else {
    tscTrace("%p add into queued async res, code:%d", pSql, pSql->res.code);
  }

  SSchedMsg schedMsg;
  schedMsg.fp = tscProcessAsyncRes;
  schedMsg.ahandle = pSql;
  schedMsg.thandle = (void *)1;
  schedMsg.msg = NULL;
  taosScheduleTask(tscQhandle, &schedMsg);
}

void tscProcessAsyncFree(SSchedMsg *pMsg) {
  SSqlObj *pSql = (SSqlObj *)pMsg->ahandle;
  tscTrace("%p sql is freed", pSql);
  taos_free_result(pSql);
}

void tscQueueAsyncFreeResult(SSqlObj *pSql) {
  tscTrace("%p sqlObj put in queue to async free", pSql);

  SSchedMsg schedMsg;
  schedMsg.fp = tscProcessAsyncFree;
  schedMsg.ahandle = pSql;
  schedMsg.thandle = (void *)1;
  schedMsg.msg = NULL;
  taosScheduleTask(tscQhandle, &schedMsg);
}

void tscAsyncInsertMultiVnodesProxy(void *param, TAOS_RES *tres, int numOfRows) {
  SSqlObj *pSql = (SSqlObj *)param;
  SSqlCmd *pCmd = &pSql->cmd;
  int32_t  code = TSDB_CODE_SUCCESS;

  assert(!pCmd->isInsertFromFile && pSql->signature == pSql);
H
hjxilinx 已提交
417
  
418 419 420 421 422
  int32_t index = 0;
  SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, index);
  
  SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, index, 0);
  assert(pQueryInfo->numOfTables == 1);
H
hjxilinx 已提交
423
  
H
hzcheng 已提交
424
  SDataBlockList *pDataBlocks = pCmd->pDataBlocks;
H
hjxilinx 已提交
425
  if (pDataBlocks == NULL || pMeterMetaInfo->vnodeIndex >= pDataBlocks->nSize) {
H
hzcheng 已提交
426 427 428 429 430
    // restore user defined fp
    pSql->fp = pSql->fetchFp;
    tscTrace("%p Async insertion completed, destroy data block list", pSql);

    // release data block data
S
slguan 已提交
431
    pCmd->pDataBlocks = tscDestroyBlockArrayList(pCmd->pDataBlocks);
H
hzcheng 已提交
432 433 434 435 436

    // all data has been sent to vnode, call user function
    (*pSql->fp)(pSql->param, tres, numOfRows);
  } else {
    do {
H
hjxilinx 已提交
437
      code = tscCopyDataBlockToPayload(pSql, pDataBlocks->pData[pMeterMetaInfo->vnodeIndex++]);
H
hzcheng 已提交
438 439
      if (code != TSDB_CODE_SUCCESS) {
        tscTrace("%p prepare submit data block failed in async insertion, vnodeIdx:%d, total:%d, code:%d",
H
hjxilinx 已提交
440
                 pSql, pMeterMetaInfo->vnodeIndex - 1, pDataBlocks->nSize, code);
H
hzcheng 已提交
441 442
      }

H
hjxilinx 已提交
443
    } while (code != TSDB_CODE_SUCCESS && pMeterMetaInfo->vnodeIndex < pDataBlocks->nSize);
H
hzcheng 已提交
444 445 446

    // build submit msg may fail
    if (code == TSDB_CODE_SUCCESS) {
H
hjxilinx 已提交
447
      tscTrace("%p async insertion, vnodeIdx:%d, total:%d", pSql, pMeterMetaInfo->vnodeIndex - 1, pDataBlocks->nSize);
H
hzcheng 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474
      tscProcessSql(pSql);
    }
  }
}

int tscSendMsgToServer(SSqlObj *pSql);

void tscMeterMetaCallBack(void *param, TAOS_RES *res, int code) {
  SSqlObj *pSql = (SSqlObj *)param;
  if (pSql == NULL || pSql->signature != pSql) return;

  STscObj *pObj = pSql->pTscObj;
  SSqlCmd *pCmd = &pSql->cmd;
  SSqlRes *pRes = &pSql->res;

  if (pSql->fp == NULL) {
    tscError("%p callBack is NULL!!!", pSql);
    return;
  }

  if (pSql->fp == (void *)1) {
    pSql->fp = NULL;

    if (code != 0) {
      code = abs(code);
      pRes->code = code;
      tscTrace("%p failed to renew meterMeta", pSql);
S
slguan 已提交
475
      tsem_post(&pSql->rspSem);
H
hzcheng 已提交
476 477
    } else {
      tscTrace("%p renew meterMeta successfully, command:%d, code:%d, thandle:%p, retry:%d",
S
slguan 已提交
478
          pSql, pSql->cmd.command, pSql->res.code, pSql->thandle, pSql->retry);
479 480 481 482 483
  
      SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfo(&pSql->cmd, 0, 0);
      assert(pMeterMetaInfo->pMeterMeta == NULL);
      
      tscGetMeterMeta(pSql, pMeterMetaInfo->name, 0);
H
hzcheng 已提交
484 485 486
      code = tscSendMsgToServer(pSql);
      if (code != 0) {
        pRes->code = code;
S
slguan 已提交
487
        tsem_post(&pSql->rspSem);
H
hzcheng 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500 501
      }
    }

    return;
  }

  if (code != 0) {
    pRes->code = (uint8_t)abs(code);
    tscQueueAsyncRes(pSql);
    return;
  }

  if (pSql->pStream == NULL) {
    // check if it is a sub-query of metric query first, if true, enter another routine
502 503 504 505
    SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(&pSql->cmd, 0);
  
    if ((pQueryInfo->type & TSDB_QUERY_TYPE_STABLE_SUBQUERY) == TSDB_QUERY_TYPE_STABLE_SUBQUERY) {
      SMeterMetaInfo* pMeterMetaInfo = tscGetMeterMetaInfoFromQueryInfo(pQueryInfo, 0);
H
hjxilinx 已提交
506
      assert(pMeterMetaInfo->pMeterMeta->numOfTags != 0 && pMeterMetaInfo->vnodeIndex >= 0 && pSql->param != NULL);
H
hzcheng 已提交
507 508 509

      SRetrieveSupport *trs = (SRetrieveSupport *)pSql->param;
      SSqlObj *         pParObj = trs->pParentSqlObj;
510
      
H
hjxilinx 已提交
511
      assert(pParObj->signature == pParObj && trs->subqueryIndex == pMeterMetaInfo->vnodeIndex &&
S
slguan 已提交
512 513
          pMeterMetaInfo->pMeterMeta->numOfTags != 0);

H
hzcheng 已提交
514
      tscTrace("%p get metricMeta during metric query successfully", pSql);
515 516
      
      code = tscGetMeterMeta(pSql, pMeterMetaInfo->name, 0);
H
hzcheng 已提交
517 518 519 520
      pRes->code = code;

      if (code == TSDB_CODE_ACTION_IN_PROGRESS) return;

S
slguan 已提交
521
      code = tscGetMetricMeta(pSql);
H
hzcheng 已提交
522 523 524 525
      pRes->code = code;

      if (code == TSDB_CODE_ACTION_IN_PROGRESS) return;
    } else {  // normal async query continues
526
      code = tsParseSql(pSql, false);
H
hzcheng 已提交
527 528
      if (code == TSDB_CODE_ACTION_IN_PROGRESS) return;
    }
S
slguan 已提交
529

H
hzcheng 已提交
530
  } else {  // stream computing
531
    SMeterMetaInfo *pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0);
S
slguan 已提交
532
    code = tscGetMeterMeta(pSql, pMeterMetaInfo->name, 0);
H
hzcheng 已提交
533 534 535 536
    pRes->code = code;

    if (code == TSDB_CODE_ACTION_IN_PROGRESS) return;

537 538
    pMeterMetaInfo = tscGetMeterMetaInfo(pCmd, 0, 0);
    if (code == TSDB_CODE_SUCCESS && UTIL_METER_IS_SUPERTABLE(pMeterMetaInfo)) {
S
slguan 已提交
539
      code = tscGetMetricMeta(pSql);
H
hzcheng 已提交
540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555
      pRes->code = code;

      if (code == TSDB_CODE_ACTION_IN_PROGRESS) return;
    }
  }

  if (code != 0) {
    tscQueueAsyncRes(pSql);
    return;
  }

  if (pSql->pStream) {
    tscTrace("%p stream:%p meta is updated, start new query, command:%d", pSql, pSql->pStream, pSql->cmd.command);
    /*
     * NOTE:
     * transfer the sql function for metric query before get meter/metric meta,
556
     * since in callback functions, only tscProcessSql(pStream->pSql) is executed!
H
hzcheng 已提交
557
     */
558 559 560
    SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0);
    
    tscTansformSQLFunctionForMetricQuery(pQueryInfo);
H
hzcheng 已提交
561 562 563 564 565 566 567
    tscIncStreamExecutionCount(pSql->pStream);
  } else {
    tscTrace("%p get meterMeta/metricMeta successfully", pSql);
  }

  tscDoQuery(pSql);
}