From 61e2f68542fef58370e9ad486cf3e768e2d1b25c Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Fri, 9 Aug 2024 16:07:29 +0800 Subject: [PATCH 1/3] fix return error --- source/dnode/vnode/src/meta/metaQuery.c | 35 ++++++++++++++++++------- source/libs/index/src/indexFilter.c | 32 +++++++++++++++++----- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/source/dnode/vnode/src/meta/metaQuery.c b/source/dnode/vnode/src/meta/metaQuery.c index 27a4179172..afe2c29a59 100644 --- a/source/dnode/vnode/src/meta/metaQuery.c +++ b/source/dnode/vnode/src/meta/metaQuery.c @@ -1234,6 +1234,9 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { SIdxCursor *pCursor = NULL; pCursor = (SIdxCursor *)taosMemoryCalloc(1, sizeof(SIdxCursor)); + if (pCursor == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } pCursor->pMeta = pMeta; pCursor->suid = param->suid; pCursor->cid = param->cid; @@ -1241,21 +1244,29 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { metaRLock(pMeta); - if (tdbTbGet(pMeta->pUidIdx, ¶m->suid, sizeof(tb_uid_t), &pData, &nData) != 0) { + if ((ret = tdbTbGet(pMeta->pUidIdx, ¶m->suid, sizeof(tb_uid_t), &pData, &nData)) != 0) { goto END; } tbDbKey.uid = param->suid; tbDbKey.version = ((SUidIdxVal *)pData)[0].version; - (void)tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData); + + if((ret = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData)) != 0) { + goto END; + } + + tDecoderInit(&dc, pData, nData); ret = metaDecodeEntry(&dc, &oStbEntry); - - if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) { - ret = -1; + if (ret != 0) { goto END; } - ret = -1; + + if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) { + ret = TSDB_CODE_INVALID_PARA; + goto END; + } + ret = TSDB_CODE_INVALID_PARA; for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) { SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i; if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) { @@ -1277,7 +1288,6 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { if (param->val == NULL) { metaError("vgId:%d, failed to filter NULL data", TD_VID(pMeta->pVnode)); - ret = -1; goto END; } else { if (IS_VAR_DATA_TYPE(param->type)) { @@ -1287,8 +1297,13 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { if (param->type == TSDB_DATA_TYPE_NCHAR) { maxSize = 4 * nTagData + 1; buf = taosMemoryCalloc(1, maxSize); + if (buf == NULL) { + ret = TSDB_CODE_OUT_OF_MEMORY; + goto END; + } + if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize)) { - ret = -1; + ret = terrno; goto END; } @@ -1328,7 +1343,7 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal); if (valid < 0) { - break; + ret = valid; } if (count > TRY_ERROR_LIMIT) { break; @@ -1344,6 +1359,7 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { count++; valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur); if (valid < 0) { + ret = valid; break; } else { continue; @@ -1372,6 +1388,7 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { } valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur); if (valid < 0) { + ret = valid; break; } } while (1); diff --git a/source/libs/index/src/indexFilter.c b/source/libs/index/src/indexFilter.c index 9563b91593..0d20fc6741 100644 --- a/source/libs/index/src/indexFilter.c +++ b/source/libs/index/src/indexFilter.c @@ -904,10 +904,13 @@ _return: } static EDealRes sifWalkFunction(SNode *pNode, void *context) { + SIFCtx *ctx = context; SFunctionNode *node = (SFunctionNode *)pNode; SIFParam output = {.result = taosArrayInit(8, sizeof(uint64_t)), .status = SFLT_COARSE_INDEX}; - - SIFCtx *ctx = context; + if (output.result == NULL) { + ctx->code = TSDB_CODE_OUT_OF_MEMORY; + return DEAL_RES_ERROR; + } ctx->code = sifExecFunction(node, ctx, &output); if (ctx->code != TSDB_CODE_SUCCESS) { sifFreeParam(&output); @@ -921,11 +924,15 @@ static EDealRes sifWalkFunction(SNode *pNode, void *context) { return DEAL_RES_CONTINUE; } static EDealRes sifWalkLogic(SNode *pNode, void *context) { + SIFCtx *ctx = context; SLogicConditionNode *node = (SLogicConditionNode *)pNode; SIFParam output = {.result = taosArrayInit(8, sizeof(uint64_t)), .status = SFLT_COARSE_INDEX}; + if (output.result == NULL) { + ctx->code = TSDB_CODE_OUT_OF_MEMORY; + return DEAL_RES_ERROR; + } - SIFCtx *ctx = context; ctx->code = sifExecLogic(node, ctx, &output); if (ctx->code) { sifFreeParam(&output); @@ -939,10 +946,14 @@ static EDealRes sifWalkLogic(SNode *pNode, void *context) { return DEAL_RES_CONTINUE; } static EDealRes sifWalkOper(SNode *pNode, void *context) { + SIFCtx *ctx = context; SOperatorNode *node = (SOperatorNode *)pNode; SIFParam output = {.result = taosArrayInit(8, sizeof(uint64_t)), .status = SFLT_COARSE_INDEX}; + if (output.result == NULL) { + ctx->code = TSDB_CODE_OUT_OF_MEMORY; + return DEAL_RES_ERROR; + } - SIFCtx *ctx = context; ctx->code = sifExecOper(node, ctx, &output); if (ctx->code) { sifFreeParam(&output); @@ -1018,7 +1029,9 @@ static int32_t sifCalculate(SNode *pNode, SIFParam *pDst) { SIF_ERR_RET(TSDB_CODE_APP_ERROR); } if (res->result != NULL) { - (void)taosArrayAddAll(pDst->result, res->result); + if (taosArrayAddAll(pDst->result, res->result) == NULL) { + SIF_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); + } } pDst->status = res->status; @@ -1078,6 +1091,10 @@ int32_t doFilterTag(SNode *pFilterNode, SIndexMetaArg *metaArg, SArray *result, SFilterInfo *filter = NULL; SArray *output = taosArrayInit(8, sizeof(uint64_t)); + if (output == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } + SIFParam param = {.arg = *metaArg, .result = output, .status = SFLT_NOT_INDEX, .api = *pAPI}; int32_t code = sifCalculate((SNode *)pFilterNode, ¶m); if (code != 0) { @@ -1090,8 +1107,11 @@ int32_t doFilterTag(SNode *pFilterNode, SIndexMetaArg *metaArg, SArray *result, *status = st; } - (void)taosArrayAddAll(result, param.result); sifFreeParam(¶m); + + if(taosArrayAddAll(result, param.result) == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } return TSDB_CODE_SUCCESS; } From fa2da69e7cecfe2ae1d31de25b0a933c6450d951 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Fri, 9 Aug 2024 17:36:20 +0800 Subject: [PATCH 2/3] refactor errno code --- source/libs/index/src/indexFilter.c | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/source/libs/index/src/indexFilter.c b/source/libs/index/src/indexFilter.c index 0d20fc6741..c362932da3 100644 --- a/source/libs/index/src/indexFilter.c +++ b/source/libs/index/src/indexFilter.c @@ -882,11 +882,10 @@ static int32_t sifExecLogic(SLogicConditionNode *node, SIFCtx *ctx, SIFParam *ou if (ctx->noExec == false) { for (int32_t m = 0; m < node->pParameterList->length; m++) { if (node->condType == LOGIC_COND_TYPE_AND) { - (void)taosArrayAddAll(output->result, params[m].result); + if (taosArrayAddAll(output->result, params[m].result) == NULL) return terrno; } else if (node->condType == LOGIC_COND_TYPE_OR) { - (void)taosArrayAddAll(output->result, params[m].result); + if (taosArrayAddAll(output->result, params[m].result) == NULL) return terrno; } else if (node->condType == LOGIC_COND_TYPE_NOT) { - // taosArrayAddAll(output->result, params[m].result); } taosArraySort(output->result, uidCompare); taosArrayRemoveDuplicate(output->result, uidCompare, NULL); @@ -894,8 +893,6 @@ static int32_t sifExecLogic(SLogicConditionNode *node, SIFCtx *ctx, SIFParam *ou } else { for (int32_t m = 0; m < node->pParameterList->length; m++) { output->status = sifMergeCond(node->condType, output->status, params[m].status); - // taosArrayDestroy(params[m].result); - // params[m].result = NULL; } } _return: @@ -904,7 +901,7 @@ _return: } static EDealRes sifWalkFunction(SNode *pNode, void *context) { - SIFCtx *ctx = context; + SIFCtx *ctx = context; SFunctionNode *node = (SFunctionNode *)pNode; SIFParam output = {.result = taosArrayInit(8, sizeof(uint64_t)), .status = SFLT_COARSE_INDEX}; if (output.result == NULL) { @@ -924,7 +921,7 @@ static EDealRes sifWalkFunction(SNode *pNode, void *context) { return DEAL_RES_CONTINUE; } static EDealRes sifWalkLogic(SNode *pNode, void *context) { - SIFCtx *ctx = context; + SIFCtx *ctx = context; SLogicConditionNode *node = (SLogicConditionNode *)pNode; SIFParam output = {.result = taosArrayInit(8, sizeof(uint64_t)), .status = SFLT_COARSE_INDEX}; @@ -946,7 +943,7 @@ static EDealRes sifWalkLogic(SNode *pNode, void *context) { return DEAL_RES_CONTINUE; } static EDealRes sifWalkOper(SNode *pNode, void *context) { - SIFCtx *ctx = context; + SIFCtx *ctx = context; SOperatorNode *node = (SOperatorNode *)pNode; SIFParam output = {.result = taosArrayInit(8, sizeof(uint64_t)), .status = SFLT_COARSE_INDEX}; if (output.result == NULL) { @@ -1029,9 +1026,9 @@ static int32_t sifCalculate(SNode *pNode, SIFParam *pDst) { SIF_ERR_RET(TSDB_CODE_APP_ERROR); } if (res->result != NULL) { - if (taosArrayAddAll(pDst->result, res->result) == NULL) { + if (taosArrayAddAll(pDst->result, res->result) == NULL) { SIF_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); - } + } } pDst->status = res->status; @@ -1090,7 +1087,7 @@ int32_t doFilterTag(SNode *pFilterNode, SIndexMetaArg *metaArg, SArray *result, SFilterInfo *filter = NULL; - SArray *output = taosArrayInit(8, sizeof(uint64_t)); + SArray *output = taosArrayInit(8, sizeof(uint64_t)); if (output == NULL) { return TSDB_CODE_OUT_OF_MEMORY; } @@ -1107,11 +1104,13 @@ int32_t doFilterTag(SNode *pFilterNode, SIndexMetaArg *metaArg, SArray *result, *status = st; } - sifFreeParam(¶m); - if(taosArrayAddAll(result, param.result) == NULL) { + if (taosArrayAddAll(result, param.result) == NULL) { + sifFreeParam(¶m); return TSDB_CODE_OUT_OF_MEMORY; } + + sifFreeParam(¶m); return TSDB_CODE_SUCCESS; } From 12fe64e5bbde85e80d34e49bb03598ba46a18276 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Fri, 9 Aug 2024 17:54:47 +0800 Subject: [PATCH 3/3] refactor errno code --- source/dnode/vnode/src/meta/metaQuery.c | 67 +++++++++---------------- 1 file changed, 25 insertions(+), 42 deletions(-) diff --git a/source/dnode/vnode/src/meta/metaQuery.c b/source/dnode/vnode/src/meta/metaQuery.c index afe2c29a59..e0d5f9cc55 100644 --- a/source/dnode/vnode/src/meta/metaQuery.c +++ b/source/dnode/vnode/src/meta/metaQuery.c @@ -1222,7 +1222,7 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { SMetaFltParam *param = arg; SMetaEntry oStbEntry = {0}; - int32_t ret = -1; + int32_t code = 0; char *buf = NULL; void *pData = NULL; int nData = 0; @@ -1244,42 +1244,34 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { metaRLock(pMeta); - if ((ret = tdbTbGet(pMeta->pUidIdx, ¶m->suid, sizeof(tb_uid_t), &pData, &nData)) != 0) { - goto END; - } + TAOS_CHECK_GOTO(tdbTbGet(pMeta->pUidIdx, ¶m->suid, sizeof(tb_uid_t), &pData, &nData), NULL, END); + tbDbKey.uid = param->suid; tbDbKey.version = ((SUidIdxVal *)pData)[0].version; - if((ret = tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData)) != 0) { - goto END; - } - - + TAOS_CHECK_GOTO(tdbTbGet(pMeta->pTbDb, &tbDbKey, sizeof(tbDbKey), &pData, &nData), NULL, END); tDecoderInit(&dc, pData, nData); - ret = metaDecodeEntry(&dc, &oStbEntry); - if (ret != 0) { - goto END; - } + + TAOS_CHECK_GOTO(metaDecodeEntry(&dc, &oStbEntry), NULL, END); if (oStbEntry.stbEntry.schemaTag.pSchema == NULL || oStbEntry.stbEntry.schemaTag.pSchema == NULL) { - ret = TSDB_CODE_INVALID_PARA; - goto END; + TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, NULL, END); } - ret = TSDB_CODE_INVALID_PARA; + + code = TSDB_CODE_INVALID_PARA; for (int i = 0; i < oStbEntry.stbEntry.schemaTag.nCols; i++) { SSchema *schema = oStbEntry.stbEntry.schemaTag.pSchema + i; if (schema->colId == param->cid && param->type == schema->type && (IS_IDX_ON(schema))) { - ret = 0; + code = 0; + } else { + TAOS_CHECK_GOTO(code, NULL, END); } } - if (ret != 0) { - goto END; - } - ret = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL); - if (ret != 0) { - goto END; + code = tdbTbcOpen(pMeta->pTagIdx, &pCursor->pCur, NULL); + if (code != 0) { + TAOS_CHECK_GOTO(terrno, NULL, END); } int32_t maxSize = 0; @@ -1298,13 +1290,11 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { maxSize = 4 * nTagData + 1; buf = taosMemoryCalloc(1, maxSize); if (buf == NULL) { - ret = TSDB_CODE_OUT_OF_MEMORY; - goto END; + TAOS_CHECK_GOTO(terrno, NULL, END); } if (false == taosMbsToUcs4(tagData, nTagData, (TdUcs4 *)buf, maxSize, &maxSize)) { - ret = terrno; - goto END; + TAOS_CHECK_GOTO(terrno, NULL, END); } tagData = buf; @@ -1315,18 +1305,12 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { nTagData = tDataTypes[param->type].bytes; } } - ret = metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type, - param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey); - if (ret != 0) { - goto END; - } + TAOS_CHECK_GOTO(metaCreateTagIdxKey(pCursor->suid, pCursor->cid, tagData, nTagData, pCursor->type, + param->reverse ? INT64_MAX : INT64_MIN, &pKey, &nKey), NULL, END); int cmp = 0; - ret = tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp); - if (ret != 0) { - goto END; - } + TAOS_CHECK_GOTO(tdbTbcMoveTo(pCursor->pCur, pKey, nKey, &cmp), 0, END); int count = 0; int32_t valid = 0; @@ -1343,7 +1327,7 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { valid = tdbTbcGet(pCursor->pCur, (const void **)&entryKey, &nEntryKey, (const void **)&entryVal, &nEntryVal); if (valid < 0) { - ret = valid; + code = valid; } if (count > TRY_ERROR_LIMIT) { break; @@ -1359,7 +1343,7 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { count++; valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur); if (valid < 0) { - ret = valid; + code = valid; break; } else { continue; @@ -1376,8 +1360,7 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { tuid = *(tb_uid_t *)(p->data + tDataTypes[pCursor->type].bytes); } if (taosArrayPush(pUids, &tuid) == NULL) { - ret = terrno; - break; + TAOS_CHECK_GOTO(terrno, NULL, END); } found = true; } else { @@ -1388,7 +1371,7 @@ int32_t metaFilterTableIds(void *pVnode, SMetaFltParam *arg, SArray *pUids) { } valid = param->reverse ? tdbTbcMoveToPrev(pCursor->pCur) : tdbTbcMoveToNext(pCursor->pCur); if (valid < 0) { - ret = valid; + code = valid; break; } } while (1); @@ -1405,7 +1388,7 @@ END: taosMemoryFree(pCursor); - return ret; + return code; } static int32_t metaGetTableTagByUid(SMeta *pMeta, int64_t suid, int64_t uid, void **tag, int32_t *len, bool lock) {