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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
#include "ic_mock.h"
#include <library/cpp/actors/core/interconnect.h>
#include <util/system/yield.h>
#include <thread>
namespace NActors {
class TInterconnectMock::TImpl {
enum {
EvInject = EventSpaceBegin(TEvents::ES_PRIVATE),
EvCheckSession,
EvRam,
};
struct TEvInject : TEventLocal<TEvInject, EvInject> {
std::deque<std::unique_ptr<IEventHandle>> Messages;
const TScopeId OriginScopeId;
const ui64 SenderSessionId;
TEvInject(std::deque<std::unique_ptr<IEventHandle>>&& messages, const TScopeId& originScopeId, ui64 senderSessionId)
: Messages(std::move(messages))
, OriginScopeId(originScopeId)
, SenderSessionId(senderSessionId)
{}
};
class TProxyMockActor;
class TConnectionState {
struct TPeerInfo {
TRWMutex Mutex;
TActorSystem *ActorSystem = nullptr;
TActorId ProxyId;
};
const ui64 Key;
TPeerInfo PeerInfo[2];
std::atomic_uint64_t SessionId = 0;
public:
TConnectionState(ui64 key)
: Key(key)
{}
void Attach(ui32 nodeId, TActorSystem *as, const TActorId& actorId) {
TPeerInfo *peer = GetPeer(nodeId);
auto guard = TWriteGuard(peer->Mutex);
Y_VERIFY(!peer->ActorSystem);
peer->ActorSystem = as;
peer->ProxyId = actorId;
as->DeferPreStop([peer] {
auto guard = TWriteGuard(peer->Mutex);
peer->ActorSystem = nullptr;
});
}
void Inject(ui32 peerNodeId, std::deque<std::unique_ptr<IEventHandle>>&& messages,
const TScopeId& originScopeId, ui64 senderSessionId) {
TPeerInfo *peer = GetPeer(peerNodeId);
auto guard = TReadGuard(peer->Mutex);
if (peer->ActorSystem) {
peer->ActorSystem->Send(new IEventHandle(peer->ProxyId, TActorId(), new TEvInject(std::move(messages),
originScopeId, senderSessionId)));
} else {
for (auto&& ev : messages) {
TActivationContext::Send(ev->ForwardOnNondelivery(TEvents::TEvUndelivered::Disconnected));
}
}
}
ui64 GetValidSessionId() const {
return SessionId;
}
void InvalidateSessionId(ui32 peerNodeId) {
++SessionId;
TPeerInfo *peer = GetPeer(peerNodeId);
auto guard = TReadGuard(peer->Mutex);
if (peer->ActorSystem) {
peer->ActorSystem->Send(new IEventHandle(EvCheckSession, 0, peer->ProxyId, {}, nullptr, 0));
}
}
private:
TPeerInfo *GetPeer(ui32 nodeId) {
if (nodeId == ui32(Key)) {
return PeerInfo;
} else if (nodeId == ui32(Key >> 32)) {
return PeerInfo + 1;
} else {
Y_FAIL();
}
}
};
class TProxyMockActor : public TActor<TProxyMockActor> {
class TSessionMockActor : public TActor<TSessionMockActor> {
std::map<TActorId, ui64> Subscribers;
TProxyMockActor* const Proxy;
std::deque<std::unique_ptr<IEventHandle>> Queue;
public:
const ui64 SessionId;
public:
TSessionMockActor(TProxyMockActor *proxy, ui64 sessionId)
: TActor(&TThis::StateFunc)
, Proxy(proxy)
, SessionId(sessionId)
{}
void Terminate() {
for (auto&& ev : std::exchange(Queue, {})) {
TActivationContext::Send(ev->ForwardOnNondelivery(TEvents::TEvUndelivered::Disconnected));
}
for (const auto& kv : Subscribers) {
Send(kv.first, new TEvInterconnect::TEvNodeDisconnected(Proxy->PeerNodeId), 0, kv.second);
}
Y_VERIFY(Proxy->Session == this);
Proxy->Session = nullptr;
PassAway();
}
void HandleForward(TAutoPtr<IEventHandle> ev) {
if (ev->Flags & IEventHandle::FlagSubscribeOnSession) {
Subscribe(ev->Sender, ev->Cookie);
}
if (Queue.empty()) {
TActivationContext::Send(new IEventHandle(EvRam, 0, SelfId(), {}, {}, 0));
}
Queue.emplace_back(ev.Release());
}
void HandleRam() {
if (SessionId != Proxy->State.GetValidSessionId()) {
Terminate();
} else {
Proxy->PeerInject(std::exchange(Queue, {}));
}
}
void Handle(TEvInterconnect::TEvConnectNode::TPtr ev) {
Subscribe(ev->Sender, ev->Cookie);
}
void Handle(TEvents::TEvSubscribe::TPtr ev) {
Subscribe(ev->Sender, ev->Cookie);
}
void Handle(TEvents::TEvUnsubscribe::TPtr ev) {
Subscribers.erase(ev->Sender);
}
void HandlePoison() {
Proxy->Disconnect();
}
STRICT_STFUNC(StateFunc,
fFunc(TEvInterconnect::EvForward, HandleForward)
hFunc(TEvInterconnect::TEvConnectNode, Handle)
hFunc(TEvents::TEvSubscribe, Handle)
hFunc(TEvents::TEvUnsubscribe, Handle)
cFunc(TEvents::TSystem::Poison, HandlePoison)
cFunc(EvRam, HandleRam)
)
private:
void Subscribe(const TActorId& actorId, ui64 cookie) {
Subscribers[actorId] = cookie;
Send(actorId, new TEvInterconnect::TEvNodeConnected(Proxy->PeerNodeId), 0, cookie);
}
};
friend class TSessionMockActor;
const ui32 NodeId;
const ui32 PeerNodeId;
TConnectionState& State;
const TInterconnectProxyCommon::TPtr Common;
TSessionMockActor *Session = nullptr;
public:
TProxyMockActor(ui32 nodeId, ui32 peerNodeId, TConnectionState& state, TInterconnectProxyCommon::TPtr common)
: TActor(&TThis::StateFunc)
, NodeId(nodeId)
, PeerNodeId(peerNodeId)
, State(state)
, Common(std::move(common))
{}
void Registered(TActorSystem *as, const TActorId& parent) override {
TActor::Registered(as, parent);
State.Attach(NodeId, as, SelfId());
}
void Handle(TEvInject::TPtr ev) {
auto *msg = ev->Get();
if (Session && Session->SessionId != msg->SenderSessionId) {
return; // drop messages from other sessions
}
if (auto *session = GetSession()) {
for (auto&& ev : ev->Get()->Messages) {
auto fw = std::make_unique<IEventHandle>(
session->SelfId(),
ev->Type,
ev->Flags & ~IEventHandle::FlagForwardOnNondelivery,
ev->Recipient,
ev->Sender,
ev->ReleaseChainBuffer(),
ev->Cookie,
msg->OriginScopeId,
std::move(ev->TraceId)
);
if (!Common->EventFilter || Common->EventFilter->CheckIncomingEvent(*fw, Common->LocalScopeId)) {
TActivationContext::Send(fw.release());
}
}
}
}
void PassAway() override {
Disconnect();
TActor::PassAway();
}
TSessionMockActor *GetSession() {
CheckSession();
if (!Session) {
Session = new TSessionMockActor(this, State.GetValidSessionId());
RegisterWithSameMailbox(Session);
}
return Session;
}
void HandleSessionEvent(TAutoPtr<IEventHandle> ev) {
auto *session = GetSession();
InvokeOtherActor(*session, &TSessionMockActor::Receive, ev,
TActivationContext::ActorContextFor(session->SelfId()));
}
void Disconnect() {
State.InvalidateSessionId(PeerNodeId);
if (Session) {
Session->Terminate();
}
}
void CheckSession() {
if (Session && Session->SessionId != State.GetValidSessionId()) {
Session->Terminate();
}
}
void PeerInject(std::deque<std::unique_ptr<IEventHandle>>&& messages) {
Y_VERIFY(Session);
return State.Inject(PeerNodeId, std::move(messages), Common->LocalScopeId, Session->SessionId);
}
STRICT_STFUNC(StateFunc,
cFunc(TEvents::TSystem::Poison, PassAway)
fFunc(TEvInterconnect::EvForward, HandleSessionEvent)
fFunc(TEvInterconnect::EvConnectNode, HandleSessionEvent)
fFunc(TEvents::TSystem::Subscribe, HandleSessionEvent)
fFunc(TEvents::TSystem::Unsubscribe, HandleSessionEvent)
cFunc(TEvInterconnect::EvDisconnect, Disconnect)
IgnoreFunc(TEvInterconnect::TEvClosePeerSocket)
IgnoreFunc(TEvInterconnect::TEvCloseInputSession)
cFunc(TEvInterconnect::EvPoisonSession, Disconnect)
hFunc(TEvInject, Handle)
cFunc(EvCheckSession, CheckSession)
)
};
std::unordered_map<ui64, TConnectionState> States;
public:
IActor *CreateProxyMock(ui32 nodeId, ui32 peerNodeId, TInterconnectProxyCommon::TPtr common) {
Y_VERIFY(nodeId != peerNodeId);
Y_VERIFY(nodeId);
Y_VERIFY(peerNodeId);
const ui64 key = std::min(nodeId, peerNodeId) | ui64(std::max(nodeId, peerNodeId)) << 32;
auto it = States.try_emplace(key, key).first;
return new TProxyMockActor(nodeId, peerNodeId, it->second, std::move(common));
}
};
TInterconnectMock::TInterconnectMock()
: Impl(std::make_unique<TImpl>())
{}
TInterconnectMock::~TInterconnectMock()
{}
IActor *TInterconnectMock::CreateProxyMock(ui32 nodeId, ui32 peerNodeId, TInterconnectProxyCommon::TPtr common) {
return Impl->CreateProxyMock(nodeId, peerNodeId, std::move(common));
}
} // NActors
|