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

103 lines
2.6 KiB
C
Raw Normal View History

2022-04-27 07:04:56 +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/>.
*/
#include "tsdb.h"
2022-07-30 11:26:10 +00:00
int32_t tsdbSetKeepCfg(STsdb *pTsdb, STsdbCfg *pCfg) {
STsdbKeepCfg *pKeepCfg = &pTsdb->keepCfg;
2022-05-07 14:33:19 +00:00
pKeepCfg->precision = pCfg->precision;
pKeepCfg->days = pCfg->days;
pKeepCfg->keep0 = pCfg->keep0;
pKeepCfg->keep1 = pCfg->keep1;
pKeepCfg->keep2 = pCfg->keep2;
2022-05-07 14:33:19 +00:00
return 0;
}
2022-05-01 16:30:47 +00:00
/**
2022-05-08 09:04:19 +00:00
* @brief
*
* @param pVnode
* @param ppTsdb
* @param dir
* @return int
2022-05-01 16:30:47 +00:00
*/
2022-10-18 05:38:26 +00:00
int tsdbOpen(SVnode *pVnode, STsdb **ppTsdb, const char *dir, STsdbKeepCfg *pKeepCfg, int8_t rollback) {
2022-04-27 07:04:56 +00:00
STsdb *pTsdb = NULL;
int slen = 0;
*ppTsdb = NULL;
2022-11-01 06:51:37 +00:00
slen = strlen(pVnode->path) + strlen(dir) + 2;
2022-04-27 07:04:56 +00:00
// create handle
pTsdb = (STsdb *)taosMemoryCalloc(1, sizeof(*pTsdb) + slen);
if (pTsdb == NULL) {
terrno = TSDB_CODE_OUT_OF_MEMORY;
return -1;
}
pTsdb->path = (char *)&pTsdb[1];
snprintf(pTsdb->path, TD_PATH_MAX, "%s%s%s", pVnode->path, TD_DIRSEP, dir);
// taosRealPath(pTsdb->path, NULL, slen);
2022-04-27 07:04:56 +00:00
pTsdb->pVnode = pVnode;
2022-07-19 06:19:01 +00:00
taosThreadRwlockInit(&pTsdb->rwLock, NULL);
if (!pKeepCfg) {
2022-07-30 11:26:10 +00:00
tsdbSetKeepCfg(pTsdb, &pVnode->config.tsdbCfg);
} else {
memcpy(&pTsdb->keepCfg, pKeepCfg, sizeof(STsdbKeepCfg));
}
2022-04-27 07:04:56 +00:00
2022-06-23 08:34:18 +00:00
// create dir
2022-10-10 13:29:36 +00:00
if (pVnode->pTfs) {
tfsMkdir(pVnode->pTfs, pTsdb->path);
} else {
taosMkDir(pTsdb->path);
}
2022-04-27 07:04:56 +00:00
// open tsdb
2022-10-18 05:38:26 +00:00
if (tsdbFSOpen(pTsdb, rollback) < 0) {
2022-04-27 07:04:56 +00:00
goto _err;
}
if (tsdbOpenCache(pTsdb) < 0) {
goto _err;
}
2022-08-02 07:57:37 +00:00
tsdbDebug("vgId:%d, tsdb is opened at %s, days:%d, keep:%d,%d,%d", TD_VID(pVnode), pTsdb->path, pTsdb->keepCfg.days,
2022-05-17 15:33:59 +00:00
pTsdb->keepCfg.keep0, pTsdb->keepCfg.keep1, pTsdb->keepCfg.keep2);
2022-04-27 07:04:56 +00:00
*ppTsdb = pTsdb;
return 0;
_err:
taosMemoryFree(pTsdb);
return -1;
}
int tsdbClose(STsdb **pTsdb) {
if (*pTsdb) {
2022-10-14 05:34:25 +00:00
taosThreadRwlockWrlock(&(*pTsdb)->rwLock);
tsdbMemTableDestroy((*pTsdb)->mem);
(*pTsdb)->mem = NULL;
2022-10-14 05:34:25 +00:00
taosThreadRwlockUnlock(&(*pTsdb)->rwLock);
taosThreadRwlockDestroy(&(*pTsdb)->rwLock);
2022-07-21 11:42:42 +00:00
tsdbFSClose(*pTsdb);
2022-08-18 06:32:11 +00:00
tsdbCloseCache(*pTsdb);
2022-06-25 03:46:05 +00:00
taosMemoryFreeClear(*pTsdb);
2022-04-27 07:04:56 +00:00
}
return 0;
}