Skip to content

Commit

Permalink
feat userver: add grpc-reflection library
Browse files Browse the repository at this point in the history
commit_hash:6914421d1e962c5b0aa163f59641fd4184a217ff
  • Loading branch information
aserebriyskiy committed Nov 7, 2024
1 parent 2eaa5e4 commit 7f8c880
Show file tree
Hide file tree
Showing 21 changed files with 782 additions and 0 deletions.
16 changes: 16 additions & 0 deletions .mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@
"cmake/install/userver-clickhouse-config.cmake":"taxi/uservices/userver/cmake/install/userver-clickhouse-config.cmake",
"cmake/install/userver-core-config.cmake":"taxi/uservices/userver/cmake/install/userver-core-config.cmake",
"cmake/install/userver-grpc-config.cmake":"taxi/uservices/userver/cmake/install/userver-grpc-config.cmake",
"cmake/install/userver-grpc-reflection-config.cmake":"taxi/uservices/userver/cmake/install/userver-grpc-reflection-config.cmake",
"cmake/install/userver-kafka-config.cmake":"taxi/uservices/userver/cmake/install/userver-kafka-config.cmake",
"cmake/install/userver-mongo-config.cmake":"taxi/uservices/userver/cmake/install/userver-mongo-config.cmake",
"cmake/install/userver-mysql-config.cmake":"taxi/uservices/userver/cmake/install/userver-mysql-config.cmake",
Expand Down Expand Up @@ -2134,6 +2135,20 @@
"kafka/utest/src/kafka/utest/kafka_fixture.cpp":"taxi/uservices/userver/kafka/utest/src/kafka/utest/kafka_fixture.cpp",
"kafka/wrapper/include/librdkafka/rdkafka.h":"taxi/uservices/userver/kafka/wrapper/include/librdkafka/rdkafka.h",
"libraries/CMakeLists.txt":"taxi/uservices/userver/libraries/CMakeLists.txt",
"libraries/grpc-reflection/CMakeLists.txt":"taxi/uservices/userver/libraries/grpc-reflection/CMakeLists.txt",
"libraries/grpc-reflection/__module_deps__.yaml":"taxi/uservices/userver/libraries/grpc-reflection/__module_deps__.yaml",
"libraries/grpc-reflection/functional_tests/CMakeLists.txt":"taxi/uservices/userver/libraries/grpc-reflection/functional_tests/CMakeLists.txt",
"libraries/grpc-reflection/functional_tests/golden_path/CMakeLists.txt":"taxi/uservices/userver/libraries/grpc-reflection/functional_tests/golden_path/CMakeLists.txt",
"libraries/grpc-reflection/functional_tests/golden_path/grpc_reflection_service.cpp":"taxi/uservices/userver/libraries/grpc-reflection/functional_tests/golden_path/grpc_reflection_service.cpp",
"libraries/grpc-reflection/functional_tests/golden_path/static_config.yaml":"taxi/uservices/userver/libraries/grpc-reflection/functional_tests/golden_path/static_config.yaml",
"libraries/grpc-reflection/functional_tests/golden_path/tests/conftest.py":"taxi/uservices/userver/libraries/grpc-reflection/functional_tests/golden_path/tests/conftest.py",
"libraries/grpc-reflection/functional_tests/golden_path/tests/test_grpc_reflection.py":"taxi/uservices/userver/libraries/grpc-reflection/functional_tests/golden_path/tests/test_grpc_reflection.py",
"libraries/grpc-reflection/include/userver/grpc-reflection/reflection_service_component.hpp":"taxi/uservices/userver/libraries/grpc-reflection/include/userver/grpc-reflection/reflection_service_component.hpp",
"libraries/grpc-reflection/library.yaml":"taxi/uservices/userver/libraries/grpc-reflection/library.yaml",
"libraries/grpc-reflection/proto/reflection.proto":"taxi/uservices/userver/libraries/grpc-reflection/proto/reflection.proto",
"libraries/grpc-reflection/src/grpc-reflection/proto_server_reflection.cpp":"taxi/uservices/userver/libraries/grpc-reflection/src/grpc-reflection/proto_server_reflection.cpp",
"libraries/grpc-reflection/src/grpc-reflection/proto_server_reflection.hpp":"taxi/uservices/userver/libraries/grpc-reflection/src/grpc-reflection/proto_server_reflection.hpp",
"libraries/grpc-reflection/src/grpc-reflection/reflection_service_component.cpp":"taxi/uservices/userver/libraries/grpc-reflection/src/grpc-reflection/reflection_service_component.cpp",
"libraries/s3api/CMakeLists.txt":"taxi/uservices/userver/libraries/s3api/CMakeLists.txt",
"libraries/s3api/include/userver/s3api/authenticators/access_key.hpp":"taxi/uservices/userver/libraries/s3api/include/userver/s3api/authenticators/access_key.hpp",
"libraries/s3api/include/userver/s3api/authenticators/interface.hpp":"taxi/uservices/userver/libraries/s3api/include/userver/s3api/authenticators/interface.hpp",
Expand Down Expand Up @@ -3355,6 +3370,7 @@
"scripts/docs/en/userver/intro.md":"taxi/uservices/userver/scripts/docs/en/userver/intro.md",
"scripts/docs/en/userver/intro_io_bound_coro.md":"taxi/uservices/userver/scripts/docs/en/userver/intro_io_bound_coro.md",
"scripts/docs/en/userver/kafka.md":"taxi/uservices/userver/scripts/docs/en/userver/kafka.md",
"scripts/docs/en/userver/libraries/grpc-reflection.md":"taxi/uservices/userver/scripts/docs/en/userver/libraries/grpc-reflection.md",
"scripts/docs/en/userver/libraries/s3api.md":"taxi/uservices/userver/scripts/docs/en/userver/libraries/s3api.md",
"scripts/docs/en/userver/log_level_running_service.md":"taxi/uservices/userver/scripts/docs/en/userver/log_level_running_service.md",
"scripts/docs/en/userver/logging.md":"taxi/uservices/userver/scripts/docs/en/userver/logging.md",
Expand Down
13 changes: 13 additions & 0 deletions cmake/install/userver-grpc-reflection-config.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
include_guard(GLOBAL)

