提交 2013f85a 编写于 作者: H Hongze Cheng

refact

上级 65e388a5
...@@ -22,6 +22,11 @@ ...@@ -22,6 +22,11 @@
extern "C" { extern "C" {
#endif #endif
typedef struct {
int level;
int id;
} SDiskID;
// tfs.c // tfs.c
int tfsInit(SDiskCfg *pDiskCfg, int ndisk); int tfsInit(SDiskCfg *pDiskCfg, int ndisk);
void tfsDestroy(); void tfsDestroy();
...@@ -30,6 +35,11 @@ void tfsPrimaryPath(char *dst); ...@@ -30,6 +35,11 @@ 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;
...@@ -47,6 +57,11 @@ void tfsBaseName(TFSFILE *pfile, char dest[]); ...@@ -47,6 +57,11 @@ void tfsBaseName(TFSFILE *pfile, char dest[]);
int tfsopen(TFSFILE *pfile); int tfsopen(TFSFILE *pfile);
int tfsclose(int, fd); int tfsclose(int, fd);
TFSFILE *tfsCreateFiles(int level, int nfile, ...);
int tfsRemoveFiles(int nfile, ...);
SDiskID tfsFileID(TFSFILE *pfile);
const char *tfsGetDiskName(int level, int id); const char *tfsGetDiskName(int level, int id);
#ifdef __cplusplus #ifdef __cplusplus
......
...@@ -67,7 +67,7 @@ const TFSFILE *tfsReadDir(TFSDIR *tdir) { ...@@ -67,7 +67,7 @@ const TFSFILE *tfsReadDir(TFSDIR *tdir) {
struct dirent *dp = readdir(tdir->dir); struct dirent *dp = readdir(tdir->dir);
if (dp != NULL) { if (dp != NULL) {
snprintf(rname, TSDB_FILENAME_LEN, "%s/%s", tdir->name, dp->d_name); snprintf(rname, TSDB_FILENAME_LEN, "%s/%s", tdir->name, dp->d_name);
tsfInitFile(&(tdir->tfsfile), tdir->level, tdir->id, rname); tfsInitFile(&(tdir->tfsfile), tdir->level, tdir->id, rname);
return &(tdir->tfsfile); return &(tdir->tfsfile);
} }
...@@ -125,6 +125,47 @@ int tfsclose(int fd) { ...@@ -125,6 +125,47 @@ int tfsclose(int fd) {
return 0 return 0
} }
TFSFILE *tfsCreateFiles(int level, int nfile, ...) {
// TODO
return NULL;
}
int tfsRemoveFiles(int nfile, ...) {
va_list valist;
TFSFILE *pfile = NULL;
int code = 0;
va_start(valist, nfile);
tfsLock();
for (int i = 0; i < nfile; i++) {
pfile = va_arg(valist, TFSFILE *);
code = remove(pfile->aname);
if (code != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
tfsUnLock();
va_end(valist);
return -1;
}
tfsDecFileAt(pfile->level, pfile->id);
}
tfsUnLock();
va_end(valist);
return 0;
}
SDiskID tfsFileID(TFSFILE *pfile) {
SDiskID did;
did.level = pfile->level;
did.id = pfile->id;
return did;
}
static int tfsOpenDirImpl(TFSDIR *tdir) { static int tfsOpenDirImpl(TFSDIR *tdir) {
char dirName[TSDB_FILENAME_LEN] = "\0"; char dirName[TSDB_FILENAME_LEN] = "\0";
...@@ -154,9 +195,20 @@ static int tfsOpenDirImpl(TFSDIR *tdir) { ...@@ -154,9 +195,20 @@ static int tfsOpenDirImpl(TFSDIR *tdir) {
return 0; return 0;
} }
static void tsfInitFile(TFSFILE *pfile, int level, int id, char *rname) { static void tfsInitFile(TFSFILE *pfile, int level, int id, char *rname) {
pfile->level = level; pfile->level = level;
pfile->id = id; pfile->id = id;
strncpy(pfile->rname, rname, TSDB_FILENAME_LEN); strncpy(pfile->rname, rname, TSDB_FILENAME_LEN);
snprintf(pfile->aname, TSDB_FILENAME_LEN, "%s/%s", tfsGetDiskName(level, id), rname); snprintf(pfile->aname, TSDB_FILENAME_LEN, "%s/%s", tfsGetDiskName(level, id), rname);
} }
\ No newline at end of file
static TFSFILE *tfsNewFile(int level, int id, char *rname) {
TFSFILE *pfile = (TFSFILE *)calloc(1, sizeof(*pfile));
if (pfile == NULL) {
terrno = TSDB_CODE_FS_OUT_OF_MEMORY;
return NULL;
}
tfsInitFile(pfile, level, id, rname);
return pfile;
}
...@@ -35,6 +35,7 @@ typedef struct { ...@@ -35,6 +35,7 @@ typedef struct {
typedef struct { typedef struct {
pthread_mutex_t lock; pthread_mutex_t lock;
bool locked;
SFSMeta meta; SFSMeta meta;
int nlevel; int nlevel;
STier tiers[TSDB_MAX_TIER]; STier tiers[TSDB_MAX_TIER];
...@@ -171,6 +172,44 @@ int tfsRename(char *oldpath, char *newpath) { ...@@ -171,6 +172,44 @@ int tfsRename(char *oldpath, char *newpath) {
return 0; return 0;
} }
void tfsIncFileAt(int level, int id) {
ASSERT(tfsIsLocked());
DISK_AT(level, id)->dmeta.nfiles++;
ASSERT(DISK_AT(level, id)->dmeta.nfiles > 0);
}
void tfsDecFileAt(int level, int id) {
ASSERT(tfsIsLocked());
DISK_AT(level, id)->dmeta.nfiles--;
ASSERT(DISK_AT(level, id)->dmeta.nfiles >= 0);
}
int tfsLock() {
int code = pthread_mutex_lock(&(pfs->lock));
if (code != 0) {
terrno = TAOS_SYSTEM_ERROR(code);
return -1;
}
pfs->locked = true;
return 0;
}
int tfsUnLock() {
pfs->locked = false;
int code = pthread_mutex_unlock(&(pfs->lock));
if (code != 0) {
terrno = TAOS_SYSTEM_ERROR(code);
return -1;
}
return 0;
}
bool tfsIsLocked() { return pfs->locked; }
const char *tfsGetDiskName(int level, int id) { const char *tfsGetDiskName(int level, int id) {
return DISK_AT(level, id)->dir; return DISK_AT(level, id)->dir;
} }
...@@ -293,26 +332,6 @@ static int tfsCheck() { ...@@ -293,26 +332,6 @@ static int tfsCheck() {
return 0; return 0;
} }
static int tfsLock() {
int code = pthread_mutex_lock(&(pfs->lock));
if (code != 0) {
terrno = TAOS_SYSTEM_ERROR(code);
return -1;
}
return 0;
}
static tfsUnLock() {
int code = pthread_mutex_unlock(&(pfs->lock));
if (code != 0) {
terrno = TAOS_SYSTEM_ERROR(code);
return -1;
}
return 0;
}
static tfsGetDiskByName(char *dirName) { static tfsGetDiskByName(char *dirName) {
} }
......
...@@ -190,15 +190,13 @@ typedef struct { ...@@ -190,15 +190,13 @@ typedef struct {
typedef struct { typedef struct {
int fd; int fd;
TFSFILE tfile; TFSFILE file;
STsdbFileInfo info; STsdbFileInfo info;
} SFile; } SFile;
typedef struct { typedef struct {
int fileId; int fileId;
int state; // 0 for health, 1 for problem int state; // 0 for health, 1 for problem
int level;
int did;
SFile files[TSDB_FILE_TYPE_MAX]; SFile files[TSDB_FILE_TYPE_MAX];
} SFileGroup; } SFileGroup;
......
...@@ -365,13 +365,8 @@ void *tsdbDecodeSFileInfo(void *buf, STsdbFileInfo *pInfo) { ...@@ -365,13 +365,8 @@ void *tsdbDecodeSFileInfo(void *buf, STsdbFileInfo *pInfo) {
void tsdbRemoveFileGroup(STsdbRepo *pRepo, SFileGroup *pFGroup) { void tsdbRemoveFileGroup(STsdbRepo *pRepo, SFileGroup *pFGroup) {
ASSERT(pFGroup != NULL); ASSERT(pFGroup != NULL);
STsdbFileH *pFileH = pRepo->tsdbFileH; STsdbFileH *pFileH = pRepo->tsdbFileH;
SDisk * pDisk = NULL;
char baseDir[TSDB_FILENAME_LEN] = "\0";
SFileGroup fileGroup = *pFGroup; SFileGroup fileGroup = *pFGroup;
tsdbGetBaseDirFromFile(fileGroup.files[0].fname, baseDir);
pDisk = tdGetDiskByName(tsDnodeTier, baseDir);
ASSERT(pDisk != NULL);
int nFilesLeft = pFileH->nFGroups - (int)(POINTER_DISTANCE(pFGroup, pFileH->pFGroup) / sizeof(SFileGroup) + 1); int nFilesLeft = pFileH->nFGroups - (int)(POINTER_DISTANCE(pFGroup, pFileH->pFGroup) / sizeof(SFileGroup) + 1);
if (nFilesLeft > 0) { if (nFilesLeft > 0) {
...@@ -381,14 +376,8 @@ void tsdbRemoveFileGroup(STsdbRepo *pRepo, SFileGroup *pFGroup) { ...@@ -381,14 +376,8 @@ void tsdbRemoveFileGroup(STsdbRepo *pRepo, SFileGroup *pFGroup) {
pFileH->nFGroups--; pFileH->nFGroups--;
ASSERT(pFileH->nFGroups >= 0); ASSERT(pFileH->nFGroups >= 0);
for (int type = 0; type < TSDB_FILE_TYPE_MAX; type++) { tfsRemoveFiles(TSDB_FILE_TYPE_MAX, &fileGroup.files[TSDB_FILE_TYPE_HEAD], &fileGroup.files[TSDB_FILE_TYPE_DATA],
if (remove(fileGroup.files[type].fname) < 0) { &fileGroup.files[TSDB_FILE_TYPE_LAST]);
tsdbError("vgId:%d failed to remove file %s", REPO_ID(pRepo), fileGroup.files[type].fname);
}
tsdbDestroyFile(&fileGroup.files[type]);
}
tdDecDiskFiles(tsDnodeTier, pDisk, true);
} }
int tsdbLoadFileHeader(SFile *pFile, uint32_t *version) { int tsdbLoadFileHeader(SFile *pFile, uint32_t *version) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册