ave-actors-actor 0.4.1

Ave actor model
Documentation
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
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
//! Actor system: creates, manages, and shuts down actors.

use crate::{
    Actor, ActorPath, ActorRef, Error, Event, Handler,
    actor::ChildErrorSender,
    runner::{ActorRunner, StopHandle, StopSender},
    sink::Sink,
};

use tokio::sync::{RwLock, broadcast, mpsc, oneshot};
use tokio_util::sync::CancellationToken;

use tracing::{Instrument, Span, debug, error, warn};

use std::{
    any::Any,
    collections::{HashMap, HashSet},
    sync::{
        Arc,
        atomic::{AtomicBool, Ordering},
    },
};

/// The reason why the actor system stopped, returned by [`SystemRunner::run`].
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ShutdownReason {
    /// System stopped gracefully (e.g., SIGTERM); exit with code 0.
    Graceful,
    /// System stopped due to an unrecoverable actor failure; exit with a non-zero code.
    Crash,
}

impl ShutdownReason {
    /// Returns `0` for a graceful shutdown or `1` for a crash.
    pub const fn exit_code(&self) -> i32 {
        match self {
            Self::Graceful => 0,
            Self::Crash => 1,
        }
    }
}

/// Entry point for building an actor system instance.
pub struct ActorSystem {}

impl ActorSystem {
    /// Creates the actor system and returns a `(SystemRef, SystemRunner)` pair.
    ///
    /// Cancel `graceful_token` for a graceful shutdown (exit code 0) or `crash_token` for a crash
    /// shutdown (exit code 1); the reason is reflected in [`SystemRunner::run`]'s return value.
    pub fn create(
        graceful_token: CancellationToken,
        crash_token: CancellationToken,
    ) -> (SystemRef, SystemRunner) {
        let (event_sender, event_receiver) = mpsc::channel(4);
        let system = SystemRef::new(event_sender, graceful_token, crash_token);
        let runner = SystemRunner::new(event_receiver);
        (system, runner)
    }
}

/// System-level events broadcast on the observable system event channel.
#[derive(Debug, Clone)]
pub enum SystemEvent {
    /// Non-fatal error emitted by a root actor that has no parent to receive it.
    ActorError {
        /// Path of the actor that emitted the error.
        path: ActorPath,
        /// Error emitted by that actor.
        error: Error,
    },
    /// Signals that the actor system should stop.
    /// Carries the reason so the runner can report it to the caller.
    StopSystem(ShutdownReason),
}

/// Cloneable, thread-safe handle to the actor system.
///
/// Use this to create root actors, spawn event sinks, and shut down the system.
/// Cloning is cheap — all clones share the same underlying system state.
#[derive(Clone)]
pub struct SystemRef {
    /// Registry of all actors in the system, indexed by their paths.
    /// Uses type erasure (Any) to store heterogeneous actor types.
    actors:
        Arc<RwLock<HashMap<ActorPath, Box<dyn Any + Send + Sync + 'static>>>>,
    /// Direct-children index to avoid scanning the full actor registry on lookups.
    child_index: Arc<RwLock<HashMap<ActorPath, HashSet<ActorPath>>>>,

    /// Registry of helper objects that can be shared across actors.
    /// Helpers can be any type (database connections, configurations, etc.).
    helpers: Arc<RwLock<HashMap<String, Box<dyn Any + Send + Sync + 'static>>>>,

    /// Stop senders for root-level actors to enable coordinated shutdown.
    root_senders: Arc<RwLock<HashMap<ActorPath, StopHandle>>>,
    /// Broadcast bus for observable system-level events such as root actor errors.
    system_event_sender: broadcast::Sender<SystemEvent>,

    /// Cancelled by an external signal (SIGTERM, operator). Exit code 0.
    graceful_token: CancellationToken,
    /// Cancelled by an actor on unrecoverable failure. Exit code 1.
    crash_token: CancellationToken,
    /// Set as soon as shutdown begins; blocks creation of new actors.
    shutting_down: Arc<AtomicBool>,
}

