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
13 changes: 9 additions & 4 deletions configs/static_config.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
config_vars: config_vars.yaml
components_manager:
task_processors: # Task processor is an executor for coroutine tasks

Expand Down Expand Up @@ -36,6 +37,7 @@ components_manager:
testsuite-support: {}

congestion-control:
load-enabled: false

http-client:
http-client-core:
Expand Down Expand Up @@ -94,6 +96,9 @@ components_manager:
leader-service:
load-enabled: true

admin-service:
load-enabled: true

handler-ping:
load-enabled: true
path: /ping
Expand All @@ -102,8 +107,8 @@ components_manager:
throttling_enabled: false
url_trailing_slash: strict-match

handler-hello: # Finally! Our handler.
handler-get-context:
load-enabled: true
path: /hello # Registering handler by URL '/hello'.
method: GET,POST # It will only reply to GET (HEAD) and POST requests.
task_processor: main-task-processor # Run it on CPU bound task processor
path: /admin/context
method: GET
task_processor: main-task-processor
27 changes: 27 additions & 0 deletions src/api/http/exceptions/handler_exceptions.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#include "handler_exceptions.hpp"

namespace NCoordinator::NInfra::NHandlers {

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

userver::formats::json::Value MakeError(std::string_view field_name, std::string_view message)
{
userver::formats::json::ValueBuilder error_builder;
error_builder["errors"][std::string{field_name}].PushBack(message);
return error_builder.ExtractValue();
}

userver::formats::json::Value MakeServerError(std::optional<std::string_view> message)
{
userver::formats::json::ValueBuilder error_builder;
error_builder["errors"].PushBack(message.value_or("Internal Server Error"));
return error_builder.ExtractValue();
}

TServerException::TServerException(std::string_view msg)
: BaseType(MakeServerError(msg))
{ }

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

} // namespace NCoordinator::NInfra::NHandlers
23 changes: 23 additions & 0 deletions src/api/http/exceptions/handler_exceptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
#pragma once

#include <userver/server/handlers/exceptions.hpp>

namespace NCoordinator::NInfra::NHandlers {

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

userver::formats::json::Value MakeError(std::string_view error);

userver::formats::json::Value MakeServerError(std::optional<std::string_view> message = std::nullopt);

// HTTP 500
class TServerException
: public userver::server::handlers::ExceptionWithCode<userver::server::handlers::HandlerErrorCode::kServerSideError>
{
public:
TServerException(std::string_view msg);
};

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

} // namespace NCoordinator::NInfra::NHandlers
45 changes: 45 additions & 0 deletions src/api/http/v1/admin/get_context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "get_context.hpp"

#include <app/dto/admin/get_context.hpp>

#include <infra/components/admin/admin_service_component.hpp>
#include <infra/serializer/serializer.hpp>

#include <api/http/exceptions/handler_exceptions.hpp>

#include <userver/logging/log.hpp>

namespace NCoordinator::NInfra::NHandlers {

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

TGetContextHandler::TGetContextHandler(
const userver::components::ComponentConfig& config,
const userver::components::ComponentContext& context)
: HttpHandlerJsonBase(config, context)
, AdminService_(context.FindComponent<NComponents::TAdminServiceComponent>().GetService())
{ }

userver::formats::json::Value TGetContextHandler::HandleRequestJsonThrow(
const userver::server::http::HttpRequest& /*request*/,
const userver::formats::json::Value& /*request_json*/,
userver::server::request::RequestContext& /*request_context*/) const
{
NApp::NDto::TGetContextResponse result;

try {
result = AdminService_.GetCoordinationContext();
} catch (const NApp::NUseCase::TGetContextTemporaryUnavailable& ex) {
LOG_ERROR() << "Get context unavailable: " << ex.what();
throw TServerException("Get context temporary unavailable");
}

userver::formats::json::ValueBuilder builder;
builder["context"] = SerializeCoordinationContext(result.Context);

return builder.ExtractValue();
}

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

} // namespace NChat::NInfra::NHandlers
34 changes: 34 additions & 0 deletions src/api/http/v1/admin/get_context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <app/services/admin/admin_service.hpp>

