diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 9b49c1908d..a52e87bbb3 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -198,7 +198,8 @@ int32_t taosGetErrSize(); #define TSDB_CODE_TSC_ENCODE_PARAM_ERROR TAOS_DEF_ERROR_CODE(0, 0X0231) #define TSDB_CODE_TSC_ENCODE_PARAM_NULL TAOS_DEF_ERROR_CODE(0, 0X0232) #define TSDB_CODE_TSC_COMPRESS_PARAM_ERROR TAOS_DEF_ERROR_CODE(0, 0X0233) -#define TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR TAOS_DEF_ERROR_CODE(0, 0X0234) +#define TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR TAOS_DEF_ERROR_CODE(0, 0X0234) +#define TSDB_CODE_TSC_FAIL_GENERATE_JSON TAOS_DEF_ERROR_CODE(0, 0X0235) #define TSDB_CODE_TSC_INTERNAL_ERROR TAOS_DEF_ERROR_CODE(0, 0X02FF) // mnode-common diff --git a/source/client/inc/clientInt.h b/source/client/inc/clientInt.h index 507738acc9..928afdaecf 100644 --- a/source/client/inc/clientInt.h +++ b/source/client/inc/clientInt.h @@ -310,7 +310,7 @@ void setResPrecision(SReqResultInfo* pResInfo, int32_t precision); int32_t setQueryResultFromRsp(SReqResultInfo* pResultInfo, const SRetrieveTableRsp* pRsp, bool convertUcs4); int32_t setResultDataPtr(SReqResultInfo* pResultInfo, TAOS_FIELD* pFields, int32_t numOfCols, int32_t numOfRows, bool convertUcs4); -void setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t numOfCols); +int32_t setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t numOfCols); void doFreeReqResultInfo(SReqResultInfo* pResInfo); int32_t transferTableNameList(const char* tbList, int32_t acctId, char* dbName, SArray** pReq); void syncCatalogFn(SMetaData* pResult, void* param, int32_t code); @@ -348,15 +348,16 @@ __async_send_cb_fn_t getMsgRspHandle(int32_t msgType); SMsgSendInfo* buildMsgInfoImpl(SRequestObj* pReqObj); -void* createTscObj(const char* user, const char* auth, const char* db, int32_t connType, SAppInstInfo* pAppInfo); +int32_t createTscObj(const char *user, const char *auth, const char *db, int32_t connType, SAppInstInfo *pAppInfo, + STscObj **p); void destroyTscObj(void* pObj); STscObj* acquireTscObj(int64_t rid); -int32_t releaseTscObj(int64_t rid); +void releaseTscObj(int64_t rid); void destroyAppInst(void* pAppInfo); uint64_t generateRequestId(); -void* createRequest(uint64_t connId, int32_t type, int64_t reqid); +int32_t createRequest(uint64_t connId, int32_t type, int64_t reqid, SRequestObj **pRequest); void destroyRequest(SRequestObj* pRequest); SRequestObj* acquireRequest(int64_t rid); int32_t releaseRequest(int64_t rid); @@ -370,7 +371,7 @@ void resetConnectDB(STscObj* pTscObj); int taos_options_imp(TSDB_OPTION option, const char* str); -void* openTransporter(const char* user, const char* auth, int32_t numOfThreads); +int32_t openTransporter(const char* user, const char* auth, int32_t numOfThreads, void **pDnodeConn); void tscStopCrashReport(); typedef struct AsyncArg { @@ -381,8 +382,8 @@ typedef struct AsyncArg { bool persistConnForSpecificMsg(void* parenct, tmsg_t msgType); void processMsgFromServer(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet); -STscObj* taos_connect_internal(const char* ip, const char* user, const char* pass, const char* auth, const char* db, - uint16_t port, int connType); +int32_t taos_connect_internal(const char* ip, const char* user, const char* pass, const char* auth, const char* db, + uint16_t port, int connType, STscObj** pObj); int32_t parseSql(SRequestObj* pRequest, bool topicQuery, SQuery** pQuery, SStmtCallback* pStmtCb); @@ -395,11 +396,11 @@ void taos_close_internal(void* taos); // --- heartbeat // global, called by mgmt -int hbMgrInit(); -void hbMgrCleanUp(); +int32_t hbMgrInit(); +void hbMgrCleanUp(); // cluster level -SAppHbMgr* appHbMgrInit(SAppInstInfo* pAppInstInfo, char* key); +int32_t appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key, SAppHbMgr **pAppHbMgr); void appHbMgrCleanup(void); void hbRemoveAppHbMrg(SAppHbMgr** pAppHbMgr); void destroyAllRequests(SHashObj* pRequests); @@ -408,7 +409,7 @@ void stopAllRequests(SHashObj* pRequests); //SAppInstInfo* getAppInstInfo(const char* clusterKey); // conn level -int hbRegisterConn(SAppHbMgr* pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType); +int32_t hbRegisterConn(SAppHbMgr* pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType); void hbDeregisterConn(STscObj* pTscObj, SClientHbKey connKey); typedef struct SSqlCallbackWrapper { @@ -427,7 +428,7 @@ void doAsyncQuery(SRequestObj* pRequest, bool forceUpdateMeta); int32_t removeMeta(STscObj* pTscObj, SArray* tbList, bool isView); int32_t handleAlterTbExecRes(void* res, struct SCatalog* pCatalog); int32_t handleCreateTbExecRes(void* res, SCatalog* pCatalog); -bool qnodeRequired(SRequestObj* pRequest); +int32_t qnodeRequired(SRequestObj* pRequest, bool *required); void continueInsertFromCsv(SSqlCallbackWrapper* pWrapper, SRequestObj* pRequest); void destorySqlCallbackWrapper(SSqlCallbackWrapper* pWrapper); void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta, int32_t code); @@ -443,6 +444,30 @@ void freeQueryParam(SSyncQueryParam* param); int32_t clientParseSqlImpl(void* param, const char* dbName, const char* sql, bool parseOnly, const char* effeciveUser, SParseSqlRes* pRes); #endif +#define TSC_ERR_RET(c) \ + do { \ + int32_t _code = c; \ + if (_code != TSDB_CODE_SUCCESS) { \ + terrno = _code; \ + return _code; \ + } \ + } while (0) +#define TSC_RET(c) \ + do { \ + int32_t _code = c; \ + if (_code != TSDB_CODE_SUCCESS) { \ + terrno = _code; \ + } \ + return _code; \ + } while (0) +#define TSC_ERR_JRET(c) \ + do { \ + code = c; \ + if (code != TSDB_CODE_SUCCESS) { \ + terrno = code; \ + goto _return; \ + } \ + } while (0) void slowQueryLog(int64_t rid, bool killed, int32_t code, int32_t cost); diff --git a/source/client/src/clientEnv.c b/source/client/src/clientEnv.c index f640618897..a315033296 100644 --- a/source/client/src/clientEnv.c +++ b/source/client/src/clientEnv.c @@ -44,6 +44,26 @@ #define TSC_VAR_NOT_RELEASE 1 #define TSC_VAR_RELEASED 0 +#define ENV_JSON_FALSE_CHECK(c) \ + do { \ + if (!c) { \ + tscError("faild to add item to JSON object");\ + code = TSDB_CODE_TSC_FAIL_GENERATE_JSON; \ + goto _end; \ + } \ + } while (0) + +#define ENV_ERR_RET(c,info) \ + do { \ + int32_t _code = c; \ + if (_code != TSDB_CODE_SUCCESS) { \ + errno = _code; \ + tscInitRes = _code; \ + tscError(info); \ + return; \ + } \ + } while (0) + STscDbg tscDbg = {0}; SAppInfo appInfo; int64_t lastClusterId = 0; @@ -57,8 +77,14 @@ static TdThreadOnce tscinit = PTHREAD_ONCE_INIT; volatile int32_t tscInitRes = 0; static int32_t registerRequest(SRequestObj *pRequest, STscObj *pTscObj) { + int32_t code = TSDB_CODE_SUCCESS; // connection has been released already, abort creating request. pRequest->self = taosAddRef(clientReqRefPool, pRequest); + if (pRequest->self < 0) { + tscError("failed to add ref to request"); + code = terrno; + return code; + } int32_t num = atomic_add_fetch_32(&pTscObj->numOfReqs, 1); @@ -72,19 +98,23 @@ static int32_t registerRequest(SRequestObj *pRequest, STscObj *pTscObj) { pRequest->self, pRequest->pTscObj->id, num, currentInst, total, pRequest->requestId); } - return TSDB_CODE_SUCCESS; + return code; } static void concatStrings(SArray *list, char* buf, int size){ int len = 0; for(int i = 0; i < taosArrayGetSize(list); i++){ char* db = taosArrayGet(list, i); + if (NULL == db) { + tscError("get dbname failed, buf:%s", buf); + break; + } char* dot = strchr(db, '.'); if (dot != NULL) { db = dot + 1; } if (i != 0){ - strcat(buf, ","); + (void)strcat(buf, ","); len += 1; } int ret = snprintf(buf + len, size - len, "%s", db); @@ -100,61 +130,70 @@ static void concatStrings(SArray *list, char* buf, int size){ } } -static void generateWriteSlowLog(STscObj *pTscObj, SRequestObj *pRequest, int32_t reqType, int64_t duration){ +static int32_t generateWriteSlowLog(STscObj *pTscObj, SRequestObj *pRequest, int32_t reqType, int64_t duration){ cJSON* json = cJSON_CreateObject(); + int32_t code = TSDB_CODE_SUCCESS; if (json == NULL) { tscError("[monitor] cJSON_CreateObject failed"); - return; + return TSDB_CODE_OUT_OF_MEMORY; } char clusterId[32] = {0}; if (snprintf(clusterId, sizeof(clusterId), "%" PRId64, pTscObj->pAppInfo->clusterId) < 0){ tscError("failed to generate clusterId:%" PRId64, pTscObj->pAppInfo->clusterId); + code = TSDB_CODE_FAILED; + goto _end; } char startTs[32] = {0}; if (snprintf(startTs, sizeof(startTs), "%" PRId64, pRequest->metric.start/1000) < 0){ tscError("failed to generate startTs:%" PRId64, pRequest->metric.start/1000); + code = TSDB_CODE_FAILED; + goto _end; } char requestId[32] = {0}; if (snprintf(requestId, sizeof(requestId), "%" PRIu64, pRequest->requestId) < 0){ tscError("failed to generate requestId:%" PRIu64, pRequest->requestId); + code = TSDB_CODE_FAILED; + goto _end; } - cJSON_AddItemToObject(json, "cluster_id", cJSON_CreateString(clusterId)); - cJSON_AddItemToObject(json, "start_ts", cJSON_CreateString(startTs)); - cJSON_AddItemToObject(json, "request_id", cJSON_CreateString(requestId)); - cJSON_AddItemToObject(json, "query_time", cJSON_CreateNumber(duration/1000)); - cJSON_AddItemToObject(json, "code", cJSON_CreateNumber(pRequest->code)); - cJSON_AddItemToObject(json, "error_info", cJSON_CreateString(tstrerror(pRequest->code))); - cJSON_AddItemToObject(json, "type", cJSON_CreateNumber(reqType)); - cJSON_AddItemToObject(json, "rows_num", cJSON_CreateNumber(pRequest->body.resInfo.numOfRows + pRequest->body.resInfo.totalRows)); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "cluster_id", cJSON_CreateString(clusterId))); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "start_ts", cJSON_CreateString(startTs))); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "request_id", cJSON_CreateString(requestId))); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "query_time", cJSON_CreateNumber(duration/1000))); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "code", cJSON_CreateNumber(pRequest->code))); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "error_info", cJSON_CreateString(tstrerror(pRequest->code)))); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "type", cJSON_CreateNumber(reqType))); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "rows_num", cJSON_CreateNumber(pRequest->body.resInfo.numOfRows + pRequest->body.resInfo.totalRows))); if(pRequest->sqlstr != NULL && strlen(pRequest->sqlstr) > pTscObj->pAppInfo->monitorParas.tsSlowLogMaxLen){ char tmp = pRequest->sqlstr[pTscObj->pAppInfo->monitorParas.tsSlowLogMaxLen]; pRequest->sqlstr[pTscObj->pAppInfo->monitorParas.tsSlowLogMaxLen] = '\0'; - cJSON_AddItemToObject(json, "sql", cJSON_CreateString(pRequest->sqlstr)); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "sql", cJSON_CreateString(pRequest->sqlstr))); pRequest->sqlstr[pTscObj->pAppInfo->monitorParas.tsSlowLogMaxLen] = tmp; }else{ - cJSON_AddItemToObject(json, "sql", cJSON_CreateString(pRequest->sqlstr)); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "sql", cJSON_CreateString(pRequest->sqlstr))); } - cJSON_AddItemToObject(json, "user", cJSON_CreateString(pTscObj->user)); - cJSON_AddItemToObject(json, "process_name", cJSON_CreateString(appInfo.appName)); - cJSON_AddItemToObject(json, "ip", cJSON_CreateString(tsLocalFqdn)); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "user", cJSON_CreateString(pTscObj->user))); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "process_name", cJSON_CreateString(appInfo.appName))); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "ip", cJSON_CreateString(tsLocalFqdn))); char pid[32] = {0}; if (snprintf(pid, sizeof(pid), "%d", appInfo.pid) < 0){ tscError("failed to generate pid:%d", appInfo.pid); + code = TSDB_CODE_FAILED; + goto _end; } - cJSON_AddItemToObject(json, "process_id", cJSON_CreateString(pid)); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "process_id", cJSON_CreateString(pid))); if(pRequest->dbList != NULL){ char dbList[1024] = {0}; concatStrings(pRequest->dbList, dbList, sizeof(dbList) - 1); - cJSON_AddItemToObject(json, "db", cJSON_CreateString(dbList)); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "db", cJSON_CreateString(dbList))); }else if(pRequest->pDb != NULL){ - cJSON_AddItemToObject(json, "db", cJSON_CreateString(pRequest->pDb)); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "db", cJSON_CreateString(pRequest->pDb))); }else{ - cJSON_AddItemToObject(json, "db", cJSON_CreateString("")); + ENV_JSON_FALSE_CHECK(cJSON_AddItemToObject(json, "db", cJSON_CreateString(""))); } char* value = cJSON_PrintUnformatted(json); @@ -162,11 +201,15 @@ static void generateWriteSlowLog(STscObj *pTscObj, SRequestObj *pRequest, int32_ data.clusterId = pTscObj->pAppInfo->clusterId; data.type = SLOW_LOG_WRITE; data.data = value; - if(monitorPutData2MonitorQueue(data) < 0){ + code = monitorPutData2MonitorQueue(data); + if (TSDB_CODE_SUCCESS != code) { taosMemoryFree(value); + goto _end; } +_end: cJSON_Delete(json); + return code; } static bool checkSlowLogExceptDb(SRequestObj *pRequest, char* exceptDb) { @@ -176,6 +219,10 @@ static bool checkSlowLogExceptDb(SRequestObj *pRequest, char* exceptDb) { for (int i = 0; i < taosArrayGetSize(pRequest->dbList); i++) { char *db = taosArrayGet(pRequest->dbList, i); + if (NULL == db) { + tscError("get dbname failed, exceptDb:%s", exceptDb); + return false; + } char *dot = strchr(db, '.'); if (dot != NULL) { db = dot + 1; @@ -215,7 +262,7 @@ static void deregisterRequest(SRequestObj *pRequest) { "us, planCost:%" PRId64 "us, exec:%" PRId64 "us", duration, pRequest->metric.parseCostUs, pRequest->metric.ctgCostUs, pRequest->metric.analyseCostUs, pRequest->metric.planCostUs, pRequest->metric.execCostUs); - atomic_add_fetch_64((int64_t *)&pActivity->insertElapsedTime, duration); + (void)atomic_add_fetch_64((int64_t *)&pActivity->insertElapsedTime, duration); reqType = SLOW_LOG_TYPE_INSERT; } else if (QUERY_NODE_SELECT_STMT == pRequest->stmtType) { tscDebug("query duration %" PRId64 "us: parseCost:%" PRId64 "us, ctgCost:%" PRId64 "us, analyseCost:%" PRId64 @@ -223,11 +270,13 @@ static void deregisterRequest(SRequestObj *pRequest) { duration, pRequest->metric.parseCostUs, pRequest->metric.ctgCostUs, pRequest->metric.analyseCostUs, pRequest->metric.planCostUs, pRequest->metric.execCostUs); - atomic_add_fetch_64((int64_t *)&pActivity->queryElapsedTime, duration); + (void)atomic_add_fetch_64((int64_t *)&pActivity->queryElapsedTime, duration); reqType = SLOW_LOG_TYPE_QUERY; } - nodesSimReleaseAllocator(pRequest->allocatorRefId); + if (TSDB_CODE_SUCCESS != nodesSimReleaseAllocator(pRequest->allocatorRefId)) { + tscError("failed to release allocator"); + } } if(pTscObj->pAppInfo->monitorParas.tsEnableMonitor){ @@ -242,14 +291,16 @@ static void deregisterRequest(SRequestObj *pRequest) { if ((duration >= pTscObj->pAppInfo->monitorParas.tsSlowLogThreshold * 1000000UL || duration >= pTscObj->pAppInfo->monitorParas.tsSlowLogThresholdTest * 1000000UL) && checkSlowLogExceptDb(pRequest, pTscObj->pAppInfo->monitorParas.tsSlowLogExceptDb)) { - atomic_add_fetch_64((int64_t *)&pActivity->numOfSlowQueries, 1); + (void)atomic_add_fetch_64((int64_t *)&pActivity->numOfSlowQueries, 1); if (pTscObj->pAppInfo->monitorParas.tsSlowLogScope & reqType) { taosPrintSlowLog("PID:%d, Conn:%u, QID:0x%" PRIx64 ", Start:%" PRId64 " us, Duration:%" PRId64 "us, SQL:%s", taosGetPId(), pTscObj->connId, pRequest->requestId, pRequest->metric.start, duration, pRequest->sqlstr); if(pTscObj->pAppInfo->monitorParas.tsEnableMonitor){ slowQueryLog(pTscObj->id, pRequest->killed, pRequest->code, duration); - generateWriteSlowLog(pTscObj, pRequest, reqType, duration); + if (TSDB_CODE_SUCCESS != generateWriteSlowLog(pTscObj, pRequest, reqType, duration)) { + tscError("failed to generate write slow log"); + } } } } @@ -289,9 +340,9 @@ static bool clientRpcTfp(int32_t code, tmsg_t msgType) { } // TODO refactor -void *openTransporter(const char *user, const char *auth, int32_t numOfThread) { +int32_t openTransporter(const char *user, const char *auth, int32_t numOfThread, void **pDnodeConn) { SRpcInit rpcInit; - memset(&rpcInit, 0, sizeof(rpcInit)); + (void)memset(&rpcInit, 0, sizeof(rpcInit)); rpcInit.localPort = 0; rpcInit.label = "TSC"; rpcInit.numOfThreads = tsNumOfRpcThreads; @@ -315,15 +366,19 @@ void *openTransporter(const char *user, const char *auth, int32_t numOfThread) { rpcInit.connLimitNum = connLimitNum; rpcInit.timeToGetConn = tsTimeToGetAvailableConn; - taosVersionStrToInt(version, &(rpcInit.compatibilityVer)); - - void *pDnodeConn = rpcOpen(&rpcInit); - if (pDnodeConn == NULL) { - tscError("failed to init connection to server"); - return NULL; + int32_t code = taosVersionStrToInt(version, &(rpcInit.compatibilityVer)); + if (TSDB_CODE_SUCCESS != code) { + tscError("invalid version string."); + return code; } - return pDnodeConn; + *pDnodeConn = rpcOpen(&rpcInit); + if (*pDnodeConn == NULL) { + tscError("failed to init connection to server."); + code = TSDB_CODE_FAILED; + } + + return code; } void destroyAllRequests(SHashObj *pRequests) { @@ -334,7 +389,7 @@ void destroyAllRequests(SHashObj *pRequests) { SRequestObj *pRequest = acquireRequest(*rid); if (pRequest) { destroyRequest(pRequest); - releaseRequest(*rid); + (void)releaseRequest(*rid); // ignore error } pIter = taosHashIterate(pRequests, pIter); @@ -349,7 +404,7 @@ void stopAllRequests(SHashObj *pRequests) { SRequestObj *pRequest = acquireRequest(*rid); if (pRequest) { taos_stop_query(pRequest); - releaseRequest(*rid); + (void)releaseRequest(*rid); // ignore error } pIter = taosHashIterate(pRequests, pIter); @@ -360,18 +415,31 @@ void destroyAppInst(void *info) { SAppInstInfo* pAppInfo = *(SAppInstInfo**)info; tscDebug("destroy app inst mgr %p", pAppInfo); - taosThreadMutexLock(&appInfo.mutex); + int32_t code = taosThreadMutexLock(&appInfo.mutex); + if (TSDB_CODE_SUCCESS != code) { + tscError("failed to lock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code))); + } hbRemoveAppHbMrg(&pAppInfo->pAppHbMgr); - taosThreadMutexUnlock(&appInfo.mutex); + code = taosThreadMutexUnlock(&appInfo.mutex); + if (TSDB_CODE_SUCCESS != code) { + tscError("failed to unlock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code))); + } taosMemoryFreeClear(pAppInfo->instKey); closeTransporter(pAppInfo); - taosThreadMutexLock(&pAppInfo->qnodeMutex); + code = taosThreadMutexLock(&pAppInfo->qnodeMutex); + if (TSDB_CODE_SUCCESS != code) { + tscError("failed to lock qnode mutex, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code))); + } + taosArrayDestroy(pAppInfo->pQnodeList); - taosThreadMutexUnlock(&pAppInfo->qnodeMutex); + code = taosThreadMutexUnlock(&pAppInfo->qnodeMutex); + if (TSDB_CODE_SUCCESS != code) { + tscError("failed to unlock qnode mutex, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code))); + } taosMemoryFree(pAppInfo); } @@ -396,97 +464,111 @@ void destroyTscObj(void *pObj) { pTscObj->pAppInfo->numOfConns); // In any cases, we should not free app inst here. Or an race condition rises. - /*int64_t connNum = */ atomic_sub_fetch_64(&pTscObj->pAppInfo->numOfConns, 1); + /*int64_t connNum = */ (void)atomic_sub_fetch_64(&pTscObj->pAppInfo->numOfConns, 1); - taosThreadMutexDestroy(&pTscObj->mutex); + (void)taosThreadMutexDestroy(&pTscObj->mutex); taosMemoryFree(pTscObj); tscTrace("end to destroy tscObj %" PRIx64 " p:%p", tscId, pTscObj); } -void *createTscObj(const char *user, const char *auth, const char *db, int32_t connType, SAppInstInfo *pAppInfo) { - STscObj *pObj = (STscObj *)taosMemoryCalloc(1, sizeof(STscObj)); - if (NULL == pObj) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; +int32_t createTscObj(const char *user, const char *auth, const char *db, int32_t connType, SAppInstInfo *pAppInfo, + STscObj **pObj) { + *pObj = (STscObj *)taosMemoryCalloc(1, sizeof(STscObj)); + if (NULL == *pObj) { + return TSDB_CODE_OUT_OF_MEMORY; } - pObj->pRequests = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK); - if (NULL == pObj->pRequests) { - taosMemoryFree(pObj); - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; + (*pObj)->pRequests = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_ENTRY_LOCK); + if (NULL == (*pObj)->pRequests) { + taosMemoryFree(*pObj); + return terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY; } - pObj->connType = connType; - pObj->pAppInfo = pAppInfo; - pObj->appHbMgrIdx = pAppInfo->pAppHbMgr->idx; - tstrncpy(pObj->user, user, sizeof(pObj->user)); - memcpy(pObj->pass, auth, TSDB_PASSWORD_LEN); + (*pObj)->connType = connType; + (*pObj)->pAppInfo = pAppInfo; + (*pObj)->appHbMgrIdx = pAppInfo->pAppHbMgr->idx; + tstrncpy((*pObj)->user, user, sizeof((*pObj)->user)); + (void)memcpy((*pObj)->pass, auth, TSDB_PASSWORD_LEN); if (db != NULL) { - tstrncpy(pObj->db, db, tListLen(pObj->db)); + tstrncpy((*pObj)->db, db, tListLen((*pObj)->db)); } - taosThreadMutexInit(&pObj->mutex, NULL); - pObj->id = taosAddRef(clientConnRefPool, pObj); + TSC_ERR_RET(taosThreadMutexInit(&(*pObj)->mutex, NULL)); - atomic_add_fetch_64(&pObj->pAppInfo->numOfConns, 1); + int32_t code = TSDB_CODE_SUCCESS; - tscDebug("connObj created, 0x%" PRIx64 ",p:%p", pObj->id, pObj); - return pObj; + (*pObj)->id = taosAddRef(clientConnRefPool, *pObj); + if ((*pObj)->id < 0) { + tscError("failed to add object to clientConnRefPool"); + code = terrno; + taosMemoryFree(*pObj); + return code; + } + + (void)atomic_add_fetch_64(&(*pObj)->pAppInfo->numOfConns, 1); + + tscDebug("connObj created, 0x%" PRIx64 ",p:%p", (*pObj)->id, *pObj); + return code; } STscObj *acquireTscObj(int64_t rid) { return (STscObj *)taosAcquireRef(clientConnRefPool, rid); } -int32_t releaseTscObj(int64_t rid) { return taosReleaseRef(clientConnRefPool, rid); } +void releaseTscObj(int64_t rid) { + int32_t code = taosReleaseRef(clientConnRefPool, rid); + if (TSDB_CODE_SUCCESS != code) { + tscWarn("failed to release TscObj, code:%s", tstrerror(code)); + } +} -void *createRequest(uint64_t connId, int32_t type, int64_t reqid) { - SRequestObj *pRequest = (SRequestObj *)taosMemoryCalloc(1, sizeof(SRequestObj)); - if (NULL == pRequest) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; +int32_t createRequest(uint64_t connId, int32_t type, int64_t reqid, SRequestObj **pRequest) { + int32_t code = TSDB_CODE_SUCCESS; + *pRequest = (SRequestObj *)taosMemoryCalloc(1, sizeof(SRequestObj)); + if (NULL == *pRequest) { + return TSDB_CODE_OUT_OF_MEMORY; } STscObj *pTscObj = acquireTscObj(connId); if (pTscObj == NULL) { - taosMemoryFree(pRequest); - terrno = TSDB_CODE_TSC_DISCONNECTED; - return NULL; + code = TSDB_CODE_TSC_DISCONNECTED; + goto _return; } SSyncQueryParam *interParam = taosMemoryCalloc(1, sizeof(SSyncQueryParam)); if (interParam == NULL) { releaseTscObj(connId); - doDestroyRequest(pRequest); - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; + code = TSDB_CODE_OUT_OF_MEMORY; + goto _return; } - tsem_init(&interParam->sem, 0, 0); - interParam->pRequest = pRequest; - pRequest->body.interParam = interParam; + TSC_ERR_JRET(tsem_init(&interParam->sem, 0, 0)); + interParam->pRequest = *pRequest; + (*pRequest)->body.interParam = interParam; - pRequest->resType = RES_TYPE__QUERY; - pRequest->requestId = reqid == 0 ? generateRequestId() : reqid; - pRequest->metric.start = taosGetTimestampUs(); + (*pRequest)->resType = RES_TYPE__QUERY; + (*pRequest)->requestId = reqid == 0 ? generateRequestId() : reqid; + (*pRequest)->metric.start = taosGetTimestampUs(); - pRequest->body.resInfo.convertUcs4 = true; // convert ucs4 by default - pRequest->type = type; - pRequest->allocatorRefId = -1; + (*pRequest)->body.resInfo.convertUcs4 = true; // convert ucs4 by default + (*pRequest)->type = type; + (*pRequest)->allocatorRefId = -1; - pRequest->pDb = getDbOfConnection(pTscObj); - pRequest->pTscObj = pTscObj; - pRequest->inCallback = false; + (*pRequest)->pDb = getDbOfConnection(pTscObj); + (*pRequest)->pTscObj = pTscObj; + (*pRequest)->inCallback = false; - pRequest->msgBuf = taosMemoryCalloc(1, ERROR_MSG_BUF_DEFAULT_SIZE); - pRequest->msgBufLen = ERROR_MSG_BUF_DEFAULT_SIZE; - tsem_init(&pRequest->body.rspSem, 0, 0); - - if (registerRequest(pRequest, pTscObj)) { - doDestroyRequest(pRequest); - return NULL; + (*pRequest)->msgBuf = taosMemoryCalloc(1, ERROR_MSG_BUF_DEFAULT_SIZE); + if (NULL == (*pRequest)->msgBuf) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _return; } + (*pRequest)->msgBufLen = ERROR_MSG_BUF_DEFAULT_SIZE; + TSC_ERR_JRET(tsem_init(&(*pRequest)->body.rspSem, 0, 0)); + TSC_ERR_JRET(registerRequest(*pRequest, pTscObj)); - return pRequest; + return TSDB_CODE_SUCCESS; +_return: + doDestroyRequest(*pRequest); + return code; } void doFreeReqResultInfo(SReqResultInfo *pResInfo) { @@ -521,12 +603,12 @@ int64_t removeFromMostPrevReq(SRequestObj* pRequest) { pTmp = acquireRequest(pTmp->relation.prevRefId); if (pTmp) { mostPrevReqRefId = pTmp->self; - releaseRequest(mostPrevReqRefId); + (void)releaseRequest(mostPrevReqRefId); // ignore error } else { break; } } - removeRequest(mostPrevReqRefId); + (void)removeRequest(mostPrevReqRefId); // ignore error return mostPrevReqRefId; } @@ -534,8 +616,8 @@ void destroyNextReq(int64_t nextRefId) { if (nextRefId) { SRequestObj* pObj = acquireRequest(nextRefId); if (pObj) { - releaseRequest(nextRefId); - releaseRequest(nextRefId); + (void)releaseRequest(nextRefId); // ignore error + (void)releaseRequest(nextRefId); // ignore error } } } @@ -555,7 +637,7 @@ void destroySubRequests(SRequestObj *pRequest) { pTmp = acquireRequest(tmpRefId); if (pTmp) { pReqList[++reqIdx] = pTmp; - releaseRequest(tmpRefId); + (void)releaseRequest(tmpRefId); // ignore error } else { tscError("prev req ref 0x%" PRIx64 " is not there", tmpRefId); break; @@ -563,7 +645,7 @@ void destroySubRequests(SRequestObj *pRequest) { } for (int32_t i = reqIdx; i >= 0; i--) { - removeRequest(pReqList[i]->self); + (void)removeRequest(pReqList[i]->self); // ignore error } tmpRefId = pRequest->relation.nextRefId; @@ -571,8 +653,8 @@ void destroySubRequests(SRequestObj *pRequest) { pTmp = acquireRequest(tmpRefId); if (pTmp) { tmpRefId = pTmp->relation.nextRefId; - removeRequest(pTmp->self); - releaseRequest(pTmp->self); + (void)removeRequest(pTmp->self); // ignore error + (void)releaseRequest(pTmp->self); // ignore error } else { tscError("next req ref 0x%" PRIx64 " is not there", tmpRefId); break; @@ -592,8 +674,10 @@ void doDestroyRequest(void *p) { int64_t nextReqRefId = pRequest->relation.nextRefId; - taosHashRemove(pRequest->pTscObj->pRequests, &pRequest->self, sizeof(pRequest->self)); - + int32_t code = taosHashRemove(pRequest->pTscObj->pRequests, &pRequest->self, sizeof(pRequest->self)); + if (TSDB_CODE_SUCCESS != code) { + tscError("failed to remove request from hash, code:%s", tstrerror(code)); + } schedulerFreeJob(&pRequest->body.queryJob, 0); destorySqlCallbackWrapper(pRequest->pWrapper); @@ -601,7 +685,7 @@ void doDestroyRequest(void *p) { taosMemoryFreeClear(pRequest->msgBuf); doFreeReqResultInfo(&pRequest->body.resInfo); - tsem_destroy(&pRequest->body.rspSem); + (void)tsem_destroy(&pRequest->body.rspSem); taosArrayDestroy(pRequest->tableList); taosArrayDestroy(pRequest->targetTableList); @@ -615,13 +699,15 @@ void doDestroyRequest(void *p) { taosMemoryFreeClear(pRequest->pDb); taosArrayDestroy(pRequest->dbList); if (pRequest->body.interParam) { - tsem_destroy(&((SSyncQueryParam *)pRequest->body.interParam)->sem); + (void)tsem_destroy(&((SSyncQueryParam *)pRequest->body.interParam)->sem); } taosMemoryFree(pRequest->body.interParam); if (TSDB_CODE_SUCCESS == nodesSimAcquireAllocator(pRequest->allocatorRefId)) { qDestroyQuery(pRequest->pQuery); - nodesSimReleaseAllocator(pRequest->allocatorRefId); + if (TSDB_CODE_SUCCESS != nodesSimReleaseAllocator(pRequest->allocatorRefId)) { + tscError("failed to release allocator"); + } } nodesDestroyAllocator(pRequest->allocatorRefId); @@ -638,7 +724,7 @@ void destroyRequest(SRequestObj *pRequest) { } taos_stop_query(pRequest); - removeFromMostPrevReq(pRequest); + (void)removeFromMostPrevReq(pRequest); } void taosStopQueryImpl(SRequestObj *pRequest) { @@ -677,7 +763,7 @@ void stopAllQueries(SRequestObj *pRequest) { for (int32_t i = reqIdx; i >= 0; i--) { taosStopQueryImpl(pReqList[i]); - releaseRequest(pReqList[i]->self); + (void)releaseRequest(pReqList[i]->self); // ignore error } taosStopQueryImpl(pRequest); @@ -688,7 +774,7 @@ void stopAllQueries(SRequestObj *pRequest) { if (pTmp) { tmpRefId = pTmp->relation.nextRefId; taosStopQueryImpl(pTmp); - releaseRequest(pTmp->self); + (void)releaseRequest(pTmp->self); // ignore error } else { tscError("next req ref 0x%" PRIx64 " is not there", tmpRefId); break; @@ -701,7 +787,7 @@ void crashReportThreadFuncUnexpectedStopped(void) { atomic_store_32(&clientStop, static void *tscCrashReportThreadFp(void *param) { setThreadName("client-crashReport"); char filepath[PATH_MAX] = {0}; - snprintf(filepath, sizeof(filepath), "%s%s.taosCrashLog", tsLogDir, TD_DIRSEP); + (void)snprintf(filepath, sizeof(filepath), "%s%s.taosCrashLog", tsLogDir, TD_DIRSEP); char *pMsg = NULL; int64_t msgLen = 0; TdFilePtr pFile = NULL; @@ -770,20 +856,27 @@ static void *tscCrashReportThreadFp(void *param) { int32_t tscCrashReportInit() { if (!tsEnableCrashReport) { - return 0; + return TSDB_CODE_SUCCESS; } - + int32_t code = TSDB_CODE_SUCCESS; TdThreadAttr thAttr; - taosThreadAttrInit(&thAttr); - taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE); + TSC_ERR_JRET(taosThreadAttrInit(&thAttr)); + TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE)); TdThread crashReportThread; if (taosThreadCreate(&crashReportThread, &thAttr, tscCrashReportThreadFp, NULL) != 0) { tscError("failed to create crashReport thread since %s", strerror(errno)); - return -1; + terrno = TAOS_SYSTEM_ERROR(errno); + TSC_ERR_RET(errno); } - taosThreadAttrDestroy(&thAttr); - return 0; + (void)taosThreadAttrDestroy(&thAttr); +_return: + if (code) { + terrno = TAOS_SYSTEM_ERROR(errno); + TSC_ERR_RET(terrno); + } + + return TSDB_CODE_SUCCESS; } void tscStopCrashReport() { @@ -842,6 +935,11 @@ void taos_init_imp(void) { appInfo.startTime = taosGetTimestampMs(); appInfo.pInstMap = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_ENTRY_LOCK); appInfo.pInstMapByClusterId = taosHashInit(4, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_ENTRY_LOCK); + if (NULL == appInfo.pInstMap || NULL == appInfo.pInstMapByClusterId) { + tscError("failed to allocate memory when init appInfo"); + tscInitRes = TSDB_CODE_OUT_OF_MEMORY; + return; + } taosHashSetFreeFp(appInfo.pInstMap, destroyAppInst); deltaToUtcInitOnce(); @@ -849,7 +947,7 @@ void taos_init_imp(void) { #ifdef CUS_PROMPT snprintf(logDirName, 64, "%slog", CUS_PROMPT); #else - snprintf(logDirName, 64, "taoslog"); + (void)snprintf(logDirName, 64, "taoslog"); #endif if (taosCreateLog(logDirName, 10, configDir, NULL, NULL, NULL, NULL, 1) != 0) { printf(" WARING: Create %s failed:%s. configDir=%s\n", logDirName, strerror(errno), configDir); @@ -857,24 +955,12 @@ void taos_init_imp(void) { return; } - if (taosInitCfg(configDir, NULL, NULL, NULL, NULL, 1) != 0) { - tscInitRes = -1; - return; - } + ENV_ERR_RET(taosInitCfg(configDir, NULL, NULL, NULL, NULL, 1), "failed to init cfg"); initQueryModuleMsgHandle(); - - if (taosConvInit() != 0) { - tscInitRes = -1; - tscError("failed to init conv"); - return; - } - if (monitorInit() != 0){ - tscInitRes = -1; - tscError("failed to init monitor"); - return; - } - rpcInit(); + ENV_ERR_RET(taosConvInit(), "failed to init conv"); + ENV_ERR_RET(monitorInit(), "failed to init monitor"); + ENV_ERR_RET(rpcInit(), "failed to init rpc"); if (InitRegexCache() != 0) { tscInitRes = -1; @@ -883,34 +969,25 @@ void taos_init_imp(void) { } SCatalogCfg cfg = {.maxDBCacheNum = 100, .maxTblCacheNum = 100}; - catalogInit(&cfg); + ENV_ERR_RET(catalogInit(&cfg), "failed to init catalog"); + ENV_ERR_RET(schedulerInit(), "failed to init scheduler"); - schedulerInit(); tscDebug("starting to initialize TAOS driver"); #ifndef WINDOWS taosSetCoreDump(true); #endif - if (initTaskQueue() != 0){ - tscInitRes = -1; - tscError("failed to init task queue"); - return; - } - if (fmFuncMgtInit() != TSDB_CODE_SUCCESS) { - tscInitRes = -1; - tscError("failed to init function manager"); - return; - } - nodesInitAllocatorSet(); + ENV_ERR_RET(initTaskQueue(), "failed to init task queue"); + ENV_ERR_RET(fmFuncMgtInit(), "failed to init funcMgt"); + ENV_ERR_RET(nodesInitAllocatorSet(), "failed to init allocator set"); clientConnRefPool = taosOpenRef(200, destroyTscObj); clientReqRefPool = taosOpenRef(40960, doDestroyRequest); - taosGetAppName(appInfo.appName, NULL); - taosThreadMutexInit(&appInfo.mutex, NULL); - - tscCrashReportInit(); + ENV_ERR_RET(taosGetAppName(appInfo.appName, NULL), "failed to get app name"); + ENV_ERR_RET(taosThreadMutexInit(&appInfo.mutex, NULL), "failed to init thread mutex"); + ENV_ERR_RET(tscCrashReportInit(), "failed to init crash report"); tscDebug("client is initialized successfully"); } @@ -957,7 +1034,7 @@ int taos_options_imp(TSDB_OPTION option, const char *str) { return -1; } newstr[0] = '"'; - memcpy(newstr+1, str, len); + (void)memcpy(newstr+1, str, len); newstr[len + 1] = '"'; newstr[len + 2] = '\0'; str = newstr; diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index e45ca2b872..0b7de322f5 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -44,28 +44,34 @@ static int32_t hbMqHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) { retur static int32_t hbProcessUserAuthInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) { - int32_t code = 0; + int32_t code = TSDB_CODE_SUCCESS; SUserAuthBatchRsp batchRsp = {0}; if (tDeserializeSUserAuthBatchRsp(value, valueLen, &batchRsp) != 0) { - terrno = TSDB_CODE_INVALID_MSG; - return -1; + return TSDB_CODE_INVALID_MSG; } int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray); for (int32_t i = 0; i < numOfBatchs; ++i) { SGetUserAuthRsp *rsp = taosArrayGet(batchRsp.pArray, i); + if (NULL == rsp) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _return; + } tscDebug("hb to update user auth, user:%s, version:%d", rsp->user, rsp->version); - catalogUpdateUserAuthInfo(pCatalog, rsp); + TSC_ERR_JRET(catalogUpdateUserAuthInfo(pCatalog, rsp)); } - if (numOfBatchs > 0) hbUpdateUserAuthInfo(pAppHbMgr, &batchRsp); + if (numOfBatchs > 0) { + TSC_ERR_JRET(hbUpdateUserAuthInfo(pAppHbMgr, &batchRsp)); + } - atomic_val_compare_exchange_8(&pAppHbMgr->connHbFlag, 1, 2); + (void)atomic_val_compare_exchange_8(&pAppHbMgr->connHbFlag, 1, 2); +_return: taosArrayDestroy(batchRsp.pArray); - return TSDB_CODE_SUCCESS; + return code; } static int32_t hbUpdateUserAuthInfo(SAppHbMgr *pAppHbMgr, SUserAuthBatchRsp *batchRsp) { @@ -211,6 +217,10 @@ static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog int32_t numOfBatchs = taosArrayGetSize(batchRsp.pArray); for (int32_t i = 0; i < numOfBatchs; ++i) { SDbHbRsp *rsp = taosArrayGet(batchRsp.pArray, i); + if (NULL == rsp) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _return; + } if (rsp->useDbRsp) { tscDebug("hb use db rsp, db:%s, vgVersion:%d, stateTs:%" PRId64 ", uid:%" PRIx64, rsp->useDbRsp->db, rsp->useDbRsp->vgVersion, rsp->useDbRsp->stateTs, rsp->useDbRsp->uid); @@ -227,7 +237,7 @@ static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog tscDebug("hb to update db vgInfo, db:%s", rsp->useDbRsp->db); - catalogUpdateDBVgInfo(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid, vgInfo); + TSC_ERR_JRET(catalogUpdateDBVgInfo(pCatalog, rsp->useDbRsp->db, rsp->useDbRsp->uid, vgInfo)); if (IS_SYS_DBNAME(rsp->useDbRsp->db)) { code = hbGenerateVgInfoFromRsp(&vgInfo, rsp->useDbRsp); @@ -235,23 +245,28 @@ static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog goto _return; } - catalogUpdateDBVgInfo(pCatalog, - (rsp->useDbRsp->db[0] == 'i') ? TSDB_PERFORMANCE_SCHEMA_DB : TSDB_INFORMATION_SCHEMA_DB, - rsp->useDbRsp->uid, vgInfo); + TSC_ERR_JRET(catalogUpdateDBVgInfo(pCatalog, + (rsp->useDbRsp->db[0] == 'i') ? + TSDB_PERFORMANCE_SCHEMA_DB : + TSDB_INFORMATION_SCHEMA_DB, + rsp->useDbRsp->uid, vgInfo)); } } } if (rsp->cfgRsp) { tscDebug("hb db cfg rsp, db:%s, cfgVersion:%d", rsp->cfgRsp->db, rsp->cfgRsp->cfgVersion); - catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp); + code = catalogUpdateDbCfg(pCatalog, rsp->cfgRsp->db, rsp->cfgRsp->dbId, rsp->cfgRsp); rsp->cfgRsp = NULL; } if (rsp->pTsmaRsp) { if (rsp->pTsmaRsp->pTsmas) { for (int32_t i = 0; i < rsp->pTsmaRsp->pTsmas->size; ++i) { STableTSMAInfo *pTsma = taosArrayGetP(rsp->pTsmaRsp->pTsmas, i); - catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion); + if (NULL == pTsma) { + TSC_ERR_JRET(TSDB_CODE_OUT_OF_RANGE); + } + TSC_ERR_JRET(catalogAsyncUpdateTSMA(pCatalog, &pTsma, rsp->dbTsmaVersion)); } taosArrayClear(rsp->pTsmaRsp->pTsmas); } @@ -265,7 +280,7 @@ _return: } static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) { - int32_t code = 0; + int32_t code = TSDB_CODE_SUCCESS; SSTbHbRsp hbRsp = {0}; if (tDeserializeSSTbHbRsp(value, valueLen, &hbRsp) != 0) { @@ -276,10 +291,13 @@ static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalo int32_t numOfMeta = taosArrayGetSize(hbRsp.pMetaRsp); for (int32_t i = 0; i < numOfMeta; ++i) { STableMetaRsp *rsp = taosArrayGet(hbRsp.pMetaRsp, i); - + if (NULL == rsp) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _return; + } if (rsp->numOfColumns < 0) { tscDebug("hb to remove stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName); - catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid); + TSC_ERR_JRET(catalogRemoveStbMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->stbName, rsp->suid)); } else { tscDebug("hb to update stb, db:%s, stb:%s", rsp->dbFName, rsp->stbName); if (rsp->pSchemas[0].colId != PRIMARYKEY_TIMESTAMP_COL_ID) { @@ -288,22 +306,26 @@ static int32_t hbProcessStbInfoRsp(void *value, int32_t valueLen, struct SCatalo return TSDB_CODE_TSC_INVALID_VALUE; } - catalogAsyncUpdateTableMeta(pCatalog, rsp); + TSC_ERR_JRET(catalogAsyncUpdateTableMeta(pCatalog, rsp)); } } int32_t numOfIndex = taosArrayGetSize(hbRsp.pIndexRsp); for (int32_t i = 0; i < numOfIndex; ++i) { STableIndexRsp *rsp = taosArrayGet(hbRsp.pIndexRsp, i); - - catalogUpdateTableIndex(pCatalog, rsp); + if (NULL == rsp) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _return; + } + TSC_ERR_JRET(catalogUpdateTableIndex(pCatalog, rsp)); } +_return: taosArrayDestroy(hbRsp.pIndexRsp); hbRsp.pIndexRsp = NULL; tFreeSSTbHbRsp(&hbRsp); - return TSDB_CODE_SUCCESS; + return code; } static int32_t hbProcessDynViewRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) { @@ -320,7 +342,7 @@ static void hbFreeSViewMetaInRsp(void *p) { } static int32_t hbProcessViewInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) { - int32_t code = 0; + int32_t code = TSDB_CODE_SUCCESS; SViewHbRsp hbRsp = {0}; if (tDeserializeSViewHbRsp(value, valueLen, &hbRsp) != 0) { @@ -332,20 +354,25 @@ static int32_t hbProcessViewInfoRsp(void *value, int32_t valueLen, struct SCatal int32_t numOfMeta = taosArrayGetSize(hbRsp.pViewRsp); for (int32_t i = 0; i < numOfMeta; ++i) { SViewMetaRsp *rsp = taosArrayGetP(hbRsp.pViewRsp, i); - + if (NULL == rsp) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _return; + } if (rsp->numOfCols < 0) { tscDebug("hb to remove view, db:%s, view:%s", rsp->dbFName, rsp->name); - catalogRemoveViewMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->name, rsp->viewId); + code = catalogRemoveViewMeta(pCatalog, rsp->dbFName, rsp->dbId, rsp->name, rsp->viewId); tFreeSViewMetaRsp(rsp); taosMemoryFreeClear(rsp); } else { tscDebug("hb to update view, db:%s, view:%s", rsp->dbFName, rsp->name); - catalogUpdateViewMeta(pCatalog, rsp); + code = catalogUpdateViewMeta(pCatalog, rsp); } + TSC_ERR_JRET(code); } +_return: taosArrayDestroy(hbRsp.pViewRsp); - return TSDB_CODE_SUCCESS; + return code; } static int32_t hbprocessTSMARsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) { @@ -363,30 +390,38 @@ static int32_t hbprocessTSMARsp(void *value, int32_t valueLen, struct SCatalog * if (!pTsmaInfo->pFuncs) { tscDebug("hb to remove tsma: %s.%s", pTsmaInfo->dbFName, pTsmaInfo->name); - catalogRemoveTSMA(pCatalog, pTsmaInfo); + code = catalogRemoveTSMA(pCatalog, pTsmaInfo); tFreeAndClearTableTSMAInfo(pTsmaInfo); } else { tscDebug("hb to update tsma: %s.%s", pTsmaInfo->dbFName, pTsmaInfo->name); - catalogUpdateTSMA(pCatalog, &pTsmaInfo); + code = catalogUpdateTSMA(pCatalog, &pTsmaInfo); tFreeAndClearTableTSMAInfo(pTsmaInfo); } + TSC_ERR_JRET(code); } +_return: taosArrayDestroy(hbRsp.pTsmas); - return TSDB_CODE_SUCCESS; + return code; } static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *pCatalog, SAppHbMgr *pAppHbMgr) { for (int32_t i = 0; i < kvNum; ++i) { SKv *kv = taosArrayGet(pKvs, i); + if (NULL == kv) { + tscError("invalid hb kv, idx:%d", i); + continue; + } switch (kv->key) { case HEARTBEAT_KEY_USER_AUTHINFO: { if (kv->valueLen <= 0 || NULL == kv->value) { tscError("invalid hb user auth info, len:%d, value:%p", kv->valueLen, kv->value); break; } - - hbProcessUserAuthInfoRsp(kv->value, kv->valueLen, pCatalog, pAppHbMgr); + if (TSDB_CODE_SUCCESS != hbProcessUserAuthInfoRsp(kv->value, kv->valueLen, pCatalog, pAppHbMgr)) { + tscError("process user auth info response faild, len:%d, value:%p", kv->valueLen, kv->value); + break; + } break; } case HEARTBEAT_KEY_DBINFO: { @@ -394,8 +429,10 @@ static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *p tscError("invalid hb db info, len:%d, value:%p", kv->valueLen, kv->value); break; } - - hbProcessDBInfoRsp(kv->value, kv->valueLen, pCatalog); + if (TSDB_CODE_SUCCESS != hbProcessDBInfoRsp(kv->value, kv->valueLen, pCatalog)) { + tscError("process db info response faild, len:%d, value:%p", kv->valueLen, kv->value); + break; + } break; } case HEARTBEAT_KEY_STBINFO: { @@ -403,8 +440,10 @@ static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *p tscError("invalid hb stb info, len:%d, value:%p", kv->valueLen, kv->value); break; } - - hbProcessStbInfoRsp(kv->value, kv->valueLen, pCatalog); + if (TSDB_CODE_SUCCESS != hbProcessStbInfoRsp(kv->value, kv->valueLen, pCatalog)) { + tscError("process stb info response faild, len:%d, value:%p", kv->valueLen, kv->value); + break; + } break; } #ifdef TD_ENTERPRISE @@ -413,8 +452,10 @@ static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *p tscError("invalid dyn view info, len:%d, value:%p", kv->valueLen, kv->value); break; } - - hbProcessDynViewRsp(kv->value, kv->valueLen, pCatalog); + if (TSDB_CODE_SUCCESS != hbProcessDynViewRsp(kv->value, kv->valueLen, pCatalog)) { + tscError("Process dyn view response failed, len: %d, value: %p", kv->valueLen, kv->value); + break; + } break; } case HEARTBEAT_KEY_VIEWINFO: { @@ -422,8 +463,10 @@ static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *p tscError("invalid view info, len:%d, value:%p", kv->valueLen, kv->value); break; } - - hbProcessViewInfoRsp(kv->value, kv->valueLen, pCatalog); + if (TSDB_CODE_SUCCESS != hbProcessViewInfoRsp(kv->value, kv->valueLen, pCatalog)) { + tscError("Process view info response failed, len: %d, value: %p", kv->valueLen, kv->value); + break; + } break; } #endif @@ -431,7 +474,9 @@ static void hbProcessQueryRspKvs(int32_t kvNum, SArray *pKvs, struct SCatalog *p if (kv->valueLen <= 0 || !kv->value) { tscError("Invalid tsma info, len: %d, value: %p", kv->valueLen, kv->value); } - hbprocessTSMARsp(kv->value, kv->valueLen, pCatalog); + if (TSDB_CODE_SUCCESS != hbprocessTSMARsp(kv->value, kv->valueLen, pCatalog)) { + tscError("Process tsma info response failed, len: %d, value: %p", kv->valueLen, kv->value); + } break; } default: @@ -478,7 +523,7 @@ static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) { tscDebug("request 0x%" PRIx64 " not exist to kill", pRsp->query->killRid); } else { taos_stop_query((TAOS_RES *)pRequest); - releaseRequest(pRsp->query->killRid); + (void)releaseRequest(pRsp->query->killRid); } } @@ -487,7 +532,9 @@ static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) { } if (pRsp->query->pQnodeList) { - updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList); + if (TSDB_CODE_SUCCESS != updateQnodeList(pTscObj->pAppInfo, pRsp->query->pQnodeList)) { + tscWarn("update qnode list failed"); + } } releaseTscObj(pRsp->connKey.tscRid); @@ -508,6 +555,7 @@ static int32_t hbQueryHbRspHandle(SAppHbMgr *pAppHbMgr, SClientHbRsp *pRsp) { } } + taosHashRelease(pAppHbMgr->activeInfo, pReq); return TSDB_CODE_SUCCESS; @@ -522,8 +570,10 @@ static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) { int32_t idx = *(int32_t *)param; SClientHbBatchRsp pRsp = {0}; if (TSDB_CODE_SUCCESS == code) { - tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp); - + code = tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp); + if (TSDB_CODE_SUCCESS != code) { + tscError("deserialize hb rsp failed"); + } int32_t now = taosGetTimestampSec(); int32_t delta = abs(now - pRsp.svrTimestamp); if (delta > timestampDeltaLimit) { @@ -534,27 +584,27 @@ static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) { int32_t rspNum = taosArrayGetSize(pRsp.rsps); - taosThreadMutexLock(&clientHbMgr.lock); + (void)taosThreadMutexLock(&clientHbMgr.lock); SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, idx); if (pAppHbMgr == NULL) { - taosThreadMutexUnlock(&clientHbMgr.lock); + (void)taosThreadMutexUnlock(&clientHbMgr.lock); tscError("appHbMgr not exist, idx:%d", idx); taosMemoryFree(pMsg->pData); taosMemoryFree(pMsg->pEpSet); tFreeClientHbBatchRsp(&pRsp); - return -1; + return TSDB_CODE_OUT_OF_RANGE; } SAppInstInfo *pInst = pAppHbMgr->pAppInstInfo; if (code != 0) { pInst->onlineDnodes = pInst->totalDnodes ? 0 : -1; tscDebug("hb rsp error %s, update server status %d/%d", tstrerror(code), pInst->onlineDnodes, pInst->totalDnodes); - taosThreadMutexUnlock(&clientHbMgr.lock); + (void)taosThreadMutexUnlock(&clientHbMgr.lock); taosMemoryFree(pMsg->pData); taosMemoryFree(pMsg->pEpSet); tFreeClientHbBatchRsp(&pRsp); - return -1; + return code; } pInst->monitorParas = pRsp.monitorParas; @@ -565,7 +615,7 @@ static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) { tscDebug("hb got %d rsp, %d empty rsp received before", rspNum, atomic_val_compare_exchange_32(&emptyRspNum, emptyRspNum, 0)); } else { - atomic_add_fetch_32(&emptyRspNum, 1); + (void)atomic_add_fetch_32(&emptyRspNum, 1); } for (int32_t i = 0; i < rspNum; ++i) { @@ -576,7 +626,7 @@ static int32_t hbAsyncCallBack(void *param, SDataBuf *pMsg, int32_t code) { } } - taosThreadMutexUnlock(&clientHbMgr.lock); + (void)taosThreadMutexUnlock(&clientHbMgr.lock); tFreeClientHbBatchRsp(&pRsp); @@ -601,7 +651,7 @@ int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) { } if (pRequest->killed || 0 == pRequest->body.queryJob) { - releaseRequest(*rid); + (void)releaseRequest(*rid); pIter = taosHashIterate(pObj->pRequests, pIter); continue; } @@ -613,14 +663,19 @@ int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) { desc.reqRid = pRequest->self; desc.stableQuery = pRequest->stableQuery; desc.isSubQuery = pRequest->isSubReq; - taosGetFqdn(desc.fqdn); + code = taosGetFqdn(desc.fqdn); + if (TSDB_CODE_SUCCESS != code) { + (void)releaseRequest(*rid); + tscError("get fqdn failed"); + return TSDB_CODE_FAILED; + } desc.subPlanNum = pRequest->body.subplanNum; if (desc.subPlanNum) { desc.subDesc = taosArrayInit(desc.subPlanNum, sizeof(SQuerySubDesc)); if (NULL == desc.subDesc) { - releaseRequest(*rid); - return TSDB_CODE_OUT_OF_MEMORY; + (void)releaseRequest(*rid); + return terrno; } code = schedulerGetTasksStatus(pRequest->body.queryJob, desc.subDesc); @@ -633,20 +688,23 @@ int32_t hbBuildQueryDesc(SQueryHbReqBasic *hbBasic, STscObj *pObj) { desc.subDesc = NULL; } - releaseRequest(*rid); - taosArrayPush(hbBasic->queryDesc, &desc); + (void)releaseRequest(*rid); + if (NULL == taosArrayPush(hbBasic->queryDesc, &desc)) { + taosArrayDestroy(desc.subDesc); + return TSDB_CODE_OUT_OF_MEMORY; + } pIter = taosHashIterate(pObj->pRequests, pIter); } - return TSDB_CODE_SUCCESS; + return code; } int32_t hbGetQueryBasicInfo(SClientHbKey *connKey, SClientHbReq *req) { STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid); if (NULL == pTscObj) { tscWarn("tscObj rid %" PRIx64 " not exist", connKey->tscRid); - return TSDB_CODE_APP_ERROR; + return terrno; } SQueryHbReqBasic *hbBasic = (SQueryHbReqBasic *)taosMemoryCalloc(1, sizeof(SQueryHbReqBasic)); @@ -671,7 +729,7 @@ int32_t hbGetQueryBasicInfo(SClientHbKey *connKey, SClientHbReq *req) { tscWarn("taosArrayInit %d queryDesc failed", numOfQueries); releaseTscObj(connKey->tscRid); taosMemoryFree(hbBasic); - return TSDB_CODE_OUT_OF_MEMORY; + return terrno; } int32_t code = hbBuildQueryDesc(hbBasic, pTscObj); @@ -694,7 +752,7 @@ static int32_t hbGetUserAuthInfo(SClientHbKey *connKey, SHbParam *param, SClient STscObj *pTscObj = (STscObj *)acquireTscObj(connKey->tscRid); if (!pTscObj) { tscWarn("tscObj rid %" PRIx64 " not exist", connKey->tscRid); - return TSDB_CODE_APP_ERROR; + return terrno; } int32_t code = 0; @@ -716,7 +774,7 @@ static int32_t hbGetUserAuthInfo(SClientHbKey *connKey, SHbParam *param, SClient SUserAuthVersion *qUserAuth = (SUserAuthVersion *)taosMemoryRealloc(pKv->value, (userNum + 1) * sizeof(SUserAuthVersion)); if (qUserAuth) { - strncpy((qUserAuth + userNum)->user, pTscObj->user, TSDB_USER_LEN); + (void)strncpy((qUserAuth + userNum)->user, pTscObj->user, TSDB_USER_LEN); (qUserAuth + userNum)->version = htonl(-1); // force get userAuthInfo pKv->value = qUserAuth; pKv->valueLen += sizeof(SUserAuthVersion); @@ -742,6 +800,10 @@ static int32_t hbGetUserAuthInfo(SClientHbKey *connKey, SHbParam *param, SClient if (!req->info) { req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); + if (NULL == req->info) { + code = terrno; + goto _return; + } } if (taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)) != 0) { @@ -789,9 +851,17 @@ int32_t hbGetExpiredUserInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, S if (NULL == req->info) { req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); + if (NULL == req->info) { + taosMemoryFree(users); + return terrno; + } } - taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); + code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); + if (TSDB_CODE_SUCCESS != code) { + taosMemoryFree(users); + return code; + } return TSDB_CODE_SUCCESS; } @@ -835,9 +905,17 @@ int32_t hbGetExpiredDBInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SCl if (NULL == req->info) { req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); + if (NULL == req->info) { + taosMemoryFree(dbs); + return terrno; + } } - taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); + code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); + if (TSDB_CODE_SUCCESS != code) { + taosMemoryFree(dbs); + return code; + } return TSDB_CODE_SUCCESS; } @@ -875,9 +953,17 @@ int32_t hbGetExpiredStbInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SC if (NULL == req->info) { req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); + if (NULL == req->info) { + taosMemoryFree(stbs); + return terrno; + } } - taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); + code = taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); + if (TSDB_CODE_SUCCESS != code) { + taosMemoryFree(stbs); + return code; + } return TSDB_CODE_SUCCESS; } @@ -888,12 +974,7 @@ int32_t hbGetExpiredViewInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, S int32_t code = 0; SDynViewVersion *pDynViewVer = NULL; - code = catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer); - if (TSDB_CODE_SUCCESS != code) { - taosMemoryFree(views); - taosMemoryFree(pDynViewVer); - return code; - } + TSC_ERR_JRET(catalogGetExpiredViews(pCatalog, &views, &viewNum, &pDynViewVer)); if (viewNum <= 0) { taosMemoryFree(views); @@ -912,6 +993,9 @@ int32_t hbGetExpiredViewInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, S if (NULL == req->info) { req->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); + if (NULL == req->info) { + TSC_ERR_JRET(terrno); + } } SKv kv = { @@ -920,15 +1004,18 @@ int32_t hbGetExpiredViewInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, S .value = pDynViewVer, }; - taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); + TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv))); kv.key = HEARTBEAT_KEY_VIEWINFO; kv.valueLen = sizeof(SViewVersion) * viewNum; kv.value = views; - taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); - + TSC_ERR_JRET(taosHashPut(req->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv))); return TSDB_CODE_SUCCESS; +_return: + taosMemoryFree(views); + taosMemoryFree(pDynViewVer); + return code; } int32_t hbGetExpiredTSMAInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, SClientHbReq *pReq) { @@ -958,22 +1045,30 @@ int32_t hbGetExpiredTSMAInfo(SClientHbKey *connKey, struct SCatalog *pCatalog, S if (!pReq->info) { pReq->info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); + if (!pReq->info) { + taosMemoryFree(tsmas); + return terrno; + } } SKv kv = {.key = HEARTBEAT_KEY_TSMA, .valueLen = sizeof(STSMAVersion) * tsmaNum, .value = tsmas}; - taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); + code = taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); + if (TSDB_CODE_SUCCESS != code) { + taosMemoryFree(tsmas); + return code; + } return TSDB_CODE_SUCCESS; } int32_t hbGetAppInfo(int64_t clusterId, SClientHbReq *req) { SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId)); if (NULL != pApp) { - memcpy(&req->app, pApp, sizeof(*pApp)); + (void)memcpy(&req->app, pApp, sizeof(*pApp)); } else { - memset(&req->app.summary, 0, sizeof(req->app.summary)); + (void)memset(&req->app.summary, 0, sizeof(req->app.summary)); req->app.pid = taosGetPId(); req->app.appId = clientHbMgr.appId; - taosGetAppName(req->app.name, NULL); + TSC_ERR_RET(taosGetAppName(req->app.name, NULL)); } return TSDB_CODE_SUCCESS; @@ -984,7 +1079,11 @@ int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req SHbParam *hbParam = (SHbParam *)param; SCatalog *pCatalog = NULL; - hbGetQueryBasicInfo(connKey, req); + code = hbGetQueryBasicInfo(connKey, req); + if (code != TSDB_CODE_SUCCESS) { + tscWarn("hbGetQueryBasicInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code)); + return code; + } if (hbParam->reqCnt == 0) { code = catalogGetHandle(hbParam->clusterId, &pCatalog); @@ -993,20 +1092,32 @@ int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req return code; } - hbGetAppInfo(hbParam->clusterId, req); + code = hbGetAppInfo(hbParam->clusterId, req); + if (TSDB_CODE_SUCCESS != code) { + tscWarn("getAppInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code)); + return code; + } if (!taosHashGet(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(hbParam->clusterId))) { code = hbGetExpiredUserInfo(connKey, pCatalog, req); if (TSDB_CODE_SUCCESS != code) { + tscWarn("hbGetExpiredUserInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code)); return code; } - taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0); + if (clientHbMgr.appHbHash) { + code = taosHashPut(clientHbMgr.appHbHash, &hbParam->clusterId, sizeof(uint64_t), NULL, 0); + if (TSDB_CODE_SUCCESS != code) { + tscWarn("hbQueryHbReqHandle put clusterId failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code)); + return code; + } + } } // invoke after hbGetExpiredUserInfo if (2 != atomic_load_8(&hbParam->pAppHbMgr->connHbFlag)) { code = hbGetUserAuthInfo(connKey, hbParam, req); if (TSDB_CODE_SUCCESS != code) { + tscWarn("hbGetUserAuthInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code)); return code; } atomic_store_8(&hbParam->pAppHbMgr->connHbFlag, 1); @@ -1014,23 +1125,34 @@ int32_t hbQueryHbReqHandle(SClientHbKey *connKey, void *param, SClientHbReq *req code = hbGetExpiredDBInfo(connKey, pCatalog, req); if (TSDB_CODE_SUCCESS != code) { + tscWarn("hbGetExpiredDBInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code)); return code; } code = hbGetExpiredStbInfo(connKey, pCatalog, req); if (TSDB_CODE_SUCCESS != code) { + tscWarn("hbGetExpiredStbInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code)); return code; } #ifdef TD_ENTERPRISE code = hbGetExpiredViewInfo(connKey, pCatalog, req); if (TSDB_CODE_SUCCESS != code) { + tscWarn("hbGetExpiredViewInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code)); return code; } #endif code = hbGetExpiredTSMAInfo(connKey, pCatalog, req); + if (TSDB_CODE_SUCCESS != code) { + tscWarn("hbGetExpiredTSMAInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code)); + return code; + } } else { - hbGetAppInfo(hbParam->clusterId, req); + code = hbGetAppInfo(hbParam->clusterId, req); + if (TSDB_CODE_SUCCESS != code) { + tscWarn("hbGetAppInfo failed, clusterId:%" PRIx64 ", error:%s", hbParam->clusterId, tstrerror(code)); + return code; + } } ++hbParam->reqCnt; // success to get catalog info @@ -1047,17 +1169,16 @@ static FORCE_INLINE void hbMgrInitHandle() { clientHbMgr.rspHandle[CONN_TYPE__TMQ] = hbMqHbRspHandle; } -SClientHbBatchReq *hbGatherAllInfo(SAppHbMgr *pAppHbMgr) { - SClientHbBatchReq *pBatchReq = taosMemoryCalloc(1, sizeof(SClientHbBatchReq)); +int32_t hbGatherAllInfo(SAppHbMgr *pAppHbMgr, SClientHbBatchReq **pBatchReq) { + *pBatchReq = taosMemoryCalloc(1, sizeof(SClientHbBatchReq)); if (pBatchReq == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; + return TSDB_CODE_OUT_OF_MEMORY; } int32_t connKeyCnt = atomic_load_32(&pAppHbMgr->connKeyCnt); - pBatchReq->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq)); - if (!pBatchReq->reqs) { - tFreeClientHbBatchReq(pBatchReq); - return NULL; + (*pBatchReq)->reqs = taosArrayInit(connKeyCnt, sizeof(SClientHbReq)); + if (!(*pBatchReq)->reqs) { + tFreeClientHbBatchReq(*pBatchReq); + return terrno; } int64_t maxIpWhiteVer = 0; @@ -1073,7 +1194,11 @@ SClientHbBatchReq *hbGatherAllInfo(SAppHbMgr *pAppHbMgr) { continue; } - pOneReq = taosArrayPush(pBatchReq->reqs, pOneReq); + pOneReq = taosArrayPush((*pBatchReq)->reqs, pOneReq); + if (NULL == pOneReq) { + releaseTscObj(connKey->tscRid); + continue; + } switch (connKey->connType) { case CONN_TYPE__QUERY: { @@ -1100,9 +1225,9 @@ SClientHbBatchReq *hbGatherAllInfo(SAppHbMgr *pAppHbMgr) { maxIpWhiteVer = TMAX(maxIpWhiteVer, ver); releaseTscObj(connKey->tscRid); } - pBatchReq->ipWhiteList = maxIpWhiteVer; + (*pBatchReq)->ipWhiteList = maxIpWhiteVer; - return pBatchReq; + return TSDB_CODE_SUCCESS; } void hbThreadFuncUnexpectedStopped(void) { atomic_store_8(&clientHbMgr.threadStop, 2); } @@ -1121,11 +1246,12 @@ void hbMergeSummary(SAppClusterSummary *dst, SAppClusterSummary *src) { int32_t hbGatherAppInfo(void) { SAppHbReq req = {0}; + int32_t code = TSDB_CODE_SUCCESS; int sz = taosArrayGetSize(clientHbMgr.appHbMgrs); if (sz > 0) { req.pid = taosGetPId(); req.appId = clientHbMgr.appId; - taosGetAppName(req.name, NULL); + TSC_ERR_RET(taosGetAppName(req.name, NULL)); } taosHashClear(clientHbMgr.appSummary); @@ -1137,9 +1263,9 @@ int32_t hbGatherAppInfo(void) { int64_t clusterId = pAppHbMgr->pAppInstInfo->clusterId; SAppHbReq *pApp = taosHashGet(clientHbMgr.appSummary, &clusterId, sizeof(clusterId)); if (NULL == pApp) { - memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary)); + (void)memcpy(&req.summary, &pAppHbMgr->pAppInstInfo->summary, sizeof(req.summary)); req.startTime = pAppHbMgr->startTime; - taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req)); + TSC_ERR_RET(taosHashPut(clientHbMgr.appSummary, &clusterId, sizeof(clusterId), &req, sizeof(req))); } else { if (pAppHbMgr->startTime < pApp->startTime) { pApp->startTime = pAppHbMgr->startTime; @@ -1164,14 +1290,24 @@ static void *hbThreadFunc(void *param) { break; } - taosThreadMutexLock(&clientHbMgr.lock); + if (TSDB_CODE_SUCCESS != taosThreadMutexLock(&clientHbMgr.lock)) { + tscError("taosThreadMutexLock failed"); + return NULL; + } int sz = taosArrayGetSize(clientHbMgr.appHbMgrs); if (sz > 0) { - hbGatherAppInfo(); + if (TSDB_CODE_SUCCESS != hbGatherAppInfo()) { + tscError("hbGatherAppInfo failed"); + return NULL; + } if (sz > 1 && !clientHbMgr.appHbHash) { clientHbMgr.appHbHash = - taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), false, HASH_NO_LOCK); + taosHashInit(0, taosGetDefaultHashFunction(TSDB_DATA_TYPE_UBIGINT), true, HASH_NO_LOCK); + if (NULL == clientHbMgr.appHbHash) { + tscError("taosHashInit failed"); + return NULL; + } } taosHashClear(clientHbMgr.appHbHash); } @@ -1186,8 +1322,10 @@ static void *hbThreadFunc(void *param) { if (connCnt == 0) { continue; } - SClientHbBatchReq *pReq = hbGatherAllInfo(pAppHbMgr); - if (pReq == NULL || taosArrayGetP(clientHbMgr.appHbMgrs, i) == NULL) { + SClientHbBatchReq *pReq = NULL; + int32_t code = hbGatherAllInfo(pAppHbMgr, &pReq); + if (TSDB_CODE_SUCCESS != code || taosArrayGetP(clientHbMgr.appHbMgrs, i) == NULL) { + terrno = code ? code : TSDB_CODE_OUT_OF_RANGE; tFreeClientHbBatchReq(pReq); continue; } @@ -1229,8 +1367,10 @@ static void *hbThreadFunc(void *param) { atomic_add_fetch_32(&pAppHbMgr->reportCnt, 1); } - taosThreadMutexUnlock(&clientHbMgr.lock); - + if (TSDB_CODE_SUCCESS != taosThreadMutexUnlock(&clientHbMgr.lock)) { + tscError("taosThreadMutexLock failed"); + return NULL; + } taosMsleep(HEARTBEAT_INTERVAL); } taosHashCleanup(clientHbMgr.appHbHash); @@ -1238,16 +1378,24 @@ static void *hbThreadFunc(void *param) { } static int32_t hbCreateThread() { + int32_t code = TSDB_CODE_SUCCESS; TdThreadAttr thAttr; - taosThreadAttrInit(&thAttr); - taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE); + TSC_ERR_JRET(taosThreadAttrInit(&thAttr)); + TSC_ERR_JRET(taosThreadAttrSetDetachState(&thAttr, PTHREAD_CREATE_JOINABLE)); if (taosThreadCreate(&clientHbMgr.thread, &thAttr, hbThreadFunc, NULL) != 0) { terrno = TAOS_SYSTEM_ERROR(errno); - return -1; + TSC_ERR_RET(terrno); } - taosThreadAttrDestroy(&thAttr); - return 0; + (void)taosThreadAttrDestroy(&thAttr); +_return: + + if (code) { + terrno = TAOS_SYSTEM_ERROR(errno); + TSC_ERR_RET(terrno); + } + + return TSDB_CODE_SUCCESS; } static void hbStopThread() { @@ -1259,54 +1407,66 @@ static void hbStopThread() { return; } + int32_t code = TSDB_CODE_SUCCESS; // thread quit mode kill or inner exit from self-thread if (clientHbMgr.quitByKill) { - taosThreadKill(clientHbMgr.thread, 0); + code = taosThreadKill(clientHbMgr.thread, 0); + if (TSDB_CODE_SUCCESS != code) { + tscError("taosThreadKill failed since %s", tstrerror(TAOS_SYSTEM_ERROR(code))); + } } else { - taosThreadJoin(clientHbMgr.thread, NULL); + code = taosThreadJoin(clientHbMgr.thread, NULL); + if (TSDB_CODE_SUCCESS != code) { + tscError("taosThreadJoin failed since %s", tstrerror(TAOS_SYSTEM_ERROR(errno))); + } } tscDebug("hb thread stopped"); } -SAppHbMgr *appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key) { - if (hbMgrInit() != 0) { - terrno = TSDB_CODE_TSC_INTERNAL_ERROR; - return NULL; - } - SAppHbMgr *pAppHbMgr = taosMemoryMalloc(sizeof(SAppHbMgr)); - if (pAppHbMgr == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; +int32_t appHbMgrInit(SAppInstInfo *pAppInstInfo, char *key, SAppHbMgr **pAppHbMgr) { + int32_t code = TSDB_CODE_SUCCESS; + TSC_ERR_RET(hbMgrInit()); + *pAppHbMgr = taosMemoryMalloc(sizeof(SAppHbMgr)); + if (*pAppHbMgr == NULL) { + TSC_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY); } // init stat - pAppHbMgr->startTime = taosGetTimestampMs(); - pAppHbMgr->connKeyCnt = 0; - pAppHbMgr->connHbFlag = 0; - pAppHbMgr->reportCnt = 0; - pAppHbMgr->reportBytes = 0; - pAppHbMgr->key = taosStrdup(key); + (*pAppHbMgr)->startTime = taosGetTimestampMs(); + (*pAppHbMgr)->connKeyCnt = 0; + (*pAppHbMgr)->connHbFlag = 0; + (*pAppHbMgr)->reportCnt = 0; + (*pAppHbMgr)->reportBytes = 0; + (*pAppHbMgr)->key = taosStrdup(key); + if ((*pAppHbMgr)->key == NULL) { + TSC_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY); + } // init app info - pAppHbMgr->pAppInstInfo = pAppInstInfo; + (*pAppHbMgr)->pAppInstInfo = pAppInstInfo; // init hash info - pAppHbMgr->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); + (*pAppHbMgr)->activeInfo = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); - if (pAppHbMgr->activeInfo == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - taosMemoryFree(pAppHbMgr); - return NULL; + if ((*pAppHbMgr)->activeInfo == NULL) { + TSC_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY); } // taosHashSetFreeFp(pAppHbMgr->activeInfo, tFreeClientHbReq); - taosThreadMutexLock(&clientHbMgr.lock); - taosArrayPush(clientHbMgr.appHbMgrs, &pAppHbMgr); - pAppHbMgr->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1; - taosThreadMutexUnlock(&clientHbMgr.lock); + TSC_ERR_JRET(taosThreadMutexLock(&clientHbMgr.lock)); + if (taosArrayPush(clientHbMgr.appHbMgrs, &(*pAppHbMgr)) == NULL) { + code = TSDB_CODE_OUT_OF_MEMORY; + (void)taosThreadMutexUnlock(&clientHbMgr.lock); + goto _return; + } + (*pAppHbMgr)->idx = taosArrayGetSize(clientHbMgr.appHbMgrs) - 1; + TSC_ERR_JRET(taosThreadMutexUnlock(&clientHbMgr.lock)); - return pAppHbMgr; + return TSDB_CODE_SUCCESS; +_return: + taosMemoryFree(*pAppHbMgr); + return code; } void hbFreeAppHbMgr(SAppHbMgr *pTarget) { @@ -1324,7 +1484,11 @@ void hbFreeAppHbMgr(SAppHbMgr *pTarget) { } void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr) { - taosThreadMutexLock(&clientHbMgr.lock); + int32_t code = TSDB_CODE_SUCCESS; + code = taosThreadMutexLock(&clientHbMgr.lock); + if (TSDB_CODE_SUCCESS != code) { + tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code))); + } int32_t mgrSize = taosArrayGetSize(clientHbMgr.appHbMgrs); for (int32_t i = 0; i < mgrSize; ++i) { SAppHbMgr *pItem = taosArrayGetP(clientHbMgr.appHbMgrs, i); @@ -1335,7 +1499,10 @@ void hbRemoveAppHbMrg(SAppHbMgr **pAppHbMgr) { break; } } - taosThreadMutexUnlock(&clientHbMgr.lock); + code = taosThreadMutexUnlock(&clientHbMgr.lock); + if (TSDB_CODE_SUCCESS != code) { + tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code))); + } } void appHbMgrCleanup(void) { @@ -1347,7 +1514,7 @@ void appHbMgrCleanup(void) { } } -int hbMgrInit() { +int32_t hbMgrInit() { // init once int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 0, 1); if (old == 1) return 0; @@ -1355,9 +1522,14 @@ int hbMgrInit() { clientHbMgr.appId = tGenIdPI64(); tscDebug("app %" PRIx64 " initialized", clientHbMgr.appId); - clientHbMgr.appSummary = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), false, HASH_NO_LOCK); + clientHbMgr.appSummary = taosHashInit(10, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK); + if (NULL == clientHbMgr.appSummary) { + uError("hbMgrInit:taosHashInit error") return terrno; + } clientHbMgr.appHbMgrs = taosArrayInit(0, sizeof(void *)); - + if (NULL == clientHbMgr.appHbMgrs) { + uError("hbMgrInit:taosArrayInit error") return terrno; + } TdThreadMutexAttr attr = {0}; int ret = taosThreadMutexAttrInit(&attr); @@ -1384,7 +1556,10 @@ int hbMgrInit() { hbMgrInitHandle(); // init backgroud thread - hbCreateThread(); + ret = hbCreateThread(); + if (ret != 0) { + uError("hbMgrInit:hbCreateThread error") return ret; + } return 0; } @@ -1396,14 +1571,20 @@ void hbMgrCleanUp() { int8_t old = atomic_val_compare_exchange_8(&clientHbMgr.inited, 1, 0); if (old == 0) return; - taosThreadMutexLock(&clientHbMgr.lock); + int32_t code = taosThreadMutexLock(&clientHbMgr.lock); + if (TSDB_CODE_SUCCESS != code) { + tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code))); + } appHbMgrCleanup(); taosArrayDestroy(clientHbMgr.appHbMgrs); clientHbMgr.appHbMgrs = NULL; - taosThreadMutexUnlock(&clientHbMgr.lock); + code = taosThreadMutexUnlock(&clientHbMgr.lock); + if (TSDB_CODE_SUCCESS != code) { + tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code))); + } } -int hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, int64_t clusterId) { +int32_t hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, int64_t clusterId) { // init hash in activeinfo void *data = taosHashGet(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey)); if (data != NULL) { @@ -1414,13 +1595,13 @@ int hbRegisterConnImpl(SAppHbMgr *pAppHbMgr, SClientHbKey connKey, int64_t clust hbReq.clusterId = clusterId; // hbReq.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); - taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq)); + TSC_ERR_RET(taosHashPut(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey), &hbReq, sizeof(SClientHbReq))); - atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1); + (void)atomic_add_fetch_32(&pAppHbMgr->connKeyCnt, 1); return 0; } -int hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) { +int32_t hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, int8_t connType) { SClientHbKey connKey = { .tscRid = tscRefId, .connType = connType, @@ -1439,7 +1620,10 @@ int hbRegisterConn(SAppHbMgr *pAppHbMgr, int64_t tscRefId, int64_t clusterId, in } void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) { - taosThreadMutexLock(&clientHbMgr.lock); + int32_t code = taosThreadMutexLock(&clientHbMgr.lock); + if (TSDB_CODE_SUCCESS != code) { + tscError("failed to lock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code))); + } SAppHbMgr *pAppHbMgr = taosArrayGetP(clientHbMgr.appHbMgrs, pTscObj->appHbMgrIdx); if (pAppHbMgr) { SClientHbReq *pReq = taosHashAcquire(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey)); @@ -1447,10 +1631,13 @@ void hbDeregisterConn(STscObj *pTscObj, SClientHbKey connKey) { tFreeClientHbReq(pReq); taosHashRemove(pAppHbMgr->activeInfo, &connKey, sizeof(SClientHbKey)); taosHashRelease(pAppHbMgr->activeInfo, pReq); - atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1); + (void)atomic_sub_fetch_32(&pAppHbMgr->connKeyCnt, 1); } } - taosThreadMutexUnlock(&clientHbMgr.lock); + code = taosThreadMutexUnlock(&clientHbMgr.lock); + if (TSDB_CODE_SUCCESS != code) { + tscError("failed to unlock clientHbMgr, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code))); + } } // set heart beat thread quit mode , if quicByKill 1 then kill thread else quit from inner diff --git a/source/client/src/clientImpl.c b/source/client/src/clientImpl.c index a2bae7f449..112731d39d 100644 --- a/source/client/src/clientImpl.c +++ b/source/client/src/clientImpl.c @@ -29,7 +29,7 @@ #include "tsched.h" #include "tversion.h" static int32_t initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSet); -static SMsgSendInfo* buildConnectMsg(SRequestObj* pRequest); +static int32_t buildConnectMsg(SRequestObj* pRequest, SMsgSendInfo** pMsgSendInfo); static bool stringLengthCheck(const char* str, size_t maxsize) { if (str == NULL) { @@ -52,7 +52,7 @@ static bool validateDbName(const char* db) { return stringLengthCheck(db, TSDB_D static char* getClusterKey(const char* user, const char* auth, const char* ip, int32_t port) { char key[512] = {0}; - snprintf(key, sizeof(key), "%s:%s:%s:%d", user, auth, ip, port); + (void)snprintf(key, sizeof(key), "%s:%s:%s:%d", user, auth, ip, port); return taosStrdup(key); } @@ -63,41 +63,35 @@ bool chkRequestKilled(void* param) { killed = true; } - releaseRequest((int64_t)param); + (void)releaseRequest((int64_t)param); return killed; } -static STscObj* taosConnectImpl(const char* user, const char* auth, const char* db, __taos_async_fn_t fp, void* param, - SAppInstInfo* pAppInfo, int connType); - -STscObj* taos_connect_internal(const char* ip, const char* user, const char* pass, const char* auth, const char* db, - uint16_t port, int connType) { - if (taos_init() != TSDB_CODE_SUCCESS) { - return NULL; - } +static int32_t taosConnectImpl(const char* user, const char* auth, const char* db, __taos_async_fn_t fp, void* param, + SAppInstInfo* pAppInfo, int connType, STscObj** pTscObj); +int32_t taos_connect_internal(const char* ip, const char* user, const char* pass, const char* auth, const char* db, + uint16_t port, int connType, STscObj** pObj) { + TSC_ERR_RET(taos_init()); if (!validateUserName(user)) { - terrno = TSDB_CODE_TSC_INVALID_USER_LENGTH; - return NULL; + TSC_ERR_RET(TSDB_CODE_TSC_INVALID_USER_LENGTH); } char localDb[TSDB_DB_NAME_LEN] = {0}; if (db != NULL && strlen(db) > 0) { if (!validateDbName(db)) { - terrno = TSDB_CODE_TSC_INVALID_DB_LENGTH; - return NULL; + TSC_ERR_RET(TSDB_CODE_TSC_INVALID_DB_LENGTH); } tstrncpy(localDb, db, sizeof(localDb)); - strdequote(localDb); + (void)strdequote(localDb); } char secretEncrypt[TSDB_PASSWORD_LEN + 1] = {0}; if (auth == NULL) { if (!validatePassword(pass)) { - terrno = TSDB_CODE_TSC_INVALID_PASS_LENGTH; - return NULL; + TSC_ERR_RET(TSDB_CODE_TSC_INVALID_PASS_LENGTH); } taosEncryptPass_c((uint8_t*)pass, strlen(pass), secretEncrypt); @@ -107,13 +101,9 @@ STscObj* taos_connect_internal(const char* ip, const char* user, const char* pas SCorEpSet epSet = {0}; if (ip) { - if (initEpSetFromCfg(ip, NULL, &epSet) < 0) { - return NULL; - } + TSC_ERR_RET(initEpSetFromCfg(ip, NULL, &epSet)); } else { - if (initEpSetFromCfg(tsFirst, tsSecond, &epSet) < 0) { - return NULL; - } + TSC_ERR_RET(initEpSetFromCfg(tsFirst, tsSecond, &epSet)); } if (port) { @@ -122,7 +112,9 @@ STscObj* taos_connect_internal(const char* ip, const char* user, const char* pas } char* key = getClusterKey(user, secretEncrypt, ip, port); - + if (NULL == key) { + TSC_ERR_RET(TSDB_CODE_OUT_OF_MEMORY); + } tscInfo("connecting to server, numOfEps:%d inUse:%d user:%s db:%s key:%s", epSet.epSet.numOfEps, epSet.epSet.inUse, user, db, key); for (int32_t i = 0; i < epSet.epSet.numOfEps; ++i) { @@ -130,29 +122,40 @@ STscObj* taos_connect_internal(const char* ip, const char* user, const char* pas } SAppInstInfo** pInst = NULL; - taosThreadMutexLock(&appInfo.mutex); + int32_t code = taosThreadMutexLock(&appInfo.mutex); + if (TSDB_CODE_SUCCESS != code) { + tscError("failed to lock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code))); + TSC_ERR_RET(code); + } pInst = taosHashGet(appInfo.pInstMap, key, strlen(key)); SAppInstInfo* p = NULL; if (pInst == NULL) { p = taosMemoryCalloc(1, sizeof(struct SAppInstInfo)); + if (NULL == p) { + TSC_ERR_JRET(TSDB_CODE_OUT_OF_MEMORY); + } p->mgmtEp = epSet; - taosThreadMutexInit(&p->qnodeMutex, NULL); - p->pTransporter = openTransporter(user, secretEncrypt, tsNumOfCores / 2); - if (p->pTransporter == NULL) { - taosThreadMutexUnlock(&appInfo.mutex); - taosMemoryFreeClear(key); + code = taosThreadMutexInit(&p->qnodeMutex, NULL); + if (TSDB_CODE_SUCCESS != code) { taosMemoryFree(p); - return NULL; + TSC_ERR_JRET(code); } - p->pAppHbMgr = appHbMgrInit(p, key); - if (NULL == p->pAppHbMgr) { + code = openTransporter(user, secretEncrypt, tsNumOfCores / 2, &p->pTransporter); + if (TSDB_CODE_SUCCESS != code) { + taosMemoryFree(p); + TSC_ERR_JRET(code); + } + code = appHbMgrInit(p, key, &p->pAppHbMgr); + if (TSDB_CODE_SUCCESS != code) { destroyAppInst(&p); - taosThreadMutexUnlock(&appInfo.mutex); - taosMemoryFreeClear(key); - return NULL; + TSC_ERR_JRET(code); + } + code = taosHashPut(appInfo.pInstMap, key, strlen(key), &p, POINTER_BYTES); + if (TSDB_CODE_SUCCESS != code) { + destroyAppInst(&p); + TSC_ERR_JRET(code); } - taosHashPut(appInfo.pInstMap, key, strlen(key), &p, POINTER_BYTES); p->instKey = key; key = NULL; tscDebug("new app inst mgr %p, user:%s, ip:%s, port:%d", p, user, epSet.epSet.eps[0].fqdn, epSet.epSet.eps[0].port); @@ -164,11 +167,17 @@ STscObj* taos_connect_internal(const char* ip, const char* user, const char* pas atomic_store_8(&(*pInst)->pAppHbMgr->connHbFlag, 0); } - taosThreadMutexUnlock(&appInfo.mutex); +_return: + + code = taosThreadMutexUnlock(&appInfo.mutex); + if (TSDB_CODE_SUCCESS != code) { + tscError("failed to unlock app info, code:%s", tstrerror(TAOS_SYSTEM_ERROR(code))); + return code; + } taosMemoryFreeClear(key); - return taosConnectImpl(user, &secretEncrypt[0], localDb, NULL, NULL, *pInst, connType); + return taosConnectImpl(user, &secretEncrypt[0], localDb, NULL, NULL, *pInst, connType, pObj); } //SAppInstInfo* getAppInstInfo(const char* clusterKey) { @@ -182,16 +191,16 @@ STscObj* taos_connect_internal(const char* ip, const char* user, const char* pas void freeQueryParam(SSyncQueryParam* param) { if (param == NULL) return; - tsem_destroy(¶m->sem); + (void)tsem_destroy(¶m->sem); taosMemoryFree(param); } int32_t buildRequest(uint64_t connId, const char* sql, int sqlLen, void* param, bool validateSql, SRequestObj** pRequest, int64_t reqid) { - *pRequest = createRequest(connId, TSDB_SQL_SELECT, reqid); - if (*pRequest == NULL) { + int32_t code = createRequest(connId, TSDB_SQL_SELECT, reqid, pRequest); + if (TSDB_CODE_SUCCESS != code) { tscError("failed to malloc sqlObj, %s", sql); - return terrno; + return code; } (*pRequest)->sqlstr = taosMemoryMalloc(sqlLen + 1); @@ -202,7 +211,7 @@ int32_t buildRequest(uint64_t connId, const char* sql, int sqlLen, void* param, return TSDB_CODE_OUT_OF_MEMORY; } - strntolower((*pRequest)->sqlstr, sql, (int32_t)sqlLen); + (void)strntolower((*pRequest)->sqlstr, sql, (int32_t)sqlLen); (*pRequest)->sqlstr[sqlLen] = 0; (*pRequest)->sqlLen = sqlLen; (*pRequest)->validateOnly = validateSql; @@ -279,7 +288,7 @@ int32_t parseSql(SRequestObj* pRequest, bool topicQuery, SQuery** pQuery, SStmtC code = qParseSql(&cxt, pQuery); if (TSDB_CODE_SUCCESS == code) { if ((*pQuery)->haveResultSet) { - setResSchemaInfo(&pRequest->body.resInfo, (*pQuery)->pResSchema, (*pQuery)->numOfResCols); + code = setResSchemaInfo(&pRequest->body.resInfo, (*pQuery)->pResSchema, (*pQuery)->numOfResCols); setResPrecision(&pRequest->body.resInfo, (*pQuery)->precision); } } @@ -322,9 +331,9 @@ int32_t execDdlQuery(SRequestObj* pRequest, SQuery* pQuery) { SMsgSendInfo* pSendMsg = buildMsgInfoImpl(pRequest); int64_t transporterId = 0; - asyncSendMsgToServer(pTscObj->pAppInfo->pTransporter, &pMsgInfo->epSet, &transporterId, pSendMsg); + TSC_ERR_RET(asyncSendMsgToServer(pTscObj->pAppInfo->pTransporter, &pMsgInfo->epSet, &transporterId, pSendMsg)); - tsem_wait(&pRequest->body.rspSem); + (void)tsem_wait(&pRequest->body.rspSem); return TSDB_CODE_SUCCESS; } @@ -399,7 +408,7 @@ int compareQueryNodeLoad(const void* elem1, const void* elem2) { } int32_t updateQnodeList(SAppInstInfo* pInfo, SArray* pNodeList) { - taosThreadMutexLock(&pInfo->qnodeMutex); + TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex)); if (pInfo->pQnodeList) { taosArrayDestroy(pInfo->pQnodeList); pInfo->pQnodeList = NULL; @@ -412,41 +421,44 @@ int32_t updateQnodeList(SAppInstInfo* pInfo, SArray* pNodeList) { tscDebug("QnodeList updated in cluster 0x%" PRIx64 ", num:%ld", pInfo->clusterId, taosArrayGetSize(pInfo->pQnodeList)); } - taosThreadMutexUnlock(&pInfo->qnodeMutex); + TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex)); return TSDB_CODE_SUCCESS; } -bool qnodeRequired(SRequestObj* pRequest) { +int32_t qnodeRequired(SRequestObj* pRequest, bool *required) { if (QUERY_POLICY_VNODE == tsQueryPolicy || QUERY_POLICY_CLIENT == tsQueryPolicy) { - return false; + *required = false; + return TSDB_CODE_SUCCESS; } + int32_t code = TSDB_CODE_SUCCESS; SAppInstInfo* pInfo = pRequest->pTscObj->pAppInfo; - bool required = false; + *required = false; - taosThreadMutexLock(&pInfo->qnodeMutex); - required = (NULL == pInfo->pQnodeList); - taosThreadMutexUnlock(&pInfo->qnodeMutex); - - return required; + TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex)); + *required = (NULL == pInfo->pQnodeList); + TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex)); + return TSDB_CODE_SUCCESS; } int32_t getQnodeList(SRequestObj* pRequest, SArray** pNodeList) { SAppInstInfo* pInfo = pRequest->pTscObj->pAppInfo; int32_t code = 0; - taosThreadMutexLock(&pInfo->qnodeMutex); + TSC_ERR_RET(taosThreadMutexLock(&pInfo->qnodeMutex)); if (pInfo->pQnodeList) { *pNodeList = taosArrayDup(pInfo->pQnodeList, NULL); } - taosThreadMutexUnlock(&pInfo->qnodeMutex); - + TSC_ERR_RET(taosThreadMutexUnlock(&pInfo->qnodeMutex)); if (NULL == *pNodeList) { SCatalog* pCatalog = NULL; code = catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog); if (TSDB_CODE_SUCCESS == code) { *pNodeList = taosArrayInit(5, sizeof(SQueryNodeLoad)); + if (NULL == pNodeList) { + TSC_ERR_RET(terrno); + } SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter, .requestId = pRequest->requestId, .requestObjRefId = pRequest->self, @@ -479,10 +491,10 @@ int32_t getPlan(SRequestObj* pRequest, SQuery* pQuery, SQueryPlan** pPlan, SArra return qCreateQueryPlan(&cxt, pPlan, pNodeList); } -void setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t numOfCols) { +int32_t setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t numOfCols) { if (pResInfo == NULL || pSchema == NULL || numOfCols <= 0) { tscError("invalid paras, pResInfo == NULL || pSchema == NULL || numOfCols <= 0"); - return; + return TSDB_CODE_INVALID_PARA; } pResInfo->numOfCols = numOfCols; @@ -494,9 +506,12 @@ void setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t } pResInfo->fields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD)); pResInfo->userFields = taosMemoryCalloc(numOfCols, sizeof(TAOS_FIELD)); + if (NULL == pResInfo->fields || NULL == pResInfo->userFields) { + return TSDB_CODE_OUT_OF_MEMORY; + } if (numOfCols != pResInfo->numOfCols) { tscError("numOfCols:%d != pResInfo->numOfCols:%d", numOfCols, pResInfo->numOfCols); - return; + return TSDB_CODE_FAILED; } for (int32_t i = 0; i < pResInfo->numOfCols; ++i) { @@ -516,6 +531,7 @@ void setResSchemaInfo(SReqResultInfo* pResInfo, const SSchema* pSchema, int32_t tstrncpy(pResInfo->fields[i].name, pSchema[i].name, tListLen(pResInfo->fields[i].name)); tstrncpy(pResInfo->userFields[i].name, pSchema[i].name, tListLen(pResInfo->userFields[i].name)); } + return TSDB_CODE_SUCCESS; } void setResPrecision(SReqResultInfo* pResInfo, int32_t precision) { @@ -529,11 +545,17 @@ void setResPrecision(SReqResultInfo* pResInfo, int32_t precision) { int32_t buildVnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SArray* pDbVgList) { SArray* nodeList = taosArrayInit(4, sizeof(SQueryNodeLoad)); + if (NULL == nodeList) { + return terrno; + } char* policy = (tsQueryPolicy == QUERY_POLICY_VNODE) ? "vnode" : "client"; int32_t dbNum = taosArrayGetSize(pDbVgList); for (int32_t i = 0; i < dbNum; ++i) { SArray* pVg = taosArrayGetP(pDbVgList, i); + if (NULL == pVg) { + continue; + } int32_t vgNum = taosArrayGetSize(pVg); if (vgNum <= 0) { continue; @@ -541,11 +563,18 @@ int32_t buildVnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArr for (int32_t j = 0; j < vgNum; ++j) { SVgroupInfo* pInfo = taosArrayGet(pVg, j); + if (NULL == pInfo) { + taosArrayDestroy(nodeList); + return TSDB_CODE_OUT_OF_RANGE; + } SQueryNodeLoad load = {0}; load.addr.nodeId = pInfo->vgId; load.addr.epSet = pInfo->epSet; - taosArrayPush(nodeList, &load); + if (NULL == taosArrayPush(nodeList, &load)) { + taosArrayDestroy(nodeList); + return TSDB_CODE_OUT_OF_MEMORY; + } } } @@ -562,7 +591,14 @@ int32_t buildVnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArr } void* pData = taosArrayGet(pMnodeList, 0); - taosArrayAddBatch(nodeList, pData, mnodeNum); + if (NULL == pData) { + taosArrayDestroy(nodeList); + return TSDB_CODE_OUT_OF_RANGE; + } + if (NULL == taosArrayAddBatch(nodeList, pData, mnodeNum)) { + taosArrayDestroy(nodeList); + return TSDB_CODE_OUT_OF_MEMORY; + } tscDebug("0x%" PRIx64 " %s policy, use mnode list, num:%d", pRequest->requestId, policy, mnodeNum); @@ -575,11 +611,21 @@ _return: int32_t buildQnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* pMnodeList, SArray* pQnodeList) { SArray* nodeList = taosArrayInit(4, sizeof(SQueryNodeLoad)); + if (NULL == nodeList) { + return terrno; + } int32_t qNodeNum = taosArrayGetSize(pQnodeList); if (qNodeNum > 0) { void* pData = taosArrayGet(pQnodeList, 0); - taosArrayAddBatch(nodeList, pData, qNodeNum); + if (NULL == pData) { + taosArrayDestroy(nodeList); + return TSDB_CODE_OUT_OF_RANGE; + } + if (NULL == taosArrayAddBatch(nodeList, pData, qNodeNum)) { + taosArrayDestroy(nodeList); + return TSDB_CODE_OUT_OF_MEMORY; + } tscDebug("0x%" PRIx64 " qnode policy, use qnode list, num:%d", pRequest->requestId, qNodeNum); goto _return; } @@ -591,7 +637,14 @@ int32_t buildQnodePolicyNodeList(SRequestObj* pRequest, SArray** pNodeList, SArr } void* pData = taosArrayGet(pMnodeList, 0); - taosArrayAddBatch(nodeList, pData, mnodeNum); + if (NULL == pData) { + taosArrayDestroy(nodeList); + return TSDB_CODE_OUT_OF_RANGE; + } + if (NULL == taosArrayAddBatch(nodeList, pData, mnodeNum)) { + taosArrayDestroy(nodeList); + return TSDB_CODE_OUT_OF_MEMORY; + } tscDebug("0x%" PRIx64 " qnode policy, use mnode list, num:%d", pRequest->requestId, mnodeNum); @@ -612,7 +665,10 @@ int32_t buildAsyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray case QUERY_POLICY_CLIENT: { if (pResultMeta) { pDbVgList = taosArrayInit(4, POINTER_BYTES); - + if (NULL == pDbVgList) { + code = terrno; + goto _return; + } int32_t dbNum = taosArrayGetSize(pResultMeta->pDbVgroup); for (int32_t i = 0; i < dbNum; ++i) { SMetaRes* pRes = taosArrayGet(pResultMeta->pDbVgroup, i); @@ -620,7 +676,10 @@ int32_t buildAsyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray continue; } - taosArrayPush(pDbVgList, &pRes->pRes); + if (NULL == taosArrayPush(pDbVgList, &pRes->pRes)) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _return; + } } } @@ -635,14 +694,22 @@ int32_t buildAsyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray pQnodeList = NULL; } else { pQnodeList = taosArrayDup((SArray*)pRes->pRes, NULL); + if (NULL == pQnodeList) { + code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY; + goto _return; + } } } else { SAppInstInfo* pInst = pRequest->pTscObj->pAppInfo; - taosThreadMutexLock(&pInst->qnodeMutex); + TSC_ERR_JRET(taosThreadMutexLock(&pInst->qnodeMutex)); if (pInst->pQnodeList) { pQnodeList = taosArrayDup(pInst->pQnodeList, NULL); + if (NULL == pQnodeList) { + code = terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY; + goto _return; + } } - taosThreadMutexUnlock(&pInst->qnodeMutex); + TSC_ERR_JRET(taosThreadMutexUnlock(&pInst->qnodeMutex)); } code = buildQnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pQnodeList); @@ -653,6 +720,7 @@ int32_t buildAsyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray return TSDB_CODE_APP_ERROR; } +_return: taosArrayDestroy(pDbVgList); taosArrayDestroy(pQnodeList); @@ -682,6 +750,10 @@ int32_t buildSyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* } pDbVgList = taosArrayInit(dbNum, POINTER_BYTES); + if (NULL == pDbVgList) { + code = terrno; + goto _return; + } SArray* pVgList = NULL; for (int32_t i = 0; i < dbNum; ++i) { char* dbFName = taosArrayGet(pRequest->dbList, i); @@ -690,12 +762,16 @@ int32_t buildSyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* .requestObjRefId = pRequest->self, .mgmtEps = getEpSet_s(&pInst->mgmtEp)}; + // catalogGetDBVgList will handle dbFName == null. code = catalogGetDBVgList(pCtg, &conn, dbFName, &pVgList); if (code) { goto _return; } - taosArrayPush(pDbVgList, &pVgList); + if (NULL == taosArrayPush(pDbVgList, &pVgList)) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _return; + } } } @@ -704,7 +780,7 @@ int32_t buildSyncExecNodeList(SRequestObj* pRequest, SArray** pNodeList, SArray* } case QUERY_POLICY_HYBRID: case QUERY_POLICY_QNODE: { - getQnodeList(pRequest, &pQnodeList); + TSC_ERR_JRET(getQnodeList(pRequest, &pQnodeList)); code = buildQnodePolicyNodeList(pRequest, pNodeList, pMnodeList, pQnodeList); break; @@ -748,7 +824,7 @@ int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList int32_t code = schedulerExecJob(&req, &pRequest->body.queryJob); destroyQueryExecRes(&pRequest->body.resInfo.execRes); - memcpy(&pRequest->body.resInfo.execRes, &res, sizeof(res)); + (void)memcpy(&pRequest->body.resInfo.execRes, &res, sizeof(res)); if (code != TSDB_CODE_SUCCESS) { schedulerFreeJob(&pRequest->body.queryJob, 0); @@ -764,7 +840,7 @@ int32_t scheduleQuery(SRequestObj* pRequest, SQueryPlan* pDag, SArray* pNodeList if (TDMT_VND_SUBMIT == pRequest->type) { STscObj* pTscObj = pRequest->pTscObj; SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary; - atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, res.numOfRows); + (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, res.numOfRows); } schedulerFreeJob(&pRequest->body.queryJob, 0); @@ -786,7 +862,7 @@ int32_t handleSubmitExecRes(SRequestObj* pRequest, void* res, SCatalog* pCatalog for (int32_t i = 0; i < tbNum; ++i) { SVCreateTbRsp* pTbRsp = (SVCreateTbRsp*)taosArrayGet(pRsp->aCreateTbRsp, i); if (pTbRsp->pMeta) { - handleCreateTbExecRes(pTbRsp->pMeta, pCatalog); + TSC_ERR_RET(handleCreateTbExecRes(pTbRsp->pMeta, pCatalog)); } } @@ -810,8 +886,15 @@ int32_t handleQueryExecRes(SRequestObj* pRequest, void* res, SCatalog* pCatalog, for (int32_t i = 0; i < tbNum; ++i) { STbVerInfo* tbInfo = taosArrayGet(pTbArray, i); + if (NULL == tbInfo) { + code = TSDB_CODE_OUT_OF_RANGE; + goto _return; + } STbSVersion tbSver = {.tbFName = tbInfo->tbFName, .sver = tbInfo->sversion, .tver = tbInfo->tversion}; - taosArrayPush(pArray, &tbSver); + if (NULL == taosArrayPush(pArray, &tbSver)) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _return; + } } SRequestConnInfo conn = {.pTrans = pRequest->pTscObj->pAppInfo->pTransporter, @@ -862,6 +945,7 @@ int32_t handleQueryExecRsp(SRequestObj* pRequest) { int32_t num = taosArrayGetSize(pList); for (int32_t i = 0; i < num; ++i) { void* res = taosArrayGetP(pList, i); + // handleCreateTbExecRes will handle res == null code = handleCreateTbExecRes(res, pCatalog); } break; @@ -871,7 +955,7 @@ int32_t handleQueryExecRsp(SRequestObj* pRequest) { break; } case TDMT_VND_SUBMIT: { - atomic_add_fetch_64((int64_t*)&pAppInfo->summary.insertBytes, pRes->numOfBytes); + (void)atomic_add_fetch_64((int64_t*)&pAppInfo->summary.insertBytes, pRes->numOfBytes); code = handleSubmitExecRes(pRequest, pRes->res, pCatalog, &epset); break; @@ -908,7 +992,7 @@ void continuePostSubQuery(SRequestObj* pRequest, SSDataBlock* pBlock) { code = qContinuePlanPostQuery(pRequest->pPostPlan); } - nodesReleaseAllocator(pWrapper->pParseCtx->allocatorId); + code = nodesReleaseAllocator(pWrapper->pParseCtx->allocatorId); handleQueryAnslyseRes(pWrapper, NULL, code); } @@ -924,7 +1008,7 @@ void returnToUser(SRequestObj* pRequest) { pUserReq->code = pRequest->code; // return to client doRequestCallback(pUserReq, pUserReq->code); - releaseRequest(pRequest->relation.userRefId); + (void)releaseRequest(pRequest->relation.userRefId); return; } else { tscError("0x%" PRIx64 ", user ref 0x%" PRIx64 " is not there, reqId:0x%" PRIx64, pRequest->self, @@ -932,20 +1016,32 @@ void returnToUser(SRequestObj* pRequest) { } } -static SSDataBlock* createResultBlock(TAOS_RES* pRes, int32_t numOfRows) { +static int32_t createResultBlock(TAOS_RES* pRes, int32_t numOfRows, SSDataBlock**pBlock) { int64_t lastTs = 0; + int32_t code = TSDB_CODE_SUCCESS; TAOS_FIELD* pResFields = taos_fetch_fields(pRes); int32_t numOfFields = taos_num_fields(pRes); - SSDataBlock* pBlock = createDataBlock(); + *pBlock = createDataBlock(); + if (NULL == *pBlock) { + return terrno; + } for(int32_t i = 0; i < numOfFields; ++i) { SColumnInfoData colInfoData = createColumnInfoData(pResFields[i].type, pResFields[i].bytes, i + 1); - blockDataAppendColInfo(pBlock, &colInfoData); + code = blockDataAppendColInfo(*pBlock, &colInfoData); + if (TSDB_CODE_SUCCESS != code) { + blockDataDestroy(*pBlock); + return code; + } } - blockDataEnsureCapacity(pBlock, numOfRows); + code = blockDataEnsureCapacity(*pBlock, numOfRows); + if (TSDB_CODE_SUCCESS != code) { + blockDataDestroy(*pBlock); + return code; + } for (int32_t i = 0; i < numOfRows; ++i) { TAOS_ROW pRow = taos_fetch_row(pRes); @@ -955,18 +1051,22 @@ static SSDataBlock* createResultBlock(TAOS_RES* pRes, int32_t numOfRows) { } for(int32_t j = 0; j < numOfFields; ++j) { - SColumnInfoData* pColInfoData = taosArrayGet(pBlock->pDataBlock, j); - colDataSetVal(pColInfoData, i, pRow[j], false); + SColumnInfoData* pColInfoData = taosArrayGet((*pBlock)->pDataBlock, j); + code = colDataSetVal(pColInfoData, i, pRow[j], false); + if (TSDB_CODE_SUCCESS != code) { + blockDataDestroy(*pBlock); + return code; + } } tscDebug("lastKey:%" PRId64 " vgId:%d, vgVer:%" PRId64, ts, *(int32_t*)pRow[1], *(int64_t*)pRow[2]); } - pBlock->info.window.ekey = lastTs; - pBlock->info.rows = numOfRows; + (*pBlock)->info.window.ekey = lastTs; + (*pBlock)->info.rows = numOfRows; tscDebug("lastKey:%"PRId64" numOfRows:%d from all vgroups", lastTs, numOfRows); - return pBlock; + return TSDB_CODE_SUCCESS; } void postSubQueryFetchCb(void* param, TAOS_RES* res, int32_t rowNum) { @@ -976,7 +1076,12 @@ void postSubQueryFetchCb(void* param, TAOS_RES* res, int32_t rowNum) { return; } - SSDataBlock* pBlock = createResultBlock(res, rowNum); + SSDataBlock* pBlock = NULL; + if (TSDB_CODE_SUCCESS != createResultBlock(res, rowNum, &pBlock)) { + tscError("0x%" PRIx64 ", create result block failed, reqId:0x%" PRIx64, pRequest->self, pRequest->requestId); + return; + } + SRequestObj* pNextReq = acquireRequest(pRequest->relation.nextRefId); if (pNextReq) { continuePostSubQuery(pNextReq, pBlock); @@ -999,7 +1104,7 @@ void handlePostSubQuery(SSqlCallbackWrapper* pWrapper) { SRequestObj* pNextReq = acquireRequest(pRequest->relation.nextRefId); if (pNextReq) { continuePostSubQuery(pNextReq, NULL); - releaseRequest(pRequest->relation.nextRefId); + (void)releaseRequest(pRequest->relation.nextRefId); } else { tscError("0x%" PRIx64 ", next req ref 0x%" PRIx64 " is not there, reqId:0x%" PRIx64, pRequest->self, pRequest->relation.nextRefId, pRequest->requestId); @@ -1015,7 +1120,7 @@ void schedulerExecCb(SExecResult* pResult, void* param, int32_t code) { pRequest->code = code; if (pResult) { destroyQueryExecRes(&pRequest->body.resInfo.execRes); - memcpy(&pRequest->body.resInfo.execRes, pResult, sizeof(*pResult)); + (void)memcpy(&pRequest->body.resInfo.execRes, pResult, sizeof(*pResult)); } int32_t type = pRequest->type; @@ -1026,7 +1131,7 @@ void schedulerExecCb(SExecResult* pResult, void* param, int32_t code) { // record the insert rows if (TDMT_VND_SUBMIT == type) { SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary; - atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, pResult->numOfRows); + (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertRows, pResult->numOfRows); } } @@ -1040,14 +1145,14 @@ void schedulerExecCb(SExecResult* pResult, void* param, int32_t code) { if (code != TSDB_CODE_SUCCESS && NEED_CLIENT_HANDLE_ERROR(code) && pRequest->sqlstr != NULL) { tscDebug("0x%" PRIx64 " client retry to handle the error, code:%s, tryCount:%d, reqId:0x%" PRIx64, pRequest->self, tstrerror(code), pRequest->retry, pRequest->requestId); - removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type)); + (void)removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type)); restartAsyncQuery(pRequest, code); return; } tscDebug("schedulerExecCb request type %s", TMSG_INFO(pRequest->type)); if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) { - removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type)); + (void)removeMeta(pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type)); } pRequest->metric.execCostUs = taosGetTimestampUs() - pRequest->metric.execStart; @@ -1084,9 +1189,9 @@ SRequestObj* launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, bool keepQue STscObj* pTscObj = pRequest->pTscObj; SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary; if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type) { - atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1); + (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1); } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) { - atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1); + (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1); } } @@ -1109,15 +1214,20 @@ SRequestObj* launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, bool keepQue break; case QUERY_EXEC_MODE_SCHEDULE: { SArray* pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad)); + if (NULL == pMnodeList) { + code = terrno; + break; + } SQueryPlan* pDag = NULL; code = getPlan(pRequest, pQuery, &pDag, pMnodeList); if (TSDB_CODE_SUCCESS == code) { pRequest->body.subplanNum = pDag->numOfSubplans; if (!pRequest->validateOnly) { SArray* pNodeList = NULL; - buildSyncExecNodeList(pRequest, &pNodeList, pMnodeList); - - code = scheduleQuery(pRequest, pDag, pNodeList); + code = buildSyncExecNodeList(pRequest, &pNodeList, pMnodeList); + if (TSDB_CODE_SUCCESS == code) { + code = scheduleQuery(pRequest, pDag, pNodeList); + } taosArrayDestroy(pNodeList); } } @@ -1136,10 +1246,12 @@ SRequestObj* launchQueryImpl(SRequestObj* pRequest, SQuery* pQuery, bool keepQue } if (NEED_CLIENT_RM_TBLMETA_REQ(pRequest->type) && NULL == pRequest->body.resInfo.execRes.res) { - removeMeta(pRequest->pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type)); + (void)removeMeta(pRequest->pTscObj, pRequest->targetTableList, IS_VIEW_REQUEST(pRequest->type)); } - handleQueryExecRsp(pRequest); + if (TSDB_CODE_SUCCESS == code) { + code = handleQueryExecRsp(pRequest); + } if (TSDB_CODE_SUCCESS != code) { pRequest->code = terrno; @@ -1163,7 +1275,9 @@ static int32_t asyncExecSchQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaDat if (!pRequest->parseOnly) { pMnodeList = taosArrayInit(4, sizeof(SQueryNodeLoad)); - + if (NULL == pMnodeList) { + code = terrno; + } SPlanContext cxt = {.queryId = pRequest->requestId, .acctId = pRequest->pTscObj->acctId, .mgmtEpSet = getEpSet_s(&pRequest->pTscObj->pAppInfo->mgmtEp), @@ -1176,8 +1290,9 @@ static int32_t asyncExecSchQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaDat .pUser = pRequest->pTscObj->user, .sysInfo = pRequest->pTscObj->sysInfo, .allocatorId = pRequest->allocatorRefId}; - - code = qCreateQueryPlan(&cxt, &pDag, pMnodeList); + if (TSDB_CODE_SUCCESS == code) { + code = qCreateQueryPlan(&cxt, &pDag, pMnodeList); + } if (code) { tscError("0x%" PRIx64 " failed to create query plan, code:%s 0x%" PRIx64, pRequest->self, tstrerror(code), pRequest->requestId); @@ -1193,7 +1308,7 @@ static int32_t asyncExecSchQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaDat if (TSDB_CODE_SUCCESS == code && !pRequest->validateOnly) { SArray* pNodeList = NULL; if (QUERY_NODE_VNODE_MODIFY_STMT != nodeType(pQuery->pRoot)) { - buildAsyncExecNodeList(pRequest, &pNodeList, pMnodeList, pResultMeta); + code = buildAsyncExecNodeList(pRequest, &pNodeList, pMnodeList, pResultMeta); } SRequestConnInfo conn = {.pTrans = getAppInfo(pRequest)->pTransporter, @@ -1215,7 +1330,9 @@ static int32_t asyncExecSchQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaDat .pExecRes = NULL, .source = pRequest->source, }; - code = schedulerExecJob(&req, &pRequest->body.queryJob); + if (TSDB_CODE_SUCCESS == code) { + code = schedulerExecJob(&req, &pRequest->body.queryJob); + } taosArrayDestroy(pNodeList); } else { qDestroyQueryPlan(pDag); @@ -1255,9 +1372,9 @@ void launchAsyncQuery(SRequestObj* pRequest, SQuery* pQuery, SMetaData* pResultM SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary; if (QUERY_NODE_VNODE_MODIFY_STMT == pQuery->pRoot->type && (0 == ((SVnodeModifyOpStmt*)pQuery->pRoot)->sqlNodeType)) { - atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1); + (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfInsertsReq, 1); } else if (QUERY_NODE_SELECT_STMT == pQuery->pRoot->type) { - atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1); + (void)atomic_add_fetch_64((int64_t*)&pActivity->numOfQueryReq, 1); } } @@ -1306,6 +1423,7 @@ int32_t refreshMeta(STscObj* pTscObj, SRequestObj* pRequest) { for (int32_t i = 0; i < dbNum; ++i) { char* dbFName = taosArrayGet(pRequest->dbList, i); + // catalogRefreshDBVgInfo will handle dbFName == null. code = catalogRefreshDBVgInfo(pCatalog, &conn, dbFName); if (code != TSDB_CODE_SUCCESS) { return code; @@ -1315,6 +1433,7 @@ int32_t refreshMeta(STscObj* pTscObj, SRequestObj* pRequest) { for (int32_t i = 0; i < tblNum; ++i) { SName* tableName = taosArrayGet(pRequest->tableList, i); + // catalogRefreshTableMeta will handle tableName == null. code = catalogRefreshTableMeta(pCatalog, &conn, tableName, -1); if (code != TSDB_CODE_SUCCESS) { return code; @@ -1336,20 +1455,23 @@ int32_t removeMeta(STscObj* pTscObj, SArray* tbList, bool isView) { for (int32_t i = 0; i < tbNum; ++i) { SName* pViewName = taosArrayGet(tbList, i); char dbFName[TSDB_DB_FNAME_LEN]; - tNameGetFullDbName(pViewName, dbFName); - catalogRemoveViewMeta(pCatalog, dbFName, 0, pViewName->tname, 0); + if (NULL == pViewName) { + continue; + } + (void)tNameGetFullDbName(pViewName, dbFName); + (void)catalogRemoveViewMeta(pCatalog, dbFName, 0, pViewName->tname, 0); } } else { for (int32_t i = 0; i < tbNum; ++i) { SName* pTbName = taosArrayGet(tbList, i); - catalogRemoveTableMeta(pCatalog, pTbName); + (void)catalogRemoveTableMeta(pCatalog, pTbName); } } return TSDB_CODE_SUCCESS; } -int initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSet) { +int32_t initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSet) { pEpSet->version = 0; // init mnode ip set @@ -1373,7 +1495,7 @@ int initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSe if (code) { tscError("failed to resolve firstEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, tstrerror(TSDB_CODE_TSC_INVALID_FQDN)); - memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps])); + (void)memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps])); } else { mgmtEpSet->numOfEps++; } @@ -1382,16 +1504,19 @@ int initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSe if (secondEp && secondEp[0] != 0) { if (strlen(secondEp) >= TSDB_EP_LEN) { terrno = TSDB_CODE_TSC_INVALID_FQDN; - return -1; + return terrno; } - taosGetFqdnPortFromEp(secondEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]); + int32_t code = taosGetFqdnPortFromEp(secondEp, &mgmtEpSet->eps[mgmtEpSet->numOfEps]); + if (code != TSDB_CODE_SUCCESS) { + return code; + } uint32_t addr = 0; - int32_t code = taosGetIpv4FromFqdn(mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr); + code = taosGetIpv4FromFqdn(mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, &addr); if (code) { tscError("failed to resolve secondEp fqdn: %s, code:%s", mgmtEpSet->eps[mgmtEpSet->numOfEps].fqdn, tstrerror(TSDB_CODE_TSC_INVALID_FQDN)); - memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps])); + (void)memset(&(mgmtEpSet->eps[mgmtEpSet->numOfEps]), 0, sizeof(mgmtEpSet->eps[mgmtEpSet->numOfEps])); } else { mgmtEpSet->numOfEps++; } @@ -1405,18 +1530,19 @@ int initEpSetFromCfg(const char* firstEp, const char* secondEp, SCorEpSet* pEpSe return 0; } -STscObj* taosConnectImpl(const char* user, const char* auth, const char* db, __taos_async_fn_t fp, void* param, - SAppInstInfo* pAppInfo, int connType) { - STscObj* pTscObj = createTscObj(user, auth, db, connType, pAppInfo); - if (NULL == pTscObj) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return pTscObj; +int32_t taosConnectImpl(const char* user, const char* auth, const char* db, __taos_async_fn_t fp, void* param, + SAppInstInfo* pAppInfo, int connType, STscObj** pTscObj) { + *pTscObj = NULL; + int32_t code = createTscObj(user, auth, db, connType, pAppInfo, pTscObj); + if (TSDB_CODE_SUCCESS != code) { + return code; } - SRequestObj* pRequest = createRequest(pTscObj->id, TDMT_MND_CONNECT, 0); - if (pRequest == NULL) { - destroyTscObj(pTscObj); - return NULL; + SRequestObj* pRequest = NULL; + code = createRequest((*pTscObj)->id, TDMT_MND_CONNECT, 0, &pRequest); + if (TSDB_CODE_SUCCESS != code) { + destroyTscObj(*pTscObj); + return code; } pRequest->sqlstr = taosStrdup("taos_connect"); @@ -1424,45 +1550,56 @@ STscObj* taosConnectImpl(const char* user, const char* auth, const char* db, __t pRequest->sqlLen = strlen(pRequest->sqlstr); } - SMsgSendInfo* body = buildConnectMsg(pRequest); + SMsgSendInfo* body = NULL; + code = buildConnectMsg(pRequest, &body); + if (TSDB_CODE_SUCCESS != code) { + destroyTscObj(*pTscObj); + return code; + } int64_t transporterId = 0; - asyncSendMsgToServer(pTscObj->pAppInfo->pTransporter, &pTscObj->pAppInfo->mgmtEp.epSet, &transporterId, body); - - tsem_wait(&pRequest->body.rspSem); + code = asyncSendMsgToServer((*pTscObj)->pAppInfo->pTransporter, &(*pTscObj)->pAppInfo->mgmtEp.epSet, &transporterId, body); + if (TSDB_CODE_SUCCESS != code) { + destroyTscObj(*pTscObj); + tscError("failed to send connect msg to server, code:%s", tstrerror(code)); + return code; + } + (void)tsem_wait(&pRequest->body.rspSem); if (pRequest->code != TSDB_CODE_SUCCESS) { - const char* errorMsg = - (pRequest->code == TSDB_CODE_RPC_FQDN_ERROR) ? taos_errstr(pRequest) : tstrerror(pRequest->code); + const char* errorMsg = (code == TSDB_CODE_RPC_FQDN_ERROR) ? taos_errstr(pRequest) : tstrerror(pRequest->code); tscError("failed to connect to server, reason: %s", errorMsg); terrno = pRequest->code; destroyRequest(pRequest); - taos_close_internal(pTscObj); - pTscObj = NULL; + taos_close_internal(*pTscObj); + *pTscObj = NULL; + return terrno; } else { - tscDebug("0x%" PRIx64 " connection is opening, connId:%u, dnodeConn:%p, reqId:0x%" PRIx64, pTscObj->id, - pTscObj->connId, pTscObj->pAppInfo->pTransporter, pRequest->requestId); + tscDebug("0x%" PRIx64 " connection is opening, connId:%u, dnodeConn:%p, reqId:0x%" PRIx64, (*pTscObj)->id, + (*pTscObj)->connId, (*pTscObj)->pAppInfo->pTransporter, pRequest->requestId); destroyRequest(pRequest); } - - return pTscObj; + return code; } -static SMsgSendInfo* buildConnectMsg(SRequestObj* pRequest) { - SMsgSendInfo* pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo)); - if (pMsgSendInfo == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; +static int32_t buildConnectMsg(SRequestObj* pRequest, SMsgSendInfo** pMsgSendInfo) { + *pMsgSendInfo = taosMemoryCalloc(1, sizeof(SMsgSendInfo)); + if (*pMsgSendInfo == NULL) { + return TSDB_CODE_OUT_OF_MEMORY; } - pMsgSendInfo->msgType = TDMT_MND_CONNECT; + (*pMsgSendInfo)->msgType = TDMT_MND_CONNECT; - pMsgSendInfo->requestObjRefId = pRequest->self; - pMsgSendInfo->requestId = pRequest->requestId; - pMsgSendInfo->fp = getMsgRspHandle(pMsgSendInfo->msgType); - pMsgSendInfo->param = taosMemoryCalloc(1, sizeof(pRequest->self)); + (*pMsgSendInfo)->requestObjRefId = pRequest->self; + (*pMsgSendInfo)->requestId = pRequest->requestId; + (*pMsgSendInfo)->fp = getMsgRspHandle((*pMsgSendInfo)->msgType); + (*pMsgSendInfo)->param = taosMemoryCalloc(1, sizeof(pRequest->self)); + if (NULL == (*pMsgSendInfo)->param) { + taosMemoryFree(*pMsgSendInfo); + return TSDB_CODE_OUT_OF_MEMORY; + } - *(int64_t*)pMsgSendInfo->param = pRequest->self; + *(int64_t*)(*pMsgSendInfo)->param = pRequest->self; SConnectReq connectReq = {0}; STscObj* pObj = pRequest->pTscObj; @@ -1484,11 +1621,20 @@ static SMsgSendInfo* buildConnectMsg(SRequestObj* pRequest) { int32_t contLen = tSerializeSConnectReq(NULL, 0, &connectReq); void* pReq = taosMemoryMalloc(contLen); - tSerializeSConnectReq(pReq, contLen, &connectReq); + if (NULL == pReq) { + taosMemoryFree(*pMsgSendInfo); + return TSDB_CODE_OUT_OF_MEMORY; + } - pMsgSendInfo->msgInfo.len = contLen; - pMsgSendInfo->msgInfo.pData = pReq; - return pMsgSendInfo; + if (-1 == tSerializeSConnectReq(pReq, contLen, &connectReq)) { + taosMemoryFree(*pMsgSendInfo); + taosMemoryFree(pReq); + return terrno; + } + + (*pMsgSendInfo)->msgInfo.len = contLen; + (*pMsgSendInfo)->msgInfo.pData = pReq; + return TSDB_CODE_SUCCESS; } void updateTargetEpSet(SMsgSendInfo* pSendInfo, STscObj* pTscObj, SRpcMsg* pMsg, SEpSet* pEpSet) { @@ -1526,7 +1672,12 @@ void updateTargetEpSet(SMsgSendInfo* pSendInfo, STscObj* pTscObj, SRpcMsg* pMsg, return; } - catalogUpdateVgEpSet(pCatalog, pSendInfo->target.dbFName, pSendInfo->target.vgId, pEpSet); + code = catalogUpdateVgEpSet(pCatalog, pSendInfo->target.dbFName, pSendInfo->target.vgId, pEpSet); + if (code != TSDB_CODE_SUCCESS) { + tscError("fail to update catalog vg epset, clusterId:%" PRIx64 ", error %s", pTscObj->pAppInfo->clusterId, + tstrerror(code)); + return; + } taosMemoryFreeClear(pSendInfo->target.dbFName); break; } @@ -1565,7 +1716,7 @@ int32_t doProcessMsgFromServer(void* param) { tscError("doProcessMsgFromServer pRequest->self:%" PRId64 " != pSendInfo->requestObjRefId:%" PRId64, pRequest->self, pSendInfo->requestObjRefId); - taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId); + (void)taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId); taosMemoryFree(arg->pEpset); rpcFreeCont(pMsg->pCont); destroySendMsgInfo(pSendInfo); @@ -1591,14 +1742,14 @@ int32_t doProcessMsgFromServer(void* param) { terrno = TSDB_CODE_OUT_OF_MEMORY; pMsg->code = TSDB_CODE_OUT_OF_MEMORY; } else { - memcpy(buf.pData, pMsg->pCont, pMsg->contLen); + (void)memcpy(buf.pData, pMsg->pCont, pMsg->contLen); } } - pSendInfo->fp(pSendInfo->param, &buf, pMsg->code); + (void)pSendInfo->fp(pSendInfo->param, &buf, pMsg->code); if (pTscObj) { - taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId); + (void)taosReleaseRef(clientReqRefPool, pSendInfo->requestObjRefId); } rpcFreeCont(pMsg->pCont); @@ -1612,7 +1763,13 @@ void processMsgFromServer(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) { SEpSet* tEpSet = NULL; if (pEpSet != NULL) { tEpSet = taosMemoryCalloc(1, sizeof(SEpSet)); - memcpy((void*)tEpSet, (void*)pEpSet, sizeof(SEpSet)); + if (NULL == tEpSet) { + pMsg->code = TSDB_CODE_OUT_OF_MEMORY; + rpcFreeCont(pMsg->pCont); + destroySendMsgInfo(pMsg->info.ahandle); + return; + } + (void)memcpy((void*)tEpSet, (void*)pEpSet, sizeof(SEpSet)); } // pMsg is response msg @@ -1631,6 +1788,12 @@ void processMsgFromServer(void* parent, SRpcMsg* pMsg, SEpSet* pEpSet) { } AsyncArg* arg = taosMemoryCalloc(1, sizeof(AsyncArg)); + if (NULL == arg) { + pMsg->code = TSDB_CODE_OUT_OF_MEMORY; + rpcFreeCont(pMsg->pCont); + destroySendMsgInfo(pMsg->info.ahandle); + return; + } arg->msg = *pMsg; arg->pEpset = tEpSet; @@ -1654,9 +1817,13 @@ TAOS* taos_connect_auth(const char* ip, const char* user, const char* auth, cons return NULL; } - STscObj* pObj = taos_connect_internal(ip, user, NULL, auth, db, port, CONN_TYPE__QUERY); - if (pObj) { + STscObj* pObj = NULL; + int32_t code = taos_connect_internal(ip, user, NULL, auth, db, port, CONN_TYPE__QUERY, &pObj); + if (TSDB_CODE_SUCCESS == code) { int64_t* rid = taosMemoryCalloc(1, sizeof(int64_t)); + if (NULL == rid) { + tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db); + } *rid = pObj->id; return (TAOS*)rid; } @@ -1671,10 +1838,10 @@ TAOS* taos_connect_l(const char* ip, int ipLen, const char* user, int userLen, c char userStr[TSDB_USER_LEN] = {0}; char passStr[TSDB_PASSWORD_LEN] = {0}; - strncpy(ipStr, ip, TMIN(TSDB_EP_LEN - 1, ipLen)); - strncpy(userStr, user, TMIN(TSDB_USER_LEN - 1, userLen)); - strncpy(passStr, pass, TMIN(TSDB_PASSWORD_LEN - 1, passLen)); - strncpy(dbStr, db, TMIN(TSDB_DB_NAME_LEN - 1, dbLen)); + (void)strncpy(ipStr, ip, TMIN(TSDB_EP_LEN - 1, ipLen)); + (void)strncpy(userStr, user, TMIN(TSDB_USER_LEN - 1, userLen)); + (void)strncpy(passStr, pass, TMIN(TSDB_PASSWORD_LEN - 1, passLen)); + (void)strncpy(dbStr, db, TMIN(TSDB_DB_NAME_LEN - 1, dbLen)); return taos_connect(ipStr, userStr, passStr, dbStr, port); } @@ -1741,7 +1908,7 @@ void* doFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4) STscObj* pTscObj = pRequest->pTscObj; SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary; - atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen); + (void)atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen); if (pResultInfo->numOfRows == 0) { return NULL; @@ -1758,7 +1925,7 @@ void* doFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4) static void syncFetchFn(void* param, TAOS_RES* res, int32_t numOfRows) { tsem_t* sem = param; - tsem_post(sem); + (void)tsem_post(sem); } void* doAsyncFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertUcs4) { @@ -1777,10 +1944,10 @@ void* doAsyncFetchRows(SRequestObj* pRequest, bool setupOneRowPtr, bool convertU // convert ucs4 to native multi-bytes string pResultInfo->convertUcs4 = convertUcs4; tsem_t sem; - tsem_init(&sem, 0, 0); + (void)tsem_init(&sem, 0, 0); taos_fetch_rows_a(pRequest, syncFetchFn, &sem); - tsem_wait(&sem); - tsem_destroy(&sem); + (void)tsem_wait(&sem); + (void)tsem_destroy(&sem); pRequest->inCallback = false; } @@ -1963,7 +2130,7 @@ static int32_t doConvertJson(SReqResultInfo* pResultInfo, int32_t numOfCols, int } int32_t len = getVersion1BlockMetaSize(p, numOfCols); - memcpy(p1, p, len); + (void)memcpy(p1, p, len); p += len; p1 += len; @@ -1972,7 +2139,7 @@ static int32_t doConvertJson(SReqResultInfo* pResultInfo, int32_t numOfCols, int len = sizeof(int32_t) * numOfCols; int32_t* colLength = (int32_t*)p; int32_t* colLength1 = (int32_t*)p1; - memcpy(p1, p, len); + (void)memcpy(p1, p, len); p += len; p1 += len; totalLen += len; @@ -1990,7 +2157,7 @@ static int32_t doConvertJson(SReqResultInfo* pResultInfo, int32_t numOfCols, int int32_t* offset = (int32_t*)pStart; int32_t* offset1 = (int32_t*)pStart1; len = numOfRows * sizeof(int32_t); - memcpy(pStart1, pStart, len); + (void)memcpy(pStart1, pStart, len); pStart += len; pStart1 += len; totalLen += len; @@ -2006,7 +2173,7 @@ static int32_t doConvertJson(SReqResultInfo* pResultInfo, int32_t numOfCols, int char* jsonInnerData = data + CHAR_BYTES; char dst[TSDB_MAX_JSON_TAG_LEN] = {0}; if (jsonInnerType == TSDB_DATA_TYPE_NULL) { - sprintf(varDataVal(dst), "%s", TSDB_DATA_NULL_STR_L); + (void)sprintf(varDataVal(dst), "%s", TSDB_DATA_NULL_STR_L); varDataSetLen(dst, strlen(varDataVal(dst))); } else if (tTagIsJson(data)) { char* jsonString = NULL; @@ -2025,10 +2192,10 @@ static int32_t doConvertJson(SReqResultInfo* pResultInfo, int32_t numOfCols, int *(char*)POINTER_SHIFT(varDataVal(dst), length + CHAR_BYTES) = '\"'; } else if (jsonInnerType == TSDB_DATA_TYPE_DOUBLE) { double jsonVd = *(double*)(jsonInnerData); - sprintf(varDataVal(dst), "%.9lf", jsonVd); + (void)sprintf(varDataVal(dst), "%.9lf", jsonVd); varDataSetLen(dst, strlen(varDataVal(dst))); } else if (jsonInnerType == TSDB_DATA_TYPE_BOOL) { - sprintf(varDataVal(dst), "%s", (*((char*)jsonInnerData) == 1) ? "true" : "false"); + (void)sprintf(varDataVal(dst), "%s", (*((char*)jsonInnerData) == 1) ? "true" : "false"); varDataSetLen(dst, strlen(varDataVal(dst))); } else { tscError("doConvertJson error: invalid type:%d", jsonInnerType); @@ -2036,7 +2203,7 @@ static int32_t doConvertJson(SReqResultInfo* pResultInfo, int32_t numOfCols, int } offset1[j] = len; - memcpy(pStart1 + len, dst, varDataTLen(dst)); + (void)memcpy(pStart1 + len, dst, varDataTLen(dst)); len += varDataTLen(dst); } colLen1 = len; @@ -2044,20 +2211,20 @@ static int32_t doConvertJson(SReqResultInfo* pResultInfo, int32_t numOfCols, int colLength1[i] = (blockVersion == BLOCK_VERSION_1) ? htonl(len) : len; } else if (IS_VAR_DATA_TYPE(pResultInfo->fields[i].type)) { len = numOfRows * sizeof(int32_t); - memcpy(pStart1, pStart, len); + (void)memcpy(pStart1, pStart, len); pStart += len; pStart1 += len; totalLen += len; totalLen += colLen; - memcpy(pStart1, pStart, colLen); + (void)memcpy(pStart1, pStart, colLen); } else { len = BitmapLen(pResultInfo->numOfRows); - memcpy(pStart1, pStart, len); + (void)memcpy(pStart1, pStart, len); pStart += len; pStart1 += len; totalLen += len; totalLen += colLen; - memcpy(pStart1, pStart, colLen); + (void)memcpy(pStart1, pStart, colLen); } pStart += colLen; pStart1 += colLen1; @@ -2166,13 +2333,13 @@ int32_t setResultDataPtr(SReqResultInfo* pResultInfo, TAOS_FIELD* pFields, int32 char* getDbOfConnection(STscObj* pObj) { char* p = NULL; - taosThreadMutexLock(&pObj->mutex); + (void)taosThreadMutexLock(&pObj->mutex); size_t len = strlen(pObj->db); if (len > 0) { p = strndup(pObj->db, tListLen(pObj->db)); } - taosThreadMutexUnlock(&pObj->mutex); + (void)taosThreadMutexUnlock(&pObj->mutex); return p; } @@ -2182,9 +2349,9 @@ void setConnectionDB(STscObj* pTscObj, const char* db) { return; } - taosThreadMutexLock(&pTscObj->mutex); + (void)taosThreadMutexLock(&pTscObj->mutex); tstrncpy(pTscObj->db, db, tListLen(pTscObj->db)); - taosThreadMutexUnlock(&pTscObj->mutex); + (void)taosThreadMutexUnlock(&pTscObj->mutex); } void resetConnectDB(STscObj* pTscObj) { @@ -2192,9 +2359,9 @@ void resetConnectDB(STscObj* pTscObj) { return; } - taosThreadMutexLock(&pTscObj->mutex); + (void)taosThreadMutexLock(&pTscObj->mutex); pTscObj->db[0] = 0; - taosThreadMutexUnlock(&pTscObj->mutex); + (void)taosThreadMutexUnlock(&pTscObj->mutex); } int32_t setQueryResultFromRsp(SReqResultInfo* pResultInfo, const SRetrieveTableRsp* pRsp, bool convertUcs4) { @@ -2216,6 +2383,11 @@ int32_t setQueryResultFromRsp(SReqResultInfo* pResultInfo, const SRetrieveTableR if (pRsp->compressed) { if (pResultInfo->decompBuf == NULL) { pResultInfo->decompBuf = taosMemoryMalloc(payloadLen); + if (pResultInfo->decompBuf == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + tscError("failed to prepare the decompress buffer, size:%d", payloadLen); + return terrno; + } pResultInfo->decompBufSize = payloadLen; } else { if (pResultInfo->decompBufSize < payloadLen) { @@ -2241,6 +2413,10 @@ int32_t setQueryResultFromRsp(SReqResultInfo* pResultInfo, const SRetrieveTableR if (pRsp->compressed && compLen < rawLen) { int32_t len = tsDecompressString(pStart, compLen, 1, pResultInfo->decompBuf, rawLen, ONE_STAGE_COMP, NULL, 0); ASSERT(len == rawLen); + if (len < 0) { + tscError("tsDecompressString failed"); + return terrno ? terrno : TSDB_CODE_FAILED; + } pResultInfo->pData = pResultInfo->decompBuf; pResultInfo->payloadLen = rawLen; @@ -2283,7 +2459,10 @@ TSDB_SERVER_STATUS taos_check_server_status(const char* fqdn, int port, char* de connLimitNum = TMIN(connLimitNum, 500); rpcInit.connLimitNum = connLimitNum; rpcInit.timeToGetConn = tsTimeToGetAvailableConn; - taosVersionStrToInt(version, &(rpcInit.compatibilityVer)); + if (TSDB_CODE_SUCCESS != taosVersionStrToInt(version, &(rpcInit.compatibilityVer))) { + tscError("faild to convert taos version from str to int, errcode:%s", terrstr()); + goto _OVER; + } clientRpc = rpcOpen(&rpcInit); if (clientRpc == NULL) { @@ -2301,7 +2480,11 @@ TSDB_SERVER_STATUS taos_check_server_status(const char* fqdn, int port, char* de tstrncpy(epSet.eps[0].fqdn, fqdn, TSDB_FQDN_LEN); epSet.eps[0].port = (uint16_t)port; - rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp); + int32_t ret = rpcSendRecv(clientRpc, &epSet, &rpcMsg, &rpcRsp); + if (TSDB_CODE_SUCCESS != ret) { + tscError("failed to send recv since %s", tstrerror(ret)); + goto _OVER; + } if (rpcRsp.code != 0 || rpcRsp.contLen <= 0 || rpcRsp.pCont == NULL) { tscError("failed to send server status req since %s", terrstr()); @@ -2368,13 +2551,20 @@ int32_t appendTbToReq(SHashObj* pHash, int32_t pos1, int32_t len1, int32_t pos2, STablesReq* pDb = taosHashGet(pHash, dbFName, strlen(dbFName)); if (pDb) { - taosArrayPush(pDb->pTables, &name); + if (NULL == taosArrayPush(pDb->pTables, &name)) { + return terrno ? terrno : TSDB_CODE_OUT_OF_MEMORY; + } } else { STablesReq db; db.pTables = taosArrayInit(20, sizeof(SName)); - strcpy(db.dbFName, dbFName); - taosArrayPush(db.pTables, &name); - taosHashPut(pHash, dbFName, strlen(dbFName), &db, sizeof(db)); + if (NULL == db.pTables) { + return terrno; + } + (void)strcpy(db.dbFName, dbFName); + if (NULL == taosArrayPush(db.pTables, &name)) { + return terrno; + } + TSC_ERR_RET(taosHashPut(pHash, dbFName, strlen(dbFName), &db, sizeof(db))); } return TSDB_CODE_SUCCESS; @@ -2383,7 +2573,6 @@ int32_t appendTbToReq(SHashObj* pHash, int32_t pos1, int32_t len1, int32_t pos2, int32_t transferTableNameList(const char* tbList, int32_t acctId, char* dbName, SArray** pReq) { SHashObj* pHash = taosHashInit(3, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK); if (NULL == pHash) { - terrno = TSDB_CODE_OUT_OF_MEMORY; return terrno; } @@ -2395,8 +2584,8 @@ int32_t transferTableNameList(const char* tbList, int32_t acctId, char* dbName, int32_t vPos[2]; int32_t vLen[2]; - memset(vPos, -1, sizeof(vPos)); - memset(vLen, 0, sizeof(vLen)); + (void)memset(vPos, -1, sizeof(vPos)); + (void)memset(vLen, 0, sizeof(vLen)); for (int32_t i = 0;; ++i) { if (0 == *(tbList + i)) { @@ -2459,8 +2648,8 @@ int32_t transferTableNameList(const char* tbList, int32_t acctId, char* dbName, goto _return; } - memset(vPos, -1, sizeof(vPos)); - memset(vLen, 0, sizeof(vLen)); + (void)memset(vPos, -1, sizeof(vPos)); + (void)memset(vLen, 0, sizeof(vLen)); vIdx = 0; continue; } @@ -2488,10 +2677,15 @@ int32_t transferTableNameList(const char* tbList, int32_t acctId, char* dbName, int32_t dbNum = taosHashGetSize(pHash); *pReq = taosArrayInit(dbNum, sizeof(STablesReq)); + if (NULL == pReq) { + TSC_ERR_JRET(terrno); + } pIter = taosHashIterate(pHash, NULL); while (pIter) { STablesReq* pDb = (STablesReq*)pIter; - taosArrayPush(*pReq, pDb); + if (NULL == taosArrayPush(*pReq, pDb)) { + TSC_ERR_JRET(terrno); + } pIter = taosHashIterate(pHash, pIter); } @@ -2519,7 +2713,7 @@ void syncCatalogFn(SMetaData* pResult, void* param, int32_t code) { SSyncQueryParam* pParam = param; pParam->pRequest->code = code; - tsem_post(&pParam->sem); + (void)tsem_post(&pParam->sem); } void syncQueryFn(void* param, void* res, int32_t code) { @@ -2530,7 +2724,7 @@ void syncQueryFn(void* param, void* res, int32_t code) { pParam->pRequest->code = code; } - tsem_post(&pParam->sem); + (void)tsem_post(&pParam->sem); } void taosAsyncQueryImpl(uint64_t connId, const char* sql, __taos_async_fn_t fp, void* param, bool validateOnly, int8_t source) { @@ -2607,10 +2801,10 @@ TAOS_RES* taosQueryImpl(TAOS* taos, const char* sql, bool validateOnly, int8_t s terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } - tsem_init(¶m->sem, 0, 0); + (void)tsem_init(¶m->sem, 0, 0); taosAsyncQueryImpl(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, source); - tsem_wait(¶m->sem); + (void)tsem_wait(¶m->sem); SRequestObj* pRequest = NULL; if (param->pRequest != NULL) { @@ -2636,10 +2830,10 @@ TAOS_RES* taosQueryImplWithReqid(TAOS* taos, const char* sql, bool validateOnly, terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } - tsem_init(¶m->sem, 0, 0); + (void)tsem_init(¶m->sem, 0, 0); taosAsyncQueryImplWithReqid(*(int64_t*)taos, sql, syncQueryFn, param, validateOnly, reqid); - tsem_wait(¶m->sem); + (void)tsem_wait(¶m->sem); SRequestObj* pRequest = NULL; if (param->pRequest != NULL) { param->pRequest->syncQuery = true; @@ -2686,7 +2880,7 @@ static void fetchCallback(void* pResult, void* param, int32_t code) { STscObj* pTscObj = pRequest->pTscObj; SAppClusterSummary* pActivity = &pTscObj->pAppInfo->summary; - atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen); + (void)atomic_add_fetch_64((int64_t*)&pActivity->fetchBytes, pRequest->body.resInfo.payloadLen); } pRequest->body.fetchFp(((SSyncQueryParam*)pRequest->body.interParam)->userParam, pRequest, pResultInfo->numOfRows); @@ -2729,7 +2923,9 @@ void taosAsyncFetchImpl(SRequestObj* pRequest, __taos_async_fn_t fp, void* param .cbParam = pRequest, }; - schedulerFetchRows(pRequest->body.queryJob, &req); + if (TSDB_CODE_SUCCESS != schedulerFetchRows(pRequest->body.queryJob, &req)) { + tscError("0x%" PRIx64 " failed to schedule fetch rows", pRequest->self); + } } void doRequestCallback(SRequestObj* pRequest, int32_t code) { @@ -2739,7 +2935,7 @@ void doRequestCallback(SRequestObj* pRequest, int32_t code) { SRequestObj* pReq = acquireRequest(this); if (pReq != NULL) { pReq->inCallback = false; - releaseRequest(this); + (void)releaseRequest(this); } } diff --git a/source/client/src/clientJniConnector.c b/source/client/src/clientJniConnector.c index edbc06b12b..4a200179f9 100644 --- a/source/client/src/clientJniConnector.c +++ b/source/client/src/clientJniConnector.c @@ -73,7 +73,7 @@ void jniGetGlobalMethod(JNIEnv *env) { } if (g_vm == NULL) { - (*env)->GetJavaVM(env, &g_vm); + (void)(*env)->GetJavaVM(env, &g_vm); } jclass arrayListClass = (*env)->FindClass(env, "java/util/ArrayList"); @@ -582,20 +582,20 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_fetchRowImp(JNIEn case TSDB_DATA_TYPE_BINARY: case TSDB_DATA_TYPE_VARBINARY: case TSDB_DATA_TYPE_GEOMETRY: { - memcpy(tmp, row[i], length[i]); // handle the case that terminated does not exist + (void)memcpy(tmp, row[i], length[i]); // handle the case that terminated does not exist (*env)->CallVoidMethod(env, rowobj, g_rowdataSetStringFp, i, (*env)->NewStringUTF(env, tmp)); - memset(tmp, 0, length[i]); + (void)memset(tmp, 0, length[i]); break; } case TSDB_DATA_TYPE_NCHAR: { (*env)->CallVoidMethod(env, rowobj, g_rowdataSetByteArrayFp, i, - jniFromNCharToByteArray(env, (char *)row[i], length[i])); + jniFromNCharToByteArray(env, (char *)row[i], length[i])); break; } case TSDB_DATA_TYPE_JSON: { (*env)->CallVoidMethod(env, rowobj, g_rowdataSetByteArrayFp, i, - jniFromNCharToByteArray(env, (char *)row[i], length[i])); + jniFromNCharToByteArray(env, (char *)row[i], length[i])); break; } case TSDB_DATA_TYPE_TIMESTAMP: { @@ -680,6 +680,10 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_validateCreateTab jsize len = (*env)->GetArrayLength(env, jsql); char *str = (char *)taosMemoryCalloc(1, sizeof(char) * (len + 1)); + if (str == NULL) { + jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tscon); + return JNI_OUT_OF_MEMORY; + } (*env)->GetByteArrayRegion(env, jsql, 0, len, (jbyte *)str); if ((*env)->ExceptionCheck(env)) { // todo handle error @@ -843,6 +847,10 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_setTableNameTagsI jsize len = (*env)->GetArrayLength(env, tags); char *tagsData = (char *)taosMemoryCalloc(1, len); + if (tagsData == NULL) { + jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tsconn); + return JNI_OUT_OF_MEMORY; + } (*env)->GetByteArrayRegion(env, tags, 0, len, (jbyte *)tagsData); if ((*env)->ExceptionCheck(env)) { // todo handle error @@ -850,18 +858,30 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_setTableNameTagsI len = (*env)->GetArrayLength(env, lengthList); int32_t *lengthArray = (int32_t *)taosMemoryCalloc(1, len); + if (lengthArray == NULL) { + jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tsconn); + return JNI_OUT_OF_MEMORY; + } (*env)->GetByteArrayRegion(env, lengthList, 0, len, (jbyte *)lengthArray); if ((*env)->ExceptionCheck(env)) { } len = (*env)->GetArrayLength(env, typeList); char *typeArray = (char *)taosMemoryCalloc(1, len); + if (typeArray == NULL) { + jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tsconn); + return JNI_OUT_OF_MEMORY; + } (*env)->GetByteArrayRegion(env, typeList, 0, len, (jbyte *)typeArray); if ((*env)->ExceptionCheck(env)) { } len = (*env)->GetArrayLength(env, nullList); char *nullArray = (char *)taosMemoryCalloc(1, len); + if (nullArray == NULL) { + jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tsconn); + return JNI_OUT_OF_MEMORY; + } (*env)->GetByteArrayRegion(env, nullList, 0, len, (jbyte *)nullArray); if ((*env)->ExceptionCheck(env)) { } @@ -870,6 +890,10 @@ JNIEXPORT jint JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_setTableNameTagsI char *curTags = tagsData; TAOS_MULTI_BIND *tagsBind = taosMemoryCalloc(numOfTags, sizeof(TAOS_MULTI_BIND)); + if (tagsBind == NULL) { + jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tsconn); + return JNI_OUT_OF_MEMORY; + } for (int32_t i = 0; i < numOfTags; ++i) { tagsBind[i].buffer_type = typeArray[i]; tagsBind[i].buffer = curTags; @@ -916,6 +940,10 @@ JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_bindColDataImp( // todo refactor jsize len = (*env)->GetArrayLength(env, colDataList); char *colBuf = (char *)taosMemoryCalloc(1, len); + if (colBuf == NULL) { + jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tscon); + return JNI_OUT_OF_MEMORY; + } (*env)->GetByteArrayRegion(env, colDataList, 0, len, (jbyte *)colBuf); if ((*env)->ExceptionCheck(env)) { // todo handle error @@ -923,18 +951,30 @@ JNIEXPORT jlong JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_bindColDataImp( len = (*env)->GetArrayLength(env, lengthList); char *lengthArray = (char *)taosMemoryCalloc(1, len); + if (lengthArray == NULL) { + jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tscon); + return JNI_OUT_OF_MEMORY; + } (*env)->GetByteArrayRegion(env, lengthList, 0, len, (jbyte *)lengthArray); if ((*env)->ExceptionCheck(env)) { } len = (*env)->GetArrayLength(env, nullList); char *nullArray = (char *)taosMemoryCalloc(1, len); + if (nullArray == NULL) { + jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tscon); + return JNI_OUT_OF_MEMORY; + } (*env)->GetByteArrayRegion(env, nullList, 0, len, (jbyte *)nullArray); if ((*env)->ExceptionCheck(env)) { } // bind multi-rows with only one invoke. TAOS_MULTI_BIND *b = taosMemoryCalloc(1, sizeof(TAOS_MULTI_BIND)); + if (b == NULL) { + jniError("jobj:%p, conn:%p, alloc memory failed", jobj, tscon); + return JNI_OUT_OF_MEMORY; + } b->num = numOfRows; b->buffer_type = dataType; // todo check data type @@ -1043,14 +1083,14 @@ JNIEXPORT jstring JNICALL Java_com_taosdata_jdbc_TSDBJNIConnector_stmtErrorMsgIm TAOS *tscon = (TAOS *)con; if (tscon == NULL) { jniError("jobj:%p, connection already closed", jobj); - sprintf(errMsg, "jobj:%p, connection already closed", jobj); + (void)sprintf(errMsg, "jobj:%p, connection already closed", jobj); return (*env)->NewStringUTF(env, errMsg); } TAOS_STMT *pStmt = (TAOS_STMT *)stmt; if (pStmt == NULL) { jniError("jobj:%p, conn:%p, invalid stmt", jobj, tscon); - sprintf(errMsg, "jobj:%p, conn:%p, invalid stmt", jobj, tscon); + (void)sprintf(errMsg, "jobj:%p, conn:%p, invalid stmt", jobj, tscon); return (*env)->NewStringUTF(env, errMsg); } diff --git a/source/client/src/clientMain.c b/source/client/src/clientMain.c index 0618e233a0..ea8cec97ba 100644 --- a/source/client/src/clientMain.c +++ b/source/client/src/clientMain.c @@ -69,15 +69,21 @@ void taos_cleanup(void) { qCleanupKeywordsTable(); nodesDestroyAllocatorSet(); - cleanupTaskQueue(); + if (TSDB_CODE_SUCCESS != cleanupTaskQueue()) { + tscWarn("failed to cleanup task queue"); + } int32_t id = clientReqRefPool; clientReqRefPool = -1; - taosCloseRef(id); + if (TSDB_CODE_SUCCESS != taosCloseRef(id)) { + tscWarn("failed to close clientReqRefPool"); + } id = clientConnRefPool; clientConnRefPool = -1; - taosCloseRef(id); + if (TSDB_CODE_SUCCESS != taosCloseRef(id)) { + tscWarn("failed to close clientReqRefPool"); + } DestroyRegexCache(); rpcCleanup(); @@ -115,11 +121,19 @@ TAOS *taos_connect(const char *ip, const char *user, const char *pass, const cha pass = TSDB_DEFAULT_PASS; } - STscObj *pObj = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY); - if (pObj) { + STscObj *pObj = NULL; + int32_t code = taos_connect_internal(ip, user, pass, NULL, db, port, CONN_TYPE__QUERY, &pObj); + if (TSDB_CODE_SUCCESS == code) { int64_t *rid = taosMemoryCalloc(1, sizeof(int64_t)); + if (NULL == rid) { + tscError("out of memory when taos connect to %s:%u, user:%s db:%s", ip, port, user, db); + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } *rid = pObj->id; return (TAOS *)rid; + } else { + terrno = code; } return NULL; @@ -140,24 +154,24 @@ int taos_set_notify_cb(TAOS *taos, __taos_notify_fn_t fp, void *param, int type) switch (type) { case TAOS_NOTIFY_PASSVER: { - taosThreadMutexLock(&pObj->mutex); + TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex)); pObj->passInfo.fp = fp; pObj->passInfo.param = param; - taosThreadMutexUnlock(&pObj->mutex); + TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex)); break; } case TAOS_NOTIFY_WHITELIST_VER: { - taosThreadMutexLock(&pObj->mutex); + TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex)); pObj->whiteListInfo.fp = fp; pObj->whiteListInfo.param = param; - taosThreadMutexUnlock(&pObj->mutex); + TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex)); break; } case TAOS_NOTIFY_USER_DROPPED: { - taosThreadMutexLock(&pObj->mutex); + TSC_ERR_RET(taosThreadMutexLock(&pObj->mutex)); pObj->userDroppedInfo.fp = fp; pObj->userDroppedInfo.param = param; - taosThreadMutexUnlock(&pObj->mutex); + TSC_ERR_RET(taosThreadMutexUnlock(&pObj->mutex)); break; } default: { @@ -189,7 +203,13 @@ int32_t fetchWhiteListCallbackFn(void* param, SDataBuf* pMsg, int32_t code) { } SGetUserWhiteListRsp wlRsp; - tDeserializeSGetUserWhiteListRsp(pMsg->pData, pMsg->len, &wlRsp); + if (TSDB_CODE_SUCCESS != tDeserializeSGetUserWhiteListRsp(pMsg->pData, pMsg->len, &wlRsp)) { + taosMemoryFree(pMsg->pData); + taosMemoryFree(pMsg->pEpSet); + taosMemoryFree(pInfo); + tFreeSGetUserWhiteListRsp(&wlRsp); + return terrno; + } uint64_t* pWhiteLists = taosMemoryMalloc(wlRsp.numWhiteLists * sizeof(uint64_t)); if (pWhiteLists == NULL) { @@ -229,8 +249,14 @@ void taos_fetch_whitelist_a(TAOS *taos, __taos_async_whitelist_fn_t fp, void *pa } SGetUserWhiteListReq req; - memcpy(req.user, pTsc->user, TSDB_USER_LEN); + (void)memcpy(req.user, pTsc->user, TSDB_USER_LEN); int32_t msgLen = tSerializeSGetUserWhiteListReq(NULL, 0, &req); + if (msgLen < 0) { + fp(param, TSDB_CODE_INVALID_PARA, taos, 0, NULL); + releaseTscObj(connId); + return; + } + void* pReq = taosMemoryMalloc(msgLen); if (pReq == NULL) { fp(param, TSDB_CODE_OUT_OF_MEMORY, taos, 0, NULL); @@ -274,7 +300,9 @@ void taos_fetch_whitelist_a(TAOS *taos, __taos_async_whitelist_fn_t fp, void *pa int64_t transportId = 0; SEpSet epSet = getEpSet_s(&pTsc->pAppInfo->mgmtEp); - asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, &transportId, pSendInfo); + if (TSDB_CODE_SUCCESS != asyncSendMsgToServer(pTsc->pAppInfo->pTransporter, &epSet, &transportId, pSendInfo)) { + tscWarn("failed to async send msg to server"); + } releaseTscObj(connId); return; } @@ -287,7 +315,7 @@ void taos_close_internal(void *taos) { STscObj *pTscObj = (STscObj *)taos; tscDebug("0x%" PRIx64 " try to close connection, numOfReq:%d", pTscObj->id, pTscObj->numOfReqs); - taosRemoveRef(clientConnRefPool, pTscObj->id); + (void)taosRemoveRef(clientConnRefPool, pTscObj->id); } void taos_close(TAOS *taos) { @@ -453,6 +481,7 @@ TAOS_ROW taos_fetch_row(TAOS_RES *res) { } else { // assert to avoid un-initialization error tscError("invalid result passed to taos_fetch_row"); + terrno = TSDB_CODE_TSC_INTERNAL_ERROR; return NULL; } } @@ -521,7 +550,7 @@ int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) if(taosAscii2Hex(row[i], charLen, &data, &size) < 0){ break; } - memcpy(str + len, data, size); + (void)memcpy(str + len, data, size); len += size; taosMemoryFree(data); }break; @@ -539,7 +568,7 @@ int taos_print_row(char *str, TAOS_ROW row, TAOS_FIELD *fields, int num_fields) } } - memcpy(str + len, row[i], charLen); + (void)memcpy(str + len, row[i], charLen); len += charLen; } break; @@ -686,7 +715,7 @@ int taos_select_db(TAOS *taos, const char *db) { } char sql[256] = {0}; - snprintf(sql, tListLen(sql), "use %s", db); + (void)snprintf(sql, tListLen(sql), "use %s", db); TAOS_RES *pRequest = taos_query(taos, sql); int32_t code = taos_errno(pRequest); @@ -742,10 +771,10 @@ int taos_fetch_block_s(TAOS_RES *res, int *numOfRows, TAOS_ROW *rows) { if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT || pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) { - return 0; + return pRequest->code; } - doAsyncFetchRows(pRequest, false, true); + (void)doAsyncFetchRows(pRequest, false, true); // TODO refactor SReqResultInfo *pResultInfo = &pRequest->body.resInfo; @@ -795,10 +824,10 @@ int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) { if (pRequest->type == TSDB_SQL_RETRIEVE_EMPTY_RESULT || pRequest->type == TSDB_SQL_INSERT || pRequest->code != TSDB_CODE_SUCCESS || taos_num_fields(res) == 0) { - return 0; + return pRequest->code; } - doAsyncFetchRows(pRequest, false, false); + (void)doAsyncFetchRows(pRequest, false, false); SReqResultInfo *pResultInfo = &pRequest->body.resInfo; @@ -806,7 +835,7 @@ int taos_fetch_raw_block(TAOS_RES *res, int *numOfRows, void **pData) { (*numOfRows) = pResultInfo->numOfRows; (*pData) = (void *)pResultInfo->pData; - return 0; + return pRequest->code; } int *taos_get_column_data_offset(TAOS_RES *res, int columnIndex) { @@ -864,26 +893,24 @@ const char *taos_get_server_info(TAOS *taos) { int taos_get_current_db(TAOS *taos, char *database, int len, int *required) { STscObj *pTscObj = acquireTscObj(*(int64_t *)taos); if (pTscObj == NULL) { - terrno = TSDB_CODE_TSC_DISCONNECTED; - return -1; + return TSDB_CODE_TSC_DISCONNECTED; } int code = TSDB_CODE_SUCCESS; - taosThreadMutexLock(&pTscObj->mutex); + (void)taosThreadMutexLock(&pTscObj->mutex); if (database == NULL || len <= 0) { if (required != NULL) *required = strlen(pTscObj->db) + 1; - terrno = TSDB_CODE_INVALID_PARA; - code = -1; + TSC_ERR_JRET(TSDB_CODE_INVALID_PARA); } else if (len < strlen(pTscObj->db) + 1) { tstrncpy(database, pTscObj->db, len); if (required) *required = strlen(pTscObj->db) + 1; - terrno = TSDB_CODE_INVALID_PARA; - code = -1; + TSC_ERR_JRET(TSDB_CODE_INVALID_PARA); } else { - strcpy(database, pTscObj->db); + (void)strcpy(database, pTscObj->db); code = 0; } - taosThreadMutexUnlock(&pTscObj->mutex); +_return: + (void)taosThreadMutexUnlock(&pTscObj->mutex); releaseTscObj(*(int64_t *)taos); return code; } @@ -924,8 +951,8 @@ static void doAsyncQueryFromAnalyse(SMetaData *pResultMeta, void *param, int32_t pRequest->metric.analyseCostUs += taosGetTimestampUs() - analyseStart; if (pRequest->parseOnly) { - memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta)); - memset(pResultMeta, 0, sizeof(*pResultMeta)); + (void)memcpy(&pRequest->parseMeta, pResultMeta, sizeof(*pResultMeta)); + (void)memset(pResultMeta, 0, sizeof(*pResultMeta)); } handleQueryAnslyseRes(pWrapper, pResultMeta, code); @@ -1013,10 +1040,12 @@ void handleQueryAnslyseRes(SSqlCallbackWrapper *pWrapper, SMetaData *pResultMeta } if (pQuery->haveResultSet) { - setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols); + code = setResSchemaInfo(&pRequest->body.resInfo, pQuery->pResSchema, pQuery->numOfResCols); setResPrecision(&pRequest->body.resInfo, pQuery->precision); } + } + if (code == TSDB_CODE_SUCCESS) { TSWAP(pRequest->dbList, (pQuery)->pDbList); TSWAP(pRequest->tableList, (pQuery)->pTableList); TSWAP(pRequest->targetTableList, (pQuery)->pTargetTableList); @@ -1201,7 +1230,7 @@ int32_t prepareAndParseSqlSyntax(SSqlCallbackWrapper **ppWrapper, SRequestObj *p code = TSDB_CODE_OUT_OF_MEMORY; } else { pWrapper->pCatalogReq->forceUpdate = updateMetaForce; - pWrapper->pCatalogReq->qNodeRequired = qnodeRequired(pRequest); + TSC_ERR_RET(qnodeRequired(pRequest, &pWrapper->pCatalogReq->qNodeRequired)); code = qParseSqlSyntax(pWrapper->pParseCtx, &pRequest->pQuery, pWrapper->pCatalogReq); } @@ -1244,7 +1273,7 @@ void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) { if (NEED_CLIENT_HANDLE_ERROR(code)) { tscDebug("0x%" PRIx64 " client retry to handle the error, code:%d - %s, tryCount:%d, reqId:0x%" PRIx64, pRequest->self, code, tstrerror(code), pRequest->retry, pRequest->requestId); - refreshMeta(pRequest->pTscObj, pRequest); + (void)refreshMeta(pRequest->pTscObj, pRequest); //ignore return code,try again pRequest->prevCode = code; doAsyncQuery(pRequest, true); return; @@ -1259,13 +1288,13 @@ void doAsyncQuery(SRequestObj *pRequest, bool updateMetaForce) { void restartAsyncQuery(SRequestObj *pRequest, int32_t code) { tscInfo("restart request: %s p: %p", pRequest->sqlstr, pRequest); SRequestObj* pUserReq = pRequest; - acquireRequest(pRequest->self); + (void)acquireRequest(pRequest->self); while (pUserReq) { if (pUserReq->self == pUserReq->relation.userRefId || pUserReq->relation.userRefId == 0) { break; } else { int64_t nextRefId = pUserReq->relation.nextRefId; - releaseRequest(pUserReq->self); + (void)releaseRequest(pUserReq->self); if (nextRefId) { pUserReq = acquireRequest(nextRefId); } @@ -1275,16 +1304,16 @@ void restartAsyncQuery(SRequestObj *pRequest, int32_t code) { if (pUserReq) { destroyCtxInRequest(pUserReq); pUserReq->prevCode = code; - memset(&pUserReq->relation, 0, sizeof(pUserReq->relation)); + (void)memset(&pUserReq->relation, 0, sizeof(pUserReq->relation)); } else { tscError("User req is missing"); - removeFromMostPrevReq(pRequest); + (void)removeFromMostPrevReq(pRequest); return; } if (hasSubRequest) - removeFromMostPrevReq(pRequest); + (void)removeFromMostPrevReq(pRequest); else - releaseRequest(pUserReq->self); + (void)releaseRequest(pUserReq->self); doAsyncQuery(pUserReq, true); } @@ -1374,7 +1403,7 @@ int taos_get_db_route_info(TAOS *taos, const char *db, TAOS_DB_ROUTE_INFO *dbInf conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp); char dbFName[TSDB_DB_FNAME_LEN] = {0}; - snprintf(dbFName, sizeof(dbFName), "%d.%s", pTscObj->acctId, db); + (void)snprintf(dbFName, sizeof(dbFName), "%d.%s", pTscObj->acctId, db); code = catalogGetDBVgInfo(pCtg, &conn, dbFName, dbInfo); if (code) { @@ -1424,7 +1453,7 @@ int taos_get_table_vgId(TAOS *taos, const char *db, const char *table, int *vgId conn.mgmtEps = getEpSet_s(&pTscObj->pAppInfo->mgmtEp); SName tableName; - toName(pTscObj->acctId, db, table, &tableName); + (void)toName(pTscObj->acctId, db, table, &tableName); SVgroupInfo vgInfo; code = catalogGetTableHashVgroup(pCtg, &conn, &tableName, &vgInfo); @@ -1545,7 +1574,7 @@ int taos_load_table_info(TAOS *taos, const char *tableNameList) { } SSyncQueryParam *pParam = pRequest->body.interParam; - tsem_wait(&pParam->sem); + (void)tsem_wait(&pParam->sem); _return: destoryCatalogReq(&catalogReq); @@ -1562,7 +1591,9 @@ TAOS_STMT *taos_stmt_init(TAOS *taos) { } TAOS_STMT *pStmt = stmtInit(pObj, 0, NULL); - + if (NULL == pStmt) { + tscError("stmt init failed, errcode:%s", terrstr()); + } releaseTscObj(*(int64_t *)taos); return pStmt; @@ -1577,7 +1608,9 @@ TAOS_STMT *taos_stmt_init_with_reqid(TAOS *taos, int64_t reqid) { } TAOS_STMT *pStmt = stmtInit(pObj, reqid, NULL); - + if (NULL == pStmt) { + tscError("stmt init failed, errcode:%s", terrstr()); + } releaseTscObj(*(int64_t *)taos); return pStmt; @@ -1592,7 +1625,9 @@ TAOS_STMT *taos_stmt_init_with_options(TAOS *taos, TAOS_STMT_OPTIONS *options) { } TAOS_STMT *pStmt = stmtInit(pObj, options->reqId, options); - + if (NULL == pStmt) { + tscError("stmt init failed, errcode:%s", terrstr()); + } releaseTscObj(*(int64_t *)taos); return pStmt; @@ -1707,7 +1742,11 @@ int taos_stmt_bind_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind) { } int32_t insert = 0; - stmtIsInsert(stmt, &insert); + int32_t code = stmtIsInsert(stmt, &insert); + if (TSDB_CODE_SUCCESS != code) { + tscError("stmt insert failed, errcode:%s", tstrerror(code)); + return code; + } if (0 == insert && bind->num > 1) { tscError("only one row data allowed for query"); terrno = TSDB_CODE_INVALID_PARA; @@ -1731,7 +1770,11 @@ int taos_stmt_bind_single_param_batch(TAOS_STMT *stmt, TAOS_MULTI_BIND *bind, in } int32_t insert = 0; - stmtIsInsert(stmt, &insert); + int32_t code = stmtIsInsert(stmt, &insert); + if (TSDB_CODE_SUCCESS != code) { + tscError("stmt insert failed, errcode:%s", tstrerror(code)); + return code; + } if (0 == insert && bind->num > 1) { tscError("only one row data allowed for query"); terrno = TSDB_CODE_INVALID_PARA; diff --git a/source/client/src/clientRawBlockWrite.c b/source/client/src/clientRawBlockWrite.c index 9e1ff6e5e5..e028f76fe3 100644 --- a/source/client/src/clientRawBlockWrite.c +++ b/source/client/src/clientRawBlockWrite.c @@ -1537,8 +1537,8 @@ int taos_write_raw_block_with_fields_with_reqid(TAOS* taos, int rows, char* pDat SQuery* pQuery = NULL; SHashObj* pVgHash = NULL; - SRequestObj* pRequest = (SRequestObj*)createRequest(*(int64_t*)taos, TSDB_SQL_INSERT, reqid); - RAW_NULL_CHECK(pRequest); + SRequestObj* pRequest = NULL; + RAW_RETURN_CHECK(createRequest(*(int64_t*)taos, TSDB_SQL_INSERT, reqid, &pRequest)); uDebug(LOG_ID_TAG " write raw block with field, rows:%d, pData:%p, tbname:%s, fields:%p, numFields:%d", LOG_ID_VALUE, rows, pData, tbname, fields, numFields); @@ -1597,8 +1597,8 @@ int taos_write_raw_block_with_reqid(TAOS* taos, int rows, char* pData, const cha SQuery* pQuery = NULL; SHashObj* pVgHash = NULL; - SRequestObj* pRequest = (SRequestObj*)createRequest(*(int64_t*)taos, TSDB_SQL_INSERT, reqid); - RAW_NULL_CHECK(pRequest); + SRequestObj* pRequest = NULL; + RAW_RETURN_CHECK(createRequest(*(int64_t*)taos, TSDB_SQL_INSERT, reqid, &pRequest)); uDebug(LOG_ID_TAG " write raw block, rows:%d, pData:%p, tbname:%s", LOG_ID_VALUE, rows, pData, tbname); @@ -1667,8 +1667,8 @@ static int32_t tmqWriteRawDataImpl(TAOS* taos, void* data, int32_t dataLen) { SDecoder decoder = {0}; STableMeta* pTableMeta = NULL; - SRequestObj* pRequest = (SRequestObj*)createRequest(*(int64_t*)taos, TSDB_SQL_INSERT, 0); - RAW_NULL_CHECK(pRequest); + SRequestObj* pRequest = NULL; + RAW_RETURN_CHECK(createRequest(*(int64_t*)taos, TSDB_SQL_INSERT, 0, &pRequest)); uDebug(LOG_ID_TAG " write raw data, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen); pRequest->syncQuery = true; @@ -1778,8 +1778,8 @@ static int32_t tmqWriteRawMetaDataImpl(TAOS* taos, void* data, int32_t dataLen) STableMeta* pTableMeta = NULL; SVCreateTbReq* pCreateReqDst = NULL; - SRequestObj* pRequest = (SRequestObj*)createRequest(*(int64_t*)taos, TSDB_SQL_INSERT, 0); - RAW_NULL_CHECK(pRequest); + SRequestObj* pRequest = NULL; + RAW_RETURN_CHECK(createRequest(*(int64_t*)taos, TSDB_SQL_INSERT, 0, &pRequest)); uDebug(LOG_ID_TAG " write raw metadata, data:%p, dataLen:%d", LOG_ID_VALUE, data, dataLen); pRequest->syncQuery = true; diff --git a/source/client/src/clientSml.c b/source/client/src/clientSml.c index 00445b4d12..11c0bd6d6f 100644 --- a/source/client/src/clientSml.c +++ b/source/client/src/clientSml.c @@ -2137,8 +2137,8 @@ TAOS_RES *taos_schemaless_insert_inner(TAOS *taos, char *lines[], char *rawLine, SSmlHandle *info = NULL; int cnt = 0; while (1) { - request = (SRequestObj *)createRequest(*(int64_t *)taos, TSDB_SQL_INSERT, reqid); - if (request == NULL) { + code = createRequest(*(int64_t *)taos, TSDB_SQL_INSERT, reqid, &request); + if (TSDB_CODE_SUCCESS != code) { uError("SML:taos_schemaless_insert error request is null"); return NULL; } diff --git a/source/client/src/clientTmq.c b/source/client/src/clientTmq.c index 650d262870..6d9956122c 100644 --- a/source/client/src/clientTmq.c +++ b/source/client/src/clientTmq.c @@ -1237,8 +1237,9 @@ tmq_t* tmq_consumer_new(tmq_conf_t* conf, char* errstr, int32_t errstrLen) { } // init connection - pTmq->pTscObj = taos_connect_internal(conf->ip, user, pass, NULL, NULL, conf->port, CONN_TYPE__TMQ); - if (pTmq->pTscObj == NULL) { + code = taos_connect_internal(conf->ip, user, pass, NULL, NULL, conf->port, CONN_TYPE__TMQ, &pTmq->pTscObj); + if (code) { + terrno = code; tscError("consumer:0x%" PRIx64 " setup failed since %s, groupId:%s", pTmq->consumerId, terrstr(), pTmq->groupId); (void)tsem2_destroy(&pTmq->rspSem); SET_ERROR_MSG_TMQ("init tscObj failed") diff --git a/source/os/src/osSysinfo.c b/source/os/src/osSysinfo.c index b0d9f076d9..aaaa9e2c2d 100644 --- a/source/os/src/osSysinfo.c +++ b/source/os/src/osSysinfo.c @@ -1009,10 +1009,6 @@ int32_t taosGetSystemUUID(char *uid, int32_t uidlen) { // it's caller's responsibility to make enough space for `uid`, that's 36-char + 1-null uuid_unparse_lower(uuid, buf); int n = snprintf(uid, uidlen, "%.*s", (int)sizeof(buf), buf); // though less performance, much safer - if (n >= uidlen) { - // target buffer is too small - return -1; - } return 0; #else int len = 0; diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 3801ae9ffd..61672478c9 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -156,6 +156,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_TSC_ENCODE_PARAM_ERROR, "Invalid encode param" TAOS_DEFINE_ERROR(TSDB_CODE_TSC_ENCODE_PARAM_NULL, "Not found compress param") TAOS_DEFINE_ERROR(TSDB_CODE_TSC_COMPRESS_PARAM_ERROR, "Invalid compress param") TAOS_DEFINE_ERROR(TSDB_CODE_TSC_COMPRESS_LEVEL_ERROR, "Invalid compress level param") +TAOS_DEFINE_ERROR(TSDB_CODE_TSC_FAIL_GENERATE_JSON, "failed to generate JSON") TAOS_DEFINE_ERROR(TSDB_CODE_TSC_INTERNAL_ERROR, "Internal error")