aimdb-core 3.0.0

Type-safe async data pipelines — one Rust codebase from MCU to cloud
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
//! [`OutboundRoutes`]: every outbound link of one scheme, pulled by the
//! connector's transport task.

use crate::outbound::events::{report, OutboundEvent};

use alloc::boxed::Box;
use alloc::sync::Arc;
use alloc::vec::Vec;
use core::future::poll_fn;
use core::task::{Context, Poll};

use super::ready::{Polled, ReadyRoutes};
use super::RouteId;
use crate::connector::SerializeError;
use crate::transport::ConnectorConfig;
use crate::{AimDb, DbError, DbResult, RuntimeContext};

/// One outbound route, for parsing per-route configuration once at build.
#[derive(Debug, Clone)]
pub struct RouteInfo {
    /// Dense index, `0..routes().len()`.
    pub id: RouteId,
    /// `TypeId` of the record the link belongs to.
    pub type_id: core::any::TypeId,
    /// The topic from the link URL, used when no topic is written.
    pub default_topic: Arc<str>,
    /// Index of the record the link belongs to, in registration order (the
    /// record's `RecordId`). Set by core, never from the link's query.
    pub record_index: usize,
    /// The link's configuration.
    pub config: ConnectorConfig,
    /// Longest topic the link's writer produces; 0 without a writer.
    pub topic_capacity: usize,
    /// Scratch for `with_serializer_into`; 0 with an owned serializer only.
    pub payload_capacity: usize,
}

/// A message pulled from [`OutboundRoutes`].
#[derive(Debug)]
pub struct OutboundMessage<'a> {
    /// The route it came from.
    pub route: &'a RouteInfo,
    /// The written topic, or the route's default.
    pub topic: &'a str,
    /// The serialized value.
    pub payload: OutboundPayload<'a>,
    /// Metadata of the write the route serialized.
    pub metadata: crate::WriteMetadata,
}

impl OutboundMessage<'_> {
    /// When the serialized value was written, on the instance clock.
    pub fn stamp(&self) -> crate::Stamp {
        self.metadata.stamp()
    }
}

/// Where a pulled message's bytes are.
#[derive(Debug, PartialEq, Eq)]
pub enum OutboundPayload<'a> {
    /// Serialized into `OutboundRoutes`' scratch (`with_serializer_into`).
    Borrowed(&'a [u8]),
    /// An owned serializer's bytes (`with_serializer`). Transports that take
    /// ownership move it in; others borrow it.
    Owned(Vec<u8>),
}

impl OutboundPayload<'_> {
    /// The bytes, whichever variant holds them.
    pub fn as_slice(&self) -> &[u8] {
        match self {
            Self::Borrowed(bytes) => bytes,
            Self::Owned(bytes) => bytes,
        }
    }

    /// The bytes as an owned `Vec`: moved out of `Owned`, copied from
    /// `Borrowed`.
    pub fn into_vec(self) -> Vec<u8> {
        match self {
            Self::Borrowed(bytes) => bytes.to_vec(),
            Self::Owned(bytes) => bytes,
        }
    }
}

/// Values taken from one route's buffer, by outcome.
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq)]
pub struct RouteStats {
    /// Staged and handed to the connector, including any it then rejected.
    pub sent: u64,
    /// Handed to the connector, which could not send them (for example,
    /// larger than its transport accepts). Reported with
    /// [`OutboundRoutes::reject`].
    pub rejected: u64,
    /// Missed because the reader fell behind.
    pub lagged: u64,
    /// Skipped: the written topic did not fit.
    pub topic_overflow: u64,
    /// Skipped: the serializer failed or reported an invalid length.
    pub serialize_failed: u64,
}

