aboutsummaryrefslogtreecommitdiffstats
path: root/library/cpp/actors/core/interconnect.h
blob: 46d4fd5303324bfdee6ac5d97abddbddf3531b39 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#pragma once

#include "events.h"
#include "event_local.h"
#include <library/cpp/actors/protos/interconnect.pb.h>
#include <util/string/cast.h>
#include <util/string/builder.h>

namespace NActors {
    class TNodeLocation {
    public:
        struct TKeys {
            enum E : int {
                DataCenter = 10,
                Module = 20,
                Rack = 30,
                Unit = 40,
            };
        };

        struct TLegacyValue {
            ui32 DataCenter;
            ui32 Room;
            ui32 Rack;
            ui32 Body;

            auto ConvertToTuple() const { return std::make_tuple(DataCenter, Room, Rack, Body); }

            int Compare(const TLegacyValue& other) const {
                const auto x = ConvertToTuple();
                const auto y = other.ConvertToTuple();
                if (x < y) {
                    return -1;
                } else if (y < x) {
                    return 1;
                } else {
                    return 0;
                }
            }

            friend bool operator ==(const TLegacyValue& x, const TLegacyValue& y) { return x.Compare(y) == 0; }

            void Serialize(NActorsInterconnect::TNodeLocation *pb) const {
                pb->SetDataCenterNum(DataCenter);
                pb->SetRoomNum(Room);
                pb->SetRackNum(Rack);
                pb->SetBodyNum(Body);
            }
        };

    private:
        std::optional<TLegacyValue> LegacyValue;
        std::vector<std::pair<TKeys::E, TString>> Items;

    public:
        // generic ctors
        TNodeLocation() = default;
        TNodeLocation(const TNodeLocation&) = default;
        TNodeLocation(TNodeLocation&&) = default;
        TNodeLocation(const TString& DataCenter, const TString& Module = "", const TString& Rack = "", const TString& Unit = "");

        // protobuf-parser ctor
        explicit TNodeLocation(const NActorsInterconnect::TNodeLocation& location);

        // serialized protobuf ctor
        static constexpr struct TFromSerialized {} FromSerialized {};
        TNodeLocation(TFromSerialized, const TString& s);

        // parser helper function
        static NActorsInterconnect::TNodeLocation ParseLocation(const TString& s);

        // assignment operators
        TNodeLocation& operator =(const TNodeLocation&) = default;
        TNodeLocation& operator =(TNodeLocation&&) = default;

        // compatibleWithOlderVersions should be set to true when this protobuf is possibly going to be delivered to 21-4
        void Serialize(NActorsInterconnect::TNodeLocation *pb, bool compatibleWithOlderVersions) const;
        TString GetSerializedLocation() const;

        TString GetDataCenterId() const { return ToStringUpTo(TKeys::DataCenter); }
        TString GetModuleId() const { return ToStringUpTo(TKeys::Module); }
        TString GetRackId() const { return ToStringUpTo(TKeys::Rack); }
        TString GetUnitId() const { return ToStringUpTo(TKeys::Unit); }
        TString ToString() const { return ToStringUpTo(TKeys::E(Max<int>())); }
        TString ToStringUpTo(TKeys::E upToKey) const;

        TLegacyValue GetLegacyValue() const;

        const std::vector<std::pair<TKeys::E, TString>>& GetItems() const { return Items; }

        bool HasKey(TKeys::E key) const {
            auto comp = [](const auto& p, TKeys::E value) { return p.first < value; };
            const auto it = std::lower_bound(Items.begin(), Items.end(), key, comp);
            return it != Items.end() && it->first == key;
        }

        int Compare(const TNodeLocation& other) const {
            if (LegacyValue || other.LegacyValue) {
                return GetLegacyValue().Compare(other.GetLegacyValue());
            } else if (Items < other.Items) {
                return -1;
            } else if (other.Items < Items) {
                return 1;
            } else {
                return 0;
            }
        }

        void InheritLegacyValue(const TNodeLocation& other) {
            LegacyValue = other.GetLegacyValue();
        }

        friend bool operator ==(const TNodeLocation& x, const TNodeLocation& y) { return x.Compare(y) == 0; }
        friend bool operator !=(const TNodeLocation& x, const TNodeLocation& y) { return x.Compare(y) != 0; }
        friend bool operator < (const TNodeLocation& x, const TNodeLocation& y) { return x.Compare(y) <  0; }
        friend bool operator <=(const TNodeLocation& x, const TNodeLocation& y) { return x.Compare(y) <= 0; }
        friend bool operator > (const TNodeLocation& x, const TNodeLocation& y) { return x.Compare(y) >  0; }
        friend bool operator >=(const TNodeLocation& x, const TNodeLocation& y) { return x.Compare(y) >= 0; }
    };

