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

1481 lines
42 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-08-02 09:06:18 +00:00
typedef struct {
int64_t suid;
int64_t uid;
TSDBROW row;
} SRowInfo;
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;
2022-08-02 09:06:18 +00:00
// data
SArray *aBlockIdx; // SArray<SBlockIdx>
int32_t iBlockIdx;
SBlockIdx *pBlockIdx;
SMapData mBlock; // SMapData<SBlock>
SBlockData bData;
// last
SArray *aBlockL; // SArray<SBlockL>
int32_t iBlockL;
SBlockL *pBlockL;
SBlockData bDatal;
int32_t iRow;
2022-08-03 08:50:36 +00:00
SRowInfo *pRowInfo;
SRowInfo rowInfo;
2022-07-30 12:08:04 +00:00
} dReader;
struct {
SDataFWriter *pWriter;
SArray *aBlockIdx; // SArray<SBlockIdx>
2022-08-01 10:03:00 +00:00
SArray *aBlockL; // SArray<SBlockL>
2022-07-30 12:08:04 +00:00
SMapData mBlock; // SMapData<SBlock>
SBlockData bData;
2022-08-01 10:03:00 +00:00
SBlockData bDatal;
2022-07-30 12:08:04 +00:00
} 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-08-06 12:23:29 +00:00
code = tsdbDelFReaderOpen(&pCommitter->pDelFReader, pDelFileR, pTsdb);
2022-06-14 09:55:04 +00:00
if (code) goto _err;
2022-06-10 11:50:06 +00:00
2022-08-06 12:23:29 +00:00
code = tsdbReadDelIdx(pCommitter->pDelFReader, pCommitter->aDelIdx);
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;
2022-08-06 12:23:29 +00:00
code = tsdbReadDelData(pCommitter->pDelFReader, pDelIdx, pCommitter->aDelData);
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-08-06 12:23:29 +00:00
code = tsdbWriteDelData(pCommitter->pDelFWriter, pCommitter->aDelData, &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-08-06 12:23:29 +00:00
code = tsdbWriteDelIdx(pCommitter->pDelFWriter, pCommitter->aDelIdxN);
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-08-03 12:16:57 +00:00
static int32_t tsdbCommitterNextLastRow(SCommitter *pCommitter) {
2022-08-03 08:50:36 +00:00
int32_t code = 0;
ASSERT(pCommitter->dReader.pReader);
ASSERT(pCommitter->dReader.pRowInfo);
2022-08-04 05:42:15 +00:00
SBlockData *pBlockDatal = &pCommitter->dReader.bDatal;
2022-08-03 08:50:36 +00:00
pCommitter->dReader.iRow++;
2022-08-04 05:42:15 +00:00
if (pCommitter->dReader.iRow < pBlockDatal->nRow) {
2022-08-04 07:58:53 +00:00
if (pBlockDatal->uid) {
pCommitter->dReader.pRowInfo->uid = pBlockDatal->uid;
} else {
2022-08-04 05:42:15 +00:00
pCommitter->dReader.pRowInfo->uid = pBlockDatal->aUid[pCommitter->dReader.iRow];
}
pCommitter->dReader.pRowInfo->row = tsdbRowFromBlockData(pBlockDatal, pCommitter->dReader.iRow);
2022-08-03 08:50:36 +00:00
} else {
pCommitter->dReader.iBlockL++;
if (pCommitter->dReader.iBlockL < taosArrayGetSize(pCommitter->dReader.aBlockL)) {
pCommitter->dReader.pBlockL = (SBlockL *)taosArrayGet(pCommitter->dReader.aBlockL, pCommitter->dReader.iBlockL);
2022-08-07 13:14:43 +00:00
code = tsdbReadLastBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockL, pBlockDatal, NULL, 0);
2022-08-03 08:50:36 +00:00
if (code) goto _exit;
pCommitter->dReader.iRow = 0;
2022-08-04 05:42:15 +00:00
pCommitter->dReader.pRowInfo->suid = pBlockDatal->suid;
if (pBlockDatal->uid) {
pCommitter->dReader.pRowInfo->uid = pBlockDatal->uid;
} else {
pCommitter->dReader.pRowInfo->uid = pBlockDatal->aUid[0];
}
pCommitter->dReader.pRowInfo->row = tsdbRowFromBlockData(pBlockDatal, pCommitter->dReader.iRow);
2022-08-03 08:50:36 +00:00
} else {
pCommitter->dReader.pRowInfo = NULL;
}
}
_exit:
return code;
}
2022-08-04 07:58:53 +00:00
static int32_t tsdbCommitterNextTableData(SCommitter *pCommitter) {
int32_t code = 0;
ASSERT(pCommitter->dReader.pBlockIdx);
pCommitter->dReader.iBlockIdx++;
if (pCommitter->dReader.iBlockIdx < taosArrayGetSize(pCommitter->dReader.aBlockIdx)) {
pCommitter->dReader.pBlockIdx =
(SBlockIdx *)taosArrayGet(pCommitter->dReader.aBlockIdx, pCommitter->dReader.iBlockIdx);
2022-08-06 12:04:42 +00:00
code = tsdbReadBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
2022-08-04 07:58:53 +00:00
if (code) goto _exit;
ASSERT(pCommitter->dReader.mBlock.nItem > 0);
} else {
pCommitter->dReader.pBlockIdx = NULL;
}
_exit:
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
2022-08-03 08:50:36 +00:00
pCommitter->commitFid = tsdbKeyFid(pCommitter->nextKey, pCommitter->minutes, pCommitter->precision);
tsdbFidKeyRange(pCommitter->commitFid, pCommitter->minutes, pCommitter->precision, &pCommitter->minKey,
&pCommitter->maxKey);
2022-06-23 13:23:43 +00:00
pCommitter->nextKey = TSKEY_MAX;
2022-06-17 12:22:45 +00:00
2022-08-01 10:03:00 +00:00
// Reader
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-08-02 09:06:18 +00:00
// data
2022-08-06 12:04:42 +00:00
code = tsdbReadBlockIdx(pCommitter->dReader.pReader, pCommitter->dReader.aBlockIdx);
2022-06-17 12:22:45 +00:00
if (code) goto _err;
2022-08-01 10:03:00 +00:00
2022-08-02 09:06:18 +00:00
pCommitter->dReader.iBlockIdx = 0;
if (pCommitter->dReader.iBlockIdx < taosArrayGetSize(pCommitter->dReader.aBlockIdx)) {
pCommitter->dReader.pBlockIdx =
(SBlockIdx *)taosArrayGet(pCommitter->dReader.aBlockIdx, pCommitter->dReader.iBlockIdx);
2022-08-06 12:04:42 +00:00
code = tsdbReadBlock(pCommitter->dReader.pReader, pCommitter->dReader.pBlockIdx, &pCommitter->dReader.mBlock);
2022-08-02 09:06:18 +00:00
if (code) goto _err;
} else {
pCommitter->dReader.pBlockIdx = NULL;
}
2022-08-04 05:42:15 +00:00
tBlockDataReset(&pCommitter->dReader.bData);
2022-08-02 09:06:18 +00:00
// last
2022-08-06 12:04:42 +00:00
code = tsdbReadBlockL(pCommitter->dReader.pReader, pCommitter->dReader.aBlockL);
2022-08-01 10:03:00 +00:00
if (code) goto _err;
2022-08-03 08:50:36 +00:00
pCommitter->dReader.iBlockL = -1;
pCommitter->dReader.iRow = -1;
pCommitter->dReader.pRowInfo = &pCommitter->dReader.rowInfo;
2022-08-04 05:42:15 +00:00
tBlockDataReset(&pCommitter->dReader.bDatal);
2022-08-03 12:16:57 +00:00
code = tsdbCommitterNextLastRow(pCommitter);
2022-08-03 08:50:36 +00:00
if (code) goto _err;
2022-08-01 10:03:00 +00:00
} else {
2022-08-02 09:06:18 +00:00
pCommitter->dReader.pBlockIdx = NULL;
2022-08-03 08:50:36 +00:00
pCommitter->dReader.pRowInfo = NULL;
2022-06-17 06:29:06 +00:00
}
2022-06-17 02:50:25 +00:00
2022-08-01 10:03:00 +00:00
// Writer
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-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;
2022-08-01 10:03:00 +00:00
fHead = (SHeadFile){.commitID = pCommitter->commitID, .offset = 0, .size = 0, .loffset = 0};
2022-07-21 06:27:32 +00:00
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;
2022-08-01 10:03:00 +00:00
fHead = (SHeadFile){.commitID = pCommitter->commitID, .offset = 0, .size = 0, .loffset = 0};
2022-07-21 06:27:32 +00:00
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-08-01 10:03:00 +00:00
taosArrayClear(pCommitter->dWriter.aBlockIdx);
taosArrayClear(pCommitter->dWriter.aBlockL);
tMapDataReset(&pCommitter->dWriter.mBlock);
tBlockDataReset(&pCommitter->dWriter.bData);
tBlockDataReset(&pCommitter->dWriter.bDatal);
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-08-04 07:58:53 +00:00
static int32_t tsdbCommitDataBlock(SCommitter *pCommitter, SBlock *pBlock) {
2022-08-05 15:22:05 +00:00
int32_t code = 0;
SBlockData *pBlockData = &pCommitter->dWriter.bData;
SBlock block;
2022-08-03 08:50:36 +00:00
2022-08-05 15:22:05 +00:00
ASSERT(pBlockData->nRow > 0);
2022-08-03 08:50:36 +00:00
2022-08-04 07:58:53 +00:00
if (pBlock) {
block = *pBlock; // as a subblock
} else {
tBlockReset(&block); // as a new block
}
2022-08-06 12:04:42 +00:00
// info
2022-08-05 15:22:05 +00:00
block.nRow += pBlockData->nRow;
for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
TSDBKEY key = {.ts = pBlockData->aTSKEY[iRow], .version = pBlockData->aVersion[iRow]};
2022-08-03 08:50:36 +00:00
2022-08-05 15:22:05 +00:00
if (iRow == 0) {
if (tsdbKeyCmprFn(&block.minKey, &key) > 0) {
block.minKey = key;
}
} else {
if (pBlockData->aTSKEY[iRow] == pBlockData->aTSKEY[iRow - 1]) {
block.hasDup = 1;
}
}
if (iRow == pBlockData->nRow - 1 && tsdbKeyCmprFn(&block.maxKey, &key) < 0) {
block.maxKey = key;
}
block.minVer = TMIN(block.minVer, key.version);
block.maxVer = TMAX(block.maxVer, key.version);
}
// write
2022-08-06 07:45:24 +00:00
block.nSubBlock++;
code = tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, &block.aSubBlock[block.nSubBlock - 1],
2022-08-06 12:48:58 +00:00
((block.nSubBlock == 1) && !block.hasDup) ? &block.smaInfo : NULL, pCommitter->cmprAlg, 0);
2022-08-05 15:22:05 +00:00
if (code) goto _err;
// put SBlock
2022-08-03 08:50:36 +00:00
code = tMapDataPutItem(&pCommitter->dWriter.mBlock, &block, tPutBlock);
2022-08-05 15:22:05 +00:00
if (code) goto _err;
2022-08-03 08:50:36 +00:00
2022-08-05 15:22:05 +00:00
// clear
tBlockDataClearData(pBlockData);
2022-08-03 08:50:36 +00:00
2022-08-05 15:22:05 +00:00
return code;
_err:
tsdbError("vgId:%d tsdb commit data block failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
2022-08-03 08:50:36 +00:00
return code;
}
static int32_t tsdbCommitLastBlock(SCommitter *pCommitter) {
2022-08-05 15:22:05 +00:00
int32_t code = 0;
SBlockL blockL;
SBlockData *pBlockData = &pCommitter->dWriter.bDatal;
ASSERT(pBlockData->nRow > 0);
2022-08-06 12:04:42 +00:00
// info
2022-08-05 15:22:05 +00:00
blockL.suid = pBlockData->suid;
blockL.nRow = pBlockData->nRow;
blockL.minVer = VERSION_MAX;
blockL.maxVer = VERSION_MIN;
for (int32_t iRow = 0; iRow < pBlockData->nRow; iRow++) {
blockL.minVer = TMIN(blockL.minVer, pBlockData->aVersion[iRow]);
2022-08-06 07:45:24 +00:00
blockL.maxVer = TMAX(blockL.maxVer, pBlockData->aVersion[iRow]);
2022-08-05 15:22:05 +00:00
}
blockL.minUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[0];
blockL.maxUid = pBlockData->uid ? pBlockData->uid : pBlockData->aUid[pBlockData->nRow - 1];
2022-08-03 08:50:36 +00:00
2022-08-05 15:22:05 +00:00
// write
2022-08-06 12:48:58 +00:00
code = tsdbWriteBlockData(pCommitter->dWriter.pWriter, pBlockData, &blockL.bInfo, NULL, pCommitter->cmprAlg, 1);
2022-08-05 15:22:05 +00:00
if (code) goto _err;
2022-08-03 08:50:36 +00:00
2022-08-05 15:22:05 +00:00
// push SBlockL
2022-08-03 08:50:36 +00:00
if (taosArrayPush(pCommitter->dWriter.aBlockL, &blockL) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
2022-08-05 15:22:05 +00:00
goto _err;
2022-08-03 08:50:36 +00:00
}
2022-08-05 15:22:05 +00:00
// clear
tBlockDataClearData(pBlockData);
2022-08-03 08:50:36 +00:00
2022-08-05 15:22:05 +00:00
return code;
_err:
tsdbError("vgId:%d tsdb commit last block failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
2022-08-03 08:50:36 +00:00
return code;
}
2022-08-03 12:16:57 +00:00
static int32_t tsdbMergeCommitData(SCommitter *pCommitter, STbDataIter *pIter, SBlock *pBlock) {
2022-06-27 11:20:11 +00:00
int32_t code = 0;
2022-08-03 12:16:57 +00:00
STbData *pTbData = pIter->pTbData;
SBlockData *pBlockDataR = &pCommitter->dReader.bData;
SBlockData *pBlockDataW = &pCommitter->dWriter.bData;
2022-06-27 11:20:11 +00:00
2022-08-07 13:14:43 +00:00
code = tsdbReadDataBlock(pCommitter->dReader.pReader, pBlock, pBlockDataR, NULL, 0);
2022-07-04 03:01:19 +00:00
if (code) goto _err;
2022-08-03 12:16:57 +00:00
tBlockDataClearData(pBlockDataW);
int32_t iRow = 0;
TSDBROW row;
TSDBROW *pRow1 = tsdbTbDataIterGet(pIter);
TSDBROW *pRow2 = &row;
*pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
while (pRow1 && pRow2) {
int32_t c = tsdbRowCmprFn(pRow1, pRow2);
2022-06-27 11:20:11 +00:00
2022-08-03 12:16:57 +00:00
if (c < 0) {
code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow1));
if (code) goto _err;
2022-06-27 11:20:11 +00:00
2022-08-04 07:58:53 +00:00
code = tBlockDataAppendRow(pBlockDataW, pRow1, pCommitter->skmRow.pTSchema, pTbData->uid);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
2022-06-29 03:34:55 +00:00
2022-08-03 12:16:57 +00:00
// next
tsdbTbDataIterNext(pIter);
pRow1 = tsdbTbDataIterGet(pIter);
} else if (c > 0) {
2022-08-04 07:58:53 +00:00
code = tBlockDataAppendRow(pBlockDataW, pRow2, NULL, pTbData->uid);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
2022-06-27 11:20:11 +00:00
2022-08-03 12:16:57 +00:00
iRow++;
if (iRow < pBlockDataR->nRow) {
*pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
2022-06-27 15:42:55 +00:00
} else {
2022-08-03 12:16:57 +00:00
pRow2 = NULL;
2022-06-27 15:42:55 +00:00
}
2022-08-03 12:16:57 +00:00
} else {
ASSERT(0);
2022-06-27 11:20:11 +00:00
}
2022-08-03 12:16:57 +00:00
// check
if (pBlockDataW->nRow >= pCommitter->maxRow * 4 / 5) {
2022-08-04 07:58:53 +00:00
code = tsdbCommitDataBlock(pCommitter, NULL);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
2022-06-29 03:34:55 +00:00
}
2022-08-03 12:16:57 +00:00
}
2022-06-29 03:34:55 +00:00
2022-08-03 12:16:57 +00:00
while (pRow2) {
2022-08-04 07:58:53 +00:00
code = tBlockDataAppendRow(pBlockDataW, pRow2, NULL, pTbData->uid);
2022-06-27 15:42:55 +00:00
if (code) goto _err;
2022-08-03 12:16:57 +00:00
iRow++;
if (iRow < pBlockDataR->nRow) {
*pRow2 = tsdbRowFromBlockData(pBlockDataR, iRow);
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-08-03 12:16:57 +00:00
// check
if (pBlockDataW->nRow >= pCommitter->maxRow * 4 / 5) {
2022-08-04 07:58:53 +00:00
code = tsdbCommitDataBlock(pCommitter, NULL);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
2022-06-29 03:34:55 +00:00
}
2022-08-03 12:16:57 +00:00
}
2022-06-27 15:42:55 +00:00
2022-08-03 12:16:57 +00:00
// check
if (pBlockDataW->nRow > 0) {
2022-08-04 07:58:53 +00:00
code = tsdbCommitDataBlock(pCommitter, NULL);
2022-06-27 15:42:55 +00:00
if (code) goto _err;
2022-06-27 11:20:11 +00:00
}
return code;
_err:
2022-08-03 12:16:57 +00:00
tsdbError("vgId:%d, tsdb merge commit data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
2022-06-27 11:20:11 +00:00
return code;
}
2022-08-03 12:16:57 +00:00
static int32_t tsdbCommitTableMemData(SCommitter *pCommitter, STbDataIter *pIter, TSDBKEY toKey) {
int32_t code = 0;
STbData *pTbData = pIter->pTbData;
2022-07-30 12:08:04 +00:00
SBlockData *pBlockData = &pCommitter->dWriter.bData;
2022-06-27 11:20:11 +00:00
2022-07-04 03:01:19 +00:00
tBlockDataClearData(pBlockData);
2022-08-03 12:16:57 +00:00
TSDBROW *pRow = tsdbTbDataIterGet(pIter);
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-08-03 12:16:57 +00:00
code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->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-08-04 07:58:53 +00:00
code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
2022-06-27 11:20:11 +00:00
if (code) goto _err;
tsdbTbDataIterNext(pIter);
pRow = tsdbTbDataIterGet(pIter);
if (pRow) {
2022-08-03 12:16:57 +00:00
TSDBKEY rowKey = TSDBROW_KEY(pRow);
if (tsdbKeyCmprFn(&rowKey, &toKey) >= 0) {
pRow = NULL;
}
}
2022-06-27 11:20:11 +00:00
2022-08-03 12:16:57 +00:00
if (pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
_write_block:
2022-08-04 07:58:53 +00:00
code = tsdbCommitDataBlock(pCommitter, NULL);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
}
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-08-03 08:50:36 +00:00
static int32_t tsdbGetNumOfRowsLessThan(STbDataIter *pIter, TSDBKEY key) {
int32_t nRow = 0;
2022-06-28 03:22:56 +00:00
2022-08-03 08:50:36 +00:00
STbDataIter iter = *pIter;
2022-06-28 03:22:56 +00:00
while (true) {
2022-08-03 08:50:36 +00:00
TSDBROW *pRow = tsdbTbDataIterGet(&iter);
2022-06-28 03:22:56 +00:00
if (pRow == NULL) break;
2022-08-03 08:50:36 +00:00
int32_t c = tsdbKeyCmprFn(&TSDBROW_KEY(pRow), &key);
if (c < 0) {
2022-06-28 03:22:56 +00:00
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-08-03 12:16:57 +00:00
STbData *pTbData = pIter->pTbData;
2022-07-30 12:08:04 +00:00
SBlockData *pBlockData = &pCommitter->dWriter.bData;
2022-07-04 03:01:19 +00:00
2022-08-03 12:16:57 +00:00
tBlockDataClearData(pBlockData);
2022-08-04 07:58:53 +00:00
TSDBROW *pRow = tsdbTbDataIterGet(pIter);
2022-06-28 03:22:56 +00:00
while (true) {
2022-08-04 07:58:53 +00:00
if (pRow == NULL) break;
code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
if (code) goto _err;
code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
if (code) goto _err;
tsdbTbDataIterNext(pIter);
pRow = tsdbTbDataIterGet(pIter);
2022-06-28 03:22:56 +00:00
if (pRow) {
2022-08-03 12:16:57 +00:00
TSDBKEY rowKey = TSDBROW_KEY(pRow);
if (tsdbKeyCmprFn(&rowKey, &pBlock->maxKey) > 0) {
2022-06-28 03:22:56 +00:00
pRow = NULL;
}
}
}
2022-08-04 07:58:53 +00:00
ASSERT(pBlockData->nRow > 0 && pBlock->nRow + pBlockData->nRow <= pCommitter->maxRow);
2022-08-03 12:16:57 +00:00
2022-08-04 07:58:53 +00:00
code = tsdbCommitDataBlock(pCommitter, pBlock);
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-08-03 12:16:57 +00:00
static int32_t tsdbMergeCommitLast(SCommitter *pCommitter, STbDataIter *pIter) {
int32_t code = 0;
STbData *pTbData = pIter->pTbData;
int32_t nRow = tsdbGetNumOfRowsLessThan(pIter, (TSDBKEY){.ts = pCommitter->maxKey + 1, .version = VERSION_MIN});
2022-08-03 08:50:36 +00:00
2022-08-03 12:16:57 +00:00
if (pCommitter->dReader.pRowInfo) {
2022-08-04 07:58:53 +00:00
if (pCommitter->dReader.pRowInfo->suid) {
for (int32_t iRow = pCommitter->dReader.iRow; iRow < pCommitter->dReader.bDatal.nRow; iRow++) {
if (pTbData->uid != pCommitter->dReader.bDatal.aUid[iRow]) break;
nRow++;
}
} else {
ASSERT(pCommitter->dReader.iRow == 0);
nRow += pCommitter->dReader.bDatal.nRow;
2022-08-03 12:16:57 +00:00
}
}
if (nRow == 0) goto _exit;
2022-08-04 07:58:53 +00:00
TSDBROW *pRow = tsdbTbDataIterGet(pIter);
2022-08-03 12:16:57 +00:00
if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
pRow = NULL;
}
2022-08-04 07:58:53 +00:00
SRowInfo *pRowInfo = pCommitter->dReader.pRowInfo;
if (pRowInfo && pRowInfo->uid != pTbData->uid) {
2022-08-03 12:16:57 +00:00
pRowInfo = NULL;
}
while (nRow) {
2022-08-04 07:58:53 +00:00
SBlockData *pBlockData;
int8_t toData;
2022-08-03 12:16:57 +00:00
if (nRow < pCommitter->minRow) { // to .last
2022-08-04 07:58:53 +00:00
toData = 0;
2022-08-03 12:16:57 +00:00
pBlockData = &pCommitter->dWriter.bDatal;
2022-08-04 07:58:53 +00:00
// commit and reset block data schema if need
if (pBlockData->nRow > 0) {
if (pBlockData->suid != pTbData->suid || pBlockData->suid == 0) {
code = tsdbCommitLastBlock(pCommitter);
if (code) goto _err;
2022-08-03 12:16:57 +00:00
2022-08-04 07:58:53 +00:00
tBlockDataReset(pBlockData);
}
}
// set block data schema if need
if (pBlockData->suid == 0 && pBlockData->uid == 0) {
code = tBlockDataSetSchema(pBlockData, pCommitter->skmTable.pTSchema, pTbData->suid,
pTbData->suid ? 0 : pTbData->uid);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
}
if (pBlockData->nRow + nRow > pCommitter->maxRow) {
code = tsdbCommitLastBlock(pCommitter);
if (code) goto _err;
}
} else { // to .data
2022-08-04 07:58:53 +00:00
toData = 1;
2022-08-03 12:16:57 +00:00
pBlockData = &pCommitter->dWriter.bData;
2022-08-04 07:58:53 +00:00
ASSERT(pBlockData->nRow == 0);
2022-08-03 12:16:57 +00:00
}
while (pRow && pRowInfo) {
int32_t c = tsdbRowCmprFn(pRow, &pRowInfo->row);
if (c < 0) {
code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
if (code) goto _err;
2022-08-04 07:58:53 +00:00
code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
tsdbTbDataIterNext(pIter);
pRow = tsdbTbDataIterGet(pIter);
if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
pRow = NULL;
}
} else if (c > 0) {
2022-08-04 07:58:53 +00:00
code = tBlockDataAppendRow(pBlockData, &pRowInfo->row, NULL, pTbData->uid);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
code = tsdbCommitterNextLastRow(pCommitter);
if (code) goto _err;
pRowInfo = pCommitter->dReader.pRowInfo;
2022-08-04 07:58:53 +00:00
if (pRowInfo && pRowInfo->uid != pTbData->uid) {
2022-08-03 12:16:57 +00:00
pRowInfo = NULL;
}
} else {
ASSERT(0);
}
nRow--;
2022-08-04 07:58:53 +00:00
if (toData) {
if (nRow == 0 || pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
code = tsdbCommitDataBlock(pCommitter, NULL);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
goto _outer_break;
}
}
}
while (pRow) {
code = tsdbCommitterUpdateRowSchema(pCommitter, pTbData->suid, pTbData->uid, TSDBROW_SVERSION(pRow));
if (code) goto _err;
2022-08-04 07:58:53 +00:00
code = tBlockDataAppendRow(pBlockData, pRow, pCommitter->skmRow.pTSchema, pTbData->uid);
2022-08-03 08:50:36 +00:00
if (code) goto _err;
tsdbTbDataIterNext(pIter);
pRow = tsdbTbDataIterGet(pIter);
if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
pRow = NULL;
}
2022-08-03 12:16:57 +00:00
nRow--;
2022-08-04 07:58:53 +00:00
if (toData) {
if (nRow == 0 || pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
code = tsdbCommitDataBlock(pCommitter, NULL);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
goto _outer_break;
2022-08-03 08:50:36 +00:00
}
}
2022-08-03 12:16:57 +00:00
}
while (pRowInfo) {
2022-08-04 07:58:53 +00:00
code = tBlockDataAppendRow(pBlockData, &pRowInfo->row, NULL, pTbData->uid);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
code = tsdbCommitterNextLastRow(pCommitter);
if (code) goto _err;
2022-08-03 08:50:36 +00:00
pRowInfo = pCommitter->dReader.pRowInfo;
2022-08-04 07:58:53 +00:00
if (pRowInfo && pRowInfo->uid != pTbData->uid) {
2022-08-03 08:50:36 +00:00
pRowInfo = NULL;
}
2022-08-03 12:16:57 +00:00
nRow--;
2022-08-04 07:58:53 +00:00
if (toData) {
if (nRow == 0 || pBlockData->nRow >= pCommitter->maxRow * 4 / 5) {
code = tsdbCommitDataBlock(pCommitter, NULL);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
goto _outer_break;
}
2022-08-03 08:50:36 +00:00
}
}
2022-08-03 12:16:57 +00:00
_outer_break:
ASSERT(nRow >= 0);
2022-08-03 08:50:36 +00:00
}
2022-08-03 12:16:57 +00:00
_exit:
2022-08-03 08:50:36 +00:00
return code;
_err:
2022-08-03 12:16:57 +00:00
tsdbError("vgId:%d tsdb merge commit last failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
2022-08-03 08:50:36 +00:00
return code;
}
2022-08-03 01:45:56 +00:00
static int32_t tsdbCommitTableData(SCommitter *pCommitter, STbData *pTbData) {
int32_t code = 0;
ASSERT(pCommitter->dReader.pBlockIdx == NULL || tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, pTbData) >= 0);
2022-08-03 08:50:36 +00:00
ASSERT(pCommitter->dReader.pRowInfo == NULL || tTABLEIDCmprFn(pCommitter->dReader.pRowInfo, pTbData) >= 0);
2022-08-03 01:45:56 +00:00
// merge commit table data
STbDataIter iter = {0};
STbDataIter *pIter = &iter;
TSDBROW *pRow;
tsdbTbDataIterOpen(pTbData, &(TSDBKEY){.ts = pCommitter->minKey, .version = VERSION_MIN}, 0, pIter);
pRow = tsdbTbDataIterGet(pIter);
if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
pRow = NULL;
}
if (pRow == NULL) goto _exit;
2022-08-03 12:16:57 +00:00
int32_t iBlock = 0;
SBlock block;
SBlock *pBlock = &block;
2022-08-04 07:58:53 +00:00
if (pCommitter->dReader.pBlockIdx && tTABLEIDCmprFn(pTbData, pCommitter->dReader.pBlockIdx) == 0) {
2022-08-03 01:45:56 +00:00
tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
} else {
pBlock = NULL;
}
2022-08-03 12:16:57 +00:00
code = tsdbCommitterUpdateTableSchema(pCommitter, pTbData->suid, pTbData->uid, pTbData->maxSkmVer);
if (code) goto _err;
2022-08-03 01:45:56 +00:00
tMapDataReset(&pCommitter->dWriter.mBlock);
2022-08-04 07:58:53 +00:00
code = tBlockDataSetSchema(&pCommitter->dWriter.bData, pCommitter->skmTable.pTSchema, pTbData->suid, pTbData->uid);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
2022-08-04 07:58:53 +00:00
// .data merge
2022-08-03 01:45:56 +00:00
while (pBlock && pRow) {
int32_t c = tBlockCmprFn(pBlock, &(SBlock){.minKey = TSDBROW_KEY(pRow), .maxKey = TSDBROW_KEY(pRow)});
if (c < 0) { // disk
code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pBlock, tPutBlock);
if (code) goto _err;
// next
iBlock++;
if (iBlock < pCommitter->dReader.mBlock.nItem) {
tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
} else {
pBlock = NULL;
}
2022-08-03 12:16:57 +00:00
} else if (c > 0) { // memory
code = tsdbCommitTableMemData(pCommitter, pIter, pBlock->minKey);
2022-08-03 01:45:56 +00:00
if (code) goto _err;
// next
pRow = tsdbTbDataIterGet(pIter);
if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
pRow = NULL;
}
} else { // merge
2022-08-03 08:50:36 +00:00
int32_t nOvlp = tsdbGetNumOfRowsLessThan(pIter, pBlock->maxKey);
2022-08-03 01:45:56 +00:00
ASSERT(nOvlp > 0);
if (pBlock->nRow + nOvlp <= pCommitter->maxRow && pBlock->nSubBlock < TSDB_MAX_SUBBLOCKS) {
code = tsdbMergeAsSubBlock(pCommitter, pIter, pBlock);
if (code) goto _err;
} else {
2022-08-03 12:16:57 +00:00
code = tsdbMergeCommitData(pCommitter, pIter, pBlock);
2022-08-03 01:45:56 +00:00
if (code) goto _err;
}
// next
pRow = tsdbTbDataIterGet(pIter);
if (pRow && TSDBROW_TS(pRow) > pCommitter->maxKey) {
pRow = NULL;
}
iBlock++;
if (iBlock < pCommitter->dReader.mBlock.nItem) {
tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
} else {
pBlock = NULL;
}
}
}
while (pBlock) {
code = tMapDataPutItem(&pCommitter->dWriter.mBlock, pBlock, tPutBlock);
if (code) goto _err;
2022-08-03 12:16:57 +00:00
// next
2022-08-03 01:45:56 +00:00
iBlock++;
if (iBlock < pCommitter->dReader.mBlock.nItem) {
tMapDataGetItemByIdx(&pCommitter->dReader.mBlock, iBlock, pBlock, tGetBlock);
} else {
pBlock = NULL;
}
}
2022-08-04 07:58:53 +00:00
// .data append and .last merge
2022-08-03 12:16:57 +00:00
code = tsdbMergeCommitLast(pCommitter, pIter);
if (code) goto _err;
2022-08-03 01:45:56 +00:00
// end
if (pCommitter->dWriter.mBlock.nItem > 0) {
SBlockIdx blockIdx = {.suid = pTbData->suid, .uid = pTbData->uid};
2022-08-06 12:04:42 +00:00
code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dWriter.mBlock, &blockIdx);
2022-08-03 01:45:56 +00:00
if (code) goto _err;
if (taosArrayPush(pCommitter->dWriter.aBlockIdx, &blockIdx) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
}
_exit:
pRow = tsdbTbDataIterGet(pIter);
if (pRow) {
pCommitter->nextKey = TMIN(pCommitter->nextKey, TSDBROW_TS(pRow));
}
return code;
_err:
tsdbError("vgId:%d tsdb commit table data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
return code;
}
2022-06-30 06:35:50 +00:00
static int32_t tsdbCommitFileDataEnd(SCommitter *pCommitter) {
int32_t code = 0;
2022-08-01 10:03:00 +00:00
// write aBlockIdx
2022-08-06 12:04:42 +00:00
code = tsdbWriteBlockIdx(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockIdx);
2022-06-30 06:35:50 +00:00
if (code) goto _err;
2022-08-03 12:16:57 +00:00
// write aBlockL
2022-08-06 12:04:42 +00:00
code = tsdbWriteBlockL(pCommitter->dWriter.pWriter, pCommitter->dWriter.aBlockL);
2022-08-03 12:16:57 +00:00
if (code) goto _err;
2022-06-30 06:35:50 +00:00
// 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;
}
2022-08-02 09:06:18 +00:00
static int32_t tsdbMoveCommitData(SCommitter *pCommitter, TABLEID toTable) {
int32_t code = 0;
2022-08-04 07:58:53 +00:00
// .data
2022-08-02 09:06:18 +00:00
while (true) {
if (pCommitter->dReader.pBlockIdx == NULL || tTABLEIDCmprFn(pCommitter->dReader.pBlockIdx, &toTable) >= 0) break;
SBlockIdx blockIdx = *pCommitter->dReader.pBlockIdx;
2022-08-06 12:04:42 +00:00
code = tsdbWriteBlock(pCommitter->dWriter.pWriter, &pCommitter->dReader.mBlock, &blockIdx);
2022-08-02 09:06:18 +00:00
if (code) goto _err;
if (taosArrayPush(pCommitter->dWriter.aBlockIdx, &blockIdx) == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _err;
}
2022-08-04 07:58:53 +00:00
code = tsdbCommitterNextTableData(pCommitter);
if (code) goto _err;
2022-08-02 09:06:18 +00:00
}
2022-08-04 07:58:53 +00:00
// .last
2022-08-02 09:06:18 +00:00
while (true) {
2022-08-03 08:50:36 +00:00
if (pCommitter->dReader.pRowInfo == NULL || tTABLEIDCmprFn(pCommitter->dReader.pRowInfo, &toTable) >= 0) break;
2022-08-02 09:06:18 +00:00
2022-08-04 07:58:53 +00:00
SBlockData *pBlockDataR = &pCommitter->dReader.bDatal;
SBlockData *pBlockDataW = &pCommitter->dWriter.bDatal;
tb_uid_t suid = pCommitter->dReader.pRowInfo->suid;
tb_uid_t uid = pCommitter->dReader.pRowInfo->uid;
ASSERT((pBlockDataR->suid && !pBlockDataR->uid) || (!pBlockDataR->suid && pBlockDataR->uid));
ASSERT(pBlockDataR->nRow > 0);
// commit and reset block data schema if need
if (pBlockDataW->nRow > 0) {
if (pBlockDataW->suid != pCommitter->dReader.pRowInfo->suid || pBlockDataW->suid == 0) {
2022-08-03 08:50:36 +00:00
code = tsdbCommitLastBlock(pCommitter);
2022-08-03 01:45:56 +00:00
if (code) goto _err;
2022-08-04 07:58:53 +00:00
tBlockDataReset(pBlockDataW);
2022-08-03 01:45:56 +00:00
}
2022-08-03 08:50:36 +00:00
}
2022-08-04 07:58:53 +00:00
// set block data schema if need
if (pBlockDataW->suid == 0 && pBlockDataW->uid == 0) {
code = tsdbCommitterUpdateTableSchema(pCommitter, suid, uid, 1 /*TOOD*/);
2022-08-03 08:50:36 +00:00
if (code) goto _err;
2022-08-04 07:58:53 +00:00
code = tBlockDataSetSchema(pBlockDataW, pCommitter->skmTable.pTSchema, suid, suid ? 0 : uid);
2022-08-03 01:45:56 +00:00
if (code) goto _err;
2022-08-02 09:06:18 +00:00
}
2022-08-03 08:50:36 +00:00
// check if it can make sure that one table data in one block
int32_t nRow = 0;
2022-08-04 07:58:53 +00:00
if (pBlockDataR->suid) {
for (int32_t iRow = pCommitter->dReader.iRow; (iRow < pBlockDataR->nRow) && (pBlockDataR->aUid[iRow] == uid);
iRow++) {
nRow++;
}
} else {
ASSERT(pCommitter->dReader.iRow == 0);
nRow = pBlockDataR->nRow;
2022-08-03 08:50:36 +00:00
}
2022-08-02 09:06:18 +00:00
2022-08-03 08:50:36 +00:00
ASSERT(nRow > 0 && nRow < pCommitter->minRow);
2022-08-02 09:06:18 +00:00
2022-08-04 07:58:53 +00:00
if (pBlockDataW->nRow + nRow > pCommitter->maxRow) {
ASSERT(pBlockDataW->nRow > 0);
2022-08-02 09:06:18 +00:00
2022-08-03 08:50:36 +00:00
code = tsdbCommitLastBlock(pCommitter);
if (code) goto _err;
2022-08-02 09:06:18 +00:00
}
2022-08-03 08:50:36 +00:00
while (nRow > 0) {
2022-08-04 07:58:53 +00:00
code = tBlockDataAppendRow(pBlockDataW, &pCommitter->dReader.pRowInfo->row, NULL, uid);
2022-08-03 08:50:36 +00:00
if (code) goto _err;
2022-08-03 12:16:57 +00:00
code = tsdbCommitterNextLastRow(pCommitter);
2022-08-03 01:45:56 +00:00
if (code) goto _err;
2022-08-03 08:50:36 +00:00
nRow--;
2022-08-02 09:06:18 +00:00
}
}
return code;
_err:
tsdbError("vgId:%d tsdb move commit data failed since %s", TD_VID(pCommitter->pTsdb->pVnode), tstrerror(code));
return code;
}
2022-06-30 06:35:50 +00:00
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-08-02 09:06:18 +00:00
for (int32_t iTbData = 0; iTbData < taosArrayGetSize(pMemTable->aTbData); iTbData++) {
STbData *pTbData = (STbData *)taosArrayGetP(pMemTable->aTbData, iTbData);
2022-06-28 05:25:09 +00:00
2022-08-02 09:06:18 +00:00
// move commit until current (suid, uid)
2022-08-03 08:50:36 +00:00
code = tsdbMoveCommitData(pCommitter, *(TABLEID *)pTbData);
2022-06-28 05:55:20 +00:00
if (code) goto _err;
2022-08-03 01:45:56 +00:00
// commit current table data
2022-08-02 09:06:18 +00:00
code = tsdbCommitTableData(pCommitter, pTbData);
2022-06-28 05:55:20 +00:00
if (code) goto _err;
2022-08-02 09:06:18 +00:00
}
2022-06-28 05:55:20 +00:00
2022-08-02 09:06:18 +00:00
code = tsdbMoveCommitData(pCommitter, (TABLEID){.suid = INT64_MAX, .uid = INT64_MAX});
if (code) goto _err;
2022-06-28 05:55:20 +00:00
2022-08-02 09:06:18 +00:00
if (pCommitter->dWriter.bDatal.nRow > 0) {
2022-08-03 08:50:36 +00:00
code = tsdbCommitLastBlock(pCommitter);
2022-06-28 05:55:20 +00:00
if (code) goto _err;
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-08-01 10:03:00 +00:00
// Reader
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-08-03 08:50:36 +00:00
code = tBlockDataInit(&pCommitter->dReader.bData);
if (code) goto _exit;
2022-08-01 10:03:00 +00:00
pCommitter->dReader.aBlockL = taosArrayInit(0, sizeof(SBlockL));
if (pCommitter->dReader.aBlockL == NULL) {
2022-07-01 15:10:46 +00:00
code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit;
}
2022-08-01 10:03:00 +00:00
code = tBlockDataInit(&pCommitter->dReader.bDatal);
if (code) goto _exit;
// Writer
pCommitter->dWriter.aBlockIdx = taosArrayInit(0, sizeof(SBlockIdx));
if (pCommitter->dWriter.aBlockIdx == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit;
}
pCommitter->dWriter.aBlockL = taosArrayInit(0, sizeof(SBlockL));
if (pCommitter->dWriter.aBlockL == NULL) {
code = TSDB_CODE_OUT_OF_MEMORY;
goto _exit;
}
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
2022-08-01 10:03:00 +00:00
code = tBlockDataInit(&pCommitter->dWriter.bDatal);
if (code) goto _exit;
2022-06-22 12:03:44 +00:00
_exit:
return code;
}
static void tsdbCommitDataEnd(SCommitter *pCommitter) {
2022-08-01 10:03:00 +00:00
// Reader
2022-07-30 12:08:04 +00:00
taosArrayDestroy(pCommitter->dReader.aBlockIdx);
tMapDataClear(&pCommitter->dReader.mBlock);
tBlockDataClear(&pCommitter->dReader.bData, 1);
2022-08-03 08:50:36 +00:00
taosArrayDestroy(pCommitter->dReader.aBlockL);
2022-08-01 10:03:00 +00:00
tBlockDataClear(&pCommitter->dReader.bDatal, 1);
// Writer
2022-07-30 12:08:04 +00:00
taosArrayDestroy(pCommitter->dWriter.aBlockIdx);
2022-08-01 10:03:00 +00:00
taosArrayDestroy(pCommitter->dWriter.aBlockL);
2022-07-30 12:08:04 +00:00
tMapDataClear(&pCommitter->dWriter.mBlock);
tBlockDataClear(&pCommitter->dWriter.bData, 1);
2022-08-01 10:03:00 +00:00
tBlockDataClear(&pCommitter->dWriter.bDatal, 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) {
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;
}