From eca60bd1bcef49bc4f2f281b85e7266a85f94379 Mon Sep 17 00:00:00 2001 From: lambdaliu Date: Mon, 23 Aug 2021 15:36:18 +0800 Subject: [PATCH 1/2] fix health checker plugin config parse --- .../plugin/health_checker/health_checker.h | 13 -------- .../plugin/health_checker/http_detector.cpp | 15 ++++++---- .../plugin/health_checker/tcp_detector.cpp | 25 +++++++++------- .../plugin/health_checker/udp_detector.cpp | 30 +++++++++++-------- 4 files changed, 42 insertions(+), 41 deletions(-) diff --git a/polaris/plugin/health_checker/health_checker.h b/polaris/plugin/health_checker/health_checker.h index 67a18e3..da59665 100644 --- a/polaris/plugin/health_checker/health_checker.h +++ b/polaris/plugin/health_checker/health_checker.h @@ -39,19 +39,6 @@ static const char kChainPluginListDefault[] = "tcp"; static const char kCheckerIntervalKey[] = "interval"; static const uint64_t kDetectorIntervalDefault = 10 * 1000; // 探活默认时间间隔10s -static const char kHttpRequestPathKey[] = "path"; -static const char kHttpRequestPathDefault[] = ""; - -static const char kTcpSendPackageKey[] = "send"; -static const char kTcpSendPackageDefault[] = ""; -static const char kTcpReceivePackageKey[] = "receive"; -static const char kTcpReceivePackageDefault[] = ""; - -static const char kUdpSendPackageKey[] = "send"; -static const char kUdpSendPackageDefault[] = ""; -static const char kUdpReceivePackageKey[] = "receive"; -static const char kUdpReceivePackageDefault[] = ""; - static const char kTimeoutKey[] = "timeout"; // 超时时间毫秒 static const uint64_t kTimeoutDefault = 500; // 默认500ms } // namespace HealthCheckerConfig diff --git a/polaris/plugin/health_checker/http_detector.cpp b/polaris/plugin/health_checker/http_detector.cpp index 4c6e27b..d9e187a 100644 --- a/polaris/plugin/health_checker/http_detector.cpp +++ b/polaris/plugin/health_checker/http_detector.cpp @@ -31,15 +31,18 @@ HttpHealthChecker::HttpHealthChecker() { timeout_ms_ = 0; } HttpHealthChecker::~HttpHealthChecker() {} ReturnCode HttpHealthChecker::Init(Config* config, Context* /*context*/) { - request_path_ = config->GetStringOrDefault(HealthCheckerConfig::kHttpRequestPathKey, - HealthCheckerConfig::kHttpRequestPathDefault); + static const char kHttpRequestPathKey[] = "path"; + static const char kHttpRequestPathDefault[] = ""; + + request_path_ = config->GetStringOrDefault(kHttpRequestPathKey, kHttpRequestPathDefault); if (request_path_.empty() || request_path_[0] != '/') { - POLARIS_LOG(LOG_ERROR, "health checker[%s] config %s invalid", kPluginHttpHealthChecker, - HealthCheckerConfig::kHttpRequestPathKey); + POLARIS_LOG(LOG_ERROR, "outlier detector[%s] config %s invalid", kPluginHttpHealthChecker, + kHttpRequestPathKey); return kReturnInvalidConfig; } - timeout_ms_ = config->GetIntOrDefault(HealthCheckerConfig::kTimeoutKey, - HealthCheckerConfig::kTimeoutDefault); + timeout_ms_ = config->GetMsOrDefault(HealthCheckerConfig::kTimeoutKey, + HealthCheckerConfig::kTimeoutDefault); + return kReturnOk; } diff --git a/polaris/plugin/health_checker/tcp_detector.cpp b/polaris/plugin/health_checker/tcp_detector.cpp index 9f6dd43..8122d3a 100644 --- a/polaris/plugin/health_checker/tcp_detector.cpp +++ b/polaris/plugin/health_checker/tcp_detector.cpp @@ -29,22 +29,27 @@ TcpHealthChecker::TcpHealthChecker() { timeout_ms_ = 0; } TcpHealthChecker::~TcpHealthChecker() {} ReturnCode TcpHealthChecker::Init(Config* config, Context* /*context*/) { - std::string send_package = config->GetStringOrDefault( - HealthCheckerConfig::kTcpSendPackageKey, HealthCheckerConfig::kTcpSendPackageDefault); + static const char kTcpSendPackageKey[] = "send"; + static const char kTcpSendPackageDefault[] = ""; + static const char kTcpReceivePackageKey[] = "receive"; + static const char kTcpReceivePackageDefault[] = ""; + + std::string send_package = config->GetStringOrDefault(kTcpSendPackageKey, kTcpSendPackageDefault); if (!send_package.empty() && !Utils::HexStringToBytes(send_package, &send_package_)) { - POLARIS_LOG(LOG_ERROR, "health checker[%s] config %s hexstring to bytes failed", - kPluginTcpHealthChecker, HealthCheckerConfig::kTcpSendPackageKey); + POLARIS_LOG(LOG_ERROR, "outlier detector[%s] config %s hexstring to bytes failed", + kPluginTcpHealthChecker, kTcpSendPackageKey); return kReturnInvalidConfig; } - std::string receive_package = config->GetStringOrDefault( - HealthCheckerConfig::kTcpReceivePackageKey, HealthCheckerConfig::kTcpReceivePackageDefault); + std::string receive_package = + config->GetStringOrDefault(kTcpReceivePackageKey, kTcpReceivePackageDefault); if (!receive_package.empty() && !Utils::HexStringToBytes(receive_package, &receive_package_)) { - POLARIS_LOG(LOG_ERROR, "health checker[%s] config %s hexstring to bytes failed", - kPluginTcpHealthChecker, HealthCheckerConfig::kTcpReceivePackageKey); + POLARIS_LOG(LOG_ERROR, "outlier detector[%s] config %s hexstring to bytes failed", + kPluginTcpHealthChecker, kTcpReceivePackageKey); return kReturnInvalidConfig; } - timeout_ms_ = config->GetIntOrDefault(HealthCheckerConfig::kTimeoutKey, - HealthCheckerConfig::kTimeoutDefault); + + timeout_ms_ = config->GetMsOrDefault(HealthCheckerConfig::kTimeoutKey, + HealthCheckerConfig::kTimeoutDefault); return kReturnOk; } diff --git a/polaris/plugin/health_checker/udp_detector.cpp b/polaris/plugin/health_checker/udp_detector.cpp index 484d6df..9f4a941 100644 --- a/polaris/plugin/health_checker/udp_detector.cpp +++ b/polaris/plugin/health_checker/udp_detector.cpp @@ -31,27 +31,33 @@ UdpHealthChecker::UdpHealthChecker() { timeout_ms_ = 0; } UdpHealthChecker::~UdpHealthChecker() {} ReturnCode UdpHealthChecker::Init(Config* config, Context* /*context*/) { - std::string send_package = config->GetStringOrDefault( - HealthCheckerConfig::kUdpSendPackageKey, HealthCheckerConfig::kUdpSendPackageDefault); + static const char kUdpSendPackageKey[] = "send"; + static const char kUdpSendPackageDefault[] = ""; + static const char kUdpReceivePackageKey[] = "receive"; + static const char kUdpReceivePackageDefault[] = ""; + + std::string send_package = config->GetStringOrDefault(kUdpSendPackageKey, kUdpSendPackageDefault); if (send_package.empty()) { - POLARIS_LOG(LOG_ERROR, "health checker[%s] config %s should not be empty", - kPluginUdpHealthChecker, HealthCheckerConfig::kUdpSendPackageKey); + POLARIS_LOG(LOG_ERROR, "outlier detector[%s] config %s should not be empty", + kPluginUdpHealthChecker, kUdpSendPackageKey); return kReturnInvalidConfig; } if (!Utils::HexStringToBytes(send_package, &send_package_)) { - POLARIS_LOG(LOG_ERROR, "health checker[%s] config %s hexstring to bytes failed", - kPluginUdpHealthChecker, HealthCheckerConfig::kUdpSendPackageKey); + POLARIS_LOG(LOG_ERROR, "outlier detector[%s] config %s hexstring to bytes failed", + kPluginUdpHealthChecker, kUdpSendPackageKey); + return kReturnInvalidConfig; } - std::string receive_package = config->GetStringOrDefault( - HealthCheckerConfig::kUdpReceivePackageKey, HealthCheckerConfig::kUdpReceivePackageDefault); + std::string receive_package = + config->GetStringOrDefault(kUdpReceivePackageKey, kUdpReceivePackageDefault); if (!receive_package.empty()) { if (!Utils::HexStringToBytes(receive_package, &receive_package_)) { - POLARIS_LOG(LOG_ERROR, "health checker[%s] config %s hexstring to bytes failed", - kPluginUdpHealthChecker, HealthCheckerConfig::kUdpReceivePackageKey); + POLARIS_LOG(LOG_ERROR, "outlier detector[%s] config %s hexstring to bytes failed", + kPluginUdpHealthChecker, kUdpReceivePackageKey); + return kReturnInvalidConfig; } } - timeout_ms_ = config->GetIntOrDefault(HealthCheckerConfig::kTimeoutKey, - HealthCheckerConfig::kTimeoutDefault); + timeout_ms_ = config->GetMsOrDefault(HealthCheckerConfig ::kTimeoutKey, + HealthCheckerConfig::kTimeoutDefault); return kReturnOk; } From ccf171a9cc50eaa0d47d5c4148ea33fe43354ded Mon Sep 17 00:00:00 2001 From: lambdaliu Date: Fri, 27 Aug 2021 10:53:54 +0800 Subject: [PATCH 2/2] fix release pre subconfig before create next subconfig --- polaris/context.cpp | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/polaris/context.cpp b/polaris/context.cpp index 58c9754..0bad2f3 100644 --- a/polaris/context.cpp +++ b/polaris/context.cpp @@ -605,7 +605,8 @@ ReturnCode ContextImpl::InitGlobalConfig(Config* config, Context* context) { } // Init stat reporter - plugin_config.Reset(config->GetSubConfig("statReporter")); + delete plugin_config.Release(); + plugin_config.Set(config->GetSubConfig("statReporter")); plugin = NULL; std::string plugin_name = plugin_config->GetStringOrDefault("name", kPluginDefaultStatReporter); PluginManager::Instance().GetPlugin(plugin_name, kPluginStatReporter, plugin); @@ -622,7 +623,8 @@ ReturnCode ContextImpl::InitGlobalConfig(Config* config, Context* context) { } // Init alert reporter - plugin_config.Reset(config->GetSubConfig("alertReporter")); + delete plugin_config.Release(); + plugin_config.Set(config->GetSubConfig("alertReporter")); plugin = NULL; plugin_name = plugin_config->GetStringOrDefault("name", kPluginDefaultAlertReporter); PluginManager::Instance().GetPlugin(plugin_name, kPluginAlertReporter, plugin); @@ -639,7 +641,8 @@ ReturnCode ContextImpl::InitGlobalConfig(Config* config, Context* context) { } // Init server metric - plugin_config.Reset(config->GetSubConfig("serverMetric")); + delete plugin_config.Release(); + plugin_config.Set(config->GetSubConfig("serverMetric")); plugin = NULL; plugin_name = plugin_config->GetStringOrDefault("name", ""); if (!plugin_name.empty()) {