提交 5a339bb1 编写于 作者: H Hongze Cheng

refact

上级 43063fd8
...@@ -35,11 +35,6 @@ void tfsPrimaryPath(char *dst); ...@@ -35,11 +35,6 @@ void tfsPrimaryPath(char *dst);
int tfsCreateDir(char *dirname); int tfsCreateDir(char *dirname);
int tfsRemoveDir(char *dirname); int tfsRemoveDir(char *dirname);
int tfsRename(char *oldpath, char *newpath); int tfsRename(char *oldpath, char *newpath);
void tfsIncFileAt(int level, int id);
void tfsDecFileAt(int level, int id);
int tfsLock();
int tfsUnLock();
bool tfsIsLocked();
// tfcntl.c // tfcntl.c
typedef struct TFSFILE TFSFILE; typedef struct TFSFILE TFSFILE;
......
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TD_TDISK_H
#define TD_TDISK_H
#include "tfslog.h"
#ifdef __cplusplus
extern "C" {
#endif
typedef struct {
uint64_t size;
uint64_t free;
uint64_t nfiles;
} SDiskMeta;
typedef struct {
int level;
int id;
char dir[TSDB_FILENAME_LEN];
SDiskMeta dmeta;
} SDisk;
SDisk *tdNewDisk(int level, int id, char *dir);
void tdFreeDisk(SDisk *pDisk);
int tdUpdateDiskInfo(SDisk *pDisk);
#ifdef __cplusplus
}
#endif
#endif
\ No newline at end of file
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TD_TFCNTL_H
#define TD_TFCNTL_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif
\ No newline at end of file
...@@ -13,13 +13,14 @@ ...@@ -13,13 +13,14 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#ifndef TD_TFSLOG_H #ifndef TD_TFSINT_H
#define TD_TFSLOG_H #define TD_TFSINT_H
#ifdef __cplusplus #ifdef __cplusplus
extern "C" { extern "C" {
#endif #endif
// For debug purpose
#define fFatal(...) { if (fsDebugFlag & DEBUG_FATAL) { taosPrintLog("TFS FATAL ", 255, __VA_ARGS__); }} #define fFatal(...) { if (fsDebugFlag & DEBUG_FATAL) { taosPrintLog("TFS FATAL ", 255, __VA_ARGS__); }}
#define fError(...) { if (fsDebugFlag & DEBUG_ERROR) { taosPrintLog("TFS ERROR ", 255, __VA_ARGS__); }} #define fError(...) { if (fsDebugFlag & DEBUG_ERROR) { taosPrintLog("TFS ERROR ", 255, __VA_ARGS__); }}
#define fWarn(...) { if (fsDebugFlag & DEBUG_WARN) { taosPrintLog("TFS WARN ", 255, __VA_ARGS__); }} #define fWarn(...) { if (fsDebugFlag & DEBUG_WARN) { taosPrintLog("TFS WARN ", 255, __VA_ARGS__); }}
...@@ -27,6 +28,32 @@ extern "C" { ...@@ -27,6 +28,32 @@ extern "C" {
#define fDebug(...) { if (fsDebugFlag & DEBUG_DEBUG) { taosPrintLog("TFS ", cqDebugFlag, __VA_ARGS__); }} #define fDebug(...) { if (fsDebugFlag & DEBUG_DEBUG) { taosPrintLog("TFS ", cqDebugFlag, __VA_ARGS__); }}
#define fTrace(...) { if (fsDebugFlag & DEBUG_TRACE) { taosPrintLog("TFS ", cqDebugFlag, __VA_ARGS__); }} #define fTrace(...) { if (fsDebugFlag & DEBUG_TRACE) { taosPrintLog("TFS ", cqDebugFlag, __VA_ARGS__); }}
// tdisk.c
typedef struct SDisk SDisk;
SDisk *tfsNewDisk(int level, int id, char *dir);
void tfsFreeDisk(SDisk *pDisk);
int tfsUpdateDiskInfo(SDisk *pDisk);
// ttier.c
typedef struct STier STier;
#define DISK_AT_TIER(pTier, id) ((pTier)->disks[id])
void tfsInitTier(STier *pTier, int level);
void tfsDestroyTier(STier *pTier);
SDisk *tfsMountDiskToTier(STier *pTier, SDiskCfg *pCfg);
int tfsUpdateTierInfo(STier *pTier);
// tfs.c
void tfsIncFileAt(int level, int id);
void tfsDecFileAt(int level, int id);
int tfsLock();
int tfsUnLock();
bool tfsIsLocked();
// tfcntl.c
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif
......
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TD_TFCNTL_H
#define TD_TFCNTL_H
#ifdef __cplusplus
extern "C" {
#endif
#ifdef __cplusplus
}
#endif
#endif
\ No newline at end of file
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef TD_TTIER_H
#define TD_TTIER_H
#include "tdisk.h"
#ifdef __cplusplus
extern "C" {
#endif
#define TSDB_MAX_DISK_PER_TIER 16
typedef struct {
int level;
int ndisk;
SDisk *disks[TSDB_MAX_DISK_PER_TIER];
} STier;
#define DISK_AT_TIER(pTier, id) ((pTier)->disks[id])
void tdInitTier(STier *pTier, int level);
void tdDestroyTier(STier *pTier);
SDisk *tdMountToTier(STier *pTier, SDiskCfg *pCfg);
int tdUpdateTierInfo(STier *pTier);
#ifdef __cplusplus
}
#endif
#endif
\ No newline at end of file
...@@ -12,11 +12,26 @@ ...@@ -12,11 +12,26 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "os.h"
#include "tdisk.h"
#include "taoserror.h" #include "taoserror.h"
#include "tfsint.h"
SDisk *tdNewDisk(int level, int id, char *dir) { typedef struct {
uint64_t size;
uint64_t free;
uint64_t nfiles;
} SDiskMeta;
struct SDisk {
int level;
int id;
char dir[TSDB_FILENAME_LEN];
SDiskMeta dmeta;
};
// PROTECTED ====================================
SDisk *tfsNewDisk(int level, int id, char *dir) {
SDisk *pDisk = (SDisk *)calloc(1, sizeof(*pDisk)); SDisk *pDisk = (SDisk *)calloc(1, sizeof(*pDisk));
if (pDisk == NULL) { if (pDisk == NULL) {
terrno = TSDB_CODE_FS_OUT_OF_MEMORY; terrno = TSDB_CODE_FS_OUT_OF_MEMORY;
...@@ -30,13 +45,13 @@ SDisk *tdNewDisk(int level, int id, char *dir) { ...@@ -30,13 +45,13 @@ SDisk *tdNewDisk(int level, int id, char *dir) {
return pDisk; return pDisk;
} }
void tdFreeDisk(SDisk *pDisk) { void tfsFreeDisk(SDisk *pDisk) {
if (pDisk) { if (pDisk) {
free(pDisk) free(pDisk)
} }
} }
int tdUpdateDiskInfo(SDisk *pDisk) { int tfsUpdateDiskInfo(SDisk *pDisk) {
SysDiskSize dstat; SysDiskSize dstat;
if (taosGetDiskSize(pDisk->dir, &dstat) < 0) { if (taosGetDiskSize(pDisk->dir, &dstat) < 0) {
fError("failed to get dir %s information since %s", pDisk->dir, strerror(errno)); fError("failed to get dir %s information since %s", pDisk->dir, strerror(errno));
......
...@@ -33,6 +33,7 @@ struct TFSDIR { ...@@ -33,6 +33,7 @@ struct TFSDIR {
DIR * dir; DIR * dir;
}; };
// PUBLIC ==========================================
TFSDIR *tfsOpenDir(char *dir) { TFSDIR *tfsOpenDir(char *dir) {
TFSDIR *tdir = (TFSDIR *)calloc(1, sizeof(*tdir)); TFSDIR *tdir = (TFSDIR *)calloc(1, sizeof(*tdir));
if (tdir == NULL) { if (tdir == NULL) {
...@@ -166,6 +167,7 @@ SDiskID tfsFileID(TFSFILE *pfile) { ...@@ -166,6 +167,7 @@ SDiskID tfsFileID(TFSFILE *pfile) {
return did; return did;
} }
// PRIVATE =============================================
static int tfsOpenDirImpl(TFSDIR *tdir) { static int tfsOpenDirImpl(TFSDIR *tdir) {
char dirName[TSDB_FILENAME_LEN] = "\0"; char dirName[TSDB_FILENAME_LEN] = "\0";
......
...@@ -17,17 +17,10 @@ ...@@ -17,17 +17,10 @@
#include "hash.h" #include "hash.h"
#include "taoserror.h" #include "taoserror.h"
#include "tfs.h" #include "tfsint.h"
#include "tglobal.h"
#include "ttier.h"
#define TSDB_MAX_TIER 3 #define TSDB_MAX_TIER 3
typedef struct {
int level;
int id;
} SDiskID;
typedef struct { typedef struct {
uint64_t tsize; uint64_t tsize;
uint64_t avail; uint64_t avail;
...@@ -48,6 +41,7 @@ static SFS *pfs = &tdFileSystem; ...@@ -48,6 +41,7 @@ static SFS *pfs = &tdFileSystem;
#define TIER_AT(level) (pfs->tiers + (level)) #define TIER_AT(level) (pfs->tiers + (level))
#define DISK_AT(level, id) DISK_AT_TIER(TIER_AT(level), id) #define DISK_AT(level, id) DISK_AT_TIER(TIER_AT(level), id)
// public:
int tfsInit(SDiskCfg *pDiskCfg, int ndisk) { int tfsInit(SDiskCfg *pDiskCfg, int ndisk) {
ASSERT(ndisk > 0); ASSERT(ndisk > 0);
...@@ -172,6 +166,7 @@ int tfsRename(char *oldpath, char *newpath) { ...@@ -172,6 +166,7 @@ int tfsRename(char *oldpath, char *newpath) {
return 0; return 0;
} }
// protected:
void tfsIncFileAt(int level, int id) { void tfsIncFileAt(int level, int id) {
ASSERT(tfsIsLocked()); ASSERT(tfsIsLocked());
DISK_AT(level, id)->dmeta.nfiles++; DISK_AT(level, id)->dmeta.nfiles++;
...@@ -214,6 +209,7 @@ const char *tfsGetDiskName(int level, int id) { ...@@ -214,6 +209,7 @@ const char *tfsGetDiskName(int level, int id) {
return DISK_AT(level, id)->dir; return DISK_AT(level, id)->dir;
} }
// private
static int tfsMount(SDiskCfg *pCfg) { static int tfsMount(SDiskCfg *pCfg) {
SDiskID did; SDiskID did;
......
...@@ -12,24 +12,32 @@ ...@@ -12,24 +12,32 @@
* You should have received a copy of the GNU Affero General Public License * You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>. * along with this program. If not, see <http://www.gnu.org/licenses/>.
*/ */
#include "os.h"
#include "ttier.h" #include "tfsint.h"
#include "tglobal.h"
#include "taoserror.h" #include "taoserror.h"
void tdInitTier(STier *pTier, int level) { #define TSDB_MAX_DISK_PER_TIER 16
struct STier {
int level;
int ndisk;
SDisk *disks[TSDB_MAX_DISK_PER_TIER];
};
// PROTECTED ==========================================
void tfsInitTier(STier *pTier, int level) {
pTier->level = level; pTier->level = level;
} }
void tdDestroyTier(STier *pTier) { void tfsDestroyTier(STier *pTier) {
for (int id = 0; id < TSDB_MAX_DISK_PER_TIER; id++) { for (int id = 0; id < TSDB_MAX_DISK_PER_TIER; id++) {
tdFreeDisk(DISK_AT_TIER(pTier, id)); tfsFreeDisk(DISK_AT_TIER(pTier, id));
pTier->disks[id] = NULL; pTier->disks[id] = NULL;
} }
pTier->ndisk = 0; pTier->ndisk = 0;
} }
SDisk *tdMountToTier(STier *pTier, SDiskCfg *pCfg) { SDisk *tfsMountDiskToTier(STier *pTier, SDiskCfg *pCfg) {
ASSERT(pTier->level == pCfg->level); ASSERT(pTier->level == pCfg->level);
int id = 0; int id = 0;
...@@ -52,19 +60,20 @@ SDisk *tdMountToTier(STier *pTier, SDiskCfg *pCfg) { ...@@ -52,19 +60,20 @@ SDisk *tdMountToTier(STier *pTier, SDiskCfg *pCfg) {
id = pTier->ndisk; id = pTier->ndisk;
} }
pTier->disks[id] = tdNewDisk(pCfg->level, id, pCfg->dir); DISK_AT_TIER(pTier, id) = tfsNewDisk(pCfg->level, id, pCfg->dir);
if (pTier->disks[id] == NULL) return -1; if (DISK_AT_TIER(pTier, id) == NULL) return -1;
pTier->ndisk++; pTier->ndisk++;
fDebug("disk %s is mounted at level %d id %d", pCfg->dir, pCfg->level, id); fDebug("disk %s is mounted to level %d id %d", pCfg->dir, pCfg->level, id);
return id; return id;
} }
int tdUpdateTierInfo(STier *pTier) { int tfsUpdateTierInfo(STier *pTier) {
for (int id = 0; id < pTier->ndisk; id++) { for (int id = 0; id < pTier->ndisk; id++) {
if (tdUpdateDiskInfo(DISK_AT_TIER(pTier, id)) < 0) { if (tfsUpdateDiskInfo(DISK_AT_TIER(pTier, id)) < 0) {
// TODO: deal with the error here return -1;
} }
} }
return 0;
} }
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册