dope_runtime/process/failure/
execute.rs1use std::convert::Infallible;
3
4use dope_core::{
5 dope_sys::thread::signal::Signal,
6 platform::{affinity, signal, thread},
7};
8use o3::alloc;
9
10use crate::process::failure::{bus, config};
11
12#[derive(Debug)]
14pub enum Cause<S, I = Infallible> {
15 Replica { cpu: affinity::Cpu, cause: I },
16 Config(config::Error),
17 Allocation(alloc::Error),
18 Signal(signal::Acquire),
19 Entropy(dope_core::Errno),
20 WorkerBus {
21 cpu: affinity::Cpu,
22 source: bus::Create,
23 },
24 BusRead {
25 cpu: affinity::Cpu,
26 source: bus::Read,
27 },
28 BusState(bus::State),
29 WorkerFailed {
30 cpu: affinity::Cpu,
31 },
32 WorkerMissing {
33 cpu: affinity::Cpu,
34 },
35 WorkerSpawn {
36 cpu: affinity::Cpu,
37 source: thread::Error,
38 },
39 Poll(dope_core::Errno),
40 TerminationWait(<signal::Source<'static> as Signal>::Error),
41 WatchReady(S),
42 WatchPulse(S),
43 WatchDrain(S),
44}
45
46impl<S, I> Cause<S, I> {
47 pub(in crate::process) fn received(
48 result: Result<bus::Event, bus::Read>,
49 cpu: affinity::Cpu,
50 ) -> Result<bus::Event, Self> {
51 match result {
52 Ok(bus::Event::Failed) => Err(Self::WorkerFailed { cpu }),
53 Ok(event) => Ok(event),
54 Err(source) => Err(Self::BusRead { cpu, source }),
55 }
56 }
57}
58
59#[derive(Debug)]
61pub enum Bus {
62 Write {
63 source: bus::Fault,
64 handoff: Option<bus::Read>,
65 },
66 Read(bus::Read),
67}
68
69#[derive(Debug)]
71pub enum Worker<W> {
72 Failed {
73 cpu: affinity::Cpu,
74 handoff: Option<bus::Read>,
75 },
76 Affinity {
77 cpu: affinity::Cpu,
78 source: affinity::Error,
79 bus: bus::Report,
80 handoff: Option<bus::Read>,
81 },
82 Entry {
83 cpu: affinity::Cpu,
84 source: W,
85 bus: bus::Report,
86 handoff: Option<bus::Read>,
87 },
88 Bus {
89 cpu: affinity::Cpu,
90 source: Bus,
91 },
92}
93
94#[derive(Debug)]
96pub enum Fault<'r, S, W, I = Infallible> {
97 Control(Cause<S, I>),
98 Stop(dope_core::Errno),
99 Release(signal::Release<'r>),
100 Worker(Worker<W>),
101}
102
103#[must_use = "every process failure must be consumed"]
105#[derive(Debug)]
106pub struct Error<'r, S, W, I = Infallible> {
107 control: Option<Cause<S, I>>,
108 stop: Option<dope_core::Errno>,
109 release: Option<signal::Release<'r>>,
110 workers: Vec<Worker<W>>,
111}
112
113impl<'r, S, W, I> Error<'r, S, W, I> {
114 pub(in crate::process) fn failed(
115 control: Cause<S, I>,
116 stop: Option<dope_core::Errno>,
117 workers: Vec<Worker<W>>,
118 ) -> Self {
119 Self {
120 control: Some(control),
121 stop,
122 release: None,
123 workers,
124 }
125 }
126
127 pub(in crate::process) fn finish(
128 stop: Option<dope_core::Errno>,
129 workers: Vec<Worker<W>>,
130 ) -> Result<(), Self> {
131 if stop.is_none() && workers.is_empty() {
132 return Ok(());
133 }
134 Err(Self {
135 control: None,
136 stop,
137 release: None,
138 workers,
139 })
140 }
141
142 pub fn faults(self) -> impl Iterator<Item = Fault<'r, S, W, I>> {
143 self.control.into_iter().map(Fault::Control)
144 .chain(self.stop.into_iter().map(Fault::Stop))
145 .chain(self.workers.into_iter().map(Fault::Worker))
146 .chain(self.release.into_iter().map(Fault::Release))
147 }
148
149}
150
151impl<'r, S, W, I> From<Cause<S, I>> for Error<'r, S, W, I> {
152 fn from(cause: Cause<S, I>) -> Self {
153 Self::failed(cause, None, Vec::new())
154 }
155}
156
157impl<S, I> From<alloc::Error> for Cause<S, I> {
158 fn from(error: alloc::Error) -> Self {
159 Self::Allocation(error)
160 }
161}
162
163impl<'r, S, W, I> From<signal::Error<'r, Error<'r, S, W, I>>> for Error<'r, S, W, I> {
164 fn from(error: signal::Error<'r, Error<'r, S, W, I>>) -> Self {
165 match error {
166 signal::Error::Acquire(error) => Self::failed(Cause::Signal(error), None, Vec::new()),
167 signal::Error::AcquireRelease { acquire, release } => {
168 let mut error = Self::failed(Cause::Signal(acquire), None, Vec::new());
169 error.release = Some(release);
170 error
171 }
172 signal::Error::Run(error) => error,
173 signal::Error::Release { run, cause } => {
174 let mut error = run.unwrap_or(Self {
175 control: None,
176 stop: None,
177 release: None,
178 workers: Vec::new(),
179 });
180 error.release = Some(cause);
181 error
182 }
183 }
184 }
185}