From 02ff9967fb10e94a3491def9377629ac39e98347 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Mon, 6 May 2024 12:52:56 +0000 Subject: [PATCH 01/22] fix log --- source/util/src/tsched.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/source/util/src/tsched.c b/source/util/src/tsched.c index 603547e30b..6a1efb5d24 100644 --- a/source/util/src/tsched.c +++ b/source/util/src/tsched.c @@ -28,7 +28,7 @@ static void taosDumpSchedulerStatus(void *qhandle, void *tmrId); void *taosInitScheduler(int32_t queueSize, int32_t numOfThreads, const char *label, SSchedQueue *pSched) { bool schedMalloced = false; - + if (NULL == pSched) { pSched = (SSchedQueue *)taosMemoryCalloc(sizeof(SSchedQueue), 1); if (pSched == NULL) { @@ -100,7 +100,7 @@ void *taosInitScheduler(int32_t queueSize, int32_t numOfThreads, const char *lab int32_t code = taosThreadCreate(pSched->qthread + i, &attr, taosProcessSchedQueue, (void *)pSched); taosThreadAttrDestroy(&attr); if (code != 0) { - uError("%s: failed to create rpc thread(%s)", label, strerror(errno)); + uError("%s: failed to create thread(%s)", label, strerror(errno)); taosCleanUpScheduler(pSched); if (schedMalloced) { taosMemoryFree(pSched); From 8c11a7e998ef94b2715a7092214d5c6efe981b83 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Tue, 7 May 2024 03:10:35 +0000 Subject: [PATCH 02/22] refactor transport --- source/libs/transport/src/transCli.c | 156 +++++++++++++-------------- 1 file changed, 77 insertions(+), 79 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index dfd7630f35..86d6ef58ff 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -212,8 +212,10 @@ static void cliHandleRelease(SCliMsg* pMsg, SCliThrd* pThrd); static void cliHandleUpdate(SCliMsg* pMsg, SCliThrd* pThrd); static void (*cliAsyncHandle[])(SCliMsg* pMsg, SCliThrd* pThrd) = {cliHandleReq, cliHandleQuit, cliHandleRelease, NULL, cliHandleUpdate}; -/// static void (*cliAsyncHandle[])(SCliMsg* pMsg, SCliThrd* pThrd) = {cliHandleReq, cliHandleQuit, cliHandleRelease, -/// NULL,cliHandleUpdate}; + +static void cliDealReq(queue* h, SCliThrd* pThrd); +static void cliBatchDealReq(queue* h, SCliThrd* pThrd); +static void (*cliDealFunc[])(queue* h, SCliThrd* pThrd) = {cliDealReq, cliBatchDealReq}; static FORCE_INLINE void destroyCmsg(void* cmsg); @@ -1695,7 +1697,7 @@ void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd) { tGTrace("%s conn %p ready", pTransInst->label, conn); } -static void cliNoBatchDealReq(queue* wq, SCliThrd* pThrd) { +static void cliDealReq(queue* wq, SCliThrd* pThrd) { int count = 0; while (!QUEUE_IS_EMPTY(wq)) { @@ -1709,7 +1711,6 @@ static void cliNoBatchDealReq(queue* wq, SCliThrd* pThrd) { continue; } (*cliAsyncHandle[pMsg->type])(pMsg, pThrd); - count++; } if (count >= 2) { @@ -1729,7 +1730,77 @@ SCliBatch* cliGetHeadFromList(SCliBatchList* pList) { SCliBatch* batch = QUEUE_DATA(hr, SCliBatch, listq); return batch; } +static void cliBuildBatch(SCliMsg* pMsg, queue* h, SCliThrd* pThrd) { + STrans* pInst = pThrd->pTransInst; + STransConnCtx* pCtx = pMsg->ctx; + char* ip = EPSET_GET_INUSE_IP(&pCtx->epSet); + uint32_t port = EPSET_GET_INUSE_PORT(&pCtx->epSet); + char key[TSDB_FQDN_LEN + 64] = {0}; + CONN_CONSTRUCT_HASH_KEY(key, ip, port); + size_t klen = strlen(key); + SCliBatchList** ppBatchList = taosHashGet(pThrd->batchCache, key, klen); + if (ppBatchList == NULL || *ppBatchList == NULL) { + SCliBatchList* pBatchList = taosMemoryCalloc(1, sizeof(SCliBatchList)); + QUEUE_INIT(&pBatchList->wq); + pBatchList->connMax = pInst->connLimitNum; + pBatchList->connCnt = 0; + pBatchList->batchLenLimit = pInst->batchSize; + pBatchList->len += 1; + + pBatchList->ip = taosStrdup(ip); + pBatchList->dst = taosStrdup(key); + pBatchList->port = port; + + SCliBatch* pBatch = taosMemoryCalloc(1, sizeof(SCliBatch)); + QUEUE_INIT(&pBatch->wq); + QUEUE_INIT(&pBatch->listq); + + QUEUE_PUSH(&pBatch->wq, h); + pBatch->wLen += 1; + pBatch->batchSize += pMsg->msg.contLen; + pBatch->pList = pBatchList; + + QUEUE_PUSH(&pBatchList->wq, &pBatch->listq); + + taosHashPut(pThrd->batchCache, key, klen, &pBatchList, sizeof(void*)); + } else { + if (QUEUE_IS_EMPTY(&(*ppBatchList)->wq)) { + SCliBatch* pBatch = taosMemoryCalloc(1, sizeof(SCliBatch)); + QUEUE_INIT(&pBatch->wq); + QUEUE_INIT(&pBatch->listq); + + QUEUE_PUSH(&pBatch->wq, h); + pBatch->wLen += 1; + pBatch->batchSize = pMsg->msg.contLen; + pBatch->pList = *ppBatchList; + + QUEUE_PUSH(&((*ppBatchList)->wq), &pBatch->listq); + (*ppBatchList)->len += 1; + } + + queue* hdr = QUEUE_TAIL(&((*ppBatchList)->wq)); + SCliBatch* pBatch = QUEUE_DATA(hdr, SCliBatch, listq); + if ((pBatch->batchSize + pMsg->msg.contLen) < (*ppBatchList)->batchLenLimit) { + QUEUE_PUSH(&pBatch->wq, h); + pBatch->batchSize += pMsg->msg.contLen; + pBatch->wLen += 1; + } else { + SCliBatch* pBatch = taosMemoryCalloc(1, sizeof(SCliBatch)); + QUEUE_INIT(&pBatch->wq); + QUEUE_INIT(&pBatch->listq); + + QUEUE_PUSH(&pBatch->wq, h); + pBatch->wLen += 1; + pBatch->batchSize += pMsg->msg.contLen; + pBatch->pList = *ppBatchList; + + QUEUE_PUSH(&((*ppBatchList)->wq), &pBatch->listq); + (*ppBatchList)->len += 1; + } + } + return; +} static void cliBatchDealReq(queue* wq, SCliThrd* pThrd) { STrans* pInst = pThrd->pTransInst; @@ -1746,75 +1817,7 @@ static void cliBatchDealReq(queue* wq, SCliThrd* pThrd) { } if (pMsg->type == Normal && REQUEST_NO_RESP(&pMsg->msg)) { - STransConnCtx* pCtx = pMsg->ctx; - - char* ip = EPSET_GET_INUSE_IP(&pCtx->epSet); - uint32_t port = EPSET_GET_INUSE_PORT(&pCtx->epSet); - char key[TSDB_FQDN_LEN + 64] = {0}; - CONN_CONSTRUCT_HASH_KEY(key, ip, port); - size_t klen = strlen(key); - SCliBatchList** ppBatchList = taosHashGet(pThrd->batchCache, key, klen); - if (ppBatchList == NULL || *ppBatchList == NULL) { - SCliBatchList* pBatchList = taosMemoryCalloc(1, sizeof(SCliBatchList)); - QUEUE_INIT(&pBatchList->wq); - pBatchList->connMax = pInst->connLimitNum; - pBatchList->connCnt = 0; - pBatchList->batchLenLimit = pInst->batchSize; - pBatchList->len += 1; - - pBatchList->ip = taosStrdup(ip); - pBatchList->dst = taosStrdup(key); - pBatchList->port = port; - - SCliBatch* pBatch = taosMemoryCalloc(1, sizeof(SCliBatch)); - QUEUE_INIT(&pBatch->wq); - QUEUE_INIT(&pBatch->listq); - - QUEUE_PUSH(&pBatch->wq, h); - pBatch->wLen += 1; - pBatch->batchSize += pMsg->msg.contLen; - pBatch->pList = pBatchList; - - QUEUE_PUSH(&pBatchList->wq, &pBatch->listq); - - taosHashPut(pThrd->batchCache, key, klen, &pBatchList, sizeof(void*)); - } else { - if (QUEUE_IS_EMPTY(&(*ppBatchList)->wq)) { - SCliBatch* pBatch = taosMemoryCalloc(1, sizeof(SCliBatch)); - QUEUE_INIT(&pBatch->wq); - QUEUE_INIT(&pBatch->listq); - - QUEUE_PUSH(&pBatch->wq, h); - pBatch->wLen += 1; - pBatch->batchSize = pMsg->msg.contLen; - pBatch->pList = *ppBatchList; - - QUEUE_PUSH(&((*ppBatchList)->wq), &pBatch->listq); - (*ppBatchList)->len += 1; - - continue; - } - - queue* hdr = QUEUE_TAIL(&((*ppBatchList)->wq)); - SCliBatch* pBatch = QUEUE_DATA(hdr, SCliBatch, listq); - if ((pBatch->batchSize + pMsg->msg.contLen) < (*ppBatchList)->batchLenLimit) { - QUEUE_PUSH(&pBatch->wq, h); - pBatch->batchSize += pMsg->msg.contLen; - pBatch->wLen += 1; - } else { - SCliBatch* pBatch = taosMemoryCalloc(1, sizeof(SCliBatch)); - QUEUE_INIT(&pBatch->wq); - QUEUE_INIT(&pBatch->listq); - - QUEUE_PUSH(&pBatch->wq, h); - pBatch->wLen += 1; - pBatch->batchSize += pMsg->msg.contLen; - pBatch->pList = *ppBatchList; - - QUEUE_PUSH(&((*ppBatchList)->wq), &pBatch->listq); - (*ppBatchList)->len += 1; - } - } + cliBuildBatch(pMsg, h, pThrd); continue; } (*cliAsyncHandle[pMsg->type])(pMsg, pThrd); @@ -1847,12 +1850,7 @@ static void cliAsyncCb(uv_async_t* handle) { QUEUE_MOVE(&item->qmsg, &wq); taosThreadMutexUnlock(&item->mtx); - int8_t supportBatch = pTransInst->supportBatch; - if (supportBatch == 0) { - cliNoBatchDealReq(&wq, pThrd); - } else if (supportBatch == 1) { - cliBatchDealReq(&wq, pThrd); - } + cliDealFunc[pTransInst->supportBatch](&wq, pThrd); if (pThrd->stopMsg != NULL) cliHandleQuit(pThrd->stopMsg, pThrd); } From 19327071370e596eb8cc6842f8a61863ba222466 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Tue, 7 May 2024 03:28:31 +0000 Subject: [PATCH 03/22] refactor transport --- source/libs/stream/src/streamBackendRocksdb.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/libs/stream/src/streamBackendRocksdb.c b/source/libs/stream/src/streamBackendRocksdb.c index 358795a4a2..fe9d78d8f9 100644 --- a/source/libs/stream/src/streamBackendRocksdb.c +++ b/source/libs/stream/src/streamBackendRocksdb.c @@ -2775,6 +2775,9 @@ int32_t streamStateGetKVByCur_rocksdb(SStreamStateCur* pCur, SWinKey* pKey, cons size_t vlen = 0; const char* valStr = rocksdb_iter_value(pCur->iter, &vlen); *pVLen = valueDecode((void*)valStr, vlen, NULL, (char**)pVal); + if (*pVLen < 0) { + return -1; + } } *pKey = pKtmp->key; From 4d3be8129e869c1191d58c92bd8e38dcffefddcd Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Wed, 8 May 2024 01:01:42 +0000 Subject: [PATCH 04/22] refactor transport --- source/libs/transport/src/transCli.c | 1 + 1 file changed, 1 insertion(+) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 86d6ef58ff..98467b5c09 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -1777,6 +1777,7 @@ static void cliBuildBatch(SCliMsg* pMsg, queue* h, SCliThrd* pThrd) { QUEUE_PUSH(&((*ppBatchList)->wq), &pBatch->listq); (*ppBatchList)->len += 1; + return; } queue* hdr = QUEUE_TAIL(&((*ppBatchList)->wq)); From 29215d2411eba4575e78e7bd3a7ef39c77d4ba38 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Wed, 8 May 2024 10:16:10 +0000 Subject: [PATCH 05/22] refactor transport --- source/libs/transport/src/transCli.c | 14 ++++---------- source/libs/transport/src/transSvr.c | 3 ++- 2 files changed, 6 insertions(+), 11 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 98467b5c09..97a15c7fc8 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -113,7 +113,6 @@ typedef struct SCliThrd { uint64_t nextTimeout; // next timeout void* pTransInst; // - int connCount; void (*destroyAhandleFp)(void* ahandle); SHashObj* fqdn2ipCache; SCvtAddr cvtAddr; @@ -915,7 +914,6 @@ static SCliConn* cliCreateConn(SCliThrd* pThrd) { conn->broken = false; transRefCliHandle(conn); - atomic_add_fetch_32(&pThrd->connCount, 1); allocConnRef(conn, false); return conn; @@ -977,8 +975,6 @@ static void cliDestroy(uv_handle_t* handle) { conn->timer = NULL; } - atomic_sub_fetch_32(&pThrd->connCount, 1); - transReleaseExHandle(transGetRefMgt(), conn->refId); transRemoveExHandle(transGetRefMgt(), conn->refId); taosMemoryFree(conn->dstAddr); @@ -2002,10 +1998,8 @@ static FORCE_INLINE void destroyCmsg(void* arg) { taosMemoryFree(pMsg); } static FORCE_INLINE void destroyCmsgWrapper(void* arg, void* param) { + if (arg == NULL) return; SCliMsg* pMsg = arg; - if (pMsg == NULL) { - return; - } SCliThrd* pThrd = param; if (pMsg->msg.info.notFreeAhandle == 0 && pThrd != NULL) { @@ -2335,9 +2329,9 @@ bool cliGenRetryRule(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) { // code, msgType - // A: epset, leader, not self - // B: epset, not know leader - // C: no epset, leader but not serivce + // A: epset,leader, not self + // B: epset,not know leader + // C: noepset,leader but not serivce bool noDelay = false; if (code == TSDB_CODE_RPC_BROKEN_LINK || code == TSDB_CODE_RPC_NETWORK_UNAVAIL) { diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index 21ad5be869..d47968eeb8 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -676,7 +676,8 @@ static FORCE_INLINE void destroySmsg(SSvrMsg* smsg) { taosMemoryFree(smsg); } static FORCE_INLINE void destroySmsgWrapper(void* smsg, void* param) { destroySmsg((SSvrMsg*)smsg); } -static void destroyAllConn(SWorkThrd* pThrd) { + +static void destroyAllConn(SWorkThrd* pThrd) { tTrace("thread %p destroy all conn ", pThrd); while (!QUEUE_IS_EMPTY(&pThrd->conn)) { queue* h = QUEUE_HEAD(&pThrd->conn); From 77c7ee264c11e22f826aac50537896e79a5522b0 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Thu, 16 May 2024 11:42:14 +0800 Subject: [PATCH 06/22] refactor transport --- source/libs/transport/src/transCli.c | 95 +++++++++++++--------------- 1 file changed, 45 insertions(+), 50 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 97a15c7fc8..419c037a5d 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -175,6 +175,7 @@ static bool cliRecvReleaseReq(SCliConn* conn, STransMsgHead* pHead); static int32_t allocConnRef(SCliConn* conn, bool update); static int cliAppCb(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg); +void cliResetConnTimer(SCliConn* conn); static SCliConn* cliCreateConn(SCliThrd* thrd); static void cliDestroyConn(SCliConn* pConn, bool clear /*clear tcp handle or not*/); @@ -367,10 +368,8 @@ bool cliConnSendSeqMsg(int64_t refId, SCliConn* conn) { return false; } -void cliHandleResp(SCliConn* conn) { +void cliResetConnTimer(SCliConn* conn) { SCliThrd* pThrd = conn->hostThrd; - STrans* pTransInst = pThrd->pTransInst; - if (conn->timer) { if (uv_is_active((uv_handle_t*)conn->timer)) { tDebug("%s conn %p stop timer", CONN_GET_INST_LABEL(conn), conn); @@ -380,6 +379,40 @@ void cliHandleResp(SCliConn* conn) { conn->timer->data = NULL; conn->timer = NULL; } +} +void cliHandleBatchResp(SCliConn* conn) { + SCliThrd* pThrd = conn->hostThrd; + STrans* pTransInst = pThrd->pTransInst; + cliResetConnTimer(conn); + + STransMsgHead* pHead = NULL; + int32_t msgLen = transDumpFromBuffer(&conn->readBuf, (char**)&pHead); + if (transDecompressMsg((char**)&pHead, msgLen) < 0) { + tDebug("%s conn %p recv invalid packet, failed to decompress", CONN_GET_INST_LABEL(conn), conn); + } + pHead->code = htonl(pHead->code); + pHead->msgLen = htonl(pHead->msgLen); + + STransMsg transMsg = {0}; + transMsg.contLen = transContLenFromMsg(pHead->msgLen); + transMsg.pCont = transContFromHead((char*)pHead); + transMsg.code = pHead->code; + transMsg.msgType = pHead->msgType; + transMsg.info.ahandle = NULL; + transMsg.info.traceId = pHead->traceId; + transMsg.info.hasEpSet = pHead->hasEpSet; + transMsg.info.cliVer = htonl(pHead->compatibilityVer); + + SCliMsg* pMsg = NULL; + STransConnCtx* pCtx = pMsg->ctx; + if (cliAppCb(conn, &transMsg, pMsg) != 0) { + return; + } +} +void cliHandleResp(SCliConn* conn) { + SCliThrd* pThrd = conn->hostThrd; + STrans* pTransInst = pThrd->pTransInst; + cliResetConnTimer(conn); STransMsgHead* pHead = NULL; @@ -578,11 +611,7 @@ void cliConnTimeout(uv_timer_t* handle) { tTrace("%s conn %p conn timeout, ref:%d", CONN_GET_INST_LABEL(conn), conn, T_REF_VAL_GET(conn)); - uv_timer_stop(handle); - handle->data = NULL; - taosArrayPush(pThrd->timerList, &conn->timer); - conn->timer = NULL; - + cliResetConnTimer(conn); cliMayUpdateFqdnCache(pThrd->fqdn2ipCache, conn->dstAddr); cliHandleFastFail(conn, UV_ECANCELED); } @@ -757,12 +786,7 @@ static void addConnToPool(void* pool, SCliConn* conn) { allocConnRef(conn, true); SCliThrd* thrd = conn->hostThrd; - if (conn->timer != NULL) { - uv_timer_stop(conn->timer); - taosArrayPush(thrd->timerList, &conn->timer); - conn->timer->data = NULL; - conn->timer = NULL; - } + cliResetConnTimer(conn); if (T_REF_VAL_GET(conn) > 1) { transUnrefCliHandle(conn); } @@ -948,12 +972,7 @@ static void cliDestroyConn(SCliConn* conn, bool clear) { transDQCancel(pThrd->timeoutQueue, conn->task); conn->task = NULL; } - if (conn->timer != NULL) { - uv_timer_stop(conn->timer); - conn->timer->data = NULL; - taosArrayPush(pThrd->timerList, &conn->timer); - conn->timer = NULL; - } + cliResetConnTimer(conn); if (clear) { if (!uv_is_closing((uv_handle_t*)conn->stream)) { @@ -968,12 +987,7 @@ static void cliDestroy(uv_handle_t* handle) { } SCliConn* conn = handle->data; SCliThrd* pThrd = conn->hostThrd; - if (conn->timer != NULL) { - uv_timer_stop(conn->timer); - taosArrayPush(pThrd->timerList, &conn->timer); - conn->timer->data = NULL; - conn->timer = NULL; - } + cliResetConnTimer(conn); transReleaseExHandle(transGetRefMgt(), conn->refId); transRemoveExHandle(transGetRefMgt(), conn->refId); @@ -1230,11 +1244,7 @@ static void cliHandleBatchReq(SCliBatch* pBatch, SCliThrd* pThrd) { uint32_t ipaddr = cliGetIpFromFqdnCache(pThrd->fqdn2ipCache, pList->ip); if (ipaddr == 0xffffffff) { - uv_timer_stop(conn->timer); - conn->timer->data = NULL; - taosArrayPush(pThrd->timerList, &conn->timer); - conn->timer = NULL; - + cliResetConnTimer(conn); cliHandleFastFail(conn, -1); return; } @@ -1266,11 +1276,7 @@ static void cliHandleBatchReq(SCliBatch* pBatch, SCliThrd* pThrd) { ret = uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, cliConnCb); if (ret != 0) { - uv_timer_stop(conn->timer); - conn->timer->data = NULL; - taosArrayPush(pThrd->timerList, &conn->timer); - conn->timer = NULL; - + cliResetConnTimer(conn); cliMayUpdateFqdnCache(pThrd->fqdn2ipCache, conn->dstAddr); cliHandleFastFail(conn, -1); return; @@ -1366,10 +1372,7 @@ void cliConnCb(uv_connect_t* req, int status) { if (pConn->timer == NULL) { timeout = true; } else { - uv_timer_stop(pConn->timer); - pConn->timer->data = NULL; - taosArrayPush(pThrd->timerList, &pConn->timer); - pConn->timer = NULL; + cliResetConnTimer(pConn); } if (status != 0) { @@ -1641,11 +1644,7 @@ void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd) { uint32_t ipaddr = cliGetIpFromFqdnCache(pThrd->fqdn2ipCache, fqdn); if (ipaddr == 0xffffffff) { - uv_timer_stop(conn->timer); - conn->timer->data = NULL; - taosArrayPush(pThrd->timerList, &conn->timer); - conn->timer = NULL; - + cliResetConnTimer(conn); cliHandleExcept(conn); return; } @@ -1679,11 +1678,7 @@ void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd) { ret = uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, cliConnCb); if (ret != 0) { - uv_timer_stop(conn->timer); - conn->timer->data = NULL; - taosArrayPush(pThrd->timerList, &conn->timer); - conn->timer = NULL; - + cliResetConnTimer(conn); cliMayUpdateFqdnCache(pThrd->fqdn2ipCache, conn->dstAddr); cliHandleFastFail(conn, ret); return; From 81a4ef73f1121d8b74e166ab42d455b6fe2a1ceb Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Thu, 16 May 2024 16:26:49 +0800 Subject: [PATCH 07/22] refactor transport --- source/libs/transport/src/transCli.c | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 419c037a5d..023cb16b8b 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -1803,13 +1803,13 @@ static void cliBatchDealReq(queue* wq, SCliThrd* pThrd) { SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q); - if (pMsg->type == Quit) { - pThrd->stopMsg = pMsg; + if (pMsg->type == Normal && REQUEST_NO_RESP(&pMsg->msg)) { + cliBuildBatch(pMsg, h, pThrd); continue; } - if (pMsg->type == Normal && REQUEST_NO_RESP(&pMsg->msg)) { - cliBuildBatch(pMsg, h, pThrd); + if (pMsg->type == Quit) { + pThrd->stopMsg = pMsg; continue; } (*cliAsyncHandle[pMsg->type])(pMsg, pThrd); From a0abe6743271db820b10189182233659ab553220 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Thu, 23 May 2024 06:49:24 +0000 Subject: [PATCH 08/22] refactor tranport --- source/libs/transport/inc/transportInt.h | 1 + source/libs/transport/src/transCli.c | 105 ++++++++++++----------- 2 files changed, 57 insertions(+), 49 deletions(-) diff --git a/source/libs/transport/inc/transportInt.h b/source/libs/transport/inc/transportInt.h index 7853e25cff..232210b53b 100644 --- a/source/libs/transport/inc/transportInt.h +++ b/source/libs/transport/inc/transportInt.h @@ -69,6 +69,7 @@ typedef struct { int8_t connLimitLock; // 0: no lock. 1. lock int8_t supportBatch; // 0: no batch, 1: support batch int32_t batchSize; + int8_t optBatchFetch; int32_t timeToGetConn; int index; void* parent; diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 023cb16b8b..f6e4c4ecf9 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -1213,23 +1213,67 @@ static void cliDestroyBatch(SCliBatch* pBatch) { p->sending -= 1; taosMemoryFree(pBatch); } + +static void cliDoConn(SCliThrd* pThrd, SCliConn* conn, char* ip, int port) { + STrans* pTransInst = pThrd->pTransInst; + uint32_t ipaddr = cliGetIpFromFqdnCache(pThrd->fqdn2ipCache, ip); + if (ipaddr == 0xffffffff) { + cliResetConnTimer(conn); + cliHandleFastFail(conn, -1); + return; + } + + struct sockaddr_in addr; + addr.sin_family = AF_INET; + addr.sin_addr.s_addr = ipaddr; + addr.sin_port = (uint16_t)htons(port); + + tTrace("%s conn %p try to connect to %s", pTransInst->label, conn, conn->dstAddr); + int32_t fd = taosCreateSocketWithTimeout(TRANS_CONN_TIMEOUT * 10); + if (fd == -1) { + tError("%s conn %p failed to create socket, reason:%s", transLabel(pTransInst), conn, + tstrerror(TAOS_SYSTEM_ERROR(errno))); + cliHandleFastFail(conn, -1); + return; + } + int ret = uv_tcp_open((uv_tcp_t*)conn->stream, fd); + if (ret != 0) { + tError("%s conn %p failed to set stream, reason:%s", transLabel(pTransInst), conn, uv_err_name(ret)); + cliHandleFastFail(conn, -1); + return; + } + ret = transSetConnOption((uv_tcp_t*)conn->stream, 20); + if (ret != 0) { + tError("%s conn %p failed to set socket opt, reason:%s", transLabel(pTransInst), conn, uv_err_name(ret)); + cliHandleFastFail(conn, -1); + return; + } + + ret = uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, cliConnCb); + if (ret != 0) { + cliResetConnTimer(conn); + cliMayUpdateFqdnCache(pThrd->fqdn2ipCache, conn->dstAddr); + cliHandleFastFail(conn, -1); + return; + } + uv_timer_start(conn->timer, cliConnTimeout, TRANS_CONN_TIMEOUT, 0); + return; +} static void cliHandleBatchReq(SCliBatch* pBatch, SCliThrd* pThrd) { + if (pBatch == NULL || pBatch->wLen == 0 || QUEUE_IS_EMPTY(&pBatch->wq)) { + return; + } + if (pThrd->quit == true) { cliDestroyBatch(pBatch); return; } - if (pBatch == NULL || pBatch->wLen == 0 || QUEUE_IS_EMPTY(&pBatch->wq)) { - return; - } STrans* pTransInst = pThrd->pTransInst; SCliBatchList* pList = pBatch->pList; - char key[TSDB_FQDN_LEN + 64] = {0}; - CONN_CONSTRUCT_HASH_KEY(key, pList->ip, pList->port); - bool exceed = false; - SCliConn* conn = getConnFromPool(pThrd, key, &exceed); + SCliConn* conn = getConnFromPool(pThrd, pList->dst, &exceed); if (conn == NULL && exceed) { tError("%s failed to send batch msg, batch size:%d, msgLen: %d, conn limit:%d", pTransInst->label, pBatch->wLen, @@ -1241,48 +1285,7 @@ static void cliHandleBatchReq(SCliBatch* pBatch, SCliThrd* pThrd) { conn = cliCreateConn(pThrd); conn->pBatch = pBatch; conn->dstAddr = taosStrdup(pList->dst); - - uint32_t ipaddr = cliGetIpFromFqdnCache(pThrd->fqdn2ipCache, pList->ip); - if (ipaddr == 0xffffffff) { - cliResetConnTimer(conn); - cliHandleFastFail(conn, -1); - return; - } - struct sockaddr_in addr; - addr.sin_family = AF_INET; - addr.sin_addr.s_addr = ipaddr; - addr.sin_port = (uint16_t)htons(pList->port); - - tTrace("%s conn %p try to connect to %s", pTransInst->label, conn, pList->dst); - int32_t fd = taosCreateSocketWithTimeout(TRANS_CONN_TIMEOUT * 10); - if (fd == -1) { - tError("%s conn %p failed to create socket, reason:%s", transLabel(pTransInst), conn, - tstrerror(TAOS_SYSTEM_ERROR(errno))); - cliHandleFastFail(conn, -1); - return; - } - int ret = uv_tcp_open((uv_tcp_t*)conn->stream, fd); - if (ret != 0) { - tError("%s conn %p failed to set stream, reason:%s", transLabel(pTransInst), conn, uv_err_name(ret)); - cliHandleFastFail(conn, -1); - return; - } - ret = transSetConnOption((uv_tcp_t*)conn->stream, 20); - if (ret != 0) { - tError("%s conn %p failed to set socket opt, reason:%s", transLabel(pTransInst), conn, uv_err_name(ret)); - cliHandleFastFail(conn, -1); - return; - } - - ret = uv_tcp_connect(&conn->connReq, (uv_tcp_t*)(conn->stream), (const struct sockaddr*)&addr, cliConnCb); - if (ret != 0) { - cliResetConnTimer(conn); - cliMayUpdateFqdnCache(pThrd->fqdn2ipCache, conn->dstAddr); - cliHandleFastFail(conn, -1); - return; - } - uv_timer_start(conn->timer, cliConnTimeout, TRANS_CONN_TIMEOUT, 0); - return; + return cliDoConn(pThrd, conn, pList->ip, pList->port); } conn->pBatch = pBatch; @@ -1803,6 +1806,10 @@ static void cliBatchDealReq(queue* wq, SCliThrd* pThrd) { SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q); + if (pMsg->type == Normal) { + cliBuildBatch(pMsg, h, pThrd); + count++; + } if (pMsg->type == Normal && REQUEST_NO_RESP(&pMsg->msg)) { cliBuildBatch(pMsg, h, pThrd); continue; From 1685fab216108a03ddaeb93afdfa4211c80889d5 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Thu, 23 May 2024 07:58:15 +0000 Subject: [PATCH 09/22] refactor tranport --- source/libs/transport/src/transCli.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index f6e4c4ecf9..5c1b970ceb 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -1806,10 +1806,11 @@ static void cliBatchDealReq(queue* wq, SCliThrd* pThrd) { SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q); - if (pMsg->type == Normal) { - cliBuildBatch(pMsg, h, pThrd); - count++; - } + // if (pMsg->type == Normal) { + // cliBuildBatch(pMsg, h, pThrd); + // continue; + // // count++; + // } if (pMsg->type == Normal && REQUEST_NO_RESP(&pMsg->msg)) { cliBuildBatch(pMsg, h, pThrd); continue; From 2cdaa8c96246b765135eeb12b74e3d7fa1d309c9 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Wed, 29 May 2024 09:19:27 +0000 Subject: [PATCH 10/22] opt transport --- include/libs/transport/trpc.h | 1 + 1 file changed, 1 insertion(+) diff --git a/include/libs/transport/trpc.h b/include/libs/transport/trpc.h index 95f70c8ff3..e6b59fd814 100644 --- a/include/libs/transport/trpc.h +++ b/include/libs/transport/trpc.h @@ -126,6 +126,7 @@ typedef struct SRpcInit { int32_t timeToGetConn; int8_t supportBatch; // 0: no batch, 1. batch int32_t batchSize; + int8_t shareConn; // 0: no share, 1. share void *parent; } SRpcInit; From 9138ea4cec30269359a007f18c990183e2b99a3c Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Wed, 29 May 2024 09:20:30 +0000 Subject: [PATCH 11/22] opt transport --- source/dnode/mgmt/node_mgmt/src/dmTransport.c | 1 + source/dnode/vnode/src/vnd/vnodeSvr.c | 14 +- source/libs/transport/inc/transportInt.h | 1 + source/libs/transport/src/trans.c | 2 + source/libs/transport/src/transCli.c | 209 +++++++++++++++++- 5 files changed, 217 insertions(+), 10 deletions(-) diff --git a/source/dnode/mgmt/node_mgmt/src/dmTransport.c b/source/dnode/mgmt/node_mgmt/src/dmTransport.c index a2355ddd22..f4e6d42e60 100644 --- a/source/dnode/mgmt/node_mgmt/src/dmTransport.c +++ b/source/dnode/mgmt/node_mgmt/src/dmTransport.c @@ -372,6 +372,7 @@ int32_t dmInitClient(SDnode *pDnode) { rpcInit.connLimitLock = 1; rpcInit.supportBatch = 1; rpcInit.batchSize = 8 * 1024; + rpcInit.shareConn = 1; rpcInit.timeToGetConn = tsTimeToGetAvailableConn; taosVersionStrToInt(version, &(rpcInit.compatibilityVer)); diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index 6d97c1cd79..6438601937 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -50,7 +50,7 @@ static int32_t vnodeProcessArbCheckSyncReq(SVnode *pVnode, void *pReq, int32_t l static int32_t vnodePreCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token); static int32_t vnodeCheckAssignedLogSyncd(SVnode *pVnode, char *member0Token, char *member1Token); -static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode* pVnode, int64_t ver, void* pReq, int32_t len, SRpcMsg* pRsp); +static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp); extern int32_t vnodeProcessKillCompactReq(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp); extern int32_t vnodeQueryCompactProgress(SVnode *pVnode, SRpcMsg *pMsg); @@ -931,13 +931,13 @@ end: return ret; } -static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode* pVnode, int64_t ver, void* pReq, int32_t len, SRpcMsg* pRsp) { +static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void *pReq, int32_t len, SRpcMsg *pRsp) { int32_t code = -1; SMetaReader mr = {0}; SVDropTtlTableReq ttlReq = {0}; SVFetchTtlExpiredTbsRsp rsp = {0}; SEncoder encoder = {0}; - SArray* pNames = NULL; + SArray *pNames = NULL; pRsp->msgType = TDMT_VND_FETCH_TTL_EXPIRED_TBS_RSP; pRsp->code = TSDB_CODE_SUCCESS; pRsp->pCont = NULL; @@ -950,8 +950,8 @@ static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode* pVnode, int64_t ver, void* ASSERT(ttlReq.nUids == taosArrayGetSize(ttlReq.pTbUids)); - tb_uid_t suid; - char ctbName[TSDB_TABLE_NAME_LEN]; + tb_uid_t suid; + char ctbName[TSDB_TABLE_NAME_LEN]; SVDropTbReq expiredTb = {.igNotExists = true}; metaReaderDoInit(&mr, pVnode->pMeta, 0); rsp.vgId = TD_VID(pVnode); @@ -965,12 +965,12 @@ static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode* pVnode, int64_t ver, void* } char buf[TSDB_TABLE_NAME_LEN]; for (int32_t i = 0; i < ttlReq.nUids; ++i) { - tb_uid_t* uid = taosArrayGet(ttlReq.pTbUids, i); + tb_uid_t *uid = taosArrayGet(ttlReq.pTbUids, i); expiredTb.suid = *uid; terrno = metaReaderGetTableEntryByUid(&mr, *uid); if (terrno < 0) goto _end; strncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN); - void* p = taosArrayPush(pNames, buf); + void *p = taosArrayPush(pNames, buf); expiredTb.name = p; if (mr.me.type == TSDB_CHILD_TABLE) { expiredTb.suid = mr.me.ctbEntry.suid; diff --git a/source/libs/transport/inc/transportInt.h b/source/libs/transport/inc/transportInt.h index 232210b53b..8e8672a357 100644 --- a/source/libs/transport/inc/transportInt.h +++ b/source/libs/transport/inc/transportInt.h @@ -75,6 +75,7 @@ typedef struct { void* parent; void* tcphandle; // returned handle from TCP initialization int64_t refId; + int8_t shareConn; TdThreadMutex mutex; } SRpcInfo; diff --git a/source/libs/transport/src/trans.c b/source/libs/transport/src/trans.c index 5ed2e00acd..4bfd0c0805 100644 --- a/source/libs/transport/src/trans.c +++ b/source/libs/transport/src/trans.c @@ -112,6 +112,8 @@ void* rpcOpen(const SRpcInit* pInit) { int64_t refId = transAddExHandle(transGetInstMgt(), pRpc); transAcquireExHandle(transGetInstMgt(), refId); pRpc->refId = refId; + + pRpc->shareConn = pInit->shareConn; return (void*)refId; } void rpcClose(void* arg) { diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 5c1b970ceb..e59065ef26 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -76,6 +76,9 @@ typedef struct SCliConn { SDelayTask* task; + HeapNode node; // for heap + int8_t inHeap; + char* dstAddr; char src[32]; char dst[32]; @@ -119,6 +122,7 @@ typedef struct SCliThrd { SHashObj* failFastCache; SHashObj* batchCache; + SHashObj* connHeapCache; SCliMsg* stopMsg; bool quit; @@ -230,6 +234,23 @@ static void destroyThrdObj(SCliThrd* pThrd); static void cliWalkCb(uv_handle_t* handle, void* arg); +typedef struct { + void* p; + HeapNode node; +} SHeapNode; +typedef struct { + // void* p; + Heap* heap; + int32_t (*cmpFunc)(const HeapNode* a, const HeapNode* b); +} SHeap; + +int32_t compareHeapNode(const HeapNode* a, const HeapNode* b); +int transHeapCreate(SHeap* heap, int32_t (*cmpFunc)(const HeapNode* a, const HeapNode* b)); +void transHeapDestroy(SHeap* heap); +void transHeapGet(SHeap* heap, SCliConn** p); +int transHeapInsert(SHeap* heap, SCliConn* p); +int transHeapDelete(SHeap* heap, SCliConn* p); + #define CLI_RELEASE_UV(loop) \ do { \ uv_walk(loop, cliWalkCb, NULL); \ @@ -1054,6 +1075,66 @@ static void cliSendCb(uv_write_t* req, int status) { } uv_read_start((uv_stream_t*)pConn->stream, cliAllocRecvBufferCb, cliRecvCb); } + +void cliSendBatch_shareConn(SCliConn* pConn) { + SCliThrd* pThrd = pConn->hostThrd; + STrans* pTransInst = pThrd->pTransInst; + int32_t size = transQueueSize(&pConn->cliMsgs); + int32_t totalLen = 0; + if (size == 0) { + tError("%s conn %p not msg to send", pTransInst->label, pConn); + cliHandleExcept(pConn); + return; + } + uv_buf_t* wb = taosMemoryCalloc(size, sizeof(uv_buf_t)); + + for (int i = 0; i < size; i++) { + SCliMsg* pCliMsg = transQueueGet(&pConn->cliMsgs, i); + + STransConnCtx* pCtx = pCliMsg->ctx; + + STransMsg* pMsg = (STransMsg*)(&pCliMsg->msg); + if (pMsg->pCont == 0) { + pMsg->pCont = (void*)rpcMallocCont(0); + pMsg->contLen = 0; + } + + int msgLen = transMsgLenFromCont(pMsg->contLen); + STransMsgHead* pHead = transHeadFromCont(pMsg->pCont); + + if (pHead->comp == 0) { + pHead->ahandle = pCtx != NULL ? (uint64_t)pCtx->ahandle : 0; + pHead->noResp = REQUEST_NO_RESP(pMsg) ? 1 : 0; + pHead->persist = REQUEST_PERSIS_HANDLE(pMsg) ? 1 : 0; + pHead->msgType = pMsg->msgType; + pHead->msgLen = (int32_t)htonl((uint32_t)msgLen); + pHead->release = REQUEST_RELEASE_HANDLE(pCliMsg) ? 1 : 0; + memcpy(pHead->user, pTransInst->user, strlen(pTransInst->user)); + pHead->traceId = pMsg->info.traceId; + pHead->magicNum = htonl(TRANS_MAGIC_NUM); + pHead->version = TRANS_VER; + pHead->compatibilityVer = htonl(pTransInst->compatibilityVer); + } + pHead->timestamp = taosHton64(taosGetTimestampUs()); + + if (pHead->comp == 0) { + if (pTransInst->compressSize != -1 && pTransInst->compressSize < pMsg->contLen) { + msgLen = transCompressMsg(pMsg->pCont, pMsg->contLen) + sizeof(STransMsgHead); + pHead->msgLen = (int32_t)htonl((uint32_t)msgLen); + } + } else { + msgLen = (int32_t)ntohl((uint32_t)(pHead->msgLen)); + } + wb[i++] = uv_buf_init((char*)pHead, msgLen); + totalLen += msgLen; + } + uv_write_t* req = taosMemoryCalloc(1, sizeof(uv_write_t)); + req->data = pConn; + tDebug("%s conn %p start to send batch msg, batch size:%d, len:%d", CONN_GET_INST_LABEL(pConn), pConn, size, + totalLen); + uv_write(req, (uv_stream_t*)pConn->stream, wb, size, cliSendBatchCb); + taosMemoryFree(wb); +} void cliSendBatch(SCliConn* pConn) { SCliThrd* pThrd = pConn->hostThrd; STrans* pTransInst = pThrd->pTransInst; @@ -1399,10 +1480,13 @@ void cliConnCb(uv_connect_t* req, int status) { tTrace("%s conn %p connect to server successfully", CONN_GET_INST_LABEL(pConn), pConn); if (pConn->pBatch != NULL) { - cliSendBatch(pConn); - } else { - cliSend(pConn); + return cliSendBatch(pConn); } + if (pConn->inHeap) { + return cliSendBatch_shareConn(pConn); + } + + return cliSend(pConn); } static void doNotifyApp(SCliMsg* pMsg, SCliThrd* pThrd, int32_t code) { @@ -1596,9 +1680,68 @@ static void doFreeTimeoutMsg(void* param) { doNotifyApp(pMsg, pThrd, code); taosMemoryFree(arg); } + +static SCliConn* getConnFromHeapCache(SHashObj* pConnHeapCache, char* key) { + size_t klen = strlen(key); + SHeap* p = taosHashGet(pConnHeapCache, key, klen); + if (p == NULL) { + SHeap heap = {0}; + transHeapCreate(&heap, compareHeapNode); + taosHashPut(pConnHeapCache, key, klen, &heap, sizeof(heap)); + + p = taosHashGet(pConnHeapCache, key, strlen(key)); + return NULL; + } + SCliConn* pConn = NULL; + transHeapGet(p, &pConn); + return pConn; +} +static void addConnToHeapCache(SHashObj* pConnHeapCacahe, char* key, SCliConn* pConn) { + size_t klen = strlen(key); + SHeap* p = taosHashGet(pConnHeapCacahe, key, klen); + if (p == NULL) { + SHeap heap = {0}; + transHeapCreate(&heap, compareHeapNode); + taosHashPut(pConnHeapCacahe, key, klen, &heap, sizeof(heap)); + p = taosHashGet(pConnHeapCacahe, key, klen); + } + transHeapInsert(p, pConn); +} +void cliHandleReq__shareConn(SCliMsg* pMsg, SCliThrd* pThrd) { + STraceId* trace = &pMsg->msg.info.traceId; + + STrans* pTransInst = pThrd->pTransInst; + + cliMayCvtFqdnToIp(&pMsg->ctx->epSet, &pThrd->cvtAddr); + if (!EPSET_IS_VALID(&pMsg->ctx->epSet)) { + destroyCmsg(pMsg); + return; + } + char addr[TSDB_FQDN_LEN + 64] = {0}; + CONN_CONSTRUCT_HASH_KEY(addr, EPSET_GET_INUSE_IP(&pMsg->ctx->epSet), EPSET_GET_INUSE_PORT(&pMsg->ctx->epSet)); + + SCliConn* pConn = getConnFromHeapCache(pThrd->connHeapCache, addr); + if (pConn == NULL) { + tGTrace("%s conn %p get from heap cache", CONN_GET_INST_LABEL(pConn), pConn); + bool ignore = false; + pConn = getConnFromPool(pThrd, addr, &ignore); + if (pConn != NULL) { + return cliSendBatch_shareConn(pConn); + } + } + + pConn = cliCreateConn(pThrd); + addConnToHeapCache(pThrd->connHeapCache, addr, pConn); + + return cliDoConn(pThrd, pConn, EPSET_GET_INUSE_IP(&pMsg->ctx->epSet), EPSET_GET_INUSE_PORT(&pMsg->ctx->epSet)); +} void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd) { STrans* pTransInst = pThrd->pTransInst; + if (pTransInst->shareConn == 1) { + return cliHandleReq__shareConn(pMsg, pThrd); + } + cliMayCvtFqdnToIp(&pMsg->ctx->epSet, &pThrd->cvtAddr); if (!EPSET_IS_VALID(&pMsg->ctx->epSet)) { destroyCmsg(pMsg); @@ -2083,6 +2226,7 @@ static SCliThrd* createThrdObj(void* trans) { pThrd->failFastCache = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); pThrd->batchCache = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); + pThrd->connHeapCache = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); pThrd->quit = false; @@ -2131,6 +2275,15 @@ static void destroyThrdObj(SCliThrd* pThrd) { pIter = (void**)taosHashIterate(pThrd->batchCache, pIter); } taosHashCleanup(pThrd->batchCache); + + void** pIter2 = taosHashIterate(pThrd->connHeapCache, NULL); + while (pIter2 != NULL) { + SHeap* heap = (SHeap*)(*pIter2); + transHeapDestroy(heap); + pIter2 = (void**)taosHashIterate(pThrd->connHeapCache, pIter2); + } + taosHashCleanup(pThrd->connHeapCache); + taosMemoryFree(pThrd); } @@ -2800,3 +2953,53 @@ int64_t transAllocHandle() { return exh->refId; } + +int32_t compareHeapNode(const HeapNode* a, const HeapNode* b) { + SCliConn* args1 = container_of(a, SCliConn, node); + SCliConn* args2 = container_of(b, SCliConn, node); + if (transQueueSize(&args1->cliMsgs) > transQueueSize(&args2->cliMsgs)) { + return 0; + } + return 1; +} +int transHeapCreate(SHeap* heap, int32_t (*cmpFunc)(const HeapNode* a, const HeapNode* b)) { + heap->heap = heapCreate(cmpFunc); + heap->cmpFunc = cmpFunc; + return 0; +} +void transHeapDestroy(SHeap* heap) { + if (heap != NULL) { + heapDestroy(heap->heap); + } +} +void transHeapGet(SHeap* heap, SCliConn** p) { + if (heapSize(heap->heap) == 0) { + *p = NULL; + return; + } + // HeapNode* minNode = headMin(heap->heap); + HeapNode* minNode = heapMin(heap->heap); + if (minNode == NULL) { + *p = NULL; + return; + } + *p = container_of(minNode, SCliConn, node); +} +int transHeapInsert(SHeap* heap, SCliConn* p) { + // impl later + if (p->inHeap == 1) { + return -1; + } + + heapInsert(heap->heap, &p->node); + p->inHeap = 1; + return 0; +} +int transHeapDelete(SHeap* heap, SCliConn* p) { + // impl later + if (p->inHeap == 0) { + return -1; + } + heapRemove(heap->heap, &p->node); + return 0; +} \ No newline at end of file From 5c191e790f41461e16fa471ff37ddc2743190255 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Thu, 30 May 2024 02:09:11 +0000 Subject: [PATCH 12/22] opt transport --- source/libs/transport/src/transCli.c | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index e59065ef26..a065b5dc2a 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -1076,6 +1076,19 @@ static void cliSendCb(uv_write_t* req, int status) { uv_read_start((uv_stream_t*)pConn->stream, cliAllocRecvBufferCb, cliRecvCb); } +static void cliSendBatch_shareConnCb(uv_write_t* req, int status) { + SCliConn* conn = req->data; + SCliThrd* thrd = conn->hostThrd; + if (status != 0) { + tDebug("%s conn %p failed to send batch msg, reason:%s", CONN_GET_INST_LABEL(conn), conn, uv_err_name(status)); + if (!uv_is_closing((uv_handle_t*)&conn->stream)) { + cliHandleExcept(conn); + } + return; + } + uv_read_start((uv_stream_t*)conn->stream, cliAllocRecvBufferCb, cliRecvCb); + taosMemoryFree(req); +} void cliSendBatch_shareConn(SCliConn* pConn) { SCliThrd* pThrd = pConn->hostThrd; STrans* pTransInst = pThrd->pTransInst; @@ -1132,7 +1145,7 @@ void cliSendBatch_shareConn(SCliConn* pConn) { req->data = pConn; tDebug("%s conn %p start to send batch msg, batch size:%d, len:%d", CONN_GET_INST_LABEL(pConn), pConn, size, totalLen); - uv_write(req, (uv_stream_t*)pConn->stream, wb, size, cliSendBatchCb); + uv_write(req, (uv_stream_t*)pConn->stream, wb, size, cliSendBatch_shareConnCb); taosMemoryFree(wb); } void cliSendBatch(SCliConn* pConn) { From 3cf063e1357e266be7f98ba4d09534ae10d7522c Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Wed, 12 Jun 2024 01:40:26 +0000 Subject: [PATCH 13/22] fix merge error --- source/libs/transport/src/transCli.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index a065b5dc2a..d075ace7d5 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -247,7 +247,7 @@ typedef struct { int32_t compareHeapNode(const HeapNode* a, const HeapNode* b); int transHeapCreate(SHeap* heap, int32_t (*cmpFunc)(const HeapNode* a, const HeapNode* b)); void transHeapDestroy(SHeap* heap); -void transHeapGet(SHeap* heap, SCliConn** p); +int transHeapGet(SHeap* heap, SCliConn** p); int transHeapInsert(SHeap* heap, SCliConn* p); int transHeapDelete(SHeap* heap, SCliConn* p); @@ -1701,8 +1701,6 @@ static SCliConn* getConnFromHeapCache(SHashObj* pConnHeapCache, char* key) { SHeap heap = {0}; transHeapCreate(&heap, compareHeapNode); taosHashPut(pConnHeapCache, key, klen, &heap, sizeof(heap)); - - p = taosHashGet(pConnHeapCache, key, strlen(key)); return NULL; } SCliConn* pConn = NULL; @@ -2985,18 +2983,19 @@ void transHeapDestroy(SHeap* heap) { heapDestroy(heap->heap); } } -void transHeapGet(SHeap* heap, SCliConn** p) { +int transHeapGet(SHeap* heap, SCliConn** p) { if (heapSize(heap->heap) == 0) { *p = NULL; - return; + return -1; } // HeapNode* minNode = headMin(heap->heap); HeapNode* minNode = heapMin(heap->heap); if (minNode == NULL) { *p = NULL; - return; + return -1; } *p = container_of(minNode, SCliConn, node); + return 0; } int transHeapInsert(SHeap* heap, SCliConn* p) { // impl later From e9c358c9d4e45868fc2ee8709876d475c701b73d Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Fri, 14 Jun 2024 12:57:28 +0000 Subject: [PATCH 14/22] refactor transport --- source/libs/transport/inc/transComm.h | 1 + source/libs/transport/src/transCli.c | 167 +++++++++++++++++++------- source/libs/transport/src/transComm.c | 4 - source/libs/transport/src/transSvr.c | 111 +++++++++-------- 4 files changed, 181 insertions(+), 102 deletions(-) diff --git a/source/libs/transport/inc/transComm.h b/source/libs/transport/inc/transComm.h index da6d71e07b..9119ae083c 100644 --- a/source/libs/transport/inc/transComm.h +++ b/source/libs/transport/inc/transComm.h @@ -187,6 +187,7 @@ typedef struct { uint32_t code; // del later uint32_t msgType; int32_t msgLen; + int32_t seqNum; uint8_t content[0]; // message body starts from here } STransMsgHead; diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index d075ace7d5..8ec54167af 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -84,6 +84,7 @@ typedef struct SCliConn { char dst[32]; int64_t refId; + int32_t seq; } SCliConn; typedef struct SCliMsg { @@ -96,6 +97,7 @@ typedef struct SCliMsg { uint64_t st; int sent; //(0: no send, 1: alread sent) queue seqq; + int32_t seqNum; } SCliMsg; typedef struct SCliThrd { @@ -402,6 +404,7 @@ void cliResetConnTimer(SCliConn* conn) { } } void cliHandleBatchResp(SCliConn* conn) { + ASSERT(0); SCliThrd* pThrd = conn->hostThrd; STrans* pTransInst = pThrd->pTransInst; cliResetConnTimer(conn); @@ -430,9 +433,70 @@ void cliHandleBatchResp(SCliConn* conn) { return; } } + +SCliMsg* cliFindMsgBySeqnum(SCliConn* conn, int32_t seqNum) { + SCliMsg* pMsg = NULL; + for (int i = 0; i < transQueueSize(&conn->cliMsgs); i++) { + pMsg = transQueueGet(&conn->cliMsgs, i); + if (pMsg->seqNum == seqNum) { + transQueueRm(&conn->cliMsgs, i); + break; + } + } + if (pMsg == NULL) { + ASSERT(0); + } + return pMsg; +} +void cliHandleResp_shareConn(SCliConn* conn) { + SCliThrd* pThrd = conn->hostThrd; + STrans* pTransInst = pThrd->pTransInst; + cliResetConnTimer(conn); + + STransMsgHead* pHead = NULL; + int32_t msgLen = transDumpFromBuffer(&conn->readBuf, (char**)&pHead); + + if (msgLen <= 0) { + taosMemoryFree(pHead); + tDebug("%s conn %p recv invalid packet ", CONN_GET_INST_LABEL(conn), conn); + return; + } + if (transDecompressMsg((char**)&pHead, msgLen) < 0) { + tDebug("%s conn %p recv invalid packet, failed to decompress", CONN_GET_INST_LABEL(conn), conn); + } + pHead->code = htonl(pHead->code); + pHead->msgLen = htonl(pHead->msgLen); + + STransMsg transMsg = {0}; + transMsg.contLen = transContLenFromMsg(pHead->msgLen); + transMsg.pCont = transContFromHead((char*)pHead); + transMsg.code = pHead->code; + transMsg.msgType = pHead->msgType; + transMsg.info.ahandle = NULL; + transMsg.info.traceId = pHead->traceId; + transMsg.info.hasEpSet = pHead->hasEpSet; + transMsg.info.cliVer = htonl(pHead->compatibilityVer); + + SCliMsg* pMsg = cliFindMsgBySeqnum(conn, pHead->seqNum); + pMsg->seqNum = 0; + + STransConnCtx* pCtx = pMsg->ctx; + transMsg.info.ahandle = pCtx ? pCtx->ahandle : NULL; + STraceId* trace = &transMsg.info.traceId; + + if (cliAppCb(conn, &transMsg, pMsg) != 0) { + return; + } + + return; +} void cliHandleResp(SCliConn* conn) { SCliThrd* pThrd = conn->hostThrd; STrans* pTransInst = pThrd->pTransInst; + + if (pTransInst->shareConn) { + return cliHandleResp_shareConn(conn); + } cliResetConnTimer(conn); STransMsgHead* pHead = NULL; @@ -716,6 +780,7 @@ static SCliConn* getConnFromPool(SCliThrd* pThrd, char* key, bool* exceed) { transDQCancel(((SCliThrd*)conn->hostThrd)->timeoutQueue, conn->task); conn->task = NULL; } + conn->seq++; return conn; } @@ -813,6 +878,7 @@ static void addConnToPool(void* pool, SCliConn* conn) { } cliDestroyConnMsgs(conn, false); + conn->seq = 0; if (conn->list == NULL) { conn->list = taosHashGet((SHashObj*)pool, conn->dstAddr, strlen(conn->dstAddr)); @@ -871,6 +937,7 @@ static int32_t allocConnRef(SCliConn* conn, bool update) { } static int32_t specifyConnRef(SCliConn* conn, bool update, int64_t handle) { + if (handle == 0) return -1; if (update) { transReleaseExHandle(transGetRefMgt(), conn->refId); transRemoveExHandle(transGetRefMgt(), conn->refId); @@ -958,7 +1025,7 @@ static SCliConn* cliCreateConn(SCliThrd* pThrd) { conn->status = ConnNormal; conn->broken = false; transRefCliHandle(conn); - + conn->seq = 0; allocConnRef(conn, false); return conn; @@ -1078,7 +1145,6 @@ static void cliSendCb(uv_write_t* req, int status) { static void cliSendBatch_shareConnCb(uv_write_t* req, int status) { SCliConn* conn = req->data; - SCliThrd* thrd = conn->hostThrd; if (status != 0) { tDebug("%s conn %p failed to send batch msg, reason:%s", CONN_GET_INST_LABEL(conn), conn, uv_err_name(status)); if (!uv_is_closing((uv_handle_t*)&conn->stream)) { @@ -1093,7 +1159,8 @@ void cliSendBatch_shareConn(SCliConn* pConn) { SCliThrd* pThrd = pConn->hostThrd; STrans* pTransInst = pThrd->pTransInst; int32_t size = transQueueSize(&pConn->cliMsgs); - int32_t totalLen = 0; + + int32_t totalLen = 0; if (size == 0) { tError("%s conn %p not msg to send", pTransInst->label, pConn); cliHandleExcept(pConn); @@ -1101,10 +1168,14 @@ void cliSendBatch_shareConn(SCliConn* pConn) { } uv_buf_t* wb = taosMemoryCalloc(size, sizeof(uv_buf_t)); + int j = 0; for (int i = 0; i < size; i++) { SCliMsg* pCliMsg = transQueueGet(&pConn->cliMsgs, i); - + if (pCliMsg->sent == 1) { + continue; + } STransConnCtx* pCtx = pCliMsg->ctx; + pConn->seq++; STransMsg* pMsg = (STransMsg*)(&pCliMsg->msg); if (pMsg->pCont == 0) { @@ -1112,7 +1183,8 @@ void cliSendBatch_shareConn(SCliConn* pConn) { pMsg->contLen = 0; } - int msgLen = transMsgLenFromCont(pMsg->contLen); + int msgLen = transMsgLenFromCont(pMsg->contLen); + STransMsgHead* pHead = transHeadFromCont(pMsg->pCont); if (pHead->comp == 0) { @@ -1129,6 +1201,7 @@ void cliSendBatch_shareConn(SCliConn* pConn) { pHead->compatibilityVer = htonl(pTransInst->compatibilityVer); } pHead->timestamp = taosHton64(taosGetTimestampUs()); + pHead->seqNum = pConn->seq; if (pHead->comp == 0) { if (pTransInst->compressSize != -1 && pTransInst->compressSize < pMsg->contLen) { @@ -1138,14 +1211,17 @@ void cliSendBatch_shareConn(SCliConn* pConn) { } else { msgLen = (int32_t)ntohl((uint32_t)(pHead->msgLen)); } - wb[i++] = uv_buf_init((char*)pHead, msgLen); + wb[j++] = uv_buf_init((char*)pHead, msgLen); totalLen += msgLen; + + pCliMsg->sent = 1; + pCliMsg->seqNum = pHead->seqNum; } uv_write_t* req = taosMemoryCalloc(1, sizeof(uv_write_t)); req->data = pConn; tDebug("%s conn %p start to send batch msg, batch size:%d, len:%d", CONN_GET_INST_LABEL(pConn), pConn, size, totalLen); - uv_write(req, (uv_stream_t*)pConn->stream, wb, size, cliSendBatch_shareConnCb); + uv_write(req, (uv_stream_t*)pConn->stream, wb, j, cliSendBatch_shareConnCb); taosMemoryFree(wb); } void cliSendBatch(SCliConn* pConn) { @@ -1422,41 +1498,30 @@ static void cliSendBatchCb(uv_write_t* req, int status) { cliDestroyBatch(p); taosMemoryFree(req); } -static void cliHandleFastFail(SCliConn* pConn, int status) { + +static void cliHandleFastFail_resp(SCliConn* pConn, int status) { SCliThrd* pThrd = pConn->hostThrd; STrans* pTransInst = pThrd->pTransInst; + SCliMsg* pMsg = transQueueGet(&pConn->cliMsgs, 0); + STraceId* trace = &pMsg->msg.info.traceId; + tGError("%s msg %s failed to send, conn %p failed to connect to %s, reason: %s", CONN_GET_INST_LABEL(pConn), + TMSG_INFO(pMsg->msg.msgType), pConn, pConn->dstAddr, uv_strerror(status)); +} + +static void cliHandleFastFail_noresp(SCliConn* pConn, int status) { + tError("%s batch msg failed to send, conn %p failed to connect to %s, reason: %s", CONN_GET_INST_LABEL(pConn), pConn, + pConn->dstAddr, uv_strerror(status)); + cliDestroyBatch(pConn->pBatch); + pConn->pBatch = NULL; +} +static void cliHandleFastFail(SCliConn* pConn, int status) { if (status == -1) status = UV_EADDRNOTAVAIL; if (pConn->pBatch == NULL) { - SCliMsg* pMsg = transQueueGet(&pConn->cliMsgs, 0); - - STraceId* trace = &pMsg->msg.info.traceId; - tGError("%s msg %s failed to send, conn %p failed to connect to %s, reason: %s", CONN_GET_INST_LABEL(pConn), - TMSG_INFO(pMsg->msg.msgType), pConn, pConn->dstAddr, uv_strerror(status)); - - if (pMsg != NULL && REQUEST_NO_RESP(&pMsg->msg) && - (pTransInst->failFastFp != NULL && pTransInst->failFastFp(pMsg->msg.msgType))) { - SFailFastItem* item = taosHashGet(pThrd->failFastCache, pConn->dstAddr, strlen(pConn->dstAddr)); - int64_t cTimestamp = taosGetTimestampMs(); - if (item != NULL) { - int32_t elapse = cTimestamp - item->timestamp; - if (elapse >= 0 && elapse <= pTransInst->failFastInterval) { - item->count++; - } else { - item->count = 1; - item->timestamp = cTimestamp; - } - } else { - SFailFastItem item = {.count = 1, .timestamp = cTimestamp}; - taosHashPut(pThrd->failFastCache, pConn->dstAddr, strlen(pConn->dstAddr), &item, sizeof(SFailFastItem)); - } - } + cliHandleFastFail_resp(pConn, status); } else { - tError("%s batch msg failed to send, conn %p failed to connect to %s, reason: %s", CONN_GET_INST_LABEL(pConn), - pConn, pConn->dstAddr, uv_strerror(status)); - cliDestroyBatch(pConn->pBatch); - pConn->pBatch = NULL; + cliHandleFastFail_noresp(pConn, status); } cliHandleExcept(pConn); } @@ -1616,9 +1681,8 @@ FORCE_INLINE void cliMayCvtFqdnToIp(SEpSet* pEpSet, SCvtAddr* pCvtAddr) { FORCE_INLINE bool cliIsEpsetUpdated(int32_t code, STransConnCtx* pCtx) { if (code != 0) return false; - // if (pCtx->retryCnt == 0) return false; - if (transEpSetIsEqual(&pCtx->epSet, &pCtx->origEpSet)) return false; - return true; + + return transEpSetIsEqual(&pCtx->epSet, &pCtx->origEpSet) ? false : true; } FORCE_INLINE int32_t cliBuildExceptResp(SCliMsg* pMsg, STransMsg* pResp) { if (pMsg == NULL) return -1; @@ -1686,11 +1750,12 @@ static void doFreeTimeoutMsg(void* param) { SCliMsg* pMsg = arg->param1; SCliThrd* pThrd = arg->param2; STrans* pTransInst = pThrd->pTransInst; - int32_t code = TSDB_CODE_RPC_MAX_SESSIONS; + QUEUE_REMOVE(&pMsg->q); STraceId* trace = &pMsg->msg.info.traceId; tGTrace("%s msg %s cannot get available conn after timeout", pTransInst->label, TMSG_INFO(pMsg->msg.msgType)); - doNotifyApp(pMsg, pThrd, code); + doNotifyApp(pMsg, pThrd, TSDB_CODE_RPC_MAX_SESSIONS); + taosMemoryFree(arg); } @@ -1718,6 +1783,19 @@ static void addConnToHeapCache(SHashObj* pConnHeapCacahe, char* key, SCliConn* p } transHeapInsert(p, pConn); } +static void delConnFromHeapCache(SHashObj* pConnHeapCache, char* key, SCliConn* pConn) { + size_t klen = strlen(key); + + SHeap* p = taosHashGet(pConnHeapCache, key, klen); + if (p == NULL) { + tDebug("failed to get heap cache for key:%s, no need to del", key); + return; + } + int ret = transHeapDelete(p, pConn); + if (ret != 0) { + tDebug("failed to delete conn %p from heap cache, no need to del", pConn); + } +} void cliHandleReq__shareConn(SCliMsg* pMsg, SCliThrd* pThrd) { STraceId* trace = &pMsg->msg.info.traceId; @@ -1742,6 +1820,7 @@ void cliHandleReq__shareConn(SCliMsg* pMsg, SCliThrd* pThrd) { } pConn = cliCreateConn(pThrd); + pConn->dstAddr = taosStrdup(addr); addConnToHeapCache(pThrd->connHeapCache, addr, pConn); return cliDoConn(pThrd, pConn, EPSET_GET_INUSE_IP(&pMsg->ctx->epSet), EPSET_GET_INUSE_PORT(&pMsg->ctx->epSet)); @@ -1790,15 +1869,13 @@ void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd) { cliSend(conn); } else { conn = cliCreateConn(pThrd); + conn->dstAddr = taosStrdup(addr); - int64_t refId = (int64_t)pMsg->msg.info.handle; - if (refId != 0) specifyConnRef(conn, true, refId); + specifyConnRef(conn, true, (int64_t)pMsg->msg.info.handle); transCtxMerge(&conn->ctx, &pMsg->ctx->appCtx); transQueuePush(&conn->cliMsgs, pMsg); - conn->dstAddr = taosStrdup(addr); - uint32_t ipaddr = cliGetIpFromFqdnCache(pThrd->fqdn2ipCache, fqdn); if (ipaddr == 0xffffffff) { cliResetConnTimer(conn); @@ -2234,7 +2311,6 @@ static SCliThrd* createThrdObj(void* trans) { pThrd->destroyAhandleFp = pTransInst->destroyFp; pThrd->fqdn2ipCache = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); - pThrd->failFastCache = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); pThrd->batchCache = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); pThrd->connHeapCache = taosHashInit(8, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); @@ -2267,7 +2343,6 @@ static void destroyThrdObj(SCliThrd* pThrd) { taosMemoryFree(pThrd->prepare); taosMemoryFree(pThrd->loop); taosHashCleanup(pThrd->fqdn2ipCache); - taosHashCleanup(pThrd->failFastCache); void** pIter = taosHashIterate(pThrd->batchCache, NULL); while (pIter != NULL) { diff --git a/source/libs/transport/src/transComm.c b/source/libs/transport/src/transComm.c index fff13e7ebb..edc2d2b899 100644 --- a/source/libs/transport/src/transComm.c +++ b/source/libs/transport/src/transComm.c @@ -326,10 +326,6 @@ void transCtxMerge(STransCtx* dst, STransCtx* src) { STransCtxVal* sVal = (STransCtxVal*)iter; key = taosHashGetKey(sVal, &klen); - // STransCtxVal* dVal = taosHashGet(dst->args, key, klen); - // if (dVal) { - // dst->freeFunc(dVal->val); - // } taosHashPut(dst->args, key, klen, sVal, sizeof(*sVal)); iter = taosHashIterate(src->args, iter); } diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index d47968eeb8..42189de5d4 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -336,6 +336,59 @@ void uvWhiteListSetConnVer(SIpWhiteListTab* pWhite, SSvrConn* pConn) { pConn->whiteListVer = pWhite->ver; } +static void uvPerfLog(SSvrConn* pConn, STransMsgHead* pHead, STransMsg* pTransMsg) { + STrans* pTransInst = pConn->pTransInst; + STraceId* trace = &pHead->traceId; + + int64_t cost = taosGetTimestampUs() - taosNtoh64(pHead->timestamp); + static int64_t EXCEPTION_LIMIT_US = 100 * 1000; + + if (pConn->status == ConnNormal && pHead->noResp == 0) { + transRefSrvHandle(pConn); + if (cost >= EXCEPTION_LIMIT_US) { + tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, cost:%dus, recv exception", + transLabel(pTransInst), pConn, TMSG_INFO(pTransMsg->msgType), pConn->dst, pConn->src, pTransMsg->contLen, + (int)cost); + } else { + tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, cost:%dus", transLabel(pTransInst), pConn, + TMSG_INFO(pTransMsg->msgType), pConn->dst, pConn->src, pTransMsg->contLen, (int)cost); + } + } else { + if (cost >= EXCEPTION_LIMIT_US) { + tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, noResp:%d, code:%d, cost:%dus, recv exception", + transLabel(pTransInst), pConn, TMSG_INFO(pTransMsg->msgType), pConn->dst, pConn->src, pTransMsg->contLen, + pHead->noResp, pTransMsg->code, (int)(cost)); + } else { + tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, noResp:%d, code:%d, cost:%dus", + transLabel(pTransInst), pConn, TMSG_INFO(pTransMsg->msgType), pConn->dst, pConn->src, pTransMsg->contLen, + pHead->noResp, pTransMsg->code, (int)(cost)); + } + } + tGTrace("%s handle %p conn:%p translated to app, refId:%" PRIu64, transLabel(pTransInst), pTransMsg->info.handle, + pConn, pConn->refId); +} + +static int8_t uvValidConn(SSvrConn* pConn) { + STrans* pTransInst = pConn->pTransInst; + SWorkThrd* pThrd = pConn->hostThrd; + int8_t forbiddenIp = 0; + if (pThrd->enableIpWhiteList) { + forbiddenIp = !uvWhiteListCheckConn(pThrd->pWhiteList, pConn) ? 1 : 0; + if (forbiddenIp == 0) { + uvWhiteListSetConnVer(pThrd->pWhiteList, pConn); + } + } + return forbiddenIp; +} +static void uvMaySetConnAcquired(SSvrConn* pConn, STransMsgHead* pHead) { + if (pConn->status == ConnNormal) { + if (pHead->persist == 1) { + pConn->status = ConnAcquire; + transRefSrvHandle(pConn); + tDebug("conn %p acquired by server app", pConn); + } + } +} static bool uvHandleReq(SSvrConn* pConn) { STrans* pTransInst = pConn->pTransInst; SWorkThrd* pThrd = pConn->hostThrd; @@ -358,14 +411,6 @@ static bool uvHandleReq(SSvrConn* pConn) { pConn->inType = pHead->msgType; memcpy(pConn->user, pHead->user, strlen(pHead->user)); - int8_t forbiddenIp = 0; - if (pThrd->enableIpWhiteList) { - forbiddenIp = !uvWhiteListCheckConn(pThrd->pWhiteList, pConn) ? 1 : 0; - if (forbiddenIp == 0) { - uvWhiteListSetConnVer(pThrd->pWhiteList, pConn); - } - } - if (uvRecvReleaseReq(pConn, pHead)) { return true; } @@ -384,38 +429,7 @@ static bool uvHandleReq(SSvrConn* pConn) { transMsg.msgType = pHead->msgType; transMsg.code = pHead->code; - if (pConn->status == ConnNormal) { - if (pHead->persist == 1) { - pConn->status = ConnAcquire; - transRefSrvHandle(pConn); - tDebug("conn %p acquired by server app", pConn); - } - } - STraceId* trace = &pHead->traceId; - - int64_t cost = taosGetTimestampUs() - taosNtoh64(pHead->timestamp); - static int64_t EXCEPTION_LIMIT_US = 100 * 1000; - - if (pConn->status == ConnNormal && pHead->noResp == 0) { - transRefSrvHandle(pConn); - if (cost >= EXCEPTION_LIMIT_US) { - tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, cost:%dus, recv exception", - transLabel(pTransInst), pConn, TMSG_INFO(transMsg.msgType), pConn->dst, pConn->src, msgLen, (int)cost); - } else { - tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, cost:%dus", transLabel(pTransInst), pConn, - TMSG_INFO(transMsg.msgType), pConn->dst, pConn->src, msgLen, (int)cost); - } - } else { - if (cost >= EXCEPTION_LIMIT_US) { - tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, noResp:%d, code:%d, cost:%dus, recv exception", - transLabel(pTransInst), pConn, TMSG_INFO(transMsg.msgType), pConn->dst, pConn->src, msgLen, pHead->noResp, - transMsg.code, (int)(cost)); - } else { - tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, noResp:%d, code:%d, cost:%dus", - transLabel(pTransInst), pConn, TMSG_INFO(transMsg.msgType), pConn->dst, pConn->src, msgLen, pHead->noResp, - transMsg.code, (int)(cost)); - } - } + uvMaySetConnAcquired(pConn, pHead); // pHead->noResp = 1, // 1. server application should not send resp on handle @@ -423,21 +437,14 @@ static bool uvHandleReq(SSvrConn* pConn) { // 3. not mixed with persist transMsg.info.ahandle = (void*)pHead->ahandle; transMsg.info.handle = (void*)transAcquireExHandle(transGetRefMgt(), pConn->refId); - transMsg.info.refId = pConn->refId; + ASSERTS(transMsg.info.handle != NULL, "trans-svr failed to alloc handle to msg"); + + transMsg.info.refId = pHead->noResp == 1 ? -1 : pConn->refId; transMsg.info.traceId = pHead->traceId; transMsg.info.cliVer = htonl(pHead->compatibilityVer); - transMsg.info.forbiddenIp = forbiddenIp; + transMsg.info.forbiddenIp = uvValidConn(pConn); - tGTrace("%s handle %p conn:%p translated to app, refId:%" PRIu64, transLabel(pTransInst), transMsg.info.handle, pConn, - pConn->refId); - ASSERTS(transMsg.info.handle != NULL, "trans-svr failed to alloc handle to msg"); - if (transMsg.info.handle == NULL) { - return false; - } - - if (pHead->noResp == 1) { - transMsg.info.refId = -1; - } + uvPerfLog(pConn, pHead, &transMsg); // set up conn info SRpcConnInfo* pConnInfo = &(transMsg.info.conn); From 1ae87fe097c16cd3bf2824d33b3f3ce2688bfe3e Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Fri, 14 Jun 2024 13:24:58 +0000 Subject: [PATCH 15/22] refactor transport --- source/libs/transport/src/transCli.c | 4 ++-- source/libs/transport/src/transSvr.c | 16 +++++++++++----- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 8ec54167af..d625b30752 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -2423,7 +2423,7 @@ static void doCloseIdleConn(void* param) { cliDestroyConn(conn, true); taosMemoryFree(arg); } -static void cliSchedMsgToDebug(SCliMsg* pMsg, char* label) { +static void cliPerfLog_schedMsg(SCliMsg* pMsg, char* label) { if (!(rpcDebugFlag & DEBUG_DEBUG)) { return; } @@ -2439,7 +2439,7 @@ static void cliSchedMsgToDebug(SCliMsg* pMsg, char* label) { static void cliSchedMsgToNextNode(SCliMsg* pMsg, SCliThrd* pThrd) { STrans* pTransInst = pThrd->pTransInst; STransConnCtx* pCtx = pMsg->ctx; - cliSchedMsgToDebug(pMsg, transLabel(pThrd->pTransInst)); + cliPerfLog_schedMsg(pMsg, transLabel(pThrd->pTransInst)); STaskArg* arg = taosMemoryMalloc(sizeof(STaskArg)); arg->param1 = pMsg; diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index 42189de5d4..57ea89b82a 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -336,7 +336,11 @@ void uvWhiteListSetConnVer(SIpWhiteListTab* pWhite, SSvrConn* pConn) { pConn->whiteListVer = pWhite->ver; } -static void uvPerfLog(SSvrConn* pConn, STransMsgHead* pHead, STransMsg* pTransMsg) { +static void uvPerfLog_receive(SSvrConn* pConn, STransMsgHead* pHead, STransMsg* pTransMsg) { + if (!(rpcDebugFlag & DEBUG_DEBUG)) { + return; + } + STrans* pTransInst = pConn->pTransInst; STraceId* trace = &pHead->traceId; @@ -344,7 +348,7 @@ static void uvPerfLog(SSvrConn* pConn, STransMsgHead* pHead, STransMsg* pTransMs static int64_t EXCEPTION_LIMIT_US = 100 * 1000; if (pConn->status == ConnNormal && pHead->noResp == 0) { - transRefSrvHandle(pConn); + // transRefSrvHandle(pConn); if (cost >= EXCEPTION_LIMIT_US) { tGDebug("%s conn %p %s received from %s, local info:%s, len:%d, cost:%dus, recv exception", transLabel(pTransInst), pConn, TMSG_INFO(pTransMsg->msgType), pConn->dst, pConn->src, pTransMsg->contLen, @@ -386,6 +390,8 @@ static void uvMaySetConnAcquired(SSvrConn* pConn, STransMsgHead* pHead) { pConn->status = ConnAcquire; transRefSrvHandle(pConn); tDebug("conn %p acquired by server app", pConn); + } else if (pHead->noResp == 0) { + transRefSrvHandle(pConn); } } } @@ -429,8 +435,6 @@ static bool uvHandleReq(SSvrConn* pConn) { transMsg.msgType = pHead->msgType; transMsg.code = pHead->code; - uvMaySetConnAcquired(pConn, pHead); - // pHead->noResp = 1, // 1. server application should not send resp on handle // 2. once send out data, cli conn released to conn pool immediately @@ -444,7 +448,9 @@ static bool uvHandleReq(SSvrConn* pConn) { transMsg.info.cliVer = htonl(pHead->compatibilityVer); transMsg.info.forbiddenIp = uvValidConn(pConn); - uvPerfLog(pConn, pHead, &transMsg); + uvMaySetConnAcquired(pConn, pHead); + + uvPerfLog_receive(pConn, pHead, &transMsg); // set up conn info SRpcConnInfo* pConnInfo = &(transMsg.info.conn); From cdaa4fa47e295b50b6a78d1edc953b8dc6357b36 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Wed, 3 Jul 2024 00:57:44 +0000 Subject: [PATCH 16/22] refactor transport --- source/libs/transport/src/transCli.c | 115 ++++++++++++++++++++------- source/util/src/theap.c | 46 ++++------- 2 files changed, 102 insertions(+), 59 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index d625b30752..adb6223a0f 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -247,7 +247,7 @@ typedef struct { } SHeap; int32_t compareHeapNode(const HeapNode* a, const HeapNode* b); -int transHeapCreate(SHeap* heap, int32_t (*cmpFunc)(const HeapNode* a, const HeapNode* b)); +int transHeapInit(SHeap* heap, int32_t (*cmpFunc)(const HeapNode* a, const HeapNode* b)); void transHeapDestroy(SHeap* heap); int transHeapGet(SHeap* heap, SCliConn** p); int transHeapInsert(SHeap* heap, SCliConn* p); @@ -1702,12 +1702,11 @@ static FORCE_INLINE uint32_t cliGetIpFromFqdnCache(SHashObj* cache, char* fqdn) uint32_t* v = taosHashGet(cache, fqdn, len); if (v == NULL) { addr = taosGetIpv4FromFqdn(fqdn); - if (addr == 0xffffffff) { + if (addr == (uint32_t)-1) { terrno = TSDB_CODE_RPC_FQDN_ERROR; tError("failed to get ip from fqdn:%s since %s", fqdn, terrstr()); return addr; } - taosHashPut(cache, fqdn, len, &addr, sizeof(addr)); } else { addr = *v; @@ -1717,7 +1716,7 @@ static FORCE_INLINE uint32_t cliGetIpFromFqdnCache(SHashObj* cache, char* fqdn) static FORCE_INLINE void cliUpdateFqdnCache(SHashObj* cache, char* fqdn) { // impl later uint32_t addr = taosGetIpv4FromFqdn(fqdn); - if (addr != 0xffffffff) { + if (addr != (uint32_t)-1) { size_t len = strlen(fqdn); uint32_t* v = taosHashGet(cache, fqdn, len); if (addr != *v) { @@ -1725,7 +1724,7 @@ static FORCE_INLINE void cliUpdateFqdnCache(SHashObj* cache, char* fqdn) { tinet_ntoa(old, *v); tinet_ntoa(new, addr); tWarn("update ip of fqdn:%s, old: %s, new: %s", fqdn, old, new); - taosHashPut(cache, fqdn, strlen(fqdn) + 1, &addr, sizeof(addr)); + taosHashPut(cache, fqdn, strlen(fqdn), &addr, sizeof(addr)); } } return; @@ -1759,29 +1758,50 @@ static void doFreeTimeoutMsg(void* param) { taosMemoryFree(arg); } -static SCliConn* getConnFromHeapCache(SHashObj* pConnHeapCache, char* key) { +static SHeap* getOrCreateHeapIfNotExist(SHashObj* pConnHeapCache, char* key) { + int ret = 0; size_t klen = strlen(key); + SHeap* p = taosHashGet(pConnHeapCache, key, klen); if (p == NULL) { SHeap heap = {0}; - transHeapCreate(&heap, compareHeapNode); - taosHashPut(pConnHeapCache, key, klen, &heap, sizeof(heap)); + ret = transHeapInit(&heap, compareHeapNode); + if (ret != 0) { + tError("failed to init heap cache for key:%s, reason: %s", key, tstrerror(terrno)); + terrno = 0; + return NULL; + } + + ret = taosHashPut(pConnHeapCache, key, klen, &heap, sizeof(heap)); + if (ret != 0) { + transHeapDestroy(&heap); + terrno = TSDB_CODE_OUT_OF_MEMORY; + tError("failed to put heap to cache for key:%s, reason: %s", key, tstrerror(terrno)); + terrno = 0; + } + p = taosHashGet(pConnHeapCache, key, klen); + return p; + } + return p; +} + +static SCliConn* getConnFromHeapCache(SHashObj* pConnHeapCache, char* key) { + int ret = 0; + SCliConn* pConn = NULL; + SHeap* p = getOrCreateHeapIfNotExist(pConnHeapCache, key); + if (p == NULL) { return NULL; } - SCliConn* pConn = NULL; - transHeapGet(p, &pConn); + ret = transHeapGet(p, &pConn); + return pConn; } -static void addConnToHeapCache(SHashObj* pConnHeapCacahe, char* key, SCliConn* pConn) { - size_t klen = strlen(key); - SHeap* p = taosHashGet(pConnHeapCacahe, key, klen); +static int32_t addConnToHeapCache(SHashObj* pConnHeapCacahe, char* key, SCliConn* pConn) { + SHeap* p = getOrCreateHeapIfNotExist(pConnHeapCacahe, key); if (p == NULL) { - SHeap heap = {0}; - transHeapCreate(&heap, compareHeapNode); - taosHashPut(pConnHeapCacahe, key, klen, &heap, sizeof(heap)); - p = taosHashGet(pConnHeapCacahe, key, klen); + return 0; } - transHeapInsert(p, pConn); + return transHeapInsert(p, pConn); } static void delConnFromHeapCache(SHashObj* pConnHeapCache, char* key, SCliConn* pConn) { size_t klen = strlen(key); @@ -1797,9 +1817,10 @@ static void delConnFromHeapCache(SHashObj* pConnHeapCache, char* key, SCliConn* } } void cliHandleReq__shareConn(SCliMsg* pMsg, SCliThrd* pThrd) { - STraceId* trace = &pMsg->msg.info.traceId; + int32_t code = 0; - STrans* pTransInst = pThrd->pTransInst; + STraceId* trace = &pMsg->msg.info.traceId; + STrans* pTransInst = pThrd->pTransInst; cliMayCvtFqdnToIp(&pMsg->ctx->epSet, &pThrd->cvtAddr); if (!EPSET_IS_VALID(&pMsg->ctx->epSet)) { @@ -1821,7 +1842,12 @@ void cliHandleReq__shareConn(SCliMsg* pMsg, SCliThrd* pThrd) { pConn = cliCreateConn(pThrd); pConn->dstAddr = taosStrdup(addr); - addConnToHeapCache(pThrd->connHeapCache, addr, pConn); + code = addConnToHeapCache(pThrd->connHeapCache, addr, pConn); + if (code != 0) { + // do nothing + } else { + // do nothing + } return cliDoConn(pThrd, pConn, EPSET_GET_INUSE_IP(&pMsg->ctx->epSet), EPSET_GET_INUSE_PORT(&pMsg->ctx->epSet)); } @@ -2858,7 +2884,7 @@ int transSendRequest(void* shandle, const SEpSet* pEpSet, STransMsg* pReq, STran int transSendRecv(void* shandle, const SEpSet* pEpSet, STransMsg* pReq, STransMsg* pRsp) { STrans* pTransInst = (STrans*)transAcquireExHandle(transGetInstMgt(), (int64_t)shandle); STransMsg* pTransRsp = taosMemoryCalloc(1, sizeof(STransMsg)); - if (pTransInst == NULL) { + if (pTransInst == NULL || pTransRsp == NULL) { transFreeMsg(pReq->pCont); taosMemoryFree(pTransRsp); return TSDB_CODE_RPC_BROKEN_LINK; @@ -2873,6 +2899,10 @@ int transSendRecv(void* shandle, const SEpSet* pEpSet, STransMsg* pReq, STransMs } tsem_t* sem = taosMemoryCalloc(1, sizeof(tsem_t)); + if (sem == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } + tsem_init(sem, 0, 0); TRACE_SET_MSGID(&pReq->info.traceId, tGenIdPI64()); @@ -2915,9 +2945,25 @@ _RETURN: } int64_t transCreateSyncMsg(STransMsg* pTransMsg) { tsem_t* sem = taosMemoryCalloc(1, sizeof(tsem_t)); - tsem_init(sem, 0, 0); + if (sem == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + int ret = tsem_init(sem, 0, 0); + if (ret != 0) { + taosMemoryFree(sem); + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } STransSyncMsg* pSyncMsg = taosMemoryCalloc(1, sizeof(STransSyncMsg)); + if (pSyncMsg == NULL) { + tsem_destroy(sem); + taosMemoryFree(sem); + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } taosInitRWLatch(&pSyncMsg->latch); pSyncMsg->inited = 0; @@ -2929,13 +2975,16 @@ int64_t transCreateSyncMsg(STransMsg* pTransMsg) { } int transSendRecvWithTimeout(void* shandle, SEpSet* pEpSet, STransMsg* pReq, STransMsg* pRsp, int8_t* epUpdated, int32_t timeoutMs) { - STrans* pTransInst = (STrans*)transAcquireExHandle(transGetInstMgt(), (int64_t)shandle); - STransMsg* pTransMsg = taosMemoryCalloc(1, sizeof(STransMsg)); + STrans* pTransInst = (STrans*)transAcquireExHandle(transGetInstMgt(), (int64_t)shandle); if (pTransInst == NULL) { transFreeMsg(pReq->pCont); - taosMemoryFree(pTransMsg); return TSDB_CODE_RPC_BROKEN_LINK; } + STransMsg* pTransMsg = taosMemoryCalloc(1, sizeof(STransMsg)); + if (pTransMsg == NULL) { + transFreeMsg(pReq->pCont); + return TSDB_CODE_OUT_OF_MEMORY; + } SCliThrd* pThrd = transGetWorkThrd(pTransInst, (int64_t)pReq->info.handle); if (pThrd == NULL) { @@ -2953,6 +3002,13 @@ int transSendRecvWithTimeout(void* shandle, SEpSet* pEpSet, STransMsg* pReq, STr pCtx->ahandle = pReq->info.ahandle; pCtx->msgType = pReq->msgType; pCtx->syncMsgRef = transCreateSyncMsg(pTransMsg); + if (pCtx->syncMsgRef < 0) { + transFreeMsg(pReq->pCont); + taosMemoryFree(pTransMsg); + taosMemoryFree(pCtx); + transReleaseExHandle(transGetInstMgt(), (int64_t)shandle); + return terrno; + } int64_t ref = pCtx->syncMsgRef; STransSyncMsg* pSyncMsg = taosAcquireRef(transGetSyncMsgMgt(), ref); @@ -3048,8 +3104,12 @@ int32_t compareHeapNode(const HeapNode* a, const HeapNode* b) { } return 1; } -int transHeapCreate(SHeap* heap, int32_t (*cmpFunc)(const HeapNode* a, const HeapNode* b)) { +int transHeapInit(SHeap* heap, int32_t (*cmpFunc)(const HeapNode* a, const HeapNode* b)) { heap->heap = heapCreate(cmpFunc); + if (heap->heap == NULL) { + return -1; + } + heap->cmpFunc = cmpFunc; return 0; } @@ -3063,7 +3123,6 @@ int transHeapGet(SHeap* heap, SCliConn** p) { *p = NULL; return -1; } - // HeapNode* minNode = headMin(heap->heap); HeapNode* minNode = heapMin(heap->heap); if (minNode == NULL) { *p = NULL; diff --git a/source/util/src/theap.c b/source/util/src/theap.c index 315ddf9367..ed117efd62 100644 --- a/source/util/src/theap.c +++ b/source/util/src/theap.c @@ -21,6 +21,7 @@ size_t heapSize(Heap* heap) { return heap->nelts; } Heap* heapCreate(HeapCompareFn fn) { Heap* heap = taosMemoryCalloc(1, sizeof(Heap)); if (heap == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -188,7 +189,6 @@ void heapRemove(Heap* heap, HeapNode* node) { void heapDequeue(Heap* heap) { heapRemove(heap, heap->min); } - struct PriorityQueue { SArray* container; pq_comp_fn fn; @@ -204,9 +204,7 @@ PriorityQueue* createPriorityQueue(pq_comp_fn fn, FDelete deleteFn, void* param) return pq; } -void taosPQSetFn(PriorityQueue* pq, pq_comp_fn fn) { - pq->fn = fn; -} +void taosPQSetFn(PriorityQueue* pq, pq_comp_fn fn) { pq->fn = fn; } void destroyPriorityQueue(PriorityQueue* pq) { if (pq->deleteFn) @@ -218,15 +216,15 @@ void destroyPriorityQueue(PriorityQueue* pq) { static size_t pqParent(size_t i) { return (--i) >> 1; /* (i - 1) / 2 */ } static size_t pqLeft(size_t i) { return (i << 1) | 1; /* i * 2 + 1 */ } -static size_t pqRight(size_t i) { return (++i) << 1; /* (i + 1) * 2 */} -static void pqSwapPQNode(PriorityQueueNode* a, PriorityQueueNode* b) { - void * tmp = a->data; - a->data = b->data; - b->data = tmp; +static size_t pqRight(size_t i) { return (++i) << 1; /* (i + 1) * 2 */ } +static void pqSwapPQNode(PriorityQueueNode* a, PriorityQueueNode* b) { + void* tmp = a->data; + a->data = b->data; + b->data = tmp; } #define pqContainerGetEle(pq, i) ((PriorityQueueNode*)taosArrayGet((pq)->container, (i))) -#define pqContainerSize(pq) (taosArrayGetSize((pq)->container)) +#define pqContainerSize(pq) (taosArrayGetSize((pq)->container)) size_t taosPQSize(PriorityQueue* pq) { return pqContainerSize(pq); } @@ -288,9 +286,7 @@ static void pqRemove(PriorityQueue* pq, size_t i) { pqUpdate(pq, i); } -PriorityQueueNode* taosPQTop(PriorityQueue* pq) { - return pqContainerGetEle(pq, 0); -} +PriorityQueueNode* taosPQTop(PriorityQueue* pq) { return pqContainerGetEle(pq, 0); } PriorityQueueNode* taosPQPush(PriorityQueue* pq, const PriorityQueueNode* node) { taosArrayPush(pq->container, node); @@ -316,9 +312,7 @@ BoundedQueue* createBoundedQueue(uint32_t maxSize, pq_comp_fn fn, FDelete delete return q; } -void taosBQSetFn(BoundedQueue* q, pq_comp_fn fn) { - taosPQSetFn(q->queue, fn); -} +void taosBQSetFn(BoundedQueue* q, pq_comp_fn fn) { taosPQSetFn(q->queue, fn); } void destroyBoundedQueue(BoundedQueue* q) { if (!q) return; @@ -343,22 +337,12 @@ PriorityQueueNode* taosBQPush(BoundedQueue* q, PriorityQueueNode* n) { } } -PriorityQueueNode* taosBQTop(BoundedQueue* q) { - return taosPQTop(q->queue); -} +PriorityQueueNode* taosBQTop(BoundedQueue* q) { return taosPQTop(q->queue); } -void taosBQBuildHeap(BoundedQueue *q) { - pqBuildHeap(q->queue); -} +void taosBQBuildHeap(BoundedQueue* q) { pqBuildHeap(q->queue); } -size_t taosBQMaxSize(BoundedQueue* q) { - return q->maxSize; -} +size_t taosBQMaxSize(BoundedQueue* q) { return q->maxSize; } -size_t taosBQSize(BoundedQueue* q) { - return taosPQSize(q->queue); -} +size_t taosBQSize(BoundedQueue* q) { return taosPQSize(q->queue); } -void taosBQPop(BoundedQueue* q) { - taosPQPop(q->queue); -} +void taosBQPop(BoundedQueue* q) { taosPQPop(q->queue); } From 93d391beb1edcf4032c8ec75d3aea80efd8e5441 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Wed, 3 Jul 2024 02:53:54 +0000 Subject: [PATCH 17/22] refactor transport --- source/libs/transport/src/transCli.c | 124 +++++++++++++-------------- source/libs/transport/src/transSvr.c | 18 +--- 2 files changed, 62 insertions(+), 80 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index adb6223a0f..aa04bc336f 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -398,41 +398,13 @@ void cliResetConnTimer(SCliConn* conn) { tDebug("%s conn %p stop timer", CONN_GET_INST_LABEL(conn), conn); uv_timer_stop(conn->timer); } + taosArrayPush(pThrd->timerList, &conn->timer); conn->timer->data = NULL; conn->timer = NULL; } } -void cliHandleBatchResp(SCliConn* conn) { - ASSERT(0); - SCliThrd* pThrd = conn->hostThrd; - STrans* pTransInst = pThrd->pTransInst; - cliResetConnTimer(conn); - - STransMsgHead* pHead = NULL; - int32_t msgLen = transDumpFromBuffer(&conn->readBuf, (char**)&pHead); - if (transDecompressMsg((char**)&pHead, msgLen) < 0) { - tDebug("%s conn %p recv invalid packet, failed to decompress", CONN_GET_INST_LABEL(conn), conn); - } - pHead->code = htonl(pHead->code); - pHead->msgLen = htonl(pHead->msgLen); - - STransMsg transMsg = {0}; - transMsg.contLen = transContLenFromMsg(pHead->msgLen); - transMsg.pCont = transContFromHead((char*)pHead); - transMsg.code = pHead->code; - transMsg.msgType = pHead->msgType; - transMsg.info.ahandle = NULL; - transMsg.info.traceId = pHead->traceId; - transMsg.info.hasEpSet = pHead->hasEpSet; - transMsg.info.cliVer = htonl(pHead->compatibilityVer); - - SCliMsg* pMsg = NULL; - STransConnCtx* pCtx = pMsg->ctx; - if (cliAppCb(conn, &transMsg, pMsg) != 0) { - return; - } -} +void cliHandleBatchResp(SCliConn* conn) { ASSERT(0); } SCliMsg* cliFindMsgBySeqnum(SCliConn* conn, int32_t seqNum) { SCliMsg* pMsg = NULL; @@ -487,8 +459,6 @@ void cliHandleResp_shareConn(SCliConn* conn) { if (cliAppCb(conn, &transMsg, pMsg) != 0) { return; } - - return; } void cliHandleResp(SCliConn* conn) { SCliThrd* pThrd = conn->hostThrd; @@ -1387,7 +1357,7 @@ static void cliDestroyBatch(SCliBatch* pBatch) { static void cliDoConn(SCliThrd* pThrd, SCliConn* conn, char* ip, int port) { STrans* pTransInst = pThrd->pTransInst; uint32_t ipaddr = cliGetIpFromFqdnCache(pThrd->fqdn2ipCache, ip); - if (ipaddr == 0xffffffff) { + if (ipaddr == (uint32_t)(-1)) { cliResetConnTimer(conn); cliHandleFastFail(conn, -1); return; @@ -1399,6 +1369,7 @@ static void cliDoConn(SCliThrd* pThrd, SCliConn* conn, char* ip, int port) { addr.sin_port = (uint16_t)htons(port); tTrace("%s conn %p try to connect to %s", pTransInst->label, conn, conn->dstAddr); + int32_t fd = taosCreateSocketWithTimeout(TRANS_CONN_TIMEOUT * 10); if (fd == -1) { tError("%s conn %p failed to create socket, reason:%s", transLabel(pTransInst), conn, @@ -1406,6 +1377,7 @@ static void cliDoConn(SCliThrd* pThrd, SCliConn* conn, char* ip, int port) { cliHandleFastFail(conn, -1); return; } + int ret = uv_tcp_open((uv_tcp_t*)conn->stream, fd); if (ret != 0) { tError("%s conn %p failed to set stream, reason:%s", transLabel(pTransInst), conn, uv_err_name(ret)); @@ -1426,7 +1398,15 @@ static void cliDoConn(SCliThrd* pThrd, SCliConn* conn, char* ip, int port) { cliHandleFastFail(conn, -1); return; } - uv_timer_start(conn->timer, cliConnTimeout, TRANS_CONN_TIMEOUT, 0); + + ret = uv_timer_start(conn->timer, cliConnTimeout, TRANS_CONN_TIMEOUT, 0); + if (ret != 0) { + tError("%s conn %p failed to start timer, reason:%s", transLabel(pTransInst), conn, uv_err_name(ret)); + cliResetConnTimer(conn); + cliMayUpdateFqdnCache(pThrd->fqdn2ipCache, conn->dstAddr); + cliHandleFastFail(conn, -1); + return; + } return; } static void cliHandleBatchReq(SCliBatch* pBatch, SCliThrd* pThrd) { @@ -1462,16 +1442,15 @@ static void cliHandleBatchReq(SCliBatch* pBatch, SCliThrd* pThrd) { cliSendBatch(conn); } static void cliSendBatchCb(uv_write_t* req, int status) { - SCliConn* conn = req->data; - SCliThrd* thrd = conn->hostThrd; + SCliConn* conn = req->data; + SCliThrd* thrd = conn->hostThrd; + SCliBatch* p = conn->pBatch; - - SCliBatchList* pBatchList = p->pList; - SCliBatch* nxtBatch = cliGetHeadFromList(pBatchList); - pBatchList->connCnt -= 1; - conn->pBatch = NULL; + SCliBatch* nxtBatch = cliGetHeadFromList(p->pList); + p->pList->connCnt -= 1; + if (status != 0) { tDebug("%s conn %p failed to send batch msg, batch size:%d, msgLen:%d, reason:%s", CONN_GET_INST_LABEL(conn), conn, p->wLen, p->batchSize, uv_err_name(status)); @@ -1491,7 +1470,6 @@ static void cliSendBatchCb(uv_write_t* req, int status) { } } else { cliDestroyBatch(nxtBatch); - // conn release by other callback } } @@ -2649,23 +2627,7 @@ bool cliGenRetryRule(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) { cliSchedMsgToNextNode(pMsg, pThrd); return true; } -int cliAppCb(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) { - SCliThrd* pThrd = pConn->hostThrd; - STrans* pTransInst = pThrd->pTransInst; - - if (pMsg == NULL || pMsg->ctx == NULL) { - tTrace("%s conn %p handle resp", pTransInst->label, pConn); - pTransInst->cfp(pTransInst->parent, pResp, NULL); - return 0; - } - - STransConnCtx* pCtx = pMsg->ctx; - - bool retry = cliGenRetryRule(pConn, pResp, pMsg); - if (retry == true) { - return -1; - } - +void cliMayReSetRespCode(STransConnCtx* pCtx, STransMsg* pResp) { if (pCtx->retryCode != TSDB_CODE_SUCCESS) { int32_t code = pResp->code; // return internal code app @@ -2683,6 +2645,24 @@ int cliAppCb(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) { pResp->code = TSDB_CODE_RPC_SOMENODE_BROKEN_LINK; } } +} +int cliAppCb(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) { + SCliThrd* pThrd = pConn->hostThrd; + STrans* pTransInst = pThrd->pTransInst; + + if (pMsg == NULL || pMsg->ctx == NULL) { + tTrace("%s conn %p handle resp", pTransInst->label, pConn); + pTransInst->cfp(pTransInst->parent, pResp, NULL); + return 0; + } + + bool retry = cliGenRetryRule(pConn, pResp, pMsg); + if (retry == true) { + return -1; + } + + STransConnCtx* pCtx = pMsg->ctx; + cliMayReSetRespCode(pCtx, pResp); STraceId* trace = &pResp->info.traceId; bool hasEpSet = cliTryExtractEpSet(pResp, &pCtx->epSet); @@ -2817,7 +2797,13 @@ int transReleaseCliHandle(void* handle) { } static SCliMsg* transInitMsg(void* shandle, const SEpSet* pEpSet, STransMsg* pReq, STransCtx* ctx) { TRACE_SET_MSGID(&pReq->info.traceId, tGenIdPI64()); + STransConnCtx* pCtx = taosMemoryCalloc(1, sizeof(STransConnCtx)); + if (pCtx == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } + epsetAssign(&pCtx->epSet, pEpSet); epsetAssign(&pCtx->origEpSet, pEpSet); @@ -2827,6 +2813,12 @@ static SCliMsg* transInitMsg(void* shandle, const SEpSet* pEpSet, STransMsg* pRe if (ctx != NULL) pCtx->appCtx = *ctx; SCliMsg* cliMsg = taosMemoryCalloc(1, sizeof(SCliMsg)); + if (cliMsg == NULL) { + taosMemoryFree(pCtx); + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } + cliMsg->ctx = pCtx; cliMsg->msg = *pReq; cliMsg->st = taosGetTimestampUs(); @@ -3059,12 +3051,16 @@ int transSetDefaultAddr(void* shandle, const char* ip, const char* fqdn) { return TSDB_CODE_RPC_BROKEN_LINK; } - SCvtAddr cvtAddr = {0}; - if (ip != NULL && fqdn != NULL) { - tstrncpy(cvtAddr.ip, ip, sizeof(cvtAddr.ip)); - tstrncpy(cvtAddr.fqdn, fqdn, sizeof(cvtAddr.fqdn)); - cvtAddr.cvt = true; + if (ip == NULL || fqdn == NULL) { + transReleaseExHandle(transGetInstMgt(), (int64_t)shandle); + return TSDB_CODE_RPC_FQDN_ERROR; } + + SCvtAddr cvtAddr = {0}; + tstrncpy(cvtAddr.ip, ip, sizeof(cvtAddr.ip)); + tstrncpy(cvtAddr.fqdn, fqdn, sizeof(cvtAddr.fqdn)); + cvtAddr.cvt = true; + for (int i = 0; i < pTransInst->numOfThreads; i++) { STransConnCtx* pCtx = taosMemoryCalloc(1, sizeof(STransConnCtx)); pCtx->cvtAddr = cvtAddr; diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index 57ea89b82a..907f132d32 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -421,15 +421,7 @@ static bool uvHandleReq(SSvrConn* pConn) { return true; } - // TODO(dengyihao): time-consuming task throwed into BG Thread - // uv_work_t* wreq = taosMemoryMalloc(sizeof(uv_work_t)); - // wreq->data = pConn; - // uv_read_stop((uv_stream_t*)pConn->pTcp); - // transRefSrvHandle(pConn); - // uv_queue_work(((SWorkThrd*)pConn->hostThrd)->loop, wreq, uvWorkDoTask, uvWorkAfterTask); - - STransMsg transMsg; - memset(&transMsg, 0, sizeof(transMsg)); + STransMsg transMsg = {0}; transMsg.contLen = transContLenFromMsg(pHead->msgLen); transMsg.pCont = pHead->content; transMsg.msgType = pHead->msgType; @@ -942,14 +934,9 @@ void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) { return; } - // uv_handle_type pending = uv_pipe_pending_type(pipe); - SSvrConn* pConn = createConn(pThrd); pConn->pTransInst = pThrd->pTransInst; - /* init conn timer*/ - // uv_timer_init(pThrd->loop, &pConn->pTimer); - // pConn->pTimer.data = pConn; pConn->hostThrd = pThrd; @@ -1107,6 +1094,7 @@ static FORCE_INLINE SSvrConn* createConn(void* hThrd) { STrans* pTransInst = pThrd->pTransInst; pConn->refId = exh->refId; + QUEUE_INIT(&exh->q); transRefSrvHandle(pConn); tTrace("%s handle %p, conn %p created, refId:%" PRId64, transLabel(pTransInst), exh, pConn, pConn->refId); @@ -1618,5 +1606,3 @@ void transSetIpWhiteList(void* thandle, void* arg, FilteFunc* func) { } transReleaseExHandle(transGetInstMgt(), (int64_t)thandle); } - -int transGetConnInfo(void* thandle, STransHandleInfo* pConnInfo) { return -1; } From 620f4c58067749854c1892797b6cac60650ac6e6 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Wed, 3 Jul 2024 06:46:38 +0000 Subject: [PATCH 18/22] refactor transport --- source/libs/transport/src/transCli.c | 81 ++++++++++++++++++++++------ 1 file changed, 66 insertions(+), 15 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index aa04bc336f..dfeea77035 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -85,6 +85,7 @@ typedef struct SCliConn { int64_t refId; int32_t seq; + int32_t shareCnt; } SCliConn; typedef struct SCliMsg { @@ -230,6 +231,10 @@ static FORCE_INLINE void destroyCmsgAndAhandle(void* cmsg); static FORCE_INLINE int cliRBChoseIdx(STrans* pTransInst); static FORCE_INLINE void transDestroyConnCtx(STransConnCtx* ctx); +static SCliConn* getConnFromHeapCache(SHashObj* pConnHeapCache, char* key); +static int32_t addConnToHeapCache(SHashObj* pConnHeapCacahe, SCliConn* pConn); +static void delConnFromHeapCache(SHashObj* pConnHeapCache, SCliConn* pConn); + // thread obj static SCliThrd* createThrdObj(void* trans); static void destroyThrdObj(SCliThrd* pThrd); @@ -420,6 +425,15 @@ SCliMsg* cliFindMsgBySeqnum(SCliConn* conn, int32_t seqNum) { } return pMsg; } +bool cliShouldAddConnToPool(SCliConn* conn) { + SCliThrd* pThrd = conn->hostThrd; + bool empty = transQueueEmpty(&conn->cliMsgs); + if (empty) { + delConnFromHeapCache(pThrd->connHeapCache, conn); + } + + return empty; +} void cliHandleResp_shareConn(SCliConn* conn) { SCliThrd* pThrd = conn->hostThrd; STrans* pTransInst = pThrd->pTransInst; @@ -1113,12 +1127,42 @@ static void cliSendCb(uv_write_t* req, int status) { uv_read_start((uv_stream_t*)pConn->stream, cliAllocRecvBufferCb, cliRecvCb); } +static void cliHandleBatch_shareConnExcept(SCliConn* conn) { + int32_t code = -1; + SCliThrd* pThrd = conn->hostThrd; + STrans* pTransInst = pThrd->pTransInst; + for (int i = 0; i < transQueueSize(&conn->cliMsgs); i++) { + SCliMsg* pMsg = transQueueGet(&conn->cliMsgs, i); + ASSERT(pMsg->type != Release); + ASSERT(REQUEST_NO_RESP(&pMsg->msg) == 0); + + STransConnCtx* pCtx = pMsg ? pMsg->ctx : NULL; + STransMsg transMsg = {0}; + transMsg.code = code == -1 ? (conn->broken ? TSDB_CODE_RPC_BROKEN_LINK : TSDB_CODE_RPC_NETWORK_UNAVAIL) : code; + transMsg.msgType = pMsg ? pMsg->msg.msgType + 1 : 0; + transMsg.info.ahandle = NULL; + transMsg.info.cliVer = pTransInst->compatibilityVer; + transMsg.info.ahandle = pCtx->ahandle; + + int32_t ret = cliAppCb(conn, &transMsg, pMsg); + } + + // SCliConn* conn = req->data; + // if (status != 0) { + // tDebug("%s conn %p failed to send batch msg, reason:%s", CONN_GET_INST_LABEL(conn), conn, uv_err_name(status)); + // return; + // } + + // uv_read_start((uv_stream_t*)conn->stream, cliAllocRecvBufferCb, cliRecvCb); + // taosMemoryFree(req); +} static void cliSendBatch_shareConnCb(uv_write_t* req, int status) { SCliConn* conn = req->data; + conn->shareCnt -= 1; if (status != 0) { tDebug("%s conn %p failed to send batch msg, reason:%s", CONN_GET_INST_LABEL(conn), conn, uv_err_name(status)); if (!uv_is_closing((uv_handle_t*)&conn->stream)) { - cliHandleExcept(conn); + cliHandleBatch_shareConnExcept(conn); } return; } @@ -1189,6 +1233,7 @@ void cliSendBatch_shareConn(SCliConn* pConn) { } uv_write_t* req = taosMemoryCalloc(1, sizeof(uv_write_t)); req->data = pConn; + pConn->shareCnt += 1; tDebug("%s conn %p start to send batch msg, batch size:%d, len:%d", CONN_GET_INST_LABEL(pConn), pConn, size, totalLen); uv_write(req, (uv_stream_t*)pConn->stream, wb, j, cliSendBatch_shareConnCb); @@ -1359,7 +1404,12 @@ static void cliDoConn(SCliThrd* pThrd, SCliConn* conn, char* ip, int port) { uint32_t ipaddr = cliGetIpFromFqdnCache(pThrd->fqdn2ipCache, ip); if (ipaddr == (uint32_t)(-1)) { cliResetConnTimer(conn); - cliHandleFastFail(conn, -1); + if (conn->pBatch != NULL) { + cliHandleFastFail(conn, -1); + } else { + cliHandleBatch_shareConnExcept(conn); + } + return; } @@ -1774,19 +1824,17 @@ static SCliConn* getConnFromHeapCache(SHashObj* pConnHeapCache, char* key) { return pConn; } -static int32_t addConnToHeapCache(SHashObj* pConnHeapCacahe, char* key, SCliConn* pConn) { - SHeap* p = getOrCreateHeapIfNotExist(pConnHeapCacahe, key); +static int32_t addConnToHeapCache(SHashObj* pConnHeapCacahe, SCliConn* pConn) { + SHeap* p = getOrCreateHeapIfNotExist(pConnHeapCacahe, pConn->dstAddr); if (p == NULL) { return 0; } return transHeapInsert(p, pConn); } -static void delConnFromHeapCache(SHashObj* pConnHeapCache, char* key, SCliConn* pConn) { - size_t klen = strlen(key); - - SHeap* p = taosHashGet(pConnHeapCache, key, klen); +static void delConnFromHeapCache(SHashObj* pConnHeapCache, SCliConn* pConn) { + SHeap* p = taosHashGet(pConnHeapCache, pConn->dstAddr, strlen(pConn->dstAddr)); if (p == NULL) { - tDebug("failed to get heap cache for key:%s, no need to del", key); + tDebug("failed to get heap cache for key:%s, no need to del", pConn->dstAddr); return; } int ret = transHeapDelete(p, pConn); @@ -1814,19 +1862,22 @@ void cliHandleReq__shareConn(SCliMsg* pMsg, SCliThrd* pThrd) { bool ignore = false; pConn = getConnFromPool(pThrd, addr, &ignore); if (pConn != NULL) { + addConnToHeapCache(pThrd->connHeapCache, pConn); + transQueuePush(&pConn->cliMsgs, pMsg); return cliSendBatch_shareConn(pConn); } + } else { + tGTrace("%s conn %p get from heap cache", CONN_GET_INST_LABEL(pConn), pConn); + transQueuePush(&pConn->cliMsgs, pMsg); + cliSendBatch_shareConn(pConn); + return; } pConn = cliCreateConn(pThrd); pConn->dstAddr = taosStrdup(addr); - code = addConnToHeapCache(pThrd->connHeapCache, addr, pConn); - if (code != 0) { - // do nothing - } else { - // do nothing - } + code = addConnToHeapCache(pThrd->connHeapCache, pConn); + transQueuePush(&pConn->cliMsgs, pMsg); return cliDoConn(pThrd, pConn, EPSET_GET_INUSE_IP(&pMsg->ctx->epSet), EPSET_GET_INUSE_PORT(&pMsg->ctx->epSet)); } void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd) { From 4e3a7855745d83b5edccee7be29abb3f3c442249 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Wed, 3 Jul 2024 08:01:48 +0000 Subject: [PATCH 19/22] refactor transport --- source/libs/transport/src/transCli.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index dfeea77035..4ece709289 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -1145,6 +1145,9 @@ static void cliHandleBatch_shareConnExcept(SCliConn* conn) { transMsg.info.ahandle = pCtx->ahandle; int32_t ret = cliAppCb(conn, &transMsg, pMsg); + if (ret != 0) { + return; + } } // SCliConn* conn = req->data; From 0a138a5078ee0ff6e6c42bdf8c9dade808d83ca7 Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Thu, 11 Jul 2024 10:14:46 +0000 Subject: [PATCH 20/22] refactor code --- source/libs/transport/src/transCli.c | 32 +++++++++++++++------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 4ece709289..1afca14e5e 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -470,8 +470,11 @@ void cliHandleResp_shareConn(SCliConn* conn) { transMsg.info.ahandle = pCtx ? pCtx->ahandle : NULL; STraceId* trace = &transMsg.info.traceId; - if (cliAppCb(conn, &transMsg, pMsg) != 0) { + int32_t ret = cliAppCb(conn, &transMsg, pMsg); + if (ret != 0) { return; + } else { + destroyCmsg(pMsg); } } void cliHandleResp(SCliConn* conn) { @@ -1131,33 +1134,32 @@ static void cliHandleBatch_shareConnExcept(SCliConn* conn) { int32_t code = -1; SCliThrd* pThrd = conn->hostThrd; STrans* pTransInst = pThrd->pTransInst; - for (int i = 0; i < transQueueSize(&conn->cliMsgs); i++) { - SCliMsg* pMsg = transQueueGet(&conn->cliMsgs, i); + while (!transQueueEmpty(&conn->cliMsgs)) { + SCliMsg* pMsg = transQueuePop(&conn->cliMsgs); ASSERT(pMsg->type != Release); ASSERT(REQUEST_NO_RESP(&pMsg->msg) == 0); STransConnCtx* pCtx = pMsg ? pMsg->ctx : NULL; - STransMsg transMsg = {0}; + + STransMsg transMsg = {0}; transMsg.code = code == -1 ? (conn->broken ? TSDB_CODE_RPC_BROKEN_LINK : TSDB_CODE_RPC_NETWORK_UNAVAIL) : code; transMsg.msgType = pMsg ? pMsg->msg.msgType + 1 : 0; transMsg.info.ahandle = NULL; transMsg.info.cliVer = pTransInst->compatibilityVer; transMsg.info.ahandle = pCtx->ahandle; - int32_t ret = cliAppCb(conn, &transMsg, pMsg); - if (ret != 0) { - return; + pMsg->seqNum = 0; + code = cliAppCb(conn, &transMsg, pMsg); + if (code != 0) { + continue; + } else { + // already notify user + destroyCmsg(pMsg); } } - // SCliConn* conn = req->data; - // if (status != 0) { - // tDebug("%s conn %p failed to send batch msg, reason:%s", CONN_GET_INST_LABEL(conn), conn, uv_err_name(status)); - // return; - // } - - // uv_read_start((uv_stream_t*)conn->stream, cliAllocRecvBufferCb, cliRecvCb); - // taosMemoryFree(req); + if (T_REF_VAL_GET(conn) > 1) transUnrefCliHandle(conn); + transUnrefCliHandle(conn); } static void cliSendBatch_shareConnCb(uv_write_t* req, int status) { SCliConn* conn = req->data; From 6e004abc7d4985194368b173702f3a73db011d0c Mon Sep 17 00:00:00 2001 From: Yihao Deng Date: Fri, 12 Jul 2024 11:35:14 +0000 Subject: [PATCH 21/22] opt transport --- source/libs/transport/src/transCli.c | 3 ++- source/libs/transport/src/transSvr.c | 3 +++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 1afca14e5e..82c0b9fd44 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -1182,7 +1182,8 @@ void cliSendBatch_shareConn(SCliConn* pConn) { int32_t totalLen = 0; if (size == 0) { tError("%s conn %p not msg to send", pTransInst->label, pConn); - cliHandleExcept(pConn); + ASSERT(0); + // cliHandleExcept(pConn); return; } uv_buf_t* wb = taosMemoryCalloc(size, sizeof(uv_buf_t)); diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index 907f132d32..c74a841a61 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -427,6 +427,9 @@ static bool uvHandleReq(SSvrConn* pConn) { transMsg.msgType = pHead->msgType; transMsg.code = pHead->code; + if (pHead->seqNum != 0) { + ASSERT(0); + } // pHead->noResp = 1, // 1. server application should not send resp on handle // 2. once send out data, cli conn released to conn pool immediately From 58569208d563d09ae77d22683528d57d69f94920 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Tue, 20 Aug 2024 11:47:07 +0800 Subject: [PATCH 22/22] refactor transport --- source/libs/transport/src/transCli.c | 258 +++++++++++++------------- source/libs/transport/src/transComm.c | 1 + 2 files changed, 133 insertions(+), 126 deletions(-) diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index b98421cb76..701cd55d9b 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -173,8 +173,8 @@ static void cliSendCb(uv_write_t* req, int status); // callback after conn to server static void cliConnCb(uv_connect_t* req, int status); static void cliAsyncCb(uv_async_t* handle); -static void cliIdleCb(uv_idle_t* handle); -static void cliPrepareCb(uv_prepare_t* handle); +// static void cliIdleCb(uv_idle_t* handle); +// static void cliPrepareCb(uv_prepare_t* handle); static void cliHandleBatchReq(SCliBatch* pBatch, SCliThrd* pThrd); static void cliSendBatchCb(uv_write_t* req, int status); @@ -185,7 +185,7 @@ static bool cliRecvReleaseReq(SCliConn* conn, STransMsgHead* pHead); static int32_t allocConnRef(SCliConn* conn, bool update); -static int cliAppCb(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg); +static int cliNotifyCb(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg); void cliResetConnTimer(SCliConn* conn); static int32_t cliCreateConn(SCliThrd* thrd, SCliConn** pCliConn); @@ -200,10 +200,11 @@ static int32_t cliPreCheckSessionLimitForMsg(SCliThrd* pThrd, char* addr, SCliMs static void cliDestroyBatch(SCliBatch* pBatch); // cli util func -static FORCE_INLINE bool cliIsEpsetUpdated(int32_t code, STransConnCtx* pCtx); -static FORCE_INLINE void cliMayCvtFqdnToIp(SEpSet* pEpSet, SCvtAddr* pCvtAddr); +static FORCE_INLINE bool cliIsEpsetUpdated(int32_t code, STransConnCtx* pCtx); +static FORCE_INLINE int32_t cliMayCvtFqdnToIp(SEpSet* pEpSet, const SCvtAddr* pCvtAddr); static FORCE_INLINE int32_t cliBuildExceptResp(SCliMsg* pMsg, STransMsg* resp); +static FORCE_INLINE int32_t cliBuildExceptRespAndNotifyCb(SCliThrd* pThrd, SCliMsg* pMsg, int32_t code); static FORCE_INLINE int32_t cliGetIpFromFqdnCache(SHashObj* cache, char* fqdn, uint32_t* ipaddr); static FORCE_INLINE int32_t cliUpdateFqdnCache(SHashObj* cache, char* fqdn); @@ -216,7 +217,7 @@ static void cliHandleExcept(SCliConn* conn, int32_t code); static void cliReleaseUnfinishedMsg(SCliConn* conn); static void cliHandleFastFail(SCliConn* pConn, int status); -static void doNotifyApp(SCliMsg* pMsg, SCliThrd* pThrd, int32_t code); +static void doNotifyCb(SCliMsg* pMsg, SCliThrd* pThrd, int32_t code); // handle req from app static void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd); static void cliHandleQuit(SCliMsg* pMsg, SCliThrd* pThrd); @@ -240,7 +241,7 @@ static FORCE_INLINE void transDestroyConnCtx(STransConnCtx* ctx); static SCliConn* getConnFromHeapCache(SHashObj* pConnHeapCache, char* key); static int32_t addConnToHeapCache(SHashObj* pConnHeapCacahe, SCliConn* pConn); -static void delConnFromHeapCache(SHashObj* pConnHeapCache, SCliConn* pConn); +static int32_t delConnFromHeapCache(SHashObj* pConnHeapCache, SCliConn* pConn); // thread obj static int32_t createThrdObj(void* trans, SCliThrd** pThrd); @@ -260,11 +261,11 @@ typedef struct { } SHeap; int32_t compareHeapNode(const HeapNode* a, const HeapNode* b); -int transHeapInit(SHeap* heap, int32_t (*cmpFunc)(const HeapNode* a, const HeapNode* b)); +int32_t transHeapInit(SHeap* heap, int32_t (*cmpFunc)(const HeapNode* a, const HeapNode* b)); void transHeapDestroy(SHeap* heap); -int transHeapGet(SHeap* heap, SCliConn** p); -int transHeapInsert(SHeap* heap, SCliConn* p); -int transHeapDelete(SHeap* heap, SCliConn* p); +int32_t transHeapGet(SHeap* heap, SCliConn** p); +int32_t transHeapInsert(SHeap* heap, SCliConn* p); +int32_t transHeapDelete(SHeap* heap, SCliConn* p); #define CLI_RELEASE_UV(loop) \ do { \ @@ -437,7 +438,7 @@ bool cliShouldAddConnToPool(SCliConn* conn) { SCliThrd* pThrd = conn->hostThrd; bool empty = transQueueEmpty(&conn->cliMsgs); if (empty) { - delConnFromHeapCache(pThrd->connHeapCache, conn); + (void)delConnFromHeapCache(pThrd->connHeapCache, conn); } return empty; @@ -478,7 +479,7 @@ void cliHandleResp_shareConn(SCliConn* conn) { transMsg.info.ahandle = pCtx ? pCtx->ahandle : NULL; STraceId* trace = &transMsg.info.traceId; - int32_t ret = cliAppCb(conn, &transMsg, pMsg); + int32_t ret = cliNotifyCb(conn, &transMsg, pMsg); if (ret != 0) { return; } else { @@ -577,7 +578,7 @@ void cliHandleResp(SCliConn* conn) { } if (pMsg == NULL || (pMsg && pMsg->type != Release)) { - if (cliAppCb(conn, &transMsg, pMsg) != 0) { + if (cliNotifyCb(conn, &transMsg, pMsg) != 0) { return; } } @@ -675,7 +676,7 @@ void cliHandleExceptImpl(SCliConn* pConn, int32_t code) { if (pMsg == NULL || (pMsg && pMsg->type != Release)) { int64_t refId = (pMsg == NULL ? 0 : (int64_t)(pMsg->msg.info.handle)); cliDestroyMsgInExhandle(refId); - if (cliAppCb(pConn, &transMsg, pMsg) != 0) { + if (cliNotifyCb(pConn, &transMsg, pMsg) != 0) { return; } } @@ -735,7 +736,7 @@ void* destroyConnPool(SCliThrd* pThrd) { transDQCancel(pThrd->waitConnQueue, pMsg->ctx->task); pMsg->ctx->task = NULL; - doNotifyApp(pMsg, pThrd, TSDB_CODE_RPC_MAX_SESSIONS); + doNotifyCb(pMsg, pThrd, TSDB_CODE_RPC_MAX_SESSIONS); } taosMemoryFree(msglist); @@ -817,14 +818,14 @@ static SCliConn* getConnFromPool2(SCliThrd* pThrd, char* key, SCliMsg** pMsg) { if (pInst->notWaitAvaliableConn || (pInst->noDelayFp != NULL && pInst->noDelayFp((*pMsg)->msg.msgType))) { tDebug("%s msg %s not to send, reason: %s", pInst->label, TMSG_INFO((*pMsg)->msg.msgType), tstrerror(TSDB_CODE_RPC_NETWORK_BUSY)); - doNotifyApp(*pMsg, pThrd, TSDB_CODE_RPC_NETWORK_BUSY); + doNotifyCb(*pMsg, pThrd, TSDB_CODE_RPC_NETWORK_BUSY); *pMsg = NULL; return NULL; } STaskArg* arg = taosMemoryMalloc(sizeof(STaskArg)); if (arg == NULL) { - doNotifyApp(*pMsg, pThrd, TSDB_CODE_OUT_OF_MEMORY); + doNotifyCb(*pMsg, pThrd, TSDB_CODE_OUT_OF_MEMORY); *pMsg = NULL; return NULL; } @@ -834,7 +835,7 @@ static SCliConn* getConnFromPool2(SCliThrd* pThrd, char* key, SCliMsg** pMsg) { SDelayTask* task = transDQSched(pThrd->waitConnQueue, doFreeTimeoutMsg, arg, pInst->timeToGetConn); if (task == NULL) { taosMemoryFree(arg); - doNotifyApp(*pMsg, pThrd, TSDB_CODE_OUT_OF_MEMORY); + doNotifyCb(*pMsg, pThrd, TSDB_CODE_OUT_OF_MEMORY); *pMsg = NULL; return NULL; } @@ -847,7 +848,7 @@ static SCliConn* getConnFromPool2(SCliThrd* pThrd, char* key, SCliMsg** pMsg) { if (!(QUEUE_IS_EMPTY(&(list)->msgQ))) { STaskArg* arg = taosMemoryMalloc(sizeof(STaskArg)); if (arg == NULL) { - doNotifyApp(*pMsg, pThrd, TSDB_CODE_OUT_OF_MEMORY); + doNotifyCb(*pMsg, pThrd, TSDB_CODE_OUT_OF_MEMORY); *pMsg = NULL; return NULL; } @@ -857,7 +858,7 @@ static SCliConn* getConnFromPool2(SCliThrd* pThrd, char* key, SCliMsg** pMsg) { SDelayTask* task = transDQSched(pThrd->waitConnQueue, doFreeTimeoutMsg, arg, pInst->timeToGetConn); if (task == NULL) { taosMemoryFree(arg); - doNotifyApp(*pMsg, pThrd, TSDB_CODE_OUT_OF_MEMORY); + doNotifyCb(*pMsg, pThrd, TSDB_CODE_OUT_OF_MEMORY); *pMsg = NULL; return NULL; } @@ -1255,7 +1256,7 @@ static void cliHandleBatch_shareConnExcept(SCliConn* conn) { transMsg.info.ahandle = pCtx->ahandle; pMsg->seqNum = 0; - code = cliAppCb(conn, &transMsg, pMsg); + code = cliNotifyCb(conn, &transMsg, pMsg); if (code != 0) { continue; } else { @@ -1752,7 +1753,7 @@ void cliConnCb(uv_connect_t* req, int status) { return cliSend(pConn); } -static void doNotifyApp(SCliMsg* pMsg, SCliThrd* pThrd, int32_t code) { +static void doNotifyCb(SCliMsg* pMsg, SCliThrd* pThrd, int32_t code) { STransConnCtx* pCtx = pMsg->ctx; STrans* pInst = pThrd->pInst; @@ -1891,14 +1892,22 @@ SCliConn* cliGetConn(SCliMsg** pMsg, SCliThrd* pThrd, bool* ignore, char* addr) } return conn; } -FORCE_INLINE void cliMayCvtFqdnToIp(SEpSet* pEpSet, SCvtAddr* pCvtAddr) { +FORCE_INLINE int32_t cliMayCvtFqdnToIp(SEpSet* pEpSet, const SCvtAddr* pCvtAddr) { if (pCvtAddr->cvt == false) { - return; + if (EPSET_IS_VALID(pEpSet)) { + return 0; + } else { + return TSDB_CODE_RPC_FQDN_ERROR; + } } if (pEpSet->numOfEps == 1 && strncmp(pEpSet->eps[0].fqdn, pCvtAddr->fqdn, TSDB_FQDN_LEN) == 0) { memset(pEpSet->eps[0].fqdn, 0, TSDB_FQDN_LEN); memcpy(pEpSet->eps[0].fqdn, pCvtAddr->ip, TSDB_FQDN_LEN); } + if (EPSET_IS_VALID(pEpSet)) { + return 0; + } + return TSDB_CODE_RPC_FQDN_ERROR; } FORCE_INLINE bool cliIsEpsetUpdated(int32_t code, STransConnCtx* pCtx) { @@ -1906,11 +1915,10 @@ FORCE_INLINE bool cliIsEpsetUpdated(int32_t code, STransConnCtx* pCtx) { return transEpSetIsEqual(&pCtx->epSet, &pCtx->origEpSet) ? false : true; } + FORCE_INLINE int32_t cliBuildExceptResp(SCliMsg* pMsg, STransMsg* pResp) { if (pMsg == NULL) return -1; - // memset(pResp, 0, sizeof(STransMsg)); - if (pResp->code == 0) { pResp->code = TSDB_CODE_RPC_BROKEN_LINK; } @@ -1921,6 +1929,20 @@ FORCE_INLINE int32_t cliBuildExceptResp(SCliMsg* pMsg, STransMsg* pResp) { return 0; } +FORCE_INLINE int32_t cliBuildExceptRespAndNotifyCb(SCliThrd* pThrd, SCliMsg* pMsg, int32_t code) { + STrans* pInst = pThrd->pInst; + + STransMsg resp = {.code = code}; + code = cliBuildExceptResp(pMsg, &resp); + if (code != 0) { + return code; + } + resp.info.cliVer = pInst->compatibilityVer; + pInst->cfp(pInst->parent, &resp, NULL); + + return 0; +} + static FORCE_INLINE int32_t cliGetIpFromFqdnCache(SHashObj* cache, char* fqdn, uint32_t* ip) { int32_t code = 0; uint32_t addr = 0; @@ -1987,7 +2009,7 @@ static void doFreeTimeoutMsg(void* param) { STraceId* trace = &pMsg->msg.info.traceId; tGTrace("%s msg %s cannot get available conn after timeout", pInst->label, TMSG_INFO(pMsg->msg.msgType)); - doNotifyApp(pMsg, pThrd, TSDB_CODE_RPC_MAX_SESSIONS); + doNotifyCb(pMsg, pThrd, TSDB_CODE_RPC_MAX_SESSIONS); taosMemoryFree(arg); } @@ -2044,28 +2066,33 @@ static int32_t addConnToHeapCache(SHashObj* pConnHeapCacahe, SCliConn* pConn) { } return transHeapInsert(p, pConn); } -static void delConnFromHeapCache(SHashObj* pConnHeapCache, SCliConn* pConn) { + +static int32_t delConnFromHeapCache(SHashObj* pConnHeapCache, SCliConn* pConn) { SHeap* p = taosHashGet(pConnHeapCache, pConn->dstAddr, strlen(pConn->dstAddr)); if (p == NULL) { tDebug("failed to get heap cache for key:%s, no need to del", pConn->dstAddr); - return; + return 0; } - int32_t code = transHeapDelete(p, pConn); + int32_t code = transHeapDelete(p, pConn); if (code != 0) { tDebug("failed to delete conn %p from heap cache since %s", pConn, tstrerror(code)); } + return code; } + void cliHandleReq__shareConn(SCliMsg* pMsg, SCliThrd* pThrd) { int32_t code = 0; STraceId* trace = &pMsg->msg.info.traceId; STrans* pInst = pThrd->pInst; - cliMayCvtFqdnToIp(&pMsg->ctx->epSet, &pThrd->cvtAddr); - if (!EPSET_IS_VALID(&pMsg->ctx->epSet)) { + code = cliMayCvtFqdnToIp(&pMsg->ctx->epSet, &pThrd->cvtAddr); + if (code != 0) { + // notifyCb destroyCmsg(pMsg); return; } + char addr[TSDB_FQDN_LEN + 64] = {0}; CONN_CONSTRUCT_HASH_KEY(addr, EPSET_GET_INUSE_IP(&pMsg->ctx->epSet), EPSET_GET_INUSE_PORT(&pMsg->ctx->epSet)); @@ -2093,18 +2120,14 @@ void cliHandleReq__shareConn(SCliMsg* pMsg, SCliThrd* pThrd) { transQueuePush(&pConn->cliMsgs, pMsg); return cliDoConn(pThrd, pConn, EPSET_GET_INUSE_IP(&pMsg->ctx->epSet), EPSET_GET_INUSE_PORT(&pMsg->ctx->epSet)); } -void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd) { - int32_t code = 0; + +void cliHandleReq__noShareConn(SCliMsg* pMsg, SCliThrd* pThrd) { + int32_t code; STrans* pInst = pThrd->pInst; - - if (pInst->shareConn == 1) { - return cliHandleReq__shareConn(pMsg, pThrd); - } - - cliMayCvtFqdnToIp(&pMsg->ctx->epSet, &pThrd->cvtAddr); - if (!EPSET_IS_VALID(&pMsg->ctx->epSet)) { + code = cliMayCvtFqdnToIp(&pMsg->ctx->epSet, &pThrd->cvtAddr); + if (code != 0) { + // notifyCb destroyCmsg(pMsg); - return; } char* fqdn = EPSET_GET_INUSE_IP(&pMsg->ctx->epSet); @@ -2117,12 +2140,8 @@ void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd) { if (ignore == true) { // persist conn already release by server STransMsg resp = {0}; - (void)cliBuildExceptResp(pMsg, &resp); - // refactorr later - resp.info.cliVer = pInst->compatibilityVer; - if (pMsg->type != Release) { - pInst->cfp(pInst->parent, &resp, NULL); + (void)cliBuildExceptRespAndNotifyCb(pThrd, pMsg, 0); } destroyCmsg(pMsg); return; @@ -2140,13 +2159,7 @@ void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd) { code = cliCreateConn(pThrd, &conn); if (code != 0) { tError("%s failed to create conn, reason:%s", pInst->label, tstrerror(code)); - STransMsg resp = {.code = code}; - (void)cliBuildExceptResp(pMsg, &resp); - - resp.info.cliVer = pInst->compatibilityVer; - if (pMsg->type != Release) { - pInst->cfp(pInst->parent, &resp, NULL); - } + (void)cliBuildExceptRespAndNotifyCb(pThrd, pMsg, code); destroyCmsg(pMsg); return; } @@ -2207,6 +2220,15 @@ void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd) { tGTrace("%s conn %p ready", pInst->label, conn); } +void cliHandleReq(SCliMsg* pMsg, SCliThrd* pThrd) { + STrans* pInst = pThrd->pInst; + if (pInst->shareConn == 1) { + return cliHandleReq__shareConn(pMsg, pThrd); + } else { + return cliHandleReq__noShareConn(pMsg, pThrd); + } +} + static void cliDealReq(queue* wq, SCliThrd* pThrd) { int count = 0; @@ -2243,6 +2265,7 @@ SCliBatch* cliGetHeadFromList(SCliBatchList* pList) { static void cliBuildBatch(SCliMsg* pMsg, queue* h, SCliThrd* pThrd) { STrans* pInst = pThrd->pInst; STransConnCtx* pCtx = pMsg->ctx; + return; } static int32_t createBatchList(SCliBatchList** ppBatchList, char* key, char* ip, uint32_t port) { SCliBatchList* pBatchList = taosMemoryCalloc(1, sizeof(SCliBatchList)); @@ -2316,11 +2339,6 @@ static void cliBatchDealReq(queue* wq, SCliThrd* pThrd) { SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q); - // if (pMsg->type == Normal) { - // cliBuildBatch(pMsg, h, pThrd); - // continue; - // // count++; - // } if (pMsg->type == Normal && REQUEST_NO_RESP(&pMsg->msg)) { cliBuildBatch(pMsg, h, pThrd); continue; @@ -2342,6 +2360,7 @@ static void cliBatchDealReq(queue* wq, SCliThrd* pThrd) { destroyCmsg(pMsg); continue; } + pBatchList->batchLenLimit = pInst->batchSize; SCliBatch* pBatch = NULL; @@ -2416,33 +2435,6 @@ static void cliAsyncCb(uv_async_t* handle) { if (pThrd->stopMsg != NULL) cliHandleQuit(pThrd->stopMsg, pThrd); } -static void cliPrepareCb(uv_prepare_t* handle) { - SCliThrd* thrd = handle->data; - tTrace("prepare work start"); - - SAsyncPool* pool = thrd->asyncPool; - for (int i = 0; i < pool->nAsync; i++) { - uv_async_t* async = &(pool->asyncs[i]); - SAsyncItem* item = async->data; - - queue wq; - (void)taosThreadMutexLock(&item->mtx); - QUEUE_MOVE(&item->qmsg, &wq); - (void)taosThreadMutexUnlock(&item->mtx); - - int count = 0; - while (!QUEUE_IS_EMPTY(&wq)) { - queue* h = QUEUE_HEAD(&wq); - QUEUE_REMOVE(h); - - SCliMsg* pMsg = QUEUE_DATA(h, SCliMsg, q); - (*cliAsyncHandle[pMsg->type])(pMsg, thrd); - count++; - } - } - tTrace("prepare work end"); - if (thrd->stopMsg != NULL) cliHandleQuit(thrd->stopMsg, thrd); -} void cliDestroyConnMsgs(SCliConn* conn, bool destroy) { transCtxCleanup(&conn->ctx); @@ -2464,12 +2456,9 @@ void cliConnFreeMsgs(SCliConn* conn) { continue; } - STransMsg resp = {0}; - if (-1 == cliBuildExceptResp(cmsg, &resp)) { + if (cliBuildExceptRespAndNotifyCb(pThrd, cmsg, 0) != 0) { continue; } - resp.info.cliVer = pInst->compatibilityVer; - pInst->cfp(pInst->parent, &resp, NULL); cmsg->ctx->ahandle = NULL; } @@ -2637,19 +2626,6 @@ static int32_t createThrdObj(void* trans, SCliThrd** ppThrd) { TAOS_CHECK_GOTO(code, NULL, _end); } - pThrd->prepare = taosMemoryCalloc(1, sizeof(uv_prepare_t)); - if (pThrd->prepare == NULL) { - tError("failed to create prepre since:%s", tstrerror(code)); - TAOS_CHECK_GOTO(code, NULL, _end); - } - - code = uv_prepare_init(pThrd->loop, pThrd->prepare); - if (code != 0) { - tError("failed to create prepre since:%s", uv_err_name(code)); - TAOS_CHECK_GOTO(TSDB_CODE_THIRDPARTY_ERROR, NULL, _end); - } - pThrd->prepare->data = pThrd; - int32_t timerSize = 64; pThrd->timerList = taosArrayInit(timerSize, sizeof(void*)); if (pThrd->timerList == NULL) { @@ -2716,7 +2692,6 @@ _end: if (pThrd) { (void)uv_loop_close(pThrd->loop); taosMemoryFree(pThrd->loop); - taosMemoryFree(pThrd->prepare); (void)taosThreadMutexDestroy(&pThrd->msgMtx); transAsyncPoolDestroy(pThrd->asyncPool); for (int i = 0; i < taosArrayGetSize(pThrd->timerList); i++) { @@ -2840,7 +2815,7 @@ static FORCE_INLINE void doDelayTask(void* param) { taosMemoryFree(arg); } -static void doCloseIdleConn(void* param) { +static FORCE_INLINE void doCloseIdleConn(void* param) { STaskArg* arg = param; SCliConn* conn = arg->param1; tDebug("%s conn %p idle, close it", CONN_GET_INST_LABEL(conn), conn); @@ -2848,7 +2823,7 @@ static void doCloseIdleConn(void* param) { cliDestroyConn(conn, true); taosMemoryFree(arg); } -static void cliPerfLog_schedMsg(SCliMsg* pMsg, char* label) { +static FORCE_INLINE void cliPerfLog_schedMsg(SCliMsg* pMsg, char* label) { if (!(rpcDebugFlag & DEBUG_DEBUG)) { return; } @@ -2856,21 +2831,30 @@ static void cliPerfLog_schedMsg(SCliMsg* pMsg, char* label) { STraceId* trace = &pMsg->msg.info.traceId; char tbuf[512] = {0}; (void)epsetToStr(&pCtx->epSet, tbuf, tListLen(tbuf)); + tGDebug("%s retry on next node,use:%s, step: %d,timeout:%" PRId64 "", label, tbuf, pCtx->retryStep, pCtx->retryNextInterval); return; } -static void cliSchedMsgToNextNode(SCliMsg* pMsg, SCliThrd* pThrd) { +static FORCE_INLINE int32_t cliSchedMsgToNextNode(SCliMsg* pMsg, SCliThrd* pThrd) { STrans* pInst = pThrd->pInst; STransConnCtx* pCtx = pMsg->ctx; cliPerfLog_schedMsg(pMsg, transLabel(pThrd->pInst)); STaskArg* arg = taosMemoryMalloc(sizeof(STaskArg)); + if (arg == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } arg->param1 = pMsg; arg->param2 = pThrd; - (void)transDQSched(pThrd->delayQueue, doDelayTask, arg, pCtx->retryNextInterval); + SDelayTask* pTask = transDQSched(pThrd->delayQueue, doDelayTask, arg, pCtx->retryNextInterval); + if (pTask == NULL) { + taosMemoryFree(arg); + return TSDB_CODE_OUT_OF_MEMORY; + } + return 0; } FORCE_INLINE bool cliTryExtractEpSet(STransMsg* pResp, SEpSet* dst) { @@ -2963,18 +2947,17 @@ bool cliResetEpset(STransConnCtx* pCtx, STransMsg* pResp, bool hasEpSet) { } return noDelay; } -bool cliGenRetryRule(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) { - SCliThrd* pThrd = pConn->hostThrd; - STrans* pInst = pThrd->pInst; - STransConnCtx* pCtx = pMsg->ctx; - int32_t code = pResp->code; - - bool retry = pInst->retry != NULL ? pInst->retry(code, pResp->msgType - 1) : false; +int8_t cliRetryShouldRetry(STrans* pInst, STransMsg* pResp) { + bool retry = pInst->retry != NULL ? pInst->retry(pResp->code, pResp->msgType - 1) : false; if (retry == false) { - return false; + return 0; } + return 1; +} +void cliRetryMayInitCtx(STrans* pInst, SCliMsg* pMsg) { + STransConnCtx* pCtx = pMsg->ctx; if (!pCtx->retryInit) { pCtx->retryMinInterval = pInst->retryMinInterval; pCtx->retryMaxInterval = pInst->retryMaxInterval; @@ -2985,15 +2968,32 @@ bool cliGenRetryRule(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) { pCtx->retryStep = 0; pCtx->retryInit = true; pCtx->retryCode = TSDB_CODE_SUCCESS; - - // already retry, not use handle specified by app; pMsg->msg.info.handle = 0; } +} +int32_t cliRetryIsTimeout(STrans* pInst, SCliMsg* pMsg) { + STransConnCtx* pCtx = pMsg->ctx; + if (pCtx->retryMaxTimeout != -1 && taosGetTimestampMs() - pCtx->retryInitTimestamp >= pCtx->retryMaxTimeout) { + return 1; + } + return 0; +} +bool cliGenRetryRule(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) { + SCliThrd* pThrd = pConn->hostThrd; + STrans* pInst = pThrd->pInst; - if (-1 != pCtx->retryMaxTimeout && taosGetTimestampMs() - pCtx->retryInitTimestamp >= pCtx->retryMaxTimeout) { + STransConnCtx* pCtx = pMsg->ctx; + int32_t code = pResp->code; + + cliRetryMayInitCtx(pInst, pMsg); + + if (!cliRetryShouldRetry(pInst, pResp)) { return false; } + if (cliRetryIsTimeout(pInst, pMsg)) { + return false; + } // code, msgType // A: epset,leader, not self @@ -3045,7 +3045,12 @@ bool cliGenRetryRule(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) { } pMsg->sent = 0; - cliSchedMsgToNextNode(pMsg, pThrd); + code = cliSchedMsgToNextNode(pMsg, pThrd); + if (code != 0) { + pResp->code = code; + tError("failed to sched msg to next node, reason:%s", tstrerror(code)); + return false; + } return true; } void cliMayReSetRespCode(STransConnCtx* pCtx, STransMsg* pResp) { @@ -3067,7 +3072,7 @@ void cliMayReSetRespCode(STransConnCtx* pCtx, STransMsg* pResp) { } } } -int cliAppCb(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) { +int cliNotifyCb(SCliConn* pConn, STransMsg* pResp, SCliMsg* pMsg) { SCliThrd* pThrd = pConn->hostThrd; STrans* pInst = pThrd->pInst; @@ -3277,7 +3282,7 @@ int32_t transSendRequest(void* shandle, const SEpSet* pEpSet, STransMsg* pReq, S int64_t handle = (int64_t)pReq->info.handle; SCliThrd* pThrd = transGetWorkThrd(pInst, handle); if (pThrd == NULL) { - TAOS_CHECK_GOTO(TSDB_CODE_RPC_BROKEN_LINK, NULL, _exception;); + TAOS_CHECK_GOTO(TSDB_CODE_RPC_BROKEN_LINK, NULL, _exception); } if (handle != 0) { @@ -3702,6 +3707,7 @@ _exception: return code; } +// conn heap int32_t compareHeapNode(const HeapNode* a, const HeapNode* b) { SCliConn* args1 = container_of(a, SCliConn, node); SCliConn* args2 = container_of(b, SCliConn, node); @@ -3724,7 +3730,7 @@ void transHeapDestroy(SHeap* heap) { heapDestroy(heap->heap); } } -int transHeapGet(SHeap* heap, SCliConn** p) { +int32_t transHeapGet(SHeap* heap, SCliConn** p) { if (heapSize(heap->heap) == 0) { *p = NULL; return -1; @@ -3737,7 +3743,7 @@ int transHeapGet(SHeap* heap, SCliConn** p) { *p = container_of(minNode, SCliConn, node); return 0; } -int transHeapInsert(SHeap* heap, SCliConn* p) { +int32_t transHeapInsert(SHeap* heap, SCliConn* p) { // impl later if (p->inHeap == 1) { return TSDB_CODE_DUP_KEY; @@ -3747,7 +3753,7 @@ int transHeapInsert(SHeap* heap, SCliConn* p) { p->inHeap = 1; return 0; } -int transHeapDelete(SHeap* heap, SCliConn* p) { +int32_t transHeapDelete(SHeap* heap, SCliConn* p) { // impl later if (p->inHeap == 0) { return TSDB_CODE_INVALID_PARA; diff --git a/source/libs/transport/src/transComm.c b/source/libs/transport/src/transComm.c index b940c494d8..d3f8da5ae4 100644 --- a/source/libs/transport/src/transComm.c +++ b/source/libs/transport/src/transComm.c @@ -330,6 +330,7 @@ int transAsyncSend(SAsyncPool* pool, queue* q) { (void)taosThreadMutexLock(&item->mtx); QUEUE_PUSH(&item->qmsg, q); (void)taosThreadMutexUnlock(&item->mtx); + int ret = uv_async_send(async); if (ret != 0) { tError("failed to send async,reason:%s", uv_err_name(ret));