elfo-core 0.2.0-alpha.21

The core of the elfo system
Documentation
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
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
use std::{alloc, fmt, mem, ptr, ptr::NonNull};

use elfo_utils::time::Instant;

use crate::{
    mailbox,
    message::{AnyMessageRef, Message, MessageRepr, MessageTypeId, Request},
    request_table::{RequestId, ResponseToken},
    tracing::TraceId,
    Addr,
};

/// An envelope is a wrapper around message with additional metadata,
/// involved in message passing between actors.
///
/// Envelopes aren't created directly in code, but are produced internally
/// by [`Context`]'s methods.
///
/// Converting an envelope to a message is usually done by calling the [`msg!`]
/// macro, which supports both owned and borrowed usages.
///
/// [`Context`]: crate::Context
/// [`msg!`]: crate::msg
pub struct Envelope(NonNull<EnvelopeHeader>);

// Messages aren't required to be `Sync`.
assert_not_impl_any!(Envelope: Sync);
assert_impl_all!(Envelope: Send);
assert_eq_size!(Envelope, usize);

// TODO: the current size (on x86-64) is 64 bytes, but it can be reduced.
// And... it should be reduced once `TraceId` is extended to 16 bytes.
pub(crate) struct EnvelopeHeader {
    /// See `mailbox.rs` for more details.
    pub(crate) link: mailbox::Link,
    created_time: Instant, // Now used also as a sent time.
    trace_id: TraceId,
    kind: MessageKind,
    /// Offset from the beginning of the envelope to the `MessageRepr`.
    message_offset: u32,
}

assert_impl_all!(EnvelopeHeader: Send);

// SAFETY: `Envelope` can point to `M: Message` only, which is `Send`.
// `EnvelopeHeader` is checked statically above to be `Send`.
unsafe impl Send for Envelope {}

// Reexported in `elfo::_priv`.
pub enum MessageKind {
    Regular { sender: Addr },
    RequestAny(ResponseToken),
    RequestAll(ResponseToken),
    Response { sender: Addr, request_id: RequestId },
}

impl MessageKind {
    #[inline]
    pub fn regular(sender: Addr) -> Self {
        Self::Regular { sender }
    }
}

// Called if the envelope hasn't been unpacked at all.
// For instance, if an actor dies with a non-empty mailbox.
// Usually, an envelope goes to `std::mem:forget()` in `unpack_*` methods.
impl Drop for Envelope {
    fn drop(&mut self) {
        let message = self.message();
        let message_layout = message._repr_layout();
        let (layout, message_offset) = envelope_repr_layout(message_layout);
        debug_assert_eq!(message_offset, self.header().message_offset);

        // Drop the message.
        // SAFETY: the message is not accessed anymore below.
        unsafe { message.drop_in_place() };

        // Drop the header.
        // SAFETY: the header is not accessed anymore below.
        unsafe { ptr::drop_in_place(self.0.as_ptr()) }

        // Deallocate the whole envelope.
        // SAFETY: memory was allocated by `alloc::alloc` with the same layout.
        unsafe { alloc::dealloc(self.0.as_ptr().cast(), layout) };
    }
}

impl Envelope {
    // This is private API. Do not use it.
    #[doc(hidden)]
    #[inline]
    pub fn new<M: Message>(message: M, kind: MessageKind) -> Self {
        Self::with_trace_id(message, kind, crate::scope::trace_id())
    }

    // This is private API. Do not use it.
    #[doc(hidden)]
    #[inline]
    pub fn with_trace_id<M: Message>(message: M, kind: MessageKind, trace_id: TraceId) -> Self {
        let message_layout = message._repr_layout();
        let (layout, message_offset) = envelope_repr_layout(message_layout);

        let header = EnvelopeHeader {
            link: <_>::default(),
            created_time: Instant::now(),
            trace_id,
            kind,
            message_offset,
        };

        // SAFETY: `layout` is correct and non-zero.
        let ptr = unsafe { alloc::alloc(layout) };

        let Some(ptr) = NonNull::new(ptr) else {
            alloc::handle_alloc_error(layout);
        };

        // SAFETY: `ptr` is valid to write the header.
        unsafe { ptr::write(ptr.cast().as_ptr(), header) };

        let this = Self(ptr.cast());
        let message_ptr = this.message_repr_ptr();

        // SAFETY: `message_ptr` is valid to write the message.
        unsafe { message._write(message_ptr) };

        this
    }

