enh: refactor return code

This commit is contained in:
kailixu 2024-07-24 16:08:12 +08:00
parent acb1d0a9cc
commit 586d3cae64
4 changed files with 75 additions and 46 deletions

View File

@ -157,18 +157,18 @@ static FORCE_INLINE int32_t taosGetTbHashVal(const char *tbname, int32_t tblen,
return (terrno = (code)); \ return (terrno = (code)); \
} while (0) } while (0)
#define TAOS_CHECK_RETURN(CMD) \ #define TAOS_CHECK_RETURN(CMD) \
do { \ do { \
int32_t code = (CMD); \ int32_t code = (CMD); \
if (code != TSDB_CODE_SUCCESS) { \ if (code != TSDB_CODE_SUCCESS) { \
TAOS_RETURN(code); \ TAOS_RETURN(code); \
} \ } \
} while (0) } while (0)
#define TAOS_CHECK_GOTO(CMD, LINO, LABEL) \ #define TAOS_CHECK_GOTO(CMD, LINO, LABEL) \
do { \ do { \
code = (CMD); \ code = (CMD); \
if (code != TSDB_CODE_SUCCESS) { \ if (code != TSDB_CODE_SUCCESS) { \
if (LINO) { \ if (LINO) { \
*((int32_t *)(LINO)) = __LINE__; \ *((int32_t *)(LINO)) = __LINE__; \
} \ } \

View File

@ -429,9 +429,7 @@ int32_t dmUpdateEncryptKey(char *key, bool toLogFile) {
} }
} }
if ((code = tGetMachineId(&machineId)) != 0) { TAOS_CHECK_GOTO(tGetMachineId(&machineId), &lino, _OVER);
goto _OVER;
}
TAOS_CHECK_GOTO(generateEncryptCode(key, machineId, &encryptCode), &lino, _OVER); TAOS_CHECK_GOTO(generateEncryptCode(key, machineId, &encryptCode), &lino, _OVER);

View File

@ -229,26 +229,23 @@ static int32_t mndProcessConnectReq(SRpcMsg *pReq) {
SUserObj *pUser = NULL; SUserObj *pUser = NULL;
SDbObj *pDb = NULL; SDbObj *pDb = NULL;
SConnObj *pConn = NULL; SConnObj *pConn = NULL;
int32_t code = -1; int32_t code = 0;
SConnectReq connReq = {0}; SConnectReq connReq = {0};
char ip[24] = {0}; char ip[24] = {0};
const STraceId *trace = &pReq->info.traceId; const STraceId *trace = &pReq->info.traceId;
if ((code = tDeserializeSConnectReq(pReq->pCont, pReq->contLen, &connReq)) != 0) { if ((code = tDeserializeSConnectReq(pReq->pCont, pReq->contLen, &connReq)) != 0) {
terrno = (-1 == code ? TSDB_CODE_INVALID_MSG : code);
goto _OVER; goto _OVER;
} }
if ((code = taosCheckVersionCompatibleFromStr(connReq.sVer, version, 3)) != 0) { if ((code = taosCheckVersionCompatibleFromStr(connReq.sVer, version, 3)) != 0) {
mGError("version not compatible. client version: %s, server version: %s", connReq.sVer, version); mGError("version not compatible. client version: %s, server version: %s", connReq.sVer, version);
terrno = code;
goto _OVER; goto _OVER;
} }
code = -1;
taosIp2String(pReq->info.conn.clientIp, ip); taosIp2String(pReq->info.conn.clientIp, ip);
if (mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CONNECT) != 0) { if ((code = mndCheckOperPrivilege(pMnode, pReq->info.conn.user, MND_OPER_CONNECT)) != 0) {
mGError("user:%s, failed to login from %s since %s", pReq->info.conn.user, ip, terrstr()); mGError("user:%s, failed to login from %s since %s", pReq->info.conn.user, ip, tstrerror(code));
goto _OVER; goto _OVER;
} }
@ -271,22 +268,22 @@ static int32_t mndProcessConnectReq(SRpcMsg *pReq) {
if (pDb == NULL) { if (pDb == NULL) {
if (0 != strcmp(connReq.db, TSDB_INFORMATION_SCHEMA_DB) && if (0 != strcmp(connReq.db, TSDB_INFORMATION_SCHEMA_DB) &&
(0 != strcmp(connReq.db, TSDB_PERFORMANCE_SCHEMA_DB))) { (0 != strcmp(connReq.db, TSDB_PERFORMANCE_SCHEMA_DB))) {
terrno = TSDB_CODE_MND_DB_NOT_EXIST; code = TSDB_CODE_MND_DB_NOT_EXIST;
mGError("user:%s, failed to login from %s while use db:%s since %s", pReq->info.conn.user, ip, connReq.db, mGError("user:%s, failed to login from %s while use db:%s since %s", pReq->info.conn.user, ip, connReq.db,
terrstr()); tstrerror(code));
goto _OVER; goto _OVER;
} }
} }
if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_READ_OR_WRITE_DB, pDb) != 0) { TAOS_CHECK_GOTO(mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_READ_OR_WRITE_DB, pDb), NULL, _OVER);
goto _OVER;
}
} }
pConn = mndCreateConn(pMnode, pReq->info.conn.user, connReq.connType, pReq->info.conn.clientIp, pConn = mndCreateConn(pMnode, pReq->info.conn.user, connReq.connType, pReq->info.conn.clientIp,
pReq->info.conn.clientPort, connReq.pid, connReq.app, connReq.startTime); pReq->info.conn.clientPort, connReq.pid, connReq.app, connReq.startTime);
if (pConn == NULL) { if (pConn == NULL) {
mGError("user:%s, failed to login from %s while create connection since %s", pReq->info.conn.user, ip, terrstr()); code = terrno;
mGError("user:%s, failed to login from %s while create connection since %s", pReq->info.conn.user, ip,
tstrerror(code));
goto _OVER; goto _OVER;
} }
@ -316,10 +313,19 @@ static int32_t mndProcessConnectReq(SRpcMsg *pReq) {
mndGetMnodeEpSet(pMnode, &connectRsp.epSet); mndGetMnodeEpSet(pMnode, &connectRsp.epSet);
int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp); int32_t contLen = tSerializeSConnectRsp(NULL, 0, &connectRsp);
if (contLen < 0) goto _OVER; if (contLen < 0) {
TAOS_CHECK_GOTO(contLen, NULL, _OVER);
}
void *pRsp = rpcMallocCont(contLen); void *pRsp = rpcMallocCont(contLen);
if (pRsp == NULL) goto _OVER; if (pRsp == NULL) {
tSerializeSConnectRsp(pRsp, contLen, &connectRsp); TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, NULL, _OVER);
}
contLen = tSerializeSConnectRsp(pRsp, contLen, &connectRsp);
if (contLen < 0) {
rpcFreeCont(pRsp);
TAOS_CHECK_GOTO(contLen, NULL, _OVER);
}
pReq->info.rspLen = contLen; pReq->info.rspLen = contLen;
pReq->info.rsp = pRsp; pReq->info.rsp = pRsp;
@ -339,7 +345,7 @@ _OVER:
mndReleaseDb(pMnode, pDb); mndReleaseDb(pMnode, pDb);
mndReleaseConn(pMnode, pConn, true); mndReleaseConn(pMnode, pConn, true);
return code; TAOS_RETURN(code);
} }
static int32_t mndSaveQueryList(SConnObj *pConn, SQueryHbReqBasic *pBasic) { static int32_t mndSaveQueryList(SConnObj *pConn, SQueryHbReqBasic *pBasic) {
@ -656,6 +662,7 @@ static int32_t mndProcessQueryHeartBeat(SMnode *pMnode, SRpcMsg *pMsg, SClientHb
static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) { static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) {
int32_t code = 0; int32_t code = 0;
int32_t lino = 0;
SMnode *pMnode = pReq->info.node; SMnode *pMnode = pReq->info.node;
SClientHbBatchReq batchReq = {0}; SClientHbBatchReq batchReq = {0};
@ -675,6 +682,9 @@ static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) {
SClientHbBatchRsp batchRsp = {0}; SClientHbBatchRsp batchRsp = {0};
batchRsp.svrTimestamp = taosGetTimestampSec(); batchRsp.svrTimestamp = taosGetTimestampSec();
batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp)); batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp));
if (batchRsp.rsps == NULL) {
TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
}
batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor; batchRsp.monitorParas.tsEnableMonitor = tsEnableMonitor;
batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval; batchRsp.monitorParas.tsMonitorInterval = tsMonitorInterval;
batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold; batchRsp.monitorParas.tsSlowLogThreshold = tsSlowLogThreshold;
@ -687,7 +697,7 @@ static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) {
for (int i = 0; i < sz; i++) { for (int i = 0; i < sz; i++) {
SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i); SClientHbReq *pHbReq = taosArrayGet(batchReq.reqs, i);
if (pHbReq->connKey.connType == CONN_TYPE__QUERY) { if (pHbReq->connKey.connType == CONN_TYPE__QUERY) {
mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj); TAOS_CHECK_EXIT(mndProcessQueryHeartBeat(pMnode, pReq, pHbReq, &batchRsp, &obj));
} else if (pHbReq->connKey.connType == CONN_TYPE__TMQ) { } else if (pHbReq->connKey.connType == CONN_TYPE__TMQ) {
SClientHbRsp *pRsp = mndMqHbBuildRsp(pMnode, pHbReq); SClientHbRsp *pRsp = mndMqHbBuildRsp(pMnode, pHbReq);
if (pRsp != NULL) { if (pRsp != NULL) {
@ -699,12 +709,22 @@ static int32_t mndProcessHeartBeatReq(SRpcMsg *pReq) {
taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq); taosArrayDestroyEx(batchReq.reqs, tFreeClientHbReq);
int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp); int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp);
void *buf = rpcMallocCont(tlen); if (tlen < 0) {
tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp); TAOS_CHECK_EXIT(tlen);
}
tFreeClientHbBatchRsp(&batchRsp); void *buf = rpcMallocCont(tlen);
if (!buf) {
TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
}
tlen = tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp);
if (tlen < 0) {
rpcFreeCont(buf);
TAOS_CHECK_EXIT(tlen);
}
pReq->info.rspLen = tlen; pReq->info.rspLen = tlen;
pReq->info.rsp = buf; pReq->info.rsp = buf;
_exit:
tFreeClientHbBatchRsp(&batchRsp);
taosArrayDestroy(obj.pQnodeList); taosArrayDestroy(obj.pQnodeList);
@ -771,24 +791,31 @@ static int32_t mndProcessKillConnReq(SRpcMsg *pReq) {
} }
static int32_t mndProcessSvrVerReq(SRpcMsg *pReq) { static int32_t mndProcessSvrVerReq(SRpcMsg *pReq) {
int32_t code = -1; int32_t code = 0;
int32_t lino = 0;
SServerVerRsp rsp = {0}; SServerVerRsp rsp = {0};
tstrncpy(rsp.ver, version, sizeof(rsp.ver)); tstrncpy(rsp.ver, version, sizeof(rsp.ver));
int32_t contLen = tSerializeSServerVerRsp(NULL, 0, &rsp); int32_t contLen = tSerializeSServerVerRsp(NULL, 0, &rsp);
if (contLen < 0) goto _over; if (contLen < 0) {
TAOS_CHECK_EXIT(contLen);
}
void *pRsp = rpcMallocCont(contLen); void *pRsp = rpcMallocCont(contLen);
if (pRsp == NULL) goto _over; if (pRsp == NULL) {
tSerializeSServerVerRsp(pRsp, contLen, &rsp); TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
}
contLen = tSerializeSServerVerRsp(pRsp, contLen, &rsp);
if (contLen < 0) {
rpcFreeCont(pRsp);
TAOS_CHECK_EXIT(contLen);
}
pReq->info.rspLen = contLen; pReq->info.rspLen = contLen;
pReq->info.rsp = pRsp; pReq->info.rsp = pRsp;
code = 0; _exit:
_over: TAOS_RETURN(code);
return code;
} }
static int32_t mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) { static int32_t mndRetrieveConns(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {

View File

@ -1958,8 +1958,7 @@ int32_t mndProcesSRetrieveIpWhiteReq(SRpcMsg *pReq) {
len = tSerializeSUpdateIpWhite(NULL, 0, &ipWhite); len = tSerializeSUpdateIpWhite(NULL, 0, &ipWhite);
if (len < 0) { if (len < 0) {
code = TSDB_CODE_OUT_OF_MEMORY; TAOS_CHECK_GOTO(len, &lino, _OVER);
TAOS_CHECK_GOTO(code, &lino, _OVER);
} }
pRsp = rpcMallocCont(len); pRsp = rpcMallocCont(len);
@ -1968,8 +1967,7 @@ int32_t mndProcesSRetrieveIpWhiteReq(SRpcMsg *pReq) {
} }
len = tSerializeSUpdateIpWhite(pRsp, len, &ipWhite); len = tSerializeSUpdateIpWhite(pRsp, len, &ipWhite);
if (len < 0) { if (len < 0) {
code = TSDB_CODE_OUT_OF_MEMORY; TAOS_CHECK_GOTO(len, &lino, _OVER);
TAOS_CHECK_GOTO(code, &lino, _OVER);
} }
_OVER: _OVER:
@ -2597,7 +2595,10 @@ static int32_t mndProcessGetUserAuthReq(SRpcMsg *pReq) {
TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY); TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
} }
TAOS_CHECK_EXIT(tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp)); contLen =tSerializeSGetUserAuthRsp(pRsp, contLen, &authRsp);
if(contLen < 0) {
TAOS_CHECK_EXIT(contLen);
}
_exit: _exit:
mndReleaseUser(pMnode, pUser); mndReleaseUser(pMnode, pUser);
@ -3190,14 +3191,17 @@ int32_t mndValidateUserAuthInfo(SMnode *pMnode, SUserAuthVersion *pUsers, int32_
} }
rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp); rspLen = tSerializeSUserAuthBatchRsp(NULL, 0, &batchRsp);
if (rspLen <= 0) { if (rspLen < 0) {
TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER); TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
} }
pRsp = taosMemoryMalloc(rspLen); pRsp = taosMemoryMalloc(rspLen);
if (pRsp == NULL) { if (pRsp == NULL) {
TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER); TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER);
} }
TAOS_CHECK_GOTO(tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp), &lino, _OVER); rspLen = tSerializeSUserAuthBatchRsp(pRsp, rspLen, &batchRsp);
if (rspLen < 0) {
TAOS_CHECK_GOTO(rspLen, &lino, _OVER);
}
_OVER: _OVER:
tFreeSUserAuthBatchRsp(&batchRsp); tFreeSUserAuthBatchRsp(&batchRsp);
if (code < 0) { if (code < 0) {