Merge pull request #29038 from taosdata/enh/useSafySysFunc

Replace unsafe memory functions with safe versions
This commit is contained in:
Shengliang Guan 2024-12-11 19:47:47 +08:00 committed by GitHub
commit a1d196688e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
40 changed files with 1107 additions and 526 deletions

View File

@ -25,7 +25,7 @@ typedef int32_t TdUcs4;
#if !defined(DISALLOW_NCHAR_WITHOUT_ICONV) && defined(DARWIN) #if !defined(DISALLOW_NCHAR_WITHOUT_ICONV) && defined(DARWIN)
#include "iconv.h" #include "iconv.h"
#else #else
typedef void *iconv_t; typedef void *iconv_t;
#endif #endif
typedef enum { M2C = 0, C2M } ConvType; typedef enum { M2C = 0, C2M } ConvType;
@ -55,29 +55,34 @@ typedef enum { M2C = 0, C2M } ConvType;
#ifdef strndup #ifdef strndup
#undef strndup #undef strndup
#endif #endif
#define strndup STR_TO_F_FUNC_TAOS_FORBID #define strndup STR_TO_F_FUNC_TAOS_FORBID
#endif #endif
#define tstrncpy(dst, src, size) \ #define tstrncpy(dst, src, size) \
do { \ do { \
(void)strncpy((dst), (src), (size)); \ (void)strncpy((dst), (src), (size)); \
(dst)[(size) - 1] = 0; \ (dst)[(size)-1] = 0; \
} while (0) } while (0)
int64_t tsnprintf(char *dst, int64_t size, const char *format, ...); int64_t tsnprintf(char *dst, int64_t size, const char *format, ...);
#define TAOS_STRCPY(_dst, _src) ((void)strcpy(_dst, _src)) #define TAOS_STRCPY(_dst, _src) ((void)strcpy(_dst, _src))
#define TAOS_STRNCPY(_dst, _src, _size) ((void)strncpy(_dst, _src, _size)) #define TAOS_STRNCPY(_dst, _src, _size) ((void)strncpy(_dst, _src, _size))
#define TAOS_STRCAT(_dst, _src) ((void)strcat(_dst, _src)) #define TAOS_STRCAT(_dst, _src) ((void)strcat(_dst, _src))
#define TAOS_STRNCAT(_dst, _src, len) ((void)strncat(_dst, _src, len)) #define TAOS_STRNCAT(_dst, _src, len) ((void)strncat(_dst, _src, len))
char *tstrdup(const char *src); char *tstrdup(const char *src);
int32_t taosUcs4len(TdUcs4 *ucs4); int32_t taosUcs4len(TdUcs4 *ucs4);
int32_t taosStr2int64(const char *str, int64_t *val); int32_t taosStr2int64(const char *str, int64_t *val);
int32_t taosStr2int16(const char *str, int16_t *val);
int32_t taosStr2int32(const char *str, int32_t *val); int32_t taosStr2int32(const char *str, int32_t *val);
int32_t taosStr2int16(const char *str, int16_t *val);
int32_t taosStr2int8(const char *str, int8_t *val); int32_t taosStr2int8(const char *str, int8_t *val);
int32_t taosStr2Uint64(const char *str, uint64_t *val);
int32_t taosStr2Uint32(const char *str, uint32_t *val);
int32_t taosStr2Uint16(const char *str, uint16_t *val);
int32_t taosStr2Uint8(const char *str, uint8_t *val);
int32_t taosConvInit(void); int32_t taosConvInit(void);
void taosConvDestroy(); void taosConvDestroy();
iconv_t taosAcquireConv(int32_t *idx, ConvType type); iconv_t taosAcquireConv(int32_t *idx, ConvType type);
@ -112,9 +117,9 @@ float taosStr2Float(const char *str, char **pEnd);
int32_t taosHex2Ascii(const char *z, uint32_t n, void **data, uint32_t *size); int32_t taosHex2Ascii(const char *z, uint32_t n, void **data, uint32_t *size);
int32_t taosAscii2Hex(const char *z, uint32_t n, void **data, uint32_t *size); int32_t taosAscii2Hex(const char *z, uint32_t n, void **data, uint32_t *size);
char *taosStrndup(const char *s, int n); char *taosStrndup(const char *s, int n);
//int32_t taosBin2Ascii(const char *z, uint32_t n, void** data, uint32_t* size); // int32_t taosBin2Ascii(const char *z, uint32_t n, void** data, uint32_t* size);
bool isHex(const char* z, uint32_t n); bool isHex(const char *z, uint32_t n);
bool isValidateHex(const char* z, uint32_t n); bool isValidateHex(const char *z, uint32_t n);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -300,7 +300,13 @@ void* doConsumeData(void* param) {
int main(int argc, char** argv) { int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv); testing::InitGoogleTest(&argc, argv);
if (argc > 1) { if (argc > 1) {
numOfThreads = atoi(argv[1]); //numOfThreads = atoi(argv[1]);
int32_t code = taosStr2int32(argv[1], &numOfThreads);
if (code != 0) {
return code;
}
} }
numOfThreads = TMAX(numOfThreads, 1); numOfThreads = TMAX(numOfThreads, 1);

View File

@ -261,7 +261,7 @@ static void responseCompleteCallback(S3Status status, const S3ErrorDetails *erro
for (int i = 0; i < error->extraDetailsCount; i++) { for (int i = 0; i < error->extraDetailsCount; i++) {
if (elen - len > 0) { if (elen - len > 0) {
len += tsnprintf(&(cbd->err_msg[len]), elen - len, " %s: %s\n", error->extraDetails[i].name, len += tsnprintf(&(cbd->err_msg[len]), elen - len, " %s: %s\n", error->extraDetails[i].name,
error->extraDetails[i].value); error->extraDetails[i].value);
} }
} }
} }
@ -742,9 +742,9 @@ upload:
TAOS_CHECK_GOTO(TAOS_SYSTEM_ERROR(EIO), &lino, _exit); TAOS_CHECK_GOTO(TAOS_SYSTEM_ERROR(EIO), &lino, _exit);
} }
n = tsnprintf(buf, sizeof(buf), n = tsnprintf(buf, sizeof(buf),
"<Part><PartNumber>%d</PartNumber>" "<Part><PartNumber>%d</PartNumber>"
"<ETag>%s</ETag></Part>", "<ETag>%s</ETag></Part>",
i + 1, manager.etags[i]); i + 1, manager.etags[i]);
size += growbuffer_append(&(manager.gb), buf, n); size += growbuffer_append(&(manager.gb), buf, n);
} }
size += growbuffer_append(&(manager.gb), "</CompleteMultipartUpload>", strlen("</CompleteMultipartUpload>")); size += growbuffer_append(&(manager.gb), "</CompleteMultipartUpload>", strlen("</CompleteMultipartUpload>"));
@ -908,10 +908,10 @@ upload:
int n; int n;
for (int i = 0; i < cp.part_num; ++i) { for (int i = 0; i < cp.part_num; ++i) {
n = tsnprintf(buf, sizeof(buf), n = tsnprintf(buf, sizeof(buf),
"<Part><PartNumber>%d</PartNumber>" "<Part><PartNumber>%d</PartNumber>"
"<ETag>%s</ETag></Part>", "<ETag>%s</ETag></Part>",
// i + 1, manager.etags[i]); // i + 1, manager.etags[i]);
cp.parts[i].index + 1, cp.parts[i].etag); cp.parts[i].index + 1, cp.parts[i].etag);
size += growbuffer_append(&(manager.gb), buf, n); size += growbuffer_append(&(manager.gb), buf, n);
} }
size += growbuffer_append(&(manager.gb), "</CompleteMultipartUpload>", strlen("</CompleteMultipartUpload>")); size += growbuffer_append(&(manager.gb), "</CompleteMultipartUpload>", strlen("</CompleteMultipartUpload>"));
@ -1916,7 +1916,8 @@ void s3EvictCache(const char *path, long object_size) {
} }
long s3Size(const char *object_name) { long s3Size(const char *object_name) {
long size = 0; int32_t code = 0;
long size = 0;
cos_pool_t *p = NULL; cos_pool_t *p = NULL;
int is_cname = 0; int is_cname = 0;
@ -1941,7 +1942,10 @@ long s3Size(const char *object_name) {
if (cos_status_is_ok(s)) { if (cos_status_is_ok(s)) {
char *content_length_str = (char *)apr_table_get(resp_headers, COS_CONTENT_LENGTH); char *content_length_str = (char *)apr_table_get(resp_headers, COS_CONTENT_LENGTH);
if (content_length_str != NULL) { if (content_length_str != NULL) {
size = atol(content_length_str); code = taosStr2Int64(content_length_str, &size);
if (code != 0) {
cos_warn_log("parse content length failed since %s\n", tstrerror(code));
}
} }
cos_warn_log("head object succeeded: %ld\n", size); cos_warn_log("head object succeeded: %ld\n", size);
} else { } else {

View File

@ -251,7 +251,7 @@ bool checkColumnEncode(char encode[TSDB_CL_COMPRESS_OPTION_LEN]) {
} }
bool checkColumnEncodeOrSetDefault(uint8_t type, char encode[TSDB_CL_COMPRESS_OPTION_LEN]) { bool checkColumnEncodeOrSetDefault(uint8_t type, char encode[TSDB_CL_COMPRESS_OPTION_LEN]) {
if (0 == strlen(encode)) { if (0 == strlen(encode)) {
strncpy(encode, getDefaultEncodeStr(type), TSDB_CL_COMPRESS_OPTION_LEN); tstrncpy(encode, getDefaultEncodeStr(type), TSDB_CL_COMPRESS_OPTION_LEN);
return true; return true;
} }
return checkColumnEncode(encode) && validColEncode(type, columnEncodeVal(encode)); return checkColumnEncode(encode) && validColEncode(type, columnEncodeVal(encode));
@ -268,7 +268,7 @@ bool checkColumnCompress(char compress[TSDB_CL_COMPRESS_OPTION_LEN]) {
} }
bool checkColumnCompressOrSetDefault(uint8_t type, char compress[TSDB_CL_COMPRESS_OPTION_LEN]) { bool checkColumnCompressOrSetDefault(uint8_t type, char compress[TSDB_CL_COMPRESS_OPTION_LEN]) {
if (0 == strlen(compress)) { if (0 == strlen(compress)) {
strncpy(compress, getDefaultCompressStr(type), TSDB_CL_COMPRESS_OPTION_LEN); tstrncpy(compress, getDefaultCompressStr(type), TSDB_CL_COMPRESS_OPTION_LEN);
return true; return true;
} }
@ -290,7 +290,7 @@ bool checkColumnLevel(char level[TSDB_CL_COMPRESS_OPTION_LEN]) {
} }
bool checkColumnLevelOrSetDefault(uint8_t type, char level[TSDB_CL_COMPRESS_OPTION_LEN]) { bool checkColumnLevelOrSetDefault(uint8_t type, char level[TSDB_CL_COMPRESS_OPTION_LEN]) {
if (0 == strlen(level)) { if (0 == strlen(level)) {
strncpy(level, getDefaultLevelStr(type), TSDB_CL_COMPRESS_OPTION_LEN); tstrncpy(level, getDefaultLevelStr(type), TSDB_CL_COMPRESS_OPTION_LEN);
return true; return true;
} }
return checkColumnLevel(level) && validColCompressLevel(type, columnLevelVal(level)); return checkColumnLevel(level) && validColCompressLevel(type, columnLevelVal(level));
@ -314,7 +314,7 @@ void setColLevel(uint32_t* compress, uint8_t level) {
int32_t setColCompressByOption(uint8_t type, uint8_t encode, uint16_t compressType, uint8_t level, bool check, int32_t setColCompressByOption(uint8_t type, uint8_t encode, uint16_t compressType, uint8_t level, bool check,
uint32_t* compress) { uint32_t* compress) {
if(compress == NULL) return TSDB_CODE_TSC_ENCODE_PARAM_ERROR; if (compress == NULL) return TSDB_CODE_TSC_ENCODE_PARAM_ERROR;
if (check && !validColEncode(type, encode)) return TSDB_CODE_TSC_ENCODE_PARAM_ERROR; if (check && !validColEncode(type, encode)) return TSDB_CODE_TSC_ENCODE_PARAM_ERROR;
setColEncode(compress, encode); setColEncode(compress, encode);

View File

@ -22,12 +22,15 @@
int32_t taosGetFqdnPortFromEp(const char* ep, SEp* pEp) { int32_t taosGetFqdnPortFromEp(const char* ep, SEp* pEp) {
pEp->port = 0; pEp->port = 0;
memset(pEp->fqdn, 0, TSDB_FQDN_LEN); memset(pEp->fqdn, 0, TSDB_FQDN_LEN);
strncpy(pEp->fqdn, ep, TSDB_FQDN_LEN - 1); tstrncpy(pEp->fqdn, ep, TSDB_FQDN_LEN);
char* temp = strchr(pEp->fqdn, ':'); char* temp = strchr(pEp->fqdn, ':');
if (temp) { if (temp) {
*temp = 0; *temp = 0;
pEp->port = atoi(temp + 1); pEp->port = taosStr2UInt16(temp + 1, NULL, 10);
if (pEp->port < 0) {
return TSDB_CODE_INVALID_PARA;
}
} }
if (pEp->port == 0) { if (pEp->port == 0) {
@ -282,7 +285,7 @@ int32_t dumpConfToDataBlock(SSDataBlock* pBlock, int32_t startCol) {
locked = 1; locked = 1;
while ((pItem = cfgNextIter(pIter)) != NULL) { while ((pItem = cfgNextIter(pIter)) != NULL) {
_start: _start:
col = startCol; col = startCol;
// GRANT_CFG_SKIP; // GRANT_CFG_SKIP;
@ -297,11 +300,11 @@ _start:
TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, name, false), NULL, _exit); TAOS_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, name, false), NULL, _exit);
char value[TSDB_CONFIG_PATH_LEN + VARSTR_HEADER_SIZE] = {0}; char value[TSDB_CONFIG_PATH_LEN + VARSTR_HEADER_SIZE] = {0};
int32_t valueLen = 0; int32_t valueLen = 0;
SDiskCfg* pDiskCfg = NULL; SDiskCfg* pDiskCfg = NULL;
if (strcasecmp(pItem->name, "dataDir") == 0 && exSize > 0) { if (strcasecmp(pItem->name, "dataDir") == 0 && exSize > 0) {
char* buf = &value[VARSTR_HEADER_SIZE]; char* buf = &value[VARSTR_HEADER_SIZE];
pDiskCfg = taosArrayGet(pItem->array, index); pDiskCfg = taosArrayGet(pItem->array, index);
valueLen = tsnprintf(buf, TSDB_CONFIG_PATH_LEN, "%s", pDiskCfg->dir); valueLen = tsnprintf(buf, TSDB_CONFIG_PATH_LEN, "%s", pDiskCfg->dir);
index++; index++;
@ -334,7 +337,7 @@ _start:
if (strcasecmp(pItem->name, "dataDir") == 0 && pDiskCfg) { if (strcasecmp(pItem->name, "dataDir") == 0 && pDiskCfg) {
char* buf = &info[VARSTR_HEADER_SIZE]; char* buf = &info[VARSTR_HEADER_SIZE];
valueLen = tsnprintf(buf, TSDB_CONFIG_INFO_LEN, "level %d primary %d disabled %" PRIi8, pDiskCfg->level, valueLen = tsnprintf(buf, TSDB_CONFIG_INFO_LEN, "level %d primary %d disabled %" PRIi8, pDiskCfg->level,
pDiskCfg->primary, pDiskCfg->disable); pDiskCfg->primary, pDiskCfg->disable);
} else { } else {
valueLen = 0; valueLen = 0;
} }
@ -351,7 +354,7 @@ _start:
if (index > 0 && index <= exSize) { if (index > 0 && index <= exSize) {
goto _start; goto _start;
} }
} }
pBlock->info.rows = numOfRows; pBlock->info.rows = numOfRows;
_exit: _exit:
if (locked) cfgUnLock(pConf); if (locked) cfgUnLock(pConf);

View File

@ -251,7 +251,7 @@ static int32_t dmParseArgs(int32_t argc, char const *argv[]) {
printf("ERROR: Encrypt key overflow, it should be at most %d characters\n", ENCRYPT_KEY_LEN); printf("ERROR: Encrypt key overflow, it should be at most %d characters\n", ENCRYPT_KEY_LEN);
return TSDB_CODE_INVALID_CFG; return TSDB_CODE_INVALID_CFG;
} }
tstrncpy(global.encryptKey, argv[i], ENCRYPT_KEY_LEN); tstrncpy(global.encryptKey, argv[i], ENCRYPT_KEY_LEN + 1);
} else { } else {
printf("'-y' requires a parameter\n"); printf("'-y' requires a parameter\n");
return TSDB_CODE_INVALID_CFG; return TSDB_CODE_INVALID_CFG;

View File

@ -114,7 +114,7 @@ static void dmMayShouldUpdateAnalFunc(SDnodeMgmt *pMgmt, int64_t newVer) {
.pCont = pHead, .pCont = pHead,
.contLen = contLen, .contLen = contLen,
.msgType = TDMT_MND_RETRIEVE_ANAL_ALGO, .msgType = TDMT_MND_RETRIEVE_ANAL_ALGO,
.info.ahandle = (void *)0x9527, .info.ahandle = 0,
.info.refId = 0, .info.refId = 0,
.info.noResp = 0, .info.noResp = 0,
.info.handle = 0, .info.handle = 0,

View File

@ -186,7 +186,7 @@ static void vmGenerateVnodeCfg(SCreateVnodeReq *pCreate, SVnodeCfg *pCfg) {
#if defined(TD_ENTERPRISE) #if defined(TD_ENTERPRISE)
pCfg->tsdbCfg.encryptAlgorithm = pCreate->encryptAlgorithm; pCfg->tsdbCfg.encryptAlgorithm = pCreate->encryptAlgorithm;
if (pCfg->tsdbCfg.encryptAlgorithm == DND_CA_SM4) { if (pCfg->tsdbCfg.encryptAlgorithm == DND_CA_SM4) {
strncpy(pCfg->tsdbCfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN); tstrncpy(pCfg->tsdbCfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
} }
#else #else
pCfg->tsdbCfg.encryptAlgorithm = 0; pCfg->tsdbCfg.encryptAlgorithm = 0;
@ -202,7 +202,7 @@ static void vmGenerateVnodeCfg(SCreateVnodeReq *pCreate, SVnodeCfg *pCfg) {
#if defined(TD_ENTERPRISE) #if defined(TD_ENTERPRISE)
pCfg->walCfg.encryptAlgorithm = pCreate->encryptAlgorithm; pCfg->walCfg.encryptAlgorithm = pCreate->encryptAlgorithm;
if (pCfg->walCfg.encryptAlgorithm == DND_CA_SM4) { if (pCfg->walCfg.encryptAlgorithm == DND_CA_SM4) {
strncpy(pCfg->walCfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN); tstrncpy(pCfg->walCfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
} }
#else #else
pCfg->walCfg.encryptAlgorithm = 0; pCfg->walCfg.encryptAlgorithm = 0;
@ -898,7 +898,7 @@ int32_t vmProcessArbHeartBeatReq(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) {
size_t size = taosArrayGetSize(arbHbReq.hbMembers); size_t size = taosArrayGetSize(arbHbReq.hbMembers);
arbHbRsp.dnodeId = pMgmt->pData->dnodeId; arbHbRsp.dnodeId = pMgmt->pData->dnodeId;
strncpy(arbHbRsp.arbToken, arbHbReq.arbToken, TSDB_ARB_TOKEN_SIZE); tstrncpy(arbHbRsp.arbToken, arbHbReq.arbToken, TSDB_ARB_TOKEN_SIZE);
arbHbRsp.hbMembers = taosArrayInit(size, sizeof(SVArbHbRspMember)); arbHbRsp.hbMembers = taosArrayInit(size, sizeof(SVArbHbRspMember));
if (arbHbRsp.hbMembers == NULL) { if (arbHbRsp.hbMembers == NULL) {
goto _OVER; goto _OVER;

View File

@ -179,7 +179,7 @@ int32_t dmInitVars(SDnode *pDnode) {
//code = taosGetCryptKey(tsAuthCode, pData->machineId, tsCryptKey); //code = taosGetCryptKey(tsAuthCode, pData->machineId, tsCryptKey);
code = 0; code = 0;
strncpy(tsEncryptKey, tsAuthCode, 16); tstrncpy(tsEncryptKey, tsAuthCode, 16);
if (code != 0) { if (code != 0) {
if(code == -1){ if(code == -1){
@ -220,6 +220,7 @@ int32_t dmInitVars(SDnode *pDnode) {
} }
extern SMonVloadInfo tsVinfo; extern SMonVloadInfo tsVinfo;
void dmClearVars(SDnode *pDnode) { void dmClearVars(SDnode *pDnode) {
for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) { for (EDndNodeType ntype = DNODE; ntype < NODE_END; ++ntype) {
SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype]; SMgmtWrapper *pWrapper = &pDnode->wrappers[ntype];

View File

@ -230,7 +230,7 @@ static int32_t dmWriteCheckCodeFile(char *file, char *realfile, char *key, bool
} }
SCryptOpts opts; SCryptOpts opts;
strncpy(opts.key, key, ENCRYPT_KEY_LEN); tstrncpy(opts.key, key, ENCRYPT_KEY_LEN + 1);
opts.len = len; opts.len = len;
opts.source = DM_KEY_INDICATOR; opts.source = DM_KEY_INDICATOR;
opts.result = result; opts.result = result;
@ -349,7 +349,7 @@ static int32_t dmCompareEncryptKey(char *file, char *key, bool toLogFile) {
} }
SCryptOpts opts = {0}; SCryptOpts opts = {0};
strncpy(opts.key, key, ENCRYPT_KEY_LEN); tstrncpy(opts.key, key, ENCRYPT_KEY_LEN + 1);
opts.len = len; opts.len = len;
opts.source = content; opts.source = content;
opts.result = result; opts.result = result;
@ -551,7 +551,7 @@ int32_t dmGetEncryptKey() {
goto _OVER; goto _OVER;
} }
strncpy(tsEncryptKey, encryptKey, ENCRYPT_KEY_LEN); strncpy(tsEncryptKey, encryptKey, ENCRYPT_KEY_LEN + 1);
taosMemoryFreeClear(encryptKey); taosMemoryFreeClear(encryptKey);
tsEncryptionKeyChksum = taosCalcChecksum(0, tsEncryptKey, strlen(tsEncryptKey)); tsEncryptionKeyChksum = taosCalcChecksum(0, tsEncryptKey, strlen(tsEncryptKey));
tsEncryptionKeyStat = ENCRYPT_KEY_STAT_LOADED; tsEncryptionKeyStat = ENCRYPT_KEY_STAT_LOADED;

View File

@ -1315,7 +1315,7 @@ static int32_t mndRetrieveArbGroups(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock
continue; continue;
} }
char dbNameInGroup[TSDB_DB_FNAME_LEN]; char dbNameInGroup[TSDB_DB_FNAME_LEN];
strncpy(dbNameInGroup, pVgObj->dbName, TSDB_DB_FNAME_LEN); tstrncpy(dbNameInGroup, pVgObj->dbName, TSDB_DB_FNAME_LEN);
sdbRelease(pSdb, pVgObj); sdbRelease(pSdb, pVgObj);
char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0}; char dbname[TSDB_DB_NAME_LEN + VARSTR_HEADER_SIZE] = {0};

View File

@ -244,7 +244,7 @@ int32_t mndCompactGetDbName(SMnode *pMnode, int32_t compactId, char *dbname, int
TAOS_RETURN(code); TAOS_RETURN(code);
} }
(void)strncpy(dbname, pCompact->dbname, len); tstrncpy(dbname, pCompact->dbname, len);
mndReleaseCompact(pMnode, pCompact); mndReleaseCompact(pMnode, pCompact);
TAOS_RETURN(code); TAOS_RETURN(code);
} }
@ -321,7 +321,7 @@ int32_t mndRetrieveCompact(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock,
TAOS_CHECK_GOTO(tNameFromString(&name, pCompact->dbname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER); TAOS_CHECK_GOTO(tNameFromString(&name, pCompact->dbname, T_NAME_ACCT | T_NAME_DB), &lino, _OVER);
(void)tNameGetDbName(&name, varDataVal(tmpBuf)); (void)tNameGetDbName(&name, varDataVal(tmpBuf));
} else { } else {
(void)strncpy(varDataVal(tmpBuf), pCompact->dbname, TSDB_SHOW_SQL_LEN); tstrncpy(varDataVal(tmpBuf), pCompact->dbname, TSDB_SHOW_SQL_LEN);
} }
varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf))); varDataSetLen(tmpBuf, strlen(varDataVal(tmpBuf)));
RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)tmpBuf, false), pCompact, &lino, _OVER); RETRIEVE_CHECK_GOTO(colDataSetVal(pColInfo, numOfRows, (const char *)tmpBuf, false), pCompact, &lino, _OVER);
@ -516,11 +516,14 @@ int32_t mndProcessKillCompactReq(SRpcMsg *pReq) {
code = TSDB_CODE_ACTION_IN_PROGRESS; code = TSDB_CODE_ACTION_IN_PROGRESS;
char obj[TSDB_INT32_ID_LEN] = {0}; char obj[TSDB_INT32_ID_LEN] = {0};
(void)sprintf(obj, "%d", pCompact->compactId); int32_t nBytes = snprintf(obj, sizeof(obj), "%d", pCompact->compactId);
if ((uint32_t)nBytes < sizeof(obj)) {
auditRecord(pReq, pMnode->clusterId, "killCompact", pCompact->dbname, obj, killCompactReq.sql, killCompactReq.sqlLen); auditRecord(pReq, pMnode->clusterId, "killCompact", pCompact->dbname, obj, killCompactReq.sql,
killCompactReq.sqlLen);
} else {
mError("compact:%" PRId32 " failed to audit since %s", pCompact->compactId, tstrerror(TSDB_CODE_OUT_OF_RANGE));
}
_OVER: _OVER:
if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) { if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {
mError("failed to kill compact %" PRId32 " since %s", killCompactReq.compactId, terrstr()); mError("failed to kill compact %" PRId32 " since %s", killCompactReq.compactId, terrstr());
@ -641,7 +644,7 @@ void mndCompactSendProgressReq(SMnode *pMnode, SCompactObj *pCompact) {
char detail[1024] = {0}; char detail[1024] = {0};
int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d", int32_t len = tsnprintf(detail, sizeof(detail), "msgType:%s numOfEps:%d inUse:%d",
TMSG_INFO(TDMT_VND_QUERY_COMPACT_PROGRESS), epSet.numOfEps, epSet.inUse); TMSG_INFO(TDMT_VND_QUERY_COMPACT_PROGRESS), epSet.numOfEps, epSet.inUse);
for (int32_t i = 0; i < epSet.numOfEps; ++i) { for (int32_t i = 0; i < epSet.numOfEps; ++i) {
len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port); len += tsnprintf(detail + len, sizeof(detail) - len, " ep:%d-%s:%u", i, epSet.eps[i].fqdn, epSet.eps[i].port);
} }

View File

@ -16,6 +16,7 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "mndDb.h" #include "mndDb.h"
#include "audit.h" #include "audit.h"
#include "command.h"
#include "mndArbGroup.h" #include "mndArbGroup.h"
#include "mndCluster.h" #include "mndCluster.h"
#include "mndDnode.h" #include "mndDnode.h"
@ -34,7 +35,6 @@
#include "systable.h" #include "systable.h"
#include "thttp.h" #include "thttp.h"
#include "tjson.h" #include "tjson.h"
#include "command.h"
#define DB_VER_NUMBER 1 #define DB_VER_NUMBER 1
#define DB_RESERVE_SIZE 27 #define DB_RESERVE_SIZE 27
@ -416,7 +416,12 @@ static int32_t mndCheckDbName(const char *dbName, SUserObj *pUser) {
return TSDB_CODE_MND_INVALID_DB; return TSDB_CODE_MND_INVALID_DB;
} }
int32_t acctId = atoi(dbName); int32_t acctId;
int32_t code = taosStr2int32(dbName, &acctId);
if (code != 0) {
return code;
}
if (acctId != pUser->acctId) { if (acctId != pUser->acctId) {
return TSDB_CODE_MND_INVALID_DB_ACCT; return TSDB_CODE_MND_INVALID_DB_ACCT;
} }
@ -1560,8 +1565,8 @@ static int32_t mndBuildDropVgroupAction(SMnode *pMnode, STrans *pTrans, SDbObj *
static int32_t mndSetDropDbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) { static int32_t mndSetDropDbRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
int32_t code = 0; int32_t code = 0;
SSdb *pSdb = pMnode->pSdb; SSdb *pSdb = pMnode->pSdb;
void *pIter = NULL; void *pIter = NULL;
while (1) { while (1) {
SVgObj *pVgroup = NULL; SVgObj *pVgroup = NULL;
@ -1941,9 +1946,9 @@ int32_t mndValidateDbInfo(SMnode *pMnode, SDbCacheInfo *pDbs, int32_t numOfDbs,
continue; continue;
} else { } else {
mTrace("db:%s, valid dbinfo, vgVersion:%d cfgVersion:%d stateTs:%" PRId64 mTrace("db:%s, valid dbinfo, vgVersion:%d cfgVersion:%d stateTs:%" PRId64
" numOfTables:%d, changed to vgVersion:%d cfgVersion:%d stateTs:%" PRId64 " numOfTables:%d", " numOfTables:%d, changed to vgVersion:%d cfgVersion:%d stateTs:%" PRId64 " numOfTables:%d",
pDbCacheInfo->dbFName, pDbCacheInfo->vgVersion, pDbCacheInfo->cfgVersion, pDbCacheInfo->stateTs, pDbCacheInfo->dbFName, pDbCacheInfo->vgVersion, pDbCacheInfo->cfgVersion, pDbCacheInfo->stateTs,
pDbCacheInfo->numOfTable, pDb->vgVersion, pDb->cfgVersion, pDb->stateTs, numOfTable); pDbCacheInfo->numOfTable, pDb->vgVersion, pDb->cfgVersion, pDb->stateTs, numOfTable);
} }
if (pDbCacheInfo->cfgVersion < pDb->cfgVersion) { if (pDbCacheInfo->cfgVersion < pDb->cfgVersion) {
@ -1955,7 +1960,7 @@ int32_t mndValidateDbInfo(SMnode *pMnode, SDbCacheInfo *pDbs, int32_t numOfDbs,
rsp.pTsmaRsp = taosMemoryCalloc(1, sizeof(STableTSMAInfoRsp)); rsp.pTsmaRsp = taosMemoryCalloc(1, sizeof(STableTSMAInfoRsp));
if (rsp.pTsmaRsp) rsp.pTsmaRsp->pTsmas = taosArrayInit(4, POINTER_BYTES); if (rsp.pTsmaRsp) rsp.pTsmaRsp->pTsmas = taosArrayInit(4, POINTER_BYTES);
if (rsp.pTsmaRsp && rsp.pTsmaRsp->pTsmas) { if (rsp.pTsmaRsp && rsp.pTsmaRsp->pTsmas) {
bool exist = false; bool exist = false;
int32_t code = mndGetDbTsmas(pMnode, 0, pDb->uid, rsp.pTsmaRsp, &exist); int32_t code = mndGetDbTsmas(pMnode, 0, pDb->uid, rsp.pTsmaRsp, &exist);
if (TSDB_CODE_SUCCESS != code) { if (TSDB_CODE_SUCCESS != code) {
mndReleaseDb(pMnode, pDb); mndReleaseDb(pMnode, pDb);
@ -2386,7 +2391,8 @@ static void mndDumpDbInfoData(SMnode *pMnode, SSDataBlock *pBlock, SDbObj *pDb,
TAOS_CHECK_GOTO(colDataSetVal(pColInfo, rows, (const char *)strictVstr, false), &lino, _OVER); TAOS_CHECK_GOTO(colDataSetVal(pColInfo, rows, (const char *)strictVstr, false), &lino, _OVER);
char durationVstr[128] = {0}; char durationVstr[128] = {0};
int32_t len = formatDurationOrKeep(&durationVstr[VARSTR_HEADER_SIZE], sizeof(durationVstr) - VARSTR_HEADER_SIZE, pDb->cfg.daysPerFile); int32_t len = formatDurationOrKeep(&durationVstr[VARSTR_HEADER_SIZE], sizeof(durationVstr) - VARSTR_HEADER_SIZE,
pDb->cfg.daysPerFile);
varDataSetLen(durationVstr, len); varDataSetLen(durationVstr, len);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
@ -2402,9 +2408,9 @@ static void mndDumpDbInfoData(SMnode *pMnode, SSDataBlock *pBlock, SDbObj *pDb,
int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pDb->cfg.daysToKeep2); int32_t lenKeep2 = formatDurationOrKeep(keep2Str, sizeof(keep2Str), pDb->cfg.daysToKeep2);
if (pDb->cfg.daysToKeep0 > pDb->cfg.daysToKeep1 || pDb->cfg.daysToKeep0 > pDb->cfg.daysToKeep2) { if (pDb->cfg.daysToKeep0 > pDb->cfg.daysToKeep1 || pDb->cfg.daysToKeep0 > pDb->cfg.daysToKeep2) {
len = sprintf(&keepVstr[VARSTR_HEADER_SIZE], "%s,%s,%s", keep1Str, keep2Str, keep0Str); len = sprintf(&keepVstr[VARSTR_HEADER_SIZE], "%s,%s,%s", keep1Str, keep2Str, keep0Str);
} else { } else {
len = sprintf(&keepVstr[VARSTR_HEADER_SIZE], "%s,%s,%s", keep0Str, keep1Str, keep2Str); len = sprintf(&keepVstr[VARSTR_HEADER_SIZE], "%s,%s,%s", keep0Str, keep1Str, keep2Str);
} }
varDataSetLen(keepVstr, len); varDataSetLen(keepVstr, len);
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);

View File

@ -462,7 +462,7 @@ int32_t mndGetDnodeData(SMnode *pMnode, SArray *pDnodeInfo) {
dInfo.isMnode = 0; dInfo.isMnode = 0;
} }
if(taosArrayPush(pDnodeInfo, &dInfo) == NULL){ if (taosArrayPush(pDnodeInfo, &dInfo) == NULL) {
code = terrno; code = terrno;
sdbCancelFetch(pSdb, pIter); sdbCancelFetch(pSdb, pIter);
break; break;
@ -471,12 +471,12 @@ int32_t mndGetDnodeData(SMnode *pMnode, SArray *pDnodeInfo) {
TAOS_RETURN(code); TAOS_RETURN(code);
} }
#define CHECK_MONITOR_PARA(para,err) \ #define CHECK_MONITOR_PARA(para, err) \
if (pCfg->monitorParas.para != para) { \ if (pCfg->monitorParas.para != para) { \
mError("dnode:%d, para:%d inconsistent with cluster:%d", pDnode->id, pCfg->monitorParas.para, para); \ mError("dnode:%d, para:%d inconsistent with cluster:%d", pDnode->id, pCfg->monitorParas.para, para); \
terrno = err; \ terrno = err; \
return err;\ return err; \
} }
static int32_t mndCheckClusterCfgPara(SMnode *pMnode, SDnodeObj *pDnode, const SClusterCfg *pCfg) { static int32_t mndCheckClusterCfgPara(SMnode *pMnode, SDnodeObj *pDnode, const SClusterCfg *pCfg) {
CHECK_MONITOR_PARA(tsEnableMonitor, DND_REASON_STATUS_MONITOR_SWITCH_NOT_MATCH); CHECK_MONITOR_PARA(tsEnableMonitor, DND_REASON_STATUS_MONITOR_SWITCH_NOT_MATCH);
@ -487,7 +487,8 @@ static int32_t mndCheckClusterCfgPara(SMnode *pMnode, SDnodeObj *pDnode, const S
CHECK_MONITOR_PARA(tsSlowLogScope, DND_REASON_STATUS_MONITOR_SLOW_LOG_SCOPE_NOT_MATCH); CHECK_MONITOR_PARA(tsSlowLogScope, DND_REASON_STATUS_MONITOR_SLOW_LOG_SCOPE_NOT_MATCH);
if (0 != strcasecmp(pCfg->monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb)) { if (0 != strcasecmp(pCfg->monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb)) {
mError("dnode:%d, tsSlowLogExceptDb:%s inconsistent with cluster:%s", pDnode->id, pCfg->monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb); mError("dnode:%d, tsSlowLogExceptDb:%s inconsistent with cluster:%s", pDnode->id,
pCfg->monitorParas.tsSlowLogExceptDb, tsSlowLogExceptDb);
terrno = TSDB_CODE_DNODE_INVALID_MONITOR_PARAS; terrno = TSDB_CODE_DNODE_INVALID_MONITOR_PARAS;
return DND_REASON_STATUS_MONITOR_NOT_MATCH; return DND_REASON_STATUS_MONITOR_NOT_MATCH;
} }
@ -583,8 +584,8 @@ static bool mndUpdateMnodeState(SMnodeObj *pObj, SMnodeLoad *pMload) {
return stateChanged; return stateChanged;
} }
extern char* tsMonFwUri; extern char *tsMonFwUri;
extern char* tsMonSlowLogUri; extern char *tsMonSlowLogUri;
static int32_t mndProcessStatisReq(SRpcMsg *pReq) { static int32_t mndProcessStatisReq(SRpcMsg *pReq) {
SMnode *pMnode = pReq->info.node; SMnode *pMnode = pReq->info.node;
SStatisReq statisReq = {0}; SStatisReq statisReq = {0};
@ -596,9 +597,9 @@ static int32_t mndProcessStatisReq(SRpcMsg *pReq) {
mInfo("process statis req,\n %s", statisReq.pCont); mInfo("process statis req,\n %s", statisReq.pCont);
} }
if (statisReq.type == MONITOR_TYPE_COUNTER){ if (statisReq.type == MONITOR_TYPE_COUNTER) {
monSendContent(statisReq.pCont, tsMonFwUri); monSendContent(statisReq.pCont, tsMonFwUri);
}else if(statisReq.type == MONITOR_TYPE_SLOW_LOG){ } else if (statisReq.type == MONITOR_TYPE_SLOW_LOG) {
monSendContent(statisReq.pCont, tsMonSlowLogUri); monSendContent(statisReq.pCont, tsMonSlowLogUri);
} }
@ -1058,27 +1059,27 @@ _OVER:
TAOS_RETURN(code); TAOS_RETURN(code);
} }
static void getSlowLogScopeString(int32_t scope, char* result){ static void getSlowLogScopeString(int32_t scope, char *result) {
if(scope == SLOW_LOG_TYPE_NULL) { if (scope == SLOW_LOG_TYPE_NULL) {
(void)strcat(result, "NONE"); (void)strcat(result, "NONE");
return; return;
} }
while(scope > 0){ while (scope > 0) {
if(scope & SLOW_LOG_TYPE_QUERY) { if (scope & SLOW_LOG_TYPE_QUERY) {
(void)strcat(result, "QUERY"); (void)strcat(result, "QUERY");
scope &= ~SLOW_LOG_TYPE_QUERY; scope &= ~SLOW_LOG_TYPE_QUERY;
} else if(scope & SLOW_LOG_TYPE_INSERT) { } else if (scope & SLOW_LOG_TYPE_INSERT) {
(void)strcat(result, "INSERT"); (void)strcat(result, "INSERT");
scope &= ~SLOW_LOG_TYPE_INSERT; scope &= ~SLOW_LOG_TYPE_INSERT;
} else if(scope & SLOW_LOG_TYPE_OTHERS) { } else if (scope & SLOW_LOG_TYPE_OTHERS) {
(void)strcat(result, "OTHERS"); (void)strcat(result, "OTHERS");
scope &= ~SLOW_LOG_TYPE_OTHERS; scope &= ~SLOW_LOG_TYPE_OTHERS;
} else{ } else {
(void)printf("invalid slow log scope:%d", scope); (void)printf("invalid slow log scope:%d", scope);
return; return;
} }
if(scope > 0) { if (scope > 0) {
(void)strcat(result, "|"); (void)strcat(result, "|");
} }
} }
@ -1440,7 +1441,7 @@ _OVER:
static int32_t mndMCfg2DCfg(SMCfgDnodeReq *pMCfgReq, SDCfgDnodeReq *pDCfgReq) { static int32_t mndMCfg2DCfg(SMCfgDnodeReq *pMCfgReq, SDCfgDnodeReq *pDCfgReq) {
int32_t code = 0; int32_t code = 0;
char *p = pMCfgReq->config; char *p = pMCfgReq->config;
while (*p) { while (*p) {
if (*p == ' ') { if (*p == ' ') {
break; break;
@ -1449,7 +1450,7 @@ static int32_t mndMCfg2DCfg(SMCfgDnodeReq *pMCfgReq, SDCfgDnodeReq *pDCfgReq) {
} }
size_t optLen = p - pMCfgReq->config; size_t optLen = p - pMCfgReq->config;
(void)strncpy(pDCfgReq->config, pMCfgReq->config, optLen); tstrncpy(pDCfgReq->config, pMCfgReq->config, optLen + 1);
pDCfgReq->config[optLen] = 0; pDCfgReq->config[optLen] = 0;
if (' ' == pMCfgReq->config[optLen]) { if (' ' == pMCfgReq->config[optLen]) {
@ -1538,7 +1539,7 @@ static int32_t mndProcessConfigDnodeReq(SRpcMsg *pReq) {
snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%d", flag); snprintf(dcfgReq.value, TSDB_DNODE_VALUE_LEN, "%d", flag);
#endif #endif
} else { } else {
TAOS_CHECK_GOTO (mndMCfg2DCfg(&cfgReq, &dcfgReq), NULL, _err_out); TAOS_CHECK_GOTO(mndMCfg2DCfg(&cfgReq, &dcfgReq), NULL, _err_out);
if (strlen(dcfgReq.config) > TSDB_DNODE_CONFIG_LEN) { if (strlen(dcfgReq.config) > TSDB_DNODE_CONFIG_LEN) {
mError("dnode:%d, failed to config since config is too long", cfgReq.dnodeId); mError("dnode:%d, failed to config since config is too long", cfgReq.dnodeId);
code = TSDB_CODE_INVALID_CFG; code = TSDB_CODE_INVALID_CFG;
@ -1881,11 +1882,19 @@ static int32_t mndMCfgGetValInt32(SMCfgDnodeReq *pMCfgReq, int32_t optLen, int32
if (' ' == pMCfgReq->config[optLen]) { if (' ' == pMCfgReq->config[optLen]) {
// 'key value' // 'key value'
if (strlen(pMCfgReq->value) != 0) goto _err; if (strlen(pMCfgReq->value) != 0) goto _err;
*pOutValue = atoi(pMCfgReq->config + optLen + 1); code = taosStr2int32(pMCfgReq->config + optLen + 1, pOutValue);
if (code != 0) {
mError("dnode:%d, failed to get cfg since %s", pMCfgReq->dnodeId, tstrerror(code));
goto _err;
}
} else { } else {
// 'key' 'value' // 'key' 'value'
if (strlen(pMCfgReq->value) == 0) goto _err; if (strlen(pMCfgReq->value) == 0) goto _err;
*pOutValue = atoi(pMCfgReq->value); code = taosStr2int32(pMCfgReq->value, pOutValue);
if (code != 0) {
mError("dnode:%d, failed to get cfg since %s", pMCfgReq->dnodeId, tstrerror(code));
goto _err;
}
} }
TAOS_RETURN(code); TAOS_RETURN(code);

View File

@ -15,8 +15,8 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "mndAcct.h" #include "mndAcct.h"
#include "mndArbGroup.h"
#include "mndAnode.h" #include "mndAnode.h"
#include "mndArbGroup.h"
#include "mndCluster.h" #include "mndCluster.h"
#include "mndCompact.h" #include "mndCompact.h"
#include "mndCompactDetail.h" #include "mndCompactDetail.h"
@ -254,7 +254,7 @@ static void mndIncreaseUpTime(SMnode *pMnode) {
.pCont = pReq, .pCont = pReq,
.contLen = contLen, .contLen = contLen,
.info.notFreeAhandle = 1, .info.notFreeAhandle = 1,
.info.ahandle = (void *)0x9527}; .info.ahandle = 0};
// TODO check return value // TODO check return value
if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) { if (tmsgPutToQueue(&pMnode->msgCb, WRITE_QUEUE, &rpcMsg) < 0) {
mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__); mError("failed to put into write-queue since %s, line:%d", terrstr(), __LINE__);
@ -530,7 +530,7 @@ static int32_t mndInitWal(SMnode *pMnode) {
code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY; code = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
TAOS_RETURN(code); TAOS_RETURN(code);
} else { } else {
(void)strncpy(cfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN); tstrncpy(cfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
} }
} }
#endif #endif

View File

@ -14,10 +14,10 @@
*/ */
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "mndMnode.h"
#include "audit.h" #include "audit.h"
#include "mndCluster.h" #include "mndCluster.h"
#include "mndDnode.h" #include "mndDnode.h"
#include "mndMnode.h"
#include "mndPrivilege.h" #include "mndPrivilege.h"
#include "mndShow.h" #include "mndShow.h"
#include "mndSync.h" #include "mndSync.h"
@ -722,10 +722,13 @@ static int32_t mndProcessCreateMnodeReq(SRpcMsg *pReq) {
code = mndCreateMnode(pMnode, pReq, pDnode, &createReq); code = mndCreateMnode(pMnode, pReq, pDnode, &createReq);
if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS; if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS;
char obj[40] = {0}; char obj[40] = {0};
sprintf(obj, "%d", createReq.dnodeId); int32_t bytes = snprintf(obj, sizeof(obj), "%d", createReq.dnodeId);
if ((uint32_t)bytes < sizeof(obj)) {
auditRecord(pReq, pMnode->clusterId, "createMnode", "", obj, createReq.sql, createReq.sqlLen); auditRecord(pReq, pMnode->clusterId, "createMnode", "", obj, createReq.sql, createReq.sqlLen);
} else {
mError("mnode:%d, failed to audit create req since %s", createReq.dnodeId, tstrerror(TSDB_CODE_OUT_OF_RANGE));
}
_OVER: _OVER:
if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) { if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) {

View File

@ -14,12 +14,12 @@
*/ */
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "mndProfile.h"
#include "audit.h" #include "audit.h"
#include "mndDb.h" #include "mndDb.h"
#include "mndDnode.h" #include "mndDnode.h"
#include "mndMnode.h" #include "mndMnode.h"
#include "mndPrivilege.h" #include "mndPrivilege.h"
#include "mndProfile.h"
#include "mndQnode.h" #include "mndQnode.h"
#include "mndShow.h" #include "mndShow.h"
#include "mndSma.h" #include "mndSma.h"
@ -336,10 +336,13 @@ static int32_t mndProcessConnectReq(SRpcMsg *pReq) {
code = 0; code = 0;
char detail[1000] = {0}; char detail[1000] = {0};
(void)sprintf(detail, "app:%s", connReq.app); int32_t nBytes = snprintf(detail, sizeof(detail), "app:%s", connReq.app);
if ((uint32_t)nBytes < sizeof(detail)) {
auditRecord(pReq, pMnode->clusterId, "login", "", "", detail, strlen(detail)); auditRecord(pReq, pMnode->clusterId, "login", "", "", detail, strlen(detail));
} else {
mError("failed to audit logic since %s", tstrerror(TSDB_CODE_OUT_OF_RANGE));
}
_OVER: _OVER:

View File

@ -15,6 +15,7 @@
#define _DEFAULT_SOURCE #define _DEFAULT_SOURCE
#include "mndSma.h" #include "mndSma.h"
#include "functionMgt.h"
#include "mndDb.h" #include "mndDb.h"
#include "mndDnode.h" #include "mndDnode.h"
#include "mndIndex.h" #include "mndIndex.h"
@ -31,7 +32,6 @@
#include "mndVgroup.h" #include "mndVgroup.h"
#include "parser.h" #include "parser.h"
#include "tname.h" #include "tname.h"
#include "functionMgt.h"
#define TSDB_SMA_VER_NUMBER 1 #define TSDB_SMA_VER_NUMBER 1
#define TSDB_SMA_RESERVE_SIZE 64 #define TSDB_SMA_RESERVE_SIZE 64
@ -48,8 +48,8 @@ static int32_t mndProcessGetTbSmaReq(SRpcMsg *pReq);
static int32_t mndRetrieveSma(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows); static int32_t mndRetrieveSma(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows);
static void mndDestroySmaObj(SSmaObj *pSmaObj); static void mndDestroySmaObj(SSmaObj *pSmaObj);
static int32_t mndProcessCreateTSMAReq(SRpcMsg* pReq); static int32_t mndProcessCreateTSMAReq(SRpcMsg *pReq);
static int32_t mndProcessDropTSMAReq(SRpcMsg* pReq); static int32_t mndProcessDropTSMAReq(SRpcMsg *pReq);
// sma and tag index comm func // sma and tag index comm func
static int32_t mndProcessDropIdxReq(SRpcMsg *pReq); static int32_t mndProcessDropIdxReq(SRpcMsg *pReq);
@ -61,11 +61,11 @@ static void mndCancelRetrieveTSMA(SMnode *pMnode, void *pIter);
static int32_t mndProcessGetTbTSMAReq(SRpcMsg *pReq); static int32_t mndProcessGetTbTSMAReq(SRpcMsg *pReq);
typedef struct SCreateTSMACxt { typedef struct SCreateTSMACxt {
SMnode * pMnode; SMnode *pMnode;
const SRpcMsg *pRpcReq; const SRpcMsg *pRpcReq;
union { union {
const SMCreateSmaReq *pCreateSmaReq; const SMCreateSmaReq *pCreateSmaReq;
const SMDropSmaReq * pDropSmaReq; const SMDropSmaReq *pDropSmaReq;
}; };
SDbObj *pDb; SDbObj *pDb;
SStbObj *pSrcStb; SStbObj *pSrcStb;
@ -298,16 +298,13 @@ void mndReleaseSma(SMnode *pMnode, SSmaObj *pSma) {
sdbRelease(pSdb, pSma); sdbRelease(pSdb, pSma);
} }
SDbObj *mndAcquireDbBySma(SMnode *pMnode, const char *db) { SDbObj *mndAcquireDbBySma(SMnode *pMnode, const char *db) { return mndAcquireDb(pMnode, db); }
return mndAcquireDb(pMnode, db);
}
static void *mndBuildVCreateSmaReq(SMnode *pMnode, SVgObj *pVgroup, SSmaObj *pSma, int32_t *pContLen) { static void *mndBuildVCreateSmaReq(SMnode *pMnode, SVgObj *pVgroup, SSmaObj *pSma, int32_t *pContLen) {
SEncoder encoder = {0}; SEncoder encoder = {0};
int32_t contLen = 0; int32_t contLen = 0;
SName name = {0}; SName name = {0};
int32_t code = tNameFromString(&name, pSma->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE); int32_t code = tNameFromString(&name, pSma->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
if (TSDB_CODE_SUCCESS != code) { if (TSDB_CODE_SUCCESS != code) {
return NULL; return NULL;
} }
@ -368,7 +365,7 @@ static void *mndBuildVDropSmaReq(SMnode *pMnode, SVgObj *pVgroup, SSmaObj *pSma,
SEncoder encoder = {0}; SEncoder encoder = {0};
int32_t contLen; int32_t contLen;
SName name = {0}; SName name = {0};
int32_t code = tNameFromString(&name, pSma->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE); int32_t code = tNameFromString(&name, pSma->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
if (TSDB_CODE_SUCCESS != code) { if (TSDB_CODE_SUCCESS != code) {
terrno = code; terrno = code;
return NULL; return NULL;
@ -424,9 +421,9 @@ static int32_t mndSetCreateSmaRedoLogs(SMnode *pMnode, STrans *pTrans, SSmaObj *
TAOS_RETURN(code); TAOS_RETURN(code);
} }
static int32_t mndSetCreateSmaUndoLogs(SMnode* pMnode, STrans* pTrans, SSmaObj* pSma) { static int32_t mndSetCreateSmaUndoLogs(SMnode *pMnode, STrans *pTrans, SSmaObj *pSma) {
int32_t code = 0; int32_t code = 0;
SSdbRaw * pUndoRaw = mndSmaActionEncode(pSma); SSdbRaw *pUndoRaw = mndSmaActionEncode(pSma);
if (!pUndoRaw) { if (!pUndoRaw) {
code = TSDB_CODE_MND_RETURN_VALUE_NULL; code = TSDB_CODE_MND_RETURN_VALUE_NULL;
if (terrno != 0) code = terrno; if (terrno != 0) code = terrno;
@ -670,7 +667,8 @@ static int32_t mndCreateSma(SMnode *pMnode, SRpcMsg *pReq, SMCreateSmaReq *pCrea
// check the maxDelay // check the maxDelay
if (streamObj.conf.triggerParam < TSDB_MIN_ROLLUP_MAX_DELAY) { if (streamObj.conf.triggerParam < TSDB_MIN_ROLLUP_MAX_DELAY) {
int64_t msInterval = -1; int64_t msInterval = -1;
int32_t code = convertTimeFromPrecisionToUnit(pCreate->interval, pDb->cfg.precision, TIME_UNIT_MILLISECOND, &msInterval); int32_t code =
convertTimeFromPrecisionToUnit(pCreate->interval, pDb->cfg.precision, TIME_UNIT_MILLISECOND, &msInterval);
if (TSDB_CODE_SUCCESS != code) { if (TSDB_CODE_SUCCESS != code) {
mError("sma:%s, failed to create since convert time failed: %s", smaObj.name, tstrerror(code)); mError("sma:%s, failed to create since convert time failed: %s", smaObj.name, tstrerror(code));
return code; return code;
@ -792,7 +790,7 @@ static int32_t mndCheckCreateSmaReq(SMCreateSmaReq *pCreate) {
} }
static int32_t mndGetStreamNameFromSmaName(char *streamName, char *smaName) { static int32_t mndGetStreamNameFromSmaName(char *streamName, char *smaName) {
SName n; SName n;
int32_t code = tNameFromString(&n, smaName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE); int32_t code = tNameFromString(&n, smaName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
if (TSDB_CODE_SUCCESS != code) { if (TSDB_CODE_SUCCESS != code) {
return code; return code;
@ -984,10 +982,10 @@ static int32_t mndSetDropSmaVgroupRedoActions(SMnode *pMnode, STrans *pTrans, SD
} }
static int32_t mndDropSma(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SSmaObj *pSma) { static int32_t mndDropSma(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SSmaObj *pSma) {
int32_t code = -1; int32_t code = -1;
SVgObj *pVgroup = NULL; SVgObj *pVgroup = NULL;
SStbObj *pStb = NULL; SStbObj *pStb = NULL;
STrans *pTrans = NULL; STrans *pTrans = NULL;
SStreamObj *pStream = NULL; SStreamObj *pStream = NULL;
pVgroup = mndAcquireVgroup(pMnode, pSma->dstVgId); pVgroup = mndAcquireVgroup(pMnode, pSma->dstVgId);
@ -1023,7 +1021,6 @@ static int32_t mndDropSma(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SSmaObj *p
goto _OVER; goto _OVER;
} }
code = mndAcquireStream(pMnode, streamName, &pStream); code = mndAcquireStream(pMnode, streamName, &pStream);
if (pStream == NULL || pStream->smaId != pSma->uid || code != 0) { if (pStream == NULL || pStream->smaId != pSma->uid || code != 0) {
sdbRelease(pMnode->pSdb, pStream); sdbRelease(pMnode->pSdb, pStream);
@ -1124,8 +1121,8 @@ _OVER:
int32_t mndDropSmasByDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) { int32_t mndDropSmasByDb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) {
int32_t code = 0; int32_t code = 0;
SSdb *pSdb = pMnode->pSdb; SSdb *pSdb = pMnode->pSdb;
void *pIter = NULL; void *pIter = NULL;
while (1) { while (1) {
SSmaObj *pSma = NULL; SSmaObj *pSma = NULL;
@ -1232,7 +1229,7 @@ static int32_t mndGetSma(SMnode *pMnode, SUserIndexReq *indexReq, SUserIndexRsp
FOREACH(node, pList) { FOREACH(node, pList) {
SFunctionNode *pFunc = (SFunctionNode *)node; SFunctionNode *pFunc = (SFunctionNode *)node;
extOffset += tsnprintf(rsp->indexExts + extOffset, sizeof(rsp->indexExts) - extOffset - 1, "%s%s", extOffset += tsnprintf(rsp->indexExts + extOffset, sizeof(rsp->indexExts) - extOffset - 1, "%s%s",
(extOffset ? "," : ""), pFunc->functionName); (extOffset ? "," : ""), pFunc->functionName);
} }
*exist = true; *exist = true;
@ -1439,8 +1436,8 @@ static int32_t mndRetrieveSma(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBloc
SName smaName = {0}; SName smaName = {0};
SName stbName = {0}; SName stbName = {0};
char n2[TSDB_DB_FNAME_LEN + VARSTR_HEADER_SIZE] = {0}; char n2[TSDB_DB_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
char n3[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0}; char n3[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
code = tNameFromString(&smaName, pSma->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE); code = tNameFromString(&smaName, pSma->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
char n1[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0}; char n1[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
@ -1448,7 +1445,7 @@ static int32_t mndRetrieveSma(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBloc
STR_TO_VARSTR(n2, (char *)mndGetDbStr(pSma->db)); STR_TO_VARSTR(n2, (char *)mndGetDbStr(pSma->db));
code = tNameFromString(&stbName, pSma->stb, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE); code = tNameFromString(&stbName, pSma->stb, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
} }
SColumnInfoData* pColInfo = NULL; SColumnInfoData *pColInfo = NULL;
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
STR_TO_VARSTR(n3, (char *)tNameGetTableName(&stbName)); STR_TO_VARSTR(n3, (char *)tNameGetTableName(&stbName));
@ -1540,7 +1537,7 @@ static void mndCancelRetrieveIdx(SMnode *pMnode, void *pIter) {
taosMemoryFree(p); taosMemoryFree(p);
} }
static void initSMAObj(SCreateTSMACxt* pCxt) { static void initSMAObj(SCreateTSMACxt *pCxt) {
memcpy(pCxt->pSma->name, pCxt->pCreateSmaReq->name, TSDB_TABLE_FNAME_LEN); memcpy(pCxt->pSma->name, pCxt->pCreateSmaReq->name, TSDB_TABLE_FNAME_LEN);
memcpy(pCxt->pSma->stb, pCxt->pCreateSmaReq->stb, TSDB_TABLE_FNAME_LEN); memcpy(pCxt->pSma->stb, pCxt->pCreateSmaReq->stb, TSDB_TABLE_FNAME_LEN);
memcpy(pCxt->pSma->db, pCxt->pDb->name, TSDB_DB_FNAME_LEN); memcpy(pCxt->pSma->db, pCxt->pDb->name, TSDB_DB_FNAME_LEN);
@ -1549,7 +1546,7 @@ static void initSMAObj(SCreateTSMACxt* pCxt) {
pCxt->pSma->uid = mndGenerateUid(pCxt->pCreateSmaReq->name, TSDB_TABLE_FNAME_LEN); pCxt->pSma->uid = mndGenerateUid(pCxt->pCreateSmaReq->name, TSDB_TABLE_FNAME_LEN);
memcpy(pCxt->pSma->dstTbName, pCxt->targetStbFullName, TSDB_TABLE_FNAME_LEN); memcpy(pCxt->pSma->dstTbName, pCxt->targetStbFullName, TSDB_TABLE_FNAME_LEN);
pCxt->pSma->dstTbUid = 0; // not used pCxt->pSma->dstTbUid = 0; // not used
pCxt->pSma->stbUid = pCxt->pSrcStb ? pCxt->pSrcStb->uid : pCxt->pCreateSmaReq->normSourceTbUid; pCxt->pSma->stbUid = pCxt->pSrcStb ? pCxt->pSrcStb->uid : pCxt->pCreateSmaReq->normSourceTbUid;
pCxt->pSma->dbUid = pCxt->pDb->uid; pCxt->pSma->dbUid = pCxt->pDb->uid;
pCxt->pSma->interval = pCxt->pCreateSmaReq->interval; pCxt->pSma->interval = pCxt->pCreateSmaReq->interval;
@ -1598,7 +1595,7 @@ static int32_t mndCreateTSMABuildCreateStreamReq(SCreateTSMACxt *pCxt) {
if (!pCxt->pCreateStreamReq->pTags) { if (!pCxt->pCreateStreamReq->pTags) {
return terrno; return terrno;
} }
SField f = {0}; SField f = {0};
int32_t code = 0; int32_t code = 0;
if (pCxt->pSrcStb) { if (pCxt->pSrcStb) {
for (int32_t idx = 0; idx < pCxt->pCreateStreamReq->numOfTags - 1; ++idx) { for (int32_t idx = 0; idx < pCxt->pCreateStreamReq->numOfTags - 1; ++idx) {
@ -1626,9 +1623,9 @@ static int32_t mndCreateTSMABuildCreateStreamReq(SCreateTSMACxt *pCxt) {
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
// construct output cols // construct output cols
SNode* pNode; SNode *pNode;
FOREACH(pNode, pCxt->pProjects) { FOREACH(pNode, pCxt->pProjects) {
SExprNode* pExprNode = (SExprNode*)pNode; SExprNode *pExprNode = (SExprNode *)pNode;
f.bytes = pExprNode->resType.bytes; f.bytes = pExprNode->resType.bytes;
f.type = pExprNode->resType.type; f.type = pExprNode->resType.type;
f.flags = COL_SMA_ON; f.flags = COL_SMA_ON;
@ -1642,7 +1639,7 @@ static int32_t mndCreateTSMABuildCreateStreamReq(SCreateTSMACxt *pCxt) {
return code; return code;
} }
static int32_t mndCreateTSMABuildDropStreamReq(SCreateTSMACxt* pCxt) { static int32_t mndCreateTSMABuildDropStreamReq(SCreateTSMACxt *pCxt) {
tstrncpy(pCxt->pDropStreamReq->name, pCxt->streamName, TSDB_STREAM_FNAME_LEN); tstrncpy(pCxt->pDropStreamReq->name, pCxt->streamName, TSDB_STREAM_FNAME_LEN);
pCxt->pDropStreamReq->igNotExists = false; pCxt->pDropStreamReq->igNotExists = false;
pCxt->pDropStreamReq->sql = taosStrdup(pCxt->pDropSmaReq->name); pCxt->pDropStreamReq->sql = taosStrdup(pCxt->pDropSmaReq->name);
@ -1685,14 +1682,13 @@ static int32_t mndSetUpdateDbTsmaVersionCommitLogs(SMnode *pMnode, STrans *pTran
TAOS_RETURN(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY)); TAOS_RETURN(sdbSetRawStatus(pCommitRaw, SDB_STATUS_READY));
} }
static int32_t mndCreateTSMATxnPrepare(SCreateTSMACxt* pCxt) { static int32_t mndCreateTSMATxnPrepare(SCreateTSMACxt *pCxt) {
int32_t code = -1; int32_t code = -1;
STransAction createStreamRedoAction = {0}; STransAction createStreamRedoAction = {0};
STransAction createStreamUndoAction = {0}; STransAction createStreamUndoAction = {0};
STransAction dropStbUndoAction = {0}; STransAction dropStbUndoAction = {0};
SMDropStbReq dropStbReq = {0}; SMDropStbReq dropStbReq = {0};
STrans *pTrans = STrans *pTrans = mndTransCreate(pCxt->pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_TSMA, pCxt->pRpcReq, "create-tsma");
mndTransCreate(pCxt->pMnode, TRN_POLICY_ROLLBACK, TRN_CONFLICT_TSMA, pCxt->pRpcReq, "create-tsma");
if (!pTrans) { if (!pTrans) {
code = terrno; code = terrno;
goto _OVER; goto _OVER;
@ -1713,7 +1709,9 @@ static int32_t mndCreateTSMATxnPrepare(SCreateTSMACxt* pCxt) {
code = terrno; code = terrno;
goto _OVER; goto _OVER;
} }
if (createStreamRedoAction.contLen != tSerializeSCMCreateStreamReq(createStreamRedoAction.pCont, createStreamRedoAction.contLen, pCxt->pCreateStreamReq)) { if (createStreamRedoAction.contLen != tSerializeSCMCreateStreamReq(createStreamRedoAction.pCont,
createStreamRedoAction.contLen,
pCxt->pCreateStreamReq)) {
mError("sma: %s, failed to create due to create stream req encode failure", pCxt->pCreateSmaReq->name); mError("sma: %s, failed to create due to create stream req encode failure", pCxt->pCreateSmaReq->name);
code = TSDB_CODE_INVALID_MSG; code = TSDB_CODE_INVALID_MSG;
goto _OVER; goto _OVER;
@ -1728,7 +1726,8 @@ static int32_t mndCreateTSMATxnPrepare(SCreateTSMACxt* pCxt) {
code = terrno; code = terrno;
goto _OVER; goto _OVER;
} }
if (createStreamUndoAction.contLen != tSerializeSMDropStreamReq(createStreamUndoAction.pCont, createStreamUndoAction.contLen, pCxt->pDropStreamReq)) { if (createStreamUndoAction.contLen !=
tSerializeSMDropStreamReq(createStreamUndoAction.pCont, createStreamUndoAction.contLen, pCxt->pDropStreamReq)) {
mError("sma: %s, failed to create due to drop stream req encode failure", pCxt->pCreateSmaReq->name); mError("sma: %s, failed to create due to drop stream req encode failure", pCxt->pCreateSmaReq->name);
code = TSDB_CODE_INVALID_MSG; code = TSDB_CODE_INVALID_MSG;
goto _OVER; goto _OVER;
@ -1746,7 +1745,8 @@ static int32_t mndCreateTSMATxnPrepare(SCreateTSMACxt* pCxt) {
code = terrno; code = terrno;
goto _OVER; goto _OVER;
} }
if (dropStbUndoAction.contLen != tSerializeSMDropStbReq(dropStbUndoAction.pCont, dropStbUndoAction.contLen, &dropStbReq)) { if (dropStbUndoAction.contLen !=
tSerializeSMDropStbReq(dropStbUndoAction.pCont, dropStbUndoAction.contLen, &dropStbReq)) {
mError("sma: %s, failed to create due to drop stb req encode failure", pCxt->pCreateSmaReq->name); mError("sma: %s, failed to create due to drop stb req encode failure", pCxt->pCreateSmaReq->name);
code = TSDB_CODE_INVALID_MSG; code = TSDB_CODE_INVALID_MSG;
goto _OVER; goto _OVER;
@ -1781,7 +1781,7 @@ static int32_t mndCreateTSMA(SCreateTSMACxt *pCxt) {
pCxt->pSma = &sma; pCxt->pSma = &sma;
initSMAObj(pCxt); initSMAObj(pCxt);
SNodeList* pProjects = NULL; SNodeList *pProjects = NULL;
code = nodesStringToList(pCxt->pCreateSmaReq->expr, &pProjects); code = nodesStringToList(pCxt->pCreateSmaReq->expr, &pProjects);
if (TSDB_CODE_SUCCESS != code) { if (TSDB_CODE_SUCCESS != code) {
goto _OVER; goto _OVER;
@ -1830,8 +1830,8 @@ _OVER:
TAOS_RETURN(code); TAOS_RETURN(code);
} }
static int32_t mndTSMAGenerateOutputName(const char* tsmaName, char* streamName, char* targetStbName) { static int32_t mndTSMAGenerateOutputName(const char *tsmaName, char *streamName, char *targetStbName) {
SName smaName; SName smaName;
int32_t code = tNameFromString(&smaName, tsmaName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE); int32_t code = tNameFromString(&smaName, tsmaName, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
if (TSDB_CODE_SUCCESS != code) { if (TSDB_CODE_SUCCESS != code) {
return code; return code;
@ -1841,17 +1841,17 @@ static int32_t mndTSMAGenerateOutputName(const char* tsmaName, char* streamName,
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
static int32_t mndProcessCreateTSMAReq(SRpcMsg* pReq) { static int32_t mndProcessCreateTSMAReq(SRpcMsg *pReq) {
#ifdef WINDOWS #ifdef WINDOWS
TAOS_RETURN(TSDB_CODE_MND_INVALID_PLATFORM); TAOS_RETURN(TSDB_CODE_MND_INVALID_PLATFORM);
#endif #endif
SMnode * pMnode = pReq->info.node; SMnode *pMnode = pReq->info.node;
int32_t code = -1; int32_t code = -1;
SDbObj * pDb = NULL; SDbObj *pDb = NULL;
SStbObj * pStb = NULL; SStbObj *pStb = NULL;
SSmaObj * pSma = NULL; SSmaObj *pSma = NULL;
SSmaObj * pBaseTsma = NULL; SSmaObj *pBaseTsma = NULL;
SStreamObj * pStream = NULL; SStreamObj *pStream = NULL;
int64_t mTraceId = TRACE_GET_ROOTID(&pReq->info.traceId); int64_t mTraceId = TRACE_GET_ROOTID(&pReq->info.traceId);
SMCreateSmaReq createReq = {0}; SMCreateSmaReq createReq = {0};
@ -1941,16 +1941,16 @@ static int32_t mndProcessCreateTSMAReq(SRpcMsg* pReq) {
} }
SCreateTSMACxt cxt = { SCreateTSMACxt cxt = {
.pMnode = pMnode, .pMnode = pMnode,
.pCreateSmaReq = &createReq, .pCreateSmaReq = &createReq,
.pCreateStreamReq = NULL, .pCreateStreamReq = NULL,
.streamName = streamName, .streamName = streamName,
.targetStbFullName = streamTargetStbFullName, .targetStbFullName = streamTargetStbFullName,
.pDb = pDb, .pDb = pDb,
.pRpcReq = pReq, .pRpcReq = pReq,
.pSma = NULL, .pSma = NULL,
.pBaseSma = pBaseTsma, .pBaseSma = pBaseTsma,
.pSrcStb = pStb, .pSrcStb = pStb,
}; };
code = mndCreateTSMA(&cxt); code = mndCreateTSMA(&cxt);
@ -1971,10 +1971,10 @@ _OVER:
TAOS_RETURN(code); TAOS_RETURN(code);
} }
static int32_t mndDropTSMA(SCreateTSMACxt* pCxt) { static int32_t mndDropTSMA(SCreateTSMACxt *pCxt) {
int32_t code = -1; int32_t code = -1;
STransAction dropStreamRedoAction = {0}; STransAction dropStreamRedoAction = {0};
STrans *pTrans = mndTransCreate(pCxt->pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_TSMA, pCxt->pRpcReq, "drop-tsma"); STrans *pTrans = mndTransCreate(pCxt->pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_TSMA, pCxt->pRpcReq, "drop-tsma");
if (!pTrans) { if (!pTrans) {
code = terrno; code = terrno;
goto _OVER; goto _OVER;
@ -2021,7 +2021,8 @@ static int32_t mndDropTSMA(SCreateTSMACxt* pCxt) {
code = terrno; code = terrno;
goto _OVER; goto _OVER;
} }
if (dropStbRedoAction.contLen != tSerializeSMDropStbReq(dropStbRedoAction.pCont, dropStbRedoAction.contLen, &dropStbReq)) { if (dropStbRedoAction.contLen !=
tSerializeSMDropStbReq(dropStbRedoAction.pCont, dropStbRedoAction.contLen, &dropStbReq)) {
mError("tsma: %s, failedto drop due to drop stb req encode failure", pCxt->pDropSmaReq->name); mError("tsma: %s, failedto drop due to drop stb req encode failure", pCxt->pDropSmaReq->name);
code = TSDB_CODE_INVALID_MSG; code = TSDB_CODE_INVALID_MSG;
goto _OVER; goto _OVER;
@ -2044,9 +2045,9 @@ _OVER:
TAOS_RETURN(code); TAOS_RETURN(code);
} }
static bool hasRecursiveTsmasBasedOnMe(SMnode* pMnode, const SSmaObj* pSma) { static bool hasRecursiveTsmasBasedOnMe(SMnode *pMnode, const SSmaObj *pSma) {
SSmaObj *pSmaObj = NULL; SSmaObj *pSmaObj = NULL;
void * pIter = NULL; void *pIter = NULL;
while (1) { while (1) {
pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSmaObj); pIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter, (void **)&pSmaObj);
if (pIter == NULL) break; if (pIter == NULL) break;
@ -2060,25 +2061,25 @@ static bool hasRecursiveTsmasBasedOnMe(SMnode* pMnode, const SSmaObj* pSma) {
return false; return false;
} }
static int32_t mndProcessDropTSMAReq(SRpcMsg* pReq) { static int32_t mndProcessDropTSMAReq(SRpcMsg *pReq) {
int32_t code = -1; int32_t code = -1;
SMDropSmaReq dropReq = {0}; SMDropSmaReq dropReq = {0};
SSmaObj * pSma = NULL; SSmaObj *pSma = NULL;
SDbObj * pDb = NULL; SDbObj *pDb = NULL;
SMnode * pMnode = pReq->info.node; SMnode *pMnode = pReq->info.node;
if (tDeserializeSMDropSmaReq(pReq->pCont, pReq->contLen, &dropReq) != TSDB_CODE_SUCCESS) { if (tDeserializeSMDropSmaReq(pReq->pCont, pReq->contLen, &dropReq) != TSDB_CODE_SUCCESS) {
code = TSDB_CODE_INVALID_MSG; code = TSDB_CODE_INVALID_MSG;
goto _OVER; goto _OVER;
} }
char streamName[TSDB_TABLE_FNAME_LEN] = {0}; char streamName[TSDB_TABLE_FNAME_LEN] = {0};
char streamTargetStbFullName[TSDB_TABLE_FNAME_LEN] = {0}; char streamTargetStbFullName[TSDB_TABLE_FNAME_LEN] = {0};
code = mndTSMAGenerateOutputName(dropReq.name, streamName, streamTargetStbFullName); code = mndTSMAGenerateOutputName(dropReq.name, streamName, streamTargetStbFullName);
if (TSDB_CODE_SUCCESS != code) { if (TSDB_CODE_SUCCESS != code) {
goto _OVER; goto _OVER;
} }
SStbObj* pStb = mndAcquireStb(pMnode, streamTargetStbFullName); SStbObj *pStb = mndAcquireStb(pMnode, streamTargetStbFullName);
pSma = mndAcquireSma(pMnode, dropReq.name); pSma = mndAcquireSma(pMnode, dropReq.name);
if (!pSma && dropReq.igNotExists) { if (!pSma && dropReq.igNotExists) {
@ -2113,13 +2114,13 @@ static int32_t mndProcessDropTSMAReq(SRpcMsg* pReq) {
} }
SCreateTSMACxt cxt = { SCreateTSMACxt cxt = {
.pDb = pDb, .pDb = pDb,
.pMnode = pMnode, .pMnode = pMnode,
.pRpcReq = pReq, .pRpcReq = pReq,
.pSma = pSma, .pSma = pSma,
.streamName = streamName, .streamName = streamName,
.targetStbFullName = streamTargetStbFullName, .targetStbFullName = streamTargetStbFullName,
.pDropSmaReq = &dropReq, .pDropSmaReq = &dropReq,
}; };
code = mndDropTSMA(&cxt); code = mndDropTSMA(&cxt);
@ -2134,10 +2135,10 @@ _OVER:
} }
static int32_t mndRetrieveTSMA(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) { static int32_t mndRetrieveTSMA(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlock, int32_t rows) {
SDbObj * pDb = NULL; SDbObj *pDb = NULL;
int32_t numOfRows = 0; int32_t numOfRows = 0;
SSmaObj * pSma = NULL; SSmaObj *pSma = NULL;
SMnode * pMnode = pReq->info.node; SMnode *pMnode = pReq->info.node;
int32_t code = 0; int32_t code = 0;
SColumnInfoData *pColInfo; SColumnInfoData *pColInfo;
if (pShow->pIter == NULL) { if (pShow->pIter == NULL) {
@ -2153,7 +2154,7 @@ static int32_t mndRetrieveTSMA(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlo
while (numOfRows < rows) { while (numOfRows < rows) {
pIter->pSmaIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter->pSmaIter, (void **)&pSma); pIter->pSmaIter = sdbFetch(pMnode->pSdb, SDB_SMA, pIter->pSmaIter, (void **)&pSma);
if (pIter->pSmaIter == NULL) break; if (pIter->pSmaIter == NULL) break;
SDbObj* pSrcDb = mndAcquireDb(pMnode, pSma->db); SDbObj *pSrcDb = mndAcquireDb(pMnode, pSma->db);
if ((pDb && pSma->dbUid != pDb->uid) || !pSrcDb) { if ((pDb && pSma->dbUid != pDb->uid) || !pSrcDb) {
sdbRelease(pMnode->pSdb, pSma); sdbRelease(pMnode->pSdb, pSma);
@ -2176,7 +2177,7 @@ static int32_t mndRetrieveTSMA(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlo
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
STR_TO_VARSTR(db, (char *)mndGetDbStr(pSma->db)); STR_TO_VARSTR(db, (char *)mndGetDbStr(pSma->db));
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
code = colDataSetVal(pColInfo, numOfRows, (const char*)db, false); code = colDataSetVal(pColInfo, numOfRows, (const char *)db, false);
} }
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
@ -2186,12 +2187,12 @@ static int32_t mndRetrieveTSMA(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlo
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
STR_TO_VARSTR(srcTb, (char *)tNameGetTableName(&n)); STR_TO_VARSTR(srcTb, (char *)tNameGetTableName(&n));
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
code = colDataSetVal(pColInfo, numOfRows, (const char*)srcTb, false); code = colDataSetVal(pColInfo, numOfRows, (const char *)srcTb, false);
} }
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
code = colDataSetVal(pColInfo, numOfRows, (const char*)db, false); code = colDataSetVal(pColInfo, numOfRows, (const char *)db, false);
} }
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
@ -2200,29 +2201,29 @@ static int32_t mndRetrieveTSMA(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlo
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
char targetTb[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0}; char targetTb[TSDB_TABLE_FNAME_LEN + VARSTR_HEADER_SIZE] = {0};
STR_TO_VARSTR(targetTb, (char*)tNameGetTableName(&n)); STR_TO_VARSTR(targetTb, (char *)tNameGetTableName(&n));
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
code = colDataSetVal(pColInfo, numOfRows, (const char*)targetTb, false); code = colDataSetVal(pColInfo, numOfRows, (const char *)targetTb, false);
} }
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
// stream name // stream name
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
code = colDataSetVal(pColInfo, numOfRows, (const char*)smaName, false); code = colDataSetVal(pColInfo, numOfRows, (const char *)smaName, false);
} }
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
pColInfo = taosArrayGet(pBlock->pDataBlock, cols++); pColInfo = taosArrayGet(pBlock->pDataBlock, cols++);
code = colDataSetVal(pColInfo, numOfRows, (const char*)(&pSma->createdTime), false); code = colDataSetVal(pColInfo, numOfRows, (const char *)(&pSma->createdTime), false);
} }
// interval // interval
char interval[64 + VARSTR_HEADER_SIZE] = {0}; char interval[64 + VARSTR_HEADER_SIZE] = {0};
int32_t len = 0; int32_t len = 0;
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
if (!IS_CALENDAR_TIME_DURATION(pSma->intervalUnit)) { if (!IS_CALENDAR_TIME_DURATION(pSma->intervalUnit)) {
len = tsnprintf(interval + VARSTR_HEADER_SIZE, 64, "%" PRId64 "%c", pSma->interval, len = tsnprintf(interval + VARSTR_HEADER_SIZE, 64, "%" PRId64 "%c", pSma->interval,
getPrecisionUnit(pSrcDb->cfg.precision)); getPrecisionUnit(pSrcDb->cfg.precision));
} else { } else {
len = tsnprintf(interval + VARSTR_HEADER_SIZE, 64, "%" PRId64 "%c", pSma->interval, pSma->intervalUnit); len = tsnprintf(interval + VARSTR_HEADER_SIZE, 64, "%" PRId64 "%c", pSma->interval, pSma->intervalUnit);
} }
@ -2243,17 +2244,17 @@ static int32_t mndRetrieveTSMA(SRpcMsg *pReq, SShowObj *pShow, SSDataBlock *pBlo
// func list // func list
len = 0; len = 0;
SNode *pNode = NULL, *pFunc = NULL; SNode *pNode = NULL, *pFunc = NULL;
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
code = nodesStringToNode(pSma->ast, &pNode); code = nodesStringToNode(pSma->ast, &pNode);
} }
if (TSDB_CODE_SUCCESS == code) { if (TSDB_CODE_SUCCESS == code) {
char * start = buf + VARSTR_HEADER_SIZE; char *start = buf + VARSTR_HEADER_SIZE;
FOREACH(pFunc, ((SSelectStmt *)pNode)->pProjectionList) { FOREACH(pFunc, ((SSelectStmt *)pNode)->pProjectionList) {
if (nodeType(pFunc) == QUERY_NODE_FUNCTION) { if (nodeType(pFunc) == QUERY_NODE_FUNCTION) {
SFunctionNode *pFuncNode = (SFunctionNode *)pFunc; SFunctionNode *pFuncNode = (SFunctionNode *)pFunc;
if (!fmIsTSMASupportedFunc(pFuncNode->funcId)) continue; if (!fmIsTSMASupportedFunc(pFuncNode->funcId)) continue;
len += tsnprintf(start, TSDB_MAX_SAVED_SQL_LEN - len, "%s%s", start != buf + VARSTR_HEADER_SIZE ? "," : "", len += tsnprintf(start, TSDB_MAX_SAVED_SQL_LEN - len, "%s%s", start != buf + VARSTR_HEADER_SIZE ? "," : "",
((SExprNode *)pFunc)->userAlias); ((SExprNode *)pFunc)->userAlias);
if (len >= TSDB_MAX_SAVED_SQL_LEN) { if (len >= TSDB_MAX_SAVED_SQL_LEN) {
len = TSDB_MAX_SAVED_SQL_LEN; len = TSDB_MAX_SAVED_SQL_LEN;
break; break;
@ -2297,7 +2298,8 @@ static void mndCancelRetrieveTSMA(SMnode *pMnode, void *pIter) {
taosMemoryFree(p); taosMemoryFree(p);
} }
int32_t dumpTSMAInfoFromSmaObj(const SSmaObj* pSma, const SStbObj* pDestStb, STableTSMAInfo* pInfo, const SSmaObj* pBaseTsma) { int32_t dumpTSMAInfoFromSmaObj(const SSmaObj *pSma, const SStbObj *pDestStb, STableTSMAInfo *pInfo,
const SSmaObj *pBaseTsma) {
int32_t code = 0; int32_t code = 0;
pInfo->interval = pSma->interval; pInfo->interval = pSma->interval;
pInfo->unit = pSma->intervalUnit; pInfo->unit = pSma->intervalUnit;
@ -2336,7 +2338,7 @@ int32_t dumpTSMAInfoFromSmaObj(const SSmaObj* pSma, const SStbObj* pDestStb, STa
SSelectStmt *pSelect = (SSelectStmt *)pNode; SSelectStmt *pSelect = (SSelectStmt *)pNode;
FOREACH(pFunc, pSelect->pProjectionList) { FOREACH(pFunc, pSelect->pProjectionList) {
STableTSMAFuncInfo funcInfo = {0}; STableTSMAFuncInfo funcInfo = {0};
SFunctionNode * pFuncNode = (SFunctionNode *)pFunc; SFunctionNode *pFuncNode = (SFunctionNode *)pFunc;
if (!fmIsTSMASupportedFunc(pFuncNode->funcId)) continue; if (!fmIsTSMASupportedFunc(pFuncNode->funcId)) continue;
funcInfo.funcId = pFuncNode->funcId; funcInfo.funcId = pFuncNode->funcId;
funcInfo.colId = ((SColumnNode *)pFuncNode->pParameterList->pHead->pNode)->colId; funcInfo.colId = ((SColumnNode *)pFuncNode->pParameterList->pHead->pNode)->colId;
@ -2383,9 +2385,9 @@ int32_t dumpTSMAInfoFromSmaObj(const SSmaObj* pSma, const SStbObj* pDestStb, STa
} }
// @note remember to mndReleaseSma(*ppOut) // @note remember to mndReleaseSma(*ppOut)
static int32_t mndGetDeepestBaseForTsma(SMnode* pMnode, SSmaObj* pSma, SSmaObj** ppOut) { static int32_t mndGetDeepestBaseForTsma(SMnode *pMnode, SSmaObj *pSma, SSmaObj **ppOut) {
int32_t code = 0; int32_t code = 0;
SSmaObj* pRecursiveTsma = NULL; SSmaObj *pRecursiveTsma = NULL;
if (pSma->baseSmaName[0]) { if (pSma->baseSmaName[0]) {
pRecursiveTsma = mndAcquireSma(pMnode, pSma->baseSmaName); pRecursiveTsma = mndAcquireSma(pMnode, pSma->baseSmaName);
if (!pRecursiveTsma) { if (!pRecursiveTsma) {
@ -2393,7 +2395,7 @@ static int32_t mndGetDeepestBaseForTsma(SMnode* pMnode, SSmaObj* pSma, SSmaObj**
return TSDB_CODE_MND_SMA_NOT_EXIST; return TSDB_CODE_MND_SMA_NOT_EXIST;
} }
while (pRecursiveTsma->baseSmaName[0]) { while (pRecursiveTsma->baseSmaName[0]) {
SSmaObj* pTmpSma = pRecursiveTsma; SSmaObj *pTmpSma = pRecursiveTsma;
pRecursiveTsma = mndAcquireSma(pMnode, pTmpSma->baseSmaName); pRecursiveTsma = mndAcquireSma(pMnode, pTmpSma->baseSmaName);
if (!pRecursiveTsma) { if (!pRecursiveTsma) {
mError("base tsma: %s for tsma: %s not found", pTmpSma->baseSmaName, pTmpSma->name); mError("base tsma: %s for tsma: %s not found", pTmpSma->baseSmaName, pTmpSma->name);
@ -2407,7 +2409,6 @@ static int32_t mndGetDeepestBaseForTsma(SMnode* pMnode, SSmaObj* pSma, SSmaObj**
return code; return code;
} }
static int32_t mndGetTSMA(SMnode *pMnode, char *tsmaFName, STableTSMAInfoRsp *rsp, bool *exist) { static int32_t mndGetTSMA(SMnode *pMnode, char *tsmaFName, STableTSMAInfoRsp *rsp, bool *exist) {
int32_t code = -1; int32_t code = -1;
SSmaObj *pSma = NULL; SSmaObj *pSma = NULL;
@ -2450,17 +2451,17 @@ static int32_t mndGetTSMA(SMnode *pMnode, char *tsmaFName, STableTSMAInfoRsp *rs
TAOS_RETURN(code); TAOS_RETURN(code);
} }
typedef bool (*tsmaFilter)(const SSmaObj* pSma, void* param); typedef bool (*tsmaFilter)(const SSmaObj *pSma, void *param);
static int32_t mndGetSomeTsmas(SMnode* pMnode, STableTSMAInfoRsp* pRsp, tsmaFilter filtered, void* param, bool* exist) { static int32_t mndGetSomeTsmas(SMnode *pMnode, STableTSMAInfoRsp *pRsp, tsmaFilter filtered, void *param, bool *exist) {
int32_t code = 0; int32_t code = 0;
SSmaObj * pSma = NULL; SSmaObj *pSma = NULL;
SSmaObj * pBaseTsma = NULL; SSmaObj *pBaseTsma = NULL;
SSdb * pSdb = pMnode->pSdb; SSdb *pSdb = pMnode->pSdb;
void * pIter = NULL; void *pIter = NULL;
SStreamObj * pStream = NULL; SStreamObj *pStream = NULL;
SStbObj * pStb = NULL; SStbObj *pStb = NULL;
bool shouldRetry = false; bool shouldRetry = false;
while (1) { while (1) {
pIter = sdbFetch(pSdb, SDB_SMA, pIter, (void **)&pSma); pIter = sdbFetch(pSdb, SDB_SMA, pIter, (void **)&pSma);
@ -2479,7 +2480,7 @@ static int32_t mndGetSomeTsmas(SMnode* pMnode, STableTSMAInfoRsp* pRsp, tsmaFilt
} }
SName smaName; SName smaName;
char streamName[TSDB_TABLE_FNAME_LEN] = {0}; char streamName[TSDB_TABLE_FNAME_LEN] = {0};
code = tNameFromString(&smaName, pSma->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE); code = tNameFromString(&smaName, pSma->name, T_NAME_ACCT | T_NAME_DB | T_NAME_TABLE);
if (TSDB_CODE_SUCCESS != code) { if (TSDB_CODE_SUCCESS != code) {
sdbRelease(pSdb, pSma); sdbRelease(pSdb, pSma);
@ -2541,8 +2542,8 @@ static int32_t mndGetSomeTsmas(SMnode* pMnode, STableTSMAInfoRsp* pRsp, tsmaFilt
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
} }
static bool tsmaTbFilter(const SSmaObj* pSma, void* param) { static bool tsmaTbFilter(const SSmaObj *pSma, void *param) {
const char* tbFName = param; const char *tbFName = param;
return pSma->stb[0] != tbFName[0] || strcmp(pSma->stb, tbFName) != 0; return pSma->stb[0] != tbFName[0] || strcmp(pSma->stb, tbFName) != 0;
} }
@ -2550,7 +2551,7 @@ static int32_t mndGetTableTSMA(SMnode *pMnode, char *tbFName, STableTSMAInfoRsp
return mndGetSomeTsmas(pMnode, pRsp, tsmaTbFilter, tbFName, exist); return mndGetSomeTsmas(pMnode, pRsp, tsmaTbFilter, tbFName, exist);
} }
static bool tsmaDbFilter(const SSmaObj* pSma, void* param) { static bool tsmaDbFilter(const SSmaObj *pSma, void *param) {
uint64_t *dbUid = param; uint64_t *dbUid = param;
return pSma->dbUid != *dbUid; return pSma->dbUid != *dbUid;
} }
@ -2564,7 +2565,7 @@ static int32_t mndProcessGetTbTSMAReq(SRpcMsg *pReq) {
int32_t code = -1; int32_t code = -1;
STableTSMAInfoReq tsmaReq = {0}; STableTSMAInfoReq tsmaReq = {0};
bool exist = false; bool exist = false;
SMnode * pMnode = pReq->info.node; SMnode *pMnode = pReq->info.node;
TAOS_CHECK_GOTO(tDeserializeTableTSMAInfoReq(pReq->pCont, pReq->contLen, &tsmaReq), NULL, _OVER); TAOS_CHECK_GOTO(tDeserializeTableTSMAInfoReq(pReq->pCont, pReq->contLen, &tsmaReq), NULL, _OVER);
@ -2635,12 +2636,12 @@ static int32_t mkNonExistTSMAInfo(const STSMAVersion *pTsmaVer, STableTSMAInfo *
int32_t mndValidateTSMAInfo(SMnode *pMnode, STSMAVersion *pTsmaVersions, int32_t numOfTsmas, void **ppRsp, int32_t mndValidateTSMAInfo(SMnode *pMnode, STSMAVersion *pTsmaVersions, int32_t numOfTsmas, void **ppRsp,
int32_t *pRspLen) { int32_t *pRspLen) {
int32_t code = -1; int32_t code = -1;
STSMAHbRsp hbRsp = {0}; STSMAHbRsp hbRsp = {0};
int32_t rspLen = 0; int32_t rspLen = 0;
void * pRsp = NULL; void *pRsp = NULL;
char tsmaFName[TSDB_TABLE_FNAME_LEN] = {0}; char tsmaFName[TSDB_TABLE_FNAME_LEN] = {0};
STableTSMAInfo * pTsmaInfo = NULL; STableTSMAInfo *pTsmaInfo = NULL;
hbRsp.pTsmas = taosArrayInit(numOfTsmas, POINTER_BYTES); hbRsp.pTsmas = taosArrayInit(numOfTsmas, POINTER_BYTES);
if (!hbRsp.pTsmas) { if (!hbRsp.pTsmas) {
@ -2649,13 +2650,13 @@ int32_t mndValidateTSMAInfo(SMnode *pMnode, STSMAVersion *pTsmaVersions, int32_t
} }
for (int32_t i = 0; i < numOfTsmas; ++i) { for (int32_t i = 0; i < numOfTsmas; ++i) {
STSMAVersion* pTsmaVer = &pTsmaVersions[i]; STSMAVersion *pTsmaVer = &pTsmaVersions[i];
pTsmaVer->dbId = be64toh(pTsmaVer->dbId); pTsmaVer->dbId = be64toh(pTsmaVer->dbId);
pTsmaVer->tsmaId = be64toh(pTsmaVer->tsmaId); pTsmaVer->tsmaId = be64toh(pTsmaVer->tsmaId);
pTsmaVer->version = ntohl(pTsmaVer->version); pTsmaVer->version = ntohl(pTsmaVer->version);
snprintf(tsmaFName, sizeof(tsmaFName), "%s.%s", pTsmaVer->dbFName, pTsmaVer->name); snprintf(tsmaFName, sizeof(tsmaFName), "%s.%s", pTsmaVer->dbFName, pTsmaVer->name);
SSmaObj* pSma = mndAcquireSma(pMnode, tsmaFName); SSmaObj *pSma = mndAcquireSma(pMnode, tsmaFName);
if (!pSma) { if (!pSma) {
code = mkNonExistTSMAInfo(pTsmaVer, &pTsmaInfo); code = mkNonExistTSMAInfo(pTsmaVer, &pTsmaInfo);
if (code) goto _OVER; if (code) goto _OVER;
@ -2683,7 +2684,7 @@ int32_t mndValidateTSMAInfo(SMnode *pMnode, STSMAVersion *pTsmaVersions, int32_t
continue; continue;
} }
SStbObj* pDestStb = mndAcquireStb(pMnode, pSma->dstTbName); SStbObj *pDestStb = mndAcquireStb(pMnode, pSma->dstTbName);
if (!pDestStb) { if (!pDestStb) {
mInfo("tsma: %s.%" PRIx64 " dest stb: %s not found, maybe dropped", tsmaFName, pTsmaVer->tsmaId, pSma->dstTbName); mInfo("tsma: %s.%" PRIx64 " dest stb: %s not found, maybe dropped", tsmaFName, pTsmaVer->tsmaId, pSma->dstTbName);
code = mkNonExistTSMAInfo(pTsmaVer, &pTsmaInfo); code = mkNonExistTSMAInfo(pTsmaVer, &pTsmaInfo);
@ -2698,7 +2699,7 @@ int32_t mndValidateTSMAInfo(SMnode *pMnode, STSMAVersion *pTsmaVersions, int32_t
} }
// dump smaObj into rsp // dump smaObj into rsp
STableTSMAInfo * pInfo = NULL; STableTSMAInfo *pInfo = NULL;
pInfo = taosMemoryCalloc(1, sizeof(STableTSMAInfo)); pInfo = taosMemoryCalloc(1, sizeof(STableTSMAInfo));
if (!pInfo) { if (!pInfo) {
code = terrno; code = terrno;
@ -2707,7 +2708,7 @@ int32_t mndValidateTSMAInfo(SMnode *pMnode, STSMAVersion *pTsmaVersions, int32_t
goto _OVER; goto _OVER;
} }
SSmaObj* pBaseSma = NULL; SSmaObj *pBaseSma = NULL;
code = mndGetDeepestBaseForTsma(pMnode, pSma, &pBaseSma); code = mndGetDeepestBaseForTsma(pMnode, pSma, &pBaseSma);
if (code == 0) code = dumpTSMAInfoFromSmaObj(pSma, pDestStb, pInfo, pBaseSma); if (code == 0) code = dumpTSMAInfoFromSmaObj(pSma, pDestStb, pInfo, pBaseSma);

View File

@ -1706,8 +1706,8 @@ static int32_t mndCreateUser(SMnode *pMnode, char *acct, SCreateUserReq *pCreate
if (pCreate->isImport != 1) { if (pCreate->isImport != 1) {
taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.pass); taosEncryptPass_c((uint8_t *)pCreate->pass, strlen(pCreate->pass), userObj.pass);
} else { } else {
// mInfo("pCreate->pass:%s", pCreate->pass) // mInfo("pCreate->pass:%s", pCreate->eass)
strncpy(userObj.pass, pCreate->pass, TSDB_PASSWORD_LEN); memcpy(userObj.pass, pCreate->pass, TSDB_PASSWORD_LEN);
} }
tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN); tstrncpy(userObj.user, pCreate->user, TSDB_USER_LEN);
tstrncpy(userObj.acct, acct, TSDB_USER_LEN); tstrncpy(userObj.acct, acct, TSDB_USER_LEN);

View File

@ -370,7 +370,7 @@ static int32_t sdbReadFileImp(SSdb *pSdb) {
opts.source = pRaw->pData; opts.source = pRaw->pData;
opts.result = plantContent; opts.result = plantContent;
opts.unitLen = 16; opts.unitLen = 16;
strncpy(opts.key, tsEncryptKey, ENCRYPT_KEY_LEN); tstrncpy(opts.key, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
count = CBC_Decrypt(&opts); count = CBC_Decrypt(&opts);
@ -510,7 +510,7 @@ static int32_t sdbWriteFileImp(SSdb *pSdb, int32_t skip_type) {
opts.source = pRaw->pData; opts.source = pRaw->pData;
opts.result = newData; opts.result = newData;
opts.unitLen = 16; opts.unitLen = 16;
strncpy(opts.key, tsEncryptKey, ENCRYPT_KEY_LEN); tstrncpy(opts.key, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
int32_t count = CBC_Encrypt(&opts); int32_t count = CBC_Encrypt(&opts);

View File

@ -396,7 +396,7 @@ static int32_t tsdbFSAddEntryToFileObjHash(STFileHash *hash, const char *fname)
STFileHashEntry *entry = taosMemoryMalloc(sizeof(*entry)); STFileHashEntry *entry = taosMemoryMalloc(sizeof(*entry));
if (entry == NULL) return terrno; if (entry == NULL) return terrno;
strncpy(entry->fname, fname, TSDB_FILENAME_LEN); tstrncpy(entry->fname, fname, TSDB_FILENAME_LEN);
uint32_t idx = MurmurHash3_32(fname, strlen(fname)) % hash->numBucket; uint32_t idx = MurmurHash3_32(fname, strlen(fname)) % hash->numBucket;

View File

@ -175,7 +175,7 @@ static int32_t tsdbWriteFilePage(STsdbFD *pFD, int32_t encryptAlgorithm, char *e
opts.result = PacketData; opts.result = PacketData;
opts.unitLen = 128; opts.unitLen = 128;
// strncpy(opts.key, tsEncryptKey, 16); // strncpy(opts.key, tsEncryptKey, 16);
strncpy(opts.key, encryptKey, ENCRYPT_KEY_LEN); tstrncpy(opts.key, encryptKey, ENCRYPT_KEY_LEN + 1);
NewLen = CBC_Encrypt(&opts); NewLen = CBC_Encrypt(&opts);
@ -249,7 +249,7 @@ static int32_t tsdbReadFilePage(STsdbFD *pFD, int64_t pgno, int32_t encryptAlgor
opts.result = PacketData; opts.result = PacketData;
opts.unitLen = 128; opts.unitLen = 128;
// strncpy(opts.key, tsEncryptKey, 16); // strncpy(opts.key, tsEncryptKey, 16);
strncpy(opts.key, encryptKey, ENCRYPT_KEY_LEN); tstrncpy(opts.key, encryptKey, ENCRYPT_KEY_LEN + 1);
NewLen = CBC_Decrypt(&opts); NewLen = CBC_Decrypt(&opts);

View File

@ -265,7 +265,7 @@ int vnodeDecodeConfig(const SJson *pJson, void *pObj) {
if (tsEncryptKey[0] == 0) { if (tsEncryptKey[0] == 0) {
return terrno = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY; return terrno = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
} else { } else {
strncpy(pCfg->tsdbCfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN); tstrncpy(pCfg->tsdbCfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
} }
} }
#endif #endif
@ -292,7 +292,7 @@ int vnodeDecodeConfig(const SJson *pJson, void *pObj) {
if (tsEncryptKey[0] == 0) { if (tsEncryptKey[0] == 0) {
return terrno = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY; return terrno = TSDB_CODE_DNODE_INVALID_ENCRYPTKEY;
} else { } else {
strncpy(pCfg->walCfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN); tstrncpy(pCfg->walCfg.encryptKey, tsEncryptKey, ENCRYPT_KEY_LEN + 1);
} }
} }
#endif #endif

View File

@ -190,7 +190,14 @@ int32_t vnodeRenameVgroupId(const char *srcPath, const char *dstPath, int32_t sr
char *tsdbFilePrefixPos = strstr(oldRname, tsdbFilePrefix); char *tsdbFilePrefixPos = strstr(oldRname, tsdbFilePrefix);
if (tsdbFilePrefixPos == NULL) continue; if (tsdbFilePrefixPos == NULL) continue;
int32_t tsdbFileVgId = atoi(tsdbFilePrefixPos + prefixLen); int32_t tsdbFileVgId = 0; // atoi(tsdbFilePrefixPos + prefixLen);
ret = taosStr2int32(tsdbFilePrefixPos + prefixLen, &tsdbFileVgId);
if (ret != 0) {
vError("vgId:%d, failed to get tsdb file vgid since %s", dstVgId, tstrerror(ret));
tfsClosedir(tsdbDir);
return ret;
}
if (tsdbFileVgId == srcVgId) { if (tsdbFileVgId == srcVgId) {
char *tsdbFileSurfixPos = tsdbFilePrefixPos + prefixLen + vnodeVgroupIdLen(srcVgId); char *tsdbFileSurfixPos = tsdbFilePrefixPos + prefixLen + vnodeVgroupIdLen(srcVgId);

View File

@ -1024,7 +1024,7 @@ static int32_t vnodeProcessFetchTtlExpiredTbs(SVnode *pVnode, int64_t ver, void
expiredTb.suid = *uid; expiredTb.suid = *uid;
terrno = metaReaderGetTableEntryByUid(&mr, *uid); terrno = metaReaderGetTableEntryByUid(&mr, *uid);
if (terrno < 0) goto _end; if (terrno < 0) goto _end;
strncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN); tstrncpy(buf, mr.me.name, TSDB_TABLE_NAME_LEN);
void *p = taosArrayPush(pNames, buf); void *p = taosArrayPush(pNames, buf);
if (p == NULL) { if (p == NULL) {
goto _end; goto _end;

View File

@ -41,10 +41,11 @@ void taos_monitor_split_str_metric(char** arr, taos_metric_t* metric, const char
memset(name, 0, size + 1); memset(name, 0, size + 1);
memcpy(name, metric->name, size); memcpy(name, metric->name, size);
char* s = strtok(name, del); char* saveptr;
char* s = strtok_r(name, del, &saveptr);
while (s != NULL) { while (s != NULL) {
*arr++ = s; *arr++ = s;
s = strtok(NULL, del); s = strtok_r(NULL, del, &saveptr);
} }
*buf = name; *buf = name;

View File

@ -2872,13 +2872,16 @@ static int32_t getIpV4RangeFromWhitelistItem(char* ipRange, SIpV4Range* pIpRange
*slash = '\0'; *slash = '\0';
struct in_addr addr; struct in_addr addr;
if (uv_inet_pton(AF_INET, ipCopy, &addr) == 0) { if (uv_inet_pton(AF_INET, ipCopy, &addr) == 0) {
int prefix = atoi(slash + 1); int32_t prefix = 0;
if (prefix < 0 || prefix > 32) { code = taosStr2int32(slash + 1, &prefix);
code = TSDB_CODE_PAR_INVALID_IP_RANGE; if (code == 0) {
} else { if (prefix < 0 || prefix > 32) {
pIpRange->ip = addr.s_addr; code = TSDB_CODE_PAR_INVALID_IP_RANGE;
pIpRange->mask = prefix; } else {
code = TSDB_CODE_SUCCESS; pIpRange->ip = addr.s_addr;
pIpRange->mask = prefix;
code = TSDB_CODE_SUCCESS;
}
} }
} else { } else {
code = TSDB_CODE_PAR_INVALID_IP_RANGE; code = TSDB_CODE_PAR_INVALID_IP_RANGE;

View File

@ -166,10 +166,12 @@ int32_t streamStateDel_rocksdb(SStreamState* pState, const SWinKey* key);
int32_t streamStateClear_rocksdb(SStreamState* pState); int32_t streamStateClear_rocksdb(SStreamState* pState);
void streamStateCurNext_rocksdb(SStreamStateCur* pCur); void streamStateCurNext_rocksdb(SStreamStateCur* pCur);
int32_t streamStateGetFirst_rocksdb(SStreamState* pState, SWinKey* key); int32_t streamStateGetFirst_rocksdb(SStreamState* pState, SWinKey* key);
int32_t streamStateGetGroupKVByCur_rocksdb(SStreamState* pState, SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen); int32_t streamStateGetGroupKVByCur_rocksdb(SStreamState* pState, SStreamStateCur* pCur, SWinKey* pKey,
const void** pVal, int32_t* pVLen);
int32_t streamStateAddIfNotExist_rocksdb(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen); int32_t streamStateAddIfNotExist_rocksdb(SStreamState* pState, const SWinKey* key, void** pVal, int32_t* pVLen);
void streamStateCurPrev_rocksdb(SStreamStateCur* pCur); void streamStateCurPrev_rocksdb(SStreamStateCur* pCur);
int32_t streamStateGetKVByCur_rocksdb(SStreamState* pState, SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen); int32_t streamStateGetKVByCur_rocksdb(SStreamState* pState, SStreamStateCur* pCur, SWinKey* pKey, const void** pVal,
int32_t* pVLen);
SStreamStateCur* streamStateGetAndCheckCur_rocksdb(SStreamState* pState, SWinKey* key); SStreamStateCur* streamStateGetAndCheckCur_rocksdb(SStreamState* pState, SWinKey* key);
SStreamStateCur* streamStateSeekKeyNext_rocksdb(SStreamState* pState, const SWinKey* key); SStreamStateCur* streamStateSeekKeyNext_rocksdb(SStreamState* pState, const SWinKey* key);
SStreamStateCur* streamStateSeekKeyPrev_rocksdb(SStreamState* pState, const SWinKey* key); SStreamStateCur* streamStateSeekKeyPrev_rocksdb(SStreamState* pState, const SWinKey* key);
@ -217,15 +219,14 @@ int32_t streamStateFillGetGroupKVByCur_rocksdb(SStreamStateCur* pCur, SWinKey* p
// partag cf // partag cf
int32_t streamStatePutParTag_rocksdb(SStreamState* pState, int64_t groupId, const void* tag, int32_t tagLen); int32_t streamStatePutParTag_rocksdb(SStreamState* pState, int64_t groupId, const void* tag, int32_t tagLen);
int32_t streamStateGetParTag_rocksdb(SStreamState* pState, int64_t groupId, void** tagVal, int32_t* tagLen); int32_t streamStateGetParTag_rocksdb(SStreamState* pState, int64_t groupId, void** tagVal, int32_t* tagLen);
void streamStateParTagSeekKeyNext_rocksdb(SStreamState* pState, const int64_t groupId, SStreamStateCur* pCur); void streamStateParTagSeekKeyNext_rocksdb(SStreamState* pState, const int64_t groupId, SStreamStateCur* pCur);
int32_t streamStateParTagGetKVByCur_rocksdb(SStreamStateCur* pCur, int64_t* pGroupId, const void** pVal, int32_t* pVLen); int32_t streamStateParTagGetKVByCur_rocksdb(SStreamStateCur* pCur, int64_t* pGroupId, const void** pVal,
int32_t* pVLen);
// parname cf // parname cf
int32_t streamStatePutParName_rocksdb(SStreamState* pState, int64_t groupId, const char tbname[TSDB_TABLE_NAME_LEN]); int32_t streamStatePutParName_rocksdb(SStreamState* pState, int64_t groupId, const char tbname[TSDB_TABLE_NAME_LEN]);
int32_t streamStateGetParName_rocksdb(SStreamState* pState, int64_t groupId, void** pVal); int32_t streamStateGetParName_rocksdb(SStreamState* pState, int64_t groupId, void** pVal);
void streamStateDestroy_rocksdb(SStreamState* pState, bool remove);
// default cf // default cf
int32_t streamDefaultPut_rocksdb(SStreamState* pState, const void* key, void* pVal, int32_t pVLen); int32_t streamDefaultPut_rocksdb(SStreamState* pState, const void* key, void* pVal, int32_t pVLen);
int32_t streamDefaultGet_rocksdb(SStreamState* pState, const void* key, void** pVal, int32_t* pVLen); int32_t streamDefaultGet_rocksdb(SStreamState* pState, const void* key, void** pVal, int32_t* pVLen);

View File

@ -153,7 +153,6 @@ void taskDbUnRefChkp(STaskDbWrapper* pTaskDb, int64_t chkp);
int32_t chkpAddExtraInfo(char* pChkpIdDir, int64_t chkpId, int64_t processId); int32_t chkpAddExtraInfo(char* pChkpIdDir, int64_t chkpId, int64_t processId);
int32_t chkpLoadExtraInfo(char* pChkpIdDir, int64_t* chkpId, int64_t* processId); int32_t chkpLoadExtraInfo(char* pChkpIdDir, int64_t* chkpId, int64_t* processId);
#define GEN_COLUMN_FAMILY_NAME(name, idstr, SUFFIX) sprintf(name, "%s_%s", idstr, (SUFFIX));
int32_t copyFiles(const char* src, const char* dst); int32_t copyFiles(const char* src, const char* dst);
uint32_t nextPow2(uint32_t x); uint32_t nextPow2(uint32_t x);
@ -1156,13 +1155,17 @@ int32_t chkpMayDelObsolete(void* arg, int64_t chkpId, char* path) {
taosArrayDestroy(pBackend->chkpSaved); taosArrayDestroy(pBackend->chkpSaved);
pBackend->chkpSaved = chkpDup; pBackend->chkpSaved = chkpDup;
chkpDup = NULL;
TAOS_UNUSED(taosThreadRwlockUnlock(&pBackend->chkpDirLock)); TAOS_UNUSED(taosThreadRwlockUnlock(&pBackend->chkpDirLock));
for (int i = 0; i < taosArrayGetSize(chkpDel); i++) { for (int i = 0; i < taosArrayGetSize(chkpDel); i++) {
int64_t id = *(int64_t*)taosArrayGet(chkpDel, i); int64_t id = *(int64_t*)taosArrayGet(chkpDel, i);
char tbuf[256] = {0}; char tbuf[256] = {0};
sprintf(tbuf, "%s%scheckpoint%" PRId64 "", path, TD_DIRSEP, id); if (snprintf(tbuf, sizeof(tbuf), "%s%scheckpoint%" PRId64 "", path, TD_DIRSEP, id) >= sizeof(tbuf)) {
code = TSDB_CODE_OUT_OF_RANGE;
TAOS_CHECK_GOTO(code, NULL, _exception);
}
stInfo("backend remove obsolete checkpoint: %s", tbuf); stInfo("backend remove obsolete checkpoint: %s", tbuf);
if (taosIsDir(tbuf)) { if (taosIsDir(tbuf)) {
@ -1187,12 +1190,17 @@ int chkpIdComp(const void* a, const void* b) {
} }
int32_t taskDbLoadChkpInfo(STaskDbWrapper* pBackend) { int32_t taskDbLoadChkpInfo(STaskDbWrapper* pBackend) {
int32_t code = 0; int32_t code = 0;
char* pChkpDir = taosMemoryCalloc(1, 256); int32_t nBytes = 0;
int32_t cap = 256;
char* pChkpDir = taosMemoryCalloc(1, cap);
if (pChkpDir == NULL) { if (pChkpDir == NULL) {
return terrno; return terrno;
} }
sprintf(pChkpDir, "%s%s%s", pBackend->path, TD_DIRSEP, "checkpoints"); nBytes = snprintf(pChkpDir, cap, "%s%s%s", pBackend->path, TD_DIRSEP, "checkpoints");
if (nBytes >= cap) {
return TSDB_CODE_OUT_OF_RANGE;
}
if (!taosIsDir(pChkpDir)) { if (!taosIsDir(pChkpDir)) {
taosMemoryFree(pChkpDir); taosMemoryFree(pChkpDir);
return 0; return 0;
@ -1413,12 +1421,18 @@ int32_t taskDbDestroySnap(void* arg, SArray* pSnapInfo) {
if (pSnapInfo == NULL) return 0; if (pSnapInfo == NULL) return 0;
SStreamMeta* pMeta = arg; SStreamMeta* pMeta = arg;
int32_t code = 0; int32_t code = 0;
int32_t cap = 256;
int32_t nBytes = 0;
streamMutexLock(&pMeta->backendMutex); streamMutexLock(&pMeta->backendMutex);
char buf[128] = {0}; char buf[256] = {0};
for (int i = 0; i < taosArrayGetSize(pSnapInfo); i++) { for (int i = 0; i < taosArrayGetSize(pSnapInfo); i++) {
SStreamTaskSnap* pSnap = taosArrayGet(pSnapInfo, i); SStreamTaskSnap* pSnap = taosArrayGet(pSnapInfo, i);
sprintf(buf, "0x%" PRIx64 "-0x%x", pSnap->streamId, (int32_t)pSnap->taskId); nBytes = snprintf(buf, cap, "0x%" PRIx64 "-0x%x", pSnap->streamId, (int32_t)pSnap->taskId);
if (nBytes <= 0 || nBytes >= cap) {
code = TSDB_CODE_OUT_OF_RANGE;
break;
}
STaskDbWrapper** pTaskDb = taosHashGet(pMeta->pTaskDbUnique, buf, strlen(buf)); STaskDbWrapper** pTaskDb = taosHashGet(pMeta->pTaskDbUnique, buf, strlen(buf));
if (pTaskDb == NULL || *pTaskDb == NULL) { if (pTaskDb == NULL || *pTaskDb == NULL) {
stWarn("stream backend:%p failed to find task db, streamId:% " PRId64 "", pMeta, pSnap->streamId); stWarn("stream backend:%p failed to find task db, streamId:% " PRId64 "", pMeta, pSnap->streamId);
@ -1430,7 +1444,7 @@ int32_t taskDbDestroySnap(void* arg, SArray* pSnapInfo) {
taskDbUnRefChkp(*pTaskDb, pSnap->chkpId); taskDbUnRefChkp(*pTaskDb, pSnap->chkpId);
} }
streamMutexUnlock(&pMeta->backendMutex); streamMutexUnlock(&pMeta->backendMutex);
return 0; return code;
} }
#ifdef BUILD_NO_CALL #ifdef BUILD_NO_CALL
int32_t streamBackendAddInUseChkp(void* arg, int64_t chkpId) { int32_t streamBackendAddInUseChkp(void* arg, int64_t chkpId) {
@ -1685,9 +1699,6 @@ void streamBackendDelCompare(void* backend, void* arg) {
taosMemoryFree(node); taosMemoryFree(node);
} }
} }
#ifdef BUILD_NO_CALL
void streamStateDestroy_rocksdb(SStreamState* pState, bool remove) { streamStateCloseBackend(pState, remove); }
#endif
void destroyRocksdbCfInst(RocksdbCfInst* inst) { void destroyRocksdbCfInst(RocksdbCfInst* inst) {
int cfLen = sizeof(ginitDict) / sizeof(ginitDict[0]); int cfLen = sizeof(ginitDict) / sizeof(ginitDict[0]);
if (inst->pHandle) { if (inst->pHandle) {
@ -1712,7 +1723,7 @@ void destroyRocksdbCfInst(RocksdbCfInst* inst) {
} }
// |key|-----value------| // |key|-----value------|
// |key|ttl|len|userData| // |key|ttl|len|userData
int defaultKeyComp(void* state, const char* aBuf, size_t aLen, const char* bBuf, size_t bLen) { int defaultKeyComp(void* state, const char* aBuf, size_t aLen, const char* bBuf, size_t bLen) {
int len = aLen < bLen ? aLen : bLen; int len = aLen < bLen ? aLen : bLen;
@ -2447,43 +2458,50 @@ void taskDbDestroyChkpOpt(STaskDbWrapper* pTaskDb) {
int32_t taskDbBuildFullPath(char* path, char* key, char** dbFullPath, char** stateFullPath) { int32_t taskDbBuildFullPath(char* path, char* key, char** dbFullPath, char** stateFullPath) {
int32_t code = 0; int32_t code = 0;
char* statePath = taosMemoryCalloc(1, strlen(path) + 128); int32_t cap = strlen(path) + 128, nBytes = 0;
char* statePath = NULL;
char* dbPath = NULL;
statePath = taosMemoryCalloc(1, cap);
if (statePath == NULL) { if (statePath == NULL) {
return terrno; TAOS_CHECK_GOTO(terrno, NULL, _err);
}
nBytes = snprintf(statePath, cap, "%s%s%s", path, TD_DIRSEP, key);
if (nBytes < 0 || nBytes >= cap) {
code = TSDB_CODE_OUT_OF_RANGE;
TAOS_CHECK_GOTO(code, NULL, _err);
} }
sprintf(statePath, "%s%s%s", path, TD_DIRSEP, key);
if (!taosDirExist(statePath)) { if (!taosDirExist(statePath)) {
code = taosMulMkDir(statePath); code = taosMulMkDir(statePath);
if (code != 0) { TAOS_CHECK_GOTO(code, NULL, _err);
code = TAOS_SYSTEM_ERROR(errno);
stError("failed to create dir: %s, reason:%s", statePath, tstrerror(code));
taosMemoryFree(statePath);
return code;
}
} }
char* dbPath = taosMemoryCalloc(1, strlen(statePath) + 128); dbPath = taosMemoryCalloc(1, cap);
if (dbPath == NULL) { if (dbPath == NULL) {
taosMemoryFree(statePath); TAOS_CHECK_GOTO(terrno, NULL, _err);
return terrno; }
nBytes = snprintf(dbPath, cap, "%s%s%s", statePath, TD_DIRSEP, "state");
if (nBytes < 0 || nBytes >= cap) {
code = TSDB_CODE_OUT_OF_RANGE;
TAOS_CHECK_GOTO(code, NULL, _err);
} }
sprintf(dbPath, "%s%s%s", statePath, TD_DIRSEP, "state");
if (!taosDirExist(dbPath)) { if (!taosDirExist(dbPath)) {
code = taosMulMkDir(dbPath); code = taosMulMkDir(dbPath);
if (code != 0) { TAOS_CHECK_GOTO(code, NULL, _err);
code = TAOS_SYSTEM_ERROR(errno);
stError("failed to create dir: %s, reason:%s", dbPath, tstrerror(code));
taosMemoryFree(statePath);
taosMemoryFree(dbPath);
return code;
}
} }
*dbFullPath = dbPath; *dbFullPath = dbPath;
*stateFullPath = statePath; *stateFullPath = statePath;
return 0; return 0;
_err:
stError("failed to create dir: %s, reason:%s", dbPath, tstrerror(code));
taosMemoryFree(statePath);
taosMemoryFree(dbPath);
return code;
} }
void taskDbUpdateChkpId(void* pTaskDb, int64_t chkpId) { void taskDbUpdateChkpId(void* pTaskDb, int64_t chkpId) {
@ -2864,6 +2882,7 @@ int32_t streamStateOpenBackendCf(void* backend, char* name, char** cfs, int32_t
int64_t streamId; int64_t streamId;
int32_t taskId, dummy = 0; int32_t taskId, dummy = 0;
char suffix[64] = {0}; char suffix[64] = {0};
int32_t code = 0;
rocksdb_options_t** cfOpts = taosMemoryCalloc(nCf, sizeof(rocksdb_options_t*)); rocksdb_options_t** cfOpts = taosMemoryCalloc(nCf, sizeof(rocksdb_options_t*));
RocksdbCfParam* params = taosMemoryCalloc(nCf, sizeof(RocksdbCfParam)); RocksdbCfParam* params = taosMemoryCalloc(nCf, sizeof(RocksdbCfParam));
@ -2873,6 +2892,7 @@ int32_t streamStateOpenBackendCf(void* backend, char* name, char** cfs, int32_t
for (int i = 0; i < nCf; i++) { for (int i = 0; i < nCf; i++) {
char* cf = cfs[i]; char* cf = cfs[i];
char funcname[64] = {0}; char funcname[64] = {0};
cfOpts[i] = rocksdb_options_create_copy(handle->dbOpt); cfOpts[i] = rocksdb_options_create_copy(handle->dbOpt);
if (i == 0) continue; if (i == 0) continue;
if (3 == sscanf(cf, "0x%" PRIx64 "-%d_%s", &streamId, &taskId, funcname)) { if (3 == sscanf(cf, "0x%" PRIx64 "-%d_%s", &streamId, &taskId, funcname)) {
@ -2909,7 +2929,7 @@ int32_t streamStateOpenBackendCf(void* backend, char* name, char** cfs, int32_t
taosMemoryFree(params); taosMemoryFree(params);
taosMemoryFree(cfOpts); taosMemoryFree(cfOpts);
// fix other leak // fix other leak
return -1; return TSDB_CODE_THIRDPARTY_ERROR;
} else { } else {
stDebug("succ to open rocksdb cf"); stDebug("succ to open rocksdb cf");
} }
@ -2929,8 +2949,13 @@ int32_t streamStateOpenBackendCf(void* backend, char* name, char** cfs, int32_t
char funcname[64] = {0}; char funcname[64] = {0};
if (3 == sscanf(cf, "0x%" PRIx64 "-%d_%s", &streamId, &taskId, funcname)) { if (3 == sscanf(cf, "0x%" PRIx64 "-%d_%s", &streamId, &taskId, funcname)) {
char idstr[128] = {0}; char idstr[128] = {0};
sprintf(idstr, "0x%" PRIx64 "-%d", streamId, taskId); int32_t nBytes = snprintf(idstr, sizeof(idstr), "0x%" PRIx64 "-%d", streamId, taskId);
if (nBytes <= 0 || nBytes >= sizeof(idstr)) {
code = TSDB_CODE_OUT_OF_RANGE;
stError("failed to open cf since %s", tstrerror(code));
return code;
}
int idx = streamStateGetCfIdx(NULL, funcname); int idx = streamStateGetCfIdx(NULL, funcname);
@ -2997,117 +3022,7 @@ int32_t streamStateOpenBackendCf(void* backend, char* name, char** cfs, int32_t
taosMemoryFree(cfOpts); taosMemoryFree(cfOpts);
return 0; return 0;
} }
#ifdef BUILD_NO_CALL
int streamStateOpenBackend(void* backend, SStreamState* pState) {
taosAcquireRef(streamBackendId, pState->streamBackendRid);
SBackendWrapper* handle = backend;
SBackendCfWrapper* pBackendCfWrapper = taosMemoryCalloc(1, sizeof(SBackendCfWrapper));
streamMutexLock(&handle->cfMutex);
RocksdbCfInst** ppInst = taosHashGet(handle->cfInst, pState->pTdbState->idstr, strlen(pState->pTdbState->idstr) + 1);
if (ppInst != NULL && *ppInst != NULL) {
RocksdbCfInst* inst = *ppInst;
pBackendCfWrapper->rocksdb = inst->db;
pBackendCfWrapper->pHandle = (void**)inst->pHandle;
pBackendCfWrapper->writeOpts = inst->wOpt;
pBackendCfWrapper->readOpts = inst->rOpt;
pBackendCfWrapper->cfOpts = (void**)(inst->cfOpt);
pBackendCfWrapper->dbOpt = handle->dbOpt;
pBackendCfWrapper->param = inst->param;
pBackendCfWrapper->pBackend = handle;
pBackendCfWrapper->pComparNode = inst->pCompareNode;
streamMutexUnlock(&handle->cfMutex);
pBackendCfWrapper->backendId = pState->streamBackendRid;
memcpy(pBackendCfWrapper->idstr, pState->pTdbState->idstr, sizeof(pState->pTdbState->idstr));
int64_t id = taosAddRef(streamBackendCfWrapperId, pBackendCfWrapper);
pState->pTdbState->backendCfWrapperId = id;
pState->pTdbState->pBackendCfWrapper = pBackendCfWrapper;
stInfo("succ to open state %p on backendWrapper, %p, %s", pState, pBackendCfWrapper, pBackendCfWrapper->idstr);
inst->pHandle = NULL;
inst->cfOpt = NULL;
inst->param = NULL;
inst->wOpt = NULL;
inst->rOpt = NULL;
return 0;
}
streamMutexUnlock(&handle->cfMutex);
char* err = NULL;
int cfLen = sizeof(ginitDict) / sizeof(ginitDict[0]);
RocksdbCfParam* param = taosMemoryCalloc(cfLen, sizeof(RocksdbCfParam));
const rocksdb_options_t** cfOpt = taosMemoryCalloc(cfLen, sizeof(rocksdb_options_t*));
for (int i = 0; i < cfLen; i++) {
cfOpt[i] = rocksdb_options_create_copy(handle->dbOpt);
// refactor later
rocksdb_block_based_table_options_t* tableOpt = rocksdb_block_based_options_create();
rocksdb_block_based_options_set_block_cache(tableOpt, handle->cache);
rocksdb_block_based_options_set_partition_filters(tableOpt, 1);
rocksdb_filterpolicy_t* filter = rocksdb_filterpolicy_create_bloom(15);
rocksdb_block_based_options_set_filter_policy(tableOpt, filter);
rocksdb_options_set_block_based_table_factory((rocksdb_options_t*)cfOpt[i], tableOpt);
param[i].tableOpt = tableOpt;
};
rocksdb_comparator_t** pCompare = taosMemoryCalloc(cfLen, sizeof(rocksdb_comparator_t*));
for (int i = 0; i < cfLen; i++) {
SCfInit* cf = &ginitDict[i];
rocksdb_comparator_t* compare = rocksdb_comparator_create(NULL, cf->destroyCmp, cf->cmpKey, cf->cmpName);
rocksdb_options_set_comparator((rocksdb_options_t*)cfOpt[i], compare);
pCompare[i] = compare;
}
rocksdb_column_family_handle_t** cfHandle = taosMemoryCalloc(cfLen, sizeof(rocksdb_column_family_handle_t*));
pBackendCfWrapper->rocksdb = handle->db;
pBackendCfWrapper->pHandle = (void**)cfHandle;
pBackendCfWrapper->writeOpts = rocksdb_writeoptions_create();
pBackendCfWrapper->readOpts = rocksdb_readoptions_create();
pBackendCfWrapper->cfOpts = (void**)cfOpt;
pBackendCfWrapper->dbOpt = handle->dbOpt;
pBackendCfWrapper->param = param;
pBackendCfWrapper->pBackend = handle;
pBackendCfWrapper->backendId = pState->streamBackendRid;
taosThreadRwlockInit(&pBackendCfWrapper->rwLock, NULL);
SCfComparator compare = {.comp = pCompare, .numOfComp = cfLen};
pBackendCfWrapper->pComparNode = streamBackendAddCompare(handle, &compare);
rocksdb_writeoptions_disable_WAL(pBackendCfWrapper->writeOpts, 1);
memcpy(pBackendCfWrapper->idstr, pState->pTdbState->idstr, sizeof(pState->pTdbState->idstr));
int64_t id = taosAddRef(streamBackendCfWrapperId, pBackendCfWrapper);
pState->pTdbState->backendCfWrapperId = id;
pState->pTdbState->pBackendCfWrapper = pBackendCfWrapper;
stInfo("succ to open state %p on backendWrapper %p %s", pState, pBackendCfWrapper, pBackendCfWrapper->idstr);
return 0;
}
void streamStateCloseBackend(SStreamState* pState, bool remove) {
SBackendCfWrapper* wrapper = pState->pTdbState->pBackendCfWrapper;
SBackendWrapper* pHandle = wrapper->pBackend;
stInfo("start to close state on backend: %p", pHandle);
streamMutexLock(&pHandle->cfMutex);
RocksdbCfInst** ppInst = taosHashGet(pHandle->cfInst, wrapper->idstr, strlen(pState->pTdbState->idstr) + 1);
if (ppInst != NULL && *ppInst != NULL) {
RocksdbCfInst* inst = *ppInst;
taosMemoryFree(inst);
taosHashRemove(pHandle->cfInst, pState->pTdbState->idstr, strlen(pState->pTdbState->idstr) + 1);
}
streamMutexUnlock(&pHandle->cfMutex);
char* status[] = {"close", "drop"};
stInfo("start to %s state %p on backendWrapper %p %s", status[remove == false ? 0 : 1], pState, wrapper,
wrapper->idstr);
wrapper->remove |= remove; // update by other pState
taosReleaseRef(streamBackendCfWrapperId, pState->pTdbState->backendCfWrapperId);
}
#endif
void streamStateDestroyCompar(void* arg) { void streamStateDestroyCompar(void* arg) {
SCfComparator* comp = (SCfComparator*)arg; SCfComparator* comp = (SCfComparator*)arg;
for (int i = 0; i < comp->numOfComp; i++) { for (int i = 0; i < comp->numOfComp; i++) {
@ -3386,7 +3301,8 @@ int32_t streamStateGetFirst_rocksdb(SStreamState* pState, SWinKey* key) {
return streamStateDel_rocksdb(pState, &tmp); return streamStateDel_rocksdb(pState, &tmp);
} }
int32_t streamStateFillGetGroupKVByCur_rocksdb(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen) { int32_t streamStateFillGetGroupKVByCur_rocksdb(SStreamStateCur* pCur, SWinKey* pKey, const void** pVal,
int32_t* pVLen) {
if (!pCur) { if (!pCur) {
return -1; return -1;
} }
@ -4343,7 +4259,7 @@ int32_t streamStatePutParTag_rocksdb(SStreamState* pState, int64_t groupId, cons
void streamStateParTagSeekKeyNext_rocksdb(SStreamState* pState, const int64_t groupId, SStreamStateCur* pCur) { void streamStateParTagSeekKeyNext_rocksdb(SStreamState* pState, const int64_t groupId, SStreamStateCur* pCur) {
if (pCur == NULL) { if (pCur == NULL) {
return ; return;
} }
STaskDbWrapper* wrapper = pState->pTdbState->pOwner->pBackend; STaskDbWrapper* wrapper = pState->pTdbState->pOwner->pBackend;
pCur->number = pState->number; pCur->number = pState->number;
@ -4353,13 +4269,13 @@ void streamStateParTagSeekKeyNext_rocksdb(SStreamState* pState, const int64_t gr
int i = streamStateGetCfIdx(pState, "partag"); int i = streamStateGetCfIdx(pState, "partag");
if (i < 0) { if (i < 0) {
stError("streamState failed to put to cf name:%s", "partag"); stError("streamState failed to put to cf name:%s", "partag");
return ; return;
} }
char buf[128] = {0}; char buf[128] = {0};
int32_t klen = ginitDict[i].enFunc((void*)&groupId, buf); int32_t klen = ginitDict[i].enFunc((void*)&groupId, buf);
if (!streamStateIterSeekAndValid(pCur->iter, buf, klen)) { if (!streamStateIterSeekAndValid(pCur->iter, buf, klen)) {
return ; return;
} }
// skip ttl expired data // skip ttl expired data
while (rocksdb_iter_valid(pCur->iter) && iterValueIsStale(pCur->iter)) { while (rocksdb_iter_valid(pCur->iter) && iterValueIsStale(pCur->iter)) {
@ -4371,13 +4287,15 @@ void streamStateParTagSeekKeyNext_rocksdb(SStreamState* pState, const int64_t gr
size_t kLen = 0; size_t kLen = 0;
char* keyStr = (char*)rocksdb_iter_key(pCur->iter, &kLen); char* keyStr = (char*)rocksdb_iter_key(pCur->iter, &kLen);
TAOS_UNUSED(parKeyDecode((void*)&curGroupId, keyStr)); TAOS_UNUSED(parKeyDecode((void*)&curGroupId, keyStr));
if (curGroupId > groupId) return ; if (curGroupId > groupId) return;
rocksdb_iter_next(pCur->iter); rocksdb_iter_next(pCur->iter);
} }
} }
int32_t streamStateParTagGetKVByCur_rocksdb(SStreamStateCur* pCur, int64_t* pGroupId, const void** pVal, int32_t* pVLen) { int32_t streamStateParTagGetKVByCur_rocksdb(SStreamStateCur* pCur, int64_t* pGroupId, const void** pVal,
int32_t* pVLen) {
stDebug("streamStateFillGetKVByCur_rocksdb");
if (!pCur) { if (!pCur) {
return -1; return -1;
} }
@ -4713,7 +4631,7 @@ int32_t compareHashTableImpl(SHashObj* p1, SHashObj* p2, SArray* diff) {
if (fname == NULL) { if (fname == NULL) {
return terrno; return terrno;
} }
TAOS_UNUSED(strncpy(fname, name, len)); tstrncpy(fname, name, strlen(name));
if (taosArrayPush(diff, &fname) == NULL) { if (taosArrayPush(diff, &fname) == NULL) {
taosMemoryFree(fname); taosMemoryFree(fname);
return terrno; return terrno;
@ -4737,17 +4655,32 @@ int32_t compareHashTable(SHashObj* p1, SHashObj* p2, SArray* add, SArray* del) {
void hashTableToDebug(SHashObj* pTbl, char** buf) { void hashTableToDebug(SHashObj* pTbl, char** buf) {
size_t sz = taosHashGetSize(pTbl); size_t sz = taosHashGetSize(pTbl);
int32_t total = 0; int32_t total = 0;
char* p = taosMemoryCalloc(1, sz * 16 + 4); int32_t cap = sz * 16 + 4;
void* pIter = taosHashIterate(pTbl, NULL);
char* p = taosMemoryCalloc(1, cap);
if (p == NULL) {
stError("failed to alloc memory for stream snapshot debug info");
return;
}
void* pIter = taosHashIterate(pTbl, NULL);
while (pIter) { while (pIter) {
size_t len = 0; size_t len = 0;
char* name = taosHashGetKey(pIter, &len); char* name = taosHashGetKey(pIter, &len);
char* tname = taosMemoryCalloc(1, len + 1); if (name == NULL || len <= 0) {
memcpy(tname, name, len); pIter = taosHashIterate(pTbl, pIter);
total += sprintf(p + total, "%s,", tname); continue;
}
int32_t left = cap - strlen(p);
int32_t nBytes = snprintf(p + total, left, "%s,", name);
if (nBytes <= 0 || nBytes >= left) {
stError("failed to debug snapshot info since %s", tstrerror(TSDB_CODE_OUT_OF_RANGE));
taosMemoryFree(p);
return;
}
pIter = taosHashIterate(pTbl, pIter); pIter = taosHashIterate(pTbl, pIter);
taosMemoryFree(tname); total += nBytes;
} }
if (total > 0) { if (total > 0) {
p[total - 1] = 0; p[total - 1] = 0;
@ -4758,13 +4691,30 @@ void strArrayDebugInfo(SArray* pArr, char** buf) {
int32_t sz = taosArrayGetSize(pArr); int32_t sz = taosArrayGetSize(pArr);
if (sz <= 0) return; if (sz <= 0) return;
char* p = (char*)taosMemoryCalloc(1, 64 + sz * 64); int32_t code = 0;
int32_t total = 0; int32_t total = 0, nBytes = 0;
int32_t cap = 64 + sz * 64;
char* p = (char*)taosMemoryCalloc(1, cap);
if (p == NULL) {
stError("failed to alloc memory for stream snapshot debug info");
return;
}
for (int i = 0; i < sz; i++) { for (int i = 0; i < sz; i++) {
char* name = taosArrayGetP(pArr, i); char* name = taosArrayGetP(pArr, i);
total += sprintf(p + total, "%s,", name); int32_t left = cap - strlen(p);
nBytes = snprintf(p + total, left, "%s,", name);
if (nBytes <= 0 || nBytes >= left) {
code = TSDB_CODE_OUT_OF_RANGE;
stError("failed to debug snapshot info since %s", tstrerror(code));
taosMemoryFree(p);
return;
}
total += nBytes;
} }
p[total - 1] = 0; p[total - 1] = 0;
*buf = p; *buf = p;
@ -4774,16 +4724,16 @@ void dbChkpDebugInfo(SDbChkp* pDb) {
char* p[4] = {NULL}; char* p[4] = {NULL};
hashTableToDebug(pDb->pSstTbl[pDb->idx], &p[0]); hashTableToDebug(pDb->pSstTbl[pDb->idx], &p[0]);
stTrace("chkp previous file: [%s]", p[0]); if (p[0]) stTrace("chkp previous file: [%s]", p[0]);
hashTableToDebug(pDb->pSstTbl[1 - pDb->idx], &p[1]); hashTableToDebug(pDb->pSstTbl[1 - pDb->idx], &p[1]);
stTrace("chkp curr file: [%s]", p[1]); if (p[1]) stTrace("chkp curr file: [%s]", p[1]);
strArrayDebugInfo(pDb->pAdd, &p[2]); strArrayDebugInfo(pDb->pAdd, &p[2]);
stTrace("chkp newly addded file: [%s]", p[2]); if (p[2]) stTrace("chkp newly addded file: [%s]", p[2]);
strArrayDebugInfo(pDb->pDel, &p[3]); strArrayDebugInfo(pDb->pDel, &p[3]);
stTrace("chkp newly deleted file: [%s]", p[3]); if (p[3]) stTrace("chkp newly deleted file: [%s]", p[3]);
for (int i = 0; i < 4; i++) { for (int i = 0; i < 4; i++) {
taosMemoryFree(p[i]); taosMemoryFree(p[i]);
@ -4875,7 +4825,7 @@ int32_t dbChkpGetDelta(SDbChkp* p, int64_t chkpId, SArray* list) {
return terrno; return terrno;
} }
TAOS_UNUSED(strncpy(fname, name, len)); tstrncpy(fname, name, strlen(name));
if (taosArrayPush(p->pAdd, &fname) == NULL) { if (taosArrayPush(p->pAdd, &fname) == NULL) {
taosMemoryFree(fname); taosMemoryFree(fname);
TAOS_UNUSED(taosThreadRwlockUnlock(&p->rwLock)); TAOS_UNUSED(taosThreadRwlockUnlock(&p->rwLock));
@ -5350,7 +5300,8 @@ SStreamStateCur* streamStateSeekKeyPrev_rocksdb(SStreamState* pState, const SWin
return NULL; return NULL;
} }
int32_t streamStateGetGroupKVByCur_rocksdb(SStreamState* pState, SStreamStateCur* pCur, SWinKey* pKey, const void** pVal, int32_t* pVLen) { int32_t streamStateGetGroupKVByCur_rocksdb(SStreamState* pState, SStreamStateCur* pCur, SWinKey* pKey,
const void** pVal, int32_t* pVLen) {
if (!pCur) { if (!pCur) {
return -1; return -1;
} }

View File

@ -99,7 +99,7 @@ int32_t metaRefMgtInit() {
void metaRefMgtCleanup() { void metaRefMgtCleanup() {
void* pIter = taosHashIterate(gMetaRefMgt.pTable, NULL); void* pIter = taosHashIterate(gMetaRefMgt.pTable, NULL);
while (pIter) { while (pIter) {
int64_t* p = *(int64_t**) pIter; int64_t* p = *(int64_t**)pIter;
taosMemoryFree(p); taosMemoryFree(p);
pIter = taosHashIterate(gMetaRefMgt.pTable, pIter); pIter = taosHashIterate(gMetaRefMgt.pTable, pIter);
} }
@ -118,14 +118,14 @@ int32_t metaRefMgtAdd(int64_t vgId, int64_t* rid) {
if (p == NULL) { if (p == NULL) {
code = taosHashPut(gMetaRefMgt.pTable, &rid, sizeof(rid), &rid, sizeof(void*)); code = taosHashPut(gMetaRefMgt.pTable, &rid, sizeof(rid), &rid, sizeof(void*));
if (code) { if (code) {
stError("vgId:%d failed to put into refId mgt, refId:%" PRId64" %p, code:%s", (int32_t)vgId, *rid, rid, stError("vgId:%d failed to put into refId mgt, refId:%" PRId64 " %p, code:%s", (int32_t)vgId, *rid, rid,
tstrerror(code)); tstrerror(code));
return code; return code;
} else { // not } else { // not
// stInfo("add refId:%"PRId64" vgId:%d, %p", *rid, (int32_t)vgId, rid); // stInfo("add refId:%"PRId64" vgId:%d, %p", *rid, (int32_t)vgId, rid);
} }
} else { } else {
stFatal("try to add refId:%"PRId64" vgId:%d, %p that already added into mgt", *rid, (int32_t) vgId, rid); stFatal("try to add refId:%" PRId64 " vgId:%d, %p that already added into mgt", *rid, (int32_t)vgId, rid);
} }
streamMutexUnlock(&gMetaRefMgt.mutex); streamMutexUnlock(&gMetaRefMgt.mutex);
@ -292,6 +292,7 @@ int32_t streamTaskSetDb(SStreamMeta* pMeta, SStreamTask* pTask, const char* key)
void* p = taskDbAddRef(*ppBackend); void* p = taskDbAddRef(*ppBackend);
if (p == NULL) { if (p == NULL) {
stError("s-task:0x%x failed to ref backend", pTask->id.taskId); stError("s-task:0x%x failed to ref backend", pTask->id.taskId);
streamMutexUnlock(&pMeta->backendMutex);
return TSDB_CODE_FAILED; return TSDB_CODE_FAILED;
} }
@ -669,7 +670,7 @@ int32_t streamMetaSaveTask(SStreamMeta* pMeta, SStreamTask* pTask) {
int64_t refId = pTask->id.refId; int64_t refId = pTask->id.refId;
int32_t ret = taosRemoveRef(streamTaskRefPool, pTask->id.refId); int32_t ret = taosRemoveRef(streamTaskRefPool, pTask->id.refId);
if (ret != 0) { if (ret != 0) {
stError("s-task:0x%x failed to remove ref, refId:%"PRId64, (int32_t) id[1], refId); stError("s-task:0x%x failed to remove ref, refId:%" PRId64, (int32_t)id[1], refId);
} }
} else { } else {
stDebug("s-task:%s vgId:%d refId:%" PRId64 " task meta save to disk", pTask->id.idStr, vgId, pTask->id.refId); stDebug("s-task:%s vgId:%d refId:%" PRId64 " task meta save to disk", pTask->id.idStr, vgId, pTask->id.refId);
@ -727,14 +728,14 @@ int32_t streamMetaRegisterTask(SStreamMeta* pMeta, int64_t ver, SStreamTask* pTa
int32_t ret = taosRemoveRef(streamTaskRefPool, refId); int32_t ret = taosRemoveRef(streamTaskRefPool, refId);
if (ret != 0) { if (ret != 0) {
stError("s-task:0x%x failed to remove ref, refId:%"PRId64, (int32_t) id.taskId, refId); stError("s-task:0x%x failed to remove ref, refId:%" PRId64, (int32_t)id.taskId, refId);
} }
return code; return code;
} }
if ((code = streamMetaSaveTask(pMeta, pTask)) != 0) { if ((code = streamMetaSaveTask(pMeta, pTask)) != 0) {
int32_t unused = taosHashRemove(pMeta->pTasksMap, &id, sizeof(id)); int32_t unused = taosHashRemove(pMeta->pTasksMap, &id, sizeof(id));
void* pUnused = taosArrayPop(pMeta->pTaskList); void* pUnused = taosArrayPop(pMeta->pTaskList);
int32_t ret = taosRemoveRef(streamTaskRefPool, refId); int32_t ret = taosRemoveRef(streamTaskRefPool, refId);
if (ret) { if (ret) {
@ -745,7 +746,7 @@ int32_t streamMetaRegisterTask(SStreamMeta* pMeta, int64_t ver, SStreamTask* pTa
if ((code = streamMetaCommit(pMeta)) != 0) { if ((code = streamMetaCommit(pMeta)) != 0) {
int32_t unused = taosHashRemove(pMeta->pTasksMap, &id, sizeof(id)); int32_t unused = taosHashRemove(pMeta->pTasksMap, &id, sizeof(id));
void* pUnused = taosArrayPop(pMeta->pTaskList); void* pUnused = taosArrayPop(pMeta->pTaskList);
int32_t ret = taosRemoveRef(streamTaskRefPool, refId); int32_t ret = taosRemoveRef(streamTaskRefPool, refId);
if (ret) { if (ret) {
@ -783,7 +784,7 @@ int32_t streamMetaAcquireTaskNoLock(SStreamMeta* pMeta, int64_t streamId, int32_
SStreamTask* p = taosAcquireRef(streamTaskRefPool, *pTaskRefId); SStreamTask* p = taosAcquireRef(streamTaskRefPool, *pTaskRefId);
if (p == NULL) { if (p == NULL) {
stDebug("s-task:%x failed to acquire task refId:%"PRId64", may have been destoried", taskId, *pTaskRefId); stDebug("s-task:%x failed to acquire task refId:%" PRId64 ", may have been destoried", taskId, *pTaskRefId);
return TSDB_CODE_STREAM_TASK_NOT_EXIST; return TSDB_CODE_STREAM_TASK_NOT_EXIST;
} }
@ -946,11 +947,10 @@ int32_t streamMetaUnregisterTask(SStreamMeta* pMeta, int64_t streamId, int32_t t
pTask->info.delaySchedParam = 0; pTask->info.delaySchedParam = 0;
} }
int64_t refId = pTask->id.refId; int64_t refId = pTask->id.refId;
int32_t ret = taosRemoveRef(streamTaskRefPool, refId); int32_t ret = taosRemoveRef(streamTaskRefPool, refId);
if (ret != 0) { if (ret != 0) {
stError("s-task:0x%x failed to remove ref, refId:%"PRId64, (int32_t) id.taskId, refId); stError("s-task:0x%x failed to remove ref, refId:%" PRId64, (int32_t)id.taskId, refId);
} }
streamMetaReleaseTask(pMeta, pTask); streamMetaReleaseTask(pMeta, pTask);
@ -1115,7 +1115,7 @@ void streamMetaLoadAllTasks(SStreamMeta* pMeta) {
STaskId id = streamTaskGetTaskId(pTask); STaskId id = streamTaskGetTaskId(pTask);
tFreeStreamTask(pTask); tFreeStreamTask(pTask);
void* px = taosArrayPush(pRecycleList, &id); void* px = taosArrayPush(pRecycleList, &id);
if (px == NULL) { if (px == NULL) {
stError("s-task:0x%x failed record the task into recycle list due to out of memory", taskId); stError("s-task:0x%x failed record the task into recycle list due to out of memory", taskId);
} }
@ -1165,7 +1165,7 @@ void streamMetaLoadAllTasks(SStreamMeta* pMeta) {
continue; continue;
} }
stInfo("s-task:0x%x vgId:%d set refId:%"PRId64, (int32_t) id.taskId, vgId, pTask->id.refId); stInfo("s-task:0x%x vgId:%d set refId:%" PRId64, (int32_t)id.taskId, vgId, pTask->id.refId);
if (pTask->info.fillHistory == 0) { if (pTask->info.fillHistory == 0) {
int32_t val = atomic_add_fetch_32(&pMeta->numOfStreamTasks, 1); int32_t val = atomic_add_fetch_32(&pMeta->numOfStreamTasks, 1);
} }

View File

@ -120,7 +120,8 @@ SStreamState* streamStateOpen(const char* path, void* pTask, int64_t streamId, i
SStreamTask* pStreamTask = pTask; SStreamTask* pStreamTask = pTask;
pState->streamId = streamId; pState->streamId = streamId;
pState->taskId = taskId; pState->taskId = taskId;
TAOS_UNUSED(tsnprintf(pState->pTdbState->idstr, sizeof(pState->pTdbState->idstr), "0x%" PRIx64 "-0x%x", pState->streamId, pState->taskId)); TAOS_UNUSED(tsnprintf(pState->pTdbState->idstr, sizeof(pState->pTdbState->idstr), "0x%" PRIx64 "-0x%x",
pState->streamId, pState->taskId));
code = streamTaskSetDb(pStreamTask->pMeta, pTask, pState->pTdbState->idstr); code = streamTaskSetDb(pStreamTask->pMeta, pTask, pState->pTdbState->idstr);
QUERY_CHECK_CODE(code, lino, _end); QUERY_CHECK_CODE(code, lino, _end);
@ -527,7 +528,6 @@ _end:
void streamStateDestroy(SStreamState* pState, bool remove) { void streamStateDestroy(SStreamState* pState, bool remove) {
streamFileStateDestroy(pState->pFileState); streamFileStateDestroy(pState->pFileState);
// streamStateDestroy_rocksdb(pState, remove);
tSimpleHashCleanup(pState->parNameMap); tSimpleHashCleanup(pState->parNameMap);
// do nothong // do nothong
taosMemoryFreeClear(pState->pTdbState); taosMemoryFreeClear(pState->pTdbState);
@ -572,7 +572,8 @@ int32_t streamStateCountWinAddIfNotExist(SStreamState* pState, SSessionKey* pKey
return getCountWinResultBuff(pState->pFileState, pKey, winCount, ppVal, pVLen, pWinCode); return getCountWinResultBuff(pState->pFileState, pKey, winCount, ppVal, pVLen, pWinCode);
} }
int32_t streamStateCountWinAdd(SStreamState* pState, SSessionKey* pKey, COUNT_TYPE winCount, void** pVal, int32_t* pVLen) { int32_t streamStateCountWinAdd(SStreamState* pState, SSessionKey* pKey, COUNT_TYPE winCount, void** pVal,
int32_t* pVLen) {
return createCountWinResultBuff(pState->pFileState, pKey, winCount, pVal, pVLen); return createCountWinResultBuff(pState->pFileState, pKey, winCount, pVal, pVLen);
} }
@ -593,9 +594,7 @@ SStreamStateCur* streamStateGroupGetCur(SStreamState* pState) {
return pCur; return pCur;
} }
void streamStateGroupCurNext(SStreamStateCur* pCur) { void streamStateGroupCurNext(SStreamStateCur* pCur) { streamFileStateGroupCurNext(pCur); }
streamFileStateGroupCurNext(pCur);
}
int32_t streamStateGroupGetKVByCur(SStreamStateCur* pCur, int64_t* pKey, void** pVal, int32_t* pVLen) { int32_t streamStateGroupGetKVByCur(SStreamStateCur* pCur, int64_t* pKey, void** pVal, int32_t* pVLen) {
if (pVal != NULL) { if (pVal != NULL) {
@ -604,13 +603,9 @@ int32_t streamStateGroupGetKVByCur(SStreamStateCur* pCur, int64_t* pKey, void**
return streamFileStateGroupGetKVByCur(pCur, pKey, pVal, pVLen); return streamFileStateGroupGetKVByCur(pCur, pKey, pVal, pVLen);
} }
void streamStateClearExpiredState(SStreamState* pState) { void streamStateClearExpiredState(SStreamState* pState) { clearExpiredState(pState->pFileState); }
clearExpiredState(pState->pFileState);
}
void streamStateSetFillInfo(SStreamState* pState) { void streamStateSetFillInfo(SStreamState* pState) { setFillInfo(pState->pFileState); }
setFillInfo(pState->pFileState);
}
int32_t streamStateGetPrev(SStreamState* pState, const SWinKey* pKey, SWinKey* pResKey, void** pVal, int32_t* pVLen, int32_t streamStateGetPrev(SStreamState* pState, const SWinKey* pKey, SWinKey* pResKey, void** pVal, int32_t* pVLen,
int32_t* pWinCode) { int32_t* pWinCode) {

View File

@ -2901,7 +2901,7 @@ void syncNodeLogConfigInfo(SSyncNode* ths, SSyncCfg* cfg, char* str) {
n += tsnprintf(buf + n, len - n, "%s", "{"); n += tsnprintf(buf + n, len - n, "%s", "{");
for (int i = 0; i < ths->peersEpset->numOfEps; i++) { for (int i = 0; i < ths->peersEpset->numOfEps; i++) {
n += tsnprintf(buf + n, len - n, "%s:%d%s", ths->peersEpset->eps[i].fqdn, ths->peersEpset->eps[i].port, n += tsnprintf(buf + n, len - n, "%s:%d%s", ths->peersEpset->eps[i].fqdn, ths->peersEpset->eps[i].port,
(i + 1 < ths->peersEpset->numOfEps ? ", " : "")); (i + 1 < ths->peersEpset->numOfEps ? ", " : ""));
} }
n += tsnprintf(buf + n, len - n, "%s", "}"); n += tsnprintf(buf + n, len - n, "%s", "}");

View File

@ -52,7 +52,7 @@ int32_t tdbOpen(const char *dbname, int32_t szPage, int32_t pages, TDB **ppDb, i
pDb->encryptAlgorithm = encryptAlgorithm; pDb->encryptAlgorithm = encryptAlgorithm;
if (encryptKey != NULL) { if (encryptKey != NULL) {
strncpy(pDb->encryptKey, encryptKey, ENCRYPT_KEY_LEN); tstrncpy(pDb->encryptKey, encryptKey, ENCRYPT_KEY_LEN + 1);
} }
ret = tdbPCacheOpen(szPage, pages, &(pDb->pCache)); ret = tdbPCacheOpen(szPage, pages, &(pDb->pCache));

View File

@ -459,7 +459,7 @@ static char *tdbEncryptPage(SPager *pPager, char *pPageData, int32_t pageSize, c
opts.source = pPageData + count; opts.source = pPageData + count;
opts.result = packetData; opts.result = packetData;
opts.unitLen = 128; opts.unitLen = 128;
strncpy(opts.key, encryptKey, ENCRYPT_KEY_LEN); tstrncpy(opts.key, encryptKey, ENCRYPT_KEY_LEN + 1);
int32_t newLen = CBC_Encrypt(&opts); int32_t newLen = CBC_Encrypt(&opts);
@ -927,7 +927,7 @@ static int tdbPagerInitPage(SPager *pPager, SPage *pPage, int (*initPage)(SPage
opts.source = pPage->pData + count; opts.source = pPage->pData + count;
opts.result = packetData; opts.result = packetData;
opts.unitLen = 128; opts.unitLen = 128;
strncpy(opts.key, encryptKey, ENCRYPT_KEY_LEN); tstrncpy(opts.key, encryptKey, ENCRYPT_KEY_LEN + 1);
int newLen = CBC_Decrypt(&opts); int newLen = CBC_Decrypt(&opts);

View File

@ -200,8 +200,8 @@ bool tfsIsSameFile(const STfsFile *pFile1, const STfsFile *pFile2) {
if (pFile1->did.level != pFile2->did.level) return false; if (pFile1->did.level != pFile2->did.level) return false;
if (pFile1->did.id != pFile2->did.id) return false; if (pFile1->did.id != pFile2->did.id) return false;
char nameBuf1[TMPNAME_LEN], nameBuf2[TMPNAME_LEN]; char nameBuf1[TMPNAME_LEN], nameBuf2[TMPNAME_LEN];
(void)strncpy(nameBuf1, pFile1->rname, TMPNAME_LEN); tstrncpy(nameBuf1, pFile1->rname, TMPNAME_LEN);
(void)strncpy(nameBuf2, pFile2->rname, TMPNAME_LEN); tstrncpy(nameBuf2, pFile2->rname, TMPNAME_LEN);
nameBuf1[TMPNAME_LEN - 1] = 0; nameBuf1[TMPNAME_LEN - 1] = 0;
nameBuf2[TMPNAME_LEN - 1] = 0; nameBuf2[TMPNAME_LEN - 1] = 0;
TAOS_UNUSED(taosRealPath(nameBuf1, NULL, TMPNAME_LEN)); TAOS_UNUSED(taosRealPath(nameBuf1, NULL, TMPNAME_LEN));
@ -573,7 +573,7 @@ static int32_t tfsCheckAndFormatCfg(STfs *pTfs, SDiskCfg *pCfg) {
TAOS_RETURN(TSDB_CODE_FS_INVLD_CFG); TAOS_RETURN(TSDB_CODE_FS_INVLD_CFG);
} }
strncpy(pCfg->dir, dirName, TSDB_FILENAME_LEN); tstrncpy(pCfg->dir, dirName, TSDB_FILENAME_LEN);
TAOS_RETURN(0); TAOS_RETURN(0);
} }

View File

@ -638,7 +638,7 @@ static FORCE_INLINE int32_t walWriteImpl(SWal *pWal, int64_t index, tmsg_t msgTy
opts.source = newBody; opts.source = newBody;
opts.result = newBodyEncrypted; opts.result = newBodyEncrypted;
opts.unitLen = 16; opts.unitLen = 16;
TAOS_UNUSED(strncpy((char *)opts.key, pWal->cfg.encryptKey, ENCRYPT_KEY_LEN)); tstrncpy((char *)opts.key, pWal->cfg.encryptKey, ENCRYPT_KEY_LEN + 1);
int32_t count = CBC_Encrypt(&opts); int32_t count = CBC_Encrypt(&opts);

View File

@ -120,33 +120,20 @@ int32_t taosStr2int64(const char *str, int64_t *val) {
if (str == NULL || val == NULL) { if (str == NULL || val == NULL) {
return TSDB_CODE_INVALID_PARA; return TSDB_CODE_INVALID_PARA;
} }
errno = 0;
char *endptr = NULL; char *endptr = NULL;
int64_t ret = strtoll(str, &endptr, 10); int64_t ret = strtoll(str, &endptr, 10);
if (errno == ERANGE && (ret == LLONG_MAX || ret == LLONG_MIN)) { if (errno != 0) {
return TAOS_SYSTEM_ERROR(errno); return TAOS_SYSTEM_ERROR(errno);
} else if (errno == EINVAL && ret == 0) {
return TSDB_CODE_INVALID_PARA;
} else { } else {
if (endptr == str) {
return TSDB_CODE_INVALID_PARA;
}
*val = ret; *val = ret;
return 0; return 0;
} }
} }
int32_t taosStr2int16(const char *str, int16_t *val) {
OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val);
int64_t tmp = 0;
int32_t code = taosStr2int64(str, &tmp);
if (code) {
return code;
} else if (tmp > INT16_MAX || tmp < INT16_MIN) {
return TAOS_SYSTEM_ERROR(ERANGE);
} else {
*val = (int16_t)tmp;
return 0;
}
}
int32_t taosStr2int32(const char *str, int32_t *val) { int32_t taosStr2int32(const char *str, int32_t *val) {
OS_PARAM_CHECK(str); OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val); OS_PARAM_CHECK(val);
@ -161,6 +148,20 @@ int32_t taosStr2int32(const char *str, int32_t *val) {
return 0; return 0;
} }
} }
int32_t taosStr2int16(const char *str, int16_t *val) {
OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val);
int64_t tmp = 0;
int32_t code = taosStr2int64(str, &tmp);
if (code) {
return code;
} else if (tmp > INT16_MAX || tmp < INT16_MIN) {
return TAOS_SYSTEM_ERROR(ERANGE);
} else {
*val = (int16_t)tmp;
return 0;
}
}
int32_t taosStr2int8(const char *str, int8_t *val) { int32_t taosStr2int8(const char *str, int8_t *val) {
OS_PARAM_CHECK(str); OS_PARAM_CHECK(str);
@ -177,6 +178,70 @@ int32_t taosStr2int8(const char *str, int8_t *val) {
} }
} }
int32_t taosStr2Uint64(const char *str, uint64_t *val) {
if (str == NULL || val == NULL) {
return TSDB_CODE_INVALID_PARA;
}
char *endptr = NULL;
errno = 0;
uint64_t ret = strtoull(str, &endptr, 10);
if (errno != 0) {
return TAOS_SYSTEM_ERROR(errno);
} else {
if (endptr == str) {
return TSDB_CODE_INVALID_PARA;
}
*val = ret;
return 0;
}
}
int32_t taosStr2Uint32(const char *str, uint32_t *val) {
OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val);
uint64_t tmp = 0;
int32_t code = taosStr2Uint64(str, &tmp);
if (code) {
return code;
} else if (tmp > UINT32_MAX) {
return TAOS_SYSTEM_ERROR(ERANGE);
} else {
*val = (int32_t)tmp;
return 0;
}
}
int32_t taosStr2Uint16(const char *str, uint16_t *val) {
OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val);
uint64_t tmp = 0;
int32_t code = taosStr2Uint64(str, &tmp);
if (code) {
return code;
} else if (tmp > UINT16_MAX) {
return TAOS_SYSTEM_ERROR(ERANGE);
} else {
*val = (int16_t)tmp;
return 0;
}
}
int32_t taosStr2Uint8(const char *str, uint8_t *val) {
OS_PARAM_CHECK(str);
OS_PARAM_CHECK(val);
uint64_t tmp = 0;
int32_t code = taosStr2Uint64(str, &tmp);
if (code) {
return code;
} else if (tmp > UINT8_MAX) {
return TAOS_SYSTEM_ERROR(ERANGE);
} else {
*val = (int8_t)tmp;
return 0;
}
}
int32_t tasoUcs4Compare(TdUcs4 *f1_ucs4, TdUcs4 *f2_ucs4, int32_t bytes) { int32_t tasoUcs4Compare(TdUcs4 *f1_ucs4, TdUcs4 *f2_ucs4, int32_t bytes) {
if ((f1_ucs4 == NULL || f2_ucs4 == NULL)) { if ((f1_ucs4 == NULL || f2_ucs4 == NULL)) {
return TSDB_CODE_INVALID_PARA; return TSDB_CODE_INVALID_PARA;
@ -223,7 +288,7 @@ int32_t tasoUcs4Copy(TdUcs4 *target_ucs4, TdUcs4 *source_ucs4, int32_t len_ucs4)
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return terrno; return terrno;
} }
(void)memcpy(target_ucs4, source_ucs4, len_ucs4 * sizeof(TdUcs4)); (void)memcpy(target_ucs4, source_ucs4, len_ucs4 * sizeof(TdUcs4));
return TSDB_CODE_SUCCESS; return TSDB_CODE_SUCCESS;
@ -289,7 +354,7 @@ void taosConvDestroy() {
} }
iconv_t taosAcquireConv(int32_t *idx, ConvType type) { iconv_t taosAcquireConv(int32_t *idx, ConvType type) {
if(idx == NULL) { if (idx == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return (iconv_t)-1; return (iconv_t)-1;
} }
@ -356,7 +421,7 @@ bool taosMbsToUcs4(const char *mbs, size_t mbsLength, TdUcs4 *ucs4, int32_t ucs4
if (ucs4_max_len == 0) { if (ucs4_max_len == 0) {
return true; return true;
} }
if(ucs4_max_len < 0 || mbs == NULL || ucs4 == NULL) { if (ucs4_max_len < 0 || mbs == NULL || ucs4 == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return false; return false;
} }
@ -372,9 +437,9 @@ bool taosMbsToUcs4(const char *mbs, size_t mbsLength, TdUcs4 *ucs4, int32_t ucs4
if ((iconv_t)-1 == conv) { if ((iconv_t)-1 == conv) {
return false; return false;
} }
size_t ucs4_input_len = mbsLength; size_t ucs4_input_len = mbsLength;
size_t outLeft = ucs4_max_len; size_t outLeft = ucs4_max_len;
if (iconv(conv, (char **)&mbs, &ucs4_input_len, (char **)&ucs4, &outLeft) == -1) { if (iconv(conv, (char **)&mbs, &ucs4_input_len, (char **)&ucs4, &outLeft) == -1) {
terrno = TAOS_SYSTEM_ERROR(errno); terrno = TAOS_SYSTEM_ERROR(errno);
taosReleaseConv(idx, conv, M2C); taosReleaseConv(idx, conv, M2C);
@ -401,7 +466,7 @@ int32_t taosUcs4ToMbs(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs) {
if (ucs4_max_len == 0) { if (ucs4_max_len == 0) {
return 0; return 0;
} }
if(ucs4_max_len < 0 || ucs4 == NULL || mbs == NULL) { if (ucs4_max_len < 0 || ucs4 == NULL || mbs == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return terrno; return terrno;
} }
@ -417,18 +482,18 @@ int32_t taosUcs4ToMbs(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs) {
if ((iconv_t)-1 == conv) { if ((iconv_t)-1 == conv) {
return terrno; return terrno;
} }
size_t ucs4_input_len = ucs4_max_len; size_t ucs4_input_len = ucs4_max_len;
size_t outLen = ucs4_max_len; size_t outLen = ucs4_max_len;
if (iconv(conv, (char **)&ucs4, &ucs4_input_len, &mbs, &outLen) == -1) { if (iconv(conv, (char **)&ucs4, &ucs4_input_len, &mbs, &outLen) == -1) {
code = TAOS_SYSTEM_ERROR(errno); code = TAOS_SYSTEM_ERROR(errno);
taosReleaseConv(idx, conv, C2M); taosReleaseConv(idx, conv, C2M);
terrno = code; terrno = code;
return code; return code;
} }
taosReleaseConv(idx, conv, C2M); taosReleaseConv(idx, conv, C2M);
return (int32_t)(ucs4_max_len - outLen); return (int32_t)(ucs4_max_len - outLen);
#endif #endif
} }
@ -439,7 +504,7 @@ int32_t taosUcs4ToMbsEx(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs, iconv_t c
if (ucs4_max_len == 0) { if (ucs4_max_len == 0) {
return 0; return 0;
} }
if(ucs4_max_len < 0 || ucs4 == NULL || mbs == NULL) { if (ucs4_max_len < 0 || ucs4 == NULL || mbs == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return terrno; return terrno;
} }
@ -455,13 +520,13 @@ int32_t taosUcs4ToMbsEx(TdUcs4 *ucs4, int32_t ucs4_max_len, char *mbs, iconv_t c
terrno = TAOS_SYSTEM_ERROR(errno); terrno = TAOS_SYSTEM_ERROR(errno);
return terrno; return terrno;
} }
return (int32_t)(ucs4_max_len - outLen); return (int32_t)(ucs4_max_len - outLen);
#endif #endif
} }
bool taosValidateEncodec(const char *encodec) { bool taosValidateEncodec(const char *encodec) {
if(encodec == NULL) { if (encodec == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return false; return false;
} }
@ -513,7 +578,7 @@ int32_t taosHexEncode(const unsigned char *src, char *dst, int32_t len, int32_t
} }
int32_t taosHexDecode(const char *src, char *dst, int32_t len) { int32_t taosHexDecode(const char *src, char *dst, int32_t len) {
if(!src || !dst || len <= 0) { if (!src || !dst || len <= 0) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return terrno; return terrno;
} }
@ -556,9 +621,10 @@ int32_t taosMbsToWchars(TdWchar *pWchars, const char *pStrs, int32_t size) {
return mbstowcs(pWchars, pStrs, size); return mbstowcs(pWchars, pStrs, size);
} }
int32_t taosWcharToMb(char *pStr, TdWchar wchar) { int32_t taosWcharToMb(char *pStr, TdWchar wchar) {
OS_PARAM_CHECK(pStr); OS_PARAM_CHECK(pStr);
return wctomb(pStr, wchar); } return wctomb(pStr, wchar);
}
char *taosStrCaseStr(const char *str, const char *pattern) { char *taosStrCaseStr(const char *str, const char *pattern) {
if (str == NULL) { if (str == NULL) {
@ -580,7 +646,7 @@ char *taosStrCaseStr(const char *str, const char *pattern) {
} }
int64_t taosStr2Int64(const char *str, char **pEnd, int32_t radix) { int64_t taosStr2Int64(const char *str, char **pEnd, int32_t radix) {
if(str == NULL) { if (str == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return 0; return 0;
} }
@ -592,7 +658,7 @@ int64_t taosStr2Int64(const char *str, char **pEnd, int32_t radix) {
} }
uint64_t taosStr2UInt64(const char *str, char **pEnd, int32_t radix) { uint64_t taosStr2UInt64(const char *str, char **pEnd, int32_t radix) {
if(str == NULL) { if (str == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return 0; return 0;
} }
@ -604,7 +670,7 @@ uint64_t taosStr2UInt64(const char *str, char **pEnd, int32_t radix) {
} }
int32_t taosStr2Int32(const char *str, char **pEnd, int32_t radix) { int32_t taosStr2Int32(const char *str, char **pEnd, int32_t radix) {
if(str == NULL) { if (str == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return 0; return 0;
} }
@ -616,7 +682,7 @@ int32_t taosStr2Int32(const char *str, char **pEnd, int32_t radix) {
} }
uint32_t taosStr2UInt32(const char *str, char **pEnd, int32_t radix) { uint32_t taosStr2UInt32(const char *str, char **pEnd, int32_t radix) {
if(str == NULL) { if (str == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return 0; return 0;
} }
@ -628,7 +694,7 @@ uint32_t taosStr2UInt32(const char *str, char **pEnd, int32_t radix) {
} }
int16_t taosStr2Int16(const char *str, char **pEnd, int32_t radix) { int16_t taosStr2Int16(const char *str, char **pEnd, int32_t radix) {
if(str == NULL) { if (str == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return 0; return 0;
} }
@ -640,7 +706,7 @@ int16_t taosStr2Int16(const char *str, char **pEnd, int32_t radix) {
} }
uint16_t taosStr2UInt16(const char *str, char **pEnd, int32_t radix) { uint16_t taosStr2UInt16(const char *str, char **pEnd, int32_t radix) {
if(str == NULL) { if (str == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return 0; return 0;
} }
@ -652,7 +718,7 @@ uint16_t taosStr2UInt16(const char *str, char **pEnd, int32_t radix) {
} }
int8_t taosStr2Int8(const char *str, char **pEnd, int32_t radix) { int8_t taosStr2Int8(const char *str, char **pEnd, int32_t radix) {
if(str == NULL) { if (str == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return 0; return 0;
} }
@ -661,7 +727,7 @@ int8_t taosStr2Int8(const char *str, char **pEnd, int32_t radix) {
} }
uint8_t taosStr2UInt8(const char *str, char **pEnd, int32_t radix) { uint8_t taosStr2UInt8(const char *str, char **pEnd, int32_t radix) {
if(str == NULL) { if (str == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return 0; return 0;
} }
@ -673,7 +739,7 @@ uint8_t taosStr2UInt8(const char *str, char **pEnd, int32_t radix) {
} }
double taosStr2Double(const char *str, char **pEnd) { double taosStr2Double(const char *str, char **pEnd) {
if(str == NULL) { if (str == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return 0; return 0;
} }
@ -682,7 +748,7 @@ double taosStr2Double(const char *str, char **pEnd) {
} }
float taosStr2Float(const char *str, char **pEnd) { float taosStr2Float(const char *str, char **pEnd) {
if(str == NULL) { if (str == NULL) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return 0; return 0;
} }
@ -698,7 +764,7 @@ bool isHex(const char *z, uint32_t n) {
} }
bool isValidateHex(const char *z, uint32_t n) { bool isValidateHex(const char *z, uint32_t n) {
if(!z) { if (!z) {
terrno = TSDB_CODE_INVALID_PARA; terrno = TSDB_CODE_INVALID_PARA;
return false; return false;
} }
@ -724,12 +790,12 @@ int32_t taosHex2Ascii(const char *z, uint32_t n, void **data, uint32_t *size) {
} }
return 0; return 0;
} }
uint8_t *tmp = (uint8_t *)taosMemoryCalloc(*size, 1); uint8_t *tmp = (uint8_t *)taosMemoryCalloc(*size, 1);
if (tmp == NULL) { if (tmp == NULL) {
return terrno; return terrno;
} }
int8_t num = 0; int8_t num = 0;
uint8_t *byte = tmp + *size - 1; uint8_t *byte = tmp + *size - 1;
@ -749,7 +815,7 @@ int32_t taosHex2Ascii(const char *z, uint32_t n, void **data, uint32_t *size) {
} }
} }
*data = tmp; *data = tmp;
return 0; return 0;
} }
@ -825,7 +891,7 @@ int32_t taosAscii2Hex(const char *z, uint32_t n, void **data, uint32_t *size) {
if (tmp == NULL) { if (tmp == NULL) {
return terrno; return terrno;
} }
*data = tmp; *data = tmp;
*(tmp++) = '\\'; *(tmp++) = '\\';
*(tmp++) = 'x'; *(tmp++) = 'x';
@ -834,7 +900,7 @@ int32_t taosAscii2Hex(const char *z, uint32_t n, void **data, uint32_t *size) {
tmp[i * 2] = valueOf(val >> 4); tmp[i * 2] = valueOf(val >> 4);
tmp[i * 2 + 1] = valueOf(val & 0x0F); tmp[i * 2 + 1] = valueOf(val & 0x0F);
} }
return 0; return 0;
} }

View File

@ -154,3 +154,483 @@ TEST(osStringTests, ostsnprintfTests) {
EXPECT_EQ(ret, 11); EXPECT_EQ(ret, 11);
EXPECT_STREQ(buffer, "Float: 3.14"); EXPECT_STREQ(buffer, "Float: 3.14");
} }
TEST(osStringTests, osStr2Int64) {
int64_t val;
int32_t result;
// 测试空指针输入
result = taosStr2int64(NULL, &val);
assert(result == TSDB_CODE_INVALID_PARA);
result = taosStr2int64("123", NULL);
ASSERT_NE(result, 0);
// 测试无效输入
result = taosStr2int64("abc", &val);
ASSERT_NE(result, 0);
result = taosStr2int64("", &val);
ASSERT_NE(result, 0);
char large_num[50];
snprintf(large_num, sizeof(large_num), "%lld", LLONG_MAX);
result = taosStr2int64(large_num, &val);
assert(result == 0);
assert(val == LLONG_MAX);
snprintf(large_num, sizeof(large_num), "%lld", LLONG_MIN);
result = taosStr2int64(large_num, &val);
assert(result == 0);
assert(val == LLONG_MIN);
result = taosStr2int64("123abc", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
result = taosStr2int64("abc123", &val);
ASSERT_NE(result, 0);
// 测试有效的整数字符串
result = taosStr2int64("12345", &val);
assert(result == 0);
assert(val == 12345);
result = taosStr2int64("-12345", &val);
assert(result == 0);
assert(val == -12345);
result = taosStr2int64("0", &val);
assert(result == 0);
assert(val == 0);
// 测试带空格的字符串
result = taosStr2int64(" 12345", &val);
assert(result == 0);
assert(val == 12345);
result = taosStr2int64("12345 ", &val);
assert(result == 0);
assert(val == 12345);
}
TEST(osStringTests, osStr2int32) {
int32_t val;
int32_t result;
// 测试空指针输入
result = taosStr2int32(NULL, &val);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
result = taosStr2int32("123", NULL);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
// 测试无效输入
result = taosStr2int32("abc", &val);
ASSERT_NE(result, 0);
result = taosStr2int32("", &val);
ASSERT_NE(result, 0);
// 测试超出范围的值
char large_num[50];
snprintf(large_num, sizeof(large_num), "%d", INT_MAX);
result = taosStr2int32(large_num, &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, INT_MAX);
snprintf(large_num, sizeof(large_num), "%d", INT_MIN);
result = taosStr2int32(large_num, &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, INT_MIN);
// 测试大于 INT32 范围的值
snprintf(large_num, sizeof(large_num), "%lld", (long long)INT_MAX + 1);
result = taosStr2int32(large_num, &val);
ASSERT_EQ(result, TAOS_SYSTEM_ERROR(ERANGE));
snprintf(large_num, sizeof(large_num), "%lld", (long long)INT_MIN - 1);
result = taosStr2int32(large_num, &val);
ASSERT_EQ(result, TAOS_SYSTEM_ERROR(ERANGE));
result = taosStr2int32("123abc", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
result = taosStr2int32("abc123", &val);
ASSERT_NE(result, 0);
// 测试有效的整数字符串
result = taosStr2int32("12345", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
result = taosStr2int32("-12345", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, -12345);
result = taosStr2int32("0", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 0);
// 测试带空格的字符串
result = taosStr2int32(" 12345", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
result = taosStr2int32("12345 ", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
}
TEST(osStringTests, taosStr2int16) {
int16_t val;
int32_t result;
// 测试空指针输入
result = taosStr2int16(NULL, &val);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
result = taosStr2int16("123", NULL);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
// 测试无效输入
result = taosStr2int16("abc", &val);
ASSERT_NE(result, 0);
result = taosStr2int16("", &val);
ASSERT_NE(result, 0);
// 测试超出范围的值
char large_num[50];
snprintf(large_num, sizeof(large_num), "%d", INT16_MAX);
result = taosStr2int16(large_num, &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, INT16_MAX);
snprintf(large_num, sizeof(large_num), "%d", INT16_MIN);
result = taosStr2int16(large_num, &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, INT16_MIN);
// 测试大于 INT16 范围的值
snprintf(large_num, sizeof(large_num), "%lld", (long long)INT16_MAX + 1);
result = taosStr2int16(large_num, &val);
ASSERT_EQ(result, TAOS_SYSTEM_ERROR(ERANGE));
snprintf(large_num, sizeof(large_num), "%lld", (long long)INT16_MIN - 1);
result = taosStr2int16(large_num, &val);
ASSERT_EQ(result, TAOS_SYSTEM_ERROR(ERANGE));
result = taosStr2int16("123abc", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
result = taosStr2int16("abc123", &val);
ASSERT_NE(result, 0);
// 测试有效的整数字符串
result = taosStr2int16("12345", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
result = taosStr2int16("-12345", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, -12345);
result = taosStr2int16("0", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 0);
// 测试带空格的字符串
result = taosStr2int16(" 12345", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
result = taosStr2int16("12345 ", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
}
TEST(osStringTests, taosStr2int8) {
int8_t val;
int32_t result;
// 测试空指针输入
result = taosStr2int8(NULL, &val);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
result = taosStr2int8("123", NULL);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
// 测试无效输入
result = taosStr2int8("abc", &val);
ASSERT_NE(result, 0);
result = taosStr2int8("", &val);
ASSERT_NE(result, 0);
// 测试超出范围的值
char large_num[50];
snprintf(large_num, sizeof(large_num), "%d", INT8_MAX);
result = taosStr2int8(large_num, &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, INT8_MAX);
snprintf(large_num, sizeof(large_num), "%d", INT8_MIN);
result = taosStr2int8(large_num, &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, INT8_MIN);
// 测试大于 INT8 范围的值
snprintf(large_num, sizeof(large_num), "%lld", (long long)INT8_MAX + 1);
result = taosStr2int8(large_num, &val);
ASSERT_EQ(result, TAOS_SYSTEM_ERROR(ERANGE));
snprintf(large_num, sizeof(large_num), "%lld", (long long)INT8_MIN - 1);
result = taosStr2int8(large_num, &val);
ASSERT_EQ(result, TAOS_SYSTEM_ERROR(ERANGE));
result = taosStr2int8("123abc", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
result = taosStr2int8("abc123", &val);
ASSERT_NE(result, 0);
// 测试有效的整数字符串
result = taosStr2int8("123", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
result = taosStr2int8("-123", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, -123);
result = taosStr2int8("0", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 0);
// 测试带空格的字符串
result = taosStr2int8(" 123", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
result = taosStr2int8("123 ", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
}
TEST(osStringTests, osStr2Uint64) {
uint64_t val;
int32_t result;
// 测试空指针输入
result = taosStr2Uint64(NULL, &val);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
result = taosStr2Uint64("123", NULL);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
// 测试无效输入
result = taosStr2Uint64("abc", &val);
ASSERT_NE(result, 0);
result = taosStr2Uint64("", &val);
ASSERT_NE(result, 0);
char large_num[50];
snprintf(large_num, sizeof(large_num), "%llu", ULLONG_MAX);
result = taosStr2Uint64(large_num, &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, ULLONG_MAX);
result = taosStr2Uint64("123abc", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
result = taosStr2Uint64("abc123", &val);
ASSERT_NE(result, 0);
// 测试有效的整数字符串
result = taosStr2Uint64("12345", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
result = taosStr2Uint64("0", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 0);
// 测试带空格的字符串
result = taosStr2Uint64(" 12345", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
result = taosStr2Uint64("12345 ", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
}
TEST(osStringTests, taosStr2Uint32) {
uint32_t val;
int32_t result;
// 测试空指针输入
result = taosStr2Uint32(NULL, &val);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
result = taosStr2Uint32("123", NULL);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
// 测试无效输入
result = taosStr2Uint32("abc", &val);
ASSERT_NE(result, 0);
result = taosStr2Uint32("", &val);
ASSERT_NE(result, 0);
// 测试超出范围的值
char large_num[50];
snprintf(large_num, sizeof(large_num), "%u", UINT32_MAX);
result = taosStr2Uint32(large_num, &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, UINT32_MAX);
// 测试大于 UINT32 范围的值
snprintf(large_num, sizeof(large_num), "%llu", (unsigned long long)UINT32_MAX + 1);
result = taosStr2Uint32(large_num, &val);
ASSERT_EQ(result, TAOS_SYSTEM_ERROR(ERANGE));
result = taosStr2Uint32("123abc", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
result = taosStr2Uint32("abc123", &val);
ASSERT_NE(result, 0);
// 测试有效的整数字符串
result = taosStr2Uint32("12345", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
result = taosStr2Uint32("0", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 0);
// 测试带空格的字符串
result = taosStr2Uint32(" 12345", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
result = taosStr2Uint32("12345 ", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
}
TEST(osStringTests, taosStr2Uint16) {
uint16_t val;
int32_t result;
// 测试空指针输入
result = taosStr2Uint16(NULL, &val);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
result = taosStr2Uint16("123", NULL);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
// 测试无效输入
result = taosStr2Uint16("abc", &val);
ASSERT_NE(result, 0);
result = taosStr2Uint16("", &val);
ASSERT_NE(result, 0);
// 测试超出范围的值
char large_num[50];
snprintf(large_num, sizeof(large_num), "%u", UINT16_MAX);
result = taosStr2Uint16(large_num, &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, UINT16_MAX);
// 测试大于 UINT16 范围的值
snprintf(large_num, sizeof(large_num), "%llu", (unsigned long long)UINT16_MAX + 1);
result = taosStr2Uint16(large_num, &val);
ASSERT_EQ(result, TAOS_SYSTEM_ERROR(ERANGE));
result = taosStr2Uint16("123abc", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
result = taosStr2Uint16("abc123", &val);
ASSERT_NE(result, 0);
// 测试有效的整数字符串
result = taosStr2Uint16("12345", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
result = taosStr2Uint16("0", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 0);
// 测试带空格的字符串
result = taosStr2Uint16(" 12345", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
result = taosStr2Uint16("12345 ", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 12345);
}
TEST(osStringTests, taosStr2Uint8) {
uint8_t val;
int32_t result;
// 测试空指针输入
result = taosStr2Uint8(NULL, &val);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
result = taosStr2Uint8("123", NULL);
ASSERT_EQ(result, TSDB_CODE_INVALID_PARA);
// 测试无效输入
result = taosStr2Uint8("abc", &val);
ASSERT_NE(result, 0);
result = taosStr2Uint8("", &val);
ASSERT_NE(result, 0);
// 测试超出范围的值
char large_num[50];
snprintf(large_num, sizeof(large_num), "%u", UINT8_MAX);
result = taosStr2Uint8(large_num, &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, UINT8_MAX);
// 测试大于 UINT8 范围的值
snprintf(large_num, sizeof(large_num), "%llu", (unsigned long long)UINT8_MAX + 1);
result = taosStr2Uint8(large_num, &val);
ASSERT_EQ(result, TAOS_SYSTEM_ERROR(ERANGE));
result = taosStr2Uint8("123abc", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
result = taosStr2Uint8("abc123", &val);
ASSERT_NE(result, 0);
// 测试有效的整数字符串
result = taosStr2Uint8("123", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
result = taosStr2Uint8("0", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 0);
// 测试带空格的字符串
result = taosStr2Uint8(" 123", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
result = taosStr2Uint8("123 ", &val);
ASSERT_EQ(result, 0);
ASSERT_EQ(val, 123);
}

View File

@ -151,11 +151,13 @@ static int32_t cfgCheckAndSetDir(SConfigItem *pItem, const char *inputDir) {
} }
static int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) { static int32_t cfgSetBool(SConfigItem *pItem, const char *value, ECfgSrcType stype) {
bool tmp = false; int32_t code = 0;
bool tmp = false;
if (strcasecmp(value, "true") == 0) { if (strcasecmp(value, "true") == 0) {
tmp = true; tmp = true;
} }
if (atoi(value) > 0) { int32_t val = 0;
if ((code = taosStr2int32(value, &val)) == 0 && val > 0) {
tmp = true; tmp = true;
} }
@ -258,6 +260,7 @@ static int32_t cfgSetTimezone(SConfigItem *pItem, const char *value, ECfgSrcType
static int32_t cfgSetTfsItem(SConfig *pCfg, const char *name, const char *value, const char *level, const char *primary, static int32_t cfgSetTfsItem(SConfig *pCfg, const char *name, const char *value, const char *level, const char *primary,
const char *disable, ECfgSrcType stype) { const char *disable, ECfgSrcType stype) {
int32_t code = 0;
(void)taosThreadMutexLock(&pCfg->lock); (void)taosThreadMutexLock(&pCfg->lock);
SConfigItem *pItem = cfgGetItem(pCfg, name); SConfigItem *pItem = cfgGetItem(pCfg, name);
@ -278,20 +281,40 @@ static int32_t cfgSetTfsItem(SConfig *pCfg, const char *name, const char *value,
SDiskCfg cfg = {0}; SDiskCfg cfg = {0};
tstrncpy(cfg.dir, pItem->str, sizeof(cfg.dir)); tstrncpy(cfg.dir, pItem->str, sizeof(cfg.dir));
cfg.level = level ? atoi(level) : 0;
cfg.primary = primary ? atoi(primary) : 1; if (level == NULL || strlen(level) == 0) {
cfg.disable = disable ? atoi(disable) : 0; cfg.level = 0;
} else {
code = taosStr2int32(level, &cfg.level);
TAOS_CHECK_GOTO(code, NULL, _err);
}
if (primary == NULL || strlen(primary) == 0) {
cfg.primary = 1;
} else {
code = taosStr2int32(primary, &cfg.primary);
TAOS_CHECK_GOTO(code, NULL, _err);
}
if (disable == NULL || strlen(disable) == 0) {
cfg.disable = 0;
} else {
code = taosStr2int8(disable, &cfg.disable);
TAOS_CHECK_GOTO(code, NULL, _err);
}
void *ret = taosArrayPush(pItem->array, &cfg); void *ret = taosArrayPush(pItem->array, &cfg);
if (ret == NULL) { if (ret == NULL) {
(void)taosThreadMutexUnlock(&pCfg->lock); code = terrno;
TAOS_CHECK_GOTO(code, NULL, _err);
TAOS_RETURN(terrno);
} }
pItem->stype = stype; pItem->stype = stype;
(void)taosThreadMutexUnlock(&pCfg->lock); (void)taosThreadMutexUnlock(&pCfg->lock);
TAOS_RETURN(TSDB_CODE_SUCCESS); TAOS_RETURN(TSDB_CODE_SUCCESS);
_err:
(void)taosThreadMutexUnlock(&pCfg->lock);
TAOS_RETURN(code);
} }
static int32_t cfgUpdateDebugFlagItem(SConfig *pCfg, const char *name, bool resetArray) { static int32_t cfgUpdateDebugFlagItem(SConfig *pCfg, const char *name, bool resetArray) {
@ -315,7 +338,7 @@ static int32_t cfgUpdateDebugFlagItem(SConfig *pCfg, const char *name, bool rese
if (pDebugFlagItem == NULL) return -1; if (pDebugFlagItem == NULL) return -1;
if (pDebugFlagItem->array != NULL) { if (pDebugFlagItem->array != NULL) {
SLogVar logVar = {0}; SLogVar logVar = {0};
(void)strncpy(logVar.name, name, TSDB_LOG_VAR_LEN - 1); tstrncpy(logVar.name, name, TSDB_LOG_VAR_LEN);
if (NULL == taosArrayPush(pDebugFlagItem->array, &logVar)) { if (NULL == taosArrayPush(pDebugFlagItem->array, &logVar)) {
TAOS_RETURN(terrno); TAOS_RETURN(terrno);
} }
@ -412,6 +435,7 @@ void cfgLock(SConfig *pCfg) {
void cfgUnLock(SConfig *pCfg) { (void)taosThreadMutexUnlock(&pCfg->lock); } void cfgUnLock(SConfig *pCfg) { (void)taosThreadMutexUnlock(&pCfg->lock); }
int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *pVal, bool isServer) { int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *pVal, bool isServer) {
int32_t code = 0;
ECfgDynType dynType = isServer ? CFG_DYN_SERVER : CFG_DYN_CLIENT; ECfgDynType dynType = isServer ? CFG_DYN_SERVER : CFG_DYN_CLIENT;
cfgLock(pCfg); cfgLock(pCfg);
@ -443,8 +467,9 @@ int32_t cfgCheckRangeForDynUpdate(SConfig *pCfg, const char *name, const char *p
} }
} break; } break;
case CFG_DTYPE_BOOL: { case CFG_DTYPE_BOOL: {
int32_t ival = (int32_t)atoi(pVal); int32_t ival = 0;
if (ival != 0 && ival != 1) { code = taosStr2int32(pVal, &ival);
if (code != 0 || (ival != 0 && ival != 1)) {
uError("cfg:%s, type:%s value:%d out of range[0, 1]", pItem->name, cfgDtypeStr(pItem->dtype), ival); uError("cfg:%s, type:%s value:%d out of range[0, 1]", pItem->name, cfgDtypeStr(pItem->dtype), ival);
cfgUnLock(pCfg); cfgUnLock(pCfg);
TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE); TAOS_RETURN(TSDB_CODE_OUT_OF_RANGE);
@ -887,9 +912,10 @@ void cfgDumpCfg(SConfig *pCfg, bool tsc, bool dump) {
for (size_t j = 0; j < sz; ++j) { for (size_t j = 0; j < sz; ++j) {
SDiskCfg *pCfg = taosArrayGet(pItem->array, j); SDiskCfg *pCfg = taosArrayGet(pItem->array, j);
if (dump) { if (dump) {
(void)printf("%s %s %s l:%d p:%d d:%"PRIi8"\n", src, name, pCfg->dir, pCfg->level, pCfg->primary, pCfg->disable); (void)printf("%s %s %s l:%d p:%d d:%" PRIi8 "\n", src, name, pCfg->dir, pCfg->level, pCfg->primary,
pCfg->disable);
} else { } else {
uInfo("%s %s %s l:%d p:%d d:%"PRIi8, src, name, pCfg->dir, pCfg->level, pCfg->primary, pCfg->disable); uInfo("%s %s %s l:%d p:%d d:%" PRIi8, src, name, pCfg->dir, pCfg->level, pCfg->primary, pCfg->disable);
} }
} }
break; break;
@ -924,7 +950,7 @@ int32_t cfgLoadFromEnvVar(SConfig *pConfig) {
name = value = value2 = value3 = value4 = NULL; name = value = value2 = value3 = value4 = NULL;
olen = vlen = vlen2 = vlen3 = vlen4 = 0; olen = vlen = vlen2 = vlen3 = vlen4 = 0;
strncpy(line, *pEnv, sizeof(line) - 1); tstrncpy(line, *pEnv, sizeof(line));
pEnv++; pEnv++;
if (taosEnvToCfg(line, line) < 0) { if (taosEnvToCfg(line, line) < 0) {
uTrace("failed to convert env to cfg:%s", line); uTrace("failed to convert env to cfg:%s", line);
@ -969,7 +995,7 @@ int32_t cfgLoadFromEnvCmd(SConfig *pConfig, const char **envCmd) {
int32_t index = 0; int32_t index = 0;
if (envCmd == NULL) TAOS_RETURN(TSDB_CODE_SUCCESS); if (envCmd == NULL) TAOS_RETURN(TSDB_CODE_SUCCESS);
while (envCmd[index] != NULL) { while (envCmd[index] != NULL) {
strncpy(buf, envCmd[index], sizeof(buf) - 1); tstrncpy(buf, envCmd[index], sizeof(buf));
buf[sizeof(buf) - 1] = 0; buf[sizeof(buf) - 1] = 0;
if (taosEnvToCfg(buf, buf) < 0) { if (taosEnvToCfg(buf, buf) < 0) {
uTrace("failed to convert env to cfg:%s", buf); uTrace("failed to convert env to cfg:%s", buf);
@ -1420,7 +1446,7 @@ int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char *apolloUrl
char **pEnv = environ; char **pEnv = environ;
line[1023] = 0; line[1023] = 0;
while (*pEnv != NULL) { while (*pEnv != NULL) {
strncpy(line, *pEnv, sizeof(line) - 1); tstrncpy(line, *pEnv, sizeof(line));
pEnv++; pEnv++;
if (strncmp(line, "TAOS_APOLLO_URL", 14) == 0) { if (strncmp(line, "TAOS_APOLLO_URL", 14) == 0) {
char *p = strchr(line, '='); char *p = strchr(line, '=');

View File

@ -18,6 +18,7 @@
#include "taoserror.h" #include "taoserror.h"
int32_t taosVersionStrToInt(const char *vstr, int32_t *vint) { int32_t taosVersionStrToInt(const char *vstr, int32_t *vint) {
int32_t code = 0;
if (vstr == NULL) { if (vstr == NULL) {
return terrno = TSDB_CODE_INVALID_VERSION_STRING; return terrno = TSDB_CODE_INVALID_VERSION_STRING;
} }
@ -31,7 +32,10 @@ int32_t taosVersionStrToInt(const char *vstr, int32_t *vint) {
if (vstr[spos] != '.') { if (vstr[spos] != '.') {
tmp[spos - tpos] = vstr[spos]; tmp[spos - tpos] = vstr[spos];
} else { } else {
vnum[vpos] = atoi(tmp); code = taosStr2int32(tmp, &vnum[vpos]);
if (code != 0) {
return code;
}
memset(tmp, 0, sizeof(tmp)); memset(tmp, 0, sizeof(tmp));
vpos++; vpos++;
tpos = spos + 1; tpos = spos + 1;
@ -39,7 +43,10 @@ int32_t taosVersionStrToInt(const char *vstr, int32_t *vint) {
} }
if ('\0' != tmp[0] && vpos < 4) { if ('\0' != tmp[0] && vpos < 4) {
vnum[vpos] = atoi(tmp); code = taosStr2int32(tmp, &vnum[vpos]);
if (code != 0) {
return code;
}
} }
if (vnum[0] <= 0) { if (vnum[0] <= 0) {