session_impl.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  1. #pragma once
  2. #include "acceptor_status.h"
  3. #include "async_result.h"
  4. #include "event_loop.h"
  5. #include "netaddr.h"
  6. #include "remote_connection.h"
  7. #include "remote_connection_status.h"
  8. #include "session_job_count.h"
  9. #include "shutdown_state.h"
  10. #include "ybus.h"
  11. #include <library/cpp/messagebus/actor/actor.h>
  12. #include <library/cpp/messagebus/actor/queue_in_actor.h>
  13. #include <library/cpp/messagebus/monitoring/mon_proto.pb.h>
  14. #include <library/cpp/threading/future/legacy_future.h>
  15. #include <util/generic/array_ref.h>
  16. #include <util/generic/string.h>
  17. namespace NBus {
  18. namespace NPrivate {
  19. typedef TIntrusivePtr<TRemoteClientConnection> TRemoteClientConnectionPtr;
  20. typedef TIntrusivePtr<TRemoteServerConnection> TRemoteServerConnectionPtr;
  21. typedef TIntrusivePtr<TRemoteServerSession> TRemoteServerSessionPtr;
  22. typedef TIntrusivePtr<TAcceptor> TAcceptorPtr;
  23. typedef TVector<TAcceptorPtr> TAcceptorsPtrs;
  24. struct TConnectionsAcceptorsSnapshot {
  25. TVector<TRemoteConnectionPtr> Connections;
  26. TVector<TAcceptorPtr> Acceptors;
  27. ui64 LastConnectionId;
  28. ui64 LastAcceptorId;
  29. TConnectionsAcceptorsSnapshot();
  30. };
  31. typedef TAtomicSharedPtr<TConnectionsAcceptorsSnapshot> TConnectionsAcceptorsSnapshotPtr;
  32. struct TOnAccept {
  33. SOCKET s;
  34. TNetAddr addr;
  35. TInstant now;
  36. };
  37. struct TStatusTag {};
  38. struct TConnectionTag {};
  39. struct TDeadConnectionTag {};
  40. struct TRemoveTag {};
  41. struct TBusSessionImpl
  42. : public virtual TBusSession,
  43. private ::NActor::TActor<TBusSessionImpl, TStatusTag>,
  44. private ::NActor::TActor<TBusSessionImpl, TConnectionTag>
  45. ,
  46. private ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionWriterIncrementalStatus, TStatusTag, TDeadConnectionTag>,
  47. private ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionReaderIncrementalStatus, TStatusTag, TDeadConnectionTag>,
  48. private ::NActor::TQueueInActor<TBusSessionImpl, TAcceptorStatus, TStatusTag, TDeadConnectionTag>
  49. ,
  50. private ::NActor::TQueueInActor<TBusSessionImpl, TOnAccept, TConnectionTag>,
  51. private ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionPtr, TConnectionTag, TRemoveTag> {
  52. friend class TAcceptor;
  53. friend class TRemoteConnection;
  54. friend class TRemoteServerConnection;
  55. friend class ::NActor::TActor<TBusSessionImpl, TStatusTag>;
  56. friend class ::NActor::TActor<TBusSessionImpl, TConnectionTag>;
  57. friend class ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionWriterIncrementalStatus, TStatusTag, TDeadConnectionTag>;
  58. friend class ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionReaderIncrementalStatus, TStatusTag, TDeadConnectionTag>;
  59. friend class ::NActor::TQueueInActor<TBusSessionImpl, TAcceptorStatus, TStatusTag, TDeadConnectionTag>;
  60. friend class ::NActor::TQueueInActor<TBusSessionImpl, TOnAccept, TConnectionTag>;
  61. friend class ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionPtr, TConnectionTag, TRemoveTag>;
  62. public:
  63. ::NActor::TQueueInActor<TBusSessionImpl, TOnAccept, TConnectionTag>* GetOnAcceptQueue() {
  64. return this;
  65. }
  66. ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionPtr, TConnectionTag, TRemoveTag>* GetRemoveConnectionQueue() {
  67. return this;
  68. }
  69. ::NActor::TActor<TBusSessionImpl, TConnectionTag>* GetConnectionActor() {
  70. return this;
  71. }
  72. typedef TGuard<TMutex> TConnectionsGuard;
  73. TBusSessionImpl(bool isSource, TBusMessageQueue* queue, TBusProtocol* proto,
  74. IBusErrorHandler* handler,
  75. const TBusSessionConfig& config, const TString& name);
  76. ~TBusSessionImpl() override;
  77. void Shutdown() override;
  78. bool IsDown();
  79. size_t GetInFlightImpl(const TNetAddr& addr) const;
  80. size_t GetConnectSyscallsNumForTestImpl(const TNetAddr& addr) const;
  81. void GetInFlightBulk(TArrayRef<const TNetAddr> addrs, TArrayRef<size_t> results) const override;
  82. void GetConnectSyscallsNumBulkForTest(TArrayRef<const TNetAddr> addrs, TArrayRef<size_t> results) const override;
  83. virtual void FillStatus();
  84. TSessionDumpStatus GetStatusRecordInternal() override;
  85. TString GetStatus(ui16 flags = YBUS_STATUS_CONNS) override;
  86. TConnectionStatusMonRecord GetStatusProtobuf() override;
  87. TString GetStatusSingleLine() override;
  88. void ProcessItem(TStatusTag, TDeadConnectionTag, const TRemoteConnectionWriterIncrementalStatus&);
  89. void ProcessItem(TStatusTag, TDeadConnectionTag, const TRemoteConnectionReaderIncrementalStatus&);
  90. void ProcessItem(TStatusTag, TDeadConnectionTag, const TAcceptorStatus&);
  91. void ProcessItem(TStatusTag, ::NActor::TDefaultTag, const TAcceptorStatus&);
  92. void ProcessItem(TConnectionTag, ::NActor::TDefaultTag, const TOnAccept&);
  93. void ProcessItem(TConnectionTag, TRemoveTag, TRemoteConnectionPtr);
  94. void ProcessConnectionsAcceptorsShapshotQueueItem(TAtomicSharedPtr<TConnectionsAcceptorsSnapshot>);
  95. void StatusUpdateCachedDump();
  96. void StatusUpdateCachedDumpIfNecessary(TInstant now);
  97. void Act(TStatusTag);
  98. void Act(TConnectionTag);
  99. TBusProtocol* GetProto() const noexcept override;
  100. const TBusSessionConfig* GetConfig() const noexcept override;
  101. TBusMessageQueue* GetQueue() const noexcept override;
  102. TString GetNameInternal() override;
  103. virtual void OnMessageReceived(TRemoteConnection* c, TVectorSwaps<TBusMessagePtrAndHeader>& newMsg) = 0;
  104. void Listen(int port, TBusMessageQueue* q);
  105. void Listen(const TVector<TBindResult>& bindTo, TBusMessageQueue* q);
  106. TBusConnection* Accept(SOCKET listen);
  107. inline ::NActor::TActor<TBusSessionImpl, TStatusTag>* GetStatusActor() {
  108. return this;
  109. }
  110. inline ::NActor::TActor<TBusSessionImpl, TConnectionTag>* GetConnectionsActor() {
  111. return this;
  112. }
  113. typedef THashMap<TBusSocketAddr, TRemoteConnectionPtr> TAddrRemoteConnections;
  114. void SendSnapshotToStatusActor();
  115. void InsertConnectionLockAcquired(TRemoteConnection* connection);
  116. void InsertAcceptorLockAcquired(TAcceptor* acceptor);
  117. void GetConnections(TVector<TRemoteConnectionPtr>*);
  118. void GetAcceptors(TVector<TAcceptorPtr>*);
  119. void GetConnectionsLockAquired(TVector<TRemoteConnectionPtr>*);
  120. void GetAcceptorsLockAquired(TVector<TAcceptorPtr>*);
  121. TRemoteConnectionPtr GetConnection(const TBusSocketAddr& addr, bool create);
  122. TRemoteConnectionPtr GetConnectionById(ui64 id);
  123. TAcceptorPtr GetAcceptorById(ui64 id);
  124. void InvokeOnError(TNonDestroyingAutoPtr<TBusMessage>, EMessageStatus);
  125. void Cron();
  126. TBusSessionJobCount JobCount;
  127. // TODO: replace with actor
  128. TMutex ConnectionsLock;
  129. struct TImpl;
  130. THolder<TImpl> Impl;
  131. const bool IsSource_;
  132. TBusMessageQueue* const Queue;
  133. TBusProtocol* const Proto;
  134. // copied to be available after Proto dies
  135. const TString ProtoName;
  136. IBusErrorHandler* const ErrorHandler;
  137. TUseCountHolder HandlerUseCountHolder;
  138. TBusSessionConfig Config; // TODO: make const
  139. NEventLoop::TEventLoop WriteEventLoop;
  140. NEventLoop::TEventLoop ReadEventLoop;
  141. THolder<NThreading::TLegacyFuture<void, false>> ReadEventLoopThread;
  142. THolder<NThreading::TLegacyFuture<void, false>> WriteEventLoopThread;
  143. THashMap<ui64, TRemoteConnectionPtr> ConnectionsById;
  144. TAddrRemoteConnections Connections;
  145. TAcceptorsPtrs Acceptors;
  146. struct TStatusData {
  147. TAtomicSharedPtr<TConnectionsAcceptorsSnapshot> ConnectionsAcceptorsSnapshot;
  148. ::NActor::TQueueForActor<TAtomicSharedPtr<TConnectionsAcceptorsSnapshot>> ConnectionsAcceptorsSnapshotsQueue;
  149. TAtomicShutdownState ShutdownState;
  150. TBusSessionStatus Status;
  151. TSessionDumpStatus StatusDumpCached;
  152. TMutex StatusDumpCachedMutex;
  153. TInstant StatusDumpCachedLastUpdate;
  154. TStatusData();
  155. };
  156. TStatusData StatusData;
  157. struct TConnectionsData {
  158. TAtomicShutdownState ShutdownState;
  159. TConnectionsData();
  160. };
  161. TConnectionsData ConnectionsData;
  162. ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionWriterIncrementalStatus,
  163. TStatusTag, TDeadConnectionTag>*
  164. GetDeadConnectionWriterStatusQueue() {
  165. return this;
  166. }
  167. ::NActor::TQueueInActor<TBusSessionImpl, TRemoteConnectionReaderIncrementalStatus,
  168. TStatusTag, TDeadConnectionTag>*
  169. GetDeadConnectionReaderStatusQueue() {
  170. return this;
  171. }
  172. ::NActor::TQueueInActor<TBusSessionImpl, TAcceptorStatus,
  173. TStatusTag, TDeadConnectionTag>*
  174. GetDeadAcceptorStatusQueue() {
  175. return this;
  176. }
  177. template <typename TItem>
  178. ::NActor::IQueueInActor<TItem>* GetQueue() {
  179. return this;
  180. }
  181. ui64 LastAcceptorId;
  182. ui64 LastConnectionId;
  183. TAtomic Down;
  184. TSystemEvent ShutdownCompleteEvent;
  185. };
  186. inline TBusProtocol* TBusSessionImpl::GetProto() const noexcept {
  187. return Proto;
  188. }
  189. inline const TBusSessionConfig* TBusSessionImpl::GetConfig() const noexcept {
  190. return &Config;
  191. }
  192. inline TBusMessageQueue* TBusSessionImpl::GetQueue() const noexcept {
  193. return Queue;
  194. }
  195. }
  196. }