From ae9698e1c71b8509b291145b68094dac07121039 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Thu, 2 Jun 2022 12:52:16 +0000 Subject: [PATCH 01/24] feat: vnode multi-version --- source/dnode/vnode/src/inc/tsdb.h | 2 + source/dnode/vnode/src/tsdb/tsdbCommit.c | 107 +++++++------- source/dnode/vnode/src/tsdb/tsdbMemTable2.c | 154 ++++++++------------ 3 files changed, 120 insertions(+), 143 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index a62b4c4409..ab23b7506e 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -43,6 +43,8 @@ typedef struct SMemTable SMemTable; int32_t tsdbMemTableCreate2(STsdb *pTsdb, SMemTable **ppMemTable); void tsdbMemTableDestroy2(SMemTable *pMemTable); +int32_t tsdbInsertTableData2(STsdb *pTsdb, int64_t version, SVSubmitBlk *pSubmitBlk); +int32_t tsdbDeleteTableData2(STsdb *pTsdb, int64_t version, tb_uid_t suid, tb_uid_t uid, TSKEY sKey, TSKEY eKey); // tsdbMemTable ================ typedef struct STsdbRow STsdbRow; diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit.c b/source/dnode/vnode/src/tsdb/tsdbCommit.c index 86929fe6d5..9e5a5a751b 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit.c @@ -87,6 +87,7 @@ static bool tsdbCanAddSubBlock(SCommitH *pCommith, SBlock *pBlock, SMergeInfo *p static void tsdbLoadAndMergeFromCache(STsdb *pTsdb, SDataCols *pDataCols, int *iter, SCommitIter *pCommitIter, SDataCols *pTarget, TSKEY maxKey, int maxRows, int8_t update); int tsdbWriteBlockIdx(SDFile *pHeadf, SArray *pIdxA, void **ppBuf); +static int tsdbApplyRtnOnFSet(STsdb *pRepo, SDFileSet *pSet, SRtn *pRtn); int tsdbBegin(STsdb *pTsdb) { if (!pTsdb) return 0; @@ -100,57 +101,6 @@ int tsdbBegin(STsdb *pTsdb) { return 0; } -int tsdbApplyRtnOnFSet(STsdb *pRepo, SDFileSet *pSet, SRtn *pRtn) { - SDiskID did; - SDFileSet nSet = {0}; - STsdbFS *pfs = REPO_FS(pRepo); - int level; - - ASSERT(pSet->fid >= pRtn->minFid); - - level = tsdbGetFidLevel(pSet->fid, pRtn); - - if (tfsAllocDisk(pRepo->pVnode->pTfs, level, &did) < 0) { - terrno = TSDB_CODE_TDB_NO_AVAIL_DISK; - return -1; - } - - if (did.level > TSDB_FSET_LEVEL(pSet)) { - // Need to move the FSET to higher level - tsdbInitDFileSet(pRepo, &nSet, did, pSet->fid, FS_TXN_VERSION(pfs)); - - if (tsdbCopyDFileSet(pSet, &nSet) < 0) { - tsdbError("vgId:%d, failed to copy FSET %d from level %d to level %d since %s", REPO_ID(pRepo), pSet->fid, - TSDB_FSET_LEVEL(pSet), did.level, tstrerror(terrno)); - return -1; - } - - if (tsdbUpdateDFileSet(pfs, &nSet) < 0) { - return -1; - } - - tsdbInfo("vgId:%d, FSET %d is copied from level %d disk id %d to level %d disk id %d", REPO_ID(pRepo), pSet->fid, - TSDB_FSET_LEVEL(pSet), TSDB_FSET_ID(pSet), did.level, did.id); - } else { - // On a correct level - if (tsdbUpdateDFileSet(pfs, pSet) < 0) { - return -1; - } - } - - return 0; -} - -int tsdbPrepareCommit(STsdb *pTsdb) { - if (pTsdb->mem == NULL) return 0; - - ASSERT(pTsdb->imem == NULL); - - pTsdb->imem = pTsdb->mem; - pTsdb->mem = NULL; - return 0; -} - int tsdbCommit(STsdb *pRepo) { SCommitH commith = {0}; SDFileSet *pSet = NULL; @@ -223,6 +173,57 @@ int tsdbCommit(STsdb *pRepo) { return 0; } +static int tsdbApplyRtnOnFSet(STsdb *pRepo, SDFileSet *pSet, SRtn *pRtn) { + SDiskID did; + SDFileSet nSet = {0}; + STsdbFS *pfs = REPO_FS(pRepo); + int level; + + ASSERT(pSet->fid >= pRtn->minFid); + + level = tsdbGetFidLevel(pSet->fid, pRtn); + + if (tfsAllocDisk(pRepo->pVnode->pTfs, level, &did) < 0) { + terrno = TSDB_CODE_TDB_NO_AVAIL_DISK; + return -1; + } + + if (did.level > TSDB_FSET_LEVEL(pSet)) { + // Need to move the FSET to higher level + tsdbInitDFileSet(pRepo, &nSet, did, pSet->fid, FS_TXN_VERSION(pfs)); + + if (tsdbCopyDFileSet(pSet, &nSet) < 0) { + tsdbError("vgId:%d, failed to copy FSET %d from level %d to level %d since %s", REPO_ID(pRepo), pSet->fid, + TSDB_FSET_LEVEL(pSet), did.level, tstrerror(terrno)); + return -1; + } + + if (tsdbUpdateDFileSet(pfs, &nSet) < 0) { + return -1; + } + + tsdbInfo("vgId:%d, FSET %d is copied from level %d disk id %d to level %d disk id %d", REPO_ID(pRepo), pSet->fid, + TSDB_FSET_LEVEL(pSet), TSDB_FSET_ID(pSet), did.level, did.id); + } else { + // On a correct level + if (tsdbUpdateDFileSet(pfs, pSet) < 0) { + return -1; + } + } + + return 0; +} + +int tsdbPrepareCommit(STsdb *pTsdb) { + if (pTsdb->mem == NULL) return 0; + + ASSERT(pTsdb->imem == NULL); + + pTsdb->imem = pTsdb->mem; + pTsdb->mem = NULL; + return 0; +} + void tsdbGetRtnSnap(STsdb *pRepo, SRtn *pRtn) { STsdbKeepCfg *pCfg = REPO_KEEP_CFG(pRepo); TSKEY minKey, midKey, maxKey, now; @@ -543,8 +544,8 @@ static int tsdbSetAndOpenCommitFile(SCommitH *pCommith, SDFileSet *pSet, int fid return -1; } - tsdbDebug("vgId:%d, FSET %d at level %d disk id %d is opened to read to commit", REPO_ID(pRepo), TSDB_FSET_FID(pSet), - TSDB_FSET_LEVEL(pSet), TSDB_FSET_ID(pSet)); + tsdbDebug("vgId:%d, FSET %d at level %d disk id %d is opened to read to commit", REPO_ID(pRepo), + TSDB_FSET_FID(pSet), TSDB_FSET_LEVEL(pSet), TSDB_FSET_ID(pSet)); } else { pCommith->isRFileSet = false; } diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c index f4d4d94142..3bad5fdd92 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c @@ -64,9 +64,10 @@ static int memDataPCmprFn(const void *p1, const void *p2); static int32_t tPutTSDBRow(uint8_t *p, TSDBROW *pRow); static int32_t tGetTSDBRow(uint8_t *p, TSDBROW *pRow); static int8_t tsdbMemSkipListRandLevel(SMemSkipList *pSl); -static void memDataMovePos(SMemData *pMemData, TSDBROW *pRow, int8_t isForward, SMemSkipListNode **pos); -static int32_t memDataPutRow(SVBufPool *pPool, SMemData *pMemData, TSDBROW *pRow, int8_t isForward, - SMemSkipListNode **pos); +static void memDataGetPosToPut(SMemData *pMemData, SMemSkipListNode **backward, TSDBROW *pRow); +static int32_t memDataDoPut(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, int8_t isForward); +static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, int64_t version, + SVSubmitBlk *pSubmitBlk); // SMemTable ============================================== int32_t tsdbMemTableCreate2(STsdb *pTsdb, SMemTable **ppMemTable) { @@ -123,28 +124,9 @@ int32_t tsdbInsertTableData2(STsdb *pTsdb, int64_t version, SVSubmitBlk *pSubmit } // do insert - int32_t nt; - int32_t n = 0; - uint8_t *p = pSubmitBlk->pData; - int32_t nRow = 0; - SMemSkipListNode *pos[SL_MAX_LEVEL] = {0}; - - for (int8_t iLevel = 0; iLevel < SL_MAX_LEVEL; iLevel++) { - pos[iLevel] = pMemData->sl.pTail; - } - while (n < pSubmitBlk->nData) { - nt = tGetTSRow(p + n, &row.tsRow); - n += nt; - - ASSERT(n <= pSubmitBlk->nData); - - memDataMovePos(pMemData, &row, nRow ? 1 : 0, pos); - code = memDataPutRow(pTsdb->pVnode->inUse, pMemData, &row, nRow ? 1 : 0, pos); - if (code) { - goto _err; - } - - nRow++; + code = tsdbInsertTableDataImpl(pMemTable, pMemData, version, pSubmitBlk); + if (code) { + goto _err; } return code; @@ -310,87 +292,79 @@ static FORCE_INLINE int8_t tsdbMemSkipListRandLevel(SMemSkipList *pSl) { return level; } -static void memDataMovePos(SMemData *pMemData, TSDBROW *pRow, int8_t isForward, SMemSkipListNode **pos) { - TSDBKEY *pKey; - int c; +static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, int64_t version, + SVSubmitBlk *pSubmitBlk) { + int32_t code = 0; + int32_t n = 0; + uint8_t *p = pSubmitBlk->pData; + int32_t nRow = 0; + TSDBROW row = {.version = version}; + + SMemSkipListNode *backward[SL_MAX_LEVEL]; + SMemSkipListNode *forward[SL_MAX_LEVEL]; + + ASSERT(pSubmitBlk->nData); + + // backward put first data + n += tGetTSRow(p + n, &row.tsRow); + ASSERT(n <= pSubmitBlk->nData); + + memDataGetPosToPut(pMemData, backward, &row); + code = memDataDoPut(pMemData, backward, &row, 0); + if (code) { + goto _exit; + } + nRow++; + + // forward put rest + while (n < pSubmitBlk->nData) { + n += tGetTSRow(p + n, &row.tsRow); + ASSERT(n <= pSubmitBlk->nData); - if (isForward) { // TODO - } else { - SMemSkipListNode *px = pMemData->sl.pTail; + nRow++; + } + +_exit: + return code; +} + +static void memDataGetPosToPut(SMemData *pMemData, SMemSkipListNode **backward, TSDBROW *pRow) { + SMemSkipListNode *px; + SMemSkipListNode *pn; + TSDBKEY *pKey; + int c; + + for (int8_t iLevel = 0; iLevel < pMemData->sl.maxLevel - 1; iLevel++) { + backward[iLevel] = pMemData->sl.pTail; + } + + if (0) { + } else { + px = pMemData->sl.pTail; for (int8_t iLevel = pMemData->sl.maxLevel - 1; iLevel >= 0; iLevel--) { if (iLevel < pMemData->sl.level) { - SMemSkipListNode *p = SL_NODE_BACKWARD(px, iLevel); - - while (p != pMemData->sl.pHead) { - pKey = (TSDBKEY *)SL_NODE_DATA(p); + pn = SL_NODE_BACKWARD(px, iLevel); + while (pn != pMemData->sl.pHead) { + pKey = (TSDBKEY *)SL_NODE_DATA(pn); c = tsdbKeyCmprFn(pKey, pRow); if (c <= 0) { break; } else { - px = p; - p = SL_NODE_BACKWARD(px, iLevel); + px = pn; + pn = SL_NODE_BACKWARD(px, iLevel); } } - - pos[iLevel] = px; } + backward[iLevel] = px; } } } -static void memMovePosFrom(SMemData *pMemData, SMemSkipListNode *pNode, TSDBROW *pRow, int8_t isForward, - SMemSkipListNode **pos) { - SMemSkipListNode *px = pNode; - TSDBKEY *pKey; - SMemSkipListNode *p; - int c; - - if (isForward) { - } else { - ASSERT(pNode != pMemData->sl.pHead); - - for (int8_t iLevel = pMemData->sl.maxLevel - 1; iLevel >= 0; iLevel--) { - p = SL_NODE_BACKWARD(px, iLevel); - while (p != pMemData->sl.pHead) { - pKey = (TSDBKEY *)SL_NODE_DATA(p); - - c = tsdbKeyCmprFn(pKey, pRow); - if (c <= 0) { - break; - } else { - px = p; - p = SL_NODE_BACKWARD(px, iLevel); - } - } - - pos[iLevel] = px; - } - } -} - -static int32_t memDataPutRow(SVBufPool *pPool, SMemData *pMemData, TSDBROW *pRow, int8_t isForward, - SMemSkipListNode **pos) { - int32_t code = 0; - int8_t level; - SMemSkipListNode *pNode; - - level = tsdbMemSkipListRandLevel(&pMemData->sl); - pNode = (SMemSkipListNode *)vnodeBufPoolMalloc(pPool, SL_NODE_SIZE(level) + tPutTSDBRow(NULL, pRow)); - if (pNode == NULL) { - code = TSDB_CODE_OUT_OF_MEMORY; - goto _exit; - } - - // do the read put - if (isForward) { - // TODO - } else { - // TODO - } - -_exit: +static int32_t memDataDoPut(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, int8_t isForward) { + int32_t code = 0; + // TODO return code; } \ No newline at end of file From 802f6d246c93ae5110b9390474840a235ffe83be Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 01:26:05 +0000 Subject: [PATCH 02/24] more --- include/common/tdataformat.h | 3 +++ source/common/src/tdataformat.c | 7 +++++-- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/include/common/tdataformat.h b/include/common/tdataformat.h index 073d796717..6a73d3d678 100644 --- a/include/common/tdataformat.h +++ b/include/common/tdataformat.h @@ -41,6 +41,9 @@ typedef struct STag STag; int32_t tTSchemaCreate(int32_t sver, SSchema *pSchema, int32_t nCols, STSchema **ppTSchema); void tTSchemaDestroy(STSchema *pTSchema); +// SValue +int tValueCmprFn(const SValue *pValue1, const SValue *pValue2, int8_t type); + // STSRow2 #define COL_VAL_NONE(CID) ((SColVal){.cid = (CID), .isNone = 1}) #define COL_VAL_NULL(CID) ((SColVal){.cid = (CID), .isNull = 1}) diff --git a/source/common/src/tdataformat.c b/source/common/src/tdataformat.c index 287dba6d3b..9aa94b0216 100644 --- a/source/common/src/tdataformat.c +++ b/source/common/src/tdataformat.c @@ -36,8 +36,6 @@ typedef struct { #define GET_BIT1(p, i) (((p)[(i) / 8] >> ((i) % 8)) & ((uint8_t)1)) #define GET_BIT2(p, i) (((p)[(i) / 4] >> ((i) % 4)) & ((uint8_t)3)) -static FORCE_INLINE int tSKVIdxCmprFn(const void *p1, const void *p2); - // SValue static FORCE_INLINE int32_t tPutValue(uint8_t *p, SValue *pValue, int8_t type) { int32_t n = 0; @@ -141,6 +139,11 @@ static FORCE_INLINE int32_t tGetValue(uint8_t *p, SValue *pValue, int8_t type) { return n; } +int tValueCmprFn(const SValue *pValue1, const SValue *pValue2, int8_t type) { + // TODO + return 0; +} + // STSRow2 ======================================================================== static void setBitMap(uint8_t *pb, uint8_t v, int32_t idx, uint8_t flags) { if (pb) { From c568aeaa4374e3421d153b4aea5b23de38aaa1ef Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 03:41:13 +0000 Subject: [PATCH 03/24] more --- source/dnode/vnode/src/tsdb/tsdbMemTable2.c | 42 ++++++++++++++------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c index 3bad5fdd92..d32caed77b 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c @@ -64,8 +64,8 @@ static int memDataPCmprFn(const void *p1, const void *p2); static int32_t tPutTSDBRow(uint8_t *p, TSDBROW *pRow); static int32_t tGetTSDBRow(uint8_t *p, TSDBROW *pRow); static int8_t tsdbMemSkipListRandLevel(SMemSkipList *pSl); -static void memDataGetPosToPut(SMemData *pMemData, SMemSkipListNode **backward, TSDBROW *pRow); -static int32_t memDataDoPut(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, int8_t isForward); +static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, int8_t forward); +static int32_t memDataDoPut(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, int8_t forward); static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, int64_t version, SVSubmitBlk *pSubmitBlk); @@ -300,8 +300,7 @@ static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, int32_t nRow = 0; TSDBROW row = {.version = version}; - SMemSkipListNode *backward[SL_MAX_LEVEL]; - SMemSkipListNode *forward[SL_MAX_LEVEL]; + SMemSkipListNode *pos[SL_MAX_LEVEL]; ASSERT(pSubmitBlk->nData); @@ -309,8 +308,8 @@ static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, n += tGetTSRow(p + n, &row.tsRow); ASSERT(n <= pSubmitBlk->nData); - memDataGetPosToPut(pMemData, backward, &row); - code = memDataDoPut(pMemData, backward, &row, 0); + memDataMovePosTo(pMemData, pos, &row, 0); + code = memDataDoPut(pMemData, pos, &row, 0); if (code) { goto _exit; } @@ -322,6 +321,11 @@ static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, ASSERT(n <= pSubmitBlk->nData); // TODO + memDataMovePosTo(pMemData, pos, &row, 1); + code = memDataDoPut(pMemData, pos, &row, 1); + if (code) { + goto _exit; + } nRow++; } @@ -330,17 +334,29 @@ _exit: return code; } -static void memDataGetPosToPut(SMemData *pMemData, SMemSkipListNode **backward, TSDBROW *pRow) { +static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, int8_t forward) { SMemSkipListNode *px; SMemSkipListNode *pn; TSDBKEY *pKey; int c; - for (int8_t iLevel = 0; iLevel < pMemData->sl.maxLevel - 1; iLevel++) { - backward[iLevel] = pMemData->sl.pTail; - } + if (forward) { + px = pMemData->sl.pHead; + for (int8_t iLevel = pMemData->sl.maxLevel - 1; iLevel >= 0; iLevel--) { + pn = SL_NODE_FORWARD(px, iLevel); + while (pn != pMemData->sl.pTail) { + pKey = (TSDBKEY *)SL_NODE_DATA(pn); - if (0) { + c = tsdbKeyCmprFn(pKey, pRow); + if (pKey >= 0) { + break; + } else { + px = pn; + pn = SL_NODE_FORWARD(px, iLevel); + } + } + pos[iLevel] = px; + } } else { px = pMemData->sl.pTail; for (int8_t iLevel = pMemData->sl.maxLevel - 1; iLevel >= 0; iLevel--) { @@ -358,12 +374,12 @@ static void memDataGetPosToPut(SMemData *pMemData, SMemSkipListNode **backward, } } } - backward[iLevel] = px; + pos[iLevel] = px; } } } -static int32_t memDataDoPut(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, int8_t isForward) { +static int32_t memDataDoPut(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, int8_t forward) { int32_t code = 0; // TODO return code; From 9fe400f20719e7bfa9dc9892c06d6a13fc4f7342 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 03:52:26 +0000 Subject: [PATCH 04/24] more --- source/dnode/vnode/src/tsdb/tsdbMemTable2.c | 55 +++++++++++---------- 1 file changed, 30 insertions(+), 25 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c index d32caed77b..78be665ae9 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c @@ -59,12 +59,15 @@ struct SMemTable { #define SL_NODE_BACKWARD(n, l) ((n)->forwards[(n)->level + (l)]) #define SL_NODE_DATA(n) (&SL_NODE_BACKWARD(n, (n)->level)) +#define SL_MOVE_BACKWARD 0x1 +#define SL_MOVE_FROM_POS 0x2 + static int32_t tsdbGetOrCreateMemData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, SMemData **ppMemData); static int memDataPCmprFn(const void *p1, const void *p2); static int32_t tPutTSDBRow(uint8_t *p, TSDBROW *pRow); static int32_t tGetTSDBRow(uint8_t *p, TSDBROW *pRow); static int8_t tsdbMemSkipListRandLevel(SMemSkipList *pSl); -static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, int8_t forward); +static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBKEY *pKey, int32_t flags); static int32_t memDataDoPut(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, int8_t forward); static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, int64_t version, SVSubmitBlk *pSubmitBlk); @@ -308,7 +311,7 @@ static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, n += tGetTSRow(p + n, &row.tsRow); ASSERT(n <= pSubmitBlk->nData); - memDataMovePosTo(pMemData, pos, &row, 0); + memDataMovePosTo(pMemData, pos, &(TSDBKEY){.version = version, .ts = row.tsRow.ts}, SL_MOVE_BACKWARD); code = memDataDoPut(pMemData, pos, &row, 0); if (code) { goto _exit; @@ -321,7 +324,7 @@ static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, ASSERT(n <= pSubmitBlk->nData); // TODO - memDataMovePosTo(pMemData, pos, &row, 1); + memDataMovePosTo(pMemData, pos, &(TSDBKEY){.version = version, .ts = row.tsRow.ts}, SL_MOVE_FROM_POS); code = memDataDoPut(pMemData, pos, &row, 1); if (code) { goto _exit; @@ -334,38 +337,23 @@ _exit: return code; } -static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, int8_t forward) { +static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBKEY *pKey, int32_t flags) { SMemSkipListNode *px; SMemSkipListNode *pn; - TSDBKEY *pKey; + TSDBKEY *pTKey; int c; + int backward = flags & SL_MOVE_BACKWARD; + int fromPos = flags & SL_MOVE_FROM_POS; - if (forward) { - px = pMemData->sl.pHead; - for (int8_t iLevel = pMemData->sl.maxLevel - 1; iLevel >= 0; iLevel--) { - pn = SL_NODE_FORWARD(px, iLevel); - while (pn != pMemData->sl.pTail) { - pKey = (TSDBKEY *)SL_NODE_DATA(pn); - - c = tsdbKeyCmprFn(pKey, pRow); - if (pKey >= 0) { - break; - } else { - px = pn; - pn = SL_NODE_FORWARD(px, iLevel); - } - } - pos[iLevel] = px; - } - } else { + if (backward) { px = pMemData->sl.pTail; for (int8_t iLevel = pMemData->sl.maxLevel - 1; iLevel >= 0; iLevel--) { if (iLevel < pMemData->sl.level) { pn = SL_NODE_BACKWARD(px, iLevel); while (pn != pMemData->sl.pHead) { - pKey = (TSDBKEY *)SL_NODE_DATA(pn); + pTKey = (TSDBKEY *)SL_NODE_DATA(pn); - c = tsdbKeyCmprFn(pKey, pRow); + c = tsdbKeyCmprFn(pTKey, pKey); if (c <= 0) { break; } else { @@ -376,6 +364,23 @@ static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW } pos[iLevel] = px; } + } else { + px = pMemData->sl.pHead; + for (int8_t iLevel = pMemData->sl.maxLevel - 1; iLevel >= 0; iLevel--) { + pn = SL_NODE_FORWARD(px, iLevel); + while (pn != pMemData->sl.pTail) { + pTKey = (TSDBKEY *)SL_NODE_DATA(pn); + + c = tsdbKeyCmprFn(pTKey, pKey); + if (pTKey >= 0) { + break; + } else { + px = pn; + pn = SL_NODE_FORWARD(px, iLevel); + } + } + pos[iLevel] = px; + } } } From 2b022f665c01d330f30d6a33fb3708c467e72084 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 04:22:00 +0000 Subject: [PATCH 05/24] more --- source/dnode/vnode/src/tsdb/tsdbMemTable2.c | 48 ++++++++++++++------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c index 78be665ae9..3b2d97092d 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c @@ -347,8 +347,15 @@ static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBKEY if (backward) { px = pMemData->sl.pTail; - for (int8_t iLevel = pMemData->sl.maxLevel - 1; iLevel >= 0; iLevel--) { - if (iLevel < pMemData->sl.level) { + + for (int8_t iLevel = pMemData->sl.maxLevel - 1; iLevel >= pMemData->sl.level; iLevel--) { + pos[iLevel] = px; + } + + if (pMemData->sl.level) { + if (fromPos) px = pos[pMemData->sl.level - 1]; + + for (int8_t iLevel = pMemData->sl.level - 1; iLevel >= 0; iLevel--) { pn = SL_NODE_BACKWARD(px, iLevel); while (pn != pMemData->sl.pHead) { pTKey = (TSDBKEY *)SL_NODE_DATA(pn); @@ -361,26 +368,37 @@ static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBKEY pn = SL_NODE_BACKWARD(px, iLevel); } } + + pos[iLevel] = px; } - pos[iLevel] = px; } } else { px = pMemData->sl.pHead; - for (int8_t iLevel = pMemData->sl.maxLevel - 1; iLevel >= 0; iLevel--) { - pn = SL_NODE_FORWARD(px, iLevel); - while (pn != pMemData->sl.pTail) { - pTKey = (TSDBKEY *)SL_NODE_DATA(pn); - c = tsdbKeyCmprFn(pTKey, pKey); - if (pTKey >= 0) { - break; - } else { - px = pn; - pn = SL_NODE_FORWARD(px, iLevel); - } - } + for (int8_t iLevel = pMemData->sl.maxLevel - 1; iLevel >= pMemData->sl.level; iLevel--) { pos[iLevel] = px; } + + if (pMemData->sl.level) { + if (fromPos) px = pos[pMemData->sl.level - 1]; + + for (int8_t iLevel = pMemData->sl.level - 1; iLevel >= 0; iLevel--) { + pn = SL_NODE_FORWARD(px, iLevel); + while (pn != pMemData->sl.pHead) { + pTKey = (TSDBKEY *)SL_NODE_DATA(pn); + + c = tsdbKeyCmprFn(pTKey, pKey); + if (c >= 0) { + break; + } else { + px = pn; + pn = SL_NODE_FORWARD(px, iLevel); + } + } + + pos[iLevel] = px; + } + } } } From a0aa003f89a6497b0f4d44d5e5db2940d745ec3c Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 06:29:01 +0000 Subject: [PATCH 06/24] more --- source/dnode/vnode/src/tsdb/tsdbMemTable2.c | 126 ++++++++++++-------- 1 file changed, 78 insertions(+), 48 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c index 3b2d97092d..d067130a12 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c @@ -67,8 +67,6 @@ static int memDataPCmprFn(const void *p1, const void *p2); static int32_t tPutTSDBRow(uint8_t *p, TSDBROW *pRow); static int32_t tGetTSDBRow(uint8_t *p, TSDBROW *pRow); static int8_t tsdbMemSkipListRandLevel(SMemSkipList *pSl); -static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBKEY *pKey, int32_t flags); -static int32_t memDataDoPut(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, int8_t forward); static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, int64_t version, SVSubmitBlk *pSubmitBlk); @@ -295,48 +293,6 @@ static FORCE_INLINE int8_t tsdbMemSkipListRandLevel(SMemSkipList *pSl) { return level; } -static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, int64_t version, - SVSubmitBlk *pSubmitBlk) { - int32_t code = 0; - int32_t n = 0; - uint8_t *p = pSubmitBlk->pData; - int32_t nRow = 0; - TSDBROW row = {.version = version}; - - SMemSkipListNode *pos[SL_MAX_LEVEL]; - - ASSERT(pSubmitBlk->nData); - - // backward put first data - n += tGetTSRow(p + n, &row.tsRow); - ASSERT(n <= pSubmitBlk->nData); - - memDataMovePosTo(pMemData, pos, &(TSDBKEY){.version = version, .ts = row.tsRow.ts}, SL_MOVE_BACKWARD); - code = memDataDoPut(pMemData, pos, &row, 0); - if (code) { - goto _exit; - } - nRow++; - - // forward put rest - while (n < pSubmitBlk->nData) { - n += tGetTSRow(p + n, &row.tsRow); - ASSERT(n <= pSubmitBlk->nData); - - // TODO - memDataMovePosTo(pMemData, pos, &(TSDBKEY){.version = version, .ts = row.tsRow.ts}, SL_MOVE_FROM_POS); - code = memDataDoPut(pMemData, pos, &row, 1); - if (code) { - goto _exit; - } - - nRow++; - } - -_exit: - return code; -} - static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBKEY *pKey, int32_t flags) { SMemSkipListNode *px; SMemSkipListNode *pn; @@ -402,8 +358,82 @@ static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBKEY } } -static int32_t memDataDoPut(SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, int8_t forward) { - int32_t code = 0; - // TODO +static int32_t memDataDoPut(SMemTable *pMemTable, SMemData *pMemData, SMemSkipListNode **pos, TSDBROW *pRow, + int8_t forward) { + int32_t code = 0; + int8_t level; + SMemSkipListNode *pNode; + SVBufPool *pPool = pMemTable->pTsdb->pVnode->inUse; + + // node + level = tsdbMemSkipListRandLevel(&pMemData->sl); + pNode = (SMemSkipListNode *)vnodeBufPoolMalloc(pPool, SL_NODE_SIZE(level) + tPutTSDBRow(NULL, pRow)); + if (pNode == NULL) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _exit; + } + pNode->level = level; + for (int8_t iLevel = 0; iLevel < level; iLevel++) { + SL_NODE_FORWARD(pNode, iLevel) = NULL; + SL_NODE_BACKWARD(pNode, iLevel) = NULL; + } + + tPutTSDBRow((uint8_t *)SL_NODE_DATA(pNode), pRow); + + // put + for (int8_t iLevel = 0; iLevel < pNode->level; iLevel++) { + /* code */ + } + + pMemData->sl.size++; + if (pMemData->sl.level < pNode->level) { + pMemData->sl.level = pNode->level; + } + +_exit: return code; -} \ No newline at end of file +} + +static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, int64_t version, + SVSubmitBlk *pSubmitBlk) { + int32_t code = 0; + int32_t n = 0; + uint8_t *p = pSubmitBlk->pData; + int32_t nRow = 0; + TSDBROW row = {.version = version}; + + SMemSkipListNode *pos[SL_MAX_LEVEL]; + + ASSERT(pSubmitBlk->nData); + + // backward put first data + n += tGetTSRow(p + n, &row.tsRow); + ASSERT(n <= pSubmitBlk->nData); + + memDataMovePosTo(pMemData, pos, &(TSDBKEY){.version = version, .ts = row.tsRow.ts}, SL_MOVE_BACKWARD); + code = memDataDoPut(pMemTable, pMemData, pos, &row, 0); + if (code) { + goto _exit; + } + nRow++; + + // forward put rest + for (int8_t iLevel = 0; iLevel < pMemData->sl.maxLevel; iLevel++) { + pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel); + } + while (n < pSubmitBlk->nData) { + n += tGetTSRow(p + n, &row.tsRow); + ASSERT(n <= pSubmitBlk->nData); + + memDataMovePosTo(pMemData, pos, &(TSDBKEY){.version = version, .ts = row.tsRow.ts}, SL_MOVE_FROM_POS); + code = memDataDoPut(pMemTable, pMemData, pos, &row, 1); + if (code) { + goto _exit; + } + + nRow++; + } + +_exit: + return code; +} From f439918d51281cb372a610e2c58599452a0848d6 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 06:46:22 +0000 Subject: [PATCH 07/24] more --- source/dnode/vnode/src/tsdb/tsdbMemTable2.c | 20 +++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c index d067130a12..866bd69cda 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c @@ -382,7 +382,25 @@ static int32_t memDataDoPut(SMemTable *pMemTable, SMemData *pMemData, SMemSkipLi // put for (int8_t iLevel = 0; iLevel < pNode->level; iLevel++) { - /* code */ + SMemSkipListNode *px = pos[iLevel]; + + if (forward) { + SMemSkipListNode *pNext = SL_NODE_FORWARD(px, iLevel); + + SL_NODE_FORWARD(pNode, iLevel) = pNext; + SL_NODE_BACKWARD(pNode, iLevel) = px; + + SL_NODE_BACKWARD(pNext, iLevel) = pNode; + SL_NODE_FORWARD(px, iLevel) = pNode; + } else { + SMemSkipListNode *pPrev = SL_NODE_BACKWARD(px, iLevel); + + SL_NODE_FORWARD(pNode, iLevel) = px; + SL_NODE_BACKWARD(pNode, iLevel) = pPrev; + + SL_NODE_FORWARD(pPrev, iLevel) = pNode; + SL_NODE_BACKWARD(px, iLevel) = pNode; + } } pMemData->sl.size++; From 23494b8ff10f07288c992512ccd1489808b80701 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 06:57:10 +0000 Subject: [PATCH 08/24] more --- source/dnode/vnode/src/tsdb/tsdbMemTable2.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c index 866bd69cda..b6d0747fe3 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c @@ -435,6 +435,13 @@ static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, } nRow++; + if (tsdbKeyCmprFn((TSDBKEY *)&row, &pMemData->minKey) < 0) { + pMemData->minKey = *(TSDBKEY *)&row; + } + if (tsdbKeyCmprFn((TSDBKEY *)&row, &pMemTable->minKey) < 0) { + pMemTable->minKey = *(TSDBKEY *)&row; + } + // forward put rest for (int8_t iLevel = 0; iLevel < pMemData->sl.maxLevel; iLevel++) { pos[iLevel] = SL_NODE_BACKWARD(pos[iLevel], iLevel); @@ -452,6 +459,14 @@ static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, nRow++; } + if (tsdbKeyCmprFn((TSDBKEY *)&row, &pMemData->maxKey) > 0) { + pMemData->maxKey = *(TSDBKEY *)&row; + } + if (tsdbKeyCmprFn((TSDBKEY *)&row, &pMemTable->maxKey) > 0) { + pMemTable->maxKey = *(TSDBKEY *)&row; + } + pMemTable->nRows += nRow; + _exit: return code; } From c7c47788f35fb67c46c94065d378b1cf370f5f80 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 07:38:27 +0000 Subject: [PATCH 09/24] more --- source/dnode/vnode/src/inc/vnodeInt.h | 4 +-- source/dnode/vnode/src/tsdb/tsdbCommit.c | 40 +++++++++++++----------- 2 files changed, 24 insertions(+), 20 deletions(-) diff --git a/source/dnode/vnode/src/inc/vnodeInt.h b/source/dnode/vnode/src/inc/vnodeInt.h index d3b5f29aac..c4da909c21 100644 --- a/source/dnode/vnode/src/inc/vnodeInt.h +++ b/source/dnode/vnode/src/inc/vnodeInt.h @@ -87,7 +87,7 @@ int metaAlterSTable(SMeta* pMeta, int64_t version, SVCreateStbReq* p int metaDropSTable(SMeta* pMeta, int64_t verison, SVDropStbReq* pReq); int metaCreateTable(SMeta* pMeta, int64_t version, SVCreateTbReq* pReq); int metaDropTable(SMeta* pMeta, int64_t version, SVDropTbReq* pReq, SArray* tbUids); -int metaAlterTable(SMeta* pMeta, int64_t version, SVAlterTbReq* pReq, STableMetaRsp *pMetaRsp); +int metaAlterTable(SMeta* pMeta, int64_t version, SVAlterTbReq* pReq, STableMetaRsp* pMetaRsp); SSchemaWrapper* metaGetTableSchema(SMeta* pMeta, tb_uid_t uid, int32_t sver, bool isinline); STSchema* metaGetTbTSchema(SMeta* pMeta, tb_uid_t uid, int32_t sver); int metaGetTableEntryByName(SMetaReader* pReader, const char* name); @@ -112,7 +112,7 @@ int32_t metaDropTSma(SMeta* pMeta, int64_t indexUid); int tsdbOpen(SVnode* pVnode, STsdb** ppTsdb, const char* dir, STsdbKeepCfg* pKeepCfg); int tsdbClose(STsdb** pTsdb); int tsdbBegin(STsdb* pTsdb); -int tsdbCommit(STsdb* pTsdb); +int32_t tsdbCommit(STsdb* pTsdb); int tsdbScanAndConvertSubmitMsg(STsdb* pTsdb, SSubmitReq* pMsg); int tsdbInsertData(STsdb* pTsdb, int64_t version, SSubmitReq* pMsg, SSubmitRsp* pRsp); int tsdbInsertTableData(STsdb* pTsdb, SSubmitMsgIter* pMsgIter, SSubmitBlk* pBlock, SSubmitBlkRsp* pRsp); diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit.c b/source/dnode/vnode/src/tsdb/tsdbCommit.c index 9e5a5a751b..928915a36d 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit.c @@ -86,7 +86,7 @@ static void tsdbCloseCommitFile(SCommitH *pCommith, bool hasError); static bool tsdbCanAddSubBlock(SCommitH *pCommith, SBlock *pBlock, SMergeInfo *pInfo); static void tsdbLoadAndMergeFromCache(STsdb *pTsdb, SDataCols *pDataCols, int *iter, SCommitIter *pCommitIter, SDataCols *pTarget, TSKEY maxKey, int maxRows, int8_t update); -int tsdbWriteBlockIdx(SDFile *pHeadf, SArray *pIdxA, void **ppBuf); +static int tsdbWriteBlockIdx(SDFile *pHeadf, SArray *pIdxA, void **ppBuf); static int tsdbApplyRtnOnFSet(STsdb *pRepo, SDFileSet *pSet, SRtn *pRtn); int tsdbBegin(STsdb *pTsdb) { @@ -101,33 +101,36 @@ int tsdbBegin(STsdb *pTsdb) { return 0; } -int tsdbCommit(STsdb *pRepo) { +int32_t tsdbCommit(STsdb *pTsdb) { + int32_t code = 0; SCommitH commith = {0}; SDFileSet *pSet = NULL; int fid; - // if (pRepo->imem == NULL) return 0; - pRepo->imem = pRepo->mem; - pRepo->mem = NULL; + ASSERT(pTsdb->imem == NULL && pTsdb->mem); + pTsdb->imem = pTsdb->mem; + pTsdb->mem = NULL; - tsdbStartCommit(pRepo); - // Resource initialization - if (tsdbInitCommitH(&commith, pRepo) < 0) { + // start commit + tsdbStartCommit(pTsdb); + if (tsdbInitCommitH(&commith, pTsdb) < 0) { return -1; } +#if 0 // Skip expired memory data and expired FSET tsdbSeekCommitIter(&commith, commith.rtn.minKey); while ((pSet = tsdbFSIterNext(&(commith.fsIter)))) { if (pSet->fid < commith.rtn.minFid) { - tsdbInfo("vgId:%d, FSET %d on level %d disk id %d expires, remove it", REPO_ID(pRepo), pSet->fid, + tsdbInfo("vgId:%d, FSET %d on level %d disk id %d expires, remove it", REPO_ID(pTsdb), pSet->fid, TSDB_FSET_LEVEL(pSet), TSDB_FSET_ID(pSet)); } else { break; } } +#endif - // Loop to commit to each file + // commit fid = tsdbNextCommitFid(&(commith)); while (true) { // Loop over both on disk and memory @@ -136,7 +139,7 @@ int tsdbCommit(STsdb *pRepo) { if (pSet && (fid == TSDB_IVLD_FID || pSet->fid < fid)) { // Only has existing FSET but no memory data to commit in this // existing FSET, only check if file in correct retention - if (tsdbApplyRtnOnFSet(pRepo, pSet, &(commith.rtn)) < 0) { + if (tsdbApplyRtnOnFSet(pTsdb, pSet, &(commith.rtn)) < 0) { tsdbDestroyCommitH(&commith); return -1; } @@ -167,10 +170,11 @@ int tsdbCommit(STsdb *pRepo) { } } + // end commit tsdbDestroyCommitH(&commith); - tsdbEndCommit(pRepo, TSDB_CODE_SUCCESS); + tsdbEndCommit(pTsdb, TSDB_CODE_SUCCESS); - return 0; + return code; } static int tsdbApplyRtnOnFSet(STsdb *pRepo, SDFileSet *pSet, SRtn *pRtn) { @@ -717,8 +721,8 @@ static int tsdbSetAndOpenCommitFile(SCommitH *pCommith, SDFileSet *pSet, int fid // extern int32_t tsTsdbMetaCompactRatio; -int tsdbWriteBlockInfoImpl(SDFile *pHeadf, STable *pTable, SArray *pSupA, SArray *pSubA, void **ppBuf, - SBlockIdx *pIdx) { +static int tsdbWriteBlockInfoImpl(SDFile *pHeadf, STable *pTable, SArray *pSupA, SArray *pSubA, void **ppBuf, + SBlockIdx *pIdx) { size_t nSupBlocks; size_t nSubBlocks; uint32_t tlen; @@ -778,7 +782,7 @@ int tsdbWriteBlockInfoImpl(SDFile *pHeadf, STable *pTable, SArray *pSupA, SArray return 0; } -int tsdbWriteBlockIdx(SDFile *pHeadf, SArray *pIdxA, void **ppBuf) { +static int tsdbWriteBlockIdx(SDFile *pHeadf, SArray *pIdxA, void **ppBuf) { SBlockIdx *pBlkIdx; size_t nidx = taosArrayGetSize(pIdxA); int tlen = 0, size; @@ -1012,8 +1016,8 @@ static int tsdbComparKeyBlock(const void *arg1, const void *arg2) { * @param ppExBuf * @return int */ -int tsdbWriteBlockImpl(STsdb *pRepo, STable *pTable, SDFile *pDFile, SDFile *pDFileAggr, SDataCols *pDataCols, - SBlock *pBlock, bool isLast, bool isSuper, void **ppBuf, void **ppCBuf, void **ppExBuf) { +static int tsdbWriteBlockImpl(STsdb *pRepo, STable *pTable, SDFile *pDFile, SDFile *pDFileAggr, SDataCols *pDataCols, + SBlock *pBlock, bool isLast, bool isSuper, void **ppBuf, void **ppCBuf, void **ppExBuf) { STsdbCfg *pCfg = REPO_CFG(pRepo); SBlockData *pBlockData = NULL; SAggrBlkData *pAggrBlkData = NULL; From de32cab384d8eec3131e0230bfdbf3e618ed32e2 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 08:27:55 +0000 Subject: [PATCH 10/24] more progress --- source/dnode/vnode/CMakeLists.txt | 1 + source/dnode/vnode/src/inc/tsdb.h | 14 +++ source/dnode/vnode/src/tsdb/tsdbCommit2.c | 104 ++++++++++++++++++++ source/dnode/vnode/src/tsdb/tsdbMemTable2.c | 24 ++--- 4 files changed, 129 insertions(+), 14 deletions(-) create mode 100644 source/dnode/vnode/src/tsdb/tsdbCommit2.c diff --git a/source/dnode/vnode/CMakeLists.txt b/source/dnode/vnode/CMakeLists.txt index f3aefdba02..05b4a270d6 100644 --- a/source/dnode/vnode/CMakeLists.txt +++ b/source/dnode/vnode/CMakeLists.txt @@ -36,6 +36,7 @@ target_sources( # tsdb "src/tsdb/tsdbCommit.c" + "src/tsdb/tsdbCommit2.c" "src/tsdb/tsdbFile.c" "src/tsdb/tsdbFS.c" "src/tsdb/tsdbOpen.c" diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index ab23b7506e..c9b8a26735 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -46,6 +46,10 @@ void tsdbMemTableDestroy2(SMemTable *pMemTable); int32_t tsdbInsertTableData2(STsdb *pTsdb, int64_t version, SVSubmitBlk *pSubmitBlk); int32_t tsdbDeleteTableData2(STsdb *pTsdb, int64_t version, tb_uid_t suid, tb_uid_t uid, TSKEY sKey, TSKEY eKey); +// tsdbCommit2.c ============================================================================================== +int32_t tsdbBegin2(STsdb *pTsdb); +int32_t tsdbCommit2(STsdb *pTsdb); + // tsdbMemTable ================ typedef struct STsdbRow STsdbRow; typedef struct STbData STbData; @@ -877,6 +881,16 @@ struct SDelOp { SDelOp *pNext; }; +struct SMemTable { + STsdb *pTsdb; + int32_t nRef; + TSDBKEY minKey; + TSDBKEY maxKey; + int64_t nRows; + SArray *aSkmInfo; + SArray *aMemData; +}; + static FORCE_INLINE int tsdbKeyCmprFn(const void *p1, const void *p2) { TSDBKEY *pKey1 = (TSDBKEY *)p1; TSDBKEY *pKey2 = (TSDBKEY *)p2; diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit2.c b/source/dnode/vnode/src/tsdb/tsdbCommit2.c new file mode 100644 index 0000000000..3e87e4be43 --- /dev/null +++ b/source/dnode/vnode/src/tsdb/tsdbCommit2.c @@ -0,0 +1,104 @@ +/* + * 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 . + */ + +#include "tsdb.h" + +typedef struct { + SMemTable *pMemTable; + SArray *aBlkIdx; +} SCommitH; + +static int32_t tsdbStartCommit(SCommitH *pCHandle, STsdb *pTsdb); +static int32_t tsdbEndCommit(SCommitH *pCHandle); +static int32_t tsdbCommitToFile(SCommitH *pCHandle, int32_t fid); + +int32_t tsdbBegin2(STsdb *pTsdb) { + int32_t code = 0; + + ASSERT(pTsdb->mem == NULL); + code = tsdbMemTableCreate2(pTsdb, (SMemTable **)&pTsdb->mem); + if (code) { + tsdbError("vgId:%d failed to begin TSDB since %s", TD_VID(pTsdb->pVnode), tstrerror(code)); + goto _exit; + } + +_exit: + return code; +} + +int32_t tsdbCommit2(STsdb *pTsdb) { + int32_t code = 0; + SCommitH ch = {0}; + + // start to commit + code = tsdbStartCommit(&ch, pTsdb); + if (code) { + goto _exit; + } + + // commit + int32_t sfid; // todo + int32_t efid; // todo + for (int32_t fid = sfid; fid <= efid; fid++) { + code = tsdbCommitToFile(&ch, fid); + if (code) { + goto _err; + } + } + + // end commit + code = tsdbEndCommit(&ch); + if (code) { + goto _exit; + } + +_exit: + return code; + +_err: + // TODO: rollback + return code; +} + +static int32_t tsdbStartCommit(SCommitH *pCHandle, STsdb *pTsdb) { + int32_t code = 0; + + ASSERT(pTsdb->imem == NULL && pTsdb->mem); + pTsdb->imem = pTsdb->mem; + pTsdb->mem = NULL; + + return code; +} + +static int32_t tsdbEndCommit(SCommitH *pCHandle) { + int32_t code = 0; + // TODO + return code; +} + +static int32_t tsdbCommitToFile(SCommitH *pCHandle, int32_t fid) { + int32_t code = 0; + TSKEY fidSKey; + TSKEY fidEKey; + + // check if there are data in the time range + for (int32_t iMemData = 0; iMemData < taosArrayGetSize(pCHandle->pMemTable->aMemData); iMemData++) { + /* code */ + } + + // has data, do commit to file + + return code; +} \ No newline at end of file diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c index b6d0747fe3..23f779ec88 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c @@ -43,14 +43,10 @@ struct SMemData { SMemSkipList sl; }; -struct SMemTable { - STsdb *pTsdb; - int32_t nRef; - TSDBKEY minKey; - TSDBKEY maxKey; - int64_t nRows; - SArray *pArray; // SArray -}; +typedef struct { + tb_uid_t uid; + STSchema *pTSchema; +} SSkmInfo; #define SL_MAX_LEVEL 5 @@ -85,8 +81,8 @@ int32_t tsdbMemTableCreate2(STsdb *pTsdb, SMemTable **ppMemTable) { pMemTable->minKey = (TSDBKEY){.version = INT64_MAX, .ts = TSKEY_MAX}; pMemTable->maxKey = (TSDBKEY){.version = -1, .ts = TSKEY_MIN}; pMemTable->nRows = 0; - pMemTable->pArray = taosArrayInit(512, sizeof(SMemData *)); - if (pMemTable->pArray == NULL) { + pMemTable->aMemData = taosArrayInit(512, sizeof(SMemData *)); + if (pMemTable->aMemData == NULL) { taosMemoryFree(pMemTable); code = TSDB_CODE_OUT_OF_MEMORY; goto _err; @@ -101,7 +97,7 @@ _err: } void tsdbMemTableDestroy2(SMemTable *pMemTable) { - taosArrayDestroyEx(pMemTable->pArray, NULL /*TODO*/); + taosArrayDestroyEx(pMemTable->aMemData, NULL /*TODO*/); taosMemoryFree(pMemTable); } @@ -196,9 +192,9 @@ static int32_t tsdbGetOrCreateMemData(SMemTable *pMemTable, tb_uid_t suid, tb_ui int8_t maxLevel = pMemTable->pTsdb->pVnode->config.tsdbCfg.slLevel; // get - idx = taosArraySearchIdx(pMemTable->pArray, &pMemDataT, memDataPCmprFn, TD_GE); + idx = taosArraySearchIdx(pMemTable->aMemData, &pMemDataT, memDataPCmprFn, TD_GE); if (idx >= 0) { - pMemData = (SMemData *)taosArrayGet(pMemTable->pArray, idx); + pMemData = (SMemData *)taosArrayGet(pMemTable->aMemData, idx); if (memDataPCmprFn(&pMemDataT, &pMemData) == 0) goto _exit; } @@ -230,7 +226,7 @@ static int32_t tsdbGetOrCreateMemData(SMemTable *pMemTable, tb_uid_t suid, tb_ui } if (idx < 0) idx = 0; - if (taosArrayInsert(pMemTable->pArray, idx, &pMemData) == NULL) { + if (taosArrayInsert(pMemTable->aMemData, idx, &pMemData) == NULL) { code = TSDB_CODE_OUT_OF_MEMORY; goto _err; } From 0bc5300d03a569480ff92fd271f4b4f31ec5f1ef Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 09:31:35 +0000 Subject: [PATCH 11/24] more progress --- source/dnode/vnode/src/inc/tsdb.h | 16 +++++++- source/dnode/vnode/src/tsdb/tsdbCommit2.c | 43 +++++++++++++++++---- source/dnode/vnode/src/tsdb/tsdbMemTable2.c | 1 - 3 files changed, 51 insertions(+), 9 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index c9b8a26735..3a2eec30ba 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -39,13 +39,21 @@ typedef struct SDelOp SDelOp; static int tsdbKeyCmprFn(const void *p1, const void *p2); // tsdbMemTable2.c ============================================================================================== -typedef struct SMemTable SMemTable; +typedef struct SMemTable SMemTable; +typedef struct SMemData SMemData; +typedef struct SMemDataIter SMemDataIter; int32_t tsdbMemTableCreate2(STsdb *pTsdb, SMemTable **ppMemTable); void tsdbMemTableDestroy2(SMemTable *pMemTable); int32_t tsdbInsertTableData2(STsdb *pTsdb, int64_t version, SVSubmitBlk *pSubmitBlk); int32_t tsdbDeleteTableData2(STsdb *pTsdb, int64_t version, tb_uid_t suid, tb_uid_t uid, TSKEY sKey, TSKEY eKey); +/* SMemDataIter */ +void tsdbMemDataIterOpen(SMemDataIter *pIter, TSDBKEY *pKey, int8_t backward); +void tsdbMemDataIterClose(SMemDataIter *pIter); +void tsdbMemDataIterNext(SMemDataIter *pIter); +void tsdbMemDataIterGet(SMemDataIter *pIter, TSDBROW **ppRow); + // tsdbCommit2.c ============================================================================================== int32_t tsdbBegin2(STsdb *pTsdb); int32_t tsdbCommit2(STsdb *pTsdb); @@ -254,6 +262,7 @@ typedef struct { uint32_t offset; uint32_t hasLast : 2; uint32_t numOfBlocks : 30; + uint64_t suid; uint64_t uid; TSKEY maxKey; } SBlockIdx; @@ -910,6 +919,11 @@ static FORCE_INLINE int tsdbKeyCmprFn(const void *p1, const void *p2) { return 0; } +struct SMemDataIter { + SMemData *pMemData; + TSDBROW row; +}; + #endif #ifdef __cplusplus diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit2.c b/source/dnode/vnode/src/tsdb/tsdbCommit2.c index 3e87e4be43..6bb53a1263 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit2.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit2.c @@ -78,7 +78,7 @@ static int32_t tsdbStartCommit(SCommitH *pCHandle, STsdb *pTsdb) { ASSERT(pTsdb->imem == NULL && pTsdb->mem); pTsdb->imem = pTsdb->mem; pTsdb->mem = NULL; - + // TODO return code; } @@ -89,16 +89,45 @@ static int32_t tsdbEndCommit(SCommitH *pCHandle) { } static int32_t tsdbCommitToFile(SCommitH *pCHandle, int32_t fid) { - int32_t code = 0; - TSKEY fidSKey; - TSKEY fidEKey; + int32_t code = 0; + SMemDataIter iter = {0}; + TSDBROW *pRow = NULL; + int8_t hasData = 0; + TSKEY fidSKey; + TSKEY fidEKey; + int32_t iMemData = 0; + int32_t nMemData = taosArrayGetSize(pCHandle->pMemTable->aMemData); + int32_t iBlockIdx = 0; + int32_t nBlockIdx; // check if there are data in the time range - for (int32_t iMemData = 0; iMemData < taosArrayGetSize(pCHandle->pMemTable->aMemData); iMemData++) { - /* code */ + for (; iMemData < nMemData; iMemData++) { + SMemData *pMemData = (SMemData *)taosArrayGetP(pCHandle->pMemTable->aMemData, iMemData); + tsdbMemDataIterOpen(&iter, &(TSDBKEY){.ts = fidSKey, .version = 0}, 0); + tsdbMemDataIterGet(&iter, &pRow); + + if (pRow->tsRow.ts >= fidSKey && pRow->tsRow.ts <= fidEKey) { + hasData = 1; + break; + } } - // has data, do commit to file + if (!hasData) return code; + + // loop to commit each table data + nBlockIdx = 0; + for (;;) { + if (iBlockIdx >= nBlockIdx && iMemData >= nMemData) break; + + SMemData *pMemData = NULL; + SBlockIdx *pBlockIdx = NULL; + if (iMemData < nMemData) { + pMemData = (SMemData *)taosArrayGetP(pCHandle->pMemTable->aMemData, iBlockIdx); + } + if (iBlockIdx < nBlockIdx) { + // pBlockIdx + } + } return code; } \ No newline at end of file diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c index 23f779ec88..1c28b086b3 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c @@ -15,7 +15,6 @@ #include "tsdb.h" -typedef struct SMemData SMemData; typedef struct SMemSkipList SMemSkipList; typedef struct SMemSkipListNode SMemSkipListNode; From b1a781cc09a6eac763c9689249c129fa60d13d4e Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 12:42:25 +0000 Subject: [PATCH 12/24] more progress --- include/common/tdataformat.h | 7 +++ source/dnode/vnode/src/inc/tsdb.h | 26 ++++++++++ source/dnode/vnode/src/tsdb/tsdbCommit2.c | 55 +++++++++++++++++++++ source/dnode/vnode/src/tsdb/tsdbMemTable2.c | 22 --------- 4 files changed, 88 insertions(+), 22 deletions(-) diff --git a/include/common/tdataformat.h b/include/common/tdataformat.h index 6a73d3d678..2e69640a06 100644 --- a/include/common/tdataformat.h +++ b/include/common/tdataformat.h @@ -34,6 +34,7 @@ typedef struct SValue SValue; typedef struct SColVal SColVal; typedef struct STSRow2 STSRow2; typedef struct STSRowBuilder STSRowBuilder; +typedef struct SColData SColData; typedef struct STagVal STagVal; typedef struct STag STag; @@ -169,6 +170,12 @@ struct STag { }; #pragma pack(pop) +struct SColData { + int16_t cid; + uint32_t nData; + uint8_t *pData; +}; + #if 1 //================================================================================================================================================ // Imported since 3.0 and use bitmap to demonstrate None/Null/Norm, while use Null/Norm below 3.0 without of bitmap. #define TD_SUPPORT_BITMAP diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index 3a2eec30ba..cb12550d1f 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -34,6 +34,7 @@ extern "C" { typedef struct TSDBROW TSDBROW; typedef struct TSDBKEY TSDBKEY; +typedef struct TABLEID TABLEID; typedef struct SDelOp SDelOp; static int tsdbKeyCmprFn(const void *p1, const void *p2); @@ -883,6 +884,11 @@ struct TSDBKEY { TSKEY ts; }; +struct TABLEID { + tb_uid_t suid; + tb_uid_t uid; +}; + struct SDelOp { int64_t version; TSKEY sKey; // included @@ -919,6 +925,26 @@ static FORCE_INLINE int tsdbKeyCmprFn(const void *p1, const void *p2) { return 0; } +typedef struct SMemSkipListNode SMemSkipListNode; +typedef struct SMemSkipList { + uint32_t seed; + int32_t size; + int8_t maxLevel; + int8_t level; + SMemSkipListNode *pHead; + SMemSkipListNode *pTail; +} SMemSkipList; + +struct SMemData { + tb_uid_t suid; + tb_uid_t uid; + TSDBKEY minKey; + TSDBKEY maxKey; + SDelOp *delOpHead; + SDelOp *delOpTail; + SMemSkipList sl; +}; + struct SMemDataIter { SMemData *pMemData; TSDBROW row; diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit2.c b/source/dnode/vnode/src/tsdb/tsdbCommit2.c index 6bb53a1263..5741da0e80 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit2.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit2.c @@ -88,6 +88,30 @@ static int32_t tsdbEndCommit(SCommitH *pCHandle) { return code; } +static int32_t tsdbCommitTableData(SCommitH *pCHandle, SMemData *pMemData, SBlockIdx *pBlockIdx) { + int32_t code = 0; + // TODO + return code; +} + +static int32_t tsdbTableIdCmprFn(const void *p1, const void *p2) { + TABLEID *pId1 = (TABLEID *)p1; + TABLEID *pId2 = (TABLEID *)p2; + + if (pId1->suid < pId2->suid) { + return -1; + } else if (pId1->suid > pId2->suid) { + return 1; + } + + if (pId1->uid < pId2->uid) { + return -1; + } else if (pId1->uid > pId2->uid) { + return 1; + } + + return 0; +} static int32_t tsdbCommitToFile(SCommitH *pCHandle, int32_t fid) { int32_t code = 0; SMemDataIter iter = {0}; @@ -114,6 +138,8 @@ static int32_t tsdbCommitToFile(SCommitH *pCHandle, int32_t fid) { if (!hasData) return code; + // create or open the file to commit(todo) + // loop to commit each table data nBlockIdx = 0; for (;;) { @@ -127,7 +153,36 @@ static int32_t tsdbCommitToFile(SCommitH *pCHandle, int32_t fid) { if (iBlockIdx < nBlockIdx) { // pBlockIdx } + + if (pMemData && pBlockIdx) { + int32_t c = tsdbTableIdCmprFn(&(TABLEID){.suid = pMemData->suid, .uid = pMemData->uid}, + &(TABLEID){.suid = pBlockIdx->suid, .uid = pBlockIdx->uid}); + if (c == 0) { + iMemData++; + iBlockIdx++; + } else if (c < 0) { + pBlockIdx = NULL; + iMemData++; + } else { + pMemData = NULL; + iBlockIdx++; + } + } else { + if (pMemData) { + iMemData++; + } else { + iBlockIdx++; + } + } + + code = tsdbCommitTableData(pCHandle, pMemData, pBlockIdx); + if (code) { + goto _err; + } } return code; + +_err: + return code; } \ No newline at end of file diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c index 1c28b086b3..dee025cae6 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c @@ -15,33 +15,11 @@ #include "tsdb.h" -typedef struct SMemSkipList SMemSkipList; -typedef struct SMemSkipListNode SMemSkipListNode; - struct SMemSkipListNode { int8_t level; SMemSkipListNode *forwards[0]; }; -struct SMemSkipList { - uint32_t seed; - int32_t size; - int8_t maxLevel; - int8_t level; - SMemSkipListNode *pHead; - SMemSkipListNode *pTail; -}; - -struct SMemData { - tb_uid_t suid; - tb_uid_t uid; - TSDBKEY minKey; - TSDBKEY maxKey; - SDelOp *delOpHead; - SDelOp *delOpTail; - SMemSkipList sl; -}; - typedef struct { tb_uid_t uid; STSchema *pTSchema; From af4aaed01f9ba67d6fbbd86aaae547bb5e3733c7 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 14:03:05 +0000 Subject: [PATCH 13/24] more --- source/dnode/vnode/src/tsdb/tsdbCommit2.c | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit2.c b/source/dnode/vnode/src/tsdb/tsdbCommit2.c index 5741da0e80..abeca274d4 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit2.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit2.c @@ -89,8 +89,17 @@ static int32_t tsdbEndCommit(SCommitH *pCHandle) { } static int32_t tsdbCommitTableData(SCommitH *pCHandle, SMemData *pMemData, SBlockIdx *pBlockIdx) { - int32_t code = 0; - // TODO + int32_t code = 0; + SMemDataIter iter = {0}; + + if (pMemData && pBlockIdx) { + // merge + } else if (pMemData) { + // new one + } else { + // save old ones + } + return code; } From 32ee7629247f5d2b28ffe1c642971674ebb59b42 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 14:27:34 +0000 Subject: [PATCH 14/24] add version on some structures --- source/dnode/vnode/src/inc/tsdb.h | 40 +++++------------- source/dnode/vnode/src/tsdb/tsdbCommit.c | 20 ++++----- source/dnode/vnode/src/tsdb/tsdbCommit2.c | 1 + source/dnode/vnode/src/tsdb/tsdbRead.c | 47 +++++++++++----------- source/dnode/vnode/src/tsdb/tsdbReadImpl.c | 12 +++--- 5 files changed, 52 insertions(+), 68 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index cb12550d1f..acf83e4a9c 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -258,6 +258,11 @@ static FORCE_INLINE TSKEY tsdbNextIterKey(SSkipListIterator *pIter) { // tsdbReadImpl typedef struct SReadH SReadH; +struct TSDBKEY { + int64_t version; + TSKEY ts; +}; + typedef struct { uint32_t len; uint32_t offset; @@ -265,25 +270,9 @@ typedef struct { uint32_t numOfBlocks : 30; uint64_t suid; uint64_t uid; - TSKEY maxKey; + TSDBKEY maxKey; } SBlockIdx; -#ifdef TD_REFACTOR_3 -typedef struct { - int64_t last : 1; - int64_t offset : 63; - int32_t algorithm : 8; - int32_t numOfRows : 24; - int32_t len; - int32_t keyLen; // key column length, keyOffset = offset+sizeof(SBlockData)+sizeof(SBlockCol)*numOfCols - int16_t numOfSubBlocks; - int16_t numOfCols; // not including timestamp column - TSKEY keyFirst; - TSKEY keyLast; -} SBlock; - -#else - typedef enum { TSDB_SBLK_VER_0 = 0, TSDB_SBLK_VER_MAX, @@ -306,17 +295,15 @@ typedef struct { int64_t offset; uint64_t aggrStat : 1; uint64_t aggrOffset : 63; - TSKEY keyFirst; - TSKEY keyLast; -} SBlockV0; - -#define SBlock SBlockV0 // latest SBlock definition + TSDBKEY minKey; + TSDBKEY maxKey; + // TSKEY keyFirst; + // TSKEY keyLast; +} SBlock; static FORCE_INLINE bool tsdbIsSupBlock(SBlock *pBlock) { return pBlock->numOfSubBlocks == 1; } static FORCE_INLINE bool tsdbIsSubBlock(SBlock *pBlock) { return pBlock->numOfSubBlocks == 0; } -#endif - typedef struct { int32_t delimiter; // For recovery usage int32_t tid; @@ -879,11 +866,6 @@ struct TSDBROW { STSRow2 tsRow; }; -struct TSDBKEY { - int64_t version; - TSKEY ts; -}; - struct TABLEID { tb_uid_t suid; tb_uid_t uid; diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit.c b/source/dnode/vnode/src/tsdb/tsdbCommit.c index 928915a36d..3013f6deef 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit.c @@ -774,7 +774,7 @@ static int tsdbWriteBlockInfoImpl(SDFile *pHeadf, STable *pTable, SArray *pSupA, pIdx->uid = TABLE_UID(pTable); pIdx->hasLast = pBlock->last ? 1 : 0; - pIdx->maxKey = pBlock->keyLast; + pIdx->maxKey = pBlock->maxKey; pIdx->numOfBlocks = (uint32_t)nSupBlocks; pIdx->len = tlen; pIdx->offset = (uint32_t)offset; @@ -895,7 +895,7 @@ static int tsdbCommitToTable(SCommitH *pCommith, int tid) { return -1; } } else { - if (tsdbCommitMemData(pCommith, pIter, pBlock->keyFirst - 1, true) < 0) { + if (tsdbCommitMemData(pCommith, pIter, pBlock->minKey.ts - 1, true) < 0) { return -1; } } @@ -990,9 +990,9 @@ static int tsdbComparKeyBlock(const void *arg1, const void *arg2) { TSKEY key = *(TSKEY *)arg1; SBlock *pBlock = (SBlock *)arg2; - if (key < pBlock->keyFirst) { + if (key < pBlock->minKey.ts) { return -1; - } else if (key > pBlock->keyLast) { + } else if (key > pBlock->maxKey.ts) { return 1; } else { return 0; @@ -1220,8 +1220,8 @@ static int tsdbWriteBlockImpl(STsdb *pRepo, STable *pTable, SDFile *pDFile, SDFi pBlock->numOfSubBlocks = isSuper ? 1 : 0; pBlock->numOfCols = nColsNotAllNull; pBlock->numOfBSma = nColsOfBlockSma; - pBlock->keyFirst = dataColsKeyFirst(pDataCols); - pBlock->keyLast = dataColsKeyLast(pDataCols); + pBlock->minKey.ts = dataColsKeyFirst(pDataCols); + pBlock->maxKey.ts = dataColsKeyLast(pDataCols); pBlock->aggrStat = aggrStatus; pBlock->blkVer = SBlockVerLatest; pBlock->aggrOffset = (uint64_t)offsetAggr; @@ -1229,7 +1229,7 @@ static int tsdbWriteBlockImpl(STsdb *pRepo, STable *pTable, SDFile *pDFile, SDFi tsdbDebug("vgId:%d, uid:%" PRId64 " a block of data is written to file %s, offset %" PRId64 " numOfRows %d len %d numOfCols %" PRId16 " keyFirst %" PRId64 " keyLast %" PRId64, REPO_ID(pRepo), TABLE_UID(pTable), TSDB_FILE_FULL_NAME(pDFile), offset, rowsToWrite, pBlock->len, - pBlock->numOfCols, pBlock->keyFirst, pBlock->keyLast); + pBlock->numOfCols, pBlock->minKey.ts, pBlock->maxKey.ts); return 0; } @@ -1312,7 +1312,7 @@ static int tsdbMergeMemData(SCommitH *pCommith, SCommitIter *pIter, int bidx) { if (bidx == nBlocks - 1) { keyLimit = pCommith->maxKey; } else { - keyLimit = pBlock[1].keyFirst - 1; + keyLimit = pBlock[1].minKey.ts - 1; } SSkipListIterator titer = *(pIter->pIter); @@ -1354,8 +1354,8 @@ static int tsdbMergeMemData(SCommitH *pCommith, SCommitIter *pIter, int bidx) { } subBlocks[pBlock->numOfSubBlocks] = block; supBlock = *pBlock; - supBlock.keyFirst = mInfo.keyFirst; - supBlock.keyLast = mInfo.keyLast; + supBlock.minKey.ts = mInfo.keyFirst; + supBlock.maxKey.ts = mInfo.keyLast; supBlock.numOfSubBlocks++; supBlock.numOfRows = pBlock->numOfRows + mInfo.rowsInserted - mInfo.rowsDeleteSucceed; supBlock.offset = taosArrayGetSize(pCommith->aSubBlk) * sizeof(SBlock); diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit2.c b/source/dnode/vnode/src/tsdb/tsdbCommit2.c index abeca274d4..38e4795a83 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit2.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit2.c @@ -17,6 +17,7 @@ typedef struct { SMemTable *pMemTable; + SReadH readh; SArray *aBlkIdx; } SCommitH; diff --git a/source/dnode/vnode/src/tsdb/tsdbRead.c b/source/dnode/vnode/src/tsdb/tsdbRead.c index 5f2ea80078..95322f5eb3 100644 --- a/source/dnode/vnode/src/tsdb/tsdbRead.c +++ b/source/dnode/vnode/src/tsdb/tsdbRead.c @@ -20,10 +20,10 @@ #define ASCENDING_TRAVERSE(o) (o == TSDB_ORDER_ASC) #define QH_GET_NUM_OF_COLS(handle) ((size_t)(taosArrayGetSize((handle)->pColumns))) -#define GET_FILE_DATA_BLOCK_INFO(_checkInfo, _block) \ - ((SDataBlockInfo){.window = {.skey = (_block)->keyFirst, .ekey = (_block)->keyLast}, \ - .numOfCols = (_block)->numOfCols, \ - .rows = (_block)->numOfRows, \ +#define GET_FILE_DATA_BLOCK_INFO(_checkInfo, _block) \ + ((SDataBlockInfo){.window = {.skey = (_block)->minKey.ts, .ekey = (_block)->maxKey.ts}, \ + .numOfCols = (_block)->numOfCols, \ + .rows = (_block)->numOfRows, \ .uid = (_checkInfo)->tableId}) enum { @@ -1105,12 +1105,12 @@ static int32_t binarySearchForBlock(SBlock* pBlock, int32_t numOfBlocks, TSKEY s if (numOfBlocks == 1) break; - if (skey > pBlock[midSlot].keyLast) { + if (skey > pBlock[midSlot].maxKey.ts) { if (numOfBlocks == 2) break; - if ((order == TSDB_ORDER_DESC) && (skey < pBlock[midSlot + 1].keyFirst)) break; + if ((order == TSDB_ORDER_DESC) && (skey < pBlock[midSlot + 1].minKey.ts)) break; firstSlot = midSlot + 1; - } else if (skey < pBlock[midSlot].keyFirst) { - if ((order == TSDB_ORDER_ASC) && (skey > pBlock[midSlot - 1].keyLast)) break; + } else if (skey < pBlock[midSlot].minKey.ts) { + if ((order == TSDB_ORDER_ASC) && (skey > pBlock[midSlot - 1].maxKey.ts)) break; lastSlot = midSlot - 1; } else { break; // got the slot @@ -1177,12 +1177,12 @@ static int32_t loadBlockInfo(STsdbReadHandle* pTsdbReadHandle, int32_t index, in int32_t start = binarySearchForBlock(pCompInfo->blocks, compIndex->numOfBlocks, s, TSDB_ORDER_ASC); int32_t end = start; - if (s > pCompInfo->blocks[start].keyLast) { + if (s > pCompInfo->blocks[start].maxKey.ts) { return 0; } // todo speedup the procedure of located end block - while (end < (int32_t)compIndex->numOfBlocks && (pCompInfo->blocks[end].keyFirst <= e)) { + while (end < (int32_t)compIndex->numOfBlocks && (pCompInfo->blocks[end].minKey.ts <= e)) { end += 1; } @@ -1275,7 +1275,7 @@ static int32_t doLoadFileDataBlock(STsdbReadHandle* pTsdbReadHandle, SBlock* pBl pBlock->numOfRows = pCols->numOfRows; // Convert from TKEY to TSKEY for primary timestamp column if current block has timestamp before 1970-01-01T00:00:00Z - if (pBlock->keyFirst < 0 && colIds[0] == PRIMARYKEY_TIMESTAMP_COL_ID) { + if (pBlock->minKey.ts < 0 && colIds[0] == PRIMARYKEY_TIMESTAMP_COL_ID) { int64_t* src = pCols->cols[0].pData; for (int32_t i = 0; i < pBlock->numOfRows; ++i) { src[i] = tdGetKey(src[i]); @@ -1287,7 +1287,7 @@ static int32_t doLoadFileDataBlock(STsdbReadHandle* pTsdbReadHandle, SBlock* pBl tsdbDebug("%p load file block into buffer, index:%d, brange:%" PRId64 "-%" PRId64 ", rows:%d, elapsed time:%" PRId64 " us, %s", - pTsdbReadHandle, slotIndex, pBlock->keyFirst, pBlock->keyLast, pBlock->numOfRows, elapsedTime, + pTsdbReadHandle, slotIndex, pBlock->minKey.ts, pBlock->maxKey.ts, pBlock->numOfRows, elapsedTime, pTsdbReadHandle->idStr); return TSDB_CODE_SUCCESS; @@ -1295,7 +1295,8 @@ _error: pBlock->numOfRows = 0; tsdbError("%p error occurs in loading file block, index:%d, brange:%" PRId64 "-%" PRId64 ", rows:%d, %s", - pTsdbReadHandle, slotIndex, pBlock->keyFirst, pBlock->keyLast, pBlock->numOfRows, pTsdbReadHandle->idStr); + pTsdbReadHandle, slotIndex, pBlock->minKey.ts, pBlock->maxKey.ts, pBlock->numOfRows, + pTsdbReadHandle->idStr); return terrno; } @@ -1423,7 +1424,7 @@ static int32_t loadFileDataBlock(STsdbReadHandle* pTsdbReadHandle, SBlock* pBloc if (asc) { // query ended in/started from current block - if (pTsdbReadHandle->window.ekey < pBlock->keyLast || pCheckInfo->lastKey > pBlock->keyFirst) { + if (pTsdbReadHandle->window.ekey < pBlock->maxKey.ts || pCheckInfo->lastKey > pBlock->minKey.ts) { if ((code = doLoadFileDataBlock(pTsdbReadHandle, pBlock, pCheckInfo, cur->slot)) != TSDB_CODE_SUCCESS) { *exists = false; return code; @@ -1432,35 +1433,35 @@ static int32_t loadFileDataBlock(STsdbReadHandle* pTsdbReadHandle, SBlock* pBloc SDataCols* pTSCol = pTsdbReadHandle->rhelper.pDCols[0]; assert(pTSCol->cols->type == TSDB_DATA_TYPE_TIMESTAMP && pTSCol->numOfRows == pBlock->numOfRows); - if (pCheckInfo->lastKey > pBlock->keyFirst) { + if (pCheckInfo->lastKey > pBlock->minKey.ts) { cur->pos = binarySearchForKey(pTSCol->cols[0].pData, pBlock->numOfRows, pCheckInfo->lastKey, pTsdbReadHandle->order); } else { cur->pos = 0; } - assert(pCheckInfo->lastKey <= pBlock->keyLast); + assert(pCheckInfo->lastKey <= pBlock->maxKey.ts); doMergeTwoLevelData(pTsdbReadHandle, pCheckInfo, pBlock); } else { // the whole block is loaded in to buffer cur->pos = asc ? 0 : (pBlock->numOfRows - 1); code = handleDataMergeIfNeeded(pTsdbReadHandle, pBlock, pCheckInfo); } } else { // desc order, query ended in current block - if (pTsdbReadHandle->window.ekey > pBlock->keyFirst || pCheckInfo->lastKey < pBlock->keyLast) { + if (pTsdbReadHandle->window.ekey > pBlock->minKey.ts || pCheckInfo->lastKey < pBlock->maxKey.ts) { if ((code = doLoadFileDataBlock(pTsdbReadHandle, pBlock, pCheckInfo, cur->slot)) != TSDB_CODE_SUCCESS) { *exists = false; return code; } SDataCols* pTsCol = pTsdbReadHandle->rhelper.pDCols[0]; - if (pCheckInfo->lastKey < pBlock->keyLast) { + if (pCheckInfo->lastKey < pBlock->maxKey.ts) { cur->pos = binarySearchForKey(pTsCol->cols[0].pData, pBlock->numOfRows, pCheckInfo->lastKey, pTsdbReadHandle->order); } else { cur->pos = pBlock->numOfRows - 1; } - assert(pCheckInfo->lastKey >= pBlock->keyFirst); + assert(pCheckInfo->lastKey >= pBlock->minKey.ts); doMergeTwoLevelData(pTsdbReadHandle, pCheckInfo, pBlock); } else { cur->pos = asc ? 0 : (pBlock->numOfRows - 1); @@ -1981,8 +1982,8 @@ static void doMergeTwoLevelData(STsdbReadHandle* pTsdbReadHandle, STableCheckInf cur->pos >= 0 && cur->pos < pBlock->numOfRows); // Even Multi-Version supported, the records with duplicated TSKEY would be merged inside of tsdbLoadData interface. TSKEY* tsArray = pCols->cols[0].pData; - assert(pCols->numOfRows == pBlock->numOfRows && tsArray[0] == pBlock->keyFirst && - tsArray[pBlock->numOfRows - 1] == pBlock->keyLast); + assert(pCols->numOfRows == pBlock->numOfRows && tsArray[0] == pBlock->minKey.ts && + tsArray[pBlock->numOfRows - 1] == pBlock->maxKey.ts); bool ascScan = ASCENDING_TRAVERSE(pTsdbReadHandle->order); int32_t step = ascScan ? 1 : -1; @@ -3576,8 +3577,8 @@ int32_t tsdbRetrieveDataBlockStatisInfo(tsdbReaderT* pTsdbReadHandle, SColumnDat assert(pPrimaryColStatis->colId == PRIMARYKEY_TIMESTAMP_COL_ID); pPrimaryColStatis->numOfNull = 0; - pPrimaryColStatis->min = pBlockInfo->compBlock->keyFirst; - pPrimaryColStatis->max = pBlockInfo->compBlock->keyLast; + pPrimaryColStatis->min = pBlockInfo->compBlock->minKey.ts; + pPrimaryColStatis->max = pBlockInfo->compBlock->maxKey.ts; pHandle->suppInfo.plist[0] = &pHandle->suppInfo.pstatis[0]; // update the number of NULL data rows diff --git a/source/dnode/vnode/src/tsdb/tsdbReadImpl.c b/source/dnode/vnode/src/tsdb/tsdbReadImpl.c index a6f2ff1394..e333f0566b 100644 --- a/source/dnode/vnode/src/tsdb/tsdbReadImpl.c +++ b/source/dnode/vnode/src/tsdb/tsdbReadImpl.c @@ -339,8 +339,8 @@ int tsdbLoadBlockData(SReadH *pReadh, SBlock *pBlock, SBlockInfo *pBlkInfo) { } ASSERT(pReadh->pDCols[0]->numOfRows <= pBlock->numOfRows); - ASSERT(dataColsKeyFirst(pReadh->pDCols[0]) == pBlock->keyFirst); - ASSERT(dataColsKeyLast(pReadh->pDCols[0]) == pBlock->keyLast); + ASSERT(dataColsKeyFirst(pReadh->pDCols[0]) == pBlock->minKey.ts); + ASSERT(dataColsKeyLast(pReadh->pDCols[0]) == pBlock->maxKey.ts); return 0; } @@ -457,8 +457,8 @@ int tsdbLoadBlockDataCols(SReadH *pReadh, SBlock *pBlock, SBlockInfo *pBlkInfo, } ASSERT(pReadh->pDCols[0]->numOfRows <= pBlock->numOfRows); - ASSERT(dataColsKeyFirst(pReadh->pDCols[0]) == pBlock->keyFirst); - ASSERT(dataColsKeyLast(pReadh->pDCols[0]) == pBlock->keyLast); + ASSERT(dataColsKeyFirst(pReadh->pDCols[0]) == pBlock->minKey.ts); + ASSERT(dataColsKeyLast(pReadh->pDCols[0]) == pBlock->maxKey.ts); return 0; } @@ -559,7 +559,7 @@ int tsdbEncodeSBlockIdx(void **buf, SBlockIdx *pIdx) { tlen += taosEncodeFixedU8(buf, pIdx->hasLast); tlen += taosEncodeVariantU32(buf, pIdx->numOfBlocks); tlen += taosEncodeFixedU64(buf, pIdx->uid); - tlen += taosEncodeFixedU64(buf, pIdx->maxKey); + tlen += taosEncodeFixedU64(buf, pIdx->maxKey.ts); return tlen; } @@ -579,7 +579,7 @@ void *tsdbDecodeSBlockIdx(void *buf, SBlockIdx *pIdx) { if ((buf = taosDecodeFixedU64(buf, &value)) == NULL) return NULL; pIdx->uid = (int64_t)value; if ((buf = taosDecodeFixedU64(buf, &value)) == NULL) return NULL; - pIdx->maxKey = (TSKEY)value; + pIdx->maxKey.ts = (TSKEY)value; return buf; } From b997509a759b1c5ebe60312ab6256179ae5145be Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sat, 4 Jun 2022 15:22:20 +0000 Subject: [PATCH 15/24] more work --- source/dnode/vnode/src/inc/tsdb.h | 12 +-- source/dnode/vnode/src/tsdb/tsdbCommit2.c | 2 +- source/dnode/vnode/src/tsdb/tsdbMemTable2.c | 87 +++++++++++++++++++++ 3 files changed, 95 insertions(+), 6 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index acf83e4a9c..08cf652d20 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -50,9 +50,8 @@ int32_t tsdbInsertTableData2(STsdb *pTsdb, int64_t version, SVSubmitBlk *pSubmit int32_t tsdbDeleteTableData2(STsdb *pTsdb, int64_t version, tb_uid_t suid, tb_uid_t uid, TSKEY sKey, TSKEY eKey); /* SMemDataIter */ -void tsdbMemDataIterOpen(SMemDataIter *pIter, TSDBKEY *pKey, int8_t backward); -void tsdbMemDataIterClose(SMemDataIter *pIter); -void tsdbMemDataIterNext(SMemDataIter *pIter); +void tsdbMemDataIterOpen(SMemData *pMemData, TSDBKEY *pKey, int8_t backward, SMemDataIter *pIter); +bool tsdbMemDataIterNext(SMemDataIter *pIter); void tsdbMemDataIterGet(SMemDataIter *pIter, TSDBROW **ppRow); // tsdbCommit2.c ============================================================================================== @@ -928,8 +927,11 @@ struct SMemData { }; struct SMemDataIter { - SMemData *pMemData; - TSDBROW row; + SMemData *pMemData; + int8_t backward; + TSDBROW *pRow; + SMemSkipListNode *pNode; // current node + TSDBROW row; }; #endif diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit2.c b/source/dnode/vnode/src/tsdb/tsdbCommit2.c index 38e4795a83..afd5cd72ee 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit2.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit2.c @@ -137,7 +137,7 @@ static int32_t tsdbCommitToFile(SCommitH *pCHandle, int32_t fid) { // check if there are data in the time range for (; iMemData < nMemData; iMemData++) { SMemData *pMemData = (SMemData *)taosArrayGetP(pCHandle->pMemTable->aMemData, iMemData); - tsdbMemDataIterOpen(&iter, &(TSDBKEY){.ts = fidSKey, .version = 0}, 0); + tsdbMemDataIterOpen(pMemData, &(TSDBKEY){.ts = fidSKey, .version = 0}, 0, &iter); tsdbMemDataIterGet(&iter, &pRow); if (pRow->tsRow.ts >= fidSKey && pRow->tsRow.ts <= fidEKey) { diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c index dee025cae6..e581e9fe3d 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c @@ -42,6 +42,7 @@ static int32_t tGetTSDBRow(uint8_t *p, TSDBROW *pRow); static int8_t tsdbMemSkipListRandLevel(SMemSkipList *pSl); static int32_t tsdbInsertTableDataImpl(SMemTable *pMemTable, SMemData *pMemData, int64_t version, SVSubmitBlk *pSubmitBlk); +static void memDataMovePosTo(SMemData *pMemData, SMemSkipListNode **pos, TSDBKEY *pKey, int32_t flags); // SMemTable ============================================== int32_t tsdbMemTableCreate2(STsdb *pTsdb, SMemTable **ppMemTable) { @@ -160,6 +161,92 @@ _err: return code; } +void tsdbMemDataIterOpen(SMemData *pMemData, TSDBKEY *pKey, int8_t backward, SMemDataIter *pIter) { + SMemSkipListNode *pos[SL_MAX_LEVEL]; + + pIter->pMemData = pMemData; + pIter->backward = backward; + pIter->pRow = NULL; + if (pKey == NULL) { + // create from head or tail + if (backward) { + pIter->pNode = SL_NODE_BACKWARD(pMemData->sl.pTail, 0); + } else { + pIter->pNode = SL_NODE_FORWARD(pMemData->sl.pHead, 0); + } + } else { + // create from a key + if (backward) { + memDataMovePosTo(pMemData, pos, pKey, SL_MOVE_BACKWARD); + pIter->pNode = SL_NODE_BACKWARD(pos[0], 0); + } else { + memDataMovePosTo(pMemData, pos, pKey, 0); + pIter->pNode = SL_NODE_FORWARD(pos[0], 0); + } + } +} + +bool tsdbMemDataIterNext(SMemDataIter *pIter) { + SMemSkipListNode *pHead = pIter->pMemData->sl.pHead; + SMemSkipListNode *pTail = pIter->pMemData->sl.pTail; + + pIter->pRow = NULL; + if (pIter->backward) { + ASSERT(pIter->pNode != pTail); + + if (pIter->pNode == pHead) { + return false; + } + + pIter->pNode = SL_NODE_BACKWARD(pIter->pNode, 0); + if (pIter->pNode == pHead) { + return false; + } + } else { + ASSERT(pIter->pNode != pHead); + + if (pIter->pNode == pTail) { + return false; + } + + pIter->pNode = SL_NODE_FORWARD(pIter->pNode, 0); + if (pIter->pNode == pTail) { + return false; + } + } + + return true; +} + +void tsdbMemDataIterGet(SMemDataIter *pIter, TSDBROW **ppRow) { + if (pIter->pRow) { + *ppRow = pIter->pRow; + } else { + SMemSkipListNode *pHead = pIter->pMemData->sl.pHead; + SMemSkipListNode *pTail = pIter->pMemData->sl.pTail; + + if (pIter->backward) { + ASSERT(pIter->pNode != pTail); + + if (pIter->pNode == pHead) { + *ppRow = NULL; + } else { + tGetTSDBRow((uint8_t *)SL_NODE_DATA(pIter->pNode), &pIter->row); + *ppRow = &pIter->row; + } + } else { + ASSERT(pIter->pNode != pHead); + + if (pIter->pNode == pTail) { + *ppRow = NULL; + } else { + tGetTSDBRow((uint8_t *)SL_NODE_DATA(pIter->pNode), &pIter->row); + *ppRow = &pIter->row; + } + } + } +} + static int32_t tsdbGetOrCreateMemData(SMemTable *pMemTable, tb_uid_t suid, tb_uid_t uid, SMemData **ppMemData) { int32_t code = 0; int32_t idx = 0; From fe27ee265428b7e03d24b8e75424fe6e4cd57fe8 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 5 Jun 2022 03:47:41 +0000 Subject: [PATCH 16/24] more work --- source/dnode/vnode/src/inc/tsdb.h | 48 +++---- source/dnode/vnode/src/tsdb/tsdbCommit2.c | 144 +++++++++++++++++++- source/dnode/vnode/src/tsdb/tsdbFS.c | 4 +- source/dnode/vnode/src/tsdb/tsdbMemTable2.c | 3 + 4 files changed, 158 insertions(+), 41 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index 08cf652d20..02b9e4fcff 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -310,33 +310,13 @@ typedef struct { SBlock blocks[]; } SBlockInfo; -#ifdef TD_REFACTOR_3 -typedef struct { - int16_t colId; - uint16_t bitmap : 1; // 0: no bitmap if all rows are NORM, 1: has bitmap if has NULL/NORM rows - uint16_t reserve : 15; - int32_t len; - uint32_t type : 8; - uint32_t offset : 24; - int64_t sum; - int64_t max; - int64_t min; - int16_t maxIndex; - int16_t minIndex; - int16_t numOfNull; - uint8_t offsetH; - char padding[1]; -} SBlockCol; -#else typedef struct { int16_t colId; uint16_t type : 6; uint16_t blen : 10; // 0 no bitmap if all rows are NORM, > 0 bitmap length uint32_t len; // data length + bitmap length uint32_t offset; -} SBlockColV0; - -#define SBlockCol SBlockColV0 // latest SBlockCol definition +} SBlockCol; typedef struct { int16_t colId; @@ -346,11 +326,7 @@ typedef struct { int64_t sum; int64_t max; int64_t min; -} SAggrBlkColV0; - -#define SAggrBlkCol SAggrBlkColV0 // latest SAggrBlkCol definition - -#endif +} SAggrBlkCol; // Code here just for back-ward compatibility static FORCE_INLINE void tsdbSetBlockColOffset(SBlockCol *pBlockCol, uint32_t offset) { @@ -411,8 +387,7 @@ struct SReadH { #define TSDB_READ_COMP_BUF(rh) ((rh)->pCBuf) #define TSDB_READ_EXBUF(rh) ((rh)->pExBuf) -#define TSDB_BLOCK_STATIS_SIZE(ncols, blkVer) \ - (sizeof(SBlockData) + sizeof(SBlockColV##blkVer) * (ncols) + sizeof(TSCKSUM)) +#define TSDB_BLOCK_STATIS_SIZE(ncols, blkVer) (sizeof(SBlockData) + sizeof(SBlockCol) * (ncols) + sizeof(TSCKSUM)) static FORCE_INLINE size_t tsdbBlockStatisSize(int nCols, uint32_t blkVer) { switch (blkVer) { @@ -422,7 +397,7 @@ static FORCE_INLINE size_t tsdbBlockStatisSize(int nCols, uint32_t blkVer) { } } -#define TSDB_BLOCK_AGGR_SIZE(ncols, blkVer) (sizeof(SAggrBlkColV##blkVer) * (ncols) + sizeof(TSCKSUM)) +#define TSDB_BLOCK_AGGR_SIZE(ncols, blkVer) (sizeof(SAggrBlkCol) * (ncols) + sizeof(TSCKSUM)) static FORCE_INLINE size_t tsdbBlockAggrSize(int nCols, uint32_t blkVer) { switch (blkVer) { @@ -520,11 +495,11 @@ static FORCE_INLINE void *taosTZfree(void *ptr) { void tsdbGetRtnSnap(STsdb *pRepo, SRtn *pRtn); -static FORCE_INLINE int TSDB_KEY_FID(TSKEY key, int32_t days, int8_t precision) { +static FORCE_INLINE int TSDB_KEY_FID(TSKEY key, int32_t minutes, int8_t precision) { if (key < 0) { - return (int)((key + 1) / tsTickPerMin[precision] / days - 1); + return (int)((key + 1) / tsTickPerMin[precision] / minutes - 1); } else { - return (int)((key / tsTickPerMin[precision] / days)); + return (int)((key / tsTickPerMin[precision] / minutes)); } } @@ -877,12 +852,21 @@ struct SDelOp { SDelOp *pNext; }; +typedef struct { + tb_uid_t suid; + tb_uid_t uid; + int64_t version; + TSKEY sKey; + TSKEY eKey; +} SDelInfo; + struct SMemTable { STsdb *pTsdb; int32_t nRef; TSDBKEY minKey; TSDBKEY maxKey; int64_t nRows; + int64_t nDelOp; SArray *aSkmInfo; SArray *aMemData; }; diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit2.c b/source/dnode/vnode/src/tsdb/tsdbCommit2.c index afd5cd72ee..4948d8a0b1 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit2.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit2.c @@ -17,13 +17,22 @@ typedef struct { SMemTable *pMemTable; + int32_t minutes; + int8_t precision; + int32_t sfid; + int32_t efid; SReadH readh; + SDFileSet wSet; + SArray *aDelInfo; SArray *aBlkIdx; + SArray *aSupBlk; + SArray *aSubBlk; } SCommitH; static int32_t tsdbStartCommit(SCommitH *pCHandle, STsdb *pTsdb); static int32_t tsdbEndCommit(SCommitH *pCHandle); static int32_t tsdbCommitToFile(SCommitH *pCHandle, int32_t fid); +static int32_t tsdbCommitDelete(SCommitH *pCHandle); int32_t tsdbBegin2(STsdb *pTsdb) { int32_t code = 0; @@ -50,15 +59,18 @@ int32_t tsdbCommit2(STsdb *pTsdb) { } // commit - int32_t sfid; // todo - int32_t efid; // todo - for (int32_t fid = sfid; fid <= efid; fid++) { + for (int32_t fid = ch.sfid; fid <= ch.efid; fid++) { code = tsdbCommitToFile(&ch, fid); if (code) { goto _err; } } + code = tsdbCommitDelete(&ch); + if (code) { + goto _err; + } + // end commit code = tsdbEndCommit(&ch); if (code) { @@ -74,18 +86,77 @@ _err: } static int32_t tsdbStartCommit(SCommitH *pCHandle, STsdb *pTsdb) { - int32_t code = 0; + int32_t code = 0; + SMemTable *pMemTable = (SMemTable *)pTsdb->mem; + tsdbInfo("vgId:%d start to commit", TD_VID(pTsdb->pVnode)); + + // switch to commit ASSERT(pTsdb->imem == NULL && pTsdb->mem); pTsdb->imem = pTsdb->mem; pTsdb->mem = NULL; - // TODO + + // open handle + pCHandle->pMemTable = pMemTable; + pCHandle->minutes = pTsdb->keepCfg.days; + pCHandle->precision = pTsdb->keepCfg.precision; + pCHandle->sfid = TSDB_KEY_FID(pMemTable->minKey.ts, pCHandle->minutes, pCHandle->precision); + pCHandle->efid = TSDB_KEY_FID(pMemTable->maxKey.ts, pCHandle->minutes, pCHandle->precision); + + code = tsdbInitReadH(&pCHandle->readh, pTsdb); + if (code) { + goto _err; + } + pCHandle->aBlkIdx = taosArrayInit(1024, sizeof(SBlockIdx)); + if (pCHandle->aBlkIdx == NULL) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _err; + } + pCHandle->aSupBlk = taosArrayInit(1024, sizeof(SBlock)); + if (pCHandle->aSupBlk == NULL) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _err; + } + pCHandle->aSubBlk = taosArrayInit(1024, sizeof(SBlock)); + if (pCHandle->aSubBlk == NULL) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _err; + } + + // start FS transaction + tsdbStartFSTxn(pTsdb, 0, 0); + + return code; + +_err: return code; } static int32_t tsdbEndCommit(SCommitH *pCHandle) { - int32_t code = 0; - // TODO + int32_t code = 0; + STsdb *pTsdb = pCHandle->pMemTable->pTsdb; + SMemTable *pMemTable = (SMemTable *)pTsdb->imem; + + // end transaction + code = tsdbEndFSTxn(pTsdb); + if (code) { + goto _err; + } + + // close handle + taosArrayClear(pCHandle->aSubBlk); + taosArrayClear(pCHandle->aSupBlk); + taosArrayClear(pCHandle->aBlkIdx); + tsdbDestroyReadH(&pCHandle->readh); + + // destroy memtable (todo: unref it) + pTsdb->imem = NULL; + tsdbMemTableDestroy2(pMemTable); + + tsdbInfo("vgId:%d commit over", TD_VID(pTsdb->pVnode)); + return code; + +_err: return code; } @@ -193,6 +264,65 @@ static int32_t tsdbCommitToFile(SCommitH *pCHandle, int32_t fid) { return code; +_err: + return code; +} + +static int32_t delInfoCmprFn(const void *p1, const void *p2) { + SDelInfo *pDelInfo1 = (SDelInfo *)p1; + SDelInfo *pDelInfo2 = (SDelInfo *)p2; + + if (pDelInfo1->suid < pDelInfo2->suid) { + return -1; + } else if (pDelInfo1->suid > pDelInfo2->suid) { + return 1; + } + + if (pDelInfo1->uid < pDelInfo2->uid) { + return -1; + } else if (pDelInfo1->uid > pDelInfo2->uid) { + return 1; + } + + if (pDelInfo1->version < pDelInfo2->version) { + return -1; + } else if (pDelInfo1->version > pDelInfo2->version) { + return 1; + } + + return 0; +} +static int32_t tsdbCommitDelete(SCommitH *pCHandle) { + int32_t code = 0; + SDelInfo delInfo; + SMemData *pMemData; + + if (pCHandle->pMemTable->nDelOp == 0) goto _exit; + + // load del array (todo) + + // loop to append SDelInfo + for (int32_t iMemData = 0; iMemData < taosArrayGetSize(pCHandle->pMemTable->aMemData); iMemData++) { + pMemData = (SMemData *)taosArrayGetP(pCHandle->pMemTable->aMemData, iMemData); + + for (SDelOp *pDelOp = pMemData->delOpHead; pDelOp; pDelOp = pDelOp->pNext) { + delInfo = (SDelInfo){.suid = pMemData->suid, + .uid = pMemData->uid, + .version = pDelOp->version, + .sKey = pDelOp->sKey, + .eKey = pDelOp->eKey}; + if (taosArrayPush(pCHandle->aDelInfo, &delInfo) == NULL) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _err; + } + } + } + + taosArraySort(pCHandle->aDelInfo, delInfoCmprFn); + +_exit: + return code; + _err: return code; } \ No newline at end of file diff --git a/source/dnode/vnode/src/tsdb/tsdbFS.c b/source/dnode/vnode/src/tsdb/tsdbFS.c index c0ca2f9594..584a1ec49c 100644 --- a/source/dnode/vnode/src/tsdb/tsdbFS.c +++ b/source/dnode/vnode/src/tsdb/tsdbFS.c @@ -37,11 +37,11 @@ static void tsdbScanAndTryFixDFilesHeader(STsdb *pRepo, int32_t *nExpired); // static int tsdbProcessExpiredFS(STsdb *pRepo); // static int tsdbCreateMeta(STsdb *pRepo); -static void tsdbGetRootDir(int repoid, const char* dir, char dirName[]) { +static void tsdbGetRootDir(int repoid, const char *dir, char dirName[]) { snprintf(dirName, TSDB_FILENAME_LEN, "vnode/vnode%d/%s", repoid, dir); } -static void tsdbGetDataDir(int repoid, const char* dir, char dirName[]) { +static void tsdbGetDataDir(int repoid, const char *dir, char dirName[]) { snprintf(dirName, TSDB_FILENAME_LEN, "vnode/vnode%d/%s/data", repoid, dir); } diff --git a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c index e581e9fe3d..cf8557bba3 100644 --- a/source/dnode/vnode/src/tsdb/tsdbMemTable2.c +++ b/source/dnode/vnode/src/tsdb/tsdbMemTable2.c @@ -59,6 +59,7 @@ int32_t tsdbMemTableCreate2(STsdb *pTsdb, SMemTable **ppMemTable) { pMemTable->minKey = (TSDBKEY){.version = INT64_MAX, .ts = TSKEY_MAX}; pMemTable->maxKey = (TSDBKEY){.version = -1, .ts = TSKEY_MIN}; pMemTable->nRows = 0; + pMemTable->nDelOp = 0; pMemTable->aMemData = taosArrayInit(512, sizeof(SMemData *)); if (pMemTable->aMemData == NULL) { taosMemoryFree(pMemTable); @@ -149,6 +150,8 @@ int32_t tsdbDeleteTableData2(STsdb *pTsdb, int64_t version, tb_uid_t suid, tb_ui // update the state of pMemTable, pMemData, last and lastrow (todo) } + pMemTable->nDelOp++; + tsdbDebug("vgId:%d, delete data from table suid:%" PRId64 " uid:%" PRId64 " sKey:%" PRId64 " eKey:%" PRId64 " since %s", TD_VID(pTsdb->pVnode), suid, uid, sKey, eKey, tstrerror(code)); From 5a8591052d805c9af16b3c974c88e392a71edc64 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 5 Jun 2022 07:46:57 +0000 Subject: [PATCH 17/24] more work --- source/dnode/vnode/src/tsdb/tsdbCommit2.c | 201 +++++++++++++++++----- 1 file changed, 156 insertions(+), 45 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit2.c b/source/dnode/vnode/src/tsdb/tsdbCommit2.c index 4948d8a0b1..6c24396810 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit2.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit2.c @@ -19,20 +19,18 @@ typedef struct { SMemTable *pMemTable; int32_t minutes; int8_t precision; - int32_t sfid; - int32_t efid; + TSKEY nCommitKey; SReadH readh; SDFileSet wSet; - SArray *aDelInfo; SArray *aBlkIdx; SArray *aSupBlk; SArray *aSubBlk; + SArray *aDelInfo; } SCommitH; -static int32_t tsdbStartCommit(SCommitH *pCHandle, STsdb *pTsdb); -static int32_t tsdbEndCommit(SCommitH *pCHandle); -static int32_t tsdbCommitToFile(SCommitH *pCHandle, int32_t fid); -static int32_t tsdbCommitDelete(SCommitH *pCHandle); +static int32_t tsdbCommitStart(SCommitH *pCHandle, STsdb *pTsdb); +static int32_t tsdbCommitEnd(SCommitH *pCHandle); +static int32_t tsdbCommitImpl(SCommitH *pCHandle); int32_t tsdbBegin2(STsdb *pTsdb) { int32_t code = 0; @@ -53,26 +51,19 @@ int32_t tsdbCommit2(STsdb *pTsdb) { SCommitH ch = {0}; // start to commit - code = tsdbStartCommit(&ch, pTsdb); + code = tsdbCommitStart(&ch, pTsdb); if (code) { goto _exit; } // commit - for (int32_t fid = ch.sfid; fid <= ch.efid; fid++) { - code = tsdbCommitToFile(&ch, fid); - if (code) { - goto _err; - } - } - - code = tsdbCommitDelete(&ch); + code = tsdbCommitImpl(&ch); if (code) { goto _err; } // end commit - code = tsdbEndCommit(&ch); + code = tsdbCommitEnd(&ch); if (code) { goto _exit; } @@ -81,11 +72,11 @@ _exit: return code; _err: - // TODO: rollback + tsdbError("vgId:%d failed to commit since %s", TD_VID(pTsdb->pVnode), tstrerror(code)); return code; } -static int32_t tsdbStartCommit(SCommitH *pCHandle, STsdb *pTsdb) { +static int32_t tsdbCommitStart(SCommitH *pCHandle, STsdb *pTsdb) { int32_t code = 0; SMemTable *pMemTable = (SMemTable *)pTsdb->mem; @@ -100,28 +91,32 @@ static int32_t tsdbStartCommit(SCommitH *pCHandle, STsdb *pTsdb) { pCHandle->pMemTable = pMemTable; pCHandle->minutes = pTsdb->keepCfg.days; pCHandle->precision = pTsdb->keepCfg.precision; - pCHandle->sfid = TSDB_KEY_FID(pMemTable->minKey.ts, pCHandle->minutes, pCHandle->precision); - pCHandle->efid = TSDB_KEY_FID(pMemTable->maxKey.ts, pCHandle->minutes, pCHandle->precision); + pCHandle->nCommitKey = pMemTable->minKey.ts; code = tsdbInitReadH(&pCHandle->readh, pTsdb); if (code) { goto _err; } - pCHandle->aBlkIdx = taosArrayInit(1024, sizeof(SBlockIdx)); + pCHandle->aBlkIdx = taosArrayInit(0, sizeof(SBlockIdx)); if (pCHandle->aBlkIdx == NULL) { code = TSDB_CODE_OUT_OF_MEMORY; goto _err; } - pCHandle->aSupBlk = taosArrayInit(1024, sizeof(SBlock)); + pCHandle->aSupBlk = taosArrayInit(0, sizeof(SBlock)); if (pCHandle->aSupBlk == NULL) { code = TSDB_CODE_OUT_OF_MEMORY; goto _err; } - pCHandle->aSubBlk = taosArrayInit(1024, sizeof(SBlock)); + pCHandle->aSubBlk = taosArrayInit(0, sizeof(SBlock)); if (pCHandle->aSubBlk == NULL) { code = TSDB_CODE_OUT_OF_MEMORY; goto _err; } + pCHandle->aDelInfo = taosArrayInit(0, sizeof(SDelInfo)); + if (pCHandle->aDelInfo == NULL) { + code = TSDB_CODE_OUT_OF_MEMORY; + goto _err; + } // start FS transaction tsdbStartFSTxn(pTsdb, 0, 0); @@ -132,7 +127,7 @@ _err: return code; } -static int32_t tsdbEndCommit(SCommitH *pCHandle) { +static int32_t tsdbCommitEnd(SCommitH *pCHandle) { int32_t code = 0; STsdb *pTsdb = pCHandle->pMemTable->pTsdb; SMemTable *pMemTable = (SMemTable *)pTsdb->imem; @@ -144,6 +139,7 @@ static int32_t tsdbEndCommit(SCommitH *pCHandle) { } // close handle + taosArrayClear(pCHandle->aDelInfo); taosArrayClear(pCHandle->aSubBlk); taosArrayClear(pCHandle->aSupBlk); taosArrayClear(pCHandle->aBlkIdx); @@ -160,10 +156,29 @@ _err: return code; } -static int32_t tsdbCommitTableData(SCommitH *pCHandle, SMemData *pMemData, SBlockIdx *pBlockIdx) { +static int32_t tsdbCommitTableStart(SCommitH *pCHandle) { + int32_t code = 0; + // TODO + return code; +} + +static int32_t tsdbCommitTableEnd(SCommitH *pCHandle) { + int32_t code = 0; + // TODO + return code; +} + +static int32_t tsdbCommitTable(SCommitH *pCHandle, SMemData *pMemData, SBlockIdx *pBlockIdx) { int32_t code = 0; SMemDataIter iter = {0}; + // commit table start + code = tsdbCommitTableStart(pCHandle); + if (code) { + goto _err; + } + + // commit table impl if (pMemData && pBlockIdx) { // merge } else if (pMemData) { @@ -172,6 +187,15 @@ static int32_t tsdbCommitTableData(SCommitH *pCHandle, SMemData *pMemData, SBloc // save old ones } + // commit table end + code = tsdbCommitTableEnd(pCHandle); + if (code) { + goto _err; + } + + return code; + +_err: return code; } @@ -193,38 +217,58 @@ static int32_t tsdbTableIdCmprFn(const void *p1, const void *p2) { return 0; } -static int32_t tsdbCommitToFile(SCommitH *pCHandle, int32_t fid) { + +static int32_t tsdbWriteBlockIdx(SDFile *pFile, SArray *pArray, uint8_t **ppBuf) { + int32_t code = 0; + // TODO + return code; +} + +static int32_t tsdbCommitFileStart(SCommitH *pCHandle, int32_t fid) { + int32_t code = 0; + // TODO + return code; +} + +static int32_t tsdbCommitFileEnd(SCommitH *pCHandle) { + int32_t code = 0; + // TODO + return code; +} + +static int32_t tsdbCommitFile(SCommitH *pCHandle, int32_t fid) { int32_t code = 0; SMemDataIter iter = {0}; TSDBROW *pRow = NULL; int8_t hasData = 0; TSKEY fidSKey; TSKEY fidEKey; - int32_t iMemData = 0; - int32_t nMemData = taosArrayGetSize(pCHandle->pMemTable->aMemData); - int32_t iBlockIdx = 0; + int32_t iMemData; + int32_t nMemData; + int32_t iBlockIdx; int32_t nBlockIdx; - // check if there are data in the time range - for (; iMemData < nMemData; iMemData++) { - SMemData *pMemData = (SMemData *)taosArrayGetP(pCHandle->pMemTable->aMemData, iMemData); - tsdbMemDataIterOpen(pMemData, &(TSDBKEY){.ts = fidSKey, .version = 0}, 0, &iter); - tsdbMemDataIterGet(&iter, &pRow); - - if (pRow->tsRow.ts >= fidSKey && pRow->tsRow.ts <= fidEKey) { - hasData = 1; - break; - } - } - - if (!hasData) return code; + pCHandle->nCommitKey = TSKEY_MAX; // create or open the file to commit(todo) + code = tsdbCommitFileStart(pCHandle, fid); + if (code) { + goto _err; + } // loop to commit each table data + iMemData = 0; + nMemData = taosArrayGetSize(pCHandle->pMemTable->aMemData); + iBlockIdx = 0; nBlockIdx = 0; for (;;) { - if (iBlockIdx >= nBlockIdx && iMemData >= nMemData) break; + if (iBlockIdx >= nBlockIdx && iMemData >= nMemData) { + // code = tsdbWriteBlockIdx(); + // if (code) { + // goto _err; + // } + break; + } SMemData *pMemData = NULL; SBlockIdx *pBlockIdx = NULL; @@ -256,12 +300,42 @@ static int32_t tsdbCommitToFile(SCommitH *pCHandle, int32_t fid) { } } - code = tsdbCommitTableData(pCHandle, pMemData, pBlockIdx); + code = tsdbCommitTable(pCHandle, pMemData, pBlockIdx); if (code) { goto _err; } } + // close file + code = tsdbCommitFileEnd(pCHandle); + if (code) { + goto _err; + } + + return code; + +_err: + return code; +} + +static int32_t tsdbCommitData(SCommitH *pCHandle) { + int32_t code = 0; + int32_t fid; + + if (pCHandle->pMemTable->nRows == 0) goto _exit; + + // loop to commit to each file + for (;;) { + if (pCHandle->nCommitKey == TSKEY_MAX) break; + + fid = TSDB_KEY_FID(pCHandle->nCommitKey, pCHandle->minutes, pCHandle->precision); + code = tsdbCommitFile(pCHandle, fid); + if (code) { + goto _err; + } + } + +_exit: return code; _err: @@ -320,9 +394,46 @@ static int32_t tsdbCommitDelete(SCommitH *pCHandle) { taosArraySort(pCHandle->aDelInfo, delInfoCmprFn); + // write to new file + _exit: return code; +_err: + return code; +} + +static int32_t tsdbCommitCache(SCommitH *pCHandle) { + int32_t code = 0; + // TODO + return code; +} + +static int32_t tsdbCommitImpl(SCommitH *pCHandle) { + int32_t code = 0; + + // commit data + code = tsdbCommitData(pCHandle); + if (code) { + goto _err; + } + + // commit delete + code = tsdbCommitDelete(pCHandle); + if (code) { + goto _err; + } + + // commit cache if need (todo) + if (0) { + code = tsdbCommitCache(pCHandle); + if (code) { + goto _err; + } + } + + return code; + _err: return code; } \ No newline at end of file From c59b52855b8ebce9ec1bd37b8c4dcd5188a1fe8f Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 5 Jun 2022 08:42:18 +0000 Subject: [PATCH 18/24] more refact --- source/dnode/vnode/src/inc/tsdb.h | 4 +- source/dnode/vnode/src/tsdb/tsdbCommit2.c | 87 +++++++++++------------ source/dnode/vnode/src/tsdb/tsdbFile.c | 2 +- 3 files changed, 45 insertions(+), 48 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index 02b9e4fcff..5ffa367ff7 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -263,12 +263,12 @@ struct TSDBKEY { }; typedef struct { + uint64_t suid; + uint64_t uid; uint32_t len; uint32_t offset; uint32_t hasLast : 2; uint32_t numOfBlocks : 30; - uint64_t suid; - uint64_t uid; TSDBKEY maxKey; } SBlockIdx; diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit2.c b/source/dnode/vnode/src/tsdb/tsdbCommit2.c index 6c24396810..321667c55b 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit2.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit2.c @@ -20,6 +20,9 @@ typedef struct { int32_t minutes; int8_t precision; TSKEY nCommitKey; + int32_t fid; + TSKEY minKey; + TSKEY maxKey; SReadH readh; SDFileSet wSet; SArray *aBlkIdx; @@ -180,11 +183,11 @@ static int32_t tsdbCommitTable(SCommitH *pCHandle, SMemData *pMemData, SBlockIdx // commit table impl if (pMemData && pBlockIdx) { - // merge + // TODO } else if (pMemData) { - // new one + // TODO } else { - // save old ones + // TODO } // commit table end @@ -224,9 +227,13 @@ static int32_t tsdbWriteBlockIdx(SDFile *pFile, SArray *pArray, uint8_t **ppBuf) return code; } -static int32_t tsdbCommitFileStart(SCommitH *pCHandle, int32_t fid) { - int32_t code = 0; - // TODO +static int32_t tsdbCommitFileStart(SCommitH *pCHandle) { + int32_t code = 0; + STsdb *pTsdb = pCHandle->pMemTable->pTsdb; + SDFileSet *pSet = NULL; + + taosArrayClear(pCHandle->aBlkIdx); + return code; } @@ -236,61 +243,50 @@ static int32_t tsdbCommitFileEnd(SCommitH *pCHandle) { return code; } -static int32_t tsdbCommitFile(SCommitH *pCHandle, int32_t fid) { - int32_t code = 0; - SMemDataIter iter = {0}; - TSDBROW *pRow = NULL; - int8_t hasData = 0; - TSKEY fidSKey; - TSKEY fidEKey; - int32_t iMemData; - int32_t nMemData; - int32_t iBlockIdx; - int32_t nBlockIdx; +static int32_t tsdbCommitFile(SCommitH *pCHandle) { + int32_t code = 0; + SMemData *pMemData; + SBlockIdx *pBlockIdx; + int32_t iMemData; + int32_t nMemData; + int32_t iBlockIdx; + int32_t nBlockIdx; - pCHandle->nCommitKey = TSKEY_MAX; - - // create or open the file to commit(todo) - code = tsdbCommitFileStart(pCHandle, fid); + // commit file start + code = tsdbCommitFileStart(pCHandle); if (code) { goto _err; } - // loop to commit each table data + // commit file impl iMemData = 0; nMemData = taosArrayGetSize(pCHandle->pMemTable->aMemData); iBlockIdx = 0; - nBlockIdx = 0; - for (;;) { - if (iBlockIdx >= nBlockIdx && iMemData >= nMemData) { - // code = tsdbWriteBlockIdx(); - // if (code) { - // goto _err; - // } - break; - } + nBlockIdx = 0; // todo - SMemData *pMemData = NULL; - SBlockIdx *pBlockIdx = NULL; + for (;;) { + if (iMemData >= nMemData && iBlockIdx >= nBlockIdx) break; + + pMemData = NULL; + pBlockIdx = NULL; if (iMemData < nMemData) { - pMemData = (SMemData *)taosArrayGetP(pCHandle->pMemTable->aMemData, iBlockIdx); + pMemData = (SMemData *)taosArrayGetP(pCHandle->pMemTable->aMemData, iMemData); } if (iBlockIdx < nBlockIdx) { - // pBlockIdx + // pBlockIdx = ; } if (pMemData && pBlockIdx) { - int32_t c = tsdbTableIdCmprFn(&(TABLEID){.suid = pMemData->suid, .uid = pMemData->uid}, - &(TABLEID){.suid = pBlockIdx->suid, .uid = pBlockIdx->uid}); - if (c == 0) { + int32_t c = tsdbTableIdCmprFn(pMemData, pBlockIdx); + if (c < 0) { iMemData++; - iBlockIdx++; - } else if (c < 0) { pBlockIdx = NULL; + } else if (c == 0) { iMemData++; - } else { - pMemData = NULL; iBlockIdx++; + } else { + iBlockIdx++; + pMemData = NULL; } } else { if (pMemData) { @@ -306,7 +302,7 @@ static int32_t tsdbCommitFile(SCommitH *pCHandle, int32_t fid) { } } - // close file + // commit file end code = tsdbCommitFileEnd(pCHandle); if (code) { goto _err; @@ -328,8 +324,9 @@ static int32_t tsdbCommitData(SCommitH *pCHandle) { for (;;) { if (pCHandle->nCommitKey == TSKEY_MAX) break; - fid = TSDB_KEY_FID(pCHandle->nCommitKey, pCHandle->minutes, pCHandle->precision); - code = tsdbCommitFile(pCHandle, fid); + pCHandle->fid = TSDB_KEY_FID(pCHandle->nCommitKey, pCHandle->minutes, pCHandle->precision); + tsdbGetFidKeyRange(pCHandle->minutes, pCHandle->precision, pCHandle->fid, &pCHandle->minKey, &pCHandle->maxKey); + code = tsdbCommitFile(pCHandle); if (code) { goto _err; } diff --git a/source/dnode/vnode/src/tsdb/tsdbFile.c b/source/dnode/vnode/src/tsdb/tsdbFile.c index 4198a94655..e7c8c7ccd9 100644 --- a/source/dnode/vnode/src/tsdb/tsdbFile.c +++ b/source/dnode/vnode/src/tsdb/tsdbFile.c @@ -25,7 +25,7 @@ static const char *TSDB_FNAME_SUFFIX[] = { "meta", // TSDB_FILE_META }; -static void tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, const char* dname, char *fname); +static void tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, const char *dname, char *fname); // static int tsdbRollBackMFile(SMFile *pMFile); static int tsdbEncodeDFInfo(void **buf, SDFInfo *pInfo); static void *tsdbDecodeDFInfo(void *buf, SDFInfo *pInfo); From 8c75980679f68b7b87a189cdd273da06f0c09e1b Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 5 Jun 2022 08:46:10 +0000 Subject: [PATCH 19/24] more refact --- source/dnode/vnode/src/inc/tsdb.h | 4 +--- source/dnode/vnode/src/tsdb/tsdbFS.c | 11 ----------- 2 files changed, 1 insertion(+), 14 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index 5ffa367ff7..94e0a8cd07 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -189,9 +189,7 @@ typedef struct { struct STsdbFS { TdThreadRwlock lock; - SFSStatus *cstatus; // current status - SHashObj *metaCache; // meta cache - SHashObj *metaCacheComp; // meta cache for compact + SFSStatus *cstatus; // current status bool intxn; SFSStatus *nstatus; // new status }; diff --git a/source/dnode/vnode/src/tsdb/tsdbFS.c b/source/dnode/vnode/src/tsdb/tsdbFS.c index 584a1ec49c..19a79ac5c4 100644 --- a/source/dnode/vnode/src/tsdb/tsdbFS.c +++ b/source/dnode/vnode/src/tsdb/tsdbFS.c @@ -216,16 +216,7 @@ STsdbFS *tsdbNewFS(const STsdbKeepCfg *pCfg) { return NULL; } - pfs->metaCache = taosHashInit(4096, taosGetDefaultHashFunction(TSDB_DATA_TYPE_BIGINT), true, HASH_NO_LOCK); - if (pfs->metaCache == NULL) { - terrno = TSDB_CODE_TDB_OUT_OF_MEMORY; - tsdbFreeFS(pfs); - return NULL; - } - pfs->intxn = false; - pfs->metaCacheComp = NULL; - pfs->nstatus = tsdbNewFSStatus(maxFSet); if (pfs->nstatus == NULL) { tsdbFreeFS(pfs); @@ -238,8 +229,6 @@ STsdbFS *tsdbNewFS(const STsdbKeepCfg *pCfg) { void *tsdbFreeFS(STsdbFS *pfs) { if (pfs) { pfs->nstatus = tsdbFreeFSStatus(pfs->nstatus); - taosHashCleanup(pfs->metaCache); - pfs->metaCache = NULL; pfs->cstatus = tsdbFreeFSStatus(pfs->cstatus); taosThreadRwlockDestroy(&(pfs->lock)); taosMemoryFree(pfs); From 0633c25a2219b4616b9aa1be6300be95e84b3e0d Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 5 Jun 2022 09:02:25 +0000 Subject: [PATCH 20/24] refact --- source/dnode/vnode/src/inc/tsdb.h | 151 +++++------------------ source/dnode/vnode/src/tsdb/tsdbCommit.c | 14 +-- source/dnode/vnode/src/tsdb/tsdbFile.c | 95 ++++++++++++++ 3 files changed, 133 insertions(+), 127 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index 94e0a8cd07..42d56eb3e3 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -58,8 +58,31 @@ void tsdbMemDataIterGet(SMemDataIter *pIter, TSDBROW **ppRow); int32_t tsdbBegin2(STsdb *pTsdb); int32_t tsdbCommit2(STsdb *pTsdb); +// tsdbFile.c ============================================================================================== +typedef int32_t TSDB_FILE_T; +typedef struct SDFInfo SDFInfo; +typedef struct SDFile SDFile; +typedef struct SDFileSet SDFileSet; + +void tsdbInitDFile(STsdb *pRepo, SDFile *pDFile, SDiskID did, int fid, uint32_t ver, TSDB_FILE_T ftype); +void tsdbInitDFileEx(SDFile *pDFile, SDFile *pODFile); +int tsdbOpenDFile(SDFile *pDFile, int flags); +void tsdbCloseDFile(SDFile *pDFile); +int64_t tsdbSeekDFile(SDFile *pDFile, int64_t offset, int whence); +int64_t tsdbWriteDFile(SDFile *pDFile, void *buf, int64_t nbyte); +void tsdbUpdateDFileMagic(SDFile *pDFile, void *pCksm); +int tsdbAppendDFile(SDFile *pDFile, void *buf, int64_t nbyte, int64_t *offset); +int tsdbRemoveDFile(SDFile *pDFile); +int64_t tsdbReadDFile(SDFile *pDFile, void *buf, int64_t nbyte); +int tsdbCopyDFile(SDFile *pSrc, SDFile *pDest); +int tsdbEncodeSDFile(void **buf, SDFile *pDFile); +void *tsdbDecodeSDFile(STsdb *pRepo, void *buf, SDFile *pDFile); +int tsdbCreateDFile(STsdb *pRepo, SDFile *pDFile, bool updateHeader, TSDB_FILE_T fType); +int tsdbUpdateDFileHeader(SDFile *pDFile); +int tsdbLoadDFileHeader(SDFile *pDFile, SDFInfo *pInfo); +int tsdbParseDFilename(const char *fname, int *vid, int *fid, TSDB_FILE_T *ftype, uint32_t *version); + // tsdbMemTable ================ -typedef struct STsdbRow STsdbRow; typedef struct STbData STbData; typedef struct STsdbMemTable STsdbMemTable; typedef struct SMergeInfo SMergeInfo; @@ -75,10 +98,6 @@ int tsdbLoadDataFromCache(STsdb *pTsdb, STable *pTable, SSkipListIterator *pIte // tsdbFS ================ typedef struct STsdbFS STsdbFS; -// tsdbSma ================ -typedef struct SSmaEnv SSmaEnv; -typedef struct SSmaEnvs SSmaEnvs; - // structs typedef struct { int minFid; @@ -113,7 +132,7 @@ struct STable { #define TABLE_TID(t) (t)->tid #define TABLE_UID(t) (t)->uid -int tsdbPrepareCommit(STsdb *pTsdb); +// int tsdbPrepareCommit(STsdb *pTsdb); typedef enum { TSDB_FILE_HEAD = 0, // .head TSDB_FILE_DATA, // .data @@ -124,7 +143,7 @@ typedef enum { TSDB_FILE_META, // meta } E_TSDB_FILE_T; -typedef struct { +struct SDFInfo { uint32_t magic; uint32_t fver; uint32_t len; @@ -133,22 +152,22 @@ typedef struct { uint32_t offset; uint64_t size; uint64_t tombSize; -} SDFInfo; +}; -typedef struct { +struct SDFile { SDFInfo info; STfsFile f; TdFilePtr pFile; uint8_t state; -} SDFile; +}; -typedef struct { +struct SDFileSet { int fid; int8_t state; // -128~127 uint8_t ver; // 0~255, DFileSet version uint16_t reserve; SDFile files[TSDB_FILE_MAX]; -} SDFileSet; +}; struct STbData { tb_uid_t uid; @@ -521,7 +540,6 @@ static FORCE_INLINE int tsdbGetFidLevel(int fid, SRtn *pRtn) { #define TSDB_FILE_STATE_OK 0 #define TSDB_FILE_STATE_BAD 1 -#define TSDB_FILE_INFO(tf) (&((tf)->info)) #define TSDB_FILE_F(tf) (&((tf)->f)) #define TSDB_FILE_PFILE(tf) ((tf)->pFile) #define TSDB_FILE_FULL_NAME(tf) (TSDB_FILE_F(tf)->aname) @@ -539,7 +557,6 @@ static FORCE_INLINE int tsdbGetFidLevel(int fid, SRtn *pRtn) { #define TSDB_FILE_IS_OK(tf) (TSDB_FILE_STATE(tf) == TSDB_FILE_STATE_OK) #define TSDB_FILE_IS_BAD(tf) (TSDB_FILE_STATE(tf) == TSDB_FILE_STATE_BAD) -typedef int32_t TSDB_FILE_T; typedef enum { TSDB_FS_VER_0 = 0, TSDB_FS_VER_MAX, @@ -560,112 +577,6 @@ static FORCE_INLINE uint32_t tsdbGetDFSVersion(TSDB_FILE_T fType) { // latest v } } -void tsdbInitDFile(STsdb *pRepo, SDFile *pDFile, SDiskID did, int fid, uint32_t ver, TSDB_FILE_T ftype); -void tsdbInitDFileEx(SDFile *pDFile, SDFile *pODFile); -int tsdbEncodeSDFile(void **buf, SDFile *pDFile); -void *tsdbDecodeSDFile(STsdb *pRepo, void *buf, SDFile *pDFile); -int tsdbCreateDFile(STsdb *pRepo, SDFile *pDFile, bool updateHeader, TSDB_FILE_T fType); -int tsdbUpdateDFileHeader(SDFile *pDFile); -int tsdbLoadDFileHeader(SDFile *pDFile, SDFInfo *pInfo); -int tsdbParseDFilename(const char *fname, int *vid, int *fid, TSDB_FILE_T *ftype, uint32_t *version); - -static FORCE_INLINE void tsdbSetDFileInfo(SDFile *pDFile, SDFInfo *pInfo) { pDFile->info = *pInfo; } - -static FORCE_INLINE int tsdbOpenDFile(SDFile *pDFile, int flags) { - ASSERT(!TSDB_FILE_OPENED(pDFile)); - - pDFile->pFile = taosOpenFile(TSDB_FILE_FULL_NAME(pDFile), flags); - if (pDFile->pFile == NULL) { - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; - } - - return 0; -} - -static FORCE_INLINE void tsdbCloseDFile(SDFile *pDFile) { - if (TSDB_FILE_OPENED(pDFile)) { - taosCloseFile(&pDFile->pFile); - TSDB_FILE_SET_CLOSED(pDFile); - } -} - -static FORCE_INLINE int64_t tsdbSeekDFile(SDFile *pDFile, int64_t offset, int whence) { - // ASSERT(TSDB_FILE_OPENED(pDFile)); - - int64_t loffset = taosLSeekFile(TSDB_FILE_PFILE(pDFile), offset, whence); - if (loffset < 0) { - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; - } - - return loffset; -} - -static FORCE_INLINE int64_t tsdbWriteDFile(SDFile *pDFile, void *buf, int64_t nbyte) { - ASSERT(TSDB_FILE_OPENED(pDFile)); - - int64_t nwrite = taosWriteFile(pDFile->pFile, buf, nbyte); - if (nwrite < nbyte) { - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; - } - - return nwrite; -} - -static FORCE_INLINE void tsdbUpdateDFileMagic(SDFile *pDFile, void *pCksm) { - pDFile->info.magic = taosCalcChecksum(pDFile->info.magic, (uint8_t *)(pCksm), sizeof(TSCKSUM)); -} - -static FORCE_INLINE int tsdbAppendDFile(SDFile *pDFile, void *buf, int64_t nbyte, int64_t *offset) { - ASSERT(TSDB_FILE_OPENED(pDFile)); - - int64_t toffset; - - if ((toffset = tsdbSeekDFile(pDFile, 0, SEEK_END)) < 0) { - return -1; - } - - ASSERT(pDFile->info.size == toffset); - - if (offset) { - *offset = toffset; - } - - if (tsdbWriteDFile(pDFile, buf, nbyte) < 0) { - return -1; - } - - pDFile->info.size += nbyte; - - return (int)nbyte; -} - -static FORCE_INLINE int tsdbRemoveDFile(SDFile *pDFile) { return tfsRemoveFile(TSDB_FILE_F(pDFile)); } - -static FORCE_INLINE int64_t tsdbReadDFile(SDFile *pDFile, void *buf, int64_t nbyte) { - ASSERT(TSDB_FILE_OPENED(pDFile)); - - int64_t nread = taosReadFile(pDFile->pFile, buf, nbyte); - if (nread < 0) { - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; - } - - return nread; -} - -static FORCE_INLINE int tsdbCopyDFile(SDFile *pSrc, SDFile *pDest) { - if (tfsCopyFile(TSDB_FILE_F(pSrc), TSDB_FILE_F(pDest)) < 0) { - terrno = TAOS_SYSTEM_ERROR(errno); - return -1; - } - - tsdbSetDFileInfo(pDest, TSDB_FILE_INFO(pSrc)); - return 0; -} - // =============== SDFileSet typedef struct { diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit.c b/source/dnode/vnode/src/tsdb/tsdbCommit.c index 3013f6deef..64e2b917ae 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit.c @@ -218,15 +218,15 @@ static int tsdbApplyRtnOnFSet(STsdb *pRepo, SDFileSet *pSet, SRtn *pRtn) { return 0; } -int tsdbPrepareCommit(STsdb *pTsdb) { - if (pTsdb->mem == NULL) return 0; +// int tsdbPrepareCommit(STsdb *pTsdb) { +// if (pTsdb->mem == NULL) return 0; - ASSERT(pTsdb->imem == NULL); +// ASSERT(pTsdb->imem == NULL); - pTsdb->imem = pTsdb->mem; - pTsdb->mem = NULL; - return 0; -} +// pTsdb->imem = pTsdb->mem; +// pTsdb->mem = NULL; +// return 0; +// } void tsdbGetRtnSnap(STsdb *pRepo, SRtn *pRtn) { STsdbKeepCfg *pCfg = REPO_KEEP_CFG(pRepo); diff --git a/source/dnode/vnode/src/tsdb/tsdbFile.c b/source/dnode/vnode/src/tsdb/tsdbFile.c index e7c8c7ccd9..1e9c256d9c 100644 --- a/source/dnode/vnode/src/tsdb/tsdbFile.c +++ b/source/dnode/vnode/src/tsdb/tsdbFile.c @@ -447,4 +447,99 @@ static void tsdbGetFilename(int vid, int fid, uint32_t ver, TSDB_FILE_T ftype, c snprintf(fname, TSDB_FILENAME_LEN, "vnode/vnode%d/tsdb/%s-ver%" PRIu32, vid, TSDB_FNAME_SUFFIX[ftype], ver); } } +} + +int tsdbOpenDFile(SDFile *pDFile, int flags) { + ASSERT(!TSDB_FILE_OPENED(pDFile)); + + pDFile->pFile = taosOpenFile(TSDB_FILE_FULL_NAME(pDFile), flags); + if (pDFile->pFile == NULL) { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } + + return 0; +} + +void tsdbCloseDFile(SDFile *pDFile) { + if (TSDB_FILE_OPENED(pDFile)) { + taosCloseFile(&pDFile->pFile); + TSDB_FILE_SET_CLOSED(pDFile); + } +} + +int64_t tsdbSeekDFile(SDFile *pDFile, int64_t offset, int whence) { + // ASSERT(TSDB_FILE_OPENED(pDFile)); + + int64_t loffset = taosLSeekFile(TSDB_FILE_PFILE(pDFile), offset, whence); + if (loffset < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } + + return loffset; +} + +int64_t tsdbWriteDFile(SDFile *pDFile, void *buf, int64_t nbyte) { + ASSERT(TSDB_FILE_OPENED(pDFile)); + + int64_t nwrite = taosWriteFile(pDFile->pFile, buf, nbyte); + if (nwrite < nbyte) { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } + + return nwrite; +} + +void tsdbUpdateDFileMagic(SDFile *pDFile, void *pCksm) { + pDFile->info.magic = taosCalcChecksum(pDFile->info.magic, (uint8_t *)(pCksm), sizeof(TSCKSUM)); +} + +int tsdbAppendDFile(SDFile *pDFile, void *buf, int64_t nbyte, int64_t *offset) { + ASSERT(TSDB_FILE_OPENED(pDFile)); + + int64_t toffset; + + if ((toffset = tsdbSeekDFile(pDFile, 0, SEEK_END)) < 0) { + return -1; + } + + ASSERT(pDFile->info.size == toffset); + + if (offset) { + *offset = toffset; + } + + if (tsdbWriteDFile(pDFile, buf, nbyte) < 0) { + return -1; + } + + pDFile->info.size += nbyte; + + return (int)nbyte; +} + +int tsdbRemoveDFile(SDFile *pDFile) { return tfsRemoveFile(TSDB_FILE_F(pDFile)); } + +int64_t tsdbReadDFile(SDFile *pDFile, void *buf, int64_t nbyte) { + ASSERT(TSDB_FILE_OPENED(pDFile)); + + int64_t nread = taosReadFile(pDFile->pFile, buf, nbyte); + if (nread < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } + + return nread; +} + +int tsdbCopyDFile(SDFile *pSrc, SDFile *pDest) { + if (tfsCopyFile(TSDB_FILE_F(pSrc), TSDB_FILE_F(pDest)) < 0) { + terrno = TAOS_SYSTEM_ERROR(errno); + return -1; + } + + pDest->info = pSrc->info; + return 0; } \ No newline at end of file From 40a396563bd7feb9fc23a339480f6fb771e5a668 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 5 Jun 2022 09:23:42 +0000 Subject: [PATCH 21/24] more refact --- source/dnode/vnode/src/inc/tsdb.h | 97 +++++----------------- source/dnode/vnode/src/tsdb/tsdbCommit.c | 2 +- source/dnode/vnode/src/tsdb/tsdbFile.c | 38 +++++++++ source/dnode/vnode/src/tsdb/tsdbReadImpl.c | 6 +- 4 files changed, 63 insertions(+), 80 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index 42d56eb3e3..d2429391ab 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -64,6 +64,7 @@ typedef struct SDFInfo SDFInfo; typedef struct SDFile SDFile; typedef struct SDFileSet SDFileSet; +// SDFile void tsdbInitDFile(STsdb *pRepo, SDFile *pDFile, SDiskID did, int fid, uint32_t ver, TSDB_FILE_T ftype); void tsdbInitDFileEx(SDFile *pDFile, SDFile *pODFile); int tsdbOpenDFile(SDFile *pDFile, int flags); @@ -82,6 +83,26 @@ int tsdbUpdateDFileHeader(SDFile *pDFile); int tsdbLoadDFileHeader(SDFile *pDFile, SDFInfo *pInfo); int tsdbParseDFilename(const char *fname, int *vid, int *fid, TSDB_FILE_T *ftype, uint32_t *version); +// SDFileSet +void tsdbInitDFileSet(STsdb *pRepo, SDFileSet *pSet, SDiskID did, int fid, uint32_t ver); +void tsdbInitDFileSetEx(SDFileSet *pSet, SDFileSet *pOSet); +int tsdbEncodeDFileSet(void **buf, SDFileSet *pSet); +void *tsdbDecodeDFileSet(STsdb *pRepo, void *buf, SDFileSet *pSet); +int tsdbEncodeDFileSetEx(void **buf, SDFileSet *pSet); +void *tsdbDecodeDFileSetEx(void *buf, SDFileSet *pSet); +int tsdbApplyDFileSetChange(SDFileSet *from, SDFileSet *to); +int tsdbCreateDFileSet(STsdb *pRepo, SDFileSet *pSet, bool updateHeader); +int tsdbUpdateDFileSetHeader(SDFileSet *pSet); +int tsdbScanAndTryFixDFileSet(STsdb *pRepo, SDFileSet *pSet); +void tsdbCloseDFileSet(SDFileSet *pSet); +int tsdbOpenDFileSet(SDFileSet *pSet, int flags); +void tsdbRemoveDFileSet(SDFileSet *pSet); +int tsdbCopyDFileSet(SDFileSet *pSrc, SDFileSet *pDest); +void tsdbGetFidKeyRange(int days, int8_t precision, int fid, TSKEY *minKey, TSKEY *maxKey); + +// tsdbFS.c ============================================================================================== +typedef struct STsdbFS STsdbFS; + // tsdbMemTable ================ typedef struct STbData STbData; typedef struct STsdbMemTable STsdbMemTable; @@ -93,11 +114,6 @@ void tsdbMemTableDestroy(STsdbMemTable *pMemTable); int tsdbLoadDataFromCache(STsdb *pTsdb, STable *pTable, SSkipListIterator *pIter, TSKEY maxKey, int maxRowsToRead, SDataCols *pCols, TKEY *filterKeys, int nFilterKeys, bool keepDup, SMergeInfo *pMergeInfo); -// tsdbCommit ================ - -// tsdbFS ================ -typedef struct STsdbFS STsdbFS; - // structs typedef struct { int minFid; @@ -313,8 +329,6 @@ typedef struct { uint64_t aggrOffset : 63; TSDBKEY minKey; TSDBKEY maxKey; - // TSKEY keyFirst; - // TSKEY keyLast; } SBlock; static FORCE_INLINE bool tsdbIsSupBlock(SBlock *pBlock) { return pBlock->numOfSubBlocks == 1; } @@ -345,26 +359,6 @@ typedef struct { int64_t min; } SAggrBlkCol; -// Code here just for back-ward compatibility -static FORCE_INLINE void tsdbSetBlockColOffset(SBlockCol *pBlockCol, uint32_t offset) { -#ifdef TD_REFACTOR_3 - pBlockCol->offset = offset & ((((uint32_t)1) << 24) - 1); - pBlockCol->offsetH = (uint8_t)(offset >> 24); -#else - pBlockCol->offset = offset; -#endif -} - -static FORCE_INLINE uint32_t tsdbGetBlockColOffset(SBlockCol *pBlockCol) { -#ifdef TD_REFACTOR_3 - uint32_t offset1 = pBlockCol->offset; - uint32_t offset2 = pBlockCol->offsetH; - return (offset1 | (offset2 << 24)); -#else - return pBlockCol->offset; -#endif -} - typedef struct { int32_t delimiter; // For recovery usage int32_t numOfCols; // For recovery usage @@ -613,55 +607,6 @@ typedef struct { } \ } while (0); -void tsdbInitDFileSet(STsdb *pRepo, SDFileSet *pSet, SDiskID did, int fid, uint32_t ver); -void tsdbInitDFileSetEx(SDFileSet *pSet, SDFileSet *pOSet); -int tsdbEncodeDFileSet(void **buf, SDFileSet *pSet); -void *tsdbDecodeDFileSet(STsdb *pRepo, void *buf, SDFileSet *pSet); -int tsdbEncodeDFileSetEx(void **buf, SDFileSet *pSet); -void *tsdbDecodeDFileSetEx(void *buf, SDFileSet *pSet); -int tsdbApplyDFileSetChange(SDFileSet *from, SDFileSet *to); -int tsdbCreateDFileSet(STsdb *pRepo, SDFileSet *pSet, bool updateHeader); -int tsdbUpdateDFileSetHeader(SDFileSet *pSet); -int tsdbScanAndTryFixDFileSet(STsdb *pRepo, SDFileSet *pSet); - -static FORCE_INLINE void tsdbCloseDFileSet(SDFileSet *pSet) { - for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) { - tsdbCloseDFile(TSDB_DFILE_IN_SET(pSet, ftype)); - } -} - -static FORCE_INLINE int tsdbOpenDFileSet(SDFileSet *pSet, int flags) { - for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) { - if (tsdbOpenDFile(TSDB_DFILE_IN_SET(pSet, ftype), flags) < 0) { - tsdbCloseDFileSet(pSet); - return -1; - } - } - return 0; -} - -static FORCE_INLINE void tsdbRemoveDFileSet(SDFileSet *pSet) { - for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) { - (void)tsdbRemoveDFile(TSDB_DFILE_IN_SET(pSet, ftype)); - } -} - -static FORCE_INLINE int tsdbCopyDFileSet(SDFileSet *pSrc, SDFileSet *pDest) { - for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) { - if (tsdbCopyDFile(TSDB_DFILE_IN_SET(pSrc, ftype), TSDB_DFILE_IN_SET(pDest, ftype)) < 0) { - tsdbRemoveDFileSet(pDest); - return -1; - } - } - - return 0; -} - -static FORCE_INLINE void tsdbGetFidKeyRange(int days, int8_t precision, int fid, TSKEY *minKey, TSKEY *maxKey) { - *minKey = fid * days * tsTickPerMin[precision]; - *maxKey = *minKey + days * tsTickPerMin[precision] - 1; -} - static FORCE_INLINE bool tsdbFSetIsOk(SDFileSet *pSet) { for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) { if (TSDB_FILE_IS_BAD(TSDB_DFILE_IN_SET(pSet, ftype))) { diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit.c b/source/dnode/vnode/src/tsdb/tsdbCommit.c index 64e2b917ae..10b890262e 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit.c @@ -1175,7 +1175,7 @@ static int tsdbWriteBlockImpl(STsdb *pRepo, STable *pTable, SDFile *pDFile, SDFi tsdbUpdateDFileMagic(pDFile, POINTER_SHIFT(tptr, flen - sizeof(TSCKSUM))); if (ncol != 0) { - tsdbSetBlockColOffset(pBlockCol, toffset); + pBlockCol->offset = toffset; pBlockCol->len = flen; // data + bitmaps pBlockCol->blen = tBitmapsLen; ++tcol; diff --git a/source/dnode/vnode/src/tsdb/tsdbFile.c b/source/dnode/vnode/src/tsdb/tsdbFile.c index 1e9c256d9c..a88cb8a353 100644 --- a/source/dnode/vnode/src/tsdb/tsdbFile.c +++ b/source/dnode/vnode/src/tsdb/tsdbFile.c @@ -542,4 +542,42 @@ int tsdbCopyDFile(SDFile *pSrc, SDFile *pDest) { pDest->info = pSrc->info; return 0; +} + +void tsdbCloseDFileSet(SDFileSet *pSet) { + for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) { + tsdbCloseDFile(TSDB_DFILE_IN_SET(pSet, ftype)); + } +} + +int tsdbOpenDFileSet(SDFileSet *pSet, int flags) { + for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) { + if (tsdbOpenDFile(TSDB_DFILE_IN_SET(pSet, ftype), flags) < 0) { + tsdbCloseDFileSet(pSet); + return -1; + } + } + return 0; +} + +void tsdbRemoveDFileSet(SDFileSet *pSet) { + for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) { + (void)tsdbRemoveDFile(TSDB_DFILE_IN_SET(pSet, ftype)); + } +} + +int tsdbCopyDFileSet(SDFileSet *pSrc, SDFileSet *pDest) { + for (TSDB_FILE_T ftype = 0; ftype < TSDB_FILE_MAX; ftype++) { + if (tsdbCopyDFile(TSDB_DFILE_IN_SET(pSrc, ftype), TSDB_DFILE_IN_SET(pDest, ftype)) < 0) { + tsdbRemoveDFileSet(pDest); + return -1; + } + } + + return 0; +} + +void tsdbGetFidKeyRange(int days, int8_t precision, int fid, TSKEY *minKey, TSKEY *maxKey) { + *minKey = fid * days * tsTickPerMin[precision]; + *maxKey = *minKey + days * tsTickPerMin[precision] - 1; } \ No newline at end of file diff --git a/source/dnode/vnode/src/tsdb/tsdbReadImpl.c b/source/dnode/vnode/src/tsdb/tsdbReadImpl.c index e333f0566b..a26436156d 100644 --- a/source/dnode/vnode/src/tsdb/tsdbReadImpl.c +++ b/source/dnode/vnode/src/tsdb/tsdbReadImpl.c @@ -726,7 +726,7 @@ static int tsdbLoadBlockDataImpl(SReadH *pReadh, SBlock *pBlock, SDataCols *pDat if (dcol != 0) { pBlockCol = &(pBlockData->cols[ccol]); tcolId = pBlockCol->colId; - toffset = tsdbGetBlockColOffset(pBlockCol); + toffset = pBlockCol->offset; tlen = pBlockCol->len; pDataCol->bitmap = pBlockCol->blen > 0 ? 1 : 0; } else { @@ -942,8 +942,8 @@ static int tsdbLoadColData(SReadH *pReadh, SDFile *pDFile, SBlock *pBlock, SBloc if (tsdbMakeRoom((void **)(&TSDB_READ_BUF(pReadh)), pBlockCol->len) < 0) return -1; if (tsdbMakeRoom((void **)(&TSDB_READ_COMP_BUF(pReadh)), tsize) < 0) return -1; - int64_t offset = pBlock->offset + tsdbBlockStatisSize(pBlock->numOfCols, (uint32_t)pBlock->blkVer) + - tsdbGetBlockColOffset(pBlockCol); + int64_t offset = + pBlock->offset + tsdbBlockStatisSize(pBlock->numOfCols, (uint32_t)pBlock->blkVer) + pBlockCol->offset; if (tsdbSeekDFile(pDFile, offset, SEEK_SET) < 0) { tsdbError("vgId:%d, failed to load block column data while seek file %s to offset %" PRId64 " since %s", TSDB_READ_REPO_ID(pReadh), TSDB_FILE_FULL_NAME(pDFile), offset, tstrerror(terrno)); From bbc2ba9fefa91b6a437a2f8e841710c66e9e230a Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 5 Jun 2022 09:29:18 +0000 Subject: [PATCH 22/24] refact TSDB --- source/dnode/vnode/src/inc/tsdb.h | 90 +++++++++------------------- source/dnode/vnode/src/tsdb/tsdbFS.c | 34 ++++++++--- 2 files changed, 54 insertions(+), 70 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index d2429391ab..ca80fe394a 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -101,7 +101,28 @@ int tsdbCopyDFileSet(SDFileSet *pSrc, SDFileSet *pDest); void tsdbGetFidKeyRange(int days, int8_t precision, int fid, TSKEY *minKey, TSKEY *maxKey); // tsdbFS.c ============================================================================================== -typedef struct STsdbFS STsdbFS; +typedef struct STsdbFS STsdbFS; +typedef struct SFSIter SFSIter; +typedef struct STsdbFSMeta STsdbFSMeta; + +STsdbFS *tsdbNewFS(const STsdbKeepCfg *pCfg); +void *tsdbFreeFS(STsdbFS *pfs); +int tsdbOpenFS(STsdb *pRepo); +void tsdbCloseFS(STsdb *pRepo); +void tsdbStartFSTxn(STsdb *pRepo, int64_t pointsAdd, int64_t storageAdd); +int tsdbEndFSTxn(STsdb *pRepo); +int tsdbEndFSTxnWithError(STsdbFS *pfs); +void tsdbUpdateFSTxnMeta(STsdbFS *pfs, STsdbFSMeta *pMeta); +// void tsdbUpdateMFile(STsdbFS *pfs, const SMFile *pMFile); +int tsdbUpdateDFileSet(STsdbFS *pfs, const SDFileSet *pSet); + +void tsdbFSIterInit(SFSIter *pIter, STsdbFS *pfs, int direction); +void tsdbFSIterSeek(SFSIter *pIter, int fid); +SDFileSet *tsdbFSIterNext(SFSIter *pIter); +int tsdbLoadMetaCache(STsdb *pRepo, bool recoverMeta); +int tsdbRLockFS(STsdbFS *pFs); +int tsdbWLockFS(STsdbFS *pFs); +int tsdbUnLockFS(STsdbFS *pFs); // tsdbMemTable ================ typedef struct STbData STbData; @@ -208,11 +229,11 @@ struct STsdbMemTable { SHashObj *pHashIdx; }; -typedef struct { +struct STsdbFSMeta { uint32_t version; // Commit version from 0 to increase int64_t totalPoints; // total points int64_t totalStorage; // Uncompressed total storage -} STsdbFSMeta; +}; // ================== typedef struct { @@ -573,21 +594,7 @@ static FORCE_INLINE uint32_t tsdbGetDFSVersion(TSDB_FILE_T fType) { // latest v // =============== SDFileSet -typedef struct { - int fid; - int8_t state; - uint8_t ver; - uint16_t reserve; -#if 0 - SDFInfo info; -#endif - STfsFile f; - TdFilePtr pFile; - -} SSFile; // files split by days with fid - -#define TSDB_LATEST_FSET_VER 0 - +#define TSDB_LATEST_FSET_VER 0 #define TSDB_FSET_FID(s) ((s)->fid) #define TSDB_FSET_STATE(s) ((s)->state) #define TSDB_FSET_VER(s) ((s)->ver) @@ -634,61 +641,18 @@ typedef struct { #define FS_VERSION(pfs) ((pfs)->cstatus->meta.version) #define FS_TXN_VERSION(pfs) ((pfs)->nstatus->meta.version) -typedef struct { +struct SFSIter { int direction; uint64_t version; // current FS version STsdbFS *pfs; int index; // used to position next fset when version the same int fid; // used to seek when version is changed SDFileSet *pSet; -} SFSIter; +}; #define TSDB_FS_ITER_FORWARD TSDB_ORDER_ASC #define TSDB_FS_ITER_BACKWARD TSDB_ORDER_DESC -STsdbFS *tsdbNewFS(const STsdbKeepCfg *pCfg); -void *tsdbFreeFS(STsdbFS *pfs); -int tsdbOpenFS(STsdb *pRepo); -void tsdbCloseFS(STsdb *pRepo); -void tsdbStartFSTxn(STsdb *pRepo, int64_t pointsAdd, int64_t storageAdd); -int tsdbEndFSTxn(STsdb *pRepo); -int tsdbEndFSTxnWithError(STsdbFS *pfs); -void tsdbUpdateFSTxnMeta(STsdbFS *pfs, STsdbFSMeta *pMeta); -// void tsdbUpdateMFile(STsdbFS *pfs, const SMFile *pMFile); -int tsdbUpdateDFileSet(STsdbFS *pfs, const SDFileSet *pSet); - -void tsdbFSIterInit(SFSIter *pIter, STsdbFS *pfs, int direction); -void tsdbFSIterSeek(SFSIter *pIter, int fid); -SDFileSet *tsdbFSIterNext(SFSIter *pIter); -int tsdbLoadMetaCache(STsdb *pRepo, bool recoverMeta); - -static FORCE_INLINE int tsdbRLockFS(STsdbFS *pFs) { - int code = taosThreadRwlockRdlock(&(pFs->lock)); - if (code != 0) { - terrno = TAOS_SYSTEM_ERROR(code); - return -1; - } - return 0; -} - -static FORCE_INLINE int tsdbWLockFS(STsdbFS *pFs) { - int code = taosThreadRwlockWrlock(&(pFs->lock)); - if (code != 0) { - terrno = TAOS_SYSTEM_ERROR(code); - return -1; - } - return 0; -} - -static FORCE_INLINE int tsdbUnLockFS(STsdbFS *pFs) { - int code = taosThreadRwlockUnlock(&(pFs->lock)); - if (code != 0) { - terrno = TAOS_SYSTEM_ERROR(code); - return -1; - } - return 0; -} - struct TSDBROW { int64_t version; STSRow2 tsRow; diff --git a/source/dnode/vnode/src/tsdb/tsdbFS.c b/source/dnode/vnode/src/tsdb/tsdbFS.c index 19a79ac5c4..f1941a3bad 100644 --- a/source/dnode/vnode/src/tsdb/tsdbFS.c +++ b/source/dnode/vnode/src/tsdb/tsdbFS.c @@ -952,13 +952,6 @@ static int tsdbRestoreDFileSet(STsdb *pRepo) { } static int tsdbRestoreCurrent(STsdb *pRepo) { - // // Loop to recover mfile - // if (tsdbRestoreMeta(pRepo) < 0) { - // tsdbError("vgId:%d, failed to restore current since %s", REPO_ID(pRepo), tstrerror(terrno)); - // return -1; - // } - - // Loop to recover dfile set if (tsdbRestoreDFileSet(pRepo) < 0) { tsdbError("vgId:%d, failed to restore DFileSet since %s", REPO_ID(pRepo), tstrerror(terrno)); return -1; @@ -1041,3 +1034,30 @@ static void tsdbScanAndTryFixDFilesHeader(STsdb *pRepo, int32_t *nExpired) { tsdbCloseDFileSet(&fset); } } + +int tsdbRLockFS(STsdbFS *pFs) { + int code = taosThreadRwlockRdlock(&(pFs->lock)); + if (code != 0) { + terrno = TAOS_SYSTEM_ERROR(code); + return -1; + } + return 0; +} + +int tsdbWLockFS(STsdbFS *pFs) { + int code = taosThreadRwlockWrlock(&(pFs->lock)); + if (code != 0) { + terrno = TAOS_SYSTEM_ERROR(code); + return -1; + } + return 0; +} + +int tsdbUnLockFS(STsdbFS *pFs) { + int code = taosThreadRwlockUnlock(&(pFs->lock)); + if (code != 0) { + terrno = TAOS_SYSTEM_ERROR(code); + return -1; + } + return 0; +} \ No newline at end of file From e812d20a1bb24ec70bf285a93a0d19d77265988b Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 5 Jun 2022 10:59:19 +0000 Subject: [PATCH 23/24] refact --- source/dnode/vnode/src/inc/tsdb.h | 3 --- 1 file changed, 3 deletions(-) diff --git a/source/dnode/vnode/src/inc/tsdb.h b/source/dnode/vnode/src/inc/tsdb.h index ca80fe394a..171d52164c 100644 --- a/source/dnode/vnode/src/inc/tsdb.h +++ b/source/dnode/vnode/src/inc/tsdb.h @@ -352,9 +352,6 @@ typedef struct { TSDBKEY maxKey; } SBlock; -static FORCE_INLINE bool tsdbIsSupBlock(SBlock *pBlock) { return pBlock->numOfSubBlocks == 1; } -static FORCE_INLINE bool tsdbIsSubBlock(SBlock *pBlock) { return pBlock->numOfSubBlocks == 0; } - typedef struct { int32_t delimiter; // For recovery usage int32_t tid; From f02e10cf9941cfade47afcf762af8c82b20668a4 Mon Sep 17 00:00:00 2001 From: Hongze Cheng Date: Sun, 5 Jun 2022 11:30:30 +0000 Subject: [PATCH 24/24] fix ci test --- source/dnode/vnode/src/tsdb/tsdbCommit.c | 2 -- 1 file changed, 2 deletions(-) diff --git a/source/dnode/vnode/src/tsdb/tsdbCommit.c b/source/dnode/vnode/src/tsdb/tsdbCommit.c index 10b890262e..59a795b77a 100644 --- a/source/dnode/vnode/src/tsdb/tsdbCommit.c +++ b/source/dnode/vnode/src/tsdb/tsdbCommit.c @@ -117,7 +117,6 @@ int32_t tsdbCommit(STsdb *pTsdb) { return -1; } -#if 0 // Skip expired memory data and expired FSET tsdbSeekCommitIter(&commith, commith.rtn.minKey); while ((pSet = tsdbFSIterNext(&(commith.fsIter)))) { @@ -128,7 +127,6 @@ int32_t tsdbCommit(STsdb *pTsdb) { break; } } -#endif // commit fid = tsdbNextCommitFid(&(commith));