From a4ee9d7c9c87d9d667b648ad818f51596d133345 Mon Sep 17 00:00:00 2001 From: hzcheng Date: Mon, 2 Mar 2020 13:37:03 +0000 Subject: [PATCH] refactor --- src/vnode/tsdb/inc/tsdb.h | 4 +-- src/vnode/tsdb/inc/tsdbMeta.h | 4 +-- src/vnode/tsdb/src/tsdbMain.c | 60 ++++++++++++++++------------------- src/vnode/tsdb/src/tsdbMeta.c | 32 +++++++++---------- 4 files changed, 47 insertions(+), 53 deletions(-) diff --git a/src/vnode/tsdb/inc/tsdb.h b/src/vnode/tsdb/inc/tsdb.h index 8478becaf2..f225fde093 100644 --- a/src/vnode/tsdb/inc/tsdb.h +++ b/src/vnode/tsdb/inc/tsdb.h @@ -37,7 +37,6 @@ typedef struct { // the TSDB repository configuration typedef struct { - char * rootDir; // TSDB repository root directory, TODO: need to adjust here int32_t tsdbId; int32_t maxTables; // maximum number of tables this repository can have int32_t daysPerFile; // day per file sharding policy @@ -45,7 +44,6 @@ typedef struct { int32_t maxRowsPerFileBlock; // maximum rows per file block int32_t keep; // day of data to keep int64_t maxCacheSize; // maximum cache size this TSDB can use - void * cachePool; // the cache pool the repository to use } STsdbCfg; // the TSDB repository info @@ -89,7 +87,7 @@ typedef struct { * * @return a TSDB repository handle on success, NULL for failure and the error number is set */ -tsdb_repo_t *tsdbCreateRepo(STsdbCfg *pCfg); +tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter); /** * Close and free all resources taken by the repository diff --git a/src/vnode/tsdb/inc/tsdbMeta.h b/src/vnode/tsdb/inc/tsdbMeta.h index 78d55e68ad..81c3594952 100644 --- a/src/vnode/tsdb/inc/tsdbMeta.h +++ b/src/vnode/tsdb/inc/tsdbMeta.h @@ -62,7 +62,7 @@ typedef struct { // A map of tableName->tableId // TODO: May use hash table void *pNameTableMap; -} SMetaHandle; +} STsdbMeta; // ---- Operation on STable #define TSDB_TABLE_ID(pTable) ((pTable)->tableId) @@ -84,7 +84,7 @@ SSchema *tsdbGetTableSchema(STable *pTable); #define TSDB_GET_TABLE_OF_NAME(pHandle, name) /* TODO */ // Create a new meta handle with configuration -SMetaHandle * tsdbCreateMetaHandle (int32_t numOfTables); +SMetaHandle * tsdbCreateMeta (int32_t numOfTables); int32_t tsdbFreeMetaHandle(SMetaHandle *pMetaHandle); // Recover the meta handle from the file diff --git a/src/vnode/tsdb/src/tsdbMain.c b/src/vnode/tsdb/src/tsdbMain.c index 826592bce6..72761e834b 100644 --- a/src/vnode/tsdb/src/tsdbMain.c +++ b/src/vnode/tsdb/src/tsdbMain.c @@ -1,27 +1,23 @@ +#include #include #include #include -#include -#include #include -#include +#include +#include // #include "taosdef.h" // #include "disk.h" -#include "tsdbFile.h" #include "tsdb.h" #include "tsdbCache.h" +#include "tsdbFile.h" #include "tsdbMeta.h" -enum { - TSDB_REPO_STATE_ACTIVE, - TSDB_REPO_STATE_CLOSED, - TSDB_REPO_STATE_CONFIGURING -}; +enum { TSDB_REPO_STATE_ACTIVE, TSDB_REPO_STATE_CLOSED, TSDB_REPO_STATE_CONFIGURING }; typedef struct _tsdb_repo { // TSDB configuration - STsdbCfg *pCfg; + STsdbCfg config; // The meter meta handle of this TSDB repository SMetaHandle *tsdbMeta; @@ -37,31 +33,40 @@ typedef struct _tsdb_repo { pthread_mutex_t tsdbMutex; + // A limiter to monitor the resources used by tsdb + void *limiter; + int8_t state; } STsdbRepo; +static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg); +static int32_t tsdbCreateRepoFiles(STsdbRepo *pRepo); + #define TSDB_GET_TABLE_BY_ID(pRepo, sid) (((STSDBRepo *)pRepo)->pTableList)[sid] #define TSDB_GET_TABLE_BY_NAME(pRepo, name) #define TSDB_IS_REPO_ACTIVE(pRepo) ((pRepo)->state == TSDB_REPO_STATE_ACTIVE) #define TSDB_IS_REPO_CLOSED(pRepo) ((pRepo)->state == TSDB_REPO_STATE_CLOSED) +tsdb_repo_t *tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter) { + + if (rootDir == NULL) return NULL; -tsdb_repo_t *tsdbCreateRepo(STsdbCfg *pCfg) { + if (access(rootDir, F_OK|R_OK|W_OK) == -1) return NULL; - // Check the configuration - if (tsdbCheckCfg(pCfg) < 0) { + if (tsdbCheckAndSetDefaultCfg(pCfg) < 0) { return NULL; } STsdbRepo *pRepo = (STsdbRepo *)malloc(sizeof(STsdbRepo)); if (pRepo == NULL) { - // TODO: deal with error return NULL; } - // TODO: Initailize pMetahandle - pRepo->tsdbMeta = tsdbCreateMetaHandle(pCfg->maxTables); + pRepo->config = *pCfg; + pRepo->limiter = limiter; + + pRepo->tsdbMeta = tsdbCreateMeta(pCfg->maxTables); if (pRepo->tsdbMeta == NULL) { // TODO: deal with error free(pRepo); @@ -77,17 +82,14 @@ tsdb_repo_t *tsdbCreateRepo(STsdbCfg *pCfg) { return NULL; } - // Set configuration - pRepo->pCfg = pCfg; - // Create the Meta data file and data directory - if (tsdbCreateFiles(pRepo) < 0) { + if (tsdbCreateRepoFiles(pRepo) < 0) { // Failed to create and save files tsdbFreeMetaHandle(pRepo->tsdbCache); free(pRepo); return NULL; } - + pRepo->state = TSDB_REPO_STATE_ACTIVE; return (tsdb_repo_t *)pRepo; @@ -110,8 +112,7 @@ int32_t tsdbDropRepo(tsdb_repo_t *repo) { } tsdb_repo_t *tsdbOpenRepo(char *tsdbDir) { - - if (access(tsdbDir, F_OK|W_OK|R_OK) < 0) { + if (access(tsdbDir, F_OK | W_OK | R_OK) < 0) { return NULL; } @@ -159,7 +160,7 @@ int32_t tsdbCloseRepo(tsdb_repo_t *repo) { int32_t tsdbConfigRepo(tsdb_repo_t *repo, STsdbCfg *pCfg) { STsdbRepo *pRepo = (STsdbRepo *)repo; - pRepo->pCfg = pCfg; + pRepo->config = pCfg; // TODO return 0; } @@ -185,18 +186,13 @@ int32_t tsdbInsertData(tsdb_repo_t *pRepo, STableId tid, char *pData, int32_t *e // TODO } -// Check the correctness of the TSDB configuration -static int32_t tsdbCheckCfg(STsdbCfg *pCfg) { - if (pCfg->rootDir == NULL) return -1; - - if (access(pCfg->rootDir, F_OK|R_OK|W_OK) == -1) { - return -1; - } +// Check the configuration and set default options +static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg) { // TODO return 0; } -static int32_t tsdbCreateFiles(STsdbRepo *pRepo) { +static int32_t tsdbCreateRepoFiles(STsdbRepo *pRepo) { // TODO } diff --git a/src/vnode/tsdb/src/tsdbMeta.c b/src/vnode/tsdb/src/tsdbMeta.c index a302498a24..f0abc2cd3c 100644 --- a/src/vnode/tsdb/src/tsdbMeta.c +++ b/src/vnode/tsdb/src/tsdbMeta.c @@ -4,39 +4,39 @@ #include "tsdb.h" #include "tsdbMeta.h" -SMetaHandle *tsdbCreateMetaHandle(int32_t numOfTables) { - SMetaHandle *pMetahandle = (SMetaHandle *)malloc(sizeof(SMetaHandle)); - if (pMetahandle == NULL) { +STsdbMeta *tsdbCreateMeta(int32_t maxNumOfTables) { + STsdbMeta *pMeta = (STsdbMeta *)malloc(sizeof(STsdbMeta)); + if (pMeta == NULL) { return NULL; } - pMetahandle->numOfTables = 0; - pMetahandle->numOfSuperTables = 0; - pMetahandle->pTables = calloc(sizeof(STable *), numOfTables); - if (pMetahandle->pTables == NULL) { - free(pMetahandle); + pMeta->numOfTables = 0; + pMeta->numOfSuperTables = 0; + pMeta->pTables = calloc(sizeof(STable *), numOfTables); + if (pMeta->pTables == NULL) { + free(pMeta); return NULL; } // TODO : initialize the map // pMetahandle->pNameTableMap = ; - if (pMetahandle->pNameTableMap == NULL) { - free(pMetahandle->pTables); - free(pMetahandle); + if (pMeta->pNameTableMap == NULL) { + free(pMeta->pTables); + free(pMeta); return NULL; } - return pMetahandle; + return pMeta; } -int32_t tsdbFreeMetaHandle(SMetaHandle *pMetaHandle) { +int32_t tsdbFreeMetaHandle(STsdbMeta *pMetaHandle) { // TODO } static int32_t tsdbCheckTableCfg(STableCfg *pCfg) { return 0; } -int32_t tsdbCreateTableImpl(SMetaHandle *pHandle, STableCfg *pCfg) { +int32_t tsdbCreateTableImpl(STsdbMeta *pHandle, STableCfg *pCfg) { if (tsdbCheckTableCfg(pCfg) < 0) { return -1; } @@ -53,10 +53,10 @@ int32_t tsdbCreateTableImpl(SMetaHandle *pHandle, STableCfg *pCfg) { return 0; } -SMetaHandle * tsdbOpenMetaHandle(char *tsdbDir) { +STsdbMeta * tsdbOpenMetaHandle(char *tsdbDir) { // Open meta file for reading - SMetaHandle *pHandle = (SMetaHandle *)malloc(sizeof(SMetaHandle)); + STsdbMeta *pHandle = (STsdbMeta *)malloc(sizeof(STsdbMeta)); if (pHandle == NULL) { return NULL; } -- GitLab