diff options
author | ilnaz <ilnaz@ydb.tech> | 2022-10-06 11:56:36 +0300 |
---|---|---|
committer | ilnaz <ilnaz@ydb.tech> | 2022-10-06 11:56:36 +0300 |
commit | a190eb72f986f7d864949720a2d6d26b723da8ad (patch) | |
tree | bfbc16811daa8f5429bda7091e3780fb7c28af1d | |
parent | 4331deb577ea9068218fdd5c88acf3e5023e73dd (diff) | |
download | ydb-a190eb72f986f7d864949720a2d6d26b723da8ad.tar.gz |
(refactoring) Little style guide changes
-rw-r--r-- | ydb/core/grpc_services/base/base.h | 161 | ||||
-rw-r--r-- | ydb/core/grpc_services/rpc_calls.h | 1 |
2 files changed, 89 insertions, 73 deletions
diff --git a/ydb/core/grpc_services/base/base.h b/ydb/core/grpc_services/base/base.h index 75deed4da9f..5a14c718c48 100644 --- a/ydb/core/grpc_services/base/base.h +++ b/ydb/core/grpc_services/base/base.h @@ -22,22 +22,20 @@ #include <ydb/core/base/events.h> namespace NKikimrConfig { -class TAppConfig; + class TAppConfig; } namespace NKikimr { namespace NSchemeCache { -struct TSchemeCacheNavigate; + struct TSchemeCacheNavigate; } namespace NRpcService { - -struct TRlPath { - TString CoordinationNode; - TString ResourcePath; -}; - + struct TRlPath { + TString CoordinationNode; + TString ResourcePath; + }; } namespace NGRpcService { @@ -235,7 +233,7 @@ void FillYdbStatus(T& resp, const NYql::TIssues& issues, Ydb::StatusIds::StatusC class TProtoResponseHelper { public: - template<typename T, typename C> + template <typename T, typename C> static void SendProtoResponse(const T& r, Ydb::StatusIds::StatusCode status, C& ctx) { T* resp = google::protobuf::Arena::CreateMessage<T>(ctx->GetArena()); resp->CopyFrom(r); @@ -278,6 +276,7 @@ public: using TPtr = TIntrusivePtr<TRespHookCtx>; using TContextPtr = TIntrusivePtr<NGrpc::IRequestContextBase>; using TMessage = NProtoBuf::Message; + TRespHookCtx(TContextPtr ctx, TMessage* data, const TString& requestName, ui64 ru, ui32 status) : Ctx_(ctx) , RespData_(data) @@ -337,6 +336,8 @@ struct TRequestAuxSettings { class IRequestProxyCtx : public virtual IRequestCtxBase { public: virtual ~IRequestProxyCtx() = default; + + // auth virtual const TMaybe<TString> GetYdbToken() const = 0; virtual void UpdateAuthState(NGrpc::TAuthState::EAuthState state) = 0; virtual void SetInternalToken(const TString& token) = 0; @@ -344,9 +345,14 @@ public: virtual void ReplyUnauthenticated(const TString& msg = "") = 0; virtual void ReplyUnavaliable() = 0; + // validation virtual bool Validate(TString& error) = 0; + + // counters virtual void UseDatabase(const TString& database) = 0; + // rate limiting + // This method allows to set hook for unary call. // The hook will be called at the reply time // TRespHookCtx::Ptr will be passed in to the hook, it is allow @@ -365,6 +371,7 @@ public: // The interface is used for rpc_ request actors class IRequestCtx : public virtual IRequestCtxBase { friend class TProtoResponseHelper; + public: virtual void SetClientLostAction(std::function<void()>&& cb) = 0; virtual const google::protobuf::Message* GetRequest() const = 0; @@ -396,7 +403,6 @@ public: }; class IRequestNoOpCtx : public IRequestCtx { - }; struct TCommonResponseFillerImpl { @@ -428,7 +434,8 @@ struct TCommonResponseFiller<TResp, false> : private TCommonResponseFillerImpl { class TRefreshTokenImpl : public IRequestProxyCtx - , public TEventLocal<TRefreshTokenImpl, TRpcServices::EvRefreshTokenRequest> { + , public TEventLocal<TRefreshTokenImpl, TRpcServices::EvRefreshTokenRequest> +{ public: TRefreshTokenImpl(const TString& token, const TString& database, TActorId from) : Token_(token) @@ -469,7 +476,8 @@ public: return {}; } - void SetRlPath(TMaybe<NRpcService::TRlPath>&&) override {} + void SetRlPath(TMaybe<NRpcService::TRlPath>&&) override { + } TMaybe<NRpcService::TRlPath> GetRlPath() const override { return Nothing(); @@ -568,15 +576,17 @@ inline TMaybe<TString> ToMaybe(const TVector<TStringBuf>& vec) { return TString{vec[0]}; } -template<ui32 TRpcId, typename TReq, typename TResp, TRateLimiterMode RlMode = TRateLimiterMode::Off> -class TGRpcRequestBiStreamWrapper : - public IRequestProxyCtx, - public TEventLocal<TGRpcRequestBiStreamWrapper<TRpcId, TReq, TResp, RlMode>, TRpcId> { +template <ui32 TRpcId, typename TReq, typename TResp, TRateLimiterMode RlMode = TRateLimiterMode::Off> +class TGRpcRequestBiStreamWrapper + : public IRequestProxyCtx + , public TEventLocal<TGRpcRequestBiStreamWrapper<TRpcId, TReq, TResp, RlMode>, TRpcId> +{ public: using TRequest = TReq; using TResponse = TResp; using IStreamCtx = NGRpcServer::IGRpcStreamingContext<TRequest, TResponse>; static constexpr TRateLimiterMode RateLimitMode = RlMode; + TGRpcRequestBiStreamWrapper(TIntrusivePtr<IStreamCtx> ctx, bool rlAllowed = true) : Ctx_(ctx) , RlAllowed_(rlAllowed) @@ -600,7 +610,7 @@ public: bool HasClientCapability(const TString& capability) const override { const auto& values = Ctx_->GetPeerMetaValues(NYdb::YDB_CLIENT_CAPABILITIES); - for(auto& value: values) { + for (const auto& value : values) { if (value == capability) return true; } @@ -632,7 +642,7 @@ public: void ReplyUnauthenticated(const TString& in) override { TStringBuilder builder; builder << (in.empty() ? TString("unauthenticated") : TString("unauthenticated, ")) << in; - for (const auto& issue: IssueManager_.GetIssues()) { + for (const auto& issue : IssueManager_.GetIssues()) { builder << " " << issue.Message; } Ctx_->Finish(grpc::Status(grpc::StatusCode::UNAUTHENTICATED, builder)); @@ -807,7 +817,9 @@ public: } private: - TDerived* Derived() noexcept { return static_cast<TDerived*>(this); } + TDerived* Derived() noexcept { + return static_cast<TDerived*>(this); + } }; class TEvProxyRuntimeEvent @@ -824,7 +836,7 @@ public: } }; -template<ui32 TRpcId, typename TDerived> +template <ui32 TRpcId, typename TDerived> class TEvProxyLegacyEvent : public IRequestProxyCtx , public TEventLocal<TDerived, TRpcId> @@ -839,17 +851,18 @@ public: } }; -template<ui32 TRpcId, typename TReq, typename TResp, bool IsOperation, typename TDerived> -class TGRpcRequestWrapperImpl : - public std::conditional_t<IsOperation, +template <ui32 TRpcId, typename TReq, typename TResp, bool IsOperation, typename TDerived> +class TGRpcRequestWrapperImpl + : public std::conditional_t<IsOperation, TGrpcResponseSenderImpl<TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, TDerived>>, - IRequestNoOpCtx>, - public std::conditional_t<TRpcId == TRpcServices::EvGrpcRuntimeRequest, + IRequestNoOpCtx> + , public std::conditional_t<TRpcId == TRpcServices::EvGrpcRuntimeRequest, TEvProxyRuntimeEvent, TEvProxyLegacyEvent<TRpcId, TDerived>> { friend class TProtoResponseHelper; friend class TGrpcResponseSenderImpl<TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, TDerived>>; + public: using TRequest = TReq; using TResponse = TResp; @@ -868,7 +881,7 @@ public: bool HasClientCapability(const TString& capability) const override { const auto& values = Ctx_->GetPeerMetaValues(NYdb::YDB_CLIENT_CAPABILITIES); - for(auto& value: values) { + for (const auto& value : values) { if (capability == value) return true; } @@ -900,7 +913,7 @@ public: void ReplyUnauthenticated(const TString& in) override { TStringBuilder builder; builder << in; - for (const auto& issue: IssueManager.GetIssues()) { + for (const auto& issue : IssueManager.GetIssues()) { builder << " " << issue.Message; } Ctx_->ReplyUnauthenticated(builder); @@ -966,7 +979,7 @@ public: return Ctx_->SslServer(); } - template<typename T> + template <typename T> static const TRequest* GetProtoRequest(const T& req) { auto request = dynamic_cast<const TRequest*>(req->GetRequest()); Y_VERIFY(request != nullptr, "Wrong using of TGRpcRequestWrapper"); @@ -1028,7 +1041,7 @@ public: //! The memory will be freed automaticaly after destroying //! corresponding request. //! Do not call delete for objects allocated here! - template<typename TResult, typename T> + template <typename TResult, typename T> static TResult* AllocateResult(T& ctx) { return google::protobuf::Arena::CreateMessage<TResult>(ctx->GetArena()); } @@ -1038,7 +1051,7 @@ public: } void SetClientLostAction(std::function<void()>&& cb) override { - auto shutdown = [cb{move(cb)}](const NGrpc::IRequestContextBase::TAsyncFinishResult& future) mutable { + auto shutdown = [cb = std::move(cb)](const NGrpc::IRequestContextBase::TAsyncFinishResult& future) mutable { Y_ASSERT(future.HasValue()); if (future.GetValue() == NGrpc::IRequestContextBase::EFinishStatus::CANCEL) { cb(); @@ -1092,11 +1105,11 @@ private: return Ctx_->Reply(resp, status); } -private: TResponse* CreateResponseMessage() { return google::protobuf::Arena::CreateMessage<TResponse>(Ctx_->GetArena()); } +private: TIntrusivePtr<NGrpc::IRequestContextBase> Ctx_; TString InternalToken_; NYql::TIssueManager IssueManager; @@ -1107,12 +1120,11 @@ private: TMaybe<NRpcService::TRlPath> RlPath; }; -template<ui32 TRpcId, typename TReq, typename TResp, bool IsOperation, typename TDerived> -class TGRpcRequestValidationWrapperImpl : - public TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, TDerived> { - +template <ui32 TRpcId, typename TReq, typename TResp, bool IsOperation, typename TDerived> +class TGRpcRequestValidationWrapperImpl : public TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, TDerived> { public: static IActor* CreateRpcActor(typename std::conditional<IsOperation, IRequestOpCtx, IRequestNoOpCtx>::type* msg); + TGRpcRequestValidationWrapperImpl(NGrpc::IRequestContextBase* ctx) : TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, TDerived>(ctx) { } @@ -1124,14 +1136,15 @@ public: // SFINAE // Check protobuf has validate feature -template<typename TProto> +template <typename TProto> struct TProtoHasValidate { private: static int Detect(...); // validate function has prototype: bool validate(TProtoStringType&) const static TProtoStringType Dummy_; - template<typename U> + template <typename U> static decltype(std::declval<U>().validate(Dummy_)) Detect(const U&); + public: static constexpr bool Value = std::is_same<bool, decltype(Detect(std::declval<TProto>()))>::value; }; @@ -1141,32 +1154,27 @@ class IFacilityProvider; template <typename TReq, typename TResp, bool IsOperation> class TGrpcRequestCall : public std::conditional_t<TProtoHasValidate<TReq>::Value, - TGRpcRequestValidationWrapperImpl< - TRpcServices::EvGrpcRuntimeRequest, TReq, TResp, IsOperation, TGrpcRequestCall<TReq, TResp, IsOperation>>, - TGRpcRequestWrapperImpl< - TRpcServices::EvGrpcRuntimeRequest, TReq, TResp, IsOperation, TGrpcRequestCall<TReq, TResp, IsOperation>>> - { - typedef typename std::conditional<IsOperation, IRequestOpCtx, IRequestNoOpCtx>::type TRequestIface; + TGRpcRequestValidationWrapperImpl< + TRpcServices::EvGrpcRuntimeRequest, TReq, TResp, IsOperation, TGrpcRequestCall<TReq, TResp, IsOperation>>, + TGRpcRequestWrapperImpl< + TRpcServices::EvGrpcRuntimeRequest, TReq, TResp, IsOperation, TGrpcRequestCall<TReq, TResp, IsOperation>>> +{ + using TRequestIface = typename std::conditional<IsOperation, IRequestOpCtx, IRequestNoOpCtx>::type; + public: - static constexpr bool IsOp = IsOperation; static IActor* CreateRpcActor(typename std::conditional<IsOperation, IRequestOpCtx, IRequestNoOpCtx>::type* msg); - using TBase = std::conditional_t<TProtoHasValidate<TReq>::Value, - TGRpcRequestValidationWrapperImpl< - TRpcServices::EvGrpcRuntimeRequest, TReq, TResp, IsOperation, TGrpcRequestCall<TReq, TResp, IsOperation>>, - TGRpcRequestWrapperImpl< - TRpcServices::EvGrpcRuntimeRequest, TReq, TResp, IsOperation, TGrpcRequestCall<TReq, TResp, IsOperation>>>; + static constexpr bool IsOp = IsOperation; - TGrpcRequestCall(NGrpc::IRequestContextBase* ctx, - void (*cb)(std::unique_ptr<TRequestIface>, const IFacilityProvider&), TRequestAuxSettings auxSettings = {}) - : TBase(ctx) - , PassMethod(cb) - , AuxSettings(std::move(auxSettings)) - { } + using TBase = std::conditional_t<TProtoHasValidate<TReq>::Value, + TGRpcRequestValidationWrapperImpl< + TRpcServices::EvGrpcRuntimeRequest, TReq, TResp, IsOperation, TGrpcRequestCall<TReq, TResp, IsOperation>>, + TGRpcRequestWrapperImpl< + TRpcServices::EvGrpcRuntimeRequest, TReq, TResp, IsOperation, TGrpcRequestCall<TReq, TResp, IsOperation>>>; - TGrpcRequestCall(NGrpc::IRequestContextBase* ctx, - std::function<void(std::unique_ptr<TRequestIface>, const IFacilityProvider&)> cb, TRequestAuxSettings auxSettings = {}) + template <typename TCallback> + TGrpcRequestCall(NGrpc::IRequestContextBase* ctx, TCallback&& cb, TRequestAuxSettings auxSettings = {}) : TBase(ctx) - , PassMethod(cb) + , PassMethod(std::forward<TCallback>(cb)) , AuxSettings(std::move(auxSettings)) { } @@ -1188,6 +1196,7 @@ public: return true; } } + private: std::function<void(std::unique_ptr<TRequestIface>, const IFacilityProvider&)> PassMethod; const TRequestAuxSettings AuxSettings; @@ -1199,10 +1208,11 @@ using TGrpcRequestOperationCall = TGrpcRequestCall<TReq, TResp, true>; template <typename TReq, typename TResp> using TGrpcRequestNoOperationCall = TGrpcRequestCall<TReq, TResp, false>; -template<ui32 TRpcId, typename TReq, typename TResp, bool IsOperation, TRateLimiterMode RlMode = TRateLimiterMode::Off> -class TGRpcRequestWrapper : - public TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, - TGRpcRequestWrapper<TRpcId, TReq, TResp, IsOperation, RlMode>> { +template <ui32 TRpcId, typename TReq, typename TResp, bool IsOperation, TRateLimiterMode RlMode = TRateLimiterMode::Off> +class TGRpcRequestWrapper + : public TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, + TGRpcRequestWrapper<TRpcId, TReq, TResp, IsOperation, RlMode>> +{ public: static IActor* CreateRpcActor(typename std::conditional<IsOperation, IRequestOpCtx, IRequestNoOpCtx>::type* msg); static constexpr bool IsOp = IsOperation; @@ -1210,7 +1220,7 @@ public: TGRpcRequestWrapper(NGrpc::IRequestContextBase* ctx) : TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, - TGRpcRequestWrapper<TRpcId, TReq, TResp, IsOperation, RlMode>>(ctx) + TGRpcRequestWrapper<TRpcId, TReq, TResp, IsOperation, RlMode>>(ctx) { } TRateLimiterMode GetRlMode() const override { @@ -1222,16 +1232,19 @@ public: } }; -template<ui32 TRpcId, typename TReq, typename TResp, bool IsOperation = true, TRateLimiterMode RlMode = TRateLimiterMode::Off> -class TGRpcRequestWrapperNoAuth : - public TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, TGRpcRequestWrapperNoAuth<TRpcId, TReq, TResp, IsOperation, RlMode>> { +template <ui32 TRpcId, typename TReq, typename TResp, bool IsOperation = true, TRateLimiterMode RlMode = TRateLimiterMode::Off> +class TGRpcRequestWrapperNoAuth + : public TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, + TGRpcRequestWrapperNoAuth<TRpcId, TReq, TResp, IsOperation, RlMode>> +{ public: static IActor* CreateRpcActor(typename std::conditional<IsOperation, IRequestOpCtx, IRequestNoOpCtx>::type* msg); static constexpr bool IsOp = IsOperation; static constexpr TRateLimiterMode RateLimitMode = RlMode; TGRpcRequestWrapperNoAuth(NGrpc::IRequestContextBase* ctx) - : TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, TGRpcRequestWrapperNoAuth<TRpcId, TReq, TResp, IsOperation, RlMode>>(ctx) + : TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, + TGRpcRequestWrapperNoAuth<TRpcId, TReq, TResp, IsOperation, RlMode>>(ctx) { } TRateLimiterMode GetRlMode() const override { @@ -1248,16 +1261,19 @@ public: } }; -template<ui32 TRpcId, typename TReq, typename TResp, bool IsOperation, TRateLimiterMode RlMode = TRateLimiterMode::Off> -class TGRpcRequestValidationWrapper : - public TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, TGRpcRequestValidationWrapper<TRpcId, TReq, TResp, IsOperation, RlMode>> { - +template <ui32 TRpcId, typename TReq, typename TResp, bool IsOperation, TRateLimiterMode RlMode = TRateLimiterMode::Off> +class TGRpcRequestValidationWrapper + : public TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, + TGRpcRequestValidationWrapper<TRpcId, TReq, TResp, IsOperation, RlMode>> +{ public: static IActor* CreateRpcActor(typename std::conditional<IsOperation, IRequestOpCtx, IRequestNoOpCtx>::type* msg); static constexpr bool IsOp = IsOperation; static constexpr TRateLimiterMode RateLimitMode = RlMode; + TGRpcRequestValidationWrapper(NGrpc::IRequestContextBase* ctx, bool rlAllowed = true) - : TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, TGRpcRequestValidationWrapper<TRpcId, TReq, TResp, IsOperation, RlMode>>(ctx) + : TGRpcRequestWrapperImpl<TRpcId, TReq, TResp, IsOperation, + TGRpcRequestValidationWrapper<TRpcId, TReq, TResp, IsOperation, RlMode>>(ctx) , RlAllowed(rlAllowed) { } @@ -1272,6 +1288,7 @@ public: bool Validate(TString& error) override { return this->GetProtoRequest()->validate(error); } + private: bool RlAllowed; }; diff --git a/ydb/core/grpc_services/rpc_calls.h b/ydb/core/grpc_services/rpc_calls.h index 5216aacb436..075b573b543 100644 --- a/ydb/core/grpc_services/rpc_calls.h +++ b/ydb/core/grpc_services/rpc_calls.h @@ -77,6 +77,5 @@ using TEvLoginRequest = TGRpcRequestWrapperNoAuth<TRpcServices::EvLogin, Ydb::Au using TEvNodeCheckRequest = TGRpcRequestWrapperNoAuth<TRpcServices::EvNodeCheckRequest, Ydb::Monitoring::NodeCheckRequest, Ydb::Monitoring::NodeCheckResponse>; using TEvCoordinationSessionRequest = TGRpcRequestBiStreamWrapper<TRpcServices::EvCoordinationSession, Ydb::Coordination::SessionRequest, Ydb::Coordination::SessionResponse>; - } // namespace NGRpcService } // namespace NKikimr |