Slide 1

Slide 1 text

Introducing Jonas Bonér CTO Typesafe

Slide 2

Slide 2 text

Introducing

Slide 3

Slide 3 text

Introducing Akka (Áhkká) The name comes from the goddess in the Sami (native swedes) mythology that represented all the wisdom and beauty in the world. It is also the name of a beautiful mountain in Laponia in the north part of Sweden

Slide 4

Slide 4 text

Introducing

Slide 5

Slide 5 text

Vision Simpler Concurrency Scalability Fault-tolerance

Slide 6

Slide 6 text

Vision ...with a single unified Programming Model Managed Runtime Open Source Distribution

Slide 7

Slide 7 text

Manage System Overload

Slide 8

Slide 8 text

Scale UP & Scale OUT

Slide 9

Slide 9 text

Program at a Higher Level

Slide 10

Slide 10 text

Program at a Higher Level

Slide 11

Slide 11 text

Program at a Higher Level • Never think in terms of shared state, state visibility, threads, locks, concurrent collections, thread notifications etc.

Slide 12

Slide 12 text

Program at a Higher Level • Never think in terms of shared state, state visibility, threads, locks, concurrent collections, thread notifications etc. • Low level concurrency plumbing BECOMES SIMPLE WORKFLOW - you only think about how messages flow in the system

Slide 13

Slide 13 text

Program at a Higher Level • Never think in terms of shared state, state visibility, threads, locks, concurrent collections, thread notifications etc. • Low level concurrency plumbing BECOMES SIMPLE WORKFLOW - you only think about how messages flow in the system • You get high CPU utilization, low latency, high throughput and scalability - FOR FREE as part of the model

Slide 14

Slide 14 text

Program at a Higher Level • Never think in terms of shared state, state visibility, threads, locks, concurrent collections, thread notifications etc. • Low level concurrency plumbing BECOMES SIMPLE WORKFLOW - you only think about how messages flow in the system • You get high CPU utilization, low latency, high throughput and scalability - FOR FREE as part of the model • Proven and superior model for detecting and recovering from errors

Slide 15

Slide 15 text

Distributable by Design

Slide 16

Slide 16 text

Distributable by Design

Slide 17

Slide 17 text

Distributable by Design • Actors are location transparent & distributable by design

Slide 18

Slide 18 text

Distributable by Design • Actors are location transparent & distributable by design • Scale UP and OUT for free as part of the model

Slide 19

Slide 19 text

Distributable by Design • Actors are location transparent & distributable by design • Scale UP and OUT for free as part of the model • You get the PERFECT FABRIC for the CLOUD

Slide 20

Slide 20 text

Distributable by Design • Actors are location transparent & distributable by design • Scale UP and OUT for free as part of the model • You get the PERFECT FABRIC for the CLOUD - elastic & dynamic

Slide 21

Slide 21 text

Distributable by Design • Actors are location transparent & distributable by design • Scale UP and OUT for free as part of the model • You get the PERFECT FABRIC for the CLOUD - elastic & dynamic - fault-tolerant & self-healing

Slide 22

Slide 22 text

Distributable by Design • Actors are location transparent & distributable by design • Scale UP and OUT for free as part of the model • You get the PERFECT FABRIC for the CLOUD - elastic & dynamic - fault-tolerant & self-healing - adaptive load-balancing, cluster rebalancing & actor migration

Slide 23

Slide 23 text

Distributable by Design • Actors are location transparent & distributable by design • Scale UP and OUT for free as part of the model • You get the PERFECT FABRIC for the CLOUD - elastic & dynamic - fault-tolerant & self-healing - adaptive load-balancing, cluster rebalancing & actor migration - build extremely loosely coupled and dynamic systems that can change and adapt at runtime

Slide 24

Slide 24 text

Selection of Akka Production Users

Slide 25

Slide 25 text

No content

Slide 26

Slide 26 text

How can we achieve this?

Slide 27

Slide 27 text

How can we achieve this?

Slide 28

Slide 28 text

How can we achieve this? Let’s use Actors

Slide 29

Slide 29 text

What is an Actor?

Slide 30

Slide 30 text

