From a9eeafe319af9d22d64033305c4c68feb56d99fc Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 20 Jan 2022 09:41:28 +0000 Subject: [PATCH] more tdb --- include/util/tlist.h | 52 +++++++++--------- source/libs/tdb/src/db/tdb_mpool.c | 85 ++++++++++++++++++++++++++--- source/libs/tdb/src/inc/tdb_mpool.h | 39 +++++++------ 3 files changed, 126 insertions(+), 50 deletions(-) diff --git a/include/util/tlist.h b/include/util/tlist.h index 4311356cd8..134873a993 100644 --- a/include/util/tlist.h +++ b/include/util/tlist.h @@ -34,7 +34,7 @@ extern "C" { #define TD_SLIST_HEAD(sl) ((sl)->sl_head_) #define TD_SLIST_NELES(sl) ((sl)->sl_neles_) #define TD_SLIST_NODE_NEXT(sln) ((sln)->sl_next_) -#define TD_SLIST_NODE_NEXT_WITH_FIELD(sln, feild) ((sln)->feild.sl_next_) +#define TD_SLIST_NODE_NEXT_WITH_FIELD(sln, field) ((sln)->field.sl_next_) #define TD_SLIST_INIT(sl) \ do { \ @@ -49,9 +49,9 @@ extern "C" { TD_SLIST_NELES(sl) += 1; \ } while (0) -#define TD_SLIST_PUSH_WITH_FIELD(sl, sln, feild) \ +#define TD_SLIST_PUSH_WITH_FIELD(sl, sln, field) \ do { \ - TD_SLIST_NODE_NEXT_WITH_FIELD(sln, feild) = TD_SLIST_HEAD(sl); \ + TD_SLIST_NODE_NEXT_WITH_FIELD(sln, field) = TD_SLIST_HEAD(sl); \ TD_SLIST_HEAD(sl) = (sln); \ TD_SLIST_NELES(sl) += 1; \ } while (0) @@ -62,9 +62,9 @@ extern "C" { TD_SLIST_NELES(sl) -= 1; \ } while (0) -#define TD_SLIST_POP_WITH_FIELD(sl, feild) \ +#define TD_SLIST_POP_WITH_FIELD(sl, field) \ do { \ - TD_SLIST_HEAD(sl) = TD_SLIST_NODE_NEXT_WITH_FIELD(TD_SLIST_HEAD(sl), feild); \ + TD_SLIST_HEAD(sl) = TD_SLIST_NODE_NEXT_WITH_FIELD(TD_SLIST_HEAD(sl), field); \ TD_SLIST_NELES(sl) -= 1; \ } while (0) @@ -84,8 +84,8 @@ extern "C" { #define TD_DLIST_NODE_PREV(dln) ((dln)->dl_prev_) #define TD_DLIST_NODE_NEXT(dln) ((dln)->dl_next_) -#define TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) ((dln)->feild.dl_prev_) -#define TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) ((dln)->feild.dl_next_) +#define TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) ((dln)->field.dl_prev_) +#define TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) ((dln)->field.dl_next_) #define TD_DLIST_HEAD(dl) ((dl)->dl_head_) #define TD_DLIST_TAIL(dl) ((dl)->dl_tail_) #define TD_DLIST_NELES(dl) ((dl)->dl_neles_) @@ -110,15 +110,15 @@ extern "C" { TD_DLIST_NELES(dl) += 1; \ } while (0) -#define TD_DLIST_APPEND_WITH_FEILD(dl, dln, feild) \ +#define TD_DLIST_APPEND_WITH_FIELD(dl, dln, field) \ do { \ if (TD_DLIST_HEAD(dl) == NULL) { \ - TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) = NULL; \ + TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = NULL; \ TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = (dln); \ } else { \ - TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) = TD_DLIST_TAIL(dl); \ - TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) = NULL; \ - TD_DLIST_NODE_NEXT_WITH_FIELD(TD_DLIST_TAIL(dl), feild) = (dln); \ + TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = TD_DLIST_TAIL(dl); \ + TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = NULL; \ + TD_DLIST_NODE_NEXT_WITH_FIELD(TD_DLIST_TAIL(dl), field) = (dln); \ TD_DLIST_TAIL(dl) = (dln); \ } \ TD_DLIST_NELES(dl) += 1; \ @@ -138,15 +138,15 @@ extern "C" { TD_DLIST_NELES(dl) += 1; \ } while (0) -#define TD_DLIST_PREPEND_WITH_FIELD(dl, dln, feild) \ +#define TD_DLIST_PREPEND_WITH_FIELD(dl, dln, field) \ do { \ if (TD_DLIST_HEAD(dl) == NULL) { \ - TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) = NULL; \ + TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = NULL; \ TD_DLIST_HEAD(dl) = TD_DLIST_TAIL(dl) = (dln); \ } else { \ - TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) = NULL; \ - TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) = TD_DLIST_HEAD(dl); \ - TD_DLIST_NODE_PREV_WITH_FIELD(TD_DLIST_HEAD(dl), feild) = (dln); \ + TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = NULL; \ + TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = TD_DLIST_HEAD(dl); \ + TD_DLIST_NODE_PREV_WITH_FIELD(TD_DLIST_HEAD(dl), field) = (dln); \ TD_DLIST_HEAD(dl) = (dln); \ } \ TD_DLIST_NELES(dl) += 1; \ @@ -173,21 +173,21 @@ extern "C" { #define TD_DLIST_POP_WITH_FIELD(dl, dln, field) \ do { \ if (TD_DLIST_HEAD(dl) == (dln)) { \ - TD_DLIST_HEAD(dl) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild); \ + TD_DLIST_HEAD(dl) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field); \ } \ if (TD_DLIST_TAIL(dl) == (dln)) { \ - TD_DLIST_TAIL(dl) = TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild); \ + TD_DLIST_TAIL(dl) = TD_DLIST_NODE_PREV_WITH_FIELD(dln, field); \ } \ - if (TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) != NULL) { \ - TD_DLIST_NODE_NEXT_WITH_FIELD(TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild), feild) = \ - TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild); \ + if (TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) != NULL) { \ + TD_DLIST_NODE_NEXT_WITH_FIELD(TD_DLIST_NODE_PREV_WITH_FIELD(dln, field), field) = \ + TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field); \ } \ - if (TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) != NULL) { \ - TD_DLIST_NODE_PREV_WITH_FIELD(TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild), feild) = \ - TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild); \ + if (TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) != NULL) { \ + TD_DLIST_NODE_PREV_WITH_FIELD(TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field), field) = \ + TD_DLIST_NODE_PREV_WITH_FIELD(dln, field); \ } \ TD_DLIST_NELES(dl) -= 1; \ - TD_DLIST_NODE_PREV_WITH_FIELD(dln, feild) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, feild) = NULL; \ + TD_DLIST_NODE_PREV_WITH_FIELD(dln, field) = TD_DLIST_NODE_NEXT_WITH_FIELD(dln, field) = NULL; \ } while (0) // General double linked list diff --git a/source/libs/tdb/src/db/tdb_mpool.c b/source/libs/tdb/src/db/tdb_mpool.c index 9f2b8bb15b..d0092a44c9 100644 --- a/source/libs/tdb/src/db/tdb_mpool.c +++ b/source/libs/tdb/src/db/tdb_mpool.c @@ -15,8 +15,10 @@ #include "tdb_mpool.h" -static int tdbGnrtFileID(const char *fname, uint8_t *fileid); -static void tdbMpoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf); +static int tdbGnrtFileID(const char *fname, uint8_t *fileid); +static void tdbMPoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf); +static void tdbMPoolUnregFile(TDB_MPOOL *mp, TDB_MPFILE *mpf); +static TDB_MPFILE *tdbMPoolGetFile(TDB_MPOOL *mp, uint8_t *fileid); int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) { TDB_MPOOL *mp = NULL; @@ -60,7 +62,7 @@ int tdbMPoolOpen(TDB_MPOOL **mpp, uint64_t cachesize, pgsize_t pgsize) { mp->pages[i]->pgid = TDB_IVLD_PGID; // add new page to the free list - TD_DLIST_APPEND_WITH_FEILD(&(mp->freeList), mp->pages[i], free); + TD_DLIST_APPEND_WITH_FIELD(&(mp->freeList), mp->pages[i], free); } #define PGTAB_FACTOR 1.0 @@ -105,7 +107,6 @@ int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) { } mpf->fd = -1; - mpf->mp = mp; if ((mpf->fname = strdup(fname)) == NULL) { goto _err; @@ -120,7 +121,7 @@ int tdbMPoolFileOpen(TDB_MPFILE **mpfp, const char *fname, TDB_MPOOL *mp) { } // Register current MPF to MP - tdbMpoolRegFile(mp, mpf); + tdbMPoolRegFile(mp, mpf); *mpfp = mpf; return 0; @@ -193,6 +194,76 @@ static int tdbGnrtFileID(const char *fname, uint8_t *fileid) { return 0; } -static void tdbMpoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf) { - // TODO +#define MPF_GET_BUCKETID(fileid) \ + ({ \ + uint64_t *tmp = fileid; \ + (tmp[0] + tmp[1] + tmp[2]) % MPF_HASH_BUCKETS; \ + }) + +static void tdbMPoolRegFile(TDB_MPOOL *mp, TDB_MPFILE *mpf) { + int bucketid; + mpf_bucket_t *bktp; + + bucketid = MPF_GET_BUCKETID(mpf->fileid); + bktp = mp->mpfht.buckets + bucketid; + + taosWLockLatch(&(bktp->latch)); + + TD_DLIST_APPEND_WITH_FIELD(bktp, mpf, node); + + taosWUnLockLatch(&(bktp->latch)); + + mpf->mp = mp; +} + +static TDB_MPFILE *tdbMPoolGetFile(TDB_MPOOL *mp, uint8_t *fileid) { + int bucketid; + TDB_MPFILE * mpf = NULL; + mpf_bucket_t *bktp; + + bucketid = MPF_GET_BUCKETID(fileid); + bktp = mp->mpfht.buckets + bucketid; + + taosRLockLatch(&(bktp->latch)); + + mpf = TD_DLIST_HEAD(bktp); + while (mpf) { + if (memcmp(fileid, mpf->fileid, TDB_FILE_ID_LEN) == 0) { + break; + } + + mpf = TD_DLIST_NODE_NEXT_WITH_FIELD(mpf, node); + } + + taosRUnLockLatch(&(bktp->latch)); + + return mpf; +} + +static void tdbMPoolUnregFile(TDB_MPOOL *mp, TDB_MPFILE *mpf) { + mpf_bucket_t *bktp; + TDB_MPFILE * tmpf; + + if (mpf->mp == NULL) return; + + ASSERT(mpf->mp == mp); + + bktp = mp->mpfht.buckets + MPF_GET_BUCKETID(mpf->fileid); + + taosWLockLatch(&(bktp->latch)); + + tmpf = TD_DLIST_HEAD(bktp); + + while (tmpf) { + if (memcmp(mpf->fileid, tmpf->fileid, TDB_FILE_ID_LEN) == 0) { + TD_DLIST_POP_WITH_FIELD(bktp, tmpf, node); + break; + } + + tmpf = TD_DLIST_NODE_NEXT_WITH_FIELD(tmpf, node); + } + + taosWUnLockLatch(&(bktp->latch)); + + ASSERT(tmpf == mpf); } \ No newline at end of file diff --git a/source/libs/tdb/src/inc/tdb_mpool.h b/source/libs/tdb/src/inc/tdb_mpool.h index e300cd2240..d77e14715d 100644 --- a/source/libs/tdb/src/inc/tdb_mpool.h +++ b/source/libs/tdb/src/inc/tdb_mpool.h @@ -26,21 +26,25 @@ extern "C" { typedef struct TDB_MPOOL TDB_MPOOL; typedef struct TDB_MPFILE TDB_MPFILE; -typedef TD_DLIST_NODE(pg_t) pg_free_list_node_t, pg_hash_list_node_t; +typedef TD_DLIST_NODE(pg_t) pg_free_dlist_node_t, pg_hash_dlist_node_t; typedef struct pg_t { - SRWLatch rwLatch; - frame_id_t frameid; - pgid_t pgid; - uint8_t dirty; - int32_t pinRef; - pg_free_list_node_t free; - pg_hash_list_node_t hash; - uint8_t data[]; + SRWLatch rwLatch; + frame_id_t frameid; + pgid_t pgid; + uint8_t dirty; + int32_t pinRef; + pg_free_dlist_node_t free; + pg_hash_dlist_node_t hash; + uint8_t data[]; } pg_t; #define MP_PAGE_SIZE(pgsize) (sizeof(pg_t) + (pgsize)) typedef TD_DLIST(pg_t) pg_list_t; +typedef struct { + SRWLatch latch; + TD_DLIST(TDB_MPFILE); +} mpf_bucket_t; struct TDB_MPOOL { int64_t cachesize; pgsize_t pgsize; @@ -52,19 +56,20 @@ struct TDB_MPOOL { pg_list_t *hashtab; } pgtab; // page table, hash struct { - int32_t nbucket; - TD_DLIST(TDB_MPFILE) hashtab[8]; - } mpftab; +#define MPF_HASH_BUCKETS 16 + mpf_bucket_t buckets[MPF_HASH_BUCKETS]; + } mpfht; // MPF hash table. MPFs using this MP will be put in this hash table }; #define MP_PAGE_AT(mp, idx) (mp)->pages[idx] +typedef TD_DLIST_NODE(TDB_MPFILE) td_mpf_dlist_node_t; struct TDB_MPFILE { - char * fname; // file name - int fd; // fd - uint8_t fileid[TDB_FILE_ID_LEN]; // file ID - TDB_MPOOL *mp; // underlying memory pool - TD_DLIST_NODE(TDB_MPFILE); + char * fname; // file name + int fd; // fd + uint8_t fileid[TDB_FILE_ID_LEN]; // file ID + TDB_MPOOL * mp; // underlying memory pool + td_mpf_dlist_node_t node; }; /*=================================================== Exposed apis ==================================================*/ -- GitLab