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

1997 lines
64 KiB
C
Raw Normal View History

2023-03-27 11:12:25 +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-04-06 06:27:18 +00:00
*/
#include "tsdbDataFileRW.h"
2025-02-07 10:52:53 +00:00
#include "meta.h"
2023-04-23 06:02:43 +00:00
2023-04-23 10:00:02 +00:00
// SDataFileReader =============================================
struct SDataFileReader {
2023-06-08 09:31:35 +00:00
SDataFileReaderConfig config[1];
2023-05-29 07:58:54 +00:00
2024-03-07 02:42:20 +00:00
SBuffer local[10];
2024-03-03 08:01:12 +00:00
SBuffer *buffers;
2023-06-02 09:36:14 +00:00
2023-05-29 07:58:54 +00:00
struct {
2023-06-08 09:31:35 +00:00
bool headFooterLoaded;
bool tombFooterLoaded;
bool brinBlkLoaded;
bool tombBlkLoaded;
2023-05-29 07:58:54 +00:00
} ctx[1];
2023-06-02 09:36:14 +00:00
STsdbFD *fd[TSDB_FTYPE_MAX];
2023-06-08 09:31:35 +00:00
SHeadFooter headFooter[1];
STombFooter tombFooter[1];
TBrinBlkArray brinBlkArray[1];
TTombBlkArray tombBlkArray[1];
2023-04-23 10:00:02 +00:00
};
2023-04-06 06:27:18 +00:00
2023-06-06 08:17:29 +00:00
static int32_t tsdbDataFileReadHeadFooter(SDataFileReader *reader) {
2024-07-17 14:09:09 +00:00
if (reader->ctx->headFooterLoaded) {
return 0;
}
2023-06-02 09:36:14 +00:00
int32_t code = 0;
int32_t lino = 0;
2023-06-08 09:31:35 +00:00
int32_t ftype = TSDB_FTYPE_HEAD;
if (reader->fd[ftype]) {
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-04-24 10:04:10 +00:00
#if 1
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(SHeadFooter),
2025-12-05 05:26:28 +00:00
(uint8_t *)reader->headFooter, sizeof(SHeadFooter), 0, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2024-04-24 10:04:10 +00:00
#else
int64_t size = reader->config->files[ftype].file.size;
for (; size > TSDB_FHDR_SIZE; size--) {
code = tsdbReadFile(reader->fd[ftype], size - sizeof(SHeadFooter), (uint8_t *)reader->headFooter,
sizeof(SHeadFooter), 0, encryptAlgorithm, encryptKey);
if (code) continue;
if (reader->headFooter->brinBlkPtr->offset + reader->headFooter->brinBlkPtr->size + sizeof(SHeadFooter) == size) {
break;
}
}
if (size <= TSDB_FHDR_SIZE) {
TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
}
#endif
2023-06-08 09:31:35 +00:00
}
2023-06-06 08:17:29 +00:00
reader->ctx->headFooterLoaded = true;
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-06 08:17:29 +00:00
}
return code;
}
static int32_t tsdbDataFileReadTombFooter(SDataFileReader *reader) {
2024-07-17 14:09:09 +00:00
if (reader->ctx->tombFooterLoaded) {
return 0;
}
2023-06-06 08:17:29 +00:00
int32_t code = 0;
int32_t lino = 0;
2023-06-08 09:31:35 +00:00
int32_t ftype = TSDB_FTYPE_TOMB;
if (reader->fd[ftype]) {
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[ftype], reader->config->files[ftype].file.size - sizeof(STombFooter),
2025-12-05 05:26:28 +00:00
(uint8_t *)reader->tombFooter, sizeof(STombFooter), 0, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-06-08 09:31:35 +00:00
}
2023-06-06 08:17:29 +00:00
reader->ctx->tombFooterLoaded = true;
2023-06-02 09:36:14 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-02 09:36:14 +00:00
}
return code;
}
2023-05-29 07:58:54 +00:00
int32_t tsdbDataFileReaderOpen(const char *fname[], const SDataFileReaderConfig *config, SDataFileReader **reader) {
int32_t code = 0;
2023-06-02 09:36:14 +00:00
int32_t lino = 0;
2023-05-29 07:58:54 +00:00
2024-07-17 14:09:09 +00:00
if ((*reader = taosMemoryCalloc(1, sizeof(**reader))) == NULL) {
TAOS_CHECK_GOTO(terrno, &lino, _exit);
2023-05-29 07:58:54 +00:00
}
2024-03-03 08:01:12 +00:00
for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); i++) {
tBufferInit(reader[0]->local + i);
}
2023-05-29 07:58:54 +00:00
reader[0]->config[0] = config[0];
2024-03-03 08:01:12 +00:00
reader[0]->buffers = config->buffers;
if (reader[0]->buffers == NULL) {
reader[0]->buffers = reader[0]->local;
2023-06-02 09:36:14 +00:00
}
if (fname) {
for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
2023-06-06 08:17:29 +00:00
if (fname[i]) {
int32_t lcn = config->files[i].file.lcn;
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbOpenFile(fname[i], config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
2023-06-06 08:17:29 +00:00
}
2023-05-29 07:58:54 +00:00
}
2023-06-02 09:36:14 +00:00
} else {
for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
2023-06-06 08:17:29 +00:00
if (config->files[i].exist) {
char fname1[TSDB_FILENAME_LEN];
2024-09-10 02:05:38 +00:00
tsdbTFileName(config->tsdb, &config->files[i].file, fname1);
int32_t lcn = config->files[i].file.lcn;
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbOpenFile(fname1, config->tsdb, TD_FILE_READ, &reader[0]->fd[i], lcn), &lino, _exit);
2023-06-06 08:17:29 +00:00
}
2023-06-02 09:36:14 +00:00
}
}
2023-05-29 07:58:54 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-05-29 07:58:54 +00:00
}
return code;
}
void tsdbDataFileReaderClose(SDataFileReader **reader) {
2024-07-17 14:09:09 +00:00
if (reader[0] == NULL) {
return;
2024-07-17 14:09:09 +00:00
}
2023-06-02 09:36:14 +00:00
2023-06-08 09:31:35 +00:00
TARRAY2_DESTROY(reader[0]->tombBlkArray, NULL);
TARRAY2_DESTROY(reader[0]->brinBlkArray, NULL);
2023-06-02 09:36:14 +00:00
for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
2023-06-08 09:31:35 +00:00
if (reader[0]->fd[i]) {
tsdbCloseFile(&reader[0]->fd[i]);
}
2023-06-02 09:36:14 +00:00
}
2024-03-03 08:01:12 +00:00
for (int32_t i = 0; i < ARRAY_SIZE(reader[0]->local); ++i) {
tBufferDestroy(reader[0]->local + i);
2023-06-02 09:36:14 +00:00
}
2023-06-08 09:31:35 +00:00
2023-06-02 09:36:14 +00:00
taosMemoryFree(reader[0]);
reader[0] = NULL;
2023-05-29 07:58:54 +00:00
}
2023-06-08 09:31:35 +00:00
int32_t tsdbDataFileReadBrinBlk(SDataFileReader *reader, const TBrinBlkArray **brinBlkArray) {
int32_t code = 0;
int32_t lino = 0;
2024-07-17 14:09:09 +00:00
void *data = NULL;
2023-06-08 09:31:35 +00:00
if (!reader->ctx->brinBlkLoaded) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileReadHeadFooter(reader), &lino, _exit);
2023-06-08 09:31:35 +00:00
if (reader->headFooter->brinBlkPtr->size > 0) {
2024-07-17 14:09:09 +00:00
data = taosMemoryMalloc(reader->headFooter->brinBlkPtr->size);
2023-06-08 09:31:35 +00:00
if (data == NULL) {
2024-09-19 11:51:51 +00:00
TAOS_CHECK_GOTO(terrno, &lino, _exit);
2023-06-08 09:31:35 +00:00
}
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[TSDB_FTYPE_HEAD], reader->headFooter->brinBlkPtr->offset, data,
2025-12-05 05:26:28 +00:00
reader->headFooter->brinBlkPtr->size, 0, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-06-08 09:31:35 +00:00
int32_t size = reader->headFooter->brinBlkPtr->size / sizeof(SBrinBlk);
TARRAY2_INIT_EX(reader->brinBlkArray, size, size, data);
} else {
TARRAY2_INIT(reader->brinBlkArray);
}
reader->ctx->brinBlkLoaded = true;
}
brinBlkArray[0] = reader->brinBlkArray;
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
taosMemoryFree(data);
2023-06-08 09:31:35 +00:00
}
return code;
}
int32_t tsdbDataFileReadBrinBlock(SDataFileReader *reader, const SBrinBlk *brinBlk, SBrinBlock *brinBlock) {
int32_t code = 0;
int32_t lino = 0;
2024-03-07 02:42:20 +00:00
SBuffer *buffer = reader->buffers + 0;
SBuffer *assist = reader->buffers + 1;
2023-06-08 09:31:35 +00:00
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-03-03 08:01:12 +00:00
// load data
2024-03-07 02:42:20 +00:00
tBufferClear(buffer);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_HEAD], brinBlk->dp->offset, brinBlk->dp->size, buffer, 0,
2025-12-05 05:26:28 +00:00
pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-06-08 09:31:35 +00:00
2024-03-03 08:01:12 +00:00
// decode brin block
2024-03-07 02:42:20 +00:00
SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
tBrinBlockClear(brinBlock);
2024-03-03 08:01:12 +00:00
brinBlock->numOfPKs = brinBlk->numOfPKs;
brinBlock->numOfRecords = brinBlk->numRec;
2024-03-04 11:25:20 +00:00
for (int32_t i = 0; i < 10; i++) { // int64_t
2024-03-29 08:40:19 +00:00
2024-03-03 08:01:12 +00:00
SCompressInfo cinfo = {
.cmprAlg = brinBlk->cmprAlg,
.dataType = TSDB_DATA_TYPE_BIGINT,
.compressedSize = brinBlk->size[i],
.originalSize = brinBlk->numRec * sizeof(int64_t),
};
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
2024-03-03 08:01:12 +00:00
br.offset += brinBlk->size[i];
}
2023-06-08 09:31:35 +00:00
2024-03-04 11:25:20 +00:00
for (int32_t i = 10; i < 15; i++) { // int32_t
2024-03-03 08:01:12 +00:00
SCompressInfo cinfo = {
.cmprAlg = brinBlk->cmprAlg,
.dataType = TSDB_DATA_TYPE_INT,
.compressedSize = brinBlk->size[i],
.originalSize = brinBlk->numRec * sizeof(int32_t),
};
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, brinBlock->buffers + i, assist), &lino, _exit);
2024-03-03 08:01:12 +00:00
br.offset += brinBlk->size[i];
2023-06-08 09:31:35 +00:00
}
2024-03-03 08:01:12 +00:00
// primary keys
if (brinBlk->numOfPKs > 0) { // decode the primary keys
SValueColumnCompressInfo firstInfos[TD_MAX_PK_COLS];
SValueColumnCompressInfo lastInfos[TD_MAX_PK_COLS];
2023-06-08 09:31:35 +00:00
2024-03-03 08:01:12 +00:00
for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, firstInfos + i), &lino, _exit);
2024-03-03 08:01:12 +00:00
}
for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tValueColumnCompressInfoDecode(&br, lastInfos + i), &lino, _exit);
2024-03-03 08:01:12 +00:00
}
for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
SValueColumnCompressInfo *info = firstInfos + i;
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), info, brinBlock->firstKeyPKs + i, assist), &lino, _exit);
2024-03-07 02:42:20 +00:00
br.offset += (info->offsetCompressedSize + info->dataCompressedSize);
2024-03-03 08:01:12 +00:00
}
2023-06-08 09:31:35 +00:00
2024-03-03 08:01:12 +00:00
for (int32_t i = 0; i < brinBlk->numOfPKs; i++) {
SValueColumnCompressInfo *info = lastInfos + i;
2023-06-08 09:31:35 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tValueColumnDecompress(BR_PTR(&br), info, brinBlock->lastKeyPKs + i, assist), &lino, _exit);
2024-03-07 02:42:20 +00:00
br.offset += (info->offsetCompressedSize + info->dataCompressedSize);
2024-03-03 08:01:12 +00:00
}
2023-06-08 09:31:35 +00:00
}
2024-08-20 06:47:05 +00:00
if (br.offset != br.buffer->size) {
tsdbError("vgId:%d %s failed at %s:%d since brin block size mismatch, expected: %u, actual: %u, fname:%s",
TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino, br.buffer->size, br.offset,
reader->fd[TSDB_FTYPE_HEAD]->path);
2024-08-20 06:47:05 +00:00
TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
}
2023-06-08 09:31:35 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-08 09:31:35 +00:00
}
return code;
}
2024-03-04 11:25:20 +00:00
extern int32_t tBlockDataDecompress(SBufferReader *br, SBlockData *blockData, SBuffer *assist);
2023-06-08 09:31:35 +00:00
int32_t tsdbDataFileReadBlockData(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData) {
int32_t code = 0;
int32_t lino = 0;
int32_t fid = reader->config->files[TSDB_FTYPE_DATA].file.fid;
2023-06-08 09:31:35 +00:00
2024-03-07 02:42:20 +00:00
SBuffer *buffer = reader->buffers + 0;
SBuffer *assist = reader->buffers + 1;
2023-06-08 09:31:35 +00:00
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-03-03 08:01:12 +00:00
// load data
2024-03-07 02:42:20 +00:00
tBufferClear(buffer);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, record->blockSize, buffer, 0,
2025-12-05 05:26:28 +00:00
pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-06-08 09:31:35 +00:00
2024-03-03 08:01:12 +00:00
// decompress
2024-03-07 02:42:20 +00:00
SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tBlockDataDecompress(&br, bData, assist), &lino, _exit);
2024-08-20 06:47:05 +00:00
if (br.offset != buffer->size) {
tsdbError("vgId:%d %s failed at %s:%d since block data size mismatch, expected: %u, actual: %u, fname:%s",
TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, buffer->size, br.offset,
reader->fd[TSDB_FTYPE_DATA]->path);
2024-08-20 06:47:05 +00:00
TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
}
2023-06-08 09:31:35 +00:00
_exit:
if (code) {
tsdbError("vgId:%d %s fid %d failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, fid,
__FILE__, lino, tstrerror(code));
2023-06-08 09:31:35 +00:00
}
return code;
}
2023-06-09 09:52:57 +00:00
int32_t tsdbDataFileReadBlockDataByColumn(SDataFileReader *reader, const SBrinRecord *record, SBlockData *bData,
STSchema *pTSchema, int16_t cids[], int32_t ncid) {
2023-06-08 09:31:35 +00:00
int32_t code = 0;
int32_t lino = 0;
int32_t fid = reader->config->files[TSDB_FTYPE_DATA].file.fid;
2023-06-08 09:31:35 +00:00
2024-01-26 09:14:00 +00:00
SDiskDataHdr hdr;
2024-03-07 05:14:56 +00:00
SBuffer *buffer0 = reader->buffers + 0;
SBuffer *buffer1 = reader->buffers + 1;
SBuffer *assist = reader->buffers + 2;
2023-06-09 09:52:57 +00:00
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-03-04 11:25:20 +00:00
// load key part
2024-03-07 05:14:56 +00:00
tBufferClear(buffer0);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset, record->blockKeySize, buffer0,
2025-12-05 05:26:28 +00:00
0, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-06-09 09:52:57 +00:00
2024-03-04 11:25:20 +00:00
// SDiskDataHdr
2024-03-07 05:14:56 +00:00
SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tGetDiskDataHdr(&br, &hdr), &lino, _exit);
2023-06-09 09:52:57 +00:00
2024-08-20 06:47:05 +00:00
if (hdr.delimiter != TSDB_FILE_DLMT) {
tsdbError("vgId:%d %s failed at %s:%d since disk data header delimiter is invalid, fname:%s",
TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, reader->fd[TSDB_FTYPE_DATA]->path);
2024-08-20 06:47:05 +00:00
TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
}
2023-06-09 09:52:57 +00:00
2024-01-26 09:14:00 +00:00
tBlockDataReset(bData);
bData->suid = hdr.suid;
bData->uid = hdr.uid;
bData->nRow = hdr.nRow;
2023-06-09 09:52:57 +00:00
2024-03-04 11:25:20 +00:00
// Key part
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tBlockDataDecompressKeyPart(&hdr, &br, bData, assist), &lino, _exit);
2024-08-20 06:47:05 +00:00
if (br.offset != buffer0->size) {
tsdbError("vgId:%d %s failed at %s:%d since key part size mismatch, expected: %u, actual: %u, fname:%s",
TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, buffer0->size, br.offset,
reader->fd[TSDB_FTYPE_DATA]->path);
2024-08-20 06:47:05 +00:00
TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
}
2023-06-09 09:52:57 +00:00
int extraColIdx = -1;
2024-03-07 05:14:56 +00:00
for (int i = 0; i < ncid; i++) {
if (tBlockDataGetColData(bData, cids[i]) == NULL) {
extraColIdx = i;
2024-03-07 05:14:56 +00:00
break;
2023-06-09 09:52:57 +00:00
}
2024-03-07 05:14:56 +00:00
}
2023-06-09 09:52:57 +00:00
if (extraColIdx < 0) {
2024-03-04 11:25:20 +00:00
goto _exit;
2024-01-26 09:14:00 +00:00
}
2024-07-17 14:09:09 +00:00
2024-03-04 11:25:20 +00:00
// load SBlockCol part
2024-03-07 05:14:56 +00:00
tBufferClear(buffer0);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA], record->blockOffset + record->blockKeySize,
2025-12-05 05:26:28 +00:00
hdr.szBlkCol, buffer0, 0, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
// calc szHint
int64_t szHint = 0;
int extraCols = 1;
for (int i = extraColIdx + 1; i < ncid; ++i) {
if (tBlockDataGetColData(bData, cids[i]) == NULL) {
++extraCols;
break;
}
}
2023-06-09 09:52:57 +00:00
if (extraCols >= 2) {
br = BUFFER_READER_INITIALIZER(0, buffer0);
2023-06-09 09:52:57 +00:00
SBlockCol blockCol = {.cid = 0};
for (int32_t i = extraColIdx; i < ncid; ++i) {
int16_t extraColCid = cids[i];
2023-06-09 09:52:57 +00:00
while (extraColCid > blockCol.cid) {
if (br.offset >= buffer0->size) {
blockCol.cid = INT16_MAX;
break;
}
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
}
if (extraColCid == blockCol.cid || blockCol.cid == INT16_MAX) {
extraColIdx = i;
break;
}
}
if (blockCol.cid > 0 && blockCol.cid < INT16_MAX /*&& blockCol->flag == HAS_VALUE*/) {
int64_t offset = blockCol.offset;
SBlockCol lastNonNoneBlockCol = {.cid = 0};
for (int32_t i = extraColIdx; i < ncid; ++i) {
int16_t extraColCid = cids[i];
while (extraColCid > blockCol.cid) {
if (br.offset >= buffer0->size) {
blockCol.cid = INT16_MAX;
break;
}
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
}
if (extraColCid == blockCol.cid) {
lastNonNoneBlockCol = blockCol;
continue;
}
if (blockCol.cid == INT16_MAX) {
break;
}
}
if (lastNonNoneBlockCol.cid > 0) {
szHint = lastNonNoneBlockCol.offset + lastNonNoneBlockCol.szBitmap + lastNonNoneBlockCol.szOffset +
lastNonNoneBlockCol.szValue - offset;
}
}
}
2024-03-04 11:25:20 +00:00
// load each column
SBlockCol blockCol = {
.cid = 0,
};
bool firstRead = true;
2024-03-07 05:14:56 +00:00
br = BUFFER_READER_INITIALIZER(0, buffer0);
2024-01-26 09:14:00 +00:00
for (int32_t i = 0; i < ncid; i++) {
2024-03-04 11:25:20 +00:00
int16_t cid = cids[i];
2024-03-07 05:14:56 +00:00
if (tBlockDataGetColData(bData, cid)) { // already loaded
2024-03-04 11:25:20 +00:00
continue;
}
2024-03-04 11:25:20 +00:00
while (cid > blockCol.cid) {
2024-03-07 05:14:56 +00:00
if (br.offset >= buffer0->size) {
2024-01-26 09:14:00 +00:00
blockCol.cid = INT16_MAX;
break;
2023-06-09 09:52:57 +00:00
}
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tGetBlockCol(&br, &blockCol, hdr.fmtVer, hdr.cmprAlg), &lino, _exit);
2024-01-26 09:14:00 +00:00
}
2023-06-09 09:52:57 +00:00
2024-03-04 11:25:20 +00:00
if (cid < blockCol.cid) {
2024-03-07 05:14:56 +00:00
const STColumn *tcol = tTSchemaSearchColumn(pTSchema, cid);
2024-08-14 05:59:33 +00:00
TSDB_CHECK_NULL(tcol, code, lino, _exit, TSDB_CODE_TDB_INVALID_TABLE_SCHEMA_VER);
2024-03-07 05:14:56 +00:00
SBlockCol none = {
.cid = cid,
.type = tcol->type,
.cflag = tcol->flags,
.flag = HAS_NONE,
.szOrigin = 0,
.szBitmap = 0,
.szOffset = 0,
.szValue = 0,
.offset = 0,
};
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &none, &br, bData, assist), &lino, _exit);
2024-03-04 11:25:20 +00:00
} else if (cid == blockCol.cid) {
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-07-17 14:09:09 +00:00
// load from file
2024-03-07 05:14:56 +00:00
tBufferClear(buffer1);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_DATA],
record->blockOffset + record->blockKeySize + hdr.szBlkCol + blockCol.offset,
blockCol.szBitmap + blockCol.szOffset + blockCol.szValue, buffer1,
2025-12-05 05:26:28 +00:00
firstRead ? szHint : 0, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-06-09 09:52:57 +00:00
firstRead = false;
2023-06-09 09:52:57 +00:00
2024-03-04 11:25:20 +00:00
// decode the buffer
2024-03-07 05:14:56 +00:00
SBufferReader br1 = BUFFER_READER_INITIALIZER(0, buffer1);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tBlockDataDecompressColData(&hdr, &blockCol, &br1, bData, assist), &lino, _exit);
2023-06-09 09:52:57 +00:00
}
}
2023-06-08 09:31:35 +00:00
_exit:
if (code) {
tsdbError("vgId:%d %s fid:%d failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, fid,
__FILE__, lino, tstrerror(code));
2023-06-08 09:31:35 +00:00
}
return code;
}
2023-06-09 09:52:57 +00:00
int32_t tsdbDataFileReadBlockSma(SDataFileReader *reader, const SBrinRecord *record,
TColumnDataAggArray *columnDataAggArray) {
2024-03-07 06:02:13 +00:00
int32_t code = 0;
int32_t lino = 0;
SBuffer *buffer = reader->buffers + 0;
2023-06-08 09:31:35 +00:00
2023-06-09 09:52:57 +00:00
TARRAY2_CLEAR(columnDataAggArray, NULL);
if (record->smaSize > 0) {
2024-03-07 06:02:13 +00:00
tBufferClear(buffer);
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
TAOS_CHECK_GOTO(
tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_SMA], record->smaOffset, record->smaSize, buffer, 0, pEncryptData),
&lino, _exit);
2023-06-09 09:52:57 +00:00
// decode sma data
2024-03-07 06:02:13 +00:00
SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer);
2024-03-04 11:25:20 +00:00
while (br.offset < record->smaSize) {
2023-06-09 09:52:57 +00:00
SColumnDataAgg sma[1];
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tGetColumnDataAgg(&br, sma), &lino, _exit);
TAOS_CHECK_GOTO(TARRAY2_APPEND_PTR(columnDataAggArray, sma), &lino, _exit);
2023-06-09 09:52:57 +00:00
}
2024-08-20 06:47:05 +00:00
if (br.offset != record->smaSize) {
tsdbError("vgId:%d %s failed at %s:%d since sma data size mismatch, expected: %u, actual: %u, fname:%s",
TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, __LINE__, record->smaSize, br.offset,
reader->fd[TSDB_FTYPE_SMA]->path);
2024-08-20 06:47:05 +00:00
TSDB_CHECK_CODE(code = TSDB_CODE_FILE_CORRUPTED, lino, _exit);
}
2023-06-09 09:52:57 +00:00
}
2023-06-08 09:31:35 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-08 09:31:35 +00:00
}
return code;
}
2023-06-05 10:29:05 +00:00
int32_t tsdbDataFileReadTombBlk(SDataFileReader *reader, const TTombBlkArray **tombBlkArray) {
int32_t code = 0;
int32_t lino = 0;
2024-07-17 14:09:09 +00:00
void *data = NULL;
2023-06-05 10:29:05 +00:00
2023-06-08 09:31:35 +00:00
if (!reader->ctx->tombBlkLoaded) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileReadTombFooter(reader), &lino, _exit);
2023-06-05 14:35:06 +00:00
2023-06-08 09:31:35 +00:00
if (reader->tombFooter->tombBlkPtr->size > 0) {
2024-07-17 14:09:09 +00:00
if ((data = taosMemoryMalloc(reader->tombFooter->tombBlkPtr->size)) == NULL) {
2024-09-19 11:51:51 +00:00
TAOS_CHECK_GOTO(terrno, &lino, _exit);
2023-06-08 09:31:35 +00:00
}
2023-06-05 14:35:06 +00:00
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbReadFile(reader->fd[TSDB_FTYPE_TOMB], reader->tombFooter->tombBlkPtr->offset, data,
2025-12-05 05:26:28 +00:00
reader->tombFooter->tombBlkPtr->size, 0, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-06-08 09:31:35 +00:00
int32_t size = reader->tombFooter->tombBlkPtr->size / sizeof(STombBlk);
TARRAY2_INIT_EX(reader->tombBlkArray, size, size, data);
} else {
TARRAY2_INIT(reader->tombBlkArray);
2023-06-05 14:35:06 +00:00
}
reader->ctx->tombBlkLoaded = true;
}
tombBlkArray[0] = reader->tombBlkArray;
2023-06-05 10:29:05 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
taosMemoryFree(data);
2023-06-05 10:29:05 +00:00
}
return code;
}
int32_t tsdbDataFileReadTombBlock(SDataFileReader *reader, const STombBlk *tombBlk, STombBlock *tData) {
int32_t code = 0;
int32_t lino = 0;
2024-03-07 06:02:13 +00:00
SBuffer *buffer0 = reader->buffers + 0;
SBuffer *assist = reader->buffers + 1;
2023-06-05 14:35:06 +00:00
2024-03-07 06:02:13 +00:00
tBufferClear(buffer0);
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(reader->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbReadFileToBuffer(reader->fd[TSDB_FTYPE_TOMB], tombBlk->dp->offset, tombBlk->dp->size, buffer0, 0,
2025-12-05 05:26:28 +00:00
pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-06-05 14:35:06 +00:00
2024-03-04 11:25:20 +00:00
int32_t size = 0;
2024-03-07 06:02:13 +00:00
SBufferReader br = BUFFER_READER_INITIALIZER(0, buffer0);
2023-06-08 09:31:35 +00:00
tTombBlockClear(tData);
2024-03-04 11:25:20 +00:00
tData->numOfRecords = tombBlk->numRec;
for (int32_t i = 0; i < ARRAY_SIZE(tData->buffers); ++i) {
SCompressInfo cinfo = {
.cmprAlg = tombBlk->cmprAlg,
.dataType = TSDB_DATA_TYPE_BIGINT,
.originalSize = tombBlk->numRec * sizeof(int64_t),
.compressedSize = tombBlk->size[i],
};
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tDecompressDataToBuffer(BR_PTR(&br), &cinfo, tData->buffers + i, assist), &lino, _exit);
2024-03-04 11:25:20 +00:00
br.offset += tombBlk->size[i];
2023-06-05 14:35:06 +00:00
}
2023-06-05 10:29:05 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(reader->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-05 10:29:05 +00:00
}
return code;
}
2023-04-23 10:00:02 +00:00
// SDataFileWriter =============================================
struct SDataFileWriter {
2023-05-28 15:33:03 +00:00
SDataFileWriterConfig config[1];
2023-05-29 07:58:54 +00:00
2023-06-01 10:56:40 +00:00
SSkmInfo skmTb[1];
SSkmInfo skmRow[1];
2024-03-07 06:02:13 +00:00
SBuffer local[10];
2024-02-28 02:58:42 +00:00
SBuffer *buffers;
2023-06-01 10:56:40 +00:00
2023-05-28 15:33:03 +00:00
struct {
2023-06-06 08:17:29 +00:00
bool opened;
SDataFileReader *reader;
2023-06-09 01:23:56 +00:00
2023-06-06 08:17:29 +00:00
// for ts data
2023-06-09 01:23:56 +00:00
TABLEID tbid[1];
bool tbHasOldData;
const TBrinBlkArray *brinBlkArray;
int32_t brinBlkArrayIdx;
SBrinBlock brinBlock[1];
int32_t brinBlockIdx;
SBlockData blockData[1];
int32_t blockDataIdx;
2023-06-06 08:17:29 +00:00
// for tomb data
bool hasOldTomb;
const TTombBlkArray *tombBlkArray;
int32_t tombBlkArrayIdx;
2023-06-09 01:23:56 +00:00
STombBlock tombBlock[1];
int32_t tombBlockIdx;
2023-08-31 11:13:58 +00:00
// range
SVersionRange range;
SVersionRange tombRange;
2023-05-28 15:33:03 +00:00
} ctx[1];
2023-05-29 07:58:54 +00:00
2023-06-02 09:36:14 +00:00
STFile files[TSDB_FTYPE_MAX];
2023-06-01 10:56:40 +00:00
STsdbFD *fd[TSDB_FTYPE_MAX];
2023-06-09 01:23:56 +00:00
SHeadFooter headFooter[1];
STombFooter tombFooter[1];
TBrinBlkArray brinBlkArray[1];
SBrinBlock brinBlock[1];
SBlockData blockData[1];
TTombBlkArray tombBlkArray[1];
STombBlock tombBlock[1];
2023-05-26 07:57:31 +00:00
};
2023-05-28 15:33:03 +00:00
static int32_t tsdbDataFileWriterCloseAbort(SDataFileWriter *writer) {
2024-08-20 06:47:05 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, __LINE__,
"not implemented");
2023-05-28 15:33:03 +00:00
return 0;
}
2023-06-01 10:56:40 +00:00
static void tsdbDataFileWriterDoClose(SDataFileWriter *writer) {
2023-06-06 08:17:29 +00:00
if (writer->ctx->reader) {
tsdbDataFileReaderClose(&writer->ctx->reader);
2023-06-06 08:17:29 +00:00
}
2023-06-09 01:23:56 +00:00
tTombBlockDestroy(writer->tombBlock);
2023-06-08 02:31:19 +00:00
TARRAY2_DESTROY(writer->tombBlkArray, NULL);
2023-06-09 08:07:06 +00:00
tBlockDataDestroy(writer->blockData);
tBrinBlockDestroy(writer->brinBlock);
2023-06-09 08:07:06 +00:00
TARRAY2_DESTROY(writer->brinBlkArray, NULL);
2023-06-09 01:23:56 +00:00
tTombBlockDestroy(writer->ctx->tombBlock);
2023-06-09 08:07:06 +00:00
tBlockDataDestroy(writer->ctx->blockData);
tBrinBlockDestroy(writer->ctx->brinBlock);
2023-06-06 08:17:29 +00:00
2024-03-03 08:01:12 +00:00
for (int32_t i = 0; i < ARRAY_SIZE(writer->local); ++i) {
tBufferDestroy(writer->local + i);
2023-06-06 08:17:29 +00:00
}
tDestroyTSchema(writer->skmRow->pTSchema);
tDestroyTSchema(writer->skmTb->pTSchema);
2023-05-28 15:33:03 +00:00
}
2023-06-01 10:56:40 +00:00
2023-06-02 09:36:14 +00:00
static int32_t tsdbDataFileWriterDoOpenReader(SDataFileWriter *writer) {
int32_t code = 0;
int32_t lino = 0;
for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
if (writer->config->files[i].exist) {
SDataFileReaderConfig config[1] = {{
.tsdb = writer->config->tsdb,
.szPage = writer->config->szPage,
2024-03-03 08:01:12 +00:00
.buffers = writer->buffers,
2023-06-02 09:36:14 +00:00
}};
for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
config->files[i].exist = writer->config->files[i].exist;
2023-06-09 01:23:56 +00:00
if (config->files[i].exist) {
config->files[i].file = writer->config->files[i].file;
}
2023-06-02 09:36:14 +00:00
}
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileReaderOpen(NULL, config, &writer->ctx->reader), &lino, _exit);
2023-06-02 09:36:14 +00:00
break;
}
}
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-02 09:36:14 +00:00
}
return code;
}
2023-05-28 15:33:03 +00:00
static int32_t tsdbDataFileWriterDoOpen(SDataFileWriter *writer) {
2023-06-01 10:56:40 +00:00
int32_t code = 0;
int32_t lino = 0;
2023-06-09 01:23:56 +00:00
int32_t ftype;
2025-02-07 10:52:53 +00:00
SDiskID diskId = {0};
2023-06-01 10:56:40 +00:00
if (!writer->config->skmTb) writer->config->skmTb = writer->skmTb;
if (!writer->config->skmRow) writer->config->skmRow = writer->skmRow;
2024-03-03 08:01:12 +00:00
writer->buffers = writer->config->buffers;
if (writer->buffers == NULL) {
writer->buffers = writer->local;
}
2023-06-01 10:56:40 +00:00
// open reader
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpenReader(writer), &lino, _exit);
2023-06-01 10:56:40 +00:00
2023-06-02 09:36:14 +00:00
// .head
2023-06-06 08:17:29 +00:00
ftype = TSDB_FTYPE_HEAD;
2025-02-10 07:56:23 +00:00
code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
2025-02-07 10:52:53 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-06-06 08:17:29 +00:00
writer->files[ftype] = (STFile){
.type = ftype,
2025-02-07 10:52:53 +00:00
.did = diskId,
2023-06-02 09:36:14 +00:00
.fid = writer->config->fid,
.cid = writer->config->cid,
.size = 0,
.minVer = VERSION_MAX,
.maxVer = VERSION_MIN,
2023-06-02 09:36:14 +00:00
};
2023-06-01 10:56:40 +00:00
2023-06-02 09:36:14 +00:00
// .data
2023-06-06 08:17:29 +00:00
ftype = TSDB_FTYPE_DATA;
if (writer->config->files[ftype].exist) {
writer->files[ftype] = writer->config->files[ftype].file;
2023-06-02 09:36:14 +00:00
} else {
2025-02-10 07:56:23 +00:00
code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
2025-02-07 10:52:53 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-06-06 08:17:29 +00:00
writer->files[ftype] = (STFile){
.type = ftype,
2025-02-07 10:52:53 +00:00
.did = diskId,
2023-06-06 08:17:29 +00:00
.fid = writer->config->fid,
.cid = writer->config->cid,
.size = 0,
feat[ts-6107]: shared storage (#31552) * add API to use s3 as shared storage * support using local file system as shared storage * upload file to shared storage * support read, compact and drop * finish basic mnode & vnode msg processing * follower sync migration state * implement mnode transaction, and improve log * send migration progress msg to dnode to avoid deadlock * implement following migration * remove mcount * avoid redo migration on startup * avoid follower deadlock when leader is down * trigger migrate by timer, avoid compact after migration * comment out the usage of 'tcs' functions in stream * change config item prefix from s3 to ss * change db option prefix from s3 to ss * rename s3 data struct, function, file to ss * rename s3 macro to ss * update s3 sql to ss * rename remaining s3 items to ss * check ss configruation, improve s3 retry * grant object storage -> shared storage, check ssEnabled * fix memory leaks * update build options * omit sensitive information when dump config * fix backward compatibility issue * fix issues found in ci-checks * fix some failed test cases * avoid follower timeout and improve log * fix: follower timeout because migration status not updated * refuse migration if there's an in progress one * fix ss test case * remove garbage files and other minor improvement * fix failed test cases * update unit test * fix failed test case * fix failed test case * update document * update document and fix failed test cases * fix minor issues in code, test and document * check new commit after migration task is scheduled * fix several issus 1. migrate information cannot be dropped sometimes because progress response was put into read queue. 2. memory leak in rare cases 3. data corruption in rare cases 4. failed test case * add shared storage upgrade tool * fix compile error
2025-07-14 08:33:53 +00:00
.lcn = writer->config->lcn == 0 ? -1 : 0,
.minVer = VERSION_MAX,
.maxVer = VERSION_MIN,
2023-06-06 08:17:29 +00:00
};
2023-06-02 09:36:14 +00:00
}
2023-06-01 10:56:40 +00:00
2023-06-02 09:36:14 +00:00
// .sma
2023-06-06 08:17:29 +00:00
ftype = TSDB_FTYPE_SMA;
if (writer->config->files[ftype].exist) {
writer->files[ftype] = writer->config->files[ftype].file;
2023-06-02 09:36:14 +00:00
} else {
2025-02-10 07:56:23 +00:00
code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
2025-02-07 10:52:53 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-06-06 08:17:29 +00:00
writer->files[ftype] = (STFile){
.type = ftype,
2025-02-07 10:52:53 +00:00
.did = diskId,
2023-06-06 08:17:29 +00:00
.fid = writer->config->fid,
.cid = writer->config->cid,
.size = 0,
.minVer = VERSION_MAX,
.maxVer = VERSION_MIN,
2023-06-06 08:17:29 +00:00
};
2023-06-01 10:56:40 +00:00
}
2023-06-06 08:17:29 +00:00
// .tomb
ftype = TSDB_FTYPE_TOMB;
2025-02-10 07:56:23 +00:00
code = tsdbAllocateDisk(writer->config->tsdb, tsdbFTypeLabel(ftype), writer->config->expLevel, &diskId);
2025-02-07 10:52:53 +00:00
TSDB_CHECK_CODE(code, lino, _exit);
2023-06-06 08:17:29 +00:00
writer->files[ftype] = (STFile){
.type = ftype,
2025-02-07 10:52:53 +00:00
.did = diskId,
2023-06-02 09:36:14 +00:00
.fid = writer->config->fid,
.cid = writer->config->cid,
.size = 0,
.minVer = VERSION_MAX,
.maxVer = VERSION_MIN,
2023-06-02 09:36:14 +00:00
};
2023-06-01 10:56:40 +00:00
2023-08-31 11:13:58 +00:00
// range
writer->ctx->range = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
writer->ctx->tombRange = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
2023-08-31 11:13:58 +00:00
2023-06-06 08:17:29 +00:00
writer->ctx->opened = true;
2023-06-05 14:55:00 +00:00
2023-06-01 10:56:40 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-01 10:56:40 +00:00
}
return code;
2023-05-28 15:33:03 +00:00
}
2023-06-01 10:56:40 +00:00
void tsdbWriterUpdVerRange(SVersionRange *range, int64_t minVer, int64_t maxVer) {
2023-08-31 11:13:58 +00:00
range->minVer = TMIN(range->minVer, minVer);
range->maxVer = TMAX(range->maxVer, maxVer);
}
2024-03-29 08:40:19 +00:00
int32_t tsdbFileWriteBrinBlock(STsdbFD *fd, SBrinBlock *brinBlock, uint32_t cmprAlg, int64_t *fileSize,
2024-04-15 07:12:32 +00:00
TBrinBlkArray *brinBlkArray, SBuffer *buffers, SVersionRange *range,
2025-12-05 05:26:28 +00:00
SEncryptData *encryptData) {
2024-07-17 14:09:09 +00:00
if (brinBlock->numOfRecords == 0) {
return 0;
}
2023-06-09 07:10:54 +00:00
2024-03-07 06:02:13 +00:00
int32_t code;
SBuffer *buffer0 = buffers + 0;
SBuffer *buffer1 = buffers + 1;
SBuffer *assist = buffers + 2;
2023-06-09 07:10:54 +00:00
2024-02-23 06:44:46 +00:00
SBrinBlk brinBlk = {
2024-03-05 07:43:46 +00:00
.dp[0] =
{
.offset = *fileSize,
.size = 0,
},
2024-02-23 06:44:46 +00:00
.numRec = brinBlock->numOfRecords,
.numOfPKs = brinBlock->numOfPKs,
.cmprAlg = cmprAlg,
2023-06-09 07:10:54 +00:00
};
2024-03-05 07:43:46 +00:00
for (int i = 0; i < brinBlock->numOfRecords; i++) {
SBrinRecord record;
2023-06-09 07:10:54 +00:00
2024-09-11 02:56:40 +00:00
TAOS_CHECK_RETURN(tBrinBlockGet(brinBlock, i, &record));
2024-03-05 07:43:46 +00:00
if (i == 0) {
brinBlk.minTbid.suid = record.suid;
brinBlk.minTbid.uid = record.uid;
brinBlk.minVer = record.minVer;
brinBlk.maxVer = record.maxVer;
}
if (i == brinBlock->numOfRecords - 1) {
brinBlk.maxTbid.suid = record.suid;
brinBlk.maxTbid.uid = record.uid;
}
if (record.minVer < brinBlk.minVer) {
brinBlk.minVer = record.minVer;
2023-06-09 07:10:54 +00:00
}
2024-03-05 07:43:46 +00:00
if (record.maxVer > brinBlk.maxVer) {
brinBlk.maxVer = record.maxVer;
2023-06-09 07:10:54 +00:00
}
}
tsdbWriterUpdVerRange(range, brinBlk.minVer, brinBlk.maxVer);
2023-06-09 07:10:54 +00:00
// write to file
2024-03-07 06:02:13 +00:00
for (int32_t i = 0; i < 10; ++i) {
2024-02-23 06:44:46 +00:00
SCompressInfo info = {
.cmprAlg = cmprAlg,
2024-03-07 06:02:13 +00:00
.dataType = TSDB_DATA_TYPE_BIGINT,
.originalSize = brinBlock->buffers[i].size,
2024-02-23 06:44:46 +00:00
};
2023-06-09 07:10:54 +00:00
2024-03-07 06:02:13 +00:00
tBufferClear(buffer0);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_RETURN(tCompressDataToBuffer(brinBlock->buffers[i].data, &info, buffer0, assist));
2025-12-05 05:26:28 +00:00
TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
2024-03-07 06:02:13 +00:00
brinBlk.size[i] = info.compressedSize;
brinBlk.dp->size += info.compressedSize;
*fileSize += info.compressedSize;
}
for (int32_t i = 10; i < 15; ++i) {
SCompressInfo info = {
.cmprAlg = cmprAlg,
.dataType = TSDB_DATA_TYPE_INT,
.originalSize = brinBlock->buffers[i].size,
};
2023-06-09 07:10:54 +00:00
2024-03-07 06:02:13 +00:00
tBufferClear(buffer0);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_RETURN(tCompressDataToBuffer(brinBlock->buffers[i].data, &info, buffer0, assist));
2025-12-05 05:26:28 +00:00
TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
2024-02-23 06:44:46 +00:00
brinBlk.size[i] = info.compressedSize;
brinBlk.dp->size += info.compressedSize;
*fileSize += info.compressedSize;
2023-06-09 07:10:54 +00:00
}
2024-02-23 06:44:46 +00:00
// write primary keys to file
if (brinBlock->numOfPKs > 0) {
2024-03-07 06:02:13 +00:00
tBufferClear(buffer0);
tBufferClear(buffer1);
2023-06-09 07:10:54 +00:00
2024-03-05 08:13:14 +00:00
// encode
for (int i = 0; i < brinBlock->numOfPKs; i++) {
2024-03-07 06:02:13 +00:00
SValueColumnCompressInfo info = {.cmprAlg = cmprAlg};
2024-07-17 14:09:09 +00:00
TAOS_CHECK_RETURN(tValueColumnCompress(&brinBlock->firstKeyPKs[i], &info, buffer1, assist));
TAOS_CHECK_RETURN(tValueColumnCompressInfoEncode(&info, buffer0));
2024-02-23 06:44:46 +00:00
}
2024-03-05 08:13:14 +00:00
for (int i = 0; i < brinBlock->numOfPKs; i++) {
2024-03-07 06:02:13 +00:00
SValueColumnCompressInfo info = {.cmprAlg = cmprAlg};
2024-07-17 14:09:09 +00:00
TAOS_CHECK_RETURN(tValueColumnCompress(&brinBlock->lastKeyPKs[i], &info, buffer1, assist));
TAOS_CHECK_RETURN(tValueColumnCompressInfoEncode(&info, buffer0));
2024-02-23 06:44:46 +00:00
}
// write to file
2025-12-05 05:26:28 +00:00
TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
2024-03-07 06:02:13 +00:00
*fileSize += buffer0->size;
brinBlk.dp->size += buffer0->size;
2025-12-05 05:26:28 +00:00
TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer1->data, buffer1->size, encryptData));
2024-03-07 06:02:13 +00:00
*fileSize += buffer1->size;
brinBlk.dp->size += buffer1->size;
2023-07-05 09:43:15 +00:00
}
2023-06-09 07:10:54 +00:00
// append to brinBlkArray
2024-07-17 14:09:09 +00:00
TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(brinBlkArray, &brinBlk));
2023-06-09 07:10:54 +00:00
tBrinBlockClear(brinBlock);
2023-06-25 07:09:15 +00:00
return 0;
}
static int32_t tsdbDataFileWriteBrinBlock(SDataFileWriter *writer) {
2024-07-17 14:09:09 +00:00
if (writer->brinBlock->numOfRecords == 0) {
return 0;
}
2023-06-25 07:09:15 +00:00
int32_t code = 0;
int32_t lino = 0;
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-03-29 02:48:49 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbFileWriteBrinBlock(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlock, writer->config->cmprAlg,
&writer->files[TSDB_FTYPE_HEAD].size, writer->brinBlkArray, writer->buffers,
2025-12-05 05:26:28 +00:00
&writer->ctx->range, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-06-09 07:10:54 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-09 07:10:54 +00:00
}
return code;
}
static int32_t tsdbDataFileWriteBrinRecord(SDataFileWriter *writer, const SBrinRecord *record) {
int32_t code = 0;
int32_t lino = 0;
2024-03-05 07:43:46 +00:00
for (;;) {
code = tBrinBlockPut(writer->brinBlock, record);
if (code == TSDB_CODE_INVALID_PARA) {
// different records with different primary keys
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
2024-03-05 07:43:46 +00:00
continue;
} else {
TSDB_CHECK_CODE(code, lino, _exit);
}
break;
}
2023-06-09 07:10:54 +00:00
2024-11-13 09:01:50 +00:00
if ((writer->brinBlock->numOfRecords) >= 256) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
2023-06-09 07:10:54 +00:00
}
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-09 07:10:54 +00:00
}
return code;
}
static int32_t tsdbDataFileDoWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
2024-07-17 14:09:09 +00:00
if (bData->nRow == 0) {
return 0;
}
2023-05-29 07:58:54 +00:00
2024-08-20 06:47:05 +00:00
if (!bData->uid) {
return TSDB_CODE_INVALID_PARA;
}
2023-06-02 06:39:54 +00:00
2024-03-07 06:02:13 +00:00
int32_t code = 0;
int32_t lino = 0;
SBuffer *buffers = writer->buffers;
SBuffer *assist = writer->buffers + 4;
2023-05-29 07:58:54 +00:00
2024-03-15 10:22:57 +00:00
SColCompressInfo cmprInfo = {.pColCmpr = NULL, .defaultCmprAlg = writer->config->cmprAlg};
2023-06-09 07:10:54 +00:00
SBrinRecord record[1] = {{
.suid = bData->suid,
.uid = bData->uid,
2023-06-02 06:39:54 +00:00
.minVer = bData->aVersion[0],
.maxVer = bData->aVersion[0],
2023-06-09 07:10:54 +00:00
.blockOffset = writer->files[TSDB_FTYPE_DATA].size,
.smaOffset = writer->files[TSDB_FTYPE_SMA].size,
.blockSize = 0,
.blockKeySize = 0,
.smaSize = 0,
.numRow = bData->nRow,
.count = 1,
2023-06-02 06:39:54 +00:00
}};
2024-02-23 06:44:46 +00:00
tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &record->firstKey);
tsdbRowGetKey(&tsdbRowFromBlockData(bData, bData->nRow - 1), &record->lastKey);
2023-06-02 06:39:54 +00:00
for (int32_t i = 1; i < bData->nRow; ++i) {
2024-03-11 06:18:04 +00:00
if (tsdbRowCompareWithoutVersion(&tsdbRowFromBlockData(bData, i - 1), &tsdbRowFromBlockData(bData, i)) != 0) {
2023-06-09 07:10:54 +00:00
record->count++;
}
if (bData->aVersion[i] < record->minVer) {
record->minVer = bData->aVersion[i];
}
if (bData->aVersion[i] > record->maxVer) {
record->maxVer = bData->aVersion[i];
2023-06-02 06:39:54 +00:00
}
}
2023-05-29 07:58:54 +00:00
tsdbWriterUpdVerRange(&writer->ctx->range, record->minVer, record->maxVer);
2024-03-15 10:22:57 +00:00
code = metaGetColCmpr(writer->config->tsdb->pVnode->pMeta, bData->suid != 0 ? bData->suid : bData->uid,
&cmprInfo.pColCmpr);
2024-08-26 02:23:21 +00:00
if (code) {
tsdbWarn("vgId:%d failed to get column compress algrithm", TD_VID(writer->config->tsdb->pVnode));
}
2024-03-15 10:22:57 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tBlockDataCompress(bData, &cmprInfo, buffers, assist), &lino, _exit);
2023-05-29 07:58:54 +00:00
2024-03-07 06:02:13 +00:00
record->blockKeySize = buffers[0].size + buffers[1].size;
record->blockSize = record->blockKeySize + buffers[2].size + buffers[3].size;
2023-05-29 07:58:54 +00:00
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-03-05 07:43:46 +00:00
for (int i = 0; i < 4; i++) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[TSDB_FTYPE_DATA], writer->files[TSDB_FTYPE_DATA].size, buffers[i].data,
2025-12-05 05:26:28 +00:00
buffers[i].size, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2024-03-07 06:02:13 +00:00
writer->files[TSDB_FTYPE_DATA].size += buffers[i].size;
2023-06-02 06:39:54 +00:00
}
2023-06-09 07:10:54 +00:00
// to .sma file
2024-03-07 06:02:13 +00:00
tBufferClear(&buffers[0]);
2023-06-02 06:39:54 +00:00
for (int32_t i = 0; i < bData->nColData; ++i) {
SColData *colData = bData->aColData + i;
2024-01-26 09:14:00 +00:00
if ((colData->cflag & COL_SMA_ON) == 0 || ((colData->flag & HAS_VALUE) == 0)) continue;
2023-06-02 06:39:54 +00:00
SColumnDataAgg sma[1] = {{.colId = colData->cid}};
feat(decimal): support decimal data type (#30060) * decimal: create table * decimal: add test case decimal.py * decimal: add decimal.c * support input decimal * decimal test * refactor svalue * fix test cases * add decimal unit test * add decimal test cmake * support insert and query decimal type * define wide integer, support decimal128 * support decimal128 divide * set decimal type expr res types * scalar decimal * convert to decimal * fix decimal64/128 from str and to str * fix decimal from str and decimal to str * decimal simple conversion * unit test for decimal * decimal conversion and unit tests * decimal + - * / * decimal scalar ops and comparision * start to refactor GET_TYPED_DATA * support decimal max func, cast func * refactor GET_TYPED_DATA interface * decimal scalar comparision * start to implement sum for decimal * support sum and avg for decimal type * decimal tests * add decimal test * decimal add test cases * decimal use int256/int128 * decimal testing * fix decimal table meta and add tests for decimal col streams * fix create stream and create tsma * test insert decimal values * decimal from str * test decimal input * test parse decimal from string * add taos_fetch_field_e api * decimal insert tests * test decimal operators * decimal operator test * feat:support decimal in raw block * decimal operator tests * decimal test * feat:support decimal in raw block * feat:support decimal in raw block * feat:add schemaExt to SMqDataRsp * feat:remove add schemaExt to SMqDataRsp * feat:remove add schemaExt to SMqDataRsp * feat:remove add schemaExt to SMqDataRsp * decimal test operators * decimal operator test * test decimal operators * test decimal compare operators * decimal unary operator test * decimal col with decimal col oper test * test decimal col filtering * fix decimal float operator test * decimal test where filtering * fix decimal filtering * fix decimal order by * fix decimal op test * test decimal agg funcs * test decimal functions * remove assert * fix ci build for ret check * fix decimal windows build * fix ci ret check * skip decimal ret check * skip decimal ret check * fix decimal tests * fix decimal ci test * decimal test * fix(tmq): heap user after free * fix(tmq): double free * fix(tmq): double free * fix decimal tests * fix(decimal): decimal test ci build * fix(decimal): windows build * fix(decimal): decimal test build * fix(decimal): fix decimal build and tests * fix(decimal): fix decimal tests * fix(decimal): fix taos_fetch_fields_e api * fix(decimal): fix decimal taos_fetch_fields_e api * fix(decimal): rebase 3.0 * fix(decimal): fix decimal functions * fix(decimal): fix decimal test case memory leak * fix(decimal): fix decimal tests * fix(decimal): fix decimal test case * fix(decimal): fix decimal tests * feat(decimal): fix unit tests * feat(decimal): fix deicmal unit test --------- Co-authored-by: wangmm0220 <wangmm0220@gmail.com> Co-authored-by: yihaoDeng <yhdeng@taosdata.com>
2025-03-14 10:08:07 +00:00
tColDataCalcSMA[colData->type](colData, sma);
2023-06-02 06:39:54 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tPutColumnDataAgg(&buffers[0], sma), &lino, _exit);
2023-06-09 09:52:57 +00:00
}
2024-03-07 06:02:13 +00:00
record->smaSize = buffers[0].size;
2023-06-02 06:39:54 +00:00
2023-06-09 07:10:54 +00:00
if (record->smaSize > 0) {
2025-12-05 05:26:28 +00:00
TAOS_CHECK_GOTO(
tsdbWriteFile(writer->fd[TSDB_FTYPE_SMA], record->smaOffset, buffers[0].data, record->smaSize, pEncryptData),
&lino, _exit);
2023-06-09 07:10:54 +00:00
writer->files[TSDB_FTYPE_SMA].size += record->smaSize;
2023-06-02 06:39:54 +00:00
}
2023-06-09 07:10:54 +00:00
// append SBrinRecord
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
2023-05-29 07:58:54 +00:00
2023-06-02 11:03:34 +00:00
tBlockDataClear(bData);
2023-05-29 07:58:54 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-05-29 07:58:54 +00:00
}
2024-03-15 10:22:57 +00:00
taosHashCleanup(cmprInfo.pColCmpr);
2023-05-29 07:58:54 +00:00
return code;
}
2023-06-01 10:56:40 +00:00
2023-05-29 10:42:31 +00:00
static int32_t tsdbDataFileDoWriteTSRow(SDataFileWriter *writer, TSDBROW *row) {
int32_t code = 0;
int32_t lino = 0;
2023-06-02 06:39:54 +00:00
// update/append
2023-05-29 10:42:31 +00:00
if (row->type == TSDBROW_ROW_FMT) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(
tsdbUpdateSkmRow(writer->config->tsdb, writer->ctx->tbid, TSDBROW_SVERSION(row), writer->config->skmRow), &lino,
_exit);
2023-05-29 10:42:31 +00:00
}
2024-03-11 06:18:04 +00:00
if (TSDBROW_VERSION(row) <= writer->config->compactVersion //
&& writer->blockData->nRow > 0 //
&&
tsdbRowCompareWithoutVersion(row, &tsdbRowFromBlockData(writer->blockData, writer->blockData->nRow - 1)) == 0 //
2023-06-02 06:39:54 +00:00
) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tBlockDataUpdateRow(writer->blockData, row, writer->config->skmRow->pTSchema), &lino, _exit);
2023-06-02 06:39:54 +00:00
} else {
2023-06-09 01:23:56 +00:00
if (writer->blockData->nRow >= writer->config->maxRow) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
2023-06-02 06:39:54 +00:00
}
2023-05-29 10:42:31 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(
tBlockDataAppendRow(writer->blockData, row, writer->config->skmRow->pTSchema, writer->ctx->tbid->uid), &lino,
_exit);
2023-05-29 10:42:31 +00:00
}
2023-06-02 06:39:54 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-02 06:39:54 +00:00
}
return code;
}
2024-03-05 07:43:46 +00:00
static FORCE_INLINE int32_t tsdbRowKeyCmprNullAsLargest(const STsdbRowKey *key1, const STsdbRowKey *key2) {
2024-02-23 06:44:46 +00:00
if (key1 == NULL) {
return 1;
} else if (key2 == NULL) {
return -1;
} else {
return tsdbRowKeyCmpr(key1, key2);
}
}
static int32_t tsdbDataFileDoWriteTableOldData(SDataFileWriter *writer, const STsdbRowKey *key) {
2024-07-17 14:09:09 +00:00
if (writer->ctx->tbHasOldData == false) {
return 0;
}
2023-06-09 08:07:06 +00:00
2024-02-23 06:44:46 +00:00
int32_t code = 0;
int32_t lino = 0;
STsdbRowKey rowKey;
2023-06-02 06:39:54 +00:00
2023-06-09 08:07:06 +00:00
for (;;) {
2023-06-09 07:10:54 +00:00
for (;;) {
2023-06-09 08:07:06 +00:00
// SBlockData
for (; writer->ctx->blockDataIdx < writer->ctx->blockData->nRow; writer->ctx->blockDataIdx++) {
2024-02-23 06:44:46 +00:00
TSDBROW row = tsdbRowFromBlockData(writer->ctx->blockData, writer->ctx->blockDataIdx);
tsdbRowGetKey(&row, &rowKey);
if (tsdbRowKeyCmprNullAsLargest(&rowKey, key) < 0) { // key <= rowKey
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSRow(writer, &row), &lino, _exit);
2024-02-23 06:44:46 +00:00
} else {
goto _exit;
2023-06-09 07:10:54 +00:00
}
2023-06-09 08:07:06 +00:00
}
// SBrinBlock
2024-02-23 06:44:46 +00:00
if (writer->ctx->brinBlockIdx >= writer->ctx->brinBlock->numOfRecords) {
2023-06-09 08:07:06 +00:00
break;
}
2023-06-06 08:17:29 +00:00
2024-02-23 06:44:46 +00:00
for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
SBrinRecord record;
2024-09-11 02:56:40 +00:00
code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record);
TSDB_CHECK_CODE(code, lino, _exit);
2024-02-23 06:44:46 +00:00
if (record.uid != writer->ctx->tbid->uid) {
2023-06-09 08:07:06 +00:00
writer->ctx->tbHasOldData = false;
goto _exit;
2023-06-09 07:10:54 +00:00
}
2023-06-02 06:39:54 +00:00
2024-03-11 06:25:26 +00:00
if (tsdbRowKeyCmprNullAsLargest(key, &record.firstKey) < 0) { // key < record->firstKey
2023-06-09 08:07:06 +00:00
goto _exit;
} else {
SBrinRecord record[1];
2024-09-11 02:56:40 +00:00
code = tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, record);
TSDB_CHECK_CODE(code, lino, _exit);
2024-02-23 06:44:46 +00:00
if (tsdbRowKeyCmprNullAsLargest(key, &record->lastKey) > 0) { // key > record->lastKey
2023-06-09 08:07:06 +00:00
if (writer->blockData->nRow > 0) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
2023-06-09 08:07:06 +00:00
}
2023-06-09 07:10:54 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, record), &lino, _exit);
2023-06-09 07:10:54 +00:00
} else {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileReadBlockData(writer->ctx->reader, record, writer->ctx->blockData), &lino,
_exit);
2023-06-09 07:10:54 +00:00
2023-06-09 08:07:06 +00:00
writer->ctx->blockDataIdx = 0;
writer->ctx->brinBlockIdx++;
break;
2023-06-09 07:10:54 +00:00
}
}
}
2023-06-09 08:07:06 +00:00
}
// SBrinBlk
if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
writer->ctx->brinBlkArray = NULL;
writer->ctx->tbHasOldData = false;
goto _exit;
2023-09-06 09:10:36 +00:00
} else {
2023-06-09 08:07:06 +00:00
const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
2023-06-02 06:39:54 +00:00
2023-06-09 08:07:06 +00:00
if (brinBlk->minTbid.uid != writer->ctx->tbid->uid) {
2023-06-09 07:10:54 +00:00
writer->ctx->tbHasOldData = false;
2023-06-09 08:07:06 +00:00
goto _exit;
2023-06-09 07:10:54 +00:00
}
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock), &lino, _exit);
2023-06-09 07:10:54 +00:00
2023-06-09 08:07:06 +00:00
writer->ctx->brinBlockIdx = 0;
writer->ctx->brinBlkArrayIdx++;
}
}
2023-06-09 07:10:54 +00:00
2023-06-09 08:07:06 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-09 08:07:06 +00:00
}
return code;
}
2023-06-02 06:39:54 +00:00
2023-06-09 08:07:06 +00:00
static int32_t tsdbDataFileDoWriteTSData(SDataFileWriter *writer, TSDBROW *row) {
int32_t code = 0;
int32_t lino = 0;
if (writer->ctx->tbHasOldData) {
2024-02-23 06:44:46 +00:00
STsdbRowKey key;
tsdbRowGetKey(row, &key);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, &key), &lino, _exit);
2023-06-02 06:39:54 +00:00
}
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSRow(writer, row), &lino, _exit);
2023-06-02 06:39:54 +00:00
2023-05-29 07:58:54 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-05-29 07:58:54 +00:00
}
return code;
}
2023-06-01 10:56:40 +00:00
2023-05-29 07:58:54 +00:00
static int32_t tsdbDataFileWriteTableDataEnd(SDataFileWriter *writer) {
2024-07-17 14:09:09 +00:00
if (writer->ctx->tbid->uid == 0) {
return 0;
}
2023-05-29 07:58:54 +00:00
int32_t code = 0;
int32_t lino = 0;
if (writer->ctx->tbHasOldData) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, NULL /* as the largest key */), &lino, _exit);
2023-06-09 01:23:56 +00:00
}
2023-05-29 07:58:54 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, writer->blockData), &lino, _exit);
2023-05-29 07:58:54 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-05-29 07:58:54 +00:00
}
return code;
}
2023-06-01 10:56:40 +00:00
2023-05-29 07:58:54 +00:00
static int32_t tsdbDataFileWriteTableDataBegin(SDataFileWriter *writer, const TABLEID *tbid) {
2023-06-12 02:26:18 +00:00
int32_t code = 0;
int32_t lino = 0;
2023-05-29 07:58:54 +00:00
2023-06-12 02:26:18 +00:00
SMetaInfo info;
bool drop = false;
TABLEID tbid1[1];
2023-05-29 10:42:31 +00:00
writer->ctx->tbHasOldData = false;
2023-06-09 01:23:56 +00:00
while (writer->ctx->brinBlkArray) { // skip data of previous table
2024-02-23 06:44:46 +00:00
for (; writer->ctx->brinBlockIdx < writer->ctx->brinBlock->numOfRecords; writer->ctx->brinBlockIdx++) {
SBrinRecord record;
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tBrinBlockGet(writer->ctx->brinBlock, writer->ctx->brinBlockIdx, &record), &lino, _exit);
2023-06-12 02:08:35 +00:00
2024-02-23 06:44:46 +00:00
if (record.uid == tbid->uid) {
2023-06-12 02:08:35 +00:00
writer->ctx->tbHasOldData = true;
goto _begin;
2024-02-23 06:44:46 +00:00
} else if (record.suid > tbid->suid || (record.suid == tbid->suid && record.uid > tbid->uid)) {
2023-06-12 02:08:35 +00:00
goto _begin;
} else {
2024-02-23 06:44:46 +00:00
if (record.uid != writer->ctx->tbid->uid) {
if (drop && tbid1->uid == record.uid) {
2023-06-12 02:08:35 +00:00
continue;
2024-02-23 06:44:46 +00:00
} else if (metaGetInfo(writer->config->tsdb->pVnode->pMeta, record.uid, &info, NULL) != 0) {
2023-06-12 02:08:35 +00:00
drop = true;
2024-02-23 06:44:46 +00:00
tbid1->suid = record.suid;
tbid1->uid = record.uid;
2023-06-12 02:08:35 +00:00
continue;
} else {
drop = false;
2024-02-23 06:44:46 +00:00
writer->ctx->tbid->suid = record.suid;
writer->ctx->tbid->uid = record.uid;
2023-06-12 02:08:35 +00:00
}
2023-06-09 01:23:56 +00:00
}
2023-05-29 10:42:31 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriteBrinRecord(writer, &record), &lino, _exit);
2023-06-09 01:23:56 +00:00
}
}
2023-05-29 07:58:54 +00:00
2023-06-09 01:23:56 +00:00
if (writer->ctx->brinBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->brinBlkArray)) {
writer->ctx->brinBlkArray = NULL;
break;
2023-09-06 09:10:36 +00:00
} else {
2023-06-09 01:23:56 +00:00
const SBrinBlk *brinBlk = TARRAY2_GET_PTR(writer->ctx->brinBlkArray, writer->ctx->brinBlkArrayIdx);
2023-06-02 06:39:54 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlock(writer->ctx->reader, brinBlk, writer->ctx->brinBlock), &lino, _exit);
2023-06-02 06:39:54 +00:00
2023-06-09 01:23:56 +00:00
writer->ctx->brinBlockIdx = 0;
writer->ctx->brinBlkArrayIdx++;
2023-05-29 07:58:54 +00:00
}
}
2023-05-29 10:42:31 +00:00
2023-06-09 01:23:56 +00:00
_begin:
writer->ctx->tbid[0] = *tbid;
2023-06-02 06:39:54 +00:00
2024-07-17 14:09:09 +00:00
if (tbid->uid == INT64_MAX) {
goto _exit;
}
2023-06-02 06:39:54 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbUpdateSkmTb(writer->config->tsdb, tbid, writer->config->skmTb), &lino, _exit);
TAOS_CHECK_GOTO(tBlockDataInit(writer->blockData, writer->ctx->tbid, writer->config->skmTb->pTSchema, NULL, 0), &lino,
_exit);
2023-06-02 06:39:54 +00:00
2023-05-29 07:58:54 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-05-29 07:58:54 +00:00
}
return code;
}
2023-06-01 10:56:40 +00:00
2025-12-05 05:26:28 +00:00
int32_t tsdbFileWriteHeadFooter(STsdbFD *fd, int64_t *fileSize, const SHeadFooter *footer, SEncryptData *encryptData) {
TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptData));
2023-06-25 08:27:14 +00:00
*fileSize += sizeof(*footer);
return 0;
}
int32_t tsdbFileWriteTombBlock(STsdbFD *fd, STombBlock *tombBlock, int8_t cmprAlg, int64_t *fileSize,
2024-04-15 07:12:32 +00:00
TTombBlkArray *tombBlkArray, SBuffer *buffers, SVersionRange *range,
2025-12-05 05:26:28 +00:00
SEncryptData *encryptData) {
int32_t code;
2024-07-17 14:09:09 +00:00
if (TOMB_BLOCK_SIZE(tombBlock) == 0) {
return 0;
}
2024-03-07 06:02:13 +00:00
SBuffer *buffer0 = buffers + 0;
SBuffer *assist = buffers + 1;
2024-03-05 07:43:46 +00:00
STombBlk tombBlk = {
.dp[0] =
{
.offset = *fileSize,
.size = 0,
},
.numRec = TOMB_BLOCK_SIZE(tombBlock),
.cmprAlg = cmprAlg,
2024-03-05 07:43:46 +00:00
};
for (int i = 0; i < TOMB_BLOCK_SIZE(tombBlock); i++) {
STombRecord record;
2024-09-26 02:43:16 +00:00
TAOS_CHECK_RETURN(tTombBlockGet(tombBlock, i, &record));
2024-03-05 07:43:46 +00:00
if (i == 0) {
tombBlk.minTbid.suid = record.suid;
tombBlk.minTbid.uid = record.uid;
tombBlk.minVer = record.version;
tombBlk.maxVer = record.version;
}
2024-03-05 07:43:46 +00:00
if (i == TOMB_BLOCK_SIZE(tombBlock) - 1) {
tombBlk.maxTbid.suid = record.suid;
tombBlk.maxTbid.uid = record.uid;
}
if (record.version < tombBlk.minVer) {
tombBlk.minVer = record.version;
}
if (record.version > tombBlk.maxVer) {
tombBlk.maxVer = record.version;
}
}
tsdbWriterUpdVerRange(range, tombBlk.minVer, tombBlk.maxVer);
2024-03-05 07:43:46 +00:00
for (int32_t i = 0; i < ARRAY_SIZE(tombBlock->buffers); i++) {
2024-03-07 06:02:13 +00:00
tBufferClear(buffer0);
2024-03-05 07:43:46 +00:00
SCompressInfo cinfo = {
.cmprAlg = cmprAlg,
.dataType = TSDB_DATA_TYPE_BIGINT,
.originalSize = tombBlock->buffers[i].size,
};
2024-07-17 14:09:09 +00:00
TAOS_CHECK_RETURN(tCompressDataToBuffer(tombBlock->buffers[i].data, &cinfo, buffer0, assist));
2025-12-05 05:26:28 +00:00
TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, buffer0->data, buffer0->size, encryptData));
2024-03-05 07:43:46 +00:00
tombBlk.size[i] = cinfo.compressedSize;
tombBlk.dp->size += tombBlk.size[i];
*fileSize += tombBlk.size[i];
}
2024-07-17 14:09:09 +00:00
TAOS_CHECK_RETURN(TARRAY2_APPEND_PTR(tombBlkArray, &tombBlk));
tTombBlockClear(tombBlock);
return 0;
}
2023-06-06 08:17:29 +00:00
static int32_t tsdbDataFileWriteHeadFooter(SDataFileWriter *writer) {
2023-05-29 07:58:54 +00:00
int32_t code = 0;
int32_t lino = 0;
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-03-29 02:48:49 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbFileWriteHeadFooter(writer->fd[TSDB_FTYPE_HEAD], &writer->files[TSDB_FTYPE_HEAD].size,
2025-12-05 05:26:28 +00:00
writer->headFooter, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-05-29 10:42:31 +00:00
2023-06-02 06:39:54 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-02 06:39:54 +00:00
}
return code;
}
2023-05-29 10:42:31 +00:00
2023-06-05 14:35:06 +00:00
static int32_t tsdbDataFileDoWriteTombBlock(SDataFileWriter *writer) {
2023-06-09 01:23:56 +00:00
if (TOMB_BLOCK_SIZE(writer->tombBlock) == 0) return 0;
2023-06-05 14:35:06 +00:00
int32_t code = 0;
int32_t lino = 0;
2023-08-31 11:13:58 +00:00
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-03-29 02:48:49 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbFileWriteTombBlock(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlock, writer->config->cmprAlg,
&writer->files[TSDB_FTYPE_TOMB].size, writer->tombBlkArray, writer->buffers,
2025-12-05 05:26:28 +00:00
&writer->ctx->tombRange, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-06-05 14:35:06 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-05 14:35:06 +00:00
}
return code;
}
2024-07-17 14:09:09 +00:00
int32_t tsdbFileWriteTombBlk(STsdbFD *fd, const TTombBlkArray *tombBlkArray, SFDataPtr *ptr, int64_t *fileSize,
2025-12-05 05:26:28 +00:00
SEncryptData *encryptData) {
ptr->size = TARRAY2_DATA_LEN(tombBlkArray);
if (ptr->size > 0) {
ptr->offset = *fileSize;
2025-12-05 05:26:28 +00:00
TAOS_CHECK_RETURN(
tsdbWriteFile(fd, *fileSize, (const uint8_t *)TARRAY2_DATA(tombBlkArray), ptr->size, encryptData));
*fileSize += ptr->size;
}
return 0;
}
2023-06-05 14:35:06 +00:00
static int32_t tsdbDataFileDoWriteTombBlk(SDataFileWriter *writer) {
2024-08-20 06:47:05 +00:00
if (TARRAY2_SIZE(writer->tombBlkArray) <= 0) {
return TSDB_CODE_INVALID_PARA;
}
2023-06-12 02:27:22 +00:00
2023-06-05 14:35:06 +00:00
int32_t code = 0;
int32_t lino = 0;
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-03-29 02:48:49 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(
tsdbFileWriteTombBlk(writer->fd[TSDB_FTYPE_TOMB], writer->tombBlkArray, writer->tombFooter->tombBlkPtr,
2025-12-05 05:26:28 +00:00
&writer->files[TSDB_FTYPE_TOMB].size, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-06-05 14:35:06 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-05 14:35:06 +00:00
}
return code;
}
2025-12-05 05:26:28 +00:00
int32_t tsdbFileWriteTombFooter(STsdbFD *fd, const STombFooter *footer, int64_t *fileSize, SEncryptData *encryptData) {
TAOS_CHECK_RETURN(tsdbWriteFile(fd, *fileSize, (const uint8_t *)footer, sizeof(*footer), encryptData));
2023-06-25 10:02:31 +00:00
*fileSize += sizeof(*footer);
return 0;
}
2023-06-06 08:17:29 +00:00
static int32_t tsdbDataFileWriteTombFooter(SDataFileWriter *writer) {
2023-06-02 06:39:54 +00:00
int32_t code = 0;
int32_t lino = 0;
2023-05-29 10:42:31 +00:00
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-03-29 02:48:49 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbFileWriteTombFooter(writer->fd[TSDB_FTYPE_TOMB], writer->tombFooter,
2025-12-05 05:26:28 +00:00
&writer->files[TSDB_FTYPE_TOMB].size, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-05-29 10:42:31 +00:00
2023-06-06 08:17:29 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-06 08:17:29 +00:00
}
return code;
}
2023-05-29 10:42:31 +00:00
2023-06-06 08:17:29 +00:00
static int32_t tsdbDataFileDoWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
int32_t code = 0;
int32_t lino = 0;
2023-06-02 06:39:54 +00:00
2023-06-06 08:17:29 +00:00
while (writer->ctx->hasOldTomb) {
2023-06-09 01:23:56 +00:00
for (; writer->ctx->tombBlockIdx < TOMB_BLOCK_SIZE(writer->ctx->tombBlock); writer->ctx->tombBlockIdx++) {
2023-06-12 02:26:18 +00:00
STombRecord record1[1];
2024-09-26 02:43:16 +00:00
TAOS_CHECK_GOTO(tTombBlockGet(writer->ctx->tombBlock, writer->ctx->tombBlockIdx, record1), &lino, _exit);
2023-06-05 14:35:06 +00:00
2023-06-08 02:31:19 +00:00
int32_t c = tTombRecordCompare(record, record1);
2023-06-06 08:17:29 +00:00
if (c < 0) {
2023-08-01 07:01:06 +00:00
goto _write;
2023-06-06 08:17:29 +00:00
} else if (c > 0) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record1), &lino, _exit);
2023-06-06 08:17:29 +00:00
2023-08-01 07:01:06 +00:00
tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
", version:%" PRId64,
TD_VID(writer->config->tsdb->pVnode), writer->fd[TSDB_FTYPE_TOMB]->path, writer->config->cid,
record1->suid, record1->uid, record1->version);
2023-06-09 01:23:56 +00:00
if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
2023-06-06 08:17:29 +00:00
}
} else {
2024-08-20 06:47:05 +00:00
tsdbError("vgId:%d duplicate tomb record, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64 ", version:%" PRId64,
TD_VID(writer->config->tsdb->pVnode), writer->config->cid, record->suid, record->uid,
record->version);
2023-06-06 08:17:29 +00:00
}
}
if (writer->ctx->tombBlkArrayIdx >= TARRAY2_SIZE(writer->ctx->tombBlkArray)) {
writer->ctx->hasOldTomb = false;
break;
2023-09-06 09:10:36 +00:00
} else {
2023-06-06 08:17:29 +00:00
const STombBlk *tombBlk = TARRAY2_GET_PTR(writer->ctx->tombBlkArray, writer->ctx->tombBlkArrayIdx);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileReadTombBlock(writer->ctx->reader, tombBlk, writer->ctx->tombBlock), &lino, _exit);
2023-06-06 08:17:29 +00:00
2023-06-09 01:23:56 +00:00
writer->ctx->tombBlockIdx = 0;
2023-06-06 08:17:29 +00:00
writer->ctx->tombBlkArrayIdx++;
}
}
_write:
2024-07-17 14:09:09 +00:00
if (record->suid == INT64_MAX) {
goto _exit;
}
2023-06-06 08:17:29 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tTombBlockPut(writer->tombBlock, record), &lino, _exit);
2023-06-02 06:39:54 +00:00
2023-08-01 07:01:06 +00:00
tsdbTrace("vgId:%d write tomb record to tomb file:%s, cid:%" PRId64 ", suid:%" PRId64 ", uid:%" PRId64
", version:%" PRId64,
TD_VID(writer->config->tsdb->pVnode), writer->fd[TSDB_FTYPE_TOMB]->path, writer->config->cid, record->suid,
record->uid, record->version);
2023-06-09 01:23:56 +00:00
if (TOMB_BLOCK_SIZE(writer->tombBlock) >= writer->config->maxRow) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
2023-06-06 08:17:29 +00:00
}
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-06 08:17:29 +00:00
}
return code;
}
2024-07-17 14:09:09 +00:00
int32_t tsdbFileWriteBrinBlk(STsdbFD *fd, TBrinBlkArray *brinBlkArray, SFDataPtr *ptr, int64_t *fileSize,
2025-12-05 05:26:28 +00:00
SEncryptData *encryptData) {
2024-08-20 06:47:05 +00:00
if (TARRAY2_SIZE(brinBlkArray) <= 0) {
return TSDB_CODE_INVALID_PARA;
}
2023-06-25 07:09:15 +00:00
ptr->offset = *fileSize;
ptr->size = TARRAY2_DATA_LEN(brinBlkArray);
2025-12-05 05:26:28 +00:00
TAOS_CHECK_RETURN(tsdbWriteFile(fd, ptr->offset, (uint8_t *)TARRAY2_DATA(brinBlkArray), ptr->size, encryptData));
2023-06-09 08:07:06 +00:00
2023-06-25 07:09:15 +00:00
*fileSize += ptr->size;
return 0;
}
static int32_t tsdbDataFileWriteBrinBlk(SDataFileWriter *writer) {
2023-06-09 08:07:06 +00:00
int32_t code = 0;
int32_t lino = 0;
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-03-29 02:48:49 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(
tsdbFileWriteBrinBlk(writer->fd[TSDB_FTYPE_HEAD], writer->brinBlkArray, writer->headFooter->brinBlkPtr,
2025-12-05 05:26:28 +00:00
&writer->files[TSDB_FTYPE_HEAD].size, pEncryptData),
2024-07-17 14:09:09 +00:00
&lino, _exit);
2023-06-09 08:07:06 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-09 08:07:06 +00:00
}
return code;
}
void tsdbTFileUpdVerRange(STFile *f, SVersionRange range) {
2023-08-31 11:13:58 +00:00
f->minVer = TMIN(f->minVer, range.minVer);
f->maxVer = TMAX(f->maxVer, range.maxVer);
}
2023-06-06 08:17:29 +00:00
static int32_t tsdbDataFileWriterCloseCommit(SDataFileWriter *writer, TFileOpArray *opArr) {
int32_t code = 0;
int32_t lino = 0;
2023-06-02 10:42:15 +00:00
int32_t ftype;
2023-06-06 08:17:29 +00:00
STFileOp op;
2023-06-02 10:42:15 +00:00
2023-06-06 08:17:29 +00:00
if (writer->fd[TSDB_FTYPE_HEAD]) {
TABLEID tbid[1] = {{
.suid = INT64_MAX,
.uid = INT64_MAX,
}};
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, tbid), &lino, _exit);
TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlock(writer), &lino, _exit);
TAOS_CHECK_GOTO(tsdbDataFileWriteBrinBlk(writer), &lino, _exit);
TAOS_CHECK_GOTO(tsdbDataFileWriteHeadFooter(writer), &lino, _exit);
2023-06-06 08:17:29 +00:00
SVersionRange ofRange = {.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
2023-06-06 08:17:29 +00:00
// .head
ftype = TSDB_FTYPE_HEAD;
if (writer->config->files[ftype].exist) {
op = (STFileOp){
.optype = TSDB_FOP_REMOVE,
.fid = writer->config->fid,
.of = writer->config->files[ftype].file,
};
ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
2023-06-06 08:17:29 +00:00
}
2023-06-02 10:42:15 +00:00
op = (STFileOp){
2023-06-06 08:17:29 +00:00
.optype = TSDB_FOP_CREATE,
2023-06-02 10:42:15 +00:00
.fid = writer->config->fid,
2023-06-06 08:17:29 +00:00
.nf = writer->files[ftype],
2023-06-02 10:42:15 +00:00
};
tsdbTFileUpdVerRange(&op.nf, ofRange);
tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
2023-06-02 06:39:54 +00:00
2023-06-06 08:17:29 +00:00
// .data
ftype = TSDB_FTYPE_DATA;
2023-06-02 09:36:14 +00:00
if (!writer->config->files[ftype].exist) {
2023-06-02 10:42:15 +00:00
op = (STFileOp){
2023-06-02 09:36:14 +00:00
.optype = TSDB_FOP_CREATE,
.fid = writer->config->fid,
.nf = writer->files[ftype],
};
tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
2023-06-02 10:42:15 +00:00
} else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
op = (STFileOp){
2023-06-02 09:36:14 +00:00
.optype = TSDB_FOP_MODIFY,
.fid = writer->config->fid,
.of = writer->config->files[ftype].file,
.nf = writer->files[ftype],
};
tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
2023-06-02 09:36:14 +00:00
}
2023-06-02 06:39:54 +00:00
2023-06-06 08:17:29 +00:00
// .sma
ftype = TSDB_FTYPE_SMA;
2023-06-02 09:36:14 +00:00
if (!writer->config->files[ftype].exist) {
2023-06-02 10:42:15 +00:00
op = (STFileOp){
2023-06-02 09:36:14 +00:00
.optype = TSDB_FOP_CREATE,
.fid = writer->config->fid,
.nf = writer->files[ftype],
};
tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
2023-06-02 10:42:15 +00:00
} else if (writer->config->files[ftype].file.size != writer->files[ftype].size) {
op = (STFileOp){
2023-06-02 09:36:14 +00:00
.optype = TSDB_FOP_MODIFY,
.fid = writer->config->fid,
.of = writer->config->files[ftype].file,
.nf = writer->files[ftype],
};
tsdbTFileUpdVerRange(&op.nf, writer->ctx->range);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
2023-06-02 09:36:14 +00:00
}
}
2023-06-02 06:39:54 +00:00
2023-06-06 08:17:29 +00:00
if (writer->fd[TSDB_FTYPE_TOMB]) {
STombRecord record[1] = {{
.suid = INT64_MAX,
.uid = INT64_MAX,
.version = INT64_MAX,
}};
2023-06-02 09:36:14 +00:00
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlock(writer), &lino, _exit);
TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombBlk(writer), &lino, _exit);
TAOS_CHECK_GOTO(tsdbDataFileWriteTombFooter(writer), &lino, _exit);
2023-06-06 08:17:29 +00:00
SVersionRange ofRange = (SVersionRange){.minVer = VERSION_MAX, .maxVer = VERSION_MIN};
2023-06-29 09:48:36 +00:00
ftype = TSDB_FTYPE_TOMB;
2023-06-06 08:17:29 +00:00
if (writer->config->files[ftype].exist) {
op = (STFileOp){
.optype = TSDB_FOP_REMOVE,
.fid = writer->config->fid,
.of = writer->config->files[ftype].file,
};
ofRange = (SVersionRange){.minVer = op.of.minVer, .maxVer = op.of.maxVer};
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
2023-06-06 08:17:29 +00:00
}
op = (STFileOp){
.optype = TSDB_FOP_CREATE,
.fid = writer->config->fid,
.nf = writer->files[ftype],
};
tsdbTFileUpdVerRange(&op.nf, ofRange);
tsdbTFileUpdVerRange(&op.nf, writer->ctx->tombRange);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(TARRAY2_APPEND(opArr, op), &lino, _exit);
2023-06-06 08:17:29 +00:00
}
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2023-06-06 08:17:29 +00:00
for (int32_t i = 0; i < TSDB_FTYPE_MAX; ++i) {
if (writer->fd[i]) {
2025-12-05 05:26:28 +00:00
TAOS_CHECK_GOTO(tsdbFsyncFile(writer->fd[i], pEncryptData), &lino, _exit);
2023-06-06 08:17:29 +00:00
tsdbCloseFile(&writer->fd[i]);
}
2023-06-02 09:36:14 +00:00
}
2023-05-29 10:42:31 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-05-29 10:42:31 +00:00
}
return code;
}
2023-06-01 10:56:40 +00:00
2023-06-02 09:36:14 +00:00
static int32_t tsdbDataFileWriterOpenDataFD(SDataFileWriter *writer) {
int32_t code = 0;
int32_t lino = 0;
2023-06-05 14:55:00 +00:00
int32_t ftypes[] = {TSDB_FTYPE_HEAD, TSDB_FTYPE_DATA, TSDB_FTYPE_SMA};
for (int32_t i = 0; i < ARRAY_SIZE(ftypes); ++i) {
int32_t ftype = ftypes[i];
2023-06-02 09:36:14 +00:00
char fname[TSDB_FILENAME_LEN];
int32_t flag = TD_FILE_READ | TD_FILE_WRITE;
2023-06-05 14:55:00 +00:00
if (writer->files[ftype].size == 0) {
2023-06-02 09:36:14 +00:00
flag |= (TD_FILE_CREATE | TD_FILE_TRUNC);
}
int32_t lcn = writer->files[ftype].lcn;
2024-09-10 02:05:38 +00:00
tsdbTFileName(writer->config->tsdb, &writer->files[ftype], fname);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd[ftype], lcn), &lino, _exit);
2023-06-02 09:36:14 +00:00
2023-06-05 14:55:00 +00:00
if (writer->files[ftype].size == 0) {
2023-06-02 09:36:14 +00:00
uint8_t hdr[TSDB_FHDR_SIZE] = {0};
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-03-29 02:48:49 +00:00
2025-12-05 05:26:28 +00:00
TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE, pEncryptData), &lino, _exit);
2023-06-02 09:36:14 +00:00
2023-06-05 14:55:00 +00:00
writer->files[ftype].size += TSDB_FHDR_SIZE;
2023-06-02 09:36:14 +00:00
}
}
2023-06-06 08:17:29 +00:00
if (writer->ctx->reader) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileReadBrinBlk(writer->ctx->reader, &writer->ctx->brinBlkArray), &lino, _exit);
2023-06-06 08:17:29 +00:00
}
2023-06-02 09:36:14 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-02 09:36:14 +00:00
}
return code;
}
2023-06-05 14:35:06 +00:00
int32_t tsdbDataFileWriterOpen(const SDataFileWriterConfig *config, SDataFileWriter **writer) {
2023-06-09 01:23:56 +00:00
writer[0] = taosMemoryCalloc(1, sizeof(*writer[0]));
2024-07-17 14:09:09 +00:00
if (!writer[0]) {
return terrno;
2024-07-17 14:09:09 +00:00
}
2023-06-05 14:35:06 +00:00
writer[0]->config[0] = config[0];
return 0;
}
int32_t tsdbDataFileWriterClose(SDataFileWriter **writer, bool abort, TFileOpArray *opArr) {
2023-06-16 04:16:53 +00:00
if (writer[0] == NULL) return 0;
2023-06-05 14:35:06 +00:00
int32_t code = 0;
int32_t lino = 0;
if (writer[0]->ctx->opened) {
if (abort) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriterCloseAbort(writer[0]), &lino, _exit);
2023-06-05 14:35:06 +00:00
} else {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriterCloseCommit(writer[0], opArr), &lino, _exit);
2023-06-05 14:35:06 +00:00
}
tsdbDataFileWriterDoClose(writer[0]);
2023-06-05 14:35:06 +00:00
}
taosMemoryFree(writer[0]);
writer[0] = NULL;
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer[0]->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-05 14:35:06 +00:00
}
return code;
}
2023-06-09 07:10:54 +00:00
int32_t tsdbDataFileWriteRow(SDataFileWriter *writer, SRowInfo *row) {
2023-05-26 07:57:31 +00:00
int32_t code = 0;
int32_t lino = 0;
2023-05-28 15:33:03 +00:00
if (!writer->ctx->opened) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
2023-05-28 15:33:03 +00:00
}
2023-06-06 08:17:29 +00:00
if (writer->fd[TSDB_FTYPE_HEAD] == NULL) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
2023-06-02 09:36:14 +00:00
}
2023-06-02 06:39:54 +00:00
if (row->uid != writer->ctx->tbid->uid) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)row), &lino, _exit);
2023-05-29 07:58:54 +00:00
}
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSData(writer, &row->row), &lino, _exit);
2023-05-29 07:58:54 +00:00
2023-05-26 07:57:31 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-05-26 07:57:31 +00:00
}
return code;
2023-05-28 15:33:03 +00:00
}
2023-06-01 09:05:20 +00:00
2023-06-09 07:10:54 +00:00
int32_t tsdbDataFileWriteBlockData(SDataFileWriter *writer, SBlockData *bData) {
2024-07-17 14:09:09 +00:00
if (bData->nRow == 0) {
return 0;
}
2023-06-02 06:39:54 +00:00
2023-06-01 10:56:40 +00:00
int32_t code = 0;
int32_t lino = 0;
2024-08-20 06:47:05 +00:00
if (!bData->uid) {
return TSDB_CODE_INVALID_PARA;
}
2023-06-01 10:56:40 +00:00
2023-06-02 09:36:14 +00:00
if (!writer->ctx->opened) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
2023-06-02 09:36:14 +00:00
}
2023-06-01 10:56:40 +00:00
2023-06-09 07:10:54 +00:00
if (writer->fd[TSDB_FTYPE_DATA] == NULL) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriterOpenDataFD(writer), &lino, _exit);
2023-06-02 09:36:14 +00:00
}
2023-06-01 10:56:40 +00:00
2023-06-02 09:36:14 +00:00
if (bData->uid != writer->ctx->tbid->uid) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataEnd(writer), &lino, _exit);
TAOS_CHECK_GOTO(tsdbDataFileWriteTableDataBegin(writer, (TABLEID *)bData), &lino, _exit);
2023-06-02 09:36:14 +00:00
}
2023-06-09 08:07:06 +00:00
if (writer->ctx->tbHasOldData) {
2024-03-11 11:17:30 +00:00
STsdbRowKey key;
2023-06-09 08:07:06 +00:00
2024-03-11 11:17:30 +00:00
tsdbRowGetKey(&tsdbRowFromBlockData(bData, 0), &key);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteTableOldData(writer, &key), &lino, _exit);
2023-06-09 08:07:06 +00:00
}
2023-06-09 01:23:56 +00:00
if (!writer->ctx->tbHasOldData //
&& writer->blockData->nRow == 0 //
2023-06-02 09:36:14 +00:00
) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteBlockData(writer, bData), &lino, _exit);
2023-06-02 09:36:14 +00:00
} else {
for (int32_t i = 0; i < bData->nRow; ++i) {
TSDBROW row[1] = {tsdbRowFromBlockData(bData, i)};
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteTSData(writer, row), &lino, _exit);
2023-06-02 09:36:14 +00:00
}
}
2023-06-01 10:56:40 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-01 10:56:40 +00:00
}
return code;
2023-06-01 09:05:20 +00:00
}
2023-06-09 07:10:54 +00:00
int32_t tsdbDataFileFlush(SDataFileWriter *writer) {
2024-08-20 06:47:05 +00:00
if (!writer->ctx->opened) {
return TSDB_CODE_INVALID_PARA;
}
2023-06-02 09:36:14 +00:00
2023-06-09 01:23:56 +00:00
if (writer->blockData->nRow == 0) return 0;
2023-06-02 09:36:14 +00:00
if (writer->ctx->tbHasOldData) return 0;
2023-06-09 07:10:54 +00:00
return tsdbDataFileDoWriteBlockData(writer, writer->blockData);
2023-06-02 06:39:54 +00:00
}
2023-06-05 14:35:06 +00:00
2023-06-05 14:55:00 +00:00
static int32_t tsdbDataFileWriterOpenTombFD(SDataFileWriter *writer) {
int32_t code = 0;
int32_t lino = 0;
char fname[TSDB_FILENAME_LEN];
2023-06-06 08:17:29 +00:00
int32_t ftype = TSDB_FTYPE_TOMB;
2023-06-05 14:55:00 +00:00
2023-06-06 08:17:29 +00:00
int32_t flag = (TD_FILE_READ | TD_FILE_WRITE | TD_FILE_CREATE | TD_FILE_TRUNC);
2023-06-05 14:55:00 +00:00
int32_t lcn = writer->files[ftype].lcn;
2024-09-10 02:05:38 +00:00
tsdbTFileName(writer->config->tsdb, writer->files + ftype, fname);
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbOpenFile(fname, writer->config->tsdb, flag, &writer->fd[ftype], lcn), &lino, _exit);
2023-06-05 14:55:00 +00:00
2023-06-06 08:17:29 +00:00
uint8_t hdr[TSDB_FHDR_SIZE] = {0};
2025-12-05 05:26:28 +00:00
SEncryptData *pEncryptData = &(writer->config->tsdb->pVnode->config.tsdbCfg.encryptData);
2024-07-17 14:09:09 +00:00
2025-12-05 05:26:28 +00:00
TAOS_CHECK_GOTO(tsdbWriteFile(writer->fd[ftype], 0, hdr, TSDB_FHDR_SIZE, pEncryptData), &lino, _exit);
2023-06-06 08:17:29 +00:00
writer->files[ftype].size += TSDB_FHDR_SIZE;
2023-06-05 14:55:00 +00:00
2023-06-06 08:17:29 +00:00
if (writer->ctx->reader) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileReadTombBlk(writer->ctx->reader, &writer->ctx->tombBlkArray), &lino, _exit);
2023-06-05 14:55:00 +00:00
2023-06-06 08:17:29 +00:00
if (TARRAY2_SIZE(writer->ctx->tombBlkArray) > 0) {
writer->ctx->hasOldTomb = true;
2023-06-05 14:55:00 +00:00
}
2023-06-06 08:17:29 +00:00
writer->ctx->tombBlkArrayIdx = 0;
2023-06-09 01:23:56 +00:00
tTombBlockClear(writer->ctx->tombBlock);
writer->ctx->tombBlockIdx = 0;
2023-06-06 08:17:29 +00:00
}
2023-06-05 14:55:00 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-05 14:55:00 +00:00
}
return code;
}
2023-06-05 14:35:06 +00:00
int32_t tsdbDataFileWriteTombRecord(SDataFileWriter *writer, const STombRecord *record) {
int32_t code = 0;
int32_t lino = 0;
if (!writer->ctx->opened) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriterDoOpen(writer), &lino, _exit);
2023-06-05 14:35:06 +00:00
}
2023-06-06 08:17:29 +00:00
if (writer->fd[TSDB_FTYPE_TOMB] == NULL) {
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileWriterOpenTombFD(writer), &lino, _exit);
2023-06-05 14:35:06 +00:00
}
2024-07-17 14:09:09 +00:00
TAOS_CHECK_GOTO(tsdbDataFileDoWriteTombRecord(writer, record), &lino, _exit);
2023-06-05 14:35:06 +00:00
_exit:
if (code) {
2024-07-17 14:09:09 +00:00
tsdbError("vgId:%d %s failed at %s:%d since %s", TD_VID(writer->config->tsdb->pVnode), __func__, __FILE__, lino,
tstrerror(code));
2023-06-05 14:35:06 +00:00
}
return code;
}