-
Notifications
You must be signed in to change notification settings - Fork 285
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat userver: add grpc-reflection library
commit_hash:6914421d1e962c5b0aa163f59641fd4184a217ff
- Loading branch information
aserebriyskiy
committed
Nov 7, 2024
1 parent
2eaa5e4
commit 7f8c880
Showing
21 changed files
with
782 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
7 changes: 7 additions & 0 deletions
7
libraries/grpc-reflection/functional_tests/golden_path/CMakeLists.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
|
33 changes: 33 additions & 0 deletions
33
libraries/grpc-reflection/functional_tests/golden_path/grpc_reflection_service.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
56 changes: 56 additions & 0 deletions
56
libraries/grpc-reflection/functional_tests/golden_path/static_config.yaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
37 changes: 37 additions & 0 deletions
37
libraries/grpc-reflection/functional_tests/golden_path/tests/conftest.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
21 changes: 21 additions & 0 deletions
21
libraries/grpc-reflection/functional_tests/golden_path/tests/test_grpc_reflection.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
41 changes: 41 additions & 0 deletions
41
libraries/grpc-reflection/include/userver/grpc-reflection/reflection_service_component.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.