impl SystemRef {
    pub(crate) fn new(
        event_sender: mpsc::Sender<SystemEvent>,
        graceful_token: CancellationToken,
        crash_token: CancellationToken,
    ) -> Self {
        let root_senders =
            Arc::new(RwLock::new(HashMap::<ActorPath, StopHandle>::new()));
        let child_index = Arc::new(RwLock::new(HashMap::new()));
        let (system_event_sender, _) = broadcast::channel::<SystemEvent>(256);
        let shutting_down = Arc::new(AtomicBool::new(false));
        let root_sender_clone = root_senders.clone();
        let system_event_sender_clone = system_event_sender.clone();
        let shutting_down_clone = shutting_down.clone();
        let graceful_clone = graceful_token.clone();
        let crash_clone = crash_token.clone();

        tokio::spawn(async move {
            let reason = tokio::select! {
                _ = graceful_clone.cancelled() => ShutdownReason::Graceful,
                _ = crash_clone.cancelled()   => ShutdownReason::Crash,
            };
            shutting_down_clone.store(true, Ordering::SeqCst);
            debug!(reason = ?reason, "Stopping actor system");
            let root_senders = {
                let mut root_senders = root_sender_clone.write().await;
                // Move the senders out while holding the lock, then release it
                // before awaiting on stop notifications.
                std::mem::take(&mut *root_senders)
            };

            // Send all stop signals first so all root actors begin shutdown concurrently.
            let mut receivers = Vec::with_capacity(root_senders.len());
            for (path, handle) in root_senders {
                let (stop_sender, stop_receiver) = oneshot::channel();
                if handle.sender().send(Some(stop_sender)).await.is_ok() {
                    receivers.push((path, handle.timeout(), stop_receiver));
                } else {
                    warn!(path = %path, "Failed to send stop signal to root actor");
                }
            }

            // Wait for all confirmations in parallel.
            for (path, timeout, receiver) in receivers {
                if let Some(timeout) = timeout {
                    if tokio::time::timeout(timeout, receiver).await.is_err() {
                        warn!(
                            path = %path,
                            timeout_ms = timeout.as_millis(),
                            "Timed out waiting for root actor shutdown acknowledgement"
                        );
                    }
                } else {
                    let _ = receiver.await;
                }
            }

            if let Err(e) = event_sender
                .send(SystemEvent::StopSystem(reason.clone()))
                .await
            {
                error!(error = %e, "Failed to send StopSystem event");
            }
            let _ =
                system_event_sender_clone.send(SystemEvent::StopSystem(reason));
        });

        Self {
            actors: Arc::new(RwLock::new(HashMap::new())),
            child_index,
            helpers: Arc::new(RwLock::new(HashMap::new())),
            graceful_token,
            crash_token,
            root_senders,
            system_event_sender,
            shutting_down,
        }
    }

    fn is_shutting_down(&self) -> bool {
        self.shutting_down.load(Ordering::SeqCst)
            || self.graceful_token.is_cancelled()
            || self.crash_token.is_cancelled()
    }

    /// Returns a broadcast receiver for system-level events such as root actor errors and shutdown.
    pub fn subscribe_system_events(&self) -> broadcast::Receiver<SystemEvent> {
        self.system_event_sender.subscribe()
    }

    pub(crate) fn publish_system_event(&self, event: SystemEvent) {
        let _ = self.system_event_sender.send(event);
    }

    async fn index_actor(&self, path: &ActorPath) {
        let parent = path.parent();
        self.child_index
            .write()
            .await
            .entry(parent)
            .or_default()
            .insert(path.clone());
    }

    async fn deindex_actor(&self, path: &ActorPath) {
        let parent = path.parent();
        let mut child_index = self.child_index.write().await;
        if let Some(children) = child_index.get_mut(&parent) {
            children.remove(path);
            if children.is_empty() {
                child_index.remove(&parent);
            }
        }
    }

    /// Returns the `ActorRef` for the actor at `path`, or `Error::NotFound` if no actor is registered there.
    pub async fn get_actor<A>(
        &self,
        path: &ActorPath,
    ) -> Result<ActorRef<A>, Error>
    where
        A: Actor + Handler<A>,
    {
        let actors = self.actors.read().await;
        actors
            .get(path)
            .and_then(|any| any.downcast_ref::<ActorRef<A>>().cloned())
            .ok_or_else(|| Error::NotFound { path: path.clone() })
    }

