up, up and out - scaling software with akka henrik engstrÖm software engineer - typesafe @h3nk3

44
Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Upload: cornelius-collins

Post on 05-Jan-2016

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Up, up and out - Scaling Software

with Akka

HENRIK ENGSTRÖMSOFTWARE ENGINEER - TYPESAFE

@h3nk3

Page 2: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

✦ Typesafe Console Tech Lead

✦ Akka Honorary Team Member

✦ Programming Scala since 2010

$ whoami

✦ Consultant since 1998 - mainly Java

✦ Been with Typesafe since 2011

✦ Arsenal Supporter + Martial Artist

Page 3: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

About Typesafe

✦ The Typesafe Platform

‣ Play, Akka, Scala, Scala IDE, Slick, SBT, etc.

✦ Subscription

✦ Training and Consulting

Page 4: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Agenda

✦ Akka Introduction

✦ Core Operations

✦ Let’s Build Something with Actors

✦ Akka Cluster

✦ Typesafe Console

Page 5: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Copyright Ingeborg van Leeuwen

Page 6: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Selection of Akka Production Users

Page 7: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3
Page 8: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

At the core - Actors✦ Carl Hewitt’s definition (1973)

‣ Processing + Storage + Communication

✦ 3 Axioms - an Actor can

- create new actors

- send messages to Actors

- designate how to handle next message

Page 9: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Proven Concept✦ Actors have successfully been

used in the Telecom industry since the 1980s

✦ Unbelievably stable:

‣ 9 nines uptime or ...

‣ 0.999999999% uptime or ...

‣ 32 ms downtime per year

Page 10: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Akka Actors

✦ Akka Actors are extremely lightweight

✦ Each Actor has its own mailbox

✦ Communication done via asynchronous message passing

✦ Very memory efficient

‣ You can have approx. 2.7 million actors/GB

Page 11: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

ActorActor MailboxMailbox

Akka Actor in Action

Page 12: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Core Actor Operations

1)DEFINE

2)CREATE

3)SEND

4)SUPERVISE

Page 13: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

case class Greeting(who: String)class GreetActor extend Actor with ActorLogging { var counter = 0 def receive = { case Greeting(who) => counter += 1 log.info("Hello %s, counter = %s". format(counter, who)) }}

1) DEFINE

Page 14: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

val system = ActorSystem("MyFirstActorSystem")val greetActor: ActorRef = system.actorOf(Props[GreetingActor], "greeter")

2) CREATE

Page 15: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

AA

BB

BarBarFooFoo

CC

BBEE

AA

DD

CC

Guardian System Actor

system.actorOf(Props[Foo], "Foo")

context.actorOf(Props[A], "A")

Actors can form hierarchies

Page 16: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

AA

BB

BarBarFooFoo

CC

BBEE

AA

DD

CC

/Foo

/Foo/A

/Foo/A/B

/Foo/A/D

Guardian System Actor

Name resolution - like a file-system

Page 17: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Sending✦ Communication done via message

passing

✦ Fire and forget, i.e. asynchronous and non-blocking

✦ Reactive Programming

- messages are kinetic energy

- huge buffered potential energy at rest

Page 18: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Throughput on single box

with 48 cores

+50 million messages per second

Page 19: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

greetActor ! Greeting("JavaOneShanghai")// orgreetActor.tell(Greeting("JavaOneShanghai"))

3) SEND

Page 20: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

case class Greeting(who: String)class GreetActor extend Actor with ActorLogging { var counter = 0 def receive = { case Greeting(who) => counter += 1 log.info("Hello %s, counter = %s".format(counter, who)) }}val system = ActorSystem("MyFirstActorSystem")val greetActor: ActorRef = system.actorOf(Props[GreetingActor], "greeter")greetActor ! Greeting("JavaOneShanghai")

Full Example

Page 21: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Supervision✦ Manage another Actor’s failures

✦ Notification based

‣ if an Actor crashes a notification will be sent to the supervisor

✦ Clean separation of processing/business logic and error handling

Page 22: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

ErrorKernel

Page 23: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

class MyActor extends Actor { override val supervisionStrategy = OneForOneStrategy() { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: IllegalArgumentException => Stop case _: Exception => Escalate } // actor implementation }

4) SUPERVISE

Page 24: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

