events.h 7.6 KB

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