From fcf32af53f37e6a098d768e6147decf403cec971 Mon Sep 17 00:00:00 2001 From: dmchen Date: Tue, 23 Jul 2024 09:46:40 +0000 Subject: [PATCH 1/5] fix/TD-30989 --- source/dnode/mnode/impl/src/mndVgroup.c | 954 +++++++++++++++--------- source/dnode/mnode/impl/src/mndView.c | 39 +- 2 files changed, 616 insertions(+), 377 deletions(-) diff --git a/source/dnode/mnode/impl/src/mndVgroup.c b/source/dnode/mnode/impl/src/mndVgroup.c index 0422bfabff..b26b563e36 100644 --- a/source/dnode/mnode/impl/src/mndVgroup.c +++ b/source/dnode/mnode/impl/src/mndVgroup.c @@ -189,9 +189,17 @@ static int32_t mndNewVgActionValidate(SMnode *pMnode, STrans *pTrans, SSdbRaw *p int code = -1; pRow = mndVgroupActionDecode(pRaw); - if (pRow == NULL) goto _OVER; + if (pRow == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } pVgroup = sdbGetRowObj(pRow); - if (pVgroup == NULL) goto _OVER; + if (pVgroup == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } int32_t maxVgId = sdbGetMaxId(pMnode->pSdb, SDB_VGROUP); if (maxVgId > pVgroup->vgId) { @@ -203,7 +211,7 @@ static int32_t mndNewVgActionValidate(SMnode *pMnode, STrans *pTrans, SSdbRaw *p _OVER: if (pVgroup) mndVgroupActionDelete(pSdb, pVgroup); taosMemoryFreeClear(pRow); - return code; + TAOS_RETURN(code); } static int32_t mndVgroupActionInsert(SSdb *pSdb, SVgObj *pVgroup) { @@ -756,28 +764,24 @@ static int32_t mndGetAvailableDnode(SMnode *pMnode, SDbObj *pDb, SVgObj *pVgroup if (size < pVgroup->replica) { mError("db:%s, vgId:%d, no enough online dnodes:%d to alloc %d replica", pVgroup->dbName, pVgroup->vgId, size, pVgroup->replica); - terrno = TSDB_CODE_MND_NO_ENOUGH_DNODES; - return -1; + TAOS_RETURN(TSDB_CODE_MND_NO_ENOUGH_DNODES); } for (int32_t v = 0; v < pVgroup->replica; ++v) { SVnodeGid *pVgid = &pVgroup->vnodeGid[v]; SDnodeObj *pDnode = taosArrayGet(pArray, v); if (pDnode == NULL) { - terrno = TSDB_CODE_MND_NO_ENOUGH_DNODES; - return -1; + TAOS_RETURN(TSDB_CODE_MND_NO_ENOUGH_DNODES); } if (pDnode->numOfVnodes >= pDnode->numOfSupportVnodes) { - terrno = TSDB_CODE_MND_NO_ENOUGH_VNODES; - return -1; + TAOS_RETURN(TSDB_CODE_MND_NO_ENOUGH_VNODES); } int64_t vgMem = mndGetVgroupMemory(pMnode, pDb, pVgroup); if (pDnode->memAvail - vgMem - pDnode->memUsed <= 0) { mError("db:%s, vgId:%d, no enough memory:%" PRId64 " in dnode:%d, avail:%" PRId64 " used:%" PRId64, pVgroup->dbName, pVgroup->vgId, vgMem, pDnode->id, pDnode->memAvail, pDnode->memUsed); - terrno = TSDB_CODE_MND_NO_ENOUGH_MEM_IN_DNODE; - return -1; + TAOS_RETURN(TSDB_CODE_MND_NO_ENOUGH_MEM_IN_DNODE); } else { pDnode->memUsed += vgMem; } @@ -799,8 +803,13 @@ static int32_t mndGetAvailableDnode(SMnode *pMnode, SDbObj *pDb, SVgObj *pVgroup } int32_t mndAllocSmaVgroup(SMnode *pMnode, SDbObj *pDb, SVgObj *pVgroup) { + int32_t code = 0; SArray *pArray = mndBuildDnodesArray(pMnode, 0); - if (pArray == NULL) return -1; + if (pArray == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } pVgroup->vgId = sdbGetMaxId(pMnode->pSdb, SDB_VGROUP); pVgroup->isTsma = 1; @@ -825,12 +834,16 @@ int32_t mndAllocVgroup(SMnode *pMnode, SDbObj *pDb, SVgObj **ppVgroups) { pVgroups = taosMemoryCalloc(pDb->cfg.numOfVgroups, sizeof(SVgObj)); if (pVgroups == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; + code = TSDB_CODE_OUT_OF_MEMORY; goto _OVER; } pArray = mndBuildDnodesArray(pMnode, 0); - if (pArray == NULL) goto _OVER; + if (pArray == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } mInfo("db:%s, total %d dnodes used to create %d vgroups (%d vnodes)", pDb->name, (int32_t)taosArrayGetSize(pArray), pDb->cfg.numOfVgroups, pDb->cfg.numOfVgroups * pDb->cfg.replications); @@ -875,7 +888,7 @@ int32_t mndAllocVgroup(SMnode *pMnode, SDbObj *pDb, SVgObj **ppVgroups) { _OVER: if (code != 0) taosMemoryFree(pVgroups); taosArrayDestroy(pArray); - return code; + TAOS_RETURN(code); } SEpSet mndGetVgroupEpset(SMnode *pMnode, const SVgObj *pVgroup) { @@ -901,7 +914,7 @@ SEpSet mndGetVgroupEpset(SMnode *pMnode, const SVgObj *pVgroup) { SEpSet mndGetVgroupEpsetById(SMnode *pMnode, int32_t vgId) { SEpSet epset = {0}; - SVgObj * pVgroup = mndAcquireVgroup(pMnode, vgId); + SVgObj *pVgroup = mndAcquireVgroup(pMnode, vgId); if (!pVgroup) return epset; for (int32_t v = 0; v < pVgroup->replica; ++v) { @@ -1199,6 +1212,7 @@ static void mndCancelGetNextVnode(SMnode *pMnode, void *pIter) { } static int32_t mndAddVnodeToVgroup(SMnode *pMnode, STrans *pTrans, SVgObj *pVgroup, SArray *pArray) { + int32_t code = 0; taosArraySort(pArray, (__compar_fn_t)mndCompareDnodeVnodes); for (int32_t i = 0; i < taosArrayGetSize(pArray); ++i) { SDnodeObj *pDnode = taosArrayGet(pArray, i); @@ -1219,20 +1233,17 @@ static int32_t mndAddVnodeToVgroup(SMnode *pMnode, STrans *pTrans, SVgObj *pVgro if (used) continue; if (pDnode == NULL) { - terrno = TSDB_CODE_MND_NO_ENOUGH_DNODES; - return -1; + TAOS_RETURN(TSDB_CODE_MND_NO_ENOUGH_DNODES); } if (pDnode->numOfVnodes >= pDnode->numOfSupportVnodes) { - terrno = TSDB_CODE_MND_NO_ENOUGH_VNODES; - return -1; + TAOS_RETURN(TSDB_CODE_MND_NO_ENOUGH_VNODES); } int64_t vgMem = mndGetVgroupMemory(pMnode, NULL, pVgroup); if (pDnode->memAvail - vgMem - pDnode->memUsed <= 0) { mError("db:%s, vgId:%d, no enough memory:%" PRId64 " in dnode:%d avail:%" PRId64 " used:%" PRId64, pVgroup->dbName, pVgroup->vgId, vgMem, pDnode->id, pDnode->memAvail, pDnode->memUsed); - terrno = TSDB_CODE_MND_NO_ENOUGH_MEM_IN_DNODE; - return -1; + TAOS_RETURN(TSDB_CODE_MND_NO_ENOUGH_MEM_IN_DNODE); } else { pDnode->memUsed += vgMem; } @@ -1246,19 +1257,23 @@ static int32_t mndAddVnodeToVgroup(SMnode *pMnode, STrans *pTrans, SVgObj *pVgro pDnode->numOfVnodes++; SSdbRaw *pVgRaw = mndVgroupActionEncode(pVgroup); - if (pVgRaw == NULL) return -1; - if (mndTransAppendRedolog(pTrans, pVgRaw) != 0) { + if (pVgRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } + if ((code = mndTransAppendRedolog(pTrans, pVgRaw)) != 0) { sdbFreeRaw(pVgRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pVgRaw, SDB_STATUS_READY); - return 0; + TAOS_RETURN(code); } - terrno = TSDB_CODE_MND_NO_ENOUGH_DNODES; - mError("db:%s, failed to add vnode to vgId:%d since %s", pVgroup->dbName, pVgroup->vgId, terrstr()); - return -1; + code = TSDB_CODE_MND_NO_ENOUGH_DNODES; + mError("db:%s, failed to add vnode to vgId:%d since %s", pVgroup->dbName, pVgroup->vgId, tstrerror(code)); + TAOS_RETURN(code); } static int32_t mndRemoveVnodeFromVgroup(SMnode *pMnode, STrans *pTrans, SVgObj *pVgroup, SArray *pArray, @@ -1293,9 +1308,9 @@ static int32_t mndRemoveVnodeFromVgroup(SMnode *pMnode, STrans *pTrans, SVgObj * _OVER: if (code != 0) { - terrno = TSDB_CODE_APP_ERROR; - mError("db:%s, failed to remove vnode from vgId:%d since %s", pVgroup->dbName, pVgroup->vgId, terrstr()); - return -1; + code = TSDB_CODE_APP_ERROR; + mError("db:%s, failed to remove vnode from vgId:%d since %s", pVgroup->dbName, pVgroup->vgId, tstrerror(code)); + TAOS_RETURN(code); } for (int32_t vn = 0; vn < pVgroup->replica; ++vn) { @@ -1304,14 +1319,18 @@ _OVER: } SSdbRaw *pVgRaw = mndVgroupActionEncode(pVgroup); - if (pVgRaw == NULL) return -1; + if (pVgRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } if (mndTransAppendRedolog(pTrans, pVgRaw) != 0) { sdbFreeRaw(pVgRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pVgRaw, SDB_STATUS_READY); - return 0; + TAOS_RETURN(code); } static int32_t mndRemoveVnodeFromVgroupWithoutSave(SMnode *pMnode, STrans *pTrans, SVgObj *pVgroup, SArray *pArray, @@ -1346,9 +1365,9 @@ static int32_t mndRemoveVnodeFromVgroupWithoutSave(SMnode *pMnode, STrans *pTran _OVER: if (code != 0) { - terrno = TSDB_CODE_APP_ERROR; - mError("db:%s, failed to remove vnode from vgId:%d since %s", pVgroup->dbName, pVgroup->vgId, terrstr()); - return -1; + code = TSDB_CODE_APP_ERROR; + mError("db:%s, failed to remove vnode from vgId:%d since %s", pVgroup->dbName, pVgroup->vgId, tstrerror(code)); + TAOS_RETURN(code); } for (int32_t vn = 0; vn < pVgroup->replica; ++vn) { @@ -1356,10 +1375,11 @@ _OVER: mInfo("db:%s, vgId:%d, vn:%d dnode:%d is reserved", pVgroup->dbName, pVgroup->vgId, vn, pVgid->dnodeId); } - return 0; + TAOS_RETURN(code); } int32_t mndAddCreateVnodeAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, SVnodeGid *pVgid) { + int32_t code = 0; STransAction action = {0}; SDnodeObj *pDnode = mndAcquireDnode(pMnode, pVgid->dnodeId); @@ -1376,38 +1396,44 @@ int32_t mndAddCreateVnodeAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVg action.msgType = TDMT_DND_CREATE_VNODE; action.acceptableCode = TSDB_CODE_VND_ALREADY_EXIST; - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pReq); - return -1; + TAOS_RETURN(code); } - return 0; + TAOS_RETURN(code); } int32_t mndRestoreAddCreateVnodeAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, SDnodeObj *pDnode) { + int32_t code = 0; STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); int32_t contLen = 0; void *pReq = mndBuildCreateVnodeReq(pMnode, pDnode, pDb, pVgroup, &contLen); - if (pReq == NULL) return -1; + if (pReq == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } action.pCont = pReq; action.contLen = contLen; action.msgType = TDMT_DND_CREATE_VNODE; action.acceptableCode = TSDB_CODE_VND_ALREADY_EXIST; - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pReq); - return -1; + TAOS_RETURN(code); } - return 0; + TAOS_RETURN(code); } int32_t mndAddAlterVnodeConfirmAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup) { + int32_t code = 0; STransAction action = {0}; action.epSet = mndGetVgroupEpset(pMnode, pVgroup); @@ -1415,8 +1441,7 @@ int32_t mndAddAlterVnodeConfirmAction(SMnode *pMnode, STrans *pTrans, SDbObj *pD int32_t contLen = sizeof(SMsgHead); SMsgHead *pHead = taosMemoryMalloc(contLen); if (pHead == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } pHead->contLen = htonl(contLen); @@ -1428,30 +1453,34 @@ int32_t mndAddAlterVnodeConfirmAction(SMnode *pMnode, STrans *pTrans, SDbObj *pD // incorrect redirect result will cause this erro action.retryCode = TSDB_CODE_VND_INVALID_VGROUP_ID; - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pHead); - return -1; + TAOS_RETURN(code); } - return 0; + TAOS_RETURN(code); } int32_t mndAddChangeConfigAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pOldVgroup, SVgObj *pNewVgroup, int32_t dnodeId) { + int32_t code = 0; STransAction action = {0}; action.epSet = mndGetVgroupEpset(pMnode, pNewVgroup); int32_t contLen = 0; void *pReq = mndBuildAlterVnodeReplicaReq(pMnode, pDb, pNewVgroup, dnodeId, &contLen); - if (pReq == NULL) return -1; + if (pReq == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } int32_t totallen = contLen + sizeof(SMsgHead); SMsgHead *pHead = taosMemoryMalloc(totallen); if (pHead == NULL) { taosMemoryFree(pReq); - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } pHead->contLen = htonl(totallen); @@ -1464,73 +1493,93 @@ int32_t mndAddChangeConfigAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SV action.contLen = totallen; action.msgType = TDMT_SYNC_CONFIG_CHANGE; - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pHead); - return -1; + TAOS_RETURN(code); } - return 0; + TAOS_RETURN(code); } static int32_t mndAddAlterVnodeHashRangeAction(SMnode *pMnode, STrans *pTrans, int32_t srcVgId, SVgObj *pVgroup) { + int32_t code = 0; STransAction action = {0}; action.epSet = mndGetVgroupEpset(pMnode, pVgroup); int32_t contLen = 0; void *pReq = mndBuildAlterVnodeHashRangeReq(pMnode, srcVgId, pVgroup, &contLen); - if (pReq == NULL) return -1; + if (pReq == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } action.pCont = pReq; action.contLen = contLen; action.msgType = TDMT_VND_ALTER_HASHRANGE; action.acceptableCode = TSDB_CODE_VND_ALREADY_EXIST; - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pReq); - return -1; + TAOS_RETURN(code); } mInfo("trans:%d, add alter vnode hash range action for from vgId:%d to vgId:%d", pTrans->id, srcVgId, pVgroup->vgId); - return 0; + TAOS_RETURN(code); } int32_t mndAddAlterVnodeConfigAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup) { + int32_t code = 0; STransAction action = {0}; action.epSet = mndGetVgroupEpset(pMnode, pVgroup); int32_t contLen = 0; void *pReq = mndBuildAlterVnodeConfigReq(pMnode, pDb, pVgroup, &contLen); - if (pReq == NULL) return -1; + if (pReq == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } action.pCont = pReq; action.contLen = contLen; action.msgType = TDMT_VND_ALTER_CONFIG; - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pReq); - return -1; + TAOS_RETURN(code); } - return 0; + TAOS_RETURN(code); } int32_t mndAddNewVgPrepareAction(SMnode *pMnode, STrans *pTrans, SVgObj *pVg) { + int32_t code = 0; SSdbRaw *pRaw = mndVgroupActionEncode(pVg); - if (pRaw == NULL) goto _err; + if (pRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _err; + } - if (mndTransAppendPrepareLog(pTrans, pRaw) != 0) goto _err; + TAOS_CHECK_GOTO(mndTransAppendPrepareLog(pTrans, pRaw), NULL, _err); (void)sdbSetRawStatus(pRaw, SDB_STATUS_CREATING); pRaw = NULL; - return 0; + TAOS_RETURN(code); _err: sdbFreeRaw(pRaw); - return -1; + TAOS_RETURN(code); } int32_t mndAddAlterVnodeReplicaAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, int32_t dnodeId) { + int32_t code = 0; SDnodeObj *pDnode = mndAcquireDnode(pMnode, dnodeId); - if (pDnode == NULL) return -1; + if (pDnode == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); @@ -1538,23 +1587,32 @@ int32_t mndAddAlterVnodeReplicaAction(SMnode *pMnode, STrans *pTrans, SDbObj *pD int32_t contLen = 0; void *pReq = mndBuildAlterVnodeReplicaReq(pMnode, pDb, pVgroup, dnodeId, &contLen); - if (pReq == NULL) return -1; + if (pReq == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } action.pCont = pReq; action.contLen = contLen; action.msgType = TDMT_VND_ALTER_REPLICA; - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pReq); - return -1; + TAOS_RETURN(code); } - return 0; + TAOS_RETURN(code); } int32_t mndAddCheckLearnerCatchupAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, int32_t dnodeId) { + int32_t code = 0; SDnodeObj *pDnode = mndAcquireDnode(pMnode, dnodeId); - if (pDnode == NULL) return -1; + if (pDnode == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); @@ -1562,7 +1620,11 @@ int32_t mndAddCheckLearnerCatchupAction(SMnode *pMnode, STrans *pTrans, SDbObj * int32_t contLen = 0; void *pReq = mndBuildCheckLearnCatchupReq(pMnode, pDb, pVgroup, dnodeId, &contLen); - if (pReq == NULL) return -1; + if (pReq == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } action.pCont = pReq; action.contLen = contLen; @@ -1570,17 +1632,22 @@ int32_t mndAddCheckLearnerCatchupAction(SMnode *pMnode, STrans *pTrans, SDbObj * action.acceptableCode = TSDB_CODE_VND_ALREADY_IS_VOTER; action.retryCode = TSDB_CODE_VND_NOT_CATCH_UP; - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pReq); - return -1; + TAOS_RETURN(code); } - return 0; + TAOS_RETURN(code); } int32_t mndAddAlterVnodeTypeAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, int32_t dnodeId) { + int32_t code = 0; SDnodeObj *pDnode = mndAcquireDnode(pMnode, dnodeId); - if (pDnode == NULL) return -1; + if (pDnode == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); @@ -1588,7 +1655,11 @@ int32_t mndAddAlterVnodeTypeAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, int32_t contLen = 0; void *pReq = mndBuildAlterVnodeReplicaReq(pMnode, pDb, pVgroup, dnodeId, &contLen); - if (pReq == NULL) return -1; + if (pReq == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } action.pCont = pReq; action.contLen = contLen; @@ -1596,22 +1667,27 @@ int32_t mndAddAlterVnodeTypeAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, action.acceptableCode = TSDB_CODE_VND_ALREADY_IS_VOTER; action.retryCode = TSDB_CODE_VND_NOT_CATCH_UP; - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pReq); - return -1; + TAOS_RETURN(code); } - return 0; + TAOS_RETURN(code); } int32_t mndRestoreAddAlterVnodeTypeAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, SDnodeObj *pDnode) { + int32_t code = 0; STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); int32_t contLen = 0; void *pReq = mndBuildAlterVnodeReplicaReq(pMnode, pDb, pVgroup, pDnode->id, &contLen); - if (pReq == NULL) return -1; + if (pReq == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } action.pCont = pReq; action.contLen = contLen; @@ -1619,18 +1695,23 @@ int32_t mndRestoreAddAlterVnodeTypeAction(SMnode *pMnode, STrans *pTrans, SDbObj action.acceptableCode = TSDB_CODE_VND_ALREADY_IS_VOTER; action.retryCode = TSDB_CODE_VND_NOT_CATCH_UP; - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pReq); - return -1; + TAOS_RETURN(code); } - return 0; + TAOS_RETURN(code); } static int32_t mndAddDisableVnodeWriteAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, int32_t dnodeId) { + int32_t code = 0; SDnodeObj *pDnode = mndAcquireDnode(pMnode, dnodeId); - if (pDnode == NULL) return -1; + if (pDnode == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); @@ -1638,32 +1719,45 @@ static int32_t mndAddDisableVnodeWriteAction(SMnode *pMnode, STrans *pTrans, SDb int32_t contLen = 0; void *pReq = mndBuildDisableVnodeWriteReq(pMnode, pDb, pVgroup->vgId, &contLen); - if (pReq == NULL) return -1; + if (pReq == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } action.pCont = pReq; action.contLen = contLen; action.msgType = TDMT_VND_DISABLE_WRITE; - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pReq); - return -1; + TAOS_RETURN(code); } - return 0; + TAOS_RETURN(code); } int32_t mndAddDropVnodeAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, SVnodeGid *pVgid, bool isRedo) { + int32_t code = 0; STransAction action = {0}; SDnodeObj *pDnode = mndAcquireDnode(pMnode, pVgid->dnodeId); - if (pDnode == NULL) return -1; + if (pDnode == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } action.epSet = mndGetDnodeEpset(pDnode); mndReleaseDnode(pMnode, pDnode); int32_t contLen = 0; void *pReq = mndBuildDropVnodeReq(pMnode, pDnode, pDb, pVgroup, &contLen); - if (pReq == NULL) return -1; + if (pReq == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } action.pCont = pReq; action.contLen = contLen; @@ -1671,23 +1765,24 @@ int32_t mndAddDropVnodeAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgOb action.acceptableCode = TSDB_CODE_VND_NOT_EXIST; if (isRedo) { - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pReq); - return -1; + TAOS_RETURN(code); } } else { - if (mndTransAppendUndoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendUndoAction(pTrans, &action)) != 0) { taosMemoryFree(pReq); - return -1; + TAOS_RETURN(code); } } - return 0; + TAOS_RETURN(code); } int32_t mndSetMoveVgroupInfoToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, int32_t vnIndex, SArray *pArray, bool force, bool unsafe) { - SVgObj newVg = {0}; + int32_t code = 0; + SVgObj newVg = {0}; memcpy(&newVg, pVgroup, sizeof(SVgObj)); mInfo("vgId:%d, vgroup info before move, replica:%d", newVg.vgId, newVg.replica); @@ -1702,12 +1797,12 @@ int32_t mndSetMoveVgroupInfoToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, if (newVg.replica == 1) { #endif mInfo("vgId:%d, will add 1 vnode, replca:%d", pVgroup->vgId, newVg.replica); - if (mndAddVnodeToVgroup(pMnode, pTrans, &newVg, pArray) != 0) return -1; + TAOS_CHECK_RETURN(mndAddVnodeToVgroup(pMnode, pTrans, &newVg, pArray)); for (int32_t i = 0; i < newVg.replica - 1; ++i) { - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg, newVg.vnodeGid[i].dnodeId) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg, newVg.vnodeGid[i].dnodeId)); } - if (mndAddCreateVnodeAction(pMnode, pTrans, pDb, &newVg, &newVg.vnodeGid[newVg.replica - 1]) != 0) return -1; - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg) != 0) return -1; + TAOS_CHECK_RETURN(mndAddCreateVnodeAction(pMnode, pTrans, pDb, &newVg, &newVg.vnodeGid[newVg.replica - 1])); + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg)); mInfo("vgId:%d, will remove 1 vnode, replca:2", pVgroup->vgId); newVg.replica--; @@ -1716,19 +1811,23 @@ int32_t mndSetMoveVgroupInfoToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, memset(&newVg.vnodeGid[newVg.replica], 0, sizeof(SVnodeGid)); { SSdbRaw *pRaw = mndVgroupActionEncode(&newVg); - if (pRaw == NULL) return -1; - if (mndTransAppendRedolog(pTrans, pRaw) != 0) { + if (pRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } + if ((code = mndTransAppendRedolog(pTrans, pRaw)) != 0) { sdbFreeRaw(pRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY); } - if (mndAddDropVnodeAction(pMnode, pTrans, pDb, &newVg, &del, true) != 0) return -1; + TAOS_CHECK_RETURN(mndAddDropVnodeAction(pMnode, pTrans, pDb, &newVg, &del, true)); for (int32_t i = 0; i < newVg.replica; ++i) { - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg, newVg.vnodeGid[i].dnodeId) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg, newVg.vnodeGid[i].dnodeId)); } - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg)); #if 1 } #else @@ -1761,33 +1860,36 @@ int32_t mndSetMoveVgroupInfoToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, #endif } else { mInfo("vgId:%d, will add 1 vnode and force remove 1 vnode", pVgroup->vgId); - if (mndAddVnodeToVgroup(pMnode, pTrans, &newVg, pArray) != 0) return -1; + TAOS_CHECK_RETURN(mndAddVnodeToVgroup(pMnode, pTrans, &newVg, pArray)); newVg.replica--; // SVnodeGid del = newVg.vnodeGid[vnIndex]; newVg.vnodeGid[vnIndex] = newVg.vnodeGid[newVg.replica]; memset(&newVg.vnodeGid[newVg.replica], 0, sizeof(SVnodeGid)); { SSdbRaw *pRaw = mndVgroupActionEncode(&newVg); - if (pRaw == NULL) return -1; - if (mndTransAppendRedolog(pTrans, pRaw) != 0) { + if (pRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } + if ((code = mndTransAppendRedolog(pTrans, pRaw)) != 0) { sdbFreeRaw(pRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY); } for (int32_t i = 0; i < newVg.replica; ++i) { if (i != vnIndex) { - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg, newVg.vnodeGid[i].dnodeId) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg, newVg.vnodeGid[i].dnodeId)); } } - if (mndAddCreateVnodeAction(pMnode, pTrans, pDb, &newVg, &newVg.vnodeGid[vnIndex]) != 0) return -1; - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg) != 0) return -1; + TAOS_CHECK_RETURN(mndAddCreateVnodeAction(pMnode, pTrans, pDb, &newVg, &newVg.vnodeGid[vnIndex])); + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg)); if (newVg.replica == 1) { if (force && !unsafe) { - terrno = TSDB_CODE_VND_META_DATA_UNSAFE_DELETE; - return -1; + TAOS_RETURN(TSDB_CODE_VND_META_DATA_UNSAFE_DELETE); } SSdb *pSdb = pMnode->pSdb; @@ -1799,10 +1901,10 @@ int32_t mndSetMoveVgroupInfoToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, if (pIter == NULL) break; if (strcmp(pStb->db, pDb->name) == 0) { - if (mndSetForceDropCreateStbRedoActions(pMnode, pTrans, &newVg, pStb) != 0) { + if ((code = mndSetForceDropCreateStbRedoActions(pMnode, pTrans, &newVg, pStb)) != 0) { sdbCancelFetch(pSdb, pIter); sdbRelease(pSdb, pStb); - return -1; + TAOS_RETURN(code); } } @@ -1815,10 +1917,14 @@ int32_t mndSetMoveVgroupInfoToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, { SSdbRaw *pRaw = mndVgroupActionEncode(&newVg); - if (pRaw == NULL) return -1; - if (mndTransAppendCommitlog(pTrans, pRaw) != 0) { + if (pRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } + if ((code = mndTransAppendCommitlog(pTrans, pRaw)) != 0) { sdbFreeRaw(pRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY); } @@ -1827,13 +1933,17 @@ int32_t mndSetMoveVgroupInfoToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, for (int32_t i = 0; i < newVg.replica; ++i) { mInfo("vgId:%d, vnode:%d dnode:%d", newVg.vgId, i, newVg.vnodeGid[i].dnodeId); } - return 0; + TAOS_RETURN(code); } int32_t mndSetMoveVgroupsInfoToTrans(SMnode *pMnode, STrans *pTrans, int32_t delDnodeId, bool force, bool unsafe) { int32_t code = 0; SArray *pArray = mndBuildDnodesArray(pMnode, delDnodeId); - if (pArray == NULL) return -1; + if (pArray == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } void *pIter = NULL; while (1) { @@ -1866,11 +1976,12 @@ int32_t mndSetMoveVgroupsInfoToTrans(SMnode *pMnode, STrans *pTrans, int32_t del } taosArrayDestroy(pArray); - return code; + TAOS_RETURN(code); } static int32_t mndAddIncVgroupReplicaToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, int32_t newDnodeId) { + int32_t code = 0; mInfo("vgId:%d, will add 1 vnode, replica:%d dnode:%d", pVgroup->vgId, pVgroup->replica, newDnodeId); // assoc dnode @@ -1881,34 +1992,39 @@ static int32_t mndAddIncVgroupReplicaToTrans(SMnode *pMnode, STrans *pTrans, SDb pGid->nodeRole = TAOS_SYNC_ROLE_LEARNER; SSdbRaw *pVgRaw = mndVgroupActionEncode(pVgroup); - if (pVgRaw == NULL) return -1; - if (mndTransAppendRedolog(pTrans, pVgRaw) != 0) { + if (pVgRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } + if ((code = mndTransAppendRedolog(pTrans, pVgRaw)) != 0) { sdbFreeRaw(pVgRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pVgRaw, SDB_STATUS_READY); // learner for (int32_t i = 0; i < pVgroup->replica - 1; ++i) { - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, pVgroup, pVgroup->vnodeGid[i].dnodeId) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, pVgroup, pVgroup->vnodeGid[i].dnodeId)); } - if (mndAddCreateVnodeAction(pMnode, pTrans, pDb, pVgroup, pGid) != 0) return -1; + TAOS_CHECK_RETURN(mndAddCreateVnodeAction(pMnode, pTrans, pDb, pVgroup, pGid)); // voter pGid->nodeRole = TAOS_SYNC_ROLE_VOTER; - if (mndAddAlterVnodeTypeAction(pMnode, pTrans, pDb, pVgroup, pGid->dnodeId) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeTypeAction(pMnode, pTrans, pDb, pVgroup, pGid->dnodeId)); for (int32_t i = 0; i < pVgroup->replica - 1; ++i) { - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, pVgroup, pVgroup->vnodeGid[i].dnodeId) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, pVgroup, pVgroup->vnodeGid[i].dnodeId)); } // confirm - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, pVgroup) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, pVgroup)); - return 0; + TAOS_RETURN(code); } static int32_t mndAddDecVgroupReplicaFromTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, int32_t delDnodeId) { + int32_t code = 0; mInfo("vgId:%d, will remove 1 vnode, replica:%d dnode:%d", pVgroup->vgId, pVgroup->replica, delDnodeId); SVnodeGid *pGid = NULL; @@ -1928,20 +2044,24 @@ static int32_t mndAddDecVgroupReplicaFromTrans(SMnode *pMnode, STrans *pTrans, S memset(&pVgroup->vnodeGid[pVgroup->replica], 0, sizeof(SVnodeGid)); SSdbRaw *pVgRaw = mndVgroupActionEncode(pVgroup); - if (pVgRaw == NULL) return -1; - if (mndTransAppendRedolog(pTrans, pVgRaw) != 0) { + if (pVgRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } + if ((code = mndTransAppendRedolog(pTrans, pVgRaw)) != 0) { sdbFreeRaw(pVgRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pVgRaw, SDB_STATUS_READY); - if (mndAddDropVnodeAction(pMnode, pTrans, pDb, pVgroup, &delGid, true) != 0) return -1; + TAOS_CHECK_RETURN(mndAddDropVnodeAction(pMnode, pTrans, pDb, pVgroup, &delGid, true)); for (int32_t i = 0; i < pVgroup->replica; ++i) { - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, pVgroup, pVgroup->vnodeGid[i].dnodeId) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, pVgroup, pVgroup->vnodeGid[i].dnodeId)); } - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, pVgroup) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, pVgroup)); - return 0; + TAOS_RETURN(code); } static int32_t mndRedistributeVgroup(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SVgObj *pVgroup, SDnodeObj *pNew1, @@ -1951,10 +2071,14 @@ static int32_t mndRedistributeVgroup(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, STrans *pTrans = NULL; pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, pReq, "red-vgroup"); - if (pTrans == NULL) goto _OVER; + if (pTrans == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } mndTransSetDbName(pTrans, pVgroup->dbName, NULL); - if (mndTransCheckConflictWithCompact(pMnode, pTrans) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndTransCheckConflictWithCompact(pMnode, pTrans), NULL, _OVER); mndTransSetSerial(pTrans); mInfo("trans:%d, used to redistribute vgroup, vgId:%d", pTrans->id, pVgroup->vgId); @@ -1972,7 +2096,7 @@ static int32_t mndRedistributeVgroup(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, if (numOfVnodes >= pNew1->numOfSupportVnodes) { mError("vgId:%d, no enough vnodes in dnode:%d, numOfVnodes:%d support:%d", newVg.vgId, pNew1->id, numOfVnodes, pNew1->numOfSupportVnodes); - terrno = TSDB_CODE_MND_NO_ENOUGH_VNODES; + code = TSDB_CODE_MND_NO_ENOUGH_VNODES; goto _OVER; } @@ -1980,14 +2104,14 @@ static int32_t mndRedistributeVgroup(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, if (pNew1->memAvail - vgMem - pNew1->memUsed <= 0) { mError("db:%s, vgId:%d, no enough memory:%" PRId64 " in dnode:%d avail:%" PRId64 " used:%" PRId64, pVgroup->dbName, pVgroup->vgId, vgMem, pNew1->id, pNew1->memAvail, pNew1->memUsed); - terrno = TSDB_CODE_MND_NO_ENOUGH_MEM_IN_DNODE; + code = TSDB_CODE_MND_NO_ENOUGH_MEM_IN_DNODE; goto _OVER; } else { pNew1->memUsed += vgMem; } - if (mndAddIncVgroupReplicaToTrans(pMnode, pTrans, pDb, &newVg, pNew1->id) != 0) goto _OVER; - if (mndAddDecVgroupReplicaFromTrans(pMnode, pTrans, pDb, &newVg, pOld1->id) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddIncVgroupReplicaToTrans(pMnode, pTrans, pDb, &newVg, pNew1->id), NULL, _OVER); + TAOS_CHECK_GOTO(mndAddDecVgroupReplicaFromTrans(pMnode, pTrans, pDb, &newVg, pOld1->id), NULL, _OVER); } if (pNew2 != NULL && pOld2 != NULL) { @@ -1995,20 +2119,20 @@ static int32_t mndRedistributeVgroup(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, if (numOfVnodes >= pNew2->numOfSupportVnodes) { mError("vgId:%d, no enough vnodes in dnode:%d, numOfVnodes:%d support:%d", newVg.vgId, pNew2->id, numOfVnodes, pNew2->numOfSupportVnodes); - terrno = TSDB_CODE_MND_NO_ENOUGH_VNODES; + code = TSDB_CODE_MND_NO_ENOUGH_VNODES; goto _OVER; } int64_t vgMem = mndGetVgroupMemory(pMnode, NULL, pVgroup); if (pNew2->memAvail - vgMem - pNew2->memUsed <= 0) { mError("db:%s, vgId:%d, no enough memory:%" PRId64 " in dnode:%d avail:%" PRId64 " used:%" PRId64, pVgroup->dbName, pVgroup->vgId, vgMem, pNew2->id, pNew2->memAvail, pNew2->memUsed); - terrno = TSDB_CODE_MND_NO_ENOUGH_MEM_IN_DNODE; + code = TSDB_CODE_MND_NO_ENOUGH_MEM_IN_DNODE; goto _OVER; } else { pNew2->memUsed += vgMem; } - if (mndAddIncVgroupReplicaToTrans(pMnode, pTrans, pDb, &newVg, pNew2->id) != 0) goto _OVER; - if (mndAddDecVgroupReplicaFromTrans(pMnode, pTrans, pDb, &newVg, pOld2->id) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddIncVgroupReplicaToTrans(pMnode, pTrans, pDb, &newVg, pNew2->id), NULL, _OVER); + TAOS_CHECK_GOTO(mndAddDecVgroupReplicaFromTrans(pMnode, pTrans, pDb, &newVg, pOld2->id), NULL, _OVER); } if (pNew3 != NULL && pOld3 != NULL) { @@ -2016,26 +2140,30 @@ static int32_t mndRedistributeVgroup(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, if (numOfVnodes >= pNew3->numOfSupportVnodes) { mError("vgId:%d, no enough vnodes in dnode:%d, numOfVnodes:%d support:%d", newVg.vgId, pNew3->id, numOfVnodes, pNew3->numOfSupportVnodes); - terrno = TSDB_CODE_MND_NO_ENOUGH_VNODES; + code = TSDB_CODE_MND_NO_ENOUGH_VNODES; goto _OVER; } int64_t vgMem = mndGetVgroupMemory(pMnode, NULL, pVgroup); if (pNew3->memAvail - vgMem - pNew3->memUsed <= 0) { mError("db:%s, vgId:%d, no enough memory:%" PRId64 " in dnode:%d avail:%" PRId64 " used:%" PRId64, pVgroup->dbName, pVgroup->vgId, vgMem, pNew3->id, pNew3->memAvail, pNew3->memUsed); - terrno = TSDB_CODE_MND_NO_ENOUGH_MEM_IN_DNODE; + code = TSDB_CODE_MND_NO_ENOUGH_MEM_IN_DNODE; goto _OVER; } else { pNew3->memUsed += vgMem; } - if (mndAddIncVgroupReplicaToTrans(pMnode, pTrans, pDb, &newVg, pNew3->id) != 0) goto _OVER; - if (mndAddDecVgroupReplicaFromTrans(pMnode, pTrans, pDb, &newVg, pOld3->id) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddIncVgroupReplicaToTrans(pMnode, pTrans, pDb, &newVg, pNew3->id), NULL, _OVER); + TAOS_CHECK_GOTO(mndAddDecVgroupReplicaFromTrans(pMnode, pTrans, pDb, &newVg, pOld3->id), NULL, _OVER); } { SSdbRaw *pRaw = mndVgroupActionEncode(&newVg); - if (pRaw == NULL) goto _OVER; - if (mndTransAppendCommitlog(pTrans, pRaw) != 0) { + if (pRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } + if ((code = mndTransAppendCommitlog(pTrans, pRaw)) != 0) { sdbFreeRaw(pRaw); goto _OVER; } @@ -2047,13 +2175,13 @@ static int32_t mndRedistributeVgroup(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, mInfo("vgId:%d, vnode:%d dnode:%d", newVg.vgId, i, newVg.vnodeGid[i].dnodeId); } - if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndTransPrepare(pMnode, pTrans), NULL, _OVER); code = 0; _OVER: mndTransDrop(pTrans); mndReleaseDb(pMnode, pDb); - return code; + TAOS_RETURN(code); } static int32_t mndProcessRedistributeVgroupMsg(SRpcMsg *pReq) { @@ -2075,24 +2203,32 @@ static int32_t mndProcessRedistributeVgroupMsg(SRpcMsg *pReq) { SRedistributeVgroupReq req = {0}; if (tDeserializeSRedistributeVgroupReq(pReq->pCont, pReq->contLen, &req) != 0) { - terrno = TSDB_CODE_INVALID_MSG; + code = TSDB_CODE_INVALID_MSG; goto _OVER; } mInfo("vgId:%d, start to redistribute vgroup to dnode %d:%d:%d", req.vgId, req.dnodeId1, req.dnodeId2, req.dnodeId3); - if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_REDISTRIBUTE_VGROUP) != 0) { + if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_REDISTRIBUTE_VGROUP)) != 0) { goto _OVER; } pVgroup = mndAcquireVgroup(pMnode, req.vgId); - if (pVgroup == NULL) goto _OVER; + if (pVgroup == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } pDb = mndAcquireDb(pMnode, pVgroup->dbName); - if (pDb == NULL) goto _OVER; + if (pDb == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } if (pVgroup->replica == 1) { if (req.dnodeId1 <= 0 || req.dnodeId2 > 0 || req.dnodeId3 > 0) { - terrno = TSDB_CODE_MND_INVALID_REPLICA; + code = TSDB_CODE_MND_INVALID_REPLICA; goto _OVER; } @@ -2103,16 +2239,24 @@ static int32_t mndProcessRedistributeVgroupMsg(SRpcMsg *pReq) { } pNew1 = mndAcquireDnode(pMnode, req.dnodeId1); - if (pNew1 == NULL) goto _OVER; + if (pNew1 == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } if (!mndIsDnodeOnline(pNew1, curMs)) { - terrno = TSDB_CODE_MND_HAS_OFFLINE_DNODE; + code = TSDB_CODE_MND_HAS_OFFLINE_DNODE; goto _OVER; } pOld1 = mndAcquireDnode(pMnode, pVgroup->vnodeGid[0].dnodeId); - if (pOld1 == NULL) goto _OVER; + if (pOld1 == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } if (!mndIsDnodeOnline(pOld1, curMs)) { - terrno = TSDB_CODE_MND_HAS_OFFLINE_DNODE; + code = TSDB_CODE_MND_HAS_OFFLINE_DNODE; goto _OVER; } @@ -2120,12 +2264,12 @@ static int32_t mndProcessRedistributeVgroupMsg(SRpcMsg *pReq) { } else if (pVgroup->replica == 3) { if (req.dnodeId1 <= 0 || req.dnodeId2 <= 0 || req.dnodeId3 <= 0) { - terrno = TSDB_CODE_MND_INVALID_REPLICA; + code = TSDB_CODE_MND_INVALID_REPLICA; goto _OVER; } if (req.dnodeId1 == req.dnodeId2 || req.dnodeId1 == req.dnodeId3 || req.dnodeId2 == req.dnodeId3) { - terrno = TSDB_CODE_MND_INVALID_REPLICA; + code = TSDB_CODE_MND_INVALID_REPLICA; goto _OVER; } @@ -2167,54 +2311,78 @@ static int32_t mndProcessRedistributeVgroupMsg(SRpcMsg *pReq) { if (newDnodeId[0] != 0) { pNew1 = mndAcquireDnode(pMnode, newDnodeId[0]); - if (pNew1 == NULL) goto _OVER; + if (pNew1 == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } if (!mndIsDnodeOnline(pNew1, curMs)) { - terrno = TSDB_CODE_MND_HAS_OFFLINE_DNODE; + code = TSDB_CODE_MND_HAS_OFFLINE_DNODE; goto _OVER; } } if (newDnodeId[1] != 0) { pNew2 = mndAcquireDnode(pMnode, newDnodeId[1]); - if (pNew2 == NULL) goto _OVER; + if (pNew2 == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } if (!mndIsDnodeOnline(pNew2, curMs)) { - terrno = TSDB_CODE_MND_HAS_OFFLINE_DNODE; + code = TSDB_CODE_MND_HAS_OFFLINE_DNODE; goto _OVER; } } if (newDnodeId[2] != 0) { pNew3 = mndAcquireDnode(pMnode, newDnodeId[2]); - if (pNew3 == NULL) goto _OVER; + if (pNew3 == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } if (!mndIsDnodeOnline(pNew3, curMs)) { - terrno = TSDB_CODE_MND_HAS_OFFLINE_DNODE; + code = TSDB_CODE_MND_HAS_OFFLINE_DNODE; goto _OVER; } } if (oldDnodeId[0] != 0) { pOld1 = mndAcquireDnode(pMnode, oldDnodeId[0]); - if (pOld1 == NULL) goto _OVER; + if (pOld1 == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } if (!mndIsDnodeOnline(pOld1, curMs)) { - terrno = TSDB_CODE_MND_HAS_OFFLINE_DNODE; + code = TSDB_CODE_MND_HAS_OFFLINE_DNODE; goto _OVER; } } if (oldDnodeId[1] != 0) { pOld2 = mndAcquireDnode(pMnode, oldDnodeId[1]); - if (pOld2 == NULL) goto _OVER; + if (pOld2 == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } if (!mndIsDnodeOnline(pOld2, curMs)) { - terrno = TSDB_CODE_MND_HAS_OFFLINE_DNODE; + code = TSDB_CODE_MND_HAS_OFFLINE_DNODE; goto _OVER; } } if (oldDnodeId[2] != 0) { pOld3 = mndAcquireDnode(pMnode, oldDnodeId[2]); - if (pOld3 == NULL) goto _OVER; + if (pOld3 == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } if (!mndIsDnodeOnline(pOld3, curMs)) { - terrno = TSDB_CODE_MND_HAS_OFFLINE_DNODE; + code = TSDB_CODE_MND_HAS_OFFLINE_DNODE; goto _OVER; } } @@ -2228,7 +2396,7 @@ static int32_t mndProcessRedistributeVgroupMsg(SRpcMsg *pReq) { code = mndRedistributeVgroup(pMnode, pReq, pDb, pVgroup, pNew1, pOld1, pNew2, pOld2, pNew3, pOld3); } else { - terrno = TSDB_CODE_MND_REQ_REJECTED; + code = TSDB_CODE_MND_REQ_REJECTED; goto _OVER; } @@ -2242,7 +2410,7 @@ static int32_t mndProcessRedistributeVgroupMsg(SRpcMsg *pReq) { _OVER: if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) { mError("vgId:%d, failed to redistribute to dnode %d:%d:%d since %s", req.vgId, req.dnodeId1, req.dnodeId2, - req.dnodeId3, terrstr()); + req.dnodeId3, tstrerror(code)); } mndReleaseDnode(pMnode, pNew1); @@ -2255,7 +2423,7 @@ _OVER: mndReleaseDb(pMnode, pDb); tFreeSRedistributeVgroupReq(&req); - return code; + TAOS_RETURN(code); } static void *mndBuildSForceBecomeFollowerReq(SMnode *pMnode, SVgObj *pVgroup, int32_t dnodeId, int32_t *pContLen) { @@ -2286,8 +2454,13 @@ static void *mndBuildSForceBecomeFollowerReq(SMnode *pMnode, SVgObj *pVgroup, in } int32_t mndAddBalanceVgroupLeaderAction(SMnode *pMnode, STrans *pTrans, SVgObj *pVgroup, int32_t dnodeId) { + int32_t code = 0; SDnodeObj *pDnode = mndAcquireDnode(pMnode, dnodeId); - if (pDnode == NULL) return -1; + if (pDnode == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } STransAction action = {0}; action.epSet = mndGetDnodeEpset(pDnode); @@ -2295,22 +2468,27 @@ int32_t mndAddBalanceVgroupLeaderAction(SMnode *pMnode, STrans *pTrans, SVgObj * int32_t contLen = 0; void *pReq = mndBuildSForceBecomeFollowerReq(pMnode, pVgroup, dnodeId, &contLen); - if (pReq == NULL) return -1; + if (pReq == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } action.pCont = pReq; action.contLen = contLen; action.msgType = TDMT_SYNC_FORCE_FOLLOWER; - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pReq); - return -1; + TAOS_RETURN(code); } - return 0; + TAOS_RETURN(code); } int32_t mndAddVgroupBalanceToTrans(SMnode *pMnode, SVgObj *pVgroup, STrans *pTrans) { - SSdb *pSdb = pMnode->pSdb; + int32_t code = 0; + SSdb *pSdb = pMnode->pSdb; int32_t vgid = pVgroup->vgId; int8_t replica = pVgroup->replica; @@ -2342,15 +2520,17 @@ int32_t mndAddVgroupBalanceToTrans(SMnode *pMnode, SVgObj *pVgroup, STrans *pTra if (exist && online) { mInfo("trans:%d, vgid:%d leader to dnode:%d", pTrans->id, vgid, dnodeId); - if (mndAddBalanceVgroupLeaderAction(pMnode, pTrans, pVgroup, dnodeId) != 0) { + if ((code = mndAddBalanceVgroupLeaderAction(pMnode, pTrans, pVgroup, dnodeId)) != 0) { mError("trans:%d, vgid:%d failed to be balanced to dnode:%d", pTrans->id, vgid, dnodeId); - return -1; + TAOS_RETURN(code); } SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName); if (pDb == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; mError("trans:%d, vgid:%d failed to be balanced to dnode:%d, because db not exist", pTrans->id, vgid, dnodeId); - return -1; + TAOS_RETURN(code); } mndReleaseDb(pMnode, pDb); @@ -2359,7 +2539,7 @@ int32_t mndAddVgroupBalanceToTrans(SMnode *pMnode, SVgObj *pVgroup, STrans *pTra online); } - return 0; + TAOS_RETURN(code); } extern int32_t mndProcessVgroupBalanceLeaderMsgImp(SRpcMsg *pReq); @@ -2392,8 +2572,7 @@ static int32_t mndCheckDnodeMemory(SMnode *pMnode, SDbObj *pOldDb, SDbObj *pNewD if (pDnode->memAvail - pDnode->memUsed <= 0) { mError("db:%s, vgId:%d, no enough memory in dnode:%d, avail:%" PRId64 " used:%" PRId64, pNewVgroup->dbName, pNewVgroup->vgId, pDnode->id, pDnode->memAvail, pDnode->memUsed); - terrno = TSDB_CODE_MND_NO_ENOUGH_MEM_IN_DNODE; - return -1; + TAOS_RETURN(TSDB_CODE_MND_NO_ENOUGH_MEM_IN_DNODE); } else if (inVgroup) { mInfo("db:%s, vgId:%d, memory in dnode:%d, avail:%" PRId64 " used:%" PRId64, pNewVgroup->dbName, pNewVgroup->vgId, pDnode->id, pDnode->memAvail, pDnode->memUsed); @@ -2405,11 +2584,12 @@ static int32_t mndCheckDnodeMemory(SMnode *pMnode, SDbObj *pOldDb, SDbObj *pNewD int32_t mndBuildAlterVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj *pOldDb, SDbObj *pNewDb, SVgObj *pVgroup, SArray *pArray, SVgObj *pNewVgroup) { + int32_t code = 0; memcpy(pNewVgroup, pVgroup, sizeof(SVgObj)); if (pVgroup->replica <= 0 || pVgroup->replica == pNewDb->cfg.replications) { - if (mndAddAlterVnodeConfigAction(pMnode, pTrans, pNewDb, pVgroup) != 0) return -1; - if (mndCheckDnodeMemory(pMnode, pOldDb, pNewDb, pNewVgroup, pVgroup, pArray) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeConfigAction(pMnode, pTrans, pNewDb, pVgroup)); + TAOS_CHECK_RETURN(mndCheckDnodeMemory(pMnode, pOldDb, pNewDb, pNewVgroup, pVgroup, pArray)); return 0; } @@ -2420,78 +2600,78 @@ int32_t mndBuildAlterVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj *pOldDb pVgroup->vnodeGid[0].dnodeId); // add second - if (mndAddVnodeToVgroup(pMnode, pTrans, pNewVgroup, pArray) != 0) return -1; + TAOS_CHECK_RETURN(mndAddVnodeToVgroup(pMnode, pTrans, pNewVgroup, pArray)); // learner stage pNewVgroup->vnodeGid[0].nodeRole = TAOS_SYNC_ROLE_VOTER; pNewVgroup->vnodeGid[1].nodeRole = TAOS_SYNC_ROLE_LEARNER; - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId) != 0) - return -1; + TAOS_CHECK_RETURN( + mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId)); - if (mndAddCreateVnodeAction(pMnode, pTrans, pNewDb, pNewVgroup, &pNewVgroup->vnodeGid[1]) != 0) return -1; + TAOS_CHECK_RETURN(mndAddCreateVnodeAction(pMnode, pTrans, pNewDb, pNewVgroup, &pNewVgroup->vnodeGid[1])); // follower stage pNewVgroup->vnodeGid[1].nodeRole = TAOS_SYNC_ROLE_VOTER; - if (mndAddAlterVnodeTypeAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[1].dnodeId) != 0) return -1; - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId) != 0) - return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeTypeAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[1].dnodeId)); + TAOS_CHECK_RETURN( + mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId)); - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, pNewVgroup) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, pNewVgroup)); // add third - if (mndAddVnodeToVgroup(pMnode, pTrans, pNewVgroup, pArray) != 0) return -1; + TAOS_CHECK_RETURN(mndAddVnodeToVgroup(pMnode, pTrans, pNewVgroup, pArray)); pNewVgroup->vnodeGid[0].nodeRole = TAOS_SYNC_ROLE_VOTER; pNewVgroup->vnodeGid[1].nodeRole = TAOS_SYNC_ROLE_VOTER; pNewVgroup->vnodeGid[2].nodeRole = TAOS_SYNC_ROLE_VOTER; - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId) != 0) - return -1; - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[1].dnodeId) != 0) - return -1; - if (mndAddCreateVnodeAction(pMnode, pTrans, pNewDb, pNewVgroup, &pNewVgroup->vnodeGid[2]) != 0) return -1; + TAOS_CHECK_RETURN( + mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId)); + TAOS_CHECK_RETURN( + mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[1].dnodeId)); + TAOS_CHECK_RETURN(mndAddCreateVnodeAction(pMnode, pTrans, pNewDb, pNewVgroup, &pNewVgroup->vnodeGid[2])); - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, pNewVgroup) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, pNewVgroup)); } else if (pNewVgroup->replica == 3 && pNewDb->cfg.replications == 1) { mInfo("db:%s, vgId:%d, will remove 2 vnodes, vn:0 dnode:%d vn:1 dnode:%d vn:2 dnode:%d", pVgroup->dbName, pVgroup->vgId, pVgroup->vnodeGid[0].dnodeId, pVgroup->vnodeGid[1].dnodeId, pVgroup->vnodeGid[2].dnodeId); SVnodeGid del1 = {0}; SVnodeGid del2 = {0}; - if (mndRemoveVnodeFromVgroup(pMnode, pTrans, pNewVgroup, pArray, &del1) != 0) return -1; - if (mndAddDropVnodeAction(pMnode, pTrans, pNewDb, pNewVgroup, &del1, true) != 0) return -1; - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId) != 0) - return -1; - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[1].dnodeId) != 0) - return -1; - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, pNewVgroup) != 0) return -1; + TAOS_CHECK_RETURN(mndRemoveVnodeFromVgroup(pMnode, pTrans, pNewVgroup, pArray, &del1)); + TAOS_CHECK_RETURN(mndAddDropVnodeAction(pMnode, pTrans, pNewDb, pNewVgroup, &del1, true)); + TAOS_CHECK_RETURN( + mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId)); + TAOS_CHECK_RETURN( + mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[1].dnodeId)); + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, pNewVgroup)); - if (mndRemoveVnodeFromVgroup(pMnode, pTrans, pNewVgroup, pArray, &del2) != 0) return -1; - if (mndAddDropVnodeAction(pMnode, pTrans, pNewDb, pNewVgroup, &del2, true) != 0) return -1; - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId) != 0) - return -1; - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, pNewVgroup) != 0) return -1; + TAOS_CHECK_RETURN(mndRemoveVnodeFromVgroup(pMnode, pTrans, pNewVgroup, pArray, &del2)); + TAOS_CHECK_RETURN(mndAddDropVnodeAction(pMnode, pTrans, pNewDb, pNewVgroup, &del2, true)); + TAOS_CHECK_RETURN( + mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId)); + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, pNewVgroup)); } else if (pNewVgroup->replica == 1 && pNewDb->cfg.replications == 2) { mInfo("db:%s, vgId:%d, will add 1 vnode, vn:0 dnode:%d", pVgroup->dbName, pVgroup->vgId, pVgroup->vnodeGid[0].dnodeId); // add second - if (mndAddVnodeToVgroup(pMnode, pTrans, pNewVgroup, pArray) != 0) return -1; + TAOS_CHECK_RETURN(mndAddVnodeToVgroup(pMnode, pTrans, pNewVgroup, pArray)); // learner stage pNewVgroup->vnodeGid[0].nodeRole = TAOS_SYNC_ROLE_VOTER; pNewVgroup->vnodeGid[1].nodeRole = TAOS_SYNC_ROLE_LEARNER; - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId) != 0) - return -1; + TAOS_CHECK_RETURN( + mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId)); - if (mndAddCreateVnodeAction(pMnode, pTrans, pNewDb, pNewVgroup, &pNewVgroup->vnodeGid[1]) != 0) return -1; + TAOS_CHECK_RETURN(mndAddCreateVnodeAction(pMnode, pTrans, pNewDb, pNewVgroup, &pNewVgroup->vnodeGid[1])); // follower stage pNewVgroup->vnodeGid[1].nodeRole = TAOS_SYNC_ROLE_VOTER; - if (mndAddAlterVnodeTypeAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[1].dnodeId) != 0) return -1; - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId) != 0) - return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeTypeAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[1].dnodeId)); + TAOS_CHECK_RETURN( + mndAddAlterVnodeReplicaAction(pMnode, pTrans, pNewDb, pNewVgroup, pNewVgroup->vnodeGid[0].dnodeId)); - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, pNewVgroup) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, pNewVgroup)); } else { return -1; } @@ -2500,25 +2680,30 @@ int32_t mndBuildAlterVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj *pOldDb { SSdbRaw *pVgRaw = mndVgroupActionEncode(pNewVgroup); - if (pVgRaw == NULL) return -1; - if (mndTransAppendCommitlog(pTrans, pVgRaw) != 0) { + if (pVgRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } + if ((code = mndTransAppendCommitlog(pTrans, pVgRaw)) != 0) { sdbFreeRaw(pVgRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pVgRaw, SDB_STATUS_READY); } - return 0; + TAOS_RETURN(code); } int32_t mndBuildRaftAlterVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj *pOldDb, SDbObj *pNewDb, SVgObj *pVgroup, SArray *pArray) { - SVgObj newVgroup = {0}; + int32_t code = 0; + SVgObj newVgroup = {0}; memcpy(&newVgroup, pVgroup, sizeof(SVgObj)); if (pVgroup->replica <= 0 || pVgroup->replica == pNewDb->cfg.replications) { - if (mndAddAlterVnodeConfigAction(pMnode, pTrans, pNewDb, pVgroup) != 0) return -1; - if (mndCheckDnodeMemory(pMnode, pOldDb, pNewDb, &newVgroup, pVgroup, pArray) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeConfigAction(pMnode, pTrans, pNewDb, pVgroup)); + TAOS_CHECK_RETURN(mndCheckDnodeMemory(pMnode, pOldDb, pNewDb, &newVgroup, pVgroup, pArray)); return 0; } @@ -2532,22 +2717,22 @@ int32_t mndBuildRaftAlterVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj *pO pVgroup->vnodeGid[0].dnodeId); // add second - if (mndAddVnodeToVgroup(pMnode, pTrans, &newVgroup, pArray) != 0) return -1; + TAOS_CHECK_RETURN(mndAddVnodeToVgroup(pMnode, pTrans, &newVgroup, pArray)); // add third - if (mndAddVnodeToVgroup(pMnode, pTrans, &newVgroup, pArray) != 0) return -1; + TAOS_CHECK_RETURN(mndAddVnodeToVgroup(pMnode, pTrans, &newVgroup, pArray)); // add learner stage newVgroup.vnodeGid[0].nodeRole = TAOS_SYNC_ROLE_VOTER; newVgroup.vnodeGid[1].nodeRole = TAOS_SYNC_ROLE_LEARNER; newVgroup.vnodeGid[2].nodeRole = TAOS_SYNC_ROLE_LEARNER; - if (mndAddChangeConfigAction(pMnode, pTrans, pNewDb, pVgroup, &newVgroup, newVgroup.vnodeGid[0].dnodeId) != 0) - return -1; + TAOS_CHECK_RETURN( + mndAddChangeConfigAction(pMnode, pTrans, pNewDb, pVgroup, &newVgroup, newVgroup.vnodeGid[0].dnodeId)); mInfo("trans:%d, vgId:%d, add change config, syncConfChangeVer:%d, version:%d, replica:%d", pTrans->id, pVgroup->vgId, newVgroup.syncConfChangeVer, pVgroup->version, pVgroup->replica); - if (mndAddCreateVnodeAction(pMnode, pTrans, pNewDb, &newVgroup, &newVgroup.vnodeGid[1]) != 0) return -1; + TAOS_CHECK_RETURN(mndAddCreateVnodeAction(pMnode, pTrans, pNewDb, &newVgroup, &newVgroup.vnodeGid[1])); mInfo("trans:%d, vgId:%d, create vnode, syncConfChangeVer:%d, version:%d, replica:%d", pTrans->id, pVgroup->vgId, newVgroup.syncConfChangeVer, pVgroup->version, pVgroup->replica); - if (mndAddCreateVnodeAction(pMnode, pTrans, pNewDb, &newVgroup, &newVgroup.vnodeGid[2]) != 0) return -1; + TAOS_CHECK_RETURN(mndAddCreateVnodeAction(pMnode, pTrans, pNewDb, &newVgroup, &newVgroup.vnodeGid[2])); mInfo("trans:%d, vgId:%d, create vnode, syncConfChangeVer:%d, version:%d, replica:%d", pTrans->id, pVgroup->vgId, newVgroup.syncConfChangeVer, pVgroup->version, pVgroup->replica); @@ -2555,33 +2740,37 @@ int32_t mndBuildRaftAlterVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj *pO newVgroup.vnodeGid[0].nodeRole = TAOS_SYNC_ROLE_VOTER; newVgroup.vnodeGid[1].nodeRole = TAOS_SYNC_ROLE_VOTER; newVgroup.vnodeGid[2].nodeRole = TAOS_SYNC_ROLE_VOTER; - if (mndAddCheckLearnerCatchupAction(pMnode, pTrans, pNewDb, &newVgroup, newVgroup.vnodeGid[1].dnodeId) != 0) - return -1; - if (mndAddCheckLearnerCatchupAction(pMnode, pTrans, pNewDb, &newVgroup, newVgroup.vnodeGid[2].dnodeId) != 0) - return -1; + TAOS_CHECK_RETURN( + mndAddCheckLearnerCatchupAction(pMnode, pTrans, pNewDb, &newVgroup, newVgroup.vnodeGid[1].dnodeId)); + TAOS_CHECK_RETURN( + mndAddCheckLearnerCatchupAction(pMnode, pTrans, pNewDb, &newVgroup, newVgroup.vnodeGid[2].dnodeId)); // change raft type newVgroup.vnodeGid[0].nodeRole = TAOS_SYNC_ROLE_VOTER; newVgroup.vnodeGid[1].nodeRole = TAOS_SYNC_ROLE_VOTER; newVgroup.vnodeGid[2].nodeRole = TAOS_SYNC_ROLE_LEARNER; - if (mndAddChangeConfigAction(pMnode, pTrans, pNewDb, pVgroup, &newVgroup, newVgroup.vnodeGid[0].dnodeId) != 0) - return -1; + TAOS_CHECK_RETURN( + mndAddChangeConfigAction(pMnode, pTrans, pNewDb, pVgroup, &newVgroup, newVgroup.vnodeGid[0].dnodeId)); - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, &newVgroup) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, &newVgroup)); newVgroup.vnodeGid[0].nodeRole = TAOS_SYNC_ROLE_VOTER; newVgroup.vnodeGid[1].nodeRole = TAOS_SYNC_ROLE_VOTER; newVgroup.vnodeGid[2].nodeRole = TAOS_SYNC_ROLE_VOTER; - if (mndAddChangeConfigAction(pMnode, pTrans, pNewDb, pVgroup, &newVgroup, newVgroup.vnodeGid[0].dnodeId) != 0) - return -1; + TAOS_CHECK_RETURN( + mndAddChangeConfigAction(pMnode, pTrans, pNewDb, pVgroup, &newVgroup, newVgroup.vnodeGid[0].dnodeId)); - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, &newVgroup) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, &newVgroup)); SSdbRaw *pVgRaw = mndVgroupActionEncode(&newVgroup); - if (pVgRaw == NULL) return -1; - if (mndTransAppendRedolog(pTrans, pVgRaw) != 0) { + if (pVgRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } + if ((code = mndTransAppendRedolog(pTrans, pVgRaw)) != 0) { sdbFreeRaw(pVgRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pVgRaw, SDB_STATUS_READY); } else if (newVgroup.replica == 3 && pNewDb->cfg.replications == 1) { @@ -2589,38 +2778,46 @@ int32_t mndBuildRaftAlterVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj *pO pVgroup->vgId, pVgroup->vnodeGid[0].dnodeId, pVgroup->vnodeGid[1].dnodeId, pVgroup->vnodeGid[2].dnodeId); SVnodeGid del1 = {0}; - if (mndRemoveVnodeFromVgroupWithoutSave(pMnode, pTrans, &newVgroup, pArray, &del1) != 0) return -1; + TAOS_CHECK_RETURN(mndRemoveVnodeFromVgroupWithoutSave(pMnode, pTrans, &newVgroup, pArray, &del1)); - if (mndAddChangeConfigAction(pMnode, pTrans, pNewDb, pVgroup, &newVgroup, newVgroup.vnodeGid[0].dnodeId) != 0) - return -1; + TAOS_CHECK_RETURN( + mndAddChangeConfigAction(pMnode, pTrans, pNewDb, pVgroup, &newVgroup, newVgroup.vnodeGid[0].dnodeId)); - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, &newVgroup) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, &newVgroup)); - if (mndAddDropVnodeAction(pMnode, pTrans, pNewDb, &newVgroup, &del1, true) != 0) return -1; + TAOS_CHECK_RETURN(mndAddDropVnodeAction(pMnode, pTrans, pNewDb, &newVgroup, &del1, true)); SSdbRaw *pVgRaw = mndVgroupActionEncode(&newVgroup); - if (pVgRaw == NULL) return -1; - if (mndTransAppendRedolog(pTrans, pVgRaw) != 0) { + if (pVgRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } + if ((code = mndTransAppendRedolog(pTrans, pVgRaw)) != 0) { sdbFreeRaw(pVgRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pVgRaw, SDB_STATUS_READY); SVnodeGid del2 = {0}; - if (mndRemoveVnodeFromVgroupWithoutSave(pMnode, pTrans, &newVgroup, pArray, &del2) != 0) return -1; + TAOS_CHECK_RETURN(mndRemoveVnodeFromVgroupWithoutSave(pMnode, pTrans, &newVgroup, pArray, &del2)); - if (mndAddChangeConfigAction(pMnode, pTrans, pNewDb, pVgroup, &newVgroup, newVgroup.vnodeGid[0].dnodeId) != 0) - return -1; + TAOS_CHECK_RETURN( + mndAddChangeConfigAction(pMnode, pTrans, pNewDb, pVgroup, &newVgroup, newVgroup.vnodeGid[0].dnodeId)); - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, &newVgroup) != 0) return -1; + TAOS_CHECK_RETURN(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pNewDb, &newVgroup)); - if (mndAddDropVnodeAction(pMnode, pTrans, pNewDb, &newVgroup, &del2, true) != 0) return -1; + TAOS_CHECK_RETURN(mndAddDropVnodeAction(pMnode, pTrans, pNewDb, &newVgroup, &del2, true)); pVgRaw = mndVgroupActionEncode(&newVgroup); - if (pVgRaw == NULL) return -1; - if (mndTransAppendRedolog(pTrans, pVgRaw) != 0) { + if (pVgRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } + if ((code = mndTransAppendRedolog(pTrans, pVgRaw)) != 0) { sdbFreeRaw(pVgRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pVgRaw, SDB_STATUS_READY); } else { @@ -2631,20 +2828,25 @@ int32_t mndBuildRaftAlterVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj *pO { SSdbRaw *pVgRaw = mndVgroupActionEncode(&newVgroup); - if (pVgRaw == NULL) return -1; - if (mndTransAppendCommitlog(pTrans, pVgRaw) != 0) { + if (pVgRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } + if ((code = mndTransAppendCommitlog(pTrans, pVgRaw)) != 0) { sdbFreeRaw(pVgRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pVgRaw, SDB_STATUS_READY); } - return 0; + TAOS_RETURN(code); } int32_t mndBuildRestoreAlterVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj *db, SVgObj *pVgroup, SDnodeObj *pDnode) { - SVgObj newVgroup = {0}; + int32_t code = 0; + SVgObj newVgroup = {0}; memcpy(&newVgroup, pVgroup, sizeof(SVgObj)); mInfo("db:%s, vgId:%d, restore vnodes, vn:0 dnode:%d", pVgroup->dbName, pVgroup->vgId, pVgroup->vnodeGid[0].dnodeId); @@ -2657,7 +2859,7 @@ int32_t mndBuildRestoreAlterVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj selected = i; } } - if (mndAddCreateVnodeAction(pMnode, pTrans, db, &newVgroup, &newVgroup.vnodeGid[selected]) != 0) return -1; + TAOS_CHECK_RETURN(mndAddCreateVnodeAction(pMnode, pTrans, db, &newVgroup, &newVgroup.vnodeGid[selected])); } else if (newVgroup.replica == 2 || newVgroup.replica == 3) { for (int i = 0; i < newVgroup.replica; i++) { if (newVgroup.vnodeGid[i].dnodeId == pDnode->id) { @@ -2666,25 +2868,29 @@ int32_t mndBuildRestoreAlterVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj newVgroup.vnodeGid[i].nodeRole = TAOS_SYNC_ROLE_VOTER; } } - if (mndRestoreAddCreateVnodeAction(pMnode, pTrans, db, &newVgroup, pDnode) != 0) return -1; + TAOS_CHECK_RETURN(mndRestoreAddCreateVnodeAction(pMnode, pTrans, db, &newVgroup, pDnode)); for (int i = 0; i < newVgroup.replica; i++) { newVgroup.vnodeGid[i].nodeRole = TAOS_SYNC_ROLE_VOTER; if (newVgroup.vnodeGid[i].dnodeId == pDnode->id) { } } - if (mndRestoreAddAlterVnodeTypeAction(pMnode, pTrans, db, &newVgroup, pDnode) != 0) return -1; + TAOS_CHECK_RETURN(mndRestoreAddAlterVnodeTypeAction(pMnode, pTrans, db, &newVgroup, pDnode)); } SSdbRaw *pVgRaw = mndVgroupActionEncode(&newVgroup); - if (pVgRaw == NULL) return -1; - if (mndTransAppendCommitlog(pTrans, pVgRaw) != 0) { + if (pVgRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } + if ((code = mndTransAppendCommitlog(pTrans, pVgRaw)) != 0) { sdbFreeRaw(pVgRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pVgRaw, SDB_STATUS_READY); - return 0; + TAOS_RETURN(code); } static int32_t mndAddAdjustVnodeHashRangeAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup) { @@ -2694,29 +2900,39 @@ static int32_t mndAddAdjustVnodeHashRangeAction(SMnode *pMnode, STrans *pTrans, typedef int32_t (*FpTransActionCb)(STrans *pTrans, SSdbRaw *pRaw); static int32_t mndAddVgStatusAction(STrans *pTrans, SVgObj *pVg, ESdbStatus vgStatus, ETrnStage stage) { + int32_t code = 0; FpTransActionCb appendActionCb = (stage == TRN_STAGE_COMMIT_ACTION) ? mndTransAppendCommitlog : mndTransAppendRedolog; SSdbRaw *pRaw = mndVgroupActionEncode(pVg); - if (pRaw == NULL) goto _err; - if (appendActionCb(pTrans, pRaw) != 0) goto _err; + if (pRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _err; + } + if ((code = appendActionCb(pTrans, pRaw)) != 0) goto _err; (void)sdbSetRawStatus(pRaw, vgStatus); pRaw = NULL; - return 0; + TAOS_RETURN(code); _err: sdbFreeRaw(pRaw); - return -1; + TAOS_RETURN(code); } static int32_t mndAddDbStatusAction(STrans *pTrans, SDbObj *pDb, ESdbStatus dbStatus, ETrnStage stage) { + int32_t code = 0; FpTransActionCb appendActionCb = (stage == TRN_STAGE_COMMIT_ACTION) ? mndTransAppendCommitlog : mndTransAppendRedolog; SSdbRaw *pRaw = mndDbActionEncode(pDb); - if (pRaw == NULL) goto _err; - if (appendActionCb(pTrans, pRaw) != 0) goto _err; + if (pRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _err; + } + if ((code = appendActionCb(pTrans, pRaw)) != 0) goto _err; (void)sdbSetRawStatus(pRaw, dbStatus); pRaw = NULL; - return 0; + TAOS_RETURN(code); _err: sdbFreeRaw(pRaw); - return -1; + TAOS_RETURN(code); } int32_t mndSplitVgroup(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SVgObj *pVgroup) { @@ -2735,11 +2951,11 @@ int32_t mndSplitVgroup(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SVgObj *pVgro // } int32_t numOfStreams = 0; - if (mndGetNumOfStreams(pMnode, pDb->name, &numOfStreams) != 0) { + if ((code = mndGetNumOfStreams(pMnode, pDb->name, &numOfStreams)) != 0) { goto _OVER; } if (numOfStreams > 0) { - terrno = TSDB_CODE_MND_STREAM_MUST_BE_DELETED; + code = TSDB_CODE_MND_STREAM_MUST_BE_DELETED; goto _OVER; } @@ -2759,7 +2975,11 @@ int32_t mndSplitVgroup(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SVgObj *pVgro } pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB, pReq, "split-vgroup"); - if (pTrans == NULL) goto _OVER; + if (pTrans == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } mndTransSetSerial(pTrans); mInfo("trans:%d, used to split vgroup, vgId:%d", pTrans->id, pVgroup->vgId); @@ -2774,31 +2994,36 @@ int32_t mndSplitVgroup(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SVgObj *pVgro } if (newVg1.replica == 1) { - if (mndAddVnodeToVgroup(pMnode, pTrans, &newVg1, pArray) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddVnodeToVgroup(pMnode, pTrans, &newVg1, pArray), NULL, _OVER); newVg1.vnodeGid[1].nodeRole = TAOS_SYNC_ROLE_LEARNER; - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg1, newVg1.vnodeGid[0].dnodeId) != 0) goto _OVER; - if (mndAddCreateVnodeAction(pMnode, pTrans, pDb, &newVg1, &newVg1.vnodeGid[1]) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg1, newVg1.vnodeGid[0].dnodeId), NULL, + _OVER); + TAOS_CHECK_GOTO(mndAddCreateVnodeAction(pMnode, pTrans, pDb, &newVg1, &newVg1.vnodeGid[1]), NULL, _OVER); newVg1.vnodeGid[1].nodeRole = TAOS_SYNC_ROLE_VOTER; - if (mndAddAlterVnodeTypeAction(pMnode, pTrans, pDb, &newVg1, newVg1.vnodeGid[1].dnodeId) != 0) goto _OVER; - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg1, newVg1.vnodeGid[0].dnodeId) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddAlterVnodeTypeAction(pMnode, pTrans, pDb, &newVg1, newVg1.vnodeGid[1].dnodeId), NULL, _OVER); + TAOS_CHECK_GOTO(mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg1, newVg1.vnodeGid[0].dnodeId), NULL, + _OVER); - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg1) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg1), NULL, _OVER); } else if (newVg1.replica == 3) { SVnodeGid del1 = {0}; - if (mndRemoveVnodeFromVgroup(pMnode, pTrans, &newVg1, pArray, &del1) != 0) goto _OVER; - if (mndAddDropVnodeAction(pMnode, pTrans, pDb, &newVg1, &del1, true) != 0) goto _OVER; - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg1, newVg1.vnodeGid[0].dnodeId) != 0) goto _OVER; - if (mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg1, newVg1.vnodeGid[1].dnodeId) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndRemoveVnodeFromVgroup(pMnode, pTrans, &newVg1, pArray, &del1), NULL, _OVER); + TAOS_CHECK_GOTO(mndAddDropVnodeAction(pMnode, pTrans, pDb, &newVg1, &del1, true), NULL, _OVER); + TAOS_CHECK_GOTO(mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg1, newVg1.vnodeGid[0].dnodeId), NULL, + _OVER); + TAOS_CHECK_GOTO(mndAddAlterVnodeReplicaAction(pMnode, pTrans, pDb, &newVg1, newVg1.vnodeGid[1].dnodeId), NULL, + _OVER); } else { goto _OVER; } for (int32_t i = 0; i < newVg1.replica; ++i) { - if (mndAddDisableVnodeWriteAction(pMnode, pTrans, pDb, &newVg1, newVg1.vnodeGid[i].dnodeId) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddDisableVnodeWriteAction(pMnode, pTrans, pDb, &newVg1, newVg1.vnodeGid[i].dnodeId), NULL, + _OVER); } - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg1) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg1), NULL, _OVER); SVgObj newVg2 = {0}; memcpy(&newVg2, &newVg1, sizeof(SVgObj)); @@ -2826,63 +3051,66 @@ int32_t mndSplitVgroup(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SVgObj *pVgro int32_t maxVgId = sdbGetMaxId(pMnode->pSdb, SDB_VGROUP); int32_t srcVgId = newVg1.vgId; newVg1.vgId = maxVgId; - if (mndAddNewVgPrepareAction(pMnode, pTrans, &newVg1) != 0) goto _OVER; - if (mndAddAlterVnodeHashRangeAction(pMnode, pTrans, srcVgId, &newVg1) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddNewVgPrepareAction(pMnode, pTrans, &newVg1), NULL, _OVER); + TAOS_CHECK_GOTO(mndAddAlterVnodeHashRangeAction(pMnode, pTrans, srcVgId, &newVg1), NULL, _OVER); maxVgId++; srcVgId = newVg2.vgId; newVg2.vgId = maxVgId; - if (mndAddNewVgPrepareAction(pMnode, pTrans, &newVg2) != 0) goto _OVER; - if (mndAddAlterVnodeHashRangeAction(pMnode, pTrans, srcVgId, &newVg2) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddNewVgPrepareAction(pMnode, pTrans, &newVg2), NULL, _OVER); + TAOS_CHECK_GOTO(mndAddAlterVnodeHashRangeAction(pMnode, pTrans, srcVgId, &newVg2), NULL, _OVER); - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg1) != 0) goto _OVER; - if (mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg2) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg1), NULL, _OVER); + TAOS_CHECK_GOTO(mndAddAlterVnodeConfirmAction(pMnode, pTrans, pDb, &newVg2), NULL, _OVER); - if (mndAddVgStatusAction(pTrans, &newVg1, SDB_STATUS_READY, TRN_STAGE_REDO_ACTION) < 0) goto _OVER; - if (mndAddVgStatusAction(pTrans, &newVg2, SDB_STATUS_READY, TRN_STAGE_REDO_ACTION) < 0) goto _OVER; - if (mndAddVgStatusAction(pTrans, pVgroup, SDB_STATUS_DROPPED, TRN_STAGE_REDO_ACTION) < 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddVgStatusAction(pTrans, &newVg1, SDB_STATUS_READY, TRN_STAGE_REDO_ACTION), NULL, _OVER); + TAOS_CHECK_GOTO(mndAddVgStatusAction(pTrans, &newVg2, SDB_STATUS_READY, TRN_STAGE_REDO_ACTION), NULL, _OVER); + TAOS_CHECK_GOTO(mndAddVgStatusAction(pTrans, pVgroup, SDB_STATUS_DROPPED, TRN_STAGE_REDO_ACTION), NULL, _OVER); // update db status memcpy(&dbObj, pDb, sizeof(SDbObj)); if (dbObj.cfg.pRetensions != NULL) { dbObj.cfg.pRetensions = taosArrayDup(pDb->cfg.pRetensions, NULL); - if (dbObj.cfg.pRetensions == NULL) goto _OVER; + if (dbObj.cfg.pRetensions == NULL) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _OVER; + } } dbObj.vgVersion++; dbObj.updateTime = taosGetTimestampMs(); dbObj.cfg.numOfVgroups++; - if (mndAddDbStatusAction(pTrans, &dbObj, SDB_STATUS_READY, TRN_STAGE_REDO_ACTION) < 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddDbStatusAction(pTrans, &dbObj, SDB_STATUS_READY, TRN_STAGE_REDO_ACTION), NULL, _OVER); // adjust vgroup replica if (pDb->cfg.replications != newVg1.replica) { SVgObj tmpGroup = {0}; - if (mndBuildAlterVgroupAction(pMnode, pTrans, pDb, pDb, &newVg1, pArray, &tmpGroup) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndBuildAlterVgroupAction(pMnode, pTrans, pDb, pDb, &newVg1, pArray, &tmpGroup), NULL, _OVER); } else { - if (mndAddVgStatusAction(pTrans, &newVg1, SDB_STATUS_READY, TRN_STAGE_COMMIT_ACTION) < 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddVgStatusAction(pTrans, &newVg1, SDB_STATUS_READY, TRN_STAGE_COMMIT_ACTION), NULL, _OVER); } if (pDb->cfg.replications != newVg2.replica) { SVgObj tmpGroup = {0}; - if (mndBuildAlterVgroupAction(pMnode, pTrans, pDb, pDb, &newVg2, pArray, &tmpGroup) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndBuildAlterVgroupAction(pMnode, pTrans, pDb, pDb, &newVg2, pArray, &tmpGroup), NULL, _OVER); } else { - if (mndAddVgStatusAction(pTrans, &newVg2, SDB_STATUS_READY, TRN_STAGE_COMMIT_ACTION) < 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddVgStatusAction(pTrans, &newVg2, SDB_STATUS_READY, TRN_STAGE_COMMIT_ACTION), NULL, _OVER); } - if (mndAddVgStatusAction(pTrans, pVgroup, SDB_STATUS_DROPPED, TRN_STAGE_COMMIT_ACTION) < 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddVgStatusAction(pTrans, pVgroup, SDB_STATUS_DROPPED, TRN_STAGE_COMMIT_ACTION), NULL, _OVER); // commit db status dbObj.vgVersion++; dbObj.updateTime = taosGetTimestampMs(); - if (mndAddDbStatusAction(pTrans, &dbObj, SDB_STATUS_READY, TRN_STAGE_COMMIT_ACTION) < 0) goto _OVER; + TAOS_CHECK_GOTO(mndAddDbStatusAction(pTrans, &dbObj, SDB_STATUS_READY, TRN_STAGE_COMMIT_ACTION), NULL, _OVER); - if (mndTransPrepare(pMnode, pTrans) != 0) goto _OVER; + TAOS_CHECK_GOTO(mndTransPrepare(pMnode, pTrans), NULL, _OVER); code = 0; _OVER: taosArrayDestroy(pArray); mndTransDrop(pTrans); taosArrayDestroy(dbObj.cfg.pRetensions); - return code; + TAOS_RETURN(code); } extern int32_t mndProcessSplitVgroupMsgImp(SRpcMsg *pReq); @@ -2895,22 +3123,27 @@ int32_t mndProcessSplitVgroupMsgImp(SRpcMsg *pReq) { return 0; } static int32_t mndSetBalanceVgroupInfoToTrans(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, SDnodeObj *pSrc, SDnodeObj *pDst) { - SVgObj newVg = {0}; + int32_t code = 0; + SVgObj newVg = {0}; memcpy(&newVg, pVgroup, sizeof(SVgObj)); mInfo("vgId:%d, vgroup info before balance, replica:%d", newVg.vgId, newVg.replica); for (int32_t i = 0; i < newVg.replica; ++i) { mInfo("vgId:%d, vnode:%d dnode:%d", newVg.vgId, i, newVg.vnodeGid[i].dnodeId); } - if (mndAddIncVgroupReplicaToTrans(pMnode, pTrans, pDb, &newVg, pDst->id) != 0) return -1; - if (mndAddDecVgroupReplicaFromTrans(pMnode, pTrans, pDb, &newVg, pSrc->id) != 0) return -1; + TAOS_CHECK_RETURN(mndAddIncVgroupReplicaToTrans(pMnode, pTrans, pDb, &newVg, pDst->id)); + TAOS_CHECK_RETURN(mndAddDecVgroupReplicaFromTrans(pMnode, pTrans, pDb, &newVg, pSrc->id)); { SSdbRaw *pRaw = mndVgroupActionEncode(&newVg); - if (pRaw == NULL) return -1; - if (mndTransAppendCommitlog(pTrans, pRaw) != 0) { + if (pRaw == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } + if ((code = mndTransAppendCommitlog(pTrans, pRaw)) != 0) { sdbFreeRaw(pRaw); - return -1; + TAOS_RETURN(code); } (void)sdbSetRawStatus(pRaw, SDB_STATUS_READY); } @@ -2919,7 +3152,7 @@ static int32_t mndSetBalanceVgroupInfoToTrans(SMnode *pMnode, STrans *pTrans, SD for (int32_t i = 0; i < newVg.replica; ++i) { mInfo("vgId:%d, vnode:%d dnode:%d", newVg.vgId, i, newVg.vnodeGid[i].dnodeId); } - return 0; + TAOS_RETURN(code); } static int32_t mndBalanceVgroupBetweenDnode(SMnode *pMnode, STrans *pTrans, SDnodeObj *pSrc, SDnodeObj *pDst, @@ -2952,6 +3185,8 @@ static int32_t mndBalanceVgroupBetweenDnode(SMnode *pMnode, STrans *pTrans, SDno SDbObj *pDb = mndAcquireDb(pMnode, pVgroup->dbName); if (pDb == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; mError("vgId:%d, balance vgroup can't find db obj dbName:%s", pVgroup->vgId, pVgroup->dbName); goto _OUT; } @@ -2986,7 +3221,11 @@ static int32_t mndBalanceVgroup(SMnode *pMnode, SRpcMsg *pReq, SArray *pArray) { if (pBalancedVgroups == NULL) goto _OVER; pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_GLOBAL, pReq, "balance-vgroup"); - if (pTrans == NULL) goto _OVER; + if (pTrans == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } mndTransSetSerial(pTrans); mInfo("trans:%d, used to balance vgroup", pTrans->id); @@ -3035,7 +3274,7 @@ static int32_t mndBalanceVgroup(SMnode *pMnode, SRpcMsg *pReq, SArray *pArray) { _OVER: taosHashCleanup(pBalancedVgroups); mndTransDrop(pTrans); - return code; + TAOS_RETURN(code); } static int32_t mndProcessBalanceVgroupMsg(SRpcMsg *pReq) { @@ -3047,12 +3286,12 @@ static int32_t mndProcessBalanceVgroupMsg(SRpcMsg *pReq) { SBalanceVgroupReq req = {0}; if (tDeserializeSBalanceVgroupReq(pReq->pCont, pReq->contLen, &req) != 0) { - terrno = TSDB_CODE_INVALID_MSG; + code = TSDB_CODE_INVALID_MSG; goto _OVER; } mInfo("start to balance vgroup"); - if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_BALANCE_VGROUP) != 0) { + if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_BALANCE_VGROUP)) != 0) { goto _OVER; } @@ -3062,7 +3301,7 @@ static int32_t mndProcessBalanceVgroupMsg(SRpcMsg *pReq) { if (pIter == NULL) break; if (!mndIsDnodeOnline(pDnode, curMs)) { sdbCancelFetch(pMnode->pSdb, pIter); - terrno = TSDB_CODE_MND_HAS_OFFLINE_DNODE; + code = TSDB_CODE_MND_HAS_OFFLINE_DNODE; mError("failed to balance vgroup since %s, dnode:%d", terrstr(), pDnode->id); sdbRelease(pMnode->pSdb, pDnode); goto _OVER; @@ -3072,7 +3311,11 @@ static int32_t mndProcessBalanceVgroupMsg(SRpcMsg *pReq) { } pArray = mndBuildDnodesArray(pMnode, 0); - if (pArray == NULL) goto _OVER; + if (pArray == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + goto _OVER; + } if (taosArrayGetSize(pArray) < 2) { mInfo("no need to balance vgroup since dnode num less than 2"); @@ -3085,12 +3328,12 @@ static int32_t mndProcessBalanceVgroupMsg(SRpcMsg *pReq) { _OVER: if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) { - mError("failed to balance vgroup since %s", terrstr()); + mError("failed to balance vgroup since %s", tstrerror(code)); } taosArrayDestroy(pArray); tFreeSBalanceVgroupReq(&req); - return code; + TAOS_RETURN(code); } bool mndVgroupInDb(SVgObj *pVgroup, int64_t dbUid) { return !pVgroup->isTsma && pVgroup->dbUid == dbUid; } @@ -3135,27 +3378,32 @@ static void *mndBuildCompactVnodeReq(SMnode *pMnode, SDbObj *pDb, SVgObj *pVgrou static int32_t mndAddCompactVnodeAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, int64_t compactTs, STimeWindow tw) { + int32_t code = 0; STransAction action = {0}; action.epSet = mndGetVgroupEpset(pMnode, pVgroup); int32_t contLen = 0; void *pReq = mndBuildCompactVnodeReq(pMnode, pDb, pVgroup, &contLen, compactTs, tw); - if (pReq == NULL) return -1; + if (pReq == NULL) { + code = TSDB_CODE_MND_RETURN_VALUE_NULL; + if (terrno != 0) code = terrno; + TAOS_RETURN(code); + } action.pCont = pReq; action.contLen = contLen; action.msgType = TDMT_VND_COMPACT; - if (mndTransAppendRedoAction(pTrans, &action) != 0) { + if ((code = mndTransAppendRedoAction(pTrans, &action)) != 0) { taosMemoryFree(pReq); - return -1; + TAOS_RETURN(code); } - return 0; + TAOS_RETURN(code); } int32_t mndBuildCompactVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SVgObj *pVgroup, int64_t compactTs, STimeWindow tw) { - if (mndAddCompactVnodeAction(pMnode, pTrans, pDb, pVgroup, compactTs, tw) != 0) return -1; + TAOS_CHECK_RETURN(mndAddCompactVnodeAction(pMnode, pTrans, pDb, pVgroup, compactTs, tw)); return 0; } diff --git a/source/dnode/mnode/impl/src/mndView.c b/source/dnode/mnode/impl/src/mndView.c index d53e740736..f4138b2afe 100755 --- a/source/dnode/mnode/impl/src/mndView.c +++ b/source/dnode/mnode/impl/src/mndView.c @@ -43,18 +43,15 @@ int32_t mndInitView(SMnode *pMnode) { #endif } -void mndCleanupView(SMnode *pMnode) { - mDebug("mnd view cleanup"); -} +void mndCleanupView(SMnode *pMnode) { mDebug("mnd view cleanup"); } int32_t mndProcessCreateViewReq(SRpcMsg *pReq) { #ifndef TD_ENTERPRISE return TSDB_CODE_OPS_NOT_SUPPORT; #else - SCMCreateViewReq createViewReq = {0}; + SCMCreateViewReq createViewReq = {0}; if (tDeserializeSCMCreateViewReq(pReq->pCont, pReq->contLen, &createViewReq) != 0) { - terrno = TSDB_CODE_INVALID_MSG; - return -1; + TAOS_RETURN(TSDB_CODE_INVALID_MSG); } mInfo("start to create view:%s, sql:%s", createViewReq.fullname, createViewReq.sql); @@ -65,17 +62,16 @@ int32_t mndProcessCreateViewReq(SRpcMsg *pReq) { int32_t mndProcessDropViewReq(SRpcMsg *pReq) { #ifndef TD_ENTERPRISE - return TSDB_CODE_OPS_NOT_SUPPORT; + return TSDB_CODE_OPS_NOT_SUPPORT; #else - SCMDropViewReq dropViewReq = {0}; - if (tDeserializeSCMDropViewReq(pReq->pCont, pReq->contLen, &dropViewReq) != 0) { - terrno = TSDB_CODE_INVALID_MSG; - return -1; - } - - mInfo("start to drop view:%s, sql:%s", dropViewReq.name, dropViewReq.sql); - - return mndProcessDropViewReqImpl(&dropViewReq, pReq); + SCMDropViewReq dropViewReq = {0}; + if (tDeserializeSCMDropViewReq(pReq->pCont, pReq->contLen, &dropViewReq) != 0) { + TAOS_RETURN(TSDB_CODE_INVALID_MSG); + } + + mInfo("start to drop view:%s, sql:%s", dropViewReq.name, dropViewReq.sql); + + return mndProcessDropViewReqImpl(&dropViewReq, pReq); #endif } @@ -83,18 +79,16 @@ int32_t mndProcessGetViewMetaReq(SRpcMsg *pReq) { #ifndef TD_ENTERPRISE return TSDB_CODE_OPS_NOT_SUPPORT; #else - SViewMetaReq req = {0}; + SViewMetaReq req = {0}; if (tDeserializeSViewMetaReq(pReq->pCont, pReq->contLen, &req) != 0) { - terrno = TSDB_CODE_INVALID_MSG; - return -1; + TAOS_RETURN(TSDB_CODE_INVALID_MSG); } return mndProcessViewMetaReqImpl(&req, pReq); -#endif +#endif } - int32_t mndRetrieveView(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) { #ifndef TD_ENTERPRISE return 0; @@ -107,6 +101,3 @@ void mndCancelGetNextView(SMnode *pMnode, void *pIter) { SSdb *pSdb = pMnode->pSdb; sdbCancelFetch(pSdb, pIter); } - - - From 47a16a94565c054e4c8656e91730f885bdff2357 Mon Sep 17 00:00:00 2001 From: dmchen Date: Tue, 23 Jul 2024 09:56:13 +0000 Subject: [PATCH 2/5] fix/TD-30989 --- source/libs/monitor/src/monMain.c | 38 +++++++++++++++---------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/source/libs/monitor/src/monMain.c b/source/libs/monitor/src/monMain.c index 3389780916..6cba9b0727 100644 --- a/source/libs/monitor/src/monMain.c +++ b/source/libs/monitor/src/monMain.c @@ -15,17 +15,17 @@ #define _DEFAULT_SOURCE #include "monInt.h" +#include "taos_monitor.h" #include "taoserror.h" +#include "tglobal.h" #include "thttp.h" #include "ttime.h" -#include "taos_monitor.h" -#include "tglobal.h" SMonitor tsMonitor = {0}; -char* tsMonUri = "/report"; -char* tsMonFwUri = "/general-metric"; -char* tsMonSlowLogUri = "/slow-sql-detail-batch"; -char* tsMonFwBasicUri = "/taosd-cluster-basic"; +char *tsMonUri = "/report"; +char *tsMonFwUri = "/general-metric"; +char *tsMonSlowLogUri = "/slow-sql-detail-batch"; +char *tsMonFwBasicUri = "/taosd-cluster-basic"; void monRecordLog(int64_t ts, ELogLevel level, const char *content) { taosThreadMutexLock(&tsMonitor.lock); @@ -54,8 +54,7 @@ int32_t monGetLogs(SMonLogs *logs) { taosArrayClear(tsMonitor.logs); taosThreadMutexUnlock(&tsMonitor.lock); if (logs->logs == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } return 0; } @@ -105,8 +104,7 @@ void monSetBmInfo(SMonBmInfo *pInfo) { int32_t monInit(const SMonCfg *pCfg) { tsMonitor.logs = taosArrayInit(16, sizeof(SMonLogItem)); if (tsMonitor.logs == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } tsMonitor.cfg = *pCfg; @@ -146,6 +144,7 @@ static void monCleanupMonitorInfo(SMonInfo *pMonitor) { } static SMonInfo *monCreateMonitorInfo() { + terrno = 0; SMonInfo *pMonitor = taosMemoryCalloc(1, sizeof(SMonInfo)); if (pMonitor == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; @@ -274,11 +273,11 @@ static void monGenClusterJsonBasic(SMonInfo *pMonitor) { SMonClusterInfo *pInfo = &pMonitor->mmInfo.cluster; if (pMonitor->mmInfo.cluster.first_ep_dnode_id == 0) return; - //tjsonAddStringToObject(pMonitor->pJson, "first_ep", pInfo->first_ep); + // tjsonAddStringToObject(pMonitor->pJson, "first_ep", pInfo->first_ep); tjsonAddStringToObject(pMonitor->pJson, "first_ep", tsFirst); tjsonAddDoubleToObject(pMonitor->pJson, "first_ep_dnode_id", pInfo->first_ep_dnode_id); tjsonAddStringToObject(pMonitor->pJson, "cluster_version", pInfo->version); - //tjsonAddDoubleToObject(pMonitor->pJson, "monitor_interval", pInfo->monitor_interval); + // tjsonAddDoubleToObject(pMonitor->pJson, "monitor_interval", pInfo->monitor_interval); } static void monGenVgroupJson(SMonInfo *pMonitor) { @@ -554,9 +553,9 @@ static void monGenLogJson(SMonInfo *pMonitor) { if (tjsonAddItemToArray(pSummaryJson, pLogTrace) != 0) tjsonDelete(pLogTrace); } -void monSendReport(SMonInfo *pMonitor){ +void monSendReport(SMonInfo *pMonitor) { char *pCont = tjsonToString(pMonitor->pJson); - if(tsMonitorLogProtocol){ + if (tsMonitorLogProtocol) { uInfoL("report cont:\n%s", pCont); } if (pCont != NULL) { @@ -591,7 +590,7 @@ void monGenAndSendReport() { SMonInfo *pMonitor = monCreateMonitorInfo(); if (pMonitor == NULL) return; - if(!tsMonitorForceV2){ + if (!tsMonitorForceV2) { monGenBasicJson(pMonitor); monGenClusterJson(pMonitor); monGenVgroupJson(pMonitor); @@ -602,8 +601,7 @@ void monGenAndSendReport() { monGenLogJson(pMonitor); monSendReport(pMonitor); - } - else{ + } else { monGenClusterInfoTable(pMonitor); monGenVgroupInfoTable(pMonitor); monGenDnodeInfoTable(pMonitor); @@ -624,10 +622,10 @@ void monGenAndSendReport() { monCleanupMonitorInfo(pMonitor); } -void monSendContent(char *pCont, const char* uri) { +void monSendContent(char *pCont, const char *uri) { if (!tsEnableMonitor || tsMonitorFqdn[0] == 0 || tsMonitorPort == 0) return; - if(tsMonitorLogProtocol){ - if (pCont != NULL){ + if (tsMonitorLogProtocol) { + if (pCont != NULL) { uInfoL("report client cont:\n%s\n", pCont); } } From 4281289eef4283f0992169a6395e197b5221c080 Mon Sep 17 00:00:00 2001 From: Shungang Li Date: Tue, 23 Jul 2024 15:06:14 +0800 Subject: [PATCH 3/5] feat: (errcode) tconfig.c --- include/util/tconfig.h | 8 +- include/util/tutil.h | 4 +- source/common/src/tglobal.c | 13 +- source/common/src/tmisce.c | 10 +- source/util/src/tconfig.c | 350 +++++++++++++++++------------------ source/util/test/cfgTest.cpp | 11 +- 6 files changed, 191 insertions(+), 205 deletions(-) diff --git a/include/util/tconfig.h b/include/util/tconfig.h index 8373e00085..f109153384 100644 --- a/include/util/tconfig.h +++ b/include/util/tconfig.h @@ -101,7 +101,7 @@ typedef struct { typedef struct SConfig SConfig; typedef struct SConfigIter SConfigIter; -SConfig *cfgInit(); +int32_t cfgInit(SConfig **ppCfg); int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const void *sourceStr); int32_t cfgLoadFromArray(SConfig *pCfg, SArray *pArgs); // SConfigPair void cfgCleanup(SConfig *pCfg); @@ -110,7 +110,7 @@ SConfigItem *cfgGetItem(SConfig *pCfg, const char *pName); int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcType stype, bool lock); int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *pVal, bool isServer); -SConfigIter *cfgCreateIter(SConfig *pConf); +int32_t cfgCreateIter(SConfig *pConf, SConfigIter **ppIter); SConfigItem *cfgNextIter(SConfigIter *pIter); void cfgDestroyIter(SConfigIter *pIter); void cfgLock(SConfig *pCfg); @@ -131,8 +131,8 @@ int32_t cfgAddTimezone(SConfig *pCfg, const char *name, const char *defaultVal, const char *cfgStypeStr(ECfgSrcType type); const char *cfgDtypeStr(ECfgDataType type); -void cfgDumpItemValue(SConfigItem *pItem, char *buf, int32_t bufSize, int32_t *pLen); -void cfgDumpItemScope(SConfigItem *pItem, char *buf, int32_t bufSize, int32_t *pLen); +int32_t cfgDumpItemValue(SConfigItem *pItem, char *buf, int32_t bufSize, int32_t *pLen); +int32_t cfgDumpItemScope(SConfigItem *pItem, char *buf, int32_t bufSize, int32_t *pLen); void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump); void cfgDumpCfgS3(SConfig *pCfg, bool tsc, bool dump); diff --git a/include/util/tutil.h b/include/util/tutil.h index 5af79dfc49..a41f7d5860 100644 --- a/include/util/tutil.h +++ b/include/util/tutil.h @@ -152,9 +152,9 @@ static FORCE_INLINE int32_t taosGetTbHashVal(const char *tbname, int32_t tblen, #define TCONTAINER_OF(ptr, type, member) ((type *)((char *)(ptr)-offsetof(type, member))) -#define TAOS_RETURN(code) \ +#define TAOS_RETURN(CODE) \ do { \ - return (terrno = (code)); \ + return (terrno = (CODE)); \ } while (0) #define TAOS_CHECK_RETURN(CMD) \ diff --git a/source/common/src/tglobal.c b/source/common/src/tglobal.c index bffc0b5557..c80d96d189 100644 --- a/source/common/src/tglobal.c +++ b/source/common/src/tglobal.c @@ -1263,8 +1263,8 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi const char *envFile, char *apolloUrl, SArray *pArgs, bool tsc) { if (tsCfg == NULL) osDefaultInit(); - SConfig *pCfg = cfgInit(); - if (pCfg == NULL) return -1; + SConfig *pCfg = NULL; + TAOS_CHECK_RETURN(cfgInit(&pCfg)); if (tsc) { tsLogEmbedded = 0; @@ -1325,8 +1325,8 @@ int32_t taosReadDataFolder(const char *cfgDir, const char **envCmd, const char * SArray *pArgs) { if (tsCfg == NULL) osDefaultInit(); - SConfig *pCfg = cfgInit(); - if (pCfg == NULL) return -1; + SConfig *pCfg = NULL; + TAOS_CHECK_RETURN(cfgInit(&pCfg)); if (cfgAddDir(pCfg, "dataDir", tsDataDir, CFG_SCOPE_SERVER, CFG_DYN_NONE) != 0) return -1; if (cfgAddInt32(pCfg, "dDebugFlag", dDebugFlag, 0, 255, CFG_SCOPE_SERVER, CFG_DYN_SERVER) != 0) return -1; @@ -1371,10 +1371,7 @@ static int32_t taosCheckGlobalCfg() { static int32_t cfgInitWrapper(SConfig **pCfg) { if (*pCfg == NULL) { - *pCfg = cfgInit(); - if (*pCfg == NULL) { - return terrno; - } + TAOS_CHECK_RETURN(cfgInit(pCfg)); } return 0; } diff --git a/source/common/src/tmisce.c b/source/common/src/tmisce.c index 154fcc3f6b..dd371c6a2a 100644 --- a/source/common/src/tmisce.c +++ b/source/common/src/tmisce.c @@ -270,11 +270,7 @@ int32_t dumpConfToDataBlock(SSDataBlock* pBlock, int32_t startCol) { TAOS_CHECK_GOTO(blockDataEnsureCapacity(pBlock, cfgGetSize(pConf)), NULL, _exit); - pIter = cfgCreateIter(pConf); - if (pIter == NULL) { - code = TSDB_CODE_OUT_OF_MEMORY; - TAOS_CHECK_GOTO(code, NULL, _exit); - } + TAOS_CHECK_GOTO(cfgCreateIter(pConf, &pIter), NULL, _exit); cfgLock(pConf); locked = 1; @@ -296,7 +292,7 @@ int32_t dumpConfToDataBlock(SSDataBlock* pBlock, int32_t startCol) { char value[TSDB_CONFIG_VALUE_LEN + VARSTR_HEADER_SIZE] = {0}; int32_t valueLen = 0; - cfgDumpItemValue(pItem, &value[VARSTR_HEADER_SIZE], TSDB_CONFIG_VALUE_LEN, &valueLen); + TAOS_CHECK_GOTO(cfgDumpItemValue(pItem, &value[VARSTR_HEADER_SIZE], TSDB_CONFIG_VALUE_LEN, &valueLen), NULL, _exit); varDataSetLen(value, valueLen); pColInfo = taosArrayGet(pBlock->pDataBlock, col++); @@ -308,7 +304,7 @@ int32_t dumpConfToDataBlock(SSDataBlock* pBlock, int32_t startCol) { TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, value, false), NULL, _exit); char scope[TSDB_CONFIG_SCOPE_LEN + VARSTR_HEADER_SIZE] = {0}; - cfgDumpItemScope(pItem, &scope[VARSTR_HEADER_SIZE], TSDB_CONFIG_SCOPE_LEN, &valueLen); + TAOS_CHECK_GOTO(cfgDumpItemScope(pItem, &scope[VARSTR_HEADER_SIZE], TSDB_CONFIG_SCOPE_LEN, &valueLen), NULL, _exit); varDataSetLen(scope, valueLen); pColInfo = taosArrayGet(pBlock->pDataBlock, col++); diff --git a/source/util/src/tconfig.c b/source/util/src/tconfig.c index 268b0b8497..83a185adae 100644 --- a/source/util/src/tconfig.c +++ b/source/util/src/tconfig.c @@ -42,22 +42,21 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url); extern char **environ; -SConfig *cfgInit() { +int32_t cfgInit(SConfig ** ppCfg) { SConfig *pCfg = taosMemoryCalloc(1, sizeof(SConfig)); if (pCfg == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } pCfg->array = taosArrayInit(32, sizeof(SConfigItem)); if (pCfg->array == NULL) { taosMemoryFree(pCfg); - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } taosThreadMutexInit(&pCfg->lock, NULL); - return pCfg; + *ppCfg = pCfg; + TAOS_RETURN(TSDB_CODE_SUCCESS); } int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const void *sourceStr) { @@ -73,7 +72,7 @@ int32_t cfgLoad(SConfig *pCfg, ECfgSrcType cfgType, const void *sourceStr) { case CFG_STYPE_ENV_CMD: return cfgLoadFromEnvCmd(pCfg, (const char **)sourceStr); default: - return -1; + return TSDB_CODE_INVALID_PARA; } } @@ -82,11 +81,11 @@ int32_t cfgLoadFromArray(SConfig *pCfg, SArray *pArgs) { for (int32_t i = 0; i < size; ++i) { SConfigPair *pPair = taosArrayGet(pArgs, i); if (cfgSetItem(pCfg, pPair->name, pPair->value, CFG_STYPE_ARG_LIST, true) != 0) { - return -1; + return TSDB_CODE_INVALID_PARA; } } - return 0; + return TSDB_CODE_SUCCESS; } void cfgItemFreeVal(SConfigItem *pItem) { @@ -126,28 +125,27 @@ static int32_t cfgCheckAndSetConf(SConfigItem *pItem, const char *conf) { pItem->str = taosStrdup(conf); if (pItem->str == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) { char fullDir[PATH_MAX] = {0}; if (taosExpandDir(inputDir, fullDir, PATH_MAX) != 0) { - uError("failed to expand dir:%s", inputDir); - return -1; + int32_t code = TAOS_SYSTEM_ERROR(errno); + uError("failed to expand dir:%s since %s", inputDir, tstrerror(code)); + TAOS_RETURN(code); } taosMemoryFreeClear(pItem->str); pItem->str = taosStrdup(fullDir); if (pItem->str == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } static int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) { @@ -166,97 +164,88 @@ static int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType sty static int32_t cfgSetInt32(SConfigItem *pItem, const char *value, ECfgSrcType stype) { int32_t ival; - int32_t code = taosStrHumanToInt32(value, &ival); - if (code != TSDB_CODE_SUCCESS) return code; + TAOS_CHECK_RETURN(taosStrHumanToInt32(value, &ival)); if (ival < pItem->imin || ival > pItem->imax) { uError("cfg:%s, type:%s src:%s value:%d out of range[%" PRId64 ", %" PRId64 "]", pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->imin, pItem->imax); - terrno = TSDB_CODE_OUT_OF_RANGE; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } pItem->i32 = ival; pItem->stype = stype; - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } static int32_t cfgSetInt64(SConfigItem *pItem, const char *value, ECfgSrcType stype) { int64_t ival; - int32_t code = taosStrHumanToInt64(value, &ival); - if (code != TSDB_CODE_SUCCESS) return code; + TAOS_CHECK_RETURN(taosStrHumanToInt64(value, &ival)); if (ival < pItem->imin || ival > pItem->imax) { uError("cfg:%s, type:%s src:%s value:%" PRId64 " out of range[%" PRId64 ", %" PRId64 "]", pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), ival, pItem->imin, pItem->imax); - terrno = TSDB_CODE_OUT_OF_RANGE; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } pItem->i64 = ival; pItem->stype = stype; - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } static int32_t cfgSetFloat(SConfigItem *pItem, const char *value, ECfgSrcType stype) { double dval; - int32_t code = parseCfgReal(value, &dval); + TAOS_CHECK_RETURN(parseCfgReal(value, &dval)); if (dval < pItem->fmin || dval > pItem->fmax) { uError("cfg:%s, type:%s src:%s value:%f out of range[%f, %f]", pItem->name, cfgDtypeStr(pItem->dtype), cfgStypeStr(stype), dval, pItem->fmin, pItem->fmax); - terrno = TSDB_CODE_OUT_OF_RANGE; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } pItem->fval = (float)dval; pItem->stype = stype; - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } static int32_t cfgSetString(SConfigItem *pItem, const char *value, ECfgSrcType stype) { char *tmp = taosStrdup(value); if (tmp == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s", pItem->name, cfgDtypeStr(pItem->dtype), - cfgStypeStr(stype), value, terrstr()); - return -1; + cfgStypeStr(stype), value, tstrerror(TSDB_CODE_OUT_OF_MEMORY)); + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } taosMemoryFreeClear(pItem->str); pItem->str = tmp; pItem->stype = stype; - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } static int32_t cfgSetDir(SConfigItem *pItem, const char *value, ECfgSrcType stype) { - if (cfgCheckAndSetDir(pItem, value) != 0) { + int32_t code = cfgCheckAndSetDir(pItem, value); + if (TSDB_CODE_SUCCESS != code) { uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s", pItem->name, cfgDtypeStr(pItem->dtype), - cfgStypeStr(stype), value, terrstr()); - return -1; + cfgStypeStr(stype), value, tstrerror(code)); + TAOS_RETURN(code); } pItem->stype = stype; - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } static int32_t doSetConf(SConfigItem *pItem, const char *value, ECfgSrcType stype) { - if (cfgCheckAndSetConf(pItem, value) != 0) { - terrno = TSDB_CODE_OUT_OF_MEMORY; + int32_t code = cfgCheckAndSetConf(pItem, value); + if (TSDB_CODE_SUCCESS != code) { uError("cfg:%s, type:%s src:%s value:%s failed to dup since %s", pItem->name, cfgDtypeStr(pItem->dtype), - cfgStypeStr(stype), value, terrstr()); - return -1; + cfgStypeStr(stype), value, tstrerror(TSDB_CODE_OUT_OF_MEMORY)); + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } pItem->stype = stype; - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } static int32_t cfgSetTimezone(SConfigItem *pItem, const char *value, ECfgSrcType stype) { - int32_t code = doSetConf(pItem, value, stype); - if (code != TSDB_CODE_SUCCESS) { - return code; - } - + TAOS_CHECK_RETURN(doSetConf(pItem, value, stype)); osSetTimezone(value); - return code; + TAOS_RETURN(TSDB_CODE_SUCCESS); } static int32_t cfgSetTfsItem(SConfig *pCfg, const char *name, const char *value, const char *level, const char *primary, @@ -267,16 +256,15 @@ static int32_t cfgSetTfsItem(SConfig *pCfg, const char *name, const char *value, if (pItem == NULL) { taosThreadMutexUnlock(&pCfg->lock); - return -1; + TAOS_RETURN(TSDB_CODE_CFG_NOT_FOUND); } if (pItem->array == NULL) { pItem->array = taosArrayInit(16, sizeof(SDiskCfg)); if (pItem->array == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; taosThreadMutexUnlock(&pCfg->lock); - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } } @@ -287,34 +275,32 @@ static int32_t cfgSetTfsItem(SConfig *pCfg, const char *name, const char *value, cfg.disable = disable ? atoi(disable) : 0; void *ret = taosArrayPush(pItem->array, &cfg); if (ret == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; taosThreadMutexUnlock(&pCfg->lock); - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } pItem->stype = stype; taosThreadMutexUnlock(&pCfg->lock); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } static int32_t cfgUpdateDebugFlagItem(SConfig *pCfg, const char *name, bool resetArray) { SConfigItem *pDebugFlagItem = cfgGetItem(pCfg, "debugFlag"); if (resetArray) { // reset - if (pDebugFlagItem == NULL) return -1; + if (pDebugFlagItem == NULL) TAOS_RETURN(TSDB_CODE_CFG_NOT_FOUND); // logflag names that should 'not' be set by 'debugFlag' if (pDebugFlagItem->array == NULL) { pDebugFlagItem->array = taosArrayInit(16, sizeof(SLogVar)); if (pDebugFlagItem->array == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } } taosArrayClear(pDebugFlagItem->array); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } // update @@ -322,9 +308,11 @@ static int32_t cfgUpdateDebugFlagItem(SConfig *pCfg, const char *name, bool rese if (pDebugFlagItem->array != NULL) { SLogVar logVar = {0}; strncpy(logVar.name, name, TSDB_LOG_VAR_LEN - 1); - taosArrayPush(pDebugFlagItem->array, &logVar); + if (NULL == taosArrayPush(pDebugFlagItem->array, &logVar)) { + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); + } } - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcType stype, bool lock) { @@ -337,9 +325,8 @@ int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcTy SConfigItem *pItem = cfgGetItem(pCfg, name); if (pItem == NULL) { - terrno = TSDB_CODE_CFG_NOT_FOUND; taosThreadMutexUnlock(&pCfg->lock); - return -1; + TAOS_RETURN(TSDB_CODE_CFG_NOT_FOUND); } switch (pItem->dtype) { @@ -382,7 +369,7 @@ int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcTy } case CFG_DTYPE_NONE: default: - terrno = TSDB_CODE_INVALID_CFG; + code = TSDB_CODE_INVALID_CFG; break; } @@ -390,7 +377,7 @@ int32_t cfgSetItem(SConfig *pCfg, const char *name, const char *value, ECfgSrcTy taosThreadMutexUnlock(&pCfg->lock); } - return code; + TAOS_RETURN(code); } SConfigItem *cfgGetItem(SConfig *pCfg, const char *pName) { @@ -403,7 +390,6 @@ SConfigItem *cfgGetItem(SConfig *pCfg, const char *pName) { } } - terrno = TSDB_CODE_CFG_NOT_FOUND; return NULL; } @@ -420,27 +406,25 @@ void cfgUnLock(SConfig *pCfg) { } int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *pVal, bool isServer) { - ECfgDynType dynType = isServer ? CFG_DYN_SERVER : CFG_DYN_CLIENT; + ECfgDynType dynType = isServer ? CFG_DYN_SERVER : CFG_DYN_CLIENT; cfgLock(pCfg); SConfigItem *pItem = cfgGetItem(pCfg, name); if (!pItem || (pItem->dynScope & dynType) == 0) { uError("failed to config:%s, not support update this config", name); - terrno = TSDB_CODE_INVALID_CFG; cfgUnLock(pCfg); - return -1; + TAOS_RETURN(TSDB_CODE_INVALID_CFG); } switch (pItem->dtype) { - case CFG_DTYPE_STRING:{ - if(strcasecmp(name, "slowLogScope") == 0){ - char* tmp = taosStrdup(pVal); - if(taosSetSlowLogScope(tmp) < 0){ - terrno = TSDB_CODE_INVALID_CFG; + case CFG_DTYPE_STRING: { + if (strcasecmp(name, "slowLogScope") == 0) { + char *tmp = taosStrdup(pVal); + if (taosSetSlowLogScope(tmp) < 0) { cfgUnLock(pCfg); taosMemoryFree(tmp); - return -1; + TAOS_RETURN(TSDB_CODE_INVALID_CFG); } taosMemoryFree(tmp); } @@ -449,9 +433,8 @@ int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *p int32_t ival = (int32_t)atoi(pVal); if (ival != 0 && ival != 1) { uError("cfg:%s, type:%s value:%d out of range[0, 1]", pItem->name, cfgDtypeStr(pItem->dtype), ival); - terrno = TSDB_CODE_OUT_OF_RANGE; cfgUnLock(pCfg); - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } } break; case CFG_DTYPE_INT32: { @@ -464,9 +447,8 @@ int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *p if (ival < pItem->imin || ival > pItem->imax) { uError("cfg:%s, type:%s value:%d out of range[%" PRId64 ", %" PRId64 "]", pItem->name, cfgDtypeStr(pItem->dtype), ival, pItem->imin, pItem->imax); - terrno = TSDB_CODE_OUT_OF_RANGE; cfgUnLock(pCfg); - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } } break; case CFG_DTYPE_INT64: { @@ -474,30 +456,28 @@ int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *p int32_t code = taosStrHumanToInt64(pVal, &ival); if (code != TSDB_CODE_SUCCESS) { cfgUnLock(pCfg); - return code; + TAOS_RETURN(code); } if (ival < pItem->imin || ival > pItem->imax) { uError("cfg:%s, type:%s value:%" PRId64 " out of range[%" PRId64 ", %" PRId64 "]", pItem->name, cfgDtypeStr(pItem->dtype), ival, pItem->imin, pItem->imax); - terrno = TSDB_CODE_OUT_OF_RANGE; cfgUnLock(pCfg); - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } } break; case CFG_DTYPE_FLOAT: case CFG_DTYPE_DOUBLE: { - double dval; + double dval; int32_t code = parseCfgReal(pVal, &dval); if (code != TSDB_CODE_SUCCESS) { cfgUnLock(pCfg); - return code; + TAOS_RETURN(code); } if (dval < pItem->fmin || dval > pItem->fmax) { uError("cfg:%s, type:%s value:%f out of range[%f, %f]", pItem->name, cfgDtypeStr(pItem->dtype), dval, pItem->fmin, pItem->fmax); - terrno = TSDB_CODE_OUT_OF_RANGE; cfgUnLock(pCfg); - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } } break; default: @@ -505,15 +485,14 @@ int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *p } cfgUnLock(pCfg); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } static int32_t cfgAddItem(SConfig *pCfg, SConfigItem *pItem, const char *name) { pItem->stype = CFG_STYPE_DEFAULT; pItem->name = taosStrdup(name); if (pItem->name == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } int32_t size = taosArrayGetSize(pCfg->array); @@ -521,7 +500,7 @@ static int32_t cfgAddItem(SConfig *pCfg, SConfigItem *pItem, const char *name) { SConfigItem *existItem = taosArrayGet(pCfg->array, i); if (existItem != NULL && strcmp(existItem->name, pItem->name) == 0) { taosMemoryFree(pItem->name); - return TSDB_CODE_INVALID_CFG; + TAOS_RETURN(TSDB_CODE_INVALID_CFG); } } @@ -535,11 +514,10 @@ static int32_t cfgAddItem(SConfig *pCfg, SConfigItem *pItem, const char *name) { } taosMemoryFree(pItem->name); - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } int32_t cfgAddBool(SConfig *pCfg, const char *name, bool defaultVal, int8_t scope, int8_t dynScope) { @@ -550,8 +528,7 @@ int32_t cfgAddBool(SConfig *pCfg, const char *name, bool defaultVal, int8_t scop int32_t cfgAddInt32(SConfig *pCfg, const char *name, int32_t defaultVal, int64_t minval, int64_t maxval, int8_t scope, int8_t dynScope) { if (defaultVal < minval || defaultVal > maxval) { - terrno = TSDB_CODE_OUT_OF_RANGE; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } SConfigItem item = {.dtype = CFG_DTYPE_INT32, @@ -566,8 +543,7 @@ int32_t cfgAddInt32(SConfig *pCfg, const char *name, int32_t defaultVal, int64_t int32_t cfgAddInt64(SConfig *pCfg, const char *name, int64_t defaultVal, int64_t minval, int64_t maxval, int8_t scope, int8_t dynScope) { if (defaultVal < minval || defaultVal > maxval) { - terrno = TSDB_CODE_OUT_OF_RANGE; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } SConfigItem item = {.dtype = CFG_DTYPE_INT64, @@ -582,8 +558,7 @@ int32_t cfgAddInt64(SConfig *pCfg, const char *name, int64_t defaultVal, int64_t int32_t cfgAddFloat(SConfig *pCfg, const char *name, float defaultVal, float minval, float maxval, int8_t scope, int8_t dynScope) { if (defaultVal < minval || defaultVal > maxval) { - terrno = TSDB_CODE_OUT_OF_RANGE; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); } SConfigItem item = {.dtype = CFG_DTYPE_FLOAT, @@ -599,45 +574,32 @@ int32_t cfgAddString(SConfig *pCfg, const char *name, const char *defaultVal, in SConfigItem item = {.dtype = CFG_DTYPE_STRING, .scope = scope, .dynScope = dynScope}; item.str = taosStrdup(defaultVal); if (item.str == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return -1; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } return cfgAddItem(pCfg, &item, name); } int32_t cfgAddDir(SConfig *pCfg, const char *name, const char *defaultVal, int8_t scope, int8_t dynScope) { SConfigItem item = {.dtype = CFG_DTYPE_DIR, .scope = scope, .dynScope = dynScope}; - if (cfgCheckAndSetDir(&item, defaultVal) != 0) { - return -1; - } - + TAOS_CHECK_RETURN(cfgCheckAndSetDir(&item, defaultVal)); return cfgAddItem(pCfg, &item, name); } int32_t cfgAddLocale(SConfig *pCfg, const char *name, const char *defaultVal, int8_t scope, int8_t dynScope) { SConfigItem item = {.dtype = CFG_DTYPE_LOCALE, .scope = scope, .dynScope = dynScope}; - if (cfgCheckAndSetConf(&item, defaultVal) != 0) { - return -1; - } - + TAOS_CHECK_RETURN(cfgCheckAndSetConf(&item, defaultVal)); return cfgAddItem(pCfg, &item, name); } int32_t cfgAddCharset(SConfig *pCfg, const char *name, const char *defaultVal, int8_t scope, int8_t dynScope) { SConfigItem item = {.dtype = CFG_DTYPE_CHARSET, .scope = scope, .dynScope = dynScope}; - if (cfgCheckAndSetConf(&item, defaultVal) != 0) { - return -1; - } - + TAOS_CHECK_RETURN(cfgCheckAndSetConf(&item, defaultVal)); return cfgAddItem(pCfg, &item, name); } int32_t cfgAddTimezone(SConfig *pCfg, const char *name, const char *defaultVal, int8_t scope, int8_t dynScope) { SConfigItem item = {.dtype = CFG_DTYPE_TIMEZONE, .scope = scope, .dynScope = dynScope}; - if (cfgCheckAndSetConf(&item, defaultVal) != 0) { - return -1; - } - + TAOS_CHECK_RETURN(cfgCheckAndSetConf(&item, defaultVal)); return cfgAddItem(pCfg, &item, name); } @@ -693,7 +655,7 @@ const char *cfgDtypeStr(ECfgDataType type) { } } -void cfgDumpItemValue(SConfigItem *pItem, char *buf, int32_t bufSize, int32_t *pLen) { +int32_t cfgDumpItemValue(SConfigItem *pItem, char *buf, int32_t bufSize, int32_t *pLen) { int32_t len = 0; switch (pItem->dtype) { case CFG_DTYPE_BOOL: @@ -719,14 +681,19 @@ void cfgDumpItemValue(SConfigItem *pItem, char *buf, int32_t bufSize, int32_t *p break; } + if (len < 0) { + TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); + } + if (len > bufSize) { len = bufSize; } *pLen = len; + TAOS_RETURN(TSDB_CODE_SUCCESS); } -void cfgDumpItemScope(SConfigItem *pItem, char *buf, int32_t bufSize, int32_t *pLen) { +int32_t cfgDumpItemScope(SConfigItem *pItem, char *buf, int32_t bufSize, int32_t *pLen) { int32_t len = 0; switch (pItem->scope) { case CFG_SCOPE_SERVER: @@ -740,11 +707,16 @@ void cfgDumpItemScope(SConfigItem *pItem, char *buf, int32_t bufSize, int32_t *p break; } + if (len < 0) { + TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); + } + if (len > bufSize) { len = bufSize; } *pLen = len; + TAOS_RETURN(TSDB_CODE_SUCCESS); } void cfgDumpCfgS3(SConfig *pCfg, bool tsc, bool dump) { @@ -919,14 +891,14 @@ int32_t cfgLoadFromEnvVar(SConfig *pConfig) { char **pEnv = environ; line[1023] = 0; - if (pEnv == NULL) return 0; + if (pEnv == NULL) TAOS_RETURN(TSDB_CODE_SUCCESS); while (*pEnv != NULL) { name = value = value2 = value3 = value4 = NULL; olen = vlen = vlen2 = vlen3 = vlen4 = 0; strncpy(line, *pEnv, sizeof(line) - 1); pEnv++; - taosEnvToCfg(line, line); + (void)taosEnvToCfg(line, line); paGetToken(line, &name, &olen); if (olen == 0) continue; @@ -943,21 +915,21 @@ int32_t cfgLoadFromEnvVar(SConfig *pConfig) { if (vlen3 != 0) { value3[vlen3] = 0; paGetToken(value3 + vlen3 + 1, &value4, &vlen4); - if(vlen4 != 0) value4[vlen4] = 0; + if (vlen4 != 0) value4[vlen4] = 0; } } code = cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_VAR, true); - if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; + if (TSDB_CODE_SUCCESS != code && TSDB_CODE_CFG_NOT_FOUND != code) break; if (strcasecmp(name, "dataDir") == 0) { code = cfgSetTfsItem(pConfig, name, value, value2, value3, value4, CFG_STYPE_ENV_VAR); - if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; + if (TSDB_CODE_SUCCESS != code && TSDB_CODE_CFG_NOT_FOUND != code) break; } } uInfo("load from env variables cfg success"); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } int32_t cfgLoadFromEnvCmd(SConfig *pConfig, const char **envCmd) { @@ -965,11 +937,11 @@ int32_t cfgLoadFromEnvCmd(SConfig *pConfig, const char **envCmd) { int32_t olen, vlen, vlen2, vlen3, vlen4; int32_t code = 0; int32_t index = 0; - if (envCmd == NULL) return 0; + if (envCmd == NULL) TAOS_RETURN(TSDB_CODE_SUCCESS); while (envCmd[index] != NULL) { strncpy(buf, envCmd[index], sizeof(buf) - 1); buf[sizeof(buf) - 1] = 0; - taosEnvToCfg(buf, buf); + (void)taosEnvToCfg(buf, buf); index++; name = value = value2 = value3 = value4 = NULL; @@ -990,21 +962,21 @@ int32_t cfgLoadFromEnvCmd(SConfig *pConfig, const char **envCmd) { if (vlen3 != 0) { value3[vlen3] = 0; paGetToken(value3 + vlen3 + 1, &value4, &vlen4); - if(vlen4 != 0) value4[vlen4] = 0; + if (vlen4 != 0) value4[vlen4] = 0; } } code = cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_CMD, true); - if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; + if (TSDB_CODE_SUCCESS != code && TSDB_CODE_CFG_NOT_FOUND != code) break; if (strcasecmp(name, "dataDir") == 0) { code = cfgSetTfsItem(pConfig, name, value, value2, value3, value4, CFG_STYPE_ENV_CMD); - if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; + if (TSDB_CODE_SUCCESS != code && TSDB_CODE_CFG_NOT_FOUND != code) break; } } uInfo("load from env cmd cfg success"); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *envFile) { @@ -1017,20 +989,19 @@ int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *envFile) { if (envFile != NULL && strlen(envFile) > 0) { if (!taosCheckExistFile(envFile)) { uError("failed to load env file:%s", envFile); - return -1; + TAOS_RETURN(TSDB_CODE_NOT_FOUND); } filepath = envFile; } else { if (!taosCheckExistFile(filepath)) { uInfo("env file:%s not load", filepath); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } } TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; + TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); } while (!taosEOFFile(pFile)) { @@ -1042,7 +1013,7 @@ int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *envFile) { break; } if (line[_bytes - 1] == '\n') line[_bytes - 1] = 0; - taosEnvToCfg(line, line); + (void)taosEnvToCfg(line, line); paGetToken(line, &name, &olen); if (olen == 0) continue; @@ -1059,23 +1030,23 @@ int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *envFile) { if (vlen3 != 0) { value3[vlen3] = 0; paGetToken(value3 + vlen3 + 1, &value4, &vlen4); - if(vlen4 != 0) value4[vlen4] = 0; + if (vlen4 != 0) value4[vlen4] = 0; } } code = cfgSetItem(pConfig, name, value, CFG_STYPE_ENV_FILE, true); - if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; + if (TSDB_CODE_SUCCESS != code && TSDB_CODE_CFG_NOT_FOUND != code) break; if (strcasecmp(name, "dataDir") == 0) { code = cfgSetTfsItem(pConfig, name, value, value2, value3, value4, CFG_STYPE_ENV_FILE); - if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; + if (TSDB_CODE_SUCCESS != code && TSDB_CODE_CFG_NOT_FOUND != code) break; } } taosCloseFile(&pFile); uInfo("load from env cfg file %s success", filepath); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) { @@ -1087,13 +1058,13 @@ int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) { TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ | TD_FILE_STREAM); if (pFile == NULL) { // success when the file does not exist + code = TAOS_SYSTEM_ERROR(errno); if (errno == ENOENT) { - terrno = TAOS_SYSTEM_ERROR(errno); - uInfo("failed to load from cfg file %s since %s, use default parameters", filepath, terrstr()); - return 0; + uInfo("failed to load from cfg file %s since %s, use default parameters", filepath, tstrerror(code)); + TAOS_RETURN(TSDB_CODE_SUCCESS); } else { - uError("failed to load from cfg file %s since %s", filepath, terrstr()); - return -1; + uError("failed to load from cfg file %s since %s", filepath, tstrerror(code)); + TAOS_RETURN(code); } } @@ -1134,7 +1105,7 @@ int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) { } code = cfgSetItem(pConfig, name, newValue, CFG_STYPE_CFG_FILE, true); - if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; + if (TSDB_CODE_SUCCESS != code && TSDB_CODE_CFG_NOT_FOUND != code) break; } else { paGetToken(value + vlen + 1, &value2, &vlen2); if (vlen2 != 0) { @@ -1148,12 +1119,12 @@ int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) { } code = cfgSetItem(pConfig, name, value, CFG_STYPE_CFG_FILE, true); - if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; + if (TSDB_CODE_SUCCESS != code && TSDB_CODE_CFG_NOT_FOUND != code) break; } if (strcasecmp(name, "dataDir") == 0) { code = cfgSetTfsItem(pConfig, name, value, value2, value3, value4, CFG_STYPE_CFG_FILE); - if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; + if (TSDB_CODE_SUCCESS != code && TSDB_CODE_CFG_NOT_FOUND != code) break; } size_t len = strlen(name); @@ -1161,18 +1132,18 @@ int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) { const size_t debugFlagLen = strlen(debugFlagStr); if (len >= debugFlagLen && strcasecmp(name + len - debugFlagLen, debugFlagStr) == 0) { code = cfgUpdateDebugFlagItem(pConfig, name, len == debugFlagLen); - if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; + if (TSDB_CODE_SUCCESS != code && TSDB_CODE_CFG_NOT_FOUND != code) break; } } taosCloseFile(&pFile); - if (code == 0 || (code != 0 && terrno == TSDB_CODE_CFG_NOT_FOUND)) { + if (TSDB_CODE_SUCCESS == code || TSDB_CODE_CFG_NOT_FOUND == code) { uInfo("load from cfg file %s success", filepath); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } else { - uError("failed to load from cfg file %s since %s", filepath, terrstr()); - return -1; + uError("failed to load from cfg file %s since %s", filepath, tstrerror(code)); + TAOS_RETURN(code); } } @@ -1247,46 +1218,52 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) { int32_t code = 0; if (url == NULL || strlen(url) == 0) { uInfo("apoll url not load"); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } char *p = strchr(url, ':'); if (p == NULL) { uError("fail to load apoll url: %s, unknown format", url); - return -1; + TAOS_RETURN(TSDB_CODE_INVALID_PARA); } p++; + SJson *pJson = NULL; if (strncmp(url, "jsonFile", 8) == 0) { char *filepath = p; if (!taosCheckExistFile(filepath)) { uError("failed to load json file:%s", filepath); - return -1; + TAOS_RETURN(TSDB_CODE_NOT_FOUND); } TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ); if (pFile == NULL) { - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; + TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); } size_t fileSize = taosLSeekFile(pFile, 0, SEEK_END); char *buf = taosMemoryMalloc(fileSize); + if (!buf) { + taosCloseFile(&pFile); + uError("load json file error: %s, failed to alloc memory", filepath); + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); + } + taosLSeekFile(pFile, 0, SEEK_SET); if (taosReadFile(pFile, buf, fileSize) <= 0) { taosCloseFile(&pFile); uError("load json file error: %s", filepath); taosMemoryFreeClear(buf); - return -1; + TAOS_RETURN(TSDB_CODE_INVALID_DATA_FMT); } taosCloseFile(&pFile); - SJson *pJson = tjsonParse(buf); + pJson = tjsonParse(buf); if (NULL == pJson) { const char *jsonParseError = tjsonGetError(); if (jsonParseError != NULL) { uError("load json file parse error: %s", jsonParseError); } taosMemoryFreeClear(buf); - return -1; + TAOS_RETURN(TSDB_CODE_INVALID_DATA_FMT); } taosMemoryFreeClear(buf); @@ -1295,13 +1272,17 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) { cJSON *item = tjsonGetArrayItem(pJson, i); if (item == NULL) break; char *itemName = NULL, *itemValueString = NULL; - tjsonGetObjectName(item, &itemName); - tjsonGetObjectName(item, &itemName); - tjsonGetObjectValueString(item, &itemValueString); + TAOS_CHECK_GOTO(tjsonGetObjectName(item, &itemName), NULL, _err_json); + TAOS_CHECK_GOTO(tjsonGetObjectValueString(item, &itemValueString), NULL, _err_json); if (itemValueString != NULL && itemName != NULL) { size_t itemNameLen = strlen(itemName); size_t itemValueStringLen = strlen(itemValueString); cfgLineBuf = taosMemoryMalloc(itemNameLen + itemValueStringLen + 2); + if (NULL == cfgLineBuf) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _err_json; + } + memcpy(cfgLineBuf, itemName, itemNameLen); cfgLineBuf[itemNameLen] = ' '; memcpy(&cfgLineBuf[itemNameLen + 1], itemValueString, itemValueStringLen); @@ -1327,11 +1308,11 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) { } code = cfgSetItem(pConfig, name, value, CFG_STYPE_APOLLO_URL, true); - if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; + if (TSDB_CODE_SUCCESS != code && TSDB_CODE_CFG_NOT_FOUND != code) break; if (strcasecmp(name, "dataDir") == 0) { code = cfgSetTfsItem(pConfig, name, value, value2, value3, value4, CFG_STYPE_APOLLO_URL); - if (code != 0 && terrno != TSDB_CODE_CFG_NOT_FOUND) break; + if (TSDB_CODE_SUCCESS != code && TSDB_CODE_CFG_NOT_FOUND != code) break; } } } @@ -1341,16 +1322,20 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) { // } else if (strncmp(url, "etcdUrl", 7) == 0) { } else { uError("Unsupported url: %s", url); - return -1; + TAOS_RETURN(TSDB_CODE_INVALID_PARA); } uInfo("load from apoll url not implemented yet"); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); + +_err_json: + tjsonDelete(pJson); + TAOS_RETURN(code); } int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char *apolloUrl) { int32_t index = 0; - if (envCmd == NULL) return 0; + if (envCmd == NULL) TAOS_RETURN(TSDB_CODE_SUCCESS); while (envCmd[index] != NULL) { if (strncmp(envCmd[index], "TAOS_APOLLO_URL", 14) == 0) { char *p = strchr(envCmd[index], '='); @@ -1362,7 +1347,7 @@ int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char *apolloUrl } memcpy(apolloUrl, p, TMIN(strlen(p) + 1, PATH_MAX)); uInfo("get apollo url from env cmd success"); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } } index++; @@ -1384,7 +1369,7 @@ int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char *apolloUrl } memcpy(apolloUrl, p, TMIN(strlen(p) + 1, PATH_MAX)); uInfo("get apollo url from env variables success, apolloUrl=%s", apolloUrl); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } } } @@ -1393,13 +1378,13 @@ int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char *apolloUrl if (envFile != NULL && strlen(envFile) > 0) { if (!taosCheckExistFile(envFile)) { uError("failed to load env file:%s", envFile); - return -1; + TAOS_RETURN(TSDB_CODE_NOT_FOUND); } filepath = envFile; } else { if (!taosCheckExistFile(filepath)) { uInfo("env file:%s not load", filepath); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } } int64_t _bytes; @@ -1422,7 +1407,7 @@ int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char *apolloUrl memcpy(apolloUrl, p, TMIN(strlen(p) + 1, PATH_MAX)); taosCloseFile(&pFile); uInfo("get apollo url from env file success"); - return 0; + TAOS_RETURN(TSDB_CODE_SUCCESS); } } } @@ -1430,7 +1415,7 @@ int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char *apolloUrl } uInfo("fail get apollo url from cmd env file"); - return -1; + TAOS_RETURN(TSDB_CODE_INVALID_PARA); } struct SConfigIter { @@ -1438,15 +1423,16 @@ struct SConfigIter { SConfig *pConf; }; -SConfigIter *cfgCreateIter(SConfig *pConf) { - SConfigIter* pIter = taosMemoryCalloc(1, sizeof(SConfigIter)); +int32_t cfgCreateIter(SConfig *pConf, SConfigIter **ppIter) { + SConfigIter *pIter = taosMemoryCalloc(1, sizeof(SConfigIter)); if (pIter == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } pIter->pConf = pConf; - return pIter; + + *ppIter = pIter; + TAOS_RETURN(TSDB_CODE_SUCCESS); } SConfigItem *cfgNextIter(SConfigIter* pIter) { @@ -1463,4 +1449,4 @@ void cfgDestroyIter(SConfigIter *pIter) { } taosMemoryFree(pIter); -} \ No newline at end of file +} diff --git a/source/util/test/cfgTest.cpp b/source/util/test/cfgTest.cpp index 9f8645b14c..33ababef10 100644 --- a/source/util/test/cfgTest.cpp +++ b/source/util/test/cfgTest.cpp @@ -51,7 +51,10 @@ TEST_F(CfgTest, 01_Str) { } TEST_F(CfgTest, 02_Basic) { - SConfig *pConfig = cfgInit(); + SConfig *pConfig = NULL; + int32_t code = cfgInit(&pConfig); + + ASSERT_EQ(code, TSDB_CODE_SUCCESS); ASSERT_NE(pConfig, nullptr); EXPECT_EQ(cfgAddBool(pConfig, "test_bool", 0, 0, 0), 0); @@ -66,7 +69,11 @@ TEST_F(CfgTest, 02_Basic) { int32_t size = cfgGetSize(pConfig); SConfigItem* pItem = NULL; - SConfigIter* pIter = cfgCreateIter(pConfig); + SConfigIter *pIter = NULL; + code = cfgCreateIter(pConfig, &pIter); + ASSERT_EQ(code, TSDB_CODE_SUCCESS); + ASSERT_NE(pIter, nullptr); + while((pItem = cfgNextIter(pIter)) != NULL) { switch (pItem->dtype) { case CFG_DTYPE_BOOL: From d908c1d7108bf6f20025873076e22cc5fca7dfec Mon Sep 17 00:00:00 2001 From: Shungang Li Date: Tue, 23 Jul 2024 16:39:52 +0800 Subject: [PATCH 4/5] feat: (errcode) more for ttime.c/geomFunc.c/geosWrapper.c --- include/common/ttime.h | 2 +- source/common/src/ttime.c | 158 +++++++++--------- source/common/test/commonTests.cpp | 3 +- source/libs/geometry/src/geomFunc.c | 217 +++++++++---------------- source/libs/geometry/src/geosWrapper.c | 114 +++++++------ 5 files changed, 213 insertions(+), 281 deletions(-) diff --git a/include/common/ttime.h b/include/common/ttime.h index 2d40bd93a6..cec5b15761 100644 --- a/include/common/ttime.h +++ b/include/common/ttime.h @@ -117,7 +117,7 @@ int32_t taosTs2Char(const char* format, SArray** formats, int64_t ts, int32_t pr int32_t taosChar2Ts(const char* format, SArray** formats, const char* tsStr, int64_t* ts, int32_t precision, char* errMsg, int32_t errMsgLen); -void TEST_ts2char(const char* format, int64_t ts, int32_t precision, char* out, int32_t outLen); +int32_t TEST_ts2char(const char* format, int64_t ts, int32_t precision, char* out, int32_t outLen); int32_t TEST_char2ts(const char* format, int64_t* ts, int32_t precision, const char* tsStr); /// @brief get offset seconds from zero timezone to input timezone diff --git a/source/common/src/ttime.c b/source/common/src/ttime.c index 4fca564804..d3207a1912 100644 --- a/source/common/src/ttime.c +++ b/source/common/src/ttime.c @@ -96,8 +96,6 @@ char* forwardToTimeStringEnd(char* str) { } int32_t parseFraction(char* str, char** end, int32_t timePrec, int64_t* pFraction) { - int32_t code = TSDB_CODE_SUCCESS; - int32_t i = 0; int64_t fraction = 0; @@ -147,8 +145,6 @@ int32_t parseFraction(char* str, char** end, int32_t timePrec, int64_t* pFractio } int32_t parseTimezone(char* str, int64_t* tzOffset) { - int32_t code = TSDB_CODE_SUCCESS; - int64_t hour = 0; int32_t i = 0; @@ -224,8 +220,6 @@ int32_t offsetOfTimezone(char* tzStr, int64_t* offset) { * 2013-04-12T15:52:01.123+0800 */ int32_t parseTimeWithTz(const char* timestr, int64_t* time, int32_t timePrec, char delim) { - int32_t code = TSDB_CODE_SUCCESS; - int64_t factor = TSDB_TICK_PER_SECOND(timePrec); int64_t tzOffset = 0; @@ -315,8 +309,6 @@ static FORCE_INLINE bool validateTm(struct tm* pTm) { } int32_t parseLocaltime(char* timestr, int32_t len, int64_t* utime, int32_t timePrec, char delim) { - int32_t code = TSDB_CODE_SUCCESS; - *utime = 0; struct tm tm = {0}; @@ -358,8 +350,6 @@ int32_t parseLocaltime(char* timestr, int32_t len, int64_t* utime, int32_t timeP } int32_t parseLocaltimeDst(char* timestr, int32_t len, int64_t* utime, int32_t timePrec, char delim) { - int32_t code = TSDB_CODE_SUCCESS; - *utime = 0; struct tm tm = {0}; tm.tm_isdst = -1; @@ -484,8 +474,6 @@ int64_t convertTimePrecision(int64_t utime, int32_t fromPrecision, int32_t toPre // !!!!notice: double lose precison if time is too large, for example: 1626006833631000000*1.0 = double = // 1626006833631000064 int32_t convertTimeFromPrecisionToUnit(int64_t time, int32_t fromPrecision, char toUnit, int64_t* pRes) { - int32_t code = TSDB_CODE_SUCCESS; - if (fromPrecision != TSDB_TIME_PRECISION_MILLI && fromPrecision != TSDB_TIME_PRECISION_MICRO && fromPrecision != TSDB_TIME_PRECISION_NANO) { TAOS_RETURN(TSDB_CODE_INVALID_PARA); @@ -559,13 +547,14 @@ int32_t convertTimeFromPrecisionToUnit(int64_t time, int32_t fromPrecision, char } int32_t convertStringToTimestamp(int16_t type, char* inputData, int64_t timePrec, int64_t* timeVal) { - int32_t code = TSDB_CODE_SUCCESS; - int32_t charLen = varDataLen(inputData); char* newColData; if (type == TSDB_DATA_TYPE_BINARY || type == TSDB_DATA_TYPE_VARBINARY) { newColData = taosMemoryCalloc(1, charLen + 1); - memcpy(newColData, varDataVal(inputData), charLen); + if (NULL == newColData) { + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); + } + (void)memcpy(newColData, varDataVal(inputData), charLen); int32_t ret = taosParseTime(newColData, timeVal, charLen, (int32_t)timePrec, tsDaylight); if (ret != TSDB_CODE_SUCCESS) { taosMemoryFree(newColData); @@ -574,6 +563,9 @@ int32_t convertStringToTimestamp(int16_t type, char* inputData, int64_t timePrec taosMemoryFree(newColData); } else if (type == TSDB_DATA_TYPE_NCHAR) { newColData = taosMemoryCalloc(1, charLen + TSDB_NCHAR_SIZE); + if (NULL == newColData) { + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); + } int len = taosUcs4ToMbs((TdUcs4*)varDataVal(inputData), charLen, newColData); if (len < 0) { taosMemoryFree(newColData); @@ -593,8 +585,6 @@ int32_t convertStringToTimestamp(int16_t type, char* inputData, int64_t timePrec } int32_t getDuration(int64_t val, char unit, int64_t* result, int32_t timePrecision) { - int32_t code = TSDB_CODE_SUCCESS; - switch (unit) { case 's': if (val > INT64_MAX / MILLISECOND_PER_SECOND) { @@ -658,8 +648,6 @@ int32_t getDuration(int64_t val, char unit, int64_t* result, int32_t timePrecisi */ int32_t parseAbsoluteDuration(const char* token, int32_t tokenlen, int64_t* duration, char* unit, int32_t timePrecision) { - int32_t code = TSDB_CODE_SUCCESS; - errno = 0; char* endPtr = NULL; @@ -680,8 +668,6 @@ int32_t parseAbsoluteDuration(const char* token, int32_t tokenlen, int64_t* dura int32_t parseNatualDuration(const char* token, int32_t tokenLen, int64_t* duration, char* unit, int32_t timePrecision, bool negativeAllow) { - int32_t code = TSDB_CODE_SUCCESS; - errno = 0; /* get the basic numeric value */ @@ -718,7 +704,7 @@ int64_t taosTimeAdd(int64_t t, int64_t duration, char unit, int32_t precision) { struct tm tm; time_t tt = (time_t)(t / TSDB_TICK_PER_SECOND(precision)); - taosLocalTime(&tt, &tm, NULL); + (void)taosLocalTime(&tt, &tm, NULL); int32_t mon = tm.tm_year * 12 + tm.tm_mon + (int32_t)numOfMonth; tm.tm_year = mon / 12; tm.tm_mon = mon % 12; @@ -779,11 +765,11 @@ int32_t taosTimeCountIntervalForFill(int64_t skey, int64_t ekey, int64_t interva struct tm tm; time_t t = (time_t)skey; - taosLocalTime(&t, &tm, NULL); + (void)taosLocalTime(&t, &tm, NULL); int32_t smon = tm.tm_year * 12 + tm.tm_mon; t = (time_t)ekey; - taosLocalTime(&t, &tm, NULL); + (void)taosLocalTime(&t, &tm, NULL); int32_t emon = tm.tm_year * 12 + tm.tm_mon; if (unit == 'y') { @@ -808,7 +794,7 @@ int64_t taosTimeTruncate(int64_t ts, const SInterval* pInterval) { start /= (int64_t)(TSDB_TICK_PER_SECOND(precision)); struct tm tm; time_t tt = (time_t)start; - taosLocalTime(&tt, &tm, NULL); + (void)taosLocalTime(&tt, &tm, NULL); tm.tm_sec = 0; tm.tm_min = 0; tm.tm_hour = 0; @@ -978,8 +964,6 @@ const char* fmtts(int64_t ts) { } int32_t taosFormatUtcTime(char* buf, int32_t bufLen, int64_t t, int32_t precision) { - int32_t code = TSDB_CODE_SUCCESS; - char ts[40] = {0}; struct tm ptm; @@ -1018,7 +1002,7 @@ int32_t taosFormatUtcTime(char* buf, int32_t bufLen, int64_t t, int32_t precisio TAOS_RETURN(TSDB_CODE_INVALID_PARA); } - if (taosLocalTime(", &ptm, buf) == NULL) { + if (NULL == taosLocalTime(", &ptm, buf)) { TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); } int32_t length = (int32_t)strftime(ts, 40, "%Y-%m-%dT%H:%M:%S", &ptm); @@ -1032,7 +1016,9 @@ int32_t taosFormatUtcTime(char* buf, int32_t bufLen, int64_t t, int32_t precisio int32_t taosTs2Tm(int64_t ts, int32_t precision, struct STm* tm) { tm->fsec = ts % TICK_PER_SECOND[precision] * (TICK_PER_SECOND[TSDB_TIME_PRECISION_NANO] / TICK_PER_SECOND[precision]); time_t t = ts / TICK_PER_SECOND[precision]; - taosLocalTime(&t, &tm->tm, NULL); + if (NULL == taosLocalTime(&t, &tm->tm, NULL)) { + TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); + } return TSDB_CODE_SUCCESS; } @@ -1344,7 +1330,7 @@ static int32_t tm2char(const SArray* formats, const struct STm* tm, char* s, int TSFormatNode* format = taosArrayGet(formats, i); if (format->type != TS_FORMAT_NODE_TYPE_KEYWORD) { if (s - start + format->len + 1 > outLen) break; - strncpy(s, format->c, format->len); + (void)strncpy(s, format->c, format->len); s += format->len; continue; } @@ -1353,37 +1339,37 @@ static int32_t tm2char(const SArray* formats, const struct STm* tm, char* s, int switch (format->key->id) { case TSFKW_AM: case TSFKW_PM: - sprintf(s, tm->tm.tm_hour % 24 >= 12 ? "PM" : "AM"); + (void)sprintf(s, tm->tm.tm_hour % 24 >= 12 ? "PM" : "AM"); s += 2; break; case TSFKW_A_M: case TSFKW_P_M: - sprintf(s, tm->tm.tm_hour % 24 >= 12 ? "P.M." : "A.M."); + (void)sprintf(s, tm->tm.tm_hour % 24 >= 12 ? "P.M." : "A.M."); s += 4; break; case TSFKW_am: case TSFKW_pm: - sprintf(s, tm->tm.tm_hour % 24 >= 12 ? "pm" : "am"); + (void)sprintf(s, tm->tm.tm_hour % 24 >= 12 ? "pm" : "am"); s += 2; break; case TSFKW_a_m: case TSFKW_p_m: - sprintf(s, tm->tm.tm_hour % 24 >= 12 ? "p.m." : "a.m."); + (void)sprintf(s, tm->tm.tm_hour % 24 >= 12 ? "p.m." : "a.m."); s += 4; break; case TSFKW_DDD: #ifdef WINDOWS return TSDB_CODE_FUNC_TO_CHAR_NOT_SUPPORTED; #endif - sprintf(s, "%03d", tm->tm.tm_yday + 1); + (void)sprintf(s, "%03d", tm->tm.tm_yday + 1); s += strlen(s); break; case TSFKW_DD: - sprintf(s, "%02d", tm->tm.tm_mday); + (void)sprintf(s, "%02d", tm->tm.tm_mday); s += 2; break; case TSFKW_D: - sprintf(s, "%d", tm->tm.tm_wday + 1); + (void)sprintf(s, "%d", tm->tm.tm_wday + 1); s += 1; break; case TSFKW_DAY: { @@ -1391,20 +1377,20 @@ static int32_t tm2char(const SArray* formats, const struct STm* tm, char* s, int const char* wd = weekDays[tm->tm.tm_wday]; char buf[10] = {0}; for (int32_t i = 0; i < strlen(wd); ++i) buf[i] = toupper(wd[i]); - sprintf(s, "%-9s", buf); + (void)sprintf(s, "%-9s", buf); s += strlen(s); break; } case TSFKW_Day: // Monday, TuesDay... - sprintf(s, "%-9s", weekDays[tm->tm.tm_wday]); + (void)sprintf(s, "%-9s", weekDays[tm->tm.tm_wday]); s += strlen(s); break; case TSFKW_day: { const char* wd = weekDays[tm->tm.tm_wday]; char buf[10] = {0}; for (int32_t i = 0; i < strlen(wd); ++i) buf[i] = tolower(wd[i]); - sprintf(s, "%-9s", buf); + (void)sprintf(s, "%-9s", buf); s += strlen(s); break; } @@ -1413,13 +1399,13 @@ static int32_t tm2char(const SArray* formats, const struct STm* tm, char* s, int const char* wd = shortWeekDays[tm->tm.tm_wday]; char buf[8] = {0}; for (int32_t i = 0; i < strlen(wd); ++i) buf[i] = toupper(wd[i]); - sprintf(s, "%3s", buf); + (void)sprintf(s, "%3s", buf); s += 3; break; } case TSFKW_Dy: // Mon, Tue - sprintf(s, "%3s", shortWeekDays[tm->tm.tm_wday]); + (void)sprintf(s, "%3s", shortWeekDays[tm->tm.tm_wday]); s += 3; break; case TSFKW_dy: { @@ -1427,33 +1413,33 @@ static int32_t tm2char(const SArray* formats, const struct STm* tm, char* s, int const char* wd = shortWeekDays[tm->tm.tm_wday]; char buf[8] = {0}; for (int32_t i = 0; i < strlen(wd); ++i) buf[i] = tolower(wd[i]); - sprintf(s, "%3s", buf); + (void)sprintf(s, "%3s", buf); s += 3; break; } case TSFKW_HH24: - sprintf(s, "%02d", tm->tm.tm_hour); + (void)sprintf(s, "%02d", tm->tm.tm_hour); s += 2; break; case TSFKW_HH: case TSFKW_HH12: // 0 or 12 o'clock in 24H coresponds to 12 o'clock (AM/PM) in 12H - sprintf(s, "%02d", tm->tm.tm_hour % 12 == 0 ? 12 : tm->tm.tm_hour % 12); + (void)sprintf(s, "%02d", tm->tm.tm_hour % 12 == 0 ? 12 : tm->tm.tm_hour % 12); s += 2; break; case TSFKW_MI: - sprintf(s, "%02d", tm->tm.tm_min); + (void)sprintf(s, "%02d", tm->tm.tm_min); s += 2; break; case TSFKW_MM: - sprintf(s, "%02d", tm->tm.tm_mon + 1); + (void)sprintf(s, "%02d", tm->tm.tm_mon + 1); s += 2; break; case TSFKW_MONTH: { const char* mon = fullMonths[tm->tm.tm_mon]; char buf[10] = {0}; for (int32_t i = 0; i < strlen(mon); ++i) buf[i] = toupper(mon[i]); - sprintf(s, "%-9s", buf); + (void)sprintf(s, "%-9s", buf); s += strlen(s); break; } @@ -1461,44 +1447,44 @@ static int32_t tm2char(const SArray* formats, const struct STm* tm, char* s, int const char* mon = months[tm->tm.tm_mon]; char buf[10] = {0}; for (int32_t i = 0; i < strlen(mon); ++i) buf[i] = toupper(mon[i]); - sprintf(s, "%s", buf); + (void)sprintf(s, "%s", buf); s += strlen(s); break; } case TSFKW_Month: - sprintf(s, "%-9s", fullMonths[tm->tm.tm_mon]); + (void)sprintf(s, "%-9s", fullMonths[tm->tm.tm_mon]); s += strlen(s); break; case TSFKW_month: { const char* mon = fullMonths[tm->tm.tm_mon]; char buf[10] = {0}; for (int32_t i = 0; i < strlen(mon); ++i) buf[i] = tolower(mon[i]); - sprintf(s, "%-9s", buf); + (void)sprintf(s, "%-9s", buf); s += strlen(s); break; } case TSFKW_Mon: - sprintf(s, "%s", months[tm->tm.tm_mon]); + (void)sprintf(s, "%s", months[tm->tm.tm_mon]); s += strlen(s); break; case TSFKW_mon: { const char* mon = months[tm->tm.tm_mon]; char buf[10] = {0}; for (int32_t i = 0; i < strlen(mon); ++i) buf[i] = tolower(mon[i]); - sprintf(s, "%s", buf); + (void)sprintf(s, "%s", buf); s += strlen(s); break; } case TSFKW_SS: - sprintf(s, "%02d", tm->tm.tm_sec); + (void)sprintf(s, "%02d", tm->tm.tm_sec); s += 2; break; case TSFKW_MS: - sprintf(s, "%03" PRId64, tm->fsec / 1000000L); + (void)sprintf(s, "%03" PRId64, tm->fsec / 1000000L); s += 3; break; case TSFKW_US: - sprintf(s, "%06" PRId64, tm->fsec / 1000L); + (void)sprintf(s, "%06" PRId64, tm->fsec / 1000L); s += 6; break; case TSFKW_NS: @@ -1506,23 +1492,23 @@ static int32_t tm2char(const SArray* formats, const struct STm* tm, char* s, int s += 9; break; case TSFKW_TZH: - sprintf(s, "%s%02d", tsTimezone < 0 ? "-" : "+", tsTimezone); + (void)sprintf(s, "%s%02d", tsTimezone < 0 ? "-" : "+", tsTimezone); s += strlen(s); break; case TSFKW_YYYY: - sprintf(s, "%04d", tm->tm.tm_year + 1900); + (void)sprintf(s, "%04d", tm->tm.tm_year + 1900); s += strlen(s); break; case TSFKW_YYY: - sprintf(s, "%03d", (tm->tm.tm_year + 1900) % 1000); + (void)sprintf(s, "%03d", (tm->tm.tm_year + 1900) % 1000); s += strlen(s); break; case TSFKW_YY: - sprintf(s, "%02d", (tm->tm.tm_year + 1900) % 100); + (void)sprintf(s, "%02d", (tm->tm.tm_year + 1900) % 100); s += strlen(s); break; case TSFKW_Y: - sprintf(s, "%01d", (tm->tm.tm_year + 1900) % 10); + (void)sprintf(s, "%01d", (tm->tm.tm_year + 1900) % 10); s += strlen(s); break; default: @@ -1557,7 +1543,7 @@ static const char* tsFormatStr2Int32(int32_t* dest, const char* str, int32_t len s = last; } else { char buf[16] = {0}; - strncpy(buf, s, len); + (void)strncpy(buf, s, len); int32_t copiedLen = strlen(buf); if (copiedLen < len) { if (!needMoreDigit) { @@ -1936,10 +1922,13 @@ static int32_t char2ts(const char* s, SArray* formats, int64_t* ts, int32_t prec int32_t taosTs2Char(const char* format, SArray** formats, int64_t ts, int32_t precision, char* out, int32_t outLen) { if (!*formats) { *formats = taosArrayInit(8, sizeof(TSFormatNode)); + if (!*formats){ + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); + } parseTsFormat(format, *formats); } struct STm tm; - taosTs2Tm(ts, precision, &tm); + TAOS_CHECK_RETURN(taosTs2Tm(ts, precision, &tm)); return tm2char(*formats, &tm, out, outLen); } @@ -1949,6 +1938,9 @@ int32_t taosChar2Ts(const char* format, SArray** formats, const char* tsStr, int int32_t fErrIdx; if (!*formats) { *formats = taosArrayInit(4, sizeof(TSFormatNode)); + if (!*formats) { + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); + } parseTsFormat(format, *formats); } int32_t code = char2ts(tsStr, *formats, ts, precision, &sErrPos, &fErrIdx); @@ -1964,16 +1956,24 @@ int32_t taosChar2Ts(const char* format, SArray** formats, const char* tsStr, int snprintf(errMsg, errMsgLen, "timestamp format not supported"); code = TSDB_CODE_FUNC_TO_TIMESTAMP_FAILED_NOT_SUPPORTED; } - return code; + TAOS_RETURN(code); } -void TEST_ts2char(const char* format, int64_t ts, int32_t precision, char* out, int32_t outLen) { +int32_t TEST_ts2char(const char* format, int64_t ts, int32_t precision, char* out, int32_t outLen) { + int32_t code = TSDB_CODE_SUCCESS; + SArray* formats = taosArrayInit(4, sizeof(TSFormatNode)); + if (!formats) { + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); + } parseTsFormat(format, formats); struct STm tm; - taosTs2Tm(ts, precision, &tm); - tm2char(formats, &tm, out, outLen); + TAOS_CHECK_GOTO(taosTs2Tm(ts, precision, &tm), NULL, _exit); + TAOS_CHECK_GOTO(tm2char(formats, &tm, out, outLen), NULL, _exit); + +_exit: taosArrayDestroy(formats); + TAOS_RETURN(TSDB_CODE_SUCCESS); } int32_t TEST_char2ts(const char* format, int64_t* ts, int32_t precision, const char* tsStr) { @@ -1997,17 +1997,19 @@ static int8_t UNIT_INDEX[26] = {/*a*/ 2, 0, -1, 6, -1, -1, -1, #define GET_UNIT_INDEX(idx) UNIT_INDEX[(idx) - 97] -static int64_t UNIT_MATRIX[10][11] = {/* ns, us, ms, s, min, h, d, w, month, y*/ - /*ns*/ {1, 1000, 0}, - /*us*/ {1000, 1, 1000, 0}, - /*ms*/ {0, 1000, 1, 1000, 0}, - /*s*/ {0, 0, 1000, 1, 60, 0}, - /*min*/ {0, 0, 0, 60, 1, 60, 0}, - /*h*/ {0, 0, 0, 0, 60, 1, 1, 0}, - /*d*/ {0, 0, 0, 0, 0, 24, 1, 7, 1, 0}, - /*w*/ {0, 0, 0, 0, 0, 0, 7, 1, -1, 0}, - /*mon*/ {0, 0, 0, 0, 0, 0, 0, 0, 1, 12, 0}, - /*y*/ {0, 0, 0, 0, 0, 0, 0, 0, 12, 1, 0}}; +// clang-format off +static int64_t UNIT_MATRIX[10][11] = { /* ns, us, ms, s, min, h, d, w, month, y*/ + /*ns*/ { 1, 1000, 0}, + /*us*/ {1000, 1, 1000, 0}, + /*ms*/ { 0, 1000, 1, 1000, 0}, + /*s*/ { 0, 0, 1000, 1, 60, 0}, + /*min*/ { 0, 0, 0, 60, 1, 60, 0}, + /*h*/ { 0, 0, 0, 0, 60, 1, 1, 0}, + /*d*/ { 0, 0, 0, 0, 0, 24, 1, 7, 1, 0}, + /*w*/ { 0, 0, 0, 0, 0, 0, 7, 1, -1, 0}, + /*mon*/ { 0, 0, 0, 0, 0, 0, 0, 0, 1, 12, 0}, + /*y*/ { 0, 0, 0, 0, 0, 0, 0, 0, 12, 1, 0}}; +// clang-format on static bool recursiveTsmaCheckRecursive(int64_t baseInterval, int8_t baseIdx, int64_t interval, int8_t idx, bool checkEq) { diff --git a/source/common/test/commonTests.cpp b/source/common/test/commonTests.cpp index 360d1ed31a..b539d6731f 100644 --- a/source/common/test/commonTests.cpp +++ b/source/common/test/commonTests.cpp @@ -465,7 +465,8 @@ TEST(timeTest, timestamp2tm) { void test_ts2char(int64_t ts, const char* format, int32_t precison, const char* expected) { char buf[256] = {0}; - TEST_ts2char(format, ts, precison, buf, 256); + int32_t code = TEST_ts2char(format, ts, precison, buf, 256); + ASSERT_EQ(code, 0); printf("ts: %ld format: %s res: [%s], expected: [%s]\n", ts, format, buf, expected); ASSERT_STREQ(expected, buf); } diff --git a/source/libs/geometry/src/geomFunc.c b/source/libs/geometry/src/geomFunc.c index 601588571e..343e6ec4ce 100644 --- a/source/libs/geometry/src/geomFunc.c +++ b/source/libs/geometry/src/geomFunc.c @@ -21,8 +21,8 @@ #include "sclInt.h" #include "sclvector.h" -typedef int32_t (*_geomDoRelationFunc_t)(const GEOSGeometry *geom1, const GEOSPreparedGeometry *preparedGeom1, const GEOSGeometry *geom2, - bool swapped, char *res); +typedef int32_t (*_geomDoRelationFunc_t)(const GEOSGeometry *geom1, const GEOSPreparedGeometry *preparedGeom1, + const GEOSGeometry *geom2, bool swapped, char *res); typedef int32_t (*_geomInitCtxFunc_t)(); typedef int32_t (*_geomExecuteOneParamFunc_t)(SColumnInfoData *pInputData, int32_t i, SColumnInfoData *pOutputData); @@ -35,7 +35,7 @@ int32_t doMakePointFunc(double x, double y, unsigned char **output) { int32_t code = TSDB_CODE_FAILED; unsigned char *outputGeom = NULL; - size_t size = 0; + size_t size = 0; code = doMakePoint(x, y, &outputGeom, &size); if (code != TSDB_CODE_SUCCESS) { goto _exit; @@ -47,7 +47,7 @@ int32_t doMakePointFunc(double x, double y, unsigned char **output) { goto _exit; } - memcpy(varDataVal(*output), outputGeom, size); + (void)memcpy(varDataVal(*output), outputGeom, size); varDataSetLen(*output, size); code = TSDB_CODE_SUCCESS; @@ -62,14 +62,14 @@ _exit: int32_t doGeomFromTextFunc(const char *input, unsigned char **output) { int32_t code = TSDB_CODE_FAILED; - if ((varDataLen(input)) == 0) { //empty value + if ((varDataLen(input)) == 0) { // empty value *output = NULL; return TSDB_CODE_SUCCESS; } - char *inputGeom = NULL; + char *inputGeom = NULL; unsigned char *outputGeom = NULL; - size_t size = 0; + size_t size = 0; // make a zero ending string inputGeom = taosMemoryCalloc(1, varDataLen(input) + 1); @@ -77,12 +77,9 @@ int32_t doGeomFromTextFunc(const char *input, unsigned char **output) { code = TSDB_CODE_OUT_OF_MEMORY; goto _exit; } - memcpy(inputGeom, varDataVal(input), varDataLen(input)); + (void)memcpy(inputGeom, varDataVal(input), varDataLen(input)); - code = doGeomFromText(inputGeom, &outputGeom, &size); - if (code != TSDB_CODE_SUCCESS) { - goto _exit; - } + TAOS_CHECK_GOTO(doGeomFromText(inputGeom, &outputGeom, &size), NULL, _exit); *output = taosMemoryCalloc(1, size + VARSTR_HEADER_SIZE); if (*output == NULL) { @@ -90,7 +87,7 @@ int32_t doGeomFromTextFunc(const char *input, unsigned char **output) { goto _exit; } - memcpy(varDataVal(*output), outputGeom, size); + (void)memcpy(varDataVal(*output), outputGeom, size); varDataSetLen(*output, size); code = TSDB_CODE_SUCCESS; @@ -106,16 +103,13 @@ _exit: int32_t doAsTextFunc(unsigned char *input, char **output) { int32_t code = TSDB_CODE_FAILED; - if ((varDataLen(input)) == 0) { //empty value + if ((varDataLen(input)) == 0) { // empty value *output = NULL; return TSDB_CODE_SUCCESS; } char *outputWKT = NULL; - code = doAsText(varDataVal(input), varDataLen(input), &outputWKT); - if (code != TSDB_CODE_SUCCESS) { - goto _exit; - } + TAOS_CHECK_GOTO(doAsText(varDataVal(input), varDataLen(input), &outputWKT), NULL, _exit); size_t size = strlen(outputWKT); *output = taosMemoryCalloc(1, size + VARSTR_HEADER_SIZE); @@ -124,7 +118,7 @@ int32_t doAsTextFunc(unsigned char *input, char **output) { goto _exit; } - memcpy(varDataVal(*output), outputWKT, size); + (void)memcpy(varDataVal(*output), outputWKT, size); varDataSetLen(*output, size); code = TSDB_CODE_SUCCESS; @@ -139,28 +133,17 @@ int32_t executeMakePointFunc(SColumnInfoData *pInputData[], int32_t iLeft, int32 int32_t code = TSDB_CODE_FAILED; _getDoubleValue_fn_t getDoubleValueFn[2]; - getDoubleValueFn[0]= getVectorDoubleValueFn(pInputData[0]->info.type); - getDoubleValueFn[1]= getVectorDoubleValueFn(pInputData[1]->info.type); + getDoubleValueFn[0] = getVectorDoubleValueFn(pInputData[0]->info.type); + getDoubleValueFn[1] = getVectorDoubleValueFn(pInputData[1]->info.type); unsigned char *output = NULL; - double leftRes = 0; - double rightRes = 0; - code = getDoubleValueFn[0](pInputData[0]->pData, iLeft, &leftRes); - if (TSDB_CODE_SUCCESS != code) { - goto _exit; - } - code = getDoubleValueFn[1](pInputData[1]->pData, iRight, &rightRes); - if (TSDB_CODE_SUCCESS != code) { - goto _exit; - } - code = doMakePointFunc(leftRes, rightRes, &output); - if (TSDB_CODE_SUCCESS != code) { - goto _exit; - } - code = colDataSetVal(pOutputData, TMAX(iLeft, iRight), output, (output == NULL)); - if (TSDB_CODE_SUCCESS != code) { - goto _exit; - } + double leftRes = 0; + double rightRes = 0; + + TAOS_CHECK_GOTO(getDoubleValueFn[0](pInputData[0]->pData, iLeft, &leftRes), NULL, _exit); + TAOS_CHECK_GOTO(getDoubleValueFn[1](pInputData[1]->pData, iRight, &rightRes), NULL, _exit); + TAOS_CHECK_GOTO(doMakePointFunc(leftRes, rightRes, &output), NULL, _exit); + TAOS_CHECK_GOTO(colDataSetVal(pOutputData, TMAX(iLeft, iRight), output, (output == NULL)), NULL, _exit); _exit: if (output) { @@ -173,14 +156,11 @@ _exit: int32_t executeGeomFromTextFunc(SColumnInfoData *pInputData, int32_t i, SColumnInfoData *pOutputData) { int32_t code = TSDB_CODE_FAILED; - char *input = colDataGetData(pInputData, i); + char *input = colDataGetData(pInputData, i); unsigned char *output = NULL; - code = doGeomFromTextFunc(input, &output); - if (code != TSDB_CODE_SUCCESS) { - goto _exit; - } - code = colDataSetVal(pOutputData, i, output, (output == NULL)); + TAOS_CHECK_GOTO(doGeomFromTextFunc(input, &output), NULL, _exit); + TAOS_CHECK_GOTO(colDataSetVal(pOutputData, i, output, (output == NULL)), NULL, _exit); _exit: if (output) { @@ -194,13 +174,10 @@ int32_t executeAsTextFunc(SColumnInfoData *pInputData, int32_t i, SColumnInfoDat int32_t code = TSDB_CODE_FAILED; unsigned char *input = colDataGetData(pInputData, i); - char *output = NULL; - code = doAsTextFunc(input, &output); - if (code != TSDB_CODE_SUCCESS) { - goto _exit; - } + char *output = NULL; - code = colDataSetVal(pOutputData, i, output, (output == NULL)); + TAOS_CHECK_GOTO(doAsTextFunc(input, &output), NULL, _exit); + TAOS_CHECK_GOTO(colDataSetVal(pOutputData, i, output, (output == NULL)), NULL, _exit); _exit: if (output) { @@ -211,36 +188,24 @@ _exit: } int32_t executeRelationFunc(const GEOSGeometry *geom1, const GEOSPreparedGeometry *preparedGeom1, - const GEOSGeometry *geom2, int32_t i, - bool swapped, SColumnInfoData *pOutputData, + const GEOSGeometry *geom2, int32_t i, bool swapped, SColumnInfoData *pOutputData, _geomDoRelationFunc_t doRelationFn) { - int32_t code = TSDB_CODE_FAILED; char res = 0; - if (!geom1 || !geom2) { //if empty input value + if (!geom1 || !geom2) { // if empty input value res = -1; - code = TSDB_CODE_SUCCESS; - } - else { - code = doRelationFn(geom1, preparedGeom1, geom2, swapped, &res); - if (code != TSDB_CODE_SUCCESS) { - return code; - } + } else { + TAOS_CHECK_RETURN(doRelationFn(geom1, preparedGeom1, geom2, swapped, &res)); } - code = colDataSetVal(pOutputData, i, &res, (res==-1)); - - return code; + return colDataSetVal(pOutputData, i, &res, (res == -1)); } -int32_t geomOneParamFunction(SScalarParam *pInput, SScalarParam *pOutput, - _geomInitCtxFunc_t initCtxFn, _geomExecuteOneParamFunc_t executeOneParamFn) { +int32_t geomOneParamFunction(SScalarParam *pInput, SScalarParam *pOutput, _geomInitCtxFunc_t initCtxFn, + _geomExecuteOneParamFunc_t executeOneParamFn) { int32_t code = TSDB_CODE_FAILED; - code = initCtxFn(); - if (code != TSDB_CODE_SUCCESS) { - return code; - } + TAOS_CHECK_RETURN(initCtxFn()); SColumnInfoData *pInputData = pInput->columnData; SColumnInfoData *pOutputData = pOutput->columnData; @@ -249,8 +214,7 @@ int32_t geomOneParamFunction(SScalarParam *pInput, SScalarParam *pOutput, if (IS_NULL_TYPE(GET_PARAM_TYPE(pInput))) { colDataSetNNULL(pOutputData, 0, pInput->numOfRows); code = TSDB_CODE_SUCCESS; - } - else { + } else { for (int32_t i = 0; i < pInput->numOfRows; ++i) { if (colDataIsNull_s(pInputData, i)) { colDataSetNULL(pOutputData, i); @@ -258,44 +222,36 @@ int32_t geomOneParamFunction(SScalarParam *pInput, SScalarParam *pOutput, continue; } - code = executeOneParamFn(pInputData, i, pOutputData); - if (code != TSDB_CODE_SUCCESS) { - return code; - } + TAOS_CHECK_RETURN(executeOneParamFn(pInputData, i, pOutputData)); } } - return code; + TAOS_RETURN(code); } -int32_t geomTwoParamsFunction(SScalarParam *pInput, SScalarParam *pOutput, - _geomInitCtxFunc_t initCtxFn, _geomExecuteTwoParamsFunc_t executeTwoParamsFn) { +int32_t geomTwoParamsFunction(SScalarParam *pInput, SScalarParam *pOutput, _geomInitCtxFunc_t initCtxFn, + _geomExecuteTwoParamsFunc_t executeTwoParamsFn) { int32_t code = TSDB_CODE_FAILED; - code = initCtxFn(); - if (code != TSDB_CODE_SUCCESS) { - return code; - } + TAOS_CHECK_RETURN(initCtxFn()); SColumnInfoData *pInputData[2]; SColumnInfoData *pOutputData = pOutput->columnData; pInputData[0] = pInput[0].columnData; pInputData[1] = pInput[1].columnData; - bool hasNullType = (IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[0])) || - IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[1]))); - bool isConstantLeft = (pInput[0].numOfRows == 1); - bool isConstantRight = (pInput[1].numOfRows == 1); + bool hasNullType = (IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[0])) || IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[1]))); + bool isConstantLeft = (pInput[0].numOfRows == 1); + bool isConstantRight = (pInput[1].numOfRows == 1); int32_t numOfRows = TMAX(pInput[0].numOfRows, pInput[1].numOfRows); pOutput->numOfRows = numOfRows; - if (hasNullType || // one of operant is NULL type - (isConstantLeft && colDataIsNull_s(pInputData[0], 0)) || // left operand is constant NULL - (isConstantRight && colDataIsNull_s(pInputData[1], 0))) { // right operand is constant NULL + if (hasNullType || // one of operant is NULL type + (isConstantLeft && colDataIsNull_s(pInputData[0], 0)) || // left operand is constant NULL + (isConstantRight && colDataIsNull_s(pInputData[1], 0))) { // right operand is constant NULL colDataSetNNULL(pOutputData, 0, numOfRows); code = TSDB_CODE_SUCCESS; - } - else { + } else { int32_t iLeft = 0; int32_t iRight = 0; for (int32_t i = 0; i < numOfRows; ++i) { @@ -309,103 +265,78 @@ int32_t geomTwoParamsFunction(SScalarParam *pInput, SScalarParam *pOutput, continue; } - code = executeTwoParamsFn(pInputData, iLeft, iRight, pOutputData); - if (code != TSDB_CODE_SUCCESS) { - return code; - } + TAOS_CHECK_RETURN(executeTwoParamsFn(pInputData, iLeft, iRight, pOutputData)); } } - return code; + TAOS_RETURN(code); } -int32_t geomRelationFunction(SScalarParam *pInput, SScalarParam *pOutput, - bool swapAllowed, _geomDoRelationFunc_t doRelationFn) { +int32_t geomRelationFunction(SScalarParam *pInput, SScalarParam *pOutput, bool swapAllowed, + _geomDoRelationFunc_t doRelationFn) { int32_t code = TSDB_CODE_FAILED; - code = initCtxRelationFunc(); - if (code != TSDB_CODE_SUCCESS) { - return code; - } + TAOS_CHECK_RETURN(initCtxRelationFunc()); // handle with all NULL output - bool hasNullType = (IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[0])) || - IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[1]))); - bool isConstant1 = (pInput[0].numOfRows == 1); - bool isConstant2 = (pInput[1].numOfRows == 1); + bool hasNullType = (IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[0])) || IS_NULL_TYPE(GET_PARAM_TYPE(&pInput[1]))); + bool isConstant1 = (pInput[0].numOfRows == 1); + bool isConstant2 = (pInput[1].numOfRows == 1); int32_t numOfRows = TMAX(pInput[0].numOfRows, pInput[1].numOfRows); pOutput->numOfRows = numOfRows; SColumnInfoData *pOutputData = pOutput->columnData; - if (hasNullType || // at least one of operant is NULL type - (isConstant1 && colDataIsNull_s(pInput[0].columnData, 0)) || // left operand is constant NULL - (isConstant2 && colDataIsNull_s(pInput[1].columnData, 0))) { // right operand is constant NULL + if (hasNullType || // at least one of operant is NULL type + (isConstant1 && colDataIsNull_s(pInput[0].columnData, 0)) || // left operand is constant NULL + (isConstant2 && colDataIsNull_s(pInput[1].columnData, 0))) { // right operand is constant NULL colDataSetNNULL(pOutputData, 0, numOfRows); - code = TSDB_CODE_SUCCESS; - return code; + TAOS_RETURN(TSDB_CODE_SUCCESS); } - bool swapped = false; + bool swapped = false; SColumnInfoData *pInputData[2]; // swap two input data to make sure input data 0 is constant if swapAllowed and only isConstant2 is true - if (swapAllowed && - !isConstant1 && isConstant2) { + if (swapAllowed && !isConstant1 && isConstant2) { pInputData[0] = pInput[1].columnData; pInputData[1] = pInput[0].columnData; isConstant1 = true; isConstant2 = false; swapped = true; - } - else { + } else { pInputData[0] = pInput[0].columnData; pInputData[1] = pInput[1].columnData; } - GEOSGeometry *geom1 = NULL; - GEOSGeometry *geom2 = NULL; + GEOSGeometry *geom1 = NULL; + GEOSGeometry *geom2 = NULL; const GEOSPreparedGeometry *preparedGeom1 = NULL; // if there is constant, make PreparedGeometry from pInputData 0 if (isConstant1) { - code = readGeometry(colDataGetData(pInputData[0], 0), &geom1, &preparedGeom1); - if (code != TSDB_CODE_SUCCESS) { - goto _exit; - } + TAOS_CHECK_GOTO(readGeometry(colDataGetData(pInputData[0], 0), &geom1, &preparedGeom1), NULL, _exit); } if (isConstant2) { - code = readGeometry(colDataGetData(pInputData[1], 0), &geom2, NULL); - if (code != TSDB_CODE_SUCCESS) { - goto _exit; - } + TAOS_CHECK_GOTO(readGeometry(colDataGetData(pInputData[1], 0), &geom2, NULL), NULL, _exit); } for (int32_t i = 0; i < numOfRows; ++i) { - if ((!isConstant1 && colDataIsNull_s(pInputData[0], i)) || - (!isConstant2 && colDataIsNull_s(pInputData[1], i))) { + if ((!isConstant1 && colDataIsNull_s(pInputData[0], i)) || (!isConstant2 && colDataIsNull_s(pInputData[1], i))) { colDataSetNULL(pOutputData, i); code = TSDB_CODE_SUCCESS; continue; } if (!isConstant1) { - code = readGeometry(colDataGetData(pInputData[0], i), &geom1, &preparedGeom1); - if (code != TSDB_CODE_SUCCESS) { - goto _exit; - } + TAOS_CHECK_GOTO(readGeometry(colDataGetData(pInputData[0], i), &geom1, &preparedGeom1), NULL, _exit); } if (!isConstant2) { - code = readGeometry(colDataGetData(pInputData[1], i), &geom2, NULL); - if (code != TSDB_CODE_SUCCESS) { - goto _exit; - } + TAOS_CHECK_GOTO(readGeometry(colDataGetData(pInputData[1], i), &geom2, NULL), NULL, _exit); } - code = executeRelationFunc(geom1, preparedGeom1, geom2, i, swapped, pOutputData, doRelationFn); - if (code != TSDB_CODE_SUCCESS) { - goto _exit; - } + TAOS_CHECK_GOTO(executeRelationFunc(geom1, preparedGeom1, geom2, i, swapped, pOutputData, doRelationFn), NULL, + _exit); if (!isConstant1) { destroyGeometry(&geom1, &preparedGeom1); @@ -419,7 +350,7 @@ _exit: destroyGeometry(&geom1, &preparedGeom1); destroyGeometry(&geom2, NULL); - return code; + TAOS_RETURN(code); } int32_t makePointFunction(SScalarParam *pInput, int32_t inputNum, SScalarParam *pOutput) { diff --git a/source/libs/geometry/src/geosWrapper.c b/source/libs/geometry/src/geosWrapper.c index c5250c8481..2142b3d62d 100644 --- a/source/libs/geometry/src/geosWrapper.c +++ b/source/libs/geometry/src/geosWrapper.c @@ -18,7 +18,8 @@ #include "types.h" typedef char (*_geosRelationFunc_t)(GEOSContextHandle_t handle, const GEOSGeometry *g1, const GEOSGeometry *g2); -typedef char (*_geosPreparedRelationFunc_t)(GEOSContextHandle_t handle, const GEOSPreparedGeometry *pg1, const GEOSGeometry *g2); +typedef char (*_geosPreparedRelationFunc_t)(GEOSContextHandle_t handle, const GEOSPreparedGeometry *pg1, + const GEOSGeometry *g2); void geosFreeBuffer(void *buffer) { if (buffer) { @@ -27,13 +28,13 @@ void geosFreeBuffer(void *buffer) { } void geosErrMsgeHandler(const char *errMsg, void *userData) { - char* targetErrMsg = userData; + char *targetErrMsg = userData; snprintf(targetErrMsg, 512, "%s", errMsg); } int32_t initCtxMakePoint() { - int32_t code = TSDB_CODE_FAILED; - SGeosContext* geosCtx = getThreadLocalGeosCtx(); + int32_t code = TSDB_CODE_FAILED; + SGeosContext *geosCtx = getThreadLocalGeosCtx(); if (geosCtx->handle == NULL) { geosCtx->handle = GEOS_init_r(); @@ -41,7 +42,7 @@ int32_t initCtxMakePoint() { return code; } - GEOSContext_setErrorMessageHandler_r(geosCtx->handle, geosErrMsgeHandler, geosCtx->errMsg); + (void)GEOSContext_setErrorMessageHandler_r(geosCtx->handle, geosErrMsgeHandler, geosCtx->errMsg); } if (geosCtx->WKBWriter == NULL) { @@ -57,10 +58,10 @@ int32_t initCtxMakePoint() { // outputWKT is a zero ending string // need to call geosFreeBuffer(*outputGeom) later int32_t doMakePoint(double x, double y, unsigned char **outputGeom, size_t *size) { - int32_t code = TSDB_CODE_FAILED; - SGeosContext* geosCtx = getThreadLocalGeosCtx(); + int32_t code = TSDB_CODE_FAILED; + SGeosContext *geosCtx = getThreadLocalGeosCtx(); - GEOSGeometry *geom = NULL; + GEOSGeometry *geom = NULL; unsigned char *wkb = NULL; geom = GEOSGeom_createPointFromXY_r(geosCtx->handle, x, y); @@ -89,6 +90,10 @@ _exit: static int32_t initWktRegex(pcre2_code **ppRegex, pcre2_match_data **ppMatchData) { int32_t code = 0; char *wktPatternWithSpace = taosMemoryCalloc(4, 1024); + if (NULL == wktPatternWithSpace) { + return TSDB_CODE_OUT_OF_MEMORY; + } + sprintf( wktPatternWithSpace, "^( *)point( *)z?m?( *)((empty)|(\\(( *)(([-+]?[0-9]+\\.?[0-9]*)|([-+]?[0-9]*\\.?[0-9]+))(e[-+]?[0-9]+)?(( " @@ -148,8 +153,8 @@ static int32_t initWktRegex(pcre2_code **ppRegex, pcre2_match_data **ppMatchData } int32_t initCtxGeomFromText() { - int32_t code = TSDB_CODE_FAILED; - SGeosContext* geosCtx = getThreadLocalGeosCtx(); + int32_t code = TSDB_CODE_FAILED; + SGeosContext *geosCtx = getThreadLocalGeosCtx(); if (geosCtx->handle == NULL) { geosCtx->handle = GEOS_init_r(); @@ -157,7 +162,7 @@ int32_t initCtxGeomFromText() { return code; } - GEOSContext_setErrorMessageHandler_r(geosCtx->handle, geosErrMsgeHandler, geosCtx->errMsg); + (void)GEOSContext_setErrorMessageHandler_r(geosCtx->handle, geosErrMsgeHandler, geosCtx->errMsg); } if (geosCtx->WKTReader == NULL) { @@ -184,15 +189,15 @@ int32_t initCtxGeomFromText() { // inputWKT is a zero ending string // need to call geosFreeBuffer(*outputGeom) later int32_t doGeomFromText(const char *inputWKT, unsigned char **outputGeom, size_t *size) { - int32_t code = TSDB_CODE_FAILED; - SGeosContext* geosCtx = getThreadLocalGeosCtx(); + int32_t code = TSDB_CODE_FAILED; + SGeosContext *geosCtx = getThreadLocalGeosCtx(); - GEOSGeometry *geom = NULL; + GEOSGeometry *geom = NULL; unsigned char *wkb = NULL; if (doRegExec(inputWKT, geosCtx->WKTRegex, geosCtx->WKTMatchData) != 0) { - code = TSDB_CODE_FUNC_FUNTION_PARA_VALUE; - goto _exit; + code = TSDB_CODE_FUNC_FUNTION_PARA_VALUE; + goto _exit; } geom = GEOSWKTReader_read_r(geosCtx->handle, geosCtx->WKTReader, inputWKT); @@ -219,8 +224,8 @@ _exit: } int32_t initCtxAsText() { - int32_t code = TSDB_CODE_FAILED; - SGeosContext* geosCtx = getThreadLocalGeosCtx(); + int32_t code = TSDB_CODE_FAILED; + SGeosContext *geosCtx = getThreadLocalGeosCtx(); if (geosCtx->handle == NULL) { geosCtx->handle = GEOS_init_r(); @@ -228,7 +233,7 @@ int32_t initCtxAsText() { return code; } - GEOSContext_setErrorMessageHandler_r(geosCtx->handle, geosErrMsgeHandler, geosCtx->errMsg); + (void)GEOSContext_setErrorMessageHandler_r(geosCtx->handle, geosErrMsgeHandler, geosCtx->errMsg); } if (geosCtx->WKBReader == NULL) { @@ -255,10 +260,10 @@ int32_t initCtxAsText() { // outputWKT is a zero ending string // need to call geosFreeBuffer(*outputWKT) later int32_t doAsText(const unsigned char *inputGeom, size_t size, char **outputWKT) { - int32_t code = TSDB_CODE_FAILED; - SGeosContext* geosCtx = getThreadLocalGeosCtx(); + int32_t code = TSDB_CODE_FAILED; + SGeosContext *geosCtx = getThreadLocalGeosCtx(); - GEOSGeometry *geom = NULL; + GEOSGeometry *geom = NULL; unsigned char *wkt = NULL; geom = GEOSWKBReader_read_r(geosCtx->handle, geosCtx->WKBReader, inputGeom, size); @@ -286,8 +291,8 @@ _exit: } int32_t initCtxRelationFunc() { - int32_t code = TSDB_CODE_FAILED; - SGeosContext* geosCtx = getThreadLocalGeosCtx(); + int32_t code = TSDB_CODE_FAILED; + SGeosContext *geosCtx = getThreadLocalGeosCtx(); if (geosCtx->handle == NULL) { geosCtx->handle = GEOS_init_r(); @@ -295,7 +300,7 @@ int32_t initCtxRelationFunc() { return code; } - GEOSContext_setErrorMessageHandler_r(geosCtx->handle, geosErrMsgeHandler, geosCtx->errMsg); + (void)GEOSContext_setErrorMessageHandler_r(geosCtx->handle, geosErrMsgeHandler, geosCtx->errMsg); } if (geosCtx->WKBReader == NULL) { @@ -309,88 +314,81 @@ int32_t initCtxRelationFunc() { } int32_t doGeosRelation(const GEOSGeometry *geom1, const GEOSPreparedGeometry *preparedGeom1, const GEOSGeometry *geom2, - bool swapped, char *res, - _geosRelationFunc_t relationFn, - _geosRelationFunc_t swappedRelationFn, + bool swapped, char *res, _geosRelationFunc_t relationFn, _geosRelationFunc_t swappedRelationFn, _geosPreparedRelationFunc_t preparedRelationFn, _geosPreparedRelationFunc_t swappedPreparedRelationFn) { - int32_t code = TSDB_CODE_FAILED; - SGeosContext* geosCtx = getThreadLocalGeosCtx(); + SGeosContext *geosCtx = getThreadLocalGeosCtx(); if (!preparedGeom1) { if (!swapped) { ASSERT(relationFn); *res = relationFn(geosCtx->handle, geom1, geom2); - } - else { + } else { ASSERT(swappedRelationFn); *res = swappedRelationFn(geosCtx->handle, geom1, geom2); } - } - else { + } else { if (!swapped) { ASSERT(preparedRelationFn); *res = preparedRelationFn(geosCtx->handle, preparedGeom1, geom2); - } - else { + } else { ASSERT(swappedPreparedRelationFn); *res = swappedPreparedRelationFn(geosCtx->handle, preparedGeom1, geom2); } } - code = TSDB_CODE_SUCCESS; - return code; + return TSDB_CODE_SUCCESS; } int32_t doIntersects(const GEOSGeometry *geom1, const GEOSPreparedGeometry *preparedGeom1, const GEOSGeometry *geom2, bool swapped, char *res) { - return doGeosRelation(geom1, preparedGeom1, geom2, swapped, res, - GEOSIntersects_r, GEOSIntersects_r, GEOSPreparedIntersects_r, GEOSPreparedIntersects_r); + return doGeosRelation(geom1, preparedGeom1, geom2, swapped, res, GEOSIntersects_r, GEOSIntersects_r, + GEOSPreparedIntersects_r, GEOSPreparedIntersects_r); } int32_t doEquals(const GEOSGeometry *geom1, const GEOSPreparedGeometry *preparedGeom1, const GEOSGeometry *geom2, bool swapped, char *res) { - return doGeosRelation(geom1, NULL, geom2, swapped, res, - GEOSEquals_r, GEOSEquals_r, NULL, NULL); // no prepared version for eguals() + return doGeosRelation(geom1, NULL, geom2, swapped, res, GEOSEquals_r, GEOSEquals_r, NULL, + NULL); // no prepared version for eguals() } int32_t doTouches(const GEOSGeometry *geom1, const GEOSPreparedGeometry *preparedGeom1, const GEOSGeometry *geom2, bool swapped, char *res) { - return doGeosRelation(geom1, preparedGeom1, geom2, swapped, res, - GEOSTouches_r, GEOSTouches_r, GEOSPreparedTouches_r, GEOSPreparedTouches_r); + return doGeosRelation(geom1, preparedGeom1, geom2, swapped, res, GEOSTouches_r, GEOSTouches_r, GEOSPreparedTouches_r, + GEOSPreparedTouches_r); } int32_t doCovers(const GEOSGeometry *geom1, const GEOSPreparedGeometry *preparedGeom1, const GEOSGeometry *geom2, bool swapped, char *res) { - return doGeosRelation(geom1, preparedGeom1, geom2, swapped, res, - GEOSCovers_r, GEOSCoveredBy_r, GEOSPreparedCovers_r, GEOSPreparedCoveredBy_r); + return doGeosRelation(geom1, preparedGeom1, geom2, swapped, res, GEOSCovers_r, GEOSCoveredBy_r, GEOSPreparedCovers_r, + GEOSPreparedCoveredBy_r); } int32_t doContains(const GEOSGeometry *geom1, const GEOSPreparedGeometry *preparedGeom1, const GEOSGeometry *geom2, bool swapped, char *res) { - return doGeosRelation(geom1, preparedGeom1, geom2, swapped, res, - GEOSContains_r, GEOSWithin_r, GEOSPreparedContains_r, GEOSPreparedWithin_r); + return doGeosRelation(geom1, preparedGeom1, geom2, swapped, res, GEOSContains_r, GEOSWithin_r, GEOSPreparedContains_r, + GEOSPreparedWithin_r); } -int32_t doContainsProperly(const GEOSGeometry *geom1, const GEOSPreparedGeometry *preparedGeom1, const GEOSGeometry *geom2, - bool swapped, char *res) { - return doGeosRelation(geom1, preparedGeom1, geom2, swapped, res, - NULL, NULL, GEOSPreparedContainsProperly_r, NULL); +int32_t doContainsProperly(const GEOSGeometry *geom1, const GEOSPreparedGeometry *preparedGeom1, + const GEOSGeometry *geom2, bool swapped, char *res) { + return doGeosRelation(geom1, preparedGeom1, geom2, swapped, res, NULL, NULL, GEOSPreparedContainsProperly_r, NULL); } // input is with VARSTR format // need to call destroyGeometry(outputGeom, outputPreparedGeom) later -int32_t readGeometry(const unsigned char *input, GEOSGeometry **outputGeom, const GEOSPreparedGeometry **outputPreparedGeom) { - SGeosContext* geosCtx = getThreadLocalGeosCtx(); +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 + ASSERT(outputGeom); // it is not allowed if outputGeom is NULL *outputGeom = NULL; - if (outputPreparedGeom) { //it means not to generate PreparedGeometry if outputPreparedGeom is NULL + if (outputPreparedGeom) { // it means not to generate PreparedGeometry if outputPreparedGeom is NULL *outputPreparedGeom = NULL; } - if (varDataLen(input) == 0) { //empty value + if (varDataLen(input) == 0) { // empty value return TSDB_CODE_SUCCESS; } @@ -410,7 +408,7 @@ int32_t readGeometry(const unsigned char *input, GEOSGeometry **outputGeom, cons } void destroyGeometry(GEOSGeometry **geom, const GEOSPreparedGeometry **preparedGeom) { - SGeosContext* geosCtx = getThreadLocalGeosCtx(); + SGeosContext *geosCtx = getThreadLocalGeosCtx(); if (preparedGeom && *preparedGeom) { GEOSPreparedGeom_destroy_r(geosCtx->handle, *preparedGeom); From 12f9116eebee4a94bf8ec71201907003e89b3048 Mon Sep 17 00:00:00 2001 From: Shungang Li Date: Wed, 24 Jul 2024 19:27:52 +0800 Subject: [PATCH 5/5] fix: (errcode) geom return value --- source/libs/geometry/src/geomFunc.c | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/source/libs/geometry/src/geomFunc.c b/source/libs/geometry/src/geomFunc.c index 343e6ec4ce..194590c06c 100644 --- a/source/libs/geometry/src/geomFunc.c +++ b/source/libs/geometry/src/geomFunc.c @@ -203,8 +203,6 @@ int32_t executeRelationFunc(const GEOSGeometry *geom1, const GEOSPreparedGeometr int32_t geomOneParamFunction(SScalarParam *pInput, SScalarParam *pOutput, _geomInitCtxFunc_t initCtxFn, _geomExecuteOneParamFunc_t executeOneParamFn) { - int32_t code = TSDB_CODE_FAILED; - TAOS_CHECK_RETURN(initCtxFn()); SColumnInfoData *pInputData = pInput->columnData; @@ -213,12 +211,10 @@ int32_t geomOneParamFunction(SScalarParam *pInput, SScalarParam *pOutput, _geomI if (IS_NULL_TYPE(GET_PARAM_TYPE(pInput))) { colDataSetNNULL(pOutputData, 0, pInput->numOfRows); - code = TSDB_CODE_SUCCESS; } else { for (int32_t i = 0; i < pInput->numOfRows; ++i) { if (colDataIsNull_s(pInputData, i)) { colDataSetNULL(pOutputData, i); - code = TSDB_CODE_SUCCESS; continue; } @@ -226,13 +222,11 @@ int32_t geomOneParamFunction(SScalarParam *pInput, SScalarParam *pOutput, _geomI } } - TAOS_RETURN(code); + TAOS_RETURN(TSDB_CODE_SUCCESS); } int32_t geomTwoParamsFunction(SScalarParam *pInput, SScalarParam *pOutput, _geomInitCtxFunc_t initCtxFn, _geomExecuteTwoParamsFunc_t executeTwoParamsFn) { - int32_t code = TSDB_CODE_FAILED; - TAOS_CHECK_RETURN(initCtxFn()); SColumnInfoData *pInputData[2]; @@ -250,7 +244,6 @@ int32_t geomTwoParamsFunction(SScalarParam *pInput, SScalarParam *pOutput, _geom (isConstantLeft && colDataIsNull_s(pInputData[0], 0)) || // left operand is constant NULL (isConstantRight && colDataIsNull_s(pInputData[1], 0))) { // right operand is constant NULL colDataSetNNULL(pOutputData, 0, numOfRows); - code = TSDB_CODE_SUCCESS; } else { int32_t iLeft = 0; int32_t iRight = 0; @@ -261,7 +254,6 @@ int32_t geomTwoParamsFunction(SScalarParam *pInput, SScalarParam *pOutput, _geom if ((!isConstantLeft && colDataIsNull_s(pInputData[0], iLeft)) || (!isConstantRight && colDataIsNull_s(pInputData[1], iRight))) { colDataSetNULL(pOutputData, i); - code = TSDB_CODE_SUCCESS; continue; } @@ -269,7 +261,7 @@ int32_t geomTwoParamsFunction(SScalarParam *pInput, SScalarParam *pOutput, _geom } } - TAOS_RETURN(code); + TAOS_RETURN(TSDB_CODE_SUCCESS); } int32_t geomRelationFunction(SScalarParam *pInput, SScalarParam *pOutput, bool swapAllowed, @@ -324,7 +316,6 @@ int32_t geomRelationFunction(SScalarParam *pInput, SScalarParam *pOutput, bool s for (int32_t i = 0; i < numOfRows; ++i) { if ((!isConstant1 && colDataIsNull_s(pInputData[0], i)) || (!isConstant2 && colDataIsNull_s(pInputData[1], i))) { colDataSetNULL(pOutputData, i); - code = TSDB_CODE_SUCCESS; continue; } @@ -346,6 +337,8 @@ int32_t geomRelationFunction(SScalarParam *pInput, SScalarParam *pOutput, bool s } } + code = TSDB_CODE_SUCCESS; + _exit: destroyGeometry(&geom1, &preparedGeom1); destroyGeometry(&geom2, NULL);