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

308 lines
7.8 KiB
C
Raw Normal View History

2020-03-05 06:13:20 +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/>.
*/
2020-06-12 10:52:48 +00:00
2022-04-26 11:04:26 +00:00
#include "tsdb.h"
2020-06-16 09:12:21 +00:00
2022-07-21 06:27:32 +00:00
int32_t tPutHeadFile(uint8_t *p, SHeadFile *pHeadFile) {
2022-06-24 13:10:39 +00:00
int32_t n = 0;
n += tPutI64v(p ? p + n : p, pHeadFile->commitID);
n += tPutI64v(p ? p + n : p, pHeadFile->size);
n += tPutI64v(p ? p + n : p, pHeadFile->offset);
return n;
}
static int32_t tGetHeadFile(uint8_t *p, SHeadFile *pHeadFile) {
int32_t n = 0;
n += tGetI64v(p + n, &pHeadFile->commitID);
n += tGetI64v(p + n, &pHeadFile->size);
n += tGetI64v(p + n, &pHeadFile->offset);
return n;
}
2022-07-21 06:27:32 +00:00
int32_t tPutDataFile(uint8_t *p, SDataFile *pDataFile) {
2022-06-24 13:10:39 +00:00
int32_t n = 0;
n += tPutI64v(p ? p + n : p, pDataFile->commitID);
n += tPutI64v(p ? p + n : p, pDataFile->size);
return n;
}
static int32_t tGetDataFile(uint8_t *p, SDataFile *pDataFile) {
int32_t n = 0;
n += tGetI64v(p + n, &pDataFile->commitID);
n += tGetI64v(p + n, &pDataFile->size);
return n;
}
2022-09-05 09:31:41 +00:00
int32_t tPutSttFile(uint8_t *p, SSttFile *pSttFile) {
2022-06-24 13:10:39 +00:00
int32_t n = 0;
2022-09-05 09:31:41 +00:00
n += tPutI64v(p ? p + n : p, pSttFile->commitID);
n += tPutI64v(p ? p + n : p, pSttFile->size);
n += tPutI64v(p ? p + n : p, pSttFile->offset);
2022-06-24 13:10:39 +00:00
return n;
}
2022-09-05 09:31:41 +00:00
static int32_t tGetSttFile(uint8_t *p, SSttFile *pSttFile) {
2022-06-24 13:10:39 +00:00
int32_t n = 0;
2022-09-05 09:31:41 +00:00
n += tGetI64v(p + n, &pSttFile->commitID);
n += tGetI64v(p + n, &pSttFile->size);
n += tGetI64v(p + n, &pSttFile->offset);
2022-06-24 13:10:39 +00:00
return n;
}
2022-07-21 06:27:32 +00:00
int32_t tPutSmaFile(uint8_t *p, SSmaFile *pSmaFile) {
2022-06-24 13:10:39 +00:00
int32_t n = 0;
n += tPutI64v(p ? p + n : p, pSmaFile->commitID);
n += tPutI64v(p ? p + n : p, pSmaFile->size);
return n;
}
static int32_t tGetSmaFile(uint8_t *p, SSmaFile *pSmaFile) {
int32_t n = 0;
n += tGetI64v(p + n, &pSmaFile->commitID);
n += tGetI64v(p + n, &pSmaFile->size);
return n;
}
// EXPOSED APIS ==================================================
2023-03-31 07:25:00 +00:00
static char* getFileNamePrefix(STsdb *pTsdb, SDiskID did, int32_t fid, uint64_t commitId, char fname[]) {
const char* p1 = tfsGetDiskPath(pTsdb->pVnode->pTfs, did);
int32_t len = strlen(p1);
2023-03-31 07:25:00 +00:00
char* p = memcpy(fname, p1, len);
p += len;
*(p++) = TD_DIRSEP[0];
len = strlen(pTsdb->path);
memcpy(p, pTsdb->path, len);
p += len;
*(p++) = TD_DIRSEP[0];
*(p++) = 'v';
2023-01-16 15:58:45 +00:00
p += titoa(TD_VID(pTsdb->pVnode), 10, p);
*(p++) = 'f';
2023-01-16 15:58:45 +00:00
p += titoa(fid, 10, p);
memcpy(p, "ver", 3);
p += 3;
2023-01-17 02:24:19 +00:00
p += titoa(commitId, 10, p);
return p;
}
void tsdbHeadFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SHeadFile *pHeadF, char fname[]) {
2023-03-31 07:25:00 +00:00
char* p = getFileNamePrefix(pTsdb, did, fid, pHeadF->commitID, fname);
memcpy(p, ".head", 5);
p[5] = 0;
2022-07-21 06:27:32 +00:00
}
2022-06-10 06:48:21 +00:00
2022-07-21 06:27:32 +00:00
void tsdbDataFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SDataFile *pDataF, char fname[]) {
2023-03-31 07:25:00 +00:00
char* p = getFileNamePrefix(pTsdb, did, fid, pDataF->commitID, fname);
2023-01-17 02:24:19 +00:00
memcpy(p, ".data", 5);
p[5] = 0;
2022-06-20 09:47:24 +00:00
}
2022-06-10 06:48:21 +00:00
2022-09-05 09:31:41 +00:00
void tsdbSttFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SSttFile *pSttF, char fname[]) {
2023-03-31 07:25:00 +00:00
char* p = getFileNamePrefix(pTsdb, did, fid, pSttF->commitID, fname);
2023-01-17 02:24:19 +00:00
memcpy(p, ".stt", 4);
p[4] = 0;
2022-07-21 06:27:32 +00:00
}
2022-06-27 07:23:38 +00:00
2022-07-21 06:27:32 +00:00
void tsdbSmaFileName(STsdb *pTsdb, SDiskID did, int32_t fid, SSmaFile *pSmaF, char fname[]) {
2023-03-31 07:25:00 +00:00
char* p = getFileNamePrefix(pTsdb, did, fid, pSmaF->commitID, fname);
2023-01-17 02:24:19 +00:00
memcpy(p, ".sma", 4);
p[4] = 0;
2022-06-27 07:23:38 +00:00
}
2022-07-18 11:45:13 +00:00
bool tsdbDelFileIsSame(SDelFile *pDelFile1, SDelFile *pDelFile2) { return pDelFile1->commitID == pDelFile2->commitID; }
2022-06-27 07:23:38 +00:00
int32_t tsdbDFileRollback(STsdb *pTsdb, SDFileSet *pSet, EDataFileT ftype) {
int32_t code = 0;
2023-02-22 02:50:42 +00:00
int64_t size = 0;
2022-07-21 06:27:32 +00:00
int64_t n;
2022-06-27 07:23:38 +00:00
TdFilePtr pFD;
2023-02-24 01:48:34 +00:00
char fname[TSDB_FILENAME_LEN] = {0};
2022-07-21 06:27:32 +00:00
char hdr[TSDB_FHDR_SIZE] = {0};
2022-06-27 07:23:38 +00:00
// truncate
switch (ftype) {
case TSDB_DATA_FILE:
2022-07-21 06:27:32 +00:00
size = pSet->pDataF->size;
tsdbDataFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pDataF, fname);
tPutDataFile(hdr, pSet->pDataF);
2022-06-27 07:23:38 +00:00
break;
case TSDB_SMA_FILE:
2022-07-21 06:27:32 +00:00
size = pSet->pSmaF->size;
tsdbSmaFileName(pTsdb, pSet->diskId, pSet->fid, pSet->pSmaF, fname);
tPutSmaFile(hdr, pSet->pSmaF);
2022-06-27 07:23:38 +00:00
break;
default:
2023-02-22 02:50:42 +00:00
goto _err; // make the coverity scan happy
2022-06-27 07:23:38 +00:00
}
2022-07-21 06:27:32 +00:00
taosCalcChecksumAppend(0, hdr, TSDB_FHDR_SIZE);
// open
pFD = taosOpenFile(fname, TD_FILE_WRITE);
if (pFD == NULL) {
code = TAOS_SYSTEM_ERROR(errno);
goto _err;
}
// ftruncate
2022-09-09 07:42:20 +00:00
if (taosFtruncateFile(pFD, tsdbLogicToFileSize(size, pTsdb->pVnode->config.tsdbPageSize)) < 0) {
2022-06-27 07:23:38 +00:00
code = TAOS_SYSTEM_ERROR(errno);
goto _err;
}
// update header
2022-07-21 06:27:32 +00:00
n = taosLSeekFile(pFD, 0, SEEK_SET);
if (n < 0) {
code = TAOS_SYSTEM_ERROR(errno);
goto _err;
}
n = taosWriteFile(pFD, hdr, TSDB_FHDR_SIZE);
if (n < 0) {
code = TAOS_SYSTEM_ERROR(errno);
goto _err;
}
2022-06-27 07:23:38 +00:00
// sync
if (taosFsyncFile(pFD) < 0) {
code = TAOS_SYSTEM_ERROR(errno);
goto _err;
}
// close
taosCloseFile(&pFD);
return code;
_err:
2022-08-01 09:23:52 +00:00
tsdbError("vgId:%d, tsdb rollback file failed since %s", TD_VID(pTsdb->pVnode), tstrerror(code));
2022-06-27 07:23:38 +00:00
return code;
}
2022-06-24 13:10:39 +00:00
int32_t tPutDFileSet(uint8_t *p, SDFileSet *pSet) {
int32_t n = 0;
n += tPutI32v(p ? p + n : p, pSet->diskId.level);
n += tPutI32v(p ? p + n : p, pSet->diskId.id);
n += tPutI32v(p ? p + n : p, pSet->fid);
2022-08-19 03:52:43 +00:00
// data
2022-07-21 06:27:32 +00:00
n += tPutHeadFile(p ? p + n : p, pSet->pHeadF);
n += tPutDataFile(p ? p + n : p, pSet->pDataF);
n += tPutSmaFile(p ? p + n : p, pSet->pSmaF);
2022-06-23 08:34:18 +00:00
2022-09-05 09:31:41 +00:00
// stt
n += tPutU8(p ? p + n : p, pSet->nSttF);
for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
n += tPutSttFile(p ? p + n : p, pSet->aSttF[iStt]);
2022-08-22 10:12:40 +00:00
}
2022-08-19 03:52:43 +00:00
2022-06-24 13:10:39 +00:00
return n;
}
2022-06-05 09:23:42 +00:00
2022-06-24 13:10:39 +00:00
int32_t tGetDFileSet(uint8_t *p, SDFileSet *pSet) {
int32_t n = 0;
2022-06-05 09:23:42 +00:00
2022-06-24 13:10:39 +00:00
n += tGetI32v(p + n, &pSet->diskId.level);
n += tGetI32v(p + n, &pSet->diskId.id);
n += tGetI32v(p + n, &pSet->fid);
2022-08-19 03:52:43 +00:00
2022-08-22 10:12:40 +00:00
// head
pSet->pHeadF = (SHeadFile *)taosMemoryCalloc(1, sizeof(SHeadFile));
if (pSet->pHeadF == NULL) {
return -1;
}
pSet->pHeadF->nRef = 1;
2022-07-21 06:27:32 +00:00
n += tGetHeadFile(p + n, pSet->pHeadF);
2022-08-22 10:12:40 +00:00
// data
pSet->pDataF = (SDataFile *)taosMemoryCalloc(1, sizeof(SDataFile));
if (pSet->pDataF == NULL) {
return -1;
}
pSet->pDataF->nRef = 1;
2022-07-21 06:27:32 +00:00
n += tGetDataFile(p + n, pSet->pDataF);
2022-08-22 10:12:40 +00:00
// sma
pSet->pSmaF = (SSmaFile *)taosMemoryCalloc(1, sizeof(SSmaFile));
if (pSet->pSmaF == NULL) {
return -1;
}
pSet->pSmaF->nRef = 1;
2022-07-21 06:27:32 +00:00
n += tGetSmaFile(p + n, pSet->pSmaF);
2022-06-24 13:10:39 +00:00
2022-09-05 09:31:41 +00:00
// stt
n += tGetU8(p + n, &pSet->nSttF);
for (int32_t iStt = 0; iStt < pSet->nSttF; iStt++) {
pSet->aSttF[iStt] = (SSttFile *)taosMemoryCalloc(1, sizeof(SSttFile));
if (pSet->aSttF[iStt] == NULL) {
2022-08-22 10:12:40 +00:00
return -1;
}
2022-09-05 09:31:41 +00:00
pSet->aSttF[iStt]->nRef = 1;
n += tGetSttFile(p + n, pSet->aSttF[iStt]);
2022-08-22 10:12:40 +00:00
}
2022-08-19 03:52:43 +00:00
2022-06-24 13:10:39 +00:00
return n;
}
2022-06-20 09:47:24 +00:00
// SDelFile ===============================================
void tsdbDelFileName(STsdb *pTsdb, SDelFile *pFile, char fname[]) {
2022-07-21 11:42:42 +00:00
snprintf(fname, TSDB_FILENAME_LEN - 1, "%s%s%s%sv%dver%" PRId64 "%s", tfsGetPrimaryPath(pTsdb->pVnode->pTfs),
TD_DIRSEP, pTsdb->path, TD_DIRSEP, TD_VID(pTsdb->pVnode), pFile->commitID, ".del");
2022-06-24 13:10:39 +00:00
}
int32_t tPutDelFile(uint8_t *p, SDelFile *pDelFile) {
int32_t n = 0;
2022-06-28 07:20:46 +00:00
n += tPutI64v(p ? p + n : p, pDelFile->commitID);
2022-06-24 13:10:39 +00:00
n += tPutI64v(p ? p + n : p, pDelFile->size);
n += tPutI64v(p ? p + n : p, pDelFile->offset);
return n;
}
int32_t tGetDelFile(uint8_t *p, SDelFile *pDelFile) {
int32_t n = 0;
2022-06-28 07:20:46 +00:00
n += tGetI64v(p + n, &pDelFile->commitID);
2022-06-24 13:10:39 +00:00
n += tGetI64v(p + n, &pDelFile->size);
n += tGetI64v(p + n, &pDelFile->offset);
return n;
}