refactor
This commit is contained in:
parent
fde6ef66d2
commit
e329a159b7
|
@ -22,16 +22,7 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
typedef struct {
|
// tfs.c
|
||||||
int level;
|
|
||||||
int id;
|
|
||||||
} SDiskID;
|
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
SDiskID did;
|
|
||||||
char fname[TSDB_FILENAME_LEN];
|
|
||||||
} STfsFile;
|
|
||||||
|
|
||||||
int tfsInit(SDiskCfg *pDiskCfg, int ndisk);
|
int tfsInit(SDiskCfg *pDiskCfg, int ndisk);
|
||||||
void tfsDestroy();
|
void tfsDestroy();
|
||||||
int tfsUpdateInfo();
|
int tfsUpdateInfo();
|
||||||
|
@ -40,6 +31,16 @@ int tfsCreateDir(char *dirname);
|
||||||
int tfsRemoveDir(char *dirname);
|
int tfsRemoveDir(char *dirname);
|
||||||
int tfsRename(char *oldpath, char *newpath);
|
int tfsRename(char *oldpath, char *newpath);
|
||||||
|
|
||||||
|
// tfcntl.c
|
||||||
|
typedef struct TFSFILE TFSFILE;
|
||||||
|
typedef struct TFSDIR TFSDIR;
|
||||||
|
|
||||||
|
TFSDIR * tfsOpenDir(char *dir);
|
||||||
|
void tfsCloseDir(TFSDIR *tdir);
|
||||||
|
const TFSFILE *tfsReadDir(TFSDIR *tdir);
|
||||||
|
|
||||||
|
const char *tfsGetDiskName(int level, int id);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
* 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
|
|
@ -20,12 +20,12 @@
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#define fFatal(...) { if (fsDebugFlag & DEBUG_FATAL) { taosPrintLog("FS FATAL ", 255, __VA_ARGS__); }}
|
#define fFatal(...) { if (fsDebugFlag & DEBUG_FATAL) { taosPrintLog("TFS FATAL ", 255, __VA_ARGS__); }}
|
||||||
#define fError(...) { if (fsDebugFlag & DEBUG_ERROR) { taosPrintLog("FS ERROR ", 255, __VA_ARGS__); }}
|
#define fError(...) { if (fsDebugFlag & DEBUG_ERROR) { taosPrintLog("TFS ERROR ", 255, __VA_ARGS__); }}
|
||||||
#define fWarn(...) { if (fsDebugFlag & DEBUG_WARN) { taosPrintLog("FS WARN ", 255, __VA_ARGS__); }}
|
#define fWarn(...) { if (fsDebugFlag & DEBUG_WARN) { taosPrintLog("TFS WARN ", 255, __VA_ARGS__); }}
|
||||||
#define fInfo(...) { if (fsDebugFlag & DEBUG_INFO) { taosPrintLog("FS ", 255, __VA_ARGS__); }}
|
#define fInfo(...) { if (fsDebugFlag & DEBUG_INFO) { taosPrintLog("TFS ", 255, __VA_ARGS__); }}
|
||||||
#define fDebug(...) { if (fsDebugFlag & DEBUG_DEBUG) { taosPrintLog("FS ", cqDebugFlag, __VA_ARGS__); }}
|
#define fDebug(...) { if (fsDebugFlag & DEBUG_DEBUG) { taosPrintLog("TFS ", cqDebugFlag, __VA_ARGS__); }}
|
||||||
#define fTrace(...) { if (fsDebugFlag & DEBUG_TRACE) { taosPrintLog("FS ", cqDebugFlag, __VA_ARGS__); }}
|
#define fTrace(...) { if (fsDebugFlag & DEBUG_TRACE) { taosPrintLog("TFS ", cqDebugFlag, __VA_ARGS__); }}
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
/*
|
||||||
|
* 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
|
|
@ -0,0 +1,118 @@
|
||||||
|
/*
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include "os.h"
|
||||||
|
#include "taoserror.h"
|
||||||
|
#include "tdisk.h"
|
||||||
|
#include "tfs.h"
|
||||||
|
|
||||||
|
struct TFSFILE {
|
||||||
|
int level;
|
||||||
|
int id;
|
||||||
|
char name[TSDB_FILENAME_LEN];
|
||||||
|
};
|
||||||
|
|
||||||
|
struct TFSDIR {
|
||||||
|
int level;
|
||||||
|
int id;
|
||||||
|
char name[TSDB_FILENAME_LEN];
|
||||||
|
TFSFILE tfsfile;
|
||||||
|
DIR * dir;
|
||||||
|
};
|
||||||
|
|
||||||
|
TFSDIR *tfsOpenDir(char *dir) {
|
||||||
|
TFSDIR *tdir = (TFSDIR *)calloc(1, sizeof(*tdir));
|
||||||
|
if (tdir == NULL) {
|
||||||
|
terrno = TSDB_CODE_FS_OUT_OF_MEMORY;
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tfsOpenDirImpl(tdir) < 0) {
|
||||||
|
tfsCloseDir(tdir);
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
return tdir;
|
||||||
|
}
|
||||||
|
|
||||||
|
void tfsCloseDir(TFSDIR *tdir) {
|
||||||
|
if (tdir) {
|
||||||
|
if (tdir->dir != NULL) {
|
||||||
|
closedir(tdir->dir);
|
||||||
|
tdir->dir = NULL;
|
||||||
|
}
|
||||||
|
free(tdir);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
const TFSFILE *tfsReadDir(TFSDIR *tdir) {
|
||||||
|
if (tdir->dir == NULL) return NULL;
|
||||||
|
|
||||||
|
while (true) {
|
||||||
|
struct dirent *dp = readdir(tdir->dir);
|
||||||
|
if (dp != NULL) {
|
||||||
|
tdir->tfsfile.level = tdir->level;
|
||||||
|
tdir->tfsfile.id = tdir->id;
|
||||||
|
snprintf(tdir->tfsfile.name, TSDB_FILENAME_LEN, "%s/%s", tdir->name, dp->d_name);
|
||||||
|
|
||||||
|
return &(tdir->tfsfile);
|
||||||
|
}
|
||||||
|
|
||||||
|
closedir(tdir->dir);
|
||||||
|
|
||||||
|
// Move to next
|
||||||
|
if (tdir->id < tfsNDisksAt(tdir->level) - 1) {
|
||||||
|
tdir->id++;
|
||||||
|
} else {
|
||||||
|
tdir->level++;
|
||||||
|
tdir->id = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tfsOpenDirImpl(tdir) < 0) {
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (tdir->dir == NULL) return NULL;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static int tfsOpenDirImpl(TFSDIR *tdir) {
|
||||||
|
char dirName[TSDB_FILENAME_LEN] = "\0";
|
||||||
|
|
||||||
|
while (tdir->level < tfsMaxLevel()) {
|
||||||
|
while (tdir->id < tfsNDisksAt(tdir->level)) {
|
||||||
|
snprintf(dirName, TSDB_FILENAME_LEN, "%s/%s", tfsGetDiskDir(tdir->level, tdir->id), tdir->name);
|
||||||
|
|
||||||
|
tdir->dir = opendir(dirName);
|
||||||
|
if (tdir->dir == NULL) {
|
||||||
|
if (errno == ENOENT) {
|
||||||
|
tdir->id++;
|
||||||
|
} else {
|
||||||
|
fError("failed to open dir %s since %s", dirName, strerror(errno));
|
||||||
|
terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tdir->id = 0;
|
||||||
|
tdir->level++;
|
||||||
|
}
|
||||||
|
|
||||||
|
ASSERT(tdir->dir == NULL);
|
||||||
|
return 0;
|
||||||
|
}
|
|
@ -23,6 +23,11 @@
|
||||||
|
|
||||||
#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;
|
||||||
|
@ -166,6 +171,10 @@ int tfsRename(char *oldpath, char *newpath) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const char *tfsGetDiskName(int level, int id) {
|
||||||
|
return DISK_AT(level, id)->dir;
|
||||||
|
}
|
||||||
|
|
||||||
static int tfsMount(SDiskCfg *pCfg) {
|
static int tfsMount(SDiskCfg *pCfg) {
|
||||||
SDiskID did;
|
SDiskID did;
|
||||||
|
|
||||||
|
|
|
@ -56,6 +56,8 @@ SDisk *tdMountToTier(STier *pTier, SDiskCfg *pCfg) {
|
||||||
if (pTier->disks[id] == NULL) return -1;
|
if (pTier->disks[id] == NULL) return -1;
|
||||||
pTier->ndisk++;
|
pTier->ndisk++;
|
||||||
|
|
||||||
|
fDebug("disk %s is mounted at level %d id %d", pCfg->dir, pCfg->level, id);
|
||||||
|
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,22 +16,15 @@
|
||||||
#define TAOS_RANDOM_FILE_FAIL_TEST
|
#define TAOS_RANDOM_FILE_FAIL_TEST
|
||||||
#include <regex.h>
|
#include <regex.h>
|
||||||
#include "os.h"
|
#include "os.h"
|
||||||
#include "tglobal.h"
|
|
||||||
#include "talgo.h"
|
#include "talgo.h"
|
||||||
#include "tchecksum.h"
|
#include "tchecksum.h"
|
||||||
#include "tsdbMain.h"
|
#include "tsdbMain.h"
|
||||||
#include "tutil.h"
|
#include "tutil.h"
|
||||||
|
#include "tfs.h"
|
||||||
|
|
||||||
const char * tsdbFileSuffix[] = {".head", ".data", ".last", ".stat", ".h", ".d", ".l", ".s"};
|
|
||||||
|
|
||||||
static void tsdbDestroyFile(SFile *pFile);
|
const char *tsdbFileSuffix[] = {".head", ".data", ".last", ".stat", ".h", ".d", ".l", ".s"};
|
||||||
static int compFGroup(const void *arg1, const void *arg2);
|
|
||||||
static int keyFGroupCompFunc(const void *key, const void *fgroup);
|
|
||||||
static int tsdbLoadFilesFromDisk(STsdbRepo *pRepo, SDisk *pDisk);
|
|
||||||
static SHashObj *tsdbGetAllFids(STsdbRepo *pRepo, char *dirName);
|
|
||||||
static int tsdbRestoreFileGroup(STsdbRepo *pRepo, SDisk *pDisk, int fid, SFileGroup *pFileGroup);
|
|
||||||
static int tsdbGetFidLevel(int fid, SFidGroup *pFidGroup);
|
|
||||||
static int tsdbCreateVnodeDataDir(char *baseDir, int vid);
|
|
||||||
|
|
||||||
// ---------------- INTERNAL FUNCTIONS ----------------
|
// ---------------- INTERNAL FUNCTIONS ----------------
|
||||||
STsdbFileH *tsdbNewFileH(STsdbCfg *pCfg) {
|
STsdbFileH *tsdbNewFileH(STsdbCfg *pCfg) {
|
||||||
|
@ -75,22 +68,6 @@ int tsdbOpenFileH(STsdbRepo *pRepo) {
|
||||||
ASSERT(pRepo != NULL && pRepo->tsdbFileH != NULL);
|
ASSERT(pRepo != NULL && pRepo->tsdbFileH != NULL);
|
||||||
char dataDir[TSDB_FILENAME_LEN] = "\0";
|
char dataDir[TSDB_FILENAME_LEN] = "\0";
|
||||||
|
|
||||||
for (int level = 0; level < tsDnodeTier->nTiers; level++) {
|
|
||||||
STier *pTier = tsDnodeTier->tiers + level;
|
|
||||||
for (int did = 0; did < pTier->nDisks; did++) {
|
|
||||||
SDisk *pDisk = pTier->disks[did];
|
|
||||||
|
|
||||||
tdGetTsdbDataDir(pDisk->dir, REPO_ID(pRepo), dataDir);
|
|
||||||
|
|
||||||
if (access(dataDir, F_OK) != 0) {
|
|
||||||
// Skip those disks without data
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
tsdbLoadFilesFromDisk(pRepo, pDisk);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -521,254 +498,254 @@ static int keyFGroupCompFunc(const void *key, const void *fgroup) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static int tsdbLoadFilesFromDisk(STsdbRepo *pRepo, SDisk *pDisk) {
|
// static int tsdbLoadFilesFromDisk(STsdbRepo *pRepo, SDisk *pDisk) {
|
||||||
char tsdbDataDir[TSDB_FILENAME_LEN] = "\0";
|
// char tsdbDataDir[TSDB_FILENAME_LEN] = "\0";
|
||||||
char tsdbRootDir[TSDB_FILENAME_LEN] = "\0";
|
// char tsdbRootDir[TSDB_FILENAME_LEN] = "\0";
|
||||||
char fname[TSDB_FILENAME_LEN] = "\0";
|
// char fname[TSDB_FILENAME_LEN] = "\0";
|
||||||
SHashObj * pFids = NULL;
|
// SHashObj * pFids = NULL;
|
||||||
SHashMutableIterator *pIter = NULL;
|
// SHashMutableIterator *pIter = NULL;
|
||||||
STsdbFileH * pFileH = pRepo->tsdbFileH;
|
// STsdbFileH * pFileH = pRepo->tsdbFileH;
|
||||||
SFileGroup fgroup = {0};
|
// SFileGroup fgroup = {0};
|
||||||
STsdbCfg * pCfg = &(pRepo->config);
|
// STsdbCfg * pCfg = &(pRepo->config);
|
||||||
SFidGroup fidGroup = {0};
|
// SFidGroup fidGroup = {0};
|
||||||
int mfid = 0;
|
// int mfid = 0;
|
||||||
|
|
||||||
tdGetTsdbRootDir(pDisk->dir, REPO_ID(pRepo), tsdbRootDir);
|
// tdGetTsdbRootDir(pDisk->dir, REPO_ID(pRepo), tsdbRootDir);
|
||||||
tdGetTsdbDataDir(pDisk->dir, REPO_ID(pRepo), tsdbDataDir);
|
// tdGetTsdbDataDir(pDisk->dir, REPO_ID(pRepo), tsdbDataDir);
|
||||||
|
|
||||||
pFids = tsdbGetAllFids(pRepo, tsdbDataDir);
|
// pFids = tsdbGetAllFids(pRepo, tsdbDataDir);
|
||||||
if (pFids == NULL) {
|
// if (pFids == NULL) {
|
||||||
goto _err;
|
// goto _err;
|
||||||
}
|
// }
|
||||||
|
|
||||||
pIter = taosHashCreateIter(pFids);
|
// pIter = taosHashCreateIter(pFids);
|
||||||
if (pIter == NULL) {
|
// if (pIter == NULL) {
|
||||||
goto _err;
|
// goto _err;
|
||||||
}
|
// }
|
||||||
|
|
||||||
tsdbGetFidGroup(pCfg, &fidGroup);
|
// tsdbGetFidGroup(pCfg, &fidGroup);
|
||||||
mfid = fidGroup.minFid;
|
// mfid = fidGroup.minFid;
|
||||||
|
|
||||||
while (taosHashIterNext(pIter)) {
|
// while (taosHashIterNext(pIter)) {
|
||||||
int32_t fid = *(int32_t *)taosHashIterGet(pIter);
|
// int32_t fid = *(int32_t *)taosHashIterGet(pIter);
|
||||||
|
|
||||||
if (fid < mfid) {
|
// if (fid < mfid) {
|
||||||
for (int type = 0; type < TSDB_FILE_TYPE_MAX; type++) {
|
// for (int type = 0; type < TSDB_FILE_TYPE_MAX; type++) {
|
||||||
tsdbGetDataFileName(tsdbRootDir, REPO_ID(pRepo), fid, type, fname);
|
// tsdbGetDataFileName(tsdbRootDir, REPO_ID(pRepo), fid, type, fname);
|
||||||
(void)remove(fname);
|
// (void)remove(fname);
|
||||||
}
|
// }
|
||||||
|
|
||||||
tsdbGetDataFileName(tsdbRootDir, REPO_ID(pRepo), fid, TSDB_FILE_TYPE_NHEAD, fname);
|
// tsdbGetDataFileName(tsdbRootDir, REPO_ID(pRepo), fid, TSDB_FILE_TYPE_NHEAD, fname);
|
||||||
(void)remove(fname);
|
// (void)remove(fname);
|
||||||
|
|
||||||
tsdbGetDataFileName(tsdbRootDir, REPO_ID(pRepo), fid, TSDB_FILE_TYPE_NLAST, fname);
|
// tsdbGetDataFileName(tsdbRootDir, REPO_ID(pRepo), fid, TSDB_FILE_TYPE_NLAST, fname);
|
||||||
(void)remove(fname);
|
// (void)remove(fname);
|
||||||
|
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
tsdbRestoreFileGroup(pRepo, pDisk, fid, &fgroup);
|
// tsdbRestoreFileGroup(pRepo, pDisk, fid, &fgroup);
|
||||||
pFileH->pFGroup[pFileH->nFGroups++] = fgroup;
|
// pFileH->pFGroup[pFileH->nFGroups++] = fgroup;
|
||||||
qsort((void *)(pFileH->pFGroup), pFileH->nFGroups, sizeof(fgroup), compFGroup);
|
// qsort((void *)(pFileH->pFGroup), pFileH->nFGroups, sizeof(fgroup), compFGroup);
|
||||||
|
|
||||||
// TODO
|
// // TODO
|
||||||
pDisk->dmeta.nfiles++;
|
// pDisk->dmeta.nfiles++;
|
||||||
}
|
// }
|
||||||
|
|
||||||
taosHashDestroyIter(pIter);
|
// taosHashDestroyIter(pIter);
|
||||||
taosHashCleanup(pFids);
|
// taosHashCleanup(pFids);
|
||||||
return 0;
|
// return 0;
|
||||||
|
|
||||||
_err:
|
// _err:
|
||||||
taosHashDestroyIter(pIter);
|
// taosHashDestroyIter(pIter);
|
||||||
taosHashCleanup(pFids);
|
// taosHashCleanup(pFids);
|
||||||
return -1;
|
// return -1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
static int tsdbRestoreFileGroup(STsdbRepo *pRepo, SDisk *pDisk, int fid, SFileGroup *pFileGroup) {
|
// static int tsdbRestoreFileGroup(STsdbRepo *pRepo, SDisk *pDisk, int fid, SFileGroup *pFileGroup) {
|
||||||
char tsdbRootDir[TSDB_FILENAME_LEN] = "\0";
|
// char tsdbRootDir[TSDB_FILENAME_LEN] = "\0";
|
||||||
char nheadF[TSDB_FILENAME_LEN] = "\0";
|
// char nheadF[TSDB_FILENAME_LEN] = "\0";
|
||||||
char nlastF[TSDB_FILENAME_LEN] = "\0";
|
// char nlastF[TSDB_FILENAME_LEN] = "\0";
|
||||||
bool newHeadExists = false;
|
// bool newHeadExists = false;
|
||||||
bool newLastExists = false;
|
// bool newLastExists = false;
|
||||||
|
|
||||||
uint32_t version = 0;
|
// uint32_t version = 0;
|
||||||
|
|
||||||
terrno = TSDB_CODE_SUCCESS;
|
// terrno = TSDB_CODE_SUCCESS;
|
||||||
|
|
||||||
memset((void *)pFileGroup, 0, sizeof(*pFileGroup));
|
// memset((void *)pFileGroup, 0, sizeof(*pFileGroup));
|
||||||
pFileGroup->fileId = fid;
|
// pFileGroup->fileId = fid;
|
||||||
for (int type = 0; type < TSDB_FILE_TYPE_MAX; type++) {
|
// for (int type = 0; type < TSDB_FILE_TYPE_MAX; type++) {
|
||||||
SFile *pFile = pFileGroup->files + type;
|
// SFile *pFile = pFileGroup->files + type;
|
||||||
pFile->fd = -1;
|
// pFile->fd = -1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
tdGetTsdbRootDir(pDisk->dir, REPO_ID(pRepo), tsdbRootDir);
|
// tdGetTsdbRootDir(pDisk->dir, REPO_ID(pRepo), tsdbRootDir);
|
||||||
for (int type = 0; type < TSDB_FILE_TYPE_MAX; type++) {
|
// for (int type = 0; type < TSDB_FILE_TYPE_MAX; type++) {
|
||||||
SFile *pFile = pFileGroup->files + type;
|
// SFile *pFile = pFileGroup->files + type;
|
||||||
tsdbGetDataFileName(tsdbRootDir, REPO_ID(pRepo), fid, TSDB_FILE_TYPE_HEAD, pFile->fname);
|
// tsdbGetDataFileName(tsdbRootDir, REPO_ID(pRepo), fid, TSDB_FILE_TYPE_HEAD, pFile->fname);
|
||||||
if (access(pFile->fname, F_OK) != 0) {
|
// if (access(pFile->fname, F_OK) != 0) {
|
||||||
memset(&(pFile->info), 0, sizeof(pFile->info));
|
// memset(&(pFile->info), 0, sizeof(pFile->info));
|
||||||
pFile->info.magic = TSDB_FILE_INIT_MAGIC;
|
// pFile->info.magic = TSDB_FILE_INIT_MAGIC;
|
||||||
pFileGroup->state = 1;
|
// pFileGroup->state = 1;
|
||||||
terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
|
// terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
tsdbGetDataFileName(tsdbRootDir, REPO_ID(pRepo), fid, TSDB_FILE_TYPE_NHEAD, nheadF);
|
// tsdbGetDataFileName(tsdbRootDir, REPO_ID(pRepo), fid, TSDB_FILE_TYPE_NHEAD, nheadF);
|
||||||
tsdbGetDataFileName(tsdbRootDir, REPO_ID(pRepo), fid, TSDB_FILE_TYPE_NLAST, nlastF);
|
// tsdbGetDataFileName(tsdbRootDir, REPO_ID(pRepo), fid, TSDB_FILE_TYPE_NLAST, nlastF);
|
||||||
|
|
||||||
if (access(nheadF, F_OK) == 0) {
|
// if (access(nheadF, F_OK) == 0) {
|
||||||
newHeadExists = true;
|
// newHeadExists = true;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (access(nlastF, F_OK) == 0) {
|
// if (access(nlastF, F_OK) == 0) {
|
||||||
newLastExists = true;
|
// newLastExists = true;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (newHeadExists) {
|
// if (newHeadExists) {
|
||||||
(void)remove(nheadF);
|
// (void)remove(nheadF);
|
||||||
(void)remove(nlastF);
|
// (void)remove(nlastF);
|
||||||
} else {
|
// } else {
|
||||||
if (newLastExists) {
|
// if (newLastExists) {
|
||||||
(void)rename(nlastF, pFileGroup->files[TSDB_FILE_TYPE_LAST].fname);
|
// (void)rename(nlastF, pFileGroup->files[TSDB_FILE_TYPE_LAST].fname);
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (terrno != TSDB_CODE_SUCCESS) {
|
// if (terrno != TSDB_CODE_SUCCESS) {
|
||||||
return -1;
|
// return -1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
for (int type = 0; type < TSDB_FILE_TYPE_MAX; type++) {
|
// for (int type = 0; type < TSDB_FILE_TYPE_MAX; type++) {
|
||||||
SFile *pFile = pFileGroup->files + type;
|
// SFile *pFile = pFileGroup->files + type;
|
||||||
if (tsdbOpenFile(pFile, O_RDONLY) < 0) {
|
// if (tsdbOpenFile(pFile, O_RDONLY) < 0) {
|
||||||
memset(&(pFile->info), 0, sizeof(pFile->info));
|
// memset(&(pFile->info), 0, sizeof(pFile->info));
|
||||||
pFile->info.magic = TSDB_FILE_INIT_MAGIC;
|
// pFile->info.magic = TSDB_FILE_INIT_MAGIC;
|
||||||
pFileGroup->state = 1;
|
// pFileGroup->state = 1;
|
||||||
terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
|
// terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (tsdbLoadFileHeader(pFile, &version) < 0) {
|
// if (tsdbLoadFileHeader(pFile, &version) < 0) {
|
||||||
memset(&(pFile->info), 0, sizeof(pFile->info));
|
// memset(&(pFile->info), 0, sizeof(pFile->info));
|
||||||
pFile->info.magic = TSDB_FILE_INIT_MAGIC;
|
// pFile->info.magic = TSDB_FILE_INIT_MAGIC;
|
||||||
pFileGroup->state = 1;
|
// pFileGroup->state = 1;
|
||||||
terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
|
// terrno = TSDB_CODE_TDB_FILE_CORRUPTED;
|
||||||
tsdbCloseFile(pFile);
|
// tsdbCloseFile(pFile);
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (version != TSDB_FILE_VERSION) {
|
// if (version != TSDB_FILE_VERSION) {
|
||||||
tsdbError("vgId:%d file %s version %u is not the same as program version %u which may cause problem",
|
// tsdbError("vgId:%d file %s version %u is not the same as program version %u which may cause problem",
|
||||||
REPO_ID(pRepo), pFile->fname, version, TSDB_FILE_VERSION);
|
// REPO_ID(pRepo), pFile->fname, version, TSDB_FILE_VERSION);
|
||||||
}
|
// }
|
||||||
|
|
||||||
tsdbCloseFile(pFile);
|
// tsdbCloseFile(pFile);
|
||||||
}
|
// }
|
||||||
|
|
||||||
if (terrno != TSDB_CODE_SUCCESS) {
|
// if (terrno != TSDB_CODE_SUCCESS) {
|
||||||
return -1;
|
// return -1;
|
||||||
} else {
|
// } else {
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
static SHashObj *tsdbGetAllFids(STsdbRepo *pRepo, char *dirName) {
|
// static SHashObj *tsdbGetAllFids(STsdbRepo *pRepo, char *dirName) {
|
||||||
DIR * dir = NULL;
|
// DIR * dir = NULL;
|
||||||
regex_t regex = {0};
|
// regex_t regex = {0};
|
||||||
int code = 0;
|
// int code = 0;
|
||||||
int32_t vid, fid;
|
// int32_t vid, fid;
|
||||||
SHashObj *pHash = NULL;
|
// SHashObj *pHash = NULL;
|
||||||
|
|
||||||
code = regcomp(®ex, "^v[0-9]+f[0-9]+\\.(head|data|last|h|d|l)$", REG_EXTENDED);
|
// code = regcomp(®ex, "^v[0-9]+f[0-9]+\\.(head|data|last|h|d|l)$", REG_EXTENDED);
|
||||||
if (code != 0) {
|
// if (code != 0) {
|
||||||
terrno = TSDB_CODE_TDB_OUT_OF_MEMORY;
|
// terrno = TSDB_CODE_TDB_OUT_OF_MEMORY;
|
||||||
goto _err;
|
// goto _err;
|
||||||
}
|
// }
|
||||||
|
|
||||||
dir = opendir(dirName);
|
// dir = opendir(dirName);
|
||||||
if (dir == NULL) {
|
// if (dir == NULL) {
|
||||||
tsdbError("vgId:%d failed to open directory %s since %s", REPO_ID(pRepo), dirName, strerror(errno));
|
// tsdbError("vgId:%d failed to open directory %s since %s", REPO_ID(pRepo), dirName, strerror(errno));
|
||||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
// terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
goto _err;
|
// goto _err;
|
||||||
}
|
// }
|
||||||
|
|
||||||
pHash = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
|
// pHash = taosHashInit(1024, taosGetDefaultHashFunction(TSDB_DATA_TYPE_INT), false, HASH_NO_LOCK);
|
||||||
if (pHash == NULL) {
|
// if (pHash == NULL) {
|
||||||
terrno = TSDB_CODE_TDB_OUT_OF_MEMORY;
|
// terrno = TSDB_CODE_TDB_OUT_OF_MEMORY;
|
||||||
goto _err;
|
// goto _err;
|
||||||
}
|
// }
|
||||||
|
|
||||||
struct dirent *dp = NULL;
|
// struct dirent *dp = NULL;
|
||||||
while ((dp = readdir(dir)) != NULL) {
|
// while ((dp = readdir(dir)) != NULL) {
|
||||||
if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) continue;
|
// if (strcmp(dp->d_name, ".") == 0 || strcmp(dp->d_name, "..") == 0) continue;
|
||||||
|
|
||||||
code = regexec(®ex, dp->d_name, 0, NULL, 0);
|
// code = regexec(®ex, dp->d_name, 0, NULL, 0);
|
||||||
if (code == 0) {
|
// if (code == 0) {
|
||||||
sscanf(dp->d_name, "v%df%d", &vid, &fid);
|
// sscanf(dp->d_name, "v%df%d", &vid, &fid);
|
||||||
|
|
||||||
if (vid != REPO_ID(pRepo)) {
|
// if (vid != REPO_ID(pRepo)) {
|
||||||
tsdbError("vgId:%d invalid file %s exists, ignore it", REPO_ID(pRepo), dp->d_name);
|
// tsdbError("vgId:%d invalid file %s exists, ignore it", REPO_ID(pRepo), dp->d_name);
|
||||||
continue;
|
// continue;
|
||||||
}
|
// }
|
||||||
|
|
||||||
taosHashPut(pHash, (void *)(&fid), sizeof(fid), (void *)(&fid), sizeof(fid));
|
// taosHashPut(pHash, (void *)(&fid), sizeof(fid), (void *)(&fid), sizeof(fid));
|
||||||
} else if (code == REG_NOMATCH) {
|
// } else if (code == REG_NOMATCH) {
|
||||||
tsdbError("vgId:%d invalid file %s exists, ignore it", REPO_ID(pRepo), dp->d_name);
|
// tsdbError("vgId:%d invalid file %s exists, ignore it", REPO_ID(pRepo), dp->d_name);
|
||||||
continue;
|
// continue;
|
||||||
} else {
|
// } else {
|
||||||
goto _err;
|
// goto _err;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
closedir(dir);
|
// closedir(dir);
|
||||||
regfree(®ex);
|
// regfree(®ex);
|
||||||
return pHash;
|
// return pHash;
|
||||||
|
|
||||||
_err:
|
// _err:
|
||||||
taosHashCleanup(pHash);
|
// taosHashCleanup(pHash);
|
||||||
if (dir != NULL) closedir(dir);
|
// if (dir != NULL) closedir(dir);
|
||||||
regfree(®ex);
|
// regfree(®ex);
|
||||||
return NULL;
|
// return NULL;
|
||||||
}
|
// }
|
||||||
|
|
||||||
static int tsdbGetFidLevel(int fid, SFidGroup *pFidGroup) {
|
// static int tsdbGetFidLevel(int fid, SFidGroup *pFidGroup) {
|
||||||
if (fid >= pFidGroup->maxFid) {
|
// if (fid >= pFidGroup->maxFid) {
|
||||||
return 0;
|
// return 0;
|
||||||
} else if (fid >= pFidGroup->midFid && fid < pFidGroup->maxFid) {
|
// } else if (fid >= pFidGroup->midFid && fid < pFidGroup->maxFid) {
|
||||||
return 1;
|
// return 1;
|
||||||
} else {
|
// } else {
|
||||||
return 2;
|
// return 2;
|
||||||
}
|
// }
|
||||||
}
|
// }
|
||||||
|
|
||||||
static int tsdbCreateVnodeDataDir(char *baseDir, int vid) {
|
// static int tsdbCreateVnodeDataDir(char *baseDir, int vid) {
|
||||||
char dirName[TSDB_FILENAME_LEN] = "\0";
|
// char dirName[TSDB_FILENAME_LEN] = "\0";
|
||||||
char tsdbRootDir[TSDB_FILENAME_LEN] = "\0";
|
// char tsdbRootDir[TSDB_FILENAME_LEN] = "\0";
|
||||||
|
|
||||||
tdGetVnodeRootDir(baseDir, dirName);
|
// tdGetVnodeRootDir(baseDir, dirName);
|
||||||
if (taosMkDir(dirName, 0755) < 0 && errno != EEXIST) {
|
// if (taosMkDir(dirName, 0755) < 0 && errno != EEXIST) {
|
||||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
// terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
return -1;
|
// return -1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
tdGetVnodeDir(baseDir, vid, dirName);
|
// tdGetVnodeDir(baseDir, vid, dirName);
|
||||||
if (taosMkDir(dirName, 0755) < 0 && errno != EEXIST) {
|
// if (taosMkDir(dirName, 0755) < 0 && errno != EEXIST) {
|
||||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
// terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
return -1;
|
// return -1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
tdGetTsdbRootDir(baseDir, vid, tsdbRootDir);
|
// tdGetTsdbRootDir(baseDir, vid, tsdbRootDir);
|
||||||
if (taosMkDir(tsdbRootDir, 0755) < 0 && errno != EEXIST) {
|
// if (taosMkDir(tsdbRootDir, 0755) < 0 && errno != EEXIST) {
|
||||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
// terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
return -1;
|
// return -1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
tdGetTsdbDataDir(baseDir, vid, dirName);
|
// tdGetTsdbDataDir(baseDir, vid, dirName);
|
||||||
if (taosMkDir(dirName, 0755) < 0 && errno != EEXIST) {
|
// if (taosMkDir(dirName, 0755) < 0 && errno != EEXIST) {
|
||||||
terrno = TAOS_SYSTEM_ERROR(errno);
|
// terrno = TAOS_SYSTEM_ERROR(errno);
|
||||||
return -1;
|
// return -1;
|
||||||
}
|
// }
|
||||||
|
|
||||||
return 0;
|
// return 0;
|
||||||
}
|
// }
|
Loading…
Reference in New Issue