1use std::any::Any;
6use std::cell::Cell;
7use std::collections::VecDeque;
8use std::future::Future;
9use std::marker::PhantomData;
10use std::ops::DerefMut;
11use std::pin::Pin;
12
13use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
14use tokio::task::JoinHandle;
15use web_time::SystemTime;
16
17use super::graph::StateLifespan;
18use super::state::StateHandle;
19use super::{LoopId, LoopTag, StateId, StateTag, SubgraphId, SubgraphTag};
20use crate::scheduled::ticks::TickInstant;
21use crate::util::priority_stack::PriorityStack;
22use crate::util::slot_vec::{SecondarySlotVec, SlotVec};
23
24pub struct Context {
30 states: SlotVec<StateTag, StateData>,
32
33 pub(super) stratum_stack: PriorityStack<usize>,
35
36 pub(super) loop_nonce_stack: Vec<usize>,
38
39 pub(super) schedule_deferred: Vec<SubgraphId>,
42
43 pub(super) stratum_queues: Vec<VecDeque<SubgraphId>>,
46
47 pub(super) event_queue_recv: UnboundedReceiver<(SubgraphId, bool)>,
49 pub(super) can_start_tick: bool,
51 pub(super) events_received_tick: bool,
53
54 pub(super) event_queue_send: UnboundedSender<(SubgraphId, bool)>,
57
58 pub(super) reschedule_loop_block: Cell<bool>,
60 pub(super) allow_another_iteration: Cell<bool>,
61
62 pub(super) current_tick: TickInstant,
63 pub(super) current_stratum: usize,
64
65 pub(super) current_tick_start: SystemTime,
66 pub(super) is_first_run_this_tick: bool,
67 pub(super) loop_iter_count: usize,
68
69 pub(super) loop_depth: SlotVec<LoopTag, usize>,
71 loop_states: SecondarySlotVec<LoopTag, Vec<StateId>>,
73 pub(super) loop_nonce: usize,
75
76 subgraph_states: SecondarySlotVec<SubgraphTag, Vec<StateId>>,
78
79 pub(super) subgraph_id: SubgraphId,
82
83 tasks_to_spawn: Vec<Pin<Box<dyn Future<Output = ()> + 'static>>>,
84 task_join_handles: Vec<JoinHandle<()>>,
86}
87impl Context {
89 pub fn current_tick(&self) -> TickInstant {
91 self.current_tick
92 }
93
94 pub fn current_tick_start(&self) -> SystemTime {
96 self.current_tick_start
97 }
98
99 pub fn is_first_run_this_tick(&self) -> bool {
101 self.is_first_run_this_tick
102 }
103
104 pub fn loop_iter_count(&self) -> usize {
106 self.loop_iter_count
107 }
108
109 pub fn current_stratum(&self) -> usize {
111 self.current_stratum
112 }
113
114 pub fn current_subgraph(&self) -> SubgraphId {
116 self.subgraph_id
117 }
118
119 pub fn schedule_subgraph(&self, sg_id: SubgraphId, is_external: bool) {
125 self.event_queue_send.send((sg_id, is_external)).unwrap()
126 }
127
128 pub fn reschedule_loop_block(&self) {
130 self.reschedule_loop_block.set(true);
131 }
132
133 pub fn allow_another_iteration(&self) {
135 self.allow_another_iteration.set(true);
136 }
137
138 pub fn waker(&self) -> std::task::Waker {
141 use std::sync::Arc;
142
143 use futures::task::ArcWake;
144
145 struct ContextWaker {
146 subgraph_id: SubgraphId,
147 event_queue_send: UnboundedSender<(SubgraphId, bool)>,
148 }
149 impl ArcWake for ContextWaker {
150 fn wake_by_ref(arc_self: &Arc<Self>) {
151 let _recv_closed_error =
152 arc_self.event_queue_send.send((arc_self.subgraph_id, true));
153 }
154 }
155
156 let context_waker = ContextWaker {
157 subgraph_id: self.subgraph_id,
158 event_queue_send: self.event_queue_send.clone(),
159 };
160 futures::task::waker(Arc::new(context_waker))
161 }
162
163 pub unsafe fn state_ref_unchecked<T>(&self, handle: StateHandle<T>) -> &'_ T
168 where
169 T: Any,
170 {
171 let state = self
172 .states
173 .get(handle.state_id)
174 .expect("Failed to find state with given handle.")
175 .state
176 .as_ref();
177
178 debug_assert!(state.is::<T>());
179
180 unsafe {
181 &*(state as *const dyn Any as *const T)
184 }
185 }
186
187 pub fn state_ref<T>(&self, handle: StateHandle<T>) -> &'_ T
189 where
190 T: Any,
191 {
192 self.states
193 .get(handle.state_id)
194 .expect("Failed to find state with given handle.")
195 .state
196 .downcast_ref()
197 .expect("StateHandle wrong type T for casting.")
198 }
199
200 pub fn state_mut<T>(&mut self, handle: StateHandle<T>) -> &'_ mut T
202 where
203 T: Any,
204 {
205 self.states
206 .get_mut(handle.state_id)
207 .expect("Failed to find state with given handle.")
208 .state
209 .downcast_mut()
210 .expect("StateHandle wrong type T for casting.")
211 }
212
213 pub fn add_state<T>(&mut self, state: T) -> StateHandle<T>
215 where
216 T: Any,
217 {
218 let state_data = StateData {
219 state: Box::new(state),
220 lifespan_hook_fn: None,
221 lifespan: None,
222 };
223 let state_id = self.states.insert(state_data);
224
225 StateHandle {
226 state_id,
227 _phantom: PhantomData,
228 }
229 }
230
231 pub fn set_state_lifespan_hook<T>(
233 &mut self,
234 handle: StateHandle<T>,
235 lifespan: StateLifespan,
236 mut hook_fn: impl 'static + FnMut(&mut T),
237 ) where
238 T: Any,
239 {
240 let state_data = self
241 .states
242 .get_mut(handle.state_id)
243 .expect("Failed to find state with given handle.");
244 state_data.lifespan_hook_fn = Some(Box::new(move |state| {
245 (hook_fn)(state.downcast_mut::<T>().unwrap());
246 }));
247 state_data.lifespan = Some(lifespan);
248
249 match lifespan {
250 StateLifespan::Subgraph(key) => {
251 self.subgraph_states
252 .get_or_insert_with(key, Vec::new)
253 .push(handle.state_id);
254 }
255 StateLifespan::Loop(loop_id) => {
256 self.loop_states
257 .get_or_insert_with(loop_id, Vec::new)
258 .push(handle.state_id);
259 }
260 StateLifespan::Tick => {
261 }
263 StateLifespan::Static => {
264 }
266 }
267 }
268
269 pub fn request_task<Fut>(&mut self, future: Fut)
271 where
272 Fut: Future<Output = ()> + 'static,
273 {
274 self.tasks_to_spawn.push(Box::pin(future));
275 }
276
277 pub fn spawn_tasks(&mut self) {
279 for task in self.tasks_to_spawn.drain(..) {
280 self.task_join_handles.push(tokio::task::spawn_local(task));
281 }
282 }
283
284 pub fn abort_tasks(&mut self) {
286 for task in self.task_join_handles.drain(..) {
287 task.abort();
288 }
289 }
290
291 pub async fn join_tasks(&mut self) {
295 futures::future::join_all(self.task_join_handles.drain(..)).await;
296 }
297}
298
299impl Default for Context {
300 fn default() -> Self {
301 let stratum_queues = vec![Default::default()]; let (event_queue_send, event_queue_recv) = mpsc::unbounded_channel();
303 let (stratum_stack, loop_depth) = Default::default();
304 Self {
305 states: SlotVec::new(),
306
307 stratum_stack,
308
309 loop_nonce_stack: Vec::new(),
310
311 schedule_deferred: Vec::new(),
312
313 stratum_queues,
314 event_queue_recv,
315 can_start_tick: false,
316 events_received_tick: false,
317
318 event_queue_send,
319 reschedule_loop_block: Cell::new(false),
320 allow_another_iteration: Cell::new(false),
321
322 current_stratum: 0,
323 current_tick: TickInstant::default(),
324
325 current_tick_start: SystemTime::now(),
326 is_first_run_this_tick: false,
327 loop_iter_count: 0,
328
329 loop_depth,
330 loop_states: SecondarySlotVec::new(),
331 loop_nonce: 0,
332
333 subgraph_states: SecondarySlotVec::new(),
334
335 subgraph_id: SubgraphId::from_raw(0),
337
338 tasks_to_spawn: Vec::new(),
339 task_join_handles: Vec::new(),
340 }
341 }
342}
343impl Context {
345 pub(super) fn init_stratum(&mut self, stratum: usize) {
347 if self.stratum_queues.len() <= stratum {
348 self.stratum_queues
349 .resize_with(stratum + 1, Default::default);
350 }
351 }
352
353 pub(super) fn run_state_hooks_tick(&mut self) {
355 tracing::trace!("Running state hooks for tick.");
356 for state_data in self.states.values_mut() {
357 let StateData {
358 state,
359 lifespan_hook_fn: Some(lifespan_hook_fn),
360 lifespan: Some(StateLifespan::Tick),
361 } = state_data
362 else {
363 continue;
364 };
365 (lifespan_hook_fn)(Box::deref_mut(state));
366 }
367 }
368
369 pub(super) fn run_state_hooks_subgraph(&mut self, subgraph_id: SubgraphId) {
370 tracing::trace!("Running state hooks for subgraph.");
371 for state_id in self.subgraph_states.get(subgraph_id).into_iter().flatten() {
372 let StateData {
373 state,
374 lifespan_hook_fn,
375 lifespan: _,
376 } = self
377 .states
378 .get_mut(*state_id)
379 .expect("Failed to find state with given ID.");
380
381 if let Some(lifespan_hook_fn) = lifespan_hook_fn {
382 (lifespan_hook_fn)(Box::deref_mut(state));
383 }
384 }
385 }
386
387 pub(super) fn run_state_hooks_loop(&mut self, loop_id: LoopId) {
390 tracing::trace!(
391 loop_id = loop_id.to_string(),
392 "Running state hooks for loop."
393 );
394 for state_id in self.loop_states.get(loop_id).into_iter().flatten() {
395 let StateData {
396 state,
397 lifespan_hook_fn,
398 lifespan: _,
399 } = self
400 .states
401 .get_mut(*state_id)
402 .expect("Failed to find state with given ID.");
403
404 if let Some(lifespan_hook_fn) = lifespan_hook_fn {
405 (lifespan_hook_fn)(Box::deref_mut(state));
406 }
407 }
408 }
409}
410
411struct StateData {
413 state: Box<dyn Any>,
414 lifespan_hook_fn: Option<LifespanResetFn>, lifespan: Option<StateLifespan>,
417}
418type LifespanResetFn = Box<dyn FnMut(&mut dyn Any)>;