    pub(crate) fn stub() -> Self {
        Self::with_trace_id(
            crate::messages::Ping,
            MessageKind::regular(Addr::NULL),
            TraceId::try_from(1).unwrap(),
        )
    }

    fn header(&self) -> &EnvelopeHeader {
        // SAFETY: `self.0` is properly initialized.
        unsafe { self.0.as_ref() }
    }

    #[inline]
    pub fn trace_id(&self) -> TraceId {
        self.header().trace_id
    }

    /// Returns a reference to the untyped message inside the envelope.
    #[inline]
    pub fn message(&self) -> AnyMessageRef<'_> {
        let message_repr = self.message_repr_ptr();

        // SAFETY: `message_repr` is valid pointer for read.
        unsafe { AnyMessageRef::new(message_repr) }
    }

    /// Part of private API. Do not use it.
    #[doc(hidden)]
    pub fn message_kind(&self) -> &MessageKind {
        &self.header().kind
    }

    #[doc(hidden)]
    #[inline]
    pub fn created_time(&self) -> Instant {
        self.header().created_time
    }

    #[inline]
    pub fn sender(&self) -> Addr {
        match self.message_kind() {
            MessageKind::Regular { sender } => *sender,
            MessageKind::RequestAny(token) => token.sender(),
            MessageKind::RequestAll(token) => token.sender(),
            MessageKind::Response { sender, .. } => *sender,
        }
    }

    #[inline]
    pub fn request_id(&self) -> Option<RequestId> {
        match self.message_kind() {
            MessageKind::Regular { .. } => None,
            MessageKind::RequestAny(token) => Some(token.request_id()),
            MessageKind::RequestAll(token) => Some(token.request_id()),
            MessageKind::Response { request_id, .. } => Some(*request_id),
        }
    }

    #[doc(hidden)]
    #[inline]
    pub fn type_id(&self) -> MessageTypeId {
        self.message().type_id()
    }

    #[inline]
    pub fn is<M: Message>(&self) -> bool {
        self.message().is::<M>()
    }

    #[doc(hidden)]
    pub fn duplicate(&self) -> Self {
        let header = self.header();
        let message = self.message();
        let message_layout = message._repr_layout();
        let (layout, message_offset) = envelope_repr_layout(message_layout);
        debug_assert_eq!(message_offset, header.message_offset);

        let out_header = EnvelopeHeader {
            link: <_>::default(),
            created_time: header.created_time,
            trace_id: header.trace_id,
            kind: match &header.kind {
                MessageKind::Regular { sender } => MessageKind::Regular { sender: *sender },
                MessageKind::RequestAny(token) => MessageKind::RequestAny(token.duplicate()),
                MessageKind::RequestAll(token) => MessageKind::RequestAll(token.duplicate()),
                MessageKind::Response { sender, request_id } => MessageKind::Response {
                    sender: *sender,
                    request_id: *request_id,
                },
            },
            message_offset,
        };

        // SAFETY: `layout` is correct and non-zero.
        let out_ptr = unsafe { alloc::alloc(layout) };

        let Some(out_ptr) = NonNull::new(out_ptr) else {
            alloc::handle_alloc_error(layout);
        };

        // SAFETY: `out_ptr` is valid to write the header.
        unsafe { ptr::write(out_ptr.cast().as_ptr(), out_header) };

        let out = Self(out_ptr.cast());
        let out_message_ptr = out.message_repr_ptr();

        // SAFETY: `out_message_ptr` is valid and has the same layout as `message`.
        unsafe { message.clone_into(out_message_ptr) };

        out
    }

    // TODO: remove the method
    pub(crate) fn set_message<M: Message>(&mut self, message: M) {
        assert!(self.is::<M>() && M::_type_id() != crate::message::AnyMessage::_type_id());

        let repr_ptr = self.message_repr_ptr().cast::<MessageRepr<M>>().as_ptr();

        // SAFETY: `repr_ptr` is valid to write the message.
        unsafe { ptr::replace(repr_ptr, MessageRepr::new(message)) };
    }

    fn message_repr_ptr(&self) -> NonNull<MessageRepr> {
        let message_offset = self.header().message_offset;

        // SAFETY: `message_offset` refers to the same allocation object.
        let ptr = unsafe { self.0.as_ptr().byte_add(message_offset as usize) };

        // SAFETY: `envelope_repr_layout()` guarantees that `ptr` is valid.
        unsafe { NonNull::new_unchecked(ptr.cast()) }
    }

    #[doc(hidden)]
    #[inline]
    pub fn unpack<M: Message>(self) -> Option<(M, MessageKind)> {
        self.is::<M>()
            // SAFETY: `self` contains a message of type `M`, checked above.
            .then(|| unsafe { self.unpack_unchecked() })
    }

    /// # Safety
    ///
    /// The caller must ensure that the message is of the correct type.
    unsafe fn unpack_unchecked<M: Message>(self) -> (M, MessageKind) {
        let message_layout = self.message()._repr_layout();
        let (layout, message_offset) = envelope_repr_layout(message_layout);
        debug_assert_eq!(message_offset, self.header().message_offset);

        let message = M::_read(self.message_repr_ptr());
        let kind = ptr::read(&self.0.as_ref().kind);

        alloc::dealloc(self.0.as_ptr().cast(), layout);
        mem::forget(self);
        (message, kind)
    }

    pub(crate) fn into_header_ptr(self) -> NonNull<EnvelopeHeader> {
        let ptr = self.0;
        mem::forget(self);
        ptr
    }

    pub(crate) unsafe fn from_header_ptr(ptr: NonNull<EnvelopeHeader>) -> Self {
        Self(ptr)
    }
}

