Skip to content

Commit

Permalink
feat grpc: grpc service usability, drop deprecated
Browse files Browse the repository at this point in the history
commit_hash:d3951b0344dbc1b1b6212d2794ea41aa24875ddc
  • Loading branch information
kpavlov00 committed Nov 13, 2024
1 parent e5a5991 commit 1d1ac72
Show file tree
Hide file tree
Showing 10 changed files with 59 additions and 133 deletions.
1 change: 0 additions & 1 deletion .mapping.json
Original file line number Diff line number Diff line change
Expand Up @@ -1919,7 +1919,6 @@
"grpc/include/userver/ugrpc/server/impl/completion_queue_pool.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/server/impl/completion_queue_pool.hpp",
"grpc/include/userver/ugrpc/server/impl/error_code.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/server/impl/error_code.hpp",
"grpc/include/userver/ugrpc/server/impl/exceptions.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/server/impl/exceptions.hpp",
"grpc/include/userver/ugrpc/server/impl/method_dispatch.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/server/impl/method_dispatch.hpp",
"grpc/include/userver/ugrpc/server/impl/service_worker.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/server/impl/service_worker.hpp",
"grpc/include/userver/ugrpc/server/impl/service_worker_impl.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/server/impl/service_worker_impl.hpp",
"grpc/include/userver/ugrpc/server/metadata_utils.hpp":"taxi/uservices/userver/grpc/include/userver/ugrpc/server/metadata_utils.hpp",
Expand Down
20 changes: 2 additions & 18 deletions grpc/include/userver/ugrpc/server/generic_service_base.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@
#include <userver/ugrpc/server/service_component_base.hpp>
#include <userver/ugrpc/server/stream.hpp>

// use by legacy
#include <userver/ugrpc/server/rpc.hpp>

USERVER_NAMESPACE_BEGIN

