From a52c4bec8ce682b9ea9ce85171969e5e3bf99749 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Thu, 11 Jun 2020 13:20:20 +0800 Subject: [PATCH] [td-592]refactor codes --- src/client/src/tscSql.c | 56 ++++++++++++++++++++++------------------- 1 file changed, 30 insertions(+), 26 deletions(-) diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index 5f2a8598db..351a0ad7ef 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -475,6 +475,33 @@ int taos_select_db(TAOS *taos, const char *db) { return code; } +// send free message to vnode to free qhandle and corresponding resources in vnode +static void tscFreeQhandleInVnode(SSqlObj* pSql) { + SSqlCmd* pCmd = &pSql->cmd; + SSqlRes* pRes = &pSql->res; + + SQueryInfo* pQueryInfo = tscGetQueryInfoDetail(pCmd, 0); + STableMetaInfo *pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); + + if (pRes->code == TSDB_CODE_SUCCESS && pRes->completed == false && !tscIsTwoStageSTableQuery(pQueryInfo, 0) && + (pCmd->command == TSDB_SQL_SELECT || + pCmd->command == TSDB_SQL_SHOW || + pCmd->command == TSDB_SQL_RETRIEVE || + pCmd->command == TSDB_SQL_FETCH) && + (pCmd->command == TSDB_SQL_SELECT && pSql->pStream == NULL && pTableMetaInfo->pTableMeta != NULL)) { + + pCmd->command = (pCmd->command > TSDB_SQL_MGMT) ? TSDB_SQL_RETRIEVE : TSDB_SQL_FETCH; + tscTrace("%p start to send msg to free qhandle in dnode, command:%s", pSql, sqlCmd[pCmd->command]); + pSql->freed = 1; + tscProcessSql(pSql); + + // in case of sync model query, waits for response and then goes on + if (pSql->fp == waitForQueryRsp || pSql->fp == waitForRetrieveRsp) { + sem_wait(&pSql->rspSem); + } + } +} + void taos_free_result(TAOS_RES *res) { SSqlObj *pSql = (SSqlObj *)res; tscTrace("%p start to free result", res); @@ -484,10 +511,8 @@ void taos_free_result(TAOS_RES *res) { return; } - SSqlRes *pRes = &pSql->res; - SSqlCmd *pCmd = &pSql->cmd; - // The semaphore can not be changed while freeing async sub query objects. + SSqlRes *pRes = &pSql->res; if (pRes == NULL || pRes->qhandle == 0) { tscTrace("%p SqlObj is freed by app, qhandle is null", pSql); tscFreeSqlObj(pSql); @@ -502,30 +527,9 @@ void taos_free_result(TAOS_RES *res) { } pQueryInfo->type = TSDB_QUERY_TYPE_FREE_RESOURCE; - STableMetaInfo *pTableMetaInfo = tscGetMetaInfo(pQueryInfo, 0); - - /* - * If the query process is cancelled by user in stable query, tscProcessSql should not be called - * for each subquery. Because the failure of execution tsProcessSql may trigger the callback function - * be executed, and the retry efforts may result in double free the resources, e.g.,SRetrieveSupport - */ - if (pRes->code == TSDB_CODE_SUCCESS && pRes->completed == false && - (pCmd->command == TSDB_SQL_SELECT || pCmd->command == TSDB_SQL_SHOW || - pCmd->command == TSDB_SQL_RETRIEVE || pCmd->command == TSDB_SQL_FETCH) && - (pCmd->command == TSDB_SQL_SELECT && pSql->pStream == NULL && pTableMetaInfo->pTableMeta != NULL)) { - pCmd->command = (pCmd->command > TSDB_SQL_MGMT) ? TSDB_SQL_RETRIEVE : TSDB_SQL_FETCH; - - tscTrace("%p start to send msg to free qhandle in dnode, command:%s", pSql, sqlCmd[pCmd->command]); - pSql->freed = 1; - tscProcessSql(pSql); - - // in case of sync model query, waits for response and then goes on - if (pSql->fp == waitForQueryRsp || pSql->fp == waitForRetrieveRsp) { - sem_wait(&pSql->rspSem); - } - } - + tscFreeQhandleInVnode(pSql); tscFreeSqlObj(pSql); + tscTrace("%p sql result is freed by app", pSql); } -- GitLab