events.h 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. #pragma once
  2. #include "event.h"
  3. #include "event_pb.h"
  4. #include <library/cpp/actors/protos/actors.pb.h>
  5. #include <util/system/unaligned_mem.h>
  6. namespace NActors {
  7. struct TEvents {
  8. enum EEventSpace {
  9. ES_HELLOWORLD = 0,
  10. ES_SYSTEM = 1,
  11. ES_INTERCONNECT = 2,
  12. ES_INTERCONNECT_MSGBUS = 3,
  13. ES_DNS = 4,
  14. ES_SOCKET_POLLER = 5,
  15. ES_LOGGER = 6,
  16. ES_MON = 7,
  17. ES_INTERCONNECT_TCP = 8,
  18. ES_PROFILER = 9,
  19. ES_YF = 10,
  20. ES_HTTP = 11,
  21. ES_PGWIRE = 12,
  22. ES_USERSPACE = 4096,
  23. ES_PRIVATE = (1 << 15) - 16,
  24. ES_MAX = (1 << 15),
  25. };
  26. #define EventSpaceBegin(eventSpace) (eventSpace << 16u)
  27. #define EventSpaceEnd(eventSpace) ((eventSpace << 16u) + (1u << 16u))
  28. struct THelloWorld {
  29. enum {
  30. Start = EventSpaceBegin(ES_HELLOWORLD),
  31. Ping,
  32. Pong,
  33. Blob,
  34. End
  35. };
  36. static_assert(End < EventSpaceEnd(ES_HELLOWORLD), "expect End < EventSpaceEnd(ES_HELLOWORLD)");
  37. };
  38. struct TEvPing: public TEventBase<TEvPing, THelloWorld::Ping> {
  39. DEFINE_SIMPLE_NONLOCAL_EVENT(TEvPing, "HelloWorld: Ping");
  40. };
  41. struct TEvPong: public TEventBase<TEvPong, THelloWorld::Pong> {
  42. DEFINE_SIMPLE_NONLOCAL_EVENT(TEvPong, "HelloWorld: Pong");
  43. };
  44. struct TEvBlob: public TEventBase<TEvBlob, THelloWorld::Blob> {
  45. const TString Blob;
  46. TEvBlob(const TString& blob) noexcept
  47. : Blob(blob)
  48. {
  49. }
  50. TString ToStringHeader() const noexcept override {
  51. return "THelloWorld::Blob";
  52. }
  53. bool SerializeToArcadiaStream(TChunkSerializer *serializer) const override {
  54. return serializer->WriteString(&Blob);
  55. }
  56. static IEventBase* Load(TEventSerializedData* bufs) noexcept {
  57. return new TEvBlob(bufs->GetString());
  58. }
  59. bool IsSerializable() const override {
  60. return true;
  61. }
  62. };
  63. struct TSystem {
  64. enum {
  65. Start = EventSpaceBegin(ES_SYSTEM),
  66. Bootstrap, // generic bootstrap event
  67. Wakeup, // generic timeout
  68. Subscribe, // generic subscribe to something
  69. Unsubscribe, // generic unsubscribe from something
  70. Delivered, // event delivered
  71. Undelivered, // event undelivered
  72. Poison, // request actor to shutdown
  73. Completed, // generic async job result event
  74. PoisonTaken, // generic Poison taken (reply to PoisonPill event, i.e. died completely)
  75. FlushLog,
  76. CallbackCompletion,
  77. CallbackException,
  78. Gone, // Generic notification of actor death
  79. TrackActor,
  80. UntrackActor,
  81. InvokeResult,
  82. CoroTimeout,
  83. InvokeQuery,
  84. Wilson,
  85. End,
  86. // Compatibility section
  87. PoisonPill = Poison,
  88. ActorDied = Gone,
  89. };
  90. static_assert(End < EventSpaceEnd(ES_SYSTEM), "expect End < EventSpaceEnd(ES_SYSTEM)");
  91. };
  92. struct TEvBootstrap: public TEventBase<TEvBootstrap, TSystem::Bootstrap> {
  93. DEFINE_SIMPLE_LOCAL_EVENT(TEvBootstrap, "System: TEvBootstrap")
  94. };
  95. struct TEvPoison : public TEventBase<TEvPoison, TSystem::Poison> {
  96. DEFINE_SIMPLE_NONLOCAL_EVENT(TEvPoison, "System: TEvPoison")
  97. };
  98. struct TEvWakeup: public TEventBase<TEvWakeup, TSystem::Wakeup> {
  99. DEFINE_SIMPLE_LOCAL_EVENT(TEvWakeup, "System: TEvWakeup")
  100. TEvWakeup(ui64 tag = 0) : Tag(tag) { }
  101. const ui64 Tag = 0;
  102. };
  103. struct TEvSubscribe: public TEventBase<TEvSubscribe, TSystem::Subscribe> {
  104. DEFINE_SIMPLE_LOCAL_EVENT(TEvSubscribe, "System: TEvSubscribe")
  105. };
  106. struct TEvUnsubscribe: public TEventBase<TEvUnsubscribe, TSystem::Unsubscribe> {
  107. DEFINE_SIMPLE_LOCAL_EVENT(TEvUnsubscribe, "System: TEvUnsubscribe")
  108. };
  109. struct TEvUndelivered: public TEventBase<TEvUndelivered, TSystem::Undelivered> {
  110. enum EReason {
  111. ReasonUnknown,
  112. ReasonActorUnknown,
  113. Disconnected
  114. };
  115. const ui32 SourceType;
  116. const EReason Reason;
  117. const bool Unsure;
  118. const TString Data;
  119. TEvUndelivered(ui32 sourceType, ui32 reason, bool unsure = false)
  120. : SourceType(sourceType)
  121. , Reason(static_cast<EReason>(reason))
  122. , Unsure(unsure)
  123. , Data(MakeData(sourceType, reason))
  124. {}
  125. TString ToStringHeader() const override;
  126. bool SerializeToArcadiaStream(TChunkSerializer *serializer) const override;
  127. static IEventBase* Load(TEventSerializedData* bufs);
  128. bool IsSerializable() const override;
  129. ui32 CalculateSerializedSize() const override { return 2 * sizeof(ui32); }
  130. static void Out(IOutputStream& o, EReason x);
  131. private:
  132. static TString MakeData(ui32 sourceType, ui32 reason) {
  133. TString s = TString::Uninitialized(sizeof(ui32) + sizeof(ui32));
  134. char *p = s.Detach();
  135. WriteUnaligned<ui32>(p + 0, sourceType);
  136. WriteUnaligned<ui32>(p + 4, reason);
  137. return s;
  138. }
  139. };
  140. struct TEvCompleted: public TEventBase<TEvCompleted, TSystem::Completed> {
  141. const ui32 Id;
  142. const ui32 Status;
  143. TEvCompleted(ui32 id = 0, ui32 status = 0)
  144. : Id(id)
  145. , Status(status)
  146. {
  147. }
  148. DEFINE_SIMPLE_LOCAL_EVENT(TEvCompleted, "System: TEvCompleted")
  149. };
  150. struct TEvPoisonTaken: public TEventBase<TEvPoisonTaken, TSystem::PoisonTaken> {
  151. DEFINE_SIMPLE_LOCAL_EVENT(TEvPoisonTaken, "System: TEvPoisonTaken")
  152. };
  153. struct TEvFlushLog: public TEventBase<TEvFlushLog, TSystem::FlushLog> {
  154. DEFINE_SIMPLE_LOCAL_EVENT(TEvFlushLog, "System: TEvFlushLog")
  155. };
  156. struct TEvCallbackException: public TEventPB<TEvCallbackException,
  157. NActorsProto::TCallbackException,
  158. TSystem::CallbackException> {
  159. TEvCallbackException(const TActorId& id, const TString& msg) {
  160. ActorIdToProto(id, Record.MutableActorId());
  161. Record.SetExceptionMessage(msg);
  162. }
  163. };
  164. struct TEvCallbackCompletion: public TEventPB<TEvCallbackCompletion,
  165. NActorsProto::TActorId,
  166. TSystem::CallbackCompletion> {
  167. TEvCallbackCompletion(const TActorId& id) {
  168. ActorIdToProto(id, &Record);
  169. }
  170. };
  171. struct TEvGone: public TEventBase<TEvGone, TSystem::Gone> {
  172. DEFINE_SIMPLE_LOCAL_EVENT(TEvGone, "System: TEvGone")
  173. };
  174. struct TEvInvokeResult;
  175. using TEvPoisonPill = TEvPoison; // Legacy name, deprecated
  176. using TEvActorDied = TEvGone;
  177. };
  178. }
  179. template <>
  180. inline void Out<NActors::TEvents::TEvUndelivered::EReason>(IOutputStream& o, NActors::TEvents::TEvUndelivered::EReason x) {
  181. NActors::TEvents::TEvUndelivered::Out(o, x);
  182. }