interconnect.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. #pragma once
  2. #include "events.h"
  3. #include "event_local.h"
  4. #include <library/cpp/actors/protos/interconnect.pb.h>
  5. #include <util/string/cast.h>
  6. #include <util/string/builder.h>
  7. namespace NActors {
  8. class TNodeLocation {
  9. public:
  10. struct TKeys {
  11. enum E : int {
  12. DataCenter = 10,
  13. Module = 20,
  14. Rack = 30,
  15. Unit = 40,
  16. };
  17. };
  18. struct TLegacyValue {
  19. ui32 DataCenter;
  20. ui32 Room;
  21. ui32 Rack;
  22. ui32 Body;
  23. auto ConvertToTuple() const { return std::make_tuple(DataCenter, Room, Rack, Body); }
  24. int Compare(const TLegacyValue& other) const {
  25. const auto x = ConvertToTuple();
  26. const auto y = other.ConvertToTuple();
  27. if (x < y) {
  28. return -1;
  29. } else if (y < x) {
  30. return 1;
  31. } else {
  32. return 0;
  33. }
  34. }
  35. friend bool operator ==(const TLegacyValue& x, const TLegacyValue& y) { return x.Compare(y) == 0; }
  36. void Serialize(NActorsInterconnect::TNodeLocation *pb) const {
  37. pb->SetDataCenterNum(DataCenter);
  38. pb->SetRoomNum(Room);
  39. pb->SetRackNum(Rack);
  40. pb->SetBodyNum(Body);
  41. }
  42. };
  43. private:
  44. std::optional<TLegacyValue> LegacyValue;
  45. std::vector<std::pair<TKeys::E, TString>> Items;
  46. public:
  47. // generic ctors
  48. TNodeLocation() = default;
  49. TNodeLocation(const TNodeLocation&) = default;
  50. TNodeLocation(TNodeLocation&&) = default;
  51. TNodeLocation(const TString& DataCenter, const TString& Module = "", const TString& Rack = "", const TString& Unit = "");
  52. // protobuf-parser ctor
  53. explicit TNodeLocation(const NActorsInterconnect::TNodeLocation& location);
  54. // serialized protobuf ctor
  55. static constexpr struct TFromSerialized {} FromSerialized {};
  56. TNodeLocation(TFromSerialized, const TString& s);
  57. // parser helper function
  58. static NActorsInterconnect::TNodeLocation ParseLocation(const TString& s);
  59. // assignment operators
  60. TNodeLocation& operator =(const TNodeLocation&) = default;
  61. TNodeLocation& operator =(TNodeLocation&&) = default;
  62. // compatibleWithOlderVersions should be set to true when this protobuf is possibly going to be delivered to 21-4
  63. void Serialize(NActorsInterconnect::TNodeLocation *pb, bool compatibleWithOlderVersions) const;
  64. TString GetSerializedLocation() const;
  65. TString GetDataCenterId() const { return ToStringUpTo(TKeys::DataCenter); }
  66. TString GetModuleId() const { return ToStringUpTo(TKeys::Module); }
  67. TString GetRackId() const { return ToStringUpTo(TKeys::Rack); }
  68. TString GetUnitId() const { return ToStringUpTo(TKeys::Unit); }
  69. TString ToString() const { return ToStringUpTo(TKeys::E(Max<int>())); }
  70. TString ToStringUpTo(TKeys::E upToKey) const;
  71. TLegacyValue GetLegacyValue() const;
  72. const std::vector<std::pair<TKeys::E, TString>>& GetItems() const { return Items; }
  73. bool HasKey(TKeys::E key) const {
  74. auto comp = [](const auto& p, TKeys::E value) { return p.first < value; };
  75. const auto it = std::lower_bound(Items.begin(), Items.end(), key, comp);
  76. return it != Items.end() && it->first == key;
  77. }
  78. int Compare(const TNodeLocation& other) const {
  79. if (LegacyValue || other.LegacyValue) {
  80. return GetLegacyValue().Compare(other.GetLegacyValue());
  81. } else if (Items < other.Items) {
  82. return -1;
  83. } else if (other.Items < Items) {
  84. return 1;
  85. } else {
  86. return 0;
  87. }
  88. }
  89. void InheritLegacyValue(const TNodeLocation& other) {
  90. LegacyValue = other.GetLegacyValue();
  91. }
  92. friend bool operator ==(const TNodeLocation& x, const TNodeLocation& y) { return x.Compare(y) == 0; }
  93. friend bool operator !=(const TNodeLocation& x, const TNodeLocation& y) { return x.Compare(y) != 0; }
  94. friend bool operator < (const TNodeLocation& x, const TNodeLocation& y) { return x.Compare(y) < 0; }
  95. friend bool operator <=(const TNodeLocation& x, const TNodeLocation& y) { return x.Compare(y) <= 0; }
  96. friend bool operator > (const TNodeLocation& x, const TNodeLocation& y) { return x.Compare(y) > 0; }
  97. friend bool operator >=(const TNodeLocation& x, const TNodeLocation& y) { return x.Compare(y) >= 0; }
  98. };
  99. struct TEvInterconnect {
  100. enum EEv {
  101. EvForward = EventSpaceBegin(TEvents::ES_INTERCONNECT),
  102. EvResolveNode, // resolve info about node (internal)
  103. EvNodeAddress, // node info (internal)
  104. EvConnectNode, // request proxy to establish connection (like: we would send something there soon)
  105. EvAcceptIncoming,
  106. EvNodeConnected, // node connected notify
  107. EvNodeDisconnected, // node disconnected notify
  108. EvRegisterNode,
  109. EvRegisterNodeResult,
  110. EvListNodes,
  111. EvNodesInfo,
  112. EvDisconnect,
  113. EvGetNode,
  114. EvNodeInfo,
  115. EvClosePeerSocket,
  116. EvCloseInputSession,
  117. EvPoisonSession,
  118. EvTerminate,
  119. EvEnd
  120. };
  121. enum ESubscribes {
  122. SubConnected,
  123. SubDisconnected,
  124. };
  125. static_assert(EvEnd < EventSpaceEnd(TEvents::ES_INTERCONNECT), "expect EvEnd < EventSpaceEnd(TEvents::ES_INTERCONNECT)");
  126. struct TEvResolveNode;
  127. struct TEvNodeAddress;
  128. struct TEvConnectNode: public TEventBase<TEvConnectNode, EvConnectNode> {
  129. DEFINE_SIMPLE_LOCAL_EVENT(TEvConnectNode, "TEvInterconnect::TEvConnectNode")
  130. };
  131. struct TEvAcceptIncoming;
  132. struct TEvNodeConnected: public TEventLocal<TEvNodeConnected, EvNodeConnected> {
  133. DEFINE_SIMPLE_LOCAL_EVENT(TEvNodeConnected, "TEvInterconnect::TEvNodeConnected")
  134. TEvNodeConnected(ui32 node) noexcept
  135. : NodeId(node)
  136. {
  137. }
  138. const ui32 NodeId;
  139. };
  140. struct TEvNodeDisconnected: public TEventLocal<TEvNodeDisconnected, EvNodeDisconnected> {
  141. DEFINE_SIMPLE_LOCAL_EVENT(TEvNodeDisconnected, "TEvInterconnect::TEvNodeDisconnected")
  142. TEvNodeDisconnected(ui32 node) noexcept
  143. : NodeId(node)
  144. {
  145. }
  146. const ui32 NodeId;
  147. };
  148. struct TEvRegisterNode;
  149. struct TEvRegisterNodeResult;
  150. struct TEvListNodes: public TEventLocal<TEvListNodes, EvListNodes> {
  151. const bool SubscribeToStaticNodeChanges = false;
  152. TEvListNodes() = default;
  153. TEvListNodes(bool subscribeToStaticNodeChanges)
  154. : SubscribeToStaticNodeChanges(subscribeToStaticNodeChanges)
  155. {}
  156. };
  157. struct TNodeInfo {
  158. ui32 NodeId;
  159. TString Address;
  160. TString Host;
  161. TString ResolveHost;
  162. ui16 Port;
  163. TNodeLocation Location;
  164. bool IsStatic = true;
  165. TNodeInfo() = default;
  166. TNodeInfo(const TNodeInfo&) = default;
  167. TNodeInfo& operator =(const TNodeInfo&) = default;
  168. TNodeInfo(ui32 nodeId,
  169. const TString& address,
  170. const TString& host,
  171. const TString& resolveHost,
  172. ui16 port,
  173. const TNodeLocation& location,
  174. bool isStatic = true)
  175. : NodeId(nodeId)
  176. , Address(address)
  177. , Host(host)
  178. , ResolveHost(resolveHost)
  179. , Port(port)
  180. , Location(location)
  181. , IsStatic(isStatic)
  182. {
  183. }
  184. operator ui32() const {
  185. return NodeId;
  186. }
  187. };
  188. struct TEvNodesInfo: public TEventLocal<TEvNodesInfo, EvNodesInfo> {
  189. TVector<TNodeInfo> Nodes;
  190. const TNodeInfo* GetNodeInfo(ui32 nodeId) const {
  191. for (const auto& x : Nodes) {
  192. if (x.NodeId == nodeId)
  193. return &x;
  194. }
  195. return nullptr;
  196. }
  197. };
  198. struct TEvDisconnect;
  199. struct TEvGetNode: public TEventLocal<TEvGetNode, EvGetNode> {
  200. ui32 NodeId;
  201. TInstant Deadline;
  202. TEvGetNode(ui32 nodeId, TInstant deadline = TInstant::Max())
  203. : NodeId(nodeId)
  204. , Deadline(deadline)
  205. {
  206. }
  207. };
  208. struct TEvNodeInfo: public TEventLocal<TEvNodeInfo, EvNodeInfo> {
  209. TEvNodeInfo(ui32 nodeId)
  210. : NodeId(nodeId)
  211. {
  212. }
  213. ui32 NodeId;
  214. THolder<TNodeInfo> Node;
  215. };
  216. struct TEvClosePeerSocket : TEventLocal<TEvClosePeerSocket, EvClosePeerSocket> {};
  217. struct TEvCloseInputSession : TEventLocal<TEvCloseInputSession, EvCloseInputSession> {};
  218. struct TEvPoisonSession : TEventLocal<TEvPoisonSession, EvPoisonSession> {};
  219. struct TEvTerminate : TEventLocal<TEvTerminate, EvTerminate> {};
  220. };
  221. }