From b01ed3bb80f17273bbed01a0f20b8ac354ce2991 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Mon, 24 Feb 2025 15:32:46 +0800 Subject: [PATCH] more code --- source/dnode/mgmt/exe/dmMain.c | 4 - source/dnode/vnode/src/meta/metaOpen.c | 191 +++---------------------- 2 files changed, 22 insertions(+), 173 deletions(-) diff --git a/source/dnode/mgmt/exe/dmMain.c b/source/dnode/mgmt/exe/dmMain.c index bd7da3f4d6..51476757e5 100644 --- a/source/dnode/mgmt/exe/dmMain.c +++ b/source/dnode/mgmt/exe/dmMain.c @@ -170,8 +170,6 @@ static void dmSetSignalHandle() { #endif } -extern bool generateNewMeta; - static int32_t dmParseArgs(int32_t argc, char const *argv[]) { global.startTime = taosGetTimestampMs(); @@ -210,8 +208,6 @@ static int32_t dmParseArgs(int32_t argc, char const *argv[]) { global.dumpSdb = true; } else if (strcmp(argv[i], "-dTxn") == 0) { global.deleteTrans = true; - } else if (strcmp(argv[i], "-r") == 0) { - generateNewMeta = true; } else if (strcmp(argv[i], "-E") == 0) { if (i < argc - 1) { if (strlen(argv[++i]) >= PATH_MAX) { diff --git a/source/dnode/vnode/src/meta/metaOpen.c b/source/dnode/vnode/src/meta/metaOpen.c index 5e185e3bb2..b80351d144 100644 --- a/source/dnode/vnode/src/meta/metaOpen.c +++ b/source/dnode/vnode/src/meta/metaOpen.c @@ -251,187 +251,40 @@ _exit: return code; } -bool generateNewMeta = false; - -static int32_t metaGenerateNewMeta(SMeta **ppMeta) { - SMeta *pNewMeta = NULL; - SMeta *pMeta = *ppMeta; - SVnode *pVnode = pMeta->pVnode; - - metaInfo("vgId:%d start to generate new meta", TD_VID(pMeta->pVnode)); - - // Open a new meta for orgainzation - int32_t code = metaOpenImpl(pMeta->pVnode, &pNewMeta, VNODE_META_TMP_DIR, false); - if (code) { - return code; - } - - code = metaBegin(pNewMeta, META_BEGIN_HEAP_NIL); - if (code) { - return code; - } - - // i == 0, scan super table - // i == 1, scan normal table and child table - for (int i = 0; i < 2; i++) { - TBC *uidCursor = NULL; - int32_t counter = 0; - - code = tdbTbcOpen(pMeta->pUidIdx, &uidCursor, NULL); - if (code) { - metaError("vgId:%d failed to open uid index cursor, reason:%s", TD_VID(pVnode), tstrerror(code)); - return code; - } - - code = tdbTbcMoveToFirst(uidCursor); - if (code) { - metaError("vgId:%d failed to move to first, reason:%s", TD_VID(pVnode), tstrerror(code)); - tdbTbcClose(uidCursor); - return code; - } - - for (;;) { - const void *pKey; - int kLen; - const void *pVal; - int vLen; - - if (tdbTbcGet(uidCursor, &pKey, &kLen, &pVal, &vLen) < 0) { - break; - } - - tb_uid_t uid = *(tb_uid_t *)pKey; - SUidIdxVal *pUidIdxVal = (SUidIdxVal *)pVal; - if ((i == 0 && (pUidIdxVal->suid && pUidIdxVal->suid == uid)) // super table - || (i == 1 && (pUidIdxVal->suid == 0 || pUidIdxVal->suid != uid)) // normal table and child table - ) { - counter++; - if (i == 0) { - metaInfo("vgId:%d counter:%d new meta handle %s table uid:%" PRId64, TD_VID(pVnode), counter, "super", uid); - } else { - metaInfo("vgId:%d counter:%d new meta handle %s table uid:%" PRId64, TD_VID(pVnode), counter, - pUidIdxVal->suid == 0 ? "normal" : "child", uid); - } - - // fetch table entry - void *value = NULL; - int valueSize = 0; - if (tdbTbGet(pMeta->pTbDb, - &(STbDbKey){ - .version = pUidIdxVal->version, - .uid = uid, - }, - sizeof(uid), &value, &valueSize) == 0) { - SDecoder dc = {0}; - SMetaEntry me = {0}; - tDecoderInit(&dc, value, valueSize); - if (metaDecodeEntry(&dc, &me) == 0) { - if (me.type == TSDB_CHILD_TABLE && - tdbTbGet(pMeta->pUidIdx, &me.ctbEntry.suid, sizeof(me.ctbEntry.suid), NULL, NULL) != 0) { - metaError("vgId:%d failed to get super table uid:%" PRId64 " for child table uid:%" PRId64, - TD_VID(pVnode), me.ctbEntry.suid, uid); - } else if (metaHandleEntry2(pNewMeta, &me) != 0) { - metaError("vgId:%d failed to handle entry, uid:%" PRId64, TD_VID(pVnode), uid); - } - } - tDecoderClear(&dc); - } - tdbFree(value); - } - - code = tdbTbcMoveToNext(uidCursor); - if (code) { - metaError("vgId:%d failed to move to next, reason:%s", TD_VID(pVnode), tstrerror(code)); - return code; - } - } - - tdbTbcClose(uidCursor); - } - - code = metaCommit(pNewMeta, pNewMeta->txn); - if (code) { - metaError("vgId:%d failed to commit, reason:%s", TD_VID(pVnode), tstrerror(code)); - return code; - } - - code = metaFinishCommit(pNewMeta, pNewMeta->txn); - if (code) { - metaError("vgId:%d failed to finish commit, reason:%s", TD_VID(pVnode), tstrerror(code)); - return code; - } - - if ((code = metaBegin(pNewMeta, META_BEGIN_HEAP_NIL)) != 0) { - metaError("vgId:%d failed to begin new meta, reason:%s", TD_VID(pVnode), tstrerror(code)); - } - metaClose(&pNewMeta); - metaInfo("vgId:%d finish to generate new meta", TD_VID(pVnode)); - return 0; +void vnodeGetMetaPath(SVnode *pVnode, const char *metaDir, char *fname) { + vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, fname, TSDB_FILENAME_LEN); + int32_t offset = strlen(fname); + snprintf(fname + offset, TSDB_FILENAME_LEN - offset - 1, "%s%s", TD_DIRSEP, metaDir); } int32_t metaOpen(SVnode *pVnode, SMeta **ppMeta, int8_t rollback) { - if (generateNewMeta) { - char path[TSDB_FILENAME_LEN] = {0}; - char oldMetaPath[TSDB_FILENAME_LEN] = {0}; - char newMetaPath[TSDB_FILENAME_LEN] = {0}; - char backupMetaPath[TSDB_FILENAME_LEN] = {0}; + int32_t code = TSDB_CODE_SUCCESS; + char metaDir[TSDB_FILENAME_LEN] = {0}; + char metaTempDir[TSDB_FILENAME_LEN] = {0}; - vnodeGetPrimaryDir(pVnode->path, pVnode->diskPrimary, pVnode->pTfs, path, TSDB_FILENAME_LEN); - snprintf(oldMetaPath, sizeof(oldMetaPath) - 1, "%s%s%s", path, TD_DIRSEP, VNODE_META_DIR); - snprintf(newMetaPath, sizeof(newMetaPath) - 1, "%s%s%s", path, TD_DIRSEP, VNODE_META_TMP_DIR); - snprintf(backupMetaPath, sizeof(backupMetaPath) - 1, "%s%s%s", path, TD_DIRSEP, VNODE_META_BACKUP_DIR); + vnodeGetMetaPath(pVnode, VNODE_META_DIR, metaDir); + vnodeGetMetaPath(pVnode, VNODE_META_TMP_DIR, metaTempDir); - bool oldMetaExist = taosCheckExistFile(oldMetaPath); - bool newMetaExist = taosCheckExistFile(newMetaPath); - bool backupMetaExist = taosCheckExistFile(backupMetaPath); - - if ((!backupMetaExist && !oldMetaExist && newMetaExist) // case 2 - || (backupMetaExist && !oldMetaExist && !newMetaExist) // case 4 - || (backupMetaExist && oldMetaExist && newMetaExist) // case 8 - ) { - metaError("vgId:%d invalid meta state, please check", TD_VID(pVnode)); + // Check file states + if (!taosCheckExistFile(metaDir)) { + if (!taosCheckExistFile(metaTempDir)) { + metaError("vgId:%d, cannot find meta dir:%s and meta temp dir:%s", TD_VID(pVnode), metaDir, metaTempDir); return TSDB_CODE_FAILED; - } else if ((backupMetaExist && oldMetaExist && !newMetaExist) // case 7 - || (!backupMetaExist && !oldMetaExist && !newMetaExist) // case 1 - ) { - return metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback); - } else if (backupMetaExist && !oldMetaExist && newMetaExist) { - if (taosRenameFile(newMetaPath, oldMetaPath) != 0) { - metaError("vgId:%d failed to rename new meta to old meta, reason:%s", TD_VID(pVnode), tstrerror(terrno)); - return terrno; - } - return metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback); } else { - int32_t code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback); + code = taosRenameFile(metaTempDir, metaDir); if (code) { - return code; - } - - code = metaGenerateNewMeta(ppMeta); - if (code) { - metaError("vgId:%d failed to generate new meta, reason:%s", TD_VID(pVnode), tstrerror(code)); - } - - metaClose(ppMeta); - if (taosRenameFile(oldMetaPath, backupMetaPath) != 0) { - metaError("vgId:%d failed to rename old meta to backup, reason:%s", TD_VID(pVnode), tstrerror(terrno)); - return terrno; - } - - // rename the new meta to old meta - if (taosRenameFile(newMetaPath, oldMetaPath) != 0) { - metaError("vgId:%d failed to rename new meta to old meta, reason:%s", TD_VID(pVnode), tstrerror(terrno)); - return terrno; - } - code = metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, false); - if (code) { - metaError("vgId:%d failed to open new meta, reason:%s", TD_VID(pVnode), tstrerror(code)); + metaError("vgId:%d, %s failed at %s:%d since %s: rename %s to %s failed", TD_VID(pVnode), __func__, __FILE__, + __LINE__, tstrerror(code), metaTempDir, metaDir); return code; } } + } - } else { - return metaOpenImpl(pVnode, ppMeta, VNODE_META_DIR, rollback); + // Do open meta + code = metaOpenImpl(pVnode, ppMeta, metaDir, rollback); + if (code) { + metaError("vgId:%d, %s failed at %s:%d since %s", TD_VID(pVnode), __func__, __FILE__, __LINE__, tstrerror(code)); + return code; } return TSDB_CODE_SUCCESS;