a gentle introduction into akka and the actor model

43
A gentle introduction into AKKA and the actor model Mike Kotsur, 2016 github.com/mkotsur

Upload: mykhailo-kotsur

Post on 09-Jan-2017

385 views

Category:

Software


0 download

TRANSCRIPT

Page 1: A gentle introduction into AKKA and the actor model

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

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

2 ways

• Simplicity

• Speed

Page 4: A gentle introduction into AKKA and the actor model

[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
Page 6: A gentle introduction into AKKA and the actor model
Page 7: A gentle introduction into AKKA and the actor model
Page 8: A gentle introduction into AKKA and the actor model
Page 9: A gentle introduction into AKKA and the actor model

More CPUs — more threads

Your code:if (check (seat, row))

book (seat, row)

Page 10: A gentle introduction into AKKA and the actor model

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
Page 12: A gentle introduction into AKKA and the actor model

But we have smart databases!

Page 13: A gentle introduction into AKKA and the actor model

• 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

– 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
Page 16: A gentle introduction into AKKA and the actor model

What an actor is not…Future Thread Actor

Processing + + +

Storage + +

Communication +

Page 17: A gentle introduction into AKKA and the actor model

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

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

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

Could we use this to sell tickets?

Page 21: A gentle introduction into AKKA and the actor model

Actors in 22 lines

https://gist.github.com/viktorklang/2362563

Page 22: A gentle introduction into AKKA and the actor model

➡ Actors

• Persistence

• Networking

• Clustering

• Streams

• HTTP

• Testkits

Page 23: A gentle introduction into AKKA and the actor model
Page 24: A gentle introduction into AKKA and the actor model

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

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

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

More about messaging tomorrow

Page 28: A gentle introduction into AKKA and the actor model
Page 29: A gentle introduction into AKKA and the actor model
Page 30: A gentle introduction into AKKA and the actor model

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
Page 32: A gentle introduction into AKKA and the actor model

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

Changing the behaviour: delayed sales example.

Page 34: A gentle introduction into AKKA and the actor model

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

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

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

But what about persistence?

Page 38: A gentle introduction into AKKA and the actor model

Traditional way: Persisting the state

Event sourcing: persisting the events

Page 39: A gentle introduction into AKKA and the actor model

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

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

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

What next?

Page 43: A gentle introduction into AKKA and the actor model

Thanks.

Some code is here: https://github.com/mkotsur/actors-introduction

And let’s chat!