From e29bb8b63cc6d8d2e6e80fa178042193eb3cc1a5 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Sun, 12 Dec 2021 10:46:49 +0800 Subject: [PATCH] TD-10431 alter stb --- include/common/taosmsg.h | 10 +- source/dnode/mgmt/impl/test/CMakeLists.txt | 1 + .../dnode/mgmt/impl/test/dnode/CMakeLists.txt | 29 ++++ source/dnode/mnode/impl/inc/mndInt.h | 2 + source/dnode/mnode/impl/src/mndStb.c | 133 +++++++++++++++++- source/dnode/mnode/impl/src/mnode.c | 7 + 6 files changed, 176 insertions(+), 6 deletions(-) create mode 100644 source/dnode/mgmt/impl/test/dnode/CMakeLists.txt diff --git a/include/common/taosmsg.h b/include/common/taosmsg.h index 5fbbbb2855..456b06b925 100644 --- a/include/common/taosmsg.h +++ b/include/common/taosmsg.h @@ -320,10 +320,14 @@ typedef struct { typedef struct { char name[TSDB_TABLE_FNAME_LEN]; - // if user specify DROP STABLE, this flag will be set. And an error will be returned if it is not a super table - int8_t supertable; int8_t igNotExists; -} SCMDropTableMsg; +} SDropStbMsg; + +typedef struct { + char name[TSDB_TABLE_FNAME_LEN]; + int8_t alterType; + SSchema schema; +} SAlterStbMsg; typedef struct { char tableFname[TSDB_TABLE_FNAME_LEN]; diff --git a/source/dnode/mgmt/impl/test/CMakeLists.txt b/source/dnode/mgmt/impl/test/CMakeLists.txt index b3b6818a5c..b340029044 100644 --- a/source/dnode/mgmt/impl/test/CMakeLists.txt +++ b/source/dnode/mgmt/impl/test/CMakeLists.txt @@ -1,5 +1,6 @@ # add_subdirectory(acct) # add_subdirectory(cluster) +add_subdirectory(dnode) # add_subdirectory(profile) # add_subdirectory(show) add_subdirectory(user) diff --git a/source/dnode/mgmt/impl/test/dnode/CMakeLists.txt b/source/dnode/mgmt/impl/test/dnode/CMakeLists.txt new file mode 100644 index 0000000000..e118cb8725 --- /dev/null +++ b/source/dnode/mgmt/impl/test/dnode/CMakeLists.txt @@ -0,0 +1,29 @@ +add_executable(dndTestDnode "") + +target_sources(dndTestDnode + PRIVATE + "dnode.cpp" + "../sut/deploy.cpp" +) + +target_link_libraries( + dndTestDnode + PUBLIC dnode + PUBLIC util + PUBLIC os + PUBLIC gtest_main +) + +target_include_directories(dndTestDnode + PUBLIC + "${CMAKE_SOURCE_DIR}/include/server/dnode/mgmt" + "${CMAKE_CURRENT_SOURCE_DIR}/../../inc" + "${CMAKE_CURRENT_SOURCE_DIR}/../sut" +) + +enable_testing() + +add_test( + NAME dndTestDnode + COMMAND dndTestDnode +) diff --git a/source/dnode/mnode/impl/inc/mndInt.h b/source/dnode/mnode/impl/inc/mndInt.h index b44d6570fe..e9913803bd 100644 --- a/source/dnode/mnode/impl/inc/mndInt.h +++ b/source/dnode/mnode/impl/inc/mndInt.h @@ -88,6 +88,8 @@ void mndSendMsgToMnode(SMnode *pMnode, SRpcMsg *pMsg); void mndSendRedirectMsg(SMnode *pMnode, SRpcMsg *pMsg); void mndSetMsgHandle(SMnode *pMnode, int32_t msgType, MndMsgFp fp); +uint64_t mndGenerateUid(char *name, int32_t len) ; + #ifdef __cplusplus } #endif diff --git a/source/dnode/mnode/impl/src/mndStb.c b/source/dnode/mnode/impl/src/mndStb.c index 9f9308b2fa..ccd7fda24b 100644 --- a/source/dnode/mnode/impl/src/mndStb.c +++ b/source/dnode/mnode/impl/src/mndStb.c @@ -240,7 +240,7 @@ static int32_t mndCreateStb(SMnode *pMnode, SMnodeMsg *pMsg, SCreateStbMsg *pCre tstrncpy(stbObj.db, pDb->name, TSDB_FULL_DB_NAME_LEN); stbObj.createdTime = taosGetTimestampMs(); stbObj.updateTime = stbObj.createdTime; - stbObj.uid = 1234; + stbObj.uid = mndGenerateUid(pCreate->name, TSDB_TABLE_FNAME_LEN); stbObj.version = 1; stbObj.numOfColumns = pCreate->numOfColumns; stbObj.numOfTags = pCreate->numOfTags; @@ -340,11 +340,138 @@ static int32_t mndProcessCreateStbMsg(SMnodeMsg *pMsg) { static int32_t mndProcessCreateStbInRsp(SMnodeMsg *pMsg) { return 0; } -static int32_t mndProcessAlterStbMsg(SMnodeMsg *pMsg) { return 0; } +static int32_t mndCheckAlterStbMsg(SAlterStbMsg *pAlter) { + SSchema *pSchema = &pAlter->schema; + pSchema->colId = htonl(pSchema->colId); + pSchema->bytes = htonl(pSchema->bytes); + + if (pSchema->type <= 0) { + terrno = TSDB_CODE_MND_STB_INVALID_COL_TYPE; + return -1; + } + if (pSchema->colId < 0 || pSchema->colId >= (TSDB_MAX_COLUMNS + TSDB_MAX_TAGS)) { + terrno = TSDB_CODE_MND_STB_INVALID_COL_ID; + return -1; + } + if (pSchema->bytes <= 0) { + terrno = TSDB_CODE_MND_STB_INVALID_COL_BYTES; + return -1; + } + if (pSchema->name[0] == 0) { + terrno = TSDB_CODE_MND_STB_INVALID_COL_NAME; + return -1; + } + + return 0; +} + +static int32_t mndUpdateStb(SMnode *pMnode, SMnodeMsg *pMsg, SStbObj *pOldStb, SStbObj *pNewStb) { return 0; } + +static int32_t mndProcessAlterStbMsg(SMnodeMsg *pMsg) { + SMnode *pMnode = pMsg->pMnode; + SAlterStbMsg *pAlter = pMsg->rpcMsg.pCont; + + mDebug("stb:%s, start to alter", pAlter->name); + + if (mndCheckAlterStbMsg(pAlter) != 0) { + mError("stb:%s, failed to alter since %s", pAlter->name, terrstr()); + return -1; + } + + SStbObj *pStb = mndAcquireStb(pMnode, pAlter->name); + if (pStb == NULL) { + terrno = TSDB_CODE_MND_STB_NOT_EXIST; + mError("stb:%s, failed to alter since %s", pAlter->name, terrstr()); + return -1; + } + + SStbObj stbObj = {0}; + memcpy(&stbObj, pStb, sizeof(SStbObj)); + + int32_t code = mndUpdateStb(pMnode, pMsg, pStb, &stbObj); + mndReleaseStb(pMnode, pStb); + + if (code != 0) { + mError("stb:%s, failed to alter since %s", pAlter->name, tstrerror(code)); + return code; + } + + return TSDB_CODE_MND_ACTION_IN_PROGRESS; +} static int32_t mndProcessAlterStbInRsp(SMnodeMsg *pMsg) { return 0; } -static int32_t mndProcessDropStbMsg(SMnodeMsg *pMsg) { return 0; } +static int32_t mndDropStb(SMnode *pMnode, SMnodeMsg *pMsg, SStbObj *pStb) { + STrans *pTrans = mndTransCreate(pMnode, TRN_POLICY_ROLLBACK, pMsg->rpcMsg.handle); + if (pTrans == NULL) { + mError("stb:%s, failed to drop since %s", pStb->name, terrstr()); + return -1; + } + mDebug("trans:%d, used to drop stb:%s", pTrans->id, pStb->name); + + SSdbRaw *pRedoRaw = mndStbActionEncode(pStb); + if (pRedoRaw == NULL || mndTransAppendRedolog(pTrans, pRedoRaw) != 0) { + mError("trans:%d, failed to append redo log since %s", pTrans->id, terrstr()); + mndTransDrop(pTrans); + return -1; + } + sdbSetRawStatus(pRedoRaw, SDB_STATUS_DROPPING); + + SSdbRaw *pUndoRaw = mndStbActionEncode(pStb); + if (pUndoRaw == NULL || mndTransAppendUndolog(pTrans, pUndoRaw) != 0) { + mError("trans:%d, failed to append undo log since %s", pTrans->id, terrstr()); + mndTransDrop(pTrans); + return -1; + } + sdbSetRawStatus(pUndoRaw, SDB_STATUS_READY); + + SSdbRaw *pCommitRaw = mndStbActionEncode(pStb); + if (pCommitRaw == NULL || mndTransAppendCommitlog(pTrans, pCommitRaw) != 0) { + mError("trans:%d, failed to append commit log since %s", pTrans->id, terrstr()); + mndTransDrop(pTrans); + return -1; + } + sdbSetRawStatus(pCommitRaw, SDB_STATUS_DROPPED); + + if (mndTransPrepare(pTrans) != 0) { + mError("trans:%d, failed to prepare since %s", pTrans->id, terrstr()); + mndTransDrop(pTrans); + return -1; + } + + mndTransDrop(pTrans); + return 0; +} + +static int32_t mndProcessDropStbMsg(SMnodeMsg *pMsg) { + SMnode *pMnode = pMsg->pMnode; + SDropStbMsg *pDrop = pMsg->rpcMsg.pCont; + + mDebug("stb:%s, start to drop", pDrop->name); + + SStbObj *pStb = mndAcquireStb(pMnode, pDrop->name); + if (pStb == NULL) { + if (pDrop->igNotExists) { + mDebug("stb:%s, not exist, ignore not exist is set", pDrop->name); + return 0; + } else { + terrno = TSDB_CODE_MND_STB_NOT_EXIST; + mError("stb:%s, failed to drop since %s", pDrop->name, terrstr()); + return -1; + } + } + + int32_t code = mndDropStb(pMnode, pMsg, pStb); + mndReleaseStb(pMnode, pStb); + + if (code != 0) { + terrno = code; + mError("stb:%s, failed to drop since %s", pDrop->name, terrstr()); + return -1; + } + + return TSDB_CODE_MND_ACTION_IN_PROGRESS; +} static int32_t mndProcessDropStbInRsp(SMnodeMsg *pMsg) { return 0; } diff --git a/source/dnode/mnode/impl/src/mnode.c b/source/dnode/mnode/impl/src/mnode.c index c9310809d8..f599c4e3ed 100644 --- a/source/dnode/mnode/impl/src/mnode.c +++ b/source/dnode/mnode/impl/src/mnode.c @@ -430,3 +430,10 @@ void mndProcessWriteMsg(SMnodeMsg *pMsg) { mndProcessRpcMsg(pMsg); } void mndProcessSyncMsg(SMnodeMsg *pMsg) { mndProcessRpcMsg(pMsg); } void mndProcessApplyMsg(SMnodeMsg *pMsg) {} + +uint64_t mndGenerateUid(char *name, int32_t len) { + int64_t us = taosGetTimestampUs(); + int32_t hashval = MurmurHash3_32(name, len); + uint64_t x = (us & 0x000000FFFFFFFFFF) << 24; + return x + ((hashval & ((1ul << 16) - 1ul)) << 8) + (taosRand() & ((1ul << 8) - 1ul)); +} \ No newline at end of file