From 9e7b43c836bb27f7e2c5a40e0d9373040caa8d4a Mon Sep 17 00:00:00 2001 From: Cary Xu Date: Sun, 15 May 2022 22:03:02 +0800 Subject: [PATCH] feat: sma code refactor --- source/common/src/tmsg.c | 5 ++--- source/dnode/vnode/inc/vnode.h | 2 +- source/dnode/vnode/src/inc/meta.h | 1 + source/dnode/vnode/src/meta/metaEntry.c | 6 ++++-- source/dnode/vnode/src/meta/metaOpen.c | 9 +++++++++ source/dnode/vnode/src/meta/metaSma.c | 15 +++++++++++---- source/dnode/vnode/src/vnd/vnodeSvr.c | 5 ++--- 7 files changed, 30 insertions(+), 13 deletions(-) diff --git a/source/common/src/tmsg.c b/source/common/src/tmsg.c index 2b164bf6b2..76b3dc0078 100644 --- a/source/common/src/tmsg.c +++ b/source/common/src/tmsg.c @@ -3586,12 +3586,11 @@ int32_t tEncodeTSma(SEncoder *pCoder, const STSma *pSma) { } int32_t tDecodeTSma(SDecoder *pCoder, STSma *pSma) { - if (tDecodeI8(pCoder, &pSma->version) < 0) return -1; if (tDecodeI8(pCoder, &pSma->version) < 0) return -1; if (tDecodeI8(pCoder, &pSma->intervalUnit) < 0) return -1; if (tDecodeI8(pCoder, &pSma->slidingUnit) < 0) return -1; if (tDecodeI8(pCoder, &pSma->timezoneInt) < 0) return -1; - if (tDecodeCStr(pCoder, (const char **)&pSma->indexName) < 0) return -1; + if (tDecodeCStrTo(pCoder, pSma->indexName) < 0) return -1; if (tDecodeI32(pCoder, &pSma->exprLen) < 0) return -1; if (tDecodeI32(pCoder, &pSma->tagsFilterLen) < 0) return -1; if (tDecodeI64(pCoder, &pSma->indexUid) < 0) return -1; @@ -3645,7 +3644,7 @@ int32_t tDecodeSVDropTSmaReq(SDecoder *pCoder, SVDropTSmaReq *pReq) { if (tStartDecode(pCoder) < 0) return -1; if (tDecodeI64(pCoder, &pReq->indexUid) < 0) return -1; - if (tDecodeCStr(pCoder, (const char**)&pReq->indexName) < 0) return -1; + if (tDecodeCStrTo(pCoder, pReq->indexName) < 0) return -1; tEndDecode(pCoder); return 0; diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index be131148fe..a2be393eb2 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -192,7 +192,7 @@ struct SMetaEntry { SSchemaWrapper schema; } ntbEntry; struct { - STSmaWrapper tsma; + STSma *tsma; } smaEntry; }; }; diff --git a/source/dnode/vnode/src/inc/meta.h b/source/dnode/vnode/src/inc/meta.h index 9b52e3d854..c5ca806829 100644 --- a/source/dnode/vnode/src/inc/meta.h +++ b/source/dnode/vnode/src/inc/meta.h @@ -73,6 +73,7 @@ struct SMeta { TDB* pCtbIdx; TDB* pTagIdx; TDB* pTtlIdx; + TDB* pSmaIdx; SMetaIdx* pIdx; }; diff --git a/source/dnode/vnode/src/meta/metaEntry.c b/source/dnode/vnode/src/meta/metaEntry.c index 8ddf3419b5..dd36906b19 100644 --- a/source/dnode/vnode/src/meta/metaEntry.c +++ b/source/dnode/vnode/src/meta/metaEntry.c @@ -36,7 +36,7 @@ int metaEncodeEntry(SEncoder *pCoder, const SMetaEntry *pME) { if (tEncodeI32(pCoder, pME->ntbEntry.ttlDays) < 0) return -1; if (tEncodeSSchemaWrapper(pCoder, &pME->ntbEntry.schema) < 0) return -1; } else if (pME->type == TSDB_TSMA_TABLE) { - if (tEncodeTSmaWrapper(pCoder, &pME->smaEntry.tsma) < 0) return -1; + if (tEncodeTSma(pCoder, pME->smaEntry.tsma) < 0) return -1; } else { ASSERT(0); } @@ -66,7 +66,9 @@ int metaDecodeEntry(SDecoder *pCoder, SMetaEntry *pME) { if (tDecodeI64(pCoder, &pME->ntbEntry.ctime) < 0) return -1; if (tDecodeI32(pCoder, &pME->ntbEntry.ttlDays) < 0) return -1; if (tDecodeSSchemaWrapper(pCoder, &pME->ntbEntry.schema) < 0) return -1; - } else { + } else if (pME->type == TSDB_TSMA_TABLE) { + if (tDecodeTSma(pCoder, pME->smaEntry.tsma) < 0) return -1; + } else { ASSERT(0); } diff --git a/source/dnode/vnode/src/meta/metaOpen.c b/source/dnode/vnode/src/meta/metaOpen.c index db47e794ba..3ce146904c 100644 --- a/source/dnode/vnode/src/meta/metaOpen.c +++ b/source/dnode/vnode/src/meta/metaOpen.c @@ -105,6 +105,13 @@ int metaOpen(SVnode *pVnode, SMeta **ppMeta) { goto _err; } + // open pSmaIdx + ret = tdbDbOpen("sma.idx", sizeof(SSmaIdxKey), 0, smaIdxKeyCmpr, pMeta->pEnv, &pMeta->pSmaIdx); + if (ret < 0) { + metaError("vgId:%d failed to open meta sma index since %s", TD_VID(pVnode), tstrerror(terrno)); + goto _err; + } + // open index if (metaOpenIdx(pMeta) < 0) { metaError("vgId:%d failed to open meta index since %s", TD_VID(pVnode), tstrerror(terrno)); @@ -118,6 +125,7 @@ int metaOpen(SVnode *pVnode, SMeta **ppMeta) { _err: if (pMeta->pIdx) metaCloseIdx(pMeta); + if (pMeta->pSmaIdx) tdbDbClose(pMeta->pSmaIdx); if (pMeta->pTtlIdx) tdbDbClose(pMeta->pTtlIdx); if (pMeta->pTagIdx) tdbDbClose(pMeta->pTagIdx); if (pMeta->pCtbIdx) tdbDbClose(pMeta->pCtbIdx); @@ -134,6 +142,7 @@ _err: int metaClose(SMeta *pMeta) { if (pMeta) { if (pMeta->pIdx) metaCloseIdx(pMeta); + if (pMeta->pSmaIdx) tdbDbClose(pMeta->pSmaIdx); if (pMeta->pTtlIdx) tdbDbClose(pMeta->pTtlIdx); if (pMeta->pTagIdx) tdbDbClose(pMeta->pTagIdx); if (pMeta->pCtbIdx) tdbDbClose(pMeta->pCtbIdx); diff --git a/source/dnode/vnode/src/meta/metaSma.c b/source/dnode/vnode/src/meta/metaSma.c index e7d4d3e117..8ce7ea5895 100644 --- a/source/dnode/vnode/src/meta/metaSma.c +++ b/source/dnode/vnode/src/meta/metaSma.c @@ -23,8 +23,6 @@ int32_t metaCreateTSma(SMeta *pMeta, int64_t version, SSmaCfg *pCfg) { // The table uid should exists and be super table or normal table. // Check other cfg value - // TODO: add atomicity - SMetaEntry me = {0}; int kLen = 0; int vLen = 0; @@ -55,7 +53,7 @@ int32_t metaCreateTSma(SMeta *pMeta, int64_t version, SSmaCfg *pCfg) { me.type = TSDB_TSMA_TABLE; me.uid = pCfg->indexUid; me.name = pCfg->indexName; - // me.smaEntry = xx; + me.smaEntry.tsma = pCfg; if (metaHandleSmaEntry(pMeta, &me) < 0) goto _err; @@ -180,8 +178,15 @@ _err: return -1; } +static int metaUpdateUidIdx(SMeta *pMeta, const SMetaEntry *pME) { + return tdbDbInsert(pMeta->pUidIdx, &pME->uid, sizeof(tb_uid_t), &pME->version, sizeof(int64_t), &pMeta->txn); +} +static int metaUpdateSmaIdx(SMeta *pMeta, const SMetaEntry *pME) { + SSmaIdxKey smaIdxKey = {.uid = pME->smaEntry.tsma->tableUid, .smaUid = pME->smaEntry.tsma->indexUid}; + return tdbDbInsert(pMeta->pSmaIdx, &smaIdxKey, sizeof(smaIdxKey), NULL, 0, &pMeta->txn); +} static int metaHandleSmaEntry(SMeta *pMeta, const SMetaEntry *pME) { metaWLock(pMeta); @@ -190,7 +195,9 @@ static int metaHandleSmaEntry(SMeta *pMeta, const SMetaEntry *pME) { if (metaSaveSmaToDB(pMeta, pME) < 0) goto _err; // // update uid.idx - // if (metaUpdateUidIdx(pMeta, pME) < 0) goto _err; + if (metaUpdateUidIdx(pMeta, pME) < 0) goto _err; + + if (metaUpdateSmaIdx(pMeta, pME) < 0) goto _err; metaULock(pMeta); return 0; diff --git a/source/dnode/vnode/src/vnd/vnodeSvr.c b/source/dnode/vnode/src/vnd/vnodeSvr.c index 6e3b46bf91..f638ac056a 100644 --- a/source/dnode/vnode/src/vnd/vnodeSvr.c +++ b/source/dnode/vnode/src/vnd/vnodeSvr.c @@ -670,12 +670,11 @@ static int vnodeProcessCreateTSmaReq(SVnode *pVnode, int64_t version, void *pReq pRsp->code = terrno; goto _err; } -#if 0 - if (metaCreateSTable(pVnode->pMeta, version, &req) < 0) { + + if (metaCreateTSma(pVnode->pMeta, version, &req) < 0) { pRsp->code = terrno; goto _err; } -#endif tDecoderClear(&coder); return 0;