diff options
author | Anton Samokhvalov <pg83@yandex.ru> | 2022-02-10 16:45:17 +0300 |
---|---|---|
committer | Daniil Cherednik <dcherednik@yandex-team.ru> | 2022-02-10 16:45:17 +0300 |
commit | d3a398281c6fd1d3672036cb2d63f842d2cb28c5 (patch) | |
tree | dd4bd3ca0f36b817e96812825ffaf10d645803f2 /library/cpp/messagebus | |
parent | 72cb13b4aff9bc9cf22e49251bc8fd143f82538f (diff) | |
download | ydb-d3a398281c6fd1d3672036cb2d63f842d2cb28c5.tar.gz |
Restoring authorship annotation for Anton Samokhvalov <pg83@yandex.ru>. Commit 2 of 2.
Diffstat (limited to 'library/cpp/messagebus')
200 files changed, 7560 insertions, 7560 deletions
diff --git a/library/cpp/messagebus/acceptor.cpp b/library/cpp/messagebus/acceptor.cpp index 3136ff3284..64a38619c2 100644 --- a/library/cpp/messagebus/acceptor.cpp +++ b/library/cpp/messagebus/acceptor.cpp @@ -100,7 +100,7 @@ void TAcceptor::Act(TDefaultTag) { ++Stats.AcceptSuccessCount; } - skipAccept:; + skipAccept:; } Channel->EnableRead(); diff --git a/library/cpp/messagebus/acceptor.h b/library/cpp/messagebus/acceptor.h index 6f645cdfa1..57cb010bf2 100644 --- a/library/cpp/messagebus/acceptor.h +++ b/library/cpp/messagebus/acceptor.h @@ -11,50 +11,50 @@ #include <util/system/event.h> -namespace NBus { - namespace NPrivate { - class TAcceptor - : public NEventLoop::IEventHandler, - private ::NActor::TActor<TAcceptor> { - friend struct TBusSessionImpl; - friend class ::NActor::TActor<TAcceptor>; +namespace NBus { + namespace NPrivate { + class TAcceptor + : public NEventLoop::IEventHandler, + private ::NActor::TActor<TAcceptor> { + friend struct TBusSessionImpl; + friend class ::NActor::TActor<TAcceptor>; - public: - TAcceptor(TBusSessionImpl* session, ui64 acceptorId, SOCKET socket, const TNetAddr& addr); + public: + TAcceptor(TBusSessionImpl* session, ui64 acceptorId, SOCKET socket, const TNetAddr& addr); - void HandleEvent(SOCKET socket, void* cookie) override; + void HandleEvent(SOCKET socket, void* cookie) override; - void Shutdown(); + void Shutdown(); - inline ::NActor::TActor<TAcceptor>* GetActor() { - return this; - } + inline ::NActor::TActor<TAcceptor>* GetActor() { + return this; + } - private: - void SendStatus(TInstant now); - void Act(::NActor::TDefaultTag); + private: + void SendStatus(TInstant now); + void Act(::NActor::TDefaultTag); - private: - const ui64 AcceptorId; + private: + const ui64 AcceptorId; - TBusSessionImpl* const Session; - NEventLoop::TChannelPtr Channel; + TBusSessionImpl* const Session; + NEventLoop::TChannelPtr Channel; - TAcceptorStatus Stats; + TAcceptorStatus Stats; - TAtomicShutdownState ShutdownState; + TAtomicShutdownState ShutdownState; - struct TGranStatus { - TGranStatus(TDuration gran) - : Listen(gran) - { - } + struct TGranStatus { + TGranStatus(TDuration gran) + : Listen(gran) + { + } - TGranUp<TAcceptorStatus> Listen; - }; + TGranUp<TAcceptorStatus> Listen; + }; - TGranStatus GranStatus; + TGranStatus GranStatus; }; - } -} + } +} diff --git a/library/cpp/messagebus/acceptor_status.cpp b/library/cpp/messagebus/acceptor_status.cpp index fcc982e752..5006ff68ae 100644 --- a/library/cpp/messagebus/acceptor_status.cpp +++ b/library/cpp/messagebus/acceptor_status.cpp @@ -53,13 +53,13 @@ TString TAcceptorStatus::PrintToString() const { if (AcceptErrorCount > 0) { p.AddRow("last accept error", - TString() + LastSystemErrorText(LastAcceptErrorErrno) + " at " + LastAcceptErrorInstant.ToString()); + TString() + LastSystemErrorText(LastAcceptErrorErrno) + " at " + LastAcceptErrorInstant.ToString()); } p.AddRow("accept success count", LeftPad(AcceptSuccessCount, 4)); if (AcceptSuccessCount > 0) { p.AddRow("last accept success", - TString() + "at " + LastAcceptSuccessInstant.ToString()); + TString() + "at " + LastAcceptSuccessInstant.ToString()); } ss << p.PrintToString(); diff --git a/library/cpp/messagebus/acceptor_status.h b/library/cpp/messagebus/acceptor_status.h index 81b5b26485..6aa1404f4d 100644 --- a/library/cpp/messagebus/acceptor_status.h +++ b/library/cpp/messagebus/acceptor_status.h @@ -4,32 +4,32 @@ #include <util/network/init.h> -namespace NBus { - namespace NPrivate { - struct TAcceptorStatus { - bool Summary; +namespace NBus { + namespace NPrivate { + struct TAcceptorStatus { + bool Summary; - ui64 AcceptorId; + ui64 AcceptorId; - SOCKET Fd; + SOCKET Fd; - TNetAddr ListenAddr; + TNetAddr ListenAddr; - unsigned AcceptSuccessCount; - TInstant LastAcceptSuccessInstant; + unsigned AcceptSuccessCount; + TInstant LastAcceptSuccessInstant; - unsigned AcceptErrorCount; - TInstant LastAcceptErrorInstant; - int LastAcceptErrorErrno; + unsigned AcceptErrorCount; + TInstant LastAcceptErrorInstant; + int LastAcceptErrorErrno; - void ResetIncremental(); + void ResetIncremental(); - TAcceptorStatus(); + TAcceptorStatus(); - TAcceptorStatus& operator+=(const TAcceptorStatus& that); + TAcceptorStatus& operator+=(const TAcceptorStatus& that); - TString PrintToString() const; - }; + TString PrintToString() const; + }; - } -} + } +} diff --git a/library/cpp/messagebus/actor/actor.h b/library/cpp/messagebus/actor/actor.h index 1f5af55097..9b8f20298a 100644 --- a/library/cpp/messagebus/actor/actor.h +++ b/library/cpp/messagebus/actor/actor.h @@ -7,58 +7,58 @@ #include <util/system/yassert.h> namespace NActor { - class IActor: protected IWorkItem { - public: - // TODO: make private - TTasks Tasks; - - public: - virtual void ScheduleHereV() = 0; - virtual void ScheduleV() = 0; - virtual void ScheduleHereAtMostOnceV() = 0; - - // TODO: make private - virtual void RefV() = 0; - virtual void UnRefV() = 0; - - // mute warnings - ~IActor() override { - } - }; - - struct TDefaultTag {}; - - template <typename TThis, typename TTag = TDefaultTag> - class TActor: public IActor { - private: - TExecutor* const Executor; - - public: - TActor(TExecutor* executor) - : Executor(executor) - { - } - - void AddTaskFromActorLoop() { - bool schedule = Tasks.AddTask(); - // TODO: check thread id - Y_ASSERT(!schedule); - } - - /** + class IActor: protected IWorkItem { + public: + // TODO: make private + TTasks Tasks; + + public: + virtual void ScheduleHereV() = 0; + virtual void ScheduleV() = 0; + virtual void ScheduleHereAtMostOnceV() = 0; + + // TODO: make private + virtual void RefV() = 0; + virtual void UnRefV() = 0; + + // mute warnings + ~IActor() override { + } + }; + + struct TDefaultTag {}; + + template <typename TThis, typename TTag = TDefaultTag> + class TActor: public IActor { + private: + TExecutor* const Executor; + + public: + TActor(TExecutor* executor) + : Executor(executor) + { + } + + void AddTaskFromActorLoop() { + bool schedule = Tasks.AddTask(); + // TODO: check thread id + Y_ASSERT(!schedule); + } + + /** * Schedule actor. * * If actor is sleeping, then actor will be executed right now. * If actor is executing right now, it will be executed one more time. * If this method is called multiple time, actor will be re-executed no more than one more time. */ - void Schedule() { - if (Tasks.AddTask()) { - EnqueueWork(); - } + void Schedule() { + if (Tasks.AddTask()) { + EnqueueWork(); + } } - /** + /** * Schedule actor, execute it in current thread. * * If actor is running, continue executing where it is executing. @@ -66,79 +66,79 @@ namespace NActor { * * Operation is useful for tasks that are likely to complete quickly. */ - void ScheduleHere() { - if (Tasks.AddTask()) { - Loop(); - } + void ScheduleHere() { + if (Tasks.AddTask()) { + Loop(); + } } - /** + /** * Schedule actor, execute in current thread no more than once. * * If actor is running, continue executing where it is executing. * If actor is sleeping, execute one iteration here, and if actor got new tasks, * reschedule it in worker pool. */ - void ScheduleHereAtMostOnce() { - if (Tasks.AddTask()) { - bool fetched = Tasks.FetchTask(); - Y_VERIFY(fetched, "happens"); - - DoAct(); - - // if someone added more tasks, schedule them - if (Tasks.FetchTask()) { - bool added = Tasks.AddTask(); - Y_VERIFY(!added, "happens"); - EnqueueWork(); - } + void ScheduleHereAtMostOnce() { + if (Tasks.AddTask()) { + bool fetched = Tasks.FetchTask(); + Y_VERIFY(fetched, "happens"); + + DoAct(); + + // if someone added more tasks, schedule them + if (Tasks.FetchTask()) { + bool added = Tasks.AddTask(); + Y_VERIFY(!added, "happens"); + EnqueueWork(); + } + } + } + + void ScheduleHereV() override { + ScheduleHere(); + } + void ScheduleV() override { + Schedule(); + } + void ScheduleHereAtMostOnceV() override { + ScheduleHereAtMostOnce(); + } + void RefV() override { + GetThis()->Ref(); + } + void UnRefV() override { + GetThis()->UnRef(); + } + + private: + TThis* GetThis() { + return static_cast<TThis*>(this); + } + + void EnqueueWork() { + GetThis()->Ref(); + Executor->EnqueueWork({this}); + } + + void DoAct() { + WHAT_THREAD_DOES_PUSH_POP_CURRENT_FUNC(); + + GetThis()->Act(TTag()); + } + + void Loop() { + // TODO: limit number of iterations + while (Tasks.FetchTask()) { + DoAct(); } } - void ScheduleHereV() override { - ScheduleHere(); - } - void ScheduleV() override { - Schedule(); - } - void ScheduleHereAtMostOnceV() override { - ScheduleHereAtMostOnce(); - } - void RefV() override { - GetThis()->Ref(); - } - void UnRefV() override { - GetThis()->UnRef(); - } - - private: - TThis* GetThis() { - return static_cast<TThis*>(this); - } - - void EnqueueWork() { - GetThis()->Ref(); - Executor->EnqueueWork({this}); - } - - void DoAct() { - WHAT_THREAD_DOES_PUSH_POP_CURRENT_FUNC(); - - GetThis()->Act(TTag()); - } - - void Loop() { - // TODO: limit number of iterations - while (Tasks.FetchTask()) { - DoAct(); - } - } - - void DoWork() override { - Y_ASSERT(GetThis()->RefCount() >= 1); - Loop(); - GetThis()->UnRef(); - } - }; - -} + void DoWork() override { + Y_ASSERT(GetThis()->RefCount() >= 1); + Loop(); + GetThis()->UnRef(); + } + }; + +} diff --git a/library/cpp/messagebus/actor/actor_ut.cpp b/library/cpp/messagebus/actor/actor_ut.cpp index 20dee53e2d..b76ab55bfa 100644 --- a/library/cpp/messagebus/actor/actor_ut.cpp +++ b/library/cpp/messagebus/actor/actor_ut.cpp @@ -11,14 +11,14 @@ using namespace NActor; template <typename TThis> -struct TTestActorBase: public TAtomicRefCount<TThis>, public TActor<TThis> { +struct TTestActorBase: public TAtomicRefCount<TThis>, public TActor<TThis> { TTestSync Started; TTestSync Acted; TTestActorBase(TExecutor* executor) : TActor<TThis>(executor) - { - } + { + } void Act(TDefaultTag) { Started.Inc(); @@ -27,23 +27,23 @@ struct TTestActorBase: public TAtomicRefCount<TThis>, public TActor<TThis> { } }; -struct TNopActor: public TTestActorBase<TNopActor> { +struct TNopActor: public TTestActorBase<TNopActor> { TObjectCounter<TNopActor> AllocCounter; TNopActor(TExecutor* executor) : TTestActorBase<TNopActor>(executor) - { - } + { + } void Act2() { } }; -struct TWaitForSignalActor: public TTestActorBase<TWaitForSignalActor> { +struct TWaitForSignalActor: public TTestActorBase<TWaitForSignalActor> { TWaitForSignalActor(TExecutor* executor) : TTestActorBase<TWaitForSignalActor>(executor) - { - } + { + } TSystemEvent WaitFor; @@ -52,7 +52,7 @@ struct TWaitForSignalActor: public TTestActorBase<TWaitForSignalActor> { } }; -struct TDecrementAndSendActor: public TTestActorBase<TDecrementAndSendActor>, public TQueueInActor<TDecrementAndSendActor, int> { +struct TDecrementAndSendActor: public TTestActorBase<TDecrementAndSendActor>, public TQueueInActor<TDecrementAndSendActor, int> { TSystemEvent Done; TDecrementAndSendActor* Next; @@ -60,8 +60,8 @@ struct TDecrementAndSendActor: public TTestActorBase<TDecrementAndSendActor>, pu TDecrementAndSendActor(TExecutor* executor) : TTestActorBase<TDecrementAndSendActor>(executor) , Next(nullptr) - { - } + { + } void ProcessItem(TDefaultTag, TDefaultTag, int n) { if (n == 0) { @@ -129,7 +129,7 @@ Y_UNIT_TEST_SUITE(TActor) { TExecutor executor(queueSize); - TVector<TIntrusivePtr<TDecrementAndSendActor>> actors; + TVector<TIntrusivePtr<TDecrementAndSendActor>> actors; for (int i = 0; i < actorCount; ++i) { actors.push_back(new TDecrementAndSendActor(&executor)); } diff --git a/library/cpp/messagebus/actor/executor.cpp b/library/cpp/messagebus/actor/executor.cpp index 4c428754f3..7a2227a458 100644 --- a/library/cpp/messagebus/actor/executor.cpp +++ b/library/cpp/messagebus/actor/executor.cpp @@ -20,10 +20,10 @@ namespace { struct TRecord { TAtomic MaxQueueSize; - TRecord() - : MaxQueueSize() - { - } + TRecord() + : MaxQueueSize() + { + } TExecutorHistory::THistoryRecord Capture() { TExecutorHistory::THistoryRecord r; @@ -70,8 +70,8 @@ namespace { } -Y_POD_STATIC_THREAD(TExecutor*) -ThreadCurrentExecutor; +Y_POD_STATIC_THREAD(TExecutor*) +ThreadCurrentExecutor; static const char* NoLocation = "nowhere"; @@ -80,80 +80,80 @@ struct TExecutorWorkerThreadLocalData { }; static TExecutorWorkerThreadLocalData WorkerNoThreadLocalData; -Y_POD_STATIC_THREAD(TExecutorWorkerThreadLocalData) -WorkerThreadLocalData; +Y_POD_STATIC_THREAD(TExecutorWorkerThreadLocalData) +WorkerThreadLocalData; namespace NActor { - struct TExecutorWorker { - TExecutor* const Executor; - TThread Thread; - const char** WhatThreadDoesLocation; - TExecutorWorkerThreadLocalData* ThreadLocalData; - - TExecutorWorker(TExecutor* executor) - : Executor(executor) - , Thread(RunThreadProc, this) - , WhatThreadDoesLocation(&NoLocation) - , ThreadLocalData(&::WorkerNoThreadLocalData) - { - Thread.Start(); - } - - void Run() { - WhatThreadDoesLocation = ::WhatThreadDoesLocation(); - AtomicSet(ThreadLocalData, &::WorkerThreadLocalData); - WHAT_THREAD_DOES_PUSH_POP_CURRENT_FUNC(); - Executor->RunWorker(); - } - - static void* RunThreadProc(void* thiz0) { - TExecutorWorker* thiz = (TExecutorWorker*)thiz0; - thiz->Run(); - return nullptr; - } - }; - - struct TExecutor::TImpl { - TExecutor* const Executor; - THistoryInternal History; + struct TExecutorWorker { + TExecutor* const Executor; + TThread Thread; + const char** WhatThreadDoesLocation; + TExecutorWorkerThreadLocalData* ThreadLocalData; + + TExecutorWorker(TExecutor* executor) + : Executor(executor) + , Thread(RunThreadProc, this) + , WhatThreadDoesLocation(&NoLocation) + , ThreadLocalData(&::WorkerNoThreadLocalData) + { + Thread.Start(); + } + + void Run() { + WhatThreadDoesLocation = ::WhatThreadDoesLocation(); + AtomicSet(ThreadLocalData, &::WorkerThreadLocalData); + WHAT_THREAD_DOES_PUSH_POP_CURRENT_FUNC(); + Executor->RunWorker(); + } + + static void* RunThreadProc(void* thiz0) { + TExecutorWorker* thiz = (TExecutorWorker*)thiz0; + thiz->Run(); + return nullptr; + } + }; + + struct TExecutor::TImpl { + TExecutor* const Executor; + THistoryInternal History; TSystemEvent HelperStopSignal; - TThread HelperThread; + TThread HelperThread; - TImpl(TExecutor* executor) - : Executor(executor) - , HelperThread(HelperThreadProc, this) - { - } + TImpl(TExecutor* executor) + : Executor(executor) + , HelperThread(HelperThreadProc, this) + { + } - void RunHelper() { - ui64 nowSeconds = TInstant::Now().Seconds(); - for (;;) { - TInstant nextStop = TInstant::Seconds(nowSeconds + 1) + TDuration::MilliSeconds(RandomNumber<ui32>(1000)); + void RunHelper() { + ui64 nowSeconds = TInstant::Now().Seconds(); + for (;;) { + TInstant nextStop = TInstant::Seconds(nowSeconds + 1) + TDuration::MilliSeconds(RandomNumber<ui32>(1000)); - if (HelperStopSignal.WaitD(nextStop)) { - return; - } + if (HelperStopSignal.WaitD(nextStop)) { + return; + } - nowSeconds = nextStop.Seconds(); + nowSeconds = nextStop.Seconds(); - THistoryInternal::TRecord& record = History.GetNowRecord(nowSeconds); + THistoryInternal::TRecord& record = History.GetNowRecord(nowSeconds); - ui32 maxQueueSize = Executor->GetMaxQueueSizeAndClear(); - if (maxQueueSize > record.MaxQueueSize) { - AtomicSet(record.MaxQueueSize, maxQueueSize); - } + ui32 maxQueueSize = Executor->GetMaxQueueSizeAndClear(); + if (maxQueueSize > record.MaxQueueSize) { + AtomicSet(record.MaxQueueSize, maxQueueSize); + } } } - static void* HelperThreadProc(void* impl0) { - TImpl* impl = (TImpl*)impl0; - impl->RunHelper(); - return nullptr; - } - }; + static void* HelperThreadProc(void* impl0) { + TImpl* impl = (TImpl*)impl0; + impl->RunHelper(); + return nullptr; + } + }; -} +} static TExecutor::TConfig MakeConfig(unsigned workerCount) { TExecutor::TConfig config; @@ -296,9 +296,9 @@ TAutoPtr<IWorkItem> TExecutor::DequeueWork() { WorkAvailable.Wait(WorkMutex); } } - - auto& wtls = TlsRef(WorkerThreadLocalData); - + + auto& wtls = TlsRef(WorkerThreadLocalData); + if (queueSize > RelaxedLoad(&wtls.MaxQueueSize)) { RelaxedStore<ui32>(&wtls.MaxQueueSize, queueSize); } @@ -334,5 +334,5 @@ void TExecutor::RunWorker() { RunWorkItem(wi); } - ThreadCurrentExecutor = (TExecutor*)nullptr; + ThreadCurrentExecutor = (TExecutor*)nullptr; } diff --git a/library/cpp/messagebus/actor/executor.h b/library/cpp/messagebus/actor/executor.h index 4b9bcb1da0..7292d8be53 100644 --- a/library/cpp/messagebus/actor/executor.h +++ b/library/cpp/messagebus/actor/executor.h @@ -11,95 +11,95 @@ #include <util/thread/lfqueue.h> namespace NActor { - namespace NPrivate { - struct TExecutorHistory { - struct THistoryRecord { - ui32 MaxQueueSize; - }; - TVector<THistoryRecord> HistoryRecords; - ui64 LastHistoryRecordSecond; - - ui64 FirstHistoryRecordSecond() const { - return LastHistoryRecordSecond - HistoryRecords.size() + 1; - } - }; - - struct TExecutorStatus { - size_t WorkQueueSize = 0; - TExecutorHistory History; - TString Status; + namespace NPrivate { + struct TExecutorHistory { + struct THistoryRecord { + ui32 MaxQueueSize; + }; + TVector<THistoryRecord> HistoryRecords; + ui64 LastHistoryRecordSecond; + + ui64 FirstHistoryRecordSecond() const { + return LastHistoryRecordSecond - HistoryRecords.size() + 1; + } }; - } - class IWorkItem { - public: - virtual ~IWorkItem() { + struct TExecutorStatus { + size_t WorkQueueSize = 0; + TExecutorHistory History; + TString Status; + }; + } + + class IWorkItem { + public: + virtual ~IWorkItem() { } - virtual void DoWork(/* must release this */) = 0; + virtual void DoWork(/* must release this */) = 0; }; - struct TExecutorWorker; + struct TExecutorWorker; - class TExecutor: public TAtomicRefCount<TExecutor> { - friend struct TExecutorWorker; + class TExecutor: public TAtomicRefCount<TExecutor> { + friend struct TExecutorWorker; - public: - struct TConfig { - size_t WorkerCount; - const char* Name; + public: + struct TConfig { + size_t WorkerCount; + const char* Name; - TConfig() - : WorkerCount(1) - , Name() - { - } - }; + TConfig() + : WorkerCount(1) + , Name() + { + } + }; - private: - struct TImpl; - THolder<TImpl> Impl; + private: + struct TImpl; + THolder<TImpl> Impl; - const TConfig Config; + const TConfig Config; - TAtomic ExitWorkers; + TAtomic ExitWorkers; - TVector<TAutoPtr<TExecutorWorker>> WorkerThreads; + TVector<TAutoPtr<TExecutorWorker>> WorkerThreads; - TRingBufferWithSpinLock<IWorkItem*> WorkItems; + TRingBufferWithSpinLock<IWorkItem*> WorkItems; - TMutex WorkMutex; - TCondVar WorkAvailable; + TMutex WorkMutex; + TCondVar WorkAvailable; - public: - explicit TExecutor(size_t workerCount); - TExecutor(const TConfig& config); - ~TExecutor(); + public: + explicit TExecutor(size_t workerCount); + TExecutor(const TConfig& config); + ~TExecutor(); - void Stop(); + void Stop(); - void EnqueueWork(TArrayRef<IWorkItem* const> w); + void EnqueueWork(TArrayRef<IWorkItem* const> w); - size_t GetWorkQueueSize() const; - TString GetStatus() const; - TString GetStatusSingleLine() const; - NPrivate::TExecutorStatus GetStatusRecordInternal() const; + size_t GetWorkQueueSize() const; + TString GetStatus() const; + TString GetStatusSingleLine() const; + NPrivate::TExecutorStatus GetStatusRecordInternal() const; - bool IsInExecutorThread() const; + bool IsInExecutorThread() const; - private: - void Init(); + private: + void Init(); - TAutoPtr<IWorkItem> DequeueWork(); + TAutoPtr<IWorkItem> DequeueWork(); - void ProcessWorkQueueHere(); + void ProcessWorkQueueHere(); - inline void RunWorkItem(TAutoPtr<IWorkItem>); + inline void RunWorkItem(TAutoPtr<IWorkItem>); - void RunWorker(); + void RunWorker(); - ui32 GetMaxQueueSizeAndClear() const; - }; + ui32 GetMaxQueueSizeAndClear() const; + }; - using TExecutorPtr = TIntrusivePtr<TExecutor>; + using TExecutorPtr = TIntrusivePtr<TExecutor>; -} +} diff --git a/library/cpp/messagebus/actor/queue_for_actor.h b/library/cpp/messagebus/actor/queue_for_actor.h index f59f229b79..40fa536b82 100644 --- a/library/cpp/messagebus/actor/queue_for_actor.h +++ b/library/cpp/messagebus/actor/queue_for_actor.h @@ -9,66 +9,66 @@ #include "temp_tls_vector.h" namespace NActor { - namespace NPrivate { - struct TTagForTl {}; + namespace NPrivate { + struct TTagForTl {}; - } + } - template <typename T> - class TQueueForActor { - private: - TLockFreeStack<T> Queue; + template <typename T> + class TQueueForActor { + private: + TLockFreeStack<T> Queue; - public: - ~TQueueForActor() { - Y_VERIFY(Queue.IsEmpty()); - } + public: + ~TQueueForActor() { + Y_VERIFY(Queue.IsEmpty()); + } - bool IsEmpty() { - return Queue.IsEmpty(); - } + bool IsEmpty() { + return Queue.IsEmpty(); + } - void Enqueue(const T& value) { - Queue.Enqueue(value); - } + void Enqueue(const T& value) { + Queue.Enqueue(value); + } - template <typename TCollection> - void EnqueueAll(const TCollection& all) { - Queue.EnqueueAll(all); - } + template <typename TCollection> + void EnqueueAll(const TCollection& all) { + Queue.EnqueueAll(all); + } - void Clear() { - TVector<T> tmp; - Queue.DequeueAll(&tmp); - } + void Clear() { + TVector<T> tmp; + Queue.DequeueAll(&tmp); + } - template <typename TFunc> - void DequeueAll(const TFunc& func - // TODO: , std::enable_if_t<TFunctionParamCount<TFunc>::Value == 1>* = 0 - ) { - TTempTlsVector<T> temp; + template <typename TFunc> + void DequeueAll(const TFunc& func + // TODO: , std::enable_if_t<TFunctionParamCount<TFunc>::Value == 1>* = 0 + ) { + TTempTlsVector<T> temp; - Queue.DequeueAllSingleConsumer(temp.GetVector()); + Queue.DequeueAllSingleConsumer(temp.GetVector()); - for (typename TVector<T>::reverse_iterator i = temp.GetVector()->rbegin(); i != temp.GetVector()->rend(); ++i) { - func(*i); - } + for (typename TVector<T>::reverse_iterator i = temp.GetVector()->rbegin(); i != temp.GetVector()->rend(); ++i) { + func(*i); + } - temp.Clear(); + temp.Clear(); - if (temp.Capacity() * sizeof(T) > 64 * 1024) { - temp.Shrink(); - } + if (temp.Capacity() * sizeof(T) > 64 * 1024) { + temp.Shrink(); + } } - template <typename TFunc> - void DequeueAllLikelyEmpty(const TFunc& func) { - if (Y_LIKELY(IsEmpty())) { - return; - } + template <typename TFunc> + void DequeueAllLikelyEmpty(const TFunc& func) { + if (Y_LIKELY(IsEmpty())) { + return; + } - DequeueAll(func); + DequeueAll(func); } - }; + }; -} +} diff --git a/library/cpp/messagebus/actor/queue_in_actor.h b/library/cpp/messagebus/actor/queue_in_actor.h index 65110967aa..9865996532 100644 --- a/library/cpp/messagebus/actor/queue_in_actor.h +++ b/library/cpp/messagebus/actor/queue_in_actor.h @@ -6,75 +6,75 @@ #include <functional> namespace NActor { - template <typename TItem> - class IQueueInActor { - public: - virtual void EnqueueAndScheduleV(const TItem& item) = 0; - virtual void DequeueAllV() = 0; - virtual void DequeueAllLikelyEmptyV() = 0; - - virtual ~IQueueInActor() { - } - }; - - template <typename TThis, typename TItem, typename TActorTag = TDefaultTag, typename TQueueTag = TDefaultTag> - class TQueueInActor: public IQueueInActor<TItem> { - typedef TQueueInActor<TThis, TItem, TActorTag, TQueueTag> TSelf; - - public: - // TODO: make protected - TQueueForActor<TItem> QueueInActor; - - private: - TActor<TThis, TActorTag>* GetActor() { - return GetThis(); - } - - TThis* GetThis() { - return static_cast<TThis*>(this); - } - - void ProcessItem(const TItem& item) { - GetThis()->ProcessItem(TActorTag(), TQueueTag(), item); - } - - public: - void EnqueueAndNoSchedule(const TItem& item) { - QueueInActor.Enqueue(item); - } - - void EnqueueAndSchedule(const TItem& item) { - EnqueueAndNoSchedule(item); - GetActor()->Schedule(); - } - - void EnqueueAndScheduleV(const TItem& item) override { - EnqueueAndSchedule(item); - } - - void Clear() { - QueueInActor.Clear(); - } - - void DequeueAll() { - QueueInActor.DequeueAll(std::bind(&TSelf::ProcessItem, this, std::placeholders::_1)); - } - - void DequeueAllV() override { - return DequeueAll(); - } - - void DequeueAllLikelyEmpty() { - QueueInActor.DequeueAllLikelyEmpty(std::bind(&TSelf::ProcessItem, this, std::placeholders::_1)); - } - - void DequeueAllLikelyEmptyV() override { - return DequeueAllLikelyEmpty(); - } - - bool IsEmpty() { - return QueueInActor.IsEmpty(); - } - }; - -} + template <typename TItem> + class IQueueInActor { + public: + virtual void EnqueueAndScheduleV(const TItem& item) = 0; + virtual void DequeueAllV() = 0; + virtual void DequeueAllLikelyEmptyV() = 0; + + virtual ~IQueueInActor() { + } + }; + + template <typename TThis, typename TItem, typename TActorTag = TDefaultTag, typename TQueueTag = TDefaultTag> + class TQueueInActor: public IQueueInActor<TItem> { + typedef TQueueInActor<TThis, TItem, TActorTag, TQueueTag> TSelf; + + public: + // TODO: make protected + TQueueForActor<TItem> QueueInActor; + + private: + TActor<TThis, TActorTag>* GetActor() { + return GetThis(); + } + + TThis* GetThis() { + return static_cast<TThis*>(this); + } + + void ProcessItem(const TItem& item) { + GetThis()->ProcessItem(TActorTag(), TQueueTag(), item); + } + + public: + void EnqueueAndNoSchedule(const TItem& item) { + QueueInActor.Enqueue(item); + } + + void EnqueueAndSchedule(const TItem& item) { + EnqueueAndNoSchedule(item); + GetActor()->Schedule(); + } + + void EnqueueAndScheduleV(const TItem& item) override { + EnqueueAndSchedule(item); + } + + void Clear() { + QueueInActor.Clear(); + } + + void DequeueAll() { + QueueInActor.DequeueAll(std::bind(&TSelf::ProcessItem, this, std::placeholders::_1)); + } + + void DequeueAllV() override { + return DequeueAll(); + } + + void DequeueAllLikelyEmpty() { + QueueInActor.DequeueAllLikelyEmpty(std::bind(&TSelf::ProcessItem, this, std::placeholders::_1)); + } + + void DequeueAllLikelyEmptyV() override { + return DequeueAllLikelyEmpty(); + } + + bool IsEmpty() { + return QueueInActor.IsEmpty(); + } + }; + +} diff --git a/library/cpp/messagebus/actor/ring_buffer.h b/library/cpp/messagebus/actor/ring_buffer.h index 8ca76bdb91..ec5706f7c7 100644 --- a/library/cpp/messagebus/actor/ring_buffer.h +++ b/library/cpp/messagebus/actor/ring_buffer.h @@ -95,11 +95,11 @@ public: } for (size_t i = 0; i < firstSize; ++i) { - Data[WritePos + i] = value[i]; + Data[WritePos + i] = value[i]; } for (size_t i = 0; i < secondSize; ++i) { - Data[i] = value[firstSize + i]; + Data[i] = value[firstSize + i]; } WritePos = (WritePos + value.size()) & CapacityMask; diff --git a/library/cpp/messagebus/actor/ring_buffer_ut.cpp b/library/cpp/messagebus/actor/ring_buffer_ut.cpp index 27926b5f2c..bdb379b3a9 100644 --- a/library/cpp/messagebus/actor/ring_buffer_ut.cpp +++ b/library/cpp/messagebus/actor/ring_buffer_ut.cpp @@ -11,11 +11,11 @@ Y_UNIT_TEST_SUITE(RingBuffer) { unsigned NextPush; unsigned NextPop; - TRingBufferTester() - : NextPush() - , NextPop() - { - } + TRingBufferTester() + : NextPush() + , NextPop() + { + } void Push() { //Cerr << "push " << NextPush << "\n"; diff --git a/library/cpp/messagebus/actor/ring_buffer_with_spin_lock.h b/library/cpp/messagebus/actor/ring_buffer_with_spin_lock.h index 39e1d05506..f0b7cd90e4 100644 --- a/library/cpp/messagebus/actor/ring_buffer_with_spin_lock.h +++ b/library/cpp/messagebus/actor/ring_buffer_with_spin_lock.h @@ -14,8 +14,8 @@ private: public: TRingBufferWithSpinLock() : CachedSize(0) - { - } + { + } void Push(const T& t) { PushAll(t); diff --git a/library/cpp/messagebus/actor/tasks.h b/library/cpp/messagebus/actor/tasks.h index e3b3f0f504..31d35931d2 100644 --- a/library/cpp/messagebus/actor/tasks.h +++ b/library/cpp/messagebus/actor/tasks.h @@ -4,45 +4,45 @@ #include <util/system/yassert.h> namespace NActor { - class TTasks { - enum { - // order of values is important - E_WAITING, - E_RUNNING_NO_TASKS, - E_RUNNING_GOT_TASKS, - }; - - private: - TAtomic State; - - public: - TTasks() - : State(E_WAITING) - { + class TTasks { + enum { + // order of values is important + E_WAITING, + E_RUNNING_NO_TASKS, + E_RUNNING_GOT_TASKS, + }; + + private: + TAtomic State; + + public: + TTasks() + : State(E_WAITING) + { } - // @return true iff caller have to either schedule task or execute it - bool AddTask() { - // High contention case optimization: AtomicGet is cheaper than AtomicSwap. - if (E_RUNNING_GOT_TASKS == AtomicGet(State)) { - return false; - } + // @return true iff caller have to either schedule task or execute it + bool AddTask() { + // High contention case optimization: AtomicGet is cheaper than AtomicSwap. + if (E_RUNNING_GOT_TASKS == AtomicGet(State)) { + return false; + } - TAtomicBase oldState = AtomicSwap(&State, E_RUNNING_GOT_TASKS); - return oldState == E_WAITING; + TAtomicBase oldState = AtomicSwap(&State, E_RUNNING_GOT_TASKS); + return oldState == E_WAITING; } - // called by executor - // @return true iff we have to recheck queues - bool FetchTask() { - TAtomicBase newState = AtomicDecrement(State); - if (newState == E_RUNNING_NO_TASKS) { - return true; - } else if (newState == E_WAITING) { - return false; - } - Y_FAIL("unknown"); - } - }; - -} + // called by executor + // @return true iff we have to recheck queues + bool FetchTask() { + TAtomicBase newState = AtomicDecrement(State); + if (newState == E_RUNNING_NO_TASKS) { + return true; + } else if (newState == E_WAITING) { + return false; + } + Y_FAIL("unknown"); + } + }; + +} diff --git a/library/cpp/messagebus/actor/temp_tls_vector.h b/library/cpp/messagebus/actor/temp_tls_vector.h index 5b94b2126f..675d92f5b0 100644 --- a/library/cpp/messagebus/actor/temp_tls_vector.h +++ b/library/cpp/messagebus/actor/temp_tls_vector.h @@ -5,13 +5,13 @@ #include <util/generic/vector.h> #include <util/system/yassert.h> -template <typename T, typename TTag = void, template <typename, class> class TVectorType = TVector> +template <typename T, typename TTag = void, template <typename, class> class TVectorType = TVector> class TTempTlsVector { private: struct TTagForTls {}; TVectorType<T, std::allocator<T>>* Vector; - + public: TVectorType<T, std::allocator<T>>* GetVector() { return Vector; diff --git a/library/cpp/messagebus/actor/thread_extra.h b/library/cpp/messagebus/actor/thread_extra.h index efd150a0b2..b5aa151618 100644 --- a/library/cpp/messagebus/actor/thread_extra.h +++ b/library/cpp/messagebus/actor/thread_extra.h @@ -3,39 +3,39 @@ #include <util/thread/singleton.h> namespace NTSAN { - template <typename T> - inline void RelaxedStore(volatile T* a, T x) { - static_assert(std::is_integral<T>::value || std::is_pointer<T>::value, "expect std::is_integral<T>::value || std::is_pointer<T>::value"); + template <typename T> + inline void RelaxedStore(volatile T* a, T x) { + static_assert(std::is_integral<T>::value || std::is_pointer<T>::value, "expect std::is_integral<T>::value || std::is_pointer<T>::value"); #ifdef _win_ - *a = x; + *a = x; #else - __atomic_store_n(a, x, __ATOMIC_RELAXED); + __atomic_store_n(a, x, __ATOMIC_RELAXED); #endif - } + } - template <typename T> - inline T RelaxedLoad(volatile T* a) { + template <typename T> + inline T RelaxedLoad(volatile T* a) { #ifdef _win_ - return *a; + return *a; #else - return __atomic_load_n(a, __ATOMIC_RELAXED); + return __atomic_load_n(a, __ATOMIC_RELAXED); #endif - } + } } void SetCurrentThreadName(const char* name); -namespace NThreadExtra { - namespace NPrivate { - template <typename TValue, typename TTag> - struct TValueHolder { - TValue Value; - }; - } -} +namespace NThreadExtra { + namespace NPrivate { + template <typename TValue, typename TTag> + struct TValueHolder { + TValue Value; + }; + } +} template <typename TValue, typename TTag> static inline TValue* FastTlsSingletonWithTag() { - return &FastTlsSingleton< ::NThreadExtra::NPrivate::TValueHolder<TValue, TTag>>()->Value; + return &FastTlsSingleton< ::NThreadExtra::NPrivate::TValueHolder<TValue, TTag>>()->Value; } diff --git a/library/cpp/messagebus/actor/what_thread_does.cpp b/library/cpp/messagebus/actor/what_thread_does.cpp index 5960093a6b..bebb6a888c 100644 --- a/library/cpp/messagebus/actor/what_thread_does.cpp +++ b/library/cpp/messagebus/actor/what_thread_does.cpp @@ -4,19 +4,19 @@ #include <util/system/tls.h> -Y_POD_STATIC_THREAD(const char*) -WhatThreadDoes; +Y_POD_STATIC_THREAD(const char*) +WhatThreadDoes; -const char* PushWhatThreadDoes(const char* what) { +const char* PushWhatThreadDoes(const char* what) { const char* r = NTSAN::RelaxedLoad(&WhatThreadDoes); NTSAN::RelaxedStore(&WhatThreadDoes, what); return r; } -void PopWhatThreadDoes(const char* prev) { +void PopWhatThreadDoes(const char* prev) { NTSAN::RelaxedStore(&WhatThreadDoes, prev); } -const char** WhatThreadDoesLocation() { +const char** WhatThreadDoesLocation() { return &WhatThreadDoes; } diff --git a/library/cpp/messagebus/actor/what_thread_does_guard.h b/library/cpp/messagebus/actor/what_thread_does_guard.h index 4fb0abef6d..f104e9e173 100644 --- a/library/cpp/messagebus/actor/what_thread_does_guard.h +++ b/library/cpp/messagebus/actor/what_thread_does_guard.h @@ -4,37 +4,37 @@ template <class T> class TWhatThreadDoesAcquireGuard: public TNonCopyable { -public: - inline TWhatThreadDoesAcquireGuard(const T& t, const char* acquire) noexcept { - Init(&t, acquire); - } - - inline TWhatThreadDoesAcquireGuard(const T* t, const char* acquire) noexcept { - Init(t, acquire); - } - - inline ~TWhatThreadDoesAcquireGuard() { - Release(); - } - - inline void Release() noexcept { - if (WasAcquired()) { - const_cast<T*>(T_)->Release(); - T_ = nullptr; +public: + inline TWhatThreadDoesAcquireGuard(const T& t, const char* acquire) noexcept { + Init(&t, acquire); + } + + inline TWhatThreadDoesAcquireGuard(const T* t, const char* acquire) noexcept { + Init(t, acquire); + } + + inline ~TWhatThreadDoesAcquireGuard() { + Release(); + } + + inline void Release() noexcept { + if (WasAcquired()) { + const_cast<T*>(T_)->Release(); + T_ = nullptr; } - } + } - inline bool WasAcquired() const noexcept { - return T_ != nullptr; - } + inline bool WasAcquired() const noexcept { + return T_ != nullptr; + } -private: - inline void Init(const T* t, const char* acquire) noexcept { - T_ = const_cast<T*>(t); - TWhatThreadDoesPushPop pp(acquire); - T_->Acquire(); - } +private: + inline void Init(const T* t, const char* acquire) noexcept { + T_ = const_cast<T*>(t); + TWhatThreadDoesPushPop pp(acquire); + T_->Acquire(); + } -private: - T* T_; +private: + T* T_; }; diff --git a/library/cpp/messagebus/async_result.h b/library/cpp/messagebus/async_result.h index 3ae910796f..d24dde284a 100644 --- a/library/cpp/messagebus/async_result.h +++ b/library/cpp/messagebus/async_result.h @@ -42,7 +42,7 @@ public: } template <typename TFunc> - void AndThen(const TFunc& onResult) { + void AndThen(const TFunc& onResult) { TGuard<TMutex> guard(Mutex); if (!!Result) { onResult(*Result); diff --git a/library/cpp/messagebus/base.h b/library/cpp/messagebus/base.h index 78d9c39e4d..79fccc312e 100644 --- a/library/cpp/messagebus/base.h +++ b/library/cpp/messagebus/base.h @@ -3,9 +3,9 @@ #include <util/system/defaults.h> namespace NBus { - /// millis since epoch - using TBusInstant = ui64; - /// returns time in milliseconds - TBusInstant Now(); + /// millis since epoch + using TBusInstant = ui64; + /// returns time in milliseconds + TBusInstant Now(); } diff --git a/library/cpp/messagebus/cc_semaphore.h b/library/cpp/messagebus/cc_semaphore.h index 34ec933184..0df8a3d664 100644 --- a/library/cpp/messagebus/cc_semaphore.h +++ b/library/cpp/messagebus/cc_semaphore.h @@ -25,9 +25,9 @@ public: } } - bool IsLocked() { - return Latch.IsLocked(); - } + bool IsLocked() { + return Latch.IsLocked(); + } private: TThis* GetThis() { diff --git a/library/cpp/messagebus/cc_semaphore_ut.cpp b/library/cpp/messagebus/cc_semaphore_ut.cpp index 0788e763df..206bb7c96a 100644 --- a/library/cpp/messagebus/cc_semaphore_ut.cpp +++ b/library/cpp/messagebus/cc_semaphore_ut.cpp @@ -5,13 +5,13 @@ #include <util/system/atomic.h> namespace { - struct TTestSemaphore: public TComplexConditionSemaphore<TTestSemaphore> { + struct TTestSemaphore: public TComplexConditionSemaphore<TTestSemaphore> { TAtomic Current; - TTestSemaphore() - : Current(0) - { - } + TTestSemaphore() + : Current(0) + { + } bool TryWait() { return AtomicGet(Current) > 0; diff --git a/library/cpp/messagebus/config/defs.h b/library/cpp/messagebus/config/defs.h index ad644cd279..92b1df9969 100644 --- a/library/cpp/messagebus/config/defs.h +++ b/library/cpp/messagebus/config/defs.h @@ -1,5 +1,5 @@ #pragma once - + // unique tag to fix pragma once gcc glueing: ./library/cpp/messagebus/defs.h #include "codegen.h" @@ -18,65 +18,65 @@ class TCrawlerModule; struct TDebugReceiverHandler; namespace NBus { - namespace NPrivate { - class TAcceptor; - struct TBusSessionImpl; - class TRemoteServerSession; - class TRemoteClientSession; - class TRemoteConnection; - class TRemoteServerConnection; - class TRemoteClientConnection; - class TBusSyncSourceSessionImpl; + namespace NPrivate { + class TAcceptor; + struct TBusSessionImpl; + class TRemoteServerSession; + class TRemoteClientSession; + class TRemoteConnection; + class TRemoteServerConnection; + class TRemoteClientConnection; + class TBusSyncSourceSessionImpl; - struct TBusMessagePtrAndHeader; + struct TBusMessagePtrAndHeader; - struct TSessionDumpStatus; + struct TSessionDumpStatus; - struct TClientRequestImpl; + struct TClientRequestImpl; - } + } - class TBusSession; - struct TBusServerSession; - struct TBusClientSession; - class TBusProtocol; - class TBusMessage; - class TBusMessageConnection; - class TBusMessageQueue; - class TBusLocator; - struct TBusQueueConfig; - struct TBusSessionConfig; - struct TBusHeader; + class TBusSession; + struct TBusServerSession; + struct TBusClientSession; + class TBusProtocol; + class TBusMessage; + class TBusMessageConnection; + class TBusMessageQueue; + class TBusLocator; + struct TBusQueueConfig; + struct TBusSessionConfig; + struct TBusHeader; - class IThreadHandler; + class IThreadHandler; - using TBusKey = ui64; - using TBusMessageList = TList<TBusMessage*>; - using TBusKeyVec = TVector<std::pair<TBusKey, TBusKey>>; + using TBusKey = ui64; + using TBusMessageList = TList<TBusMessage*>; + using TBusKeyVec = TVector<std::pair<TBusKey, TBusKey>>; - using TBusMessageQueuePtr = TIntrusivePtr<TBusMessageQueue>; + using TBusMessageQueuePtr = TIntrusivePtr<TBusMessageQueue>; - class TBusModule; + class TBusModule; - using TBusData = TString; - using TBusService = const char*; + using TBusData = TString; + using TBusService = const char*; -#define YBUS_KEYMIN TBusKey(0L) -#define YBUS_KEYMAX TBusKey(-1L) -#define YBUS_KEYLOCAL TBusKey(7L) +#define YBUS_KEYMIN TBusKey(0L) +#define YBUS_KEYMAX TBusKey(-1L) +#define YBUS_KEYLOCAL TBusKey(7L) #define YBUS_KEYINVALID TBusKey(99999999L) - // Check that generated id is valid for remote message - inline bool IsBusKeyValid(TBusKey key) { - return key != YBUS_KEYINVALID && key != YBUS_KEYMAX && key > YBUS_KEYLOCAL; - } + // Check that generated id is valid for remote message + inline bool IsBusKeyValid(TBusKey key) { + return key != YBUS_KEYINVALID && key != YBUS_KEYMAX && key > YBUS_KEYLOCAL; + } -#define YBUS_VERSION 0 +#define YBUS_VERSION 0 -#define YBUS_INFINITE (1u << 30u) +#define YBUS_INFINITE (1u << 30u) -#define YBUS_STATUS_BASIC 0x0000 -#define YBUS_STATUS_CONNS 0x0001 +#define YBUS_STATUS_BASIC 0x0000 +#define YBUS_STATUS_CONNS 0x0001 #define YBUS_STATUS_INFLIGHT 0x0002 -} +} diff --git a/library/cpp/messagebus/config/netaddr.cpp b/library/cpp/messagebus/config/netaddr.cpp index 73081240a0..962ac538e2 100644 --- a/library/cpp/messagebus/config/netaddr.cpp +++ b/library/cpp/messagebus/config/netaddr.cpp @@ -5,175 +5,175 @@ #include <cstdlib> namespace NBus { - const char* ToCString(EIpVersion ipVersion) { - switch (ipVersion) { - case EIP_VERSION_ANY: - return "EIP_VERSION_ANY"; - case EIP_VERSION_4: - return "EIP_VERSION_4"; - case EIP_VERSION_6: - return "EIP_VERSION_6"; - } - Y_FAIL(); - } - - int ToAddrFamily(EIpVersion ipVersion) { - switch (ipVersion) { - case EIP_VERSION_ANY: - return AF_UNSPEC; - case EIP_VERSION_4: - return AF_INET; - case EIP_VERSION_6: - return AF_INET6; - } - Y_FAIL(); - } - - class TNetworkAddressRef: private TNetworkAddress, public TAddrInfo { - public: - TNetworkAddressRef(const TNetworkAddress& na, const TAddrInfo& ai) - : TNetworkAddress(na) - , TAddrInfo(ai) - { - } - }; - - static bool Compare(const IRemoteAddr& l, const IRemoteAddr& r) noexcept { - if (l.Addr()->sa_family != r.Addr()->sa_family) { - return false; - } - - switch (l.Addr()->sa_family) { - case AF_INET: { - return memcmp(&(((const sockaddr_in*)l.Addr())->sin_addr), &(((const sockaddr_in*)r.Addr())->sin_addr), sizeof(in_addr)) == 0 && - ((const sockaddr_in*)l.Addr())->sin_port == ((const sockaddr_in*)r.Addr())->sin_port; - } - - case AF_INET6: { - return memcmp(&(((const sockaddr_in6*)l.Addr())->sin6_addr), &(((const sockaddr_in6*)r.Addr())->sin6_addr), sizeof(in6_addr)) == 0 && - ((const sockaddr_in6*)l.Addr())->sin6_port == ((const sockaddr_in6*)r.Addr())->sin6_port; - } + const char* ToCString(EIpVersion ipVersion) { + switch (ipVersion) { + case EIP_VERSION_ANY: + return "EIP_VERSION_ANY"; + case EIP_VERSION_4: + return "EIP_VERSION_4"; + case EIP_VERSION_6: + return "EIP_VERSION_6"; } + Y_FAIL(); + } + + int ToAddrFamily(EIpVersion ipVersion) { + switch (ipVersion) { + case EIP_VERSION_ANY: + return AF_UNSPEC; + case EIP_VERSION_4: + return AF_INET; + case EIP_VERSION_6: + return AF_INET6; + } + Y_FAIL(); + } + + class TNetworkAddressRef: private TNetworkAddress, public TAddrInfo { + public: + TNetworkAddressRef(const TNetworkAddress& na, const TAddrInfo& ai) + : TNetworkAddress(na) + , TAddrInfo(ai) + { + } + }; + + static bool Compare(const IRemoteAddr& l, const IRemoteAddr& r) noexcept { + if (l.Addr()->sa_family != r.Addr()->sa_family) { + return false; + } + + switch (l.Addr()->sa_family) { + case AF_INET: { + return memcmp(&(((const sockaddr_in*)l.Addr())->sin_addr), &(((const sockaddr_in*)r.Addr())->sin_addr), sizeof(in_addr)) == 0 && + ((const sockaddr_in*)l.Addr())->sin_port == ((const sockaddr_in*)r.Addr())->sin_port; + } + + case AF_INET6: { + return memcmp(&(((const sockaddr_in6*)l.Addr())->sin6_addr), &(((const sockaddr_in6*)r.Addr())->sin6_addr), sizeof(in6_addr)) == 0 && + ((const sockaddr_in6*)l.Addr())->sin6_port == ((const sockaddr_in6*)r.Addr())->sin6_port; + } + } + + return memcmp(l.Addr(), r.Addr(), Min<size_t>(l.Len(), r.Len())) == 0; + } + + TNetAddr::TNetAddr() + : Ptr(new TOpaqueAddr) + { + } - return memcmp(l.Addr(), r.Addr(), Min<size_t>(l.Len(), r.Len())) == 0; - } - - TNetAddr::TNetAddr() - : Ptr(new TOpaqueAddr) - { - } - - TNetAddr::TNetAddr(TAutoPtr<IRemoteAddr> addr) - : Ptr(addr) - { - Y_VERIFY(!!Ptr); - } - - namespace { - using namespace NAddr; - - const char* Describe(EIpVersion version) { - switch (version) { - case EIP_VERSION_4: - return "ipv4 address"; - case EIP_VERSION_6: - return "ipv6 address"; - case EIP_VERSION_ANY: - return "any address"; - default: - Y_FAIL("unreachable"); - } + TNetAddr::TNetAddr(TAutoPtr<IRemoteAddr> addr) + : Ptr(addr) + { + Y_VERIFY(!!Ptr); + } + + namespace { + using namespace NAddr; + + const char* Describe(EIpVersion version) { + switch (version) { + case EIP_VERSION_4: + return "ipv4 address"; + case EIP_VERSION_6: + return "ipv6 address"; + case EIP_VERSION_ANY: + return "any address"; + default: + Y_FAIL("unreachable"); + } } - TAutoPtr<IRemoteAddr> MakeAddress(const TNetworkAddress& na, EIpVersion requireVersion, EIpVersion preferVersion) { - TAutoPtr<IRemoteAddr> addr; - for (TNetworkAddress::TIterator it = na.Begin(); it != na.End(); ++it) { - if (IsFamilyAllowed(it->ai_family, requireVersion)) { - if (IsFamilyAllowed(it->ai_family, preferVersion)) { - return new TNetworkAddressRef(na, &*it); - } else if (!addr) { - addr.Reset(new TNetworkAddressRef(na, &*it)); - } + TAutoPtr<IRemoteAddr> MakeAddress(const TNetworkAddress& na, EIpVersion requireVersion, EIpVersion preferVersion) { + TAutoPtr<IRemoteAddr> addr; + for (TNetworkAddress::TIterator it = na.Begin(); it != na.End(); ++it) { + if (IsFamilyAllowed(it->ai_family, requireVersion)) { + if (IsFamilyAllowed(it->ai_family, preferVersion)) { + return new TNetworkAddressRef(na, &*it); + } else if (!addr) { + addr.Reset(new TNetworkAddressRef(na, &*it)); + } } } - return addr; + return addr; } - TAutoPtr<IRemoteAddr> MakeAddress(TStringBuf host, int port, EIpVersion requireVersion, EIpVersion preferVersion) { - TString hostString(host); - TNetworkAddress na(hostString, port); - return MakeAddress(na, requireVersion, preferVersion); - } - TAutoPtr<IRemoteAddr> MakeAddress(const char* hostPort, EIpVersion requireVersion, EIpVersion preferVersion) { - const char* portStr = strchr(hostPort, ':'); - if (!portStr) { - ythrow TNetAddr::TError() << "port not specified in " << hostPort; - } - int port = atoi(portStr + 1); - TNetworkAddress na(TString(hostPort, portStr), port); - return MakeAddress(na, requireVersion, preferVersion); - } - } - - TNetAddr::TNetAddr(const char* hostPort, EIpVersion requireVersion /*= EIP_VERSION_ANY*/, EIpVersion preferVersion /*= EIP_VERSION_ANY*/) - : Ptr(MakeAddress(hostPort, requireVersion, preferVersion)) - { - if (!Ptr) { - ythrow TNetAddr::TError() << "cannot resolve " << hostPort << " into " << Describe(requireVersion); - } - } - - TNetAddr::TNetAddr(TStringBuf host, int port, EIpVersion requireVersion /*= EIP_VERSION_ANY*/, EIpVersion preferVersion /*= EIP_VERSION_ANY*/) - : Ptr(MakeAddress(host, port, requireVersion, preferVersion)) - { - if (!Ptr) { - ythrow TNetAddr::TError() << "cannot resolve " << host << ":" << port << " into " << Describe(requireVersion); + TAutoPtr<IRemoteAddr> MakeAddress(TStringBuf host, int port, EIpVersion requireVersion, EIpVersion preferVersion) { + TString hostString(host); + TNetworkAddress na(hostString, port); + return MakeAddress(na, requireVersion, preferVersion); + } + TAutoPtr<IRemoteAddr> MakeAddress(const char* hostPort, EIpVersion requireVersion, EIpVersion preferVersion) { + const char* portStr = strchr(hostPort, ':'); + if (!portStr) { + ythrow TNetAddr::TError() << "port not specified in " << hostPort; + } + int port = atoi(portStr + 1); + TNetworkAddress na(TString(hostPort, portStr), port); + return MakeAddress(na, requireVersion, preferVersion); } } - TNetAddr::TNetAddr(const TNetworkAddress& na, EIpVersion requireVersion /*= EIP_VERSION_ANY*/, EIpVersion preferVersion /*= EIP_VERSION_ANY*/) - : Ptr(MakeAddress(na, requireVersion, preferVersion)) - { - if (!Ptr) { - ythrow TNetAddr::TError() << "cannot resolve into " << Describe(requireVersion); - } + TNetAddr::TNetAddr(const char* hostPort, EIpVersion requireVersion /*= EIP_VERSION_ANY*/, EIpVersion preferVersion /*= EIP_VERSION_ANY*/) + : Ptr(MakeAddress(hostPort, requireVersion, preferVersion)) + { + if (!Ptr) { + ythrow TNetAddr::TError() << "cannot resolve " << hostPort << " into " << Describe(requireVersion); + } } - TNetAddr::TNetAddr(const TNetworkAddress& na, const TAddrInfo& ai) - : Ptr(new TNetworkAddressRef(na, ai)) - { + TNetAddr::TNetAddr(TStringBuf host, int port, EIpVersion requireVersion /*= EIP_VERSION_ANY*/, EIpVersion preferVersion /*= EIP_VERSION_ANY*/) + : Ptr(MakeAddress(host, port, requireVersion, preferVersion)) + { + if (!Ptr) { + ythrow TNetAddr::TError() << "cannot resolve " << host << ":" << port << " into " << Describe(requireVersion); + } } - const sockaddr* TNetAddr::Addr() const { - return Ptr->Addr(); + TNetAddr::TNetAddr(const TNetworkAddress& na, EIpVersion requireVersion /*= EIP_VERSION_ANY*/, EIpVersion preferVersion /*= EIP_VERSION_ANY*/) + : Ptr(MakeAddress(na, requireVersion, preferVersion)) + { + if (!Ptr) { + ythrow TNetAddr::TError() << "cannot resolve into " << Describe(requireVersion); + } } - socklen_t TNetAddr::Len() const { - return Ptr->Len(); - } + TNetAddr::TNetAddr(const TNetworkAddress& na, const TAddrInfo& ai) + : Ptr(new TNetworkAddressRef(na, ai)) + { + } - int TNetAddr::GetPort() const { - switch (Ptr->Addr()->sa_family) { - case AF_INET: - return InetToHost(((sockaddr_in*)Ptr->Addr())->sin_port); - case AF_INET6: - return InetToHost(((sockaddr_in6*)Ptr->Addr())->sin6_port); - default: - Y_FAIL("unknown AF: %d", (int)Ptr->Addr()->sa_family); - throw 1; - } - } + const sockaddr* TNetAddr::Addr() const { + return Ptr->Addr(); + } + + socklen_t TNetAddr::Len() const { + return Ptr->Len(); + } + + int TNetAddr::GetPort() const { + switch (Ptr->Addr()->sa_family) { + case AF_INET: + return InetToHost(((sockaddr_in*)Ptr->Addr())->sin_port); + case AF_INET6: + return InetToHost(((sockaddr_in6*)Ptr->Addr())->sin6_port); + default: + Y_FAIL("unknown AF: %d", (int)Ptr->Addr()->sa_family); + throw 1; + } + } - bool TNetAddr::IsIpv4() const { - return Ptr->Addr()->sa_family == AF_INET; - } + bool TNetAddr::IsIpv4() const { + return Ptr->Addr()->sa_family == AF_INET; + } - bool TNetAddr::IsIpv6() const { - return Ptr->Addr()->sa_family == AF_INET6; + bool TNetAddr::IsIpv6() const { + return Ptr->Addr()->sa_family == AF_INET6; } - bool TNetAddr::operator==(const TNetAddr& rhs) const { - return Ptr == rhs.Ptr || Compare(*Ptr, *rhs.Ptr); - } + bool TNetAddr::operator==(const TNetAddr& rhs) const { + return Ptr == rhs.Ptr || Compare(*Ptr, *rhs.Ptr); + } } diff --git a/library/cpp/messagebus/config/netaddr.h b/library/cpp/messagebus/config/netaddr.h index 839f3d8f5d..b79c0cc355 100644 --- a/library/cpp/messagebus/config/netaddr.h +++ b/library/cpp/messagebus/config/netaddr.h @@ -8,79 +8,79 @@ #include <util/network/address.h> namespace NBus { - using namespace NAddr; - - /// IP protocol version. - enum EIpVersion { - EIP_VERSION_4 = 1, - EIP_VERSION_6 = 2, - EIP_VERSION_ANY = EIP_VERSION_4 | EIP_VERSION_6, - }; - - inline bool IsFamilyAllowed(ui16 sa_family, EIpVersion ipVersion) { - if (ipVersion == EIP_VERSION_4 && sa_family != AF_INET) { - return false; - } - if (ipVersion == EIP_VERSION_6 && sa_family != AF_INET6) { - return false; - } - return true; + using namespace NAddr; + + /// IP protocol version. + enum EIpVersion { + EIP_VERSION_4 = 1, + EIP_VERSION_6 = 2, + EIP_VERSION_ANY = EIP_VERSION_4 | EIP_VERSION_6, + }; + + inline bool IsFamilyAllowed(ui16 sa_family, EIpVersion ipVersion) { + if (ipVersion == EIP_VERSION_4 && sa_family != AF_INET) { + return false; + } + if (ipVersion == EIP_VERSION_6 && sa_family != AF_INET6) { + return false; + } + return true; } - const char* ToCString(EIpVersion); - int ToAddrFamily(EIpVersion); - - /// Hold referenced pointer to address description structure (ex. sockaddr_storage) - /// It's make possible to work with IPv4 / IPv6 addresses simultaneously - class TNetAddr: public IRemoteAddr { - public: - class TError: public yexception { - }; - - TNetAddr(); - TNetAddr(TAutoPtr<IRemoteAddr> addr); - TNetAddr(const char* hostPort, EIpVersion requireVersion = EIP_VERSION_ANY, EIpVersion preferVersion = EIP_VERSION_ANY); - TNetAddr(TStringBuf host, int port, EIpVersion requireVersion = EIP_VERSION_ANY, EIpVersion preferVersion = EIP_VERSION_ANY); - TNetAddr(const TNetworkAddress& na, EIpVersion requireVersion = EIP_VERSION_ANY, EIpVersion preferVersion = EIP_VERSION_ANY); - TNetAddr(const TNetworkAddress& na, const TAddrInfo& ai); - - bool operator==(const TNetAddr&) const; - bool operator!=(const TNetAddr& other) const { - return !(*this == other); - } - inline explicit operator bool() const noexcept { - return !!Ptr; - } - - const sockaddr* Addr() const override; - socklen_t Len() const override; - - bool IsIpv4() const; - bool IsIpv6() const; - int GetPort() const; - - private: - TAtomicSharedPtr<IRemoteAddr> Ptr; - }; - - using TSockAddrInVector = TVector<TNetAddr>; - - struct TNetAddrHostPortHash { - inline size_t operator()(const TNetAddr& a) const { - const sockaddr* s = a.Addr(); - const sockaddr_in* const sa = reinterpret_cast<const sockaddr_in*>(s); - const sockaddr_in6* const sa6 = reinterpret_cast<const sockaddr_in6*>(s); - - switch (s->sa_family) { - case AF_INET: + const char* ToCString(EIpVersion); + int ToAddrFamily(EIpVersion); + + /// Hold referenced pointer to address description structure (ex. sockaddr_storage) + /// It's make possible to work with IPv4 / IPv6 addresses simultaneously + class TNetAddr: public IRemoteAddr { + public: + class TError: public yexception { + }; + + TNetAddr(); + TNetAddr(TAutoPtr<IRemoteAddr> addr); + TNetAddr(const char* hostPort, EIpVersion requireVersion = EIP_VERSION_ANY, EIpVersion preferVersion = EIP_VERSION_ANY); + TNetAddr(TStringBuf host, int port, EIpVersion requireVersion = EIP_VERSION_ANY, EIpVersion preferVersion = EIP_VERSION_ANY); + TNetAddr(const TNetworkAddress& na, EIpVersion requireVersion = EIP_VERSION_ANY, EIpVersion preferVersion = EIP_VERSION_ANY); + TNetAddr(const TNetworkAddress& na, const TAddrInfo& ai); + + bool operator==(const TNetAddr&) const; + bool operator!=(const TNetAddr& other) const { + return !(*this == other); + } + inline explicit operator bool() const noexcept { + return !!Ptr; + } + + const sockaddr* Addr() const override; + socklen_t Len() const override; + + bool IsIpv4() const; + bool IsIpv6() const; + int GetPort() const; + + private: + TAtomicSharedPtr<IRemoteAddr> Ptr; + }; + + using TSockAddrInVector = TVector<TNetAddr>; + + struct TNetAddrHostPortHash { + inline size_t operator()(const TNetAddr& a) const { + const sockaddr* s = a.Addr(); + const sockaddr_in* const sa = reinterpret_cast<const sockaddr_in*>(s); + const sockaddr_in6* const sa6 = reinterpret_cast<const sockaddr_in6*>(s); + + switch (s->sa_family) { + case AF_INET: return CombineHashes<size_t>(ComputeHash(TStringBuf(reinterpret_cast<const char*>(&sa->sin_addr), sizeof(sa->sin_addr))), IntHashImpl(sa->sin_port)); - case AF_INET6: + case AF_INET6: return CombineHashes<size_t>(ComputeHash(TStringBuf(reinterpret_cast<const char*>(&sa6->sin6_addr), sizeof(sa6->sin6_addr))), IntHashImpl(sa6->sin6_port)); - } + } return ComputeHash(TStringBuf(reinterpret_cast<const char*>(s), a.Len())); } - }; + }; -} +} diff --git a/library/cpp/messagebus/config/session_config.cpp b/library/cpp/messagebus/config/session_config.cpp index 31c7dd29f2..fbbbb106c9 100644 --- a/library/cpp/messagebus/config/session_config.cpp +++ b/library/cpp/messagebus/config/session_config.cpp @@ -13,8 +13,8 @@ TBusSessionConfig::TSecret::TSecret() TBusSessionConfig::TBusSessionConfig() : BUS_SESSION_CONFIG_MAP(STRUCT_FIELD_INIT, COMMA) -{ -} +{ +} TString TBusSessionConfig::PrintToString() const { TStringStream ss; @@ -40,10 +40,10 @@ static int ParseToSForMessageBus(const char* option) { return tos; } -template <class T> -static T ParseWithKmgSuffixT(const char* option) { +template <class T> +static T ParseWithKmgSuffixT(const char* option) { TStringBuf str(option); - T multiplier = 1; + T multiplier = 1; if (str.EndsWith('k')) { multiplier = 1024; str = str.Head(str.size() - 1); @@ -54,104 +54,104 @@ static T ParseWithKmgSuffixT(const char* option) { multiplier = 1024 * 1024 * 1024; str = str.Head(str.size() - 1); } - return FromString<T>(str) * multiplier; + return FromString<T>(str) * multiplier; +} + +static ui64 ParseWithKmgSuffix(const char* option) { + return ParseWithKmgSuffixT<ui64>(option); +} + +static i64 ParseWithKmgSuffixS(const char* option) { + return ParseWithKmgSuffixT<i64>(option); } -static ui64 ParseWithKmgSuffix(const char* option) { - return ParseWithKmgSuffixT<ui64>(option); -} - -static i64 ParseWithKmgSuffixS(const char* option) { - return ParseWithKmgSuffixT<i64>(option); -} - void TBusSessionConfig::ConfigureLastGetopt(NLastGetopt::TOpts& opts, const TString& prefix) { opts.AddLongOption(prefix + "total-timeout") - .RequiredArgument("MILLISECONDS") - .DefaultValue(ToString(TotalTimeout)) + .RequiredArgument("MILLISECONDS") + .DefaultValue(ToString(TotalTimeout)) .StoreMappedResultT<const char*>(&TotalTimeout, &ParseDurationForMessageBus); opts.AddLongOption(prefix + "connect-timeout") - .RequiredArgument("MILLISECONDS") - .DefaultValue(ToString(ConnectTimeout)) + .RequiredArgument("MILLISECONDS") + .DefaultValue(ToString(ConnectTimeout)) .StoreMappedResultT<const char*>(&ConnectTimeout, &ParseDurationForMessageBus); opts.AddLongOption(prefix + "send-timeout") - .RequiredArgument("MILLISECONDS") - .DefaultValue(ToString(SendTimeout)) + .RequiredArgument("MILLISECONDS") + .DefaultValue(ToString(SendTimeout)) .StoreMappedResultT<const char*>(&SendTimeout, &ParseDurationForMessageBus); opts.AddLongOption(prefix + "send-threshold") - .RequiredArgument("BYTES") - .DefaultValue(ToString(SendThreshold)) + .RequiredArgument("BYTES") + .DefaultValue(ToString(SendThreshold)) .StoreMappedResultT<const char*>(&SendThreshold, &ParseWithKmgSuffix); opts.AddLongOption(prefix + "max-in-flight") - .RequiredArgument("COUNT") - .DefaultValue(ToString(MaxInFlight)) + .RequiredArgument("COUNT") + .DefaultValue(ToString(MaxInFlight)) .StoreMappedResultT<const char*>(&MaxInFlight, &ParseWithKmgSuffix); opts.AddLongOption(prefix + "max-in-flight-by-size") - .RequiredArgument("BYTES") - .DefaultValue( - ToString(MaxInFlightBySize)) - .StoreMappedResultT<const char*>(&MaxInFlightBySize, &ParseWithKmgSuffixS); + .RequiredArgument("BYTES") + .DefaultValue( + ToString(MaxInFlightBySize)) + .StoreMappedResultT<const char*>(&MaxInFlightBySize, &ParseWithKmgSuffixS); opts.AddLongOption(prefix + "per-con-max-in-flight") - .RequiredArgument("COUNT") - .DefaultValue(ToString(PerConnectionMaxInFlight)) + .RequiredArgument("COUNT") + .DefaultValue(ToString(PerConnectionMaxInFlight)) .StoreMappedResultT<const char*>(&PerConnectionMaxInFlight, &ParseWithKmgSuffix); opts.AddLongOption(prefix + "per-con-max-in-flight-by-size") - .RequiredArgument("BYTES") - .DefaultValue( - ToString(PerConnectionMaxInFlightBySize)) + .RequiredArgument("BYTES") + .DefaultValue( + ToString(PerConnectionMaxInFlightBySize)) .StoreMappedResultT<const char*>(&PerConnectionMaxInFlightBySize, &ParseWithKmgSuffix); opts.AddLongOption(prefix + "default-buffer-size") - .RequiredArgument("BYTES") - .DefaultValue(ToString(DefaultBufferSize)) + .RequiredArgument("BYTES") + .DefaultValue(ToString(DefaultBufferSize)) .StoreMappedResultT<const char*>(&DefaultBufferSize, &ParseWithKmgSuffix); opts.AddLongOption(prefix + "max-buffer-size") - .RequiredArgument("BYTES") - .DefaultValue(ToString(MaxBufferSize)) + .RequiredArgument("BYTES") + .DefaultValue(ToString(MaxBufferSize)) .StoreMappedResultT<const char*>(&MaxBufferSize, &ParseWithKmgSuffix); opts.AddLongOption(prefix + "max-message-size") - .RequiredArgument("BYTES") - .DefaultValue(ToString(MaxMessageSize)) + .RequiredArgument("BYTES") + .DefaultValue(ToString(MaxMessageSize)) .StoreMappedResultT<const char*>(&MaxMessageSize, &ParseWithKmgSuffix); opts.AddLongOption(prefix + "socket-recv-buffer-size") - .RequiredArgument("BYTES") - .DefaultValue(ToString(SocketRecvBufferSize)) + .RequiredArgument("BYTES") + .DefaultValue(ToString(SocketRecvBufferSize)) .StoreMappedResultT<const char*>(&SocketRecvBufferSize, &ParseWithKmgSuffix); opts.AddLongOption(prefix + "socket-send-buffer-size") - .RequiredArgument("BYTES") - .DefaultValue(ToString(SocketSendBufferSize)) + .RequiredArgument("BYTES") + .DefaultValue(ToString(SocketSendBufferSize)) .StoreMappedResultT<const char*>(&SocketSendBufferSize, &ParseWithKmgSuffix); opts.AddLongOption(prefix + "socket-tos") .RequiredArgument("[0x00, 0xFF]") - .StoreMappedResultT<const char*>(&SocketToS, &ParseToSForMessageBus); - ; + .StoreMappedResultT<const char*>(&SocketToS, &ParseToSForMessageBus); + ; opts.AddLongOption(prefix + "tcp-cork") - .RequiredArgument("BOOL") - .DefaultValue(ToString(TcpCork)) + .RequiredArgument("BOOL") + .DefaultValue(ToString(TcpCork)) .StoreResult(&TcpCork); opts.AddLongOption(prefix + "cork") - .RequiredArgument("SECONDS") - .DefaultValue( - ToString(Cork.Seconds())) + .RequiredArgument("SECONDS") + .DefaultValue( + ToString(Cork.Seconds())) .StoreMappedResultT<const char*>(&Cork, &TDuration::Parse); opts.AddLongOption(prefix + "on-message-in-pool") - .RequiredArgument("BOOL") - .DefaultValue(ToString(ExecuteOnMessageInWorkerPool)) + .RequiredArgument("BOOL") + .DefaultValue(ToString(ExecuteOnMessageInWorkerPool)) .StoreResult(&ExecuteOnMessageInWorkerPool); opts.AddLongOption(prefix + "on-reply-in-pool") - .RequiredArgument("BOOL") - .DefaultValue(ToString(ExecuteOnReplyInWorkerPool)) + .RequiredArgument("BOOL") + .DefaultValue(ToString(ExecuteOnReplyInWorkerPool)) .StoreResult(&ExecuteOnReplyInWorkerPool); } diff --git a/library/cpp/messagebus/config/session_config.h b/library/cpp/messagebus/config/session_config.h index db17018bbc..84753350a9 100644 --- a/library/cpp/messagebus/config/session_config.h +++ b/library/cpp/messagebus/config/session_config.h @@ -8,58 +8,58 @@ #include <util/generic/string.h> namespace NBus { -#define BUS_SESSION_CONFIG_MAP(XX, comma) \ - XX(Name, TString, "") \ - comma \ - XX(NumRetries, int, 0) comma \ - XX(RetryInterval, int, 1000) comma \ - XX(ReconnectWhenIdle, bool, false) comma \ - XX(MaxInFlight, i64, 1000) comma \ - XX(PerConnectionMaxInFlight, unsigned, 0) comma \ - XX(PerConnectionMaxInFlightBySize, unsigned, 0) comma \ - XX(MaxInFlightBySize, i64, -1) comma \ - XX(TotalTimeout, i64, 0) comma \ - XX(SendTimeout, i64, 0) comma \ - XX(ConnectTimeout, i64, 0) comma \ - XX(DefaultBufferSize, size_t, 10 * 1024) comma \ - XX(MaxBufferSize, size_t, 1024 * 1024) comma \ - XX(SocketRecvBufferSize, unsigned, 0) comma \ - XX(SocketSendBufferSize, unsigned, 0) comma \ - XX(SocketToS, int, -1) comma \ - XX(SendThreshold, size_t, 10 * 1024) comma \ - XX(Cork, TDuration, TDuration::Zero()) comma \ - XX(MaxMessageSize, unsigned, 26 << 20) comma \ - XX(TcpNoDelay, bool, false) comma \ - XX(TcpCork, bool, false) comma \ - XX(ExecuteOnMessageInWorkerPool, bool, true) comma \ - XX(ExecuteOnReplyInWorkerPool, bool, true) comma \ - XX(ReusePort, bool, false) comma \ - XX(ListenPort, unsigned, 0) /* TODO: server only */ +#define BUS_SESSION_CONFIG_MAP(XX, comma) \ + XX(Name, TString, "") \ + comma \ + XX(NumRetries, int, 0) comma \ + XX(RetryInterval, int, 1000) comma \ + XX(ReconnectWhenIdle, bool, false) comma \ + XX(MaxInFlight, i64, 1000) comma \ + XX(PerConnectionMaxInFlight, unsigned, 0) comma \ + XX(PerConnectionMaxInFlightBySize, unsigned, 0) comma \ + XX(MaxInFlightBySize, i64, -1) comma \ + XX(TotalTimeout, i64, 0) comma \ + XX(SendTimeout, i64, 0) comma \ + XX(ConnectTimeout, i64, 0) comma \ + XX(DefaultBufferSize, size_t, 10 * 1024) comma \ + XX(MaxBufferSize, size_t, 1024 * 1024) comma \ + XX(SocketRecvBufferSize, unsigned, 0) comma \ + XX(SocketSendBufferSize, unsigned, 0) comma \ + XX(SocketToS, int, -1) comma \ + XX(SendThreshold, size_t, 10 * 1024) comma \ + XX(Cork, TDuration, TDuration::Zero()) comma \ + XX(MaxMessageSize, unsigned, 26 << 20) comma \ + XX(TcpNoDelay, bool, false) comma \ + XX(TcpCork, bool, false) comma \ + XX(ExecuteOnMessageInWorkerPool, bool, true) comma \ + XX(ExecuteOnReplyInWorkerPool, bool, true) comma \ + XX(ReusePort, bool, false) comma \ + XX(ListenPort, unsigned, 0) /* TODO: server only */ - //////////////////////////////////////////////////////////////////// - /// \brief Configuration for client and server session - struct TBusSessionConfig { - BUS_SESSION_CONFIG_MAP(STRUCT_FIELD_GEN, ) + //////////////////////////////////////////////////////////////////// + /// \brief Configuration for client and server session + struct TBusSessionConfig { + BUS_SESSION_CONFIG_MAP(STRUCT_FIELD_GEN, ) - struct TSecret { - TDuration TimeoutPeriod; - TDuration StatusFlushPeriod; + struct TSecret { + TDuration TimeoutPeriod; + TDuration StatusFlushPeriod; - TSecret(); - }; + TSecret(); + }; - // secret options are available, but you shouldn't probably use them - TSecret Secret; + // secret options are available, but you shouldn't probably use them + TSecret Secret; - /// initialized with default settings - TBusSessionConfig(); + /// initialized with default settings + TBusSessionConfig(); - TString PrintToString() const; + TString PrintToString() const; - void ConfigureLastGetopt(NLastGetopt::TOpts&, const TString& prefix = "mb-"); - }; + void ConfigureLastGetopt(NLastGetopt::TOpts&, const TString& prefix = "mb-"); + }; - using TBusClientSessionConfig = TBusSessionConfig; - using TBusServerSessionConfig = TBusSessionConfig; + using TBusClientSessionConfig = TBusSessionConfig; + using TBusServerSessionConfig = TBusSessionConfig; } // NBus diff --git a/library/cpp/messagebus/connection.h b/library/cpp/messagebus/connection.h index d88f6f753a..b1df64ddc1 100644 --- a/library/cpp/messagebus/connection.h +++ b/library/cpp/messagebus/connection.h @@ -6,56 +6,56 @@ #include <util/generic/ptr.h> namespace NBus { - struct TBusClientConnection { - /// if you want to open connection early - virtual void OpenConnection() = 0; - - /// Send message to the destination - /// If addr is set then use it as destination. - /// Takes ownership of addr (see ClearState method). - virtual EMessageStatus SendMessage(TBusMessage* pMes, bool wait = false) = 0; - - virtual EMessageStatus SendMessageOneWay(TBusMessage* pMes, bool wait = false) = 0; - - /// Like SendMessage but cares about message - template <typename T /* <: TBusMessage */> - EMessageStatus SendMessageAutoPtr(const TAutoPtr<T>& mes, bool wait = false) { - EMessageStatus status = SendMessage(mes.Get(), wait); - if (status == MESSAGE_OK) + struct TBusClientConnection { + /// if you want to open connection early + virtual void OpenConnection() = 0; + + /// Send message to the destination + /// If addr is set then use it as destination. + /// Takes ownership of addr (see ClearState method). + virtual EMessageStatus SendMessage(TBusMessage* pMes, bool wait = false) = 0; + + virtual EMessageStatus SendMessageOneWay(TBusMessage* pMes, bool wait = false) = 0; + + /// Like SendMessage but cares about message + template <typename T /* <: TBusMessage */> + EMessageStatus SendMessageAutoPtr(const TAutoPtr<T>& mes, bool wait = false) { + EMessageStatus status = SendMessage(mes.Get(), wait); + if (status == MESSAGE_OK) Y_UNUSED(mes.Release()); - return status; - } - - /// Like SendMessageOneWay but cares about message - template <typename T /* <: TBusMessage */> - EMessageStatus SendMessageOneWayAutoPtr(const TAutoPtr<T>& mes, bool wait = false) { - EMessageStatus status = SendMessageOneWay(mes.Get(), wait); - if (status == MESSAGE_OK) + return status; + } + + /// Like SendMessageOneWay but cares about message + template <typename T /* <: TBusMessage */> + EMessageStatus SendMessageOneWayAutoPtr(const TAutoPtr<T>& mes, bool wait = false) { + EMessageStatus status = SendMessageOneWay(mes.Get(), wait); + if (status == MESSAGE_OK) Y_UNUSED(mes.Release()); - return status; - } + return status; + } - EMessageStatus SendMessageMove(TBusMessageAutoPtr message, bool wait = false) { - return SendMessageAutoPtr(message, wait); - } + EMessageStatus SendMessageMove(TBusMessageAutoPtr message, bool wait = false) { + return SendMessageAutoPtr(message, wait); + } - EMessageStatus SendMessageOneWayMove(TBusMessageAutoPtr message, bool wait = false) { - return SendMessageOneWayAutoPtr(message, wait); - } + EMessageStatus SendMessageOneWayMove(TBusMessageAutoPtr message, bool wait = false) { + return SendMessageOneWayAutoPtr(message, wait); + } - // TODO: implement similar one-way methods + // TODO: implement similar one-way methods - virtual ~TBusClientConnection() { - } + virtual ~TBusClientConnection() { + } }; - namespace NPrivate { - struct TBusClientConnectionPtrOps { - static void Ref(TBusClientConnection*); - static void UnRef(TBusClientConnection*); - }; - } + namespace NPrivate { + struct TBusClientConnectionPtrOps { + static void Ref(TBusClientConnection*); + static void UnRef(TBusClientConnection*); + }; + } - using TBusClientConnectionPtr = TIntrusivePtr<TBusClientConnection, NPrivate::TBusClientConnectionPtrOps>; + using TBusClientConnectionPtr = TIntrusivePtr<TBusClientConnection, NPrivate::TBusClientConnectionPtrOps>; } diff --git a/library/cpp/messagebus/coreconn.cpp b/library/cpp/messagebus/coreconn.cpp index edfb174a55..d9411bb5db 100644 --- a/library/cpp/messagebus/coreconn.cpp +++ b/library/cpp/messagebus/coreconn.cpp @@ -4,27 +4,27 @@ #include <util/datetime/base.h> #include <util/generic/yexception.h> -#include <util/network/socket.h> +#include <util/network/socket.h> #include <util/string/util.h> -#include <util/system/thread.h> - +#include <util/system/thread.h> + namespace NBus { - TBusInstant Now() { - return millisec(); - } + TBusInstant Now() { + return millisec(); + } - EIpVersion MakeIpVersion(bool allowIpv4, bool allowIpv6) { - if (allowIpv4) { - if (allowIpv6) { - return EIP_VERSION_ANY; - } else { - return EIP_VERSION_4; - } - } else if (allowIpv6) { - return EIP_VERSION_6; - } + EIpVersion MakeIpVersion(bool allowIpv4, bool allowIpv6) { + if (allowIpv4) { + if (allowIpv6) { + return EIP_VERSION_ANY; + } else { + return EIP_VERSION_4; + } + } else if (allowIpv6) { + return EIP_VERSION_6; + } - ythrow yexception() << "Neither of IPv4/IPv6 is allowed."; + ythrow yexception() << "Neither of IPv4/IPv6 is allowed."; } } diff --git a/library/cpp/messagebus/coreconn.h b/library/cpp/messagebus/coreconn.h index 4d50e0b5fb..fca228d82e 100644 --- a/library/cpp/messagebus/coreconn.h +++ b/library/cpp/messagebus/coreconn.h @@ -3,65 +3,65 @@ ////////////////////////////////////////////////////////////// /// \file /// \brief Definitions for asynchonous connection queue - + #include "base.h" #include "event_loop.h" #include "netaddr.h" #include <util/datetime/base.h> -#include <util/generic/algorithm.h> +#include <util/generic/algorithm.h> #include <util/generic/list.h> #include <util/generic/map.h> #include <util/generic/set.h> #include <util/generic/string.h> #include <util/generic/vector.h> #include <util/network/address.h> -#include <util/network/ip.h> +#include <util/network/ip.h> #include <util/network/poller.h> #include <util/string/util.h> #include <util/system/condvar.h> -#include <util/system/mutex.h> +#include <util/system/mutex.h> #include <util/system/thread.h> #include <util/thread/lfqueue.h> - + #include <deque> #include <utility> #ifdef NO_ERROR -#undef NO_ERROR +#undef NO_ERROR #endif #define BUS_WORKER_CONDVAR //#define BUS_WORKER_MIXED -namespace NBus { - class TBusConnection; - class TBusConnectionFactory; - class TBusServerFactory; +namespace NBus { + class TBusConnection; + class TBusConnectionFactory; + class TBusServerFactory; + + using TBusConnectionList = TList<TBusConnection*>; - using TBusConnectionList = TList<TBusConnection*>; + /// @throw yexception + EIpVersion MakeIpVersion(bool allowIpv4, bool allowIpv6); - /// @throw yexception - EIpVersion MakeIpVersion(bool allowIpv4, bool allowIpv6); + inline bool WouldBlock() { + int syserr = LastSystemError(); + return syserr == EAGAIN || syserr == EINPROGRESS || syserr == EWOULDBLOCK || syserr == EINTR; + } - inline bool WouldBlock() { - int syserr = LastSystemError(); - return syserr == EAGAIN || syserr == EINPROGRESS || syserr == EWOULDBLOCK || syserr == EINTR; - } + class TBusSession; - class TBusSession; - - struct TMaxConnectedException: public yexception { - TMaxConnectedException(unsigned maxConnect) { - yexception& exc = *this; + struct TMaxConnectedException: public yexception { + TMaxConnectedException(unsigned maxConnect) { + yexception& exc = *this; exc << TStringBuf("Exceeded maximum number of outstanding connections: "); - exc << maxConnect; - } - }; + exc << maxConnect; + } + }; - enum EPollType { - POLL_READ, - POLL_WRITE - }; + enum EPollType { + POLL_READ, + POLL_WRITE + }; } diff --git a/library/cpp/messagebus/debug_receiver/debug_receiver_handler.cpp b/library/cpp/messagebus/debug_receiver/debug_receiver_handler.cpp index 7a535290ab..05f99e94ca 100644 --- a/library/cpp/messagebus/debug_receiver/debug_receiver_handler.cpp +++ b/library/cpp/messagebus/debug_receiver/debug_receiver_handler.cpp @@ -5,16 +5,16 @@ #include <util/generic/cast.h> #include <util/string/printf.h> -void TDebugReceiverHandler::OnError(TAutoPtr<NBus::TBusMessage>, NBus::EMessageStatus status) { +void TDebugReceiverHandler::OnError(TAutoPtr<NBus::TBusMessage>, NBus::EMessageStatus status) { Cerr << "error " << status << "\n"; } -void TDebugReceiverHandler::OnMessage(NBus::TOnMessageContext& message) { +void TDebugReceiverHandler::OnMessage(NBus::TOnMessageContext& message) { TDebugReceiverMessage* typedMessage = VerifyDynamicCast<TDebugReceiverMessage*>(message.GetMessage()); Cerr << "type=" << typedMessage->GetHeader()->Type - << " size=" << typedMessage->GetHeader()->Size - << " flags=" << Sprintf("0x%04x", (int)typedMessage->GetHeader()->FlagsInternal) - << "\n"; + << " size=" << typedMessage->GetHeader()->Size + << " flags=" << Sprintf("0x%04x", (int)typedMessage->GetHeader()->FlagsInternal) + << "\n"; message.ForgetRequest(); } diff --git a/library/cpp/messagebus/debug_receiver/debug_receiver_handler.h b/library/cpp/messagebus/debug_receiver/debug_receiver_handler.h index a01140e06c..0aed6b9984 100644 --- a/library/cpp/messagebus/debug_receiver/debug_receiver_handler.h +++ b/library/cpp/messagebus/debug_receiver/debug_receiver_handler.h @@ -2,7 +2,7 @@ #include <library/cpp/messagebus/ybus.h> -struct TDebugReceiverHandler: public NBus::IBusServerHandler { +struct TDebugReceiverHandler: public NBus::IBusServerHandler { NBus::TBusServerSession* ServerSession; void OnError(TAutoPtr<NBus::TBusMessage> pMessage, NBus::EMessageStatus status) override; diff --git a/library/cpp/messagebus/debug_receiver/debug_receiver_proto.cpp b/library/cpp/messagebus/debug_receiver/debug_receiver_proto.cpp index 3dcb949a20..0c74f9ecc3 100644 --- a/library/cpp/messagebus/debug_receiver/debug_receiver_proto.cpp +++ b/library/cpp/messagebus/debug_receiver/debug_receiver_proto.cpp @@ -7,11 +7,11 @@ TDebugReceiverProtocol::TDebugReceiverProtocol() { } -void TDebugReceiverProtocol::Serialize(const NBus::TBusMessage*, TBuffer&) { +void TDebugReceiverProtocol::Serialize(const NBus::TBusMessage*, TBuffer&) { Y_FAIL("it is receiver only"); } -TAutoPtr<NBus::TBusMessage> TDebugReceiverProtocol::Deserialize(ui16, TArrayRef<const char> payload) { +TAutoPtr<NBus::TBusMessage> TDebugReceiverProtocol::Deserialize(ui16, TArrayRef<const char> payload) { THolder<TDebugReceiverMessage> r(new TDebugReceiverMessage(ECreateUninitialized())); r->Payload.Append(payload.data(), payload.size()); diff --git a/library/cpp/messagebus/debug_receiver/debug_receiver_proto.h b/library/cpp/messagebus/debug_receiver/debug_receiver_proto.h index ed08d3cd9e..d34710dcf7 100644 --- a/library/cpp/messagebus/debug_receiver/debug_receiver_proto.h +++ b/library/cpp/messagebus/debug_receiver/debug_receiver_proto.h @@ -2,26 +2,26 @@ #include <library/cpp/messagebus/ybus.h> -struct TDebugReceiverMessage: public NBus::TBusMessage { +struct TDebugReceiverMessage: public NBus::TBusMessage { /// constructor to create messages on sending end - TDebugReceiverMessage(ui16 type) - : NBus::TBusMessage(type) - { - } + TDebugReceiverMessage(ui16 type) + : NBus::TBusMessage(type) + { + } /// constructor with serialzed data to examine the header - TDebugReceiverMessage(NBus::ECreateUninitialized) - : NBus::TBusMessage(NBus::ECreateUninitialized()) - { - } + TDebugReceiverMessage(NBus::ECreateUninitialized) + : NBus::TBusMessage(NBus::ECreateUninitialized()) + { + } TBuffer Payload; }; -struct TDebugReceiverProtocol: public NBus::TBusProtocol { +struct TDebugReceiverProtocol: public NBus::TBusProtocol { TDebugReceiverProtocol(); - void Serialize(const NBus::TBusMessage* mess, TBuffer& data) override; + void Serialize(const NBus::TBusMessage* mess, TBuffer& data) override; TAutoPtr<NBus::TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) override; }; diff --git a/library/cpp/messagebus/defs.h b/library/cpp/messagebus/defs.h index 7d7f34a743..cb553acc45 100644 --- a/library/cpp/messagebus/defs.h +++ b/library/cpp/messagebus/defs.h @@ -1,4 +1,4 @@ #pragma once - + #include <library/cpp/messagebus/config/defs.h> diff --git a/library/cpp/messagebus/dummy_debugger.h b/library/cpp/messagebus/dummy_debugger.h index 911d56ce73..89a4e18716 100644 --- a/library/cpp/messagebus/dummy_debugger.h +++ b/library/cpp/messagebus/dummy_debugger.h @@ -3,7 +3,7 @@ #include <util/datetime/base.h> #include <util/stream/output.h> -#define MB_TRACE() \ - do { \ +#define MB_TRACE() \ + do { \ Cerr << TInstant::Now() << " " << __FILE__ << ":" << __LINE__ << " " << __FUNCTION__ << Endl; \ } while (false) diff --git a/library/cpp/messagebus/duration_histogram.cpp b/library/cpp/messagebus/duration_histogram.cpp index a2bbdef46a..32a0001d41 100644 --- a/library/cpp/messagebus/duration_histogram.cpp +++ b/library/cpp/messagebus/duration_histogram.cpp @@ -47,7 +47,7 @@ namespace { }; } -TString TDurationHistogram::LabelBefore(unsigned i) { +TString TDurationHistogram::LabelBefore(unsigned i) { Y_VERIFY(i < Buckets); TDuration d = Singleton<TMarks>()->Marks[i]; @@ -65,7 +65,7 @@ TString TDurationHistogram::LabelBefore(unsigned i) { return ss.Str(); } -TString TDurationHistogram::PrintToString() const { +TString TDurationHistogram::PrintToString() const { TStringStream ss; for (auto time : Times) { ss << time << "\n"; diff --git a/library/cpp/messagebus/duration_histogram_ut.cpp b/library/cpp/messagebus/duration_histogram_ut.cpp index 006022216c..01bcc095e9 100644 --- a/library/cpp/messagebus/duration_histogram_ut.cpp +++ b/library/cpp/messagebus/duration_histogram_ut.cpp @@ -4,15 +4,15 @@ Y_UNIT_TEST_SUITE(TDurationHistogramTest) { Y_UNIT_TEST(BucketFor) { - UNIT_ASSERT_VALUES_EQUAL(0u, TDurationHistogram::BucketFor(TDuration::MicroSeconds(0))); - UNIT_ASSERT_VALUES_EQUAL(0u, TDurationHistogram::BucketFor(TDuration::MicroSeconds(1))); - UNIT_ASSERT_VALUES_EQUAL(0u, TDurationHistogram::BucketFor(TDuration::MicroSeconds(900))); + UNIT_ASSERT_VALUES_EQUAL(0u, TDurationHistogram::BucketFor(TDuration::MicroSeconds(0))); + UNIT_ASSERT_VALUES_EQUAL(0u, TDurationHistogram::BucketFor(TDuration::MicroSeconds(1))); + UNIT_ASSERT_VALUES_EQUAL(0u, TDurationHistogram::BucketFor(TDuration::MicroSeconds(900))); UNIT_ASSERT_VALUES_EQUAL(1u, TDurationHistogram::BucketFor(TDuration::MicroSeconds(1500))); UNIT_ASSERT_VALUES_EQUAL(2u, TDurationHistogram::BucketFor(TDuration::MicroSeconds(2500))); unsigned sb = TDurationHistogram::SecondBoundary; - UNIT_ASSERT_VALUES_EQUAL(sb - 1, TDurationHistogram::BucketFor(TDuration::MilliSeconds(999))); + UNIT_ASSERT_VALUES_EQUAL(sb - 1, TDurationHistogram::BucketFor(TDuration::MilliSeconds(999))); UNIT_ASSERT_VALUES_EQUAL(sb, TDurationHistogram::BucketFor(TDuration::MilliSeconds(1000))); UNIT_ASSERT_VALUES_EQUAL(sb, TDurationHistogram::BucketFor(TDuration::MilliSeconds(1001))); diff --git a/library/cpp/messagebus/event_loop.cpp b/library/cpp/messagebus/event_loop.cpp index 42477bf091..f685135bed 100644 --- a/library/cpp/messagebus/event_loop.cpp +++ b/library/cpp/messagebus/event_loop.cpp @@ -131,7 +131,7 @@ TEventLoop::TEventLoop(const char* name) { } -TEventLoop::~TEventLoop() { +TEventLoop::~TEventLoop() { } void TEventLoop::Run() { @@ -273,7 +273,7 @@ TEventLoop::TImpl::TImpl(const char* name) SetNonBlock(WakeupReadSocket, true); Poller.WaitRead(WakeupReadSocket, - reinterpret_cast<void*>(this)); + reinterpret_cast<void*>(this)); } void TEventLoop::TImpl::Run() { @@ -303,8 +303,8 @@ void TEventLoop::TImpl::Run() { s->CallHandler(); } - SOCKET socket = -1; - while (SocketsToRemove.Dequeue(&socket)) { + SOCKET socket = -1; + while (SocketsToRemove.Dequeue(&socket)) { TGuard<TMutex> guard(Mutex); Y_VERIFY(Data.erase(socket) == 1, "must be removed once"); } diff --git a/library/cpp/messagebus/event_loop.h b/library/cpp/messagebus/event_loop.h index a5d376b60a..d5b0a53b0c 100644 --- a/library/cpp/messagebus/event_loop.h +++ b/library/cpp/messagebus/event_loop.h @@ -7,10 +7,10 @@ namespace NEventLoop { struct IEventHandler - : public TAtomicRefCount<IEventHandler> { + : public TAtomicRefCount<IEventHandler> { virtual void HandleEvent(SOCKET socket, void* cookie) = 0; - virtual ~IEventHandler() { - } + virtual ~IEventHandler() { + } }; typedef TIntrusivePtr<IEventHandler> TEventHandlerPtr; @@ -20,7 +20,7 @@ namespace NEventLoop { // TODO: make TChannel itself a pointer // to avoid confusion with Drop and Unregister class TChannel - : public TAtomicRefCount<TChannel> { + : public TAtomicRefCount<TChannel> { public: ~TChannel(); diff --git a/library/cpp/messagebus/extra_ref.h b/library/cpp/messagebus/extra_ref.h index 274dda1c71..2927123266 100644 --- a/library/cpp/messagebus/extra_ref.h +++ b/library/cpp/messagebus/extra_ref.h @@ -6,10 +6,10 @@ class TExtraRef { TAtomic Holds; public: - TExtraRef() - : Holds(false) - { - } + TExtraRef() + : Holds(false) + { + } ~TExtraRef() { Y_VERIFY(!Holds); } diff --git a/library/cpp/messagebus/futex_like.cpp b/library/cpp/messagebus/futex_like.cpp index 1d6b105ba4..7f965126db 100644 --- a/library/cpp/messagebus/futex_like.cpp +++ b/library/cpp/messagebus/futex_like.cpp @@ -3,11 +3,11 @@ #ifdef _linux_ #include <sys/syscall.h> #include <linux/futex.h> - -#if !defined(SYS_futex) -#define SYS_futex __NR_futex + +#if !defined(SYS_futex) +#define SYS_futex __NR_futex +#endif #endif -#endif #include <errno.h> @@ -17,8 +17,8 @@ #ifdef _linux_ namespace { - int futex(int* uaddr, int op, int val, const struct timespec* timeout, - int* uaddr2, int val3) { + int futex(int* uaddr, int op, int val, const struct timespec* timeout, + int* uaddr2, int val3) { return syscall(SYS_futex, uaddr, op, val, timeout, uaddr2, val3); } } diff --git a/library/cpp/messagebus/futex_like.h b/library/cpp/messagebus/futex_like.h index 754d1a44c0..31d60c60f1 100644 --- a/library/cpp/messagebus/futex_like.h +++ b/library/cpp/messagebus/futex_like.h @@ -17,8 +17,8 @@ private: public: TFutexLike() : Value(0) - { - } + { + } int AddAndGet(int add) { #ifdef _linux_ diff --git a/library/cpp/messagebus/handler.cpp b/library/cpp/messagebus/handler.cpp index eec3f3191d..333bd52934 100644 --- a/library/cpp/messagebus/handler.cpp +++ b/library/cpp/messagebus/handler.cpp @@ -6,31 +6,31 @@ using namespace NBus; using namespace NBus::NPrivate; -void IBusErrorHandler::OnError(TAutoPtr<TBusMessage> pMessage, EMessageStatus status) { - Y_UNUSED(pMessage); - Y_UNUSED(status); -} -void IBusServerHandler::OnSent(TAutoPtr<TBusMessage> pMessage) { - Y_UNUSED(pMessage); -} -void IBusClientHandler::OnMessageSent(TBusMessage* pMessage) { - Y_UNUSED(pMessage); -} -void IBusClientHandler::OnMessageSentOneWay(TAutoPtr<TBusMessage> pMessage) { - Y_UNUSED(pMessage); -} +void IBusErrorHandler::OnError(TAutoPtr<TBusMessage> pMessage, EMessageStatus status) { + Y_UNUSED(pMessage); + Y_UNUSED(status); +} +void IBusServerHandler::OnSent(TAutoPtr<TBusMessage> pMessage) { + Y_UNUSED(pMessage); +} +void IBusClientHandler::OnMessageSent(TBusMessage* pMessage) { + Y_UNUSED(pMessage); +} +void IBusClientHandler::OnMessageSentOneWay(TAutoPtr<TBusMessage> pMessage) { + Y_UNUSED(pMessage); +} -void IBusClientHandler::OnClientConnectionEvent(const TClientConnectionEvent&) { -} +void IBusClientHandler::OnClientConnectionEvent(const TClientConnectionEvent&) { +} -void TOnMessageContext::ForgetRequest() { +void TOnMessageContext::ForgetRequest() { Session->ForgetRequest(Ident); } -TNetAddr TOnMessageContext::GetPeerAddrNetAddr() const { +TNetAddr TOnMessageContext::GetPeerAddrNetAddr() const { return Ident.GetNetAddr(); } bool TOnMessageContext::IsConnectionAlive() const { - return !!Ident.Connection && Ident.Connection->IsAlive(); + return !!Ident.Connection && Ident.Connection->IsAlive(); } diff --git a/library/cpp/messagebus/handler.h b/library/cpp/messagebus/handler.h index 211b2f5bca..60002c68a6 100644 --- a/library/cpp/messagebus/handler.h +++ b/library/cpp/messagebus/handler.h @@ -9,127 +9,127 @@ #include <util/generic/noncopyable.h> namespace NBus { - ///////////////////////////////////////////////////////////////// - /// \brief Interface to message bus handler - - struct IBusErrorHandler { - friend struct ::NBus::NPrivate::TBusSessionImpl; - - private: - TUseAfterFreeChecker UseAfterFreeChecker; - TUseCountChecker UseCountChecker; - - public: - /// called when message or reply can't be delivered - virtual void OnError(TAutoPtr<TBusMessage> pMessage, EMessageStatus status); - - virtual ~IBusErrorHandler() { - } - }; - - class TClientConnectionEvent { - public: - enum EType { - CONNECTED, - DISCONNECTED, - }; - - private: - EType Type; - ui64 Id; - TNetAddr Addr; - - public: - TClientConnectionEvent(EType type, ui64 id, TNetAddr addr) - : Type(type) - , Id(id) - , Addr(addr) - { - } - - EType GetType() const { - return Type; - } - ui64 GetId() const { - return Id; - } - TNetAddr GetAddr() const { - return Addr; - } + ///////////////////////////////////////////////////////////////// + /// \brief Interface to message bus handler + + struct IBusErrorHandler { + friend struct ::NBus::NPrivate::TBusSessionImpl; + + private: + TUseAfterFreeChecker UseAfterFreeChecker; + TUseCountChecker UseCountChecker; + + public: + /// called when message or reply can't be delivered + virtual void OnError(TAutoPtr<TBusMessage> pMessage, EMessageStatus status); + + virtual ~IBusErrorHandler() { + } + }; + + class TClientConnectionEvent { + public: + enum EType { + CONNECTED, + DISCONNECTED, + }; + + private: + EType Type; + ui64 Id; + TNetAddr Addr; + + public: + TClientConnectionEvent(EType type, ui64 id, TNetAddr addr) + : Type(type) + , Id(id) + , Addr(addr) + { + } + + EType GetType() const { + return Type; + } + ui64 GetId() const { + return Id; + } + TNetAddr GetAddr() const { + return Addr; + } + }; + + class TOnMessageContext : TNonCopyable { + private: + THolder<TBusMessage> Message; + TBusIdentity Ident; + // TODO: we don't need to store session, we have connection in ident + TBusServerSession* Session; + + public: + TOnMessageContext() + : Session() + { + } + TOnMessageContext(TAutoPtr<TBusMessage> message, TBusIdentity& ident, TBusServerSession* session) + : Message(message) + , Session(session) + { + Ident.Swap(ident); + } + + bool IsInWork() const { + return Ident.IsInWork(); + } + + bool operator!() const { + return !IsInWork(); + } + + TBusMessage* GetMessage() { + return Message.Get(); + } + + TBusMessage* ReleaseMessage() { + return Message.Release(); + } + + TBusServerSession* GetSession() { + return Session; + } + + template <typename U /* <: TBusMessage */> + EMessageStatus SendReplyAutoPtr(TAutoPtr<U>& rep); + + EMessageStatus SendReplyMove(TBusMessageAutoPtr response); + + void AckMessage(TBusIdentity& ident); + + void ForgetRequest(); + + void Swap(TOnMessageContext& that) { + DoSwap(Message, that.Message); + Ident.Swap(that.Ident); + DoSwap(Session, that.Session); + } + + TNetAddr GetPeerAddrNetAddr() const; + + bool IsConnectionAlive() const; + }; + + struct IBusServerHandler: public IBusErrorHandler { + virtual void OnMessage(TOnMessageContext& onMessage) = 0; + /// called when reply has been sent from destination + virtual void OnSent(TAutoPtr<TBusMessage> pMessage); + }; + + struct IBusClientHandler: public IBusErrorHandler { + /// called on source when reply arrives from destination + virtual void OnReply(TAutoPtr<TBusMessage> pMessage, TAutoPtr<TBusMessage> pReply) = 0; + /// called when client side message has gone into wire, place to call AckMessage() + virtual void OnMessageSent(TBusMessage* pMessage); + virtual void OnMessageSentOneWay(TAutoPtr<TBusMessage> pMessage); + virtual void OnClientConnectionEvent(const TClientConnectionEvent&); }; - class TOnMessageContext : TNonCopyable { - private: - THolder<TBusMessage> Message; - TBusIdentity Ident; - // TODO: we don't need to store session, we have connection in ident - TBusServerSession* Session; - - public: - TOnMessageContext() - : Session() - { - } - TOnMessageContext(TAutoPtr<TBusMessage> message, TBusIdentity& ident, TBusServerSession* session) - : Message(message) - , Session(session) - { - Ident.Swap(ident); - } - - bool IsInWork() const { - return Ident.IsInWork(); - } - - bool operator!() const { - return !IsInWork(); - } - - TBusMessage* GetMessage() { - return Message.Get(); - } - - TBusMessage* ReleaseMessage() { - return Message.Release(); - } - - TBusServerSession* GetSession() { - return Session; - } - - template <typename U /* <: TBusMessage */> - EMessageStatus SendReplyAutoPtr(TAutoPtr<U>& rep); - - EMessageStatus SendReplyMove(TBusMessageAutoPtr response); - - void AckMessage(TBusIdentity& ident); - - void ForgetRequest(); - - void Swap(TOnMessageContext& that) { - DoSwap(Message, that.Message); - Ident.Swap(that.Ident); - DoSwap(Session, that.Session); - } - - TNetAddr GetPeerAddrNetAddr() const; - - bool IsConnectionAlive() const; - }; - - struct IBusServerHandler: public IBusErrorHandler { - virtual void OnMessage(TOnMessageContext& onMessage) = 0; - /// called when reply has been sent from destination - virtual void OnSent(TAutoPtr<TBusMessage> pMessage); - }; - - struct IBusClientHandler: public IBusErrorHandler { - /// called on source when reply arrives from destination - virtual void OnReply(TAutoPtr<TBusMessage> pMessage, TAutoPtr<TBusMessage> pReply) = 0; - /// called when client side message has gone into wire, place to call AckMessage() - virtual void OnMessageSent(TBusMessage* pMessage); - virtual void OnMessageSentOneWay(TAutoPtr<TBusMessage> pMessage); - virtual void OnClientConnectionEvent(const TClientConnectionEvent&); - }; - -} +} diff --git a/library/cpp/messagebus/handler_impl.h b/library/cpp/messagebus/handler_impl.h index 94088cfba0..6593f04cc3 100644 --- a/library/cpp/messagebus/handler_impl.h +++ b/library/cpp/messagebus/handler_impl.h @@ -5,19 +5,19 @@ #include "session.h" namespace NBus { - template <typename U /* <: TBusMessage */> - EMessageStatus TOnMessageContext::SendReplyAutoPtr(TAutoPtr<U>& response) { - return Session->SendReplyAutoPtr(Ident, response); - } + template <typename U /* <: TBusMessage */> + EMessageStatus TOnMessageContext::SendReplyAutoPtr(TAutoPtr<U>& response) { + return Session->SendReplyAutoPtr(Ident, response); + } - inline EMessageStatus TOnMessageContext::SendReplyMove(TBusMessageAutoPtr response) { - return SendReplyAutoPtr(response); - } + inline EMessageStatus TOnMessageContext::SendReplyMove(TBusMessageAutoPtr response) { + return SendReplyAutoPtr(response); + } - inline void TOnMessageContext::AckMessage(TBusIdentity& ident) { - Y_VERIFY(Ident.LocalFlags == NPrivate::MESSAGE_IN_WORK); - Y_VERIFY(ident.LocalFlags == 0); - Ident.Swap(ident); - } + inline void TOnMessageContext::AckMessage(TBusIdentity& ident) { + Y_VERIFY(Ident.LocalFlags == NPrivate::MESSAGE_IN_WORK); + Y_VERIFY(ident.LocalFlags == 0); + Ident.Swap(ident); + } } diff --git a/library/cpp/messagebus/hash.h b/library/cpp/messagebus/hash.h index d20bb370b7..cc1b136a86 100644 --- a/library/cpp/messagebus/hash.h +++ b/library/cpp/messagebus/hash.h @@ -3,17 +3,17 @@ #include <util/str_stl.h> #include <util/digest/numeric.h> -namespace NBus { - namespace NPrivate { - template <typename T> - size_t Hash(const T& val) { - return THash<T>()(val); - } +namespace NBus { + namespace NPrivate { + template <typename T> + size_t Hash(const T& val) { + return THash<T>()(val); + } - template <typename T, typename U> - size_t HashValues(const T& a, const U& b) { - return CombineHashes(Hash(a), Hash(b)); - } + template <typename T, typename U> + size_t HashValues(const T& a, const U& b) { + return CombineHashes(Hash(a), Hash(b)); + } } -} +} diff --git a/library/cpp/messagebus/key_value_printer.cpp b/library/cpp/messagebus/key_value_printer.cpp index 75a82a10e2..c8592145c7 100644 --- a/library/cpp/messagebus/key_value_printer.cpp +++ b/library/cpp/messagebus/key_value_printer.cpp @@ -4,11 +4,11 @@ TKeyValuePrinter::TKeyValuePrinter(const TString& sep) : Sep(sep) -{ -} +{ +} -TKeyValuePrinter::~TKeyValuePrinter() { -} +TKeyValuePrinter::~TKeyValuePrinter() { +} void TKeyValuePrinter::AddRowImpl(const TString& key, const TString& value, bool alignLeft) { Keys.push_back(key); diff --git a/library/cpp/messagebus/key_value_printer.h b/library/cpp/messagebus/key_value_printer.h index b0277b15b6..bca1fde50e 100644 --- a/library/cpp/messagebus/key_value_printer.h +++ b/library/cpp/messagebus/key_value_printer.h @@ -11,7 +11,7 @@ private: TVector<TString> Keys; TVector<TString> Values; TVector<bool> AlignLefts; - + public: TKeyValuePrinter(const TString& sep = TString(": ")); ~TKeyValuePrinter(); diff --git a/library/cpp/messagebus/latch.h b/library/cpp/messagebus/latch.h index b6f64c2db3..373f4c0e13 100644 --- a/library/cpp/messagebus/latch.h +++ b/library/cpp/messagebus/latch.h @@ -9,12 +9,12 @@ private: TAtomic Locked; TMutex Mutex; TCondVar CondVar; - + public: - TLatch() - : Locked(0) - { - } + TLatch() + : Locked(0) + { + } void Wait() { // optimistic path diff --git a/library/cpp/messagebus/left_right_buffer.h b/library/cpp/messagebus/left_right_buffer.h index a221324c23..f937cefad0 100644 --- a/library/cpp/messagebus/left_right_buffer.h +++ b/library/cpp/messagebus/left_right_buffer.h @@ -4,75 +4,75 @@ #include <util/generic/noncopyable.h> #include <util/system/yassert.h> -namespace NBus { - namespace NPrivate { - class TLeftRightBuffer : TNonCopyable { - private: - TBuffer Buffer; - size_t Left; - - void CheckInvariant() { - Y_ASSERT(Left <= Buffer.Size()); - } - - public: - TLeftRightBuffer() - : Left(0) - { - } - - TBuffer& GetBuffer() { - return Buffer; - } - - size_t Capacity() { - return Buffer.Capacity(); - } - - void Clear() { - Buffer.Clear(); - Left = 0; - } - - void Reset() { - Buffer.Reset(); - Left = 0; - } - - void Compact() { - Buffer.ChopHead(Left); - Left = 0; - } - - char* LeftPos() { - return Buffer.Data() + Left; - } - - size_t LeftSize() { - return Left; - } - - void LeftProceed(size_t count) { - Y_ASSERT(count <= Size()); - Left += count; - } - - size_t Size() { - return Buffer.Size() - Left; - } - - bool Empty() { - return Size() == 0; - } - - char* RightPos() { - return Buffer.Data() + Buffer.Size(); - } - - size_t Avail() { - return Buffer.Avail(); - } - }; +namespace NBus { + namespace NPrivate { + class TLeftRightBuffer : TNonCopyable { + private: + TBuffer Buffer; + size_t Left; + + void CheckInvariant() { + Y_ASSERT(Left <= Buffer.Size()); + } + + public: + TLeftRightBuffer() + : Left(0) + { + } + + TBuffer& GetBuffer() { + return Buffer; + } + + size_t Capacity() { + return Buffer.Capacity(); + } + + void Clear() { + Buffer.Clear(); + Left = 0; + } + + void Reset() { + Buffer.Reset(); + Left = 0; + } + + void Compact() { + Buffer.ChopHead(Left); + Left = 0; + } + + char* LeftPos() { + return Buffer.Data() + Left; + } + + size_t LeftSize() { + return Left; + } + + void LeftProceed(size_t count) { + Y_ASSERT(count <= Size()); + Left += count; + } + + size_t Size() { + return Buffer.Size() - Left; + } + + bool Empty() { + return Size() == 0; + } + + char* RightPos() { + return Buffer.Data() + Buffer.Size(); + } + + size_t Avail() { + return Buffer.Avail(); + } + }; } -} +} diff --git a/library/cpp/messagebus/lfqueue_batch.h b/library/cpp/messagebus/lfqueue_batch.h index 6c28f0ade6..8128d3154d 100644 --- a/library/cpp/messagebus/lfqueue_batch.h +++ b/library/cpp/messagebus/lfqueue_batch.h @@ -5,17 +5,17 @@ #include <util/generic/vector.h> #include <util/thread/lfstack.h> -template <typename T, template <typename, class> class TVectorType = TVector> +template <typename T, template <typename, class> class TVectorType = TVector> class TLockFreeQueueBatch { private: TLockFreeStack<TVectorType<T, std::allocator<T>>*> Stack; - + public: bool IsEmpty() { return Stack.IsEmpty(); } - void EnqueueAll(TAutoPtr<TVectorType<T, std::allocator<T>>> vec) { + void EnqueueAll(TAutoPtr<TVectorType<T, std::allocator<T>>> vec) { Stack.Enqueue(vec.Release()); } @@ -24,7 +24,7 @@ public: Stack.DequeueAllSingleConsumer(vs.GetVector()); for (typename TVector<TVectorType<T, std::allocator<T>>*>::reverse_iterator i = vs.GetVector()->rbegin(); - i != vs.GetVector()->rend(); ++i) { + i != vs.GetVector()->rend(); ++i) { if (i == vs.GetVector()->rend()) { r->swap(**i); } else { diff --git a/library/cpp/messagebus/lfqueue_batch_ut.cpp b/library/cpp/messagebus/lfqueue_batch_ut.cpp index b60586d141..f80434c0d4 100644 --- a/library/cpp/messagebus/lfqueue_batch_ut.cpp +++ b/library/cpp/messagebus/lfqueue_batch_ut.cpp @@ -6,7 +6,7 @@ Y_UNIT_TEST_SUITE(TLockFreeQueueBatch) { Y_UNIT_TEST(Order1) { TLockFreeQueueBatch<unsigned> q; { - TAutoPtr<TVector<unsigned>> v(new TVector<unsigned>); + TAutoPtr<TVector<unsigned>> v(new TVector<unsigned>); v->push_back(0); v->push_back(1); q.EnqueueAll(v); @@ -28,13 +28,13 @@ Y_UNIT_TEST_SUITE(TLockFreeQueueBatch) { Y_UNIT_TEST(Order2) { TLockFreeQueueBatch<unsigned> q; { - TAutoPtr<TVector<unsigned>> v(new TVector<unsigned>); + TAutoPtr<TVector<unsigned>> v(new TVector<unsigned>); v->push_back(0); v->push_back(1); q.EnqueueAll(v); } { - TAutoPtr<TVector<unsigned>> v(new TVector<unsigned>); + TAutoPtr<TVector<unsigned>> v(new TVector<unsigned>); v->push_back(2); v->push_back(3); v->push_back(4); diff --git a/library/cpp/messagebus/local_flags.cpp b/library/cpp/messagebus/local_flags.cpp index 41602f00b9..877e533f76 100644 --- a/library/cpp/messagebus/local_flags.cpp +++ b/library/cpp/messagebus/local_flags.cpp @@ -14,15 +14,15 @@ TString NBus::NPrivate::LocalFlagSetToString(ui32 flags0) { ui32 flags = flags0; TStringStream ss; -#define P(name, value, ...) \ - do \ - if (flags & value) { \ - if (!ss.Str().empty()) { \ - ss << "|"; \ - } \ - ss << #name; \ - flags &= ~name; \ - } \ +#define P(name, value, ...) \ + do \ + if (flags & value) { \ + if (!ss.Str().empty()) { \ + ss << "|"; \ + } \ + ss << #name; \ + flags &= ~name; \ + } \ while (false); MESSAGE_LOCAL_FLAGS_MAP(P) if (flags != 0) { diff --git a/library/cpp/messagebus/local_flags.h b/library/cpp/messagebus/local_flags.h index 2677e6616d..f589283188 100644 --- a/library/cpp/messagebus/local_flags.h +++ b/library/cpp/messagebus/local_flags.h @@ -5,22 +5,22 @@ #include <util/generic/string.h> #include <util/stream/output.h> -namespace NBus { - namespace NPrivate { -#define MESSAGE_LOCAL_FLAGS_MAP(XX) \ - XX(MESSAGE_REPLY_INTERNAL, 0x0001) \ - XX(MESSAGE_IN_WORK, 0x0002) \ - XX(MESSAGE_IN_FLIGHT_ON_CLIENT, 0x0004) \ - XX(MESSAGE_REPLY_IS_BEGING_SENT, 0x0008) \ - XX(MESSAGE_ONE_WAY_INTERNAL, 0x0010) \ - /**/ +namespace NBus { + namespace NPrivate { +#define MESSAGE_LOCAL_FLAGS_MAP(XX) \ + XX(MESSAGE_REPLY_INTERNAL, 0x0001) \ + XX(MESSAGE_IN_WORK, 0x0002) \ + XX(MESSAGE_IN_FLIGHT_ON_CLIENT, 0x0004) \ + XX(MESSAGE_REPLY_IS_BEGING_SENT, 0x0008) \ + XX(MESSAGE_ONE_WAY_INTERNAL, 0x0010) \ + /**/ - enum EMessageLocalFlags { - MESSAGE_LOCAL_FLAGS_MAP(ENUM_VALUE_GEN) - }; + enum EMessageLocalFlags { + MESSAGE_LOCAL_FLAGS_MAP(ENUM_VALUE_GEN) + }; - ENUM_TO_STRING(EMessageLocalFlags, MESSAGE_LOCAL_FLAGS_MAP) + ENUM_TO_STRING(EMessageLocalFlags, MESSAGE_LOCAL_FLAGS_MAP) - TString LocalFlagSetToString(ui32); - } -} + TString LocalFlagSetToString(ui32); + } +} diff --git a/library/cpp/messagebus/local_flags_ut.cpp b/library/cpp/messagebus/local_flags_ut.cpp index 361333892e..189d73eb0f 100644 --- a/library/cpp/messagebus/local_flags_ut.cpp +++ b/library/cpp/messagebus/local_flags_ut.cpp @@ -9,10 +9,10 @@ Y_UNIT_TEST_SUITE(EMessageLocalFlags) { Y_UNIT_TEST(TestLocalFlagSetToString) { UNIT_ASSERT_VALUES_EQUAL("0", LocalFlagSetToString(0)); UNIT_ASSERT_VALUES_EQUAL("MESSAGE_REPLY_INTERNAL", - LocalFlagSetToString(MESSAGE_REPLY_INTERNAL)); + LocalFlagSetToString(MESSAGE_REPLY_INTERNAL)); UNIT_ASSERT_VALUES_EQUAL("MESSAGE_IN_WORK|MESSAGE_IN_FLIGHT_ON_CLIENT", - LocalFlagSetToString(MESSAGE_IN_WORK | MESSAGE_IN_FLIGHT_ON_CLIENT)); + LocalFlagSetToString(MESSAGE_IN_WORK | MESSAGE_IN_FLIGHT_ON_CLIENT)); UNIT_ASSERT_VALUES_EQUAL("0xff3456", - LocalFlagSetToString(0xff3456)); + LocalFlagSetToString(0xff3456)); } } diff --git a/library/cpp/messagebus/local_tasks.h b/library/cpp/messagebus/local_tasks.h index f7de96590d..d8e801a457 100644 --- a/library/cpp/messagebus/local_tasks.h +++ b/library/cpp/messagebus/local_tasks.h @@ -9,8 +9,8 @@ private: public: TLocalTasks() : GotTasks(0) - { - } + { + } void AddTask() { AtomicSet(GotTasks, 1); diff --git a/library/cpp/messagebus/locator.cpp b/library/cpp/messagebus/locator.cpp index f0f8da7cb0..e38a35c426 100644 --- a/library/cpp/messagebus/locator.cpp +++ b/library/cpp/messagebus/locator.cpp @@ -1,5 +1,5 @@ //////////////////////////////////////////////////////////////////////////// -/// \file +/// \file /// \brief Implementation of locator service #include "locator.h" @@ -10,418 +10,418 @@ #include <util/system/hostname.h> namespace NBus { - using namespace NAddr; - - static TIpPort GetAddrPort(const IRemoteAddr& addr) { - switch (addr.Addr()->sa_family) { - case AF_INET: { - return ntohs(((const sockaddr_in*)addr.Addr())->sin_port); - } - - case AF_INET6: { - return ntohs(((const sockaddr_in6*)addr.Addr())->sin6_port); - } - - default: { - ythrow yexception() << "not implemented"; - break; - } + using namespace NAddr; + + static TIpPort GetAddrPort(const IRemoteAddr& addr) { + switch (addr.Addr()->sa_family) { + case AF_INET: { + return ntohs(((const sockaddr_in*)addr.Addr())->sin_port); + } + + case AF_INET6: { + return ntohs(((const sockaddr_in6*)addr.Addr())->sin6_port); + } + + default: { + ythrow yexception() << "not implemented"; + break; + } } - } - - static inline bool GetIp6AddressFromVector(const TVector<TNetAddr>& addrs, TNetAddr* addr) { - for (size_t i = 1; i < addrs.size(); ++i) { - if (addrs[i - 1].Addr()->sa_family == addrs[i].Addr()->sa_family) { - return false; - } - - if (GetAddrPort(addrs[i - 1]) != GetAddrPort(addrs[i])) { - return false; - } + } + + static inline bool GetIp6AddressFromVector(const TVector<TNetAddr>& addrs, TNetAddr* addr) { + for (size_t i = 1; i < addrs.size(); ++i) { + if (addrs[i - 1].Addr()->sa_family == addrs[i].Addr()->sa_family) { + return false; + } + + if (GetAddrPort(addrs[i - 1]) != GetAddrPort(addrs[i])) { + return false; + } } - for (size_t i = 0; i < addrs.size(); ++i) { - if (addrs[i].Addr()->sa_family == AF_INET6) { - *addr = addrs[i]; - return true; - } + for (size_t i = 0; i < addrs.size(); ++i) { + if (addrs[i].Addr()->sa_family == AF_INET6) { + *addr = addrs[i]; + return true; + } } - - return false; + + return false; } - EMessageStatus TBusProtocol::GetDestination(const TBusClientSession*, TBusMessage* mess, TBusLocator* locator, TNetAddr* addr) { - TBusService service = GetService(); - TBusKey key = GetKey(mess); - TVector<TNetAddr> addrs; - - /// check for special local key - if (key == YBUS_KEYLOCAL) { - locator->GetLocalAddresses(service, addrs); - } else { - /// lookup address/port in the locator table - locator->LocateAll(service, key, addrs); + EMessageStatus TBusProtocol::GetDestination(const TBusClientSession*, TBusMessage* mess, TBusLocator* locator, TNetAddr* addr) { + TBusService service = GetService(); + TBusKey key = GetKey(mess); + TVector<TNetAddr> addrs; + + /// check for special local key + if (key == YBUS_KEYLOCAL) { + locator->GetLocalAddresses(service, addrs); + } else { + /// lookup address/port in the locator table + locator->LocateAll(service, key, addrs); } - if (addrs.size() == 0) { - return MESSAGE_SERVICE_UNKNOWN; - } else if (addrs.size() == 1) { - *addr = addrs[0]; - } else { - if (!GetIp6AddressFromVector(addrs, addr)) { - /// default policy can't make choice for you here, overide GetDestination() function - /// to implement custom routing strategy for your service. - return MESSAGE_SERVICE_TOOMANY; - } + if (addrs.size() == 0) { + return MESSAGE_SERVICE_UNKNOWN; + } else if (addrs.size() == 1) { + *addr = addrs[0]; + } else { + if (!GetIp6AddressFromVector(addrs, addr)) { + /// default policy can't make choice for you here, overide GetDestination() function + /// to implement custom routing strategy for your service. + return MESSAGE_SERVICE_TOOMANY; + } } - - return MESSAGE_OK; + + return MESSAGE_OK; } - static const sockaddr_in* SockAddrIpV4(const IRemoteAddr& a) { - return (const sockaddr_in*)a.Addr(); + static const sockaddr_in* SockAddrIpV4(const IRemoteAddr& a) { + return (const sockaddr_in*)a.Addr(); } - static const sockaddr_in6* SockAddrIpV6(const IRemoteAddr& a) { - return (const sockaddr_in6*)a.Addr(); + static const sockaddr_in6* SockAddrIpV6(const IRemoteAddr& a) { + return (const sockaddr_in6*)a.Addr(); } - static bool IsAddressEqual(const IRemoteAddr& a1, const IRemoteAddr& a2) { - if (a1.Addr()->sa_family == a2.Addr()->sa_family) { - if (a1.Addr()->sa_family == AF_INET) { - return memcmp(&SockAddrIpV4(a1)->sin_addr, &SockAddrIpV4(a2)->sin_addr, sizeof(in_addr)) == 0; - } else { - return memcmp(&SockAddrIpV6(a1)->sin6_addr, &SockAddrIpV6(a2)->sin6_addr, sizeof(in6_addr)) == 0; - } + static bool IsAddressEqual(const IRemoteAddr& a1, const IRemoteAddr& a2) { + if (a1.Addr()->sa_family == a2.Addr()->sa_family) { + if (a1.Addr()->sa_family == AF_INET) { + return memcmp(&SockAddrIpV4(a1)->sin_addr, &SockAddrIpV4(a2)->sin_addr, sizeof(in_addr)) == 0; + } else { + return memcmp(&SockAddrIpV6(a1)->sin6_addr, &SockAddrIpV6(a2)->sin6_addr, sizeof(in6_addr)) == 0; + } } - return false; + return false; } - TBusLocator::TBusLocator() - : MyInterfaces(GetNetworkInterfaces()) - { - } + TBusLocator::TBusLocator() + : MyInterfaces(GetNetworkInterfaces()) + { + } - bool TBusLocator::TItem::operator<(const TItem& y) const { - const TItem& x = *this; + bool TBusLocator::TItem::operator<(const TItem& y) const { + const TItem& x = *this; - if (x.ServiceId == y.ServiceId) { - return (x.End < y.End) || ((x.End == y.End) && CompareByHost(x.Addr, y.Addr) < 0); + if (x.ServiceId == y.ServiceId) { + return (x.End < y.End) || ((x.End == y.End) && CompareByHost(x.Addr, y.Addr) < 0); } - return x.ServiceId < y.ServiceId; + return x.ServiceId < y.ServiceId; } - bool TBusLocator::TItem::operator==(const TItem& y) const { - return ServiceId == y.ServiceId && Start == y.Start && End == y.End && Addr == y.Addr; - } + bool TBusLocator::TItem::operator==(const TItem& y) const { + return ServiceId == y.ServiceId && Start == y.Start && End == y.End && Addr == y.Addr; + } - TBusLocator::TItem::TItem(TServiceId serviceId, TBusKey start, TBusKey end, const TNetAddr& addr) - : ServiceId(serviceId) - , Start(start) - , End(end) - , Addr(addr) - { + TBusLocator::TItem::TItem(TServiceId serviceId, TBusKey start, TBusKey end, const TNetAddr& addr) + : ServiceId(serviceId) + , Start(start) + , End(end) + , Addr(addr) + { } - bool TBusLocator::IsLocal(const TNetAddr& addr) { - for (const auto& myInterface : MyInterfaces) { - if (IsAddressEqual(addr, *myInterface.Address)) { - return true; - } - } + bool TBusLocator::IsLocal(const TNetAddr& addr) { + for (const auto& myInterface : MyInterfaces) { + if (IsAddressEqual(addr, *myInterface.Address)) { + return true; + } + } - return false; - } + return false; + } - TBusLocator::TServiceId TBusLocator::GetServiceId(const char* name) { - const char* c = ServiceIdSet.insert(name).first->c_str(); - return (ui64)c; + TBusLocator::TServiceId TBusLocator::GetServiceId(const char* name) { + const char* c = ServiceIdSet.insert(name).first->c_str(); + return (ui64)c; } - int TBusLocator::RegisterBreak(TBusService service, const TVector<TBusKey>& starts, const TNetAddr& addr) { - TGuard<TMutex> G(Lock); + int TBusLocator::RegisterBreak(TBusService service, const TVector<TBusKey>& starts, const TNetAddr& addr) { + TGuard<TMutex> G(Lock); - TServiceId serviceId = GetServiceId(service); - for (size_t i = 0; i < starts.size(); ++i) { - RegisterBreak(serviceId, starts[i], addr); - } - return 0; + TServiceId serviceId = GetServiceId(service); + for (size_t i = 0; i < starts.size(); ++i) { + RegisterBreak(serviceId, starts[i], addr); + } + return 0; } - int TBusLocator::RegisterBreak(TServiceId serviceId, const TBusKey start, const TNetAddr& addr) { - TItems::const_iterator it = Items.lower_bound(TItem(serviceId, 0, start, addr)); - TItems::const_iterator service_it = + int TBusLocator::RegisterBreak(TServiceId serviceId, const TBusKey start, const TNetAddr& addr) { + TItems::const_iterator it = Items.lower_bound(TItem(serviceId, 0, start, addr)); + TItems::const_iterator service_it = Items.lower_bound(TItem(serviceId, 0, 0, TNetAddr())); - THolder<TItem> left; - THolder<TItem> right; - if ((it != Items.end() || Items.begin() != Items.end()) && service_it != Items.end() && service_it->ServiceId == serviceId) { - if (it == Items.end()) { - --it; - } - const TItem& item = *it; - left.Reset(new TItem(serviceId, item.Start, - Max<TBusKey>(item.Start, start - 1), item.Addr)); - right.Reset(new TItem(serviceId, start, item.End, addr)); - Items.erase(*it); - } else { - left.Reset(new TItem(serviceId, YBUS_KEYMIN, start, addr)); - if (start < YBUS_KEYMAX) { - right.Reset(new TItem(serviceId, start + 1, YBUS_KEYMAX, addr)); - } + THolder<TItem> left; + THolder<TItem> right; + if ((it != Items.end() || Items.begin() != Items.end()) && service_it != Items.end() && service_it->ServiceId == serviceId) { + if (it == Items.end()) { + --it; + } + const TItem& item = *it; + left.Reset(new TItem(serviceId, item.Start, + Max<TBusKey>(item.Start, start - 1), item.Addr)); + right.Reset(new TItem(serviceId, start, item.End, addr)); + Items.erase(*it); + } else { + left.Reset(new TItem(serviceId, YBUS_KEYMIN, start, addr)); + if (start < YBUS_KEYMAX) { + right.Reset(new TItem(serviceId, start + 1, YBUS_KEYMAX, addr)); + } } - Items.insert(*left); - Items.insert(*right); - NormalizeBreaks(serviceId); - return 0; + Items.insert(*left); + Items.insert(*right); + NormalizeBreaks(serviceId); + return 0; + } + + int TBusLocator::UnregisterBreak(TBusService service, const TNetAddr& addr) { + TGuard<TMutex> G(Lock); + + TServiceId serviceId = GetServiceId(service); + return UnregisterBreak(serviceId, addr); } - int TBusLocator::UnregisterBreak(TBusService service, const TNetAddr& addr) { - TGuard<TMutex> G(Lock); - - TServiceId serviceId = GetServiceId(service); - return UnregisterBreak(serviceId, addr); - } - - int TBusLocator::UnregisterBreak(TServiceId serviceId, const TNetAddr& addr) { - int deleted = 0; - TItems::iterator it = Items.begin(); - while (it != Items.end()) { - const TItem& item = *it; - if (item.ServiceId != serviceId) { - ++it; - continue; - } - TItems::iterator itErase = it++; - if (item.ServiceId == serviceId && item.Addr == addr) { - Items.erase(itErase); - deleted += 1; - } - } - - if (Items.begin() == Items.end()) { - return deleted; - } - TBusKey keyItem = YBUS_KEYMAX; - it = Items.end(); - TItems::iterator first = it; - do { - --it; - // item.Start is not used in set comparison function - // so you can't violate set sort order by changing it - // hence const_cast() - TItem& item = const_cast<TItem&>(*it); - if (item.ServiceId != serviceId) { - continue; - } - first = it; - if (item.End < keyItem) { - item.End = keyItem; - } - keyItem = item.Start - 1; - } while (it != Items.begin()); - - if (first != Items.end() && first->Start != 0) { - TItem item(serviceId, YBUS_KEYMIN, first->Start - 1, first->Addr); - Items.insert(item); + int TBusLocator::UnregisterBreak(TServiceId serviceId, const TNetAddr& addr) { + int deleted = 0; + TItems::iterator it = Items.begin(); + while (it != Items.end()) { + const TItem& item = *it; + if (item.ServiceId != serviceId) { + ++it; + continue; + } + TItems::iterator itErase = it++; + if (item.ServiceId == serviceId && item.Addr == addr) { + Items.erase(itErase); + deleted += 1; + } } - NormalizeBreaks(serviceId); + if (Items.begin() == Items.end()) { + return deleted; + } + TBusKey keyItem = YBUS_KEYMAX; + it = Items.end(); + TItems::iterator first = it; + do { + --it; + // item.Start is not used in set comparison function + // so you can't violate set sort order by changing it + // hence const_cast() + TItem& item = const_cast<TItem&>(*it); + if (item.ServiceId != serviceId) { + continue; + } + first = it; + if (item.End < keyItem) { + item.End = keyItem; + } + keyItem = item.Start - 1; + } while (it != Items.begin()); + + if (first != Items.end() && first->Start != 0) { + TItem item(serviceId, YBUS_KEYMIN, first->Start - 1, first->Addr); + Items.insert(item); + } + + NormalizeBreaks(serviceId); return deleted; } - void TBusLocator::NormalizeBreaks(TServiceId serviceId) { - TItems::const_iterator first = Items.lower_bound(TItem(serviceId, YBUS_KEYMIN, YBUS_KEYMIN, TNetAddr())); - TItems::const_iterator last = Items.end(); + void TBusLocator::NormalizeBreaks(TServiceId serviceId) { + TItems::const_iterator first = Items.lower_bound(TItem(serviceId, YBUS_KEYMIN, YBUS_KEYMIN, TNetAddr())); + TItems::const_iterator last = Items.end(); - if ((Items.end() != first) && (first->ServiceId == serviceId)) { - if (serviceId != Max<TServiceId>()) { - last = Items.lower_bound(TItem(serviceId + 1, YBUS_KEYMIN, YBUS_KEYMIN, TNetAddr())); - } + if ((Items.end() != first) && (first->ServiceId == serviceId)) { + if (serviceId != Max<TServiceId>()) { + last = Items.lower_bound(TItem(serviceId + 1, YBUS_KEYMIN, YBUS_KEYMIN, TNetAddr())); + } - --last; - Y_ASSERT(Items.end() != last); - Y_ASSERT(last->ServiceId == serviceId); + --last; + Y_ASSERT(Items.end() != last); + Y_ASSERT(last->ServiceId == serviceId); - TItem& beg = const_cast<TItem&>(*first); - beg.Addr = last->Addr; + TItem& beg = const_cast<TItem&>(*first); + beg.Addr = last->Addr; } } - int TBusLocator::LocateAll(TBusService service, TBusKey key, TVector<TNetAddr>& addrs) { - TGuard<TMutex> G(Lock); - Y_VERIFY(addrs.empty(), "Non emtpy addresses"); + int TBusLocator::LocateAll(TBusService service, TBusKey key, TVector<TNetAddr>& addrs) { + TGuard<TMutex> G(Lock); + Y_VERIFY(addrs.empty(), "Non emtpy addresses"); - TServiceId serviceId = GetServiceId(service); - TItems::const_iterator it; + TServiceId serviceId = GetServiceId(service); + TItems::const_iterator it; - for (it = Items.lower_bound(TItem(serviceId, 0, key, TNetAddr())); - it != Items.end() && it->ServiceId == serviceId && it->Start <= key && key <= it->End; - ++it) { - const TItem& item = *it; - addrs.push_back(item.Addr); - } + for (it = Items.lower_bound(TItem(serviceId, 0, key, TNetAddr())); + it != Items.end() && it->ServiceId == serviceId && it->Start <= key && key <= it->End; + ++it) { + const TItem& item = *it; + addrs.push_back(item.Addr); + } - if (addrs.size() == 0) { - return -1; - } - return (int)addrs.size(); + if (addrs.size() == 0) { + return -1; + } + return (int)addrs.size(); } - - int TBusLocator::Locate(TBusService service, TBusKey key, TNetAddr* addr) { - TGuard<TMutex> G(Lock); - TServiceId serviceId = GetServiceId(service); - TItems::const_iterator it; + int TBusLocator::Locate(TBusService service, TBusKey key, TNetAddr* addr) { + TGuard<TMutex> G(Lock); - it = Items.lower_bound(TItem(serviceId, 0, key, TNetAddr())); + TServiceId serviceId = GetServiceId(service); + TItems::const_iterator it; - if (it != Items.end()) { - const TItem& item = *it; - if (item.ServiceId == serviceId && item.Start <= key && key < item.End) { - *addr = item.Addr; + it = Items.lower_bound(TItem(serviceId, 0, key, TNetAddr())); - return 0; - } - } - - return -1; - } + if (it != Items.end()) { + const TItem& item = *it; + if (item.ServiceId == serviceId && item.Start <= key && key < item.End) { + *addr = item.Addr; - int TBusLocator::GetLocalPort(TBusService service) { - TGuard<TMutex> G(Lock); - TServiceId serviceId = GetServiceId(service); - TItems::const_iterator it; - int port = 0; - - for (it = Items.lower_bound(TItem(serviceId, 0, 0, TNetAddr())); it != Items.end(); ++it) { - const TItem& item = *it; - if (item.ServiceId != serviceId) { - break; - } - - if (IsLocal(item.Addr)) { - if (port != 0 && port != GetAddrPort(item.Addr)) { - Y_ASSERT(0 && "Can't decide which port to use."); - return 0; - } - port = GetAddrPort(item.Addr); - } + return 0; + } } - return port; + return -1; } - int TBusLocator::GetLocalAddresses(TBusService service, TVector<TNetAddr>& addrs) { - TGuard<TMutex> G(Lock); - TServiceId serviceId = GetServiceId(service); - TItems::const_iterator it; + int TBusLocator::GetLocalPort(TBusService service) { + TGuard<TMutex> G(Lock); + TServiceId serviceId = GetServiceId(service); + TItems::const_iterator it; + int port = 0; + + for (it = Items.lower_bound(TItem(serviceId, 0, 0, TNetAddr())); it != Items.end(); ++it) { + const TItem& item = *it; + if (item.ServiceId != serviceId) { + break; + } + + if (IsLocal(item.Addr)) { + if (port != 0 && port != GetAddrPort(item.Addr)) { + Y_ASSERT(0 && "Can't decide which port to use."); + return 0; + } + port = GetAddrPort(item.Addr); + } + } - for (it = Items.lower_bound(TItem(serviceId, 0, 0, TNetAddr())); it != Items.end(); ++it) { - const TItem& item = *it; - if (item.ServiceId != serviceId) { - break; - } + return port; + } - if (IsLocal(item.Addr)) { - addrs.push_back(item.Addr); - } + int TBusLocator::GetLocalAddresses(TBusService service, TVector<TNetAddr>& addrs) { + TGuard<TMutex> G(Lock); + TServiceId serviceId = GetServiceId(service); + TItems::const_iterator it; + + for (it = Items.lower_bound(TItem(serviceId, 0, 0, TNetAddr())); it != Items.end(); ++it) { + const TItem& item = *it; + if (item.ServiceId != serviceId) { + break; + } + + if (IsLocal(item.Addr)) { + addrs.push_back(item.Addr); + } } - if (addrs.size() == 0) { - return -1; + if (addrs.size() == 0) { + return -1; } - return (int)addrs.size(); + return (int)addrs.size(); } - int TBusLocator::LocateHost(TBusService service, TBusKey key, TString* host, int* port, bool* isLocal) { - int ret; - TNetAddr addr; - ret = Locate(service, key, &addr); - if (ret != 0) { - return ret; - } - - { - TGuard<TMutex> G(Lock); - THostAddrMap::const_iterator it = HostAddrMap.find(addr); - if (it == HostAddrMap.end()) { - return -1; - } - *host = it->second; - } - - *port = GetAddrPort(addr); - if (isLocal != nullptr) { - *isLocal = IsLocal(addr); - } - return 0; + int TBusLocator::LocateHost(TBusService service, TBusKey key, TString* host, int* port, bool* isLocal) { + int ret; + TNetAddr addr; + ret = Locate(service, key, &addr); + if (ret != 0) { + return ret; + } + + { + TGuard<TMutex> G(Lock); + THostAddrMap::const_iterator it = HostAddrMap.find(addr); + if (it == HostAddrMap.end()) { + return -1; + } + *host = it->second; + } + + *port = GetAddrPort(addr); + if (isLocal != nullptr) { + *isLocal = IsLocal(addr); + } + return 0; } - int TBusLocator::LocateKeys(TBusService service, TBusKeyVec& keys, bool onlyLocal) { + int TBusLocator::LocateKeys(TBusService service, TBusKeyVec& keys, bool onlyLocal) { TGuard<TMutex> G(Lock); - Y_VERIFY(keys.empty(), "Non empty keys"); - - TServiceId serviceId = GetServiceId(service); - TItems::const_iterator it; - for (it = Items.begin(); it != Items.end(); ++it) { - const TItem& item = *it; - if (item.ServiceId != serviceId) { - continue; - } - if (onlyLocal && !IsLocal(item.Addr)) { - continue; - } - keys.push_back(std::make_pair(item.Start, item.End)); + Y_VERIFY(keys.empty(), "Non empty keys"); + + TServiceId serviceId = GetServiceId(service); + TItems::const_iterator it; + for (it = Items.begin(); it != Items.end(); ++it) { + const TItem& item = *it; + if (item.ServiceId != serviceId) { + continue; + } + if (onlyLocal && !IsLocal(item.Addr)) { + continue; + } + keys.push_back(std::make_pair(item.Start, item.End)); } - return (int)keys.size(); + return (int)keys.size(); } - int TBusLocator::Register(TBusService service, const char* hostName, int port, TBusKey start /*= YBUS_KEYMIN*/, TBusKey end /*= YBUS_KEYMAX*/, EIpVersion requireVersion /*= EIP_VERSION_4*/, EIpVersion preferVersion /*= EIP_VERSION_ANY*/) { - TNetAddr addr(hostName, port, requireVersion, preferVersion); // throws - { - TGuard<TMutex> G(Lock); - HostAddrMap[addr] = hostName; - } - Register(service, start, end, addr); - return 0; + int TBusLocator::Register(TBusService service, const char* hostName, int port, TBusKey start /*= YBUS_KEYMIN*/, TBusKey end /*= YBUS_KEYMAX*/, EIpVersion requireVersion /*= EIP_VERSION_4*/, EIpVersion preferVersion /*= EIP_VERSION_ANY*/) { + TNetAddr addr(hostName, port, requireVersion, preferVersion); // throws + { + TGuard<TMutex> G(Lock); + HostAddrMap[addr] = hostName; + } + Register(service, start, end, addr); + return 0; } - int TBusLocator::Register(TBusService service, TBusKey start, TBusKey end, const TNetworkAddress& na, EIpVersion requireVersion /*= EIP_VERSION_4*/, EIpVersion preferVersion /*= EIP_VERSION_ANY*/) { - TNetAddr addr(na, requireVersion, preferVersion); // throws - Register(service, start, end, addr); - return 0; + int TBusLocator::Register(TBusService service, TBusKey start, TBusKey end, const TNetworkAddress& na, EIpVersion requireVersion /*= EIP_VERSION_4*/, EIpVersion preferVersion /*= EIP_VERSION_ANY*/) { + TNetAddr addr(na, requireVersion, preferVersion); // throws + Register(service, start, end, addr); + return 0; } - int TBusLocator::Register(TBusService service, TBusKey start, TBusKey end, const TNetAddr& addr) { + int TBusLocator::Register(TBusService service, TBusKey start, TBusKey end, const TNetAddr& addr) { TGuard<TMutex> G(Lock); - TServiceId serviceId = GetServiceId(service); - TItems::const_iterator it; - - TItem itemToReg(serviceId, start, end, addr); - for (it = Items.lower_bound(TItem(serviceId, 0, start, TNetAddr())); - it != Items.end() && it->ServiceId == serviceId; - ++it) { - const TItem& item = *it; - if (item == itemToReg) { - return 0; - } - if ((item.Start < start && start < item.End) || (item.Start < end && end < item.End)) { - Y_FAIL("Overlap in registered keys with non-identical range"); - } - } - - Items.insert(itemToReg); - return 0; - } - - int TBusLocator::Unregister(TBusService service, TBusKey start, TBusKey end) { - TGuard<TMutex> G(Lock); - TServiceId serviceId = GetServiceId(service); - Items.erase(TItem(serviceId, start, end, TNetAddr())); - return 0; + TServiceId serviceId = GetServiceId(service); + TItems::const_iterator it; + + TItem itemToReg(serviceId, start, end, addr); + for (it = Items.lower_bound(TItem(serviceId, 0, start, TNetAddr())); + it != Items.end() && it->ServiceId == serviceId; + ++it) { + const TItem& item = *it; + if (item == itemToReg) { + return 0; + } + if ((item.Start < start && start < item.End) || (item.Start < end && end < item.End)) { + Y_FAIL("Overlap in registered keys with non-identical range"); + } + } + + Items.insert(itemToReg); + return 0; + } + + int TBusLocator::Unregister(TBusService service, TBusKey start, TBusKey end) { + TGuard<TMutex> G(Lock); + TServiceId serviceId = GetServiceId(service); + Items.erase(TItem(serviceId, start, end, TNetAddr())); + return 0; } } diff --git a/library/cpp/messagebus/locator.h b/library/cpp/messagebus/locator.h index 2c455ac53e..f8556a3fce 100644 --- a/library/cpp/messagebus/locator.h +++ b/library/cpp/messagebus/locator.h @@ -10,84 +10,84 @@ #include <util/system/mutex.h> namespace NBus { - /////////////////////////////////////////////// - /// \brief Client interface to locator service + /////////////////////////////////////////////// + /// \brief Client interface to locator service - /// This interface abstracts clustering/location service that - /// allows clients find servers (address, port) using "name" and "key". - /// The instance lives in TBusMessageQueue-object, but can be shared by different queues. - class TBusLocator: public TAtomicRefCount<TBusLocator>, public TNonCopyable { - private: - typedef ui64 TServiceId; - typedef TSet<TString> TServiceIdSet; - TServiceIdSet ServiceIdSet; - TServiceId GetServiceId(const char* name); + /// This interface abstracts clustering/location service that + /// allows clients find servers (address, port) using "name" and "key". + /// The instance lives in TBusMessageQueue-object, but can be shared by different queues. + class TBusLocator: public TAtomicRefCount<TBusLocator>, public TNonCopyable { + private: + typedef ui64 TServiceId; + typedef TSet<TString> TServiceIdSet; + TServiceIdSet ServiceIdSet; + TServiceId GetServiceId(const char* name); - typedef TMap<TNetAddr, TString> THostAddrMap; - THostAddrMap HostAddrMap; + typedef TMap<TNetAddr, TString> THostAddrMap; + THostAddrMap HostAddrMap; - TNetworkInterfaceList MyInterfaces; + TNetworkInterfaceList MyInterfaces; - struct TItem { - TServiceId ServiceId; - TBusKey Start; - TBusKey End; - TNetAddr Addr; + struct TItem { + TServiceId ServiceId; + TBusKey Start; + TBusKey End; + TNetAddr Addr; - bool operator<(const TItem& y) const; + bool operator<(const TItem& y) const; - bool operator==(const TItem& y) const; + bool operator==(const TItem& y) const; - TItem(TServiceId serviceId, TBusKey start, TBusKey end, const TNetAddr& addr); - }; + TItem(TServiceId serviceId, TBusKey start, TBusKey end, const TNetAddr& addr); + }; - typedef TMultiSet<TItem> TItems; - TItems Items; - TMutex Lock; + typedef TMultiSet<TItem> TItems; + TItems Items; + TMutex Lock; - int RegisterBreak(TServiceId serviceId, const TBusKey start, const TNetAddr& addr); - int UnregisterBreak(TServiceId serviceId, const TNetAddr& addr); + int RegisterBreak(TServiceId serviceId, const TBusKey start, const TNetAddr& addr); + int UnregisterBreak(TServiceId serviceId, const TNetAddr& addr); - void NormalizeBreaks(TServiceId serviceId); + void NormalizeBreaks(TServiceId serviceId); - private: - int Register(TBusService service, TBusKey start, TBusKey end, const TNetAddr& addr); + private: + int Register(TBusService service, TBusKey start, TBusKey end, const TNetAddr& addr); - public: - /// creates instance that obtains location table from locator server (not implemented) - TBusLocator(); + public: + /// creates instance that obtains location table from locator server (not implemented) + TBusLocator(); - /// returns true if this address is on the same node for YBUS_KEYLOCAL - bool IsLocal(const TNetAddr& addr); + /// returns true if this address is on the same node for YBUS_KEYLOCAL + bool IsLocal(const TNetAddr& addr); - /// returns first address for service and key - int Locate(TBusService service, TBusKey key, TNetAddr* addr); + /// returns first address for service and key + int Locate(TBusService service, TBusKey key, TNetAddr* addr); - /// returns all addresses mathing service and key - int LocateAll(TBusService service, TBusKey key, TVector<TNetAddr>& addrs); + /// returns all addresses mathing service and key + int LocateAll(TBusService service, TBusKey key, TVector<TNetAddr>& addrs); - /// returns actual host name for service and key - int LocateHost(TBusService service, TBusKey key, TString* host, int* port, bool* isLocal = nullptr); + /// returns actual host name for service and key + int LocateHost(TBusService service, TBusKey key, TString* host, int* port, bool* isLocal = nullptr); - /// returns all key ranges for the given service - int LocateKeys(TBusService service, TBusKeyVec& keys, bool onlyLocal = false); + /// returns all key ranges for the given service + int LocateKeys(TBusService service, TBusKeyVec& keys, bool onlyLocal = false); - /// returns port on the local node for the service - int GetLocalPort(TBusService service); + /// returns port on the local node for the service + int GetLocalPort(TBusService service); - /// returns addresses of the local node for the service - int GetLocalAddresses(TBusService service, TVector<TNetAddr>& addrs); + /// returns addresses of the local node for the service + int GetLocalAddresses(TBusService service, TVector<TNetAddr>& addrs); - /// register service instance - int Register(TBusService service, TBusKey start, TBusKey end, const TNetworkAddress& addr, EIpVersion requireVersion = EIP_VERSION_4, EIpVersion preferVersion = EIP_VERSION_ANY); - /// @throws yexception - int Register(TBusService service, const char* host, int port, TBusKey start = YBUS_KEYMIN, TBusKey end = YBUS_KEYMAX, EIpVersion requireVersion = EIP_VERSION_4, EIpVersion preferVersion = EIP_VERSION_ANY); + /// register service instance + int Register(TBusService service, TBusKey start, TBusKey end, const TNetworkAddress& addr, EIpVersion requireVersion = EIP_VERSION_4, EIpVersion preferVersion = EIP_VERSION_ANY); + /// @throws yexception + int Register(TBusService service, const char* host, int port, TBusKey start = YBUS_KEYMIN, TBusKey end = YBUS_KEYMAX, EIpVersion requireVersion = EIP_VERSION_4, EIpVersion preferVersion = EIP_VERSION_ANY); - /// unregister service instance - int Unregister(TBusService service, TBusKey start, TBusKey end); + /// unregister service instance + int Unregister(TBusService service, TBusKey start, TBusKey end); - int RegisterBreak(TBusService service, const TVector<TBusKey>& starts, const TNetAddr& addr); - int UnregisterBreak(TBusService service, const TNetAddr& addr); - }; + int RegisterBreak(TBusService service, const TVector<TBusKey>& starts, const TNetAddr& addr); + int UnregisterBreak(TBusService service, const TNetAddr& addr); + }; -} +} diff --git a/library/cpp/messagebus/mb_lwtrace.h b/library/cpp/messagebus/mb_lwtrace.h index f8d5c7afe7..e62728b265 100644 --- a/library/cpp/messagebus/mb_lwtrace.h +++ b/library/cpp/messagebus/mb_lwtrace.h @@ -4,12 +4,12 @@ #include <util/generic/string.h> -#define LWTRACE_MESSAGEBUS_PROVIDER(PROBE, EVENT, GROUPS, TYPES, NAMES) \ +#define LWTRACE_MESSAGEBUS_PROVIDER(PROBE, EVENT, GROUPS, TYPES, NAMES) \ PROBE(Error, GROUPS("MessagebusRare"), TYPES(TString, TString, TString), NAMES("status", "address", "misc")) \ - PROBE(ServerUnknownVersion, GROUPS("MessagebusRare"), TYPES(TString, ui32), NAMES("address", "version")) \ - PROBE(Accepted, GROUPS("MessagebusRare"), TYPES(TString), NAMES("address")) \ - PROBE(Disconnected, GROUPS("MessagebusRare"), TYPES(TString), NAMES("address")) \ - PROBE(Read, GROUPS(), TYPES(ui32), NAMES("size")) \ + PROBE(ServerUnknownVersion, GROUPS("MessagebusRare"), TYPES(TString, ui32), NAMES("address", "version")) \ + PROBE(Accepted, GROUPS("MessagebusRare"), TYPES(TString), NAMES("address")) \ + PROBE(Disconnected, GROUPS("MessagebusRare"), TYPES(TString), NAMES("address")) \ + PROBE(Read, GROUPS(), TYPES(ui32), NAMES("size")) \ /**/ LWTRACE_DECLARE_PROVIDER(LWTRACE_MESSAGEBUS_PROVIDER) diff --git a/library/cpp/messagebus/memory.h b/library/cpp/messagebus/memory.h index 2c79d3ec7a..b2c0544491 100644 --- a/library/cpp/messagebus/memory.h +++ b/library/cpp/messagebus/memory.h @@ -11,14 +11,14 @@ #define CACHE_LINE_PADDING char UNIQUE_NAME[CACHE_LINE_SIZE]; static inline void* MallocAligned(size_t size, size_t alignment) { - void** ptr = (void**)malloc(size + alignment + sizeof(size_t*)); + void** ptr = (void**)malloc(size + alignment + sizeof(size_t*)); if (!ptr) { return nullptr; } size_t mask = ~(alignment - 1); intptr_t roundedDown = intptr_t(ptr) & mask; - void** alignedPtr = (void**)(roundedDown + alignment); + void** alignedPtr = (void**)(roundedDown + alignment); alignedPtr[-1] = ptr; return alignedPtr; } @@ -28,8 +28,8 @@ static inline void FreeAligned(void* ptr) { return; } - void** typedPtr = (void**)ptr; - void* originalPtr = typedPtr[-1]; + void** typedPtr = (void**)ptr; + void* originalPtr = typedPtr[-1]; free(originalPtr); } diff --git a/library/cpp/messagebus/message.cpp b/library/cpp/messagebus/message.cpp index a57e235977..bfa7ed8e9b 100644 --- a/library/cpp/messagebus/message.cpp +++ b/library/cpp/messagebus/message.cpp @@ -10,185 +10,185 @@ using namespace NBus; namespace NBus { - using namespace NBus::NPrivate; - - TBusIdentity::TBusIdentity() - : MessageId(0) - , Size(0) - , Flags(0) - , LocalFlags(0) - { - } + using namespace NBus::NPrivate; + + TBusIdentity::TBusIdentity() + : MessageId(0) + , Size(0) + , Flags(0) + , LocalFlags(0) + { + } - TBusIdentity::~TBusIdentity() { + TBusIdentity::~TBusIdentity() { // TODO: print local flags #ifndef NDEBUG - Y_VERIFY(LocalFlags == 0, "local flags must be zero at this point; message type is %s", + Y_VERIFY(LocalFlags == 0, "local flags must be zero at this point; message type is %s", MessageType.value_or("unknown").c_str()); #else - Y_VERIFY(LocalFlags == 0, "local flags must be zero at this point"); + Y_VERIFY(LocalFlags == 0, "local flags must be zero at this point"); #endif - } - - TNetAddr TBusIdentity::GetNetAddr() const { - if (!!Connection) { - return Connection->GetAddr(); - } else { - Y_FAIL(); - } - } - - void TBusIdentity::Pack(char* dest) { - memcpy(dest, this, sizeof(TBusIdentity)); - LocalFlags = 0; - - // prevent decref - new (&Connection) TIntrusivePtr<TRemoteServerConnection>; - } - - void TBusIdentity::Unpack(const char* src) { - Y_VERIFY(LocalFlags == 0); - Y_VERIFY(!Connection); - - memcpy(this, src, sizeof(TBusIdentity)); - } - - void TBusHeader::GenerateId() { - for (;;) { - Id = RandomNumber<TBusKey>(); - // Skip reserved ids - if (IsBusKeyValid(Id)) - return; - } - } - - TBusMessage::TBusMessage(ui16 type, int approxsize) - //: TCtr("BusMessage") - : TRefCounted<TBusMessage, TAtomicCounter, TDelete>(1) - , LocalFlags(0) - , RequestSize(0) - , Data(nullptr) - { - Y_UNUSED(approxsize); - GetHeader()->Type = type; - DoReset(); - } - - TBusMessage::TBusMessage(ECreateUninitialized) - //: TCtr("BusMessage") - : TRefCounted<TBusMessage, TAtomicCounter, TDelete>(1) - , LocalFlags(0) - , Data(nullptr) - { - } - - TString TBusMessage::Describe() const { + } + + TNetAddr TBusIdentity::GetNetAddr() const { + if (!!Connection) { + return Connection->GetAddr(); + } else { + Y_FAIL(); + } + } + + void TBusIdentity::Pack(char* dest) { + memcpy(dest, this, sizeof(TBusIdentity)); + LocalFlags = 0; + + // prevent decref + new (&Connection) TIntrusivePtr<TRemoteServerConnection>; + } + + void TBusIdentity::Unpack(const char* src) { + Y_VERIFY(LocalFlags == 0); + Y_VERIFY(!Connection); + + memcpy(this, src, sizeof(TBusIdentity)); + } + + void TBusHeader::GenerateId() { + for (;;) { + Id = RandomNumber<TBusKey>(); + // Skip reserved ids + if (IsBusKeyValid(Id)) + return; + } + } + + TBusMessage::TBusMessage(ui16 type, int approxsize) + //: TCtr("BusMessage") + : TRefCounted<TBusMessage, TAtomicCounter, TDelete>(1) + , LocalFlags(0) + , RequestSize(0) + , Data(nullptr) + { + Y_UNUSED(approxsize); + GetHeader()->Type = type; + DoReset(); + } + + TBusMessage::TBusMessage(ECreateUninitialized) + //: TCtr("BusMessage") + : TRefCounted<TBusMessage, TAtomicCounter, TDelete>(1) + , LocalFlags(0) + , Data(nullptr) + { + } + + TString TBusMessage::Describe() const { return Sprintf("object type: %s, message type: %d", TypeName(*this).data(), int(GetHeader()->Type)); - } + } - TBusMessage::~TBusMessage() { + TBusMessage::~TBusMessage() { #ifndef NDEBUG - Y_VERIFY(GetHeader()->Id != YBUS_KEYINVALID, "must not be invalid key, message type: %d, ", int(Type)); - GetHeader()->Id = YBUS_KEYINVALID; - Data = (void*)17; - CheckClean(); + Y_VERIFY(GetHeader()->Id != YBUS_KEYINVALID, "must not be invalid key, message type: %d, ", int(Type)); + GetHeader()->Id = YBUS_KEYINVALID; + Data = (void*)17; + CheckClean(); #endif - } - - void TBusMessage::DoReset() { - GetHeader()->SendTime = 0; - GetHeader()->Size = 0; - GetHeader()->FlagsInternal = 0; - GetHeader()->GenerateId(); - GetHeader()->SetVersionInternal(); - } - - void TBusMessage::Reset() { - CheckClean(); - DoReset(); - } - - void TBusMessage::CheckClean() const { - if (Y_UNLIKELY(LocalFlags != 0)) { - TString describe = Describe(); - TString localFlags = LocalFlagSetToString(LocalFlags); + } + + void TBusMessage::DoReset() { + GetHeader()->SendTime = 0; + GetHeader()->Size = 0; + GetHeader()->FlagsInternal = 0; + GetHeader()->GenerateId(); + GetHeader()->SetVersionInternal(); + } + + void TBusMessage::Reset() { + CheckClean(); + DoReset(); + } + + void TBusMessage::CheckClean() const { + if (Y_UNLIKELY(LocalFlags != 0)) { + TString describe = Describe(); + TString localFlags = LocalFlagSetToString(LocalFlags); Y_FAIL("message local flags must be zero, got: %s, message: %s", localFlags.data(), describe.data()); - } - } - - /////////////////////////////////////////////////////// - /// \brief Unpacks header from network order - - /// \todo ntoh instead of memcpy - int TBusHeader::ReadHeader(TArrayRef<const char> data) { - Y_ASSERT(data.size() >= sizeof(TBusHeader)); - memcpy(this, data.data(), sizeof(TBusHeader)); - return sizeof(TBusHeader); - } - - /////////////////////////////////////////////////////// - /// \brief Packs header to network order - - ////////////////////////////////////////////////////////// - /// \brief serialize message identity to be used to construct reply message - - /// function stores messageid, flags and connection reply address into the buffer - /// that can later be used to construct a reply to the message - void TBusMessage::GetIdentity(TBusIdentity& data) const { - data.MessageId = GetHeader()->Id; - data.Size = GetHeader()->Size; - data.Flags = GetHeader()->FlagsInternal; - //data.LocalFlags = LocalFlags; - } - - //////////////////////////////////////////////////////////// - /// \brief set message identity from serialized form - - /// function restores messageid, flags and connection reply address from the buffer - /// into the reply message - void TBusMessage::SetIdentity(const TBusIdentity& data) { - // TODO: wrong assertion: YBUS_KEYMIN is 0 - Y_ASSERT(data.MessageId != 0); - bool compressed = IsCompressed(); - GetHeader()->Id = data.MessageId; - GetHeader()->FlagsInternal = data.Flags; - LocalFlags = data.LocalFlags & ~MESSAGE_IN_WORK; - ReplyTo = data.Connection->PeerAddrSocketAddr; - SetCompressed(compressed || IsCompressedResponse()); - } - - void TBusMessage::SetCompressed(bool v) { - if (v) { - GetHeader()->FlagsInternal |= MESSAGE_COMPRESS_INTERNAL; - } else { - GetHeader()->FlagsInternal &= ~(MESSAGE_COMPRESS_INTERNAL); - } - } - - void TBusMessage::SetCompressedResponse(bool v) { - if (v) { - GetHeader()->FlagsInternal |= MESSAGE_COMPRESS_RESPONSE; - } else { - GetHeader()->FlagsInternal &= ~(MESSAGE_COMPRESS_RESPONSE); - } - } - - TString TBusIdentity::ToString() const { - TStringStream ss; - ss << "msg-id=" << MessageId - << " size=" << Size; - if (!!Connection) { - ss << " conn=" << Connection->GetAddr(); - } - ss + } + } + + /////////////////////////////////////////////////////// + /// \brief Unpacks header from network order + + /// \todo ntoh instead of memcpy + int TBusHeader::ReadHeader(TArrayRef<const char> data) { + Y_ASSERT(data.size() >= sizeof(TBusHeader)); + memcpy(this, data.data(), sizeof(TBusHeader)); + return sizeof(TBusHeader); + } + + /////////////////////////////////////////////////////// + /// \brief Packs header to network order + + ////////////////////////////////////////////////////////// + /// \brief serialize message identity to be used to construct reply message + + /// function stores messageid, flags and connection reply address into the buffer + /// that can later be used to construct a reply to the message + void TBusMessage::GetIdentity(TBusIdentity& data) const { + data.MessageId = GetHeader()->Id; + data.Size = GetHeader()->Size; + data.Flags = GetHeader()->FlagsInternal; + //data.LocalFlags = LocalFlags; + } + + //////////////////////////////////////////////////////////// + /// \brief set message identity from serialized form + + /// function restores messageid, flags and connection reply address from the buffer + /// into the reply message + void TBusMessage::SetIdentity(const TBusIdentity& data) { + // TODO: wrong assertion: YBUS_KEYMIN is 0 + Y_ASSERT(data.MessageId != 0); + bool compressed = IsCompressed(); + GetHeader()->Id = data.MessageId; + GetHeader()->FlagsInternal = data.Flags; + LocalFlags = data.LocalFlags & ~MESSAGE_IN_WORK; + ReplyTo = data.Connection->PeerAddrSocketAddr; + SetCompressed(compressed || IsCompressedResponse()); + } + + void TBusMessage::SetCompressed(bool v) { + if (v) { + GetHeader()->FlagsInternal |= MESSAGE_COMPRESS_INTERNAL; + } else { + GetHeader()->FlagsInternal &= ~(MESSAGE_COMPRESS_INTERNAL); + } + } + + void TBusMessage::SetCompressedResponse(bool v) { + if (v) { + GetHeader()->FlagsInternal |= MESSAGE_COMPRESS_RESPONSE; + } else { + GetHeader()->FlagsInternal &= ~(MESSAGE_COMPRESS_RESPONSE); + } + } + + TString TBusIdentity::ToString() const { + TStringStream ss; + ss << "msg-id=" << MessageId + << " size=" << Size; + if (!!Connection) { + ss << " conn=" << Connection->GetAddr(); + } + ss << " flags=" << Flags << " local-flags=" << LocalFlags #ifndef NDEBUG << " msg-type= " << MessageType.value_or("unknown").c_str() #endif ; - return ss.Str(); - } + return ss.Str(); + } } diff --git a/library/cpp/messagebus/message.h b/library/cpp/messagebus/message.h index 275f1859df..005ca10c65 100644 --- a/library/cpp/messagebus/message.h +++ b/library/cpp/messagebus/message.h @@ -18,98 +18,98 @@ #include <typeinfo> namespace NBus { - /////////////////////////////////////////////////////////////////// - /// \brief Structure to preserve identity from message to reply - struct TBusIdentity : TNonCopyable { - friend class TBusMessage; - friend class NPrivate::TRemoteServerSession; - friend struct NPrivate::TClientRequestImpl; - friend class TOnMessageContext; - - // TODO: make private - TBusKey MessageId; - - private: - ui32 Size; - TIntrusivePtr<NPrivate::TRemoteServerConnection> Connection; - ui16 Flags; - ui32 LocalFlags; - TInstant RecvTime; + /////////////////////////////////////////////////////////////////// + /// \brief Structure to preserve identity from message to reply + struct TBusIdentity : TNonCopyable { + friend class TBusMessage; + friend class NPrivate::TRemoteServerSession; + friend struct NPrivate::TClientRequestImpl; + friend class TOnMessageContext; + + // TODO: make private + TBusKey MessageId; + + private: + ui32 Size; + TIntrusivePtr<NPrivate::TRemoteServerConnection> Connection; + ui16 Flags; + ui32 LocalFlags; + TInstant RecvTime; #ifndef NDEBUG std::optional<TString> MessageType; #endif private: - // TODO: drop - TNetAddr GetNetAddr() const; - - public: - void Pack(char* dest); - void Unpack(const char* src); - - bool IsInWork() const { - return LocalFlags & NPrivate::MESSAGE_IN_WORK; - } - - // for internal use only - void BeginWork() { - SetInWork(true); - } - - // for internal use only - void EndWork() { - SetInWork(false); - } - - TBusIdentity(); - ~TBusIdentity(); - - void Swap(TBusIdentity& that) { - DoSwap(MessageId, that.MessageId); - DoSwap(Size, that.Size); - DoSwap(Connection, that.Connection); - DoSwap(Flags, that.Flags); - DoSwap(LocalFlags, that.LocalFlags); - DoSwap(RecvTime, that.RecvTime); + // TODO: drop + TNetAddr GetNetAddr() const; + + public: + void Pack(char* dest); + void Unpack(const char* src); + + bool IsInWork() const { + return LocalFlags & NPrivate::MESSAGE_IN_WORK; + } + + // for internal use only + void BeginWork() { + SetInWork(true); + } + + // for internal use only + void EndWork() { + SetInWork(false); + } + + TBusIdentity(); + ~TBusIdentity(); + + void Swap(TBusIdentity& that) { + DoSwap(MessageId, that.MessageId); + DoSwap(Size, that.Size); + DoSwap(Connection, that.Connection); + DoSwap(Flags, that.Flags); + DoSwap(LocalFlags, that.LocalFlags); + DoSwap(RecvTime, that.RecvTime); #ifndef NDEBUG - DoSwap(MessageType, that.MessageType); + DoSwap(MessageType, that.MessageType); #endif - } - - TString ToString() const; - - private: - void SetInWork(bool inWork) { - if (LocalFlags == 0 && inWork) { - LocalFlags = NPrivate::MESSAGE_IN_WORK; - } else if (LocalFlags == NPrivate::MESSAGE_IN_WORK && !inWork) { - LocalFlags = 0; - } else { - Y_FAIL("impossible combination of flag and parameter: %s %d", - inWork ? "true" : "false", unsigned(LocalFlags)); - } + } + + TString ToString() const; + + private: + void SetInWork(bool inWork) { + if (LocalFlags == 0 && inWork) { + LocalFlags = NPrivate::MESSAGE_IN_WORK; + } else if (LocalFlags == NPrivate::MESSAGE_IN_WORK && !inWork) { + LocalFlags = 0; + } else { + Y_FAIL("impossible combination of flag and parameter: %s %d", + inWork ? "true" : "false", unsigned(LocalFlags)); + } } void SetMessageType(const std::type_info& messageTypeInfo) { #ifndef NDEBUG - Y_VERIFY(!MessageType, "state check"); + Y_VERIFY(!MessageType, "state check"); MessageType = TypeName(messageTypeInfo); #else Y_UNUSED(messageTypeInfo); #endif - } - }; + } + }; - static const size_t BUS_IDENTITY_PACKED_SIZE = sizeof(TBusIdentity); + static const size_t BUS_IDENTITY_PACKED_SIZE = sizeof(TBusIdentity); - /////////////////////////////////////////////////////////////// - /// \brief Message flags in TBusHeader.Flags - enum EMessageFlags { - MESSAGE_COMPRESS_INTERNAL = 0x8000, ///< message is compressed - MESSAGE_COMPRESS_RESPONSE = 0x4000, ///< message prefers compressed response - MESSAGE_VERSION_INTERNAL = 0x00F0, ///< these bits are used as version - }; + /////////////////////////////////////////////////////////////// + /// \brief Message flags in TBusHeader.Flags + enum EMessageFlags { + MESSAGE_COMPRESS_INTERNAL = 0x8000, ///< message is compressed + MESSAGE_COMPRESS_RESPONSE = 0x4000, ///< message prefers compressed response + MESSAGE_VERSION_INTERNAL = 0x00F0, ///< these bits are used as version + }; ////////////////////////////////////////////////////////// /// \brief Message header present in all message send and received @@ -117,8 +117,8 @@ namespace NBus { /// This header is send into the wire. /// \todo fix for low/high end, 32/64bit some day #pragma pack(1) - struct TBusHeader { - friend class TBusMessage; + struct TBusHeader { + friend class TBusMessage; TBusKey Id = 0; ///< unique message ID ui32 Size = 0; ///< total size of the message @@ -126,147 +126,147 @@ namespace NBus { ui16 FlagsInternal = 0; ///< TRACE is one of the flags ui16 Type = 0; ///< to be used by TBusProtocol - int GetVersionInternal() { - return (FlagsInternal & MESSAGE_VERSION_INTERNAL) >> 4; - } - void SetVersionInternal(unsigned ver = YBUS_VERSION) { - FlagsInternal |= (ver << 4); - } - - public: - TBusHeader() { - } - TBusHeader(TArrayRef<const char> data) { - ReadHeader(data); - } - - private: - /// function for serialization/deserialization of the header - /// returns number of bytes written/read - int ReadHeader(TArrayRef<const char> data); - - void GenerateId(); - }; + int GetVersionInternal() { + return (FlagsInternal & MESSAGE_VERSION_INTERNAL) >> 4; + } + void SetVersionInternal(unsigned ver = YBUS_VERSION) { + FlagsInternal |= (ver << 4); + } + + public: + TBusHeader() { + } + TBusHeader(TArrayRef<const char> data) { + ReadHeader(data); + } + + private: + /// function for serialization/deserialization of the header + /// returns number of bytes written/read + int ReadHeader(TArrayRef<const char> data); + + void GenerateId(); + }; #pragma pack() -#define TBUSMAX_MESSAGE 26 * 1024 * 1024 + sizeof(NBus::TBusHeader) ///< is't it enough? -#define TBUSMIN_MESSAGE sizeof(NBus::TBusHeader) ///< can't be less then header - - inline bool IsVersionNegotiation(const NBus::TBusHeader& header) { - return header.Id == 0 && header.Size == sizeof(TBusHeader); - } - - ////////////////////////////////////////////////////////// - /// \brief Base class for all messages passed in the system - - enum ECreateUninitialized { - MESSAGE_CREATE_UNINITIALIZED, - }; - - class TBusMessage - : protected TBusHeader, - public TRefCounted<TBusMessage, TAtomicCounter, TDelete>, - private TNonCopyable { - friend class TLocalSession; - friend struct ::NBus::NPrivate::TBusSessionImpl; - friend class ::NBus::NPrivate::TRemoteServerSession; - friend class ::NBus::NPrivate::TRemoteClientSession; - friend class ::NBus::NPrivate::TRemoteConnection; - friend class ::NBus::NPrivate::TRemoteClientConnection; - friend class ::NBus::NPrivate::TRemoteServerConnection; - friend struct ::NBus::NPrivate::TBusMessagePtrAndHeader; - - private: - ui32 LocalFlags; - - /// connection identity for reply set by PushMessage() - NPrivate::TBusSocketAddr ReplyTo; - // server-side response only, hack - ui32 RequestSize; - - TInstant RecvTime; - - public: - /// constructor to create messages on sending end - TBusMessage(ui16 type, int approxsize = sizeof(TBusHeader)); - - /// constructor with serialzed data to examine the header - TBusMessage(ECreateUninitialized); - - // slow, for diagnostics only - virtual TString Describe() const; - - // must be called if this message object needs to be reused - void Reset(); - - void CheckClean() const; - - void SetCompressed(bool); - void SetCompressedResponse(bool); - - private: - bool IsCompressed() const { - return FlagsInternal & MESSAGE_COMPRESS_INTERNAL; - } - bool IsCompressedResponse() const { - return FlagsInternal & MESSAGE_COMPRESS_RESPONSE; - } - - public: - /// can have private data to destroy - virtual ~TBusMessage(); - - /// returns header of the message - TBusHeader* GetHeader() { - return this; - } - const TBusHeader* GetHeader() const { - return this; - } - - /// helper to return type for protocol object to unpack object - static ui16 GetType(TArrayRef<const char> data) { - return TBusHeader(data).Type; - } - - /// returns payload data - static TArrayRef<const char> GetPayload(TArrayRef<const char> data) { - return data.Slice(sizeof(TBusHeader)); - } - - private: - void DoReset(); - - /// serialize message identity to be used to construct reply message - void GetIdentity(TBusIdentity& ident) const; - - /// set message identity from serialized form - void SetIdentity(const TBusIdentity& ident); - - public: - TNetAddr GetReplyTo() const { - return ReplyTo.ToNetAddr(); - } - - /// store of application specific data, never serialized into wire - void* Data; - }; - - class TBusMessageAutoPtr: public TAutoPtr<TBusMessage> { - public: - TBusMessageAutoPtr() { - } - - TBusMessageAutoPtr(TBusMessage* message) - : TAutoPtr<TBusMessage>(message) - { - } - - template <typename T1> - TBusMessageAutoPtr(const TAutoPtr<T1>& that) - : TAutoPtr<TBusMessage>(that.Release()) - { - } - }; - -} +#define TBUSMAX_MESSAGE 26 * 1024 * 1024 + sizeof(NBus::TBusHeader) ///< is't it enough? +#define TBUSMIN_MESSAGE sizeof(NBus::TBusHeader) ///< can't be less then header + + inline bool IsVersionNegotiation(const NBus::TBusHeader& header) { + return header.Id == 0 && header.Size == sizeof(TBusHeader); + } + + ////////////////////////////////////////////////////////// + /// \brief Base class for all messages passed in the system + + enum ECreateUninitialized { + MESSAGE_CREATE_UNINITIALIZED, + }; + + class TBusMessage + : protected TBusHeader, + public TRefCounted<TBusMessage, TAtomicCounter, TDelete>, + private TNonCopyable { + friend class TLocalSession; + friend struct ::NBus::NPrivate::TBusSessionImpl; + friend class ::NBus::NPrivate::TRemoteServerSession; + friend class ::NBus::NPrivate::TRemoteClientSession; + friend class ::NBus::NPrivate::TRemoteConnection; + friend class ::NBus::NPrivate::TRemoteClientConnection; + friend class ::NBus::NPrivate::TRemoteServerConnection; + friend struct ::NBus::NPrivate::TBusMessagePtrAndHeader; + + private: + ui32 LocalFlags; + + /// connection identity for reply set by PushMessage() + NPrivate::TBusSocketAddr ReplyTo; + // server-side response only, hack + ui32 RequestSize; + + TInstant RecvTime; + + public: + /// constructor to create messages on sending end + TBusMessage(ui16 type, int approxsize = sizeof(TBusHeader)); + + /// constructor with serialzed data to examine the header + TBusMessage(ECreateUninitialized); + + // slow, for diagnostics only + virtual TString Describe() const; + + // must be called if this message object needs to be reused + void Reset(); + + void CheckClean() const; + + void SetCompressed(bool); + void SetCompressedResponse(bool); + + private: + bool IsCompressed() const { + return FlagsInternal & MESSAGE_COMPRESS_INTERNAL; + } + bool IsCompressedResponse() const { + return FlagsInternal & MESSAGE_COMPRESS_RESPONSE; + } + + public: + /// can have private data to destroy + virtual ~TBusMessage(); + + /// returns header of the message + TBusHeader* GetHeader() { + return this; + } + const TBusHeader* GetHeader() const { + return this; + } + + /// helper to return type for protocol object to unpack object + static ui16 GetType(TArrayRef<const char> data) { + return TBusHeader(data).Type; + } + + /// returns payload data + static TArrayRef<const char> GetPayload(TArrayRef<const char> data) { + return data.Slice(sizeof(TBusHeader)); + } + + private: + void DoReset(); + + /// serialize message identity to be used to construct reply message + void GetIdentity(TBusIdentity& ident) const; + + /// set message identity from serialized form + void SetIdentity(const TBusIdentity& ident); + + public: + TNetAddr GetReplyTo() const { + return ReplyTo.ToNetAddr(); + } + + /// store of application specific data, never serialized into wire + void* Data; + }; + + class TBusMessageAutoPtr: public TAutoPtr<TBusMessage> { + public: + TBusMessageAutoPtr() { + } + + TBusMessageAutoPtr(TBusMessage* message) + : TAutoPtr<TBusMessage>(message) + { + } + + template <typename T1> + TBusMessageAutoPtr(const TAutoPtr<T1>& that) + : TAutoPtr<TBusMessage>(that.Release()) + { + } + }; + +} diff --git a/library/cpp/messagebus/message_counter.cpp b/library/cpp/messagebus/message_counter.cpp index 1a945eecf5..04d9343f6a 100644 --- a/library/cpp/messagebus/message_counter.cpp +++ b/library/cpp/messagebus/message_counter.cpp @@ -11,8 +11,8 @@ TMessageCounter::TMessageCounter() , Count(0) , CountCompressed(0) , CountCompressionRequests(0) -{ -} +{ +} TMessageCounter& TMessageCounter::operator+=(const TMessageCounter& that) { BytesData += that.BytesData; diff --git a/library/cpp/messagebus/message_counter.h b/library/cpp/messagebus/message_counter.h index 8723e56c54..e4be1180b0 100644 --- a/library/cpp/messagebus/message_counter.h +++ b/library/cpp/messagebus/message_counter.h @@ -4,33 +4,33 @@ #include <cstddef> -namespace NBus { - namespace NPrivate { - struct TMessageCounter { - size_t BytesData; - size_t BytesNetwork; - size_t Count; - size_t CountCompressed; - size_t CountCompressionRequests; // reader only - - void AddMessage(size_t bytesData, size_t bytesCompressed, bool Compressed, bool compressionRequested) { - BytesData += bytesData; - BytesNetwork += bytesCompressed; - Count += 1; - if (Compressed) { - CountCompressed += 1; - } - if (compressionRequested) { - CountCompressionRequests += 1; - } - } - - TMessageCounter& operator+=(const TMessageCounter& that); - - TString ToString(bool reader) const; - - TMessageCounter(); - }; - +namespace NBus { + namespace NPrivate { + struct TMessageCounter { + size_t BytesData; + size_t BytesNetwork; + size_t Count; + size_t CountCompressed; + size_t CountCompressionRequests; // reader only + + void AddMessage(size_t bytesData, size_t bytesCompressed, bool Compressed, bool compressionRequested) { + BytesData += bytesData; + BytesNetwork += bytesCompressed; + Count += 1; + if (Compressed) { + CountCompressed += 1; + } + if (compressionRequested) { + CountCompressionRequests += 1; + } + } + + TMessageCounter& operator+=(const TMessageCounter& that); + + TString ToString(bool reader) const; + + TMessageCounter(); + }; + } -} +} diff --git a/library/cpp/messagebus/message_ptr_and_header.h b/library/cpp/messagebus/message_ptr_and_header.h index 0974e8244e..9b4e2fd270 100644 --- a/library/cpp/messagebus/message_ptr_and_header.h +++ b/library/cpp/messagebus/message_ptr_and_header.h @@ -6,31 +6,31 @@ #include <util/generic/noncopyable.h> #include <util/generic/utility.h> -namespace NBus { - namespace NPrivate { - struct TBusMessagePtrAndHeader : TNonCopyable { - TNonDestroyingHolder<TBusMessage> MessagePtr; - TBusHeader Header; - ui32 LocalFlags; +namespace NBus { + namespace NPrivate { + struct TBusMessagePtrAndHeader : TNonCopyable { + TNonDestroyingHolder<TBusMessage> MessagePtr; + TBusHeader Header; + ui32 LocalFlags; - TBusMessagePtrAndHeader() - : LocalFlags() - { - } + TBusMessagePtrAndHeader() + : LocalFlags() + { + } explicit TBusMessagePtrAndHeader(TBusMessage* messagePtr) : MessagePtr(messagePtr) - , Header(*MessagePtr->GetHeader()) - , LocalFlags(MessagePtr->LocalFlags) - { - } + , Header(*MessagePtr->GetHeader()) + , LocalFlags(MessagePtr->LocalFlags) + { + } - void Swap(TBusMessagePtrAndHeader& that) { - DoSwap(MessagePtr, that.MessagePtr); - DoSwap(Header, that.Header); - DoSwap(LocalFlags, that.LocalFlags); - } - }; + void Swap(TBusMessagePtrAndHeader& that) { + DoSwap(MessagePtr, that.MessagePtr); + DoSwap(Header, that.Header); + DoSwap(LocalFlags, that.LocalFlags); + } + }; - } -} + } +} diff --git a/library/cpp/messagebus/message_status.cpp b/library/cpp/messagebus/message_status.cpp index a18d82f305..41ad62b73f 100644 --- a/library/cpp/messagebus/message_status.cpp +++ b/library/cpp/messagebus/message_status.cpp @@ -3,9 +3,9 @@ using namespace NBus; const char* NBus::MessageStatusDescription(EMessageStatus messageStatus) { -#define MESSAGE_STATUS_DESCRIPTION_GEN(name, description, ...) \ - if (messageStatus == name) \ - return description; +#define MESSAGE_STATUS_DESCRIPTION_GEN(name, description, ...) \ + if (messageStatus == name) \ + return description; MESSAGE_STATUS_MAP(MESSAGE_STATUS_DESCRIPTION_GEN) diff --git a/library/cpp/messagebus/message_status.h b/library/cpp/messagebus/message_status.h index f449004e96..e1878960b3 100644 --- a/library/cpp/messagebus/message_status.h +++ b/library/cpp/messagebus/message_status.h @@ -9,49 +9,49 @@ namespace NBus { //////////////////////////////////////////////////////////////// /// \brief Status of message communication -#define MESSAGE_STATUS_MAP(XX) \ - XX(MESSAGE_OK, "OK") \ - XX(MESSAGE_CONNECT_FAILED, "Connect failed") \ - XX(MESSAGE_TIMEOUT, "Message timed out") \ - XX(MESSAGE_SERVICE_UNKNOWN, "Locator hasn't found address for key") \ - XX(MESSAGE_BUSY, "Too many messages in flight") \ - XX(MESSAGE_UNKNOWN, "Request not found by id, usually it means that message is timed out") \ - XX(MESSAGE_DESERIALIZE_ERROR, "Deserialize by TBusProtocol failed") \ - XX(MESSAGE_HEADER_CORRUPTED, "Header corrupted") \ - XX(MESSAGE_DECOMPRESS_ERROR, "Failed to decompress") \ - XX(MESSAGE_MESSAGE_TOO_LARGE, "Message too large") \ - XX(MESSAGE_REPLY_FAILED, "Unused by messagebus, used by other code") \ - XX(MESSAGE_DELIVERY_FAILED, "Message delivery failed because connection is closed") \ - XX(MESSAGE_INVALID_VERSION, "Protocol error: invalid version") \ - XX(MESSAGE_SERVICE_TOOMANY, "Locator failed to resolve address") \ - XX(MESSAGE_SHUTDOWN, "Failure because of either session or connection shutdown") \ - XX(MESSAGE_DONT_ASK, "Internal error code used by modules") - - enum EMessageStatus { - MESSAGE_STATUS_MAP(ENUM_VALUE_GEN_NO_VALUE) - MESSAGE_STATUS_COUNT - }; - - ENUM_TO_STRING(EMessageStatus, MESSAGE_STATUS_MAP) - - const char* MessageStatusDescription(EMessageStatus); - - static inline const char* GetMessageStatus(EMessageStatus status) { - return ToCString(status); - } - - // For lwtrace - struct TMessageStatusField { - typedef int TStoreType; +#define MESSAGE_STATUS_MAP(XX) \ + XX(MESSAGE_OK, "OK") \ + XX(MESSAGE_CONNECT_FAILED, "Connect failed") \ + XX(MESSAGE_TIMEOUT, "Message timed out") \ + XX(MESSAGE_SERVICE_UNKNOWN, "Locator hasn't found address for key") \ + XX(MESSAGE_BUSY, "Too many messages in flight") \ + XX(MESSAGE_UNKNOWN, "Request not found by id, usually it means that message is timed out") \ + XX(MESSAGE_DESERIALIZE_ERROR, "Deserialize by TBusProtocol failed") \ + XX(MESSAGE_HEADER_CORRUPTED, "Header corrupted") \ + XX(MESSAGE_DECOMPRESS_ERROR, "Failed to decompress") \ + XX(MESSAGE_MESSAGE_TOO_LARGE, "Message too large") \ + XX(MESSAGE_REPLY_FAILED, "Unused by messagebus, used by other code") \ + XX(MESSAGE_DELIVERY_FAILED, "Message delivery failed because connection is closed") \ + XX(MESSAGE_INVALID_VERSION, "Protocol error: invalid version") \ + XX(MESSAGE_SERVICE_TOOMANY, "Locator failed to resolve address") \ + XX(MESSAGE_SHUTDOWN, "Failure because of either session or connection shutdown") \ + XX(MESSAGE_DONT_ASK, "Internal error code used by modules") + + enum EMessageStatus { + MESSAGE_STATUS_MAP(ENUM_VALUE_GEN_NO_VALUE) + MESSAGE_STATUS_COUNT + }; + + ENUM_TO_STRING(EMessageStatus, MESSAGE_STATUS_MAP) + + const char* MessageStatusDescription(EMessageStatus); + + static inline const char* GetMessageStatus(EMessageStatus status) { + return ToCString(status); + } + + // For lwtrace + struct TMessageStatusField { + typedef int TStoreType; typedef int TFuncParam; - static void ToString(int value, TString* out) { - *out = GetMessageStatus((NBus::EMessageStatus)value); - } + static void ToString(int value, TString* out) { + *out = GetMessageStatus((NBus::EMessageStatus)value); + } static int ToStoreType(int value) { return value; } - }; + }; } // ns diff --git a/library/cpp/messagebus/message_status_counter.cpp b/library/cpp/messagebus/message_status_counter.cpp index c8222a475b..891c8f5bb2 100644 --- a/library/cpp/messagebus/message_status_counter.cpp +++ b/library/cpp/messagebus/message_status_counter.cpp @@ -10,7 +10,7 @@ using namespace NBus; using namespace NBus::NPrivate; -TMessageStatusCounter::TMessageStatusCounter() { +TMessageStatusCounter::TMessageStatusCounter() { Zero(Counts); } diff --git a/library/cpp/messagebus/message_status_counter.h b/library/cpp/messagebus/message_status_counter.h index f54e79919b..e8ba2fdd31 100644 --- a/library/cpp/messagebus/message_status_counter.h +++ b/library/cpp/messagebus/message_status_counter.h @@ -8,29 +8,29 @@ #include <array> -namespace NBus { - namespace NPrivate { - struct TMessageStatusCounter { - static TMessageStatusRecord::EMessageStatus MessageStatusToProtobuf(EMessageStatus status) { - return (TMessageStatusRecord::EMessageStatus)status; - } +namespace NBus { + namespace NPrivate { + struct TMessageStatusCounter { + static TMessageStatusRecord::EMessageStatus MessageStatusToProtobuf(EMessageStatus status) { + return (TMessageStatusRecord::EMessageStatus)status; + } - std::array<unsigned, MESSAGE_STATUS_COUNT> Counts; + std::array<unsigned, MESSAGE_STATUS_COUNT> Counts; - unsigned& operator[](EMessageStatus index) { - return Counts[index]; - } - const unsigned& operator[](EMessageStatus index) const { - return Counts[index]; - } + unsigned& operator[](EMessageStatus index) { + return Counts[index]; + } + const unsigned& operator[](EMessageStatus index) const { + return Counts[index]; + } - TMessageStatusCounter(); + TMessageStatusCounter(); - TMessageStatusCounter& operator+=(const TMessageStatusCounter&); + TMessageStatusCounter& operator+=(const TMessageStatusCounter&); - TString PrintToString() const; - void FillErrorsProtobuf(TConnectionStatusMonRecord*) const; - }; + TString PrintToString() const; + void FillErrorsProtobuf(TConnectionStatusMonRecord*) const; + }; - } -} + } +} diff --git a/library/cpp/messagebus/messqueue.cpp b/library/cpp/messagebus/messqueue.cpp index e7ee811fd4..3474d62705 100644 --- a/library/cpp/messagebus/messqueue.cpp +++ b/library/cpp/messagebus/messqueue.cpp @@ -59,7 +59,7 @@ TBusMessageQueue::~TBusMessageQueue() { Stop(); } -void TBusMessageQueue::Stop() { +void TBusMessageQueue::Stop() { if (!AtomicCas(&Running, 0, 1)) { ShutdownComplete.WaitI(); return; @@ -98,14 +98,14 @@ TString TBusMessageQueue::GetStatus(ui16 flags) const { ss << GetStatusSelf(); - TList<TIntrusivePtr<TBusSessionImpl>> sessions; + TList<TIntrusivePtr<TBusSessionImpl>> sessions; { TGuard<TMutex> scope(Lock); sessions = Sessions; } - for (TList<TIntrusivePtr<TBusSessionImpl>>::const_iterator session = sessions.begin(); - session != sessions.end(); ++session) { + for (TList<TIntrusivePtr<TBusSessionImpl>>::const_iterator session = sessions.begin(); + session != sessions.end(); ++session) { ss << Endl; ss << (*session)->GetStatus(flags); } @@ -124,13 +124,13 @@ TString TBusMessageQueue::GetStatus(ui16 flags) const { return ss.Str(); } -TBusClientSessionPtr TBusMessageQueue::CreateSource(TBusProtocol* proto, IBusClientHandler* handler, const TBusClientSessionConfig& config, const TString& name) { +TBusClientSessionPtr TBusMessageQueue::CreateSource(TBusProtocol* proto, IBusClientHandler* handler, const TBusClientSessionConfig& config, const TString& name) { TRemoteClientSessionPtr session(new TRemoteClientSession(this, proto, handler, config, name)); Add(session.Get()); return session.Get(); } -TBusServerSessionPtr TBusMessageQueue::CreateDestination(TBusProtocol* proto, IBusServerHandler* handler, const TBusClientSessionConfig& config, const TString& name) { +TBusServerSessionPtr TBusMessageQueue::CreateDestination(TBusProtocol* proto, IBusServerHandler* handler, const TBusClientSessionConfig& config, const TString& name) { TRemoteServerSessionPtr session(new TRemoteServerSession(this, proto, handler, config, name)); try { int port = config.ListenPort; @@ -150,7 +150,7 @@ TBusServerSessionPtr TBusMessageQueue::CreateDestination(TBusProtocol* proto, IB } } -TBusServerSessionPtr TBusMessageQueue::CreateDestination(TBusProtocol* proto, IBusServerHandler* handler, const TBusServerSessionConfig& config, const TVector<TBindResult>& bindTo, const TString& name) { +TBusServerSessionPtr TBusMessageQueue::CreateDestination(TBusProtocol* proto, IBusServerHandler* handler, const TBusServerSessionConfig& config, const TVector<TBindResult>& bindTo, const TString& name) { TRemoteServerSessionPtr session(new TRemoteServerSession(this, proto, handler, config, name)); try { session->Listen(bindTo, this); @@ -162,23 +162,23 @@ TBusServerSessionPtr TBusMessageQueue::CreateDestination(TBusProtocol* proto, IB } void TBusMessageQueue::Add(TIntrusivePtr<TBusSessionImpl> session) { - TGuard<TMutex> scope(Lock); - Sessions.push_back(session); + TGuard<TMutex> scope(Lock); + Sessions.push_back(session); } void TBusMessageQueue::Remove(TBusSession* session) { TGuard<TMutex> scope(Lock); - TList<TIntrusivePtr<TBusSessionImpl>>::iterator it = std::find(Sessions.begin(), Sessions.end(), session); + TList<TIntrusivePtr<TBusSessionImpl>>::iterator it = std::find(Sessions.begin(), Sessions.end(), session); Y_VERIFY(it != Sessions.end(), "do not destroy session twice"); Sessions.erase(it); } -void TBusMessageQueue::Destroy(TBusSession* session) { +void TBusMessageQueue::Destroy(TBusSession* session) { session->Shutdown(); } void TBusMessageQueue::DestroyAllSessions() { - TList<TIntrusivePtr<TBusSessionImpl>> sessions; + TList<TIntrusivePtr<TBusSessionImpl>> sessions; { TGuard<TMutex> scope(Lock); sessions = Sessions; diff --git a/library/cpp/messagebus/misc/atomic_box.h b/library/cpp/messagebus/misc/atomic_box.h index c36b6c747f..401621f933 100644 --- a/library/cpp/messagebus/misc/atomic_box.h +++ b/library/cpp/messagebus/misc/atomic_box.h @@ -12,23 +12,23 @@ private: T ValueForDebugger; }; - static_assert(sizeof(T) <= sizeof(TAtomic), "expect sizeof(T) <= sizeof(TAtomic)"); - + static_assert(sizeof(T) <= sizeof(TAtomic), "expect sizeof(T) <= sizeof(TAtomic)"); + public: - TAtomicBox(T value = T()) - : Value(value) - { - } + TAtomicBox(T value = T()) + : Value(value) + { + } void Set(T value) { - AtomicSet(Value, (TAtomic)value); + AtomicSet(Value, (TAtomic)value); } T Get() const { - return (T)AtomicGet(Value); + return (T)AtomicGet(Value); } bool CompareAndSet(T expected, T set) { - return AtomicCas(&Value, (TAtomicBase)set, (TAtomicBase)expected); + return AtomicCas(&Value, (TAtomicBase)set, (TAtomicBase)expected); } }; diff --git a/library/cpp/messagebus/misc/granup.h b/library/cpp/messagebus/misc/granup.h index ff6cf522b4..36ecfebc93 100644 --- a/library/cpp/messagebus/misc/granup.h +++ b/library/cpp/messagebus/misc/granup.h @@ -12,29 +12,29 @@ namespace NBus { TGranUp(TDuration gran) : Gran(gran) , Next(TInstant::MicroSeconds(0)) - { - } + { + } - template <typename TFunctor> - void Update(TFunctor functor, TInstant now, bool force = false) { - if (force || now > Next) + template <typename TFunctor> + void Update(TFunctor functor, TInstant now, bool force = false) { + if (force || now > Next) Set(functor(), now); } - void Update(const TItem& item, TInstant now, bool force = false) { - if (force || now > Next) + void Update(const TItem& item, TInstant now, bool force = false) { + if (force || now > Next) Set(item, now); } - TItem Get() const noexcept { - TGuard<TLocker> guard(Lock); + TItem Get() const noexcept { + TGuard<TLocker> guard(Lock); return Item; } protected: - void Set(const TItem& item, TInstant now) { - TGuard<TLocker> guard(Lock); + void Set(const TItem& item, TInstant now) { + TGuard<TLocker> guard(Lock); Item = item; @@ -43,8 +43,8 @@ namespace NBus { private: const TDuration Gran; - TLocker Lock; - TItem Item; - TInstant Next; + TLocker Lock; + TItem Item; + TInstant Next; }; } diff --git a/library/cpp/messagebus/misc/test_sync.h b/library/cpp/messagebus/misc/test_sync.h index 9a6a718f7b..be3f4f20b8 100644 --- a/library/cpp/messagebus/misc/test_sync.h +++ b/library/cpp/messagebus/misc/test_sync.h @@ -9,12 +9,12 @@ private: TMutex Mutex; TCondVar CondVar; - + public: TTestSync() : Current(0) - { - } + { + } void Inc() { TGuard<TMutex> guard(Mutex); diff --git a/library/cpp/messagebus/misc/tokenquota.h b/library/cpp/messagebus/misc/tokenquota.h index d5bdd79625..190547fa54 100644 --- a/library/cpp/messagebus/misc/tokenquota.h +++ b/library/cpp/messagebus/misc/tokenquota.h @@ -25,26 +25,26 @@ namespace NBus { Y_UNUSED(padd_); } - bool Acquire(TAtomic level = 1, bool force = false) { - level = Max(TAtomicBase(level), TAtomicBase(1)); + bool Acquire(TAtomic level = 1, bool force = false) { + level = Max(TAtomicBase(level), TAtomicBase(1)); - if (Enabled && (Acquired < level || force)) { + if (Enabled && (Acquired < level || force)) { Acquired += AtomicSwap(&Tokens_, 0); } return !Enabled || Acquired >= level; } - void Consume(size_t items) { - if (Enabled) { + void Consume(size_t items) { + if (Enabled) { Y_ASSERT(Acquired >= TAtomicBase(items)); Acquired -= items; } } - bool Return(size_t items_) noexcept { - if (!Enabled || items_ == 0) + bool Return(size_t items_) noexcept { + if (!Enabled || items_ == 0) return false; const TAtomic items = items_; @@ -53,31 +53,31 @@ namespace NBus { return (value - items < WakeLev && value >= WakeLev); } - bool IsEnabled() const noexcept { + bool IsEnabled() const noexcept { return Enabled; } - bool IsAboveWake() const noexcept { + bool IsAboveWake() const noexcept { return !Enabled || (WakeLev <= AtomicGet(Tokens_)); } - size_t Tokens() const noexcept { + size_t Tokens() const noexcept { return Acquired + AtomicGet(Tokens_); } - size_t Check(const TAtomic level) const noexcept { + size_t Check(const TAtomic level) const noexcept { return !Enabled || level <= Acquired; } private: - bool Enabled; - TAtomicBase Acquired; + bool Enabled; + TAtomicBase Acquired; const TAtomicBase WakeLev; - TAtomic Tokens_; + TAtomic Tokens_; /* This padd requires for align Tokens_ member on its own CPU cacheline. */ - ui64 padd_; + ui64 padd_; }; } diff --git a/library/cpp/messagebus/misc/weak_ptr.h b/library/cpp/messagebus/misc/weak_ptr.h index ac20cd7d43..70fdeb0e2a 100644 --- a/library/cpp/messagebus/misc/weak_ptr.h +++ b/library/cpp/messagebus/misc/weak_ptr.h @@ -8,18 +8,18 @@ struct TWeakPtr; template <typename TSelf> struct TWeakRefCounted { - template <typename> - friend struct TWeakPtr; - + template <typename> + friend struct TWeakPtr; + private: - struct TRef: public TAtomicRefCount<TRef> { + struct TRef: public TAtomicRefCount<TRef> { TMutex Mutex; TSelf* Outer; - TRef(TSelf* outer) - : Outer(outer) - { - } + TRef(TSelf* outer) + : Outer(outer) + { + } void Release() { TGuard<TMutex> g(Mutex); @@ -40,8 +40,8 @@ private: public: TWeakRefCounted() : RefPtr(new TRef(static_cast<TSelf*>(this))) - { - } + { + } void Ref() { Counter.Inc(); @@ -72,10 +72,10 @@ struct TWeakPtr { private: typedef TIntrusivePtr<typename T::TRef> TRefPtr; TRefPtr RefPtr; - + public: - TWeakPtr() { - } + TWeakPtr() { + } TWeakPtr(T* t) { if (!!t) { diff --git a/library/cpp/messagebus/misc/weak_ptr_ut.cpp b/library/cpp/messagebus/misc/weak_ptr_ut.cpp index 9677ae21b5..5a325278db 100644 --- a/library/cpp/messagebus/misc/weak_ptr_ut.cpp +++ b/library/cpp/messagebus/misc/weak_ptr_ut.cpp @@ -6,13 +6,13 @@ Y_UNIT_TEST_SUITE(TWeakPtrTest) { struct TWeakPtrTester: public TWeakRefCounted<TWeakPtrTester> { int* const CounterPtr; - TWeakPtrTester(int* counterPtr) - : CounterPtr(counterPtr) - { - } - ~TWeakPtrTester() { - ++*CounterPtr; - } + TWeakPtrTester(int* counterPtr) + : CounterPtr(counterPtr) + { + } + ~TWeakPtrTester() { + ++*CounterPtr; + } }; Y_UNIT_TEST(Simple) { diff --git a/library/cpp/messagebus/moved.h b/library/cpp/messagebus/moved.h index 4490de10c3..ede8dcd244 100644 --- a/library/cpp/messagebus/moved.h +++ b/library/cpp/messagebus/moved.h @@ -6,10 +6,10 @@ template <typename T> class TMoved { private: mutable T Value; - + public: - TMoved() { - } + TMoved() { + } TMoved(const TMoved<T>& that) { DoSwap(Value, that.Value); } diff --git a/library/cpp/messagebus/moved_ut.cpp b/library/cpp/messagebus/moved_ut.cpp index 32e726fb19..c1a07cce7e 100644 --- a/library/cpp/messagebus/moved_ut.cpp +++ b/library/cpp/messagebus/moved_ut.cpp @@ -5,13 +5,13 @@ Y_UNIT_TEST_SUITE(TMovedTest) { Y_UNIT_TEST(Simple) { TMoved<THolder<int>> h1(MakeHolder<int>(10)); - TMoved<THolder<int>> h2 = h1; + TMoved<THolder<int>> h2 = h1; UNIT_ASSERT(!*h1); UNIT_ASSERT(!!*h2); UNIT_ASSERT_VALUES_EQUAL(10, **h2); } - void Foo(TMoved<THolder<int>> h) { + void Foo(TMoved<THolder<int>> h) { UNIT_ASSERT_VALUES_EQUAL(11, **h); } diff --git a/library/cpp/messagebus/network.cpp b/library/cpp/messagebus/network.cpp index a4978f5bc4..304bedae5a 100644 --- a/library/cpp/messagebus/network.cpp +++ b/library/cpp/messagebus/network.cpp @@ -41,11 +41,11 @@ namespace { socklen_t len; if (af == AF_INET) { len = sizeof(sockaddr_in); - ((sockaddr_in*)sa)->sin_port = HostToInet((ui16)port); - ((sockaddr_in*)sa)->sin_addr.s_addr = INADDR_ANY; + ((sockaddr_in*)sa)->sin_port = HostToInet((ui16)port); + ((sockaddr_in*)sa)->sin_addr.s_addr = INADDR_ANY; } else { len = sizeof(sockaddr_in6); - ((sockaddr_in6*)sa)->sin6_port = HostToInet((ui16)port); + ((sockaddr_in6*)sa)->sin6_port = HostToInet((ui16)port); } if (af == AF_INET6) { @@ -75,7 +75,7 @@ namespace { TMaybe<TBindResult> TryBindOnPortProto(int port, int af, bool reusePort) { try { - return {BindOnPortProto(port, af, reusePort)}; + return {BindOnPortProto(port, af, reusePort)}; } catch (const TSystemError&) { return {}; } @@ -99,7 +99,7 @@ std::pair<unsigned, TVector<TBindResult>> NBus::BindOnPort(int port, bool reuseP if (port != 0) { return AggregateBindResults(BindOnPortProto(port, AF_INET, reusePort), - BindOnPortProto(port, AF_INET6, reusePort)); + BindOnPortProto(port, AF_INET6, reusePort)); } // use nothrow versions in cycle @@ -124,7 +124,7 @@ std::pair<unsigned, TVector<TBindResult>> NBus::BindOnPort(int port, bool reuseP void NBus::NPrivate::SetSockOptTcpCork(SOCKET s, bool value) { #ifdef _linux_ - CheckedSetSockOpt(s, IPPROTO_TCP, TCP_CORK, (int)value, "TCP_CORK"); + CheckedSetSockOpt(s, IPPROTO_TCP, TCP_CORK, (int)value, "TCP_CORK"); #else Y_UNUSED(s); Y_UNUSED(value); diff --git a/library/cpp/messagebus/network.h b/library/cpp/messagebus/network.h index 0a2be05dc2..cc4bd76ea3 100644 --- a/library/cpp/messagebus/network.h +++ b/library/cpp/messagebus/network.h @@ -9,20 +9,20 @@ #include <utility> namespace NBus { - namespace NPrivate { - void SetSockOptTcpCork(SOCKET s, bool value); + namespace NPrivate { + void SetSockOptTcpCork(SOCKET s, bool value); [[nodiscard]] ssize_t SocketSend(SOCKET s, TArrayRef<const char> data); [[nodiscard]] ssize_t SocketRecv(SOCKET s, TArrayRef<char> buffer); - } + } - struct TBindResult { - TSimpleSharedPtr<TSocketHolder> Socket; - TNetAddr Addr; - }; + struct TBindResult { + TSimpleSharedPtr<TSocketHolder> Socket; + TNetAddr Addr; + }; - std::pair<unsigned, TVector<TBindResult>> BindOnPort(int port, bool reusePort); + std::pair<unsigned, TVector<TBindResult>> BindOnPort(int port, bool reusePort); -} +} diff --git a/library/cpp/messagebus/network_ut.cpp b/library/cpp/messagebus/network_ut.cpp index 47edd1b59c..f1798419db 100644 --- a/library/cpp/messagebus/network_ut.cpp +++ b/library/cpp/messagebus/network_ut.cpp @@ -15,14 +15,14 @@ namespace { socklen_t len = sizeof(addr); - int r = ::getsockname(socket, (sockaddr*)&addr, &len); + int r = ::getsockname(socket, (sockaddr*)&addr, &len); UNIT_ASSERT(r >= 0); if (addr.ss_family == AF_INET) { - sockaddr_in* addr_in = (sockaddr_in*)&addr; + sockaddr_in* addr_in = (sockaddr_in*)&addr; return InetToHost(addr_in->sin_port); } else if (addr.ss_family == AF_INET6) { - sockaddr_in6* addr_in6 = (sockaddr_in6*)&addr; + sockaddr_in6* addr_in6 = (sockaddr_in6*)&addr; return InetToHost(addr_in6->sin6_port); } else { UNIT_FAIL("unknown AF"); diff --git a/library/cpp/messagebus/oldmodule/module.cpp b/library/cpp/messagebus/oldmodule/module.cpp index f423214aa3..24bd778799 100644 --- a/library/cpp/messagebus/oldmodule/module.cpp +++ b/library/cpp/messagebus/oldmodule/module.cpp @@ -16,680 +16,680 @@ using namespace NBus; using namespace NBus::NPrivate; namespace { - Y_POD_STATIC_THREAD(TBusJob*) - ThreadCurrentJob; - - struct TThreadCurrentJobGuard { - TBusJob* Prev; - - TThreadCurrentJobGuard(TBusJob* job) - : Prev(ThreadCurrentJob) - { - Y_ASSERT(!ThreadCurrentJob || ThreadCurrentJob == job); - ThreadCurrentJob = job; - } - ~TThreadCurrentJobGuard() { - ThreadCurrentJob = Prev; - } - }; - - void ClearState(NBus::TJobState* state) { - /// skip sendbacks handlers - if (state->Message != state->Reply) { - if (state->Message) { - delete state->Message; - state->Message = nullptr; - } - - if (state->Reply) { - delete state->Reply; - state->Reply = nullptr; - } - } - } - - void ClearJobStateVector(NBus::TJobStateVec* vec) { - Y_ASSERT(vec); - - for (auto& call : *vec) { - ClearState(&call); + Y_POD_STATIC_THREAD(TBusJob*) + ThreadCurrentJob; + + struct TThreadCurrentJobGuard { + TBusJob* Prev; + + TThreadCurrentJobGuard(TBusJob* job) + : Prev(ThreadCurrentJob) + { + Y_ASSERT(!ThreadCurrentJob || ThreadCurrentJob == job); + ThreadCurrentJob = job; + } + ~TThreadCurrentJobGuard() { + ThreadCurrentJob = Prev; + } + }; + + void ClearState(NBus::TJobState* state) { + /// skip sendbacks handlers + if (state->Message != state->Reply) { + if (state->Message) { + delete state->Message; + state->Message = nullptr; + } + + if (state->Reply) { + delete state->Reply; + state->Reply = nullptr; + } + } + } + + void ClearJobStateVector(NBus::TJobStateVec* vec) { + Y_ASSERT(vec); + + for (auto& call : *vec) { + ClearState(&call); } - vec->clear(); + vec->clear(); } } namespace NBus { - namespace NPrivate { - class TJobStorage { - }; - - struct TModuleClientHandler - : public IBusClientHandler { - TModuleClientHandler(TBusModuleImpl* module) - : Module(module) - { - } - - void OnReply(TAutoPtr<TBusMessage> req, TAutoPtr<TBusMessage> reply) override; - void OnMessageSentOneWay(TAutoPtr<TBusMessage> pMessage) override; - void OnError(TAutoPtr<TBusMessage> msg, EMessageStatus status) override; - void OnClientConnectionEvent(const TClientConnectionEvent& event) override; - - TBusModuleImpl* const Module; - }; - - struct TModuleServerHandler - : public IBusServerHandler { - TModuleServerHandler(TBusModuleImpl* module) - : Module(module) - { - } - - void OnMessage(TOnMessageContext& msg) override; - - TBusModuleImpl* const Module; - }; - - struct TBusModuleImpl: public TBusModuleInternal { - TBusModule* const Module; - - TBusMessageQueue* Queue; - - TScheduler Scheduler; - - const char* const Name; - - typedef TList<TJobRunner*> TBusJobList; - /// jobs currently in-flight on this module - TBusJobList Jobs; - /// module level mutex - TMutex Lock; - TCondVar ShutdownCondVar; - TAtomic JobCount; - - enum EState { - CREATED, - RUNNING, - STOPPED, - }; - - TAtomic State; - TBusModuleConfig ModuleConfig; - TBusServerSessionPtr ExternalSession; - /// protocol for local proxy session - THolder<IBusClientHandler> ModuleClientHandler; - THolder<IBusServerHandler> ModuleServerHandler; - TVector<TSimpleSharedPtr<TBusStarter>> Starters; - - // Sessions must be destroyed before - // ModuleClientHandler / ModuleServerHandler - TVector<TBusClientSessionPtr> ClientSessions; - TVector<TBusServerSessionPtr> ServerSessions; - - TBusModuleImpl(TBusModule* module, const char* name) - : Module(module) - , Queue() - , Name(name) - , JobCount(0) - , State(CREATED) - , ExternalSession(nullptr) - , ModuleClientHandler(new TModuleClientHandler(this)) - , ModuleServerHandler(new TModuleServerHandler(this)) - { - } - - ~TBusModuleImpl() override { - // Shutdown cannot be called from destructor, - // because module has virtual methods. - Y_VERIFY(State != RUNNING, "if running, must explicitly call Shutdown() before destructor"); - - Scheduler.Stop(); - - while (!Jobs.empty()) { - DestroyJob(Jobs.front()); - } - Y_VERIFY(JobCount == 0, "state check"); - } - - void OnMessageReceived(TAutoPtr<TBusMessage> msg, TOnMessageContext&); - - void AddJob(TJobRunner* jobRunner); - - void DestroyJob(TJobRunner* job); - - /// terminate job on this message - void CancelJob(TBusJob* job, EMessageStatus status); - /// prints statuses of jobs - TString GetStatus(unsigned flags); - - size_t Size() const { - return AtomicGet(JobCount); - } - - void Shutdown(); - - TVector<TBusClientSessionPtr> GetClientSessionsInternal() override { - return ClientSessions; - } - - TVector<TBusServerSessionPtr> GetServerSessionsInternal() override { - return ServerSessions; - } - - TBusMessageQueue* GetQueue() override { - return Queue; - } - - TString GetNameInternal() override { - return Name; - } - - TString GetStatusSingleLine() override { - TStringStream ss; - ss << "jobs: " << Size(); - return ss.Str(); - } - - void OnClientConnectionEvent(const TClientConnectionEvent& event) { - Module->OnClientConnectionEvent(event); - } - }; - - struct TJobResponseMessage { - TBusMessage* Request; - TBusMessage* Response; - EMessageStatus Status; - - TJobResponseMessage(TBusMessage* request, TBusMessage* response, EMessageStatus status) - : Request(request) - , Response(response) - , Status(status) - { - } - }; - - struct TJobRunner: public TAtomicRefCount<TJobRunner>, - public NActor::TActor<TJobRunner>, - public NActor::TQueueInActor<TJobRunner, TJobResponseMessage>, - public TScheduleActor<TJobRunner> { - THolder<TBusJob> Job; - - TList<TJobRunner*>::iterator JobStorageIterator; - - TJobRunner(TAutoPtr<TBusJob> job) - : NActor::TActor<TJobRunner>(job->ModuleImpl->Queue->GetExecutor()) - , TScheduleActor<TJobRunner>(&job->ModuleImpl->Scheduler) - , Job(job.Release()) - , JobStorageIterator() - { - Job->Runner = this; - } - - ~TJobRunner() override { - Y_ASSERT(JobStorageIterator == TList<TJobRunner*>::iterator()); - } - - void ProcessItem(NActor::TDefaultTag, NActor::TDefaultTag, const TJobResponseMessage& message) { - Job->CallReplyHandler(message.Status, message.Request, message.Response); - } - - void Destroy() { - if (!!Job->OnMessageContext) { - if (!Job->ReplySent) { - Job->OnMessageContext.ForgetRequest(); - } - } - Job->ModuleImpl->DestroyJob(this); + namespace NPrivate { + class TJobStorage { + }; + + struct TModuleClientHandler + : public IBusClientHandler { + TModuleClientHandler(TBusModuleImpl* module) + : Module(module) + { } - void Act(NActor::TDefaultTag) { - if (JobStorageIterator == TList<TJobRunner*>::iterator()) { - return; - } - - if (Job->SleepUntil != 0) { - if (AtomicGet(Job->ModuleImpl->State) == TBusModuleImpl::STOPPED) { - Destroy(); - return; - } - } - - TThreadCurrentJobGuard g(Job.Get()); - - NActor::TQueueInActor<TJobRunner, TJobResponseMessage>::DequeueAll(); - - if (Alarm.FetchTask()) { - if (Job->AnyPendingToSend()) { - Y_ASSERT(Job->SleepUntil == 0); - Job->SendPending(); - if (Job->AnyPendingToSend()) { - } - } else { - // regular alarm - Y_ASSERT(Job->Pending.empty()); - Y_ASSERT(Job->SleepUntil != 0); - Job->SleepUntil = 0; - } + void OnReply(TAutoPtr<TBusMessage> req, TAutoPtr<TBusMessage> reply) override; + void OnMessageSentOneWay(TAutoPtr<TBusMessage> pMessage) override; + void OnError(TAutoPtr<TBusMessage> msg, EMessageStatus status) override; + void OnClientConnectionEvent(const TClientConnectionEvent& event) override; + + TBusModuleImpl* const Module; + }; + + struct TModuleServerHandler + : public IBusServerHandler { + TModuleServerHandler(TBusModuleImpl* module) + : Module(module) + { + } + + void OnMessage(TOnMessageContext& msg) override; + + TBusModuleImpl* const Module; + }; + + struct TBusModuleImpl: public TBusModuleInternal { + TBusModule* const Module; + + TBusMessageQueue* Queue; + + TScheduler Scheduler; + + const char* const Name; + + typedef TList<TJobRunner*> TBusJobList; + /// jobs currently in-flight on this module + TBusJobList Jobs; + /// module level mutex + TMutex Lock; + TCondVar ShutdownCondVar; + TAtomic JobCount; + + enum EState { + CREATED, + RUNNING, + STOPPED, + }; + + TAtomic State; + TBusModuleConfig ModuleConfig; + TBusServerSessionPtr ExternalSession; + /// protocol for local proxy session + THolder<IBusClientHandler> ModuleClientHandler; + THolder<IBusServerHandler> ModuleServerHandler; + TVector<TSimpleSharedPtr<TBusStarter>> Starters; + + // Sessions must be destroyed before + // ModuleClientHandler / ModuleServerHandler + TVector<TBusClientSessionPtr> ClientSessions; + TVector<TBusServerSessionPtr> ServerSessions; + + TBusModuleImpl(TBusModule* module, const char* name) + : Module(module) + , Queue() + , Name(name) + , JobCount(0) + , State(CREATED) + , ExternalSession(nullptr) + , ModuleClientHandler(new TModuleClientHandler(this)) + , ModuleServerHandler(new TModuleServerHandler(this)) + { + } + + ~TBusModuleImpl() override { + // Shutdown cannot be called from destructor, + // because module has virtual methods. + Y_VERIFY(State != RUNNING, "if running, must explicitly call Shutdown() before destructor"); + + Scheduler.Stop(); + + while (!Jobs.empty()) { + DestroyJob(Jobs.front()); } + Y_VERIFY(JobCount == 0, "state check"); + } - for (;;) { - if (Job->Pending.empty() && !!Job->Handler && Job->Status == MESSAGE_OK) { - TWhatThreadDoesPushPop pp("do call job handler (do not confuse with reply handler)"); + void OnMessageReceived(TAutoPtr<TBusMessage> msg, TOnMessageContext&); - Job->Handler = Job->Handler(Job->Module, Job.Get(), Job->Message); - } + void AddJob(TJobRunner* jobRunner); - if (Job->SleepUntil != 0) { - ScheduleAt(TInstant::MilliSeconds(Job->SleepUntil)); - return; - } + void DestroyJob(TJobRunner* job); - Job->SendPending(); + /// terminate job on this message + void CancelJob(TBusJob* job, EMessageStatus status); + /// prints statuses of jobs + TString GetStatus(unsigned flags); - if (Job->AnyPendingToSend()) { - ScheduleAt(TInstant::Now() + TDuration::Seconds(1)); - return; - } + size_t Size() const { + return AtomicGet(JobCount); + } - if (!Job->Pending.empty()) { - // waiting replies - return; - } + void Shutdown(); - if (Job->IsDone()) { - Destroy(); - return; - } - } + TVector<TBusClientSessionPtr> GetClientSessionsInternal() override { + return ClientSessions; } - }; - - } - - static inline TJobRunner* GetJob(TBusMessage* message) { - return (TJobRunner*)message->Data; - } - - static inline void SetJob(TBusMessage* message, TJobRunner* job) { - message->Data = job; - } - - TBusJob::TBusJob(TBusModule* module, TBusMessage* message) - : Status(MESSAGE_OK) - , Runner() - , Message(message) - , ReplySent(false) - , Module(module) - , ModuleImpl(module->Impl.Get()) - , SleepUntil(0) - { - Handler = TJobHandler(&TBusModule::Start); - } - - TBusJob::~TBusJob() { - Y_ASSERT(Pending.size() == 0); - //Y_ASSERT(SleepUntil == 0); - - ClearAllMessageStates(); - } - - TNetAddr TBusJob::GetPeerAddrNetAddr() const { - Y_VERIFY(!!OnMessageContext); - return OnMessageContext.GetPeerAddrNetAddr(); - } - - void TBusJob::CheckThreadCurrentJob() { - Y_ASSERT(ThreadCurrentJob == this); - } - - ///////////////////////////////////////////////////////// - /// \brief Send messages in pending list - - /// If at least one message is gone return true - /// If message has not been send, move it to Finished with appropriate error code - bool TBusJob::SendPending() { - // Iterator type must be size_t, not vector::iterator, - // because `DoCallReplyHandler` may call `Send` that modifies `Pending` vector, - // that in turn invalidates iterator. - // Implementation assumes that `DoCallReplyHandler` only pushes back to `Pending` - // (not erases, and not inserts) so iteration by index is valid. - size_t it = 0; - while (it != Pending.size()) { - TJobState& call = Pending[it]; - - if (call.Status == MESSAGE_DONT_ASK) { - EMessageStatus getAddressStatus = MESSAGE_OK; - TNetAddr addr; - if (call.UseAddr) { - addr = call.Addr; - } else { - getAddressStatus = const_cast<TBusProtocol*>(call.Session->GetProto())->GetDestination(call.Session, call.Message, call.Session->GetQueue()->GetLocator(), &addr); - } - - if (getAddressStatus == MESSAGE_OK) { - // hold extra reference for each request in flight - Runner->Ref(); - - if (call.OneWay) { - call.Status = call.Session->SendMessageOneWay(call.Message, &addr); - } else { - call.Status = call.Session->SendMessage(call.Message, &addr); - } - - if (call.Status != MESSAGE_OK) { - Runner->UnRef(); - } + TVector<TBusServerSessionPtr> GetServerSessionsInternal() override { + return ServerSessions; + } + + TBusMessageQueue* GetQueue() override { + return Queue; + } + + TString GetNameInternal() override { + return Name; + } + + TString GetStatusSingleLine() override { + TStringStream ss; + ss << "jobs: " << Size(); + return ss.Str(); + } + + void OnClientConnectionEvent(const TClientConnectionEvent& event) { + Module->OnClientConnectionEvent(event); + } + }; + + struct TJobResponseMessage { + TBusMessage* Request; + TBusMessage* Response; + EMessageStatus Status; + + TJobResponseMessage(TBusMessage* request, TBusMessage* response, EMessageStatus status) + : Request(request) + , Response(response) + , Status(status) + { + } + }; + + struct TJobRunner: public TAtomicRefCount<TJobRunner>, + public NActor::TActor<TJobRunner>, + public NActor::TQueueInActor<TJobRunner, TJobResponseMessage>, + public TScheduleActor<TJobRunner> { + THolder<TBusJob> Job; + + TList<TJobRunner*>::iterator JobStorageIterator; + + TJobRunner(TAutoPtr<TBusJob> job) + : NActor::TActor<TJobRunner>(job->ModuleImpl->Queue->GetExecutor()) + , TScheduleActor<TJobRunner>(&job->ModuleImpl->Scheduler) + , Job(job.Release()) + , JobStorageIterator() + { + Job->Runner = this; + } + + ~TJobRunner() override { + Y_ASSERT(JobStorageIterator == TList<TJobRunner*>::iterator()); + } + + void ProcessItem(NActor::TDefaultTag, NActor::TDefaultTag, const TJobResponseMessage& message) { + Job->CallReplyHandler(message.Status, message.Request, message.Response); + } + + void Destroy() { + if (!!Job->OnMessageContext) { + if (!Job->ReplySent) { + Job->OnMessageContext.ForgetRequest(); + } + } + Job->ModuleImpl->DestroyJob(this); + } + + void Act(NActor::TDefaultTag) { + if (JobStorageIterator == TList<TJobRunner*>::iterator()) { + return; + } + + if (Job->SleepUntil != 0) { + if (AtomicGet(Job->ModuleImpl->State) == TBusModuleImpl::STOPPED) { + Destroy(); + return; + } + } + + TThreadCurrentJobGuard g(Job.Get()); + + NActor::TQueueInActor<TJobRunner, TJobResponseMessage>::DequeueAll(); + + if (Alarm.FetchTask()) { + if (Job->AnyPendingToSend()) { + Y_ASSERT(Job->SleepUntil == 0); + Job->SendPending(); + if (Job->AnyPendingToSend()) { + } + } else { + // regular alarm + Y_ASSERT(Job->Pending.empty()); + Y_ASSERT(Job->SleepUntil != 0); + Job->SleepUntil = 0; + } + } + + for (;;) { + if (Job->Pending.empty() && !!Job->Handler && Job->Status == MESSAGE_OK) { + TWhatThreadDoesPushPop pp("do call job handler (do not confuse with reply handler)"); + + Job->Handler = Job->Handler(Job->Module, Job.Get(), Job->Message); + } + + if (Job->SleepUntil != 0) { + ScheduleAt(TInstant::MilliSeconds(Job->SleepUntil)); + return; + } + + Job->SendPending(); + + if (Job->AnyPendingToSend()) { + ScheduleAt(TInstant::Now() + TDuration::Seconds(1)); + return; + } + + if (!Job->Pending.empty()) { + // waiting replies + return; + } + + if (Job->IsDone()) { + Destroy(); + return; + } + } + } + }; + + } + + static inline TJobRunner* GetJob(TBusMessage* message) { + return (TJobRunner*)message->Data; + } + + static inline void SetJob(TBusMessage* message, TJobRunner* job) { + message->Data = job; + } + + TBusJob::TBusJob(TBusModule* module, TBusMessage* message) + : Status(MESSAGE_OK) + , Runner() + , Message(message) + , ReplySent(false) + , Module(module) + , ModuleImpl(module->Impl.Get()) + , SleepUntil(0) + { + Handler = TJobHandler(&TBusModule::Start); + } + + TBusJob::~TBusJob() { + Y_ASSERT(Pending.size() == 0); + //Y_ASSERT(SleepUntil == 0); + + ClearAllMessageStates(); + } + + TNetAddr TBusJob::GetPeerAddrNetAddr() const { + Y_VERIFY(!!OnMessageContext); + return OnMessageContext.GetPeerAddrNetAddr(); + } + + void TBusJob::CheckThreadCurrentJob() { + Y_ASSERT(ThreadCurrentJob == this); + } + + ///////////////////////////////////////////////////////// + /// \brief Send messages in pending list + + /// If at least one message is gone return true + /// If message has not been send, move it to Finished with appropriate error code + bool TBusJob::SendPending() { + // Iterator type must be size_t, not vector::iterator, + // because `DoCallReplyHandler` may call `Send` that modifies `Pending` vector, + // that in turn invalidates iterator. + // Implementation assumes that `DoCallReplyHandler` only pushes back to `Pending` + // (not erases, and not inserts) so iteration by index is valid. + size_t it = 0; + while (it != Pending.size()) { + TJobState& call = Pending[it]; + + if (call.Status == MESSAGE_DONT_ASK) { + EMessageStatus getAddressStatus = MESSAGE_OK; + TNetAddr addr; + if (call.UseAddr) { + addr = call.Addr; } else { - call.Status = getAddressStatus; + getAddressStatus = const_cast<TBusProtocol*>(call.Session->GetProto())->GetDestination(call.Session, call.Message, call.Session->GetQueue()->GetLocator(), &addr); } - } - - if (call.Status == MESSAGE_OK) { - ++it; // keep pending list until we get reply - } else if (call.Status == MESSAGE_BUSY) { - Y_FAIL("MESSAGE_BUSY is prohibited in modules. Please increase MaxInFlight"); - } else if (call.Status == MESSAGE_CONNECT_FAILED && call.NumRetries < call.MaxRetries) { - ++it; // try up to call.MaxRetries times to send message - call.NumRetries++; - DoCallReplyHandler(call); - call.Status = MESSAGE_DONT_ASK; - call.Message->Reset(); // generate new Id + + if (getAddressStatus == MESSAGE_OK) { + // hold extra reference for each request in flight + Runner->Ref(); + + if (call.OneWay) { + call.Status = call.Session->SendMessageOneWay(call.Message, &addr); + } else { + call.Status = call.Session->SendMessage(call.Message, &addr); + } + + if (call.Status != MESSAGE_OK) { + Runner->UnRef(); + } + + } else { + call.Status = getAddressStatus; + } + } + + if (call.Status == MESSAGE_OK) { + ++it; // keep pending list until we get reply + } else if (call.Status == MESSAGE_BUSY) { + Y_FAIL("MESSAGE_BUSY is prohibited in modules. Please increase MaxInFlight"); + } else if (call.Status == MESSAGE_CONNECT_FAILED && call.NumRetries < call.MaxRetries) { + ++it; // try up to call.MaxRetries times to send message + call.NumRetries++; + DoCallReplyHandler(call); + call.Status = MESSAGE_DONT_ASK; + call.Message->Reset(); // generate new Id } else { - Finished.push_back(call); - DoCallReplyHandler(call); - Pending.erase(Pending.begin() + it); + Finished.push_back(call); + DoCallReplyHandler(call); + Pending.erase(Pending.begin() + it); } } - return Pending.size() > 0; - } - - bool TBusJob::AnyPendingToSend() { - for (unsigned i = 0; i < Pending.size(); ++i) { - if (Pending[i].Status == MESSAGE_DONT_ASK) { - return true; - } + return Pending.size() > 0; + } + + bool TBusJob::AnyPendingToSend() { + for (unsigned i = 0; i < Pending.size(); ++i) { + if (Pending[i].Status == MESSAGE_DONT_ASK) { + return true; + } } - - return false; + + return false; } - bool TBusJob::IsDone() { - bool r = (SleepUntil == 0 && Pending.size() == 0 && (Handler == nullptr || Status != MESSAGE_OK)); - return r; + bool TBusJob::IsDone() { + bool r = (SleepUntil == 0 && Pending.size() == 0 && (Handler == nullptr || Status != MESSAGE_OK)); + return r; + } + + void TBusJob::CallJobHandlerOnly() { + TThreadCurrentJobGuard threadCurrentJobGuard(this); + TWhatThreadDoesPushPop pp("do call job handler (do not confuse with reply handler)"); + + Handler = Handler(ModuleImpl->Module, this, Message); } - void TBusJob::CallJobHandlerOnly() { - TThreadCurrentJobGuard threadCurrentJobGuard(this); - TWhatThreadDoesPushPop pp("do call job handler (do not confuse with reply handler)"); + bool TBusJob::CallJobHandler() { + /// go on as far as we can go without waiting + while (!IsDone()) { + /// call the handler + CallJobHandlerOnly(); - Handler = Handler(ModuleImpl->Module, this, Message); - } + /// quit if job is canceled + if (Status != MESSAGE_OK) { + break; + } - bool TBusJob::CallJobHandler() { - /// go on as far as we can go without waiting - while (!IsDone()) { - /// call the handler - CallJobHandlerOnly(); - - /// quit if job is canceled - if (Status != MESSAGE_OK) { - break; - } + /// there are messages to send and wait for reply + SendPending(); - /// there are messages to send and wait for reply - SendPending(); + if (!Pending.empty()) { + break; + } - if (!Pending.empty()) { - break; - } + /// asked to sleep + if (SleepUntil) { + break; + } + } - /// asked to sleep - if (SleepUntil) { - break; - } - } + Y_VERIFY(!(Pending.size() == 0 && Handler == nullptr && Status == MESSAGE_OK && !ReplySent), + "Handler returned NULL without Cancel() or SendReply() for message=%016" PRIx64 " type=%d", + Message->GetHeader()->Id, Message->GetHeader()->Type); - Y_VERIFY(!(Pending.size() == 0 && Handler == nullptr && Status == MESSAGE_OK && !ReplySent), - "Handler returned NULL without Cancel() or SendReply() for message=%016" PRIx64 " type=%d", - Message->GetHeader()->Id, Message->GetHeader()->Type); + return IsDone(); + } - return IsDone(); - } + void TBusJob::DoCallReplyHandler(TJobState& call) { + if (call.Handler) { + TWhatThreadDoesPushPop pp("do call reply handler (do not confuse with job handler)"); - void TBusJob::DoCallReplyHandler(TJobState& call) { - if (call.Handler) { - TWhatThreadDoesPushPop pp("do call reply handler (do not confuse with job handler)"); + TThreadCurrentJobGuard threadCurrentJobGuard(this); + (Module->*(call.Handler))(this, call.Status, call.Message, call.Reply); + } + } - TThreadCurrentJobGuard threadCurrentJobGuard(this); - (Module->*(call.Handler))(this, call.Status, call.Message, call.Reply); + int TBusJob::CallReplyHandler(EMessageStatus status, TBusMessage* mess, TBusMessage* reply) { + /// find handler for given message and update it's status + size_t i = 0; + for (; i < Pending.size(); ++i) { + TJobState& call = Pending[i]; + if (call.Message == mess) { + break; + } } - } - - int TBusJob::CallReplyHandler(EMessageStatus status, TBusMessage* mess, TBusMessage* reply) { - /// find handler for given message and update it's status - size_t i = 0; - for (; i < Pending.size(); ++i) { - TJobState& call = Pending[i]; - if (call.Message == mess) { - break; - } + + /// if not found, report error + if (i == Pending.size()) { + Y_FAIL("must not happen"); } - /// if not found, report error - if (i == Pending.size()) { - Y_FAIL("must not happen"); - } - - /// fill in response into job state - TJobState& call = Pending[i]; - call.Status = status; - Y_ASSERT(call.Message == mess); - call.Reply = reply; - - if ((status == MESSAGE_TIMEOUT || status == MESSAGE_DELIVERY_FAILED) && call.NumRetries < call.MaxRetries) { - call.NumRetries++; - call.Status = MESSAGE_DONT_ASK; - call.Message->Reset(); // generate new Id - DoCallReplyHandler(call); - return 0; + /// fill in response into job state + TJobState& call = Pending[i]; + call.Status = status; + Y_ASSERT(call.Message == mess); + call.Reply = reply; + + if ((status == MESSAGE_TIMEOUT || status == MESSAGE_DELIVERY_FAILED) && call.NumRetries < call.MaxRetries) { + call.NumRetries++; + call.Status = MESSAGE_DONT_ASK; + call.Message->Reset(); // generate new Id + DoCallReplyHandler(call); + return 0; } - /// call the handler if provided - DoCallReplyHandler(call); + /// call the handler if provided + DoCallReplyHandler(call); - /// move job state into the finished stack - Finished.push_back(Pending[i]); - Pending.erase(Pending.begin() + i); + /// move job state into the finished stack + Finished.push_back(Pending[i]); + Pending.erase(Pending.begin() + i); return 0; } - /////////////////////////////////////////////////////////////// - /// send message to any other session or application - void TBusJob::Send(TBusMessageAutoPtr mess, TBusClientSession* session, TReplyHandler rhandler, size_t maxRetries) { - CheckThreadCurrentJob(); + /////////////////////////////////////////////////////////////// + /// send message to any other session or application + void TBusJob::Send(TBusMessageAutoPtr mess, TBusClientSession* session, TReplyHandler rhandler, size_t maxRetries) { + CheckThreadCurrentJob(); - SetJob(mess.Get(), Runner); - Pending.push_back(TJobState(rhandler, MESSAGE_DONT_ASK, mess.Release(), session, nullptr, maxRetries, nullptr, false)); - } + SetJob(mess.Get(), Runner); + Pending.push_back(TJobState(rhandler, MESSAGE_DONT_ASK, mess.Release(), session, nullptr, maxRetries, nullptr, false)); + } - void TBusJob::Send(TBusMessageAutoPtr mess, TBusClientSession* session, TReplyHandler rhandler, size_t maxRetries, const TNetAddr& addr) { - CheckThreadCurrentJob(); + void TBusJob::Send(TBusMessageAutoPtr mess, TBusClientSession* session, TReplyHandler rhandler, size_t maxRetries, const TNetAddr& addr) { + CheckThreadCurrentJob(); - SetJob(mess.Get(), Runner); - Pending.push_back(TJobState(rhandler, MESSAGE_DONT_ASK, mess.Release(), session, nullptr, maxRetries, &addr, false)); - } + SetJob(mess.Get(), Runner); + Pending.push_back(TJobState(rhandler, MESSAGE_DONT_ASK, mess.Release(), session, nullptr, maxRetries, &addr, false)); + } - void TBusJob::SendOneWayTo(TBusMessageAutoPtr req, TBusClientSession* session, const TNetAddr& addr) { - CheckThreadCurrentJob(); + void TBusJob::SendOneWayTo(TBusMessageAutoPtr req, TBusClientSession* session, const TNetAddr& addr) { + CheckThreadCurrentJob(); - SetJob(req.Get(), Runner); - Pending.push_back(TJobState(nullptr, MESSAGE_DONT_ASK, req.Release(), session, nullptr, 0, &addr, true)); - } + SetJob(req.Get(), Runner); + Pending.push_back(TJobState(nullptr, MESSAGE_DONT_ASK, req.Release(), session, nullptr, 0, &addr, true)); + } - void TBusJob::SendOneWayWithLocator(TBusMessageAutoPtr req, TBusClientSession* session) { - CheckThreadCurrentJob(); + void TBusJob::SendOneWayWithLocator(TBusMessageAutoPtr req, TBusClientSession* session) { + CheckThreadCurrentJob(); - SetJob(req.Get(), Runner); - Pending.push_back(TJobState(nullptr, MESSAGE_DONT_ASK, req.Release(), session, nullptr, 0, nullptr, true)); - } + SetJob(req.Get(), Runner); + Pending.push_back(TJobState(nullptr, MESSAGE_DONT_ASK, req.Release(), session, nullptr, 0, nullptr, true)); + } - /////////////////////////////////////////////////////////////// - /// send reply to the starter message - void TBusJob::SendReply(TBusMessageAutoPtr reply) { - CheckThreadCurrentJob(); + /////////////////////////////////////////////////////////////// + /// send reply to the starter message + void TBusJob::SendReply(TBusMessageAutoPtr reply) { + CheckThreadCurrentJob(); - Y_VERIFY(!ReplySent, "cannot call SendReply twice"); - ReplySent = true; - if (!OnMessageContext) - return; + Y_VERIFY(!ReplySent, "cannot call SendReply twice"); + ReplySent = true; + if (!OnMessageContext) + return; - EMessageStatus ok = OnMessageContext.SendReplyMove(reply); - if (ok != MESSAGE_OK) { - // TODO: count errors - } - } + EMessageStatus ok = OnMessageContext.SendReplyMove(reply); + if (ok != MESSAGE_OK) { + // TODO: count errors + } + } - /// set the flag to terminate job at the earliest convenience - void TBusJob::Cancel(EMessageStatus status) { - CheckThreadCurrentJob(); + /// set the flag to terminate job at the earliest convenience + void TBusJob::Cancel(EMessageStatus status) { + CheckThreadCurrentJob(); - Status = status; + Status = status; } - void TBusJob::ClearState(TJobState& call) { - TJobStateVec::iterator it; - for (it = Finished.begin(); it != Finished.end(); ++it) { - TJobState& state = *it; - if (&call == &state) { - ::ClearState(&call); - Finished.erase(it); - return; - } + void TBusJob::ClearState(TJobState& call) { + TJobStateVec::iterator it; + for (it = Finished.begin(); it != Finished.end(); ++it) { + TJobState& state = *it; + if (&call == &state) { + ::ClearState(&call); + Finished.erase(it); + return; + } } - Y_ASSERT(0); + Y_ASSERT(0); } - void TBusJob::ClearAllMessageStates() { - ClearJobStateVector(&Finished); - ClearJobStateVector(&Pending); - } + void TBusJob::ClearAllMessageStates() { + ClearJobStateVector(&Finished); + ClearJobStateVector(&Pending); + } - void TBusJob::Sleep(int milliSeconds) { - CheckThreadCurrentJob(); + void TBusJob::Sleep(int milliSeconds) { + CheckThreadCurrentJob(); - Y_VERIFY(Pending.empty(), "sleep is not allowed when there are pending job"); - Y_VERIFY(SleepUntil == 0, "must not override sleep"); + Y_VERIFY(Pending.empty(), "sleep is not allowed when there are pending job"); + Y_VERIFY(SleepUntil == 0, "must not override sleep"); - SleepUntil = Now() + milliSeconds; - } + SleepUntil = Now() + milliSeconds; + } - TString TBusJob::GetStatus(unsigned flags) { - TString strReturn; - strReturn += Sprintf(" job=%016" PRIx64 " type=%d sent=%d pending=%d (%d) %s\n", - Message->GetHeader()->Id, - (int)Message->GetHeader()->Type, - (int)(Now() - Message->GetHeader()->SendTime) / 1000, - (int)Pending.size(), - (int)Finished.size(), + TString TBusJob::GetStatus(unsigned flags) { + TString strReturn; + strReturn += Sprintf(" job=%016" PRIx64 " type=%d sent=%d pending=%d (%d) %s\n", + Message->GetHeader()->Id, + (int)Message->GetHeader()->Type, + (int)(Now() - Message->GetHeader()->SendTime) / 1000, + (int)Pending.size(), + (int)Finished.size(), Status != MESSAGE_OK ? ToString(Status).data() : ""); - - TJobStateVec::iterator it; - for (it = Pending.begin(); it != Pending.end(); ++it) { - TJobState& call = *it; - strReturn += call.GetStatus(flags); - } - return strReturn; - } - - TString TJobState::GetStatus(unsigned flags) { - Y_UNUSED(flags); - TString strReturn; - strReturn += Sprintf(" pending=%016" PRIx64 " type=%d (%s) sent=%d %s\n", - Message->GetHeader()->Id, - (int)Message->GetHeader()->Type, - Session->GetProto()->GetService(), - (int)(Now() - Message->GetHeader()->SendTime) / 1000, + + TJobStateVec::iterator it; + for (it = Pending.begin(); it != Pending.end(); ++it) { + TJobState& call = *it; + strReturn += call.GetStatus(flags); + } + return strReturn; + } + + TString TJobState::GetStatus(unsigned flags) { + Y_UNUSED(flags); + TString strReturn; + strReturn += Sprintf(" pending=%016" PRIx64 " type=%d (%s) sent=%d %s\n", + Message->GetHeader()->Id, + (int)Message->GetHeader()->Type, + Session->GetProto()->GetService(), + (int)(Now() - Message->GetHeader()->SendTime) / 1000, ToString(Status).data()); - return strReturn; - } - - ////////////////////////////////////////////////////////////////////// - - void TBusModuleImpl::CancelJob(TBusJob* job, EMessageStatus status) { - TWhatThreadDoesAcquireGuard<TMutex> G(Lock, "modules: acquiring lock for CancelJob"); - if (job) { - job->Cancel(status); - } - } - - TString TBusModuleImpl::GetStatus(unsigned flags) { - Y_UNUSED(flags); - TWhatThreadDoesAcquireGuard<TMutex> G(Lock, "modules: acquiring lock for GetStatus"); - TString strReturn = Sprintf("JobsInFlight=%d\n", (int)Jobs.size()); - for (auto job : Jobs) { - //strReturn += job->Job->GetStatus(flags); - Y_UNUSED(job); - strReturn += "TODO\n"; - } - return strReturn; - } - - TBusModuleConfig::TBusModuleConfig() - : StarterMaxInFlight(1000) - { - } - - TBusModuleConfig::TSecret::TSecret() - : SchedulePeriod(TDuration::Seconds(1)) - { - } - - TBusModule::TBusModule(const char* name) - : Impl(new TBusModuleImpl(this, name)) - { - } - - TBusModule::~TBusModule() { - } - - const char* TBusModule::GetName() const { - return Impl->Name; - } - - void TBusModule::SetConfig(const TBusModuleConfig& config) { - Impl->ModuleConfig = config; - } - - bool TBusModule::StartInput() { - Y_VERIFY(Impl->State == TBusModuleImpl::CREATED, "state check"); - Y_VERIFY(!!Impl->Queue, "state check"); - Impl->State = TBusModuleImpl::RUNNING; - - Y_ASSERT(!Impl->ExternalSession); - TBusServerSessionPtr extSession = CreateExtSession(*Impl->Queue); - if (extSession != nullptr) { - Impl->ExternalSession = extSession; - } - - return true; - } - - bool TBusModule::Shutdown() { - Impl->Shutdown(); - - return true; - } - - TBusJob* TBusModule::CreateJobInstance(TBusMessage* message) { - TBusJob* job = new TBusJob(this, message); - return job; - } - - /** + return strReturn; + } + + ////////////////////////////////////////////////////////////////////// + + void TBusModuleImpl::CancelJob(TBusJob* job, EMessageStatus status) { + TWhatThreadDoesAcquireGuard<TMutex> G(Lock, "modules: acquiring lock for CancelJob"); + if (job) { + job->Cancel(status); + } + } + + TString TBusModuleImpl::GetStatus(unsigned flags) { + Y_UNUSED(flags); + TWhatThreadDoesAcquireGuard<TMutex> G(Lock, "modules: acquiring lock for GetStatus"); + TString strReturn = Sprintf("JobsInFlight=%d\n", (int)Jobs.size()); + for (auto job : Jobs) { + //strReturn += job->Job->GetStatus(flags); + Y_UNUSED(job); + strReturn += "TODO\n"; + } + return strReturn; + } + + TBusModuleConfig::TBusModuleConfig() + : StarterMaxInFlight(1000) + { + } + + TBusModuleConfig::TSecret::TSecret() + : SchedulePeriod(TDuration::Seconds(1)) + { + } + + TBusModule::TBusModule(const char* name) + : Impl(new TBusModuleImpl(this, name)) + { + } + + TBusModule::~TBusModule() { + } + + const char* TBusModule::GetName() const { + return Impl->Name; + } + + void TBusModule::SetConfig(const TBusModuleConfig& config) { + Impl->ModuleConfig = config; + } + + bool TBusModule::StartInput() { + Y_VERIFY(Impl->State == TBusModuleImpl::CREATED, "state check"); + Y_VERIFY(!!Impl->Queue, "state check"); + Impl->State = TBusModuleImpl::RUNNING; + + Y_ASSERT(!Impl->ExternalSession); + TBusServerSessionPtr extSession = CreateExtSession(*Impl->Queue); + if (extSession != nullptr) { + Impl->ExternalSession = extSession; + } + + return true; + } + + bool TBusModule::Shutdown() { + Impl->Shutdown(); + + return true; + } + + TBusJob* TBusModule::CreateJobInstance(TBusMessage* message) { + TBusJob* job = new TBusJob(this, message); + return job; + } + + /** Example for external session creation: TBusSession* TMyModule::CreateExtSession(TBusMessageQueue& queue) { @@ -698,77 +698,77 @@ TBusSession* TMyModule::CreateExtSession(TBusMessageQueue& queue) { return session; */ - bool TBusModule::CreatePrivateSessions(TBusMessageQueue* queue) { - Impl->Queue = queue; - return true; - } - - int TBusModule::GetModuleSessionInFlight() const { - return Impl->Size(); - } - - TIntrusivePtr<TBusModuleInternal> TBusModule::GetInternal() { - return Impl.Get(); - } - - TBusServerSessionPtr TBusModule::CreateDefaultDestination( - TBusMessageQueue& queue, TBusProtocol* proto, const TBusServerSessionConfig& config, const TString& name) { - TBusServerSessionConfig patchedConfig = config; - patchedConfig.ExecuteOnMessageInWorkerPool = false; - if (!patchedConfig.Name) { - patchedConfig.Name = name; - } - if (!patchedConfig.Name) { - patchedConfig.Name = Impl->Name; - } - TBusServerSessionPtr session = - TBusServerSession::Create(proto, Impl->ModuleServerHandler.Get(), patchedConfig, &queue); - Impl->ServerSessions.push_back(session); - return session; - } - - TBusClientSessionPtr TBusModule::CreateDefaultSource( - TBusMessageQueue& queue, TBusProtocol* proto, const TBusClientSessionConfig& config, const TString& name) { - TBusClientSessionConfig patchedConfig = config; - patchedConfig.ExecuteOnReplyInWorkerPool = false; - if (!patchedConfig.Name) { - patchedConfig.Name = name; - } - if (!patchedConfig.Name) { - patchedConfig.Name = Impl->Name; - } - TBusClientSessionPtr session = - TBusClientSession::Create(proto, Impl->ModuleClientHandler.Get(), patchedConfig, &queue); - Impl->ClientSessions.push_back(session); - return session; - } - - TBusStarter* TBusModule::CreateDefaultStarter(TBusMessageQueue&, const TBusSessionConfig& config) { - TBusStarter* session = new TBusStarter(this, config); - Impl->Starters.push_back(session); - return session; - } - - void TBusModule::OnClientConnectionEvent(const TClientConnectionEvent& event) { - Y_UNUSED(event); - } - - TString TBusModule::GetStatus(unsigned flags) { - TString strReturn = Sprintf("%s\n", Impl->Name); - strReturn += Impl->GetStatus(flags); - return strReturn; - } + bool TBusModule::CreatePrivateSessions(TBusMessageQueue* queue) { + Impl->Queue = queue; + return true; + } + + int TBusModule::GetModuleSessionInFlight() const { + return Impl->Size(); + } + + TIntrusivePtr<TBusModuleInternal> TBusModule::GetInternal() { + return Impl.Get(); + } + + TBusServerSessionPtr TBusModule::CreateDefaultDestination( + TBusMessageQueue& queue, TBusProtocol* proto, const TBusServerSessionConfig& config, const TString& name) { + TBusServerSessionConfig patchedConfig = config; + patchedConfig.ExecuteOnMessageInWorkerPool = false; + if (!patchedConfig.Name) { + patchedConfig.Name = name; + } + if (!patchedConfig.Name) { + patchedConfig.Name = Impl->Name; + } + TBusServerSessionPtr session = + TBusServerSession::Create(proto, Impl->ModuleServerHandler.Get(), patchedConfig, &queue); + Impl->ServerSessions.push_back(session); + return session; + } + + TBusClientSessionPtr TBusModule::CreateDefaultSource( + TBusMessageQueue& queue, TBusProtocol* proto, const TBusClientSessionConfig& config, const TString& name) { + TBusClientSessionConfig patchedConfig = config; + patchedConfig.ExecuteOnReplyInWorkerPool = false; + if (!patchedConfig.Name) { + patchedConfig.Name = name; + } + if (!patchedConfig.Name) { + patchedConfig.Name = Impl->Name; + } + TBusClientSessionPtr session = + TBusClientSession::Create(proto, Impl->ModuleClientHandler.Get(), patchedConfig, &queue); + Impl->ClientSessions.push_back(session); + return session; + } + + TBusStarter* TBusModule::CreateDefaultStarter(TBusMessageQueue&, const TBusSessionConfig& config) { + TBusStarter* session = new TBusStarter(this, config); + Impl->Starters.push_back(session); + return session; + } + + void TBusModule::OnClientConnectionEvent(const TClientConnectionEvent& event) { + Y_UNUSED(event); + } + + TString TBusModule::GetStatus(unsigned flags) { + TString strReturn = Sprintf("%s\n", Impl->Name); + strReturn += Impl->GetStatus(flags); + return strReturn; + } } -void TBusModuleImpl::AddJob(TJobRunner* jobRunner) { +void TBusModuleImpl::AddJob(TJobRunner* jobRunner) { TWhatThreadDoesAcquireGuard<TMutex> G(Lock, "modules: acquiring lock for AddJob"); Jobs.push_back(jobRunner); jobRunner->JobStorageIterator = Jobs.end(); --jobRunner->JobStorageIterator; } -void TBusModuleImpl::DestroyJob(TJobRunner* job) { +void TBusModuleImpl::DestroyJob(TJobRunner* job) { Y_ASSERT(job->JobStorageIterator != TList<TJobRunner*>::iterator()); { @@ -837,7 +837,7 @@ EMessageStatus TBusModule::StartJob(TAutoPtr<TBusMessage> message) { Y_VERIFY(Impl->State == TBusModuleImpl::RUNNING); Y_VERIFY(!!Impl->Queue); - if ((unsigned)AtomicGet(Impl->JobCount) >= Impl->ModuleConfig.StarterMaxInFlight) { + if ((unsigned)AtomicGet(Impl->JobCount) >= Impl->ModuleConfig.StarterMaxInFlight) { return MESSAGE_BUSY; } diff --git a/library/cpp/messagebus/oldmodule/module.h b/library/cpp/messagebus/oldmodule/module.h index c917f9cae7..8d1c4a5d52 100644 --- a/library/cpp/messagebus/oldmodule/module.h +++ b/library/cpp/messagebus/oldmodule/module.h @@ -1,37 +1,37 @@ -#pragma once - +#pragma once + /////////////////////////////////////////////////////////////////////////// /// \file /// \brief Application interface for modules - + /// NBus::TBusModule provides foundation for implementation of asynchnous /// modules that communicate with multiple external or local sessions -/// NBus::TBusSession. - +/// NBus::TBusSession. + /// To implement the module some virtual functions needs to be overridden: -/// NBus::TBusModule::CreateExtSession() creates and registers an +/// NBus::TBusModule::CreateExtSession() creates and registers an /// external session that receives incoming messages as input for module /// processing. - -/// When new incoming message arrives the new NBus::TBusJob is created. + +/// When new incoming message arrives the new NBus::TBusJob is created. /// NBus::TBusJob is somewhat similar to a thread, it maintains all the state -/// during processing of one incoming message. Default implementation of -/// NBus::TBusJob will maintain all send and received messages during +/// during processing of one incoming message. Default implementation of +/// NBus::TBusJob will maintain all send and received messages during /// lifetime of this job. Each message, status and reply can be found /// within NBus::TJobState using NBus::TBusJob::GetState(). If your module /// needs to maintain an additional information during lifetime of the job -/// you can derive your own class from NBus::TBusJob and override job +/// you can derive your own class from NBus::TBusJob and override job /// factory method NBus::IJobFactory::CreateJobInstance() to create your instances. - + /// Processing of a given message starts with a call to NBus::TBusModule::Start() /// handler that should be overridden in the module implementation. Within /// the callback handler module can perform any computation and access any /// datastore tables that it needs. The handler can also access any module -/// variables. However, same handler can be called from multiple threads so, +/// variables. However, same handler can be called from multiple threads so, /// it is recommended that handler only access read-only module level variables. - -/// Handler should use NBus::TBusJob::Send() to send messages to other client + +/// Handler should use NBus::TBusJob::Send() to send messages to other client /// sessions and it can use NBus::TBusJob::Reply() to send reply to the main /// job message. When handler is done, it returns the pointer to the next handler to call /// when all pending messages have cleared. If handler @@ -47,364 +47,364 @@ #include <util/generic/object_counter.h> namespace NBus { - class TBusJob; - class TBusModule; - - namespace NPrivate { - struct TCallJobHandlerWorkItem; - struct TBusModuleImpl; - struct TModuleServerHandler; - struct TModuleClientHandler; - struct TJobRunner; - } - - class TJobHandler { - protected: - typedef TJobHandler (TBusModule::*TBusHandlerPtr)(TBusJob* job, TBusMessage* mess); - TBusHandlerPtr MyPtr; - - public: - template <class B> - TJobHandler(TJobHandler (B::*fptr)(TBusJob* job, TBusMessage* mess)) { - MyPtr = static_cast<TBusHandlerPtr>(fptr); - } - TJobHandler(TBusHandlerPtr fptr = nullptr) { - MyPtr = fptr; - } + class TBusJob; + class TBusModule; + + namespace NPrivate { + struct TCallJobHandlerWorkItem; + struct TBusModuleImpl; + struct TModuleServerHandler; + struct TModuleClientHandler; + struct TJobRunner; + } + + class TJobHandler { + protected: + typedef TJobHandler (TBusModule::*TBusHandlerPtr)(TBusJob* job, TBusMessage* mess); + TBusHandlerPtr MyPtr; + + public: + template <class B> + TJobHandler(TJobHandler (B::*fptr)(TBusJob* job, TBusMessage* mess)) { + MyPtr = static_cast<TBusHandlerPtr>(fptr); + } + TJobHandler(TBusHandlerPtr fptr = nullptr) { + MyPtr = fptr; + } TJobHandler(const TJobHandler&) = default; TJobHandler& operator =(const TJobHandler&) = default; bool operator==(TJobHandler h) const { - return MyPtr == h.MyPtr; - } + return MyPtr == h.MyPtr; + } bool operator!=(TJobHandler h) const { - return MyPtr != h.MyPtr; - } - bool operator!() const { - return !MyPtr; - } - TJobHandler operator()(TBusModule* b, TBusJob* job, TBusMessage* mess) { - return (b->*MyPtr)(job, mess); - } - }; - - typedef void (TBusModule::*TReplyHandler)(TBusJob* job, EMessageStatus status, TBusMessage* mess, TBusMessage* reply); - - //////////////////////////////////////////////////// - /// \brief Pending message state - - struct TJobState { - friend class TBusJob; - friend class ::TCrawlerModule; - - TReplyHandler Handler; - EMessageStatus Status; - TBusMessage* Message; - TBusMessage* Reply; - TBusClientSession* Session; - size_t NumRetries; - size_t MaxRetries; - // If != NULL then use it as destination. - TNetAddr Addr; - bool UseAddr; - bool OneWay; - - private: - TJobState(TReplyHandler handler, - EMessageStatus status, - TBusMessage* mess, TBusClientSession* session, TBusMessage* reply, size_t maxRetries = 0, - const TNetAddr* addr = nullptr, bool oneWay = false) - : Handler(handler) - , Status(status) - , Message(mess) - , Reply(reply) - , Session(session) - , NumRetries(0) - , MaxRetries(maxRetries) - , OneWay(oneWay) - { - if (!!addr) { - Addr = *addr; - } - UseAddr = !!addr; + return MyPtr != h.MyPtr; + } + bool operator!() const { + return !MyPtr; + } + TJobHandler operator()(TBusModule* b, TBusJob* job, TBusMessage* mess) { + return (b->*MyPtr)(job, mess); + } + }; + + typedef void (TBusModule::*TReplyHandler)(TBusJob* job, EMessageStatus status, TBusMessage* mess, TBusMessage* reply); + + //////////////////////////////////////////////////// + /// \brief Pending message state + + struct TJobState { + friend class TBusJob; + friend class ::TCrawlerModule; + + TReplyHandler Handler; + EMessageStatus Status; + TBusMessage* Message; + TBusMessage* Reply; + TBusClientSession* Session; + size_t NumRetries; + size_t MaxRetries; + // If != NULL then use it as destination. + TNetAddr Addr; + bool UseAddr; + bool OneWay; + + private: + TJobState(TReplyHandler handler, + EMessageStatus status, + TBusMessage* mess, TBusClientSession* session, TBusMessage* reply, size_t maxRetries = 0, + const TNetAddr* addr = nullptr, bool oneWay = false) + : Handler(handler) + , Status(status) + , Message(mess) + , Reply(reply) + , Session(session) + , NumRetries(0) + , MaxRetries(maxRetries) + , OneWay(oneWay) + { + if (!!addr) { + Addr = *addr; + } + UseAddr = !!addr; + } + + public: + TString GetStatus(unsigned flags); + }; + + using TJobStateVec = TVector<TJobState>; + + ///////////////////////////////////////////////////////// + /// \brief Execution item = thread + + /// Maintains internal state of document in computation + class TBusJob { + TObjectCounter<TBusJob> ObjectCounter; + + private: + void CheckThreadCurrentJob(); + + public: + /// given a module and starter message + TBusJob(TBusModule* module, TBusMessage* message); + + /// destructor will free all the message that were send and received + virtual ~TBusJob(); + + TBusMessage* GetMessage() const { + return Message; + } + + TNetAddr GetPeerAddrNetAddr() const; + + /// send message to any other session or application + /// If addr is set then use it as destination. + void Send(TBusMessageAutoPtr mess, TBusClientSession* session, TReplyHandler rhandler, size_t maxRetries, const TNetAddr& addr); + void Send(TBusMessageAutoPtr mess, TBusClientSession* session, TReplyHandler rhandler = nullptr, size_t maxRetries = 0); + + void SendOneWayTo(TBusMessageAutoPtr req, TBusClientSession* session, const TNetAddr& addr); + void SendOneWayWithLocator(TBusMessageAutoPtr req, TBusClientSession* session); + + /// send reply to the starter message + virtual void SendReply(TBusMessageAutoPtr reply); + + /// set the flag to terminate job at the earliest convenience + void Cancel(EMessageStatus status); + + /// helper to put item on finished list of states + /// It should not be a part of public API, + /// so prohibit it for all except current users. + private: + friend class ::TCrawlerModule; + void PutState(const TJobState& state) { + Finished.push_back(state); } - public: - TString GetStatus(unsigned flags); - }; - - using TJobStateVec = TVector<TJobState>; - - ///////////////////////////////////////////////////////// - /// \brief Execution item = thread - - /// Maintains internal state of document in computation - class TBusJob { - TObjectCounter<TBusJob> ObjectCounter; - - private: - void CheckThreadCurrentJob(); - - public: - /// given a module and starter message - TBusJob(TBusModule* module, TBusMessage* message); - - /// destructor will free all the message that were send and received - virtual ~TBusJob(); - - TBusMessage* GetMessage() const { - return Message; - } - - TNetAddr GetPeerAddrNetAddr() const; - - /// send message to any other session or application - /// If addr is set then use it as destination. - void Send(TBusMessageAutoPtr mess, TBusClientSession* session, TReplyHandler rhandler, size_t maxRetries, const TNetAddr& addr); - void Send(TBusMessageAutoPtr mess, TBusClientSession* session, TReplyHandler rhandler = nullptr, size_t maxRetries = 0); - - void SendOneWayTo(TBusMessageAutoPtr req, TBusClientSession* session, const TNetAddr& addr); - void SendOneWayWithLocator(TBusMessageAutoPtr req, TBusClientSession* session); - - /// send reply to the starter message - virtual void SendReply(TBusMessageAutoPtr reply); - - /// set the flag to terminate job at the earliest convenience - void Cancel(EMessageStatus status); - - /// helper to put item on finished list of states - /// It should not be a part of public API, - /// so prohibit it for all except current users. - private: - friend class ::TCrawlerModule; - void PutState(const TJobState& state) { - Finished.push_back(state); - } - - public: - /// retrieve all pending messages - void GetPending(TJobStateVec* stateVec) { - Y_ASSERT(stateVec); - *stateVec = Pending; - } - - /// helper function to find state of previously sent messages - template <class MessageType> - TJobState* GetState(int* startFrom = nullptr) { - for (int i = startFrom ? *startFrom : 0; i < int(Finished.size()); i++) { - TJobState* call = &Finished[i]; - if (call->Reply != nullptr && dynamic_cast<MessageType*>(call->Reply)) { - if (startFrom) { - *startFrom = i; - } - return call; - } - if (call->Message != nullptr && dynamic_cast<MessageType*>(call->Message)) { - if (startFrom) { - *startFrom = i; - } - return call; - } + public: + /// retrieve all pending messages + void GetPending(TJobStateVec* stateVec) { + Y_ASSERT(stateVec); + *stateVec = Pending; + } + + /// helper function to find state of previously sent messages + template <class MessageType> + TJobState* GetState(int* startFrom = nullptr) { + for (int i = startFrom ? *startFrom : 0; i < int(Finished.size()); i++) { + TJobState* call = &Finished[i]; + if (call->Reply != nullptr && dynamic_cast<MessageType*>(call->Reply)) { + if (startFrom) { + *startFrom = i; + } + return call; + } + if (call->Message != nullptr && dynamic_cast<MessageType*>(call->Message)) { + if (startFrom) { + *startFrom = i; + } + return call; + } } - return nullptr; + return nullptr; } - /// helper function to find response for previously sent messages - template <class MessageType> - MessageType* Get(int* startFrom = nullptr) { - for (int i = startFrom ? *startFrom : 0; i < int(Finished.size()); i++) { - TJobState& call = Finished[i]; - if (call.Reply != nullptr && dynamic_cast<MessageType*>(call.Reply)) { - if (startFrom) { - *startFrom = i; - } - return static_cast<MessageType*>(call.Reply); - } - if (call.Message != nullptr && dynamic_cast<MessageType*>(call.Message)) { - if (startFrom) { - *startFrom = i; - } - return static_cast<MessageType*>(call.Message); - } + /// helper function to find response for previously sent messages + template <class MessageType> + MessageType* Get(int* startFrom = nullptr) { + for (int i = startFrom ? *startFrom : 0; i < int(Finished.size()); i++) { + TJobState& call = Finished[i]; + if (call.Reply != nullptr && dynamic_cast<MessageType*>(call.Reply)) { + if (startFrom) { + *startFrom = i; + } + return static_cast<MessageType*>(call.Reply); + } + if (call.Message != nullptr && dynamic_cast<MessageType*>(call.Message)) { + if (startFrom) { + *startFrom = i; + } + return static_cast<MessageType*>(call.Message); + } } - return nullptr; + return nullptr; } - /// helper function to find status for previously sent message - template <class MessageType> - EMessageStatus GetStatus(int* startFrom = nullptr) { - for (int i = startFrom ? *startFrom : 0; i < int(Finished.size()); i++) { - TJobState& call = Finished[i]; - if (call.Message != nullptr && dynamic_cast<MessageType*>(call.Message)) { - if (startFrom) { - *startFrom = i; - } - return call.Status; - } + /// helper function to find status for previously sent message + template <class MessageType> + EMessageStatus GetStatus(int* startFrom = nullptr) { + for (int i = startFrom ? *startFrom : 0; i < int(Finished.size()); i++) { + TJobState& call = Finished[i]; + if (call.Message != nullptr && dynamic_cast<MessageType*>(call.Message)) { + if (startFrom) { + *startFrom = i; + } + return call.Status; + } } - return MESSAGE_UNKNOWN; + return MESSAGE_UNKNOWN; } - /// helper function to clear state of previosly sent messages - template <class MessageType> - void Clear() { - for (size_t i = 0; i < Finished.size();) { - // `Finished.size() - i` decreases with each iteration - // we either increment i, or remove element from Finished. - TJobState& call = Finished[i]; - if (call.Message != nullptr && dynamic_cast<MessageType*>(call.Message)) { - ClearState(call); - } else { - ++i; - } + /// helper function to clear state of previosly sent messages + template <class MessageType> + void Clear() { + for (size_t i = 0; i < Finished.size();) { + // `Finished.size() - i` decreases with each iteration + // we either increment i, or remove element from Finished. + TJobState& call = Finished[i]; + if (call.Message != nullptr && dynamic_cast<MessageType*>(call.Message)) { + ClearState(call); + } else { + ++i; + } } } - /// helper function to clear state in order to try again - void ClearState(TJobState& state); - - /// clears all message states - void ClearAllMessageStates(); - - /// returns true if job is done - bool IsDone(); - - /// return human reabable status of this job - virtual TString GetStatus(unsigned flags); - - /// set sleep time for job - void Sleep(int milliSeconds); - - void CallJobHandlerOnly(); - - private: - bool CallJobHandler(); - void DoCallReplyHandler(TJobState&); - /// send out all Pending jobs, failed sends will be migrated to Finished - bool SendPending(); - bool AnyPendingToSend(); - - public: - /// helper to call from OnReply() and OnError() - int CallReplyHandler(EMessageStatus status, TBusMessage* mess, TBusMessage* reply); - - public: - TJobHandler Handler; ///< job handler to be executed within next CallJobHandler() - EMessageStatus Status; ///< set != MESSAGE_OK if job should terminate asap - private: - NPrivate::TJobRunner* Runner; - TBusMessage* Message; - THolder<TBusMessage> MessageHolder; - TOnMessageContext OnMessageContext; // starter - public: - bool ReplySent; - - private: - friend class TBusModule; - friend struct NPrivate::TBusModuleImpl; - friend struct NPrivate::TCallJobHandlerWorkItem; - friend struct NPrivate::TModuleServerHandler; - friend struct NPrivate::TModuleClientHandler; - friend struct NPrivate::TJobRunner; - - TJobStateVec Pending; ///< messages currently outstanding via Send() - TJobStateVec Finished; ///< messages that were replied to - TBusModule* Module; - NPrivate::TBusModuleImpl* ModuleImpl; ///< module which created the job - TBusInstant SleepUntil; ///< time to wakeup, 0 if no sleep - }; - - //////////////////////////////////////////////////////////////////// - /// \brief Classes to implement basic module functionality - - class IJobFactory { - protected: - virtual ~IJobFactory() { - } - - public: - /// job factory method, override to create custom jobs - virtual TBusJob* CreateJobInstance(TBusMessage* message) = 0; + /// helper function to clear state in order to try again + void ClearState(TJobState& state); + + /// clears all message states + void ClearAllMessageStates(); + + /// returns true if job is done + bool IsDone(); + + /// return human reabable status of this job + virtual TString GetStatus(unsigned flags); + + /// set sleep time for job + void Sleep(int milliSeconds); + + void CallJobHandlerOnly(); + + private: + bool CallJobHandler(); + void DoCallReplyHandler(TJobState&); + /// send out all Pending jobs, failed sends will be migrated to Finished + bool SendPending(); + bool AnyPendingToSend(); + + public: + /// helper to call from OnReply() and OnError() + int CallReplyHandler(EMessageStatus status, TBusMessage* mess, TBusMessage* reply); + + public: + TJobHandler Handler; ///< job handler to be executed within next CallJobHandler() + EMessageStatus Status; ///< set != MESSAGE_OK if job should terminate asap + private: + NPrivate::TJobRunner* Runner; + TBusMessage* Message; + THolder<TBusMessage> MessageHolder; + TOnMessageContext OnMessageContext; // starter + public: + bool ReplySent; + + private: + friend class TBusModule; + friend struct NPrivate::TBusModuleImpl; + friend struct NPrivate::TCallJobHandlerWorkItem; + friend struct NPrivate::TModuleServerHandler; + friend struct NPrivate::TModuleClientHandler; + friend struct NPrivate::TJobRunner; + + TJobStateVec Pending; ///< messages currently outstanding via Send() + TJobStateVec Finished; ///< messages that were replied to + TBusModule* Module; + NPrivate::TBusModuleImpl* ModuleImpl; ///< module which created the job + TBusInstant SleepUntil; ///< time to wakeup, 0 if no sleep }; - struct TBusModuleConfig { - unsigned StarterMaxInFlight; + //////////////////////////////////////////////////////////////////// + /// \brief Classes to implement basic module functionality - struct TSecret { - TDuration SchedulePeriod; + class IJobFactory { + protected: + virtual ~IJobFactory() { + } - TSecret(); - }; - TSecret Secret; + public: + /// job factory method, override to create custom jobs + virtual TBusJob* CreateJobInstance(TBusMessage* message) = 0; + }; + + struct TBusModuleConfig { + unsigned StarterMaxInFlight; + + struct TSecret { + TDuration SchedulePeriod; - TBusModuleConfig(); - }; + TSecret(); + }; + TSecret Secret; - namespace NPrivate { - struct TBusModuleInternal: public TAtomicRefCount<TBusModuleInternal> { - virtual TVector<TBusClientSessionPtr> GetClientSessionsInternal() = 0; - virtual TVector<TBusServerSessionPtr> GetServerSessionsInternal() = 0; - virtual TBusMessageQueue* GetQueue() = 0; + TBusModuleConfig(); + }; + + namespace NPrivate { + struct TBusModuleInternal: public TAtomicRefCount<TBusModuleInternal> { + virtual TVector<TBusClientSessionPtr> GetClientSessionsInternal() = 0; + virtual TVector<TBusServerSessionPtr> GetServerSessionsInternal() = 0; + virtual TBusMessageQueue* GetQueue() = 0; - virtual TString GetNameInternal() = 0; + virtual TString GetNameInternal() = 0; - virtual TString GetStatusSingleLine() = 0; + virtual TString GetStatusSingleLine() = 0; - virtual ~TBusModuleInternal() { - } - }; - } + virtual ~TBusModuleInternal() { + } + }; + } - class TBusModule: public IJobFactory, TNonCopyable { - friend class TBusJob; + class TBusModule: public IJobFactory, TNonCopyable { + friend class TBusJob; - TObjectCounter<TBusModule> ObjectCounter; + TObjectCounter<TBusModule> ObjectCounter; - TIntrusivePtr<NPrivate::TBusModuleImpl> Impl; + TIntrusivePtr<NPrivate::TBusModuleImpl> Impl; - public: - /// Each module should have a name which is used as protocol service - TBusModule(const char* name); - ~TBusModule() override; + public: + /// Each module should have a name which is used as protocol service + TBusModule(const char* name); + ~TBusModule() override; - const char* GetName() const; + const char* GetName() const; - void SetConfig(const TBusModuleConfig& config); + void SetConfig(const TBusModuleConfig& config); - /// get status of all jobs in flight - TString GetStatus(unsigned flags = 0); + /// get status of all jobs in flight + TString GetStatus(unsigned flags = 0); - /// called when application is about to start - virtual bool StartInput(); - /// called when application is about to exit - virtual bool Shutdown(); + /// called when application is about to start + virtual bool StartInput(); + /// called when application is about to exit + virtual bool Shutdown(); - // this default implementation just creates TBusJob object - TBusJob* CreateJobInstance(TBusMessage* message) override; + // this default implementation just creates TBusJob object + TBusJob* CreateJobInstance(TBusMessage* message) override; - EMessageStatus StartJob(TAutoPtr<TBusMessage> message); + EMessageStatus StartJob(TAutoPtr<TBusMessage> message); - /// creates private sessions, calls CreateExtSession(), should be called before StartInput() - bool CreatePrivateSessions(TBusMessageQueue* queue); + /// creates private sessions, calls CreateExtSession(), should be called before StartInput() + bool CreatePrivateSessions(TBusMessageQueue* queue); - virtual void OnClientConnectionEvent(const TClientConnectionEvent& event); + virtual void OnClientConnectionEvent(const TClientConnectionEvent& event); - public: - /// entry point into module, first function to call - virtual TJobHandler Start(TBusJob* job, TBusMessage* mess) = 0; + public: + /// entry point into module, first function to call + virtual TJobHandler Start(TBusJob* job, TBusMessage* mess) = 0; + + protected: + /// override this function to create destination session + virtual TBusServerSessionPtr CreateExtSession(TBusMessageQueue& queue) = 0; + + public: + int GetModuleSessionInFlight() const; + + TIntrusivePtr<NPrivate::TBusModuleInternal> GetInternal(); + + protected: + TBusServerSessionPtr CreateDefaultDestination(TBusMessageQueue& queue, TBusProtocol* proto, const TBusServerSessionConfig& config, const TString& name = TString()); + TBusClientSessionPtr CreateDefaultSource(TBusMessageQueue& queue, TBusProtocol* proto, const TBusClientSessionConfig& config, const TString& name = TString()); + TBusStarter* CreateDefaultStarter(TBusMessageQueue& unused, const TBusSessionConfig& config); + }; - protected: - /// override this function to create destination session - virtual TBusServerSessionPtr CreateExtSession(TBusMessageQueue& queue) = 0; - - public: - int GetModuleSessionInFlight() const; - - TIntrusivePtr<NPrivate::TBusModuleInternal> GetInternal(); - - protected: - TBusServerSessionPtr CreateDefaultDestination(TBusMessageQueue& queue, TBusProtocol* proto, const TBusServerSessionConfig& config, const TString& name = TString()); - TBusClientSessionPtr CreateDefaultSource(TBusMessageQueue& queue, TBusProtocol* proto, const TBusClientSessionConfig& config, const TString& name = TString()); - TBusStarter* CreateDefaultStarter(TBusMessageQueue& unused, const TBusSessionConfig& config); - }; - } diff --git a/library/cpp/messagebus/oldmodule/startsession.cpp b/library/cpp/messagebus/oldmodule/startsession.cpp index 76171ba1d5..7c38801d62 100644 --- a/library/cpp/messagebus/oldmodule/startsession.cpp +++ b/library/cpp/messagebus/oldmodule/startsession.cpp @@ -1,14 +1,14 @@ /////////////////////////////////////////////////////////// -/// \file +/// \file /// \brief Starter session implementation - + /// Starter session will generate emtpy message to insert /// into local session that are registered under same protocol - + /// Starter (will one day) automatically adjust number /// of message inflight to make sure that at least one of source /// sessions within message queue is at the limit (bottle neck) - + /// Maximum number of messages that starter will instert into /// the pipeline is configured by NBus::TBusSessionConfig::MaxInFlight @@ -19,46 +19,46 @@ #include <library/cpp/messagebus/ybus.h> namespace NBus { - void* TBusStarter::_starter(void* data) { - TBusStarter* pThis = static_cast<TBusStarter*>(data); - pThis->Starter(); - return nullptr; - } + void* TBusStarter::_starter(void* data) { + TBusStarter* pThis = static_cast<TBusStarter*>(data); + pThis->Starter(); + return nullptr; + } - TBusStarter::TBusStarter(TBusModule* module, const TBusSessionConfig& config) - : Module(module) - , Config(config) - , StartThread(_starter, this) - , Exiting(false) - { - StartThread.Start(); - } + TBusStarter::TBusStarter(TBusModule* module, const TBusSessionConfig& config) + : Module(module) + , Config(config) + , StartThread(_starter, this) + , Exiting(false) + { + StartThread.Start(); + } - TBusStarter::~TBusStarter() { - Shutdown(); - } + TBusStarter::~TBusStarter() { + Shutdown(); + } - void TBusStarter::Shutdown() { - { - TGuard<TMutex> g(ExitLock); - Exiting = true; - ExitSignal.Signal(); - } - StartThread.Join(); - } + void TBusStarter::Shutdown() { + { + TGuard<TMutex> g(ExitLock); + Exiting = true; + ExitSignal.Signal(); + } + StartThread.Join(); + } - void TBusStarter::Starter() { + void TBusStarter::Starter() { TGuard<TMutex> g(ExitLock); - while (!Exiting) { - TAutoPtr<TBusMessage> empty(new TBusMessage(0)); + while (!Exiting) { + TAutoPtr<TBusMessage> empty(new TBusMessage(0)); - EMessageStatus status = Module->StartJob(empty); + EMessageStatus status = Module->StartJob(empty); - if (Config.SendTimeout > 0) { - ExitSignal.WaitT(ExitLock, TDuration::MilliSeconds(Config.SendTimeout)); - } else { - ExitSignal.WaitT(ExitLock, (status == MESSAGE_BUSY) ? TDuration::MilliSeconds(1) : TDuration::Zero()); - } + if (Config.SendTimeout > 0) { + ExitSignal.WaitT(ExitLock, TDuration::MilliSeconds(Config.SendTimeout)); + } else { + ExitSignal.WaitT(ExitLock, (status == MESSAGE_BUSY) ? TDuration::MilliSeconds(1) : TDuration::Zero()); + } } } diff --git a/library/cpp/messagebus/oldmodule/startsession.h b/library/cpp/messagebus/oldmodule/startsession.h index afe25ac809..5e26e7e1e5 100644 --- a/library/cpp/messagebus/oldmodule/startsession.h +++ b/library/cpp/messagebus/oldmodule/startsession.h @@ -5,30 +5,30 @@ #include <util/system/thread.h> namespace NBus { - class TBusModule; - - class TBusStarter { - private: - TBusModule* Module; - TBusSessionConfig Config; - TThread StartThread; - bool Exiting; - TCondVar ExitSignal; - TMutex ExitLock; - - static void* _starter(void* data); - - void Starter(); - - TString GetStatus(ui16 /*flags=YBUS_STATUS_CONNS*/) { - return ""; - } - - public: - TBusStarter(TBusModule* module, const TBusSessionConfig& config); - ~TBusStarter(); - - void Shutdown(); - }; - -} + class TBusModule; + + class TBusStarter { + private: + TBusModule* Module; + TBusSessionConfig Config; + TThread StartThread; + bool Exiting; + TCondVar ExitSignal; + TMutex ExitLock; + + static void* _starter(void* data); + + void Starter(); + + TString GetStatus(ui16 /*flags=YBUS_STATUS_CONNS*/) { + return ""; + } + + public: + TBusStarter(TBusModule* module, const TBusSessionConfig& config); + ~TBusStarter(); + + void Shutdown(); + }; + +} diff --git a/library/cpp/messagebus/protobuf/ybusbuf.cpp b/library/cpp/messagebus/protobuf/ybusbuf.cpp index 4848d2c323..63415b3737 100644 --- a/library/cpp/messagebus/protobuf/ybusbuf.cpp +++ b/library/cpp/messagebus/protobuf/ybusbuf.cpp @@ -41,10 +41,10 @@ TArrayRef<TBusBufferBase* const> TBusBufferProtocol::GetTypes() const { return Types; } -void TBusBufferProtocol::Serialize(const TBusMessage* mess, TBuffer& data) { +void TBusBufferProtocol::Serialize(const TBusMessage* mess, TBuffer& data) { TWhatThreadDoesPushPop pp("serialize protobuf message"); - const TBusHeader* header = mess->GetHeader(); + const TBusHeader* header = mess->GetHeader(); if (!IsRegisteredType(header->Type)) { Y_FAIL("unknown message type: %d", int(header->Type)); @@ -57,7 +57,7 @@ void TBusBufferProtocol::Serialize(const TBusMessage* mess, TBuffer& data) { unsigned size = bmess->GetRecord()->ByteSize(); data.Reserve(data.Size() + size); - char* after = (char*)bmess->GetRecord()->SerializeWithCachedSizesToArray((ui8*)data.Pos()); + char* after = (char*)bmess->GetRecord()->SerializeWithCachedSizesToArray((ui8*)data.Pos()); Y_VERIFY(after - data.Pos() == size); data.Advance(size); @@ -66,7 +66,7 @@ void TBusBufferProtocol::Serialize(const TBusMessage* mess, TBuffer& data) { TAutoPtr<TBusMessage> TBusBufferProtocol::Deserialize(ui16 messageType, TArrayRef<const char> payload) { TWhatThreadDoesPushPop pp("deserialize protobuf message"); - TBusBufferBase* messageTemplate = FindType(messageType); + TBusBufferBase* messageTemplate = FindType(messageType); if (messageTemplate == nullptr) { return nullptr; //Y_FAIL("unknown message type: %d", unsigned(messageType)); diff --git a/library/cpp/messagebus/protobuf/ybusbuf.h b/library/cpp/messagebus/protobuf/ybusbuf.h index b01338f986..57b4267ea5 100644 --- a/library/cpp/messagebus/protobuf/ybusbuf.h +++ b/library/cpp/messagebus/protobuf/ybusbuf.h @@ -12,222 +12,222 @@ #include <array> namespace NBus { - using TBusBufferRecord = ::google::protobuf::Message; - - template <class TBufferMessage> - class TBusBufferMessagePtr; - template <class TBufferMessage> - class TBusBufferMessageAutoPtr; - - class TBusBufferBase: public TBusMessage { - public: - TBusBufferBase(int type) - : TBusMessage((ui16)type) - { - } - TBusBufferBase(ECreateUninitialized) - : TBusMessage(MESSAGE_CREATE_UNINITIALIZED) - { - } - - ui16 GetType() const { - return GetHeader()->Type; - } - - virtual TBusBufferRecord* GetRecord() const = 0; - virtual TBusBufferBase* New() = 0; - }; - - /////////////////////////////////////////////////////////////////// - /// \brief Template for all messages that have protobuf description - - /// @param TBufferRecord is record described in .proto file with namespace - /// @param MessageFile is offset for .proto file message ids - - /// \attention If you want one protocol NBus::TBusBufferProtocol to handle - /// messageges described in different .proto files, make sure that they have - /// unique values for MessageFile - - template <class TBufferRecord, int MType> - class TBusBufferMessage: public TBusBufferBase { - public: - static const int MessageType = MType; - - typedef TBusBufferMessagePtr<TBusBufferMessage<TBufferRecord, MType>> TPtr; - typedef TBusBufferMessageAutoPtr<TBusBufferMessage<TBufferRecord, MType>> TAutoPtr; - - public: - typedef TBufferRecord RecordType; - TBufferRecord Record; - - public: - TBusBufferMessage() - : TBusBufferBase(MessageType) - { - } - TBusBufferMessage(ECreateUninitialized) - : TBusBufferBase(MESSAGE_CREATE_UNINITIALIZED) - { - } - explicit TBusBufferMessage(const TBufferRecord& record) - : TBusBufferBase(MessageType) - , Record(record) - { - } - explicit TBusBufferMessage(TBufferRecord&& record) - : TBusBufferBase(MessageType) - , Record(std::move(record)) - { - } - - public: - TBusBufferRecord* GetRecord() const override { - return (TBusBufferRecord*)&Record; - } - TBusBufferBase* New() override { - return new TBusBufferMessage<TBufferRecord, MessageType>(); - } - }; - - template <class TSelf, class TBufferMessage> - class TBusBufferMessagePtrBase { - public: - typedef typename TBufferMessage::RecordType RecordType; - - private: - TSelf* GetSelf() { - return static_cast<TSelf*>(this); - } - const TSelf* GetSelf() const { - return static_cast<const TSelf*>(this); - } - - public: - RecordType* operator->() { - Y_ASSERT(GetSelf()->Get()); - return &(GetSelf()->Get()->Record); - } - const RecordType* operator->() const { - Y_ASSERT(GetSelf()->Get()); - return &(GetSelf()->Get()->Record); - } - RecordType& operator*() { - Y_ASSERT(GetSelf()->Get()); - return GetSelf()->Get()->Record; - } - const RecordType& operator*() const { - Y_ASSERT(GetSelf()->Get()); - return GetSelf()->Get()->Record; - } - - TBusHeader* GetHeader() { - return GetSelf()->Get()->GetHeader(); - } - const TBusHeader* GetHeader() const { - return GetSelf()->Get()->GetHeader(); - } - }; - - template <class TBufferMessage> - class TBusBufferMessagePtr: public TBusBufferMessagePtrBase<TBusBufferMessagePtr<TBufferMessage>, TBufferMessage> { - protected: - TBufferMessage* Holder; - - public: - TBusBufferMessagePtr(TBufferMessage* mess) - : Holder(mess) - { - } - static TBusBufferMessagePtr<TBufferMessage> DynamicCast(TBusMessage* message) { - return dynamic_cast<TBufferMessage*>(message); - } - TBufferMessage* Get() { - return Holder; - } - const TBufferMessage* Get() const { - return Holder; - } - - operator TBufferMessage*() { - return Holder; - } - operator const TBufferMessage*() const { - return Holder; - } - - operator TAutoPtr<TBusMessage>() { - TAutoPtr<TBusMessage> r(Holder); - Holder = 0; - return r; - } - operator TBusMessageAutoPtr() { - TBusMessageAutoPtr r(Holder); - Holder = nullptr; - return r; - } - }; - - template <class TBufferMessage> - class TBusBufferMessageAutoPtr: public TBusBufferMessagePtrBase<TBusBufferMessageAutoPtr<TBufferMessage>, TBufferMessage> { - public: - TAutoPtr<TBufferMessage> AutoPtr; - - public: - TBusBufferMessageAutoPtr() { - } - TBusBufferMessageAutoPtr(TBufferMessage* message) - : AutoPtr(message) - { - } - - TBufferMessage* Get() { - return AutoPtr.Get(); - } - const TBufferMessage* Get() const { - return AutoPtr.Get(); - } - - TBufferMessage* Release() const { - return AutoPtr.Release(); - } - - operator TAutoPtr<TBusMessage>() { - return AutoPtr.Release(); - } - operator TBusMessageAutoPtr() { - return AutoPtr.Release(); - } - }; - - ///////////////////////////////////////////// - /// \brief Generic protocol object for messages descibed with protobuf - - /// \attention If you mix messages in the same protocol from more than - /// .proto file make sure that they have different MessageFile parameter - /// in the NBus::TBusBufferMessage template - - class TBusBufferProtocol: public TBusProtocol { - private: - TVector<TBusBufferBase*> Types; - std::array<ui32, ((1 << 16) >> 5)> TypeMask; - - TBusBufferBase* FindType(int type); - bool IsRegisteredType(unsigned type); - - public: - TBusBufferProtocol(TBusService name, int port); - - ~TBusBufferProtocol() override; - - /// register all the message that this protocol should handle - void RegisterType(TAutoPtr<TBusBufferBase> mess); - - TArrayRef<TBusBufferBase* const> GetTypes() const; - - /// serialized protocol specific data into TBusData - void Serialize(const TBusMessage* mess, TBuffer& data) override; - - TAutoPtr<TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) override; - }; - -} + using TBusBufferRecord = ::google::protobuf::Message; + + template <class TBufferMessage> + class TBusBufferMessagePtr; + template <class TBufferMessage> + class TBusBufferMessageAutoPtr; + + class TBusBufferBase: public TBusMessage { + public: + TBusBufferBase(int type) + : TBusMessage((ui16)type) + { + } + TBusBufferBase(ECreateUninitialized) + : TBusMessage(MESSAGE_CREATE_UNINITIALIZED) + { + } + + ui16 GetType() const { + return GetHeader()->Type; + } + + virtual TBusBufferRecord* GetRecord() const = 0; + virtual TBusBufferBase* New() = 0; + }; + + /////////////////////////////////////////////////////////////////// + /// \brief Template for all messages that have protobuf description + + /// @param TBufferRecord is record described in .proto file with namespace + /// @param MessageFile is offset for .proto file message ids + + /// \attention If you want one protocol NBus::TBusBufferProtocol to handle + /// messageges described in different .proto files, make sure that they have + /// unique values for MessageFile + + template <class TBufferRecord, int MType> + class TBusBufferMessage: public TBusBufferBase { + public: + static const int MessageType = MType; + + typedef TBusBufferMessagePtr<TBusBufferMessage<TBufferRecord, MType>> TPtr; + typedef TBusBufferMessageAutoPtr<TBusBufferMessage<TBufferRecord, MType>> TAutoPtr; + + public: + typedef TBufferRecord RecordType; + TBufferRecord Record; + + public: + TBusBufferMessage() + : TBusBufferBase(MessageType) + { + } + TBusBufferMessage(ECreateUninitialized) + : TBusBufferBase(MESSAGE_CREATE_UNINITIALIZED) + { + } + explicit TBusBufferMessage(const TBufferRecord& record) + : TBusBufferBase(MessageType) + , Record(record) + { + } + explicit TBusBufferMessage(TBufferRecord&& record) + : TBusBufferBase(MessageType) + , Record(std::move(record)) + { + } + + public: + TBusBufferRecord* GetRecord() const override { + return (TBusBufferRecord*)&Record; + } + TBusBufferBase* New() override { + return new TBusBufferMessage<TBufferRecord, MessageType>(); + } + }; + + template <class TSelf, class TBufferMessage> + class TBusBufferMessagePtrBase { + public: + typedef typename TBufferMessage::RecordType RecordType; + + private: + TSelf* GetSelf() { + return static_cast<TSelf*>(this); + } + const TSelf* GetSelf() const { + return static_cast<const TSelf*>(this); + } + + public: + RecordType* operator->() { + Y_ASSERT(GetSelf()->Get()); + return &(GetSelf()->Get()->Record); + } + const RecordType* operator->() const { + Y_ASSERT(GetSelf()->Get()); + return &(GetSelf()->Get()->Record); + } + RecordType& operator*() { + Y_ASSERT(GetSelf()->Get()); + return GetSelf()->Get()->Record; + } + const RecordType& operator*() const { + Y_ASSERT(GetSelf()->Get()); + return GetSelf()->Get()->Record; + } + + TBusHeader* GetHeader() { + return GetSelf()->Get()->GetHeader(); + } + const TBusHeader* GetHeader() const { + return GetSelf()->Get()->GetHeader(); + } + }; + + template <class TBufferMessage> + class TBusBufferMessagePtr: public TBusBufferMessagePtrBase<TBusBufferMessagePtr<TBufferMessage>, TBufferMessage> { + protected: + TBufferMessage* Holder; + + public: + TBusBufferMessagePtr(TBufferMessage* mess) + : Holder(mess) + { + } + static TBusBufferMessagePtr<TBufferMessage> DynamicCast(TBusMessage* message) { + return dynamic_cast<TBufferMessage*>(message); + } + TBufferMessage* Get() { + return Holder; + } + const TBufferMessage* Get() const { + return Holder; + } + + operator TBufferMessage*() { + return Holder; + } + operator const TBufferMessage*() const { + return Holder; + } + + operator TAutoPtr<TBusMessage>() { + TAutoPtr<TBusMessage> r(Holder); + Holder = 0; + return r; + } + operator TBusMessageAutoPtr() { + TBusMessageAutoPtr r(Holder); + Holder = nullptr; + return r; + } + }; + + template <class TBufferMessage> + class TBusBufferMessageAutoPtr: public TBusBufferMessagePtrBase<TBusBufferMessageAutoPtr<TBufferMessage>, TBufferMessage> { + public: + TAutoPtr<TBufferMessage> AutoPtr; + + public: + TBusBufferMessageAutoPtr() { + } + TBusBufferMessageAutoPtr(TBufferMessage* message) + : AutoPtr(message) + { + } + + TBufferMessage* Get() { + return AutoPtr.Get(); + } + const TBufferMessage* Get() const { + return AutoPtr.Get(); + } + + TBufferMessage* Release() const { + return AutoPtr.Release(); + } + + operator TAutoPtr<TBusMessage>() { + return AutoPtr.Release(); + } + operator TBusMessageAutoPtr() { + return AutoPtr.Release(); + } + }; + + ///////////////////////////////////////////// + /// \brief Generic protocol object for messages descibed with protobuf + + /// \attention If you mix messages in the same protocol from more than + /// .proto file make sure that they have different MessageFile parameter + /// in the NBus::TBusBufferMessage template + + class TBusBufferProtocol: public TBusProtocol { + private: + TVector<TBusBufferBase*> Types; + std::array<ui32, ((1 << 16) >> 5)> TypeMask; + + TBusBufferBase* FindType(int type); + bool IsRegisteredType(unsigned type); + + public: + TBusBufferProtocol(TBusService name, int port); + + ~TBusBufferProtocol() override; + + /// register all the message that this protocol should handle + void RegisterType(TAutoPtr<TBusBufferBase> mess); + + TArrayRef<TBusBufferBase* const> GetTypes() const; + + /// serialized protocol specific data into TBusData + void Serialize(const TBusMessage* mess, TBuffer& data) override; + + TAutoPtr<TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) override; + }; + +} diff --git a/library/cpp/messagebus/queue_config.cpp b/library/cpp/messagebus/queue_config.cpp index 6e665ce8ce..78fb52ee49 100644 --- a/library/cpp/messagebus/queue_config.cpp +++ b/library/cpp/messagebus/queue_config.cpp @@ -8,11 +8,11 @@ TBusQueueConfig::TBusQueueConfig() { } void TBusQueueConfig::ConfigureLastGetopt( - NLastGetopt::TOpts& opts, const TString& prefix) { + NLastGetopt::TOpts& opts, const TString& prefix) { opts.AddLongOption(prefix + "worker-count") - .RequiredArgument("COUNT") - .DefaultValue(ToString(NumWorkers)) - .StoreResult(&NumWorkers); + .RequiredArgument("COUNT") + .DefaultValue(ToString(NumWorkers)) + .StoreResult(&NumWorkers); } TString TBusQueueConfig::PrintToString() const { diff --git a/library/cpp/messagebus/queue_config.h b/library/cpp/messagebus/queue_config.h index 61a9395886..a9955f0c70 100644 --- a/library/cpp/messagebus/queue_config.h +++ b/library/cpp/messagebus/queue_config.h @@ -3,17 +3,17 @@ #include <library/cpp/getopt/last_getopt.h> namespace NBus { - ////////////////////////////////////////////////////////////////// - /// \brief Configuration for message queue - struct TBusQueueConfig { - TString Name; - int NumWorkers; ///< number of threads calling OnMessage(), OnReply() handlers + ////////////////////////////////////////////////////////////////// + /// \brief Configuration for message queue + struct TBusQueueConfig { + TString Name; + int NumWorkers; ///< number of threads calling OnMessage(), OnReply() handlers - TBusQueueConfig(); ///< initializes with default settings + TBusQueueConfig(); ///< initializes with default settings - void ConfigureLastGetopt(NLastGetopt::TOpts&, const TString& prefix = "mb-"); + void ConfigureLastGetopt(NLastGetopt::TOpts&, const TString& prefix = "mb-"); - TString PrintToString() const; - }; + TString PrintToString() const; + }; } diff --git a/library/cpp/messagebus/rain_check/core/coro.cpp b/library/cpp/messagebus/rain_check/core/coro.cpp index 8dda6c9f97..500841dd5b 100644 --- a/library/cpp/messagebus/rain_check/core/coro.cpp +++ b/library/cpp/messagebus/rain_check/core/coro.cpp @@ -26,12 +26,12 @@ TCoroTaskRunner::~TCoroTaskRunner() { Y_ASSERT(CoroDone); } -Y_POD_STATIC_THREAD(TContMachineContext*) -CallerContext; -Y_POD_STATIC_THREAD(TCoroTaskRunner*) -Task; +Y_POD_STATIC_THREAD(TContMachineContext*) +CallerContext; +Y_POD_STATIC_THREAD(TCoroTaskRunner*) +Task; -bool TCoroTaskRunner::ReplyReceived() { +bool TCoroTaskRunner::ReplyReceived() { Y_ASSERT(!CoroDone); TContMachineContext me; @@ -49,12 +49,12 @@ bool TCoroTaskRunner::ReplyReceived() { return !CoroDone; } -void NRainCheck::TCoroTaskRunner::DoRun() { +void NRainCheck::TCoroTaskRunner::DoRun() { GetImpl()->Run(); CoroDone = true; ContMachineContext.SwitchTo(CallerContext); } -void NRainCheck::ICoroTask::WaitForSubtasks() { +void NRainCheck::ICoroTask::WaitForSubtasks() { Task->ContMachineContext.SwitchTo(CallerContext); } diff --git a/library/cpp/messagebus/rain_check/core/coro.h b/library/cpp/messagebus/rain_check/core/coro.h index c62bef5ebc..95e2a30f9b 100644 --- a/library/cpp/messagebus/rain_check/core/coro.h +++ b/library/cpp/messagebus/rain_check/core/coro.h @@ -14,7 +14,7 @@ namespace NRainCheck { class TCoroTaskRunner: public TTaskRunnerBase, private ITrampoLine { friend class ICoroTask; - + private: NPrivate::TCoroStack Stack; TContMachineContext ContMachineContext; @@ -31,9 +31,9 @@ namespace NRainCheck { void DoRun() override /* override */; - ICoroTask* GetImpl() { - return (ICoroTask*)GetImplBase(); - } + ICoroTask* GetImpl() { + return (ICoroTask*)GetImplBase(); + } }; class ICoroTask: public ITaskBase { @@ -46,10 +46,10 @@ namespace NRainCheck { typedef TCoroTaskRunner TTaskRunner; typedef ICoroTask ITask; - ICoroTask(size_t stackSize = 0x2000) - : StackSize(stackSize) - { - } + ICoroTask(size_t stackSize = 0x2000) + : StackSize(stackSize) + { + } virtual void Run() = 0; static void WaitForSubtasks(); diff --git a/library/cpp/messagebus/rain_check/core/coro_stack.cpp b/library/cpp/messagebus/rain_check/core/coro_stack.cpp index be69939415..83b984ca6e 100644 --- a/library/cpp/messagebus/rain_check/core/coro_stack.cpp +++ b/library/cpp/messagebus/rain_check/core/coro_stack.cpp @@ -22,11 +22,11 @@ TCoroStack::TCoroStack(size_t size) *MagicNumberLocation() = MAGIC_NUMBER; #if defined(WITH_VALGRIND) - ValgrindStackId = VALGRIND_STACK_REGISTER(Data(), (char*)Data() + Size()); + ValgrindStackId = VALGRIND_STACK_REGISTER(Data(), (char*)Data() + Size()); #endif } -TCoroStack::~TCoroStack() { +TCoroStack::~TCoroStack() { #if defined(WITH_VALGRIND) VALGRIND_STACK_DEREGISTER(ValgrindStackId); #endif diff --git a/library/cpp/messagebus/rain_check/core/coro_stack.h b/library/cpp/messagebus/rain_check/core/coro_stack.h index 6d40da1ea1..2f3520e6e4 100644 --- a/library/cpp/messagebus/rain_check/core/coro_stack.h +++ b/library/cpp/messagebus/rain_check/core/coro_stack.h @@ -4,51 +4,51 @@ #include <util/generic/ptr.h> #include <util/system/valgrind.h> -namespace NRainCheck { - namespace NPrivate { - struct TCoroStack { - THolder<void, TFree> DataHolder; - size_t SizeValue; +namespace NRainCheck { + namespace NPrivate { + struct TCoroStack { + THolder<void, TFree> DataHolder; + size_t SizeValue; #if defined(WITH_VALGRIND) - size_t ValgrindStackId; + size_t ValgrindStackId; #endif - TCoroStack(size_t size); - ~TCoroStack(); + TCoroStack(size_t size); + ~TCoroStack(); - void* Data() { - return DataHolder.Get(); - } + void* Data() { + return DataHolder.Get(); + } - size_t Size() { - return SizeValue; - } + size_t Size() { + return SizeValue; + } TArrayRef<char> MemRegion() { return TArrayRef((char*)Data(), Size()); - } + } - ui32* MagicNumberLocation() { + ui32* MagicNumberLocation() { #if STACK_GROW_DOWN == 1 - return (ui32*)Data(); + return (ui32*)Data(); #elif STACK_GROW_DOWN == 0 - return ((ui32*)(((char*)Data()) + Size())) - 1; + return ((ui32*)(((char*)Data()) + Size())) - 1; #else #error "unknown" #endif - } + } - static void FailStackOverflow(); + static void FailStackOverflow(); - inline void VerifyNoStackOverflow() noexcept { - if (Y_UNLIKELY(*MagicNumberLocation() != MAGIC_NUMBER)) { - FailStackOverflow(); - } + inline void VerifyNoStackOverflow() noexcept { + if (Y_UNLIKELY(*MagicNumberLocation() != MAGIC_NUMBER)) { + FailStackOverflow(); + } } - static const ui32 MAGIC_NUMBER = 0xAB4D15FE; - }; + static const ui32 MAGIC_NUMBER = 0xAB4D15FE; + }; - } -} + } +} diff --git a/library/cpp/messagebus/rain_check/core/coro_ut.cpp b/library/cpp/messagebus/rain_check/core/coro_ut.cpp index 95edce680b..61a33584a5 100644 --- a/library/cpp/messagebus/rain_check/core/coro_ut.cpp +++ b/library/cpp/messagebus/rain_check/core/coro_ut.cpp @@ -8,7 +8,7 @@ using namespace NRainCheck; Y_UNIT_TEST_SUITE(RainCheckCoro) { - struct TSimpleCoroTask : ICoroTask { + struct TSimpleCoroTask : ICoroTask { TTestSync* const TestSync; TSimpleCoroTask(TTestEnv*, TTestSync* testSync) @@ -30,7 +30,7 @@ Y_UNIT_TEST_SUITE(RainCheckCoro) { testSync.WaitForAndIncrement(1); } - struct TSleepCoroTask : ICoroTask { + struct TSleepCoroTask : ICoroTask { TTestEnv* const Env; TTestSync* const TestSync; @@ -59,7 +59,7 @@ Y_UNIT_TEST_SUITE(RainCheckCoro) { testSync.WaitForAndIncrement(1); } - struct TSubtask : ICoroTask { + struct TSubtask : ICoroTask { TTestEnv* const Env; TTestSync* const TestSync; @@ -74,7 +74,7 @@ Y_UNIT_TEST_SUITE(RainCheckCoro) { } }; - struct TSpawnCoroTask : ICoroTask { + struct TSpawnCoroTask : ICoroTask { TTestEnv* const Env; TTestSync* const TestSync; diff --git a/library/cpp/messagebus/rain_check/core/env.h b/library/cpp/messagebus/rain_check/core/env.h index 945364c3b3..f6dd7fceb6 100644 --- a/library/cpp/messagebus/rain_check/core/env.h +++ b/library/cpp/messagebus/rain_check/core/env.h @@ -10,15 +10,15 @@ namespace NRainCheck { struct IEnv { virtual ::NActor::TExecutor* GetExecutor() = 0; - virtual ~IEnv() { - } + virtual ~IEnv() { + } }; template <typename TSelf> struct TEnvTemplate: public IEnv { template <typename TTask, typename TParam> TIntrusivePtr<typename TTask::TTaskRunner> SpawnTask(TParam param) { - return ::NRainCheck::SpawnTask<TTask, TSelf>((TSelf*)this, param); + return ::NRainCheck::SpawnTask<TTask, TSelf>((TSelf*)this, param); } }; @@ -29,19 +29,19 @@ namespace NRainCheck { TSimpleEnvTemplate(unsigned threadCount = 0) : Executor(new ::NActor::TExecutor(threadCount != 0 ? threadCount : 4)) - { - } + { + } - ::NActor::TExecutor* GetExecutor() override { - return Executor.Get(); - } + ::NActor::TExecutor* GetExecutor() override { + return Executor.Get(); + } }; struct TSimpleEnv: public TSimpleEnvTemplate<TSimpleEnv> { - TSimpleEnv(unsigned threadCount = 0) - : TSimpleEnvTemplate<TSimpleEnv>(threadCount) - { - } + TSimpleEnv(unsigned threadCount = 0) + : TSimpleEnvTemplate<TSimpleEnv>(threadCount) + { + } }; } diff --git a/library/cpp/messagebus/rain_check/core/simple.cpp b/library/cpp/messagebus/rain_check/core/simple.cpp index 46a1d0e8aa..70182b2f93 100644 --- a/library/cpp/messagebus/rain_check/core/simple.cpp +++ b/library/cpp/messagebus/rain_check/core/simple.cpp @@ -12,7 +12,7 @@ TSimpleTaskRunner::~TSimpleTaskRunner() { Y_ASSERT(!ContinueFunc); } -bool TSimpleTaskRunner::ReplyReceived() { +bool TSimpleTaskRunner::ReplyReceived() { ContinueFunc = (GetImpl()->*(ContinueFunc.Func))(); return !!ContinueFunc; } diff --git a/library/cpp/messagebus/rain_check/core/simple.h b/library/cpp/messagebus/rain_check/core/simple.h index 82664cabab..20e1bf19f5 100644 --- a/library/cpp/messagebus/rain_check/core/simple.h +++ b/library/cpp/messagebus/rain_check/core/simple.h @@ -15,17 +15,17 @@ namespace NRainCheck { public: TContinueFunc() : Func(nullptr) - { - } + { + } TContinueFunc(void*) : Func(nullptr) - { - } + { + } template <typename TTask> TContinueFunc(TContinueFunc (TTask::*func)()) - : Func((TFunc)func) + : Func((TFunc)func) { static_assert((std::is_base_of<ISimpleTask, TTask>::value), "expect (std::is_base_of<ISimpleTask, TTask>::value)"); } @@ -46,9 +46,9 @@ namespace NRainCheck { bool ReplyReceived() override /* override */; - ISimpleTask* GetImpl() { - return (ISimpleTask*)GetImplBase(); - } + ISimpleTask* GetImpl() { + return (ISimpleTask*)GetImplBase(); + } }; class ISimpleTask: public ITaskBase { diff --git a/library/cpp/messagebus/rain_check/core/simple_ut.cpp b/library/cpp/messagebus/rain_check/core/simple_ut.cpp index 820996d11a..d4545e05aa 100644 --- a/library/cpp/messagebus/rain_check/core/simple_ut.cpp +++ b/library/cpp/messagebus/rain_check/core/simple_ut.cpp @@ -16,8 +16,8 @@ Y_UNIT_TEST_SUITE(RainCheckSimple) { TTaskWithCompletionCallback(TTestEnv* env, TTestSync* testSync) : Env(env) , TestSync(testSync) - { - } + { + } TSubtaskCompletion SleepCompletion; @@ -38,7 +38,7 @@ Y_UNIT_TEST_SUITE(RainCheckSimple) { SleepCompletion.SetCompletionCallback(&TTaskWithCompletionCallback::NextSleepCompletionCallback); } - void NextSleepCompletionCallback(TSubtaskCompletion*) { + void NextSleepCompletionCallback(TSubtaskCompletion*) { TestSync->CheckAndIncrement(2); } diff --git a/library/cpp/messagebus/rain_check/core/sleep.cpp b/library/cpp/messagebus/rain_check/core/sleep.cpp index 4fb645b7d6..f5d0b4cac9 100644 --- a/library/cpp/messagebus/rain_check/core/sleep.cpp +++ b/library/cpp/messagebus/rain_check/core/sleep.cpp @@ -18,7 +18,7 @@ NRainCheck::TSleepService::TSleepService() { } -NRainCheck::TSleepService::~TSleepService() { +NRainCheck::TSleepService::~TSleepService() { if (!!SchedulerHolder) { Scheduler->Stop(); } @@ -40,7 +40,7 @@ namespace { }; } -void TSleepService::Sleep(TSubtaskCompletion* r, TDuration duration) { +void TSleepService::Sleep(TSubtaskCompletion* r, TDuration duration) { TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask(); r->SetRunning(current); Scheduler->Schedule(new TSleepServiceScheduleItem(r, TInstant::Now() + duration)); diff --git a/library/cpp/messagebus/rain_check/core/sleep.h b/library/cpp/messagebus/rain_check/core/sleep.h index dc63c098df..1a7a1f8674 100644 --- a/library/cpp/messagebus/rain_check/core/sleep.h +++ b/library/cpp/messagebus/rain_check/core/sleep.h @@ -11,7 +11,7 @@ namespace NRainCheck { private: THolder< ::NBus::NPrivate::TScheduler> SchedulerHolder; ::NBus::NPrivate::TScheduler* const Scheduler; - + public: TSleepService(::NBus::NPrivate::TScheduler*); TSleepService(); diff --git a/library/cpp/messagebus/rain_check/core/sleep_ut.cpp b/library/cpp/messagebus/rain_check/core/sleep_ut.cpp index cddcb2b44a..2ae85a87b1 100644 --- a/library/cpp/messagebus/rain_check/core/sleep_ut.cpp +++ b/library/cpp/messagebus/rain_check/core/sleep_ut.cpp @@ -15,8 +15,8 @@ Y_UNIT_TEST_SUITE(Sleep) { TTestTask(TSimpleEnv* env, TTestSync* testSync) : Env(env) , TestSync(testSync) - { - } + { + } TSubtaskCompletion Sleep; diff --git a/library/cpp/messagebus/rain_check/core/spawn.cpp b/library/cpp/messagebus/rain_check/core/spawn.cpp index 879192e26a..c570355fbe 100644 --- a/library/cpp/messagebus/rain_check/core/spawn.cpp +++ b/library/cpp/messagebus/rain_check/core/spawn.cpp @@ -1,5 +1,5 @@ #include "spawn.h" -void NRainCheck::NPrivate::SpawnTaskImpl(TTaskRunnerBase* task) { +void NRainCheck::NPrivate::SpawnTaskImpl(TTaskRunnerBase* task) { task->Schedule(); } diff --git a/library/cpp/messagebus/rain_check/core/spawn.h b/library/cpp/messagebus/rain_check/core/spawn.h index f61b8cc8c2..f2b146bf29 100644 --- a/library/cpp/messagebus/rain_check/core/spawn.h +++ b/library/cpp/messagebus/rain_check/core/spawn.h @@ -38,7 +38,7 @@ namespace NRainCheck { template <typename TTask, typename TEnv, typename TParam> TIntrusivePtr<typename TTask::TTaskRunner> SpawnTask(TEnv* env, TParam param1, ISubtaskListener* subtaskListener = &TNopSubtaskListener::Instance) { return NPrivate::SpawnTaskWithRunner< - TTask, typename TTask::ITask, typename TTask::TTaskRunner, TEnv, TParam>(env, param1, subtaskListener); + TTask, typename TTask::ITask, typename TTask::TTaskRunner, TEnv, TParam>(env, param1, subtaskListener); } // Instantiate and start subtask of given task. diff --git a/library/cpp/messagebus/rain_check/core/spawn_ut.cpp b/library/cpp/messagebus/rain_check/core/spawn_ut.cpp index a0391953e9..ba5a5e41cf 100644 --- a/library/cpp/messagebus/rain_check/core/spawn_ut.cpp +++ b/library/cpp/messagebus/rain_check/core/spawn_ut.cpp @@ -19,8 +19,8 @@ Y_UNIT_TEST_SUITE(Spawn) { TTestTask(TSimpleEnv*, TTestSync* testSync) : TestSync(testSync) , I(0) - { - } + { + } TSystemEvent Started; @@ -60,8 +60,8 @@ Y_UNIT_TEST_SUITE(Spawn) { TSubtask(TTestEnv* env, TTestSync* testSync) : Env(env) , TestSync(testSync) - { - } + { + } TContinueFunc Start() override { Sleep(TDuration::MilliSeconds(1)); @@ -77,8 +77,8 @@ Y_UNIT_TEST_SUITE(Spawn) { TSpawnTask(TTestEnv* env, TTestSync* testSync) : Env(env) , TestSync(testSync) - { - } + { + } TSubtaskCompletion SubtaskCompletion; @@ -109,12 +109,12 @@ Y_UNIT_TEST_SUITE(Spawn) { TTestSync* const TestSync; unsigned I; - TSpawnLongTask(TTestEnv* env, TTestSync* testSync) - : Env(env) - , TestSync(testSync) - , I(0) - { - } + TSpawnLongTask(TTestEnv* env, TTestSync* testSync) + : Env(env) + , TestSync(testSync) + , I(0) + { + } std::array<TSubtaskCompletion, 3> Subtasks; diff --git a/library/cpp/messagebus/rain_check/core/task.cpp b/library/cpp/messagebus/rain_check/core/task.cpp index 3c5c609d07..a098437d53 100644 --- a/library/cpp/messagebus/rain_check/core/task.cpp +++ b/library/cpp/messagebus/rain_check/core/task.cpp @@ -11,12 +11,12 @@ using namespace NRainCheck::NPrivate; using namespace NActor; namespace { - Y_POD_STATIC_THREAD(TTaskRunnerBase*) - ThreadCurrentTask; + Y_POD_STATIC_THREAD(TTaskRunnerBase*) + ThreadCurrentTask; } -void TNopSubtaskListener::SetDone() { -} +void TNopSubtaskListener::SetDone() { +} TNopSubtaskListener TNopSubtaskListener::Instance; @@ -46,12 +46,12 @@ namespace { ~TRunningInThisThreadGuard() { Y_ASSERT(ThreadCurrentTask == Task); - ThreadCurrentTask = nullptr; + ThreadCurrentTask = nullptr; } }; } -void NRainCheck::TTaskRunnerBase::Act(NActor::TDefaultTag) { +void NRainCheck::TTaskRunnerBase::Act(NActor::TDefaultTag) { Y_ASSERT(RefCount() > 0); TRunningInThisThreadGuard g(this); @@ -90,16 +90,16 @@ void NRainCheck::TTaskRunnerBase::Act(NActor::TDefaultTag) { } } -bool TTaskRunnerBase::IsRunningInThisThread() const { +bool TTaskRunnerBase::IsRunningInThisThread() const { return ThreadCurrentTask == this; } -TSubtaskCompletion::~TSubtaskCompletion() { +TSubtaskCompletion::~TSubtaskCompletion() { ESubtaskState state = State.Get(); Y_ASSERT(state == CREATED || state == DONE || state == CANCELED); } -void TSubtaskCompletion::FireCompletionCallback(ITaskBase* task) { +void TSubtaskCompletion::FireCompletionCallback(ITaskBase* task) { Y_ASSERT(IsComplete()); if (!!CompletionFunc) { @@ -111,7 +111,7 @@ void TSubtaskCompletion::FireCompletionCallback(ITaskBase* task) { } } -void NRainCheck::TSubtaskCompletion::Cancel() { +void NRainCheck::TSubtaskCompletion::Cancel() { for (;;) { ESubtaskState state = State.Get(); if (state == CREATED && State.CompareAndSet(CREATED, CANCELED)) { @@ -129,7 +129,7 @@ void NRainCheck::TSubtaskCompletion::Cancel() { } } -void TSubtaskCompletion::SetRunning(TTaskRunnerBase* parent) { +void TSubtaskCompletion::SetRunning(TTaskRunnerBase* parent) { Y_ASSERT(!TaskRunner); Y_ASSERT(!!parent); @@ -150,7 +150,7 @@ void TSubtaskCompletion::SetRunning(TTaskRunnerBase* parent) { } } -void TSubtaskCompletion::SetDone() { +void TSubtaskCompletion::SetDone() { Y_ASSERT(!!TaskRunner); TTaskRunnerBase* temp = TaskRunner; TaskRunner = nullptr; @@ -194,11 +194,11 @@ void NRainCheck::TTaskRunnerBase::ReleaseRef() } #endif -void TTaskRunnerBase::AssertInThisThread() const { +void TTaskRunnerBase::AssertInThisThread() const { Y_ASSERT(IsRunningInThisThread()); } -TTaskRunnerBase* TTaskRunnerBase::CurrentTask() { +TTaskRunnerBase* TTaskRunnerBase::CurrentTask() { Y_VERIFY(!!ThreadCurrentTask); return ThreadCurrentTask; } diff --git a/library/cpp/messagebus/rain_check/core/task.h b/library/cpp/messagebus/rain_check/core/task.h index 16a0b3cb19..7d8778bcda 100644 --- a/library/cpp/messagebus/rain_check/core/task.h +++ b/library/cpp/messagebus/rain_check/core/task.h @@ -14,8 +14,8 @@ namespace NRainCheck { struct ISubtaskListener { virtual void SetDone() = 0; - virtual ~ISubtaskListener() { - } + virtual ~ISubtaskListener() { + } }; struct TNopSubtaskListener: public ISubtaskListener { @@ -33,17 +33,17 @@ namespace NRainCheck { public: TSubtaskCompletionFunc() : Func(nullptr) - { - } + { + } TSubtaskCompletionFunc(void*) : Func(nullptr) - { - } + { + } template <typename TTask> TSubtaskCompletionFunc(void (TTask::*func)(TSubtaskCompletion*)) - : Func((TFunc)func) + : Func((TFunc)func) { static_assert((std::is_base_of<ITaskBase, TTask>::value), "expect (std::is_base_of<ITaskBase, TTask>::value)"); } @@ -56,13 +56,13 @@ namespace NRainCheck { template <typename T> class TTaskFuture; -#define SUBTASK_STATE_MAP(XX) \ - XX(CREATED, "Initial") \ - XX(RUNNING, "Running") \ - XX(DONE, "Completed") \ - XX(CANCEL_REQUESTED, "Cancel requested, but still executing") \ - XX(CANCELED, "Canceled") \ - /**/ +#define SUBTASK_STATE_MAP(XX) \ + XX(CREATED, "Initial") \ + XX(RUNNING, "Running") \ + XX(DONE, "Completed") \ + XX(CANCEL_REQUESTED, "Cancel requested, but still executing") \ + XX(CANCELED, "Canceled") \ + /**/ enum ESubtaskState { SUBTASK_STATE_MAP(ENUM_VALUE_GEN_NO_VALUE) @@ -70,38 +70,38 @@ namespace NRainCheck { ENUM_TO_STRING(ESubtaskState, SUBTASK_STATE_MAP) - class TSubtaskCompletion : TNonCopyable, public ISubtaskListener { + class TSubtaskCompletion : TNonCopyable, public ISubtaskListener { friend struct TTaskAccessor; - + private: TAtomicBox<ESubtaskState> State; TTaskRunnerBase* volatile TaskRunner; TSubtaskCompletionFunc CompletionFunc; - + public: - TSubtaskCompletion() - : State(CREATED) - , TaskRunner() - { - } + TSubtaskCompletion() + : State(CREATED) + , TaskRunner() + { + } ~TSubtaskCompletion() override; // Either done or cancel requested or cancelled bool IsComplete() const { ESubtaskState state = State.Get(); switch (state) { - case RUNNING: - return false; - case DONE: - return true; - case CANCEL_REQUESTED: - return false; - case CANCELED: - return true; - case CREATED: - Y_FAIL("not started"); - default: - Y_FAIL("unknown value: %u", (unsigned)state); + case RUNNING: + return false; + case DONE: + return true; + case CANCEL_REQUESTED: + return false; + case CANCELED: + return true; + case CREATED: + Y_FAIL("not started"); + default: + Y_FAIL("unknown value: %u", (unsigned)state); } } @@ -128,7 +128,7 @@ namespace NRainCheck { }; // See ISimpleTask, ICoroTask - class TTaskRunnerBase: public TAtomicRefCount<TTaskRunnerBase>, public NActor::TActor<TTaskRunnerBase> { + class TTaskRunnerBase: public TAtomicRefCount<TTaskRunnerBase>, public NActor::TActor<TTaskRunnerBase> { friend class NActor::TActor<TTaskRunnerBase>; friend class TContinueFunc; friend struct TTaskAccessor; @@ -163,9 +163,9 @@ namespace NRainCheck { protected: //void RetainRef(); //void ReleaseRef(); - ITaskBase* GetImplBase() { - return Impl.Get(); - } + ITaskBase* GetImplBase() { + return Impl.Get(); + } private: // true if need to call again @@ -174,11 +174,11 @@ namespace NRainCheck { class ITaskBase { public: - virtual ~ITaskBase() { - } + virtual ~ITaskBase() { + } }; // Check that current method executed inside some task. bool AreWeInsideTask(); -} +} diff --git a/library/cpp/messagebus/rain_check/core/track.cpp b/library/cpp/messagebus/rain_check/core/track.cpp index d704ee022a..092a51a214 100644 --- a/library/cpp/messagebus/rain_check/core/track.cpp +++ b/library/cpp/messagebus/rain_check/core/track.cpp @@ -3,7 +3,7 @@ using namespace NRainCheck; using namespace NRainCheck::NPrivate; -void TTaskTrackerReceipt::SetDone() { +void TTaskTrackerReceipt::SetDone() { TaskTracker->GetQueue<TTaskTrackerReceipt*>()->EnqueueAndSchedule(this); } @@ -11,12 +11,12 @@ TString TTaskTrackerReceipt::GetStatusSingleLine() { return Task->GetStatusSingleLine(); } -TTaskTracker::TTaskTracker(NActor::TExecutor* executor) - : NActor::TActor<TTaskTracker>(executor) +TTaskTracker::TTaskTracker(NActor::TExecutor* executor) + : NActor::TActor<TTaskTracker>(executor) { } -TTaskTracker::~TTaskTracker() { +TTaskTracker::~TTaskTracker() { Y_ASSERT(Tasks.Empty()); } @@ -47,7 +47,7 @@ void TTaskTracker::ProcessItem(NActor::TDefaultTag, NActor::TDefaultTag, TAsyncR status->SetResult(s); } -void TTaskTracker::Act(NActor::TDefaultTag) { +void TTaskTracker::Act(NActor::TDefaultTag) { GetQueue<TAsyncResult<TTaskTrackerStatus>*>()->DequeueAll(); GetQueue<ITaskFactory*>()->DequeueAll(); GetQueue<TTaskTrackerReceipt*>()->DequeueAll(); diff --git a/library/cpp/messagebus/rain_check/core/track.h b/library/cpp/messagebus/rain_check/core/track.h index 88a4e6fd9e..d387de7574 100644 --- a/library/cpp/messagebus/rain_check/core/track.h +++ b/library/cpp/messagebus/rain_check/core/track.h @@ -16,18 +16,18 @@ namespace NRainCheck { namespace NPrivate { struct ITaskFactory { virtual TIntrusivePtr<TTaskRunnerBase> NewTask(ISubtaskListener*) = 0; - virtual ~ITaskFactory() { - } + virtual ~ITaskFactory() { + } }; struct TTaskTrackerReceipt: public ISubtaskListener, public TIntrusiveListItem<TTaskTrackerReceipt> { TTaskTracker* const TaskTracker; TIntrusivePtr<TTaskRunnerBase> Task; - TTaskTrackerReceipt(TTaskTracker* taskTracker) - : TaskTracker(taskTracker) - { - } + TTaskTrackerReceipt(TTaskTracker* taskTracker) + : TaskTracker(taskTracker) + { + } void SetDone() override; @@ -41,11 +41,11 @@ namespace NRainCheck { } class TTaskTracker - : public TAtomicRefCount<TTaskTracker>, - public NActor::TActor<TTaskTracker>, - public NActor::TQueueInActor<TTaskTracker, NPrivate::ITaskFactory*>, - public NActor::TQueueInActor<TTaskTracker, NPrivate::TTaskTrackerReceipt*>, - public NActor::TQueueInActor<TTaskTracker, TAsyncResult<NPrivate::TTaskTrackerStatus>*> { + : public TAtomicRefCount<TTaskTracker>, + public NActor::TActor<TTaskTracker>, + public NActor::TQueueInActor<TTaskTracker, NPrivate::ITaskFactory*>, + public NActor::TQueueInActor<TTaskTracker, NPrivate::TTaskTrackerReceipt*>, + public NActor::TQueueInActor<TTaskTracker, TAsyncResult<NPrivate::TTaskTrackerStatus>*> { friend struct NPrivate::TTaskTrackerReceipt; private: @@ -77,11 +77,11 @@ namespace NRainCheck { TEnv* const Env; TParam Param; - TTaskFactory(TEnv* env, TParam param) - : Env(env) - , Param(param) - { - } + TTaskFactory(TEnv* env, TParam param) + : Env(env) + , Param(param) + { + } TIntrusivePtr<TTaskRunnerBase> NewTask(ISubtaskListener* subtaskListener) override { return NRainCheck::SpawnTask<TTask>(Env, Param, subtaskListener).Get(); diff --git a/library/cpp/messagebus/rain_check/core/track_ut.cpp b/library/cpp/messagebus/rain_check/core/track_ut.cpp index ef76a7a2af..05f7de1319 100644 --- a/library/cpp/messagebus/rain_check/core/track_ut.cpp +++ b/library/cpp/messagebus/rain_check/core/track_ut.cpp @@ -11,10 +11,10 @@ Y_UNIT_TEST_SUITE(TaskTracker) { struct TTaskForTracker: public ISimpleTask { TTestSync* const TestSync; - TTaskForTracker(TTestEnv*, TTestSync* testSync) - : TestSync(testSync) - { - } + TTaskForTracker(TTestEnv*, TTestSync* testSync) + : TestSync(testSync) + { + } TContinueFunc Start() override { TestSync->WaitForAndIncrement(0); diff --git a/library/cpp/messagebus/rain_check/http/client.cpp b/library/cpp/messagebus/rain_check/http/client.cpp index 0fb8a93420..5ef5ceeece 100644 --- a/library/cpp/messagebus/rain_check/http/client.cpp +++ b/library/cpp/messagebus/rain_check/http/client.cpp @@ -14,141 +14,141 @@ #include <util/stream/str.h> namespace NRainCheck { - class THttpCallback: public NNeh::IOnRecv { - public: - THttpCallback(NRainCheck::THttpFuture* future) - : Future(future) - { - Y_VERIFY(!!future, "future is NULL"); - } - - void OnRecv(NNeh::THandle& handle) override { - THolder<THttpCallback> self(this); - NNeh::TResponseRef response = handle.Get(); - Future->SetDoneAndSchedule(response); - } - - private: - NRainCheck::THttpFuture* const Future; - }; - - THttpFuture::THttpFuture() - : Task(nullptr) - , ErrorCode(THttpFuture::NoError) + class THttpCallback: public NNeh::IOnRecv { + public: + THttpCallback(NRainCheck::THttpFuture* future) + : Future(future) + { + Y_VERIFY(!!future, "future is NULL"); + } + + void OnRecv(NNeh::THandle& handle) override { + THolder<THttpCallback> self(this); + NNeh::TResponseRef response = handle.Get(); + Future->SetDoneAndSchedule(response); + } + + private: + NRainCheck::THttpFuture* const Future; + }; + + THttpFuture::THttpFuture() + : Task(nullptr) + , ErrorCode(THttpFuture::NoError) { } - THttpFuture::~THttpFuture() { + THttpFuture::~THttpFuture() { } - bool THttpFuture::HasError() const { - return (ErrorCode != THttpFuture::NoError); - } + bool THttpFuture::HasError() const { + return (ErrorCode != THttpFuture::NoError); + } - THttpFuture::EError THttpFuture::GetErrorCode() const { - return ErrorCode; - } + THttpFuture::EError THttpFuture::GetErrorCode() const { + return ErrorCode; + } - TString THttpFuture::GetErrorDescription() const { - return ErrorDescription; - } + TString THttpFuture::GetErrorDescription() const { + return ErrorDescription; + } - THttpClientService::THttpClientService() - : GetProtocol(NNeh::ProtocolFactory()->Protocol("http")) - , FullProtocol(NNeh::ProtocolFactory()->Protocol("full")) - { - Y_VERIFY(!!GetProtocol, "GET protocol is NULL."); - Y_VERIFY(!!FullProtocol, "POST protocol is NULL."); - } + THttpClientService::THttpClientService() + : GetProtocol(NNeh::ProtocolFactory()->Protocol("http")) + , FullProtocol(NNeh::ProtocolFactory()->Protocol("full")) + { + Y_VERIFY(!!GetProtocol, "GET protocol is NULL."); + Y_VERIFY(!!FullProtocol, "POST protocol is NULL."); + } - THttpClientService::~THttpClientService() { - } + THttpClientService::~THttpClientService() { + } - void THttpClientService::SendPost(TString addr, const TString& data, const THttpHeaders& headers, THttpFuture* future) { - Y_VERIFY(!!future, "future is NULL."); + void THttpClientService::SendPost(TString addr, const TString& data, const THttpHeaders& headers, THttpFuture* future) { + Y_VERIFY(!!future, "future is NULL."); - TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask(); - future->SetRunning(current); - future->Task = current; + TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask(); + future->SetRunning(current); + future->Task = current; - THolder<THttpCallback> callback(new THttpCallback(future)); - NNeh::TServiceStatRef stat; - try { + THolder<THttpCallback> callback(new THttpCallback(future)); + NNeh::TServiceStatRef stat; + try { NNeh::TMessage msg(addr.replace(0, NNeh::TParsedLocation(addr).Scheme.size(), "post"), data); - TStringStream headersText; - headers.OutTo(&headersText); - NNeh::NHttp::MakeFullRequest(msg, headersText.Str(), TString()); - FullProtocol->ScheduleRequest(msg, callback.Get(), stat); + TStringStream headersText; + headers.OutTo(&headersText); + NNeh::NHttp::MakeFullRequest(msg, headersText.Str(), TString()); + FullProtocol->ScheduleRequest(msg, callback.Get(), stat); Y_UNUSED(callback.Release()); - } catch (const TNetworkResolutionError& err) { - future->SetFail(THttpFuture::CantResolveNameError, err.AsStrBuf()); - } catch (const yexception& err) { - future->SetFail(THttpFuture::OtherError, err.AsStrBuf()); - } - } - - void THttpClientService::Send(const TString& request, THttpFuture* future) { - Y_VERIFY(!!future, "future is NULL."); - - TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask(); - future->SetRunning(current); - future->Task = current; - - THolder<THttpCallback> callback(new THttpCallback(future)); - NNeh::TServiceStatRef stat; - try { - GetProtocol->ScheduleRequest(NNeh::TMessage::FromString(request), - callback.Get(), - stat); + } catch (const TNetworkResolutionError& err) { + future->SetFail(THttpFuture::CantResolveNameError, err.AsStrBuf()); + } catch (const yexception& err) { + future->SetFail(THttpFuture::OtherError, err.AsStrBuf()); + } + } + + void THttpClientService::Send(const TString& request, THttpFuture* future) { + Y_VERIFY(!!future, "future is NULL."); + + TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask(); + future->SetRunning(current); + future->Task = current; + + THolder<THttpCallback> callback(new THttpCallback(future)); + NNeh::TServiceStatRef stat; + try { + GetProtocol->ScheduleRequest(NNeh::TMessage::FromString(request), + callback.Get(), + stat); Y_UNUSED(callback.Release()); - } catch (const TNetworkResolutionError& err) { - future->SetFail(THttpFuture::CantResolveNameError, err.AsStrBuf()); - } catch (const yexception& err) { - future->SetFail(THttpFuture::OtherError, err.AsStrBuf()); - } - } - - bool THttpFuture::HasHttpCode() const { - return !!HttpCode; + } catch (const TNetworkResolutionError& err) { + future->SetFail(THttpFuture::CantResolveNameError, err.AsStrBuf()); + } catch (const yexception& err) { + future->SetFail(THttpFuture::OtherError, err.AsStrBuf()); + } + } + + bool THttpFuture::HasHttpCode() const { + return !!HttpCode; + } + + bool THttpFuture::HasResponseBody() const { + return !!Response; } - bool THttpFuture::HasResponseBody() const { - return !!Response; - } + ui32 THttpFuture::GetHttpCode() const { + Y_ASSERT(IsDone()); + Y_ASSERT(HasHttpCode()); - ui32 THttpFuture::GetHttpCode() const { - Y_ASSERT(IsDone()); - Y_ASSERT(HasHttpCode()); + return static_cast<ui32>(*HttpCode); + } + + TString THttpFuture::GetResponseBody() const { + Y_ASSERT(IsDone()); + Y_ASSERT(HasResponseBody()); + + return Response->Data; + } - return static_cast<ui32>(*HttpCode); + void THttpFuture::SetDoneAndSchedule(TAutoPtr<NNeh::TResponse> response) { + if (!response->IsError()) { + ErrorCode = THttpFuture::NoError; + HttpCode = HttpCodes::HTTP_OK; + } else { + ErrorCode = THttpFuture::BadHttpCodeError; + ErrorDescription = response->GetErrorText(); + + HttpCode = TryGetHttpCodeFromErrorDescription(ErrorDescription); + } + Response.Reset(response); + SetDone(); } - TString THttpFuture::GetResponseBody() const { - Y_ASSERT(IsDone()); - Y_ASSERT(HasResponseBody()); - - return Response->Data; - } - - void THttpFuture::SetDoneAndSchedule(TAutoPtr<NNeh::TResponse> response) { - if (!response->IsError()) { - ErrorCode = THttpFuture::NoError; - HttpCode = HttpCodes::HTTP_OK; - } else { - ErrorCode = THttpFuture::BadHttpCodeError; - ErrorDescription = response->GetErrorText(); - - HttpCode = TryGetHttpCodeFromErrorDescription(ErrorDescription); - } - Response.Reset(response); - SetDone(); - } - - void THttpFuture::SetFail(THttpFuture::EError errorCode, const TStringBuf& errorDescription) { - ErrorCode = errorCode; - ErrorDescription = errorDescription; - Response.Destroy(); - SetDone(); + void THttpFuture::SetFail(THttpFuture::EError errorCode, const TStringBuf& errorDescription) { + ErrorCode = errorCode; + ErrorDescription = errorDescription; + Response.Destroy(); + SetDone(); } } diff --git a/library/cpp/messagebus/rain_check/http/client.h b/library/cpp/messagebus/rain_check/http/client.h index b7f822ae10..d4199c4c98 100644 --- a/library/cpp/messagebus/rain_check/http/client.h +++ b/library/cpp/messagebus/rain_check/http/client.h @@ -13,66 +13,66 @@ class THttpHeaders; namespace NNeh { - class IProtocol; - struct TResponse; -} + class IProtocol; + struct TResponse; +} namespace NRainCheck { - class THttpCallback; - class THttpClientService; + class THttpCallback; + class THttpClientService; - class THttpFuture: public TSubtaskCompletion { - public: - enum EError { - NoError = 0, + class THttpFuture: public TSubtaskCompletion { + public: + enum EError { + NoError = 0, - CantResolveNameError = 1, - BadHttpCodeError = 2, + CantResolveNameError = 1, + BadHttpCodeError = 2, - OtherError = 100 - }; + OtherError = 100 + }; - private: - friend class THttpCallback; - friend class THttpClientService; + private: + friend class THttpCallback; + friend class THttpClientService; - public: - THttpFuture(); - ~THttpFuture() override; + public: + THttpFuture(); + ~THttpFuture() override; - bool HasHttpCode() const; - bool HasResponseBody() const; + bool HasHttpCode() const; + bool HasResponseBody() const; - ui32 GetHttpCode() const; - TString GetResponseBody() const; + ui32 GetHttpCode() const; + TString GetResponseBody() const; - bool HasError() const; - EError GetErrorCode() const; - TString GetErrorDescription() const; + bool HasError() const; + EError GetErrorCode() const; + TString GetErrorDescription() const; - private: - void SetDoneAndSchedule(TAutoPtr<NNeh::TResponse> response); - void SetFail(EError errorCode, const TStringBuf& errorDescription); + private: + void SetDoneAndSchedule(TAutoPtr<NNeh::TResponse> response); + void SetFail(EError errorCode, const TStringBuf& errorDescription); - private: - TTaskRunnerBase* Task; - TMaybe<HttpCodes> HttpCode; - THolder<NNeh::TResponse> Response; - EError ErrorCode; - TString ErrorDescription; - }; + private: + TTaskRunnerBase* Task; + TMaybe<HttpCodes> HttpCode; + THolder<NNeh::TResponse> Response; + EError ErrorCode; + TString ErrorDescription; + }; - class THttpClientService { - public: - THttpClientService(); - virtual ~THttpClientService(); + class THttpClientService { + public: + THttpClientService(); + virtual ~THttpClientService(); - void Send(const TString& request, THttpFuture* future); - void SendPost(TString addr, const TString& data, const THttpHeaders& headers, THttpFuture* future); + void Send(const TString& request, THttpFuture* future); + void SendPost(TString addr, const TString& data, const THttpHeaders& headers, THttpFuture* future); - private: - NNeh::IProtocol* const GetProtocol; - NNeh::IProtocol* const FullProtocol; - }; + private: + NNeh::IProtocol* const GetProtocol; + NNeh::IProtocol* const FullProtocol; + }; -} +} diff --git a/library/cpp/messagebus/rain_check/http/client_ut.cpp b/library/cpp/messagebus/rain_check/http/client_ut.cpp index 51d0296a37..1628114391 100644 --- a/library/cpp/messagebus/rain_check/http/client_ut.cpp +++ b/library/cpp/messagebus/rain_check/http/client_ut.cpp @@ -28,116 +28,116 @@ using namespace NRainCheck; using namespace NBus::NTest; namespace { - class THttpClientEnv: public TTestEnvTemplate<THttpClientEnv> { - public: - THttpClientService HttpClientService; - }; - - const TString TEST_SERVICE = "test-service"; - const TString TEST_GET_PARAMS = "p=GET"; - const TString TEST_POST_PARAMS = "p=POST"; - const TString TEST_POST_HEADERS = "Content-Type: application/json\r\n"; - const TString TEST_GET_RECV = "GET was ok."; - const TString TEST_POST_RECV = "POST was ok."; - - TString BuildServiceLocation(ui32 port) { + class THttpClientEnv: public TTestEnvTemplate<THttpClientEnv> { + public: + THttpClientService HttpClientService; + }; + + const TString TEST_SERVICE = "test-service"; + const TString TEST_GET_PARAMS = "p=GET"; + const TString TEST_POST_PARAMS = "p=POST"; + const TString TEST_POST_HEADERS = "Content-Type: application/json\r\n"; + const TString TEST_GET_RECV = "GET was ok."; + const TString TEST_POST_RECV = "POST was ok."; + + TString BuildServiceLocation(ui32 port) { return Sprintf("http://*:%" PRIu32 "/%s", port, TEST_SERVICE.data()); - } + } - TString BuildPostServiceLocation(ui32 port) { + TString BuildPostServiceLocation(ui32 port) { return Sprintf("post://*:%" PRIu32 "/%s", port + 1, TEST_SERVICE.data()); - } - - TString BuildGetTestRequest(ui32 port) { - return BuildServiceLocation(port) + "?" + TEST_GET_PARAMS; - } - - class TSimpleServer { - public: - inline void ServeRequest(const NNeh::IRequestRef& req) { - NNeh::TData response; - if (req->Data() == TEST_GET_PARAMS) { - response.assign(TEST_GET_RECV.begin(), TEST_GET_RECV.end()); - } else { - response.assign(TEST_POST_RECV.begin(), TEST_POST_RECV.end()); - } - req->SendReply(response); - } - }; - - NNeh::IServicesRef RunServer(ui32 port, TSimpleServer& server) { - NNeh::IServicesRef runner = NNeh::CreateLoop(); - runner->Add(BuildServiceLocation(port), server); - runner->Add(BuildPostServiceLocation(port), server); - - try { - const int THR_POOL_SIZE = 2; - runner->ForkLoop(THR_POOL_SIZE); - } catch (...) { + } + + TString BuildGetTestRequest(ui32 port) { + return BuildServiceLocation(port) + "?" + TEST_GET_PARAMS; + } + + class TSimpleServer { + public: + inline void ServeRequest(const NNeh::IRequestRef& req) { + NNeh::TData response; + if (req->Data() == TEST_GET_PARAMS) { + response.assign(TEST_GET_RECV.begin(), TEST_GET_RECV.end()); + } else { + response.assign(TEST_POST_RECV.begin(), TEST_POST_RECV.end()); + } + req->SendReply(response); + } + }; + + NNeh::IServicesRef RunServer(ui32 port, TSimpleServer& server) { + NNeh::IServicesRef runner = NNeh::CreateLoop(); + runner->Add(BuildServiceLocation(port), server); + runner->Add(BuildPostServiceLocation(port), server); + + try { + const int THR_POOL_SIZE = 2; + runner->ForkLoop(THR_POOL_SIZE); + } catch (...) { Y_FAIL("Can't run server: %s", CurrentExceptionMessage().data()); } - return runner; + return runner; } - enum ERequestType { - RT_HTTP_GET = 0, - RT_HTTP_POST = 1 - }; - - using TTaskParam = std::pair<TIpPort, ERequestType>; - - class THttpClientTask: public ISimpleTask { - public: - THttpClientTask(THttpClientEnv* env, TTaskParam param) - : Env(env) - , ServerPort(param.first) - , ReqType(param.second) - { - } - - TContinueFunc Start() override { - switch (ReqType) { - case RT_HTTP_GET: { - TString getRequest = BuildGetTestRequest(ServerPort); - for (size_t i = 0; i < 3; ++i) { - Requests.push_back(new THttpFuture()); - Env->HttpClientService.Send(getRequest, Requests[i].Get()); - } - break; + enum ERequestType { + RT_HTTP_GET = 0, + RT_HTTP_POST = 1 + }; + + using TTaskParam = std::pair<TIpPort, ERequestType>; + + class THttpClientTask: public ISimpleTask { + public: + THttpClientTask(THttpClientEnv* env, TTaskParam param) + : Env(env) + , ServerPort(param.first) + , ReqType(param.second) + { + } + + TContinueFunc Start() override { + switch (ReqType) { + case RT_HTTP_GET: { + TString getRequest = BuildGetTestRequest(ServerPort); + for (size_t i = 0; i < 3; ++i) { + Requests.push_back(new THttpFuture()); + Env->HttpClientService.Send(getRequest, Requests[i].Get()); + } + break; } - case RT_HTTP_POST: { - TString servicePath = BuildPostServiceLocation(ServerPort); - TStringInput headersText(TEST_POST_HEADERS); - THttpHeaders headers(&headersText); - for (size_t i = 0; i < 3; ++i) { - Requests.push_back(new THttpFuture()); - Env->HttpClientService.SendPost(servicePath, TEST_POST_PARAMS, headers, Requests[i].Get()); - } - break; + case RT_HTTP_POST: { + TString servicePath = BuildPostServiceLocation(ServerPort); + TStringInput headersText(TEST_POST_HEADERS); + THttpHeaders headers(&headersText); + for (size_t i = 0; i < 3; ++i) { + Requests.push_back(new THttpFuture()); + Env->HttpClientService.SendPost(servicePath, TEST_POST_PARAMS, headers, Requests[i].Get()); + } + break; } } - - return &THttpClientTask::GotReplies; + + return &THttpClientTask::GotReplies; } - TContinueFunc GotReplies() { - const TString& TEST_OK_RECV = (ReqType == RT_HTTP_GET) ? TEST_GET_RECV : TEST_POST_RECV; - for (size_t i = 0; i < Requests.size(); ++i) { - UNIT_ASSERT_EQUAL(Requests[i]->GetHttpCode(), 200); - UNIT_ASSERT_EQUAL(Requests[i]->GetResponseBody(), TEST_OK_RECV); - } + TContinueFunc GotReplies() { + const TString& TEST_OK_RECV = (ReqType == RT_HTTP_GET) ? TEST_GET_RECV : TEST_POST_RECV; + for (size_t i = 0; i < Requests.size(); ++i) { + UNIT_ASSERT_EQUAL(Requests[i]->GetHttpCode(), 200); + UNIT_ASSERT_EQUAL(Requests[i]->GetResponseBody(), TEST_OK_RECV); + } + + Env->TestSync.CheckAndIncrement(0); - Env->TestSync.CheckAndIncrement(0); - - return nullptr; + return nullptr; } - THttpClientEnv* const Env; - const TIpPort ServerPort; - const ERequestType ReqType; + THttpClientEnv* const Env; + const TIpPort ServerPort; + const ERequestType ReqType; - TVector<TSimpleSharedPtr<THttpFuture>> Requests; - }; + TVector<TSimpleSharedPtr<THttpFuture>> Requests; + }; } // anonymous namespace @@ -175,12 +175,12 @@ Y_UNIT_TEST_SUITE(RainCheckHttpClient) { } Y_UNIT_TEST(HttpCodeExtraction) { - // Find "request failed(" string, then copy len("HTTP/1.X NNN") chars and try to convert NNN to HTTP code. + // Find "request failed(" string, then copy len("HTTP/1.X NNN") chars and try to convert NNN to HTTP code. -#define CHECK_VALID_LINE(line, code) \ - UNIT_ASSERT_NO_EXCEPTION(TryGetHttpCodeFromErrorDescription(line)); \ - UNIT_ASSERT(!!TryGetHttpCodeFromErrorDescription(line)); \ - UNIT_ASSERT_EQUAL(*TryGetHttpCodeFromErrorDescription(line), code) +#define CHECK_VALID_LINE(line, code) \ + UNIT_ASSERT_NO_EXCEPTION(TryGetHttpCodeFromErrorDescription(line)); \ + UNIT_ASSERT(!!TryGetHttpCodeFromErrorDescription(line)); \ + UNIT_ASSERT_EQUAL(*TryGetHttpCodeFromErrorDescription(line), code) CHECK_VALID_LINE(TStringBuf("library/cpp/neh/http.cpp:<LINE>: request failed(HTTP/1.0 200 Some random message"), 200); CHECK_VALID_LINE(TStringBuf("library/cpp/neh/http.cpp:<LINE>: request failed(HTTP/1.0 404 Some random message"), 404); @@ -189,9 +189,9 @@ Y_UNIT_TEST_SUITE(RainCheckHttpClient) { CHECK_VALID_LINE(TStringBuf("request failed(HTTP/1.1 2004 Some random message"), 200); #undef CHECK_VALID_LINE -#define CHECK_INVALID_LINE(line) \ - UNIT_ASSERT_NO_EXCEPTION(TryGetHttpCodeFromErrorDescription(line)); \ - UNIT_ASSERT(!TryGetHttpCodeFromErrorDescription(line)) +#define CHECK_INVALID_LINE(line) \ + UNIT_ASSERT_NO_EXCEPTION(TryGetHttpCodeFromErrorDescription(line)); \ + UNIT_ASSERT(!TryGetHttpCodeFromErrorDescription(line)) CHECK_INVALID_LINE(TStringBuf("library/cpp/neh/http.cpp:<LINE>: request failed(HTTP/1.1 1 Some random message")); CHECK_INVALID_LINE(TStringBuf("request failed(HTTP/1.0 asdf Some random message")); diff --git a/library/cpp/messagebus/rain_check/http/http_code_extractor.cpp b/library/cpp/messagebus/rain_check/http/http_code_extractor.cpp index b0925c5a6b..51d75762f6 100644 --- a/library/cpp/messagebus/rain_check/http/http_code_extractor.cpp +++ b/library/cpp/messagebus/rain_check/http/http_code_extractor.cpp @@ -8,32 +8,32 @@ #include <util/string/cast.h> namespace NRainCheck { - TMaybe<HttpCodes> TryGetHttpCodeFromErrorDescription(const TStringBuf& errorMessage) { + TMaybe<HttpCodes> TryGetHttpCodeFromErrorDescription(const TStringBuf& errorMessage) { // Try to get HttpCode from library/cpp/neh response. // If response has HttpCode and it is not 200 OK, library/cpp/neh will send a message // "library/cpp/neh/http.cpp:<LINE>: request failed(<FIRST-HTTP-RESPONSE-LINE>)" // (see library/cpp/neh/http.cpp:625). So, we will try to parse this message and - // find out HttpCode in it. It is bad temporary solution, but we have no choice. + // find out HttpCode in it. It is bad temporary solution, but we have no choice. const TStringBuf SUBSTR = "request failed("; - const size_t SUBSTR_LEN = SUBSTR.size(); + const size_t SUBSTR_LEN = SUBSTR.size(); const size_t FIRST_LINE_LEN = TStringBuf("HTTP/1.X NNN").size(); - TMaybe<HttpCodes> httpCode; + TMaybe<HttpCodes> httpCode; - const size_t substrPos = errorMessage.find(SUBSTR); - if (substrPos != TStringBuf::npos) { - const TStringBuf firstLineStart = errorMessage.SubStr(substrPos + SUBSTR_LEN, FIRST_LINE_LEN); - try { - httpCode = static_cast<HttpCodes>(ParseHttpRetCode(firstLineStart)); - if (*httpCode < HTTP_CONTINUE || *httpCode >= HTTP_CODE_MAX) { - httpCode = Nothing(); - } - } catch (const TFromStringException& ex) { - // Can't parse HttpCode: it is OK, because ErrorDescription can be random string. + const size_t substrPos = errorMessage.find(SUBSTR); + if (substrPos != TStringBuf::npos) { + const TStringBuf firstLineStart = errorMessage.SubStr(substrPos + SUBSTR_LEN, FIRST_LINE_LEN); + try { + httpCode = static_cast<HttpCodes>(ParseHttpRetCode(firstLineStart)); + if (*httpCode < HTTP_CONTINUE || *httpCode >= HTTP_CODE_MAX) { + httpCode = Nothing(); + } + } catch (const TFromStringException& ex) { + // Can't parse HttpCode: it is OK, because ErrorDescription can be random string. } } - - return httpCode; + + return httpCode; } } diff --git a/library/cpp/messagebus/rain_check/http/http_code_extractor.h b/library/cpp/messagebus/rain_check/http/http_code_extractor.h index 322fe24fa0..33b565fa1c 100644 --- a/library/cpp/messagebus/rain_check/http/http_code_extractor.h +++ b/library/cpp/messagebus/rain_check/http/http_code_extractor.h @@ -10,7 +10,7 @@ namespace NRainCheck { // If response has HttpCode and it is not 200 OK, library/cpp/neh will send a message // "library/cpp/neh/http.cpp:<LINE>: request failed(<FIRST-HTTP-RESPONSE-LINE>)" // (see library/cpp/neh/http.cpp:625). So, we will try to parse this message and - // find out HttpCode in it. It is bad temporary solution, but we have no choice. - TMaybe<HttpCodes> TryGetHttpCodeFromErrorDescription(const TStringBuf& errorMessage); + // find out HttpCode in it. It is bad temporary solution, but we have no choice. + TMaybe<HttpCodes> TryGetHttpCodeFromErrorDescription(const TStringBuf& errorMessage); -} +} diff --git a/library/cpp/messagebus/rain_check/messagebus/messagebus_client.cpp b/library/cpp/messagebus/rain_check/messagebus/messagebus_client.cpp index d5d20ab2d6..daac8d9a99 100644 --- a/library/cpp/messagebus/rain_check/messagebus/messagebus_client.cpp +++ b/library/cpp/messagebus/rain_check/messagebus/messagebus_client.cpp @@ -4,17 +4,17 @@ using namespace NRainCheck; using namespace NBus; TBusClientService::TBusClientService( - const NBus::TBusSessionConfig& config, - NBus::TBusProtocol* proto, - NBus::TBusMessageQueue* queue) { + const NBus::TBusSessionConfig& config, + NBus::TBusProtocol* proto, + NBus::TBusMessageQueue* queue) { Session = queue->CreateSource(proto, this, config); } -TBusClientService::~TBusClientService() { +TBusClientService::~TBusClientService() { Session->Shutdown(); } -void TBusClientService::SendCommon(NBus::TBusMessage* message, const NBus::TNetAddr&, TBusFuture* future) { +void TBusClientService::SendCommon(NBus::TBusMessage* message, const NBus::TNetAddr&, TBusFuture* future) { TTaskRunnerBase* current = TTaskRunnerBase::CurrentTask(); future->SetRunning(current); @@ -29,8 +29,8 @@ void TBusClientService::SendCommon(NBus::TBusMessage* message, const NBus::TNetA } void TBusClientService::ProcessResultCommon(NBus::TBusMessageAutoPtr message, - const NBus::TNetAddr&, TBusFuture* future, - NBus::EMessageStatus status) { + const NBus::TNetAddr&, TBusFuture* future, + NBus::EMessageStatus status) { Y_UNUSED(message.Release()); if (status == NBus::MESSAGE_OK) { @@ -41,8 +41,8 @@ void TBusClientService::ProcessResultCommon(NBus::TBusMessageAutoPtr message, } void TBusClientService::SendOneWay( - NBus::TBusMessageAutoPtr message, const NBus::TNetAddr& addr, - TBusFuture* future) { + NBus::TBusMessageAutoPtr message, const NBus::TNetAddr& addr, + TBusFuture* future) { SendCommon(message.Get(), addr, future); EMessageStatus ok = Session->SendMessageOneWay(message.Get(), &addr, false); @@ -54,8 +54,8 @@ NBus::TBusClientSessionPtr TBusClientService::GetSessionForMonitoring() const { } void TBusClientService::Send( - TBusMessageAutoPtr message, const TNetAddr& addr, - TBusFuture* future) { + TBusMessageAutoPtr message, const TNetAddr& addr, + TBusFuture* future) { SendCommon(message.Get(), addr, future); EMessageStatus ok = Session->SendMessage(message.Get(), &addr, false); @@ -63,35 +63,35 @@ void TBusClientService::Send( } void TBusClientService::OnReply( - TAutoPtr<TBusMessage> request, - TAutoPtr<TBusMessage> response) { - TBusFuture* future = (TBusFuture*)request->Data; + TAutoPtr<TBusMessage> request, + TAutoPtr<TBusMessage> response) { + TBusFuture* future = (TBusFuture*)request->Data; Y_ASSERT(future->Request.Get() == request.Get()); Y_UNUSED(request.Release()); future->SetDoneAndSchedule(MESSAGE_OK, response); } void NRainCheck::TBusClientService::OnMessageSentOneWay( - TAutoPtr<NBus::TBusMessage> request) { - TBusFuture* future = (TBusFuture*)request->Data; + TAutoPtr<NBus::TBusMessage> request) { + TBusFuture* future = (TBusFuture*)request->Data; Y_ASSERT(future->Request.Get() == request.Get()); Y_UNUSED(request.Release()); future->SetDoneAndSchedule(MESSAGE_OK, nullptr); } void TBusClientService::OnError( - TAutoPtr<TBusMessage> message, NBus::EMessageStatus status) { + TAutoPtr<TBusMessage> message, NBus::EMessageStatus status) { if (message->Data == nullptr) { return; } - TBusFuture* future = (TBusFuture*)message->Data; + TBusFuture* future = (TBusFuture*)message->Data; Y_ASSERT(future->Request.Get() == message.Get()); Y_UNUSED(message.Release()); future->SetDoneAndSchedule(status, nullptr); } -void TBusFuture::SetDoneAndSchedule(EMessageStatus status, TAutoPtr<TBusMessage> response) { +void TBusFuture::SetDoneAndSchedule(EMessageStatus status, TAutoPtr<TBusMessage> response) { Status = status; Response.Reset(response.Release()); SetDone(); diff --git a/library/cpp/messagebus/rain_check/messagebus/messagebus_client.h b/library/cpp/messagebus/rain_check/messagebus/messagebus_client.h index b47ec13408..0a291cdea6 100644 --- a/library/cpp/messagebus/rain_check/messagebus/messagebus_client.h +++ b/library/cpp/messagebus/rain_check/messagebus/messagebus_client.h @@ -7,24 +7,24 @@ namespace NRainCheck { class TBusFuture: public TSubtaskCompletion { friend class TBusClientService; - + private: THolder<NBus::TBusMessage> Request; THolder<NBus::TBusMessage> Response; NBus::EMessageStatus Status; - + private: TTaskRunnerBase* Task; void SetDoneAndSchedule(NBus::EMessageStatus, TAutoPtr<NBus::TBusMessage>); - + public: // TODO: add MESSAGE_UNDEFINED - TBusFuture() - : Status(NBus::MESSAGE_DONT_ASK) - , Task(nullptr) - { - } + TBusFuture() + : Status(NBus::MESSAGE_DONT_ASK) + , Task(nullptr) + { + } NBus::TBusMessage* GetRequest() const { return Request.Get(); @@ -44,7 +44,7 @@ namespace NRainCheck { class TBusClientService: private NBus::IBusClientHandler { private: NBus::TBusClientSessionPtr Session; - + public: TBusClientService(const NBus::TBusSessionConfig&, NBus::TBusProtocol*, NBus::TBusMessageQueue*); ~TBusClientService() override; diff --git a/library/cpp/messagebus/rain_check/messagebus/messagebus_client_ut.cpp b/library/cpp/messagebus/rain_check/messagebus/messagebus_client_ut.cpp index e63ba86be7..1b3618558b 100644 --- a/library/cpp/messagebus/rain_check/messagebus/messagebus_client_ut.cpp +++ b/library/cpp/messagebus/rain_check/messagebus/messagebus_client_ut.cpp @@ -28,8 +28,8 @@ struct TMessageBusClientEnv: public TTestEnvTemplate<TMessageBusClientEnv> { TMessageBusClientEnv() : Queue(CreateMessageQueue(GetExecutor())) , BusClientService(TBusSessionConfig(), &Proto, Queue.Get()) - { - } + { + } }; Y_UNIT_TEST_SUITE(RainCheckMessageBusClient) { @@ -44,7 +44,7 @@ Y_UNIT_TEST_SUITE(RainCheckMessageBusClient) { { } - TVector<TSimpleSharedPtr<TBusFuture>> Requests; + TVector<TSimpleSharedPtr<TBusFuture>> Requests; TContinueFunc Start() override { for (unsigned i = 0; i < 3; ++i) { @@ -108,7 +108,7 @@ Y_UNIT_TEST_SUITE(RainCheckMessageBusClient) { { } - TVector<TSimpleSharedPtr<TBusFuture>> Requests; + TVector<TSimpleSharedPtr<TBusFuture>> Requests; TContinueFunc Start() override { Env->TestSync.CheckAndIncrement(0); diff --git a/library/cpp/messagebus/rain_check/messagebus/messagebus_server.cpp b/library/cpp/messagebus/rain_check/messagebus/messagebus_server.cpp index 1868cfa06e..5d4b13d664 100644 --- a/library/cpp/messagebus/rain_check/messagebus/messagebus_server.cpp +++ b/library/cpp/messagebus/rain_check/messagebus/messagebus_server.cpp @@ -9,9 +9,9 @@ TBusTaskStarter::TBusTaskStarter(TAutoPtr<ITaskFactory> taskFactory) { } -void TBusTaskStarter::OnMessage(NBus::TOnMessageContext& onMessage) { +void TBusTaskStarter::OnMessage(NBus::TOnMessageContext& onMessage) { TaskFactory->NewTask(onMessage); } -TBusTaskStarter::~TBusTaskStarter() { +TBusTaskStarter::~TBusTaskStarter() { } diff --git a/library/cpp/messagebus/rain_check/messagebus/messagebus_server.h b/library/cpp/messagebus/rain_check/messagebus/messagebus_server.h index 10fc8b0dc7..1334f05fe4 100644 --- a/library/cpp/messagebus/rain_check/messagebus/messagebus_server.h +++ b/library/cpp/messagebus/rain_check/messagebus/messagebus_server.h @@ -12,14 +12,14 @@ namespace NRainCheck { private: struct ITaskFactory { virtual void NewTask(NBus::TOnMessageContext&) = 0; - virtual ~ITaskFactory() { - } + virtual ~ITaskFactory() { + } }; THolder<ITaskFactory> TaskFactory; void OnMessage(NBus::TOnMessageContext&) override; - + public: TBusTaskStarter(TAutoPtr<ITaskFactory>); ~TBusTaskStarter() override; @@ -30,10 +30,10 @@ namespace NRainCheck { struct TTaskFactory: public ITaskFactory { TEnv* const Env; - TTaskFactory(TEnv* env) - : Env(env) - { - } + TTaskFactory(TEnv* env) + : Env(env) + { + } void NewTask(NBus::TOnMessageContext& context) override { SpawnTask<TTask, TEnv, NBus::TOnMessageContext&>(Env, context); diff --git a/library/cpp/messagebus/rain_check/messagebus/messagebus_server_ut.cpp b/library/cpp/messagebus/rain_check/messagebus/messagebus_server_ut.cpp index 9b3bd58ad7..7c11399f1b 100644 --- a/library/cpp/messagebus/rain_check/messagebus/messagebus_server_ut.cpp +++ b/library/cpp/messagebus/rain_check/messagebus/messagebus_server_ut.cpp @@ -19,7 +19,7 @@ Y_UNIT_TEST_SUITE(RainCheckMessageBusServer) { private: TMessageBusServerEnv* const Env; TOnMessageContext MessageContext; - + public: TSimpleServerTask(TMessageBusServerEnv* env, TOnMessageContext& messageContext) : Env(env) diff --git a/library/cpp/messagebus/rain_check/test/helper/misc.cpp b/library/cpp/messagebus/rain_check/test/helper/misc.cpp index b176878cab..c0fcb27252 100644 --- a/library/cpp/messagebus/rain_check/test/helper/misc.cpp +++ b/library/cpp/messagebus/rain_check/test/helper/misc.cpp @@ -4,7 +4,7 @@ using namespace NRainCheck; -void TSpawnNopTasksCoroTask::Run() { +void TSpawnNopTasksCoroTask::Run() { Y_VERIFY(Count <= Completion.size()); for (unsigned i = 0; i < Count; ++i) { SpawnSubtask<TNopCoroTask>(Env, &Completion[i], ""); @@ -13,7 +13,7 @@ void TSpawnNopTasksCoroTask::Run() { WaitForSubtasks(); } -TContinueFunc TSpawnNopTasksSimpleTask::Start() { +TContinueFunc TSpawnNopTasksSimpleTask::Start() { Y_VERIFY(Count <= Completion.size()); for (unsigned i = 0; i < Count; ++i) { SpawnSubtask<TNopSimpleTask>(Env, &Completion[i], ""); @@ -22,6 +22,6 @@ TContinueFunc TSpawnNopTasksSimpleTask::Start() { return &TSpawnNopTasksSimpleTask::Join; } -TContinueFunc TSpawnNopTasksSimpleTask::Join() { +TContinueFunc TSpawnNopTasksSimpleTask::Join() { return nullptr; } diff --git a/library/cpp/messagebus/rain_check/test/helper/misc.h b/library/cpp/messagebus/rain_check/test/helper/misc.h index 5d8648462d..9150be4d2f 100644 --- a/library/cpp/messagebus/rain_check/test/helper/misc.h +++ b/library/cpp/messagebus/rain_check/test/helper/misc.h @@ -6,8 +6,8 @@ namespace NRainCheck { struct TNopSimpleTask: public ISimpleTask { - TNopSimpleTask(IEnv*, const void*) { - } + TNopSimpleTask(IEnv*, const void*) { + } TContinueFunc Start() override { return nullptr; @@ -15,11 +15,11 @@ namespace NRainCheck { }; struct TNopCoroTask: public ICoroTask { - TNopCoroTask(IEnv*, const void*) { - } + TNopCoroTask(IEnv*, const void*) { + } - void Run() override { - } + void Run() override { + } }; struct TSpawnNopTasksCoroTask: public ICoroTask { @@ -29,8 +29,8 @@ namespace NRainCheck { TSpawnNopTasksCoroTask(IEnv* env, unsigned count) : Env(env) , Count(count) - { - } + { + } std::array<TSubtaskCompletion, 2> Completion; @@ -44,8 +44,8 @@ namespace NRainCheck { TSpawnNopTasksSimpleTask(IEnv* env, unsigned count) : Env(env) , Count(count) - { - } + { + } std::array<TSubtaskCompletion, 2> Completion; diff --git a/library/cpp/messagebus/rain_check/test/perftest/perftest.cpp b/library/cpp/messagebus/rain_check/test/perftest/perftest.cpp index c71499e84a..22edbd8c6b 100644 --- a/library/cpp/messagebus/rain_check/test/perftest/perftest.cpp +++ b/library/cpp/messagebus/rain_check/test/perftest/perftest.cpp @@ -16,17 +16,17 @@ struct TRainCheckPerftestEnv: public TSimpleEnvTemplate<TRainCheckPerftestEnv> { TRainCheckPerftestEnv() : TSimpleEnvTemplate<TRainCheckPerftestEnv>(4) , SubtasksPerTask(1000) - { - } + { + } }; struct TCoroOuter: public ICoroTask { TRainCheckPerftestEnv* const Env; - TCoroOuter(TRainCheckPerftestEnv* env) - : Env(env) - { - } + TCoroOuter(TRainCheckPerftestEnv* env) + : Env(env) + { + } void Run() override { for (;;) { @@ -63,10 +63,10 @@ struct TCoroOuter: public ICoroTask { struct TSimpleOuter: public ISimpleTask { TRainCheckPerftestEnv* const Env; - TSimpleOuter(TRainCheckPerftestEnv* env, const void*) - : Env(env) - { - } + TSimpleOuter(TRainCheckPerftestEnv* env, const void*) + : Env(env) + { + } TInstant StartInstant; unsigned Count; @@ -119,10 +119,10 @@ struct TSimpleOuter: public ISimpleTask { struct TReproduceCrashTask: public ISimpleTask { TRainCheckPerftestEnv* const Env; - TReproduceCrashTask(TRainCheckPerftestEnv* env) - : Env(env) - { - } + TReproduceCrashTask(TRainCheckPerftestEnv* env) + : Env(env) + { + } std::array<TSubtaskCompletion, SUBTASKS> Completion; diff --git a/library/cpp/messagebus/ref_counted.h b/library/cpp/messagebus/ref_counted.h index c1c4b95c52..29b87764e3 100644 --- a/library/cpp/messagebus/ref_counted.h +++ b/library/cpp/messagebus/ref_counted.h @@ -1,6 +1,6 @@ #pragma once -class TAtomicRefCountedObject: public TAtomicRefCount<TAtomicRefCountedObject> { - virtual ~TAtomicRefCountedObject() { - } +class TAtomicRefCountedObject: public TAtomicRefCount<TAtomicRefCountedObject> { + virtual ~TAtomicRefCountedObject() { + } }; diff --git a/library/cpp/messagebus/remote_client_connection.cpp b/library/cpp/messagebus/remote_client_connection.cpp index 5006869683..8c7a6db3a8 100644 --- a/library/cpp/messagebus/remote_client_connection.cpp +++ b/library/cpp/messagebus/remote_client_connection.cpp @@ -33,7 +33,7 @@ TBusMessage* TRemoteClientConnection::PopAck(TBusKey id) { return AckMessages.Pop(id); } -SOCKET TRemoteClientConnection::CreateSocket(const TNetAddr& addr) { +SOCKET TRemoteClientConnection::CreateSocket(const TNetAddr& addr) { SOCKET handle = socket(addr.Addr()->sa_family, SOCK_STREAM, 0); Y_VERIFY(handle != INVALID_SOCKET, "failed to create socket: %s", LastSystemErrorText()); @@ -138,33 +138,33 @@ void TRemoteClientConnection::BeforeTryWrite() { TimeoutMessages(); } -namespace NBus { - namespace NPrivate { - class TInvokeOnReply: public IWorkItem { - private: - TRemoteClientSession* RemoteClientSession; - TNonDestroyingHolder<TBusMessage> Request; - TBusMessagePtrAndHeader Response; - - public: - TInvokeOnReply(TRemoteClientSession* session, - TNonDestroyingAutoPtr<TBusMessage> request, TBusMessagePtrAndHeader& response) - : RemoteClientSession(session) - , Request(request) - { - Response.Swap(response); - } - - void DoWork() override { - THolder<TInvokeOnReply> holder(this); - RemoteClientSession->ReleaseInFlightAndCallOnReply(Request.Release(), Response); - // TODO: TRemoteClientSessionSemaphore should be enough - RemoteClientSession->JobCount.Decrement(); - } - }; - - } -} +namespace NBus { + namespace NPrivate { + class TInvokeOnReply: public IWorkItem { + private: + TRemoteClientSession* RemoteClientSession; + TNonDestroyingHolder<TBusMessage> Request; + TBusMessagePtrAndHeader Response; + + public: + TInvokeOnReply(TRemoteClientSession* session, + TNonDestroyingAutoPtr<TBusMessage> request, TBusMessagePtrAndHeader& response) + : RemoteClientSession(session) + , Request(request) + { + Response.Swap(response); + } + + void DoWork() override { + THolder<TInvokeOnReply> holder(this); + RemoteClientSession->ReleaseInFlightAndCallOnReply(Request.Release(), Response); + // TODO: TRemoteClientSessionSemaphore should be enough + RemoteClientSession->JobCount.Decrement(); + } + }; + + } +} void TRemoteClientConnection::ProcessReplyQueue() { if (AtomicGet(WriterData.Down)) { diff --git a/library/cpp/messagebus/remote_client_connection.h b/library/cpp/messagebus/remote_client_connection.h index 95061f1f36..fe80b7d2f9 100644 --- a/library/cpp/messagebus/remote_client_connection.h +++ b/library/cpp/messagebus/remote_client_connection.h @@ -7,59 +7,59 @@ #include <util/generic/object_counter.h> -namespace NBus { - namespace NPrivate { - class TRemoteClientConnection: public TRemoteConnection, public TBusClientConnection { - friend class TRemoteConnection; - friend struct TBusSessionImpl; - friend class TRemoteClientSession; +namespace NBus { + namespace NPrivate { + class TRemoteClientConnection: public TRemoteConnection, public TBusClientConnection { + friend class TRemoteConnection; + friend struct TBusSessionImpl; + friend class TRemoteClientSession; - private: - TObjectCounter<TRemoteClientConnection> ObjectCounter; + private: + TObjectCounter<TRemoteClientConnection> ObjectCounter; - TSyncAckMessages AckMessages; + TSyncAckMessages AckMessages; - TLocalTasks TimeToTimeoutMessages; + TLocalTasks TimeToTimeoutMessages; - IBusClientHandler* const ClientHandler; + IBusClientHandler* const ClientHandler; - public: - TRemoteClientConnection(TRemoteClientSessionPtr session, ui64 id, TNetAddr addr); + public: + TRemoteClientConnection(TRemoteClientSessionPtr session, ui64 id, TNetAddr addr); - inline TRemoteClientSession* GetSession(); + inline TRemoteClientSession* GetSession(); - SOCKET CreateSocket(const TNetAddr& addr); + SOCKET CreateSocket(const TNetAddr& addr); - void TryConnect() override; + void TryConnect() override; - void HandleEvent(SOCKET socket, void* cookie) override; + void HandleEvent(SOCKET socket, void* cookie) override; - TBusMessage* PopAck(TBusKey id); + TBusMessage* PopAck(TBusKey id); - void WriterFillStatus() override; + void WriterFillStatus() override; - void ClearOutgoingQueue(TMessagesPtrs& result, bool reconnect) override; + void ClearOutgoingQueue(TMessagesPtrs& result, bool reconnect) override; - void BeforeTryWrite() override; + void BeforeTryWrite() override; - void ProcessReplyQueue(); + void ProcessReplyQueue(); - void MessageSent(TArrayRef<TBusMessagePtrAndHeader> messages) override; + void MessageSent(TArrayRef<TBusMessagePtrAndHeader> messages) override; - void TimeoutMessages(); + void TimeoutMessages(); - void ScheduleTimeoutMessages(); + void ScheduleTimeoutMessages(); - void ReaderProcessMessageUnknownVersion(TArrayRef<const char> dataRef) override; + void ReaderProcessMessageUnknownVersion(TArrayRef<const char> dataRef) override; - EMessageStatus SendMessage(TBusMessage* pMes, bool wait) override; + EMessageStatus SendMessage(TBusMessage* pMes, bool wait) override; - EMessageStatus SendMessageOneWay(TBusMessage* pMes, bool wait) override; + EMessageStatus SendMessageOneWay(TBusMessage* pMes, bool wait) override; - EMessageStatus SendMessageImpl(TBusMessage*, bool wait, bool oneWay); + EMessageStatus SendMessageImpl(TBusMessage*, bool wait, bool oneWay); - void OpenConnection() override; - }; + void OpenConnection() override; + }; - } -} + } +} diff --git a/library/cpp/messagebus/remote_client_session.cpp b/library/cpp/messagebus/remote_client_session.cpp index 77051f1099..3bc421944f 100644 --- a/library/cpp/messagebus/remote_client_session.cpp +++ b/library/cpp/messagebus/remote_client_session.cpp @@ -14,8 +14,8 @@ using namespace NBus; using namespace NBus::NPrivate; TRemoteClientSession::TRemoteClientSession(TBusMessageQueue* queue, - TBusProtocol* proto, IBusClientHandler* handler, - const TBusClientSessionConfig& config, const TString& name) + TBusProtocol* proto, IBusClientHandler* handler, + const TBusClientSessionConfig& config, const TString& name) : TBusSessionImpl(true, queue, proto, handler, config, name) , ClientRemoteInFlight(config.MaxInFlight, "ClientRemoteInFlight") , ClientHandler(handler) @@ -27,7 +27,7 @@ TRemoteClientSession::~TRemoteClientSession() { } void TRemoteClientSession::OnMessageReceived(TRemoteConnection* c, TVectorSwaps<TBusMessagePtrAndHeader>& newMsg) { - TAutoPtr<TVectorSwaps<TBusMessagePtrAndHeader>> temp(new TVectorSwaps<TBusMessagePtrAndHeader>); + TAutoPtr<TVectorSwaps<TBusMessagePtrAndHeader>> temp(new TVectorSwaps<TBusMessagePtrAndHeader>); temp->swap(newMsg); c->ReplyQueue.EnqueueAll(temp); c->ScheduleWrite(); @@ -46,7 +46,7 @@ EMessageStatus TRemoteClientSession::SendMessageImpl(TBusMessage* msg, const TNe msg->ReplyTo = resolvedAddr; - TRemoteConnectionPtr c = ((TBusSessionImpl*)this)->GetConnection(resolvedAddr, true); + TRemoteConnectionPtr c = ((TBusSessionImpl*)this)->GetConnection(resolvedAddr, true); Y_ASSERT(!!c); return CheckedCast<TRemoteClientConnection*>(c.Get())->SendMessageImpl(msg, wait, oneWay); @@ -103,7 +103,7 @@ void TRemoteClientSession::ReleaseInFlightAndCallOnReply(TNonDestroyingAutoPtr<T } } -EMessageStatus TRemoteClientSession::GetMessageDestination(TBusMessage* mess, const TNetAddr* addrp, TBusSocketAddr* dest) { +EMessageStatus TRemoteClientSession::GetMessageDestination(TBusMessage* mess, const TNetAddr* addrp, TBusSocketAddr* dest) { if (addrp) { *dest = *addrp; } else { @@ -123,5 +123,5 @@ void TRemoteClientSession::OpenConnection(const TNetAddr& addr) { TBusClientConnectionPtr TRemoteClientSession::GetConnection(const TNetAddr& addr) { // TODO: GetConnection should not open - return CheckedCast<TRemoteClientConnection*>(((TBusSessionImpl*)this)->GetConnection(addr, true).Get()); + return CheckedCast<TRemoteClientConnection*>(((TBusSessionImpl*)this)->GetConnection(addr, true).Get()); } diff --git a/library/cpp/messagebus/remote_client_session.h b/library/cpp/messagebus/remote_client_session.h index 7378fcb0bd..7160d0dae9 100644 --- a/library/cpp/messagebus/remote_client_session.h +++ b/library/cpp/messagebus/remote_client_session.h @@ -8,52 +8,52 @@ #ifdef _MSC_VER #pragma warning(push) -#pragma warning(disable : 4250) // 'NBus::NPrivate::TRemoteClientSession' : inherits 'NBus::NPrivate::TBusSessionImpl::NBus::NPrivate::TBusSessionImpl::GetConfig' via dominance +#pragma warning(disable : 4250) // 'NBus::NPrivate::TRemoteClientSession' : inherits 'NBus::NPrivate::TBusSessionImpl::NBus::NPrivate::TBusSessionImpl::GetConfig' via dominance #endif -namespace NBus { - namespace NPrivate { - using TRemoteClientSessionPtr = TIntrusivePtr<TRemoteClientSession>; +namespace NBus { + namespace NPrivate { + using TRemoteClientSessionPtr = TIntrusivePtr<TRemoteClientSession>; - class TRemoteClientSession: public TBusClientSession, public TBusSessionImpl { - friend class TRemoteClientConnection; - friend class TInvokeOnReply; + class TRemoteClientSession: public TBusClientSession, public TBusSessionImpl { + friend class TRemoteClientConnection; + friend class TInvokeOnReply; - public: - TObjectCounter<TRemoteClientSession> ObjectCounter; + public: + TObjectCounter<TRemoteClientSession> ObjectCounter; - TRemoteClientSessionSemaphore ClientRemoteInFlight; - IBusClientHandler* const ClientHandler; + TRemoteClientSessionSemaphore ClientRemoteInFlight; + IBusClientHandler* const ClientHandler; - public: - TRemoteClientSession(TBusMessageQueue* queue, TBusProtocol* proto, - IBusClientHandler* handler, - const TBusSessionConfig& config, const TString& name); + public: + TRemoteClientSession(TBusMessageQueue* queue, TBusProtocol* proto, + IBusClientHandler* handler, + const TBusSessionConfig& config, const TString& name); - ~TRemoteClientSession() override; + ~TRemoteClientSession() override; - void OnMessageReceived(TRemoteConnection* c, TVectorSwaps<TBusMessagePtrAndHeader>& newMsg) override; + void OnMessageReceived(TRemoteConnection* c, TVectorSwaps<TBusMessagePtrAndHeader>& newMsg) override; - EMessageStatus SendMessageImpl(TBusMessage* msg, const TNetAddr* addr, bool wait, bool oneWay); - EMessageStatus SendMessage(TBusMessage* msg, const TNetAddr* addr = nullptr, bool wait = false) override; - EMessageStatus SendMessageOneWay(TBusMessage* msg, const TNetAddr* addr = nullptr, bool wait = false) override; + EMessageStatus SendMessageImpl(TBusMessage* msg, const TNetAddr* addr, bool wait, bool oneWay); + EMessageStatus SendMessage(TBusMessage* msg, const TNetAddr* addr = nullptr, bool wait = false) override; + EMessageStatus SendMessageOneWay(TBusMessage* msg, const TNetAddr* addr = nullptr, bool wait = false) override; - int GetInFlight() const noexcept override; - void FillStatus() override; - void AcquireInFlight(TArrayRef<TBusMessage* const> messages); - void ReleaseInFlight(TArrayRef<TBusMessage* const> messages); - void ReleaseInFlightAndCallOnReply(TNonDestroyingAutoPtr<TBusMessage> request, TBusMessagePtrAndHeader& response); + int GetInFlight() const noexcept override; + void FillStatus() override; + void AcquireInFlight(TArrayRef<TBusMessage* const> messages); + void ReleaseInFlight(TArrayRef<TBusMessage* const> messages); + void ReleaseInFlightAndCallOnReply(TNonDestroyingAutoPtr<TBusMessage> request, TBusMessagePtrAndHeader& response); - EMessageStatus GetMessageDestination(TBusMessage* mess, const TNetAddr* addrp, TBusSocketAddr* dest); + EMessageStatus GetMessageDestination(TBusMessage* mess, const TNetAddr* addrp, TBusSocketAddr* dest); - void OpenConnection(const TNetAddr&) override; + void OpenConnection(const TNetAddr&) override; - TBusClientConnectionPtr GetConnection(const TNetAddr&) override; - }; + TBusClientConnectionPtr GetConnection(const TNetAddr&) override; + }; #ifdef _MSC_VER #pragma warning(pop) #endif - } -} + } +} diff --git a/library/cpp/messagebus/remote_client_session_semaphore.cpp b/library/cpp/messagebus/remote_client_session_semaphore.cpp index fce7349a36..f877ed4257 100644 --- a/library/cpp/messagebus/remote_client_session_semaphore.cpp +++ b/library/cpp/messagebus/remote_client_session_semaphore.cpp @@ -16,7 +16,7 @@ TRemoteClientSessionSemaphore::TRemoteClientSessionSemaphore(TAtomicBase limit, Y_UNUSED(Name); } -TRemoteClientSessionSemaphore::~TRemoteClientSessionSemaphore() { +TRemoteClientSessionSemaphore::~TRemoteClientSessionSemaphore() { Y_VERIFY(AtomicGet(Current) == 0); } diff --git a/library/cpp/messagebus/remote_client_session_semaphore.h b/library/cpp/messagebus/remote_client_session_semaphore.h index 955440b25f..286ca3c86f 100644 --- a/library/cpp/messagebus/remote_client_session_semaphore.h +++ b/library/cpp/messagebus/remote_client_session_semaphore.h @@ -7,36 +7,36 @@ #include <util/system/condvar.h> #include <util/system/mutex.h> -namespace NBus { - namespace NPrivate { - class TRemoteClientSessionSemaphore: public TComplexConditionSemaphore<TRemoteClientSessionSemaphore> { - private: - const char* const Name; - - TAtomicBase const Limit; - TAtomic Current; - TAtomic StopSignal; - - public: - TRemoteClientSessionSemaphore(TAtomicBase limit, const char* name = "unnamed"); - ~TRemoteClientSessionSemaphore(); - - TAtomicBase GetCurrent() const { - return AtomicGet(Current); - } - - void Acquire(); - bool TryAcquire(); - void Increment(); - void IncrementMultiple(TAtomicBase count); - bool TryWait(); - void Release(); - void ReleaseMultiple(TAtomicBase count); - void Stop(); - - private: - void CheckNeedToUnlock(); - }; - - } -} +namespace NBus { + namespace NPrivate { + class TRemoteClientSessionSemaphore: public TComplexConditionSemaphore<TRemoteClientSessionSemaphore> { + private: + const char* const Name; + + TAtomicBase const Limit; + TAtomic Current; + TAtomic StopSignal; + + public: + TRemoteClientSessionSemaphore(TAtomicBase limit, const char* name = "unnamed"); + ~TRemoteClientSessionSemaphore(); + + TAtomicBase GetCurrent() const { + return AtomicGet(Current); + } + + void Acquire(); + bool TryAcquire(); + void Increment(); + void IncrementMultiple(TAtomicBase count); + bool TryWait(); + void Release(); + void ReleaseMultiple(TAtomicBase count); + void Stop(); + + private: + void CheckNeedToUnlock(); + }; + + } +} diff --git a/library/cpp/messagebus/remote_connection.cpp b/library/cpp/messagebus/remote_connection.cpp index ca4a66f68a..22932569db 100644 --- a/library/cpp/messagebus/remote_connection.cpp +++ b/library/cpp/messagebus/remote_connection.cpp @@ -20,955 +20,955 @@ using namespace NActor; using namespace NBus; using namespace NBus::NPrivate; -namespace NBus { - namespace NPrivate { - TRemoteConnection::TRemoteConnection(TRemoteSessionPtr session, ui64 connectionId, TNetAddr addr) - : TActor<TRemoteConnection, TWriterTag>(session->Queue->WorkQueue.Get()) - , TActor<TRemoteConnection, TReaderTag>(session->Queue->WorkQueue.Get()) - , TScheduleActor<TRemoteConnection, TWriterTag>(&session->Queue->Scheduler) - , Session(session) - , Proto(session->Proto) - , Config(session->Config) - , RemovedFromSession(false) - , ConnectionId(connectionId) - , PeerAddr(addr) - , PeerAddrSocketAddr(addr) - , CreatedTime(TInstant::Now()) - , ReturnConnectFailedImmediately(false) - , GranStatus(Config.Secret.StatusFlushPeriod) - , QuotaMsg(!Session->IsSource_, Config.PerConnectionMaxInFlight, 0) - , QuotaBytes(!Session->IsSource_, Config.PerConnectionMaxInFlightBySize, 0) - , MaxBufferSize(session->Config.MaxBufferSize) - , ShutdownReason(MESSAGE_OK) - { - WriterData.Status.ConnectionId = connectionId; - WriterData.Status.PeerAddr = PeerAddr; - ReaderData.Status.ConnectionId = connectionId; - - const TInstant now = TInstant::Now(); - - WriterFillStatus(); - - GranStatus.Writer.Update(WriterData.Status, now, true); - GranStatus.Reader.Update(ReaderData.Status, now, true); - } - - TRemoteConnection::~TRemoteConnection() { - Y_VERIFY(ReplyQueue.IsEmpty()); - } - - TRemoteConnection::TWriterData::TWriterData() - : Down(0) - , SocketVersion(0) - , InFlight(0) - , AwakeFlags(0) - , State(WRITER_FILLING) - { - } - - TRemoteConnection::TWriterData::~TWriterData() { +namespace NBus { + namespace NPrivate { + TRemoteConnection::TRemoteConnection(TRemoteSessionPtr session, ui64 connectionId, TNetAddr addr) + : TActor<TRemoteConnection, TWriterTag>(session->Queue->WorkQueue.Get()) + , TActor<TRemoteConnection, TReaderTag>(session->Queue->WorkQueue.Get()) + , TScheduleActor<TRemoteConnection, TWriterTag>(&session->Queue->Scheduler) + , Session(session) + , Proto(session->Proto) + , Config(session->Config) + , RemovedFromSession(false) + , ConnectionId(connectionId) + , PeerAddr(addr) + , PeerAddrSocketAddr(addr) + , CreatedTime(TInstant::Now()) + , ReturnConnectFailedImmediately(false) + , GranStatus(Config.Secret.StatusFlushPeriod) + , QuotaMsg(!Session->IsSource_, Config.PerConnectionMaxInFlight, 0) + , QuotaBytes(!Session->IsSource_, Config.PerConnectionMaxInFlightBySize, 0) + , MaxBufferSize(session->Config.MaxBufferSize) + , ShutdownReason(MESSAGE_OK) + { + WriterData.Status.ConnectionId = connectionId; + WriterData.Status.PeerAddr = PeerAddr; + ReaderData.Status.ConnectionId = connectionId; + + const TInstant now = TInstant::Now(); + + WriterFillStatus(); + + GranStatus.Writer.Update(WriterData.Status, now, true); + GranStatus.Reader.Update(ReaderData.Status, now, true); + } + + TRemoteConnection::~TRemoteConnection() { + Y_VERIFY(ReplyQueue.IsEmpty()); + } + + TRemoteConnection::TWriterData::TWriterData() + : Down(0) + , SocketVersion(0) + , InFlight(0) + , AwakeFlags(0) + , State(WRITER_FILLING) + { + } + + TRemoteConnection::TWriterData::~TWriterData() { Y_VERIFY(AtomicGet(Down)); - Y_VERIFY(SendQueue.Empty()); - } - - bool TRemoteConnection::TReaderData::HasBytesInBuf(size_t bytes) noexcept { - size_t left = Buffer.Size() - Offset; - - return (MoreBytes = left >= bytes ? 0 : bytes - left) == 0; - } - - void TRemoteConnection::TWriterData::SetChannel(NEventLoop::TChannelPtr channel) { - Y_VERIFY(!Channel, "must not have channel"); - Y_VERIFY(Buffer.GetBuffer().Empty() && Buffer.LeftSize() == 0, "buffer must be empty"); - Y_VERIFY(State == WRITER_FILLING, "state must be initial"); - Channel = channel; - } - - void TRemoteConnection::TReaderData::SetChannel(NEventLoop::TChannelPtr channel) { - Y_VERIFY(!Channel, "must not have channel"); - Y_VERIFY(Buffer.Empty(), "buffer must be empty"); - Channel = channel; - } - - void TRemoteConnection::TWriterData::DropChannel() { - if (!!Channel) { - Channel->Unregister(); - Channel.Drop(); - } - - Buffer.Reset(); - State = WRITER_FILLING; - } - - void TRemoteConnection::TReaderData::DropChannel() { - // TODO: make Drop call Unregister - if (!!Channel) { - Channel->Unregister(); - Channel.Drop(); - } - Buffer.Reset(); - Offset = 0; - } - - TRemoteConnection::TReaderData::TReaderData() - : Down(0) - , SocketVersion(0) - , Offset(0) - , MoreBytes(0) - { - } - - TRemoteConnection::TReaderData::~TReaderData() { + Y_VERIFY(SendQueue.Empty()); + } + + bool TRemoteConnection::TReaderData::HasBytesInBuf(size_t bytes) noexcept { + size_t left = Buffer.Size() - Offset; + + return (MoreBytes = left >= bytes ? 0 : bytes - left) == 0; + } + + void TRemoteConnection::TWriterData::SetChannel(NEventLoop::TChannelPtr channel) { + Y_VERIFY(!Channel, "must not have channel"); + Y_VERIFY(Buffer.GetBuffer().Empty() && Buffer.LeftSize() == 0, "buffer must be empty"); + Y_VERIFY(State == WRITER_FILLING, "state must be initial"); + Channel = channel; + } + + void TRemoteConnection::TReaderData::SetChannel(NEventLoop::TChannelPtr channel) { + Y_VERIFY(!Channel, "must not have channel"); + Y_VERIFY(Buffer.Empty(), "buffer must be empty"); + Channel = channel; + } + + void TRemoteConnection::TWriterData::DropChannel() { + if (!!Channel) { + Channel->Unregister(); + Channel.Drop(); + } + + Buffer.Reset(); + State = WRITER_FILLING; + } + + void TRemoteConnection::TReaderData::DropChannel() { + // TODO: make Drop call Unregister + if (!!Channel) { + Channel->Unregister(); + Channel.Drop(); + } + Buffer.Reset(); + Offset = 0; + } + + TRemoteConnection::TReaderData::TReaderData() + : Down(0) + , SocketVersion(0) + , Offset(0) + , MoreBytes(0) + { + } + + TRemoteConnection::TReaderData::~TReaderData() { Y_VERIFY(AtomicGet(Down)); - } - - void TRemoteConnection::Send(TNonDestroyingAutoPtr<TBusMessage> msg) { - BeforeSendQueue.Enqueue(msg.Release()); - AtomicIncrement(WriterData.InFlight); - ScheduleWrite(); - } - - void TRemoteConnection::ClearOutgoingQueue(TMessagesPtrs& result, bool reconnect) { - if (!reconnect) { - // Do not clear send queue if reconnecting - WriterData.SendQueue.Clear(&result); - } - } + } - void TRemoteConnection::Shutdown(EMessageStatus status) { - ScheduleShutdown(status); + void TRemoteConnection::Send(TNonDestroyingAutoPtr<TBusMessage> msg) { + BeforeSendQueue.Enqueue(msg.Release()); + AtomicIncrement(WriterData.InFlight); + ScheduleWrite(); + } - ReaderData.ShutdownComplete.WaitI(); - WriterData.ShutdownComplete.WaitI(); - } + void TRemoteConnection::ClearOutgoingQueue(TMessagesPtrs& result, bool reconnect) { + if (!reconnect) { + // Do not clear send queue if reconnecting + WriterData.SendQueue.Clear(&result); + } + } - void TRemoteConnection::TryConnect() { - Y_FAIL("TryConnect is client connection only operation"); - } + void TRemoteConnection::Shutdown(EMessageStatus status) { + ScheduleShutdown(status); - void TRemoteConnection::ScheduleRead() { - GetReaderActor()->Schedule(); - } + ReaderData.ShutdownComplete.WaitI(); + WriterData.ShutdownComplete.WaitI(); + } - void TRemoteConnection::ScheduleWrite() { - GetWriterActor()->Schedule(); - } + void TRemoteConnection::TryConnect() { + Y_FAIL("TryConnect is client connection only operation"); + } - void TRemoteConnection::WriterRotateCounters() { - if (!WriterData.TimeToRotateCounters.FetchTask()) { - return; - } + void TRemoteConnection::ScheduleRead() { + GetReaderActor()->Schedule(); + } - WriterData.Status.DurationCounterPrev = WriterData.Status.DurationCounter; - Reset(WriterData.Status.DurationCounter); - } + void TRemoteConnection::ScheduleWrite() { + GetWriterActor()->Schedule(); + } - void TRemoteConnection::WriterSendStatus(TInstant now, bool force) { - GranStatus.Writer.Update(std::bind(&TRemoteConnection::WriterGetStatus, this), now, force); - } + void TRemoteConnection::WriterRotateCounters() { + if (!WriterData.TimeToRotateCounters.FetchTask()) { + return; + } - void TRemoteConnection::ReaderSendStatus(TInstant now, bool force) { - GranStatus.Reader.Update(std::bind(&TRemoteConnection::ReaderFillStatus, this), now, force); - } + WriterData.Status.DurationCounterPrev = WriterData.Status.DurationCounter; + Reset(WriterData.Status.DurationCounter); + } - const TRemoteConnectionReaderStatus& TRemoteConnection::ReaderFillStatus() { - ReaderData.Status.BufferSize = ReaderData.Buffer.Capacity(); - ReaderData.Status.QuotaMsg = QuotaMsg.Tokens(); - ReaderData.Status.QuotaBytes = QuotaBytes.Tokens(); + void TRemoteConnection::WriterSendStatus(TInstant now, bool force) { + GranStatus.Writer.Update(std::bind(&TRemoteConnection::WriterGetStatus, this), now, force); + } - return ReaderData.Status; - } + void TRemoteConnection::ReaderSendStatus(TInstant now, bool force) { + GranStatus.Reader.Update(std::bind(&TRemoteConnection::ReaderFillStatus, this), now, force); + } + + const TRemoteConnectionReaderStatus& TRemoteConnection::ReaderFillStatus() { + ReaderData.Status.BufferSize = ReaderData.Buffer.Capacity(); + ReaderData.Status.QuotaMsg = QuotaMsg.Tokens(); + ReaderData.Status.QuotaBytes = QuotaBytes.Tokens(); + + return ReaderData.Status; + } - void TRemoteConnection::ProcessItem(TReaderTag, ::NActor::TDefaultTag, TWriterToReaderSocketMessage readSocket) { - if (AtomicGet(ReaderData.Down)) { - ReaderData.Status.Fd = INVALID_SOCKET; - return; - } + void TRemoteConnection::ProcessItem(TReaderTag, ::NActor::TDefaultTag, TWriterToReaderSocketMessage readSocket) { + if (AtomicGet(ReaderData.Down)) { + ReaderData.Status.Fd = INVALID_SOCKET; + return; + } - ReaderData.DropChannel(); + ReaderData.DropChannel(); - ReaderData.Status.Fd = readSocket.Socket; - ReaderData.SocketVersion = readSocket.SocketVersion; + ReaderData.Status.Fd = readSocket.Socket; + ReaderData.SocketVersion = readSocket.SocketVersion; - if (readSocket.Socket != INVALID_SOCKET) { - ReaderData.SetChannel(Session->ReadEventLoop.Register(readSocket.Socket, this, ReadCookie)); - ReaderData.Channel->EnableRead(); - } - } + if (readSocket.Socket != INVALID_SOCKET) { + ReaderData.SetChannel(Session->ReadEventLoop.Register(readSocket.Socket, this, ReadCookie)); + ReaderData.Channel->EnableRead(); + } + } - void TRemoteConnection::ProcessItem(TWriterTag, TReconnectTag, ui32 socketVersion) { - Y_VERIFY(socketVersion <= WriterData.SocketVersion, "something weird"); + void TRemoteConnection::ProcessItem(TWriterTag, TReconnectTag, ui32 socketVersion) { + Y_VERIFY(socketVersion <= WriterData.SocketVersion, "something weird"); - if (WriterData.SocketVersion != socketVersion) { - return; - } - Y_VERIFY(WriterData.Status.Connected, "must be connected at this point"); - Y_VERIFY(!!WriterData.Channel, "must have channel at this point"); + if (WriterData.SocketVersion != socketVersion) { + return; + } + Y_VERIFY(WriterData.Status.Connected, "must be connected at this point"); + Y_VERIFY(!!WriterData.Channel, "must have channel at this point"); - WriterData.Status.Connected = false; - WriterData.DropChannel(); - WriterData.Status.MyAddr = TNetAddr(); - ++WriterData.SocketVersion; - LastConnectAttempt = TInstant(); + WriterData.Status.Connected = false; + WriterData.DropChannel(); + WriterData.Status.MyAddr = TNetAddr(); + ++WriterData.SocketVersion; + LastConnectAttempt = TInstant(); - TMessagesPtrs cleared; - ClearOutgoingQueue(cleared, true); - WriterErrorMessages(cleared, MESSAGE_DELIVERY_FAILED); + TMessagesPtrs cleared; + ClearOutgoingQueue(cleared, true); + WriterErrorMessages(cleared, MESSAGE_DELIVERY_FAILED); - FireClientConnectionEvent(TClientConnectionEvent::DISCONNECTED); + FireClientConnectionEvent(TClientConnectionEvent::DISCONNECTED); - ReaderGetSocketQueue()->EnqueueAndSchedule(TWriterToReaderSocketMessage(INVALID_SOCKET, WriterData.SocketVersion)); - } + ReaderGetSocketQueue()->EnqueueAndSchedule(TWriterToReaderSocketMessage(INVALID_SOCKET, WriterData.SocketVersion)); + } - void TRemoteConnection::ProcessItem(TWriterTag, TWakeReaderTag, ui32 awakeFlags) { - WriterData.AwakeFlags |= awakeFlags; + void TRemoteConnection::ProcessItem(TWriterTag, TWakeReaderTag, ui32 awakeFlags) { + WriterData.AwakeFlags |= awakeFlags; - ReadQuotaWakeup(); - } + ReadQuotaWakeup(); + } - void TRemoteConnection::Act(TReaderTag) { - TInstant now = TInstant::Now(); + void TRemoteConnection::Act(TReaderTag) { + TInstant now = TInstant::Now(); - ReaderData.Status.Acts += 1; + ReaderData.Status.Acts += 1; - ReaderGetSocketQueue()->DequeueAllLikelyEmpty(); + ReaderGetSocketQueue()->DequeueAllLikelyEmpty(); - if (AtomicGet(ReaderData.Down)) { - ReaderData.DropChannel(); + if (AtomicGet(ReaderData.Down)) { + ReaderData.DropChannel(); - ReaderProcessStatusDown(); - ReaderData.ShutdownComplete.Signal(); + ReaderProcessStatusDown(); + ReaderData.ShutdownComplete.Signal(); - } else if (!!ReaderData.Channel) { - Y_ASSERT(ReaderData.ReadMessages.empty()); + } else if (!!ReaderData.Channel) { + Y_ASSERT(ReaderData.ReadMessages.empty()); - for (int i = 0;; ++i) { - if (i == 100) { - // perform other tasks - GetReaderActor()->AddTaskFromActorLoop(); - break; - } + for (int i = 0;; ++i) { + if (i == 100) { + // perform other tasks + GetReaderActor()->AddTaskFromActorLoop(); + break; + } - if (NeedInterruptRead()) { - ReaderData.Channel->EnableRead(); - break; - } + if (NeedInterruptRead()) { + ReaderData.Channel->EnableRead(); + break; + } - if (!ReaderFillBuffer()) - break; + if (!ReaderFillBuffer()) + break; - if (!ReaderProcessBuffer()) - break; - } + if (!ReaderProcessBuffer()) + break; + } - ReaderFlushMessages(); + ReaderFlushMessages(); } - ReaderSendStatus(now); + ReaderSendStatus(now); } - bool TRemoteConnection::QuotaAcquire(size_t msg, size_t bytes) { - ui32 wakeFlags = 0; + bool TRemoteConnection::QuotaAcquire(size_t msg, size_t bytes) { + ui32 wakeFlags = 0; - if (!QuotaMsg.Acquire(msg)) - wakeFlags |= WAKE_QUOTA_MSG; + if (!QuotaMsg.Acquire(msg)) + wakeFlags |= WAKE_QUOTA_MSG; - else if (!QuotaBytes.Acquire(bytes)) - wakeFlags |= WAKE_QUOTA_BYTES; + else if (!QuotaBytes.Acquire(bytes)) + wakeFlags |= WAKE_QUOTA_BYTES; - if (wakeFlags) { - ReaderData.Status.QuotaExhausted++; + if (wakeFlags) { + ReaderData.Status.QuotaExhausted++; - WriterGetWakeQueue()->EnqueueAndSchedule(wakeFlags); - } + WriterGetWakeQueue()->EnqueueAndSchedule(wakeFlags); + } - return wakeFlags == 0; - } + return wakeFlags == 0; + } - void TRemoteConnection::QuotaConsume(size_t msg, size_t bytes) { - QuotaMsg.Consume(msg); - QuotaBytes.Consume(bytes); - } + void TRemoteConnection::QuotaConsume(size_t msg, size_t bytes) { + QuotaMsg.Consume(msg); + QuotaBytes.Consume(bytes); + } - void TRemoteConnection::QuotaReturnSelf(size_t items, size_t bytes) { - if (QuotaReturnValues(items, bytes)) - ReadQuotaWakeup(); - } + void TRemoteConnection::QuotaReturnSelf(size_t items, size_t bytes) { + if (QuotaReturnValues(items, bytes)) + ReadQuotaWakeup(); + } - void TRemoteConnection::QuotaReturnAside(size_t items, size_t bytes) { - if (QuotaReturnValues(items, bytes) && !AtomicGet(WriterData.Down)) - WriterGetWakeQueue()->EnqueueAndSchedule(0x0); - } + void TRemoteConnection::QuotaReturnAside(size_t items, size_t bytes) { + if (QuotaReturnValues(items, bytes) && !AtomicGet(WriterData.Down)) + WriterGetWakeQueue()->EnqueueAndSchedule(0x0); + } - bool TRemoteConnection::QuotaReturnValues(size_t items, size_t bytes) { - bool rMsg = QuotaMsg.Return(items); - bool rBytes = QuotaBytes.Return(bytes); + bool TRemoteConnection::QuotaReturnValues(size_t items, size_t bytes) { + bool rMsg = QuotaMsg.Return(items); + bool rBytes = QuotaBytes.Return(bytes); - return rMsg || rBytes; - } + return rMsg || rBytes; + } - void TRemoteConnection::ReadQuotaWakeup() { - const ui32 mask = WriterData.AwakeFlags & WriteWakeFlags(); + void TRemoteConnection::ReadQuotaWakeup() { + const ui32 mask = WriterData.AwakeFlags & WriteWakeFlags(); - if (mask && mask == WriterData.AwakeFlags) { - WriterData.Status.ReaderWakeups++; - WriterData.AwakeFlags = 0; + if (mask && mask == WriterData.AwakeFlags) { + WriterData.Status.ReaderWakeups++; + WriterData.AwakeFlags = 0; - ScheduleRead(); - } - } + ScheduleRead(); + } + } - ui32 TRemoteConnection::WriteWakeFlags() const { - ui32 awakeFlags = 0; + ui32 TRemoteConnection::WriteWakeFlags() const { + ui32 awakeFlags = 0; - if (QuotaMsg.IsAboveWake()) - awakeFlags |= WAKE_QUOTA_MSG; + if (QuotaMsg.IsAboveWake()) + awakeFlags |= WAKE_QUOTA_MSG; - if (QuotaBytes.IsAboveWake()) - awakeFlags |= WAKE_QUOTA_BYTES; + if (QuotaBytes.IsAboveWake()) + awakeFlags |= WAKE_QUOTA_BYTES; - return awakeFlags; - } + return awakeFlags; + } - bool TRemoteConnection::ReaderProcessBuffer() { - TInstant now = TInstant::Now(); + bool TRemoteConnection::ReaderProcessBuffer() { + TInstant now = TInstant::Now(); + + for (;;) { + if (!ReaderData.HasBytesInBuf(sizeof(TBusHeader))) { + break; + } + + TBusHeader header(MakeArrayRef(ReaderData.Buffer.Data() + ReaderData.Offset, ReaderData.Buffer.Size() - ReaderData.Offset)); + + if (header.Size < sizeof(TBusHeader)) { + LWPROBE(Error, ToString(MESSAGE_HEADER_CORRUPTED), ToString(PeerAddr), ToString(header.Size)); + ReaderData.Status.Incremental.StatusCounter[MESSAGE_HEADER_CORRUPTED] += 1; + ScheduleShutdownOnServerOrReconnectOnClient(MESSAGE_HEADER_CORRUPTED, false); + return false; + } + + if (!IsVersionNegotiation(header) && !IsBusKeyValid(header.Id)) { + LWPROBE(Error, ToString(MESSAGE_HEADER_CORRUPTED), ToString(PeerAddr), ToString(header.Size)); + ReaderData.Status.Incremental.StatusCounter[MESSAGE_HEADER_CORRUPTED] += 1; + ScheduleShutdownOnServerOrReconnectOnClient(MESSAGE_HEADER_CORRUPTED, false); + return false; + } + + if (header.Size > Config.MaxMessageSize) { + LWPROBE(Error, ToString(MESSAGE_MESSAGE_TOO_LARGE), ToString(PeerAddr), ToString(header.Size)); + ReaderData.Status.Incremental.StatusCounter[MESSAGE_MESSAGE_TOO_LARGE] += 1; + ScheduleShutdownOnServerOrReconnectOnClient(MESSAGE_MESSAGE_TOO_LARGE, false); + return false; + } + + if (!ReaderData.HasBytesInBuf(header.Size)) { + if (ReaderData.Offset == 0) { + ReaderData.Buffer.Reserve(header.Size); + } + break; + } + + if (!QuotaAcquire(1, header.Size)) + return false; + + if (!MessageRead(MakeArrayRef(ReaderData.Buffer.Data() + ReaderData.Offset, header.Size), now)) { + return false; + } + + ReaderData.Offset += header.Size; + } - for (;;) { - if (!ReaderData.HasBytesInBuf(sizeof(TBusHeader))) { - break; - } + ReaderData.Buffer.ChopHead(ReaderData.Offset); + ReaderData.Offset = 0; - TBusHeader header(MakeArrayRef(ReaderData.Buffer.Data() + ReaderData.Offset, ReaderData.Buffer.Size() - ReaderData.Offset)); + if (ReaderData.Buffer.Capacity() > MaxBufferSize && ReaderData.Buffer.Size() <= MaxBufferSize) { + ReaderData.Status.Incremental.BufferDrops += 1; - if (header.Size < sizeof(TBusHeader)) { - LWPROBE(Error, ToString(MESSAGE_HEADER_CORRUPTED), ToString(PeerAddr), ToString(header.Size)); - ReaderData.Status.Incremental.StatusCounter[MESSAGE_HEADER_CORRUPTED] += 1; - ScheduleShutdownOnServerOrReconnectOnClient(MESSAGE_HEADER_CORRUPTED, false); - return false; - } + TBuffer temp; + // probably should use another constant + temp.Reserve(Config.DefaultBufferSize); + temp.Append(ReaderData.Buffer.Data(), ReaderData.Buffer.Size()); - if (!IsVersionNegotiation(header) && !IsBusKeyValid(header.Id)) { - LWPROBE(Error, ToString(MESSAGE_HEADER_CORRUPTED), ToString(PeerAddr), ToString(header.Size)); - ReaderData.Status.Incremental.StatusCounter[MESSAGE_HEADER_CORRUPTED] += 1; - ScheduleShutdownOnServerOrReconnectOnClient(MESSAGE_HEADER_CORRUPTED, false); - return false; - } + ReaderData.Buffer.Swap(temp); + } - if (header.Size > Config.MaxMessageSize) { - LWPROBE(Error, ToString(MESSAGE_MESSAGE_TOO_LARGE), ToString(PeerAddr), ToString(header.Size)); - ReaderData.Status.Incremental.StatusCounter[MESSAGE_MESSAGE_TOO_LARGE] += 1; - ScheduleShutdownOnServerOrReconnectOnClient(MESSAGE_MESSAGE_TOO_LARGE, false); - return false; - } + return true; + } - if (!ReaderData.HasBytesInBuf(header.Size)) { - if (ReaderData.Offset == 0) { - ReaderData.Buffer.Reserve(header.Size); - } - break; - } + bool TRemoteConnection::ReaderFillBuffer() { + if (!ReaderData.BufferMore()) + return true; - if (!QuotaAcquire(1, header.Size)) - return false; + if (ReaderData.Buffer.Avail() == 0) { + if (ReaderData.Buffer.Size() == 0) { + ReaderData.Buffer.Reserve(Config.DefaultBufferSize); + } else { + ReaderData.Buffer.Reserve(ReaderData.Buffer.Size() * 2); + } + } - if (!MessageRead(MakeArrayRef(ReaderData.Buffer.Data() + ReaderData.Offset, header.Size), now)) { - return false; - } + Y_ASSERT(ReaderData.Buffer.Avail() > 0); - ReaderData.Offset += header.Size; - } + ssize_t bytes; + { + TWhatThreadDoesPushPop pp("recv syscall"); + bytes = SocketRecv(ReaderData.Channel->GetSocket(), TArrayRef<char>(ReaderData.Buffer.Pos(), ReaderData.Buffer.Avail())); + } - ReaderData.Buffer.ChopHead(ReaderData.Offset); - ReaderData.Offset = 0; + if (bytes < 0) { + if (WouldBlock()) { + ReaderData.Channel->EnableRead(); + return false; + } else { + ReaderData.Channel->DisableRead(); + ScheduleShutdownOnServerOrReconnectOnClient(MESSAGE_DELIVERY_FAILED, false); + return false; + } + } - if (ReaderData.Buffer.Capacity() > MaxBufferSize && ReaderData.Buffer.Size() <= MaxBufferSize) { - ReaderData.Status.Incremental.BufferDrops += 1; + if (bytes == 0) { + ReaderData.Channel->DisableRead(); + // TODO: incorrect: it is possible that only input is shutdown, and output is available + ScheduleShutdownOnServerOrReconnectOnClient(MESSAGE_DELIVERY_FAILED, false); + return false; + } - TBuffer temp; - // probably should use another constant - temp.Reserve(Config.DefaultBufferSize); - temp.Append(ReaderData.Buffer.Data(), ReaderData.Buffer.Size()); + ReaderData.Status.Incremental.NetworkOps += 1; - ReaderData.Buffer.Swap(temp); - } + ReaderData.Buffer.Advance(bytes); + ReaderData.MoreBytes = 0; + return true; + } - return true; + void TRemoteConnection::ClearBeforeSendQueue(EMessageStatus reason) { + BeforeSendQueue.DequeueAll(std::bind(&TRemoteConnection::WriterBeforeWriteErrorMessage, this, std::placeholders::_1, reason)); } - bool TRemoteConnection::ReaderFillBuffer() { - if (!ReaderData.BufferMore()) - return true; + void TRemoteConnection::ClearReplyQueue(EMessageStatus reason) { + TVectorSwaps<TBusMessagePtrAndHeader> replyQueueTemp; + Y_ASSERT(replyQueueTemp.empty()); + ReplyQueue.DequeueAllSingleConsumer(&replyQueueTemp); - if (ReaderData.Buffer.Avail() == 0) { - if (ReaderData.Buffer.Size() == 0) { - ReaderData.Buffer.Reserve(Config.DefaultBufferSize); - } else { - ReaderData.Buffer.Reserve(ReaderData.Buffer.Size() * 2); - } + TVector<TBusMessage*> messages; + for (TVectorSwaps<TBusMessagePtrAndHeader>::reverse_iterator message = replyQueueTemp.rbegin(); + message != replyQueueTemp.rend(); ++message) { + messages.push_back(message->MessagePtr.Release()); } - Y_ASSERT(ReaderData.Buffer.Avail() > 0); + WriterErrorMessages(messages, reason); - ssize_t bytes; - { - TWhatThreadDoesPushPop pp("recv syscall"); - bytes = SocketRecv(ReaderData.Channel->GetSocket(), TArrayRef<char>(ReaderData.Buffer.Pos(), ReaderData.Buffer.Avail())); - } - - if (bytes < 0) { - if (WouldBlock()) { - ReaderData.Channel->EnableRead(); - return false; - } else { - ReaderData.Channel->DisableRead(); - ScheduleShutdownOnServerOrReconnectOnClient(MESSAGE_DELIVERY_FAILED, false); - return false; - } - } - - if (bytes == 0) { - ReaderData.Channel->DisableRead(); - // TODO: incorrect: it is possible that only input is shutdown, and output is available - ScheduleShutdownOnServerOrReconnectOnClient(MESSAGE_DELIVERY_FAILED, false); - return false; - } - - ReaderData.Status.Incremental.NetworkOps += 1; - - ReaderData.Buffer.Advance(bytes); - ReaderData.MoreBytes = 0; - return true; - } - - void TRemoteConnection::ClearBeforeSendQueue(EMessageStatus reason) { - BeforeSendQueue.DequeueAll(std::bind(&TRemoteConnection::WriterBeforeWriteErrorMessage, this, std::placeholders::_1, reason)); - } - - void TRemoteConnection::ClearReplyQueue(EMessageStatus reason) { - TVectorSwaps<TBusMessagePtrAndHeader> replyQueueTemp; - Y_ASSERT(replyQueueTemp.empty()); - ReplyQueue.DequeueAllSingleConsumer(&replyQueueTemp); - - TVector<TBusMessage*> messages; - for (TVectorSwaps<TBusMessagePtrAndHeader>::reverse_iterator message = replyQueueTemp.rbegin(); - message != replyQueueTemp.rend(); ++message) { - messages.push_back(message->MessagePtr.Release()); - } - - WriterErrorMessages(messages, reason); - - replyQueueTemp.clear(); - } - - void TRemoteConnection::ProcessBeforeSendQueueMessage(TBusMessage* message, TInstant now) { - // legacy clients expect this field to be set - if (!Session->IsSource_) { - message->SendTime = now.MilliSeconds(); - } - - WriterData.SendQueue.PushBack(message); - } - - void TRemoteConnection::ProcessBeforeSendQueue(TInstant now) { - BeforeSendQueue.DequeueAll(std::bind(&TRemoteConnection::ProcessBeforeSendQueueMessage, this, std::placeholders::_1, now)); - } - - void TRemoteConnection::WriterFillInFlight() { - // this is hack for TLoadBalancedProtocol - WriterFillStatus(); - AtomicSet(WriterData.InFlight, WriterData.Status.GetInFlight()); - } - - const TRemoteConnectionWriterStatus& TRemoteConnection::WriterGetStatus() { - WriterRotateCounters(); - WriterFillStatus(); - - return WriterData.Status; - } - - void TRemoteConnection::WriterFillStatus() { - if (!!WriterData.Channel) { - WriterData.Status.Fd = WriterData.Channel->GetSocket(); - } else { - WriterData.Status.Fd = INVALID_SOCKET; - } - WriterData.Status.BufferSize = WriterData.Buffer.Capacity(); - WriterData.Status.SendQueueSize = WriterData.SendQueue.Size(); - WriterData.Status.State = WriterData.State; - } - - void TRemoteConnection::WriterProcessStatusDown() { - Session->GetDeadConnectionWriterStatusQueue()->EnqueueAndSchedule(WriterData.Status.Incremental); - Reset(WriterData.Status.Incremental); - } - - void TRemoteConnection::ReaderProcessStatusDown() { - Session->GetDeadConnectionReaderStatusQueue()->EnqueueAndSchedule(ReaderData.Status.Incremental); - Reset(ReaderData.Status.Incremental); - } - - void TRemoteConnection::ProcessWriterDown() { - if (!RemovedFromSession) { - Session->GetRemoveConnectionQueue()->EnqueueAndSchedule(this); - - if (Session->IsSource_) { - if (WriterData.Status.Connected) { - FireClientConnectionEvent(TClientConnectionEvent::DISCONNECTED); - } - } - - LWPROBE(Disconnected, ToString(PeerAddr)); - RemovedFromSession = true; - } - - WriterData.DropChannel(); + replyQueueTemp.clear(); + } - DropEnqueuedData(ShutdownReason, MESSAGE_SHUTDOWN); + void TRemoteConnection::ProcessBeforeSendQueueMessage(TBusMessage* message, TInstant now) { + // legacy clients expect this field to be set + if (!Session->IsSource_) { + message->SendTime = now.MilliSeconds(); + } - WriterProcessStatusDown(); + WriterData.SendQueue.PushBack(message); + } - WriterData.ShutdownComplete.Signal(); - } + void TRemoteConnection::ProcessBeforeSendQueue(TInstant now) { + BeforeSendQueue.DequeueAll(std::bind(&TRemoteConnection::ProcessBeforeSendQueueMessage, this, std::placeholders::_1, now)); + } - void TRemoteConnection::DropEnqueuedData(EMessageStatus reason, EMessageStatus reasonForQueues) { - ClearReplyQueue(reasonForQueues); - ClearBeforeSendQueue(reasonForQueues); - WriterGetReconnectQueue()->Clear(); - WriterGetWakeQueue()->Clear(); + void TRemoteConnection::WriterFillInFlight() { + // this is hack for TLoadBalancedProtocol + WriterFillStatus(); + AtomicSet(WriterData.InFlight, WriterData.Status.GetInFlight()); + } - TMessagesPtrs cleared; - ClearOutgoingQueue(cleared, false); + const TRemoteConnectionWriterStatus& TRemoteConnection::WriterGetStatus() { + WriterRotateCounters(); + WriterFillStatus(); - if (!Session->IsSource_) { - for (auto& i : cleared) { - TBusMessagePtrAndHeader h(i); - CheckedCast<TRemoteServerSession*>(Session.Get())->ReleaseInWorkResponses(MakeArrayRef(&h, 1)); - // assignment back is weird - i = h.MessagePtr.Release(); - // and this part is not batch - } - } + return WriterData.Status; + } - WriterErrorMessages(cleared, reason); - } + void TRemoteConnection::WriterFillStatus() { + if (!!WriterData.Channel) { + WriterData.Status.Fd = WriterData.Channel->GetSocket(); + } else { + WriterData.Status.Fd = INVALID_SOCKET; + } + WriterData.Status.BufferSize = WriterData.Buffer.Capacity(); + WriterData.Status.SendQueueSize = WriterData.SendQueue.Size(); + WriterData.Status.State = WriterData.State; + } - void TRemoteConnection::BeforeTryWrite() { - } + void TRemoteConnection::WriterProcessStatusDown() { + Session->GetDeadConnectionWriterStatusQueue()->EnqueueAndSchedule(WriterData.Status.Incremental); + Reset(WriterData.Status.Incremental); + } - void TRemoteConnection::Act(TWriterTag) { - TInstant now = TInstant::Now(); + void TRemoteConnection::ReaderProcessStatusDown() { + Session->GetDeadConnectionReaderStatusQueue()->EnqueueAndSchedule(ReaderData.Status.Incremental); + Reset(ReaderData.Status.Incremental); + } - WriterData.Status.Acts += 1; + void TRemoteConnection::ProcessWriterDown() { + if (!RemovedFromSession) { + Session->GetRemoveConnectionQueue()->EnqueueAndSchedule(this); - if (Y_UNLIKELY(AtomicGet(WriterData.Down))) { - // dump status must work even if WriterDown - WriterSendStatus(now, true); - ProcessWriterDown(); - return; + if (Session->IsSource_) { + if (WriterData.Status.Connected) { + FireClientConnectionEvent(TClientConnectionEvent::DISCONNECTED); + } + } + + LWPROBE(Disconnected, ToString(PeerAddr)); + RemovedFromSession = true; } - ProcessBeforeSendQueue(now); + WriterData.DropChannel(); - BeforeTryWrite(); + DropEnqueuedData(ShutdownReason, MESSAGE_SHUTDOWN); - WriterFillInFlight(); + WriterProcessStatusDown(); - WriterGetReconnectQueue()->DequeueAllLikelyEmpty(); + WriterData.ShutdownComplete.Signal(); + } - if (!WriterData.Status.Connected) { - TryConnect(); - } else { - for (int i = 0;; ++i) { - if (i == 100) { - // perform other tasks - GetWriterActor()->AddTaskFromActorLoop(); - break; - } + void TRemoteConnection::DropEnqueuedData(EMessageStatus reason, EMessageStatus reasonForQueues) { + ClearReplyQueue(reasonForQueues); + ClearBeforeSendQueue(reasonForQueues); + WriterGetReconnectQueue()->Clear(); + WriterGetWakeQueue()->Clear(); + + TMessagesPtrs cleared; + ClearOutgoingQueue(cleared, false); + + if (!Session->IsSource_) { + for (auto& i : cleared) { + TBusMessagePtrAndHeader h(i); + CheckedCast<TRemoteServerSession*>(Session.Get())->ReleaseInWorkResponses(MakeArrayRef(&h, 1)); + // assignment back is weird + i = h.MessagePtr.Release(); + // and this part is not batch + } + } - if (WriterData.State == WRITER_FILLING) { - WriterFillBuffer(); + WriterErrorMessages(cleared, reason); + } - if (WriterData.State == WRITER_FILLING) { - WriterData.Channel->DisableWrite(); - break; - } + void TRemoteConnection::BeforeTryWrite() { + } - Y_ASSERT(!WriterData.Buffer.Empty()); - } + void TRemoteConnection::Act(TWriterTag) { + TInstant now = TInstant::Now(); - if (WriterData.State == WRITER_FLUSHING) { - WriterFlushBuffer(); + WriterData.Status.Acts += 1; - if (WriterData.State == WRITER_FLUSHING) { - break; - } - } - } - } + if (Y_UNLIKELY(AtomicGet(WriterData.Down))) { + // dump status must work even if WriterDown + WriterSendStatus(now, true); + ProcessWriterDown(); + return; + } - WriterGetWakeQueue()->DequeueAllLikelyEmpty(); + ProcessBeforeSendQueue(now); - WriterSendStatus(now); - } + BeforeTryWrite(); - void TRemoteConnection::WriterFlushBuffer() { - Y_ASSERT(WriterData.State == WRITER_FLUSHING); - Y_ASSERT(!WriterData.Buffer.Empty()); + WriterFillInFlight(); - WriterData.CorkUntil = TInstant::Zero(); + WriterGetReconnectQueue()->DequeueAllLikelyEmpty(); - while (!WriterData.Buffer.Empty()) { - ssize_t bytes; - { - TWhatThreadDoesPushPop pp("send syscall"); - bytes = SocketSend(WriterData.Channel->GetSocket(), TArrayRef<const char>(WriterData.Buffer.LeftPos(), WriterData.Buffer.Size())); - } + if (!WriterData.Status.Connected) { + TryConnect(); + } else { + for (int i = 0;; ++i) { + if (i == 100) { + // perform other tasks + GetWriterActor()->AddTaskFromActorLoop(); + break; + } + + if (WriterData.State == WRITER_FILLING) { + WriterFillBuffer(); + + if (WriterData.State == WRITER_FILLING) { + WriterData.Channel->DisableWrite(); + break; + } + + Y_ASSERT(!WriterData.Buffer.Empty()); + } + + if (WriterData.State == WRITER_FLUSHING) { + WriterFlushBuffer(); + + if (WriterData.State == WRITER_FLUSHING) { + break; + } + } + } + } + + WriterGetWakeQueue()->DequeueAllLikelyEmpty(); - if (bytes < 0) { - if (WouldBlock()) { - WriterData.Channel->EnableWrite(); - return; - } else { - WriterData.Channel->DisableWrite(); - ScheduleShutdownOnServerOrReconnectOnClient(MESSAGE_DELIVERY_FAILED, true); - return; - } - } + WriterSendStatus(now); + } + + void TRemoteConnection::WriterFlushBuffer() { + Y_ASSERT(WriterData.State == WRITER_FLUSHING); + Y_ASSERT(!WriterData.Buffer.Empty()); - WriterData.Status.Incremental.NetworkOps += 1; + WriterData.CorkUntil = TInstant::Zero(); - WriterData.Buffer.LeftProceed(bytes); + while (!WriterData.Buffer.Empty()) { + ssize_t bytes; + { + TWhatThreadDoesPushPop pp("send syscall"); + bytes = SocketSend(WriterData.Channel->GetSocket(), TArrayRef<const char>(WriterData.Buffer.LeftPos(), WriterData.Buffer.Size())); + } + + if (bytes < 0) { + if (WouldBlock()) { + WriterData.Channel->EnableWrite(); + return; + } else { + WriterData.Channel->DisableWrite(); + ScheduleShutdownOnServerOrReconnectOnClient(MESSAGE_DELIVERY_FAILED, true); + return; + } + } + + WriterData.Status.Incremental.NetworkOps += 1; + + WriterData.Buffer.LeftProceed(bytes); } - WriterData.Buffer.Clear(); - if (WriterData.Buffer.Capacity() > MaxBufferSize) { - WriterData.Status.Incremental.BufferDrops += 1; - WriterData.Buffer.Reset(); + WriterData.Buffer.Clear(); + if (WriterData.Buffer.Capacity() > MaxBufferSize) { + WriterData.Status.Incremental.BufferDrops += 1; + WriterData.Buffer.Reset(); } - WriterData.State = WRITER_FILLING; - } + WriterData.State = WRITER_FILLING; + } - void TRemoteConnection::ScheduleShutdownOnServerOrReconnectOnClient(EMessageStatus status, bool writer) { - if (Session->IsSource_) { - WriterGetReconnectQueue()->EnqueueAndSchedule(writer ? WriterData.SocketVersion : ReaderData.SocketVersion); - } else { - ScheduleShutdown(status); + void TRemoteConnection::ScheduleShutdownOnServerOrReconnectOnClient(EMessageStatus status, bool writer) { + if (Session->IsSource_) { + WriterGetReconnectQueue()->EnqueueAndSchedule(writer ? WriterData.SocketVersion : ReaderData.SocketVersion); + } else { + ScheduleShutdown(status); } } - void TRemoteConnection::ScheduleShutdown(EMessageStatus status) { - ShutdownReason = status; + void TRemoteConnection::ScheduleShutdown(EMessageStatus status) { + ShutdownReason = status; - AtomicSet(ReaderData.Down, 1); - ScheduleRead(); + AtomicSet(ReaderData.Down, 1); + ScheduleRead(); - AtomicSet(WriterData.Down, 1); - ScheduleWrite(); - } + AtomicSet(WriterData.Down, 1); + ScheduleWrite(); + } - void TRemoteConnection::CallSerialize(TBusMessage* msg, TBuffer& buffer) const { - size_t posForAssertion = buffer.Size(); - Proto->Serialize(msg, buffer); - Y_VERIFY(buffer.Size() >= posForAssertion, - "incorrect Serialize implementation, pos before serialize: %d, pos after serialize: %d", - int(posForAssertion), int(buffer.Size())); + void TRemoteConnection::CallSerialize(TBusMessage* msg, TBuffer& buffer) const { + size_t posForAssertion = buffer.Size(); + Proto->Serialize(msg, buffer); + Y_VERIFY(buffer.Size() >= posForAssertion, + "incorrect Serialize implementation, pos before serialize: %d, pos after serialize: %d", + int(posForAssertion), int(buffer.Size())); } - namespace { + namespace { inline void WriteHeader(const TBusHeader& header, TBuffer& data) { - data.Reserve(data.Size() + sizeof(TBusHeader)); - /// \todo hton instead of memcpy - memcpy(data.Data() + data.Size(), &header, sizeof(TBusHeader)); - data.Advance(sizeof(TBusHeader)); + data.Reserve(data.Size() + sizeof(TBusHeader)); + /// \todo hton instead of memcpy + memcpy(data.Data() + data.Size(), &header, sizeof(TBusHeader)); + data.Advance(sizeof(TBusHeader)); } inline void WriteDummyHeader(TBuffer& data) { - data.Resize(data.Size() + sizeof(TBusHeader)); - } + data.Resize(data.Size() + sizeof(TBusHeader)); + } - } + } - void TRemoteConnection::SerializeMessage(TBusMessage* msg, TBuffer* data, TMessageCounter* counter) const { - size_t pos = data->Size(); + void TRemoteConnection::SerializeMessage(TBusMessage* msg, TBuffer* data, TMessageCounter* counter) const { + size_t pos = data->Size(); - size_t dataSize; + size_t dataSize; - bool compressionRequested = msg->IsCompressed(); + bool compressionRequested = msg->IsCompressed(); - if (compressionRequested) { - TBuffer compdata; - TBuffer plaindata; - CallSerialize(msg, plaindata); + if (compressionRequested) { + TBuffer compdata; + TBuffer plaindata; + CallSerialize(msg, plaindata); - dataSize = sizeof(TBusHeader) + plaindata.Size(); + dataSize = sizeof(TBusHeader) + plaindata.Size(); - NCodecs::TCodecPtr c = Proto->GetTransportCodec(); + NCodecs::TCodecPtr c = Proto->GetTransportCodec(); c->Encode(TStringBuf{plaindata.data(), plaindata.size()}, compdata); - if (compdata.Size() < plaindata.Size()) { - plaindata.Clear(); - msg->GetHeader()->Size = sizeof(TBusHeader) + compdata.Size(); - WriteHeader(*msg->GetHeader(), *data); - data->Append(compdata.Data(), compdata.Size()); - } else { - compdata.Clear(); - msg->SetCompressed(false); - msg->GetHeader()->Size = sizeof(TBusHeader) + plaindata.Size(); - WriteHeader(*msg->GetHeader(), *data); - data->Append(plaindata.Data(), plaindata.Size()); - } - } else { - WriteDummyHeader(*data); - CallSerialize(msg, *data); - - dataSize = msg->GetHeader()->Size = data->Size() - pos; - - data->Proceed(pos); - WriteHeader(*msg->GetHeader(), *data); - data->Proceed(pos + msg->GetHeader()->Size); - } - - Y_ASSERT(msg->GetHeader()->Size == data->Size() - pos); - counter->AddMessage(dataSize, data->Size() - pos, msg->IsCompressed(), compressionRequested); - } - - TBusMessage* TRemoteConnection::DeserializeMessage(TArrayRef<const char> dataRef, const TBusHeader* header, TMessageCounter* messageCounter, EMessageStatus* status) const { - size_t dataSize; - - TBusMessage* message; - if (header->FlagsInternal & MESSAGE_COMPRESS_INTERNAL) { - TBuffer msg; - { - TBuffer plaindata; - NCodecs::TCodecPtr c = Proto->GetTransportCodec(); - try { - TArrayRef<const char> payload = TBusMessage::GetPayload(dataRef); - c->Decode(TStringBuf{payload.data(), payload.size()}, plaindata); - } catch (...) { - // catch all, because - // http://nga.at.yandex-team.ru/replies.xml?item_no=3884 - *status = MESSAGE_DECOMPRESS_ERROR; - return nullptr; - } - - msg.Append(dataRef.data(), sizeof(TBusHeader)); - msg.Append(plaindata.Data(), plaindata.Size()); - } - TArrayRef<const char> msgRef(msg.Data(), msg.Size()); - dataSize = sizeof(TBusHeader) + msgRef.size(); - // TODO: track error types - message = Proto->Deserialize(header->Type, msgRef.Slice(sizeof(TBusHeader))).Release(); - if (!message) { - *status = MESSAGE_DESERIALIZE_ERROR; - return nullptr; - } - *message->GetHeader() = *header; - message->SetCompressed(true); - } else { - dataSize = dataRef.size(); - message = Proto->Deserialize(header->Type, dataRef.Slice(sizeof(TBusHeader))).Release(); - if (!message) { - *status = MESSAGE_DESERIALIZE_ERROR; - return nullptr; - } - *message->GetHeader() = *header; - } - - messageCounter->AddMessage(dataSize, dataRef.size(), header->FlagsInternal & MESSAGE_COMPRESS_INTERNAL, false); - - return message; - } - - void TRemoteConnection::ResetOneWayFlag(TArrayRef<TBusMessage*> messages) { - for (auto message : messages) { - message->LocalFlags &= ~MESSAGE_ONE_WAY_INTERNAL; - } - } - - void TRemoteConnection::ReaderFlushMessages() { - if (!ReaderData.ReadMessages.empty()) { - Session->OnMessageReceived(this, ReaderData.ReadMessages); - ReaderData.ReadMessages.clear(); - } - } - - // @return false if actor should break - bool TRemoteConnection::MessageRead(TArrayRef<const char> readDataRef, TInstant now) { - TBusHeader header(readDataRef); - - Y_ASSERT(readDataRef.size() == header.Size); - - if (header.GetVersionInternal() != YBUS_VERSION) { - ReaderProcessMessageUnknownVersion(readDataRef); - return true; - } - - EMessageStatus deserializeFailureStatus = MESSAGE_OK; - TBusMessage* r = DeserializeMessage(readDataRef, &header, &ReaderData.Status.Incremental.MessageCounter, &deserializeFailureStatus); - - if (!r) { - Y_VERIFY(deserializeFailureStatus != MESSAGE_OK, "state check"); - LWPROBE(Error, ToString(deserializeFailureStatus), ToString(PeerAddr), ""); - ReaderData.Status.Incremental.StatusCounter[deserializeFailureStatus] += 1; - ScheduleShutdownOnServerOrReconnectOnClient(deserializeFailureStatus, false); - return false; - } - - LWPROBE(Read, r->GetHeader()->Size); - - r->ReplyTo = PeerAddrSocketAddr; - - TBusMessagePtrAndHeader h(r); - r->RecvTime = now; - - QuotaConsume(1, header.Size); - - ReaderData.ReadMessages.push_back(h); - if (ReaderData.ReadMessages.size() >= 100) { - ReaderFlushMessages(); - } - - return true; - } - - void TRemoteConnection::WriterFillBuffer() { - Y_ASSERT(WriterData.State == WRITER_FILLING); - - Y_ASSERT(WriterData.Buffer.LeftSize() == 0); - - if (Y_UNLIKELY(!WrongVersionRequests.IsEmpty())) { - TVector<TBusHeader> headers; - WrongVersionRequests.DequeueAllSingleConsumer(&headers); - for (TVector<TBusHeader>::reverse_iterator header = headers.rbegin(); - header != headers.rend(); ++header) { - TBusHeader response = *header; - response.SendTime = NBus::Now(); - response.Size = sizeof(TBusHeader); - response.FlagsInternal = 0; - response.SetVersionInternal(YBUS_VERSION); - WriteHeader(response, WriterData.Buffer.GetBuffer()); - } - - Y_ASSERT(!WriterData.Buffer.Empty()); - WriterData.State = WRITER_FLUSHING; - return; - } - - TTempTlsVector<TBusMessagePtrAndHeader, void, TVectorSwaps> writeMessages; - - for (;;) { - THolder<TBusMessage> writeMessage(WriterData.SendQueue.PopFront()); - if (!writeMessage) { - break; - } - - if (Config.Cork != TDuration::Zero()) { - if (WriterData.CorkUntil == TInstant::Zero()) { - WriterData.CorkUntil = TInstant::Now() + Config.Cork; - } - } + if (compdata.Size() < plaindata.Size()) { + plaindata.Clear(); + msg->GetHeader()->Size = sizeof(TBusHeader) + compdata.Size(); + WriteHeader(*msg->GetHeader(), *data); + data->Append(compdata.Data(), compdata.Size()); + } else { + compdata.Clear(); + msg->SetCompressed(false); + msg->GetHeader()->Size = sizeof(TBusHeader) + plaindata.Size(); + WriteHeader(*msg->GetHeader(), *data); + data->Append(plaindata.Data(), plaindata.Size()); + } + } else { + WriteDummyHeader(*data); + CallSerialize(msg, *data); + + dataSize = msg->GetHeader()->Size = data->Size() - pos; + + data->Proceed(pos); + WriteHeader(*msg->GetHeader(), *data); + data->Proceed(pos + msg->GetHeader()->Size); + } - size_t sizeBeforeSerialize = WriterData.Buffer.Size(); + Y_ASSERT(msg->GetHeader()->Size == data->Size() - pos); + counter->AddMessage(dataSize, data->Size() - pos, msg->IsCompressed(), compressionRequested); + } - TMessageCounter messageCounter = WriterData.Status.Incremental.MessageCounter; + TBusMessage* TRemoteConnection::DeserializeMessage(TArrayRef<const char> dataRef, const TBusHeader* header, TMessageCounter* messageCounter, EMessageStatus* status) const { + size_t dataSize; + + TBusMessage* message; + if (header->FlagsInternal & MESSAGE_COMPRESS_INTERNAL) { + TBuffer msg; + { + TBuffer plaindata; + NCodecs::TCodecPtr c = Proto->GetTransportCodec(); + try { + TArrayRef<const char> payload = TBusMessage::GetPayload(dataRef); + c->Decode(TStringBuf{payload.data(), payload.size()}, plaindata); + } catch (...) { + // catch all, because + // http://nga.at.yandex-team.ru/replies.xml?item_no=3884 + *status = MESSAGE_DECOMPRESS_ERROR; + return nullptr; + } + + msg.Append(dataRef.data(), sizeof(TBusHeader)); + msg.Append(plaindata.Data(), plaindata.Size()); + } + TArrayRef<const char> msgRef(msg.Data(), msg.Size()); + dataSize = sizeof(TBusHeader) + msgRef.size(); + // TODO: track error types + message = Proto->Deserialize(header->Type, msgRef.Slice(sizeof(TBusHeader))).Release(); + if (!message) { + *status = MESSAGE_DESERIALIZE_ERROR; + return nullptr; + } + *message->GetHeader() = *header; + message->SetCompressed(true); + } else { + dataSize = dataRef.size(); + message = Proto->Deserialize(header->Type, dataRef.Slice(sizeof(TBusHeader))).Release(); + if (!message) { + *status = MESSAGE_DESERIALIZE_ERROR; + return nullptr; + } + *message->GetHeader() = *header; + } - SerializeMessage(writeMessage.Get(), &WriterData.Buffer.GetBuffer(), &messageCounter); + messageCounter->AddMessage(dataSize, dataRef.size(), header->FlagsInternal & MESSAGE_COMPRESS_INTERNAL, false); - size_t written = WriterData.Buffer.Size() - sizeBeforeSerialize; - if (written > Config.MaxMessageSize) { - WriterData.Buffer.GetBuffer().EraseBack(written); - WriterBeforeWriteErrorMessage(writeMessage.Release(), MESSAGE_MESSAGE_TOO_LARGE); - continue; - } + return message; + } + + void TRemoteConnection::ResetOneWayFlag(TArrayRef<TBusMessage*> messages) { + for (auto message : messages) { + message->LocalFlags &= ~MESSAGE_ONE_WAY_INTERNAL; + } + } + + void TRemoteConnection::ReaderFlushMessages() { + if (!ReaderData.ReadMessages.empty()) { + Session->OnMessageReceived(this, ReaderData.ReadMessages); + ReaderData.ReadMessages.clear(); + } + } + + // @return false if actor should break + bool TRemoteConnection::MessageRead(TArrayRef<const char> readDataRef, TInstant now) { + TBusHeader header(readDataRef); - WriterData.Status.Incremental.MessageCounter = messageCounter; + Y_ASSERT(readDataRef.size() == header.Size); + + if (header.GetVersionInternal() != YBUS_VERSION) { + ReaderProcessMessageUnknownVersion(readDataRef); + return true; + } - TBusMessagePtrAndHeader h(writeMessage.Release()); - writeMessages.GetVector()->push_back(h); + EMessageStatus deserializeFailureStatus = MESSAGE_OK; + TBusMessage* r = DeserializeMessage(readDataRef, &header, &ReaderData.Status.Incremental.MessageCounter, &deserializeFailureStatus); - Y_ASSERT(!WriterData.Buffer.Empty()); - if (WriterData.Buffer.Size() >= Config.SendThreshold) { - break; - } + if (!r) { + Y_VERIFY(deserializeFailureStatus != MESSAGE_OK, "state check"); + LWPROBE(Error, ToString(deserializeFailureStatus), ToString(PeerAddr), ""); + ReaderData.Status.Incremental.StatusCounter[deserializeFailureStatus] += 1; + ScheduleShutdownOnServerOrReconnectOnClient(deserializeFailureStatus, false); + return false; } - - if (!WriterData.Buffer.Empty()) { - if (WriterData.Buffer.Size() >= Config.SendThreshold) { - WriterData.State = WRITER_FLUSHING; - } else if (WriterData.CorkUntil == TInstant::Zero()) { - WriterData.State = WRITER_FLUSHING; - } else if (TInstant::Now() >= WriterData.CorkUntil) { - WriterData.State = WRITER_FLUSHING; - } else { - // keep filling - Y_ASSERT(WriterData.State == WRITER_FILLING); - GetWriterSchedulerActor()->ScheduleAt(WriterData.CorkUntil); - } - } else { - // keep filling - Y_ASSERT(WriterData.State == WRITER_FILLING); - } - - size_t bytes = MessageSize(*writeMessages.GetVector()); - - QuotaReturnSelf(writeMessages.GetVector()->size(), bytes); - - // This is called before `send` syscall inducing latency - MessageSent(*writeMessages.GetVector()); - } - - size_t TRemoteConnection::MessageSize(TArrayRef<TBusMessagePtrAndHeader> messages) { - size_t size = 0; - for (const auto& message : messages) - size += message.MessagePtr->RequestSize; - - return size; - } - - size_t TRemoteConnection::GetInFlight() { - return AtomicGet(WriterData.InFlight); - } - - size_t TRemoteConnection::GetConnectSyscallsNumForTest() { - return WriterData.Status.ConnectSyscalls; - } - - void TRemoteConnection::WriterBeforeWriteErrorMessage(TBusMessage* message, EMessageStatus status) { - if (Session->IsSource_) { - CheckedCast<TRemoteClientSession*>(Session.Get())->ReleaseInFlight({message}); - WriterErrorMessage(message, status); - } else { - TBusMessagePtrAndHeader h(message); - CheckedCast<TRemoteServerSession*>(Session.Get())->ReleaseInWorkResponses(MakeArrayRef(&h, 1)); - WriterErrorMessage(h.MessagePtr.Release(), status); - } - } - - void TRemoteConnection::WriterErrorMessage(TNonDestroyingAutoPtr<TBusMessage> m, EMessageStatus status) { - TBusMessage* released = m.Release(); - WriterErrorMessages(MakeArrayRef(&released, 1), status); - } - - void TRemoteConnection::WriterErrorMessages(const TArrayRef<TBusMessage*> ms, EMessageStatus status) { - ResetOneWayFlag(ms); - - WriterData.Status.Incremental.StatusCounter[status] += ms.size(); - for (auto m : ms) { - Session->InvokeOnError(m, status); - } - } - - void TRemoteConnection::FireClientConnectionEvent(TClientConnectionEvent::EType type) { - Y_VERIFY(Session->IsSource_, "state check"); - TClientConnectionEvent event(type, ConnectionId, PeerAddr); - TRemoteClientSession* session = CheckedCast<TRemoteClientSession*>(Session.Get()); - session->ClientHandler->OnClientConnectionEvent(event); - } - - bool TRemoteConnection::IsAlive() const { - return !AtomicGet(WriterData.Down); - } + + LWPROBE(Read, r->GetHeader()->Size); + + r->ReplyTo = PeerAddrSocketAddr; + + TBusMessagePtrAndHeader h(r); + r->RecvTime = now; + + QuotaConsume(1, header.Size); + + ReaderData.ReadMessages.push_back(h); + if (ReaderData.ReadMessages.size() >= 100) { + ReaderFlushMessages(); + } + + return true; + } + + void TRemoteConnection::WriterFillBuffer() { + Y_ASSERT(WriterData.State == WRITER_FILLING); + + Y_ASSERT(WriterData.Buffer.LeftSize() == 0); + + if (Y_UNLIKELY(!WrongVersionRequests.IsEmpty())) { + TVector<TBusHeader> headers; + WrongVersionRequests.DequeueAllSingleConsumer(&headers); + for (TVector<TBusHeader>::reverse_iterator header = headers.rbegin(); + header != headers.rend(); ++header) { + TBusHeader response = *header; + response.SendTime = NBus::Now(); + response.Size = sizeof(TBusHeader); + response.FlagsInternal = 0; + response.SetVersionInternal(YBUS_VERSION); + WriteHeader(response, WriterData.Buffer.GetBuffer()); + } + + Y_ASSERT(!WriterData.Buffer.Empty()); + WriterData.State = WRITER_FLUSHING; + return; + } + + TTempTlsVector<TBusMessagePtrAndHeader, void, TVectorSwaps> writeMessages; + + for (;;) { + THolder<TBusMessage> writeMessage(WriterData.SendQueue.PopFront()); + if (!writeMessage) { + break; + } + + if (Config.Cork != TDuration::Zero()) { + if (WriterData.CorkUntil == TInstant::Zero()) { + WriterData.CorkUntil = TInstant::Now() + Config.Cork; + } + } + + size_t sizeBeforeSerialize = WriterData.Buffer.Size(); + + TMessageCounter messageCounter = WriterData.Status.Incremental.MessageCounter; + + SerializeMessage(writeMessage.Get(), &WriterData.Buffer.GetBuffer(), &messageCounter); + + size_t written = WriterData.Buffer.Size() - sizeBeforeSerialize; + if (written > Config.MaxMessageSize) { + WriterData.Buffer.GetBuffer().EraseBack(written); + WriterBeforeWriteErrorMessage(writeMessage.Release(), MESSAGE_MESSAGE_TOO_LARGE); + continue; + } + + WriterData.Status.Incremental.MessageCounter = messageCounter; + + TBusMessagePtrAndHeader h(writeMessage.Release()); + writeMessages.GetVector()->push_back(h); + + Y_ASSERT(!WriterData.Buffer.Empty()); + if (WriterData.Buffer.Size() >= Config.SendThreshold) { + break; + } + } + + if (!WriterData.Buffer.Empty()) { + if (WriterData.Buffer.Size() >= Config.SendThreshold) { + WriterData.State = WRITER_FLUSHING; + } else if (WriterData.CorkUntil == TInstant::Zero()) { + WriterData.State = WRITER_FLUSHING; + } else if (TInstant::Now() >= WriterData.CorkUntil) { + WriterData.State = WRITER_FLUSHING; + } else { + // keep filling + Y_ASSERT(WriterData.State == WRITER_FILLING); + GetWriterSchedulerActor()->ScheduleAt(WriterData.CorkUntil); + } + } else { + // keep filling + Y_ASSERT(WriterData.State == WRITER_FILLING); + } + + size_t bytes = MessageSize(*writeMessages.GetVector()); + + QuotaReturnSelf(writeMessages.GetVector()->size(), bytes); + + // This is called before `send` syscall inducing latency + MessageSent(*writeMessages.GetVector()); + } + + size_t TRemoteConnection::MessageSize(TArrayRef<TBusMessagePtrAndHeader> messages) { + size_t size = 0; + for (const auto& message : messages) + size += message.MessagePtr->RequestSize; + + return size; + } + + size_t TRemoteConnection::GetInFlight() { + return AtomicGet(WriterData.InFlight); + } + + size_t TRemoteConnection::GetConnectSyscallsNumForTest() { + return WriterData.Status.ConnectSyscalls; + } + + void TRemoteConnection::WriterBeforeWriteErrorMessage(TBusMessage* message, EMessageStatus status) { + if (Session->IsSource_) { + CheckedCast<TRemoteClientSession*>(Session.Get())->ReleaseInFlight({message}); + WriterErrorMessage(message, status); + } else { + TBusMessagePtrAndHeader h(message); + CheckedCast<TRemoteServerSession*>(Session.Get())->ReleaseInWorkResponses(MakeArrayRef(&h, 1)); + WriterErrorMessage(h.MessagePtr.Release(), status); + } + } + + void TRemoteConnection::WriterErrorMessage(TNonDestroyingAutoPtr<TBusMessage> m, EMessageStatus status) { + TBusMessage* released = m.Release(); + WriterErrorMessages(MakeArrayRef(&released, 1), status); + } + + void TRemoteConnection::WriterErrorMessages(const TArrayRef<TBusMessage*> ms, EMessageStatus status) { + ResetOneWayFlag(ms); + + WriterData.Status.Incremental.StatusCounter[status] += ms.size(); + for (auto m : ms) { + Session->InvokeOnError(m, status); + } + } + + void TRemoteConnection::FireClientConnectionEvent(TClientConnectionEvent::EType type) { + Y_VERIFY(Session->IsSource_, "state check"); + TClientConnectionEvent event(type, ConnectionId, PeerAddr); + TRemoteClientSession* session = CheckedCast<TRemoteClientSession*>(Session.Get()); + session->ClientHandler->OnClientConnectionEvent(event); + } + + bool TRemoteConnection::IsAlive() const { + return !AtomicGet(WriterData.Down); + } } } diff --git a/library/cpp/messagebus/remote_connection.h b/library/cpp/messagebus/remote_connection.h index 575fbff32e..4538947368 100644 --- a/library/cpp/messagebus/remote_connection.h +++ b/library/cpp/messagebus/remote_connection.h @@ -26,269 +26,269 @@ #include <util/system/event.h> #include <util/thread/lfstack.h> -namespace NBus { - namespace NPrivate { - class TRemoteConnection; - - typedef TIntrusivePtr<TRemoteConnection> TRemoteConnectionPtr; - typedef TIntrusivePtr<TBusSessionImpl> TRemoteSessionPtr; - - static void* const WriteCookie = (void*)1; - static void* const ReadCookie = (void*)2; - - enum { - WAKE_QUOTA_MSG = 0x01, - WAKE_QUOTA_BYTES = 0x02 - }; - - struct TWriterTag {}; - struct TReaderTag {}; - struct TReconnectTag {}; - struct TWakeReaderTag {}; - - struct TWriterToReaderSocketMessage { - TSocket Socket; - ui32 SocketVersion; - - TWriterToReaderSocketMessage(TSocket socket, ui32 socketVersion) - : Socket(socket) - , SocketVersion(socketVersion) - { - } - }; - - class TRemoteConnection - : public NEventLoop::IEventHandler, - public ::NActor::TActor<TRemoteConnection, TWriterTag>, - public ::NActor::TActor<TRemoteConnection, TReaderTag>, - private ::NActor::TQueueInActor<TRemoteConnection, TWriterToReaderSocketMessage, TReaderTag>, - private ::NActor::TQueueInActor<TRemoteConnection, ui32, TWriterTag, TReconnectTag>, - private ::NActor::TQueueInActor<TRemoteConnection, ui32, TWriterTag, TWakeReaderTag>, - public TScheduleActor<TRemoteConnection, TWriterTag> { - friend struct TBusSessionImpl; - friend class TRemoteClientSession; - friend class TRemoteServerSession; - friend class ::NActor::TQueueInActor<TRemoteConnection, TWriterToReaderSocketMessage, TReaderTag>; - friend class ::NActor::TQueueInActor<TRemoteConnection, ui32, TWriterTag, TReconnectTag>; - friend class ::NActor::TQueueInActor<TRemoteConnection, ui32, TWriterTag, TWakeReaderTag>; - - protected: - ::NActor::TQueueInActor<TRemoteConnection, TWriterToReaderSocketMessage, TReaderTag>* ReaderGetSocketQueue() { - return this; - } - - ::NActor::TQueueInActor<TRemoteConnection, ui32, TWriterTag, TReconnectTag>* WriterGetReconnectQueue() { - return this; - } - - ::NActor::TQueueInActor<TRemoteConnection, ui32, TWriterTag, TWakeReaderTag>* WriterGetWakeQueue() { - return this; - } - - protected: - TRemoteConnection(TRemoteSessionPtr session, ui64 connectionId, TNetAddr addr); - ~TRemoteConnection() override; - - virtual void ClearOutgoingQueue(TMessagesPtrs&, bool reconnect /* or shutdown */); - - public: - void Send(TNonDestroyingAutoPtr<TBusMessage> msg); - void Shutdown(EMessageStatus status); - - inline const TNetAddr& GetAddr() const noexcept; - - private: - friend class TScheduleConnect; - friend class TWorkIO; - - protected: - static size_t MessageSize(TArrayRef<TBusMessagePtrAndHeader>); - bool QuotaAcquire(size_t msg, size_t bytes); - void QuotaConsume(size_t msg, size_t bytes); - void QuotaReturnSelf(size_t items, size_t bytes); - bool QuotaReturnValues(size_t items, size_t bytes); - - bool ReaderProcessBuffer(); - bool ReaderFillBuffer(); - void ReaderFlushMessages(); - - void ReadQuotaWakeup(); - ui32 WriteWakeFlags() const; - - virtual bool NeedInterruptRead() { - return false; - } - - public: - virtual void TryConnect(); - void ProcessItem(TReaderTag, ::NActor::TDefaultTag, TWriterToReaderSocketMessage); - void ProcessItem(TWriterTag, TReconnectTag, ui32 socketVersion); - void ProcessItem(TWriterTag, TWakeReaderTag, ui32 awakeFlags); - void Act(TReaderTag); - inline void WriterBeforeWriteErrorMessage(TBusMessage*, EMessageStatus); - void ClearBeforeSendQueue(EMessageStatus reasonForQueues); - void ClearReplyQueue(EMessageStatus reasonForQueues); - inline void ProcessBeforeSendQueueMessage(TBusMessage*, TInstant now); - void ProcessBeforeSendQueue(TInstant now); - void WriterProcessStatusDown(); - void ReaderProcessStatusDown(); - void ProcessWriterDown(); - void DropEnqueuedData(EMessageStatus reason, EMessageStatus reasonForQueues); - const TRemoteConnectionWriterStatus& WriterGetStatus(); - virtual void WriterFillStatus(); - void WriterFillInFlight(); - virtual void BeforeTryWrite(); - void Act(TWriterTag); - void ScheduleRead(); - void ScheduleWrite(); - void ScheduleShutdownOnServerOrReconnectOnClient(EMessageStatus status, bool writer); - void ScheduleShutdown(EMessageStatus status); - void WriterFlushBuffer(); - void WriterFillBuffer(); - void ReaderSendStatus(TInstant now, bool force = false); - const TRemoteConnectionReaderStatus& ReaderFillStatus(); - void WriterRotateCounters(); - void WriterSendStatus(TInstant now, bool force = false); - void WriterSendStatusIfNecessary(TInstant now); - void QuotaReturnAside(size_t items, size_t bytes); - virtual void ReaderProcessMessageUnknownVersion(TArrayRef<const char> dataRef) = 0; - bool MessageRead(TArrayRef<const char> dataRef, TInstant now); - virtual void MessageSent(TArrayRef<TBusMessagePtrAndHeader> messages) = 0; - - void CallSerialize(TBusMessage* msg, TBuffer& buffer) const; - void SerializeMessage(TBusMessage* msg, TBuffer* data, TMessageCounter* counter) const; - TBusMessage* DeserializeMessage(TArrayRef<const char> dataRef, const TBusHeader* header, TMessageCounter* messageCounter, EMessageStatus* status) const; - - void ResetOneWayFlag(TArrayRef<TBusMessage*>); - - inline ::NActor::TActor<TRemoteConnection, TWriterTag>* GetWriterActor() { - return this; - } - inline ::NActor::TActor<TRemoteConnection, TReaderTag>* GetReaderActor() { - return this; - } - inline TScheduleActor<TRemoteConnection, TWriterTag>* GetWriterSchedulerActor() { - return this; - } - - void WriterErrorMessage(TNonDestroyingAutoPtr<TBusMessage> m, EMessageStatus status); - // takes ownership of ms - void WriterErrorMessages(const TArrayRef<TBusMessage*> ms, EMessageStatus status); - - void FireClientConnectionEvent(TClientConnectionEvent::EType); - - size_t GetInFlight(); - size_t GetConnectSyscallsNumForTest(); - - bool IsReturnConnectFailedImmediately() { - return (bool)AtomicGet(ReturnConnectFailedImmediately); - } - - bool IsAlive() const; - - TRemoteSessionPtr Session; - TBusProtocol* const Proto; - TBusSessionConfig const Config; - bool RemovedFromSession; - const ui64 ConnectionId; - const TNetAddr PeerAddr; - const TBusSocketAddr PeerAddrSocketAddr; - - const TInstant CreatedTime; - TInstant LastConnectAttempt; - TAtomic ReturnConnectFailedImmediately; - - protected: - ::NActor::TQueueForActor<TBusMessage*> BeforeSendQueue; - TLockFreeStack<TBusHeader> WrongVersionRequests; - - struct TWriterData { - TAtomic Down; - - NEventLoop::TChannelPtr Channel; - ui32 SocketVersion; - - TRemoteConnectionWriterStatus Status; - TInstant StatusLastSendTime; - - TLocalTasks TimeToRotateCounters; - - TAtomic InFlight; - - TTimedMessages SendQueue; - ui32 AwakeFlags; - EWriterState State; - TLeftRightBuffer Buffer; - TInstant CorkUntil; +namespace NBus { + namespace NPrivate { + class TRemoteConnection; + + typedef TIntrusivePtr<TRemoteConnection> TRemoteConnectionPtr; + typedef TIntrusivePtr<TBusSessionImpl> TRemoteSessionPtr; + + static void* const WriteCookie = (void*)1; + static void* const ReadCookie = (void*)2; + + enum { + WAKE_QUOTA_MSG = 0x01, + WAKE_QUOTA_BYTES = 0x02 + }; + + struct TWriterTag {}; + struct TReaderTag {}; + struct TReconnectTag {}; + struct TWakeReaderTag {}; + + struct TWriterToReaderSocketMessage { + TSocket Socket; + ui32 SocketVersion; + + TWriterToReaderSocketMessage(TSocket socket, ui32 socketVersion) + : Socket(socket) + , SocketVersion(socketVersion) + { + } + }; + + class TRemoteConnection + : public NEventLoop::IEventHandler, + public ::NActor::TActor<TRemoteConnection, TWriterTag>, + public ::NActor::TActor<TRemoteConnection, TReaderTag>, + private ::NActor::TQueueInActor<TRemoteConnection, TWriterToReaderSocketMessage, TReaderTag>, + private ::NActor::TQueueInActor<TRemoteConnection, ui32, TWriterTag, TReconnectTag>, + private ::NActor::TQueueInActor<TRemoteConnection, ui32, TWriterTag, TWakeReaderTag>, + public TScheduleActor<TRemoteConnection, TWriterTag> { + friend struct TBusSessionImpl; + friend class TRemoteClientSession; + friend class TRemoteServerSession; + friend class ::NActor::TQueueInActor<TRemoteConnection, TWriterToReaderSocketMessage, TReaderTag>; + friend class ::NActor::TQueueInActor<TRemoteConnection, ui32, TWriterTag, TReconnectTag>; + friend class ::NActor::TQueueInActor<TRemoteConnection, ui32, TWriterTag, TWakeReaderTag>; + + protected: + ::NActor::TQueueInActor<TRemoteConnection, TWriterToReaderSocketMessage, TReaderTag>* ReaderGetSocketQueue() { + return this; + } + + ::NActor::TQueueInActor<TRemoteConnection, ui32, TWriterTag, TReconnectTag>* WriterGetReconnectQueue() { + return this; + } + + ::NActor::TQueueInActor<TRemoteConnection, ui32, TWriterTag, TWakeReaderTag>* WriterGetWakeQueue() { + return this; + } + + protected: + TRemoteConnection(TRemoteSessionPtr session, ui64 connectionId, TNetAddr addr); + ~TRemoteConnection() override; + + virtual void ClearOutgoingQueue(TMessagesPtrs&, bool reconnect /* or shutdown */); + + public: + void Send(TNonDestroyingAutoPtr<TBusMessage> msg); + void Shutdown(EMessageStatus status); + + inline const TNetAddr& GetAddr() const noexcept; + + private: + friend class TScheduleConnect; + friend class TWorkIO; + + protected: + static size_t MessageSize(TArrayRef<TBusMessagePtrAndHeader>); + bool QuotaAcquire(size_t msg, size_t bytes); + void QuotaConsume(size_t msg, size_t bytes); + void QuotaReturnSelf(size_t items, size_t bytes); + bool QuotaReturnValues(size_t items, size_t bytes); + + bool ReaderProcessBuffer(); + bool ReaderFillBuffer(); + void ReaderFlushMessages(); + + void ReadQuotaWakeup(); + ui32 WriteWakeFlags() const; + + virtual bool NeedInterruptRead() { + return false; + } + + public: + virtual void TryConnect(); + void ProcessItem(TReaderTag, ::NActor::TDefaultTag, TWriterToReaderSocketMessage); + void ProcessItem(TWriterTag, TReconnectTag, ui32 socketVersion); + void ProcessItem(TWriterTag, TWakeReaderTag, ui32 awakeFlags); + void Act(TReaderTag); + inline void WriterBeforeWriteErrorMessage(TBusMessage*, EMessageStatus); + void ClearBeforeSendQueue(EMessageStatus reasonForQueues); + void ClearReplyQueue(EMessageStatus reasonForQueues); + inline void ProcessBeforeSendQueueMessage(TBusMessage*, TInstant now); + void ProcessBeforeSendQueue(TInstant now); + void WriterProcessStatusDown(); + void ReaderProcessStatusDown(); + void ProcessWriterDown(); + void DropEnqueuedData(EMessageStatus reason, EMessageStatus reasonForQueues); + const TRemoteConnectionWriterStatus& WriterGetStatus(); + virtual void WriterFillStatus(); + void WriterFillInFlight(); + virtual void BeforeTryWrite(); + void Act(TWriterTag); + void ScheduleRead(); + void ScheduleWrite(); + void ScheduleShutdownOnServerOrReconnectOnClient(EMessageStatus status, bool writer); + void ScheduleShutdown(EMessageStatus status); + void WriterFlushBuffer(); + void WriterFillBuffer(); + void ReaderSendStatus(TInstant now, bool force = false); + const TRemoteConnectionReaderStatus& ReaderFillStatus(); + void WriterRotateCounters(); + void WriterSendStatus(TInstant now, bool force = false); + void WriterSendStatusIfNecessary(TInstant now); + void QuotaReturnAside(size_t items, size_t bytes); + virtual void ReaderProcessMessageUnknownVersion(TArrayRef<const char> dataRef) = 0; + bool MessageRead(TArrayRef<const char> dataRef, TInstant now); + virtual void MessageSent(TArrayRef<TBusMessagePtrAndHeader> messages) = 0; + + void CallSerialize(TBusMessage* msg, TBuffer& buffer) const; + void SerializeMessage(TBusMessage* msg, TBuffer* data, TMessageCounter* counter) const; + TBusMessage* DeserializeMessage(TArrayRef<const char> dataRef, const TBusHeader* header, TMessageCounter* messageCounter, EMessageStatus* status) const; + + void ResetOneWayFlag(TArrayRef<TBusMessage*>); + + inline ::NActor::TActor<TRemoteConnection, TWriterTag>* GetWriterActor() { + return this; + } + inline ::NActor::TActor<TRemoteConnection, TReaderTag>* GetReaderActor() { + return this; + } + inline TScheduleActor<TRemoteConnection, TWriterTag>* GetWriterSchedulerActor() { + return this; + } + + void WriterErrorMessage(TNonDestroyingAutoPtr<TBusMessage> m, EMessageStatus status); + // takes ownership of ms + void WriterErrorMessages(const TArrayRef<TBusMessage*> ms, EMessageStatus status); + + void FireClientConnectionEvent(TClientConnectionEvent::EType); + + size_t GetInFlight(); + size_t GetConnectSyscallsNumForTest(); + + bool IsReturnConnectFailedImmediately() { + return (bool)AtomicGet(ReturnConnectFailedImmediately); + } + + bool IsAlive() const; + + TRemoteSessionPtr Session; + TBusProtocol* const Proto; + TBusSessionConfig const Config; + bool RemovedFromSession; + const ui64 ConnectionId; + const TNetAddr PeerAddr; + const TBusSocketAddr PeerAddrSocketAddr; + + const TInstant CreatedTime; + TInstant LastConnectAttempt; + TAtomic ReturnConnectFailedImmediately; + + protected: + ::NActor::TQueueForActor<TBusMessage*> BeforeSendQueue; + TLockFreeStack<TBusHeader> WrongVersionRequests; + + struct TWriterData { + TAtomic Down; + + NEventLoop::TChannelPtr Channel; + ui32 SocketVersion; + + TRemoteConnectionWriterStatus Status; + TInstant StatusLastSendTime; + + TLocalTasks TimeToRotateCounters; + + TAtomic InFlight; + + TTimedMessages SendQueue; + ui32 AwakeFlags; + EWriterState State; + TLeftRightBuffer Buffer; + TInstant CorkUntil; TSystemEvent ShutdownComplete; - void SetChannel(NEventLoop::TChannelPtr channel); - void DropChannel(); + void SetChannel(NEventLoop::TChannelPtr channel); + void DropChannel(); - TWriterData(); - ~TWriterData(); - }; + TWriterData(); + ~TWriterData(); + }; - struct TReaderData { - TAtomic Down; + struct TReaderData { + TAtomic Down; - NEventLoop::TChannelPtr Channel; - ui32 SocketVersion; + NEventLoop::TChannelPtr Channel; + ui32 SocketVersion; - TRemoteConnectionReaderStatus Status; - TInstant StatusLastSendTime; + TRemoteConnectionReaderStatus Status; + TInstant StatusLastSendTime; - TBuffer Buffer; - size_t Offset; /* offset in read buffer */ - size_t MoreBytes; /* more bytes required from socket */ - TVectorSwaps<TBusMessagePtrAndHeader> ReadMessages; + TBuffer Buffer; + size_t Offset; /* offset in read buffer */ + size_t MoreBytes; /* more bytes required from socket */ + TVectorSwaps<TBusMessagePtrAndHeader> ReadMessages; TSystemEvent ShutdownComplete; - bool BufferMore() const noexcept { - return MoreBytes > 0; - } + bool BufferMore() const noexcept { + return MoreBytes > 0; + } - bool HasBytesInBuf(size_t bytes) noexcept; - void SetChannel(NEventLoop::TChannelPtr channel); - void DropChannel(); + bool HasBytesInBuf(size_t bytes) noexcept; + void SetChannel(NEventLoop::TChannelPtr channel); + void DropChannel(); - TReaderData(); - ~TReaderData(); - }; + TReaderData(); + ~TReaderData(); + }; - // owned by session status actor - struct TGranStatus { - TGranStatus(TDuration gran) - : Writer(gran) - , Reader(gran) - { - } + // owned by session status actor + struct TGranStatus { + TGranStatus(TDuration gran) + : Writer(gran) + , Reader(gran) + { + } - TGranUp<TRemoteConnectionWriterStatus> Writer; - TGranUp<TRemoteConnectionReaderStatus> Reader; - }; + TGranUp<TRemoteConnectionWriterStatus> Writer; + TGranUp<TRemoteConnectionReaderStatus> Reader; + }; - TWriterData WriterData; - TReaderData ReaderData; - TGranStatus GranStatus; - TTokenQuota QuotaMsg; - TTokenQuota QuotaBytes; + TWriterData WriterData; + TReaderData ReaderData; + TGranStatus GranStatus; + TTokenQuota QuotaMsg; + TTokenQuota QuotaBytes; - size_t MaxBufferSize; + size_t MaxBufferSize; - // client connection only - TLockFreeQueueBatch<TBusMessagePtrAndHeader, TVectorSwaps> ReplyQueue; + // client connection only + TLockFreeQueueBatch<TBusMessagePtrAndHeader, TVectorSwaps> ReplyQueue; - EMessageStatus ShutdownReason; + EMessageStatus ShutdownReason; }; - inline const TNetAddr& TRemoteConnection::GetAddr() const noexcept { - return PeerAddr; - } + inline const TNetAddr& TRemoteConnection::GetAddr() const noexcept { + return PeerAddr; + } - typedef TIntrusivePtr<TRemoteConnection> TRemoteConnectionPtr; + typedef TIntrusivePtr<TRemoteConnection> TRemoteConnectionPtr; } -} +} diff --git a/library/cpp/messagebus/remote_connection_status.cpp b/library/cpp/messagebus/remote_connection_status.cpp index 05b47e281e..2c48b2a287 100644 --- a/library/cpp/messagebus/remote_connection_status.cpp +++ b/library/cpp/messagebus/remote_connection_status.cpp @@ -62,7 +62,7 @@ TRemoteConnectionStatusBase::TRemoteConnectionStatusBase() { } -TRemoteConnectionStatusBase& TRemoteConnectionStatusBase ::operator+=(const TRemoteConnectionStatusBase& that) { +TRemoteConnectionStatusBase& TRemoteConnectionStatusBase ::operator+=(const TRemoteConnectionStatusBase& that) { REMOTE_CONNECTION_STATUS_BASE_MAP(STRUCT_FIELD_ADD, ) return *this; } @@ -72,8 +72,8 @@ TRemoteConnectionIncrementalStatusBase::TRemoteConnectionIncrementalStatusBase() { } -TRemoteConnectionIncrementalStatusBase& TRemoteConnectionIncrementalStatusBase::operator+=( - const TRemoteConnectionIncrementalStatusBase& that) { +TRemoteConnectionIncrementalStatusBase& TRemoteConnectionIncrementalStatusBase::operator+=( + const TRemoteConnectionIncrementalStatusBase& that) { REMOTE_CONNECTION_INCREMENTAL_STATUS_BASE_MAP(STRUCT_FIELD_ADD, ) return *this; } @@ -84,8 +84,8 @@ TRemoteConnectionReaderIncrementalStatus::TRemoteConnectionReaderIncrementalStat } TRemoteConnectionReaderIncrementalStatus& TRemoteConnectionReaderIncrementalStatus::operator+=( - const TRemoteConnectionReaderIncrementalStatus& that) { - TRemoteConnectionIncrementalStatusBase::operator+=(that); + const TRemoteConnectionReaderIncrementalStatus& that) { + TRemoteConnectionIncrementalStatusBase::operator+=(that); REMOTE_CONNECTION_READER_INCREMENTAL_STATUS_MAP(STRUCT_FIELD_ADD, ) return *this; } @@ -96,7 +96,7 @@ TRemoteConnectionReaderStatus::TRemoteConnectionReaderStatus() } TRemoteConnectionReaderStatus& TRemoteConnectionReaderStatus::operator+=(const TRemoteConnectionReaderStatus& that) { - TRemoteConnectionStatusBase::operator+=(that); + TRemoteConnectionStatusBase::operator+=(that); REMOTE_CONNECTION_READER_STATUS_MAP(STRUCT_FIELD_ADD, ) return *this; } @@ -107,8 +107,8 @@ TRemoteConnectionWriterIncrementalStatus::TRemoteConnectionWriterIncrementalStat } TRemoteConnectionWriterIncrementalStatus& TRemoteConnectionWriterIncrementalStatus::operator+=( - const TRemoteConnectionWriterIncrementalStatus& that) { - TRemoteConnectionIncrementalStatusBase::operator+=(that); + const TRemoteConnectionWriterIncrementalStatus& that) { + TRemoteConnectionIncrementalStatusBase::operator+=(that); REMOTE_CONNECTION_WRITER_INCREMENTAL_STATUS(STRUCT_FIELD_ADD, ) return *this; } @@ -119,7 +119,7 @@ TRemoteConnectionWriterStatus::TRemoteConnectionWriterStatus() } TRemoteConnectionWriterStatus& TRemoteConnectionWriterStatus::operator+=(const TRemoteConnectionWriterStatus& that) { - TRemoteConnectionStatusBase::operator+=(that); + TRemoteConnectionStatusBase::operator+=(that); REMOTE_CONNECTION_WRITER_STATUS(STRUCT_FIELD_ADD, ) return *this; } @@ -162,9 +162,9 @@ TString TRemoteConnectionStatus::PrintToString() const { if (!Summary) { // TODO: print MyAddr too, but only if it is set ss << WriterStatus.PeerAddr << " (" << WriterStatus.ConnectionId << ")" - << ", writefd=" << WriterStatus.Fd - << ", readfd=" << ReaderStatus.Fd - << Endl; + << ", writefd=" << WriterStatus.Fd + << ", readfd=" << ReaderStatus.Fd + << Endl; if (WriterStatus.Connected) { p.AddRow("connect time", WriterStatus.ConnectTime.ToString()); p.AddRow("writer state", ToCString(WriterStatus.State)); @@ -182,7 +182,7 @@ TString TRemoteConnectionStatus::PrintToString() const { p.AddRow("send queue", LeftPad(WriterStatus.SendQueueSize, 6)); - if (Server) { + if (Server) { p.AddRow("quota msg", LeftPad(ReaderStatus.QuotaMsg, 6)); p.AddRow("quota bytes", LeftPad(ReaderStatus.QuotaBytes, 6)); p.AddRow("quota exhausted", LeftPad(ReaderStatus.QuotaExhausted, 6)); @@ -254,7 +254,7 @@ TString TSessionDumpStatus::PrintToString() const { return ss.Str(); } -TString TBusMessageQueueStatus::PrintToString() const { +TString TBusMessageQueueStatus::PrintToString() const { TStringStream ss; ss << "work queue:\n"; ss << ExecutorStatus.Status; diff --git a/library/cpp/messagebus/remote_connection_status.h b/library/cpp/messagebus/remote_connection_status.h index f98f1ff8e9..5db10e51ea 100644 --- a/library/cpp/messagebus/remote_connection_status.h +++ b/library/cpp/messagebus/remote_connection_status.h @@ -15,200 +15,200 @@ namespace NBus { class TConnectionStatusMonRecord; } -namespace NBus { - namespace NPrivate { +namespace NBus { + namespace NPrivate { #define WRITER_STATE_MAP(XX) \ - XX(WRITER_UNKNOWN) \ - XX(WRITER_FILLING) \ - XX(WRITER_FLUSHING) \ + XX(WRITER_UNKNOWN) \ + XX(WRITER_FILLING) \ + XX(WRITER_FLUSHING) \ /**/ - // TODO: move elsewhere - enum EWriterState { - WRITER_STATE_MAP(ENUM_VALUE_GEN_NO_VALUE) - }; + // TODO: move elsewhere + enum EWriterState { + WRITER_STATE_MAP(ENUM_VALUE_GEN_NO_VALUE) + }; - ENUM_TO_STRING(EWriterState, WRITER_STATE_MAP) + ENUM_TO_STRING(EWriterState, WRITER_STATE_MAP) #define STRUCT_FIELD_ADD(name, type, func) func(name, that.name); - template <typename T> - void Reset(T& t) { - t.~T(); - new (&t) T(); - } + template <typename T> + void Reset(T& t) { + t.~T(); + new (&t) T(); + } -#define DURATION_COUNTER_MAP(XX, comma) \ - XX(Count, unsigned, Add) \ - comma \ - XX(SumDuration, TDuration, Add) comma \ - XX(MaxDuration, TDuration, Max) /**/ +#define DURATION_COUNTER_MAP(XX, comma) \ + XX(Count, unsigned, Add) \ + comma \ + XX(SumDuration, TDuration, Add) comma \ + XX(MaxDuration, TDuration, Max) /**/ - struct TDurationCounter { - DURATION_COUNTER_MAP(STRUCT_FIELD_GEN, ) + struct TDurationCounter { + DURATION_COUNTER_MAP(STRUCT_FIELD_GEN, ) - TDuration AvgDuration() const; + TDuration AvgDuration() const; - TDurationCounter(); + TDurationCounter(); - void AddDuration(TDuration d) { - Count += 1; - SumDuration += d; - if (d > MaxDuration) { - MaxDuration = d; - } - } + void AddDuration(TDuration d) { + Count += 1; + SumDuration += d; + if (d > MaxDuration) { + MaxDuration = d; + } + } - TDurationCounter& operator+=(const TDurationCounter&); + TDurationCounter& operator+=(const TDurationCounter&); - TString ToString() const; - }; + TString ToString() const; + }; #define REMOTE_CONNECTION_STATUS_BASE_MAP(XX, comma) \ - XX(ConnectionId, ui64, AssertZero) \ - comma \ - XX(Fd, SOCKET, AssertZero) comma \ - XX(Acts, ui64, Add) comma \ - XX(BufferSize, ui64, Add) /**/ + XX(ConnectionId, ui64, AssertZero) \ + comma \ + XX(Fd, SOCKET, AssertZero) comma \ + XX(Acts, ui64, Add) comma \ + XX(BufferSize, ui64, Add) /**/ - struct TRemoteConnectionStatusBase { - REMOTE_CONNECTION_STATUS_BASE_MAP(STRUCT_FIELD_GEN, ) + struct TRemoteConnectionStatusBase { + REMOTE_CONNECTION_STATUS_BASE_MAP(STRUCT_FIELD_GEN, ) - TRemoteConnectionStatusBase& operator+=(const TRemoteConnectionStatusBase&); + TRemoteConnectionStatusBase& operator+=(const TRemoteConnectionStatusBase&); - TRemoteConnectionStatusBase(); - }; + TRemoteConnectionStatusBase(); + }; #define REMOTE_CONNECTION_INCREMENTAL_STATUS_BASE_MAP(XX, comma) \ - XX(BufferDrops, unsigned, Add) \ - comma \ - XX(NetworkOps, unsigned, Add) /**/ + XX(BufferDrops, unsigned, Add) \ + comma \ + XX(NetworkOps, unsigned, Add) /**/ - struct TRemoteConnectionIncrementalStatusBase { - REMOTE_CONNECTION_INCREMENTAL_STATUS_BASE_MAP(STRUCT_FIELD_GEN, ) + struct TRemoteConnectionIncrementalStatusBase { + REMOTE_CONNECTION_INCREMENTAL_STATUS_BASE_MAP(STRUCT_FIELD_GEN, ) - TRemoteConnectionIncrementalStatusBase& operator+=(const TRemoteConnectionIncrementalStatusBase&); + TRemoteConnectionIncrementalStatusBase& operator+=(const TRemoteConnectionIncrementalStatusBase&); - TRemoteConnectionIncrementalStatusBase(); - }; + TRemoteConnectionIncrementalStatusBase(); + }; #define REMOTE_CONNECTION_READER_INCREMENTAL_STATUS_MAP(XX, comma) \ - XX(MessageCounter, TMessageCounter, Add) \ - comma \ - XX(StatusCounter, TMessageStatusCounter, Add) /**/ + XX(MessageCounter, TMessageCounter, Add) \ + comma \ + XX(StatusCounter, TMessageStatusCounter, Add) /**/ - struct TRemoteConnectionReaderIncrementalStatus: public TRemoteConnectionIncrementalStatusBase { - REMOTE_CONNECTION_READER_INCREMENTAL_STATUS_MAP(STRUCT_FIELD_GEN, ) + struct TRemoteConnectionReaderIncrementalStatus: public TRemoteConnectionIncrementalStatusBase { + REMOTE_CONNECTION_READER_INCREMENTAL_STATUS_MAP(STRUCT_FIELD_GEN, ) - TRemoteConnectionReaderIncrementalStatus& operator+=(const TRemoteConnectionReaderIncrementalStatus&); + TRemoteConnectionReaderIncrementalStatus& operator+=(const TRemoteConnectionReaderIncrementalStatus&); - TRemoteConnectionReaderIncrementalStatus(); - }; + TRemoteConnectionReaderIncrementalStatus(); + }; #define REMOTE_CONNECTION_READER_STATUS_MAP(XX, comma) \ - XX(QuotaMsg, size_t, Add) \ - comma \ - XX(QuotaBytes, size_t, Add) comma \ - XX(QuotaExhausted, size_t, Add) comma \ - XX(Incremental, TRemoteConnectionReaderIncrementalStatus, Add) /**/ + XX(QuotaMsg, size_t, Add) \ + comma \ + XX(QuotaBytes, size_t, Add) comma \ + XX(QuotaExhausted, size_t, Add) comma \ + XX(Incremental, TRemoteConnectionReaderIncrementalStatus, Add) /**/ - struct TRemoteConnectionReaderStatus: public TRemoteConnectionStatusBase { - REMOTE_CONNECTION_READER_STATUS_MAP(STRUCT_FIELD_GEN, ) + struct TRemoteConnectionReaderStatus: public TRemoteConnectionStatusBase { + REMOTE_CONNECTION_READER_STATUS_MAP(STRUCT_FIELD_GEN, ) - TRemoteConnectionReaderStatus& operator+=(const TRemoteConnectionReaderStatus&); + TRemoteConnectionReaderStatus& operator+=(const TRemoteConnectionReaderStatus&); - TRemoteConnectionReaderStatus(); - }; + TRemoteConnectionReaderStatus(); + }; #define REMOTE_CONNECTION_WRITER_INCREMENTAL_STATUS(XX, comma) \ - XX(MessageCounter, TMessageCounter, Add) \ - comma \ - XX(StatusCounter, TMessageStatusCounter, Add) comma \ - XX(ProcessDurationHistogram, TDurationHistogram, Add) /**/ + XX(MessageCounter, TMessageCounter, Add) \ + comma \ + XX(StatusCounter, TMessageStatusCounter, Add) comma \ + XX(ProcessDurationHistogram, TDurationHistogram, Add) /**/ - struct TRemoteConnectionWriterIncrementalStatus: public TRemoteConnectionIncrementalStatusBase { - REMOTE_CONNECTION_WRITER_INCREMENTAL_STATUS(STRUCT_FIELD_GEN, ) + struct TRemoteConnectionWriterIncrementalStatus: public TRemoteConnectionIncrementalStatusBase { + REMOTE_CONNECTION_WRITER_INCREMENTAL_STATUS(STRUCT_FIELD_GEN, ) - TRemoteConnectionWriterIncrementalStatus& operator+=(const TRemoteConnectionWriterIncrementalStatus&); + TRemoteConnectionWriterIncrementalStatus& operator+=(const TRemoteConnectionWriterIncrementalStatus&); - TRemoteConnectionWriterIncrementalStatus(); - }; + TRemoteConnectionWriterIncrementalStatus(); + }; -#define REMOTE_CONNECTION_WRITER_STATUS(XX, comma) \ - XX(Connected, bool, AssertZero) \ - comma \ - XX(ConnectTime, TInstant, AssertZero) comma /* either connect time on client or accept time on server */ \ - XX(ConnectError, int, AssertZero) comma \ - XX(ConnectSyscalls, unsigned, Add) comma \ - XX(PeerAddr, TNetAddr, AssertZero) comma \ - XX(MyAddr, TNetAddr, AssertZero) comma \ - XX(State, EWriterState, AssertZero) comma \ - XX(SendQueueSize, size_t, Add) comma \ - XX(AckMessagesSize, size_t, Add) comma /* client only */ \ - XX(DurationCounter, TDurationCounter, Add) comma /* server only */ \ - XX(DurationCounterPrev, TDurationCounter, Add) comma /* server only */ \ - XX(Incremental, TRemoteConnectionWriterIncrementalStatus, Add) comma \ - XX(ReaderWakeups, size_t, Add) /**/ +#define REMOTE_CONNECTION_WRITER_STATUS(XX, comma) \ + XX(Connected, bool, AssertZero) \ + comma \ + XX(ConnectTime, TInstant, AssertZero) comma /* either connect time on client or accept time on server */ \ + XX(ConnectError, int, AssertZero) comma \ + XX(ConnectSyscalls, unsigned, Add) comma \ + XX(PeerAddr, TNetAddr, AssertZero) comma \ + XX(MyAddr, TNetAddr, AssertZero) comma \ + XX(State, EWriterState, AssertZero) comma \ + XX(SendQueueSize, size_t, Add) comma \ + XX(AckMessagesSize, size_t, Add) comma /* client only */ \ + XX(DurationCounter, TDurationCounter, Add) comma /* server only */ \ + XX(DurationCounterPrev, TDurationCounter, Add) comma /* server only */ \ + XX(Incremental, TRemoteConnectionWriterIncrementalStatus, Add) comma \ + XX(ReaderWakeups, size_t, Add) /**/ - struct TRemoteConnectionWriterStatus: public TRemoteConnectionStatusBase { - REMOTE_CONNECTION_WRITER_STATUS(STRUCT_FIELD_GEN, ) + struct TRemoteConnectionWriterStatus: public TRemoteConnectionStatusBase { + REMOTE_CONNECTION_WRITER_STATUS(STRUCT_FIELD_GEN, ) - TRemoteConnectionWriterStatus(); + TRemoteConnectionWriterStatus(); - TRemoteConnectionWriterStatus& operator+=(const TRemoteConnectionWriterStatus&); + TRemoteConnectionWriterStatus& operator+=(const TRemoteConnectionWriterStatus&); - size_t GetInFlight() const; - }; + size_t GetInFlight() const; + }; #define REMOTE_CONNECTION_STATUS_MAP(XX, comma) \ - XX(Summary, bool) \ - comma \ - XX(Server, bool) /**/ - - struct TRemoteConnectionStatus { - REMOTE_CONNECTION_STATUS_MAP(STRUCT_FIELD_GEN, ) - - TRemoteConnectionReaderStatus ReaderStatus; - TRemoteConnectionWriterStatus WriterStatus; - - TRemoteConnectionStatus(); - - TString PrintToString() const; - TConnectionStatusMonRecord GetStatusProtobuf() const; - }; - - struct TBusSessionStatus { - size_t InFlightCount; - size_t InFlightSize; - bool InputPaused; - - TBusSessionStatus(); - }; - - struct TSessionDumpStatus { - bool Shutdown; - TString Head; - TString Acceptors; - TString ConnectionsSummary; - TString Connections; - TBusSessionStatus Status; - TRemoteConnectionStatus ConnectionStatusSummary; - TBusSessionConfig Config; - - TSessionDumpStatus() - : Shutdown(false) - { - } - - TString PrintToString() const; - }; - - // without sessions - struct TBusMessageQueueStatus { - NActor::NPrivate::TExecutorStatus ExecutorStatus; - TBusQueueConfig Config; - - TString PrintToString() const; - }; - } -} + XX(Summary, bool) \ + comma \ + XX(Server, bool) /**/ + + struct TRemoteConnectionStatus { + REMOTE_CONNECTION_STATUS_MAP(STRUCT_FIELD_GEN, ) + + TRemoteConnectionReaderStatus ReaderStatus; + TRemoteConnectionWriterStatus WriterStatus; + + TRemoteConnectionStatus(); + + TString PrintToString() const; + TConnectionStatusMonRecord GetStatusProtobuf() const; + }; + + struct TBusSessionStatus { + size_t InFlightCount; + size_t InFlightSize; + bool InputPaused; + + TBusSessionStatus(); + }; + + struct TSessionDumpStatus { + bool Shutdown; + TString Head; + TString Acceptors; + TString ConnectionsSummary; + TString Connections; + TBusSessionStatus Status; + TRemoteConnectionStatus ConnectionStatusSummary; + TBusSessionConfig Config; + + TSessionDumpStatus() + : Shutdown(false) + { + } + + TString PrintToString() const; + }; + + // without sessions + struct TBusMessageQueueStatus { + NActor::NPrivate::TExecutorStatus ExecutorStatus; + TBusQueueConfig Config; + + TString PrintToString() const; + }; + } +} diff --git a/library/cpp/messagebus/remote_server_connection.cpp b/library/cpp/messagebus/remote_server_connection.cpp index 17df4dd4a3..74be34ded9 100644 --- a/library/cpp/messagebus/remote_server_connection.cpp +++ b/library/cpp/messagebus/remote_server_connection.cpp @@ -15,7 +15,7 @@ TRemoteServerConnection::TRemoteServerConnection(TRemoteServerSessionPtr session { } -void TRemoteServerConnection::Init(SOCKET socket, TInstant now) { +void TRemoteServerConnection::Init(SOCKET socket, TInstant now) { WriterData.Status.ConnectTime = now; WriterData.Status.Connected = true; diff --git a/library/cpp/messagebus/remote_server_connection.h b/library/cpp/messagebus/remote_server_connection.h index 925338f3ff..63d7f20646 100644 --- a/library/cpp/messagebus/remote_server_connection.h +++ b/library/cpp/messagebus/remote_server_connection.h @@ -4,29 +4,29 @@ #include <util/generic/object_counter.h> -namespace NBus { - namespace NPrivate { - class TRemoteServerConnection: public TRemoteConnection { - friend struct TBusSessionImpl; - friend class TRemoteServerSession; +namespace NBus { + namespace NPrivate { + class TRemoteServerConnection: public TRemoteConnection { + friend struct TBusSessionImpl; + friend class TRemoteServerSession; - TObjectCounter<TRemoteServerConnection> ObjectCounter; + TObjectCounter<TRemoteServerConnection> ObjectCounter; - public: - TRemoteServerConnection(TRemoteServerSessionPtr session, ui64 id, TNetAddr addr); + public: + TRemoteServerConnection(TRemoteServerSessionPtr session, ui64 id, TNetAddr addr); - void Init(SOCKET socket, TInstant now); + void Init(SOCKET socket, TInstant now); - inline TRemoteServerSession* GetSession(); + inline TRemoteServerSession* GetSession(); - void HandleEvent(SOCKET socket, void* cookie) override; + void HandleEvent(SOCKET socket, void* cookie) override; - bool NeedInterruptRead() override; + bool NeedInterruptRead() override; - void MessageSent(TArrayRef<TBusMessagePtrAndHeader> messages) override; + void MessageSent(TArrayRef<TBusMessagePtrAndHeader> messages) override; - void ReaderProcessMessageUnknownVersion(TArrayRef<const char> dataRef) override; - }; + void ReaderProcessMessageUnknownVersion(TArrayRef<const char> dataRef) override; + }; - } -} + } +} diff --git a/library/cpp/messagebus/remote_server_session.cpp b/library/cpp/messagebus/remote_server_session.cpp index 0bc2853e94..6abbf88a60 100644 --- a/library/cpp/messagebus/remote_server_session.cpp +++ b/library/cpp/messagebus/remote_server_session.cpp @@ -16,47 +16,47 @@ using namespace NBus; using namespace NBus::NPrivate; TRemoteServerSession::TRemoteServerSession(TBusMessageQueue* queue, - TBusProtocol* proto, IBusServerHandler* handler, - const TBusServerSessionConfig& config, const TString& name) + TBusProtocol* proto, IBusServerHandler* handler, + const TBusServerSessionConfig& config, const TString& name) : TBusSessionImpl(false, queue, proto, handler, config, name) , ServerOwnedMessages(config.MaxInFlight, config.MaxInFlightBySize, "ServerOwnedMessages") , ServerHandler(handler) { - if (config.PerConnectionMaxInFlightBySize > 0) { - if (config.PerConnectionMaxInFlightBySize < config.MaxMessageSize) + if (config.PerConnectionMaxInFlightBySize > 0) { + if (config.PerConnectionMaxInFlightBySize < config.MaxMessageSize) ythrow yexception() << "too low PerConnectionMaxInFlightBySize value"; } } -namespace NBus { - namespace NPrivate { - class TInvokeOnMessage: public IWorkItem { - private: - TRemoteServerSession* RemoteServerSession; - TBusMessagePtrAndHeader Request; - TIntrusivePtr<TRemoteServerConnection> Connection; +namespace NBus { + namespace NPrivate { + class TInvokeOnMessage: public IWorkItem { + private: + TRemoteServerSession* RemoteServerSession; + TBusMessagePtrAndHeader Request; + TIntrusivePtr<TRemoteServerConnection> Connection; + + public: + TInvokeOnMessage(TRemoteServerSession* session, TBusMessagePtrAndHeader& request, TIntrusivePtr<TRemoteServerConnection>& connection) + : RemoteServerSession(session) + { + Y_ASSERT(!!connection); + Connection.Swap(connection); + + Request.Swap(request); + } + + void DoWork() override { + THolder<TInvokeOnMessage> holder(this); + RemoteServerSession->InvokeOnMessage(Request, Connection); + // TODO: TRemoteServerSessionSemaphore should be enough + RemoteServerSession->JobCount.Decrement(); + } + }; - public: - TInvokeOnMessage(TRemoteServerSession* session, TBusMessagePtrAndHeader& request, TIntrusivePtr<TRemoteServerConnection>& connection) - : RemoteServerSession(session) - { - Y_ASSERT(!!connection); - Connection.Swap(connection); - - Request.Swap(request); - } - - void DoWork() override { - THolder<TInvokeOnMessage> holder(this); - RemoteServerSession->InvokeOnMessage(Request, Connection); - // TODO: TRemoteServerSessionSemaphore should be enough - RemoteServerSession->JobCount.Decrement(); - } - }; - - } -} + } +} void TRemoteServerSession::OnMessageReceived(TRemoteConnection* c, TVectorSwaps<TBusMessagePtrAndHeader>& messages) { AcquireInWorkRequests(messages); @@ -165,7 +165,7 @@ void TRemoteServerSession::ReleaseInWorkResponses(TArrayRef<const TBusMessagePtr ServerOwnedMessages.ReleaseMultiple(responses.size(), size); } -void TRemoteServerSession::ReleaseInWorkRequests(TRemoteConnection& con, TBusMessage* request) { +void TRemoteServerSession::ReleaseInWorkRequests(TRemoteConnection& con, TBusMessage* request) { Y_ASSERT((request->LocalFlags & MESSAGE_IN_WORK)); request->LocalFlags &= ~MESSAGE_IN_WORK; @@ -175,7 +175,7 @@ void TRemoteServerSession::ReleaseInWorkRequests(TRemoteConnection& con, TBusMes ServerOwnedMessages.ReleaseMultiple(1, size); } -void TRemoteServerSession::ReleaseInWork(TBusIdentity& ident) { +void TRemoteServerSession::ReleaseInWork(TBusIdentity& ident) { ident.SetInWork(false); ident.Connection->QuotaReturnAside(1, ident.Size); diff --git a/library/cpp/messagebus/remote_server_session.h b/library/cpp/messagebus/remote_server_session.h index 5de07c4e59..f5c266a7f7 100644 --- a/library/cpp/messagebus/remote_server_session.h +++ b/library/cpp/messagebus/remote_server_session.h @@ -5,50 +5,50 @@ #ifdef _MSC_VER #pragma warning(push) -#pragma warning(disable : 4250) // 'NBus::NPrivate::TRemoteClientSession' : inherits 'NBus::NPrivate::TBusSessionImpl::NBus::NPrivate::TBusSessionImpl::GetConfig' via dominance +#pragma warning(disable : 4250) // 'NBus::NPrivate::TRemoteClientSession' : inherits 'NBus::NPrivate::TBusSessionImpl::NBus::NPrivate::TBusSessionImpl::GetConfig' via dominance #endif -namespace NBus { - namespace NPrivate { - class TRemoteServerSession: public TBusServerSession, public TBusSessionImpl { - friend class TRemoteServerConnection; +namespace NBus { + namespace NPrivate { + class TRemoteServerSession: public TBusServerSession, public TBusSessionImpl { + friend class TRemoteServerConnection; - private: - TObjectCounter<TRemoteServerSession> ObjectCounter; + private: + TObjectCounter<TRemoteServerSession> ObjectCounter; - TRemoteServerSessionSemaphore ServerOwnedMessages; - IBusServerHandler* const ServerHandler; + TRemoteServerSessionSemaphore ServerOwnedMessages; + IBusServerHandler* const ServerHandler; - public: - TRemoteServerSession(TBusMessageQueue* queue, TBusProtocol* proto, - IBusServerHandler* handler, - const TBusSessionConfig& config, const TString& name); + public: + TRemoteServerSession(TBusMessageQueue* queue, TBusProtocol* proto, + IBusServerHandler* handler, + const TBusSessionConfig& config, const TString& name); - void OnMessageReceived(TRemoteConnection* c, TVectorSwaps<TBusMessagePtrAndHeader>& newMsg) override; - void InvokeOnMessage(TBusMessagePtrAndHeader& request, TIntrusivePtr<TRemoteServerConnection>& conn); + void OnMessageReceived(TRemoteConnection* c, TVectorSwaps<TBusMessagePtrAndHeader>& newMsg) override; + void InvokeOnMessage(TBusMessagePtrAndHeader& request, TIntrusivePtr<TRemoteServerConnection>& conn); - EMessageStatus SendReply(const TBusIdentity& ident, TBusMessage* pRep) override; + EMessageStatus SendReply(const TBusIdentity& ident, TBusMessage* pRep) override; - EMessageStatus ForgetRequest(const TBusIdentity& ident) override; + EMessageStatus ForgetRequest(const TBusIdentity& ident) override; - int GetInFlight() const noexcept override; - void FillStatus() override; + int GetInFlight() const noexcept override; + void FillStatus() override; - void Shutdown() override; + void Shutdown() override; - void PauseInput(bool pause) override; - unsigned GetActualListenPort() override; + void PauseInput(bool pause) override; + unsigned GetActualListenPort() override; - void AcquireInWorkRequests(TArrayRef<const TBusMessagePtrAndHeader> requests); - void ReleaseInWorkResponses(TArrayRef<const TBusMessagePtrAndHeader> responses); - void ReleaseInWorkRequests(TRemoteConnection&, TBusMessage*); - void ReleaseInWork(TBusIdentity&); - void ConvertInWork(TBusIdentity& req, TBusMessage* reply); - }; + void AcquireInWorkRequests(TArrayRef<const TBusMessagePtrAndHeader> requests); + void ReleaseInWorkResponses(TArrayRef<const TBusMessagePtrAndHeader> responses); + void ReleaseInWorkRequests(TRemoteConnection&, TBusMessage*); + void ReleaseInWork(TBusIdentity&); + void ConvertInWork(TBusIdentity& req, TBusMessage* reply); + }; #ifdef _MSC_VER #pragma warning(pop) #endif - } -} + } +} diff --git a/library/cpp/messagebus/remote_server_session_semaphore.cpp b/library/cpp/messagebus/remote_server_session_semaphore.cpp index 3287c869c8..6094a3586e 100644 --- a/library/cpp/messagebus/remote_server_session_semaphore.cpp +++ b/library/cpp/messagebus/remote_server_session_semaphore.cpp @@ -7,7 +7,7 @@ using namespace NBus; using namespace NBus::NPrivate; TRemoteServerSessionSemaphore::TRemoteServerSessionSemaphore( - TAtomicBase limitCount, TAtomicBase limitSize, const char* name) + TAtomicBase limitCount, TAtomicBase limitSize, const char* name) : Name(name) , LimitCount(limitCount) , LimitSize(limitSize) @@ -20,7 +20,7 @@ TRemoteServerSessionSemaphore::TRemoteServerSessionSemaphore( Y_UNUSED(Name); } -TRemoteServerSessionSemaphore::~TRemoteServerSessionSemaphore() { +TRemoteServerSessionSemaphore::~TRemoteServerSessionSemaphore() { Y_VERIFY(AtomicGet(CurrentCount) == 0); // TODO: fix spider and enable //Y_VERIFY(AtomicGet(CurrentSize) == 0); diff --git a/library/cpp/messagebus/remote_server_session_semaphore.h b/library/cpp/messagebus/remote_server_session_semaphore.h index 32b0f27acc..de714fd342 100644 --- a/library/cpp/messagebus/remote_server_session_semaphore.h +++ b/library/cpp/messagebus/remote_server_session_semaphore.h @@ -4,39 +4,39 @@ #include <util/generic/noncopyable.h> -namespace NBus { - namespace NPrivate { - class TRemoteServerSessionSemaphore: public TComplexConditionSemaphore<TRemoteServerSessionSemaphore> { - private: - const char* const Name; - - TAtomicBase const LimitCount; - TAtomicBase const LimitSize; - TAtomic CurrentCount; - TAtomic CurrentSize; - TAtomic PausedByUser; - TAtomic StopSignal; - - public: - TRemoteServerSessionSemaphore(TAtomicBase limitCount, TAtomicBase limitSize, const char* name = "unnamed"); - ~TRemoteServerSessionSemaphore(); - - TAtomicBase GetCurrentCount() const { - return AtomicGet(CurrentCount); - } - TAtomicBase GetCurrentSize() const { - return AtomicGet(CurrentSize); - } - - void IncrementMultiple(TAtomicBase count, TAtomicBase size); - bool TryWait(); - void ReleaseMultiple(TAtomicBase count, TAtomicBase size); - void Stop(); - void PauseByUsed(bool pause); - - private: - void CheckNeedToUnlock(); - }; - - } -} +namespace NBus { + namespace NPrivate { + class TRemoteServerSessionSemaphore: public TComplexConditionSemaphore<TRemoteServerSessionSemaphore> { + private: + const char* const Name; + + TAtomicBase const LimitCount; + TAtomicBase const LimitSize; + TAtomic CurrentCount; + TAtomic CurrentSize; + TAtomic PausedByUser; + TAtomic StopSignal; + + public: + TRemoteServerSessionSemaphore(TAtomicBase limitCount, TAtomicBase limitSize, const char* name = "unnamed"); + ~TRemoteServerSessionSemaphore(); + + TAtomicBase GetCurrentCount() const { + return AtomicGet(CurrentCount); + } + TAtomicBase GetCurrentSize() const { + return AtomicGet(CurrentSize); + } + + void IncrementMultiple(TAtomicBase count, TAtomicBase size); + bool TryWait(); + void ReleaseMultiple(TAtomicBase count, TAtomicBase size); + void Stop(); + void PauseByUsed(bool pause); + + private: + void CheckNeedToUnlock(); + }; + + } +} diff --git a/library/cpp/messagebus/scheduler/scheduler.cpp b/library/cpp/messagebus/scheduler/scheduler.cpp index f4122e6f9f..5a5fe52894 100644 --- a/library/cpp/messagebus/scheduler/scheduler.cpp +++ b/library/cpp/messagebus/scheduler/scheduler.cpp @@ -11,14 +11,14 @@ using namespace NBus::NPrivate; class TScheduleDeadlineCompare { public: - bool operator()(const IScheduleItemAutoPtr& i1, const IScheduleItemAutoPtr& i2) const noexcept { + bool operator()(const IScheduleItemAutoPtr& i1, const IScheduleItemAutoPtr& i2) const noexcept { return i1->GetScheduleTime() > i2->GetScheduleTime(); } }; TScheduler::TScheduler() : StopThread(false) - , Thread([&] { this->SchedulerThread(); }) + , Thread([&] { this->SchedulerThread(); }) { } diff --git a/library/cpp/messagebus/scheduler/scheduler.h b/library/cpp/messagebus/scheduler/scheduler.h index 0a2d1228c2..afcc0de55d 100644 --- a/library/cpp/messagebus/scheduler/scheduler.h +++ b/library/cpp/messagebus/scheduler/scheduler.h @@ -11,58 +11,58 @@ #include <util/system/mutex.h> #include <util/system/thread.h> -namespace NBus { - namespace NPrivate { - class IScheduleItem { - public: - inline IScheduleItem(TInstant scheduleTime) noexcept; - virtual ~IScheduleItem() { - } +namespace NBus { + namespace NPrivate { + class IScheduleItem { + public: + inline IScheduleItem(TInstant scheduleTime) noexcept; + virtual ~IScheduleItem() { + } - virtual void Do() = 0; - inline TInstant GetScheduleTime() const noexcept; + virtual void Do() = 0; + inline TInstant GetScheduleTime() const noexcept; - private: - TInstant ScheduleTime; - }; + private: + TInstant ScheduleTime; + }; - using IScheduleItemAutoPtr = TAutoPtr<IScheduleItem>; + using IScheduleItemAutoPtr = TAutoPtr<IScheduleItem>; - class TScheduler { - public: - TScheduler(); - ~TScheduler(); - void Stop(); - void Schedule(TAutoPtr<IScheduleItem> i); + class TScheduler { + public: + TScheduler(); + ~TScheduler(); + void Stop(); + void Schedule(TAutoPtr<IScheduleItem> i); - size_t Size() const; + size_t Size() const; - private: - void SchedulerThread(); + private: + void SchedulerThread(); - void FillNextItem(); + void FillNextItem(); - private: - TVector<IScheduleItemAutoPtr> Items; - IScheduleItemAutoPtr NextItem; - typedef TMutex TLock; - TLock Lock; - TCondVar CondVar; + private: + TVector<IScheduleItemAutoPtr> Items; + IScheduleItemAutoPtr NextItem; + typedef TMutex TLock; + TLock Lock; + TCondVar CondVar; - TObjectCounter<TScheduler> ObjectCounter; + TObjectCounter<TScheduler> ObjectCounter; - bool StopThread; - NThreading::TLegacyFuture<> Thread; - }; + bool StopThread; + NThreading::TLegacyFuture<> Thread; + }; - inline IScheduleItem::IScheduleItem(TInstant scheduleTime) noexcept - : ScheduleTime(scheduleTime) - { - } + inline IScheduleItem::IScheduleItem(TInstant scheduleTime) noexcept + : ScheduleTime(scheduleTime) + { + } - inline TInstant IScheduleItem::GetScheduleTime() const noexcept { - return ScheduleTime; - } + inline TInstant IScheduleItem::GetScheduleTime() const noexcept { + return ScheduleTime; + } - } + } } diff --git a/library/cpp/messagebus/scheduler/scheduler_ut.cpp b/library/cpp/messagebus/scheduler/scheduler_ut.cpp index f6d69ceffb..a5ea641c10 100644 --- a/library/cpp/messagebus/scheduler/scheduler_ut.cpp +++ b/library/cpp/messagebus/scheduler/scheduler_ut.cpp @@ -14,8 +14,8 @@ Y_UNIT_TEST_SUITE(TSchedulerTests) { TSimpleScheduleItem(TTestSync* testSync) : IScheduleItem((TInstant::Now() + TDuration::MilliSeconds(1))) , TestSync(testSync) - { - } + { + } void Do() override { TestSync->WaitForAndIncrement(0); diff --git a/library/cpp/messagebus/scheduler_actor.h b/library/cpp/messagebus/scheduler_actor.h index 4176ea45a9..d0c23c94c4 100644 --- a/library/cpp/messagebus/scheduler_actor.h +++ b/library/cpp/messagebus/scheduler_actor.h @@ -8,78 +8,78 @@ #include <util/system/mutex.h> -namespace NBus { - namespace NPrivate { - template <typename TThis, typename TTag = NActor::TDefaultTag> - class TScheduleActor { - typedef NActor::TActor<TThis, TTag> TActorForMe; - - private: - TScheduler* const Scheduler; - - TMutex Mutex; - - TInstant ScheduleTime; - - public: - TLocalTasks Alarm; - - private: - struct TScheduleItemImpl: public IScheduleItem { - TIntrusivePtr<TThis> Thiz; - - TScheduleItemImpl(TIntrusivePtr<TThis> thiz, TInstant when) - : IScheduleItem(when) - , Thiz(thiz) - { - } - - void Do() override { - { - TWhatThreadDoesAcquireGuard<TMutex> guard(Thiz->Mutex, "scheduler actor: acquiring lock for Do"); - - if (Thiz->ScheduleTime == TInstant::Max()) { - // was already fired - return; - } - - Thiz->ScheduleTime = TInstant::Max(); - } - - Thiz->Alarm.AddTask(); - Thiz->GetActorForMe()->Schedule(); - } - }; - - public: - TScheduleActor(TScheduler* scheduler) - : Scheduler(scheduler) - , ScheduleTime(TInstant::Max()) +namespace NBus { + namespace NPrivate { + template <typename TThis, typename TTag = NActor::TDefaultTag> + class TScheduleActor { + typedef NActor::TActor<TThis, TTag> TActorForMe; + + private: + TScheduler* const Scheduler; + + TMutex Mutex; + + TInstant ScheduleTime; + + public: + TLocalTasks Alarm; + + private: + struct TScheduleItemImpl: public IScheduleItem { + TIntrusivePtr<TThis> Thiz; + + TScheduleItemImpl(TIntrusivePtr<TThis> thiz, TInstant when) + : IScheduleItem(when) + , Thiz(thiz) + { + } + + void Do() override { + { + TWhatThreadDoesAcquireGuard<TMutex> guard(Thiz->Mutex, "scheduler actor: acquiring lock for Do"); + + if (Thiz->ScheduleTime == TInstant::Max()) { + // was already fired + return; + } + + Thiz->ScheduleTime = TInstant::Max(); + } + + Thiz->Alarm.AddTask(); + Thiz->GetActorForMe()->Schedule(); + } + }; + + public: + TScheduleActor(TScheduler* scheduler) + : Scheduler(scheduler) + , ScheduleTime(TInstant::Max()) { - } - - /// call Act(TTag) at specified time, unless it is already scheduled at earlier time. - void ScheduleAt(TInstant when) { - TWhatThreadDoesAcquireGuard<TMutex> guard(Mutex, "scheduler: acquiring lock for ScheduleAt"); - - if (when > ScheduleTime) { - // already scheduled + } + + /// call Act(TTag) at specified time, unless it is already scheduled at earlier time. + void ScheduleAt(TInstant when) { + TWhatThreadDoesAcquireGuard<TMutex> guard(Mutex, "scheduler: acquiring lock for ScheduleAt"); + + if (when > ScheduleTime) { + // already scheduled return; } - ScheduleTime = when; - Scheduler->Schedule(new TScheduleItemImpl(GetThis(), when)); + ScheduleTime = when; + Scheduler->Schedule(new TScheduleItemImpl(GetThis(), when)); } - private: - TThis* GetThis() { - return static_cast<TThis*>(this); - } + private: + TThis* GetThis() { + return static_cast<TThis*>(this); + } - TActorForMe* GetActorForMe() { - return static_cast<TActorForMe*>(GetThis()); - } - }; + TActorForMe* GetActorForMe() { + return static_cast<TActorForMe*>(GetThis()); + } + }; } -} +} diff --git a/library/cpp/messagebus/scheduler_actor_ut.cpp b/library/cpp/messagebus/scheduler_actor_ut.cpp index e1f1a95f0b..e81ffd3186 100644 --- a/library/cpp/messagebus/scheduler_actor_ut.cpp +++ b/library/cpp/messagebus/scheduler_actor_ut.cpp @@ -8,15 +8,15 @@ using namespace NBus::NPrivate; using namespace NActor; Y_UNIT_TEST_SUITE(TSchedulerActorTests) { - struct TMyActor: public TAtomicRefCount<TMyActor>, public TActor<TMyActor>, public TScheduleActor<TMyActor> { + struct TMyActor: public TAtomicRefCount<TMyActor>, public TActor<TMyActor>, public TScheduleActor<TMyActor> { TTestSync TestSync; TMyActor(TExecutor* executor, TScheduler* scheduler) : TActor<TMyActor>(executor) , TScheduleActor<TMyActor>(scheduler) , Iteration(0) - { - } + { + } unsigned Iteration; diff --git a/library/cpp/messagebus/session.cpp b/library/cpp/messagebus/session.cpp index 772984a086..46a7ece6a8 100644 --- a/library/cpp/messagebus/session.cpp +++ b/library/cpp/messagebus/session.cpp @@ -5,126 +5,126 @@ using namespace NBus; namespace NBus { - TBusSession::TBusSession() { - } - - //////////////////////////////////////////////////////////////////// - /// \brief Adds peer of connection into connection list - - int CompareByHost(const IRemoteAddr& l, const IRemoteAddr& r) noexcept { - if (l.Addr()->sa_family != r.Addr()->sa_family) { - return l.Addr()->sa_family < r.Addr()->sa_family ? -1 : +1; - } - - switch (l.Addr()->sa_family) { - case AF_INET: { - return memcmp(&(((const sockaddr_in*)l.Addr())->sin_addr), &(((const sockaddr_in*)r.Addr())->sin_addr), sizeof(in_addr)); - } - - case AF_INET6: { - return memcmp(&(((const sockaddr_in6*)l.Addr())->sin6_addr), &(((const sockaddr_in6*)r.Addr())->sin6_addr), sizeof(in6_addr)); - } + TBusSession::TBusSession() { + } + + //////////////////////////////////////////////////////////////////// + /// \brief Adds peer of connection into connection list + + int CompareByHost(const IRemoteAddr& l, const IRemoteAddr& r) noexcept { + if (l.Addr()->sa_family != r.Addr()->sa_family) { + return l.Addr()->sa_family < r.Addr()->sa_family ? -1 : +1; + } + + switch (l.Addr()->sa_family) { + case AF_INET: { + return memcmp(&(((const sockaddr_in*)l.Addr())->sin_addr), &(((const sockaddr_in*)r.Addr())->sin_addr), sizeof(in_addr)); + } + + case AF_INET6: { + return memcmp(&(((const sockaddr_in6*)l.Addr())->sin6_addr), &(((const sockaddr_in6*)r.Addr())->sin6_addr), sizeof(in6_addr)); + } } - return memcmp(l.Addr(), r.Addr(), Min<size_t>(l.Len(), r.Len())); + return memcmp(l.Addr(), r.Addr(), Min<size_t>(l.Len(), r.Len())); + } + + bool operator<(const TNetAddr& a1, const TNetAddr& a2) { + return CompareByHost(a1, a2) < 0; + } + + size_t TBusSession::GetInFlight(const TNetAddr& addr) const { + size_t r; + GetInFlightBulk({addr}, MakeArrayRef(&r, 1)); + return r; } - bool operator<(const TNetAddr& a1, const TNetAddr& a2) { - return CompareByHost(a1, a2) < 0; - } - - size_t TBusSession::GetInFlight(const TNetAddr& addr) const { - size_t r; - GetInFlightBulk({addr}, MakeArrayRef(&r, 1)); - return r; - } - - size_t TBusSession::GetConnectSyscallsNumForTest(const TNetAddr& addr) const { - size_t r; - GetConnectSyscallsNumBulkForTest({addr}, MakeArrayRef(&r, 1)); - return r; - } - - // Split 'host' into name and port taking into account that host can be specified - // as ipv6 address ('[<ipv6 address]:port' notion). - bool SplitHost(const TString& host, TString* hostName, TString* portNum) { - hostName->clear(); - portNum->clear(); - - // Simple check that we have to deal with ipv6 address specification or - // just host name or ipv4 address. + size_t TBusSession::GetConnectSyscallsNumForTest(const TNetAddr& addr) const { + size_t r; + GetConnectSyscallsNumBulkForTest({addr}, MakeArrayRef(&r, 1)); + return r; + } + + // Split 'host' into name and port taking into account that host can be specified + // as ipv6 address ('[<ipv6 address]:port' notion). + bool SplitHost(const TString& host, TString* hostName, TString* portNum) { + hostName->clear(); + portNum->clear(); + + // Simple check that we have to deal with ipv6 address specification or + // just host name or ipv4 address. if (!host.empty() && (host[0] == '[')) { - size_t pos = host.find(']'); - if (pos < 2 || pos == TString::npos) { - // '[]' and '[<address>' are errors. - return false; - } - - *hostName = host.substr(1, pos - 1); - - pos++; - if (pos != host.length()) { - if (host[pos] != ':') { - // Do not allow '[...]a' but '[...]:' is ok (as for ipv4 before - return false; - } - - *portNum = host.substr(pos + 1); + size_t pos = host.find(']'); + if (pos < 2 || pos == TString::npos) { + // '[]' and '[<address>' are errors. + return false; } - } else { - size_t pos = host.find(':'); - if (pos != TString::npos) { - if (pos == 0) { - // Treat ':<port>' as errors but allow or '<host>:' for compatibility. - return false; - } - - *portNum = host.substr(pos + 1); + + *hostName = host.substr(1, pos - 1); + + pos++; + if (pos != host.length()) { + if (host[pos] != ':') { + // Do not allow '[...]a' but '[...]:' is ok (as for ipv4 before + return false; + } + + *portNum = host.substr(pos + 1); + } + } else { + size_t pos = host.find(':'); + if (pos != TString::npos) { + if (pos == 0) { + // Treat ':<port>' as errors but allow or '<host>:' for compatibility. + return false; + } + + *portNum = host.substr(pos + 1); } - *hostName = host.substr(0, pos); + *hostName = host.substr(0, pos); } - return true; + return true; } - /// registers external session on host:port with locator service - int TBusSession::RegisterService(const char* host, TBusKey start /*= YBUS_KEYMIN*/, TBusKey end /*= YBUS_KEYMAX*/, EIpVersion ipVersion) { - TString hostName; - TString port; - int portNum; + /// registers external session on host:port with locator service + int TBusSession::RegisterService(const char* host, TBusKey start /*= YBUS_KEYMIN*/, TBusKey end /*= YBUS_KEYMAX*/, EIpVersion ipVersion) { + TString hostName; + TString port; + int portNum; - if (!SplitHost(host, &hostName, &port)) { - hostName = host; - } + if (!SplitHost(host, &hostName, &port)) { + hostName = host; + } if (port.empty()) { - portNum = GetProto()->GetPort(); - } else { - try { - portNum = FromString<int>(port); - } catch (const TFromStringException&) { - return -1; - } - } - - TBusService service = GetProto()->GetService(); + portNum = GetProto()->GetPort(); + } else { + try { + portNum = FromString<int>(port); + } catch (const TFromStringException&) { + return -1; + } + } + + TBusService service = GetProto()->GetService(); return GetQueue()->GetLocator()->Register(service, hostName.data(), portNum, start, end, ipVersion); } - TBusSession::~TBusSession() { + TBusSession::~TBusSession() { } } -TBusClientSessionPtr TBusClientSession::Create(TBusProtocol* proto, IBusClientHandler* handler, const TBusClientSessionConfig& config, TBusMessageQueuePtr queue) { +TBusClientSessionPtr TBusClientSession::Create(TBusProtocol* proto, IBusClientHandler* handler, const TBusClientSessionConfig& config, TBusMessageQueuePtr queue) { return queue->CreateSource(proto, handler, config); } -TBusServerSessionPtr TBusServerSession::Create(TBusProtocol* proto, IBusServerHandler* handler, const TBusServerSessionConfig& config, TBusMessageQueuePtr queue) { +TBusServerSessionPtr TBusServerSession::Create(TBusProtocol* proto, IBusServerHandler* handler, const TBusServerSessionConfig& config, TBusMessageQueuePtr queue) { return queue->CreateDestination(proto, handler, config); } -TBusServerSessionPtr TBusServerSession::Create(TBusProtocol* proto, IBusServerHandler* handler, const TBusServerSessionConfig& config, TBusMessageQueuePtr queue, const TVector<TBindResult>& bindTo) { +TBusServerSessionPtr TBusServerSession::Create(TBusProtocol* proto, IBusServerHandler* handler, const TBusServerSessionConfig& config, TBusMessageQueuePtr queue, const TVector<TBindResult>& bindTo) { return queue->CreateDestination(proto, handler, config, bindTo); } diff --git a/library/cpp/messagebus/session.h b/library/cpp/messagebus/session.h index 857f58d7e5..fb12ab7c22 100644 --- a/library/cpp/messagebus/session.h +++ b/library/cpp/messagebus/session.h @@ -15,211 +15,211 @@ #include <util/generic/ptr.h> namespace NBus { - template <typename TBusSessionSubclass> - class TBusSessionPtr; - using TBusClientSessionPtr = TBusSessionPtr<TBusClientSession>; - using TBusServerSessionPtr = TBusSessionPtr<TBusServerSession>; - - /////////////////////////////////////////////////////////////////// - /// \brief Interface of session object. - - /// Each client and server - /// should instantiate session object to be able to communicate via bus - /// client: sess = queue->CreateSource(protocol, handler); - /// server: sess = queue->CreateDestination(protocol, handler); - - class TBusSession: public TWeakRefCounted<TBusSession> { - public: - size_t GetInFlight(const TNetAddr& addr) const; - size_t GetConnectSyscallsNumForTest(const TNetAddr& addr) const; - - virtual void GetInFlightBulk(TArrayRef<const TNetAddr> addrs, TArrayRef<size_t> results) const = 0; - virtual void GetConnectSyscallsNumBulkForTest(TArrayRef<const TNetAddr> addrs, TArrayRef<size_t> results) const = 0; - - virtual int GetInFlight() const noexcept = 0; - /// monitoring status of current session and it's connections - virtual TString GetStatus(ui16 flags = YBUS_STATUS_CONNS) = 0; - virtual TConnectionStatusMonRecord GetStatusProtobuf() = 0; - virtual NPrivate::TSessionDumpStatus GetStatusRecordInternal() = 0; - virtual TString GetStatusSingleLine() = 0; - /// return session config - virtual const TBusSessionConfig* GetConfig() const noexcept = 0; - /// return session protocol - virtual const TBusProtocol* GetProto() const noexcept = 0; - virtual TBusMessageQueue* GetQueue() const noexcept = 0; - - /// registers external session on host:port with locator service - int RegisterService(const char* hostname, TBusKey start = YBUS_KEYMIN, TBusKey end = YBUS_KEYMAX, EIpVersion ipVersion = EIP_VERSION_4); - - protected: - TBusSession(); - - public: - virtual TString GetNameInternal() = 0; - - virtual void Shutdown() = 0; - - virtual ~TBusSession(); - }; - - struct TBusClientSession: public virtual TBusSession { - typedef ::NBus::NPrivate::TRemoteClientSession TImpl; - - static TBusClientSessionPtr Create( - TBusProtocol* proto, - IBusClientHandler* handler, + template <typename TBusSessionSubclass> + class TBusSessionPtr; + using TBusClientSessionPtr = TBusSessionPtr<TBusClientSession>; + using TBusServerSessionPtr = TBusSessionPtr<TBusServerSession>; + + /////////////////////////////////////////////////////////////////// + /// \brief Interface of session object. + + /// Each client and server + /// should instantiate session object to be able to communicate via bus + /// client: sess = queue->CreateSource(protocol, handler); + /// server: sess = queue->CreateDestination(protocol, handler); + + class TBusSession: public TWeakRefCounted<TBusSession> { + public: + size_t GetInFlight(const TNetAddr& addr) const; + size_t GetConnectSyscallsNumForTest(const TNetAddr& addr) const; + + virtual void GetInFlightBulk(TArrayRef<const TNetAddr> addrs, TArrayRef<size_t> results) const = 0; + virtual void GetConnectSyscallsNumBulkForTest(TArrayRef<const TNetAddr> addrs, TArrayRef<size_t> results) const = 0; + + virtual int GetInFlight() const noexcept = 0; + /// monitoring status of current session and it's connections + virtual TString GetStatus(ui16 flags = YBUS_STATUS_CONNS) = 0; + virtual TConnectionStatusMonRecord GetStatusProtobuf() = 0; + virtual NPrivate::TSessionDumpStatus GetStatusRecordInternal() = 0; + virtual TString GetStatusSingleLine() = 0; + /// return session config + virtual const TBusSessionConfig* GetConfig() const noexcept = 0; + /// return session protocol + virtual const TBusProtocol* GetProto() const noexcept = 0; + virtual TBusMessageQueue* GetQueue() const noexcept = 0; + + /// registers external session on host:port with locator service + int RegisterService(const char* hostname, TBusKey start = YBUS_KEYMIN, TBusKey end = YBUS_KEYMAX, EIpVersion ipVersion = EIP_VERSION_4); + + protected: + TBusSession(); + + public: + virtual TString GetNameInternal() = 0; + + virtual void Shutdown() = 0; + + virtual ~TBusSession(); + }; + + struct TBusClientSession: public virtual TBusSession { + typedef ::NBus::NPrivate::TRemoteClientSession TImpl; + + static TBusClientSessionPtr Create( + TBusProtocol* proto, + IBusClientHandler* handler, const TBusClientSessionConfig& config, TBusMessageQueuePtr queue); - virtual TBusClientConnectionPtr GetConnection(const TNetAddr&) = 0; + virtual TBusClientConnectionPtr GetConnection(const TNetAddr&) = 0; - /// if you want to open connection early - virtual void OpenConnection(const TNetAddr&) = 0; + /// if you want to open connection early + virtual void OpenConnection(const TNetAddr&) = 0; - /// Send message to the destination - /// If addr is set then use it as destination. - /// Takes ownership of addr (see ClearState method). - virtual EMessageStatus SendMessage(TBusMessage* pMes, const TNetAddr* addr = nullptr, bool wait = false) = 0; + /// Send message to the destination + /// If addr is set then use it as destination. + /// Takes ownership of addr (see ClearState method). + virtual EMessageStatus SendMessage(TBusMessage* pMes, const TNetAddr* addr = nullptr, bool wait = false) = 0; - virtual EMessageStatus SendMessageOneWay(TBusMessage* pMes, const TNetAddr* addr = nullptr, bool wait = false) = 0; + virtual EMessageStatus SendMessageOneWay(TBusMessage* pMes, const TNetAddr* addr = nullptr, bool wait = false) = 0; - /// Like SendMessage but cares about message - template <typename T /* <: TBusMessage */> - EMessageStatus SendMessageAutoPtr(const TAutoPtr<T>& mes, const TNetAddr* addr = nullptr, bool wait = false) { - EMessageStatus status = SendMessage(mes.Get(), addr, wait); - if (status == MESSAGE_OK) + /// Like SendMessage but cares about message + template <typename T /* <: TBusMessage */> + EMessageStatus SendMessageAutoPtr(const TAutoPtr<T>& mes, const TNetAddr* addr = nullptr, bool wait = false) { + EMessageStatus status = SendMessage(mes.Get(), addr, wait); + if (status == MESSAGE_OK) Y_UNUSED(mes.Release()); - return status; - } - - /// Like SendMessageOneWay but cares about message - template <typename T /* <: TBusMessage */> - EMessageStatus SendMessageOneWayAutoPtr(const TAutoPtr<T>& mes, const TNetAddr* addr = nullptr, bool wait = false) { - EMessageStatus status = SendMessageOneWay(mes.Get(), addr, wait); - if (status == MESSAGE_OK) + return status; + } + + /// Like SendMessageOneWay but cares about message + template <typename T /* <: TBusMessage */> + EMessageStatus SendMessageOneWayAutoPtr(const TAutoPtr<T>& mes, const TNetAddr* addr = nullptr, bool wait = false) { + EMessageStatus status = SendMessageOneWay(mes.Get(), addr, wait); + if (status == MESSAGE_OK) Y_UNUSED(mes.Release()); - return status; - } + return status; + } - EMessageStatus SendMessageMove(TBusMessageAutoPtr message, const TNetAddr* addr = nullptr, bool wait = false) { - return SendMessageAutoPtr(message, addr, wait); - } + EMessageStatus SendMessageMove(TBusMessageAutoPtr message, const TNetAddr* addr = nullptr, bool wait = false) { + return SendMessageAutoPtr(message, addr, wait); + } - EMessageStatus SendMessageOneWayMove(TBusMessageAutoPtr message, const TNetAddr* addr = nullptr, bool wait = false) { - return SendMessageOneWayAutoPtr(message, addr, wait); - } + EMessageStatus SendMessageOneWayMove(TBusMessageAutoPtr message, const TNetAddr* addr = nullptr, bool wait = false) { + return SendMessageOneWayAutoPtr(message, addr, wait); + } - // TODO: implement similar one-way methods - }; + // TODO: implement similar one-way methods + }; - struct TBusServerSession: public virtual TBusSession { - typedef ::NBus::NPrivate::TRemoteServerSession TImpl; + struct TBusServerSession: public virtual TBusSession { + typedef ::NBus::NPrivate::TRemoteServerSession TImpl; - static TBusServerSessionPtr Create( - TBusProtocol* proto, - IBusServerHandler* handler, + static TBusServerSessionPtr Create( + TBusProtocol* proto, + IBusServerHandler* handler, const TBusServerSessionConfig& config, TBusMessageQueuePtr queue); - static TBusServerSessionPtr Create( - TBusProtocol* proto, - IBusServerHandler* handler, + static TBusServerSessionPtr Create( + TBusProtocol* proto, + IBusServerHandler* handler, const TBusServerSessionConfig& config, TBusMessageQueuePtr queue, const TVector<TBindResult>& bindTo); - // TODO: make parameter non-const - virtual EMessageStatus SendReply(const TBusIdentity& ident, TBusMessage* pRep) = 0; + // TODO: make parameter non-const + virtual EMessageStatus SendReply(const TBusIdentity& ident, TBusMessage* pRep) = 0; - // TODO: make parameter non-const - virtual EMessageStatus ForgetRequest(const TBusIdentity& ident) = 0; + // TODO: make parameter non-const + virtual EMessageStatus ForgetRequest(const TBusIdentity& ident) = 0; - template <typename U /* <: TBusMessage */> - EMessageStatus SendReplyAutoPtr(TBusIdentity& ident, TAutoPtr<U>& resp) { - EMessageStatus status = SendReply(const_cast<const TBusIdentity&>(ident), resp.Get()); - if (status == MESSAGE_OK) { + template <typename U /* <: TBusMessage */> + EMessageStatus SendReplyAutoPtr(TBusIdentity& ident, TAutoPtr<U>& resp) { + EMessageStatus status = SendReply(const_cast<const TBusIdentity&>(ident), resp.Get()); + if (status == MESSAGE_OK) { Y_UNUSED(resp.Release()); - } - return status; + } + return status; } - EMessageStatus SendReplyMove(TBusIdentity& ident, TBusMessageAutoPtr resp) { - return SendReplyAutoPtr(ident, resp); - } - - /// Pause input from the network. - /// It is valid to call this method in parallel. - /// TODO: pull this method up to TBusSession. - virtual void PauseInput(bool pause) = 0; - virtual unsigned GetActualListenPort() = 0; - }; - - namespace NPrivate { - template <typename TBusSessionSubclass> - class TBusOwnerSessionPtr: public TAtomicRefCount<TBusOwnerSessionPtr<TBusSessionSubclass>> { - private: - TIntrusivePtr<TBusSessionSubclass> Ptr; - - public: - TBusOwnerSessionPtr(TBusSessionSubclass* session) - : Ptr(session) - { - Y_ASSERT(!!Ptr); - } - - ~TBusOwnerSessionPtr() { - Ptr->Shutdown(); - } - - TBusSessionSubclass* Get() const { - return reinterpret_cast<TBusSessionSubclass*>(Ptr.Get()); - } - }; + EMessageStatus SendReplyMove(TBusIdentity& ident, TBusMessageAutoPtr resp) { + return SendReplyAutoPtr(ident, resp); + } + + /// Pause input from the network. + /// It is valid to call this method in parallel. + /// TODO: pull this method up to TBusSession. + virtual void PauseInput(bool pause) = 0; + virtual unsigned GetActualListenPort() = 0; + }; + + namespace NPrivate { + template <typename TBusSessionSubclass> + class TBusOwnerSessionPtr: public TAtomicRefCount<TBusOwnerSessionPtr<TBusSessionSubclass>> { + private: + TIntrusivePtr<TBusSessionSubclass> Ptr; + + public: + TBusOwnerSessionPtr(TBusSessionSubclass* session) + : Ptr(session) + { + Y_ASSERT(!!Ptr); + } + + ~TBusOwnerSessionPtr() { + Ptr->Shutdown(); + } + + TBusSessionSubclass* Get() const { + return reinterpret_cast<TBusSessionSubclass*>(Ptr.Get()); + } + }; } - template <typename TBusSessionSubclass> - class TBusSessionPtr { - private: - TIntrusivePtr<NPrivate::TBusOwnerSessionPtr<TBusSessionSubclass>> SmartPtr; - TBusSessionSubclass* Ptr; - - public: - TBusSessionPtr() - : Ptr() - { - } - TBusSessionPtr(TBusSessionSubclass* session) - : SmartPtr(!!session ? new NPrivate::TBusOwnerSessionPtr<TBusSessionSubclass>(session) : nullptr) - , Ptr(session) - { - } - - TBusSessionSubclass* Get() const { - return Ptr; - } - operator TBusSessionSubclass*() { - return Get(); - } - TBusSessionSubclass& operator*() const { - return *Get(); - } - TBusSessionSubclass* operator->() const { - return Get(); - } - - bool operator!() const { - return !Ptr; - } - - void Swap(TBusSessionPtr& t) noexcept { - DoSwap(SmartPtr, t.SmartPtr); - DoSwap(Ptr, t.Ptr); - } - - void Drop() { - TBusSessionPtr().Swap(*this); - } - }; + template <typename TBusSessionSubclass> + class TBusSessionPtr { + private: + TIntrusivePtr<NPrivate::TBusOwnerSessionPtr<TBusSessionSubclass>> SmartPtr; + TBusSessionSubclass* Ptr; + + public: + TBusSessionPtr() + : Ptr() + { + } + TBusSessionPtr(TBusSessionSubclass* session) + : SmartPtr(!!session ? new NPrivate::TBusOwnerSessionPtr<TBusSessionSubclass>(session) : nullptr) + , Ptr(session) + { + } + + TBusSessionSubclass* Get() const { + return Ptr; + } + operator TBusSessionSubclass*() { + return Get(); + } + TBusSessionSubclass& operator*() const { + return *Get(); + } + TBusSessionSubclass* operator->() const { + return Get(); + } + + bool operator!() const { + return !Ptr; + } + + void Swap(TBusSessionPtr& t) noexcept { + DoSwap(SmartPtr, t.SmartPtr); + DoSwap(Ptr, t.Ptr); + } + + void Drop() { + TBusSessionPtr().Swap(*this); + } + }; } diff --git a/library/cpp/messagebus/session_impl.cpp b/library/cpp/messagebus/session_impl.cpp index cff202c5b8..ddf9f360c4 100644 --- a/library/cpp/messagebus/session_impl.cpp +++ b/library/cpp/messagebus/session_impl.cpp @@ -40,10 +40,10 @@ namespace { } TConnectionsAcceptorsSnapshot::TConnectionsAcceptorsSnapshot() - : LastConnectionId(0) - , LastAcceptorId(0) -{ -} + : LastConnectionId(0) + , LastAcceptorId(0) +{ +} struct TBusSessionImpl::TImpl { TRemoteConnectionWriterIncrementalStatus DeadConnectionWriterStatusSummary; @@ -62,7 +62,7 @@ namespace { copy.TotalTimeout = config.SendTimeout + TDuration::MilliSeconds(10).MilliSeconds(); } else if (copy.SendTimeout == 0) { Y_ASSERT(copy.TotalTimeout != 0); - if ((ui64)copy.TotalTimeout > (ui64)TDuration::MilliSeconds(10).MilliSeconds()) { + if ((ui64)copy.TotalTimeout > (ui64)TDuration::MilliSeconds(10).MilliSeconds()) { copy.SendTimeout = copy.TotalTimeout - TDuration::MilliSeconds(10).MilliSeconds(); } else { copy.SendTimeout = copy.TotalTimeout; @@ -90,8 +90,8 @@ namespace { } TBusSessionImpl::TBusSessionImpl(bool isSource, TBusMessageQueue* queue, TBusProtocol* proto, - IBusErrorHandler* handler, - const TBusSessionConfig& config, const TString& name) + IBusErrorHandler* handler, + const TBusSessionConfig& config, const TString& name) : TActor<TBusSessionImpl, TStatusTag>(queue->WorkQueue.Get()) , TActor<TBusSessionImpl, TConnectionTag>(queue->WorkQueue.Get()) , Impl(new TImpl) @@ -111,7 +111,7 @@ TBusSessionImpl::TBusSessionImpl(bool isSource, TBusMessageQueue* queue, TBusPro Impl->DeadAcceptorStatusSummary.Summary = true; ReadEventLoopThread.Reset(new NThreading::TLegacyFuture<void, false>(std::bind(&TEventLoop::Run, std::ref(ReadEventLoop)))); - WriteEventLoopThread.Reset(new NThreading::TLegacyFuture<void, false>(std::bind(&TEventLoop::Run, std::ref(WriteEventLoop)))); + WriteEventLoopThread.Reset(new NThreading::TLegacyFuture<void, false>(std::bind(&TEventLoop::Run, std::ref(WriteEventLoop)))); Queue->Schedule(IScheduleItemAutoPtr(new TScheduleSession(this, TInstant::Now() + Config.Secret.TimeoutPeriod))); } @@ -127,8 +127,8 @@ TBusSessionStatus::TBusSessionStatus() : InFlightCount(0) , InFlightSize(0) , InputPaused(false) -{ -} +{ +} void TBusSessionImpl::Shutdown() { if (!AtomicCas(&Down, 1, 0)) { @@ -233,7 +233,7 @@ void TBusSessionImpl::FillStatus() { TSessionDumpStatus TBusSessionImpl::GetStatusRecordInternal() { // Probably useless, because it returns cached info now Y_VERIFY(!Queue->GetExecutor()->IsInExecutorThread(), - "GetStatus must not be called from executor thread"); + "GetStatus must not be called from executor thread"); TGuard<TMutex> guard(StatusData.StatusDumpCachedMutex); // TODO: returns zeros for a second after start @@ -247,9 +247,9 @@ TString TBusSessionImpl::GetStatus(ui16 flags) { return GetStatusRecordInternal().PrintToString(); } -TConnectionStatusMonRecord TBusSessionImpl::GetStatusProtobuf() { +TConnectionStatusMonRecord TBusSessionImpl::GetStatusProtobuf() { Y_VERIFY(!Queue->GetExecutor()->IsInExecutorThread(), - "GetStatus must not be called from executor thread"); + "GetStatus must not be called from executor thread"); TGuard<TMutex> guard(StatusData.StatusDumpCachedMutex); @@ -289,8 +289,8 @@ void TBusSessionImpl::ProcessItem(TConnectionTag, ::NActor::TDefaultTag, const T } //if (Connections.find(addr) != Connections.end()) { - // TODO: it is possible - // won't be a problem after socket address replaced with id + // TODO: it is possible + // won't be a problem after socket address replaced with id //} TRemoteConnectionPtr c(new TRemoteServerConnection(VerifyDynamicCast<TRemoteServerSession*>(this), ++LastConnectionId, onAccept.addr)); @@ -316,14 +316,14 @@ void TBusSessionImpl::ProcessItem(TConnectionTag, TRemoveTag, TRemoteConnectionP SendSnapshotToStatusActor(); } -void TBusSessionImpl::ProcessConnectionsAcceptorsShapshotQueueItem(TAtomicSharedPtr<TConnectionsAcceptorsSnapshot> snapshot) { +void TBusSessionImpl::ProcessConnectionsAcceptorsShapshotQueueItem(TAtomicSharedPtr<TConnectionsAcceptorsSnapshot> snapshot) { for (TVector<TRemoteConnectionPtr>::const_iterator connection = snapshot->Connections.begin(); - connection != snapshot->Connections.end(); ++connection) { + connection != snapshot->Connections.end(); ++connection) { Y_ASSERT((*connection)->ConnectionId <= snapshot->LastConnectionId); } for (TVector<TAcceptorPtr>::const_iterator acceptor = snapshot->Acceptors.begin(); - acceptor != snapshot->Acceptors.end(); ++acceptor) { + acceptor != snapshot->Acceptors.end(); ++acceptor) { Y_ASSERT((*acceptor)->AcceptorId <= snapshot->LastAcceptorId); } @@ -388,7 +388,7 @@ void TBusSessionImpl::StatusUpdateCachedDump() { TStringStream ss; for (TVector<TAcceptorPtr>::const_iterator acceptor = acceptors.begin(); - acceptor != acceptors.end(); ++acceptor) { + acceptor != acceptors.end(); ++acceptor) { const TAcceptorStatus status = (*acceptor)->GranStatus.Listen.Get(); acceptorStatusSummary += status; @@ -406,7 +406,7 @@ void TBusSessionImpl::StatusUpdateCachedDump() { TStringStream ss; for (TVector<TRemoteConnectionPtr>::const_iterator connection = connections.begin(); - connection != connections.end(); ++connection) { + connection != connections.end(); ++connection) { if (connection != connections.begin()) { ss << "\n"; } @@ -434,8 +434,8 @@ void TBusSessionImpl::StatusUpdateCachedDump() { TBusSessionImpl::TStatusData::TStatusData() : ConnectionsAcceptorsSnapshot(new TConnectionsAcceptorsSnapshot) -{ -} +{ +} void TBusSessionImpl::Act(TStatusTag) { TInstant now = TInstant::Now(); @@ -463,8 +463,8 @@ void TBusSessionImpl::Act(TStatusTag) { StatusUpdateCachedDumpIfNecessary(now); } -TBusSessionImpl::TConnectionsData::TConnectionsData() { -} +TBusSessionImpl::TConnectionsData::TConnectionsData() { +} void TBusSessionImpl::Act(TConnectionTag) { TConnectionsGuard guard(ConnectionsLock); @@ -487,11 +487,11 @@ void TBusSessionImpl::Listen(int port, TBusMessageQueue* q) { Listen(BindOnPort(port, Config.ReusePort).second, q); } -void TBusSessionImpl::Listen(const TVector<TBindResult>& bindTo, TBusMessageQueue* q) { +void TBusSessionImpl::Listen(const TVector<TBindResult>& bindTo, TBusMessageQueue* q) { Y_ASSERT(q == Queue); int actualPort = -1; - for (const TBindResult& br : bindTo) { + for (const TBindResult& br : bindTo) { if (actualPort == -1) { actualPort = br.Addr.GetPort(); } else { @@ -513,7 +513,7 @@ void TBusSessionImpl::Listen(const TVector<TBindResult>& bindTo, TBusMessageQueu void TBusSessionImpl::SendSnapshotToStatusActor() { //Y_ASSERT(ConnectionsLock.IsLocked()); - TAtomicSharedPtr<TConnectionsAcceptorsSnapshot> snapshot(new TConnectionsAcceptorsSnapshot); + TAtomicSharedPtr<TConnectionsAcceptorsSnapshot> snapshot(new TConnectionsAcceptorsSnapshot); GetAcceptorsLockAquired(&snapshot->Acceptors); GetConnectionsLockAquired(&snapshot->Connections); snapshot->LastAcceptorId = LastAcceptorId; diff --git a/library/cpp/messagebus/session_impl.h b/library/cpp/messagebus/session_impl.h index e099c798b9..90ef246ff8 100644 --- a/library/cpp/messagebus/session_impl.h +++ b/library/cpp/messagebus/session_impl.h @@ -19,241 +19,241 @@ #include <util/generic/array_ref.h> #include <util/generic/string.h> -namespace NBus { - namespace NPrivate { - typedef TIntrusivePtr<TRemoteClientConnection> TRemoteClientConnectionPtr; - typedef TIntrusivePtr<TRemoteServerConnection> TRemoteServerConnectionPtr; - - typedef TIntrusivePtr<TRemoteServerSession> TRemoteServerSessionPtr; - - typedef TIntrusivePtr<TAcceptor> TAcceptorPtr; - typedef TVector<TAcceptorPtr> TAcceptorsPtrs; - - struct TConnectionsAcceptorsSnapshot { - TVector<TRemoteConnectionPtr> Connections; - TVector<TAcceptorPtr> Acceptors; - ui64 LastConnectionId; - ui64 LastAcceptorId; - - TConnectionsAcceptorsSnapshot(); - }; - - typedef TAtomicSharedPtr<TConnectionsAcceptorsSnapshot> TConnectionsAcceptorsSnapshotPtr; - - struct TOnAccept { - SOCKET s; - TNetAddr addr; - TInstant now; - }; - - struct TStatusTag {}; - struct TConnectionTag {}; - - struct TDeadConnectionTag {}; - struct TRemoveTag {}; - - struct TBusSessionImpl - : public virtual TBusSession, - private ::NActor::TActor<TBusSessionImpl, TStatusTag>, - private ::NActor::TActor<TBusSessionImpl, TConnectionTag> - - , - private ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionWriterIncrementalStatus, TStatusTag, TDeadConnectionTag>, - private ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionReaderIncrementalStatus, TStatusTag, TDeadConnectionTag>, - private ::NActor::TQueueInActor<TBusSessionImpl, TAcceptorStatus, TStatusTag, TDeadConnectionTag> - - , - private ::NActor::TQueueInActor<TBusSessionImpl, TOnAccept, TConnectionTag>, - private ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionPtr, TConnectionTag, TRemoveTag> { - friend class TAcceptor; - friend class TRemoteConnection; - friend class TRemoteServerConnection; - friend class ::NActor::TActor<TBusSessionImpl, TStatusTag>; - friend class ::NActor::TActor<TBusSessionImpl, TConnectionTag>; - friend class ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionWriterIncrementalStatus, TStatusTag, TDeadConnectionTag>; - friend class ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionReaderIncrementalStatus, TStatusTag, TDeadConnectionTag>; - friend class ::NActor::TQueueInActor<TBusSessionImpl, TAcceptorStatus, TStatusTag, TDeadConnectionTag>; - friend class ::NActor::TQueueInActor<TBusSessionImpl, TOnAccept, TConnectionTag>; - friend class ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionPtr, TConnectionTag, TRemoveTag>; - - public: - ::NActor::TQueueInActor<TBusSessionImpl, TOnAccept, TConnectionTag>* GetOnAcceptQueue() { - return this; - } +namespace NBus { + namespace NPrivate { + typedef TIntrusivePtr<TRemoteClientConnection> TRemoteClientConnectionPtr; + typedef TIntrusivePtr<TRemoteServerConnection> TRemoteServerConnectionPtr; + + typedef TIntrusivePtr<TRemoteServerSession> TRemoteServerSessionPtr; + + typedef TIntrusivePtr<TAcceptor> TAcceptorPtr; + typedef TVector<TAcceptorPtr> TAcceptorsPtrs; + + struct TConnectionsAcceptorsSnapshot { + TVector<TRemoteConnectionPtr> Connections; + TVector<TAcceptorPtr> Acceptors; + ui64 LastConnectionId; + ui64 LastAcceptorId; + + TConnectionsAcceptorsSnapshot(); + }; + + typedef TAtomicSharedPtr<TConnectionsAcceptorsSnapshot> TConnectionsAcceptorsSnapshotPtr; + + struct TOnAccept { + SOCKET s; + TNetAddr addr; + TInstant now; + }; + + struct TStatusTag {}; + struct TConnectionTag {}; + + struct TDeadConnectionTag {}; + struct TRemoveTag {}; + + struct TBusSessionImpl + : public virtual TBusSession, + private ::NActor::TActor<TBusSessionImpl, TStatusTag>, + private ::NActor::TActor<TBusSessionImpl, TConnectionTag> + + , + private ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionWriterIncrementalStatus, TStatusTag, TDeadConnectionTag>, + private ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionReaderIncrementalStatus, TStatusTag, TDeadConnectionTag>, + private ::NActor::TQueueInActor<TBusSessionImpl, TAcceptorStatus, TStatusTag, TDeadConnectionTag> + + , + private ::NActor::TQueueInActor<TBusSessionImpl, TOnAccept, TConnectionTag>, + private ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionPtr, TConnectionTag, TRemoveTag> { + friend class TAcceptor; + friend class TRemoteConnection; + friend class TRemoteServerConnection; + friend class ::NActor::TActor<TBusSessionImpl, TStatusTag>; + friend class ::NActor::TActor<TBusSessionImpl, TConnectionTag>; + friend class ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionWriterIncrementalStatus, TStatusTag, TDeadConnectionTag>; + friend class ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionReaderIncrementalStatus, TStatusTag, TDeadConnectionTag>; + friend class ::NActor::TQueueInActor<TBusSessionImpl, TAcceptorStatus, TStatusTag, TDeadConnectionTag>; + friend class ::NActor::TQueueInActor<TBusSessionImpl, TOnAccept, TConnectionTag>; + friend class ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionPtr, TConnectionTag, TRemoveTag>; + + public: + ::NActor::TQueueInActor<TBusSessionImpl, TOnAccept, TConnectionTag>* GetOnAcceptQueue() { + return this; + } - ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionPtr, TConnectionTag, TRemoveTag>* GetRemoveConnectionQueue() { - return this; - } - - ::NActor::TActor<TBusSessionImpl, TConnectionTag>* GetConnectionActor() { - return this; - } - - typedef TGuard<TMutex> TConnectionsGuard; - - TBusSessionImpl(bool isSource, TBusMessageQueue* queue, TBusProtocol* proto, - IBusErrorHandler* handler, - const TBusSessionConfig& config, const TString& name); - - ~TBusSessionImpl() override; + ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionPtr, TConnectionTag, TRemoveTag>* GetRemoveConnectionQueue() { + return this; + } + + ::NActor::TActor<TBusSessionImpl, TConnectionTag>* GetConnectionActor() { + return this; + } + + typedef TGuard<TMutex> TConnectionsGuard; + + TBusSessionImpl(bool isSource, TBusMessageQueue* queue, TBusProtocol* proto, + IBusErrorHandler* handler, + const TBusSessionConfig& config, const TString& name); + + ~TBusSessionImpl() override; - void Shutdown() override; - bool IsDown(); + void Shutdown() override; + bool IsDown(); - size_t GetInFlightImpl(const TNetAddr& addr) const; - size_t GetConnectSyscallsNumForTestImpl(const TNetAddr& addr) const; + size_t GetInFlightImpl(const TNetAddr& addr) const; + size_t GetConnectSyscallsNumForTestImpl(const TNetAddr& addr) const; - void GetInFlightBulk(TArrayRef<const TNetAddr> addrs, TArrayRef<size_t> results) const override; - void GetConnectSyscallsNumBulkForTest(TArrayRef<const TNetAddr> addrs, TArrayRef<size_t> results) const override; + void GetInFlightBulk(TArrayRef<const TNetAddr> addrs, TArrayRef<size_t> results) const override; + void GetConnectSyscallsNumBulkForTest(TArrayRef<const TNetAddr> addrs, TArrayRef<size_t> results) const override; - virtual void FillStatus(); - TSessionDumpStatus GetStatusRecordInternal() override; - TString GetStatus(ui16 flags = YBUS_STATUS_CONNS) override; - TConnectionStatusMonRecord GetStatusProtobuf() override; - TString GetStatusSingleLine() override; + virtual void FillStatus(); + TSessionDumpStatus GetStatusRecordInternal() override; + TString GetStatus(ui16 flags = YBUS_STATUS_CONNS) override; + TConnectionStatusMonRecord GetStatusProtobuf() override; + TString GetStatusSingleLine() override; - void ProcessItem(TStatusTag, TDeadConnectionTag, const TRemoteConnectionWriterIncrementalStatus&); - void ProcessItem(TStatusTag, TDeadConnectionTag, const TRemoteConnectionReaderIncrementalStatus&); - void ProcessItem(TStatusTag, TDeadConnectionTag, const TAcceptorStatus&); - void ProcessItem(TStatusTag, ::NActor::TDefaultTag, const TAcceptorStatus&); - void ProcessItem(TConnectionTag, ::NActor::TDefaultTag, const TOnAccept&); - void ProcessItem(TConnectionTag, TRemoveTag, TRemoteConnectionPtr); - void ProcessConnectionsAcceptorsShapshotQueueItem(TAtomicSharedPtr<TConnectionsAcceptorsSnapshot>); - void StatusUpdateCachedDump(); - void StatusUpdateCachedDumpIfNecessary(TInstant now); - void Act(TStatusTag); - void Act(TConnectionTag); + void ProcessItem(TStatusTag, TDeadConnectionTag, const TRemoteConnectionWriterIncrementalStatus&); + void ProcessItem(TStatusTag, TDeadConnectionTag, const TRemoteConnectionReaderIncrementalStatus&); + void ProcessItem(TStatusTag, TDeadConnectionTag, const TAcceptorStatus&); + void ProcessItem(TStatusTag, ::NActor::TDefaultTag, const TAcceptorStatus&); + void ProcessItem(TConnectionTag, ::NActor::TDefaultTag, const TOnAccept&); + void ProcessItem(TConnectionTag, TRemoveTag, TRemoteConnectionPtr); + void ProcessConnectionsAcceptorsShapshotQueueItem(TAtomicSharedPtr<TConnectionsAcceptorsSnapshot>); + void StatusUpdateCachedDump(); + void StatusUpdateCachedDumpIfNecessary(TInstant now); + void Act(TStatusTag); + void Act(TConnectionTag); - TBusProtocol* GetProto() const noexcept override; - const TBusSessionConfig* GetConfig() const noexcept override; - TBusMessageQueue* GetQueue() const noexcept override; - TString GetNameInternal() override; - - virtual void OnMessageReceived(TRemoteConnection* c, TVectorSwaps<TBusMessagePtrAndHeader>& newMsg) = 0; - - void Listen(int port, TBusMessageQueue* q); - void Listen(const TVector<TBindResult>& bindTo, TBusMessageQueue* q); - TBusConnection* Accept(SOCKET listen); - - inline ::NActor::TActor<TBusSessionImpl, TStatusTag>* GetStatusActor() { - return this; - } - inline ::NActor::TActor<TBusSessionImpl, TConnectionTag>* GetConnectionsActor() { - return this; - } - - typedef THashMap<TBusSocketAddr, TRemoteConnectionPtr> TAddrRemoteConnections; - - void SendSnapshotToStatusActor(); + TBusProtocol* GetProto() const noexcept override; + const TBusSessionConfig* GetConfig() const noexcept override; + TBusMessageQueue* GetQueue() const noexcept override; + TString GetNameInternal() override; + + virtual void OnMessageReceived(TRemoteConnection* c, TVectorSwaps<TBusMessagePtrAndHeader>& newMsg) = 0; + + void Listen(int port, TBusMessageQueue* q); + void Listen(const TVector<TBindResult>& bindTo, TBusMessageQueue* q); + TBusConnection* Accept(SOCKET listen); + + inline ::NActor::TActor<TBusSessionImpl, TStatusTag>* GetStatusActor() { + return this; + } + inline ::NActor::TActor<TBusSessionImpl, TConnectionTag>* GetConnectionsActor() { + return this; + } + + typedef THashMap<TBusSocketAddr, TRemoteConnectionPtr> TAddrRemoteConnections; + + void SendSnapshotToStatusActor(); - void InsertConnectionLockAcquired(TRemoteConnection* connection); - void InsertAcceptorLockAcquired(TAcceptor* acceptor); - - void GetConnections(TVector<TRemoteConnectionPtr>*); - void GetAcceptors(TVector<TAcceptorPtr>*); - void GetConnectionsLockAquired(TVector<TRemoteConnectionPtr>*); - void GetAcceptorsLockAquired(TVector<TAcceptorPtr>*); - - TRemoteConnectionPtr GetConnection(const TBusSocketAddr& addr, bool create); - TRemoteConnectionPtr GetConnectionById(ui64 id); - TAcceptorPtr GetAcceptorById(ui64 id); - - void InvokeOnError(TNonDestroyingAutoPtr<TBusMessage>, EMessageStatus); - - void Cron(); + void InsertConnectionLockAcquired(TRemoteConnection* connection); + void InsertAcceptorLockAcquired(TAcceptor* acceptor); + + void GetConnections(TVector<TRemoteConnectionPtr>*); + void GetAcceptors(TVector<TAcceptorPtr>*); + void GetConnectionsLockAquired(TVector<TRemoteConnectionPtr>*); + void GetAcceptorsLockAquired(TVector<TAcceptorPtr>*); + + TRemoteConnectionPtr GetConnection(const TBusSocketAddr& addr, bool create); + TRemoteConnectionPtr GetConnectionById(ui64 id); + TAcceptorPtr GetAcceptorById(ui64 id); + + void InvokeOnError(TNonDestroyingAutoPtr<TBusMessage>, EMessageStatus); + + void Cron(); - TBusSessionJobCount JobCount; - - // TODO: replace with actor - TMutex ConnectionsLock; + TBusSessionJobCount JobCount; + + // TODO: replace with actor + TMutex ConnectionsLock; - struct TImpl; - THolder<TImpl> Impl; + struct TImpl; + THolder<TImpl> Impl; - const bool IsSource_; + const bool IsSource_; - TBusMessageQueue* const Queue; - TBusProtocol* const Proto; - // copied to be available after Proto dies - const TString ProtoName; + TBusMessageQueue* const Queue; + TBusProtocol* const Proto; + // copied to be available after Proto dies + const TString ProtoName; - IBusErrorHandler* const ErrorHandler; - TUseCountHolder HandlerUseCountHolder; - TBusSessionConfig Config; // TODO: make const + IBusErrorHandler* const ErrorHandler; + TUseCountHolder HandlerUseCountHolder; + TBusSessionConfig Config; // TODO: make const - NEventLoop::TEventLoop WriteEventLoop; - NEventLoop::TEventLoop ReadEventLoop; - THolder<NThreading::TLegacyFuture<void, false>> ReadEventLoopThread; - THolder<NThreading::TLegacyFuture<void, false>> WriteEventLoopThread; + NEventLoop::TEventLoop WriteEventLoop; + NEventLoop::TEventLoop ReadEventLoop; + THolder<NThreading::TLegacyFuture<void, false>> ReadEventLoopThread; + THolder<NThreading::TLegacyFuture<void, false>> WriteEventLoopThread; - THashMap<ui64, TRemoteConnectionPtr> ConnectionsById; - TAddrRemoteConnections Connections; - TAcceptorsPtrs Acceptors; + THashMap<ui64, TRemoteConnectionPtr> ConnectionsById; + TAddrRemoteConnections Connections; + TAcceptorsPtrs Acceptors; - struct TStatusData { - TAtomicSharedPtr<TConnectionsAcceptorsSnapshot> ConnectionsAcceptorsSnapshot; - ::NActor::TQueueForActor<TAtomicSharedPtr<TConnectionsAcceptorsSnapshot>> ConnectionsAcceptorsSnapshotsQueue; + struct TStatusData { + TAtomicSharedPtr<TConnectionsAcceptorsSnapshot> ConnectionsAcceptorsSnapshot; + ::NActor::TQueueForActor<TAtomicSharedPtr<TConnectionsAcceptorsSnapshot>> ConnectionsAcceptorsSnapshotsQueue; - TAtomicShutdownState ShutdownState; + TAtomicShutdownState ShutdownState; - TBusSessionStatus Status; + TBusSessionStatus Status; - TSessionDumpStatus StatusDumpCached; - TMutex StatusDumpCachedMutex; - TInstant StatusDumpCachedLastUpdate; + TSessionDumpStatus StatusDumpCached; + TMutex StatusDumpCachedMutex; + TInstant StatusDumpCachedLastUpdate; - TStatusData(); - }; - TStatusData StatusData; + TStatusData(); + }; + TStatusData StatusData; - struct TConnectionsData { - TAtomicShutdownState ShutdownState; + struct TConnectionsData { + TAtomicShutdownState ShutdownState; - TConnectionsData(); - }; - TConnectionsData ConnectionsData; + TConnectionsData(); + }; + TConnectionsData ConnectionsData; - ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionWriterIncrementalStatus, - TStatusTag, TDeadConnectionTag>* - GetDeadConnectionWriterStatusQueue() { - return this; - } + ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionWriterIncrementalStatus, + TStatusTag, TDeadConnectionTag>* + GetDeadConnectionWriterStatusQueue() { + return this; + } - ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionReaderIncrementalStatus, - TStatusTag, TDeadConnectionTag>* - GetDeadConnectionReaderStatusQueue() { - return this; - } + ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionReaderIncrementalStatus, + TStatusTag, TDeadConnectionTag>* + GetDeadConnectionReaderStatusQueue() { + return this; + } - ::NActor::TQueueInActor<TBusSessionImpl, TAcceptorStatus, - TStatusTag, TDeadConnectionTag>* - GetDeadAcceptorStatusQueue() { - return this; - } + ::NActor::TQueueInActor<TBusSessionImpl, TAcceptorStatus, + TStatusTag, TDeadConnectionTag>* + GetDeadAcceptorStatusQueue() { + return this; + } - template <typename TItem> - ::NActor::IQueueInActor<TItem>* GetQueue() { - return this; - } + template <typename TItem> + ::NActor::IQueueInActor<TItem>* GetQueue() { + return this; + } - ui64 LastAcceptorId; - ui64 LastConnectionId; + ui64 LastAcceptorId; + ui64 LastConnectionId; - TAtomic Down; + TAtomic Down; TSystemEvent ShutdownCompleteEvent; - }; + }; - inline TBusProtocol* TBusSessionImpl::GetProto() const noexcept { - return Proto; - } + inline TBusProtocol* TBusSessionImpl::GetProto() const noexcept { + return Proto; + } - inline const TBusSessionConfig* TBusSessionImpl::GetConfig() const noexcept { - return &Config; - } + inline const TBusSessionConfig* TBusSessionImpl::GetConfig() const noexcept { + return &Config; + } - inline TBusMessageQueue* TBusSessionImpl::GetQueue() const noexcept { - return Queue; - } + inline TBusMessageQueue* TBusSessionImpl::GetQueue() const noexcept { + return Queue; + } } -} +} diff --git a/library/cpp/messagebus/session_job_count.cpp b/library/cpp/messagebus/session_job_count.cpp index 50b27e2f94..33322b1910 100644 --- a/library/cpp/messagebus/session_job_count.cpp +++ b/library/cpp/messagebus/session_job_count.cpp @@ -7,8 +7,8 @@ using namespace NBus::NPrivate; TBusSessionJobCount::TBusSessionJobCount() : JobCount(0) -{ -} +{ +} TBusSessionJobCount::~TBusSessionJobCount() { Y_VERIFY(JobCount == 0, "must be 0 job count to destroy job"); diff --git a/library/cpp/messagebus/session_job_count.h b/library/cpp/messagebus/session_job_count.h index ff56c7b392..23aca618b1 100644 --- a/library/cpp/messagebus/session_job_count.h +++ b/library/cpp/messagebus/session_job_count.h @@ -4,36 +4,36 @@ #include <util/system/condvar.h> #include <util/system/mutex.h> -namespace NBus { - namespace NPrivate { - class TBusSessionJobCount { - private: - TAtomic JobCount; +namespace NBus { + namespace NPrivate { + class TBusSessionJobCount { + private: + TAtomic JobCount; - TMutex Mutex; - TCondVar CondVar; + TMutex Mutex; + TCondVar CondVar; - public: - TBusSessionJobCount(); - ~TBusSessionJobCount(); + public: + TBusSessionJobCount(); + ~TBusSessionJobCount(); - void Add(unsigned delta) { - AtomicAdd(JobCount, delta); - } + void Add(unsigned delta) { + AtomicAdd(JobCount, delta); + } - void Increment() { - Add(1); - } + void Increment() { + Add(1); + } - void Decrement() { - if (AtomicDecrement(JobCount) == 0) { - TGuard<TMutex> guard(Mutex); - CondVar.BroadCast(); - } - } + void Decrement() { + if (AtomicDecrement(JobCount) == 0) { + TGuard<TMutex> guard(Mutex); + CondVar.BroadCast(); + } + } - void WaitForZero(); - }; + void WaitForZero(); + }; } -} +} diff --git a/library/cpp/messagebus/shutdown_state.cpp b/library/cpp/messagebus/shutdown_state.cpp index 86ef5d5324..a4e2bfa8b2 100644 --- a/library/cpp/messagebus/shutdown_state.cpp +++ b/library/cpp/messagebus/shutdown_state.cpp @@ -2,19 +2,19 @@ #include <util/system/yassert.h> -void TAtomicShutdownState::ShutdownCommand() { +void TAtomicShutdownState::ShutdownCommand() { Y_VERIFY(State.CompareAndSet(SS_RUNNING, SS_SHUTDOWN_COMMAND)); } -void TAtomicShutdownState::CompleteShutdown() { +void TAtomicShutdownState::CompleteShutdown() { Y_VERIFY(State.CompareAndSet(SS_SHUTDOWN_COMMAND, SS_SHUTDOWN_COMPLETE)); ShutdownComplete.Signal(); } -bool TAtomicShutdownState::IsRunning() { +bool TAtomicShutdownState::IsRunning() { return State.Get() == SS_RUNNING; } -TAtomicShutdownState::~TAtomicShutdownState() { +TAtomicShutdownState::~TAtomicShutdownState() { Y_VERIFY(SS_SHUTDOWN_COMPLETE == State.Get()); } diff --git a/library/cpp/messagebus/socket_addr.cpp b/library/cpp/messagebus/socket_addr.cpp index 0b4095abf0..c1b3a28fbe 100644 --- a/library/cpp/messagebus/socket_addr.cpp +++ b/library/cpp/messagebus/socket_addr.cpp @@ -11,66 +11,66 @@ using namespace NAddr; using namespace NBus; using namespace NBus::NPrivate; -static_assert(ADDR_UNSPEC == 0, "expect ADDR_UNSPEC == 0"); +static_assert(ADDR_UNSPEC == 0, "expect ADDR_UNSPEC == 0"); NBus::NPrivate::TBusSocketAddr::TBusSocketAddr(const NAddr::IRemoteAddr* addr) : IPv6ScopeID(0) { const sockaddr* sa = addr->Addr(); - switch ((EAddrFamily)sa->sa_family) { - case AF_UNSPEC: { - IpAddr.Clear(); - Port = 0; - break; - } - case AF_INET: { - IpAddr.SetInAddr(((const sockaddr_in*)sa)->sin_addr); - Port = InetToHost(((const sockaddr_in*)sa)->sin_port); - break; - } - case AF_INET6: { - IpAddr.SetIn6Addr(((const sockaddr_in6*)sa)->sin6_addr); - Port = InetToHost(((const sockaddr_in*)sa)->sin_port); - IPv6ScopeID = InetToHost(((const sockaddr_in6*)sa)->sin6_scope_id); - break; - } - default: - Y_FAIL("unknown address family"); + switch ((EAddrFamily)sa->sa_family) { + case AF_UNSPEC: { + IpAddr.Clear(); + Port = 0; + break; + } + case AF_INET: { + IpAddr.SetInAddr(((const sockaddr_in*)sa)->sin_addr); + Port = InetToHost(((const sockaddr_in*)sa)->sin_port); + break; + } + case AF_INET6: { + IpAddr.SetIn6Addr(((const sockaddr_in6*)sa)->sin6_addr); + Port = InetToHost(((const sockaddr_in*)sa)->sin_port); + IPv6ScopeID = InetToHost(((const sockaddr_in6*)sa)->sin6_scope_id); + break; + } + default: + Y_FAIL("unknown address family"); } } -NBus::NPrivate::TBusSocketAddr::TBusSocketAddr(TStringBuf host, unsigned port) { +NBus::NPrivate::TBusSocketAddr::TBusSocketAddr(TStringBuf host, unsigned port) { *this = TNetAddr(host, port); } -NBus::NPrivate::TBusSocketAddr::TBusSocketAddr(const TNetAddr& addr) { +NBus::NPrivate::TBusSocketAddr::TBusSocketAddr(const TNetAddr& addr) { *this = TBusSocketAddr(&addr); } -TNetAddr NBus::NPrivate::TBusSocketAddr::ToNetAddr() const { +TNetAddr NBus::NPrivate::TBusSocketAddr::ToNetAddr() const { sockaddr_storage storage; Zero(storage); - storage.ss_family = (ui16)IpAddr.GetAddrFamily(); + storage.ss_family = (ui16)IpAddr.GetAddrFamily(); switch (IpAddr.GetAddrFamily()) { - case ADDR_UNSPEC: - return TNetAddr(); - case ADDR_IPV4: { - ((sockaddr_in*)&storage)->sin_addr = IpAddr.GetInAddr(); - ((sockaddr_in*)&storage)->sin_port = HostToInet(Port); - break; - } - case ADDR_IPV6: { - ((sockaddr_in6*)&storage)->sin6_addr = IpAddr.GetIn6Addr(); - ((sockaddr_in6*)&storage)->sin6_port = HostToInet(Port); - ((sockaddr_in6*)&storage)->sin6_scope_id = HostToInet(IPv6ScopeID); - break; - } + case ADDR_UNSPEC: + return TNetAddr(); + case ADDR_IPV4: { + ((sockaddr_in*)&storage)->sin_addr = IpAddr.GetInAddr(); + ((sockaddr_in*)&storage)->sin_port = HostToInet(Port); + break; + } + case ADDR_IPV6: { + ((sockaddr_in6*)&storage)->sin6_addr = IpAddr.GetIn6Addr(); + ((sockaddr_in6*)&storage)->sin6_port = HostToInet(Port); + ((sockaddr_in6*)&storage)->sin6_scope_id = HostToInet(IPv6ScopeID); + break; + } } - return TNetAddr(new TOpaqueAddr((sockaddr*)&storage)); + return TNetAddr(new TOpaqueAddr((sockaddr*)&storage)); } template <> diff --git a/library/cpp/messagebus/socket_addr.h b/library/cpp/messagebus/socket_addr.h index e66735c458..959eafe689 100644 --- a/library/cpp/messagebus/socket_addr.h +++ b/library/cpp/messagebus/socket_addr.h @@ -13,90 +13,90 @@ namespace NBus { class TNetAddr; } -namespace NBus { - namespace NPrivate { - enum EAddrFamily { - ADDR_UNSPEC = AF_UNSPEC, - ADDR_IPV4 = AF_INET, - ADDR_IPV6 = AF_INET6, - }; - - class TBusIpAddr { - private: - EAddrFamily Af; - - union { - in_addr In4; - in6_addr In6; - }; - - public: - TBusIpAddr() { - Clear(); - } - - EAddrFamily GetAddrFamily() const { - return Af; - } - - void Clear() { - Zero(*this); - } - - in_addr GetInAddr() const { - Y_ASSERT(Af == ADDR_IPV4); - return In4; - } - - void SetInAddr(const in_addr& in4) { - Clear(); - Af = ADDR_IPV4; - In4 = in4; - } - - in6_addr GetIn6Addr() const { - Y_ASSERT(Af == ADDR_IPV6); - return In6; - } - - void SetIn6Addr(const in6_addr& in6) { - Clear(); - Af = ADDR_IPV6; - In6 = in6; - } - - bool operator==(const TBusIpAddr& that) const { - return memcmp(this, &that, sizeof(that)) == 0; - } - }; - - class TBusSocketAddr { - public: - TBusIpAddr IpAddr; - ui16 Port; - - //Only makes sense for IPv6 link-local addresses - ui32 IPv6ScopeID; - - TBusSocketAddr() - : Port(0) - , IPv6ScopeID(0) - { - } - - TBusSocketAddr(const NAddr::IRemoteAddr*); - TBusSocketAddr(const TNetAddr&); - TBusSocketAddr(TStringBuf host, unsigned port); - - TNetAddr ToNetAddr() const; - - bool operator==(const TBusSocketAddr& that) const { - return IpAddr == that.IpAddr && Port == that.Port; - } - }; - - } -} +namespace NBus { + namespace NPrivate { + enum EAddrFamily { + ADDR_UNSPEC = AF_UNSPEC, + ADDR_IPV4 = AF_INET, + ADDR_IPV6 = AF_INET6, + }; + + class TBusIpAddr { + private: + EAddrFamily Af; + + union { + in_addr In4; + in6_addr In6; + }; + + public: + TBusIpAddr() { + Clear(); + } + + EAddrFamily GetAddrFamily() const { + return Af; + } + + void Clear() { + Zero(*this); + } + + in_addr GetInAddr() const { + Y_ASSERT(Af == ADDR_IPV4); + return In4; + } + + void SetInAddr(const in_addr& in4) { + Clear(); + Af = ADDR_IPV4; + In4 = in4; + } + + in6_addr GetIn6Addr() const { + Y_ASSERT(Af == ADDR_IPV6); + return In6; + } + + void SetIn6Addr(const in6_addr& in6) { + Clear(); + Af = ADDR_IPV6; + In6 = in6; + } + + bool operator==(const TBusIpAddr& that) const { + return memcmp(this, &that, sizeof(that)) == 0; + } + }; + + class TBusSocketAddr { + public: + TBusIpAddr IpAddr; + ui16 Port; + + //Only makes sense for IPv6 link-local addresses + ui32 IPv6ScopeID; + + TBusSocketAddr() + : Port(0) + , IPv6ScopeID(0) + { + } + + TBusSocketAddr(const NAddr::IRemoteAddr*); + TBusSocketAddr(const TNetAddr&); + TBusSocketAddr(TStringBuf host, unsigned port); + + TNetAddr ToNetAddr() const; + + bool operator==(const TBusSocketAddr& that) const { + return IpAddr == that.IpAddr && Port == that.Port; + } + }; + + } +} template <> struct THash<NBus::NPrivate::TBusIpAddr> { diff --git a/library/cpp/messagebus/storage.cpp b/library/cpp/messagebus/storage.cpp index 030284a74c..efefc87340 100644 --- a/library/cpp/messagebus/storage.cpp +++ b/library/cpp/messagebus/storage.cpp @@ -2,159 +2,159 @@ #include <typeinfo> -namespace NBus { - namespace NPrivate { - TTimedMessages::TTimedMessages() { - } - - TTimedMessages::~TTimedMessages() { - Y_VERIFY(Items.empty()); - } - - void TTimedMessages::PushBack(TNonDestroyingAutoPtr<TBusMessage> m) { - TItem i; - i.Message.Reset(m.Release()); - Items.push_back(i); - } - - TNonDestroyingAutoPtr<TBusMessage> TTimedMessages::PopFront() { - TBusMessage* r = nullptr; - if (!Items.empty()) { - r = Items.front()->Message.Release(); - Items.pop_front(); - } - return r; - } - - bool TTimedMessages::Empty() const { - return Items.empty(); - } - - size_t TTimedMessages::Size() const { - return Items.size(); - } - - void TTimedMessages::Timeout(TInstant before, TMessagesPtrs* r) { - // shortcut - if (before == TInstant::Max()) { - Clear(r); - return; - } - - while (!Items.empty()) { - TItem& i = *Items.front(); - if (TInstant::MilliSeconds(i.Message->GetHeader()->SendTime) > before) { - break; - } - r->push_back(i.Message.Release()); - Items.pop_front(); - } - } - - void TTimedMessages::Clear(TMessagesPtrs* r) { - while (!Items.empty()) { - r->push_back(Items.front()->Message.Release()); - Items.pop_front(); - } - } - - TSyncAckMessages::TSyncAckMessages() { - KeyToMessage.set_empty_key(0); - KeyToMessage.set_deleted_key(1); - } - - TSyncAckMessages::~TSyncAckMessages() { - Y_VERIFY(KeyToMessage.empty()); - Y_VERIFY(TimedItems.empty()); - } - - void TSyncAckMessages::Push(TBusMessagePtrAndHeader& m) { - // Perform garbage collection if `TimedMessages` contain too many junk data - if (TimedItems.size() > 1000 && TimedItems.size() > KeyToMessage.size() * 4) { - Gc(); - } - - TValue value = {m.MessagePtr.Release()}; - - std::pair<TKeyToMessage::iterator, bool> p = KeyToMessage.insert(TKeyToMessage::value_type(m.Header.Id, value)); +namespace NBus { + namespace NPrivate { + TTimedMessages::TTimedMessages() { + } + + TTimedMessages::~TTimedMessages() { + Y_VERIFY(Items.empty()); + } + + void TTimedMessages::PushBack(TNonDestroyingAutoPtr<TBusMessage> m) { + TItem i; + i.Message.Reset(m.Release()); + Items.push_back(i); + } + + TNonDestroyingAutoPtr<TBusMessage> TTimedMessages::PopFront() { + TBusMessage* r = nullptr; + if (!Items.empty()) { + r = Items.front()->Message.Release(); + Items.pop_front(); + } + return r; + } + + bool TTimedMessages::Empty() const { + return Items.empty(); + } + + size_t TTimedMessages::Size() const { + return Items.size(); + } + + void TTimedMessages::Timeout(TInstant before, TMessagesPtrs* r) { + // shortcut + if (before == TInstant::Max()) { + Clear(r); + return; + } + + while (!Items.empty()) { + TItem& i = *Items.front(); + if (TInstant::MilliSeconds(i.Message->GetHeader()->SendTime) > before) { + break; + } + r->push_back(i.Message.Release()); + Items.pop_front(); + } + } + + void TTimedMessages::Clear(TMessagesPtrs* r) { + while (!Items.empty()) { + r->push_back(Items.front()->Message.Release()); + Items.pop_front(); + } + } + + TSyncAckMessages::TSyncAckMessages() { + KeyToMessage.set_empty_key(0); + KeyToMessage.set_deleted_key(1); + } + + TSyncAckMessages::~TSyncAckMessages() { + Y_VERIFY(KeyToMessage.empty()); + Y_VERIFY(TimedItems.empty()); + } + + void TSyncAckMessages::Push(TBusMessagePtrAndHeader& m) { + // Perform garbage collection if `TimedMessages` contain too many junk data + if (TimedItems.size() > 1000 && TimedItems.size() > KeyToMessage.size() * 4) { + Gc(); + } + + TValue value = {m.MessagePtr.Release()}; + + std::pair<TKeyToMessage::iterator, bool> p = KeyToMessage.insert(TKeyToMessage::value_type(m.Header.Id, value)); Y_VERIFY(p.second, "non-unique id; %s", value.Message->Describe().data()); - TTimedItem item = {m.Header.Id, m.Header.SendTime}; - TimedItems.push_back(item); - } + TTimedItem item = {m.Header.Id, m.Header.SendTime}; + TimedItems.push_back(item); + } - TBusMessage* TSyncAckMessages::Pop(TBusKey id) { - TKeyToMessage::iterator it = KeyToMessage.find(id); - if (it == KeyToMessage.end()) { - return nullptr; - } - TValue v = it->second; - KeyToMessage.erase(it); + TBusMessage* TSyncAckMessages::Pop(TBusKey id) { + TKeyToMessage::iterator it = KeyToMessage.find(id); + if (it == KeyToMessage.end()) { + return nullptr; + } + TValue v = it->second; + KeyToMessage.erase(it); - // `TimedMessages` still contain record about this message + // `TimedMessages` still contain record about this message - return v.Message; - } + return v.Message; + } - void TSyncAckMessages::Timeout(TInstant before, TMessagesPtrs* r) { - // shortcut - if (before == TInstant::Max()) { - Clear(r); - return; - } + void TSyncAckMessages::Timeout(TInstant before, TMessagesPtrs* r) { + // shortcut + if (before == TInstant::Max()) { + Clear(r); + return; + } - Y_ASSERT(r->empty()); + Y_ASSERT(r->empty()); - while (!TimedItems.empty()) { - TTimedItem i = TimedItems.front(); - if (TInstant::MilliSeconds(i.SendTime) > before) { - break; - } + while (!TimedItems.empty()) { + TTimedItem i = TimedItems.front(); + if (TInstant::MilliSeconds(i.SendTime) > before) { + break; + } - TKeyToMessage::iterator itMessage = KeyToMessage.find(i.Key); + TKeyToMessage::iterator itMessage = KeyToMessage.find(i.Key); - if (itMessage != KeyToMessage.end()) { - r->push_back(itMessage->second.Message); - KeyToMessage.erase(itMessage); - } + if (itMessage != KeyToMessage.end()) { + r->push_back(itMessage->second.Message); + KeyToMessage.erase(itMessage); + } - TimedItems.pop_front(); - } + TimedItems.pop_front(); + } } - void TSyncAckMessages::Clear(TMessagesPtrs* r) { - for (TKeyToMessage::const_iterator i = KeyToMessage.begin(); i != KeyToMessage.end(); ++i) { - r->push_back(i->second.Message); - } + void TSyncAckMessages::Clear(TMessagesPtrs* r) { + for (TKeyToMessage::const_iterator i = KeyToMessage.begin(); i != KeyToMessage.end(); ++i) { + r->push_back(i->second.Message); + } - KeyToMessage.clear(); - TimedItems.clear(); + KeyToMessage.clear(); + TimedItems.clear(); } - void TSyncAckMessages::Gc() { - TDeque<TTimedItem> tmp; + void TSyncAckMessages::Gc() { + TDeque<TTimedItem> tmp; - for (auto& timedItem : TimedItems) { - if (KeyToMessage.find(timedItem.Key) == KeyToMessage.end()) { - continue; - } - tmp.push_back(timedItem); - } + for (auto& timedItem : TimedItems) { + if (KeyToMessage.find(timedItem.Key) == KeyToMessage.end()) { + continue; + } + tmp.push_back(timedItem); + } - TimedItems.swap(tmp); - } + TimedItems.swap(tmp); + } - void TSyncAckMessages::RemoveAll(const TMessagesPtrs& messages) { - for (auto message : messages) { - TKeyToMessage::iterator it = KeyToMessage.find(message->GetHeader()->Id); - Y_VERIFY(it != KeyToMessage.end(), "delete non-existent message"); - KeyToMessage.erase(it); - } - } + void TSyncAckMessages::RemoveAll(const TMessagesPtrs& messages) { + for (auto message : messages) { + TKeyToMessage::iterator it = KeyToMessage.find(message->GetHeader()->Id); + Y_VERIFY(it != KeyToMessage.end(), "delete non-existent message"); + KeyToMessage.erase(it); + } + } - void TSyncAckMessages::DumpState() { - Cerr << TimedItems.size() << Endl; - Cerr << KeyToMessage.size() << Endl; + void TSyncAckMessages::DumpState() { + Cerr << TimedItems.size() << Endl; + Cerr << KeyToMessage.size() << Endl; } } diff --git a/library/cpp/messagebus/storage.h b/library/cpp/messagebus/storage.h index 694ea2fcfe..7d168844ed 100644 --- a/library/cpp/messagebus/storage.h +++ b/library/cpp/messagebus/storage.h @@ -10,85 +10,85 @@ #include <util/generic/noncopyable.h> #include <util/generic/utility.h> -namespace NBus { - namespace NPrivate { - typedef TVector<TBusMessage*> TMessagesPtrs; +namespace NBus { + namespace NPrivate { + typedef TVector<TBusMessage*> TMessagesPtrs; - class TTimedMessages { - public: - TTimedMessages(); - ~TTimedMessages(); + class TTimedMessages { + public: + TTimedMessages(); + ~TTimedMessages(); - struct TItem { - THolder<TBusMessage> Message; + struct TItem { + THolder<TBusMessage> Message; - void Swap(TItem& that) { - DoSwap(Message, that.Message); - } - }; + void Swap(TItem& that) { + DoSwap(Message, that.Message); + } + }; - typedef TDeque<TMoved<TItem>> TItems; + typedef TDeque<TMoved<TItem>> TItems; - void PushBack(TNonDestroyingAutoPtr<TBusMessage> m); - TNonDestroyingAutoPtr<TBusMessage> PopFront(); - bool Empty() const; - size_t Size() const; + void PushBack(TNonDestroyingAutoPtr<TBusMessage> m); + TNonDestroyingAutoPtr<TBusMessage> PopFront(); + bool Empty() const; + size_t Size() const; - void Timeout(TInstant before, TMessagesPtrs* r); - void Clear(TMessagesPtrs* r); + void Timeout(TInstant before, TMessagesPtrs* r); + void Clear(TMessagesPtrs* r); - private: - TItems Items; - }; + private: + TItems Items; + }; - class TSyncAckMessages : TNonCopyable { - public: - TSyncAckMessages(); - ~TSyncAckMessages(); + class TSyncAckMessages : TNonCopyable { + public: + TSyncAckMessages(); + ~TSyncAckMessages(); - void Push(TBusMessagePtrAndHeader& m); - TBusMessage* Pop(TBusKey id); + void Push(TBusMessagePtrAndHeader& m); + TBusMessage* Pop(TBusKey id); - void Timeout(TInstant before, TMessagesPtrs* r); + void Timeout(TInstant before, TMessagesPtrs* r); - void Clear(TMessagesPtrs* r); + void Clear(TMessagesPtrs* r); - size_t Size() const { - return KeyToMessage.size(); - } + size_t Size() const { + return KeyToMessage.size(); + } - void RemoveAll(const TMessagesPtrs&); + void RemoveAll(const TMessagesPtrs&); - void Gc(); + void Gc(); - void DumpState(); + void DumpState(); - private: - struct TTimedItem { - TBusKey Key; - TBusInstant SendTime; - }; + private: + struct TTimedItem { + TBusKey Key; + TBusInstant SendTime; + }; - typedef TDeque<TTimedItem> TTimedItems; - typedef TDeque<TTimedItem>::iterator TTimedIterator; + typedef TDeque<TTimedItem> TTimedItems; + typedef TDeque<TTimedItem>::iterator TTimedIterator; - TTimedItems TimedItems; + TTimedItems TimedItems; - struct TValue { - TBusMessage* Message; - }; + struct TValue { + TBusMessage* Message; + }; - // keys are already random, no need to hash them further - struct TIdHash { - size_t operator()(TBusKey value) const { - return value; - } - }; + // keys are already random, no need to hash them further + struct TIdHash { + size_t operator()(TBusKey value) const { + return value; + } + }; typedef google::dense_hash_map<TBusKey, TValue, TIdHash> TKeyToMessage; - TKeyToMessage KeyToMessage; + TKeyToMessage KeyToMessage; }; - } -} + } +} diff --git a/library/cpp/messagebus/synchandler.cpp b/library/cpp/messagebus/synchandler.cpp index 3498efbb21..8e891d66b3 100644 --- a/library/cpp/messagebus/synchandler.cpp +++ b/library/cpp/messagebus/synchandler.cpp @@ -13,43 +13,43 @@ using namespace NBus::NPrivate; /// 3. Message reply /// 4. Reply status struct TBusSyncMessageData { - TCondVar ReplyEvent; - TMutex ReplyLock; - TBusMessage* Reply; + TCondVar ReplyEvent; + TMutex ReplyLock; + TBusMessage* Reply; EMessageStatus ReplyStatus; TBusSyncMessageData() : Reply(nullptr) , ReplyStatus(MESSAGE_DONT_ASK) - { - } + { + } }; -class TSyncHandler: public IBusClientHandler { +class TSyncHandler: public IBusClientHandler { public: TSyncHandler(bool expectReply = true) : ExpectReply(expectReply) , Session(nullptr) - { - } - ~TSyncHandler() override { - } + { + } + ~TSyncHandler() override { + } void OnReply(TAutoPtr<TBusMessage> pMessage0, TAutoPtr<TBusMessage> pReply0) override { TBusMessage* pMessage = pMessage0.Release(); TBusMessage* pReply = pReply0.Release(); - if (!ExpectReply) { // Maybe need VERIFY, but it will be better to support backward compatibility here. + if (!ExpectReply) { // Maybe need VERIFY, but it will be better to support backward compatibility here. return; } - TBusSyncMessageData* data = static_cast<TBusSyncMessageData*>(pMessage->Data); + TBusSyncMessageData* data = static_cast<TBusSyncMessageData*>(pMessage->Data); SignalResult(data, pReply, MESSAGE_OK); } void OnError(TAutoPtr<TBusMessage> pMessage0, EMessageStatus status) override { TBusMessage* pMessage = pMessage0.Release(); - TBusSyncMessageData* data = static_cast<TBusSyncMessageData*>(pMessage->Data); + TBusSyncMessageData* data = static_cast<TBusSyncMessageData*>(pMessage->Data); if (!data) { return; } @@ -64,7 +64,7 @@ public: void OnMessageSentOneWay(TAutoPtr<TBusMessage> pMessage) override { Y_ASSERT(!ExpectReply); - TBusSyncMessageData* data = static_cast<TBusSyncMessageData*>(pMessage.Release()->Data); + TBusSyncMessageData* data = static_cast<TBusSyncMessageData*>(pMessage.Release()->Data); SignalResult(data, /*pReply=*/nullptr, MESSAGE_OK); } @@ -75,7 +75,7 @@ public: } private: - void SignalResult(TBusSyncMessageData* data, TBusMessage* pReply, EMessageStatus status) const { + void SignalResult(TBusSyncMessageData* data, TBusMessage* pReply, EMessageStatus status) const { Y_VERIFY(data, "Message data is set to NULL."); TGuard<TMutex> G(data->ReplyLock); data->Reply = pReply; @@ -89,76 +89,76 @@ private: TRemoteClientSession* Session; }; -namespace NBus { - namespace NPrivate { +namespace NBus { + namespace NPrivate { #ifdef _MSC_VER #pragma warning(push) -#pragma warning(disable : 4250) // 'NBus::NPrivate::TRemoteClientSession' : inherits 'NBus::NPrivate::TBusSessionImpl::NBus::NPrivate::TBusSessionImpl::GetConfig' via dominance +#pragma warning(disable : 4250) // 'NBus::NPrivate::TRemoteClientSession' : inherits 'NBus::NPrivate::TBusSessionImpl::NBus::NPrivate::TBusSessionImpl::GetConfig' via dominance #endif - /////////////////////////////////////////////////////////////////////////// - class TBusSyncSourceSessionImpl - : private TSyncHandler - // TODO: do not extend TRemoteClientSession - , - public TRemoteClientSession { - private: - bool NeedReply; - - public: - TBusSyncSourceSessionImpl(TBusMessageQueue* queue, TBusProtocol* proto, const TBusClientSessionConfig& config, bool needReply, const TString& name) - : TSyncHandler(needReply) - , TRemoteClientSession(queue, proto, this, config, name) - , NeedReply(needReply) - { - SetSession(this); - } - - TBusMessage* SendSyncMessage(TBusMessage* pMessage, EMessageStatus& status, const TNetAddr* addr = nullptr) { - Y_VERIFY(!Queue->GetExecutor()->IsInExecutorThread(), - "SendSyncMessage must not be called from executor thread"); - - TBusMessage* reply = nullptr; - THolder<TBusSyncMessageData> data(new TBusSyncMessageData()); - - pMessage->Data = data.Get(); - - { - TGuard<TMutex> G(data->ReplyLock); - if (NeedReply) { - status = SendMessage(pMessage, addr, false); // probably should be true - } else { - status = SendMessageOneWay(pMessage, addr); - } - - if (status == MESSAGE_OK) { - data->ReplyEvent.Wait(data->ReplyLock); - TBusSyncMessageData* rdata = static_cast<TBusSyncMessageData*>(pMessage->Data); - Y_VERIFY(rdata == data.Get(), "Message data pointer should not be modified."); - reply = rdata->Reply; - status = rdata->ReplyStatus; - } - } - - // deletion of message and reply is a job of application. - pMessage->Data = nullptr; - - return reply; - } - }; - + /////////////////////////////////////////////////////////////////////////// + class TBusSyncSourceSessionImpl + : private TSyncHandler + // TODO: do not extend TRemoteClientSession + , + public TRemoteClientSession { + private: + bool NeedReply; + + public: + TBusSyncSourceSessionImpl(TBusMessageQueue* queue, TBusProtocol* proto, const TBusClientSessionConfig& config, bool needReply, const TString& name) + : TSyncHandler(needReply) + , TRemoteClientSession(queue, proto, this, config, name) + , NeedReply(needReply) + { + SetSession(this); + } + + TBusMessage* SendSyncMessage(TBusMessage* pMessage, EMessageStatus& status, const TNetAddr* addr = nullptr) { + Y_VERIFY(!Queue->GetExecutor()->IsInExecutorThread(), + "SendSyncMessage must not be called from executor thread"); + + TBusMessage* reply = nullptr; + THolder<TBusSyncMessageData> data(new TBusSyncMessageData()); + + pMessage->Data = data.Get(); + + { + TGuard<TMutex> G(data->ReplyLock); + if (NeedReply) { + status = SendMessage(pMessage, addr, false); // probably should be true + } else { + status = SendMessageOneWay(pMessage, addr); + } + + if (status == MESSAGE_OK) { + data->ReplyEvent.Wait(data->ReplyLock); + TBusSyncMessageData* rdata = static_cast<TBusSyncMessageData*>(pMessage->Data); + Y_VERIFY(rdata == data.Get(), "Message data pointer should not be modified."); + reply = rdata->Reply; + status = rdata->ReplyStatus; + } + } + + // deletion of message and reply is a job of application. + pMessage->Data = nullptr; + + return reply; + } + }; + #ifdef _MSC_VER #pragma warning(pop) #endif - } -} + } +} TBusSyncSourceSession::TBusSyncSourceSession(TIntrusivePtr< ::NBus::NPrivate::TBusSyncSourceSessionImpl> session) : Session(session) -{ -} +{ +} -TBusSyncSourceSession::~TBusSyncSourceSession() { +TBusSyncSourceSession::~TBusSyncSourceSession() { Shutdown(); } @@ -170,7 +170,7 @@ TBusMessage* TBusSyncSourceSession::SendSyncMessage(TBusMessage* pMessage, EMess return Session->SendSyncMessage(pMessage, status, addr); } -int TBusSyncSourceSession::RegisterService(const char* hostname, TBusKey start, TBusKey end, EIpVersion ipVersion) { +int TBusSyncSourceSession::RegisterService(const char* hostname, TBusKey start, TBusKey end, EIpVersion ipVersion) { return Session->RegisterService(hostname, start, end, ipVersion); } diff --git a/library/cpp/messagebus/test/example/client/client.cpp b/library/cpp/messagebus/test/example/client/client.cpp index 0a4097f5f4..89b5f2c9be 100644 --- a/library/cpp/messagebus/test/example/client/client.cpp +++ b/library/cpp/messagebus/test/example/client/client.cpp @@ -6,7 +6,7 @@ using namespace NBus; using namespace NCalculator; namespace NCalculator { - struct TCalculatorClient: public IBusClientHandler { + struct TCalculatorClient: public IBusClientHandler { TCalculatorProtocol Proto; TBusMessageQueuePtr MessageQueue; TBusClientSessionPtr ClientSession; diff --git a/library/cpp/messagebus/test/example/common/proto.h b/library/cpp/messagebus/test/example/common/proto.h index 904dbad713..a151aac468 100644 --- a/library/cpp/messagebus/test/example/common/proto.h +++ b/library/cpp/messagebus/test/example/common/proto.h @@ -10,7 +10,7 @@ namespace NCalculator { typedef ::NBus::TBusBufferMessage<TRequestMulRecord, 2> TRequestMul; typedef ::NBus::TBusBufferMessage<TResponseRecord, 3> TResponse; - struct TCalculatorProtocol: public ::NBus::TBusBufferProtocol { + struct TCalculatorProtocol: public ::NBus::TBusBufferProtocol { TCalculatorProtocol(); }; diff --git a/library/cpp/messagebus/test/example/server/server.cpp b/library/cpp/messagebus/test/example/server/server.cpp index 1d065c1ef6..13e52d75f5 100644 --- a/library/cpp/messagebus/test/example/server/server.cpp +++ b/library/cpp/messagebus/test/example/server/server.cpp @@ -4,7 +4,7 @@ using namespace NBus; using namespace NCalculator; namespace NCalculator { - struct TCalculatorServer: public IBusServerHandler { + struct TCalculatorServer: public IBusServerHandler { TCalculatorProtocol Proto; TBusMessageQueuePtr MessageQueue; TBusServerSessionPtr ServerSession; @@ -43,7 +43,7 @@ namespace NCalculator { } } }; -} +} int main(int, char**) { TCalculatorServer server; diff --git a/library/cpp/messagebus/test/helper/alloc_counter.h b/library/cpp/messagebus/test/helper/alloc_counter.h index 7011b61b9d..ec9041cb15 100644 --- a/library/cpp/messagebus/test/helper/alloc_counter.h +++ b/library/cpp/messagebus/test/helper/alloc_counter.h @@ -4,14 +4,14 @@ #include <util/system/atomic.h> #include <util/system/yassert.h> -class TAllocCounter : TNonCopyable { +class TAllocCounter : TNonCopyable { private: TAtomic* CountPtr; - + public: - TAllocCounter(TAtomic* countPtr) - : CountPtr(countPtr) - { + TAllocCounter(TAtomic* countPtr) + : CountPtr(countPtr) + { AtomicIncrement(*CountPtr); } diff --git a/library/cpp/messagebus/test/helper/example.cpp b/library/cpp/messagebus/test/helper/example.cpp index 6260541e1b..7c6d704042 100644 --- a/library/cpp/messagebus/test/helper/example.cpp +++ b/library/cpp/messagebus/test/helper/example.cpp @@ -9,9 +9,9 @@ using namespace NBus::NTest; static void FillWithJunk(TArrayRef<char> data) { TStringBuf junk = - "01234567890123456789012345678901234567890123456789012345678901234567890123456789" - "01234567890123456789012345678901234567890123456789012345678901234567890123456789" - "01234567890123456789012345678901234567890123456789012345678901234567890123456789" + "01234567890123456789012345678901234567890123456789012345678901234567890123456789" + "01234567890123456789012345678901234567890123456789012345678901234567890123456789" + "01234567890123456789012345678901234567890123456789012345678901234567890123456789" "01234567890123456789012345678901234567890123456789012345678901234567890123456789"; for (size_t i = 0; i < data.size(); i += junk.size()) { @@ -37,8 +37,8 @@ TExampleRequest::TExampleRequest(TAtomic* counterPtr, size_t payloadSize) TExampleRequest::TExampleRequest(ECreateUninitialized, TAtomic* counterPtr) : TBusMessage(MESSAGE_CREATE_UNINITIALIZED) , AllocCounter(counterPtr) -{ -} +{ +} TExampleResponse::TExampleResponse(TAtomic* counterPtr, size_t payloadSize) : TBusMessage(79) @@ -50,8 +50,8 @@ TExampleResponse::TExampleResponse(TAtomic* counterPtr, size_t payloadSize) TExampleResponse::TExampleResponse(ECreateUninitialized, TAtomic* counterPtr) : TBusMessage(MESSAGE_CREATE_UNINITIALIZED) , AllocCounter(counterPtr) -{ -} +{ +} TExampleProtocol::TExampleProtocol(int port) : TBusProtocol("Example", port) @@ -60,8 +60,8 @@ TExampleProtocol::TExampleProtocol(int port) , RequestCountDeserialized(0) , ResponseCountDeserialized(0) , StartCount(0) -{ -} +{ +} TExampleProtocol::~TExampleProtocol() { if (UncaughtException()) { @@ -124,13 +124,13 @@ TExampleClient::TExampleClient(const TBusClientSessionConfig sessionConfig, int TExampleClient::~TExampleClient() { } -EMessageStatus TExampleClient::SendMessage(const TNetAddr* addr) { +EMessageStatus TExampleClient::SendMessage(const TNetAddr* addr) { TAutoPtr<TExampleRequest> message(new TExampleRequest(&Proto.RequestCount, DataSize)); message->SetCompressed(UseCompression); return Session->SendMessageAutoPtr(message, addr); } -void TExampleClient::SendMessages(size_t count, const TNetAddr* addr) { +void TExampleClient::SendMessages(size_t count, const TNetAddr* addr) { UNIT_ASSERT(MessageCount == 0); UNIT_ASSERT(RepliesCount == 0); UNIT_ASSERT(Errors == 0); @@ -184,7 +184,7 @@ void TExampleClient::WaitForError(EMessageStatus status) { UNIT_ASSERT_VALUES_EQUAL(status, error); } -void TExampleClient::SendMessagesWaitReplies(size_t count, const TNetAddr* addr) { +void TExampleClient::SendMessagesWaitReplies(size_t count, const TNetAddr* addr) { SendMessages(count, addr); WaitReplies(); } @@ -215,8 +215,8 @@ void TExampleClient::OnError(TAutoPtr<TBusMessage> mess, EMessageStatus status) } TExampleServer::TExampleServer( - const char* name, - const TBusServerSessionConfig& sessionConfig) + const char* name, + const TBusServerSessionConfig& sessionConfig) : UseCompression(false) , AckMessageBeforeSendReply(false) , ForgetRequest(false) diff --git a/library/cpp/messagebus/test/helper/example.h b/library/cpp/messagebus/test/helper/example.h index 819562719d..26b7475308 100644 --- a/library/cpp/messagebus/test/helper/example.h +++ b/library/cpp/messagebus/test/helper/example.h @@ -10,123 +10,123 @@ #include <util/system/event.h> -namespace NBus { - namespace NTest { - class TExampleRequest: public TBusMessage { - friend class TExampleProtocol; - - private: - TAllocCounter AllocCounter; - - public: - TString Data; - - public: - TExampleRequest(TAtomic* counterPtr, size_t payloadSize = 320); - TExampleRequest(ECreateUninitialized, TAtomic* counterPtr); - }; - - class TExampleResponse: public TBusMessage { - friend class TExampleProtocol; - - private: - TAllocCounter AllocCounter; - - public: - TString Data; - TExampleResponse(TAtomic* counterPtr, size_t payloadSize = 320); - TExampleResponse(ECreateUninitialized, TAtomic* counterPtr); - }; - - class TExampleProtocol: public TBusProtocol { - public: - TAtomic RequestCount; - TAtomic ResponseCount; - TAtomic RequestCountDeserialized; - TAtomic ResponseCountDeserialized; - TAtomic StartCount; - - TExampleProtocol(int port = 0); - - ~TExampleProtocol() override; - - void Serialize(const TBusMessage* message, TBuffer& buffer) override; - - TAutoPtr<TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) override; - }; - - class TExampleClient: private TBusClientHandlerError { - public: - TExampleProtocol Proto; - bool UseCompression; - bool CrashOnError; - size_t DataSize; - - ssize_t MessageCount; - TAtomic RepliesCount; - TAtomic Errors; - EMessageStatus LastError; +namespace NBus { + namespace NTest { + class TExampleRequest: public TBusMessage { + friend class TExampleProtocol; + + private: + TAllocCounter AllocCounter; + + public: + TString Data; + + public: + TExampleRequest(TAtomic* counterPtr, size_t payloadSize = 320); + TExampleRequest(ECreateUninitialized, TAtomic* counterPtr); + }; + + class TExampleResponse: public TBusMessage { + friend class TExampleProtocol; + + private: + TAllocCounter AllocCounter; + + public: + TString Data; + TExampleResponse(TAtomic* counterPtr, size_t payloadSize = 320); + TExampleResponse(ECreateUninitialized, TAtomic* counterPtr); + }; + + class TExampleProtocol: public TBusProtocol { + public: + TAtomic RequestCount; + TAtomic ResponseCount; + TAtomic RequestCountDeserialized; + TAtomic ResponseCountDeserialized; + TAtomic StartCount; + + TExampleProtocol(int port = 0); + + ~TExampleProtocol() override; + + void Serialize(const TBusMessage* message, TBuffer& buffer) override; + + TAutoPtr<TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) override; + }; + + class TExampleClient: private TBusClientHandlerError { + public: + TExampleProtocol Proto; + bool UseCompression; + bool CrashOnError; + size_t DataSize; + + ssize_t MessageCount; + TAtomic RepliesCount; + TAtomic Errors; + EMessageStatus LastError; TSystemEvent WorkDone; - TBusMessageQueuePtr Bus; - TBusClientSessionPtr Session; + TBusMessageQueuePtr Bus; + TBusClientSessionPtr Session; - public: - TExampleClient(const TBusClientSessionConfig sessionConfig = TBusClientSessionConfig(), int port = 0); - ~TExampleClient() override; + public: + TExampleClient(const TBusClientSessionConfig sessionConfig = TBusClientSessionConfig(), int port = 0); + ~TExampleClient() override; - EMessageStatus SendMessage(const TNetAddr* addr = nullptr); + EMessageStatus SendMessage(const TNetAddr* addr = nullptr); - void SendMessages(size_t count, const TNetAddr* addr = nullptr); - void SendMessages(size_t count, const TNetAddr& addr); + void SendMessages(size_t count, const TNetAddr* addr = nullptr); + void SendMessages(size_t count, const TNetAddr& addr); - void ResetCounters(); - void WaitReplies(); - EMessageStatus WaitForError(); - void WaitForError(EMessageStatus status); + void ResetCounters(); + void WaitReplies(); + EMessageStatus WaitForError(); + void WaitForError(EMessageStatus status); - void SendMessagesWaitReplies(size_t count, const TNetAddr* addr = nullptr); - void SendMessagesWaitReplies(size_t count, const TNetAddr& addr); + void SendMessagesWaitReplies(size_t count, const TNetAddr* addr = nullptr); + void SendMessagesWaitReplies(size_t count, const TNetAddr& addr); - void OnReply(TAutoPtr<TBusMessage> mess, TAutoPtr<TBusMessage> reply) override; + void OnReply(TAutoPtr<TBusMessage> mess, TAutoPtr<TBusMessage> reply) override; - void OnError(TAutoPtr<TBusMessage> mess, EMessageStatus) override; - }; + void OnError(TAutoPtr<TBusMessage> mess, EMessageStatus) override; + }; - class TExampleServer: private TBusServerHandlerError { - public: - TExampleProtocol Proto; - bool UseCompression; - bool AckMessageBeforeSendReply; - TMaybe<size_t> DataSize; // Nothing means use request size - bool ForgetRequest; + class TExampleServer: private TBusServerHandlerError { + public: + TExampleProtocol Proto; + bool UseCompression; + bool AckMessageBeforeSendReply; + TMaybe<size_t> DataSize; // Nothing means use request size + bool ForgetRequest; - TTestSync TestSync; + TTestSync TestSync; - TBusMessageQueuePtr Bus; - TBusServerSessionPtr Session; + TBusMessageQueuePtr Bus; + TBusServerSessionPtr Session; - public: - TExampleServer( - const char* name = "TExampleServer", - const TBusServerSessionConfig& sessionConfig = TBusServerSessionConfig()); + public: + TExampleServer( + const char* name = "TExampleServer", + const TBusServerSessionConfig& sessionConfig = TBusServerSessionConfig()); - TExampleServer(unsigned port, const char* name = "TExampleServer"); + TExampleServer(unsigned port, const char* name = "TExampleServer"); - ~TExampleServer() override; + ~TExampleServer() override; - public: - size_t GetInFlight() const; - unsigned GetActualListenPort() const; - // any of - TNetAddr GetActualListenAddr() const; + public: + size_t GetInFlight() const; + unsigned GetActualListenPort() const; + // any of + TNetAddr GetActualListenAddr() const; - void WaitForOnMessageCount(unsigned n); + void WaitForOnMessageCount(unsigned n); - protected: - void OnMessage(TOnMessageContext& mess) override; - }; + protected: + void OnMessage(TOnMessageContext& mess) override; + }; - } -} + } +} diff --git a/library/cpp/messagebus/test/helper/example_module.cpp b/library/cpp/messagebus/test/helper/example_module.cpp index 26dc184b16..65ecfcf73f 100644 --- a/library/cpp/messagebus/test/helper/example_module.cpp +++ b/library/cpp/messagebus/test/helper/example_module.cpp @@ -31,10 +31,10 @@ TBusServerSessionPtr TExampleServerModule::CreateExtSession(TBusMessageQueue& qu return r; } -TExampleClientModule::TExampleClientModule() - : Source() -{ -} +TExampleClientModule::TExampleClientModule() + : Source() +{ +} TBusServerSessionPtr TExampleClientModule::CreateExtSession(TBusMessageQueue& queue) { Source = CreateDefaultSource(queue, &Proto, TBusServerSessionConfig()); diff --git a/library/cpp/messagebus/test/helper/example_module.h b/library/cpp/messagebus/test/helper/example_module.h index 1f00b25990..a0b295f613 100644 --- a/library/cpp/messagebus/test/helper/example_module.h +++ b/library/cpp/messagebus/test/helper/example_module.h @@ -4,34 +4,34 @@ #include <library/cpp/messagebus/oldmodule/module.h> -namespace NBus { - namespace NTest { - struct TExampleModule: public TBusModule { - TExampleProtocol Proto; - TBusMessageQueuePtr Queue; +namespace NBus { + namespace NTest { + struct TExampleModule: public TBusModule { + TExampleProtocol Proto; + TBusMessageQueuePtr Queue; - TExampleModule(); + TExampleModule(); - void StartModule(); + void StartModule(); - bool Shutdown() override; + bool Shutdown() override; - // nop by default - TBusServerSessionPtr CreateExtSession(TBusMessageQueue& queue) override; - }; + // nop by default + TBusServerSessionPtr CreateExtSession(TBusMessageQueue& queue) override; + }; - struct TExampleServerModule: public TExampleModule { - TNetAddr ServerAddr; - TBusServerSessionPtr CreateExtSession(TBusMessageQueue& queue) override; - }; + struct TExampleServerModule: public TExampleModule { + TNetAddr ServerAddr; + TBusServerSessionPtr CreateExtSession(TBusMessageQueue& queue) override; + }; - struct TExampleClientModule: public TExampleModule { - TBusClientSessionPtr Source; + struct TExampleClientModule: public TExampleModule { + TBusClientSessionPtr Source; - TExampleClientModule(); + TExampleClientModule(); - TBusServerSessionPtr CreateExtSession(TBusMessageQueue& queue) override; - }; + TBusServerSessionPtr CreateExtSession(TBusMessageQueue& queue) override; + }; - } -} + } +} diff --git a/library/cpp/messagebus/test/helper/fixed_port.cpp b/library/cpp/messagebus/test/helper/fixed_port.cpp index 540fa7c39d..258da0d1a5 100644 --- a/library/cpp/messagebus/test/helper/fixed_port.cpp +++ b/library/cpp/messagebus/test/helper/fixed_port.cpp @@ -4,7 +4,7 @@ #include <stdlib.h> -bool NBus::NTest::IsFixedPortTestAllowed() { +bool NBus::NTest::IsFixedPortTestAllowed() { // TODO: report skipped tests to test return !GetEnv("MB_TESTS_SKIP_FIXED_PORT"); } diff --git a/library/cpp/messagebus/test/helper/fixed_port.h b/library/cpp/messagebus/test/helper/fixed_port.h index e59d933b2f..a9c61ebc63 100644 --- a/library/cpp/messagebus/test/helper/fixed_port.h +++ b/library/cpp/messagebus/test/helper/fixed_port.h @@ -1,11 +1,11 @@ #pragma once -namespace NBus { - namespace NTest { - bool IsFixedPortTestAllowed(); +namespace NBus { + namespace NTest { + bool IsFixedPortTestAllowed(); - // Must not be in range OS uses for bind on random port. - const unsigned FixedPort = 4927; + // Must not be in range OS uses for bind on random port. + const unsigned FixedPort = 4927; - } -} + } +} diff --git a/library/cpp/messagebus/test/helper/hanging_server.cpp b/library/cpp/messagebus/test/helper/hanging_server.cpp index b9c2f0571d..a35514b00d 100644 --- a/library/cpp/messagebus/test/helper/hanging_server.cpp +++ b/library/cpp/messagebus/test/helper/hanging_server.cpp @@ -4,7 +4,7 @@ using namespace NBus; -THangingServer::THangingServer(int port) { +THangingServer::THangingServer(int port) { BindResult = BindOnPort(port, false); } diff --git a/library/cpp/messagebus/test/helper/hanging_server.h b/library/cpp/messagebus/test/helper/hanging_server.h index 2804b81f6f..cc9fb274d8 100644 --- a/library/cpp/messagebus/test/helper/hanging_server.h +++ b/library/cpp/messagebus/test/helper/hanging_server.h @@ -7,7 +7,7 @@ class THangingServer { private: std::pair<unsigned, TVector<NBus::TBindResult>> BindResult; - + public: // listen on given port, and nothing else THangingServer(int port = 0); diff --git a/library/cpp/messagebus/test/helper/message_handler_error.h b/library/cpp/messagebus/test/helper/message_handler_error.h index 13097b771d..a314b10761 100644 --- a/library/cpp/messagebus/test/helper/message_handler_error.h +++ b/library/cpp/messagebus/test/helper/message_handler_error.h @@ -2,18 +2,18 @@ #include <library/cpp/messagebus/ybus.h> -namespace NBus { - namespace NTest { - struct TBusClientHandlerError: public IBusClientHandler { - void OnError(TAutoPtr<TBusMessage> pMessage, EMessageStatus status) override; - void OnMessageSentOneWay(TAutoPtr<TBusMessage> pMessage) override; - void OnReply(TAutoPtr<TBusMessage> pMessage, TAutoPtr<TBusMessage> pReply) override; - }; +namespace NBus { + namespace NTest { + struct TBusClientHandlerError: public IBusClientHandler { + void OnError(TAutoPtr<TBusMessage> pMessage, EMessageStatus status) override; + void OnMessageSentOneWay(TAutoPtr<TBusMessage> pMessage) override; + void OnReply(TAutoPtr<TBusMessage> pMessage, TAutoPtr<TBusMessage> pReply) override; + }; - struct TBusServerHandlerError: public IBusServerHandler { - void OnError(TAutoPtr<TBusMessage> pMessage, EMessageStatus status) override; - void OnMessage(TOnMessageContext& pMessage) override; - }; + struct TBusServerHandlerError: public IBusServerHandler { + void OnError(TAutoPtr<TBusMessage> pMessage, EMessageStatus status) override; + void OnMessage(TOnMessageContext& pMessage) override; + }; - } -} + } +} diff --git a/library/cpp/messagebus/test/helper/object_count_check.h b/library/cpp/messagebus/test/helper/object_count_check.h index 7843fb0f30..1c4756e58c 100644 --- a/library/cpp/messagebus/test/helper/object_count_check.h +++ b/library/cpp/messagebus/test/helper/object_count_check.h @@ -1,7 +1,7 @@ #pragma once #include <library/cpp/testing/unittest/registar.h> - + #include <library/cpp/messagebus/remote_client_connection.h> #include <library/cpp/messagebus/remote_client_session.h> #include <library/cpp/messagebus/remote_server_connection.h> @@ -23,15 +23,15 @@ struct TObjectCountCheck { struct TReset { TObjectCountCheck* const Thiz; - TReset(TObjectCountCheck* thiz) - : Thiz(thiz) - { - } + TReset(TObjectCountCheck* thiz) + : Thiz(thiz) + { + } - void operator()() { + void operator()() { long oldValue = TObjectCounter<T>::ResetObjectCount(); if (oldValue != 0) { - Cerr << "warning: previous counter: " << oldValue << " for " << TypeName<T>() << Endl; + Cerr << "warning: previous counter: " << oldValue << " for " << TypeName<T>() << Endl; Cerr << "won't check in this test" << Endl; Thiz->Enabled = false; } @@ -45,10 +45,10 @@ struct TObjectCountCheck { template <typename T> struct TCheckZero { - TCheckZero(TObjectCountCheck*) { - } + TCheckZero(TObjectCountCheck*) { + } - void operator()() { + void operator()() { UNIT_ASSERT_VALUES_EQUAL_C(0L, TObjectCounter<T>::ObjectCount(), TypeName<T>()); } }; diff --git a/library/cpp/messagebus/test/helper/wait_for.h b/library/cpp/messagebus/test/helper/wait_for.h index 029ab0da48..f09958d4c0 100644 --- a/library/cpp/messagebus/test/helper/wait_for.h +++ b/library/cpp/messagebus/test/helper/wait_for.h @@ -3,12 +3,12 @@ #include <util/datetime/base.h> #include <util/system/yassert.h> -#define UNIT_WAIT_FOR(condition) \ - do { \ +#define UNIT_WAIT_FOR(condition) \ + do { \ TInstant start(TInstant::Now()); \ while (!(condition) && (TInstant::Now() - start < TDuration::Seconds(10))) { \ - Sleep(TDuration::MilliSeconds(1)); \ + Sleep(TDuration::MilliSeconds(1)); \ } \ /* TODO: use UNIT_ASSERT if in unittest thread */ \ - Y_VERIFY(condition, "condition failed after 10 seconds wait"); \ + Y_VERIFY(condition, "condition failed after 10 seconds wait"); \ } while (0) diff --git a/library/cpp/messagebus/test/perftest/perftest.cpp b/library/cpp/messagebus/test/perftest/perftest.cpp index 8ce4c175a2..8489319278 100644 --- a/library/cpp/messagebus/test/perftest/perftest.cpp +++ b/library/cpp/messagebus/test/perftest/perftest.cpp @@ -41,18 +41,18 @@ using namespace NBus; const int DEFAULT_PORT = 55666; struct TPerftestConfig { - TString Nodes; ///< node1:port1,node2:port2 - int ClientCount; - int MessageSize; ///< size of message to send - int Delay; ///< server delay (milliseconds) - float Failure; ///< simulated failure rate - int ServerPort; - int Run; - bool ServerUseModules; - bool ExecuteOnMessageInWorkerPool; - bool ExecuteOnReplyInWorkerPool; - bool UseCompression; - bool Profile; + TString Nodes; ///< node1:port1,node2:port2 + int ClientCount; + int MessageSize; ///< size of message to send + int Delay; ///< server delay (milliseconds) + float Failure; ///< simulated failure rate + int ServerPort; + int Run; + bool ServerUseModules; + bool ExecuteOnMessageInWorkerPool; + bool ExecuteOnReplyInWorkerPool; + bool UseCompression; + bool Profile; unsigned WwwPort; TPerftestConfig(); @@ -61,8 +61,8 @@ struct TPerftestConfig { fprintf(stderr, "ClientCount=%d\n", ClientCount); fprintf(stderr, "ServerPort=%d\n", ServerPort); fprintf(stderr, "Delay=%d usecs\n", Delay); - fprintf(stderr, "MessageSize=%d bytes\n", MessageSize); - fprintf(stderr, "Failure=%.3f%%\n", Failure * 100.0); + fprintf(stderr, "MessageSize=%d bytes\n", MessageSize); + fprintf(stderr, "Failure=%.3f%%\n", Failure * 100.0); fprintf(stderr, "Runtime=%d seconds\n", Run); fprintf(stderr, "ServerUseModules=%s\n", ServerUseModules ? "true" : "false"); fprintf(stderr, "ExecuteOnMessageInWorkerPool=%s\n", ExecuteOnMessageInWorkerPool ? "true" : "false"); @@ -73,7 +73,7 @@ struct TPerftestConfig { } }; -extern TPerftestConfig* TheConfig; +extern TPerftestConfig* TheConfig; extern bool TheExit; TVector<TNetAddr> ServerAddresses; @@ -191,26 +191,26 @@ struct TTestStats { TAtomic Errors; TAtomic Replies; - void IncMessage() { - AtomicIncrement(Messages); - } - void IncReplies() { - AtomicDecrement(Messages); - AtomicIncrement(Replies); - } - int NumMessage() { - return AtomicGet(Messages); - } - void IncErrors() { - AtomicDecrement(Messages); - AtomicIncrement(Errors); - } - int NumErrors() { - return AtomicGet(Errors); - } - int NumReplies() { - return AtomicGet(Replies); - } + void IncMessage() { + AtomicIncrement(Messages); + } + void IncReplies() { + AtomicDecrement(Messages); + AtomicIncrement(Replies); + } + int NumMessage() { + return AtomicGet(Messages); + } + void IncErrors() { + AtomicDecrement(Messages); + AtomicIncrement(Errors); + } + int NumErrors() { + return AtomicGet(Errors); + } + int NumReplies() { + return AtomicGet(Replies); + } double GetThroughput() { return NumReplies() * 1000000.0 / (TInstant::Now() - Start).MicroSeconds(); @@ -232,7 +232,7 @@ TTestStats Stats; //////////////////////////////////////////////////////////////////// /// \brief Fast of the client session -class TPerftestClient : IBusClientHandler { +class TPerftestClient : IBusClientHandler { public: TBusClientSessionPtr Session; THolder<TBusProtocol> Proto; @@ -270,7 +270,7 @@ public: connection = Connections.at(RandomNumber<size_t>()).Get(); } - TBusMessage* message = NewRequest().Release(); + TBusMessage* message = NewRequest().Release(); int ret = connection->SendMessage(message, true); if (ret == MESSAGE_OK) { @@ -386,7 +386,7 @@ public: CheckRequest(typed); /// forget replies for few messages, see what happends - if (TheConfig->Failure > RandomNumber<double>()) { + if (TheConfig->Failure > RandomNumber<double>()) { return; } @@ -420,7 +420,7 @@ public: Y_VERIFY(StartInput(), "failed to start input"); } - ~TPerftestUsingModule() override { + ~TPerftestUsingModule() override { Shutdown(); } @@ -435,7 +435,7 @@ private: } /// forget replies for few messages, see what happends - if (TheConfig->Failure > RandomNumber<double>()) { + if (TheConfig->Failure > RandomNumber<double>()) { return nullptr; } @@ -454,15 +454,15 @@ using namespace std; using namespace NBus; static TNetworkAddress ParseNetworkAddress(const char* string) { - TString Name; - int Port; + TString Name; + int Port; - const char* port = strchr(string, ':'); + const char* port = strchr(string, ':'); if (port != nullptr) { Name.append(string, port - string); Port = atoi(port + 1); - } else { + } else { Name.append(string); Port = TheConfig->ServerPort != 0 ? TheConfig->ServerPort : DEFAULT_PORT; } @@ -503,19 +503,19 @@ TPerftestConfig::TPerftestConfig() { WwwPort = 0; } -TPerftestConfig* TheConfig = new TPerftestConfig(); -bool TheExit = false; +TPerftestConfig* TheConfig = new TPerftestConfig(); +bool TheExit = false; TSystemEvent StopEvent; -TSimpleSharedPtr<TPerftestServer> Server; -TSimpleSharedPtr<TPerftestUsingModule> ServerUsingModule; +TSimpleSharedPtr<TPerftestServer> Server; +TSimpleSharedPtr<TPerftestUsingModule> ServerUsingModule; -TVector<TSimpleSharedPtr<TPerftestClient>> Clients; +TVector<TSimpleSharedPtr<TPerftestClient>> Clients; TMutex ClientsLock; void stopsignal(int /*sig*/) { - fprintf(stderr, "\n-------------------- exiting ------------------\n"); + fprintf(stderr, "\n-------------------- exiting ------------------\n"); TheExit = true; StopEvent.Signal(); } @@ -531,22 +531,22 @@ void TTestStats::PeriodicallyPrint() { if (TheExit) break; - TVector<TSimpleSharedPtr<TPerftestClient>> clients; + TVector<TSimpleSharedPtr<TPerftestClient>> clients; { TGuard<TMutex> guard(ClientsLock); clients = Clients; } fprintf(stderr, "replies=%d errors=%d throughput=%.3f mess/sec\n", - NumReplies(), NumErrors(), GetThroughput()); + NumReplies(), NumErrors(), GetThroughput()); if (!!Server) { fprintf(stderr, "server: q: %u %s\n", - (unsigned)Server->Bus->GetExecutor()->GetWorkQueueSize(), + (unsigned)Server->Bus->GetExecutor()->GetWorkQueueSize(), Server->Session->GetStatusSingleLine().data()); } if (!!ServerUsingModule) { fprintf(stderr, "server: q: %u %s\n", - (unsigned)ServerUsingModule->Bus->GetExecutor()->GetWorkQueueSize(), + (unsigned)ServerUsingModule->Bus->GetExecutor()->GetWorkQueueSize(), ServerUsingModule->Session->GetStatusSingleLine().data()); } for (const auto& client : clients) { @@ -587,19 +587,19 @@ void TTestStats::PeriodicallyPrint() { } } -int main(int argc, char* argv[]) { +int main(int argc, char* argv[]) { NLWTrace::StartLwtraceFromEnv(); - /* unix foo */ + /* unix foo */ setvbuf(stdout, nullptr, _IONBF, 0); setvbuf(stderr, nullptr, _IONBF, 0); Umask(0); - SetAsyncSignalHandler(SIGINT, stopsignal); + SetAsyncSignalHandler(SIGINT, stopsignal); SetAsyncSignalHandler(SIGTERM, stopsignal); #ifndef _win_ SetAsyncSignalHandler(SIGUSR1, stopsignal); #endif - signal(SIGPIPE, SIG_IGN); + signal(SIGPIPE, SIG_IGN); NLastGetopt::TOpts opts = NLastGetopt::TOpts::Default(); opts.AddLongOption('s', "server-port", "server port").RequiredArgument("port").StoreResult(&TheConfig->ServerPort); @@ -611,11 +611,11 @@ int main(int argc, char* argv[]) { opts.AddLongOption("client-count", "amount of clients").RequiredArgument("count").StoreResult(&TheConfig->ClientCount).DefaultValue("1"); opts.AddLongOption("server-use-modules").StoreResult(&TheConfig->ServerUseModules, true); opts.AddLongOption("on-message-in-pool", "execute OnMessage callback in worker pool") - .RequiredArgument("BOOL") - .StoreResult(&TheConfig->ExecuteOnMessageInWorkerPool); + .RequiredArgument("BOOL") + .StoreResult(&TheConfig->ExecuteOnMessageInWorkerPool); opts.AddLongOption("on-reply-in-pool", "execute OnReply callback in worker pool") - .RequiredArgument("BOOL") - .StoreResult(&TheConfig->ExecuteOnReplyInWorkerPool); + .RequiredArgument("BOOL") + .StoreResult(&TheConfig->ExecuteOnReplyInWorkerPool); opts.AddLongOption("compression", "use compression").RequiredArgument("BOOL").StoreResult(&TheConfig->UseCompression); opts.AddLongOption("simple-proto").SetFlag(&Config.SimpleProtocol); opts.AddLongOption("profile").SetFlag(&TheConfig->Profile); @@ -651,8 +651,8 @@ int main(int argc, char* argv[]) { www->RegisterServerSession(Server->Session); } } - - TVector<TSimpleSharedPtr<NThreading::TLegacyFuture<void, false>>> futures; + + TVector<TSimpleSharedPtr<NThreading::TLegacyFuture<void, false>>> futures; if (ServerAddresses.size() > 0 && TheConfig->ClientCount > 0) { for (int i = 0; i < TheConfig->ClientCount; ++i) { @@ -684,7 +684,7 @@ int main(int argc, char* argv[]) { ServerUsingModule->Stop(); } - TVector<TSimpleSharedPtr<TPerftestClient>> clients; + TVector<TSimpleSharedPtr<TPerftestClient>> clients; { TGuard<TMutex> guard(ClientsLock); clients = Clients; diff --git a/library/cpp/messagebus/test/perftest/simple_proto.cpp b/library/cpp/messagebus/test/perftest/simple_proto.cpp index a54d4b3493..19d6c15b9d 100644 --- a/library/cpp/messagebus/test/perftest/simple_proto.cpp +++ b/library/cpp/messagebus/test/perftest/simple_proto.cpp @@ -6,10 +6,10 @@ using namespace NBus; -void TSimpleProtocol::Serialize(const TBusMessage* mess, TBuffer& data) { +void TSimpleProtocol::Serialize(const TBusMessage* mess, TBuffer& data) { Y_VERIFY(typeid(TSimpleMessage) == typeid(*mess)); const TSimpleMessage* typed = static_cast<const TSimpleMessage*>(mess); - data.Append((const char*)&typed->Payload, 4); + data.Append((const char*)&typed->Payload, 4); } TAutoPtr<TBusMessage> TSimpleProtocol::Deserialize(ui16, TArrayRef<const char> payload) { diff --git a/library/cpp/messagebus/test/perftest/simple_proto.h b/library/cpp/messagebus/test/perftest/simple_proto.h index b61c4f4ae6..4a0cc08db3 100644 --- a/library/cpp/messagebus/test/perftest/simple_proto.h +++ b/library/cpp/messagebus/test/perftest/simple_proto.h @@ -2,28 +2,28 @@ #include <library/cpp/messagebus/ybus.h> -struct TSimpleMessage: public NBus::TBusMessage { +struct TSimpleMessage: public NBus::TBusMessage { ui32 Payload; TSimpleMessage() - : TBusMessage(1) - , Payload(0) - { - } + : TBusMessage(1) + , Payload(0) + { + } TSimpleMessage(NBus::ECreateUninitialized) : TBusMessage(NBus::ECreateUninitialized()) - { - } + { + } }; struct TSimpleProtocol: public NBus::TBusProtocol { - TSimpleProtocol() - : NBus::TBusProtocol("simple", 55666) - { - } + TSimpleProtocol() + : NBus::TBusProtocol("simple", 55666) + { + } - void Serialize(const NBus::TBusMessage* mess, TBuffer& data) override; + void Serialize(const NBus::TBusMessage* mess, TBuffer& data) override; TAutoPtr<NBus::TBusMessage> Deserialize(ui16 ty, TArrayRef<const char> payload) override; }; diff --git a/library/cpp/messagebus/test/ut/count_down_latch.h b/library/cpp/messagebus/test/ut/count_down_latch.h index a4d6b72bfa..5117db5731 100644 --- a/library/cpp/messagebus/test/ut/count_down_latch.h +++ b/library/cpp/messagebus/test/ut/count_down_latch.h @@ -7,12 +7,12 @@ class TCountDownLatch { private: TAtomic Current; TSystemEvent EventObject; - + public: TCountDownLatch(unsigned initial) : Current(initial) - { - } + { + } void CountDown() { if (AtomicDecrement(Current) == 0) { diff --git a/library/cpp/messagebus/test/ut/locator_uniq_ut.cpp b/library/cpp/messagebus/test/ut/locator_uniq_ut.cpp index dd5dfc4cca..3fdd175d73 100644 --- a/library/cpp/messagebus/test/ut/locator_uniq_ut.cpp +++ b/library/cpp/messagebus/test/ut/locator_uniq_ut.cpp @@ -4,12 +4,12 @@ #include <library/cpp/messagebus/ybus.h> class TLocatorRegisterUniqTest: public TTestBase { - UNIT_TEST_SUITE(TLocatorRegisterUniqTest); - UNIT_TEST(TestRegister); - UNIT_TEST_SUITE_END(); + UNIT_TEST_SUITE(TLocatorRegisterUniqTest); + UNIT_TEST(TestRegister); + UNIT_TEST_SUITE_END(); -protected: - void TestRegister(); +protected: + void TestRegister(); }; UNIT_TEST_SUITE_REGISTRATION(TLocatorRegisterUniqTest); diff --git a/library/cpp/messagebus/test/ut/messagebus_ut.cpp b/library/cpp/messagebus/test/ut/messagebus_ut.cpp index 92839e9cf9..040f9b7702 100644 --- a/library/cpp/messagebus/test/ut/messagebus_ut.cpp +++ b/library/cpp/messagebus/test/ut/messagebus_ut.cpp @@ -10,8 +10,8 @@ #include <util/network/sock.h> -#include <utility> - +#include <utility> + using namespace NBus; using namespace NBus::NTest; @@ -23,10 +23,10 @@ namespace { TExampleClientSlowOnMessageSent() : SentCompleted(0) - { - } + { + } - ~TExampleClientSlowOnMessageSent() override { + ~TExampleClientSlowOnMessageSent() override { Session->Shutdown(); } @@ -48,7 +48,7 @@ namespace { Y_UNIT_TEST_SUITE(TMessageBusTests) { void TestDestinationTemplate(bool useCompression, bool ackMessageBeforeReply, - const TBusServerSessionConfig& sessionConfig) { + const TBusServerSessionConfig& sessionConfig) { TObjectCountCheck objectCountCheck; TExampleServer server; @@ -121,17 +121,17 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { client.SendMessagesWaitReplies(19, serverAddr); } - struct TestNoServerImplClient: public TExampleClient { + struct TestNoServerImplClient: public TExampleClient { TTestSync TestSync; int failures = 0; - template <typename... Args> - TestNoServerImplClient(Args&&... args) - : TExampleClient(std::forward<Args>(args)...) - { - } + template <typename... Args> + TestNoServerImplClient(Args&&... args) + : TExampleClient(std::forward<Args>(args)...) + { + } - ~TestNoServerImplClient() override { + ~TestNoServerImplClient() override { Session->Shutdown(); } @@ -155,7 +155,7 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { if (oneWay) { status = client.Session->SendMessageOneWay(new TExampleRequest(&client.Proto.RequestCount), &noServerAddr); } else { - TAutoPtr<TBusMessage> message(new TExampleRequest(&client.Proto.RequestCount)); + TAutoPtr<TBusMessage> message(new TExampleRequest(&client.Proto.RequestCount)); status = client.Session->SendMessageAutoPtr(message, &noServerAddr); } @@ -168,7 +168,7 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { client.TestSync.WaitForAndIncrement(count * 2 + 1); } - client.TestSync.WaitForAndIncrement(count * 2); + client.TestSync.WaitForAndIncrement(count * 2); } void HangingServerImpl(unsigned port) { @@ -241,7 +241,7 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { client.WaitReplies(); } - struct TSendTimeoutCheckerExampleClient: public TExampleClient { + struct TSendTimeoutCheckerExampleClient: public TExampleClient { static TBusClientSessionConfig SessionConfig(bool periodLessThanConnectTimeout) { TBusClientSessionConfig sessionConfig; if (periodLessThanConnectTimeout) { @@ -256,8 +256,8 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { TSendTimeoutCheckerExampleClient(bool periodLessThanConnectTimeout) : TExampleClient(SessionConfig(periodLessThanConnectTimeout)) - { - } + { + } ~TSendTimeoutCheckerExampleClient() override { Session->Shutdown(); @@ -470,7 +470,7 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { client.WaitForError(MESSAGE_MESSAGE_TOO_LARGE); } - struct TServerForResponseTooLarge: public TExampleServer { + struct TServerForResponseTooLarge: public TExampleServer { TTestSync TestSync; static TBusServerSessionConfig Config() { @@ -481,10 +481,10 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { TServerForResponseTooLarge() : TExampleServer("TServerForResponseTooLarge", Config()) - { - } + { + } - ~TServerForResponseTooLarge() override { + ~TServerForResponseTooLarge() override { Session->Shutdown(); } @@ -530,7 +530,7 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { UNIT_ASSERT_VALUES_EQUAL(1, client.Session->GetInFlight()); } - struct TServerForRequestTooLarge: public TExampleServer { + struct TServerForRequestTooLarge: public TExampleServer { TTestSync TestSync; static TBusServerSessionConfig Config() { @@ -541,10 +541,10 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { TServerForRequestTooLarge() : TExampleServer("TServerForRequestTooLarge", Config()) - { - } + { + } - ~TServerForRequestTooLarge() override { + ~TServerForRequestTooLarge() override { Session->Shutdown(); } @@ -674,7 +674,7 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { client.WaitReplies(); } - struct TResetAfterSendOneWayErrorInCallbackClient: public TExampleClient { + struct TResetAfterSendOneWayErrorInCallbackClient: public TExampleClient { TTestSync TestSync; static TBusClientSessionConfig SessionConfig() { @@ -691,7 +691,7 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { { } - ~TResetAfterSendOneWayErrorInCallbackClient() override { + ~TResetAfterSendOneWayErrorInCallbackClient() override { Session->Shutdown(); } @@ -716,10 +716,10 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { client.TestSync.WaitForAndIncrement(2); } - struct TResetAfterSendMessageOneWayDuringShutdown: public TExampleClient { + struct TResetAfterSendMessageOneWayDuringShutdown: public TExampleClient { TTestSync TestSync; - ~TResetAfterSendMessageOneWayDuringShutdown() override { + ~TResetAfterSendMessageOneWayDuringShutdown() override { Session->Shutdown(); } @@ -770,10 +770,10 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { TestNoServerImpl(17, true); } - struct TResetAfterSendOneWaySuccessClient: public TExampleClient { + struct TResetAfterSendOneWaySuccessClient: public TExampleClient { TTestSync TestSync; - ~TResetAfterSendOneWaySuccessClient() override { + ~TResetAfterSendOneWaySuccessClient() override { Session->Shutdown(); } @@ -835,7 +835,7 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { TExampleProtocol proto; TBusServerHandlerError handler; TBusServerSessionPtr session = TBusServerSession::Create( - &proto, &handler, TBusServerSessionConfig(), queue); + &proto, &handler, TBusServerSessionConfig(), queue); unsigned port = session->GetActualListenPort(); UNIT_ASSERT(port > 0); @@ -873,7 +873,7 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { size_t pos = 0; while (pos < sizeof(response)) { - size_t count = input.Read(((char*)&response) + pos, sizeof(response) - pos); + size_t count = input.Read(((char*)&response) + pos, sizeof(response) - pos); pos += count; } @@ -882,10 +882,10 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { UNIT_ASSERT_VALUES_EQUAL(YBUS_VERSION, response.GetVersionInternal()); } - struct TOnConnectionEventClient: public TExampleClient { + struct TOnConnectionEventClient: public TExampleClient { TTestSync Sync; - ~TOnConnectionEventClient() override { + ~TOnConnectionEventClient() override { Session->Shutdown(); } @@ -913,13 +913,13 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { } }; - struct TOnConnectionEventServer: public TExampleServer { + struct TOnConnectionEventServer: public TExampleServer { TOnConnectionEventServer() - : TExampleServer("TOnConnectionEventServer") - { - } + : TExampleServer("TOnConnectionEventServer") + { + } - ~TOnConnectionEventServer() override { + ~TOnConnectionEventServer() override { Session->Shutdown(); } @@ -963,9 +963,9 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { client.Sync.WaitForAndIncrement(3); } - struct TServerForQuotaWake: public TExampleServer { + struct TServerForQuotaWake: public TExampleServer { TSystemEvent GoOn; - TMutex OneLock; + TMutex OneLock; TOnMessageContext OneMessage; @@ -981,16 +981,16 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { TServerForQuotaWake() : TExampleServer("TServerForQuotaWake", Config()) - { - } + { + } - ~TServerForQuotaWake() override { + ~TServerForQuotaWake() override { Session->Shutdown(); } void OnMessage(TOnMessageContext& req) override { if (!GoOn.Wait(0)) { - TGuard<TMutex> guard(OneLock); + TGuard<TMutex> guard(OneLock); UNIT_ASSERT(!OneMessage); @@ -1000,7 +1000,7 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { } void WakeOne() { - TGuard<TMutex> guard(OneLock); + TGuard<TMutex> guard(OneLock); UNIT_ASSERT(!!OneMessage); @@ -1035,13 +1035,13 @@ Y_UNIT_TEST_SUITE(TMessageBusTests) { count++; } else if (status == MESSAGE_BUSY) { - if (count == test_msg_count) { + if (count == test_msg_count) { TInstant now = TInstant::Now(); - if (start.GetValue() == 0) { + if (start.GetValue() == 0) { start = now; - // TODO: properly check that server is blocked + // TODO: properly check that server is blocked } else if (start + TDuration::MilliSeconds(100) < now) { break; } diff --git a/library/cpp/messagebus/test/ut/module_client_one_way_ut.cpp b/library/cpp/messagebus/test/ut/module_client_one_way_ut.cpp index fd511e2dd9..4083cf3b7b 100644 --- a/library/cpp/messagebus/test/ut/module_client_one_way_ut.cpp +++ b/library/cpp/messagebus/test/ut/module_client_one_way_ut.cpp @@ -43,8 +43,8 @@ Y_UNIT_TEST_SUITE(ModuleClientOneWay) { : TBusModule("m") , TestSync(testSync) , Port(port) - { - } + { + } TJobHandler Start(TBusJob* job, TBusMessage*) override { TestSync->WaitForAndIncrement(0); @@ -94,8 +94,8 @@ Y_UNIT_TEST_SUITE(ModuleClientOneWay) { TSendErrorModule(TTestSync* testSync) : TBusModule("m") , TestSync(testSync) - { - } + { + } TJobHandler Start(TBusJob* job, TBusMessage*) override { TestSync->WaitForAndIncrement(0); diff --git a/library/cpp/messagebus/test/ut/module_client_ut.cpp b/library/cpp/messagebus/test/ut/module_client_ut.cpp index 84897ce5c4..ebfe185cc6 100644 --- a/library/cpp/messagebus/test/ut/module_client_ut.cpp +++ b/library/cpp/messagebus/test/ut/module_client_ut.cpp @@ -20,34 +20,34 @@ using namespace NBus::NTest; // helper class that cleans TBusJob instance, so job's destructor can // be completed without assertion fail. struct TJobGuard { -public: - TJobGuard(NBus::TBusJob* job) - : Job(job) - { - } - - ~TJobGuard() { - Job->ClearAllMessageStates(); - } - -private: - NBus::TBusJob* Job; +public: + TJobGuard(NBus::TBusJob* job) + : Job(job) + { + } + + ~TJobGuard() { + Job->ClearAllMessageStates(); + } + +private: + NBus::TBusJob* Job; }; -class TMessageOk: public NBus::TBusMessage { -public: - TMessageOk() - : NBus::TBusMessage(1) - { - } +class TMessageOk: public NBus::TBusMessage { +public: + TMessageOk() + : NBus::TBusMessage(1) + { + } }; -class TMessageError: public NBus::TBusMessage { -public: - TMessageError() - : NBus::TBusMessage(2) - { - } +class TMessageError: public NBus::TBusMessage { +public: + TMessageError() + : NBus::TBusMessage(2) + { + } }; Y_UNIT_TEST_SUITE(BusJobTest) { @@ -108,8 +108,8 @@ Y_UNIT_TEST_SUITE(BusJobTest) { TParallelOnReplyModule(const TNetAddr& serverAddr) : ServerAddr(serverAddr) , RepliesLatch(2) - { - } + { + } TJobHandler Start(TBusJob* job, TBusMessage* mess) override { Y_UNUSED(mess); @@ -166,8 +166,8 @@ Y_UNIT_TEST_SUITE(BusJobTest) { : ServerAddr("localhost", 17) , GotReplyLatch(2) , SentMessage() - { - } + { + } TJobHandler Start(TBusJob* job, TBusMessage* mess) override { Y_UNUSED(mess); @@ -222,7 +222,7 @@ Y_UNIT_TEST_SUITE(BusJobTest) { module.Shutdown(); } - struct TSlowReplyServer: public TBusServerHandlerError { + struct TSlowReplyServer: public TBusServerHandlerError { TTestSync* const TestSync; TBusMessageQueuePtr Bus; TBusServerSessionPtr ServerSession; @@ -248,7 +248,7 @@ Y_UNIT_TEST_SUITE(BusJobTest) { } }; - struct TModuleThatSendsReplyEarly: public TExampleClientModule { + struct TModuleThatSendsReplyEarly: public TExampleClientModule { TTestSync* const TestSync; const unsigned ServerPort; @@ -260,8 +260,8 @@ Y_UNIT_TEST_SUITE(BusJobTest) { , ServerPort(serverPort) , ServerSession(nullptr) , ReplyCount(0) - { - } + { + } TJobHandler Start(TBusJob* job, TBusMessage* mess) override { Y_UNUSED(mess); @@ -318,22 +318,22 @@ Y_UNIT_TEST_SUITE(BusJobTest) { module.Shutdown(); } - struct TShutdownCalledBeforeReplyReceivedModule: public TExampleClientModule { + struct TShutdownCalledBeforeReplyReceivedModule: public TExampleClientModule { unsigned ServerPort; TTestSync TestSync; TShutdownCalledBeforeReplyReceivedModule(unsigned serverPort) : ServerPort(serverPort) - { - } + { + } TJobHandler Start(TBusJob* job, TBusMessage*) override { TestSync.CheckAndIncrement(0); job->Send(new TExampleRequest(&Proto.RequestCount), Source, - TReplyHandler(&TShutdownCalledBeforeReplyReceivedModule::HandleReply), - 0, TNetAddr("localhost", ServerPort)); + TReplyHandler(&TShutdownCalledBeforeReplyReceivedModule::HandleReply), + 0, TNetAddr("localhost", ServerPort)); return &TShutdownCalledBeforeReplyReceivedModule::End; } diff --git a/library/cpp/messagebus/test/ut/module_server_ut.cpp b/library/cpp/messagebus/test/ut/module_server_ut.cpp index 4258ae4bf7..88fe1dd9b6 100644 --- a/library/cpp/messagebus/test/ut/module_server_ut.cpp +++ b/library/cpp/messagebus/test/ut/module_server_ut.cpp @@ -21,7 +21,7 @@ Y_UNIT_TEST_SUITE(ModuleServerTests) { /// create or get instance of message queue, need one per application TBusMessageQueuePtr bus(CreateMessageQueue()); - THostInfoHandler hostHandler(bus.Get()); + THostInfoHandler hostHandler(bus.Get()); TDupDetectModule module(hostHandler.GetActualListenAddr()); bool success; success = module.Init(bus.Get()); @@ -39,13 +39,13 @@ Y_UNIT_TEST_SUITE(ModuleServerTests) { dupHandler.DupDetect->Shutdown(); } - struct TParallelOnMessageModule: public TExampleServerModule { + struct TParallelOnMessageModule: public TExampleServerModule { TCountDownLatch WaitTwoRequestsLatch; TParallelOnMessageModule() : WaitTwoRequestsLatch(2) - { - } + { + } TJobHandler Start(TBusJob* job, TBusMessage* mess) override { WaitTwoRequestsLatch.CountDown(); diff --git a/library/cpp/messagebus/test/ut/moduletest.h b/library/cpp/messagebus/test/ut/moduletest.h index e67da02701..d5da72c0cb 100644 --- a/library/cpp/messagebus/test/ut/moduletest.h +++ b/library/cpp/messagebus/test/ut/moduletest.h @@ -11,211 +11,211 @@ #include <library/cpp/messagebus/ybus.h> #include <library/cpp/messagebus/oldmodule/module.h> -namespace NBus { - namespace NTest { - using namespace std; +namespace NBus { + namespace NTest { + using namespace std; -#define TYPE_HOSTINFOREQUEST 100 +#define TYPE_HOSTINFOREQUEST 100 #define TYPE_HOSTINFORESPONSE 101 - //////////////////////////////////////////////////////////////////// - /// \brief DupDetect protocol that common between client and server - //////////////////////////////////////////////////////////////////// - /// \brief HostInfo request class - class THostInfoMessage: public TBusMessage { - public: - THostInfoMessage() - : TBusMessage(TYPE_HOSTINFOREQUEST) - { - } - THostInfoMessage(ECreateUninitialized) - : TBusMessage(MESSAGE_CREATE_UNINITIALIZED) - { - } - - ~THostInfoMessage() override { - } - }; - - //////////////////////////////////////////////////////////////////// - /// \brief HostInfo reply class - class THostInfoReply: public TBusMessage { - public: - THostInfoReply() - : TBusMessage(TYPE_HOSTINFORESPONSE) - { - } - THostInfoReply(ECreateUninitialized) - : TBusMessage(MESSAGE_CREATE_UNINITIALIZED) - { - } - - ~THostInfoReply() override { - } - }; - - //////////////////////////////////////////////////////////////////// - /// \brief HostInfo protocol that common between client and server - class THostInfoProtocol: public TBusProtocol { - public: - THostInfoProtocol() - : TBusProtocol("HOSTINFO", 0) - { - } - /// serialized protocol specific data into TBusData - void Serialize(const TBusMessage* mess, TBuffer& data) override { - Y_UNUSED(data); - Y_UNUSED(mess); - } - - /// deserialized TBusData into new instance of the message - TAutoPtr<TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) override { - Y_UNUSED(payload); - - if (messageType == TYPE_HOSTINFOREQUEST) { - return new THostInfoMessage(MESSAGE_CREATE_UNINITIALIZED); - } else if (messageType == TYPE_HOSTINFORESPONSE) { - return new THostInfoReply(MESSAGE_CREATE_UNINITIALIZED); - } else { - Y_FAIL("unknown"); - } - } - }; - - ////////////////////////////////////////////////////////////// - /// \brief HostInfo handler (should convert it to module too) - struct THostInfoHandler: public TBusServerHandlerError { - TBusServerSessionPtr Session; - TBusServerSessionConfig HostInfoConfig; - THostInfoProtocol HostInfoProto; - - THostInfoHandler(TBusMessageQueue* queue) { - Session = TBusServerSession::Create(&HostInfoProto, this, HostInfoConfig, queue); - } - - void OnMessage(TOnMessageContext& mess) override { - usleep(10 * 1000); /// pretend we are doing something - - TAutoPtr<THostInfoReply> reply(new THostInfoReply()); - - mess.SendReplyMove(reply); - } - - TNetAddr GetActualListenAddr() { - return TNetAddr("localhost", Session->GetActualListenPort()); - } - }; - - ////////////////////////////////////////////////////////////// - /// \brief DupDetect handler (should convert it to module too) - struct TDupDetectHandler: public TBusClientHandlerError { - TNetAddr ServerAddr; - - TBusClientSessionPtr DupDetect; - TBusClientSessionConfig DupDetectConfig; - TExampleProtocol DupDetectProto; - - int NumMessages; - int NumReplies; - - TDupDetectHandler(const TNetAddr& serverAddr, TBusMessageQueuePtr queue) - : ServerAddr(serverAddr) - { - DupDetect = TBusClientSession::Create(&DupDetectProto, this, DupDetectConfig, queue); - DupDetect->RegisterService("localhost"); - } - - void Work() { - NumMessages = 10; - NumReplies = 0; - - for (int i = 0; i < NumMessages; i++) { - TExampleRequest* mess = new TExampleRequest(&DupDetectProto.RequestCount); - DupDetect->SendMessage(mess, &ServerAddr); - } - } - - void OnReply(TAutoPtr<TBusMessage> mess, TAutoPtr<TBusMessage> reply) override { - Y_UNUSED(mess); - Y_UNUSED(reply); - NumReplies++; - } - }; - - ///////////////////////////////////////////////////////////////// - /// \brief DupDetect module - - struct TDupDetectModule: public TBusModule { - TNetAddr HostInfoAddr; - - TBusClientSessionPtr HostInfoClientSession; - TBusClientSessionConfig HostInfoConfig; - THostInfoProtocol HostInfoProto; - - TExampleProtocol DupDetectProto; - TBusServerSessionConfig DupDetectConfig; - - TNetAddr ListenAddr; - - TDupDetectModule(const TNetAddr& hostInfoAddr) - : TBusModule("DUPDETECTMODULE") - , HostInfoAddr(hostInfoAddr) - { - } - - bool Init(TBusMessageQueue* queue) { - HostInfoClientSession = CreateDefaultSource(*queue, &HostInfoProto, HostInfoConfig); - HostInfoClientSession->RegisterService("localhost"); - - return TBusModule::CreatePrivateSessions(queue); - } - - TBusServerSessionPtr CreateExtSession(TBusMessageQueue& queue) override { - TBusServerSessionPtr session = CreateDefaultDestination(queue, &DupDetectProto, DupDetectConfig); - - ListenAddr = TNetAddr("localhost", session->GetActualListenPort()); - - return session; - } - - /// entry point into module, first function to call - TJobHandler Start(TBusJob* job, TBusMessage* mess) override { - TExampleRequest* dmess = dynamic_cast<TExampleRequest*>(mess); - Y_UNUSED(dmess); - - THostInfoMessage* hmess = new THostInfoMessage(); - - /// send message to imaginary hostinfo server - job->Send(hmess, HostInfoClientSession, TReplyHandler(), 0, HostInfoAddr); - - return TJobHandler(&TDupDetectModule::ProcessHostInfo); - } - - /// next handler is executed when all outstanding requests from previous handler is completed - TJobHandler ProcessHostInfo(TBusJob* job, TBusMessage* mess) { - TExampleRequest* dmess = dynamic_cast<TExampleRequest*>(mess); - Y_UNUSED(dmess); - - THostInfoMessage* hmess = job->Get<THostInfoMessage>(); - THostInfoReply* hreply = job->Get<THostInfoReply>(); - EMessageStatus hstatus = job->GetStatus<THostInfoMessage>(); - Y_ASSERT(hmess != nullptr); - Y_ASSERT(hreply != nullptr); - Y_ASSERT(hstatus == MESSAGE_OK); - - return TJobHandler(&TDupDetectModule::Finish); - } - - /// last handler sends reply and returns NULL - TJobHandler Finish(TBusJob* job, TBusMessage* mess) { - Y_UNUSED(mess); - - TExampleResponse* reply = new TExampleResponse(&DupDetectProto.ResponseCount); - job->SendReply(reply); - - return nullptr; - } - }; + //////////////////////////////////////////////////////////////////// + /// \brief DupDetect protocol that common between client and server + //////////////////////////////////////////////////////////////////// + /// \brief HostInfo request class + class THostInfoMessage: public TBusMessage { + public: + THostInfoMessage() + : TBusMessage(TYPE_HOSTINFOREQUEST) + { + } + THostInfoMessage(ECreateUninitialized) + : TBusMessage(MESSAGE_CREATE_UNINITIALIZED) + { + } + + ~THostInfoMessage() override { + } + }; + + //////////////////////////////////////////////////////////////////// + /// \brief HostInfo reply class + class THostInfoReply: public TBusMessage { + public: + THostInfoReply() + : TBusMessage(TYPE_HOSTINFORESPONSE) + { + } + THostInfoReply(ECreateUninitialized) + : TBusMessage(MESSAGE_CREATE_UNINITIALIZED) + { + } + + ~THostInfoReply() override { + } + }; + + //////////////////////////////////////////////////////////////////// + /// \brief HostInfo protocol that common between client and server + class THostInfoProtocol: public TBusProtocol { + public: + THostInfoProtocol() + : TBusProtocol("HOSTINFO", 0) + { + } + /// serialized protocol specific data into TBusData + void Serialize(const TBusMessage* mess, TBuffer& data) override { + Y_UNUSED(data); + Y_UNUSED(mess); + } + + /// deserialized TBusData into new instance of the message + TAutoPtr<TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) override { + Y_UNUSED(payload); + + if (messageType == TYPE_HOSTINFOREQUEST) { + return new THostInfoMessage(MESSAGE_CREATE_UNINITIALIZED); + } else if (messageType == TYPE_HOSTINFORESPONSE) { + return new THostInfoReply(MESSAGE_CREATE_UNINITIALIZED); + } else { + Y_FAIL("unknown"); + } + } + }; + + ////////////////////////////////////////////////////////////// + /// \brief HostInfo handler (should convert it to module too) + struct THostInfoHandler: public TBusServerHandlerError { + TBusServerSessionPtr Session; + TBusServerSessionConfig HostInfoConfig; + THostInfoProtocol HostInfoProto; + + THostInfoHandler(TBusMessageQueue* queue) { + Session = TBusServerSession::Create(&HostInfoProto, this, HostInfoConfig, queue); + } + + void OnMessage(TOnMessageContext& mess) override { + usleep(10 * 1000); /// pretend we are doing something + + TAutoPtr<THostInfoReply> reply(new THostInfoReply()); + + mess.SendReplyMove(reply); + } + + TNetAddr GetActualListenAddr() { + return TNetAddr("localhost", Session->GetActualListenPort()); + } + }; + + ////////////////////////////////////////////////////////////// + /// \brief DupDetect handler (should convert it to module too) + struct TDupDetectHandler: public TBusClientHandlerError { + TNetAddr ServerAddr; + + TBusClientSessionPtr DupDetect; + TBusClientSessionConfig DupDetectConfig; + TExampleProtocol DupDetectProto; + + int NumMessages; + int NumReplies; + + TDupDetectHandler(const TNetAddr& serverAddr, TBusMessageQueuePtr queue) + : ServerAddr(serverAddr) + { + DupDetect = TBusClientSession::Create(&DupDetectProto, this, DupDetectConfig, queue); + DupDetect->RegisterService("localhost"); + } + + void Work() { + NumMessages = 10; + NumReplies = 0; + + for (int i = 0; i < NumMessages; i++) { + TExampleRequest* mess = new TExampleRequest(&DupDetectProto.RequestCount); + DupDetect->SendMessage(mess, &ServerAddr); + } + } + + void OnReply(TAutoPtr<TBusMessage> mess, TAutoPtr<TBusMessage> reply) override { + Y_UNUSED(mess); + Y_UNUSED(reply); + NumReplies++; + } + }; + + ///////////////////////////////////////////////////////////////// + /// \brief DupDetect module + + struct TDupDetectModule: public TBusModule { + TNetAddr HostInfoAddr; + + TBusClientSessionPtr HostInfoClientSession; + TBusClientSessionConfig HostInfoConfig; + THostInfoProtocol HostInfoProto; + + TExampleProtocol DupDetectProto; + TBusServerSessionConfig DupDetectConfig; + + TNetAddr ListenAddr; + + TDupDetectModule(const TNetAddr& hostInfoAddr) + : TBusModule("DUPDETECTMODULE") + , HostInfoAddr(hostInfoAddr) + { + } + + bool Init(TBusMessageQueue* queue) { + HostInfoClientSession = CreateDefaultSource(*queue, &HostInfoProto, HostInfoConfig); + HostInfoClientSession->RegisterService("localhost"); + + return TBusModule::CreatePrivateSessions(queue); + } + + TBusServerSessionPtr CreateExtSession(TBusMessageQueue& queue) override { + TBusServerSessionPtr session = CreateDefaultDestination(queue, &DupDetectProto, DupDetectConfig); + + ListenAddr = TNetAddr("localhost", session->GetActualListenPort()); + + return session; + } + + /// entry point into module, first function to call + TJobHandler Start(TBusJob* job, TBusMessage* mess) override { + TExampleRequest* dmess = dynamic_cast<TExampleRequest*>(mess); + Y_UNUSED(dmess); + + THostInfoMessage* hmess = new THostInfoMessage(); + + /// send message to imaginary hostinfo server + job->Send(hmess, HostInfoClientSession, TReplyHandler(), 0, HostInfoAddr); + + return TJobHandler(&TDupDetectModule::ProcessHostInfo); + } + + /// next handler is executed when all outstanding requests from previous handler is completed + TJobHandler ProcessHostInfo(TBusJob* job, TBusMessage* mess) { + TExampleRequest* dmess = dynamic_cast<TExampleRequest*>(mess); + Y_UNUSED(dmess); + + THostInfoMessage* hmess = job->Get<THostInfoMessage>(); + THostInfoReply* hreply = job->Get<THostInfoReply>(); + EMessageStatus hstatus = job->GetStatus<THostInfoMessage>(); + Y_ASSERT(hmess != nullptr); + Y_ASSERT(hreply != nullptr); + Y_ASSERT(hstatus == MESSAGE_OK); + + return TJobHandler(&TDupDetectModule::Finish); + } + + /// last handler sends reply and returns NULL + TJobHandler Finish(TBusJob* job, TBusMessage* mess) { + Y_UNUSED(mess); + + TExampleResponse* reply = new TExampleResponse(&DupDetectProto.ResponseCount); + job->SendReply(reply); + + return nullptr; + } + }; } -} +} diff --git a/library/cpp/messagebus/test/ut/one_way_ut.cpp b/library/cpp/messagebus/test/ut/one_way_ut.cpp index a8e0cb960b..9c21227e2b 100644 --- a/library/cpp/messagebus/test/ut/one_way_ut.cpp +++ b/library/cpp/messagebus/test/ut/one_way_ut.cpp @@ -1,7 +1,7 @@ /////////////////////////////////////////////////////////////////// /// \file -/// \brief Example of reply-less communication - +/// \brief Example of reply-less communication + /// This example demostrates how asynchronous message passing library /// can be used to send message and do not wait for reply back. /// The usage of reply-less communication should be restricted to @@ -9,19 +9,19 @@ /// utility. Removing replies from the communication removes any restriction /// on how many message can be send to server and rougue clients may overwelm /// server without thoughtput control. - + /// 1) To implement reply-less client \n - -/// Call NBus::TBusSession::AckMessage() -/// from within NBus::IMessageHandler::OnSent() handler when message has -/// gone into wire on client end. See example in NBus::NullClient::OnMessageSent(). + +/// Call NBus::TBusSession::AckMessage() +/// from within NBus::IMessageHandler::OnSent() handler when message has +/// gone into wire on client end. See example in NBus::NullClient::OnMessageSent(). /// Discard identity for reply message. - + /// 2) To implement reply-less server \n - + /// Call NBus::TBusSession::AckMessage() from within NBus::IMessageHandler::OnMessage() -/// handler when message has been received on server end. -/// See example in NBus::NullServer::OnMessage(). +/// handler when message has been received on server end. +/// See example in NBus::NullServer::OnMessage(). /// Discard identity for reply message. #include <library/cpp/messagebus/test/helper/alloc_counter.h> @@ -41,7 +41,7 @@ using namespace NBus::NTest; //////////////////////////////////////////////////////////////////// //////////////////////////////////////////////////////////////////// /// \brief Reply-less client and handler -struct NullClient : TBusClientHandlerError { +struct NullClient : TBusClientHandlerError { TNetAddr ServerAddr; TBusMessageQueuePtr Queue; @@ -69,11 +69,11 @@ struct NullClient : TBusClientHandlerError { } /// dispatch of requests is done here - void Work() { + void Work() { int batch = 10; - for (int i = 0; i < batch; i++) { - TExampleRequest* mess = new TExampleRequest(&Proto.RequestCount); + for (int i = 0; i < batch; i++) { + TExampleRequest* mess = new TExampleRequest(&Proto.RequestCount); mess->Data = "TADA"; Session->SendMessageOneWay(mess, &ServerAddr); } @@ -112,7 +112,7 @@ public: /// when message comes do not send reply, just acknowledge void OnMessage(TOnMessageContext& mess) override { - TExampleRequest* fmess = static_cast<TExampleRequest*>(mess.GetMessage()); + TExampleRequest* fmess = static_cast<TExampleRequest*>(mess.GetMessage()); Y_ASSERT(fmess->Data == "TADA"); @@ -126,7 +126,7 @@ public: void OnSent(TAutoPtr<TBusMessage> mess) override { Y_UNUSED(mess); Y_FAIL("This server does not sent replies"); - } + } }; Y_UNIT_TEST_SUITE(TMessageBusTests_OneWay) { @@ -158,8 +158,8 @@ Y_UNIT_TEST_SUITE(TMessageBusTests_OneWay) { TMessageTooLargeClient(unsigned port) : NullClient(TNetAddr("localhost", port), Config()) - { - } + { + } ~TMessageTooLargeClient() override { Session->Shutdown(); @@ -187,7 +187,7 @@ Y_UNIT_TEST_SUITE(TMessageBusTests_OneWay) { client.GotTooLarge.WaitI(); } - struct TCheckTimeoutClient: public NullClient { + struct TCheckTimeoutClient: public NullClient { ~TCheckTimeoutClient() override { Session->Shutdown(); } @@ -200,10 +200,10 @@ Y_UNIT_TEST_SUITE(TMessageBusTests_OneWay) { return sessionConfig; } - TCheckTimeoutClient(const TNetAddr& serverAddr) - : NullClient(serverAddr, SessionConfig()) - { - } + TCheckTimeoutClient(const TNetAddr& serverAddr) + : NullClient(serverAddr, SessionConfig()) + { + } TSystemEvent GotError; diff --git a/library/cpp/messagebus/test/ut/starter_ut.cpp b/library/cpp/messagebus/test/ut/starter_ut.cpp index b9ff9a449d..dd4d3aaa5e 100644 --- a/library/cpp/messagebus/test/ut/starter_ut.cpp +++ b/library/cpp/messagebus/test/ut/starter_ut.cpp @@ -8,7 +8,7 @@ using namespace NBus; using namespace NBus::NTest; Y_UNIT_TEST_SUITE(TBusStarterTest) { - struct TStartJobTestModule: public TExampleModule { + struct TStartJobTestModule: public TExampleModule { using TBusModule::CreateDefaultStarter; TAtomic StartCount; @@ -75,7 +75,7 @@ Y_UNIT_TEST_SUITE(TBusStarterTest) { module.Shutdown(); } - struct TSleepModule: public TExampleServerModule { + struct TSleepModule: public TExampleServerModule { TSystemEvent MessageReceivedEvent; TJobHandler Start(TBusJob* job, TBusMessage* mess) override { @@ -110,7 +110,7 @@ Y_UNIT_TEST_SUITE(TBusStarterTest) { module.Shutdown(); } - struct TSendReplyModule: public TExampleServerModule { + struct TSendReplyModule: public TExampleServerModule { TSystemEvent MessageReceivedEvent; TJobHandler Start(TBusJob* job, TBusMessage* mess) override { diff --git a/library/cpp/messagebus/test/ut/sync_client_ut.cpp b/library/cpp/messagebus/test/ut/sync_client_ut.cpp index 7a7189dbec..400128193f 100644 --- a/library/cpp/messagebus/test/ut/sync_client_ut.cpp +++ b/library/cpp/messagebus/test/ut/sync_client_ut.cpp @@ -2,67 +2,67 @@ #include <library/cpp/messagebus/test/helper/object_count_check.h> namespace NBus { - namespace NTest { - using namespace std; + namespace NTest { + using namespace std; - //////////////////////////////////////////////////////////////////// - /// \brief Client for sending synchronous message to local server - struct TSyncClient { - TNetAddr ServerAddr; + //////////////////////////////////////////////////////////////////// + /// \brief Client for sending synchronous message to local server + struct TSyncClient { + TNetAddr ServerAddr; - TExampleProtocol Proto; - TBusMessageQueuePtr Bus; - TBusSyncClientSessionPtr Session; + TExampleProtocol Proto; + TBusMessageQueuePtr Bus; + TBusSyncClientSessionPtr Session; - int NumReplies; - int NumMessages; + int NumReplies; + int NumMessages; - /// constructor creates instances of queue, protocol and session - TSyncClient(const TNetAddr& serverAddr) - : ServerAddr(serverAddr) - { - /// create or get instance of message queue, need one per application - Bus = CreateMessageQueue(); + /// constructor creates instances of queue, protocol and session + TSyncClient(const TNetAddr& serverAddr) + : ServerAddr(serverAddr) + { + /// create or get instance of message queue, need one per application + Bus = CreateMessageQueue(); - NumReplies = 0; - NumMessages = 10; + NumReplies = 0; + NumMessages = 10; - /// register source/client session - TBusClientSessionConfig sessionConfig; - Session = Bus->CreateSyncSource(&Proto, sessionConfig); - Session->RegisterService("localhost"); - } + /// register source/client session + TBusClientSessionConfig sessionConfig; + Session = Bus->CreateSyncSource(&Proto, sessionConfig); + Session->RegisterService("localhost"); + } - ~TSyncClient() { - Session->Shutdown(); - } + ~TSyncClient() { + Session->Shutdown(); + } - /// dispatch of requests is done here - void Work() { - for (int i = 0; i < NumMessages; i++) { - THolder<TExampleRequest> mess(new TExampleRequest(&Proto.RequestCount)); - EMessageStatus status; - THolder<TBusMessage> reply(Session->SendSyncMessage(mess.Get(), status, &ServerAddr)); - if (!!reply) { - NumReplies++; - } - } - } - }; + /// dispatch of requests is done here + void Work() { + for (int i = 0; i < NumMessages; i++) { + THolder<TExampleRequest> mess(new TExampleRequest(&Proto.RequestCount)); + EMessageStatus status; + THolder<TBusMessage> reply(Session->SendSyncMessage(mess.Get(), status, &ServerAddr)); + if (!!reply) { + NumReplies++; + } + } + } + }; Y_UNIT_TEST_SUITE(SyncClientTest) { Y_UNIT_TEST(TestSync) { - TObjectCountCheck objectCountCheck; + TObjectCountCheck objectCountCheck; - TExampleServer server; - TSyncClient client(server.GetActualListenAddr()); - client.Work(); - // assert correct number of replies - UNIT_ASSERT_EQUAL(client.NumReplies, client.NumMessages); - // assert that there is no message left in flight - UNIT_ASSERT_EQUAL(server.Session->GetInFlight(), 0); - UNIT_ASSERT_EQUAL(client.Session->GetInFlight(), 0); - } + TExampleServer server; + TSyncClient client(server.GetActualListenAddr()); + client.Work(); + // assert correct number of replies + UNIT_ASSERT_EQUAL(client.NumReplies, client.NumMessages); + // assert that there is no message left in flight + UNIT_ASSERT_EQUAL(server.Session->GetInFlight(), 0); + UNIT_ASSERT_EQUAL(client.Session->GetInFlight(), 0); + } } } diff --git a/library/cpp/messagebus/test/ya.make b/library/cpp/messagebus/test/ya.make index 5a3b771a1c..0dc4bd4720 100644 --- a/library/cpp/messagebus/test/ya.make +++ b/library/cpp/messagebus/test/ya.make @@ -1,7 +1,7 @@ OWNER(g:messagebus) -RECURSE( +RECURSE( example perftest ut -) +) diff --git a/library/cpp/messagebus/test_utils.h b/library/cpp/messagebus/test_utils.h index 5a6724c27a..2abdf504b1 100644 --- a/library/cpp/messagebus/test_utils.h +++ b/library/cpp/messagebus/test_utils.h @@ -1,12 +1,12 @@ #pragma once // Do nothing if there is no support for IPv4 -#define ASSUME_IP_V4_ENABLED \ - do { \ - try { \ - TNetworkAddress("192.168.0.42", 80); \ - } catch (const TNetworkResolutionError& ex) { \ - Y_UNUSED(ex); \ - return; \ - } \ +#define ASSUME_IP_V4_ENABLED \ + do { \ + try { \ + TNetworkAddress("192.168.0.42", 80); \ + } catch (const TNetworkResolutionError& ex) { \ + Y_UNUSED(ex); \ + return; \ + } \ } while (0) diff --git a/library/cpp/messagebus/use_after_free_checker.cpp b/library/cpp/messagebus/use_after_free_checker.cpp index 01f38d8d34..4904e7c614 100644 --- a/library/cpp/messagebus/use_after_free_checker.cpp +++ b/library/cpp/messagebus/use_after_free_checker.cpp @@ -3,8 +3,8 @@ #include <util/system/yassert.h> namespace { - const ui64 VALID = (ui64)0xAABBCCDDEEFF0011LL; - const ui64 INVALID = (ui64)0x1122334455667788LL; + const ui64 VALID = (ui64)0xAABBCCDDEEFF0011LL; + const ui64 INVALID = (ui64)0x1122334455667788LL; } TUseAfterFreeChecker::TUseAfterFreeChecker() @@ -12,7 +12,7 @@ TUseAfterFreeChecker::TUseAfterFreeChecker() { } -TUseAfterFreeChecker::~TUseAfterFreeChecker() { +TUseAfterFreeChecker::~TUseAfterFreeChecker() { Y_VERIFY(Magic == VALID, "Corrupted"); Magic = INVALID; } diff --git a/library/cpp/messagebus/use_after_free_checker.h b/library/cpp/messagebus/use_after_free_checker.h index 7f6db63af7..590b076156 100644 --- a/library/cpp/messagebus/use_after_free_checker.h +++ b/library/cpp/messagebus/use_after_free_checker.h @@ -6,7 +6,7 @@ class TUseAfterFreeChecker { private: ui64 Magic; - + public: TUseAfterFreeChecker(); ~TUseAfterFreeChecker(); @@ -17,7 +17,7 @@ public: class TUseAfterFreeCheckerGuard { private: const TUseAfterFreeChecker& Check; - + public: TUseAfterFreeCheckerGuard(const TUseAfterFreeChecker& check) : Check(check) diff --git a/library/cpp/messagebus/use_count_checker.cpp b/library/cpp/messagebus/use_count_checker.cpp index 387688af39..c6243ea21f 100644 --- a/library/cpp/messagebus/use_count_checker.cpp +++ b/library/cpp/messagebus/use_count_checker.cpp @@ -8,7 +8,7 @@ TUseCountChecker::TUseCountChecker() { TUseCountChecker::~TUseCountChecker() { TAtomicBase count = Counter.Val(); - Y_VERIFY(count == 0, "must not release when count is not zero: %ld", (long)count); + Y_VERIFY(count == 0, "must not release when count is not zero: %ld", (long)count); } void TUseCountChecker::Inc() { @@ -21,8 +21,8 @@ void TUseCountChecker::Dec() { TUseCountHolder::TUseCountHolder() : CurrentChecker(nullptr) -{ -} +{ +} TUseCountHolder::TUseCountHolder(TUseCountChecker* currentChecker) : CurrentChecker(currentChecker) @@ -32,7 +32,7 @@ TUseCountHolder::TUseCountHolder(TUseCountChecker* currentChecker) } } -TUseCountHolder::~TUseCountHolder() { +TUseCountHolder::~TUseCountHolder() { if (!!CurrentChecker) { CurrentChecker->Dec(); } diff --git a/library/cpp/messagebus/use_count_checker.h b/library/cpp/messagebus/use_count_checker.h index 9e008f0c22..70bef6fa8a 100644 --- a/library/cpp/messagebus/use_count_checker.h +++ b/library/cpp/messagebus/use_count_checker.h @@ -5,7 +5,7 @@ class TUseCountChecker { private: TAtomicCounter Counter; - + public: TUseCountChecker(); ~TUseCountChecker(); @@ -16,7 +16,7 @@ public: class TUseCountHolder { private: TUseCountChecker* CurrentChecker; - + public: TUseCountHolder(); explicit TUseCountHolder(TUseCountChecker* currentChecker); diff --git a/library/cpp/messagebus/vector_swaps.h b/library/cpp/messagebus/vector_swaps.h index 7f22fd7eb3..b920bcf03e 100644 --- a/library/cpp/messagebus/vector_swaps.h +++ b/library/cpp/messagebus/vector_swaps.h @@ -8,7 +8,7 @@ #include <stdlib.h> template <typename T, class A = std::allocator<T>> -class TVectorSwaps : TNonCopyable { +class TVectorSwaps : TNonCopyable { private: T* Start; T* Finish; @@ -23,15 +23,15 @@ public: typedef T* iterator; typedef const T* const_iterator; - typedef std::reverse_iterator<iterator> reverse_iterator; - typedef std::reverse_iterator<const_iterator> const_reverse_iterator; + typedef std::reverse_iterator<iterator> reverse_iterator; + typedef std::reverse_iterator<const_iterator> const_reverse_iterator; - TVectorSwaps() - : Start() - , Finish() - , EndOfStorage() - { - } + TVectorSwaps() + : Start() + , Finish() + , EndOfStorage() + { + } ~TVectorSwaps() { for (size_t i = 0; i < size(); ++i) { @@ -94,19 +94,19 @@ public: return Finish; } - reverse_iterator rbegin() { - return reverse_iterator(end()); - } - reverse_iterator rend() { - return reverse_iterator(begin()); - } + reverse_iterator rbegin() { + return reverse_iterator(end()); + } + reverse_iterator rend() { + return reverse_iterator(begin()); + } - const_reverse_iterator rbegin() const { - return reverse_iterator(end()); - } - const_reverse_iterator rend() const { - return reverse_iterator(begin()); - } + const_reverse_iterator rbegin() const { + return reverse_iterator(end()); + } + const_reverse_iterator rend() const { + return reverse_iterator(begin()); + } void swap(TVectorSwaps<T>& that) { DoSwap(Start, that.Start); @@ -121,7 +121,7 @@ public: size_t newCapacity = FastClp2(n); TVectorSwaps<T> tmp; - tmp.Start = (T*)malloc(sizeof(T) * newCapacity); + tmp.Start = (T*)malloc(sizeof(T) * newCapacity); Y_VERIFY(!!tmp.Start); tmp.EndOfStorage = tmp.Start + newCapacity; diff --git a/library/cpp/messagebus/vector_swaps_ut.cpp b/library/cpp/messagebus/vector_swaps_ut.cpp index 788cccc7e6..693cc6857b 100644 --- a/library/cpp/messagebus/vector_swaps_ut.cpp +++ b/library/cpp/messagebus/vector_swaps_ut.cpp @@ -4,7 +4,7 @@ Y_UNIT_TEST_SUITE(TVectorSwapsTest) { Y_UNIT_TEST(Simple) { - TVectorSwaps<THolder<unsigned>> v; + TVectorSwaps<THolder<unsigned>> v; for (unsigned i = 0; i < 100; ++i) { THolder<unsigned> tmp(new unsigned(i)); v.push_back(tmp); diff --git a/library/cpp/messagebus/www/concat_strings.h b/library/cpp/messagebus/www/concat_strings.h index 77a77d4695..7b730564eb 100644 --- a/library/cpp/messagebus/www/concat_strings.h +++ b/library/cpp/messagebus/www/concat_strings.h @@ -5,16 +5,16 @@ // ATTN: not equivalent to TString::Join - cat concat anything "outputable" to stream, not only TString convertable types. -inline void DoConcatStrings(TStringStream&) { +inline void DoConcatStrings(TStringStream&) { } -template <class T, class... R> -inline void DoConcatStrings(TStringStream& ss, const T& t, const R&... r) { +template <class T, class... R> +inline void DoConcatStrings(TStringStream& ss, const T& t, const R&... r) { ss << t; DoConcatStrings(ss, r...); } -template <class... R> +template <class... R> inline TString ConcatStrings(const R&... r) { TStringStream ss; DoConcatStrings(ss, r...); diff --git a/library/cpp/messagebus/www/html_output.cpp b/library/cpp/messagebus/www/html_output.cpp index 5128f74718..10ea2e163b 100644 --- a/library/cpp/messagebus/www/html_output.cpp +++ b/library/cpp/messagebus/www/html_output.cpp @@ -1,4 +1,4 @@ #include "html_output.h" -Y_POD_THREAD(IOutputStream*) -HtmlOutputStreamPtr; +Y_POD_THREAD(IOutputStream*) +HtmlOutputStreamPtr; diff --git a/library/cpp/messagebus/www/html_output.h b/library/cpp/messagebus/www/html_output.h index 50c3f1fa15..27e77adefa 100644 --- a/library/cpp/messagebus/www/html_output.h +++ b/library/cpp/messagebus/www/html_output.h @@ -32,26 +32,26 @@ struct TChars { TString Text; bool NeedEscape; - TChars(TStringBuf text) - : Text(text) - , NeedEscape(true) - { - } - TChars(TStringBuf text, bool escape) - : Text(text) - , NeedEscape(escape) - { - } - TChars(const char* text) - : Text(text) - , NeedEscape(true) - { - } - TChars(const char* text, bool escape) - : Text(text) - , NeedEscape(escape) - { - } + TChars(TStringBuf text) + : Text(text) + , NeedEscape(true) + { + } + TChars(TStringBuf text, bool escape) + : Text(text) + , NeedEscape(escape) + { + } + TChars(const char* text) + : Text(text) + , NeedEscape(true) + { + } + TChars(const char* text, bool escape) + : Text(text) + , NeedEscape(escape) + { + } TString Escape() { if (NeedEscape) { @@ -67,13 +67,13 @@ struct TAttr { TString Value; TAttr(TStringBuf name, TStringBuf value) - : Name(name) - , Value(value) - { - } + : Name(name) + , Value(value) + { + } - TAttr() { - } + TAttr() { + } bool operator!() const { return !Name; @@ -239,9 +239,9 @@ static inline void BootstrapInfo(TStringBuf text) { static inline void ScriptHref(TStringBuf href) { Open("script", - TAttr("language", "javascript"), - TAttr("type", "text/javascript"), - TAttr("src", href)); + TAttr("language", "javascript"), + TAttr("type", "text/javascript"), + TAttr("src", href)); Close("script"); Nl(); } @@ -290,17 +290,17 @@ struct TTagGuard { struct TDivGuard: public TTagGuard { TDivGuard(TStringBuf cssClass, TStringBuf id = "") : TTagGuard("div", cssClass, id) - { - } + { + } TDivGuard(TAttr a1 = TAttr(), TAttr a2 = TAttr(), TAttr a3 = TAttr()) : TTagGuard("div", a1, a2, a3) - { - } + { + } }; struct TAGuard { - TAGuard(TStringBuf href) { + TAGuard(TStringBuf href) { OpenA(href); } @@ -318,7 +318,7 @@ struct TScriptFunctionGuard { Line("$(function() {"); } - ~TScriptFunctionGuard() { + ~TScriptFunctionGuard() { Line("});"); } }; diff --git a/library/cpp/messagebus/www/www.cpp b/library/cpp/messagebus/www/www.cpp index 9af596a143..62ec241d85 100644 --- a/library/cpp/messagebus/www/www.cpp +++ b/library/cpp/messagebus/www/www.cpp @@ -35,7 +35,7 @@ namespace { template <typename TValuePtr> struct TNamedValues { - TVector<std::pair<TString, TValuePtr>> Entries; + TVector<std::pair<TString, TValuePtr>> Entries; TValuePtr FindByName(TStringBuf name) { Y_VERIFY(!!name); @@ -79,7 +79,7 @@ namespace { } } - for (unsigned i = 1;; ++i) { + for (unsigned i = 1;; ++i) { TString prefix = p->GetNameInternal(); if (!prefix) { prefix = "unnamed"; @@ -169,7 +169,7 @@ const unsigned char WWW_STATIC_DATA[] = { #include "www_static.inc" }; -class TWwwStaticLoader: public TArchiveReader { +class TWwwStaticLoader: public TArchiveReader { public: TWwwStaticLoader() : TArchiveReader(TBlob::NoCopy(WWW_STATIC_DATA, sizeof(WWW_STATIC_DATA))) @@ -180,8 +180,8 @@ public: struct TBusWww::TImpl { // TODO: use weak pointers TNamedValues<TBusMessageQueuePtr> Queues; - TSessionValues<TIntrusivePtr<TBusClientSession>> ClientSessions; - TSessionValues<TIntrusivePtr<TBusServerSession>> ServerSessions; + TSessionValues<TIntrusivePtr<TBusClientSession>> ClientSessions; + TSessionValues<TIntrusivePtr<TBusServerSession>> ServerSessions; TSessionValues<TBusModuleInternalPtr> Modules; TMutex Mutex; @@ -250,12 +250,12 @@ struct TBusWww::TImpl { const TOptionalParams& Params; TRequest(TImpl* outer, IOutputStream& os, const TCgiParameters& cgiParams, const TOptionalParams& params) - : Outer(outer) - , Os(os) - , CgiParams(cgiParams) - , Params(params) - { - } + : Outer(outer) + , Os(os) + , CgiParams(cgiParams) + , Params(params) + { + } void CrumbsParentLinks() { for (unsigned i = 0; i < Params.ParentLinks.size(); ++i) { @@ -482,7 +482,7 @@ struct TBusWww::TImpl { if (needTick) { ticks.BeginList(); ticks.WriteInt(i); - ticks.WriteString(Sprintf(":%02u:%02u", (unsigned)minuteOfHour, (unsigned)secondOfMinute)); + ticks.WriteString(Sprintf(":%02u:%02u", (unsigned)minuteOfHour, (unsigned)secondOfMinute)); ticks.EndList(); } } @@ -554,10 +554,10 @@ struct TBusWww::TImpl { { TDivGuard div; TTagGuard button("button", - TAttr("type", "button"), - TAttr("class", "btn"), - TAttr("data-toggle", "collapse"), - TAttr("data-target", "#connections")); + TAttr("type", "button"), + TAttr("class", "btn"), + TAttr("data-toggle", "collapse"), + TAttr("data-target", "#connections")); Text("Show connection details"); } { @@ -572,7 +572,7 @@ struct TBusWww::TImpl { H3("Message process time histogram"); const TDurationHistogram& h = - dumpStatus.ConnectionStatusSummary.WriterStatus.Incremental.ProcessDurationHistogram; + dumpStatus.ConnectionStatusSummary.WriterStatus.Incremental.ProcessDurationHistogram; { TDivGuard div; @@ -630,7 +630,7 @@ struct TBusWww::TImpl { } void WriteMessageCounterSensors(NMonitoring::TDeprecatedJsonWriter& sj, - TStringBuf labelName, TStringBuf sessionName, bool read, const TMessageCounter& counter) { + TStringBuf labelName, TStringBuf sessionName, bool read, const TMessageCounter& counter) { TStringBuf readOrWrite = read ? "read" : "write"; sj.OpenMetric(); @@ -647,7 +647,7 @@ struct TBusWww::TImpl { } void WriteSessionStatus(NMonitoring::TDeprecatedJsonWriter& sj, TStringBuf sessionName, bool client, - TBusSession* session) { + TBusSession* session) { TStringBuf labelName = client ? "mb_client_session" : "mb_server_session"; auto status = session->GetStatusRecordInternal(); @@ -675,9 +675,9 @@ struct TBusWww::TImpl { } WriteMessageCounterSensors(sj, labelName, sessionName, false, - status.ConnectionStatusSummary.WriterStatus.Incremental.MessageCounter); + status.ConnectionStatusSummary.WriterStatus.Incremental.MessageCounter); WriteMessageCounterSensors(sj, labelName, sessionName, true, - status.ConnectionStatusSummary.ReaderStatus.Incremental.MessageCounter); + status.ConnectionStatusSummary.ReaderStatus.Incremental.MessageCounter); } void ServeSolomonJson(const TString& q, const TString& cs, const TString& ss) { @@ -813,14 +813,14 @@ NBus::TBusWww::TBusWww() { } -NBus::TBusWww::~TBusWww() { +NBus::TBusWww::~TBusWww() { } -void NBus::TBusWww::RegisterClientSession(TBusClientSessionPtr s) { +void NBus::TBusWww::RegisterClientSession(TBusClientSessionPtr s) { Impl->RegisterClientSession(s); } -void TBusWww::RegisterServerSession(TBusServerSessionPtr s) { +void TBusWww::RegisterServerSession(TBusServerSessionPtr s) { Impl->RegisterServerSession(s); } @@ -833,8 +833,8 @@ void TBusWww::RegisterModule(TBusModule* module) { } void TBusWww::ServeHttp(IOutputStream& httpOutputStream, - const TCgiParameters& queryArgs, - const TBusWww::TOptionalParams& params) { + const TCgiParameters& queryArgs, + const TBusWww::TOptionalParams& params) { Impl->ServeHttp(httpOutputStream, queryArgs, params); } @@ -861,8 +861,8 @@ struct TBusWwwHttpServer::TImpl: public THttpServer::ICallBack { TClientRequestImpl(TBusWwwHttpServer::TImpl* outer) : Outer(outer) - { - } + { + } bool Reply(void*) override { Outer->ServeRequest(Input(), Output()); @@ -878,8 +878,8 @@ struct TBusWwwHttpServer::TImpl: public THttpServer::ICallBack { } TStringStream ss; ss << "HTTP/1.1 " - << code << " " << text << "\r\nConnection: Close\r\n\r\n" - << content; + << code << " " << text << "\r\nConnection: Close\r\n\r\n" + << content; return ss.Str(); } @@ -908,7 +908,7 @@ struct TBusWwwHttpServer::TImpl: public THttpServer::ICallBack { Www->ServeHttp(output, cgiParams, params); } catch (...) { output << MakeSimpleResponse(500, "Exception", - TString() + "Exception: " + CurrentExceptionMessage()); + TString() + "Exception: " + CurrentExceptionMessage()); } } @@ -926,5 +926,5 @@ NBus::TBusWwwHttpServer::TBusWwwHttpServer(TIntrusivePtr<TBusWww> www, unsigned { } -NBus::TBusWwwHttpServer::~TBusWwwHttpServer() { +NBus::TBusWwwHttpServer::~TBusWwwHttpServer() { } diff --git a/library/cpp/messagebus/www/www.h b/library/cpp/messagebus/www/www.h index 064f42b9db..6cd652b477 100644 --- a/library/cpp/messagebus/www/www.h +++ b/library/cpp/messagebus/www/www.h @@ -8,38 +8,38 @@ #include <library/cpp/cgiparam/cgiparam.h> namespace NBus { - class TBusWww: public TAtomicRefCount<TBusWww> { - public: - struct TLink { - TString Title; - TString Href; - }; + class TBusWww: public TAtomicRefCount<TBusWww> { + public: + struct TLink { + TString Title; + TString Href; + }; - struct TOptionalParams { - TVector<TLink> ParentLinks; - }; + struct TOptionalParams { + TVector<TLink> ParentLinks; + }; - TBusWww(); - ~TBusWww(); + TBusWww(); + ~TBusWww(); - void RegisterClientSession(TBusClientSessionPtr); - void RegisterServerSession(TBusServerSessionPtr); - void RegisterQueue(TBusMessageQueuePtr); - void RegisterModule(TBusModule*); + void RegisterClientSession(TBusClientSessionPtr); + void RegisterServerSession(TBusServerSessionPtr); + void RegisterQueue(TBusMessageQueuePtr); + void RegisterModule(TBusModule*); - void ServeHttp(IOutputStream& httpOutputStream, const TCgiParameters& queryArgs, const TOptionalParams& params = TOptionalParams()); + void ServeHttp(IOutputStream& httpOutputStream, const TCgiParameters& queryArgs, const TOptionalParams& params = TOptionalParams()); - struct TImpl; - THolder<TImpl> Impl; - }; + struct TImpl; + THolder<TImpl> Impl; + }; - class TBusWwwHttpServer { - public: - TBusWwwHttpServer(TIntrusivePtr<TBusWww> www, unsigned port); - ~TBusWwwHttpServer(); + class TBusWwwHttpServer { + public: + TBusWwwHttpServer(TIntrusivePtr<TBusWww> www, unsigned port); + ~TBusWwwHttpServer(); - struct TImpl; - THolder<TImpl> Impl; - }; + struct TImpl; + THolder<TImpl> Impl; + }; -} +} diff --git a/library/cpp/messagebus/ybus.h b/library/cpp/messagebus/ybus.h index 784fba4c1b..de21ad8521 100644 --- a/library/cpp/messagebus/ybus.h +++ b/library/cpp/messagebus/ybus.h @@ -1,8 +1,8 @@ -#pragma once - +#pragma once + /// Asynchronous Messaging Library implements framework for sending and /// receiving messages between loosely connected processes. - + #include "coreconn.h" #include "defs.h" #include "handler.h" @@ -35,171 +35,171 @@ #include <util/system/mutex.h> namespace NBus { - //////////////////////////////////////////////////////// - /// \brief Common structure to store address information - - int CompareByHost(const IRemoteAddr& l, const IRemoteAddr& r) noexcept; - bool operator<(const TNetAddr& a1, const TNetAddr& a2); // compare by addresses - - ///////////////////////////////////////////////////////////////////////// - /// \brief Handles routing and data encoding to/from wire - - /// Protocol is stateless threadsafe singleton object that - /// encapsulates relationship between a message (TBusMessage) object - /// and destination server. Protocol object is reponsible for serializing in-memory - /// message and reply into the wire, retuning name of the service and resource - /// distribution key for given protocol. - - /// Protocol object should transparently handle messages and replies. - /// This is interface only class, actuall instances of the protocols - /// should be created using templates inhereted from this base class. - class TBusProtocol { - private: - TString ServiceName; - int ServicePort; - - public: - TBusProtocol(TBusService name = "UNKNOWN", int port = 0) - : ServiceName(name) - , ServicePort(port) - { - } - - /// returns service type for this protocol and message - TBusService GetService() const { + //////////////////////////////////////////////////////// + /// \brief Common structure to store address information + + int CompareByHost(const IRemoteAddr& l, const IRemoteAddr& r) noexcept; + bool operator<(const TNetAddr& a1, const TNetAddr& a2); // compare by addresses + + ///////////////////////////////////////////////////////////////////////// + /// \brief Handles routing and data encoding to/from wire + + /// Protocol is stateless threadsafe singleton object that + /// encapsulates relationship between a message (TBusMessage) object + /// and destination server. Protocol object is reponsible for serializing in-memory + /// message and reply into the wire, retuning name of the service and resource + /// distribution key for given protocol. + + /// Protocol object should transparently handle messages and replies. + /// This is interface only class, actuall instances of the protocols + /// should be created using templates inhereted from this base class. + class TBusProtocol { + private: + TString ServiceName; + int ServicePort; + + public: + TBusProtocol(TBusService name = "UNKNOWN", int port = 0) + : ServiceName(name) + , ServicePort(port) + { + } + + /// returns service type for this protocol and message + TBusService GetService() const { return ServiceName.data(); - } + } - /// returns port number for destination session to open socket - int GetPort() const { - return ServicePort; - } + /// returns port number for destination session to open socket + int GetPort() const { + return ServicePort; + } - virtual ~TBusProtocol() { - } + virtual ~TBusProtocol() { + } - /// \brief serialized protocol specific data into TBusData - /// \note buffer passed to the function (data) is not empty, use append functions - virtual void Serialize(const TBusMessage* mess, TBuffer& data) = 0; + /// \brief serialized protocol specific data into TBusData + /// \note buffer passed to the function (data) is not empty, use append functions + virtual void Serialize(const TBusMessage* mess, TBuffer& data) = 0; - /// deserialized TBusData into new instance of the message - virtual TAutoPtr<TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) = 0; + /// deserialized TBusData into new instance of the message + virtual TAutoPtr<TBusMessage> Deserialize(ui16 messageType, TArrayRef<const char> payload) = 0; - /// returns key for messages of this protocol - virtual TBusKey GetKey(const TBusMessage*) { - return YBUS_KEYMIN; - } + /// returns key for messages of this protocol + virtual TBusKey GetKey(const TBusMessage*) { + return YBUS_KEYMIN; + } - /// default implementation of routing policy to allow overrides - virtual EMessageStatus GetDestination(const TBusClientSession* session, TBusMessage* mess, TBusLocator* locator, TNetAddr* addr); + /// default implementation of routing policy to allow overrides + virtual EMessageStatus GetDestination(const TBusClientSession* session, TBusMessage* mess, TBusLocator* locator, TNetAddr* addr); - /// codec for transport level compression - virtual NCodecs::TCodecPtr GetTransportCodec(void) const { - return NCodecs::ICodec::GetInstance("snappy"); - } - }; + /// codec for transport level compression + virtual NCodecs::TCodecPtr GetTransportCodec(void) const { + return NCodecs::ICodec::GetInstance("snappy"); + } + }; - class TBusSyncSourceSession: public TAtomicRefCount<TBusSyncSourceSession> { - friend class TBusMessageQueue; + class TBusSyncSourceSession: public TAtomicRefCount<TBusSyncSourceSession> { + friend class TBusMessageQueue; - public: - TBusSyncSourceSession(TIntrusivePtr< ::NBus::NPrivate::TBusSyncSourceSessionImpl> session); - ~TBusSyncSourceSession(); + public: + TBusSyncSourceSession(TIntrusivePtr< ::NBus::NPrivate::TBusSyncSourceSessionImpl> session); + ~TBusSyncSourceSession(); - void Shutdown(); + void Shutdown(); - TBusMessage* SendSyncMessage(TBusMessage* pMessage, EMessageStatus& status, const TNetAddr* addr = nullptr); + TBusMessage* SendSyncMessage(TBusMessage* pMessage, EMessageStatus& status, const TNetAddr* addr = nullptr); - int RegisterService(const char* hostname, TBusKey start = YBUS_KEYMIN, TBusKey end = YBUS_KEYMAX, EIpVersion ipVersion = EIP_VERSION_4); + int RegisterService(const char* hostname, TBusKey start = YBUS_KEYMIN, TBusKey end = YBUS_KEYMAX, EIpVersion ipVersion = EIP_VERSION_4); - int GetInFlight(); + int GetInFlight(); - const TBusProtocol* GetProto() const; + const TBusProtocol* GetProto() const; - const TBusClientSession* GetBusClientSessionWorkaroundDoNotUse() const; // It's for TLoadBalancedProtocol::GetDestination() function that really needs TBusClientSession* unlike all other protocols. Look at review 32425 (http://rb.yandex-team.ru/arc/r/32425/) for more information. - private: - TIntrusivePtr< ::NBus::NPrivate::TBusSyncSourceSessionImpl> Session; - }; + const TBusClientSession* GetBusClientSessionWorkaroundDoNotUse() const; // It's for TLoadBalancedProtocol::GetDestination() function that really needs TBusClientSession* unlike all other protocols. Look at review 32425 (http://rb.yandex-team.ru/arc/r/32425/) for more information. + private: + TIntrusivePtr< ::NBus::NPrivate::TBusSyncSourceSessionImpl> Session; + }; - using TBusSyncClientSessionPtr = TIntrusivePtr<TBusSyncSourceSession>; + using TBusSyncClientSessionPtr = TIntrusivePtr<TBusSyncSourceSession>; - /////////////////////////////////////////////////////////////////// - /// \brief Main message queue object, need one per application - class TBusMessageQueue: public TAtomicRefCount<TBusMessageQueue> { - /// allow mesage queue to be created only via factory - friend TBusMessageQueuePtr CreateMessageQueue(const TBusQueueConfig& config, NActor::TExecutorPtr executor, TBusLocator* locator, const char* name); - friend class ::NBus::NPrivate::TRemoteConnection; - friend struct ::NBus::NPrivate::TBusSessionImpl; - friend class ::NBus::NPrivate::TAcceptor; - friend struct ::NBus::TBusServerSession; + /////////////////////////////////////////////////////////////////// + /// \brief Main message queue object, need one per application + class TBusMessageQueue: public TAtomicRefCount<TBusMessageQueue> { + /// allow mesage queue to be created only via factory + friend TBusMessageQueuePtr CreateMessageQueue(const TBusQueueConfig& config, NActor::TExecutorPtr executor, TBusLocator* locator, const char* name); + friend class ::NBus::NPrivate::TRemoteConnection; + friend struct ::NBus::NPrivate::TBusSessionImpl; + friend class ::NBus::NPrivate::TAcceptor; + friend struct ::NBus::TBusServerSession; - private: - const TBusQueueConfig Config; - TMutex Lock; - TList<TIntrusivePtr< ::NBus::NPrivate::TBusSessionImpl>> Sessions; - TSimpleIntrusivePtr<TBusLocator> Locator; - NPrivate::TScheduler Scheduler; + private: + const TBusQueueConfig Config; + TMutex Lock; + TList<TIntrusivePtr< ::NBus::NPrivate::TBusSessionImpl>> Sessions; + TSimpleIntrusivePtr<TBusLocator> Locator; + NPrivate::TScheduler Scheduler; - ::NActor::TExecutorPtr WorkQueue; + ::NActor::TExecutorPtr WorkQueue; - TAtomic Running; + TAtomic Running; TSystemEvent ShutdownComplete; - private: - /// constructor is protected, used NBus::CreateMessageQueue() to create a instance - TBusMessageQueue(const TBusQueueConfig& config, NActor::TExecutorPtr executor, TBusLocator* locator, const char* name); + private: + /// constructor is protected, used NBus::CreateMessageQueue() to create a instance + TBusMessageQueue(const TBusQueueConfig& config, NActor::TExecutorPtr executor, TBusLocator* locator, const char* name); - public: - TString GetNameInternal() const; + public: + TString GetNameInternal() const; - ~TBusMessageQueue(); + ~TBusMessageQueue(); - void Stop(); - bool IsRunning(); + void Stop(); + bool IsRunning(); - public: - void EnqueueWork(TArrayRef< ::NActor::IWorkItem* const> w) { - WorkQueue->EnqueueWork(w); - } + public: + void EnqueueWork(TArrayRef< ::NActor::IWorkItem* const> w) { + WorkQueue->EnqueueWork(w); + } - ::NActor::TExecutor* GetExecutor() { - return WorkQueue.Get(); - } + ::NActor::TExecutor* GetExecutor() { + return WorkQueue.Get(); + } - TString GetStatus(ui16 flags = YBUS_STATUS_CONNS) const; - // without sessions - NPrivate::TBusMessageQueueStatus GetStatusRecordInternal() const; - TString GetStatusSelf() const; - TString GetStatusSingleLine() const; + TString GetStatus(ui16 flags = YBUS_STATUS_CONNS) const; + // without sessions + NPrivate::TBusMessageQueueStatus GetStatusRecordInternal() const; + TString GetStatusSelf() const; + TString GetStatusSingleLine() const; - TBusLocator* GetLocator() const { - return Locator.Get(); - } + TBusLocator* GetLocator() const { + return Locator.Get(); + } - TBusClientSessionPtr CreateSource(TBusProtocol* proto, IBusClientHandler* handler, const TBusClientSessionConfig& config, const TString& name = ""); - TBusSyncClientSessionPtr CreateSyncSource(TBusProtocol* proto, const TBusClientSessionConfig& config, bool needReply = true, const TString& name = ""); - TBusServerSessionPtr CreateDestination(TBusProtocol* proto, IBusServerHandler* hander, const TBusServerSessionConfig& config, const TString& name = ""); - TBusServerSessionPtr CreateDestination(TBusProtocol* proto, IBusServerHandler* hander, const TBusServerSessionConfig& config, const TVector<TBindResult>& bindTo, const TString& name = ""); + TBusClientSessionPtr CreateSource(TBusProtocol* proto, IBusClientHandler* handler, const TBusClientSessionConfig& config, const TString& name = ""); + TBusSyncClientSessionPtr CreateSyncSource(TBusProtocol* proto, const TBusClientSessionConfig& config, bool needReply = true, const TString& name = ""); + TBusServerSessionPtr CreateDestination(TBusProtocol* proto, IBusServerHandler* hander, const TBusServerSessionConfig& config, const TString& name = ""); + TBusServerSessionPtr CreateDestination(TBusProtocol* proto, IBusServerHandler* hander, const TBusServerSessionConfig& config, const TVector<TBindResult>& bindTo, const TString& name = ""); - private: - void Destroy(TBusSession* session); - void Destroy(TBusSyncClientSessionPtr session); + private: + void Destroy(TBusSession* session); + void Destroy(TBusSyncClientSessionPtr session); - public: - void Schedule(NPrivate::IScheduleItemAutoPtr i); + public: + void Schedule(NPrivate::IScheduleItemAutoPtr i); - private: - void DestroyAllSessions(); - void Add(TIntrusivePtr< ::NBus::NPrivate::TBusSessionImpl> session); - void Remove(TBusSession* session); - }; + private: + void DestroyAllSessions(); + void Add(TIntrusivePtr< ::NBus::NPrivate::TBusSessionImpl> session); + void Remove(TBusSession* session); + }; - ///////////////////////////////////////////////////////////////// - /// Factory methods to construct message queue - TBusMessageQueuePtr CreateMessageQueue(const char* name = ""); - TBusMessageQueuePtr CreateMessageQueue(NActor::TExecutorPtr executor, const char* name = ""); - TBusMessageQueuePtr CreateMessageQueue(const TBusQueueConfig& config, const char* name = ""); - TBusMessageQueuePtr CreateMessageQueue(const TBusQueueConfig& config, TBusLocator* locator, const char* name = ""); - TBusMessageQueuePtr CreateMessageQueue(const TBusQueueConfig& config, NActor::TExecutorPtr executor, TBusLocator* locator, const char* name = ""); + ///////////////////////////////////////////////////////////////// + /// Factory methods to construct message queue + TBusMessageQueuePtr CreateMessageQueue(const char* name = ""); + TBusMessageQueuePtr CreateMessageQueue(NActor::TExecutorPtr executor, const char* name = ""); + TBusMessageQueuePtr CreateMessageQueue(const TBusQueueConfig& config, const char* name = ""); + TBusMessageQueuePtr CreateMessageQueue(const TBusQueueConfig& config, TBusLocator* locator, const char* name = ""); + TBusMessageQueuePtr CreateMessageQueue(const TBusQueueConfig& config, NActor::TExecutorPtr executor, TBusLocator* locator, const char* name = ""); -} +} |