diff --git a/src/inc/taoserror.h b/src/inc/taoserror.h index c9071f4cb0c14e689583b25c1c7e2939ec4a6fb6..98355a8672b94e4fc6b6051e31cba6bb985a0565 100644 --- a/src/inc/taoserror.h +++ b/src/inc/taoserror.h @@ -115,6 +115,7 @@ TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_QUERY_ID, 0, 255, "invalid query i TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_STREAM_ID, 0, 256, "invalid stream id") TAOS_DEFINE_ERROR(TSDB_CODE_INVALID_CONNECTION, 0, 257, "invalid connection") TAOS_DEFINE_ERROR(TSDB_CODE_SDB_ERROR, 0, 258, "sdb error") +TAOS_DEFINE_ERROR(TSDB_CODE_TIMESTAMP_OUT_OF_RANGE, 0, 259, "timestamp is out of range") // acct TAOS_DEFINE_ERROR(TSDB_CODE_ACCT_ALREADY_EXIST, 0, 300, "accounts already exist") diff --git a/src/tsdb/inc/tsdbMain.h b/src/tsdb/inc/tsdbMain.h index 22d069141654b129f366c6b5dfc5c1a32373dc46..89708a5553c6fc6e4472a9bc00f0f7b804ff862f 100644 --- a/src/tsdb/inc/tsdbMain.h +++ b/src/tsdb/inc/tsdbMain.h @@ -227,13 +227,13 @@ typedef struct { int maxFGroups; int numOfFGroups; - SFileGroup fGroup[]; + SFileGroup *fGroup; } STsdbFileH; #define TSDB_MIN_FILE_ID(fh) (fh)->fGroup[0].fileId #define TSDB_MAX_FILE_ID(fh) (fh)->fGroup[(fh)->numOfFGroups - 1].fileId -STsdbFileH *tsdbInitFileH(char *dataDir, int maxFiles); +STsdbFileH *tsdbInitFileH(char *dataDir, STsdbCfg *pCfg); void tsdbCloseFileH(STsdbFileH *pFileH); int tsdbCreateFile(char *dataDir, int fileId, const char *suffix, int maxTables, SFile *pFile, int writeHeader, int toClose); @@ -486,6 +486,9 @@ int tsdbMoveLastBlockIfNeccessary(SRWHelper *pHelper); int tsdbWriteCompInfo(SRWHelper *pHelper); int tsdbWriteCompIdx(SRWHelper *pHelper); +// --------- Other functions need to further organize +void tsdbFitRetention(STsdbRepo *pRepo); + #ifdef __cplusplus } #endif diff --git a/src/tsdb/src/tsdbFile.c b/src/tsdb/src/tsdbFile.c index ab76f69bedf1d289be57f7b2539ff1b9fbcac466..51bdda79e65d5fd43dc95eef69648d58d935c1cc 100644 --- a/src/tsdb/src/tsdbFile.c +++ b/src/tsdb/src/tsdbFile.c @@ -27,6 +27,7 @@ #include "tchecksum.h" #include "tsdbMain.h" #include "tutil.h" +#include "ttime.h" const char *tsdbFileSuffix[] = { ".head", // TSDB_FILE_TYPE_HEAD @@ -40,13 +41,19 @@ static int tsdbWriteFileHead(SFile *pFile); static int tsdbWriteHeadFileIdx(SFile *pFile, int maxTables); static int tsdbOpenFGroup(STsdbFileH *pFileH, char *dataDir, int fid); -STsdbFileH *tsdbInitFileH(char *dataDir, int maxFiles) { - STsdbFileH *pFileH = (STsdbFileH *)calloc(1, sizeof(STsdbFileH) + sizeof(SFileGroup) * maxFiles); +STsdbFileH *tsdbInitFileH(char *dataDir, STsdbCfg *pCfg) { + STsdbFileH *pFileH = (STsdbFileH *)calloc(1, sizeof(STsdbFileH)); if (pFileH == NULL) { // TODO: deal with ERROR here return NULL; } - pFileH->maxFGroups = maxFiles; + pFileH->maxFGroups = pCfg->keep / pCfg->daysPerFile + 2; + + pFileH->fGroup = (SFileGroup *)calloc(pFileH->maxFGroups, sizeof(SFileGroup)); + if (pFileH->fGroup == NULL) { + free(pFileH); + return NULL; + } DIR *dir = opendir(dataDir); if (dir == NULL) { @@ -69,7 +76,12 @@ STsdbFileH *tsdbInitFileH(char *dataDir, int maxFiles) { return pFileH; } -void tsdbCloseFileH(STsdbFileH *pFileH) { free(pFileH); } +void tsdbCloseFileH(STsdbFileH *pFileH) { + if (pFileH) { + tfree(pFileH->fGroup); + free(pFileH); + } +} static int tsdbInitFile(char *dataDir, int fid, const char *suffix, SFile *pFile) { tsdbGetFileName(dataDir, fid, suffix, pFile->fname); @@ -161,6 +173,18 @@ void tsdbInitFileGroupIter(STsdbFileH *pFileH, SFileGroupIter *pIter, int direct } } +void tsdbFitRetention(STsdbRepo *pRepo) { + STsdbFileH *pFileH = pRepo->tsdbFileH; + SFileGroup *pGroup = pFileH->fGroup; + + int mfid = + tsdbGetKeyFileId(taosGetTimestamp(pRepo->config.precision), pRepo->config.daysPerFile, pRepo->config.precision); + + while (pGroup[0].fileId < mfid) { + tsdbRemoveFileGroup(pFileH, pGroup[0].fileId); + } +} + void tsdbSeekFileGroupIter(SFileGroupIter *pIter, int fid) { if (pIter->numOfFGroups == 0) { assert(pIter->pFileGroup == NULL); @@ -252,43 +276,6 @@ int tsdbCopyBlockDataInFile(SFile *pOutFile, SFile *pInFile, SCompInfo *pCompInf return 0; } -// int tsdbLoadCompIdx(SFileGroup *pGroup, void *buf, int maxTables) { -// SFile *pFile = &(pGroup->files[TSDB_FILE_TYPE_HEAD]); -// if (lseek(pFile->fd, TSDB_FILE_HEAD_SIZE, SEEK_SET) < 0) return -1; - -// if (read(pFile->fd, buf, sizeof(SCompIdx) * maxTables) < 0) return -1; -// // TODO: need to check the correctness -// return 0; -// } - -// int tsdbLoadCompBlocks(SFileGroup *pGroup, SCompIdx *pIdx, void *buf) { -// SFile *pFile = &(pGroup->files[TSDB_FILE_TYPE_HEAD]); - -// if (lseek(pFile->fd, pIdx->offset, SEEK_SET) < 0) return -1; - -// if (read(pFile->fd, buf, pIdx->len) < 0) return -1; - -// // TODO: need to check the correctness - -// return 0; -// } - -// int tsdbLoadCompCols(SFile *pFile, SCompBlock *pBlock, void *buf) { -// // assert(pBlock->numOfSubBlocks == 0 || pBlock->numOfSubBlocks == 1); - -// if (lseek(pFile->fd, pBlock->offset, SEEK_SET) < 0) return -1; -// size_t size = sizeof(SCompData) + sizeof(SCompCol) * pBlock->numOfCols; -// if (read(pFile->fd, buf, size) < 0) return -1; - -// return 0; -// } - -// int tsdbLoadColData(SFile *pFile, SCompCol *pCol, int64_t blockBaseOffset, void *buf) { -// if (lseek(pFile->fd, blockBaseOffset + pCol->offset, SEEK_SET) < 0) return -1; -// if (read(pFile->fd, buf, pCol->len) < 0) return -1; -// return 0; -// } - static int compFGroupKey(const void *key, const void *fgroup) { int fid = *(int *)key; SFileGroup *pFGroup = (SFileGroup *)fgroup; diff --git a/src/tsdb/src/tsdbMain.c b/src/tsdb/src/tsdbMain.c index b1ef3d2d9ce38e0c0e0347ac9d1c6b9a61905cb3..dd5dbd3f8d4f0040ba51f1f6b31f37bcb62c95b8 100644 --- a/src/tsdb/src/tsdbMain.c +++ b/src/tsdb/src/tsdbMain.c @@ -6,6 +6,7 @@ #include "tsdbMain.h" #include "tscompression.h" #include "tchecksum.h" +#include "ttime.h" int tsdbDebugFlag = 135; @@ -27,7 +28,7 @@ static int32_t tsdbCheckAndSetDefaultCfg(STsdbCfg *pCfg); static int32_t tsdbSetRepoEnv(STsdbRepo *pRepo); static int32_t tsdbDestroyRepoEnv(STsdbRepo *pRepo); // static int tsdbOpenMetaFile(char *tsdbDir); -static int32_t tsdbInsertDataToTable(TsdbRepoT *repo, SSubmitBlk *pBlock); +static int32_t tsdbInsertDataToTable(TsdbRepoT *repo, SSubmitBlk *pBlock, TSKEY now); static int32_t tsdbRestoreCfg(STsdbRepo *pRepo, STsdbCfg *pCfg); static int32_t tsdbGetDataDirName(STsdbRepo *pRepo, char *fname); static void * tsdbCommitData(void *arg); @@ -214,7 +215,7 @@ TsdbRepoT *tsdbOpenRepo(char *tsdbDir, STsdbAppH *pAppH) { } tsdbGetDataDirName(pRepo, dataDir); - pRepo->tsdbFileH = tsdbInitFileH(dataDir, pRepo->config.maxTables); + pRepo->tsdbFileH = tsdbInitFileH(dataDir, &(pRepo->config)); if (pRepo->tsdbFileH == NULL) { tsdbFreeCache(pRepo->tsdbCache); tsdbFreeMeta(pRepo->tsdbMeta); @@ -394,13 +395,16 @@ STableInfo *tsdbGetTableInfo(TsdbRepoT *pRepo, STableId tableId) { // TODO: need to return the number of data inserted int32_t tsdbInsertData(TsdbRepoT *repo, SSubmitMsg *pMsg) { SSubmitMsgIter msgIter; + STsdbRepo *pRepo = (STsdbRepo *)repo; tsdbInitSubmitMsgIter(pMsg, &msgIter); SSubmitBlk *pBlock = NULL; int32_t code = TSDB_CODE_SUCCESS; - + + TSKEY now = taosGetTimestamp(pRepo->config.precision); + while ((pBlock = tsdbGetSubmitMsgNext(&msgIter)) != NULL) { - if ((code = tsdbInsertDataToTable(repo, pBlock)) != TSDB_CODE_SUCCESS) { + if ((code = tsdbInsertDataToTable(repo, pBlock, now)) != TSDB_CODE_SUCCESS) { return code; } } @@ -787,21 +791,31 @@ static int32_t tdInsertRowToTable(STsdbRepo *pRepo, SDataRow row, STable *pTable return 0; } -static int32_t tsdbInsertDataToTable(TsdbRepoT *repo, SSubmitBlk *pBlock) { +static int32_t tsdbInsertDataToTable(TsdbRepoT *repo, SSubmitBlk *pBlock, TSKEY now) { STsdbRepo *pRepo = (STsdbRepo *)repo; STableId tableId = {.uid = pBlock->uid, .tid = pBlock->tid}; STable *pTable = tsdbIsValidTableToInsert(pRepo->tsdbMeta, tableId); if (pTable == NULL) { - uError("failed to get table for insert, uid:%" PRIu64 ", tid:%d", tableId.uid, tableId.tid); + tsdbError("failed to get table for insert, uid:%" PRIu64 ", tid:%d", tableId.uid, tableId.tid); return TSDB_CODE_INVALID_TABLE_ID; } - SSubmitBlkIter blkIter; - SDataRow row; + SSubmitBlkIter blkIter = {0}; + SDataRow row = NULL; + + TSKEY minKey = now - tsMsPerDay[pRepo->config.precision] * pRepo->config.keep; + TSKEY maxKey = now + tsMsPerDay[pRepo->config.precision] * pRepo->config.daysPerFile; tsdbInitSubmitBlkIter(pBlock, &blkIter); while ((row = tsdbGetSubmitBlkNext(&blkIter)) != NULL) { + if (dataRowKey(row) < minKey || dataRowKey(row) > maxKey) { + tsdbError( + "tsdbId: %d, table tid: %d, talbe uid: %ld timestamp is out of range. now: %ld maxKey: %ld, minKey: %ld", + pRepo->config.tsdbId, pTable->tableId.tid, pTable->tableId.uid, now, minKey, maxKey); + return TSDB_CODE_TIMESTAMP_OUT_OF_RANGE; + } + if (tdInsertRowToTable(pRepo, row, pTable) < 0) { return -1; } @@ -903,6 +917,9 @@ static void *tsdbCommitData(void *arg) { } } + // Do retention actions + tsdbFitRetention(pRepo); + _exit: tdFreeDataCols(pDataCols); tsdbDestroyTableIters(iters, pCfg->maxTables); diff --git a/src/util/inc/tcoding.h b/src/util/inc/tcoding.h new file mode 100644 index 0000000000000000000000000000000000000000..f3e7a5294233b73068197d31608fa9327a1e535e --- /dev/null +++ b/src/util/inc/tcoding.h @@ -0,0 +1,124 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ +#ifndef _TD_CODING_H_ +#define _TD_CODING_H_ + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include + +#include "tutil.h" + +const int TNUMBER = 1; +#define IS_LITTLE_ENDIAN() (*(char *)(&TNUMBER) != 0) + +static FORCE_INLINE void *taosEncodeFixed16(void *buf, uint16_t value) { + if (IS_LITTLE_ENDIAN()) { + memcpy(buf, &value, sizeof(value)); + } else { + ((char *)buf)[0] = value & 0xff; + ((char *)buf)[1] = (value >> 8) & 0xff; + } + + return POINTER_DRIFT(buf, sizeof(value)); +} + +static FORCE_INLINE void *taosEncodeFixed32(void *buf, uint32_t value) { + if (IS_LITTLE_ENDIAN()) { + memcpy(buf, &value, sizeof(value)); + } else { + ((char *)buf)[0] = value & 0xff; + ((char *)buf)[1] = (value >> 8) & 0xff; + ((char *)buf)[2] = (value >> 16) & 0xff; + ((char *)buf)[3] = (value >> 24) & 0xff; + } + + return POINTER_DRIFT(buf, sizeof(value)); +} + +static FORCE_INLINE void *taosEncodeFixed64(void *buf, uint64_t value) { + if (IS_LITTLE_ENDIAN()) { + memcpy(buf, &value, sizeof(value)); + } else { + ((char *)buf)[0] = value & 0xff; + ((char *)buf)[1] = (value >> 8) & 0xff; + ((char *)buf)[2] = (value >> 16) & 0xff; + ((char *)buf)[3] = (value >> 24) & 0xff; + ((char *)buf)[4] = (value >> 32) & 0xff; + ((char *)buf)[5] = (value >> 40) & 0xff; + ((char *)buf)[6] = (value >> 48) & 0xff; + ((char *)buf)[7] = (value >> 56) & 0xff; + } + + return POINTER_DRIFT(buf, sizeof(value)); +} + +static FORCE_INLINE void *taosDecodeFixed16(void *buf, uint16_t *value) { + if (IS_LITTLE_ENDIAN()) { + memcpy(value, buf, sizeof(*value)); + } else { + ((char *)value)[1] = ((char *)buf)[0]; + ((char *)value)[0] = ((char *)buf)[1]; + } + + return POINTER_DRIFT(buf, sizeof(*value)); +} + +static FORCE_INLINE void *taosDecodeFixed32(void *buf, uint32_t *value) { + if (IS_LITTLE_ENDIAN()) { + memcpy(value, buf, sizeof(*value)); + } else { + ((char *)value)[3] = ((char *)buf)[0]; + ((char *)value)[2] = ((char *)buf)[1]; + ((char *)value)[1] = ((char *)buf)[2]; + ((char *)value)[0] = ((char *)buf)[3]; + } + + return POINTER_DRIFT(buf, sizeof(*value)); +} + +static FORCE_INLINE void *taosDecodeFixed64(void *buf, uint64_t *value) { + if (IS_LITTLE_ENDIAN()) { + memcpy(value, buf, sizeof(*value)); + } else { + ((char *)value)[7] = ((char *)buf)[0]; + ((char *)value)[6] = ((char *)buf)[1]; + ((char *)value)[5] = ((char *)buf)[2]; + ((char *)value)[4] = ((char *)buf)[3]; + ((char *)value)[3] = ((char *)buf)[4]; + ((char *)value)[2] = ((char *)buf)[5]; + ((char *)value)[1] = ((char *)buf)[6]; + ((char *)value)[0] = ((char *)buf)[7]; + } + + return POINTER_DRIFT(buf, sizeof(*value)); +} + +// TODO +static FORCE_INLINE void *taosEncodeVariant16(void *buf, uint16_t value) {} +static FORCE_INLINE void *taosEncodeVariant32(void *buf, uint32_t value) {} +static FORCE_INLINE void *taosEncodeVariant64(void *buf, uint64_t value) {} +static FORCE_INLINE void *taosDecodeVariant16(void *buf, uint16_t *value) {} +static FORCE_INLINE void *taosDecodeVariant32(void *buf, uint32_t *value) {} +static FORCE_INLINE void *taosDecodeVariant64(void *buf, uint64_t *value) {} + +#ifdef __cplusplus +} +#endif + +#endif \ No newline at end of file diff --git a/src/util/inc/ttime.h b/src/util/inc/ttime.h index 34c241cbc0f22afc511660cee475c82d08466599..61df65f345afc3085890cdcb8396ddba6a2fe857 100644 --- a/src/util/inc/ttime.h +++ b/src/util/inc/ttime.h @@ -22,22 +22,37 @@ extern "C" { #include #include +#include "tutil.h" //@return timestamp in second int32_t taosGetTimestampSec(); //@return timestamp in millisecond -int64_t taosGetTimestampMs(); +static FORCE_INLINE int64_t taosGetTimestampMs() { + struct timeval systemTime; + gettimeofday(&systemTime, NULL); + return (int64_t)systemTime.tv_sec * 1000L + (uint64_t)systemTime.tv_usec / 1000; +} //@return timestamp in microsecond -int64_t taosGetTimestampUs(); +static FORCE_INLINE int64_t taosGetTimestampUs() { + struct timeval systemTime; + gettimeofday(&systemTime, NULL); + return (int64_t)systemTime.tv_sec * 1000000L + (uint64_t)systemTime.tv_usec; +} /* * @return timestamp decided by global conf variable, tsTimePrecision * if precision == TSDB_TIME_PRECISION_MICRO, it returns timestamp in microsecond. * precision == TSDB_TIME_PRECISION_MILLI, it returns timestamp in millisecond. */ -int64_t taosGetTimestamp(int32_t precision); +static FORCE_INLINE int64_t taosGetTimestamp(int32_t precision) { + if (precision == TSDB_TIME_PRECISION_MICRO) { + return taosGetTimestampUs(); + } else { + return taosGetTimestampMs(); + } +} int32_t getTimestampInUsFromStr(char* token, int32_t tokenlen, int64_t* ts); diff --git a/src/util/src/ttime.c b/src/util/src/ttime.c index 015cb19606ff11b5ddb857a1fc11299f224afd16..d34bf0e6ce922a09eb9941bb8cac9c98141a68cc 100644 --- a/src/util/src/ttime.c +++ b/src/util/src/ttime.c @@ -121,30 +121,6 @@ static int32_t parseLocaltime(char* timestr, int64_t* time, int32_t timePrec); int32_t taosGetTimestampSec() { return (int32_t)time(NULL); } -int64_t taosGetTimestampMs() { - struct timeval systemTime; - gettimeofday(&systemTime, NULL); - return (int64_t)systemTime.tv_sec * 1000L + (uint64_t)systemTime.tv_usec / 1000; -} - -int64_t taosGetTimestampUs() { - struct timeval systemTime; - gettimeofday(&systemTime, NULL); - return (int64_t)systemTime.tv_sec * 1000000L + (uint64_t)systemTime.tv_usec; -} - -/* - * If tsTimePrecision == 1, taosGetTimestamp will return timestamp in microsecond. - * Otherwise, it will return timestamp in millisecond. - */ -int64_t taosGetTimestamp(int32_t precision) { - if (precision == TSDB_TIME_PRECISION_MICRO) { - return taosGetTimestampUs(); - } else { - return taosGetTimestampMs(); - } -} - int32_t taosParseTime(char* timestr, int64_t* time, int32_t len, int32_t timePrec) { /* parse datatime string in with tz */ if (strnchr(timestr, 'T', len, false) != NULL) {