Akka Actors




@Sander_Mak
Akka
              Part of



              stack

Written in Scala
With officially supported Java API
Akka

     ‘s premise
It’s too hard to write systems that are:

  Concurrent & Correct
  Highly scalable
  Fault-tolerant
Akka

   2.0
Actors
Software Transactional Memory
Dataflow concurrency
Futures
Akka
who uses



           ?
               traffic management
Actors
Inspired by               :


    Asynchronous message passing
    Scale up, scale out
    Fault-tolerant (‘let it crash’)
Actors
How to define an actor?
                         ~400 bytes




                         Local state

                          Behavior




               mailbox
Actors
How to define an actor?
                              ~400 bytes
  Create more actors
  Send messages
  Modify state                Local state

  Modify behavior              Behavior




                    mailbox
Actors
How to define an actor?


class MyFirstActor extends Actor {
  var total = 0

    def receive = {
      case MyMessage(payload) => println(“Received: “ + payload”)
      case countMessage: Int => total += countMessage
      case _ => println("Unknown message")
    }
}
Actors
Dispatcher: contains executor strategy




                           Local state
       Event-driven
                           Behavior




                 mailbox
Actors and Fork/Join
Ping/pong messages (no processing)




                   Improved
 48
                 JSR-166 build (     ?)
cores
Demo
                 NearestCityActor
                 NearestCityActor
                  NearestCityActor
                  NearestCityActor



  RoundRobin                       Result
    Router                     ListenerActor



Messages with cities             Print results
Supervision
Hierarchical supervision
 Child actors supervised by parent
Trap exceptions and apply recovery
Error Kernel pattern

 Actor 1     create child
                actor
                            Actor 2   dangerous
                                      operation
  critical
   state      supervise




Distinguish scratch and critical state
Supervision
class GuardianActor extends Actor {

    def supervisorStrategy = OneForOneStrategy({
        case _:CompanyBankruptException    => Stop
        case _:InvalidStateException       => Restart
        case _:ServiceUnavailableException => Resume
        case _                             => Escalate
     }, maxNrOfRetries = 3, 5.seconds)

    def receive = {
      ...
    }
}




             Actor may implement
            preRestart & postRestart
Remote actors
Dispatchers for scale-up, remote actors for scala-out


   Machine A                           Machine B
                    actor2 ! SomeMsg
      Actor 1                           Actor 2
                     Netty, Protobuf



      Router                            Actor 3


   Actor creation
   Supervision
   Remote deploy through config (no code change)
Remote actors
Creating 10 actors on 2 nodes with a router
 akka	
  {
 	
  	
  actor	
  {
 	
  	
  	
  	
  deployment	
  {
 	
  	
  	
  	
  	
  	
  /serviceA/aggregation	
  {
 	
  	
  	
  	
  	
  	
  	
  	
  router	
  =	
  "round-­‐robin"
 	
  	
  	
  	
  	
  	
  	
  	
  nr-­‐of-­‐instances	
  =	
  10
 	
  	
  	
  	
  	
  	
  	
  	
  target	
  {
 	
  	
  	
  	
  	
  	
  	
  	
  	
  	
  nodes	
  =	
  ["akka://app@10.0.0.2:2552",	
  "akka://app@10.0.0.3:2552"]
 	
  	
  	
  	
  	
  	
  	
  	
  }
 	
  	
  	
  	
  	
  	
  }
 	
  	
  	
  	
  }
 	
  	
  }
 }




Looking up a remote actor
 val	
  actor	
  =	
  
 	
  	
  	
  context.actorFor("akka://actorSystemName@10.0.0.1:2552/user/actorName")
Blocking
Actors lift some ForkJoin restrictions
    Remoting possible
    Recursive decomposition optional
    Fault-tolerant

Still, there are restrictions similar to ForkJoin tasks

    No blocking IO
    No explicit locking
    No shared memory writes
Async IO
 Based on composable Futures

Serve http
                mini               akka-Spray


Consume http
               async-http-client


Persistence
                hammersmith
Async IO
          Based on Java NIO


Akka IO

   Immutable abstractions on top of:
     Channels
     Streams (Iteratee based)
   Bridges (low-level) async IO and actors
Async IO
       Practical application:




  JMeter => 1 user, 1 thread     Gatling => 1 user, 1 actor
+ blocking IO                   + async-http-client

= OutOfMemory errors            =
  or clustering necessary

  time drift in results
Async IO
       Practical application:




  JMeter => 1 user, 1 thread     Gatling => 1 user, 1 actor
+ blocking IO                   + async-http-client

= OutOfMemory errors            =
  or clustering necessary

  time drift in results
Is that all?
             Finite State Machine          Akka TestKit




 core
          Scheduler                 Event Bus




modules
Is that all?
                   Typesafe
                   Console




Commercial
subscription
Questions?


      Code @ bit.ly/bejug-akka