use std::collections::{HashMap, HashSet, VecDeque, hash_map::Entry};
use core::{any::Any, hash::Hash};
use smol_bytes::Utf8Bytes;
use crate::operator::Receipts;
use super::{
NodeName,
fate::Fate,
lifecycle::{Lifecycle, Observation},
message::{Ack, AckReceiver, Answer, Custody, ReceiptMap, Unreceipted},
};
#[derive(Debug)]
struct Tracked<K> {
key: K,
released: bool,
}
#[derive(Debug)]
struct Flight<K> {
items: Vec<Tracked<K>>,
ack: Option<AckReceiver<K>>,
}
#[derive(Debug)]
pub struct Answered<K> {
worker: Utf8Bytes,
observation: Observation,
message: Option<Box<dyn Any + Send + Sync>>,
receipts: ReceiptMap<K>,
covered: Vec<K>,
released: Vec<K>,
}
impl<K> Answered<K> {
#[inline]
#[must_use]
pub fn worker(&self) -> &NodeName {
NodeName::new(&self.worker)
}
#[inline]
#[must_use]
pub const fn observation(&self) -> &Observation {
&self.observation
}
#[inline]
#[must_use]
pub fn message<M: Any>(&self) -> Option<&M> {
self
.message
.as_ref()
.and_then(|message| message.downcast_ref())
}
#[inline]
pub fn receipt<R: Any>(&self, key: &K) -> Result<&R, Unreceipted>
where
K: Eq + Hash,
{
let receipt = self
.receipts
.as_ref()
.and_then(|receipts| receipts.get(key))
.ok_or(Unreceipted::Silent)?;
receipt.downcast_ref().ok_or(Unreceipted::Mismatched)
}
#[inline]
pub fn receipt_of<M>(&self, key: &K) -> Result<&M::Receipt, Unreceipted>
where
K: Eq + Hash,
M: Receipts,
M::Receipt: Any,
{
self.receipt::<M::Receipt>(key)
}
#[inline]
#[must_use]
pub fn covered(&self) -> &[K] {
&self.covered
}
#[inline]
#[must_use]
pub fn released(&self) -> &[K] {
&self.released
}
}
#[derive(Debug)]
pub struct Harvest<K> {
answers: Vec<Answered<K>>,
}
impl<K> Harvest<K> {
#[inline]
#[must_use]
pub fn answers(&self) -> &[Answered<K>] {
&self.answers
}
#[inline]
pub fn pairs(&self) -> impl Iterator<Item = (&K, &Answered<K>)> {
self
.answers
.iter()
.flat_map(|answer| answer.covered.iter().map(move |key| (key, answer)))
}
#[inline]
pub fn released(&self) -> impl Iterator<Item = &K> {
self
.answers
.iter()
.flat_map(|answer| answer.released.iter())
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.answers.is_empty()
}
#[inline]
#[must_use]
pub fn into_answers(self) -> Vec<Answered<K>> {
self.answers
}
}
#[derive(Debug)]
pub struct Outbound<K> {
flights: VecDeque<Flight<K>>,
position: Option<K>,
lost: usize,
}
impl<K> Outbound<K> {
#[inline]
#[must_use]
pub const fn new() -> Self {
Self {
flights: VecDeque::new(),
position: None,
lost: 0,
}
}
#[inline]
#[must_use]
pub fn with_capacity(deliveries: usize) -> Self {
Self {
flights: VecDeque::with_capacity(deliveries),
..Self::new()
}
}
pub fn record(&mut self, keys: impl IntoIterator<Item = K>, ack: AckReceiver<K>) {
self.flights.push_back(Flight {
items: keys
.into_iter()
.map(|key| Tracked {
key,
released: false,
})
.collect(),
ack: Some(ack),
});
}
#[inline]
#[must_use]
pub const fn position(&self) -> Option<&K> {
self.position.as_ref()
}
#[inline]
#[must_use]
pub const fn lost(&self) -> usize {
self.lost
}
#[must_use]
pub fn outstanding(&self) -> usize {
self
.flights
.iter()
.map(|flight| flight.items.iter().filter(|item| !item.released).count())
.sum()
}
#[inline]
#[must_use]
pub fn is_drained(&self) -> bool {
self.flights.is_empty()
}
pub fn harvest(&mut self) -> Harvest<K>
where
K: Clone + Eq + Hash,
{
let mut answered: HashMap<K, usize> = HashMap::new();
let mut answers: Vec<Answered<K>> = Vec::new();
for flight in &mut self.flights {
let Some(ack) = flight.ack.as_ref() else {
continue;
};
match ack.take() {
Answer::Settled(settled) => {
let (keys, worker, observation, message, receipts) = settled.into_parts();
let at = answers.len();
let named = keys.len();
for key in keys {
answered.insert(key, at);
}
answers.push(Answered {
worker,
observation,
message,
receipts,
covered: flight.items.iter().map(|item| item.key.clone()).collect(),
released: Vec::with_capacity(named),
});
flight.ack = None;
}
Answer::Waiting => {}
Answer::HungUp => {
flight.ack = None;
self.lost += 1;
}
}
}
if !answered.is_empty() {
for flight in &mut self.flights {
for item in &mut flight.items {
if item.released {
continue;
}
if let Some(&at) = answered.get(&item.key) {
item.released = true;
answers[at].released.push(item.key.clone());
}
}
}
}
self.advance();
Harvest { answers }
}
fn advance(&mut self) {
while let Some(flight) = self.flights.front_mut() {
let crossed = flight
.items
.iter()
.position(|item| !item.released)
.unwrap_or(flight.items.len());
if let Some(last) = flight.items.drain(..crossed).next_back() {
self.position = Some(last.key);
}
if !flight.items.is_empty() {
return;
}
self.flights.pop_front();
}
}
pub fn rewind(&mut self) -> Vec<K> {
let retracted = core::mem::take(&mut self.flights)
.into_iter()
.flat_map(|flight| flight.items.into_iter().map(|item| item.key))
.collect();
self.lost = 0;
retracted
}
}
impl<K> Default for Outbound<K> {
#[inline]
fn default() -> Self {
Self::new()
}
}
#[derive(Debug)]
pub struct Owed<K> {
ack: Option<Custody<K>>,
pending: HashSet<K>,
released: HashSet<K>,
receipts: ReceiptMap<K>,
}
impl<K> Owed<K> {
pub fn open(keys: impl IntoIterator<Item = K>, ack: Option<Custody<K>>) -> Self
where
K: Eq + Hash,
{
Self {
ack,
pending: keys.into_iter().collect(),
released: HashSet::new(),
receipts: None,
}
}
pub fn release(&mut self, key: K)
where
K: Eq + Hash,
{
if self.pending.remove(&key) {
self.released.insert(key);
}
}
pub fn receipt<R>(&mut self, key: K, receipt: R)
where
K: Eq + Hash,
R: Any + Send + Sync,
{
self
.receipts
.get_or_insert_with(Box::default)
.insert(key, Box::new(receipt));
}
pub fn receipt_of<M>(&mut self, key: K, receipt: M::Receipt)
where
K: Eq + Hash,
M: Receipts,
M::Receipt: Any + Send + Sync,
{
self.receipt(key, receipt);
}
pub(crate) fn receipting(&mut self, key: K, receipt: Box<dyn Any + Send + Sync>)
where
K: Eq + Hash,
{
self
.receipts
.get_or_insert_with(Box::default)
.insert(key, receipt);
}
pub fn withhold(&mut self, key: &K)
where
K: Eq + Hash,
{
self.pending.remove(key);
}
#[inline]
#[must_use]
pub fn is_complete(&self) -> bool {
self.pending.is_empty()
}
#[inline]
#[must_use]
pub const fn is_settled(&self) -> bool {
self.ack.is_none()
}
#[inline]
#[must_use]
pub fn held(&self) -> usize {
self.pending.len()
}
pub fn settle<I>(&mut self, worker: impl Into<Utf8Bytes>, lifecycle: Lifecycle<I>) -> bool
where
K: Eq + Hash,
I: core::any::Any + Clone + Send + Sync,
{
let Some(ack) = self.ack.take() else {
return false;
};
ack.send(
Ack::new(core::mem::take(&mut self.released), worker, lifecycle)
.carrying(self.receipts.take()),
)
}
pub fn settle_fate<M>(&mut self, worker: impl Into<Utf8Bytes>, fate: Fate<M>) -> bool
where
K: Eq + Hash,
M: Any + Clone + Send + Sync,
{
let Some(ack) = self.ack.take() else {
return false;
};
let credited: HashSet<K> = self.released.drain().chain(self.pending.drain()).collect();
ack.fate(credited, worker, fate)
}
}
#[derive(Debug)]
pub struct Lineage<In, Out> {
parent: HashMap<Out, In>,
outstanding: HashMap<In, usize>,
}
impl<In, Out> Lineage<In, Out> {
#[inline]
#[must_use]
pub fn new() -> Self {
Self {
parent: HashMap::new(),
outstanding: HashMap::new(),
}
}
pub fn expect(&mut self, input: In, outputs: impl IntoIterator<Item = Out>) -> bool
where
In: Eq + Hash + Clone,
Out: Eq + Hash,
{
let mut count = 0usize;
for output in outputs {
self.parent.insert(output, input.clone());
count += 1;
}
if count == 0 {
return true;
}
*self.outstanding.entry(input).or_insert(0) += count;
false
}
pub fn retire(&mut self, output: &Out) -> Option<In>
where
In: Eq + Hash,
Out: Eq + Hash,
{
let input = self.parent.remove(output)?;
let Entry::Occupied(mut entry) = self.outstanding.entry(input) else {
return None;
};
*entry.get_mut() -= 1;
if *entry.get() == 0 {
return Some(entry.remove_entry().0);
}
None
}
pub fn retract(&mut self, output: &Out) -> Option<In>
where
In: Eq + Hash + Clone,
Out: Eq + Hash,
{
let input = self.parent.remove(output)?;
if let Entry::Occupied(mut entry) = self.outstanding.entry(input.clone()) {
*entry.get_mut() -= 1;
if *entry.get() == 0 {
entry.remove_entry();
}
}
Some(input)
}
#[inline]
#[must_use]
pub fn waiting(&self) -> usize {
self.outstanding.len()
}
#[inline]
#[must_use]
pub fn is_empty(&self) -> bool {
self.outstanding.is_empty() && self.parent.is_empty()
}
}
impl<In, Out> Default for Lineage<In, Out> {
#[inline]
fn default() -> Self {
Self::new()
}
}
#[cfg(test)]
mod tests;