refact
This commit is contained in:
parent
65e388a584
commit
2013f85ae4
|
@ -22,6 +22,11 @@
|
|||
extern "C" {
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
int level;
|
||||
int id;
|
||||
} SDiskID;
|
||||
|
||||
// tfs.c
|
||||
int tfsInit(SDiskCfg *pDiskCfg, int ndisk);
|
||||
void tfsDestroy();
|
||||
|
@ -30,6 +35,11 @@ void tfsPrimaryPath(char *dst);
|
|||
int tfsCreateDir(char *dirname);
|
||||
int tfsRemoveDir(char *dirname);
|
||||
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
|
||||
typedef struct TFSFILE TFSFILE;
|
||||
|
@ -47,6 +57,11 @@ void tfsBaseName(TFSFILE *pfile, char dest[]);
|
|||
int tfsopen(TFSFILE *pfile);
|
||||
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);
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
|
|
@ -67,7 +67,7 @@ const TFSFILE *tfsReadDir(TFSDIR *tdir) {
|
|||
struct dirent *dp = readdir(tdir->dir);
|
||||
if (dp != NULL) {
|
||||
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);
|
||||
}
|
||||
|
@ -125,6 +125,47 @@ int tfsclose(int fd) {
|
|||
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) {
|
||||
char dirName[TSDB_FILENAME_LEN] = "\0";
|
||||
|
||||
|
@ -154,9 +195,20 @@ static int tfsOpenDirImpl(TFSDIR *tdir) {
|
|||
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->id = id;
|
||||
strncpy(pfile->rname, rname, TSDB_FILENAME_LEN);
|
||||
snprintf(pfile->aname, TSDB_FILENAME_LEN, "%s/%s", tfsGetDiskName(level, id), rname);
|
||||
}
|
||||
}
|
||||
|
||||
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 {
|
|||
|
||||
typedef struct {
|
||||
pthread_mutex_t lock;
|
||||
bool locked;
|
||||
SFSMeta meta;
|
||||
int nlevel;
|
||||
STier tiers[TSDB_MAX_TIER];
|
||||
|
@ -171,6 +172,44 @@ int tfsRename(char *oldpath, char *newpath) {
|
|||
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) {
|
||||
return DISK_AT(level, id)->dir;
|
||||
}
|
||||
|
@ -293,26 +332,6 @@ static int tfsCheck() {
|
|||
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) {
|
||||
}
|
||||
|
||||
|
|
|
@ -190,15 +190,13 @@ typedef struct {
|
|||
|
||||
typedef struct {
|
||||
int fd;
|
||||
TFSFILE tfile;
|
||||
TFSFILE file;
|
||||
STsdbFileInfo info;
|
||||
} SFile;
|
||||
|
||||
typedef struct {
|
||||
int fileId;
|
||||
int state; // 0 for health, 1 for problem
|
||||
int level;
|
||||
int did;
|
||||
SFile files[TSDB_FILE_TYPE_MAX];
|
||||
} SFileGroup;
|
||||
|
||||
|
|
|
@ -365,13 +365,8 @@ void *tsdbDecodeSFileInfo(void *buf, STsdbFileInfo *pInfo) {
|
|||
void tsdbRemoveFileGroup(STsdbRepo *pRepo, SFileGroup *pFGroup) {
|
||||
ASSERT(pFGroup != NULL);
|
||||
STsdbFileH *pFileH = pRepo->tsdbFileH;
|
||||
SDisk * pDisk = NULL;
|
||||
char baseDir[TSDB_FILENAME_LEN] = "\0";
|
||||
|
||||
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);
|
||||
if (nFilesLeft > 0) {
|
||||
|
@ -381,14 +376,8 @@ void tsdbRemoveFileGroup(STsdbRepo *pRepo, SFileGroup *pFGroup) {
|
|||
pFileH->nFGroups--;
|
||||
ASSERT(pFileH->nFGroups >= 0);
|
||||
|
||||
for (int type = 0; type < TSDB_FILE_TYPE_MAX; type++) {
|
||||
if (remove(fileGroup.files[type].fname) < 0) {
|
||||
tsdbError("vgId:%d failed to remove file %s", REPO_ID(pRepo), fileGroup.files[type].fname);
|
||||
}
|
||||
tsdbDestroyFile(&fileGroup.files[type]);
|
||||
}
|
||||
|
||||
tdDecDiskFiles(tsDnodeTier, pDisk, true);
|
||||
tfsRemoveFiles(TSDB_FILE_TYPE_MAX, &fileGroup.files[TSDB_FILE_TYPE_HEAD], &fileGroup.files[TSDB_FILE_TYPE_DATA],
|
||||
&fileGroup.files[TSDB_FILE_TYPE_LAST]);
|
||||
}
|
||||
|
||||
int tsdbLoadFileHeader(SFile *pFile, uint32_t *version) {
|
||||
|
|
Loading…
Reference in New Issue