diff --git a/include/util/taoserror.h b/include/util/taoserror.h index 854d16f67d57db105d866a7b2adb0585fde850f2..4f424111a8ee50027e68d6904a999d9c27ea18ae 100644 --- a/include/util/taoserror.h +++ b/include/util/taoserror.h @@ -406,7 +406,7 @@ int32_t* taosGetErrno(); #define TSDB_CODE_WAL_OUT_OF_MEMORY TAOS_DEF_ERROR_CODE(0, 0x1004) //"WAL out of memory") // tfs -#define TSDB_CODE_FS_OUT_OF_MEMORY TAOS_DEF_ERROR_CODE(0, 0x2200) //"tfs out of memory") +#define TSDB_CODE_FS_APP_ERROR TAOS_DEF_ERROR_CODE(0, 0x2200) //"tfs out of memory") #define TSDB_CODE_FS_INVLD_CFG TAOS_DEF_ERROR_CODE(0, 0x2201) //"tfs invalid mount config") #define TSDB_CODE_FS_TOO_MANY_MOUNT TAOS_DEF_ERROR_CODE(0, 0x2202) //"tfs too many mount") #define TSDB_CODE_FS_DUP_PRIMARY TAOS_DEF_ERROR_CODE(0, 0x2203) //"tfs duplicate primary mount") diff --git a/source/libs/tfs/inc/tfsInt.h b/source/libs/tfs/inc/tfsInt.h index 40eb61ba079853eea3b0a6a94f94f1f8b5a5d47f..4e53fcdb8eda1af53246b9a9439eb36dad23ed77 100644 --- a/source/libs/tfs/inc/tfsInt.h +++ b/source/libs/tfs/inc/tfsInt.h @@ -26,7 +26,7 @@ #include "thash.h" #include "tlog.h" -extern int fsDebugFlag; +extern int32_t fsDebugFlag; // For debug purpose #define fFatal(...) { if (fsDebugFlag & DEBUG_FATAL) { taosPrintLog("TFS FATAL ", 255, __VA_ARGS__); }} @@ -47,8 +47,8 @@ typedef struct { } SDiskMeta; typedef struct SDisk { - int level; - int id; + int32_t level; + int32_t id; char dir[TSDB_FILENAME_LEN]; SDiskMeta dmeta; } SDisk; @@ -61,19 +61,19 @@ typedef struct SDisk { #define DISK_USED_SIZE(pd) ((pd)->dmeta.used) #define DISK_FREE_SIZE(pd) ((pd)->dmeta.free) -SDisk *tfsNewDisk(int level, int id, const char *dir); -SDisk *tfsFreeDisk(SDisk *pDisk); -int tfsUpdateDiskInfo(SDisk *pDisk); +SDisk *tfsNewDisk(int32_t level, int32_t id, const char *dir); +SDisk *tfsFreeDisk(SDisk *pDisk); +int32_t tfsUpdateDiskInfo(SDisk *pDisk); // ttier.c ====================================================== typedef struct STier { pthread_spinlock_t lock; - int level; + int32_t level; int16_t ndisk; // # of disks mounted to this tier int16_t nextid; // next disk id to allocate STierMeta tmeta; - SDisk * disks[TSDB_MAX_DISKS_PER_TIER]; + SDisk *disks[TSDB_MAX_DISKS_PER_TIER]; } STier; #define TIER_LEVEL(pt) ((pt)->level) @@ -83,12 +83,11 @@ typedef struct STier { #define TIER_AVAIL_DISKS(pt) ((pt)->tmeta.nAvailDisks) #define DISK_AT_TIER(pt, id) ((pt)->disks[id]) -int tfsInitTier(STier *pTier, int level); -void tfsDestroyTier(STier *pTier); -SDisk *tfsMountDiskToTier(STier *pTier, SDiskCfg *pCfg); -void tfsUpdateTierInfo(STier *pTier, STierMeta *pTierMeta); -int tfsAllocDiskOnTier(STier *pTier); -void tfsGetTierMeta(STier *pTier, STierMeta *pTierMeta); +int32_t tfsInitTier(STier *pTier, int32_t level); +void tfsDestroyTier(STier *pTier); +SDisk *tfsMountDiskToTier(STier *pTier, SDiskCfg *pCfg); +void tfsUpdateTierInfo(STier *pTier, STierMeta *pTierMeta); +int32_t tfsAllocDiskOnTier(STier *pTier); void tfsPosNextId(STier *pTier); #ifdef __cplusplus diff --git a/source/libs/tfs/src/tfsDisk.c b/source/libs/tfs/src/tfsDisk.c index 98656c84e61f39861b64bb66d255bb6becd62d22..dd4f349c420f2cb0eb31f49245eb8cd05ebada73 100644 --- a/source/libs/tfs/src/tfsDisk.c +++ b/source/libs/tfs/src/tfsDisk.c @@ -19,7 +19,7 @@ SDisk *tfsNewDisk(int32_t level, int32_t id, const char *dir) { SDisk *pDisk = calloc(1, sizeof(SDisk)); if (pDisk == NULL) { - terrno = TSDB_CODE_FS_OUT_OF_MEMORY; + terrno = TSDB_CODE_OUT_OF_MEMORY; return NULL; } @@ -34,6 +34,7 @@ SDisk *tfsFreeDisk(SDisk *pDisk) { if (pDisk != NULL) { free(pDisk); } + return NULL; } @@ -44,17 +45,15 @@ int32_t tfsUpdateDiskInfo(SDisk *pDisk) { } SysDiskSize diskSize = {0}; - - int32_t code = taosGetDiskSize(pDisk->dir, &diskSize); - if (code != 0) { + if (taosGetDiskSize(pDisk->dir, &diskSize) != 0) { + terrno = TAOS_SYSTEM_ERROR(errno); fError("failed to update disk information at level %d id %d dir %s since %s", pDisk->level, pDisk->id, pDisk->dir, strerror(errno)); - terrno = TAOS_SYSTEM_ERROR(errno); + return -1 } pDisk->dmeta.size = diskSize.tsize; pDisk->dmeta.used = diskSize.used; pDisk->dmeta.free = diskSize.avail; - - return code; + return 0; } diff --git a/source/libs/tfs/src/tfsTier.c b/source/libs/tfs/src/tfsTier.c index 90057e61d5e8862a996a2e5147e01b86c26bd7e5..8b057d87559717d17a3fc77ce4a16d4ccb68b4c6 100644 --- a/source/libs/tfs/src/tfsTier.c +++ b/source/libs/tfs/src/tfsTier.c @@ -16,45 +16,50 @@ #define _DEFAULT_SOURCE #include "tfsInt.h" -#define tfsLockTier(pTier) pthread_spin_lock(&((pTier)->lock)) -#define tfsUnLockTier(pTier) pthread_spin_unlock(&((pTier)->lock)) +#define tfsLockTier(pTier) pthread_spin_lock(&(pTier)->lock) +#define tfsUnLockTier(pTier) pthread_spin_unlock(&(pTier)->lock) -// PROTECTED ========================================== -int tfsInitTier(STier *pTier, int level) { - memset((void *)pTier, 0, sizeof(*pTier)); +int32_t tfsInitTier(STier *pTier, int32_t level) { + if (pTier == NULL) { + terrno = TSDB_CODE_INVALID_PARA; + return -1; + } + + memset(pTier, 0, sizeof(STier)); - int code = pthread_spin_init(&(pTier->lock), 0); - if (code) { + int32_t code = pthread_spin_init(&pTier->lock, 0); + if (code != 0) { terrno = TAOS_SYSTEM_ERROR(code); return -1; } pTier->level = level; - return 0; } void tfsDestroyTier(STier *pTier) { - for (int id = 0; id < TSDB_MAX_DISKS_PER_TIER; id++) { + if (pTier == NULL) return; + + for (int32_t id = 0; id < TSDB_MAX_DISKS_PER_TIER; id++) { DISK_AT_TIER(pTier, id) = tfsFreeDisk(DISK_AT_TIER(pTier, id)); } pTier->ndisk = 0; - pthread_spin_destroy(&(pTier->lock)); } SDisk *tfsMountDiskToTier(STier *pTier, SDiskCfg *pCfg) { - ASSERT(pTier->level == pCfg->level); - - int id = 0; - SDisk *pDisk; + if (pTier == NULL || pCfg == NULL || pTier->level != pCfg->level) { + terrno = TSDB_CODE_INVALID_PARA; + return -1; + } if (TIER_NDISKS(pTier) >= TSDB_MAX_DISKS_PER_TIER) { terrno = TSDB_CODE_FS_TOO_MANY_MOUNT; return NULL; } + int32_t id = 0; if (pTier->level == 0) { if (DISK_AT_TIER(pTier, 0) != NULL) { id = pTier->ndisk; @@ -73,30 +78,31 @@ SDisk *tfsMountDiskToTier(STier *pTier, SDiskCfg *pCfg) { id = pTier->ndisk; } - pDisk = tfsNewDisk(pCfg->level, id, pCfg->dir); + SDisk *pDisk = tfsNewDisk(pCfg->level, id, pCfg->dir); if (pDisk == NULL) return NULL; + DISK_AT_TIER(pTier, id) = pDisk; pTier->ndisk++; fInfo("disk %s is mounted to tier level %d id %d", pCfg->dir, pCfg->level, id); - return DISK_AT_TIER(pTier, id); } void tfsUpdateTierInfo(STier *pTier, STierMeta *pTierMeta) { - STierMeta tmeta; + STierMeta tmeta = {0}; if (pTierMeta == NULL) { pTierMeta = &tmeta; } - memset(pTierMeta, 0, sizeof(*pTierMeta)); + memset(pTierMeta, 0, sizeof(STierMeta)); tfsLockTier(pTier); - for (int id = 0; id < pTier->ndisk; id++) { - if (tfsUpdateDiskInfo(DISK_AT_TIER(pTier, id)) < 0) { + for (int32_t id = 0; id < pTier->ndisk; id++) { + if (tfsUpdateDiskInfo(DISK_AT_TIER(pTier, id)) != 0) { continue; } + pTierMeta->size += DISK_SIZE(DISK_AT_TIER(pTier, id)); pTierMeta->used += DISK_USED_SIZE(DISK_AT_TIER(pTier, id)); pTierMeta->free += DISK_FREE_SIZE(DISK_AT_TIER(pTier, id)); @@ -109,22 +115,26 @@ void tfsUpdateTierInfo(STier *pTier, STierMeta *pTierMeta) { } // Round-Robin to allocate disk on a tier -int tfsAllocDiskOnTier(STier *pTier) { - ASSERT(pTier->ndisk > 0); - int id = TFS_UNDECIDED_ID; - SDisk *pDisk; +int32_t tfsAllocDiskOnTier(STier *pTier) { + if (pTier == NULL || pTier->ndisk <= 0) { + terrno = TSDB_CODE_INVALID_PARA; + return -1; + } tfsLockTier(pTier); if (TIER_AVAIL_DISKS(pTier) <= 0) { tfsUnLockTier(pTier); - return id; + return TFS_UNDECIDED_ID; } - id = pTier->nextid; + int32_t id = pTier->nextid; while (true) { - pDisk = DISK_AT_TIER(pTier, id); - ASSERT(pDisk != NULL); + SDisk *pDisk = DISK_AT_TIER(pTier, id); + if (pDisk == NULL) { + tfsUnLockTier(pTier); + return TFS_UNDECIDED_ID; + } if (DISK_FREE_SIZE(pDisk) < TFS_MIN_DISK_FREE_SIZE) { id = (id + 1) % pTier->ndisk; @@ -145,7 +155,7 @@ int tfsAllocDiskOnTier(STier *pTier) { } void tfsGetTierMeta(STier *pTier, STierMeta *pTierMeta) { - ASSERT(pTierMeta != NULL); + if (pTierMeta == NULL || pTierMeta == NULL) return; tfsLockTier(pTier); *pTierMeta = pTier->tmeta; @@ -153,10 +163,11 @@ void tfsGetTierMeta(STier *pTier, STierMeta *pTierMeta) { } void tfsPosNextId(STier *pTier) { - ASSERT(pTier->ndisk > 0); - int nextid = 0; + if (pTier == NULL || pTier->ndisk <= 0) return; + + int32_t nextid = 0; - for (int id = 1; id < pTier->ndisk; id++) { + for (int32_t id = 1; id < pTier->ndisk; id++) { SDisk *pLDisk = DISK_AT_TIER(pTier, nextid); SDisk *pDisk = DISK_AT_TIER(pTier, id); if (DISK_FREE_SIZE(pDisk) > TFS_MIN_DISK_FREE_SIZE && DISK_FREE_SIZE(pDisk) > DISK_FREE_SIZE(pLDisk)) { diff --git a/source/util/src/terror.c b/source/util/src/terror.c index 8294bca959242198af9838f31397011f23d202cd..6d0547c7aec9f1f4436732ea0528093270558816 100644 --- a/source/util/src/terror.c +++ b/source/util/src/terror.c @@ -387,7 +387,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_WAL_FILE_CORRUPTED, "WAL file is corrupted TAOS_DEFINE_ERROR(TSDB_CODE_WAL_SIZE_LIMIT, "WAL size exceeds limit") // tfs -TAOS_DEFINE_ERROR(TSDB_CODE_FS_OUT_OF_MEMORY, "tfs out of memory") +TAOS_DEFINE_ERROR(TSDB_CODE_FS_APP_ERROR, "tfs out of memory") TAOS_DEFINE_ERROR(TSDB_CODE_FS_INVLD_CFG, "tfs invalid mount config") TAOS_DEFINE_ERROR(TSDB_CODE_FS_TOO_MANY_MOUNT, "tfs too many mount") TAOS_DEFINE_ERROR(TSDB_CODE_FS_DUP_PRIMARY, "tfs duplicate primary mount")