What is an Actor?

Slide 31

Slide 31 text

What is an Actor? • Akka's unit of code organization is called an Actor

Slide 32

Slide 32 text

What is an Actor? • Akka's unit of code organization is called an Actor • Actors helps you create concurrent, scalable and fault-tolerant applications

Slide 33

Slide 33 text

What is an Actor? • Akka's unit of code organization is called an Actor • Actors helps you create concurrent, scalable and fault-tolerant applications • Like Java EE servlets and session beans, Actors is a model for organizing your code that keeps many “policy decisions” separate from the business logic

Slide 34

Slide 34 text

What is an Actor? • Akka's unit of code organization is called an Actor • Actors helps you create concurrent, scalable and fault-tolerant applications • Like Java EE servlets and session beans, Actors is a model for organizing your code that keeps many “policy decisions” separate from the business logic • Actors may be new to many in the Java community, but they are a tried-and-true concept (Hewitt 1973) used for many years in telecom systems with 9 nines uptime

Slide 35

Slide 35 text

No content

Slide 36

Slide 36 text

Actors can be seen as Virtual Machines (nodes) in Cloud Computing

Slide 37

Slide 37 text

No content

Slide 38

Slide 38 text

Encapsulated and decoupled black boxes...

Slide 39

Slide 39 text

Encapsulated and decoupled black boxes...

Slide 40

Slide 40 text

No content

Slide 41

Slide 41 text

...that manages their own memory and behavior

Slide 42

Slide 42 text

...that manages their own memory and behavior

Slide 43

Slide 43 text

No content

Slide 44

Slide 44 text

Communicates with asynchronous non-blocking messages

Slide 45

Slide 45 text

Communicates with asynchronous non-blocking messages

Slide 46

Slide 46 text

No content

Slide 47

Slide 47 text

Elastic - grow and shrink on demand

Slide 48

Slide 48 text

Elastic - grow and shrink on demand

Slide 49

Slide 49 text

Elastic - grow and shrink on demand

Slide 50

Slide 50 text

Elastic - grow and shrink on demand

Slide 51

Slide 51 text

No content

Slide 52

Slide 52 text

Hot deploy - change behavior at runtime

Slide 53

Slide 53 text

Hot deploy - change behavior at runtime

Slide 54

Slide 54 text

Now - if we replace with Actor EVERYTHING will STILL HOLD for both LOCAL and DISTRIBUTED Actors

Slide 55

Slide 55 text

What can I use Actors for?

Slide 56

Slide 56 text

What can I use Actors for? In different scenarios, an Actor may be an alternative to:

Slide 57

Slide 57 text

What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread

Slide 58

Slide 58 text

What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or component

Slide 59

Slide 59 text

What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or component - a callback or listener

Slide 60

Slide 60 text

What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or component - a callback or listener - a singleton or service

Slide 61

Slide 61 text

What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or component - a callback or listener - a singleton or service - a router, load-balancer or pool

Slide 62

Slide 62 text

What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or component - a callback or listener - a singleton or service - a router, load-balancer or pool - a Java EE Session Bean or Message-Driven Bean

Slide 63

Slide 63 text

What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or component - a callback or listener - a singleton or service - a router, load-balancer or pool - a Java EE Session Bean or Message-Driven Bean - an out-of-process service

Slide 64

Slide 64 text

What can I use Actors for? In different scenarios, an Actor may be an alternative to: - a thread - an object instance or component - a callback or listener - a singleton or service - a router, load-balancer or pool - a Java EE Session Bean or Message-Driven Bean - an out-of-process service - a Finite State Machine (FSM)

Slide 65

Slide 65 text

So, what is the Actor Model?

Slide 66

Slide 66 text

Carl Hewitt’s definition http://bit.ly/hewitt-on-actors

Slide 67

Slide 67 text

Carl Hewitt’s definition http://bit.ly/hewitt-on-actors - The fundamental unit of computation that embodies:

Slide 68

Slide 68 text

Carl Hewitt’s definition http://bit.ly/hewitt-on-actors - The fundamental unit of computation that embodies: - Processing

Slide 69

Slide 69 text

