From c8a746574da5e35bc88f918a469aba0e394c4f69 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Wed, 15 Dec 2021 16:32:24 +0800 Subject: [PATCH] TD-10431 create db test --- include/util/taoserror.h | 26 ++---- include/util/tdef.h | 1 + source/dnode/mgmt/impl/test/db/db.cpp | 30 +++---- source/dnode/mgmt/impl/test/user/user.cpp | 2 +- source/dnode/mnode/impl/src/mndDb.c | 101 ++++++++++++++++------ source/util/src/terror.c | 18 +--- 6 files changed, 97 insertions(+), 81 deletions(-) diff --git a/include/util/taoserror.h b/include/util/taoserror.h index c53e7931ac..4f1ef7da7b 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -197,29 +197,13 @@ int32_t* taosGetErrno(); #define TSDB_CODE_MND_DB_NOT_SELECTED TAOS_DEF_ERROR_CODE(0, 0x0383) #define TSDB_CODE_MND_INVALID_DB TAOS_DEF_ERROR_CODE(0, 0x0384) #define TSDB_CODE_MND_INVALID_DB_OPTION TAOS_DEF_ERROR_CODE(0, 0x0385) -#define TSDB_CODE_MND_INVALID_DB_CACHE_SIZE TAOS_DEF_ERROR_CODE(0, 0x0386) -#define TSDB_CODE_MND_INVALID_DB_TOTAL_BLOCKS TAOS_DEF_ERROR_CODE(0, 0x0387) -#define TSDB_CODE_MND_INVALID_DB_DAYS TAOS_DEF_ERROR_CODE(0, 0x0388) -#define TSDB_CODE_MND_INVALID_DB_KEEP0 TAOS_DEF_ERROR_CODE(0, 0x0389) -#define TSDB_CODE_MND_INVALID_DB_KEEP1 TAOS_DEF_ERROR_CODE(0, 0x038A) -#define TSDB_CODE_MND_INVALID_DB_KEEP2 TAOS_DEF_ERROR_CODE(0, 0x038B) -#define TSDB_CODE_MND_INVALID_DB_MIN_ROWS TAOS_DEF_ERROR_CODE(0, 0x038C) -#define TSDB_CODE_MND_INVALID_DB_MAX_ROWS TAOS_DEF_ERROR_CODE(0, 0x038D) -#define TSDB_CODE_MND_INVALID_DB_COMMIT_TIME TAOS_DEF_ERROR_CODE(0, 0x038E) -#define TSDB_CODE_MND_INVALID_DB_FSYNC_PERIOD TAOS_DEF_ERROR_CODE(0, 0x038F) -#define TSDB_CODE_MND_INVALID_DB_WAL_LEVEL TAOS_DEF_ERROR_CODE(0, 0x0390) -#define TSDB_CODE_MND_INVALID_DB_PRECISION TAOS_DEF_ERROR_CODE(0, 0x0391) -#define TSDB_CODE_MND_INVALID_DB_COMP TAOS_DEF_ERROR_CODE(0, 0x0392) -#define TSDB_CODE_MND_INVALID_DB_REPLICA TAOS_DEF_ERROR_CODE(0, 0x0393) -#define TSDB_CODE_MND_INVALID_DB_QUORUM TAOS_DEF_ERROR_CODE(0, 0x0394) -#define TSDB_CODE_MND_INVALID_DB_UPDATE TAOS_DEF_ERROR_CODE(0, 0x0395) -#define TSDB_CODE_MND_INVALID_DB_CACHE_LAST TAOS_DEF_ERROR_CODE(0, 0x0396) -#define TSDB_CODE_MND_DB_OPTION_UNCHANGED TAOS_DEF_ERROR_CODE(0, 0x0397) +#define TSDB_CODE_MND_INVALID_DB_ACCT TAOS_DEF_ERROR_CODE(0, 0x0386) +#define TSDB_CODE_MND_DB_OPTION_UNCHANGED TAOS_DEF_ERROR_CODE(0, 0x0387) // mnode-vgroup -#define TSDB_CODE_MND_VGROUP_NOT_EXIST TAOS_DEF_ERROR_CODE(0, 0x03A0) -#define TSDB_CODE_MND_VGROUP_NOT_IN_DNODE TAOS_DEF_ERROR_CODE(0, 0x03A1) -#define TSDB_CODE_MND_VGROUP_ALREADY_IN_DNODE TAOS_DEF_ERROR_CODE(0, 0x03A2) +#define TSDB_CODE_MND_VGROUP_NOT_EXIST TAOS_DEF_ERROR_CODE(0, 0x0390) +#define TSDB_CODE_MND_VGROUP_NOT_IN_DNODE TAOS_DEF_ERROR_CODE(0, 0x0391) +#define TSDB_CODE_MND_VGROUP_ALREADY_IN_DNODE TAOS_DEF_ERROR_CODE(0, 0x0392) // mnode-stable #define TSDB_CODE_MND_STB_NOT_EXIST TAOS_DEF_ERROR_CODE(0, 0x0360) diff --git a/include/util/tdef.h b/include/util/tdef.h index 300b190e97..1511190c3e 100644 --- a/include/util/tdef.h +++ b/include/util/tdef.h @@ -209,6 +209,7 @@ do { \ #define TSDB_STEP_NAME_LEN 32 #define TSDB_STEP_DESC_LEN 128 +#define TSDB_ERROR_MSG_LEN 1024 #define TSDB_DNODE_CONFIG_LEN 128 #define TSDB_MQTT_HOSTNAME_LEN 64 diff --git a/source/dnode/mgmt/impl/test/db/db.cpp b/source/dnode/mgmt/impl/test/db/db.cpp index d88d9d644f..4fa5e43883 100644 --- a/source/dnode/mgmt/impl/test/db/db.cpp +++ b/source/dnode/mgmt/impl/test/db/db.cpp @@ -24,7 +24,7 @@ class DndTestDb : public ::testing::Test { } static void SetUpTestSuite() { - initLog("/tmp/dnode_test_db"); + initLog("/tmp/tdlog"); const char* fqdn = "localhost"; const char* firstEp = "localhost:9040"; @@ -207,17 +207,17 @@ TEST_F(DndTestDb, ShowDb) { TEST_F(DndTestDb, CreateDb_01) { { SCreateDbMsg* pReq = (SCreateDbMsg*)rpcMallocCont(sizeof(SCreateDbMsg)); - strcpy(pReq->db, "d1"); - pReq->cacheBlockSize = 16; - pReq->totalBlocks = 10; - pReq->daysPerFile = 10; - pReq->daysToKeep0 = 3650; - pReq->daysToKeep1 = 3650; - pReq->daysToKeep2 = 3650; - pReq->minRowsPerFileBlock = 100; - pReq->maxRowsPerFileBlock = 4096; - pReq->commitTime = 3600; - pReq->fsyncPeriod = 3000; + strcpy(pReq->db, "1.d1"); + pReq->cacheBlockSize = htonl(16); + pReq->totalBlocks = htonl(10); + pReq->daysPerFile = htonl(10); + pReq->daysToKeep0 = htonl(3650); + pReq->daysToKeep1 = htonl(3650); + pReq->daysToKeep2 = htonl(3650); + pReq->minRowsPerFileBlock = htonl(100); + pReq->maxRowsPerFileBlock = htonl(4096); + pReq->commitTime = htonl(3600); + pReq->fsyncPeriod = htonl(3000); pReq->walLevel = 1; pReq->precision = 0; pReq->compression = 2; @@ -229,7 +229,7 @@ TEST_F(DndTestDb, CreateDb_01) { SRpcMsg rpcMsg = {0}; rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SCreateUserMsg); + rpcMsg.contLen = sizeof(SCreateDbMsg); rpcMsg.msgType = TSDB_MSG_TYPE_CREATE_DB; sendMsg(pClient, &rpcMsg); @@ -238,8 +238,8 @@ TEST_F(DndTestDb, CreateDb_01) { ASSERT_EQ(pMsg->code, 0); } - SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_USER, "show databases", 6); - SendThenCheckShowRetrieveMsg(3); + SendTheCheckShowMetaMsg(TSDB_MGMT_TABLE_DB, "show databases", 16); + SendThenCheckShowRetrieveMsg(1); CheckBinary("d1", TSDB_DB_NAME_LEN - 1); CheckTimestamp(); CheckInt16(1); // replica diff --git a/source/dnode/mgmt/impl/test/user/user.cpp b/source/dnode/mgmt/impl/test/user/user.cpp index 9d3acbc273..6a48a2cb49 100644 --- a/source/dnode/mgmt/impl/test/user/user.cpp +++ b/source/dnode/mgmt/impl/test/user/user.cpp @@ -24,7 +24,7 @@ class DndTestUser : public ::testing::Test { } static void SetUpTestSuite() { - initLog("/tmp/dnode_test_user"); + initLog("/tmp/tdlog"); const char* fqdn = "localhost"; const char* firstEp = "localhost:9140"; diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index e44e25cbab..6426c3f33f 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -152,7 +152,7 @@ static int32_t mndDbActionDelete(SSdb *pSdb, SDbObj *pDb) { static int32_t mndDbActionUpdate(SSdb *pSdb, SDbObj *pOldDb, SDbObj *pNewDb) { mTrace("db:%s, perform update action", pOldDb->name); pOldDb->updateTime = pNewDb->createdTime; - memcpy(&pOldDb->cfg, &pNewDb->cfg, sizeof(SDbObj)); + memcpy(&pOldDb->cfg, &pNewDb->cfg, sizeof(SDbCfg)); return 0; } @@ -166,109 +166,146 @@ void mndReleaseDb(SMnode *pMnode, SDbObj *pDb) { sdbRelease(pSdb, pDb); } -static int32_t mndCheckDbCfg(SMnode *pMnode, SDbCfg *pCfg) { +static int32_t mndCheckDbName(char *dbName, SUserObj *pUser) { + char *pos = strstr(dbName, TS_PATH_DELIMITER); + if (pos == NULL) { + terrno = TSDB_CODE_MND_INVALID_DB; + return -1; + } + + int32_t acctId = atoi(dbName); + if (acctId != pUser->acctId) { + terrno = TSDB_CODE_MND_INVALID_DB_ACCT; + return -1; + } + + return 0; +} + +static int32_t mndCheckDbCfg(SMnode *pMnode, SDbCfg *pCfg, char *errMsg, int32_t len) { if (pCfg->cacheBlockSize < TSDB_MIN_CACHE_BLOCK_SIZE || pCfg->cacheBlockSize > TSDB_MAX_CACHE_BLOCK_SIZE) { - terrno = TSDB_CODE_MND_INVALID_DB_CACHE_SIZE; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database cache block size option", len); return -1; } if (pCfg->totalBlocks < TSDB_MIN_TOTAL_BLOCKS || pCfg->totalBlocks > TSDB_MAX_TOTAL_BLOCKS) { - terrno = TSDB_CODE_MND_INVALID_DB_TOTAL_BLOCKS; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database total blocks option", len); return -1; } if (pCfg->daysPerFile < TSDB_MIN_DAYS_PER_FILE || pCfg->daysPerFile > TSDB_MAX_DAYS_PER_FILE) { - terrno = TSDB_CODE_MND_INVALID_DB_DAYS; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database days option", len); return -1; } if (pCfg->daysToKeep0 < pCfg->daysPerFile) { - terrno = TSDB_CODE_MND_INVALID_DB_KEEP0; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database days option", len); return -1; } if (pCfg->daysToKeep0 < TSDB_MIN_KEEP || pCfg->daysToKeep0 > TSDB_MAX_KEEP || pCfg->daysToKeep0 > pCfg->daysToKeep1) { - terrno = TSDB_CODE_MND_INVALID_DB_KEEP0; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database keep0 option", len); return -1; } if (pCfg->daysToKeep1 < TSDB_MIN_KEEP || pCfg->daysToKeep1 > TSDB_MAX_KEEP || pCfg->daysToKeep1 > pCfg->daysToKeep2) { - terrno = TSDB_CODE_MND_INVALID_DB_KEEP1; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database keep1 option", len); return -1; } if (pCfg->daysToKeep2 < TSDB_MIN_KEEP || pCfg->daysToKeep2 > TSDB_MAX_KEEP) { - terrno = TSDB_CODE_MND_INVALID_DB_KEEP1; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database keep2 option", len); return -1; } if (pCfg->minRowsPerFileBlock < TSDB_MIN_MIN_ROW_FBLOCK || pCfg->minRowsPerFileBlock > TSDB_MAX_MIN_ROW_FBLOCK) { - terrno = TSDB_CODE_MND_INVALID_DB_MIN_ROWS; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database minrows option", len); return -1; } if (pCfg->maxRowsPerFileBlock < TSDB_MIN_MAX_ROW_FBLOCK || pCfg->maxRowsPerFileBlock > TSDB_MAX_MAX_ROW_FBLOCK) { - terrno = TSDB_CODE_MND_INVALID_DB_MAX_ROWS; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database maxrows option", len); return -1; } if (pCfg->minRowsPerFileBlock > pCfg->maxRowsPerFileBlock) { - terrno = TSDB_CODE_MND_INVALID_DB_MIN_ROWS; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database minrows option", len); return -1; } if (pCfg->commitTime < TSDB_MIN_COMMIT_TIME || pCfg->commitTime > TSDB_MAX_COMMIT_TIME) { - terrno = TSDB_CODE_MND_INVALID_DB_COMMIT_TIME; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database commit option", len); return -1; } if (pCfg->fsyncPeriod < TSDB_MIN_FSYNC_PERIOD || pCfg->fsyncPeriod > TSDB_MAX_FSYNC_PERIOD) { - terrno = TSDB_CODE_MND_INVALID_DB_FSYNC_PERIOD; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database fsync option", len); return -1; } if (pCfg->walLevel < TSDB_MIN_WAL_LEVEL || pCfg->walLevel > TSDB_MAX_WAL_LEVEL) { - terrno = TSDB_CODE_MND_INVALID_DB_WAL_LEVEL; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database wal level option", len); return -1; } if (pCfg->precision < TSDB_MIN_PRECISION && pCfg->precision > TSDB_MAX_PRECISION) { - terrno = TSDB_CODE_MND_INVALID_DB_PRECISION; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid precision option", len); return -1; } if (pCfg->compression < TSDB_MIN_COMP_LEVEL || pCfg->compression > TSDB_MAX_COMP_LEVEL) { - terrno = TSDB_CODE_MND_INVALID_DB_COMP; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database compression option", len); return -1; } if (pCfg->replications < TSDB_MIN_DB_REPLICA_OPTION || pCfg->replications > TSDB_MAX_DB_REPLICA_OPTION) { - terrno = TSDB_CODE_MND_INVALID_DB_REPLICA; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database replication option", len); return -1; } if (pCfg->replications > mndGetDnodeSize(pMnode)) { - terrno = TSDB_CODE_MND_INVALID_DB_REPLICA; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database replication option", len); return -1; } if (pCfg->quorum < TSDB_MIN_DB_QUORUM_OPTION || pCfg->quorum > TSDB_MAX_DB_QUORUM_OPTION) { - terrno = TSDB_CODE_MND_INVALID_DB_QUORUM; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database quorum option", len); return -1; } if (pCfg->quorum > pCfg->replications) { - terrno = TSDB_CODE_MND_INVALID_DB_QUORUM; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database quorum option", len); return -1; } if (pCfg->update < TSDB_MIN_DB_UPDATE || pCfg->update > TSDB_MAX_DB_UPDATE) { - terrno = TSDB_CODE_MND_INVALID_DB_UPDATE; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database update option", len); return -1; } if (pCfg->cacheLastRow < TSDB_MIN_DB_CACHE_LAST_ROW || pCfg->cacheLastRow > TSDB_MAX_DB_CACHE_LAST_ROW) { - terrno = TSDB_CODE_MND_INVALID_DB_CACHE_LAST; + terrno = TSDB_CODE_MND_INVALID_DB_OPTION; + tstrncpy(errMsg, "Invalid database cachelast option", len); return -1; } @@ -295,10 +332,10 @@ static void mndSetDefaultDbCfg(SDbCfg *pCfg) { if (pCfg->cacheLastRow < 0) pCfg->cacheLastRow = TSDB_DEFAULT_CACHE_LAST_ROW; } -static int32_t mndCreateDb(SMnode *pMnode, SMnodeMsg *pMsg, SCreateDbMsg *pCreate, char *acct) { +static int32_t mndCreateDb(SMnode *pMnode, SMnodeMsg *pMsg, SCreateDbMsg *pCreate, SUserObj *pUser) { SDbObj dbObj = {0}; tstrncpy(dbObj.name, pCreate->db, TSDB_FULL_DB_NAME_LEN); - tstrncpy(dbObj.acct, acct, TSDB_USER_LEN); + tstrncpy(dbObj.acct, pUser->acct, TSDB_USER_LEN); dbObj.createdTime = taosGetTimestampMs(); dbObj.updateTime = dbObj.createdTime; dbObj.uid = mndGenerateUid(dbObj.name, TSDB_FULL_DB_NAME_LEN); @@ -322,7 +359,13 @@ static int32_t mndCreateDb(SMnode *pMnode, SMnodeMsg *pMsg, SCreateDbMsg *pCreat mndSetDefaultDbCfg(&dbObj.cfg); - if (mndCheckDbCfg(pMnode, &dbObj.cfg) != 0) { + if (mndCheckDbName(dbObj.name, pUser) != 0) { + mError("db:%s, failed to create since %s", pCreate->db, terrstr()); + return -1; + } + + char errMsg[TSDB_ERROR_MSG_LEN] = {0}; + if (mndCheckDbCfg(pMnode, &dbObj.cfg, errMsg, TSDB_ERROR_MSG_LEN) != 0) { mError("db:%s, failed to create since %s", pCreate->db, terrstr()); return -1; } @@ -404,7 +447,7 @@ static int32_t mndProcessCreateDbMsg(SMnodeMsg *pMsg) { return -1; } - int32_t code = mndCreateDb(pMnode, pMsg, pCreate, pOperUser->acct); + int32_t code = mndCreateDb(pMnode, pMsg, pCreate, pOperUser); mndReleaseUser(pMnode, pOperUser); if (code != 0) { @@ -776,6 +819,10 @@ char *mnGetDbStr(char *src) { char *pos = strstr(src, TS_PATH_DELIMITER); if (pos != NULL) ++pos; + if (pos == NULL) { + return src; + } + return pos; } diff --git a/source/util/src/terror.c b/source/util/src/terror.c index b8520821fc..70a3dc622f 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -207,23 +207,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_MND_TOO_MANY_DATABASES, "Too many databases fo TAOS_DEFINE_ERROR(TSDB_CODE_MND_DB_NOT_SELECTED, "Database not specified or available") TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB, "Invalid database name") TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_OPTION, "Invalid database options") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_CACHE_SIZE, "Invalid database cache block size option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_TOTAL_BLOCKS, "Invalid database total blocks option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_DAYS, "Invalid database days option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_KEEP0, "Invalid database keep0 option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_KEEP1, "Invalid database keep1 option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_KEEP2, "Invalid database keep2 option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_MIN_ROWS, "Invalid database min rows option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_MAX_ROWS, "Invalid database max rows option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_COMMIT_TIME, "Invalid database commit time option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_FSYNC_PERIOD, "Invalid database fsync periodoptions") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_WAL_LEVEL, "Invalid database wal level option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_PRECISION, "Invalid database precisin option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_COMP, "Invalid database compression option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_REPLICA, "Invalid database replication option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_QUORUM, "Invalid database quorum option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_UPDATE, "Invalid database update option") -TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_CACHE_LAST, "Invalid database cache last option") +TAOS_DEFINE_ERROR(TSDB_CODE_MND_INVALID_DB_ACCT, "Invalid database account") TAOS_DEFINE_ERROR(TSDB_CODE_MND_DB_OPTION_UNCHANGED, "Database options not changed") // mnode-vgroup -- GitLab