-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexecutor.rs
More file actions
420 lines (362 loc) · 13.2 KB
/
executor.rs
File metadata and controls
420 lines (362 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
use core::fmt;
use std::{collections::HashMap, panic, sync::RwLock};
use futures::{future::select_all, FutureExt};
use tokio::sync::{mpsc, watch};
use vec_collections::{AbstractVecMap, VecMap};
use crate::{
node_graph::{InputId, NodeId, NodeOutput, OutputId},
pipeline::{
nodes::{DynPipelineNode, PipelineNode},
Pipeline,
},
};
use super::{
ConnectionHandle, DynNodeTask, InvalidationCause, InvalidationNotifier, Invalidator, NodeTask,
NodeTaskBuilder, Request, TaskOutput,
};
// MARK: PipelineExecutor
/// Pipeline execution system.
///
/// For every node in the high level [Pipeline] description it creates a
/// [tokio::task], running an event loop. This task is referred to as a node
/// task. It further manages a node tasks connections to other tasks and syncing
/// it with its high level [PipelineNode], by sending commands about connection
/// changes and sync requests to the tasks event loop. Each node task is
/// described using the [NodeTask] trait. Implementers are required to listen to
/// data requests from other tasks. Each task handles only one request at a
/// time. The processing of a request might be canceled when the configuration
/// changes, or other tasks are invalidated, which this task depends on.
///
/// [Self::update] syncs the high level [Pipeline] description with the node
/// tasks.
///
/// There is no shared state between node tasks and [PipelineExecutor]. Syncing
/// only uses message channels from [tokio::sync] to communicate to node tasks.
#[derive(Debug)]
pub struct PipelineExecutor {
/// Each runner corresponds to one node in the [Pipeline].
runners: HashMap<NodeId, RwLock<NodeTaskRunner>>,
}
impl PipelineExecutor {
pub fn new() -> Self {
Self {
runners: HashMap::new(),
}
}
pub fn update(&mut self, pipeline: &mut Pipeline) {
// Deleted nodes
self.runners.retain(|id, _| pipeline.nodes.contains_key(id));
// New nodes
for (node_id, node) in &mut pipeline.nodes {
if !self.runners.contains_key(node_id) {
self.runners.insert(
*node_id,
RwLock::new(NodeTaskRunner::from_node(node.as_mut())),
);
}
}
// Connections and node sync
for (node_id, runner) in &self.runners {
let node = pipeline.nodes.get(node_id).expect("Nodes should be synced");
let mut runner = runner.write().unwrap();
runner.sync_connections(node.as_ref(), &self.runners);
runner.sync_node(node.as_ref());
}
}
pub fn get_output(&self, node_id: NodeId, output_id: OutputId) -> Option<ConnectionHandle> {
self.runners
.get(&node_id)
.and_then(|r| r.read().unwrap().get_output(output_id))
}
pub fn clear(&mut self) {
self.runners.clear();
}
}
// MARK: NodeTaskRunner
/// Handle to a node task, holding information about the node task and all
/// channel ends to communicate to the node task.
struct NodeTaskRunner {
output_handles: VecMap<[(OutputId, ConnectionHandle); 4]>,
inputs: VecMap<[(InputId, NodeOutput); 4]>,
control_tx: mpsc::UnboundedSender<ControlMsg>,
sync_tx: watch::Sender<Box<dyn DynPipelineNode>>,
}
impl NodeTaskRunner {
pub fn from_node(node: &mut dyn DynPipelineNode) -> Self {
let (task, output_handles, invalidator) = node.create_node_task();
let (control_tx, control_rx) = mpsc::unbounded_channel();
let (sync_tx, sync_rx) = watch::channel(node.clone_boxed());
tokio::spawn(
RunningNodeTask {
node_task: task,
control_rx,
sync_rx,
input_connections: Vec::new(),
output_invalidator: invalidator,
error_on_last_run: false,
}
.run(),
);
Self {
output_handles,
inputs: VecMap::empty(),
control_tx,
sync_tx,
}
}
pub fn get_output(&self, output_id: OutputId) -> Option<ConnectionHandle> {
self.output_handles.get(&output_id).cloned()
}
pub fn sync_node(&mut self, node: &dyn DynPipelineNode) {
self.sync_tx.send_if_modified(|v| {
if v.changed(node) {
*v = node.clone_boxed();
true
} else {
false
}
});
}
pub fn sync_connections(
&mut self,
node: &dyn DynPipelineNode,
runners: &HashMap<NodeId, RwLock<NodeTaskRunner>>,
) {
let inputs = node.inputs();
for (input_id, incoming) in inputs {
let existing = self
.inputs
.iter()
.find(|(id, _)| *id == input_id)
.map(|(_, o)| *o);
match (incoming, existing) {
(i, e) if i == e => {
// Do nothing when equal
}
(Some(incoming), _) => {
self.connect_input(input_id, incoming, runners);
}
(None, _) => {
self.disconnect_input(input_id);
}
}
}
}
pub fn disconnect_input(&mut self, input_id: InputId) {
self.control_tx
.send(ControlMsg::Disconnect(input_id))
.expect("Task should be running");
self.inputs.retain(|(id, _)| *id != input_id);
}
pub fn connect_input(
&mut self,
input_id: InputId,
output: NodeOutput,
runners: &HashMap<NodeId, RwLock<NodeTaskRunner>>,
) {
// Find runner that we want to connect to
let Some(out_runner) = runners.get(&output.node_id) else {
eprintln!(
"Failed to find runner for node with id {:?}",
output.node_id
);
return;
};
// Get read lock on runner
let out_runner = out_runner.read().unwrap();
// Find creator for output
let Some(connection) = out_runner.get_output(output.output_id) else {
eprintln!(
"Failed to find connection for output with id {:?}",
output.output_id
);
return;
};
// Send connect message to task
self.control_tx
.send(ControlMsg::Connect(input_id, connection.clone()))
.expect("Task should be running");
// Update input record
self.inputs.insert(input_id, output);
}
}
impl fmt::Debug for NodeTaskRunner {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("NodeTaskRunner")
.field(
"output_handles",
&self
.output_handles
.iter()
.map(|(id, _)| id)
.collect::<Vec<_>>(),
)
.field("inputs", &self.inputs)
.finish()
}
}
// MARK: RunningNodeTask
/// The actual task running. It runs an event loop to receive control and sync
/// commands from the [PipelineExecutor] and runs the [NodeTask]s own event
/// loop, which is responsible for receiving data requests and processing data.
struct RunningNodeTask {
node_task: Box<dyn DynNodeTask>,
control_rx: mpsc::UnboundedReceiver<ControlMsg>,
sync_rx: watch::Receiver<Box<dyn DynPipelineNode>>,
input_connections: Vec<(InputId, InvalidationNotifier)>,
output_invalidator: Vec<Invalidator>,
error_on_last_run: bool,
}
impl RunningNodeTask {
/// Main entry point and event loop.
pub async fn run(mut self) {
loop {
tokio::select! {
biased;
msg = self.control_rx.recv() => {
match msg {
Some(ControlMsg::Connect(input_id, mut connection)) => {
connection.reset_connection();
self.node_task.connect(input_id, &mut connection);
if connection.did_connect() {
self.input_connections.push((input_id, connection.get_invalidation_notifier()));
self.invalidate(InvalidationCause::Connected(input_id));
}
}
Some(ControlMsg::Disconnect(input_id)) => {
self.node_task.disconnect(input_id);
self.input_connections.retain(|(id, _)| *id != input_id);
self.invalidate(InvalidationCause::Disconnected(input_id));
}
None => break,
};
}
_ = self.sync_rx.changed() => {
self.node_task.sync_node(self.sync_rx.borrow().as_ref());
self.invalidate(InvalidationCause::Synced);
}
input_id = Self::on_invalidation(&mut self.input_connections) => {
// An input got invalidated
self.invalidate(InvalidationCause::InputInvalidated(input_id));
}
is_error = Self::run_task(self.error_on_last_run, self.node_task.as_mut()) => {
self.error_on_last_run = is_error;
}
}
}
}
/// Run the [NodeTask::run] method, additionally handling panics and errors.
async fn run_task(error_on_last_run: bool, task: &mut dyn DynNodeTask) -> bool {
if error_on_last_run {
let () = futures::future::pending().await;
}
let result = panic::AssertUnwindSafe(task.run()).catch_unwind().await;
let is_error = !matches!(result, Ok(Ok(_)));
match result {
Ok(Ok(_)) => {}
Ok(Err(e)) => eprintln!("Task failed: {:?}", e),
Err(e) => eprintln!(
"Task panicked: {}",
if let Some(msg) = e.downcast_ref::<&'static str>() {
msg.to_string()
} else if let Some(msg) = e.downcast_ref::<String>() {
msg.clone()
} else {
format!("?{:?}", e)
}
),
}
is_error
}
/// Invalidates all outputs of this node task and itself.
fn invalidate(&mut self, cause: InvalidationCause) {
self.output_invalidator
.iter()
.for_each(Invalidator::invalidate);
self.node_task.invalidate(cause);
self.error_on_last_run = false;
}
/// Returned future completes when any input received an invalidation
/// notice.
async fn on_invalidation(notifiers: &mut Vec<(InputId, InvalidationNotifier)>) -> InputId {
// Cannot borrow self as mutable again at call site
if !notifiers.is_empty() {
// Await all notifiers, stopping if any completes
let (is_channel_open, index, ..) =
select_all(notifiers.iter_mut().map(|(_, n)| n.on_invalidate())).await;
let id = notifiers[index].0;
if !is_channel_open {
// Partner dropped, already disconnected
notifiers.remove(index);
}
id
} else {
futures::future::pending().await
}
}
}
// MARK: ControlMsg
enum ControlMsg {
Connect(InputId, ConnectionHandle),
Disconnect(InputId),
}
impl fmt::Debug for ControlMsg {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
ControlMsg::Connect(input_id, ..) => f.debug_tuple("Connect").field(input_id).finish(),
ControlMsg::Disconnect(input_id) => {
f.debug_tuple("Disconnect").field(input_id).finish()
}
}
}
}
// MARK: NodeTaskBuilderImpl
pub struct NodeTaskBuilderImpl<T: PipelineNode> {
connection_handles: VecMap<[(OutputId, ConnectionHandle); 4]>,
output_invalidator: Vec<Invalidator>,
task: Option<Box<dyn DynNodeTask>>,
_phantom: std::marker::PhantomData<T>,
}
impl<T: PipelineNode> NodeTaskBuilderImpl<T> {
pub fn new() -> Self {
Self {
connection_handles: VecMap::empty(),
task: None,
output_invalidator: Vec::new(),
_phantom: Default::default(),
}
}
pub fn build(
self,
) -> (
Box<dyn DynNodeTask>,
VecMap<[(OutputId, ConnectionHandle); 4]>,
Vec<Invalidator>,
) {
let task = self
.task
.expect("NodeTaskBuilder::task should be called before building");
(task, self.connection_handles, self.output_invalidator)
}
}
impl<T: PipelineNode> NodeTaskBuilder for NodeTaskBuilderImpl<T> {
type PipelineNode = T;
fn output<Req: Request>(
&mut self,
output_id: <Self::PipelineNode as PipelineNode>::OutputId,
) -> TaskOutput<Req> {
let (handle, output) = ConnectionHandle::new();
self.connection_handles.insert(output_id.into(), handle);
self.output_invalidator.push(output.get_invalidator());
output
}
fn task(
&mut self,
task: impl NodeTask<
InputId = <Self::PipelineNode as PipelineNode>::InputId,
PipelineNode = Self::PipelineNode,
>,
) {
self.task = Some(Box::new(task));
}
}