Carl Hewitt’s definition http://bit.ly/hewitt-on-actors - The fundamental unit of computation that embodies: - Processing - Storage

Slide 70

Slide 70 text

Carl Hewitt’s definition http://bit.ly/hewitt-on-actors - The fundamental unit of computation that embodies: - Processing - Storage - Communication

Slide 71

Slide 71 text

Carl Hewitt’s definition http://bit.ly/hewitt-on-actors - The fundamental unit of computation that embodies: - Processing - Storage - Communication - 3 axioms - When an Actor receives a message it can:

Slide 72

Slide 72 text

Carl Hewitt’s definition http://bit.ly/hewitt-on-actors - The fundamental unit of computation that embodies: - Processing - Storage - Communication - 3 axioms - When an Actor receives a message it can: - Create new Actors

Slide 73

Slide 73 text

Carl Hewitt’s definition http://bit.ly/hewitt-on-actors - The fundamental unit of computation that embodies: - Processing - Storage - Communication - 3 axioms - When an Actor receives a message it can: - Create new Actors - Send messages to Actors it knows

Slide 74

Slide 74 text

Carl Hewitt’s definition http://bit.ly/hewitt-on-actors - The fundamental unit of computation that embodies: - Processing - Storage - Communication - 3 axioms - When an Actor receives a message it can: - Create new Actors - Send messages to Actors it knows - Designate how it should handle the next message it receives

Slide 75

Slide 75 text

4 core Actor operations 0. DEFINE 1. CREATE 2. SEND 3. BECOME 4. SUPERVISE

Slide 76

Slide 76 text

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message) throws Exception { if (message instanceof Greeting) log.info("Hello {}", ((Greeting) message).who); } } } 0. DEFINE

Slide 77

Slide 77 text

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message) throws Exception { if (message instanceof Greeting) log.info("Hello {}", ((Greeting) message).who); } } } 0. DEFINE Define the message(s) the Actor should be able to respond to

Slide 78

Slide 78 text

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message) throws Exception { if (message instanceof Greeting) log.info("Hello {}", ((Greeting) message).who); } } } 0. DEFINE Define the message(s) the Actor should be able to respond to Define the Actor class

Slide 79

Slide 79 text

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message) throws Exception { if (message instanceof Greeting) log.info("Hello {}", ((Greeting) message).who); } } } 0. DEFINE Define the message(s) the Actor should be able to respond to Define the Actor class Define the Actor’s behavior

Slide 80

Slide 80 text

case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello {}", who) } } Scala version

Slide 81

Slide 81 text

1. CREATE •CREATE - creates a new instance of an Actor •Extremely lightweight (2.7 Million per Gb RAM) •Very strong encapsulation - encapsulates: - state - behavior - message queue •State & behavior is indistinguishable from each other •Only way to observe state is by sending an actor a message and see how it reacts

Slide 82

Slide 82 text

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message) throws Exception { if (message instanceof Greeting) log.info("Hello {}", ((Greeting) message).who); } } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter"); CREATE Actor

Slide 83

Slide 83 text

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message) throws Exception { if (message instanceof Greeting) log.info("Hello {}", ((Greeting) message).who); } } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter"); CREATE Actor Create an Actor system

Slide 84

Slide 84 text

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message) throws Exception { if (message instanceof Greeting) log.info("Hello {}", ((Greeting) message).who); } } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter"); CREATE Actor Create an Actor system Actor configuration

Slide 85

Slide 85 text

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message) throws Exception { if (message instanceof Greeting) log.info("Hello {}", ((Greeting) message).who); } } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter"); CREATE Actor Create an Actor system Actor configuration Give it a name

Slide 86

Slide 86 text

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message) throws Exception { if (message instanceof Greeting) log.info("Hello {}", ((Greeting) message).who); } } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter"); CREATE Actor Create an Actor system Create the Actor Actor configuration Give it a name

Slide 87

Slide 87 text

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message) throws Exception { if (message instanceof Greeting) log.info("Hello {}", ((Greeting) message).who); } } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter"); CREATE Actor Create an Actor system Create the Actor Actor configuration Give it a name You get an ActorRef back

