From 0cae5f178c9b80a9577e0c9271206f6552ebbd3f Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Sat, 26 Oct 2024 20:58:02 +0800 Subject: [PATCH] enh: increase synatx parser --- include/common/tmsg.h | 1 + include/libs/nodes/cmdnodes.h | 1 + include/util/taoserror.h | 1 + include/util/tdef.h | 1 + source/common/src/tmsg.c | 5 +++++ source/dnode/mnode/impl/src/mndDb.c | 9 +++++++++ source/libs/parser/inc/parAst.h | 1 + source/libs/parser/inc/sql.y | 1 + source/libs/parser/src/parAstCreater.c | 11 +++++++++++ source/libs/parser/src/parTranslater.c | 2 ++ source/util/src/terror.c | 1 + 11 files changed, 34 insertions(+) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 01808d4f2f..d14facec25 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -1337,6 +1337,7 @@ typedef struct { char* sql; int8_t withArbitrator; int8_t encryptAlgorithm; + char dnodeListStr[TSDB_DNODE_LIST_LEN]; } SCreateDbReq; int32_t tSerializeSCreateDbReq(void* buf, int32_t bufLen, SCreateDbReq* pReq); diff --git a/include/libs/nodes/cmdnodes.h b/include/libs/nodes/cmdnodes.h index bbf2889289..514eddbc24 100644 --- a/include/libs/nodes/cmdnodes.h +++ b/include/libs/nodes/cmdnodes.h @@ -72,6 +72,7 @@ typedef struct SDatabaseOptions { int8_t compressionLevel; int8_t encryptAlgorithm; int32_t daysPerFile; + char dnodeListStr[TSDB_DNODE_LIST_LEN]; char encryptAlgorithmStr[TSDB_ENCRYPT_ALGO_STR_LEN]; SValueNode* pDaysPerFile; int32_t fsyncPeriod; diff --git a/include/util/taoserror.h b/include/util/taoserror.h index a53923b904..db4a3c3fe4 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -352,6 +352,7 @@ int32_t taosGetErrSize(); #define TSDB_CODE_MND_INVALID_SYS_TABLENAME TAOS_DEF_ERROR_CODE(0, 0x039A) #define TSDB_CODE_MND_ENCRYPT_NOT_ALLOW_CHANGE TAOS_DEF_ERROR_CODE(0, 0x039B) #define TSDB_CODE_MND_INVALID_WAL_LEVEL TAOS_DEF_ERROR_CODE(0, 0x039C) +#define TSDB_CODE_MND_INVALID_DNODE_LIST_FMT TAOS_DEF_ERROR_CODE(0, 0x039D) // mnode-node #define TSDB_CODE_MND_MNODE_ALREADY_EXIST TAOS_DEF_ERROR_CODE(0, 0x03A0) diff --git a/include/util/tdef.h b/include/util/tdef.h index e15ec0b499..11d4878ebc 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -411,6 +411,7 @@ typedef enum ELogicConditionType { #define TSDB_CACHE_MODEL_LAST_ROW 1 #define TSDB_CACHE_MODEL_LAST_VALUE 2 #define TSDB_CACHE_MODEL_BOTH 3 +#define TSDB_DNODE_LIST_LEN 256 #define TSDB_ENCRYPT_ALGO_STR_LEN 16 #define TSDB_ENCRYPT_ALGO_NONE_STR "none" #define TSDB_ENCRYPT_ALGO_SM4_STR "sm4" diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 63fcf900bf..6cbf3665a7 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -3874,6 +3874,7 @@ int32_t tSerializeSCreateDbReq(void *buf, int32_t bufLen, SCreateDbReq *pReq) { TAOS_CHECK_EXIT(tEncodeI32(&encoder, pReq->s3ChunkSize)); TAOS_CHECK_EXIT(tEncodeI32(&encoder, pReq->s3KeepLocal)); TAOS_CHECK_EXIT(tEncodeI8(&encoder, pReq->s3Compact)); + TAOS_CHECK_EXIT(tEncodeCStr(&encoder, pReq->dnodeListStr)); tEndEncode(&encoder); @@ -3962,6 +3963,10 @@ int32_t tDeserializeSCreateDbReq(void *buf, int32_t bufLen, SCreateDbReq *pReq) TAOS_CHECK_EXIT(tDecodeI8(&decoder, &pReq->s3Compact)); } + if (!tDecodeIsEnd(&decoder)) { + TAOS_CHECK_EXIT(tDecodeCStrTo(&decoder, pReq->dnodeListStr)); + } + tEndDecode(&decoder); _exit: diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index aed00af3c1..aea92662a1 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -883,6 +883,13 @@ static void mndBuildAuditDetailInt64(char *detail, char *tmp, char *format, int6 } } +static int32_t mndCheckDbDnodeList(SMnode *pMnode, SCreateDbReq *pReq) { + if (pReq->dnodeListStr[0] == 0) return 0; + + mInfo("db:%s, dnode list is %s", pReq->db, pReq->dnodeListStr); + return 0; +} + static int32_t mndCheckDbEncryptKey(SMnode *pMnode, SCreateDbReq *pReq) { int32_t code = 0; SSdb *pSdb = pMnode->pSdb; @@ -975,6 +982,8 @@ static int32_t mndProcessCreateDbReq(SRpcMsg *pReq) { TAOS_CHECK_GOTO(mndCheckDbEncryptKey(pMnode, &createReq), &lino, _OVER); + TAOS_CHECK_GOTO(mndCheckDbDnodeList(pMnode, &createReq), &lino, _OVER); + TAOS_CHECK_GOTO(mndAcquireUser(pMnode, pReq->info.conn.user, &pUser), &lino, _OVER); TAOS_CHECK_GOTO(mndCreateDb(pMnode, pReq, &createReq, pUser), &lino, _OVER); diff --git a/source/libs/parser/inc/parAst.h b/source/libs/parser/inc/parAst.h index 28e867965f..94ad7316e8 100644 --- a/source/libs/parser/inc/parAst.h +++ b/source/libs/parser/inc/parAst.h @@ -69,6 +69,7 @@ typedef enum EDatabaseOptionType { DB_OPTION_S3_COMPACT, DB_OPTION_KEEP_TIME_OFFSET, DB_OPTION_ENCRYPT_ALGORITHM, + DB_OPTION_DNODES, } EDatabaseOptionType; typedef enum ETableOptionType { diff --git a/source/libs/parser/inc/sql.y b/source/libs/parser/inc/sql.y index 99f301445a..ed6202021e 100644 --- a/source/libs/parser/inc/sql.y +++ b/source/libs/parser/inc/sql.y @@ -286,6 +286,7 @@ db_options(A) ::= db_options(B) S3_KEEPLOCAL NK_VARIABLE(C). db_options(A) ::= db_options(B) S3_COMPACT NK_INTEGER(C). { A = setDatabaseOption(pCxt, B, DB_OPTION_S3_COMPACT, &C); } db_options(A) ::= db_options(B) KEEP_TIME_OFFSET NK_INTEGER(C). { A = setDatabaseOption(pCxt, B, DB_OPTION_KEEP_TIME_OFFSET, &C); } db_options(A) ::= db_options(B) ENCRYPT_ALGORITHM NK_STRING(C). { A = setDatabaseOption(pCxt, B, DB_OPTION_ENCRYPT_ALGORITHM, &C); } +db_options(A) ::= db_options(B) DNODES NK_STRING(C). { A = setDatabaseOption(pCxt, B, DB_OPTION_DNODES, &C); } alter_db_options(A) ::= alter_db_option(B). { A = createAlterDatabaseOptions(pCxt); A = setAlterDatabaseOption(pCxt, A, &B); } alter_db_options(A) ::= alter_db_options(B) alter_db_option(C). { A = setAlterDatabaseOption(pCxt, B, &C); } diff --git a/source/libs/parser/src/parAstCreater.c b/source/libs/parser/src/parAstCreater.c index e031ee0fe1..a770ed3490 100644 --- a/source/libs/parser/src/parAstCreater.c +++ b/source/libs/parser/src/parAstCreater.c @@ -1799,6 +1799,7 @@ SNode* createDefaultDatabaseOptions(SAstCreateContext* pCxt) { pOptions->s3Compact = TSDB_DEFAULT_S3_COMPACT; pOptions->withArbitrator = TSDB_DEFAULT_DB_WITH_ARBITRATOR; pOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO; + pOptions->dnodeListStr[0] = 0; return (SNode*)pOptions; _err: return NULL; @@ -1842,6 +1843,7 @@ SNode* createAlterDatabaseOptions(SAstCreateContext* pCxt) { pOptions->s3Compact = -1; pOptions->withArbitrator = -1; pOptions->encryptAlgorithm = -1; + pOptions->dnodeListStr[0] = 0; return (SNode*)pOptions; _err: return NULL; @@ -1982,6 +1984,15 @@ static SNode* setDatabaseOptionImpl(SAstCreateContext* pCxt, SNode* pOptions, ED pDbOptions->encryptAlgorithm = TSDB_DEFAULT_ENCRYPT_ALGO; break; } + case DB_OPTION_DNODES: + if (((SToken*)pVal)->n >= TSDB_DNODE_LIST_LEN) { + snprintf(pCxt->pQueryCxt->pMsg, pCxt->pQueryCxt->msgLen, "the dnode list is too long (should less than %d)", + TSDB_DNODE_LIST_LEN); + pCxt->errCode = TSDB_CODE_PAR_SYNTAX_ERROR; + } else { + COPY_STRING_FORM_STR_TOKEN(pDbOptions->dnodeListStr, (SToken*)pVal); + } + break; default: break; } diff --git a/source/libs/parser/src/parTranslater.c b/source/libs/parser/src/parTranslater.c index 636be7c5cc..f9006d032f 100755 --- a/source/libs/parser/src/parTranslater.c +++ b/source/libs/parser/src/parTranslater.c @@ -7532,6 +7532,8 @@ static int32_t buildCreateDbReq(STranslateContext* pCxt, SCreateDatabaseStmt* pS pReq->ignoreExist = pStmt->ignoreExists; pReq->withArbitrator = pStmt->pOptions->withArbitrator; pReq->encryptAlgorithm = pStmt->pOptions->encryptAlgorithm; + tstrncpy(pReq->dnodeListStr, pStmt->pOptions->dnodeListStr, TSDB_DNODE_LIST_LEN); + return buildCreateDbRetentions(pStmt->pOptions->pRetentions, pReq); } diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 52a3be120d..43fd47fcc7 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -268,6 +268,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_MND_DB_IN_CREATING, "Database in creating TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_SYS_TABLENAME, "Invalid system table name") TAOS_DEFINE_ERROR(TSDB_CODE_MND_ENCRYPT_NOT_ALLOW_CHANGE, "Encryption is not allowed to be changed after database is created") TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_WAL_LEVEL, "Invalid option, wal_level 0 should be used with replica 1") +TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DNODE_LIST_FMT, "Invalid dnode list format") // mnode-node TAOS_DEFINE_ERROR(TSDB_CODE_MND_MNODE_ALREADY_EXIST, "Mnode already exists")