From d7fc732de3eac60499fabf4e2a38de168c540244 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Sat, 12 Feb 2022 17:14:33 +0800 Subject: [PATCH 1/7] serailze db req --- source/dnode/mnode/impl/src/mndDb.c | 105 +++++++++++---------- source/dnode/mnode/impl/test/db/db.cpp | 92 +++++++++--------- source/dnode/mnode/impl/test/stb/stb.cpp | 46 ++++----- source/dnode/mnode/impl/test/user/user.cpp | 46 ++++----- source/libs/catalog/test/catalogTests.cpp | 98 +++++++++---------- source/util/src/terror.c | 2 +- 6 files changed, 196 insertions(+), 193 deletions(-) diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index 5ed3e9e8df..1a01c3852a 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -15,6 +15,7 @@ #define _DEFAULT_SOURCE #include "mndDb.h" +#include "mndAuth.h" #include "mndDnode.h" #include "mndShow.h" #include "mndTrans.h" @@ -369,7 +370,7 @@ static int32_t mndSetCreateDbUndoActions(SMnode *pMnode, STrans *pTrans, SDbObj action.pCont = pReq; action.contLen = sizeof(SDropVnodeReq); action.msgType = TDMT_DND_DROP_VNODE; - action.acceptableCode = TSDB_CODE_DND_VNODE_NOT_DEPLOYED; + action.acceptableCode = TSDB_CODE_DND_VNODE_NOT_DEPLOYED; if (mndTransAppendUndoAction(pTrans, &action) != 0) { free(pReq); return -1; @@ -451,54 +452,54 @@ CREATE_DB_OVER: } static int32_t mndProcessCreateDbReq(SMnodeMsg *pReq) { - SMnode *pMnode = pReq->pMnode; - SCreateDbReq *pCreate = pReq->rpcMsg.pCont; + SMnode *pMnode = pReq->pMnode; + int32_t code = -1; + SDbObj *pDb = NULL; + SUserObj *pUser = NULL; + SCreateDbReq createReq = {0}; - pCreate->numOfVgroups = htonl(pCreate->numOfVgroups); - pCreate->cacheBlockSize = htonl(pCreate->cacheBlockSize); - pCreate->totalBlocks = htonl(pCreate->totalBlocks); - pCreate->daysPerFile = htonl(pCreate->daysPerFile); - pCreate->daysToKeep0 = htonl(pCreate->daysToKeep0); - pCreate->daysToKeep1 = htonl(pCreate->daysToKeep1); - pCreate->daysToKeep2 = htonl(pCreate->daysToKeep2); - pCreate->minRows = htonl(pCreate->minRows); - pCreate->maxRows = htonl(pCreate->maxRows); - pCreate->commitTime = htonl(pCreate->commitTime); - pCreate->fsyncPeriod = htonl(pCreate->fsyncPeriod); + if (tDeserializeSCreateDbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &createReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + goto CREATE_DB_OVER; + } - mDebug("db:%s, start to create, vgroups:%d", pCreate->db, pCreate->numOfVgroups); + mDebug("db:%s, start to create, vgroups:%d", createReq.db, createReq.numOfVgroups); - SDbObj *pDb = mndAcquireDb(pMnode, pCreate->db); + pDb = mndAcquireDb(pMnode, createReq.db); if (pDb != NULL) { - mndReleaseDb(pMnode, pDb); - if (pCreate->ignoreExist) { - mDebug("db:%s, already exist, ignore exist is set", pCreate->db); - return 0; + if (createReq.ignoreExist) { + mDebug("db:%s, already exist, ignore exist is set", createReq.db); + code = 0; + goto CREATE_DB_OVER; } else { terrno = TSDB_CODE_MND_DB_ALREADY_EXIST; - mError("db:%s, failed to create since %s", pCreate->db, terrstr()); - return -1; + goto CREATE_DB_OVER; } } else if (terrno != TSDB_CODE_MND_DB_NOT_EXIST) { - mError("db:%s, failed to create since %s", pCreate->db, terrstr()); - return -1; + goto CREATE_DB_OVER; } - SUserObj *pOperUser = mndAcquireUser(pMnode, pReq->user); - if (pOperUser == NULL) { - mError("db:%s, failed to create since %s", pCreate->db, terrstr()); - return -1; + pUser = mndAcquireUser(pMnode, pReq->user); + if (pUser == NULL) { + goto CREATE_DB_OVER; } - int32_t code = mndCreateDb(pMnode, pReq, pCreate, pOperUser); - mndReleaseUser(pMnode, pOperUser); - - if (code != 0) { - mError("db:%s, failed to create since %s", pCreate->db, terrstr()); - return -1; + if (mndCheckCreateDbAuth(pUser) != 0) { + goto CREATE_DB_OVER; } - return TSDB_CODE_MND_ACTION_IN_PROGRESS; + code = mndCreateDb(pMnode, pReq, &createReq, pUser); + if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS; + +CREATE_DB_OVER: + if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) { + mError("db:%s, failed to create since %s", createReq.db, terrstr()); + } + + mndReleaseDb(pMnode, pDb); + mndReleaseUser(pMnode, pUser); + + return code; } static int32_t mndSetDbCfgFromAlterDbReq(SDbObj *pDb, SAlterDbReq *pAlter) { @@ -818,7 +819,7 @@ static int32_t mndProcessDropDbReq(SMnodeMsg *pReq) { static void mndBuildDBVgroupInfo(SDbObj *pDb, SMnode *pMnode, SVgroupInfo *vgList, int32_t *vgNum) { int32_t vindex = 0; - SSdb *pSdb = pMnode->pSdb; + SSdb *pSdb = pMnode->pSdb; void *pIter = NULL; while (vindex < pDb->cfg.numOfVgroups) { @@ -833,9 +834,9 @@ static void mndBuildDBVgroupInfo(SDbObj *pDb, SMnode *pMnode, SVgroupInfo *vgLis pInfo->hashEnd = htonl(pVgroup->hashEnd); pInfo->epset.numOfEps = pVgroup->replica; for (int32_t gid = 0; gid < pVgroup->replica; ++gid) { - SVnodeGid *pVgid = &pVgroup->vnodeGid[gid]; - SEp * pEp = &pInfo->epset.eps[gid]; - SDnodeObj *pDnode = mndAcquireDnode(pMnode, pVgid->dnodeId); + SVnodeGid *pVgid = &pVgroup->vnodeGid[gid]; + SEp *pEp = &pInfo->epset.eps[gid]; + SDnodeObj *pDnode = mndAcquireDnode(pMnode, pVgid->dnodeId); if (pDnode != NULL) { memcpy(pEp->fqdn, pDnode->fqdn, TSDB_FQDN_LEN); pEp->port = htons(pDnode->port); @@ -895,12 +896,12 @@ static int32_t mndProcessUseDbReq(SMnodeMsg *pReq) { } int32_t mndValidateDBInfo(SMnode *pMnode, SDbVgVersion *dbs, int32_t num, void **rsp, int32_t *rspLen) { - SSdb *pSdb = pMnode->pSdb; - int32_t bufSize = num * (sizeof(SUseDbRsp) + TSDB_DEFAULT_VN_PER_DB * sizeof(SVgroupInfo)); - void *buf = malloc(bufSize); - int32_t len = 0; - int32_t contLen = 0; - int32_t bufOffset = 0; + SSdb *pSdb = pMnode->pSdb; + int32_t bufSize = num * (sizeof(SUseDbRsp) + TSDB_DEFAULT_VN_PER_DB * sizeof(SVgroupInfo)); + void *buf = malloc(bufSize); + int32_t len = 0; + int32_t contLen = 0; + int32_t bufOffset = 0; SUseDbRsp *pRsp = NULL; for (int32_t i = 0; i < num; ++i) { @@ -909,11 +910,11 @@ int32_t mndValidateDBInfo(SMnode *pMnode, SDbVgVersion *dbs, int32_t num, void * db->vgVersion = ntohl(db->vgVersion); len = 0; - + SDbObj *pDb = mndAcquireDb(pMnode, db->dbFName); if (pDb == NULL) { mInfo("db %s not exist", db->dbFName); - + len = sizeof(SUseDbRsp); } else if (pDb->uid != db->dbId || db->vgVersion < pDb->vgVersion) { len = sizeof(SUseDbRsp) + pDb->cfg.numOfVgroups * sizeof(SVgroupInfo); @@ -921,16 +922,16 @@ int32_t mndValidateDBInfo(SMnode *pMnode, SDbVgVersion *dbs, int32_t num, void * if (0 == len) { mndReleaseDb(pMnode, pDb); - + continue; } - + contLen += len; - + if (contLen > bufSize) { buf = realloc(buf, contLen); } - + pRsp = (SUseDbRsp *)((char *)buf + bufOffset); memcpy(pRsp->db, db->dbFName, TSDB_DB_FNAME_LEN); if (pDb) { @@ -949,7 +950,7 @@ int32_t mndValidateDBInfo(SMnode *pMnode, SDbVgVersion *dbs, int32_t num, void * } bufOffset += len; - + mndReleaseDb(pMnode, pDb); } diff --git a/source/dnode/mnode/impl/test/db/db.cpp b/source/dnode/mnode/impl/test/db/db.cpp index 2d1574467e..4c09caed9c 100644 --- a/source/dnode/mnode/impl/test/db/db.cpp +++ b/source/dnode/mnode/impl/test/db/db.cpp @@ -53,29 +53,31 @@ TEST_F(MndTestDb, 01_ShowDb) { TEST_F(MndTestDb, 02_Create_Alter_Drop_Db) { { - int32_t contLen = sizeof(SCreateDbReq); + SCreateDbReq createReq = {0}; + strcpy(createReq.db, "1.d1"); + createReq.numOfVgroups = 2; + createReq.cacheBlockSize = 16; + createReq.totalBlocks = 10; + createReq.daysPerFile = 10; + createReq.daysToKeep0 = 3650; + createReq.daysToKeep1 = 3650; + createReq.daysToKeep2 = 3650; + createReq.minRows = 100; + createReq.maxRows = 4096; + createReq.commitTime = 3600; + createReq.fsyncPeriod = 3000; + createReq.walLevel = 1; + createReq.precision = 0; + createReq.compression = 2; + createReq.replications = 1; + createReq.quorum = 1; + createReq.update = 0; + createReq.cacheLastRow = 0; + createReq.ignoreExist = 1; - SCreateDbReq* pReq = (SCreateDbReq*)rpcMallocCont(contLen); - strcpy(pReq->db, "1.d1"); - pReq->numOfVgroups = htonl(2); - 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->minRows = htonl(100); - pReq->maxRows = htonl(4096); - pReq->commitTime = htonl(3600); - pReq->fsyncPeriod = htonl(3000); - pReq->walLevel = 1; - pReq->precision = 0; - pReq->compression = 2; - pReq->replications = 1; - pReq->quorum = 1; - pReq->update = 0; - pReq->cacheLastRow = 0; - pReq->ignoreExist = 1; + int32_t contLen = tSerializeSCreateDbReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSCreateDbReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -217,29 +219,31 @@ TEST_F(MndTestDb, 02_Create_Alter_Drop_Db) { TEST_F(MndTestDb, 03_Create_Use_Restart_Use_Db) { { - int32_t contLen = sizeof(SCreateDbReq); + SCreateDbReq createReq = {0}; + strcpy(createReq.db, "1.d2"); + createReq.numOfVgroups = 2; + createReq.cacheBlockSize = 16; + createReq.totalBlocks = 10; + createReq.daysPerFile = 10; + createReq.daysToKeep0 = 3650; + createReq.daysToKeep1 = 3650; + createReq.daysToKeep2 = 3650; + createReq.minRows = 100; + createReq.maxRows = 4096; + createReq.commitTime = 3600; + createReq.fsyncPeriod = 3000; + createReq.walLevel = 1; + createReq.precision = 0; + createReq.compression = 2; + createReq.replications = 1; + createReq.quorum = 1; + createReq.update = 0; + createReq.cacheLastRow = 0; + createReq.ignoreExist = 1; - SCreateDbReq* pReq = (SCreateDbReq*)rpcMallocCont(contLen); - strcpy(pReq->db, "1.d2"); - pReq->numOfVgroups = htonl(2); - 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->minRows = htonl(100); - pReq->maxRows = htonl(4096); - pReq->commitTime = htonl(3600); - pReq->fsyncPeriod = htonl(3000); - pReq->walLevel = 1; - pReq->precision = 0; - pReq->compression = 2; - pReq->replications = 1; - pReq->quorum = 1; - pReq->update = 0; - pReq->cacheLastRow = 0; - pReq->ignoreExist = 1; + int32_t contLen = tSerializeSCreateDbReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSCreateDbReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen); ASSERT_NE(pRsp, nullptr); diff --git a/source/dnode/mnode/impl/test/stb/stb.cpp b/source/dnode/mnode/impl/test/stb/stb.cpp index c5ab44a014..6d0005fd15 100644 --- a/source/dnode/mnode/impl/test/stb/stb.cpp +++ b/source/dnode/mnode/impl/test/stb/stb.cpp @@ -38,29 +38,31 @@ class MndTestStb : public ::testing::Test { Testbase MndTestStb::test; void* MndTestStb::BuildCreateDbReq(const char* dbname, int32_t* pContLen) { - int32_t contLen = sizeof(SCreateDbReq); + SCreateDbReq createReq = {0}; + strcpy(createReq.db, dbname); + createReq.numOfVgroups = 2; + createReq.cacheBlockSize = 16; + createReq.totalBlocks = 10; + createReq.daysPerFile = 10; + createReq.daysToKeep0 = 3650; + createReq.daysToKeep1 = 3650; + createReq.daysToKeep2 = 3650; + createReq.minRows = 100; + createReq.maxRows = 4096; + createReq.commitTime = 3600; + createReq.fsyncPeriod = 3000; + createReq.walLevel = 1; + createReq.precision = 0; + createReq.compression = 2; + createReq.replications = 1; + createReq.quorum = 1; + createReq.update = 0; + createReq.cacheLastRow = 0; + createReq.ignoreExist = 1; - SCreateDbReq* pReq = (SCreateDbReq*)rpcMallocCont(contLen); - strcpy(pReq->db, dbname); - pReq->numOfVgroups = htonl(2); - 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->minRows = htonl(100); - pReq->maxRows = htonl(4096); - pReq->commitTime = htonl(3600); - pReq->fsyncPeriod = htonl(3000); - pReq->walLevel = 1; - pReq->precision = 0; - pReq->compression = 2; - pReq->replications = 1; - pReq->quorum = 1; - pReq->update = 0; - pReq->cacheLastRow = 0; - pReq->ignoreExist = 1; + int32_t contLen = tSerializeSCreateDbReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSCreateDbReq(pReq, contLen, &createReq); *pContLen = contLen; return pReq; diff --git a/source/dnode/mnode/impl/test/user/user.cpp b/source/dnode/mnode/impl/test/user/user.cpp index 8f9edfab09..d8ce599be1 100644 --- a/source/dnode/mnode/impl/test/user/user.cpp +++ b/source/dnode/mnode/impl/test/user/user.cpp @@ -319,29 +319,31 @@ TEST_F(MndTestUser, 03_Alter_User) { } { - int32_t contLen = sizeof(SCreateDbReq); + SCreateDbReq createReq = {0}; + strcpy(createReq.db, "1.d2"); + createReq.numOfVgroups = 2; + createReq.cacheBlockSize = 16; + createReq.totalBlocks = 10; + createReq.daysPerFile = 10; + createReq.daysToKeep0 = 3650; + createReq.daysToKeep1 = 3650; + createReq.daysToKeep2 = 3650; + createReq.minRows = 100; + createReq.maxRows = 4096; + createReq.commitTime = 3600; + createReq.fsyncPeriod = 3000; + createReq.walLevel = 1; + createReq.precision = 0; + createReq.compression = 2; + createReq.replications = 1; + createReq.quorum = 1; + createReq.update = 0; + createReq.cacheLastRow = 0; + createReq.ignoreExist = 1; - SCreateDbReq* pReq = (SCreateDbReq*)rpcMallocCont(contLen); - strcpy(pReq->db, "1.d2"); - pReq->numOfVgroups = htonl(2); - 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->minRows = htonl(100); - pReq->maxRows = htonl(4096); - pReq->commitTime = htonl(3600); - pReq->fsyncPeriod = htonl(3000); - pReq->walLevel = 1; - pReq->precision = 0; - pReq->compression = 2; - pReq->replications = 1; - pReq->quorum = 1; - pReq->update = 0; - pReq->cacheLastRow = 0; - pReq->ignoreExist = 1; + int32_t contLen = tSerializeSCreateDbReq(NULL, 0, &createReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSCreateDbReq(pReq, contLen, &createReq); SRpcMsg* pRsp = test.SendReq(TDMT_MND_CREATE_DB, pReq, contLen); ASSERT_NE(pRsp, nullptr); diff --git a/source/libs/catalog/test/catalogTests.cpp b/source/libs/catalog/test/catalogTests.cpp index d0f98e3c2a..4eeae0a244 100644 --- a/source/libs/catalog/test/catalogTests.cpp +++ b/source/libs/catalog/test/catalogTests.cpp @@ -39,7 +39,7 @@ namespace { extern "C" int32_t ctgGetTableMetaFromCache(struct SCatalog *pCatalog, const SName *pTableName, STableMeta **pTableMeta, int32_t *exist); extern "C" int32_t ctgUpdateTableMetaCache(struct SCatalog *pCatalog, STableMetaOutput *output); -extern "C" int32_t ctgDbgGetClusterCacheNum(struct SCatalog* pCatalog, int32_t type); +extern "C" int32_t ctgDbgGetClusterCacheNum(struct SCatalog *pCatalog, int32_t type); void ctgTestSetPrepareTableMeta(); void ctgTestSetPrepareCTableMeta(); @@ -52,14 +52,14 @@ bool ctgTestDeadLoop = false; int32_t ctgTestPrintNum = 200000; int32_t ctgTestMTRunSec = 5; -int32_t ctgTestCurrentVgVersion = 0; -int32_t ctgTestVgVersion = 1; -int32_t ctgTestVgNum = 10; -int32_t ctgTestColNum = 2; -int32_t ctgTestTagNum = 1; -int32_t ctgTestSVersion = 1; -int32_t ctgTestTVersion = 1; -int32_t ctgTestSuid = 2; +int32_t ctgTestCurrentVgVersion = 0; +int32_t ctgTestVgVersion = 1; +int32_t ctgTestVgNum = 10; +int32_t ctgTestColNum = 2; +int32_t ctgTestTagNum = 1; +int32_t ctgTestSVersion = 1; +int32_t ctgTestTVersion = 1; +int32_t ctgTestSuid = 2; uint64_t ctgTestDbId = 33; uint64_t ctgTestClusterId = 0x1; @@ -69,31 +69,35 @@ char *ctgTestCTablename = "ctable1"; char *ctgTestSTablename = "stable1"; void sendCreateDbMsg(void *shandle, SEpSet *pEpSet) { - SCreateDbReq *pReq = (SCreateDbReq *)rpcMallocCont(sizeof(SCreateDbReq)); - strcpy(pReq->db, "1.db1"); - pReq->numOfVgroups = htonl(2); - 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->minRows = htonl(100); - pReq->maxRows = htonl(4096); - pReq->commitTime = htonl(3600); - pReq->fsyncPeriod = htonl(3000); - pReq->walLevel = 1; - pReq->precision = 0; - pReq->compression = 2; - pReq->replications = 1; - pReq->quorum = 1; - pReq->update = 0; - pReq->cacheLastRow = 0; - pReq->ignoreExist = 1; + SCreateDbReq createReq = {0}; + strcpy(createReq.db, "1.db1"); + createReq.numOfVgroups = 2; + createReq.cacheBlockSize = 16; + createReq.totalBlocks = 10; + createReq.daysPerFile = 10; + createReq.daysToKeep0 = 3650; + createReq.daysToKeep1 = 3650; + createReq.daysToKeep2 = 3650; + createReq.minRows = 100; + createReq.maxRows = 4096; + createReq.commitTime = 3600; + createReq.fsyncPeriod = 3000; + createReq.walLevel = 1; + createReq.precision = 0; + createReq.compression = 2; + createReq.replications = 1; + createReq.quorum = 1; + createReq.update = 0; + createReq.cacheLastRow = 0; + createReq.ignoreExist = 1; + + int32_t contLen = tSerializeSCreateDbReq(NULL, 0, &createReq); + void *pReq = rpcMallocCont(contLen); + tSerializeSCreateDbReq(pReq, contLen, &createReq); SRpcMsg rpcMsg = {0}; rpcMsg.pCont = pReq; - rpcMsg.contLen = sizeof(SCreateDbReq); + rpcMsg.contLen = contLen; rpcMsg.msgType = TDMT_MND_CREATE_DB; SRpcMsg rpcRsp = {0}; @@ -210,7 +214,6 @@ void ctgTestBuildDBVgroup(SDBVgroupInfo **pdbVgroup) { *pdbVgroup = dbVgroup; } - void ctgTestBuildSTableMetaRsp(STableMetaRsp *rspMsg) { strcpy(rspMsg->dbFName, ctgTestDbname); sprintf(rspMsg->tbName, "%s", ctgTestSTablename); @@ -248,7 +251,6 @@ void ctgTestBuildSTableMetaRsp(STableMetaRsp *rspMsg) { return; } - void ctgTestPrepareDbVgroups(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) { SUseDbRsp *rspMsg = NULL; // todo @@ -372,8 +374,8 @@ void ctgTestPrepareSTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpc pRsp->pCont = calloc(1, pRsp->contLen); rspMsg = (STableMetaRsp *)pRsp->pCont; strcpy(rspMsg->dbFName, ctgTestDbname); - strcpy(rspMsg->tbName, ctgTestSTablename); - strcpy(rspMsg->stbName, ctgTestSTablename); + strcpy(rspMsg->tbName, ctgTestSTablename); + strcpy(rspMsg->stbName, ctgTestSTablename); rspMsg->numOfTags = htonl(ctgTestTagNum); rspMsg->numOfColumns = htonl(ctgTestColNum); rspMsg->precision = 1; @@ -635,7 +637,7 @@ void *ctgTestGetDbVgroupThread(void *param) { void *ctgTestSetSameDbVgroupThread(void *param) { struct SCatalog *pCtg = (struct SCatalog *)param; int32_t code = 0; - SDBVgroupInfo *dbVgroup = NULL; + SDBVgroupInfo *dbVgroup = NULL; int32_t n = 0; while (!ctgTestStop) { @@ -656,11 +658,10 @@ void *ctgTestSetSameDbVgroupThread(void *param) { return NULL; } - void *ctgTestSetDiffDbVgroupThread(void *param) { struct SCatalog *pCtg = (struct SCatalog *)param; int32_t code = 0; - SDBVgroupInfo *dbVgroup = NULL; + SDBVgroupInfo *dbVgroup = NULL; int32_t n = 0; while (!ctgTestStop) { @@ -681,7 +682,6 @@ void *ctgTestSetDiffDbVgroupThread(void *param) { return NULL; } - void *ctgTestGetCtableMetaThread(void *param) { struct SCatalog *pCtg = (struct SCatalog *)param; int32_t code = 0; @@ -741,7 +741,6 @@ void *ctgTestSetCtableMetaThread(void *param) { return NULL; } - TEST(tableMeta, normalTable) { struct SCatalog *pCtg = NULL; void *mockPointer = (void *)0x1; @@ -914,7 +913,7 @@ TEST(tableMeta, childTableCase) { } if (stbNum) { - printf("got expired stb,suid:%" PRId64 ",dbFName:%s, stbName:%s\n", stb->suid, stb->dbFName, stb->stbName); + printf("got expired stb,suid:%" PRId64 ",dbFName:%s, stbName:%s\n", stb->suid, stb->dbFName, stb->stbName); free(stb); stb = NULL; } else { @@ -1016,7 +1015,7 @@ TEST(tableMeta, superTableCase) { if (stbNum) { printf("got expired stb,suid:%" PRId64 ",dbFName:%s, stbName:%s\n", stb->suid, stb->dbFName, stb->stbName); - + free(stb); stb = NULL; } else { @@ -1078,7 +1077,7 @@ TEST(tableMeta, rmStbMeta) { ASSERT_EQ(ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_STB_NUM), 0); ASSERT_EQ(ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_DB_RENT_NUM), 1); ASSERT_EQ(ctgDbgGetClusterCacheNum(pCtg, CTG_DBG_STB_RENT_NUM), 0); - + catalogDestroy(); } @@ -1146,12 +1145,10 @@ TEST(tableMeta, updateStbMeta) { ASSERT_EQ(tableMeta->tableInfo.rowSize, 12); tfree(tableMeta); - + catalogDestroy(); } - - TEST(tableDistVgroup, normalTable) { struct SCatalog *pCtg = NULL; void *mockPointer = (void *)0x1; @@ -1258,7 +1255,7 @@ TEST(dbVgroup, getSetDbVgroupCase) { void *mockPointer = (void *)0x1; SVgroupInfo vgInfo = {0}; SVgroupInfo *pvgInfo = NULL; - SDBVgroupInfo *dbVgroup = NULL; + SDBVgroupInfo *dbVgroup = NULL; SArray *vgList = NULL; ctgTestInitLogFile(); @@ -1418,8 +1415,6 @@ TEST(multiThread, getSetRmDiffDbVgroup) { catalogDestroy(); } - - TEST(multiThread, ctableMeta) { struct SCatalog *pCtg = NULL; void *mockPointer = (void *)0x1; @@ -1470,8 +1465,6 @@ TEST(multiThread, ctableMeta) { catalogDestroy(); } - - TEST(rentTest, allRent) { struct SCatalog *pCtg = NULL; void *mockPointer = (void *)0x1; @@ -1530,7 +1523,8 @@ TEST(rentTest, allRent) { printf("%d - expired stableNum:%d\n", i, num); if (stable) { for (int32_t n = 0; n < num; ++n) { - printf("suid:%" PRId64 ", dbFName:%s, stbName:%s, sversion:%d, tversion:%d\n", stable[n].suid, stable[n].dbFName, stable[n].stbName, stable[n].sversion, stable[n].tversion); + printf("suid:%" PRId64 ", dbFName:%s, stbName:%s, sversion:%d, tversion:%d\n", stable[n].suid, + stable[n].dbFName, stable[n].stbName, stable[n].sversion, stable[n].tversion); } free(stable); stable = NULL; diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 4cb1e392b9..70608e117e 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -77,7 +77,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_PTR, "Invalid pointer") TAOS_DEFINE_ERROR(TSDB_CODE_MEMORY_CORRUPTED, "Memory corrupted") TAOS_DEFINE_ERROR(TSDB_CODE_FILE_CORRUPTED, "Data file corrupted") TAOS_DEFINE_ERROR(TSDB_CODE_CHECKSUM_ERROR, "Checksum error") -TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_MSG, "Invalid config message") +TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_MSG, "Invalid message") TAOS_DEFINE_ERROR(TSDB_CODE_MSG_NOT_PROCESSED, "Message not processed") TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_PARA, "Invalid parameters") TAOS_DEFINE_ERROR(TSDB_CODE_REPEAT_INIT, "Repeat initialization") From e89dc53200a1451b28ef546443c14e47a0eced31 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 14 Feb 2022 15:27:38 +0800 Subject: [PATCH 2/7] seralize db msg --- include/common/tmsg.h | 131 +++--- source/client/src/clientHb.c | 39 +- source/common/src/tmsg.c | 279 +++++++++--- source/dnode/mnode/impl/inc/mndDb.h | 2 +- source/dnode/mnode/impl/src/mndDb.c | 407 +++++++++++------- source/dnode/mnode/impl/src/mndProfile.c | 37 +- source/dnode/mnode/impl/src/mndTrans.c | 8 +- source/dnode/mnode/impl/test/db/db.cpp | 101 ++--- .../dnode/mnode/impl/test/profile/profile.cpp | 26 +- source/libs/catalog/test/catalogTests.cpp | 57 +-- source/libs/qcom/src/querymsg.c | 55 +-- 11 files changed, 713 insertions(+), 429 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 1bd347fc24..52fa0c28f8 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -328,6 +328,26 @@ static FORCE_INLINE void* taosDecodeSEpSet(void* buf, SEpSet* pEp) { return buf; } +static FORCE_INLINE int32_t tEncodeSEpSet(SCoder* pEncoder, const SEpSet* pEp) { + if (tEncodeI8(pEncoder, pEp->inUse) < 0) return -1; + if (tEncodeI8(pEncoder, pEp->numOfEps) < 0) return -1; + for (int i = 0; i < TSDB_MAX_REPLICA; i++) { + if (tEncodeU16(pEncoder, pEp->eps[i].port) < 0) return -1; + if (tEncodeCStr(pEncoder, pEp->eps[i].fqdn) < 0) return -1; + } + return 0; +} + +static FORCE_INLINE int32_t tDecodeSEpSet(SCoder* pDecoder, SEpSet* pEp) { + if (tDecodeI8(pDecoder, &pEp->inUse) < 0) return -1; + if (tDecodeI8(pDecoder, &pEp->numOfEps) < 0) return -1; + for (int i = 0; i < TSDB_MAX_REPLICA; i++) { + if (tDecodeU16(pDecoder, &pEp->eps[i].port) < 0) return -1; + if (tDecodeCStrTo(pDecoder, pEp->eps[i].fqdn) < 0) return -1; + } + return 0; +} + typedef struct { int32_t acctId; int64_t clusterId; @@ -612,6 +632,27 @@ typedef struct { int32_t tSerializeSUseDbReq(void* buf, int32_t bufLen, SUseDbReq* pReq); int32_t tDeserializeSUseDbReq(void* buf, int32_t bufLen, SUseDbReq* pReq); +typedef struct { + char db[TSDB_DB_FNAME_LEN]; + uint64_t uid; + int32_t vgVersion; + int32_t vgNum; + int8_t hashMethod; + SArray* pVgroupInfos; // Array of SVgroupInfo +} SUseDbRsp; + +int32_t tSerializeSUseDbRsp(void* buf, int32_t bufLen, SUseDbRsp* pRsp); +int32_t tDeserializeSUseDbRsp(void* buf, int32_t bufLen, SUseDbRsp* pRsp); +void tFreeSUsedbRsp(SUseDbRsp* pRsp); + +typedef struct { + SArray* pArray; // Array of SUseDbRsp +} SUseDbBatchRsp; + +int32_t tSerializeSUseDbBatchRsp(void* buf, int32_t bufLen, SUseDbBatchRsp* pRsp); +int32_t tDeserializeSUseDbBatchRsp(void* buf, int32_t bufLen, SUseDbBatchRsp* pRsp); +void tFreeSUseDbBatchRsp(SUseDbBatchRsp* pRsp); + typedef struct { char db[TSDB_DB_FNAME_LEN]; } SSyncDbReq, SCompactDbReq; @@ -841,15 +882,6 @@ typedef struct { char* data; } STagData; -typedef struct { - char db[TSDB_DB_FNAME_LEN]; - uint64_t uid; - int32_t vgVersion; - int32_t vgNum; - int8_t hashMethod; - SVgroupInfo vgroupInfo[]; -} SUseDbRsp; - /* * sql: show tables like '%a_%' * payload is the query condition, e.g., '%a_%' @@ -1535,43 +1567,30 @@ typedef struct { SArray* rsps; // SArray } SClientHbBatchRsp; -static FORCE_INLINE uint32_t hbKeyHashFunc(const char* key, uint32_t keyLen) { - return taosIntHash_64(key, keyLen); -} +static FORCE_INLINE uint32_t hbKeyHashFunc(const char* key, uint32_t keyLen) { return taosIntHash_64(key, keyLen); } -int tSerializeSClientHbReq(void** buf, const SClientHbReq* pReq); -void* tDeserializeSClientHbReq(void* buf, SClientHbReq* pReq); - -int tSerializeSClientHbRsp(void** buf, const SClientHbRsp* pRsp); -void* tDeserializeSClientHbRsp(void* buf, SClientHbRsp* pRsp); - - -static FORCE_INLINE void tFreeReqKvHash(SHashObj* info) { - void *pIter = taosHashIterate(info, NULL); +static FORCE_INLINE void tFreeReqKvHash(SHashObj* info) { + void* pIter = taosHashIterate(info, NULL); while (pIter != NULL) { SKv* kv = (SKv*)pIter; - tfree(kv->value); - pIter = taosHashIterate(info, pIter); } } - -static FORCE_INLINE void tFreeClientHbReq(void *pReq) { +static FORCE_INLINE void tFreeClientHbReq(void* pReq) { SClientHbReq* req = (SClientHbReq*)pReq; if (req->info) { tFreeReqKvHash(req->info); - taosHashCleanup(req->info); } } -int tSerializeSClientHbBatchReq(void** buf, const SClientHbBatchReq* pReq); -void* tDeserializeSClientHbBatchReq(void* buf, SClientHbBatchReq* pReq); +int32_t tSerializeSClientHbBatchReq(void* buf, int32_t bufLen, const SClientHbBatchReq* pReq); +int32_t tDeserializeSClientHbBatchReq(void* buf, int32_t bufLen, SClientHbBatchReq* pReq); static FORCE_INLINE void tFreeClientHbBatchReq(void* pReq, bool deep) { - SClientHbBatchReq *req = (SClientHbBatchReq*)pReq; + SClientHbBatchReq* req = (SClientHbBatchReq*)pReq; if (deep) { taosArrayDestroyEx(req->reqs, tFreeClientHbReq); } else { @@ -1580,54 +1599,52 @@ static FORCE_INLINE void tFreeClientHbBatchReq(void* pReq, bool deep) { free(pReq); } -static FORCE_INLINE void tFreeClientKv(void *pKv) { - SKv *kv = (SKv *)pKv; +static FORCE_INLINE void tFreeClientKv(void* pKv) { + SKv* kv = (SKv*)pKv; if (kv) { tfree(kv->value); } } -static FORCE_INLINE void tFreeClientHbRsp(void *pRsp) { +static FORCE_INLINE void tFreeClientHbRsp(void* pRsp) { SClientHbRsp* rsp = (SClientHbRsp*)pRsp; if (rsp->info) taosArrayDestroyEx(rsp->info, tFreeClientKv); } - static FORCE_INLINE void tFreeClientHbBatchRsp(void* pRsp) { - SClientHbBatchRsp *rsp = (SClientHbBatchRsp*)pRsp; + SClientHbBatchRsp* rsp = (SClientHbBatchRsp*)pRsp; taosArrayDestroyEx(rsp->rsps, tFreeClientHbRsp); } +int32_t tSerializeSClientHbBatchRsp(void* buf, int32_t bufLen, const SClientHbBatchRsp* pBatchRsp); +int32_t tDeserializeSClientHbBatchRsp(void* buf, int32_t bufLen, SClientHbBatchRsp* pBatchRsp); -int tSerializeSClientHbBatchRsp(void** buf, const SClientHbBatchRsp* pBatchRsp); -void* tDeserializeSClientHbBatchRsp(void* buf, SClientHbBatchRsp* pBatchRsp); - -static FORCE_INLINE int taosEncodeSKv(void** buf, const SKv* pKv) { - int tlen = 0; - tlen += taosEncodeFixedI32(buf, pKv->key); - tlen += taosEncodeFixedI32(buf, pKv->valueLen); - tlen += taosEncodeBinary(buf, pKv->value, pKv->valueLen); - return tlen; +static FORCE_INLINE int32_t tEncodeSKv(SCoder* pEncoder, const SKv* pKv) { + if (tEncodeI32(pEncoder, pKv->key) < 0) return -1; + if (tEncodeI32(pEncoder, pKv->valueLen) < 0) return -1; + if (tEncodeCStrWithLen(pEncoder, (const char*)pKv->value, pKv->valueLen) < 0) return -1; + return 0; } -static FORCE_INLINE void* taosDecodeSKv(void* buf, SKv* pKv) { - buf = taosDecodeFixedI32(buf, &pKv->key); - buf = taosDecodeFixedI32(buf, &pKv->valueLen); - buf = taosDecodeBinary(buf, &pKv->value, pKv->valueLen); - return buf; +static FORCE_INLINE int32_t tDecodeSKv(SCoder* pDecoder, SKv* pKv) { + if (tDecodeI32(pDecoder, &pKv->key) < 0) return -1; + if (tDecodeI32(pDecoder, &pKv->valueLen) < 0) return -1; + pKv->value = malloc(pKv->valueLen + 1); + if (pKv->value == NULL) return -1; + if (tDecodeCStrTo(pDecoder, (char*)pKv->value) < 0) return -1; + return 0; } -static FORCE_INLINE int taosEncodeSClientHbKey(void** buf, const SClientHbKey* pKey) { - int tlen = 0; - tlen += taosEncodeFixedI32(buf, pKey->connId); - tlen += taosEncodeFixedI32(buf, pKey->hbType); - return tlen; +static FORCE_INLINE int32_t tEncodeSClientHbKey(SCoder* pEncoder, const SClientHbKey* pKey) { + if (tEncodeI32(pEncoder, pKey->connId) < 0) return -1; + if (tEncodeI32(pEncoder, pKey->hbType) < 0) return -1; + return 0; } -static FORCE_INLINE void* taosDecodeSClientHbKey(void* buf, SClientHbKey* pKey) { - buf = taosDecodeFixedI32(buf, &pKey->connId); - buf = taosDecodeFixedI32(buf, &pKey->hbType); - return buf; +static FORCE_INLINE int32_t tDecodeSClientHbKey(SCoder* pDecoder, SClientHbKey* pKey) { + if (tDecodeI32(pDecoder, &pKey->connId) < 0) return -1; + if (tDecodeI32(pDecoder, &pKey->hbType) < 0) return -1; + return 0; } typedef struct SMqHbVgInfo { diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index 3e1af765b0..7920ae0658 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -30,14 +30,16 @@ static int32_t hbMqHbRspHandle(struct SAppHbMgr *pAppHbMgr, SClientHbRsp* pRsp) static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) { int32_t msgLen = 0; int32_t code = 0; - - while (msgLen < valueLen) { - SUseDbRsp *rsp = (SUseDbRsp *)((char *)value + msgLen); - rsp->vgVersion = ntohl(rsp->vgVersion); - rsp->vgNum = ntohl(rsp->vgNum); - rsp->uid = be64toh(rsp->uid); + SUseDbBatchRsp batchUseRsp = {0}; + if (tDeserializeSUseDbBatchRsp(value, valueLen, &batchUseRsp) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } + int32_t numOfBatchs = taosArrayGetSize(batchUseRsp.pArray); + for (int32_t i = 0; i < numOfBatchs; ++i) { + SUseDbRsp *rsp = taosArrayGet(batchUseRsp.pArray, i); tscDebug("hb db rsp, db:%s, vgVersion:%d, uid:%"PRIx64, rsp->db, rsp->vgVersion, rsp->uid); if (rsp->vgVersion < 0) { @@ -52,22 +54,15 @@ static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog return TSDB_CODE_TSC_OUT_OF_MEMORY; } - for (int32_t i = 0; i < rsp->vgNum; ++i) { - rsp->vgroupInfo[i].vgId = ntohl(rsp->vgroupInfo[i].vgId); - rsp->vgroupInfo[i].hashBegin = ntohl(rsp->vgroupInfo[i].hashBegin); - rsp->vgroupInfo[i].hashEnd = ntohl(rsp->vgroupInfo[i].hashEnd); - - for (int32_t n = 0; n < rsp->vgroupInfo[i].epset.numOfEps; ++n) { - rsp->vgroupInfo[i].epset.eps[n].port = ntohs(rsp->vgroupInfo[i].epset.eps[n].port); - } - - if (0 != taosHashPut(vgInfo.vgHash, &rsp->vgroupInfo[i].vgId, sizeof(rsp->vgroupInfo[i].vgId), &rsp->vgroupInfo[i], sizeof(rsp->vgroupInfo[i]))) { + for (int32_t j = 0; j < rsp->vgNum; ++j) { + SVgroupInfo *pInfo = taosArrayGet(rsp->pVgroupInfos, j); + if (taosHashPut(vgInfo.vgHash, &pInfo->vgId, sizeof(int32_t), pInfo, sizeof(SVgroupInfo)) != 0) { tscError("hash push failed, errno:%d", errno); taosHashCleanup(vgInfo.vgHash); return TSDB_CODE_TSC_OUT_OF_MEMORY; } - } - + } + code = catalogUpdateDBVgroup(pCatalog, rsp->db, rsp->uid, &vgInfo); if (code) { taosHashCleanup(vgInfo.vgHash); @@ -201,9 +196,10 @@ static int32_t hbMqAsyncCallBack(void* param, const SDataBuf* pMsg, int32_t code tfree(param); return -1; } + char *key = (char *)param; SClientHbBatchRsp pRsp = {0}; - tDeserializeSClientHbBatchRsp(pMsg->pData, &pRsp); + tDeserializeSClientHbBatchRsp(pMsg->pData, pMsg->len, &pRsp); int32_t rspNum = taosArrayGetSize(pRsp.rsps); @@ -416,7 +412,7 @@ static void* hbThreadFunc(void* param) { if (pReq == NULL) { continue; } - int tlen = tSerializeSClientHbBatchReq(NULL, pReq); + int tlen = tSerializeSClientHbBatchReq(NULL, 0, pReq); void *buf = malloc(tlen); if (buf == NULL) { terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; @@ -424,8 +420,7 @@ static void* hbThreadFunc(void* param) { hbClearReqInfo(pAppHbMgr); break; } - void *abuf = buf; - tSerializeSClientHbBatchReq(&abuf, pReq); + tSerializeSClientHbBatchReq(buf, tlen, pReq); SMsgSendInfo *pInfo = malloc(sizeof(SMsgSendInfo)); if (pInfo == NULL) { terrno = TSDB_CODE_TSC_OUT_OF_MEMORY; diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 20e1da9d07..68924e1be4 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -85,118 +85,156 @@ STSRow *tGetSubmitBlkNext(SSubmitBlkIter *pIter) { } } -int32_t tSerializeSClientHbReq(void **buf, const SClientHbReq *pReq) { - int32_t tlen = 0; - tlen += taosEncodeSClientHbKey(buf, &pReq->connKey); +static int32_t tSerializeSClientHbReq(SCoder *pEncoder, const SClientHbReq *pReq) { + if (tEncodeSClientHbKey(pEncoder, &pReq->connKey) < 0) return -1; int32_t kvNum = taosHashGetSize(pReq->info); - tlen += taosEncodeFixedI32(buf, kvNum); - SKv *kv; + if (tEncodeI32(pEncoder, kvNum) < 0) return -1; void *pIter = taosHashIterate(pReq->info, NULL); while (pIter != NULL) { - kv = pIter; - tlen += taosEncodeSKv(buf, kv); - + SKv *kv = pIter; + if (tEncodeSKv(pEncoder, kv) < 0) return -1; pIter = taosHashIterate(pReq->info, pIter); } - return tlen; + + return 0; } -void *tDeserializeSClientHbReq(void *buf, SClientHbReq *pReq) { - buf = taosDecodeSClientHbKey(buf, &pReq->connKey); +static int32_t tDeserializeSClientHbReq(SCoder *pDecoder, SClientHbReq *pReq) { + if (tDecodeSClientHbKey(pDecoder, &pReq->connKey) < 0) return -1; - // TODO: error handling - int32_t kvNum; - buf = taosDecodeFixedI32(buf, &kvNum); + int32_t kvNum = 0; + if (tDecodeI32(pDecoder, &kvNum) < 0) return -1; if (pReq->info == NULL) { pReq->info = taosHashInit(kvNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), true, HASH_NO_LOCK); } + if (pReq->info == NULL) return -1; for (int32_t i = 0; i < kvNum; i++) { - SKv kv; - buf = taosDecodeSKv(buf, &kv); + SKv kv = {0}; + if (tDecodeSKv(pDecoder, &kv) < 0) return -1; taosHashPut(pReq->info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); } - return buf; + return 0; } -int32_t tSerializeSClientHbRsp(void **buf, const SClientHbRsp *pRsp) { - int32_t tlen = 0; +static int32_t tSerializeSClientHbRsp(SCoder *pEncoder, const SClientHbRsp *pRsp) { + if (tEncodeSClientHbKey(pEncoder, &pRsp->connKey) < 0) return -1; + if (tEncodeI32(pEncoder, pRsp->status) < 0) return -1; + int32_t kvNum = taosArrayGetSize(pRsp->info); - tlen += taosEncodeSClientHbKey(buf, &pRsp->connKey); - tlen += taosEncodeFixedI32(buf, pRsp->status); - tlen += taosEncodeFixedI32(buf, kvNum); + if (tEncodeI32(pEncoder, kvNum) < 0) return -1; for (int32_t i = 0; i < kvNum; i++) { - SKv *kv = (SKv *)taosArrayGet(pRsp->info, i); - tlen += taosEncodeSKv(buf, kv); + SKv *kv = taosArrayGet(pRsp->info, i); + if (tEncodeSKv(pEncoder, kv) < 0) return -1; } - return tlen; + + return 0; } -void *tDeserializeSClientHbRsp(void *buf, SClientHbRsp *pRsp) { +static int32_t tDeserializeSClientHbRsp(SCoder *pDecoder, SClientHbRsp *pRsp) { + if (tDecodeSClientHbKey(pDecoder, &pRsp->connKey) < 0) return -1; + if (tDecodeI32(pDecoder, &pRsp->status) < 0) return -1; + int32_t kvNum = 0; - buf = taosDecodeSClientHbKey(buf, &pRsp->connKey); - buf = taosDecodeFixedI32(buf, &pRsp->status); - buf = taosDecodeFixedI32(buf, &kvNum); + if (tDecodeI32(pDecoder, &kvNum) < 0) return -1; pRsp->info = taosArrayInit(kvNum, sizeof(SKv)); + if (pRsp->info == NULL) return -1; for (int32_t i = 0; i < kvNum; i++) { SKv kv = {0}; - buf = taosDecodeSKv(buf, &kv); + tDecodeSKv(pDecoder, &kv); taosArrayPush(pRsp->info, &kv); } - return buf; + return 0; } -int32_t tSerializeSClientHbBatchReq(void **buf, const SClientHbBatchReq *pBatchReq) { - int32_t tlen = 0; - tlen += taosEncodeFixedI64(buf, pBatchReq->reqId); +int32_t tSerializeSClientHbBatchReq(void *buf, int32_t bufLen, const SClientHbBatchReq *pBatchReq) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; + if (tEncodeI64(&encoder, pBatchReq->reqId) < 0) return -1; + int32_t reqNum = taosArrayGetSize(pBatchReq->reqs); - tlen += taosEncodeFixedI32(buf, reqNum); + if (tEncodeI32(&encoder, reqNum) < 0) return -1; for (int32_t i = 0; i < reqNum; i++) { SClientHbReq *pReq = taosArrayGet(pBatchReq->reqs, i); - tlen += tSerializeSClientHbReq(buf, pReq); + if (tSerializeSClientHbReq(&encoder, pReq) < 0) return -1; } + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); return tlen; } -void *tDeserializeSClientHbBatchReq(void *buf, SClientHbBatchReq *pBatchReq) { - buf = taosDecodeFixedI64(buf, &pBatchReq->reqId); - if (pBatchReq->reqs == NULL) { - pBatchReq->reqs = taosArrayInit(0, sizeof(SClientHbReq)); - } +int32_t tDeserializeSClientHbBatchReq(void *buf, int32_t bufLen, SClientHbBatchReq *pBatchReq) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); - int32_t reqNum; - buf = taosDecodeFixedI32(buf, &reqNum); + if (tStartDecode(&decoder) < 0) return -1; + if (tDecodeI64(&decoder, &pBatchReq->reqId) < 0) return -1; + + int32_t reqNum = 0; + if (tDecodeI32(&decoder, &reqNum) < 0) return -1; + if (pBatchReq->reqs == NULL) { + pBatchReq->reqs = taosArrayInit(reqNum, sizeof(SClientHbReq)); + } for (int32_t i = 0; i < reqNum; i++) { SClientHbReq req = {0}; - buf = tDeserializeSClientHbReq(buf, &req); + tDeserializeSClientHbReq(&decoder, &req); taosArrayPush(pBatchReq->reqs, &req); } - return buf; + + tEndDecode(&decoder); + tCoderClear(&decoder); + return 0; } -int32_t tSerializeSClientHbBatchRsp(void **buf, const SClientHbBatchRsp *pBatchRsp) { - int32_t tlen = 0; - int32_t sz = taosArrayGetSize(pBatchRsp->rsps); - tlen += taosEncodeFixedI32(buf, sz); - for (int32_t i = 0; i < sz; i++) { +int32_t tSerializeSClientHbBatchRsp(void *buf, int32_t bufLen, const SClientHbBatchRsp *pBatchRsp) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; + if (tEncodeI64(&encoder, pBatchRsp->reqId) < 0) return -1; + if (tEncodeI64(&encoder, pBatchRsp->rspId) < 0) return -1; + + int32_t rspNum = taosArrayGetSize(pBatchRsp->rsps); + if (tEncodeI32(&encoder, rspNum) < 0) return -1; + for (int32_t i = 0; i < rspNum; i++) { SClientHbRsp *pRsp = taosArrayGet(pBatchRsp->rsps, i); - tlen += tSerializeSClientHbRsp(buf, pRsp); + if (tSerializeSClientHbRsp(&encoder, pRsp) < 0) return -1; } + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); return tlen; } -void *tDeserializeSClientHbBatchRsp(void *buf, SClientHbBatchRsp *pBatchRsp) { - int32_t sz; - buf = taosDecodeFixedI32(buf, &sz); - pBatchRsp->rsps = taosArrayInit(sz, sizeof(SClientHbRsp)); - for (int32_t i = 0; i < sz; i++) { +int32_t tDeserializeSClientHbBatchRsp(void *buf, int32_t bufLen, SClientHbBatchRsp *pBatchRsp) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + if (tDecodeI64(&decoder, &pBatchRsp->reqId) < 0) return -1; + if (tDecodeI64(&decoder, &pBatchRsp->rspId) < 0) return -1; + + int32_t rspNum = 0; + if (tDecodeI32(&decoder, &rspNum) < 0) return -1; + if (pBatchRsp->rsps == NULL) { + pBatchRsp->rsps = taosArrayInit(rspNum, sizeof(SClientHbReq)); + } + for (int32_t i = 0; i < rspNum; i++) { SClientHbRsp rsp = {0}; - buf = tDeserializeSClientHbRsp(buf, &rsp); + tDeserializeSClientHbRsp(&decoder, &rsp); taosArrayPush(pBatchRsp->rsps, &rsp); } - return buf; + + tEndDecode(&decoder); + tCoderClear(&decoder); + return 0; } int32_t tSerializeSVCreateTbReq(void **buf, SVCreateTbReq *pReq) { @@ -1306,4 +1344,129 @@ int32_t tDeserializeSSyncDbReq(void *buf, int32_t bufLen, SSyncDbReq *pReq) { tCoderClear(&decoder); return 0; +} + +static int32_t tSerializeSUseDbRspImp(SCoder *pEncoder, SUseDbRsp *pRsp) { + if (tEncodeCStr(pEncoder, pRsp->db) < 0) return -1; + if (tEncodeU64(pEncoder, pRsp->uid) < 0) return -1; + if (tEncodeI32(pEncoder, pRsp->vgVersion) < 0) return -1; + if (tEncodeI32(pEncoder, pRsp->vgNum) < 0) return -1; + if (tEncodeI8(pEncoder, pRsp->hashMethod) < 0) return -1; + + for (int32_t i = 0; i < pRsp->vgNum; ++i) { + SVgroupInfo *pVgInfo = taosArrayGet(pRsp->pVgroupInfos, i); + if (tEncodeI32(pEncoder, pVgInfo->vgId) < 0) return -1; + if (tEncodeU32(pEncoder, pVgInfo->hashBegin) < 0) return -1; + if (tEncodeU32(pEncoder, pVgInfo->hashEnd) < 0) return -1; + if (tEncodeSEpSet(pEncoder, &pVgInfo->epset) < 0) return -1; + } + + return 0; +} + +int32_t tSerializeSUseDbRsp(void *buf, int32_t bufLen, SUseDbRsp *pRsp) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; + if (tSerializeSUseDbRspImp(&encoder, pRsp) < 0) return -1; + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); + return tlen; +} + +int32_t tSerializeSUseDbBatchRsp(void *buf, int32_t bufLen, SUseDbBatchRsp *pRsp) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; + + int32_t numOfBatch = taosArrayGetSize(pRsp->pArray); + if (tEncodeI32(&encoder, numOfBatch) < 0) return -1; + for (int32_t i = 0; i < numOfBatch; ++i) { + SUseDbRsp *pUsedbRsp = taosArrayGet(pRsp->pArray, i); + if (tSerializeSUseDbRspImp(&encoder, pUsedbRsp) < 0) return -1; + } + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); + return tlen; +} + +int32_t tDeserializeSUseDbRspImp(SCoder *pDecoder, SUseDbRsp *pRsp) { + if (tDecodeCStrTo(pDecoder, pRsp->db) < 0) return -1; + if (tDecodeU64(pDecoder, &pRsp->uid) < 0) return -1; + if (tDecodeI32(pDecoder, &pRsp->vgVersion) < 0) return -1; + if (tDecodeI32(pDecoder, &pRsp->vgNum) < 0) return -1; + if (tDecodeI8(pDecoder, &pRsp->hashMethod) < 0) return -1; + + pRsp->pVgroupInfos = taosArrayInit(pRsp->vgNum, sizeof(SVgroupInfo)); + if (pRsp->pVgroupInfos == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + for (int32_t i = 0; i < pRsp->vgNum; ++i) { + SVgroupInfo vgInfo = {0}; + if (tDecodeI32(pDecoder, &vgInfo.vgId) < 0) return -1; + if (tDecodeU32(pDecoder, &vgInfo.hashBegin) < 0) return -1; + if (tDecodeU32(pDecoder, &vgInfo.hashEnd) < 0) return -1; + if (tDecodeSEpSet(pDecoder, &vgInfo.epset) < 0) return -1; + taosArrayPush(pRsp->pVgroupInfos, &vgInfo); + } + + return 0; +} + +int32_t tDeserializeSUseDbRsp(void *buf, int32_t bufLen, SUseDbRsp *pRsp) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + if (tDeserializeSUseDbRspImp(&decoder, pRsp) < 0) return -1; + tEndDecode(&decoder); + + tCoderClear(&decoder); + return 0; +} + +int32_t tDeserializeSUseDbBatchRsp(void *buf, int32_t bufLen, SUseDbBatchRsp *pRsp) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + + int32_t numOfBatch = taosArrayGetSize(pRsp->pArray); + if (tDecodeI32(&decoder, &numOfBatch) < 0) return -1; + + pRsp->pArray = taosArrayInit(numOfBatch, sizeof(SUseDbBatchRsp)); + if (pRsp->pArray == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return -1; + } + + for (int32_t i = 0; i < numOfBatch; ++i) { + SUseDbRsp usedbRsp = {0}; + if (tDeserializeSUseDbRspImp(&decoder, &usedbRsp) < 0) return -1; + taosArrayPush(pRsp->pArray, &usedbRsp); + } + tEndDecode(&decoder); + + tCoderClear(&decoder); + return 0; +} + +void tFreeSUsedbRsp(SUseDbRsp *pRsp) { taosArrayDestroy(pRsp->pVgroupInfos); } + +void tFreeSUseDbBatchRsp(SUseDbBatchRsp *pRsp) { + int32_t numOfBatch = taosArrayGetSize(pRsp->pArray); + for (int32_t i = 0; i < numOfBatch; ++i) { + SUseDbRsp *pUsedbRsp = taosArrayGet(pRsp->pArray, i); + tFreeSUsedbRsp(pUsedbRsp); + } + + taosArrayDestroy(pRsp->pArray); } \ No newline at end of file diff --git a/source/dnode/mnode/impl/inc/mndDb.h b/source/dnode/mnode/impl/inc/mndDb.h index 5a3e6ed26e..ff0095f9f4 100644 --- a/source/dnode/mnode/impl/inc/mndDb.h +++ b/source/dnode/mnode/impl/inc/mndDb.h @@ -26,7 +26,7 @@ int32_t mndInitDb(SMnode *pMnode); void mndCleanupDb(SMnode *pMnode); SDbObj *mndAcquireDb(SMnode *pMnode, char *db); void mndReleaseDb(SMnode *pMnode, SDbObj *pDb); -int32_t mndValidateDBInfo(SMnode *pMnode, SDbVgVersion *dbs, int32_t num, void **rsp, int32_t *rspLen); +int32_t mndValidateDbInfo(SMnode *pMnode, SDbVgVersion *pDbs, int32_t numOfDbs, void **ppRsp, int32_t *pRspLen); #ifdef __cplusplus } diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index 1a01c3852a..56d743cd5a 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -634,43 +634,56 @@ UPDATE_DB_OVER: } static int32_t mndProcessAlterDbReq(SMnodeMsg *pReq) { - SMnode *pMnode = pReq->pMnode; - SAlterDbReq *pAlter = pReq->rpcMsg.pCont; - pAlter->totalBlocks = htonl(pAlter->totalBlocks); - pAlter->daysToKeep0 = htonl(pAlter->daysToKeep0); - pAlter->daysToKeep1 = htonl(pAlter->daysToKeep1); - pAlter->daysToKeep2 = htonl(pAlter->daysToKeep2); - pAlter->fsyncPeriod = htonl(pAlter->fsyncPeriod); + SMnode *pMnode = pReq->pMnode; + int32_t code = -1; + SDbObj *pDb = NULL; + SUserObj *pUser = NULL; + SAlterDbReq alterReq = {0}; - mDebug("db:%s, start to alter", pAlter->db); + if (tDeserializeSAlterDbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &alterReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + goto ALTER_DB_OVER; + } - SDbObj *pDb = mndAcquireDb(pMnode, pAlter->db); + mDebug("db:%s, start to alter", alterReq.db); + + pDb = mndAcquireDb(pMnode, alterReq.db); if (pDb == NULL) { - mError("db:%s, failed to alter since %s", pAlter->db, terrstr()); - return TSDB_CODE_MND_DB_NOT_EXIST; + terrno = TSDB_CODE_MND_DB_NOT_EXIST; + goto ALTER_DB_OVER; + } + + pUser = mndAcquireUser(pMnode, pReq->user); + if (pUser == NULL) { + goto ALTER_DB_OVER; + } + + if (mndCheckAlterDropCompactSyncDbAuth(pUser, pDb) != 0) { + goto ALTER_DB_OVER; } SDbObj dbObj = {0}; memcpy(&dbObj, pDb, sizeof(SDbObj)); - int32_t code = mndSetDbCfgFromAlterDbReq(&dbObj, pAlter); + code = mndSetDbCfgFromAlterDbReq(&dbObj, &alterReq); if (code != 0) { - mndReleaseDb(pMnode, pDb); - mError("db:%s, failed to alter since %s", pAlter->db, tstrerror(code)); - return code; + goto ALTER_DB_OVER; } dbObj.cfgVersion++; dbObj.updateTime = taosGetTimestampMs(); code = mndUpdateDb(pMnode, pReq, pDb, &dbObj); - mndReleaseDb(pMnode, pDb); + if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS; - if (code != 0) { - mError("db:%s, failed to alter since %s", pAlter->db, tstrerror(code)); - return code; +ALTER_DB_OVER: + if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) { + mError("db:%s, failed to alter since %s", alterReq.db, terrstr()); } - return TSDB_CODE_MND_ACTION_IN_PROGRESS; + mndReleaseDb(pMnode, pDb); + mndReleaseUser(pMnode, pUser); + + return code; } static int32_t mndSetDropDbRedoLogs(SMnode *pMnode, STrans *pTrans, SDbObj *pDb) { @@ -772,11 +785,18 @@ static int32_t mndDropDb(SMnode *pMnode, SMnodeMsg *pReq, SDbObj *pDb) { if (mndSetDropDbCommitLogs(pMnode, pTrans, pDb) != 0) goto DROP_DB_OVER; if (mndSetDropDbRedoActions(pMnode, pTrans, pDb) != 0) goto DROP_DB_OVER; - int32_t rspLen = sizeof(SDropDbRsp); - SDropDbRsp *pRsp = rpcMallocCont(rspLen); - if (pRsp == NULL) goto DROP_DB_OVER; - memcpy(pRsp->db, pDb->name, TSDB_DB_FNAME_LEN); - pRsp->uid = htobe64(pDb->uid); + SDropDbRsp dropRsp = {0}; + memcpy(dropRsp.db, pDb->name, TSDB_DB_FNAME_LEN); + dropRsp.uid = pDb->uid; + + int32_t rspLen = tSerializeSDropDbRsp(NULL, 0, &dropRsp); + void *pRsp = malloc(rspLen); + if (pRsp == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + goto DROP_DB_OVER; + } + tSerializeSDropDbRsp(pRsp, rspLen, &dropRsp); + mndTransSetRpcRsp(pTrans, pRsp, rspLen); if (mndTransPrepare(pMnode, pTrans) != 0) goto DROP_DB_OVER; @@ -789,35 +809,54 @@ DROP_DB_OVER: } static int32_t mndProcessDropDbReq(SMnodeMsg *pReq) { - SMnode *pMnode = pReq->pMnode; - SDropDbReq *pDrop = pReq->rpcMsg.pCont; + SMnode *pMnode = pReq->pMnode; + int32_t code = -1; + SDbObj *pDb = NULL; + SUserObj *pUser = NULL; + SDropDbReq dropReq = {0}; - mDebug("db:%s, start to drop", pDrop->db); + if (tDeserializeSDropDbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &dropReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + goto DROP_DB_OVER; + } - SDbObj *pDb = mndAcquireDb(pMnode, pDrop->db); + mDebug("db:%s, start to drop", dropReq.db); + + pDb = mndAcquireDb(pMnode, dropReq.db); if (pDb == NULL) { - if (pDrop->ignoreNotExists) { - mDebug("db:%s, not exist, ignore not exist is set", pDrop->db); - return TSDB_CODE_SUCCESS; + if (dropReq.ignoreNotExists) { + code = 0; + goto DROP_DB_OVER; } else { terrno = TSDB_CODE_MND_DB_NOT_EXIST; - mError("db:%s, failed to drop since %s", pDrop->db, terrstr()); - return -1; + goto DROP_DB_OVER; } } - int32_t code = mndDropDb(pMnode, pReq, pDb); - mndReleaseDb(pMnode, pDb); - - if (code != 0) { - mError("db:%s, failed to drop since %s", pDrop->db, terrstr()); - return code; + pUser = mndAcquireUser(pMnode, pReq->user); + if (pUser == NULL) { + goto DROP_DB_OVER; } - return TSDB_CODE_MND_ACTION_IN_PROGRESS; + if (mndCheckAlterDropCompactSyncDbAuth(pUser, pDb) != 0) { + goto DROP_DB_OVER; + } + + code = mndDropDb(pMnode, pReq, pDb); + if (code == 0) code = TSDB_CODE_MND_ACTION_IN_PROGRESS; + +DROP_DB_OVER: + if (code != 0 && code != TSDB_CODE_MND_ACTION_IN_PROGRESS) { + mError("db:%s, failed to drop since %s", dropReq.db, terrstr()); + } + + mndReleaseDb(pMnode, pDb); + mndReleaseUser(pMnode, pUser); + + return code; } -static void mndBuildDBVgroupInfo(SDbObj *pDb, SMnode *pMnode, SVgroupInfo *vgList, int32_t *vgNum) { +static void mndBuildDBVgroupInfo(SDbObj *pDb, SMnode *pMnode, SArray *pVgList) { int32_t vindex = 0; SSdb *pSdb = pMnode->pSdb; @@ -828,168 +867,236 @@ static void mndBuildDBVgroupInfo(SDbObj *pDb, SMnode *pMnode, SVgroupInfo *vgLis if (pIter == NULL) break; if (pVgroup->dbUid == pDb->uid) { - SVgroupInfo *pInfo = &vgList[vindex]; - pInfo->vgId = htonl(pVgroup->vgId); - pInfo->hashBegin = htonl(pVgroup->hashBegin); - pInfo->hashEnd = htonl(pVgroup->hashEnd); - pInfo->epset.numOfEps = pVgroup->replica; + SVgroupInfo vgInfo = {0}; + vgInfo.vgId = pVgroup->vgId; + vgInfo.hashBegin = pVgroup->hashBegin; + vgInfo.hashEnd = pVgroup->hashEnd; + vgInfo.epset.numOfEps = pVgroup->replica; for (int32_t gid = 0; gid < pVgroup->replica; ++gid) { SVnodeGid *pVgid = &pVgroup->vnodeGid[gid]; - SEp *pEp = &pInfo->epset.eps[gid]; + SEp *pEp = &vgInfo.epset.eps[gid]; SDnodeObj *pDnode = mndAcquireDnode(pMnode, pVgid->dnodeId); if (pDnode != NULL) { memcpy(pEp->fqdn, pDnode->fqdn, TSDB_FQDN_LEN); - pEp->port = htons(pDnode->port); + pEp->port = pDnode->port; } mndReleaseDnode(pMnode, pDnode); if (pVgid->role == TAOS_SYNC_STATE_LEADER) { - pInfo->epset.inUse = gid; + vgInfo.epset.inUse = gid; } } vindex++; + taosArrayPush(pVgList, &vgInfo); } sdbRelease(pSdb, pVgroup); } - - *vgNum = vindex; } static int32_t mndProcessUseDbReq(SMnodeMsg *pReq) { - SMnode *pMnode = pReq->pMnode; - SSdb *pSdb = pMnode->pSdb; - SUseDbReq *pUse = pReq->rpcMsg.pCont; - pUse->vgVersion = htonl(pUse->vgVersion); + SMnode *pMnode = pReq->pMnode; + int32_t code = -1; + SDbObj *pDb = NULL; + SUserObj *pUser = NULL; + SUseDbReq usedbReq = {0}; + SUseDbRsp usedbRsp = {0}; - SDbObj *pDb = mndAcquireDb(pMnode, pUse->db); - if (pDb == NULL) { - terrno = TSDB_CODE_MND_DB_NOT_EXIST; - mError("db:%s, failed to process use db req since %s", pUse->db, terrstr()); - return -1; + if (tDeserializeSUseDbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &usedbReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + goto USE_DB_OVER; } - int32_t contLen = sizeof(SUseDbRsp) + pDb->cfg.numOfVgroups * sizeof(SVgroupInfo); - SUseDbRsp *pRsp = rpcMallocCont(contLen); + pDb = mndAcquireDb(pMnode, usedbReq.db); + if (pDb == NULL) { + terrno = TSDB_CODE_MND_DB_NOT_EXIST; + goto USE_DB_OVER; + } + + pUser = mndAcquireUser(pMnode, pReq->user); + if (pUser == NULL) { + goto USE_DB_OVER; + } + + if (mndCheckUseDbAuth(pUser, pDb) != 0) { + goto USE_DB_OVER; + } + + usedbRsp.pVgroupInfos = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SVgroupInfo)); + if (usedbRsp.pVgroupInfos == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + goto USE_DB_OVER; + } + + if (usedbReq.vgVersion < pDb->vgVersion) { + mndBuildDBVgroupInfo(pDb, pMnode, usedbRsp.pVgroupInfos); + } + + memcpy(usedbRsp.db, pDb->name, TSDB_DB_FNAME_LEN); + usedbRsp.uid = pDb->uid; + usedbRsp.vgVersion = pDb->vgVersion; + usedbRsp.vgNum = taosArrayGetSize(usedbRsp.pVgroupInfos); + usedbRsp.hashMethod = pDb->hashMethod; + + int32_t contLen = tSerializeSUseDbRsp(NULL, 0, &usedbRsp); + void *pRsp = rpcMallocCont(contLen); if (pRsp == NULL) { - mndReleaseDb(pMnode, pDb); + terrno = TSDB_CODE_OUT_OF_MEMORY; + goto USE_DB_OVER; + } + + tSerializeSUseDbRsp(pRsp, contLen, &usedbRsp); + + pReq->pCont = pRsp; + pReq->contLen = contLen; + code = 0; + +USE_DB_OVER: + if (code != 0) { + mError("db:%s, failed to process use db req since %s", usedbReq.db, terrstr()); + } + + mndReleaseDb(pMnode, pDb); + mndReleaseUser(pMnode, pUser); + tFreeSUsedbRsp(&usedbRsp); + + return code; +} + +int32_t mndValidateDbInfo(SMnode *pMnode, SDbVgVersion *pDbs, int32_t numOfDbs, void **ppRsp, int32_t *pRspLen) { + SUseDbBatchRsp batchUseRsp = {0}; + batchUseRsp.pArray = taosArrayInit(numOfDbs, sizeof(SUseDbRsp)); + if (batchUseRsp.pArray == NULL) { terrno = TSDB_CODE_OUT_OF_MEMORY; return -1; } - int32_t vgNum = 0; + for (int32_t i = 0; i < numOfDbs; ++i) { + SDbVgVersion *pDbVgVersion = &pDbs[i]; + pDbVgVersion->dbId = htobe64(pDbVgVersion->dbId); + pDbVgVersion->vgVersion = htonl(pDbVgVersion->vgVersion); - if (pUse->vgVersion < pDb->vgVersion) { - mndBuildDBVgroupInfo(pDb, pMnode, pRsp->vgroupInfo, &vgNum); - } - - memcpy(pRsp->db, pDb->name, TSDB_DB_FNAME_LEN); - pRsp->uid = htobe64(pDb->uid); - pRsp->vgVersion = htonl(pDb->vgVersion); - pRsp->vgNum = htonl(vgNum); - pRsp->hashMethod = pDb->hashMethod; - - pReq->pCont = pRsp; - pReq->contLen = contLen; - mndReleaseDb(pMnode, pDb); - - return 0; -} - -int32_t mndValidateDBInfo(SMnode *pMnode, SDbVgVersion *dbs, int32_t num, void **rsp, int32_t *rspLen) { - SSdb *pSdb = pMnode->pSdb; - int32_t bufSize = num * (sizeof(SUseDbRsp) + TSDB_DEFAULT_VN_PER_DB * sizeof(SVgroupInfo)); - void *buf = malloc(bufSize); - int32_t len = 0; - int32_t contLen = 0; - int32_t bufOffset = 0; - SUseDbRsp *pRsp = NULL; - - for (int32_t i = 0; i < num; ++i) { - SDbVgVersion *db = &dbs[i]; - db->dbId = be64toh(db->dbId); - db->vgVersion = ntohl(db->vgVersion); - - len = 0; - - SDbObj *pDb = mndAcquireDb(pMnode, db->dbFName); - if (pDb == NULL) { - mInfo("db %s not exist", db->dbFName); - - len = sizeof(SUseDbRsp); - } else if (pDb->uid != db->dbId || db->vgVersion < pDb->vgVersion) { - len = sizeof(SUseDbRsp) + pDb->cfg.numOfVgroups * sizeof(SVgroupInfo); - } - - if (0 == len) { + SDbObj *pDb = mndAcquireDb(pMnode, pDbVgVersion->dbFName); + if (pDb == NULL || pDbVgVersion->vgVersion >= pDb->vgVersion) { mndReleaseDb(pMnode, pDb); - + mDebug("db:%s, no need to use db", pDbVgVersion->dbFName); continue; } - contLen += len; - - if (contLen > bufSize) { - buf = realloc(buf, contLen); + SUseDbRsp usedbRsp = {0}; + usedbRsp.pVgroupInfos = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SVgroupInfo)); + if (usedbRsp.pVgroupInfos == NULL) { + mndReleaseDb(pMnode, pDb); + mError("db:%s, failed to malloc usedb response", pDb->name); + continue; } - pRsp = (SUseDbRsp *)((char *)buf + bufOffset); - memcpy(pRsp->db, db->dbFName, TSDB_DB_FNAME_LEN); - if (pDb) { - int32_t vgNum = 0; - mndBuildDBVgroupInfo(pDb, pMnode, pRsp->vgroupInfo, &vgNum); - - pRsp->uid = htobe64(pDb->uid); - pRsp->vgVersion = htonl(pDb->vgVersion); - pRsp->vgNum = htonl(vgNum); - pRsp->hashMethod = pDb->hashMethod; - } else { - pRsp->uid = htobe64(db->dbId); - pRsp->vgNum = htonl(0); - pRsp->hashMethod = 0; - pRsp->vgVersion = htonl(-1); - } - - bufOffset += len; + mndBuildDBVgroupInfo(pDb, pMnode, usedbRsp.pVgroupInfos); + memcpy(usedbRsp.db, pDb->name, TSDB_DB_FNAME_LEN); + usedbRsp.uid = pDb->uid; + usedbRsp.vgVersion = pDb->vgVersion; + usedbRsp.vgNum = (int32_t)taosArrayGetSize(usedbRsp.pVgroupInfos); + usedbRsp.hashMethod = pDb->hashMethod; + taosArrayPush(batchUseRsp.pArray, &usedbRsp); mndReleaseDb(pMnode, pDb); } - if (contLen > 0) { - *rsp = buf; - *rspLen = contLen; - } else { - *rsp = NULL; - tfree(buf); - *rspLen = 0; + int32_t rspLen = tSerializeSUseDbBatchRsp(NULL, 0, &batchUseRsp); + void *pRsp = malloc(rspLen); + if (pRsp == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + tFreeSUseDbBatchRsp(&batchUseRsp); + return -1; } + tSerializeSUseDbBatchRsp(pRsp, rspLen, &batchUseRsp); + *ppRsp = pRsp; + *pRspLen = rspLen; + + tFreeSUseDbBatchRsp(&batchUseRsp); return 0; } static int32_t mndProcessSyncDbReq(SMnodeMsg *pReq) { - SMnode *pMnode = pReq->pMnode; - SSyncDbReq *pSync = pReq->rpcMsg.pCont; - SDbObj *pDb = mndAcquireDb(pMnode, pSync->db); + SMnode *pMnode = pReq->pMnode; + int32_t code = -1; + SDbObj *pDb = NULL; + SUserObj *pUser = NULL; + SSyncDbReq syncReq = {0}; + + if (tDeserializeSSyncDbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &syncReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + goto SYNC_DB_OVER; + } + + mDebug("db:%s, start to sync", syncReq.db); + + pDb = mndAcquireDb(pMnode, syncReq.db); if (pDb == NULL) { - mError("db:%s, failed to process sync db req since %s", pSync->db, terrstr()); - return -1; + goto SYNC_DB_OVER; + } + + pUser = mndAcquireUser(pMnode, pReq->user); + if (pUser == NULL) { + goto SYNC_DB_OVER; + } + + if (mndCheckAlterDropCompactSyncDbAuth(pUser, pDb) != 0) { + goto SYNC_DB_OVER; + } + + // code = mndSyncDb(); + +SYNC_DB_OVER: + if (code != 0) { + mError("db:%s, failed to process sync db req since %s", syncReq.db, terrstr()); } mndReleaseDb(pMnode, pDb); - return 0; + mndReleaseUser(pMnode, pUser); + + return code; } static int32_t mndProcessCompactDbReq(SMnodeMsg *pReq) { - SMnode *pMnode = pReq->pMnode; - SCompactDbReq *pCompact = pReq->rpcMsg.pCont; - SDbObj *pDb = mndAcquireDb(pMnode, pCompact->db); + SMnode *pMnode = pReq->pMnode; + int32_t code = -1; + SDbObj *pDb = NULL; + SUserObj *pUser = NULL; + SCompactDbReq compactReq = {0}; + + if (tDeserializeSSyncDbReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &compactReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + goto SYNC_DB_OVER; + } + + mDebug("db:%s, start to sync", compactReq.db); + + pDb = mndAcquireDb(pMnode, compactReq.db); if (pDb == NULL) { - mError("db:%s, failed to process compact db req since %s", pCompact->db, terrstr()); - return -1; + goto SYNC_DB_OVER; + } + + pUser = mndAcquireUser(pMnode, pReq->user); + if (pUser == NULL) { + goto SYNC_DB_OVER; + } + + if (mndCheckAlterDropCompactSyncDbAuth(pUser, pDb) != 0) { + goto SYNC_DB_OVER; + } + + // code = mndSyncDb(); + +SYNC_DB_OVER: + if (code != 0) { + mError("db:%s, failed to process compact db req since %s", compactReq.db, terrstr()); } mndReleaseDb(pMnode, pDb); - return 0; + mndReleaseUser(pMnode, pUser); + + return code; } static int32_t mndGetDbMeta(SMnodeMsg *pReq, SShowObj *pShow, STableMetaRsp *pMeta) { diff --git a/source/dnode/mnode/impl/src/mndProfile.c b/source/dnode/mnode/impl/src/mndProfile.c index d63ade4320..f798928d9c 100644 --- a/source/dnode/mnode/impl/src/mndProfile.c +++ b/source/dnode/mnode/impl/src/mndProfile.c @@ -343,17 +343,21 @@ static SClientHbRsp* mndMqHbBuildRsp(SMnode* pMnode, SClientHbReq* pReq) { static int32_t mndProcessHeartBeatReq(SMnodeMsg *pReq) { SMnode *pMnode = pReq->pMnode; - char *batchReqStr = pReq->rpcMsg.pCont; + SClientHbBatchReq batchReq = {0}; - tDeserializeSClientHbBatchReq(batchReqStr, &batchReq); + if (tDeserializeSClientHbBatchReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &batchReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } + SArray *pArray = batchReq.reqs; - int sz = taosArrayGetSize(pArray); + int32_t sz = taosArrayGetSize(pArray); SClientHbBatchRsp batchRsp = {0}; batchRsp.rsps = taosArrayInit(0, sizeof(SClientHbRsp)); for (int i = 0; i < sz; i++) { - SClientHbReq* pHbReq = taosArrayGet(pArray, i); + SClientHbReq *pHbReq = taosArrayGet(pArray, i); if (pHbReq->connKey.hbType == HEARTBEAT_TYPE_QUERY) { int32_t kvNum = taosHashGetSize(pHbReq->info); if (NULL == pHbReq->info || kvNum <= 0) { @@ -364,13 +368,13 @@ static int32_t mndProcessHeartBeatReq(SMnodeMsg *pReq) { void *pIter = taosHashIterate(pHbReq->info, NULL); while (pIter != NULL) { - SKv* kv = pIter; - + SKv *kv = pIter; + switch (kv->key) { case HEARTBEAT_KEY_DBINFO: { - void *rspMsg = NULL; + void *rspMsg = NULL; int32_t rspLen = 0; - mndValidateDBInfo(pMnode, (SDbVgVersion *)kv->value, kv->valueLen/sizeof(SDbVgVersion), &rspMsg, &rspLen); + mndValidateDbInfo(pMnode, kv->value, kv->valueLen / sizeof(SDbVgVersion), &rspMsg, &rspLen); if (rspMsg && rspLen > 0) { SKv kv = {.key = HEARTBEAT_KEY_DBINFO, .valueLen = rspLen, .value = rspMsg}; taosArrayPush(hbRsp.info, &kv); @@ -378,9 +382,9 @@ static int32_t mndProcessHeartBeatReq(SMnodeMsg *pReq) { break; } case HEARTBEAT_KEY_STBINFO: { - void *rspMsg = NULL; + void *rspMsg = NULL; int32_t rspLen = 0; - mndValidateStbInfo(pMnode, (SSTableMetaVersion *)kv->value, kv->valueLen/sizeof(SSTableMetaVersion), &rspMsg, &rspLen); + mndValidateStbInfo(pMnode, kv->value, kv->valueLen / sizeof(SSTableMetaVersion), &rspMsg, &rspLen); if (rspMsg && rspLen > 0) { SKv kv = {.key = HEARTBEAT_KEY_STBINFO, .valueLen = rspLen, .value = rspMsg}; taosArrayPush(hbRsp.info, &kv); @@ -392,7 +396,7 @@ static int32_t mndProcessHeartBeatReq(SMnodeMsg *pReq) { hbRsp.status = TSDB_CODE_MND_APP_ERROR; break; } - + pIter = taosHashIterate(pHbReq->info, pIter); } @@ -407,15 +411,14 @@ static int32_t mndProcessHeartBeatReq(SMnodeMsg *pReq) { } taosArrayDestroyEx(pArray, tFreeClientHbReq); - int32_t tlen = tSerializeSClientHbBatchRsp(NULL, &batchRsp); - void* buf = rpcMallocCont(tlen); - void* abuf = buf; - tSerializeSClientHbBatchRsp(&abuf, &batchRsp); - + int32_t tlen = tSerializeSClientHbBatchRsp(NULL, 0, &batchRsp); + void *buf = rpcMallocCont(tlen); + tSerializeSClientHbBatchRsp(buf, tlen, &batchRsp); + int32_t rspNum = (int32_t)taosArrayGetSize(batchRsp.rsps); for (int32_t i = 0; i < rspNum; ++i) { SClientHbRsp *rsp = taosArrayGet(batchRsp.rsps, i); - int32_t kvNum = (rsp->info) ? taosArrayGetSize(rsp->info): 0; + int32_t kvNum = (rsp->info) ? taosArrayGetSize(rsp->info) : 0; for (int32_t n = 0; n < kvNum; ++n) { SKv *kv = taosArrayGet(rsp->info, n); tfree(kv->value); diff --git a/source/dnode/mnode/impl/src/mndTrans.c b/source/dnode/mnode/impl/src/mndTrans.c index 6686c0887f..3230074add 100644 --- a/source/dnode/mnode/impl/src/mndTrans.c +++ b/source/dnode/mnode/impl/src/mndTrans.c @@ -542,12 +542,18 @@ static void mndTransSendRpcRsp(STrans *pTrans) { } if (sendRsp && pTrans->rpcHandle != NULL) { + void *rpcCont = rpcMallocCont(pTrans->rpcRspLen); + if (rpcCont != NULL) { + memcpy(rpcCont, pTrans->rpcRsp, pTrans->rpcRspLen); + } + free(pTrans->rpcRsp); + mDebug("trans:%d, send rsp, code:0x%x stage:%d app:%p", pTrans->id, pTrans->code & 0xFFFF, pTrans->stage, pTrans->rpcAHandle); SRpcMsg rspMsg = {.handle = pTrans->rpcHandle, .code = pTrans->code, .ahandle = pTrans->rpcAHandle, - .pCont = pTrans->rpcRsp, + .pCont = rpcCont, .contLen = pTrans->rpcRspLen}; rpcSendResponse(&rspMsg); pTrans->rpcHandle = NULL; diff --git a/source/dnode/mnode/impl/test/db/db.cpp b/source/dnode/mnode/impl/test/db/db.cpp index 4c09caed9c..a3efad8aa2 100644 --- a/source/dnode/mnode/impl/test/db/db.cpp +++ b/source/dnode/mnode/impl/test/db/db.cpp @@ -127,18 +127,20 @@ TEST_F(MndTestDb, 02_Create_Alter_Drop_Db) { CheckBinary("master", 9); { - int32_t contLen = sizeof(SAlterDbReq); + SAlterDbReq alterdbReq = {0}; + strcpy(alterdbReq.db, "1.d1"); + alterdbReq.totalBlocks = 12; + alterdbReq.daysToKeep0 = 300; + alterdbReq.daysToKeep1 = 400; + alterdbReq.daysToKeep2 = 500; + alterdbReq.fsyncPeriod = 4000; + alterdbReq.walLevel = 2; + alterdbReq.quorum = 2; + alterdbReq.cacheLastRow = 1; - SAlterDbReq* pReq = (SAlterDbReq*)rpcMallocCont(contLen); - strcpy(pReq->db, "1.d1"); - pReq->totalBlocks = htonl(12); - pReq->daysToKeep0 = htonl(300); - pReq->daysToKeep1 = htonl(400); - pReq->daysToKeep2 = htonl(500); - pReq->fsyncPeriod = htonl(4000); - pReq->walLevel = 2; - pReq->quorum = 2; - pReq->cacheLastRow = 1; + int32_t contLen = tSerializeSAlterDbReq(NULL, 0, &alterdbReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSAlterDbReq(pReq, contLen, &alterdbReq); SRpcMsg* pRsp = test.SendReq(TDMT_MND_ALTER_DB, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -196,18 +198,20 @@ TEST_F(MndTestDb, 02_Create_Alter_Drop_Db) { CheckInt8(0); // update { - int32_t contLen = sizeof(SDropDbReq); + SDropDbReq dropdbReq = {0}; + strcpy(dropdbReq.db, "1.d1"); - SDropDbReq* pReq = (SDropDbReq*)rpcMallocCont(contLen); - strcpy(pReq->db, "1.d1"); + int32_t contLen = tSerializeSDropDbReq(NULL, 0, &dropdbReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSDropDbReq(pReq, contLen, &dropdbReq); SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_DB, pReq, contLen); ASSERT_NE(pRsp, nullptr); ASSERT_EQ(pRsp->code, 0); - SDropDbRsp* pDrop = (SDropDbRsp*)pRsp->pCont; - pDrop->uid = htobe64(pDrop->uid); - EXPECT_STREQ(pDrop->db, "1.d1"); + SDropDbRsp dropdbRsp = {0}; + tDeserializeSDropDbRsp(pRsp->pCont, pRsp->contLen, &dropdbRsp); + EXPECT_STREQ(dropdbRsp.db, "1.d1"); } test.SendShowMetaReq(TSDB_MGMT_TABLE_DB, ""); @@ -260,73 +264,74 @@ TEST_F(MndTestDb, 03_Create_Use_Restart_Use_Db) { uint64_t d2_uid = 0; { - int32_t contLen = sizeof(SUseDbReq); + SUseDbReq usedbReq = {0}; + strcpy(usedbReq.db, "1.d2"); + usedbReq.vgVersion = -1; - SUseDbReq* pReq = (SUseDbReq*)rpcMallocCont(contLen); - strcpy(pReq->db, "1.d2"); - pReq->vgVersion = htonl(-1); + int32_t contLen = tSerializeSUseDbReq(NULL, 0, &usedbReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSUseDbReq(pReq, contLen, &usedbReq); SRpcMsg* pMsg = test.SendReq(TDMT_MND_USE_DB, pReq, contLen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); - SUseDbRsp* pRsp = (SUseDbRsp*)pMsg->pCont; - EXPECT_STREQ(pRsp->db, "1.d2"); - pRsp->uid = htobe64(pRsp->uid); - d2_uid = pRsp->uid; - pRsp->vgVersion = htonl(pRsp->vgVersion); - pRsp->vgNum = htonl(pRsp->vgNum); - pRsp->hashMethod = pRsp->hashMethod; - EXPECT_EQ(pRsp->vgVersion, 1); - EXPECT_EQ(pRsp->vgNum, 2); - EXPECT_EQ(pRsp->hashMethod, 1); + SUseDbRsp usedbRsp = {0}; + tDeserializeSUseDbRsp(pMsg->pCont, pMsg->contLen, &usedbRsp); + EXPECT_STREQ(usedbRsp.db, "1.d2"); + EXPECT_EQ(usedbRsp.vgVersion, 1); + EXPECT_EQ(usedbRsp.vgNum, 2); + EXPECT_EQ(usedbRsp.hashMethod, 1); + d2_uid = usedbRsp.uid; { - SVgroupInfo* pInfo = &pRsp->vgroupInfo[0]; - pInfo->vgId = htonl(pInfo->vgId); - pInfo->hashBegin = htonl(pInfo->hashBegin); - pInfo->hashEnd = htonl(pInfo->hashEnd); + SVgroupInfo* pInfo = (SVgroupInfo*)taosArrayGet(usedbRsp.pVgroupInfos, 0); + pInfo->vgId = pInfo->vgId; + pInfo->hashBegin = pInfo->hashBegin; + pInfo->hashEnd = pInfo->hashEnd; EXPECT_GT(pInfo->vgId, 0); EXPECT_EQ(pInfo->hashBegin, 0); EXPECT_EQ(pInfo->hashEnd, UINT32_MAX / 2 - 1); EXPECT_EQ(pInfo->epset.inUse, 0); EXPECT_EQ(pInfo->epset.numOfEps, 1); SEp* pAddr = &pInfo->epset.eps[0]; - pAddr->port = htons(pAddr->port); EXPECT_EQ(pAddr->port, 9030); EXPECT_STREQ(pAddr->fqdn, "localhost"); } { - SVgroupInfo* pInfo = &pRsp->vgroupInfo[1]; - pInfo->vgId = htonl(pInfo->vgId); - pInfo->hashBegin = htonl(pInfo->hashBegin); - pInfo->hashEnd = htonl(pInfo->hashEnd); + SVgroupInfo* pInfo = (SVgroupInfo*)taosArrayGet(usedbRsp.pVgroupInfos, 1); + pInfo->vgId = pInfo->vgId; + pInfo->hashBegin = pInfo->hashBegin; + pInfo->hashEnd = pInfo->hashEnd; EXPECT_GT(pInfo->vgId, 0); EXPECT_EQ(pInfo->hashBegin, UINT32_MAX / 2); EXPECT_EQ(pInfo->hashEnd, UINT32_MAX); EXPECT_EQ(pInfo->epset.inUse, 0); EXPECT_EQ(pInfo->epset.numOfEps, 1); SEp* pAddr = &pInfo->epset.eps[0]; - pAddr->port = htons(pAddr->port); EXPECT_EQ(pAddr->port, 9030); EXPECT_STREQ(pAddr->fqdn, "localhost"); } + + tFreeSUsedbRsp(&usedbRsp); } { - int32_t contLen = sizeof(SDropDbReq); + SDropDbReq dropdbReq = {0}; + strcpy(dropdbReq.db, "1.d2"); - SDropDbReq* pReq = (SDropDbReq*)rpcMallocCont(contLen); - strcpy(pReq->db, "1.d2"); + int32_t contLen = tSerializeSDropDbReq(NULL, 0, &dropdbReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSDropDbReq(pReq, contLen, &dropdbReq); SRpcMsg* pRsp = test.SendReq(TDMT_MND_DROP_DB, pReq, contLen); ASSERT_NE(pRsp, nullptr); ASSERT_EQ(pRsp->code, 0); - SDropDbRsp* pDrop = (SDropDbRsp*)pRsp->pCont; - pDrop->uid = htobe64(pDrop->uid); - EXPECT_STREQ(pDrop->db, "1.d2"); - EXPECT_EQ(pDrop->uid, d2_uid); + SDropDbRsp dropdbRsp = {0}; + tDeserializeSDropDbRsp(pRsp->pCont, pRsp->contLen, &dropdbRsp); + EXPECT_STREQ(dropdbRsp.db, "1.d2"); + EXPECT_EQ(dropdbRsp.uid, d2_uid); } } diff --git a/source/dnode/mnode/impl/test/profile/profile.cpp b/source/dnode/mnode/impl/test/profile/profile.cpp index ccf13d5d66..5b7bcd47b8 100644 --- a/source/dnode/mnode/impl/test/profile/profile.cpp +++ b/source/dnode/mnode/impl/test/profile/profile.cpp @@ -96,35 +96,35 @@ TEST_F(MndTestProfile, 03_ConnectMsg_Show) { } TEST_F(MndTestProfile, 04_HeartBeatMsg) { - SClientHbBatchReq batchReq; + SClientHbBatchReq batchReq = {0}; batchReq.reqs = taosArrayInit(0, sizeof(SClientHbReq)); SClientHbReq req = {0}; req.connKey = {.connId = 123, .hbType = HEARTBEAT_TYPE_MQ}; req.info = taosHashInit(64, hbKeyHashFunc, 1, HASH_ENTRY_LOCK); - SKv kv; + SKv kv = {0}; kv.key = 123; kv.value = (void*)"bcd"; kv.valueLen = 4; taosHashPut(req.info, &kv.key, sizeof(kv.key), &kv, sizeof(kv)); taosArrayPush(batchReq.reqs, &req); - int32_t tlen = tSerializeSClientHbBatchReq(NULL, &batchReq); - - void* buf = (SClientHbBatchReq*)rpcMallocCont(tlen); - void* bufCopy = buf; - tSerializeSClientHbBatchReq(&bufCopy, &batchReq); + int32_t tlen = tSerializeSClientHbBatchReq(NULL, 0, &batchReq); + void* buf = (SClientHbBatchReq*)rpcMallocCont(tlen); + tSerializeSClientHbBatchReq(buf, tlen, &batchReq); + SRpcMsg* pMsg = test.SendReq(TDMT_MND_HEARTBEAT, buf, tlen); ASSERT_NE(pMsg, nullptr); ASSERT_EQ(pMsg->code, 0); - char* pRspChar = (char*)pMsg->pCont; + SClientHbBatchRsp rsp = {0}; - tDeserializeSClientHbBatchRsp(pRspChar, &rsp); + tDeserializeSClientHbBatchRsp(pMsg->pCont, pMsg->contLen, &rsp); int sz = taosArrayGetSize(rsp.rsps); ASSERT_EQ(sz, 0); - //SClientHbRsp* pRsp = (SClientHbRsp*) taosArrayGet(rsp.rsps, 0); - //EXPECT_EQ(pRsp->connKey.connId, 123); - //EXPECT_EQ(pRsp->connKey.hbType, HEARTBEAT_TYPE_MQ); - //EXPECT_EQ(pRsp->status, 0); + + // SClientHbRsp* pRsp = (SClientHbRsp*) taosArrayGet(rsp.rsps, 0); + // EXPECT_EQ(pRsp->connKey.connId, 123); + // EXPECT_EQ(pRsp->connKey.hbType, HEARTBEAT_TYPE_MQ); + // EXPECT_EQ(pRsp->status, 0); #if 0 int32_t contLen = sizeof(SHeartBeatReq); diff --git a/source/libs/catalog/test/catalogTests.cpp b/source/libs/catalog/test/catalogTests.cpp index 4eeae0a244..7742d8ee22 100644 --- a/source/libs/catalog/test/catalogTests.cpp +++ b/source/libs/catalog/test/catalogTests.cpp @@ -101,7 +101,6 @@ void sendCreateDbMsg(void *shandle, SEpSet *pEpSet) { rpcMsg.msgType = TDMT_MND_CREATE_DB; SRpcMsg rpcRsp = {0}; - rpcSendRecv(shandle, pEpSet, &rpcMsg, &rpcRsp); ASSERT_EQ(rpcRsp.code, 0); @@ -252,39 +251,43 @@ void ctgTestBuildSTableMetaRsp(STableMetaRsp *rspMsg) { } void ctgTestPrepareDbVgroups(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) { - SUseDbRsp *rspMsg = NULL; // todo - - pRsp->code = 0; - pRsp->contLen = sizeof(SUseDbRsp) + ctgTestVgNum * sizeof(SVgroupInfo); - pRsp->pCont = calloc(1, pRsp->contLen); - rspMsg = (SUseDbRsp *)pRsp->pCont; - strcpy(rspMsg->db, ctgTestDbname); - rspMsg->vgVersion = htonl(ctgTestVgVersion); + SUseDbRsp usedbRsp = {0}; + strcpy(usedbRsp.db, ctgTestDbname); + usedbRsp.vgVersion = ctgTestVgVersion; ctgTestCurrentVgVersion = ctgTestVgVersion; - rspMsg->vgNum = htonl(ctgTestVgNum); - rspMsg->hashMethod = 0; - rspMsg->uid = htobe64(ctgTestDbId); + usedbRsp.vgNum = ctgTestVgNum; + usedbRsp.hashMethod = 0; + usedbRsp.uid = ctgTestDbId; + usedbRsp.pVgroupInfos = taosArrayInit(usedbRsp.vgNum, sizeof(SVgroupInfo)); - SVgroupInfo *vg = NULL; - uint32_t hashUnit = UINT32_MAX / ctgTestVgNum; + uint32_t hashUnit = UINT32_MAX / ctgTestVgNum; for (int32_t i = 0; i < ctgTestVgNum; ++i) { - vg = &rspMsg->vgroupInfo[i]; - - vg->vgId = htonl(i + 1); - vg->hashBegin = htonl(i * hashUnit); - vg->hashEnd = htonl(hashUnit * (i + 1) - 1); - vg->epset.numOfEps = i % TSDB_MAX_REPLICA + 1; - vg->epset.inUse = i % vg->epset.numOfEps; - for (int32_t n = 0; n < vg->epset.numOfEps; ++n) { - SEp *addr = &vg->epset.eps[n]; - strcpy(addr->fqdn, "a0"); - addr->port = htons(n + 22); + SVgroupInfo vg = {0}; + vg.vgId = i + 1; + vg.hashBegin = i * hashUnit; + vg.hashEnd = hashUnit * (i + 1) - 1; + if (i == ctgTestVgNum - 1) { + vg.hashEnd = htonl(UINT32_MAX); } + + vg.epset.numOfEps = i % TSDB_MAX_REPLICA + 1; + vg.epset.inUse = i % vg.epset.numOfEps; + for (int32_t n = 0; n < vg.epset.numOfEps; ++n) { + SEp *addr = &vg.epset.eps[n]; + strcpy(addr->fqdn, "a0"); + addr->port = n + 22; + } + + taosArrayPush(usedbRsp.pVgroupInfos, &vg); } - vg->hashEnd = htonl(UINT32_MAX); + int32_t contLen = tSerializeSUseDbRsp(NULL, 0, &usedbRsp); + void *pReq = rpcMallocCont(contLen); + tSerializeSUseDbRsp(pReq, contLen, &usedbRsp); - return; + pRsp->code = 0; + pRsp->contLen = contLen; + pRsp->pCont = pReq; } void ctgTestPrepareTableMeta(void *shandle, SEpSet *pEpSet, SRpcMsg *pMsg, SRpcMsg *pRsp) { diff --git a/source/libs/qcom/src/querymsg.c b/source/libs/qcom/src/querymsg.c index 3e14bfca09..4acfb6239b 100644 --- a/source/libs/qcom/src/querymsg.c +++ b/source/libs/qcom/src/querymsg.c @@ -83,78 +83,63 @@ int32_t queryBuildUseDbMsg(void* input, char **msg, int32_t msgSize, int32_t *ms return TSDB_CODE_SUCCESS; } - -int32_t queryProcessUseDBRsp(void* output, char *msg, int32_t msgSize) { +int32_t queryProcessUseDBRsp(void *output, char *msg, int32_t msgSize) { if (NULL == output || NULL == msg || msgSize <= 0) { return TSDB_CODE_TSC_INVALID_INPUT; } - SUseDbRsp *pRsp = (SUseDbRsp *)msg; SUseDbOutput *pOut = (SUseDbOutput *)output; - int32_t code = 0; + int32_t code = 0; - if (msgSize <= sizeof(*pRsp)) { - qError("invalid use db rsp msg size, msgSize:%d", msgSize); - return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE; + SUseDbRsp usedbRsp = {0}; + if (tDeserializeSUseDbRsp(msg, msgSize, &usedbRsp) != 0) { + qError("invalid use db rsp msg, msgSize:%d", msgSize); + return TSDB_CODE_INVALID_MSG; } - - pRsp->vgVersion = ntohl(pRsp->vgVersion); - pRsp->vgNum = ntohl(pRsp->vgNum); - pRsp->uid = be64toh(pRsp->uid); - if (pRsp->vgNum < 0) { - qError("invalid db[%s] vgroup number[%d]", pRsp->db, pRsp->vgNum); + if (usedbRsp.vgNum < 0) { + qError("invalid db[%s] vgroup number[%d]", usedbRsp.db, usedbRsp.vgNum); return TSDB_CODE_TSC_INVALID_VALUE; } - int32_t expectSize = pRsp->vgNum * sizeof(pRsp->vgroupInfo[0]) + sizeof(*pRsp); - if (msgSize != expectSize) { - qError("use db rsp size mis-match, msgSize:%d, expected:%d, vgnumber:%d", msgSize, expectSize, pRsp->vgNum); - return TSDB_CODE_TSC_VALUE_OUT_OF_RANGE; - } - pOut->dbVgroup = calloc(1, sizeof(SDBVgroupInfo)); if (NULL == pOut->dbVgroup) { qError("calloc %d failed", (int32_t)sizeof(SDBVgroupInfo)); return TSDB_CODE_TSC_OUT_OF_MEMORY; } - pOut->dbId = pRsp->uid; - pOut->dbVgroup->vgVersion = pRsp->vgVersion; - pOut->dbVgroup->hashMethod = pRsp->hashMethod; - pOut->dbVgroup->vgHash = taosHashInit(pRsp->vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); + pOut->dbId = usedbRsp.uid; + pOut->dbVgroup->vgVersion = usedbRsp.vgVersion; + pOut->dbVgroup->hashMethod = usedbRsp.hashMethod; + pOut->dbVgroup->vgHash = + taosHashInit(usedbRsp.vgNum, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), true, HASH_ENTRY_LOCK); if (NULL == pOut->dbVgroup->vgHash) { - qError("taosHashInit %d failed", pRsp->vgNum); + qError("taosHashInit %d failed", usedbRsp.vgNum); tfree(pOut->dbVgroup); return TSDB_CODE_TSC_OUT_OF_MEMORY; } - for (int32_t i = 0; i < pRsp->vgNum; ++i) { - pRsp->vgroupInfo[i].vgId = ntohl(pRsp->vgroupInfo[i].vgId); - pRsp->vgroupInfo[i].hashBegin = ntohl(pRsp->vgroupInfo[i].hashBegin); - pRsp->vgroupInfo[i].hashEnd = ntohl(pRsp->vgroupInfo[i].hashEnd); + for (int32_t i = 0; i < usedbRsp.vgNum; ++i) { + SVgroupInfo *pVgInfo = taosArrayGet(usedbRsp.pVgroupInfos, i); - for (int32_t n = 0; n < pRsp->vgroupInfo[i].epset.numOfEps; ++n) { - pRsp->vgroupInfo[i].epset.eps[n].port = ntohs(pRsp->vgroupInfo[i].epset.eps[n].port); - } - - if (0 != taosHashPut(pOut->dbVgroup->vgHash, &pRsp->vgroupInfo[i].vgId, sizeof(pRsp->vgroupInfo[i].vgId), &pRsp->vgroupInfo[i], sizeof(pRsp->vgroupInfo[i]))) { + if (0 != taosHashPut(pOut->dbVgroup->vgHash, &pVgInfo->vgId, sizeof(int32_t), pVgInfo, sizeof(SVgroupInfo))) { qError("taosHashPut failed"); goto _return; } } - memcpy(pOut->db, pRsp->db, sizeof(pOut->db)); + memcpy(pOut->db, usedbRsp.db, TSDB_DB_FNAME_LEN); return code; _return: + taosArrayDestroy(usedbRsp.pVgroupInfos); if (pOut) { taosHashCleanup(pOut->dbVgroup->vgHash); tfree(pOut->dbVgroup); } - + return code; } From 54584fd1e1e99e2fa9792b5a100a0fa0ca5612f7 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 14 Feb 2022 17:39:08 +0800 Subject: [PATCH 3/7] seralize db msg --- source/client/src/clientHb.c | 3 - source/client/src/clientMsgHandler.c | 18 +-- source/dnode/mnode/impl/src/mndDb.c | 47 ++++--- source/dnode/mnode/impl/test/stb/stb.cpp | 8 +- source/libs/parser/inc/astToMsg.h | 2 +- source/libs/parser/src/astToMsg.c | 156 +++++++++++++++++++---- source/libs/parser/src/dCDAstProcess.c | 131 +++---------------- source/libs/qcom/src/querymsg.c | 32 ++--- 8 files changed, 207 insertions(+), 190 deletions(-) diff --git a/source/client/src/clientHb.c b/source/client/src/clientHb.c index 7920ae0658..9e5cdee516 100644 --- a/source/client/src/clientHb.c +++ b/source/client/src/clientHb.c @@ -28,7 +28,6 @@ static int32_t hbMqHbRspHandle(struct SAppHbMgr *pAppHbMgr, SClientHbRsp* pRsp) } static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog *pCatalog) { - int32_t msgLen = 0; int32_t code = 0; SUseDbBatchRsp batchUseRsp = {0}; @@ -72,8 +71,6 @@ static int32_t hbProcessDBInfoRsp(void *value, int32_t valueLen, struct SCatalog if (code) { return code; } - - msgLen += sizeof(SUseDbRsp) + rsp->vgNum * sizeof(SVgroupInfo); } return TSDB_CODE_SUCCESS; diff --git a/source/client/src/clientMsgHandler.c b/source/client/src/clientMsgHandler.c index 8ab1880069..0f3e4af575 100644 --- a/source/client/src/clientMsgHandler.c +++ b/source/client/src/clientMsgHandler.c @@ -264,9 +264,13 @@ int32_t processUseDbRsp(void* param, const SDataBuf* pMsg, int32_t code) { return code; } - SUseDbRsp* pUseDbRsp = (SUseDbRsp*) pMsg->pData; + SUseDbRsp usedbRsp = {0}; + tDeserializeSUseDbRsp(pMsg->pData, pMsg->len, &usedbRsp); + SName name = {0}; - tNameFromString(&name, pUseDbRsp->db, T_NAME_ACCT|T_NAME_DB); + tNameFromString(&name, usedbRsp.db, T_NAME_ACCT|T_NAME_DB); + + tFreeSUsedbRsp(&usedbRsp); char db[TSDB_DB_NAME_LEN] = {0}; tNameGetDbName(&name, db); @@ -300,14 +304,12 @@ int32_t processDropDbRsp(void* param, const SDataBuf* pMsg, int32_t code) { return code; } - SDropDbRsp *rsp = (SDropDbRsp *)pMsg->pData; - - struct SCatalog *pCatalog = NULL; - rsp->uid = be64toh(rsp->uid); + SDropDbRsp dropdbRsp = {0}; + tDeserializeSDropDbRsp(pMsg->pData, pMsg->len, &dropdbRsp); + struct SCatalog* pCatalog = NULL; catalogGetHandle(pRequest->pTscObj->pAppInfo->clusterId, &pCatalog); - - catalogRemoveDB(pCatalog, rsp->db, rsp->uid); + catalogRemoveDB(pCatalog, dropdbRsp.db, dropdbRsp.uid); tsem_post(&pRequest->body.rspSem); return code; diff --git a/source/dnode/mnode/impl/src/mndDb.c b/source/dnode/mnode/impl/src/mndDb.c index 56d743cd5a..7c42ed5df6 100644 --- a/source/dnode/mnode/impl/src/mndDb.c +++ b/source/dnode/mnode/impl/src/mndDb.c @@ -975,30 +975,37 @@ int32_t mndValidateDbInfo(SMnode *pMnode, SDbVgVersion *pDbs, int32_t numOfDbs, pDbVgVersion->dbId = htobe64(pDbVgVersion->dbId); pDbVgVersion->vgVersion = htonl(pDbVgVersion->vgVersion); - SDbObj *pDb = mndAcquireDb(pMnode, pDbVgVersion->dbFName); - if (pDb == NULL || pDbVgVersion->vgVersion >= pDb->vgVersion) { - mndReleaseDb(pMnode, pDb); - mDebug("db:%s, no need to use db", pDbVgVersion->dbFName); - continue; - } - SUseDbRsp usedbRsp = {0}; - usedbRsp.pVgroupInfos = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SVgroupInfo)); - if (usedbRsp.pVgroupInfos == NULL) { + + SDbObj *pDb = mndAcquireDb(pMnode, pDbVgVersion->dbFName); + if (pDb == NULL) { + mDebug("db:%s, no exist", pDbVgVersion->dbFName); + memcpy(usedbRsp.db, pDbVgVersion->dbFName, TSDB_DB_FNAME_LEN); + usedbRsp.uid = pDbVgVersion->dbId; + usedbRsp.vgVersion = -1; + taosArrayPush(batchUseRsp.pArray, &usedbRsp); + } else if (pDbVgVersion->vgVersion >= pDb->vgVersion) { + mDebug("db:%s, version not changed", pDbVgVersion->dbFName); mndReleaseDb(pMnode, pDb); - mError("db:%s, failed to malloc usedb response", pDb->name); continue; + } else { + usedbRsp.pVgroupInfos = taosArrayInit(pDb->cfg.numOfVgroups, sizeof(SVgroupInfo)); + if (usedbRsp.pVgroupInfos == NULL) { + mndReleaseDb(pMnode, pDb); + mError("db:%s, failed to malloc usedb response", pDb->name); + continue; + } + + mndBuildDBVgroupInfo(pDb, pMnode, usedbRsp.pVgroupInfos); + memcpy(usedbRsp.db, pDb->name, TSDB_DB_FNAME_LEN); + usedbRsp.uid = pDb->uid; + usedbRsp.vgVersion = pDb->vgVersion; + usedbRsp.vgNum = (int32_t)taosArrayGetSize(usedbRsp.pVgroupInfos); + usedbRsp.hashMethod = pDb->hashMethod; + + taosArrayPush(batchUseRsp.pArray, &usedbRsp); + mndReleaseDb(pMnode, pDb); } - - mndBuildDBVgroupInfo(pDb, pMnode, usedbRsp.pVgroupInfos); - memcpy(usedbRsp.db, pDb->name, TSDB_DB_FNAME_LEN); - usedbRsp.uid = pDb->uid; - usedbRsp.vgVersion = pDb->vgVersion; - usedbRsp.vgNum = (int32_t)taosArrayGetSize(usedbRsp.pVgroupInfos); - usedbRsp.hashMethod = pDb->hashMethod; - - taosArrayPush(batchUseRsp.pArray, &usedbRsp); - mndReleaseDb(pMnode, pDb); } int32_t rspLen = tSerializeSUseDbBatchRsp(NULL, 0, &batchUseRsp); diff --git a/source/dnode/mnode/impl/test/stb/stb.cpp b/source/dnode/mnode/impl/test/stb/stb.cpp index 6d0005fd15..87df50ef0e 100644 --- a/source/dnode/mnode/impl/test/stb/stb.cpp +++ b/source/dnode/mnode/impl/test/stb/stb.cpp @@ -69,10 +69,12 @@ void* MndTestStb::BuildCreateDbReq(const char* dbname, int32_t* pContLen) { } void* MndTestStb::BuildDropDbReq(const char* dbname, int32_t* pContLen) { - int32_t contLen = sizeof(SDropDbReq); + SDropDbReq dropdbReq = {0}; + strcpy(dropdbReq.db, dbname); - SDropDbReq* pReq = (SDropDbReq*)rpcMallocCont(contLen); - strcpy(pReq->db, dbname); + int32_t contLen = tSerializeSDropDbReq(NULL, 0, &dropdbReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSDropDbReq(pReq, contLen, &dropdbReq); *pContLen = contLen; return pReq; diff --git a/source/libs/parser/inc/astToMsg.h b/source/libs/parser/inc/astToMsg.h index d7b6469abb..a1e4225410 100644 --- a/source/libs/parser/inc/astToMsg.h +++ b/source/libs/parser/inc/astToMsg.h @@ -9,7 +9,7 @@ char* buildUserManipulationMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* buildAcctManipulationMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgLen); char* buildDropUserMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgLen); SShowReq* buildShowMsg(SShowInfo* pShowInfo, SParseContext* pParseCtx, SMsgBuf* pMsgBuf); -SCreateDbReq* buildCreateDbMsg(SCreateDbInfo* pCreateDbInfo, SParseContext *pCtx, SMsgBuf* pMsgBuf); +char* buildCreateDbMsg(SCreateDbInfo* pCreateDbInfo, int32_t *len, SParseContext *pCtx, SMsgBuf* pMsgBuf); char* buildCreateStbReq(SCreateTableSql* pCreateTableSql, int32_t* len, SParseContext* pParseCtx, SMsgBuf* pMsgBuf); char* buildDropStableReq(SSqlInfo* pInfo, int32_t* len, SParseContext* pParseCtx, SMsgBuf* pMsgBuf); char* buildCreateDnodeMsg(SSqlInfo* pInfo, int32_t* len, SMsgBuf* pMsgBuf); diff --git a/source/libs/parser/src/astToMsg.c b/source/libs/parser/src/astToMsg.c index 1fae979ee9..adcd740ef6 100644 --- a/source/libs/parser/src/astToMsg.c +++ b/source/libs/parser/src/astToMsg.c @@ -39,14 +39,11 @@ char* buildAcctManipulationMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, SCreateAcctInfo* pAcctOpt = &pInfo->pMiscInfo->acctOpt; - createReq.maxUsers = htonl(pAcctOpt->maxUsers); - createReq.maxDbs = htonl(pAcctOpt->maxDbs); - createReq.maxTimeSeries = htonl(pAcctOpt->maxTimeSeries); - createReq.maxStreams = htonl(pAcctOpt->maxStreams); - // createReq.maxPointsPerSecond = htonl(pAcctOpt->maxPointsPerSecond); - createReq.maxStorage = htobe64(pAcctOpt->maxStorage); - // createReq.maxQueryTime = htobe64(pAcctOpt->maxQueryTime); - // createReq.maxConnections = htonl(pAcctOpt->maxConnections); + createReq.maxUsers = pAcctOpt->maxUsers; + createReq.maxDbs = pAcctOpt->maxDbs; + createReq.maxTimeSeries = pAcctOpt->maxTimeSeries; + createReq.maxStreams = pAcctOpt->maxStreams; + createReq.maxStorage = pAcctOpt->maxStorage; if (pAcctOpt->stat.n == 0) { createReq.accessState = -1; @@ -147,9 +144,9 @@ static int32_t setKeepOption(SCreateDbReq* pMsg, const SCreateDbInfo* pCreateDb, const char* msg2 = "invalid keep value"; const char* msg3 = "invalid keep value, should be keep0 <= keep1 <= keep2"; - pMsg->daysToKeep0 = htonl(-1); - pMsg->daysToKeep1 = htonl(-1); - pMsg->daysToKeep2 = htonl(-1); + pMsg->daysToKeep0 = -1; + pMsg->daysToKeep1 = -1; + pMsg->daysToKeep2 = -1; SArray* pKeep = pCreateDb->keep; if (pKeep != NULL) { @@ -209,13 +206,13 @@ static int32_t setTimePrecision(SCreateDbReq* pMsg, const SCreateDbInfo* pCreate } static void doSetDbOptions(SCreateDbReq* pMsg, const SCreateDbInfo* pCreateDb) { - pMsg->cacheBlockSize = htonl(pCreateDb->cacheBlockSize); - pMsg->totalBlocks = htonl(pCreateDb->numOfBlocks); - pMsg->daysPerFile = htonl(pCreateDb->daysPerFile); - pMsg->commitTime = htonl((int32_t)pCreateDb->commitTime); - pMsg->minRows = htonl(pCreateDb->minRowsPerBlock); - pMsg->maxRows = htonl(pCreateDb->maxRowsPerBlock); - pMsg->fsyncPeriod = htonl(pCreateDb->fsyncPeriod); + pMsg->cacheBlockSize = pCreateDb->cacheBlockSize; + pMsg->totalBlocks = pCreateDb->numOfBlocks; + pMsg->daysPerFile = pCreateDb->daysPerFile; + pMsg->commitTime = (int32_t)pCreateDb->commitTime; + pMsg->minRows = pCreateDb->minRowsPerBlock; + pMsg->maxRows = pCreateDb->maxRowsPerBlock; + pMsg->fsyncPeriod = pCreateDb->fsyncPeriod; pMsg->compression = (int8_t)pCreateDb->compressionLevel; pMsg->walLevel = (char)pCreateDb->walLevel; pMsg->replications = pCreateDb->replica; @@ -223,7 +220,7 @@ static void doSetDbOptions(SCreateDbReq* pMsg, const SCreateDbInfo* pCreateDb) { pMsg->ignoreExist = pCreateDb->ignoreExists; pMsg->update = pCreateDb->update; pMsg->cacheLastRow = pCreateDb->cachelast; - pMsg->numOfVgroups = htonl(pCreateDb->numOfVgroups); + pMsg->numOfVgroups = pCreateDb->numOfVgroups; } int32_t setDbOptions(SCreateDbReq* pCreateDbMsg, const SCreateDbInfo* pCreateDbSql, SMsgBuf* pMsgBuf) { @@ -240,12 +237,104 @@ int32_t setDbOptions(SCreateDbReq* pCreateDbMsg, const SCreateDbInfo* pCreateDbS return TSDB_CODE_SUCCESS; } -SCreateDbReq* buildCreateDbMsg(SCreateDbInfo* pCreateDbInfo, SParseContext* pCtx, SMsgBuf* pMsgBuf) { - SCreateDbReq* pCreateMsg = calloc(1, sizeof(SCreateDbReq)); - if (setDbOptions(pCreateMsg, pCreateDbInfo, pMsgBuf) != TSDB_CODE_SUCCESS) { - tfree(pCreateMsg); - terrno = TSDB_CODE_TSC_INVALID_OPERATION; +// can only perform the parameters based on the macro definitation +static int32_t doCheckDbOptions(SCreateDbReq* pCreate, SMsgBuf* pMsgBuf) { + char msg[512] = {0}; + if (pCreate->walLevel != -1 && (pCreate->walLevel < TSDB_MIN_WAL_LEVEL || pCreate->walLevel > TSDB_MAX_WAL_LEVEL)) { + snprintf(msg, tListLen(msg), "invalid db option walLevel: %d, only 1-2 allowed", pCreate->walLevel); + return buildInvalidOperationMsg(pMsgBuf, msg); + } + + if (pCreate->replications != -1 && + (pCreate->replications < TSDB_MIN_DB_REPLICA_OPTION || pCreate->replications > TSDB_MAX_DB_REPLICA_OPTION)) { + snprintf(msg, tListLen(msg), "invalid db option replications: %d valid range: [%d, %d]", pCreate->replications, + TSDB_MIN_DB_REPLICA_OPTION, TSDB_MAX_DB_REPLICA_OPTION); + return buildInvalidOperationMsg(pMsgBuf, msg); + } + + int32_t blocks = pCreate->totalBlocks; + if (blocks != -1 && (blocks < TSDB_MIN_TOTAL_BLOCKS || blocks > TSDB_MAX_TOTAL_BLOCKS)) { + snprintf(msg, tListLen(msg), "invalid db option totalBlocks: %d valid range: [%d, %d]", blocks, + TSDB_MIN_TOTAL_BLOCKS, TSDB_MAX_TOTAL_BLOCKS); + return buildInvalidOperationMsg(pMsgBuf, msg); + } + + if (pCreate->quorum != -1 && + (pCreate->quorum < TSDB_MIN_DB_QUORUM_OPTION || pCreate->quorum > TSDB_MAX_DB_QUORUM_OPTION)) { + snprintf(msg, tListLen(msg), "invalid db option quorum: %d valid range: [%d, %d]", pCreate->quorum, + TSDB_MIN_DB_QUORUM_OPTION, TSDB_MAX_DB_QUORUM_OPTION); + return buildInvalidOperationMsg(pMsgBuf, msg); + } + + int32_t val = pCreate->daysPerFile; + if (val != -1 && (val < TSDB_MIN_DAYS_PER_FILE || val > TSDB_MAX_DAYS_PER_FILE)) { + snprintf(msg, tListLen(msg), "invalid db option daysPerFile: %d valid range: [%d, %d]", val, TSDB_MIN_DAYS_PER_FILE, + TSDB_MAX_DAYS_PER_FILE); + return buildInvalidOperationMsg(pMsgBuf, msg); + } + + val = pCreate->cacheBlockSize; + if (val != -1 && (val < TSDB_MIN_CACHE_BLOCK_SIZE || val > TSDB_MAX_CACHE_BLOCK_SIZE)) { + snprintf(msg, tListLen(msg), "invalid db option cacheBlockSize: %d valid range: [%d, %d]", val, + TSDB_MIN_CACHE_BLOCK_SIZE, TSDB_MAX_CACHE_BLOCK_SIZE); + return buildInvalidOperationMsg(pMsgBuf, msg); + } + + if (pCreate->precision != TSDB_TIME_PRECISION_MILLI && pCreate->precision != TSDB_TIME_PRECISION_MICRO && + pCreate->precision != TSDB_TIME_PRECISION_NANO) { + snprintf(msg, tListLen(msg), "invalid db option timePrecision: %d valid value: [%d, %d, %d]", pCreate->precision, + TSDB_TIME_PRECISION_MILLI, TSDB_TIME_PRECISION_MICRO, TSDB_TIME_PRECISION_NANO); + return buildInvalidOperationMsg(pMsgBuf, msg); + } + + val = pCreate->commitTime; + if (val != -1 && (val < TSDB_MIN_COMMIT_TIME || val > TSDB_MAX_COMMIT_TIME)) { + snprintf(msg, tListLen(msg), "invalid db option commitTime: %d valid range: [%d, %d]", val, TSDB_MIN_COMMIT_TIME, + TSDB_MAX_COMMIT_TIME); + return buildInvalidOperationMsg(pMsgBuf, msg); + } + + val = pCreate->fsyncPeriod; + if (val != -1 && (val < TSDB_MIN_FSYNC_PERIOD || val > TSDB_MAX_FSYNC_PERIOD)) { + snprintf(msg, tListLen(msg), "invalid db option fsyncPeriod: %d valid range: [%d, %d]", val, TSDB_MIN_FSYNC_PERIOD, + TSDB_MAX_FSYNC_PERIOD); + return buildInvalidOperationMsg(pMsgBuf, msg); + } + + if (pCreate->compression != -1 && + (pCreate->compression < TSDB_MIN_COMP_LEVEL || pCreate->compression > TSDB_MAX_COMP_LEVEL)) { + snprintf(msg, tListLen(msg), "invalid db option compression: %d valid range: [%d, %d]", pCreate->compression, + TSDB_MIN_COMP_LEVEL, TSDB_MAX_COMP_LEVEL); + return buildInvalidOperationMsg(pMsgBuf, msg); + } + + val = pCreate->numOfVgroups; + if (val < TSDB_MIN_VNODES_PER_DB || val > TSDB_MAX_VNODES_PER_DB) { + snprintf(msg, tListLen(msg), "invalid number of vgroups for DB:%d valid range: [%d, %d]", val, + TSDB_MIN_VNODES_PER_DB, TSDB_MAX_VNODES_PER_DB); + } + + val = pCreate->maxRows; + if (val < TSDB_MIN_MAX_ROW_FBLOCK || val > TSDB_MAX_MAX_ROW_FBLOCK) { + snprintf(msg, tListLen(msg), "invalid number of max rows in file block for DB:%d valid range: [%d, %d]", val, + TSDB_MIN_MAX_ROW_FBLOCK, TSDB_MAX_MAX_ROW_FBLOCK); + } + + val = pCreate->minRows; + if (val < TSDB_MIN_MIN_ROW_FBLOCK || val > TSDB_MAX_MIN_ROW_FBLOCK) { + snprintf(msg, tListLen(msg), "invalid number of min rows in file block for DB:%d valid range: [%d, %d]", val, + TSDB_MIN_MIN_ROW_FBLOCK, TSDB_MAX_MIN_ROW_FBLOCK); + } + + return TSDB_CODE_SUCCESS; +} + +char* buildCreateDbMsg(SCreateDbInfo* pCreateDbInfo, int32_t* len, SParseContext* pCtx, SMsgBuf* pMsgBuf) { + SCreateDbReq createReq = {0}; + + if (setDbOptions(&createReq, pCreateDbInfo, pMsgBuf) != TSDB_CODE_SUCCESS) { + terrno = TSDB_CODE_TSC_INVALID_OPERATION; return NULL; } @@ -256,8 +345,23 @@ SCreateDbReq* buildCreateDbMsg(SCreateDbInfo* pCreateDbInfo, SParseContext* pCtx return NULL; } - tNameGetFullDbName(&name, pCreateMsg->db); - return pCreateMsg; + tNameGetFullDbName(&name, createReq.db); + + if (doCheckDbOptions(&createReq, pMsgBuf) != TSDB_CODE_SUCCESS) { + terrno = TSDB_CODE_TSC_INVALID_OPERATION; + return NULL; + } + + int32_t tlen = tSerializeSCreateDbReq(NULL, 0, &createReq); + void* pReq = malloc(tlen); + if (pReq == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } + + tSerializeSCreateDbReq(pReq, tlen, &createReq); + *len = tlen; + return pReq; } char* buildCreateStbReq(SCreateTableSql* pCreateTableSql, int32_t* len, SParseContext* pParseCtx, SMsgBuf* pMsgBuf) { diff --git a/source/libs/parser/src/dCDAstProcess.c b/source/libs/parser/src/dCDAstProcess.c index 4b70d2c6c3..1a933f5d85 100644 --- a/source/libs/parser/src/dCDAstProcess.c +++ b/source/libs/parser/src/dCDAstProcess.c @@ -125,99 +125,6 @@ static int32_t setShowInfo(SShowInfo* pShowInfo, SParseContext* pCtx, void** out return TSDB_CODE_SUCCESS; } -// can only perform the parameters based on the macro definitation -static int32_t doCheckDbOptions(SCreateDbReq* pCreate, SMsgBuf* pMsgBuf) { - char msg[512] = {0}; - - if (pCreate->walLevel != -1 && (pCreate->walLevel < TSDB_MIN_WAL_LEVEL || pCreate->walLevel > TSDB_MAX_WAL_LEVEL)) { - snprintf(msg, tListLen(msg), "invalid db option walLevel: %d, only 1-2 allowed", pCreate->walLevel); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - if (pCreate->replications != -1 && - (pCreate->replications < TSDB_MIN_DB_REPLICA_OPTION || pCreate->replications > TSDB_MAX_DB_REPLICA_OPTION)) { - snprintf(msg, tListLen(msg), "invalid db option replications: %d valid range: [%d, %d]", pCreate->replications, - TSDB_MIN_DB_REPLICA_OPTION, TSDB_MAX_DB_REPLICA_OPTION); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - int32_t blocks = ntohl(pCreate->totalBlocks); - if (blocks != -1 && (blocks < TSDB_MIN_TOTAL_BLOCKS || blocks > TSDB_MAX_TOTAL_BLOCKS)) { - snprintf(msg, tListLen(msg), "invalid db option totalBlocks: %d valid range: [%d, %d]", blocks, - TSDB_MIN_TOTAL_BLOCKS, TSDB_MAX_TOTAL_BLOCKS); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - if (pCreate->quorum != -1 && - (pCreate->quorum < TSDB_MIN_DB_QUORUM_OPTION || pCreate->quorum > TSDB_MAX_DB_QUORUM_OPTION)) { - snprintf(msg, tListLen(msg), "invalid db option quorum: %d valid range: [%d, %d]", pCreate->quorum, - TSDB_MIN_DB_QUORUM_OPTION, TSDB_MAX_DB_QUORUM_OPTION); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - int32_t val = htonl(pCreate->daysPerFile); - if (val != -1 && (val < TSDB_MIN_DAYS_PER_FILE || val > TSDB_MAX_DAYS_PER_FILE)) { - snprintf(msg, tListLen(msg), "invalid db option daysPerFile: %d valid range: [%d, %d]", val, TSDB_MIN_DAYS_PER_FILE, - TSDB_MAX_DAYS_PER_FILE); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - val = htonl(pCreate->cacheBlockSize); - if (val != -1 && (val < TSDB_MIN_CACHE_BLOCK_SIZE || val > TSDB_MAX_CACHE_BLOCK_SIZE)) { - snprintf(msg, tListLen(msg), "invalid db option cacheBlockSize: %d valid range: [%d, %d]", val, - TSDB_MIN_CACHE_BLOCK_SIZE, TSDB_MAX_CACHE_BLOCK_SIZE); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - if (pCreate->precision != TSDB_TIME_PRECISION_MILLI && pCreate->precision != TSDB_TIME_PRECISION_MICRO && - pCreate->precision != TSDB_TIME_PRECISION_NANO) { - snprintf(msg, tListLen(msg), "invalid db option timePrecision: %d valid value: [%d, %d, %d]", pCreate->precision, - TSDB_TIME_PRECISION_MILLI, TSDB_TIME_PRECISION_MICRO, TSDB_TIME_PRECISION_NANO); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - val = htonl(pCreate->commitTime); - if (val != -1 && (val < TSDB_MIN_COMMIT_TIME || val > TSDB_MAX_COMMIT_TIME)) { - snprintf(msg, tListLen(msg), "invalid db option commitTime: %d valid range: [%d, %d]", val, TSDB_MIN_COMMIT_TIME, - TSDB_MAX_COMMIT_TIME); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - val = htonl(pCreate->fsyncPeriod); - if (val != -1 && (val < TSDB_MIN_FSYNC_PERIOD || val > TSDB_MAX_FSYNC_PERIOD)) { - snprintf(msg, tListLen(msg), "invalid db option fsyncPeriod: %d valid range: [%d, %d]", val, TSDB_MIN_FSYNC_PERIOD, - TSDB_MAX_FSYNC_PERIOD); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - if (pCreate->compression != -1 && - (pCreate->compression < TSDB_MIN_COMP_LEVEL || pCreate->compression > TSDB_MAX_COMP_LEVEL)) { - snprintf(msg, tListLen(msg), "invalid db option compression: %d valid range: [%d, %d]", pCreate->compression, - TSDB_MIN_COMP_LEVEL, TSDB_MAX_COMP_LEVEL); - return buildInvalidOperationMsg(pMsgBuf, msg); - } - - val = htonl(pCreate->numOfVgroups); - if (val < TSDB_MIN_VNODES_PER_DB || val > TSDB_MAX_VNODES_PER_DB) { - snprintf(msg, tListLen(msg), "invalid number of vgroups for DB:%d valid range: [%d, %d]", val, - TSDB_MIN_VNODES_PER_DB, TSDB_MAX_VNODES_PER_DB); - } - - val = htonl(pCreate->maxRows); - if (val < TSDB_MIN_MAX_ROW_FBLOCK || val > TSDB_MAX_MAX_ROW_FBLOCK) { - snprintf(msg, tListLen(msg), "invalid number of max rows in file block for DB:%d valid range: [%d, %d]", val, - TSDB_MIN_MAX_ROW_FBLOCK, TSDB_MAX_MAX_ROW_FBLOCK); - } - - val = htonl(pCreate->minRows); - if (val < TSDB_MIN_MIN_ROW_FBLOCK || val > TSDB_MAX_MIN_ROW_FBLOCK) { - snprintf(msg, tListLen(msg), "invalid number of min rows in file block for DB:%d valid range: [%d, %d]", val, - TSDB_MIN_MIN_ROW_FBLOCK, TSDB_MAX_MIN_ROW_FBLOCK); - } - - return TSDB_CODE_SUCCESS; -} - static int32_t validateTableColumns(SArray* pFieldList, int32_t maxRowLength, int32_t maxColumns, SMsgBuf* pMsgBuf) { const char* msg2 = "row length exceeds max length"; const char* msg3 = "duplicated column names"; @@ -852,11 +759,15 @@ SDclStmtInfo* qParserValidateDclSqlNode(SSqlInfo* pInfo, SParseContext* pCtx, ch goto _error; } - SUseDbReq* pUseDbMsg = (SUseDbReq*)calloc(1, sizeof(SUseDbReq)); - tNameExtractFullName(&n, pUseDbMsg->db); + SUseDbReq usedbReq = {0}; + tNameExtractFullName(&n, usedbReq.db); - pDcl->pMsg = (char*)pUseDbMsg; - pDcl->msgLen = sizeof(SUseDbReq); + int32_t bufLen = tSerializeSUseDbReq(NULL, 0, &usedbReq); + void* pBuf = malloc(bufLen); + tSerializeSUseDbReq(pBuf, bufLen, &usedbReq); + + pDcl->pMsg = pBuf; + pDcl->msgLen = bufLen; pDcl->msgType = TDMT_MND_USE_DB; break; } @@ -880,14 +791,11 @@ SDclStmtInfo* qParserValidateDclSqlNode(SSqlInfo* pInfo, SParseContext* pCtx, ch goto _error; } - SCreateDbReq* pCreateMsg = buildCreateDbMsg(pCreateDB, pCtx, pMsgBuf); - if (doCheckDbOptions(pCreateMsg, pMsgBuf) != TSDB_CODE_SUCCESS) { - code = TSDB_CODE_TSC_INVALID_OPERATION; - goto _error; - } + int32_t bufLen = 0; + char* pBuf = buildCreateDbMsg(pCreateDB, &bufLen, pCtx, pMsgBuf); - pDcl->pMsg = (char*)pCreateMsg; - pDcl->msgLen = sizeof(SCreateDbReq); + pDcl->pMsg = pBuf; + pDcl->msgLen = bufLen; pDcl->msgType = (pInfo->type == TSDB_SQL_CREATE_DB) ? TDMT_MND_CREATE_DB : TDMT_MND_ALTER_DB; break; } @@ -905,15 +813,18 @@ SDclStmtInfo* qParserValidateDclSqlNode(SSqlInfo* pInfo, SParseContext* pCtx, ch goto _error; } - SDropDbReq* pDropDbMsg = (SDropDbReq*)calloc(1, sizeof(SDropDbReq)); - - code = tNameExtractFullName(&name, pDropDbMsg->db); - pDropDbMsg->ignoreNotExists = pInfo->pMiscInfo->existsCheck ? 1 : 0; + SDropDbReq dropdbReq = {0}; + code = tNameExtractFullName(&name, dropdbReq.db); + dropdbReq.ignoreNotExists = pInfo->pMiscInfo->existsCheck ? 1 : 0; assert(code == TSDB_CODE_SUCCESS && name.type == TSDB_DB_NAME_T); + int32_t bufLen = tSerializeSDropDbReq(NULL, 0, &dropdbReq); + void* pBuf = malloc(bufLen); + tSerializeSDropDbReq(pBuf, bufLen, &dropdbReq); + pDcl->msgType = TDMT_MND_DROP_DB; - pDcl->msgLen = sizeof(SDropDbReq); - pDcl->pMsg = (char*)pDropDbMsg; + pDcl->msgLen = bufLen; + pDcl->pMsg = pBuf; break; } diff --git a/source/libs/qcom/src/querymsg.c b/source/libs/qcom/src/querymsg.c index 4acfb6239b..53ee288930 100644 --- a/source/libs/qcom/src/querymsg.c +++ b/source/libs/qcom/src/querymsg.c @@ -55,32 +55,26 @@ int32_t queryBuildTableMetaReqMsg(void* input, char **msg, int32_t msgSize, int3 return TSDB_CODE_SUCCESS; } -int32_t queryBuildUseDbMsg(void* input, char **msg, int32_t msgSize, int32_t *msgLen) { +int32_t queryBuildUseDbMsg(void *input, char **msg, int32_t msgSize, int32_t *msgLen) { if (NULL == input || NULL == msg || NULL == msgLen) { return TSDB_CODE_TSC_INVALID_INPUT; } - SBuildUseDBInput* bInput = (SBuildUseDBInput *)input; + SBuildUseDBInput *bInput = input; - int32_t estimateSize = sizeof(SUseDbReq); - if (NULL == *msg || msgSize < estimateSize) { - tfree(*msg); - *msg = rpcMallocCont(estimateSize); - if (NULL == *msg) { - return TSDB_CODE_TSC_OUT_OF_MEMORY; - } - } + SUseDbReq usedbReq = {0}; + strncpy(usedbReq.db, bInput->db, sizeof(usedbReq.db)); + usedbReq.db[sizeof(usedbReq.db) - 1] = 0; + usedbReq.vgVersion = bInput->vgVersion; - SUseDbReq *bMsg = (SUseDbReq *)*msg; + int32_t bufLen = tSerializeSUseDbReq(NULL, 0, &usedbReq); + void *pBuf = rpcMallocCont(bufLen); + tSerializeSUseDbReq(pBuf, bufLen, &usedbReq); - strncpy(bMsg->db, bInput->db, sizeof(bMsg->db)); - bMsg->db[sizeof(bMsg->db) - 1] = 0; + *msg = pBuf; + *msgLen = bufLen; - bMsg->vgVersion = bInput->vgVersion; - - *msgLen = (int32_t)sizeof(*bMsg); - - return TSDB_CODE_SUCCESS; + return TSDB_CODE_SUCCESS; } int32_t queryProcessUseDBRsp(void *output, char *msg, int32_t msgSize) { @@ -133,7 +127,7 @@ int32_t queryProcessUseDBRsp(void *output, char *msg, int32_t msgSize) { return code; _return: - taosArrayDestroy(usedbRsp.pVgroupInfos); + tFreeSUsedbRsp(&usedbRsp); if (pOut) { taosHashCleanup(pOut->dbVgroup->vgHash); From a72fe7750b556f849699da26c241bfb1c0b6c12f Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 14 Feb 2022 18:42:42 +0800 Subject: [PATCH 4/7] serialize show req --- include/common/tmsg.h | 11 ++- source/common/src/tmsg.c | 46 +++++++++++- source/dnode/mgmt/impl/test/sut/src/sut.cpp | 14 ++-- source/dnode/mnode/impl/src/mndShow.c | 47 ++++++------ source/dnode/mnode/impl/test/acct/acct.cpp | 9 ++- source/dnode/mnode/impl/test/show/show.cpp | 20 +++-- source/libs/parser/inc/astToMsg.h | 28 +++++-- source/libs/parser/src/astToMsg.c | 83 ++++++++++++++------- source/libs/parser/src/dCDAstProcess.c | 4 +- 9 files changed, 182 insertions(+), 80 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index 52fa0c28f8..b386fb0e0d 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -275,6 +275,7 @@ typedef struct { int32_t tSerializeSMCreateStbReq(void** buf, SMCreateStbReq* pReq); void* tDeserializeSMCreateStbReq(void* buf, SMCreateStbReq* pReq); +void tFreeSMCreateStbReq(SMCreateStbReq* pReq); typedef struct { char name[TSDB_TABLE_FNAME_LEN]; @@ -888,12 +889,16 @@ typedef struct { * payloadLen is the length of payload */ typedef struct { - int8_t type; + int32_t type; char db[TSDB_DB_FNAME_LEN]; - int16_t payloadLen; - char payload[]; + int32_t payloadLen; + char* payload; } SShowReq; +int32_t tSerializeSShowReq(void* buf, int32_t bufLen, SShowReq* pReq); +int32_t tDeserializeSShowReq(void* buf, int32_t bufLen, SShowReq* pReq); +void tFreeSShowReq(SShowReq* pReq); + typedef struct { char db[TSDB_DB_FNAME_LEN]; int32_t numOfVgroup; diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 68924e1be4..fec517bc1b 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -440,6 +440,11 @@ void *tDeserializeSMCreateStbReq(void *buf, SMCreateStbReq *pReq) { return buf; } +void tFreeSMCreateStbReq(SMCreateStbReq *pReq) { + taosArrayDestroy(pReq->pColumns); + taosArrayDestroy(pReq->pTags); +} + int32_t tSerializeSMDropStbReq(void **buf, SMDropStbReq *pReq) { int32_t tlen = 0; @@ -1469,4 +1474,43 @@ void tFreeSUseDbBatchRsp(SUseDbBatchRsp *pRsp) { } taosArrayDestroy(pRsp->pArray); -} \ No newline at end of file +} + +int32_t tSerializeSShowReq(void *buf, int32_t bufLen, SShowReq *pReq) { + SCoder encoder = {0}; + tCoderInit(&encoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_ENCODER); + + if (tStartEncode(&encoder) < 0) return -1; + if (tEncodeI32(&encoder, pReq->type) < 0) return -1; + if (tEncodeCStr(&encoder, pReq->db) < 0) return -1; + if (tEncodeI32(&encoder, pReq->payloadLen) < 0) return -1; + if (pReq->payloadLen > 0) { + if (tEncodeCStr(&encoder, pReq->payload) < 0) return -1; + } + tEndEncode(&encoder); + + int32_t tlen = encoder.pos; + tCoderClear(&encoder); + return tlen; +} + +int32_t tDeserializeSShowReq(void *buf, int32_t bufLen, SShowReq *pReq) { + SCoder decoder = {0}; + tCoderInit(&decoder, TD_LITTLE_ENDIAN, buf, bufLen, TD_DECODER); + + if (tStartDecode(&decoder) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->type) < 0) return -1; + if (tDecodeCStrTo(&decoder, pReq->db) < 0) return -1; + if (tDecodeI32(&decoder, &pReq->payloadLen) < 0) return -1; + if (pReq->payloadLen > 0) { + pReq->payload = malloc(pReq->payloadLen); + if (pReq->payload == NULL) return -1; + if (tDecodeCStrTo(&decoder, pReq->payload) < 0) return -1; + } + + tEndDecode(&decoder); + tCoderClear(&decoder); + return 0; +} + +void tFreeSShowReq(SShowReq *pReq) { free(pReq->payload); } diff --git a/source/dnode/mgmt/impl/test/sut/src/sut.cpp b/source/dnode/mgmt/impl/test/sut/src/sut.cpp index dc8390fba3..7d17be3382 100644 --- a/source/dnode/mgmt/impl/test/sut/src/sut.cpp +++ b/source/dnode/mgmt/impl/test/sut/src/sut.cpp @@ -80,12 +80,16 @@ SRpcMsg* Testbase::SendReq(tmsg_t msgType, void* pCont, int32_t contLen) { } void Testbase::SendShowMetaReq(int8_t showType, const char* db) { - int32_t contLen = sizeof(SShowReq); - SShowReq* pShow = (SShowReq*)rpcMallocCont(contLen); - pShow->type = showType; - strcpy(pShow->db, db); + SShowReq showReq = {0}; + showReq.type = showType; + //strcpy(showReq.db, db); - SRpcMsg* pRsp = SendReq(TDMT_MND_SHOW, pShow, contLen); + int32_t contLen = tSerializeSShowReq(NULL, 0, &showReq); + char* pReq = (char*)rpcMallocCont(contLen); + tSerializeSShowReq(pReq, contLen, &showReq); + tFreeSShowReq(&showReq); + + SRpcMsg* pRsp = SendReq(TDMT_MND_SHOW, pReq, contLen); SShowRsp* pShowRsp = (SShowRsp*)pRsp->pCont; ASSERT(pShowRsp != nullptr); diff --git a/source/dnode/mnode/impl/src/mndShow.c b/source/dnode/mnode/impl/src/mndShow.c index e2ddcee0e9..31e3f539ff 100644 --- a/source/dnode/mnode/impl/src/mndShow.c +++ b/source/dnode/mnode/impl/src/mndShow.c @@ -118,27 +118,28 @@ static void mndReleaseShowObj(SShowObj *pShow, bool forceRemove) { static int32_t mndProcessShowReq(SMnodeMsg *pReq) { SMnode *pMnode = pReq->pMnode; SShowMgmt *pMgmt = &pMnode->showMgmt; - SShowReq *pShowReq = pReq->rpcMsg.pCont; - int8_t type = pShowReq->type; - int16_t payloadLen = htonl(pShowReq->payloadLen); + int32_t code = -1; + SShowReq showReq = {0}; - if (type <= TSDB_MGMT_TABLE_START || type >= TSDB_MGMT_TABLE_MAX) { - terrno = TSDB_CODE_MND_INVALID_MSG_TYPE; - mError("failed to process show-meta req since %s", terrstr()); - return -1; + if (tDeserializeSShowReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &showReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + goto SHOW_OVER; } - ShowMetaFp metaFp = pMgmt->metaFps[type]; + if (showReq.type <= TSDB_MGMT_TABLE_START || showReq.type >= TSDB_MGMT_TABLE_MAX) { + terrno = TSDB_CODE_MND_INVALID_MSG_TYPE; + goto SHOW_OVER; + } + + ShowMetaFp metaFp = pMgmt->metaFps[showReq.type]; if (metaFp == NULL) { terrno = TSDB_CODE_MND_INVALID_MSG_TYPE; - mError("failed to process show-meta req:%s since %s", mndShowStr(type), terrstr()); - return -1; + goto SHOW_OVER; } - SShowObj *pShow = mndCreateShowObj(pMnode, pShowReq); + SShowObj *pShow = mndCreateShowObj(pMnode, &showReq); if (pShow == NULL) { - mError("failed to process show-meta req:%s since %s", mndShowStr(type), terrstr()); - return -1; + goto SHOW_OVER; } int32_t size = sizeof(SShowRsp) + sizeof(SSchema) * TSDB_MAX_COLUMNS + TSDB_EXTRA_PAYLOAD_SIZE; @@ -146,26 +147,30 @@ static int32_t mndProcessShowReq(SMnodeMsg *pReq) { if (pRsp == NULL) { mndReleaseShowObj(pShow, true); terrno = TSDB_CODE_OUT_OF_MEMORY; - mError("show:0x%" PRIx64 ", failed to process show-meta req:%s since malloc rsp error", pShow->id, - mndShowStr(type)); - return -1; + goto SHOW_OVER; } - int32_t code = (*metaFp)(pReq, pShow, &pRsp->tableMeta); - mDebug("show:0x%" PRIx64 ", get meta finished, numOfRows:%d cols:%d type:%s, result:%s", pShow->id, pShow->numOfRows, - pShow->numOfColumns, mndShowStr(type), tstrerror(code)); + code = (*metaFp)(pReq, pShow, &pRsp->tableMeta); + mDebug("show:0x%" PRIx64 ", get meta finished, numOfRows:%d cols:%d showReq.type:%s, result:%s", pShow->id, + pShow->numOfRows, pShow->numOfColumns, mndShowStr(showReq.type), tstrerror(code)); if (code == TSDB_CODE_SUCCESS) { pReq->contLen = sizeof(SShowRsp) + sizeof(SSchema) * pShow->numOfColumns; pReq->pCont = pRsp; pRsp->showId = htobe64(pShow->id); mndReleaseShowObj(pShow, false); - return TSDB_CODE_SUCCESS; } else { rpcFreeCont(pRsp); mndReleaseShowObj(pShow, true); - return code; } + +SHOW_OVER: + if (code != 0) { + mError("failed to process show-meta req since %s", terrstr()); + } + + tFreeSShowReq(&showReq); + return code; } static int32_t mndProcessRetrieveReq(SMnodeMsg *pReq) { diff --git a/source/dnode/mnode/impl/test/acct/acct.cpp b/source/dnode/mnode/impl/test/acct/acct.cpp index 315f23f798..2bc4f40614 100644 --- a/source/dnode/mnode/impl/test/acct/acct.cpp +++ b/source/dnode/mnode/impl/test/acct/acct.cpp @@ -56,10 +56,13 @@ TEST_F(MndTestAcct, 03_Drop_Acct) { } TEST_F(MndTestAcct, 04_Show_Acct) { - int32_t contLen = sizeof(SShowReq); + SShowReq showReq = {0}; + showReq.type = TSDB_MGMT_TABLE_ACCT; - SShowReq* pReq = (SShowReq*)rpcMallocCont(contLen); - pReq->type = TSDB_MGMT_TABLE_ACCT; + int32_t contLen = tSerializeSShowReq(NULL, 0, &showReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSShowReq(pReq, contLen, &showReq); + tFreeSShowReq(&showReq); SRpcMsg* pRsp = test.SendReq(TDMT_MND_SHOW, pReq, contLen); ASSERT_NE(pRsp, nullptr); diff --git a/source/dnode/mnode/impl/test/show/show.cpp b/source/dnode/mnode/impl/test/show/show.cpp index bc31630ffe..e7e17d65c6 100644 --- a/source/dnode/mnode/impl/test/show/show.cpp +++ b/source/dnode/mnode/impl/test/show/show.cpp @@ -26,11 +26,13 @@ class MndTestShow : public ::testing::Test { Testbase MndTestShow::test; TEST_F(MndTestShow, 01_ShowMsg_InvalidMsgMax) { - int32_t contLen = sizeof(SShowReq); + SShowReq showReq = {0}; + showReq.type = TSDB_MGMT_TABLE_MAX; - SShowReq* pReq = (SShowReq*)rpcMallocCont(contLen); - pReq->type = TSDB_MGMT_TABLE_MAX; - strcpy(pReq->db, ""); + int32_t contLen = tSerializeSShowReq(NULL, 0, &showReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSShowReq(pReq, contLen, &showReq); + tFreeSShowReq(&showReq); SRpcMsg* pRsp = test.SendReq(TDMT_MND_SHOW, pReq, contLen); ASSERT_NE(pRsp, nullptr); @@ -38,11 +40,13 @@ TEST_F(MndTestShow, 01_ShowMsg_InvalidMsgMax) { } TEST_F(MndTestShow, 02_ShowMsg_InvalidMsgStart) { - int32_t contLen = sizeof(SShowReq); + SShowReq showReq = {0}; + showReq.type = TSDB_MGMT_TABLE_START; - SShowReq* pReq = (SShowReq*)rpcMallocCont(sizeof(SShowReq)); - pReq->type = TSDB_MGMT_TABLE_START; - strcpy(pReq->db, ""); + int32_t contLen = tSerializeSShowReq(NULL, 0, &showReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSShowReq(pReq, contLen, &showReq); + tFreeSShowReq(&showReq); SRpcMsg* pRsp = test.SendReq(TDMT_MND_SHOW, pReq, contLen); ASSERT_NE(pRsp, nullptr); diff --git a/source/libs/parser/inc/astToMsg.h b/source/libs/parser/inc/astToMsg.h index a1e4225410..77d6900acf 100644 --- a/source/libs/parser/inc/astToMsg.h +++ b/source/libs/parser/inc/astToMsg.h @@ -1,18 +1,32 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + #ifndef TDENGINE_ASTTOMSG_H #define TDENGINE_ASTTOMSG_H #include "parserInt.h" #include "tmsg.h" - char* buildUserManipulationMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgLen); char* buildAcctManipulationMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgLen); char* buildDropUserMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgLen); -SShowReq* buildShowMsg(SShowInfo* pShowInfo, SParseContext* pParseCtx, SMsgBuf* pMsgBuf); -char* buildCreateDbMsg(SCreateDbInfo* pCreateDbInfo, int32_t *len, SParseContext *pCtx, SMsgBuf* pMsgBuf); -char* buildCreateStbReq(SCreateTableSql* pCreateTableSql, int32_t* len, SParseContext* pParseCtx, SMsgBuf* pMsgBuf); -char* buildDropStableReq(SSqlInfo* pInfo, int32_t* len, SParseContext* pParseCtx, SMsgBuf* pMsgBuf); -char* buildCreateDnodeMsg(SSqlInfo* pInfo, int32_t* len, SMsgBuf* pMsgBuf); -char* buildDropDnodeMsg(SSqlInfo* pInfo, int32_t* len, SMsgBuf* pMsgBuf); +char* buildShowMsg(SShowInfo* pShowInfo, int32_t* outputLen, SParseContext* pParseCtx, SMsgBuf* pMsgBuf); +char* buildCreateDbMsg(SCreateDbInfo* pCreateDbInfo, int32_t* outputLen, SParseContext* pCtx, SMsgBuf* pMsgBuf); +char* buildCreateStbReq(SCreateTableSql* pCreateTableSql, int32_t* outputLen, SParseContext* pParseCtx, SMsgBuf* pMsgBuf); +char* buildDropStableReq(SSqlInfo* pInfo, int32_t* outputLen, SParseContext* pParseCtx, SMsgBuf* pMsgBuf); +char* buildCreateDnodeMsg(SSqlInfo* pInfo, int32_t* outputLen, SMsgBuf* pMsgBuf); +char* buildDropDnodeMsg(SSqlInfo* pInfo, int32_t* outputLen, SMsgBuf* pMsgBuf); #endif // TDENGINE_ASTTOMSG_H diff --git a/source/libs/parser/src/astToMsg.c b/source/libs/parser/src/astToMsg.c index adcd740ef6..19d1569669 100644 --- a/source/libs/parser/src/astToMsg.c +++ b/source/libs/parser/src/astToMsg.c @@ -1,3 +1,18 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * This program is free software: you can use, redistribute, and/or modify + * it under the terms of the GNU Affero General Public License, version 3 + * or later ("AGPL"), as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + #include "astGenerator.h" #include "parserInt.h" #include "parserUtil.h" @@ -11,7 +26,7 @@ char* buildUserManipulationMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, createReq.superUser = (int8_t)pUser->type; if (pUser->type == TSDB_ALTER_USER_PRIVILEGES) { - // pMsg->privilege = (char)pCmd->count; + // pMsg->privilege = (char)pCmd->count; } else { strncpy(createReq.pass, pUser->passwd.z, pUser->passwd.n); } @@ -71,7 +86,7 @@ char* buildAcctManipulationMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, return pReq; } -char* buildDropUserMsg(SSqlInfo* pInfo, int32_t* msgLen, int64_t id, char* msgBuf, int32_t msgBufLen) { +char* buildDropUserMsg(SSqlInfo* pInfo, int32_t* outputLen, int64_t id, char* msgBuf, int32_t msgBufLen) { SDropUserReq dropReq = {0}; SToken* pName = taosArrayGet(pInfo->pMiscInfo->a, 0); @@ -89,30 +104,26 @@ char* buildDropUserMsg(SSqlInfo* pInfo, int32_t* msgLen, int64_t id, char* msgBu } tSerializeSDropUserReq(pReq, tlen, &dropReq); - *msgLen = tlen; + *outputLen = tlen; return pReq; } -SShowReq* buildShowMsg(SShowInfo* pShowInfo, SParseContext* pCtx, SMsgBuf* pMsgBuf) { - SShowReq* pShowMsg = calloc(1, sizeof(SShowReq)); - if (pShowMsg == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return pShowMsg; - } +char* buildShowMsg(SShowInfo* pShowInfo, int32_t* outputLen, SParseContext* pCtx, SMsgBuf* pMsgBuf) { + SShowReq showReq = {.type = pShowInfo->showType}; - pShowMsg->type = pShowInfo->showType; if (pShowInfo->showType != TSDB_MGMT_TABLE_VNODES) { SToken* pPattern = &pShowInfo->pattern; if (pPattern->type > 0) { // only show tables support wildcard query - strncpy(pShowMsg->payload, pPattern->z, pPattern->n); - pShowMsg->payloadLen = htons(pPattern->n); + showReq.payloadLen = pPattern->n; + showReq.payload = malloc(showReq.payloadLen); + strncpy(showReq.payload, pPattern->z, pPattern->n); } } else { SToken* pEpAddr = &pShowInfo->prefix; assert(pEpAddr->n > 0 && pEpAddr->type > 0); - - strncpy(pShowMsg->payload, pEpAddr->z, pEpAddr->n); - pShowMsg->payloadLen = htons(pEpAddr->n); + showReq.payloadLen = pEpAddr->n; + showReq.payload = malloc(showReq.payloadLen); + strncpy(showReq.payload, pEpAddr->z, pEpAddr->n); } if (pShowInfo->showType == TSDB_MGMT_TABLE_STB || pShowInfo->showType == TSDB_MGMT_TABLE_VGROUP) { @@ -121,22 +132,32 @@ SShowReq* buildShowMsg(SShowInfo* pShowInfo, SParseContext* pCtx, SMsgBuf* pMsgB if (pShowInfo->prefix.n > 0) { if (pShowInfo->prefix.n >= TSDB_DB_FNAME_LEN) { terrno = buildInvalidOperationMsg(pMsgBuf, "prefix name is too long"); - tfree(pShowMsg); + tFreeSShowReq(&showReq); return NULL; } tNameSetDbName(&n, pCtx->acctId, pShowInfo->prefix.z, pShowInfo->prefix.n); } else if (pCtx->db == NULL || strlen(pCtx->db) == 0) { terrno = buildInvalidOperationMsg(pMsgBuf, "database is not specified"); - tfree(pShowMsg); + tFreeSShowReq(&showReq); return NULL; } else { tNameSetDbName(&n, pCtx->acctId, pCtx->db, strlen(pCtx->db)); } - tNameGetFullDbName(&n, pShowMsg->db); + tNameGetFullDbName(&n, showReq.db); } - return pShowMsg; + int32_t tlen = tSerializeSShowReq(NULL, 0, &showReq); + void* pReq = malloc(tlen); + if (pReq == NULL) { + terrno = TSDB_CODE_OUT_OF_MEMORY; + return NULL; + } + + tSerializeSShowReq(pReq, tlen, &showReq); + tFreeSShowReq(&showReq); + *outputLen = tlen; + return pReq; } static int32_t setKeepOption(SCreateDbReq* pMsg, const SCreateDbInfo* pCreateDb, SMsgBuf* pMsgBuf) { @@ -330,7 +351,7 @@ static int32_t doCheckDbOptions(SCreateDbReq* pCreate, SMsgBuf* pMsgBuf) { return TSDB_CODE_SUCCESS; } -char* buildCreateDbMsg(SCreateDbInfo* pCreateDbInfo, int32_t* len, SParseContext* pCtx, SMsgBuf* pMsgBuf) { +char* buildCreateDbMsg(SCreateDbInfo* pCreateDbInfo, int32_t* outputLen, SParseContext* pCtx, SMsgBuf* pMsgBuf) { SCreateDbReq createReq = {0}; if (setDbOptions(&createReq, pCreateDbInfo, pMsgBuf) != TSDB_CODE_SUCCESS) { @@ -360,11 +381,12 @@ char* buildCreateDbMsg(SCreateDbInfo* pCreateDbInfo, int32_t* len, SParseContext } tSerializeSCreateDbReq(pReq, tlen, &createReq); - *len = tlen; + *outputLen = tlen; return pReq; } -char* buildCreateStbReq(SCreateTableSql* pCreateTableSql, int32_t* len, SParseContext* pParseCtx, SMsgBuf* pMsgBuf) { +char* buildCreateStbReq(SCreateTableSql* pCreateTableSql, int32_t* outputLen, SParseContext* pParseCtx, + SMsgBuf* pMsgBuf) { SMCreateStbReq createReq = {0}; createReq.igExists = pCreateTableSql->existCheck ? 1 : 0; createReq.pColumns = pCreateTableSql->colInfo.pColumns; @@ -374,11 +396,13 @@ char* buildCreateStbReq(SCreateTableSql* pCreateTableSql, int32_t* len, SParseCo SName n = {0}; if (createSName(&n, &pCreateTableSql->name, pParseCtx, pMsgBuf) != 0) { + tFreeSMCreateStbReq(&createReq); return NULL; } if (tNameExtractFullName(&n, createReq.name) != 0) { buildInvalidOperationMsg(pMsgBuf, "invalid table name or database not specified"); + tFreeSMCreateStbReq(&createReq); return NULL; } @@ -391,11 +415,12 @@ char* buildCreateStbReq(SCreateTableSql* pCreateTableSql, int32_t* len, SParseCo void* pBuf = pReq; tSerializeSMCreateStbReq(&pBuf, &createReq); - *len = tlen; + tFreeSMCreateStbReq(&createReq); + *outputLen = tlen; return pReq; } -char* buildDropStableReq(SSqlInfo* pInfo, int32_t* len, SParseContext* pParseCtx, SMsgBuf* pMsgBuf) { +char* buildDropStableReq(SSqlInfo* pInfo, int32_t* outputLen, SParseContext* pParseCtx, SMsgBuf* pMsgBuf) { SToken* tableName = taosArrayGet(pInfo->pMiscInfo->a, 0); SName name = {0}; @@ -420,11 +445,11 @@ char* buildDropStableReq(SSqlInfo* pInfo, int32_t* len, SParseContext* pParseCtx void* pBuf = pReq; tSerializeSMDropStbReq(&pBuf, &dropReq); - *len = tlen; + *outputLen = tlen; return pReq; } -char* buildCreateDnodeMsg(SSqlInfo* pInfo, int32_t* len, SMsgBuf* pMsgBuf) { +char* buildCreateDnodeMsg(SSqlInfo* pInfo, int32_t* outputLen, SMsgBuf* pMsgBuf) { const char* msg1 = "invalid host name (name too long, maximum length 128)"; const char* msg2 = "dnode name can not be string"; const char* msg3 = "port should be an integer that is less than 65535 and greater than 0"; @@ -469,11 +494,11 @@ char* buildCreateDnodeMsg(SSqlInfo* pInfo, int32_t* len, SMsgBuf* pMsgBuf) { } tSerializeSCreateDnodeReq(pReq, tlen, &createReq); - *len = tlen; + *outputLen = tlen; return pReq; } -char* buildDropDnodeMsg(SSqlInfo* pInfo, int32_t* len, SMsgBuf* pMsgBuf) { +char* buildDropDnodeMsg(SSqlInfo* pInfo, int32_t* outputLen, SMsgBuf* pMsgBuf) { SDropDnodeReq dropReq = {0}; SToken* pzName = taosArrayGet(pInfo->pMiscInfo->a, 0); @@ -494,6 +519,6 @@ char* buildDropDnodeMsg(SSqlInfo* pInfo, int32_t* len, SMsgBuf* pMsgBuf) { } tSerializeSDropDnodeReq(pReq, tlen, &dropReq); - *len = tlen; + *outputLen = tlen; return pReq; } \ No newline at end of file diff --git a/source/libs/parser/src/dCDAstProcess.c b/source/libs/parser/src/dCDAstProcess.c index 1a933f5d85..6283b56754 100644 --- a/source/libs/parser/src/dCDAstProcess.c +++ b/source/libs/parser/src/dCDAstProcess.c @@ -114,12 +114,10 @@ static int32_t setShowInfo(SShowInfo* pShowInfo, SParseContext* pCtx, void** out } *pEpSet = pCtx->mgmtEpSet; - *output = buildShowMsg(pShowInfo, pCtx, pMsgBuf); + *output = buildShowMsg(pShowInfo, outputLen, pCtx, pMsgBuf); if (*output == NULL) { return terrno; } - - *outputLen = sizeof(SShowReq) /* + htons(pShowMsg->payloadLen)*/; } return TSDB_CODE_SUCCESS; From 71826d7d2dc75cd625fec2a7ea3c9c280ba1a330 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 14 Feb 2022 18:53:18 +0800 Subject: [PATCH 5/7] minor changes --- source/libs/parser/src/astToMsg.c | 3 --- 1 file changed, 3 deletions(-) diff --git a/source/libs/parser/src/astToMsg.c b/source/libs/parser/src/astToMsg.c index 19d1569669..2bbf9116c2 100644 --- a/source/libs/parser/src/astToMsg.c +++ b/source/libs/parser/src/astToMsg.c @@ -396,13 +396,11 @@ char* buildCreateStbReq(SCreateTableSql* pCreateTableSql, int32_t* outputLen, SP SName n = {0}; if (createSName(&n, &pCreateTableSql->name, pParseCtx, pMsgBuf) != 0) { - tFreeSMCreateStbReq(&createReq); return NULL; } if (tNameExtractFullName(&n, createReq.name) != 0) { buildInvalidOperationMsg(pMsgBuf, "invalid table name or database not specified"); - tFreeSMCreateStbReq(&createReq); return NULL; } @@ -415,7 +413,6 @@ char* buildCreateStbReq(SCreateTableSql* pCreateTableSql, int32_t* outputLen, SP void* pBuf = pReq; tSerializeSMCreateStbReq(&pBuf, &createReq); - tFreeSMCreateStbReq(&createReq); *outputLen = tlen; return pReq; } From fb34af9106cb5a348e09395b1b8e9653dc76b6a4 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 14 Feb 2022 21:00:39 +0800 Subject: [PATCH 6/7] serialize retrieve request --- source/client/src/clientMsgHandler.c | 15 ++++++++------- source/dnode/mgmt/impl/test/sut/src/sut.cpp | 12 +++++++----- source/dnode/mnode/impl/src/mndShow.c | 13 ++++++++----- 3 files changed, 23 insertions(+), 17 deletions(-) diff --git a/source/client/src/clientMsgHandler.c b/source/client/src/clientMsgHandler.c index 0f3e4af575..16b017b0c5 100644 --- a/source/client/src/clientMsgHandler.c +++ b/source/client/src/clientMsgHandler.c @@ -97,14 +97,15 @@ SMsgSendInfo* buildMsgInfoImpl(SRequestObj *pRequest) { if (pRequest->type == TDMT_MND_SHOW_RETRIEVE || pRequest->type == TDMT_VND_SHOW_TABLES_FETCH) { if (pRequest->type == TDMT_MND_SHOW_RETRIEVE) { - SRetrieveTableReq* pRetrieveMsg = calloc(1, sizeof(SRetrieveTableReq)); - if (pRetrieveMsg == NULL) { - return NULL; - } + SRetrieveTableReq retrieveReq = {0}; + retrieveReq.showId = pRequest->body.showInfo.execId; - pRetrieveMsg->showId = htobe64(pRequest->body.showInfo.execId); - pMsgSendInfo->msgInfo.pData = pRetrieveMsg; - pMsgSendInfo->msgInfo.len = sizeof(SRetrieveTableReq); + int32_t contLen = tSerializeSRetrieveTableReq(NULL, 0, &retrieveReq); + void* pReq = malloc(contLen); + tSerializeSRetrieveTableReq(pReq, contLen, &retrieveReq); + + pMsgSendInfo->msgInfo.pData = pReq; + pMsgSendInfo->msgInfo.len = contLen; } else { SVShowTablesFetchReq* pFetchMsg = calloc(1, sizeof(SVShowTablesFetchReq)); if (pFetchMsg == NULL) { diff --git a/source/dnode/mgmt/impl/test/sut/src/sut.cpp b/source/dnode/mgmt/impl/test/sut/src/sut.cpp index 7d17be3382..e91fbd2fb7 100644 --- a/source/dnode/mgmt/impl/test/sut/src/sut.cpp +++ b/source/dnode/mgmt/impl/test/sut/src/sut.cpp @@ -132,13 +132,15 @@ int32_t Testbase::GetMetaNum() { return pMeta->numOfColumns; } const char* Testbase::GetMetaTbName() { return pMeta->tbName; } void Testbase::SendShowRetrieveReq() { - int32_t contLen = sizeof(SRetrieveTableReq); + SRetrieveTableReq retrieveReq = {0}; + retrieveReq.showId = showId; + retrieveReq.free = 0; - SRetrieveTableReq* pRetrieve = (SRetrieveTableReq*)rpcMallocCont(contLen); - pRetrieve->showId = htobe64(showId); - pRetrieve->free = 0; + int32_t contLen = tSerializeSRetrieveTableReq(NULL, 0, &retrieveReq); + void* pReq = rpcMallocCont(contLen); + tSerializeSRetrieveTableReq(pReq, contLen, &retrieveReq); - SRpcMsg* pRsp = SendReq(TDMT_MND_SHOW_RETRIEVE, pRetrieve, contLen); + SRpcMsg* pRsp = SendReq(TDMT_MND_SHOW_RETRIEVE, pReq, contLen); pRetrieveRsp = (SRetrieveTableRsp*)pRsp->pCont; pRetrieveRsp->numOfRows = htonl(pRetrieveRsp->numOfRows); pRetrieveRsp->useconds = htobe64(pRetrieveRsp->useconds); diff --git a/source/dnode/mnode/impl/src/mndShow.c b/source/dnode/mnode/impl/src/mndShow.c index 31e3f539ff..3b470fc769 100644 --- a/source/dnode/mnode/impl/src/mndShow.c +++ b/source/dnode/mnode/impl/src/mndShow.c @@ -180,10 +180,13 @@ static int32_t mndProcessRetrieveReq(SMnodeMsg *pReq) { int32_t size = 0; int32_t rowsRead = 0; - SRetrieveTableReq *pRetrieve = pReq->rpcMsg.pCont; - int64_t showId = htobe64(pRetrieve->showId); + SRetrieveTableReq retrieveReq = {0}; + if (tDeserializeSRetrieveTableReq(pReq->rpcMsg.pCont, pReq->rpcMsg.contLen, &retrieveReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + return -1; + } - SShowObj *pShow = mndAcquireShowObj(pMnode, showId); + SShowObj *pShow = mndAcquireShowObj(pMnode, retrieveReq.showId); if (pShow == NULL) { terrno = TSDB_CODE_MND_INVALID_SHOWOBJ; mError("failed to process show-retrieve req:%p since %s", pShow, terrstr()); @@ -207,7 +210,7 @@ static int32_t mndProcessRetrieveReq(SMnodeMsg *pReq) { pShow->numOfReads = pShow->numOfRows; } - if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) { + if ((retrieveReq.free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) { rowsToRead = pShow->numOfRows - pShow->numOfReads; } @@ -231,7 +234,7 @@ static int32_t mndProcessRetrieveReq(SMnodeMsg *pReq) { } // if free flag is set, client wants to clean the resources - if ((pRetrieve->free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) { + if ((retrieveReq.free & TSDB_QUERY_TYPE_FREE_RESOURCE) != TSDB_QUERY_TYPE_FREE_RESOURCE) { rowsRead = (*retrieveFp)(pReq, pShow, pRsp->data, rowsToRead); } From 08a0bc9b051ecb55dd492ee07a087f719af3a6db Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 14 Feb 2022 22:43:17 +0800 Subject: [PATCH 7/7] minor changes --- source/dnode/mgmt/impl/test/sut/src/sut.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/source/dnode/mgmt/impl/test/sut/src/sut.cpp b/source/dnode/mgmt/impl/test/sut/src/sut.cpp index e91fbd2fb7..61119c4308 100644 --- a/source/dnode/mgmt/impl/test/sut/src/sut.cpp +++ b/source/dnode/mgmt/impl/test/sut/src/sut.cpp @@ -82,7 +82,7 @@ SRpcMsg* Testbase::SendReq(tmsg_t msgType, void* pCont, int32_t contLen) { void Testbase::SendShowMetaReq(int8_t showType, const char* db) { SShowReq showReq = {0}; showReq.type = showType; - //strcpy(showReq.db, db); + strcpy(showReq.db, db); int32_t contLen = tSerializeSShowReq(NULL, 0, &showReq); char* pReq = (char*)rpcMallocCont(contLen);