/// What polling one route did.
pub(crate) enum RouteOutcome {
    /// A value was serialized. `topic_len`: bytes written into the topic
    /// scratch, `None` for the default topic.
    Staged {
        topic_len: Option<usize>,
        payload: StagedPayload,
        metadata: crate::WriteMetadata,
    },
    /// The reader lagged by this many values.
    Lagged(u64),
    /// A value was taken; its topic did not fit.
    TopicOverflow,
    /// A value was taken; serializing it failed.
    SerializeFailed(SerializeFailure),
    /// The buffer is gone.
    Closed(DbError),
}

/// Where a staged payload is.
pub(crate) enum StagedPayload {
    /// The first `len` bytes of the payload scratch.
    Scratch(usize),
    Owned(Vec<u8>),
}

/// Why a value that was taken could not be serialized. The caller logs it
/// with its route.
#[derive(Debug)]
pub(crate) enum SerializeFailure {
    /// The owned serializer (`with_serializer`) failed.
    Owned(SerializeError),
    /// `with_serializer_into` failed.
    Into(SerializeError),
    /// The value did not fit `with_serializer_into`'s scratch, and the owned
    /// serializer it fell back to failed.
    Fallback(SerializeError),
    /// `with_serializer_into` reported more bytes than its scratch holds.
    InvalidLength { len: usize, capacity: usize },
}

// Through `Display`: the variants carry a `SerializeError` and lengths, and
// one rendering keeps the defmt line identical to the tracing/log one.
#[cfg(feature = "defmt")]
impl defmt::Format for SerializeFailure {
    fn format(&self, f: defmt::Formatter) {
        defmt::write!(f, "{}", defmt::Display2Format(self))
    }
}

impl core::fmt::Display for SerializeFailure {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        match self {
            Self::Owned(e) => write!(f, "serializer failed: {e:?}"),
            Self::Into(e) => write!(f, "serializer failed into scratch: {e:?}"),
            Self::Fallback(e) => write!(
                f,
                "value did not fit the scratch and the owned fallback failed: {e:?}"
            ),
            Self::InvalidLength { len, capacity } => write!(
                f,
                "serializer returned invalid length {len} for {capacity}-byte scratch"
            ),
        }
    }
}

/// A route's typed reader, topic writer and serializers, polled through one
/// erased call.
pub(crate) trait PollRoute: Send {
    /// Poll the reader once with `cx`. On a value, write its topic into
    /// `topic` and its payload into `payload` (or an owned `Vec`).
    fn poll_route(
        &mut self,
        cx: &mut Context<'_>,
        ctx: &RuntimeContext,
        topic: &mut [u8],
        payload: &mut [u8],
    ) -> Poll<RouteOutcome>;
}

/// What a link's route factory builds.
pub(crate) struct RouteParts {
    pub(crate) route: Box<dyn PollRoute>,
    pub(crate) topic_capacity: usize,
    pub(crate) payload_capacity: usize,
}

/// Builds a link's [`RouteParts`], subscribing to its record.
pub(crate) type RouteFactoryFn = Arc<dyn Fn(&AimDb) -> RouteParts + Send + Sync>;

struct Staged {
    id: RouteId,
    topic_len: Option<usize>,
    payload: StagedPayload,
    metadata: crate::WriteMetadata,
}

/// Every outbound link of one scheme, read by the connector's transport task.
///
/// Built in the connector's `build()`: cursors start then, so an SPMC ring
/// keeps what is produced before the transport first polls. Only routes
/// whose readers woke are polled, in round-robin order.
pub struct OutboundRoutes {
    routes: Box<[RouteInfo]>,
    states: Box<[Box<dyn PollRoute>]>,
    stats: Box<[RouteStats]>,
    ready: ReadyRoutes,
    /// Topic region (`topic_region` bytes), then payload region.
    scratch: Box<[u8]>,
    topic_region: usize,
    staged: Option<Staged>,
    ctx: RuntimeContext,
}

// Moved into the connector's `Send` transport task.
const _: fn() = || {
    fn assert_send<T: Send>() {}
    assert_send::<OutboundRoutes>();
};

