From b6c38a4f1bbc1e6aae2f1c13c2c8212c742f8e83 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Sun, 26 Jun 2022 15:13:22 +0800 Subject: [PATCH 01/36] enh: stop query process --- source/client/inc/clientInt.h | 3 +- source/client/src/clientEnv.c | 15 +- source/client/src/clientHb.c | 2 +- source/client/src/clientImpl.c | 28 ++- source/libs/catalog/src/ctgCache.c | 2 - source/libs/catalog/src/ctgDbg.c | 2 +- source/libs/parser/src/parInsert.c | 2 +- source/libs/scheduler/inc/schedulerInt.h | 28 +-- source/libs/scheduler/src/schJob.c | 144 +++++------- source/libs/scheduler/src/scheduler.c | 60 +++-- tests/script/api/batchprepare.c | 22 +- tests/script/api/makefile | 1 + tests/script/api/stopquery.c | 268 +++++++++++++++++++++++ 13 files changed, 422 insertions(+), 155 deletions(-) create mode 100644 tests/script/api/stopquery.c diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index cca79186d0..63f6486197 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -183,7 +183,7 @@ typedef struct SRequestSendRecvBody { void* param; SDataBuf requestMsg; int64_t queryJob; // query job, created according to sql query DAG. - struct SQueryPlan* pDag; // the query dag, generated according to the sql statement. + int32_t subplanNum; SReqResultInfo resInfo; } SRequestSendRecvBody; @@ -300,6 +300,7 @@ void* createRequest(STscObj* pObj, int32_t type); void destroyRequest(SRequestObj* pRequest); SRequestObj* acquireRequest(int64_t rid); int32_t releaseRequest(int64_t rid); +int32_t removeRequest(int64_t rid); char* getDbOfConnection(STscObj* pObj); void setConnectionDB(STscObj* pTscObj, const char* db); diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 9f04e89694..52806343af 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -119,7 +119,7 @@ void closeAllRequests(SHashObj *pRequests) { while (pIter != NULL) { int64_t *rid = pIter; - releaseRequest(*rid); + removeRequest(*rid); pIter = taosHashIterate(pRequests, pIter); } @@ -222,6 +222,12 @@ void doFreeReqResultInfo(SReqResultInfo *pResInfo) { } } +SRequestObj *acquireRequest(int64_t rid) { return (SRequestObj *)taosAcquireRef(clientReqRefPool, rid); } + +int32_t releaseRequest(int64_t rid) { return taosReleaseRef(clientReqRefPool, rid); } + +int32_t removeRequest(int64_t rid) { return taosRemoveRef(clientReqRefPool, rid); } + static void doDestroyRequest(void *p) { assert(p != NULL); SRequestObj *pRequest = (SRequestObj *)p; @@ -239,7 +245,6 @@ static void doDestroyRequest(void *p) { taosMemoryFreeClear(pRequest->pDb); doFreeReqResultInfo(&pRequest->body.resInfo); - qDestroyQueryPlan(pRequest->body.pDag); taosArrayDestroy(pRequest->tableList); taosArrayDestroy(pRequest->dbList); @@ -255,13 +260,9 @@ void destroyRequest(SRequestObj *pRequest) { return; } - taosRemoveRef(clientReqRefPool, pRequest->self); + removeRequest(pRequest->self); } -SRequestObj *acquireRequest(int64_t rid) { return (SRequestObj *)taosAcquireRef(clientReqRefPool, rid); } - -int32_t releaseRequest(int64_t rid) { return taosReleaseRef(clientReqRefPool, rid); } - 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. diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index 86562fea97..0681b5d0a5 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -320,7 +320,7 @@ int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) { desc.reqRid = pRequest->self; desc.stableQuery = pRequest->stableQuery; taosGetFqdn(desc.fqdn); - desc.subPlanNum = pRequest->body.pDag ? pRequest->body.pDag->numOfSubplans : 0; + desc.subPlanNum = pRequest->body.subplanNum; if (desc.subPlanNum) { desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc)); diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index b3aaeaea78..a3c818adae 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -876,13 +876,17 @@ SRequestObj* launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, bool keepQue break; case QUERY_EXEC_MODE_SCHEDULE: { SArray* pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad)); - code = getPlan(pRequest, pQuery, &pRequest->body.pDag, pMnodeList); - if (TSDB_CODE_SUCCESS == code && !pRequest->validateOnly) { - SArray* pNodeList = NULL; - buildSyncExecNodeList(pRequest, &pNodeList, pMnodeList); - - code = scheduleQuery(pRequest, pRequest->body.pDag, pNodeList); - taosArrayDestroy(pNodeList); + SQueryPlan* pDag = NULL; + code = getPlan(pRequest, pQuery, &pDag, pMnodeList); + if (TSDB_CODE_SUCCESS == code) { + pRequest->body.subplanNum = pDag->numOfSubplans; + if (!pRequest->validateOnly) { + SArray* pNodeList = NULL; + buildSyncExecNodeList(pRequest, &pNodeList, pMnodeList); + + code = scheduleQuery(pRequest, pDag, pNodeList); + taosArrayDestroy(pNodeList); + } } taosArrayDestroy(pMnodeList); break; @@ -959,10 +963,13 @@ void launchAsyncQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData *pResultM .msgLen = ERROR_MSG_BUF_DEFAULT_SIZE}; SAppInstInfo* pAppInfo = getAppInfo(pRequest); - code = qCreateQueryPlan(&cxt, &pRequest->body.pDag, pMnodeList); + SQueryPlan* pDag = NULL; + code = qCreateQueryPlan(&cxt, &pDag, pMnodeList); if (code) { tscError("0x%" PRIx64 " failed to create query plan, code:%s 0x%" PRIx64, pRequest->self, tstrerror(code), pRequest->requestId); + } else { + pRequest->body.subplanNum = pDag->numOfSubplans; } if (TSDB_CODE_SUCCESS == code && !pRequest->validateOnly) { @@ -973,7 +980,7 @@ void launchAsyncQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData *pResultM .pTrans = pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self}; SSchedulerReq req = {.pConn = &conn, .pNodeList = pNodeList, - .pDag = pRequest->body.pDag, + .pDag = pDag, .sql = pRequest->sqlstr, .startTs = pRequest->metric.start, .fp = schedulerExecCb, @@ -2026,6 +2033,7 @@ void taosAsyncQueryImpl(TAOS *taos, const char *sql, __taos_async_fn_t fp, void if (sqlLen > (size_t)TSDB_MAX_ALLOWED_SQL_LEN) { tscError("sql string exceeds max length:%d", TSDB_MAX_ALLOWED_SQL_LEN); terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT; + releaseTscObj(*(int64_t *)taos); fp(param, NULL, terrno); return; @@ -2035,6 +2043,7 @@ void taosAsyncQueryImpl(TAOS *taos, const char *sql, __taos_async_fn_t fp, void int32_t code = buildRequest(pTscObj, sql, sqlLen, &pRequest); if (code != TSDB_CODE_SUCCESS) { terrno = code; + releaseTscObj(*(int64_t *)taos); fp(param, NULL, terrno); return; } @@ -2043,6 +2052,7 @@ void taosAsyncQueryImpl(TAOS *taos, const char *sql, __taos_async_fn_t fp, void pRequest->body.queryFp = fp; pRequest->body.param = param; doAsyncQuery(pRequest, false); + releaseTscObj(*(int64_t *)taos); } diff --git a/source/libs/catalog/src/ctgCache.c b/source/libs/catalog/src/ctgCache.c index 9c9aa4001c..8cd6c7d203 100644 --- a/source/libs/catalog/src/ctgCache.c +++ b/source/libs/catalog/src/ctgCache.c @@ -1536,8 +1536,6 @@ void ctgClearAllInstance(void) { pIter = taosHashIterate(gCtgMgmt.pCluster, pIter); } - - taosHashClear(gCtgMgmt.pCluster); } void ctgFreeAllInstance(void) { diff --git a/source/libs/catalog/src/ctgDbg.c b/source/libs/catalog/src/ctgDbg.c index 2cb6f0209f..9195747bee 100644 --- a/source/libs/catalog/src/ctgDbg.c +++ b/source/libs/catalog/src/ctgDbg.c @@ -19,7 +19,7 @@ #include "catalogInt.h" extern SCatalogMgmt gCtgMgmt; -SCtgDebug gCTGDebug = {.lockEnable = true, .apiEnable = true}; +SCtgDebug gCTGDebug = {0}; void ctgdUserCallback(SMetaData* pResult, void* param, int32_t code) { ASSERT(*(int32_t*)param == 1); diff --git a/source/libs/parser/src/parInsert.c b/source/libs/parser/src/parInsert.c index 5d34250444..7cd8398f86 100644 --- a/source/libs/parser/src/parInsert.c +++ b/source/libs/parser/src/parInsert.c @@ -1757,7 +1757,7 @@ int32_t qBindStmtTagsValue(void* pBlock, void* boundTags, int64_t suid, char* tN } int32_t code = TSDB_CODE_SUCCESS; - SSchema* pSchema = pDataBlock->pTableMeta->schema; + SSchema* pSchema = getTableTagSchema(pDataBlock->pTableMeta); bool isJson = false; STag* pTag = NULL; diff --git a/source/libs/scheduler/inc/schedulerInt.h b/source/libs/scheduler/inc/schedulerInt.h index 6b2570c5b7..843ce7d55a 100644 --- a/source/libs/scheduler/inc/schedulerInt.h +++ b/source/libs/scheduler/inc/schedulerInt.h @@ -212,7 +212,7 @@ typedef struct SSchJob { SRequestConnInfo conn; SArray *nodeList; // qnode/vnode list, SArray SArray *levels; // starting from 0. SArray - SNodeList *subPlans; // subplan pointer copied from DAG, no need to free it in scheduler + SQueryPlan *pDag; SArray *dataSrcTasks; // SArray int32_t levelIdx; @@ -334,13 +334,13 @@ extern SSchedulerMgmt schMgmt; #define SCH_UNLOCK(type, _lock) (SCH_READ == (type) ? taosRUnLockLatch(_lock) : taosWUnLockLatch(_lock)) -void schDeregisterTaskHb(SSchJob *pJob, SSchTask *pTask); -void schCleanClusterHb(void* pTrans); +void schDeregisterTaskHb(SSchJob *pJob, SSchTask *pTask); +void schCleanClusterHb(void* pTrans); int32_t schLaunchTask(SSchJob *job, SSchTask *task); int32_t schBuildAndSendMsg(SSchJob *job, SSchTask *task, SQueryNodeAddr *addr, int32_t msgType); SSchJob *schAcquireJob(int64_t refId); int32_t schReleaseJob(int64_t refId); -void schFreeFlowCtrl(SSchJob *pJob); +void schFreeFlowCtrl(SSchJob *pJob); int32_t schChkJobNeedFlowCtrl(SSchJob *pJob, SSchLevel *pLevel); int32_t schDecTaskFlowQuota(SSchJob *pJob, SSchTask *pTask); int32_t schCheckIncTaskFlowQuota(SSchJob *pJob, SSchTask *pTask, bool *enough); @@ -351,38 +351,40 @@ int32_t schProcessOnTaskFailure(SSchJob *pJob, SSchTask *pTask, int32_t errCode) int32_t schBuildAndSendHbMsg(SQueryNodeEpId *nodeEpId, SArray* taskAction); int32_t schCloneSMsgSendInfo(void *src, void **dst); int32_t schValidateAndBuildJob(SQueryPlan *pDag, SSchJob *pJob); -void schFreeJobImpl(void *job); +void schFreeJobImpl(void *job); int32_t schMakeHbRpcCtx(SSchJob *pJob, SSchTask *pTask, SRpcCtx *pCtx); int32_t schEnsureHbConnection(SSchJob *pJob, SSchTask *pTask); int32_t schUpdateHbConnection(SQueryNodeEpId *epId, SSchTrans *trans); int32_t schHandleHbCallback(void *param, const SDataBuf *pMsg, int32_t code); -void schFreeRpcCtx(SRpcCtx *pCtx); +void schFreeRpcCtx(SRpcCtx *pCtx); int32_t schGetCallbackFp(int32_t msgType, __async_send_cb_fn_t *fp); -bool schJobNeedToStop(SSchJob *pJob, int8_t *pStatus); +bool schJobNeedToStop(SSchJob *pJob, int8_t *pStatus); int32_t schProcessOnTaskSuccess(SSchJob *pJob, SSchTask *pTask); int32_t schSaveJobQueryRes(SSchJob *pJob, SQueryTableRsp *rsp); int32_t schProcessOnExplainDone(SSchJob *pJob, SSchTask *pTask, SRetrieveTableRsp *pRsp); -void schProcessOnDataFetched(SSchJob *job); +void schProcessOnDataFetched(SSchJob *job); int32_t schGetTaskInJob(SSchJob *pJob, uint64_t taskId, SSchTask **pTask); -void schFreeRpcCtxVal(const void *arg); +void schFreeRpcCtxVal(const void *arg); int32_t schMakeBrokenLinkVal(SSchJob *pJob, SSchTask *pTask, SRpcBrokenlinkVal *brokenVal, bool isHb); int32_t schAppendTaskExecNode(SSchJob *pJob, SSchTask *pTask, SQueryNodeAddr *addr, int32_t execIdx); int32_t schExecStaticExplainJob(SSchedulerReq *pReq, int64_t *job, bool sync); -int32_t schExecJobImpl(SSchedulerReq *pReq, int64_t *job, SQueryResult* pRes, bool sync); +int32_t schExecJobImpl(SSchedulerReq *pReq, SSchJob *pJob, bool sync); int32_t schUpdateJobStatus(SSchJob *pJob, int8_t newStatus); int32_t schCancelJob(SSchJob *pJob); int32_t schProcessOnJobDropped(SSchJob *pJob, int32_t errCode); uint64_t schGenTaskId(void); -void schCloseJobRef(void); +void schCloseJobRef(void); int32_t schExecJob(SSchedulerReq *pReq, int64_t *pJob, SQueryResult *pRes); int32_t schAsyncExecJob(SSchedulerReq *pReq, int64_t *pJob); int32_t schFetchRows(SSchJob *pJob); int32_t schAsyncFetchRows(SSchJob *pJob); int32_t schUpdateTaskHandle(SSchJob *pJob, SSchTask *pTask, bool dropExecNode, void *handle, int32_t execIdx); int32_t schProcessOnTaskStatusRsp(SQueryNodeEpId* pEpId, SArray* pStatusList); -void schFreeSMsgSendInfo(SMsgSendInfo *msgSendInfo); -char* schGetOpStr(SCH_OP_TYPE type); +void schFreeSMsgSendInfo(SMsgSendInfo *msgSendInfo); +char* schGetOpStr(SCH_OP_TYPE type); int32_t schBeginOperation(SSchJob *pJob, SCH_OP_TYPE type, bool sync); +int32_t schInitJob(SSchedulerReq *pReq, SSchJob **pSchJob); +int32_t schSetJobQueryRes(SSchJob* pJob, SQueryResult* pRes); #ifdef __cplusplus diff --git a/source/libs/scheduler/src/schJob.c b/source/libs/scheduler/src/schJob.c index 72809e1f93..7843482af3 100644 --- a/source/libs/scheduler/src/schJob.c +++ b/source/libs/scheduler/src/schJob.c @@ -42,7 +42,7 @@ int32_t schInitTask(SSchJob *pJob, SSchTask *pTask, SSubplan *pPlan, SSchLevel * return TSDB_CODE_SUCCESS; } -int32_t schInitJob(SSchedulerReq *pReq, SSchJob **pSchJob, SQueryResult* pRes, bool syncSchedule) { +int32_t schInitJob(SSchedulerReq *pReq, SSchJob **pSchJob) { int32_t code = 0; int64_t refId = -1; SSchJob *pJob = taosMemoryCalloc(1, sizeof(SSchJob)); @@ -54,12 +54,14 @@ int32_t schInitJob(SSchedulerReq *pReq, SSchJob **pSchJob, SQueryResult* pRes, b pJob->attr.explainMode = pReq->pDag->explainInfo.mode; pJob->conn = *pReq->pConn; pJob->sql = pReq->sql; + pJob->pDag = pReq->pDag; pJob->reqKilled = pReq->reqKilled; - pJob->userRes.queryRes = pRes; pJob->userRes.execFp = pReq->fp; pJob->userRes.userParam = pReq->cbParam; - - if (pReq->pNodeList != NULL) { + + if (pReq->pNodeList == NULL || taosArrayGetSize(pReq->pNodeList) <= 0) { + qDebug("QID:0x%" PRIx64 " input exec nodeList is empty", pReq->pDag->queryId); + } else { pJob->nodeList = taosArrayDup(pReq->pNodeList); } @@ -547,8 +549,6 @@ int32_t schValidateAndBuildJob(SQueryPlan *pDag, SSchJob *pJob) { pJob->levelNum = levelNum; pJob->levelIdx = levelNum - 1; - pJob->subPlans = pDag->pSubplans; - SSchLevel level = {0}; SNodeListNode *plans = NULL; int32_t taskNum = 0; @@ -1491,8 +1491,6 @@ void schFreeJobImpl(void *job) { schDropJobAllTasks(pJob); - pJob->subPlans = NULL; // it is a reference to pDag->pSubplans - int32_t numOfLevels = taosArrayGetSize(pJob->levels); for (int32_t i = 0; i < numOfLevels; ++i) { SSchLevel *pLevel = taosArrayGet(pJob->levels, i); @@ -1521,6 +1519,8 @@ void schFreeJobImpl(void *job) { destroyQueryExecRes(&pJob->execRes); + qDestroyQueryPlan(pJob->pDag); + taosMemoryFreeClear(pJob->userRes.queryRes); taosMemoryFreeClear(pJob->resData); taosMemoryFree(pJob); @@ -1533,88 +1533,11 @@ void schFreeJobImpl(void *job) { } } -int32_t schExecJobImpl(SSchedulerReq *pReq, int64_t *job, SQueryResult* pRes, bool sync) { - if (pReq->pNodeList == NULL || taosArrayGetSize(pReq->pNodeList) <= 0) { - qDebug("QID:0x%" PRIx64 " input exec nodeList is empty", pReq->pDag->queryId); - } - - int32_t code = 0; - SSchJob *pJob = NULL; - SCH_ERR_JRET(schInitJob(pReq, &pJob, pRes, sync)); - - qDebug("QID:0x%" PRIx64 " sch job refId 0x%"PRIx64 " started", pReq->pDag->queryId, pJob->refId); - *job = pJob->refId; - - SCH_ERR_JRET(schBeginOperation(pJob, SCH_OP_EXEC, sync)); - - code = schLaunchJob(pJob); - - if (sync) { - SCH_JOB_DLOG("will wait for rsp now, job status:%s", SCH_GET_JOB_STATUS_STR(pJob)); - tsem_wait(&pJob->rspSem); - - schEndOperation(pJob); - } else if (code) { - schPostJobRes(pJob, SCH_OP_EXEC); - } - - SCH_JOB_DLOG("job exec done, job status:%s, jobId:0x%" PRIx64, SCH_GET_JOB_STATUS_STR(pJob), pJob->refId); - - schReleaseJob(pJob->refId); - - SCH_RET(code); - -_return: - - if (!sync) { - pReq->fp(NULL, pReq->cbParam, code); - } - - schReleaseJob(pJob->refId); - - SCH_RET(code); -} - -int32_t schExecJob(SSchedulerReq *pReq, int64_t *pJob, SQueryResult *pRes) { - int32_t code = 0; - - *pJob = 0; - - if (EXPLAIN_MODE_STATIC == pReq->pDag->explainInfo.mode) { - SCH_ERR_JRET(schExecStaticExplainJob(pReq, pJob, true)); - } else { - SCH_ERR_JRET(schExecJobImpl(pReq, pJob, NULL, true)); - } - -_return: - - if (*pJob) { - SSchJob *job = schAcquireJob(*pJob); - schSetJobQueryRes(job, pRes); - schReleaseJob(*pJob); - } - - return code; -} - -int32_t schAsyncExecJob(SSchedulerReq *pReq, int64_t *pJob) { - int32_t code = 0; - - *pJob = 0; - - if (EXPLAIN_MODE_STATIC == pReq->pDag->explainInfo.mode) { - SCH_RET(schExecStaticExplainJob(pReq, pJob, false)); - } - - SCH_ERR_RET(schExecJobImpl(pReq, pJob, NULL, false)); - - return code; -} - -int32_t schExecStaticExplainJob(SSchedulerReq *pReq, int64_t *job, bool sync) { +int32_t schLaunchStaticExplainJob(SSchedulerReq *pReq, SSchJob *pJob, bool sync) { qDebug("QID:0x%" PRIx64 " job started", pReq->pDag->queryId); int32_t code = 0; +/* SSchJob *pJob = taosMemoryCalloc(1, sizeof(SSchJob)); if (NULL == pJob) { qError("QID:0x%" PRIx64 " calloc %d failed", pReq->pDag->queryId, (int32_t)sizeof(SSchJob)); @@ -1625,10 +1548,10 @@ int32_t schExecStaticExplainJob(SSchedulerReq *pReq, int64_t *job, bool sync) { pJob->sql = pReq->sql; pJob->reqKilled = pReq->reqKilled; + pJob->pDag = pReq->pDag; pJob->attr.queryJob = true; pJob->attr.explainMode = pReq->pDag->explainInfo.mode; pJob->queryId = pReq->pDag->queryId; - pJob->subPlans = pReq->pDag->pSubplans; pJob->userRes.execFp = pReq->fp; pJob->userRes.userParam = pReq->cbParam; @@ -1637,11 +1560,14 @@ int32_t schExecStaticExplainJob(SSchedulerReq *pReq, int64_t *job, bool sync) { code = schBeginOperation(pJob, SCH_OP_EXEC, sync); if (code) { pReq->fp(NULL, pReq->cbParam, code); + schFreeJobImpl(pJob); SCH_ERR_RET(code); } - +*/ + SCH_ERR_JRET(qExecStaticExplain(pReq->pDag, (SRetrieveTableRsp **)&pJob->resData)); +/* int64_t refId = taosAddRef(schMgmt.jobRef, pJob); if (refId < 0) { SCH_JOB_ELOG("taosAddRef job failed, error:%s", tstrerror(terrno)); @@ -1656,10 +1582,10 @@ int32_t schExecStaticExplainJob(SSchedulerReq *pReq, int64_t *job, bool sync) { pJob->refId = refId; SCH_JOB_DLOG("job refId:0x%" PRIx64, pJob->refId); +*/ pJob->status = JOB_TASK_STATUS_PARTIAL_SUCCEED; - *job = pJob->refId; SCH_JOB_DLOG("job exec done, job status:%s", SCH_GET_JOB_STATUS_STR(pJob)); if (!sync) { @@ -1668,7 +1594,7 @@ int32_t schExecStaticExplainJob(SSchedulerReq *pReq, int64_t *job, bool sync) { schEndOperation(pJob); } - schReleaseJob(pJob->refId); +// schReleaseJob(pJob->refId); SCH_RET(code); @@ -1715,3 +1641,39 @@ int32_t schAsyncFetchRows(SSchJob *pJob) { } +int32_t schExecJobImpl(SSchedulerReq *pReq, SSchJob *pJob, bool sync) { + int32_t code = 0; + + qDebug("QID:0x%" PRIx64 " sch job refId 0x%"PRIx64 " started", pReq->pDag->queryId, pJob->refId); + + SCH_ERR_JRET(schBeginOperation(pJob, SCH_OP_EXEC, sync)); + + if (EXPLAIN_MODE_STATIC == pReq->pDag->explainInfo.mode) { + code = schLaunchStaticExplainJob(pReq, pJob, true); + } else { + code = schLaunchJob(pJob); + if (sync) { + SCH_JOB_DLOG("will wait for rsp now, job status:%s", SCH_GET_JOB_STATUS_STR(pJob)); + tsem_wait(&pJob->rspSem); + + schEndOperation(pJob); + } else if (code) { + schPostJobRes(pJob, SCH_OP_EXEC); + } + } + + SCH_JOB_DLOG("job exec done, job status:%s, jobId:0x%" PRIx64, SCH_GET_JOB_STATUS_STR(pJob), pJob->refId); + + SCH_RET(code); + +_return: + + if (!sync) { + pReq->fp(NULL, pReq->cbParam, code); + } + + SCH_RET(code); +} + + + diff --git a/source/libs/scheduler/src/scheduler.c b/source/libs/scheduler/src/scheduler.c index 57a405ffa3..15a687531c 100644 --- a/source/libs/scheduler/src/scheduler.c +++ b/source/libs/scheduler/src/scheduler.c @@ -67,33 +67,57 @@ int32_t schedulerInit(SSchedulerCfg *cfg) { return TSDB_CODE_SUCCESS; } -int32_t schedulerExecJob(SSchedulerReq *pReq, int64_t *pJob, SQueryResult *pRes) { +int32_t schedulerExecJob(SSchedulerReq *pReq, int64_t *pJobId, SQueryResult *pRes) { qDebug("scheduler sync exec job start"); + + int32_t code = 0; + SSchJob *pJob = NULL; + SCH_ERR_JRET(schInitJob(pReq, &pJob)); + + *pJobId = pJob->refId; - if (NULL == pReq || NULL == pJob || NULL == pRes) { - SCH_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); - } - - SCH_RET(schExecJob(pReq, pJob, pRes)); -} - -int32_t schedulerAsyncExecJob(SSchedulerReq *pReq, int64_t *pJob) { - qDebug("scheduler async exec job start"); - - int32_t code = 0; - if (NULL == pReq || NULL == pJob) { - SCH_ERR_JRET(TSDB_CODE_QRY_INVALID_INPUT); - } - - schAsyncExecJob(pReq, pJob); + SCH_ERR_JRET(schExecJobImpl(pReq, pJob, true)); _return: + if (code && NULL == pJob) { + qDestroyQueryPlan(pReq->pDag); + } + + if (pJob) { + schSetJobQueryRes(pJob, pRes); + schReleaseJob(pJob->refId); + } + + return code; +} + +int32_t schedulerAsyncExecJob(SSchedulerReq *pReq, int64_t *pJobId) { + qDebug("scheduler async exec job start"); + + int32_t code = 0; + SSchJob *pJob = NULL; + SCH_ERR_JRET(schInitJob(pReq, &pJob)); + + *pJobId = pJob->refId; + + SCH_ERR_JRET(schExecJobImpl(pReq, pJob, false)); + +_return: + + if (code && NULL == pJob) { + qDestroyQueryPlan(pReq->pDag); + } + + if (pJob) { + schReleaseJob(pJob->refId); + } + if (code != TSDB_CODE_SUCCESS) { pReq->fp(NULL, pReq->cbParam, code); } - SCH_RET(code); + return code; } int32_t schedulerFetchRows(int64_t job, void **pData) { diff --git a/tests/script/api/batchprepare.c b/tests/script/api/batchprepare.c index 0e7030b230..b31c39718c 100644 --- a/tests/script/api/batchprepare.c +++ b/tests/script/api/batchprepare.c @@ -915,7 +915,7 @@ int32_t prepareInsertData(BindData *data) { data->colNum = 0; data->colTypes = taosMemoryCalloc(30, sizeof(int32_t)); data->sql = taosMemoryCalloc(1, 1024); - data->pBind = taosMemoryCalloc((allRowNum/gCurCase->bindRowNum)*gCurCase->bindColNum, sizeof(TAOS_MULTI_BIND)); + data->pBind = taosMemoryCalloc((int32_t)(allRowNum/gCurCase->bindRowNum)*gCurCase->bindColNum, sizeof(TAOS_MULTI_BIND)); data->pTags = taosMemoryCalloc(gCurCase->tblNum*gCurCase->bindTagNum, sizeof(TAOS_MULTI_BIND)); data->tsData = taosMemoryMalloc(allRowNum * sizeof(int64_t)); data->boolData = taosMemoryMalloc(allRowNum * sizeof(bool)); @@ -932,7 +932,7 @@ int32_t prepareInsertData(BindData *data) { data->binaryData = taosMemoryMalloc(allRowNum * gVarCharSize); data->binaryLen = taosMemoryMalloc(allRowNum * sizeof(int32_t)); if (gCurCase->bindNullNum) { - data->isNull = taosMemoryCalloc(allRowNum, sizeof(char)); + data->isNull = taosMemoryCalloc((int32_t)allRowNum, sizeof(char)); } for (int32_t i = 0; i < allRowNum; ++i) { @@ -950,7 +950,7 @@ int32_t prepareInsertData(BindData *data) { data->doubleData[i] = (double)(i+1); memset(data->binaryData + gVarCharSize * i, 'a'+i%26, gVarCharLen); if (gCurCase->bindNullNum) { - data->isNull[i] = i % 2; + data->isNull[i] = (char)(i % 2); } data->binaryLen[i] = gVarCharLen; } @@ -979,7 +979,7 @@ int32_t prepareQueryCondData(BindData *data, int32_t tblIdx) { data->colNum = 0; data->colTypes = taosMemoryCalloc(30, sizeof(int32_t)); data->sql = taosMemoryCalloc(1, 1024); - data->pBind = taosMemoryCalloc(bindNum*gCurCase->bindColNum, sizeof(TAOS_MULTI_BIND)); + data->pBind = taosMemoryCalloc((int32_t)bindNum*gCurCase->bindColNum, sizeof(TAOS_MULTI_BIND)); data->tsData = taosMemoryMalloc(bindNum * sizeof(int64_t)); data->boolData = taosMemoryMalloc(bindNum * sizeof(bool)); data->tinyData = taosMemoryMalloc(bindNum * sizeof(int8_t)); @@ -995,7 +995,7 @@ int32_t prepareQueryCondData(BindData *data, int32_t tblIdx) { data->binaryData = taosMemoryMalloc(bindNum * gVarCharSize); data->binaryLen = taosMemoryMalloc(bindNum * sizeof(int32_t)); if (gCurCase->bindNullNum) { - data->isNull = taosMemoryCalloc(bindNum, sizeof(char)); + data->isNull = taosMemoryCalloc((int32_t)bindNum, sizeof(char)); } for (int32_t i = 0; i < bindNum; ++i) { @@ -1013,7 +1013,7 @@ int32_t prepareQueryCondData(BindData *data, int32_t tblIdx) { data->doubleData[i] = (double)(tblIdx*gCurCase->rowNum + rand() % gCurCase->rowNum); memset(data->binaryData + gVarCharSize * i, 'a'+i%26, gVarCharLen); if (gCurCase->bindNullNum) { - data->isNull[i] = i % 2; + data->isNull[i] = (char)(i % 2); } data->binaryLen[i] = gVarCharLen; } @@ -1036,7 +1036,7 @@ int32_t prepareQueryMiscData(BindData *data, int32_t tblIdx) { data->colNum = 0; data->colTypes = taosMemoryCalloc(30, sizeof(int32_t)); data->sql = taosMemoryCalloc(1, 1024); - data->pBind = taosMemoryCalloc(bindNum*gCurCase->bindColNum, sizeof(TAOS_MULTI_BIND)); + data->pBind = taosMemoryCalloc((int32_t)bindNum*gCurCase->bindColNum, sizeof(TAOS_MULTI_BIND)); data->tsData = taosMemoryMalloc(bindNum * sizeof(int64_t)); data->boolData = taosMemoryMalloc(bindNum * sizeof(bool)); data->tinyData = taosMemoryMalloc(bindNum * sizeof(int8_t)); @@ -1052,7 +1052,7 @@ int32_t prepareQueryMiscData(BindData *data, int32_t tblIdx) { data->binaryData = taosMemoryMalloc(bindNum * gVarCharSize); data->binaryLen = taosMemoryMalloc(bindNum * sizeof(int32_t)); if (gCurCase->bindNullNum) { - data->isNull = taosMemoryCalloc(bindNum, sizeof(char)); + data->isNull = taosMemoryCalloc((int32_t)bindNum, sizeof(char)); } for (int32_t i = 0; i < bindNum; ++i) { @@ -1070,7 +1070,7 @@ int32_t prepareQueryMiscData(BindData *data, int32_t tblIdx) { data->doubleData[i] = (double)(tblIdx*gCurCase->rowNum + rand() % gCurCase->rowNum); memset(data->binaryData + gVarCharSize * i, 'a'+i%26, gVarCharLen); if (gCurCase->bindNullNum) { - data->isNull[i] = i % 2; + data->isNull[i] = (char)(i % 2); } data->binaryLen[i] = gVarCharLen; } @@ -1279,7 +1279,7 @@ void bpCheckQueryResult(TAOS_STMT *stmt, TAOS *taos, char *stmtSql, TAOS_MULTI_B } memcpy(&sql[len], p, (int64_t)s - (int64_t)p); - len += (int64_t)s - (int64_t)p; + len += (int32_t)((int64_t)s - (int64_t)p); if (bind[i].is_null && bind[i].is_null[0]) { bpAppendValueString(sql, TSDB_DATA_TYPE_NULL, NULL, 0, &len); @@ -2669,7 +2669,7 @@ int main(int argc, char *argv[]) { TAOS *taos = NULL; - srand(time(NULL)); + srand((unsigned int)time(NULL)); // connect to server if (argc < 2) { diff --git a/tests/script/api/makefile b/tests/script/api/makefile index 46a172cc3a..1f725f17c9 100644 --- a/tests/script/api/makefile +++ b/tests/script/api/makefile @@ -12,6 +12,7 @@ all: $(TARGET) exe: gcc $(CFLAGS) ./batchprepare.c -o $(ROOT)batchprepare $(LFLAGS) + gcc $(CFLAGS) ./stopquery.c -o $(ROOT)stopquery $(LFLAGS) clean: rm $(ROOT)batchprepare diff --git a/tests/script/api/stopquery.c b/tests/script/api/stopquery.c new file mode 100644 index 0000000000..5fa1c3654d --- /dev/null +++ b/tests/script/api/stopquery.c @@ -0,0 +1,268 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +// TAOS asynchronous API example +// this example opens multiple tables, insert/retrieve multiple tables +// it is used by TAOS internally for one performance testing +// to compiple: gcc -o asyncdemo asyncdemo.c -ltaos + +#include +#include +#include +#include +#include +#include "taos.h" + + +int points = 5; +int numOfTables = 3; +int tablesInsertProcessed = 0; +int tablesSelectProcessed = 0; +int64_t st, et; + +char hostName[128]; +char dbName[128]; +char tbName[128]; +char runTimes = 1; + +typedef struct { + int id; + TAOS *taos; + char name[16]; + time_t timeStamp; + int value; + int rowsInserted; + int rowsTried; + int rowsRetrieved; +} STable; + +typedef struct SSP_CB_PARAM { + bool fetch; + int32_t *end; +} SSP_CB_PARAM; + +#define CASE_ENTER() do { printf("enter case %s\n", __FUNCTION__); } while (0) +#define CASE_LEAVE() do { printf("leave case %s, runTimes %d\n", __FUNCTION__, runTimes); } while (0) + +static void sqExecSQL(TAOS *taos, char *command) { + int i; + int32_t code = -1; + + TAOS_RES *pSql = taos_query(taos, command); + code = taos_errno(pSql); + if (code != 0) { + fprintf(stderr, "Failed to run %s, reason: %s\n", command, taos_errstr(pSql)); + taos_free_result(pSql); + taos_close(taos); + taos_cleanup(); + exit(EXIT_FAILURE); + } + + taos_free_result(pSql); +} + +void sqExit(char* prefix, const char* errMsg) { + fprintf(stderr, "%s error: %s\n", prefix, errMsg); + exit(1); +} + +void sqStopFetchCb(void *param, TAOS_RES *pRes, int numOfRows) { + SSP_CB_PARAM *qParam = (SSP_CB_PARAM *)param; + taos_stop_query(pRes); + taos_free_result(pRes); + + *qParam->end = 1; +} + +void sqStopQueryCb(void *param, TAOS_RES *pRes, int code) { + SSP_CB_PARAM *qParam = (SSP_CB_PARAM *)param; + if (code == 0 && pRes) { + if (qParam->fetch) { + taos_fetch_rows_a(pRes, sqStopFetchCb, param); + } else { + taos_stop_query(pRes); + taos_free_result(pRes); + *qParam->end = 1; + } + } else { + sqExit("select", taos_errstr(pRes)); + } +} + +void sqFreeFetchCb(void *param, TAOS_RES *pRes, int numOfRows) { + SSP_CB_PARAM *qParam = (SSP_CB_PARAM *)param; + taos_free_result(pRes); + + *qParam->end = 1; +} + +void sqFreeQueryCb(void *param, TAOS_RES *pRes, int code) { + SSP_CB_PARAM *qParam = (SSP_CB_PARAM *)param; + if (code == 0 && pRes) { + if (qParam->fetch) { + taos_fetch_rows_a(pRes, sqFreeFetchCb, param); + } else { + taos_free_result(pRes); + *qParam->end = 1; + } + } else { + sqExit("select", taos_errstr(pRes)); + } +} + + +int sqSyncStopQuery(bool fetch) { + CASE_ENTER(); + for (int32_t i = 0; i < runTimes; ++i) { + char sql[1024] = {0}; + int32_t code = 0; + TAOS *taos = taos_connect(hostName, "root", "taosdata", NULL, 0); + if (taos == NULL) sqExit("taos_connect", taos_errstr(NULL)); + + sprintf(sql, "use %s", dbName); + sqExecSQL(taos, sql); + + sprintf(sql, "select * from %s", tbName); + TAOS_RES* pRes = taos_query(taos, sql); + code = taos_errno(pRes); + if (code) { + sqExit("taos_query", taos_errstr(pRes)); + } + + if (fetch) { + taos_fetch_row(pRes); + } + + taos_stop_query(pRes); + taos_free_result(pRes); + + taos_close(taos); + } + CASE_LEAVE(); +} + +int sqAsyncStopQuery(bool fetch) { + CASE_ENTER(); + for (int32_t i = 0; i < runTimes; ++i) { + char sql[1024] = {0}; + int32_t code = 0; + TAOS *taos = taos_connect(hostName, "root", "taosdata", NULL, 0); + if (taos == NULL) sqExit("taos_connect", taos_errstr(NULL)); + + sprintf(sql, "use %s", dbName); + sqExecSQL(taos, sql); + + sprintf(sql, "select * from %s", tbName); + + int32_t qEnd = 0; + SSP_CB_PARAM param = {0}; + param.fetch = fetch; + param.end = &qEnd; + taos_query_a(taos, sql, sqStopQueryCb, ¶m); + while (0 == qEnd) { + usleep(5000); + } + + taos_close(taos); + } + CASE_LEAVE(); +} + +int sqSyncFreeQuery(bool fetch) { + CASE_ENTER(); + for (int32_t i = 0; i < runTimes; ++i) { + char sql[1024] = {0}; + int32_t code = 0; + TAOS *taos = taos_connect(hostName, "root", "taosdata", NULL, 0); + if (taos == NULL) sqExit("taos_connect", taos_errstr(NULL)); + + sprintf(sql, "use %s", dbName); + sqExecSQL(taos, sql); + + sprintf(sql, "select * from %s", tbName); + TAOS_RES* pRes = taos_query(taos, sql); + code = taos_errno(pRes); + if (code) { + sqExit("taos_query", taos_errstr(pRes)); + } + + if (fetch) { + taos_fetch_row(pRes); + } + + taos_free_result(pRes); + taos_close(taos); + } + CASE_LEAVE(); +} + +int sqAsyncFreeQuery(bool fetch) { + CASE_ENTER(); + for (int32_t i = 0; i < runTimes; ++i) { + char sql[1024] = {0}; + int32_t code = 0; + TAOS *taos = taos_connect(hostName, "root", "taosdata", NULL, 0); + if (taos == NULL) sqExit("taos_connect", taos_errstr(NULL)); + + sprintf(sql, "use %s", dbName); + sqExecSQL(taos, sql); + + sprintf(sql, "select * from %s", tbName); + + int32_t qEnd = 0; + SSP_CB_PARAM param = {0}; + param.fetch = fetch; + param.end = &qEnd; + taos_query_a(taos, sql, sqFreeQueryCb, ¶m); + while (0 == qEnd) { + usleep(5000); + } + + taos_close(taos); + } + CASE_LEAVE(); +} + + +void sqRunAllCase(void) { + sqSyncStopQuery(false); + sqSyncStopQuery(true); + sqAsyncStopQuery(false); + sqAsyncStopQuery(true); + + sqSyncFreeQuery(false); + sqSyncFreeQuery(true); + sqAsyncFreeQuery(false); + sqAsyncFreeQuery(true); + +} + + +int main(int argc, char *argv[]) { + if (argc != 4) { + printf("usage: %s server-ip dbname tablename\n", argv[0]); + exit(0); + } + + strcpy(hostName, argv[1]); + strcpy(dbName, argv[2]); + strcpy(tbName, argv[3]); + + sqRunAllCase(); + + return 0; +} + + From c364f98978b30ba0e42c0ea0c8638be0c61232ef Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Sun, 26 Jun 2022 19:34:50 +0800 Subject: [PATCH 02/36] enh: stop query process --- source/client/inc/clientInt.h | 1 + source/client/src/clientEnv.c | 23 ++-- source/client/src/clientImpl.c | 4 +- source/libs/catalog/src/catalog.c | 19 ++-- tests/script/api/stopquery.c | 168 +++++++++++++++++++++++++++++- 5 files changed, 199 insertions(+), 16 deletions(-) diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index 63f6486197..cad262a00f 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -301,6 +301,7 @@ void destroyRequest(SRequestObj* pRequest); SRequestObj* acquireRequest(int64_t rid); int32_t releaseRequest(int64_t rid); int32_t removeRequest(int64_t rid); +void doDestroyRequest(void *p); char* getDbOfConnection(STscObj* pObj); void setConnectionDB(STscObj* pTscObj, const char* db); diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 510d231855..500933b68b 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -37,10 +37,12 @@ int32_t clientConnRefPool = -1; static TdThreadOnce tscinit = PTHREAD_ONCE_INIT; volatile int32_t tscInitRes = 0; -static void registerRequest(SRequestObj *pRequest) { +static int32_t registerRequest(SRequestObj *pRequest) { STscObj *pTscObj = acquireTscObj(*(int64_t *)pRequest->pTscObj->id); - - assert(pTscObj != NULL); + if (NULL == pTscObj) { + terrno = TSDB_CODE_TSC_DISCONNECTED; + return terrno; + } // connection has been released already, abort creating request. pRequest->self = taosAddRef(clientReqRefPool, pRequest); @@ -56,6 +58,8 @@ static void registerRequest(SRequestObj *pRequest) { ", current:%d, app current:%d, total:%d, reqId:0x%" PRIx64, pRequest->self, *(int64_t *)pRequest->pTscObj->id, num, currentInst, total, pRequest->requestId); } + + return TSDB_CODE_SUCCESS; } static void deregisterRequest(SRequestObj *pRequest) { @@ -202,7 +206,10 @@ void *createRequest(STscObj *pObj, int32_t type) { pRequest->msgBufLen = ERROR_MSG_BUF_DEFAULT_SIZE; tsem_init(&pRequest->body.rspSem, 0, 0); - registerRequest(pRequest); + if (registerRequest(pRequest)) { + doDestroyRequest(pRequest); + return NULL; + } return pRequest; } @@ -230,12 +237,10 @@ int32_t releaseRequest(int64_t rid) { return taosReleaseRef(clientReqRefPool, ri int32_t removeRequest(int64_t rid) { return taosRemoveRef(clientReqRefPool, rid); } -static void doDestroyRequest(void *p) { +void doDestroyRequest(void *p) { assert(p != NULL); SRequestObj *pRequest = (SRequestObj *)p; - assert(RID_VALID(pRequest->self)); - taosHashRemove(pRequest->pTscObj->pRequests, &pRequest->self, sizeof(pRequest->self)); if (pRequest->body.queryJob != 0) { @@ -253,7 +258,9 @@ static void doDestroyRequest(void *p) { destroyQueryExecRes(&pRequest->body.resInfo.execRes); - deregisterRequest(pRequest); + if (pRequest->self) { + deregisterRequest(pRequest); + } taosMemoryFreeClear(pRequest); } diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index d83ca89c69..00bc7200b1 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -2008,7 +2008,9 @@ void syncCatalogFn(SMetaData* pResult, void* param, int32_t code) { void syncQueryFn(void* param, void* res, int32_t code) { SSyncQueryParam* pParam = param; pParam->pRequest = res; - pParam->pRequest->code = code; + if (pParam->pRequest) { + pParam->pRequest->code = code; + } tsem_post(&pParam->sem); } diff --git a/source/libs/catalog/src/catalog.c b/source/libs/catalog/src/catalog.c index b459beb658..0c46d6f5fa 100644 --- a/source/libs/catalog/src/catalog.c +++ b/source/libs/catalog/src/catalog.c @@ -1127,12 +1127,14 @@ int32_t catalogGetExpiredUsers(SCatalog* pCtg, SUserAuthVersion** users, uint32_ } *num = taosHashGetSize(pCtg->userCache); - if (*num > 0) { - *users = taosMemoryCalloc(*num, sizeof(SUserAuthVersion)); - if (NULL == *users) { - ctgError("calloc %d userAuthVersion failed", *num); - CTG_API_LEAVE(TSDB_CODE_OUT_OF_MEMORY); - } + if (*num <= 0) { + CTG_API_LEAVE(TSDB_CODE_SUCCESS); + } + + *users = taosMemoryCalloc(*num, sizeof(SUserAuthVersion)); + if (NULL == *users) { + ctgError("calloc %d userAuthVersion failed", *num); + CTG_API_LEAVE(TSDB_CODE_OUT_OF_MEMORY); } uint32_t i = 0; @@ -1144,6 +1146,11 @@ int32_t catalogGetExpiredUsers(SCatalog* pCtg, SUserAuthVersion** users, uint32_ (*users)[i].user[len] = 0; (*users)[i].version = pAuth->version; ++i; + if (i >= *num) { + taosHashCancelIterate(pCtg->userCache, pAuth); + break; + } + pAuth = taosHashIterate(pCtg->userCache, pAuth); } diff --git a/tests/script/api/stopquery.c b/tests/script/api/stopquery.c index 5fa1c3654d..4c7964c983 100644 --- a/tests/script/api/stopquery.c +++ b/tests/script/api/stopquery.c @@ -23,6 +23,7 @@ #include #include #include +#include #include "taos.h" @@ -35,7 +36,7 @@ int64_t st, et; char hostName[128]; char dbName[128]; char tbName[128]; -char runTimes = 1; +int32_t runTimes = 10000; typedef struct { int id; @@ -49,6 +50,7 @@ typedef struct { } STable; typedef struct SSP_CB_PARAM { + TAOS *taos; bool fetch; int32_t *end; } SSP_CB_PARAM; @@ -73,6 +75,16 @@ static void sqExecSQL(TAOS *taos, char *command) { taos_free_result(pSql); } +static void sqExecSQLE(TAOS *taos, char *command) { + int i; + int32_t code = -1; + + TAOS_RES *pSql = taos_query(taos, command); + + taos_free_result(pSql); +} + + void sqExit(char* prefix, const char* errMsg) { fprintf(stderr, "%s error: %s\n", prefix, errMsg); exit(1); @@ -123,6 +135,27 @@ void sqFreeQueryCb(void *param, TAOS_RES *pRes, int code) { } +void sqCloseFetchCb(void *param, TAOS_RES *pRes, int numOfRows) { + SSP_CB_PARAM *qParam = (SSP_CB_PARAM *)param; + taos_close(qParam->taos); + + *qParam->end = 1; +} + +void sqCloseQueryCb(void *param, TAOS_RES *pRes, int code) { + SSP_CB_PARAM *qParam = (SSP_CB_PARAM *)param; + if (code == 0 && pRes) { + if (qParam->fetch) { + taos_fetch_rows_a(pRes, sqFreeFetchCb, param); + } else { + taos_close(qParam->taos); + *qParam->end = 1; + } + } else { + sqExit("select", taos_errstr(pRes)); + } +} + int sqSyncStopQuery(bool fetch) { CASE_ENTER(); for (int32_t i = 0; i < runTimes; ++i) { @@ -131,6 +164,9 @@ int sqSyncStopQuery(bool fetch) { TAOS *taos = taos_connect(hostName, "root", "taosdata", NULL, 0); if (taos == NULL) sqExit("taos_connect", taos_errstr(NULL)); + sprintf(sql, "reset query cache"); + sqExecSQL(taos, sql); + sprintf(sql, "use %s", dbName); sqExecSQL(taos, sql); @@ -161,6 +197,9 @@ int sqAsyncStopQuery(bool fetch) { TAOS *taos = taos_connect(hostName, "root", "taosdata", NULL, 0); if (taos == NULL) sqExit("taos_connect", taos_errstr(NULL)); + sprintf(sql, "reset query cache"); + sqExecSQL(taos, sql); + sprintf(sql, "use %s", dbName); sqExecSQL(taos, sql); @@ -188,6 +227,9 @@ int sqSyncFreeQuery(bool fetch) { TAOS *taos = taos_connect(hostName, "root", "taosdata", NULL, 0); if (taos == NULL) sqExit("taos_connect", taos_errstr(NULL)); + sprintf(sql, "reset query cache"); + sqExecSQL(taos, sql); + sprintf(sql, "use %s", dbName); sqExecSQL(taos, sql); @@ -216,6 +258,9 @@ int sqAsyncFreeQuery(bool fetch) { TAOS *taos = taos_connect(hostName, "root", "taosdata", NULL, 0); if (taos == NULL) sqExit("taos_connect", taos_errstr(NULL)); + sprintf(sql, "reset query cache"); + sqExecSQL(taos, sql); + sprintf(sql, "use %s", dbName); sqExecSQL(taos, sql); @@ -235,8 +280,119 @@ int sqAsyncFreeQuery(bool fetch) { CASE_LEAVE(); } +int sqSyncCloseQuery(bool fetch) { + CASE_ENTER(); + for (int32_t i = 0; i < runTimes; ++i) { + char sql[1024] = {0}; + int32_t code = 0; + TAOS *taos = taos_connect(hostName, "root", "taosdata", NULL, 0); + if (taos == NULL) sqExit("taos_connect", taos_errstr(NULL)); + + sprintf(sql, "reset query cache"); + sqExecSQL(taos, sql); + + sprintf(sql, "use %s", dbName); + sqExecSQL(taos, sql); + + sprintf(sql, "select * from %s", tbName); + TAOS_RES* pRes = taos_query(taos, sql); + code = taos_errno(pRes); + if (code) { + sqExit("taos_query", taos_errstr(pRes)); + } + + if (fetch) { + taos_fetch_row(pRes); + } + + taos_close(taos); + } + CASE_LEAVE(); +} + +int sqAsyncCloseQuery(bool fetch) { + CASE_ENTER(); + for (int32_t i = 0; i < runTimes; ++i) { + char sql[1024] = {0}; + int32_t code = 0; + TAOS *taos = taos_connect(hostName, "root", "taosdata", NULL, 0); + if (taos == NULL) sqExit("taos_connect", taos_errstr(NULL)); + + sprintf(sql, "reset query cache"); + sqExecSQL(taos, sql); + + sprintf(sql, "use %s", dbName); + sqExecSQL(taos, sql); + + sprintf(sql, "select * from %s", tbName); + + int32_t qEnd = 0; + SSP_CB_PARAM param = {0}; + param.fetch = fetch; + param.end = &qEnd; + taos_query_a(taos, sql, sqFreeQueryCb, ¶m); + while (0 == qEnd) { + usleep(5000); + } + } + CASE_LEAVE(); +} + +void *syncQueryThreadFp(void *arg) { + SSP_CB_PARAM* qParam = (SSP_CB_PARAM*)arg; + char sql[1024] = {0}; + int32_t code = 0; + TAOS *taos = taos_connect(hostName, "root", "taosdata", NULL, 0); + if (taos == NULL) sqExit("taos_connect", taos_errstr(NULL)); + + qParam->taos = taos; + + sprintf(sql, "reset query cache"); + sqExecSQLE(taos, sql); + + sprintf(sql, "use %s", dbName); + sqExecSQLE(taos, sql); + + sprintf(sql, "select * from %s", tbName); + TAOS_RES* pRes = taos_query(taos, sql); + + if (qParam->fetch) { + taos_fetch_row(pRes); + } + + taos_free_result(pRes); +} + +void *closeThreadFp(void *arg) { + SSP_CB_PARAM* qParam = (SSP_CB_PARAM*)arg; + while (true) { + if (qParam->taos) { + usleep(rand() % 10000); + taos_close(qParam->taos); + break; + } + usleep(1); + } +} + + +int sqConSyncCloseQuery(bool fetch) { + CASE_ENTER(); + pthread_t qid, cid; + for (int32_t i = 0; i < runTimes; ++i) { + SSP_CB_PARAM param = {0}; + param.fetch = fetch; + pthread_create(&qid, NULL, syncQueryThreadFp, (void*)¶m); + pthread_create(&cid, NULL, closeThreadFp, (void*)¶m); + + pthread_join(qid, NULL); + pthread_join(cid, NULL); + } + CASE_LEAVE(); +} void sqRunAllCase(void) { +/* sqSyncStopQuery(false); sqSyncStopQuery(true); sqAsyncStopQuery(false); @@ -247,6 +403,14 @@ void sqRunAllCase(void) { sqAsyncFreeQuery(false); sqAsyncFreeQuery(true); + sqSyncCloseQuery(false); + sqSyncCloseQuery(true); + sqAsyncCloseQuery(false); + sqAsyncCloseQuery(true); +*/ + sqConSyncCloseQuery(false); + sqConSyncCloseQuery(true); + } @@ -256,6 +420,8 @@ int main(int argc, char *argv[]) { exit(0); } + srand((unsigned int)time(NULL)); + strcpy(hostName, argv[1]); strcpy(dbName, argv[2]); strcpy(tbName, argv[3]); From 73dfd1173a914c2e15cb0f7b277ed3d0cd592b41 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Mon, 27 Jun 2022 09:50:42 +0800 Subject: [PATCH 03/36] enh: stop query --- include/client/taos.h | 6 ++--- source/client/inc/clientInt.h | 2 +- source/client/src/clientEnv.c | 22 ++++++++---------- source/client/src/clientImpl.c | 34 ++++++++++++++++++---------- source/client/src/clientMain.c | 18 +++++++++++---- source/client/src/clientMsgHandler.c | 4 ++-- source/client/src/clientSml.c | 22 +++++++++++------- 7 files changed, 65 insertions(+), 43 deletions(-) diff --git a/include/client/taos.h b/include/client/taos.h index 5e7f12de0a..669512e9b1 100644 --- a/include/client/taos.h +++ b/include/client/taos.h @@ -130,10 +130,10 @@ DLL_EXPORT void taos_cleanup(void); DLL_EXPORT int taos_options(TSDB_OPTION option, const void *arg, ...); DLL_EXPORT int taos_init(void); DLL_EXPORT TAOS *taos_connect(const char *ip, const char *user, const char *pass, const char *db, uint16_t port); -DLL_EXPORT TAOS *taos_connect_auth(const char *ip, const char *user, const char *auth, const char *db, uint16_t port); -DLL_EXPORT void taos_close(TAOS *taos); +DLL_EXPORT TAOS *taos_connect_auth(const char *ip, const char *user, const char *auth, const char *db, uint16_t port); +DLL_EXPORT void taos_close(TAOS *taos); -const char *taos_data_type(int type); +const char *taos_data_type(int type); DLL_EXPORT TAOS_STMT *taos_stmt_init(TAOS *taos); DLL_EXPORT int taos_stmt_prepare(TAOS_STMT *stmt, const char *sql, unsigned long length); diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index cad262a00f..48efe89361 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -139,7 +139,7 @@ typedef struct STscObj { int8_t connType; int32_t acctId; uint32_t connId; - TAOS* id; // ref ID returned by taosAddRef + int64_t id; // ref ID returned by taosAddRef TdThreadMutex mutex; // used to protect the operation on db int32_t numOfReqs; // number of sqlObj bound to this connection SAppInstInfo* pAppInfo; diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 500933b68b..cad1b4b1cf 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -38,7 +38,7 @@ static TdThreadOnce tscinit = PTHREAD_ONCE_INIT; volatile int32_t tscInitRes = 0; static int32_t registerRequest(SRequestObj *pRequest) { - STscObj *pTscObj = acquireTscObj(*(int64_t *)pRequest->pTscObj->id); + STscObj *pTscObj = acquireTscObj(pRequest->pTscObj->id); if (NULL == pTscObj) { terrno = TSDB_CODE_TSC_DISCONNECTED; return terrno; @@ -56,7 +56,7 @@ static int32_t registerRequest(SRequestObj *pRequest) { int32_t currentInst = atomic_add_fetch_64((int64_t *)&pSummary->currentRequests, 1); tscDebug("0x%" PRIx64 " new Request from connObj:0x%" PRIx64 ", current:%d, app current:%d, total:%d, reqId:0x%" PRIx64, - pRequest->self, *(int64_t *)pRequest->pTscObj->id, num, currentInst, total, pRequest->requestId); + pRequest->self, pRequest->pTscObj->id, num, currentInst, total, pRequest->requestId); } return TSDB_CODE_SUCCESS; @@ -74,8 +74,8 @@ static void deregisterRequest(SRequestObj *pRequest) { int64_t duration = taosGetTimestampUs() - pRequest->metric.start; tscDebug("0x%" PRIx64 " free Request from connObj: 0x%" PRIx64 ", reqId:0x%" PRIx64 " elapsed:%" PRIu64 " ms, current:%d, app current:%d", - pRequest->self, *(int64_t *)pTscObj->id, pRequest->requestId, duration / 1000, num, currentInst); - releaseTscObj(*(int64_t *)pTscObj->id); + pRequest->self, pTscObj->id, pRequest->requestId, duration / 1000, num, currentInst); + releaseTscObj(pTscObj->id); } // todo close the transporter properly @@ -84,7 +84,7 @@ void closeTransporter(STscObj *pTscObj) { return; } - tscDebug("free transporter:%p in connObj: 0x%" PRIx64, pTscObj->pAppInfo->pTransporter, *(int64_t *)pTscObj->id); + tscDebug("free transporter:%p in connObj: 0x%" PRIx64, pTscObj->pAppInfo->pTransporter, pTscObj->id); rpcClose(pTscObj->pAppInfo->pTransporter); } @@ -133,16 +133,15 @@ void closeAllRequests(SHashObj *pRequests) { void destroyTscObj(void *pObj) { STscObj *pTscObj = pObj; - SClientHbKey connKey = {.tscRid = *(int64_t *)pTscObj->id, .connType = pTscObj->connType}; + SClientHbKey connKey = {.tscRid = pTscObj->id, .connType = pTscObj->connType}; hbDeregisterConn(pTscObj->pAppInfo->pAppHbMgr, connKey); int64_t connNum = atomic_sub_fetch_64(&pTscObj->pAppInfo->numOfConns, 1); closeAllRequests(pTscObj->pRequests); schedulerStopQueryHb(pTscObj->pAppInfo->pTransporter); if (0 == connNum) { - // TODO - // closeTransporter(pTscObj); + closeTransporter(pTscObj); } - tscDebug("connObj 0x%" PRIx64 " destroyed, totalConn:%" PRId64, *(int64_t *)pTscObj->id, + tscDebug("connObj 0x%" PRIx64 " p:%p destroyed, totalConn:%" PRId64, pTscObj->id, pTscObj, pTscObj->pAppInfo->numOfConns); taosThreadMutexDestroy(&pTscObj->mutex); taosMemoryFreeClear(pTscObj); @@ -172,11 +171,10 @@ void *createTscObj(const char *user, const char *auth, const char *db, int32_t c } taosThreadMutexInit(&pObj->mutex, NULL); - pObj->id = taosMemoryMalloc(sizeof(int64_t)); - *(int64_t *)pObj->id = taosAddRef(clientConnRefPool, pObj); + pObj->id = taosAddRef(clientConnRefPool, pObj); pObj->schemalessType = 1; - tscDebug("connObj created, 0x%" PRIx64, *(int64_t *)pObj->id); + tscDebug("connObj created, 0x%" PRIx64 ",p:%p", pObj->id, pObj); return pObj; } diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 00bc7200b1..d3510b91ae 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -1170,7 +1170,7 @@ STscObj* taosConnectImpl(const char* user, const char* auth, const char* db, __t taos_close_internal(pTscObj); pTscObj = NULL; } else { - tscDebug("0x%" PRIx64 " connection is opening, connId:%u, dnodeConn:%p, reqId:0x%" PRIx64, *(int64_t*)pTscObj->id, + tscDebug("0x%" PRIx64 " connection is opening, connId:%u, dnodeConn:%p, reqId:0x%" PRIx64, pTscObj->id, pTscObj->connId, pTscObj->pAppInfo->pTransporter, pRequest->requestId); destroyRequest(pRequest); } @@ -1333,7 +1333,9 @@ TAOS* taos_connect_auth(const char* ip, const char* user, const char* auth, cons STscObj* pObj = taos_connect_internal(ip, user, NULL, auth, db, port, CONN_TYPE__QUERY); if (pObj) { - return pObj->id; + int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t)); + *rid = pObj->id; + return (TAOS*)rid; } return NULL; @@ -2016,11 +2018,18 @@ void syncQueryFn(void* param, void* res, int32_t code) { } void taosAsyncQueryImpl(TAOS* taos, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly) { - STscObj* pTscObj = acquireTscObj(*(int64_t*)taos); + if (NULL == taos) { + terrno = TSDB_CODE_TSC_DISCONNECTED; + fp(param, NULL, terrno); + return; + } + + int64_t rid = *(int64_t*)taos; + STscObj* pTscObj = acquireTscObj(rid); if (pTscObj == NULL || sql == NULL || NULL == fp) { terrno = TSDB_CODE_INVALID_PARA; if (pTscObj) { - releaseTscObj(*(int64_t*)taos); + releaseTscObj(rid); } else { terrno = TSDB_CODE_TSC_DISCONNECTED; } @@ -2032,7 +2041,7 @@ void taosAsyncQueryImpl(TAOS* taos, const char* sql, __taos_async_fn_t fp, void* if (sqlLen > (size_t)TSDB_MAX_ALLOWED_SQL_LEN) { tscError("sql string exceeds max length:%d", TSDB_MAX_ALLOWED_SQL_LEN); terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT; - releaseTscObj(*(int64_t *)taos); + releaseTscObj(rid); fp(param, NULL, terrno); return; @@ -2042,7 +2051,7 @@ void taosAsyncQueryImpl(TAOS* taos, const char* sql, __taos_async_fn_t fp, void* int32_t code = buildRequest(pTscObj, sql, sqlLen, &pRequest); if (code != TSDB_CODE_SUCCESS) { terrno = code; - releaseTscObj(*(int64_t *)taos); + releaseTscObj(rid); fp(param, NULL, terrno); return; } @@ -2051,7 +2060,7 @@ void taosAsyncQueryImpl(TAOS* taos, const char* sql, __taos_async_fn_t fp, void* pRequest->body.queryFp = fp; pRequest->body.param = param; doAsyncQuery(pRequest, false); - releaseTscObj(*(int64_t *)taos); + releaseTscObj(rid); } TAOS_RES* taosQueryImpl(TAOS* taos, const char* sql, bool validateOnly) { @@ -2060,7 +2069,8 @@ TAOS_RES* taosQueryImpl(TAOS* taos, const char* sql, bool validateOnly) { return NULL; } - STscObj* pTscObj = acquireTscObj(*(int64_t*)taos); + int64_t rid = *(int64_t*)taos; + STscObj* pTscObj = acquireTscObj(rid); if (pTscObj == NULL || sql == NULL) { terrno = TSDB_CODE_TSC_DISCONNECTED; return NULL; @@ -2070,16 +2080,16 @@ TAOS_RES* taosQueryImpl(TAOS* taos, const char* sql, bool validateOnly) { SSyncQueryParam* param = taosMemoryCalloc(1, sizeof(SSyncQueryParam)); tsem_init(¶m->sem, 0, 0); - taosAsyncQueryImpl(taos, sql, syncQueryFn, param, validateOnly); + taosAsyncQueryImpl((TAOS*)&rid, sql, syncQueryFn, param, validateOnly); tsem_wait(¶m->sem); - releaseTscObj(*(int64_t*)taos); + releaseTscObj(rid); return param->pRequest; #else size_t sqlLen = strlen(sql); if (sqlLen > (size_t)TSDB_MAX_ALLOWED_SQL_LEN) { - releaseTscObj(*(int64_t*)taos); + releaseTscObj(rid); tscError("sql string exceeds max length:%d", TSDB_MAX_ALLOWED_SQL_LEN); terrno = TSDB_CODE_TSC_EXCEED_SQL_LIMIT; return NULL; @@ -2087,7 +2097,7 @@ TAOS_RES* taosQueryImpl(TAOS* taos, const char* sql, bool validateOnly) { TAOS_RES* pRes = execQuery(pTscObj, sql, sqlLen, validateOnly); - releaseTscObj(*(int64_t*)taos); + releaseTscObj(rid); return pRes; #endif diff --git a/source/client/src/clientMain.c b/source/client/src/clientMain.c index bbd477fa3b..a24e6faf7f 100644 --- a/source/client/src/clientMain.c +++ b/source/client/src/clientMain.c @@ -93,7 +93,9 @@ TAOS *taos_connect(const char *ip, const char *user, const char *pass, const cha STscObj *pObj = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY); if (pObj) { - return pObj->id; + int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t)); + *rid = pObj->id; + return (TAOS*)rid; } return NULL; @@ -105,9 +107,9 @@ void taos_close_internal(void *taos) { } STscObj *pTscObj = (STscObj *)taos; - tscDebug("0x%" PRIx64 " try to close connection, numOfReq:%d", *(int64_t *)pTscObj->id, pTscObj->numOfReqs); + tscDebug("0x%" PRIx64 " try to close connection, numOfReq:%d", pTscObj->id, pTscObj->numOfReqs); - taosRemoveRef(clientConnRefPool, *(int64_t *)pTscObj->id); + taosRemoveRef(clientConnRefPool, pTscObj->id); } void taos_close(TAOS *taos) { @@ -880,6 +882,12 @@ void taos_unsubscribe(TAOS_SUB *tsub, int keepProgress) { } int taos_load_table_info(TAOS *taos, const char *tableNameList) { + if (NULL == taos) { + terrno = TSDB_CODE_TSC_DISCONNECTED; + return terrno; + } + + int64_t rid = *(int64_t*)taos; const int32_t MAX_TABLE_NAME_LENGTH = 12 * 1024 * 1024; // 12MB list int32_t code = 0; SRequestObj *pRequest = NULL; @@ -897,7 +905,7 @@ int taos_load_table_info(TAOS *taos, const char *tableNameList) { return TSDB_CODE_TSC_INVALID_OPERATION; } - STscObj *pTscObj = acquireTscObj(*(int64_t *)taos); + STscObj *pTscObj = acquireTscObj(rid); if (pTscObj == NULL) { terrno = TSDB_CODE_TSC_DISCONNECTED; return terrno; @@ -942,7 +950,7 @@ _return: taosArrayDestroy(catalogReq.pTableMeta); destroyRequest(pRequest); - releaseTscObj(*(int64_t *)taos); + releaseTscObj(rid); return code; } diff --git a/source/client/src/clientMsgHandler.c b/source/client/src/clientMsgHandler.c index 45a525d124..818762eff6 100644 --- a/source/client/src/clientMsgHandler.c +++ b/source/client/src/clientMsgHandler.c @@ -77,7 +77,7 @@ int32_t processConnectRsp(void* param, const SDataBuf* pMsg, int32_t code) { for (int32_t i = 0; i < connectRsp.epSet.numOfEps; ++i) { tscDebug("0x%" PRIx64 " epSet.fqdn[%d]:%s port:%d, connObj:0x%" PRIx64, pRequest->requestId, i, - connectRsp.epSet.eps[i].fqdn, connectRsp.epSet.eps[i].port, *(int64_t*)pTscObj->id); + connectRsp.epSet.eps[i].fqdn, connectRsp.epSet.eps[i].port, pTscObj->id); } pTscObj->connId = connectRsp.connId; @@ -91,7 +91,7 @@ int32_t processConnectRsp(void* param, const SDataBuf* pMsg, int32_t code) { pTscObj->connType = connectRsp.connType; - hbRegisterConn(pTscObj->pAppInfo->pAppHbMgr, *(int64_t*)pTscObj->id, connectRsp.clusterId, connectRsp.connType); + hbRegisterConn(pTscObj->pAppInfo->pAppHbMgr, pTscObj->id, connectRsp.clusterId, connectRsp.connType); // pRequest->body.resInfo.pRspMsg = pMsg->pData; tscDebug("0x%" PRIx64 " clusterId:%" PRId64 ", totalConn:%" PRId64, pRequest->requestId, connectRsp.clusterId, diff --git a/source/client/src/clientSml.c b/source/client/src/clientSml.c index 7d2bf019d2..1cb0e2d54b 100644 --- a/source/client/src/clientSml.c +++ b/source/client/src/clientSml.c @@ -309,7 +309,7 @@ static int32_t smlApplySchemaAction(SSmlHandle *info, SSchemaAction *action) { case SCHEMA_ACTION_ADD_COLUMN: { int n = sprintf(result, "alter stable `%s` add column ", action->alterSTable.sTableName); smlBuildColumnDescription(action->alterSTable.field, result + n, capacity - n, &outBytes); - TAOS_RES *res = taos_query(info->taos->id, result); // TODO async doAsyncQuery + TAOS_RES *res = taos_query((TAOS*)&info->taos->id, result); // TODO async doAsyncQuery code = taos_errno(res); const char *errStr = taos_errstr(res); if (code != TSDB_CODE_SUCCESS) { @@ -323,7 +323,7 @@ static int32_t smlApplySchemaAction(SSmlHandle *info, SSchemaAction *action) { case SCHEMA_ACTION_ADD_TAG: { int n = sprintf(result, "alter stable `%s` add tag ", action->alterSTable.sTableName); smlBuildColumnDescription(action->alterSTable.field, result + n, capacity - n, &outBytes); - TAOS_RES *res = taos_query(info->taos->id, result); // TODO async doAsyncQuery + TAOS_RES *res = taos_query((TAOS*)&info->taos->id, result); // TODO async doAsyncQuery code = taos_errno(res); const char *errStr = taos_errstr(res); if (code != TSDB_CODE_SUCCESS) { @@ -337,7 +337,7 @@ static int32_t smlApplySchemaAction(SSmlHandle *info, SSchemaAction *action) { case SCHEMA_ACTION_CHANGE_COLUMN_SIZE: { int n = sprintf(result, "alter stable `%s` modify column ", action->alterSTable.sTableName); smlBuildColumnDescription(action->alterSTable.field, result + n, capacity - n, &outBytes); - TAOS_RES *res = taos_query(info->taos->id, result); // TODO async doAsyncQuery + TAOS_RES *res = taos_query((TAOS*)&info->taos->id, result); // TODO async doAsyncQuery code = taos_errno(res); if (code != TSDB_CODE_SUCCESS) { uError("SML:0x%" PRIx64 " apply schema action. error : %s", info->id, taos_errstr(res)); @@ -350,7 +350,7 @@ static int32_t smlApplySchemaAction(SSmlHandle *info, SSchemaAction *action) { case SCHEMA_ACTION_CHANGE_TAG_SIZE: { int n = sprintf(result, "alter stable `%s` modify tag ", action->alterSTable.sTableName); smlBuildColumnDescription(action->alterSTable.field, result + n, capacity - n, &outBytes); - TAOS_RES *res = taos_query(info->taos->id, result); // TODO async doAsyncQuery + TAOS_RES *res = taos_query((TAOS*)&info->taos->id, result); // TODO async doAsyncQuery code = taos_errno(res); if (code != TSDB_CODE_SUCCESS) { uError("SML:0x%" PRIx64 " apply schema action. error : %s", info->id, taos_errstr(res)); @@ -405,7 +405,7 @@ static int32_t smlApplySchemaAction(SSmlHandle *info, SSchemaAction *action) { pos--; ++freeBytes; outBytes = snprintf(pos, freeBytes, ")"); - TAOS_RES *res = taos_query(info->taos->id, result); + TAOS_RES *res = taos_query((TAOS*)&info->taos->id, result); code = taos_errno(res); if (code != TSDB_CODE_SUCCESS) { uError("SML:0x%" PRIx64 " apply schema action. error : %s", info->id, taos_errstr(res)); @@ -2436,7 +2436,13 @@ static void smlInsertCallback(void *param, void *res, int32_t code) { */ TAOS_RES* taos_schemaless_insert(TAOS* taos, char* lines[], int numLines, int protocol, int precision) { - STscObj* pTscObj = acquireTscObj(*(int64_t*)taos); + if (NULL == taos) { + terrno = TSDB_CODE_TSC_DISCONNECTED; + return NULL; + } + + int64_t rid = *(int64_t*)taos; + STscObj* pTscObj = acquireTscObj(rid); if (NULL == pTscObj) { terrno = TSDB_CODE_TSC_DISCONNECTED; uError("SML:taos_schemaless_insert invalid taos"); @@ -2445,7 +2451,7 @@ TAOS_RES* taos_schemaless_insert(TAOS* taos, char* lines[], int numLines, int pr SRequestObj* request = (SRequestObj*)createRequest(pTscObj, TSDB_SQL_INSERT); if(!request){ - releaseTscObj(*(int64_t*)taos); + releaseTscObj(rid); uError("SML:taos_schemaless_insert error request is null"); return NULL; } @@ -2533,6 +2539,6 @@ end: // ((STscObj *)taos)->schemalessType = 0; pTscObj->schemalessType = 1; uDebug("resultend:%s", request->msgBuf); - releaseTscObj(*(int64_t*)taos); + releaseTscObj(rid); return (TAOS_RES*)request; } From de06f055c2afb53ba532e56973eea5b1f1beb798 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Mon, 27 Jun 2022 13:33:29 +0800 Subject: [PATCH 04/36] enh: stop query --- source/client/inc/clientInt.h | 4 ++- source/client/src/clientEnv.c | 34 ++++++++++++++++------ source/client/src/clientHb.c | 42 ++++++++++++++++++++-------- source/client/src/clientImpl.c | 5 +++- source/client/src/clientMsgHandler.c | 1 - source/libs/catalog/src/ctgAsync.c | 2 ++ tests/script/api/stopquery.c | 1 + 7 files changed, 66 insertions(+), 23 deletions(-) diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index 48efe89361..9225d67131 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -66,7 +66,7 @@ enum { typedef struct SAppInstInfo SAppInstInfo; typedef struct { - char* key; + char* key; // statistics int32_t reportCnt; int32_t connKeyCnt; @@ -118,6 +118,7 @@ struct SAppInstInfo { uint64_t clusterId; void* pTransporter; SAppHbMgr* pAppHbMgr; + char* instKey; }; typedef struct SAppInfo { @@ -336,6 +337,7 @@ int hbHandleRsp(SClientHbBatchRsp* hbRsp); // cluster level SAppHbMgr* appHbMgrInit(SAppInstInfo* pAppInstInfo, char* key); void appHbMgrCleanup(void); +void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr); // conn level int hbRegisterConn(SAppHbMgr* pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType); diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index cad1b4b1cf..f5b5c48d47 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -79,13 +79,13 @@ static void deregisterRequest(SRequestObj *pRequest) { } // todo close the transporter properly -void closeTransporter(STscObj *pTscObj) { - if (pTscObj == NULL || pTscObj->pAppInfo->pTransporter == NULL) { +void closeTransporter(SAppInstInfo *pAppInfo) { + if (pAppInfo == NULL || pAppInfo->pTransporter == NULL) { return; } - tscDebug("free transporter:%p in connObj: 0x%" PRIx64, pTscObj->pAppInfo->pTransporter, pTscObj->id); - rpcClose(pTscObj->pAppInfo->pTransporter); + tscDebug("free transporter:%p in app inst %p", pAppInfo->pTransporter, pAppInfo); + rpcClose(pAppInfo->pTransporter); } static bool clientRpcRfp(int32_t code) { @@ -130,6 +130,21 @@ void closeAllRequests(SHashObj *pRequests) { } } +void destroyAppInst(SAppInstInfo* pAppInfo) { + tscDebug("destroy app inst mgr %p", pAppInfo); + + hbRemoveAppHbMrg(&pAppInfo->pAppHbMgr); + taosHashRemove(appInfo.pInstMap, pAppInfo->instKey, strlen(pAppInfo->instKey)); + taosMemoryFreeClear(pAppInfo->instKey); + closeTransporter(pAppInfo); + + taosThreadMutexLock(&pAppInfo->qnodeMutex); + taosArrayDestroy(pAppInfo->pQnodeList); + taosThreadMutexUnlock(&pAppInfo->qnodeMutex); + + taosMemoryFree(pAppInfo); +} + void destroyTscObj(void *pObj) { STscObj *pTscObj = pObj; @@ -138,11 +153,12 @@ void destroyTscObj(void *pObj) { int64_t connNum = atomic_sub_fetch_64(&pTscObj->pAppInfo->numOfConns, 1); closeAllRequests(pTscObj->pRequests); schedulerStopQueryHb(pTscObj->pAppInfo->pTransporter); - if (0 == connNum) { - closeTransporter(pTscObj); - } - tscDebug("connObj 0x%" PRIx64 " p:%p destroyed, totalConn:%" PRId64, pTscObj->id, pTscObj, + tscDebug("connObj 0x%" PRIx64 " p:%p destroyed, remain inst totalConn:%" PRId64, pTscObj->id, pTscObj, pTscObj->pAppInfo->numOfConns); + + if (0 == connNum) { + destroyAppInst(pTscObj->pAppInfo); + } taosThreadMutexDestroy(&pTscObj->mutex); taosMemoryFreeClear(pTscObj); } @@ -174,6 +190,8 @@ void *createTscObj(const char *user, const char *auth, const char *db, int32_t c pObj->id = taosAddRef(clientConnRefPool, pObj); pObj->schemalessType = 1; + atomic_add_fetch_64(&pObj->pAppInfo->numOfConns, 1); + tscDebug("connObj created, 0x%" PRIx64 ",p:%p", pObj->id, pObj); return pObj; } diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index 0681b5d0a5..4f4bb83fe5 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -790,22 +790,40 @@ SAppHbMgr *appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key) { return pAppHbMgr; } +void hbFreeAppHbMgr(SAppHbMgr *pTarget) { + void *pIter = taosHashIterate(pTarget->activeInfo, NULL); + while (pIter != NULL) { + SClientHbReq *pOneReq = pIter; + tFreeClientHbReq(pOneReq); + pIter = taosHashIterate(pTarget->activeInfo, pIter); + } + taosHashCleanup(pTarget->activeInfo); + pTarget->activeInfo = NULL; + + taosMemoryFree(pTarget->key); + taosMemoryFree(pTarget); +} + +void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr) { + taosThreadMutexLock(&clientHbMgr.lock); + int32_t mgrSize = taosArrayGetSize(clientHbMgr.appHbMgrs); + for (int32_t i = 0; i < mgrSize; ++i) { + SAppHbMgr *pItem = taosArrayGetP(clientHbMgr.appHbMgrs, i); + if (pItem == *pAppHbMgr) { + hbFreeAppHbMgr(*pAppHbMgr); + *pAppHbMgr = NULL; + taosArrayRemove(clientHbMgr.appHbMgrs, i); + break; + } + } + taosThreadMutexUnlock(&clientHbMgr.lock); +} + void appHbMgrCleanup(void) { int sz = taosArrayGetSize(clientHbMgr.appHbMgrs); for (int i = 0; i < sz; i++) { SAppHbMgr *pTarget = taosArrayGetP(clientHbMgr.appHbMgrs, i); - - void *pIter = taosHashIterate(pTarget->activeInfo, NULL); - while (pIter != NULL) { - SClientHbReq *pOneReq = pIter; - tFreeClientHbReq(pOneReq); - pIter = taosHashIterate(pTarget->activeInfo, pIter); - } - taosHashCleanup(pTarget->activeInfo); - pTarget->activeInfo = NULL; - - taosMemoryFree(pTarget->key); - taosMemoryFree(pTarget); + hbFreeAppHbMgr(pTarget); } } diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index d3510b91ae..8c63046323 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -122,7 +122,10 @@ STscObj* taos_connect_internal(const char* ip, const char* user, const char* pas p->pTransporter = openTransporter(user, secretEncrypt, tsNumOfCores); p->pAppHbMgr = appHbMgrInit(p, key); taosHashPut(appInfo.pInstMap, key, strlen(key), &p, POINTER_BYTES); - + p->instKey = key; + key = NULL; + tscDebug("new app inst mgr %p, user:%s, ip:%s, port:%d", p, user, ip, port); + pInst = &p; } diff --git a/source/client/src/clientMsgHandler.c b/source/client/src/clientMsgHandler.c index 818762eff6..5c30df4ae2 100644 --- a/source/client/src/clientMsgHandler.c +++ b/source/client/src/clientMsgHandler.c @@ -87,7 +87,6 @@ int32_t processConnectRsp(void* param, const SDataBuf* pMsg, int32_t code) { // update the appInstInfo pTscObj->pAppInfo->clusterId = connectRsp.clusterId; - atomic_add_fetch_64(&pTscObj->pAppInfo->numOfConns, 1); pTscObj->connType = connectRsp.connType; diff --git a/source/libs/catalog/src/ctgAsync.c b/source/libs/catalog/src/ctgAsync.c index 6184d13533..139539821c 100644 --- a/source/libs/catalog/src/ctgAsync.c +++ b/source/libs/catalog/src/ctgAsync.c @@ -789,6 +789,8 @@ _return: int32_t ctgCallUserCb(void* param) { SCtgJob* pJob = (SCtgJob*)param; + + //taosSsleep(2); (*pJob->userFp)(&pJob->jobRes, pJob->userParam, pJob->jobResCode); diff --git a/tests/script/api/stopquery.c b/tests/script/api/stopquery.c index 4c7964c983..0008aa3303 100644 --- a/tests/script/api/stopquery.c +++ b/tests/script/api/stopquery.c @@ -368,6 +368,7 @@ void *closeThreadFp(void *arg) { while (true) { if (qParam->taos) { usleep(rand() % 10000); + //usleep(1000000); taos_close(qParam->taos); break; } From 49b2c544be13bd6c42c9b201cc9c6b30f36f71e9 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Mon, 27 Jun 2022 17:38:02 +0800 Subject: [PATCH 05/36] enh: change refMgt of rpc --- source/libs/transport/inc/transComm.h | 19 ++++++---- source/libs/transport/src/trans.c | 5 ++- source/libs/transport/src/transCli.c | 33 +++++++---------- source/libs/transport/src/transComm.c | 40 +++++++++++++------- source/libs/transport/src/transSvr.c | 53 ++++++++++----------------- 5 files changed, 74 insertions(+), 76 deletions(-) diff --git a/source/libs/transport/inc/transComm.h b/source/libs/transport/inc/transComm.h index 963a85922f..a593dcf817 100644 --- a/source/libs/transport/inc/transComm.h +++ b/source/libs/transport/inc/transComm.h @@ -252,7 +252,7 @@ int transSendAsync(SAsyncPool* pool, queue* mq); do { \ if (id > 0) { \ tTrace("handle step1"); \ - SExHandle* exh2 = transAcquireExHandle(refMgt, id); \ + SExHandle* exh2 = transAcquireExHandle(id); \ if (exh2 == NULL || id != exh2->refId) { \ tTrace("handle %p except, may already freed, ignore msg, ref1: %" PRIu64 ", ref2 : %" PRIu64 "", exh1, \ exh2 ? exh2->refId : 0, id); \ @@ -260,7 +260,7 @@ int transSendAsync(SAsyncPool* pool, queue* mq); } \ } else if (id == 0) { \ tTrace("handle step2"); \ - SExHandle* exh2 = transAcquireExHandle(refMgt, id); \ + SExHandle* exh2 = transAcquireExHandle(id); \ if (exh2 == NULL || id == exh2->refId) { \ tTrace("handle %p except, may already freed, ignore msg, ref1: %" PRIu64 ", ref2 : %" PRIu64 "", exh1, id, \ exh2 ? exh2->refId : 0); \ @@ -273,6 +273,7 @@ int transSendAsync(SAsyncPool* pool, queue* mq); goto _return2; \ } \ } while (0) + int transInitBuffer(SConnBuffer* buf); int transClearBuffer(SConnBuffer* buf); int transDestroyBuffer(SConnBuffer* buf); @@ -390,13 +391,15 @@ bool transEpSetIsEqual(SEpSet* a, SEpSet* b); */ void transThreadOnce(); -void transInitEnv(); +void transInit(); +void transCleanup(); + int32_t transOpenExHandleMgt(int size); -void transCloseExHandleMgt(int32_t mgt); -int64_t transAddExHandle(int32_t mgt, void* p); -int32_t transRemoveExHandle(int32_t mgt, int64_t refId); -SExHandle* transAcquireExHandle(int32_t mgt, int64_t refId); -int32_t transReleaseExHandle(int32_t mgt, int64_t refId); +void transCloseExHandleMgt(); +int64_t transAddExHandle(void* p); +int32_t transRemoveExHandle(int64_t refId); +SExHandle* transAcquireExHandle(int64_t refId); +int32_t transReleaseExHandle(int64_t refId); void transDestoryExHandle(void* handle); #ifdef __cplusplus diff --git a/source/libs/transport/src/trans.c b/source/libs/transport/src/trans.c index 4f7b19b539..d48bd85832 100644 --- a/source/libs/transport/src/trans.c +++ b/source/libs/transport/src/trans.c @@ -36,7 +36,7 @@ static int32_t transValidLocalFqdn(const char* localFqdn, uint32_t* ip) { return 0; } void* rpcOpen(const SRpcInit* pInit) { - transInitEnv(); + rpcInit(); SRpcInfo* pRpc = taosMemoryCalloc(1, sizeof(SRpcInfo)); if (pRpc == NULL) { @@ -82,7 +82,6 @@ void rpcClose(void* arg) { tInfo("start to close rpc"); SRpcInfo* pRpc = (SRpcInfo*)arg; (*taosCloseHandle[pRpc->connType])(pRpc->tcphandle); - transCloseExHandleMgt(pRpc->refMgt); taosMemoryFree(pRpc); return; @@ -170,11 +169,13 @@ void rpcSetDefaultAddr(void* thandle, const char* ip, const char* fqdn) { //} int32_t rpcInit() { + transInit(); // impl later return 0; } void rpcCleanup(void) { // impl later + transCleanup(); return; } diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 7374d1fffc..c960cb4c63 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -15,9 +15,6 @@ #ifdef USE_UV #include "transComm.h" -static int32_t transSCliInst = 0; -static int32_t refMgt = 0; - typedef struct SCliConn { T_REF_DECLARE() uv_connect_t connReq; @@ -503,12 +500,12 @@ static SCliConn* getConnFromPool(void* pool, char* ip, uint32_t port) { } static void allocConnRef(SCliConn* conn, bool update) { if (update) { - transRemoveExHandle(refMgt, conn->refId); + transRemoveExHandle(conn->refId); } SExHandle* exh = taosMemoryCalloc(1, sizeof(SExHandle)); exh->handle = conn; exh->pThrd = conn->hostThrd; - exh->refId = transAddExHandle(refMgt, exh); + exh->refId = transAddExHandle(exh); conn->refId = exh->refId; } static void addConnToPool(void* pool, SCliConn* conn) { @@ -602,9 +599,13 @@ static void cliDestroyConn(SCliConn* conn, bool clear) { tTrace("%s conn %p remove from conn pool", CONN_GET_INST_LABEL(conn), conn); QUEUE_REMOVE(&conn->conn); QUEUE_INIT(&conn->conn); - transRemoveExHandle(refMgt, conn->refId); + transRemoveExHandle(conn->refId); if (clear) { - uv_close((uv_handle_t*)conn->stream, cliDestroy); + if (uv_is_active((uv_handle_t*)conn->stream)) { + uv_close((uv_handle_t*)conn->stream, cliDestroy); + } else { + cliDestroy((uv_handle_t*)conn->stream); + } } } static void cliDestroy(uv_handle_t* handle) { @@ -735,7 +736,7 @@ static void cliHandleQuit(SCliMsg* pMsg, SCliThrd* pThrd) { } static void cliHandleRelease(SCliMsg* pMsg, SCliThrd* pThrd) { int64_t refId = (int64_t)(pMsg->msg.info.handle); - SExHandle* exh = transAcquireExHandle(refMgt, refId); + SExHandle* exh = transAcquireExHandle(refId); if (exh == NULL) { tDebug("%" PRId64 " already release", refId); } @@ -761,7 +762,7 @@ SCliConn* cliGetConn(SCliMsg* pMsg, SCliThrd* pThrd, bool* ignore) { SCliConn* conn = NULL; int64_t refId = (int64_t)(pMsg->msg.info.handle); if (refId != 0) { - SExHandle* exh = transAcquireExHandle(refMgt, refId); + SExHandle* exh = transAcquireExHandle(refId); if (exh == NULL) { *ignore = true; destroyCmsg(pMsg); @@ -769,7 +770,7 @@ SCliConn* cliGetConn(SCliMsg* pMsg, SCliThrd* pThrd, bool* ignore) { // assert(0); } else { conn = exh->handle; - transReleaseExHandle(refMgt, refId); + transReleaseExHandle(refId); } return conn; }; @@ -899,10 +900,6 @@ void* transInitClient(uint32_t ip, uint32_t port, char* label, int numOfThreads, } cli->pThreadObj[i] = pThrd; } - int ref = atomic_add_fetch_32(&transSCliInst, 1); - if (ref == 1) { - refMgt = transOpenExHandleMgt(50000); - } return cli; } @@ -1086,10 +1083,6 @@ void transCloseClient(void* arg) { } taosMemoryFree(cli->pThreadObj); taosMemoryFree(cli); - int ref = atomic_sub_fetch_32(&transSCliInst, 1); - if (ref == 0) { - transCloseExHandleMgt(refMgt); - } } void transRefCliHandle(void* handle) { if (handle == NULL) { @@ -1111,12 +1104,12 @@ void transUnrefCliHandle(void* handle) { } SCliThrd* transGetWorkThrdFromHandle(int64_t handle) { SCliThrd* pThrd = NULL; - SExHandle* exh = transAcquireExHandle(refMgt, handle); + SExHandle* exh = transAcquireExHandle(handle); if (exh == NULL) { return NULL; } pThrd = exh->pThrd; - transReleaseExHandle(refMgt, handle); + transReleaseExHandle(handle); return pThrd; } SCliThrd* transGetWorkThrd(STrans* trans, int64_t handle) { diff --git a/source/libs/transport/src/transComm.c b/source/libs/transport/src/transComm.c index bff7d79bd3..bc158bb316 100644 --- a/source/libs/transport/src/transComm.c +++ b/source/libs/transport/src/transComm.c @@ -16,7 +16,9 @@ #include "transComm.h" -// static TdThreadOnce transModuleInit = PTHREAD_ONCE_INIT; +static TdThreadOnce transModuleInit = PTHREAD_ONCE_INIT; + +static int32_t refMgt; int transAuthenticateMsg(void* pMsg, int msgLen, void* pAuth, void* pKey) { T_MD5_CTX context; @@ -478,35 +480,47 @@ bool transEpSetIsEqual(SEpSet* a, SEpSet* b) { return true; } -void transInitEnv() { - // +static void transInitEnv() { + refMgt = transOpenExHandleMgt(50000); uv_os_setenv("UV_TCP_SINGLE_ACCEPT", "1"); } +static void transDestroyEnv() { + // close ref + transCloseExHandleMgt(refMgt); +} +void transInit() { + // init env + taosThreadOnce(&transModuleInit, transInitEnv); +} +void transCleanup() { + // clean env + transDestroyEnv(); +} int32_t transOpenExHandleMgt(int size) { // added into once later return taosOpenRef(size, transDestoryExHandle); } -void transCloseExHandleMgt(int32_t mgt) { +void transCloseExHandleMgt() { // close ref - taosCloseRef(mgt); + taosCloseRef(refMgt); } -int64_t transAddExHandle(int32_t mgt, void* p) { +int64_t transAddExHandle(void* p) { // acquire extern handle - return taosAddRef(mgt, p); + return taosAddRef(refMgt, p); } -int32_t transRemoveExHandle(int32_t mgt, int64_t refId) { +int32_t transRemoveExHandle(int64_t refId) { // acquire extern handle - return taosRemoveRef(mgt, refId); + return taosRemoveRef(refMgt, refId); } -SExHandle* transAcquireExHandle(int32_t mgt, int64_t refId) { +SExHandle* transAcquireExHandle(int64_t refId) { // acquire extern handle - return (SExHandle*)taosAcquireRef(mgt, refId); + return (SExHandle*)taosAcquireRef(refMgt, refId); } -int32_t transReleaseExHandle(int32_t mgt, int64_t refId) { +int32_t transReleaseExHandle(int64_t refId) { // release extern handle - return taosReleaseRef(mgt, refId); + return taosReleaseRef(refMgt, refId); } void transDestoryExHandle(void* handle) { if (handle == NULL) { diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index 892d32696e..a78adfb397 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -19,9 +19,7 @@ static TdThreadOnce transModuleInit = PTHREAD_ONCE_INIT; -static char* notify = "a"; -static int32_t tranSSvrInst = 0; -static int32_t refMgt = 0; +static char* notify = "a"; typedef struct { int notifyCount; // @@ -274,7 +272,7 @@ static void uvHandleReq(SSvrConn* pConn) { // 2. once send out data, cli conn released to conn pool immediately // 3. not mixed with persist transMsg.info.ahandle = (void*)pHead->ahandle; - transMsg.info.handle = (void*)transAcquireExHandle(refMgt, pConn->refId); + transMsg.info.handle = (void*)transAcquireExHandle(pConn->refId); transMsg.info.refId = pConn->refId; transMsg.info.traceId = pHead->traceId; @@ -292,7 +290,7 @@ static void uvHandleReq(SSvrConn* pConn) { pConnInfo->clientPort = ntohs(pConn->addr.sin_port); tstrncpy(pConnInfo->user, pConn->user, sizeof(pConnInfo->user)); - transReleaseExHandle(refMgt, pConn->refId); + transReleaseExHandle(pConn->refId); STrans* pTransInst = pConn->pTransInst; (*pTransInst->cfp)(pTransInst->parent, &transMsg, NULL); @@ -523,15 +521,15 @@ void uvWorkerAsyncCb(uv_async_t* handle) { SExHandle* exh1 = transMsg.info.handle; int64_t refId = transMsg.info.refId; - SExHandle* exh2 = transAcquireExHandle(refMgt, refId); + SExHandle* exh2 = transAcquireExHandle(refId); if (exh2 == NULL || exh1 != exh2) { tTrace("handle except msg %p, ignore it", exh1); - transReleaseExHandle(refMgt, refId); + transReleaseExHandle(refId); destroySmsg(msg); continue; } msg->pConn = exh1->handle; - transReleaseExHandle(refMgt, refId); + transReleaseExHandle(refId); (*transAsyncHandle[msg->type])(msg, pThrd); } } @@ -773,8 +771,8 @@ static SSvrConn* createConn(void* hThrd) { SExHandle* exh = taosMemoryMalloc(sizeof(SExHandle)); exh->handle = pConn; exh->pThrd = pThrd; - exh->refId = transAddExHandle(refMgt, exh); - transAcquireExHandle(refMgt, exh->refId); + exh->refId = transAddExHandle(exh); + transAcquireExHandle(exh->refId); pConn->refId = exh->refId; transRefSrvHandle(pConn); @@ -803,14 +801,14 @@ static void destroyConnRegArg(SSvrConn* conn) { } } static int reallocConnRef(SSvrConn* conn) { - transReleaseExHandle(refMgt, conn->refId); - transRemoveExHandle(refMgt, conn->refId); + transReleaseExHandle(conn->refId); + transRemoveExHandle(conn->refId); // avoid app continue to send msg on invalid handle SExHandle* exh = taosMemoryMalloc(sizeof(SExHandle)); exh->handle = conn; exh->pThrd = conn->hostThrd; - exh->refId = transAddExHandle(refMgt, exh); - transAcquireExHandle(refMgt, exh->refId); + exh->refId = transAddExHandle(exh); + transAcquireExHandle(exh->refId); conn->refId = exh->refId; return 0; @@ -822,8 +820,8 @@ static void uvDestroyConn(uv_handle_t* handle) { } SWorkThrd* thrd = conn->hostThrd; - transReleaseExHandle(refMgt, conn->refId); - transRemoveExHandle(refMgt, conn->refId); + transReleaseExHandle(conn->refId); + transRemoveExHandle(conn->refId); tDebug("%s conn %p destroy", transLabel(thrd->pTransInst), conn); // uv_timer_stop(&conn->pTimer); @@ -871,12 +869,6 @@ void* transInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads, srv->port = port; uv_loop_init(srv->loop); - // taosThreadOnce(&transModuleInit, uvInitEnv); - int ref = atomic_add_fetch_32(&tranSSvrInst, 1); - if (ref == 1) { - refMgt = transOpenExHandleMgt(50000); - } - assert(0 == uv_pipe_init(srv->loop, &srv->pipeListen, 0)); #ifdef WINDOWS char pipeName[64]; @@ -1028,11 +1020,6 @@ void transCloseServer(void* arg) { taosMemoryFree(srv->pipe); taosMemoryFree(srv); - - int ref = atomic_sub_fetch_32(&tranSSvrInst, 1); - if (ref == 0) { - transCloseExHandleMgt(refMgt); - } } void transRefSrvHandle(void* handle) { @@ -1071,11 +1058,11 @@ void transReleaseSrvHandle(void* handle) { tTrace("%s conn %p start to release", transLabel(pThrd->pTransInst), exh->handle); transSendAsync(pThrd->asyncPool, &m->q); - transReleaseExHandle(refMgt, refId); + transReleaseExHandle(refId); return; _return1: tTrace("handle %p failed to send to release handle", exh); - transReleaseExHandle(refMgt, refId); + transReleaseExHandle(refId); return; _return2: tTrace("handle %p failed to send to release handle", exh); @@ -1100,12 +1087,12 @@ void transSendResponse(const STransMsg* msg) { STraceId* trace = (STraceId*)&msg->info.traceId; tGTrace("conn %p start to send resp (1/2)", exh->handle); transSendAsync(pThrd->asyncPool, &m->q); - transReleaseExHandle(refMgt, refId); + transReleaseExHandle(refId); return; _return1: tTrace("handle %p failed to send resp", exh); rpcFreeCont(msg->pCont); - transReleaseExHandle(refMgt, refId); + transReleaseExHandle(refId); return; _return2: tTrace("handle %p failed to send resp", exh); @@ -1129,13 +1116,13 @@ void transRegisterMsg(const STransMsg* msg) { tTrace("%s conn %p start to register brokenlink callback", transLabel(pThrd->pTransInst), exh->handle); transSendAsync(pThrd->asyncPool, &m->q); - transReleaseExHandle(refMgt, refId); + transReleaseExHandle(refId); return; _return1: tTrace("handle %p failed to register brokenlink", exh); rpcFreeCont(msg->pCont); - transReleaseExHandle(refMgt, refId); + transReleaseExHandle(refId); return; _return2: tTrace("handle %p failed to register brokenlink", exh); From 393a5dcf661c4681d4f40ef63b17bf56310a7390 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Mon, 27 Jun 2022 18:25:16 +0800 Subject: [PATCH 06/36] enh: change refMgt of rpc --- source/libs/transport/src/trans.c | 5 ----- 1 file changed, 5 deletions(-) diff --git a/source/libs/transport/src/trans.c b/source/libs/transport/src/trans.c index d48bd85832..3c09877106 100644 --- a/source/libs/transport/src/trans.c +++ b/source/libs/transport/src/trans.c @@ -163,11 +163,6 @@ void rpcSetDefaultAddr(void* thandle, const char* ip, const char* fqdn) { transSetDefaultAddr(thandle, ip, fqdn); } -// void rpcSetMsgTraceId(SRpcMsg* pMsg, STraceId uid) { -// SRpcHandleInfo* pInfo = &pMsg->info; -// pInfo->traceId = uid; -//} - int32_t rpcInit() { transInit(); // impl later From d40271646594a57a69e6a5ce996b18c3adda3dac Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Mon, 27 Jun 2022 18:29:28 +0800 Subject: [PATCH 07/36] enh: stop query --- include/client/taos.h | 1 + source/client/inc/clientInt.h | 1 + source/client/src/clientHb.c | 22 +++++++++++++------- source/client/src/clientMain.c | 11 ++++++++++ source/dnode/mnode/impl/src/mndMain.c | 5 +++++ source/libs/catalog/src/ctgAsync.c | 5 +++-- source/libs/catalog/src/ctgCache.c | 14 ++++++------- source/libs/scalar/src/scalar.c | 30 ++++++++++++++++++++++++--- tests/script/api/stopquery.c | 6 +++--- 9 files changed, 72 insertions(+), 23 deletions(-) diff --git a/include/client/taos.h b/include/client/taos.h index 669512e9b1..651faf5dcc 100644 --- a/include/client/taos.h +++ b/include/client/taos.h @@ -163,6 +163,7 @@ DLL_EXPORT TAOS_RES *taos_query(TAOS *taos, const char *sql); DLL_EXPORT TAOS_ROW taos_fetch_row(TAOS_RES *res); DLL_EXPORT int taos_result_precision(TAOS_RES *res); // get the time precision of result DLL_EXPORT void taos_free_result(TAOS_RES *res); +DLL_EXPORT void taos_kill_query(TAOS *taos); DLL_EXPORT int taos_field_count(TAOS_RES *res); DLL_EXPORT int taos_num_fields(TAOS_RES *res); DLL_EXPORT int taos_affected_rows(TAOS_RES *res); diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index 9225d67131..3b21258fe1 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -338,6 +338,7 @@ int hbHandleRsp(SClientHbBatchRsp* hbRsp); SAppHbMgr* appHbMgrInit(SAppInstInfo* pAppInstInfo, char* key); void appHbMgrCleanup(void); void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr); +void closeAllRequests(SHashObj *pRequests); // conn level int hbRegisterConn(SAppHbMgr* pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType); diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index 4f4bb83fe5..43e83dd501 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -66,25 +66,31 @@ static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog if (rsp->vgVersion < 0) { code = catalogRemoveDB(pCatalog, rsp->db, rsp->uid); } else { - SDBVgInfo vgInfo = {0}; - vgInfo.vgVersion = rsp->vgVersion; - vgInfo.hashMethod = rsp->hashMethod; - vgInfo.vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); - if (NULL == vgInfo.vgHash) { + SDBVgInfo *vgInfo = taosMemoryCalloc(1, sizeof(SDBVgInfo)); + if (NULL == vgInfo) { + return TSDB_CODE_TSC_OUT_OF_MEMORY; + } + + vgInfo->vgVersion = rsp->vgVersion; + vgInfo->hashMethod = rsp->hashMethod; + vgInfo->vgHash = taosHashInit(rsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); + if (NULL == vgInfo->vgHash) { + taosMemoryFree(vgInfo); tscError("hash init[%d] failed", rsp->vgNum); return TSDB_CODE_TSC_OUT_OF_MEMORY; } for (int32_t j = 0; j < rsp->vgNum; ++j) { SVgroupInfo *pInfo = taosArrayGet(rsp->pVgroupInfos, j); - if (taosHashPut(vgInfo.vgHash, &pInfo->vgId, sizeof(int32_t), pInfo, sizeof(SVgroupInfo)) != 0) { + if (taosHashPut(vgInfo->vgHash, &pInfo->vgId, sizeof(int32_t), pInfo, sizeof(SVgroupInfo)) != 0) { tscError("hash push failed, errno:%d", errno); - taosHashCleanup(vgInfo.vgHash); + taosHashCleanup(vgInfo->vgHash); + taosMemoryFree(vgInfo); return TSDB_CODE_TSC_OUT_OF_MEMORY; } } - catalogUpdateDBVgInfo(pCatalog, rsp->db, rsp->uid, &vgInfo); + catalogUpdateDBVgInfo(pCatalog, rsp->db, rsp->uid, vgInfo); } if (code) { diff --git a/source/client/src/clientMain.c b/source/client/src/clientMain.c index a24e6faf7f..be3efe0f98 100644 --- a/source/client/src/clientMain.c +++ b/source/client/src/clientMain.c @@ -181,6 +181,17 @@ void taos_free_result(TAOS_RES *res) { } } +void taos_kill_query(TAOS *taos) { + if (NULL == taos) { + return; + } + int64_t rid = *(int64_t*)taos; + + STscObj* pTscObj = acquireTscObj(rid); + closeAllRequests(pTscObj->pRequests); + releaseTscObj(rid); +} + int taos_field_count(TAOS_RES *res) { if (res == NULL || TD_RES_TMQ_META(res)) { return 0; diff --git a/source/dnode/mnode/impl/src/mndMain.c b/source/dnode/mnode/impl/src/mndMain.c index 4c9974ba1a..e03f82ba18 100644 --- a/source/dnode/mnode/impl/src/mndMain.c +++ b/source/dnode/mnode/impl/src/mndMain.c @@ -527,6 +527,11 @@ int32_t mndProcessSyncMsg(SRpcMsg *pMsg) { static int32_t mndCheckMnodeState(SRpcMsg *pMsg) { if (!IsReq(pMsg)) return 0; + if (pMsg->msgType == TDMT_VND_QUERY || pMsg->msgType == TDMT_VND_QUERY_CONTINUE || + pMsg->msgType == TDMT_VND_QUERY_HEARTBEAT || pMsg->msgType == TDMT_VND_FETCH || + pMsg->msgType == TDMT_VND_DROP_TASK) { + return 0; + } if (mndAcquireRpcRef(pMsg->info.node) == 0) return 0; if (pMsg->msgType == TDMT_MND_MQ_TIMER || pMsg->msgType == TDMT_MND_TELEM_TIMER || pMsg->msgType == TDMT_MND_TRANS_TIMER || pMsg->msgType == TDMT_MND_TTL_TIMER) { diff --git a/source/libs/catalog/src/ctgAsync.c b/source/libs/catalog/src/ctgAsync.c index 139539821c..683cbdb47c 100644 --- a/source/libs/catalog/src/ctgAsync.c +++ b/source/libs/catalog/src/ctgAsync.c @@ -789,8 +789,6 @@ _return: int32_t ctgCallUserCb(void* param) { SCtgJob* pJob = (SCtgJob*)param; - - //taosSsleep(2); (*pJob->userFp)(&pJob->jobRes, pJob->userParam, pJob->jobResCode); @@ -827,6 +825,9 @@ _return: qDebug("QID:0x%" PRIx64 " ctg call user callback with rsp %s", pJob->queryId, tstrerror(code)); pJob->jobResCode = code; + + taosSsleep(2); + qDebug("QID:0x%" PRIx64 " ctg after sleep", pJob->queryId); taosAsyncExec(ctgCallUserCb, pJob, NULL); diff --git a/source/libs/catalog/src/ctgCache.c b/source/libs/catalog/src/ctgCache.c index 8cd6c7d203..301cdbef20 100644 --- a/source/libs/catalog/src/ctgCache.c +++ b/source/libs/catalog/src/ctgCache.c @@ -1564,27 +1564,27 @@ int32_t ctgOpUpdateVgroup(SCtgCacheOperation *operation) { SCatalog* pCtg = msg->pCtg; if (NULL == dbInfo->vgHash) { - return TSDB_CODE_SUCCESS; + goto _return; } if (dbInfo->vgVersion < 0 || taosHashGetSize(dbInfo->vgHash) <= 0) { ctgError("invalid db vgInfo, dbFName:%s, vgHash:%p, vgVersion:%d, vgHashSize:%d", dbFName, dbInfo->vgHash, dbInfo->vgVersion, taosHashGetSize(dbInfo->vgHash)); - CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); + CTG_ERR_JRET(TSDB_CODE_APP_ERROR); } bool newAdded = false; SDbVgVersion vgVersion = {.dbId = msg->dbId, .vgVersion = dbInfo->vgVersion, .numOfTable = dbInfo->numOfTable}; SCtgDBCache *dbCache = NULL; - CTG_ERR_RET(ctgGetAddDBCache(msg->pCtg, dbFName, msg->dbId, &dbCache)); + CTG_ERR_JRET(ctgGetAddDBCache(msg->pCtg, dbFName, msg->dbId, &dbCache)); if (NULL == dbCache) { ctgInfo("conflict db update, ignore this update, dbFName:%s, dbId:0x%"PRIx64, dbFName, msg->dbId); - CTG_ERR_RET(TSDB_CODE_CTG_INTERNAL_ERROR); + CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR); } SCtgVgCache *vgCache = &dbCache->vgCache; - CTG_ERR_RET(ctgWLockVgInfo(msg->pCtg, dbCache)); + CTG_ERR_JRET(ctgWLockVgInfo(msg->pCtg, dbCache)); if (vgCache->vgInfo) { SDBVgInfo *vgInfo = vgCache->vgInfo; @@ -1593,14 +1593,14 @@ int32_t ctgOpUpdateVgroup(SCtgCacheOperation *operation) { ctgDebug("db vgVer is old, dbFName:%s, vgVer:%d, curVer:%d", dbFName, dbInfo->vgVersion, vgInfo->vgVersion); ctgWUnlockVgInfo(dbCache); - return TSDB_CODE_SUCCESS; + goto _return; } if (dbInfo->vgVersion == vgInfo->vgVersion && dbInfo->numOfTable == vgInfo->numOfTable) { ctgDebug("no new db vgVer or numOfTable, dbFName:%s, vgVer:%d, numOfTable:%d", dbFName, dbInfo->vgVersion, dbInfo->numOfTable); ctgWUnlockVgInfo(dbCache); - return TSDB_CODE_SUCCESS; + goto _return; } ctgFreeVgInfo(vgInfo); diff --git a/source/libs/scalar/src/scalar.c b/source/libs/scalar/src/scalar.c index 75a9bc3809..cef8ff7075 100644 --- a/source/libs/scalar/src/scalar.c +++ b/source/libs/scalar/src/scalar.c @@ -936,6 +936,25 @@ EDealRes sclCalcWalker(SNode* pNode, void* pContext) { return DEAL_RES_ERROR; } +int32_t sclExtendResRows(SScalarParam *pDst, SScalarParam *pSrc, SArray *pBlockList) { + SSDataBlock* pb = taosArrayGetP(pBlockList, 0); + SScalarParam *pLeft = taosMemoryCalloc(1, sizeof(SScalarParam)); + if (NULL == pLeft) { + sclError("calloc %d failed", (int32_t)sizeof(SScalarParam)); + SCL_ERR_RET(TSDB_CODE_QRY_OUT_OF_MEMORY); + } + + pLeft->numOfRows = pb->info.rows; + colInfoDataEnsureCapacity(pDst->columnData, pb->info.rows); + + _bin_scalar_fn_t OperatorFn = getBinScalarOperatorFn(OP_TYPE_ASSIGN); + OperatorFn(pLeft, pSrc, pDst, TSDB_ORDER_ASC); + + taosMemoryFree(pLeft); + + return TSDB_CODE_SUCCESS; +} + int32_t scalarCalculateConstants(SNode *pNode, SNode **pRes) { if (NULL == pNode) { SCL_ERR_RET(TSDB_CODE_QRY_INVALID_INPUT); @@ -983,9 +1002,14 @@ int32_t scalarCalculate(SNode *pNode, SArray *pBlockList, SScalarParam *pDst) { SCL_ERR_JRET(TSDB_CODE_QRY_APP_ERROR); } - colInfoDataEnsureCapacity(pDst->columnData, res->numOfRows); - colDataAssign(pDst->columnData, res->columnData, res->numOfRows, NULL); - pDst->numOfRows = res->numOfRows; + if (1 == res->numOfRows) { + SCL_ERR_JRET(sclExtendResRows(pDst, res, pBlockList)); + } else { + colInfoDataEnsureCapacity(pDst->columnData, res->numOfRows); + colDataAssign(pDst->columnData, res->columnData, res->numOfRows, NULL); + pDst->numOfRows = res->numOfRows; + } + taosHashRemove(ctx.pRes, (void *)&pNode, POINTER_BYTES); } diff --git a/tests/script/api/stopquery.c b/tests/script/api/stopquery.c index 0008aa3303..addd1272cf 100644 --- a/tests/script/api/stopquery.c +++ b/tests/script/api/stopquery.c @@ -36,7 +36,7 @@ int64_t st, et; char hostName[128]; char dbName[128]; char tbName[128]; -int32_t runTimes = 10000; +int32_t runTimes = 1; typedef struct { int id; @@ -367,8 +367,8 @@ void *closeThreadFp(void *arg) { SSP_CB_PARAM* qParam = (SSP_CB_PARAM*)arg; while (true) { if (qParam->taos) { - usleep(rand() % 10000); - //usleep(1000000); + //usleep(rand() % 10000); + usleep(1000000); taos_close(qParam->taos); break; } From 1d8a0d622678abf331f7553285098a42074f93da Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Mon, 27 Jun 2022 21:00:40 +0800 Subject: [PATCH 08/36] feat: refactor rpc code --- source/libs/transport/src/transSvr.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index a78adfb397..f1937c93fe 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -140,17 +140,6 @@ static void uvHandleRegister(SSvrMsg* msg, SWorkThrd* thrd); static void (*transAsyncHandle[])(SSvrMsg* msg, SWorkThrd* thrd) = {uvHandleResp, uvHandleQuit, uvHandleRelease, uvHandleRegister, NULL}; -static int32_t exHandlesMgt; - -// void uvInitEnv(); -// void uvOpenExHandleMgt(int size); -// void uvCloseExHandleMgt(); -// int64_t uvAddExHandle(void* p); -// int32_t uvRemoveExHandle(int64_t refId); -// int32_t uvReleaseExHandle(int64_t refId); -// void uvDestoryExHandle(void* handle); -// SExHandle* uvAcquireExHandle(int64_t refId); - static void uvDestroyConn(uv_handle_t* handle); // server and worker thread @@ -787,11 +776,15 @@ static void destroyConn(SSvrConn* conn, bool clear) { transDestroyBuffer(&conn->readBuf); if (clear) { - tTrace("conn %p to be destroyed", conn); - // uv_shutdown_t* req = taosMemoryMalloc(sizeof(uv_shutdown_t)); - uv_close((uv_handle_t*)conn->pTcp, uvDestroyConn); - // uv_close(conn->pTcp) - // uv_shutdown(req, (uv_stream_t*)conn->pTcp, uvShutDownCb); + if (uv_is_active((uv_handle_t*)conn->pTcp)) { + tTrace("conn %p to be destroyed", conn); + // uv_shutdown_t* req = taosMemoryMalloc(sizeof(uv_shutdown_t)); + uv_close((uv_handle_t*)conn->pTcp, uvDestroyConn); + // uv_close(conn->pTcp) + // uv_shutdown(req, (uv_stream_t*)conn->pTcp, uvShutDownCb); + } else { + uvDestroyConn((uv_handle_t*)conn->pTcp); + } } } static void destroyConnRegArg(SSvrConn* conn) { @@ -824,7 +817,6 @@ static void uvDestroyConn(uv_handle_t* handle) { transRemoveExHandle(conn->refId); tDebug("%s conn %p destroy", transLabel(thrd->pTransInst), conn); - // uv_timer_stop(&conn->pTimer); transQueueDestroy(&conn->srvMsgs); QUEUE_REMOVE(&conn->queue); From 1ab7e3a90b70848d1fd8fbbf18d6d402530f9e32 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Mon, 27 Jun 2022 21:07:41 +0800 Subject: [PATCH 09/36] feat: refactor rpc code --- source/libs/transport/src/trans.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/libs/transport/src/trans.c b/source/libs/transport/src/trans.c index 3c09877106..a26725c41c 100644 --- a/source/libs/transport/src/trans.c +++ b/source/libs/transport/src/trans.c @@ -165,11 +165,9 @@ void rpcSetDefaultAddr(void* thandle, const char* ip, const char* fqdn) { int32_t rpcInit() { transInit(); - // impl later return 0; } void rpcCleanup(void) { - // impl later transCleanup(); return; } From c9d9c3ffd03a8ae2fc5668780d71ef7cd056d9f1 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Tue, 28 Jun 2022 09:28:50 +0800 Subject: [PATCH 10/36] enh: stop query --- source/client/src/clientEnv.c | 5 ++++ source/client/src/clientHb.c | 5 ++++ source/libs/catalog/src/ctgAsync.c | 4 +-- source/libs/catalog/src/ctgCache.c | 2 +- source/libs/scheduler/inc/schedulerInt.h | 4 +-- source/libs/scheduler/src/schJob.c | 32 ++++++------------------ tests/script/api/stopquery.c | 5 ++-- 7 files changed, 23 insertions(+), 34 deletions(-) diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index f5b5c48d47..56b675596f 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -132,9 +132,14 @@ void closeAllRequests(SHashObj *pRequests) { void destroyAppInst(SAppInstInfo* pAppInfo) { tscDebug("destroy app inst mgr %p", pAppInfo); + + taosThreadMutexLock(&appInfo.mutex); hbRemoveAppHbMrg(&pAppInfo->pAppHbMgr); taosHashRemove(appInfo.pInstMap, pAppInfo->instKey, strlen(pAppInfo->instKey)); + + taosThreadMutexUnlock(&appInfo.mutex); + taosMemoryFreeClear(pAppInfo->instKey); closeTransporter(pAppInfo); diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index 43e83dd501..2de630e181 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -275,8 +275,11 @@ static int32_t hbAsyncCallBack(void *param, const SDataBuf *pMsg, int32_t code) int32_t rspNum = taosArrayGetSize(pRsp.rsps); + taosThreadMutexLock(&appInfo.mutex); + SAppInstInfo **pInst = taosHashGet(appInfo.pInstMap, key, strlen(key)); if (pInst == NULL || NULL == *pInst) { + taosThreadMutexUnlock(&appInfo.mutex); tscError("cluster not exist, key:%s", key); taosMemoryFreeClear(param); tFreeClientHbBatchRsp(&pRsp); @@ -300,6 +303,8 @@ static int32_t hbAsyncCallBack(void *param, const SDataBuf *pMsg, int32_t code) } } + taosThreadMutexUnlock(&appInfo.mutex); + tFreeClientHbBatchRsp(&pRsp); return code; diff --git a/source/libs/catalog/src/ctgAsync.c b/source/libs/catalog/src/ctgAsync.c index 683cbdb47c..455f2bd6a7 100644 --- a/source/libs/catalog/src/ctgAsync.c +++ b/source/libs/catalog/src/ctgAsync.c @@ -826,8 +826,8 @@ _return: pJob->jobResCode = code; - taosSsleep(2); - qDebug("QID:0x%" PRIx64 " ctg after sleep", pJob->queryId); + //taosSsleep(2); + //qDebug("QID:0x%" PRIx64 " ctg after sleep", pJob->queryId); taosAsyncExec(ctgCallUserCb, pJob, NULL); diff --git a/source/libs/catalog/src/ctgCache.c b/source/libs/catalog/src/ctgCache.c index 301cdbef20..77c1e5b8b1 100644 --- a/source/libs/catalog/src/ctgCache.c +++ b/source/libs/catalog/src/ctgCache.c @@ -1083,7 +1083,7 @@ int32_t ctgMetaRentUpdate(SCtgRentMgmt *mgmt, void *meta, int64_t id, int32_t si CTG_LOCK(CTG_WRITE, &slot->lock); if (NULL == slot->meta) { - qError("empty meta slot, id:0x%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type); + qDebug("empty meta slot, id:0x%"PRIx64", slot idx:%d, type:%d", id, widx, mgmt->type); CTG_ERR_JRET(TSDB_CODE_CTG_INTERNAL_ERROR); } diff --git a/source/libs/scheduler/inc/schedulerInt.h b/source/libs/scheduler/inc/schedulerInt.h index 843ce7d55a..a119795787 100644 --- a/source/libs/scheduler/inc/schedulerInt.h +++ b/source/libs/scheduler/inc/schedulerInt.h @@ -218,9 +218,7 @@ typedef struct SSchJob { int32_t levelIdx; SEpSet dataSrcEps; SHashObj *taskList; - SHashObj *execTasks; // executing tasks, key:taskid, value:SQueryTask* - SHashObj *succTasks; // succeed tasks, key:taskid, value:SQueryTask* - SHashObj *failTasks; // failed tasks, key:taskid, value:SQueryTask* + SHashObj *execTasks; // executing and executed tasks, key:taskid, value:SQueryTask* SHashObj *flowCtrl; // key is ep, element is SSchFlowControl SExplainCtx *explainCtx; diff --git a/source/libs/scheduler/src/schJob.c b/source/libs/scheduler/src/schJob.c index 7843482af3..89f355d78c 100644 --- a/source/libs/scheduler/src/schJob.c +++ b/source/libs/scheduler/src/schJob.c @@ -85,20 +85,6 @@ int32_t schInitJob(SSchedulerReq *pReq, SSchJob **pSchJob) { SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); } - pJob->succTasks = - taosHashInit(pReq->pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK); - if (NULL == pJob->succTasks) { - SCH_JOB_ELOG("taosHashInit %d succTasks failed", pReq->pDag->numOfSubplans); - SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); - } - - pJob->failTasks = - taosHashInit(pReq->pDag->numOfSubplans, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_ENTRY_LOCK); - if (NULL == pJob->failTasks) { - SCH_JOB_ELOG("taosHashInit %d failTasks failed", pReq->pDag->numOfSubplans); - SCH_ERR_JRET(TSDB_CODE_QRY_OUT_OF_MEMORY); - } - tsem_init(&pJob->rspSem, 0, 0); refId = taosAddRef(schMgmt.jobRef, pJob); @@ -724,6 +710,7 @@ int32_t schPushTaskToExecList(SSchJob *pJob, SSchTask *pTask) { return TSDB_CODE_SUCCESS; } +/* int32_t schMoveTaskToSuccList(SSchJob *pJob, SSchTask *pTask, bool *moved) { if (0 != taosHashRemove(pJob->execTasks, &pTask->taskId, sizeof(pTask->taskId))) { SCH_TASK_WLOG("remove task from execTask list failed, may not exist, status:%s", SCH_GET_TASK_STATUS_STR(pTask)); @@ -801,6 +788,7 @@ int32_t schMoveTaskToExecList(SSchJob *pJob, SSchTask *pTask, bool *moved) { return TSDB_CODE_SUCCESS; } +*/ int32_t schTaskCheckSetRetry(SSchJob *pJob, SSchTask *pTask, int32_t errCode, bool *needRetry) { int8_t status = 0; @@ -1047,9 +1035,7 @@ int32_t schProcessOnTaskFailure(SSchJob *pJob, SSchTask *pTask, int32_t errCode) if (!needRetry) { SCH_TASK_ELOG("task failed and no more retry, code:%s", tstrerror(errCode)); - if (SCH_GET_TASK_STATUS(pTask) == JOB_TASK_STATUS_EXECUTING) { - SCH_ERR_JRET(schMoveTaskToFailList(pJob, pTask, &moved)); - } else { + if (SCH_GET_TASK_STATUS(pTask) != JOB_TASK_STATUS_EXECUTING) { SCH_TASK_ELOG("task not in executing list, status:%s", SCH_GET_TASK_STATUS_STR(pTask)); SCH_ERR_JRET(TSDB_CODE_SCH_STATUS_ERROR); } @@ -1115,8 +1101,6 @@ int32_t schProcessOnTaskSuccess(SSchJob *pJob, SSchTask *pTask) { SCH_LOG_TASK_END_TS(pTask); - SCH_ERR_JRET(schMoveTaskToSuccList(pJob, pTask, &moved)); - SCH_SET_TASK_STATUS(pTask, JOB_TASK_STATUS_PARTIAL_SUCCEED); SCH_ERR_JRET(schRecordTaskSucceedNode(pJob, pTask)); @@ -1150,8 +1134,6 @@ int32_t schProcessOnTaskSuccess(SSchJob *pJob, SSchTask *pTask) { pJob->fetchTask = pTask; - SCH_ERR_JRET(schMoveTaskToExecList(pJob, pTask, &moved)); - SCH_RET(schProcessOnJobPartialSuccess(pJob)); } @@ -1466,8 +1448,8 @@ void schDropTaskInHashList(SSchJob *pJob, SHashObj *list) { void schDropJobAllTasks(SSchJob *pJob) { schDropTaskInHashList(pJob, pJob->execTasks); - schDropTaskInHashList(pJob, pJob->succTasks); - schDropTaskInHashList(pJob, pJob->failTasks); +// schDropTaskInHashList(pJob, pJob->succTasks); +// schDropTaskInHashList(pJob, pJob->failTasks); } int32_t schCancelJob(SSchJob *pJob) { @@ -1507,8 +1489,8 @@ void schFreeJobImpl(void *job) { schFreeFlowCtrl(pJob); taosHashCleanup(pJob->execTasks); - taosHashCleanup(pJob->failTasks); - taosHashCleanup(pJob->succTasks); +// taosHashCleanup(pJob->failTasks); +// taosHashCleanup(pJob->succTasks); taosHashCleanup(pJob->taskList); taosArrayDestroy(pJob->levels); diff --git a/tests/script/api/stopquery.c b/tests/script/api/stopquery.c index addd1272cf..4c7964c983 100644 --- a/tests/script/api/stopquery.c +++ b/tests/script/api/stopquery.c @@ -36,7 +36,7 @@ int64_t st, et; char hostName[128]; char dbName[128]; char tbName[128]; -int32_t runTimes = 1; +int32_t runTimes = 10000; typedef struct { int id; @@ -367,8 +367,7 @@ void *closeThreadFp(void *arg) { SSP_CB_PARAM* qParam = (SSP_CB_PARAM*)arg; while (true) { if (qParam->taos) { - //usleep(rand() % 10000); - usleep(1000000); + usleep(rand() % 10000); taos_close(qParam->taos); break; } From 6000e2e20b25c4fbfa3d6b1f35449bd3f1f5951b Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Tue, 28 Jun 2022 11:45:20 +0800 Subject: [PATCH 11/36] fix: fix rpc quit problem --- source/libs/transport/src/transCli.c | 2 +- source/libs/transport/src/transSvr.c | 6 +++++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index c960cb4c63..4535150240 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -601,7 +601,7 @@ static void cliDestroyConn(SCliConn* conn, bool clear) { QUEUE_INIT(&conn->conn); transRemoveExHandle(conn->refId); if (clear) { - if (uv_is_active((uv_handle_t*)conn->stream)) { + if (!uv_is_closing((uv_handle_t*)conn->stream)) { uv_close((uv_handle_t*)conn->stream, cliDestroy); } else { cliDestroy((uv_handle_t*)conn->stream); diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index f1937c93fe..8bd1c09042 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -431,10 +431,14 @@ static void uvPrepareSendData(SSvrMsg* smsg, uv_buf_t* wb) { } static void uvStartSendRespInternal(SSvrMsg* smsg) { + SSvrConn* pConn = smsg->pConn; + if (pConn->broken) { + return; + } + uv_buf_t wb; uvPrepareSendData(smsg, &wb); - SSvrConn* pConn = smsg->pConn; // uv_timer_stop(&pConn->pTimer); uv_write(&pConn->pWriter, (uv_stream_t*)pConn->pTcp, &wb, 1, uvOnSendCb); } From 421efe365b5dfebdebfa0a67c56b05ac3fb29f58 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Tue, 28 Jun 2022 14:41:56 +0800 Subject: [PATCH 12/36] fix: fix rpc quit problem --- source/libs/transport/src/transSvr.c | 17 +++-------------- 1 file changed, 3 insertions(+), 14 deletions(-) diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index 2adcd66e7a..c53cab13d5 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -332,19 +332,10 @@ void uvOnTimeoutCb(uv_timer_t* handle) { void uvOnSendCb(uv_write_t* req, int status) { SSvrConn* conn = req->data; - // transClearBuffer(&conn->readBuf); if (status == 0) { tTrace("conn %p data already was written on stream", conn); if (!transQueueEmpty(&conn->srvMsgs)) { SSvrMsg* msg = transQueuePop(&conn->srvMsgs); - // if (msg->type == Release && conn->status != ConnNormal) { - // conn->status = ConnNormal; - // transUnrefSrvHandle(conn); - // reallocConnRef(conn); - // destroySmsg(msg); - // transQueueClear(&conn->srvMsgs); - // return; - //} destroySmsg(msg); // send second data, just use for push if (!transQueueEmpty(&conn->srvMsgs)) { @@ -370,6 +361,7 @@ void uvOnSendCb(uv_write_t* req, int status) { } } } + transUnrefSrvHandle(conn); } else { tError("conn %p failed to write data, %s", conn, uv_err_name(status)); conn->broken = true; @@ -439,7 +431,7 @@ static void uvStartSendRespInternal(SSvrMsg* smsg) { uv_buf_t wb; uvPrepareSendData(smsg, &wb); - // uv_timer_stop(&pConn->pTimer); + transRefSrvHandle(pConn); uv_write(&pConn->pWriter, (uv_stream_t*)pConn->pTcp, &wb, 1, uvOnSendCb); } static void uvStartSendResp(SSvrMsg* smsg) { @@ -780,12 +772,9 @@ static void destroyConn(SSvrConn* conn, bool clear) { transDestroyBuffer(&conn->readBuf); if (clear) { - if (uv_is_active((uv_handle_t*)conn->pTcp)) { + if (!uv_is_closing((uv_handle_t*)conn->pTcp)) { tTrace("conn %p to be destroyed", conn); - // uv_shutdown_t* req = taosMemoryMalloc(sizeof(uv_shutdown_t)); uv_close((uv_handle_t*)conn->pTcp, uvDestroyConn); - // uv_close(conn->pTcp) - // uv_shutdown(req, (uv_stream_t*)conn->pTcp, uvShutDownCb); } else { uvDestroyConn((uv_handle_t*)conn->pTcp); } From 66e8e06861fa0316e46e3b1e4bfeab994c06fd53 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Tue, 28 Jun 2022 15:49:33 +0800 Subject: [PATCH 13/36] fix: fix rpc quit problem --- source/dnode/mgmt/node_mgmt/src/dmTransport.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/dnode/mgmt/node_mgmt/src/dmTransport.c b/source/dnode/mgmt/node_mgmt/src/dmTransport.c index 7e31cc3144..cf71b7c243 100644 --- a/source/dnode/mgmt/node_mgmt/src/dmTransport.c +++ b/source/dnode/mgmt/node_mgmt/src/dmTransport.c @@ -264,7 +264,7 @@ int32_t dmInitClient(SDnode *pDnode) { SDnodeTrans *pTrans = &pDnode->trans; SRpcInit rpcInit = {0}; - rpcInit.label = "DND"; + rpcInit.label = "DND-C"; rpcInit.numOfThreads = 1; rpcInit.cfp = (RpcCfp)dmProcessRpcMsg; rpcInit.sessions = 1024; @@ -298,7 +298,7 @@ int32_t dmInitServer(SDnode *pDnode) { SRpcInit rpcInit = {0}; strncpy(rpcInit.localFqdn, tsLocalFqdn, strlen(tsLocalFqdn)); rpcInit.localPort = tsServerPort; - rpcInit.label = "DND"; + rpcInit.label = "DND-S"; rpcInit.numOfThreads = tsNumOfRpcThreads; rpcInit.cfp = (RpcCfp)dmProcessRpcMsg; rpcInit.sessions = tsMaxShellConns; From c0183e1fa8962b8630b2f45c5ae232837ae3e05f Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 28 Jun 2022 15:50:47 +0800 Subject: [PATCH 14/36] fix: duplicate response while set standby in vnode --- source/dnode/mgmt/mgmt_vnode/src/vmWorker.c | 8 +------- source/dnode/vnode/src/vnd/vnodeSync.c | 2 +- 2 files changed, 2 insertions(+), 8 deletions(-) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c index 5e1ef23f1c..91ef292360 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmWorker.c @@ -107,13 +107,7 @@ static void vmProcessSyncQueue(SQueueInfo *pInfo, STaosQall *qall, int32_t numOf const STraceId *trace = &pMsg->info.traceId; dGTrace("vgId:%d, msg:%p get from vnode-sync queue", pVnode->vgId, pMsg); - int32_t code = vnodeProcessSyncReq(pVnode->pImpl, pMsg, NULL); - if (code != 0) { - if (terrno != 0) code = terrno; - dGError("vgId:%d, msg:%p failed to sync since %s", pVnode->vgId, pMsg, terrstr()); - vmSendRsp(pMsg, code); - } - + int32_t code = vnodeProcessSyncReq(pVnode->pImpl, pMsg, NULL); // no response here dGTrace("vgId:%d, msg:%p is freed, code:0x%x", pVnode->vgId, pMsg, code); rpcFreeCont(pMsg->pCont); taosFreeQitem(pMsg); diff --git a/source/dnode/vnode/src/vnd/vnodeSync.c b/source/dnode/vnode/src/vnd/vnodeSync.c index b998f40195..7cf6ccd13f 100644 --- a/source/dnode/vnode/src/vnd/vnodeSync.c +++ b/source/dnode/vnode/src/vnd/vnodeSync.c @@ -73,7 +73,7 @@ static int32_t vnodeSetStandBy(SVnode *pVnode) { vInfo("vgId:%d, set standby success", TD_VID(pVnode)); return 0; } else { - vError("vgId:%d, failed to set standby since %s", TD_VID(pVnode), terrstr()); + vError("vgId:%d, failed to set standby after leader transfer since %s", TD_VID(pVnode), terrstr()); return -1; } } From bb95a3427eb25fc35503816edfc2d93738c87261 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Tue, 28 Jun 2022 19:19:40 +0800 Subject: [PATCH 15/36] fix: fix rpc quit problem --- source/libs/transport/src/trans.c | 1 + source/libs/transport/src/transCli.c | 16 +++++++++++++--- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/source/libs/transport/src/trans.c b/source/libs/transport/src/trans.c index a26725c41c..acf9b6b4a7 100644 --- a/source/libs/transport/src/trans.c +++ b/source/libs/transport/src/trans.c @@ -83,6 +83,7 @@ void rpcClose(void* arg) { SRpcInfo* pRpc = (SRpcInfo*)arg; (*taosCloseHandle[pRpc->connType])(pRpc->tcphandle); taosMemoryFree(pRpc); + tInfo("finish to close rpc"); return; } diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index b0d9b85625..5274c5c593 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -316,8 +316,9 @@ void cliHandleResp(SCliConn* conn) { if (CONN_NO_PERSIST_BY_APP(conn)) { pMsg = transQueuePop(&conn->cliMsgs); - pCtx = pMsg->ctx; - transMsg.info.ahandle = pCtx->ahandle; + + pCtx = pMsg ? pMsg->ctx : NULL; + transMsg.info.ahandle = pCtx ? pCtx->ahandle : NULL; tDebug("%s conn %p get ahandle %p, persist: 0", CONN_GET_INST_LABEL(conn), conn, transMsg.info.ahandle); } else { uint64_t ahandle = (uint64_t)pHead->ahandle; @@ -501,6 +502,7 @@ static SCliConn* getConnFromPool(void* pool, char* ip, uint32_t port) { static void allocConnRef(SCliConn* conn, bool update) { if (update) { transRemoveExHandle(conn->refId); + conn->refId = -1; } SExHandle* exh = taosMemoryCalloc(1, sizeof(SExHandle)); exh->handle = conn; @@ -600,6 +602,8 @@ static void cliDestroyConn(SCliConn* conn, bool clear) { QUEUE_REMOVE(&conn->conn); QUEUE_INIT(&conn->conn); transRemoveExHandle(conn->refId); + conn->refId = -1; + if (clear) { if (!uv_is_closing((uv_handle_t*)conn->stream)) { uv_close((uv_handle_t*)conn->stream, cliDestroy); @@ -609,8 +613,11 @@ static void cliDestroyConn(SCliConn* conn, bool clear) { } } static void cliDestroy(uv_handle_t* handle) { + if (uv_handle_get_type(handle) != UV_TCP || handle->data == NULL) return; SCliConn* conn = handle->data; + transRemoveExHandle(conn->refId); taosMemoryFree(conn->ip); + conn->stream->data = NULL; taosMemoryFree(conn->stream); transCtxCleanup(&conn->ctx); transQueueDestroy(&conn->cliMsgs); @@ -968,7 +975,7 @@ void cliSendQuit(SCliThrd* thrd) { } void cliWalkCb(uv_handle_t* handle, void* arg) { if (!uv_is_closing(handle)) { - uv_close(handle, NULL); + uv_close(handle, cliDestroy); } } @@ -1106,8 +1113,11 @@ SCliThrd* transGetWorkThrdFromHandle(int64_t handle) { SCliThrd* pThrd = NULL; SExHandle* exh = transAcquireExHandle(handle); if (exh == NULL) { + tTrace("no, no %" PRId64 "", handle); return NULL; } + tTrace("YY %" PRId64 "", handle); + pThrd = exh->pThrd; transReleaseExHandle(handle); return pThrd; From c1690655fc20fea391d58aeb966827710dac169e Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Tue, 28 Jun 2022 19:21:04 +0800 Subject: [PATCH 16/36] fix: fix rpc quit problem --- source/libs/transport/src/transCli.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 5274c5c593..7d7e459aa8 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -1113,11 +1113,8 @@ SCliThrd* transGetWorkThrdFromHandle(int64_t handle) { SCliThrd* pThrd = NULL; SExHandle* exh = transAcquireExHandle(handle); if (exh == NULL) { - tTrace("no, no %" PRId64 "", handle); return NULL; } - tTrace("YY %" PRId64 "", handle); - pThrd = exh->pThrd; transReleaseExHandle(handle); return pThrd; From 1fc9eaadd08c7ff8ee62962d3daa2a8422b73be3 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Tue, 28 Jun 2022 20:30:17 +0800 Subject: [PATCH 17/36] fix: fix rpc quit problem --- source/client/src/clientEnv.c | 14 +++++++------- source/libs/transport/src/transCli.c | 8 +++++--- 2 files changed, 12 insertions(+), 10 deletions(-) diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 22b491994e..9bb9c6da05 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -40,7 +40,7 @@ volatile int32_t tscInitRes = 0; static int32_t registerRequest(SRequestObj *pRequest) { STscObj *pTscObj = acquireTscObj(pRequest->pTscObj->id); if (NULL == pTscObj) { - terrno = TSDB_CODE_TSC_DISCONNECTED; + terrno = TSDB_CODE_TSC_DISCONNECTED; return terrno; } @@ -91,7 +91,7 @@ void closeTransporter(SAppInstInfo *pAppInfo) { static bool clientRpcRfp(int32_t code, tmsg_t msgType) { if (code == TSDB_CODE_RPC_REDIRECT || code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_NODE_NOT_DEPLOYED || code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_APP_NOT_READY) { - if (msgType == TDMT_VND_QUERY || msgType == TDMT_VND_FETCH) { + if (/*msgType == TDMT_VND_QUERY ||*/ msgType == TDMT_VND_FETCH) { return false; } return true; @@ -133,11 +133,11 @@ void closeAllRequests(SHashObj *pRequests) { } } -void destroyAppInst(SAppInstInfo* pAppInfo) { +void destroyAppInst(SAppInstInfo *pAppInfo) { tscDebug("destroy app inst mgr %p", pAppInfo); taosThreadMutexLock(&appInfo.mutex); - + hbRemoveAppHbMrg(&pAppInfo->pAppHbMgr); taosHashRemove(appInfo.pInstMap, pAppInfo->instKey, strlen(pAppInfo->instKey)); @@ -145,10 +145,10 @@ void destroyAppInst(SAppInstInfo* pAppInfo) { taosMemoryFreeClear(pAppInfo->instKey); closeTransporter(pAppInfo); - + taosThreadMutexLock(&pAppInfo->qnodeMutex); taosArrayDestroy(pAppInfo->pQnodeList); - taosThreadMutexUnlock(&pAppInfo->qnodeMutex); + taosThreadMutexUnlock(&pAppInfo->qnodeMutex); taosMemoryFree(pAppInfo); } @@ -161,7 +161,7 @@ void destroyTscObj(void *pObj) { int64_t connNum = atomic_sub_fetch_64(&pTscObj->pAppInfo->numOfConns, 1); closeAllRequests(pTscObj->pRequests); schedulerStopQueryHb(pTscObj->pAppInfo->pTransporter); - tscDebug("connObj 0x%" PRIx64 " p:%p destroyed, remain inst totalConn:%" PRId64, pTscObj->id, pTscObj, + tscDebug("connObj 0x%" PRIx64 " p:%p destroyed, remain inst totalConn:%" PRId64, pTscObj->id, pTscObj, pTscObj->pAppInfo->numOfConns); if (0 == connNum) { diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 7d7e459aa8..612f0cc3fd 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -464,8 +464,7 @@ void* destroyConnPool(void* pool) { SConnList* connList = taosHashIterate((SHashObj*)pool, NULL); while (connList != NULL) { while (!QUEUE_IS_EMPTY(&connList->conn)) { - queue* h = QUEUE_HEAD(&connList->conn); - // QUEUE_REMOVE(h); + queue* h = QUEUE_HEAD(&connList->conn); SCliConn* c = QUEUE_DATA(h, SCliConn, conn); cliDestroyConn(c, true); } @@ -613,7 +612,10 @@ static void cliDestroyConn(SCliConn* conn, bool clear) { } } static void cliDestroy(uv_handle_t* handle) { - if (uv_handle_get_type(handle) != UV_TCP || handle->data == NULL) return; + if (uv_handle_get_type(handle) != UV_TCP || handle->data == NULL) { + return; + } + SCliConn* conn = handle->data; transRemoveExHandle(conn->refId); taosMemoryFree(conn->ip); From 3625a93df1b216dc39f4219c29500405dd104b71 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Tue, 28 Jun 2022 20:32:06 +0800 Subject: [PATCH 18/36] fix: fix rpc quit problem --- source/client/src/clientEnv.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 9bb9c6da05..014f8f55ac 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -91,7 +91,7 @@ void closeTransporter(SAppInstInfo *pAppInfo) { static bool clientRpcRfp(int32_t code, tmsg_t msgType) { if (code == TSDB_CODE_RPC_REDIRECT || code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_NODE_NOT_DEPLOYED || code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_APP_NOT_READY) { - if (/*msgType == TDMT_VND_QUERY ||*/ msgType == TDMT_VND_FETCH) { + if (msgType == TDMT_VND_QUERY || msgType == TDMT_VND_FETCH) { return false; } return true; From ad29b90cc2836473994d87ae81f7ccd32722a9a3 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Tue, 28 Jun 2022 21:43:40 +0800 Subject: [PATCH 19/36] fix: fix rpc quit problem --- include/libs/transport/trpc.h | 2 +- source/libs/transport/inc/transComm.h | 1 + source/libs/transport/src/transCli.c | 24 ++++++++++++++++++++++++ source/libs/transport/src/transSvr.c | 22 +++------------------- 4 files changed, 29 insertions(+), 20 deletions(-) diff --git a/include/libs/transport/trpc.h b/include/libs/transport/trpc.h index 2b8c6a895e..27153b5efd 100644 --- a/include/libs/transport/trpc.h +++ b/include/libs/transport/trpc.h @@ -45,7 +45,7 @@ typedef struct SRpcHandleInfo { int32_t noResp; // has response or not(default 0, 0: resp, 1: no resp); int32_t persistHandle; // persist handle or not STraceId traceId; - // int64_t traceId; + int8_t hasEpSet; // app info void *ahandle; // app handle set by client diff --git a/source/libs/transport/inc/transComm.h b/source/libs/transport/inc/transComm.h index 55db0b129a..1283946240 100644 --- a/source/libs/transport/inc/transComm.h +++ b/source/libs/transport/inc/transComm.h @@ -148,6 +148,7 @@ typedef struct { char release : 2; char secured : 2; char spi : 2; + char hasEpSet : 2; // contain epset or not, 0(default): no epset, 1: contain epset char user[TSDB_UNI_LEN]; STraceId traceId; diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index aba2e6957b..9e52fc21a3 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -312,6 +312,7 @@ void cliHandleResp(SCliConn* conn) { transMsg.msgType = pHead->msgType; transMsg.info.ahandle = NULL; transMsg.info.traceId = pHead->traceId; + transMsg.info.hasEpSet = pHead->hasEpSet; SCliMsg* pMsg = NULL; STransConnCtx* pCtx = NULL; @@ -1014,6 +1015,23 @@ void cliCompareAndSwap(int8_t* val, int8_t exp, int8_t newVal) { *val = newVal; } } + +bool cliTryToExtractEpSet(STransMsg* pResp, SEpSet* dst) { + if (pResp == NULL || pResp->info.hasEpSet == 0) { + return false; + } + tDeserializeSEpSet(pResp->pCont, pResp->contLen, dst); + int32_t tlen = tSerializeSEpSet(NULL, 0, dst); + + int32_t bufLen = pResp->contLen - tlen; + char* buf = rpcMallocCont(bufLen); + + memcpy(buf, pResp->pCont + tlen, bufLen); + + pResp->pCont = buf; + pResp->contLen = bufLen; + return true; +} int cliAppCb(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) { SCliThrd* pThrd = pConn->hostThrd; STrans* pTransInst = pThrd->pTransInst; @@ -1058,6 +1076,12 @@ int cliAppCb(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) { } STraceId* trace = &pResp->info.traceId; + + if (cliTryToExtractEpSet(pResp, &pCtx->epSet)) { + char tbuf[256] = {0}; + EPSET_DEBUG_STR(&pCtx->epSet, tbuf); + tGTrace("%s conn %p extract epset from msg", CONN_GET_INST_LABEL(pConn), pConn); + } if (pCtx->pSem != NULL) { tGTrace("%s conn %p(sync) handle resp", CONN_GET_INST_LABEL(pConn), pConn); if (pCtx->pRsp == NULL) { diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index 08363b3c7c..ed1abd10f7 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -144,15 +144,6 @@ static void (*transAsyncHandle[])(SSvrMsg* msg, SWorkThrd* thrd) = {uvHandleResp static int32_t exHandlesMgt; -// void uvInitEnv(); -// void uvOpenExHandleMgt(int size); -// void uvCloseExHandleMgt(); -// int64_t uvAddExHandle(void* p); -// int32_t uvRemoveExHandle(int64_t refId); -// int32_t uvReleaseExHandle(int64_t refId); -// void uvDestoryExHandle(void* handle); -// SExHandle* uvAcquireExHandle(int64_t refId); - static void uvDestroyConn(uv_handle_t* handle); // server and worker thread @@ -350,16 +341,8 @@ void uvOnSendCb(uv_write_t* req, int status) { tTrace("conn %p data already was written on stream", conn); if (!transQueueEmpty(&conn->srvMsgs)) { SSvrMsg* msg = transQueuePop(&conn->srvMsgs); - // if (msg->type == Release && conn->status != ConnNormal) { - // conn->status = ConnNormal; - // transUnrefSrvHandle(conn); - // reallocConnRef(conn); - // destroySmsg(msg); - // transQueueClear(&conn->srvMsgs); - // return; - //} destroySmsg(msg); - // send second data, just use for push + // send cached data if (!transQueueEmpty(&conn->srvMsgs)) { msg = (SSvrMsg*)transQueueGet(&conn->srvMsgs, 0); if (msg->type == Register && conn->status == ConnAcquire) { @@ -396,7 +379,6 @@ static void uvOnPipeWriteCb(uv_write_t* req, int status) { tError("fail to dispatch conn to work thread"); } uv_close((uv_handle_t*)req->data, uvFreeCb); - // taosMemoryFree(req->data); taosMemoryFree(req); } @@ -410,6 +392,7 @@ static void uvPrepareSendData(SSvrMsg* smsg, uv_buf_t* wb) { STransMsgHead* pHead = transHeadFromCont(pMsg->pCont); pHead->ahandle = (uint64_t)pMsg->info.ahandle; pHead->traceId = pMsg->info.traceId; + pHead->hasEpSet = pMsg->info.hasEpSet; if (pConn->status == ConnNormal) { pHead->msgType = pConn->inType + 1; @@ -422,6 +405,7 @@ static void uvPrepareSendData(SSvrMsg* smsg, uv_buf_t* wb) { transUnrefSrvHandle(pConn); } else { pHead->msgType = pMsg->msgType; + // set up resp msg type if (pHead->msgType == 0 && transMsgLenFromCont(pMsg->contLen) == sizeof(STransMsgHead)) pHead->msgType = pConn->inType + 1; } From 51fc9a7b24d8f0d716c1659c73c4ca25383de254 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Wed, 29 Jun 2022 09:52:23 +0800 Subject: [PATCH 20/36] fix: fix explain issue --- source/libs/scheduler/src/schJob.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/libs/scheduler/src/schJob.c b/source/libs/scheduler/src/schJob.c index 89f355d78c..53f9a934f8 100644 --- a/source/libs/scheduler/src/schJob.c +++ b/source/libs/scheduler/src/schJob.c @@ -1631,7 +1631,7 @@ int32_t schExecJobImpl(SSchedulerReq *pReq, SSchJob *pJob, bool sync) { SCH_ERR_JRET(schBeginOperation(pJob, SCH_OP_EXEC, sync)); if (EXPLAIN_MODE_STATIC == pReq->pDag->explainInfo.mode) { - code = schLaunchStaticExplainJob(pReq, pJob, true); + code = schLaunchStaticExplainJob(pReq, pJob, sync); } else { code = schLaunchJob(pJob); if (sync) { From 32c00b380170ce51e5fe793642ee43579d3105e2 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 29 Jun 2022 10:04:02 +0800 Subject: [PATCH 21/36] modify case --- source/client/src/clientEnv.c | 3 -- source/client/src/clientImpl.c | 44 +++++++++---------- source/dnode/mgmt/node_mgmt/src/dmTransport.c | 3 -- .../drop_dnode_has_multi_vnode_replica1.sim | 2 +- 4 files changed, 23 insertions(+), 29 deletions(-) diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index 014f8f55ac..d150d68c8d 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -91,9 +91,6 @@ void closeTransporter(SAppInstInfo *pAppInfo) { static bool clientRpcRfp(int32_t code, tmsg_t msgType) { if (code == TSDB_CODE_RPC_REDIRECT || code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_NODE_NOT_DEPLOYED || code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_APP_NOT_READY) { - if (msgType == TDMT_VND_QUERY || msgType == TDMT_VND_FETCH) { - return false; - } return true; } else { return false; diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index 8c63046323..ce993d7d6d 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -125,7 +125,7 @@ STscObj* taos_connect_internal(const char* ip, const char* user, const char* pas p->instKey = key; key = NULL; tscDebug("new app inst mgr %p, user:%s, ip:%s, port:%d", p, user, ip, port); - + pInst = &p; } @@ -620,12 +620,12 @@ int32_t scheduleAsyncQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNod .requestId = pRequest->requestId, .requestObjRefId = pRequest->self}; SSchedulerReq req = {.pConn = &conn, - .pNodeList = pNodeList, - .pDag = pDag, - .sql = pRequest->sqlstr, - .startTs = pRequest->metric.start, - .fp = schdExecCallback, - .cbParam = &res}; + .pNodeList = pNodeList, + .pDag = pDag, + .sql = pRequest->sqlstr, + .startTs = pRequest->metric.start, + .fp = schdExecCallback, + .cbParam = &res}; int32_t code = schedulerAsyncExecJob(&req, &pRequest->body.queryJob); @@ -672,13 +672,13 @@ int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList .requestId = pRequest->requestId, .requestObjRefId = pRequest->self}; SSchedulerReq req = {.pConn = &conn, - .pNodeList = pNodeList, - .pDag = pDag, - .sql = pRequest->sqlstr, - .startTs = pRequest->metric.start, - .fp = NULL, - .cbParam = NULL, - .reqKilled = &pRequest->killed}; + .pNodeList = pNodeList, + .pDag = pDag, + .sql = pRequest->sqlstr, + .startTs = pRequest->metric.start, + .fp = NULL, + .cbParam = NULL, + .reqKilled = &pRequest->killed}; int32_t code = schedulerExecJob(&req, &pRequest->body.queryJob, &res); pRequest->body.resInfo.execRes = res.res; @@ -877,15 +877,15 @@ SRequestObj* launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, bool keepQue } break; case QUERY_EXEC_MODE_SCHEDULE: { - SArray* pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad)); + SArray* pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad)); SQueryPlan* pDag = NULL; code = getPlan(pRequest, pQuery, &pDag, pMnodeList); - if (TSDB_CODE_SUCCESS == code) { + if (TSDB_CODE_SUCCESS == code) { pRequest->body.subplanNum = pDag->numOfSubplans; if (!pRequest->validateOnly) { SArray* pNodeList = NULL; buildSyncExecNodeList(pRequest, &pNodeList, pMnodeList); - + code = scheduleQuery(pRequest, pDag, pNodeList); taosArrayDestroy(pNodeList); } @@ -966,7 +966,7 @@ void launchAsyncQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultM .pUser = pRequest->pTscObj->user}; SAppInstInfo* pAppInfo = getAppInfo(pRequest); - SQueryPlan* pDag = NULL; + SQueryPlan* pDag = NULL; code = qCreateQueryPlan(&cxt, &pDag, pMnodeList); if (code) { tscError("0x%" PRIx64 " failed to create query plan, code:%s 0x%" PRIx64, pRequest->self, tstrerror(code), @@ -1336,7 +1336,7 @@ TAOS* taos_connect_auth(const char* ip, const char* user, const char* auth, cons STscObj* pObj = taos_connect_internal(ip, user, NULL, auth, db, port, CONN_TYPE__QUERY); if (pObj) { - int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t)); + int64_t* rid = taosMemoryCalloc(1, sizeof(int64_t)); *rid = pObj->id; return (TAOS*)rid; } @@ -2027,7 +2027,7 @@ void taosAsyncQueryImpl(TAOS* taos, const char* sql, __taos_async_fn_t fp, void* return; } - int64_t rid = *(int64_t*)taos; + int64_t rid = *(int64_t*)taos; STscObj* pTscObj = acquireTscObj(rid); if (pTscObj == NULL || sql == NULL || NULL == fp) { terrno = TSDB_CODE_INVALID_PARA; @@ -2063,7 +2063,7 @@ void taosAsyncQueryImpl(TAOS* taos, const char* sql, __taos_async_fn_t fp, void* pRequest->body.queryFp = fp; pRequest->body.param = param; doAsyncQuery(pRequest, false); - releaseTscObj(rid); + releaseTscObj(rid); } TAOS_RES* taosQueryImpl(TAOS* taos, const char* sql, bool validateOnly) { @@ -2072,7 +2072,7 @@ TAOS_RES* taosQueryImpl(TAOS* taos, const char* sql, bool validateOnly) { return NULL; } - int64_t rid = *(int64_t*)taos; + int64_t rid = *(int64_t*)taos; STscObj* pTscObj = acquireTscObj(rid); if (pTscObj == NULL || sql == NULL) { terrno = TSDB_CODE_TSC_DISCONNECTED; diff --git a/source/dnode/mgmt/node_mgmt/src/dmTransport.c b/source/dnode/mgmt/node_mgmt/src/dmTransport.c index cf71b7c243..1c5d74bb94 100644 --- a/source/dnode/mgmt/node_mgmt/src/dmTransport.c +++ b/source/dnode/mgmt/node_mgmt/src/dmTransport.c @@ -251,9 +251,6 @@ static inline void dmReleaseHandle(SRpcHandleInfo *pHandle, int8_t type) { static bool rpcRfp(int32_t code, tmsg_t msgType) { if (code == TSDB_CODE_RPC_REDIRECT || code == TSDB_CODE_RPC_NETWORK_UNAVAIL || code == TSDB_CODE_NODE_NOT_DEPLOYED || code == TSDB_CODE_SYN_NOT_LEADER || code == TSDB_CODE_APP_NOT_READY) { - if (msgType == TDMT_VND_QUERY || msgType == TDMT_VND_FETCH) { - return false; - } return true; } else { return false; diff --git a/tests/script/tsim/dnode/drop_dnode_has_multi_vnode_replica1.sim b/tests/script/tsim/dnode/drop_dnode_has_multi_vnode_replica1.sim index 78bca45f31..20eac3836c 100644 --- a/tests/script/tsim/dnode/drop_dnode_has_multi_vnode_replica1.sim +++ b/tests/script/tsim/dnode/drop_dnode_has_multi_vnode_replica1.sim @@ -124,7 +124,7 @@ if $data(5)[3] != 3 then return -1 endi -#sql reset query cache +sql reset query cache print =============== step4: select data sql show d1.tables From 54caa629ac568bca0a39aa817180e56c23bff5c2 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 29 Jun 2022 10:08:44 +0800 Subject: [PATCH 22/36] fix:disable ttl temporary --- source/dnode/vnode/src/vnd/vnodeSvr.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index 72c766e0ae..c50bcbc60b 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -136,7 +136,7 @@ int32_t vnodeProcessWriteReq(SVnode *pVnode, SRpcMsg *pMsg, int64_t version, SRp if (vnodeProcessDropTbReq(pVnode, version, pReq, len, pRsp) < 0) goto _err; break; case TDMT_VND_DROP_TTL_TABLE: - if (vnodeProcessDropTtlTbReq(pVnode, version, pReq, len, pRsp) < 0) goto _err; + //if (vnodeProcessDropTtlTbReq(pVnode, version, pReq, len, pRsp) < 0) goto _err; break; case TDMT_VND_CREATE_SMA: { if (vnodeProcessCreateTSmaReq(pVnode, version, pReq, len, pRsp) < 0) goto _err; From 286a7c6d95bf9bbd75f7c23202291891a99057d1 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 29 Jun 2022 10:22:28 +0800 Subject: [PATCH 23/36] fix: fix rpc quit problem --- tests/system-test/2-query/json_tag.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/system-test/2-query/json_tag.py b/tests/system-test/2-query/json_tag.py index 9b96b6ebd0..ff6a249d3e 100644 --- a/tests/system-test/2-query/json_tag.py +++ b/tests/system-test/2-query/json_tag.py @@ -199,7 +199,7 @@ class TDTestCase: tdSql.checkData(0, 0, "true") # test select json tag->'key', value is null tdSql.query("select jtag->'tag1' from jsons1_4") - tdSql.checkData(0, 0, "null") + tdSql.checkData(0, 0, None) # test select json tag->'key', value is double tdSql.query("select jtag->'tag1' from jsons1_5") tdSql.checkData(0, 0, "1.232000000") From fc5cd5810f59caba6399f5db714cf92f29ef9406 Mon Sep 17 00:00:00 2001 From: wangmm0220 Date: Wed, 29 Jun 2022 10:36:48 +0800 Subject: [PATCH 24/36] fix:disable ttl temporary --- tests/pytest/a.sh | 13 +++++++++++++ tests/system-test/fulltest.sh | 2 +- 2 files changed, 14 insertions(+), 1 deletion(-) create mode 100755 tests/pytest/a.sh diff --git a/tests/pytest/a.sh b/tests/pytest/a.sh new file mode 100755 index 0000000000..cf6157980d --- /dev/null +++ b/tests/pytest/a.sh @@ -0,0 +1,13 @@ +#!/bin/bash +for i in {1..100} +do + echo $i + python3 ./test.py -f query/nestedQuery/nestedQuery_datacheck.py >>log 2>&1 + if [ $? -eq 0 ] + then + echo success + else + echo failed + break + fi +done diff --git a/tests/system-test/fulltest.sh b/tests/system-test/fulltest.sh index b0b8aa7108..d0fee5c49c 100755 --- a/tests/system-test/fulltest.sh +++ b/tests/system-test/fulltest.sh @@ -23,7 +23,7 @@ python3 ./test.py -f 1-insert/alter_stable.py python3 ./test.py -f 1-insert/alter_table.py python3 ./test.py -f 1-insert/insertWithMoreVgroup.py python3 ./test.py -f 1-insert/table_comment.py -python3 ./test.py -f 1-insert/table_param_ttl.py +#python3 ./test.py -f 1-insert/table_param_ttl.py python3 ./test.py -f 2-query/between.py python3 ./test.py -f 2-query/distinct.py python3 ./test.py -f 2-query/varchar.py From ce21baaa86cf8e72acfaf3e016c366a619f77bb1 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 29 Jun 2022 10:44:18 +0800 Subject: [PATCH 25/36] feat: enh redirect --- source/libs/transport/src/transCli.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 9e52fc21a3..6390855e43 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -1026,7 +1026,7 @@ bool cliTryToExtractEpSet(STransMsg* pResp, SEpSet* dst) { int32_t bufLen = pResp->contLen - tlen; char* buf = rpcMallocCont(bufLen); - memcpy(buf, pResp->pCont + tlen, bufLen); + memcpy(buf, (char*)pResp->pCont + tlen, bufLen); pResp->pCont = buf; pResp->contLen = bufLen; From 5691b0e82f02bdc905b8814b7f89c782cda0dbb5 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 29 Jun 2022 11:04:04 +0800 Subject: [PATCH 26/36] feat:modify code --- include/libs/transport/trpc.h | 2 +- source/libs/transport/inc/transComm.h | 7 ++----- source/libs/transport/src/transSvr.c | 2 -- 3 files changed, 3 insertions(+), 8 deletions(-) diff --git a/include/libs/transport/trpc.h b/include/libs/transport/trpc.h index 27153b5efd..8471aa8286 100644 --- a/include/libs/transport/trpc.h +++ b/include/libs/transport/trpc.h @@ -123,7 +123,7 @@ void * rpcReallocCont(void *ptr, int32_t contLen); void rpcSendRequest(void *thandle, const SEpSet *pEpSet, SRpcMsg *pMsg, int64_t *rid); void rpcSendResponse(const SRpcMsg *pMsg); void rpcRegisterBrokenLinkArg(SRpcMsg *msg); -void rpcReleaseHandle(void *handle, int8_t type); // just release client conn to rpc instance, no close sock +void rpcReleaseHandle(void *handle, int8_t type); // just release conn to rpc instance, no close sock // These functions will not be called in the child process void rpcSendRedirectRsp(void *pConn, const SEpSet *pEpSet); diff --git a/source/libs/transport/inc/transComm.h b/source/libs/transport/inc/transComm.h index 1283946240..59f79db6fb 100644 --- a/source/libs/transport/inc/transComm.h +++ b/source/libs/transport/inc/transComm.h @@ -378,13 +378,10 @@ typedef struct SDelayQueue { uv_loop_t* loop; } SDelayQueue; -int transDQCreate(uv_loop_t* loop, SDelayQueue** queue); - +int transDQCreate(uv_loop_t* loop, SDelayQueue** queue); void transDQDestroy(SDelayQueue* queue); +int transDQSched(SDelayQueue* queue, void (*func)(void* arg), void* arg, uint64_t timeoutMs); -int transDQSched(SDelayQueue* queue, void (*func)(void* arg), void* arg, uint64_t timeoutMs); - -// void transPrintEpSet(SEpSet* pEpSet); bool transEpSetIsEqual(SEpSet* a, SEpSet* b); /* * init global func diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index ed1abd10f7..c190950a17 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -1126,6 +1126,4 @@ _return2: rpcFreeCont(msg->pCont); } -int transGetConnInfo(void* thandle, STransHandleInfo* pConnInfo) { return -1; } - #endif From 6dc7eaa107a5de21aea2531c58ffa4709cf3d2d0 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 29 Jun 2022 11:45:18 +0800 Subject: [PATCH 27/36] modify case --- source/libs/transport/inc/transComm.h | 1 - source/libs/transport/src/trans.c | 2 +- source/libs/transport/src/transSvr.c | 2 -- 3 files changed, 1 insertion(+), 4 deletions(-) diff --git a/source/libs/transport/inc/transComm.h b/source/libs/transport/inc/transComm.h index 4b5f11a497..d656d87b12 100644 --- a/source/libs/transport/inc/transComm.h +++ b/source/libs/transport/inc/transComm.h @@ -295,7 +295,6 @@ void transSendRequest(void* shandle, const SEpSet* pEpSet, STransMsg* pMsg, STra void transSendRecv(void* shandle, const SEpSet* pEpSet, STransMsg* pMsg, STransMsg* pRsp); void transSendResponse(const STransMsg* msg); void transRegisterMsg(const STransMsg* msg); -int transGetConnInfo(void* thandle, STransHandleInfo* pInfo); void transSetDefaultAddr(void* shandle, const char* ip, const char* fqdn); void* transInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle); diff --git a/source/libs/transport/src/trans.c b/source/libs/transport/src/trans.c index acf9b6b4a7..6082b7f608 100644 --- a/source/libs/transport/src/trans.c +++ b/source/libs/transport/src/trans.c @@ -141,7 +141,7 @@ void rpcSendRecv(void* shandle, SEpSet* pEpSet, SRpcMsg* pMsg, SRpcMsg* pRsp) { } void rpcSendResponse(const SRpcMsg* pMsg) { transSendResponse(pMsg); } -int32_t rpcGetConnInfo(void* thandle, SRpcConnInfo* pInfo) { return transGetConnInfo((void*)thandle, pInfo); } +int32_t rpcGetConnInfo(void* thandle, SRpcConnInfo* pInfo) { return 0; } void rpcRefHandle(void* handle, int8_t type) { assert(type == TAOS_CONN_SERVER || type == TAOS_CONN_CLIENT); diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index c53cab13d5..df0c3bed45 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -1114,6 +1114,4 @@ _return2: rpcFreeCont(msg->pCont); } -int transGetConnInfo(void* thandle, STransHandleInfo* pConnInfo) { return -1; } - #endif From 41f24314d0b789ef28ff30208e2d582ffeac5881 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 29 Jun 2022 15:44:30 +0800 Subject: [PATCH 28/36] refactor(sync): add SYNC_TERM_INVALID --- source/libs/sync/inc/syncInt.h | 1 - source/libs/sync/src/syncAppendEntries.c | 8 +- source/libs/sync/src/syncMain.c | 162 +++-------------------- source/libs/sync/src/syncRaftLog.c | 43 ++++-- source/libs/sync/src/syncRequestVote.c | 4 + 5 files changed, 57 insertions(+), 161 deletions(-) diff --git a/source/libs/sync/inc/syncInt.h b/source/libs/sync/inc/syncInt.h index d351dc50f4..d58ae83dd3 100644 --- a/source/libs/sync/inc/syncInt.h +++ b/source/libs/sync/inc/syncInt.h @@ -221,7 +221,6 @@ void syncNodeVoteForSelf(SSyncNode* pSyncNode); // snapshot -------------- bool syncNodeHasSnapshot(SSyncNode* pSyncNode); -bool syncNodeIsIndexInSnapshot(SSyncNode* pSyncNode, SyncIndex index); SyncIndex syncNodeGetLastIndex(SSyncNode* pSyncNode); SyncTerm syncNodeGetLastTerm(SSyncNode* pSyncNode); diff --git a/source/libs/sync/src/syncAppendEntries.c b/source/libs/sync/src/syncAppendEntries.c index ba3dd66550..a10b81d165 100644 --- a/source/libs/sync/src/syncAppendEntries.c +++ b/source/libs/sync/src/syncAppendEntries.c @@ -860,7 +860,9 @@ int32_t syncNodeOnAppendEntriesSnapshotCb(SSyncNode* ths, SyncAppendEntries* pMs } code = ths->pLogStore->syncLogAppendEntry(ths->pLogStore, pAppendEntry); - ASSERT(code == 0); + if (code != 0) { + return -1; + } // pre commit code = syncNodePreCommit(ths, pAppendEntry); @@ -971,7 +973,9 @@ int32_t syncNodeOnAppendEntriesSnapshotCb(SSyncNode* ths, SyncAppendEntries* pMs ASSERT(pAppendEntry != NULL); code = ths->pLogStore->syncLogAppendEntry(ths->pLogStore, pAppendEntry); - ASSERT(code == 0); + if (code != 0) { + return -1; + } // pre commit code = syncNodePreCommit(ths, pAppendEntry); diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index f6768bf494..cc2b5a7706 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -1935,19 +1935,6 @@ bool syncNodeHasSnapshot(SSyncNode* pSyncNode) { return ret; } -#if 0 -bool syncNodeIsIndexInSnapshot(SSyncNode* pSyncNode, SyncIndex index) { - ASSERT(syncNodeHasSnapshot(pSyncNode)); - ASSERT(pSyncNode->pFsm->FpGetSnapshotInfo != NULL); - ASSERT(index >= SYNC_INDEX_BEGIN); - - SSnapshot snapshot; - pSyncNode->pFsm->FpGetSnapshotInfo(pSyncNode->pFsm, &snapshot); - bool b = (index <= snapshot.lastApplyIndex); - return b; -} -#endif - SyncIndex syncNodeGetLastIndex(SSyncNode* pSyncNode) { SSnapshot snapshot = {.data = NULL, .lastApplyIndex = -1, .lastApplyTerm = 0}; if (pSyncNode->pFsm->FpGetSnapshotInfo != NULL) { @@ -2004,21 +1991,6 @@ SyncIndex syncNodeGetPreIndex(SSyncNode* pSyncNode, SyncIndex index) { return preIndex; } -/* -SyncIndex syncNodeGetPreIndex(SSyncNode* pSyncNode, SyncIndex index) { - ASSERT(index >= SYNC_INDEX_BEGIN); - - SyncIndex syncStartIndex = syncNodeSyncStartIndex(pSyncNode); - if (index > syncStartIndex) { - syncNodeLog3("syncNodeGetPreIndex", pSyncNode); - ASSERT(0); - } - - SyncIndex preIndex = index - 1; - return preIndex; -} -*/ - SyncTerm syncNodeGetPreTerm(SSyncNode* pSyncNode, SyncIndex index) { if (index < SYNC_INDEX_BEGIN) { return SYNC_TERM_INVALID; @@ -2056,112 +2028,6 @@ SyncTerm syncNodeGetPreTerm(SSyncNode* pSyncNode, SyncIndex index) { return SYNC_TERM_INVALID; } -#if 0 -SyncTerm syncNodeGetPreTerm(SSyncNode* pSyncNode, SyncIndex index) { - ASSERT(index >= SYNC_INDEX_BEGIN); - - SyncIndex syncStartIndex = syncNodeSyncStartIndex(pSyncNode); - if (index > syncStartIndex) { - syncNodeLog3("syncNodeGetPreTerm", pSyncNode); - ASSERT(0); - } - - if (index == SYNC_INDEX_BEGIN) { - return 0; - } - - SyncTerm preTerm = 0; - SyncIndex preIndex = index - 1; - SSyncRaftEntry* pPreEntry = NULL; - int32_t code = pSyncNode->pLogStore->syncLogGetEntry(pSyncNode->pLogStore, preIndex, &pPreEntry); - if (code == 0) { - ASSERT(pPreEntry != NULL); - preTerm = pPreEntry->term; - taosMemoryFree(pPreEntry); - return preTerm; - } else { - if (terrno == TSDB_CODE_WAL_LOG_NOT_EXIST) { - SSnapshot snapshot = {.data = NULL, .lastApplyIndex = -1, .lastApplyTerm = 0, .lastConfigIndex = -1}; - if (pSyncNode->pFsm->FpGetSnapshotInfo != NULL) { - pSyncNode->pFsm->FpGetSnapshotInfo(pSyncNode->pFsm, &snapshot); - if (snapshot.lastApplyIndex == preIndex) { - return snapshot.lastApplyTerm; - } - } - } - } - - ASSERT(0); - return -1; -} -#endif - -#if 0 -SyncTerm syncNodeGetPreTerm(SSyncNode* pSyncNode, SyncIndex index) { - ASSERT(index >= SYNC_INDEX_BEGIN); - - SyncIndex syncStartIndex = syncNodeSyncStartIndex(pSyncNode); - if (index > syncStartIndex) { - syncNodeLog3("syncNodeGetPreTerm", pSyncNode); - ASSERT(0); - } - - if (index == SYNC_INDEX_BEGIN) { - return 0; - } - - SyncTerm preTerm = 0; - if (syncNodeHasSnapshot(pSyncNode)) { - // has snapshot - SSnapshot snapshot = {.data = NULL, .lastApplyIndex = -1, .lastApplyTerm = 0, .lastConfigIndex = -1}; - if (pSyncNode->pFsm->FpGetSnapshotInfo != NULL) { - pSyncNode->pFsm->FpGetSnapshotInfo(pSyncNode->pFsm, &snapshot); - } - - if (index > snapshot.lastApplyIndex + 1) { - // should be log preTerm - SSyncRaftEntry* pPreEntry = NULL; - int32_t code = pSyncNode->pLogStore->syncLogGetEntry(pSyncNode->pLogStore, index - 1, &pPreEntry); - ASSERT(code == 0); - ASSERT(pPreEntry != NULL); - - preTerm = pPreEntry->term; - taosMemoryFree(pPreEntry); - - } else if (index == snapshot.lastApplyIndex + 1) { - preTerm = snapshot.lastApplyTerm; - - } else { - // maybe snapshot change - sError("sync get pre term, bad scene. index:%ld", index); - logStoreLog2("sync get pre term, bad scene", pSyncNode->pLogStore); - - SSyncRaftEntry* pPreEntry = NULL; - int32_t code = pSyncNode->pLogStore->syncLogGetEntry(pSyncNode->pLogStore, index - 1, &pPreEntry); - ASSERT(code == 0); - ASSERT(pPreEntry != NULL); - - preTerm = pPreEntry->term; - taosMemoryFree(pPreEntry); - } - - } else { - // no snapshot - ASSERT(index > SYNC_INDEX_BEGIN); - - SSyncRaftEntry* pPreEntry = NULL; - int32_t code = pSyncNode->pLogStore->syncLogGetEntry(pSyncNode->pLogStore, index - 1, &pPreEntry); - ASSERT(code == 0); - ASSERT(pPreEntry != NULL); - - preTerm = pPreEntry->term; - taosMemoryFree(pPreEntry); - } - - return preTerm; -} -#endif - // get pre index and term of "index" int32_t syncNodeGetPreIndexTerm(SSyncNode* pSyncNode, SyncIndex index, SyncIndex* pPreIndex, SyncTerm* pPreTerm) { *pPreIndex = syncNodeGetPreIndex(pSyncNode, index); @@ -2351,8 +2217,8 @@ static int32_t syncNodeAppendNoop(SSyncNode* ths) { ASSERT(pEntry != NULL); if (ths->state == TAOS_SYNC_STATE_LEADER) { - // ths->pLogStore->appendEntry(ths->pLogStore, pEntry); - ths->pLogStore->syncLogAppendEntry(ths->pLogStore, pEntry); + int32_t code = ths->pLogStore->syncLogAppendEntry(ths->pLogStore, pEntry); + ASSERT(code == 0); syncNodeReplicate(ths); } @@ -2406,6 +2272,7 @@ int32_t syncNodeOnPingReplyCb(SSyncNode* ths, SyncPingReply* pMsg) { // int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg, SyncIndex* pRetIndex) { int32_t ret = 0; + int32_t code = 0; syncClientRequestLog2("==syncNodeOnClientRequestCb==", pMsg); SyncIndex index = ths->pLogStore->syncLogWriteIndex(ths->pLogStore); @@ -2414,18 +2281,24 @@ int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg, SyncI ASSERT(pEntry != NULL); if (ths->state == TAOS_SYNC_STATE_LEADER) { - // ths->pLogStore->appendEntry(ths->pLogStore, pEntry); - ths->pLogStore->syncLogAppendEntry(ths->pLogStore, pEntry); + // append entry + code = ths->pLogStore->syncLogAppendEntry(ths->pLogStore, pEntry); + if (code != 0) { + // del resp mgr, call FpCommitCb + ASSERT(0); + return -1; + } - // start replicate right now! - syncNodeReplicate(ths); + // if mulit replica, start replicate right now + if (ths->replicaNum > 1) { + syncNodeReplicate(ths); + } // pre commit SRpcMsg rpcMsg; syncEntry2OriginalRpc(pEntry, &rpcMsg); if (ths->pFsm != NULL) { - // if (ths->pFsm->FpPreCommitCb != NULL && pEntry->originalRpcType != TDMT_SYNC_NOOP) { if (ths->pFsm->FpPreCommitCb != NULL && syncUtilUserPreCommit(pEntry->originalRpcType)) { SFsmCbMeta cbMeta = {0}; cbMeta.index = pEntry->index; @@ -2439,8 +2312,10 @@ int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg, SyncI } rpcFreeCont(rpcMsg.pCont); - // only myself, maybe commit - syncMaybeAdvanceCommitIndex(ths); + // if only myself, maybe commit right now + if (ths->replicaNum == 1) { + syncMaybeAdvanceCommitIndex(ths); + } } else { // pre commit @@ -2448,7 +2323,6 @@ int32_t syncNodeOnClientRequestCb(SSyncNode* ths, SyncClientRequest* pMsg, SyncI syncEntry2OriginalRpc(pEntry, &rpcMsg); if (ths->pFsm != NULL) { - // if (ths->pFsm->FpPreCommitCb != NULL && pEntry->originalRpcType != TDMT_SYNC_NOOP) { if (ths->pFsm->FpPreCommitCb != NULL && syncUtilUserPreCommit(pEntry->originalRpcType)) { SFsmCbMeta cbMeta = {0}; cbMeta.index = pEntry->index; diff --git a/source/libs/sync/src/syncRaftLog.c b/source/libs/sync/src/syncRaftLog.c index a574c8cc1e..a026892629 100644 --- a/source/libs/sync/src/syncRaftLog.c +++ b/source/libs/sync/src/syncRaftLog.c @@ -168,6 +168,9 @@ static SyncIndex raftLogWriteIndex(struct SSyncLogStore* pLogStore) { return lastVer + 1; } +// if success, return last term +// if not log, return 0 +// if error, return SYNC_TERM_INVALID static SyncTerm raftLogLastTerm(struct SSyncLogStore* pLogStore) { SSyncLogStoreData* pData = pLogStore->data; SWal* pWal = pData->pWal; @@ -176,15 +179,17 @@ static SyncTerm raftLogLastTerm(struct SSyncLogStore* pLogStore) { } else { SSyncRaftEntry* pLastEntry; int32_t code = raftLogGetLastEntry(pLogStore, &pLastEntry); - ASSERT(code == 0); - ASSERT(pLastEntry != NULL); - - SyncTerm lastTerm = pLastEntry->term; - taosMemoryFree(pLastEntry); - return lastTerm; + if (code == 0 && pLastEntry != NULL) { + SyncTerm lastTerm = pLastEntry->term; + taosMemoryFree(pLastEntry); + return lastTerm; + } else { + return SYNC_TERM_INVALID; + } } - return 0; + // can not be here! + return SYNC_TERM_INVALID; } static int32_t raftLogAppendEntry(struct SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry) { @@ -218,16 +223,21 @@ static int32_t raftLogAppendEntry(struct SSyncLogStore* pLogStore, SSyncRaftEntr ASSERT(0); } - walFsync(pWal, true); + // walFsync(pWal, true); - char eventLog[128]; - snprintf(eventLog, sizeof(eventLog), "write index:%ld, type:%s,%d, type2:%s,%d", pEntry->index, - TMSG_INFO(pEntry->msgType), pEntry->msgType, TMSG_INFO(pEntry->originalRpcType), pEntry->originalRpcType); - syncNodeEventLog(pData->pSyncNode, eventLog); + do { + char eventLog[128]; + snprintf(eventLog, sizeof(eventLog), "write index:%ld, type:%s,%d, type2:%s,%d", pEntry->index, + TMSG_INFO(pEntry->msgType), pEntry->msgType, TMSG_INFO(pEntry->originalRpcType), pEntry->originalRpcType); + syncNodeEventLog(pData->pSyncNode, eventLog); + } while (0); return code; } +// entry found, return 0 +// entry not found, return -1, terrno = TSDB_CODE_WAL_LOG_NOT_EXIST +// other error, return -1 static int32_t raftLogGetEntry(struct SSyncLogStore* pLogStore, SyncIndex index, SSyncRaftEntry** ppEntry) { SSyncLogStoreData* pData = pLogStore->data; SWal* pWal = pData->pWal; @@ -238,6 +248,7 @@ static int32_t raftLogGetEntry(struct SSyncLogStore* pLogStore, SyncIndex index, // SWalReadHandle* pWalHandle = walOpenReadHandle(pWal); SWalReadHandle* pWalHandle = pData->pWalHandle; if (pWalHandle == NULL) { + terrno = TSDB_CODE_SYN_INTERNAL_ERROR; return -1; } @@ -309,6 +320,9 @@ static int32_t raftLogTruncate(struct SSyncLogStore* pLogStore, SyncIndex fromIn return code; } +// entry found, return 0 +// entry not found, return -1, terrno = TSDB_CODE_WAL_LOG_NOT_EXIST +// other error, return -1 static int32_t raftLogGetLastEntry(SSyncLogStore* pLogStore, SSyncRaftEntry** ppLastEntry) { SSyncLogStoreData* pData = pLogStore->data; SWal* pWal = pData->pWal; @@ -320,7 +334,8 @@ static int32_t raftLogGetLastEntry(SSyncLogStore* pLogStore, SSyncRaftEntry** pp return -1; } else { SyncIndex lastIndex = raftLogLastIndex(pLogStore); - int32_t code = raftLogGetEntry(pLogStore, lastIndex, ppLastEntry); + ASSERT(lastIndex >= SYNC_INDEX_BEGIN); + int32_t code = raftLogGetEntry(pLogStore, lastIndex, ppLastEntry); return code; } @@ -356,7 +371,7 @@ int32_t logStoreAppendEntry(SSyncLogStore* pLogStore, SSyncRaftEntry* pEntry) { ASSERT(0); } - walFsync(pWal, true); + // walFsync(pWal, true); char eventLog[128]; snprintf(eventLog, sizeof(eventLog), "old write index:%ld, type:%s,%d, type2:%s,%d", pEntry->index, diff --git a/source/libs/sync/src/syncRequestVote.c b/source/libs/sync/src/syncRequestVote.c index 95dec6cb83..d272e0175f 100644 --- a/source/libs/sync/src/syncRequestVote.c +++ b/source/libs/sync/src/syncRequestVote.c @@ -156,6 +156,10 @@ static bool syncNodeOnRequestVoteLogOK(SSyncNode* pSyncNode, SyncRequestVote* pM SyncTerm myLastTerm = syncNodeGetLastTerm(pSyncNode); SyncIndex myLastIndex = syncNodeGetLastIndex(pSyncNode); + if (myLastTerm == SYNC_TERM_INVALID) { + return false; + } + if (pMsg->lastLogTerm > myLastTerm) { return true; } From f8cf9761f8ea6e2d0e1d5d4c2fcd4b1a0e5fe021 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Wed, 29 Jun 2022 16:18:23 +0800 Subject: [PATCH 29/36] fix: avoid compile error --- source/libs/transport/inc/transComm.h | 1 - source/libs/transport/src/trans.c | 2 +- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/source/libs/transport/inc/transComm.h b/source/libs/transport/inc/transComm.h index 59f79db6fb..8183b7fd9f 100644 --- a/source/libs/transport/inc/transComm.h +++ b/source/libs/transport/inc/transComm.h @@ -295,7 +295,6 @@ void transSendRequest(void* shandle, const SEpSet* pEpSet, STransMsg* pMsg, STra void transSendRecv(void* shandle, const SEpSet* pEpSet, STransMsg* pMsg, STransMsg* pRsp); void transSendResponse(const STransMsg* msg); void transRegisterMsg(const STransMsg* msg); -int transGetConnInfo(void* thandle, STransHandleInfo* pInfo); void transSetDefaultAddr(void* shandle, const char* ip, const char* fqdn); void* transInitServer(uint32_t ip, uint32_t port, char* label, int numOfThreads, void* fp, void* shandle); diff --git a/source/libs/transport/src/trans.c b/source/libs/transport/src/trans.c index cc2e95cfb3..bd8195462e 100644 --- a/source/libs/transport/src/trans.c +++ b/source/libs/transport/src/trans.c @@ -141,7 +141,7 @@ void rpcSendRecv(void* shandle, SEpSet* pEpSet, SRpcMsg* pMsg, SRpcMsg* pRsp) { } void rpcSendResponse(const SRpcMsg* pMsg) { transSendResponse(pMsg); } -int32_t rpcGetConnInfo(void* thandle, SRpcConnInfo* pInfo) { return transGetConnInfo((void*)thandle, pInfo); } +int32_t rpcGetConnInfo(void* thandle, SRpcConnInfo* pInfo) { return -1; } void rpcRefHandle(void* handle, int8_t type) { assert(type == TAOS_CONN_SERVER || type == TAOS_CONN_CLIENT); From 1b8c5d9269d6148e404c93f74b858a88d3186096 Mon Sep 17 00:00:00 2001 From: Haojun Liao Date: Wed, 29 Jun 2022 16:19:19 +0800 Subject: [PATCH 30/36] enh(query): add api to extract scan status --- include/libs/executor/executor.h | 16 +++++---- source/libs/executor/inc/executorimpl.h | 44 +++++++++++++----------- source/libs/executor/src/executorMain.c | 17 ++++----- source/libs/executor/src/executorimpl.c | 24 +++++++++++-- source/libs/executor/src/groupoperator.c | 4 +-- source/libs/executor/src/scanoperator.c | 8 ++++- 6 files changed, 69 insertions(+), 44 deletions(-) diff --git a/include/libs/executor/executor.h b/include/libs/executor/executor.h index 00acc4741d..23fb9d2ee5 100644 --- a/include/libs/executor/executor.h +++ b/include/libs/executor/executor.h @@ -36,7 +36,6 @@ typedef struct SReadHandle { void* vnode; void* mnd; SMsgCb* pMsgCb; -// int8_t initTsdbReader; } SReadHandle; enum { @@ -140,12 +139,6 @@ int32_t qKillTask(qTaskInfo_t tinfo); */ int32_t qAsyncKillTask(qTaskInfo_t tinfo); -/** - * return whether query is completed or not - * @param tinfo - * @return - */ -int32_t qIsTaskCompleted(qTaskInfo_t tinfo); /** * destroy query info structure @@ -176,6 +169,15 @@ int32_t qSerializeTaskStatus(qTaskInfo_t tinfo, char** pOutput, int32_t* len); int32_t qDeserializeTaskStatus(qTaskInfo_t tinfo, const char* pInput, int32_t len); +/** + * return the scan info, in the form of tuple of two items, including table uid and current timestamp + * @param tinfo + * @param uid + * @param ts + * @return + */ +int32_t qGetStreamScanStatus(qTaskInfo_t tinfo, uint64_t* uid, int64_t* ts); + #ifdef __cplusplus } #endif diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index 29dd2b1656..dfc7342051 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -253,18 +253,15 @@ typedef struct STableScanInfo { SReadHandle readHandle; SFileBlockLoadRecorder readRecorder; - int64_t numOfRows; SScanInfo scanInfo; int32_t scanTimes; SNode* pFilterNode; // filter info, which is push down by optimizer - SqlFunctionCtx* pCtx; // which belongs to the direct upstream operator operator query context - SResultRowInfo* pResultRowInfo; - int32_t* rowEntryInfoOffset; - SExprInfo* pExpr; + SqlFunctionCtx* pCtx; // which belongs to the direct upstream operator operator query context,todo: remove this by using SExprSup + int32_t* rowEntryInfoOffset; // todo: remove this by using SExprSup + SExprInfo* pExpr;// todo: remove this by using SExprSup + SSDataBlock* pResBlock; SArray* pColMatchInfo; - int32_t numOfOutput; - SExprSupp pseudoSup; SQueryTableDataCond cond; int32_t scanFlag; // table scan flag to denote if it is a repeat/reverse/main scan @@ -275,8 +272,13 @@ typedef struct STableScanInfo { int32_t curTWinIdx; int32_t currentGroupId; - uint64_t queryId; - uint64_t taskId; + uint64_t queryId; // todo remove it + uint64_t taskId; // todo remove it + + struct { + uint64_t uid; + int64_t t; + } scanStatus; } STableScanInfo; typedef struct STagScanInfo { @@ -321,31 +323,31 @@ typedef struct SessionWindowSupporter { } SessionWindowSupporter; typedef struct SStreamBlockScanInfo { + uint64_t tableUid; // queried super table uid + SExprInfo* pPseudoExpr; + int32_t numOfPseudoExpr; + int32_t primaryTsIndex; // primary time stamp slot id + SReadHandle readHandle; + SInterval interval; // if the upstream is an interval operator, the interval info is also kept here. + SArray* pColMatchInfo; // + SNode* pCondition; + SArray* pBlockLists; // multiple SSDatablock. SSDataBlock* pRes; // result SSDataBlock SSDataBlock* pUpdateRes; // update SSDataBlock int32_t updateResIndex; int32_t blockType; // current block type int32_t validBlockIndex; // Is current data has returned? - SColumnInfo* pCols; // the output column info uint64_t numOfExec; // execution times void* streamBlockReader;// stream block reader handle - SArray* pColMatchInfo; // - SNode* pCondition; + int32_t tsArrayIndex; SArray* tsArray; uint64_t groupId; SUpdateInfo* pUpdateInfo; - SExprInfo* pPseudoExpr; - int32_t numOfPseudoExpr; - - int32_t primaryTsIndex; // primary time stamp slot id - SReadHandle readHandle; - uint64_t tableUid; // queried super table uid EStreamScanMode scanMode; SOperatorInfo* pSnapshotReadOp; - SInterval interval; // if the upstream is an interval operator, the interval info is also kept here. SArray* childIds; SessionWindowSupporter sessionSup; bool assignBlockUid; // assign block uid to groupId, temporarily used for generating rollup SMA. @@ -683,7 +685,7 @@ int32_t appendDownstream(SOperatorInfo* p, SOperatorInfo** pDownstream, int32_t void initBasicInfo(SOptrBasicInfo* pInfo, SSDataBlock* pBlock); void cleanupBasicInfo(SOptrBasicInfo* pInfo); int32_t initExprSupp(SExprSupp* pSup, SExprInfo* pExprInfo, int32_t numOfExpr); -void cleanupExprSup(SExprSupp* pSup); +void cleanupExprSupp(SExprSupp* pSup); int32_t initAggInfo(SExprSupp *pSup, SAggSupporter* pAggSup, SExprInfo* pExprInfo, int32_t numOfCols, size_t keyBufSize, const char* pkey); void initResultSizeInfo(SOperatorInfo* pOperator, int32_t numOfRows); @@ -707,7 +709,7 @@ void destroyBasicOperatorInfo(void* param, int32_t numOfOutput); void appendOneRowToDataBlock(SSDataBlock* pBlock, STupleHandle* pTupleHandle); void setTbNameColData(void* pMeta, const SSDataBlock* pBlock, SColumnInfoData* pColInfoData, int32_t functionId); -void cleanupExecSupp(SExprSupp* pSupp); +int32_t doGetScanStatus(SOperatorInfo* pOperator, uint64_t* uid, int64_t* ts); SSDataBlock* loadNextDataBlock(void* param); diff --git a/source/libs/executor/src/executorMain.c b/source/libs/executor/src/executorMain.c index a9e1e03178..3a89f7136a 100644 --- a/source/libs/executor/src/executorMain.c +++ b/source/libs/executor/src/executorMain.c @@ -191,16 +191,6 @@ int32_t qAsyncKillTask(qTaskInfo_t qinfo) { return TSDB_CODE_SUCCESS; } -int32_t qIsTaskCompleted(qTaskInfo_t qinfo) { - SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qinfo; - - if (pTaskInfo == NULL) { - return TSDB_CODE_QRY_INVALID_QHANDLE; - } - - return isTaskKilled(pTaskInfo); -} - void qDestroyTask(qTaskInfo_t qTaskHandle) { SExecTaskInfo* pTaskInfo = (SExecTaskInfo*)qTaskHandle; qDebug("%s execTask completed, numOfRows:%" PRId64, GET_TASKID(pTaskInfo), pTaskInfo->pRoot->resultInfo.totalRows); @@ -236,3 +226,10 @@ int32_t qDeserializeTaskStatus(qTaskInfo_t tinfo, const char* pInput, int32_t le } +int32_t qGetStreamScanStatus(qTaskInfo_t tinfo, uint64_t* uid, int64_t* ts) { + SExecTaskInfo* pTaskInfo = (SExecTaskInfo*) tinfo; + + return TSDB_CODE_SUCCESS; +} + + diff --git a/source/libs/executor/src/executorimpl.c b/source/libs/executor/src/executorimpl.c index ff3baf64bd..b176d8e88f 100644 --- a/source/libs/executor/src/executorimpl.c +++ b/source/libs/executor/src/executorimpl.c @@ -1034,7 +1034,7 @@ static uint32_t doFilterByBlockTimeWindow(STableScanInfo* pTableScanInfo, SSData SqlFunctionCtx* pCtx = pTableScanInfo->pCtx; uint32_t status = BLK_DATA_NOT_LOAD; - int32_t numOfOutput = pTableScanInfo->numOfOutput; + int32_t numOfOutput = 0;//pTableScanInfo->numOfOutput; for (int32_t i = 0; i < numOfOutput; ++i) { int32_t functionId = pCtx[i].functionId; int32_t colId = pTableScanInfo->pExpr[i].base.pParam[0].pCol->colId; @@ -2822,6 +2822,24 @@ int32_t getTableScanInfo(SOperatorInfo* pOperator, int32_t* order, int32_t* scan } } +int32_t doGetScanStatus(SOperatorInfo* pOperator, uint64_t* uid, int64_t* ts) { + int32_t type = pOperator->operatorType; + if (type == QUERY_NODE_PHYSICAL_PLAN_STREAM_SCAN) { + SStreamBlockScanInfo* pScanInfo = pOperator->info; + STableScanInfo* pSnapShotScanInfo = pScanInfo->pSnapshotReadOp->info; + *uid = pSnapShotScanInfo->scanStatus.uid; + *ts = pSnapShotScanInfo->scanStatus.t; + } else { + if (pOperator->pDownstream[0] == NULL) { + return TSDB_CODE_INVALID_PARA; + } else { + doGetScanStatus(pOperator->pDownstream[0], uid, ts); + } + } + + return TSDB_CODE_SUCCESS; +} + // this is a blocking operator static int32_t doOpenAggregateOptr(SOperatorInfo* pOperator) { if (OPTR_IS_OPENED(pOperator)) { @@ -3544,7 +3562,7 @@ static void destroyProjectOperatorInfo(void* param, int32_t numOfOutput) { taosArrayDestroy(pInfo->pPseudoColInfo); } -void cleanupExecSupp(SExprSupp* pSupp) { +void cleanupExprSupp(SExprSupp* pSupp) { destroySqlFunctionCtx(pSupp->pCtx, pSupp->numOfExprs); destroyExprInfo(pSupp->pExprInfo, pSupp->numOfExprs); @@ -3557,7 +3575,7 @@ static void destroyIndefinitOperatorInfo(void* param, int32_t numOfOutput) { taosArrayDestroy(pInfo->pPseudoColInfo); cleanupAggSup(&pInfo->aggSup); - cleanupExecSupp(&pInfo->scalarSup); + cleanupExprSupp(&pInfo->scalarSup); } void destroyExchangeOperatorInfo(void* param, int32_t numOfOutput) { diff --git a/source/libs/executor/src/groupoperator.c b/source/libs/executor/src/groupoperator.c index 4e4aaba7f4..0a14993c21 100644 --- a/source/libs/executor/src/groupoperator.c +++ b/source/libs/executor/src/groupoperator.c @@ -37,7 +37,7 @@ static void destroyGroupOperatorInfo(void* param, int32_t numOfOutput) { taosMemoryFreeClear(pInfo->keyBuf); taosArrayDestroy(pInfo->pGroupCols); taosArrayDestroy(pInfo->pGroupColVals); - cleanupExecSupp(&pInfo->scalarSup); + cleanupExprSupp(&pInfo->scalarSup); } static int32_t initGroupOptrInfo(SArray** pGroupColVals, int32_t* keyLen, char** keyBuf, const SArray* pGroupColList) { @@ -701,7 +701,7 @@ static void destroyPartitionOperatorInfo(void* param, int32_t numOfOutput) { taosHashCleanup(pInfo->pGroupSet); taosMemoryFree(pInfo->columnOffset); - cleanupExecSupp(&pInfo->scalarSup); + cleanupExprSupp(&pInfo->scalarSup); } SOperatorInfo* createPartitionOperatorInfo(SOperatorInfo* downstream, SPartitionPhysiNode* pPartNode, SExecTaskInfo* pTaskInfo) { diff --git a/source/libs/executor/src/scanoperator.c b/source/libs/executor/src/scanoperator.c index d89e6f8874..6c38e4d70c 100644 --- a/source/libs/executor/src/scanoperator.c +++ b/source/libs/executor/src/scanoperator.c @@ -13,6 +13,7 @@ * along with this program. If not, see . */ +#include #include #include "filter.h" #include "function.h" @@ -413,6 +414,11 @@ static SSDataBlock* doTableScanImpl(SOperatorInfo* pOperator) { pTableScanInfo->readRecorder.elapsedTime += (taosGetTimestampUs() - st) / 1000.0; pOperator->cost.totalCost = pTableScanInfo->readRecorder.elapsedTime; + + // todo refactor + pTableScanInfo->scanStatus.uid = pBlock->info.uid; + pTableScanInfo->scanStatus.t = pBlock->info.window.ekey; + return pBlock; } return NULL; @@ -459,7 +465,7 @@ static SSDataBlock* doTableScanGroup(SOperatorInfo* pOperator) { int32_t total = pTableScanInfo->scanInfo.numOfAsc + pTableScanInfo->scanInfo.numOfDesc; if (pTableScanInfo->scanTimes < total) { if (pTableScanInfo->cond.order == TSDB_ORDER_ASC) { - prepareForDescendingScan(pTableScanInfo, pTableScanInfo->pCtx, pTableScanInfo->numOfOutput); + prepareForDescendingScan(pTableScanInfo, pTableScanInfo->pCtx, 0); tsdbResetReadHandle(pTableScanInfo->dataReader, &pTableScanInfo->cond, 0); pTableScanInfo->curTWinIdx = 0; } From 16c6427e7b3b9c3fb94273bfe9331b93cb914b3e Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Wed, 29 Jun 2022 16:29:12 +0800 Subject: [PATCH 31/36] fix: fix scheduler callback issue --- source/libs/scheduler/src/scheduler.c | 4 ---- 1 file changed, 4 deletions(-) diff --git a/source/libs/scheduler/src/scheduler.c b/source/libs/scheduler/src/scheduler.c index 15a687531c..8b125811cf 100644 --- a/source/libs/scheduler/src/scheduler.c +++ b/source/libs/scheduler/src/scheduler.c @@ -113,10 +113,6 @@ _return: schReleaseJob(pJob->refId); } - if (code != TSDB_CODE_SUCCESS) { - pReq->fp(NULL, pReq->cbParam, code); - } - return code; } From 3440822ebf6b6196a060949f3f4cf9ea53e238ce Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 29 Jun 2022 16:40:36 +0800 Subject: [PATCH 32/36] refactor(sync): add some comments --- source/libs/sync/src/syncMain.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/source/libs/sync/src/syncMain.c b/source/libs/sync/src/syncMain.c index cc2b5a7706..d32153e5ed 100644 --- a/source/libs/sync/src/syncMain.c +++ b/source/libs/sync/src/syncMain.c @@ -1923,6 +1923,8 @@ void syncNodeVoteForSelf(SSyncNode* pSyncNode) { } // snapshot -------------- + +// return if has a snapshot bool syncNodeHasSnapshot(SSyncNode* pSyncNode) { bool ret = false; SSnapshot snapshot = {.data = NULL, .lastApplyIndex = -1, .lastApplyTerm = 0, .lastConfigIndex = -1}; @@ -1935,8 +1937,10 @@ bool syncNodeHasSnapshot(SSyncNode* pSyncNode) { return ret; } +// return max(logLastIndex, snapshotLastIndex) +// if no snapshot and log, return -1 SyncIndex syncNodeGetLastIndex(SSyncNode* pSyncNode) { - SSnapshot snapshot = {.data = NULL, .lastApplyIndex = -1, .lastApplyTerm = 0}; + SSnapshot snapshot = {.data = NULL, .lastApplyIndex = -1, .lastApplyTerm = 0, .lastConfigIndex = -1}; if (pSyncNode->pFsm->FpGetSnapshotInfo != NULL) { pSyncNode->pFsm->FpGetSnapshotInfo(pSyncNode->pFsm, &snapshot); } @@ -1946,6 +1950,8 @@ SyncIndex syncNodeGetLastIndex(SSyncNode* pSyncNode) { return lastIndex; } +// return the last term of snapshot and log +// if error, return SYNC_TERM_INVALID (by syncLogLastTerm) SyncTerm syncNodeGetLastTerm(SSyncNode* pSyncNode) { SyncTerm lastTerm = 0; if (syncNodeHasSnapshot(pSyncNode)) { @@ -1977,11 +1983,14 @@ int32_t syncNodeGetLastIndexTerm(SSyncNode* pSyncNode, SyncIndex* pLastIndex, Sy return 0; } +// return append-entries first try index SyncIndex syncNodeSyncStartIndex(SSyncNode* pSyncNode) { SyncIndex syncStartIndex = syncNodeGetLastIndex(pSyncNode) + 1; return syncStartIndex; } +// if index > 0, return index - 1 +// else, return -1 SyncIndex syncNodeGetPreIndex(SSyncNode* pSyncNode, SyncIndex index) { SyncIndex preIndex = index - 1; if (preIndex < SYNC_INDEX_INVALID) { @@ -1991,6 +2000,10 @@ SyncIndex syncNodeGetPreIndex(SSyncNode* pSyncNode, SyncIndex index) { return preIndex; } +// if index < 0, return SYNC_TERM_INVALID +// if index == 0, return 0 +// if index > 0, return preTerm +// if error, return SYNC_TERM_INVALID SyncTerm syncNodeGetPreTerm(SSyncNode* pSyncNode, SyncIndex index) { if (index < SYNC_INDEX_BEGIN) { return SYNC_TERM_INVALID; From 5e0cbdf0b7b4632c6f3d284bd9d4207c4ddfeb13 Mon Sep 17 00:00:00 2001 From: 54liuyao <54liuyao@163.com> Date: Wed, 29 Jun 2022 15:13:56 +0800 Subject: [PATCH 33/36] feat(stream): ignore close window --- source/common/src/tdatablock.c | 4 +- source/libs/executor/inc/executorimpl.h | 4 + source/libs/executor/src/timewindowoperator.c | 120 ++++++++++++------ 3 files changed, 89 insertions(+), 39 deletions(-) diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index 1ec298ee15..22999b38ee 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -1662,8 +1662,8 @@ char* dumpBlockData(SSDataBlock* pDataBlock, const char* flag, char** pDataBuf) int32_t colNum = taosArrayGetSize(pDataBlock->pDataBlock); int32_t rows = pDataBlock->info.rows; int32_t len = 0; - len += snprintf(dumpBuf + len, size - len, "\n%s |block type %d |child id %d|\n", flag, - (int32_t)pDataBlock->info.type, pDataBlock->info.childId); + len += snprintf(dumpBuf + len, size - len, "\n%s |block type %d |child id %d|group id %lu|\n", flag, + (int32_t)pDataBlock->info.type, pDataBlock->info.childId, pDataBlock->info.groupId); for (int32_t j = 0; j < rows; j++) { len += snprintf(dumpBuf + len, size - len, "%s |", flag); for (int32_t k = 0; k < colNum; k++) { diff --git a/source/libs/executor/inc/executorimpl.h b/source/libs/executor/inc/executorimpl.h index 1cc3f9b874..aeef0fae2a 100644 --- a/source/libs/executor/inc/executorimpl.h +++ b/source/libs/executor/inc/executorimpl.h @@ -416,6 +416,7 @@ typedef struct SIntervalAggOperatorInfo { STimeWindowAggSupp twAggSup; bool invertible; SArray* pPrevValues; // SArray used to keep the previous not null value for interpolation. + bool ignoreCloseWindow; } SIntervalAggOperatorInfo; typedef struct SStreamFinalIntervalOperatorInfo { @@ -437,6 +438,7 @@ typedef struct SStreamFinalIntervalOperatorInfo { SArray* pPullWins; // SPullWindowInfo int32_t pullIndex; SSDataBlock* pPullDataRes; + bool ignoreCloseWindow; } SStreamFinalIntervalOperatorInfo; typedef struct SAggOperatorInfo { @@ -574,6 +576,7 @@ typedef struct SStreamSessionAggOperatorInfo { SArray* pChildren; // cache for children's result; final stream operator SPhysiNode* pPhyNode; // create new child bool isFinal; + bool ignoreCloseWindow; } SStreamSessionAggOperatorInfo; typedef struct STimeSliceOperatorInfo { @@ -617,6 +620,7 @@ typedef struct SStreamStateAggOperatorInfo { void* pDelIterator; SArray* pScanWindow; SArray* pChildren; // cache for children's result; + bool ignoreCloseWindow; } SStreamStateAggOperatorInfo; typedef struct SSortedMergeOperatorInfo { diff --git a/source/libs/executor/src/timewindowoperator.c b/source/libs/executor/src/timewindowoperator.c index 963e714972..ea5c4a2c8e 100644 --- a/source/libs/executor/src/timewindowoperator.c +++ b/source/libs/executor/src/timewindowoperator.c @@ -813,6 +813,16 @@ static void removeResults(SArray* pWins, SArray* pUpdated) { } } + +bool isOverdue(TSKEY ts, STimeWindowAggSupp* pSup) { + ASSERT(pSup->maxTs == INT64_MIN || pSup->maxTs > 0); + return pSup->maxTs != INT64_MIN && ts < pSup->maxTs - pSup->waterMark; +} + +bool isCloseWindow(STimeWindow* pWin, STimeWindowAggSupp* pSup) { + return isOverdue(pWin->ekey, pSup); +} + static void hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pResultRowInfo, SSDataBlock* pBlock, int32_t scanFlag, SArray* pUpdated) { SIntervalAggOperatorInfo* pInfo = (SIntervalAggOperatorInfo*)pOperatorInfo->info; @@ -830,15 +840,16 @@ static void hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pResul STimeWindow win = getActiveTimeWindow(pInfo->aggSup.pResultBuf, pResultRowInfo, ts, &pInfo->interval, pInfo->interval.precision, &pInfo->win); + int32_t ret = TSDB_CODE_SUCCESS; + if (!pInfo->ignoreCloseWindow || !isCloseWindow(&win, &pInfo->twAggSup)) { + ret = setTimeWindowOutputBuf(pResultRowInfo, &win, (scanFlag == MAIN_SCAN), &pResult, tableGroupId, + pSup->pCtx, numOfOutput, pSup->rowEntryInfoOffset, &pInfo->aggSup, pTaskInfo); + if (ret != TSDB_CODE_SUCCESS || pResult == NULL) { + longjmp(pTaskInfo->env, TSDB_CODE_QRY_OUT_OF_MEMORY); + } - int32_t ret = setTimeWindowOutputBuf(pResultRowInfo, &win, (scanFlag == MAIN_SCAN), &pResult, tableGroupId, - pSup->pCtx, numOfOutput, pSup->rowEntryInfoOffset, &pInfo->aggSup, pTaskInfo); - if (ret != TSDB_CODE_SUCCESS || pResult == NULL) { - longjmp(pTaskInfo->env, TSDB_CODE_QRY_OUT_OF_MEMORY); - } - - if (pInfo->execModel == OPTR_EXEC_MODEL_STREAM) { - if (pInfo->twAggSup.calTrigger == STREAM_TRIGGER_AT_ONCE) { + if (pInfo->execModel == OPTR_EXEC_MODEL_STREAM && + pInfo->twAggSup.calTrigger == STREAM_TRIGGER_AT_ONCE) { saveResultRow(pResult, tableGroupId, pUpdated); } } @@ -864,9 +875,11 @@ static void hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pResul doWindowBorderInterpolation(pInfo, pBlock, pResult, &win, startPos, forwardRows, pSup); } - updateTimeWindowInfo(&pInfo->twAggSup.timeWindowData, &win, true); - doApplyFunctions(pTaskInfo, pSup->pCtx, &win, &pInfo->twAggSup.timeWindowData, startPos, forwardRows, tsCols, - pBlock->info.rows, numOfOutput, pInfo->order); + if (!pInfo->ignoreCloseWindow || !isCloseWindow(&win, &pInfo->twAggSup)) { + updateTimeWindowInfo(&pInfo->twAggSup.timeWindowData, &win, true); + doApplyFunctions(pTaskInfo, pSup->pCtx, &win, &pInfo->twAggSup.timeWindowData, startPos, forwardRows, tsCols, + pBlock->info.rows, numOfOutput, pInfo->order); + } doCloseWindow(pResultRowInfo, pInfo, pResult); @@ -877,6 +890,12 @@ static void hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pResul if (startPos < 0) { break; } + if (pInfo->ignoreCloseWindow && isCloseWindow(&nextWin, &pInfo->twAggSup)) { + ekey = ascScan ? nextWin.ekey : nextWin.skey; + forwardRows = + getNumOfRowsInTimeWindow(&pBlock->info, tsCols, startPos, ekey, binarySearchForKey, NULL, pInfo->order); + continue; + } // null data, failed to allocate more memory buffer int32_t code = setTimeWindowOutputBuf(pResultRowInfo, &nextWin, (scanFlag == MAIN_SCAN), &pResult, tableGroupId, @@ -885,10 +904,9 @@ static void hashIntervalAgg(SOperatorInfo* pOperatorInfo, SResultRowInfo* pResul longjmp(pTaskInfo->env, TSDB_CODE_QRY_OUT_OF_MEMORY); } - if (pInfo->execModel == OPTR_EXEC_MODEL_STREAM) { - if (pInfo->twAggSup.calTrigger == STREAM_TRIGGER_AT_ONCE) { - saveResultRow(pResult, tableGroupId, pUpdated); - } + if (pInfo->execModel == OPTR_EXEC_MODEL_STREAM && + pInfo->twAggSup.calTrigger == STREAM_TRIGGER_AT_ONCE) { + saveResultRow(pResult, tableGroupId, pUpdated); } ekey = ascScan ? nextWin.ekey : nextWin.skey; @@ -1292,11 +1310,6 @@ static int32_t getAllIntervalWindow(SHashObj* pHashMap, SArray* resWins) { return TSDB_CODE_SUCCESS; } -bool isCloseWindow(STimeWindow* pWin, STimeWindowAggSupp* pSup) { - ASSERT(pSup->maxTs == INT64_MIN || pSup->maxTs > 0); - return pSup->maxTs != INT64_MIN && pWin->ekey < pSup->maxTs - pSup->waterMark; -} - static int32_t closeIntervalWindow(SHashObj* pHashMap, STimeWindowAggSupp* pSup, SInterval* pInterval, SHashObj* pPullDataMap, SArray* closeWins) { void* pIte = NULL; @@ -1411,7 +1424,7 @@ static SSDataBlock* doStreamIntervalAgg(SOperatorInfo* pOperator) { doBuildResultDatablock(pOperator, &pInfo->binfo, &pInfo->groupResInfo, pInfo->aggSup.pResultBuf); pOperator->status = OP_RES_TO_RETURN; - + printDataBlock(pInfo->binfo.pRes, "single interval"); return pInfo->binfo.pRes->info.rows == 0 ? NULL : pInfo->binfo.pRes; } @@ -1521,6 +1534,7 @@ SOperatorInfo* createIntervalOperatorInfo(SOperatorInfo* downstream, SExprInfo* pInfo->interval = *pInterval; pInfo->execModel = pTaskInfo->execModel; pInfo->twAggSup = *pTwAggSupp; + pInfo->ignoreCloseWindow = false; if (pPhyNode->window.pExprs != NULL) { int32_t numOfScalar = 0; @@ -2276,7 +2290,15 @@ static void doHashInterval(SOperatorInfo* pOperatorInfo, SSDataBlock* pSDataBloc STimeWindow nextWin = getActiveTimeWindow(pInfo->aggSup.pResultBuf, pResultRowInfo, ts, &pInfo->interval, pInfo->interval.precision, NULL); while (1) { - if (IS_FINAL_OP(pInfo) && isCloseWindow(&nextWin, &pInfo->twAggSup) && pInfo->pChildren) { + bool isClosed = isCloseWindow(&nextWin, &pInfo->twAggSup); + if (pInfo->ignoreCloseWindow && isClosed) { + startPos = getNexWindowPos(&pInfo->interval, &pSDataBlock->info, tsCols, startPos, nextWin.ekey, &nextWin); + if (startPos < 0) { + break; + } + continue; + } + if (IS_FINAL_OP(pInfo) && isClosed && pInfo->pChildren) { bool ignore = true; SWinRes winRes = {.ts = nextWin.skey, .groupId = tableGroupId,}; void* chIds = taosHashGet(pInfo->pPullDataMap, &winRes, sizeof(SWinRes)); @@ -2684,6 +2706,7 @@ SOperatorInfo* createStreamFinalIntervalOperatorInfo(SOperatorInfo* downstream, _hash_fn_t hashFn = taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY); pInfo->pPullDataMap = taosHashInit(64, hashFn, false, HASH_NO_LOCK); pInfo->pPullDataRes = createPullDataBlock(); + pInfo->ignoreCloseWindow = false; pOperator->operatorType = pPhyNode->type; pOperator->blocking = true; @@ -2830,6 +2853,7 @@ SOperatorInfo* createStreamSessionAggOperatorInfo(SOperatorInfo* downstream, SPh pInfo->pChildren = NULL; pInfo->isFinal = false; pInfo->pPhyNode = pPhyNode; + pInfo->ignoreCloseWindow = false; pOperator->name = "StreamSessionWindowAggOperator"; pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_STREAM_SESSION; @@ -3007,6 +3031,9 @@ static int32_t doOneWindowAggImpl(int32_t tsColId, SOptrBasicInfo* pBinfo, SStre updateTimeWindowInfo(pTimeWindowData, &pCurWin->win, false); doApplyFunctions(pTaskInfo, pSup->pCtx, &pCurWin->win, pTimeWindowData, startIndex, winRows, tsCols, pSDataBlock->info.rows, numOutput, TSDB_ORDER_ASC); + SFilePage* bufPage = getBufPage(pAggSup->pResultBuf, pCurWin->pos.pageId); + setBufPageDirty(bufPage, true); + releaseBufPage(pAggSup->pResultBuf, bufPage); return TSDB_CODE_SUCCESS; } @@ -3063,7 +3090,13 @@ void compactTimeWindow(SStreamSessionAggOperatorInfo* pInfo, int32_t startIndex, pWinInfo->isOutput = false; } taosArrayRemove(pInfo->streamAggSup.pCurWins, i); + SFilePage* tmpPage = getBufPage(pInfo->streamAggSup.pResultBuf, pWinInfo->pos.pageId); + releaseBufPage(pInfo->streamAggSup.pResultBuf, tmpPage); } + SFilePage* bufPage = getBufPage(pInfo->streamAggSup.pResultBuf, pCurWin->pos.pageId); + ASSERT(num > 0); + setBufPageDirty(bufPage, true); + releaseBufPage(pInfo->streamAggSup.pResultBuf, bufPage); } static void doStreamSessionAggImpl(SOperatorInfo* pOperator, SSDataBlock* pSDataBlock, SHashObj* pStUpdated, @@ -3083,22 +3116,23 @@ static void doStreamSessionAggImpl(SOperatorInfo* pOperator, SSDataBlock* pSData SResultRow* pResult = NULL; int32_t winRows = 0; - if (pSDataBlock->pDataBlock != NULL) { - SColumnInfoData* pStartTsCol = taosArrayGet(pSDataBlock->pDataBlock, pInfo->primaryTsIndex); - startTsCols = (int64_t*)pStartTsCol->pData; - SColumnInfoData* pEndTsCol = NULL; - if (hasEndTs) { - pEndTsCol = taosArrayGet(pSDataBlock->pDataBlock, pInfo->endTsIndex); - } else { - pEndTsCol = taosArrayGet(pSDataBlock->pDataBlock, pInfo->primaryTsIndex); - } - endTsCols = (int64_t*)pEndTsCol->pData; + ASSERT(pSDataBlock->pDataBlock); + SColumnInfoData* pStartTsCol = taosArrayGet(pSDataBlock->pDataBlock, pInfo->primaryTsIndex); + startTsCols = (int64_t*)pStartTsCol->pData; + SColumnInfoData* pEndTsCol = NULL; + if (hasEndTs) { + pEndTsCol = taosArrayGet(pSDataBlock->pDataBlock, pInfo->endTsIndex); } else { - return; + pEndTsCol = taosArrayGet(pSDataBlock->pDataBlock, pInfo->primaryTsIndex); } + endTsCols = (int64_t*)pEndTsCol->pData; SStreamAggSupporter* pAggSup = &pInfo->streamAggSup; for (int32_t i = 0; i < pSDataBlock->info.rows;) { + if (pInfo->ignoreCloseWindow && isOverdue(endTsCols[i], &pInfo->twAggSup)) { + i++; + continue; + } int32_t winIndex = 0; SResultWindowInfo* pCurWin = getSessionTimeWindow(pAggSup, startTsCols[i], endTsCols[i], groupId, gap, &winIndex); winRows = @@ -3205,17 +3239,24 @@ static void rebuildTimeWindow(SStreamSessionAggOperatorInfo* pInfo, SArray* pWin index = 0; } for (int32_t k = index; k < chWinSize; k++) { - SResultWindowInfo* pcw = taosArrayGet(pChWins, k); - if (pParentWin->win.skey <= pcw->win.skey && pcw->win.ekey <= pParentWin->win.ekey) { + SResultWindowInfo* pChWin = taosArrayGet(pChWins, k); + if (pParentWin->win.skey <= pChWin->win.skey && pChWin->win.ekey <= pParentWin->win.ekey) { SResultRow* pChResult = NULL; - setWindowOutputBuf(pcw, &pChResult, pChild->exprSupp.pCtx, groupId, numOfOutput, + setWindowOutputBuf(pChWin, &pChResult, pChild->exprSupp.pCtx, groupId, numOfOutput, pChild->exprSupp.rowEntryInfoOffset, &pChInfo->streamAggSup, pTaskInfo); compactFunctions(pSup->pCtx, pChild->exprSupp.pCtx, numOfOutput, pTaskInfo); + SFilePage* bufPage = getBufPage(pInfo->streamAggSup.pResultBuf, pChWin->pos.pageId); + setBufPageDirty(bufPage, true); + releaseBufPage(pInfo->streamAggSup.pResultBuf, bufPage); continue; } break; } } + SFilePage* bufPage = getBufPage(pInfo->streamAggSup.pResultBuf, pParentWin->pos.pageId); + ASSERT(size > 0); + setBufPageDirty(bufPage, true); + releaseBufPage(pInfo->streamAggSup.pResultBuf, bufPage); } } @@ -3234,7 +3275,7 @@ int32_t closeSessionWindow(SHashObj* pHashMap, STimeWindowAggSupp* pTwSup, SArra for (int32_t i = 0; i < size; i++) { void* pWin = taosArrayGet(pWins, i); SResultWindowInfo* pSeWin = fn(pWin); - if (pSeWin->win.ekey < pTwSup->maxTs - pTwSup->waterMark) { + if (isCloseWindow(&pSeWin->win, pTwSup)) { if (!pSeWin->isClosed) { pSeWin->isClosed = true; if (pTwSup->calTrigger == STREAM_TRIGGER_WINDOW_CLOSE) { @@ -3745,6 +3786,10 @@ static void doStreamStateAggImpl(SOperatorInfo* pOperator, SSDataBlock* pSDataBl SStreamAggSupporter* pAggSup = &pInfo->streamAggSup; SColumnInfoData* pKeyColInfo = taosArrayGet(pSDataBlock->pDataBlock, pInfo->stateCol.slotId); for (int32_t i = 0; i < pSDataBlock->info.rows; i += winRows) { + if (pInfo->ignoreCloseWindow && isOverdue(tsCols[i], &pInfo->twAggSup)) { + i++; + continue; + } char* pKeyData = colDataGetData(pKeyColInfo, i); int32_t winIndex = 0; bool allEqual = true; @@ -3895,6 +3940,7 @@ SOperatorInfo* createStreamStateAggOperatorInfo(SOperatorInfo* downstream, SPhys pInfo->pDelRes->info.type = STREAM_DELETE; blockDataEnsureCapacity(pInfo->pDelRes, 64); pInfo->pChildren = NULL; + pInfo->ignoreCloseWindow = false; pOperator->name = "StreamStateAggOperator"; pOperator->operatorType = QUERY_NODE_PHYSICAL_PLAN_STREAM_STATE; From 4c82ce1810ef2424d04c468d60d59af93d9e0193 Mon Sep 17 00:00:00 2001 From: dapan1121 Date: Wed, 29 Jun 2022 17:00:48 +0800 Subject: [PATCH 34/36] fix: fix request freed issue --- include/libs/scheduler/scheduler.h | 14 ++-- source/client/src/clientImpl.c | 78 +++++-------------- source/libs/scheduler/inc/schedulerInt.h | 63 +++++++-------- source/libs/scheduler/src/schJob.c | 13 ++-- source/libs/scheduler/src/scheduler.c | 2 +- source/libs/scheduler/test/schedulerTests.cpp | 20 ++--- 6 files changed, 78 insertions(+), 112 deletions(-) diff --git a/include/libs/scheduler/scheduler.h b/include/libs/scheduler/scheduler.h index ecb21335b9..be3d16ab0d 100644 --- a/include/libs/scheduler/scheduler.h +++ b/include/libs/scheduler/scheduler.h @@ -69,18 +69,20 @@ typedef struct SSchdFetchParam { int32_t* code; } SSchdFetchParam; -typedef void (*schedulerExecCallback)(SQueryResult* pResult, void* param, int32_t code); -typedef void (*schedulerFetchCallback)(void* pResult, void* param, int32_t code); +typedef void (*schedulerExecFp)(SQueryResult* pResult, void* param, int32_t code); +typedef void (*schedulerFetchFp)(void* pResult, void* param, int32_t code); +typedef bool (*schedulerChkKillFp)(void* param); typedef struct SSchedulerReq { - bool *reqKilled; SRequestConnInfo *pConn; SArray *pNodeList; SQueryPlan *pDag; const char *sql; int64_t startTs; - schedulerExecCallback fp; - void* cbParam; + schedulerExecFp execFp; + void* execParam; + schedulerChkKillFp chkKillFp; + void* chkKillParam; } SSchedulerReq; @@ -110,7 +112,7 @@ int32_t schedulerExecJob(SSchedulerReq *pReq, int64_t *pJob, SQueryResult *pRes) */ int32_t schedulerFetchRows(int64_t job, void **data); -void schedulerAsyncFetchRows(int64_t job, schedulerFetchCallback fp, void* param); +void schedulerAsyncFetchRows(int64_t job, schedulerFetchFp fp, void* param); int32_t schedulerGetTasksStatus(int64_t job, SArray *pSub); diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index ce993d7d6d..09fb73cfba 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -55,6 +55,18 @@ static char* getClusterKey(const char* user, const char* auth, const char* ip, i return strdup(key); } +bool chkRequestKilled(void* param) { + bool killed = false; + SRequestObj* pRequest = acquireRequest((int64_t)param); + if (NULL == pRequest || pRequest->killed) { + killed = true; + } + + releaseRequest((int64_t)param); + + return killed; +} + static STscObj* taosConnectImpl(const char* user, const char* auth, const char* db, __taos_async_fn_t fp, void* param, SAppInstInfo* pAppInfo, int connType); @@ -612,58 +624,6 @@ _return: return code; } -int32_t scheduleAsyncQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList) { - tsem_init(&schdRspSem, 0, 0); - - SQueryResult res = {.code = 0, .numOfRows = 0}; - SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter, - .requestId = pRequest->requestId, - .requestObjRefId = pRequest->self}; - SSchedulerReq req = {.pConn = &conn, - .pNodeList = pNodeList, - .pDag = pDag, - .sql = pRequest->sqlstr, - .startTs = pRequest->metric.start, - .fp = schdExecCallback, - .cbParam = &res}; - - int32_t code = schedulerAsyncExecJob(&req, &pRequest->body.queryJob); - - pRequest->body.resInfo.execRes = res.res; - - while (true) { - if (code != TSDB_CODE_SUCCESS) { - if (pRequest->body.queryJob != 0) { - schedulerFreeJob(pRequest->body.queryJob, 0); - } - - pRequest->code = code; - terrno = code; - return pRequest->code; - } else { - tsem_wait(&schdRspSem); - - if (res.code) { - code = res.code; - } else { - break; - } - } - } - - if (TDMT_VND_SUBMIT == pRequest->type || TDMT_VND_CREATE_TABLE == pRequest->type) { - pRequest->body.resInfo.numOfRows = res.numOfRows; - - if (pRequest->body.queryJob != 0) { - schedulerFreeJob(pRequest->body.queryJob, 0); - } - } - - pRequest->code = res.code; - terrno = res.code; - return pRequest->code; -} - int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList) { void* pTransporter = pRequest->pTscObj->pAppInfo->pTransporter; @@ -676,9 +636,10 @@ int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList .pDag = pDag, .sql = pRequest->sqlstr, .startTs = pRequest->metric.start, - .fp = NULL, - .cbParam = NULL, - .reqKilled = &pRequest->killed}; + .execFp = NULL, + .execParam = NULL, + .chkKillFp = chkRequestKilled, + .chkKillParam = (void*)pRequest->self}; int32_t code = schedulerExecJob(&req, &pRequest->body.queryJob, &res); pRequest->body.resInfo.execRes = res.res; @@ -986,9 +947,10 @@ void launchAsyncQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultM .pDag = pDag, .sql = pRequest->sqlstr, .startTs = pRequest->metric.start, - .fp = schedulerExecCb, - .cbParam = pRequest, - .reqKilled = &pRequest->killed}; + .execFp = schedulerExecCb, + .execParam = pRequest, + .chkKillFp = chkRequestKilled, + .chkKillParam = (void*)pRequest->self}; code = schedulerAsyncExecJob(&req, &pRequest->body.queryJob); taosArrayDestroy(pNodeList); } else { diff --git a/source/libs/scheduler/inc/schedulerInt.h b/source/libs/scheduler/inc/schedulerInt.h index a119795787..5998ab6965 100644 --- a/source/libs/scheduler/inc/schedulerInt.h +++ b/source/libs/scheduler/inc/schedulerInt.h @@ -99,8 +99,8 @@ typedef struct SSchStat { typedef struct SSchResInfo { SQueryResult* queryRes; void** fetchRes; - schedulerExecCallback execFp; - schedulerFetchCallback fetchFp; + schedulerExecFp execFp; + schedulerFetchFp fetchFp; void* userParam; } SSchResInfo; @@ -204,37 +204,38 @@ typedef struct { } SSchOpStatus; typedef struct SSchJob { - int64_t refId; - uint64_t queryId; - SSchJobAttr attr; - int32_t levelNum; - int32_t taskNum; - SRequestConnInfo conn; - SArray *nodeList; // qnode/vnode list, SArray - SArray *levels; // starting from 0. SArray - SQueryPlan *pDag; + int64_t refId; + uint64_t queryId; + SSchJobAttr attr; + int32_t levelNum; + int32_t taskNum; + SRequestConnInfo conn; + SArray *nodeList; // qnode/vnode list, SArray + SArray *levels; // starting from 0. SArray + SQueryPlan *pDag; - SArray *dataSrcTasks; // SArray - int32_t levelIdx; - SEpSet dataSrcEps; - SHashObj *taskList; - SHashObj *execTasks; // executing and executed tasks, key:taskid, value:SQueryTask* - SHashObj *flowCtrl; // key is ep, element is SSchFlowControl + SArray *dataSrcTasks; // SArray + int32_t levelIdx; + SEpSet dataSrcEps; + SHashObj *taskList; + SHashObj *execTasks; // executing and executed tasks, key:taskid, value:SQueryTask* + SHashObj *flowCtrl; // key is ep, element is SSchFlowControl - SExplainCtx *explainCtx; - int8_t status; - SQueryNodeAddr resNode; - tsem_t rspSem; - SSchOpStatus opStatus; - bool *reqKilled; - SSchTask *fetchTask; - int32_t errCode; - SRWLatch resLock; - SQueryExecRes execRes; - void *resData; //TODO free it or not - int32_t resNumOfRows; - SSchResInfo userRes; - const char *sql; + SExplainCtx *explainCtx; + int8_t status; + SQueryNodeAddr resNode; + tsem_t rspSem; + SSchOpStatus opStatus; + schedulerChkKillFp chkKillFp; + void* chkKillParam; + SSchTask *fetchTask; + int32_t errCode; + SRWLatch resLock; + SQueryExecRes execRes; + void *resData; //TODO free it or not + int32_t resNumOfRows; + SSchResInfo userRes; + const char *sql; SQueryProfileSummary summary; } SSchJob; diff --git a/source/libs/scheduler/src/schJob.c b/source/libs/scheduler/src/schJob.c index 53f9a934f8..bd3a944c3f 100644 --- a/source/libs/scheduler/src/schJob.c +++ b/source/libs/scheduler/src/schJob.c @@ -55,9 +55,10 @@ int32_t schInitJob(SSchedulerReq *pReq, SSchJob **pSchJob) { pJob->conn = *pReq->pConn; pJob->sql = pReq->sql; pJob->pDag = pReq->pDag; - pJob->reqKilled = pReq->reqKilled; - pJob->userRes.execFp = pReq->fp; - pJob->userRes.userParam = pReq->cbParam; + pJob->chkKillFp = pReq->chkKillFp; + pJob->chkKillParam = pReq->chkKillParam; + pJob->userRes.execFp = pReq->execFp; + pJob->userRes.userParam = pReq->execParam; if (pReq->pNodeList == NULL || taosArrayGetSize(pReq->pNodeList) <= 0) { qDebug("QID:0x%" PRIx64 " input exec nodeList is empty", pReq->pDag->queryId); @@ -182,7 +183,7 @@ FORCE_INLINE bool schJobNeedToStop(SSchJob *pJob, int8_t *pStatus) { *pStatus = status; } - if (*pJob->reqKilled) { + if ((*pJob->chkKillFp)(pJob->chkKillParam)) { schUpdateJobStatus(pJob, JOB_TASK_STATUS_DROPPING); schUpdateJobErrCode(pJob, TSDB_CODE_TSC_QUERY_KILLED); @@ -1584,7 +1585,7 @@ _return: schEndOperation(pJob); if (!sync) { - pReq->fp(NULL, pReq->cbParam, code); + pReq->execFp(NULL, pReq->execParam, code); } schFreeJobImpl(pJob); @@ -1651,7 +1652,7 @@ int32_t schExecJobImpl(SSchedulerReq *pReq, SSchJob *pJob, bool sync) { _return: if (!sync) { - pReq->fp(NULL, pReq->cbParam, code); + pReq->execFp(NULL, pReq->execParam, code); } SCH_RET(code); diff --git a/source/libs/scheduler/src/scheduler.c b/source/libs/scheduler/src/scheduler.c index 8b125811cf..74ddc89b40 100644 --- a/source/libs/scheduler/src/scheduler.c +++ b/source/libs/scheduler/src/scheduler.c @@ -140,7 +140,7 @@ int32_t schedulerFetchRows(int64_t job, void **pData) { SCH_RET(code); } -void schedulerAsyncFetchRows(int64_t job, schedulerFetchCallback fp, void* param) { +void schedulerAsyncFetchRows(int64_t job, schedulerFetchFp fp, void* param) { qDebug("scheduler async fetch rows start"); int32_t code = 0; diff --git a/source/libs/scheduler/test/schedulerTests.cpp b/source/libs/scheduler/test/schedulerTests.cpp index e5cc3cd481..b372ee3ead 100644 --- a/source/libs/scheduler/test/schedulerTests.cpp +++ b/source/libs/scheduler/test/schedulerTests.cpp @@ -511,8 +511,8 @@ void* schtRunJobThread(void *aa) { req.pNodeList = qnodeList; req.pDag = &dag; req.sql = "select * from tb"; - req.fp = schtQueryCb; - req.cbParam = &queryDone; + req.execFp = schtQueryCb; + req.execParam = &queryDone; code = schedulerAsyncExecJob(&req, &queryJobRefId); assert(code == 0); @@ -663,8 +663,8 @@ TEST(queryTest, normalCase) { req.pNodeList = qnodeList; req.pDag = &dag; req.sql = "select * from tb"; - req.fp = schtQueryCb; - req.cbParam = &queryDone; + req.execFp = schtQueryCb; + req.execParam = &queryDone; code = schedulerAsyncExecJob(&req, &job); ASSERT_EQ(code, 0); @@ -767,8 +767,8 @@ TEST(queryTest, readyFirstCase) { req.pNodeList = qnodeList; req.pDag = &dag; req.sql = "select * from tb"; - req.fp = schtQueryCb; - req.cbParam = &queryDone; + req.execFp = schtQueryCb; + req.execParam = &queryDone; code = schedulerAsyncExecJob(&req, &job); ASSERT_EQ(code, 0); @@ -874,8 +874,8 @@ TEST(queryTest, flowCtrlCase) { req.pNodeList = qnodeList; req.pDag = &dag; req.sql = "select * from tb"; - req.fp = schtQueryCb; - req.cbParam = &queryDone; + req.execFp = schtQueryCb; + req.execParam = &queryDone; code = schedulerAsyncExecJob(&req, &job); ASSERT_EQ(code, 0); @@ -987,8 +987,8 @@ TEST(insertTest, normalCase) { req.pNodeList = qnodeList; req.pDag = &dag; req.sql = "insert into tb values(now,1)"; - req.fp = schtQueryCb; - req.cbParam = NULL; + req.execFp = schtQueryCb; + req.execParam = NULL; code = schedulerExecJob(&req, &insertJobRefId, &res); ASSERT_EQ(code, 0); From c3b2b98454a52806f2fabaa0e48921326f3cedc0 Mon Sep 17 00:00:00 2001 From: Minghao Li Date: Wed, 29 Jun 2022 19:18:15 +0800 Subject: [PATCH 35/36] refactor(sync): delete some asserts --- include/libs/sync/syncTools.h | 17 +++++++++++++++++ source/libs/sync/src/syncElection.c | 6 +++++- source/libs/sync/src/syncRaftCfg.c | 19 ++++++++++++++----- 3 files changed, 36 insertions(+), 6 deletions(-) diff --git a/include/libs/sync/syncTools.h b/include/libs/sync/syncTools.h index 5d892352d6..46f279ed85 100644 --- a/include/libs/sync/syncTools.h +++ b/include/libs/sync/syncTools.h @@ -324,6 +324,23 @@ void syncAppendEntriesPrint2(char* s, const SyncAppendEntries* pMsg); void syncAppendEntriesLog(const SyncAppendEntries* pMsg); void syncAppendEntriesLog2(char* s, const SyncAppendEntries* pMsg); +// --------------------------------------------- +typedef struct SyncAppendEntriesBatch { + uint32_t bytes; + int32_t vgId; + uint32_t msgType; + SRaftId srcId; + SRaftId destId; + // private data + SyncTerm term; + SyncIndex prevLogIndex; + SyncTerm prevLogTerm; + SyncIndex commitIndex; + SyncTerm privateTerm; + uint32_t dataLen; + char data[]; +} SyncAppendEntriesBatch; + // --------------------------------------------- typedef struct SyncAppendEntriesReply { uint32_t bytes; diff --git a/source/libs/sync/src/syncElection.c b/source/libs/sync/src/syncElection.c index 738b17b9cb..816430b5b5 100644 --- a/source/libs/sync/src/syncElection.c +++ b/source/libs/sync/src/syncElection.c @@ -75,7 +75,11 @@ int32_t syncNodeElect(SSyncNode* pSyncNode) { if (pSyncNode->state == TAOS_SYNC_STATE_FOLLOWER) { syncNodeFollower2Candidate(pSyncNode); } - ASSERT(pSyncNode->state == TAOS_SYNC_STATE_CANDIDATE); + + if (pSyncNode->state != TAOS_SYNC_STATE_CANDIDATE) { + syncNodeErrorLog(pSyncNode, "not candidate, can not elect"); + return -1; + } // start election raftStoreNextTerm(pSyncNode->pRaftStore); diff --git a/source/libs/sync/src/syncRaftCfg.c b/source/libs/sync/src/syncRaftCfg.c index 9d16bed6c1..43890c196c 100644 --- a/source/libs/sync/src/syncRaftCfg.c +++ b/source/libs/sync/src/syncRaftCfg.c @@ -101,7 +101,7 @@ cJSON *syncCfg2Json(SSyncCfg *pSyncCfg) { char *syncCfg2Str(SSyncCfg *pSyncCfg) { cJSON *pJson = syncCfg2Json(pSyncCfg); - char * serialized = cJSON_Print(pJson); + char *serialized = cJSON_Print(pJson); cJSON_Delete(pJson); return serialized; } @@ -109,7 +109,7 @@ char *syncCfg2Str(SSyncCfg *pSyncCfg) { char *syncCfg2SimpleStr(SSyncCfg *pSyncCfg) { if (pSyncCfg != NULL) { int32_t len = 512; - char * s = taosMemoryMalloc(len); + char *s = taosMemoryMalloc(len); memset(s, 0, len); snprintf(s, len, "{replica-num:%d, my-index:%d, ", pSyncCfg->replicaNum, pSyncCfg->myIndex); @@ -205,7 +205,7 @@ cJSON *raftCfg2Json(SRaftCfg *pRaftCfg) { char *raftCfg2Str(SRaftCfg *pRaftCfg) { cJSON *pJson = raftCfg2Json(pRaftCfg); - char * serialized = cJSON_Print(pJson); + char *serialized = cJSON_Print(pJson); cJSON_Delete(pJson); return serialized; } @@ -214,7 +214,16 @@ int32_t raftCfgCreateFile(SSyncCfg *pCfg, SRaftCfgMeta meta, const char *path) { ASSERT(pCfg != NULL); TdFilePtr pFile = taosOpenFile(path, TD_FILE_CREATE | TD_FILE_WRITE); - ASSERT(pFile != NULL); + if (pFile == NULL) { + int32_t err = terrno; + const char *errStr = tstrerror(err); + int32_t sysErr = errno; + const char *sysErrStr = strerror(errno); + sError("create raft cfg file error, err:%d %X, msg:%s, syserr:%d, sysmsg:%s", err, err, errStr, sysErr, sysErrStr); + ASSERT(0); + + return -1; + } SRaftCfg raftCfg; raftCfg.cfg = *pCfg; @@ -271,7 +280,7 @@ int32_t raftCfgFromJson(const cJSON *pRoot, SRaftCfg *pRaftCfg) { (pRaftCfg->configIndexArr)[i] = atoll(pIndex->valuestring); } - cJSON * pJsonSyncCfg = cJSON_GetObjectItem(pJson, "SSyncCfg"); + cJSON *pJsonSyncCfg = cJSON_GetObjectItem(pJson, "SSyncCfg"); int32_t code = syncCfgFromJson(pJsonSyncCfg, &(pRaftCfg->cfg)); ASSERT(code == 0); From 4e8a925da42a815c7ca71ce913bac3fdcf65ccd8 Mon Sep 17 00:00:00 2001 From: Shuduo Sang Date: Wed, 29 Jun 2022 19:21:39 +0800 Subject: [PATCH 36/36] fix: install script for3.0 (#14363) * fix: remove sudo from directory maniplation for 3.0 * fix: remove more sudo from directory maniplation --- packaging/release.sh | 18 ++++++------------ packaging/rpm/makerpm.sh | 0 2 files changed, 6 insertions(+), 12 deletions(-) mode change 100644 => 100755 packaging/rpm/makerpm.sh diff --git a/packaging/release.sh b/packaging/release.sh index 00a4ad7009..1d9f6dfc68 100755 --- a/packaging/release.sh +++ b/packaging/release.sh @@ -181,15 +181,9 @@ cd "${curr_dir}" # 2. cmake executable file compile_dir="${top_dir}/debug" -if [ -d ${compile_dir} ]; then - ${csudo}rm -rf ${compile_dir} -fi +${csudo}rm -rf ${compile_dir} -if [ "$osType" != "Darwin" ]; then - ${csudo}mkdir -p ${compile_dir} -else - mkdir -p ${compile_dir} -fi +mkdir -p ${compile_dir} cd ${compile_dir} if [[ "$allocator" == "jemalloc" ]]; then @@ -255,9 +249,9 @@ if [ "$osType" != "Darwin" ]; then echo "====do deb package for the ubuntu system====" output_dir="${top_dir}/debs" if [ -d ${output_dir} ]; then - ${csudo}rm -rf ${output_dir} + rm -rf ${output_dir} fi - ${csudo}mkdir -p ${output_dir} + mkdir -p ${output_dir} cd ${script_dir}/deb ${csudo}./makedeb.sh ${compile_dir} ${output_dir} ${verNumber} ${cpuType} ${osType} ${verMode} ${verType} @@ -280,9 +274,9 @@ if [ "$osType" != "Darwin" ]; then echo "====do rpm package for the centos system====" output_dir="${top_dir}/rpms" if [ -d ${output_dir} ]; then - ${csudo}rm -rf ${output_dir} + rm -rf ${output_dir} fi - ${csudo}mkdir -p ${output_dir} + mkdir -p ${output_dir} cd ${script_dir}/rpm ${csudo}./makerpm.sh ${compile_dir} ${output_dir} ${verNumber} ${cpuType} ${osType} ${verMode} ${verType} diff --git a/packaging/rpm/makerpm.sh b/packaging/rpm/makerpm.sh old mode 100644 new mode 100755