TDengine/source/dnode/vnode/src/vnd/vnodeOpen.c

419 lines
13 KiB
C
Raw Normal View History

2021-11-07 07:58:32 +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/>.
*/
2022-04-26 11:04:26 +00:00
#include "vnd.h"
2021-11-07 07:58:32 +00:00
2022-10-20 08:47:03 +00:00
int32_t vnodeCreate(const char *path, SVnodeCfg *pCfg, STfs *pTfs) {
2022-04-15 05:47:57 +00:00
SVnodeInfo info = {0};
2022-10-20 08:47:03 +00:00
char dir[TSDB_FILENAME_LEN] = {0};
2022-04-14 12:36:35 +00:00
// check config
if (vnodeCheckCfg(pCfg) < 0) {
2022-08-02 07:57:37 +00:00
vError("vgId:%d, failed to create vnode since:%s", pCfg->vgId, tstrerror(terrno));
2022-04-14 12:36:35 +00:00
return -1;
}
// create vnode env
2022-10-10 10:17:58 +00:00
if (pTfs) {
if (tfsMkdirAt(pTfs, path, (SDiskID){0}) < 0) {
vError("vgId:%d, failed to create vnode since:%s", pCfg->vgId, tstrerror(terrno));
return -1;
}
snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pTfs), TD_DIRSEP, path);
} else {
if (taosMkDir(path)) {
return TAOS_SYSTEM_ERROR(errno);
}
2022-10-14 05:34:25 +00:00
snprintf(dir, TSDB_FILENAME_LEN, "%s", path);
2022-04-15 05:47:57 +00:00
}
2022-10-10 10:17:58 +00:00
if (pCfg) {
info.config = *pCfg;
} else {
info.config = vnodeCfgDefault;
}
2022-04-20 11:58:36 +00:00
info.state.committed = -1;
info.state.applied = -1;
2022-06-23 03:08:19 +00:00
info.state.commitID = 0;
2022-04-15 05:47:57 +00:00
vInfo("vgId:%d, save config while create", pCfg->vgId);
2023-02-10 08:24:29 +00:00
if (vnodeSaveInfo(dir, &info) < 0 || vnodeCommitInfo(dir) < 0) {
2022-10-14 05:34:25 +00:00
vError("vgId:%d, failed to save vnode config since %s", pCfg ? pCfg->vgId : 0, tstrerror(terrno));
2022-04-14 12:36:35 +00:00
return -1;
}
2022-10-10 10:17:58 +00:00
vInfo("vgId:%d, vnode is created", info.config.vgId);
2022-10-20 08:47:03 +00:00
return 0;
}
2023-01-30 09:46:42 +00:00
int32_t vnodeAlterReplica(const char *path, SAlterVnodeReplicaReq *pReq, STfs *pTfs) {
2022-10-20 08:47:03 +00:00
SVnodeInfo info = {0};
char dir[TSDB_FILENAME_LEN] = {0};
int32_t ret = 0;
if (pTfs) {
snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pTfs), TD_DIRSEP, path);
} else {
snprintf(dir, TSDB_FILENAME_LEN, "%s", path);
}
2022-04-15 06:27:04 +00:00
2022-10-20 08:47:03 +00:00
ret = vnodeLoadInfo(dir, &info);
if (ret < 0) {
vError("vgId:%d, failed to read vnode config from %s since %s", pReq->vgId, path, tstrerror(terrno));
return -1;
}
SSyncCfg *pCfg = &info.config.syncCfg;
pCfg->myIndex = pReq->selfIndex;
pCfg->replicaNum = pReq->replica;
memset(&pCfg->nodeInfo, 0, sizeof(pCfg->nodeInfo));
vInfo("vgId:%d, save config while alter, replicas:%d selfIndex:%d", pReq->vgId, pCfg->replicaNum, pCfg->myIndex);
2022-10-20 08:47:03 +00:00
for (int i = 0; i < pReq->replica; ++i) {
SNodeInfo *pNode = &pCfg->nodeInfo[i];
pNode->nodeId = pReq->replicas[i].id;
2022-10-20 08:47:03 +00:00
pNode->nodePort = pReq->replicas[i].port;
tstrncpy(pNode->nodeFqdn, pReq->replicas[i].fqdn, sizeof(pNode->nodeFqdn));
(void)tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
vInfo("vgId:%d, replica:%d ep:%s:%u dnode:%d", pReq->vgId, i, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
2022-10-20 08:47:03 +00:00
}
2022-10-24 03:57:26 +00:00
info.config.syncCfg = *pCfg;
2022-10-20 08:47:03 +00:00
ret = vnodeSaveInfo(dir, &info);
if (ret < 0) {
vError("vgId:%d, failed to save vnode config since %s", pReq->vgId, tstrerror(terrno));
return -1;
}
2023-02-10 08:24:29 +00:00
ret = vnodeCommitInfo(dir);
2022-10-24 03:57:26 +00:00
if (ret < 0) {
vError("vgId:%d, failed to commit vnode config since %s", pReq->vgId, tstrerror(terrno));
return -1;
}
2022-10-20 08:47:03 +00:00
vInfo("vgId:%d, vnode config is saved", info.config.vgId);
2022-04-14 12:36:35 +00:00
return 0;
}
2023-01-31 08:28:28 +00:00
int32_t vnodeRenameVgroupId(const char *srcPath, const char *dstPath, int32_t srcVgId, int32_t dstVgId, STfs *pTfs) {
int32_t ret = tfsRename(pTfs, srcPath, dstPath);
if (ret != 0) return ret;
char oldRname[TSDB_FILENAME_LEN] = {0};
char newRname[TSDB_FILENAME_LEN] = {0};
char tsdbPath[TSDB_FILENAME_LEN] = {0};
char tsdbFilePrefix[TSDB_FILENAME_LEN] = {0};
snprintf(tsdbPath, TSDB_FILENAME_LEN, "%s%stsdb", dstPath, TD_DIRSEP);
snprintf(tsdbFilePrefix, TSDB_FILENAME_LEN, "tsdb%sv", TD_DIRSEP);
STfsDir *tsdbDir = tfsOpendir(pTfs, tsdbPath);
if (tsdbDir == NULL) return 0;
while (1) {
const STfsFile *tsdbFile = tfsReaddir(tsdbDir);
if (tsdbFile == NULL) break;
if (tsdbFile->rname == NULL) continue;
tstrncpy(oldRname, tsdbFile->rname, TSDB_FILENAME_LEN);
char *tsdbFilePrefixPos = strstr(oldRname, tsdbFilePrefix);
if (tsdbFilePrefixPos == NULL) continue;
int32_t tsdbFileVgId = atoi(tsdbFilePrefixPos + 6);
if (tsdbFileVgId == srcVgId) {
char *tsdbFileSurfixPos = strstr(tsdbFilePrefixPos, "f");
if (tsdbFileSurfixPos == NULL) continue;
tsdbFilePrefixPos[6] = 0;
snprintf(newRname, TSDB_FILENAME_LEN, "%s%d%s", oldRname, dstVgId, tsdbFileSurfixPos);
vInfo("vgId:%d, rename file from %s to %s", dstVgId, tsdbFile->rname, newRname);
ret = tfsRename(pTfs, tsdbFile->rname, newRname);
if (ret != 0) {
vInfo("vgId:%d, failed to rename file from %s to %s since %s", dstVgId, tsdbFile->rname, newRname, terrstr());
tfsClosedir(tsdbDir);
return ret;
}
}
}
tfsClosedir(tsdbDir);
return 0;
}
2023-01-30 09:46:42 +00:00
int32_t vnodeAlterHashRange(const char *srcPath, const char *dstPath, SAlterVnodeHashRangeReq *pReq, STfs *pTfs) {
SVnodeInfo info = {0};
char dir[TSDB_FILENAME_LEN] = {0};
int32_t ret = 0;
if (pTfs) {
snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pTfs), TD_DIRSEP, srcPath);
} else {
snprintf(dir, TSDB_FILENAME_LEN, "%s", srcPath);
}
2023-01-31 08:40:53 +00:00
// todo add stat file to handle exception while vnode open
2023-01-30 09:46:42 +00:00
ret = vnodeLoadInfo(dir, &info);
if (ret < 0) {
vError("vgId:%d, failed to read vnode config from %s since %s", pReq->srcVgId, srcPath, tstrerror(terrno));
return -1;
}
2023-02-03 06:40:12 +00:00
vInfo("vgId:%d, alter hashrange from [%u, %u] to [%u, %u]", pReq->srcVgId, info.config.hashBegin, info.config.hashEnd,
pReq->hashBegin, pReq->hashEnd);
2023-01-30 09:46:42 +00:00
info.config.vgId = pReq->dstVgId;
info.config.hashBegin = pReq->hashBegin;
info.config.hashEnd = pReq->hashEnd;
info.config.walCfg.vgId = pReq->dstVgId;
SSyncCfg *pCfg = &info.config.syncCfg;
pCfg->myIndex = 0;
pCfg->replicaNum = 1;
memset(&pCfg->nodeInfo, 0, sizeof(pCfg->nodeInfo));
vInfo("vgId:%d, alter vnode replicas to 1", pReq->srcVgId);
SNodeInfo *pNode = &pCfg->nodeInfo[0];
pNode->nodePort = tsServerPort;
tstrncpy(pNode->nodeFqdn, tsLocalFqdn, TSDB_FQDN_LEN);
(void)tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort);
vInfo("vgId:%d, ep:%s:%u dnode:%d", pReq->srcVgId, pNode->nodeFqdn, pNode->nodePort, pNode->nodeId);
info.config.syncCfg = *pCfg;
ret = vnodeSaveInfo(dir, &info);
if (ret < 0) {
vError("vgId:%d, failed to save vnode config since %s", pReq->dstVgId, tstrerror(terrno));
return -1;
}
2023-02-10 08:24:29 +00:00
ret = vnodeCommitInfo(dir);
2023-01-30 09:46:42 +00:00
if (ret < 0) {
vError("vgId:%d, failed to commit vnode config since %s", pReq->dstVgId, tstrerror(terrno));
return -1;
}
2023-02-03 06:40:12 +00:00
vInfo("vgId:%d, rename %s to %s", pReq->dstVgId, srcPath, dstPath);
2023-01-31 08:28:28 +00:00
ret = vnodeRenameVgroupId(srcPath, dstPath, pReq->srcVgId, pReq->dstVgId, pTfs);
2023-01-30 09:46:42 +00:00
if (ret < 0) {
vError("vgId:%d, failed to rename vnode from %s to %s since %s", pReq->dstVgId, srcPath, dstPath,
tstrerror(terrno));
return -1;
}
2023-01-31 08:40:53 +00:00
// todo vnode compact here
2023-01-31 02:35:27 +00:00
2023-01-30 09:46:42 +00:00
vInfo("vgId:%d, vnode hashrange is altered", info.config.vgId);
return 0;
}
2022-10-20 08:47:03 +00:00
void vnodeDestroy(const char *path, STfs *pTfs) {
vInfo("path:%s is removed while destroy vnode", path);
tfsRmdir(pTfs, path);
}
2022-04-16 07:04:25 +00:00
2022-04-16 07:16:10 +00:00
SVnode *vnodeOpen(const char *path, STfs *pTfs, SMsgCb msgCb) {
SVnode *pVnode = NULL;
SVnodeInfo info = {0};
2022-10-20 08:47:03 +00:00
char dir[TSDB_FILENAME_LEN] = {0};
char tdir[TSDB_FILENAME_LEN * 2] = {0};
int32_t ret = 0;
2021-11-08 02:04:24 +00:00
2022-10-10 10:29:14 +00:00
if (pTfs) {
snprintf(dir, TSDB_FILENAME_LEN, "%s%s%s", tfsGetPrimaryPath(pTfs), TD_DIRSEP, path);
} else {
snprintf(dir, TSDB_FILENAME_LEN, "%s", path);
}
2022-04-16 07:16:10 +00:00
2022-09-08 08:56:29 +00:00
info.config = vnodeCfgDefault;
2022-04-16 07:16:10 +00:00
// load vnode info
ret = vnodeLoadInfo(dir, &info);
if (ret < 0) {
vError("failed to open vnode from %s since %s", path, tstrerror(terrno));
2021-11-08 02:04:24 +00:00
return NULL;
}
// save vnode info on dnode ep changed
bool updated = false;
SSyncCfg *pCfg = &info.config.syncCfg;
for (int32_t i = 0; i < pCfg->replicaNum; ++i) {
SNodeInfo *pNode = &pCfg->nodeInfo[i];
if (tmsgUpdateDnodeInfo(&pNode->nodeId, &pNode->clusterId, pNode->nodeFqdn, &pNode->nodePort)) {
updated = true;
}
}
if (updated) {
vInfo("vgId:%d, save vnode info since dnode info changed", info.config.vgId);
(void)vnodeSaveInfo(dir, &info);
2023-02-10 08:24:29 +00:00
(void)vnodeCommitInfo(dir);
}
2022-04-16 08:19:21 +00:00
// create handle
2022-10-20 08:47:03 +00:00
pVnode = taosMemoryCalloc(1, sizeof(*pVnode) + strlen(path) + 1);
2021-11-08 02:04:24 +00:00
if (pVnode == NULL) {
2022-04-16 08:19:21 +00:00
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-06-02 05:57:39 +00:00
vError("vgId:%d, failed to open vnode since %s", info.config.vgId, tstrerror(terrno));
2021-11-08 02:04:24 +00:00
return NULL;
}
2022-04-16 11:22:49 +00:00
pVnode->path = (char *)&pVnode[1];
strcpy(pVnode->path, path);
2022-04-16 08:19:21 +00:00
pVnode->config = info.config;
2022-05-21 03:13:58 +00:00
pVnode->state.committed = info.state.committed;
2022-07-06 09:46:14 +00:00
pVnode->state.commitTerm = info.state.commitTerm;
2022-06-23 03:08:19 +00:00
pVnode->state.commitID = info.state.commitID;
pVnode->state.applied = info.state.committed;
pVnode->state.applyTerm = info.state.commitTerm;
2022-04-16 08:19:21 +00:00
pVnode->pTfs = pTfs;
pVnode->msgCb = msgCb;
taosThreadMutexInit(&pVnode->lock, NULL);
pVnode->blocked = false;
2022-04-16 08:19:21 +00:00
2022-06-08 06:44:42 +00:00
tsem_init(&pVnode->syncSem, 0, 0);
2022-04-16 08:19:21 +00:00
tsem_init(&(pVnode->canCommit), 0, 1);
2022-07-19 08:30:49 +00:00
taosThreadMutexInit(&pVnode->mutex, NULL);
taosThreadCondInit(&pVnode->poolNotEmpty, NULL);
2022-04-16 08:19:21 +00:00
vnodeUpdCommitSched(pVnode);
2022-10-18 05:38:26 +00:00
int8_t rollback = vnodeShouldRollback(pVnode);
2022-04-16 10:17:33 +00:00
// open buffer pool
2022-10-09 05:52:44 +00:00
if (vnodeOpenBufPool(pVnode) < 0) {
2022-06-02 05:57:39 +00:00
vError("vgId:%d, failed to open vnode buffer pool since %s", TD_VID(pVnode), tstrerror(terrno));
2022-04-16 10:17:33 +00:00
goto _err;
2021-11-08 06:50:20 +00:00
}
2022-04-16 10:17:33 +00:00
// open meta
2022-10-18 05:38:26 +00:00
if (metaOpen(pVnode, &pVnode->pMeta, rollback) < 0) {
2022-06-02 05:57:39 +00:00
vError("vgId:%d, failed to open vnode meta since %s", TD_VID(pVnode), tstrerror(terrno));
2022-04-16 10:17:33 +00:00
goto _err;
2021-11-08 05:40:45 +00:00
}
2022-04-16 10:17:33 +00:00
// open tsdb
2022-10-18 05:38:26 +00:00
if (!VND_IS_RSMA(pVnode) && tsdbOpen(pVnode, &VND_TSDB(pVnode), VNODE_TSDB_DIR, NULL, rollback) < 0) {
2022-06-02 05:57:39 +00:00
vError("vgId:%d, failed to open vnode tsdb since %s", TD_VID(pVnode), tstrerror(terrno));
goto _err;
}
// open sma
2022-10-18 05:38:26 +00:00
if (smaOpen(pVnode, rollback)) {
2022-06-02 05:57:39 +00:00
vError("vgId:%d, failed to open vnode sma since %s", TD_VID(pVnode), tstrerror(terrno));
goto _err;
2021-11-08 05:40:45 +00:00
}
2022-04-16 10:17:33 +00:00
// open wal
sprintf(tdir, "%s%s%s", dir, TD_DIRSEP, VNODE_WAL_DIR);
2022-05-19 07:18:18 +00:00
taosRealPath(tdir, NULL, sizeof(tdir));
2022-04-16 10:17:33 +00:00
pVnode->pWal = walOpen(tdir, &(pVnode->config.walCfg));
2021-11-16 07:49:05 +00:00
if (pVnode->pWal == NULL) {
vError("vgId:%d, failed to open vnode wal since %s. wal:%s", TD_VID(pVnode), tstrerror(terrno), tdir);
2022-04-16 10:17:33 +00:00
goto _err;
2021-11-16 07:49:05 +00:00
}
2021-11-08 05:40:45 +00:00
2022-04-16 10:17:33 +00:00
// open tq
sprintf(tdir, "%s%s%s", dir, TD_DIRSEP, VNODE_TQ_DIR);
2022-05-19 07:18:18 +00:00
taosRealPath(tdir, NULL, sizeof(tdir));
2022-07-22 08:05:28 +00:00
pVnode->pTq = tqOpen(tdir, pVnode);
2022-01-20 07:39:28 +00:00
if (pVnode->pTq == NULL) {
2022-06-02 05:57:39 +00:00
vError("vgId:%d, failed to open vnode tq since %s", TD_VID(pVnode), tstrerror(terrno));
2022-04-16 10:17:33 +00:00
goto _err;
2022-01-20 07:39:28 +00:00
}
2022-04-16 10:17:33 +00:00
// open query
2021-12-24 01:41:09 +00:00
if (vnodeQueryOpen(pVnode)) {
2022-06-02 05:57:39 +00:00
vError("vgId:%d, failed to open vnode query since %s", TD_VID(pVnode), tstrerror(terrno));
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-04-16 10:17:33 +00:00
goto _err;
2021-12-24 01:41:09 +00:00
}
2022-04-19 13:10:03 +00:00
// vnode begin
if (vnodeBegin(pVnode) < 0) {
2022-06-02 05:57:39 +00:00
vError("vgId:%d, failed to begin since %s", TD_VID(pVnode), tstrerror(terrno));
terrno = TSDB_CODE_OUT_OF_MEMORY;
2022-04-16 10:17:33 +00:00
goto _err;
}
// open sync
if (vnodeSyncOpen(pVnode, dir)) {
2022-06-02 05:57:39 +00:00
vError("vgId:%d, failed to open sync since %s", TD_VID(pVnode), tstrerror(terrno));
2022-04-16 10:17:33 +00:00
goto _err;
}
2022-10-18 05:38:26 +00:00
if (rollback) {
vnodeRollback(pVnode);
}
2022-04-16 10:17:33 +00:00
return pVnode;
_err:
if (pVnode->pQuery) vnodeQueryClose(pVnode);
if (pVnode->pTq) tqClose(pVnode->pTq);
if (pVnode->pWal) walClose(pVnode->pWal);
if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
2022-07-03 12:38:54 +00:00
if (pVnode->pSma) smaClose(pVnode->pSma);
if (pVnode->pMeta) metaClose(&pVnode->pMeta);
2023-01-05 07:40:15 +00:00
if (pVnode->freeList) vnodeCloseBufPool(pVnode);
2022-06-26 10:44:49 +00:00
2022-04-16 10:17:33 +00:00
tsem_destroy(&(pVnode->canCommit));
taosMemoryFree(pVnode);
return NULL;
2021-11-08 02:17:51 +00:00
}
2022-12-01 12:26:56 +00:00
void vnodePreClose(SVnode *pVnode) {
2022-11-11 09:13:55 +00:00
vnodeQueryPreClose(pVnode);
2022-12-01 12:26:56 +00:00
vnodeSyncPreClose(pVnode);
2022-11-11 09:13:55 +00:00
}
void vnodePostClose(SVnode *pVnode) { vnodeSyncPostClose(pVnode); }
2022-04-16 10:17:33 +00:00
void vnodeClose(SVnode *pVnode) {
2021-11-08 05:40:45 +00:00
if (pVnode) {
tsem_wait(&pVnode->canCommit);
2022-04-20 02:54:27 +00:00
vnodeSyncClose(pVnode);
2022-03-09 08:13:46 +00:00
vnodeQueryClose(pVnode);
2022-04-16 10:17:33 +00:00
walClose(pVnode->pWal);
tqClose(pVnode->pTq);
if (pVnode->pTsdb) tsdbClose(&pVnode->pTsdb);
2022-07-03 12:38:54 +00:00
smaClose(pVnode->pSma);
if (pVnode->pMeta) metaClose(&pVnode->pMeta);
2022-04-16 10:17:33 +00:00
vnodeCloseBufPool(pVnode);
2022-12-23 12:16:23 +00:00
tsem_post(&pVnode->canCommit);
2022-04-16 10:17:33 +00:00
// destroy handle
tsem_destroy(&(pVnode->canCommit));
2022-06-08 06:44:42 +00:00
tsem_destroy(&pVnode->syncSem);
2022-07-19 08:30:49 +00:00
taosThreadCondDestroy(&pVnode->poolNotEmpty);
taosThreadMutexDestroy(&pVnode->mutex);
taosThreadMutexDestroy(&pVnode->lock);
2022-04-16 10:17:33 +00:00
taosMemoryFree(pVnode);
2021-11-08 05:40:45 +00:00
}
2022-01-20 07:18:33 +00:00
}
2022-04-19 09:07:42 +00:00
2022-04-25 07:39:52 +00:00
// start the sync timer after the queue is ready
int32_t vnodeStart(SVnode *pVnode) { return vnodeSyncStart(pVnode); }
2022-04-25 07:39:52 +00:00
void vnodeStop(SVnode *pVnode) {}
2022-04-20 11:30:18 +00:00
int64_t vnodeGetSyncHandle(SVnode *pVnode) { return pVnode->sync; }
2022-07-06 09:46:14 +00:00
void vnodeGetSnapshot(SVnode *pVnode, SSnapshot *pSnapshot) {
pSnapshot->data = NULL;
pSnapshot->lastApplyIndex = pVnode->state.committed;
pSnapshot->lastApplyTerm = pVnode->state.commitTerm;
pSnapshot->lastConfigIndex = -1;
}