    struct TEvInterconnect {
        enum EEv {
            EvForward = EventSpaceBegin(TEvents::ES_INTERCONNECT),
            EvResolveNode, // resolve info about node (internal)
            EvNodeAddress, // node info (internal)
            EvConnectNode, // request proxy to establish connection (like: we would send something there soon)
            EvAcceptIncoming,
            EvNodeConnected,    // node connected notify
            EvNodeDisconnected, // node disconnected notify
            EvRegisterNode,
            EvRegisterNodeResult,
            EvListNodes,
            EvNodesInfo,
            EvDisconnect,
            EvGetNode,
            EvNodeInfo,
            EvClosePeerSocket,
            EvCloseInputSession,
            EvPoisonSession,
            EvTerminate,
            EvEnd
        };

        enum ESubscribes {
            SubConnected,
            SubDisconnected,
        };

        static_assert(EvEnd < EventSpaceEnd(TEvents::ES_INTERCONNECT), "expect EvEnd < EventSpaceEnd(TEvents::ES_INTERCONNECT)");

        struct TEvResolveNode;
        struct TEvNodeAddress;

        struct TEvConnectNode: public TEventBase<TEvConnectNode, EvConnectNode> {
            DEFINE_SIMPLE_LOCAL_EVENT(TEvConnectNode, "TEvInterconnect::TEvConnectNode")
        };

        struct TEvAcceptIncoming;

        struct TEvNodeConnected: public TEventLocal<TEvNodeConnected, EvNodeConnected> {
            DEFINE_SIMPLE_LOCAL_EVENT(TEvNodeConnected, "TEvInterconnect::TEvNodeConnected")
            TEvNodeConnected(ui32 node) noexcept
                : NodeId(node)
            {
            }
            const ui32 NodeId;
        };

        struct TEvNodeDisconnected: public TEventLocal<TEvNodeDisconnected, EvNodeDisconnected> {
            DEFINE_SIMPLE_LOCAL_EVENT(TEvNodeDisconnected, "TEvInterconnect::TEvNodeDisconnected")
            TEvNodeDisconnected(ui32 node) noexcept
                : NodeId(node)
            {
            }
            const ui32 NodeId;
        };

        struct TEvRegisterNode;
        struct TEvRegisterNodeResult;

        struct TEvListNodes: public TEventLocal<TEvListNodes, EvListNodes> {
            const bool SubscribeToStaticNodeChanges = false;

            TEvListNodes() = default;

            TEvListNodes(bool subscribeToStaticNodeChanges)
                : SubscribeToStaticNodeChanges(subscribeToStaticNodeChanges)
            {}
        };

        struct TNodeInfo {
            ui32 NodeId;
            TString Address;
            TString Host;
            TString ResolveHost;
            ui16 Port;
            TNodeLocation Location;
            bool IsStatic = true;

            TNodeInfo() = default;
            TNodeInfo(const TNodeInfo&) = default;
            TNodeInfo& operator =(const TNodeInfo&) = default;
            TNodeInfo(ui32 nodeId,
                      const TString& address,
                      const TString& host,
                      const TString& resolveHost,
                      ui16 port,
                      const TNodeLocation& location,
                      bool isStatic = true)
                : NodeId(nodeId)
                , Address(address)
                , Host(host)
                , ResolveHost(resolveHost)
                , Port(port)
                , Location(location)
                , IsStatic(isStatic)
            {
            }

            operator ui32() const {
                return NodeId;
            }
        };

        struct TEvNodesInfo: public TEventLocal<TEvNodesInfo, EvNodesInfo> {
            TVector<TNodeInfo> Nodes;

            const TNodeInfo* GetNodeInfo(ui32 nodeId) const {
                for (const auto& x : Nodes) {
                    if (x.NodeId == nodeId)
                        return &x;
                }
                return nullptr;
            }
        };

        struct TEvDisconnect;

        struct TEvGetNode: public TEventLocal<TEvGetNode, EvGetNode> {
            ui32 NodeId;
            TInstant Deadline;

            TEvGetNode(ui32 nodeId, TInstant deadline = TInstant::Max())
                : NodeId(nodeId)
                , Deadline(deadline)
            {
            }
        };

        struct TEvNodeInfo: public TEventLocal<TEvNodeInfo, EvNodeInfo> {
            TEvNodeInfo(ui32 nodeId)
                : NodeId(nodeId)
            {
            }

            ui32 NodeId;
            THolder<TNodeInfo> Node;
        };

        struct TEvClosePeerSocket : TEventLocal<TEvClosePeerSocket, EvClosePeerSocket> {};

        struct TEvCloseInputSession : TEventLocal<TEvCloseInputSession, EvCloseInputSession> {};

        struct TEvPoisonSession : TEventLocal<TEvPoisonSession, EvPoisonSession> {};

        struct TEvTerminate : TEventLocal<TEvTerminate, EvTerminate> {};
    };
}