2023-03-20 02:59:16 +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/>.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-05-10 06:54:13 +00:00
|
|
|
#include "inc/tsdbCommit.h"
|
2023-03-20 02:59:16 +00:00
|
|
|
|
2023-03-23 05:15:14 +00:00
|
|
|
// extern dependencies
|
2023-03-22 06:41:16 +00:00
|
|
|
typedef struct {
|
2023-05-30 10:06:28 +00:00
|
|
|
STsdb *tsdb;
|
2023-03-22 06:41:16 +00:00
|
|
|
int32_t minutes;
|
|
|
|
|
int8_t precision;
|
|
|
|
|
int32_t minRow;
|
|
|
|
|
int32_t maxRow;
|
|
|
|
|
int8_t cmprAlg;
|
|
|
|
|
int8_t sttTrigger;
|
2023-05-30 10:06:28 +00:00
|
|
|
int64_t compactVersion;
|
|
|
|
|
|
|
|
|
|
struct {
|
|
|
|
|
int64_t now;
|
|
|
|
|
TSKEY nextKey;
|
|
|
|
|
int32_t fid;
|
|
|
|
|
int32_t expLevel;
|
|
|
|
|
TSKEY minKey;
|
|
|
|
|
TSKEY maxKey;
|
|
|
|
|
STFileSet *fset;
|
|
|
|
|
TABLEID tbid[1];
|
|
|
|
|
} ctx[1];
|
|
|
|
|
|
|
|
|
|
int64_t eid; // edit id
|
|
|
|
|
TFileOpArray fopArray[1];
|
|
|
|
|
TTsdbIterArray iterArray[1];
|
|
|
|
|
SIterMerger *iterMerger;
|
2023-05-10 06:54:13 +00:00
|
|
|
|
2023-03-23 05:15:14 +00:00
|
|
|
// writer
|
2023-05-30 10:06:28 +00:00
|
|
|
SDataFileWriter *dataWriter;
|
|
|
|
|
SSttFileWriter *sttWriter;
|
|
|
|
|
} SCommitter2;
|
2023-03-20 02:59:16 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
static int32_t tsdbCommitOpenNewSttWriter(SCommitter2 *pCommitter) {
|
2023-04-11 06:22:36 +00:00
|
|
|
int32_t code = 0;
|
2023-05-11 10:16:55 +00:00
|
|
|
int32_t lino = 0;
|
2023-05-30 10:06:28 +00:00
|
|
|
STsdb *pTsdb = pCommitter->tsdb;
|
2023-05-18 07:06:21 +00:00
|
|
|
SVnode *pVnode = pTsdb->pVnode;
|
|
|
|
|
int32_t vid = TD_VID(pVnode);
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
SSttFileWriterConfig config[1];
|
|
|
|
|
SDiskID did[1];
|
2023-05-18 07:06:21 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
if (tfsAllocDisk(pVnode->pTfs, pCommitter->ctx->expLevel, did) < 0) {
|
2023-05-18 07:06:21 +00:00
|
|
|
code = TSDB_CODE_FS_NO_VALID_DISK;
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
config->tsdb = pTsdb;
|
|
|
|
|
config->maxRow = pCommitter->maxRow;
|
|
|
|
|
config->szPage = pVnode->config.tsdbPageSize;
|
|
|
|
|
config->cmprAlg = pCommitter->cmprAlg;
|
|
|
|
|
config->skmTb = NULL;
|
|
|
|
|
config->skmRow = NULL;
|
|
|
|
|
config->aBuf = NULL;
|
|
|
|
|
config->file.type = TSDB_FTYPE_STT;
|
|
|
|
|
config->file.did = did[0];
|
|
|
|
|
config->file.fid = pCommitter->ctx->fid;
|
|
|
|
|
config->file.cid = pCommitter->eid;
|
|
|
|
|
config->file.size = 0;
|
|
|
|
|
config->file.stt->level = 0;
|
|
|
|
|
config->file.stt->nseg = 0;
|
|
|
|
|
|
|
|
|
|
code = tsdbSttFileWriterOpen(config, &pCommitter->sttWriter);
|
2023-05-18 07:06:21 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
|
if (code) {
|
|
|
|
|
tsdbError("vgId:%d %s failed at line %d since %s", vid, __func__, lino, tstrerror(code));
|
|
|
|
|
} else {
|
|
|
|
|
tsdbDebug("vgId:%d %s success", vid, __func__);
|
|
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
2023-05-30 10:06:28 +00:00
|
|
|
static int32_t tsdbCommitOpenExistSttWriter(SCommitter2 *pCommitter, const STFile *pFile) {
|
2023-05-18 07:06:21 +00:00
|
|
|
int32_t code = 0;
|
|
|
|
|
int32_t lino = 0;
|
2023-05-30 10:06:28 +00:00
|
|
|
STsdb *pTsdb = pCommitter->tsdb;
|
2023-05-18 07:06:21 +00:00
|
|
|
SVnode *pVnode = pTsdb->pVnode;
|
|
|
|
|
int32_t vid = TD_VID(pVnode);
|
2023-04-11 06:22:36 +00:00
|
|
|
|
2023-05-11 10:16:55 +00:00
|
|
|
SSttFileWriterConfig config = {
|
2023-05-18 07:06:21 +00:00
|
|
|
//
|
2023-05-26 11:32:02 +00:00
|
|
|
.tsdb = pTsdb,
|
2023-03-27 11:12:25 +00:00
|
|
|
.maxRow = pCommitter->maxRow,
|
2023-05-18 07:06:21 +00:00
|
|
|
.szPage = pVnode->config.tsdbPageSize,
|
2023-03-27 11:12:25 +00:00
|
|
|
.cmprAlg = pCommitter->cmprAlg,
|
2023-05-26 11:32:02 +00:00
|
|
|
.skmTb = NULL,
|
|
|
|
|
.skmRow = NULL,
|
2023-03-27 11:12:25 +00:00
|
|
|
.aBuf = NULL,
|
2023-05-18 07:06:21 +00:00
|
|
|
.file = *pFile //
|
2023-03-27 11:12:25 +00:00
|
|
|
};
|
2023-03-23 08:10:08 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
code = tsdbSttFileWriterOpen(&config, &pCommitter->sttWriter);
|
2023-03-23 08:10:08 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
|
if (code) {
|
2023-05-18 07:06:21 +00:00
|
|
|
tsdbError("vgId:%d %s failed at line %d since %s", vid, __func__, lino, tstrerror(code));
|
|
|
|
|
} else {
|
|
|
|
|
tsdbDebug("vgId:%d %s success", vid, __func__);
|
2023-03-23 08:10:08 +00:00
|
|
|
}
|
2023-03-23 05:15:14 +00:00
|
|
|
return code;
|
|
|
|
|
}
|
2023-05-30 10:06:28 +00:00
|
|
|
static int32_t tsdbCommitOpenWriter(SCommitter2 *committer) {
|
|
|
|
|
if (!committer->ctx->fset) {
|
|
|
|
|
return tsdbCommitOpenNewSttWriter(committer);
|
2023-05-18 07:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
const SSttLvl *lvl0 = tsdbTFileSetGetLvl(committer->ctx->fset, 0);
|
2023-05-18 07:06:21 +00:00
|
|
|
if (lvl0 == NULL) {
|
2023-05-30 10:06:28 +00:00
|
|
|
return tsdbCommitOpenNewSttWriter(committer);
|
2023-05-18 07:06:21 +00:00
|
|
|
}
|
|
|
|
|
|
2023-05-31 02:59:47 +00:00
|
|
|
ASSERT(TARRAY2_SIZE(lvl0->fobjArr) > 0);
|
|
|
|
|
STFileObj *fobj = TARRAY2_LAST(lvl0->fobjArr);
|
2023-05-30 10:06:28 +00:00
|
|
|
if (fobj->f->stt->nseg >= committer->sttTrigger) {
|
|
|
|
|
return tsdbCommitOpenNewSttWriter(committer);
|
2023-05-18 07:06:21 +00:00
|
|
|
} else {
|
2023-05-30 10:06:28 +00:00
|
|
|
return tsdbCommitOpenExistSttWriter(committer, fobj->f);
|
2023-05-18 07:06:21 +00:00
|
|
|
}
|
|
|
|
|
}
|
2023-03-23 05:15:14 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
static int32_t tsdbCommitTSRow(SCommitter2 *committer, SRowInfo *row) {
|
|
|
|
|
return tsdbSttFileWriteTSData(committer->sttWriter, row);
|
2023-03-20 02:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
static int32_t tsdbCommitWriteDelData(SCommitter2 *pCommitter, int64_t suid, int64_t uid, int64_t version, int64_t sKey,
|
2023-03-23 05:15:14 +00:00
|
|
|
int64_t eKey) {
|
|
|
|
|
int32_t code = 0;
|
|
|
|
|
// TODO
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
static int32_t tsdbCommitTSData(SCommitter2 *committer) {
|
|
|
|
|
int32_t code = 0;
|
|
|
|
|
int32_t lino = 0;
|
|
|
|
|
int64_t nRow = 0;
|
|
|
|
|
int32_t vid = TD_VID(committer->tsdb->pVnode);
|
|
|
|
|
SRowInfo *row;
|
|
|
|
|
|
|
|
|
|
if (committer->tsdb->imem->nRow == 0) goto _exit;
|
|
|
|
|
|
|
|
|
|
// open iter and iter merger
|
|
|
|
|
STsdbIter *iter;
|
|
|
|
|
STsdbIterConfig config[1] = {{
|
|
|
|
|
.type = TSDB_ITER_TYPE_MEMT,
|
|
|
|
|
.memt = committer->tsdb->imem,
|
|
|
|
|
.from = {{
|
|
|
|
|
.ts = committer->ctx->minKey,
|
|
|
|
|
.version = VERSION_MIN,
|
|
|
|
|
}},
|
|
|
|
|
}};
|
|
|
|
|
|
|
|
|
|
code = tsdbIterOpen(config, &iter);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
2023-03-23 05:15:14 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
code = TARRAY2_APPEND(committer->iterArray, iter);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
2023-03-22 11:24:40 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
code = tsdbIterMergerInit(committer->iterArray, &committer->iterMerger);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
2023-03-22 11:24:40 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
// loop iter
|
|
|
|
|
while ((row = tsdbIterMergerGet(committer->iterMerger)) != NULL) {
|
|
|
|
|
if (row->uid != committer->ctx->tbid->uid) {
|
|
|
|
|
committer->ctx->tbid->suid = row->suid;
|
|
|
|
|
committer->ctx->tbid->uid = row->uid;
|
|
|
|
|
|
|
|
|
|
// Ignore deleted table
|
|
|
|
|
SMetaInfo info[1];
|
|
|
|
|
if (metaGetInfo(committer->tsdb->pVnode->pMeta, row->uid, info, NULL) != 0) {
|
|
|
|
|
code = tsdbIterMergerSkipTableData(committer->iterMerger, committer->ctx->tbid);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
continue;
|
2023-03-22 11:24:40 +00:00
|
|
|
}
|
2023-05-30 10:06:28 +00:00
|
|
|
}
|
2023-03-22 11:24:40 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
TSKEY ts = TSDBROW_TS(&row->row);
|
|
|
|
|
if (ts > committer->ctx->maxKey) {
|
|
|
|
|
committer->ctx->nextKey = TMIN(committer->ctx->nextKey, ts);
|
|
|
|
|
|
|
|
|
|
code = tsdbIterMergerSkipTableData(committer->iterMerger, committer->ctx->tbid);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
} else {
|
|
|
|
|
code = tsdbCommitTSRow(committer, row);
|
2023-03-22 11:24:40 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
2023-04-11 03:00:35 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
code = tsdbIterMergerNext(committer->iterMerger);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
2023-03-22 11:24:40 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
|
if (code) {
|
2023-05-30 10:06:28 +00:00
|
|
|
TSDB_ERROR_LOG(vid, lino, code);
|
2023-03-22 11:24:40 +00:00
|
|
|
} else {
|
2023-05-30 10:06:28 +00:00
|
|
|
tsdbDebug("vgId:%d %s done, fid:%d nRow:%" PRId64, vid, __func__, committer->ctx->fid, nRow);
|
2023-03-22 11:24:40 +00:00
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
static int32_t tsdbCommitDelData(SCommitter2 *pCommitter) {
|
2023-03-22 11:24:40 +00:00
|
|
|
int32_t code = 0;
|
|
|
|
|
int32_t lino;
|
|
|
|
|
|
2023-05-11 10:16:55 +00:00
|
|
|
return 0;
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
#if 0
|
2023-04-11 03:00:35 +00:00
|
|
|
ASSERTS(0, "TODO: Not implemented yet");
|
2023-03-23 05:26:57 +00:00
|
|
|
|
2023-03-23 05:15:14 +00:00
|
|
|
int64_t nDel = 0;
|
2023-05-30 10:06:28 +00:00
|
|
|
SMemTable *pMem = pCommitter->tsdb->imem;
|
2023-03-22 11:24:40 +00:00
|
|
|
|
2023-03-23 05:15:14 +00:00
|
|
|
if (pMem->nDel == 0) { // no del data
|
|
|
|
|
goto _exit;
|
|
|
|
|
}
|
2023-03-22 11:24:40 +00:00
|
|
|
|
|
|
|
|
for (int32_t iTbData = 0; iTbData < taosArrayGetSize(pCommitter->aTbDataP); iTbData++) {
|
|
|
|
|
STbData *pTbData = (STbData *)taosArrayGetP(pCommitter->aTbDataP, iTbData);
|
|
|
|
|
|
|
|
|
|
for (SDelData *pDelData = pTbData->pHead; pDelData; pDelData = pDelData->pNext) {
|
2023-05-30 10:06:28 +00:00
|
|
|
if (pDelData->eKey < pCommitter->ctx->minKey) continue;
|
|
|
|
|
if (pDelData->sKey > pCommitter->ctx->maxKey) {
|
|
|
|
|
pCommitter->ctx->nextKey = TMIN(pCommitter->ctx->nextKey, pDelData->sKey);
|
2023-03-23 05:15:14 +00:00
|
|
|
continue;
|
|
|
|
|
}
|
2023-03-22 11:24:40 +00:00
|
|
|
|
2023-03-23 05:15:14 +00:00
|
|
|
code = tsdbCommitWriteDelData(pCommitter, pTbData->suid, pTbData->uid, pDelData->version,
|
|
|
|
|
pDelData->sKey /* TODO */, pDelData->eKey /* TODO */);
|
2023-03-22 11:24:40 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
|
if (code) {
|
2023-05-30 10:06:28 +00:00
|
|
|
tsdbError("vgId:%d failed at line %d since %s", TD_VID(pCommitter->tsdb->pVnode), lino, tstrerror(code));
|
2023-03-22 11:24:40 +00:00
|
|
|
} else {
|
2023-05-30 10:06:28 +00:00
|
|
|
tsdbDebug("vgId:%d %s done, fid:%d nDel:%" PRId64, TD_VID(pCommitter->tsdb->pVnode), __func__, pCommitter->ctx->fid,
|
2023-03-22 11:24:40 +00:00
|
|
|
pMem->nDel);
|
|
|
|
|
}
|
|
|
|
|
return code;
|
2023-05-30 10:06:28 +00:00
|
|
|
#endif
|
2023-03-22 11:24:40 +00:00
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
static int32_t tsdbCommitFileSetBegin(SCommitter2 *committer) {
|
|
|
|
|
int32_t code = 0;
|
|
|
|
|
int32_t lino = 0;
|
|
|
|
|
STsdb *tsdb = committer->tsdb;
|
|
|
|
|
int32_t vid = TD_VID(tsdb->pVnode);
|
|
|
|
|
|
|
|
|
|
committer->ctx->fid = tsdbKeyFid(committer->ctx->nextKey, committer->minutes, committer->precision);
|
|
|
|
|
tsdbFidKeyRange(committer->ctx->fid, committer->minutes, committer->precision, &committer->ctx->minKey,
|
|
|
|
|
&committer->ctx->maxKey);
|
|
|
|
|
committer->ctx->expLevel = tsdbFidLevel(committer->ctx->fid, &tsdb->keepCfg, committer->ctx->now);
|
|
|
|
|
committer->ctx->nextKey = TSKEY_MAX;
|
2023-05-11 10:16:55 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
// TODO: use a thread safe function to get fset
|
|
|
|
|
tsdbFSGetFSet(tsdb->pFS, committer->ctx->fid, &committer->ctx->fset);
|
2023-03-22 11:24:40 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
code = tsdbCommitOpenWriter(committer);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
2023-04-11 06:22:36 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
_exit:
|
|
|
|
|
if (code) {
|
|
|
|
|
TSDB_ERROR_LOG(vid, lino, code);
|
|
|
|
|
} else {
|
|
|
|
|
tsdbDebug("vgId:%d %s done, fid:%d minKey:%" PRId64 " maxKey:%" PRId64 " expLevel:%d", vid, __func__,
|
|
|
|
|
committer->ctx->fid, committer->ctx->minKey, committer->ctx->maxKey, committer->ctx->expLevel);
|
|
|
|
|
}
|
2023-03-23 08:10:08 +00:00
|
|
|
return 0;
|
2023-03-20 02:59:16 +00:00
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
static int32_t tsdbCommitFileSetEnd(SCommitter2 *pCommitter) {
|
2023-03-22 11:24:40 +00:00
|
|
|
int32_t code = 0;
|
2023-05-11 10:16:55 +00:00
|
|
|
int32_t lino = 0;
|
2023-05-30 10:06:28 +00:00
|
|
|
int32_t vid = TD_VID(pCommitter->tsdb->pVnode);
|
2023-03-22 11:24:40 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
if (pCommitter->sttWriter == NULL) return 0;
|
2023-04-11 06:22:36 +00:00
|
|
|
|
2023-05-22 10:39:10 +00:00
|
|
|
STFileOp op;
|
2023-05-30 10:06:28 +00:00
|
|
|
code = tsdbSttFileWriterClose(&pCommitter->sttWriter, 0, &op);
|
2023-05-22 10:39:10 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
2023-04-11 06:22:36 +00:00
|
|
|
|
2023-05-28 15:33:03 +00:00
|
|
|
if (op.optype != TSDB_FOP_NONE) {
|
2023-05-30 10:06:28 +00:00
|
|
|
code = TARRAY2_APPEND(pCommitter->fopArray, op);
|
2023-05-28 15:33:03 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
}
|
2023-03-22 11:24:40 +00:00
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
|
if (code) {
|
2023-05-11 10:16:55 +00:00
|
|
|
tsdbError("vgId:%d failed at line %d since %s", vid, lino, tstrerror(code));
|
2023-04-11 06:22:36 +00:00
|
|
|
} else {
|
2023-05-30 10:06:28 +00:00
|
|
|
tsdbDebug("vgId:%d %s done, fid:%d", vid, __func__, pCommitter->ctx->fid);
|
2023-03-22 11:24:40 +00:00
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
static int32_t tsdbCommitFileSet(SCommitter2 *committer) {
|
2023-03-20 02:59:16 +00:00
|
|
|
int32_t code = 0;
|
|
|
|
|
int32_t lino = 0;
|
2023-05-30 10:06:28 +00:00
|
|
|
int32_t vid = TD_VID(committer->tsdb->pVnode);
|
2023-03-20 02:59:16 +00:00
|
|
|
|
2023-03-22 11:24:40 +00:00
|
|
|
// fset commit start
|
2023-05-30 10:06:28 +00:00
|
|
|
code = tsdbCommitFileSetBegin(committer);
|
2023-03-22 11:24:40 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
2023-03-20 02:59:16 +00:00
|
|
|
|
2023-03-22 06:41:16 +00:00
|
|
|
// commit fset
|
2023-05-30 10:06:28 +00:00
|
|
|
code = tsdbCommitTSData(committer);
|
2023-03-20 02:59:16 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
code = tsdbCommitDelData(committer);
|
2023-03-22 11:24:40 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
|
|
|
|
|
// fset commit end
|
2023-05-30 10:06:28 +00:00
|
|
|
code = tsdbCommitFileSetEnd(committer);
|
2023-03-22 11:24:40 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
2023-03-20 02:59:16 +00:00
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
|
if (code) {
|
2023-05-30 10:06:28 +00:00
|
|
|
TSDB_ERROR_LOG(vid, lino, code);
|
2023-05-11 10:16:55 +00:00
|
|
|
} else {
|
2023-05-30 10:06:28 +00:00
|
|
|
tsdbDebug("vgId:%d %s done, fid:%d", vid, __func__, committer->ctx->fid);
|
2023-03-20 02:59:16 +00:00
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
static int32_t tsdbOpenCommitter(STsdb *tsdb, SCommitInfo *info, SCommitter2 *committer) {
|
2023-03-20 02:59:16 +00:00
|
|
|
int32_t code = 0;
|
2023-05-30 10:06:28 +00:00
|
|
|
int32_t lino = 0;
|
|
|
|
|
int32_t vid = TD_VID(tsdb->pVnode);
|
2023-03-20 02:59:16 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
memset(committer, 0, sizeof(committer[0]));
|
|
|
|
|
|
|
|
|
|
committer->tsdb = tsdb;
|
|
|
|
|
committer->minutes = tsdb->keepCfg.days;
|
|
|
|
|
committer->precision = tsdb->keepCfg.precision;
|
|
|
|
|
committer->minRow = info->info.config.tsdbCfg.minRows;
|
|
|
|
|
committer->maxRow = info->info.config.tsdbCfg.maxRows;
|
|
|
|
|
committer->cmprAlg = info->info.config.tsdbCfg.compression;
|
|
|
|
|
committer->sttTrigger = info->info.config.sttTrigger;
|
|
|
|
|
committer->compactVersion = INT64_MAX; // TODO: use a function
|
2023-03-20 02:59:16 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
TARRAY2_INIT(committer->fopArray);
|
|
|
|
|
tsdbFSAllocEid(tsdb->pFS, &committer->eid);
|
|
|
|
|
|
|
|
|
|
committer->ctx->now = taosGetTimestampSec();
|
|
|
|
|
committer->ctx->nextKey = tsdb->imem->minKey; // TODO
|
2023-03-20 02:59:16 +00:00
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
|
if (code) {
|
2023-05-30 10:06:28 +00:00
|
|
|
TSDB_ERROR_LOG(vid, lino, code);
|
2023-03-20 02:59:16 +00:00
|
|
|
} else {
|
2023-05-30 10:06:28 +00:00
|
|
|
tsdbDebug("vgId:%d %s done", TD_VID(tsdb->pVnode), __func__);
|
2023-03-20 02:59:16 +00:00
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
static int32_t tsdbCloseCommitter(SCommitter2 *pCommiter, int32_t eno) {
|
2023-03-20 02:59:16 +00:00
|
|
|
int32_t code = 0;
|
2023-05-11 10:16:55 +00:00
|
|
|
int32_t lino = 0;
|
2023-05-30 10:06:28 +00:00
|
|
|
int32_t vid = TD_VID(pCommiter->tsdb->pVnode);
|
2023-04-06 06:27:18 +00:00
|
|
|
|
2023-04-11 07:34:40 +00:00
|
|
|
if (eno == 0) {
|
2023-05-30 10:06:28 +00:00
|
|
|
code = tsdbFSEditBegin(pCommiter->tsdb->pFS, pCommiter->fopArray, TSDB_FEDIT_COMMIT);
|
2023-05-11 10:16:55 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
2023-04-11 07:34:40 +00:00
|
|
|
} else {
|
2023-05-11 10:16:55 +00:00
|
|
|
// TODO
|
|
|
|
|
ASSERT(0);
|
2023-04-11 07:34:40 +00:00
|
|
|
}
|
2023-04-06 06:27:18 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
ASSERT(pCommiter->sttWriter == NULL);
|
|
|
|
|
TARRAY2_FREE(pCommiter->fopArray);
|
2023-04-12 07:18:12 +00:00
|
|
|
|
2023-04-06 06:27:18 +00:00
|
|
|
_exit:
|
2023-04-11 07:34:40 +00:00
|
|
|
if (code) {
|
2023-05-11 10:16:55 +00:00
|
|
|
tsdbError("vgId:%d %s failed at line %d since %s, eid:%" PRId64, vid, __func__, lino, tstrerror(code),
|
|
|
|
|
pCommiter->eid);
|
2023-04-11 07:34:40 +00:00
|
|
|
} else {
|
2023-05-11 10:16:55 +00:00
|
|
|
tsdbDebug("vgId:%d %s done, eid:%" PRId64, vid, __func__, pCommiter->eid);
|
2023-04-11 07:34:40 +00:00
|
|
|
}
|
2023-03-20 02:59:16 +00:00
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
int32_t tsdbPreCommit(STsdb *tsdb) {
|
|
|
|
|
taosThreadRwlockWrlock(&tsdb->rwLock);
|
|
|
|
|
ASSERT(tsdb->imem == NULL);
|
|
|
|
|
tsdb->imem = tsdb->mem;
|
|
|
|
|
tsdb->mem = NULL;
|
|
|
|
|
taosThreadRwlockUnlock(&tsdb->rwLock);
|
2023-03-20 02:59:16 +00:00
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
int32_t tsdbCommitBegin(STsdb *tsdb, SCommitInfo *info) {
|
|
|
|
|
if (!tsdb) return 0;
|
2023-03-20 02:59:16 +00:00
|
|
|
|
|
|
|
|
int32_t code = 0;
|
|
|
|
|
int32_t lino = 0;
|
2023-05-30 10:06:28 +00:00
|
|
|
int32_t vid = TD_VID(tsdb->pVnode);
|
|
|
|
|
SMemTable *memt = tsdb->imem;
|
|
|
|
|
int64_t nRow = memt->nRow;
|
|
|
|
|
int64_t nDel = memt->nDel;
|
|
|
|
|
|
|
|
|
|
if (!nRow && !nDel) {
|
|
|
|
|
taosThreadRwlockWrlock(&tsdb->rwLock);
|
|
|
|
|
tsdb->imem = NULL;
|
|
|
|
|
taosThreadRwlockUnlock(&tsdb->rwLock);
|
|
|
|
|
tsdbUnrefMemTable(memt, NULL, true);
|
2023-03-22 06:41:16 +00:00
|
|
|
} else {
|
2023-05-30 10:06:28 +00:00
|
|
|
SCommitter2 committer[1];
|
2023-03-20 02:59:16 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
code = tsdbOpenCommitter(tsdb, info, committer);
|
2023-03-22 06:41:16 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
2023-03-20 02:59:16 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
while (committer->ctx->nextKey != TSKEY_MAX) {
|
|
|
|
|
code = tsdbCommitFileSet(committer);
|
2023-05-11 10:16:55 +00:00
|
|
|
if (code) {
|
|
|
|
|
lino = __LINE__;
|
|
|
|
|
break;
|
|
|
|
|
}
|
2023-03-22 06:41:16 +00:00
|
|
|
}
|
2023-03-20 02:59:16 +00:00
|
|
|
|
2023-05-30 10:06:28 +00:00
|
|
|
code = tsdbCloseCommitter(committer, code);
|
2023-03-22 06:41:16 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
}
|
2023-03-20 02:59:16 +00:00
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
|
if (code) {
|
2023-05-30 10:06:28 +00:00
|
|
|
TSDB_ERROR_LOG(vid, lino, code);
|
2023-03-22 06:41:16 +00:00
|
|
|
} else {
|
2023-05-30 10:06:28 +00:00
|
|
|
tsdbInfo("vgId:%d %s done, nRow:%" PRId64 " nDel:%" PRId64, vid, __func__, nRow, nDel);
|
2023-03-20 02:59:16 +00:00
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-10 09:52:51 +00:00
|
|
|
int32_t tsdbCommitCommit(STsdb *pTsdb) {
|
2023-05-11 10:16:55 +00:00
|
|
|
int32_t code = 0;
|
|
|
|
|
int32_t lino = 0;
|
|
|
|
|
int32_t vid = TD_VID(pTsdb->pVnode);
|
2023-04-10 09:52:51 +00:00
|
|
|
|
2023-05-11 10:16:55 +00:00
|
|
|
if (pTsdb->imem == NULL) goto _exit;
|
2023-04-10 09:52:51 +00:00
|
|
|
|
2023-05-11 10:16:55 +00:00
|
|
|
SMemTable *pMemTable = pTsdb->imem;
|
|
|
|
|
taosThreadRwlockWrlock(&pTsdb->rwLock);
|
2023-05-09 08:46:23 +00:00
|
|
|
code = tsdbFSEditCommit(pTsdb->pFS);
|
2023-04-10 09:52:51 +00:00
|
|
|
if (code) {
|
|
|
|
|
taosThreadRwlockUnlock(&pTsdb->rwLock);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
}
|
|
|
|
|
pTsdb->imem = NULL;
|
|
|
|
|
taosThreadRwlockUnlock(&pTsdb->rwLock);
|
2023-05-11 10:16:55 +00:00
|
|
|
tsdbUnrefMemTable(pMemTable, NULL, true);
|
2023-04-10 09:52:51 +00:00
|
|
|
|
2023-05-24 03:52:38 +00:00
|
|
|
// TODO: make this call async
|
|
|
|
|
code = tsdbMerge(pTsdb);
|
|
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
2023-05-24 01:30:14 +00:00
|
|
|
|
2023-04-10 09:52:51 +00:00
|
|
|
_exit:
|
|
|
|
|
if (code) {
|
2023-05-11 10:16:55 +00:00
|
|
|
tsdbError("vgId:%d, %s failed at line %d since %s", vid, __func__, lino, tstrerror(code));
|
2023-04-10 09:52:51 +00:00
|
|
|
} else {
|
2023-05-11 10:16:55 +00:00
|
|
|
tsdbInfo("vgId:%d %s done", vid, __func__);
|
2023-04-10 09:52:51 +00:00
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int32_t tsdbCommitAbort(STsdb *pTsdb) {
|
|
|
|
|
int32_t code = 0;
|
|
|
|
|
int32_t lino = 0;
|
2023-05-11 10:16:55 +00:00
|
|
|
int32_t vid = TD_VID(pTsdb->pVnode);
|
|
|
|
|
|
|
|
|
|
if (pTsdb->imem == NULL) goto _exit;
|
2023-04-10 09:52:51 +00:00
|
|
|
|
2023-05-09 08:46:23 +00:00
|
|
|
code = tsdbFSEditAbort(pTsdb->pFS);
|
2023-04-10 09:52:51 +00:00
|
|
|
TSDB_CHECK_CODE(code, lino, _exit);
|
|
|
|
|
|
|
|
|
|
_exit:
|
|
|
|
|
if (code) {
|
2023-05-11 10:16:55 +00:00
|
|
|
tsdbError("vgId:%d, %s failed at line %d since %s", vid, __func__, lino, tstrerror(code));
|
2023-04-10 09:52:51 +00:00
|
|
|
} else {
|
2023-05-11 10:16:55 +00:00
|
|
|
tsdbInfo("vgId:%d %s done", vid, __func__);
|
2023-04-10 09:52:51 +00:00
|
|
|
}
|
|
|
|
|
return code;
|
|
|
|
|
}
|