From db61fbb6a222c62f776e9f064accf86d346e7139 Mon Sep 17 00:00:00 2001 From: yihaoDeng Date: Sat, 4 Feb 2023 12:52:02 +0800 Subject: [PATCH] opt index --- include/common/tmsg.h | 8 +- source/common/src/tmsg.c | 14 +-- source/dnode/mnode/impl/src/mndIndex.c | 129 +++++++++++++++++++++---- 3 files changed, 113 insertions(+), 38 deletions(-) diff --git a/include/common/tmsg.h b/include/common/tmsg.h index abf35c67cc..9fd9c4b6de 100644 --- a/include/common/tmsg.h +++ b/include/common/tmsg.h @@ -2811,13 +2811,7 @@ typedef struct { int32_t tSerializeSCreateTagIdxReq(void* buf, int32_t bufLen, SCreateTagIndexReq* pReq); int32_t tDeserializeSCreateTagIdxReq(void* buf, int32_t bufLen, SCreateTagIndexReq* pReq); -typedef struct { - char dbFName[TSDB_DB_FNAME_LEN]; - char stbName[TSDB_TABLE_FNAME_LEN]; - char colName[TSDB_COL_NAME_LEN]; - char idxName[TSDB_COL_NAME_LEN]; - int8_t idxType; -} SDropTagIndexReq; +typedef SMDropSmaReq SDropTagIndexReq; int32_t tSerializeSDropTagIdxReq(void* buf, int32_t bufLen, SDropTagIndexReq* pReq); int32_t tDeserializeSDropTagIdxReq(void* buf, int32_t bufLen, SDropTagIndexReq* pReq); diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index f44f023704..3e6bd4b4dd 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -957,11 +957,8 @@ int32_t tSerializeSDropTagIdxReq(void *buf, int32_t bufLen, SDropTagIndexReq *pR if (tStartEncode(&encoder) < 0) return -1; tEndEncode(&encoder); - if (tEncodeCStr(&encoder, pReq->dbFName) < 0) return -1; - if (tEncodeCStr(&encoder, pReq->stbName) < 0) return -1; - if (tEncodeCStr(&encoder, pReq->colName) < 0) return -1; - if (tEncodeCStr(&encoder, pReq->idxName) < 0) return -1; - if (tEncodeI8(&encoder, pReq->idxType) < 0) return -1; + if (tEncodeCStr(&encoder, pReq->name) < 0) return -1; + if (tEncodeI8(&encoder, pReq->igNotExists) < 0) return -1; int32_t tlen = encoder.pos; tEncoderClear(&encoder); @@ -972,11 +969,8 @@ int32_t tDeserializeSDropTagIdxReq(void *buf, int32_t bufLen, SDropTagIndexReq * tDecoderInit(&decoder, buf, bufLen); if (tStartDecode(&decoder) < 0) return -1; - if (tDecodeCStrTo(&decoder, pReq->dbFName) < 0) return -1; - if (tDecodeCStrTo(&decoder, pReq->stbName) < 0) return -1; - if (tDecodeCStrTo(&decoder, pReq->colName) < 0) return -1; - if (tDecodeCStrTo(&decoder, pReq->idxName) < 0) return -1; - if (tDecodeI8(&decoder, &pReq->idxType) < 0) return -1; + if (tDecodeCStrTo(&decoder, pReq->name) < 0) return -1; + if (tDecodeI8(&decoder, &pReq->igNotExists) < 0) return -1; tEndDecode(&decoder); tDecoderClear(&decoder); diff --git a/source/dnode/mnode/impl/src/mndIndex.c b/source/dnode/mnode/impl/src/mndIndex.c index b31a529836..6079ed3b37 100644 --- a/source/dnode/mnode/impl/src/mndIndex.c +++ b/source/dnode/mnode/impl/src/mndIndex.c @@ -122,6 +122,42 @@ int mndSetCreateIdxRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStb return 0; } +int mndSetDropIdxRedoActions(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb, SIdxObj *pIdx) { + SSdb *pSdb = pMnode->pSdb; + SVgObj *pVgroup = NULL; + void *pIter = NULL; + int32_t contLen; + + while (1) { + pIter = sdbFetch(pSdb, SDB_VGROUP, pIter, (void **)&pVgroup); + if (pIter == NULL) break; + if (!mndVgroupInDb(pVgroup, pDb->uid)) { + sdbRelease(pSdb, pVgroup); + continue; + } + + void *pReq = mndBuildVCreateStbReq(pMnode, pVgroup, pStb, &contLen, NULL, 0); + if (pReq == NULL) { + sdbCancelFetch(pSdb, pIter); + sdbRelease(pSdb, pVgroup); + return -1; + } + STransAction action = {0}; + action.epSet = mndGetVgroupEpset(pMnode, pVgroup); + action.pCont = pReq; + action.contLen = contLen; + action.msgType = TDMT_VND_DROP_INDEX; + if (mndTransAppendRedoAction(pTrans, &action) != 0) { + taosMemoryFree(pReq); + sdbCancelFetch(pSdb, pIter); + sdbRelease(pSdb, pVgroup); + return -1; + } + sdbRelease(pSdb, pVgroup); + } + + return 0; +} void mndCleanupIdx(SMnode *pMnode) { // do nothing @@ -388,14 +424,6 @@ static int32_t mndSetDropIdxCommitLogs(SMnode *pMnode, STrans *pTrans, SIdxObj * return 0; } -static int32_t mndDropIdx(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SIdxObj *pIdx) { - int32_t code = -1; - SVgObj *pVgroup = NULL; - SStbObj *pStb = NULL; - STrans *pTrans = NULL; - return 0; -} - int32_t mndDropIdxsByStb(SMnode *pMnode, STrans *pTrans, SDbObj *pDb, SStbObj *pStb) { // stb return 0; @@ -704,6 +732,7 @@ _OVER: mndTransDrop(pTrans); return code; } + static int32_t mndAddIndex(SMnode *pMnode, SRpcMsg *pReq, SCreateTagIndexReq *req, SDbObj *pDb, SStbObj *pStb) { SIdxObj idxObj = {0}; memcpy(idxObj.name, req->idxName, TSDB_TABLE_FNAME_LEN); @@ -750,21 +779,79 @@ static int32_t mndAddIndex(SMnode *pMnode, SRpcMsg *pReq, SCreateTagIndexReq *re return code; } + +static int32_t mndDropIdx(SMnode *pMnode, SRpcMsg *pReq, SDbObj *pDb, SIdxObj *pIdx) { + int32_t code = -1; + SStbObj *pStb = NULL; + STrans *pTrans = NULL; + + SStbObj newObj = {0}; + + pStb = mndAcquireStb(pMnode, pIdx->stb); + if (pStb == NULL) goto _OVER; + + pTrans = mndTransCreate(pMnode, TRN_POLICY_RETRY, TRN_CONFLICT_DB, pReq, "drop-index"); + if (pTrans == NULL) goto _OVER; + + mInfo("trans:%d, used to drop idx:%s", pTrans->id, pIdx->name); + mndTransSetDbName(pTrans, pDb->name, NULL); + if (mndTrancCheckConflict(pMnode, pTrans) != 0) goto _OVER; + + mndTransSetSerial(pTrans); + if (mndSetDropIdxRedoLogs(pMnode, pTrans, pIdx) != 0) goto _OVER; + if (mndSetDropIdxCommitLogs(pMnode, pTrans, pIdx) != 0) goto _OVER; + + if (mndSetUpdateIdxStbCommitLogs(pMnode, pTrans, pStb, &newObj, pIdx->colName) != 0) goto _OVER; + if (mndSetDropIdxRedoActions(pMnode, pTrans, pStb, &newObj, pIdx) != 0) goto _OVER; + +_OVER: + mndTransDrop(pTrans); + mndReleaseStb(pMnode, pStb); +} static int32_t mndProcessDropIdxReq(SRpcMsg *pReq) { - SMnode *pMnode = pReq->info.node; - int32_t code = -1; - // SDbObj *pDb = NULL; - // SStbObj *pStb = NULL; - // SDropTagIndexReq dropReq = {0}; - // if (tDeserializeSDropTagIdxReq(pReq->pCont, pReq->contLen, &dropReq) != 0) { - // terrno = TSDB_CODE_INVALID_MSG; - // goto _OVER; - // } - // - // return TSDB_CODE_SUCCESS; - //_OVER: - // return code; + SMnode *pMnode = pReq->info.node; + int32_t code = -1; + int32_t code = -1; + SDbObj *pDb = NULL; + SIdxObj *pIdx = NULL; + + SDropTagIndexReq req = {0}; + if (tDeserializeSDropTagIdxReq(pReq->pCont, pReq->contLen, &dropReq) != 0) { + terrno = TSDB_CODE_INVALID_MSG; + goto _OVER; + } + mInfo("idx:%s, start to drop", req.name); + + pIdx = mndAcquireIdx(pMnode, req.name); + if (pIdx == NULL) { + if (req.igNotExists) { + mInfo("idx:%s, not exist, ignore not exist is set", req.name); + code = 0; + goto _OVER; + } else { + terrno = TSDB_CODE_MND_SMA_NOT_EXIST; + goto _OVER; + } + } + + pDb = mndAcquireDbByIdx(pMnode, dropReq.name); + if (pDb == NULL) { + terrno = TSDB_CODE_MND_DB_NOT_SELECTED; + goto _OVER; + } + + if (mndCheckDbPrivilege(pMnode, pReq->info.conn.user, MND_OPER_WRITE_DB, pDb) != 0) { + goto _OVER; + } + + code = mndDropIdx(pMnode, pReq, pDb, pIdx); + if (code == 0) code = TSDB_CODE_ACTION_IN_PROGRESS; return code; + +_OVER: + if (code != 0 && code != TSDB_CODE_ACTION_IN_PROGRESS) { + mError("idx:%s, failed to drop since %s", req.name, terrstr()); + } } static int32_t mndProcessGetIdxReq(SRpcMsg *pReq) { // do nothing