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
use std::sync::Arc;
use crate::{
Engine, ExecutionStyle,
config::Config,
engine::computation_graph::{CallerInformation, computing::QueryComputing},
query::QueryID,
};
/// A drop guard for undoing the registration of a callee query.
///
/// This aims to ensure cancelation safety in case of the task being yielded and
/// canceled mid query.
pub struct UndoRegisterCallee {
query_computing: Arc<QueryComputing>,
callee_target: QueryID,
defused: bool,
}
impl UndoRegisterCallee {
/// Creates a new [`UndoRegisterCallee`] instance.
pub const fn new(
query_computing: Arc<QueryComputing>,
callee_target: QueryID,
) -> Self {
Self { query_computing, callee_target, defused: false }
}
/// Don't undo the registration when dropped.
pub fn defuse(mut self) { self.defused = true; }
}
impl Drop for UndoRegisterCallee {
fn drop(&mut self) {
if self.defused {
return;
}
self.query_computing.abort_callee(&self.callee_target);
}
}
impl<C: Config> Engine<C> {
pub(super) fn register_callee(
&self,
caller: &CallerInformation,
calee_target: &QueryID,
) -> Option<UndoRegisterCallee> {
// record the dependency first, don't necessary need to figure out
// the observed value fingerprint yet
caller.get_query_caller().map_or_else(
|| None,
|caller| {
let computing = caller.computing();
assert!(
!computing.query_kind().is_external_input(),
"`ExternalInput` queries cannot call other queries"
);
// Invariant Check: projection query can only requires firewall
// queries or projection queries.
if computing.query_kind().is_projection() {
// get the kind of query about to be registerd by looking
// up from the executor registry
let entry =
self.executor_registry.get_executor_entry_by_type_id(
&calee_target.stable_type_id(),
);
let exec_style = entry.obtain_execution_style();
assert!(
matches!(
exec_style,
ExecutionStyle::Firewall
| ExecutionStyle::Projection
),
"Projection query can only depend on firewall or \
projection queries"
);
}
computing.register_calee(calee_target);
Some(UndoRegisterCallee::new(computing.clone(), *calee_target))
},
)
}
}