if(userver_grpc_reflection_FOUND)
return()
endif()

find_package(userver REQUIRED COMPONENTS
core grpc
)

set(userver_grpc_reflection_FOUND TRUE)


7 changes: 7 additions & 0 deletions libraries/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
option(USERVER_FEATURE_S3API "Build S3 api client library" "${USERVER_LIB_ENABLED_DEFAULT}")
option(USERVER_FEATURE_GRPC_REFLECTION "Build grpc reflection library" "${USERVER_LIB_ENABLED_DEFAULT}")

if (USERVER_FEATURE_S3API)
add_subdirectory(s3api)
endif()

if (USERVER_FEATURE_GRPC_REFLECTION)
if (NOT USERVER_FEATURE_GRPC)
message(FATAL_ERROR "'USERVER_FEATURE_GRPC_REFLECTION' requires 'USERVER_FEATURE_GRPC=ON'")
endif()
add_subdirectory(grpc-reflection)
endif()
15 changes: 15 additions & 0 deletions libraries/grpc-reflection/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
project(userver-grpc-reflection CXX)

userver_add_grpc_library(${PROJECT_NAME}-proto PROTOS reflection.proto)

_userver_install_targets(COMPONENT grpc-reflection TARGETS ${PROJECT_NAME}-proto)

userver_module(grpc-reflection
SOURCE_DIR "${CMAKE_CURRENT_SOURCE_DIR}"
LINK_LIBRARIES ${PROJECT_NAME}-proto userver::grpc
UTEST_SOURCES "${CMAKE_CURRENT_SOURCE_DIR}/src/*_test.cpp"
)

if(USERVER_IS_THE_ROOT_PROJECT)
add_subdirectory(functional_tests)
endif()
11 changes: 11 additions & 0 deletions libraries/grpc-reflection/__module_deps__.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
# THIS FILE IS AUTOGENERATED, DO NOT EDIT !!!
version: 1
includes:
- taxi/schemas/schemas/proto/grpc_reflection/__module_deps__.yaml
paths:
- taxi/schemas/schemas/configs/declarations/other
- taxi/schemas/schemas/configs/declarations/userver
- taxi/schemas/schemas/configs/declarations/userver-grpc
- taxi/uservices/libraries/grpc-reflection
- taxi/uservices/userver/core
- taxi/uservices/userver/grpc
6 changes: 6 additions & 0 deletions libraries/grpc-reflection/functional_tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
project(userver-grpc-reflection-tests CXX)

add_custom_target(${PROJECT_NAME})

add_subdirectory(golden_path)
add_dependencies(${PROJECT_NAME} ${PROJECT_NAME}-golden-path)
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
project(userver-grpc-reflection-tests-golden-path CXX)

add_executable(${PROJECT_NAME} "grpc_reflection_service.cpp")
target_link_libraries(${PROJECT_NAME} userver-core userver-grpc userver-grpc-reflection)

userver_chaos_testsuite_add()

Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#include <userver/utest/using_namespace_userver.hpp> // IWYU pragma: keep

#include <userver/clients/dns/component.hpp>
#include <userver/clients/http/component.hpp>
#include <userver/components/component.hpp>
#include <userver/components/minimal_server_component_list.hpp>
#include <userver/server/handlers/tests_control.hpp>
#include <userver/storages/secdist/component.hpp>
#include <userver/storages/secdist/provider_component.hpp>
#include <userver/testsuite/testsuite_support.hpp>
#include <userver/ugrpc/server/health/component.hpp>
#include <userver/ugrpc/server/server_component.hpp>
#include <userver/utils/daemon_run.hpp>