impl OutboundRoutes {
    /// Subscribes every outbound link of `scheme` and allocates the scratch
    /// once: the largest topic capacity plus the largest payload capacity.
    pub fn new(db: &AimDb, scheme: &str) -> DbResult<Self> {
        let mut routes = Vec::new();
        let mut states = Vec::new();

        for (record_index, type_id, link) in db.outbound_links(scheme) {
            let parts = (link.route_factory)(db);
            routes.push(RouteInfo {
                id: routes.len(),
                type_id,
                default_topic: Arc::from(link.url.resource_id()),
                record_index,
                config: ConnectorConfig::from_query(&link.config),
                topic_capacity: parts.topic_capacity,
                payload_capacity: parts.payload_capacity,
            });
            states.push(parts.route);
        }

        let topic_region = routes.iter().map(|r| r.topic_capacity).max().unwrap_or(0);
        let payload_region = routes.iter().map(|r| r.payload_capacity).max().unwrap_or(0);
        Ok(Self {
            stats: alloc::vec![RouteStats::default(); routes.len()].into_boxed_slice(),
            ready: ReadyRoutes::new(routes.len()),
            routes: routes.into_boxed_slice(),
            states: states.into_boxed_slice(),
            scratch: alloc::vec![0; topic_region + payload_region].into_boxed_slice(),
            topic_region,
            staged: None,
            ctx: db.runtime_ctx(),
        })
    }

    /// Routes, for parsing per-route configuration once at build.
    pub fn routes(&self) -> &[RouteInfo] {
        &self.routes
    }

    /// Values taken from route `id`'s buffer so far, by outcome.
    pub fn stats(&self, id: RouteId) -> Option<RouteStats> {
        self.stats.get(id).copied()
    }

    /// Take the next ready value from a route that woke and serialize it into
    /// the scratch. Lends nothing.
    ///
    /// - `Ready(Some(id))`: a message from route `id` is staged; take it with
    ///   [`take_staged`](Self::take_staged). A staged message that was never
    ///   taken is returned again, not replaced.
    /// - `Ready(None)`: every route is closed, or there were none. Final: a
    ///   `select` arm must be disarmed after it.
    /// - `Pending`: no woken route had a value. On Tokio this can also mean
    ///   the task's budget is spent; the task is woken to try again.
    ///
    /// A value leaves its buffer only in a call that returns `Ready`, so this
    /// is safe as a `select` arm. Skipped values (topic overflow, serializer
    /// error) and lag are logged and counted in [`stats`](Self::stats).
    pub fn poll_stage(&mut self, cx: &mut Context<'_>) -> Poll<Option<RouteId>> {
        if let Some(staged) = &self.staged {
            return Poll::Ready(Some(staged.id));
        }
        let Self {
            routes,
            states,
            stats,
            ready,
            scratch,
            topic_region,
            staged,
            ctx,
        } = self;
        let (topic_buf, payload_buf) = scratch.split_at_mut(*topic_region);
        ready.poll_ready(cx, |id, route_cx| {
            let info = &routes[id];
            let topic = &mut topic_buf[..info.topic_capacity];
            let payload = &mut payload_buf[..info.payload_capacity];
            let stats = &mut stats[id];
            match states[id].poll_route(route_cx, ctx, topic, payload) {
                Poll::Pending => Polled::Pending,
                Poll::Ready(RouteOutcome::Staged {
                    topic_len,
                    payload,
                    metadata,
                }) => {
                    stats.sent += 1;
                    *staged = Some(Staged {
                        id,
                        topic_len,
                        payload,
                        metadata,
                    });
                    Polled::Staged
                }
                Poll::Ready(RouteOutcome::Lagged(n)) => {
                    stats.lagged += n;
                    report(OutboundEvent::Lagged {
                        topic: &info.default_topic,
                        skipped: n,
                    });
                    Polled::Skipped
                }
                Poll::Ready(RouteOutcome::TopicOverflow) => {
                    stats.topic_overflow += 1;
                    report(OutboundEvent::TopicOverflow {
                        topic: &info.default_topic,
                        capacity: info.topic_capacity,
                    });
                    Polled::Skipped
                }
                Poll::Ready(RouteOutcome::SerializeFailed(_failure)) => {
                    stats.serialize_failed += 1;
                    report(OutboundEvent::SerializeFailed {
                        topic: &info.default_topic,
                        error: &_failure,
                    });
                    Polled::Skipped
                }
                Poll::Ready(RouteOutcome::Closed(_e)) => {
                    report(OutboundEvent::Closed {
                        topic: &info.default_topic,
                        error: &_e,
                    });
                    Polled::Closed
                }
            }
        })
    }

