enh: return error code

This commit is contained in:
kailixu 2024-07-21 11:43:39 +08:00
parent b2c60945d7
commit cd8a54cbf7
11 changed files with 167 additions and 155 deletions

View File

@ -44,9 +44,9 @@ typedef struct {
* *
* @param pCfg Config of the fs. * @param pCfg Config of the fs.
* @param ndisk Length of the config. * @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. * @brief Close a fs.
@ -275,7 +275,7 @@ int32_t tfsCopyFile(const STfsFile *pFile1, const STfsFile *pFile2);
* @param rname The rel name of file. * @param rname The rel name of file.
* @return STfsDir* The dir object. * @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. * @brief Get a file from dir and move to next pos.

View File

@ -110,6 +110,7 @@ static bool dmCheckDiskSpace() {
} }
int32_t dmDiskInit() { int32_t dmDiskInit() {
int32_t code = 0;
SDnode *pDnode = dmInstance(); SDnode *pDnode = dmInstance();
SDiskCfg dCfg = {.level = 0, .primary = 1, .disable = 0}; SDiskCfg dCfg = {.level = 0, .primary = 1, .disable = 0};
tstrncpy(dCfg.dir, tsDataDir, TSDB_FILENAME_LEN); tstrncpy(dCfg.dir, tsDataDir, TSDB_FILENAME_LEN);
@ -120,10 +121,10 @@ int32_t dmDiskInit() {
numOfDisks = 1; numOfDisks = 1;
} }
pDnode->pTfs = tfsOpen(pDisks, numOfDisks); code = tfsOpen(pDisks, numOfDisks, &pDnode->pTfs);
if (pDnode->pTfs == NULL) { if (pDnode->pTfs == NULL) {
dError("failed to init tfs since %s", terrstr()); dError("failed to init tfs since %s", tstrerror(code));
return -1; TAOS_RETURN(code);
} }
return 0; return 0;
} }

View File

@ -648,7 +648,7 @@ SIpWhiteList *createIpWhiteList(void *buf, int32_t len) {
static int32_t createDefaultIpWhiteList(SIpWhiteList **ppWhiteList) { static int32_t createDefaultIpWhiteList(SIpWhiteList **ppWhiteList) {
*ppWhiteList = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range) * 1); *ppWhiteList = taosMemoryCalloc(1, sizeof(SIpWhiteList) + sizeof(SIpV4Range) * 1);
if (*ppWhiteList == NULL) { if (*ppWhiteList == NULL) {
return TSDB_CODE_OUT_OF_MEMORY; TAOS_RETURN(TSDB_CODE_OUT_OF_MEMORY);
} }
(*ppWhiteList)->num = 1; (*ppWhiteList)->num = 1;
SIpV4Range *range = &((*ppWhiteList)->pIpRange[0]); SIpV4Range *range = &((*ppWhiteList)->pIpRange[0]);
@ -722,7 +722,7 @@ static int32_t mndCreateDefaultUsers(SMnode *pMnode) {
} }
SSdbRaw *mndUserActionEncode(SUserObj *pUser) { SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
terrno = TSDB_CODE_OUT_OF_MEMORY; int32_t code = TSDB_CODE_OUT_OF_MEMORY;
int32_t ipWhiteReserve = int32_t ipWhiteReserve =
pUser->pIpWhiteList ? (sizeof(SIpV4Range) * pUser->pIpWhiteList->num + sizeof(SIpWhiteList) + 4) : 16; 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); SSdbRaw *pRaw = sdbAllocRaw(SDB_USER, USER_VER_NUMBER, size);
if (pRaw == NULL) { if (pRaw == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, NULL, _OVER);
goto _OVER;
} }
int32_t dataPos = 0; int32_t dataPos = 0;
@ -981,8 +980,7 @@ SSdbRaw *mndUserActionEncode(SUserObj *pUser) {
int32_t tlen = sizeof(SIpWhiteList) + num * sizeof(SIpV4Range) + 4; int32_t tlen = sizeof(SIpWhiteList) + num * sizeof(SIpV4Range) + 4;
char *buf = taosMemoryCalloc(1, tlen); char *buf = taosMemoryCalloc(1, tlen);
if (buf == NULL) { if (buf == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, NULL, _OVER);
goto _OVER;
} }
int32_t len = tSerializeIpWhiteList(buf, tlen, pUser->pIpWhiteList); 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_RESERVE(pRaw, dataPos, USER_RESERVE_SIZE, _OVER)
SDB_SET_DATALEN(pRaw, dataPos, _OVER) SDB_SET_DATALEN(pRaw, dataPos, _OVER)
terrno = 0; code = 0;
_OVER: _OVER:
if (terrno != 0) { if (code != 0) {
mError("user:%s, failed to encode to raw:%p since %s", pUser->user, pRaw, terrstr()); mError("user:%s, failed to encode to raw:%p since %s", pUser->user, pRaw, tstrerror(code));
sdbFreeRaw(pRaw); sdbFreeRaw(pRaw);
return NULL; return NULL;
} }

