From ae9a948ba3d6501d1ff2608cb8346559fe5a05e0 Mon Sep 17 00:00:00 2001 From: hjxilinx Date: Wed, 4 Dec 2019 18:35:10 +0800 Subject: [PATCH] [tbase-897] --- src/client/inc/tscUtil.h | 8 -- src/client/inc/tsclient.h | 2 +- src/client/src/tscAsync.c | 2 +- src/client/src/tscSql.c | 13 ++-- src/client/src/tscUtil.c | 159 +++++++------------------------------- src/inc/taoserror.h | 4 +- src/inc/tglobalcfg.h | 2 +- src/inc/tsdb.h | 1 + src/rpc/src/tstring.c | 2 +- src/util/src/tglobalcfg.c | 7 +- src/util/src/tstrbuild.c | 4 +- 11 files changed, 50 insertions(+), 154 deletions(-) diff --git a/src/client/inc/tscUtil.h b/src/client/inc/tscUtil.h index 6666fa24a6..0c485894e6 100644 --- a/src/client/inc/tscUtil.h +++ b/src/client/inc/tscUtil.h @@ -107,14 +107,6 @@ void tscAddSpecialColumnForSelect(SSqlCmd* pCmd, int32_t outputColIndex, int16_t void addRequiredTagColumn(SSqlCmd* pCmd, int32_t tagColIndex, int32_t tableIndex); -//TODO refactor, remove -void SStringFree(SString* str); -void SStringCopy(SString* pDest, const SString* pSrc); -SString SStringCreate(const char* str); - -int32_t SStringAlloc(SString* pStr, int32_t size); -int32_t SStringEnsureRemain(SString* pStr, int32_t size); - int32_t setMeterID(SSqlObj* pSql, SSQLToken* pzTableName, int32_t tableIndex); void tscClearInterpInfo(SSqlCmd* pCmd); diff --git a/src/client/inc/tsclient.h b/src/client/inc/tsclient.h index 6226cf1f1d..d439ba9929 100644 --- a/src/client/inc/tsclient.h +++ b/src/client/inc/tsclient.h @@ -188,7 +188,7 @@ typedef struct SString { typedef struct SCond { uint64_t uid; - SString cond; + char* cond; } SCond; typedef struct SJoinNode { diff --git a/src/client/src/tscAsync.c b/src/client/src/tscAsync.c index d11a279247..abf91e7c43 100644 --- a/src/client/src/tscAsync.c +++ b/src/client/src/tscAsync.c @@ -51,7 +51,7 @@ void taos_query_a(TAOS *taos, const char *sqlstr, void (*fp)(void *, TAOS_RES *, } int32_t sqlLen = strlen(sqlstr); - if (sqlLen > TSDB_MAX_SQL_LEN) { + if (sqlLen > tsMaxSQLStringLen) { tscError("sql string too long"); tscQueueAsyncError(fp, param); return; diff --git a/src/client/src/tscSql.c b/src/client/src/tscSql.c index 233a37dc59..4d7f2734a9 100644 --- a/src/client/src/tscSql.c +++ b/src/client/src/tscSql.c @@ -270,7 +270,7 @@ int taos_query(TAOS *taos, const char *sqlstr) { SSqlRes *pRes = &pSql->res; size_t sqlLen = strlen(sqlstr); - if (sqlLen > TSDB_MAX_SQL_LEN) { + if (sqlLen > tsMaxSQLStringLen) { pRes->code = tscInvalidSQLErrMsg(pSql->cmd.payload, "sql too long", NULL); // set the additional error msg for invalid sql tscError("%p SQL result:%d, %s pObj:%p", pSql, pRes->code, taos_errstr(taos), pObj); @@ -786,7 +786,6 @@ int taos_errno(TAOS *taos) { char *taos_errstr(TAOS *taos) { STscObj *pObj = (STscObj *)taos; uint8_t code; -// char temp[256] = {0}; if (pObj == NULL || pObj->signature != pObj) return tsError[globalCode]; @@ -797,11 +796,13 @@ char *taos_errstr(TAOS *taos) { // for invalid sql, additional information is attached to explain why the sql is invalid if (code == TSDB_CODE_INVALID_SQL) { -// snprintf(temp, tListLen(temp), "invalid SQL: %s", pObj->pSql->cmd.payload); -// strcpy(pObj->pSql->cmd.payload, temp); return pObj->pSql->cmd.payload; } else { - return tsError[code]; + if (code < 0 || code > TSDB_CODE_MAX_ERROR_CODE) { + return tsError[TSDB_CODE_SUCCESS]; + } else { + return tsError[code]; + } } } @@ -924,7 +925,7 @@ int taos_validate_sql(TAOS *taos, const char *sql) { tscTrace("%p Valid SQL: %s pObj:%p", pSql, sql, pObj); int32_t sqlLen = strlen(sql); - if (sqlLen > TSDB_MAX_SQL_LEN) { + if (sqlLen > tsMaxSQLStringLen) { tscError("%p sql too long", pSql); pRes->code = TSDB_CODE_INVALID_SQL; return pRes->code; diff --git a/src/client/src/tscUtil.c b/src/client/src/tscUtil.c index baafb57f6a..7fd3d7706b 100644 --- a/src/client/src/tscUtil.c +++ b/src/client/src/tscUtil.c @@ -51,7 +51,6 @@ void tscGetMetricMetaCacheKey(SSqlCmd* pCmd, char* str, uint64_t uid) { assert(len < tListLen(tagIdBuf)); const int32_t maxKeySize = TSDB_MAX_TAGS_LEN; // allowed max key size - char* tmp = calloc(1, TSDB_MAX_SQL_LEN); SCond* cond = tsGetMetricQueryCondPos(pTagCond, uid); @@ -60,12 +59,24 @@ void tscGetMetricMetaCacheKey(SSqlCmd* pCmd, char* str, uint64_t uid) { sprintf(join, "%s,%s", pTagCond->joinInfo.left.meterId, pTagCond->joinInfo.right.meterId); } - int32_t keyLen = - snprintf(tmp, TSDB_MAX_SQL_LEN, "%s,%s,%s,%d,%s,[%s],%d", pMeterMetaInfo->name, - (cond != NULL ? cond->cond.z : NULL), pTagCond->tbnameCond.cond.n > 0 ? pTagCond->tbnameCond.cond.z : NULL, + // estimate the buffer size + size_t tbnameCondLen = pTagCond->tbnameCond.cond != NULL? strlen(pTagCond->tbnameCond.cond):0; + size_t redundantLen = 20; + + size_t bufSize = strlen(pMeterMetaInfo->name) + tbnameCondLen + strlen(join) + strlen(tagIdBuf); + if (cond != NULL) { + bufSize += strlen(cond->cond); + } + + bufSize = (size_t) ((bufSize + redundantLen) * 1.5); + char* tmp = calloc(1, bufSize); + + int32_t keyLen = snprintf(tmp, bufSize, "%s,%s,%s,%d,%s,[%s],%d", pMeterMetaInfo->name, + (cond != NULL ? cond->cond : NULL), + (tbnameCondLen > 0 ? pTagCond->tbnameCond.cond : NULL), pTagCond->relType, join, tagIdBuf, pCmd->groupbyExpr.orderType); - assert(keyLen <= TSDB_MAX_SQL_LEN); + assert(keyLen <= bufSize); if (keyLen < maxKeySize) { strcpy(str, tmp); @@ -99,7 +110,7 @@ void tsSetMetricQueryCond(STagCond* pTagCond, uint64_t uid, const char* str) { SCond* pDest = &pTagCond->cond[pTagCond->numOfTagCond]; pDest->uid = uid; - pDest->cond = SStringCreate(str); + pDest->cond = strdup(str); pTagCond->numOfTagCond += 1; } @@ -1340,14 +1351,20 @@ bool tscValidateColumnId(SSqlCmd* pCmd, int32_t colId) { void tscTagCondCopy(STagCond* dest, const STagCond* src) { memset(dest, 0, sizeof(STagCond)); + + if (src->tbnameCond.cond != NULL) { + dest->tbnameCond.cond = strdup(src->tbnameCond.cond); + } - SStringCopy(&dest->tbnameCond.cond, &src->tbnameCond.cond); dest->tbnameCond.uid = src->tbnameCond.uid; memcpy(&dest->joinInfo, &src->joinInfo, sizeof(SJoinInfo)); for (int32_t i = 0; i < src->numOfTagCond; ++i) { - SStringCopy(&dest->cond[i].cond, &src->cond[i].cond); + if (src->cond[i].cond != NULL) { + dest->cond[i].cond = strdup(src->cond[i].cond); + } + dest->cond[i].uid = src->cond[i].uid; } @@ -1356,10 +1373,9 @@ void tscTagCondCopy(STagCond* dest, const STagCond* src) { } void tscTagCondRelease(STagCond* pCond) { - SStringFree(&pCond->tbnameCond.cond); - + free(pCond->tbnameCond.cond); for (int32_t i = 0; i < pCond->numOfTagCond; ++i) { - SStringFree(&pCond->cond[i].cond); + free(pCond->cond[i].cond); } memset(pCond, 0, sizeof(STagCond)); @@ -1571,123 +1587,6 @@ void tscResetForNextRetrieve(SSqlRes* pRes) { pRes->numOfRows = 0; } -SString SStringCreate(const char* str) { - size_t len = strlen(str); - - SString dest = {.n = len, .alloc = len + 1}; - dest.z = calloc(1, dest.alloc); - strcpy(dest.z, str); - - return dest; -} - -void SStringCopy(SString* pDest, const SString* pSrc) { - if (pSrc->n > 0) { - pDest->n = pSrc->n; - pDest->alloc = pDest->n + 1; // one additional space for null terminate - - pDest->z = calloc(1, pDest->alloc); - - memcpy(pDest->z, pSrc->z, pDest->n); - } else { - memset(pDest, 0, sizeof(SString)); - } -} - -void SStringFree(SString* pStr) { - if (pStr->alloc > 0) { - tfree(pStr->z); - pStr->alloc = 0; - } -} - -void SStringShrink(SString* pStr) { - if (pStr->alloc > (pStr->n + 1) && pStr->alloc > (pStr->n * 2)) { - pStr->z = realloc(pStr->z, pStr->n + 1); - assert(pStr->z != NULL); - - pStr->alloc = pStr->n + 1; - } -} - -int32_t SStringAlloc(SString* pStr, int32_t size) { - if (pStr->alloc >= size) { - return TSDB_CODE_SUCCESS; - } - - size = ALIGN8(size); - - char* tmp = NULL; - if (pStr->z != NULL) { - tmp = realloc(pStr->z, size); - memset(pStr->z + pStr->n, 0, size - pStr->n); - } else { - tmp = calloc(1, size); - } - - if (tmp == NULL) { -#ifdef WINDOWS - LPVOID lpMsgBuf; - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, - GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR)&lpMsgBuf, 0, NULL); - tscTrace("failed to allocate memory, reason:%s", lpMsgBuf); - LocalFree(lpMsgBuf); -#else - char errmsg[256] = {0}; - strerror_r(errno, errmsg, tListLen(errmsg)); - tscTrace("failed to allocate memory, reason:%s", errmsg); -#endif - return TSDB_CODE_CLI_OUT_OF_MEMORY; - } - - pStr->z = tmp; - pStr->alloc = size; - - return TSDB_CODE_SUCCESS; -} - -#define MIN_ALLOC_SIZE 8 - -int32_t SStringEnsureRemain(SString* pStr, int32_t size) { - if (pStr->alloc - pStr->n > size) { - return TSDB_CODE_SUCCESS; - } - - // remain space is insufficient, allocate more spaces - int32_t inc = (size >= MIN_ALLOC_SIZE) ? size : MIN_ALLOC_SIZE; - if (inc < (pStr->alloc >> 1)) { - inc = (pStr->alloc >> 1); - } - - // get the new size - int32_t newsize = pStr->alloc + inc; - - char* tmp = realloc(pStr->z, newsize); - if (tmp == NULL) { -#ifdef WINDOWS - LPVOID lpMsgBuf; - FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS, NULL, - GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language - (LPTSTR)&lpMsgBuf, 0, NULL); - tscTrace("failed to allocate memory, reason:%s", lpMsgBuf); - LocalFree(lpMsgBuf); -#else - char errmsg[256] = {0}; - strerror_r(errno, errmsg, tListLen(errmsg)); - tscTrace("failed to allocate memory, reason:%s", errmsg); -#endif - - return TSDB_CODE_CLI_OUT_OF_MEMORY; - } - - memset(tmp + pStr->n, 0, inc); - pStr->alloc = newsize; - pStr->z = tmp; - - return TSDB_CODE_SUCCESS; -} - SSqlObj* createSubqueryObj(SSqlObj* pSql, int32_t vnodeIndex, int16_t tableIndex, void (*fp)(), void* param, SSqlObj* pPrevSql) { SSqlCmd* pCmd = &pSql->cmd; @@ -1822,9 +1721,7 @@ void tscDoQuery(SSqlObj* pSql) { } } -int16_t tscGetJoinTagColIndexByUid(SSqlCmd* pCmd, uint64_t uid) { - STagCond* pTagCond = &pCmd->tagCond; - +int16_t tscGetJoinTagColIndexByUid(STagCond* pTagCond, uint64_t uid) { if (pTagCond->joinInfo.left.uid == uid) { return pTagCond->joinInfo.left.tagCol; } else { diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index 5510212dbc..5fee1d7da5 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -128,7 +128,7 @@ extern "C" { #define TSDB_CODE_CACHE_BLOCK_TS_DISORDERED 107 // time stamp in cache block is disordered #define TSDB_CODE_FILE_BLOCK_TS_DISORDERED 108 // time stamp in file block is disordered #define TSDB_CODE_INVALID_COMMIT_LOG 109 // commit log init failed -#define TSDB_CODE_SERVER_NO_SPACE 110 +#define TSDB_CODE_SERV_NO_DISKSPACE 110 #define TSDB_CODE_NOT_SUPER_TABLE 111 // operation only available for super table #define TSDB_CODE_DUPLICATE_TAGS 112 // tags value for join not unique #define TSDB_CODE_INVALID_SUBMIT_MSG 113 @@ -137,6 +137,8 @@ extern "C" { #define TSDB_CODE_INVALID_VNODE_STATUS 116 #define TSDB_CODE_FAILED_TO_LOCK_RESOURCES 117 +#define TSDB_CODE_MAX_ERROR_CODE 118 + #ifdef __cplusplus } #endif diff --git a/src/inc/tglobalcfg.h b/src/inc/tglobalcfg.h index ede3c97ce9..ed91964d61 100644 --- a/src/inc/tglobalcfg.h +++ b/src/inc/tglobalcfg.h @@ -106,7 +106,6 @@ extern int tsMaxDbs; extern int tsMaxTables; extern int tsMaxDnodes; extern int tsMaxVGroups; -extern int tsShellActivityTimer; extern char tsMgmtZone[]; extern char tsLocalIp[]; @@ -127,6 +126,7 @@ extern int tsEnableHttpModule; extern int tsEnableMonitorModule; extern int tsRestRowLimit; extern int tsCompressMsgSize; +extern int tsMaxSQLStringLen; extern char tsSocketType[4]; diff --git a/src/inc/tsdb.h b/src/inc/tsdb.h index ea356a257a..58055fd8c9 100644 --- a/src/inc/tsdb.h +++ b/src/inc/tsdb.h @@ -100,6 +100,7 @@ extern "C" { #define TSDB_COL_NAME_LEN 64 #define TSDB_MAX_SAVED_SQL_LEN TSDB_MAX_COLUMNS * 16 #define TSDB_MAX_SQL_LEN TSDB_PAYLOAD_SIZE +#define TSDB_MAX_ALLOWED_SQL_LEN (8*1024*1024U) // sql length should be less than 6mb #define TSDB_MAX_BYTES_PER_ROW TSDB_MAX_COLUMNS * 16 #define TSDB_MAX_TAGS_LEN 512 diff --git a/src/rpc/src/tstring.c b/src/rpc/src/tstring.c index e3daca130a..63f65b882f 100644 --- a/src/rpc/src/tstring.c +++ b/src/rpc/src/tstring.c @@ -238,7 +238,7 @@ char *tsError[] = {"success", "only super table has metric meta info", "tags value not unique for join", "invalid submit message", - "not active table(not created yet or deleted already)", //114 + "not active table(not created yet or dropped already)", //114 "invalid table id", "invalid vnode status", //116 "failed to lock resources", diff --git a/src/util/src/tglobalcfg.c b/src/util/src/tglobalcfg.c index cef11d30cb..80f76a3d25 100644 --- a/src/util/src/tglobalcfg.c +++ b/src/util/src/tglobalcfg.c @@ -124,6 +124,7 @@ int tsMgmtEqualVnodeNum = 0; int tsEnableHttpModule = 1; int tsEnableMonitorModule = 1; int tsRestRowLimit = 10240; +int tsMaxSQLStringLen = TSDB_MAX_SQL_LEN; /* * denote if the server needs to compress response message at the application layer to client, including query rsp, @@ -653,7 +654,11 @@ static void doInitGlobalConfig() { tsInitConfigOption(cfg++, "compressMsgSize", &tsCompressMsgSize, TSDB_CFG_VTYPE_INT, TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_CLIENT | TSDB_CFG_CTYPE_B_SHOW, -1, 10000000, 0, TSDB_CFG_UTYPE_NONE); - + + tsInitConfigOption(cfg++, "maxSQLLength", &tsMaxSQLStringLen, TSDB_CFG_VTYPE_INT, + TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_CLIENT | TSDB_CFG_CTYPE_B_SHOW, + TSDB_MAX_SQL_LEN, TSDB_MAX_ALLOWED_SQL_LEN, 0, TSDB_CFG_UTYPE_BYTE); + // locale & charset tsInitConfigOption(cfg++, "timezone", tsTimezone, TSDB_CFG_VTYPE_STRING, TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_CLIENT, diff --git a/src/util/src/tstrbuild.c b/src/util/src/tstrbuild.c index 6fb970bd6e..6914357493 100644 --- a/src/util/src/tstrbuild.c +++ b/src/util/src/tstrbuild.c @@ -13,10 +13,8 @@ * along with this program. If not, see . */ +#include "os.h" #include "tstrbuild.h" -#include -#include -#include void taosStringBuilderEnsureCapacity(SStringBuilder* sb, size_t size) { size += sb->pos; -- GitLab