    /// Lend the staged message and clear it. `None` if nothing is staged.
    pub fn take_staged(&mut self) -> Option<OutboundMessage<'_>> {
        let staged = self.staged.take()?;
        let route = &self.routes[staged.id];
        let (topic_buf, payload_buf) = self.scratch.split_at(self.topic_region);
        let topic = match staged.topic_len {
            // Written through `TopicBuf`, which only ever holds whole `&str`s.
            Some(len) => core::str::from_utf8(&topic_buf[..len]).unwrap_or_default(),
            None => &route.default_topic,
        };
        let payload = match staged.payload {
            StagedPayload::Scratch(len) => OutboundPayload::Borrowed(&payload_buf[..len]),
            StagedPayload::Owned(bytes) => OutboundPayload::Owned(bytes),
        };
        Some(OutboundMessage {
            route,
            topic,
            payload,
            metadata: staged.metadata,
        })
    }

    /// Count a message from route `id` that the connector took but could not
    /// send. The connector logs why; this keeps the count beside the route's
    /// other outcomes.
    pub fn reject(&mut self, id: RouteId) {
        if let Some(stats) = self.stats.get_mut(id) {
            stats.rejected += 1;
        }
    }

    /// [`poll_stage`](Self::poll_stage), then [`take_staged`](Self::take_staged),
    /// for hand-written `poll` code.
    pub fn poll_next(&mut self, cx: &mut Context<'_>) -> Poll<Option<OutboundMessage<'_>>> {
        match self.poll_stage(cx) {
            Poll::Ready(Some(_)) => Poll::Ready(self.take_staged()),
            Poll::Ready(None) => Poll::Ready(None),
            Poll::Pending => Poll::Pending,
        }
    }

    /// The next message, or `None` once every route is closed.
    ///
    /// Cancel-safe: dropping the future before it completes takes nothing.
    pub async fn next(&mut self) -> Option<OutboundMessage<'_>> {
        poll_fn(|cx| self.poll_stage(cx)).await?;
        self.take_staged()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn payload_helpers_cover_both_variants() {
        let scratch = [1u8, 2, 3];
        assert_eq!(OutboundPayload::Borrowed(&scratch).as_slice(), [1, 2, 3]);
        assert_eq!(OutboundPayload::Borrowed(&scratch).into_vec(), [1, 2, 3]);

        let owned = alloc::vec![4u8, 5];
        let ptr = owned.as_ptr();
        let payload = OutboundPayload::Owned(owned);
        assert_eq!(payload.as_slice(), [4, 5]);
        let moved = payload.into_vec();
        assert_eq!(moved.as_ptr(), ptr, "moved, not copied");
    }

    #[tokio::test]
    async fn reject_counts_beside_the_routes_other_outcomes() {
        let (db, _runner) = crate::AimDbBuilder::new()
            .runtime(Arc::new(crate::executor::test_support::NoopRuntimeOps))
            .build()
            .await
            .expect("empty database");
        let mut routes = OutboundRoutes::new(&db, "mqtt").unwrap();
        // No routes: an unknown id is ignored rather than a panic.
        routes.reject(0);
        assert_eq!(routes.stats(0), None);
    }
}