diff --git a/src/inc/tfs.h b/src/inc/tfs.h index c02dc2bb0f6234dd5dc4e6470abf4b0eb200f913..edc7e37feb69d1d5f96917fd95db75f65adb10d4 100644 --- a/src/inc/tfs.h +++ b/src/inc/tfs.h @@ -36,7 +36,9 @@ int tfsInit(SDiskCfg *pDiskCfg, int ndisk); void tfsDestroy(); int tfsUpdateInfo(); void tfsPrimaryPath(char *dst); -int tfsCreateDir(char *name); +int tfsCreateDir(char *dirname); +int tfsRemoveDir(char *dirname); +int tfsRename(char *oldpath, char *newpath); #ifdef __cplusplus } diff --git a/src/tfs/src/tfs.c b/src/tfs/src/tfs.c index 63c9d056ae00dc21d6fd866bc79834ce563b4749..46e15fc9cfb2f06b11da7881d83e8b5a4ad867df 100644 --- a/src/tfs/src/tfs.c +++ b/src/tfs/src/tfs.c @@ -104,7 +104,7 @@ void tfsPrimaryPath(char *dst) { strncpy(dst, DISK_AT(0, 0)->dir, TSDB_FILENAME_LEN); } -int tfsCreateDir(char *name) { +int tfsCreateDir(char *dirname) { char dirName[TSDB_FILENAME_LEN] = "\0"; for (int level = 0; level < pfs->nlevel; level++) { @@ -114,7 +114,7 @@ int tfsCreateDir(char *name) { ASSERT(pDisk != NULL); - snprintf(dirName, TSDB_FILENAME_LEN, "%s/%s", pDisk->dir, name); + snprintf(dirName, TSDB_FILENAME_LEN, "%s/%s", pDisk->dir, dirname); if (mkdir(dirName, 0755) != 0 && errno != EEXIST) { terrno = TAOS_SYSTEM_ERROR(errno); @@ -126,6 +126,46 @@ int tfsCreateDir(char *name) { return 0; } +int tfsRemoveDir(char *dirname) { + char dirName[TSDB_FILENAME_LEN] = "\0"; + + for (int level = 0; level < pfs->nlevel; level++) { + STier *pTier = TIER_AT(level); + for (int id = 0; id < pTier->ndisk; id++) { + SDisk *pDisk = DISK_AT_TIER(pTier, id); + + ASSERT(pDisk != NULL); + + snprintf(dirName, TSDB_FILENAME_LEN, "%s/%s", pDisk->dir, dirname); + + taosRemoveDir(dirName); + } + } + + return 0; +} + +int tfsRename(char *oldpath, char *newpath) { + char oldName[TSDB_FILENAME_LEN] = "\0"; + char newName[TSDB_FILENAME_LEN] = "\0"; + + for (int level = 0; level < pfs->nlevel; level++) { + STier *pTier = TIER_AT(level); + for (int id = 0; id < pTier->ndisk; id++) { + SDisk *pDisk = DISK_AT_TIER(pTier, id); + + ASSERT(pDisk != NULL); + + snprintf(oldName, TSDB_FILENAME_LEN, "%s/%s", pDisk->dir, oldpath); + snprintf(newName, TSDB_FILENAME_LEN, "%s/%s", pDisk->dir, newpath); + + taosRename(oldName, newName); + } + } + + return 0; +} + static int tfsMount(SDiskCfg *pCfg) { SDiskID did; @@ -134,7 +174,7 @@ static int tfsMount(SDiskCfg *pCfg) { did.level = pCfg->level; did.id = tdAddDiskToTier(TIER_AT(pCfg->level), pCfg); if (did.id < 0) { - fError("failed to add disk %s to FS since %s", pCfg->dir, tstrerror(terrno)); + fError("failed to mount %s to FS since %s", pCfg->dir, tstrerror(terrno)); return -1; } @@ -151,51 +191,51 @@ static int tfsCheckAndFormatCfg(SDiskCfg *pCfg) { struct stat pstat; if (pCfg->level < 0 || pCfg->level >= TSDB_MAX_TIER) { - fError("failed to add disk %s to FS since invalid level %d", pCfg->dir, pCfg->level); + fError("failed to mount %s to FS since invalid level %d", pCfg->dir, pCfg->level); terrno = TSDB_CODE_FS_INVLD_CFG; return -1; } if (pCfg->primary) { if (pCfg->level != 0) { - fError("failed to add disk %s to FS since disk is primary but level %d not 0", pCfg->dir, pCfg->level); + fError("failed to mount %s to FS since disk is primary but level %d not 0", pCfg->dir, pCfg->level); terrno = TSDB_CODE_FS_INVLD_CFG; return -1; } if (DISK_AT(0, 0) != NULL) { - fError("failed to add disk %s to FS since duplicate primary mount", pCfg->dir, pCfg->level); + fError("failed to mount %s to FS since duplicate primary mount", pCfg->dir, pCfg->level); terrno = TSDB_CODE_FS_DUP_PRIMARY; return -1; } } if (tfsFormatDir(pCfg->dir, dirName) < 0) { - fError("failed to add disk %s to FS since invalid dir format", pCfg->dir); + fError("failed to mount %s to FS since invalid dir format", pCfg->dir); terrno = TSDB_CODE_FS_INVLD_CFG; return -1; } if (tfsGetDiskByName(dirName) != NULL) { - fError("failed to add disk %s to FS since duplicate mount", pCfg->dir); + fError("failed to mount %s to FS since duplicate mount", pCfg->dir); terrno = TSDB_CODE_FS_INVLD_CFG; return -1; } if (access(dirName, W_OK | R_OK | F_OK) != 0) { - fError("failed to add disk %s to FS since no R/W access rights", pCfg->dir); + fError("failed to mount %s to FS since no R/W access rights", pCfg->dir); terrno = TSDB_CODE_FS_INVLD_CFG; return -1; } if (stat(dirName, &pstat) < 0) { - fError("failed to add disk %s to FS since %s", pCfg->dir, strerror(errno)); + fError("failed to mount %s to FS since %s", pCfg->dir, strerror(errno)); terrno = TAOS_SYSTEM_ERROR(errno); return -1; } if (!S_ISDIR(pstat.st_mode)) { - fError("failed to add disk %s to FS since not a directory", pCfg->dir); + fError("failed to mount %s to FS since not a directory", pCfg->dir); terrno = TSDB_CODE_FS_INVLD_CFG; return -1; } diff --git a/src/vnode/src/vnodeMain.c b/src/vnode/src/vnodeMain.c index 6ec8ea87f396244584cce0d7b18d2c89a3a64ae0..d8ea94d282ed326b808b02c7567e57df70bc57ab 100644 --- a/src/vnode/src/vnodeMain.c +++ b/src/vnode/src/vnodeMain.c @@ -21,14 +21,15 @@ #include "trpc.h" #include "tsdb.h" #include "tutil.h" -#include "dnode.h" #include "vnode.h" #include "vnodeInt.h" +#include "query.h" +#include "dnode.h" #include "vnodeCfg.h" #include "vnodeVersion.h" #include "dnodeVWrite.h" #include "dnodeVRead.h" -#include "query.h" +#include "tfs.h" static SHashObj*tsVnodesHash; static void vnodeCleanUp(SVnodeObj *pVnode); @@ -99,32 +100,19 @@ int32_t vnodeCreate(SCreateVnodeMsg *pVnodeCfg) { return TSDB_CODE_SUCCESS; } - if (mkdir(tsVnodeDir, 0755) != 0 && errno != EEXIST) { - vError("vgId:%d, failed to create vnode, reason:%s dir:%s", pVnodeCfg->cfg.vgId, strerror(errno), tsVnodeDir); - if (errno == EACCES) { - return TSDB_CODE_VND_NO_DISK_PERMISSIONS; - } else if (errno == ENOSPC) { - return TSDB_CODE_VND_NO_DISKSPACE; - } else if (errno == ENOENT) { - return TSDB_CODE_VND_NO_SUCH_FILE_OR_DIR; - } else { - return TSDB_CODE_VND_INIT_FAILED; - } + if (tfsCreateDir("vnode") < 0) { + vError("vgId:%d, failed to create vnode dir, reason:%s", pVnodeCfg->cfg.vgId, tstrerror(terrno)); + return terrno; } char rootDir[TSDB_FILENAME_LEN] = {0}; - tdGetVnodeDir(tsDataDir, pVnodeCfg->cfg.vgId, rootDir); - if (mkdir(rootDir, 0755) != 0 && errno != EEXIST) { - vError("vgId:%d, failed to create vnode, reason:%s dir:%s", pVnodeCfg->cfg.vgId, strerror(errno), rootDir); - if (errno == EACCES) { - return TSDB_CODE_VND_NO_DISK_PERMISSIONS; - } else if (errno == ENOSPC) { - return TSDB_CODE_VND_NO_DISKSPACE; - } else if (errno == ENOENT) { - return TSDB_CODE_VND_NO_SUCH_FILE_OR_DIR; - } else { - return TSDB_CODE_VND_INIT_FAILED; - } + sprintf(rootDir, "%s/vnode%d", tsVnodeDir, pVnodeCfg->cfg.vgId); + + char vnodeDir[TSDB_FILENAME_LEN] = "\0"; + snprintf(vnodeDir, TSDB_FILENAME_LEN, "vnode%d", pVnodeCfg->cfg.vgId); + if (tfsCreateDir(vnodeDir) < 0) { + vError("vgId:%d, failed to create vnode %d dir, reason:%s", pVnodeCfg->cfg.vgId, strerror(errno)); + return terrno; } code = vnodeWriteCfg(pVnodeCfg); @@ -146,7 +134,7 @@ int32_t vnodeCreate(SCreateVnodeMsg *pVnodeCfg) { tsdbCfg.update = pVnodeCfg->cfg.update; char tsdbDir[TSDB_FILENAME_LEN] = {0}; - tdGetTsdbRootDir(tsDataDir, pVnodeCfg->cfg.vgId, tsdbDir); + sprintf(tsdbDir, "%s/vnode%d/tsdb", tsVnodeDir, pVnodeCfg->cfg.vgId); if (tsdbCreateRepo(tsdbDir, &tsdbCfg) < 0) { vError("vgId:%d, failed to create tsdb in vnode, reason:%s", pVnodeCfg->cfg.vgId, tstrerror(terrno)); return TSDB_CODE_VND_INIT_FAILED; @@ -445,28 +433,17 @@ void vnodeRelease(void *vparam) { if (pVnode->dropped) { char rootDir[TSDB_FILENAME_LEN] = {0}; char newDir[TSDB_FILENAME_LEN] = {0}; + sprintf(rootDir, "%s/vnode%d", "vnode", vgId); + sprintf(newDir, "%s/vnode%d", "vnode_bak", vgId); - for (int i = 0; i < tsDnodeTier->nTiers; i++) { - STier *pTier = tsDnodeTier->tiers + i; - for (int j = 0; j < pTier->nDisks; j++) { - SDisk *pDisk = pTier->disks[j]; - - tdGetVnodeDir(pDisk->dir, vgId, rootDir); - tdGetVnodeBackDir(pDisk->dir, vgId, newDir); - - if (access(rootDir, F_OK) == 0) { - if (0 == tsEnableVnodeBak) { - vInfo("vgId:%d, vnode backup not enabled", pVnode->vgId); - } else { - taosRemoveDir(newDir); - taosRename(rootDir, newDir); - } - - taosRemoveDir(rootDir); - } - } + if (0 == tsEnableVnodeBak) { + vInfo("vgId:%d, vnode backup not enabled", pVnode->vgId); + } else { + tfsRemoveDir(newDir); + tfsRename(rootDir, newDir); } - + + tfsRemoveDir(rootDir); dnodeSendStatusMsgToMnode(); }