From 1244bbe49a0ea7c3a0eec5c4ef3a02e87d894245 Mon Sep 17 00:00:00 2001 From: Shengliang Guan Date: Mon, 21 Feb 2022 18:04:22 +0800 Subject: [PATCH] integrate cfg --- include/libs/config/config.h | 2 +- source/dnode/mgmt/daemon/CMakeLists.txt | 10 ++-- source/dnode/mgmt/daemon/inc/dmnInt.h | 38 ++++++++++++++ source/dnode/mgmt/daemon/src/daemon.c | 38 ++------------ source/dnode/mgmt/daemon/src/dmnCfg.c | 50 +++++++++++++++++++ source/libs/config/inc/cfgInt.h | 4 +- .../config/src/{cfgDotEnv.c => cfgEnvFile.c} | 4 +- source/libs/config/src/cfgEnvVar.c | 2 +- source/libs/config/src/config.c | 16 +++--- source/libs/config/test/cfgTest.cpp | 8 +-- 10 files changed, 116 insertions(+), 56 deletions(-) create mode 100644 source/dnode/mgmt/daemon/inc/dmnInt.h create mode 100644 source/dnode/mgmt/daemon/src/dmnCfg.c rename source/libs/config/src/{cfgDotEnv.c => cfgEnvFile.c} (85%) diff --git a/include/libs/config/config.h b/include/libs/config/config.h index c785c5d797..3806d9f605 100644 --- a/include/libs/config/config.h +++ b/include/libs/config/config.h @@ -26,7 +26,7 @@ extern "C" { typedef enum { CFG_TYPE_DEFAULT, CFG_TYPE_CFG_FILE, - CFG_TYPE_DOT_ENV, + CFG_TYPE_ENV_FILE, CFG_TYPE_ENV_VAR, CFG_TYPE_APOLLO_URL, CFG_TYPE_CONSOLE_PARA diff --git a/source/dnode/mgmt/daemon/CMakeLists.txt b/source/dnode/mgmt/daemon/CMakeLists.txt index 7f9a7c4d21..a577344761 100644 --- a/source/dnode/mgmt/daemon/CMakeLists.txt +++ b/source/dnode/mgmt/daemon/CMakeLists.txt @@ -1,9 +1,9 @@ aux_source_directory(src DAEMON_SRC) add_executable(taosd ${DAEMON_SRC}) -target_link_libraries( + +target_include_directories( taosd - PUBLIC dnode - PUBLIC config - PUBLIC util - PUBLIC os + PRIVATE "${CMAKE_CURRENT_SOURCE_DIR}/inc" ) + +target_link_libraries(taosd dnode config util os) diff --git a/source/dnode/mgmt/daemon/inc/dmnInt.h b/source/dnode/mgmt/daemon/inc/dmnInt.h new file mode 100644 index 0000000000..1bba29bbd2 --- /dev/null +++ b/source/dnode/mgmt/daemon/inc/dmnInt.h @@ -0,0 +1,38 @@ + +/* + * 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 _TD_DMN_INT_H_ +#define _TD_DMN_INT_H_ + +#include "config.h" +#include "dnode.h" +#include "tconfig.h" +#include "tglobal.h" +#include "tnote.h" +#include "ulog.h" + +#ifdef __cplusplus +extern "C" { +#endif + +int32_t dnmInitCfg(SDnodeEnvCfg *pEnvCfg, SDnodeObjCfg *pObjCfg, const char *configFile, const char *envFile, + const char *apolloUrl); + +#ifdef __cplusplus +} +#endif + +#endif /*_TD_DMN_INT_H_*/ diff --git a/source/dnode/mgmt/daemon/src/daemon.c b/source/dnode/mgmt/daemon/src/daemon.c index ce672bf7b8..a40867d11d 100644 --- a/source/dnode/mgmt/daemon/src/daemon.c +++ b/source/dnode/mgmt/daemon/src/daemon.c @@ -15,11 +15,7 @@ #define _DEFAULT_SOURCE #include "dnode.h" -#include "os.h" -#include "tconfig.h" -#include "tglobal.h" -#include "tnote.h" -#include "ulog.h" +#include "dmnInt.h" static struct { bool stop; @@ -136,38 +132,14 @@ void dmnWaitSignal() { } } -void dnmInitEnvCfg(SDnodeEnvCfg *pCfg) { - pCfg->sver = 30000000; // 3.0.0.0 - pCfg->numOfCores = tsNumOfCores; - pCfg->numOfCommitThreads = tsNumOfCommitThreads; - pCfg->enableTelem = 0; - tstrncpy(pCfg->timezone, tsTimezone, TSDB_TIMEZONE_LEN); - tstrncpy(pCfg->locale, tsLocale, TSDB_LOCALE_LEN); - tstrncpy(pCfg->charset, tsCharset, TSDB_LOCALE_LEN); - tstrncpy(pCfg->buildinfo, buildinfo, 64); - tstrncpy(pCfg->gitinfo, gitinfo, 48); -} - -void dmnInitObjCfg(SDnodeObjCfg *pCfg) { - pCfg->numOfSupportVnodes = tsNumOfSupportVnodes; - pCfg->statusInterval = tsStatusInterval; - pCfg->numOfThreadsPerCore = tsNumOfThreadsPerCore; - pCfg->ratioOfQueryCores = tsRatioOfQueryCores; - pCfg->maxShellConns = tsMaxShellConns; - pCfg->shellActivityTimer = tsShellActivityTimer; - pCfg->serverPort = tsServerPort; - tstrncpy(pCfg->dataDir, tsDataDir, TSDB_FILENAME_LEN); - tstrncpy(pCfg->localEp, tsLocalEp, TSDB_EP_LEN); - tstrncpy(pCfg->localFqdn, tsLocalFqdn, TSDB_FQDN_LEN); - tstrncpy(pCfg->firstEp, tsFirst, TSDB_EP_LEN); -} - int dmnRunDnode() { SDnodeEnvCfg envCfg = {0}; SDnodeObjCfg objCfg = {0}; - dnmInitEnvCfg(&envCfg); - dmnInitObjCfg(&objCfg); + if (dnmInitCfg(&envCfg, &objCfg, "", "", "") != 0) { + uInfo("Failed to start TDengine since load config error"); + return -1; + } if (dndInit(&envCfg) != 0) { uInfo("Failed to start TDengine, please check the log at %s", tsLogDir); diff --git a/source/dnode/mgmt/daemon/src/dmnCfg.c b/source/dnode/mgmt/daemon/src/dmnCfg.c new file mode 100644 index 0000000000..c941f5959c --- /dev/null +++ b/source/dnode/mgmt/daemon/src/dmnCfg.c @@ -0,0 +1,50 @@ +/* + * 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 "dmnInt.h" + +static void dmnInitEnvCfg(SDnodeEnvCfg *pCfg) { + pCfg->sver = 30000000; // 3.0.0.0 + pCfg->numOfCores = tsNumOfCores; + pCfg->numOfCommitThreads = tsNumOfCommitThreads; + pCfg->enableTelem = 0; + tstrncpy(pCfg->timezone, tsTimezone, TSDB_TIMEZONE_LEN); + tstrncpy(pCfg->locale, tsLocale, TSDB_LOCALE_LEN); + tstrncpy(pCfg->charset, tsCharset, TSDB_LOCALE_LEN); + tstrncpy(pCfg->buildinfo, buildinfo, 64); + tstrncpy(pCfg->gitinfo, gitinfo, 48); +} + +static void dmnInitObjCfg(SDnodeObjCfg *pCfg) { + pCfg->numOfSupportVnodes = tsNumOfSupportVnodes; + pCfg->statusInterval = tsStatusInterval; + pCfg->numOfThreadsPerCore = tsNumOfThreadsPerCore; + pCfg->ratioOfQueryCores = tsRatioOfQueryCores; + pCfg->maxShellConns = tsMaxShellConns; + pCfg->shellActivityTimer = tsShellActivityTimer; + pCfg->serverPort = tsServerPort; + tstrncpy(pCfg->dataDir, tsDataDir, TSDB_FILENAME_LEN); + tstrncpy(pCfg->localEp, tsLocalEp, TSDB_EP_LEN); + tstrncpy(pCfg->localFqdn, tsLocalFqdn, TSDB_FQDN_LEN); + tstrncpy(pCfg->firstEp, tsFirst, TSDB_EP_LEN); +} + +int32_t dnmInitCfg(SDnodeEnvCfg *pEnvCfg, SDnodeObjCfg *pObjCfg, const char *configFile, const char *envFile, + const char *apolloUrl) { + dmnInitEnvCfg(pEnvCfg); + dmnInitObjCfg(pObjCfg); + return 0; +} \ No newline at end of file diff --git a/source/libs/config/inc/cfgInt.h b/source/libs/config/inc/cfgInt.h index 56cc58e658..821e920610 100644 --- a/source/libs/config/inc/cfgInt.h +++ b/source/libs/config/inc/cfgInt.h @@ -32,8 +32,8 @@ typedef struct SConfig { } SConfig; int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath); -int32_t cfgLoadFromDotEnvFile(SConfig *pConfig, const char *filepath); -int32_t cfgLoadFromGlobalEnvVariable(SConfig *pConfig); +int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *filepath); +int32_t cfgLoadFromEnvVar(SConfig *pConfig); int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url); #ifdef __cplusplus diff --git a/source/libs/config/src/cfgDotEnv.c b/source/libs/config/src/cfgEnvFile.c similarity index 85% rename from source/libs/config/src/cfgDotEnv.c rename to source/libs/config/src/cfgEnvFile.c index e65dddae7e..a12d1cd1d5 100644 --- a/source/libs/config/src/cfgDotEnv.c +++ b/source/libs/config/src/cfgEnvFile.c @@ -16,7 +16,7 @@ #define _DEFAULT_SOURCE #include "cfgInt.h" -int32_t cfgLoadFromDotEnvFile(SConfig *pConfig, const char *filepath) { - uInfo("load from .env file %s", filepath); +int32_t cfgLoadFromEnvFile(SConfig *pConfig, const char *filepath) { + uInfo("load from env file %s", filepath); return 0; } \ No newline at end of file diff --git a/source/libs/config/src/cfgEnvVar.c b/source/libs/config/src/cfgEnvVar.c index caa5a88110..c3b3df4047 100644 --- a/source/libs/config/src/cfgEnvVar.c +++ b/source/libs/config/src/cfgEnvVar.c @@ -16,7 +16,7 @@ #define _DEFAULT_SOURCE #include "cfgInt.h" -int32_t cfgLoadFromGlobalEnvVariable(SConfig *pConfig) { +int32_t cfgLoadFromEnvVar(SConfig *pConfig) { uInfo("load from global env variables"); return 0; } \ No newline at end of file diff --git a/source/libs/config/src/config.c b/source/libs/config/src/config.c index 9650489655..98d3ad234c 100644 --- a/source/libs/config/src/config.c +++ b/source/libs/config/src/config.c @@ -37,10 +37,10 @@ int32_t cfgLoad(SConfig *pConfig, ECfgSrcType cfgType, const char *sourceStr) { switch (cfgType) { case CFG_TYPE_CFG_FILE: return cfgLoadFromCfgFile(pConfig, sourceStr); - case CFG_TYPE_DOT_ENV: - return cfgLoadFromDotEnvFile(pConfig, sourceStr); + case CFG_TYPE_ENV_FILE: + return cfgLoadFromEnvFile(pConfig, sourceStr); case CFG_TYPE_ENV_VAR: - return cfgLoadFromGlobalEnvVariable(pConfig); + return cfgLoadFromEnvVar(pConfig); case CFG_TYPE_APOLLO_URL: return cfgLoadFromApollUrl(pConfig, sourceStr); default: @@ -206,13 +206,13 @@ const char *cfgStypeStr(ECfgSrcType type) { case CFG_TYPE_DEFAULT: return "default"; case CFG_TYPE_CFG_FILE: - return "cfg"; - case CFG_TYPE_DOT_ENV: - return ".env"; + return "cfg_file"; + case CFG_TYPE_ENV_FILE: + return "env_file"; case CFG_TYPE_ENV_VAR: - return "env"; + return "env_var"; case CFG_TYPE_APOLLO_URL: - return "apollo"; + return "apollo_url"; default: return "invalid"; } diff --git a/source/libs/config/test/cfgTest.cpp b/source/libs/config/test/cfgTest.cpp index d198d0bfe1..8e031e8a89 100644 --- a/source/libs/config/test/cfgTest.cpp +++ b/source/libs/config/test/cfgTest.cpp @@ -30,10 +30,10 @@ const char *CfgTest::pConfig; TEST_F(CfgTest, 02_Str) { EXPECT_STREQ(cfgStypeStr(CFG_TYPE_DEFAULT), "default"); - EXPECT_STREQ(cfgStypeStr(CFG_TYPE_CFG_FILE), "cfg"); - EXPECT_STREQ(cfgStypeStr(CFG_TYPE_DOT_ENV), ".env"); - EXPECT_STREQ(cfgStypeStr(CFG_TYPE_ENV_VAR), "env"); - EXPECT_STREQ(cfgStypeStr(CFG_TYPE_APOLLO_URL), "apollo"); + EXPECT_STREQ(cfgStypeStr(CFG_TYPE_CFG_FILE), "cfg_file"); + EXPECT_STREQ(cfgStypeStr(CFG_TYPE_ENV_FILE), "env_file"); + EXPECT_STREQ(cfgStypeStr(CFG_TYPE_ENV_VAR), "env_var"); + EXPECT_STREQ(cfgStypeStr(CFG_TYPE_APOLLO_URL), "apollo_url"); EXPECT_STREQ(cfgStypeStr(ECfgSrcType(1024)), "invalid"); EXPECT_STREQ(cfgDtypeStr(CFG_DTYPE_NONE), "none");