up up and out scaling software with akka
play

UP UP AND OUT: SCALING SOFTWARE WITH AKKA Jonas Bonr CTO Typesafe - PowerPoint PPT Presentation

UP UP AND OUT: SCALING SOFTWARE WITH AKKA Jonas Bonr CTO Typesafe @jboner Scaling software with Jonas Bonr CTO Typesafe @jboner Scaling Scaling software with software with Scaling Scaling software with software with Akka


  1. 0. DEFINE Define the message(s) the Actor should be able to respond to public class Greeting implements Serializable { public final String who; Define the Actor class 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); } } }

  2. 0. DEFINE Define the message(s) the Actor should be able to respond to public class Greeting implements Serializable { public final String who; Define the Actor class 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); } } Define the Actor’s behavior }

  3. 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

  4. CREATE Actor 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");

  5. CREATE Actor 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); } Create an Actor system } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf( new Props(GreetingActor.class), "greeter");

  6. CREATE Actor 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); } Create an Actor system Actor configuration } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf( new Props(GreetingActor.class), "greeter");

  7. CREATE Actor 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); } Create an Actor system Actor configuration } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf( new Props(GreetingActor.class), "greeter"); Give it a name

  8. CREATE Actor 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); } Create an Actor system Actor configuration } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf( new Props(GreetingActor.class), "greeter"); Create the Actor Give it a name

  9. CREATE Actor 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); } Create an Actor system Actor configuration } } ActorSystem system = ActorSystem.create("MySystem"); ActorRef greeter = system.actorOf( new Props(GreetingActor.class), "greeter"); Create the Actor You get an ActorRef back Give it a name

  10. Actors can form hierarchies Guardian System Actor

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

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

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

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

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

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

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

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

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

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

  21. 2. SEND

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

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

  24. 2. SEND • SEND - sends a message to an Actor • Asynchronous and Non-blocking - Fire-forget • EVERYTHING is asynchronous and lockless

  25. 2. SEND • SEND - sends a message to an Actor • Asynchronous and Non-blocking - Fire-forget • EVERYTHING is asynchronous and lockless • Everything happens REACTIVELY

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

  27. 2. SEND • SEND - sends a message to an Actor • Asynchronous and Non-blocking - Fire-forget • EVERYTHING is asynchronous and lockless • 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

  28. 2. SEND • SEND - sends a message to an Actor • Asynchronous and Non-blocking - Fire-forget • EVERYTHING is asynchronous and lockless • 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

  29. SEND message 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"));

  30. SEND message 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 the message

  31. Full example 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"));

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

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

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

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

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

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

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

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

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

  41. 3. BECOME

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

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

  44. 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

  45. 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

Recommend


More recommend