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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
|
#pragma once
#include "defs.h"
#include "actorid.h"
#include "callstack.h"
#include "event_load.h"
#include <library/cpp/actors/wilson/wilson_trace.h>
#include <util/system/hp_timer.h>
#include <util/generic/maybe.h>
namespace NActors {
class TChunkSerializer;
class IActor;
class ISerializerToStream {
public:
virtual bool SerializeToArcadiaStream(TChunkSerializer*) const = 0;
};
class IEventBase
: TNonCopyable,
public ISerializerToStream {
protected:
// for compatibility with virtual actors
virtual bool DoExecute(IActor* /*actor*/, std::unique_ptr<IEventHandle> /*eventPtr*/) {
Y_VERIFY_DEBUG(false);
return false;
}
public:
// actual typing is performed by IEventHandle
virtual ~IEventBase() {
}
bool Execute(IActor* actor, std::unique_ptr<IEventHandle> eventPtr) {
return DoExecute(actor, std::move(eventPtr));
}
virtual TString ToStringHeader() const = 0;
virtual TString ToString() const {
return ToStringHeader();
}
virtual ui32 CalculateSerializedSize() const {
return 0;
}
virtual ui32 Type() const = 0;
virtual bool SerializeToArcadiaStream(TChunkSerializer*) const = 0;
virtual bool IsSerializable() const = 0;
virtual ui32 CalculateSerializedSizeCached() const {
return CalculateSerializedSize();
}
virtual TEventSerializationInfo CreateSerializationInfo() const { return {}; }
};
// fat handle
class IEventHandle : TNonCopyable {
struct TOnNondelivery {
TActorId Recipient;
TOnNondelivery(const TActorId& recipient)
: Recipient(recipient)
{
}
};
public:
template <typename TEv>
inline TEv* CastAsLocal() const noexcept {
auto fits = GetTypeRewrite() == TEv::EventType;
return fits ? static_cast<TEv*>(Event.Get()) : nullptr;
}
template <typename TEventType>
TEventType* Get() {
if (Type != TEventType::EventType)
Y_FAIL("Event type %" PRIu32 " doesn't match the expected type %" PRIu32, Type, TEventType::EventType);
if (!Event) {
static TEventSerializedData empty;
Event.Reset(TEventType::Load(Buffer ? Buffer.Get() : &empty));
}
if (Event) {
return static_cast<TEventType*>(Event.Get());
}
Y_FAIL("Failed to Load() event type %" PRIu32 " class %s", Type, TypeName<TEventType>().data());
}
template <typename T>
TAutoPtr<T> Release() {
TAutoPtr<T> x = Get<T>();
Y_UNUSED(Event.Release());
Buffer.Reset();
return x;
}
enum EFlags: ui32 {
FlagTrackDelivery = 1 << 0,
FlagForwardOnNondelivery = 1 << 1,
FlagSubscribeOnSession = 1 << 2,
FlagUseSubChannel = 1 << 3,
FlagGenerateUnsureUndelivered = 1 << 4,
FlagExtendedFormat = 1 << 5,
};
using TEventFlags = ui32;
const ui32 Type;
const TEventFlags Flags;
const TActorId Recipient;
TActorId Sender;
const ui64 Cookie;
const TScopeId OriginScopeId = TScopeId::LocallyGenerated; // filled in when the message is received from Interconnect
// if set, used by ActorSystem/Interconnect to report tracepoints
NWilson::TTraceId TraceId;
// filled if feeded by interconnect session
const TActorId InterconnectSession;
#ifdef ACTORSLIB_COLLECT_EXEC_STATS
::NHPTimer::STime SendTime;
#endif
static const size_t ChannelBits = 12;
static const size_t ChannelShift = (sizeof(ui32) << 3) - ChannelBits;
#ifdef USE_ACTOR_CALLSTACK
TCallstack Callstack;
#endif
ui16 GetChannel() const noexcept {
return Flags >> ChannelShift;
}
ui64 GetSubChannel() const noexcept {
return Flags & FlagUseSubChannel ? Sender.LocalId() : 0ULL;
}
static ui32 MakeFlags(ui32 channel, TEventFlags flags) {
Y_VERIFY(channel < (1 << ChannelBits));
Y_VERIFY(flags < (1 << ChannelShift));
return (flags | (channel << ChannelShift));
}
private:
THolder<IEventBase> Event;
TIntrusivePtr<TEventSerializedData> Buffer;
TActorId RewriteRecipient;
ui32 RewriteType;
THolder<TOnNondelivery> OnNondeliveryHolder; // only for local events
public:
void Rewrite(ui32 typeRewrite, TActorId recipientRewrite) {
RewriteRecipient = recipientRewrite;
RewriteType = typeRewrite;
}
void DropRewrite() {
RewriteRecipient = Recipient;
RewriteType = Type;
}
const TActorId& GetRecipientRewrite() const {
return RewriteRecipient;
}
ui32 GetTypeRewrite() const {
return RewriteType;
}
TActorId GetForwardOnNondeliveryRecipient() const {
return OnNondeliveryHolder.Get() ? OnNondeliveryHolder->Recipient : TActorId();
}
IEventHandle(const TActorId& recipient, const TActorId& sender, IEventBase* ev, TEventFlags flags = 0, ui64 cookie = 0,
const TActorId* forwardOnNondelivery = nullptr, NWilson::TTraceId traceId = {})
: Type(ev->Type())
, Flags(flags)
, Recipient(recipient)
, Sender(sender)
, Cookie(cookie)
, TraceId(std::move(traceId))
#ifdef ACTORSLIB_COLLECT_EXEC_STATS
, SendTime(0)
#endif
, Event(ev)
, RewriteRecipient(Recipient)
, RewriteType(Type)
{
if (forwardOnNondelivery)
OnNondeliveryHolder.Reset(new TOnNondelivery(*forwardOnNondelivery));
}
IEventHandle(ui32 type,
TEventFlags flags,
const TActorId& recipient,
const TActorId& sender,
TIntrusivePtr<TEventSerializedData> buffer,
ui64 cookie,
const TActorId* forwardOnNondelivery = nullptr,
NWilson::TTraceId traceId = {})
: Type(type)
, Flags(flags)
, Recipient(recipient)
, Sender(sender)
, Cookie(cookie)
, TraceId(std::move(traceId))
#ifdef ACTORSLIB_COLLECT_EXEC_STATS
, SendTime(0)
#endif
, Buffer(std::move(buffer))
, RewriteRecipient(Recipient)
, RewriteType(Type)
{
if (forwardOnNondelivery)
OnNondeliveryHolder.Reset(new TOnNondelivery(*forwardOnNondelivery));
}
// Special ctor for events from interconnect.
IEventHandle(const TActorId& session,
ui32 type,
TEventFlags flags,
const TActorId& recipient,
const TActorId& sender,
TIntrusivePtr<TEventSerializedData> buffer,
ui64 cookie,
TScopeId originScopeId,
NWilson::TTraceId traceId) noexcept
: Type(type)
, Flags(flags)
, Recipient(recipient)
, Sender(sender)
, Cookie(cookie)
, OriginScopeId(originScopeId)
, TraceId(std::move(traceId))
, InterconnectSession(session)
#ifdef ACTORSLIB_COLLECT_EXEC_STATS
, SendTime(0)
#endif
, Buffer(std::move(buffer))
, RewriteRecipient(Recipient)
, RewriteType(Type)
{
}
TIntrusivePtr<TEventSerializedData> GetChainBuffer();
TIntrusivePtr<TEventSerializedData> ReleaseChainBuffer();
ui32 GetSize() const {
if (Buffer) {
return Buffer->GetSize();
} else if (Event) {
return Event->CalculateSerializedSize();
} else {
return 0;
}
}
bool HasBuffer() const {
return bool(Buffer);
}
bool HasEvent() const {
return bool(Event);
}
IEventBase* GetBase() {
if (!Event) {
if (!Buffer)
return nullptr;
else
ythrow TWithBackTrace<yexception>() << "don't know how to load the event from buffer";
}
return Event.Get();
}
TAutoPtr<IEventBase> ReleaseBase() {
TAutoPtr<IEventBase> x = GetBase();
Y_UNUSED(Event.Release());
Buffer.Reset();
return x;
}
TAutoPtr<IEventHandle> Forward(const TActorId& dest) {
if (Event)
return new IEventHandle(dest, Sender, Event.Release(), Flags, Cookie, nullptr, std::move(TraceId));
else
return new IEventHandle(Type, Flags, dest, Sender, Buffer, Cookie, nullptr, std::move(TraceId));
}
TString GetTypeName() const;
TString ToString() const;
[[nodiscard]] static std::unique_ptr<IEventHandle> Forward(std::unique_ptr<IEventHandle>&& ev, TActorId recipient);
[[nodiscard]] static std::unique_ptr<IEventHandle> ForwardOnNondelivery(std::unique_ptr<IEventHandle>&& ev, ui32 reason, bool unsure = false);
[[nodiscard]] static TAutoPtr<IEventHandle> Forward(TAutoPtr<IEventHandle>&& ev, TActorId recipient) {
return Forward(std::unique_ptr<IEventHandle>(ev.Release()), recipient).release();
}
[[nodiscard]] static THolder<IEventHandle> Forward(THolder<IEventHandle>&& ev, TActorId recipient) {
return THolder(Forward(std::unique_ptr<IEventHandle>(ev.Release()), recipient).release());
}
[[nodiscard]] static TAutoPtr<IEventHandle> ForwardOnNondelivery(TAutoPtr<IEventHandle>&& ev, ui32 reason, bool unsure = false) {
return ForwardOnNondelivery(std::unique_ptr<IEventHandle>(ev.Release()), reason, unsure).release();
}
[[nodiscard]] static THolder<IEventHandle> ForwardOnNondelivery(THolder<IEventHandle>&& ev, ui32 reason, bool unsure = false) {
return THolder(ForwardOnNondelivery(std::unique_ptr<IEventHandle>(ev.Release()), reason, unsure).release());
}
template<typename T>
static TAutoPtr<T> Release(TAutoPtr<IEventHandle>& ev) {
return ev->Release<T>();
}
template<typename T>
static TAutoPtr<T> Release(THolder<IEventHandle>& ev) {
return ev->Release<T>();
}
template <typename TEv>
inline TEv* StaticCastAsLocal() const noexcept { // blind cast
return static_cast<TEv*>(Event.Get());
}
};
template <typename TEventType>
class TEventHandle: public IEventHandle {
TEventHandle(); // we never made instance of TEventHandle
public:
TEventType* Get() {
return IEventHandle::Get<TEventType>();
}
TAutoPtr<TEventType> Release() {
return IEventHandle::Release<TEventType>();
}
};
static_assert(sizeof(TEventHandle<IEventBase>) == sizeof(IEventHandle), "expect sizeof(TEventHandle<IEventBase>) == sizeof(IEventHandle)");
template <typename TEventType, ui32 EventType0>
class TEventBase: public IEventBase {
public:
static constexpr ui32 EventType = EventType0;
ui32 Type() const override {
return EventType0;
}
// still abstract
typedef TEventHandle<TEventType> THandle;
typedef TAutoPtr<THandle> TPtr;
};
#define DEFINE_SIMPLE_LOCAL_EVENT(eventType, header) \
TString ToStringHeader() const override { \
return TString(header); \
} \
bool SerializeToArcadiaStream(NActors::TChunkSerializer*) const override { \
Y_FAIL("Local event " #eventType " is not serializable"); \
} \
static IEventBase* Load(NActors::TEventSerializedData*) { \
Y_FAIL("Local event " #eventType " has no load method"); \
} \
bool IsSerializable() const override { \
return false; \
}
#define DEFINE_SIMPLE_NONLOCAL_EVENT(eventType, header) \
TString ToStringHeader() const override { \
return TString(header); \
} \
bool SerializeToArcadiaStream(NActors::TChunkSerializer*) const override { \
return true; \
} \
static IEventBase* Load(NActors::TEventSerializedData*) { \
return new eventType(); \
} \
bool IsSerializable() const override { \
return true; \
}
}
|