rustorb-core 0.3.0

ORB core of rustorb: connections, request dispatch and POA
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
//! Reading GIOP messages off a stream, shared by client and server
//! connections.
//!
//! A connection is read either by its reader task on the runtime or, for a
//! while, by a thread outside it: on the server, the upcall thread that
//! just answered a request of the connection reads the next one (leader /
//! follower); on the client, the caller waiting for the only reply
//! outstanding reads it. [`Inbound`] hands the reading between the two so
//! that only one of them reads at a time; see docs/read-ownership.md.

use std::io;
use std::mem::MaybeUninit;
use std::net::SocketAddr;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex, MutexGuard};
use std::time::Instant;

use rustorb_giop::{FragmentAssembler, Message, MessageHeader, Version};
use socket2::SockRef;
use tokio::io::Interest;
use tokio::net::tcp::OwnedReadHalf;
use tokio::sync::Notify;

use crate::tls::Tls;
use crate::trace;

/// A peer address in trace lines.
pub(crate) struct PeerAddr(pub(crate) Option<SocketAddr>);

impl std::fmt::Display for PeerAddr {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self.0 {
            Some(addr) => addr.fmt(f),
            None => f.write_str("an unknown peer"),
        }
    }
}

/// How much a connection reads ahead: a small message and the header of
/// the next one usually come in with one read. Bodies larger than this
/// are read straight into their own buffer.
const READ_AHEAD: usize = 16 * 1024;

/// Why no message could be read.
#[derive(Debug)]
pub(crate) enum ReadError {
    /// The stream ended, or failed, before a message header.
    Closed(io::Error),
    /// The header is not a GIOP header.
    BadHeader(String),
    /// The message is longer than the limit; carries its version.
    TooLarge(usize, Version),
    /// The stream ended, or failed, inside a message.
    Truncated(io::Error),
    /// A whole message that could not be joined or decoded.
    Malformed(Version, String),
}

/// Reads one byte source into spare capacity; `WouldBlock` when nothing is
/// there now.
type ReadFn<'a> = dyn FnMut(&mut [MaybeUninit<u8>]) -> io::Result<usize> + 'a;

/// Cuts a stream into whole messages without waiting: what has arrived is
/// kept until the rest of the message does.
#[derive(Debug)]
struct MessageReader {
    /// Read-ahead; the bytes not taken yet are `buf[start..]`.
    buf: Vec<u8>,
    start: usize,
    /// A message larger than the read-ahead, read straight into its own
    /// buffer: its header and the bytes so far.
    large: Option<(MessageHeader, Vec<u8>)>,
    max_message_size: usize,
}

impl MessageReader {
    fn new(max_message_size: usize) -> MessageReader {
        MessageReader {
            buf: Vec::with_capacity(READ_AHEAD),
            start: 0,
            large: None,
            max_message_size,
        }
    }

    /// Whether the next message is larger than the read-ahead.
    fn starts_large(&self) -> bool {
        let buffered = &self.buf[self.start..];
        self.large.is_some()
            || buffered.len() >= MessageHeader::SIZE
                && MessageHeader::parse(&buffered[..MessageHeader::SIZE])
                    .is_ok_and(|h| h.total_len() > READ_AHEAD)
    }

    /// Whether part of a message (or more) was read and not taken.
    fn has_buffered(&self) -> bool {
        self.start < self.buf.len() || self.large.is_some()
    }

