From 9f2078ae0d1404983987241d2b649142bf963598 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Tue, 31 Jan 2023 16:28:28 +0800 Subject: [PATCH] enh: rename tsdb file --- source/dnode/mgmt/mgmt_vnode/src/vmHandle.c | 2 +- source/dnode/vnode/src/vnd/vnodeOpen.c | 47 ++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c b/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c index f19d715ce8..3137e749e5 100644 --- a/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c +++ b/source/dnode/mgmt/mgmt_vnode/src/vmHandle.c @@ -342,7 +342,7 @@ int32_t vmProcessAlterHashRangeReq(SVnodeMgmt *pMgmt, SRpcMsg *pMsg) { return -1; } - dInfo("vgId:%d, open vnode", dstVgId); + dInfo("vgId:%d, start to open vnode", dstVgId); SVnode *pImpl = vnodeOpen(dstPath, pMgmt->pTfs, pMgmt->msgCb); if (pImpl == NULL) { dError("vgId:%d, failed to open vnode at %s since %s", dstVgId, dstPath, terrstr()); diff --git a/source/dnode/vnode/src/vnd/vnodeOpen.c b/source/dnode/vnode/src/vnd/vnodeOpen.c index 64cc31596e..68ba8f8b14 100644 --- a/source/dnode/vnode/src/vnd/vnodeOpen.c +++ b/source/dnode/vnode/src/vnd/vnodeOpen.c @@ -107,6 +107,51 @@ int32_t vnodeAlterReplica(const char *path, SAlterVnodeReplicaReq *pReq, STfs *p return 0; } +int32_t vnodeRenameVgroupId(const char *srcPath, const char *dstPath, int32_t srcVgId, int32_t dstVgId, STfs *pTfs) { + int32_t ret = tfsRename(pTfs, srcPath, dstPath); + if (ret != 0) return ret; + + char oldRname[TSDB_FILENAME_LEN] = {0}; + char newRname[TSDB_FILENAME_LEN] = {0}; + char tsdbPath[TSDB_FILENAME_LEN] = {0}; + char tsdbFilePrefix[TSDB_FILENAME_LEN] = {0}; + snprintf(tsdbPath, TSDB_FILENAME_LEN, "%s%stsdb", dstPath, TD_DIRSEP); + snprintf(tsdbFilePrefix, TSDB_FILENAME_LEN, "tsdb%sv", TD_DIRSEP); + + STfsDir *tsdbDir = tfsOpendir(pTfs, tsdbPath); + if (tsdbDir == NULL) return 0; + + while (1) { + const STfsFile *tsdbFile = tfsReaddir(tsdbDir); + if (tsdbFile == NULL) break; + if (tsdbFile->rname == NULL) continue; + tstrncpy(oldRname, tsdbFile->rname, TSDB_FILENAME_LEN); + + char *tsdbFilePrefixPos = strstr(oldRname, tsdbFilePrefix); + if (tsdbFilePrefixPos == NULL) continue; + + int32_t tsdbFileVgId = atoi(tsdbFilePrefixPos + 6); + if (tsdbFileVgId == srcVgId) { + char *tsdbFileSurfixPos = strstr(tsdbFilePrefixPos, "f"); + if (tsdbFileSurfixPos == NULL) continue; + + tsdbFilePrefixPos[6] = 0; + snprintf(newRname, TSDB_FILENAME_LEN, "%s%d%s", oldRname, dstVgId, tsdbFileSurfixPos); + vInfo("vgId:%d, rename file from %s to %s", dstVgId, tsdbFile->rname, newRname); + + ret = tfsRename(pTfs, tsdbFile->rname, newRname); + if (ret != 0) { + vInfo("vgId:%d, failed to rename file from %s to %s since %s", dstVgId, tsdbFile->rname, newRname, terrstr()); + tfsClosedir(tsdbDir); + return ret; + } + } + } + + tfsClosedir(tsdbDir); + return 0; +} + int32_t vnodeAlterHashRange(const char *srcPath, const char *dstPath, SAlterVnodeHashRangeReq *pReq, STfs *pTfs) { SVnodeInfo info = {0}; char dir[TSDB_FILENAME_LEN] = {0}; @@ -158,7 +203,7 @@ int32_t vnodeAlterHashRange(const char *srcPath, const char *dstPath, SAlterVnod } vInfo("vgId:%d, start to rename %s to %s", pReq->dstVgId, srcPath, dstPath); - ret = tfsRename(pTfs, srcPath, dstPath); + ret = vnodeRenameVgroupId(srcPath, dstPath, pReq->srcVgId, pReq->dstVgId, pTfs); if (ret < 0) { vError("vgId:%d, failed to rename vnode from %s to %s since %s", pReq->dstVgId, srcPath, dstPath, tstrerror(terrno)); -- GitLab