diff --git a/include/libs/qcom/query.h b/include/libs/qcom/query.h index b8d09e2262fd83f64d2f88794612f25daac1fd2b..268649524f7186ba82ecf5cf1cd55a38489ff91a 100644 --- a/include/libs/qcom/query.h +++ b/include/libs/qcom/query.h @@ -239,6 +239,7 @@ int32_t dataConverToStr(char* str, int type, void* buf, int32_t bufSize, int32_t char* parseTagDatatoJson(void* p); int32_t cloneTableMeta(STableMeta* pSrc, STableMeta** pDst); int32_t cloneDbVgInfo(SDBVgInfo* pSrc, SDBVgInfo** pDst); +void freeVgInfo(SDBVgInfo* vgInfo); extern int32_t (*queryBuildMsg[TDMT_MAX])(void* input, char** msg, int32_t msgSize, int32_t* msgLen, void* (*mallocFp)(int64_t)); diff --git a/source/libs/catalog/inc/catalogInt.h b/source/libs/catalog/inc/catalogInt.h index 085b2c1112d12de41227861bfea1fa1d43271d33..ba9990658955f29b437ae0f17c36e24b3a6e84c3 100644 --- a/source/libs/catalog/inc/catalogInt.h +++ b/source/libs/catalog/inc/catalogInt.h @@ -762,7 +762,6 @@ int32_t ctgCloneMetaOutput(STableMetaOutput* output, STableMetaOutput** pOutput) int32_t ctgGenerateVgList(SCatalog* pCtg, SHashObj* vgHash, SArray** pList); void ctgFreeJob(void* job); void ctgFreeHandleImpl(SCatalog* pCtg); -void ctgFreeVgInfo(SDBVgInfo* vgInfo); int32_t ctgGetVgInfoFromHashValue(SCatalog* pCtg, SDBVgInfo* dbInfo, const SName* pTableName, SVgroupInfo* pVgroup); int32_t ctgGetVgInfosFromHashValue(SCatalog* pCtg, SCtgTaskReq* tReq, SDBVgInfo* dbInfo, SCtgTbHashsCtx* pCtx, char* dbFName, SArray* pNames, bool update); @@ -790,6 +789,7 @@ int32_t ctgGetTbHashVgroup(SCatalog* pCtg, SRequestConnInfo* pConn, const SName* SName* ctgGetFetchName(SArray* pNames, SCtgFetch* pFetch); int32_t ctgdGetOneHandle(SCatalog **pHandle); int ctgVgInfoComp(const void* lp, const void* rp); +int32_t ctgMakeVgArray(SDBVgInfo* dbInfo); extern SCatalogMgmt gCtgMgmt; extern SCtgDebug gCTGDebug; diff --git a/source/libs/catalog/src/catalog.c b/source/libs/catalog/src/catalog.c index 3a398d15513eeda063448373b44bfeca10025d15..2b1bdeadf8fc93cf1b39d33226e3c7830a10316d 100644 --- a/source/libs/catalog/src/catalog.c +++ b/source/libs/catalog/src/catalog.c @@ -505,8 +505,7 @@ _return: taosMemoryFreeClear(tbMeta); if (vgInfo) { - taosHashCleanup(vgInfo->vgHash); - taosMemoryFreeClear(vgInfo); + freeVgInfo(vgInfo); } if (vgList) { @@ -546,8 +545,7 @@ _return: } if (vgInfo) { - taosHashCleanup(vgInfo->vgHash); - taosMemoryFreeClear(vgInfo); + freeVgInfo(vgInfo); } CTG_RET(code); @@ -778,8 +776,7 @@ _return: } if (vgInfo) { - taosHashCleanup(vgInfo->vgHash); - taosMemoryFreeClear(vgInfo); + freeVgInfo(vgInfo); } CTG_API_LEAVE(code); @@ -836,8 +833,7 @@ _return: ctgRUnlockVgInfo(dbCache); ctgReleaseDBCache(pCtg, dbCache); } else if (dbInfo) { - taosHashCleanup(dbInfo->vgHash); - taosMemoryFreeClear(dbInfo); + freeVgInfo(dbInfo); } CTG_API_LEAVE(code); @@ -849,7 +845,7 @@ int32_t catalogUpdateDBVgInfo(SCatalog* pCtg, const char* dbFName, uint64_t dbId int32_t code = 0; if (NULL == pCtg || NULL == dbFName || NULL == dbInfo) { - ctgFreeVgInfo(dbInfo); + freeVgInfo(dbInfo); CTG_ERR_JRET(TSDB_CODE_CTG_INVALID_INPUT); } diff --git a/source/libs/catalog/src/ctgCache.c b/source/libs/catalog/src/ctgCache.c index c319a8350f09349fd39964ffcd5711737ceefe14..4626d4527dfb8c6294eceaf5e034d8e0e41ce690 100644 --- a/source/libs/catalog/src/ctgCache.c +++ b/source/libs/catalog/src/ctgCache.c @@ -843,7 +843,7 @@ int32_t ctgUpdateVgroupEnqueue(SCatalog *pCtg, const char *dbFName, int64_t dbId if (NULL == msg) { ctgError("malloc %d failed", (int32_t)sizeof(SCtgUpdateVgMsg)); taosMemoryFree(op); - ctgFreeVgInfo(dbInfo); + freeVgInfo(dbInfo); CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); } @@ -852,16 +852,12 @@ int32_t ctgUpdateVgroupEnqueue(SCatalog *pCtg, const char *dbFName, int64_t dbId dbFName = p + 1; } - if (dbInfo->vgHash && NULL == dbInfo->vgArray) { - dbInfo->vgArray = taosArrayInit(100, sizeof(SVgroupInfo)); - - void* pIter = taosHashIterate(dbInfo->vgHash, NULL); - while (pIter) { - taosArrayPush(dbInfo->vgArray, pIter); - pIter = taosHashIterate(dbInfo->vgHash, pIter); - } - - taosArraySort(dbInfo->vgArray, ctgVgInfoComp); + code = ctgMakeVgArray(dbInfo); + if (code) { + taosMemoryFree(op); + taosMemoryFree(msg); + freeVgInfo(dbInfo); + CTG_ERR_RET(code); } tstrncpy(msg->dbFName, dbFName, sizeof(msg->dbFName)); @@ -877,7 +873,7 @@ int32_t ctgUpdateVgroupEnqueue(SCatalog *pCtg, const char *dbFName, int64_t dbId _return: - ctgFreeVgInfo(dbInfo); + freeVgInfo(dbInfo); CTG_RET(code); } @@ -1675,7 +1671,7 @@ int32_t ctgOpUpdateVgroup(SCtgCacheOperation *operation) { goto _return; } - ctgFreeVgInfo(vgInfo); + freeVgInfo(vgInfo); } vgCache->vgInfo = dbInfo; @@ -1696,7 +1692,7 @@ int32_t ctgOpUpdateVgroup(SCtgCacheOperation *operation) { _return: - ctgFreeVgInfo(msg->dbInfo); + freeVgInfo(msg->dbInfo); taosMemoryFreeClear(msg); CTG_RET(code); @@ -1749,7 +1745,7 @@ int32_t ctgOpDropDbVgroup(SCtgCacheOperation *operation) { CTG_ERR_JRET(ctgWLockVgInfo(pCtg, dbCache)); - ctgFreeVgInfo(dbCache->vgCache.vgInfo); + freeVgInfo(dbCache->vgCache.vgInfo); dbCache->vgCache.vgInfo = NULL; ctgDebug("db vgInfo removed, dbFName:%s", msg->dbFName); @@ -2139,7 +2135,7 @@ void ctgFreeCacheOperationData(SCtgCacheOperation *op) { switch (op->opId) { case CTG_OP_UPDATE_VGROUP: { SCtgUpdateVgMsg *msg = op->data; - ctgFreeVgInfo(msg->dbInfo); + freeVgInfo(msg->dbInfo); taosMemoryFreeClear(op->data); break; } diff --git a/source/libs/catalog/src/ctgUtil.c b/source/libs/catalog/src/ctgUtil.c index 17ee8bb68ee0f3d10c373938daa9ec46c168bf17..67db0c79d69d741bd8376bae66fdda0eeceefd46 100644 --- a/source/libs/catalog/src/ctgUtil.c +++ b/source/libs/catalog/src/ctgUtil.c @@ -231,18 +231,7 @@ void ctgFreeTbCache(SCtgDBCache* dbCache) { CTG_CACHE_STAT_DEC(numOfTbl, tblNum); } -void ctgFreeVgInfo(SDBVgInfo* vgInfo) { - if (NULL == vgInfo) { - return; - } - - taosHashCleanup(vgInfo->vgHash); - taosArrayDestroy(vgInfo->vgArray); - - taosMemoryFreeClear(vgInfo); -} - -void ctgFreeVgInfoCache(SCtgDBCache* dbCache) { ctgFreeVgInfo(dbCache->vgCache.vgInfo); } +void ctgFreeVgInfoCache(SCtgDBCache* dbCache) { freeVgInfo(dbCache->vgCache.vgInfo); } void ctgFreeDbCache(SCtgDBCache* dbCache) { if (NULL == dbCache) { @@ -364,8 +353,7 @@ void ctgFreeSUseDbOutput(SUseDbOutput* pOutput) { } if (pOutput->dbVgroup) { - taosHashCleanup(pOutput->dbVgroup->vgHash); - taosMemoryFreeClear(pOutput->dbVgroup); + freeVgInfo(pOutput->dbVgroup); } taosMemoryFree(pOutput); @@ -571,8 +559,7 @@ void ctgFreeSubTaskRes(CTG_TASK_TYPE type, void** pRes) { case CTG_TASK_GET_DB_VGROUP: { if (*pRes) { SDBVgInfo* pInfo = (SDBVgInfo*)*pRes; - taosHashCleanup(pInfo->vgHash); - taosMemoryFreeClear(*pRes); + freeVgInfo(pInfo); } break; } @@ -1035,7 +1022,33 @@ int32_t ctgDbVgVersionSortCompare(const void* key1, const void* key2) { } } +int32_t ctgMakeVgArray(SDBVgInfo* dbInfo) { + if (NULL == dbInfo) { + return TSDB_CODE_SUCCESS; + } + + if (dbInfo->vgHash && NULL == dbInfo->vgArray) { + dbInfo->vgArray = taosArrayInit(100, sizeof(SVgroupInfo)); + if (NULL == dbInfo->vgArray) { + CTG_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); + } + + void* pIter = taosHashIterate(dbInfo->vgHash, NULL); + while (pIter) { + taosArrayPush(dbInfo->vgArray, pIter); + pIter = taosHashIterate(dbInfo->vgHash, pIter); + } + + taosArraySort(dbInfo->vgArray, ctgVgInfoComp); + } + + return TSDB_CODE_SUCCESS; +} + + int32_t ctgCloneVgInfo(SDBVgInfo* src, SDBVgInfo** dst) { + CTG_ERR_RET(ctgMakeVgArray(src)); + *dst = taosMemoryMalloc(sizeof(SDBVgInfo)); if (NULL == *dst) { qError("malloc %d failed", (int32_t)sizeof(SDBVgInfo)); diff --git a/source/libs/qcom/src/queryUtil.c b/source/libs/qcom/src/queryUtil.c index 6eadf80e3d3ee8d44668f01a4a7dbb47d70c2c05..96e02c364334615a7b5d62368008cb38f66bfe45 100644 --- a/source/libs/qcom/src/queryUtil.c +++ b/source/libs/qcom/src/queryUtil.c @@ -447,6 +447,19 @@ int32_t cloneTableMeta(STableMeta* pSrc, STableMeta** pDst) { return TSDB_CODE_SUCCESS; } + +void freeVgInfo(SDBVgInfo* vgInfo) { + if (NULL == vgInfo) { + return; + } + + taosHashCleanup(vgInfo->vgHash); + taosArrayDestroy(vgInfo->vgArray); + + taosMemoryFreeClear(vgInfo); +} + + int32_t cloneDbVgInfo(SDBVgInfo* pSrc, SDBVgInfo** pDst) { if (NULL == pSrc) { *pDst = NULL; @@ -475,8 +488,7 @@ int32_t cloneDbVgInfo(SDBVgInfo* pSrc, SDBVgInfo** pDst) { if (0 != taosHashPut((*pDst)->vgHash, vgId, sizeof(*vgId), vgInfo, sizeof(*vgInfo))) { qError("taosHashPut failed, vgId:%d", vgInfo->vgId); taosHashCancelIterate(pSrc->vgHash, pIter); - taosHashCleanup((*pDst)->vgHash); - taosMemoryFreeClear(*pDst); + freeVgInfo(*pDst); return TSDB_CODE_OUT_OF_MEMORY; }