    /// The next whole message, header included, in a buffer of its own
    /// (not zeroed first). `Ok(None)` when `read` has nothing more now.
    /// With `defer_large`, a message larger than the read-ahead is left
    /// for later ([`starts_large`](Self::starts_large) tells).
    fn next(
        &mut self,
        read: &mut ReadFn<'_>,
        defer_large: bool,
    ) -> Result<Option<(MessageHeader, Vec<u8>)>, ReadError> {
        loop {
            if let Some((header, msg)) = &mut self.large {
                let total = header.total_len();
                while msg.len() < total {
                    let left = total - msg.len();
                    match read(&mut msg.spare_capacity_mut()[..left]) {
                        Ok(0) => {
                            return Err(ReadError::Truncated(io::ErrorKind::UnexpectedEof.into()));
                        }
                        Ok(n) => {
                            // SAFETY: `read` initialised the first `n`
                            // bytes of the spare capacity.
                            unsafe { msg.set_len(msg.len() + n) };
                        }
                        Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
                        Err(e) if e.kind() == io::ErrorKind::WouldBlock => return Ok(None),
                        Err(e) => return Err(ReadError::Truncated(e)),
                    }
                }
                return Ok(self.large.take());
            }
            let buffered = &self.buf[self.start..];
            if buffered.len() >= MessageHeader::SIZE {
                let header = MessageHeader::parse(&buffered[..MessageHeader::SIZE])
                    .map_err(|e| ReadError::BadHeader(e.to_string()))?;
                let total = header.total_len();
                if total > self.max_message_size {
                    return Err(ReadError::TooLarge(total, header.version));
                }
                if buffered.len() >= total {
                    let msg = buffered[..total].to_vec();
                    self.start += total;
                    return Ok(Some((header, msg)));
                }
                if total > READ_AHEAD {
                    if defer_large {
                        return Ok(None);
                    }
                    let mut msg = Vec::with_capacity(total);
                    msg.extend_from_slice(buffered);
                    self.start = self.buf.len();
                    self.large = Some((header, msg));
                    continue;
                }
            }
            match self.fill(read) {
                Ok(true) => {}
                Ok(false) => return Ok(None),
                Err(e) => return Err(e),
            }
        }
    }

    /// Reads what `read` has into the read-ahead. `Ok(false)` when it had
    /// nothing.
    fn fill(&mut self, read: &mut ReadFn<'_>) -> Result<bool, ReadError> {
        if self.start == self.buf.len() {
            self.buf.clear();
            self.start = 0;
        } else if self.buf.len() == self.buf.capacity() {
            self.buf.drain(..self.start);
            self.start = 0;
        }
        let at_boundary = self.start == self.buf.len();
        loop {
            match read(self.buf.spare_capacity_mut()) {
                Ok(0) => {
                    let eof = io::Error::from(io::ErrorKind::UnexpectedEof);
                    return Err(if at_boundary {
                        ReadError::Closed(eof)
                    } else {
                        ReadError::Truncated(eof)
                    });
                }
                Ok(n) => {
                    // SAFETY: `read` initialised the first `n` bytes of the
                    // spare capacity.
                    unsafe { self.buf.set_len(self.buf.len() + n) };
                    return Ok(true);
                }
                Err(e) if e.kind() == io::ErrorKind::Interrupted => {}
                Err(e) if e.kind() == io::ErrorKind::WouldBlock => return Ok(false),
                Err(e) if at_boundary => return Err(ReadError::Closed(e)),
                Err(e) => return Err(ReadError::Truncated(e)),
            }
        }
    }

    /// Reads into the read-ahead until `read` has nothing more or it is
    /// full, without taking a message. Returns whether there is anything
    /// for [`next`](Self::next): something buffered, or a failure, which
    /// `read` reports again.
    fn drain(&mut self, read: &mut ReadFn<'_>) -> bool {
        if self.large.is_none() {
            while self.buf.len() < self.buf.capacity() || self.start > 0 {
                match self.fill(read) {
                    Ok(true) => {}
                    Ok(false) => break,
                    Err(_) => return true,
                }
            }
        }
        self.has_buffered()
    }
}

/// The read side of a connection: what was read and not taken, and the
/// fragments of messages not complete yet.
#[derive(Debug)]
pub(crate) struct ReadSide {
    reader: MessageReader,
    pub(crate) assembler: FragmentAssembler,
    /// A message or failure that a thread read and left for the reader
    /// task.
    held: Option<Result<(Version, Message), ReadError>>,
    /// The address of the peer, for trace lines.
    peer: Option<SocketAddr>,
}