#include <userver/components/component_context.hpp>
#include <userver/server/handlers/http_handler_json_base.hpp>
#include <userver/ydb/component.hpp>

namespace NCoordinator::NInfra::NHandlers {

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

class TGetContextHandler final
: public userver::server::handlers::HttpHandlerJsonBase
{
public:
static constexpr std::string_view kName = "handler-get-context";

TGetContextHandler(
const userver::components::ComponentConfig&,
const userver::components::ComponentContext&);

userver::formats::json::Value HandleRequestJsonThrow(
const userver::server::http::HttpRequest& request,
const userver::formats::json::Value& request_json,
userver::server::request::RequestContext& context) const override;

private:
NApp::NService::TAdminService& AdminService_;
};

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

} // namespace NChat::NInfra::NHandlers
15 changes: 15 additions & 0 deletions src/app/dto/admin/get_context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#pragma once

#include <core/coordination/coordination_context.hpp>

namespace NCoordinator::NApp::NDto {

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

struct TGetContextResponse {
NCore::NDomain::TCoordinationContext Context;
};

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

} // namespace NCoordinator::NApp::NDto
18 changes: 18 additions & 0 deletions src/app/exceptions.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#pragma once

#include <stdexcept>

namespace NCoordinator::NApp {

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

class TApplicationException
: public std::runtime_error
{
public:
using std::runtime_error::runtime_error;
};

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

} // namespace NCoordinator::NApp
18 changes: 18 additions & 0 deletions src/app/services/admin/admin_service.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "admin_service.hpp"

namespace NCoordinator::NApp::NService {

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

TAdminService::TAdminService(NCore::NDomain::ICoordinationRepository& coordinationRepository)
: GetContextUseCase_(coordinationRepository)
{ }

NDto::TGetContextResponse TAdminService::GetCoordinationContext() const
{
return GetContextUseCase_.Execute();
}

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

} // namespace NCoordinator::NApp::NService
26 changes: 26 additions & 0 deletions src/app/services/admin/admin_service.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#pragma once

#include <app/dto/admin/get_context.hpp>
#include <app/use_cases/admin/get_context/get_context.hpp>
#include <core/coordination/coordination_gateway.hpp>
#include <core/coordination/coordination_repository.hpp>
#include <core/hub/hub_gateway.hpp>

