Akka JUGL 2012

109
Above the Clouds: Introducing Akka Vojin Jovanovic, EPFL Philipp Haller, Typesafe Based on material provided by the Akka team at Typesafe

description

Indroduction to Akka (http://www.akka.io) by Vojin Jovanovic at the Lausanne Java User Group (http://jugl.myxwiki.org/xwiki/bin/view/Main/WebHome).

Transcript of Akka JUGL 2012

Page 1: Akka JUGL 2012

Above the Clouds:Introducing Akka

Vojin Jovanovic, EPFLPhilipp Haller, Typesafe

Based on material provided by the Akka team at Typesafe

Page 2: Akka JUGL 2012

The problem

It is way too hard to build:1. correct highly concurrent systems

2. truly scalable systems

3. fault-tolerant systems that self-heal

...using “state-of-the-art” tools

Page 3: Akka JUGL 2012

Vision

Simpler

Concurrency

Scalability

Fault-tolerance

Page 4: Akka JUGL 2012

Vision

...with a single unified

Programming Model

Managed Runtime

Open Source Distribution

Page 5: Akka JUGL 2012

CORE SERVICES

ARCHITECTURE

Page 6: Akka JUGL 2012

ARCHITECTUREPlay-miniPlay! Microkernel

TestKit Camelintegration

AMQPintegration

ADD-ON MODULES

Page 7: Akka JUGL 2012

ARCHITECTUREPlay-miniPlay! Microkernel

TestKit Camelintegration

AMQPintegration

TYPESAFE STACK

ADD-ONS

Page 8: Akka JUGL 2012

FINANCE

• Stock trend Analysis & Simulation

• Event-driven messaging systems

BETTING & GAMING

• Massive multiplayer online gaming

• High throughput and transactional betting

TELECOM

• Streaming media network gateways

SIMULATION

• 3D simulation engines

E-COMMERCE

• Social media community sites

SOME EXAMPLES:

WHERE IS AKKA USED?

Page 9: Akka JUGL 2012

Akka Actorsone tool in the toolbox

Page 10: Akka JUGL 2012

ACTOR CONCURRENCY

• Actors = lightweight isolated processes• No shared state• Asynchronous message passing (non-blocking)• Sequential message processing (one message at a

time)• Actor mailbox: queue of not-yet-processed messages

Page 11: Akka JUGL 2012

Behavior

State

Actor

Page 12: Akka JUGL 2012

Event-driven

Behavior

State

Actor

Page 13: Akka JUGL 2012

Event-driven

Behavior

State

Actor

Page 14: Akka JUGL 2012

Event-driven

Behavior

State

Actor

Page 15: Akka JUGL 2012

Event-driven

Behavior

State

Actor

Page 16: Akka JUGL 2012

Event-driven

Behavior

State

Actor

Page 17: Akka JUGL 2012

Behavior

State

Actor

Page 18: Akka JUGL 2012

Event-driven

Behavior

State

Actor

Page 19: Akka JUGL 2012

case object Tick

class Counter extends Actor { var counter = 0

def receive = { case Tick => counter += 1 println(counter) }}

Actors

Scala API

Page 20: Akka JUGL 2012

class Counter extends UntypedActor { int counter = 0;

void onReceive(Object msg) { if (msg.equals(“Tick”)) { counter += 1; System.out.println(counter); } }}

Actors

Java API

Page 21: Akka JUGL 2012

ACTOR SYSTEMS

• Actors are created in the context of an actor system• Actor system = unit for managing shared facilities:

scheduling services, configuration, logging etc.• Several actor systems with different configurations

may co-exist within the same JVM instance (there is no global state within Akka itself)

• There may be millions of actors within a single actor system

Page 22: Akka JUGL 2012

val conf = ConfigFactory.load(“application”)

val system = ActorSystem(“my-app”, conf)

Scala API

Create Application

Page 23: Akka JUGL 2012

Config conf = ConfigFactory.load(“application”);

ActorSystem system = ActorSystem.create(“my-app”, conf);

Create Application

Java API

Page 24: Akka JUGL 2012

val counter = system.actorOf(Props[Counter])

Create Actors

counter is an ActorRefCreates a top-level actor

Scala API

Page 25: Akka JUGL 2012

ActorRef counter = system.actorOf(new Props(Counter.class));

Create Actors

Java API

Creates a top-level actor

Page 26: Akka JUGL 2012

system.stop(counter)

Stop actors

...also stops all actors in the hierarchy below

Page 27: Akka JUGL 2012

counter ! Tick

Send: !

Scala API

fire-forget

Page 28: Akka JUGL 2012

counter tell Tick

...or use tell

fire-forget

Scala API

Page 29: Akka JUGL 2012

counter.tell(tick);

...or use tell

Java API

fire-forget

Page 30: Akka JUGL 2012

import akka.patterns.ask

// returns a futureval future = actor ? message

future onSuccess { case x => println(x)}

Send: ?

returns a Future

Scala API

Page 31: Akka JUGL 2012

class SomeActor extends Actor { def receive = { case User(name) => // reply to sender sender ! (“Hi ” + name) }}

Reply

Scala API

Page 32: Akka JUGL 2012

class SomeActor extends UntypedActor { void onReceive(Object msg) { if (msg instanceof User) { User user = (User) msg; // reply to sender getSender().tell(“Hi ” + user.name); } }}

Reply

Java API

Page 33: Akka JUGL 2012

import akka.patterns.ask

// returns a futureval future = actor ask message

future onSuccess { case x => println(x)}

...or use ask

Scala API

Page 34: Akka JUGL 2012

import static akka.patterns.Patterns.ask;

Future<Object> future = ask(actor, message, timeout);

future.onSuccess(new OnSuccess<Object>() { public void onSuccess(String result) { System.out.println(result); }});

...or use ask

Java API

Page 35: Akka JUGL 2012

Futureval f = Promise[String]()

f onComplete { ... } onSuccess { ... } onFailure { ... }f foreach { ... }f map { ... }f flatMap { ... }f filter { ... }f zip otherFf fallbackTo otherF

Await.result(f, 5 seconds)

Scala API

Page 36: Akka JUGL 2012

firstCompletedOf fold

reduce find

traversesequence

Future

Combinators for collections of Futures

Page 37: Akka JUGL 2012

Promise

promise1.successful(...) promise2.failure(...)promise3.completeWith(future)

Promise is the write side of the FutureFuture is the read side

Page 38: Akka JUGL 2012

become

context become { // new body case NewMessage => ... }

Scala API

Page 39: Akka JUGL 2012

becomecontext.become(new Procedure[Object]() { void apply(Object msg) { // new body if (msg instanceof NewMessage) { NewMessage newMsg = (NewMessage)msg; ... } }});

Java API

Page 40: Akka JUGL 2012

unbecome

context.unbecome()

Page 41: Akka JUGL 2012

Scale up?

Page 42: Akka JUGL 2012

ThreadPoolExecutor

Page 43: Akka JUGL 2012

ThreadPoolExecutor

Page 44: Akka JUGL 2012

new ForkJoinPool

Page 45: Akka JUGL 2012

new ForkJoinPool

Page 46: Akka JUGL 2012

New Remote Actors

Page 47: Akka JUGL 2012

val actor = system.actorOf(Props[MyActor], “my-service”)

Name

Bind the actor to a name

Scala API

Page 48: Akka JUGL 2012

ActorRef actor = system.actorOf( new Props(MyActor.class), “my-service”);

Name

Bind the actor to a name

Java API

Page 49: Akka JUGL 2012

Actor name is virtual and decoupled from how it is deployed

Deployment

Page 50: Akka JUGL 2012

Deployment

If no deployment configuration exists then actor is deployed as local

Page 51: Akka JUGL 2012

Deployment

The same system can be configured as distributed without code change

(even change at runtime)

Page 52: Akka JUGL 2012

Deployment

Write as local but deploy as distributed in the cloud without code change

Page 53: Akka JUGL 2012

Deployment

Allows runtime to dynamically and adaptively change topology

Page 54: Akka JUGL 2012

akka { actor { deployment { /my-service { router = "round-robin" nr-of-instances = 3 target {

nodes = ["wallace:2552", "gromit:2552"] } } } }}

Deployment configuration

Page 55: Akka JUGL 2012

Let it crash fault-tolerance

Page 56: Akka JUGL 2012

The

Erlangmodel

Page 57: Akka JUGL 2012

ErrorKernel

Page 58: Akka JUGL 2012

Node 1 Node 2

Page 59: Akka JUGL 2012

// from within an actorval child = context.actorOf(Props[MyActor], “my-actor”)

Parental automatic supervision

Scala API

transparent and automatic fault handling by design

Page 60: Akka JUGL 2012

// from within an actorActorRef child = getContext().actorOf( new Props(MyActor.class), “my-actor”);

transparent and automatic fault handling by design

Parental automatic supervision

Java API

Page 61: Akka JUGL 2012

Guardian System Actor

Parental automatic supervision

Page 62: Akka JUGL 2012

Guardian System Actor

Parental automatic supervision

system.actorOf(Props[Foo], “Foo”)

Page 63: Akka JUGL 2012

Foo

Guardian System Actor

Parental automatic supervision

system.actorOf(Props[Foo], “Foo”)

Page 64: Akka JUGL 2012

Foo

Guardian System Actor

Parental automatic supervision

context.actorOf(Props[A], “A”)

Page 65: Akka JUGL 2012

A

Foo

Guardian System Actor

Parental automatic supervision

context.actorOf(Props[A], “A”)

Page 66: Akka JUGL 2012

A

B

BarFoo

C

BE

A

D

C

Guardian System Actor

Parental automatic supervision

Page 67: Akka JUGL 2012

A

B

BarFoo

C

BE

A

D

C

Name resolutionGuardian System Actor

Page 68: Akka JUGL 2012

A

B

BarFoo

C

BE

A

D

C

/Foo

Name resolutionGuardian System Actor

Page 69: Akka JUGL 2012

A

B

BarFoo

C

BE

A

D

C

/Foo

/Foo/A

Name resolutionGuardian System Actor

Page 70: Akka JUGL 2012

A

B

BarFoo

C

BE

A

D

C

/Foo

/Foo/A

/Foo/A/B

Name resolutionGuardian System Actor

Page 71: Akka JUGL 2012

A

B

BarFoo

C

BE

A

D

C

/Foo

/Foo/A

/Foo/A/B

Name resolution

/Foo/A/D

Guardian System Actor

Page 72: Akka JUGL 2012

Supervisionclass MySupervisor extends Actor { def supervisorStrategy = OneForOneStrategy({ case _: ActorKilledException => Stop case _: ArithmeticException => Resume case _: Exception => Restart case _ => Escalate

   },   maxNrOfRetries = None,   withinTimeRange = None)

def receive = { case NewUser(name) => ... = context.actorOf[User](name) }} Scala API

Page 73: Akka JUGL 2012

Supervisionclass MySupervisor extends Actor { def supervisorStrategy = OneForOneStrategy({ case _: ActorKilledException => Stop case _: ArithmeticException => Resume case _: Exception => Restart case _ => Escalate

   },   maxNrOfRetries = None,   withinTimeRange = None)

def receive = { case NewUser(name) => ... = context.actorOf[User](name) }}

AllForOneStrategy

Scala API

Page 74: Akka JUGL 2012

class FaultTolerantService extends Actor { ... override def preRestart( reason: Throwable, message: Option[Any]) = { ... // clean up before restart } override def postRestart(reason: Throwable) = { ... // init after restart }}

Manage failure

Scala API

Page 75: Akka JUGL 2012

val buddy: ActorRef = ...

val watcher = system.actorOf(Props( new Actor { context.watch(buddy) def receive = { case t: Terminated => ... } }))

watch/unwatch

Scala API

Page 76: Akka JUGL 2012

AMQPDataflow

...and much much more

HTTP/REST

FSM

TransactorsSpring

Pub/Sub

ZeroMQ

MicrokernelIO

TestKit

Agents

SLF4J

Durable Mailboxes

EventBus

Camel

Page 77: Akka JUGL 2012

get it and learn morehttp://akka.io

http://typesafe.com

Page 78: Akka JUGL 2012

Typesafe Console

Page 79: Akka JUGL 2012

Typesafe Console

Page 80: Akka JUGL 2012
Page 81: Akka JUGL 2012

Akka 2.1+

Page 82: Akka JUGL 2012

Decentralized P2P gossip-based cluster membership (dynamo-style w/ vector clocks, hand-off on fail-

over etc.)

The runtime provides

Page 83: Akka JUGL 2012

Automatic adaptive cluster rebalancing

The runtime provides

Page 84: Akka JUGL 2012

Automatic cluster-wide deployment

The runtime provides

Page 85: Akka JUGL 2012

Highly available configuration service

The runtime provides

Page 86: Akka JUGL 2012

Automatic replication with automatic fail-over upon node crash

The runtime provides

Page 87: Akka JUGL 2012

Transparent and user-configurable load-balancing

The runtime provides

Page 88: Akka JUGL 2012

Akka Node

Page 89: Akka JUGL 2012

Akka Nodeval ping = actorOf(Props[Ping], “ping”)val pong = actorOf(Props[Pong], “pong”)

ping ! Ball(pong)

Page 90: Akka JUGL 2012

Akka Nodeval ping = actorOf(Props[Ping], “ping”)val pong = actorOf(Props[Pong], “pong”)

ping ! Ball(pong)

Ping Pong

Page 91: Akka JUGL 2012

Akka Node

AkkaCluster Node

Ping Pong

Page 92: Akka JUGL 2012

Akka Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

Ping Pong

Page 93: Akka JUGL 2012

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

Ping Pong

Page 94: Akka JUGL 2012

akka { actor { deployment { /pong { router = "round-robin" nr-of-instances = 3 } } }}

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

Ping Pong

Page 95: Akka JUGL 2012

akka { actor { deployment { /pong { router = "round-robin" nr-of-instances = 3 } } }}

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster NodePing

Pong

Pong

Pong

Page 96: Akka JUGL 2012

akka { actor { deployment { /pong { router = "round-robin" nr-of-instances = 3 } } }}

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster Node

AkkaCluster NodePing

Pong

Pong

Pong

Fail-over

Redirect references

Page 97: Akka JUGL 2012
Page 98: Akka JUGL 2012
Page 99: Akka JUGL 2012

Develop

Page 100: Akka JUGL 2012

Scala IDE MigrationManager

sbt

Page 101: Akka JUGL 2012

Scala IDE MigrationManager

Run

sbt

Page 102: Akka JUGL 2012

Scala IDE MigrationManager

Scala Play

sbt

Akka

Page 103: Akka JUGL 2012

Manage

Scala IDE MigrationManager

Scala Play

sbt

Akka

Page 104: Akka JUGL 2012

Scala IDE MigrationManager

Scala Play

ProvisioningTypesafe

SubscriptionTypesafeConsole

sbt

Akka

Page 105: Akka JUGL 2012

Scala IDE MigrationManager

Scala Play

ProvisioningTypesafe

SubscriptionTypesafeConsole

sbt

Akka

Page 106: Akka JUGL 2012

http://console-demo.typesafe.com

live demo

Page 107: Akka JUGL 2012

sample videoof the

Typesafe Console

Page 108: Akka JUGL 2012

sample videoof the

Typesafe Console

Page 109: Akka JUGL 2012

EOF