Slide 88

Slide 88 text

Guardian System Actor Actors can form hierarchies

Slide 89

Slide 89 text

Guardian System Actor system.actorOf(Props[Foo], “Foo”) Actors can form hierarchies

Slide 90

Slide 90 text

Foo Guardian System Actor system.actorOf(Props[Foo], “Foo”) Actors can form hierarchies

Slide 91

Slide 91 text

Foo Guardian System Actor context.actorOf(Props[A], “A”) Actors can form hierarchies

Slide 92

Slide 92 text

A Foo Guardian System Actor context.actorOf(Props[A], “A”) Actors can form hierarchies

Slide 93

Slide 93 text

A B Bar Foo C B E A D C Guardian System Actor Actors can form hierarchies

Slide 94

Slide 94 text

A B Bar Foo C B E A D C Guardian System Actor Name resolution - like a file-system

Slide 95

Slide 95 text

A B Bar Foo C B E A D C /Foo Guardian System Actor Name resolution - like a file-system

Slide 96

Slide 96 text

A B Bar Foo C B E A D C /Foo /Foo/A Guardian System Actor Name resolution - like a file-system

Slide 97

Slide 97 text

A B Bar Foo C B E A D C /Foo /Foo/A /Foo/A/B Guardian System Actor Name resolution - like a file-system

Slide 98

Slide 98 text

A B Bar Foo C B E A D C /Foo /Foo/A /Foo/A/B /Foo/A/D Guardian System Actor Name resolution - like a file-system

Slide 99

Slide 99 text

2. SEND

Slide 100

Slide 100 text

2. SEND •SEND - sends a message to an Actor

Slide 101

Slide 101 text

2. SEND •SEND - sends a message to an Actor •Asynchronous and Non-blocking - Fire-forget

Slide 102

Slide 102 text

2. SEND •SEND - sends a message to an Actor •Asynchronous and Non-blocking - Fire-forget •Everything happens Reactively

Slide 103

Slide 103 text

2. SEND •SEND - sends a message to an Actor •Asynchronous and Non-blocking - Fire-forget •Everything happens Reactively - An Actor is passive until a message is sent to it, which triggers something within the Actor

Slide 104

Slide 104 text

2. SEND •SEND - sends a message to an Actor •Asynchronous and Non-blocking - Fire-forget •Everything happens Reactively - An Actor is passive until a message is sent to it, which triggers something within the Actor - Messages is the Kinetic Energy in an Actor system

Slide 105

Slide 105 text

2. SEND •SEND - sends a message to an Actor •Asynchronous and Non-blocking - Fire-forget •Everything happens Reactively - An Actor is passive until a message is sent to it, which triggers something within the Actor - Messages is the Kinetic Energy in an Actor system - Actors can have lots of buffered Potential Energy but can't do anything with it until it is triggered by a message

Slide 106

Slide 106 text

2. SEND •SEND - sends a message to an Actor •Asynchronous and Non-blocking - Fire-forget •Everything happens Reactively - An Actor is passive until a message is sent to it, which triggers something within the Actor - Messages is the Kinetic Energy in an Actor system - Actors can have lots of buffered Potential Energy but can't do anything with it until it is triggered by a message •EVERYTHING is asynchronous and lockless

Slide 107

Slide 107 text

Throughput on a single box +50 million messages per second

Slide 108

Slide 108 text

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message) throws Exception { if (message instanceof Greeting) log.info("Hello {}", ((Greeting) message).who); } } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter"); greeter.tell(new Greeting("Charlie Parker")); SEND message

Slide 109

Slide 109 text

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message) throws Exception { if (message instanceof Greeting) log.info("Hello {}", ((Greeting) message).who); } } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter"); greeter.tell(new Greeting("Charlie Parker")); SEND message Send the message

Slide 110

Slide 110 text

public class Greeting implements Serializable { public final String who; public Greeting(String who) { this.who = who; } } public class GreetingActor extends UntypedActor { LoggingAdapter log = Logging.getLogger(getContext().system(), this); public void onReceive(Object message) throws Exception { if (message instanceof Greeting) log.info("Hello {}", ((Greeting) message).who); } } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf(new Props(GreetingActor.class), "greeter"); greeter.tell(new Greeting("Charlie Parker")); Full example

