diff options
author | Alexey Borzenkov <snaury@yandex-team.ru> | 2022-02-10 16:47:43 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:47:43 +0300 |
commit | 330c83f8c116bd45316397b179275e9d87007e7d (patch) | |
tree | c0748b5dcbade83af788c0abfa89c0383d6b779c /library/cpp/grpc/server | |
parent | 22d92781ba2a10b7fb5b977b7d1a5c40ff53885f (diff) | |
download | ydb-330c83f8c116bd45316397b179275e9d87007e7d.tar.gz |
Restoring authorship annotation for Alexey Borzenkov <snaury@yandex-team.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/grpc/server')
-rw-r--r-- | library/cpp/grpc/server/grpc_counters.h | 12 | ||||
-rw-r--r-- | library/cpp/grpc/server/grpc_request.h | 56 | ||||
-rw-r--r-- | library/cpp/grpc/server/grpc_request_base.h | 6 | ||||
-rw-r--r-- | library/cpp/grpc/server/grpc_server.cpp | 12 | ||||
-rw-r--r-- | library/cpp/grpc/server/grpc_server.h | 198 |
5 files changed, 142 insertions, 142 deletions
diff --git a/library/cpp/grpc/server/grpc_counters.h b/library/cpp/grpc/server/grpc_counters.h index e86299cbd94..0b6c36c84cc 100644 --- a/library/cpp/grpc/server/grpc_counters.h +++ b/library/cpp/grpc/server/grpc_counters.h @@ -83,13 +83,13 @@ public: } void CountRequestBytes(ui32 requestSize) override { - *RequestBytes += requestSize; - } - + *RequestBytes += requestSize; + } + void CountResponseBytes(ui32 responseSize) override { - *ResponseBytes += responseSize; - } - + *ResponseBytes += responseSize; + } + void StartProcessing(ui32 requestSize) override { TotalCounter->Inc(); InflyCounter->Inc(); diff --git a/library/cpp/grpc/server/grpc_request.h b/library/cpp/grpc/server/grpc_request.h index 1fcd8b66556..5bd8d3902b5 100644 --- a/library/cpp/grpc/server/grpc_request.h +++ b/library/cpp/grpc/server/grpc_request.h @@ -117,24 +117,24 @@ public: return TString(this->Context.peer()); } - bool SslServer() const override { - return Server_->SslServer(); - } - + bool SslServer() const override { + return Server_->SslServer(); + } + void Run() { - // Start request unless server is shutting down - if (auto guard = Server_->ProtectShutdown()) { - Ref(); //For grpc c runtime - this->Context.AsyncNotifyWhenDone(OnFinishTag.Prepare()); - if (RequestCallback_) { - (this->Service->*RequestCallback_) - (&this->Context, Request_, - reinterpret_cast<grpc::ServerAsyncResponseWriter<TOut>*>(Writer_.Get()), this->CQ, this->CQ, GetGRpcTag()); - } else { - (this->Service->*StreamRequestCallback_) - (&this->Context, Request_, - reinterpret_cast<grpc::ServerAsyncWriter<TOut>*>(StreamWriter_.Get()), this->CQ, this->CQ, GetGRpcTag()); - } + // Start request unless server is shutting down + if (auto guard = Server_->ProtectShutdown()) { + Ref(); //For grpc c runtime + this->Context.AsyncNotifyWhenDone(OnFinishTag.Prepare()); + if (RequestCallback_) { + (this->Service->*RequestCallback_) + (&this->Context, Request_, + reinterpret_cast<grpc::ServerAsyncResponseWriter<TOut>*>(Writer_.Get()), this->CQ, this->CQ, GetGRpcTag()); + } else { + (this->Service->*StreamRequestCallback_) + (&this->Context, Request_, + reinterpret_cast<grpc::ServerAsyncWriter<TOut>*>(StreamWriter_.Get()), this->CQ, this->CQ, GetGRpcTag()); + } } } @@ -148,10 +148,10 @@ public: } void DestroyRequest() override { - if (RequestRegistered_) { - Server_->DeregisterRequestCtx(this); - RequestRegistered_ = false; - } + if (RequestRegistered_) { + Server_->DeregisterRequestCtx(this); + RequestRegistered_ = false; + } UnRef(); } @@ -346,15 +346,15 @@ private: ok ? "true" : "false", makeRequestString().data(), this->Context.peer().c_str()); if (this->Context.c_call() == nullptr) { - Y_VERIFY(!ok); + Y_VERIFY(!ok); // One ref by OnFinishTag, grpc will not call this tag if no request received UnRef(); - } else if (!(RequestRegistered_ = Server_->RegisterRequestCtx(this))) { - // Request cannot be registered due to shutdown - // It's unsafe to continue, so drop this request without processing + } else if (!(RequestRegistered_ = Server_->RegisterRequestCtx(this))) { + // Request cannot be registered due to shutdown + // It's unsafe to continue, so drop this request without processing GRPC_LOG_DEBUG(Logger_, "[%p] dropping request Name# %s due to shutdown", this, Name_); - this->Context.TryCancel(); - return false; + this->Context.TryCancel(); + return false; } Clone(); // TODO: Request pool? @@ -501,7 +501,7 @@ private: ui32 ResponseStatus = 0; THPTimer RequestTimer; TAuthState AuthState_ = 0; - bool RequestRegistered_ = false; + bool RequestRegistered_ = false; using TFixedEvent = TQueueFixedEvent<TGRpcRequestImpl>; TFixedEvent OnFinishTag = { this, &TGRpcRequestImpl::OnFinish }; diff --git a/library/cpp/grpc/server/grpc_request_base.h b/library/cpp/grpc/server/grpc_request_base.h index dc293ec01a9..fcfce1c181a 100644 --- a/library/cpp/grpc/server/grpc_request_base.h +++ b/library/cpp/grpc/server/grpc_request_base.h @@ -108,9 +108,9 @@ public: //! Returns peer address virtual TString GetPeer() const = 0; - - //! Returns true if server is using ssl - virtual bool SslServer() const = 0; + + //! Returns true if server is using ssl + virtual bool SslServer() const = 0; }; } // namespace NGrpc diff --git a/library/cpp/grpc/server/grpc_server.cpp b/library/cpp/grpc/server/grpc_server.cpp index 9bc83053908..7437b7a8f5e 100644 --- a/library/cpp/grpc/server/grpc_server.cpp +++ b/library/cpp/grpc/server/grpc_server.cpp @@ -77,7 +77,7 @@ void TGRpcServer::Start() { builder.SetMaxReceiveMessageSize(Options_.MaxMessageSize); builder.SetMaxSendMessageSize(Options_.MaxMessageSize); for (IGRpcServicePtr service : Services_) { - service->SetServerOptions(Options_); + service->SetServerOptions(Options_); builder.RegisterService(service->GetService()); service->SetGlobalLimiterHandle(&Limiter_); } @@ -192,14 +192,14 @@ void TGRpcServer::Stop() { } for (ui64 attempt = 0; ; ++attempt) { - bool unsafe = false; + bool unsafe = false; size_t infly = 0; for (auto& service : Services_) { - unsafe |= service->IsUnsafeToShutdown(); - infly += service->RequestsInProgress(); + unsafe |= service->IsUnsafeToShutdown(); + infly += service->RequestsInProgress(); } - if (!unsafe && !infly) + if (!unsafe && !infly) break; auto spent = (TInstant::Now() - now).SecondsFloat(); @@ -208,7 +208,7 @@ void TGRpcServer::Stop() { Cerr << "GRpc shutdown warning: left infly: " << infly << ", spent: " << spent << " sec" << Endl; } - if (!unsafe && spent > Options_.GRpcShutdownDeadline.SecondsFloat()) + if (!unsafe && spent > Options_.GRpcShutdownDeadline.SecondsFloat()) break; Sleep(TDuration::MilliSeconds(10)); } diff --git a/library/cpp/grpc/server/grpc_server.h b/library/cpp/grpc/server/grpc_server.h index 59ed364bc9d..d6814a90a0d 100644 --- a/library/cpp/grpc/server/grpc_server.h +++ b/library/cpp/grpc/server/grpc_server.h @@ -167,77 +167,77 @@ public: virtual void StopService() noexcept = 0; virtual void InitService(grpc::ServerCompletionQueue* cq, TLoggerPtr logger) = 0; virtual void SetGlobalLimiterHandle(TGlobalLimiter* limiter) = 0; - virtual bool IsUnsafeToShutdown() const = 0; - virtual size_t RequestsInProgress() const = 0; - - /** - * Called before service is added to the server builder. This allows - * service to inspect server options and initialize accordingly. - */ - virtual void SetServerOptions(const TServerOptions& options) = 0; + virtual bool IsUnsafeToShutdown() const = 0; + virtual size_t RequestsInProgress() const = 0; + + /** + * Called before service is added to the server builder. This allows + * service to inspect server options and initialize accordingly. + */ + virtual void SetServerOptions(const TServerOptions& options) = 0; }; template<typename T> class TGrpcServiceBase: public IGRpcService { public: - class TShutdownGuard { - using TOwner = TGrpcServiceBase<T>; - friend class TGrpcServiceBase<T>; - - public: - TShutdownGuard() - : Owner(nullptr) - { } - - ~TShutdownGuard() { - Release(); - } - - TShutdownGuard(TShutdownGuard&& other) - : Owner(other.Owner) - { - other.Owner = nullptr; - } - - TShutdownGuard& operator=(TShutdownGuard&& other) { - if (Y_LIKELY(this != &other)) { - Release(); - Owner = other.Owner; - other.Owner = nullptr; - } - return *this; - } - - explicit operator bool() const { - return bool(Owner); - } - - void Release() { - if (Owner) { - AtomicDecrement(Owner->GuardCount_); - Owner = nullptr; - } - } - - TShutdownGuard(const TShutdownGuard&) = delete; - TShutdownGuard& operator=(const TShutdownGuard&) = delete; - - private: - explicit TShutdownGuard(TOwner* owner) - : Owner(owner) - { } - - private: - TOwner* Owner; - }; - -public: + class TShutdownGuard { + using TOwner = TGrpcServiceBase<T>; + friend class TGrpcServiceBase<T>; + + public: + TShutdownGuard() + : Owner(nullptr) + { } + + ~TShutdownGuard() { + Release(); + } + + TShutdownGuard(TShutdownGuard&& other) + : Owner(other.Owner) + { + other.Owner = nullptr; + } + + TShutdownGuard& operator=(TShutdownGuard&& other) { + if (Y_LIKELY(this != &other)) { + Release(); + Owner = other.Owner; + other.Owner = nullptr; + } + return *this; + } + + explicit operator bool() const { + return bool(Owner); + } + + void Release() { + if (Owner) { + AtomicDecrement(Owner->GuardCount_); + Owner = nullptr; + } + } + + TShutdownGuard(const TShutdownGuard&) = delete; + TShutdownGuard& operator=(const TShutdownGuard&) = delete; + + private: + explicit TShutdownGuard(TOwner* owner) + : Owner(owner) + { } + + private: + TOwner* Owner; + }; + +public: using TCurrentGRpcService = T; void StopService() noexcept override { with_lock(Lock_) { - AtomicSet(ShuttingDown_, 1); - + AtomicSet(ShuttingDown_, 1); + // Send TryCansel to event (can be send after finishing). // Actual dtors will be called from grpc thread, so deadlock impossible for (auto* request : Requests_) { @@ -246,21 +246,21 @@ public: } } - TShutdownGuard ProtectShutdown() noexcept { - AtomicIncrement(GuardCount_); - if (IsShuttingDown()) { - AtomicDecrement(GuardCount_); - return { }; - } - - return TShutdownGuard(this); - }; - - bool IsUnsafeToShutdown() const override { - return AtomicGet(GuardCount_) > 0; - } - - size_t RequestsInProgress() const override { + TShutdownGuard ProtectShutdown() noexcept { + AtomicIncrement(GuardCount_); + if (IsShuttingDown()) { + AtomicDecrement(GuardCount_); + return { }; + } + + return TShutdownGuard(this); + }; + + bool IsUnsafeToShutdown() const override { + return AtomicGet(GuardCount_) > 0; + } + + size_t RequestsInProgress() const override { size_t c = 0; with_lock(Lock_) { c = Requests_.size(); @@ -268,9 +268,9 @@ public: return c; } - void SetServerOptions(const TServerOptions& options) override { - SslServer_ = bool(options.SslData); - NeedAuth_ = options.UseAuth; + void SetServerOptions(const TServerOptions& options) override { + SslServer_ = bool(options.SslData); + NeedAuth_ = options.UseAuth; } void SetGlobalLimiterHandle(TGlobalLimiter* /*limiter*/) override {} @@ -280,32 +280,32 @@ public: return AtomicGet(ShuttingDown_); } - bool SslServer() const { - return SslServer_; - } - + bool SslServer() const { + return SslServer_; + } + bool NeedAuth() const { return NeedAuth_; } - bool RegisterRequestCtx(ICancelableContext* req) { + bool RegisterRequestCtx(ICancelableContext* req) { with_lock(Lock_) { - auto r = Requests_.emplace(req); - Y_VERIFY(r.second, "Ctx already registered"); - - if (IsShuttingDown()) { - // Server is already shutting down - Requests_.erase(r.first); - return false; - } + auto r = Requests_.emplace(req); + Y_VERIFY(r.second, "Ctx already registered"); + + if (IsShuttingDown()) { + // Server is already shutting down + Requests_.erase(r.first); + return false; + } } - - return true; + + return true; } void DeregisterRequestCtx(ICancelableContext* req) { with_lock(Lock_) { - Y_VERIFY(Requests_.erase(req), "Ctx is not registered"); + Y_VERIFY(Requests_.erase(req), "Ctx is not registered"); } } @@ -313,15 +313,15 @@ protected: using TGrpcAsyncService = typename TCurrentGRpcService::AsyncService; TGrpcAsyncService Service_; - TGrpcAsyncService* GetService() override { + TGrpcAsyncService* GetService() override { return &Service_; } private: TAtomic ShuttingDown_ = 0; - TAtomic GuardCount_ = 0; + TAtomic GuardCount_ = 0; - bool SslServer_ = false; + bool SslServer_ = false; bool NeedAuth_ = false; THashSet<ICancelableContext*> Requests_; |