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