Slide 111

Slide 111 text

case class Greeting(who: String) class GreetingActor extends Actor with ActorLogging { def receive = { case Greeting(who) => log.info("Hello {}", who) } } val system = ActorSystem("MySystem") val greeter = system.actorOf(Props[GreetingActor], name = "greeter") greeter tell Greeting("Charlie Parker") Scala version

Slide 112

Slide 112 text

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

Slide 113

Slide 113 text

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

Slide 114

Slide 114 text

akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } } } Just feed the ActorSystem with this configuration Remote deployment

Slide 115

Slide 115 text

akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } } } Just feed the ActorSystem with this configuration Configure a Remote Provider Remote deployment

Slide 116

Slide 116 text

akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } } } Just feed the ActorSystem with this configuration Configure a Remote Provider For the Greeter actor Remote deployment

Slide 117

Slide 117 text

akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } } } Just feed the ActorSystem with this configuration Configure a Remote Provider Define Remote Path For the Greeter actor Remote deployment

Slide 118

Slide 118 text

akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } } } Just feed the ActorSystem with this configuration Configure a Remote Provider Define Remote Path Protocol For the Greeter actor akka:// Remote deployment

Slide 119

Slide 119 text

akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } } } Just feed the ActorSystem with this configuration Configure a Remote Provider Define Remote Path Protocol Actor System For the Greeter actor akka://MySystem Remote deployment

Slide 120

Slide 120 text

akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } } } Just feed the ActorSystem with this configuration Configure a Remote Provider Define Remote Path Protocol Actor System Hostname For the Greeter actor akka://MySystem@machine1 Remote deployment

Slide 121

Slide 121 text

akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } } } Just feed the ActorSystem with this configuration Configure a Remote Provider Define Remote Path Protocol Actor System Hostname Port For the Greeter actor akka://MySystem@machine1:2552 Remote deployment

Slide 122

Slide 122 text

akka { actor { provider = akka.remote.RemoteActorRefProvider deployment { /Greeter { remote = } } } } Just feed the ActorSystem with this configuration Zero code changes Configure a Remote Provider Define Remote Path Protocol Actor System Hostname Port For the Greeter actor akka://MySystem@machine1:2552 Remote deployment

Slide 123

Slide 123 text

3. BECOME

Slide 124

Slide 124 text

3. BECOME •BECOME - dynamically redefines Actor’s behavior

Slide 125

Slide 125 text

3. BECOME •BECOME - dynamically redefines Actor’s behavior •Triggered reactively by receive of message

Slide 126

Slide 126 text

3. BECOME •BECOME - dynamically redefines Actor’s behavior •Triggered reactively by receive of message •In a type system analogy it is as if the object changed type - changed interface, protocol & implementation

Slide 127

Slide 127 text

3. BECOME •BECOME - dynamically redefines Actor’s behavior •Triggered reactively by receive of message •In a type system analogy it is as if the object changed type - changed interface, protocol & implementation •Will now react differently to the messages it receives

Slide 128

Slide 128 text

3. BECOME •BECOME - dynamically redefines Actor’s behavior •Triggered reactively by receive of message •In a type system analogy it is as if the object changed type - changed interface, protocol & implementation •Will now react differently to the messages it receives •Behaviors are stacked & can be pushed and popped

Slide 129

Slide 129 text

Why would I want to do that?

Slide 130

Slide 130 text

Why would I want to do that? •Let a highly contended Actor adaptively transform himself into an Actor Pool or a Router

Slide 131

Slide 131 text

Why would I want to do that? •Let a highly contended Actor adaptively transform himself into an Actor Pool or a Router •Implement an FSM (Finite State Machine)

Slide 132

Slide 132 text

Why would I want to do that? •Let a highly contended Actor adaptively transform himself into an Actor Pool or a Router •Implement an FSM (Finite State Machine) •Implement graceful degradation

Slide 133

Slide 133 text

