TDengine/source/dnode/mgmt/mgmt_mnode/src/mmInt.c

192 lines
5.6 KiB
C
Raw Normal View History

2022-03-14 02: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/>.
*/
#define _DEFAULT_SOURCE
#include "mmInt.h"
2022-03-24 10:32:14 +00:00
#include "wal.h"
2022-03-16 11:25:33 +00:00
2022-05-11 09:38:00 +00:00
static bool mmDeployRequired(const SMgmtInputOpt *pInput) {
2022-05-14 10:16:52 +00:00
if (pInput->pData->dnodeId > 0) return false;
if (pInput->pData->clusterId > 0) return false;
2022-05-16 13:03:20 +00:00
if (strcmp(tsLocalEp, tsFirst) != 0) return false;
2022-03-18 09:01:00 +00:00
return true;
2022-03-15 09:00:16 +00:00
}
2022-05-11 09:38:00 +00:00
static int32_t mmRequire(const SMgmtInputOpt *pInput, bool *required) {
2024-07-16 10:01:09 +00:00
int32_t code = 0;
SMnodeOpt option = {0};
2024-07-16 10:01:09 +00:00
if ((code = mmReadFile(pInput->path, &option)) != 0) {
return code;
2022-03-15 09:00:16 +00:00
}
if (!option.deploy) {
2022-05-11 09:38:00 +00:00
*required = mmDeployRequired(pInput);
2023-10-18 08:58:58 +00:00
if (*required) {
2025-02-26 03:34:50 +00:00
dInfo("deploy mnode required. dnodeId:%d<=0, clusterId:0x%" PRIx64 "<=0, localEp:%s==firstEp",
2023-10-18 08:58:58 +00:00
pInput->pData->dnodeId, pInput->pData->clusterId, tsLocalEp);
}
} else {
*required = true;
2023-10-18 08:58:58 +00:00
dInfo("deploy mnode required. option deploy:%d", option.deploy);
2022-03-15 09:00:16 +00:00
}
2024-07-16 10:01:09 +00:00
return code;
2022-03-15 09:00:16 +00:00
}
2022-05-11 09:38:00 +00:00
static void mmBuildOptionForDeploy(SMnodeMgmt *pMgmt, const SMgmtInputOpt *pInput, SMnodeOpt *pOption) {
2022-05-24 14:18:11 +00:00
pOption->deploy = true;
2022-05-11 09:38:00 +00:00
pOption->msgCb = pMgmt->msgCb;
2022-05-25 08:41:38 +00:00
pOption->dnodeId = pMgmt->pData->dnodeId;
pOption->selfIndex = 0;
pOption->numOfReplicas = 1;
2023-04-18 11:03:45 +00:00
pOption->numOfTotalReplicas = 1;
pOption->replicas[0].id = 1;
pOption->replicas[0].port = tsServerPort;
tstrncpy(pOption->replicas[0].fqdn, tsLocalFqdn, TSDB_FQDN_LEN);
2023-04-18 11:03:45 +00:00
pOption->lastIndex = SYNC_INDEX_INVALID;
2022-03-15 09:00:16 +00:00
}
static void mmBuildOptionForOpen(SMnodeMgmt *pMgmt, SMnodeOpt *pOption) {
2022-06-14 01:43:22 +00:00
pOption->deploy = false;
2022-05-25 08:41:38 +00:00
pOption->msgCb = pMgmt->msgCb;
pOption->dnodeId = pMgmt->pData->dnodeId;
2022-03-15 09:00:16 +00:00
}
2022-05-11 09:38:00 +00:00
static void mmClose(SMnodeMgmt *pMgmt) {
2022-04-14 03:33:31 +00:00
if (pMgmt->pMnode != NULL) {
mmStopWorker(pMgmt);
mndClose(pMgmt->pMnode);
2024-07-29 11:41:44 +00:00
(void)taosThreadRwlockDestroy(&pMgmt->lock);
2022-04-14 03:33:31 +00:00
pMgmt->pMnode = NULL;
}
2022-03-25 16:29:53 +00:00
taosMemoryFree(pMgmt);
2022-03-18 09:01:00 +00:00
}
2024-07-16 10:01:09 +00:00
static int32_t mndOpenWrapper(const char *path, SMnodeOpt *opt, SMnode **pMnode) {
int32_t code = 0;
*pMnode = mndOpen(path, opt);
if (*pMnode == NULL) {
code = terrno;
}
///*pMnode = pNode;
return code;
}
2022-05-14 10:16:52 +00:00
static int32_t mmOpen(SMgmtInputOpt *pInput, SMgmtOutputOpt *pOutput) {
2024-07-16 10:01:09 +00:00
int32_t code = 0;
2024-08-15 03:55:20 +00:00
if ((code = walInit(pInput->stopDnodeFp)) != 0) {
2024-07-16 10:01:09 +00:00
dError("failed to init wal since %s", tstrerror(code));
return code;
2022-03-15 09:00:16 +00:00
}
2024-07-16 10:01:09 +00:00
if ((code = syncInit()) != 0) {
dError("failed to init sync since %s", tstrerror(code));
return code;
2022-05-21 13:26:27 +00:00
}
2022-03-25 16:29:53 +00:00
SMnodeMgmt *pMgmt = taosMemoryCalloc(1, sizeof(SMnodeMgmt));
2022-03-18 09:01:00 +00:00
if (pMgmt == NULL) {
code = terrno;
2024-07-16 10:01:09 +00:00
return code;
2022-03-15 09:00:16 +00:00
}
2022-05-16 13:36:29 +00:00
pMgmt->pData = pInput->pData;
2022-05-11 09:38:00 +00:00
pMgmt->path = pInput->path;
pMgmt->name = pInput->name;
pMgmt->msgCb = pInput->msgCb;
2022-06-14 01:43:22 +00:00
pMgmt->msgCb.putToQueueFp = (PutToQueueFp)mmPutMsgToQueue;
2022-05-16 15:23:49 +00:00
pMgmt->msgCb.mgmt = pMgmt;
2024-07-29 11:41:44 +00:00
(void)taosThreadRwlockInit(&pMgmt->lock, NULL);
2022-03-15 09:00:16 +00:00
SMnodeOpt option = {0};
2024-07-16 10:01:09 +00:00
if ((code = mmReadFile(pMgmt->path, &option)) != 0) {
dError("failed to read file since %s", tstrerror(code));
2022-05-11 09:38:00 +00:00
mmClose(pMgmt);
2024-07-16 10:01:09 +00:00
return code;
2022-04-14 03:33:31 +00:00
}
if (!option.deploy) {
2022-04-14 03:33:31 +00:00
dInfo("mnode start to deploy");
2022-05-16 13:36:29 +00:00
pMgmt->pData->dnodeId = 1;
2022-05-11 09:38:00 +00:00
mmBuildOptionForDeploy(pMgmt, pInput, &option);
2022-03-18 09:01:00 +00:00
} else {
2022-04-14 03:33:31 +00:00
dInfo("mnode start to open");
mmBuildOptionForOpen(pMgmt, &option);
2022-03-15 09:00:16 +00:00
}
2024-07-16 10:01:09 +00:00
code = mndOpenWrapper(pMgmt->path, &option, &pMgmt->pMnode);
if (code != 0) {
dError("failed to open mnode since %s", tstrerror(code));
2022-05-11 09:38:00 +00:00
mmClose(pMgmt);
2024-07-16 10:01:09 +00:00
return code;
2022-04-14 03:33:31 +00:00
}
2022-05-11 09:38:00 +00:00
tmsgReportStartup("mnode-impl", "initialized");
2022-03-18 09:01:00 +00:00
2024-07-16 10:01:09 +00:00
if ((code = mmStartWorker(pMgmt)) != 0) {
dError("failed to start mnode worker since %s", tstrerror(code));
2022-05-11 09:38:00 +00:00
mmClose(pMgmt);
2024-07-16 10:01:09 +00:00
return code;
2022-04-14 03:33:31 +00:00
}
2022-05-11 09:38:00 +00:00
tmsgReportStartup("mnode-worker", "initialized");
2022-04-14 03:33:31 +00:00
2023-04-18 11:03:45 +00:00
if (option.numOfTotalReplicas > 0) {
option.deploy = true;
option.numOfReplicas = 0;
2023-04-18 11:03:45 +00:00
option.numOfTotalReplicas = 0;
2024-07-16 10:01:09 +00:00
if ((code = mmWriteFile(pMgmt->path, &option)) != 0) {
dError("failed to write mnode file since %s", tstrerror(code));
return code;
2022-04-14 04:03:15 +00:00
}
}
2022-05-16 13:36:29 +00:00
pInput->pData->dnodeId = pMgmt->pData->dnodeId;
2022-05-11 09:38:00 +00:00
pOutput->pMgmt = pMgmt;
2022-04-14 03:33:31 +00:00
return 0;
}
2022-03-14 07:51:09 +00:00
2022-05-11 09:38:00 +00:00
static int32_t mmStart(SMnodeMgmt *pMgmt) {
2022-03-24 11:59:37 +00:00
dDebug("mnode-mgmt start to run");
2022-03-19 16:25:37 +00:00
return mndStart(pMgmt->pMnode);
}
2022-05-11 09:38:00 +00:00
static void mmStop(SMnodeMgmt *pMgmt) {
2022-04-13 07:52:14 +00:00
dDebug("mnode-mgmt start to stop");
mndPreClose(pMgmt->pMnode);
2024-07-29 11:41:44 +00:00
(void)taosThreadRwlockWrlock(&pMgmt->lock);
2022-06-16 09:39:31 +00:00
pMgmt->stopped = 1;
2024-07-29 11:41:44 +00:00
(void)taosThreadRwlockUnlock(&pMgmt->lock);
2022-06-16 09:39:31 +00:00
2022-05-11 09:38:00 +00:00
mndStop(pMgmt->pMnode);
2022-04-13 07:52:14 +00:00
}
2024-07-16 10:01:09 +00:00
static int32_t mmSyncIsCatchUp(SMnodeMgmt *pMgmt) { return mndIsCatchUp(pMgmt->pMnode); }
2023-04-18 11:03:45 +00:00
2024-07-16 10:01:09 +00:00
static ESyncRole mmSyncGetRole(SMnodeMgmt *pMgmt) { return mndGetRole(pMgmt->pMnode); }
2022-05-11 09:38:00 +00:00
SMgmtFunc mmGetMgmtFunc() {
SMgmtFunc mgmtFunc = {0};
mgmtFunc.openFp = mmOpen;
mgmtFunc.closeFp = (NodeCloseFp)mmClose;
mgmtFunc.startFp = (NodeStartFp)mmStart;
mgmtFunc.stopFp = (NodeStopFp)mmStop;
mgmtFunc.createFp = (NodeCreateFp)mmProcessCreateReq;
mgmtFunc.dropFp = (NodeDropFp)mmProcessDropReq;
mgmtFunc.requiredFp = mmRequire;
mgmtFunc.getHandlesFp = mmGetMsgHandles;
2023-04-18 11:03:45 +00:00
mgmtFunc.isCatchUpFp = (NodeIsCatchUpFp)mmSyncIsCatchUp;
mgmtFunc.nodeRoleFp = (NodeRole)mmSyncGetRole;
2022-05-11 09:38:00 +00:00
return mgmtFunc;
2022-03-14 02:04:56 +00:00
}