TDengine/source/dnode/vnode/src/tsdb/tsdbCommit.c

1195 lines
33 KiB
C
Raw Normal View History

2021-12-15 06:48:47 +00:00
/*
* Copyright (c) 2019 TAOS Data, Inc. <jhtao@taosdata.com>
*
* This program is free software: you can use, redistribute, and/or modify
* it under the terms of the GNU Affero General Public License, version 3
* or later ("AGPL"), as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
2022-04-26 11:04:26 +00:00
#include "tsdb.h"
2022-07-02 10:04:51 +00:00
typedef struct {
int64_t suid;
int64_t uid;
STSchema *pTSchema;
} SSkmInfo;
2021-12-15 06:48:47 +00:00
2022-06-14 06:57:07 +00:00
typedef struct {
2022-06-14 13:19:46 +00:00
STsdb *pTsdb;
2022-06-10 09:49:01 +00:00
/* commit data */
2022-06-23 03:08:19 +00:00
int64_t commitID;
2022-06-10 06:48:21 +00:00
int32_t minutes;
int8_t precision;
2022-06-13 13:13:37 +00:00
int32_t minRow;
int32_t maxRow;
2022-06-24 07:42:17 +00:00
int8_t cmprAlg;
2022-07-21 11:42:42 +00:00
STsdbFS fs;
2022-06-14 13:19:46 +00:00
// --------------
2022-06-20 06:41:17 +00:00
TSKEY nextKey; // reset by each table commit
2022-06-14 13:19:46 +00:00
int32_t commitFid;
TSKEY minKey;
TSKEY maxKey;
2022-06-10 06:48:21 +00:00
// commit file data
2022-07-30 12:08:04 +00:00
struct {
SDataFReader *pReader;
SArray *aBlockIdx; // SArray<SBlockIdx>
SMapData mBlock; // SMapData<SBlock>, read from reader
SBlockData bData;
} dReader;
struct {
SDataFWriter *pWriter;
SArray *aBlockIdx; // SArray<SBlockIdx>
SMapData mBlock; // SMapData<SBlock>
SBlockData bData;
} dWriter;
SSkmInfo skmTable;
SSkmInfo skmRow;
2022-06-10 09:49:01 +00:00
/* commit del */
2022-06-10 11:50:06 +00:00
SDelFReader *pDelFReader;
SDelFWriter *pDelFWriter;
2022-06-28 07:20:46 +00:00
SArray *aDelIdx; // SArray<SDelIdx>
SArray *aDelIdxN; // SArray<SDelIdx>
SArray *aDelData; // SArray<SDelData>
2022-06-14 06:57:07 +00:00
} SCommitter;
2022-01-08 04:39:12 +00:00
2022-06-10 06:48:21 +00:00
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter);
static int32_t tsdbCommitData(SCommitter *pCommitter);
static int32_t tsdbCommitDel(SCommitter *pCommitter);
static int32_t tsdbCommitCache(SCommitter *pCommitter);
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno);
2022-01-08 04:39:12 +00:00
2022-06-07 08:49:25 +00:00
int32_t tsdbBegin(STsdb *pTsdb) {
2022-06-10 06:48:21 +00:00
int32_t code = 0;
2022-06-01 10:34:17 +00:00
2022-06-25 12:46:16 +00:00
if (!pTsdb) return code;
2022-07-19 06:19:01 +00:00
SMemTable *pMemTable;
code = tsdbMemTableCreate(pTsdb, &pMemTable);
2022-06-20 06:41:17 +00:00
if (code) goto _err;
2022-06-01 10:34:17 +00:00
2022-07-19 06:19:01 +00:00
// lock
code = taosThreadRwlockWrlock(&pTsdb->rwLock);
if (code) {
code = TAOS_SYSTEM_ERROR(code);
goto _err;
}
pTsdb->mem = pMemTable;
// unlock
code = taosThreadRwlockUnlock(&pTsdb->rwLock);
if (code) {
code = TAOS_SYSTEM_ERROR(code);
goto _err;
}
2022-06-10 06:48:21 +00:00
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, tsdb begin failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
2022-06-10 06:48:21 +00:00
return code;
2022-06-01 10:34:17 +00:00
}
2022-06-04 07:38:27 +00:00
int32_t tsdbCommit(STsdb *pTsdb) {
if (!pTsdb) return 0;
2022-06-14 08:45:11 +00:00
2022-06-04 07:38:27 +00:00
int32_t code = 0;
2022-06-14 06:57:07 +00:00
SCommitter commith;
SMemTable *pMemTable = pTsdb->mem;
// check
2022-06-20 06:41:17 +00:00
if (pMemTable->nRow == 0 && pMemTable->nDel == 0) {
2022-07-19 06:19:01 +00:00
taosThreadRwlockWrlock(&pTsdb->rwLock);
2022-06-14 06:57:07 +00:00
pTsdb->mem = NULL;
2022-07-19 06:19:01 +00:00
taosThreadRwlockUnlock(&pTsdb->rwLock);
tsdbUnrefMemTable(pMemTable);
2022-06-14 06:57:07 +00:00
goto _exit;
}
2022-01-08 04:39:12 +00:00
2022-06-04 07:38:27 +00:00
// start commit
2022-06-07 11:08:41 +00:00
code = tsdbStartCommit(pTsdb, &commith);
2022-06-20 06:41:17 +00:00
if (code) goto _err;
2022-01-08 04:39:12 +00:00
2022-06-07 08:49:25 +00:00
// commit impl
code = tsdbCommitData(&commith);
2022-06-20 06:41:17 +00:00
if (code) goto _err;
2022-06-07 08:49:25 +00:00
code = tsdbCommitDel(&commith);
2022-06-20 06:41:17 +00:00
if (code) goto _err;
2022-06-07 08:49:25 +00:00
// end commit
2022-06-07 11:08:41 +00:00
code = tsdbEndCommit(&commith, 0);
2022-06-20 06:41:17 +00:00
if (code) goto _err;
2022-06-07 08:49:25 +00:00
2022-06-14 06:57:07 +00:00
_exit:
2022-06-07 08:49:25 +00:00
return code;
_err:
2022-06-14 06:57:07 +00:00
tsdbEndCommit(&commith, code);
2022-06-09 12:02:55 +00:00
tsdbError("vgId:%d, failed to commit since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
2022-06-07 08:49:25 +00:00
return code;
}
2022-06-10 06:48:21 +00:00
static int32_t tsdbCommitDelStart(SCommitter *pCommitter) {
2022-06-10 11:50:06 +00:00
int32_t code = 0;
STsdb *pTsdb = pCommitter->pTsdb;
SMemTable *pMemTable = pTsdb->imem;
2022-06-28 07:20:46 +00:00
pCommitter->aDelIdx = taosArrayInit(0, sizeof(SDelIdx));
if (pCommitter->aDelIdx == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
pCommitter->aDelData = taosArrayInit(0, sizeof(SDelData));
if (pCommitter->aDelData == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
pCommitter->aDelIdxN = taosArrayInit(0, sizeof(SDelIdx));
if (pCommitter->aDelIdxN == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
2022-06-14 08:45:11 +00:00
2022-07-21 11:42:42 +00:00
SDelFile *pDelFileR = pCommitter->fs.pDelFile;
2022-06-10 11:50:06 +00:00
if (pDelFileR) {
2022-06-11 03:59:06 +00:00
code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb, NULL);
2022-06-14 09:55:04 +00:00
if (code) goto _err;
2022-06-10 11:50:06 +00:00
2022-06-28 07:20:46 +00:00
code = tsdbReadDelIdx(pCommitter->pDelFReader, pCommitter->aDelIdx, NULL);
2022-06-14 09:55:04 +00:00
if (code) goto _err;
2022-06-10 11:50:06 +00:00
}
2022-06-11 06:36:22 +00:00
// prepare new
2022-06-28 07:20:46 +00:00
SDelFile wDelFile = {.commitID = pCommitter->commitID, .size = 0, .offset = 0};
code = tsdbDelFWriterOpen(&pCommitter->pDelFWriter, &wDelFile, pTsdb);
2022-06-14 09:55:04 +00:00
if (code) goto _err;
2022-06-10 11:50:06 +00:00
_exit:
2022-08-01 09:23:52 +00:00
tsdbDebug("vgId:%d, commit del start", TD_VID(pTsdb->pVnode));
2022-06-10 11:50:06 +00:00
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, commit del start failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
2022-06-10 06:48:21 +00:00
return code;
}
2022-06-14 11:46:46 +00:00
static int32_t tsdbCommitTableDel(SCommitter *pCommitter, STbData *pTbData, SDelIdx *pDelIdx) {
2022-06-18 02:27:09 +00:00
int32_t code = 0;
2022-06-28 07:20:46 +00:00
SDelData *pDelData;
2022-06-18 02:27:09 +00:00
tb_uid_t suid;
tb_uid_t uid;
2022-06-14 11:46:46 +00:00
if (pTbData) {
2022-06-28 07:20:46 +00:00
suid = pTbData->suid;
uid = pTbData->uid;
2022-06-14 11:46:46 +00:00
2022-06-28 07:20:46 +00:00
if (pTbData->pHead == NULL) {
pTbData = NULL;
}
}
2022-06-14 11:46:46 +00:00
if (pDelIdx) {
2022-06-28 07:20:46 +00:00
suid = pDelIdx->suid;
uid = pDelIdx->uid;
code = tsdbReadDelData(pCommitter->pDelFReader, pDelIdx, pCommitter->aDelData, NULL);
2022-06-14 11:46:46 +00:00
if (code) goto _err;
2022-07-07 06:41:05 +00:00
} else {
taosArrayClear(pCommitter->aDelData);
2022-06-14 11:46:46 +00:00
}
2022-06-28 07:20:46 +00:00
if (pTbData == NULL && pDelIdx == NULL) goto _exit;
2022-06-14 11:46:46 +00:00
2022-06-28 07:20:46 +00:00
SDelIdx delIdx = {.suid = suid, .uid = uid};
2022-06-14 11:46:46 +00:00
// memory
2022-06-18 02:27:09 +00:00
pDelData = pTbData ? pTbData->pHead : NULL;
for (; pDelData; pDelData = pDelData->pNext) {
2022-06-28 07:20:46 +00:00
if (taosArrayPush(pCommitter->aDelData, pDelData) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
2022-06-14 11:46:46 +00:00
}
// write
2022-06-28 07:20:46 +00:00
code = tsdbWriteDelData(pCommitter->pDelFWriter, pCommitter->aDelData, NULL, &delIdx);
2022-06-14 11:46:46 +00:00
if (code) goto _err;
// put delIdx
2022-07-07 06:41:05 +00:00
if (taosArrayPush(pCommitter->aDelIdxN, &delIdx) == NULL) {
2022-06-28 07:20:46 +00:00
code = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
2022-06-14 11:46:46 +00:00
_exit:
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, commit table del failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
2022-06-14 11:46:46 +00:00
return code;
}
2022-06-10 06:48:21 +00:00
static int32_t tsdbCommitDelEnd(SCommitter *pCommitter) {
int32_t code = 0;
2022-06-28 07:20:46 +00:00
STsdb *pTsdb = pCommitter->pTsdb;
2022-06-10 09:49:01 +00:00
2022-06-28 07:20:46 +00:00
code = tsdbWriteDelIdx(pCommitter->pDelFWriter, pCommitter->aDelIdxN, NULL);
2022-06-14 09:55:04 +00:00
if (code) goto _err;
2022-06-10 12:54:02 +00:00
2022-06-28 07:20:46 +00:00
code = tsdbUpdateDelFileHdr(pCommitter->pDelFWriter);
if (code) goto _err;
2022-07-21 11:42:42 +00:00
code = tsdbFSUpsertDelFile(&pCommitter->fs, &pCommitter->pDelFWriter->fDel);
2022-06-14 09:55:04 +00:00
if (code) goto _err;
2022-06-11 06:36:22 +00:00
2022-07-05 03:20:25 +00:00
code = tsdbDelFWriterClose(&pCommitter->pDelFWriter, 1);
2022-06-14 09:55:04 +00:00
if (code) goto _err;
2022-06-10 12:54:02 +00:00
if (pCommitter->pDelFReader) {
2022-07-05 03:20:25 +00:00
code = tsdbDelFReaderClose(&pCommitter->pDelFReader);
2022-06-10 12:54:02 +00:00
if (code) goto _err;
}
2022-06-28 07:20:46 +00:00
taosArrayDestroy(pCommitter->aDelIdx);
taosArrayDestroy(pCommitter->aDelData);
taosArrayDestroy(pCommitter->aDelIdxN);
2022-06-10 09:49:01 +00:00
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, commit del end failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
2022-06-10 06:48:21 +00:00
return code;
}
2022-06-23 13:23:43 +00:00
static int32_t tsdbCommitFileDataStart(SCommitter *pCommitter) {
int32_t code = 0;
STsdb *pTsdb = pCommitter->pTsdb;
SDFileSet *pRSet = NULL;
2022-06-17 12:22:45 +00:00
2022-06-23 13:23:43 +00:00
// memory
pCommitter->nextKey = TSKEY_MAX;
2022-06-17 12:22:45 +00:00
2022-06-23 13:23:43 +00:00
// old
2022-07-30 12:08:04 +00:00
taosArrayClear(pCommitter->dReader.aBlockIdx);
tMapDataReset(&pCommitter->dReader.mBlock);
tBlockDataReset(&pCommitter->dReader.bData);
2022-07-21 11:42:42 +00:00
pRSet = (SDFileSet *)taosArraySearch(pCommitter->fs.aDFileSet, &(SDFileSet){.fid = pCommitter->commitFid},
tDFileSetCmprFn, TD_EQ);
2022-06-23 13:23:43 +00:00
if (pRSet) {
2022-07-30 12:08:04 +00:00
code = tsdbDataFReaderOpen(&pCommitter->dReader.pReader, pTsdb, pRSet);
2022-06-17 12:22:45 +00:00
if (code) goto _err;
2022-07-30 12:08:04 +00:00
code = tsdbReadBlockIdx(pCommitter->dReader.pReader, pCommitter->dReader.aBlockIdx, NULL);
2022-06-17 12:22:45 +00:00
if (code) goto _err;
2022-06-17 06:29:06 +00:00
}
2022-06-17 02:50:25 +00:00
2022-06-23 13:23:43 +00:00
// new
2022-07-21 06:27:32 +00:00
SHeadFile fHead;
SDataFile fData;
SLastFile fLast;
SSmaFile fSma;
SDFileSet wSet = {.pHeadF = &fHead, .pDataF = &fData, .pLastF = &fLast, .pSmaF = &fSma};
2022-07-30 12:08:04 +00:00
taosArrayClear(pCommitter->dWriter.aBlockIdx);
tMapDataReset(&pCommitter->dWriter.mBlock);
tBlockDataReset(&pCommitter->dWriter.bData);
2022-06-23 13:23:43 +00:00
if (pRSet) {
2022-07-21 06:27:32 +00:00
wSet.diskId = pRSet->diskId;
wSet.fid = pCommitter->commitFid;
fHead = (SHeadFile){.commitID = pCommitter->commitID, .offset = 0, .size = 0};
fData = *pRSet->pDataF;
fLast = (SLastFile){.commitID = pCommitter->commitID, .size = 0};
fSma = *pRSet->pSmaF;
2022-06-23 13:23:43 +00:00
} else {
2022-07-29 12:01:45 +00:00
SDiskID did = {0};
tfsAllocDisk(pTsdb->pVnode->pTfs, 0, &did);
2022-07-30 07:20:38 +00:00
tfsMkdirRecurAt(pTsdb->pVnode->pTfs, pTsdb->path, did);
2022-07-29 12:01:45 +00:00
wSet.diskId = did;
2022-07-21 06:27:32 +00:00
wSet.fid = pCommitter->commitFid;
fHead = (SHeadFile){.commitID = pCommitter->commitID, .offset = 0, .size = 0};
fData = (SDataFile){.commitID = pCommitter->commitID, .size = 0};
fLast = (SLastFile){.commitID = pCommitter->commitID, .size = 0};
fSma = (SSmaFile){.commitID = pCommitter->commitID, .size = 0};
2022-06-17 02:50:25 +00:00
}
2022-07-30 12:08:04 +00:00
code = tsdbDataFWriterOpen(&pCommitter->dWriter.pWriter, pTsdb, &wSet);
2022-06-23 13:23:43 +00:00
if (code) goto _err;
2022-06-17 02:50:25 +00:00
2022-06-23 13:23:43 +00:00
_exit:
2022-06-17 02:50:25 +00:00
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, commit file data start failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
2022-06-16 12:53:25 +00:00
return code;
2022-06-15 11:13:55 +00:00
}
2022-07-02 10:04:51 +00:00
static int32_t tsdbCommitterUpdateTableSchema(SCommitter *pCommitter, int64_t suid, int64_t uid, int32_t sver) {
int32_t code = 0;
if (pCommitter->skmTable.pTSchema) {
if (pCommitter->skmTable.suid == suid) {
if (suid == 0) {
if (pCommitter->skmTable.uid == uid && sver == pCommitter->skmTable.pTSchema->version) goto _exit;
} else {
if (sver == pCommitter->skmTable.pTSchema->version) goto _exit;
}
}
}
pCommitter->skmTable.suid = suid;
pCommitter->skmTable.uid = uid;
tTSchemaDestroy(pCommitter->skmTable.pTSchema);
2022-07-04 08:26:16 +00:00
code = metaGetTbTSchemaEx(pCommitter->pTsdb->pVnode->pMeta, suid, uid, sver, &pCommitter->skmTable.pTSchema);
if (code) goto _exit;
2022-07-02 10:04:51 +00:00
_exit:
return code;
}
static int32_t tsdbCommitterUpdateRowSchema(SCommitter *pCommitter, int64_t suid, int64_t uid, int32_t sver) {
2022-06-24 07:42:17 +00:00
int32_t code = 0;
2022-07-02 10:04:51 +00:00
if (pCommitter->skmRow.pTSchema) {
if (pCommitter->skmRow.suid == suid) {
2022-06-24 07:42:17 +00:00
if (suid == 0) {
2022-07-02 10:04:51 +00:00
if (pCommitter->skmRow.uid == uid && sver == pCommitter->skmRow.pTSchema->version) goto _exit;
2022-06-24 07:42:17 +00:00
} else {
2022-07-02 10:04:51 +00:00
if (sver == pCommitter->skmRow.pTSchema->version) goto _exit;
2022-06-24 07:42:17 +00:00
}
}
}
2022-07-02 10:04:51 +00:00
pCommitter->skmRow.suid = suid;
pCommitter->skmRow.uid = uid;
tTSchemaDestroy(pCommitter->skmRow.pTSchema);
2022-07-04 08:26:16 +00:00
code = metaGetTbTSchemaEx(pCommitter->pTsdb->pVnode->pMeta, suid, uid, sver, &pCommitter->skmRow.pTSchema);
if (code) {
goto _exit;
2022-06-24 07:42:17 +00:00
}
_exit:
return code;
}
2022-06-29 07:07:01 +00:00
static int32_t tsdbCommitBlockData(SCommitter *pCommitter, SBlockData *pBlockData, SBlock *pBlock, SBlockIdx *pBlockIdx,
int8_t toDataOnly) {
int32_t code = 0;
2022-06-30 08:01:48 +00:00
if (pBlock->nSubBlock == 0) {
if (!toDataOnly && pBlockData->nRow < pCommitter->minRow) {
pBlock->last = 1;
} else {
pBlock->last = 0;
}
2022-06-29 07:07:01 +00:00
}
2022-07-30 12:08:04 +00:00
code =
tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, NULL, NULL, pBlockIdx, pBlock, pCommitter->cmprAlg);
2022-06-29 07:07:01 +00:00
if (code) goto _err;
2022-07-30 12:08:04 +00:00
code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pBlock, tPutBlock);
2022-06-29 07:07:01 +00:00
if (code) goto _err;
return code;
_err:
return code;
}
2022-06-27 15:42:55 +00:00
static int32_t tsdbMergeTableData(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlockMerge, TSDBKEY toKey,
int8_t toDataOnly) {
2022-06-27 11:20:11 +00:00
int32_t code = 0;
2022-06-27 15:42:55 +00:00
SBlockIdx *pBlockIdx = &(SBlockIdx){.suid = pIter->pTbData->suid, .uid = pIter->pTbData->uid};
2022-07-30 12:08:04 +00:00
SBlockData *pBlockDataMerge = &pCommitter->dReader.bData;
SBlockData *pBlockData = &pCommitter->dWriter.bData;
2022-07-01 15:10:46 +00:00
SBlock block;
SBlock *pBlock = &block;
2022-06-27 11:20:11 +00:00
TSDBROW *pRow1;
2022-06-27 15:42:55 +00:00
TSDBROW row2;
TSDBROW *pRow2 = &row2;
2022-06-27 11:20:11 +00:00
// read SBlockData
2022-07-30 12:08:04 +00:00
code = tsdbReadBlockData(pCommitter->dReader.pReader, pBlockIdx, pBlockMerge, pBlockDataMerge, NULL, NULL);
2022-06-27 11:20:11 +00:00
if (code) goto _err;
2022-07-04 03:01:19 +00:00
code = tBlockDataSetSchema(pBlockData, pCommitter->skmTable.pTSchema);
if (code) goto _err;
2022-06-27 11:20:11 +00:00
// loop to merge
pRow1 = tsdbTbDataIterGet(pIter);
2022-06-27 15:42:55 +00:00
*pRow2 = tsdbRowFromBlockData(pBlockDataMerge, 0);
2022-06-28 02:35:37 +00:00
ASSERT(pRow1 && tsdbKeyCmprFn(&TSDBROW_KEY(pRow1), &toKey) < 0);
2022-06-27 11:20:11 +00:00
ASSERT(tsdbKeyCmprFn(&TSDBROW_KEY(pRow2), &toKey) < 0);
2022-07-02 10:04:51 +00:00
code = tsdbCommitterUpdateRowSchema(pCommitter, pBlockIdx->suid, pBlockIdx->uid, TSDBROW_SVERSION(pRow1));
2022-06-27 11:20:11 +00:00
if (code) goto _err;
2022-06-27 15:42:55 +00:00
tBlockReset(pBlock);
2022-07-04 03:01:19 +00:00
tBlockDataClearData(pBlockData);
2022-06-27 11:20:11 +00:00
while (true) {
if (pRow1 == NULL && pRow2 == NULL) {
2022-06-27 15:42:55 +00:00
if (pBlockData->nRow == 0) {
2022-06-27 11:20:11 +00:00
break;
} else {
goto _write_block;
}
}
if (pRow1 && pRow2) {
2022-06-29 03:34:55 +00:00
int32_t c = tsdbRowCmprFn(pRow1, pRow2);
if (c < 0) {
goto _append_mem_row;
} else if (c > 0) {
goto _append_block_row;
2022-06-27 11:20:11 +00:00
} else {
ASSERT(0);
}
} else if (pRow1) {
2022-06-29 03:34:55 +00:00
goto _append_mem_row;
2022-06-27 11:20:11 +00:00
} else {
2022-06-29 03:34:55 +00:00
goto _append_block_row;
}
_append_mem_row:
2022-07-02 10:04:51 +00:00
code = tBlockDataAppendRow(pBlockData, pRow1, pCommitter->skmRow.pTSchema);
2022-06-29 03:34:55 +00:00
if (code) goto _err;
2022-06-27 11:20:11 +00:00
2022-06-29 03:34:55 +00:00
tsdbTbDataIterNext(pIter);
pRow1 = tsdbTbDataIterGet(pIter);
if (pRow1) {
if (tsdbKeyCmprFn(&TSDBROW_KEY(pRow1), &toKey) < 0) {
2022-07-02 10:04:51 +00:00
code = tsdbCommitterUpdateRowSchema(pCommitter, pBlockIdx->suid, pBlockIdx->uid, TSDBROW_SVERSION(pRow1));
2022-06-29 03:34:55 +00:00
if (code) goto _err;
2022-06-27 15:42:55 +00:00
} else {
2022-06-29 03:34:55 +00:00
pRow1 = NULL;
2022-06-27 15:42:55 +00:00
}
2022-06-27 11:20:11 +00:00
}
2022-06-29 03:34:55 +00:00
if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
goto _write_block;
} else {
continue;
}
_append_block_row:
2022-06-29 03:43:33 +00:00
code = tBlockDataAppendRow(pBlockData, pRow2, NULL);
2022-06-27 15:42:55 +00:00
if (code) goto _err;
2022-06-29 03:34:55 +00:00
if (pRow2->iRow + 1 < pBlockDataMerge->nRow) {
*pRow2 = tsdbRowFromBlockData(pBlockDataMerge, pRow2->iRow + 1);
2022-06-27 11:20:11 +00:00
} else {
2022-06-29 03:34:55 +00:00
pRow2 = NULL;
2022-06-27 11:20:11 +00:00
}
2022-06-29 03:34:55 +00:00
if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
goto _write_block;
} else {
continue;
}
2022-06-27 15:42:55 +00:00
2022-06-27 11:20:11 +00:00
_write_block:
2022-06-30 08:01:48 +00:00
code = tsdbCommitBlockData(pCommitter, pBlockData, pBlock, pBlockIdx, toDataOnly);
2022-06-27 15:42:55 +00:00
if (code) goto _err;
tBlockReset(pBlock);
2022-07-04 03:01:19 +00:00
tBlockDataClearData(pBlockData);
2022-06-27 11:20:11 +00:00
}
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, tsdb merge block and mem failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
2022-06-27 11:20:11 +00:00
return code;
}
2022-06-27 13:05:17 +00:00
static int32_t tsdbCommitTableMemData(SCommitter *pCommitter, STbDataIter *pIter, TSDBKEY toKey, int8_t toDataOnly) {
2022-06-27 11:20:11 +00:00
int32_t code = 0;
TSDBROW *pRow;
2022-07-01 15:10:46 +00:00
SBlock block;
SBlock *pBlock = &block;
2022-07-30 12:08:04 +00:00
SBlockData *pBlockData = &pCommitter->dWriter.bData;
2022-06-27 15:42:55 +00:00
int64_t suid = pIter->pTbData->suid;
int64_t uid = pIter->pTbData->uid;
2022-06-27 11:20:11 +00:00
2022-07-04 03:01:19 +00:00
code = tBlockDataSetSchema(pBlockData, pCommitter->skmTable.pTSchema);
if (code) goto _err;
2022-06-27 15:42:55 +00:00
tBlockReset(pBlock);
2022-07-04 03:01:19 +00:00
tBlockDataClearData(pBlockData);
2022-06-27 15:42:55 +00:00
pRow = tsdbTbDataIterGet(pIter);
2022-06-28 02:35:37 +00:00
ASSERT(pRow && tsdbKeyCmprFn(&TSDBROW_KEY(pRow), &toKey) < 0);
2022-06-27 11:20:11 +00:00
while (true) {
2022-06-28 02:35:37 +00:00
if (pRow == NULL) {
2022-06-27 11:20:11 +00:00
if (pBlockData->nRow > 0) {
goto _write_block;
} else {
break;
}
}
2022-06-27 15:42:55 +00:00
// update schema
2022-07-02 10:04:51 +00:00
code = tsdbCommitterUpdateRowSchema(pCommitter, suid, uid, TSDBROW_SVERSION(pRow));
2022-06-27 11:20:11 +00:00
if (code) goto _err;
2022-06-27 15:42:55 +00:00
// append
2022-07-02 10:04:51 +00:00
code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema);
2022-06-27 11:20:11 +00:00
if (code) goto _err;
tsdbTbDataIterNext(pIter);
pRow = tsdbTbDataIterGet(pIter);
// if (pRow && tsdbKeyCmprFn(&TSDBROW_KEY(pRow), &toKey) >= 0) pRow = NULL;
// crash on CI, use the block following
if (pRow) {
TSDBKEY tmpKey = TSDBROW_KEY(pRow);
if (tsdbKeyCmprFn(&tmpKey, &toKey) >= 0) {
pRow = NULL;
}
}
2022-06-27 11:20:11 +00:00
if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) goto _write_block;
2022-06-27 15:42:55 +00:00
continue;
2022-06-27 11:20:11 +00:00
_write_block:
2022-06-30 08:01:48 +00:00
code = tsdbCommitBlockData(pCommitter, pBlockData, pBlock, &(SBlockIdx){.suid = suid, .uid = uid}, toDataOnly);
2022-06-27 15:42:55 +00:00
if (code) goto _err;
tBlockReset(pBlock);
2022-07-04 03:01:19 +00:00
tBlockDataClearData(pBlockData);
2022-06-27 11:20:11 +00:00
}
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, tsdb commit table mem data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
2022-06-27 11:20:11 +00:00
return code;
}
2022-06-27 13:05:17 +00:00
static int32_t tsdbCommitTableDiskData(SCommitter *pCommitter, SBlock *pBlock, SBlockIdx *pBlockIdx) {
2022-06-27 11:20:11 +00:00
int32_t code = 0;
2022-07-01 15:10:46 +00:00
SBlock block;
2022-06-27 12:14:33 +00:00
if (pBlock->last) {
2022-07-30 12:08:04 +00:00
code = tsdbReadBlockData(pCommitter->dReader.pReader, pBlockIdx, pBlock, &pCommitter->dReader.bData, NULL, NULL);
2022-06-27 12:14:33 +00:00
if (code) goto _err;
2022-07-01 15:10:46 +00:00
tBlockReset(&block);
2022-07-30 12:08:04 +00:00
code = tsdbCommitBlockData(pCommitter, &pCommitter->dReader.bData, &block, pBlockIdx, 0);
2022-06-27 12:14:33 +00:00
if (code) goto _err;
} else {
2022-07-30 12:08:04 +00:00
code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pBlock, tPutBlock);
2022-06-27 12:14:33 +00:00
if (code) goto _err;
}
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, tsdb commit table disk data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
2022-06-27 11:20:11 +00:00
return code;
}
2022-06-27 13:05:17 +00:00
static int32_t tsdbCommitTableDataEnd(SCommitter *pCommitter, int64_t suid, int64_t uid) {
int32_t code = 0;
2022-06-30 06:02:47 +00:00
SBlockIdx blockIdx = {.suid = suid, .uid = uid};
SBlockIdx *pBlockIdx = &blockIdx;
2022-06-27 13:05:17 +00:00
2022-07-30 12:08:04 +00:00
code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dWriter.mBlock, NULL, pBlockIdx);
2022-06-27 13:05:17 +00:00
if (code) goto _err;
2022-07-30 12:08:04 +00:00
if (taosArrayPush(pCommitter->dWriter.aBlockIdx, pBlockIdx) == NULL) {
2022-06-30 06:02:47 +00:00
code = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
2022-06-27 13:05:17 +00:00
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, commit table data end failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
2022-06-27 13:05:17 +00:00
return code;
}
2022-06-28 03:22:56 +00:00
static int32_t tsdbGetOvlpNRow(STbDataIter *pIter, SBlock *pBlock) {
int32_t nRow = 0;
TSDBROW *pRow;
TSDBKEY key;
int32_t c = 0;
STbDataIter iter = *pIter;
iter.pRow = NULL;
while (true) {
2022-07-07 09:07:14 +00:00
pRow = tsdbTbDataIterGet(&iter);
2022-06-28 03:22:56 +00:00
if (pRow == NULL) break;
key = TSDBROW_KEY(pRow);
c = tBlockCmprFn(&(SBlock){.maxKey = key, .minKey = key}, pBlock);
if (c == 0) {
nRow++;
2022-07-07 09:07:14 +00:00
tsdbTbDataIterNext(&iter);
2022-06-28 03:22:56 +00:00
} else if (c > 0) {
break;
} else {
ASSERT(0);
}
}
return nRow;
}
static int32_t tsdbMergeAsSubBlock(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlock) {
int32_t code = 0;
2022-07-30 12:08:04 +00:00
SBlockData *pBlockData = &pCommitter->dWriter.bData;
2022-06-28 03:22:56 +00:00
SBlockIdx *pBlockIdx = &(SBlockIdx){.suid = pIter->pTbData->suid, .uid = pIter->pTbData->uid};
2022-07-01 15:10:46 +00:00
SBlock block;
2022-06-28 03:22:56 +00:00
TSDBROW *pRow;
2022-07-04 03:01:19 +00:00
code = tBlockDataSetSchema(pBlockData, pCommitter->skmTable.pTSchema);
if (code) goto _err;
2022-06-28 03:22:56 +00:00
pRow = tsdbTbDataIterGet(pIter);
2022-07-02 10:04:51 +00:00
code = tsdbCommitterUpdateRowSchema(pCommitter, pBlockIdx->suid, pBlockIdx->uid, TSDBROW_SVERSION(pRow));
2022-06-28 03:22:56 +00:00
if (code) goto _err;
while (true) {
2022-07-07 09:07:14 +00:00
if (pRow == NULL) break;
2022-07-02 10:04:51 +00:00
code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema);
2022-06-28 03:22:56 +00:00
if (code) goto _err;
tsdbTbDataIterNext(pIter);
pRow = tsdbTbDataIterGet(pIter);
if (pRow) {
2022-07-04 07:01:54 +00:00
TSDBKEY key = TSDBROW_KEY(pRow);
int32_t c = tBlockCmprFn(&(SBlock){.minKey = key, .maxKey = key}, pBlock);
2022-06-28 03:22:56 +00:00
if (c == 0) {
2022-07-02 10:04:51 +00:00
code =
tsdbCommitterUpdateRowSchema(pCommitter, pIter->pTbData->suid, pIter->pTbData->uid, TSDBROW_SVERSION(pRow));
2022-06-28 03:22:56 +00:00
if (code) goto _err;
} else if (c > 0) {
pRow = NULL;
} else {
ASSERT(0);
}
}
}
2022-07-01 15:10:46 +00:00
block = *pBlock;
code = tsdbCommitBlockData(pCommitter, pBlockData, &block, pBlockIdx, 0);
2022-06-28 03:22:56 +00:00
if (code) goto _err;
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, tsdb merge as subblock failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
2022-06-28 03:22:56 +00:00
return code;
}
2022-06-27 12:03:19 +00:00
static int32_t tsdbCommitTableData(SCommitter *pCommitter, STbData *pTbData, SBlockIdx *pBlockIdx) {
int32_t code = 0;
2022-06-30 06:02:47 +00:00
STbDataIter iter = {0};
STbDataIter *pIter = &iter;
2022-06-27 12:03:19 +00:00
TSDBROW *pRow;
2022-06-28 02:35:37 +00:00
int32_t iBlock;
2022-06-27 12:03:19 +00:00
int32_t nBlock;
2022-06-27 13:05:17 +00:00
int64_t suid;
int64_t uid;
2022-06-27 12:03:19 +00:00
if (pTbData) {
tsdbTbDataIterOpen(pTbData, &(TSDBKEY){.ts = pCommitter->minKey, .version = VERSION_MIN}, 0, pIter);
pRow = tsdbTbDataIterGet(pIter);
2022-06-28 02:35:37 +00:00
if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
2022-06-27 13:05:17 +00:00
suid = pTbData->suid;
uid = pTbData->uid;
2022-06-27 12:03:19 +00:00
} else {
pIter = NULL;
pRow = NULL;
}
if (pBlockIdx) {
2022-07-30 12:08:04 +00:00
code = tsdbReadBlock(pCommitter->dReader.pReader, pBlockIdx, &pCommitter->dReader.mBlock, NULL);
2022-06-27 12:03:19 +00:00
if (code) goto _err;
2022-07-30 12:08:04 +00:00
nBlock = pCommitter->dReader.mBlock.nItem;
2022-06-27 12:03:19 +00:00
ASSERT(nBlock > 0);
2022-06-28 02:35:37 +00:00
2022-06-27 13:05:17 +00:00
suid = pBlockIdx->suid;
uid = pBlockIdx->uid;
2022-06-27 12:03:19 +00:00
} else {
nBlock = 0;
}
2022-06-28 02:35:37 +00:00
if (pRow == NULL && nBlock == 0) goto _exit;
2022-06-27 12:03:19 +00:00
// start ===========
2022-07-30 12:08:04 +00:00
tMapDataReset(&pCommitter->dWriter.mBlock);
2022-07-01 15:10:46 +00:00
SBlock block;
SBlock *pBlock = &block;
2022-06-27 12:03:19 +00:00
2022-06-28 02:35:37 +00:00
iBlock = 0;
2022-06-27 13:05:17 +00:00
if (iBlock < nBlock) {
2022-07-30 12:08:04 +00:00
tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
2022-06-27 13:05:17 +00:00
} else {
pBlock = NULL;
}
2022-07-04 03:01:19 +00:00
if (pRow) {
code = tsdbCommitterUpdateTableSchema(pCommitter, pTbData->suid, pTbData->uid, pTbData->maxSkmVer);
if (code) goto _err;
}
2022-06-27 12:03:19 +00:00
// merge ===========
while (true) {
2022-06-28 02:35:37 +00:00
if (pRow == NULL && pBlock == NULL) break;
2022-06-27 12:03:19 +00:00
2022-06-28 02:35:37 +00:00
if (pRow && pBlock) {
2022-06-27 12:03:19 +00:00
if (pBlock->last) {
2022-06-27 15:42:55 +00:00
code = tsdbMergeTableData(pCommitter, pIter, pBlock,
2022-06-27 13:05:17 +00:00
(TSDBKEY){.ts = pCommitter->maxKey + 1, .version = VERSION_MIN}, 0);
2022-06-27 12:03:19 +00:00
if (code) goto _err;
pRow = tsdbTbDataIterGet(pIter);
2022-06-28 02:35:37 +00:00
if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
2022-06-27 12:03:19 +00:00
iBlock++;
2022-06-27 15:44:33 +00:00
if (iBlock < nBlock) {
2022-07-30 12:08:04 +00:00
tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
2022-06-27 15:44:33 +00:00
} else {
pBlock = NULL;
}
2022-06-28 02:35:37 +00:00
ASSERT(pRow == NULL && pBlock == NULL);
2022-06-27 12:03:19 +00:00
} else {
2022-06-27 15:42:55 +00:00
int32_t c = tBlockCmprFn(&(SBlock){.maxKey = TSDBROW_KEY(pRow), .minKey = TSDBROW_KEY(pRow)}, pBlock);
2022-06-27 12:03:19 +00:00
if (c > 0) {
2022-06-28 02:35:37 +00:00
// only disk data
2022-06-27 13:05:17 +00:00
code = tsdbCommitTableDiskData(pCommitter, pBlock, pBlockIdx);
2022-06-27 12:03:19 +00:00
if (code) goto _err;
2022-06-27 13:05:17 +00:00
iBlock++;
if (iBlock < nBlock) {
2022-07-30 12:08:04 +00:00
tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
2022-06-27 13:05:17 +00:00
} else {
pBlock = NULL;
}
2022-06-27 12:03:19 +00:00
} else if (c < 0) {
2022-06-28 02:35:37 +00:00
// only memory data
2022-06-27 13:05:17 +00:00
code = tsdbCommitTableMemData(pCommitter, pIter, pBlock->minKey, 1);
2022-06-27 12:03:19 +00:00
if (code) goto _err;
pRow = tsdbTbDataIterGet(pIter);
2022-06-28 02:35:37 +00:00
if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
2022-06-27 12:03:19 +00:00
} else {
2022-06-28 02:35:37 +00:00
// merge memory and disk
2022-06-28 03:22:56 +00:00
int32_t nOvlp = tsdbGetOvlpNRow(pIter, pBlock);
ASSERT(nOvlp);
2022-06-27 12:03:19 +00:00
if (pBlock->nRow + nOvlp <= pCommitter->maxRow && pBlock->nSubBlock < TSDB_MAX_SUBBLOCKS) {
2022-06-28 03:22:56 +00:00
code = tsdbMergeAsSubBlock(pCommitter, pIter, pBlock);
if (code) goto _err;
2022-06-27 12:03:19 +00:00
} else {
2022-06-28 03:22:56 +00:00
TSDBKEY toKey = {.ts = pCommitter->maxKey + 1, .version = VERSION_MIN};
int8_t toDataOnly = 0;
if (iBlock < nBlock - 1) {
toDataOnly = 1;
SBlock nextBlock = {0};
tBlockReset(&nextBlock);
2022-07-30 12:08:04 +00:00
tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock + 1, &nextBlock, tGetBlock);
2022-06-28 03:22:56 +00:00
toKey = nextBlock.minKey;
2022-06-27 12:03:19 +00:00
}
2022-06-28 03:22:56 +00:00
code = tsdbMergeTableData(pCommitter, pIter, pBlock, toKey, toDataOnly);
if (code) goto _err;
2022-06-27 12:03:19 +00:00
}
2022-06-28 02:35:37 +00:00
pRow = tsdbTbDataIterGet(pIter);
if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
iBlock++;
if (iBlock < nBlock) {
2022-07-30 12:08:04 +00:00
tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
2022-06-28 02:35:37 +00:00
} else {
pBlock = NULL;
}
2022-06-27 12:03:19 +00:00
}
}
} else if (pBlock) {
2022-06-27 13:05:17 +00:00
code = tsdbCommitTableDiskData(pCommitter, pBlock, pBlockIdx);
2022-06-27 12:03:19 +00:00
if (code) goto _err;
2022-06-27 13:05:17 +00:00
iBlock++;
if (iBlock < nBlock) {
2022-07-30 12:08:04 +00:00
tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
2022-06-27 13:05:17 +00:00
} else {
pBlock = NULL;
}
2022-06-27 12:03:19 +00:00
} else {
2022-06-27 13:05:17 +00:00
code =
tsdbCommitTableMemData(pCommitter, pIter, (TSDBKEY){.ts = pCommitter->maxKey + 1, .version = VERSION_MIN}, 0);
2022-06-27 12:03:19 +00:00
if (code) goto _err;
pRow = tsdbTbDataIterGet(pIter);
2022-06-28 02:35:37 +00:00
if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) pRow = NULL;
ASSERT(pRow == NULL);
2022-06-27 12:03:19 +00:00
}
}
2022-06-27 13:05:17 +00:00
// end =====================
code = tsdbCommitTableDataEnd(pCommitter, suid, uid);
2022-06-27 12:03:19 +00:00
if (code) goto _err;
_exit:
if (pIter) {
pRow = tsdbTbDataIterGet(pIter);
if (pRow) pCommitter->nextKey = TMIN(pCommitter->nextKey, TSDBROW_TS(pRow));
}
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, tsdb commit table data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
2022-06-27 12:03:19 +00:00
return code;
}
2022-06-30 06:35:50 +00:00
static int32_t tsdbCommitFileDataEnd(SCommitter *pCommitter) {
int32_t code = 0;
// write blockIdx
2022-07-30 12:08:04 +00:00
code = tsdbWriteBlockIdx(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockIdx, NULL);
2022-06-30 06:35:50 +00:00
if (code) goto _err;
// update file header
2022-07-30 12:08:04 +00:00
code = tsdbUpdateDFileSetHeader(pCommitter->dWriter.pWriter);
2022-06-30 06:35:50 +00:00
if (code) goto _err;
// upsert SDFileSet
2022-07-30 12:08:04 +00:00
code = tsdbFSUpsertFSet(&pCommitter->fs, &pCommitter->dWriter.pWriter->wSet);
2022-06-30 06:35:50 +00:00
if (code) goto _err;
// close and sync
2022-07-30 12:08:04 +00:00
code = tsdbDataFWriterClose(&pCommitter->dWriter.pWriter, 1);
2022-06-30 06:35:50 +00:00
if (code) goto _err;
2022-07-30 12:08:04 +00:00
if (pCommitter->dReader.pReader) {
code = tsdbDataFReaderClose(&pCommitter->dReader.pReader);
2022-07-07 02:56:12 +00:00
if (code) goto _err;
2022-06-30 06:35:50 +00:00
}
_exit:
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, commit file data end failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
2022-06-30 06:35:50 +00:00
return code;
}
static int32_t tsdbCommitFileData(SCommitter *pCommitter) {
2022-06-10 06:48:21 +00:00
int32_t code = 0;
STsdb *pTsdb = pCommitter->pTsdb;
SMemTable *pMemTable = pTsdb->imem;
2022-06-30 06:35:50 +00:00
// commit file data start
code = tsdbCommitFileDataStart(pCommitter);
if (code) goto _err;
// commit file data impl
2022-06-10 06:48:21 +00:00
int32_t iTbData = 0;
int32_t nTbData = taosArrayGetSize(pMemTable->aTbData);
2022-06-13 10:44:57 +00:00
int32_t iBlockIdx = 0;
2022-07-30 12:08:04 +00:00
int32_t nBlockIdx = taosArrayGetSize(pCommitter->dReader.aBlockIdx);
2022-06-10 06:48:21 +00:00
STbData *pTbData;
2022-06-30 06:02:47 +00:00
SBlockIdx *pBlockIdx;
2022-06-10 06:48:21 +00:00
2022-06-14 13:19:46 +00:00
ASSERT(nTbData > 0);
2022-06-15 07:41:14 +00:00
2022-06-14 13:19:46 +00:00
pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
2022-07-30 12:08:04 +00:00
pBlockIdx = (iBlockIdx < nBlockIdx) ? (SBlockIdx *)taosArrayGet(pCommitter->dReader.aBlockIdx, iBlockIdx) : NULL;
2022-06-28 05:25:09 +00:00
while (pTbData || pBlockIdx) {
if (pTbData && pBlockIdx) {
int32_t c = tTABLEIDCmprFn(pTbData, pBlockIdx);
2022-06-23 13:23:43 +00:00
2022-06-28 05:25:09 +00:00
if (c == 0) {
2022-06-28 05:55:20 +00:00
goto _commit_table_mem_and_disk;
2022-06-28 05:25:09 +00:00
} else if (c < 0) {
2022-06-28 05:55:20 +00:00
goto _commit_table_mem_data;
2022-06-23 13:23:43 +00:00
} else {
2022-06-28 05:55:20 +00:00
goto _commit_table_disk_data;
2022-06-23 13:23:43 +00:00
}
2022-06-28 05:25:09 +00:00
} else if (pBlockIdx) {
2022-06-28 05:55:20 +00:00
goto _commit_table_disk_data;
} else {
goto _commit_table_mem_data;
}
2022-06-28 05:25:09 +00:00
2022-06-28 05:55:20 +00:00
_commit_table_mem_data:
code = tsdbCommitTableData(pCommitter, pTbData, NULL);
if (code) goto _err;
iTbData++;
2022-06-30 06:02:47 +00:00
pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData) : NULL;
2022-06-28 05:55:20 +00:00
continue;
2022-06-23 13:23:43 +00:00
2022-06-28 05:55:20 +00:00
_commit_table_disk_data:
code = tsdbCommitTableData(pCommitter, NULL, pBlockIdx);
if (code) goto _err;
iBlockIdx++;
2022-07-30 12:08:04 +00:00
pBlockIdx = (iBlockIdx < nBlockIdx) ? (SBlockIdx *)taosArrayGet(pCommitter->dReader.aBlockIdx, iBlockIdx) : NULL;
2022-06-28 05:55:20 +00:00
continue;
_commit_table_mem_and_disk:
code = tsdbCommitTableData(pCommitter, pTbData, pBlockIdx);
if (code) goto _err;
iBlockIdx++;
2022-07-30 12:08:04 +00:00
pBlockIdx = (iBlockIdx < nBlockIdx) ? (SBlockIdx *)taosArrayGet(pCommitter->dReader.aBlockIdx, iBlockIdx) : NULL;
2022-06-28 05:55:20 +00:00
iTbData++;
2022-06-30 06:02:47 +00:00
pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData) : NULL;
2022-06-28 05:55:20 +00:00
continue;
2022-06-23 13:23:43 +00:00
}
2022-06-14 13:19:46 +00:00
// commit file data end
code = tsdbCommitFileDataEnd(pCommitter);
2022-06-20 06:41:17 +00:00
if (code) goto _err;
2022-06-10 06:48:21 +00:00
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, commit file data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
2022-07-30 12:08:04 +00:00
tsdbDataFReaderClose(&pCommitter->dReader.pReader);
tsdbDataFWriterClose(&pCommitter->dWriter.pWriter, 0);
2022-06-10 06:48:21 +00:00
return code;
}
2022-06-14 13:19:46 +00:00
// ----------------------------------------------------------------------------
static int32_t tsdbStartCommit(STsdb *pTsdb, SCommitter *pCommitter) {
2022-06-10 06:48:21 +00:00
int32_t code = 0;
2022-06-13 10:44:57 +00:00
2022-06-14 13:19:46 +00:00
memset(pCommitter, 0, sizeof(*pCommitter));
ASSERT(pTsdb->mem && pTsdb->imem == NULL);
2022-06-22 12:03:44 +00:00
2022-07-19 06:26:09 +00:00
taosThreadRwlockWrlock(&pTsdb->rwLock);
2022-06-14 13:19:46 +00:00
pTsdb->imem = pTsdb->mem;
pTsdb->mem = NULL;
2022-07-19 06:26:09 +00:00
taosThreadRwlockUnlock(&pTsdb->rwLock);
2022-06-13 10:44:57 +00:00
2022-06-14 13:19:46 +00:00
pCommitter->pTsdb = pTsdb;
2022-06-23 03:08:19 +00:00
pCommitter->commitID = pTsdb->pVnode->state.commitID;
2022-06-20 06:41:17 +00:00
pCommitter->minutes = pTsdb->keepCfg.days;
pCommitter->precision = pTsdb->keepCfg.precision;
pCommitter->minRow = pTsdb->pVnode->config.tsdbCfg.minRows;
pCommitter->maxRow = pTsdb->pVnode->config.tsdbCfg.maxRows;
2022-06-24 07:42:17 +00:00
pCommitter->cmprAlg = pTsdb->pVnode->config.tsdbCfg.compression;
2022-06-10 06:48:21 +00:00
2022-07-21 11:42:42 +00:00
code = tsdbFSCopy(pTsdb, &pCommitter->fs);
2022-06-22 12:03:44 +00:00
if (code) goto _err;
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, tsdb start commit failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
2022-06-10 06:48:21 +00:00
return code;
}
2022-06-22 12:03:44 +00:00
static int32_t tsdbCommitDataStart(SCommitter *pCommitter) {
int32_t code = 0;
2022-07-30 12:08:04 +00:00
pCommitter->dReader.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
if (pCommitter->dReader.aBlockIdx == NULL) {
2022-07-01 15:10:46 +00:00
code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit;
}
2022-07-30 12:08:04 +00:00
pCommitter->dWriter.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
if (pCommitter->dWriter.aBlockIdx == NULL) {
2022-07-01 15:10:46 +00:00
code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit;
}
2022-07-30 12:08:04 +00:00
code = tBlockDataInit(&pCommitter->dReader.bData);
2022-06-22 12:03:44 +00:00
if (code) goto _exit;
2022-07-01 15:10:46 +00:00
2022-07-30 12:08:04 +00:00
code = tBlockDataInit(&pCommitter->dWriter.bData);
2022-07-01 15:10:46 +00:00
if (code) goto _exit;
2022-06-22 12:03:44 +00:00
_exit:
return code;
}
static void tsdbCommitDataEnd(SCommitter *pCommitter) {
2022-07-30 12:08:04 +00:00
taosArrayDestroy(pCommitter->dReader.aBlockIdx);
tMapDataClear(&pCommitter->dReader.mBlock);
tBlockDataClear(&pCommitter->dReader.bData, 1);
taosArrayDestroy(pCommitter->dWriter.aBlockIdx);
tMapDataClear(&pCommitter->dWriter.mBlock);
tBlockDataClear(&pCommitter->dWriter.bData, 1);
2022-07-02 10:04:51 +00:00
tTSchemaDestroy(pCommitter->skmTable.pTSchema);
tTSchemaDestroy(pCommitter->skmRow.pTSchema);
2022-06-22 12:03:44 +00:00
}
2022-06-14 11:46:46 +00:00
static int32_t tsdbCommitData(SCommitter *pCommitter) {
int32_t code = 0;
STsdb *pTsdb = pCommitter->pTsdb;
SMemTable *pMemTable = pTsdb->imem;
2022-06-11 09:20:29 +00:00
2022-06-14 11:46:46 +00:00
// check
2022-06-15 07:41:14 +00:00
if (pMemTable->nRow == 0) goto _exit;
2022-06-13 06:05:05 +00:00
2022-06-22 12:03:44 +00:00
// start ====================
code = tsdbCommitDataStart(pCommitter);
2022-06-23 01:39:47 +00:00
if (code) goto _err;
2022-06-22 12:03:44 +00:00
// impl ====================
pCommitter->nextKey = pMemTable->minKey;
2022-06-14 11:46:46 +00:00
while (pCommitter->nextKey < TSKEY_MAX) {
pCommitter->commitFid = tsdbKeyFid(pCommitter->nextKey, pCommitter->minutes, pCommitter->precision);
tsdbFidKeyRange(pCommitter->commitFid, pCommitter->minutes, pCommitter->precision, &pCommitter->minKey,
&pCommitter->maxKey);
code = tsdbCommitFileData(pCommitter);
2022-06-14 09:55:04 +00:00
if (code) goto _err;
2022-06-13 06:05:05 +00:00
}
2022-06-11 09:20:29 +00:00
2022-06-22 12:03:44 +00:00
// end ====================
tsdbCommitDataEnd(pCommitter);
2022-06-14 11:46:46 +00:00
_exit:
2022-08-01 09:23:52 +00:00
tsdbDebug("vgId:%d, commit data done, nRow:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nRow);
2022-06-14 11:46:46 +00:00
return code;
2022-06-11 09:20:29 +00:00
2022-06-14 11:46:46 +00:00
_err:
2022-06-22 12:03:44 +00:00
tsdbCommitDataEnd(pCommitter);
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, commit data failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
2022-06-14 11:46:46 +00:00
return code;
}
2022-06-11 09:20:29 +00:00
2022-06-14 11:46:46 +00:00
static int32_t tsdbCommitDel(SCommitter *pCommitter) {
int32_t code = 0;
STsdb *pTsdb = pCommitter->pTsdb;
SMemTable *pMemTable = pTsdb->imem;
2022-06-11 09:20:29 +00:00
2022-06-14 11:46:46 +00:00
if (pMemTable->nDel == 0) {
goto _exit;
2022-06-14 09:55:04 +00:00
}
2022-06-11 09:20:29 +00:00
2022-06-14 11:46:46 +00:00
// start
code = tsdbCommitDelStart(pCommitter);
if (code) {
goto _err;
}
2022-06-11 09:20:29 +00:00
2022-06-14 11:46:46 +00:00
// impl
2022-06-30 06:44:44 +00:00
int32_t iDelIdx = 0;
int32_t nDelIdx = taosArrayGetSize(pCommitter->aDelIdx);
int32_t iTbData = 0;
int32_t nTbData = taosArrayGetSize(pMemTable->aTbData);
STbData *pTbData;
SDelIdx *pDelIdx;
ASSERT(nTbData > 0);
pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
while (true) {
if (pTbData == NULL && pDelIdx == NULL) break;
if (pTbData && pDelIdx) {
int32_t c = tTABLEIDCmprFn(pTbData, pDelIdx);
if (c == 0) {
goto _commit_mem_and_disk_del;
} else if (c < 0) {
goto _commit_mem_del;
} else {
goto _commit_disk_del;
}
} else if (pTbData) {
goto _commit_mem_del;
} else {
goto _commit_disk_del;
}
_commit_mem_del:
code = tsdbCommitTableDel(pCommitter, pTbData, NULL);
if (code) goto _err;
iTbData++;
pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData) : NULL;
continue;
_commit_disk_del:
code = tsdbCommitTableDel(pCommitter, NULL, pDelIdx);
if (code) goto _err;
iDelIdx++;
pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
continue;
_commit_mem_and_disk_del:
code = tsdbCommitTableDel(pCommitter, pTbData, pDelIdx);
if (code) goto _err;
iTbData++;
pTbData = (iTbData < nTbData) ? (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData) : NULL;
iDelIdx++;
pDelIdx = (iDelIdx < nDelIdx) ? (SDelIdx *)taosArrayGet(pCommitter->aDelIdx, iDelIdx) : NULL;
continue;
2022-06-14 11:46:46 +00:00
}
2022-06-11 09:20:29 +00:00
2022-06-14 11:46:46 +00:00
// end
code = tsdbCommitDelEnd(pCommitter);
if (code) {
goto _err;
}
2022-06-11 09:20:29 +00:00
2022-06-14 09:55:04 +00:00
_exit:
2022-08-01 09:23:52 +00:00
tsdbDebug("vgId:%d, commit del done, nDel:%" PRId64, TD_VID(pTsdb->pVnode), pMemTable->nDel);
2022-06-11 09:20:29 +00:00
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, commit del failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
2022-06-11 09:20:29 +00:00
return code;
2022-06-14 11:46:46 +00:00
}
2022-06-14 13:19:46 +00:00
static int32_t tsdbEndCommit(SCommitter *pCommitter, int32_t eno) {
2022-06-24 13:10:39 +00:00
int32_t code = 0;
STsdb *pTsdb = pCommitter->pTsdb;
SMemTable *pMemTable = pTsdb->imem;
2022-07-21 11:56:18 +00:00
ASSERT(eno == 0);
2022-07-21 11:42:42 +00:00
code = tsdbFSCommit1(pTsdb, &pCommitter->fs);
if (code) goto _err;
2022-06-24 13:10:39 +00:00
2022-07-21 11:42:42 +00:00
// lock
2022-07-19 06:19:01 +00:00
taosThreadRwlockWrlock(&pTsdb->rwLock);
2022-07-21 11:42:42 +00:00
// commit or rollback
code = tsdbFSCommit2(pTsdb, &pCommitter->fs);
if (code) {
taosThreadRwlockUnlock(&pTsdb->rwLock);
goto _err;
}
2022-06-24 13:10:39 +00:00
pTsdb->imem = NULL;
2022-07-21 11:42:42 +00:00
// unlock
2022-07-19 06:19:01 +00:00
taosThreadRwlockUnlock(&pTsdb->rwLock);
tsdbUnrefMemTable(pMemTable);
2022-07-21 11:42:42 +00:00
tsdbFSDestroy(&pCommitter->fs);
2022-06-24 13:10:39 +00:00
2022-08-01 09:23:52 +00:00
tsdbInfo("vgId:%d, tsdb end commit", TD_VID(pTsdb->pVnode));
2022-06-24 13:10:39 +00:00
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, tsdb end commit failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
2022-06-14 13:19:46 +00:00
return code;
}