diff --git a/src/inc/tfs.h b/src/inc/tfs.h index 796d1563e0e71467e44fac7d2321077af2cae1ed..23ca7ed6acd3288f545a2e4b4afc73a942d8e5f6 100644 --- a/src/inc/tfs.h +++ b/src/inc/tfs.h @@ -74,6 +74,7 @@ void tfsbasename(const TFILE *pf, char *dest); void tfsdirname(const TFILE *pf, char *dest); // DIR APIs ==================================== +int tfsMkdirAt(const char *rname, int level, int id); int tfsMkdir(const char *rname); int tfsRmdir(const char *rname); int tfsRename(char *orname, char *nrname); diff --git a/src/tfs/src/tfs.c b/src/tfs/src/tfs.c index c7314cf2eda356094bea484bede3fde8c76c8281..207d28a4d790ba8217438c50acbe5851a5216b2b 100644 --- a/src/tfs/src/tfs.c +++ b/src/tfs/src/tfs.c @@ -237,18 +237,24 @@ void tfsdirname(const TFILE *pf, char *dest) { } // DIR APIs ==================================== -int tfsMkdir(const char *rname) { - char aname[TSDB_FILENAME_LEN] = "\0"; +int tfsMkdirAt(const char *rname, int level, int id) { + SDisk *pDisk = TFS_DISK_AT(level, id); + char aname[TSDB_FILENAME_LEN]; + + snprintf(aname, TSDB_FILENAME_LEN, "%s/%s", DISK_DIR(pDisk), rname); + if (taosMkDir(aname, 0755) != 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } + return 0; +} + +int tfsMkdir(const char *rname) { for (int level = 0; level < TFS_NLEVEL(); level++) { STier *pTier = TFS_TIER_AT(level); for (int id = 0; id < TIER_NDISKS(pTier); id++) { - SDisk *pDisk = DISK_AT_TIER(pTier, id); - snprintf(aname, TSDB_FILENAME_LEN, "%s/%s", DISK_DIR(pDisk), rname); - - if (mkdir(aname, 0755) != 0 && errno != EEXIST) { - fError("failed to create directory %s since %s", aname, strerror(errno)); - terrno = TAOS_SYSTEM_ERROR(errno); + if (tfsMkdirAt(rname, level, id) < 0) { return -1; } } diff --git a/src/tsdb/src/tsdbFS.c b/src/tsdb/src/tsdbFS.c index acc1fd200afe52f45d04aca44df181e7b3ede2ad..51c5a92edd2a33a8b6f2e56c02ce8ae53740068a 100644 --- a/src/tsdb/src/tsdbFS.c +++ b/src/tsdb/src/tsdbFS.c @@ -29,6 +29,7 @@ static int tsdbScanAndTryFixFS(STsdbRepo *pRepo); static int tsdbScanRootDir(STsdbRepo *pRepo); static int tsdbScanDataDir(STsdbRepo *pRepo); static bool tsdbIsTFileInFS(STsdbFS *pfs, const TFILE *pf); +static int tsdbRestoreCurrent(STsdbRepo *pRepo); // ================== CURRENT file header info static int tsdbEncodeFSHeader(void **buf, SFSHeader *pHeader) { @@ -232,7 +233,6 @@ void *tsdbFreeFS(STsdbFS *pfs) { return NULL; } -// TODO int tsdbOpenFS(STsdbRepo *pRepo) { STsdbFS * pfs = REPO_FS(pRepo); char current[TSDB_FILENAME_LEN] = "\0"; @@ -247,7 +247,10 @@ int tsdbOpenFS(STsdbRepo *pRepo) { return -1; } } else { - // TODO: current file not exists, try to recover it + if (tsdbRestoreCurrent(pRepo) < 0) { + tsdbError("vgId:%d failed to restore current file since %s", REPO_ID(pRepo), tstrerror(terrno)); + return -1; + } } // Load meta cache if has meta file @@ -259,7 +262,6 @@ int tsdbOpenFS(STsdbRepo *pRepo) { return 0; } -// TODO void tsdbCloseFS(STsdbRepo *pRepo) { // TODO } @@ -896,4 +898,44 @@ static bool tsdbIsTFileInFS(STsdbFS *pfs, const TFILE *pf) { } return false; +} + +static int tsdbRestoreCurrent(STsdbRepo *pRepo) { + char rootDir[TSDB_FILENAME_LEN]; + char dataDir[TSDB_FILENAME_LEN]; + TDIR * tdir = NULL; + const TFILE *pf = NULL; + char bname[TSDB_FILENAME_LEN]; + + // Loop to recover mfile + tsdbGetRootDir(REPO_ID(pRepo), rootDir); + tdir = tfsOpendir(rootDir); + if (tdir == NULL) { + tsdbError("vgId:%d failed to open dir %s since %s", REPO_ID(pRepo), rootDir, tstrerror(terrno)); + return -1; + } + + while ((pf = tfsReaddir(tdir))) { + tfsbasename(pf, bname); + if (strncmp(bname, "meta", sizeof("meta")) == 0) { + // TODO + break; + } + } + + tfsClosedir(tdir); + + // Loop to recover dfile set + tsdbGetDataDir(REPO_ID(pRepo), dataDir); + tdir = tfsOpendir(dataDir); + if (tdir == NULL) { + tsdbError("vgId:%d failed to open dir %s since %s", REPO_ID(pRepo), rootDir, tstrerror(terrno)); + return -1; + } + + // TODO + + tfsClosedir(tdir); + + return 0; } \ No newline at end of file diff --git a/src/tsdb/src/tsdbFile.c b/src/tsdb/src/tsdbFile.c index 09d212e377463a4ea7626ee1c41907b7ee825529..01d264b59c0b1ecd1d54d67a6ad5eec4d3addad8 100644 --- a/src/tsdb/src/tsdbFile.c +++ b/src/tsdb/src/tsdbFile.c @@ -308,6 +308,7 @@ int tsdbCreateDFile(SDFile *pDFile, bool updateHeader) { ASSERT(pDFile->info.size == 0 && pDFile->info.magic == TSDB_FILE_INIT_MAGIC); char buf[TSDB_FILE_HEAD_SIZE] = "\0"; + // TODO: need to check if directory exists, if not, create the directory pDFile->fd = open(TSDB_FILE_FULL_NAME(pDFile), O_WRONLY | O_CREAT | O_TRUNC, 0755); if (pDFile->fd < 0) { diff --git a/src/tsdb/src/tsdbSync.c b/src/tsdb/src/tsdbSync.c index 7195a3b81880f3a7fc12b30c25c371875a066a70..b40a667d79eaa220bf6973bcf269b95a0c86b702 100644 --- a/src/tsdb/src/tsdbSync.c +++ b/src/tsdb/src/tsdbSync.c @@ -655,6 +655,7 @@ static int32_t tsdbRecvDFileSetInfo(SSyncH *pSynch) { } static int tsdbReload(STsdbRepo *pRepo, bool isMfChanged) { + // TODO: may need to stop and restart stream if (isMfChanged) { tsdbCloseMeta(pRepo); tsdbFreeMeta(pRepo->tsdbMeta);