From 499cf0c2942925f337c6ff574feb7a57e5e152e2 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 8 Feb 2025 14:34:22 +0800 Subject: [PATCH] more code --- source/dnode/vnode/src/tsdb/tsdbUtil.c | 9 ++-- source/libs/tfs/inc/tfsInt.h | 5 +- source/libs/tfs/src/tfs.c | 6 +-- source/libs/tfs/src/tfsTier.c | 67 ++++++++++++++++++++------ 4 files changed, 62 insertions(+), 25 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbUtil.c b/source/dnode/vnode/src/tsdb/tsdbUtil.c index 62afba0a38..78cdcfa2ae 100644 --- a/source/dnode/vnode/src/tsdb/tsdbUtil.c +++ b/source/dnode/vnode/src/tsdb/tsdbUtil.c @@ -1813,12 +1813,14 @@ int32_t tsdbAllocateDisk(STsdb *tsdb, int32_t fid, const char *label, SDiskID *d int32_t expectedLevel = tsdbFidLevel(fid, &tsdb->keepCfg, taosGetTimestampSec()); code = tfsAllocDisk(tfs, expectedLevel, label, &did); if (code) { - tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code)); + tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, __LINE__, + tstrerror(code)); return code; } if (tfsMkdirRecurAt(tfs, tsdb->path, did) != 0) { - tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code)); + tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, __LINE__, + tstrerror(code)); } if (diskId) { @@ -1840,7 +1842,8 @@ int32_t tsdbAllocateDiskAtLevel(STsdb *tsdb, int32_t fid, int32_t level, const c } if (tfsMkdirRecurAt(tfs, tsdb->path, did) != 0) { - tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, lino, tstrerror(code)); + tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(tsdb->pVnode), __func__, __FILE__, __LINE__, + tstrerror(code)); } if (diskId) { diff --git a/source/libs/tfs/inc/tfsInt.h b/source/libs/tfs/inc/tfsInt.h index 7f8c712947..9b4b29963e 100644 --- a/source/libs/tfs/inc/tfsInt.h +++ b/source/libs/tfs/inc/tfsInt.h @@ -50,10 +50,11 @@ typedef struct { typedef struct { TdThreadSpinlock lock; int32_t level; - int32_t nextid; // next disk id to allocate + int32_t nextid; int32_t ndisk; // # of disks mounted to this tier int32_t nAvailDisks; // # of Available disks STfsDisk *disks[TFS_MAX_DISKS_PER_TIER]; + SHashObj *hash; // label -> nextid SDiskSize size; } STfsTier; @@ -87,7 +88,7 @@ int32_t tfsInitTier(STfsTier *pTier, int32_t level); void tfsDestroyTier(STfsTier *pTier); int32_t tfsMountDiskToTier(STfsTier *pTier, SDiskCfg *pCfg, STfsDisk **ppDisk); void tfsUpdateTierSize(STfsTier *pTier); -int32_t tfsAllocDiskOnTier(STfsTier *pTier); +int32_t tfsAllocDiskOnTier(STfsTier *pTier, const char *label); void tfsPosNextId(STfsTier *pTier); #define tfsLockTier(pTier) taosThreadSpinLock(&(pTier)->lock) diff --git a/source/libs/tfs/src/tfs.c b/source/libs/tfs/src/tfs.c index 7634bd59b4..969e17f53c 100644 --- a/source/libs/tfs/src/tfs.c +++ b/source/libs/tfs/src/tfs.c @@ -156,7 +156,7 @@ int32_t tfsAllocDiskAtLevel(STfs *pTfs, int32_t level, const char *label, SDiskI pDiskId->level = level; pDiskId->id = -1; - pDiskId->id = tfsAllocDiskOnTier(&pTfs->tiers[pDiskId->level]); + pDiskId->id = tfsAllocDiskOnTier(&pTfs->tiers[pDiskId->level], label); if (pDiskId->id < 0) { TAOS_RETURN(TSDB_CODE_FS_NO_VALID_DISK); } @@ -165,10 +165,8 @@ int32_t tfsAllocDiskAtLevel(STfs *pTfs, int32_t level, const char *label, SDiskI } int32_t tfsAllocDisk(STfs *pTfs, int32_t expLevel, const char *label, SDiskID *pDiskId) { - if (expLevel >= pTfs->nlevel) { + if (expLevel >= pTfs->nlevel || expLevel < 0) { expLevel = pTfs->nlevel - 1; - } else if (expLevel < 0) { - expLevel = 0; } for (; expLevel >= 0; expLevel--) { diff --git a/source/libs/tfs/src/tfsTier.c b/source/libs/tfs/src/tfsTier.c index acc8168538..8f229f1f84 100644 --- a/source/libs/tfs/src/tfsTier.c +++ b/source/libs/tfs/src/tfsTier.c @@ -18,6 +18,26 @@ extern int64_t tsMinDiskFreeSize; +typedef struct { + int32_t nextid; +} SDiskCursor; + +static SDiskCursor *tfsDiskCursorNew(int32_t nextid) { + SDiskCursor *pCursor = (SDiskCursor *)taosMemoryMalloc(sizeof(SDiskCursor)); + if (pCursor == NULL) { + return NULL; + } + pCursor->nextid = nextid; + return pCursor; +} + +static void tfsDiskCursorFree(void *p) { + if (p) { + SDiskCursor *pCursor = *(SDiskCursor **)p; + taosMemoryFree(pCursor); + } +} + int32_t tfsInitTier(STfsTier *pTier, int32_t level) { (void)memset(pTier, 0, sizeof(STfsTier)); @@ -26,10 +46,17 @@ int32_t tfsInitTier(STfsTier *pTier, int32_t level) { } pTier->level = level; + pTier->hash = taosHashInit(64, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BINARY), false, HASH_NO_LOCK); + if (pTier->hash == NULL) { + TAOS_RETURN(terrno); + } + taosHashSetFreeFp(pTier->hash, tfsDiskCursorFree); return 0; } void tfsDestroyTier(STfsTier *pTier) { + taosHashCleanup(pTier->hash); + pTier->hash = NULL; for (int32_t id = 0; id < TFS_MAX_DISKS_PER_TIER; id++) { pTier->disks[id] = tfsFreeDisk(pTier->disks[id]); } @@ -108,7 +135,7 @@ void tfsUpdateTierSize(STfsTier *pTier) { } // Round-Robin to allocate disk on a tier -int32_t tfsAllocDiskOnTier(STfsTier *pTier) { +int32_t tfsAllocDiskOnTier(STfsTier *pTier, const char *label) { TAOS_UNUSED(tfsLockTier(pTier)); if (pTier->ndisk <= 0 || pTier->nAvailDisks <= 0) { @@ -116,11 +143,31 @@ int32_t tfsAllocDiskOnTier(STfsTier *pTier) { TAOS_RETURN(TSDB_CODE_FS_NO_VALID_DISK); } + // get the existing cursor or create a new one + SDiskCursor *pCursor = NULL; + void *p = taosHashGet(pTier->hash, label, strlen(label)); + + if (p) { + pCursor = *(SDiskCursor **)p; + } else { + pCursor = tfsDiskCursorNew(pTier->nextid); + if (pCursor == NULL) { + TAOS_UNUSED(tfsUnLockTier(pTier)); + TAOS_RETURN(terrno); + } + + int32_t code = taosHashPut(pTier->hash, label, strlen(label), &pCursor, sizeof(pCursor)); + if (code != 0) { + TAOS_UNUSED(tfsUnLockTier(pTier)); + TAOS_RETURN(code); + } + } + + // do allocation int32_t retId = -1; int64_t avail = 0; for (int32_t id = 0; id < TFS_MAX_DISKS_PER_TIER; ++id) { -#if 1 // round-robin - int32_t diskId = (pTier->nextid + id) % pTier->ndisk; + int32_t diskId = (pCursor->nextid + id) % pTier->ndisk; STfsDisk *pDisk = pTier->disks[diskId]; if (pDisk == NULL) continue; @@ -139,20 +186,8 @@ int32_t tfsAllocDiskOnTier(STfsTier *pTier) { retId = diskId; terrno = 0; - pTier->nextid = (diskId + 1) % pTier->ndisk; + pCursor->nextid = (diskId + 1) % pTier->ndisk; break; -#else // select the disk with the most available space - STfsDisk *pDisk = pTier->disks[id]; - if (pDisk == NULL) continue; - - if (pDisk->size.avail < tsMinDiskFreeSize) continue; - - if (pDisk->size.avail > avail) { - avail = pDisk->size.avail; - retId = id; - terrno = 0; - } -#endif } TAOS_UNUSED(tfsUnLockTier(pTier));