fn envelope_repr_layout(message_layout: alloc::Layout) -> (alloc::Layout, u32) {
    let (layout, message_offset) = alloc::Layout::new::<EnvelopeHeader>()
        .extend(message_layout)
        .expect("impossible envelope layout");

    let message_offset =
        u32::try_from(message_offset).expect("message requires too large alignment");

    (layout.pad_to_align(), message_offset)
}

impl fmt::Debug for MessageKind {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            MessageKind::Regular { sender: _ } => f.debug_struct("Regular").finish(),
            MessageKind::RequestAny(token) => f
                .debug_tuple("RequestAny")
                .field(&token.request_id())
                .finish(),
            MessageKind::RequestAll(token) => f
                .debug_tuple("RequestAll")
                .field(&token.request_id())
                .finish(),
            MessageKind::Response {
                sender: _,
                request_id,
            } => f.debug_tuple("Response").field(request_id).finish(),
        }
    }
}

impl fmt::Debug for Envelope {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_struct("Envelope")
            .field("trace_id", &self.trace_id())
            .field("sender", &self.sender())
            .field("kind", &self.message_kind())
            .field("message", &self.message())
            .finish()
    }
}

// Extra traits to support both owned and borrowed usages of `msg!(..)`.
// Both traits are private and reexported in `elfo::_priv` only

#[doc(hidden)]
pub trait EnvelopeOwned {
    /// # Safety
    ///
    /// The caller must ensure that the message is of the correct type.
    unsafe fn unpack_regular_unchecked<M: Message>(self) -> M;

    /// # Safety
    ///
    /// The caller must ensure that the request is of the correct type.
    unsafe fn unpack_request_unchecked<R: Request>(self) -> (R, ResponseToken<R>);
}

#[doc(hidden)]
pub trait EnvelopeBorrowed {
    /// # Safety
    ///
    /// The caller must ensure that the message is of the correct type.
    unsafe fn unpack_regular_unchecked<M: Message>(&self) -> &M;
}

impl EnvelopeOwned for Envelope {
    #[inline]
    unsafe fn unpack_regular_unchecked<M: Message>(self) -> M {
        let (message, kind) = self.unpack_unchecked();

        #[cfg(feature = "network")]
        if let MessageKind::RequestAny(token) | MessageKind::RequestAll(token) = kind {
            // The sender thought this is a request, but for the current node it isn't.
            // Mark the token as received to return `RequestError::Ignored` to the sender.
            let _ = token.into_received::<()>();
        }

        // This check is debug-only because it's already checked in `msg!` in
        // compile-time, which should be the only way to call this consuming method.
        #[cfg(not(feature = "network"))]
        debug_assert!(!matches!(
            kind,
            MessageKind::RequestAny(_) | MessageKind::RequestAll(_)
        ));

        message
    }

