From c57b157461b50eb50ef908ccc80442f8d7dbe629 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Thu, 14 Apr 2022 12:03:15 +0800 Subject: [PATCH] refact(cluster): node mgmt --- source/dnode/mgmt/implement/src/dmEps.c | 13 ++++++------- source/dnode/mgmt/mgmt_mnode/src/mmFile.c | 19 +++++++++++++------ source/dnode/mgmt/mgmt_mnode/src/mmInt.c | 21 ++++++++++++++++++++- 3 files changed, 39 insertions(+), 14 deletions(-) diff --git a/source/dnode/mgmt/implement/src/dmEps.c b/source/dnode/mgmt/implement/src/dmEps.c index 6e8ee638dc..ae1cd513b5 100644 --- a/source/dnode/mgmt/implement/src/dmEps.c +++ b/source/dnode/mgmt/implement/src/dmEps.c @@ -45,7 +45,7 @@ int32_t dmReadEps(SDnode *pDnode) { int32_t maxLen = 256 * 1024; char *content = taosMemoryCalloc(1, maxLen + 1); cJSON *root = NULL; - char file[PATH_MAX]; + char file[PATH_MAX] = {0}; TdFilePtr pFile = NULL; pDnode->data.dnodeEps = taosArrayInit(1, sizeof(SDnodeEp)); @@ -54,7 +54,7 @@ int32_t dmReadEps(SDnode *pDnode) { goto PRASE_DNODE_OVER; } - snprintf(file, sizeof(file), "%s%sdnode.json", pDnode->data.dataDir, TD_DIRSEP); + snprintf(file, sizeof(file), "%s%sdnode.json", pDnode->wrappers[DNODE].path, TD_DIRSEP); pFile = taosOpenFile(file, TD_FILE_READ); if (pFile == NULL) { // dDebug("file %s not exist", file); @@ -175,8 +175,10 @@ PRASE_DNODE_OVER: } int32_t dmWriteEps(SDnode *pDnode) { - char file[PATH_MAX]; - snprintf(file, sizeof(file), "%s%sdnode.json.bak", pDnode->data.dataDir, TD_DIRSEP); + char file[PATH_MAX] = {0}; + char realfile[PATH_MAX]; + snprintf(file, sizeof(file), "%s%sdnode.json.bak", pDnode->wrappers[DNODE].path, TD_DIRSEP); + snprintf(realfile, sizeof(realfile), "%s%sdnode.json", pDnode->wrappers[DNODE].path, TD_DIRSEP); TdFilePtr pFile = taosOpenFile(file, TD_FILE_CREATE | TD_FILE_WRITE | TD_FILE_TRUNC); if (pFile == NULL) { @@ -215,9 +217,6 @@ int32_t dmWriteEps(SDnode *pDnode) { taosCloseFile(&pFile); taosMemoryFree(content); - char realfile[PATH_MAX]; - snprintf(realfile, sizeof(realfile), "%s%sdnode.json", pDnode->data.dataDir, TD_DIRSEP); - if (taosRenameFile(file, realfile) != 0) { terrno = TAOS_SYSTEM_ERROR(errno); dError("failed to rename %s since %s", file, terrstr()); diff --git a/source/dnode/mgmt/mgmt_mnode/src/mmFile.c b/source/dnode/mgmt/mgmt_mnode/src/mmFile.c index 9724cc04a2..baf16f591d 100644 --- a/source/dnode/mgmt/mgmt_mnode/src/mmFile.c +++ b/source/dnode/mgmt/mgmt_mnode/src/mmFile.c @@ -121,22 +121,29 @@ int32_t mmWriteFile(SMgmtWrapper *pWrapper, SDCreateMnodeReq *pReq, bool deploye int32_t len = 0; int32_t maxLen = 4096; char *content = taosMemoryCalloc(1, maxLen + 1); - + len += snprintf(content + len, maxLen - len, "{\n"); - if (pReq != NULL) { - len += snprintf(content + len, maxLen - len, " \"mnodes\": [{\n"); - for (int32_t i = 0; i < pReq->replica; ++i) { - SReplica *pReplica = &pReq->replicas[i]; + len += snprintf(content + len, maxLen - len, " \"mnodes\": [{\n"); + + SMnodeMgmt *pMgmt = pWrapper->pMgmt; + if (pReq != NULL || pMgmt != NULL) { + int8_t replica = (pReq != NULL ? pReq->replica : pMgmt->replica); + for (int32_t i = 0; i < replica; ++i) { + SReplica *pReplica = &pMgmt->replicas[i]; + if (pReq != NULL) { + pReplica = &pReq->replicas[i]; + } len += snprintf(content + len, maxLen - len, " \"id\": %d,\n", pReplica->id); len += snprintf(content + len, maxLen - len, " \"fqdn\": \"%s\",\n", pReplica->fqdn); len += snprintf(content + len, maxLen - len, " \"port\": %u\n", pReplica->port); - if (i < pReq->replica - 1) { + if (i < replica - 1) { len += snprintf(content + len, maxLen - len, " },{\n"); } else { len += snprintf(content + len, maxLen - len, " }],\n"); } } } + len += snprintf(content + len, maxLen - len, " \"deployed\": %d\n", deployed); len += snprintf(content + len, maxLen - len, "}\n"); diff --git a/source/dnode/mgmt/mgmt_mnode/src/mmInt.c b/source/dnode/mgmt/mgmt_mnode/src/mmInt.c index 35ffe86735..69b4d50939 100644 --- a/source/dnode/mgmt/mgmt_mnode/src/mmInt.c +++ b/source/dnode/mgmt/mgmt_mnode/src/mmInt.c @@ -103,7 +103,18 @@ int32_t mmAlter(SMnodeMgmt *pMgmt, SDAlterMnodeReq *pReq) { if (mmBuildOptionFromReq(pMgmt, &option, pReq) != 0) { return -1; } - return mndAlter(pMgmt->pMnode, &option); + + if (mndAlter(pMgmt->pMnode, &option) != 0) { + return -1; + } + + bool deployed = true; + if (mmWriteFile(pMgmt->pWrapper, pReq, deployed) != 0) { + dError("failed to write mnode file since %s", terrstr()); + return -1; + } + + return 0; } static void mmClose(SMgmtWrapper *pWrapper) { @@ -172,6 +183,14 @@ static int32_t mmOpen(SMgmtWrapper *pWrapper) { return -1; } + if (!deployed) { + deployed = true; + if (mmWriteFile(pWrapper, NULL, deployed) != 0) { + dError("failed to write mnode file since %s", terrstr()); + return -1; + } + } + dInfo("mnode-mgmt is initialized"); return 0; }