Why would I want to do that? •Let a highly contended Actor adaptively transform himself into an Actor Pool or a Router •Implement an FSM (Finite State Machine) •Implement graceful degradation •Spawn up (empty) generic Worker processes that can become whatever the Master currently needs

Slide 134

Slide 134 text

Why would I want to do that? •Let a highly contended Actor adaptively transform himself into an Actor Pool or a Router •Implement an FSM (Finite State Machine) •Implement graceful degradation •Spawn up (empty) generic Worker processes that can become whatever the Master currently needs •etc. use your imagination

Slide 135

Slide 135 text

Why would I want to do that? •Let a highly contended Actor adaptively transform himself into an Actor Pool or a Router •Implement an FSM (Finite State Machine) •Implement graceful degradation •Spawn up (empty) generic Worker processes that can become whatever the Master currently needs •etc. use your imagination •Very useful once you get the used to it

Slide 136

Slide 136 text

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

Slide 137

Slide 137 text

context.become(new Procedure[Object]() { void apply(Object msg) { // new body if (msg instanceof NewMessage) { NewMessage newMsg = (NewMessage)msg; ... } } }); Actor context available from within an Actor become

Slide 138

Slide 138 text

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

Slide 139

Slide 139 text

Load Balancing

Slide 140

Slide 140 text

Routers val router = system.actorOf( Props[SomeActor].withRouter( RoundRobinRouter(nrOfInstances = 5))) Scala API

Slide 141

Slide 141 text

Router + Resizer val resizer = DefaultResizer(lowerBound = 2, upperBound = 15) val router = system.actorOf( Props[ExampleActor1].withRouter( RoundRobinRouter(resizer = Some(resizer)))) Scala API

Slide 142

Slide 142 text

Failure management in Java/C/C# etc.

Slide 143

Slide 143 text

• You are given a SINGLE thread of control Failure management in Java/C/C# etc.

Slide 144

Slide 144 text

• You are given a SINGLE thread of control • If this thread blows up you are screwed Failure management in Java/C/C# etc.

Slide 145

Slide 145 text

• You are given a SINGLE thread of control • If this thread blows up you are screwed • So you need to do all explicit error handling WITHIN this single thread Failure management in Java/C/C# etc.

Slide 146

Slide 146 text

• You are given a SINGLE thread of control • If this thread blows up you are screwed • So you need to do all explicit error handling WITHIN this single thread • To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed Failure management in Java/C/C# etc.

Slide 147

Slide 147 text

• You are given a SINGLE thread of control • If this thread blows up you are screwed • So you need to do all explicit error handling WITHIN this single thread • To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed • This leads to DEFENSIVE programming with: Failure management in Java/C/C# etc.

Slide 148

Slide 148 text

• You are given a SINGLE thread of control • If this thread blows up you are screwed • So you need to do all explicit error handling WITHIN this single thread • To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed • This leads to DEFENSIVE programming with: • Error handling TANGLED with business logic Failure management in Java/C/C# etc.

Slide 149

Slide 149 text

• You are given a SINGLE thread of control • If this thread blows up you are screwed • So you need to do all explicit error handling WITHIN this single thread • To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed • This leads to DEFENSIVE programming with: • Error handling TANGLED with business logic • SCATTERED all over the code base Failure management in Java/C/C# etc.

Slide 150

Slide 150 text

• You are given a SINGLE thread of control • If this thread blows up you are screwed • So you need to do all explicit error handling WITHIN this single thread • To make things worse - errors do not propagate between threads so there is NO WAY OF EVEN FINDING OUT that something have failed • This leads to DEFENSIVE programming with: • Error handling TANGLED with business logic • SCATTERED all over the code base We can do better than this!!! Failure management in Java/C/C# etc.

Slide 151

Slide 151 text

4. SUPERVISE

Slide 152

Slide 152 text

4. SUPERVISE •SUPERVISE - manage another Actor’s failures

Slide 153

Slide 153 text

4. SUPERVISE •SUPERVISE - manage another Actor’s failures •Error handling in actors is handle by letting Actors monitor (supervise) each other for failure

Slide 154

Slide 154 text

4. SUPERVISE •SUPERVISE - manage another Actor’s failures •Error handling in actors is handle by letting Actors monitor (supervise) each other for failure •This means that if an Actor crashes, a notification will be sent to his supervisor, who can react upon the failure

Slide 155

Slide 155 text

4. SUPERVISE •SUPERVISE - manage another Actor’s failures •Error handling in actors is handle by letting Actors monitor (supervise) each other for failure •This means that if an Actor crashes, a notification will be sent to his supervisor, who can react upon the failure •This provides clean separation of processing and error handling

Slide 156

Slide 156 text

...let’s take a standard OO application

Slide 157

Slide 157 text

No content

Slide 158

Slide 158 text

Which components have critically important state and explicit error handling?

Slide 159

Slide 159 text

No content

Slide 160

Slide 160 text

Fault-tolerant onion-layered Error Kernel

Slide 161

Slide 161 text

Error Kernel

Slide 162

Slide 162 text

Error Kernel

Slide 163

Slide 163 text

Error Kernel

Slide 164

Slide 164 text

Error Kernel

Slide 165

Slide 165 text

Error Kernel

Slide 166

Slide 166 text

Error Kernel

Slide 167

Slide 167 text

Error Kernel Node 1 Node 2

Slide 168

Slide 168 text

SUPERVISE Actor Every single actor has a default supervisor strategy. Which is usually sufficient. But it can be overridden.

Slide 169

Slide 169 text

class Supervisor extends UntypedActor { private SupervisorStrategy strategy = new OneForOneStrategy( 10, Duration.parse("1 minute"), new Function() { @Override public Directive apply(Throwable t) { if (t instanceof ArithmeticException) return resume(); else if (t instanceof NullPointerException) return restart(); else return escalate(); } }); @Override public SupervisorStrategy supervisorStrategy() { SUPERVISE Actor Every single actor has a default supervisor strategy. Which is usually sufficient. But it can be overridden.

Slide 170

Slide 170 text

class Supervisor extends UntypedActor { private SupervisorStrategy strategy = new OneForOneStrategy( 10, Duration.parse("1 minute"), new Function() { @Override public Directive apply(Throwable t) { if (t instanceof ArithmeticException) return resume(); else if (t instanceof NullPointerException) return restart(); else return escalate(); } }); @Override public SupervisorStrategy supervisorStrategy() { return strategy; } ActorRef worker = context.actorOf(new Props(Worker.class)); public void onReceive(Object message) throws Exception { if (message instanceof Integer) worker.forward(message); } } SUPERVISE Actor

Slide 171

Slide 171 text

class Supervisor extends Actor { override val supervisorStrategy = (maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: Exception => Escalate } val worker = context.actorOf(Props[Worker]) def receive = { case n: Int => worker forward n } } Scala version

Slide 172

Slide 172 text

class Supervisor extends Actor { override val supervisorStrategy = (maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: Exception => Escalate } val worker = context.actorOf(Props[Worker]) def receive = { case n: Int => worker forward n } } Scala version OneForOneStrategy

Slide 173

Slide 173 text

class Supervisor extends Actor { override val supervisorStrategy = (maxNrOfRetries = 10, withinTimeRange = 1 minute) { case _: ArithmeticException => Resume case _: NullPointerException => Restart case _: Exception => Escalate } val worker = context.actorOf(Props[Worker]) def receive = { case n: Int => worker forward n } } Scala version AllForOneStrategy

Slide 174

Slide 174 text

class Worker 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

Slide 175

Slide 175 text

This was Akka 2.x

Slide 176

Slide 176 text

This was Akka 2.x Well...it’s a start...

Slide 177

Slide 177 text

...we have much much more

Slide 178

Slide 178 text

AMQP Dataflow ...we have much much more Cluster FSM Transactors Spring Pub/Sub ZeroMQ Microkernel IO TestKit Agents SLF4J Durable Mailboxes EventBus Camel TypedActor Extensions HTTP/REST

Slide 179

Slide 179 text

get it and learn more http://akka.io http://typesafe.com http://letitcrash.com

Slide 180

Slide 180 text

EOF