    #[inline]
    unsafe fn unpack_request_unchecked<R: Request>(self) -> (R, ResponseToken<R>) {
        let (message, kind) = self.unpack_unchecked();

        let token = match kind {
            MessageKind::RequestAny(token) | MessageKind::RequestAll(token) => token,
            // A request sent by using `ctx.send()` ("fire and forget").
            // Also it's useful for the protocol evolution between remote nodes.
            _ => ResponseToken::forgotten(),
        };

        (message, token.into_received())
    }
}

impl EnvelopeBorrowed for Envelope {
    #[inline]
    unsafe fn unpack_regular_unchecked<M: Message>(&self) -> &M {
        self.message().downcast_ref_unchecked()
    }
}

#[cfg(test)]
mod tests_miri {
    use std::sync::Arc;

    use elfo_utils::time;

    use super::*;
    use crate::{message, AnyMessage};

    fn make_regular_envelope(message: impl Message) -> Envelope {
        // Miri doesn't support asm, so mock the time.
        // TODO: support miri in `elfo_utils::time`.
        time::with_instant_mock(|_mock| {
            let addr = Addr::NULL;
            let trace_id = TraceId::try_from(1).unwrap();
            Envelope::with_trace_id(message, MessageKind::regular(addr), trace_id)
        })
    }

    #[message]
    #[derive(PartialEq)]
    struct P8(u64);

    #[test]
    fn basic_ops() {
        let message = P8(42);
        let envelope = make_regular_envelope(message.clone());

        assert_eq!(envelope.trace_id(), TraceId::try_from(1).unwrap());
        assert_eq!(envelope.sender(), Addr::NULL);

        assert_eq!(envelope.type_id(), P8::_type_id());
        assert!(envelope.is::<P8>());
        assert!(envelope.is::<AnyMessage>());
        assert!(!envelope.is::<crate::messages::Ping>());

        let (actual_message, _) = envelope.unpack::<P8>().unwrap();
        assert_eq!(actual_message, message);

        // Unpack to `AnyMessage`
        let envelope = make_regular_envelope(message.clone());

        let (actual_message, _) = envelope.unpack::<AnyMessage>().unwrap();
        assert_eq!(format!("{actual_message:?}"), format!("{message:?}"));
    }

    #[test]
    fn set_message() {
        let message = P8(42);
        let mut envelope = make_regular_envelope(message.clone());
        envelope.set_message(P8(43));

        let (actual_message, _) = envelope.unpack::<P8>().unwrap();
        assert_eq!(actual_message, P8(43));
    }

    #[test]
    fn duplicate() {
        #[message]
        #[derive(PartialEq)]
        struct Sample {
            value: u128,
            counter: Arc<()>,
        }

        impl Sample {
            fn new(value: u128) -> (Arc<()>, Self) {
                let this = Self {
                    value,
                    counter: Arc::new(()),
                };

                (this.counter.clone(), this)
            }
        }

        let (counter, message) = Sample::new(42);
        let envelope = make_regular_envelope(message);

        assert_eq!(Arc::strong_count(&counter), 2);
        let envelope2 = envelope.duplicate();
        assert_eq!(Arc::strong_count(&counter), 3);
        assert!(envelope2.is::<Sample>());
        let envelope3 = envelope2.duplicate();
        assert_eq!(Arc::strong_count(&counter), 4);
        assert!(envelope3.is::<Sample>());

        drop(envelope2);
        assert_eq!(Arc::strong_count(&counter), 3);

        drop(envelope3);
        assert_eq!(Arc::strong_count(&counter), 2);

        let envelope4 = envelope.duplicate();
        assert_eq!(Arc::strong_count(&counter), 3);
        assert!(envelope4.is::<Sample>());

        drop(envelope);
        assert_eq!(Arc::strong_count(&counter), 2);

        drop(envelope4);
        assert_eq!(Arc::strong_count(&counter), 1);
    }
}