Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/infra/components/hub/hub_gateway_component.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <userver/clients/http/component.hpp>
#include <userver/components/component.hpp>
#include <userver/components/component_context.hpp>
#include <userver/dynamic_config/storage/component.hpp>

namespace NCoordinator::NInfra::NComponents {

Expand All @@ -16,8 +17,9 @@ THubGatewayComponent::THubGatewayComponent(
: LoggableComponentBase(config, context)
{
auto& httpClient = context.FindComponent<userver::components::HttpClient>().GetHttpClient();
auto configSource = context.FindComponent<userver::components::DynamicConfig>().GetSource();

Gateway_ = std::make_unique<NInfra::NGateway::THubGateway>(httpClient);
Gateway_ = std::make_unique<NInfra::NGateway::THubGateway>(httpClient, std::move(configSource));
}

NCore::NDomain::IHubGateway& THubGatewayComponent::GetGateway()
Expand Down
22 changes: 22 additions & 0 deletions src/infra/dynconfig/hub_gateway/hub_gateway_config.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#include "hub_gateway_config.hpp"

#include <userver/dynamic_config/value.hpp>
#include <userver/formats/parse/common_containers.hpp>

namespace NCoordinator::NInfra {

////////////////////////////////////////////////////////////////////////////////

THubGatewaySettings Parse(
const userver::formats::json::Value& value,
userver::formats::parse::To<THubGatewaySettings>)
{
return THubGatewaySettings{
.Timeout = value["timeout_seconds"].As<std::chrono::seconds>(),
.Retries = value["retries"].As<std::size_t>(),
};
}

////////////////////////////////////////////////////////////////////////////////

} // namespace NCoordinator::NInfra
30 changes: 30 additions & 0 deletions src/infra/dynconfig/hub_gateway/hub_gateway_config.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#pragma once

#include <userver/dynamic_config/snapshot.hpp>
#include <userver/formats/json/value.hpp>

namespace NCoordinator::NInfra {

////////////////////////////////////////////////////////////////////////////////

struct THubGatewaySettings {
std::chrono::seconds Timeout{5};
std::size_t Retries{3};
};

inline const userver::dynamic_config::Key<THubGatewaySettings> 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<THubGatewaySettings>);

////////////////////////////////////////////////////////////////////////////////

} // namespace NCoordinator::NInfra
7 changes: 3 additions & 4 deletions src/infra/dynconfig/predictor/predictor_config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

#include <userver/dynamic_config/value.hpp>
#include <userver/formats/parse/common_containers.hpp>
#include <userver/ydb/settings.hpp>

namespace NCoordinator::NInfra {

Expand All @@ -14,12 +13,12 @@ TPredictorSettings Parse(
const userver::formats::json::Value& value,
userver::formats::parse::To<TPredictorSettings>)
{
const auto defaultFirstLoadFactor = NCore::NDomain::TLoadFactor{
value["default_first_load_factor"].As<NCore::NDomain::TLoadFactor::UnderlyingType>()
const auto firstLoadFactor = NCore::NDomain::TLoadFactor{
value["first_load_factor"].As<NCore::NDomain::TLoadFactor::UnderlyingType>()
};

return TPredictorSettings{
.DefaultFirstLoadFactor = defaultFirstLoadFactor,
.FirstLoadFactor = firstLoadFactor,
};
}

Expand Down
4 changes: 2 additions & 2 deletions src/infra/dynconfig/predictor/predictor_config.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@ namespace NCoordinator::NInfra {
////////////////////////////////////////////////////////////////////////////////

struct TPredictorSettings {
NCore::NDomain::TLoadFactor DefaultFirstLoadFactor;
NCore::NDomain::TLoadFactor FirstLoadFactor;
};

inline const userver::dynamic_config::Key<TPredictorSettings> PREDICTOR_CONFIG{
"PREDICTOR_CONFIG",
userver::dynamic_config::DefaultAsJsonString{R"(
{
"default_first_load_factor": 5
"first_load_factor": 5
}
)"}};

Expand Down
23 changes: 8 additions & 15 deletions src/infra/hub_gateway/hub_gateway.cpp
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
#include "hub_gateway.hpp"

#include <infra/serializer/serializer.hpp>
#include <infra/dynconfig/hub_gateway/hub_gateway_config.hpp>

#include <userver/engine/wait_any.hpp>
#include <userver/formats/json/serialize.hpp>
#include <userver/logging/log.hpp>

#include <chrono>

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<NCore::NDomain::THubReport> THubGateway::GetHubReports(
Expand All @@ -34,12 +24,15 @@ std::vector<NCore::NDomain::THubReport> THubGateway::GetHubReports(
std::vector<userver::clients::http::ResponseFuture> 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));
Expand Down
4 changes: 3 additions & 1 deletion src/infra/hub_gateway/hub_gateway.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <core/hub/hub_report.hpp>

#include <userver/clients/http/client.hpp>
#include <userver/dynamic_config/source.hpp>

#include <vector>

Expand All @@ -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<NCore::NDomain::THubReport> GetHubReports(
const std::vector<NCore::NDomain::THubEndpoint>& hubs) const override;

private:
userver::clients::http::Client& Client_;
userver::dynamic_config::Source ConfigSource_;
};

////////////////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/infra/load_factor_predictor/heuristic_predictor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ NCore::NDomain::TLoadFactor THeuristicPredictor::PredictLoadFactor(
if (currentTotalWeight <= std::numeric_limits<double>::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};
Expand Down
2 changes: 1 addition & 1 deletion src/infra/load_factor_predictor/heuristic_predictor_ut.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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} )");

////////////////////////////////////////////////////////////////////////////////

Expand Down