TDengine/source/dnode/mnode/sdb/src/sdbFile.c

694 lines
19 KiB
C
Raw Normal View History

2021-11-12 07:06:58 +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/>.
*/
#define _DEFAULT_SOURCE
2022-05-25 04:13:36 +00:00
#include "sdb.h"
#include "sync.h"
2021-11-12 11:37:22 +00:00
#include "tchecksum.h"
2022-04-26 09:19:23 +00:00
#include "wal.h"
2021-11-12 07:06:58 +00:00
2022-04-28 09:44:42 +00:00
#define SDB_TABLE_SIZE 24
2022-01-04 03:23:54 +00:00
#define SDB_RESERVE_SIZE 512
2022-05-20 12:47:00 +00:00
#define SDB_FILE_VER 1
2022-01-04 03:23:54 +00:00
2022-05-28 03:11:48 +00:00
static int32_t sdbDeployData(SSdb *pSdb) {
2022-09-23 07:42:36 +00:00
mInfo("start to deploy sdb");
2021-11-12 11:11:35 +00:00
2022-01-04 03:23:54 +00:00
for (int32_t i = SDB_MAX - 1; i >= 0; --i) {
2021-11-29 07:53:02 +00:00
SdbDeployFp fp = pSdb->deployFps[i];
2021-11-12 07:06:58 +00:00
if (fp == NULL) continue;
2021-11-29 07:53:02 +00:00
2022-09-23 07:42:36 +00:00
mInfo("start to deploy sdb:%s", sdbTableName(i));
2021-11-30 07:28:51 +00:00
if ((*fp)(pSdb->pMnode) != 0) {
2022-04-26 09:42:57 +00:00
mError("failed to deploy sdb:%s since %s", sdbTableName(i), terrstr());
2021-11-29 11:35:42 +00:00
return -1;
2021-11-12 07:06:58 +00:00
}
}
2022-09-23 07:42:36 +00:00
mInfo("sdb deploy success");
2021-11-12 07:06:58 +00:00
return 0;
}
2022-05-28 03:11:48 +00:00
static void sdbResetData(SSdb *pSdb) {
2022-09-23 07:42:36 +00:00
mInfo("start to reset sdb");
2022-05-28 03:11:48 +00:00
for (ESdbType i = 0; i < SDB_MAX; ++i) {
SHashObj *hash = pSdb->hashObjs[i];
if (hash == NULL) continue;
SSdbRow **ppRow = taosHashIterate(hash, NULL);
while (ppRow != NULL) {
SSdbRow *pRow = *ppRow;
if (pRow == NULL) continue;
sdbFreeRow(pSdb, pRow, true);
ppRow = taosHashIterate(hash, ppRow);
}
}
for (ESdbType i = 0; i < SDB_MAX; ++i) {
SHashObj *hash = pSdb->hashObjs[i];
if (hash == NULL) continue;
taosHashClear(pSdb->hashObjs[i]);
pSdb->tableVer[i] = 0;
pSdb->maxId[i] = 0;
2022-09-23 07:42:36 +00:00
mInfo("sdb:%s is reset", sdbTableName(i));
2022-05-28 03:11:48 +00:00
}
2022-06-17 06:46:59 +00:00
pSdb->applyIndex = -1;
pSdb->applyTerm = -1;
pSdb->applyConfig = -1;
pSdb->commitIndex = -1;
pSdb->commitTerm = -1;
pSdb->commitConfig = -1;
2022-09-23 07:42:36 +00:00
mInfo("sdb reset success");
2022-05-28 03:11:48 +00:00
}
static int32_t sdbReadFileHead(SSdb *pSdb, TdFilePtr pFile) {
2022-05-20 12:47:00 +00:00
int64_t sver = 0;
int32_t ret = taosReadFile(pFile, &sver, sizeof(int64_t));
if (ret < 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
if (ret != sizeof(int64_t)) {
terrno = TSDB_CODE_FILE_CORRUPTED;
return -1;
}
if (sver != SDB_FILE_VER) {
terrno = TSDB_CODE_FILE_CORRUPTED;
return -1;
}
2022-06-17 06:46:59 +00:00
ret = taosReadFile(pFile, &pSdb->applyIndex, sizeof(int64_t));
2022-01-04 03:23:54 +00:00
if (ret < 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
if (ret != sizeof(int64_t)) {
terrno = TSDB_CODE_FILE_CORRUPTED;
return -1;
}
2022-06-17 06:46:59 +00:00
ret = taosReadFile(pFile, &pSdb->applyTerm, sizeof(int64_t));
2022-05-23 05:14:54 +00:00
if (ret < 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
if (ret != sizeof(int64_t)) {
terrno = TSDB_CODE_FILE_CORRUPTED;
return -1;
}
2022-06-17 06:46:59 +00:00
ret = taosReadFile(pFile, &pSdb->applyConfig, sizeof(int64_t));
2022-06-09 06:12:52 +00:00
if (ret < 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
if (ret != sizeof(int64_t)) {
terrno = TSDB_CODE_FILE_CORRUPTED;
return -1;
}
2022-01-04 03:23:54 +00:00
for (int32_t i = 0; i < SDB_TABLE_SIZE; ++i) {
2022-04-28 12:17:37 +00:00
int64_t maxId = 0;
ret = taosReadFile(pFile, &maxId, sizeof(int64_t));
2022-01-04 03:23:54 +00:00
if (ret < 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
if (ret != sizeof(int64_t)) {
terrno = TSDB_CODE_FILE_CORRUPTED;
return -1;
}
if (i < SDB_MAX) {
pSdb->maxId[i] = maxId;
}
}
for (int32_t i = 0; i < SDB_TABLE_SIZE; ++i) {
2022-04-28 12:32:27 +00:00
int64_t ver = 0;
ret = taosReadFile(pFile, &ver, sizeof(int64_t));
2022-01-04 03:23:54 +00:00
if (ret < 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
if (ret != sizeof(int64_t)) {
terrno = TSDB_CODE_FILE_CORRUPTED;
return -1;
}
if (i < SDB_MAX) {
pSdb->tableVer[i] = ver;
}
}
char reserve[SDB_RESERVE_SIZE] = {0};
ret = taosReadFile(pFile, reserve, sizeof(reserve));
2022-01-04 03:23:54 +00:00
if (ret < 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
if (ret != sizeof(reserve)) {
terrno = TSDB_CODE_FILE_CORRUPTED;
return -1;
}
return 0;
}
static int32_t sdbWriteFileHead(SSdb *pSdb, TdFilePtr pFile) {
2022-05-20 12:47:00 +00:00
int64_t sver = SDB_FILE_VER;
if (taosWriteFile(pFile, &sver, sizeof(int64_t)) != sizeof(int64_t)) {
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
2022-06-17 06:46:59 +00:00
if (taosWriteFile(pFile, &pSdb->applyIndex, sizeof(int64_t)) != sizeof(int64_t)) {
2022-01-04 03:23:54 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
2022-06-17 06:46:59 +00:00
if (taosWriteFile(pFile, &pSdb->applyTerm, sizeof(int64_t)) != sizeof(int64_t)) {
2022-05-23 05:14:54 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
2022-06-17 06:46:59 +00:00
if (taosWriteFile(pFile, &pSdb->applyConfig, sizeof(int64_t)) != sizeof(int64_t)) {
2022-06-09 06:12:52 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
2022-01-04 03:23:54 +00:00
for (int32_t i = 0; i < SDB_TABLE_SIZE; ++i) {
2022-04-28 12:17:37 +00:00
int64_t maxId = 0;
2022-01-04 03:23:54 +00:00
if (i < SDB_MAX) {
maxId = pSdb->maxId[i];
}
if (taosWriteFile(pFile, &maxId, sizeof(int64_t)) != sizeof(int64_t)) {
2022-01-04 03:23:54 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
}
for (int32_t i = 0; i < SDB_TABLE_SIZE; ++i) {
2022-04-28 12:17:37 +00:00
int64_t ver = 0;
2022-01-04 03:23:54 +00:00
if (i < SDB_MAX) {
ver = pSdb->tableVer[i];
}
if (taosWriteFile(pFile, &ver, sizeof(int64_t)) != sizeof(int64_t)) {
2022-01-04 03:23:54 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
}
2022-01-04 05:40:47 +00:00
char reserve[SDB_RESERVE_SIZE] = {0};
if (taosWriteFile(pFile, reserve, sizeof(reserve)) != sizeof(reserve)) {
2022-01-04 03:23:54 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
return -1;
}
return 0;
}
2022-05-28 03:11:48 +00:00
static int32_t sdbReadFileImp(SSdb *pSdb) {
2021-11-29 07:53:02 +00:00
int64_t offset = 0;
int32_t code = 0;
int32_t readLen = 0;
int64_t ret = 0;
2022-05-28 03:11:48 +00:00
char file[PATH_MAX] = {0};
2023-01-11 08:37:45 +00:00
int32_t bufLen = TSDB_MAX_MSG_SIZE;
2022-05-28 03:11:48 +00:00
snprintf(file, sizeof(file), "%s%ssdb.data", pSdb->currDir, TD_DIRSEP);
2022-09-23 07:42:36 +00:00
mInfo("start to read sdb file:%s", file);
2021-11-29 07:53:02 +00:00
2023-01-11 08:37:45 +00:00
SSdbRaw *pRaw = taosMemoryMalloc(bufLen + 100);
2021-11-12 07:06:58 +00:00
if (pRaw == NULL) {
2021-11-29 11:35:42 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
mError("failed read sdb file since %s", terrstr());
2021-11-29 11:35:42 +00:00
return -1;
2021-11-12 07:06:58 +00:00
}
TdFilePtr pFile = taosOpenFile(file, TD_FILE_READ);
if (pFile == NULL) {
2022-03-25 16:29:53 +00:00
taosMemoryFree(pRaw);
2021-11-29 11:35:42 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
2023-01-04 03:48:30 +00:00
mInfo("read sdb file:%s finished since %s", file, terrstr());
2021-12-23 09:47:21 +00:00
return 0;
2021-11-12 07:06:58 +00:00
}
if (sdbReadFileHead(pSdb, pFile) != 0) {
mError("failed to read sdb file:%s head since %s", file, terrstr());
2022-03-25 16:29:53 +00:00
taosMemoryFree(pRaw);
taosCloseFile(&pFile);
2022-01-04 03:23:54 +00:00
return -1;
}
2022-04-28 09:44:42 +00:00
int64_t tableVer[SDB_MAX] = {0};
memcpy(tableVer, pSdb->tableVer, sizeof(tableVer));
2021-11-12 07:06:58 +00:00
while (1) {
2021-11-12 13:35:29 +00:00
readLen = sizeof(SSdbRaw);
ret = taosReadFile(pFile, pRaw, readLen);
2021-11-12 07:06:58 +00:00
if (ret == 0) break;
if (ret < 0) {
code = TAOS_SYSTEM_ERROR(errno);
mError("failed to read sdb file:%s since %s", file, tstrerror(code));
goto _OVER;
2021-11-12 07:06:58 +00:00
}
2021-11-12 13:35:29 +00:00
if (ret != readLen) {
2021-11-12 11:37:22 +00:00
code = TSDB_CODE_FILE_CORRUPTED;
mError("failed to read sdb file:%s since %s, ret:%" PRId64 " != readLen:%d", file, tstrerror(code), ret, readLen);
goto _OVER;
2021-11-12 11:37:22 +00:00
}
2021-11-12 13:35:29 +00:00
readLen = pRaw->dataLen + sizeof(int32_t);
2023-01-11 08:37:45 +00:00
if (readLen >= bufLen) {
bufLen = pRaw->dataLen * 2;
SSdbRaw *pNewRaw = taosMemoryMalloc(bufLen + 100);
if (pNewRaw == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
2023-01-11 08:37:45 +00:00
mError("failed read sdb file since malloc new sdbRaw size:%d failed", bufLen);
goto _OVER;
}
2023-01-11 08:37:45 +00:00
mInfo("malloc new sdb raw size:%d, type:%d", bufLen, pRaw->type);
memcpy(pNewRaw, pRaw, sizeof(SSdbRaw));
sdbFreeRaw(pRaw);
pRaw = pNewRaw;
}
ret = taosReadFile(pFile, pRaw->pData, readLen);
2021-11-12 11:37:22 +00:00
if (ret < 0) {
code = TAOS_SYSTEM_ERROR(errno);
mError("failed to read sdb file:%s since %s, ret:%" PRId64 " readLen:%d", file, tstrerror(code), ret, readLen);
goto _OVER;
2021-11-12 11:37:22 +00:00
}
2021-11-12 13:35:29 +00:00
if (ret != readLen) {
2021-11-12 11:37:22 +00:00
code = TSDB_CODE_FILE_CORRUPTED;
mError("failed to read sdb file:%s since %s, ret:%" PRId64 " != readLen:%d", file, tstrerror(code), ret, readLen);
goto _OVER;
2021-11-12 11:37:22 +00:00
}
2021-11-12 13:35:29 +00:00
int32_t totalLen = sizeof(SSdbRaw) + pRaw->dataLen + sizeof(int32_t);
if ((!taosCheckChecksumWhole((const uint8_t *)pRaw, totalLen)) != 0) {
2021-11-12 11:37:22 +00:00
code = TSDB_CODE_CHECKSUM_ERROR;
mError("failed to read sdb file:%s since %s, readLen:%d", file, tstrerror(code), readLen);
goto _OVER;
2021-11-12 07:06:58 +00:00
}
2022-04-26 07:47:45 +00:00
code = sdbWriteWithoutFree(pSdb, pRaw);
2021-11-12 07:06:58 +00:00
if (code != 0) {
mError("failed to read sdb file:%s since %s", file, terrstr());
2022-04-28 12:32:27 +00:00
goto _OVER;
2021-11-12 07:06:58 +00:00
}
}
code = 0;
2022-06-17 06:46:59 +00:00
pSdb->commitIndex = pSdb->applyIndex;
pSdb->commitTerm = pSdb->applyTerm;
pSdb->commitConfig = pSdb->applyConfig;
2022-04-28 09:44:42 +00:00
memcpy(pSdb->tableVer, tableVer, sizeof(tableVer));
2022-10-13 03:56:16 +00:00
mInfo("read sdb file:%s success, commit index:%" PRId64 " term:%" PRId64 " config:%" PRId64, file, pSdb->commitIndex,
pSdb->commitTerm, pSdb->commitConfig);
2021-11-12 07:06:58 +00:00
2022-04-28 12:32:27 +00:00
_OVER:
taosCloseFile(&pFile);
2021-11-12 11:37:22 +00:00
sdbFreeRaw(pRaw);
2021-11-29 07:53:02 +00:00
2021-11-29 11:35:42 +00:00
terrno = code;
2021-11-12 07:06:58 +00:00
return code;
}
2022-05-28 03:11:48 +00:00
int32_t sdbReadFile(SSdb *pSdb) {
taosThreadMutexLock(&pSdb->filelock);
sdbResetData(pSdb);
int32_t code = sdbReadFileImp(pSdb);
if (code != 0) {
mError("failed to read sdb file since %s", terrstr());
2022-05-28 03:11:48 +00:00
sdbResetData(pSdb);
}
taosThreadMutexUnlock(&pSdb->filelock);
return code;
}
2022-01-25 11:46:15 +00:00
static int32_t sdbWriteFileImp(SSdb *pSdb) {
2021-11-29 07:53:02 +00:00
int32_t code = 0;
2021-11-12 07:06:58 +00:00
char tmpfile[PATH_MAX] = {0};
2021-11-29 11:35:42 +00:00
snprintf(tmpfile, sizeof(tmpfile), "%s%ssdb.data", pSdb->tmpDir, TD_DIRSEP);
2021-11-29 07:53:02 +00:00
char curfile[PATH_MAX] = {0};
2021-11-29 11:35:42 +00:00
snprintf(curfile, sizeof(curfile), "%s%ssdb.data", pSdb->currDir, TD_DIRSEP);
2021-11-12 07:06:58 +00:00
2022-09-23 07:42:36 +00:00
mInfo("start to write sdb file, apply index:%" PRId64 " term:%" PRId64 " config:%" PRId64 ", commit index:%" PRId64
2022-10-13 03:56:16 +00:00
" term:%" PRId64 " config:%" PRId64 ", file:%s",
pSdb->applyIndex, pSdb->applyTerm, pSdb->applyConfig, pSdb->commitIndex, pSdb->commitTerm, pSdb->commitConfig,
curfile);
2021-12-31 02:52:10 +00:00
2022-04-11 10:55:43 +00:00
TdFilePtr pFile = taosOpenFile(tmpfile, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
if (pFile == NULL) {
2021-11-29 11:35:42 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
mError("failed to open sdb file:%s for write since %s", tmpfile, terrstr());
2021-11-29 11:35:42 +00:00
return -1;
2021-11-12 07:06:58 +00:00
}
if (sdbWriteFileHead(pSdb, pFile) != 0) {
mError("failed to write sdb file:%s head since %s", tmpfile, terrstr());
taosCloseFile(&pFile);
2022-01-04 03:23:54 +00:00
return -1;
}
for (int32_t i = SDB_MAX - 1; i >= 0; --i) {
2021-11-29 07:53:02 +00:00
SdbEncodeFp encodeFp = pSdb->encodeFps[i];
2021-11-12 11:11:35 +00:00
if (encodeFp == NULL) continue;
2022-09-23 07:42:36 +00:00
mInfo("write %s to sdb file, total %d rows", sdbTableName(i), sdbGetSize(pSdb, i));
2021-12-31 02:52:10 +00:00
SHashObj *hash = pSdb->hashObjs[i];
sdbWriteLock(pSdb, i);
2021-11-12 07:06:58 +00:00
2021-11-12 13:35:29 +00:00
SSdbRow **ppRow = taosHashIterate(hash, NULL);
while (ppRow != NULL) {
SSdbRow *pRow = *ppRow;
if (pRow == NULL) {
ppRow = taosHashIterate(hash, ppRow);
continue;
}
if (pRow->status != SDB_STATUS_READY && pRow->status != SDB_STATUS_DROPPING) {
sdbPrintOper(pSdb, pRow, "not-write");
2021-11-12 13:35:29 +00:00
ppRow = taosHashIterate(hash, ppRow);
continue;
}
2021-11-12 11:37:22 +00:00
2022-04-26 07:47:45 +00:00
sdbPrintOper(pSdb, pRow, "write");
2021-12-31 02:52:10 +00:00
2021-11-12 07:06:58 +00:00
SSdbRaw *pRaw = (*encodeFp)(pRow->pObj);
if (pRaw != NULL) {
2021-11-12 13:35:29 +00:00
pRaw->status = pRow->status;
2021-11-12 11:37:22 +00:00
int32_t writeLen = sizeof(SSdbRaw) + pRaw->dataLen;
if (taosWriteFile(pFile, pRaw, writeLen) != writeLen) {
2022-01-04 03:23:54 +00:00
code = TAOS_SYSTEM_ERROR(errno);
2021-11-12 13:35:29 +00:00
taosHashCancelIterate(hash, ppRow);
2021-12-28 12:51:05 +00:00
sdbFreeRaw(pRaw);
2021-11-12 11:37:22 +00:00
break;
}
2021-11-12 13:35:29 +00:00
int32_t cksum = taosCalcChecksum(0, (const uint8_t *)pRaw, sizeof(SSdbRaw) + pRaw->dataLen);
if (taosWriteFile(pFile, &cksum, sizeof(int32_t)) != sizeof(int32_t)) {
2022-01-04 03:23:54 +00:00
code = TAOS_SYSTEM_ERROR(errno);
2021-11-12 13:35:29 +00:00
taosHashCancelIterate(hash, ppRow);
2021-12-28 12:51:05 +00:00
sdbFreeRaw(pRaw);
2021-11-12 11:37:22 +00:00
break;
}
2021-11-12 07:06:58 +00:00
} else {
2022-12-03 02:03:18 +00:00
code = TSDB_CODE_APP_ERROR;
2021-11-12 13:35:29 +00:00
taosHashCancelIterate(hash, ppRow);
2021-11-12 07:06:58 +00:00
break;
}
2021-12-28 12:51:05 +00:00
sdbFreeRaw(pRaw);
2021-11-12 13:35:29 +00:00
ppRow = taosHashIterate(hash, ppRow);
2021-11-12 07:06:58 +00:00
}
sdbUnLock(pSdb, i);
2021-11-12 07:06:58 +00:00
}
if (code == 0) {
code = taosFsyncFile(pFile);
2021-11-29 07:53:02 +00:00
if (code != 0) {
code = TAOS_SYSTEM_ERROR(errno);
mError("failed to sync sdb file:%s since %s", tmpfile, tstrerror(code));
2021-11-29 07:53:02 +00:00
}
2021-11-12 07:06:58 +00:00
}
taosCloseFile(&pFile);
2021-11-12 13:35:29 +00:00
2021-11-12 11:11:35 +00:00
if (code == 0) {
2021-11-12 07:06:58 +00:00
code = taosRenameFile(tmpfile, curfile);
2021-11-29 07:53:02 +00:00
if (code != 0) {
code = TAOS_SYSTEM_ERROR(errno);
mError("failed to write sdb file:%s since %s", curfile, tstrerror(code));
2021-11-29 07:53:02 +00:00
}
2021-11-12 07:06:58 +00:00
}
if (code != 0) {
mError("failed to write sdb file:%s since %s", curfile, tstrerror(code));
2021-11-12 07:06:58 +00:00
} else {
2022-06-17 06:46:59 +00:00
pSdb->commitIndex = pSdb->applyIndex;
pSdb->commitTerm = pSdb->applyTerm;
pSdb->commitConfig = pSdb->applyConfig;
2022-09-23 07:42:36 +00:00
mInfo("write sdb file success, commit index:%" PRId64 " term:%" PRId64 " config:%" PRId64 " file:%s",
2022-10-13 03:56:16 +00:00
pSdb->commitIndex, pSdb->commitTerm, pSdb->commitConfig, curfile);
2021-11-12 07:06:58 +00:00
}
2021-11-29 11:35:42 +00:00
terrno = code;
2021-11-12 07:06:58 +00:00
return code;
}
2022-06-17 07:23:17 +00:00
int32_t sdbWriteFile(SSdb *pSdb, int32_t delta) {
int32_t code = 0;
2022-06-17 06:46:59 +00:00
if (pSdb->applyIndex == pSdb->commitIndex) {
2022-01-25 11:46:15 +00:00
return 0;
}
2022-06-17 07:23:17 +00:00
if (pSdb->applyIndex - pSdb->commitIndex < delta) {
return 0;
}
2022-05-28 03:11:48 +00:00
taosThreadMutexLock(&pSdb->filelock);
if (pSdb->pWal != NULL) {
2023-03-01 02:02:33 +00:00
if (pSdb->sync > 0) {
code = syncBeginSnapshot(pSdb->sync, pSdb->applyIndex);
}
}
if (code == 0) {
code = sdbWriteFileImp(pSdb);
}
if (code == 0) {
if (pSdb->pWal != NULL) {
2023-03-01 02:02:33 +00:00
if (pSdb->sync > 0) {
code = syncEndSnapshot(pSdb->sync);
}
}
}
2022-05-28 03:11:48 +00:00
if (code != 0) {
mError("failed to write sdb file since %s", terrstr());
2022-05-28 03:11:48 +00:00
}
taosThreadMutexUnlock(&pSdb->filelock);
return code;
2022-01-25 11:46:15 +00:00
}
2021-11-29 07:53:02 +00:00
int32_t sdbDeploy(SSdb *pSdb) {
2022-05-28 03:11:48 +00:00
if (sdbDeployData(pSdb) != 0) {
2021-11-29 11:35:42 +00:00
return -1;
2021-11-12 07:06:58 +00:00
}
2022-06-17 07:23:17 +00:00
if (sdbWriteFile(pSdb, 0) != 0) {
2021-11-29 11:35:42 +00:00
return -1;
2021-11-12 07:06:58 +00:00
}
return 0;
}
2022-05-24 11:29:23 +00:00
2022-05-28 08:41:38 +00:00
static SSdbIter *sdbCreateIter(SSdb *pSdb) {
2022-05-24 11:29:23 +00:00
SSdbIter *pIter = taosMemoryCalloc(1, sizeof(SSdbIter));
if (pIter == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return NULL;
}
2022-05-28 08:41:38 +00:00
char name[PATH_MAX + 100] = {0};
snprintf(name, sizeof(name), "%s%ssdb.data.%" PRIu64, pSdb->tmpDir, TD_DIRSEP, (uint64_t)pIter);
pIter->name = taosStrdup(name);
2022-05-28 08:41:38 +00:00
if (pIter->name == NULL) {
2022-05-24 11:29:23 +00:00
taosMemoryFree(pIter);
2022-05-28 08:41:38 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-05-24 11:29:23 +00:00
return NULL;
}
return pIter;
}
2022-05-28 08:41:38 +00:00
static void sdbCloseIter(SSdbIter *pIter) {
2022-05-27 14:49:18 +00:00
if (pIter == NULL) return;
2022-05-28 08:41:38 +00:00
2022-05-27 14:49:18 +00:00
if (pIter->file != NULL) {
taosCloseFile(&pIter->file);
2022-05-28 08:41:38 +00:00
pIter->file = NULL;
2022-05-27 14:49:18 +00:00
}
2022-05-28 03:11:48 +00:00
2022-05-28 08:41:38 +00:00
if (pIter->name != NULL) {
2022-09-28 01:29:54 +00:00
(void)taosRemoveFile(pIter->name);
2022-05-28 08:41:38 +00:00
taosMemoryFree(pIter->name);
pIter->name = NULL;
}
2022-05-28 03:11:48 +00:00
2022-09-23 07:42:36 +00:00
mInfo("sdbiter:%p, is closed, total:%" PRId64, pIter, pIter->total);
2022-05-27 14:49:18 +00:00
taosMemoryFree(pIter);
}
int32_t sdbStartRead(SSdb *pSdb, SSdbIter **ppIter, int64_t *index, int64_t *term, int64_t *config) {
2022-05-28 08:41:38 +00:00
SSdbIter *pIter = sdbCreateIter(pSdb);
if (pIter == NULL) return -1;
2022-05-27 14:49:18 +00:00
2022-05-28 08:41:38 +00:00
char datafile[PATH_MAX] = {0};
snprintf(datafile, sizeof(datafile), "%s%ssdb.data", pSdb->currDir, TD_DIRSEP);
taosThreadMutexLock(&pSdb->filelock);
2022-06-17 06:46:59 +00:00
int64_t commitIndex = pSdb->commitIndex;
int64_t commitTerm = pSdb->commitTerm;
int64_t commitConfig = pSdb->commitConfig;
2022-05-28 08:41:38 +00:00
if (taosCopyFile(datafile, pIter->name) < 0) {
taosThreadMutexUnlock(&pSdb->filelock);
terrno = TAOS_SYSTEM_ERROR(errno);
mError("failed to copy sdb file %s to %s since %s", datafile, pIter->name, terrstr());
2022-05-28 08:41:38 +00:00
sdbCloseIter(pIter);
return -1;
2022-05-27 14:49:18 +00:00
}
2022-05-28 08:41:38 +00:00
taosThreadMutexUnlock(&pSdb->filelock);
2022-05-27 14:49:18 +00:00
2022-05-28 08:41:38 +00:00
pIter->file = taosOpenFile(pIter->name, TD_FILE_READ);
if (pIter->file == NULL) {
terrno = TAOS_SYSTEM_ERROR(errno);
mError("failed to open sdb file:%s since %s", pIter->name, terrstr());
2022-05-28 08:41:38 +00:00
sdbCloseIter(pIter);
return -1;
}
*ppIter = pIter;
if (index != NULL) *index = commitIndex;
if (term != NULL) *term = commitTerm;
if (config != NULL) *config = commitConfig;
2022-10-13 03:56:16 +00:00
mInfo("sdbiter:%p, is created to read snapshot, commit index:%" PRId64 " term:%" PRId64 " config:%" PRId64 " file:%s",
pIter, commitIndex, commitTerm, commitConfig, pIter->name);
2022-05-28 08:41:38 +00:00
return 0;
2022-05-27 14:49:18 +00:00
}
void sdbStopRead(SSdb *pSdb, SSdbIter *pIter) { sdbCloseIter(pIter); }
2022-05-27 14:49:18 +00:00
2022-05-28 08:41:38 +00:00
int32_t sdbDoRead(SSdb *pSdb, SSdbIter *pIter, void **ppBuf, int32_t *len) {
2022-09-23 07:42:36 +00:00
int32_t maxlen = 4096;
2022-05-28 07:00:18 +00:00
void *pBuf = taosMemoryCalloc(1, maxlen);
2022-05-24 11:29:23 +00:00
if (pBuf == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-05-27 14:49:18 +00:00
return -1;
2022-05-24 11:29:23 +00:00
}
int32_t readlen = taosReadFile(pIter->file, pBuf, maxlen);
2022-05-28 07:00:18 +00:00
if (readlen < 0 || readlen > maxlen) {
2022-05-24 11:29:23 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
2022-05-27 14:49:18 +00:00
mError("sdbiter:%p, failed to read snapshot since %s, total:%" PRId64, pIter, terrstr(), pIter->total);
*ppBuf = NULL;
*len = 0;
2022-05-24 11:29:23 +00:00
taosMemoryFree(pBuf);
2022-05-27 14:49:18 +00:00
return -1;
} else if (readlen == 0) {
2022-09-23 07:42:36 +00:00
mInfo("sdbiter:%p, read snapshot to the end, total:%" PRId64, pIter, pIter->total);
2022-05-27 14:49:18 +00:00
*ppBuf = NULL;
*len = 0;
taosMemoryFree(pBuf);
return 0;
2022-05-28 07:00:18 +00:00
} else { // (readlen <= maxlen)
2022-05-27 14:49:18 +00:00
pIter->total += readlen;
2022-09-23 07:42:36 +00:00
mInfo("sdbiter:%p, read:%d bytes from snapshot, total:%" PRId64, pIter, readlen, pIter->total);
2022-05-27 14:49:18 +00:00
*ppBuf = pBuf;
*len = readlen;
return 0;
2022-05-24 11:29:23 +00:00
}
}
2022-05-27 14:49:18 +00:00
2022-05-28 08:41:38 +00:00
int32_t sdbStartWrite(SSdb *pSdb, SSdbIter **ppIter) {
SSdbIter *pIter = sdbCreateIter(pSdb);
if (pIter == NULL) return -1;
2022-05-27 14:49:18 +00:00
2022-05-28 08:41:38 +00:00
pIter->file = taosOpenFile(pIter->name, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC);
if (pIter->file == NULL) {
2022-05-27 14:49:18 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
2022-05-28 08:41:38 +00:00
mError("failed to open %s since %s", pIter->name, terrstr());
2022-09-28 01:29:54 +00:00
sdbCloseIter(pIter);
2022-05-27 14:49:18 +00:00
return -1;
}
2022-05-28 08:41:38 +00:00
*ppIter = pIter;
2022-09-23 07:42:36 +00:00
mInfo("sdbiter:%p, is created to write snapshot, file:%s", pIter, pIter->name);
2022-05-28 08:41:38 +00:00
return 0;
}
2022-05-27 14:49:18 +00:00
int32_t sdbStopWrite(SSdb *pSdb, SSdbIter *pIter, bool isApply, int64_t index, int64_t term, int64_t config) {
2023-01-06 07:40:09 +00:00
int32_t code = -1;
2022-05-28 08:41:38 +00:00
if (!isApply) {
2022-09-23 07:42:36 +00:00
mInfo("sdbiter:%p, not apply to sdb", pIter);
2023-01-06 07:40:09 +00:00
code = 0;
goto _OVER;
2022-05-27 14:49:18 +00:00
}
2023-01-06 07:40:09 +00:00
if (taosFsyncFile(pIter->file) != 0) {
terrno = TAOS_SYSTEM_ERROR(errno);
mError("sdbiter:%p, failed to fasync file %s since %s", pIter, pIter->name, terrstr());
goto _OVER;
2022-05-27 14:49:18 +00:00
}
2022-05-28 08:41:38 +00:00
taosCloseFile(&pIter->file);
pIter->file = NULL;
2022-05-27 14:49:18 +00:00
2022-05-28 08:41:38 +00:00
char datafile[PATH_MAX] = {0};
snprintf(datafile, sizeof(datafile), "%s%ssdb.data", pSdb->currDir, TD_DIRSEP);
if (taosRenameFile(pIter->name, datafile) != 0) {
2022-05-27 14:49:18 +00:00
terrno = TAOS_SYSTEM_ERROR(errno);
2022-05-28 08:41:38 +00:00
mError("sdbiter:%p, failed to rename file %s to %s since %s", pIter, pIter->name, datafile, terrstr());
2023-01-06 07:40:09 +00:00
goto _OVER;
2022-05-27 14:49:18 +00:00
}
if (sdbReadFile(pSdb) != 0) {
2022-05-28 08:41:38 +00:00
mError("sdbiter:%p, failed to read from %s since %s", pIter, datafile, terrstr());
2023-01-06 07:40:09 +00:00
goto _OVER;
2022-05-28 08:41:38 +00:00
}
if (config > 0) {
pSdb->commitConfig = config;
}
if (term > 0) {
pSdb->commitTerm = term;
}
if (index > 0) {
pSdb->commitIndex = index;
}
2022-09-23 07:42:36 +00:00
mInfo("sdbiter:%p, success applyed to sdb", pIter);
2023-01-06 07:40:09 +00:00
code = 0;
_OVER:
2022-09-28 01:29:54 +00:00
sdbCloseIter(pIter);
2023-01-06 07:40:09 +00:00
return code;
2022-05-28 08:41:38 +00:00
}
int32_t sdbDoWrite(SSdb *pSdb, SSdbIter *pIter, void *pBuf, int32_t len) {
int32_t writelen = taosWriteFile(pIter->file, pBuf, len);
if (writelen != len) {
terrno = TAOS_SYSTEM_ERROR(errno);
mError("failed to write len:%d since %s, total:%" PRId64, len, terrstr(), pIter->total);
2022-05-27 14:49:18 +00:00
return -1;
}
2022-05-28 08:41:38 +00:00
pIter->total += writelen;
2022-09-23 07:42:36 +00:00
mInfo("sdbiter:%p, write:%d bytes to snapshot, total:%" PRId64, pIter, writelen, pIter->total);
2022-05-27 14:49:18 +00:00
return 0;
}