    pub(crate) async fn create_actor_path<A>(
        &self,
        path: ActorPath,
        actor: A,
        parent_error_sender: Option<ChildErrorSender>,
        span: Span,
    ) -> Result<(ActorRef<A>, StopSender), Error>
    where
        A: Actor + Handler<A>,
    {
        if self.is_shutting_down() {
            debug!(path = %path, "Rejecting actor creation during shutdown");
            return Err(Error::SystemStopped);
        }

        // Create the actor runner and init it.
        let system = self.clone();
        let is_root = parent_error_sender.is_none();
        let (mut runner, actor_ref, stop_sender) =
            ActorRunner::create(path.clone(), actor, parent_error_sender);

        // Atomically check+insert under the same write lock to avoid
        // concurrent duplicate creations for the same path.
        {
            let mut actors = self.actors.write().await;
            if actors.contains_key(&path) {
                debug!(path = %path, "Actor already exists");
                return Err(Error::Exists { path });
            }
            actors.insert(path.clone(), Box::new(actor_ref.clone()));
        }
        self.index_actor(&path).await;

        if is_root {
            let mut root_senders = self.root_senders.write().await;
            if self.is_shutting_down() {
                drop(root_senders);
                self.remove_actor(&path).await;
                debug!(path = %path, "Rejecting root actor creation after shutdown started");
                return Err(Error::SystemStopped);
            }
            root_senders.insert(
                path.clone(),
                StopHandle::new(stop_sender.clone(), A::stop_timeout()),
            );
        }

        let (sender, receiver) = oneshot::channel::<bool>();

        let stop_sender_clone = stop_sender.clone();
        let span_clone = span.clone();
        let init_handle = tokio::spawn(
            async move {
                runner
                    .init(system, stop_sender_clone, Some(sender), span_clone)
                    .await;
            }
            .instrument(span),
        );

        let startup_result = match A::startup_timeout() {
            Some(timeout) => tokio::time::timeout(timeout, receiver)
                .await
                .map_err(|_| timeout),
            None => Ok(receiver.await),
        };

        match startup_result {
            Ok(Ok(true)) => {
                debug!(path = %path, "Actor initialized successfully");
                Ok((actor_ref, stop_sender))
            }
            Ok(Ok(false)) => {
                error!(path = %path, "Actor runner failed to initialize");
                self.remove_actor(&path).await;
                if is_root {
                    self.root_senders.write().await.remove(&path);
                }
                Err(Error::FunctionalCritical {
                    description: format!("Runner can not init {}", path),
                })
            }
            Ok(Err(e)) => {
                error!(path = %path, error = %e, "Failed to receive initialization signal");
                self.remove_actor(&path).await;
                if is_root {
                    self.root_senders.write().await.remove(&path);
                }
                Err(Error::FunctionalCritical {
                    description: e.to_string(),
                })
            }
            Err(timeout) => {
                init_handle.abort();
                self.remove_actor(&path).await;
                if is_root {
                    self.root_senders.write().await.remove(&path);
                }
                Err(Error::Timeout {
                    ms: timeout.as_millis(),
                })
            }
        }
    }

    /// Spawns a top-level actor under `/user/{name}` and returns an `ActorRef` to it.
    ///
    /// `actor_init` can be the actor itself for [`NotPersistentActor`](crate::NotPersistentActor)
    /// types, or any other value implementing [`IntoActor`](crate::IntoActor), such as
    /// `InitializedActor<A>` from `ave-actors-store`.
    ///
    /// Returns `Error::Exists` if a root actor with the same name already exists, or
    /// `Error::SystemStopped` if the system is shutting down.
    pub async fn create_root_actor<A, I>(
        &self,
        name: &str,
        actor_init: I,
    ) -> Result<ActorRef<A>, Error>
    where
        A: Actor + Handler<A>,
        I: crate::IntoActor<A>,
    {
        let actor = actor_init.into_actor();
        let path = ActorPath::from("/user") / name;
        let id = &path.key();

        let (actor_ref, ..) = self
            .create_actor_path::<A>(
                path.clone(),
                actor,
                None,
                A::get_span(id, None),
            )
            .await?;

        // When this root actor fully terminates on its own, remove its stop
        // sender entry so shutdown only sees live roots.
        let root_senders = self.root_senders.clone();
        let watch = actor_ref.clone();
        let watch_path = path.clone();
        tokio::spawn(async move {
            watch.closed().await;
            root_senders.write().await.remove(&watch_path);
        });

        Ok(actor_ref)
    }

