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 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579
//! Implementation of a highly-scalable and ergonomic actor model for Rust //! //! [![Latest version](https://img.shields.io/crates/v/maxim.svg)](https://crates.io/crates/maxim) //! [![Average time to resolve an issue](https://isitmaintained.com/badge/resolution/katharostech/maxim.svg)](https://isitmaintained.com/project/katharostech/maxim) //! [![License](https://img.shields.io/crates/l/maxim.svg)](https://github.com/rsimmonsjr/maxim#license) //! [![Changelog.md](https://img.shields.io/badge/Keep%20a%20Changelog-Changelog.md-blue)](https://github.com/katharostech/maxim/blob/master/CHANGELOG.md) //! //! # Maxim //! //! Maxim is a fork of the [Axiom](https://github.com/rsimmonsjr/axiom) actor framework that was made so we could use the awesome Actor framework while experimenting with our own ideas for Actor framework design. //! //! Maxim brings a highly-scalable actor model to the Rust language based on the many lessons learned over years of Actor model implementations in Akka and Erlang. Maxim is, however, not a direct re-implementation of either of the two aforementioned actor models but rather a new implementation deriving inspiration from the good parts of those projects. //! //! Current development on Maxim is focused on learning how the framework works and experimenting with our design ideas. We will be pushing `0.1.0-alpha` releases with our changes util it gets to a point that is relatively usable. The first thing we've added since the fork was a `spawn_pool` feature that allows you to create pools of actors. This and other features we add are likely to change and adapt as we test them in our projects. //! //! Other things that we are thinking about changing are: //! //! - Using [Agnostik](https://github.com/bastion-rs/agnostik) as an executor to allow Maxim to run on any executor //! - If Agnostik will not suffice for some reason we will probably switch to Tokio for the executor to avoid maintaining our own //! - Adding an optional macro for matching on message type //! - Adding an option to use either bounded or unbounded channels for actor messages ( see "Design Principals of Maxim" below for more info ) //! - This woud probably involve using [Flume](https://github.com/zesterer/flume) for backing the channels //! //! # Getting Started //! //! _An actor model is an architectural asynchronous programming paradigm characterized by the use of actors for all processing activities._ //! //! Actors have the following characteristics: //! //! 1. An actor can be interacted with only by means of messages. //! 2. An actor processes only one message at a time. //! 3. An actor will process a message only once. //! 4. An actor can send a message to any other actor without knowledge of that actor's internals. //! 5. Actors send only immutable data as messages, though they may have mutable internal state. //! 6. Actors are location agnostic; they can be sent a message from anywhere in the cluster. //! //! Note that within the language of Rust, rule five cannot be enforced by Rust but is a best practice which is important for developers creating actors based on Maxim. In Erlang and Elixir rule five cannot be violated because of the structure of the language but this also leads to performance limitations. It's better to allow internal mutable state and encourage the good practice of not sending mutable messages. //! //! What is important to understand is that these rules combined together makes each actor operate like a micro-service in the memory space of the program using them. Since actor messages are immutable, actors can trade information safely and easily without copying large data structures. //! //! Although programming in the actor model is quite an involved process you can get started with Maxim in only a few lines of code. //! //! ```rust //! use maxim::prelude::*; //! use std::sync::Arc; //! use std::time::Duration; //! //! let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2)); //! //! let aid = system //! .spawn() //! .with( //! 0 as usize, //! |state: usize, _context: Context, _message: Message| async move { //! Ok(Status::done(state)) //! } //! ) //! .unwrap(); //! //! aid.send(Message::new(11)).unwrap(); //! //! // It is worth noting that you probably wouldn't just unwrap in real code but deal with //! // the result as a panic in Maxim will take down a dispatcher thread and potentially //! // hang the system. //! //! // This will wrap the value `17` in a Message for you! //! aid.send_new(17).unwrap(); //! //! // We can also create and send separately using just `send`, not `send_new`. //! let message = Message::new(19); //! aid.send(message).unwrap(); //! //! // Another neat capability is to send a message after some time has elapsed. //! aid.send_after(Message::new(7), Duration::from_millis(10)).unwrap(); //! aid.send_new_after(7, Duration::from_millis(10)).unwrap(); //! ``` //! //! This code creates an actor system, fetches a builder for an actor via the `spawn()` method, spawns an actor and finally sends the actor a message. Once the actor is done processing a message it returns the new state of the actor and the status after handling this message. In this case we didnt change the state so we just return it. Creating an Maxim actor is literally that easy but there is a lot more functionality available as well. //! //! Keep in mind that if you are capturing variables from the environment you will have to wrap the `async move {}` block in another block and then move your variables into the first block. Please see the test cases for more examples of this. //! //! If you want to create an actor with a struct that is simple as well. Let's create one that handles a couple of different message types: //! //! ```rust //! use maxim::prelude::*; //! use std::sync::Arc; //! //! let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2)); //! //! struct Data { //! value: i32, //! } //! //! impl Data { //! fn handle_bool(mut self, message: bool) -> ActorResult<Self> { //! if message { //! self.value += 1; //! } else { //! self.value -= 1; //! } //! Ok(Status::done(self)) //! } //! //! fn handle_i32(mut self, message: i32) -> ActorResult<Self> { //! self.value += message; //! Ok(Status::done(self)) //! } //! //! async fn handle(mut self, _context: Context, message: Message) -> ActorResult<Self> { //! if let Some(msg) = message.content_as::<bool>() { //! self.handle_bool(*msg) //! } else if let Some(msg) = message.content_as::<i32>() { //! self.handle_i32(*msg) //! } else { //! panic!("Failed to dispatch properly"); //! } //! } //! } //! //! let data = Data { value: 0 }; //! let aid = system.spawn().name("Fred").with(data, Data::handle).unwrap(); //! //! aid.send_new(11).unwrap(); //! aid.send_new(true).unwrap(); //! aid.send_new(false).unwrap(); //! ``` //! //! This code creates a named actor out of an arbitrary struct. Since the only requirement to make an actor is to have a function that is compliant with the [`maxim::actors::Processor`] trait, anything can be an actor. If this struct had been declared somewhere outside of your control you could use it in an actor as state by declaring your own handler function and making the calls to the 3rd party structure. //! //! _It's important to keep in mind that the starting state is moved into the actor and you will not have external access to it afterwards._ This is by design and although you could conceivably use a [`Arc`] or [`Mutex`] enclosing a structure as state, that would definitely be a bad idea as it would break the rules we laid out for actors. //! //! # Detailed Examples //! //! - [Hello World](https://github.com/katharostech/maxim/blob/master/examples/hello_world.rs): The //! obligatory introduction to any computer system. //! - [Dining Philosophers](https://github.com/katharostech/maxim/blob/master/examples/philosophers.rs): //! An example of using Maxim to solve a classic Finite State Machine problem in computer science. //! - [Monte Carlo](https://github.com/katharostech/maxim/blob/master/examples/montecarlo.rs): An //! example of how to use Maxim for parallel computation. //! //! ## Design Principals of Maxim //! //! These are the core principals of Axiom, the project Maxim was forked from: //! //! 1. **At its core an actor is just an function that processes messages.** The simplest actor is a //! function that takes a message and simply ignores it. The benefit to the functional approach over //! the Akka model is that it allows the user to create actors easily and simply. This is the notion //! of _micro module programming_; the notion of building a complex system from the smallest //! components. Software based on the actor model can get complicated; keeping it simple at the core //! is fundamental to solid architecture. //! 2. **Actors can be a Finite State Machine (FSM).** Actors receive and process messages nominally //! in the order received. However, there are certain circumstances where an actor has to change to //! another state and process other messages, skipping certain messages to be processed later. //! 3. **When skipping messages, the messages must not move.** Akka allows the skipping of messages //! by _stashing_ the message in another data structure and then restoring this stash later. This //! process has many inherent flaws. Instead Axiom allows an actor to skip messages in its channel //! but leave them where they are, increasing performance and avoiding many problems. //! 4. **Actors use a bounded capacity channel.** In Axiom the message capacity for the actor's //! channel is bounded, resulting in greater simplicity and an emphasis on good actor design. //! 5. **Axiom should be kept as small as possible.** Axiom is the core of the actor model and //! should not be expanded to include everything possible for actors. That should be the job of //! libraries that extend Axiom. Axiom itself should be an example of _micro module programming_. //! 6. **The tests are the best place for examples.** The tests of Axiom will be extensive and well //! maintained and should be a resource for those wanting to use Axiom. They should not be a dumping //! ground for copy-paste or throwaway code. The best tests will look like architected code. //! 7. **A huge emphasis is put on crate user ergonomics.** Axiom should be easy to use. //! //! The principals that Maxim may **not** preserve are principals 4 and 6. To address those: //! //! - **Bounded capacity channel:** While it may be best to have a bounded capacity channel, we will need to do some experimentation with the design before we settle our own opinion on it and our initial reaction is that it would be good to have the user be allowed to choose. As far as complexity is concerned we will probably look into out-sourcing our channel implementation to something like [Flume](https://github.com/zesterer/flume). There has not been enough investigation made to make such statements with any certainty, though. //! - **The tests are the best place for examples:** While we agree that tests should be examples of how the code will actually be used, we are less along the lines of telling users to go look at the unit tests to find out how to use the library. We want the documentation to be rich and helpful to the users so that they don't _have_ to look at the tests to find out how to use the tool. //! //! [`maxim::actors::processor`]: https://docs.rs/maxim/latest/maxim/actors/trait.Processor.html //! [`arc`]: https://doc.rust-lang.org/stable/std/sync/struct.Arc.html //! [`mutex`]: https://doc.rust-lang.org/stable/std/sync/struct.Mutex.html use std::any::Any; use std::error::Error; use std::fmt::{Display, Formatter}; // Re-export futures so the user doesn't need to import it. pub use futures; use prelude::*; pub mod actors; pub mod cluster; mod executor; pub mod message; pub mod system; pub mod prelude; /// A helper alias to ensure returned errors conform as needed. pub type StdError = Box<dyn Error + Send + Sync + 'static>; /// A type for a result from an actor's message processor. /// A Result::Err is treated as a fatal error, and the Actor will be stopped. pub type ActorResult<State> = Result<(State, Status), StdError>; #[derive(Debug)] pub struct Panic { panic_payload: String, } impl Display for Panic { fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { write!(f, "{}", self.panic_payload) } } impl Error for Panic {} impl From<Box<dyn Any + Send + 'static>> for Panic { fn from(val: Box<dyn Any + Send + 'static>) -> Self { let panic_payload = match val.downcast::<&'static str>() { Ok(s) => String::from(*s), Err(val) => match val.downcast::<String>() { Ok(s) => *s, Err(_) => String::from("Panic payload unserializable"), }, }; Self { panic_payload } } } #[cfg(test)] mod tests { use std::thread; use std::time::Duration; use log::LevelFilter; use secc::{SeccReceiver, SeccSender}; use serde::{Deserialize, Serialize}; use super::*; #[derive(Clone)] pub struct AssertCollect { tx: SeccSender<(bool, String)>, rx: SeccReceiver<(bool, String)>, } impl AssertCollect { pub fn new() -> Self { let (tx, rx) = secc::create(256, Duration::from_millis(10)); Self { tx, rx } } pub fn assert(&self, cond: bool, msg: impl Into<String>) { let m = msg.into(); self.tx.send((cond, m.clone())).unwrap(); if !cond { panic!("{}", m) } } pub fn panic(&self, msg: impl Into<String>) -> ! { let m = msg.into(); self.tx.send((false, m.clone())).unwrap(); panic!("{}", m) } pub fn collect(&self) { while let Ok((cond, s)) = self.rx.receive() { assert!(cond, "{}", s); } } } pub fn init_test_log() { let _ = env_logger::builder() .filter_level(LevelFilter::Warn) .is_test(true) .try_init(); } pub fn sleep(millis: u64) { thread::sleep(Duration::from_millis(millis)) } /// A function that just returns `Ok(Status::Done)` which can be used as a handler for /// a simple dummy actor. pub async fn simple_handler(_: (), _: Context, _: Message) -> ActorResult<()> { Ok(Status::done(())) } /// A utility that waits for a certain number of messages to arrive in a certain time and /// returns an `Ok<()>` when they do or an `Err<String>` when not. pub fn await_received(aid: &Aid, count: u8, timeout_ms: u64) -> Result<(), String> { use std::time::Instant; let start = Instant::now(); let duration = Duration::from_millis(timeout_ms); while aid.received().unwrap() < count as usize { if Instant::elapsed(&start) > duration { return Err(format!( "Timed out after {}ms! Messages received: {}; Messages expected: {}", timeout_ms, aid.received().unwrap(), count )); } } Ok(()) } #[test] #[should_panic] fn test_assert_receive() { let tracker = AssertCollect::new(); let t2 = tracker.clone(); let join = thread::spawn(move || t2.panic("This is a panic")); let _ = join.join(); tracker.collect(); } /// This test shows how the simplest actor can be built and used. This actor uses a closure /// that simply returns that the message is processed without doing anything with it. #[test] fn test_simplest_actor() { init_test_log(); let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2)); // We spawn the actor using a closure. Note that because of a bug in the Rust compiler // as of 2019-07-12 regarding type inference we have to specify all of the types manually // but when that bug goes away this will be even simpler. let aid = system .spawn() .with((), |_: (), _: Context, _: Message| async { Ok(Status::done(())) }) .unwrap(); // Send a message to the actor. aid.send_new(11).unwrap(); // The actor will get two messages including the Start message. await_received(&aid, 2, 1000).unwrap(); system.trigger_and_await_shutdown(None); } /// This test shows how the simplest struct-based actor can be built and used. This actor /// merely returns that the message was processed. #[test] fn test_simplest_struct_actor() { init_test_log(); let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2)); // We declare a basic struct that has a handle method that does basically nothing. // Subsequently we will create that struct as a starting state when we spawn the actor // and then send the actor a message. struct Data {} impl Data { async fn handle(self, _: Context, _: Message) -> ActorResult<Self> { Ok(Status::done(self)) } } let aid = system.spawn().with(Data {}, Data::handle).unwrap(); // Send a message to the actor. aid.send_new(11).unwrap(); await_received(&aid, 2, 1000).unwrap(); system.trigger_and_await_shutdown(None); } /// This test shows how a closure based actor can be created to process different kinds of /// messages and mutate the actor's state based upon the messages passed. Note that the /// state of the actor is not available outside the actor itself. #[test] fn test_dispatching_with_closure() { init_test_log(); let tracker = AssertCollect::new(); let t = tracker.clone(); let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2)); let starting_state: usize = 0 as usize; let closure = move |mut state: usize, context: Context, message: Message| { let t = t.clone(); async move { // Expected messages in the expected order. let expected: Vec<i32> = vec![11, 13, 17]; // Attempt to downcast to expected message. if let Some(_msg) = message.content_as::<SystemMsg>() { state += 1; Ok(Status::done(state)) } else if let Some(msg) = message.content_as::<i32>() { t.assert(expected[state - 1] == *msg, "Unexpected message content"); t.assert(state == context.aid.received().unwrap(), "Unexpected state"); state += 1; Ok(Status::done(state)) } else if let Some(_msg) = message.content_as::<SystemMsg>() { // Note that we put this last because it only is ever received once, we // want the most frequently received messages first. Ok(Status::done(state)) } else { t.panic("Failed to dispatch properly") } } }; let aid = system.spawn().with(starting_state, closure).unwrap(); // First message will always be the SystemMsg::Start. assert_eq!(1, aid.sent().unwrap()); // Send some messages to the actor in the order required in the test. In a real actor // its unlikely any order restriction would be needed. However this test makes sure that // the messages are processed correctly. aid.send_new(11 as i32).unwrap(); assert_eq!(2, aid.sent().unwrap()); aid.send_new(13 as i32).unwrap(); assert_eq!(3, aid.sent().unwrap()); aid.send_new(17 as i32).unwrap(); assert_eq!(4, aid.sent().unwrap()); await_received(&aid, 4, 1000).unwrap(); system.trigger_and_await_shutdown(None); tracker.collect(); } /// This test shows how a struct-based actor can be used and process different kinds of /// messages and mutate the actor's state based upon the messages passed. Note that the /// state of the actor is not available outside the actor itself. #[test] fn test_dispatching_with_struct() { init_test_log(); let tracker = AssertCollect::new(); let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2)); // We create a basic struct with a handler and use that handler to dispatch to other // inherent methods in the struct. Note that we don't have to implement any traits here // and there is nothing forcing the handler to be an inherent method. struct Data { value: i32, tracker: AssertCollect, } impl Data { fn handle_bool(mut self, message: bool) -> ActorResult<Self> { if message { self.value += 1; } else { self.value -= 1; } Ok(Status::done(self)) // This assertion will fail but we still have to return. } fn handle_i32(mut self, message: i32) -> ActorResult<Self> { self.value += message; Ok(Status::done(self)) // This assertion will fail but we still have to return. } async fn handle(self, _context: Context, message: Message) -> ActorResult<Self> { if let Some(msg) = message.content_as::<bool>() { self.handle_bool(*msg) } else if let Some(msg) = message.content_as::<i32>() { self.handle_i32(*msg) } else if let Some(_msg) = message.content_as::<SystemMsg>() { // Note that we put this last because it only is ever received once, we // want the most frequently received messages first. Ok(Status::done(self)) } else { self.tracker.panic("Failed to dispatch properly") } } } let data = Data { value: 0, tracker: tracker.clone(), }; let aid = system.spawn().with(data, Data::handle).unwrap(); // Send some messages to the actor. aid.send_new(11).unwrap(); aid.send_new(true).unwrap(); aid.send_new(true).unwrap(); aid.send_new(false).unwrap(); await_received(&aid, 4, 1000).unwrap(); system.trigger_and_await_shutdown(None); tracker.collect(); } /// Tests and demonstrates the process to create a closure that captures the environment /// outside the closure in a manner sufficient to be used in a future. #[test] fn test_closure_with_move() { init_test_log(); let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2)); let target_aid = system.spawn().with((), simple_handler).unwrap(); let aid_moved = target_aid.clone(); // clone for the closure let aid = system .spawn() .with((), move |_: (), _: Context, _: Message| { // Each future needs its own copy of the target aid. let tgt = aid_moved.clone(); async move { tgt.send_new(11)?; Ok(Status::done(())) } }) .unwrap(); aid.send_new(11).unwrap(); await_received(&target_aid, 2, 1000).unwrap(); system.trigger_and_await_shutdown(None); } /// Tests an example where one actor starts another actor, the actors exchange a simple /// ping-pong message and then the first actor triggers a shutdown when the pong message is /// received. Note that these actors just use simple functions to accomplish the task though /// they could have used functions on structures, closures, and even had a multiple methods /// to handle the messages. #[test] fn test_ping_pong() { /// A simple enum used as test messages. #[derive(Serialize, Deserialize)] pub enum PingPong { Ping(Aid), Pong, } async fn ping(_: (), context: Context, message: Message) -> ActorResult<()> { if let Some(msg) = message.content_as::<PingPong>() { match &*msg { PingPong::Pong => { context.system.trigger_shutdown(); Ok(Status::done(())) } _ => Err("Unexpected message".to_string().into()), } } else if let Some(msg) = message.content_as::<SystemMsg>() { // Start messages happen only once so we keep them last. match &*msg { SystemMsg::Start => { // Now we will spawn a new actor to handle our pong and send to it. let pong_aid = context.system.spawn().with((), pong)?; pong_aid.send_new(PingPong::Ping(context.aid.clone()))?; Ok(Status::done(())) } _ => Ok(Status::done(())), } } else { Ok(Status::done(())) } } async fn pong(_: (), _: Context, message: Message) -> ActorResult<()> { if let Some(msg) = message.content_as::<PingPong>() { match &*msg { PingPong::Ping(from) => { from.send_new(PingPong::Pong)?; Ok(Status::done(())) } _ => Err("Unexpected message".into()), } } else { Ok(Status::done(())) } } let system = ActorSystem::create(ActorSystemConfig::default().thread_pool_size(2)); system.spawn().with((), ping).unwrap(); system.await_shutdown(None); } }