From ddd49a970a7d6a8ff6013e353c4dc0113edc7760 Mon Sep 17 00:00:00 2001 From: Ilya Repin Date: Mon, 9 Feb 2026 04:23:20 +0000 Subject: [PATCH] add hub gateway dynconfig --- .../components/hub/hub_gateway_component.cpp | 4 ++- .../hub_gateway/hub_gateway_config.cpp | 22 ++++++++++++++ .../hub_gateway/hub_gateway_config.hpp | 30 +++++++++++++++++++ .../dynconfig/predictor/predictor_config.cpp | 7 ++--- .../dynconfig/predictor/predictor_config.hpp | 4 +-- src/infra/hub_gateway/hub_gateway.cpp | 23 +++++--------- src/infra/hub_gateway/hub_gateway.hpp | 4 ++- .../heuristic_predictor.cpp | 2 +- .../heuristic_predictor_ut.cpp | 2 +- 9 files changed, 73 insertions(+), 25 deletions(-) create mode 100644 src/infra/dynconfig/hub_gateway/hub_gateway_config.cpp create mode 100644 src/infra/dynconfig/hub_gateway/hub_gateway_config.hpp diff --git a/src/infra/components/hub/hub_gateway_component.cpp b/src/infra/components/hub/hub_gateway_component.cpp index a608f27..463e35c 100644 --- a/src/infra/components/hub/hub_gateway_component.cpp +++ b/src/infra/components/hub/hub_gateway_component.cpp @@ -5,6 +5,7 @@ #include #include #include +#include namespace NCoordinator::NInfra::NComponents { @@ -16,8 +17,9 @@ THubGatewayComponent::THubGatewayComponent( : LoggableComponentBase(config, context) { auto& httpClient = context.FindComponent().GetHttpClient(); + auto configSource = context.FindComponent().GetSource(); - Gateway_ = std::make_unique(httpClient); + Gateway_ = std::make_unique(httpClient, std::move(configSource)); } NCore::NDomain::IHubGateway& THubGatewayComponent::GetGateway() diff --git a/src/infra/dynconfig/hub_gateway/hub_gateway_config.cpp b/src/infra/dynconfig/hub_gateway/hub_gateway_config.cpp new file mode 100644 index 0000000..572f9b5 --- /dev/null +++ b/src/infra/dynconfig/hub_gateway/hub_gateway_config.cpp @@ -0,0 +1,22 @@ +#include "hub_gateway_config.hpp" + +#include +#include + +namespace NCoordinator::NInfra { + +//////////////////////////////////////////////////////////////////////////////// + +THubGatewaySettings Parse( + const userver::formats::json::Value& value, + userver::formats::parse::To) +{ + return THubGatewaySettings{ + .Timeout = value["timeout_seconds"].As(), + .Retries = value["retries"].As(), + }; +} + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NCoordinator::NInfra diff --git a/src/infra/dynconfig/hub_gateway/hub_gateway_config.hpp b/src/infra/dynconfig/hub_gateway/hub_gateway_config.hpp new file mode 100644 index 0000000..1384f21 --- /dev/null +++ b/src/infra/dynconfig/hub_gateway/hub_gateway_config.hpp @@ -0,0 +1,30 @@ +#pragma once + +#include +#include + +namespace NCoordinator::NInfra { + +//////////////////////////////////////////////////////////////////////////////// + +struct THubGatewaySettings { + std::chrono::seconds Timeout{5}; + std::size_t Retries{3}; +}; + +inline const userver::dynamic_config::Key HUB_GATEWAY_CONFIG{ + "HUB_GATEWAY_CONFIG", + userver::dynamic_config::DefaultAsJsonString{R"( +{ + "timeout_seconds": 5, + "retries": 3 +} +)"}}; + +THubGatewaySettings Parse( + const userver::formats::json::Value& value, + userver::formats::parse::To); + +//////////////////////////////////////////////////////////////////////////////// + +} // namespace NCoordinator::NInfra diff --git a/src/infra/dynconfig/predictor/predictor_config.cpp b/src/infra/dynconfig/predictor/predictor_config.cpp index e2830a7..7ede5a4 100644 --- a/src/infra/dynconfig/predictor/predictor_config.cpp +++ b/src/infra/dynconfig/predictor/predictor_config.cpp @@ -4,7 +4,6 @@ #include #include -#include namespace NCoordinator::NInfra { @@ -14,12 +13,12 @@ TPredictorSettings Parse( const userver::formats::json::Value& value, userver::formats::parse::To) { - const auto defaultFirstLoadFactor = NCore::NDomain::TLoadFactor{ - value["default_first_load_factor"].As() + const auto firstLoadFactor = NCore::NDomain::TLoadFactor{ + value["first_load_factor"].As() }; return TPredictorSettings{ - .DefaultFirstLoadFactor = defaultFirstLoadFactor, + .FirstLoadFactor = firstLoadFactor, }; } diff --git a/src/infra/dynconfig/predictor/predictor_config.hpp b/src/infra/dynconfig/predictor/predictor_config.hpp index f0522c5..17cc329 100644 --- a/src/infra/dynconfig/predictor/predictor_config.hpp +++ b/src/infra/dynconfig/predictor/predictor_config.hpp @@ -10,14 +10,14 @@ namespace NCoordinator::NInfra { //////////////////////////////////////////////////////////////////////////////// struct TPredictorSettings { - NCore::NDomain::TLoadFactor DefaultFirstLoadFactor; + NCore::NDomain::TLoadFactor FirstLoadFactor; }; inline const userver::dynamic_config::Key PREDICTOR_CONFIG{ "PREDICTOR_CONFIG", userver::dynamic_config::DefaultAsJsonString{R"( { - "default_first_load_factor": 5 + "first_load_factor": 5 } )"}}; diff --git a/src/infra/hub_gateway/hub_gateway.cpp b/src/infra/hub_gateway/hub_gateway.cpp index 60cae69..b7f1439 100644 --- a/src/infra/hub_gateway/hub_gateway.cpp +++ b/src/infra/hub_gateway/hub_gateway.cpp @@ -1,6 +1,7 @@ #include "hub_gateway.hpp" #include +#include #include #include @@ -8,24 +9,13 @@ #include -namespace { - -//////////////////////////////////////////////////////////////////////////////// - -inline constexpr auto DEFAULT_TIMEOUT = std::chrono::seconds(5); - -inline constexpr auto DEFAULT_RETRIES = 3; - -//////////////////////////////////////////////////////////////////////////////// - -} - namespace NCoordinator::NInfra::NGateway { //////////////////////////////////////////////////////////////////////////////// -THubGateway::THubGateway(userver::clients::http::Client& client) +THubGateway::THubGateway(userver::clients::http::Client& client, userver::dynamic_config::Source configSource) : Client_(client) + , ConfigSource_(configSource) { } std::vector THubGateway::GetHubReports( @@ -34,12 +24,15 @@ std::vector THubGateway::GetHubReports( std::vector requests; requests.reserve(hubs.size()); + const auto snapshot = ConfigSource_.GetSnapshot(); + const auto config = snapshot[HUB_GATEWAY_CONFIG]; + for (const auto& hub : hubs) { auto request = Client_.CreateRequest() .follow_redirects(false) .get(hub.GetUnderlying()) - .timeout(DEFAULT_TIMEOUT) - .retry(DEFAULT_RETRIES) + .timeout(config.Timeout) + .retry(config.Retries) .async_perform(); requests.emplace_back(std::move(request)); diff --git a/src/infra/hub_gateway/hub_gateway.hpp b/src/infra/hub_gateway/hub_gateway.hpp index 055fd92..dca13c6 100644 --- a/src/infra/hub_gateway/hub_gateway.hpp +++ b/src/infra/hub_gateway/hub_gateway.hpp @@ -4,6 +4,7 @@ #include #include +#include #include @@ -15,13 +16,14 @@ class THubGateway : public NCore::NDomain::IHubGateway { public: - THubGateway(userver::clients::http::Client& client); + THubGateway(userver::clients::http::Client& client, userver::dynamic_config::Source configSource); std::vector GetHubReports( const std::vector& hubs) const override; private: userver::clients::http::Client& Client_; + userver::dynamic_config::Source ConfigSource_; }; //////////////////////////////////////////////////////////////////////////////// diff --git a/src/infra/load_factor_predictor/heuristic_predictor.cpp b/src/infra/load_factor_predictor/heuristic_predictor.cpp index 6cfd161..2b18fa8 100644 --- a/src/infra/load_factor_predictor/heuristic_predictor.cpp +++ b/src/infra/load_factor_predictor/heuristic_predictor.cpp @@ -23,7 +23,7 @@ NCore::NDomain::TLoadFactor THeuristicPredictor::PredictLoadFactor( if (currentTotalWeight <= std::numeric_limits::epsilon() || params.TotalPartitions == 0) { if (params.Increasing) { const auto snapshot = ConfigSource_.GetSnapshot(); - auto loadFactor = snapshot[PREDICTOR_CONFIG].DefaultFirstLoadFactor; + auto loadFactor = snapshot[PREDICTOR_CONFIG].FirstLoadFactor; return loadFactor; } else { return NCore::NDomain::TLoadFactor{0}; diff --git a/src/infra/load_factor_predictor/heuristic_predictor_ut.cpp b/src/infra/load_factor_predictor/heuristic_predictor_ut.cpp index 543693b..3a6c0cd 100644 --- a/src/infra/load_factor_predictor/heuristic_predictor_ut.cpp +++ b/src/infra/load_factor_predictor/heuristic_predictor_ut.cpp @@ -33,7 +33,7 @@ TPredictionParams MakeParams( return p; } -const auto DEFAULT_JSON = userver::formats::json::FromString(R"( {"default_first_load_factor": 5} )"); +const auto DEFAULT_JSON = userver::formats::json::FromString(R"( {"first_load_factor": 5} )"); ////////////////////////////////////////////////////////////////////////////////