#include <iostream>
#include <string>
#include <string_view>

#include <fmt/format.h>

#include <userver/grpc-reflection/reflection_service_component.hpp>

int main(int argc, char* argv[]) {
const auto component_list = components::MinimalServerComponentList()
.Append<grpc_reflection::ReflectionServiceComponent>()
.Append<components::TestsuiteSupport>()
.Append<server::handlers::TestsControl>()
.Append<ugrpc::server::HealthComponent>()
.Append<components::HttpClient>()
.Append<ugrpc::server::ServerComponent>()
.Append<clients::dns::Component>();
return utils::DaemonMain(argc, argv, component_list);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
components_manager:
task_processors:
main-task-processor:
worker_threads: 8
fs-task-processor:
worker_threads: 4
grpc-blocking-task-processor: # For blocking gRPC channel creation
worker_threads: 2
thread_name: grpc-worker
default_task_processor: main-task-processor

components:
grpc-reflection-service:
grpc-health:

testsuite-support:

# Common configuration for gRPC server
grpc-server:
# The single listening unix socket for incoming RPCs
unix-socket-path: '/tmp/my_socket'
completion-queue-count: 3
service-defaults:
task-processor: main-task-processor
middlewares: []



http-client:
fs-task-processor: main-task-processor

tests-control:
method: POST
path: /tests/{action}
skip-unregistered-testpoints: true
task_processor: main-task-processor
testpoint-timeout: 10s
testpoint-url: $mockserver/testpoint
throttling_enabled: false

logging:
fs-task-processor: fs-task-processor
loggers:
default:
file_path: "@stderr"
level: debug
overflow_behavior: discard

dns-client:
fs-task-processor: fs-task-processor

server:
listener:
port: 8187
task_processor: main-task-processor

Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import pathlib
import tempfile

from grpc_reflection.v1alpha.proto_reflection_descriptor_database import (
ServerReflectionStub,
)
import pytest

pytest_plugins = ['pytest_userver.plugins.grpc']

USERVER_CONFIG_HOOKS = ['prepare_service_config']


@pytest.fixture(scope='session')
def unix_socket_path(tmp_path_factory) -> pathlib.Path:
with tempfile.TemporaryDirectory(prefix='userver-grpc-socket-') as name:
yield pathlib.Path(name) / 's'


@pytest.fixture(scope='session')
def grpc_service_endpoint(service_config) -> str:
components = service_config['components_manager']['components']
return f'unix:{components["grpc-server"]["unix-socket-path"]}'


@pytest.fixture(name='prepare_service_config', scope='session')
def _prepare_service_config(unix_socket_path):
def patch_config(config, config_vars):
components = config['components_manager']['components']
components['grpc-server']['unix-socket-path'] = str(unix_socket_path)

return patch_config


@pytest.fixture
def grpc_reflection_client(grpc_channel, service_client):
return ServerReflectionStub(grpc_channel)
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import logging

from grpc_reflection.v1alpha.proto_reflection_descriptor_database import (
ServerReflectionRequest,
)

logger = logging.getLogger(__name__)


async def test_happy_path(grpc_reflection_client):
request = ServerReflectionRequest(list_services='')
response = grpc_reflection_client.ServerReflectionInfo(iter([request]))
res = await response.read()
list_services = res.list_services_response
services = list_services.service
result = set([service.name for service in services])
reference = set([
'grpc.reflection.v1alpha.ServerReflection',
'grpc.health.v1.Health',
])
assert result == reference
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#pragma once

#include <string_view>

#include <userver/components/component_config.hpp>
#include <userver/components/component_context.hpp>
#include <userver/ugrpc/server/server.hpp>
#include <userver/ugrpc/server/service_component_base.hpp>

USERVER_NAMESPACE_BEGIN

/// @brief Top namespace for grpc-reflection library
///
/// For more information see @ref grpc-reflection_library.
namespace grpc_reflection {

class ProtoServerReflection;

class ReflectionServiceComponent final : public ugrpc::server::ServiceComponentBase {
public:
static constexpr std::string_view kName = "grpc-reflection-service";

ReflectionServiceComponent(const components::ComponentConfig& config, const components::ComponentContext& context);

~ReflectionServiceComponent();

private:
void OnAllComponentsLoaded() override;

components::ComponentHealth GetComponentHealth() const override;

void AddService(std::string_view service_name);

std::unique_ptr<grpc_reflection::ProtoServerReflection> service_;
ugrpc::server::Server& ugrpc_server_;
std::atomic<bool> ready_{false};
};

} // namespace grpc_reflection

USERVER_NAMESPACE_END
10 changes: 10 additions & 0 deletions libraries/grpc-reflection/library.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
project-name: userver-grpc-reflection
project-alt-names:
- yandex-userver-grpc-reflection
description: library with reflection implementation

maintainers:
- Common componets

libraries:
- userver-core
Loading

0 comments on commit 7f8c880

Please sign in to comment.