use std::convert::Infallible;
use dope_core::{
dope_sys::thread::signal::Signal,
platform::{affinity, signal, thread},
};
use o3::alloc;
use crate::process::failure::{bus, config};
#[derive(Debug)]
pub enum Cause<S, I = Infallible> {
Replica { cpu: affinity::Cpu, cause: I },
Config(config::Error),
Allocation(alloc::Error),
Signal(signal::Acquire),
Entropy(dope_core::Errno),
WorkerBus {
cpu: affinity::Cpu,
source: bus::Create,
},
BusRead {
cpu: affinity::Cpu,
source: bus::Read,
},
BusState(bus::State),
WorkerFailed {
cpu: affinity::Cpu,
},
WorkerMissing {
cpu: affinity::Cpu,
},
WorkerSpawn {
cpu: affinity::Cpu,
source: thread::Error,
},
Poll(dope_core::Errno),
TerminationWait(<signal::Source<'static> as Signal>::Error),
WatchReady(S),
WatchPulse(S),
WatchDrain(S),
}
impl<S, I> Cause<S, I> {
pub(in crate::process) fn received(
result: Result<bus::Event, bus::Read>,
cpu: affinity::Cpu,
) -> Result<bus::Event, Self> {
match result {
Ok(bus::Event::Failed) => Err(Self::WorkerFailed { cpu }),
Ok(event) => Ok(event),
Err(source) => Err(Self::BusRead { cpu, source }),
}
}
}
#[derive(Debug)]
pub enum Bus {
Write {
source: bus::Fault,
handoff: Option<bus::Read>,
},
Read(bus::Read),
}
#[derive(Debug)]
pub enum Worker<W> {
Failed {
cpu: affinity::Cpu,
handoff: Option<bus::Read>,
},
Affinity {
cpu: affinity::Cpu,
source: affinity::Error,
bus: bus::Report,
handoff: Option<bus::Read>,
},
Entry {
cpu: affinity::Cpu,
source: W,
bus: bus::Report,
handoff: Option<bus::Read>,
},
Bus {
cpu: affinity::Cpu,
source: Bus,
},
}
#[derive(Debug)]
pub enum Fault<'r, S, W, I = Infallible> {
Control(Cause<S, I>),
Stop(dope_core::Errno),
Release(signal::Release<'r>),
Worker(Worker<W>),
}
#[must_use = "every process failure must be consumed"]
#[derive(Debug)]
pub struct Error<'r, S, W, I = Infallible> {
control: Option<Cause<S, I>>,
stop: Option<dope_core::Errno>,
release: Option<signal::Release<'r>>,
workers: Vec<Worker<W>>,
}
impl<'r, S, W, I> Error<'r, S, W, I> {
pub(in crate::process) fn failed(
control: Cause<S, I>,
stop: Option<dope_core::Errno>,
workers: Vec<Worker<W>>,
) -> Self {
Self {
control: Some(control),
stop,
release: None,
workers,
}
}
pub(in crate::process) fn finish(
stop: Option<dope_core::Errno>,
workers: Vec<Worker<W>>,
) -> Result<(), Self> {
if stop.is_none() && workers.is_empty() {
return Ok(());
}
Err(Self {
control: None,
stop,
release: None,
workers,
})
}
pub fn faults(self) -> impl Iterator<Item = Fault<'r, S, W, I>> {
self.control.into_iter().map(Fault::Control)
.chain(self.stop.into_iter().map(Fault::Stop))
.chain(self.workers.into_iter().map(Fault::Worker))
.chain(self.release.into_iter().map(Fault::Release))
}
}
impl<'r, S, W, I> From<Cause<S, I>> for Error<'r, S, W, I> {
fn from(cause: Cause<S, I>) -> Self {
Self::failed(cause, None, Vec::new())
}
}
impl<S, I> From<alloc::Error> for Cause<S, I> {
fn from(error: alloc::Error) -> Self {
Self::Allocation(error)
}
}
impl<'r, S, W, I> From<signal::Error<'r, Error<'r, S, W, I>>> for Error<'r, S, W, I> {
fn from(error: signal::Error<'r, Error<'r, S, W, I>>) -> Self {
match error {
signal::Error::Acquire(error) => Self::failed(Cause::Signal(error), None, Vec::new()),
signal::Error::AcquireRelease { acquire, release } => {
let mut error = Self::failed(Cause::Signal(acquire), None, Vec::new());
error.release = Some(release);
error
}
signal::Error::Run(error) => error,
signal::Error::Release { run, cause } => {
let mut error = run.unwrap_or(Self {
control: None,
stop: None,
release: None,
workers: Vec::new(),
});
error.release = Some(cause);
error
}
}
}
}