A gentle introduction into AKKA and the actor model
-
Upload
mykhailo-kotsur -
Category
Software
-
view
386 -
download
0
Transcript of A gentle introduction into AKKA and the actor model
![Page 1: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/1.jpg)
A gentle
introduction
into AKKA and
the actor model
Mike Kotsur, 2016 github.com/mkotsur
![Page 2: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/2.jpg)
Goals• A problem (one of them) and traditional solutions
• Actor Model (since 1973)
• Akka Actors
• The rest of Akka goodies
• To choose, or not to choose (Akka)
![Page 3: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/3.jpg)
2 ways
• Simplicity
• Speed
![Page 4: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/4.jpg)
[1] Herb Sutter. The Free Lunch Is Over: A Fundamental Turn Toward Concurrency in Software, 2005. – http://www.gotw.ca/publications/concurrency-ddj.htm
Concurrency is the next major revolution
in how we write software [1].
![Page 5: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/5.jpg)
![Page 6: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/6.jpg)
![Page 7: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/7.jpg)
![Page 8: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/8.jpg)
![Page 9: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/9.jpg)
More CPUs — more threads
Your code:if (check (seat, row))
book (seat, row)
![Page 10: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/10.jpg)
More CPUs — more threads
check (1, 10)
book (1, 10)
check (1, 10)
book (1, 10)
check (1, 10)
check (1, 10)
book (1, 10)
book (1, 10)
:-(
![Page 11: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/11.jpg)
![Page 12: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/12.jpg)
But we have smart databases!
![Page 13: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/13.jpg)
• Managing shared state requires synchronisation;
• Which leads to keeping the threads busy waiting;
• The granularity control is limited;
• There are different solutions to the problem: • synchronized,
• database,
• something else?
![Page 14: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/14.jpg)
– Carl Hewitt
“An actor is the fundamental unit of computation which embodies the 3 things –
processing, storage and communications – that are essential to computation.”
![Page 15: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/15.jpg)
![Page 16: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/16.jpg)
What an actor is not…Future Thread Actor
Processing + + +
Storage + +
Communication +
![Page 17: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/17.jpg)
What an actor can do?
• Designate how to handle the next message it receives;
• Create new actors;
• Send messages to actors it knows.
![Page 18: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/18.jpg)
Messages
• Processed one at a time;
• May be not delivered in the same order as sent;
• At-most-once delivery guarantee.
![Page 19: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/19.jpg)
Why actors are interesting?
• Unified model for concurrency and parallelism;
• Out-of-the box multi-server support.
![Page 20: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/20.jpg)
Could we use this to sell tickets?
![Page 21: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/21.jpg)
Actors in 22 lines
https://gist.github.com/viktorklang/2362563
![Page 22: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/22.jpg)
➡ Actors
• Persistence
• Networking
• Clustering
• Streams
• HTTP
• Testkits
![Page 23: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/23.jpg)
![Page 24: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/24.jpg)
object TicketDeskActor {
case class BookTicket(eventId: String, row: Int, seat: Int)
case class TicketBooked(ticketId: Long)
}
class TicketDeskActor extends Actor {
override def receive: Receive = { case BookTicket(eventId, row, seat) => ??? }
}
![Page 25: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/25.jpg)
val mySystem = ActorSystem("TheatreApp")
val myProps: Props = Props[TicketDeskActor]
val myActor: ActorRef = mySystem. actorOf(myProps, "ticket-desk")
print(s"Created ${myActor.path}") // Created akka://TheatreApp/user/ticket-desk
mySystem.terminate()
Actor recipe
Actor path
Actor ref
![Page 26: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/26.jpg)
val result: Future[Any] = myActor ? BookTicket(…)
myActor ! BookTicket(…)
Tell (a.k.a. “fire and forget”)
Ask
![Page 27: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/27.jpg)
More about messaging tomorrow
![Page 28: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/28.jpg)
![Page 29: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/29.jpg)
![Page 30: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/30.jpg)
class TicketDeskActor extends Actor {
override def receive: Receive = { case msg @ BookTicket(eventId, _, _) => val evtActorName = s"event-$eventId" val evtActorProps = Props(new EventActor(eventId))
val eventActorRef = context.child(evtActorName).getOrElse { context.actorOf(evtActorProps, evtActorName) }
eventActorRef forward msg }
}
![Page 31: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/31.jpg)
![Page 32: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/32.jpg)
class EventActor(eventId: String) extends Actor with ActorLogging {
override def preStart(): Unit = { log.info(s"Starting an event actor for $eventId") }
override def receive: Receive = { case BookTicket(_, seat, row) => log.info("Updating the state with the newly booked ticket") }
}
[INFO] [04/03/2016 20:17:57.907] [TheatreApp-akka.actor.default-dispatcher-3] [akka://TheatreApp/user/ticket-desk/event-JustinBieber] Starting an event actor for JustinBieber
Actor path
Logging trait
![Page 33: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/33.jpg)
Changing the behaviour: delayed sales example.
![Page 34: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/34.jpg)
object DelayedEventActor { case object SaleIsNotStartedYet case object StartSale }
class DelayedEventActor(eventId: String, saleStarts: LocalDateTime) extends EventActor(eventId) with ActorLogging {
var cancellable: Option[Cancellable] = None
override def preStart(): Unit = { super.preStart() val delay = Duration.between(saleStarts, LocalDateTime.now()) import context.dispatcher cancellable = Some( context.system.scheduler.scheduleOnce(delay.toNanos nanos, self, StartSale) ) }
override def postStop(): Unit = { super.postStop() cancellable.foreach(_.cancel()) }
override def receive: Receive = { case StartSale => context.become(super.receive) case msg: BookTicket => log.warning(s"Trying to book a ticket too early") sender() ! SaleIsNotStartedYet }
}
Scheduling a message to self
Changing the behaviour
Extends the normal EventActor
![Page 35: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/35.jpg)
Lifecycle: stopping• Actors can be terminated by stop() or PoisonPill
message;
• postStop() method is called;
• The rest of the messages is sent to the dead letters;
• If an actor is terminated, all its children are terminated as well.
![Page 36: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/36.jpg)
Lifecycle: fault tolerance
• Supervision by parent actors;
• On exception: Resume, Restart, Stop, or Escalate, based on the supervision strategy;
![Page 37: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/37.jpg)
But what about persistence?
![Page 38: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/38.jpg)
Traditional way: Persisting the state
Event sourcing: persisting the events
![Page 39: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/39.jpg)
Clustering• Actor model is very good for clustering;
• Akka has very good clustering support:
• Sharding;
• Cluster singleton;
• Other stuff. Check it out.
![Page 40: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/40.jpg)
Is Akka a good choice for you?
Paul Chiusano: Actors are overly nondeterminstic :-(
Roland Kuhn: …they are the essence of concurrency and therefore by nature as non-
deterministic as they can be.
Actors are overly nondeterminstic, Paul Chiusano's blog, 2013 — http://pchiusano.blogspot.nl/2013/09/actors-are-overly-nondeterminstic.html
![Page 41: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/41.jpg)
How to start?
• Find a task where the advantage is obvious!
• Micro service Pet-project scale first?
• The structure and lifecycle is important!
![Page 42: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/42.jpg)
What next?
![Page 43: A gentle introduction into AKKA and the actor model](https://reader031.fdocuments.in/reader031/viewer/2022030305/58729f8e1a28ab07208b571b/html5/thumbnails/43.jpg)
Thanks.
Some code is here: https://github.com/mkotsur/actors-introduction
And let’s chat!