use core::{any::Any, future::Future, hash::Hash, marker::PhantomData, time::Duration};
use std::{
collections::{HashMap, HashSet},
sync::Arc,
time::{Instant, SystemTime},
};
use smol_bytes::Utf8Bytes;
use crate::{
Async, AsyncOperator, Block, Context, Driven, Emission, Emitter, Operator, Resources, Skip,
SkipReason,
};
use super::{
AckMode, Effect, ItemId, NodeName, Rings,
channel::{Inbox, Outbox, Wake},
downstream::{Downstream, Sink},
intake::Intake,
journal::{Attributes, JournalRow, Ledger},
lane::{AsyncLands, Commit, Unwritten, block_on},
lifecycle::{Failure, Lifecycle, LifecycleTap, Observation, Station, Taps},
message::{Delivery, Item, Payload, ack_channel},
observe::{self, TracingLevel},
policy::Policies,
poll::{Clock, PollUpstream, Schedule, Seats, Swept, Unpolled},
registry::{Counting, Registries, RootKey},
window::{Lineage, Outbound, Owed},
};
type Emitted<T> = Vec<Vec<Item<T>>>;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Tally {
processed: usize,
emitted: usize,
skipped: usize,
failed: usize,
refused: usize,
}
impl Tally {
#[inline]
#[must_use]
pub const fn processed(&self) -> usize {
self.processed
}
#[inline]
#[must_use]
pub const fn emitted(&self) -> usize {
self.emitted
}
#[inline]
#[must_use]
pub const fn skipped(&self) -> usize {
self.skipped
}
#[inline]
#[must_use]
pub const fn failed(&self) -> usize {
self.failed
}
#[inline]
#[must_use]
pub const fn refused(&self) -> usize {
self.refused
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Drained {
deliveries: usize,
items: usize,
sweeps: usize,
ticks: usize,
tally: Tally,
}
impl Drained {
#[inline]
#[must_use]
pub const fn deliveries(&self) -> usize {
self.deliveries
}
#[inline]
#[must_use]
pub const fn items(&self) -> usize {
self.items
}
#[inline]
#[must_use]
pub const fn sweeps(&self) -> usize {
self.sweeps
}
#[inline]
#[must_use]
pub const fn ticks(&self) -> usize {
self.ticks
}
#[inline]
#[must_use]
pub const fn tally(&self) -> Tally {
self.tally
}
}
#[derive(Debug, Default)]
struct Verdict {
tally: Tally,
failure: Option<Failure>,
skip: Option<Skip>,
}
impl Verdict {
fn station(&self, effect: Effect) -> Station {
if self.failure.is_some() {
return Station::Failed;
}
if self.tally.emitted == 0 && self.skip.is_some() {
return Station::Skipped;
}
effect.station()
}
fn into_lifecycle(self, effect: Effect) -> Lifecycle<Tally> {
if let Some(failure) = self.failure {
return Lifecycle::Failed(failure);
}
if self.tally.emitted == 0
&& let Some(skip) = self.skip
{
return Lifecycle::Skipped(skip);
}
effect.reached(self.tally)
}
}
#[derive(Debug)]
struct Held {
id: u64,
owed: Owed<ItemId>,
verdict: Verdict,
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
struct HeldItem {
delivery: u64,
key: ItemId,
}
impl HeldItem {
#[inline]
const fn new(delivery: u64, key: ItemId) -> Self {
Self { delivery, key }
}
#[inline]
const fn delivery(&self) -> u64 {
self.delivery
}
#[inline]
const fn into_key(self) -> ItemId {
self.key
}
}
#[derive(Debug)]
struct Descent {
key: ItemId,
produced: Vec<ItemId>,
survived: bool,
refused: bool,
}
impl Descent {
#[inline]
const fn new(key: ItemId, survived: bool) -> Self {
Self {
key,
produced: Vec::new(),
survived,
refused: false,
}
}
#[inline]
const fn is_done(&self) -> bool {
self.survived && !self.refused
}
}
#[derive(Debug)]
struct Staged<T> {
id: u64,
owed: Owed<ItemId>,
verdict: Verdict,
outputs: Vec<T>,
owner: Vec<usize>,
descent: Vec<Descent>,
}
#[derive(Debug)]
struct Pass<T> {
verdict: Verdict,
outputs: Vec<T>,
owner: Vec<usize>,
descent: Vec<Descent>,
receipts: Vec<(ItemId, Box<dyn core::any::Any + Send + Sync>)>,
}
impl<T> Pass<T> {
fn opening(items: usize) -> Self {
Self {
verdict: Verdict::default(),
outputs: Vec::new(),
owner: Vec::new(),
descent: Vec::with_capacity(items),
receipts: Vec::new(),
}
}
fn absorb<E>(&mut self, key: ItemId, mut emitter: Emitter<T>, outcome: Result<(), E>)
where
E: core::error::Error + Send + Sync + 'static,
{
if let Some(receipt) = emitter.take_receipt() {
self.receipts.push((key, receipt));
}
let mine = self.descent.len();
for emission in emitter.drain() {
match emission {
Emission::Emit(output) => {
self.outputs.push(output);
self.owner.push(mine);
}
Emission::Skip(skip) => {
self.verdict.tally.skipped += 1;
if self.verdict.skip.is_none() {
self.verdict.skip = Some(skip);
}
}
}
}
match outcome {
Ok(()) => {
self.verdict.tally.processed += 1;
self.descent.push(Descent::new(key, true));
}
Err(error) => {
self.verdict.tally.failed += 1;
if self.verdict.failure.is_none() {
self.verdict.failure = Some(Failure::new(error));
}
self.descent.push(Descent::new(key, false));
}
}
}
fn staged(self, id: u64, mut owed: Owed<ItemId>) -> Staged<T> {
for (key, receipt) in self.receipts {
owed.receipting(key, receipt);
}
Staged {
id,
owed,
verdict: self.verdict,
outputs: self.outputs,
owner: self.owner,
descent: self.descent,
}
}
}
#[derive(Debug)]
pub struct Resident<
O,
W,
A = (),
L = (),
C = Unpolled,
S = Downstream<<O as Operator>::Out>,
I = Inbox<ItemId, <O as Operator>::In>,
M = Block,
> where
O: Driven<M>,
A: Attributes<<O as Driven<M>>::Out>,
{
name: Utf8Bytes,
operator: O,
intake: I,
downstream: S,
windows: Vec<Outbound<ItemId>>,
lane: W,
ack: AckMode,
effect: Effect,
sweep: Duration,
schedule: Schedule,
context: C,
window: Context,
taps: Taps,
policies: Policies,
registries: Option<Arc<Registries>>,
tracing: TracingLevel,
attribution: A,
ledger: L,
roots: HashMap<ItemId, A::Root>,
lineage: Lineage<HeldItem, ItemId>,
held: HashMap<u64, Held>,
next_held: u64,
next_item: u64,
mode: PhantomData<fn() -> M>,
}
impl<O, I, M> Resident<O, (), (), (), Unpolled, Downstream<<O as Driven<M>>::Out>, I, M>
where
O: Driven<M>,
{
#[must_use]
pub fn new(name: impl Into<Utf8Bytes>, operator: O, intake: I) -> Self {
let name = name.into();
Self {
window: Context::new(name.clone()),
name,
operator,
intake,
downstream: Downstream::new(),
windows: Vec::new(),
lane: (),
ack: AckMode::Local,
effect: Effect::Pure,
sweep: Self::SWEEP,
schedule: Schedule::new(),
context: Unpolled,
taps: Taps::new(),
policies: Policies::new(),
registries: None,
tracing: TracingLevel::Info,
attribution: (),
ledger: (),
roots: HashMap::new(),
lineage: Lineage::new(),
held: HashMap::new(),
next_held: 0,
next_item: 0,
mode: PhantomData,
}
}
}
impl<O, W, A, L, C, I, M> Resident<O, W, A, L, C, Downstream<<O as Driven<M>>::Out>, I, M>
where
O: Driven<M>,
A: Attributes<<O as Driven<M>>::Out>,
{
#[must_use]
pub fn publish(mut self, outbox: Outbox<ItemId, <O as Driven<M>>::Out>) -> Self {
self.downstream.attach(outbox);
self.windows.push(Outbound::new());
self
}
}
impl<O, W, A, L, C, S, I, M> Resident<O, W, A, L, C, S, I, M>
where
O: Driven<M>,
A: Attributes<<O as Driven<M>>::Out>,
{
pub const SWEEP: Duration = Duration::from_secs(1);
#[must_use]
pub fn land<X>(self, lane: X) -> Resident<O, X, A, L, C, S, I, M> {
Resident {
name: self.name,
operator: self.operator,
intake: self.intake,
downstream: self.downstream,
windows: self.windows,
lane,
ack: self.ack,
effect: Effect::Lands,
sweep: self.sweep,
schedule: self.schedule,
context: self.context,
window: self.window,
taps: self.taps,
policies: self.policies,
registries: self.registries,
tracing: self.tracing,
attribution: self.attribution,
ledger: self.ledger,
roots: self.roots,
lineage: self.lineage,
held: self.held,
next_held: self.next_held,
next_item: self.next_item,
mode: self.mode,
}
}
#[must_use]
pub fn recording_to<X>(self, ledger: X) -> Resident<O, W, A, X, C, S, I, M> {
Resident {
name: self.name,
operator: self.operator,
intake: self.intake,
downstream: self.downstream,
windows: self.windows,
lane: self.lane,
ack: self.ack,
effect: self.effect,
sweep: self.sweep,
schedule: self.schedule,
context: self.context,
window: self.window,
taps: self.taps,
policies: self.policies,
registries: self.registries,
tracing: self.tracing,
attribution: self.attribution,
ledger,
roots: self.roots,
lineage: self.lineage,
held: self.held,
next_held: self.next_held,
next_item: self.next_item,
mode: self.mode,
}
}
#[must_use]
pub fn attributing<B>(self, attribution: B) -> Resident<O, W, B, L, C, S, I, M>
where
B: Attributes<<O as Driven<M>>::Out>,
{
Resident {
name: self.name,
operator: self.operator,
intake: self.intake,
downstream: self.downstream,
windows: self.windows,
lane: self.lane,
ack: self.ack,
effect: self.effect,
sweep: self.sweep,
schedule: self.schedule,
context: self.context,
window: self.window,
taps: self.taps,
policies: self.policies,
registries: self.registries,
tracing: self.tracing,
attribution,
ledger: self.ledger,
roots: HashMap::new(),
lineage: self.lineage,
held: self.held,
next_held: self.next_held,
next_item: self.next_item,
mode: self.mode,
}
}
#[must_use]
pub fn polling<X>(self, schedule: Schedule, context: X) -> Resident<O, W, A, L, X, S, I, M>
where
X: Seats<O>,
{
Resident {
name: self.name,
operator: self.operator,
intake: self.intake,
downstream: self.downstream,
windows: self.windows,
lane: self.lane,
ack: self.ack,
effect: self.effect,
sweep: self.sweep,
schedule,
context,
window: self.window,
taps: self.taps,
policies: self.policies,
registries: self.registries,
tracing: self.tracing,
attribution: self.attribution,
ledger: self.ledger,
roots: self.roots,
lineage: self.lineage,
held: self.held,
next_held: self.next_held,
next_item: self.next_item,
mode: self.mode,
}
}
#[must_use]
pub fn wire<X>(self, downstream: X) -> Resident<O, W, A, L, C, X, I, M>
where
X: Sink<<O as Driven<M>>::Out>,
{
Resident {
windows: core::iter::repeat_with(Outbound::new)
.take(downstream.arity())
.collect(),
downstream,
name: self.name,
operator: self.operator,
intake: self.intake,
lane: self.lane,
ack: self.ack,
effect: self.effect,
sweep: self.sweep,
schedule: self.schedule,
context: self.context,
window: self.window,
taps: self.taps,
policies: self.policies,
registries: self.registries,
tracing: self.tracing,
attribution: self.attribution,
ledger: self.ledger,
roots: self.roots,
lineage: self.lineage,
held: self.held,
next_held: self.next_held,
next_item: self.next_item,
mode: self.mode,
}
}
#[inline]
#[must_use]
pub const fn with_ack(mut self, ack: AckMode) -> Self {
self.set_ack(ack);
self
}
#[inline]
pub const fn set_ack(&mut self, ack: AckMode) -> &mut Self {
self.ack = ack;
self
}
#[inline]
#[must_use]
pub const fn with_effect(mut self, effect: Effect) -> Self {
self.set_effect(effect);
self
}
#[inline]
pub const fn set_effect(&mut self, effect: Effect) -> &mut Self {
self.effect = effect;
self
}
#[inline]
#[must_use]
pub const fn with_sweep(mut self, sweep: Duration) -> Self {
self.set_sweep(sweep);
self
}
#[inline]
pub const fn set_sweep(&mut self, sweep: Duration) -> &mut Self {
self.sweep = sweep;
self
}
#[inline]
#[must_use]
pub fn with_resources(mut self, resources: Arc<Resources>) -> Self {
self.set_resources(resources);
self
}
#[inline]
pub fn set_resources(&mut self, resources: Arc<Resources>) -> &mut Self {
self.window.set_resources(resources);
self
}
#[inline]
#[must_use]
pub fn with_shutdown(mut self, shutdown: crate::ShutdownSignal) -> Self {
self.set_shutdown(shutdown);
self
}
#[inline]
pub fn set_shutdown(&mut self, shutdown: crate::ShutdownSignal) -> &mut Self {
self.window.set_shutdown(shutdown);
self
}
#[inline]
#[must_use]
pub fn with_taps(mut self, taps: Taps) -> Self {
self.set_taps(taps);
self
}
#[inline]
pub fn set_taps(&mut self, taps: Taps) -> &mut Self {
self.taps = taps;
self
}
#[inline]
#[must_use]
pub fn with_policies(mut self, policies: Policies) -> Self {
self.set_policies(policies);
self
}
#[inline]
pub fn set_policies(&mut self, policies: Policies) -> &mut Self {
self.policies = policies;
self
}
#[inline]
#[must_use]
pub const fn policies(&self) -> &Policies {
&self.policies
}
#[inline]
#[must_use]
pub fn with_registries(mut self, registries: Arc<Registries>) -> Self {
self.set_registries(registries);
self
}
#[inline]
pub fn set_registries(&mut self, registries: Arc<Registries>) -> &mut Self {
self.registries = Some(registries);
self
}
#[inline]
#[must_use]
pub fn registries(&self) -> Option<&Registries> {
self.registries.as_deref()
}
#[inline]
#[must_use]
pub const fn with_tracing_level(mut self, level: TracingLevel) -> Self {
self.tracing = level;
self
}
#[inline]
#[must_use]
pub const fn tracing_level(&self) -> TracingLevel {
self.tracing
}
#[inline]
#[must_use]
pub fn name(&self) -> &NodeName {
NodeName::new(&self.name)
}
#[inline]
#[must_use]
pub fn into_operator(self) -> O {
self.operator
}
#[inline]
#[must_use]
pub const fn operator(&self) -> &O {
&self.operator
}
#[inline]
#[must_use]
pub const fn ack(&self) -> AckMode {
self.ack
}
#[inline]
#[must_use]
pub const fn effect(&self) -> Effect {
self.effect
}
#[inline]
#[must_use]
pub const fn sweep(&self) -> Duration {
self.sweep
}
#[inline]
#[must_use]
pub const fn taps(&self) -> &Taps {
&self.taps
}
#[inline]
#[must_use]
pub const fn schedule(&self) -> &Schedule {
&self.schedule
}
#[inline]
#[must_use]
pub const fn context(&self) -> &C {
&self.context
}
#[inline]
#[must_use]
pub fn resources(&self) -> Option<&Resources> {
self.window.resources()
}
#[inline]
#[must_use]
pub const fn window(&self) -> &Context {
&self.window
}
#[inline]
#[must_use]
pub const fn lane(&self) -> &W {
&self.lane
}
#[inline]
#[must_use]
pub const fn attribution(&self) -> &A {
&self.attribution
}
#[inline]
#[must_use]
pub const fn ledger(&self) -> &L {
&self.ledger
}
#[must_use]
pub fn nudges(&self) -> Vec<Box<dyn Rings>>
where
S: Sink<<O as Driven<M>>::Out>,
{
self.downstream.nudges()
}
#[inline]
#[must_use]
pub const fn downstream(&self) -> &S {
&self.downstream
}
#[must_use]
pub fn outstanding(&self) -> usize {
self.windows.iter().map(Outbound::outstanding).sum()
}
#[inline]
#[must_use]
pub fn holding(&self) -> usize {
self.held.len()
}
}
impl<O, W, A, L, C, S, I, M> Resident<O, W, A, L, C, S, I, M>
where
O: Driven<M>,
C: Seats<O>,
<O as Driven<M>>::Out: Clone,
S: Sink<<O as Driven<M>>::Out>,
I: Intake<<O as Driven<M>>::In>,
<O as Driven<M>>::Error: core::error::Error + Send + Sync + 'static,
<C as Seats<O>>::Error: core::error::Error + Send + Sync + 'static,
<C as Seats<O>>::Info: Any + Send + Sync,
W: AsyncLands<<O as Driven<M>>::Out>,
W::Error: core::error::Error + Send + Sync + 'static,
A: Attributes<<O as Driven<M>>::Out>,
A::Root: RootKey,
L: Ledger,
L::Row: JournalRow<A::Root>,
L::Error: core::error::Error + Send + Sync + 'static,
{
fn counting(&self) -> Option<Counting<A::Root>> {
Counting::open(self.registries.as_ref(), A::FAMILY)
}
fn landing(&mut self, staged: Staged<<O as Driven<M>>::Out>, report: &mut Drained) {
let commit = {
let _entered =
observe::commit(self.tracing, self.name(), self.effect, staged.outputs.len()).entered();
block_on(self.lane.land(&staged.outputs))
};
let (batches, mut held, journal) = self.reconcile(staged, commit, report);
let recorded = self.ledger.record(&journal);
Self::sealed(&mut held, recorded);
block_on(self.dispatch(batches));
self.settle(held);
}
async fn landing_async(&mut self, staged: Staged<<O as Driven<M>>::Out>, report: &mut Drained) {
let commit = observe::commit(self.tracing, self.name(), self.effect, staged.outputs.len())
.over(self.lane.land(&staged.outputs))
.await;
let (batches, mut held, journal) = self.reconcile(staged, commit, report);
let recorded = self.ledger.record_async(&journal).await;
Self::sealed(&mut held, recorded);
self.dispatch(batches).await;
self.settle(held);
}
fn collect(
&mut self,
first: Wake<ItemId, <O as Driven<M>>::In>,
report: &mut Drained,
ticked: &mut Vec<PollUpstream>,
) -> Vec<Delivery<<O as Driven<M>>::In>> {
let mut wakes = vec![first];
self.intake.drain(&mut wakes);
let mut deliveries = Vec::new();
let mut swept = false;
for wake in wakes {
match wake {
Wake::Delivered(delivery) => deliveries.push(delivery),
Wake::Sweep => swept = true,
Wake::Tick(tick) => ticked.push(tick.upstream()),
}
}
if swept {
report.sweeps += 1;
}
report.deliveries += deliveries.len();
deliveries
}
fn looks(mut ticked: Vec<PollUpstream>) -> Vec<PollUpstream> {
ticked.sort_unstable();
ticked.dedup();
ticked
}
async fn swept(&mut self, upstream: PollUpstream) {
let Self {
operator,
context,
effect,
..
} = self;
let observation = match context.seat(operator, upstream).await {
Ok(Swept::Ran(info)) => effect.reached(info).erase(),
Ok(Swept::Dispatched) => return,
Ok(Swept::Lapped) => Observation::Lapped { poll: upstream },
Err(error) => Lifecycle::<<C as Seats<O>>::Info>::Failed(Failure::new(error)).erase(),
};
self.taps.observe(NodeName::new(&self.name), &observation);
}
fn reconcile(
&mut self,
staged: Staged<<O as Driven<M>>::Out>,
commit: Commit<W::Error>,
report: &mut Drained,
) -> (Emitted<<O as Driven<M>>::Out>, Held, Vec<L::Row>) {
let Staged {
id,
mut owed,
mut verdict,
outputs,
owner,
mut descent,
} = staged;
let mut batches: Emitted<<O as Driven<M>>::Out> = core::iter::repeat_with(Vec::new)
.take(self.downstream.arity())
.collect();
let attempted = outputs.len();
let mut committed = 0usize;
let mut landed: Vec<A::Root> = Vec::new();
let mut seen: HashSet<A::Root> = HashSet::new();
let counting = if attempted == 0 {
None
} else {
self.counting()
};
for (position, output) in outputs.into_iter().enumerate() {
let mine = owner[position];
if commit.committed(position) {
committed += 1;
let root = self.attribution.root(&output);
if seen.insert(root.clone()) {
landed.push(root.clone());
}
self.fan_out(
output,
root,
&mut batches,
&mut descent[mine].produced,
&mut verdict,
counting.as_ref(),
);
} else {
verdict.tally.refused += 1;
descent[mine].refused = true;
}
}
if committed < attempted
&& verdict.failure.is_none()
&& let Some(refusal) = commit.into_refusal()
{
verdict.failure = Some(Failure::new(Unwritten::new(attempted, committed, refusal)));
}
match self.ack {
AckMode::Local => {
for item in descent {
if item.is_done() {
owed.release(item.key);
}
}
}
AckMode::Chained => {
for item in descent {
if !item.is_done() {
continue;
}
if self
.lineage
.expect(HeldItem::new(id, item.key), item.produced)
{
owed.release(item.key);
}
}
}
}
report.tally.processed += verdict.tally.processed;
report.tally.emitted += verdict.tally.emitted;
report.tally.skipped += verdict.tally.skipped;
report.tally.failed += verdict.tally.failed;
report.tally.refused += verdict.tally.refused;
let journal = if verdict.station(self.effect).is_persisted() {
let now = SystemTime::now();
landed
.iter()
.map(|root| L::Row::of(root, NodeName::new(&self.name), &Self::completed(), now))
.collect()
} else {
Vec::new()
};
(batches, Held { id, owed, verdict }, journal)
}
fn completed() -> super::Arrival {
Lifecycle::<()>::Persisted(()).arrive()
}
fn sealed(held: &mut Held, recorded: Result<(), L::Error>) {
if let Err(refusal) = recorded
&& held.verdict.failure.is_none()
{
held.verdict.failure = Some(Failure::new(refusal));
}
}
fn fan_out(
&mut self,
output: <O as Driven<M>>::Out,
root: A::Root,
batches: &mut [Vec<Item<<O as Driven<M>>::Out>>],
produced: &mut Vec<ItemId>,
verdict: &mut Verdict,
counting: Option<&Counting<A::Root>>,
) {
let last = (0..batches.len())
.rev()
.find(|edge| self.downstream.carries(*edge, &output));
let Some(last) = last else {
if self.effect.is_pure() {
verdict.tally.skipped += 1;
if verdict.skip.is_none() {
verdict.skip = Some(Skip::new(SkipReason::NoConsumers));
}
}
return;
};
verdict.tally.emitted += 1;
let mut value = Some(output);
for (edge, batch) in batches.iter_mut().enumerate().take(last + 1) {
let held = value
.as_ref()
.expect("the value is taken only at the last edge");
if edge != last && !self.downstream.carries(edge, held) {
continue;
}
let id = ItemId::new(self.next_item);
self.next_item += 1;
let copy = if edge == last {
value.take().expect("taken exactly once")
} else {
held.clone()
};
batch.push(Item::new(id, copy));
self.roots.insert(id, root.clone());
if let Some(counting) = counting {
counting.dispatched(&root);
}
produced.push(id);
}
}
async fn dispatch(&mut self, batches: Emitted<<O as Driven<M>>::Out>) {
for (edge, batch) in batches.into_iter().enumerate() {
if batch.is_empty() {
continue;
}
let ids: Vec<ItemId> = batch.iter().map(|item| *item.key()).collect();
let (ack, answers) = ack_channel();
if self.downstream.deliver(edge, batch, ack).await {
self.windows[edge].record(ids, answers);
} else {
self.hung_up(&ids);
}
}
}
fn hung_up(&mut self, ids: &[ItemId]) {
let observation = Lifecycle::<Tally>::Skipped(Skip::new(SkipReason::ServiceInactive)).erase();
self.taps.observe(NodeName::new(&self.name), &observation);
let counting = self.counting();
for id in ids {
let credited = self.roots.remove(id);
if let (Some(counting), Some(root)) = (counting.as_ref(), credited) {
counting.observed(&root);
}
if self.ack.is_chained() {
self.lineage.retract(id);
}
}
}
fn harvest(&mut self) {
let counting = if self.roots.is_empty() {
None
} else {
self.counting()
};
let taps = &self.taps;
let attribution = &self.attribution;
let roots = &mut self.roots;
let mut released: Vec<ItemId> = Vec::new();
for window in &mut self.windows {
for answer in window.harvest().into_answers() {
taps.observe(answer.worker(), answer.observation());
released.extend(answer.released().iter().copied());
let Some(arrival) = answer.observation().arrival() else {
continue;
};
for id in answer.covered() {
if let Some(root) = roots.remove(id) {
attribution.observe(&root, answer.worker(), arrival);
if let Some(counting) = counting.as_ref() {
counting.observed(&root);
}
}
}
}
}
if !self.ack.is_chained() {
return;
}
for id in released {
let Some(parent) = self.lineage.retire(&id) else {
continue;
};
let delivery = parent.delivery();
let Some(entry) = self.held.get_mut(&delivery) else {
continue;
};
entry.owed.release(parent.into_key());
if entry.owed.is_complete() {
let mut entry = self.held.remove(&delivery).expect("just borrowed it");
let lifecycle = core::mem::take(&mut entry.verdict).into_lifecycle(self.effect);
entry.owed.settle(self.name.clone(), lifecycle);
}
}
}
fn settle(&mut self, mut held: Held) {
if self.ack.is_local() || held.owed.is_complete() {
let lifecycle = core::mem::take(&mut held.verdict).into_lifecycle(self.effect);
held.owed.settle(self.name.clone(), lifecycle);
return;
}
self.held.insert(held.id, held);
}
fn finish(&mut self) {
self.harvest();
let effect = self.effect;
let name = self.name.clone();
for (_, mut held) in self.held.drain() {
let lifecycle = core::mem::take(&mut held.verdict).into_lifecycle(effect);
held.owed.settle(name.clone(), lifecycle);
}
}
}
#[diagnostic::on_unimplemented(
message = "`{Self}` has no engine",
label = "this resident cannot be run",
note = "a resident is drivable when every seam around its operator is satisfied: the downstream \
set is a `Sink` of the product, the intake an `Intake` of the message, the lane an \
`AsyncLands`, the poll context a `Seats`, the ledger a `Ledger`, and the operator's own \
error, the sweep's error and info, and the attribution's root carry the driver's \
erasure bounds",
note = "and the FACE has to match: `Resident<…, Block>` is driven for an `Operator` and \
`Resident<…, Async>` for an `AsyncOperator`"
)]
pub trait Drives {
const SYNC: bool;
fn run(&mut self) -> Drained;
fn run_async(&mut self) -> impl Future<Output = Drained>;
}
impl<O, W, A, L, C, S, I> Resident<O, W, A, L, C, S, I, Block>
where
O: Operator,
A: Attributes<<O as Operator>::Out>,
S: Sink<<O as Operator>::Out>,
<O as Operator>::Error: core::error::Error + Send + Sync + 'static,
{
fn process(
&mut self,
delivery: Delivery<<O as Operator>::In>,
report: &mut Drained,
) -> Staged<<O as Operator>::Out> {
let (payload, ack) = delivery.into_parts();
report.items += payload.len();
let owed = Owed::open(payload.keys().copied(), ack);
let mut pass = Pass::opening(payload.len());
let Self {
operator,
window,
downstream,
..
} = self;
let wanted = downstream.connected();
for item in payload {
let key = *item.key();
let mut emitter = Emitter::new().wanting(wanted);
let outcome = operator.accept(
window,
Delivery::in_custody(Payload::One(item)),
&mut emitter,
);
pass.absorb(key, emitter, outcome);
}
let id = self.next_held;
self.next_held += 1;
pass.staged(id, owed)
}
}
impl<O, W, A, L, C, S, I> Resident<O, W, A, L, C, S, I, Async>
where
O: AsyncOperator,
A: Attributes<<O as AsyncOperator>::Out>,
S: Sink<<O as AsyncOperator>::Out>,
<O as AsyncOperator>::Error: core::error::Error + Send + Sync + 'static,
{
async fn process(
&mut self,
delivery: Delivery<<O as AsyncOperator>::In>,
report: &mut Drained,
) -> Staged<<O as AsyncOperator>::Out> {
let (payload, ack) = delivery.into_parts();
report.items += payload.len();
let owed = Owed::open(payload.keys().copied(), ack);
let mut pass = Pass::opening(payload.len());
let Self {
operator,
window,
downstream,
..
} = self;
let wanted = downstream.connected();
for item in payload {
let key = *item.key();
let mut emitter = Emitter::new().wanting(wanted);
let outcome = operator
.accept(
window,
Delivery::in_custody(Payload::One(item)),
&mut emitter,
)
.await;
pass.absorb(key, emitter, outcome);
}
let id = self.next_held;
self.next_held += 1;
pass.staged(id, owed)
}
}
impl<O, W, A, L, C, S, I> Drives for Resident<O, W, A, L, C, S, I, Block>
where
O: Operator,
C: Seats<O>,
<O as Operator>::Out: Clone,
S: Sink<<O as Operator>::Out>,
I: Intake<<O as Operator>::In>,
<O as Operator>::Error: core::error::Error + Send + Sync + 'static,
<C as Seats<O>>::Error: core::error::Error + Send + Sync + 'static,
<C as Seats<O>>::Info: Any + Send + Sync,
W: AsyncLands<<O as Operator>::Out>,
W::Error: core::error::Error + Send + Sync + 'static,
A: Attributes<<O as Operator>::Out>,
A::Root: RootKey,
L: Ledger,
L::Row: JournalRow<A::Root>,
L::Error: core::error::Error + Send + Sync + 'static,
{
const SYNC: bool = <O as Driven<Block>>::SYNC;
fn run(&mut self) -> Drained {
let mut report = Drained::default();
let mut clock = Clock::start(&self.schedule, Instant::now());
let ticks = self.policies.ticks(self.name.clone(), self.taps.clone());
loop {
let Some(wake) = self.intake.park(clock.park_for(self.sweep, Instant::now())) else {
break;
};
let mut ticked = Vec::new();
for delivery in self.collect(wake, &mut report, &mut ticked) {
let batch = observe::batch(self.tracing, self.name(), delivery.payload().len());
let staged = {
let _entered = batch.entered();
self.process(delivery, &mut report)
};
self.landing(staged, &mut report);
}
let mut looks = Self::looks(ticked);
clock.take_due(Instant::now(), &mut looks, &ticks);
for upstream in looks {
let _entered = observe::tick(self.tracing, self.name(), upstream).entered();
block_on(self.swept(upstream));
report.ticks += 1;
}
self.harvest();
}
self.finish();
report
}
async fn run_async(&mut self) -> Drained {
let mut report = Drained::default();
while let Some(wake) = self.intake.park_async().await {
let mut ticked = Vec::new();
for delivery in self.collect(wake, &mut report, &mut ticked) {
let batch = observe::batch(self.tracing, self.name(), delivery.payload().len());
let staged = {
let _entered = batch.entered();
self.process(delivery, &mut report)
};
self.landing_async(staged, &mut report).await;
}
for upstream in Self::looks(ticked) {
observe::tick(self.tracing, self.name(), upstream)
.over(self.swept(upstream))
.await;
report.ticks += 1;
}
self.harvest();
}
self.finish();
report
}
}
impl<O, W, A, L, C, S, I> Drives for Resident<O, W, A, L, C, S, I, Async>
where
O: AsyncOperator,
C: Seats<O>,
<O as AsyncOperator>::Out: Clone,
S: Sink<<O as AsyncOperator>::Out>,
I: Intake<<O as AsyncOperator>::In>,
<O as AsyncOperator>::Error: core::error::Error + Send + Sync + 'static,
<C as Seats<O>>::Error: core::error::Error + Send + Sync + 'static,
<C as Seats<O>>::Info: Any + Send + Sync,
W: AsyncLands<<O as AsyncOperator>::Out>,
W::Error: core::error::Error + Send + Sync + 'static,
A: Attributes<<O as AsyncOperator>::Out>,
A::Root: RootKey,
L: Ledger,
L::Row: JournalRow<A::Root>,
L::Error: core::error::Error + Send + Sync + 'static,
{
const SYNC: bool = <O as Driven<Async>>::SYNC;
fn run(&mut self) -> Drained {
let mut report = Drained::default();
let mut clock = Clock::start(&self.schedule, Instant::now());
let ticks = self.policies.ticks(self.name.clone(), self.taps.clone());
loop {
let Some(wake) = self.intake.park(clock.park_for(self.sweep, Instant::now())) else {
break;
};
let mut ticked = Vec::new();
for delivery in self.collect(wake, &mut report, &mut ticked) {
let batch = observe::batch(self.tracing, self.name(), delivery.payload().len());
let staged = {
let _entered = batch.entered();
block_on(self.process(delivery, &mut report))
};
self.landing(staged, &mut report);
}
let mut looks = Self::looks(ticked);
clock.take_due(Instant::now(), &mut looks, &ticks);
for upstream in looks {
let _entered = observe::tick(self.tracing, self.name(), upstream).entered();
block_on(self.swept(upstream));
report.ticks += 1;
}
self.harvest();
}
self.finish();
report
}
async fn run_async(&mut self) -> Drained {
let mut report = Drained::default();
while let Some(wake) = self.intake.park_async().await {
let mut ticked = Vec::new();
for delivery in self.collect(wake, &mut report, &mut ticked) {
let batch = observe::batch(self.tracing, self.name(), delivery.payload().len());
let staged = batch.over(self.process(delivery, &mut report)).await;
self.landing_async(staged, &mut report).await;
}
for upstream in Self::looks(ticked) {
observe::tick(self.tracing, self.name(), upstream)
.over(self.swept(upstream))
.await;
report.ticks += 1;
}
self.harvest();
}
self.finish();
report
}
}
#[cfg(test)]
mod inflight_tests;
#[cfg(test)]
mod matrix_tests;
#[cfg(test)]
mod tests;