Merge pull request #26845 from taosdata/fix/TD-31105-3.0

fix: load config from apollo url
This commit is contained in:
Hongze Cheng 2024-07-30 09:15:43 +08:00 committed by GitHub
commit 3b6ce5d7dc
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 50 additions and 41 deletions

View File

@ -396,8 +396,9 @@ struct SConfig *taosGetCfg() { return tsCfg; }
static int32_t taosLoadCfg(SConfig *pCfg, const char **envCmd, const char *inputCfgDir, const char *envFile, static int32_t taosLoadCfg(SConfig *pCfg, const char **envCmd, const char *inputCfgDir, const char *envFile,
char *apolloUrl) { char *apolloUrl) {
char cfgDir[PATH_MAX] = {0}; int32_t code = 0;
char cfgFile[PATH_MAX + 100] = {0}; char cfgDir[PATH_MAX] = {0};
char cfgFile[PATH_MAX + 100] = {0};
TAOS_CHECK_RETURN(taosExpandDir(inputCfgDir, cfgDir, PATH_MAX)); TAOS_CHECK_RETURN(taosExpandDir(inputCfgDir, cfgDir, PATH_MAX));
char lastC = cfgDir[strlen(cfgDir) - 1]; char lastC = cfgDir[strlen(cfgDir) - 1];
@ -424,32 +425,32 @@ static int32_t taosLoadCfg(SConfig *pCfg, const char **envCmd, const char *input
} }
if (apolloUrl != NULL && apolloUrl[0] == '\0') { if (apolloUrl != NULL && apolloUrl[0] == '\0') {
TAOS_CHECK_RETURN(cfgGetApollUrl(envCmd, envFile, apolloUrl)); (void)(cfgGetApollUrl(envCmd, envFile, apolloUrl));
} }
if (cfgLoad(pCfg, CFG_STYPE_APOLLO_URL, apolloUrl) != 0) { if ((code = cfgLoad(pCfg, CFG_STYPE_APOLLO_URL, apolloUrl)) != 0) {
uError("failed to load from apollo url:%s since %s", apolloUrl, terrstr()); uError("failed to load from apollo url:%s since %s", apolloUrl, tstrerror(code));
TAOS_RETURN(TSDB_CODE_INVALID_CFG); TAOS_RETURN(code);
} }
if (cfgLoad(pCfg, CFG_STYPE_CFG_FILE, cfgFile) != 0) { if ((code = cfgLoad(pCfg, CFG_STYPE_CFG_FILE, cfgFile)) != 0) {
uError("failed to load from cfg file:%s since %s", cfgFile, terrstr()); uError("failed to load from cfg file:%s since %s", cfgFile, tstrerror(code));
TAOS_RETURN(TSDB_CODE_INVALID_CFG); TAOS_RETURN(code);
} }
if (cfgLoad(pCfg, CFG_STYPE_ENV_FILE, envFile) != 0) { if ((code = cfgLoad(pCfg, CFG_STYPE_ENV_FILE, envFile)) != 0) {
uError("failed to load from env file:%s since %s", envFile, terrstr()); uError("failed to load from env file:%s since %s", envFile, tstrerror(code));
TAOS_RETURN(TSDB_CODE_INVALID_CFG); TAOS_RETURN(code);
} }
if (cfgLoad(pCfg, CFG_STYPE_ENV_VAR, NULL) != 0) { if ((code = cfgLoad(pCfg, CFG_STYPE_ENV_VAR, NULL)) != 0) {
uError("failed to load from global env variables since %s", terrstr()); uError("failed to load from global env variables since %s", tstrerror(code));
TAOS_RETURN(TSDB_CODE_INVALID_CFG); TAOS_RETURN(code);
} }
if (cfgLoad(pCfg, CFG_STYPE_ENV_CMD, envCmd) != 0) { if ((code = cfgLoad(pCfg, CFG_STYPE_ENV_CMD, envCmd)) != 0) {
uError("failed to load from cmd env variables since %s", terrstr()); uError("failed to load from cmd env variables since %s", tstrerror(code));
TAOS_RETURN(TSDB_CODE_INVALID_CFG); TAOS_RETURN(code);
} }
TAOS_RETURN(TSDB_CODE_SUCCESS); TAOS_RETURN(TSDB_CODE_SUCCESS);
@ -1280,12 +1281,12 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi
} }
if ((code = taosLoadCfg(pCfg, envCmd, cfgDir, envFile, apolloUrl)) != TSDB_CODE_SUCCESS) { if ((code = taosLoadCfg(pCfg, envCmd, cfgDir, envFile, apolloUrl)) != TSDB_CODE_SUCCESS) {
printf("failed to load cfg since %s", tstrerror(code)); printf("failed to load cfg since %s\n", tstrerror(code));
goto _exit; goto _exit;
} }
if ((code = cfgLoadFromArray(pCfg, pArgs)) != TSDB_CODE_SUCCESS) { if ((code = cfgLoadFromArray(pCfg, pArgs)) != TSDB_CODE_SUCCESS) {
printf("failed to load cfg from array since %s", tstrerror(code)); printf("failed to load cfg from array since %s\n", tstrerror(code));
goto _exit; goto _exit;
} }
@ -1302,12 +1303,12 @@ int32_t taosCreateLog(const char *logname, int32_t logFileNum, const char *cfgDi
TAOS_CHECK_RETURN(taosSetAllDebugFlag(pCfg, pDebugItem->i32)); TAOS_CHECK_RETURN(taosSetAllDebugFlag(pCfg, pDebugItem->i32));
if ((code = taosMulModeMkDir(tsLogDir, 0777, true)) != TSDB_CODE_SUCCESS) { if ((code = taosMulModeMkDir(tsLogDir, 0777, true)) != TSDB_CODE_SUCCESS) {
printf("failed to create dir:%s since %s", tsLogDir, tstrerror(code)); printf("failed to create dir:%s since %s\n", tsLogDir, tstrerror(code));
goto _exit; goto _exit;
} }
if ((code = taosInitLog(logname, logFileNum)) != 0) { if ((code = taosInitLog(logname, logFileNum)) != 0) {
printf("failed to init log file since %s", terrstr()); printf("failed to init log file since %s\n", tstrerror(code));
goto _exit; goto _exit;
} }
@ -1329,12 +1330,12 @@ int32_t taosReadDataFolder(const char *cfgDir, const char **envCmd, const char *
if (cfgAddInt32(pCfg, "dDebugFlag", dDebugFlag, 0, 255, CFG_SCOPE_SERVER, CFG_DYN_SERVER) != 0) return -1; if (cfgAddInt32(pCfg, "dDebugFlag", dDebugFlag, 0, 255, CFG_SCOPE_SERVER, CFG_DYN_SERVER) != 0) return -1;
if ((code = taosLoadCfg(pCfg, envCmd, cfgDir, envFile, apolloUrl)) != 0) { if ((code = taosLoadCfg(pCfg, envCmd, cfgDir, envFile, apolloUrl)) != 0) {
printf("failed to load cfg since %s", terrstr()); printf("failed to load cfg since %s\n", tstrerror(code));
goto _exit; goto _exit;
} }
if ((code = cfgLoadFromArray(pCfg, pArgs)) != 0) { if ((code = cfgLoadFromArray(pCfg, pArgs)) != 0) {
printf("failed to load cfg from array since %s", terrstr()); printf("failed to load cfg from array since %s\n", tstrerror(code));
goto _exit; goto _exit;
} }

View File

@ -941,7 +941,7 @@ int32_t taosBlockSIGPIPE() {
#endif #endif
} }
int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t* ip) { int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t *ip) {
#ifdef WINDOWS #ifdef WINDOWS
// Initialize Winsock // Initialize Winsock
WSADATA wsaData; WSADATA wsaData;
@ -959,7 +959,7 @@ int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t* ip) {
hints.ai_socktype = SOCK_STREAM; hints.ai_socktype = SOCK_STREAM;
struct addrinfo *result = NULL; struct addrinfo *result = NULL;
bool inRetry = false; bool inRetry = false;
while (true) { while (true) {
int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result); int32_t ret = getaddrinfo(fqdn, NULL, &hints, &result);
@ -972,7 +972,7 @@ int32_t taosGetIpv4FromFqdn(const char *fqdn, uint32_t* ip) {
return terrno; return terrno;
} }
terrno = TAOS_SYSTEM_ERROR(ret); terrno = TAOS_SYSTEM_ERROR(errno);
return terrno; return terrno;
} }

