From 35c656f91b7e348762c1d4cbc757ef56b0ad3957 Mon Sep 17 00:00:00 2001 From: Shuduo Sang Date: Thu, 16 Apr 2020 11:35:12 +0800 Subject: [PATCH 01/14] add test coverage support. [TD-105] --- tests/test-all.sh | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100755 tests/test-all.sh diff --git a/tests/test-all.sh b/tests/test-all.sh new file mode 100755 index 0000000000..8bd01119c4 --- /dev/null +++ b/tests/test-all.sh @@ -0,0 +1,39 @@ +#!/bin/bash + +# Color setting +RED='\033[0;31m' +GREEN='\033[1;32m' +GREEN_DARK='\033[0;32m' +GREEN_UNDERLINE='\033[4;32m' +NC='\033[0m' + +cd script +sudo ./test.sh 2>&1 | grep 'success\|failed' | tee out.txt + +total_success=`grep success out.txt | wc -l` + +if [ "$total_success" -gt "0" ]; then + total_success=`expr $total_success - 1` + echo -e "${GREEN} ### Total $total_success TSIM case(s) succeed! ### ${NC}" +fi + +total_failed=`grep failed out.txt | wc -l` +if [ "$total_failed" -ne "0" ]; then + echo -e "${RED} ### Total $total_failed TSIM case(s) failed! ### ${NC}" + exit $total_failed +fi + +cd ../pytest +sudo ./simpletest.sh 2>&1 | grep 'successfully executed\|failed' | tee pytest-out.txt +total_py_success=`grep 'successfully executed' pytest-out.txt | wc -l` + +if [ "$total_py_success" -gt "0" ]; then + echo -e "${GREEN} ### Total $total_py_success python case(s) succeed! ### ${NC}" +fi + +total_py_failed=`grep 'failed' pytest-out.txt | wc -l` +if [ "$total_py_failed" -ne "0" ]; then + echo -e "${RED} ### Total $total_py_failed python case(s) failed! ### ${NC}" + exit $total_py_failed +fi + From fe12d0fe110fb52d7bbc30d26dd6bfb449f297f8 Mon Sep 17 00:00:00 2001 From: slguan Date: Thu, 16 Apr 2020 17:17:03 +0800 Subject: [PATCH 02/14] add cluster codes --- src/dnode/src/dnodeMain.c | 4 + src/inc/dnode.h | 2 + src/inc/mnode.h | 15 +- src/inc/{taccount.h => tacct.h} | 20 +- src/mnode/inc/mgmtAcct.h | 44 ++ src/mnode/inc/mgmtDb.h | 2 +- src/{inc/tcluster.h => mnode/inc/mgmtDnode.h} | 27 +- src/mnode/src/mgmtAcct.c | 173 +++++- src/mnode/src/mgmtBalance.c | 10 +- src/mnode/src/mgmtDClient.c | 2 +- src/mnode/src/mgmtDb.c | 22 +- src/mnode/src/mgmtDnode.c | 576 +++++++++++++++--- src/mnode/src/mgmtMain.c | 12 +- src/mnode/src/mgmtProfile.c | 10 +- src/mnode/src/mgmtShell.c | 4 +- src/mnode/src/mgmtTable.c | 56 +- src/mnode/src/mgmtUser.c | 16 +- src/mnode/src/mgmtVgroup.c | 26 +- 18 files changed, 787 insertions(+), 234 deletions(-) rename src/inc/{taccount.h => tacct.h} (58%) create mode 100644 src/mnode/inc/mgmtAcct.h rename src/{inc/tcluster.h => mnode/inc/mgmtDnode.h} (60%) diff --git a/src/dnode/src/dnodeMain.c b/src/dnode/src/dnodeMain.c index 5fba941788..c80de0ce6e 100644 --- a/src/dnode/src/dnodeMain.c +++ b/src/dnode/src/dnodeMain.c @@ -229,3 +229,7 @@ static int32_t dnodeInitStorage() { } static void dnodeCleanupStorage() {} + +bool dnodeIsFirstDeploy() { + return strcmp(tsMasterIp, tsPrivateIp) == 0; +} \ No newline at end of file diff --git a/src/inc/dnode.h b/src/inc/dnode.h index db39906c68..25ec747ac9 100644 --- a/src/inc/dnode.h +++ b/src/inc/dnode.h @@ -44,6 +44,8 @@ void *dnodeAllocateRqueue(void *pVnode); void dnodeFreeRqueue(void *rqueue); void dnodeSendRpcWriteRsp(void *pVnode, void *param, int32_t code); +bool dnodeIsFirstDeploy(); + #ifdef __cplusplus } #endif diff --git a/src/inc/mnode.h b/src/inc/mnode.h index f0407aa9e4..a5817ac9df 100644 --- a/src/inc/mnode.h +++ b/src/inc/mnode.h @@ -60,14 +60,16 @@ typedef struct _dnode_obj { int32_t dnodeId; uint32_t privateIp; uint32_t publicIp; + uint16_t mnodeShellPort; + uint16_t mnodeDnodePort; + uint16_t dnodeShellPort; + uint16_t dnodeMnodePort; + uint16_t syncPort; uint32_t moduleStatus; int64_t createdTime; uint32_t lastAccess; int32_t openVnodes; - int32_t numOfTotalVnodes; // from dnode status msg, config information - uint32_t rack; - uint16_t idc; - uint16_t slot; + int32_t totalVnodes; // from dnode status msg, config information uint16_t numOfCores; // from dnode status msg int8_t alternativeRole; // from dnode status msg, 0-any, 1-mgmt, 2-dnode int8_t status; // set in balance function @@ -88,7 +90,6 @@ typedef struct _dnode_obj { typedef struct { int32_t dnodeId; - uint16_t port; uint32_t privateIp; uint32_t publicIp; } SVnodeGid; @@ -209,10 +210,10 @@ typedef struct _acct_obj { SAcctCfg cfg; int32_t acctId; int64_t createdTime; - int8_t dirty; + int8_t status; int8_t reserved[14]; int8_t updateEnd[1]; - int32_t refCount; + int32_t refCount; SAcctInfo acctInfo; pthread_mutex_t mutex; } SAcctObj; diff --git a/src/inc/taccount.h b/src/inc/tacct.h similarity index 58% rename from src/inc/taccount.h rename to src/inc/tacct.h index 18a974a574..52215fac52 100644 --- a/src/inc/taccount.h +++ b/src/inc/tacct.h @@ -20,27 +20,15 @@ extern "C" { #endif -struct _acct_obj; -struct _user_obj; -struct _db_obj; - typedef enum { - TSDB_ACCT_USER, - TSDB_ACCT_DB, - TSDB_ACCT_TABLE + ACCT_GRANT_USER, + ACCT_GRANT_DB, + ACCT_GRANT_TABLE } EAcctGrantType; int32_t acctInit(); void acctCleanUp(); -void *acctGetAcct(char *acctName); -void acctIncRef(struct _acct_obj *pAcct); -void acctReleaseAcct(struct _acct_obj *pAcct); -int32_t acctCheck(struct _acct_obj *pAcct, EAcctGrantType type); - -void acctAddDb(struct _acct_obj *pAcct, struct _db_obj *pDb); -void acctRemoveDb(struct _acct_obj *pAcct, struct _db_obj *pDb); -void acctAddUser(struct _acct_obj *pAcct, struct _user_obj *pUser); -void acctRemoveUser(struct _acct_obj *pAcct, struct _user_obj *pUser); +int32_t acctCheck(void *pAcct, EAcctGrantType type); #ifdef __cplusplus } diff --git a/src/mnode/inc/mgmtAcct.h b/src/mnode/inc/mgmtAcct.h new file mode 100644 index 0000000000..32d7aa7bfb --- /dev/null +++ b/src/mnode/inc/mgmtAcct.h @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef TDENGINE_MGMT_ACCT_H +#define TDENGINE_MGMT_ACCT_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "tacct.h" + +struct _acct_obj; +struct _user_obj; +struct _db_obj; + +int32_t mgmtInitAccts(); +void mgmtCleanUpAccts(); +void *mgmtGetAcct(char *acctName); +void mgmtIncAcctRef(struct _acct_obj *pAcct); +void mgmtDecAcctRef(struct _acct_obj *pAcct); + +void mgmtAddDbToAcct(struct _acct_obj *pAcct, struct _db_obj *pDb); +void mgmtDropDbFromAcct(struct _acct_obj *pAcct, struct _db_obj *pDb); +void mgmtAddUserToAcct(struct _acct_obj *pAcct, struct _user_obj *pUser); +void mgmtDropUserFromAcct(struct _acct_obj *pAcct, struct _user_obj *pUser); + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/mnode/inc/mgmtDb.h b/src/mnode/inc/mgmtDb.h index 22c92bff6d..0479b274bb 100644 --- a/src/mnode/inc/mgmtDb.h +++ b/src/mnode/inc/mgmtDb.h @@ -33,7 +33,7 @@ void mgmtCleanUpDbs(); SDbObj *mgmtGetDb(char *db); SDbObj *mgmtGetDbByTableId(char *db); void mgmtIncDbRef(SDbObj *pDb); -void mgmtReleaseDb(SDbObj *pDb); +void mgmtDecDbRef(SDbObj *pDb); bool mgmtCheckIsMonitorDB(char *db, char *monitordb); void mgmtDropAllDbs(SAcctObj *pAcct); diff --git a/src/inc/tcluster.h b/src/mnode/inc/mgmtDnode.h similarity index 60% rename from src/inc/tcluster.h rename to src/mnode/inc/mgmtDnode.h index a56285fe1c..f964222960 100644 --- a/src/inc/tcluster.h +++ b/src/mnode/inc/mgmtDnode.h @@ -33,21 +33,20 @@ enum _TAOS_DN_STATUS { TAOS_DN_STATUS_READY }; -int32_t clusterInit(); -void clusterCleanUp(); -char* clusterGetDnodeStatusStr(int32_t dnodeStatus); -bool clusterCheckModuleInDnode(struct _dnode_obj *pDnode, int moduleType); -void clusterMonitorDnodeModule(); +int32_t mgmtInitDnodes(); +void mgmtCleanupDnodes(); -int32_t clusterInitDnodes(); -void clusterCleanupDnodes(); -int32_t clusterGetDnodesNum(); -void * clusterGetNextDnode(void *pNode, struct _dnode_obj **pDnode); -void clusterReleaseDnode(struct _dnode_obj *pDnode); -void * clusterGetDnode(int32_t dnodeId); -void * clusterGetDnodeByIp(uint32_t ip); -void clusterUpdateDnode(struct _dnode_obj *pDnode); -int32_t clusterDropDnode(struct _dnode_obj *pDnode); +char* mgmtGetDnodeStatusStr(int32_t dnodeStatus); +bool mgmtCheckModuleInDnode(struct _dnode_obj *pDnode, int moduleType); +void mgmtMonitorDnodeModule(); + +int32_t mgmtGetDnodesNum(); +void * mgmtGetNextDnode(void *pNode, struct _dnode_obj **pDnode); +void mgmtReleaseDnode(struct _dnode_obj *pDnode); +void * mgmtGetDnode(int32_t dnodeId); +void * mgmtGetDnodeByIp(uint32_t ip); +void mgmtUpdateDnode(struct _dnode_obj *pDnode); +int32_t mgmtDropDnode(struct _dnode_obj *pDnode); #ifdef __cplusplus } diff --git a/src/mnode/src/mgmtAcct.c b/src/mnode/src/mgmtAcct.c index a22313c52e..4741888e60 100644 --- a/src/mnode/src/mgmtAcct.c +++ b/src/mnode/src/mgmtAcct.c @@ -16,49 +16,178 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taoserror.h" +#include "dnode.h" #include "mnode.h" -#include "taccount.h" +#include "mgmtAcct.h" #include "mgmtDb.h" +#include "mgmtSdb.h" #include "mgmtUser.h" -#ifndef _ACCOUNT +static void * tsAcctSdb = NULL; +static int32_t tsAcctUpdateSize; +static void mgmtCreateRootAcct(); -static SAcctObj tsAcctObj = {0}; - -int32_t acctInit() { - tsAcctObj.acctId = 0; - strcpy(tsAcctObj.user, "root"); +static int32_t mgmtActionAcctDestroy(SSdbOperDesc *pOper) { + SAcctObj *pAcct = pOper->pObj; + pthread_mutex_destroy(&pAcct->mutex); + tfree(pOper->pObj); return TSDB_CODE_SUCCESS; } -void acctCleanUp() {} -void *acctGetAcct(char *acctName) { return &tsAcctObj; } -void acctIncRef(struct _acct_obj *pAcct) {} -void acctReleaseAcct(SAcctObj *pAcct) {} -int32_t acctCheck(SAcctObj *pAcct, EAcctGrantType type) { return TSDB_CODE_SUCCESS; } +static int32_t mgmtAcctActionInsert(SSdbOperDesc *pOper) { + SAcctObj *pAcct = pOper->pObj; + memset(&pAcct->acctInfo, 0, sizeof(SAcctInfo)); + pthread_mutex_init(&pAcct->mutex, NULL); + return TSDB_CODE_SUCCESS; +} -#endif +static int32_t mgmtActionAcctDelete(SSdbOperDesc *pOper) { + SAcctObj *pAcct = pOper->pObj; + mgmtDropAllUsers(pAcct); + mgmtDropAllDbs(pAcct); + return TSDB_CODE_SUCCESS; +} -void acctAddDb(SAcctObj *pAcct, SDbObj *pDb) { +static int32_t mgmtActionAcctUpdate(SSdbOperDesc *pOper) { + SAcctObj *pAcct = pOper->pObj; + SAcctObj *pSaved = mgmtGetAcct(pAcct->user); + if (pAcct != pSaved) { + memcpy(pSaved, pAcct, tsAcctUpdateSize); + free(pAcct); + } + return TSDB_CODE_SUCCESS; +} + +static int32_t mgmtActionActionEncode(SSdbOperDesc *pOper) { + SAcctObj *pAcct = pOper->pObj; + memcpy(pOper->rowData, pAcct, tsAcctUpdateSize); + pOper->rowSize = tsAcctUpdateSize; + return TSDB_CODE_SUCCESS; +} + +static int32_t mgmtActionAcctDecode(SSdbOperDesc *pOper) { + SAcctObj *pAcct = (SAcctObj *) calloc(1, sizeof(SAcctObj)); + if (pAcct == NULL) return TSDB_CODE_SERV_OUT_OF_MEMORY; + + memcpy(pAcct, pOper->rowData, tsAcctUpdateSize); + pOper->pObj = pAcct; + return TSDB_CODE_SUCCESS; +} + +static int32_t mgmtActionAcctRestored() { + if (dnodeIsFirstDeploy()) { + mgmtCreateRootAcct(); + } + return TSDB_CODE_SUCCESS; +} + +int32_t mgmtInitAccts() { + SAcctObj tObj; + tsAcctUpdateSize = (int8_t *)tObj.updateEnd - (int8_t *)&tObj; + + SSdbTableDesc tableDesc = { + .tableId = SDB_TABLE_ACCOUNT, + .tableName = "accounts", + .hashSessions = TSDB_MAX_ACCOUNTS, + .maxRowSize = tsAcctUpdateSize, + .refCountPos = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj, + .keyType = SDB_KEY_STRING, + .insertFp = mgmtAcctActionInsert, + .deleteFp = mgmtActionAcctDelete, + .updateFp = mgmtActionAcctUpdate, + .encodeFp = mgmtActionActionEncode, + .decodeFp = mgmtActionAcctDecode, + .destroyFp = mgmtActionAcctDestroy, + .restoredFp = mgmtActionAcctRestored + }; + + tsAcctSdb = sdbOpenTable(&tableDesc); + if (tsAcctSdb == NULL) { + mError("failed to init acct data"); + return -1; + } + + mTrace("account table is created"); + return acctInit(); +} + +void mgmtCleanUpAccts() { + sdbCloseTable(tsAcctSdb); + acctCleanUp(); +} + +void *mgmtGetAcct(char *name) { + return sdbGetRow(tsAcctSdb, name); +} + +void mgmtIncAcctRef(SAcctObj *pAcct) { + sdbIncRef(tsAcctSdb, pAcct); +} + +void mgmtDecAcctRef(SAcctObj *pAcct) { + sdbDecRef(tsAcctSdb, pAcct); +} + +void mgmtAddDbToAcct(SAcctObj *pAcct, SDbObj *pDb) { atomic_add_fetch_32(&pAcct->acctInfo.numOfDbs, 1); pDb->pAcct = pAcct; - acctIncRef(pAcct); + mgmtIncAcctRef(pAcct); } -void acctRemoveDb(SAcctObj *pAcct, SDbObj *pDb) { +void mgmtDropDbFromAcct(SAcctObj *pAcct, SDbObj *pDb) { atomic_sub_fetch_32(&pAcct->acctInfo.numOfDbs, 1); pDb->pAcct = NULL; - acctReleaseAcct(pAcct); + mgmtDecAcctRef(pAcct); } -void acctAddUser(SAcctObj *pAcct, SUserObj *pUser) { +void mgmtAddUserToAcct(SAcctObj *pAcct, SUserObj *pUser) { atomic_add_fetch_32(&pAcct->acctInfo.numOfUsers, 1); pUser->pAcct = pAcct; - acctIncRef(pAcct); + mgmtIncAcctRef(pAcct); } -void acctRemoveUser(SAcctObj *pAcct, SUserObj *pUser) { +void mgmtDropUserFromAcct(SAcctObj *pAcct, SUserObj *pUser) { atomic_sub_fetch_32(&pAcct->acctInfo.numOfUsers, 1); pUser->pAcct = NULL; - acctReleaseAcct(pAcct); -} \ No newline at end of file + mgmtDecAcctRef(pAcct); +} + +static void mgmtCreateRootAcct() { + int32_t numOfAccts = sdbGetNumOfRows(tsAcctSdb); + if (numOfAccts != 0) return; + + SAcctObj *pAcct = malloc(sizeof(SAcctObj)); + memset(pAcct, 0, sizeof(SAcctObj)); + strcpy(pAcct->user, "root"); + taosEncryptPass((uint8_t*)"taosdata", strlen("taosdata"), pAcct->pass); + pAcct->cfg = (SAcctCfg){ + .maxUsers = 10, + .maxDbs = 64, + .maxTimeSeries = INT32_MAX, + .maxConnections = 1024, + .maxStreams = 1000, + .maxPointsPerSecond = 10000000, + .maxStorage = INT64_MAX, + .maxQueryTime = INT64_MAX, + .maxInbound = 0, + .maxOutbound = 0, + .accessState = TSDB_VN_ALL_ACCCESS + }; + pAcct->acctId = sdbGetId(tsAcctSdb); + pAcct->createdTime = taosGetTimestampMs(); + + SSdbOperDesc oper = { + .type = SDB_OPER_GLOBAL, + .table = tsAcctSdb, + .pObj = pAcct, + }; + sdbInsertRow(&oper); +} + +#ifndef _ACCT + +int32_t acctInit() { return TSDB_CODE_SUCCESS; } +void acctCleanUp() {} +int32_t acctCheck(void *pAcct, EAcctGrantType type) { return TSDB_CODE_SUCCESS; } + +#endif \ No newline at end of file diff --git a/src/mnode/src/mgmtBalance.c b/src/mnode/src/mgmtBalance.c index 77d68f43d8..0b9e025acf 100644 --- a/src/mnode/src/mgmtBalance.c +++ b/src/mnode/src/mgmtBalance.c @@ -16,7 +16,7 @@ #define _DEFAULT_SOURCE #include "tbalance.h" #include "mnode.h" -#include "tcluster.h" +#include "mgmtDnode.h" #include "mgmtVgroup.h" #ifndef _VPEER @@ -31,17 +31,17 @@ int32_t balanceAllocVnodes(SVgObj *pVgroup) { float vnodeUsage = 1.0; while (1) { - pNode = clusterGetNextDnode(pNode, &pDnode); + pNode = mgmtGetNextDnode(pNode, &pDnode); if (pDnode == NULL) break; - if (pDnode->numOfTotalVnodes > 0 && pDnode->openVnodes < pDnode->numOfTotalVnodes) { - float usage = (float)pDnode->openVnodes / pDnode->numOfTotalVnodes; + if (pDnode->totalVnodes > 0 && pDnode->openVnodes < pDnode->totalVnodes) { + float usage = (float)pDnode->openVnodes / pDnode->totalVnodes; if (usage <= vnodeUsage) { pSelDnode = pDnode; vnodeUsage = usage; } } - clusterReleaseDnode(pDnode); + mgmtReleaseDnode(pDnode); } if (pSelDnode == NULL) { diff --git a/src/mnode/src/mgmtDClient.c b/src/mnode/src/mgmtDClient.c index b402a85005..699a1551d4 100644 --- a/src/mnode/src/mgmtDClient.c +++ b/src/mnode/src/mgmtDClient.c @@ -23,7 +23,7 @@ #include "mnode.h" #include "tbalance.h" #include "mgmtDb.h" -#include "tcluster.h" +#include "mgmtDnode.h" #include "tgrant.h" #include "mgmtProfile.h" #include "mgmtShell.h" diff --git a/src/mnode/src/mgmtDb.c b/src/mnode/src/mgmtDb.c index c38fe8af3e..99bcc365ae 100644 --- a/src/mnode/src/mgmtDb.c +++ b/src/mnode/src/mgmtDb.c @@ -19,10 +19,10 @@ #include "tutil.h" #include "name.h" #include "mnode.h" -#include "taccount.h" #include "tbalance.h" +#include "mgmtAcct.h" #include "mgmtDb.h" -#include "tcluster.h" +#include "mgmtDnode.h" #include "tgrant.h" #include "mpeer.h" #include "mgmtShell.h" @@ -51,7 +51,7 @@ static int32_t mgmtDbActionDestroy(SSdbOperDesc *pOper) { static int32_t mgmtDbActionInsert(SSdbOperDesc *pOper) { SDbObj *pDb = pOper->pObj; - SAcctObj *pAcct = acctGetAcct(pDb->cfg.acct); + SAcctObj *pAcct = mgmtGetAcct(pDb->cfg.acct); pDb->pHead = NULL; pDb->pTail = NULL; @@ -60,7 +60,7 @@ static int32_t mgmtDbActionInsert(SSdbOperDesc *pOper) { pDb->numOfSuperTables = 0; if (pAcct != NULL) { - acctAddDb(pAcct, pDb); + mgmtAddDbToAcct(pAcct, pDb); } else { mError("db:%s, acct:%s info not exist in sdb", pDb->name, pDb->cfg.acct); @@ -72,9 +72,9 @@ static int32_t mgmtDbActionInsert(SSdbOperDesc *pOper) { static int32_t mgmtDbActionDelete(SSdbOperDesc *pOper) { SDbObj *pDb = pOper->pObj; - SAcctObj *pAcct = acctGetAcct(pDb->cfg.acct); + SAcctObj *pAcct = mgmtGetAcct(pDb->cfg.acct); - acctRemoveDb(pAcct, pDb); + mgmtDropDbFromAcct(pAcct, pDb); mgmtDropAllChildTables(pDb); mgmtDropAllSuperTables(pDb); mgmtDropAllVgroups(pDb); @@ -156,7 +156,7 @@ void mgmtIncDbRef(SDbObj *pDb) { return sdbIncRef(tsDbSdb, pDb); } -void mgmtReleaseDb(SDbObj *pDb) { +void mgmtDecDbRef(SDbObj *pDb) { return sdbDecRef(tsDbSdb, pDb); } @@ -288,14 +288,14 @@ static int32_t mgmtCheckDbParams(SCMCreateDbMsg *pCreate) { } static int32_t mgmtCreateDb(SAcctObj *pAcct, SCMCreateDbMsg *pCreate) { - int32_t code = acctCheck(pAcct, TSDB_ACCT_DB); + int32_t code = acctCheck(pAcct, ACCT_GRANT_DB); if (code != 0) { return code; } SDbObj *pDb = mgmtGetDb(pCreate->db); if (pDb != NULL) { - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); return TSDB_CODE_DB_ALREADY_EXIST; } @@ -641,7 +641,7 @@ static int32_t mgmtRetrieveDbs(SShowObj *pShow, char *data, int32_t rows, void * cols++; numOfRows++; - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); } pShow->numOfReads += numOfRows; @@ -888,7 +888,7 @@ void mgmtDropAllDbs(SAcctObj *pAcct) { mgmtSetDbDropping(pDb); numOfDbs++; } - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); } mTrace("acct:%s, all dbs is is set dirty", pAcct->user, numOfDbs); diff --git a/src/mnode/src/mgmtDnode.c b/src/mnode/src/mgmtDnode.c index d13d37586a..97e9a89c8c 100644 --- a/src/mnode/src/mgmtDnode.c +++ b/src/mnode/src/mgmtDnode.c @@ -17,91 +17,209 @@ #include "os.h" #include "tmodule.h" #include "tbalance.h" -#include "tcluster.h" +#include "tgrant.h" +#include "mgmtDnode.h" #include "mnode.h" #include "mpeer.h" #include "mgmtDClient.h" -#include "mgmtShell.h" #include "mgmtDServer.h" +#include "mgmtSdb.h" +#include "mgmtShell.h" #include "mgmtUser.h" #include "mgmtVgroup.h" +#include "dnodeMClient.h" -static void clusterProcessCfgDnodeMsg(SQueuedMsg *pMsg); -static void clusterProcessCfgDnodeMsgRsp(SRpcMsg *rpcMsg) ; -static void clusterProcessDnodeStatusMsg(SRpcMsg *rpcMsg); -static int32_t clusterGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn); -static int32_t clusterRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn); -static int32_t clusterGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn); -static int32_t clusterRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn); -static int32_t clusterGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn); -static int32_t clusterRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn); -static int32_t clusterGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn); -static int32_t clusterRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn); +void *tsDnodeSdb = NULL; +int32_t tsDnodeUpdateSize = 0; +extern void * tsVgroupSdb; -#ifndef _CLUSTER +static int32_t mgmtCreateDnode(uint32_t ip); +static void mgmtProcessCreateDnodeMsg(SQueuedMsg *pMsg); +static void mgmtProcessDropDnodeMsg(SQueuedMsg *pMsg); +static void mgmtProcessCfgDnodeMsg(SQueuedMsg *pMsg); +static void mgmtProcessCfgDnodeMsgRsp(SRpcMsg *rpcMsg) ; +static void mgmtProcessDnodeStatusMsg(SRpcMsg *rpcMsg); +static int32_t mgmtGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn); +static int32_t mgmtRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn); +static int32_t mgmtGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn); +static int32_t mgmtRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn); +static int32_t mgmtGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn); +static int32_t mgmtRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn); +static int32_t mgmtGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn); +static int32_t mgmtRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn); -static SDnodeObj tsDnodeObj = {0}; +static int32_t mgmtDnodeActionDestroy(SSdbOperDesc *pOper) { + tfree(pOper->pObj); + return TSDB_CODE_SUCCESS; +} -int32_t clusterInitDnodes() { - tsDnodeObj.dnodeId = 1; - tsDnodeObj.privateIp = inet_addr(tsPrivateIp); - tsDnodeObj.publicIp = inet_addr(tsPublicIp); - tsDnodeObj.createdTime = taosGetTimestampMs(); - tsDnodeObj.numOfTotalVnodes = tsNumOfTotalVnodes; - tsDnodeObj.status = TAOS_DN_STATUS_OFFLINE; - tsDnodeObj.lastReboot = taosGetTimestampSec(); - sprintf(tsDnodeObj.dnodeName, "%d", tsDnodeObj.dnodeId); - - tsDnodeObj.moduleStatus |= (1 << TSDB_MOD_MGMT); - if (tsEnableHttpModule) { - tsDnodeObj.moduleStatus |= (1 << TSDB_MOD_HTTP); +static int32_t mgmtDnodeActionInsert(SSdbOperDesc *pOper) { + SDnodeObj *pDnode = pOper->pObj; + if (pDnode->status != TAOS_DN_STATUS_DROPPING) { + pDnode->status = TAOS_DN_STATUS_OFFLINE; } - if (tsEnableMonitorModule) { - tsDnodeObj.moduleStatus |= (1 << TSDB_MOD_MONITOR); + + return TSDB_CODE_SUCCESS; +} + +static int32_t mgmtDnodeActionDelete(SSdbOperDesc *pOper) { + SDnodeObj *pDnode = pOper->pObj; + void * pNode = NULL; + void * pLastNode = NULL; + SVgObj * pVgroup = NULL; + int32_t numOfVgroups = 0; + + while (1) { + pLastNode = pNode; + pNode = sdbFetchRow(tsVgroupSdb, pNode, (void **)&pVgroup); + if (pVgroup == NULL) break; + + if (pVgroup->vnodeGid[0].dnodeId == pDnode->dnodeId) { + SSdbOperDesc oper = { + .type = SDB_OPER_LOCAL, + .table = tsVgroupSdb, + .pObj = pVgroup, + }; + sdbDeleteRow(&oper); + pNode = pLastNode; + numOfVgroups++; + continue; + } } + + mTrace("dnode:%d, all vgroups:%d is dropped from sdb", pDnode->dnodeId, numOfVgroups); + return TSDB_CODE_SUCCESS; +} + +static int32_t mgmtDnodeActionUpdate(SSdbOperDesc *pOper) { + SDnodeObj *pDnode = pOper->pObj; + SDnodeObj *pSaved = mgmtGetDnode(pDnode->dnodeId); + if (pDnode != pSaved) { + memcpy(pSaved, pDnode, pOper->rowSize); + free(pDnode); + } + return TSDB_CODE_SUCCESS; +} + +static int32_t mgmtDnodeActionEncode(SSdbOperDesc *pOper) { + SDnodeObj *pDnode = pOper->pObj; + memcpy(pOper->rowData, pDnode, tsDnodeUpdateSize); + pOper->rowSize = tsDnodeUpdateSize; + return TSDB_CODE_SUCCESS; +} + +static int32_t mgmtDnodeActionDecode(SSdbOperDesc *pOper) { + SDnodeObj *pDnode = (SDnodeObj *) calloc(1, sizeof(SDnodeObj)); + if (pDnode == NULL) return TSDB_CODE_SERV_OUT_OF_MEMORY; + + memcpy(pDnode, pOper->rowData, tsDnodeUpdateSize); + pOper->pObj = pDnode; + return TSDB_CODE_SUCCESS; +} + +static int32_t mgmtDnodeActionRestored() { + int32_t numOfRows = sdbGetNumOfRows(tsDnodeSdb); + if (numOfRows <= 0) { + if (strcmp(tsMasterIp, tsPrivateIp) == 0) { + mgmtCreateDnode(inet_addr(tsPrivateIp)); + } + } + return 0; } -void *clusterGetNextDnode(void *pNode, SDnodeObj **pDnode) { - if (*pDnode == NULL) { - *pDnode = &tsDnodeObj; - } else { - *pDnode = NULL; +int32_t mgmtInitDnodes() { + SDnodeObj tObj; + tsDnodeUpdateSize = (int8_t *)tObj.updateEnd - (int8_t *)&tObj; + + SSdbTableDesc tableDesc = { + .tableId = SDB_TABLE_DNODE, + .tableName = "dnodes", + .hashSessions = TSDB_MAX_DNODES, + .maxRowSize = tsDnodeUpdateSize, + .refCountPos = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj, + .keyType = SDB_KEY_AUTO, + .insertFp = mgmtDnodeActionInsert, + .deleteFp = mgmtDnodeActionDelete, + .updateFp = mgmtDnodeActionUpdate, + .encodeFp = mgmtDnodeActionEncode, + .decodeFp = mgmtDnodeActionDecode, + .destroyFp = mgmtDnodeActionDestroy, + .restoredFp = mgmtDnodeActionRestored + }; + + tsDnodeSdb = sdbOpenTable(&tableDesc); + if (tsDnodeSdb == NULL) { + mError("failed to init dnodes data"); + return -1; } - return *pDnode; -} -void clusterCleanupDnodes() {} -int32_t clusterGetDnodesNum() { return 1; } -void * clusterGetDnode(int32_t dnodeId) { return dnodeId == 1 ? &tsDnodeObj : NULL; } -void * clusterGetDnodeByIp(uint32_t ip) { return &tsDnodeObj; } -void clusterReleaseDnode(struct _dnode_obj *pDnode) {} -void clusterUpdateDnode(struct _dnode_obj *pDnode) {} -void clusterMonitorDnodeModule() {} - -#endif - -int32_t clusterInit() { - mgmtAddShellMsgHandle(TSDB_MSG_TYPE_CM_CONFIG_DNODE, clusterProcessCfgDnodeMsg); - mgmtAddDClientRspHandle(TSDB_MSG_TYPE_MD_CONFIG_DNODE_RSP, clusterProcessCfgDnodeMsgRsp); - mgmtAddDServerMsgHandle(TSDB_MSG_TYPE_DM_STATUS, clusterProcessDnodeStatusMsg); - mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_MODULE, clusterGetModuleMeta); - mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_MODULE, clusterRetrieveModules); - mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_CONFIGS, clusterGetConfigMeta); - mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_CONFIGS, clusterRetrieveConfigs); - mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_VNODES, clusterGetVnodeMeta); - mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_VNODES, clusterRetrieveVnodes); - mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_DNODE, clusterGetDnodeMeta); - mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_DNODE, clusterRetrieveDnodes); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_CM_CREATE_DNODE, mgmtProcessCreateDnodeMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_CM_DROP_DNODE, mgmtProcessDropDnodeMsg); + mgmtAddShellMsgHandle(TSDB_MSG_TYPE_CM_CONFIG_DNODE, mgmtProcessCfgDnodeMsg); + mgmtAddDClientRspHandle(TSDB_MSG_TYPE_MD_CONFIG_DNODE_RSP, mgmtProcessCfgDnodeMsgRsp); + mgmtAddDServerMsgHandle(TSDB_MSG_TYPE_DM_STATUS, mgmtProcessDnodeStatusMsg); + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_MODULE, mgmtGetModuleMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_MODULE, mgmtRetrieveModules); + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_CONFIGS, mgmtGetConfigMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_CONFIGS, mgmtRetrieveConfigs); + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_VNODES, mgmtGetVnodeMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_VNODES, mgmtRetrieveVnodes); + mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_DNODE, mgmtGetDnodeMeta); + mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_DNODE, mgmtRetrieveDnodes); - return clusterInitDnodes(); + mTrace("dnodes table is created"); + return 0; } -void clusterCleanUp() { - clusterCleanupDnodes(); +void mgmtCleanupDnodes() { + sdbCloseTable(tsDnodeSdb); } -void clusterProcessCfgDnodeMsg(SQueuedMsg *pMsg) { +void *mgmtGetNextDnode(void *pNode, SDnodeObj **pDnode) { + return sdbFetchRow(tsDnodeSdb, pNode, (void **)pDnode); +} + +int32_t mgmtGetDnodesNum() { + return sdbGetNumOfRows(tsDnodeSdb); +} + +void *mgmtGetDnode(int32_t dnodeId) { + return sdbGetRow(tsDnodeSdb, &dnodeId); +} + +void *mgmtGetDnodeByIp(uint32_t ip) { + SDnodeObj *pDnode = NULL; + void * pNode = NULL; + + while (1) { + pNode = sdbFetchRow(tsDnodeSdb, pNode, (void**)&pDnode); + if (pDnode == NULL) break; + if (ip == pDnode->privateIp) { + return pDnode; + } + mgmtReleaseDnode(pDnode); + } + + return NULL; +} + +void mgmtReleaseDnode(SDnodeObj *pDnode) { + sdbDecRef(tsDnodeSdb, pDnode); +} + +void mgmtUpdateDnode(SDnodeObj *pDnode) { + SSdbOperDesc oper = { + .type = SDB_OPER_GLOBAL, + .table = tsDnodeSdb, + .pObj = pDnode, + .rowSize = tsDnodeUpdateSize + }; + + sdbUpdateRow(&oper); +} + +void mgmtProcessCfgDnodeMsg(SQueuedMsg *pMsg) { SRpcMsg rpcRsp = {.handle = pMsg->thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; SCMCfgDnodeMsg *pCmCfgDnode = pMsg->pCont; @@ -137,11 +255,11 @@ void clusterProcessCfgDnodeMsg(SQueuedMsg *pMsg) { rpcSendResponse(&rpcRsp); } -static void clusterProcessCfgDnodeMsgRsp(SRpcMsg *rpcMsg) { +static void mgmtProcessCfgDnodeMsgRsp(SRpcMsg *rpcMsg) { mPrint("cfg vnode rsp is received, result:%s", tstrerror(rpcMsg->code)); } -void clusterProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { +void mgmtProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { SDMStatusMsg *pStatus = rpcMsg->pCont; pStatus->dnodeId = htonl(pStatus->dnodeId); pStatus->privateIp = htonl(pStatus->privateIp); @@ -159,14 +277,14 @@ void clusterProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { SDnodeObj *pDnode = NULL; if (pStatus->dnodeId == 0) { - pDnode = clusterGetDnodeByIp(pStatus->privateIp); + pDnode = mgmtGetDnodeByIp(pStatus->privateIp); if (pDnode == NULL) { mTrace("dnode not created, privateIp:%s", taosIpStr(pStatus->privateIp)); mgmtSendSimpleResp(rpcMsg->handle, TSDB_CODE_DNODE_NOT_EXIST); return; } } else { - pDnode = clusterGetDnode(pStatus->dnodeId); + pDnode = mgmtGetDnode(pStatus->dnodeId); if (pDnode == NULL) { mError("dnode:%d, not exist, privateIp:%s", pStatus->dnodeId, taosIpStr(pStatus->privateIp)); mgmtSendSimpleResp(rpcMsg->handle, TSDB_CODE_DNODE_NOT_EXIST); @@ -180,7 +298,7 @@ void clusterProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { pDnode->numOfCores = pStatus->numOfCores; pDnode->diskAvailable = pStatus->diskAvailable; pDnode->alternativeRole = pStatus->alternativeRole; - pDnode->numOfTotalVnodes = pStatus->numOfTotalVnodes; + pDnode->totalVnodes = pStatus->numOfTotalVnodes; if (pStatus->dnodeId == 0) { mTrace("dnode:%d, first access, privateIp:%s, name:%s", pDnode->dnodeId, taosIpStr(pDnode->privateIp), pDnode->dnodeName); @@ -209,10 +327,10 @@ void clusterProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { mTrace("dnode:%d, from offline to online", pDnode->dnodeId); pDnode->status = TAOS_DN_STATUS_READY; balanceNotify(); - clusterMonitorDnodeModule(); + mgmtMonitorDnodeModule(); } - clusterReleaseDnode(pDnode); + mgmtReleaseDnode(pDnode); int32_t contLen = sizeof(SDMStatusRsp) + TSDB_MAX_VNODES * sizeof(SVnodeAccess); SDMStatusRsp *pRsp = rpcMallocCont(contLen); @@ -242,7 +360,123 @@ void clusterProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { rpcSendResponse(&rpcRsp); } -static int32_t clusterGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) { +static int32_t mgmtCreateDnode(uint32_t ip) { + int32_t grantCode = grantCheck(TSDB_GRANT_DNODE); + if (grantCode != TSDB_CODE_SUCCESS) { + return grantCode; + } + + SDnodeObj *pDnode = mgmtGetDnodeByIp(ip); + if (pDnode != NULL) { + mError("dnode:%d is alredy exist, ip:%s", pDnode->dnodeId, taosIpStr(pDnode->privateIp)); + return TSDB_CODE_DNODE_ALREADY_EXIST; + } + + pDnode = (SDnodeObj *) calloc(1, sizeof(SDnodeObj)); + pDnode->privateIp = ip; + pDnode->publicIp = ip; + pDnode->createdTime = taosGetTimestampMs(); + pDnode->status = TAOS_DN_STATUS_OFFLINE; + pDnode->totalVnodes = TSDB_INVALID_VNODE_NUM; + sprintf(pDnode->dnodeName, "n%d", sdbGetId(tsDnodeSdb) + 1); + + if (pDnode->privateIp == inet_addr(tsMasterIp)) { + pDnode->moduleStatus |= (1 << TSDB_MOD_MGMT); + } + + SSdbOperDesc oper = { + .type = SDB_OPER_GLOBAL, + .table = tsDnodeSdb, + .pObj = pDnode, + .rowSize = sizeof(SDnodeObj) + }; + + int32_t code = sdbInsertRow(&oper); + if (code != TSDB_CODE_SUCCESS) { + tfree(pDnode); + code = TSDB_CODE_SDB_ERROR; + } + + mPrint("dnode:%d is created, result:%s", pDnode->dnodeId, tstrerror(code)); + return code; +} + +int32_t mgmtDropDnode(SDnodeObj *pDnode) { + SSdbOperDesc oper = { + .type = SDB_OPER_GLOBAL, + .table = tsDnodeSdb, + .pObj = pDnode + }; + + int32_t code = sdbDeleteRow(&oper); + if (code != TSDB_CODE_SUCCESS) { + code = TSDB_CODE_SDB_ERROR; + } + + mLPrint("dnode:%d is dropped from cluster, result:%s", pDnode->dnodeId, tstrerror(code)); + return code; +} + +static int32_t clusterDropDnodeByIp(uint32_t ip) { + SDnodeObj *pDnode = mgmtGetDnodeByIp(ip); + if (pDnode == NULL) { + mError("dnode:%s, is not exist", taosIpStr(ip)); + return TSDB_CODE_INVALID_VALUE; + } + + if (pDnode->privateIp == dnodeGetMnodeMasteIp()) { + mError("dnode:%d, can't drop dnode which is master", pDnode->dnodeId); + return TSDB_CODE_NO_REMOVE_MASTER; + } + +#ifndef _VPEER + return mgmtDropDnode(pDnode); +#else + return balanceDropDnode(pDnode); +#endif +} + +static void mgmtProcessCreateDnodeMsg(SQueuedMsg *pMsg) { + SRpcMsg rpcRsp = {.handle = pMsg->thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + + SCMCreateDnodeMsg *pCreate = pMsg->pCont; + + if (strcmp(pMsg->pUser->pAcct->user, "root") != 0) { + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + } else { + uint32_t ip = inet_addr(pCreate->ip); + rpcRsp.code = mgmtCreateDnode(ip); + if (rpcRsp.code == TSDB_CODE_SUCCESS) { + SDnodeObj *pDnode = mgmtGetDnodeByIp(ip); + mLPrint("dnode:%d, ip:%s is created by %s", pDnode->dnodeId, pCreate->ip, pMsg->pUser->user); + } else { + mError("failed to create dnode:%s, reason:%s", pCreate->ip, tstrerror(rpcRsp.code)); + } + } + rpcSendResponse(&rpcRsp); +} + + +static void mgmtProcessDropDnodeMsg(SQueuedMsg *pMsg) { + SRpcMsg rpcRsp = {.handle = pMsg->thandle, .pCont = NULL, .contLen = 0, .code = 0, .msgType = 0}; + + SCMDropDnodeMsg *pDrop = pMsg->pCont; + if (strcmp(pMsg->pUser->pAcct->user, "root") != 0) { + rpcRsp.code = TSDB_CODE_NO_RIGHTS; + } else { + uint32_t ip = inet_addr(pDrop->ip); + rpcRsp.code = clusterDropDnodeByIp(ip); + if (rpcRsp.code == TSDB_CODE_SUCCESS) { + mLPrint("dnode:%s is dropped by %s", pDrop->ip, pMsg->pUser->user); + } else { + mError("failed to drop dnode:%s, reason:%s", pDrop->ip, tstrerror(rpcRsp.code)); + } + } + + rpcSendResponse(&rpcRsp); +} + +static int32_t mgmtGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) { SUserObj *pUser = mgmtGetUserFromConn(pConn, NULL); if (pUser == NULL) return 0; @@ -309,7 +543,7 @@ static int32_t clusterGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void * pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1]; } - pShow->numOfRows = clusterGetDnodesNum(); + pShow->numOfRows = mgmtGetDnodesNum(); pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; pShow->pNode = NULL; @@ -318,7 +552,7 @@ static int32_t clusterGetDnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void * return 0; } -static int32_t clusterRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) { +static int32_t mgmtRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) { int32_t numOfRows = 0; int32_t cols = 0; SDnodeObj *pDnode = NULL; @@ -326,7 +560,7 @@ static int32_t clusterRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, char ipstr[32]; while (numOfRows < rows) { - pShow->pNode = clusterGetNextDnode(pShow->pNode, &pDnode); + pShow->pNode = mgmtGetNextDnode(pShow->pNode, &pDnode); if (pDnode == NULL) break; cols = 0; @@ -350,7 +584,7 @@ static int32_t clusterRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, clusterGetDnodeStatusStr(pDnode->status)); + strcpy(pWrite, mgmtGetDnodeStatusStr(pDnode->status)); cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; @@ -358,29 +592,29 @@ static int32_t clusterRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - *(int16_t *)pWrite = pDnode->numOfTotalVnodes; + *(int16_t *)pWrite = pDnode->totalVnodes; cols++; #ifdef _VPEER pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, clusterGetDnodeStatusStr(pDnode->status)); + strcpy(pWrite, mgmtGetDnodeStatusStr(pDnode->status)); cols++; #endif numOfRows++; - clusterReleaseDnode(pDnode); + mgmtReleaseDnode(pDnode); } pShow->numOfReads += numOfRows; return numOfRows; } -bool clusterCheckModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) { +bool mgmtCheckModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) { uint32_t status = pDnode->moduleStatus & (1 << moduleType); return status > 0; } -static int32_t clusterGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) { +static int32_t mgmtGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) { int32_t cols = 0; SUserObj *pUser = mgmtGetUserFromConn(pConn, NULL); @@ -419,10 +653,10 @@ static int32_t clusterGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void pShow->numOfRows = 0; SDnodeObj *pDnode = NULL; while (1) { - pShow->pNode = clusterGetNextDnode(pShow->pNode, (SDnodeObj **)&pDnode); + pShow->pNode = mgmtGetNextDnode(pShow->pNode, (SDnodeObj **)&pDnode); if (pDnode == NULL) break; for (int32_t moduleType = 0; moduleType < TSDB_MOD_MAX; ++moduleType) { - if (clusterCheckModuleInDnode(pDnode, moduleType)) { + if (mgmtCheckModuleInDnode(pDnode, moduleType)) { pShow->numOfRows++; } } @@ -435,7 +669,7 @@ static int32_t clusterGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void return 0; } -int32_t clusterRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn) { +int32_t mgmtRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn) { int32_t numOfRows = 0; SDnodeObj *pDnode = NULL; char * pWrite; @@ -443,12 +677,12 @@ int32_t clusterRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void * char ipstr[20]; while (numOfRows < rows) { - clusterReleaseDnode(pDnode); - pShow->pNode = clusterGetNextDnode(pShow->pNode, (SDnodeObj **)&pDnode); + mgmtReleaseDnode(pDnode); + pShow->pNode = mgmtGetNextDnode(pShow->pNode, (SDnodeObj **)&pDnode); if (pDnode == NULL) break; for (int32_t moduleType = 0; moduleType < TSDB_MOD_MAX; ++moduleType) { - if (!clusterCheckModuleInDnode(pDnode, moduleType)) { + if (!mgmtCheckModuleInDnode(pDnode, moduleType)) { continue; } @@ -464,7 +698,7 @@ int32_t clusterRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void * cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, clusterGetDnodeStatusStr(pDnode->status)); + strcpy(pWrite, mgmtGetDnodeStatusStr(pDnode->status)); cols++; numOfRows++; @@ -481,7 +715,7 @@ static bool clusterCheckConfigShow(SGlobalConfig *cfg) { return true; } -static int32_t clusterGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) { +static int32_t mgmtGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) { int32_t cols = 0; SUserObj *pUser = mgmtGetUserFromConn(pConn, NULL); @@ -523,7 +757,7 @@ static int32_t clusterGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void return 0; } -static int32_t clusterRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn) { +static int32_t mgmtRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, void *pConn) { int32_t numOfRows = 0; for (int32_t i = tsGlobalConfigNum - 1; i >= 0 && numOfRows < rows; --i) { @@ -570,7 +804,7 @@ static int32_t clusterRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, return numOfRows; } -static int32_t clusterGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) { +static int32_t mgmtGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) { int32_t cols = 0; SUserObj *pUser = mgmtGetUserFromConn(pConn, NULL); if (pUser == NULL) return 0; @@ -599,7 +833,7 @@ static int32_t clusterGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void * SDnodeObj *pDnode = NULL; if (pShow->payloadLen > 0 ) { uint32_t ip = ip2uint(pShow->payload); - pDnode = clusterGetDnodeByIp(ip); + pDnode = mgmtGetDnodeByIp(ip); if (NULL == pDnode) { return TSDB_CODE_NODE_OFFLINE; } @@ -616,7 +850,7 @@ static int32_t clusterGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void * pShow->pNode = pDnode; } else { while (true) { - pShow->pNode = clusterGetNextDnode(pShow->pNode, (SDnodeObj **)&pDnode); + pShow->pNode = mgmtGetNextDnode(pShow->pNode, (SDnodeObj **)&pDnode); if (pDnode == NULL) break; pShow->numOfRows += pDnode->openVnodes; @@ -627,13 +861,13 @@ static int32_t clusterGetVnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void * } pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; - clusterReleaseDnode(pDnode); + mgmtReleaseDnode(pDnode); mgmtReleaseUser(pUser); return 0; } -static int32_t clusterRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) { +static int32_t mgmtRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn) { int32_t numOfRows = 0; SDnodeObj *pDnode = NULL; char * pWrite; @@ -674,7 +908,7 @@ static int32_t clusterRetrieveVnodes(SShowObj *pShow, char *data, int32_t rows, return numOfRows; } -char* clusterGetDnodeStatusStr(int32_t dnodeStatus) { +char* mgmtGetDnodeStatusStr(int32_t dnodeStatus) { switch (dnodeStatus) { case TAOS_DN_STATUS_OFFLINE: return "offline"; case TAOS_DN_STATUS_DROPPING: return "dropping"; @@ -683,3 +917,155 @@ char* clusterGetDnodeStatusStr(int32_t dnodeStatus) { default: return "undefined"; } } + + +static void clusterSetModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) { + pDnode->moduleStatus |= (1 << moduleType); + mgmtUpdateDnode(pDnode); + + if (moduleType == TSDB_MOD_MGMT) { + mpeerAddMnode(pDnode->dnodeId); + mPrint("dnode:%d, add it into mnode list", pDnode->dnodeId); + } +} + +static void clusterUnSetModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) { + pDnode->moduleStatus &= ~(1 << moduleType); + mgmtUpdateDnode(pDnode); + + if (moduleType == TSDB_MOD_MGMT) { + mpeerRemoveMnode(pDnode->dnodeId); + mPrint("dnode:%d, remove it from mnode list", pDnode->dnodeId); + } +} + +static void clusterStopAllModuleInDnode(SDnodeObj *pDnode) { + for (int32_t moduleType = 0; moduleType < TSDB_MOD_MAX; ++moduleType) { + if (!mgmtCheckModuleInDnode(pDnode, moduleType)) { + continue; + } + + mPrint("dnode:%d, stop %s module for its offline or remove", pDnode->dnodeId, tsModule[moduleType].name); + clusterUnSetModuleInDnode(pDnode, moduleType); + } +} + +static void clusterStartModuleInAllDnodes(int32_t moduleType) { + void * pNode = NULL; + SDnodeObj *pDnode = NULL; + + while (1) { + pNode = mgmtGetNextDnode(pNode, &pDnode); + if (pDnode == NULL) break; + + if (!mgmtCheckModuleInDnode(pDnode, moduleType) + && pDnode->status != TAOS_DN_STATUS_OFFLINE + && pDnode->status != TAOS_DN_STATUS_DROPPING) { + mPrint("dnode:%d, add %s module for schedule", pDnode->dnodeId, tsModule[moduleType].name); + clusterSetModuleInDnode(pDnode, moduleType); + } + + mgmtReleaseDnode(pNode); + } +} + +static void clusterStartModuleInOneDnode(int32_t moduleType) { + void * pNode = NULL; + SDnodeObj *pDnode = NULL; + + while (1) { + pNode = mgmtGetNextDnode(pNode, &pDnode); + if (pDnode == NULL) break; + + if (!mgmtCheckModuleInDnode(pDnode, moduleType) + && pDnode->status != TAOS_DN_STATUS_OFFLINE + && pDnode->status != TAOS_DN_STATUS_DROPPING + && !(moduleType == TSDB_MOD_MGMT && pDnode->alternativeRole == TSDB_DNODE_ROLE_VNODE)) { + mPrint("dnode:%d, add %s module for schedule", pDnode->dnodeId, tsModule[moduleType].name); + clusterSetModuleInDnode(pDnode, moduleType); + mgmtReleaseDnode(pNode); + break; + } + + mgmtReleaseDnode(pNode); + } +} + +static void clusterStopModuleInOneDnode(int32_t moduleType) { + void * pNode = NULL; + SDnodeObj *pDnode = NULL; + + while (1) { + pNode = mgmtGetNextDnode(pNode, &pDnode); + if (pDnode == NULL) break; + + if (mgmtCheckModuleInDnode(pDnode, moduleType)) { + mPrint("dnode:%d, stop %s module for schedule", pDnode->dnodeId, tsModule[moduleType].name); + clusterUnSetModuleInDnode(pDnode, moduleType); + mgmtReleaseDnode(pNode); + break; + } + + mgmtReleaseDnode(pNode); + } +} + +void mgmtMonitorDnodeModule() { + void * pNode = NULL; + SDnodeObj *pDnode = NULL; + int32_t onlineDnodes = 0; + + for (int32_t moduleType = 0; moduleType < TSDB_MOD_MGMT+1; ++moduleType) { + tsModule[moduleType].curNum = 0; + } + + // dnode loop + while (1) { + pNode = mgmtGetNextDnode(pNode, &pDnode); + if (pDnode == NULL) break; + + if (pDnode->status == TAOS_DN_STATUS_DROPPING) { + mPrint("dnode:%d, status:%d, remove all modules for removing", pDnode->dnodeId, pDnode->status); + clusterStopAllModuleInDnode(pDnode); + mgmtReleaseDnode(pDnode); + continue; + } + + for (int32_t moduleType = 0; moduleType < TSDB_MOD_MGMT+1; ++moduleType) { + if (mgmtCheckModuleInDnode(pDnode, moduleType)) { + tsModule[moduleType].curNum ++; + } + } + + if (pDnode->status != TAOS_DN_STATUS_OFFLINE) { + onlineDnodes++; + } + + mgmtReleaseDnode(pDnode); + } + + for (int32_t moduleType = 0; moduleType < TSDB_MOD_MGMT+1; ++moduleType) { + if (tsModule[moduleType].num == -1) { + clusterStartModuleInAllDnodes(moduleType); + continue; + } + if (tsModule[moduleType].curNum < tsModule[moduleType].num) { + if (onlineDnodes <= tsModule[moduleType].curNum) { + continue; + } + mTrace("need add %s module, curNum:%d, expectNum:%d", tsModule[moduleType].name, tsModule[moduleType].curNum, + tsModule[moduleType].num); + for (int32_t i = tsModule[moduleType].curNum; i < tsModule[moduleType].num; ++i) { + clusterStartModuleInOneDnode(moduleType); + } + } else if (tsModule[moduleType].curNum > tsModule[moduleType].num) { + mTrace("need drop %s module, curNum:%d, expectNum:%d", tsModule[moduleType].name, tsModule[moduleType].curNum, + tsModule[moduleType].num); + for (int32_t i = tsModule[moduleType].num; i < tsModule[moduleType].curNum; ++i) { + clusterStopModuleInOneDnode(moduleType); + } + } else { + } + } +} + diff --git a/src/mnode/src/mgmtMain.c b/src/mnode/src/mgmtMain.c index b6fb1ba425..46721f4834 100644 --- a/src/mnode/src/mgmtMain.c +++ b/src/mnode/src/mgmtMain.c @@ -19,9 +19,9 @@ #include "tmodule.h" #include "tsched.h" #include "mnode.h" -#include "taccount.h" +#include "mgmtAcct.h" #include "tbalance.h" -#include "tcluster.h" +#include "mgmtDnode.h" #include "tgrant.h" #include "mpeer.h" #include "mgmtDb.h" @@ -74,7 +74,7 @@ int32_t mgmtStartSystem() { return -1; } - if (acctInit() < 0) { + if (mgmtInitAccts() < 0) { mError("failed to init accts"); return -1; } @@ -89,7 +89,7 @@ int32_t mgmtStartSystem() { return -1; } - if (clusterInit() < 0) { + if (mgmtInitDnodes() < 0) { mError("failed to init dnodes"); return -1; } @@ -160,9 +160,9 @@ void mgmtCleanUpSystem() { mgmtCleanUpTables(); mgmtCleanUpVgroups(); mgmtCleanUpDbs(); - clusterCleanUp(); + mgmtCleanupDnodes(); mgmtCleanUpUsers(); - acctCleanUp(); + mgmtCleanUpAccts(); sdbCleanUp(); taosTmrCleanUp(tsMgmtTmr); mPrint("mgmt is cleaned up"); diff --git a/src/mnode/src/mgmtProfile.c b/src/mnode/src/mgmtProfile.c index f7dec4656b..db1b764ca7 100644 --- a/src/mnode/src/mgmtProfile.c +++ b/src/mnode/src/mgmtProfile.c @@ -16,8 +16,8 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taosmsg.h" -#include "taccount.h" -#include "tcluster.h" +#include "mgmtAcct.h" +#include "mgmtDnode.h" #include "mgmtDb.h" #include "mpeer.h" #include "mgmtProfile.h" @@ -787,11 +787,11 @@ void mgmtFreeQueuedMsg(SQueuedMsg *pMsg) { if (pMsg != NULL) { rpcFreeCont(pMsg->pCont); if (pMsg->pUser) mgmtReleaseUser(pMsg->pUser); - if (pMsg->pDb) mgmtReleaseDb(pMsg->pDb); + if (pMsg->pDb) mgmtDecDbRef(pMsg->pDb); if (pMsg->pVgroup) mgmtReleaseVgroup(pMsg->pVgroup); if (pMsg->pTable) mgmtDecTableRef(pMsg->pTable); - if (pMsg->pAcct) acctReleaseAcct(pMsg->pAcct); - if (pMsg->pDnode) clusterReleaseDnode(pMsg->pDnode); + if (pMsg->pAcct) mgmtDecAcctRef(pMsg->pAcct); + if (pMsg->pDnode) mgmtReleaseDnode(pMsg->pDnode); free(pMsg); } } diff --git a/src/mnode/src/mgmtShell.c b/src/mnode/src/mgmtShell.c index 5010429db3..82eb2bae1e 100644 --- a/src/mnode/src/mgmtShell.c +++ b/src/mnode/src/mgmtShell.c @@ -22,10 +22,10 @@ #include "tsched.h" #include "dnode.h" #include "mnode.h" -#include "taccount.h" +#include "mgmtAcct.h" #include "tbalance.h" #include "mgmtDb.h" -#include "tcluster.h" +#include "mgmtDnode.h" #include "tgrant.h" #include "mpeer.h" #include "mgmtProfile.h" diff --git a/src/mnode/src/mgmtTable.c b/src/mnode/src/mgmtTable.c index 10cd343a5b..65dfb06ad5 100644 --- a/src/mnode/src/mgmtTable.c +++ b/src/mnode/src/mgmtTable.c @@ -23,10 +23,10 @@ #include "taosmsg.h" #include "tscompression.h" #include "name.h" -#include "taccount.h" +#include "mgmtAcct.h" #include "mgmtDClient.h" #include "mgmtDb.h" -#include "tcluster.h" +#include "mgmtDnode.h" #include "mgmtDServer.h" #include "tgrant.h" #include "mpeer.h" @@ -101,14 +101,14 @@ static int32_t mgmtChildTableActionInsert(SSdbOperDesc *pOper) { mError("ctable:%s, vgroup:%d not in db:%s", pTable->info.tableId, pVgroup->vgId, pVgroup->dbName); return TSDB_CODE_INVALID_DB; } - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); - SAcctObj *pAcct = acctGetAcct(pDb->cfg.acct); + SAcctObj *pAcct = mgmtGetAcct(pDb->cfg.acct); if (pAcct == NULL) { mError("ctable:%s, account:%s not exists", pTable->info.tableId, pDb->cfg.acct); return TSDB_CODE_INVALID_ACCT; } - acctReleaseAcct(pAcct); + mgmtDecAcctRef(pAcct); if (pTable->info.type == TSDB_CHILD_TABLE) { pTable->superTable = mgmtGetSuperTable(pTable->superTableId); @@ -143,14 +143,14 @@ static int32_t mgmtChildTableActionDelete(SSdbOperDesc *pOper) { mError("ctable:%s, vgroup:%d not in DB:%s", pTable->info.tableId, pVgroup->vgId, pVgroup->dbName); return TSDB_CODE_INVALID_DB; } - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); - SAcctObj *pAcct = acctGetAcct(pDb->cfg.acct); + SAcctObj *pAcct = mgmtGetAcct(pDb->cfg.acct); if (pAcct == NULL) { mError("ctable:%s, account:%s not exists", pTable->info.tableId, pDb->cfg.acct); return TSDB_CODE_INVALID_ACCT; } - acctReleaseAcct(pAcct); + mgmtDecAcctRef(pAcct); if (pTable->info.type == TSDB_CHILD_TABLE) { grantRestore(TSDB_GRANT_TIMESERIES, pTable->superTable->numOfColumns - 1); @@ -258,7 +258,7 @@ static int32_t mgmtChildTableActionRestored() { pNode = pLastNode; continue; } - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); SVgObj *pVgroup = mgmtGetVgroup(pTable->vgId); if (pVgroup == NULL) { @@ -401,7 +401,7 @@ static int32_t mgmtSuperTableActionInsert(SSdbOperDesc *pOper) { if (pDb != NULL) { mgmtAddSuperTableIntoDb(pDb); } - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); return TSDB_CODE_SUCCESS; } @@ -413,7 +413,7 @@ static int32_t mgmtSuperTableActionDelete(SSdbOperDesc *pOper) { mgmtRemoveSuperTableFromDb(pDb); mgmtDropAllChildTablesInStable((SSuperTableObj *)pStable); } - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); return TSDB_CODE_SUCCESS; } @@ -923,10 +923,10 @@ static int32_t mgmtAddSuperTableColumn(SDbObj *pDb, SSuperTableObj *pStable, SSc pStable->numOfColumns += ncols; pStable->sversion++; - SAcctObj *pAcct = acctGetAcct(pDb->cfg.acct); + SAcctObj *pAcct = mgmtGetAcct(pDb->cfg.acct); if (pAcct != NULL) { pAcct->acctInfo.numOfTimeSeries += (ncols * pStable->numOfTables); - acctReleaseAcct(pAcct); + mgmtDecAcctRef(pAcct); } SSdbOperDesc oper = { @@ -960,10 +960,10 @@ static int32_t mgmtDropSuperTableColumn(SDbObj *pDb, SSuperTableObj *pStable, ch int32_t schemaSize = sizeof(SSchema) * (pStable->numOfTags + pStable->numOfColumns); pStable->schema = realloc(pStable->schema, schemaSize); - SAcctObj *pAcct = acctGetAcct(pDb->cfg.acct); + SAcctObj *pAcct = mgmtGetAcct(pDb->cfg.acct); if (pAcct != NULL) { pAcct->acctInfo.numOfTimeSeries -= pStable->numOfTables; - acctReleaseAcct(pAcct); + mgmtDecAcctRef(pAcct); } SSdbOperDesc oper = { @@ -1029,7 +1029,7 @@ static int32_t mgmtGetShowSuperTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, pShow->numOfRows = pDb->numOfSuperTables; pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); return 0; } @@ -1094,7 +1094,7 @@ int32_t mgmtRetrieveShowSuperTables(SShowObj *pShow, char *data, int32_t rows, v } pShow->numOfReads += numOfRows; - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); return numOfRows; } @@ -1191,14 +1191,14 @@ static void mgmtProcessSuperTableVgroupMsg(SQueuedMsg *pMsg) { pRsp->vgroups[vg].vgId = htonl(vgId); for (int32_t vn = 0; vn < pVgroup->numOfVnodes; ++vn) { - SDnodeObj *pDnode = clusterGetDnode(pVgroup->vnodeGid[vn].dnodeId); + SDnodeObj *pDnode = mgmtGetDnode(pVgroup->vnodeGid[vn].dnodeId); if (pDnode == NULL) break; pRsp->vgroups[vg].ipAddr[vn].ip = htonl(pDnode->privateIp); pRsp->vgroups[vg].ipAddr[vn].port = htons(tsDnodeShellPort); pRsp->vgroups[vg].numOfIps++; - clusterReleaseDnode(pDnode); + mgmtReleaseDnode(pDnode); } mgmtReleaseVgroup(pVgroup); @@ -1500,10 +1500,10 @@ static int32_t mgmtAddNormalTableColumn(SDbObj *pDb, SChildTableObj *pTable, SSc pTable->numOfColumns += ncols; pTable->sversion++; - SAcctObj *pAcct = acctGetAcct(pDb->cfg.acct); + SAcctObj *pAcct = mgmtGetAcct(pDb->cfg.acct); if (pAcct != NULL) { pAcct->acctInfo.numOfTimeSeries += ncols; - acctReleaseAcct(pAcct); + mgmtDecAcctRef(pAcct); } SSdbOperDesc oper = { @@ -1534,10 +1534,10 @@ static int32_t mgmtDropNormalTableColumn(SDbObj *pDb, SChildTableObj *pTable, ch pTable->numOfColumns--; pTable->sversion++; - SAcctObj *pAcct = acctGetAcct(pDb->cfg.acct); + SAcctObj *pAcct = mgmtGetAcct(pDb->cfg.acct); if (pAcct != NULL) { pAcct->acctInfo.numOfTimeSeries--; - acctReleaseAcct(pAcct); + mgmtDecAcctRef(pAcct); } SSdbOperDesc oper = { @@ -1600,7 +1600,7 @@ static int32_t mgmtDoGetChildTableMeta(SQueuedMsg *pMsg, STableMetaMsg *pMeta) { } for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) { - SDnodeObj *pDnode = clusterGetDnode(pVgroup->vnodeGid[i].dnodeId); + SDnodeObj *pDnode = mgmtGetDnode(pVgroup->vnodeGid[i].dnodeId); if (pDnode == NULL) break; if (usePublicIp) { pMeta->vgroup.ipAddr[i].ip = htonl(pDnode->publicIp); @@ -1610,7 +1610,7 @@ static int32_t mgmtDoGetChildTableMeta(SQueuedMsg *pMsg, STableMetaMsg *pMeta) { pMeta->vgroup.ipAddr[i].port = htonl(tsDnodeShellPort); } pMeta->vgroup.numOfIps++; - clusterReleaseDnode(pDnode); + mgmtReleaseDnode(pDnode); } pMeta->vgroup.vgId = htonl(pVgroup->vgId); @@ -1730,7 +1730,7 @@ static void mgmtDropAllChildTablesInStable(SSuperTableObj *pStable) { } static SChildTableObj* mgmtGetTableByPos(uint32_t dnodeId, int32_t vnode, int32_t sid) { - SDnodeObj *pObj = clusterGetDnode(dnodeId); + SDnodeObj *pObj = mgmtGetDnode(dnodeId); SVgObj *pVgroup = mgmtGetVgroup(vnode); if (pObj == NULL || pVgroup == NULL) { @@ -1968,7 +1968,7 @@ static int32_t mgmtGetShowTableMeta(STableMetaMsg *pMeta, SShowObj *pShow, void pShow->numOfRows = pDb->numOfTables; pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); return 0; } @@ -2045,7 +2045,7 @@ static int32_t mgmtRetrieveShowTables(SShowObj *pShow, char *data, int32_t rows, const int32_t NUM_OF_COLUMNS = 4; mgmtVacuumResult(data, NUM_OF_COLUMNS, numOfRows, rows, pShow); - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); return numOfRows; } diff --git a/src/mnode/src/mgmtUser.c b/src/mnode/src/mgmtUser.c index 931272fb58..7a7f2999b3 100644 --- a/src/mnode/src/mgmtUser.c +++ b/src/mnode/src/mgmtUser.c @@ -18,7 +18,7 @@ #include "trpc.h" #include "ttime.h" #include "tutil.h" -#include "taccount.h" +#include "mgmtAcct.h" #include "tgrant.h" #include "mpeer.h" #include "mgmtSdb.h" @@ -40,10 +40,10 @@ static int32_t mgmtUserActionDestroy(SSdbOperDesc *pOper) { static int32_t mgmtUserActionInsert(SSdbOperDesc *pOper) { SUserObj *pUser = pOper->pObj; - SAcctObj *pAcct = acctGetAcct(pUser->acct); + SAcctObj *pAcct = mgmtGetAcct(pUser->acct); if (pAcct != NULL) { - acctAddUser(pAcct, pUser); + mgmtAddUserToAcct(pAcct, pUser); } else { mError("user:%s, acct:%s info not exist in sdb", pUser->user, pUser->acct); @@ -55,10 +55,10 @@ static int32_t mgmtUserActionInsert(SSdbOperDesc *pOper) { static int32_t mgmtUserActionDelete(SSdbOperDesc *pOper) { SUserObj *pUser = pOper->pObj; - SAcctObj *pAcct = acctGetAcct(pUser->acct); + SAcctObj *pAcct = mgmtGetAcct(pUser->acct); if (pAcct != NULL) { - acctRemoveUser(pAcct, pUser); + mgmtDropUserFromAcct(pAcct, pUser); } return TSDB_CODE_SUCCESS; @@ -92,11 +92,11 @@ static int32_t mgmtUserActionDecode(SSdbOperDesc *pOper) { static int32_t mgmtUserActionRestored() { if (strcmp(tsMasterIp, tsPrivateIp) == 0) { - SAcctObj *pAcct = acctGetAcct("root"); + SAcctObj *pAcct = mgmtGetAcct("root"); mgmtCreateUser(pAcct, "root", "taosdata"); mgmtCreateUser(pAcct, "monitor", tsInternalPass); mgmtCreateUser(pAcct, "_root", tsInternalPass); - acctReleaseAcct(pAcct); + mgmtDecAcctRef(pAcct); } return 0; @@ -167,7 +167,7 @@ static int32_t mgmtUpdateUser(SUserObj *pUser) { } int32_t mgmtCreateUser(SAcctObj *pAcct, char *name, char *pass) { - int32_t code = acctCheck(pAcct, TSDB_ACCT_USER); + int32_t code = acctCheck(pAcct, ACCT_GRANT_USER); if (code != 0) { return code; } diff --git a/src/mnode/src/mgmtVgroup.c b/src/mnode/src/mgmtVgroup.c index 98d9b22d8e..ee9afd9586 100644 --- a/src/mnode/src/mgmtVgroup.c +++ b/src/mnode/src/mgmtVgroup.c @@ -19,7 +19,7 @@ #include "tlog.h" #include "tbalance.h" #include "tsync.h" -#include "tcluster.h" +#include "mgmtDnode.h" #include "mnode.h" #include "mgmtDb.h" #include "mgmtDClient.h" @@ -63,7 +63,7 @@ static int32_t mgmtVgroupActionInsert(SSdbOperDesc *pOper) { if (pDb == NULL) { return TSDB_CODE_INVALID_DB; } - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); pVgroup->pDb = pDb; pVgroup->prev = NULL; @@ -84,12 +84,12 @@ static int32_t mgmtVgroupActionInsert(SSdbOperDesc *pOper) { } for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) { - SDnodeObj *pDnode = clusterGetDnode(pVgroup->vnodeGid[i].dnodeId); + SDnodeObj *pDnode = mgmtGetDnode(pVgroup->vnodeGid[i].dnodeId); if (pDnode != NULL) { pVgroup->vnodeGid[i].privateIp = pDnode->privateIp; pVgroup->vnodeGid[i].publicIp = pDnode->publicIp; atomic_add_fetch_32(&pDnode->openVnodes, 1); - clusterReleaseDnode(pDnode); + mgmtReleaseDnode(pDnode); } } @@ -106,14 +106,14 @@ static int32_t mgmtVgroupActionDelete(SSdbOperDesc *pOper) { mgmtRemoveVgroupFromDb(pVgroup); } - mgmtReleaseDb(pVgroup->pDb); + mgmtDecDbRef(pVgroup->pDb); for (int32_t i = 0; i < pVgroup->numOfVnodes; ++i) { - SDnodeObj *pDnode = clusterGetDnode(pVgroup->vnodeGid[i].dnodeId); + SDnodeObj *pDnode = mgmtGetDnode(pVgroup->vnodeGid[i].dnodeId); if (pDnode) { atomic_sub_fetch_32(&pDnode->openVnodes, 1); } - clusterReleaseDnode(pDnode); + mgmtReleaseDnode(pDnode); } return TSDB_CODE_SUCCESS; @@ -381,18 +381,18 @@ int32_t mgmtGetVgroupMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) { pShow->pNode = pVgroup; } - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); return 0; } char *mgmtGetVnodeStatus(SVgObj *pVgroup, SVnodeGid *pVnode) { - SDnodeObj *pDnode = clusterGetDnode(pVnode->dnodeId); + SDnodeObj *pDnode = mgmtGetDnode(pVnode->dnodeId); if (pDnode == NULL) { mError("vgroup:%d, not exist in dnode:%d", pVgroup->vgId, pDnode->dnodeId); return "null"; } - clusterReleaseDnode(pDnode); + mgmtReleaseDnode(pDnode); if (pDnode->status == TAOS_DN_STATUS_OFFLINE) { return "offline"; @@ -467,7 +467,7 @@ int32_t mgmtRetrieveVgroups(SShowObj *pShow, char *data, int32_t rows, void *pCo } pShow->numOfReads += numOfRows; - mgmtReleaseDb(pDb); + mgmtDecDbRef(pDb); return numOfRows; } @@ -676,13 +676,13 @@ static void mgmtProcessVnodeCfgMsg(SRpcMsg *rpcMsg) { pCfg->dnodeId = htonl(pCfg->dnodeId); pCfg->vgId = htonl(pCfg->vgId); - SDnodeObj *pDnode = clusterGetDnode(pCfg->dnodeId); + SDnodeObj *pDnode = mgmtGetDnode(pCfg->dnodeId); if (pDnode == NULL) { mTrace("dnode:%s, invalid dnode", taosIpStr(pCfg->dnodeId), pCfg->vgId); mgmtSendSimpleResp(rpcMsg->handle, TSDB_CODE_NOT_ACTIVE_VNODE); return; } - clusterReleaseDnode(pDnode); + mgmtReleaseDnode(pDnode); SVgObj *pVgroup = mgmtGetVgroup(pCfg->vgId); if (pVgroup == NULL) { From 48872f6c4bf8aac33ec0fcca9afd07133e49b725 Mon Sep 17 00:00:00 2001 From: hzcheng Date: Thu, 16 Apr 2020 18:52:07 +0800 Subject: [PATCH 03/14] move header --- src/{tsdb => }/inc/tsdb.h | 0 src/util/src/talgo.c | 2 ++ 2 files changed, 2 insertions(+) rename src/{tsdb => }/inc/tsdb.h (100%) diff --git a/src/tsdb/inc/tsdb.h b/src/inc/tsdb.h similarity index 100% rename from src/tsdb/inc/tsdb.h rename to src/inc/tsdb.h diff --git a/src/util/src/talgo.c b/src/util/src/talgo.c index 7a682cd466..76de87e67d 100644 --- a/src/util/src/talgo.c +++ b/src/util/src/talgo.c @@ -168,6 +168,7 @@ void * taosbsearch(const void *key, const void *base, size_t nmemb, size_t size, if (flags == TD_EQ) { return bsearch(key, base, nmemb, size, compar); } else if (flags == TD_GE) { + if (nmemb <= 0) return NULL; if ((*compar)(key, elePtrAt(base, size, 0)) <= 0) return elePtrAt(base, size, 0); if ((*compar)(key, elePtrAt(base, size, nmemb - 1)) > 0) return NULL; @@ -193,6 +194,7 @@ void * taosbsearch(const void *key, const void *base, size_t nmemb, size_t size, } } } else if (flags == TD_LE) { + if (nmemb <= 0) return NULL; if ((*compar)(key, elePtrAt(base, size, nmemb - 1)) >= 0) return elePtrAt(base, size, nmemb - 1); if ((*compar)(key, elePtrAt(base, size, 0)) < 0) return NULL; From 0f202d9fc45d6d498f8887632d8b1edd2d18cc7f Mon Sep 17 00:00:00 2001 From: slguan Date: Thu, 16 Apr 2020 18:57:13 +0800 Subject: [PATCH 04/14] rearrage some codes --- cmake/define.inc | 19 +- src/dnode/CMakeLists.txt | 13 +- src/dnode/src/dnodeMClient.c | 4 +- src/inc/mnode.h | 24 +- src/inc/{tbalance.h => treplica.h} | 22 +- src/{inc/mpeer.h => mnode/inc/mgmtMnode.h} | 34 +-- src/mnode/src/mgmtDClient.c | 2 +- src/mnode/src/mgmtDServer.c | 2 +- src/mnode/src/mgmtDb.c | 3 +- src/mnode/src/mgmtDnode.c | 36 +-- src/mnode/src/mgmtMain.c | 14 +- src/mnode/src/mgmtMnode.c | 271 +++++++++++++----- src/mnode/src/mgmtProfile.c | 2 +- .../src/{mgmtBalance.c => mgmtReplica.c} | 18 +- src/mnode/src/mgmtSdb.c | 7 +- src/mnode/src/mgmtShell.c | 21 +- src/mnode/src/mgmtTable.c | 2 +- src/mnode/src/mgmtUser.c | 2 +- src/mnode/src/mgmtVgroup.c | 6 +- 19 files changed, 303 insertions(+), 199 deletions(-) rename src/inc/{tbalance.h => treplica.h} (68%) rename src/{inc/mpeer.h => mnode/inc/mgmtMnode.h} (53%) rename src/mnode/src/{mgmtBalance.c => mgmtReplica.c} (82%) diff --git a/cmake/define.inc b/cmake/define.inc index 5f17ee1216..9f56dc654d 100755 --- a/cmake/define.inc +++ b/cmake/define.inc @@ -1,25 +1,18 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) PROJECT(TDengine) -IF (TD_CLUSTER) - ADD_DEFINITIONS(-D_CLUSTER) -ENDIF () - -IF (TD_MPEER) - ADD_DEFINITIONS(-D_MPEER) -ENDIF () - -IF (TD_VPEER) - ADD_DEFINITIONS(-D_VPEER) - #ADD_DEFINITIONS(-DTSDB_REPLICA_MAX_NUM=3) -ELSE () - #ADD_DEFINITIONS(-DTSDB_REPLICA_MAX_NUM=1) +IF (TD_SYNC) + ADD_DEFINITIONS(-D_SYNC) ENDIF () IF (TD_ACCOUNT) ADD_DEFINITIONS(-D_ACCOUNT) ENDIF () +IF (TD_ADMIN) + ADD_DEFINITIONS(-D_ADMIN) +ENDIF () + IF (TD_GRANT) ADD_DEFINITIONS(-D_GRANT) ENDIF () diff --git a/src/dnode/CMakeLists.txt b/src/dnode/CMakeLists.txt index 8999770618..5735e1a8c1 100644 --- a/src/dnode/CMakeLists.txt +++ b/src/dnode/CMakeLists.txt @@ -21,20 +21,13 @@ IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) IF (TD_ACCOUNT) TARGET_LINK_LIBRARIES(taosd account) ENDIF () + IF (TD_GRANT) TARGET_LINK_LIBRARIES(taosd grant) ENDIF () - IF (TD_CLUSTER) - TARGET_LINK_LIBRARIES(taosd cluster) - ENDIF () - - IF (TD_VPEER) - TARGET_LINK_LIBRARIES(taosd balance sync) - ENDIF () - - IF (TD_MPEER) - TARGET_LINK_LIBRARIES(taosd mpeer sync) + IF (TD_SYNC) + TARGET_LINK_LIBRARIES(taosd replica sync) ENDIF () SET(PREPARE_ENV_CMD "prepare_env_cmd") diff --git a/src/dnode/src/dnodeMClient.c b/src/dnode/src/dnodeMClient.c index 85454af095..460c866ce1 100644 --- a/src/dnode/src/dnodeMClient.c +++ b/src/dnode/src/dnodeMClient.c @@ -23,12 +23,12 @@ #include "tsync.h" #include "ttime.h" #include "ttimer.h" +#include "treplica.h" #include "dnode.h" #include "dnodeMClient.h" #include "dnodeModule.h" #include "dnodeMgmt.h" #include "vnode.h" -#include "mpeer.h" #define MPEER_CONTENT_LEN 2000 @@ -181,7 +181,7 @@ static void dnodeProcessStatusRsp(SRpcMsg *pMsg) { tsMnodeInfos.nodeInfos[i].nodeName); } dnodeSaveMnodeIpList(); - mpeerUpdateSync(); + replicaNotify(); } taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, NULL, tsDnodeTmr, &tsStatusTimer); diff --git a/src/inc/mnode.h b/src/inc/mnode.h index a5817ac9df..cbd768c295 100644 --- a/src/inc/mnode.h +++ b/src/inc/mnode.h @@ -43,19 +43,6 @@ struct _acct_obj; struct _user_obj; struct _mnode_obj; -typedef struct _mnode_obj { - int32_t mnodeId; - int64_t createdTime; - int8_t reserved[14]; - int8_t updateEnd[1]; - int32_t refCount; - uint32_t privateIp; - uint32_t publicIp; - uint16_t port; - int8_t role; - char mnodeName[TSDB_NODE_NAME_LEN + 1]; -} SMnodeObj; - typedef struct _dnode_obj { int32_t dnodeId; uint32_t privateIp; @@ -88,6 +75,17 @@ typedef struct _dnode_obj { int16_t bandwidthUsage; // calc from sys.band } SDnodeObj; +typedef struct _mnode_obj { + int32_t mnodeId; + int64_t createdTime; + int8_t reserved[14]; + int8_t updateEnd[1]; + int32_t refCount; + int8_t role; + SDnodeObj *pDnode; +} SMnodeObj; + + typedef struct { int32_t dnodeId; uint32_t privateIp; diff --git a/src/inc/tbalance.h b/src/inc/treplica.h similarity index 68% rename from src/inc/tbalance.h rename to src/inc/treplica.h index c73d6a91a9..b8915d64a0 100644 --- a/src/inc/tbalance.h +++ b/src/inc/treplica.h @@ -13,27 +13,23 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_BALANCE_H -#define TDENGINE_BALANCE_H +#ifndef TDENGINE_REPLICA_H +#define TDENGINE_REPLICA_H #ifdef __cplusplus extern "C" { #endif -#include -#include -#include - -struct _db_obj; struct _vg_obj; struct _dnode_obj; -int32_t balanceInit(); -void balanceCleanUp(); -void balanceNotify(); -void balanceReset(); -int32_t balanceAllocVnodes(struct _vg_obj *pVgroup); -int32_t balanceDropDnode(struct _dnode_obj *pDnode); +int32_t replicaInit(); +void replicaCleanUp(); +void replicaNotify(); +void replicaReset(); +int32_t replicaAllocVnodes(struct _vg_obj *pVgroup); +int32_t replicaForwardReqToPeer(void *pHead); +int32_t replicaDropDnode(struct _dnode_obj *pDnode); #ifdef __cplusplus } diff --git a/src/inc/mpeer.h b/src/mnode/inc/mgmtMnode.h similarity index 53% rename from src/inc/mpeer.h rename to src/mnode/inc/mgmtMnode.h index ba1b7d32cf..d89ebb0c01 100644 --- a/src/inc/mpeer.h +++ b/src/mnode/inc/mgmtMnode.h @@ -13,8 +13,8 @@ * along with this program. If not, see . */ -#ifndef TDENGINE_MPEER_H -#define TDENGINE_MPEER_H +#ifndef TDENGINE_MGMT_MNODE_H +#define TDENGINE_MGMT_MNODE_H #ifdef __cplusplus extern "C" { @@ -28,29 +28,21 @@ enum _TAOS_MN_STATUS { TAOS_MN_STATUS_READY }; -// general implementation -int32_t mpeerInit(); -void mpeerCleanup(); +int32_t mgmtInitMnodes(); +void mgmtCleanupMnodes(); -// special implementation -int32_t mpeerInitMnodes(); -void mpeerCleanupMnodes(); -int32_t mpeerAddMnode(int32_t dnodeId); -int32_t mpeerRemoveMnode(int32_t dnodeId); +int32_t mgmtAddMnode(int32_t dnodeId); +int32_t mgmtDropMnode(int32_t dnodeId); -void * mpeerGetMnode(int32_t mnodeId); -int32_t mpeerGetMnodesNum(); -void * mpeerGetNextMnode(void *pNode, struct _mnode_obj **pMnode); -void mpeerReleaseMnode(struct _mnode_obj *pMnode); +void * mgmtGetMnode(int32_t mnodeId); +int32_t mgmtGetMnodesNum(); +void * mgmtGetNextMnode(void *pNode, struct _mnode_obj **pMnode); +void mgmtReleaseMnode(struct _mnode_obj *pMnode); -bool mpeerIsMaster(); +bool mgmtIsMaster(); -void mpeerGetPrivateIpList(SRpcIpSet *ipSet); -void mpeerGetPublicIpList(SRpcIpSet *ipSet); -void mpeerGetMpeerInfos(void *mpeers); - -int32_t mpeerForwardReqToPeer(void *pHead); -void mpeerUpdateSync(); +void mgmtGetMnodeIpList(SRpcIpSet *ipSet, bool usePublicIp); +void mgmtGetMnodeList(void *mpeers); #ifdef __cplusplus } diff --git a/src/mnode/src/mgmtDClient.c b/src/mnode/src/mgmtDClient.c index 699a1551d4..8552519e02 100644 --- a/src/mnode/src/mgmtDClient.c +++ b/src/mnode/src/mgmtDClient.c @@ -21,7 +21,7 @@ #include "tutil.h" #include "dnode.h" #include "mnode.h" -#include "tbalance.h" +#include "mgmtMnode.h" #include "mgmtDb.h" #include "mgmtDnode.h" #include "tgrant.h" diff --git a/src/mnode/src/mgmtDServer.c b/src/mnode/src/mgmtDServer.c index 0ac93d429c..e4c5e797b3 100644 --- a/src/mnode/src/mgmtDServer.c +++ b/src/mnode/src/mgmtDServer.c @@ -22,7 +22,7 @@ #include "tutil.h" #include "dnode.h" #include "mnode.h" -#include "tbalance.h" +#include "treplica.h" #include "mgmtDb.h" #include "mgmtDServer.h" #include "tgrant.h" diff --git a/src/mnode/src/mgmtDb.c b/src/mnode/src/mgmtDb.c index 99bcc365ae..5b77e6b60e 100644 --- a/src/mnode/src/mgmtDb.c +++ b/src/mnode/src/mgmtDb.c @@ -19,12 +19,11 @@ #include "tutil.h" #include "name.h" #include "mnode.h" -#include "tbalance.h" #include "mgmtAcct.h" #include "mgmtDb.h" #include "mgmtDnode.h" #include "tgrant.h" -#include "mpeer.h" +#include "mgmtMnode.h" #include "mgmtShell.h" #include "mgmtProfile.h" #include "mgmtSdb.h" diff --git a/src/mnode/src/mgmtDnode.c b/src/mnode/src/mgmtDnode.c index 97e9a89c8c..17212806a9 100644 --- a/src/mnode/src/mgmtDnode.c +++ b/src/mnode/src/mgmtDnode.c @@ -16,13 +16,13 @@ #define _DEFAULT_SOURCE #include "os.h" #include "tmodule.h" -#include "tbalance.h" #include "tgrant.h" -#include "mgmtDnode.h" +#include "treplica.h" #include "mnode.h" -#include "mpeer.h" #include "mgmtDClient.h" #include "mgmtDServer.h" +#include "mgmtDnode.h" +#include "mgmtMnode.h" #include "mgmtSdb.h" #include "mgmtShell.h" #include "mgmtUser.h" @@ -119,13 +119,15 @@ static int32_t mgmtDnodeActionDecode(SSdbOperDesc *pOper) { static int32_t mgmtDnodeActionRestored() { int32_t numOfRows = sdbGetNumOfRows(tsDnodeSdb); - if (numOfRows <= 0) { - if (strcmp(tsMasterIp, tsPrivateIp) == 0) { - mgmtCreateDnode(inet_addr(tsPrivateIp)); - } + if (numOfRows <= 0 && strcmp(tsMasterIp, tsPrivateIp) == 0) { + uint32_t ip = inet_addr(tsPrivateIp); + mgmtCreateDnode(ip); + SDnodeObj *pDnode = mgmtGetDnodeByIp(ip); + mgmtAddMnode(pDnode->dnodeId); + mgmtReleaseDnode(pDnode); } - return 0; + return TSDB_CODE_SUCCESS; } int32_t mgmtInitDnodes() { @@ -326,7 +328,7 @@ void mgmtProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { if (pDnode->status == TAOS_DN_STATUS_OFFLINE) { mTrace("dnode:%d, from offline to online", pDnode->dnodeId); pDnode->status = TAOS_DN_STATUS_READY; - balanceNotify(); + replicaNotify(); mgmtMonitorDnodeModule(); } @@ -339,7 +341,7 @@ void mgmtProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { return; } - mpeerGetMpeerInfos(&pRsp->mpeers); + mgmtGetMnodeList(&pRsp->mpeers); pRsp->dnodeState.dnodeId = htonl(pDnode->dnodeId); pRsp->dnodeState.moduleStatus = htonl(pDnode->moduleStatus); @@ -417,7 +419,7 @@ int32_t mgmtDropDnode(SDnodeObj *pDnode) { return code; } -static int32_t clusterDropDnodeByIp(uint32_t ip) { +static int32_t mgmtDropDnodeByIp(uint32_t ip) { SDnodeObj *pDnode = mgmtGetDnodeByIp(ip); if (pDnode == NULL) { mError("dnode:%s, is not exist", taosIpStr(ip)); @@ -465,7 +467,7 @@ static void mgmtProcessDropDnodeMsg(SQueuedMsg *pMsg) { rpcRsp.code = TSDB_CODE_NO_RIGHTS; } else { uint32_t ip = inet_addr(pDrop->ip); - rpcRsp.code = clusterDropDnodeByIp(ip); + rpcRsp.code = mgmtDropDnodeByIp(ip); if (rpcRsp.code == TSDB_CODE_SUCCESS) { mLPrint("dnode:%s is dropped by %s", pDrop->ip, pMsg->pUser->user); } else { @@ -709,7 +711,7 @@ int32_t mgmtRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pCo return numOfRows; } -static bool clusterCheckConfigShow(SGlobalConfig *cfg) { +static bool mgmtCheckConfigShow(SGlobalConfig *cfg) { if (!(cfg->cfgType & TSDB_CFG_CTYPE_B_SHOW)) return false; return true; @@ -746,7 +748,7 @@ static int32_t mgmtGetConfigMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pC pShow->numOfRows = 0; for (int32_t i = tsGlobalConfigNum - 1; i >= 0; --i) { SGlobalConfig *cfg = tsGlobalConfig + i; - if (!clusterCheckConfigShow(cfg)) continue; + if (!mgmtCheckConfigShow(cfg)) continue; pShow->numOfRows++; } @@ -762,7 +764,7 @@ static int32_t mgmtRetrieveConfigs(SShowObj *pShow, char *data, int32_t rows, vo for (int32_t i = tsGlobalConfigNum - 1; i >= 0 && numOfRows < rows; --i) { SGlobalConfig *cfg = tsGlobalConfig + i; - if (!clusterCheckConfigShow(cfg)) continue; + if (!mgmtCheckConfigShow(cfg)) continue; char *pWrite; int32_t cols = 0; @@ -924,7 +926,7 @@ static void clusterSetModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) { mgmtUpdateDnode(pDnode); if (moduleType == TSDB_MOD_MGMT) { - mpeerAddMnode(pDnode->dnodeId); + mgmtAddMnode(pDnode->dnodeId); mPrint("dnode:%d, add it into mnode list", pDnode->dnodeId); } } @@ -934,7 +936,7 @@ static void clusterUnSetModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) { mgmtUpdateDnode(pDnode); if (moduleType == TSDB_MOD_MGMT) { - mpeerRemoveMnode(pDnode->dnodeId); + mgmtDropMnode(pDnode->dnodeId); mPrint("dnode:%d, remove it from mnode list", pDnode->dnodeId); } } diff --git a/src/mnode/src/mgmtMain.c b/src/mnode/src/mgmtMain.c index 46721f4834..a4ce22f158 100644 --- a/src/mnode/src/mgmtMain.c +++ b/src/mnode/src/mgmtMain.c @@ -20,10 +20,10 @@ #include "tsched.h" #include "mnode.h" #include "mgmtAcct.h" -#include "tbalance.h" +#include "treplica.h" #include "mgmtDnode.h" #include "tgrant.h" -#include "mpeer.h" +#include "mgmtMnode.h" #include "mgmtDb.h" #include "mgmtDClient.h" #include "mgmtDServer.h" @@ -109,7 +109,7 @@ int32_t mgmtStartSystem() { return -1; } - if (mpeerInit() < 0) { + if (mgmtInitMnodes() < 0) { mError("failed to init mpeers"); return -1; } @@ -127,7 +127,7 @@ int32_t mgmtStartSystem() { return -1; } - if (balanceInit() < 0) { + if (replicaInit() < 0) { mError("failed to init dnode balance") } @@ -140,7 +140,7 @@ int32_t mgmtStartSystem() { void mgmtStopSystem() { - if (mpeerIsMaster()) { + if (mgmtIsMaster()) { mTrace("it is a master mgmt node, it could not be stopped"); return; } @@ -152,8 +152,8 @@ void mgmtStopSystem() { void mgmtCleanUpSystem() { mPrint("starting to clean up mgmt"); grantCleanUp(); - mpeerCleanup(); - balanceCleanUp(); + mgmtCleanupMnodes(); + replicaCleanUp(); mgmtCleanUpShell(); mgmtCleanupDClient(); mgmtCleanupDServer(); diff --git a/src/mnode/src/mgmtMnode.c b/src/mnode/src/mgmtMnode.c index e2edb201b9..eff6e8e01a 100644 --- a/src/mnode/src/mgmtMnode.c +++ b/src/mnode/src/mgmtMnode.c @@ -16,91 +16,129 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taoserror.h" +#include "tmodule.h" #include "trpc.h" #include "tsync.h" -#include "mpeer.h" +#include "treplica.h" +#include "mnode.h" +#include "mgmtMnode.h" +#include "mgmtDnode.h" +#include "mgmtSdb.h" #include "mgmtShell.h" #include "mgmtUser.h" +static void * tsMnodeSdb = NULL; +static int32_t tsMnodeUpdateSize = 0; +static int32_t tsMnodeIsMaster = true; static int32_t mgmtGetMnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn); static int32_t mgmtRetrieveMnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn); -#ifndef _MPEER +static int32_t mgmtMnodeActionDestroy(SSdbOperDesc *pOper) { + tfree(pOper->pObj); + return TSDB_CODE_SUCCESS; +} -static SMnodeObj tsMnodeObj = {0}; - -int32_t mpeerInitMnodes() { - tsMnodeObj.mnodeId = 1; - tsMnodeObj.privateIp = inet_addr(tsPrivateIp); - tsMnodeObj.publicIp = inet_addr(tsPublicIp); - tsMnodeObj.createdTime = taosGetTimestampMs(); - tsMnodeObj.role = TAOS_SYNC_ROLE_MASTER; - tsMnodeObj.port = tsMnodeDnodePort; - sprintf(tsMnodeObj.mnodeName, "m%d", tsMnodeObj.mnodeId); +static int32_t mgmtMnodeActionInsert(SSdbOperDesc *pOper) { + SMnodeObj *pMnode = pOper->pObj; + SDnodeObj *pDnode = mgmtGetDnode(pMnode->mnodeId); + if (pDnode == NULL) return TSDB_CODE_DNODE_NOT_EXIST; + pMnode->pDnode = pDnode; + mgmtReleaseDnode(pDnode); return TSDB_CODE_SUCCESS; } -void mpeerCleanupMnodes() {} -int32_t mpeerAddMnode(int32_t dnodeId) { return TSDB_CODE_SUCCESS; } -int32_t mpeerRemoveMnode(int32_t dnodeId) { return TSDB_CODE_SUCCESS; } -void * mpeerGetMnode(int32_t mnodeId) { return &tsMnodeObj; } -int32_t mpeerGetMnodesNum() { return 1; } -void mpeerReleaseMnode(struct _mnode_obj *pMnode) {} -bool mpeerIsMaster() { return tsMnodeObj.role == TAOS_SYNC_ROLE_MASTER; } -void mpeerUpdateSync() {} - -void *mpeerGetNextMnode(void *pNode, SMnodeObj **pMnode) { - if (*pMnode == NULL) { - *pMnode = &tsMnodeObj; - } else { - *pMnode = NULL; - } - - return *pMnode; -} - -void mpeerGetPrivateIpList(SRpcIpSet *ipSet) { - ipSet->inUse = 0; - ipSet->numOfIps = 1; - ipSet->port = htons(tsMnodeObj.port); - ipSet->ip[0] = htonl(tsMnodeObj.privateIp); -} - -void mpeerGetPublicIpList(SRpcIpSet *ipSet) { - ipSet->inUse = 0; - ipSet->numOfIps = 1; - ipSet->port = htons(tsMnodeObj.port); - ipSet->ip[0] = htonl(tsMnodeObj.publicIp); -} - -void mpeerGetMpeerInfos(void *param) { - SDMNodeInfos *mpeers = param; - mpeers->inUse = 0; - mpeers->nodeNum = 1; - mpeers->nodeInfos[0].nodeId = htonl(tsMnodeObj.mnodeId); - mpeers->nodeInfos[0].nodeIp = htonl(tsMnodeObj.privateIp); - mpeers->nodeInfos[0].nodePort = htons(tsMnodeObj.port); - strcpy(mpeers->nodeInfos[0].nodeName, tsMnodeObj.mnodeName); -} - -int32_t mpeerForwardReqToPeer(void *pHead) { +static int32_t mgmtMnodeActionDelete(SSdbOperDesc *pOper) { + SMnodeObj *pMnode = pOper->pObj; + mTrace("mnode:%d, is dropped from sdb", pMnode->mnodeId); return TSDB_CODE_SUCCESS; } -#endif +static int32_t mgmtMnodeActionUpdate(SSdbOperDesc *pOper) { + SMnodeObj *pMnode = pOper->pObj; + SMnodeObj *pSaved = mgmtGetMnode(pMnode->mnodeId); + if (pMnode != pSaved) { + memcpy(pSaved, pMnode, pOper->rowSize); + free(pMnode); + } + + return TSDB_CODE_SUCCESS; +} + +static int32_t mgmtMnodeActionEncode(SSdbOperDesc *pOper) { + SMnodeObj *pMnode = pOper->pObj; + memcpy(pOper->rowData, pMnode, tsMnodeUpdateSize); + pOper->rowSize = tsMnodeUpdateSize; + return TSDB_CODE_SUCCESS; +} + +static int32_t mgmtMnodeActionDecode(SSdbOperDesc *pOper) { + SMnodeObj *pMnode = calloc(1, sizeof(SMnodeObj)); + if (pMnode == NULL) return TSDB_CODE_SERV_OUT_OF_MEMORY; + + memcpy(pMnode, pOper->rowData, tsMnodeUpdateSize); + pOper->pObj = pMnode; + return TSDB_CODE_SUCCESS; +} + +static int32_t mgmtMnodeActionRestored() { + return TSDB_CODE_SUCCESS; +} + +int32_t mgmtInitMnodes() { + SMnodeObj tObj; + tsMnodeUpdateSize = (int8_t *)tObj.updateEnd - (int8_t *)&tObj; + + SSdbTableDesc tableDesc = { + .tableId = SDB_TABLE_MNODE, + .tableName = "mnodes", + .hashSessions = TSDB_MAX_MNODES, + .maxRowSize = tsMnodeUpdateSize, + .refCountPos = (int8_t *)(&tObj.refCount) - (int8_t *)&tObj, + .keyType = SDB_KEY_INT, + .insertFp = mgmtMnodeActionInsert, + .deleteFp = mgmtMnodeActionDelete, + .updateFp = mgmtMnodeActionUpdate, + .encodeFp = mgmtMnodeActionEncode, + .decodeFp = mgmtMnodeActionDecode, + .destroyFp = mgmtMnodeActionDestroy, + .restoredFp = mgmtMnodeActionRestored + }; + + tsMnodeSdb = sdbOpenTable(&tableDesc); + if (tsMnodeSdb == NULL) { + mError("failed to init mnodes data"); + return -1; + } -int32_t mpeerInit() { mgmtAddShellShowMetaHandle(TSDB_MGMT_TABLE_MNODE, mgmtGetMnodeMeta); mgmtAddShellShowRetrieveHandle(TSDB_MGMT_TABLE_MNODE, mgmtRetrieveMnodes); - return mpeerInitMnodes(); + + mTrace("mnodes table is created"); + return TSDB_CODE_SUCCESS; } -void mpeerCleanup() { - mpeerCleanupMnodes(); +void mgmtCleanupMnodes() { + sdbCloseTable(tsMnodeSdb); } -static char *mpeerGetMnodeRoleStr(int32_t role) { +int32_t mgmtGetMnodesNum() { + return sdbGetNumOfRows(tsMnodeSdb); +} + +void *mgmtGetMnode(int32_t mnodeId) { + return sdbGetRow(tsMnodeSdb, &mnodeId); +} + +void mgmtReleaseMnode(struct _mnode_obj *pMnode) { + sdbDecRef(tsMnodeSdb, pMnode); +} + +void *mgmtGetNextMnode(void *pNode, SMnodeObj **pMnode) { + return sdbFetchRow(tsMnodeSdb, pNode, (void **)pMnode); +} + +static char *mgmtGetMnodeRoleStr(int32_t role) { switch (role) { case TAOS_SYNC_ROLE_OFFLINE: return "offline"; @@ -115,6 +153,101 @@ static char *mpeerGetMnodeRoleStr(int32_t role) { } } +bool mgmtIsMaster() { return tsMnodeIsMaster; } + +void mgmtGetMnodeIpList(SRpcIpSet *ipSet, bool usePublicIp) { + void *pNode = NULL; + while (1) { + SMnodeObj *pMnode = NULL; + pNode = mgmtGetNextMnode(pNode, &pMnode); + if (pMnode == NULL) break; + + if (usePublicIp) { + ipSet->ip[ipSet->numOfIps] = htonl(pMnode->pDnode->publicIp); + } else { + ipSet->ip[ipSet->numOfIps] = htonl(pMnode->pDnode->privateIp); + } + + if (pMnode->role == TAOS_SYNC_ROLE_MASTER) { + ipSet->inUse = ipSet->numOfIps; + } + + ipSet->numOfIps++; + ipSet->port = htons(pMnode->pDnode->mnodeShellPort); + + mgmtReleaseMnode(pMnode); + } +} + +void mgmtGetMnodeList(void *param) { + SDMNodeInfos *mnodes = param; + mnodes->inUse = 0; + + int32_t index = 0; + void *pNode = NULL; + while (1) { + SMnodeObj *pMnode = NULL; + pNode = mgmtGetNextMnode(pNode, &pMnode); + if (pMnode == NULL) break; + + mnodes->nodeInfos[index].nodeId = htonl(pMnode->mnodeId); + mnodes->nodeInfos[index].nodeIp = htonl(pMnode->pDnode->privateIp); + mnodes->nodeInfos[index].nodePort = htons(pMnode->pDnode->mnodeDnodePort); + strcpy(mnodes->nodeInfos[index].nodeName, pMnode->pDnode->dnodeName); + mPrint("node:%d role:%s", pMnode->mnodeId, mgmtGetMnodeRoleStr(pMnode->role)); + if (pMnode->role == TAOS_SYNC_ROLE_MASTER) { + mnodes->inUse = index; + mPrint("node:%d inUse:%d", pMnode->mnodeId, mnodes->inUse); + } + + index++; + mgmtReleaseMnode(pMnode); + } + + mnodes->nodeNum = index; +} + +int32_t mgmtAddMnode(int32_t dnodeId) { + SMnodeObj *pMnode = calloc(1, sizeof(SMnodeObj)); + pMnode->mnodeId = dnodeId; + pMnode->createdTime = taosGetTimestampMs(); + + SSdbOperDesc oper = { + .type = SDB_OPER_GLOBAL, + .table = tsMnodeSdb, + .pObj = pMnode, + }; + + int32_t code = sdbInsertRow(&oper); + if (code != TSDB_CODE_SUCCESS) { + tfree(pMnode); + code = TSDB_CODE_SDB_ERROR; + } + + return code; +} + +int32_t mgmtDropMnode(int32_t dnodeId) { + SMnodeObj *pMnode = sdbGetRow(tsMnodeSdb, &dnodeId); + if (pMnode == NULL) { + return TSDB_CODE_DNODE_NOT_EXIST; + } + + SSdbOperDesc oper = { + .type = SDB_OPER_GLOBAL, + .table = tsMnodeSdb, + .pObj = pMnode + }; + + int32_t code = sdbDeleteRow(&oper); + if (code != TSDB_CODE_SUCCESS) { + code = TSDB_CODE_SDB_ERROR; + } + + sdbDecRef(tsMnodeSdb, pMnode); + return code; +} + static int32_t mgmtGetMnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn) { SUserObj *pUser = mgmtGetUserFromConn(pConn, NULL); if (pUser == NULL) return 0; @@ -162,7 +295,7 @@ static int32_t mgmtGetMnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pCo pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1]; } - pShow->numOfRows = mpeerGetMnodesNum(); + pShow->numOfRows = mgmtGetMnodesNum(); pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; pShow->pNode = NULL; mgmtReleaseUser(pUser); @@ -178,7 +311,7 @@ static int32_t mgmtRetrieveMnodes(SShowObj *pShow, char *data, int32_t rows, voi char ipstr[32]; while (numOfRows < rows) { - pShow->pNode = mpeerGetNextMnode(pShow->pNode, &pMnode); + pShow->pNode = mgmtGetNextMnode(pShow->pNode, &pMnode); if (pMnode == NULL) break; cols = 0; @@ -187,12 +320,12 @@ static int32_t mgmtRetrieveMnodes(SShowObj *pShow, char *data, int32_t rows, voi *(int16_t *)pWrite = pMnode->mnodeId; cols++; - tinet_ntoa(ipstr, pMnode->privateIp); + tinet_ntoa(ipstr, pMnode->pDnode->privateIp); pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; strcpy(pWrite, ipstr); cols++; - tinet_ntoa(ipstr, pMnode->publicIp); + tinet_ntoa(ipstr, pMnode->pDnode->publicIp); pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; strcpy(pWrite, ipstr); cols++; @@ -202,15 +335,15 @@ static int32_t mgmtRetrieveMnodes(SShowObj *pShow, char *data, int32_t rows, voi cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, mpeerGetMnodeRoleStr(pMnode->role)); + strcpy(pWrite, mgmtGetMnodeRoleStr(pMnode->role)); cols++; numOfRows++; - mpeerReleaseMnode(pMnode); + mgmtReleaseMnode(pMnode); } pShow->numOfReads += numOfRows; return numOfRows; -} \ No newline at end of file +} diff --git a/src/mnode/src/mgmtProfile.c b/src/mnode/src/mgmtProfile.c index db1b764ca7..eb9efc8e5d 100644 --- a/src/mnode/src/mgmtProfile.c +++ b/src/mnode/src/mgmtProfile.c @@ -19,7 +19,7 @@ #include "mgmtAcct.h" #include "mgmtDnode.h" #include "mgmtDb.h" -#include "mpeer.h" +#include "mgmtMnode.h" #include "mgmtProfile.h" #include "mgmtShell.h" #include "mgmtTable.h" diff --git a/src/mnode/src/mgmtBalance.c b/src/mnode/src/mgmtReplica.c similarity index 82% rename from src/mnode/src/mgmtBalance.c rename to src/mnode/src/mgmtReplica.c index 0b9e025acf..bb906fa5ec 100644 --- a/src/mnode/src/mgmtBalance.c +++ b/src/mnode/src/mgmtReplica.c @@ -14,17 +14,23 @@ */ #define _DEFAULT_SOURCE -#include "tbalance.h" +#include "os.h" +#include "trpc.h" +#include "treplica.h" #include "mnode.h" +#include "mgmtMnode.h" #include "mgmtDnode.h" #include "mgmtVgroup.h" -#ifndef _VPEER -int32_t balanceInit() { return 0; } -void balanceCleanUp() {} -void balanceNotify() {} +#ifndef _SYNC -int32_t balanceAllocVnodes(SVgObj *pVgroup) { +int32_t replicaInit() { return TSDB_CODE_SUCCESS; } +void replicaCleanUp() {} +void replicaNotify() {} +void replicaReset() {} +int32_t replicaForwardReqToPeer(void *pHead) { return TSDB_CODE_SUCCESS; } + +int32_t replicaAllocVnodes(SVgObj *pVgroup) { void * pNode = NULL; SDnodeObj *pDnode = NULL; SDnodeObj *pSelDnode = NULL; diff --git a/src/mnode/src/mgmtSdb.c b/src/mnode/src/mgmtSdb.c index 12fab55875..7fdac8fd60 100644 --- a/src/mnode/src/mgmtSdb.c +++ b/src/mnode/src/mgmtSdb.c @@ -18,11 +18,12 @@ #include "taoserror.h" #include "tlog.h" #include "trpc.h" +#include "treplica.h" #include "tqueue.h" #include "twal.h" #include "hashint.h" #include "hashstr.h" -#include "mpeer.h" +#include "mgmtMnode.h" #include "mgmtSdb.h" typedef struct _SSdbTable { @@ -131,7 +132,7 @@ int32_t sdbInit() { sdbTrace("sdb is initialized, version:%d totalRows:%d numOfTables:%d", tsSdbObj->version, totalRows, numOfTables); - mpeerUpdateSync(); + replicaNotify(); return TSDB_CODE_SUCCESS; } @@ -264,7 +265,7 @@ static int32_t sdbProcessWriteFromApp(SSdbTable *pTable, SWalHead *pHead, int32_ tsSdbObj->version++; pHead->version = tsSdbObj->version; - code = mpeerForwardReqToPeer(pHead); + code = replicaForwardReqToPeer(pHead); if (code != TSDB_CODE_SUCCESS) { pthread_mutex_unlock(&tsSdbObj->mutex); sdbError("table:%s, failed to forward %s record:%s from file, version:%" PRId64 ", reason:%s", pTable->tableName, diff --git a/src/mnode/src/mgmtShell.c b/src/mnode/src/mgmtShell.c index 82eb2bae1e..8633a6359e 100644 --- a/src/mnode/src/mgmtShell.c +++ b/src/mnode/src/mgmtShell.c @@ -23,11 +23,10 @@ #include "dnode.h" #include "mnode.h" #include "mgmtAcct.h" -#include "tbalance.h" #include "mgmtDb.h" #include "mgmtDnode.h" #include "tgrant.h" -#include "mpeer.h" +#include "mgmtMnode.h" #include "mgmtProfile.h" #include "mgmtSdb.h" #include "mgmtShell.h" @@ -141,7 +140,7 @@ static void mgmtProcessMsgFromShell(SRpcMsg *rpcMsg) { return; } - if (!mpeerIsMaster()) { + if (!mgmtIsMaster()) { // rpcSendRedirectRsp(rpcMsg->handle, mgmtGetMnodeIpListForRedirect()); mgmtSendSimpleResp(rpcMsg->handle, TSDB_CODE_NO_MASTER); rpcFreeCont(rpcMsg->pCont); @@ -329,12 +328,8 @@ static void mgmtProcessHeartBeatMsg(SQueuedMsg *pMsg) { return; } - if (pMsg->usePublicIp) { - mpeerGetPublicIpList(&pHBRsp->ipList); - } else { - mpeerGetPrivateIpList(&pHBRsp->ipList); - } - + mgmtGetMnodeIpList(&pHBRsp->ipList, pMsg->usePublicIp); + /* * TODO * Dispose kill stream or kill query message @@ -415,12 +410,8 @@ static void mgmtProcessConnectMsg(SQueuedMsg *pMsg) { pConnectRsp->writeAuth = pUser->writeAuth; pConnectRsp->superAuth = pUser->superAuth; - if (pMsg->usePublicIp) { - mpeerGetPublicIpList(&pConnectRsp->ipList); - } else { - mpeerGetPrivateIpList(&pConnectRsp->ipList); - } - + mgmtGetMnodeIpList(&pConnectRsp->ipList, pMsg->usePublicIp); + connect_over: rpcRsp.code = code; if (code != TSDB_CODE_SUCCESS) { diff --git a/src/mnode/src/mgmtTable.c b/src/mnode/src/mgmtTable.c index 65dfb06ad5..dff6f97209 100644 --- a/src/mnode/src/mgmtTable.c +++ b/src/mnode/src/mgmtTable.c @@ -29,7 +29,7 @@ #include "mgmtDnode.h" #include "mgmtDServer.h" #include "tgrant.h" -#include "mpeer.h" +#include "mgmtMnode.h" #include "mgmtProfile.h" #include "mgmtSdb.h" #include "mgmtShell.h" diff --git a/src/mnode/src/mgmtUser.c b/src/mnode/src/mgmtUser.c index 7a7f2999b3..3077f0005d 100644 --- a/src/mnode/src/mgmtUser.c +++ b/src/mnode/src/mgmtUser.c @@ -20,7 +20,7 @@ #include "tutil.h" #include "mgmtAcct.h" #include "tgrant.h" -#include "mpeer.h" +#include "mgmtMnode.h" #include "mgmtSdb.h" #include "mgmtShell.h" #include "mgmtUser.h" diff --git a/src/mnode/src/mgmtVgroup.c b/src/mnode/src/mgmtVgroup.c index ee9afd9586..0af2af93ab 100644 --- a/src/mnode/src/mgmtVgroup.c +++ b/src/mnode/src/mgmtVgroup.c @@ -17,14 +17,14 @@ #include "os.h" #include "taoserror.h" #include "tlog.h" -#include "tbalance.h" #include "tsync.h" +#include "treplica.h" #include "mgmtDnode.h" #include "mnode.h" #include "mgmtDb.h" #include "mgmtDClient.h" #include "mgmtDServer.h" -#include "mpeer.h" +#include "mgmtMnode.h" #include "mgmtProfile.h" #include "mgmtSdb.h" #include "mgmtShell.h" @@ -244,7 +244,7 @@ void mgmtCreateVgroup(SQueuedMsg *pMsg, SDbObj *pDb) { strcpy(pVgroup->dbName, pDb->name); pVgroup->numOfVnodes = pDb->cfg.replications; pVgroup->createdTime = taosGetTimestampMs(); - if (balanceAllocVnodes(pVgroup) != 0) { + if (replicaAllocVnodes(pVgroup) != 0) { mError("db:%s, no enough dnode to alloc %d vnodes to vgroup", pDb->name, pVgroup->numOfVnodes); free(pVgroup); mgmtSendSimpleResp(pMsg->thandle, TSDB_CODE_NO_ENOUGH_DNODES); From f18e32173a0249ea4f645d6275697bb93b846a57 Mon Sep 17 00:00:00 2001 From: Jeff Tao Date: Thu, 16 Apr 2020 20:01:11 +0800 Subject: [PATCH 05/14] change the structure name --- src/util/inc/tsched.h | 11 +++-------- 1 file changed, 3 insertions(+), 8 deletions(-) diff --git a/src/util/inc/tsched.h b/src/util/inc/tsched.h index c9ce6b388f..3e481cbc32 100644 --- a/src/util/inc/tsched.h +++ b/src/util/inc/tsched.h @@ -20,22 +20,17 @@ extern "C" { #endif -typedef struct _sched_msg { - void (*fp)(struct _sched_msg *); - +typedef struct SSchedMsg { + void (*fp)(struct SSchedMsg *); void (*tfp)(void *, void *); - void *msg; void *ahandle; void *thandle; } SSchedMsg; void *taosInitScheduler(int queueSize, int numOfThreads, const char *label); - void *taosInitSchedulerWithInfo(int queueSize, int numOfThreads, const char *label, void *tmrCtrl); - -int taosScheduleTask(void *qhandle, SSchedMsg *pMsg); - +int taosScheduleTask(void *qhandle, SSchedMsg *pMsg); void taosCleanUpScheduler(void *param); #ifdef __cplusplus From 4fd24378d987a7d5211707bfad8bbd6c57f8ac2e Mon Sep 17 00:00:00 2001 From: hzcheng Date: Thu, 16 Apr 2020 22:59:46 +0800 Subject: [PATCH 06/14] refactor tsdb.h --- src/inc/tsdb.h | 96 ++++++++--------------------------------- src/tsdb/inc/tsdbMain.h | 71 ++++++++++++++++++++---------- 2 files changed, 68 insertions(+), 99 deletions(-) diff --git a/src/inc/tsdb.h b/src/inc/tsdb.h index e385239dba..b787869c38 100644 --- a/src/inc/tsdb.h +++ b/src/inc/tsdb.h @@ -20,10 +20,10 @@ #include #include "dataformat.h" +#include "name.h" #include "taosdef.h" #include "taosmsg.h" #include "tarray.h" -#include "name.h" #ifdef __cplusplus extern "C" { @@ -62,14 +62,11 @@ void tsdbFreeCfg(STsdbCfg *pCfg); // --------- TSDB REPOSITORY DEFINITION typedef void tsdb_repo_t; // use void to hide implementation details from outside -int tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter); -int32_t tsdbDropRepo(tsdb_repo_t *repo); -tsdb_repo_t * tsdbOpenRepo(char *tsdbDir, STsdbAppH *pAppH); -int32_t tsdbCloseRepo(tsdb_repo_t *repo); -int32_t tsdbConfigRepo(tsdb_repo_t *repo, STsdbCfg *pCfg); -int32_t tsdbTriggerCommit(tsdb_repo_t *repo); -int32_t tsdbLockRepo(tsdb_repo_t *repo); -int32_t tsdbUnLockRepo(tsdb_repo_t *repo); +int tsdbCreateRepo(char *rootDir, STsdbCfg *pCfg, void *limiter); +int32_t tsdbDropRepo(tsdb_repo_t *repo); +tsdb_repo_t *tsdbOpenRepo(char *tsdbDir, STsdbAppH *pAppH); +int32_t tsdbCloseRepo(tsdb_repo_t *repo); +int32_t tsdbConfigRepo(tsdb_repo_t *repo, STsdbCfg *pCfg); // --------- TSDB TABLE DEFINITION typedef struct { @@ -99,27 +96,6 @@ int tsdbCreateTable(tsdb_repo_t *repo, STableCfg *pCfg); int tsdbDropTable(tsdb_repo_t *pRepo, STableId tableId); int tsdbAlterTable(tsdb_repo_t *repo, STableCfg *pCfg); -typedef struct { - int32_t totalLen; - int32_t len; - SDataRow row; -} SSubmitBlkIter; - -int tsdbInitSubmitBlkIter(SSubmitBlk *pBlock, SSubmitBlkIter *pIter); -SDataRow tsdbGetSubmitBlkNext(SSubmitBlkIter *pIter); - -#define TSDB_SUBMIT_MSG_HEAD_SIZE sizeof(SSubmitMsg) - -// SSubmitMsg Iterator -typedef struct { - int32_t totalLen; - int32_t len; - SSubmitBlk *pBlock; -} SSubmitMsgIter; - -int tsdbInitSubmitMsgIter(SSubmitMsg *pMsg, SSubmitMsgIter *pIter); -SSubmitBlk *tsdbGetSubmitMsgNext(SSubmitMsgIter *pIter); - // the TSDB repository info typedef struct STsdbRepoInfo { STsdbCfg tsdbCfg; @@ -137,35 +113,7 @@ typedef struct { int64_t tableTotalDataSize; // In bytes int64_t tableTotalDiskSize; // In bytes } STableInfo; -STableInfo * tsdbGetTableInfo(tsdb_repo_t *pRepo, STableId tid); - -// -- For table manipulation - -/** - * Create/Alter a table in a TSDB repository handle - * @param repo the TSDB repository handle - * @param pCfg the table configurations, the upper layer should free the pointer - * - * @return 0 for success, -1 for failure and the error number is set - */ - -/** - * Drop a table in a repository and free all the resources it takes - * @param pRepo the TSDB repository handle - * @param tid the ID of the table to drop - * @param error the error number to set when failure occurs - * - * @return 0 for success, -1 for failure and the error number is set - */ - -/** - * Get the information of a table in the repository - * @param pRepo the TSDB repository handle - * @param tid the ID of the table to drop - * @param error the error number to set when failure occurs - * - * @return a table information handle for success, NULL for failure and the error number is set - */ +STableInfo *tsdbGetTableInfo(tsdb_repo_t *pRepo, STableId tid); // -- FOR INSERT DATA /** @@ -179,18 +127,18 @@ int32_t tsdbInsertData(tsdb_repo_t *pRepo, SSubmitMsg *pMsg); // -- FOR QUERY TIME SERIES DATA -typedef void* tsdb_query_handle_t; // Use void to hide implementation details +typedef void *tsdb_query_handle_t; // Use void to hide implementation details -typedef struct STableGroupList { // qualified table object list in group - SArray* pGroupList; - int32_t numOfTables; +typedef struct STableGroupList { // qualified table object list in group + SArray *pGroupList; + int32_t numOfTables; } STableGroupList; // query condition to build vnode iterator typedef struct STsdbQueryCond { - STimeWindow twindow; - int32_t order; // desc/asc order to iterate the data block - SColumnInfoData* colList; + STimeWindow twindow; + int32_t order; // desc/asc order to iterate the data block + SColumnInfoData *colList; } STsdbQueryCond; typedef struct SBlockInfo { @@ -202,12 +150,6 @@ typedef struct SBlockInfo { STableId tableId; } SBlockInfo; -// TODO: move this data struct out of the module -//typedef struct SData { -// int32_t num; -// char * data; -//} SData; - typedef struct SDataBlockInfo { STimeWindow window; int32_t rows; @@ -235,7 +177,7 @@ typedef void *tsdbpos_t; * @param pTableList table sid list * @return */ -tsdb_query_handle_t *tsdbQueryTables(tsdb_repo_t* tsdb, STsdbQueryCond *pCond, SArray *idList, SArray *pColumnInfo); +tsdb_query_handle_t *tsdbQueryTables(tsdb_repo_t *tsdb, STsdbQueryCond *pCond, SArray *idList, SArray *pColumnInfo); /** * move to next block @@ -284,7 +226,7 @@ SArray *tsdbRetrieveDataBlock(tsdb_query_handle_t *pQueryHandle, SArray *pIdList * @param order ascending order or descending order * @return */ -int32_t tsdbResetQuery(tsdb_query_handle_t *pQueryHandle, STimeWindow* window, tsdbpos_t position, int16_t order); +int32_t tsdbResetQuery(tsdb_query_handle_t *pQueryHandle, STimeWindow *window, tsdbpos_t position, int16_t order); /** * return the access position of current query handle @@ -337,10 +279,10 @@ SArray *tsdbGetTableList(tsdb_query_handle_t *pQueryHandle); * @param pTagCond. tag query condition * */ -int32_t tsdbQueryTags(tsdb_repo_t* tsdb, int64_t uid, const char* pTagCond, size_t len, SArray** pGroupList, - SColIndex* pColIndex, int32_t numOfCols); +int32_t tsdbQueryTags(tsdb_repo_t *tsdb, int64_t uid, const char *pTagCond, size_t len, SArray **pGroupList, + SColIndex *pColIndex, int32_t numOfCols); -int32_t tsdbGetOneTableGroup(tsdb_repo_t* tsdb, int64_t uid, SArray** pGroupList); +int32_t tsdbGetOneTableGroup(tsdb_repo_t *tsdb, int64_t uid, SArray **pGroupList); /** * clean up the query handle diff --git a/src/tsdb/inc/tsdbMain.h b/src/tsdb/inc/tsdbMain.h index d9fed4327b..ce6999a9e1 100644 --- a/src/tsdb/inc/tsdbMain.h +++ b/src/tsdb/inc/tsdbMain.h @@ -15,9 +15,9 @@ #ifndef _TD_TSDB_MAIN_H_ #define _TD_TSDB_MAIN_H_ -#include "tsdb.h" -#include "tlist.h" #include "tglobalcfg.h" +#include "tlist.h" +#include "tsdb.h" #include "tskiplist.h" #include "tutil.h" @@ -90,9 +90,9 @@ typedef struct { STable *superList; // super table list TODO: change it to list container - void *map; // table map of (uid ===> table) + void *map; // table map of (uid ===> table) - SMetaFile *mfh; // meta file handle + SMetaFile *mfh; // meta file handle int maxRowBytes; int maxCols; } STsdbMeta; @@ -119,14 +119,14 @@ STSchema * tsdbGetTableTagSchema(STsdbMeta *pMeta, STable *pTable); #define TSDB_TABLE_OF_ID(pHandle, id) ((pHandle)->pTables)[id] #define TSDB_GET_TABLE_OF_NAME(pHandle, name) /* TODO */ -STsdbMeta* tsdbGetMeta(tsdb_repo_t* pRepo); +STsdbMeta *tsdbGetMeta(tsdb_repo_t *pRepo); int32_t tsdbCreateTableImpl(STsdbMeta *pMeta, STableCfg *pCfg); int32_t tsdbDropTableImpl(STsdbMeta *pMeta, STableId tableId); STable *tsdbIsValidTableToInsert(STsdbMeta *pMeta, STableId tableId); // int32_t tsdbInsertRowToTableImpl(SSkipListNode *pNode, STable *pTable); STable *tsdbGetTableByUid(STsdbMeta *pMeta, int64_t uid); -char *getTupleKey(const void * data); +char * getTupleKey(const void *data); // ------------------------------ TSDB CACHE INTERFACES ------------------------------ #define TSDB_DEFAULT_CACHE_BLOCK_SIZE 16 * 1024 * 1024 /* 16M */ @@ -192,8 +192,8 @@ typedef struct { } SFileInfo; typedef struct { - int fd; - char fname[128]; + int fd; + char fname[128]; SFileInfo info; } SFile; @@ -217,13 +217,15 @@ typedef struct { STsdbFileH *tsdbInitFileH(char *dataDir, int maxFiles); void tsdbCloseFileH(STsdbFileH *pFileH); -int tsdbCreateFile(char *dataDir, int fileId, const char *suffix, int maxTables, SFile *pFile, int writeHeader, int toClose); +int tsdbCreateFile(char *dataDir, int fileId, const char *suffix, int maxTables, SFile *pFile, int writeHeader, + int toClose); int tsdbCreateFGroup(STsdbFileH *pFileH, char *dataDir, int fid, int maxTables); int tsdbOpenFile(SFile *pFile, int oflag); -int tsdbCloseFile(SFile *pFile); SFileGroup *tsdbOpenFilesForCommit(STsdbFileH *pFileH, int fid); +int tsdbCloseFile(SFile *pFile); +SFileGroup *tsdbOpenFilesForCommit(STsdbFileH *pFileH, int fid); int tsdbRemoveFileGroup(STsdbFileH *pFile, int fid); -#define TSDB_FGROUP_ITER_FORWARD TSDB_ORDER_ASC +#define TSDB_FGROUP_ITER_FORWARD TSDB_ORDER_ASC #define TSDB_FGROUP_ITER_BACKWARD TSDB_ORDER_DESC typedef struct { @@ -278,15 +280,15 @@ typedef struct { } SCompInfo; #define TSDB_COMPBLOCK_AT(pCompInfo, idx) ((pCompInfo)->blocks + (idx)) -#define TSDB_COMPBLOCK_GET_START_AND_SIZE(pCompInfo, pCompBlock, size)\ -do {\ - if (pCompBlock->numOfSubBlocks > 1) {\ - pCompBlock = pCompInfo->blocks + pCompBlock->offset;\ - size = pCompBlock->numOfSubBlocks;\ - } else {\ - size = 1;\ - }\ -} while (0) +#define TSDB_COMPBLOCK_GET_START_AND_SIZE(pCompInfo, pCompBlock, size) \ + do { \ + if (pCompBlock->numOfSubBlocks > 1) { \ + pCompBlock = pCompInfo->blocks + pCompBlock->offset; \ + size = pCompBlock->numOfSubBlocks; \ + } else { \ + size = 1; \ + } \ + } while (0) // TODO: take pre-calculation into account typedef struct { @@ -304,9 +306,10 @@ typedef struct { SCompCol cols[]; } SCompData; -STsdbFileH* tsdbGetFile(tsdb_repo_t* pRepo); +STsdbFileH *tsdbGetFile(tsdb_repo_t *pRepo); -int tsdbCopyBlockDataInFile(SFile *pOutFile, SFile *pInFile, SCompInfo *pCompInfo, int idx, int isLast, SDataCols *pCols); +int tsdbCopyBlockDataInFile(SFile *pOutFile, SFile *pInFile, SCompInfo *pCompInfo, int idx, int isLast, + SDataCols *pCols); int tsdbLoadCompIdx(SFileGroup *pGroup, void *buf, int maxTables); int tsdbLoadCompBlocks(SFileGroup *pGroup, SCompIdx *pIdx, void *buf); @@ -350,6 +353,30 @@ typedef struct _tsdb_repo { } STsdbRepo; +typedef struct { + int32_t totalLen; + int32_t len; + SDataRow row; +} SSubmitBlkIter; + +int tsdbInitSubmitBlkIter(SSubmitBlk *pBlock, SSubmitBlkIter *pIter); +SDataRow tsdbGetSubmitBlkNext(SSubmitBlkIter *pIter); + +#define TSDB_SUBMIT_MSG_HEAD_SIZE sizeof(SSubmitMsg) + +// SSubmitMsg Iterator +typedef struct { + int32_t totalLen; + int32_t len; + SSubmitBlk *pBlock; +} SSubmitMsgIter; + +int tsdbInitSubmitMsgIter(SSubmitMsg *pMsg, SSubmitMsgIter *pIter); +SSubmitBlk *tsdbGetSubmitMsgNext(SSubmitMsgIter *pIter); + +int32_t tsdbTriggerCommit(tsdb_repo_t *repo); +int32_t tsdbLockRepo(tsdb_repo_t *repo); +int32_t tsdbUnLockRepo(tsdb_repo_t *repo); #ifdef __cplusplus } From 19b1d449de3a602ff1b9e7548cff3a955937fcc4 Mon Sep 17 00:00:00 2001 From: slguan Date: Fri, 17 Apr 2020 00:07:09 +0800 Subject: [PATCH 07/14] rearrange code directory --- src/{util => common}/src/tstring.c | 0 src/dnode/inc/dnodeMClient.h | 1 - src/dnode/inc/dnodeModule.h | 6 +- src/dnode/src/dnodeMain.c | 1 - src/dnode/src/dnodeModule.c | 151 +++++++++--------- src/inc/dnode.h | 4 +- src/inc/mnode.h | 233 --------------------------- src/inc/taosdef.h | 13 +- src/inc/treplica.h | 8 +- src/mnode/inc/mgmtAcct.h | 16 +- src/mnode/inc/mgmtDb.h | 2 +- src/mnode/inc/mgmtDef.h | 246 +++++++++++++++++++++++++++++ src/mnode/inc/mgmtDnode.h | 20 +-- src/mnode/inc/mgmtLog.h | 68 ++++++++ src/mnode/inc/mgmtMnode.h | 10 +- src/mnode/inc/mgmtProfile.h | 2 +- src/mnode/inc/mgmtShell.h | 2 +- src/mnode/inc/mgmtTable.h | 5 +- src/mnode/inc/mgmtUser.h | 2 +- src/mnode/inc/mgmtVgroup.h | 4 +- src/mnode/src/mgmtAcct.c | 5 +- src/mnode/src/mgmtDClient.c | 6 +- src/mnode/src/mgmtDServer.c | 8 +- src/mnode/src/mgmtDb.c | 7 +- src/mnode/src/mgmtDnode.c | 229 ++++++--------------------- src/mnode/src/mgmtGrant.c | 2 +- src/mnode/src/mgmtMain.c | 80 +++++----- src/mnode/src/mgmtMnode.c | 11 +- src/mnode/src/mgmtProfile.c | 4 + src/mnode/src/mgmtReplica.c | 3 +- src/mnode/src/mgmtSdb.c | 1 + src/mnode/src/mgmtShell.c | 6 +- src/mnode/src/mgmtTable.c | 4 +- src/mnode/src/mgmtUser.c | 2 + src/mnode/src/mgmtVgroup.c | 9 +- src/rpc/CMakeLists.txt | 2 +- src/tsdb/inc/tsdb.h | 4 +- src/tsdb/src/tsdbMain.c | 2 +- src/util/inc/tlog.h | 39 ----- src/util/inc/tmodule.h | 57 ------- src/util/src/tmodule.c | 20 --- tests/tsim/inc/sim.h | 1 - 42 files changed, 569 insertions(+), 727 deletions(-) rename src/{util => common}/src/tstring.c (100%) create mode 100644 src/mnode/inc/mgmtDef.h create mode 100644 src/mnode/inc/mgmtLog.h delete mode 100644 src/util/inc/tmodule.h delete mode 100644 src/util/src/tmodule.c diff --git a/src/util/src/tstring.c b/src/common/src/tstring.c similarity index 100% rename from src/util/src/tstring.c rename to src/common/src/tstring.c diff --git a/src/dnode/inc/dnodeMClient.h b/src/dnode/inc/dnodeMClient.h index 594fb84d3b..a8e97dd9de 100644 --- a/src/dnode/inc/dnodeMClient.h +++ b/src/dnode/inc/dnodeMClient.h @@ -23,7 +23,6 @@ extern "C" { int32_t dnodeInitMClient(); void dnodeCleanupMClient(); void dnodeSendMsgToMnode(SRpcMsg *rpcMsg); -uint32_t dnodeGetMnodeMasteIp(); void * dnodeGetMpeerInfos(); int32_t dnodeGetDnodeId(); diff --git a/src/dnode/inc/dnodeModule.h b/src/dnode/inc/dnodeModule.h index 728630748f..b6b57be3d1 100644 --- a/src/dnode/inc/dnodeModule.h +++ b/src/dnode/inc/dnodeModule.h @@ -21,9 +21,9 @@ extern "C" { #endif int32_t dnodeInitModules(); -void dnodeCleanUpModules(); -void dnodeStartModules(); -void dnodeProcessModuleStatus(uint32_t moduleStatus); +void dnodeStartModules(); +void dnodeCleanUpModules(); +void dnodeProcessModuleStatus(uint32_t moduleStatus); #ifdef __cplusplus } diff --git a/src/dnode/src/dnodeMain.c b/src/dnode/src/dnodeMain.c index c80de0ce6e..a4e7b55995 100644 --- a/src/dnode/src/dnodeMain.c +++ b/src/dnode/src/dnodeMain.c @@ -17,7 +17,6 @@ #include "os.h" #include "tglobalcfg.h" #include "tlog.h" -#include "tmodule.h" #include "trpc.h" #include "tutil.h" #include "dnode.h" diff --git a/src/dnode/src/dnodeModule.c b/src/dnode/src/dnodeModule.c index 7a2facb255..c50391613e 100644 --- a/src/dnode/src/dnodeModule.c +++ b/src/dnode/src/dnodeModule.c @@ -15,55 +15,74 @@ #define _DEFAULT_SOURCE #include "os.h" +#include "taosdef.h" #include "tlog.h" -#include "tmodule.h" #include "tglobalcfg.h" #include "mnode.h" #include "http.h" #include "monitor.h" #include "dnodeModule.h" -#include "dnode.h" + +typedef struct { + bool enable; + char * name; + int32_t (*initFp)(); + int32_t (*startFp)(); + void (*cleanUpFp)(); + void (*stopFp)(); +} SModule; + +static SModule tsModule[TSDB_MOD_MAX] = {0}; +static uint32_t tsModuleStatus = 0; + +static void dnodeSetModuleStatus(int32_t module) { + tsModuleStatus |= (1 << module); +} + +static void dnodeUnSetModuleStatus(int32_t module) { + tsModuleStatus &= ~(1 << module); +} static void dnodeAllocModules() { - tsModule[TSDB_MOD_MGMT].name = "mgmt"; - tsModule[TSDB_MOD_MGMT].initFp = mgmtInitSystem; - tsModule[TSDB_MOD_MGMT].cleanUpFp = mgmtCleanUpSystem; - tsModule[TSDB_MOD_MGMT].startFp = mgmtStartSystem; - tsModule[TSDB_MOD_MGMT].stopFp = mgmtStopSystem; - tsModule[TSDB_MOD_MGMT].num = tsNumOfMPeers; - tsModule[TSDB_MOD_MGMT].curNum = 0; - tsModule[TSDB_MOD_MGMT].equalVnodeNum = tsMgmtEqualVnodeNum; - - tsModule[TSDB_MOD_HTTP].name = "http"; - tsModule[TSDB_MOD_HTTP].initFp = httpInitSystem; - tsModule[TSDB_MOD_HTTP].cleanUpFp = httpCleanUpSystem; - tsModule[TSDB_MOD_HTTP].startFp = httpStartSystem; - tsModule[TSDB_MOD_HTTP].stopFp = httpStopSystem; - tsModule[TSDB_MOD_HTTP].num = (tsEnableHttpModule == 1) ? -1 : 0; - tsModule[TSDB_MOD_HTTP].curNum = 0; - tsModule[TSDB_MOD_HTTP].equalVnodeNum = 0; - - tsModule[TSDB_MOD_MONITOR].name = "monitor"; - tsModule[TSDB_MOD_MONITOR].initFp = monitorInitSystem; - tsModule[TSDB_MOD_MONITOR].cleanUpFp = monitorCleanUpSystem; - tsModule[TSDB_MOD_MONITOR].startFp = monitorStartSystem; - tsModule[TSDB_MOD_MONITOR].stopFp = monitorStopSystem; - tsModule[TSDB_MOD_MONITOR].num = (tsEnableMonitorModule == 1) ? -1 : 0; - tsModule[TSDB_MOD_MONITOR].curNum = 0; - tsModule[TSDB_MOD_MONITOR].equalVnodeNum = 0; + tsModule[TSDB_MOD_MGMT].name = false; + tsModule[TSDB_MOD_MGMT].name = "mgmt"; + tsModule[TSDB_MOD_MGMT].initFp = mgmtInitSystem; + tsModule[TSDB_MOD_MGMT].cleanUpFp = mgmtCleanUpSystem; + tsModule[TSDB_MOD_MGMT].startFp = mgmtStartSystem; + tsModule[TSDB_MOD_MGMT].stopFp = mgmtStopSystem; + + tsModule[TSDB_MOD_HTTP].enable = (tsEnableHttpModule == 1); + tsModule[TSDB_MOD_HTTP].name = "http"; + tsModule[TSDB_MOD_HTTP].initFp = httpInitSystem; + tsModule[TSDB_MOD_HTTP].cleanUpFp = httpCleanUpSystem; + tsModule[TSDB_MOD_HTTP].startFp = httpStartSystem; + tsModule[TSDB_MOD_HTTP].stopFp = httpStopSystem; + if (tsEnableHttpModule) { + dnodeSetModuleStatus(TSDB_MOD_HTTP); + } + + tsModule[TSDB_MOD_MONITOR].enable = (tsEnableMonitorModule == 1); + tsModule[TSDB_MOD_MONITOR].name = "monitor"; + tsModule[TSDB_MOD_MONITOR].initFp = monitorInitSystem; + tsModule[TSDB_MOD_MONITOR].cleanUpFp = monitorCleanUpSystem; + tsModule[TSDB_MOD_MONITOR].startFp = monitorStartSystem; + tsModule[TSDB_MOD_MONITOR].stopFp = monitorStopSystem; + if (tsEnableMonitorModule) { + dnodeSetModuleStatus(TSDB_MOD_MONITOR); + } } void dnodeCleanUpModules() { - for (int mod = 1; mod < TSDB_MOD_MAX; ++mod) { - if (tsModule[mod].num != 0 && tsModule[mod].stopFp) { - (*tsModule[mod].stopFp)(); + for (int32_t module = 1; module < TSDB_MOD_MAX; ++module) { + if (tsModule[module].enable && tsModule[module].stopFp) { + (*tsModule[module].stopFp)(); } - if (tsModule[mod].num != 0 && tsModule[mod].cleanUpFp) { - (*tsModule[mod].cleanUpFp)(); + if (tsModule[module].cleanUpFp) { + (*tsModule[module].cleanUpFp)(); } } - if (tsModule[TSDB_MOD_MGMT].num != 0 && tsModule[TSDB_MOD_MGMT].cleanUpFp) { + if (tsModule[TSDB_MOD_MGMT].enable && tsModule[TSDB_MOD_MGMT].cleanUpFp) { (*tsModule[TSDB_MOD_MGMT].cleanUpFp)(); } } @@ -71,57 +90,41 @@ void dnodeCleanUpModules() { int32_t dnodeInitModules() { dnodeAllocModules(); - for (int mod = 0; mod < TSDB_MOD_MAX; ++mod) { - if (tsModule[mod].num != 0 && tsModule[mod].initFp) { - if ((*tsModule[mod].initFp)() != 0) { - dError("failed to init modules"); + for (int32_t module = 0; module < TSDB_MOD_MAX; ++module) { + if (tsModule[module].initFp) { + if ((*tsModule[module].initFp)() != 0) { + dError("failed to init module:%s", tsModule[module].name); return -1; } } } - return TSDB_CODE_SUCCESS; + return 0; } void dnodeStartModules() { - // for (int mod = 1; mod < TSDB_MOD_MAX; ++mod) { - // if (tsModule[mod].num != 0 && tsModule[mod].startFp) { - // if ((*tsModule[mod].startFp)() != 0) { - // dError("failed to start module:%d", mod); - // } - // } - // } + for (int32_t module = 1; module < TSDB_MOD_MAX; ++module) { + if (tsModule[module].enable && tsModule[module].startFp) { + if ((*tsModule[module].startFp)() != 0) { + dError("failed to start module:%s", tsModule[module].name); + } + } + } } void dnodeProcessModuleStatus(uint32_t moduleStatus) { - if (moduleStatus == tsModuleStatus) return; - - dPrint("module status is received, old:%d, new:%d", tsModuleStatus, moduleStatus); - - int news = moduleStatus; - int olds = tsModuleStatus; - - for (int moduleType = 0; moduleType < TSDB_MOD_MAX; ++moduleType) { - int newStatus = news & (1 << moduleType); - int oldStatus = olds & (1 << moduleType); - - if (oldStatus > 0) { - if (newStatus == 0) { - if (tsModule[moduleType].stopFp) { - dPrint("module:%s is stopped on this node", tsModule[moduleType].name); - (*tsModule[moduleType].stopFp)(); - } - } - } else if (oldStatus == 0) { - if (newStatus > 0) { - if (tsModule[moduleType].startFp) { - dPrint("module:%s is started on this node", tsModule[moduleType].name); - (*tsModule[moduleType].startFp)(); - } - } - } else { - } + bool enableMgmtModule = moduleStatus & (1 << TSDB_MOD_MGMT); + if (!tsModule[TSDB_MOD_MGMT].enable && enableMgmtModule) { + dPrint("module status is received, start mgmt module", tsModuleStatus, moduleStatus); + tsModule[TSDB_MOD_MGMT].enable = true; + dnodeSetModuleStatus(TSDB_MOD_MGMT); + (*tsModule[TSDB_MOD_MGMT].stopFp)(); } - tsModuleStatus = moduleStatus; + if (tsModule[TSDB_MOD_MGMT].enable && !enableMgmtModule) { + dPrint("module status is received, stop mgmt module", tsModuleStatus, moduleStatus); + tsModule[TSDB_MOD_MGMT].enable = false; + dnodeUnSetModuleStatus(TSDB_MOD_MGMT); + (*tsModule[TSDB_MOD_MGMT].stopFp)(); + } } diff --git a/src/inc/dnode.h b/src/inc/dnode.h index 25ec747ac9..6215b5a7ee 100644 --- a/src/inc/dnode.h +++ b/src/inc/dnode.h @@ -20,9 +20,6 @@ extern "C" { #endif -#include -#include - typedef struct { int32_t queryReqNum; int32_t submitReqNum; @@ -45,6 +42,7 @@ void dnodeFreeRqueue(void *rqueue); void dnodeSendRpcWriteRsp(void *pVnode, void *param, int32_t code); bool dnodeIsFirstDeploy(); +uint32_t dnodeGetMnodeMasteIp(); #ifdef __cplusplus } diff --git a/src/inc/mnode.h b/src/inc/mnode.h index cbd768c295..c30e1e37ba 100644 --- a/src/inc/mnode.h +++ b/src/inc/mnode.h @@ -20,245 +20,12 @@ extern "C" { #endif -#include "os.h" -#include "taosdef.h" -#include "taosmsg.h" -#include "taoserror.h" -#include "tglobalcfg.h" -#include "thash.h" -#include "tidpool.h" -#include "tlog.h" -#include "tmempool.h" -#include "trpc.h" -#include "taosdef.h" -#include "tskiplist.h" -#include "tsocket.h" -#include "ttime.h" -#include "ttimer.h" -#include "tutil.h" - -struct _vg_obj; -struct _db_obj; -struct _acct_obj; -struct _user_obj; -struct _mnode_obj; - -typedef struct _dnode_obj { - int32_t dnodeId; - uint32_t privateIp; - uint32_t publicIp; - uint16_t mnodeShellPort; - uint16_t mnodeDnodePort; - uint16_t dnodeShellPort; - uint16_t dnodeMnodePort; - uint16_t syncPort; - uint32_t moduleStatus; - int64_t createdTime; - uint32_t lastAccess; - int32_t openVnodes; - int32_t totalVnodes; // from dnode status msg, config information - uint16_t numOfCores; // from dnode status msg - int8_t alternativeRole; // from dnode status msg, 0-any, 1-mgmt, 2-dnode - int8_t status; // set in balance function - int32_t customScore; // config by user - char dnodeName[TSDB_NODE_NAME_LEN + 1]; - int8_t reserved[15]; - int8_t updateEnd[1]; - int32_t refCount; - SVnodeLoad vload[TSDB_MAX_VNODES]; - uint32_t lastReboot; // time stamp for last reboot - float score; // calc in balance function - float diskAvailable; // from dnode status msg - int16_t diskAvgUsage; // calc from sys.disk - int16_t cpuAvgUsage; // calc from sys.cpu - int16_t memoryAvgUsage; // calc from sys.mem - int16_t bandwidthUsage; // calc from sys.band -} SDnodeObj; - -typedef struct _mnode_obj { - int32_t mnodeId; - int64_t createdTime; - int8_t reserved[14]; - int8_t updateEnd[1]; - int32_t refCount; - int8_t role; - SDnodeObj *pDnode; -} SMnodeObj; - - -typedef struct { - int32_t dnodeId; - uint32_t privateIp; - uint32_t publicIp; -} SVnodeGid; - -typedef struct { - char tableId[TSDB_TABLE_ID_LEN + 1]; - int8_t type; -} STableObj; - -typedef struct SSuperTableObj { - STableObj info; - uint64_t uid; - int64_t createdTime; - int32_t sversion; - int32_t numOfColumns; - int32_t numOfTags; - int8_t reserved[15]; - int8_t updateEnd[1]; - int32_t refCount; - int32_t numOfTables; - int16_t nextColId; - SSchema * schema; - int32_t vgLen; - int32_t * vgList; -} SSuperTableObj; - -typedef struct { - STableObj info; - uint64_t uid; - int64_t createdTime; - int32_t sversion; //used by normal table - int32_t numOfColumns; //used by normal table - int32_t sid; - int32_t vgId; - char superTableId[TSDB_TABLE_ID_LEN + 1]; - int32_t sqlLen; - int8_t reserved[1]; - int8_t updateEnd[1]; - int16_t nextColId; //used by normal table - int32_t refCount; - char* sql; //used by normal table - SSchema* schema; //used by normal table - SSuperTableObj *superTable; -} SChildTableObj; - -typedef struct _vg_obj { - uint32_t vgId; - char dbName[TSDB_DB_NAME_LEN + 1]; - int64_t createdTime; - SVnodeGid vnodeGid[TSDB_VNODES_SUPPORT]; - int32_t numOfVnodes; - int32_t lbDnodeId; - int32_t lbTime; - int8_t status; - int8_t inUse; - int8_t reserved[13]; - int8_t updateEnd[1]; - int32_t refCount; - struct _vg_obj *prev, *next; - struct _db_obj *pDb; - int32_t numOfTables; - void * idPool; - SChildTableObj ** tableList; -} SVgObj; - -typedef struct _db_obj { - char name[TSDB_DB_NAME_LEN + 1]; - int8_t status; - int64_t createdTime; - SDbCfg cfg; - int8_t reserved[15]; - int8_t updateEnd[1]; - int32_t refCount; - int32_t numOfVgroups; - int32_t numOfTables; - int32_t numOfSuperTables; - SVgObj *pHead; - SVgObj *pTail; - struct _acct_obj *pAcct; -} SDbObj; - -typedef struct _user_obj { - char user[TSDB_USER_LEN + 1]; - char pass[TSDB_KEY_LEN + 1]; - char acct[TSDB_USER_LEN + 1]; - int64_t createdTime; - int8_t superAuth; - int8_t writeAuth; - int8_t reserved[13]; - int8_t updateEnd[1]; - int32_t refCount; - struct _acct_obj * pAcct; - SQqueryList * pQList; // query list - SStreamList * pSList; // stream list -} SUserObj; - -typedef struct { - int32_t numOfUsers; - int32_t numOfDbs; - int32_t numOfTimeSeries; - int32_t numOfPointsPerSecond; - int32_t numOfConns; - int32_t numOfQueries; - int32_t numOfStreams; - int64_t totalStorage; // Total storage wrtten from this account - int64_t compStorage; // Compressed storage on disk - int64_t queryTime; - int64_t totalPoints; - int64_t inblound; - int64_t outbound; - int64_t sKey; - int8_t accessState; // Checked by mgmt heartbeat message -} SAcctInfo; - -typedef struct _acct_obj { - char user[TSDB_USER_LEN + 1]; - char pass[TSDB_KEY_LEN + 1]; - SAcctCfg cfg; - int32_t acctId; - int64_t createdTime; - int8_t status; - int8_t reserved[14]; - int8_t updateEnd[1]; - int32_t refCount; - SAcctInfo acctInfo; - pthread_mutex_t mutex; -} SAcctObj; - -typedef struct { - int8_t type; - char db[TSDB_DB_NAME_LEN + 1]; - void * pNode; - int16_t numOfColumns; - int32_t rowSize; - int32_t numOfRows; - int32_t numOfReads; - int16_t offset[TSDB_MAX_COLUMNS]; - int16_t bytes[TSDB_MAX_COLUMNS]; - void * signature; - uint16_t payloadLen; - char payload[]; -} SShowObj; - -typedef struct { - uint8_t msgType; - int8_t usePublicIp; - int8_t received; - int8_t successed; - int8_t expected; - int8_t retry; - int8_t maxRetry; - int32_t contLen; - int32_t code; - void *ahandle; - void *thandle; - void *pCont; - SAcctObj *pAcct; - SDnodeObj*pDnode; - SUserObj *pUser; - SDbObj *pDb; - SVgObj *pVgroup; - STableObj *pTable; -} SQueuedMsg; - int32_t mgmtInitSystem(); int32_t mgmtStartSystem(); void mgmtCleanUpSystem(); void mgmtStopSystem(); extern char version[]; -extern void *tsMgmtTmr; extern char tsMnodeDir[]; #ifdef __cplusplus diff --git a/src/inc/taosdef.h b/src/inc/taosdef.h index 2b15e7a4c6..b46986d750 100644 --- a/src/inc/taosdef.h +++ b/src/inc/taosdef.h @@ -319,11 +319,11 @@ void tsDataSwap(void *pLeft, void *pRight, int32_t type, int32_t size); #define TSDB_MAX_NORMAL_TABLES 1000 #define TSDB_MAX_CHILD_TABLES 100000 -enum { +typedef enum { TSDB_PRECISION_MILLI, TSDB_PRECISION_MICRO, TSDB_PRECISION_NANO -}; +} EPrecisionType; typedef enum { TSDB_SUPER_TABLE = 0, // super table @@ -331,7 +331,14 @@ typedef enum { TSDB_NORMAL_TABLE = 2, // ordinary table TSDB_STREAM_TABLE = 3, // table created from stream computing TSDB_TABLE_MAX = 4 -} TSDB_TABLE_TYPE; +} ETableType; + +typedef enum { + TSDB_MOD_MGMT, + TSDB_MOD_HTTP, + TSDB_MOD_MONITOR, + TSDB_MOD_MAX +} EModuleType; #ifdef __cplusplus } diff --git a/src/inc/treplica.h b/src/inc/treplica.h index b8915d64a0..3abed1c4aa 100644 --- a/src/inc/treplica.h +++ b/src/inc/treplica.h @@ -20,16 +20,16 @@ extern "C" { #endif -struct _vg_obj; -struct _dnode_obj; +struct SVgObj; +struct SDnodeObj; int32_t replicaInit(); void replicaCleanUp(); void replicaNotify(); void replicaReset(); -int32_t replicaAllocVnodes(struct _vg_obj *pVgroup); +int32_t replicaAllocVnodes(struct SVgObj *pVgroup); int32_t replicaForwardReqToPeer(void *pHead); -int32_t replicaDropDnode(struct _dnode_obj *pDnode); +int32_t replicaDropDnode(struct SDnodeObj *pDnode); #ifdef __cplusplus } diff --git a/src/mnode/inc/mgmtAcct.h b/src/mnode/inc/mgmtAcct.h index 32d7aa7bfb..67c98d1eb2 100644 --- a/src/mnode/inc/mgmtAcct.h +++ b/src/mnode/inc/mgmtAcct.h @@ -22,20 +22,16 @@ extern "C" { #include "tacct.h" -struct _acct_obj; -struct _user_obj; -struct _db_obj; - int32_t mgmtInitAccts(); void mgmtCleanUpAccts(); void *mgmtGetAcct(char *acctName); -void mgmtIncAcctRef(struct _acct_obj *pAcct); -void mgmtDecAcctRef(struct _acct_obj *pAcct); +void mgmtIncAcctRef(SAcctObj *pAcct); +void mgmtDecAcctRef(SAcctObj *pAcct); -void mgmtAddDbToAcct(struct _acct_obj *pAcct, struct _db_obj *pDb); -void mgmtDropDbFromAcct(struct _acct_obj *pAcct, struct _db_obj *pDb); -void mgmtAddUserToAcct(struct _acct_obj *pAcct, struct _user_obj *pUser); -void mgmtDropUserFromAcct(struct _acct_obj *pAcct, struct _user_obj *pUser); +void mgmtAddDbToAcct(SAcctObj *pAcct, SDbObj *pDb); +void mgmtDropDbFromAcct(SAcctObj *pAcct, SDbObj *pDb); +void mgmtAddUserToAcct(SAcctObj *pAcct, SUserObj *pUser); +void mgmtDropUserFromAcct(SAcctObj *pAcct, SUserObj *pUser); #ifdef __cplusplus } diff --git a/src/mnode/inc/mgmtDb.h b/src/mnode/inc/mgmtDb.h index 0479b274bb..920217b9b8 100644 --- a/src/mnode/inc/mgmtDb.h +++ b/src/mnode/inc/mgmtDb.h @@ -20,7 +20,7 @@ extern "C" { #endif -#include "mnode.h" +#include "mgmtDef.h" enum _TSDB_DB_STATUS { TSDB_DB_STATUS_READY, diff --git a/src/mnode/inc/mgmtDef.h b/src/mnode/inc/mgmtDef.h new file mode 100644 index 0000000000..a31500750e --- /dev/null +++ b/src/mnode/inc/mgmtDef.h @@ -0,0 +1,246 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef TDENGINE_MGMT_DEF_H +#define TDENGINE_MGMT_DEF_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "taosdef.h" +#include "taosmsg.h" + +struct SVgObj; +struct SDbObj; +struct SAcctObj; +struct SUserObj; +struct SMnodeObj; + +typedef struct SDnodeObj { + int32_t dnodeId; + uint32_t privateIp; + uint32_t publicIp; + uint16_t mnodeShellPort; + uint16_t mnodeDnodePort; + uint16_t dnodeShellPort; + uint16_t dnodeMnodePort; + uint16_t syncPort; + int64_t createdTime; + uint32_t lastAccess; + int32_t openVnodes; + int32_t totalVnodes; // from dnode status msg, config information + int32_t customScore; // config by user + uint16_t numOfCores; // from dnode status msg + int8_t alternativeRole; // from dnode status msg, 0-any, 1-mgmt, 2-dnode + int8_t status; // set in balance function + int8_t isMgmt; + char dnodeName[TSDB_NODE_NAME_LEN + 1]; + int8_t reserved[15]; + int8_t updateEnd[1]; + int32_t refCount; + SVnodeLoad vload[TSDB_MAX_VNODES]; + uint32_t moduleStatus; + uint32_t lastReboot; // time stamp for last reboot + float score; // calc in balance function + float diskAvailable; // from dnode status msg + int16_t diskAvgUsage; // calc from sys.disk + int16_t cpuAvgUsage; // calc from sys.cpu + int16_t memoryAvgUsage; // calc from sys.mem + int16_t bandwidthUsage; // calc from sys.band +} SDnodeObj; + +typedef struct SMnodeObj { + int32_t mnodeId; + int64_t createdTime; + int8_t reserved[14]; + int8_t updateEnd[1]; + int32_t refCount; + int8_t role; + SDnodeObj *pDnode; +} SMnodeObj; + + +typedef struct { + int32_t dnodeId; + uint32_t privateIp; + uint32_t publicIp; +} SVnodeGid; + +typedef struct { + char tableId[TSDB_TABLE_ID_LEN + 1]; + int8_t type; +} STableObj; + +typedef struct SSuperTableObj { + STableObj info; + uint64_t uid; + int64_t createdTime; + int32_t sversion; + int32_t numOfColumns; + int32_t numOfTags; + int8_t reserved[15]; + int8_t updateEnd[1]; + int32_t refCount; + int32_t numOfTables; + int16_t nextColId; + SSchema * schema; + int32_t vgLen; + int32_t * vgList; +} SSuperTableObj; + +typedef struct { + STableObj info; + uint64_t uid; + int64_t createdTime; + int32_t sversion; //used by normal table + int32_t numOfColumns; //used by normal table + int32_t sid; + int32_t vgId; + char superTableId[TSDB_TABLE_ID_LEN + 1]; + int32_t sqlLen; + int8_t reserved[1]; + int8_t updateEnd[1]; + int16_t nextColId; //used by normal table + int32_t refCount; + char* sql; //used by normal table + SSchema* schema; //used by normal table + SSuperTableObj *superTable; +} SChildTableObj; + +typedef struct SVgObj { + uint32_t vgId; + char dbName[TSDB_DB_NAME_LEN + 1]; + int64_t createdTime; + SVnodeGid vnodeGid[TSDB_VNODES_SUPPORT]; + int32_t numOfVnodes; + int32_t lbDnodeId; + int32_t lbTime; + int8_t status; + int8_t inUse; + int8_t reserved[13]; + int8_t updateEnd[1]; + int32_t refCount; + struct SVgObj *prev, *next; + struct SDbObj *pDb; + int32_t numOfTables; + void * idPool; + SChildTableObj ** tableList; +} SVgObj; + +typedef struct SDbObj { + char name[TSDB_DB_NAME_LEN + 1]; + int8_t status; + int64_t createdTime; + SDbCfg cfg; + int8_t reserved[15]; + int8_t updateEnd[1]; + int32_t refCount; + int32_t numOfVgroups; + int32_t numOfTables; + int32_t numOfSuperTables; + SVgObj *pHead; + SVgObj *pTail; + struct SAcctObj *pAcct; +} SDbObj; + +typedef struct SUserObj { + char user[TSDB_USER_LEN + 1]; + char pass[TSDB_KEY_LEN + 1]; + char acct[TSDB_USER_LEN + 1]; + int64_t createdTime; + int8_t superAuth; + int8_t writeAuth; + int8_t reserved[13]; + int8_t updateEnd[1]; + int32_t refCount; + struct SAcctObj * pAcct; + SQqueryList * pQList; // query list + SStreamList * pSList; // stream list +} SUserObj; + +typedef struct { + int32_t numOfUsers; + int32_t numOfDbs; + int32_t numOfTimeSeries; + int32_t numOfPointsPerSecond; + int32_t numOfConns; + int32_t numOfQueries; + int32_t numOfStreams; + int64_t totalStorage; // Total storage wrtten from this account + int64_t compStorage; // Compressed storage on disk + int64_t queryTime; + int64_t totalPoints; + int64_t inblound; + int64_t outbound; + int64_t sKey; + int8_t accessState; // Checked by mgmt heartbeat message +} SAcctInfo; + +typedef struct SAcctObj { + char user[TSDB_USER_LEN + 1]; + char pass[TSDB_KEY_LEN + 1]; + SAcctCfg cfg; + int32_t acctId; + int64_t createdTime; + int8_t status; + int8_t reserved[14]; + int8_t updateEnd[1]; + int32_t refCount; + SAcctInfo acctInfo; + pthread_mutex_t mutex; +} SAcctObj; + +typedef struct { + int8_t type; + char db[TSDB_DB_NAME_LEN + 1]; + void * pNode; + int16_t numOfColumns; + int32_t rowSize; + int32_t numOfRows; + int32_t numOfReads; + int16_t offset[TSDB_MAX_COLUMNS]; + int16_t bytes[TSDB_MAX_COLUMNS]; + void * signature; + uint16_t payloadLen; + char payload[]; +} SShowObj; + +typedef struct { + uint8_t msgType; + int8_t usePublicIp; + int8_t received; + int8_t successed; + int8_t expected; + int8_t retry; + int8_t maxRetry; + int32_t contLen; + int32_t code; + void *ahandle; + void *thandle; + void *pCont; + SAcctObj *pAcct; + SDnodeObj*pDnode; + SUserObj *pUser; + SDbObj *pDb; + SVgObj *pVgroup; + STableObj *pTable; +} SQueuedMsg; + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/mnode/inc/mgmtDnode.h b/src/mnode/inc/mgmtDnode.h index f964222960..14bb6e04e5 100644 --- a/src/mnode/inc/mgmtDnode.h +++ b/src/mnode/inc/mgmtDnode.h @@ -20,33 +20,27 @@ extern "C" { #endif -#include -#include -#include - -struct _dnode_obj; - -enum _TAOS_DN_STATUS { +typedef enum { TAOS_DN_STATUS_OFFLINE, TAOS_DN_STATUS_DROPPING, TAOS_DN_STATUS_BALANCING, TAOS_DN_STATUS_READY -}; +} EDnodeStatus; int32_t mgmtInitDnodes(); void mgmtCleanupDnodes(); char* mgmtGetDnodeStatusStr(int32_t dnodeStatus); -bool mgmtCheckModuleInDnode(struct _dnode_obj *pDnode, int moduleType); +bool mgmtCheckModuleInDnode(SDnodeObj *pDnode, int moduleType); void mgmtMonitorDnodeModule(); int32_t mgmtGetDnodesNum(); -void * mgmtGetNextDnode(void *pNode, struct _dnode_obj **pDnode); -void mgmtReleaseDnode(struct _dnode_obj *pDnode); +void * mgmtGetNextDnode(void *pNode, SDnodeObj **pDnode); +void mgmtReleaseDnode(SDnodeObj *pDnode); void * mgmtGetDnode(int32_t dnodeId); void * mgmtGetDnodeByIp(uint32_t ip); -void mgmtUpdateDnode(struct _dnode_obj *pDnode); -int32_t mgmtDropDnode(struct _dnode_obj *pDnode); +void mgmtUpdateDnode(SDnodeObj *pDnode); +int32_t mgmtDropDnode(SDnodeObj *pDnode); #ifdef __cplusplus } diff --git a/src/mnode/inc/mgmtLog.h b/src/mnode/inc/mgmtLog.h new file mode 100644 index 0000000000..401b8ac32c --- /dev/null +++ b/src/mnode/inc/mgmtLog.h @@ -0,0 +1,68 @@ +/* + * Copyright (c) 2019 TAOS Data, Inc. + * + * 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 . + */ + +#ifndef TDENGINE_MGMT_LOG_H +#define TDENGINE_MGMT_LOG_H + +#ifdef __cplusplus +extern "C" { +#endif + +#include "tlog.h" + +// mnode log function +#define mError(...) \ + if (mdebugFlag & DEBUG_ERROR) { \ + tprintf("ERROR MND ", 255, __VA_ARGS__); \ + } +#define mWarn(...) \ + if (mdebugFlag & DEBUG_WARN) { \ + tprintf("WARN MND ", mdebugFlag, __VA_ARGS__); \ + } +#define mTrace(...) \ + if (mdebugFlag & DEBUG_TRACE) { \ + tprintf("MND ", mdebugFlag, __VA_ARGS__); \ + } +#define mPrint(...) \ + { tprintf("MND ", 255, __VA_ARGS__); } + +#define mLError(...) taosLogError(__VA_ARGS__) mError(__VA_ARGS__) +#define mLWarn(...) taosLogWarn(__VA_ARGS__) mWarn(__VA_ARGS__) +#define mLPrint(...) taosLogPrint(__VA_ARGS__) mPrint(__VA_ARGS__) + +#define sdbError(...) \ + if (sdbDebugFlag & DEBUG_ERROR) { \ + tprintf("ERROR MND-SDB ", 255, __VA_ARGS__); \ + } +#define sdbWarn(...) \ + if (sdbDebugFlag & DEBUG_WARN) { \ + tprintf("WARN MND-SDB ", sdbDebugFlag, __VA_ARGS__); \ + } +#define sdbTrace(...) \ + if (sdbDebugFlag & DEBUG_TRACE) { \ + tprintf("MND-SDB ", sdbDebugFlag, __VA_ARGS__); \ + } +#define sdbPrint(...) \ + { tprintf("MND-SDB ", 255, __VA_ARGS__); } + +#define sdbLError(...) taosLogError(__VA_ARGS__) sdbError(__VA_ARGS__) +#define sdbLWarn(...) taosLogWarn(__VA_ARGS__) sdbWarn(__VA_ARGS__) +#define sdbLPrint(...) taosLogPrint(__VA_ARGS__) sdbPrint(__VA_ARGS__) + +#ifdef __cplusplus +} +#endif + +#endif diff --git a/src/mnode/inc/mgmtMnode.h b/src/mnode/inc/mgmtMnode.h index d89ebb0c01..1faa616ceb 100644 --- a/src/mnode/inc/mgmtMnode.h +++ b/src/mnode/inc/mgmtMnode.h @@ -20,13 +20,13 @@ extern "C" { #endif -struct _mnode_obj; +struct SMnodeObj; -enum _TAOS_MN_STATUS { +typedef enum { TAOS_MN_STATUS_OFFLINE, TAOS_MN_STATUS_DROPPING, TAOS_MN_STATUS_READY -}; +} EMnodeStatus; int32_t mgmtInitMnodes(); void mgmtCleanupMnodes(); @@ -36,8 +36,8 @@ int32_t mgmtDropMnode(int32_t dnodeId); void * mgmtGetMnode(int32_t mnodeId); int32_t mgmtGetMnodesNum(); -void * mgmtGetNextMnode(void *pNode, struct _mnode_obj **pMnode); -void mgmtReleaseMnode(struct _mnode_obj *pMnode); +void * mgmtGetNextMnode(void *pNode, struct SMnodeObj **pMnode); +void mgmtReleaseMnode(struct SMnodeObj *pMnode); bool mgmtIsMaster(); diff --git a/src/mnode/inc/mgmtProfile.h b/src/mnode/inc/mgmtProfile.h index b05877f32c..07ed3f0f13 100644 --- a/src/mnode/inc/mgmtProfile.h +++ b/src/mnode/inc/mgmtProfile.h @@ -19,7 +19,7 @@ #ifdef __cplusplus extern "C" { #endif -#include "mnode.h" +#include "mgmtDef.h" int32_t mgmtInitProfile(); void mgmtCleanUpProfile(); diff --git a/src/mnode/inc/mgmtShell.h b/src/mnode/inc/mgmtShell.h index 171c93a390..3941a0a87d 100644 --- a/src/mnode/inc/mgmtShell.h +++ b/src/mnode/inc/mgmtShell.h @@ -19,7 +19,7 @@ #ifdef __cplusplus extern "C" { #endif -#include "mnode.h" +#include "mgmtDef.h" int32_t mgmtInitShell(); void mgmtCleanUpShell(); diff --git a/src/mnode/inc/mgmtTable.h b/src/mnode/inc/mgmtTable.h index 4d3e0f6b43..03d31d8e4b 100644 --- a/src/mnode/inc/mgmtTable.h +++ b/src/mnode/inc/mgmtTable.h @@ -20,10 +20,7 @@ extern "C" { #endif -#include -#include -#include -#include "mnode.h" +#include "mgmtDef.h" int32_t mgmtInitTables(); void mgmtCleanUpTables(); diff --git a/src/mnode/inc/mgmtUser.h b/src/mnode/inc/mgmtUser.h index 6997081c7c..b6a71f6efd 100644 --- a/src/mnode/inc/mgmtUser.h +++ b/src/mnode/inc/mgmtUser.h @@ -19,7 +19,7 @@ #ifdef __cplusplus extern "C" { #endif -#include "mnode.h" +#include "mgmtDef.h" int32_t mgmtInitUsers(); void mgmtCleanUpUsers(); diff --git a/src/mnode/inc/mgmtVgroup.h b/src/mnode/inc/mgmtVgroup.h index 072c616f3d..3da002026b 100644 --- a/src/mnode/inc/mgmtVgroup.h +++ b/src/mnode/inc/mgmtVgroup.h @@ -20,9 +20,7 @@ extern "C" { #endif -#include -#include -#include "mnode.h" +#include "mgmtDef.h" enum _TSDB_VG_STATUS { TSDB_VG_STATUS_READY, diff --git a/src/mnode/src/mgmtAcct.c b/src/mnode/src/mgmtAcct.c index 4741888e60..792d5fa9b8 100644 --- a/src/mnode/src/mgmtAcct.c +++ b/src/mnode/src/mgmtAcct.c @@ -16,8 +16,11 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taoserror.h" +#include "ttime.h" +#include "tutil.h" #include "dnode.h" -#include "mnode.h" +#include "mgmtDef.h" +#include "mgmtLog.h" #include "mgmtAcct.h" #include "mgmtDb.h" #include "mgmtSdb.h" diff --git a/src/mnode/src/mgmtDClient.c b/src/mnode/src/mgmtDClient.c index 8552519e02..177934ccfd 100644 --- a/src/mnode/src/mgmtDClient.c +++ b/src/mnode/src/mgmtDClient.c @@ -19,12 +19,14 @@ #include "tsched.h" #include "tsystem.h" #include "tutil.h" +#include "tglobalcfg.h" #include "dnode.h" -#include "mnode.h" +#include "tgrant.h" +#include "mgmtDef.h" +#include "mgmtLog.h" #include "mgmtMnode.h" #include "mgmtDb.h" #include "mgmtDnode.h" -#include "tgrant.h" #include "mgmtProfile.h" #include "mgmtShell.h" #include "mgmtTable.h" diff --git a/src/mnode/src/mgmtDServer.c b/src/mnode/src/mgmtDServer.c index e4c5e797b3..4d8163dece 100644 --- a/src/mnode/src/mgmtDServer.c +++ b/src/mnode/src/mgmtDServer.c @@ -20,12 +20,14 @@ #include "tsched.h" #include "tsystem.h" #include "tutil.h" -#include "dnode.h" -#include "mnode.h" +#include "tgrant.h" #include "treplica.h" +#include "tglobalcfg.h" +#include "dnode.h" +#include "mgmtDef.h" +#include "mgmtLog.h" #include "mgmtDb.h" #include "mgmtDServer.h" -#include "tgrant.h" #include "mgmtProfile.h" #include "mgmtShell.h" #include "mgmtTable.h" diff --git a/src/mnode/src/mgmtDb.c b/src/mnode/src/mgmtDb.c index 5b77e6b60e..9bf05ecfc4 100644 --- a/src/mnode/src/mgmtDb.c +++ b/src/mnode/src/mgmtDb.c @@ -17,12 +17,15 @@ #include "os.h" #include "taoserror.h" #include "tutil.h" +#include "tgrant.h" +#include "tglobalcfg.h" +#include "ttime.h" #include "name.h" -#include "mnode.h" +#include "mgmtDef.h" +#include "mgmtLog.h" #include "mgmtAcct.h" #include "mgmtDb.h" #include "mgmtDnode.h" -#include "tgrant.h" #include "mgmtMnode.h" #include "mgmtShell.h" #include "mgmtProfile.h" diff --git a/src/mnode/src/mgmtDnode.c b/src/mnode/src/mgmtDnode.c index 17212806a9..c3ff92ce6c 100644 --- a/src/mnode/src/mgmtDnode.c +++ b/src/mnode/src/mgmtDnode.c @@ -15,10 +15,15 @@ #define _DEFAULT_SOURCE #include "os.h" -#include "tmodule.h" #include "tgrant.h" #include "treplica.h" -#include "mnode.h" +#include "tglobalcfg.h" +#include "ttime.h" +#include "tutil.h" +#include "tsocket.h" +#include "dnode.h" +#include "mgmtDef.h" +#include "mgmtLog.h" #include "mgmtDClient.h" #include "mgmtDServer.h" #include "mgmtDnode.h" @@ -27,7 +32,6 @@ #include "mgmtShell.h" #include "mgmtUser.h" #include "mgmtVgroup.h" -#include "dnodeMClient.h" void *tsDnodeSdb = NULL; int32_t tsDnodeUpdateSize = 0; @@ -329,7 +333,6 @@ void mgmtProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { mTrace("dnode:%d, from offline to online", pDnode->dnodeId); pDnode->status = TAOS_DN_STATUS_READY; replicaNotify(); - mgmtMonitorDnodeModule(); } mgmtReleaseDnode(pDnode); @@ -626,21 +629,27 @@ static int32_t mgmtGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pC SSchema *pSchema = pMeta->schema; + pShow->bytes[cols] = 2; + pSchema[cols].type = TSDB_DATA_TYPE_SMALLINT; + strcpy(pSchema[cols].name, "id"); + pSchema[cols].bytes = htons(pShow->bytes[cols]); + cols++; + pShow->bytes[cols] = 16; pSchema[cols].type = TSDB_DATA_TYPE_BINARY; - strcpy(pSchema[cols].name, "IP"); + strcpy(pSchema[cols].name, "ip"); pSchema[cols].bytes = htons(pShow->bytes[cols]); cols++; - pShow->bytes[cols] = 10; + pShow->bytes[cols] = 8; pSchema[cols].type = TSDB_DATA_TYPE_BINARY; - strcpy(pSchema[cols].name, "module type"); + strcpy(pSchema[cols].name, "module"); pSchema[cols].bytes = htons(pShow->bytes[cols]); cols++; - pShow->bytes[cols] = 10; + pShow->bytes[cols] = 8; pSchema[cols].type = TSDB_DATA_TYPE_BINARY; - strcpy(pSchema[cols].name, "module status"); + strcpy(pSchema[cols].name, "status"); pSchema[cols].bytes = htons(pShow->bytes[cols]); cols++; @@ -652,18 +661,7 @@ static int32_t mgmtGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pC pShow->offset[i] = pShow->offset[i - 1] + pShow->bytes[i - 1]; } - pShow->numOfRows = 0; - SDnodeObj *pDnode = NULL; - while (1) { - pShow->pNode = mgmtGetNextDnode(pShow->pNode, (SDnodeObj **)&pDnode); - if (pDnode == NULL) break; - for (int32_t moduleType = 0; moduleType < TSDB_MOD_MAX; ++moduleType) { - if (mgmtCheckModuleInDnode(pDnode, moduleType)) { - pShow->numOfRows++; - } - } - } - + pShow->numOfRows = mgmtGetDnodesNum() * TSDB_MOD_MAX; pShow->rowSize = pShow->offset[cols - 1] + pShow->bytes[cols - 1]; pShow->pNode = NULL; mgmtReleaseUser(pUser); @@ -672,39 +670,52 @@ static int32_t mgmtGetModuleMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pC } int32_t mgmtRetrieveModules(SShowObj *pShow, char *data, int32_t rows, void *pConn) { - int32_t numOfRows = 0; - SDnodeObj *pDnode = NULL; - char * pWrite; - int32_t cols = 0; - char ipstr[20]; + int32_t numOfRows = 0; + char * pWrite; while (numOfRows < rows) { - mgmtReleaseDnode(pDnode); + SDnodeObj *pDnode = NULL; pShow->pNode = mgmtGetNextDnode(pShow->pNode, (SDnodeObj **)&pDnode); if (pDnode == NULL) break; for (int32_t moduleType = 0; moduleType < TSDB_MOD_MAX; ++moduleType) { - if (!mgmtCheckModuleInDnode(pDnode, moduleType)) { - continue; - } + int32_t cols = 0; - cols = 0; + pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; + *(int16_t *)pWrite = pDnode->dnodeId; + cols++; + char ipstr[20]; tinet_ntoa(ipstr, pDnode->privateIp); pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; strcpy(pWrite, ipstr); cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, tsModule[moduleType].name); + switch (moduleType) { + case TSDB_MOD_MGMT: + strcpy(pWrite, "mgmt"); + break; + case TSDB_MOD_HTTP: + strcpy(pWrite, "http"); + break; + case TSDB_MOD_MONITOR: + strcpy(pWrite, "monitor"); + break; + default: + strcpy(pWrite, "unknown"); + } cols++; pWrite = data + pShow->offset[cols] * rows + pShow->bytes[cols] * numOfRows; - strcpy(pWrite, mgmtGetDnodeStatusStr(pDnode->status)); + bool enable = mgmtCheckModuleInDnode(pDnode, moduleType); + strcpy(pWrite, enable ? "enable" : "disable"); cols++; numOfRows++; } + + mgmtReleaseDnode(pDnode); } pShow->numOfReads += numOfRows; @@ -919,155 +930,3 @@ char* mgmtGetDnodeStatusStr(int32_t dnodeStatus) { default: return "undefined"; } } - - -static void clusterSetModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) { - pDnode->moduleStatus |= (1 << moduleType); - mgmtUpdateDnode(pDnode); - - if (moduleType == TSDB_MOD_MGMT) { - mgmtAddMnode(pDnode->dnodeId); - mPrint("dnode:%d, add it into mnode list", pDnode->dnodeId); - } -} - -static void clusterUnSetModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) { - pDnode->moduleStatus &= ~(1 << moduleType); - mgmtUpdateDnode(pDnode); - - if (moduleType == TSDB_MOD_MGMT) { - mgmtDropMnode(pDnode->dnodeId); - mPrint("dnode:%d, remove it from mnode list", pDnode->dnodeId); - } -} - -static void clusterStopAllModuleInDnode(SDnodeObj *pDnode) { - for (int32_t moduleType = 0; moduleType < TSDB_MOD_MAX; ++moduleType) { - if (!mgmtCheckModuleInDnode(pDnode, moduleType)) { - continue; - } - - mPrint("dnode:%d, stop %s module for its offline or remove", pDnode->dnodeId, tsModule[moduleType].name); - clusterUnSetModuleInDnode(pDnode, moduleType); - } -} - -static void clusterStartModuleInAllDnodes(int32_t moduleType) { - void * pNode = NULL; - SDnodeObj *pDnode = NULL; - - while (1) { - pNode = mgmtGetNextDnode(pNode, &pDnode); - if (pDnode == NULL) break; - - if (!mgmtCheckModuleInDnode(pDnode, moduleType) - && pDnode->status != TAOS_DN_STATUS_OFFLINE - && pDnode->status != TAOS_DN_STATUS_DROPPING) { - mPrint("dnode:%d, add %s module for schedule", pDnode->dnodeId, tsModule[moduleType].name); - clusterSetModuleInDnode(pDnode, moduleType); - } - - mgmtReleaseDnode(pNode); - } -} - -static void clusterStartModuleInOneDnode(int32_t moduleType) { - void * pNode = NULL; - SDnodeObj *pDnode = NULL; - - while (1) { - pNode = mgmtGetNextDnode(pNode, &pDnode); - if (pDnode == NULL) break; - - if (!mgmtCheckModuleInDnode(pDnode, moduleType) - && pDnode->status != TAOS_DN_STATUS_OFFLINE - && pDnode->status != TAOS_DN_STATUS_DROPPING - && !(moduleType == TSDB_MOD_MGMT && pDnode->alternativeRole == TSDB_DNODE_ROLE_VNODE)) { - mPrint("dnode:%d, add %s module for schedule", pDnode->dnodeId, tsModule[moduleType].name); - clusterSetModuleInDnode(pDnode, moduleType); - mgmtReleaseDnode(pNode); - break; - } - - mgmtReleaseDnode(pNode); - } -} - -static void clusterStopModuleInOneDnode(int32_t moduleType) { - void * pNode = NULL; - SDnodeObj *pDnode = NULL; - - while (1) { - pNode = mgmtGetNextDnode(pNode, &pDnode); - if (pDnode == NULL) break; - - if (mgmtCheckModuleInDnode(pDnode, moduleType)) { - mPrint("dnode:%d, stop %s module for schedule", pDnode->dnodeId, tsModule[moduleType].name); - clusterUnSetModuleInDnode(pDnode, moduleType); - mgmtReleaseDnode(pNode); - break; - } - - mgmtReleaseDnode(pNode); - } -} - -void mgmtMonitorDnodeModule() { - void * pNode = NULL; - SDnodeObj *pDnode = NULL; - int32_t onlineDnodes = 0; - - for (int32_t moduleType = 0; moduleType < TSDB_MOD_MGMT+1; ++moduleType) { - tsModule[moduleType].curNum = 0; - } - - // dnode loop - while (1) { - pNode = mgmtGetNextDnode(pNode, &pDnode); - if (pDnode == NULL) break; - - if (pDnode->status == TAOS_DN_STATUS_DROPPING) { - mPrint("dnode:%d, status:%d, remove all modules for removing", pDnode->dnodeId, pDnode->status); - clusterStopAllModuleInDnode(pDnode); - mgmtReleaseDnode(pDnode); - continue; - } - - for (int32_t moduleType = 0; moduleType < TSDB_MOD_MGMT+1; ++moduleType) { - if (mgmtCheckModuleInDnode(pDnode, moduleType)) { - tsModule[moduleType].curNum ++; - } - } - - if (pDnode->status != TAOS_DN_STATUS_OFFLINE) { - onlineDnodes++; - } - - mgmtReleaseDnode(pDnode); - } - - for (int32_t moduleType = 0; moduleType < TSDB_MOD_MGMT+1; ++moduleType) { - if (tsModule[moduleType].num == -1) { - clusterStartModuleInAllDnodes(moduleType); - continue; - } - if (tsModule[moduleType].curNum < tsModule[moduleType].num) { - if (onlineDnodes <= tsModule[moduleType].curNum) { - continue; - } - mTrace("need add %s module, curNum:%d, expectNum:%d", tsModule[moduleType].name, tsModule[moduleType].curNum, - tsModule[moduleType].num); - for (int32_t i = tsModule[moduleType].curNum; i < tsModule[moduleType].num; ++i) { - clusterStartModuleInOneDnode(moduleType); - } - } else if (tsModule[moduleType].curNum > tsModule[moduleType].num) { - mTrace("need drop %s module, curNum:%d, expectNum:%d", tsModule[moduleType].name, tsModule[moduleType].curNum, - tsModule[moduleType].num); - for (int32_t i = tsModule[moduleType].num; i < tsModule[moduleType].curNum; ++i) { - clusterStopModuleInOneDnode(moduleType); - } - } else { - } - } -} - diff --git a/src/mnode/src/mgmtGrant.c b/src/mnode/src/mgmtGrant.c index ed32f97426..449799ef95 100644 --- a/src/mnode/src/mgmtGrant.c +++ b/src/mnode/src/mgmtGrant.c @@ -17,8 +17,8 @@ #ifndef _GRANT #include "os.h" #include "taoserror.h" -#include "tlog.h" #include "tgrant.h" +#include "mgmtLog.h" int32_t grantInit() { return TSDB_CODE_SUCCESS; } void grantCleanUp() {} diff --git a/src/mnode/src/mgmtMain.c b/src/mnode/src/mgmtMain.c index a4ce22f158..371a09c03f 100644 --- a/src/mnode/src/mgmtMain.c +++ b/src/mnode/src/mgmtMain.c @@ -16,13 +16,14 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taosdef.h" -#include "tmodule.h" #include "tsched.h" -#include "mnode.h" -#include "mgmtAcct.h" #include "treplica.h" -#include "mgmtDnode.h" #include "tgrant.h" +#include "ttimer.h" +#include "mgmtDef.h" +#include "mgmtLog.h" +#include "mgmtAcct.h" +#include "mgmtDnode.h" #include "mgmtMnode.h" #include "mgmtDb.h" #include "mgmtDClient.h" @@ -33,41 +34,21 @@ #include "mgmtTable.h" #include "mgmtShell.h" -static int32_t mgmtCheckMgmtRunning(); void *tsMgmtTmr = NULL; - -int32_t mgmtInitSystem() { - if (mgmtInitShell() != 0) { - mError("failed to init shell"); - return -1; - } - - struct stat dirstat; - bool fileExist = (stat(tsMnodeDir, &dirstat) == 0); - bool asMaster = (strcmp(tsMasterIp, tsPrivateIp) == 0); - - if (asMaster || fileExist) { - if (mgmtStartSystem() != 0) { - return -1; - } - } - - return 0; -} +static bool tsMgmtIsRunning = false; int32_t mgmtStartSystem() { - mPrint("starting to initialize TDengine mgmt ..."); + if (tsMgmtIsRunning) { + mPrint("TDengine mgmt module already started..."); + return 0; + } + mPrint("starting to initialize TDengine mgmt ..."); struct stat dirstat; if (stat(tsMnodeDir, &dirstat) < 0) { mkdir(tsMnodeDir, 0755); } - if (mgmtCheckMgmtRunning() != 0) { - mPrint("TDengine mgmt module already started..."); - return 0; - } - tsMgmtTmr = taosTmrInit((tsMaxShellConns) * 3, 200, 3600000, "MND"); if (tsMgmtTmr == NULL) { mError("failed to init timer"); @@ -132,21 +113,30 @@ int32_t mgmtStartSystem() { } grantReset(TSDB_GRANT_ALL, 0); + tsMgmtIsRunning = true; mPrint("TDengine mgmt is initialized successfully"); return 0; } - -void mgmtStopSystem() { - if (mgmtIsMaster()) { - mTrace("it is a master mgmt node, it could not be stopped"); - return; +int32_t mgmtInitSystem() { + if (mgmtInitShell() != 0) { + mError("failed to init shell"); + return -1; } - mgmtCleanUpSystem(); - remove(tsMnodeDir); + struct stat dirstat; + bool fileExist = (stat(tsMnodeDir, &dirstat) == 0); + bool asMaster = (strcmp(tsMasterIp, tsPrivateIp) == 0); + + if (asMaster || fileExist) { + if (mgmtStartSystem() != 0) { + return -1; + } + } + + return 0; } void mgmtCleanUpSystem() { @@ -165,14 +155,18 @@ void mgmtCleanUpSystem() { mgmtCleanUpAccts(); sdbCleanUp(); taosTmrCleanUp(tsMgmtTmr); + tsMgmtIsRunning = false; mPrint("mgmt is cleaned up"); } -static int32_t mgmtCheckMgmtRunning() { - if (tsModuleStatus & (1 << TSDB_MOD_MGMT)) { - return -1; +void mgmtStopSystem() { + if (mgmtIsMaster()) { + mTrace("it is a master mgmt node, it could not be stopped"); + return; } - tsetModuleStatus(TSDB_MOD_MGMT); - return 0; -} \ No newline at end of file + mgmtCleanUpSystem(); + + mPrint("mgmt file is removed"); + remove(tsMnodeDir); +} diff --git a/src/mnode/src/mgmtMnode.c b/src/mnode/src/mgmtMnode.c index eff6e8e01a..ec3030f85e 100644 --- a/src/mnode/src/mgmtMnode.c +++ b/src/mnode/src/mgmtMnode.c @@ -16,11 +16,14 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taoserror.h" -#include "tmodule.h" #include "trpc.h" #include "tsync.h" #include "treplica.h" -#include "mnode.h" +#include "tutil.h" +#include "ttime.h" +#include "tsocket.h" +#include "mgmtDef.h" +#include "mgmtLog.h" #include "mgmtMnode.h" #include "mgmtDnode.h" #include "mgmtSdb.h" @@ -42,7 +45,9 @@ static int32_t mgmtMnodeActionInsert(SSdbOperDesc *pOper) { SMnodeObj *pMnode = pOper->pObj; SDnodeObj *pDnode = mgmtGetDnode(pMnode->mnodeId); if (pDnode == NULL) return TSDB_CODE_DNODE_NOT_EXIST; + pMnode->pDnode = pDnode; + pDnode->isMgmt = true; mgmtReleaseDnode(pDnode); return TSDB_CODE_SUCCESS; @@ -130,7 +135,7 @@ void *mgmtGetMnode(int32_t mnodeId) { return sdbGetRow(tsMnodeSdb, &mnodeId); } -void mgmtReleaseMnode(struct _mnode_obj *pMnode) { +void mgmtReleaseMnode(SMnodeObj *pMnode) { sdbDecRef(tsMnodeSdb, pMnode); } diff --git a/src/mnode/src/mgmtProfile.c b/src/mnode/src/mgmtProfile.c index eb9efc8e5d..1f557681ff 100644 --- a/src/mnode/src/mgmtProfile.c +++ b/src/mnode/src/mgmtProfile.c @@ -16,6 +16,10 @@ #define _DEFAULT_SOURCE #include "os.h" #include "taosmsg.h" +#include "taoserror.h" +#include "tutil.h" +#include "mgmtDef.h" +#include "mgmtLog.h" #include "mgmtAcct.h" #include "mgmtDnode.h" #include "mgmtDb.h" diff --git a/src/mnode/src/mgmtReplica.c b/src/mnode/src/mgmtReplica.c index bb906fa5ec..05a303a69b 100644 --- a/src/mnode/src/mgmtReplica.c +++ b/src/mnode/src/mgmtReplica.c @@ -17,7 +17,8 @@ #include "os.h" #include "trpc.h" #include "treplica.h" -#include "mnode.h" +#include "mgmtDef.h" +#include "mgmtLog.h" #include "mgmtMnode.h" #include "mgmtDnode.h" #include "mgmtVgroup.h" diff --git a/src/mnode/src/mgmtSdb.c b/src/mnode/src/mgmtSdb.c index 7fdac8fd60..013c0236d0 100644 --- a/src/mnode/src/mgmtSdb.c +++ b/src/mnode/src/mgmtSdb.c @@ -23,6 +23,7 @@ #include "twal.h" #include "hashint.h" #include "hashstr.h" +#include "mgmtLog.h" #include "mgmtMnode.h" #include "mgmtSdb.h" diff --git a/src/mnode/src/mgmtShell.c b/src/mnode/src/mgmtShell.c index 8633a6359e..1dec4bd015 100644 --- a/src/mnode/src/mgmtShell.c +++ b/src/mnode/src/mgmtShell.c @@ -20,8 +20,11 @@ #include "tlog.h" #include "trpc.h" #include "tsched.h" +#include "tutil.h" +#include "ttimer.h" #include "dnode.h" -#include "mnode.h" +#include "mgmtDef.h" +#include "mgmtLog.h" #include "mgmtAcct.h" #include "mgmtDb.h" #include "mgmtDnode.h" @@ -47,6 +50,7 @@ static void mgmtProcessHeartBeatMsg(SQueuedMsg *queuedMsg); static void mgmtProcessConnectMsg(SQueuedMsg *queuedMsg); static void mgmtProcessUseMsg(SQueuedMsg *queuedMsg); +extern void *tsMgmtTmr; static void *tsMgmtShellRpc = NULL; static void *tsMgmtTranQhandle = NULL; static void (*tsMgmtProcessShellMsgFp[TSDB_MSG_TYPE_MAX])(SQueuedMsg *) = {0}; diff --git a/src/mnode/src/mgmtTable.c b/src/mnode/src/mgmtTable.c index dff6f97209..3fb4272b7f 100644 --- a/src/mnode/src/mgmtTable.c +++ b/src/mnode/src/mgmtTable.c @@ -15,7 +15,6 @@ #define _DEFAULT_SOURCE #include "os.h" - #include "taosmsg.h" #include "ttime.h" #include "tutil.h" @@ -23,6 +22,9 @@ #include "taosmsg.h" #include "tscompression.h" #include "name.h" +#include "tidpool.h" +#include "mgmtDef.h" +#include "mgmtLog.h" #include "mgmtAcct.h" #include "mgmtDClient.h" #include "mgmtDb.h" diff --git a/src/mnode/src/mgmtUser.c b/src/mnode/src/mgmtUser.c index 3077f0005d..92037ba793 100644 --- a/src/mnode/src/mgmtUser.c +++ b/src/mnode/src/mgmtUser.c @@ -18,6 +18,8 @@ #include "trpc.h" #include "ttime.h" #include "tutil.h" +#include "mgmtDef.h" +#include "mgmtLog.h" #include "mgmtAcct.h" #include "tgrant.h" #include "mgmtMnode.h" diff --git a/src/mnode/src/mgmtVgroup.c b/src/mnode/src/mgmtVgroup.c index 0af2af93ab..9be95f4087 100644 --- a/src/mnode/src/mgmtVgroup.c +++ b/src/mnode/src/mgmtVgroup.c @@ -17,13 +17,18 @@ #include "os.h" #include "taoserror.h" #include "tlog.h" +#include "tutil.h" +#include "tsocket.h" +#include "tidpool.h" #include "tsync.h" +#include "ttime.h" #include "treplica.h" -#include "mgmtDnode.h" -#include "mnode.h" +#include "mgmtDef.h" +#include "mgmtLog.h" #include "mgmtDb.h" #include "mgmtDClient.h" #include "mgmtDServer.h" +#include "mgmtDnode.h" #include "mgmtMnode.h" #include "mgmtProfile.h" #include "mgmtSdb.h" diff --git a/src/rpc/CMakeLists.txt b/src/rpc/CMakeLists.txt index 229b1077f8..141ae3499a 100644 --- a/src/rpc/CMakeLists.txt +++ b/src/rpc/CMakeLists.txt @@ -24,7 +24,7 @@ ELSEIF (TD_WINDOWS_64) ENDIF () ADD_LIBRARY(trpc ${SRC}) -TARGET_LINK_LIBRARIES(trpc tutil lz4) +TARGET_LINK_LIBRARIES(trpc tutil lz4 common) ADD_SUBDIRECTORY(test) diff --git a/src/tsdb/inc/tsdb.h b/src/tsdb/inc/tsdb.h index e385239dba..06e4f456db 100644 --- a/src/tsdb/inc/tsdb.h +++ b/src/tsdb/inc/tsdb.h @@ -79,7 +79,7 @@ typedef struct { // --------- TSDB TABLE configuration typedef struct { - TSDB_TABLE_TYPE type; + ETableType type; STableId tableId; int32_t sversion; int64_t superUid; @@ -88,7 +88,7 @@ typedef struct { SDataRow tagValues; } STableCfg; -int tsdbInitTableCfg(STableCfg *config, TSDB_TABLE_TYPE type, int64_t uid, int32_t tid); +int tsdbInitTableCfg(STableCfg *config, ETableType type, int64_t uid, int32_t tid); int tsdbTableSetSuperUid(STableCfg *config, int64_t uid); int tsdbTableSetSchema(STableCfg *config, STSchema *pSchema, bool dup); int tsdbTableSetTagSchema(STableCfg *config, STSchema *pSchema, bool dup); diff --git a/src/tsdb/src/tsdbMain.c b/src/tsdb/src/tsdbMain.c index 8f99a6359a..f26b44f1fc 100644 --- a/src/tsdb/src/tsdbMain.c +++ b/src/tsdb/src/tsdbMain.c @@ -387,7 +387,7 @@ int32_t tsdbInsertData(tsdb_repo_t *repo, SSubmitMsg *pMsg) { /** * Initialize a table configuration */ -int tsdbInitTableCfg(STableCfg *config, TSDB_TABLE_TYPE type, int64_t uid, int32_t tid) { +int tsdbInitTableCfg(STableCfg *config, ETableType type, int64_t uid, int32_t tid) { if (config == NULL) return -1; if (type != TSDB_NORMAL_TABLE && type != TSDB_CHILD_TABLE) return -1; diff --git a/src/util/inc/tlog.h b/src/util/inc/tlog.h index 8a3bd6bc7c..591f53f410 100644 --- a/src/util/inc/tlog.h +++ b/src/util/inc/tlog.h @@ -177,26 +177,6 @@ extern uint32_t cdebugFlag; tprintf("DND QRY ", qdebugFlag, __VA_ARGS__); \ } -// mnode log function -#define mError(...) \ - if (mdebugFlag & DEBUG_ERROR) { \ - tprintf("ERROR MND ", 255, __VA_ARGS__); \ - } -#define mWarn(...) \ - if (mdebugFlag & DEBUG_WARN) { \ - tprintf("WARN MND ", mdebugFlag, __VA_ARGS__); \ - } -#define mTrace(...) \ - if (mdebugFlag & DEBUG_TRACE) { \ - tprintf("MND ", mdebugFlag, __VA_ARGS__); \ - } -#define mPrint(...) \ - { tprintf("MND ", 255, __VA_ARGS__); } - -#define mLError(...) taosLogError(__VA_ARGS__) mError(__VA_ARGS__) -#define mLWarn(...) taosLogWarn(__VA_ARGS__) mWarn(__VA_ARGS__) -#define mLPrint(...) taosLogPrint(__VA_ARGS__) mPrint(__VA_ARGS__) - #define httpError(...) \ if (httpDebugFlag & DEBUG_ERROR) { \ tprintf("ERROR HTP ", 255, __VA_ARGS__); \ @@ -239,25 +219,6 @@ extern uint32_t cdebugFlag; #define monitorLWarn(...) taosLogWarn(__VA_ARGS__) monitorWarn(__VA_ARGS__) #define monitorLPrint(...) taosLogPrint(__VA_ARGS__) monitorPrint(__VA_ARGS__) -#define sdbError(...) \ - if (sdbDebugFlag & DEBUG_ERROR) { \ - tprintf("ERROR MND-SDB ", 255, __VA_ARGS__); \ - } -#define sdbWarn(...) \ - if (sdbDebugFlag & DEBUG_WARN) { \ - tprintf("WARN MND-SDB ", sdbDebugFlag, __VA_ARGS__); \ - } -#define sdbTrace(...) \ - if (sdbDebugFlag & DEBUG_TRACE) { \ - tprintf("MND-SDB ", sdbDebugFlag, __VA_ARGS__); \ - } -#define sdbPrint(...) \ - { tprintf("MND-SDB ", 255, __VA_ARGS__); } - -#define sdbLError(...) taosLogError(__VA_ARGS__) sdbError(__VA_ARGS__) -#define sdbLWarn(...) taosLogWarn(__VA_ARGS__) sdbWarn(__VA_ARGS__) -#define sdbLPrint(...) taosLogPrint(__VA_ARGS__) sdbPrint(__VA_ARGS__) - #ifdef __cplusplus } #endif diff --git a/src/util/inc/tmodule.h b/src/util/inc/tmodule.h deleted file mode 100644 index f5ad6a6b60..0000000000 --- a/src/util/inc/tmodule.h +++ /dev/null @@ -1,57 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#ifndef TDENGINE_TMODULE_H -#define TDENGINE_TMODULE_H - -#ifdef __cplusplus -extern "C" { -#endif - -#include -#include -#include - -enum _module { - TSDB_MOD_MGMT, - TSDB_MOD_HTTP, - TSDB_MOD_MONITOR, - TSDB_MOD_MAX -}; - -#define tsetModuleStatus(mod) \ - { tsModuleStatus |= (1 << mod); } -#define tclearModuleStatus(mod) \ - { tsModuleStatus &= ~(1 << mod); } - -typedef struct { - char *name; - int (*initFp)(); - void (*cleanUpFp)(); - int (*startFp)(); - void (*stopFp)(); - int num; - int curNum; - int equalVnodeNum; -} SModule; - -extern uint32_t tsModuleStatus; -extern SModule tsModule[]; - -#ifdef __cplusplus -} -#endif - -#endif diff --git a/src/util/src/tmodule.c b/src/util/src/tmodule.c deleted file mode 100644 index 8faff15cfa..0000000000 --- a/src/util/src/tmodule.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - * Copyright (c) 2019 TAOS Data, Inc. - * - * 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 . - */ - -#define _DEFAULT_SOURCE -#include "tmodule.h" - -SModule tsModule[TSDB_MOD_MAX] = {0}; -uint32_t tsModuleStatus = 0; \ No newline at end of file diff --git a/tests/tsim/inc/sim.h b/tests/tsim/inc/sim.h index 9d5227ff1e..01fd0789a9 100644 --- a/tests/tsim/inc/sim.h +++ b/tests/tsim/inc/sim.h @@ -23,7 +23,6 @@ #include "taos.h" #include "tidpool.h" #include "tlog.h" -#include "tmodule.h" #include "tutil.h" #define MAX_MAIN_SCRIPT_NUM 10 From c2067ab4cd24da40e50a1b7056edb15665d98c76 Mon Sep 17 00:00:00 2001 From: slguan Date: Fri, 17 Apr 2020 00:31:33 +0800 Subject: [PATCH 08/14] fix status message error --- CMakeLists.txt | 9 +++++---- src/dnode/src/dnodeMClient.c | 30 +++++++++++++++--------------- src/inc/taosmsg.h | 2 +- src/mnode/src/mgmtDnode.c | 8 +++++++- 4 files changed, 28 insertions(+), 21 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 5a9a42cc36..e53cca2ea8 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,15 +1,16 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) PROJECT(TDengine) -SET(TD_CLUSTER FALSE) +SET(TD_SYNC FALSE) SET(TD_ACCOUNT FALSE) -SET(TD_VPEER FALSE) -SET(TD_MPEER FALSE) +SET(TD_ADMIN FALSE) SET(TD_GRANT FALSE) + SET(TD_COVER FALSE) +SET(TD_MEM_CHECK FALSE) + SET(TD_PAGMODE_LITE FALSE) SET(TD_GODLL FALSE) -SET(TD_MEM_CHECK FALSE) SET(TD_COMMUNITY_DIR ${PROJECT_SOURCE_DIR}) MESSAGE(STATUS "Community directory: " ${TD_COMMUNITY_DIR}) diff --git a/src/dnode/src/dnodeMClient.c b/src/dnode/src/dnodeMClient.c index 460c866ce1..42f947588b 100644 --- a/src/dnode/src/dnodeMClient.c +++ b/src/dnode/src/dnodeMClient.c @@ -142,9 +142,9 @@ static void dnodeProcessStatusRsp(SRpcMsg *pMsg) { } SDMStatusRsp *pStatusRsp = pMsg->pCont; - SDMNodeInfos *mpeers = &pStatusRsp->mpeers; - if (mpeers->nodeNum <= 0) { - dError("status msg is invalid, num of ips is %d", mpeers->nodeNum); + SDMNodeInfos *mnodes = &pStatusRsp->mnodes; + if (mnodes->nodeNum <= 0) { + dError("status msg is invalid, num of ips is %d", mnodes->nodeNum); taosTmrReset(dnodeSendStatusMsg, tsStatusInterval * 1000, NULL, tsDnodeTmr, &tsStatusTimer); return; } @@ -159,23 +159,23 @@ static void dnodeProcessStatusRsp(SRpcMsg *pMsg) { dnodeUpdateDnodeInfo(pState->dnodeId); SRpcIpSet mgmtIpSet = {0}; - mgmtIpSet.inUse = mpeers->inUse; - mgmtIpSet.numOfIps = mpeers->nodeNum; - mgmtIpSet.port = htons(mpeers->nodeInfos[0].nodePort); - for (int32_t i = 0; i < mpeers->nodeNum; i++) { - mgmtIpSet.ip[i] = htonl(mpeers->nodeInfos[i].nodeIp); + mgmtIpSet.inUse = mnodes->inUse; + mgmtIpSet.numOfIps = mnodes->nodeNum; + mgmtIpSet.port = htons(mnodes->nodeInfos[0].nodePort); + for (int32_t i = 0; i < mnodes->nodeNum; i++) { + mgmtIpSet.ip[i] = htonl(mnodes->nodeInfos[i].nodeIp); } if (memcmp(&mgmtIpSet, &tsMnodeIpList, sizeof(SRpcIpSet)) != 0 || tsMnodeInfos.nodeNum == 0) { memcpy(&tsMnodeIpList, &mgmtIpSet, sizeof(SRpcIpSet)); - tsMnodeInfos.inUse = mpeers->inUse; - tsMnodeInfos.nodeNum = mpeers->nodeNum; + tsMnodeInfos.inUse = mnodes->inUse; + tsMnodeInfos.nodeNum = mnodes->nodeNum; dPrint("mnode ip list is changed, numOfIps:%d inUse:%d", tsMnodeInfos.nodeNum, tsMnodeInfos.inUse); - for (int32_t i = 0; i < mpeers->nodeNum; i++) { - tsMnodeInfos.nodeInfos[i].nodeId = htonl(mpeers->nodeInfos[i].nodeId); - tsMnodeInfos.nodeInfos[i].nodeIp = htonl(mpeers->nodeInfos[i].nodeIp); - tsMnodeInfos.nodeInfos[i].nodePort = htons(mpeers->nodeInfos[i].nodePort); - strcpy(tsMnodeInfos.nodeInfos[i].nodeName, mpeers->nodeInfos[i].nodeName); + for (int32_t i = 0; i < mnodes->nodeNum; i++) { + tsMnodeInfos.nodeInfos[i].nodeId = htonl(mnodes->nodeInfos[i].nodeId); + tsMnodeInfos.nodeInfos[i].nodeIp = htonl(mnodes->nodeInfos[i].nodeIp); + tsMnodeInfos.nodeInfos[i].nodePort = htons(mnodes->nodeInfos[i].nodePort); + strcpy(tsMnodeInfos.nodeInfos[i].nodeName, mnodes->nodeInfos[i].nodeName); dPrint("mnode:%d, ip:%s:%u name:%s", tsMnodeInfos.nodeInfos[i].nodeId, taosIpStr(tsMnodeInfos.nodeInfos[i].nodeIp), tsMnodeInfos.nodeInfos[i].nodePort, tsMnodeInfos.nodeInfos[i].nodeName); diff --git a/src/inc/taosmsg.h b/src/inc/taosmsg.h index 3d0c72e531..79d086153a 100644 --- a/src/inc/taosmsg.h +++ b/src/inc/taosmsg.h @@ -608,7 +608,7 @@ typedef struct { } SDMNodeInfos; typedef struct { - SDMNodeInfos mpeers; + SDMNodeInfos mnodes; SDnodeState dnodeState; SVnodeAccess vnodeAccess[]; } SDMStatusRsp; diff --git a/src/mnode/src/mgmtDnode.c b/src/mnode/src/mgmtDnode.c index c3ff92ce6c..89c4796d85 100644 --- a/src/mnode/src/mgmtDnode.c +++ b/src/mnode/src/mgmtDnode.c @@ -63,6 +63,12 @@ static int32_t mgmtDnodeActionInsert(SSdbOperDesc *pOper) { pDnode->status = TAOS_DN_STATUS_OFFLINE; } + pDnode->mnodeShellPort = tsMnodeShellPort; + pDnode->mnodeDnodePort = tsMnodeDnodePort; + pDnode->dnodeShellPort = tsDnodeShellPort; + pDnode->dnodeMnodePort = tsDnodeMnodePort; + pDnode->syncPort = 0; + return TSDB_CODE_SUCCESS; } @@ -344,7 +350,7 @@ void mgmtProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { return; } - mgmtGetMnodeList(&pRsp->mpeers); + mgmtGetMnodeList(&pRsp->mnodes); pRsp->dnodeState.dnodeId = htonl(pDnode->dnodeId); pRsp->dnodeState.moduleStatus = htonl(pDnode->moduleStatus); From 467dcc07aca4372575432faf1ebaf05d85108064 Mon Sep 17 00:00:00 2001 From: localvar Date: Fri, 17 Apr 2020 10:08:56 +0800 Subject: [PATCH 09/14] fix memory leaks in timer --- src/os/darwin/src/tdarwin.c | 5 +++++ src/os/linux/src/tlinux.c | 13 ++++++++----- src/os/windows/src/twintimer.c | 6 +++++- src/util/inc/tutil.h | 1 + src/util/src/ttimer.c | 24 ++++++++++++++++-------- 5 files changed, 35 insertions(+), 14 deletions(-) diff --git a/src/os/darwin/src/tdarwin.c b/src/os/darwin/src/tdarwin.c index 7896592030..a8dfdb1e3f 100644 --- a/src/os/darwin/src/tdarwin.c +++ b/src/os/darwin/src/tdarwin.c @@ -243,6 +243,11 @@ int taosInitTimer(void (*callback)(int), int ms) { return setitimer(ITIMER_REAL, &tv, NULL); } +void taosUninitTimer() { + struct itimerval tv = { 0 }; + return setitimer(ITIMER_REAL, &tv, NULL); +} + void taosGetSystemTimezone() { // get and set default timezone SGlobalConfig *cfg_timezone = tsGetConfigOption("timezone"); diff --git a/src/os/linux/src/tlinux.c b/src/os/linux/src/tlinux.c index c1bd0ceb5f..88d69816ab 100644 --- a/src/os/linux/src/tlinux.c +++ b/src/os/linux/src/tlinux.c @@ -286,20 +286,23 @@ void *taosProcessAlarmSignal(void *tharg) { return NULL; } +static pthread_t timerThread; + int taosInitTimer(void (*callback)(int), int ms) { - pthread_t thread; pthread_attr_t tattr; pthread_attr_init(&tattr); - pthread_attr_setdetachstate(&tattr, PTHREAD_CREATE_DETACHED); - int code = pthread_create(&thread, &tattr, taosProcessAlarmSignal, callback); - pthread_detach(thread); + int code = pthread_create(&timerThread, &tattr, taosProcessAlarmSignal, callback); pthread_attr_destroy(&tattr); if (code != 0) { tmrError("failed to create timer thread"); return -1; } + return 0; +} - return thread; +void taosUninitTimer() { + pthread_cancel(timerThread); + pthread_join(timerThread, NULL); } ssize_t tread(int fd, void *buf, size_t count) { diff --git a/src/os/windows/src/twintimer.c b/src/os/windows/src/twintimer.c index 68899bea51..2bb8478f09 100644 --- a/src/os/windows/src/twintimer.c +++ b/src/os/windows/src/twintimer.c @@ -30,8 +30,8 @@ void WINAPI taosWinOnTimer(UINT wTimerID, UINT msg, DWORD_PTR dwUser, DWORD_PTR } } +static MMRESULT timerId; int taosInitTimer(win_timer_f callback, int ms) { - MMRESULT timerId; DWORD_PTR param = *((int64_t *) & callback); timerId = timeSetEvent(ms, 1, (LPTIMECALLBACK)taosWinOnTimer, param, TIME_PERIODIC); @@ -41,6 +41,10 @@ int taosInitTimer(win_timer_f callback, int ms) { return 0; } +void taosUninitTimer() { + timeKillEvent(timerId); +} + void taosMsleep(int mseconds) { Sleep(mseconds); } diff --git a/src/util/inc/tutil.h b/src/util/inc/tutil.h index 81cf177e73..60b2868ccf 100644 --- a/src/util/inc/tutil.h +++ b/src/util/inc/tutil.h @@ -139,6 +139,7 @@ int32_t taosFileRename(char *fullPath, char *suffix, char delimiter, char **dstP void getTmpfilePath(const char *fileNamePattern, char *dstPath); int32_t taosInitTimer(void (*callback)(int), int32_t ms); +void taosUninitTimer(); bool taosMbsToUcs4(char *mbs, int32_t mbs_len, char *ucs4, int32_t ucs4_max_len); diff --git a/src/util/src/ttimer.c b/src/util/src/ttimer.c index 04cad75655..25862fbec3 100644 --- a/src/util/src/ttimer.c +++ b/src/util/src/ttimer.c @@ -84,8 +84,6 @@ static tmr_ctrl_t* tmrCtrls; static tmr_ctrl_t* unusedTmrCtrl = NULL; static void* tmrQhandle; static int numOfTmrCtrl = 0; -//static void* tmrContext = NULL; -static int athread = 0; int taosTmrThreads = 1; @@ -519,7 +517,7 @@ static void taosTmrModuleInit(void) { } tmrQhandle = taosInitScheduler(10000, taosTmrThreads, "tmr"); - athread = taosInitTimer(taosTimerLoopFunc, MSECONDS_PER_TICK); + taosInitTimer(taosTimerLoopFunc, MSECONDS_PER_TICK); tmrTrace("timer module is initialized, number of threads: %d", taosTmrThreads); } @@ -562,19 +560,29 @@ void taosTmrCleanUp(void* handle) { pthread_mutex_unlock(&tmrCtrlMutex); if (numOfTmrCtrl <=0) { -// pthread_cancel(athread); + taosUninitTimer(); + taosCleanUpScheduler(tmrQhandle); + for (int i = 0; i < tListLen(wheels); i++) { time_wheel_t* wheel = wheels + i; pthread_mutex_destroy(&wheel->mutex); free(wheel->slots); } - pthread_mutex_destroy(&tmrCtrlMutex); - free(timerMap.slots); + pthread_mutex_destroy(&tmrCtrlMutex); + + for (size_t i = 0; i < timerMap.size; i++) { + timer_list_t* list = timerMap.slots + i; + tmr_obj_t* t = list->timers; + while (t != NULL) { + tmr_obj_t* next = t->mnext; + free(t); + t = next; + } + } + free(timerMap.slots); free(tmrCtrls); - taosCleanUpScheduler(tmrQhandle); - tmrModuleInit = PTHREAD_ONCE_INIT; tmrTrace("timer module is cleaned up"); } From 0732786632e4451f69c4a03d9ac808f9d547ccd5 Mon Sep 17 00:00:00 2001 From: slguan Date: Fri, 17 Apr 2020 12:18:28 +0800 Subject: [PATCH 10/14] [TD-52] refactor interface --- cmake/define.inc | 2 +- cmake/input.inc | 8 ----- src/dnode/inc/dnodeMClient.h | 2 +- src/dnode/src/dnodeMClient.c | 2 +- src/inc/mnode.h | 3 -- src/inc/tadmin.h | 11 ++----- src/mnode/src/mgmtAcct.c | 6 ++-- src/mnode/src/mgmtMnode.c | 2 +- src/mnode/src/mgmtReplica.c | 3 ++ src/plugins/http/CMakeLists.txt | 4 +-- src/plugins/http/inc/httpHandle.h | 29 +++++++++++++++-- src/plugins/http/src/httpSystem.c | 27 +++++----------- src/plugins/monitor/src/monitorSystem.c | 24 ++++++++++++-- src/util/CMakeLists.txt | 4 --- src/util/inc/tlog.h | 42 ------------------------- src/vnode/src/vnodeMain.c | 6 ---- 16 files changed, 70 insertions(+), 105 deletions(-) diff --git a/cmake/define.inc b/cmake/define.inc index 9f56dc654d..da100f4260 100755 --- a/cmake/define.inc +++ b/cmake/define.inc @@ -6,7 +6,7 @@ IF (TD_SYNC) ENDIF () IF (TD_ACCOUNT) - ADD_DEFINITIONS(-D_ACCOUNT) + ADD_DEFINITIONS(-D_ACCT) ENDIF () IF (TD_ADMIN) diff --git a/cmake/input.inc b/cmake/input.inc index d29b7c5ff8..5a17e0319c 100755 --- a/cmake/input.inc +++ b/cmake/input.inc @@ -1,14 +1,6 @@ CMAKE_MINIMUM_REQUIRED(VERSION 2.8) PROJECT(TDengine) -IF (${CLUSTER} MATCHES "true") - SET(TD_CLUSTER TRUE) - MESSAGE(STATUS "Build with cluster plugins") -ELSEIF (${CLUSTER} MATCHES "false") - SET(TD_CLUSTER FALSE) - MESSAGE(STATUS "Build without cluster plugins") -ENDIF () - IF (${ACCOUNT} MATCHES "true") SET(TD_ACCOUNT TRUE) MESSAGE(STATUS "Build with account plugins") diff --git a/src/dnode/inc/dnodeMClient.h b/src/dnode/inc/dnodeMClient.h index a8e97dd9de..fdaf54e0e5 100644 --- a/src/dnode/inc/dnodeMClient.h +++ b/src/dnode/inc/dnodeMClient.h @@ -23,7 +23,7 @@ extern "C" { int32_t dnodeInitMClient(); void dnodeCleanupMClient(); void dnodeSendMsgToMnode(SRpcMsg *rpcMsg); -void * dnodeGetMpeerInfos(); +void * dnodeGetMnodeList(); int32_t dnodeGetDnodeId(); #ifdef __cplusplus diff --git a/src/dnode/src/dnodeMClient.c b/src/dnode/src/dnodeMClient.c index 42f947588b..90a093560f 100644 --- a/src/dnode/src/dnodeMClient.c +++ b/src/dnode/src/dnodeMClient.c @@ -332,7 +332,7 @@ uint32_t dnodeGetMnodeMasteIp() { return tsMnodeIpList.ip[tsMnodeIpList.inUse]; } -void* dnodeGetMpeerInfos() { +void* dnodeGetMnodeList() { return &tsMnodeInfos; } diff --git a/src/inc/mnode.h b/src/inc/mnode.h index c30e1e37ba..e7ad88d6b6 100644 --- a/src/inc/mnode.h +++ b/src/inc/mnode.h @@ -25,9 +25,6 @@ int32_t mgmtStartSystem(); void mgmtCleanUpSystem(); void mgmtStopSystem(); -extern char version[]; -extern char tsMnodeDir[]; - #ifdef __cplusplus } #endif diff --git a/src/inc/tadmin.h b/src/inc/tadmin.h index 4a883965f4..b7de33576a 100644 --- a/src/inc/tadmin.h +++ b/src/inc/tadmin.h @@ -20,15 +20,10 @@ extern "C" { #endif -#include -#include +struct HttpServer; -void adminInit(); - -struct _http_server_obj_; - -extern void (*adminInitHandleFp)(struct _http_server_obj_* pServer); -extern void (*opInitHandleFp)(struct _http_server_obj_* pServer); +void adminInitHandle(struct HttpServer* pServer); +void opInitHandle(struct HttpServer* pServer); #ifdef __cplusplus } diff --git a/src/mnode/src/mgmtAcct.c b/src/mnode/src/mgmtAcct.c index 792d5fa9b8..3a52715274 100644 --- a/src/mnode/src/mgmtAcct.c +++ b/src/mnode/src/mgmtAcct.c @@ -26,9 +26,9 @@ #include "mgmtSdb.h" #include "mgmtUser.h" -static void * tsAcctSdb = NULL; -static int32_t tsAcctUpdateSize; -static void mgmtCreateRootAcct(); +void * tsAcctSdb = NULL; +int32_t tsAcctUpdateSize; +static void mgmtCreateRootAcct(); static int32_t mgmtActionAcctDestroy(SSdbOperDesc *pOper) { SAcctObj *pAcct = pOper->pObj; diff --git a/src/mnode/src/mgmtMnode.c b/src/mnode/src/mgmtMnode.c index ec3030f85e..491fcb5302 100644 --- a/src/mnode/src/mgmtMnode.c +++ b/src/mnode/src/mgmtMnode.c @@ -30,9 +30,9 @@ #include "mgmtShell.h" #include "mgmtUser.h" +int32_t tsMnodeIsMaster = true; static void * tsMnodeSdb = NULL; static int32_t tsMnodeUpdateSize = 0; -static int32_t tsMnodeIsMaster = true; static int32_t mgmtGetMnodeMeta(STableMetaMsg *pMeta, SShowObj *pShow, void *pConn); static int32_t mgmtRetrieveMnodes(SShowObj *pShow, char *data, int32_t rows, void *pConn); diff --git a/src/mnode/src/mgmtReplica.c b/src/mnode/src/mgmtReplica.c index 05a303a69b..4ce9a79572 100644 --- a/src/mnode/src/mgmtReplica.c +++ b/src/mnode/src/mgmtReplica.c @@ -64,4 +64,7 @@ int32_t replicaAllocVnodes(SVgObj *pVgroup) { return TSDB_CODE_SUCCESS; } +tsync_h syncStart(const SSyncInfo *info) { return NULL; } +int syncForwardToPeer(tsync_h shandle, void *pHead, void *mhandle) { return 0; } + #endif diff --git a/src/plugins/http/CMakeLists.txt b/src/plugins/http/CMakeLists.txt index 9b36f4029a..40dee94117 100644 --- a/src/plugins/http/CMakeLists.txt +++ b/src/plugins/http/CMakeLists.txt @@ -15,7 +15,7 @@ IF ((TD_LINUX_64) OR (TD_LINUX_32 AND TD_ARM)) ADD_LIBRARY(http ${SRC}) TARGET_LINK_LIBRARIES(http taos_static z) - IF (TD_CLUSTER) - TARGET_LINK_LIBRARIES(http) + IF (TD_ADMIN) + TARGET_LINK_LIBRARIES(http admin) ENDIF () ENDIF () diff --git a/src/plugins/http/inc/httpHandle.h b/src/plugins/http/inc/httpHandle.h index 9c6a263c5b..ad5a28e10e 100644 --- a/src/plugins/http/inc/httpHandle.h +++ b/src/plugins/http/inc/httpHandle.h @@ -23,7 +23,7 @@ #include "taosdef.h" #include "tutil.h" #include "zlib.h" - +#include "tlog.h" #include "http.h" #include "httpJson.h" @@ -206,10 +206,10 @@ typedef struct HttpThread { int threadId; char label[HTTP_LABEL_SIZE]; bool (*processData)(HttpContext *pContext); - struct _http_server_obj_ *pServer; // handle passed by upper layer during pServer initialization + struct HttpServer *pServer; // handle passed by upper layer during pServer initialization } HttpThread; -typedef struct _http_server_obj_ { +typedef struct HttpServer { char label[HTTP_LABEL_SIZE]; char serverIp[16]; uint16_t serverPort; @@ -313,4 +313,27 @@ const char* httpContextStateStr(HttpContextState state); bool httpAlterContextState(HttpContext *pContext, HttpContextState srcState, HttpContextState destState); void httpRemoveContextFromEpoll(HttpThread *pThread, HttpContext *pContext); +#define httpError(...) \ + if (httpDebugFlag & DEBUG_ERROR) { \ + tprintf("ERROR HTP ", 255, __VA_ARGS__); \ + } +#define httpWarn(...) \ + if (httpDebugFlag & DEBUG_WARN) { \ + tprintf("WARN HTP ", httpDebugFlag, __VA_ARGS__); \ + } +#define httpTrace(...) \ + if (httpDebugFlag & DEBUG_TRACE) { \ + tprintf("HTP ", httpDebugFlag, __VA_ARGS__); \ + } +#define httpDump(...) \ + if (httpDebugFlag & DEBUG_TRACE) { \ + taosPrintLongString("HTP ", httpDebugFlag, __VA_ARGS__); \ + } +#define httpPrint(...) \ + { tprintf("HTP ", 255, __VA_ARGS__); } + +#define httpLError(...) taosLogError(__VA_ARGS__) httpError(__VA_ARGS__) +#define httpLWarn(...) taosLogWarn(__VA_ARGS__) httpWarn(__VA_ARGS__) +#define httpLPrint(...) taosLogPrint(__VA_ARGS__) httpPrint(__VA_ARGS__) + #endif diff --git a/src/plugins/http/src/httpSystem.c b/src/plugins/http/src/httpSystem.c index 2a118cc2b1..52910cb1db 100644 --- a/src/plugins/http/src/httpSystem.c +++ b/src/plugins/http/src/httpSystem.c @@ -13,11 +13,9 @@ * along with this program. If not, see . */ -#include -#include -#include -#include - +#define _DEFAULT_SOURCE +#include "os.h" +#include "tadmin.h" #include "http.h" #include "httpCode.h" #include "httpHandle.h" @@ -27,28 +25,17 @@ #include "tglobalcfg.h" #include "tsocket.h" #include "ttimer.h" - #include "gcHandle.h" #include "httpHandle.h" #include "restHandle.h" #include "tgHandle.h" -#include "tlog.h" +#ifndef _ADMIN -void (*adminInitHandleFp)(HttpServer* pServer) = NULL; -void (*opInitHandleFp)(HttpServer* pServer) = NULL; +void adminInitHandle(HttpServer* pServer) {} +void opInitHandle(HttpServer* pServer) {} -void adminInitHandle(HttpServer* pServer) { - if (adminInitHandleFp) { - (*adminInitHandleFp)(pServer); - } -} - -void opInitHandle(HttpServer* pServer) { - if (opInitHandleFp) { - (*opInitHandleFp)(pServer); - } -} +#endif static HttpServer *httpServer = NULL; void taosInitNote(int numOfNoteLines, int maxNotes, char* lable); diff --git a/src/plugins/monitor/src/monitorSystem.c b/src/plugins/monitor/src/monitorSystem.c index 4fb3ba2f5b..a547d13545 100644 --- a/src/plugins/monitor/src/monitorSystem.c +++ b/src/plugins/monitor/src/monitorSystem.c @@ -13,17 +13,37 @@ * along with this program. If not, see . */ +#define _DEFAULT_SOURCE #include "os.h" - +#include "tlog.h" #include "monitor.h" #include "dnode.h" -#include "monitorSystem.h" #include "tsclient.h" #include "taosdef.h" #include "tsystem.h" #include "ttime.h" #include "ttimer.h" #include "tutil.h" +#include "monitorSystem.h" + +#define monitorError(...) \ + if (monitorDebugFlag & DEBUG_ERROR) { \ + tprintf("ERROR MON ", 255, __VA_ARGS__); \ + } +#define monitorWarn(...) \ + if (monitorDebugFlag & DEBUG_WARN) { \ + tprintf("WARN MON ", monitorDebugFlag, __VA_ARGS__); \ + } +#define monitorTrace(...) \ + if (monitorDebugFlag & DEBUG_TRACE) { \ + tprintf("MON ", monitorDebugFlag, __VA_ARGS__); \ + } +#define monitorPrint(...) \ + { tprintf("MON ", 255, __VA_ARGS__); } + +#define monitorLError(...) taosLogError(__VA_ARGS__) monitorError(__VA_ARGS__) +#define monitorLWarn(...) taosLogWarn(__VA_ARGS__) monitorWarn(__VA_ARGS__) +#define monitorLPrint(...) taosLogPrint(__VA_ARGS__) monitorPrint(__VA_ARGS__) #define SQL_LENGTH 1024 #define LOG_LEN_STR 80 diff --git a/src/util/CMakeLists.txt b/src/util/CMakeLists.txt index a814bf52ab..f875cfff94 100644 --- a/src/util/CMakeLists.txt +++ b/src/util/CMakeLists.txt @@ -107,8 +107,4 @@ ELSEIF(TD_DARWIN_64) TARGET_LINK_LIBRARIES(tutil iconv pthread os) ENDIF() -#IF (TD_CLUSTER) # TARGET_LINK_LIBRARIES(tutil mstorage) -#ENDIF () - - diff --git a/src/util/inc/tlog.h b/src/util/inc/tlog.h index 591f53f410..485f8644d3 100644 --- a/src/util/inc/tlog.h +++ b/src/util/inc/tlog.h @@ -177,48 +177,6 @@ extern uint32_t cdebugFlag; tprintf("DND QRY ", qdebugFlag, __VA_ARGS__); \ } -#define httpError(...) \ - if (httpDebugFlag & DEBUG_ERROR) { \ - tprintf("ERROR HTP ", 255, __VA_ARGS__); \ - } -#define httpWarn(...) \ - if (httpDebugFlag & DEBUG_WARN) { \ - tprintf("WARN HTP ", httpDebugFlag, __VA_ARGS__); \ - } -#define httpTrace(...) \ - if (httpDebugFlag & DEBUG_TRACE) { \ - tprintf("HTP ", httpDebugFlag, __VA_ARGS__); \ - } -#define httpDump(...) \ - if (httpDebugFlag & DEBUG_TRACE) { \ - taosPrintLongString("HTP ", httpDebugFlag, __VA_ARGS__); \ - } -#define httpPrint(...) \ - { tprintf("HTP ", 255, __VA_ARGS__); } - -#define httpLError(...) taosLogError(__VA_ARGS__) httpError(__VA_ARGS__) -#define httpLWarn(...) taosLogWarn(__VA_ARGS__) httpWarn(__VA_ARGS__) -#define httpLPrint(...) taosLogPrint(__VA_ARGS__) httpPrint(__VA_ARGS__) - -#define monitorError(...) \ - if (monitorDebugFlag & DEBUG_ERROR) { \ - tprintf("ERROR MON ", 255, __VA_ARGS__); \ - } -#define monitorWarn(...) \ - if (monitorDebugFlag & DEBUG_WARN) { \ - tprintf("WARN MON ", monitorDebugFlag, __VA_ARGS__); \ - } -#define monitorTrace(...) \ - if (monitorDebugFlag & DEBUG_TRACE) { \ - tprintf("MON ", monitorDebugFlag, __VA_ARGS__); \ - } -#define monitorPrint(...) \ - { tprintf("MON ", 255, __VA_ARGS__); } - -#define monitorLError(...) taosLogError(__VA_ARGS__) monitorError(__VA_ARGS__) -#define monitorLWarn(...) taosLogWarn(__VA_ARGS__) monitorWarn(__VA_ARGS__) -#define monitorLPrint(...) taosLogPrint(__VA_ARGS__) monitorPrint(__VA_ARGS__) - #ifdef __cplusplus } #endif diff --git a/src/vnode/src/vnodeMain.c b/src/vnode/src/vnodeMain.c index 7ec9b0fef7..a7c53d1258 100644 --- a/src/vnode/src/vnodeMain.c +++ b/src/vnode/src/vnodeMain.c @@ -41,12 +41,6 @@ static int vnodeGetWalInfo(void *ahandle, char *name, uint32_t *index); static void vnodeNotifyRole(void *ahandle, int8_t role); static pthread_once_t vnodeModuleInit = PTHREAD_ONCE_INIT; - -#ifndef _VPEER -tsync_h syncStart(const SSyncInfo *info) { return NULL; } -int syncForwardToPeer(tsync_h shandle, void *pHead, void *mhandle) { return 0; } -#endif - static void vnodeInit() { vnodeInitWriteFp(); vnodeInitReadFp(); From 84fc0e3a968adba6b34e0b2ed5883e7741d41b4a Mon Sep 17 00:00:00 2001 From: slguan Date: Fri, 17 Apr 2020 12:21:16 +0800 Subject: [PATCH 11/14] fix compile error --- src/mnode/src/mgmtReplica.c | 3 --- src/vnode/src/vnodeMain.c | 6 ++++++ 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/mnode/src/mgmtReplica.c b/src/mnode/src/mgmtReplica.c index 4ce9a79572..05a303a69b 100644 --- a/src/mnode/src/mgmtReplica.c +++ b/src/mnode/src/mgmtReplica.c @@ -64,7 +64,4 @@ int32_t replicaAllocVnodes(SVgObj *pVgroup) { return TSDB_CODE_SUCCESS; } -tsync_h syncStart(const SSyncInfo *info) { return NULL; } -int syncForwardToPeer(tsync_h shandle, void *pHead, void *mhandle) { return 0; } - #endif diff --git a/src/vnode/src/vnodeMain.c b/src/vnode/src/vnodeMain.c index a7c53d1258..0827d90ebc 100644 --- a/src/vnode/src/vnodeMain.c +++ b/src/vnode/src/vnodeMain.c @@ -41,6 +41,12 @@ static int vnodeGetWalInfo(void *ahandle, char *name, uint32_t *index); static void vnodeNotifyRole(void *ahandle, int8_t role); static pthread_once_t vnodeModuleInit = PTHREAD_ONCE_INIT; + +#ifndef _SYNC +tsync_h syncStart(const SSyncInfo *info) { return NULL; } +int syncForwardToPeer(tsync_h shandle, void *pHead, void *mhandle) { return 0; } +#endif + static void vnodeInit() { vnodeInitWriteFp(); vnodeInitReadFp(); From ecc95e70543753e01d612bfe5641f6f8a2397f68 Mon Sep 17 00:00:00 2001 From: slguan Date: Fri, 17 Apr 2020 13:10:54 +0800 Subject: [PATCH 12/14] set master when there is only one node --- src/mnode/src/mgmtMnode.c | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/mnode/src/mgmtMnode.c b/src/mnode/src/mgmtMnode.c index 491fcb5302..e469dc4f6f 100644 --- a/src/mnode/src/mgmtMnode.c +++ b/src/mnode/src/mgmtMnode.c @@ -87,6 +87,14 @@ static int32_t mgmtMnodeActionDecode(SSdbOperDesc *pOper) { } static int32_t mgmtMnodeActionRestored() { + if (mgmtGetMnodesNum() == 1) { + SMnodeObj *pMnode = NULL; + mgmtGetNextMnode(NULL, &pMnode); + if (pMnode != NULL) { + pMnode->role = TAOS_SYNC_ROLE_MASTER; + mgmtReleaseMnode(pMnode); + } + } return TSDB_CODE_SUCCESS; } From f3b76d1acfd78cc4a1ae85c8c6a51cfa2afd929c Mon Sep 17 00:00:00 2001 From: slguan Date: Fri, 17 Apr 2020 16:36:31 +0800 Subject: [PATCH 13/14] old wal of sdb is deleted after recovery --- src/wal/src/walMain.c | 43 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 42 insertions(+), 1 deletion(-) diff --git a/src/wal/src/walMain.c b/src/wal/src/walMain.c index edca4e371c..9a9c658f1b 100644 --- a/src/wal/src/walMain.c +++ b/src/wal/src/walMain.c @@ -52,6 +52,7 @@ static uint32_t walSignature = 0xFAFBFDFE; static int walHandleExistingFiles(const char *path); static int walRestoreWalFile(const char *name, void *pVnode, FWalWrite writeFp); static int walRemoveWalFiles(const char *path); +static int walMoveOldWalFilesBack(const char *path); void *walOpen(const char *path, const SWalCfg *pCfg) { SWal *pWal = calloc(sizeof(SWal), 1); @@ -213,7 +214,11 @@ int walRestore(void *handle, void *pVnode, int (*writeFp)(void *, void *, int)) } if (code == 0) { - code = walRemoveWalFiles(opath); + if (pWal->keep) { + code = walMoveOldWalFilesBack(pWal->path); + } else { + code = walRemoveWalFiles(opath); + } if (code == 0) { if (remove(opath) < 0) { wError("wal:%s, failed to remove directory(%s)", opath, strerror(errno)); @@ -365,4 +370,40 @@ static int walRemoveWalFiles(const char *path) { return code; } +int walMoveOldWalFilesBack(const char *path) { + char oname[TSDB_FILENAME_LEN * 3]; + char nname[TSDB_FILENAME_LEN * 3]; + char opath[TSDB_FILENAME_LEN]; + struct dirent *ent; + int plen = strlen(walPrefix); + int code = 0; + + sprintf(opath, "%s/old", path); + + if (access(opath, F_OK) == 0) { + // move all old files to wal directory + int count = 0; + + DIR *dir = opendir(opath); + while ((ent = readdir(dir))!= NULL) { + if ( strncmp(ent->d_name, walPrefix, plen) == 0) { + sprintf(oname, "%s/%s", opath, ent->d_name); + sprintf(nname, "%s/%s", path, ent->d_name); + if (rename(oname, nname) < 0) { + wError("wal:%s, failed to move to new:%s", oname, nname); + code = -1; + break; + } + + count++; + } + } + + wTrace("wal:%s, %d old files are move back for keep option is set", path, count); + closedir(dir); + } + + return code; +} + From a1c8ec36bbd651b89b82c7cf426641dc00052e9e Mon Sep 17 00:00:00 2001 From: slguan Date: Fri, 17 Apr 2020 21:04:43 +0800 Subject: [PATCH 14/14] [TD-52] fix bug in sdb sync --- src/dnode/inc/dnodeMClient.h | 2 -- src/dnode/src/dnodeMgmt.c | 2 +- src/dnode/src/dnodeModule.c | 2 +- src/inc/dnode.h | 2 ++ src/inc/taosmsg.h | 1 + src/mnode/inc/mgmtDnode.h | 1 - src/mnode/src/mgmtDnode.c | 12 +++++------- src/mnode/src/mgmtMain.c | 9 +++++---- src/mnode/src/mgmtMnode.c | 6 ++++++ src/mnode/src/mgmtSdb.c | 12 +++++++----- src/mnode/src/mgmtUser.c | 3 ++- src/util/src/tglobalcfg.c | 7 +------ 12 files changed, 31 insertions(+), 28 deletions(-) diff --git a/src/dnode/inc/dnodeMClient.h b/src/dnode/inc/dnodeMClient.h index fdaf54e0e5..6d413ada88 100644 --- a/src/dnode/inc/dnodeMClient.h +++ b/src/dnode/inc/dnodeMClient.h @@ -23,8 +23,6 @@ extern "C" { int32_t dnodeInitMClient(); void dnodeCleanupMClient(); void dnodeSendMsgToMnode(SRpcMsg *rpcMsg); -void * dnodeGetMnodeList(); -int32_t dnodeGetDnodeId(); #ifdef __cplusplus } diff --git a/src/dnode/src/dnodeMgmt.c b/src/dnode/src/dnodeMgmt.c index abfee2239b..8f62e3adc0 100644 --- a/src/dnode/src/dnodeMgmt.c +++ b/src/dnode/src/dnodeMgmt.c @@ -22,11 +22,11 @@ #include "trpc.h" #include "tsdb.h" #include "twal.h" +#include "vnode.h" #include "dnodeMClient.h" #include "dnodeMgmt.h" #include "dnodeRead.h" #include "dnodeWrite.h" -#include "vnode.h" static int32_t dnodeOpenVnodes(); static void dnodeCloseVnodes(); diff --git a/src/dnode/src/dnodeModule.c b/src/dnode/src/dnodeModule.c index c50391613e..57ef655078 100644 --- a/src/dnode/src/dnodeModule.c +++ b/src/dnode/src/dnodeModule.c @@ -118,7 +118,7 @@ void dnodeProcessModuleStatus(uint32_t moduleStatus) { dPrint("module status is received, start mgmt module", tsModuleStatus, moduleStatus); tsModule[TSDB_MOD_MGMT].enable = true; dnodeSetModuleStatus(TSDB_MOD_MGMT); - (*tsModule[TSDB_MOD_MGMT].stopFp)(); + (*tsModule[TSDB_MOD_MGMT].startFp)(); } if (tsModule[TSDB_MOD_MGMT].enable && !enableMgmtModule) { diff --git a/src/inc/dnode.h b/src/inc/dnode.h index 6215b5a7ee..0be18a007e 100644 --- a/src/inc/dnode.h +++ b/src/inc/dnode.h @@ -43,6 +43,8 @@ void dnodeSendRpcWriteRsp(void *pVnode, void *param, int32_t code); bool dnodeIsFirstDeploy(); uint32_t dnodeGetMnodeMasteIp(); +void * dnodeGetMnodeList(); +int32_t dnodeGetDnodeId(); #ifdef __cplusplus } diff --git a/src/inc/taosmsg.h b/src/inc/taosmsg.h index 79d086153a..f292345e68 100644 --- a/src/inc/taosmsg.h +++ b/src/inc/taosmsg.h @@ -584,6 +584,7 @@ typedef struct { char dnodeName[TSDB_NODE_NAME_LEN + 1]; uint32_t privateIp; uint32_t publicIp; + uint32_t moduleStatus; uint32_t lastReboot; // time stamp for last reboot uint16_t numOfTotalVnodes; // from config file uint16_t openVnodes; diff --git a/src/mnode/inc/mgmtDnode.h b/src/mnode/inc/mgmtDnode.h index 14bb6e04e5..48111d3110 100644 --- a/src/mnode/inc/mgmtDnode.h +++ b/src/mnode/inc/mgmtDnode.h @@ -31,7 +31,6 @@ int32_t mgmtInitDnodes(); void mgmtCleanupDnodes(); char* mgmtGetDnodeStatusStr(int32_t dnodeStatus); -bool mgmtCheckModuleInDnode(SDnodeObj *pDnode, int moduleType); void mgmtMonitorDnodeModule(); int32_t mgmtGetDnodesNum(); diff --git a/src/mnode/src/mgmtDnode.c b/src/mnode/src/mgmtDnode.c index 89c4796d85..3c66ff6c57 100644 --- a/src/mnode/src/mgmtDnode.c +++ b/src/mnode/src/mgmtDnode.c @@ -129,7 +129,7 @@ static int32_t mgmtDnodeActionDecode(SSdbOperDesc *pOper) { static int32_t mgmtDnodeActionRestored() { int32_t numOfRows = sdbGetNumOfRows(tsDnodeSdb); - if (numOfRows <= 0 && strcmp(tsMasterIp, tsPrivateIp) == 0) { + if (numOfRows <= 0 && dnodeIsFirstDeploy()) { uint32_t ip = inet_addr(tsPrivateIp); mgmtCreateDnode(ip); SDnodeObj *pDnode = mgmtGetDnodeByIp(ip); @@ -276,6 +276,7 @@ void mgmtProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { pStatus->dnodeId = htonl(pStatus->dnodeId); pStatus->privateIp = htonl(pStatus->privateIp); pStatus->publicIp = htonl(pStatus->publicIp); + pStatus->moduleStatus = htonl(pStatus->moduleStatus); pStatus->lastReboot = htonl(pStatus->lastReboot); pStatus->numOfCores = htons(pStatus->numOfCores); pStatus->numOfTotalVnodes = htons(pStatus->numOfTotalVnodes); @@ -311,6 +312,7 @@ void mgmtProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { pDnode->diskAvailable = pStatus->diskAvailable; pDnode->alternativeRole = pStatus->alternativeRole; pDnode->totalVnodes = pStatus->numOfTotalVnodes; + pDnode->moduleStatus = pStatus->moduleStatus; if (pStatus->dnodeId == 0) { mTrace("dnode:%d, first access, privateIp:%s, name:%s", pDnode->dnodeId, taosIpStr(pDnode->privateIp), pDnode->dnodeName); @@ -353,7 +355,7 @@ void mgmtProcessDnodeStatusMsg(SRpcMsg *rpcMsg) { mgmtGetMnodeList(&pRsp->mnodes); pRsp->dnodeState.dnodeId = htonl(pDnode->dnodeId); - pRsp->dnodeState.moduleStatus = htonl(pDnode->moduleStatus); + pRsp->dnodeState.moduleStatus = htonl((int32_t)pDnode->isMgmt); pRsp->dnodeState.createdTime = htonl(pDnode->createdTime / 1000); pRsp->dnodeState.numOfVnodes = 0; @@ -391,10 +393,6 @@ static int32_t mgmtCreateDnode(uint32_t ip) { pDnode->totalVnodes = TSDB_INVALID_VNODE_NUM; sprintf(pDnode->dnodeName, "n%d", sdbGetId(tsDnodeSdb) + 1); - if (pDnode->privateIp == inet_addr(tsMasterIp)) { - pDnode->moduleStatus |= (1 << TSDB_MOD_MGMT); - } - SSdbOperDesc oper = { .type = SDB_OPER_GLOBAL, .table = tsDnodeSdb, @@ -620,7 +618,7 @@ static int32_t mgmtRetrieveDnodes(SShowObj *pShow, char *data, int32_t rows, voi return numOfRows; } -bool mgmtCheckModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) { +static bool mgmtCheckModuleInDnode(SDnodeObj *pDnode, int32_t moduleType) { uint32_t status = pDnode->moduleStatus & (1 << moduleType); return status > 0; } diff --git a/src/mnode/src/mgmtMain.c b/src/mnode/src/mgmtMain.c index 371a09c03f..38f18b462a 100644 --- a/src/mnode/src/mgmtMain.c +++ b/src/mnode/src/mgmtMain.c @@ -20,6 +20,7 @@ #include "treplica.h" #include "tgrant.h" #include "ttimer.h" +#include "dnode.h" #include "mgmtDef.h" #include "mgmtLog.h" #include "mgmtAcct.h" @@ -100,6 +101,10 @@ int32_t mgmtStartSystem() { return -1; } + if (replicaInit() < 0) { + mError("failed to init replica") + } + if (mgmtInitDClient() < 0) { return -1; } @@ -108,10 +113,6 @@ int32_t mgmtStartSystem() { return -1; } - if (replicaInit() < 0) { - mError("failed to init dnode balance") - } - grantReset(TSDB_GRANT_ALL, 0); tsMgmtIsRunning = true; diff --git a/src/mnode/src/mgmtMnode.c b/src/mnode/src/mgmtMnode.c index e469dc4f6f..8087ce5ad1 100644 --- a/src/mnode/src/mgmtMnode.c +++ b/src/mnode/src/mgmtMnode.c @@ -55,6 +55,12 @@ static int32_t mgmtMnodeActionInsert(SSdbOperDesc *pOper) { static int32_t mgmtMnodeActionDelete(SSdbOperDesc *pOper) { SMnodeObj *pMnode = pOper->pObj; + + SDnodeObj *pDnode = mgmtGetDnode(pMnode->mnodeId); + if (pDnode == NULL) return TSDB_CODE_DNODE_NOT_EXIST; + pDnode->isMgmt = false; + mgmtReleaseDnode(pDnode); + mTrace("mnode:%d, is dropped from sdb", pMnode->mnodeId); return TSDB_CODE_SUCCESS; } diff --git a/src/mnode/src/mgmtSdb.c b/src/mnode/src/mgmtSdb.c index 013c0236d0..4bc18d6a0d 100644 --- a/src/mnode/src/mgmtSdb.c +++ b/src/mnode/src/mgmtSdb.c @@ -69,10 +69,16 @@ static void *(*sdbGetIndexFp[])(void *handle, void *key) = {sdbGetStrHashData, s static void (*sdbCleanUpIndexFp[])(void *handle) = {sdbCloseStrHash, sdbCloseIntHash, sdbCloseIntHash}; static void *(*sdbFetchRowFp[])(void *handle, void *ptr, void **ppRow) = {sdbFetchStrHashData, sdbFetchIntHashData, sdbFetchIntHashData}; -uint64_t sdbGetVersion() { return tsSdbObj->version; } int32_t sdbGetId(void *handle) { return ((SSdbTable *)handle)->autoIndex; } int64_t sdbGetNumOfRows(void *handle) { return ((SSdbTable *)handle)->numOfRows; } +uint64_t sdbGetVersion() { + if (tsSdbObj) + return tsSdbObj->version; + else + return 0; +} + static char *sdbGetActionStr(int32_t action) { switch (action) { case SDB_ACTION_INSERT: @@ -147,10 +153,6 @@ void sdbCleanUp() { } } -SSdbObject *sdbGetObj() { - return tsSdbObj; -} - void sdbIncRef(void *handle, void *pRow) { if (pRow) { SSdbTable *pTable = handle; diff --git a/src/mnode/src/mgmtUser.c b/src/mnode/src/mgmtUser.c index 92037ba793..9098a0c17d 100644 --- a/src/mnode/src/mgmtUser.c +++ b/src/mnode/src/mgmtUser.c @@ -18,6 +18,7 @@ #include "trpc.h" #include "ttime.h" #include "tutil.h" +#include "dnode.h" #include "mgmtDef.h" #include "mgmtLog.h" #include "mgmtAcct.h" @@ -93,7 +94,7 @@ static int32_t mgmtUserActionDecode(SSdbOperDesc *pOper) { } static int32_t mgmtUserActionRestored() { - if (strcmp(tsMasterIp, tsPrivateIp) == 0) { + if (dnodeIsFirstDeploy()) { SAcctObj *pAcct = mgmtGetAcct("root"); mgmtCreateUser(pAcct, "root", "taosdata"); mgmtCreateUser(pAcct, "monitor", tsInternalPass); diff --git a/src/util/src/tglobalcfg.c b/src/util/src/tglobalcfg.c index 8ce4530dbf..d912301641 100644 --- a/src/util/src/tglobalcfg.c +++ b/src/util/src/tglobalcfg.c @@ -110,12 +110,7 @@ short tsDaysPerFile = 10; int tsDaysToKeep = 3650; int tsReplications = TSDB_REPLICA_MIN_NUM; -#ifdef _MPEER int tsNumOfMPeers = 3; -#else -int tsNumOfMPeers = 1; -#endif - int tsMaxShellConns = 2000; int tsMaxTables = 100000; @@ -556,7 +551,7 @@ static void doInitGlobalConfig() { tsInitConfigOption(cfg++, "tblocks", &tsNumOfBlocksPerMeter, TSDB_CFG_VTYPE_SHORT, TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_SHOW, 32, 4096, 0, TSDB_CFG_UTYPE_NONE); -#ifdef _MPEER +#ifdef _SYNC tsInitConfigOption(cfg++, "numOfMPeers", &tsNumOfMPeers, TSDB_CFG_VTYPE_INT, TSDB_CFG_CTYPE_B_CONFIG | TSDB_CFG_CTYPE_B_SHOW | TSDB_CFG_CTYPE_B_CLUSTER, 1, 3, 0, TSDB_CFG_UTYPE_NONE);