View File

@ -99,8 +99,7 @@ int32_t smaBegin(SSma *pSma) {
} }
} }
_exit: _exit:
terrno = code; TAOS_RETURN(code);
return code;
} }
extern int32_t tsdbCommitCommit(STsdb *tsdb); extern int32_t tsdbCommitCommit(STsdb *tsdb);
@ -119,7 +118,7 @@ _exit:
if (code) { if (code) {
smaError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(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) { if (code) {
smaError("vgId:%d, %s failed at line %d since %s(%d)", SMA_VID(pSma), __func__, lino, tstrerror(code), isCommit); 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) { if (code) {
smaError("vgId:%d, %s failed at line %d since %s", TD_VID(pVnode), __func__, lino, tstrerror(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) { static int32_t tdProcessRSmaAsyncPostCommitImpl(SSma *pSma) {
SSmaEnv *pEnv = SMA_RSMA_ENV(pSma); SSmaEnv *pEnv = SMA_RSMA_ENV(pSma);
if (!pEnv) { if (!pEnv) {
return TSDB_CODE_SUCCESS; TAOS_RETURN(TSDB_CODE_SUCCESS);
} }
SRSmaStat *pRSmaStat = (SRSmaStat *)SMA_ENV_STAT(pEnv); 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); atomic_store_8(RSMA_COMMIT_STAT(pRSmaStat), 0);
return TSDB_CODE_SUCCESS; TAOS_RETURN(TSDB_CODE_SUCCESS);
} }

View File

@ -523,12 +523,8 @@ static int32_t tsdbFSDoSanAndFix(STFileSystem *fs) {
} }
{ // clear unreferenced files { // clear unreferenced files
STfsDir *dir = tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path); STfsDir *dir = NULL;
if (dir == NULL) { TAOS_CHECK_GOTO(tfsOpendir(fs->tsdb->pVnode->pTfs, fs->tsdb->path, &dir), &lino, _exit);
code = TAOS_SYSTEM_ERROR(terrno);
lino = __LINE__;
goto _exit;
}
STFileHash fobjHash = {0}; STFileHash fobjHash = {0};
code = tsdbFSCreateFileObjHash(fs, &fobjHash); code = tsdbFSCreateFileObjHash(fs, &fobjHash);

View File

@ -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); snprintf(tsdbFilePrefix, TSDB_FILENAME_LEN, "tsdb%sv", TD_DIRSEP);
int32_t prefixLen = strlen(tsdbFilePrefix); int32_t prefixLen = strlen(tsdbFilePrefix);
STfsDir *tsdbDir = tfsOpendir(pTfs, tsdbPath); STfsDir *tsdbDir = NULL;
(void)tfsOpendir(pTfs, tsdbPath, &tsdbDir);
if (tsdbDir == NULL) return 0; if (tsdbDir == NULL) return 0;
while (1) { while (1) {

View File

@ -371,7 +371,7 @@ TEST(testCase, tSma_Data_Insert_Query_Test) {
pDisks.disable = 0; pDisks.disable = 0;
strncpy(pDisks.dir, TD_DATA_DIR_PATH, TSDB_FILENAME_LEN); strncpy(pDisks.dir, TD_DATA_DIR_PATH, TSDB_FILENAME_LEN);
int32_t numOfDisks = 1; int32_t numOfDisks = 1;
pTsdb->pTfs = tfsOpen(&pDisks, numOfDisks); (void)tfsOpen(&pDisks, numOfDisks, &pTsdb->pTfs);
EXPECT_NE(pTsdb->pTfs, nullptr); EXPECT_NE(pTsdb->pTfs, nullptr);
// generate SSubmitReq msg and update expire window // generate SSubmitReq msg and update expire window

View File

@ -21,61 +21,56 @@ static int32_t tfsMount(STfs *pTfs, SDiskCfg *pCfg);
static int32_t tfsCheck(STfs *pTfs); static int32_t tfsCheck(STfs *pTfs);
static int32_t tfsCheckAndFormatCfg(STfs *pTfs, SDiskCfg *pCfg); static int32_t tfsCheckAndFormatCfg(STfs *pTfs, SDiskCfg *pCfg);
static int32_t tfsFormatDir(char *idir, char *odir); 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 int32_t tfsOpendirImpl(STfs *pTfs, STfsDir *pDir);
static STfsDisk *tfsNextDisk(STfs *pTfs, SDiskIter *pIter); 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) { if (ndisk <= 0 || ndisk > TFS_MAX_DISKS) {
terrno = TSDB_CODE_INVALID_PARA; TAOS_CHECK_GOTO(TSDB_CODE_INVALID_PARA, &lino, _exit);
return NULL;
} }
STfs *pTfs = taosMemoryCalloc(1, sizeof(STfs)); pTfs = taosMemoryCalloc(1, sizeof(STfs));
if (pTfs == NULL) { if (pTfs == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
return NULL;
} }
if (taosThreadSpinInit(&pTfs->lock, 0) != 0) { if (taosThreadSpinInit(&pTfs->lock, 0) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno); TAOS_CHECK_GOTO(TAOS_SYSTEM_ERROR(errno), &lino, _exit);
tfsClose(pTfs);
return NULL;
} }
for (int32_t level = 0; level < TFS_MAX_TIERS; level++) { for (int32_t level = 0; level < TFS_MAX_TIERS; level++) {
STfsTier *pTier = &pTfs->tiers[level]; STfsTier *pTier = &pTfs->tiers[level];
if (tfsInitTier(pTier, level) < 0) { TAOS_CHECK_GOTO(tfsInitTier(pTier, level), &lino, _exit);
tfsClose(pTfs);
return NULL;
}
} }
pTfs->hash = taosHashInit(TFS_MAX_DISKS * 2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK); pTfs->hash = taosHashInit(TFS_MAX_DISKS * 2, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK);
if (pTfs->hash == NULL) { if (pTfs->hash == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
tfsClose(pTfs);
return NULL;
} }
for (int32_t idisk = 0; idisk < ndisk; idisk++) { for (int32_t idisk = 0; idisk < ndisk; idisk++) {
if (tfsMount(pTfs, &pCfg[idisk]) < 0) { TAOS_CHECK_GOTO(tfsMount(pTfs, &pCfg[idisk]), &lino, _exit);
tfsClose(pTfs);
return NULL;
}
} }
if (tfsCheck(pTfs) < 0) { TAOS_CHECK_GOTO(tfsCheck(pTfs) < 0, &lino, _exit);
tfsClose(pTfs);
return NULL;
}
tfsUpdateSize(pTfs); tfsUpdateSize(pTfs);
for (int32_t level = 0; level < pTfs->nlevel; level++) { for (int32_t level = 0; level < pTfs->nlevel; level++) {
tfsPosNextId(&pTfs->tiers[level]); tfsPosNextId(&pTfs->tiers[level]);
} }
return pTfs; _exit:
if (code != 0) {
tfsClose(pTfs);
pTfs = NULL;
}
*ppTfs = pTfs;
TAOS_RETURN(code);
} }
void tfsClose(STfs *pTfs) { 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) { int32_t tfsGetDisksAtLevel(STfs *pTfs, int32_t level) {
if (level < 0 || level >= pTfs->nlevel) { if (level < 0 || level >= pTfs->nlevel) {
return 0; TAOS_RETURN(0);
} }
STfsTier *pTier = TFS_TIER_AT(pTfs, level); STfsTier *pTier = TFS_TIER_AT(pTfs, level);
@ -177,10 +172,10 @@ int32_t tfsAllocDisk(STfs *pTfs, int32_t expLevel, SDiskID *pDiskId) {
continue; 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; } 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]; char aname[TMPNAME_LEN];
if (pDisk == NULL) { if (pDisk == NULL) {
return -1; TAOS_RETURN(TSDB_CODE_FS_INVLD_CFG);
} }
snprintf(aname, TMPNAME_LEN, "%s%s%s", pDisk->path, TD_DIRSEP, rname); snprintf(aname, TMPNAME_LEN, "%s%s%s", pDisk->path, TD_DIRSEP, rname);
if (taosMkDir(aname) != 0) { if (taosMkDir(aname) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno); TAOS_RETURN(TAOS_SYSTEM_ERROR(errno));
return -1;
} }
return 0; TAOS_RETURN(0);
} }
int32_t tfsMkdirRecurAt(STfs *pTfs, const char *rname, SDiskID diskId) { 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) { if (errno == ENOENT) {
// Try to create upper // 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. // 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 // 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. // the pointer directly in this recursion.
// See // See
// https://developer.apple.com/library/archive/documentation/System/Conceptual/ManPages_iPhoneOS/man3/dirname.3.html // 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) { if ((dir = taosStrdup(taosDirName(s))) == NULL) {
taosMemoryFree(s); TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, &lino, _exit);
taosMemoryFree(dir);
return -1;
} }
taosMemoryFree(s);
taosMemoryFree(dir);
if (tfsMkdirAt(pTfs, rname, diskId) < 0) { if (strlen(dir) >= strlen(rname)) { // TODO: check if it is necessary for equal length
return -1; 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 { } 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) { 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); STfsTier *pTier = TFS_TIER_AT(pTfs, level);
for (int32_t id = 0; id < pTier->ndisk; id++) { for (int32_t id = 0; id < pTier->ndisk; id++) {
SDiskID did = {.id = id, .level = level}; SDiskID did = {.id = id, .level = level};
if (tfsMkdirRecurAt(pTfs, rname, did) < 0) { TAOS_CHECK_RETURN(tfsMkdirRecurAt(pTfs, rname, did));
return -1;
}
} }
} }
return 0; TAOS_RETURN(0);
} }
int32_t tfsMkdir(STfs *pTfs, const char *rname) { 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); STfsTier *pTier = TFS_TIER_AT(pTfs, level);
for (int32_t id = 0; id < pTier->ndisk; id++) { for (int32_t id = 0; id < pTier->ndisk; id++) {
SDiskID did = {.id = id, .level = level}; SDiskID did = {.id = id, .level = level};
if (tfsMkdirAt(pTfs, rname, did) < 0) { TAOS_CHECK_RETURN(tfsMkdirAt(pTfs, rname, did));
return -1;
}
} }
} }
return 0; TAOS_RETURN(0);
} }
bool tfsDirExistAt(STfs *pTfs, const char *rname, SDiskID diskId) { 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) { int32_t tfsRmdir(STfs *pTfs, const char *rname) {
if (rname[0] == 0) { if (rname[0] == 0) {
return 0; TAOS_RETURN(0);
} }
char aname[TMPNAME_LEN] = "\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) { 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); snprintf(naname, TMPNAME_LEN, "%s%s%s", pDisk->path, TD_DIRSEP, nrname);
if (taosRenameFile(oaname, naname) != 0 && errno != ENOENT) { if (taosRenameFile(oaname, naname) != 0 && errno != ENOENT) {
terrno = TAOS_SYSTEM_ERROR(errno); int32_t code = TAOS_SYSTEM_ERROR(errno);
fError("failed to rename %s to %s since %s", oaname, naname, terrstr()); fError("%s failed to rename %s to %s since %s", __func__, oaname, naname, tstrerror(code));
return -1; TAOS_RETURN(code);
} }
return 0; TAOS_RETURN(0);
} }
int32_t tfsRename(STfs *pTfs, int32_t diskPrimary, const char *orname, const char *nrname) { 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}; SDiskID diskId = {.level = level, .id = id};
if (tfsRenameAt(pTfs, diskId, orname, nrname)) { TAOS_CHECK_RETURN(tfsRenameAt(pTfs, diskId, orname, nrname));
return -1;
}
} }
} }
SDiskID diskId = {.level = 0, .id = diskPrimary}; 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) { 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; 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)); STfsDir *pDir = taosMemoryCalloc(1, sizeof(STfsDir));
if (pDir == NULL) { if (pDir == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY; TAOS_CHECK_GOTO(TSDB_CODE_OUT_OF_MEMORY, NULL, _exit);
return NULL;
} }
SDiskID diskId = {.id = 0, .level = 0}; SDiskID diskId = {.id = 0, .level = 0};
@ -438,12 +437,11 @@ STfsDir *tfsOpendir(STfs *pTfs, const char *rname) {
pDir->pTfs = pTfs; pDir->pTfs = pTfs;
tstrncpy(pDir->dirName, rname, TSDB_FILENAME_LEN); tstrncpy(pDir->dirName, rname, TSDB_FILENAME_LEN);
if (tfsOpendirImpl(pTfs, pDir) < 0) { TAOS_CHECK_GOTO(tfsOpendirImpl(pTfs, pDir), NULL, _exit);
taosMemoryFree(pDir);
return NULL;
}
return pDir; _exit:
taosMemoryFree(pDir);
TAOS_RETURN(code);
} }
const STfsFile *tfsReaddir(STfsDir *pTfsDir) { const STfsFile *tfsReaddir(STfsDir *pTfsDir) {
@ -490,30 +488,35 @@ void tfsClosedir(STfsDir *pTfsDir) {
} }
static int32_t tfsMount(STfs *pTfs, SDiskCfg *pCfg) { static int32_t tfsMount(STfs *pTfs, SDiskCfg *pCfg) {
if (tfsCheckAndFormatCfg(pTfs, pCfg) < 0) { int32_t code = 0;
return -1; int32_t lino = 0;
}
TAOS_CHECK_GOTO(tfsCheckAndFormatCfg(pTfs, pCfg), &lino, _exit);
SDiskID did = {.level = pCfg->level}; SDiskID did = {.level = pCfg->level};
STfsDisk *pDisk = NULL; STfsDisk *pDisk = NULL;
tfsMountDiskToTier(TFS_TIER_AT(pTfs, did.level), pCfg, &pDisk); TAOS_CHECK_GOTO(tfsMountDiskToTier(TFS_TIER_AT(pTfs, did.level), pCfg, &pDisk), &lino, _exit);
if (pDisk == NULL) {
fError("failed to mount disk %s to level %d since %s", pCfg->dir, pCfg->level, terrstr());
return -1;
}
did.id = pDisk->id; 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) { if (pTfs->nlevel < pCfg->level + 1) {
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) { 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) { if (pCfg->level < 0 || pCfg->level >= TFS_MAX_TIERS) {
fError("failed to mount %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);
@ -542,32 +545,34 @@ static int32_t tfsCheckAndFormatCfg(STfs *pTfs, SDiskCfg *pCfg) {
} }
} }
if (tfsFormatDir(pCfg->dir, dirName) < 0) { if ((code = tfsFormatDir(pCfg->dir, dirName)) < 0) {
fError("failed to mount %s to FS since %s", pCfg->dir, terrstr()); fError("failed to mount %s to FS since %s", pCfg->dir, tstrerror(code));
return -1; 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); fError("failed to mount %s to FS since duplicate mount", pCfg->dir);
terrno = TSDB_CODE_FS_INVLD_CFG; TAOS_RETURN(TSDB_CODE_FS_INVLD_CFG);
return -1;
} }
if (!taosCheckAccessFile(dirName, TD_FILE_ACCESS_EXIST_OK | TD_FILE_ACCESS_READ_OK | TD_FILE_ACCESS_WRITE_OK)) { 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); fError("failed to mount %s to FS since no R/W access rights", pCfg->dir);
terrno = TSDB_CODE_FS_INVLD_CFG; TAOS_RETURN(TSDB_CODE_FS_INVLD_CFG);
return -1;
} }
if (!taosIsDir(dirName)) { if (!taosIsDir(dirName)) {
fError("failed to mount %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; TAOS_RETURN(TSDB_CODE_FS_INVLD_CFG);
return -1;
} }
strncpy(pCfg->dir, dirName, TSDB_FILENAME_LEN); strncpy(pCfg->dir, dirName, TSDB_FILENAME_LEN);
return 0; TAOS_RETURN(0);
} }
static int32_t tfsFormatDir(char *idir, char *odir) { static int32_t tfsFormatDir(char *idir, char *odir) {
@ -587,7 +592,7 @@ static int32_t tfsFormatDir(char *idir, char *odir) {
strcpy(odir, tmp); strcpy(odir, tmp);
wordfree(&wep); wordfree(&wep);
return 0; TAOS_RETURN(0);
} }
static int32_t tfsCheck(STfs *pTfs) { 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)); 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; SDiskID did = *(SDiskID *)pr;
STfsDisk *pDisk = TFS_DISK_AT(pTfs, did); *ppDisk = TFS_DISK_AT(pTfs, did);
return pDisk; TAOS_RETURN(0);
} }
static int32_t tfsOpendirImpl(STfs *pTfs, STfsDir *pTfsDir) { static int32_t tfsOpendirImpl(STfs *pTfs, STfsDir *pTfsDir) {
@ -635,7 +642,7 @@ static int32_t tfsOpendirImpl(STfs *pTfs, STfsDir *pTfsDir) {
while (true) { while (true) {
pDisk = tfsNextDisk(pTfs, &pTfsDir->iter); pDisk = tfsNextDisk(pTfs, &pTfsDir->iter);
if (pDisk == NULL) return 0; if (pDisk == NULL) TAOS_RETURN(0);
pTfsDir->did.level = pDisk->level; pTfsDir->did.level = pDisk->level;
pTfsDir->did.id = pDisk->id; pTfsDir->did.id = pDisk->id;
@ -647,9 +654,10 @@ static int32_t tfsOpendirImpl(STfs *pTfs, STfsDir *pTfsDir) {
} }
pTfsDir->pDir = taosOpenDir(adir); pTfsDir->pDir = taosOpenDir(adir);
if (pTfsDir->pDir != NULL) break; 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) { static STfsDisk *tfsNextDisk(STfs *pTfs, SDiskIter *pIter) {
@ -702,5 +710,5 @@ int32_t tfsGetMonitorInfo(STfs *pTfs, SMonDiskInfo *pInfo) {
} }
tfsUnLock(pTfs); tfsUnLock(pTfs);
return 0; TAOS_RETURN(0);
} }

View File

@ -22,11 +22,11 @@ int32_t tfsNewDisk(int32_t level, int32_t id, int8_t disable, const char *path,
STfsDisk *pDisk = NULL; STfsDisk *pDisk = NULL;
if ((pDisk = taosMemoryCalloc(1, sizeof(STfsDisk))) == 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) { 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; 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; pDisk->disable = disable;
if (taosGetDiskSize(pDisk->path, &pDisk->size) < 0) { if (taosGetDiskSize(pDisk->path, &pDisk->size) < 0) {
code = TAOS_SYSTEM_ERROR(errno); // TODO: refactor this line 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) { if (code != 0) {
pDisk = tfsFreeDisk(pDisk); pDisk = tfsFreeDisk(pDisk);
fError("%s failed at line %d since %s, disk:%s level:%d id:%d ", __func__, lino, tstrerror(code), path, level, id); fError("%s failed at line %d since %s, disk:%s level:%d id:%d ", __func__, lino, tstrerror(code), path, level, id);

View File

@ -44,7 +44,7 @@ int32_t tfsMountDiskToTier(STfsTier *pTier, SDiskCfg *pCfg, STfsDisk **ppDisk) {
STfsDisk *pDisk = NULL; STfsDisk *pDisk = NULL;
if (pTier->ndisk >= TFS_MAX_DISKS_PER_TIER) { 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; int32_t id = 0;
@ -63,15 +63,15 @@ int32_t tfsMountDiskToTier(STfsTier *pTier, SDiskCfg *pCfg, STfsDisk **ppDisk) {
} }
if (id >= TFS_MAX_DISKS_PER_TIER) { 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->disks[id] = pDisk;
pTier->ndisk++; pTier->ndisk++;
_OVER: _exit:
if (code != 0) { if (code != 0) {
pDisk = tfsFreeDisk(pDisk); pDisk = tfsFreeDisk(pDisk);
fError("%s failed at line %d since %s, disk:%s level:%d id:%d", __func__, lino, tstrerror(code), pCfg->dir, fError("%s failed at line %d since %s, disk:%s level:%d id:%d", __func__, lino, tstrerror(code), pCfg->dir,

View File

@ -40,11 +40,12 @@ TEST_F(TfsTest, 01_Open_Close) {
dCfg.disable = 0; dCfg.disable = 0;
taosRemoveDir(root); taosRemoveDir(root);
STfs *pTfs = tfsOpen(&dCfg, 1); STfs *pTfs = NULL;
(void)tfsOpen(&dCfg, 1, &pTfs);
ASSERT_EQ(pTfs, nullptr); ASSERT_EQ(pTfs, nullptr);
taosMulMkDir(root); taosMulMkDir(root);
pTfs = tfsOpen(&dCfg, 1); (void)tfsOpen(&dCfg, 1, &pTfs);
ASSERT_NE(pTfs, nullptr); ASSERT_NE(pTfs, nullptr);
tfsUpdateSize(pTfs); tfsUpdateSize(pTfs);
@ -68,7 +69,8 @@ TEST_F(TfsTest, 02_AllocDisk) {
taosRemoveDir(root); taosRemoveDir(root);
taosMkDir(root); taosMkDir(root);
STfs *pTfs = tfsOpen(&dCfg, 1); STfs *pTfs = NULL;
(void)tfsOpen(&dCfg, 1, &pTfs);
ASSERT_NE(pTfs, nullptr); ASSERT_NE(pTfs, nullptr);
SDiskID did; SDiskID did;
@ -120,7 +122,8 @@ TEST_F(TfsTest, 03_Dir) {
taosRemoveDir(root); taosRemoveDir(root);
taosMkDir(root); taosMkDir(root);
STfs *pTfs = tfsOpen(&dCfg, 1); STfs *pTfs = NULL;
(void)tfsOpen(&dCfg, 1, &pTfs);
ASSERT_NE(pTfs, nullptr); ASSERT_NE(pTfs, nullptr);
char p1[] = "p1"; char p1[] = "p1";
@ -175,7 +178,8 @@ TEST_F(TfsTest, 04_File) {
taosRemoveDir(root); taosRemoveDir(root);
taosMkDir(root); taosMkDir(root);
STfs *pTfs = tfsOpen(&dCfg, 1); STfs *pTfs = NULL;
(void)tfsOpen(&dCfg, 1, &pTfs);
ASSERT_NE(pTfs, nullptr); ASSERT_NE(pTfs, nullptr);
STfsFile file0; STfsFile file0;
@ -264,7 +268,8 @@ TEST_F(TfsTest, 04_File) {
EXPECT_NE(taosDirExist(af2), 1); EXPECT_NE(taosDirExist(af2), 1);
{ {
STfsDir *pDir = tfsOpendir(pTfs, "t3"); STfsDir *pDir = NULL;
tfsOpendir(pTfs, "t3", &pDir);
const STfsFile *pf1 = tfsReaddir(pDir); const STfsFile *pf1 = tfsReaddir(pDir);
EXPECT_NE(pf1, nullptr); EXPECT_NE(pf1, nullptr);
@ -281,7 +286,8 @@ TEST_F(TfsTest, 04_File) {
EXPECT_GT(tfsCopyFile(&f1, &f2), 0); EXPECT_GT(tfsCopyFile(&f1, &f2), 0);
{ {
STfsDir *pDir = tfsOpendir(pTfs, "t3"); STfsDir *pDir = NULL;
tfsOpendir(pTfs, "t3", &pDir);
const STfsFile *pf1 = tfsReaddir(pDir); const STfsFile *pf1 = tfsReaddir(pDir);
EXPECT_NE(pf1, nullptr); EXPECT_NE(pf1, nullptr);
@ -386,17 +392,18 @@ TEST_F(TfsTest, 05_MultiDisk) {
taosMkDir(root22); taosMkDir(root22);
taosMkDir(root23); taosMkDir(root23);
STfs *pTfs = tfsOpen(dCfg, 9); STfs *pTfs = NULL;
(void)tfsOpen(dCfg, 9, &pTfs);
ASSERT_EQ(pTfs, nullptr); ASSERT_EQ(pTfs, nullptr);
dCfg[0].primary = 1; dCfg[0].primary = 1;
dCfg[1].primary = 1; dCfg[1].primary = 1;
pTfs = tfsOpen(dCfg, 9); (void)tfsOpen(dCfg, 9, &pTfs);
ASSERT_EQ(pTfs, nullptr); ASSERT_EQ(pTfs, nullptr);
dCfg[0].primary = 0; dCfg[0].primary = 0;
dCfg[1].primary = 1; dCfg[1].primary = 1;
pTfs = tfsOpen(dCfg, 9); (void)tfsOpen(dCfg, 9, &pTfs);
ASSERT_NE(pTfs, nullptr); ASSERT_NE(pTfs, nullptr);
tfsUpdateSize(pTfs); tfsUpdateSize(pTfs);
@ -693,7 +700,8 @@ TEST_F(TfsTest, 05_MultiDisk) {
tfsRemoveFile(&f2); tfsRemoveFile(&f2);
{ {
STfsDir *pDir = tfsOpendir(pTfs, "t3"); STfsDir *pDir = NULL;
tfsOpendir(pTfs, "t3", &pDir);
const STfsFile *pf1 = tfsReaddir(pDir); const STfsFile *pf1 = tfsReaddir(pDir);
EXPECT_NE(pf1, nullptr); EXPECT_NE(pf1, nullptr);
@ -711,7 +719,8 @@ TEST_F(TfsTest, 05_MultiDisk) {
EXPECT_GT(tfsCopyFile(&f1, &f2), 0); EXPECT_GT(tfsCopyFile(&f1, &f2), 0);
{ {
STfsDir *pDir = tfsOpendir(pTfs, "t3"); STfsDir *pDir = NULL;
tfsOpendir(pTfs, "t3", &pDir);
const STfsFile *pf1 = tfsReaddir(pDir); const STfsFile *pf1 = tfsReaddir(pDir);
EXPECT_NE(pf1, nullptr); EXPECT_NE(pf1, nullptr);