impl ReadSide {
    /// The next whole message, fragments joined. `Ok(None)` when `read`
    /// has nothing more now.
    fn next(
        &mut self,
        read: &mut ReadFn<'_>,
        defer_large: bool,
    ) -> Result<Option<(Version, Message)>, ReadError> {
        if let Some(held) = self.held.take() {
            return held.map(Some);
        }
        loop {
            let Some((header, msg)) = self.reader.next(read, defer_large)? else {
                return Ok(None);
            };
            trace::trace!(
                "received {} from {}",
                trace::MessageSummary(&msg, msg.len()),
                PeerAddr(self.peer)
            );
            let version = header.version;
            let malformed = |e: rustorb_giop::Error| ReadError::Malformed(version, e.to_string());
            let Some(whole) = self.assembler.push_owned(msg).map_err(malformed)? else {
                continue;
            };
            let (_, message) = whole.into_decoded().map_err(malformed)?;
            return Ok(Some((version, message)));
        }
    }
}

/// Who reads the connection.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum Holder {
    /// The reader task; `active` while it reads or handles what it read.
    Task { active: bool },
    /// A thread reads, or waits for the stream to become readable.
    Thread,
    /// The thread that read last handles what it read and means to read
    /// again. The reader task reads if more arrives meanwhile.
    Busy,
}

/// What [`Inbound::read_here`] got.
#[derive(Debug)]
pub(crate) enum Here {
    /// A whole message; the thread still holds the reading.
    Message(Version, Message),
    /// Nothing came before the deadline; the reading went back to the
    /// task.
    TimedOut,
    /// The stream ended or failed; that is left for the task, which has
    /// the reading back.
    HandedBack,
}

/// The read half of a connection, read by its reader task or, for a while,
/// by one thread.
///
/// The task reads through the runtime's readiness (so that a `WouldBlock`
/// clears it); a thread reads the socket directly and waits with `poll`.
/// A thread may take the reading only from an idle task. Data arriving
/// while a thread reads wakes the task too; it then forgets that
/// readiness. Whenever the reading goes back to the task, the thread first
/// reads what has arrived (under the holder lock, so nothing arrives
/// unseen in between) and wakes the task if anything is buffered. The lock
/// order is `holder`, then `side`.
#[derive(Debug)]
pub(crate) struct Inbound {
    stream: OwnedReadHalf,
    /// TLS on the stream: every read goes through it.
    tls: Option<Arc<Tls>>,
    holder: Mutex<Holder>,
    side: Mutex<ReadSide>,
    /// Wakes the task when a thread hands the reading back with something
    /// buffered.
    back: Notify,
    /// Set with `back`: the task's next read goes to the socket even if
    /// the runtime has seen no readiness (the thread's reads bypass it,
    /// and the rest of a large message may have arrived already).
    handed_back: AtomicBool,
    peer: Option<SocketAddr>,
}

impl Inbound {
    pub(crate) fn new(
        stream: OwnedReadHalf,
        tls: Option<Arc<Tls>>,
        max_message_size: usize,
    ) -> Inbound {
        let peer = stream.peer_addr().ok();
        Inbound {
            stream,
            tls,
            holder: Mutex::new(Holder::Task { active: true }),
            side: Mutex::new(ReadSide {
                reader: MessageReader::new(max_message_size),
                assembler: FragmentAssembler::new(max_message_size),
                held: None,
                peer,
            }),
            back: Notify::new(),
            handed_back: AtomicBool::new(false),
            peer,
        }
    }

    /// Reads `buf` with `raw`, through TLS if the stream has it.
    fn read(&self, raw: &mut ReadFn<'_>, buf: &mut [MaybeUninit<u8>]) -> io::Result<usize> {
        match &self.tls {
            Some(tls) => tls.read(buf, raw),
            None => raw(buf),
        }
    }

