From 6e7ab6ca4d62c961bf1ddec897d84da1d8d6ce26 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 16 Apr 2022 08:19:21 +0000 Subject: [PATCH] refact vnode --- source/dnode/vnode/inc/vnode.h | 2 - source/dnode/vnode/src/inc/vnodeInt.h | 4 +- source/dnode/vnode/src/vnd/vnodeOpen.c | 54 ++++++++------------------ 3 files changed, 19 insertions(+), 41 deletions(-) diff --git a/source/dnode/vnode/inc/vnode.h b/source/dnode/vnode/inc/vnode.h index c1e96ffcb1..c969ad5abc 100644 --- a/source/dnode/vnode/inc/vnode.h +++ b/source/dnode/vnode/inc/vnode.h @@ -135,7 +135,6 @@ struct STsdbCfg { struct SVnodeCfg { int32_t vgId; uint64_t dbId; - STfs *pTfs; uint64_t wsize; uint64_t ssize; uint64_t lsize; @@ -146,7 +145,6 @@ struct SVnodeCfg { bool isWeak; STsdbCfg tsdbCfg; SWalCfg walCfg; - SMsgCb msgCb; uint32_t hashBegin; uint32_t hashEnd; int8_t hashMethod; diff --git a/source/dnode/vnode/src/inc/vnodeInt.h b/source/dnode/vnode/src/inc/vnodeInt.h index 04103ec26d..a765f5e88a 100644 --- a/source/dnode/vnode/src/inc/vnodeInt.h +++ b/source/dnode/vnode/src/inc/vnodeInt.h @@ -83,6 +83,8 @@ struct SVnode { char* path; SVnodeCfg config; SVState state; + STfs* pTfs; + SMsgCb msgCb; SVBufPool* pBufPool; SMeta* pMeta; STsdb* pTsdb; @@ -91,8 +93,6 @@ struct SVnode { SSink* pSink; tsem_t canCommit; SQHandle* pQuery; - SMsgCb msgCb; - STfs* pTfs; }; #define TD_VID(PVNODE) (PVNODE)->config.vgId diff --git a/source/dnode/vnode/src/vnd/vnodeOpen.c b/source/dnode/vnode/src/vnd/vnodeOpen.c index ea27cda998..e7aeb75ea5 100644 --- a/source/dnode/vnode/src/vnd/vnodeOpen.c +++ b/source/dnode/vnode/src/vnd/vnodeOpen.c @@ -15,10 +15,8 @@ #include "vnodeInt.h" -static SVnode *vnodeNew(const char *path, const SVnodeCfg *pVnodeCfg); -static void vnodeFree(SVnode *pVnode); -static int vnodeOpenImpl(SVnode *pVnode); -static void vnodeCloseImpl(SVnode *pVnode); +static int vnodeOpenImpl(SVnode *pVnode); +static void vnodeCloseImpl(SVnode *pVnode); int vnodeCreate(const char *path, SVnodeCfg *pCfg, STfs *pTfs) { SVnodeInfo info = {0}; @@ -68,17 +66,24 @@ SVnode *vnodeOpen(const char *path, STfs *pTfs, SMsgCb msgCb) { return NULL; } - info.config.pTfs = pTfs; - info.config.msgCb = msgCb; - - // crate handle - pVnode = vnodeNew(dir, &info.config); + // create handle + pVnode = (SVnode *)taosMemoryCalloc(1, sizeof(*pVnode)); if (pVnode == NULL) { - // TODO: handle error + terrno = TSDB_CODE_OUT_OF_MEMORY; + vError("vgId: %d failed to open vnode since %s", info.config.vgId, tstrerror(terrno)); return NULL; } - // Open the vnode + pVnode->path = strdup(dir); + pVnode->config = info.config; + pVnode->state.committed = info.state.committed; + pVnode->state.processed = pVnode->state.applied = pVnode->state.committed; + pVnode->pTfs = pTfs; + pVnode->msgCb = msgCb; + + tsem_init(&(pVnode->canCommit), 0, 1); + + // open the vnode if (vnodeOpenImpl(pVnode) < 0) { // TODO: handle error return NULL; @@ -90,38 +95,13 @@ SVnode *vnodeOpen(const char *path, STfs *pTfs, SMsgCb msgCb) { void vnodeClose(SVnode *pVnode) { if (pVnode) { vnodeCloseImpl(pVnode); - vnodeFree(pVnode); - } -} - -/* ------------------------ STATIC METHODS ------------------------ */ -static SVnode *vnodeNew(const char *path, const SVnodeCfg *pVnodeCfg) { - SVnode *pVnode = NULL; - - pVnode = (SVnode *)taosMemoryCalloc(1, sizeof(*pVnode)); - if (pVnode == NULL) { - // TODO - return NULL; - } - - pVnode->msgCb = pVnodeCfg->msgCb; - pVnode->pTfs = pVnodeCfg->pTfs; - pVnode->path = strdup(path); - vnodeOptionsCopy(&(pVnode->config), pVnodeCfg); - - tsem_init(&(pVnode->canCommit), 0, 1); - - return pVnode; -} - -static void vnodeFree(SVnode *pVnode) { - if (pVnode) { tsem_destroy(&(pVnode->canCommit)); taosMemoryFreeClear(pVnode->path); taosMemoryFree(pVnode); } } +/* ------------------------ STATIC METHODS ------------------------ */ static int vnodeOpenImpl(SVnode *pVnode) { char dir[TSDB_FILENAME_LEN];