class ActorA extends Actor { val b = context.actorOf(Props[ActorB], "b") context.watch(b) def receive = { case Terminated(a) => // take proper action }}

Death Watch

Page 25: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Akka Configuration

✦ All settings that are programmable are also configurable

‣ A simple restart can totally change the behavior of your application

‣ Will keep your DevOps happy

Page 26: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Let’s Build Something!

✦ Let’s create a hierarchy of words

‣ Each letter becomes one actor

‣ They form a parent child relationship

‣ "hi" becomes Actor("h") as parent to Actor("i")

‣ Each actor has an internal counter to keep track of number of occurrences

Page 27: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Example Hierarchyhhhh

ii22ii22

ee99ee99

llll

ll22ll22

oo44oo44

rooroott

rooroott

ii33ii33

ss1414ss

1414

Page 28: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

case class CreateWord(word: String)class LetterActor extends Actor { var count = 0 def receive = { case CreateWord(word) => if(word.isEmpty) count += 1 else { val firstLetter = word.head.toString context.child(firstLetter). getOrElse(context.actorOf(Props[LetterActor], firstLetter)) ! CreateWord(word.tail) } } }

LetterActor

Page 29: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

class WordActor extends Actor { def receive = { case CreateWord(word) => val firstLetter = word.head.toString context.child(firstLetter). getOrElse(context.actorOf(Props[LetterActor], firstLetter)) ! CreateWord(word.tail) }}

WordActor

Page 30: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

class MainActor extends Actor { val root = context.actorOf(Props[WordActor], "wordActor") def receive = { case cw: CreateWord => root ! cw }}

MainActor

Page 31: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

object WordDemo { def main(args: Array[String]) { val system = ActorSystem("WordCount") val main = system.actorOf(Props[MainActor], "mainActor") main ! CreateWord("hello") 1 to 3 foreach { c => main ! CreateWord("hi") } main ! CreateWord("hell") main ! CreateWord("helium") }}

Main Program

Page 32: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Resulting Hierarchy

hhhh eeeellll

ll11ll11

oo11oo11

rooroott

rooroott

ii33ii33

iiii

uuuu

mm11mm11

Page 33: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Akka Cluster

✦ Gossip-based cluster membership

✦ Leader determination

✦ Accrual failure detection

✦ Cluster death watch

✦ Cluster aware routers

Page 34: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Cluster Membership

✦ Node ring style inspired by Riak/Dynamo

✦ Gossip protocol - used for

- membership

- configuration data

- leader determination

✦ Vector clocks to detect convergence

Page 35: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Node Ring with Gossiping Members

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

Gossip

Page 36: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Phi Accrual Failure Detector

• B monitors A

• Sample inter-arrival time to expect next beat

• B measures continuum of deadness of A

AA BBregular messages

http://ddg.jaist.ac.jp/pub/HDY+04.pdf

Page 37: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Selective Failure Detection

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

Heartbeat

Page 38: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Selective Failure Detection

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

MembMemberer

NodeNode

Heartbeat

Page 39: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Cluster Death Watch

✦ Triggered by marking node <A> DOWN

‣ Tell parents of their lost children on <A>

‣ Delete all children of actors on <A>

‣ Send TERMINATED for actors on <A>

Page 40: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

akka { actor { provider = "akka.cluster.ClusterActorRefProvider" ... } extensions = ["akka.cluster.Cluster"] cluster { seed-nodes = [ "akka://[email protected]:2551", "akka://[email protected]:2552" ] auto-down = on }}

Enable Clustering

Page 41: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Typesafe Console

✦ Monitoring tool for Akka (and soon Play) applications

✦ Low overhead - always turned on

✦ Traces everything in your Akka application

✦ UI or REST service to retrieve statistics

✦ Free to use for developers via Activator

Page 42: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Typesafe Console

Page 43: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

Dataflow

Akka has much much moreFSM

Transactors

Pub/Sub

ZeroMQ

Microkernel

IO

TestKit

Agents

SLF4J

Durable Mailboxes

EventBus

Camel

Pooling

TypedActor

Extensions

Typed Channels

Page 44: Up, up and out - Scaling Software with Akka HENRIK ENGSTRÖM SOFTWARE ENGINEER - TYPESAFE @h3nk3

EOF@h3nk3