    pub(crate) fn side(&self) -> MutexGuard<'_, ReadSide> {
        self.side.lock().unwrap()
    }

    /// The address of the peer.
    pub(crate) fn peer(&self) -> PeerAddr {
        PeerAddr(self.peer)
    }

    /// Whether part of a message, or a message not handled yet, was read,
    /// or a thread is reading: the connection is not idle.
    pub(crate) fn mid_message(&self) -> bool {
        match self.side.try_lock() {
            Ok(side) => {
                side.reader.has_buffered() || side.assembler.has_pending() || side.held.is_some()
            }
            Err(_) => true,
        }
    }

    /// Shuts the read direction down, which ends a thread's wait and the
    /// task's reading.
    pub(crate) fn shutdown(&self) {
        let _ = SockRef::from(self.stream.as_ref()).shutdown(std::net::Shutdown::Read);
    }

    // --- the reader task ---------------------------------------------------

    /// The next message for the reader task, waiting for it as needed.
    /// The task counts as active from here until it waits again.
    pub(crate) async fn next(&self) -> Result<(Version, Message), ReadError> {
        loop {
            if self.task_may_read() {
                let stream = self.stream.as_ref();
                let direct = self.handed_back.swap(false, Ordering::AcqRel);
                let mut raw = |buf: &mut [MaybeUninit<u8>]| {
                    if direct {
                        SockRef::from(stream).recv(buf)
                    } else {
                        stream.try_io(Interest::READABLE, || SockRef::from(stream).recv(buf))
                    }
                };
                let mut read = |buf: &mut [MaybeUninit<u8>]| self.read(&mut raw, buf);
                if let Some(msg) = self.side().next(&mut read, false)? {
                    return Ok(msg);
                }
            }
            self.task_wait().await;
        }
    }

    /// Whether the task holds the reading, taking it from a busy thread if
    /// more arrived.
    fn task_may_read(&self) -> bool {
        let mut holder = self.holder.lock().unwrap();
        match *holder {
            Holder::Task { .. } => {
                *holder = Holder::Task { active: true };
                true
            }
            Holder::Busy => {
                // The thread handles a request; read what arrived since,
                // so that requests on one connection run side by side.
                let stream = self.stream.as_ref();
                let mut side = self.side.lock().unwrap();
                let mut raw = |buf: &mut [MaybeUninit<u8>]| {
                    stream.try_io(Interest::READABLE, || SockRef::from(stream).recv(buf))
                };
                let more = side.reader.drain(&mut |buf| self.read(&mut raw, buf));
                if more {
                    *holder = Holder::Task { active: true };
                    true
                } else {
                    false
                }
            }
            Holder::Thread => {
                // The thread reads what woke the task.
                let _ = self.stream.as_ref().try_io(Interest::READABLE, || {
                    Err::<(), _>(io::ErrorKind::WouldBlock.into())
                });
                false
            }
        }
    }

    /// Waits until the stream is readable or a thread hands the reading
    /// back. The task is idle meanwhile, so a thread may take the reading.
    async fn task_wait(&self) {
        {
            let mut holder = self.holder.lock().unwrap();
            if let Holder::Task { .. } = *holder {
                *holder = Holder::Task { active: false };
            }
        }
        let mut back = std::pin::pin!(self.back.notified());
        let mut readable = std::pin::pin!(self.stream.readable());
        std::future::poll_fn(|cx| {
            if back.as_mut().poll(cx).is_ready() || readable.as_mut().poll(cx).is_ready() {
                std::task::Poll::Ready(())
            } else {
                std::task::Poll::Pending
            }
        })
        .await;
    }

    // --- a thread ----------------------------------------------------------

    /// Takes the reading for the calling thread, from an idle task or from
    /// the thread's own busy turn. Only where a thread can wait for the
    /// socket (`poll`).
    pub(crate) fn claim(&self) -> bool {
        if !cfg!(unix) {
            return false;
        }
        let mut holder = self.holder.lock().unwrap();
        match *holder {
            Holder::Task { active: false } | Holder::Busy => {
                *holder = Holder::Thread;
                true
            }
            _ => false,
        }
    }

    /// Reads the next message on the calling thread, which holds the
    /// reading, waiting for it until `until` (for ever with `None`).
    pub(crate) fn read_here(&self, until: Option<Instant>) -> Here {
        let mut side = self.side();
        let stream = self.stream.as_ref();
        let mut raw = |buf: &mut [MaybeUninit<u8>]| SockRef::from(stream).recv(buf);
        let mut read = |buf: &mut [MaybeUninit<u8>]| self.read(&mut raw, buf);
        loop {
            match side.next(&mut read, true) {
                Ok(Some((version, msg))) => return Here::Message(version, msg),
                // A large message comes in many pieces: the task reads
                // them as they come, which costs fewer wake-ups than a
                // thread waiting for each, and allocates its buffer where
                // it did before.
                Ok(None) if side.reader.starts_large() => {
                    drop(side);
                    self.release();
                    return Here::HandedBack;
                }
                Ok(None) => {}
                Err(e) => {
                    side.held = Some(Err(e));
                    drop(side);
                    self.release();
                    return Here::HandedBack;
                }
            }
            if !wait_readable(stream, until) {
                drop(side);
                self.release();
                return Here::TimedOut;
            }
        }
    }

    /// Leaves a message the thread read, and does not handle, for the
    /// task, and hands the reading back.
    pub(crate) fn give_back(&self, version: Version, msg: Message) {
        self.side().held = Some(Ok((version, msg)));
        self.release();
    }

    /// The thread goes on to handle the message it read. Returns whether
    /// it keeps the reading for afterwards ([`claim`] then takes it
    /// back); if more is buffered already, the task gets the reading now.
    pub(crate) fn busy(&self) -> bool {
        let mut holder = self.holder.lock().unwrap();
        let stream = self.stream.as_ref();
        let mut side = self.side.lock().unwrap();
        let mut raw = |buf: &mut [MaybeUninit<u8>]| SockRef::from(stream).recv(buf);
        let more = side.reader.drain(&mut |buf| self.read(&mut raw, buf));
        if more || side.held.is_some() {
            *holder = Holder::Task { active: false };
            drop(side);
            drop(holder);
            self.hand_back();
            false
        } else {
            *holder = Holder::Busy;
            true
        }
    }

    /// Wakes the task to read what a thread left.
    fn hand_back(&self) {
        self.handed_back.store(true, Ordering::Release);
        self.back.notify_one();
    }

    /// Ends the busy turn of a thread that will not read again.
    pub(crate) fn unbusy(&self) {
        let mut holder = self.holder.lock().unwrap();
        if *holder == Holder::Busy {
            *holder = Holder::Task { active: false };
        }
    }

    /// Gives the reading back to the task; by the thread holding it.
    pub(crate) fn release(&self) {
        let mut holder = self.holder.lock().unwrap();
        match *holder {
            Holder::Thread => {
                let stream = self.stream.as_ref();
                let mut side = self.side.lock().unwrap();
                let mut raw = |buf: &mut [MaybeUninit<u8>]| SockRef::from(stream).recv(buf);
                let more = side.reader.drain(&mut |buf| self.read(&mut raw, buf));
                *holder = Holder::Task { active: false };
                if more || side.held.is_some() {
                    drop(side);
                    drop(holder);
                    self.hand_back();
                }
            }
            // Whatever arrived while busy woke the task, which read it.
            Holder::Busy => *holder = Holder::Task { active: false },
            Holder::Task { .. } => {}
        }
    }
}

/// Waits until `stream` has something to read, or `until`. Returns
/// whether it may have.
#[cfg(unix)]
fn wait_readable(stream: &tokio::net::TcpStream, until: Option<Instant>) -> bool {
    use std::os::fd::AsRawFd;
    loop {
        let timeout = match until {
            None => -1,
            Some(at) => {
                let left = at.saturating_duration_since(Instant::now());
                if left.is_zero() {
                    return false;
                }
                // Rounded up, so that the wait does not end early.
                i32::try_from(left.as_micros().div_ceil(1000)).unwrap_or(i32::MAX)
            }
        };
        let mut fd = libc::pollfd {
            fd: stream.as_raw_fd(),
            events: libc::POLLIN,
            revents: 0,
        };
        // SAFETY: one valid pollfd.
        let n = unsafe { libc::poll(&mut fd, 1, timeout) };
        if n > 0 {
            return true;
        }
        if n < 0 && io::Error::last_os_error().kind() != io::ErrorKind::Interrupted {
            // Let the read find the error.
            return true;
        }
    }
}

#[cfg(not(unix))]
fn wait_readable(_: &tokio::net::TcpStream, _: Option<Instant>) -> bool {
    // A thread never claims the reading here.
    false
}