namespace ugrpc::server {
Expand Down Expand Up @@ -76,21 +73,8 @@ class GenericServiceBase {
virtual ~GenericServiceBase();

/// @brief Override this method in the derived class to handle all RPCs.
/// RPC name can be obtained through @ref CallAnyBase::GetCallName.
/// @note RPCs of all kinds (including unary RPCs) are represented using
/// BidirectionalStream API.
/// @note The implementation of the method should call `Finish` or
/// `FinishWithError`, otherwise the server will respond with an "internal
/// server error" status.
virtual GenericResult Handle(GenericCallContext& context, GenericReaderWriter& stream);

// Legacy
using Call = BidirectionalStream<grpc::ByteBuffer, grpc::ByteBuffer>;
[[deprecated(
"Use 'grpc::Status Handle(GenericCallContext&, "
"GenericReaderWriter&)'"
)]] virtual void
Handle(Call& call);
/// RPC name can be obtained through @ref GenericCallContext::GetCallName.
virtual GenericResult Handle(GenericCallContext& context, GenericReaderWriter& stream) = 0;

protected:
GenericServiceBase() = default;
Expand Down
29 changes: 21 additions & 8 deletions grpc/include/userver/ugrpc/server/impl/call_traits.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#pragma once

#include <userver/ugrpc/server/call_context.hpp>
#include <userver/ugrpc/server/result.hpp>
#include <userver/ugrpc/server/rpc.hpp>
#include <userver/ugrpc/server/stream.hpp>

USERVER_NAMESPACE_BEGIN

Expand All @@ -20,54 +23,64 @@ template <typename HandlerMethod>
struct CallTraits;

template <typename ServiceBaseType, typename RequestType, typename ResponseType>
struct CallTraits<void (ServiceBaseType::*)(UnaryCall<ResponseType>&, RequestType&&)> final {
struct CallTraits<ugrpc::server::Result<ResponseType> (ServiceBaseType::*)(ugrpc::server::CallContext&, RequestType&&)>
final {
using ServiceBase = ServiceBaseType;
using Request = RequestType;
using Response = ResponseType;
using RawCall = impl::RawResponseWriter<ResponseType>;
using InitialRequest = Request;
using Call = UnaryCall<Response>;
using ContextType = ::grpc::ServerContext;
using ServiceMethod = void (ServiceBase::*)(Call&, Request&&);
using ServiceMethod = ugrpc::server::Result<Response> (ServiceBase::*)(ugrpc::server::CallContext&, Request&&);
static constexpr auto kCallCategory = CallCategory::kUnary;
};

template <typename ServiceBaseType, typename RequestType, typename ResponseType>
struct CallTraits<void (ServiceBaseType::*)(InputStream<RequestType, ResponseType>&)> final {
struct CallTraits<ugrpc::server::Result<
ResponseType> (ServiceBaseType::*)(ugrpc::server::CallContext&, ugrpc::server::Reader<RequestType>&)>
final {
using ServiceBase = ServiceBaseType;
using Request = RequestType;
using Response = ResponseType;
using RawCall = impl::RawReader<Request, Response>;
using InitialRequest = NoInitialRequest;
using Call = InputStream<Request, Response>;
using ContextType = ::grpc::ServerContext;
using ServiceMethod = void (ServiceBase::*)(Call&);
using ServiceMethod =
ugrpc::server::Result<Response> (ServiceBase::*)(ugrpc::server::CallContext&, ugrpc::server::Reader<Request>&);
static constexpr auto kCallCategory = CallCategory::kInputStream;
};

template <typename ServiceBaseType, typename RequestType, typename ResponseType>
struct CallTraits<void (ServiceBaseType::*)(OutputStream<ResponseType>&, RequestType&&)> final {
struct CallTraits<ugrpc::server::StreamingResult<
ResponseType> (ServiceBaseType::*)(ugrpc::server::CallContext&, RequestType&&, ugrpc::server::Writer<ResponseType>&)>
final {
using ServiceBase = ServiceBaseType;
using Request = RequestType;
using Response = ResponseType;
using RawCall = impl::RawWriter<Response>;
using InitialRequest = Request;
using Call = OutputStream<Response>;
using ContextType = ::grpc::ServerContext;
using ServiceMethod = void (ServiceBase::*)(Call&, Request&&);
using ServiceMethod = ugrpc::server::StreamingResult<
Response> (ServiceBase::*)(ugrpc::server::CallContext&, Request&&, ugrpc::server::Writer<Response>&);
static constexpr auto kCallCategory = CallCategory::kOutputStream;
};

template <typename ServiceBaseType, typename RequestType, typename ResponseType>
struct CallTraits<void (ServiceBaseType::*)(BidirectionalStream<RequestType, ResponseType>&)> final {
struct CallTraits<ugrpc::server::StreamingResult<
ResponseType> (ServiceBaseType::*)(ugrpc::server::CallContext&, ugrpc::server::ReaderWriter<RequestType, ResponseType>&)>
final {
using ServiceBase = ServiceBaseType;
using Request = RequestType;
using Response = ResponseType;
using RawCall = impl::RawReaderWriter<Request, Response>;
using InitialRequest = NoInitialRequest;
using Call = BidirectionalStream<Request, Response>;
using ContextType = ::grpc::ServerContext;
using ServiceMethod = void (ServiceBase::*)(Call&);
using ServiceMethod = ugrpc::server::StreamingResult<
Response> (ServiceBase::*)(ugrpc::server::CallContext&, ugrpc::server::ReaderWriter<Request, Response>&);
static constexpr auto kCallCategory = CallCategory::kBidirectionalStream;
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@

#include <userver/ugrpc/impl/protobuf_collector.hpp>
#include <userver/ugrpc/server/impl/call_utils.hpp>
#include <userver/ugrpc/server/impl/method_dispatch.hpp>
#include <userver/ugrpc/server/impl/service_worker_impl.hpp>
31 changes: 0 additions & 31 deletions grpc/include/userver/ugrpc/server/impl/method_dispatch.hpp

This file was deleted.

30 changes: 27 additions & 3 deletions grpc/include/userver/ugrpc/server/impl/service_worker_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,12 @@
#include <userver/ugrpc/impl/static_metadata.hpp>
#include <userver/ugrpc/impl/statistics.hpp>
#include <userver/ugrpc/impl/statistics_scope.hpp>
#include <userver/ugrpc/server/call_context.hpp>
#include <userver/ugrpc/server/impl/async_method_invocation.hpp>
#include <userver/ugrpc/server/impl/async_service.hpp>
#include <userver/ugrpc/server/impl/call_params.hpp>
#include <userver/ugrpc/server/impl/call_traits.hpp>
#include <userver/ugrpc/server/impl/call_utils.hpp>
#include <userver/ugrpc/server/impl/error_code.hpp>
#include <userver/ugrpc/server/impl/exceptions.hpp>
#include <userver/ugrpc/server/impl/service_worker.hpp>
Expand Down Expand Up @@ -185,6 +187,7 @@ class CallData final {
auto& access_tskv_logger = method_data_.service_data.settings.access_tskv_logger;
auto& statistics_storage = method_data_.service_data.settings.statistics_storage;
utils::AnyStorage<StorageContext> storage_context;

Call responder(
CallParams{
context_,
Expand All @@ -199,11 +202,32 @@ class CallData final {
middlewares},
raw_responder_
);

auto do_call = [&] {
if constexpr (std::is_same_v<InitialRequest, NoInitialRequest>) {
(method_data_.service.*(method_data_.service_method))(responder);
if constexpr (CallTraits::kCallCategory == CallCategory::kGeneric) {
GenericCallContext context{responder};
auto result = (method_data_.service.*(method_data_.service_method))(context, responder);
Finalize(responder, std::move(result));
} else {
(method_data_.service.*(method_data_.service_method))(responder, std::move(initial_request_));
CallContext context{responder};
if constexpr (CallTraits::kCallCategory == CallCategory::kUnary) {
auto result =
(method_data_.service.*(method_data_.service_method))(context, std::move(initial_request_));
Finalize(responder, std::move(result));
} else if constexpr (CallTraits::kCallCategory == CallCategory::kInputStream) {
auto result = (method_data_.service.*(method_data_.service_method))(context, responder);
Finalize(responder, std::move(result));
} else if constexpr (CallTraits::kCallCategory == CallCategory::kOutputStream) {
auto result =
(method_data_.service.*(method_data_.service_method)
)(context, std::move(initial_request_), responder);
Finalize(responder, std::move(result));
} else if constexpr (CallTraits::kCallCategory == CallCategory::kBidirectionalStream) {
auto result = (method_data_.service.*(method_data_.service_method))(context, responder);
Finalize(responder, std::move(result));
} else {
static_assert(!sizeof(CallTraits), "Unexpected CallCategory");
}
}
};

Expand Down
16 changes: 0 additions & 16 deletions grpc/src/ugrpc/server/generic_service_base.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,6 @@ namespace ugrpc::server {

GenericServiceBase::~GenericServiceBase() = default;

GenericServiceBase::GenericResult
GenericServiceBase::Handle(GenericCallContext& /*context*/, GenericReaderWriter& /*stream*/) {
UASSERT_MSG(
false,
"Called not implemented GenericServiceBase/Handle(GenericCallContext&, "
"GenericReaderWriter&)"
);
return USERVER_NAMESPACE::ugrpc::server::impl::kUnimplementedStatus;
}

void GenericServiceBase::Handle(Call& call) {
GenericCallContext context{call};
auto result = Handle(context, call);
impl::Finalize(call, std::move(result));
}

} // namespace ugrpc::server

USERVER_NAMESPACE_END
13 changes: 8 additions & 5 deletions grpc/src/ugrpc/server/impl/generic_service_worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,20 +18,23 @@ constexpr std::string_view kGenericMethodFullNamesFake[] = {
constexpr std::string_view kGenericServiceNameFake = "Generic";
constexpr ugrpc::impl::StaticServiceMetadata kGenericMetadataFake{kGenericServiceNameFake, kGenericMethodFullNamesFake};

auto Dispatch(void (GenericServiceBase::*service_method)(GenericServiceBase::Call&)) { return service_method; }

} // namespace

template <>
struct CallTraits<void (GenericServiceBase::*)(GenericServiceBase::Call&)> final {
struct CallTraits<ugrpc::server::StreamingResult<
grpc::
ByteBuffer> (GenericServiceBase::*)(ugrpc::server::GenericCallContext&, ugrpc::server::ReaderWriter<grpc::ByteBuffer, grpc::ByteBuffer>&)>
final {
using ServiceBase = GenericServiceBase;
using Request = grpc::ByteBuffer;
using Response = grpc::ByteBuffer;
using RawCall = impl::RawReaderWriter<Request, Response>;
using InitialRequest = NoInitialRequest;
using Call = BidirectionalStream<Request, Response>;
using ContextType = grpc::GenericServerContext;
using ServiceMethod = void (ServiceBase::*)(Call&);
using ServiceMethod = ugrpc::server::StreamingResult<
grpc::
ByteBuffer> (ServiceBase::*)(ugrpc::server::GenericCallContext&, ugrpc::server::ReaderWriter<Request, Response>&);
static constexpr auto kCallCategory = CallCategory::kGeneric;
};

Expand Down Expand Up @@ -83,7 +86,7 @@ GenericServiceWorker::~GenericServiceWorker() = default;
grpc::AsyncGenericService& GenericServiceWorker::GetService() { return impl_->service_data.async_service.GetService(); }

void GenericServiceWorker::Start() {
impl::StartServing(impl_->service_data, impl_->service, Dispatch(&GenericServiceBase::Handle));
impl::StartServing(impl_->service_data, impl_->service, &GenericServiceBase::Handle);
}

} // namespace ugrpc::server::impl
Expand Down
33 changes: 1 addition & 32 deletions scripts/grpc/templates/service.usrv.cpp.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,6 @@ inline const bool k{{service.name}}TypesRegistration =
return USERVER_NAMESPACE::ugrpc::server::impl::kUnimplementedStatus;
}

// Legacy
void {{service.name}}Base::{{method.name}}({{method.name}}Call& call) {
CallContext context{call};
auto result = {{method.name}}(context, call);
USERVER_NAMESPACE::ugrpc::server::impl::Finalize(call, std::move(result));
}

{% elif method.client_streaming %}

{{service.name}}Base::{{method.name}}Result {{service.name}}Base::{{method.name}}(
Expand All @@ -55,13 +48,6 @@ void {{service.name}}Base::{{method.name}}({{method.name}}Call& call) {
return USERVER_NAMESPACE::ugrpc::server::impl::kUnimplementedStatus;
}

// Legacy
void {{service.name}}Base::{{method.name}}({{method.name}}Call& call) {
CallContext context{call};
auto result = {{method.name}}(context, call);
USERVER_NAMESPACE::ugrpc::server::impl::Finalize(call, std::move(result));
}

{% elif method.server_streaming %}

{{service.name}}Base::{{method.name}}Result {{service.name}}Base::{{method.name}}(
Expand All @@ -72,14 +58,6 @@ void {{service.name}}Base::{{method.name}}({{method.name}}Call& call) {
return USERVER_NAMESPACE::ugrpc::server::impl::kUnimplementedStatus;
}

// Legacy
void {{service.name}}Base::{{method.name}}({{method.name}}Call& call,
{{ method.input_type | grpc_to_cpp_name }}&& request) {
CallContext context{call};
auto result = {{method.name}}(context, std::move(request), call);
USERVER_NAMESPACE::ugrpc::server::impl::Finalize(call, std::move(result));
}

{% else %}

{{service.name}}Base::{{method.name}}Result {{service.name}}Base::{{method.name}}(
Expand All @@ -89,14 +67,6 @@ void {{service.name}}Base::{{method.name}}({{method.name}}Call& call,
return USERVER_NAMESPACE::ugrpc::server::impl::kUnimplementedStatus;
}

// Legacy
void {{service.name}}Base::{{method.name}}({{method.name}}Call& call,
{{ method.input_type | grpc_to_cpp_name }}&& request) {
CallContext context{call};
auto result = {{method.name}}(context, std::move(request));
USERVER_NAMESPACE::ugrpc::server::impl::Finalize(call, std::move(result));
}

{% endif %}
{% endfor %}

Expand All @@ -109,8 +79,7 @@ std::unique_ptr<USERVER_NAMESPACE::ugrpc::server::impl::ServiceWorker>
{{utils.namespace_with_colons(proto.namespace)}}::{{service.name}}>(
std::move(settings), k{{service.name}}MethodNames, *this,
{% for method in service.method %}
USERVER_NAMESPACE::ugrpc::server::impl::Dispatch(
&{{service.name}}Base::{{method.name}}){{ "," if not loop.last else ");" }}
&{{service.name}}Base::{{method.name}}{{ "," if not loop.last else ");" }}
{% endfor %}
}

Expand Down
18 changes: 0 additions & 18 deletions scripts/grpc/templates/service.usrv.hpp.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,6 @@ class {{service.name}}Base
using {{method.name}}Result = USERVER_NAMESPACE::ugrpc::server::StreamingResult<
{{ method.output_type | grpc_to_cpp_name }}>;
virtual {{method.name}}Result {{method.name}}(CallContext& context, {{method.name}}ReaderWriter& stream);
// Legacy
using {{method.name}}Call = USERVER_NAMESPACE::ugrpc::server::BidirectionalStream<
{{ method.input_type | grpc_to_cpp_name }}, {{ method.output_type | grpc_to_cpp_name }}>;
[[deprecated("Use '{{method.name}}Result {{method.name}}(CallContext& context, {{method.name}}ReaderWriter& stream)'")]]
virtual void {{method.name}}({{method.name}}Call& call);

{% elif method.client_streaming %}

Expand All @@ -39,11 +34,6 @@ class {{service.name}}Base
using {{method.name}}Result = USERVER_NAMESPACE::ugrpc::server::Result<
{{ method.output_type | grpc_to_cpp_name }}>;
virtual {{method.name}}Result {{method.name}}(CallContext& context, {{method.name}}Reader& reader);
// Legacy
using {{method.name}}Call = USERVER_NAMESPACE::ugrpc::server::InputStream<
{{ method.input_type | grpc_to_cpp_name }}, {{ method.output_type | grpc_to_cpp_name }}>;
[[deprecated("Use '{{method.name}}Result {{method.name}}(CallContext& context, {{method.name}}Reader& reader)'")]]
virtual void {{method.name}}({{method.name}}Call& call);

{% elif method.server_streaming %}

Expand All @@ -54,20 +44,12 @@ class {{service.name}}Base
virtual {{method.name}}Result {{method.name}}(CallContext& context,
{{ method.input_type | grpc_to_cpp_name }}&& request,
{{method.name}}Writer& writer);
// Legacy
using {{method.name}}Call = USERVER_NAMESPACE::ugrpc::server::OutputStream<{{ method.output_type | grpc_to_cpp_name }}>;
[[deprecated("Use '{{method.name}}Result {{method.name}}(CallContext& context, {{ method.input_type | grpc_to_cpp_name }}&& request, {{method.name}}Writer& writer)'")]]
virtual void {{method.name}}({{method.name}}Call& call, {{ method.input_type | grpc_to_cpp_name }}&& request);

{% else %}

using {{method.name}}Result = USERVER_NAMESPACE::ugrpc::server::Result<
{{ method.output_type | grpc_to_cpp_name }}>;
virtual {{method.name}}Result {{method.name}}(CallContext& context, {{ method.input_type | grpc_to_cpp_name }}&& request);
// Legacy
using {{method.name}}Call = USERVER_NAMESPACE::ugrpc::server::UnaryCall<{{ method.output_type | grpc_to_cpp_name }}>;
[[deprecated("Use '{{method.name}}Result {{method.name}}(CallContext& context, {{ method.input_type | grpc_to_cpp_name }}&& request)'")]]
virtual void {{method.name}}({{method.name}}Call& call, {{ method.input_type | grpc_to_cpp_name }}&& request);

{% endif %}
{% endfor %}
Expand Down

0 comments on commit 1d1ac72

Please sign in to comment.