    pub(crate) async fn remove_actor(&self, path: &ActorPath) {
        let mut actors = self.actors.write().await;
        let removed = actors.remove(path).is_some();
        drop(actors);
        if removed {
            self.deindex_actor(path).await;
        }
    }

    /// Initiates graceful shutdown, stopping all root actors and causing [`SystemRunner::run`] to return [`ShutdownReason::Graceful`].
    pub fn stop_system(&self) {
        self.shutting_down.store(true, Ordering::SeqCst);
        self.graceful_token.cancel();
    }

    /// Initiates a crash shutdown, causing [`SystemRunner::run`] to return [`ShutdownReason::Crash`] with exit code 1.
    pub fn crash_system(&self) {
        self.shutting_down.store(true, Ordering::SeqCst);
        self.crash_token.cancel();
    }

    /// Returns the paths of all currently registered direct children of the actor at `path`.
    pub async fn children(&self, path: &ActorPath) -> Vec<ActorPath> {
        self.child_index
            .read()
            .await
            .get(path)
            .into_iter()
            .flat_map(|children| children.iter())
            .cloned()
            .collect()
    }

    /// Stores a shared resource (e.g. a database pool or config object) under `name` for retrieval by any actor.
    pub async fn add_helper<H>(&self, name: &str, helper: H)
    where
        H: Any + Send + Sync + Clone + 'static,
    {
        let mut helpers = self.helpers.write().await;
        helpers.insert(name.to_owned(), Box::new(helper));
    }

    /// Returns the helper stored under `name`, or `None` if not found or if the type does not match.
    pub async fn get_helper<H>(&self, name: &str) -> Option<H>
    where
        H: Any + Send + Sync + Clone + 'static,
    {
        let helpers = self.helpers.read().await;
        helpers
            .get(name)
            .and_then(|any| any.downcast_ref::<H>())
            .cloned()
    }

    /// Spawns a [`Sink`] in a background Tokio task so it processes actor events asynchronously.
    pub async fn run_sink<E>(&self, mut sink: Sink<E>)
    where
        E: Event,
    {
        tokio::spawn(async move {
            sink.run().await;
        });
    }
}

/// Drives the actor system event loop; block on [`SystemRunner::run`] to keep the system alive until shutdown.
pub struct SystemRunner {
    /// Receiver for system-wide events.
    event_receiver: mpsc::Receiver<SystemEvent>,
}

impl SystemRunner {
    pub(crate) const fn new(
        event_receiver: mpsc::Receiver<SystemEvent>,
    ) -> Self {
        Self { event_receiver }
    }

    /// Runs the system event loop until shutdown, returning the [`ShutdownReason`] for use as a process exit code.
    pub async fn run(&mut self) -> ShutdownReason {
        debug!("Running actor system");
        loop {
            match self.event_receiver.recv().await {
                Some(SystemEvent::StopSystem(reason)) => {
                    debug!(reason = ?reason, "Actor system stopped");
                    return reason;
                }
                Some(SystemEvent::ActorError { path, error }) => {
                    warn!(path = %path, error = %error, "Ignoring observable ActorError on control channel");
                }
                None => {
                    warn!("System event channel closed unexpectedly");
                    return ShutdownReason::Graceful;
                }
            }
        }
    }
}

#[cfg(test)]
mod tests {

    use super::*;
    use test_log::test;

    #[test(tokio::test)]
    async fn test_helpers() {
        let (system, _) = ActorSystem::create(
            CancellationToken::new(),
            CancellationToken::new(),
        );
        let helper = TestHelper { value: 42 };
        system.add_helper("test", helper).await;
        let helper: Option<TestHelper> = system.get_helper("test").await;
        assert_eq!(helper, Some(TestHelper { value: 42 }));
    }

    #[derive(Debug, Clone, PartialEq)]
    pub struct TestHelper {
        pub value: i32,
    }
}