diff --git a/include/common/trow.h b/include/common/trow.h index 8d30384c61..6b836add21 100644 --- a/include/common/trow.h +++ b/include/common/trow.h @@ -310,7 +310,7 @@ int32_t tdAppendColValToRow(SRowBuilder *pBuilder, col_id_t colId, int8_t colTyp int32_t tdGetTpRowValOfCol(SCellVal *output, STSRow *pRow, void *pBitmap, int8_t colType, int32_t offset, int16_t colIdx); int32_t tdGetKvRowValOfCol(SCellVal *output, STSRow *pRow, void *pBitmap, int32_t offset, int16_t colIdx); -void tTSRowGetVal(STSRow *pRow, STSchema *pTSchema, int16_t iCol, SColVal *pColVal); +int32_t tTSRowGetVal(STSRow *pRow, STSchema *pTSchema, int16_t iCol, SColVal *pColVal); typedef struct { STSchema *pSchema; diff --git a/include/os/osFile.h b/include/os/osFile.h index 8bacb1bf7c..a56c54b086 100644 --- a/include/os/osFile.h +++ b/include/os/osFile.h @@ -120,15 +120,29 @@ int32_t taosSetFileHandlesLimit(); int32_t taosLinkFile(char *src, char *dst); -FILE* taosOpenCFile(const char* filename, const char* mode); -int taosSeekCFile(FILE* file, int64_t offset, int whence); -size_t taosReadFromCFile(void *buffer, size_t size, size_t count, FILE *stream ); -size_t taosWriteToCFile(const void* ptr, size_t size, size_t nitems, FILE* stream); -int taosCloseCFile(FILE *); -int taosSetAutoDelFile(char* path); +FILE *taosOpenCFile(const char *filename, const char *mode); +int taosSeekCFile(FILE *file, int64_t offset, int whence); +size_t taosReadFromCFile(void *buffer, size_t size, size_t count, FILE *stream); +size_t taosWriteToCFile(const void *ptr, size_t size, size_t nitems, FILE *stream); +int taosCloseCFile(FILE *); +int taosSetAutoDelFile(char *path); bool lastErrorIsFileNotExist(); +#ifdef BUILD_WITH_RAND_ERR +#define STUB_RAND_NETWORK_ERR(status) \ + do { \ + if (tsEnableRandErr && (tsRandErrScope & RAND_ERR_NETWORK)) { \ + uint32_t r = taosRand() % tsRandErrDivisor; \ + if ((r + 1) <= tsRandErrChance) { \ + status = TSDB_CODE_RPC_NETWORK_UNAVAIL; \ + } \ + } \ + while (0) +#else +#define STUB_RAND_NETWORK_ERR(status) +#endif + #ifdef __cplusplus } #endif diff --git a/include/util/taoserror.h b/include/util/taoserror.h index cbcf977994..02e6a49bb6 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -468,6 +468,8 @@ int32_t taosGetErrSize(); #define TSDB_CODE_DNODE_INVALID_TTL_CHG_ON_WR TAOS_DEF_ERROR_CODE(0, 0x0427) #define TSDB_CODE_DNODE_INVALID_EN_WHITELIST TAOS_DEF_ERROR_CODE(0, 0x0428) #define TSDB_CODE_DNODE_INVALID_MONITOR_PARAS TAOS_DEF_ERROR_CODE(0, 0x0429) +#define TSDB_CODE_MNODE_STOPPED TAOS_DEF_ERROR_CODE(0, 0x042A) + // mnode-sma #define TSDB_CODE_MND_SMA_ALREADY_EXIST TAOS_DEF_ERROR_CODE(0, 0x0480) diff --git a/include/util/tdef.h b/include/util/tdef.h index 35c4adab50..f087c28684 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -294,7 +294,7 @@ typedef enum ELogicConditionType { #define TSDB_SHOW_SUBQUERY_LEN 1000 #define TSDB_LOG_VAR_LEN 32 -#define TSDB_MAX_EP_NUM 10 +#define TSDB_MAX_EP_NUM 10 #define TSDB_ARB_GROUP_MEMBER_NUM 2 #define TSDB_ARB_TOKEN_SIZE 32 @@ -568,12 +568,7 @@ enum { SND_WORKER_TYPE__UNIQUE, }; -enum { - RAND_ERR_MEMORY = 1, - RAND_ERR_FILE = 2, - // RAND_ERR_SCOPE_XXX... = 4, - // ... -}; +enum { RAND_ERR_MEMORY = 1, RAND_ERR_FILE = 2, RAND_ERR_NETWORK = 4 }; #define DEFAULT_HANDLE 0 #define MNODE_HANDLE 1 diff --git a/include/util/tlosertree.h b/include/util/tlosertree.h index b3aa37a537..c82eff4bd7 100644 --- a/include/util/tlosertree.h +++ b/include/util/tlosertree.h @@ -45,9 +45,9 @@ int32_t tMergeTreeCreate(SMultiwayMergeTreeInfo **pTree, uint32_t numOfEntries, void tMergeTreeDestroy(SMultiwayMergeTreeInfo **pTree); -void tMergeTreeAdjust(SMultiwayMergeTreeInfo *pTree, int32_t idx); +int32_t tMergeTreeAdjust(SMultiwayMergeTreeInfo *pTree, int32_t idx); -void tMergeTreeRebuild(SMultiwayMergeTreeInfo *pTree); +int32_t tMergeTreeRebuild(SMultiwayMergeTreeInfo *pTree); void tMergeTreePrint(const SMultiwayMergeTreeInfo *pTree); diff --git a/source/client/src/clientMsgHandler.c b/source/client/src/clientMsgHandler.c index 4dea9c17b0..771a22b7e3 100644 --- a/source/client/src/clientMsgHandler.c +++ b/source/client/src/clientMsgHandler.c @@ -576,8 +576,8 @@ static int32_t buildShowVariablesRsp(SArray* pVars, SRetrieveTableRsp** pRsp) { size_t rspSize = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pBlock) + PAYLOAD_PREFIX_LEN; *pRsp = taosMemoryCalloc(1, rspSize); if (NULL == *pRsp) { - blockDataDestroy(pBlock); - return TSDB_CODE_OUT_OF_MEMORY; + code = terrno; + goto _exit; } (*pRsp)->useconds = 0; @@ -589,6 +589,11 @@ static int32_t buildShowVariablesRsp(SArray* pVars, SRetrieveTableRsp** pRsp) { (*pRsp)->numOfCols = htonl(SHOW_VARIABLES_RESULT_COLS); int32_t len = blockEncode(pBlock, (*pRsp)->data + PAYLOAD_PREFIX_LEN, SHOW_VARIABLES_RESULT_COLS); + if(len < 0) { + uError("buildShowVariablesRsp error, len:%d", len); + code = terrno; + goto _exit; + } blockDataDestroy(pBlock); SET_PAYLOAD_LEN((*pRsp)->data, len, len); @@ -600,10 +605,21 @@ static int32_t buildShowVariablesRsp(SArray* pVars, SRetrieveTableRsp** pRsp) { if (payloadLen != rspSize - sizeof(SRetrieveTableRsp)) { uError("buildShowVariablesRsp error, len:%d != rspSize - sizeof(SRetrieveTableRsp):%" PRIu64, len, (uint64_t)(rspSize - sizeof(SRetrieveTableRsp))); - return TSDB_CODE_TSC_INVALID_INPUT; + code = TSDB_CODE_TSC_INVALID_INPUT; + goto _exit; } return TSDB_CODE_SUCCESS; +_exit: + if(*pRsp) { + taosMemoryFree(*pRsp); + *pRsp = NULL; + } + if(pBlock) { + blockDataDestroy(pBlock); + pBlock = NULL; + } + return code; } int32_t processShowVariablesRsp(void* param, SDataBuf* pMsg, int32_t code) { @@ -711,8 +727,8 @@ static int32_t buildRetriveTableRspForCompactDb(SCompactDbRsp* pCompactDb, SRetr size_t rspSize = sizeof(SRetrieveTableRsp) + blockGetEncodeSize(pBlock) + PAYLOAD_PREFIX_LEN; *pRsp = taosMemoryCalloc(1, rspSize); if (NULL == *pRsp) { - blockDataDestroy(pBlock); - return TSDB_CODE_OUT_OF_MEMORY; + code = terrno; + goto _exit; } (*pRsp)->useconds = 0; @@ -725,6 +741,11 @@ static int32_t buildRetriveTableRspForCompactDb(SCompactDbRsp* pCompactDb, SRetr (*pRsp)->numOfCols = htonl(COMPACT_DB_RESULT_COLS); int32_t len = blockEncode(pBlock, (*pRsp)->data + PAYLOAD_PREFIX_LEN, COMPACT_DB_RESULT_COLS); + if(len < 0) { + uError("buildRetriveTableRspForCompactDb error, len:%d", len); + code = terrno; + goto _exit; + } blockDataDestroy(pBlock); SET_PAYLOAD_LEN((*pRsp)->data, len, len); @@ -736,10 +757,21 @@ static int32_t buildRetriveTableRspForCompactDb(SCompactDbRsp* pCompactDb, SRetr if (payloadLen != rspSize - sizeof(SRetrieveTableRsp)) { uError("buildRetriveTableRspForCompactDb error, len:%d != rspSize - sizeof(SRetrieveTableRsp):%" PRIu64, len, (uint64_t)(rspSize - sizeof(SRetrieveTableRsp))); - return TSDB_CODE_TSC_INVALID_INPUT; + code = TSDB_CODE_TSC_INVALID_INPUT; + goto _exit; } return TSDB_CODE_SUCCESS; +_exit: + if(*pRsp) { + taosMemoryFree(*pRsp); + *pRsp = NULL; + } + if(pBlock) { + blockDataDestroy(pBlock); + pBlock = NULL; + } + return code; } diff --git a/source/common/src/tdatablock.c b/source/common/src/tdatablock.c index b1e1dcadc2..d0bae5d6a7 100644 --- a/source/common/src/tdatablock.c +++ b/source/common/src/tdatablock.c @@ -973,6 +973,9 @@ int32_t blockDataToBuf(char* buf, const SSDataBlock* pBlock) { int32_t blockDataFromBuf(SSDataBlock* pBlock, const char* buf) { int32_t numOfRows = *(int32_t*)buf; + if (numOfRows == 0) { + return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; + } int32_t code = blockDataEnsureCapacity(pBlock, numOfRows); if (code) { return code; @@ -2888,6 +2891,7 @@ int32_t buildCtbNameByGroupIdImpl(const char* stbFullName, uint64_t groupId, cha return code; } +// return length of encoded data, return -1 if failed int32_t blockEncode(const SSDataBlock* pBlock, char* data, int32_t numOfCols) { int32_t dataLen = 0; @@ -2921,7 +2925,7 @@ int32_t blockEncode(const SSDataBlock* pBlock, char* data, int32_t numOfCols) { for (int32_t i = 0; i < numOfCols; ++i) { SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, i); if (pColInfoData == NULL) { - return terrno; + return -1; } *((int8_t*)data) = pColInfoData->info.type; @@ -2940,7 +2944,7 @@ int32_t blockEncode(const SSDataBlock* pBlock, char* data, int32_t numOfCols) { for (int32_t col = 0; col < numOfCols; ++col) { SColumnInfoData* pColRes = (SColumnInfoData*)taosArrayGet(pBlock->pDataBlock, col); if (pColRes == NULL) { - return terrno; + return -1; } // copy the null bitmap @@ -2991,7 +2995,6 @@ int32_t blockEncode(const SSDataBlock* pBlock, char* data, int32_t numOfCols) { *actualLen = dataLen; *groupId = pBlock->info.id.groupId; - ASSERT(dataLen > 0); return dataLen; } diff --git a/source/common/src/trow.c b/source/common/src/trow.c index 942255cd7c..3e0e52a860 100644 --- a/source/common/src/trow.c +++ b/source/common/src/trow.c @@ -232,7 +232,6 @@ void tdSRowPrint(STSRow *row, STSchema *pSchema, const char *tag) { if (!tdSTSRowIterNext(&iter, &sVal)) { break; } - ASSERT(sVal.valType == 0 || sVal.valType == 1 || sVal.valType == 2); tdSCellValPrint(&sVal, cols[iter.colIdx - 1].type); } printf("\n"); @@ -389,7 +388,7 @@ bool tdSTSRowIterNext(STSRowIter *pIter, SCellVal *pVal) { } else if (TD_IS_KV_ROW(pIter->pRow)) { (void)tdSTSRowIterGetKvVal(pIter, pCol->colId, &pIter->kvIdx, pVal); } else { - ASSERT(0); + return false; } ++pIter->colIdx; @@ -409,7 +408,9 @@ int32_t tdSTSRowNew(SArray *pArray, STSchema *pTSchema, STSRow **ppRow, int8_t r void *varBuf = NULL; bool isAlloc = false; - ASSERT(nColVal > 1); + if(nColVal <= 1) { + TAOS_RETURN(TSDB_CODE_INVALID_PARA); + } for (int32_t iColumn = 0; iColumn < pTSchema->numOfCols; ++iColumn) { pTColumn = &pTSchema->columns[iColumn]; @@ -423,9 +424,10 @@ int32_t tdSTSRowNew(SArray *pArray, STSchema *pTSchema, STSRow **ppRow, int8_t r } if (iColumn == 0) { - ASSERT(pColVal && pColVal->cid == pTColumn->colId); - ASSERT(pTColumn->type == TSDB_DATA_TYPE_TIMESTAMP); - ASSERT(pTColumn->colId == PRIMARYKEY_TIMESTAMP_COL_ID); + if ((pColVal && pColVal->cid != pTColumn->colId) || (pTColumn->type != TSDB_DATA_TYPE_TIMESTAMP) || + (pTColumn->colId != PRIMARYKEY_TIMESTAMP_COL_ID)) { + TAOS_RETURN(TSDB_CODE_INVALID_PARA); + } } else { if (IS_VAR_DATA_TYPE(pTColumn->type)) { if (pColVal && COL_VAL_IS_VALUE(pColVal)) { @@ -598,7 +600,10 @@ bool tdSTSRowGetVal(STSRowIter *pIter, col_id_t colId, col_type_t colType, SCell int32_t tdGetKvRowValOfCol(SCellVal *output, STSRow *pRow, void *pBitmap, int32_t offset, int16_t colIdx) { #ifdef TD_SUPPORT_BITMAP - ASSERT(colIdx < tdRowGetNCols(pRow) - 1); + if (!(colIdx < tdRowGetNCols(pRow) - 1)) { + output->valType = TD_VTYPE_NONE; + TAOS_RETURN(TSDB_CODE_INVALID_PARA); + } int32_t code = 0; if ((code = tdGetBitmapValType(pBitmap, colIdx, &output->valType, 0)) != TSDB_CODE_SUCCESS) { output->valType = TD_VTYPE_NONE; @@ -699,7 +704,6 @@ int32_t tdAppendColValToRow(SRowBuilder *pBuilder, col_id_t colId, int8_t colTyp int32_t tdAppendColValToKvRow(SRowBuilder *pBuilder, TDRowValT valType, const void *val, bool isCopyVarData, int8_t colType, int16_t colIdx, int32_t offset, col_id_t colId) { if (colIdx < 1) { - ASSERTS(0, "colIdx is %" PRIi64, colIdx); TAOS_RETURN(TSDB_CODE_INVALID_PARA); } --colIdx; @@ -775,7 +779,9 @@ int32_t tdSRowResetBuf(SRowBuilder *pBuilder, void *pBuf) { TD_ROW_SET_INFO(pBuilder->pBuf, 0); TD_ROW_SET_TYPE(pBuilder->pBuf, pBuilder->rowType); - ASSERT(pBuilder->nBitmaps > 0 && pBuilder->flen > 0); + if(!(pBuilder->nBitmaps > 0 && pBuilder->flen > 0)) { + TAOS_RETURN(TSDB_CODE_INVALID_PARA); + } uint32_t len = 0; switch (pBuilder->rowType) { @@ -814,7 +820,9 @@ int32_t tdSRowGetBuf(SRowBuilder *pBuilder, void *pBuf) { TAOS_RETURN(TSDB_CODE_INVALID_PARA); } - ASSERT(pBuilder->nBitmaps > 0 && pBuilder->flen > 0); + if(!(pBuilder->nBitmaps > 0 && pBuilder->flen > 0)) { + TAOS_RETURN(TSDB_CODE_INVALID_PARA); + } uint32_t len = 0; switch (pBuilder->rowType) { @@ -930,18 +938,20 @@ int32_t tdSetBitmapValType(void *pBitmap, int16_t colIdx, TDRowValT valType, int TAOS_RETURN(TSDB_CODE_SUCCESS); } -void tTSRowGetVal(STSRow *pRow, STSchema *pTSchema, int16_t iCol, SColVal *pColVal) { +int32_t tTSRowGetVal(STSRow *pRow, STSchema *pTSchema, int16_t iCol, SColVal *pColVal) { STColumn *pTColumn = &pTSchema->columns[iCol]; SCellVal cv = {0}; - ASSERT((pTColumn->colId == PRIMARYKEY_TIMESTAMP_COL_ID) || (iCol > 0)); + if (!((pTColumn->colId == PRIMARYKEY_TIMESTAMP_COL_ID) || (iCol > 0))) { + TAOS_RETURN(TSDB_CODE_INVALID_PARA); + } if (TD_IS_TP_ROW(pRow)) { (void)tdSTpRowGetVal(pRow, pTColumn->colId, pTColumn->type, pTSchema->flen, pTColumn->offset, iCol - 1, &cv); } else if (TD_IS_KV_ROW(pRow)) { (void)tdSKvRowGetVal(pRow, pTColumn->colId, iCol - 1, &cv); } else { - ASSERT(0); + TAOS_RETURN(TSDB_CODE_INVALID_PARA); } if (tdValTypeIsNone(cv.valType)) { @@ -960,4 +970,5 @@ void tTSRowGetVal(STSRow *pRow, STSchema *pTSchema, int16_t iCol, SColVal *pColV (void)memcpy(&pColVal->value.val, cv.val, tDataTypes[pTColumn->type].bytes); } } + return 0; } \ No newline at end of file diff --git a/source/dnode/mgmt/mgmt_dnode/src/dmHandle.c b/source/dnode/mgmt/mgmt_dnode/src/dmHandle.c index 70f258a362..0de0a34c25 100644 --- a/source/dnode/mgmt/mgmt_dnode/src/dmHandle.c +++ b/source/dnode/mgmt/mgmt_dnode/src/dmHandle.c @@ -479,6 +479,12 @@ int32_t dmProcessRetrieve(SDnodeMgmt *pMgmt, SRpcMsg *pMsg) { } int32_t len = blockEncode(pBlock, pStart, numOfCols); + if(len < 0) { + dError("failed to retrieve data since %s", tstrerror(code)); + blockDataDestroy(pBlock); + rpcFreeCont(pRsp); + return terrno; + } pRsp->numOfRows = htonl(pBlock->info.rows); pRsp->precision = TSDB_TIME_PRECISION_MILLI; // millisecond time precision diff --git a/source/dnode/mgmt/mgmt_mnode/src/mmWorker.c b/source/dnode/mgmt/mgmt_mnode/src/mmWorker.c index fc070d0d05..e8e7a75889 100644 --- a/source/dnode/mgmt/mgmt_mnode/src/mmWorker.c +++ b/source/dnode/mgmt/mgmt_mnode/src/mmWorker.c @@ -22,7 +22,7 @@ static inline int32_t mmAcquire(SMnodeMgmt *pMgmt) { int32_t code = 0; (void)taosThreadRwlockRdlock(&pMgmt->lock); if (pMgmt->stopped) { - code = -1; + code = TSDB_CODE_MNODE_STOPPED; } else { (void)atomic_add_fetch_32(&pMgmt->refCount, 1); } @@ -134,16 +134,19 @@ int32_t mmPutMsgToReadQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) { } int32_t mmPutMsgToQueryQueue(SMnodeMgmt *pMgmt, SRpcMsg *pMsg) { + int32_t code = 0; if (NULL == pMgmt->pMnode) { const STraceId *trace = &pMsg->info.traceId; - dGError("msg:%p, stop to pre-process in mnode since mnode is NULL, type:%s", pMsg, TMSG_INFO(pMsg->msgType)); - return -1; + code = TSDB_CODE_MNODE_NOT_FOUND; + dGError("msg:%p, stop to pre-process in mnode since %s, type:%s", pMsg, tstrerror(code), TMSG_INFO(pMsg->msgType)); + return code; } pMsg->info.node = pMgmt->pMnode; - if (mndPreProcessQueryMsg(pMsg) != 0) { + if ((code = mndPreProcessQueryMsg(pMsg)) != 0) { const STraceId *trace = &pMsg->info.traceId; - dGError("msg:%p, failed to pre-process in mnode since %s, type:%s", pMsg, terrstr(), TMSG_INFO(pMsg->msgType)); - return -1; + dGError("msg:%p, failed to pre-process in mnode since %s, type:%s", pMsg, tstrerror(code), + TMSG_INFO(pMsg->msgType)); + return code; } return mmPutMsgToWorker(pMgmt, &pMgmt->queryWorker, pMsg); } diff --git a/source/dnode/mnode/impl/inc/mndArbGroup.h b/source/dnode/mnode/impl/inc/mndArbGroup.h index fcd11310e7..779d64c7e2 100644 --- a/source/dnode/mnode/impl/inc/mndArbGroup.h +++ b/source/dnode/mnode/impl/inc/mndArbGroup.h @@ -29,7 +29,7 @@ void mndReleaseArbGroup(SMnode *pMnode, SArbGroup *pObj); SSdbRaw *mndArbGroupActionEncode(SArbGroup *pGroup); SSdbRow *mndArbGroupActionDecode(SSdbRaw *pRaw); -void mndArbGroupInitFromVgObj(SVgObj *pVgObj, SArbGroup *outGroup); +int32_t mndArbGroupInitFromVgObj(SVgObj *pVgObj, SArbGroup *outGroup); int32_t mndSetCreateArbGroupRedoLogs(STrans *pTrans, SArbGroup *pGroup); int32_t mndSetCreateArbGroupUndoLogs(STrans *pTrans, SArbGroup *pGroup); diff --git a/source/dnode/mnode/impl/src/mndArbGroup.c b/source/dnode/mnode/impl/src/mndArbGroup.c index 32b62422e9..23434e5ee3 100644 --- a/source/dnode/mnode/impl/src/mndArbGroup.c +++ b/source/dnode/mnode/impl/src/mndArbGroup.c @@ -102,8 +102,10 @@ void mndReleaseArbGroup(SMnode *pMnode, SArbGroup *pGroup) { sdbRelease(pSdb, pGroup); } -void mndArbGroupInitFromVgObj(SVgObj *pVgObj, SArbGroup *outGroup) { - ASSERT(pVgObj->replica == 2); +int32_t mndArbGroupInitFromVgObj(SVgObj *pVgObj, SArbGroup *outGroup) { + if (pVgObj->replica != 2) { + TAOS_RETURN(TSDB_CODE_INVALID_PARA); + } (void)memset(outGroup, 0, sizeof(SArbGroup)); outGroup->dbUid = pVgObj->dbUid; outGroup->vgId = pVgObj->vgId; @@ -111,6 +113,8 @@ void mndArbGroupInitFromVgObj(SVgObj *pVgObj, SArbGroup *outGroup) { SArbGroupMember *pMember = &outGroup->members[i]; pMember->info.dnodeId = pVgObj->vnodeGid[i].dnodeId; } + + TAOS_RETURN(TSDB_CODE_SUCCESS); } SSdbRaw *mndArbGroupActionEncode(SArbGroup *pGroup) { diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index d0eed37f99..9b83fa8a1d 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -629,7 +629,7 @@ static int32_t mndSetCreateDbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pD for (int32_t v = 0; v < pDb->cfg.numOfVgroups; ++v) { SVgObj *pVgObj = pVgroups + v; SArbGroup arbGroup = {0}; - mndArbGroupInitFromVgObj(pVgObj, &arbGroup); + TAOS_CHECK_RETURN(mndArbGroupInitFromVgObj(pVgObj, &arbGroup)); TAOS_CHECK_RETURN(mndSetCreateArbGroupRedoLogs(pTrans, &arbGroup)); } } @@ -663,7 +663,7 @@ static int32_t mndSetCreateDbUndoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pD for (int32_t v = 0; v < pDb->cfg.numOfVgroups; ++v) { SVgObj *pVgObj = pVgroups + v; SArbGroup arbGroup = {0}; - mndArbGroupInitFromVgObj(pVgObj, &arbGroup); + TAOS_CHECK_RETURN(mndArbGroupInitFromVgObj(pVgObj, &arbGroup)); TAOS_CHECK_RETURN(mndSetCreateArbGroupUndoLogs(pTrans, &arbGroup)); } } @@ -698,7 +698,7 @@ static int32_t mndSetCreateDbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj * for (int32_t v = 0; v < pDb->cfg.numOfVgroups; ++v) { SVgObj *pVgObj = pVgroups + v; SArbGroup arbGroup = {0}; - mndArbGroupInitFromVgObj(pVgObj, &arbGroup); + TAOS_CHECK_RETURN(mndArbGroupInitFromVgObj(pVgObj, &arbGroup)); TAOS_CHECK_RETURN(mndSetCreateArbGroupCommitLogs(pTrans, &arbGroup)); } } @@ -1156,44 +1156,30 @@ static int32_t mndSetAlterDbCommitLogs(SMnode *pMnode, STrans *pTrans, SDbObj *p } static int32_t mndSetAlterDbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pOldDb, SDbObj *pNewDb) { - int32_t code = 0; + int32_t code = 0, lino = 0; SSdb *pSdb = pMnode->pSdb; void *pIter = NULL; + SVgObj *pVgroup = NULL; SArray *pArray = mndBuildDnodesArray(pMnode, 0); while (1) { - SVgObj *pVgroup = NULL; pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup); if (pIter == NULL) break; if (mndVgroupInDb(pVgroup, pNewDb->uid)) { SVgObj newVgroup = {0}; - if ((code = mndBuildAlterVgroupAction(pMnode, pTrans, pOldDb, pNewDb, pVgroup, pArray, &newVgroup)) != 0) { - sdbCancelFetch(pSdb, pIter); - sdbRelease(pSdb, pVgroup); - taosArrayDestroy(pArray); - TAOS_RETURN(code); - } + TAOS_CHECK_GOTO(mndBuildAlterVgroupAction(pMnode, pTrans, pOldDb, pNewDb, pVgroup, pArray, &newVgroup), &lino, + _err); + if (pNewDb->cfg.withArbitrator != pOldDb->cfg.withArbitrator) { if (pNewDb->cfg.withArbitrator) { SArbGroup arbGroup = {0}; - mndArbGroupInitFromVgObj(&newVgroup, &arbGroup); - if ((code = mndSetCreateArbGroupCommitLogs(pTrans, &arbGroup)) != 0) { - sdbCancelFetch(pSdb, pIter); - sdbRelease(pSdb, pVgroup); - taosArrayDestroy(pArray); - TAOS_RETURN(code); - } - + TAOS_CHECK_GOTO(mndArbGroupInitFromVgObj(&newVgroup, &arbGroup), &lino, _err); + TAOS_CHECK_GOTO(mndSetCreateArbGroupCommitLogs(pTrans, &arbGroup), &lino, _err); } else { SArbGroup arbGroup = {0}; - mndArbGroupInitFromVgObj(pVgroup, &arbGroup); - if ((code = mndSetDropArbGroupCommitLogs(pTrans, &arbGroup)) != 0) { - sdbCancelFetch(pSdb, pIter); - sdbRelease(pSdb, pVgroup); - taosArrayDestroy(pArray); - TAOS_RETURN(code); - } + TAOS_CHECK_GOTO(mndArbGroupInitFromVgObj(pVgroup, &arbGroup), &lino, _err); + TAOS_CHECK_GOTO(mndSetDropArbGroupCommitLogs(pTrans, &arbGroup), &lino, _err); } } } @@ -1203,6 +1189,14 @@ static int32_t mndSetAlterDbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj * taosArrayDestroy(pArray); TAOS_RETURN(code); + +_err: + mError("db:%s, %s failed at %d since %s", pNewDb->name, __func__, lino, tstrerror(code)); + + sdbCancelFetch(pSdb, pIter); + sdbRelease(pSdb, pVgroup); + taosArrayDestroy(pArray); + TAOS_RETURN(code); } static int32_t mndAlterDb(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pOld, SDbObj *pNew) { diff --git a/source/dnode/mnode/impl/src/mndShow.c b/source/dnode/mnode/impl/src/mndShow.c index 6989e1e4f1..86802914b8 100644 --- a/source/dnode/mnode/impl/src/mndShow.c +++ b/source/dnode/mnode/impl/src/mndShow.c @@ -180,7 +180,11 @@ static void mndFreeShowObj(SShowObj *pShow) { ShowFreeIterFp freeFp = pMgmt->freeIterFps[pShow->type]; if (freeFp != NULL) { if (pShow->pIter != NULL) { + mTrace("show:0x%" PRIx64 ", is destroying, data:%p, pIter:%p, ", pShow->id, pShow, pShow->pIter); + (*freeFp)(pMnode, pShow->pIter); + + pShow->pIter = NULL; } } @@ -330,11 +334,9 @@ static int32_t mndProcessRetrieveSysTableReq(SRpcMsg *pReq) { SRetrieveMetaTableRsp *pRsp = rpcMallocCont(size); if (pRsp == NULL) { - mndReleaseShowObj(pShow, false); - code = TSDB_CODE_OUT_OF_MEMORY; mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, tstrerror(code)); - blockDataDestroy(pBlock); - TAOS_RETURN(code); + code = terrno; + goto _exit; } pRsp->handle = htobe64(pShow->id); @@ -356,6 +358,11 @@ static int32_t mndProcessRetrieveSysTableReq(SRpcMsg *pReq) { } int32_t len = blockEncode(pBlock, pStart, pShow->pMeta->numOfColumns); + if(len < 0){ + mError("show:0x%" PRIx64 ", failed to retrieve data since %s", pShow->id, tstrerror(code)); + code = terrno; + return code; + } } pRsp->numOfRows = htonl(rowsRead); @@ -374,6 +381,13 @@ static int32_t mndProcessRetrieveSysTableReq(SRpcMsg *pReq) { blockDataDestroy(pBlock); return TSDB_CODE_SUCCESS; +_exit: + mndReleaseShowObj(pShow, false); + blockDataDestroy(pBlock); + if(pRsp) { + rpcFreeCont(pRsp); + } + return code; } static bool mndCheckRetrieveFinished(SShowObj *pShow) { diff --git a/source/dnode/mnode/impl/src/mndSync.c b/source/dnode/mnode/impl/src/mndSync.c index 3c5724dde3..da5873039b 100644 --- a/source/dnode/mnode/impl/src/mndSync.c +++ b/source/dnode/mnode/impl/src/mndSync.c @@ -189,13 +189,16 @@ int32_t mndProcessWriteMsg(SMnode *pMnode, SRpcMsg *pMsg, SFsmCbMeta *pMeta) { goto _OUT; } + (void)taosThreadMutexLock(&pMnode->pSdb->filelock); code = sdbWriteWithoutFree(pMnode->pSdb, pRaw); if (code != 0) { mError("trans:%d, failed to write to sdb since %s", transId, terrstr()); // code = 0; + (void)taosThreadMutexUnlock(&pMnode->pSdb->filelock); pMeta->code = code; goto _OUT; } + (void)taosThreadMutexUnlock(&pMnode->pSdb->filelock); pTrans = mndAcquireTrans(pMnode, transId); if (pTrans == NULL) { diff --git a/source/dnode/vnode/src/meta/metaTable.c b/source/dnode/vnode/src/meta/metaTable.c index 21602627e9..0334990365 100644 --- a/source/dnode/vnode/src/meta/metaTable.c +++ b/source/dnode/vnode/src/meta/metaTable.c @@ -129,7 +129,11 @@ static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const } SIndexMultiTerm *terms = indexMultiTermCreate(); - int16_t nCols = taosArrayGetSize(pTagVals); + if (terms == NULL) { + return terrno; + } + + int16_t nCols = taosArrayGetSize(pTagVals); for (int i = 0; i < nCols; i++) { STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i); char type = pTagVal->type; @@ -142,8 +146,14 @@ static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0); } else if (type == TSDB_DATA_TYPE_NCHAR) { if (pTagVal->nData > 0) { - char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE); + char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE); + if (val == NULL) { + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, NULL, _exception); + } int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE); + if (len < 0) { + TAOS_CHECK_GOTO(len, NULL, _exception); + } memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE); type = TSDB_DATA_TYPE_VARCHAR; term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, val, len); @@ -160,16 +170,24 @@ static int metaSaveJsonVarToIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const int len = sizeof(val); term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_BOOL, key, nKey, (const char *)&val, len); } + if (term != NULL) { (void)indexMultiTermAdd(terms, term); + } else { + code = terrno; + goto _exception; } } - (void)indexJsonPut(pMeta->pTagIvtIdx, terms, tuid); + code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid); indexMultiTermDestroy(terms); taosArrayDestroy(pTagVals); #endif - return 0; + return code; +_exception: + indexMultiTermDestroy(terms); + taosArrayDestroy(pTagVals); + return code; } int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSchema *pSchema) { #ifdef USE_INVERTED_INDEX @@ -191,7 +209,11 @@ int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSche } SIndexMultiTerm *terms = indexMultiTermCreate(); - int16_t nCols = taosArrayGetSize(pTagVals); + if (terms == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; + } + + int16_t nCols = taosArrayGetSize(pTagVals); for (int i = 0; i < nCols; i++) { STagVal *pTagVal = (STagVal *)taosArrayGet(pTagVals, i); char type = pTagVal->type; @@ -204,8 +226,14 @@ int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSche term = indexTermCreate(suid, DEL_VALUE, TSDB_DATA_TYPE_VARCHAR, key, nKey, NULL, 0); } else if (type == TSDB_DATA_TYPE_NCHAR) { if (pTagVal->nData > 0) { - char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE); + char *val = taosMemoryCalloc(1, pTagVal->nData + VARSTR_HEADER_SIZE); + if (val == NULL) { + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, NULL, _exception); + } int32_t len = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, val + VARSTR_HEADER_SIZE); + if (len < 0) { + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, NULL, _exception); + } memcpy(val, (uint16_t *)&len, VARSTR_HEADER_SIZE); type = TSDB_DATA_TYPE_VARCHAR; term = indexTermCreate(suid, DEL_VALUE, type, key, nKey, val, len); @@ -224,13 +252,20 @@ int metaDelJsonVarFromIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry, const SSche } if (term != NULL) { (void)indexMultiTermAdd(terms, term); + } else { + code = terrno; + goto _exception; } } - (void)indexJsonPut(pMeta->pTagIvtIdx, terms, tuid); + code = indexJsonPut(pMeta->pTagIvtIdx, terms, tuid); indexMultiTermDestroy(terms); taosArrayDestroy(pTagVals); #endif - return 0; + return code; +_exception: + indexMultiTermDestroy(terms); + taosArrayDestroy(pTagVals); + return code; } static inline void metaTimeSeriesNotifyCheck(SMeta *pMeta) { @@ -867,7 +902,6 @@ int metaDropIndexFromSTable(SMeta *pMeta, int64_t version, SDropIndexReq *pReq) goto _err; } - nStbEntry.stbEntry.schemaRow = *row; nStbEntry.stbEntry.schemaTag = *tag; nStbEntry.stbEntry.rsmaParam = oStbEntry.stbEntry.rsmaParam; @@ -1580,7 +1614,12 @@ static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAl : pAlterTbReq->compress; (void)updataTableColCmpr(&entry.colCmpr, pCol, 1, compress); freeColCmpr = true; - ASSERT(entry.colCmpr.nCols == pSchema->nCols); + if (entry.colCmpr.nCols != pSchema->nCols) { + if (pNewSchema) taosMemoryFree(pNewSchema); + if (freeColCmpr) taosMemoryFree(entry.colCmpr.pColCmpr); + terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION; + goto _err; + } break; case TSDB_ALTER_TABLE_DROP_COLUMN: if (pColumn == NULL) { @@ -1617,7 +1656,10 @@ static int metaAlterTableColumn(SMeta *pMeta, int64_t version, SVAlterTbReq *pAl } (void)updataTableColCmpr(&entry.colCmpr, &tScheam, 0, 0); - ASSERT(entry.colCmpr.nCols == pSchema->nCols); + if (entry.colCmpr.nCols != pSchema->nCols) { + terrno = TSDB_CODE_VND_INVALID_TABLE_ACTION; + goto _err; + } break; case TSDB_ALTER_TABLE_UPDATE_COLUMN_BYTES: if (pColumn == NULL) { @@ -1698,7 +1740,7 @@ _err: (void)tdbTbcClose(pUidIdxc); tDecoderClear(&dc); - return TSDB_CODE_FAILED; + return terrno != 0 ? terrno : TSDB_CODE_FAILED; } static int metaUpdateTableTagVal(SMeta *pMeta, int64_t version, SVAlterTbReq *pAlterTbReq) { @@ -2514,8 +2556,7 @@ static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry) { if (tdbTbGet(pMeta->pUidIdx, &pCtbEntry->ctbEntry.suid, sizeof(tb_uid_t), &pData, &nData) != 0) { metaError("vgId:%d, failed to get stable suid for update. version:%" PRId64, TD_VID(pMeta->pVnode), pCtbEntry->version); - terrno = TSDB_CODE_TDB_INVALID_TABLE_ID; - ret = -1; + ret = TSDB_CODE_TDB_INVALID_TABLE_ID; goto end; } tbDbKey.uid = pCtbEntry->ctbEntry.suid; @@ -2529,6 +2570,7 @@ static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry) { } if (stbEntry.stbEntry.schemaTag.pSchema == NULL) { + ret = TSDB_CODE_INVALID_PARA; goto end; } @@ -2573,7 +2615,9 @@ static int metaUpdateTagIdx(SMeta *pMeta, const SMetaEntry *pCtbEntry) { } } end: - // metaDestroyTagIdxKey(pTagIdxKey); + if (terrno != 0) { + ret = terrno; + } tDecoderClear(&dc); tdbFree(pData); return ret; diff --git a/source/dnode/vnode/src/sma/smaRollup.c b/source/dnode/vnode/src/sma/smaRollup.c index ebff03ff99..4acdc3e92c 100644 --- a/source/dnode/vnode/src/sma/smaRollup.c +++ b/source/dnode/vnode/src/sma/smaRollup.c @@ -928,7 +928,7 @@ static int32_t tdAcquireRSmaInfoBySuid(SSma *pSma, int64_t suid, SRSmaInfo **ppR tdRefRSmaInfo(pSma, pRSmaInfo); taosRUnLockLatch(SMA_ENV_LOCK(pEnv)); - if (ASSERTS(pRSmaInfo->suid == suid, "suid:%" PRIi64 " != %" PRIi64, pRSmaInfo->suid, suid)) { + if (pRSmaInfo->suid != suid) { TAOS_RETURN(TSDB_CODE_APP_ERROR); } *ppRSmaInfo = pRSmaInfo; @@ -1645,7 +1645,7 @@ int32_t tdRSmaProcessExecImpl(SSma *pSma, ERsmaExecType type) { ((oldStat == 2) && atomic_load_8(RSMA_TRIGGER_STAT(pRSmaStat)) < TASK_TRIGGER_STAT_PAUSED)) { int32_t oldVal = atomic_fetch_add_32(&pRSmaStat->nFetchAll, 1); - if (ASSERTS(oldVal >= 0, "oldVal of nFetchAll: %d < 0", oldVal)) { + if (oldVal < 0) { code = TSDB_CODE_APP_ERROR; taosHashCancelIterate(infoHash, pIter); TSDB_CHECK_CODE(code, lino, _exit); diff --git a/source/dnode/vnode/src/sma/smaTimeRange.c b/source/dnode/vnode/src/sma/smaTimeRange.c index 96010728c2..9498e8c5ff 100644 --- a/source/dnode/vnode/src/sma/smaTimeRange.c +++ b/source/dnode/vnode/src/sma/smaTimeRange.c @@ -350,8 +350,7 @@ static int32_t tdProcessTSmaInsertImpl(SSma *pSma, int64_t indexUid, const char } } - if (ASSERTS(pTsmaStat->pTSma->indexUid == indexUid, "indexUid:%" PRIi64 " != %" PRIi64, pTsmaStat->pTSma->indexUid, - indexUid)) { + if (pTsmaStat->pTSma->indexUid != indexUid) { code = TSDB_CODE_APP_ERROR; TSDB_CHECK_CODE(code, lino, _exit); } diff --git a/source/dnode/vnode/src/tq/tqScan.c b/source/dnode/vnode/src/tq/tqScan.c index 4357456790..8ca50f9de7 100644 --- a/source/dnode/vnode/src/tq/tqScan.c +++ b/source/dnode/vnode/src/tq/tqScan.c @@ -29,6 +29,10 @@ int32_t tqAddBlockDataToRsp(const SSDataBlock* pBlock, void* pRsp, int32_t numOf pRetrieve->numOfRows = htobe64((int64_t)pBlock->info.rows); int32_t actualLen = blockEncode(pBlock, pRetrieve->data, numOfCols); + if(actualLen < 0){ + taosMemoryFree(buf); + return terrno; + } actualLen += sizeof(SRetrieveTableRspForTmq); if (taosArrayPush(((SMqDataRspCommon*)pRsp)->blockDataLen, &actualLen) == NULL){ taosMemoryFree(buf); diff --git a/source/dnode/vnode/src/tsdb/tsdbCache.c b/source/dnode/vnode/src/tsdb/tsdbCache.c index 20d4363c73..ae57576279 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCache.c +++ b/source/dnode/vnode/src/tsdb/tsdbCache.c @@ -3162,6 +3162,7 @@ static int32_t mergeLastCid(tb_uid_t uid, STsdb *pTsdb, SArray **ppLastArray, SC SLastCol lastCol = {.rowKey = rowKey.key, .colVal = *pColVal, .cacheStatus = TSDB_LAST_CACHE_VALID}; TAOS_CHECK_GOTO(tsdbCacheReallocSLastCol(&lastCol, NULL), &lino, _err); + tsdbCacheFreeSLastColItem(lastColVal); taosArraySet(pColArray, iCol, &lastCol); int32_t aColIndex = taosArraySearchIdx(aColArray, &lastCol.colVal.cid, compareInt16Val, TD_EQ); if (aColIndex >= 0) { diff --git a/source/dnode/vnode/src/tsdb/tsdbRead2.c b/source/dnode/vnode/src/tsdb/tsdbRead2.c index 9be2c3b3f6..391b7f636d 100644 --- a/source/dnode/vnode/src/tsdb/tsdbRead2.c +++ b/source/dnode/vnode/src/tsdb/tsdbRead2.c @@ -137,7 +137,6 @@ static int32_t tGetPrimaryKeyIndex(uint8_t* p, SPrimaryKeyIndex* index) { static void tRowGetPrimaryKeyDeepCopy(SRow* pRow, SRowKey* pKey) { SPrimaryKeyIndex indices[TD_MAX_PK_COLS]; - ASSERT(pKey->numOfPKs <= TD_MAX_PK_COLS); uint8_t* data = pRow->data; for (int32_t i = 0; i < pRow->numOfPKs; i++) { @@ -673,7 +672,10 @@ static int32_t doLoadBlockIndex(STsdbReader* pReader, SDataFileReader* pFileRead break; } - ASSERT(pBrinBlk->minTbid.suid <= pReader->info.suid && pBrinBlk->maxTbid.suid >= pReader->info.suid); + if (!(pBrinBlk->minTbid.suid <= pReader->info.suid && pBrinBlk->maxTbid.suid >= pReader->info.suid)) { + tsdbError("tsdb failed at: %s %d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; + } if (pBrinBlk->maxTbid.suid == pReader->info.suid && pBrinBlk->maxTbid.uid < pList->tableUidList[0]) { i += 1; continue; @@ -753,7 +755,10 @@ static int32_t loadFileBlockBrinInfo(STsdbReader* pReader, SArray* pIndexList, S continue; } - ASSERT(pRecord->suid == pReader->info.suid && uid == pRecord->uid); + if (!(pRecord->suid == pReader->info.suid && uid == pRecord->uid)) { + tsdbError("tsdb failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; + } STableBlockScanInfo* pScanInfo = NULL; code = getTableBlockScanInfo(pReader->status.pTableMap, uid, &pScanInfo, pReader->idStr); @@ -924,21 +929,23 @@ static int32_t getCurrentBlockInfo(SDataBlockIter* pBlockIter, SFileDataBlockInf size_t num = TARRAY_SIZE(pBlockIter->blockList); if (num == 0) { - ASSERT(pBlockIter->numOfBlocks == num); - return TSDB_CODE_FAILED; + tsdbError("tsdb read failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; } *pInfo = taosArrayGet(pBlockIter->blockList, pBlockIter->index); return (*pInfo) != NULL? TSDB_CODE_SUCCESS:TSDB_CODE_FAILED; } -static int doBinarySearchKey(const TSKEY* keyList, int num, int pos, TSKEY key, int order) { +static int32_t doBinarySearchKey(const TSKEY* keyList, int num, int pos, TSKEY key, int order) { // start end position int s, e; s = pos; // check - ASSERT(pos >= 0 && pos < num && num > 0); + if (!(pos >= 0 && pos < num && num > 0)) { + return -1; + } if (order == TSDB_ORDER_ASC) { // find the first position which is smaller than the key e = num - 1; @@ -1242,7 +1249,10 @@ static int32_t copyBlockDataToSDataBlock(STsdbReader* pReader, SRowKey* pLastPro } pDumpInfo->rowIndex = findFirstPos(pBlockData->aTSKEY, pRecord->numRow, pDumpInfo->rowIndex, (!asc)); - ASSERT(pReader->info.verRange.minVer <= pRecord->maxVer && pReader->info.verRange.maxVer >= pRecord->minVer); + if (!(pReader->info.verRange.minVer <= pRecord->maxVer && pReader->info.verRange.maxVer >= pRecord->minVer)) { + tsdbError("tsdb failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INVALID_PARA; + } // find the appropriate start position that satisfies the version requirement. if ((pReader->info.verRange.maxVer >= pRecord->minVer && pReader->info.verRange.maxVer < pRecord->maxVer) || @@ -1382,7 +1392,12 @@ static int32_t copyBlockDataToSDataBlock(STsdbReader* pReader, SRowKey* pLastPro } static FORCE_INLINE STSchema* getTableSchemaImpl(STsdbReader* pReader, uint64_t uid) { - ASSERT(pReader->info.pSchema == NULL); + if (pReader->info.pSchema != NULL) { + terrno = TSDB_CODE_INVALID_PARA; + tsdbError("tsdb invalid input param at: %s:%d", __func__, __LINE__); + return NULL; + } + int32_t code = metaGetTbTSchemaEx(pReader->pTsdb->pVnode->pMeta, pReader->info.suid, uid, -1, &pReader->info.pSchema); if (code != TSDB_CODE_SUCCESS || pReader->info.pSchema == NULL) { terrno = code; @@ -1663,7 +1678,6 @@ static void getBlockToLoadInfo(SDataBlockToLoadInfo* pInfo, SFileDataBlockInfo* pInfo->overlapWithDelInfo = overlapWithDelSkyline(pScanInfo, &pRecord, order); // todo handle the primary key overlap case - ASSERT(pScanInfo->sttKeyInfo.status != STT_FILE_READER_UNINIT); if (pScanInfo->sttKeyInfo.status == STT_FILE_HAS_DATA) { int64_t nextProcKeyInStt = pScanInfo->sttKeyInfo.nextProcKey.ts; pInfo->overlapWithSttBlock = !(pBlockInfo->lastKey < nextProcKeyInStt || pBlockInfo->firstKey > nextProcKeyInStt); @@ -1921,7 +1935,10 @@ static int32_t doMergeBufAndFileRows(STsdbReader* pReader, STableBlockScanInfo* // merge is not initialized yet, due to the fact that the pReader->info.pSchema is not initialized if (pMerger->pArray == NULL) { - ASSERT(pReader->info.pSchema == NULL); + if (pReader->info.pSchema != NULL) { + tsdbError("tsdb failed at %s:%d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; + } STSchema* ps = getTableSchemaImpl(pReader, pBlockScanInfo->uid); if (ps == NULL) { return terrno; @@ -2014,7 +2031,10 @@ static int32_t mergeFileBlockAndSttBlock(STsdbReader* pReader, SSttBlockReader* // merge is not initialized yet, due to the fact that the pReader->info.pSchema is not initialized if (pMerger->pArray == NULL) { - ASSERT(pReader->info.pSchema == NULL); + if (pReader->info.pSchema) { + tsdbError("tsdb failed at %s %d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; + } STSchema* ps = getTableSchemaImpl(pReader, pBlockScanInfo->uid); if (ps == NULL) { return terrno; @@ -2147,7 +2167,10 @@ static int32_t doMergeMultiLevelRows(STsdbReader* pReader, STableBlockScanInfo* // merge is not initialized yet, due to the fact that the pReader->info.pSchema is not initialized if (pMerger->pArray == NULL) { - ASSERT(pReader->info.pSchema == NULL); + if (pReader->info.pSchema != NULL) { + tsdbError("tsdb read failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; + } STSchema* ps = getTableSchemaImpl(pReader, pBlockScanInfo->uid); if (ps == NULL) { return terrno; @@ -2548,7 +2571,10 @@ int32_t mergeRowsInFileBlocks(SBlockData* pBlockData, STableBlockScanInfo* pBloc // merge is not initialized yet, due to the fact that the pReader->info.pSchema is not initialized if (pMerger->pArray == NULL) { - ASSERT(pReader->info.pSchema == NULL); + if (pReader->info.pSchema != NULL) { + tsdbError("tsdb reader failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; + } STSchema* ps = getTableSchemaImpl(pReader, pBlockScanInfo->uid); if (ps == NULL) { return terrno; @@ -3267,8 +3293,6 @@ static int32_t doLoadSttBlockSequentially(STsdbReader* pReader) { // current active data block not overlap with the stt-files/stt-blocks static bool notOverlapWithFiles(SFileDataBlockInfo* pBlockInfo, STableBlockScanInfo* pScanInfo, bool asc) { - ASSERT(pScanInfo->sttKeyInfo.status != STT_FILE_READER_UNINIT); - if ((!hasDataInSttBlock(pScanInfo)) || (pScanInfo->cleanSttBlocks == true)) { return true; } else { @@ -3336,7 +3360,10 @@ static int32_t doBuildDataBlock(STsdbReader* pReader) { // data block, so the overlap check is invalid actually. buildCleanBlockFromDataFiles(pReader, pScanInfo, pBlockInfo, pBlockIter->index); } else { // clean stt block - ASSERT(pReader->info.execMode == READER_EXEC_ROWS && pSttBlockReader->mergeTree.pIter == NULL); + if (!(pReader->info.execMode == READER_EXEC_ROWS && pSttBlockReader->mergeTree.pIter == NULL)) { + tsdbError("tsdb reader failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; + } code = buildCleanBlockFromSttFiles(pReader, pScanInfo); return code; } @@ -3357,7 +3384,10 @@ static int32_t doBuildDataBlock(STsdbReader* pReader) { // no data in stt block, no need to proceed. while (hasDataInSttBlock(pScanInfo)) { - ASSERT(pScanInfo->sttKeyInfo.status == STT_FILE_HAS_DATA); + if (pScanInfo->sttKeyInfo.status != STT_FILE_HAS_DATA) { + tsdbError("tsdb reader failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; + } code = buildComposedDataBlockImpl(pReader, pScanInfo, &pReader->status.fileBlockData, pSttBlockReader); if (code != TSDB_CODE_SUCCESS) { @@ -3580,7 +3610,11 @@ static ERetrieveType doReadDataFromSttFiles(STsdbReader* pReader) { } // all data blocks are checked in this stt file, now let's try the next file set - ASSERT(pReader->status.pTableIter == NULL); + if (pReader->status.pTableIter != NULL) { + terrno = TSDB_CODE_INTERNAL_ERROR; + tsdbError("tsdb reader failed at: %s:%d", __func__, __LINE__); + return TSDB_READ_RETURN; + } code = initForFirstBlockInFile(pReader, pBlockIter); // error happens or all the data files are completely checked @@ -3794,7 +3828,7 @@ bool hasBeenDropped(const SArray* pDelList, int32_t* index, int64_t key, int64_t return false; } - ASSERT(key >= last->ts); + // ASSERT(key >= last->ts); if (key > last->ts) { return false; } else if (key == last->ts) { @@ -3857,7 +3891,7 @@ bool hasBeenDropped(const SArray* pDelList, int32_t* index, int64_t key, int64_t } else if (key == pFirst->ts) { return pFirst->version >= ver; } else { - ASSERT(0); + // ASSERT(0); } } else { TSDBKEY* pCurrent = taosArrayGet(pDelList, *index); diff --git a/source/dnode/vnode/src/tsdb/tsdbReadUtil.c b/source/dnode/vnode/src/tsdb/tsdbReadUtil.c index 57405c0e48..5d4dc94431 100644 --- a/source/dnode/vnode/src/tsdb/tsdbReadUtil.c +++ b/source/dnode/vnode/src/tsdb/tsdbReadUtil.c @@ -741,7 +741,10 @@ int32_t initBlockIterator(STsdbReader* pReader, SDataBlockIter* pBlockIter, int3 } numOfTotal += 1; - tMergeTreeAdjust(pTree, tMergeTreeGetAdjustIndex(pTree)); + code = tMergeTreeAdjust(pTree, tMergeTreeGetAdjustIndex(pTree)); + if (TSDB_CODE_SUCCESS != code) { + return code; + } } for (int32_t i = 0; i < numOfTables; ++i) { @@ -843,7 +846,10 @@ static int32_t doCheckTombBlock(STombBlock* pBlock, STsdbReader* pReader, int32_ continue; } - ASSERT(record.suid == pReader->info.suid && uid == record.uid); + if (!(record.suid == pReader->info.suid && uid == record.uid)) { + tsdbError("tsdb reader failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; + } if (record.version <= pReader->info.verRange.maxVer) { SDelData delData = {.version = record.version, .sKey = record.skey, .eKey = record.ekey}; @@ -877,7 +883,10 @@ static int32_t doLoadTombDataFromTombBlk(const TTombBlkArray* pTombBlkArray, STs break; } - ASSERT(pTombBlk->minTbid.suid <= pReader->info.suid && pTombBlk->maxTbid.suid >= pReader->info.suid); + if (!(pTombBlk->minTbid.suid <= pReader->info.suid && pTombBlk->maxTbid.suid >= pReader->info.suid)) { + tsdbError("tsdb reader failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; + } if (pTombBlk->maxTbid.suid == pReader->info.suid && pTombBlk->maxTbid.uid < pList->tableUidList[0]) { i += 1; continue; diff --git a/source/dnode/vnode/src/vnd/vnodeQuery.c b/source/dnode/vnode/src/vnd/vnodeQuery.c index 5849ef9fcc..e080463fc8 100644 --- a/source/dnode/vnode/src/vnd/vnodeQuery.c +++ b/source/dnode/vnode/src/vnd/vnodeQuery.c @@ -129,7 +129,6 @@ int32_t vnodeGetTableMeta(SVnode *pVnode, SRpcMsg *pMsg, bool direct) { SMetaReader *pReader = mer1.me.type == TSDB_CHILD_TABLE ? &mer2 : &mer1; code = fillTableColCmpr(pReader, metaRsp.pSchemaExt, metaRsp.numOfColumns); if (code < 0) { - code = TSDB_CODE_INVALID_MSG; goto _exit; } } else { @@ -545,7 +544,7 @@ int32_t vnodeGetTableList(void *pVnode, int8_t type, SArray *pList) { } int32_t vnodeGetAllTableList(SVnode *pVnode, uint64_t uid, SArray *list) { - int32_t code = TSDB_CODE_SUCCESS; + int32_t code = TSDB_CODE_SUCCESS; SMCtbCursor *pCur = metaOpenCtbCursor(pVnode, uid, 1); if (NULL == pCur) { qError("vnode get all table list failed"); @@ -575,7 +574,7 @@ int32_t vnodeGetCtbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bo } int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) { - int32_t code = TSDB_CODE_SUCCESS; + int32_t code = TSDB_CODE_SUCCESS; SVnode *pVnodeObj = pVnode; SMCtbCursor *pCur = metaOpenCtbCursor(pVnodeObj, suid, 1); if (NULL == pCur) { @@ -589,7 +588,7 @@ int32_t vnodeGetCtbIdList(void *pVnode, int64_t suid, SArray *list) { break; } - if (NULL == taosArrayPush(list, &id)) { + if (NULL == taosArrayPush(list, &id)) { qError("taosArrayPush failed"); code = TSDB_CODE_OUT_OF_MEMORY; goto _exit; @@ -602,7 +601,7 @@ _exit: } int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) { - int32_t code = TSDB_CODE_SUCCESS; + int32_t code = TSDB_CODE_SUCCESS; SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid); if (!pCur) { return TSDB_CODE_OUT_OF_MEMORY; @@ -614,7 +613,7 @@ int32_t vnodeGetStbIdList(SVnode *pVnode, int64_t suid, SArray *list) { break; } - if (NULL == taosArrayPush(list, &id)) { + if (NULL == taosArrayPush(list, &id)) { qError("taosArrayPush failed"); code = TSDB_CODE_OUT_OF_MEMORY; goto _exit; @@ -628,7 +627,7 @@ _exit: int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bool (*filter)(void *arg, void *arg1), void *arg) { - int32_t code = TSDB_CODE_SUCCESS; + int32_t code = TSDB_CODE_SUCCESS; SMStbCursor *pCur = metaOpenStbCursor(pVnode->pMeta, suid); if (!pCur) { return terrno; @@ -644,7 +643,7 @@ int32_t vnodeGetStbIdListByFilter(SVnode *pVnode, int64_t suid, SArray *list, bo continue; } - if (NULL == taosArrayPush(list, &id)) { + if (NULL == taosArrayPush(list, &id)) { qError("taosArrayPush failed"); code = TSDB_CODE_OUT_OF_MEMORY; goto _exit; diff --git a/source/libs/command/src/command.c b/source/libs/command/src/command.c index e3a6168bab..48256c744a 100644 --- a/source/libs/command/src/command.c +++ b/source/libs/command/src/command.c @@ -24,13 +24,13 @@ #include "tgrant.h" #define COL_DATA_SET_VAL_AND_CHECK(pCol, rows, buf, isNull) \ - do { \ - int _code = colDataSetVal(pCol, rows, buf, isNull);\ - if (TSDB_CODE_SUCCESS != _code) { \ - terrno = _code; \ - return _code; \ - } \ - } while(0) + do { \ + int _code = colDataSetVal(pCol, rows, buf, isNull); \ + if (TSDB_CODE_SUCCESS != _code) { \ + terrno = _code; \ + return _code; \ + } \ + } while (0) extern SConfig* tsCfg; @@ -50,6 +50,10 @@ static int32_t buildRetrieveTableRsp(SSDataBlock* pBlock, int32_t numOfCols, SRe (*pRsp)->numOfCols = htonl(numOfCols); int32_t len = blockEncode(pBlock, (*pRsp)->data + PAYLOAD_PREFIX_LEN, numOfCols); + if(len < 0) { + taosMemoryFree(*pRsp); + return terrno; + } SET_PAYLOAD_LEN((*pRsp)->data, len, len); int32_t payloadLen = len + PAYLOAD_PREFIX_LEN; @@ -77,7 +81,7 @@ static int32_t buildDescResultDataBlock(SSDataBlock** pOutput) { QRY_OPTR_CHECK(pOutput); SSDataBlock* pBlock = NULL; - int32_t code = createDataBlock(&pBlock); + int32_t code = createDataBlock(&pBlock); if (code) { return code; } @@ -235,7 +239,7 @@ static int32_t buildCreateDBResultDataBlock(SSDataBlock** pOutput) { QRY_OPTR_CHECK(pOutput); SSDataBlock* pBlock = NULL; - int32_t code = createDataBlock(&pBlock); + int32_t code = createDataBlock(&pBlock); if (code) { return code; } @@ -280,15 +284,15 @@ int64_t getValOfDiffPrecision(int8_t unit, int64_t val) { return v; } -static int32_t buildRetension(SArray* pRetension, char **ppRetentions ) { +static int32_t buildRetension(SArray* pRetension, char** ppRetentions) { size_t size = taosArrayGetSize(pRetension); if (size == 0) { *ppRetentions = NULL; return TSDB_CODE_SUCCESS; } - char* p1 = taosMemoryCalloc(1, 100); - if(NULL == p1) { + char* p1 = taosMemoryCalloc(1, 100); + if (NULL == p1) { return terrno; } int32_t len = 0; @@ -368,7 +372,7 @@ static int32_t setCreateDBResultIntoDataBlock(SSDataBlock* pBlock, char* dbName, break; } - char* pRetentions = NULL; + char* pRetentions = NULL; QRY_ERR_RET(buildRetension(pCfg->pRetensions, &pRetentions)); int32_t dbFNameLen = strlen(dbFName); int32_t hashPrefix = 0; @@ -427,7 +431,7 @@ static int32_t buildCreateTbResultDataBlock(SSDataBlock** pOutput) { QRY_OPTR_CHECK(pOutput); SSDataBlock* pBlock = NULL; - int32_t code = createDataBlock(&pBlock); + int32_t code = createDataBlock(&pBlock); if (code) { return code; } @@ -451,7 +455,7 @@ static int32_t buildCreateViewResultDataBlock(SSDataBlock** pOutput) { QRY_OPTR_CHECK(pOutput); SSDataBlock* pBlock = NULL; - int32_t code = createDataBlock(&pBlock); + int32_t code = createDataBlock(&pBlock); if (code) { return code; } @@ -578,36 +582,7 @@ int32_t appendTagValues(char* buf, int32_t* len, STableCfg* pCfg) { } else { *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, "NULL"); } - - /* - if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_GEOMETRY) { - if (pTagVal->nData > 0) { - if (num) { - *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ", "); - } - - memcpy(buf + VARSTR_HEADER_SIZE + *len, pTagVal->pData, pTagVal->nData); - *len += pTagVal->nData; - } - } else if (type == TSDB_DATA_TYPE_NCHAR) { - if (pTagVal->nData > 0) { - if (num) { - *len += sprintf(buf + VARSTR_HEADER_SIZE + *len, ", "); - } - int32_t tlen = taosUcs4ToMbs((TdUcs4 *)pTagVal->pData, pTagVal->nData, buf + VARSTR_HEADER_SIZE + *len); - } - } else if (type == TSDB_DATA_TYPE_DOUBLE) { - double val = *(double *)(&pTagVal->i64); - int len = 0; - term = indexTermCreate(suid, ADD_VALUE, type, key, nKey, (const char *)&val, len); - } else if (type == TSDB_DATA_TYPE_BOOL) { - int val = *(int *)(&pTagVal->i64); - int len = 0; - term = indexTermCreate(suid, ADD_VALUE, TSDB_DATA_TYPE_INT, key, nKey, (const char *)&val, len); - } - */ } - _exit: taosArrayDestroy(pTagVals); @@ -867,26 +842,26 @@ static int32_t buildLocalVariablesResultDataBlock(SSDataBlock** pOutput) { infoData.info.type = TSDB_DATA_TYPE_VARCHAR; infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD1_LEN; - if(taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) { + if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) { goto _exit; } infoData.info.type = TSDB_DATA_TYPE_VARCHAR; infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD2_LEN; - if(taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) { + if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) { goto _exit; } infoData.info.type = TSDB_DATA_TYPE_VARCHAR; infoData.info.bytes = SHOW_LOCAL_VARIABLES_RESULT_FIELD3_LEN; - if(taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) { + if (taosArrayPush(pBlock->pDataBlock, &infoData) == NULL) { goto _exit; } *pOutput = pBlock; _exit: - if(terrno != TSDB_CODE_SUCCESS) { + if (terrno != TSDB_CODE_SUCCESS) { taosMemoryFree(pBlock); taosArrayDestroy(pBlock->pDataBlock); } @@ -910,7 +885,7 @@ static int32_t createSelectResultDataBlock(SNodeList* pProjects, SSDataBlock** p QRY_OPTR_CHECK(pOutput); SSDataBlock* pBlock = NULL; - int32_t code = createDataBlock(&pBlock); + int32_t code = createDataBlock(&pBlock); if (code) { return code; } @@ -945,7 +920,8 @@ int32_t buildSelectResultDataBlock(SNodeList* pProjects, SSDataBlock* pBlock) { if (((SValueNode*)pProj)->isNull) { QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, NULL, true)); } else { - QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, nodesGetValueFromNode((SValueNode*)pProj), false)); + QRY_ERR_RET(colDataSetVal(taosArrayGet(pBlock->pDataBlock, index++), 0, + nodesGetValueFromNode((SValueNode*)pProj), false)); } } } diff --git a/source/libs/command/src/explain.c b/source/libs/command/src/explain.c index 3a73c05de2..b9f79d1e00 100644 --- a/source/libs/command/src/explain.c +++ b/source/libs/command/src/explain.c @@ -1978,6 +1978,10 @@ int32_t qExplainGetRspFromCtx(void *ctx, SRetrieveTableRsp **pRsp) { rsp->numOfRows = htobe64((int64_t)rowNum); int32_t len = blockEncode(pBlock, rsp->data + PAYLOAD_PREFIX_LEN, taosArrayGetSize(pBlock->pDataBlock)); + if(len < 0) { + qError("qExplainGetRspFromCtx: blockEncode failed"); + QRY_ERR_JRET(terrno); + } rsp->compLen = htonl(len); rsp->payloadLen = htonl(len); diff --git a/source/libs/executor/src/countwindowoperator.c b/source/libs/executor/src/countwindowoperator.c index f4eb0dd87e..ba07e666a0 100644 --- a/source/libs/executor/src/countwindowoperator.c +++ b/source/libs/executor/src/countwindowoperator.c @@ -32,6 +32,7 @@ typedef struct SCountWindowResult { typedef struct SCountWindowSupp { SArray* pWinStates; int32_t stateIndex; + int32_t curStateIndex; } SCountWindowSupp; typedef struct SCountWindowOperatorInfo { @@ -45,6 +46,8 @@ typedef struct SCountWindowOperatorInfo { int32_t windowCount; int32_t windowSliding; SCountWindowSupp countSup; + SSDataBlock* pPreDataBlock; + int32_t preStateIndex; } SCountWindowOperatorInfo; void destroyCountWindowOperatorInfo(void* param) { @@ -65,6 +68,7 @@ static void clearWinStateBuff(SCountWindowResult* pBuff) { pBuff->winRows = 0; } static SCountWindowResult* getCountWinStateInfo(SCountWindowSupp* pCountSup) { SCountWindowResult* pBuffInfo = taosArrayGet(pCountSup->pWinStates, pCountSup->stateIndex); + pCountSup->curStateIndex = pCountSup->stateIndex; if (!pBuffInfo) { terrno = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(terrno)); @@ -113,6 +117,19 @@ void doCountWindowAggImpl(SOperatorInfo* pOperator, SSDataBlock* pBlock) { SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, pInfo->tsSlotId); QUERY_CHECK_NULL(pColInfoData, code, lino, _end, terrno); TSKEY* tsCols = (TSKEY*)pColInfoData->pData; + int32_t numOfBuff = taosArrayGetSize(pInfo->countSup.pWinStates); + if (numOfBuff == 0) { + code = TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; + qError("%s failed at line %d since %s", __func__, __LINE__, tstrerror(code)); + T_LONG_JMP(pTaskInfo->env, code); + } + pInfo->countSup.stateIndex = (pInfo->preStateIndex + 1) % numOfBuff; + + int32_t newSize = pRes->info.rows + pBlock->info.rows / pInfo->windowSliding + 1; + if (newSize > pRes->info.capacity) { + code = blockDataEnsureCapacity(pRes, newSize); + QUERY_CHECK_CODE(code, lino, _end); + } for (int32_t i = 0; i < pBlock->info.rows;) { SCountWindowResult* pBuffInfo = NULL; @@ -132,14 +149,6 @@ void doCountWindowAggImpl(SOperatorInfo* pOperator, SSDataBlock* pBlock) { updateTimeWindowInfo(&pInfo->twAggSup.timeWindowData, &pInfo->pRow->win, 0); applyAggFunctionOnPartialTuples(pTaskInfo, pExprSup->pCtx, &pInfo->twAggSup.timeWindowData, i, num, pBlock->info.rows, pExprSup->numOfExprs); - if (pBuffInfo->winRows == pInfo->windowCount) { - doUpdateNumOfRows(pExprSup->pCtx, pInfo->pRow, pExprSup->numOfExprs, pExprSup->rowEntryInfoOffset); - copyResultrowToDataBlock(pExprSup->pExprInfo, pExprSup->numOfExprs, pInfo->pRow, pExprSup->pCtx, pRes, - pExprSup->rowEntryInfoOffset, pTaskInfo); - pRes->info.rows += pInfo->pRow->numOfRows; - clearWinStateBuff(pBuffInfo); - clearResultRowInitFlag(pExprSup->pCtx, pExprSup->numOfExprs); - } if (pInfo->windowCount != pInfo->windowSliding) { if (prevRows <= pInfo->windowSliding) { if (pBuffInfo->winRows > pInfo->windowSliding) { @@ -151,9 +160,21 @@ void doCountWindowAggImpl(SOperatorInfo* pOperator, SSDataBlock* pBlock) { step = 0; } } + if (pBuffInfo->winRows == pInfo->windowCount) { + doUpdateNumOfRows(pExprSup->pCtx, pInfo->pRow, pExprSup->numOfExprs, pExprSup->rowEntryInfoOffset); + copyResultrowToDataBlock(pExprSup->pExprInfo, pExprSup->numOfExprs, pInfo->pRow, pExprSup->pCtx, pRes, + pExprSup->rowEntryInfoOffset, pTaskInfo); + pRes->info.rows += pInfo->pRow->numOfRows; + clearWinStateBuff(pBuffInfo); + pInfo->preStateIndex = pInfo->countSup.curStateIndex; + clearResultRowInitFlag(pExprSup->pCtx, pExprSup->numOfExprs); + } i += step; } + code = doFilter(pRes, pOperator->exprSupp.pFilterInfo, NULL); + QUERY_CHECK_CODE(code, lino, _end); + _end: if (code != TSDB_CODE_SUCCESS) { qError("%s failed at line %d since %s", __func__, lino, tstrerror(code)); @@ -163,11 +184,18 @@ _end: } static void buildCountResult(SExprSupp* pExprSup, SCountWindowSupp* pCountSup, SExecTaskInfo* pTaskInfo, - SFilterInfo* pFilterInfo, SSDataBlock* pBlock) { + SFilterInfo* pFilterInfo, int32_t preStateIndex, SSDataBlock* pBlock) { SResultRow* pResultRow = NULL; int32_t code = TSDB_CODE_SUCCESS; int32_t lino = 0; - for (int32_t i = 0; i < taosArrayGetSize(pCountSup->pWinStates); i++) { + int32_t numOfBuff = taosArrayGetSize(pCountSup->pWinStates); + int32_t newSize = pBlock->info.rows + numOfBuff; + if (newSize > pBlock->info.capacity) { + code = blockDataEnsureCapacity(pBlock, newSize); + QUERY_CHECK_CODE(code, lino, _end); + } + pCountSup->stateIndex = (preStateIndex + 1) % numOfBuff; + for (int32_t i = 0; i < numOfBuff; i++) { SCountWindowResult* pBuff = NULL; code = setCountWindowOutputBuff(pExprSup, pCountSup, &pResultRow, &pBuff); QUERY_CHECK_CODE(code, lino, _end); @@ -204,7 +232,14 @@ static int32_t countWindowAggregateNext(SOperatorInfo* pOperator, SSDataBlock** blockDataCleanup(pRes); while (1) { - SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0); + SSDataBlock* pBlock = NULL; + if (pInfo->pPreDataBlock == NULL) { + pBlock = getNextBlockFromDownstream(pOperator, 0); + } else { + pBlock = pInfo->pPreDataBlock; + pInfo->pPreDataBlock = NULL; + } + if (pBlock == NULL) { break; } @@ -226,18 +261,26 @@ static int32_t countWindowAggregateNext(SOperatorInfo* pOperator, SSDataBlock** if (pInfo->groupId == 0) { pInfo->groupId = pBlock->info.id.groupId; } else if (pInfo->groupId != pBlock->info.id.groupId) { - buildCountResult(pExprSup, &pInfo->countSup, pTaskInfo, pOperator->exprSupp.pFilterInfo, pRes); + pInfo->pPreDataBlock = pBlock; + pRes->info.id.groupId = pInfo->groupId; + buildCountResult(pExprSup, &pInfo->countSup, pTaskInfo, pOperator->exprSupp.pFilterInfo, pInfo->preStateIndex, pRes); pInfo->groupId = pBlock->info.id.groupId; + if (pRes->info.rows > 0) { + (*ppRes) = pRes; + return code; + } } doCountWindowAggImpl(pOperator, pBlock); if (pRes->info.rows >= pOperator->resultInfo.threshold) { + pRes->info.id.groupId = pInfo->groupId; (*ppRes) = pRes; return code; } } - buildCountResult(pExprSup, &pInfo->countSup, pTaskInfo, pOperator->exprSupp.pFilterInfo, pRes); + pRes->info.id.groupId = pInfo->groupId; + buildCountResult(pExprSup, &pInfo->countSup, pTaskInfo, pOperator->exprSupp.pFilterInfo, pInfo->preStateIndex, pRes); _end: if (code != TSDB_CODE_SUCCESS) { @@ -320,6 +363,8 @@ int32_t createCountwindowOperatorInfo(SOperatorInfo* downstream, SPhysiNode* phy } pInfo->countSup.stateIndex = 0; + pInfo->pPreDataBlock = NULL; + pInfo->preStateIndex = 0; code = filterInitFromNode((SNode*)pCountWindowNode->window.node.pConditions, &pOperator->exprSupp.pFilterInfo, 0); QUERY_CHECK_CODE(code, lino, _error); diff --git a/source/libs/executor/src/dataDispatcher.c b/source/libs/executor/src/dataDispatcher.c index 6eb1f9cd18..fb3ed06224 100644 --- a/source/libs/executor/src/dataDispatcher.c +++ b/source/libs/executor/src/dataDispatcher.c @@ -106,6 +106,10 @@ static int32_t toDataCacheEntry(SDataDispatchHandle* pHandle, const SInputData* } int32_t dataLen = blockEncode(pInput->pData, pHandle->pCompressBuf, numOfCols); + if(dataLen < 0) { + qError("failed to encode data block, code: %d", dataLen); + return terrno; + } int32_t len = tsCompressString(pHandle->pCompressBuf, dataLen, 1, pEntry->data, pBuf->allocSize, ONE_STAGE_COMP, NULL, 0); if (len < dataLen) { @@ -120,6 +124,10 @@ static int32_t toDataCacheEntry(SDataDispatchHandle* pHandle, const SInputData* } } else { pEntry->dataLen = blockEncode(pInput->pData, pEntry->data, numOfCols); + if(pEntry->dataLen < 0) { + qError("failed to encode data block, code: %d", pEntry->dataLen); + return terrno; + } pEntry->rawLen = pEntry->dataLen; } } diff --git a/source/libs/executor/src/eventwindowoperator.c b/source/libs/executor/src/eventwindowoperator.c index b80ea74006..7218291f8c 100644 --- a/source/libs/executor/src/eventwindowoperator.c +++ b/source/libs/executor/src/eventwindowoperator.c @@ -36,6 +36,7 @@ typedef struct SEventWindowOperatorInfo { SFilterInfo* pEndCondInfo; bool inWindow; SResultRow* pRow; + SSDataBlock* pPreDataBlock; } SEventWindowOperatorInfo; static SSDataBlock* eventWindowAggregate(SOperatorInfo* pOperator); @@ -126,6 +127,7 @@ int32_t createEventwindowOperatorInfo(SOperatorInfo* downstream, SPhysiNode* phy QUERY_CHECK_CODE(code, lino, _error); pInfo->tsSlotId = tsSlotId; + pInfo->pPreDataBlock = NULL; setOperatorInfo(pOperator, "EventWindowOperator", QUERY_NODE_PHYSICAL_PLAN_MERGE_STATE, true, OP_NOT_OPENED, pInfo, pTaskInfo); @@ -199,7 +201,14 @@ static int32_t eventWindowAggregateNext(SOperatorInfo* pOperator, SSDataBlock** SOperatorInfo* downstream = pOperator->pDownstream[0]; while (1) { - SSDataBlock* pBlock = getNextBlockFromDownstream(pOperator, 0); + SSDataBlock* pBlock = NULL; + if (pInfo->pPreDataBlock == NULL) { + pBlock = getNextBlockFromDownstream(pOperator, 0); + } else { + pBlock = pInfo->pPreDataBlock; + pInfo->pPreDataBlock = NULL; + } + if (pBlock == NULL) { break; } @@ -224,7 +233,8 @@ static int32_t eventWindowAggregateNext(SOperatorInfo* pOperator, SSDataBlock** code = doFilter(pRes, pSup->pFilterInfo, NULL); QUERY_CHECK_CODE(code, lino, _end); - if (pRes->info.rows >= pOperator->resultInfo.threshold) { + if (pRes->info.rows >= pOperator->resultInfo.threshold || + (pRes->info.id.groupId != pInfo->groupId && pRes->info.rows > 0)) { (*ppRes) = pRes; return code; } @@ -303,7 +313,10 @@ int32_t eventWindowAggImpl(SOperatorInfo* pOperator, SEventWindowOperatorInfo* p // this is a new group, reset the info pInfo->inWindow = false; pInfo->groupId = gid; + pInfo->pPreDataBlock = pBlock; + goto _return; } + pRes->info.id.groupId = pInfo->groupId; SFilterColumnParam param1 = {.numOfCols = taosArrayGetSize(pBlock->pDataBlock), .pDataBlock = pBlock->pDataBlock}; diff --git a/source/libs/executor/src/groupoperator.c b/source/libs/executor/src/groupoperator.c index e5289fa216..959ace1228 100644 --- a/source/libs/executor/src/groupoperator.c +++ b/source/libs/executor/src/groupoperator.c @@ -548,7 +548,7 @@ int32_t createGroupOperatorInfo(SOperatorInfo* downstream, SAggPhysiNode* pAggNo SGroupbyOperatorInfo* pInfo = taosMemoryCalloc(1, sizeof(SGroupbyOperatorInfo)); SOperatorInfo* pOperator = taosMemoryCalloc(1, sizeof(SOperatorInfo)); if (pInfo == NULL || pOperator == NULL) { - code = TSDB_CODE_OUT_OF_MEMORY; + code = terrno; goto _error; } diff --git a/source/libs/executor/src/operator.c b/source/libs/executor/src/operator.c index af52c31364..0ff6870405 100644 --- a/source/libs/executor/src/operator.c +++ b/source/libs/executor/src/operator.c @@ -240,7 +240,12 @@ int32_t getTableScanInfo(SOperatorInfo* pOperator, int32_t* order, int32_t* scan *order = info.order; *scanFlag = info.scanFlag; - ASSERT(*order == TSDB_ORDER_ASC || *order == TSDB_ORDER_DESC); + if (p.code == TSDB_CODE_SUCCESS) { + if (!(*order == TSDB_ORDER_ASC || *order == TSDB_ORDER_DESC)) { + qError("operator failed at: %s:%d", __func__, __LINE__); + p.code = TSDB_CODE_INVALID_PARA; + } + } return p.code; } diff --git a/source/libs/executor/src/projectoperator.c b/source/libs/executor/src/projectoperator.c index dd0acc42ed..bf523af918 100644 --- a/source/libs/executor/src/projectoperator.c +++ b/source/libs/executor/src/projectoperator.c @@ -218,7 +218,10 @@ static int32_t discardGroupDataBlock(SSDataBlock* pBlock, SLimitInfo* pLimitInfo static int32_t setInfoForNewGroup(SSDataBlock* pBlock, SLimitInfo* pLimitInfo, SOperatorInfo* pOperator) { // remainGroupOffset == 0 // here check for a new group data, we need to handle the data of the previous group. - ASSERT(pLimitInfo->remainGroupOffset == 0 || pLimitInfo->remainGroupOffset == -1); + if (!(pLimitInfo->remainGroupOffset == 0 || pLimitInfo->remainGroupOffset == -1)) { + qError("project failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INVALID_PARA; + } bool newGroup = false; if (0 == pBlock->info.id.groupId) { @@ -818,7 +821,10 @@ int32_t doGenerateSourceData(SOperatorInfo* pOperator) { } int32_t startOffset = pRes->info.rows; - ASSERT(pRes->info.capacity > 0); + if (pRes->info.capacity <= 0) { + qError("project failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; + } code = colDataAssign(pResColData, &idata, dest.numOfRows, &pRes->info); if (code) { return code; @@ -875,7 +881,11 @@ int32_t projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBloc for (int32_t k = 0; k < numOfOutput; ++k) { int32_t outputSlotId = pExpr[k].base.resSchema.slotId; - ASSERT(pExpr[k].pExpr->nodeType == QUERY_NODE_VALUE); + if (pExpr[k].pExpr->nodeType != QUERY_NODE_VALUE) { + qError("project failed at: %s:%d", __func__, __LINE__); + code = TSDB_CODE_INVALID_PARA; + TSDB_CHECK_CODE(code, lino, _exit); + } SColumnInfoData* pColInfoData = taosArrayGet(pResult->pDataBlock, outputSlotId); if (pColInfoData == NULL) { code = terrno; @@ -1019,7 +1029,11 @@ int32_t projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBloc } int32_t startOffset = createNewColModel ? 0 : pResult->info.rows; - ASSERT(pResult->info.capacity > 0); + if (pResult->info.capacity <= 0) { + qError("project failed at: %s:%d", __func__, __LINE__); + code = TSDB_CODE_INVALID_PARA; + TSDB_CHECK_CODE(code, lino, _exit); + } int32_t ret = colDataMergeCol(pResColData, startOffset, (int32_t*)&pResult->info.capacity, &idata, dest.numOfRows); if (ret < 0) { @@ -1146,7 +1160,11 @@ int32_t projectApplyFunctions(SExprInfo* pExpr, SSDataBlock* pResult, SSDataBloc } int32_t startOffset = createNewColModel ? 0 : pResult->info.rows; - ASSERT(pResult->info.capacity > 0); + if (pResult->info.capacity <= 0) { + qError("project failed at: %s:%d", __func__, __LINE__); + code = TSDB_CODE_INVALID_PARA; + TSDB_CHECK_CODE(code, lino, _exit); + } int32_t ret = colDataMergeCol(pResColData, startOffset, (int32_t*)&pResult->info.capacity, &idata, dest.numOfRows); if (ret < 0) { code = ret; diff --git a/source/libs/executor/src/scanoperator.c b/source/libs/executor/src/scanoperator.c index b7b5e4fff9..8a0cd143ed 100644 --- a/source/libs/executor/src/scanoperator.c +++ b/source/libs/executor/src/scanoperator.c @@ -592,6 +592,7 @@ int32_t addTagPseudoColumnData(SReadHandle* pHandle, const SExprInfo* pExpr, int SMetaReader mr = {0}; const char* idStr = pTask->id.str; int32_t insertRet = TAOS_LRU_STATUS_OK; + STableCachedVal* pVal = NULL; // currently only the tbname pseudo column if (numOfExpr <= 0) { @@ -656,19 +657,11 @@ int32_t addTagPseudoColumnData(SReadHandle* pHandle, const SExprInfo* pExpr, int pHandle->api.metaReaderFn.readerReleaseLock(&mr); - STableCachedVal* pVal = NULL; code = createTableCacheVal(&mr, &pVal); QUERY_CHECK_CODE(code, lino, _end); val = *pVal; freeReader = true; - - insertRet = taosLRUCacheInsert(pCache->pTableMetaEntryCache, &pBlock->info.id.uid, sizeof(uint64_t), pVal, - sizeof(STableCachedVal), freeCachedMetaItem, NULL, TAOS_LRU_PRIORITY_LOW, NULL); - if (insertRet != TAOS_LRU_STATUS_OK) { - qError("failed to put meta into lru cache, code:%d, %s", insertRet, idStr); - taosMemoryFreeClear(pVal); - } } else { pCache->cacheHit += 1; STableCachedVal* pVal = taosLRUCacheValue(pCache->pTableMetaEntryCache, h); @@ -742,9 +735,17 @@ int32_t addTagPseudoColumnData(SReadHandle* pHandle, const SExprInfo* pExpr, int pBlock->info.rows = backupRows; _end: - if (insertRet != TAOS_LRU_STATUS_OK) { - freeTableCachedValObj(&val); + + if (NULL != pVal) { + insertRet = taosLRUCacheInsert(pCache->pTableMetaEntryCache, &pBlock->info.id.uid, sizeof(uint64_t), pVal, + sizeof(STableCachedVal), freeCachedMetaItem, NULL, TAOS_LRU_PRIORITY_LOW, NULL); + if (insertRet != TAOS_LRU_STATUS_OK) { + qError("failed to put meta into lru cache, code:%d, %s", insertRet, idStr); + taosMemoryFreeClear(pVal); + freeTableCachedValObj(&val); + } } + if (freeReader) { pHandle->api.metaReaderFn.clearReader(&mr); } @@ -5118,9 +5119,7 @@ static int32_t adjustSubTableForNextRow(SOperatorInfo* pOperatorInfo, STmsSubTab } } - tMergeTreeAdjust(pSubTblsInfo->pTree, tMergeTreeGetAdjustIndex(pSubTblsInfo->pTree)); - - return TSDB_CODE_SUCCESS; + return tMergeTreeAdjust(pSubTblsInfo->pTree, tMergeTreeGetAdjustIndex(pSubTblsInfo->pTree)); } static int32_t appendChosenRowToDataBlock(STmsSubTablesMergeInfo* pSubTblsInfo, SSDataBlock* pBlock) { diff --git a/source/libs/executor/src/sortoperator.c b/source/libs/executor/src/sortoperator.c index e23988d357..d9b1e40510 100644 --- a/source/libs/executor/src/sortoperator.c +++ b/source/libs/executor/src/sortoperator.c @@ -295,6 +295,9 @@ static int32_t getSortedBlockData(SSortHandle* pHandle, SSDataBlock* pDataBlock, break; } } + if (TSDB_CODE_SUCCESS != code) { + return code; + } if (p->info.rows > 0) { code = blockDataEnsureCapacity(pDataBlock, capacity); diff --git a/source/libs/executor/src/timesliceoperator.c b/source/libs/executor/src/timesliceoperator.c index f48393273f..7f23255257 100644 --- a/source/libs/executor/src/timesliceoperator.c +++ b/source/libs/executor/src/timesliceoperator.c @@ -1247,6 +1247,9 @@ void destroyTimeSliceOperatorInfo(void* param) { taosMemoryFree(pInfo->pPrevGroupKey->pData); taosMemoryFree(pInfo->pPrevGroupKey); } + if (pInfo->hasPk && IS_VAR_DATA_TYPE(pInfo->pkCol.type)) { + taosMemoryFreeClear(pInfo->prevKey.pks[0].pData); + } cleanupExprSupp(&pInfo->scalarSup); if (pInfo->pFillColInfo != NULL) { diff --git a/source/libs/executor/src/tlinearhash.c b/source/libs/executor/src/tlinearhash.c index 0c70428e78..55628c18f9 100644 --- a/source/libs/executor/src/tlinearhash.c +++ b/source/libs/executor/src/tlinearhash.c @@ -14,6 +14,7 @@ */ #include "tlinearhash.h" +#include "query.h" #include "taoserror.h" #include "tdef.h" #include "tpagedbuf.h" @@ -59,7 +60,11 @@ static int32_t doGetBucketIdFromHashVal(int32_t hashv, int32_t bits) { return ha static int32_t doGetAlternativeBucketId(int32_t bucketId, int32_t bits, int32_t numOfBuckets) { int32_t v = bucketId - (1ul << (bits - 1)); - ASSERT(v < numOfBuckets); + if (v >= numOfBuckets) { + qError("tlinearhash failed at: %s:%d", __func__, __LINE__); + terrno = TSDB_CODE_INTERNAL_ERROR; + return -1; + } return v; } @@ -85,11 +90,15 @@ static int32_t doAddToBucket(SLHashObj* pHashObj, SLHashBucket* pBucket, int32_t int32_t pageId = *(int32_t*)taosArrayGetLast(pBucket->pPageIdList); SFilePage* pPage = getBufPage(pHashObj->pBuf, pageId); - ASSERT(pPage != NULL); + if (pPage == NULL) { + return TSDB_CODE_INVALID_PARA; + } // put to current buf page size_t nodeSize = sizeof(SLHashNode) + keyLen + size; - ASSERT(nodeSize + sizeof(SFilePage) <= getBufPageSize(pHashObj->pBuf)); + if (nodeSize + sizeof(SFilePage) > getBufPageSize(pHashObj->pBuf)) { + return TSDB_CODE_INVALID_PARA; + } if (pPage->num + nodeSize > getBufPageSize(pHashObj->pBuf)) { releaseBufPage(pHashObj->pBuf, pPage); @@ -174,7 +183,7 @@ static void doTrimBucketPages(SLHashObj* pHashObj, SLHashBucket* pBucket) { setBufPageDirty(pFirst, true); setBufPageDirty(pLast, true); - ASSERT(pLast->num >= nodeSize + sizeof(SFilePage)); + // ASSERT(pLast->num >= nodeSize + sizeof(SFilePage)); pFirst->num += nodeSize; pLast->num -= nodeSize; @@ -320,6 +329,9 @@ int32_t tHashPut(SLHashObj* pHashObj, const void* key, size_t keyLen, void* data // printf("bucketId: 0x%x not exists, put it into 0x%x instead\n", v, newBucketId); v = newBucketId; } + if (v < 0) { + return terrno; + } SLHashBucket* pBucket = pHashObj->pBucket[v]; code = doAddToBucket(pHashObj, pBucket, v, key, keyLen, data, size); @@ -343,7 +355,10 @@ int32_t tHashPut(SLHashObj* pHashObj, const void* key, size_t keyLen, void* data int32_t numOfBits = ceil(log(pHashObj->numOfBuckets) / log(2)); if (numOfBits > pHashObj->bits) { // printf("extend the bits from %d to %d, new bucket:%d\n", pHashObj->bits, numOfBits, newBucketId); - ASSERT(numOfBits == pHashObj->bits + 1); + if (numOfBits != pHashObj->bits + 1) { + qError("linear hash faield at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; + } pHashObj->bits = numOfBits; } @@ -360,14 +375,20 @@ int32_t tHashPut(SLHashObj* pHashObj, const void* key, size_t keyLen, void* data char* pStart = p->data; while (pStart - ((char*)p) < p->num) { SLHashNode* pNode = (SLHashNode*)pStart; - ASSERT(pNode->keyLen > 0); + if (pNode->keyLen <= 0) { + qError("linear hash faield at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; + } char* k = GET_LHASH_NODE_KEY(pNode); int32_t hashv = pHashObj->hashFn(k, pNode->keyLen); int32_t v1 = doGetBucketIdFromHashVal(hashv, pHashObj->bits); if (v1 != splitBucketId) { // place it into the new bucket - ASSERT(v1 == newBucketId); + if (v1 != newBucketId) { + qError("linear hash failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INTERNAL_ERROR; + } // printf("move key:%d to 0x%x bucket, remain items:%d\n", *(int32_t*)k, v1, pBucket->size - 1); SLHashBucket* pNewBucket = pHashObj->pBucket[newBucketId]; code = doAddToBucket(pHashObj, pNewBucket, newBucketId, (void*)GET_LHASH_NODE_KEY(pNode), pNode->keyLen, @@ -395,6 +416,9 @@ char* tHashGet(SLHashObj* pHashObj, const void* key, size_t keyLen) { if (bucketId >= pHashObj->numOfBuckets) { bucketId = doGetAlternativeBucketId(bucketId, pHashObj->bits, pHashObj->numOfBuckets); } + if (bucketId < 0) { + return NULL; + } SLHashBucket* pBucket = pHashObj->pBucket[bucketId]; int32_t num = taosArrayGetSize(pBucket->pPageIdList); diff --git a/source/libs/executor/src/tsort.c b/source/libs/executor/src/tsort.c index 37cf131a95..c1edf700d1 100644 --- a/source/libs/executor/src/tsort.c +++ b/source/libs/executor/src/tsort.c @@ -468,7 +468,11 @@ static int32_t doAddNewExternalMemSource(SDiskbasedBuf* pBuf, SArray* pAllSource // The value of numOfRows must be greater than 0, which is guaranteed by the previous memory allocation int32_t numOfRows = (getBufPageSize(pBuf) - blockDataGetSerialMetaSize(taosArrayGetSize(pBlock->pDataBlock))) / rowSize; - ASSERT(numOfRows > 0); + if (numOfRows <= 0) { + qError("sort failed at: %s:%d", __func__, __LINE__); + taosArrayDestroy(pPageIdList); + return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; + } return blockDataEnsureCapacity(pSource->src.pBlock, numOfRows); } @@ -528,7 +532,10 @@ static int32_t doAddToBuf(SSDataBlock* pDataBlock, SSortHandle* pHandle) { } int32_t size = blockDataGetSize(p) + sizeof(int32_t) + taosArrayGetSize(p->pDataBlock) * sizeof(int32_t); - ASSERT(size <= getBufPageSize(pHandle->pBuf)); + if (size > getBufPageSize(pHandle->pBuf)) { + qError("sort failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; + } code = blockDataToBuf(pPage, p); if (code) { @@ -733,7 +740,10 @@ static int32_t adjustMergeTreeForNextTuple(SSortSource* pSource, SMultiwayMergeT tMergeTreePrint(pTree); #endif - tMergeTreeAdjust(pTree, leafNodeIndex); + int32_t code = tMergeTreeAdjust(pTree, leafNodeIndex); + if (TSDB_CODE_SUCCESS != code) { + return code; + } #ifdef _DEBUG_VIEW printf("\nafter adjust:\t"); @@ -1040,7 +1050,10 @@ static int32_t doInternalMergeSort(SSortHandle* pHandle) { int32_t size = blockDataGetSize(pDataBlock) + sizeof(int32_t) + taosArrayGetSize(pDataBlock->pDataBlock) * sizeof(int32_t); - ASSERT(size <= getBufPageSize(pHandle->pBuf)); + if (size > getBufPageSize(pHandle->pBuf)) { + qError("sort failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; + } code= blockDataToBuf(pPage, pDataBlock); if (code) { @@ -1310,7 +1323,10 @@ static int32_t getRowBufFromExtMemFile(SSortHandle* pHandle, int32_t regionId, i } pRegion->bufLen = readBytes; } - ASSERT(pRegion->bufRegOffset <= tupleOffset); + if (pRegion->bufRegOffset > tupleOffset) { + qError("sort failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; + } if (pRegion->bufRegOffset + pRegion->bufLen >= tupleOffset + rowLen) { *pFreeRow = false; *ppRow = pRegion->buf + tupleOffset - pRegion->bufRegOffset; @@ -1487,7 +1503,10 @@ static int32_t tsortCloseRegion(SSortHandle* pHandle) { static int32_t tsortFinalizeRegions(SSortHandle* pHandle) { SSortMemFile* pMemFile = pHandle->pExtRowsMemFile; size_t numRegions = taosArrayGetSize(pMemFile->aFileRegions); - ASSERT(numRegions == (pMemFile->currRegionId + 1)); + if (numRegions != (pMemFile->currRegionId + 1)) { + qError("sort failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; + } if (numRegions == 0) { return TSDB_CODE_SUCCESS; } @@ -1812,7 +1831,10 @@ static int32_t appendDataBlockToPageBuf(SSortHandle* pHandle, SSDataBlock* blk, } int32_t size = blockDataGetSize(blk) + sizeof(int32_t) + taosArrayGetSize(blk->pDataBlock) * sizeof(int32_t); - ASSERT(size <= getBufPageSize(pHandle->pBuf)); + if (size > getBufPageSize(pHandle->pBuf)) { + qError("sort failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; + } int32_t code = blockDataToBuf(pPage, blk); @@ -1863,12 +1885,18 @@ static int32_t getPageBufIncForRowIdSort(SSDataBlock* pDstBlock, int32_t srcRowI int32_t numOfCols = blockDataGetNumOfCols(pDstBlock); if (pPkCol == NULL) { // no var column - ASSERT((numOfCols == 4) && (!pDstBlock->info.hasVarCol)); + if (!((numOfCols == 4) && (!pDstBlock->info.hasVarCol))) { + qError("sort failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; + } size += numOfCols * ((dstRowIndex & 0x7) == 0 ? 1: 0); size += blockDataGetRowSize(pDstBlock); } else { - ASSERT(numOfCols == 5); + if (numOfCols != 5) { + qError("sort failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; + } size += (numOfCols - 1) * (((dstRowIndex & 0x7) == 0)? 1:0); for(int32_t i = 0; i < numOfCols - 1; ++i) { @@ -2018,7 +2046,7 @@ static int32_t sortBlocksToExtSource(SSortHandle* pHandle, SArray* aBlk, SArray* SArray* aPgId = taosArrayInit(8, sizeof(int32_t)); if (aPgId == NULL) { - return terrno; + goto _error; } int32_t nRows = 0; @@ -2039,10 +2067,7 @@ static int32_t sortBlocksToExtSource(SSortHandle* pHandle, SArray* aBlk, SArray* lastPageBufTs = ((int64_t*)tsCol->pData)[pHandle->pDataBlock->info.rows - 1]; code = appendDataBlockToPageBuf(pHandle, pHandle->pDataBlock, aPgId); if (code != TSDB_CODE_SUCCESS) { - taosMemoryFree(pTree); - taosArrayDestroy(aPgId); - cleanupMergeSup(&sup); - return code; + goto _error; } nMergedRows += pHandle->pDataBlock->info.rows; @@ -2064,7 +2089,7 @@ static int32_t sortBlocksToExtSource(SSortHandle* pHandle, SArray* aBlk, SArray* code = blockDataEnsureCapacity(pHandle->pDataBlock, pHandle->pDataBlock->info.rows + 1); if (code) { - return code; + goto _error; } if (pHandle->bSortByRowId) { @@ -2074,11 +2099,14 @@ static int32_t sortBlocksToExtSource(SSortHandle* pHandle, SArray* aBlk, SArray* } if (code) { - return code; + goto _error; } blkPgSz += bufInc; - ASSERT(blkPgSz == blockDataGetSize(pHandle->pDataBlock) + pageHeaderSize); + if (blkPgSz != blockDataGetSize(pHandle->pDataBlock) + pageHeaderSize) { + qError("sort failed at: %s:%d", __func__, __LINE__); + goto _error; + } ++nRows; @@ -2087,7 +2115,10 @@ static int32_t sortBlocksToExtSource(SSortHandle* pHandle, SArray* aBlk, SArray* } else { ++sup.aRowIdx[minIdx]; } - tMergeTreeAdjust(pTree, tMergeTreeGetAdjustIndex(pTree)); + code = tMergeTreeAdjust(pTree, tMergeTreeGetAdjustIndex(pTree)); + if (TSDB_CODE_SUCCESS != code) { + goto _error; + } } if (pHandle->pDataBlock->info.rows > 0) { @@ -2096,10 +2127,7 @@ static int32_t sortBlocksToExtSource(SSortHandle* pHandle, SArray* aBlk, SArray* lastPageBufTs = ((int64_t*)tsCol->pData)[pHandle->pDataBlock->info.rows - 1]; code = appendDataBlockToPageBuf(pHandle, pHandle->pDataBlock, aPgId); if (code != TSDB_CODE_SUCCESS) { - taosArrayDestroy(aPgId); - taosMemoryFree(pTree); - cleanupMergeSup(&sup); - return code; + goto _error; } nMergedRows += pHandle->pDataBlock->info.rows; if ((pHandle->mergeLimit != -1) && (nMergedRows >= pHandle->mergeLimit)) { @@ -2115,11 +2143,7 @@ static int32_t sortBlocksToExtSource(SSortHandle* pHandle, SArray* aBlk, SArray* SSDataBlock* pMemSrcBlk = NULL; code = createOneDataBlock(pHandle->pDataBlock, false, &pMemSrcBlk); - if (code) { - cleanupMergeSup(&sup); - tMergeTreeDestroy(&pTree); - return code; - } + if (code) goto _error; code = doAddNewExternalMemSource(pHandle->pBuf, aExtSrc, pMemSrcBlk, &pHandle->sourceId, aPgId); @@ -2127,6 +2151,12 @@ static int32_t sortBlocksToExtSource(SSortHandle* pHandle, SArray* aBlk, SArray* tMergeTreeDestroy(&pTree); return code; + +_error: + tMergeTreeDestroy(&pTree); + cleanupMergeSup(&sup); + if (aPgId) taosArrayDestroy(aPgId); + return code; } static int32_t getRowsBlockWithinMergeLimit(const SSortHandle* pHandle, SSHashObj* mTableNumRows, SSDataBlock* pOrigBlk, @@ -2502,7 +2532,10 @@ static int32_t tsortOpenForBufMergeSort(SSortHandle* pHandle) { int32_t numOfSources = taosArrayGetSize(pHandle->pOrderedSource); if (pHandle->pBuf != NULL) { - ASSERT(numOfSources <= getNumOfInMemBufPages(pHandle->pBuf)); + if (numOfSources > getNumOfInMemBufPages(pHandle->pBuf)) { + qError("sort failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; + } } if (numOfSources == 0) { @@ -2596,7 +2629,10 @@ static int32_t tsortBufMergeSortNextTuple(SSortHandle* pHandle, STupleHandle** p index = tMergeTreeGetChosenIndex(pHandle->pMergeTree); pSource = pHandle->cmpParam.pSources[index]; - ASSERT(pSource->src.pBlock != NULL); + if (pSource->src.pBlock == NULL) { + qError("sort failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_QRY_EXECUTOR_INTERNAL_ERROR; + } pHandle->tupleHandle.rowIndex = pSource->src.rowIndex; pHandle->tupleHandle.pBlock = pSource->src.pBlock; diff --git a/source/libs/geometry/src/geosWrapper.c b/source/libs/geometry/src/geosWrapper.c index dde34edc91..b349ab5cbe 100644 --- a/source/libs/geometry/src/geosWrapper.c +++ b/source/libs/geometry/src/geosWrapper.c @@ -331,18 +331,26 @@ int32_t doGeosRelation(const GEOSGeometry *geom1, const GEOSPreparedGeometry *pr if (!preparedGeom1) { if (!swapped) { - ASSERT(relationFn); + if (!relationFn) { + return TSDB_CODE_FUNC_FUNTION_PARA_VALUE; + } *res = relationFn(geosCtx->handle, geom1, geom2); } else { - ASSERT(swappedRelationFn); + if (!swappedRelationFn) { + return TSDB_CODE_FUNC_FUNTION_PARA_VALUE; + } *res = swappedRelationFn(geosCtx->handle, geom1, geom2); } } else { if (!swapped) { - ASSERT(preparedRelationFn); + if (!preparedRelationFn) { + return TSDB_CODE_FUNC_FUNTION_PARA_VALUE; + } *res = preparedRelationFn(geosCtx->handle, preparedGeom1, geom2); } else { - ASSERT(swappedPreparedRelationFn); + if (!swappedPreparedRelationFn) { + return TSDB_CODE_FUNC_FUNTION_PARA_VALUE; + } *res = swappedPreparedRelationFn(geosCtx->handle, preparedGeom1, geom2); } } @@ -391,7 +399,9 @@ int32_t readGeometry(const unsigned char *input, GEOSGeometry **outputGeom, const GEOSPreparedGeometry **outputPreparedGeom) { SGeosContext *geosCtx = getThreadLocalGeosCtx(); - ASSERT(outputGeom); // it is not allowed if outputGeom is NULL + if (!outputGeom) { + return TSDB_CODE_FUNC_FUNTION_PARA_VALUE; + } *outputGeom = NULL; if (outputPreparedGeom) { // it means not to generate PreparedGeometry if outputPreparedGeom is NULL diff --git a/source/libs/geometry/test/geomFuncTestUtil.cpp b/source/libs/geometry/test/geomFuncTestUtil.cpp index 0918781499..25b2dd7c8d 100644 --- a/source/libs/geometry/test/geomFuncTestUtil.cpp +++ b/source/libs/geometry/test/geomFuncTestUtil.cpp @@ -66,7 +66,7 @@ void setScalarParam(SScalarParam *sclParam, int32_t type, void *valueArray, TDRo break; } default: { - ASSERT(0); + ASSERT_TRUE(false); break; } } diff --git a/source/libs/index/src/index.c b/source/libs/index/src/index.c index 0f2b53c68a..e2c74f0dbb 100644 --- a/source/libs/index/src/index.c +++ b/source/libs/index/src/index.c @@ -237,8 +237,7 @@ int32_t indexPut(SIndex* index, SIndexMultiTerm* fVals, uint64_t uid) { indexDebug("w suid:%" PRIu64 ", colName:%s, colType:%d", key.suid, key.colName, key.colType); IndexCache** cache = taosHashGet(index->colObj, buf, sz); - ASSERTS(*cache != NULL, "index-cache already release"); - if (*cache == NULL) return -1; + if (*cache == NULL) return TSDB_CODE_INVALID_PTR; int ret = idxCachePut(*cache, p, uid); if (ret != 0) { @@ -295,7 +294,9 @@ void indexMultiTermQueryDestroy(SIndexMultiTermQuery* pQuery) { }; int32_t indexMultiTermQueryAdd(SIndexMultiTermQuery* pQuery, SIndexTerm* term, EIndexQueryType qType) { SIndexTermQuery q = {.qType = qType, .term = term}; - (void)taosArrayPush(pQuery->query, &q); + if (taosArrayPush(pQuery->query, &q) == NULL) { + return terrno; + } return 0; } @@ -303,6 +304,7 @@ SIndexTerm* indexTermCreate(int64_t suid, SIndexOperOnColumn oper, uint8_t colTy int32_t nColName, const char* colVal, int32_t nColVal) { SIndexTerm* tm = (SIndexTerm*)taosMemoryCalloc(1, (sizeof(SIndexTerm))); if (tm == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -311,6 +313,10 @@ SIndexTerm* indexTermCreate(int64_t suid, SIndexOperOnColumn oper, uint8_t colTy tm->colType = colType; tm->colName = (char*)taosMemoryCalloc(1, nColName + 1); + if (tm->colName == NULL) { + taosMemoryFree(tm); + return NULL; + } memcpy(tm->colName, colName, nColName); tm->nColName = nColName; @@ -326,8 +332,23 @@ SIndexTerm* indexTermCreate(int64_t suid, SIndexOperOnColumn oper, uint8_t colTy buf = strndup(emptyStr, (int32_t)strlen(emptyStr)); len = (int32_t)strlen(emptyStr); } + tm->colVal = buf; + if (tm->colVal == NULL) { + taosMemoryFree(tm->colName); + taosMemoryFree(tm); + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } + tm->nColVal = len; + if (tm->nColVal < 0) { + taosMemoryFree(tm->colName); + taosMemoryFree(tm->colVal); + taosMemoryFree(tm); + terrno = len; + return NULL; + } return tm; } diff --git a/source/libs/index/src/indexComm.c b/source/libs/index/src/indexComm.c index e62f506b1a..8dc63ed105 100644 --- a/source/libs/index/src/indexComm.c +++ b/source/libs/index/src/indexComm.c @@ -308,90 +308,6 @@ int idxUidCompare(const void* a, const void* b) { uint64_t r = *(uint64_t*)b; return l - r; } -#ifdef BUILD_NO_CALL -int32_t idxConvertData(void* src, int8_t type, void** dst) { - int tlen = -1; - switch (type) { - case TSDB_DATA_TYPE_TIMESTAMP: - tlen = taosEncodeFixedI64(NULL, *(int64_t*)src); - *dst = taosMemoryCalloc(1, tlen + 1); - tlen = taosEncodeFixedI64(dst, *(int64_t*)src); - break; - case TSDB_DATA_TYPE_BOOL: - case TSDB_DATA_TYPE_UTINYINT: - tlen = taosEncodeFixedU8(NULL, *(uint8_t*)src); - *dst = taosMemoryCalloc(1, tlen + 1); - tlen = taosEncodeFixedU8(dst, *(uint8_t*)src); - break; - case TSDB_DATA_TYPE_TINYINT: - tlen = taosEncodeFixedI8(NULL, *(uint8_t*)src); - *dst = taosMemoryCalloc(1, tlen + 1); - tlen = taosEncodeFixedI8(dst, *(uint8_t*)src); - break; - case TSDB_DATA_TYPE_SMALLINT: - tlen = taosEncodeFixedI16(NULL, *(int16_t*)src); - *dst = taosMemoryCalloc(1, tlen + 1); - tlen = taosEncodeFixedI16(dst, *(int16_t*)src); - break; - case TSDB_DATA_TYPE_USMALLINT: - tlen = taosEncodeFixedU16(NULL, *(uint16_t*)src); - *dst = taosMemoryCalloc(1, tlen + 1); - tlen = taosEncodeFixedU16(dst, *(uint16_t*)src); - break; - case TSDB_DATA_TYPE_INT: - tlen = taosEncodeFixedI32(NULL, *(int32_t*)src); - *dst = taosMemoryCalloc(1, tlen + 1); - tlen = taosEncodeFixedI32(dst, *(int32_t*)src); - break; - case TSDB_DATA_TYPE_FLOAT: - tlen = taosEncodeBinary(NULL, src, sizeof(float)); - *dst = taosMemoryCalloc(1, tlen + 1); - tlen = taosEncodeBinary(dst, src, sizeof(float)); - break; - case TSDB_DATA_TYPE_UINT: - tlen = taosEncodeFixedU32(NULL, *(uint32_t*)src); - *dst = taosMemoryCalloc(1, tlen + 1); - tlen = taosEncodeFixedU32(dst, *(uint32_t*)src); - break; - case TSDB_DATA_TYPE_BIGINT: - tlen = taosEncodeFixedI64(NULL, *(int64_t*)src); - *dst = taosMemoryCalloc(1, tlen + 1); - tlen = taosEncodeFixedI64(dst, *(int64_t*)src); - break; - case TSDB_DATA_TYPE_DOUBLE: - tlen = taosEncodeBinary(NULL, src, sizeof(double)); - *dst = taosMemoryCalloc(1, tlen + 1); - tlen = taosEncodeBinary(dst, src, sizeof(double)); - break; - case TSDB_DATA_TYPE_UBIGINT: - tlen = taosEncodeFixedU64(NULL, *(uint64_t*)src); - *dst = taosMemoryCalloc(1, tlen + 1); - tlen = taosEncodeFixedU64(dst, *(uint64_t*)src); - break; - case TSDB_DATA_TYPE_NCHAR: { - tlen = taosEncodeBinary(NULL, varDataVal(src), varDataLen(src)); - *dst = taosMemoryCalloc(1, tlen + 1); - tlen = taosEncodeBinary(dst, varDataVal(src), varDataLen(src)); - - break; - } - case TSDB_DATA_TYPE_VARCHAR: // TSDB_DATA_TYPE_BINARY - case TSDB_DATA_TYPE_VARBINARY: - case TSDB_DATA_TYPE_GEOMETRY: { - tlen = taosEncodeBinary(NULL, src, strlen(src)); - *dst = taosMemoryCalloc(1, tlen + 1); - tlen = taosEncodeBinary(dst, src, strlen(src)); - break; - } - default: - ASSERTS(0, "index invalid input type"); - break; - } - *dst = (char*)*dst - tlen; - // indexMayFillNumbericData(*dst, tlen); - return tlen; -} -#endif int32_t idxConvertDataToStr(void* src, int8_t type, void** dst) { if (src == NULL) { @@ -513,7 +429,7 @@ int32_t idxConvertDataToStr(void* src, int8_t type, void** dst) { break; } default: - ASSERTS(0, "index invalid input type"); + tlen = TSDB_CODE_INVALID_DATA_FMT; break; } return tlen; diff --git a/source/libs/index/src/indexFilter.c b/source/libs/index/src/indexFilter.c index 2858d09c30..03474c0143 100644 --- a/source/libs/index/src/indexFilter.c +++ b/source/libs/index/src/indexFilter.c @@ -661,7 +661,16 @@ static int32_t sifDoIndex(SIFParam *left, SIFParam *right, int8_t operType, SIFP } SIndexMultiTermQuery *mtm = indexMultiTermQueryCreate(MUST); - (void)indexMultiTermQueryAdd(mtm, tm, qtype); + if (mtm == NULL) { + indexTermDestroy(tm); + return TSDB_CODE_OUT_OF_MEMORY; + } + + if ((ret = indexMultiTermQueryAdd(mtm, tm, qtype)) != 0) { + indexMultiTermQueryDestroy(mtm); + return ret; + } + ret = indexJsonSearch(arg->ivtIdx, mtm, output->result); indexMultiTermQueryDestroy(mtm); } else { @@ -1119,7 +1128,6 @@ int32_t doFilterTag(SNode *pFilterNode, SIndexMetaArg *metaArg, SArray *result, *status = st; } - if (taosArrayAddAll(result, param.result) == NULL) { sifFreeParam(¶m); return TSDB_CODE_OUT_OF_MEMORY; diff --git a/source/libs/index/src/indexFst.c b/source/libs/index/src/indexFst.c index 54d28f4784..e57ba6267d 100644 --- a/source/libs/index/src/indexFst.c +++ b/source/libs/index/src/indexFst.c @@ -98,7 +98,7 @@ void fstUnFinishedNodesAddSuffix(FstUnFinishedNodes* nodes, FstSlice bs, Output } int32_t sz = taosArrayGetSize(nodes->stack) - 1; FstBuilderNodeUnfinished* un = taosArrayGet(nodes->stack, sz); - ASSERTS(un->last == NULL, "index-fst meet unexpected node"); + if (un->last != NULL) return; // FstLastTransition *trn = taosMemoryMalloc(sizeof(FstLastTransition)); @@ -318,7 +318,7 @@ void fstStateCompileForAnyTrans(IdxFstFile* w, CompiledAddr addr, FstBuilderNode // set_comm_input void fstStateSetCommInput(FstState* s, uint8_t inp) { - ASSERT(s->state == OneTransNext || s->state == OneTrans); + // ASSERT(s->state == OneTransNext || s->state == OneTrans); uint8_t val; COMMON_INDEX(inp, 0b111111, val); @@ -327,7 +327,7 @@ void fstStateSetCommInput(FstState* s, uint8_t inp) { // comm_input uint8_t fstStateCommInput(FstState* s, bool* null) { - ASSERT(s->state == OneTransNext || s->state == OneTrans); + // ASSERT(s->state == OneTransNext || s->state == OneTrans); uint8_t v = s->val & 0b00111111; if (v == 0) { *null = true; @@ -340,7 +340,7 @@ uint8_t fstStateCommInput(FstState* s, bool* null) { // input_len uint64_t fstStateInputLen(FstState* s) { - ASSERT(s->state == OneTransNext || s->state == OneTrans); + // ASSERT(s->state == OneTransNext || s->state == OneTrans); bool null = false; (void)fstStateCommInput(s, &null); return null ? 1 : 0; @@ -348,11 +348,11 @@ uint64_t fstStateInputLen(FstState* s) { // end_addr uint64_t fstStateEndAddrForOneTransNext(FstState* s, FstSlice* data) { - ASSERT(s->state == OneTransNext); + // ASSERT(s->state == OneTransNext); return FST_SLICE_LEN(data) - 1 - fstStateInputLen(s); } uint64_t fstStateEndAddrForOneTrans(FstState* s, FstSlice* data, PackSizes sizes) { - ASSERT(s->state == OneTrans); + // ASSERT(s->state == OneTrans); return FST_SLICE_LEN(data) - 1 - fstStateInputLen(s) - 1 // pack size - FST_GET_TRANSITION_PACK_SIZE(sizes) - FST_GET_OUTPUT_PACK_SIZE(sizes); } @@ -366,7 +366,7 @@ uint64_t fstStateEndAddrForAnyTrans(FstState* state, uint64_t version, FstSlice* } // input uint8_t fstStateInput(FstState* s, FstNode* node) { - ASSERT(s->state == OneTransNext || s->state == OneTrans); + // ASSERT(s->state == OneTransNext || s->state == OneTrans); FstSlice* slice = &node->data; bool null = false; uint8_t inp = fstStateCommInput(s, &null); @@ -374,7 +374,7 @@ uint8_t fstStateInput(FstState* s, FstNode* node) { return null == false ? inp : data[node->start - 1]; } uint8_t fstStateInputForAnyTrans(FstState* s, FstNode* node, uint64_t i) { - ASSERT(s->state == AnyTrans); + // ASSERT(s->state == AnyTrans); FstSlice* slice = &node->data; uint64_t at = node->start - fstStateNtransLen(s) - 1 // pack size @@ -386,7 +386,7 @@ uint8_t fstStateInputForAnyTrans(FstState* s, FstNode* node, uint64_t i) { // trans_addr CompiledAddr fstStateTransAddr(FstState* s, FstNode* node) { - ASSERT(s->state == OneTransNext || s->state == OneTrans); + // ASSERT(s->state == OneTransNext || s->state == OneTrans); FstSlice* slice = &node->data; if (s->state == OneTransNext) { return (CompiledAddr)(node->end) - 1; @@ -402,7 +402,7 @@ CompiledAddr fstStateTransAddr(FstState* s, FstNode* node) { } } CompiledAddr fstStateTransAddrForAnyTrans(FstState* s, FstNode* node, uint64_t i) { - ASSERT(s->state == AnyTrans); + // ASSERT(s->state == AnyTrans); FstSlice* slice = &node->data; uint8_t tSizes = FST_GET_TRANSITION_PACK_SIZE(node->sizes); @@ -414,7 +414,7 @@ CompiledAddr fstStateTransAddrForAnyTrans(FstState* s, FstNode* node, uint64_t i // sizes PackSizes fstStateSizes(FstState* s, FstSlice* slice) { - ASSERT(s->state == OneTrans || s->state == AnyTrans); + /// ASSERT(s->state == OneTrans || s->state == AnyTrans); uint64_t i; if (s->state == OneTrans) { i = FST_SLICE_LEN(slice) - 1 - fstStateInputLen(s) - 1; @@ -427,7 +427,7 @@ PackSizes fstStateSizes(FstState* s, FstSlice* slice) { } // Output Output fstStateOutput(FstState* s, FstNode* node) { - ASSERT(s->state == OneTrans); + // ASSERT(s->state == OneTrans); uint8_t oSizes = FST_GET_OUTPUT_PACK_SIZE(node->sizes); if (oSizes == 0) { @@ -441,7 +441,7 @@ Output fstStateOutput(FstState* s, FstNode* node) { return unpackUint64(data + i, oSizes); } Output fstStateOutputForAnyTrans(FstState* s, FstNode* node, uint64_t i) { - ASSERT(s->state == AnyTrans); + // ASSERT(s->state == AnyTrans); uint8_t oSizes = FST_GET_OUTPUT_PACK_SIZE(node->sizes); if (oSizes == 0) { @@ -458,19 +458,19 @@ Output fstStateOutputForAnyTrans(FstState* s, FstNode* node, uint64_t i) { // anyTrans specify function void fstStateSetFinalState(FstState* s, bool yes) { - ASSERT(s->state == AnyTrans); + // ASSERT(s->state == AnyTrans); if (yes) { s->val |= 0b01000000; } return; } bool fstStateIsFinalState(FstState* s) { - ASSERT(s->state == AnyTrans); + // ASSERT(s->state == AnyTrans); return (s->val & 0b01000000) == 0b01000000; } void fstStateSetStateNtrans(FstState* s, uint8_t n) { - ASSERT(s->state == AnyTrans); + // ASSERT(s->state == AnyTrans); if (n <= 0b00111111) { s->val = (s->val & 0b11000000) | n; } @@ -478,7 +478,7 @@ void fstStateSetStateNtrans(FstState* s, uint8_t n) { } // state_ntrans uint8_t fstStateStateNtrans(FstState* s, bool* null) { - ASSERT(s->state == AnyTrans); + // ASSERT(s->state == AnyTrans); *null = false; uint8_t n = s->val & 0b00111111; @@ -488,16 +488,16 @@ uint8_t fstStateStateNtrans(FstState* s, bool* null) { return n; } uint64_t fstStateTotalTransSize(FstState* s, uint64_t version, PackSizes sizes, uint64_t nTrans) { - ASSERT(s->state == AnyTrans); + // ASSERT(s->state == AnyTrans); uint64_t idxSize = fstStateTransIndexSize(s, version, nTrans); return nTrans + (nTrans * FST_GET_TRANSITION_PACK_SIZE(sizes)) + idxSize; } uint64_t fstStateTransIndexSize(FstState* s, uint64_t version, uint64_t nTrans) { - ASSERT(s->state == AnyTrans); + // ASSERT(s->state == AnyTrans); return (version >= 2 && nTrans > TRANS_INDEX_THRESHOLD) ? 256 : 0; } uint64_t fstStateNtransLen(FstState* s) { - ASSERT(s->state == AnyTrans); + // ASSERT(s->state == AnyTrans); bool null = false; (void)fstStateStateNtrans(s, &null); return null == true ? 1 : 0; @@ -526,7 +526,7 @@ Output fstStateFinalOutput(FstState* s, uint64_t version, FstSlice* slice, PackS return unpackUint64(data + at, (uint8_t)oSizes); } uint64_t fstStateFindInput(FstState* s, FstNode* node, uint8_t b, bool* null) { - ASSERT(s->state == AnyTrans); + // ASSERT(s->state == AnyTrans); FstSlice* slice = &node->data; if (node->version >= 2 && node->nTrans > TRANS_INDEX_THRESHOLD) { uint64_t at = node->start - fstStateNtransLen(s) - 1 // pack size @@ -672,17 +672,17 @@ bool fstNodeGetTransitionAddrAt(FstNode* node, uint64_t i, CompiledAddr* res) { bool s = true; FstState* st = &node->state; if (st->state == OneTransNext) { - ASSERT(i == 0); + /// ASSERT(i == 0); (void)fstStateTransAddr(st, node); } else if (st->state == OneTrans) { - ASSERT(i == 0); + // ASSERT(i == 0); (void)fstStateTransAddr(st, node); } else if (st->state == AnyTrans) { (void)fstStateTransAddrForAnyTrans(st, node, i); } else if (FST_STATE_EMPTY_FINAL(node)) { s = false; } else { - ASSERT(0); + // ASSERT(0); } return s; } @@ -718,7 +718,7 @@ bool fstNodeFindInput(FstNode* node, uint8_t b, uint64_t* res) { bool fstNodeCompile(FstNode* node, void* w, CompiledAddr lastAddr, CompiledAddr addr, FstBuilderNode* builderNode) { int32_t sz = taosArrayGetSize(builderNode->trans); - ASSERT(sz < 256); + // ASSERT(sz < 256); if (sz == 0 && builderNode->isFinal && builderNode->finalOutput == 0) { return true; } else if (sz != 1 || builderNode->isFinal) { @@ -800,7 +800,7 @@ void fstBuilderInsertOutput(FstBuilder* b, FstSlice bs, Output in) { uint64_t prefixLen = fstUnFinishedNodesFindCommPrefixAndSetOutput(b->unfinished, bs, in, &out); if (prefixLen == FST_SLICE_LEN(s)) { - ASSERT(out == 0); + // ASSERT(out == 0); return; } @@ -844,7 +844,7 @@ void fstBuilderCompileFrom(FstBuilder* b, uint64_t istate) { addr = fstBuilderCompile(b, bn); fstBuilderNodeDestroy(bn); - ASSERT(addr != NONE_ADDRESS); + // ASSERT(addr != NONE_ADDRESS); } fstUnFinishedNodesTopLastFreeze(b->unfinished, addr); return; diff --git a/source/libs/index/src/indexFstFile.c b/source/libs/index/src/indexFstFile.c index 69fee8d29d..5c6a39d13c 100644 --- a/source/libs/index/src/indexFstFile.c +++ b/source/libs/index/src/indexFstFile.c @@ -104,7 +104,10 @@ static int idxFileCtxDoReadFrom(IFileCtx* ctx, uint8_t* buf, int len, int32_t of do { char key[1024] = {0}; - ASSERT(strlen(ctx->file.buf) + 1 + 64 < sizeof(key)); + if (strlen(ctx->file.buf) + 1 + 64 >= sizeof(key)) { + return TSDB_CODE_INDEX_INVALID_FILE; + } + idxGenLRUKey(key, ctx->file.buf, blkId); LRUHandle* h = taosLRUCacheLookup(ctx->lru, key, strlen(key)); @@ -118,8 +121,10 @@ static int idxFileCtxDoReadFrom(IFileCtx* ctx, uint8_t* buf, int len, int32_t of if (left < kBlockSize) { nread = TMIN(left, len); int32_t bytes = taosPReadFile(ctx->file.pFile, buf + total, nread, offset); - ASSERTS(bytes == nread, "index read incomplete data"); - if (bytes != nread) break; + if (bytes != nread) { + total = TSDB_CODE_INDEX_INVALID_FILE; + break; + } total += bytes; return total; @@ -129,7 +134,6 @@ static int idxFileCtxDoReadFrom(IFileCtx* ctx, uint8_t* buf, int len, int32_t of SDataBlock* blk = taosMemoryCalloc(1, cacheMemSize); blk->blockId = blkId; blk->nread = taosPReadFile(ctx->file.pFile, blk->buf, kBlockSize, blkId * kBlockSize); - ASSERTS(blk->nread <= kBlockSize, "index read incomplete data"); if (blk->nread < kBlockSize && blk->nread < len) { taosMemoryFree(blk); break; @@ -288,7 +292,6 @@ int32_t idxFileWrite(IdxFstFile* write, uint8_t* buf, uint32_t len) { // update checksum IFileCtx* ctx = write->wrt; int nWrite = ctx->write(ctx, buf, len); - ASSERTS(nWrite == len, "index write incomplete data"); if (nWrite != len) { code = TAOS_SYSTEM_ERROR(errno); return code; diff --git a/source/libs/index/src/indexFstNode.c b/source/libs/index/src/indexFstNode.c index b934cb0536..041444f1c9 100644 --- a/source/libs/index/src/indexFstNode.c +++ b/source/libs/index/src/indexFstNode.c @@ -97,9 +97,8 @@ void fstBuilderNodeCloneFrom(FstBuilderNode* dst, FstBuilderNode* src) { // bool fstBuilderNodeCompileTo(FstBuilderNode *b, IdxFile *wrt, CompiledAddr lastAddr, CompiledAddr // startAddr) { - // size_t sz = taosArrayGetSize(b->trans); -// assert(sz < 256); +// ASSERT(sz < 256); // if (FST_BUILDER_NODE_IS_FINAL(b) // && FST_BUILDER_NODE_TRANS_ISEMPTY(b) // && FST_BUILDER_NODE_FINALOUTPUT_ISZERO(b)) { diff --git a/source/libs/index/src/indexFstRegister.c b/source/libs/index/src/indexFstRegister.c index e27ab939b3..a8c4365a44 100644 --- a/source/libs/index/src/indexFstRegister.c +++ b/source/libs/index/src/indexFstRegister.c @@ -57,8 +57,9 @@ static void fstRegistryCellPromote(SArray* arr, uint32_t start, uint32_t end) { if (start >= sz && end >= sz) { return; } - ASSERTS(start >= end, "index-fst start lower than end"); - if (start < end) return; + if (start < end) { + return; + } int32_t s = (int32_t)start; int32_t e = (int32_t)end; diff --git a/source/libs/index/src/indexTfile.c b/source/libs/index/src/indexTfile.c index 1e81445d11..8c06296732 100644 --- a/source/libs/index/src/indexTfile.c +++ b/source/libs/index/src/indexTfile.c @@ -933,17 +933,20 @@ static int tfileWriteFooter(TFileWriter* write) { char buf[sizeof(FILE_MAGIC_NUMBER) + 1] = {0}; void* pBuf = (void*)buf; (void)taosEncodeFixedU64((void**)(void*)&pBuf, FILE_MAGIC_NUMBER); - int nwrite = write->ctx->write(write->ctx, buf, (int32_t)strlen(buf)); + int nwrite = write->ctx->write(write->ctx, (uint8_t*)buf, (int32_t)strlen(buf)); indexInfo("tfile write footer size: %d", write->ctx->size(write->ctx)); - ASSERTS(nwrite == sizeof(FILE_MAGIC_NUMBER), "index write incomplete data"); - return nwrite; + if (nwrite != sizeof(FILE_MAGIC_NUMBER)) { + return TAOS_SYSTEM_ERROR(errno); + } else { + return nwrite; + } } static int tfileReaderLoadHeader(TFileReader* reader) { // TODO simple tfile header later char buf[TFILE_HEADER_SIZE] = {0}; - int64_t nread = reader->ctx->readFrom(reader->ctx, buf, sizeof(buf), 0); + int64_t nread = reader->ctx->readFrom(reader->ctx, (uint8_t*)buf, sizeof(buf), 0); if (nread == -1) { indexError("actual Read: %d, to read: %d, code:0x%x, filename: %s", (int)(nread), (int)sizeof(buf), errno, @@ -967,14 +970,13 @@ static int tfileReaderLoadFst(TFileReader* reader) { } int64_t ts = taosGetTimestampUs(); - int32_t nread = ctx->readFrom(ctx, buf, fstSize, reader->header.fstOffset); + int32_t nread = ctx->readFrom(ctx, (uint8_t*)buf, fstSize, reader->header.fstOffset); int64_t cost = taosGetTimestampUs() - ts; indexInfo("nread = %d, and fst offset=%d, fst size: %d, filename: %s, file size: %d, time cost: %" PRId64 "us", nread, reader->header.fstOffset, fstSize, ctx->file.buf, size, cost); // we assuse fst size less than FST_MAX_SIZE - ASSERTS(nread > 0 && nread <= fstSize, "index read incomplete fst"); if (nread <= 0 || nread > fstSize) { - return -1; + return TSDB_CODE_INDEX_INVALID_FILE; } FstSlice st = fstSliceCreate((uint8_t*)buf, nread); @@ -989,8 +991,10 @@ static int tfileReaderLoadTableIds(TFileReader* reader, int32_t offset, SArray* IFileCtx* ctx = reader->ctx; // add block cache char block[4096] = {0}; - int32_t nread = ctx->readFrom(ctx, block, sizeof(block), offset); - ASSERT(nread >= sizeof(uint32_t)); + int32_t nread = ctx->readFrom(ctx, (uint8_t*)block, sizeof(block), offset); + if (nread < sizeof(uint32_t)) { + return TSDB_CODE_INDEX_INVALID_FILE; + } char* p = block; int32_t nid = *(int32_t*)p; @@ -1007,7 +1011,7 @@ static int tfileReaderLoadTableIds(TFileReader* reader, int32_t offset, SArray* memset(block, 0, sizeof(block)); offset += sizeof(block); - nread = ctx->readFrom(ctx, block, sizeof(block), offset); + nread = ctx->readFrom(ctx, (uint8_t*)block, sizeof(block), offset); memcpy(buf + left, block, sizeof(uint64_t) - left); (void)taosArrayPush(result, (uint64_t*)buf); @@ -1026,13 +1030,14 @@ static int tfileReaderVerify(TFileReader* reader) { int size = ctx->size(ctx); if (size < sizeof(tMagicNumber) || size <= sizeof(reader->header)) { - return -1; - } else if (ctx->readFrom(ctx, buf, sizeof(tMagicNumber), size - sizeof(tMagicNumber)) != sizeof(tMagicNumber)) { - return -1; + return TSDB_CODE_INDEX_INVALID_FILE; + } else if (ctx->readFrom(ctx, (uint8_t*)buf, sizeof(tMagicNumber), size - sizeof(tMagicNumber)) != + sizeof(tMagicNumber)) { + return TSDB_CODE_INDEX_INVALID_FILE; } (void)taosDecodeFixedU64(buf, &tMagicNumber); - return tMagicNumber == FILE_MAGIC_NUMBER ? 0 : -1; + return tMagicNumber == FILE_MAGIC_NUMBER ? 0 : TSDB_CODE_INDEX_INVALID_FILE; } void tfileReaderRef(TFileReader* rd) { diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 05cceb656e..7e84ef8482 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -4014,7 +4014,6 @@ static int32_t setTableTsmas(STranslateContext* pCxt, SName* pName, SRealTableNo if (TSDB_CODE_SUCCESS == code) code = catalogGetCachedTableHashVgroup(pCxt->pParseCxt->pCatalog, &tsmaTargetTbName, &vgInfo, &exists); if (TSDB_CODE_SUCCESS == code) { - ASSERT(exists); if (!pRealTable->tsmaTargetTbVgInfo) { pRealTable->tsmaTargetTbVgInfo = taosArrayInit(pRealTable->pTsmas->size, POINTER_BYTES); if (!pRealTable->tsmaTargetTbVgInfo) { @@ -7320,7 +7319,10 @@ static int32_t checkDbRetentionsOption(STranslateContext* pCxt, SNodeList* pRete SValueNode* pFreq = (SValueNode*)nodesListGetNode(((SNodeListNode*)pRetention)->pNodeList, 0); SValueNode* pKeep = (SValueNode*)nodesListGetNode(((SNodeListNode*)pRetention)->pNodeList, 1); - ASSERTS(IS_DURATION_VAL(pFreq->flag) && IS_DURATION_VAL(pKeep->flag), "Retentions freq/keep should have unit"); + if (!IS_DURATION_VAL(pFreq->flag) || !IS_DURATION_VAL(pKeep->flag)) { + return generateSyntaxErrMsgExt(&pCxt->msgBuf, TSDB_CODE_PAR_INVALID_DB_OPTION, + "Retentions freq/keep should have unit"); + } // check unit if (IS_DURATION_VAL(pFreq->flag) && TIME_UNIT_SECOND != pFreq->unit && TIME_UNIT_MINUTE != pFreq->unit && @@ -9427,7 +9429,9 @@ static int32_t buildCreateTagIndexReq(STranslateContext* pCxt, SCreateIndexStmt* (void)tNameGetFullDbName(&name, pReq->dbFName); SNode* pNode = NULL; - ASSERT(LIST_LENGTH(pStmt->pCols) == 1); + if (LIST_LENGTH(pStmt->pCols) != 1) { + return TSDB_CODE_PAR_INVALID_TAGS_NUM; + } FOREACH(pNode, pStmt->pCols) { SColumnNode* p = (SColumnNode*)pNode; memcpy(pReq->colName, p->colName, sizeof(p->colName)); @@ -11068,7 +11072,10 @@ static int32_t createStreamReqVersionInfo(SSDataBlock* pBlock, SArray** pArray, for (int32_t i = 0; i < pBlock->info.rows; ++i) { SVgroupVer v = {.vgId = *(int32_t*)colDataGetData(pCol1, i), .ver = *(int64_t*)colDataGetData(pCol2, i)}; - (void)taosArrayPush(*pArray, &v); + if((taosArrayPush(*pArray, &v)) == NULL) { + taosArrayDestroy(*pArray); + return terrno; + } } } else { int32_t precision = (pInterval->interval > 0) ? pInterval->precision : TSDB_TIME_PRECISION_MILLI; diff --git a/source/libs/parser/src/parUtil.c b/source/libs/parser/src/parUtil.c index 6a36ea7b85..250e9385b4 100644 --- a/source/libs/parser/src/parUtil.c +++ b/source/libs/parser/src/parUtil.c @@ -1281,8 +1281,10 @@ int32_t getTsmaFromCache(SParseMetaCache* pMetaCache, const SName* pTsmaName, ST } STableTSMAInfoRsp* pTsmaRsp = NULL; code = getMetaDataFromHash(tsmaFName, strlen(tsmaFName), pMetaCache->pTSMAs, (void**)&pTsmaRsp); - if (TSDB_CODE_SUCCESS == code && pTsmaRsp) { - ASSERT(pTsmaRsp->pTsmas->size == 1); + if (TSDB_CODE_SUCCESS == code) { + if (!pTsmaRsp || pTsmaRsp->pTsmas->size != 1) { + return TSDB_CODE_PAR_INTERNAL_ERROR; + } *pTsma = taosArrayGetP(pTsmaRsp->pTsmas, 0); } else if (code == TSDB_CODE_PAR_INTERNAL_ERROR){ code = TSDB_CODE_MND_SMA_NOT_EXIST; diff --git a/source/libs/planner/src/planOptimizer.c b/source/libs/planner/src/planOptimizer.c index c7ce9aed76..d48871fd70 100644 --- a/source/libs/planner/src/planOptimizer.c +++ b/source/libs/planner/src/planOptimizer.c @@ -6493,6 +6493,7 @@ static int32_t partitionColsOpt(SOptimizeContext* pCxt, SLogicSubplan* pLogicSub pSort->calcGroupId = true; code = replaceLogicNode(pLogicSubplan, (SLogicNode*)pNode, (SLogicNode*)pSort); if (code == TSDB_CODE_SUCCESS) { + nodesDestroyNode((SNode*)pNode); pCxt->optimized = true; } else { nodesDestroyNode((SNode*)pSort); @@ -6641,7 +6642,6 @@ static int32_t fillTSMAOptCtx(STSMAOptCtx* pTsmaOptCtx, SScanLogicNode* pScan) { pTsmaOptCtx->pAggFuncs = pWindow->pFuncs; pTsmaOptCtx->ppParentTsmaSubplans = &pWindow->pTsmaSubplans; } else { - ASSERT(nodeType(pTsmaOptCtx->pParent) == QUERY_NODE_LOGIC_PLAN_AGG); SAggLogicNode* pAgg = (SAggLogicNode*)pTsmaOptCtx->pParent; pTsmaOptCtx->pAggFuncs = pAgg->pAggFuncs; pTsmaOptCtx->ppParentTsmaSubplans = &pAgg->pTsmaSubplans; @@ -6939,8 +6939,9 @@ static int32_t tsmaOptSplitWindows(STSMAOptCtx* pTsmaOptCtx, const STimeWindow* } int32_t tsmaOptCreateTsmaScanCols(const STSMAOptUsefulTsma* pTsma, const SNodeList* pAggFuncs, SNodeList** ppList) { - ASSERT(pTsma->pTsma); - ASSERT(pTsma->pTsmaScanCols); + if (!pTsma->pTsma || !pTsma->pTsmaScanCols) { + return TSDB_CODE_PLAN_INTERNAL_ERROR; + } int32_t code; SNode* pNode; SNodeList* pScanCols = NULL; @@ -6996,8 +6997,7 @@ static int32_t tsmaOptRewriteTag(const STSMAOptCtx* pTsmaOptCtx, const STSMAOptU break; } } - ASSERT(found); - return 0; + return found ? TSDB_CODE_SUCCESS : TSDB_CODE_PLAN_INTERNAL_ERROR; } static int32_t tsmaOptRewriteTbname(const STSMAOptCtx* pTsmaOptCtx, SNode** pTbNameNode, @@ -7116,7 +7116,6 @@ static int32_t tsmaOptRewriteScan(STSMAOptCtx* pTsmaOptCtx, SScanLogicNode* pNew SColumnNode* pPkTsCol = NULL; FOREACH(pNode, pNewScan->pScanCols) { SColumnNode* pCol = (SColumnNode*)pNode; - ASSERT(pTsma->pTsmaScanCols); if (pCol->colId == PRIMARYKEY_TIMESTAMP_COL_ID) { pPkTsCol = NULL; code = nodesCloneNode((SNode*)pCol, (SNode**)&pPkTsCol); @@ -7302,7 +7301,6 @@ static int32_t tsmaOptGeneratePlan(STSMAOptCtx* pTsmaOptCtx) { for (int32_t j = 0; j < pTsmaOptCtx->pScan->pTsmas->size; ++j) { if (taosArrayGetP(pTsmaOptCtx->pScan->pTsmas, j) == pTsma->pTsma) { const STsmaTargetTbInfo* ptbInfo = taosArrayGet(pTsmaOptCtx->pScan->pTsmaTargetTbInfo, j); - ASSERT(ptbInfo->uid != 0); strcpy(pTsma->targetTbName, ptbInfo->tableName); pTsma->targetTbUid = ptbInfo->uid; } diff --git a/source/libs/scalar/src/sclfunc.c b/source/libs/scalar/src/sclfunc.c index 477a768ccf..ed55bde663 100644 --- a/source/libs/scalar/src/sclfunc.c +++ b/source/libs/scalar/src/sclfunc.c @@ -1544,9 +1544,15 @@ int32_t replaceFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pO needFreeFrom = true; } if (GET_PARAM_TYPE(&pInput[2]) != GET_PARAM_TYPE(&pInput[0])) { - SCL_ERR_JRET(convBetweenNcharAndVarchar(varDataVal(colDataGetData(pInputData[2], colIdx3)), &toStr, - varDataLen(colDataGetData(pInputData[2], colIdx3)), &toLen, - GET_PARAM_TYPE(&pInput[0]))); + code = convBetweenNcharAndVarchar(varDataVal(colDataGetData(pInputData[2], colIdx3)), &toStr, + varDataLen(colDataGetData(pInputData[2], colIdx3)), &toLen, + GET_PARAM_TYPE(&pInput[0])); + if (TSDB_CODE_SUCCESS != code) { + if (needFreeFrom) { + taosMemoryFree(fromStr); + } + goto _return; + } needFreeTo = true; } @@ -1660,9 +1666,9 @@ int32_t substrIdxFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam * SCL_ERR_JRET(colDataSetVal(pOutputData, k, output, false)); } - -_return: pOutput->numOfRows = numOfRows; +_return: + taosMemoryFree(outputBuf); return code; } diff --git a/source/libs/stream/src/streamBackendRocksdb.c b/source/libs/stream/src/streamBackendRocksdb.c index 83e55791d2..29fb18ef07 100644 --- a/source/libs/stream/src/streamBackendRocksdb.c +++ b/source/libs/stream/src/streamBackendRocksdb.c @@ -420,7 +420,10 @@ int32_t remoteChkpGetDelFile(char* path, SArray* toDel) { } void cleanDir(const char* pPath, const char* id) { - ASSERT(pPath != NULL); + if (pPath == NULL) { + stError("%s try to clean dir, but path is NULL", id); + return; + } if (taosIsDir(pPath)) { taosRemoveDir(pPath); @@ -2603,7 +2606,7 @@ void taskDbDestroy(void* pDb, bool flush) { stDebug("succ to destroy stream backend:%p", wrapper); int8_t nCf = tListLen(ginitDict); - if (flush && wrapper->removeAllFiles == 0) { + if (flush && wrapper->removeAllFiles == 0) { if (wrapper->db && wrapper->pCf) { rocksdb_flushoptions_t* flushOpt = rocksdb_flushoptions_create(); rocksdb_flushoptions_set_wait(flushOpt, 1); diff --git a/source/libs/stream/src/streamDispatch.c b/source/libs/stream/src/streamDispatch.c index de67d55703..8928138950 100644 --- a/source/libs/stream/src/streamDispatch.c +++ b/source/libs/stream/src/streamDispatch.c @@ -158,6 +158,10 @@ static int32_t buildStreamRetrieveReq(SStreamTask* pTask, const SSDataBlock* pBl pRetrieve->version = htobe64(pBlock->info.version); int32_t actualLen = blockEncode(pBlock, pRetrieve->data + PAYLOAD_PREFIX_LEN, numOfCols); + if(actualLen < 0) { + taosMemoryFree(pRetrieve); + return terrno; + } SET_PAYLOAD_LEN(pRetrieve->data, actualLen, actualLen); int32_t payloadLen = actualLen + PAYLOAD_PREFIX_LEN; @@ -1064,7 +1068,7 @@ int32_t streamAddBlockIntoDispatchMsg(const SSDataBlock* pBlock, SStreamDispatch void* buf = taosMemoryCalloc(1, dataStrLen); if (buf == NULL) { - return -1; + return terrno; } SRetrieveTableRsp* pRetrieve = (SRetrieveTableRsp*)buf; @@ -1084,6 +1088,10 @@ int32_t streamAddBlockIntoDispatchMsg(const SSDataBlock* pBlock, SStreamDispatch pRetrieve->numOfCols = htonl(numOfCols); int32_t actualLen = blockEncode(pBlock, pRetrieve->data + PAYLOAD_PREFIX_LEN, numOfCols); + if(actualLen < 0) { + taosMemoryFree(buf); + return terrno; + } SET_PAYLOAD_LEN(pRetrieve->data, actualLen, actualLen); int32_t payloadLen = actualLen + PAYLOAD_PREFIX_LEN; diff --git a/source/libs/transport/inc/transComm.h b/source/libs/transport/inc/transComm.h index 820075787f..e8172385b2 100644 --- a/source/libs/transport/inc/transComm.h +++ b/source/libs/transport/inc/transComm.h @@ -240,6 +240,7 @@ typedef struct { void* pThrd; queue qmsg; TdThreadMutex mtx; // protect qmsg; + int64_t num; } SAsyncItem; typedef struct { diff --git a/source/libs/transport/src/thttp.c b/source/libs/transport/src/thttp.c index 908468f094..6096f69ca2 100644 --- a/source/libs/transport/src/thttp.c +++ b/source/libs/transport/src/thttp.c @@ -30,6 +30,8 @@ static int32_t FAST_FAILURE_LIMIT = 1; static int64_t httpDefaultChanId = -1; +static int64_t httpSeqNum = 0; + typedef struct SHttpModule { uv_loop_t* loop; SAsyncPool* asyncPool; @@ -50,6 +52,7 @@ typedef struct SHttpMsg { EHttpCompFlag flag; int8_t quit; int64_t chanId; + int64_t seq; } SHttpMsg; typedef struct SHttpClient { @@ -62,6 +65,7 @@ typedef struct SHttpClient { uint16_t port; struct sockaddr_in dest; int64_t chanId; + int64_t seq; } SHttpClient; typedef struct SHttpConnList { @@ -193,14 +197,14 @@ static FORCE_INLINE int32_t taosBuildDstAddr(const char* server, uint16_t port, uint32_t ip = 0; int32_t code = taosGetIpv4FromFqdn(server, &ip); if (code) { - tError("http-report failed to resolving domain names: %s", server); + tError("http-report failed to resolving domain names %s, reason: %s", server, tstrerror(code)); return TSDB_CODE_RPC_FQDN_ERROR; } char buf[256] = {0}; tinet_ntoa(buf, ip); int ret = uv_ip4_addr(buf, port, dest); if (ret != 0) { - tError("http-report failed to get addr %s", uv_err_name(ret)); + tError("http-report failed to get addr, reason:%s", uv_err_name(ret)); return TSDB_CODE_THIRDPARTY_ERROR; } return 0; @@ -215,14 +219,15 @@ static void* httpThread(void* arg) { static int32_t httpCreateMsg(const char* server, const char* uri, uint16_t port, char* pCont, int32_t contLen, EHttpCompFlag flag, int64_t chanId, SHttpMsg** httpMsg) { + int64_t seqNum = atomic_fetch_add_64(&httpSeqNum, 1); if (server == NULL || uri == NULL) { - tError("http-report failed to report to invalid addr, chanId:%" PRId64 "", chanId); + tError("http-report failed to report to invalid addr, chanId:%" PRId64 ", seq:%" PRId64 "", chanId, seqNum); *httpMsg = NULL; return TSDB_CODE_INVALID_PARA; } if (pCont == NULL || contLen == 0) { - tError("http-report failed to report empty packet, chanId:%" PRId64 "", chanId); + tError("http-report failed to report empty packet, chanId:%" PRId64 ", seq:%" PRId64 "", chanId, seqNum); *httpMsg = NULL; return TSDB_CODE_INVALID_PARA; } @@ -233,6 +238,7 @@ static int32_t httpCreateMsg(const char* server, const char* uri, uint16_t port, return TSDB_CODE_OUT_OF_MEMORY; } + msg->seq = seqNum; msg->port = port; msg->server = taosStrdup(server); msg->uri = taosStrdup(uri); @@ -259,7 +265,11 @@ static void httpDestroyMsg(SHttpMsg* msg) { taosMemoryFree(msg->cont); taosMemoryFree(msg); } -static void httpDestroyMsgWrapper(void* cont, void* param) { httpDestroyMsg((SHttpMsg*)cont); } +static void httpDestroyMsgWrapper(void* cont, void* param) { + SHttpMsg* pMsg = cont; + tWarn("http-report destroy msg, chanId:%" PRId64 ", seq:%" PRId64 "", pMsg->chanId, pMsg->seq); + httpDestroyMsg(pMsg); +} static void httpMayDiscardMsg(SHttpModule* http, SAsyncItem* item) { SHttpMsg *msg = NULL, *quitMsg = NULL; @@ -272,6 +282,8 @@ static void httpMayDiscardMsg(SHttpModule* http, SAsyncItem* item) { QUEUE_REMOVE(h); msg = QUEUE_DATA(h, SHttpMsg, q); if (!msg->quit) { + tError("http-report failed to report chanId:%" PRId64 ",seq:%" PRId64 ", reason: %s", msg->chanId, msg->seq, + tstrerror(TSDB_CODE_HTTP_MODULE_QUIT)); httpDestroyMsg(msg); } else { quitMsg = msg; @@ -281,6 +293,25 @@ static void httpMayDiscardMsg(SHttpModule* http, SAsyncItem* item) { QUEUE_PUSH(&item->qmsg, &quitMsg->q); } } +static void httpTrace(queue* q) { + if (!(rpcDebugFlag & DEBUG_DEBUG) || (QUEUE_IS_EMPTY(q))) { + return; + } + + int64_t startSeq = 0, endSeq = 0; + SHttpMsg* msg = NULL; + + queue* h = QUEUE_HEAD(q); + msg = QUEUE_DATA(h, SHttpMsg, q); + startSeq = msg->seq; + + h = QUEUE_TAIL(q); + msg = QUEUE_DATA(h, SHttpMsg, q); + endSeq = msg->seq; + + tDebug("http-report process msg, start_seq:%" PRId64 ", end_seq:%" PRId64 ", max_seq:%" PRId64 "", startSeq, endSeq, + atomic_load_64(&httpSeqNum) - 1); +} static void httpAsyncCb(uv_async_t* handle) { SAsyncItem* item = handle->data; @@ -290,7 +321,7 @@ static void httpAsyncCb(uv_async_t* handle) { queue wq; QUEUE_INIT(&wq); - static int32_t BATCH_SIZE = 5; + static int32_t BATCH_SIZE = 20; int32_t count = 0; (void)taosThreadMutexLock(&item->mtx); @@ -303,6 +334,8 @@ static void httpAsyncCb(uv_async_t* handle) { } (void)taosThreadMutexUnlock(&item->mtx); + httpTrace(&wq); + while (!QUEUE_IS_EMPTY(&wq)) { queue* h = QUEUE_HEAD(&wq); QUEUE_REMOVE(h); @@ -345,47 +378,50 @@ static FORCE_INLINE void clientAllocBuffCb(uv_handle_t* handle, size_t suggested } static FORCE_INLINE void clientRecvCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) { + STUB_RAND_NETWORK_ERR(nread); SHttpClient* cli = handle->data; if (nread < 0) { - tError("http-report recv error:%s", uv_strerror(nread)); + tError("http-report recv error:%s, seq:%" PRId64 "", uv_strerror(nread), cli->seq); } else { - tTrace("http-report succ to recv %d bytes", (int32_t)nread); + tTrace("http-report succ to recv %d bytes, seq:%" PRId64 "", (int32_t)nread, cli->seq); } if (!uv_is_closing((uv_handle_t*)&cli->tcp)) { uv_close((uv_handle_t*)&cli->tcp, clientCloseCb); } } static void clientSentCb(uv_write_t* req, int32_t status) { + STUB_RAND_NETWORK_ERR(status); SHttpClient* cli = req->data; if (status != 0) { - tError("http-report failed to send data, reason: %s, dst:%s:%d, chanId:%" PRId64 "", uv_strerror(status), cli->addr, - cli->port, cli->chanId); + tError("http-report failed to send data, reason: %s, dst:%s:%d, chanId:%" PRId64 ", seq:%" PRId64 "", + uv_strerror(status), cli->addr, cli->port, cli->chanId, cli->seq); if (!uv_is_closing((uv_handle_t*)&cli->tcp)) { uv_close((uv_handle_t*)&cli->tcp, clientCloseCb); } return; } else { - tTrace("http-report succ to send data, chanId:%" PRId64 "", cli->chanId); + tTrace("http-report succ to send data, chanId:%" PRId64 ", seq:%" PRId64 "", cli->chanId, cli->seq); } + status = uv_read_start((uv_stream_t*)&cli->tcp, clientAllocBuffCb, clientRecvCb); if (status != 0) { - tError("http-report failed to recv data,reason:%s, dst:%s:%d, chanId:%" PRId64 "", uv_strerror(status), cli->addr, - cli->port, cli->chanId); + tError("http-report failed to recv data,reason:%s, dst:%s:%d, chanId:%" PRId64 ", seq:%" PRId64 "", + uv_strerror(status), cli->addr, cli->port, cli->chanId, cli->seq); if (!uv_is_closing((uv_handle_t*)&cli->tcp)) { uv_close((uv_handle_t*)&cli->tcp, clientCloseCb); } } } static void clientConnCb(uv_connect_t* req, int32_t status) { + STUB_RAND_NETWORK_ERR(status); SHttpClient* cli = req->data; int64_t chanId = cli->chanId; SHttpModule* http = taosAcquireRef(httpRefMgt, chanId); if (status != 0) { httpFailFastMayUpdate(http->connStatusTable, cli->addr, cli->port, 0); - - tError("http-report failed to conn to server, reason:%s, dst:%s:%d, chanId:%" PRId64 "", uv_strerror(status), - cli->addr, cli->port, chanId); + tError("http-report failed to conn to server, reason:%s, dst:%s:%d, chanId:%" PRId64 ", seq:%" PRId64 "", + uv_strerror(status), cli->addr, cli->port, chanId, cli->seq); if (!uv_is_closing((uv_handle_t*)&cli->tcp)) { uv_close((uv_handle_t*)&cli->tcp, clientCloseCb); } @@ -398,8 +434,8 @@ static void clientConnCb(uv_connect_t* req, int32_t status) { status = uv_write(&cli->req, (uv_stream_t*)&cli->tcp, cli->wbuf, 2, clientSentCb); if (0 != status) { - tError("http-report failed to send data,reason:%s, dst:%s:%d, chanId:%" PRId64 "", uv_strerror(status), cli->addr, - cli->port, chanId); + tError("http-report failed to send data,reason:%s, dst:%s:%d, chanId:%" PRId64 ", seq:%" PRId64 "", + uv_strerror(status), cli->addr, cli->port, chanId, cli->seq); if (!uv_is_closing((uv_handle_t*)&cli->tcp)) { uv_close((uv_handle_t*)&cli->tcp, clientCloseCb); } @@ -412,7 +448,7 @@ int32_t httpSendQuit(SHttpModule* http, int64_t chanId) { if (msg == NULL) { return TSDB_CODE_OUT_OF_MEMORY; } - + msg->seq = atomic_fetch_add_64(&httpSeqNum, 1); msg->quit = 1; msg->chanId = chanId; @@ -442,10 +478,11 @@ static void httpWalkCb(uv_handle_t* handle, void* arg) { return; } static void httpHandleQuit(SHttpMsg* msg) { + int64_t seq = msg->seq; int64_t chanId = msg->chanId; taosMemoryFree(msg); - tDebug("http-report receive quit, chanId:%" PRId64 "", chanId); + tDebug("http-report receive quit, chanId:%" PRId64 ", seq:%" PRId64 "", chanId, seq); SHttpModule* http = taosAcquireRef(httpRefMgt, chanId); if (http == NULL) return; uv_walk(http->loop, httpWalkCb, NULL); @@ -542,7 +579,7 @@ static void httpHandleReq(SHttpMsg* msg) { code = TSDB_CODE_OUT_OF_MEMORY; goto END; } - + cli->seq = msg->seq; cli->conn.data = cli; cli->tcp.data = cli; cli->req.data = cli; @@ -556,8 +593,8 @@ static void httpHandleReq(SHttpMsg* msg) { cli->wbuf = wb; cli->rbuf = taosMemoryCalloc(1, HTTP_RECV_BUF_SIZE); if (cli->rbuf == NULL) { - tError("http-report failed to alloc read buf, dst:%s:%d,chanId:%" PRId64 ", reason:%s", cli->addr, cli->port, - chanId, tstrerror(TSDB_CODE_OUT_OF_MEMORY)); + tError("http-report failed to alloc read buf, dst:%s:%d, chanId:%" PRId64 ", seq:%" PRId64 ",reason:%s", cli->addr, + cli->port, chanId, cli->seq, tstrerror(TSDB_CODE_OUT_OF_MEMORY)); destroyHttpClient(cli); (void)taosReleaseRef(httpRefMgt, chanId); return; @@ -565,8 +602,8 @@ static void httpHandleReq(SHttpMsg* msg) { int err = uv_tcp_init(http->loop, &cli->tcp); if (err != 0) { - tError("http-report failed to init socket handle, dst:%s:%d,chanId:%" PRId64 ", reason:%s", cli->addr, cli->port, - chanId, uv_strerror(err)); + tError("http-report failed to init socket handle, dst:%s:%d, chanId:%" PRId64 ", seq:%" PRId64 ", reason:%s", + cli->addr, cli->port, chanId, cli->seq, uv_strerror(err)); destroyHttpClient(cli); (void)taosReleaseRef(httpRefMgt, chanId); return; @@ -575,8 +612,8 @@ static void httpHandleReq(SHttpMsg* msg) { // set up timeout to avoid stuck; int32_t fd = taosCreateSocketWithTimeout(5000); if (fd < 0) { - tError("http-report failed to open socket, dst:%s:%d, chanId:%" PRId64 ", reason:%s", cli->addr, cli->port, chanId, - tstrerror(TAOS_SYSTEM_ERROR(errno))); + tError("http-report failed to open socket, dst:%s:%d, chanId:%" PRId64 ", seq:%" PRId64 ", reason:%s", cli->addr, + cli->port, chanId, cli->seq, tstrerror(TAOS_SYSTEM_ERROR(errno))); destroyHttpClient(cli); (void)taosReleaseRef(httpRefMgt, chanId); return; @@ -584,8 +621,9 @@ static void httpHandleReq(SHttpMsg* msg) { int ret = uv_tcp_open((uv_tcp_t*)&cli->tcp, fd); if (ret != 0) { - tError("http-report failed to open socket, reason:%s, dst:%s:%d, chanId:%" PRId64 ",reason:%s", uv_strerror(ret), - cli->addr, cli->port, chanId, uv_strerror(ret)); + tError("http-report failed to open socket, dst:%s:%d, chanId:%" PRId64 ", seq:%" PRId64 ", reason:%s", cli->addr, + cli->port, chanId, cli->seq, uv_strerror(ret)); + destroyHttpClient(cli); (void)taosReleaseRef(httpRefMgt, chanId); return; @@ -593,8 +631,8 @@ static void httpHandleReq(SHttpMsg* msg) { ret = uv_tcp_connect(&cli->conn, &cli->tcp, (const struct sockaddr*)&cli->dest, clientConnCb); if (ret != 0) { - tError("http-report failed to connect to http-server,dst:%s:%d, chanId:%" PRId64 ",reson:%s", cli->addr, cli->port, - chanId, uv_strerror(ret)); + tError("http-report failed to connect to http-server,dst:%s:%d, chanId:%" PRId64 ", seq:%" PRId64 ", reson:%s", + cli->addr, cli->port, chanId, cli->seq, uv_strerror(ret)); httpFailFastMayUpdate(http->connStatusTable, cli->addr, cli->port, 0); destroyHttpClient(cli); } @@ -603,8 +641,8 @@ static void httpHandleReq(SHttpMsg* msg) { END: if (ignore == false) { - tError("http-report failed to report to addr: %s:%d, chanId:%" PRId64 ", reason:%s", msg->server, msg->port, chanId, - tstrerror(code)); + tError("http-report failed to report to addr: %s:%d, chanId:%" PRId64 ",seq:%" PRId64 " reason:%s", msg->server, + msg->port, chanId, msg->seq, tstrerror(code)); } httpDestroyMsg(msg); taosMemoryFree(header); @@ -652,6 +690,7 @@ static int32_t taosSendHttpReportImplByChan(const char* server, const char* uri, code = TSDB_CODE_HTTP_MODULE_QUIT; goto _ERROR; } + tDebug("http-report start to report, chanId:%" PRId64 ", seq:%" PRId64 "", chanId, msg->seq); code = transAsyncSend(load->asyncPool, &(msg->q)); if (code != 0) { @@ -661,6 +700,11 @@ static int32_t taosSendHttpReportImplByChan(const char* server, const char* uri, msg = NULL; _ERROR: + + if (code != 0) { + tError("http-report failed to report reason:%s, chanId:%" PRId64 ", seq:%" PRId64 "", tstrerror(code), chanId, + msg->seq); + } httpDestroyMsg(msg); if (load != NULL) taosReleaseRef(httpRefMgt, chanId); return code; @@ -684,6 +728,7 @@ int64_t transInitHttpChanImpl(); static void transHttpEnvInit() { httpRefMgt = taosOpenRef(64, transHttpDestroyHandle); httpDefaultChanId = transInitHttpChanImpl(); + httpSeqNum = 0; } void transHttpEnvDestroy() { @@ -748,7 +793,7 @@ int64_t taosInitHttpChan() { } void taosDestroyHttpChan(int64_t chanId) { - tDebug("http-report send quit, chanId:%" PRId64 "", chanId); + tDebug("http-report send quit, chanId: %" PRId64 "", chanId); int ret = 0; SHttpModule* load = taosAcquireRef(httpRefMgt, chanId); @@ -772,4 +817,4 @@ void taosDestroyHttpChan(int64_t chanId) { (void)taosReleaseRef(httpRefMgt, chanId); (void)taosRemoveRef(httpRefMgt, chanId); -} \ No newline at end of file +} diff --git a/source/libs/transport/src/transCli.c b/source/libs/transport/src/transCli.c index 862a74c72b..336a182153 100644 --- a/source/libs/transport/src/transCli.c +++ b/source/libs/transport/src/transCli.c @@ -923,10 +923,12 @@ static void cliAllocRecvBufferCb(uv_handle_t* handle, size_t suggested_size, uv_ } } static void cliRecvCb(uv_stream_t* handle, ssize_t nread, const uv_buf_t* buf) { - // impl later + STUB_RAND_NETWORK_ERR(nread); + if (handle->data == NULL) { return; } + SCliConn* conn = handle->data; SConnBuffer* pBuf = &conn->readBuf; if (nread > 0) { @@ -1117,6 +1119,8 @@ static bool cliHandleNoResp(SCliConn* conn) { return res; } static void cliSendCb(uv_write_t* req, int status) { + STUB_RAND_NETWORK_ERR(status); + SCliConn* pConn = transReqQueueRemove(req); if (pConn == NULL) return; @@ -1434,6 +1438,7 @@ static void cliHandleBatchReq(SCliBatch* pBatch, SCliThrd* pThrd) { cliSendBatch(conn); } static void cliSendBatchCb(uv_write_t* req, int status) { + STUB_RAND_NETWORK_ERR(status); SCliConn* conn = req->data; SCliThrd* thrd = conn->hostThrd; SCliBatch* p = conn->pBatch; @@ -1523,6 +1528,8 @@ void cliConnCb(uv_connect_t* req, int status) { pConn->timer = NULL; } + STUB_RAND_NETWORK_ERR(status); + if (status != 0) { cliMayUpdateFqdnCache(pThrd->fqdn2ipCache, pConn->dstAddr); if (timeout == false) { @@ -2193,7 +2200,6 @@ bool cliRecvReleaseReq(SCliConn* conn, STransMsgHead* pHead) { for (int i = 0; ahandle == 0 && i < transQueueSize(&conn->cliMsgs); i++) { SCliMsg* cliMsg = transQueueGet(&conn->cliMsgs, i); if (cliMsg->type == Release) { - ASSERTS(pMsg == NULL, "trans-cli recv invaid release-req"); tDebug("%s conn %p receive release request, refId:%" PRId64 ", ignore msg", CONN_GET_INST_LABEL(conn), conn, conn->refId); cliDestroyConn(conn, true); @@ -3015,7 +3021,6 @@ _exception: } int32_t transSendRequestWithId(void* shandle, const SEpSet* pEpSet, STransMsg* pReq, int64_t* transpointId) { if (transpointId == NULL) { - ASSERT(0); return TSDB_CODE_INVALID_PARA; } int32_t code = 0; diff --git a/source/libs/transport/src/transComm.c b/source/libs/transport/src/transComm.c index 5d82e157b3..a0836eae3d 100644 --- a/source/libs/transport/src/transComm.c +++ b/source/libs/transport/src/transComm.c @@ -183,7 +183,7 @@ int32_t transResetBuffer(SConnBuffer* connBuf, int8_t resetBuf) { } } } else { - ASSERTS(0, "invalid read from sock buf"); + tError("failed to reset buffer, total:%d, len:%d, reason:%s", p->total, p->len, tstrerror(TSDB_CODE_INVALID_MSG)); return TSDB_CODE_INVALID_MSG; } return 0; @@ -869,3 +869,8 @@ int32_t transUtilSWhiteListToStr(SIpWhiteList* pList, char** ppBuf) { *ppBuf = pBuf; return len; } + +// int32_t transGenRandomError(int32_t status) { +// STUB_RAND_NETWORK_ERR(status) +// return status; +// } diff --git a/source/libs/transport/src/transSvr.c b/source/libs/transport/src/transSvr.c index c1b934c812..53a7dee7be 100644 --- a/source/libs/transport/src/transSvr.c +++ b/source/libs/transport/src/transSvr.c @@ -468,8 +468,8 @@ static bool uvHandleReq(SSvrConn* 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) { + tError("%s handle %p conn:%p handle failed to init" PRIu64, transLabel(pTransInst), transMsg.info.handle, pConn); return false; } @@ -493,6 +493,8 @@ void uvOnRecvCb(uv_stream_t* cli, ssize_t nread, const uv_buf_t* buf) { SSvrConn* conn = cli->data; SWorkThrd* pThrd = conn->hostThrd; + STUB_RAND_NETWORK_ERR(nread); + if (true == pThrd->quit) { tInfo("work thread received quit msg, destroy conn"); destroyConn(conn, true); @@ -553,6 +555,7 @@ void uvOnTimeoutCb(uv_timer_t* handle) { } void uvOnSendCb(uv_write_t* req, int status) { + STUB_RAND_NETWORK_ERR(status); SSvrConn* conn = transReqQueueRemove(req); if (conn == NULL) return; @@ -602,6 +605,7 @@ void uvOnSendCb(uv_write_t* req, int status) { } } static void uvOnPipeWriteCb(uv_write_t* req, int status) { + STUB_RAND_NETWORK_ERR(status); if (status == 0) { tTrace("success to dispatch conn to work thread"); } else { @@ -949,6 +953,7 @@ void uvOnAcceptCb(uv_stream_t* stream, int status) { } } void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) { + STUB_RAND_NETWORK_ERR(nread); if (nread < 0) { if (nread != UV_EOF) { tError("read error %s", uv_err_name(nread)); @@ -960,15 +965,19 @@ void uvOnConnectionCb(uv_stream_t* q, ssize_t nread, const uv_buf_t* buf) { return; } // free memory allocated by - ASSERTS(nread == strlen(notify), "trans-svr mem corrupted"); - ASSERTS(buf->base[0] == notify[0], "trans-svr mem corrupted"); + if (nread != strlen(notify) || strncmp(buf->base, notify, strlen(notify)) != 0) { + tError("failed to read pip "); + taosMemoryFree(buf->base); + uv_close((uv_handle_t*)q, NULL); + } + taosMemoryFree(buf->base); SWorkThrd* pThrd = q->data; uv_pipe_t* pipe = (uv_pipe_t*)q; if (!uv_pipe_pending_count(pipe)) { - tError("No pending count"); + tError("no pending count, unexpected error"); uv_close((uv_handle_t*)q, NULL); return; } @@ -1041,9 +1050,11 @@ void* transAcceptThread(void* arg) { return NULL; } void uvOnPipeConnectionCb(uv_connect_t* connect, int status) { + STUB_RAND_NETWORK_ERR(status); if (status != 0) { return; - } + }; + SWorkThrd* pThrd = container_of(connect, SWorkThrd, connect_req); (void)uv_read_start((uv_stream_t*)pThrd->pipe, uvAllocConnBufferCb, uvOnConnectionCb); } @@ -1344,25 +1355,32 @@ static void uvPipeListenCb(uv_stream_t* handle, int status) { uv_pipe_t* pipe = &(srv->pipe[srv->numOfWorkerReady][0]); int ret = uv_pipe_init(srv->loop, pipe, 1); - ASSERTS(ret == 0, "trans-svr failed to init pipe"); - if (ret != 0) return; + if (ret != 0) { + tError("trans-svr failed to init pipe, errmsg: %s", uv_err_name(ret)); + } ret = uv_accept((uv_stream_t*)&srv->pipeListen, (uv_stream_t*)pipe); - ASSERTS(ret == 0, "trans-svr failed to accept pipe msg"); - if (ret != 0) return; + if (ret != 0) { + tError("trans-svr failed to accept pipe, errmsg: %s", uv_err_name(ret)); + return; + } ret = uv_is_readable((uv_stream_t*)pipe); - ASSERTS(ret == 1, "trans-svr pipe status corrupted"); - if (ret != 1) return; - + if (ret != 1) { + tError("trans-svr failed to check pipe, pip not readable"); + return; + } ret = uv_is_writable((uv_stream_t*)pipe); - ASSERTS(ret == 1, "trans-svr pipe status corrupted"); - if (ret != 1) return; + if (ret != 1) { + tError("trans-svr failed to check pipe, pip not writable"); + return; + } ret = uv_is_closing((uv_handle_t*)pipe); - ASSERTS(ret == 0, "trans-svr pipe status corrupted"); - if (ret != 0) return; - + if (ret != 0) { + tError("trans-svr failed to check pipe, pip is closing"); + return; + } srv->numOfWorkerReady++; } diff --git a/source/os/src/osFile.c b/source/os/src/osFile.c index c274c7fbab..144baeb148 100644 --- a/source/os/src/osFile.c +++ b/source/os/src/osFile.c @@ -904,7 +904,7 @@ int32_t taosFStatFile(TdFilePtr pFile, int64_t *size, int32_t *mtime) { int32_t code = _fstat64(pFile->fd, &fileStat); #else struct stat fileStat; - int32_t code = fstat(pFile->fd, &fileStat); + int32_t code = fstat(pFile->fd, &fileStat); #endif if (-1 == code) { terrno = TAOS_SYSTEM_ERROR(errno); @@ -1611,7 +1611,7 @@ int taosSeekCFile(FILE *file, int64_t offset, int whence) { #ifdef WINDOWS return _fseeki64(file, offset, whence); #else - int code = fseeko(file, offset, whence); + int code = fseeko(file, offset, whence); if (-1 == code) { terrno = TAOS_SYSTEM_ERROR(errno); } diff --git a/source/os/src/osMemory.c b/source/os/src/osMemory.c index 91eb7763bc..49a5c2a2a2 100644 --- a/source/os/src/osMemory.c +++ b/source/os/src/osMemory.c @@ -24,7 +24,7 @@ int32_t tsRandErrChance = 1; int64_t tsRandErrDivisor = 10001; -int64_t tsRandErrScope = (RAND_ERR_MEMORY | RAND_ERR_FILE); +int64_t tsRandErrScope = (RAND_ERR_MEMORY | RAND_ERR_FILE | RAND_ERR_NETWORK); threadlocal bool tsEnableRandErr = 0; #if defined(USE_TD_MEMORY) || defined(USE_ADDR2LINE) @@ -385,7 +385,7 @@ char *taosStrdup(const char *ptr) { } #endif - return tstrdup(ptr); + return tstrdup(ptr); #endif } diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 7c7afe8d6f..6701842ec9 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -389,6 +389,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_DNODE_INVALID_CHARSET, "charset not match") TAOS_DEFINE_ERROR(TSDB_CODE_DNODE_INVALID_LOCALE, "locale not match") TAOS_DEFINE_ERROR(TSDB_CODE_DNODE_INVALID_TTL_CHG_ON_WR, "ttlChangeOnWrite not match") TAOS_DEFINE_ERROR(TSDB_CODE_DNODE_INVALID_EN_WHITELIST, "enableWhiteList not match") +TAOS_DEFINE_ERROR(TSDB_CODE_MNODE_STOPPED, "Mnode stopped") // vnode TAOS_DEFINE_ERROR(TSDB_CODE_VND_INVALID_VGROUP_ID, "Vnode is closed or removed") diff --git a/source/util/src/tlosertree.c b/source/util/src/tlosertree.c index 7973a84593..06ed9b7690 100644 --- a/source/util/src/tlosertree.c +++ b/source/util/src/tlosertree.c @@ -19,8 +19,11 @@ #include "tlog.h" // Set the initial value of the multiway merge tree. -static void tMergeTreeInit(SMultiwayMergeTreeInfo* pTree) { - ASSERT((pTree->totalSources & 0x01) == 0 && (pTree->numOfSources << 1 == pTree->totalSources)); +static int32_t tMergeTreeInit(SMultiwayMergeTreeInfo* pTree) { + if (!((pTree->totalSources & 0x01) == 0 && (pTree->numOfSources << 1 == pTree->totalSources))) { + uError("losertree failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INVALID_PARA; + } for (int32_t i = 0; i < pTree->totalSources; ++i) { if (i < pTree->numOfSources) { @@ -29,6 +32,7 @@ static void tMergeTreeInit(SMultiwayMergeTreeInfo* pTree) { pTree->pNode[i].index = i - pTree->numOfSources; } } + return TSDB_CODE_SUCCESS; } int32_t tMergeTreeCreate(SMultiwayMergeTreeInfo** pTree, uint32_t numOfSources, void* param, @@ -50,7 +54,11 @@ int32_t tMergeTreeCreate(SMultiwayMergeTreeInfo** pTree, uint32_t numOfSources, pTreeInfo->comparFn = compareFn; // set initial value for loser tree - tMergeTreeInit(pTreeInfo); + int32_t code = tMergeTreeInit(pTreeInfo); + if (TSDB_CODE_SUCCESS != code) { + taosMemoryFree(pTreeInfo); + return code; + } #ifdef _DEBUG_VIEW printf("the initial value of loser tree:\n"); @@ -58,7 +66,11 @@ int32_t tMergeTreeCreate(SMultiwayMergeTreeInfo** pTree, uint32_t numOfSources, #endif for (int32_t i = totalEntries - 1; i >= numOfSources; i--) { - tMergeTreeAdjust(pTreeInfo, i); + code = tMergeTreeAdjust(pTreeInfo, i); + if (TSDB_CODE_SUCCESS != code) { + taosMemoryFree(pTreeInfo); + return code; + } } #if defined(_DEBUG_VIEW) @@ -79,13 +91,17 @@ void tMergeTreeDestroy(SMultiwayMergeTreeInfo** pTree) { taosMemoryFreeClear(*pTree); } -void tMergeTreeAdjust(SMultiwayMergeTreeInfo* pTree, int32_t idx) { - ASSERT(idx <= pTree->totalSources - 1 && idx >= pTree->numOfSources && pTree->totalSources >= 2); +int32_t tMergeTreeAdjust(SMultiwayMergeTreeInfo* pTree, int32_t idx) { + int32_t code = 0; + if (!(idx <= pTree->totalSources - 1 && idx >= pTree->numOfSources && pTree->totalSources >= 2)) { + uError("losertree failed at: %s:%d", __func__, __LINE__); + return TSDB_CODE_INVALID_PARA; + } if (pTree->totalSources == 2) { pTree->pNode[0].index = 0; pTree->pNode[1].index = 0; - return; + return code; } int32_t parentId = idx >> 1; @@ -95,7 +111,7 @@ void tMergeTreeAdjust(SMultiwayMergeTreeInfo* pTree, int32_t idx) { STreeNode* pCur = &pTree->pNode[parentId]; if (pCur->index == -1) { pTree->pNode[parentId] = kLeaf; - return; + return code; } int32_t ret = pTree->comparFn(pCur, &kLeaf, pTree->param); @@ -112,13 +128,21 @@ void tMergeTreeAdjust(SMultiwayMergeTreeInfo* pTree, int32_t idx) { // winner cannot be identical to the loser, which is pTreeNode[1] pTree->pNode[0] = kLeaf; } + return code; } -void tMergeTreeRebuild(SMultiwayMergeTreeInfo* pTree) { - tMergeTreeInit(pTree); - for (int32_t i = pTree->totalSources - 1; i >= pTree->numOfSources; i--) { - tMergeTreeAdjust(pTree, i); +int32_t tMergeTreeRebuild(SMultiwayMergeTreeInfo* pTree) { + int32_t code = tMergeTreeInit(pTree); + if (TSDB_CODE_SUCCESS != code) { + return code; } + for (int32_t i = pTree->totalSources - 1; i >= pTree->numOfSources; i--) { + code = tMergeTreeAdjust(pTree, i); + if (TSDB_CODE_SUCCESS != code) { + return code; + } + } + return TSDB_CODE_SUCCESS; } /* diff --git a/source/util/src/tpagedbuf.c b/source/util/src/tpagedbuf.c index ef0a45233a..32b9b6e18d 100644 --- a/source/util/src/tpagedbuf.c +++ b/source/util/src/tpagedbuf.c @@ -313,7 +313,6 @@ static SListNode* getEldestUnrefedPage(SDiskbasedBuf* pBuf) { static char* evictBufPage(SDiskbasedBuf* pBuf) { SListNode* pn = getEldestUnrefedPage(pBuf); if (pn == NULL) { // no available buffer pages now, return. - terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } diff --git a/tests/script/tsim/query/event.sim b/tests/script/tsim/query/event.sim index 16fe0f4a13..740cdb11dd 100644 --- a/tests/script/tsim/query/event.sim +++ b/tests/script/tsim/query/event.sim @@ -236,4 +236,54 @@ sql_error select count(*) from sta event_window start with f1 >0 end with f2 > 0 sql_error select count(*) from sta event_window start with f1 >0 end with f2 > 0 group by tbname; sql_error select count(*) from sta event_window start with f1 >0 end with f2 > 0 fill(NULL); +print step2 +print =============== create database +sql create database test2 vgroups 4; +sql use test2; +sql create stable st(ts timestamp,a int,b int,c int, d double) tags(ta int,tb int,tc int); +sql create table t1 using st tags(1,1,1); +sql create table t2 using st tags(2,2,2); +sql create table t3 using st tags(3,3,3); + +sql insert into t1 values(1648791213000,1,2,3,1.0); +sql insert into t1 values(1648791223001,2,2,3,1.1); +sql insert into t1 values(1648791233002,3,2,3,2.1); +sql insert into t1 values(1648791243003,4,2,3,3.1); +sql insert into t1 values(1648791253004,5,2,3,4.1); + +sql insert into t2 values(1648791213000,1,2,3,1.0); +sql insert into t2 values(1648791223001,2,2,3,1.1); +sql insert into t2 values(1648791233002,3,2,3,2.1); +sql insert into t2 values(1648791243003,4,2,3,3.1); +sql insert into t2 values(1648791253004,5,2,3,4.1); + +sql insert into t3 values(1648791213000,1,2,3,1.0); +sql insert into t3 values(1648791223001,2,2,3,1.1); +sql insert into t3 values(1648791233002,3,2,3,2.1); +sql insert into t3 values(1648791243003,4,2,3,3.1); +sql insert into t3 values(1648791253004,5,2,3,4.1); + +$loop_count = 0 +loop4: + +sleep 300 + +sql select _wstart, count(*) c1, tbname from st partition by tbname event_window start with a > 0 end with b = 2 slimit 2 limit 2; + +print $data00 $data01 $data02 $data03 +print $data10 $data11 $data12 $data13 +print $data20 $data21 $data22 $data23 +print $data30 $data31 $data32 $data33 + +$loop_count = $loop_count + 1 +if $loop_count == 10 then + return -1 +endi + +# row 0 +if $rows != 4 then + print ======rows=$rows + goto loop4 +endi + system sh/exec.sh -n dnode1 -s stop -x SIGINT diff --git a/tests/script/tsim/query/query_count1.sim b/tests/script/tsim/query/query_count1.sim index 043b604263..9711a09d97 100644 --- a/tests/script/tsim/query/query_count1.sim +++ b/tests/script/tsim/query/query_count1.sim @@ -101,5 +101,57 @@ sql_error select _wstart as s, count(*) c1, sum(b), max(c) from t1 count_windo sql select _wstart as s, count(*) c1, sum(b), max(c) from t1 count_window(2); sql select _wstart as s, count(*) c1, sum(b), max(c) from t1 count_window(2147483647); + +print step3 +print =============== create database +sql create database test2 vgroups 4; +sql use test2; +sql create stable st(ts timestamp,a int,b int,c int, d double) tags(ta int,tb int,tc int); +sql create table t1 using st tags(1,1,1); +sql create table t2 using st tags(2,2,2); +sql create table t3 using st tags(3,3,3); + +sql insert into t1 values(1648791213000,1,2,3,1.0); +sql insert into t1 values(1648791223001,2,2,3,1.1); +sql insert into t1 values(1648791233002,3,2,3,2.1); +sql insert into t1 values(1648791243003,4,2,3,3.1); +sql insert into t1 values(1648791253004,5,2,3,4.1); + +sql insert into t2 values(1648791213000,1,2,3,1.0); +sql insert into t2 values(1648791223001,2,2,3,1.1); +sql insert into t2 values(1648791233002,3,2,3,2.1); +sql insert into t2 values(1648791243003,4,2,3,3.1); +sql insert into t2 values(1648791253004,5,2,3,4.1); + +sql insert into t3 values(1648791213000,1,2,3,1.0); +sql insert into t3 values(1648791223001,2,2,3,1.1); +sql insert into t3 values(1648791233002,3,2,3,2.1); +sql insert into t3 values(1648791243003,4,2,3,3.1); +sql insert into t3 values(1648791253004,5,2,3,4.1); + +$loop_count = 0 +loop4: + +sleep 300 + +sql select _wstart, count(*) c1, tbname from st partition by tbname count_window(2) slimit 2 limit 2; + +print $data00 $data01 $data02 $data03 +print $data10 $data11 $data12 $data13 +print $data20 $data21 $data22 $data23 +print $data30 $data31 $data32 $data33 + +$loop_count = $loop_count + 1 +if $loop_count == 10 then + return -1 +endi + +# row 0 +if $rows != 4 then + print ======rows=$rows + goto loop4 +endi + + print query_count0 end system sh/exec.sh -n dnode1 -s stop -x SIGINT diff --git a/tests/system-test/2-query/concat.py b/tests/system-test/2-query/concat.py index 326f6940f6..b43d4fafa8 100644 --- a/tests/system-test/2-query/concat.py +++ b/tests/system-test/2-query/concat.py @@ -144,6 +144,9 @@ class TDTestCase: f"{dbname}.ct2", f"{dbname}.ct4", ] + tdSql.query("select concat(null,null)") # TD-31571 + tdSql.checkRows(1) + tdSql.checkData(0, 0, None) for tb in tbname: for i in range(2,8): self.__concat_check(tb,i) diff --git a/tests/system-test/2-query/concat_ws.py b/tests/system-test/2-query/concat_ws.py index 26731715c1..338166c61e 100644 --- a/tests/system-test/2-query/concat_ws.py +++ b/tests/system-test/2-query/concat_ws.py @@ -156,7 +156,9 @@ class TDTestCase: f"{dbname}.ct2", f"{dbname}.ct4", ] - + tdSql.query("select concat_ws(null,null,null);") # TD-31572 + tdSql.checkRows(1) + tdSql.checkData(0, 0, None) for tb in tbname: for errsql in self.__concat_ws_err_check(tb): tdSql.error(sql=errsql)