Akka2

3,534 views

Published on

Slides from my Akka 2 presentation on Paris Scala User Group (28/06/2012)

0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,534
On SlideShare
0
From Embeds
0
Number of Embeds
11
Actions
Shares
0
Downloads
40
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • Akka2

    1. 1. Akka 2David Galichetconsultant @ XebiaCTO @ CoachClub
    2. 2. Akka != Haka
    3. 3. Schedule• Why Akka ?• Actor model• Futures API• Supervision / Fault tolerance• Remote actors• What’s next ?
    4. 4. Why Akka ?• We need to use multiple CPU cores• But concurrent programming is complex !
    5. 5. Why Akka ?• Distributed computing is necessary ... • ... to get more power at lower price • ... to get high availability• Akka helps building distributed applications
    6. 6. Why Akka ?• Distributed applications are error prone (network, error rate increase with number of nodes ...)• Fault tolerance / Embrace failure principle “Let it crash” model
    7. 7. Why Akka ? • Other features provided by Akka • Software Transactional Memory (via Transactors) • Finite State Machine (will gain popularity with subjects like Event Sourcing) • Dataflow Concurrency • IO, Agents ...Unfortunately will not be covered during this session.
    8. 8. Schedule• Why Akka ?• Actor model• Futures API• Supervision / Fault tolerance• Remote actors• What’s next ?
    9. 9. Actor model• Simple and high-level abstraction for concurrency and parallelism• Asynchronous, non-blocking and highly performant event-driven programming level• Very lightweight event-driven processes
    10. 10. Actor model• An Akka Actor has : • a state • a behavior • a mailbox • potentially some children • a supervisor strategy
    11. 11. Actor model• Communications between Actors • tell (!) : fire and forget • ask (?) : returns a Future • forward : forward the message to another Actor• Messages are posted into Actor mailbox
    12. 12. Actor model• Actor send rule : the send of the message to Actor happens before the receive of that message by the same Actor• Actor subsequent processing rule : processing of one message happens before processing of the next message by the same Actor
    13. 13. Actor model• An Actor will be executed by only one Thread at a given time (processing a message)• An Actor will be executed by different Threads Never use ThreadLocal !
    14. 14. Actor model• Akka Actors Hierarchy • an Actor is supervised by another Actor • root hierarchy is a specific ActorSystem
    15. 15. Actor model • A dispatcher : • defines the execution context • is used by Actors or Futures for executionDon’t forget ‘import context._’ to implicitly definecurrent Dispatcher !
    16. 16. Actor model • Routers • a special Actor that route message to other Actors • messages are routed to ‘rootees’ • e.g. RoundRobingRouter for load balancingsystem.actorOf(Props[Worker].withRouter( RoundRobinRouter(nrOfInstances = 5)))
    17. 17. Schedule• Why Akka ?• Actor model• Futures API• Supervision / Fault tolerance• Remote actors• What’s next ?
    18. 18. Future API• Futures are monads •map •flatMap • for comprehension •sequence • ...
    19. 19. Future APIval f1: Future[Matrix] = (Grid ?Inv(x)).mapTo[Matrix]val f2: Future[Matrix] = (Grid ?Inv(y)).mapTo[Matrix]def product(x: Matrix, y: Matrix): Future[Matrix] =(Grid ? Product(x, y)).mapTo[Matrix]val future: Future[(Matrix,Matrix,Matrix)] = for { ix <- f1 // f1.flatMap( ix => iy <- f2 // f2.flatMap( iy => z <- product(ix, iy) // product(ix, iy).map(z =>} yield (ix, iy, z) // (ix, iy, z))))
    20. 20. DEMO !
    21. 21. Schedule• Why Akka ?• Actor model• Futures API• Supervision / Fault tolerance• Remote actors• What’s next ?
    22. 22. Actors Supervision• Each Actor is supervised by its parent Actor• Parent Actor manages child’s lifecycle (start, stop ...)• Supervision Strategy defines what to do when an Actor fails
    23. 23. Actors Supervision• Supervision actions in case of failure (Exception) • resume : resume the Actor • restart : replace the Actor by a new one (loose data if stateful) • stop : stop the Actor • escalate : ask parent Actor what to do• We can fine tune by Exception type
    24. 24. Actors Supervision• Supervision strategy • one-for-one : action performed only on the failing Actor • all-for-one : action performed on all children• all-for-one strategy is mainly used in case of functional dependencies between children• It’s possible to define a maximum number of restart before stopping the Actor
    25. 25. Actors Supervision • It’s also possible to watch an Actor • When an Actor stops, all watchers will be notifier by a Terminated message referencing the stopped Actorcontext.watch(failingActRef)def receive = { ... case Terminated(`failingActRef`) => // embracefailure}
    26. 26. DEMO !
    27. 27. Schedule• Why Akka ?• Actor model• Futures API• Supervision / Fault tolerance• Remote actors• What’s next ?
    28. 28. Remote Actors• Create an Actor on a different SystemActor/JVM/Node and look for it• Remote deploy Actor on another SystemActor (potentially on another JVM or cluster node)
    29. 29. Remote Actors• Take benefit of Location Transparency • an Actor children can be on another node • don’t care if it’s a local or a remote Actor (part of improvement on Akka 2)
    30. 30. Remote Actors• In Akka 1, remote Actors where as special type of Actors change the program to distribute Actors• In Akka 2, remote Actors are ‘normal’ Actors with specific configuration Actor remoting is now configuration centric
    31. 31. DEMO !
    32. 32. Schedule• Why Akka ?• Actor model• Futures API• Supervision / Fault tolerance• Remote actors• What’s next ?
    33. 33. What’s next ?• Clustering• Failure detection• Adaptive clustered routers (load ...)• Automatic replication of Actors (stateless & stateful) and failover• ...
    34. 34. What’s next ?• Clustering• Failure detection• Adaptive clustered routers (load ...)• Automatic replication of Actors (stateless & stateful) and failover• ... See you on Q1 2013 to explore that ?

    ×