namespace NCoordinator::NApp::NService {

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

class TAdminService final
{
public:
TAdminService(NCore::NDomain::ICoordinationRepository& coordinationRepository);

NDto::TGetContextResponse GetCoordinationContext() const;

private:
NUseCase::TGetContextUseCase GetContextUseCase_;
};

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

} // namespace NCoordinator::NApp::NService
6 changes: 3 additions & 3 deletions src/app/services/leader/leader_service.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ namespace NCoordinator::NApp::NService {
////////////////////////////////////////////////////////////////////////////////

TLeaderService::TLeaderService(
NCore::NDomain::ICoordinationGateway& coordinationGateway_,
NCore::NDomain::ICoordinationRepository& coordinationRepository_,
NCore::NDomain::ICoordinationGateway& coordinationGateway,
NCore::NDomain::ICoordinationRepository& coordinationRepository,
NCore::NDomain::IHubGateway& hubGateway,
NCore::ILoadFactorPredictor& loadFactorPredictor)
: CoordinationUseCase_(coordinationGateway_, coordinationRepository_, hubGateway, loadFactorPredictor)
: CoordinationUseCase_(coordinationGateway, coordinationRepository, hubGateway, loadFactorPredictor)
{ }

void TLeaderService::Coordinate(const NDto::TCoordinationRequest& request) const
Expand Down
4 changes: 2 additions & 2 deletions src/app/services/leader/leader_service.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ class TLeaderService final
{
public:
TLeaderService(
NCore::NDomain::ICoordinationGateway& coordinationGateway_,
NCore::NDomain::ICoordinationRepository& coordinationRepository_,
NCore::NDomain::ICoordinationGateway& coordinationGateway,
NCore::NDomain::ICoordinationRepository& coordinationRepository,
NCore::NDomain::IHubGateway& hubGateway,
NCore::ILoadFactorPredictor& loadFactorPredictor);

Expand Down
37 changes: 37 additions & 0 deletions src/app/use_cases/admin/get_context/get_context.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "get_context.hpp"

#include <core/coordination/coordination_state.hpp>

#include <userver/logging/log.hpp>

#include <fmt/format.h>

namespace NCoordinator::NApp::NUseCase {

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

TGetContextUseCase::TGetContextUseCase(NCore::NDomain::ICoordinationRepository& coordinationRepository)
: CoordinationRepository_(coordinationRepository)
{ }

NDto::TGetContextResponse TGetContextUseCase::Execute() const
{

NCore::NDomain::TCoordinationContext context;

try {
context = CoordinationRepository_.GetCoordinationContext();
} catch (std::exception& ex) {
throw TGetContextTemporaryUnavailable(fmt::format("Failed to get coordination context: {}", ex.what()));
}

NDto::TGetContextResponse response{
.Context = std::move(context),
};

return response;
}

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

} // namespace NCoordinator::NApp::NUseCase
34 changes: 34 additions & 0 deletions src/app/use_cases/admin/get_context/get_context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#pragma once

#include <app/exceptions.hpp>
#include <app/dto/admin/get_context.hpp>
#include <core/coordination/coordination_gateway.hpp>
#include <core/coordination/coordination_repository.hpp>
#include <core/partition_balancing/partition_balancer.hpp>
#include <core/partition_balancing/load_factor_predictor.hpp>
#include <core/hub/hub_gateway.hpp>

namespace NCoordinator::NApp::NUseCase {

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

class TGetContextTemporaryUnavailable
: public TApplicationException
{
using TApplicationException::TApplicationException;
};

class TGetContextUseCase final
{
public:
TGetContextUseCase(NCore::NDomain::ICoordinationRepository& coordinationRepository);

NDto::TGetContextResponse Execute() const;

private:
NCore::NDomain::ICoordinationRepository& CoordinationRepository_;
};

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

} // namespace NCoordinator::NApp::NUseCase
8 changes: 4 additions & 4 deletions src/app/use_cases/leader/coordination/coordination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@ namespace NCoordinator::NApp::NUseCase {
////////////////////////////////////////////////////////////////////////////////

TCoordinationUseCase::TCoordinationUseCase(
NCore::NDomain::ICoordinationGateway& coordinationGateway_,
NCore::NDomain::ICoordinationRepository& coordinationRepository_,
NCore::NDomain::ICoordinationGateway& coordinationGateway,
NCore::NDomain::ICoordinationRepository& coordinationRepository,
NCore::NDomain::IHubGateway& hubGateway,
NCore::ILoadFactorPredictor& loadFactorPredictor)
: CoordinationGateway_(coordinationGateway_)
, CoordinationRepository_(coordinationRepository_)
: CoordinationGateway_(coordinationGateway)
, CoordinationRepository_(coordinationRepository)
, HubGateway_(hubGateway)
, Balancer_(loadFactorPredictor)
{ }
Expand Down
4 changes: 2 additions & 2 deletions src/app/use_cases/leader/coordination/coordination.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ class TCoordinationUseCase final
{
public:
TCoordinationUseCase(
NCore::NDomain::ICoordinationGateway& coordinationGateway_,
NCore::NDomain::ICoordinationRepository& coordinationRepository_,
NCore::NDomain::ICoordinationGateway& coordinationGateway,
NCore::NDomain::ICoordinationRepository& coordinationRepository,
NCore::NDomain::IHubGateway& hubGateway,
NCore::ILoadFactorPredictor& loadFactorPredictor);

Expand Down
Loading