From cd8a54cbf7dfd9adb80091323950eec814c8540f Mon Sep 17 00:00:00 2001 From: kailixu Date: Sun, 21 Jul 2024 11:43:39 +0800 Subject: [PATCH] enh: return error code --- include/libs/tfs/tfs.h | 6 +- source/dnode/mgmt/node_mgmt/src/dmEnv.c | 7 +- source/dnode/mnode/impl/src/mndUser.c | 16 +- source/dnode/vnode/src/sma/smaCommit.c | 13 +- source/dnode/vnode/src/tsdb/tsdbFS2.c | 8 +- source/dnode/vnode/src/vnd/vnodeOpen.c | 3 +- source/dnode/vnode/test/tsdbSmaTest.cpp | 2 +- source/libs/tfs/src/tfs.c | 218 ++++++++++++------------ source/libs/tfs/src/tfsDisk.c | 8 +- source/libs/tfs/src/tfsTier.c | 8 +- source/libs/tfs/test/tfsTest.cpp | 33 ++-- 11 files changed, 167 insertions(+), 155 deletions(-) diff --git a/include/libs/tfs/tfs.h b/include/libs/tfs/tfs.h index 2b90e3226c..446c1d6fd7 100644 --- a/include/libs/tfs/tfs.h +++ b/include/libs/tfs/tfs.h @@ -44,9 +44,9 @@ typedef struct { * * @param pCfg Config of the fs. * @param ndisk Length of the config. - * @return STfs* The fs object. + * @param ppTfs The fs object. */ -STfs *tfsOpen(SDiskCfg *pCfg, int32_t ndisk); +int32_t tfsOpen(SDiskCfg *pCfg, int32_t ndisk, STfs **ppTfs); /** * @brief Close a fs. @@ -275,7 +275,7 @@ int32_t tfsCopyFile(const STfsFile *pFile1, const STfsFile *pFile2); * @param rname The rel name of file. * @return STfsDir* The dir object. */ -STfsDir *tfsOpendir(STfs *pTfs, const char *rname); +int32_t tfsOpendir(STfs *pTfs, const char *rname, STfsDir **ppDir); /** * @brief Get a file from dir and move to next pos. diff --git a/source/dnode/mgmt/node_mgmt/src/dmEnv.c b/source/dnode/mgmt/node_mgmt/src/dmEnv.c index f636629b3a..fa73b608e5 100644 --- a/source/dnode/mgmt/node_mgmt/src/dmEnv.c +++ b/source/dnode/mgmt/node_mgmt/src/dmEnv.c @@ -110,6 +110,7 @@ static bool dmCheckDiskSpace() { } int32_t dmDiskInit() { + int32_t code = 0; SDnode *pDnode = dmInstance(); SDiskCfg dCfg = {.level = 0, .primary = 1, .disable = 0}; tstrncpy(dCfg.dir, tsDataDir, TSDB_FILENAME_LEN); @@ -120,10 +121,10 @@ int32_t dmDiskInit() { numOfDisks = 1; } - pDnode->pTfs = tfsOpen(pDisks, numOfDisks); + code = tfsOpen(pDisks, numOfDisks, &pDnode->pTfs); if (pDnode->pTfs == NULL) { - dError("failed to init tfs since %s", terrstr()); - return -1; + dError("failed to init tfs since %s", tstrerror(code)); + TAOS_RETURN(code); } return 0; } diff --git a/source/dnode/mnode/impl/src/mndUser.c b/source/dnode/mnode/impl/src/mndUser.c index a019533133..12c365ce46 100644 --- a/source/dnode/mnode/impl/src/mndUser.c +++ b/source/dnode/mnode/impl/src/mndUser.c @@ -648,7 +648,7 @@ SIpWhiteList *createIpWhiteList(void *buf, int32_t len) { static int32_t createDefaultIpWhiteList(SIpWhiteList **ppWhiteList) { *ppWhiteList = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range) * 1); if (*ppWhiteList == NULL) { - return TSDB_CODE_OUT_OF_MEMORY; + TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY); } (*ppWhiteList)->num = 1; SIpV4Range *range = &((*ppWhiteList)->pIpRange[0]); @@ -722,7 +722,7 @@ static int32_t mndCreateDefaultUsers(SMnode *pMnode) { } SSdbRaw *mndUserActionEncode(SUserObj *pUser) { - terrno = TSDB_CODE_OUT_OF_MEMORY; + int32_t code = TSDB_CODE_OUT_OF_MEMORY; int32_t ipWhiteReserve = pUser->pIpWhiteList ? (sizeof(SIpV4Range) * pUser->pIpWhiteList->num + sizeof(SIpWhiteList) + 4) : 16; @@ -835,8 +835,7 @@ SSdbRaw *mndUserActionEncode(SUserObj *pUser) { SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size); if (pRaw == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - goto _OVER; + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, NULL, _OVER); } int32_t dataPos = 0; @@ -981,8 +980,7 @@ SSdbRaw *mndUserActionEncode(SUserObj *pUser) { int32_t tlen = sizeof(SIpWhiteList) + num * sizeof(SIpV4Range) + 4; char *buf = taosMemoryCalloc(1, tlen); if (buf == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - goto _OVER; + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, NULL, _OVER); } int32_t len = tSerializeIpWhiteList(buf, tlen, pUser->pIpWhiteList); @@ -995,11 +993,11 @@ SSdbRaw *mndUserActionEncode(SUserObj *pUser) { SDB_SET_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER) SDB_SET_DATALEN(pRaw, dataPos, _OVER) - terrno = 0; + code = 0; _OVER: - if (terrno != 0) { - mError("user:%s, failed to encode to raw:%p since %s", pUser->user, pRaw, terrstr()); + if (code != 0) { + mError("user:%s, failed to encode to raw:%p since %s", pUser->user, pRaw, tstrerror(code)); sdbFreeRaw(pRaw); return NULL; } diff --git a/source/dnode/vnode/src/sma/smaCommit.c b/source/dnode/vnode/src/sma/smaCommit.c index 3512f1476f..e707f2150d 100644 --- a/source/dnode/vnode/src/sma/smaCommit.c +++ b/source/dnode/vnode/src/sma/smaCommit.c @@ -99,8 +99,7 @@ int32_t smaBegin(SSma *pSma) { } } _exit: - terrno = code; - return code; + TAOS_RETURN(code); } extern int32_t tsdbCommitCommit(STsdb *tsdb); @@ -119,7 +118,7 @@ _exit: if (code) { smaError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code)); } - return code; + TAOS_RETURN(code); } /** @@ -202,7 +201,7 @@ _exit: if (code) { smaError("vgId:%d, %s failed at line %d since %s(%d)", SMA_VID(pSma), __func__, lino, tstrerror(code), isCommit); } - return code; + TAOS_RETURN(code); } /** @@ -229,7 +228,7 @@ _exit: if (code) { smaError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(code)); } - return code; + TAOS_RETURN(code); } /** @@ -241,7 +240,7 @@ _exit: static int32_t tdProcessRSmaAsyncPostCommitImpl(SSma *pSma) { SSmaEnv *pEnv = SMA_RSMA_ENV(pSma); if (!pEnv) { - return TSDB_CODE_SUCCESS; + TAOS_RETURN(TSDB_CODE_SUCCESS); } SRSmaStat *pRSmaStat = (SRSmaStat *)SMA_ENV_STAT(pEnv); @@ -276,5 +275,5 @@ static int32_t tdProcessRSmaAsyncPostCommitImpl(SSma *pSma) { atomic_store_8(RSMA_COMMIT_STAT(pRSmaStat), 0); - return TSDB_CODE_SUCCESS; + TAOS_RETURN(TSDB_CODE_SUCCESS); } diff --git a/source/dnode/vnode/src/tsdb/tsdbFS2.c b/source/dnode/vnode/src/tsdb/tsdbFS2.c index 9b8a979d01..7742e55db7 100644 --- a/source/dnode/vnode/src/tsdb/tsdbFS2.c +++ b/source/dnode/vnode/src/tsdb/tsdbFS2.c @@ -523,12 +523,8 @@ static int32_t tsdbFSDoSanAndFix(STFileSystem *fs) { } { // clear unreferenced files - STfsDir *dir = tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path); - if (dir == NULL) { - code = TAOS_SYSTEM_ERROR(terrno); - lino = __LINE__; - goto _exit; - } + STfsDir *dir = NULL; + TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit); STFileHash fobjHash = {0}; code = tsdbFSCreateFileObjHash(fs, &fobjHash); diff --git a/source/dnode/vnode/src/vnd/vnodeOpen.c b/source/dnode/vnode/src/vnd/vnodeOpen.c index ea9209c6b4..a2b39e5284 100644 --- a/source/dnode/vnode/src/vnd/vnodeOpen.c +++ b/source/dnode/vnode/src/vnd/vnodeOpen.c @@ -175,7 +175,8 @@ int32_t vnodeRenameVgroupId(const char *srcPath, const char *dstPath, int32_t sr snprintf(tsdbFilePrefix, TSDB_FILENAME_LEN, "tsdb%sv", TD_DIRSEP); int32_t prefixLen = strlen(tsdbFilePrefix); - STfsDir *tsdbDir = tfsOpendir(pTfs, tsdbPath); + STfsDir *tsdbDir = NULL; + (void)tfsOpendir(pTfs, tsdbPath, &tsdbDir); if (tsdbDir == NULL) return 0; while (1) { diff --git a/source/dnode/vnode/test/tsdbSmaTest.cpp b/source/dnode/vnode/test/tsdbSmaTest.cpp index 8b19c0dc95..15e8d666c1 100644 --- a/source/dnode/vnode/test/tsdbSmaTest.cpp +++ b/source/dnode/vnode/test/tsdbSmaTest.cpp @@ -371,7 +371,7 @@ TEST(testCase, tSma_Data_Insert_Query_Test) { pDisks.disable = 0; strncpy(pDisks.dir, TD_DATA_DIR_PATH, TSDB_FILENAME_LEN); int32_t numOfDisks = 1; - pTsdb->pTfs = tfsOpen(&pDisks, numOfDisks); + (void)tfsOpen(&pDisks, numOfDisks, &pTsdb->pTfs); EXPECT_NE(pTsdb->pTfs, nullptr); // generate SSubmitReq msg and update expire window diff --git a/source/libs/tfs/src/tfs.c b/source/libs/tfs/src/tfs.c index 3432b2a069..07c1fab54f 100644 --- a/source/libs/tfs/src/tfs.c +++ b/source/libs/tfs/src/tfs.c @@ -21,61 +21,56 @@ static int32_t tfsMount(STfs *pTfs, SDiskCfg *pCfg); static int32_t tfsCheck(STfs *pTfs); static int32_t tfsCheckAndFormatCfg(STfs *pTfs, SDiskCfg *pCfg); static int32_t tfsFormatDir(char *idir, char *odir); -static STfsDisk *tfsGetDiskByName(STfs *pTfs, const char *dir); +static int32_t tfsGetDiskByName(STfs *pTfs, const char *dir, STfsDisk **ppDisk); static int32_t tfsOpendirImpl(STfs *pTfs, STfsDir *pDir); static STfsDisk *tfsNextDisk(STfs *pTfs, SDiskIter *pIter); -STfs *tfsOpen(SDiskCfg *pCfg, int32_t ndisk) { +int32_t tfsOpen(SDiskCfg *pCfg, int32_t ndisk, STfs **ppTfs) { + int32_t code = 0; + int32_t lino = 0; + STfs *pTfs = NULL; + if (ndisk <= 0 || ndisk > TFS_MAX_DISKS) { - terrno = TSDB_CODE_INVALID_PARA; - return NULL; + TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, &lino, _exit); } - STfs *pTfs = taosMemoryCalloc(1, sizeof(STfs)); + pTfs = taosMemoryCalloc(1, sizeof(STfs)); if (pTfs == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit); } if (taosThreadSpinInit(&pTfs->lock, 0) != 0) { - terrno = TAOS_SYSTEM_ERROR(errno); - tfsClose(pTfs); - return NULL; + TAOS_CHECK_GOTO(TAOS_SYSTEM_ERROR(errno), &lino, _exit); } for (int32_t level = 0; level < TFS_MAX_TIERS; level++) { STfsTier *pTier = &pTfs->tiers[level]; - if (tfsInitTier(pTier, level) < 0) { - tfsClose(pTfs); - return NULL; - } + TAOS_CHECK_GOTO(tfsInitTier(pTier, level), &lino, _exit); } pTfs->hash = taosHashInit(TFS_MAX_DISKS * 2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK); if (pTfs->hash == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - tfsClose(pTfs); - return NULL; + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit); } for (int32_t idisk = 0; idisk < ndisk; idisk++) { - if (tfsMount(pTfs, &pCfg[idisk]) < 0) { - tfsClose(pTfs); - return NULL; - } + TAOS_CHECK_GOTO(tfsMount(pTfs, &pCfg[idisk]), &lino, _exit); } - if (tfsCheck(pTfs) < 0) { - tfsClose(pTfs); - return NULL; - } + TAOS_CHECK_GOTO(tfsCheck(pTfs) < 0, &lino, _exit); tfsUpdateSize(pTfs); for (int32_t level = 0; level < pTfs->nlevel; level++) { tfsPosNextId(&pTfs->tiers[level]); } - return pTfs; +_exit: + if (code != 0) { + tfsClose(pTfs); + pTfs = NULL; + } + *ppTfs = pTfs; + TAOS_RETURN(code); } void tfsClose(STfs *pTfs) { @@ -149,7 +144,7 @@ bool tfsDiskSpaceSufficient(STfs *pTfs, int32_t level, int32_t disk) { int32_t tfsGetDisksAtLevel(STfs *pTfs, int32_t level) { if (level < 0 || level >= pTfs->nlevel) { - return 0; + TAOS_RETURN(0); } STfsTier *pTier = TFS_TIER_AT(pTfs, level); @@ -177,10 +172,10 @@ int32_t tfsAllocDisk(STfs *pTfs, int32_t expLevel, SDiskID *pDiskId) { continue; } - return (terrno = 0); + TAOS_RETURN(0); } - return (terrno = TSDB_CODE_FS_NO_VALID_DISK); + TAOS_RETURN(TSDB_CODE_FS_NO_VALID_DISK); } const char *tfsGetPrimaryPath(STfs *pTfs) { return TFS_PRIMARY_DISK(pTfs)->path; } @@ -270,22 +265,27 @@ int32_t tfsMkdirAt(STfs *pTfs, const char *rname, SDiskID diskId) { char aname[TMPNAME_LEN]; if (pDisk == NULL) { - return -1; + TAOS_RETURN(TSDB_CODE_FS_INVLD_CFG); } snprintf(aname, TMPNAME_LEN, "%s%s%s", pDisk->path, TD_DIRSEP, rname); if (taosMkDir(aname) != 0) { - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; + TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); } - return 0; + TAOS_RETURN(0); } int32_t tfsMkdirRecurAt(STfs *pTfs, const char *rname, SDiskID diskId) { - if (tfsMkdirAt(pTfs, rname, diskId) < 0) { + int32_t code = 0; + int32_t lino = 0; + char *s = NULL; + char *dir = NULL; + if ((code = tfsMkdirAt(pTfs, rname, diskId)) < 0) { if (errno == ENOENT) { // Try to create upper - char *s = taosStrdup(rname); + if ((s = taosStrdup(rname)) == NULL) { + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit); + } // Make a copy of dirname(s) because the implementation of 'dirname' differs on different platforms. // Some platform may modify the contents of the string passed into dirname(). Others may return a pointer to @@ -293,25 +293,30 @@ int32_t tfsMkdirRecurAt(STfs *pTfs, const char *rname, SDiskID diskId) { // the pointer directly in this recursion. // See // https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dirname.3.html - char *dir = taosStrdup(taosDirName(s)); - if (strlen(dir) >= strlen(rname) || tfsMkdirRecurAt(pTfs, dir, diskId) < 0) { - taosMemoryFree(s); - taosMemoryFree(dir); - return -1; + if ((dir = taosStrdup(taosDirName(s))) == NULL) { + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit); } - taosMemoryFree(s); - taosMemoryFree(dir); - if (tfsMkdirAt(pTfs, rname, diskId) < 0) { - return -1; + if (strlen(dir) >= strlen(rname)) { // TODO: check if it is necessary for equal length + TAOS_CHECK_GOTO(TSDB_CODE_APP_ERROR, &lino, _exit); } + TAOS_CHECK_GOTO(tfsMkdirRecurAt(pTfs, dir, diskId), &lino, _exit); + TAOS_CHECK_GOTO(tfsMkdirAt(pTfs, rname, diskId), &lino, _exit); } else { - return -1; + TAOS_CHECK_GOTO(code, &lino, _exit); } + } else { + TAOS_RETURN(code); } - return 0; +_exit: + if (code != 0) { + fError("%s failed at line %d since %s", __func__, lino, tstrerror(code)); + } + taosMemoryFree(s); + taosMemoryFree(dir); + TAOS_RETURN(code); } int32_t tfsMkdirRecur(STfs *pTfs, const char *rname) { @@ -319,13 +324,11 @@ int32_t tfsMkdirRecur(STfs *pTfs, const char *rname) { STfsTier *pTier = TFS_TIER_AT(pTfs, level); for (int32_t id = 0; id < pTier->ndisk; id++) { SDiskID did = {.id = id, .level = level}; - if (tfsMkdirRecurAt(pTfs, rname, did) < 0) { - return -1; - } + TAOS_CHECK_RETURN(tfsMkdirRecurAt(pTfs, rname, did)); } } - return 0; + TAOS_RETURN(0); } int32_t tfsMkdir(STfs *pTfs, const char *rname) { @@ -333,13 +336,11 @@ int32_t tfsMkdir(STfs *pTfs, const char *rname) { STfsTier *pTier = TFS_TIER_AT(pTfs, level); for (int32_t id = 0; id < pTier->ndisk; id++) { SDiskID did = {.id = id, .level = level}; - if (tfsMkdirAt(pTfs, rname, did) < 0) { - return -1; - } + TAOS_CHECK_RETURN(tfsMkdirAt(pTfs, rname, did)); } } - return 0; + TAOS_RETURN(0); } bool tfsDirExistAt(STfs *pTfs, const char *rname, SDiskID diskId) { @@ -352,7 +353,7 @@ bool tfsDirExistAt(STfs *pTfs, const char *rname, SDiskID diskId) { int32_t tfsRmdir(STfs *pTfs, const char *rname) { if (rname[0] == 0) { - return 0; + TAOS_RETURN(0); } char aname[TMPNAME_LEN] = "\0"; @@ -367,7 +368,7 @@ int32_t tfsRmdir(STfs *pTfs, const char *rname) { } } - return 0; + TAOS_RETURN(0); } static int32_t tfsRenameAt(STfs *pTfs, SDiskID diskId, const char *orname, const char *nrname) { @@ -382,12 +383,12 @@ static int32_t tfsRenameAt(STfs *pTfs, SDiskID diskId, const char *orname, const snprintf(naname, TMPNAME_LEN, "%s%s%s", pDisk->path, TD_DIRSEP, nrname); if (taosRenameFile(oaname, naname) != 0 && errno != ENOENT) { - terrno = TAOS_SYSTEM_ERROR(errno); - fError("failed to rename %s to %s since %s", oaname, naname, terrstr()); - return -1; + int32_t code = TAOS_SYSTEM_ERROR(errno); + fError("%s failed to rename %s to %s since %s", __func__, oaname, naname, tstrerror(code)); + TAOS_RETURN(code); } - return 0; + TAOS_RETURN(0); } int32_t tfsRename(STfs *pTfs, int32_t diskPrimary, const char *orname, const char *nrname) { @@ -399,14 +400,12 @@ int32_t tfsRename(STfs *pTfs, int32_t diskPrimary, const char *orname, const cha } SDiskID diskId = {.level = level, .id = id}; - if (tfsRenameAt(pTfs, diskId, orname, nrname)) { - return -1; - } + TAOS_CHECK_RETURN(tfsRenameAt(pTfs, diskId, orname, nrname)); } } SDiskID diskId = {.level = 0, .id = diskPrimary}; - return tfsRenameAt(pTfs, diskId, orname, nrname); + TAOS_RETURN(tfsRenameAt(pTfs, diskId, orname, nrname)); } int32_t tfsSearch(STfs *pTfs, int32_t level, const char *fname) { @@ -426,11 +425,11 @@ int32_t tfsSearch(STfs *pTfs, int32_t level, const char *fname) { return -1; } -STfsDir *tfsOpendir(STfs *pTfs, const char *rname) { +int32_t tfsOpendir(STfs *pTfs, const char *rname, STfsDir **ppDir) { + int32_t code = 0; STfsDir *pDir = taosMemoryCalloc(1, sizeof(STfsDir)); if (pDir == NULL) { - terrno = TSDB_CODE_OUT_OF_MEMORY; - return NULL; + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, NULL, _exit); } SDiskID diskId = {.id = 0, .level = 0}; @@ -438,12 +437,11 @@ STfsDir *tfsOpendir(STfs *pTfs, const char *rname) { pDir->pTfs = pTfs; tstrncpy(pDir->dirName, rname, TSDB_FILENAME_LEN); - if (tfsOpendirImpl(pTfs, pDir) < 0) { - taosMemoryFree(pDir); - return NULL; - } + TAOS_CHECK_GOTO(tfsOpendirImpl(pTfs, pDir), NULL, _exit); - return pDir; +_exit: + taosMemoryFree(pDir); + TAOS_RETURN(code); } const STfsFile *tfsReaddir(STfsDir *pTfsDir) { @@ -490,30 +488,35 @@ void tfsClosedir(STfsDir *pTfsDir) { } static int32_t tfsMount(STfs *pTfs, SDiskCfg *pCfg) { - if (tfsCheckAndFormatCfg(pTfs, pCfg) < 0) { - return -1; - } + int32_t code = 0; + int32_t lino = 0; + + TAOS_CHECK_GOTO(tfsCheckAndFormatCfg(pTfs, pCfg), &lino, _exit); SDiskID did = {.level = pCfg->level}; STfsDisk *pDisk = NULL; - - tfsMountDiskToTier(TFS_TIER_AT(pTfs, did.level), pCfg, &pDisk); - if (pDisk == NULL) { - fError("failed to mount disk %s to level %d since %s", pCfg->dir, pCfg->level, terrstr()); - return -1; - } + + TAOS_CHECK_GOTO(tfsMountDiskToTier(TFS_TIER_AT(pTfs, did.level), pCfg, &pDisk), &lino, _exit); did.id = pDisk->id; - taosHashPut(pTfs->hash, (void *)(pCfg->dir), strnlen(pCfg->dir, TSDB_FILENAME_LEN), (void *)(&did), sizeof(did)); + if (taosHashPut(pTfs->hash, (void *)(pCfg->dir), strnlen(pCfg->dir, TSDB_FILENAME_LEN), (void *)(&did), + sizeof(did)) != 0) { + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit); + } if (pTfs->nlevel < pCfg->level + 1) { pTfs->nlevel = pCfg->level + 1; } - return 0; +_exit: + if (code != 0) { + fError("%s failed at line %d since %s", __func__, lino, tstrerror(code)); + } + TAOS_RETURN(code); } static int32_t tfsCheckAndFormatCfg(STfs *pTfs, SDiskCfg *pCfg) { - char dirName[TSDB_FILENAME_LEN] = "\0"; + int32_t code = 0; + char dirName[TSDB_FILENAME_LEN] = "\0"; if (pCfg->level < 0 || pCfg->level >= TFS_MAX_TIERS) { fError("failed to mount %s to FS since invalid level %d", pCfg->dir, pCfg->level); @@ -542,32 +545,34 @@ static int32_t tfsCheckAndFormatCfg(STfs *pTfs, SDiskCfg *pCfg) { } } - if (tfsFormatDir(pCfg->dir, dirName) < 0) { - fError("failed to mount %s to FS since %s", pCfg->dir, terrstr()); - return -1; + if ((code = tfsFormatDir(pCfg->dir, dirName)) < 0) { + fError("failed to mount %s to FS since %s", pCfg->dir, tstrerror(code)); + TAOS_RETURN(code); } - if (tfsGetDiskByName(pTfs, dirName) != NULL) { + STfsDisk *pDisk = NULL; + if ((code = tfsGetDiskByName(pTfs, dirName, NULL)) != 0) { + fError("failed to mount %s to FS since %s", pCfg->dir, tstrerror(code)); + TAOS_RETURN(code); + } + if (pDisk != NULL) { fError("failed to mount %s to FS since duplicate mount", pCfg->dir); - terrno = TSDB_CODE_FS_INVLD_CFG; - return -1; + TAOS_RETURN(TSDB_CODE_FS_INVLD_CFG); } if (!taosCheckAccessFile(dirName, TD_FILE_ACCESS_EXIST_OK | TD_FILE_ACCESS_READ_OK | TD_FILE_ACCESS_WRITE_OK)) { fError("failed to mount %s to FS since no R/W access rights", pCfg->dir); - terrno = TSDB_CODE_FS_INVLD_CFG; - return -1; + TAOS_RETURN(TSDB_CODE_FS_INVLD_CFG); } if (!taosIsDir(dirName)) { fError("failed to mount %s to FS since not a directory", pCfg->dir); - terrno = TSDB_CODE_FS_INVLD_CFG; - return -1; + TAOS_RETURN(TSDB_CODE_FS_INVLD_CFG); } strncpy(pCfg->dir, dirName, TSDB_FILENAME_LEN); - return 0; + TAOS_RETURN(0); } static int32_t tfsFormatDir(char *idir, char *odir) { @@ -587,7 +592,7 @@ static int32_t tfsFormatDir(char *idir, char *odir) { strcpy(odir, tmp); wordfree(&wep); - return 0; + TAOS_RETURN(0); } static int32_t tfsCheck(STfs *pTfs) { @@ -611,17 +616,19 @@ static int32_t tfsCheck(STfs *pTfs) { } } - return 0; + TAOS_RETURN(0); } -static STfsDisk *tfsGetDiskByName(STfs *pTfs, const char *dir) { +static int32_t tfsGetDiskByName(STfs *pTfs, const char *dir, STfsDisk **ppDisk) { void *pr = taosHashGet(pTfs->hash, (void *)dir, strnlen(dir, TSDB_FILENAME_LEN)); - if (pr == NULL) return NULL; + if (pr == NULL) { + TAOS_RETURN(terrno); + } - SDiskID did = *(SDiskID *)pr; - STfsDisk *pDisk = TFS_DISK_AT(pTfs, did); + SDiskID did = *(SDiskID *)pr; + *ppDisk = TFS_DISK_AT(pTfs, did); - return pDisk; + TAOS_RETURN(0); } static int32_t tfsOpendirImpl(STfs *pTfs, STfsDir *pTfsDir) { @@ -635,7 +642,7 @@ static int32_t tfsOpendirImpl(STfs *pTfs, STfsDir *pTfsDir) { while (true) { pDisk = tfsNextDisk(pTfs, &pTfsDir->iter); - if (pDisk == NULL) return 0; + if (pDisk == NULL) TAOS_RETURN(0); pTfsDir->did.level = pDisk->level; pTfsDir->did.id = pDisk->id; @@ -647,9 +654,10 @@ static int32_t tfsOpendirImpl(STfs *pTfs, STfsDir *pTfsDir) { } pTfsDir->pDir = taosOpenDir(adir); if (pTfsDir->pDir != NULL) break; + fWarn("%s failed to open dir %s since %s", __func__, adir, tstrerror(TAOS_SYSTEM_ERROR(errno))); } - return 0; + TAOS_RETURN(0); } static STfsDisk *tfsNextDisk(STfs *pTfs, SDiskIter *pIter) { @@ -702,5 +710,5 @@ int32_t tfsGetMonitorInfo(STfs *pTfs, SMonDiskInfo *pInfo) { } tfsUnLock(pTfs); - return 0; + TAOS_RETURN(0); } diff --git a/source/libs/tfs/src/tfsDisk.c b/source/libs/tfs/src/tfsDisk.c index 424eb886ba..07fa8d79b9 100644 --- a/source/libs/tfs/src/tfsDisk.c +++ b/source/libs/tfs/src/tfsDisk.c @@ -22,11 +22,11 @@ int32_t tfsNewDisk(int32_t level, int32_t id, int8_t disable, const char *path, STfsDisk *pDisk = NULL; if ((pDisk = taosMemoryCalloc(1, sizeof(STfsDisk))) == NULL) { - TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER); + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit); } if ((pDisk->path = taosStrdup(path)) == NULL) { - TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _OVER); + TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit); } pDisk->level = level; @@ -34,9 +34,9 @@ int32_t tfsNewDisk(int32_t level, int32_t id, int8_t disable, const char *path, pDisk->disable = disable; if (taosGetDiskSize(pDisk->path, &pDisk->size) < 0) { code = TAOS_SYSTEM_ERROR(errno); // TODO: refactor this line - TAOS_CHECK_GOTO(code, &lino, _OVER); + TAOS_CHECK_GOTO(code, &lino, _exit); } -_OVER: +_exit: if (code != 0) { pDisk = tfsFreeDisk(pDisk); fError("%s failed at line %d since %s, disk:%s level:%d id:%d ", __func__, lino, tstrerror(code), path, level, id); diff --git a/source/libs/tfs/src/tfsTier.c b/source/libs/tfs/src/tfsTier.c index a0fece3691..685f95c517 100644 --- a/source/libs/tfs/src/tfsTier.c +++ b/source/libs/tfs/src/tfsTier.c @@ -44,7 +44,7 @@ int32_t tfsMountDiskToTier(STfsTier *pTier, SDiskCfg *pCfg, STfsDisk **ppDisk) { STfsDisk *pDisk = NULL; if (pTier->ndisk >= TFS_MAX_DISKS_PER_TIER) { - TAOS_CHECK_GOTO(TSDB_CODE_FS_TOO_MANY_MOUNT, &lino, _OVER); + TAOS_CHECK_GOTO(TSDB_CODE_FS_TOO_MANY_MOUNT, &lino, _exit); } int32_t id = 0; @@ -63,15 +63,15 @@ int32_t tfsMountDiskToTier(STfsTier *pTier, SDiskCfg *pCfg, STfsDisk **ppDisk) { } if (id >= TFS_MAX_DISKS_PER_TIER) { - TAOS_CHECK_GOTO(TSDB_CODE_FS_TOO_MANY_MOUNT, &lino, _OVER); + TAOS_CHECK_GOTO(TSDB_CODE_FS_TOO_MANY_MOUNT, &lino, _exit); } - TAOS_CHECK_GOTO(tfsNewDisk(pCfg->level, id, pCfg->disable, pCfg->dir, &pDisk), &lino, _OVER); + TAOS_CHECK_GOTO(tfsNewDisk(pCfg->level, id, pCfg->disable, pCfg->dir, &pDisk), &lino, _exit); pTier->disks[id] = pDisk; pTier->ndisk++; -_OVER: +_exit: if (code != 0) { pDisk = tfsFreeDisk(pDisk); fError("%s failed at line %d since %s, disk:%s level:%d id:%d", __func__, lino, tstrerror(code), pCfg->dir, diff --git a/source/libs/tfs/test/tfsTest.cpp b/source/libs/tfs/test/tfsTest.cpp index d8c117818e..bb89fbe69f 100644 --- a/source/libs/tfs/test/tfsTest.cpp +++ b/source/libs/tfs/test/tfsTest.cpp @@ -40,11 +40,12 @@ TEST_F(TfsTest, 01_Open_Close) { dCfg.disable = 0; taosRemoveDir(root); - STfs *pTfs = tfsOpen(&dCfg, 1); + STfs *pTfs = NULL; + (void)tfsOpen(&dCfg, 1, &pTfs); ASSERT_EQ(pTfs, nullptr); taosMulMkDir(root); - pTfs = tfsOpen(&dCfg, 1); + (void)tfsOpen(&dCfg, 1, &pTfs); ASSERT_NE(pTfs, nullptr); tfsUpdateSize(pTfs); @@ -68,7 +69,8 @@ TEST_F(TfsTest, 02_AllocDisk) { taosRemoveDir(root); taosMkDir(root); - STfs *pTfs = tfsOpen(&dCfg, 1); + STfs *pTfs = NULL; + (void)tfsOpen(&dCfg, 1, &pTfs); ASSERT_NE(pTfs, nullptr); SDiskID did; @@ -120,7 +122,8 @@ TEST_F(TfsTest, 03_Dir) { taosRemoveDir(root); taosMkDir(root); - STfs *pTfs = tfsOpen(&dCfg, 1); + STfs *pTfs = NULL; + (void)tfsOpen(&dCfg, 1, &pTfs); ASSERT_NE(pTfs, nullptr); char p1[] = "p1"; @@ -175,7 +178,8 @@ TEST_F(TfsTest, 04_File) { taosRemoveDir(root); taosMkDir(root); - STfs *pTfs = tfsOpen(&dCfg, 1); + STfs *pTfs = NULL; + (void)tfsOpen(&dCfg, 1, &pTfs); ASSERT_NE(pTfs, nullptr); STfsFile file0; @@ -264,7 +268,8 @@ TEST_F(TfsTest, 04_File) { EXPECT_NE(taosDirExist(af2), 1); { - STfsDir *pDir = tfsOpendir(pTfs, "t3"); + STfsDir *pDir = NULL; + tfsOpendir(pTfs, "t3", &pDir); const STfsFile *pf1 = tfsReaddir(pDir); EXPECT_NE(pf1, nullptr); @@ -281,7 +286,8 @@ TEST_F(TfsTest, 04_File) { EXPECT_GT(tfsCopyFile(&f1, &f2), 0); { - STfsDir *pDir = tfsOpendir(pTfs, "t3"); + STfsDir *pDir = NULL; + tfsOpendir(pTfs, "t3", &pDir); const STfsFile *pf1 = tfsReaddir(pDir); EXPECT_NE(pf1, nullptr); @@ -386,17 +392,18 @@ TEST_F(TfsTest, 05_MultiDisk) { taosMkDir(root22); taosMkDir(root23); - STfs *pTfs = tfsOpen(dCfg, 9); + STfs *pTfs = NULL; + (void)tfsOpen(dCfg, 9, &pTfs); ASSERT_EQ(pTfs, nullptr); dCfg[0].primary = 1; dCfg[1].primary = 1; - pTfs = tfsOpen(dCfg, 9); + (void)tfsOpen(dCfg, 9, &pTfs); ASSERT_EQ(pTfs, nullptr); dCfg[0].primary = 0; dCfg[1].primary = 1; - pTfs = tfsOpen(dCfg, 9); + (void)tfsOpen(dCfg, 9, &pTfs); ASSERT_NE(pTfs, nullptr); tfsUpdateSize(pTfs); @@ -693,7 +700,8 @@ TEST_F(TfsTest, 05_MultiDisk) { tfsRemoveFile(&f2); { - STfsDir *pDir = tfsOpendir(pTfs, "t3"); + STfsDir *pDir = NULL; + tfsOpendir(pTfs, "t3", &pDir); const STfsFile *pf1 = tfsReaddir(pDir); EXPECT_NE(pf1, nullptr); @@ -711,7 +719,8 @@ TEST_F(TfsTest, 05_MultiDisk) { EXPECT_GT(tfsCopyFile(&f1, &f2), 0); { - STfsDir *pDir = tfsOpendir(pTfs, "t3"); + STfsDir *pDir = NULL; + tfsOpendir(pTfs, "t3", &pDir); const STfsFile *pf1 = tfsReaddir(pDir); EXPECT_NE(pf1, nullptr);