View File

@ -18,12 +18,12 @@
#include "cJSON.h" #include "cJSON.h"
#include "taoserror.h" #include "taoserror.h"
#include "tenv.h" #include "tenv.h"
#include "tglobal.h"
#include "tgrant.h" #include "tgrant.h"
#include "tjson.h" #include "tjson.h"
#include "tlog.h" #include "tlog.h"
#include "tunit.h" #include "tunit.h"
#include "tutil.h" #include "tutil.h"
#include "tglobal.h"
#define CFG_NAME_PRINT_LEN 24 #define CFG_NAME_PRINT_LEN 24
#define CFG_SRC_PRINT_LEN 12 #define CFG_SRC_PRINT_LEN 12
@ -1214,8 +1214,9 @@ int32_t cfgLoadFromCfgFile(SConfig *pConfig, const char *filepath) {
int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) { int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) {
char *cfgLineBuf = NULL, *name, *value, *value2, *value3, *value4; char *cfgLineBuf = NULL, *name, *value, *value2, *value3, *value4;
SJson *pJson = NULL;
int32_t olen, vlen, vlen2, vlen3, vlen4; int32_t olen, vlen, vlen2, vlen3, vlen4;
int32_t code = 0; int32_t code = 0, lino = 0;
if (url == NULL || strlen(url) == 0) { if (url == NULL || strlen(url) == 0) {
uInfo("apoll url not load"); uInfo("apoll url not load");
TAOS_RETURN(TSDB_CODE_SUCCESS); TAOS_RETURN(TSDB_CODE_SUCCESS);
@ -1228,7 +1229,6 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) {
} }
p++; p++;
SJson *pJson = NULL;
if (strncmp(url, "jsonFile", 8) == 0) { if (strncmp(url, "jsonFile", 8) == 0) {
char *filepath = p; char *filepath = p;
if (!taosCheckExistFile(filepath)) { if (!taosCheckExistFile(filepath)) {
@ -1238,7 +1238,7 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) {
TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ); TdFilePtr pFile = taosOpenFile(filepath, TD_FILE_READ);
if (pFile == NULL) { if (pFile == NULL) {
TAOS_RETURN(TAOS_SYSTEM_ERROR(errno)); TAOS_CHECK_EXIT(TAOS_SYSTEM_ERROR(errno));
} }
size_t fileSize = taosLSeekFile(pFile, 0, SEEK_END); size_t fileSize = taosLSeekFile(pFile, 0, SEEK_END);
char *buf = taosMemoryMalloc(fileSize + 1); char *buf = taosMemoryMalloc(fileSize + 1);
@ -1264,7 +1264,7 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) {
uError("load json file parse error: %s", jsonParseError); uError("load json file parse error: %s", jsonParseError);
} }
taosMemoryFreeClear(buf); taosMemoryFreeClear(buf);
TAOS_RETURN(TSDB_CODE_INVALID_DATA_FMT); TAOS_CHECK_EXIT(TSDB_CODE_INVALID_DATA_FMT);
} }
taosMemoryFreeClear(buf); taosMemoryFreeClear(buf);
@ -1273,16 +1273,19 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) {
cJSON *item = tjsonGetArrayItem(pJson, i); cJSON *item = tjsonGetArrayItem(pJson, i);
if (item == NULL) break; if (item == NULL) break;
char *itemName = NULL, *itemValueString = NULL; char *itemName = NULL, *itemValueString = NULL;
TAOS_CHECK_GOTO(tjsonGetObjectName(item, &itemName), NULL, _err_json); if (tjsonGetObjectName(item, &itemName) != 0) {
TAOS_CHECK_GOTO(tjsonGetObjectValueString(item, &itemValueString), NULL, _err_json); TAOS_CHECK_EXIT(TSDB_CODE_INVALID_DATA_FMT);
}
if (tjsonGetObjectValueString(item, &itemValueString) != 0) {
TAOS_CHECK_EXIT(TSDB_CODE_INVALID_DATA_FMT);
}
if (itemValueString != NULL && itemName != NULL) { if (itemValueString != NULL && itemName != NULL) {
size_t itemNameLen = strlen(itemName); size_t itemNameLen = strlen(itemName);
size_t itemValueStringLen = strlen(itemValueString); size_t itemValueStringLen = strlen(itemValueString);
void* px = taosMemoryRealloc(cfgLineBuf, itemNameLen + itemValueStringLen + 3); void *px = taosMemoryRealloc(cfgLineBuf, itemNameLen + itemValueStringLen + 3);
if (NULL == px) { if (NULL == px) {
code = TSDB_CODE_OUT_OF_MEMORY; TAOS_CHECK_EXIT(TSDB_CODE_OUT_OF_MEMORY);
goto _err_json;
} }
cfgLineBuf = px; cfgLineBuf = px;
@ -1321,6 +1324,7 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) {
} }
} }
tjsonDelete(pJson); tjsonDelete(pJson);
pJson = NULL;
// } else if (strncmp(url, "jsonUrl", 7) == 0) { // } else if (strncmp(url, "jsonUrl", 7) == 0) {
// } else if (strncmp(url, "etcdUrl", 7) == 0) { // } else if (strncmp(url, "etcdUrl", 7) == 0) {
@ -1333,8 +1337,12 @@ int32_t cfgLoadFromApollUrl(SConfig *pConfig, const char *url) {
uInfo("load from apoll url not implemented yet"); uInfo("load from apoll url not implemented yet");
TAOS_RETURN(TSDB_CODE_SUCCESS); TAOS_RETURN(TSDB_CODE_SUCCESS);
_err_json: _exit:
taosMemoryFree(cfgLineBuf);
tjsonDelete(pJson); tjsonDelete(pJson);
if (code != 0) {
uError("failed to load from apollo url:%s at line %d since %s", url, lino, tstrerror(code));
}
TAOS_RETURN(code); TAOS_RETURN(code);
} }
@ -1420,7 +1428,7 @@ int32_t cfgGetApollUrl(const char **envCmd, const char *envFile, char *apolloUrl
} }
uInfo("fail get apollo url from cmd env file"); uInfo("fail get apollo url from cmd env file");
TAOS_RETURN(TSDB_CODE_INVALID_PARA); TAOS_RETURN(TSDB_CODE_NOT_FOUND);
} }
struct SConfigIter { struct SConfigIter {