From 37e11f9b04d9ad3410cfdb570df68ca5685f5fe9 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 13 Aug 2020 21:16:53 +0800 Subject: [PATCH] try to solve sync problem --- src/tsdb/inc/tsdbMain.h | 1 + src/tsdb/src/tsdbFile.c | 29 +++++++++++++++++++++++++++++ src/tsdb/src/tsdbMain.c | 25 +++++++++++++------------ src/util/inc/tkvstore.h | 1 + src/util/src/tkvstore.c | 25 +++++++++++++++++++++++++ 5 files changed, 69 insertions(+), 12 deletions(-) diff --git a/src/tsdb/inc/tsdbMain.h b/src/tsdb/inc/tsdbMain.h index 678e167351..7936ea423f 100644 --- a/src/tsdb/inc/tsdbMain.h +++ b/src/tsdb/inc/tsdbMain.h @@ -475,6 +475,7 @@ int tsdbUpdateFileHeader(SFile* pFile); int tsdbEncodeSFileInfo(void** buf, const STsdbFileInfo* pInfo); void* tsdbDecodeSFileInfo(void* buf, STsdbFileInfo* pInfo); void tsdbRemoveFileGroup(STsdbRepo* pRepo, SFileGroup* pFGroup); +void tsdbGetFileInfoImpl(char* fname, uint32_t* magic, int32_t* size); void tsdbGetFidKeyRange(int daysPerFile, int8_t precision, int fileId, TSKEY *minKey, TSKEY *maxKey); // ------------------ tsdbRWHelper.c diff --git a/src/tsdb/src/tsdbFile.c b/src/tsdb/src/tsdbFile.c index 5ba9a68e37..8bc4be6d67 100644 --- a/src/tsdb/src/tsdbFile.c +++ b/src/tsdb/src/tsdbFile.c @@ -423,6 +423,35 @@ void tsdbRemoveFileGroup(STsdbRepo *pRepo, SFileGroup *pFGroup) { } } +void tsdbGetFileInfoImpl(char *fname, uint32_t *magic, int32_t *size) { + char buf[TSDB_FILE_HEAD_SIZE] = "\0"; + uint32_t version = 0; + STsdbFileInfo info = {0}; + + int fd = open(fname, O_RDONLY); + if (fd < 0) goto _err; + + if (taosTRead(fd, buf, TSDB_FILE_HEAD_SIZE) < TSDB_FILE_HEAD_SIZE) goto _err; + + if (!taosCheckChecksumWhole((uint8_t *)buf, TSDB_FILE_HEAD_SIZE)) goto _err; + + void *pBuf = (void *)buf; + pBuf = taosDecodeFixedU32(pBuf, &version); + pBuf = tsdbDecodeSFileInfo(pBuf, &info); + + off_t offset = lseek(fd, 0, SEEK_END); + if (offset < 0) goto _err; + close(fd); + + *magic = info.magic; + *size = (int32_t)offset; + +_err: + if (fd >= 0) close(fd); + *magic = TSDB_FILE_INIT_MAGIC; + *size = 0; +} + // ---------------- LOCAL FUNCTIONS ---------------- static int tsdbInitFile(SFile *pFile, STsdbRepo *pRepo, int fid, int type) { uint32_t version; diff --git a/src/tsdb/src/tsdbMain.c b/src/tsdb/src/tsdbMain.c index 628a8bac81..dd647ddd9b 100644 --- a/src/tsdb/src/tsdbMain.c +++ b/src/tsdb/src/tsdbMain.c @@ -216,9 +216,9 @@ uint32_t tsdbGetFileInfo(TSDB_REPO_T *repo, char *name, uint32_t *index, uint32_ char *sdup = strdup(pRepo->rootDir); char *prefix = dirname(sdup); int prefixLen = (int)strlen(prefix); - taosTFree(sdup); if (name[0] == 0) { // get the file from index or after, but not larger than eindex + taosTFree(sdup); int fid = (*index) / TSDB_FILE_TYPE_MAX; if (pFileH->nFGroups == 0 || fid > pFileH->pFGroup[pFileH->nFGroups - 1].fileId) { @@ -248,18 +248,19 @@ uint32_t tsdbGetFileInfo(TSDB_REPO_T *repo, char *name, uint32_t *index, uint32_ strcpy(name, fname + prefixLen); } else { // get the named file at the specified index. If not there, return 0 if (*index == TSDB_META_FILE_INDEX) { // get meta file - fname = tsdbGetMetaFileName(pRepo->rootDir); - magic = TSDB_META_FILE_MAGIC(pRepo->tsdbMeta); + fname = malloc(prefixLen + strlen(name) + 2); + sprintf(fname, "%s/%s", prefix, name); + tsdbGetStoreInfo(fname, &magic, size); + taosFree(fname); + taosFree(sdup); + return magic; } else { - int fid = (*index) / TSDB_FILE_TYPE_MAX; - SFileGroup *pFGroup = tsdbSearchFGroup(pFileH, fid, TD_EQ); - if (pFGroup == NULL) { // not found - return 0; - } - - SFile *pFile = &pFGroup->files[(*index) % TSDB_FILE_TYPE_MAX]; - fname = strdup(pFile->fname); - magic = pFile->info.magic; + fname = malloc(prefixLen + strlen(name) + 2); + sprintf(fname, "%s/%s", prefix, name); + tsdbGetFileInfoImpl(fname, &magic, size); + taosFree(fname); + taosFree(sdup); + return magic; } } diff --git a/src/util/inc/tkvstore.h b/src/util/inc/tkvstore.h index a6cc82e751..3b4e8e3757 100644 --- a/src/util/inc/tkvstore.h +++ b/src/util/inc/tkvstore.h @@ -58,6 +58,7 @@ int tdKVStoreStartCommit(SKVStore *pStore); int tdUpdateKVStoreRecord(SKVStore *pStore, uint64_t uid, void *cont, int contLen); int tdDropKVStoreRecord(SKVStore *pStore, uint64_t uid); int tdKVStoreEndCommit(SKVStore *pStore); +void tsdbGetStoreInfo(char *fname, uint32_t *magic, int32_t *size); #ifdef __cplusplus } diff --git a/src/util/src/tkvstore.c b/src/util/src/tkvstore.c index 499edb4c1a..dd0600ec3c 100644 --- a/src/util/src/tkvstore.c +++ b/src/util/src/tkvstore.c @@ -330,6 +330,31 @@ int tdKVStoreEndCommit(SKVStore *pStore) { return 0; } +void tsdbGetStoreInfo(char *fname, uint32_t *magic, int32_t *size) { + char buf[TD_KVSTORE_HEADER_SIZE] = "\0"; + SStoreInfo info = {0}; + + int fd = open(fname, O_RDONLY); + if (fd < 0) goto _err; + + if (taosTRead(fd, buf, TD_KVSTORE_HEADER_SIZE) < TD_KVSTORE_HEADER_SIZE) goto _err; + if (!taosCheckChecksumWhole((uint8_t *)buf, TD_KVSTORE_HEADER_SIZE)) goto _err; + + void *pBuf = (void *)buf; + pBuf = tdDecodeStoreInfo(pBuf, &info); + off_t offset = lseek(fd, 0, SEEK_END); + if (offset < 0) goto _err; + close(fd); + + *magic = info.magic; + *size = (int32_t)offset; + +_err: + if (fd >= 0) close(fd); + *magic = TD_KVSTORE_INIT_MAGIC; + *size = 0; +} + static int tdLoadKVStoreHeader(int fd, char *fname, SStoreInfo *pInfo, uint32_t *version) { char buf[TD_KVSTORE_HEADER_SIZE] = "\0"; -- GitLab