• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM
 

Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM

on

  • 8,438 views

 

Statistics

Views

Total Views
8,438
Views on SlideShare
7,141
Embed Views
1,297

Actions

Likes
18
Downloads
173
Comments
4

33 Embeds 1,297

http://eao197.blogspot.com 600
http://bibagimon.blogspot.jp 251
http://eao197.blogspot.ru 169
http://www.scoop.it 141
http://bibagimon.blogspot.com 28
http://bibagimon.blogspot.se 22
http://bibagimon.blogspot.ru 18
http://feeds2.feedburner.com 7
http://bibagimon.blogspot.it 7
http://bibagimon.blogspot.in 7
http://www.linkedin.com 5
http://bibagimon.blogspot.fr 5
http://bibagimon.blogspot.co.uk 4
http://www.google.ru 4
http://bibagimon.blogspot.ca 3
http://bibagimon.blogspot.com.es 3
http://bibagimon.blogspot.com.br 3
http://eao197.blogspot.nl 2
http://twitter.com 2
http://bibagimon.blogspot.de 2
http://eao197.blogspot.de 2
http://eao197.blogspot.ca 1
http://bibagimon.blogspot.com.ar 1
http://bibagimon.blogspot.sg 1
http://bibagimon.blogspot.ch 1
http://eao197.blogspot.ie 1
http://bibagimon.blogspot.tw 1
http://eao197.blogspot.se 1
http://eao197.blogspot.co.uk 1
http://bitly.com 1
http://feeds.feedburner.com 1
http://127.0.0.1:8795 1
https://www.linkedin.com 1
More...

Accessibility

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel

14 of 4 previous next Post a comment

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
  • Helpful to me, thanks
    Are you sure you want to
    Your message goes here
    Processing…
  • Very funny and informative presentation, it shows basics concepts of Akka in Java, but they can be applied to scala programming too.
    Are you sure you want to
    Your message goes here
    Processing…
  • I have a video, but it's in Italian. Sorry.
    Are you sure you want to
    Your message goes here
    Processing…
  • Great presentation, Do you have a video of you presenting this?
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment

    Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM Concurrency, Scalability & Fault-tolerance 2.0 with Akka Actors & STM Presentation Transcript

    • Concurrency, Scalability
      & Fault-tolerance 2.0 with Actors & STM
      by Mario Fusco
      mario.fusco@gmail.com
      twitter: @mariofusco
    • Which are the biggestchallengesin contemporary software development?
    • Deal with overloadedsystems
    • Do concurrencyeffectively
    • Design, develop and deployapplicationsthat scale well
    • The native Java concurrency model
      Based on:
      Threads
      Semaphores
      Synchronization
      Locks
      They are sometimes plain evil …
      … and sometimes a necessary pain …
      … but always the wrong default
    • Actors: a different model of concurrency
      • Implements Message-Passing Concurrency
      • Share NOTHING
      • Isolated lightweight processes
      • Communicates through messages
      • Asynchronous and non-blocking
      • Each actor has a mailbox (message queue)
      • A single thread can managemany (thousands of) actors
    • People are bad at parallel thinking
      With actors is easier to reason about and easier to avoid:
      Race conditions, Deadlocks, Starvation and Live locks
    • What is Akka?
      • An actor based concurrency framework
      • Written in Scala, butalsoworking in Java
      • Nowrealeasing 1.0 after 2 years of development (currentlyRC4 available)
      • Better performances and throughput and smallermemoryfootprint (650 bytes) than native scala actors
      • Open source (Apache2)
    • Untyped Actors
      public class MyUntypedActorextends UntypedActor {
      public void onReceive(Object message) throws Exception {
      if (message instanceofString) {
      String msg = (String)message;
      System.out.println("Received message: " + msg);
      }
      }
      }
      UntypedActorRef actor = UntypedActor.actorOf(MyUntypedActor.class);
      actor.start();
      // --- Do some work here ---
      actor.stop();
    • Sending message to UntypedActors
      There are 3 different ways to send a message to an actor:
      Fire-and-forget ( ! In Scala )
      actor.sendOneWay(msg);
      Uses a Future under the hood, blocking the sender Actor until eithera reply is received or the Future times out ( !! In Scala )
      Object res1 = actor.sendRequestReply(msg);
      Object res2 = actor.sendRequestReply(msg, 1000); // timeout
      Explicitly returns a Future ( !!! In Scala )
      Future future= actor.sendRequestReplyFuture(msg);
      IMPORTANT: Messages can be any kind of object but have to be immutable.
      Akkacan’t enforce immutability (yet) so this has to be by convention
    • Typed Actors
      public class CounterImplextends TypedActorimplements Counter {
      private int counter = 0;
      public void count() {
      counter++;
      System.out.println(counter);
      }
      public intgetCountNr() {
      returncounter;
      }
      }
      Counter counter= (Counter)TypedActor
      .newInstance(Counter.class, CounterImpl.class, 1000);
      counter.count(); // Fire-and-forget
      intcount = counter.getCountNr(); // uses Future under the hood
    • “Let it crash”
      fault-tolerance
      through
      supervisor hierarchies
    • You can’t avoid failures: shit happens
    • What’swrong in trying to preventerrors?
      Java and other languages and frameworks (not designed with concurrency in mind) signal faults/errors with exceptions
      Throwing an exception in concurrent code will just simply blow up the thread that currently executes the code that threw it:
      There is no way to find out that things went wrong, apart from inspecting the stack trace
      There is nothing you can do to recover from the problem and bring back your system to a normal functioning
    • Fail fast & build self-repairing systems
    • Supervisedactors
      • Actors provide a clean way of getting error notification and do something about it
      • Supervised actors also allow you to create sets (hierarchies) of actors where you can be sure that either all are dead or none are dead
      • That encourages non-defensive programming: don’t try to prevent things from go wrong, because they will. Failures are a natural state in the life-cycle of your app: crash early and let someone else (that sees the whole picture), deal with it
      • A supervisor is responsible for starting, stopping and monitoring its child processes. It can act with 2 fault handling strategies:
      One-For-One
      or All-For-One
    • Declarative supervised actors configuration
      Supervisor supervisor = new Supervisor(
      new SupervisorConfig(
      new AllForOneStrategy( // or OneForOneStrategy
      new Class[]{Exception.class}, // Exceptions to handle
      3, // maximum number of restart retries
      5000), // within time in millis
      new Supervise[] {
      new Supervise(
      UntypedActor.actorOf(MyActor1.class),
      permanent()), // the actor will always be restarted
      new Supervise(
      UntypedActor.actorOf(MyActor2.class),
      temporary()) // the actor won’t be shut down normally
      }));
      // You can also link and unlink actors from a supervisor
      supervisor.link(actor1);
      supervisor.unlink(actor1);
    • Remote actors
      • Akka provides remote actors in a completely transparent way both in regards to sending messages, error handling and supervision
      • Uses a very efficient and scalable I/O implementation built upon JBossNetty and Google Protocol Buffers
      • Can be secured with a cookie authentication mechanism
      • Can be bothTyped and Untyped
      • Twotypes of remote actors:
      • server-managed: run on the machine thatcreateditwhile client can lookup and obtain a proxy
      • client managed: makethemrunning on a different machine while the localone just hold a proxy to sendthemmessages
    • Server-managed
      Client-managed
      Typed
      Bean actor =
      TypedActor.newInstance(
      Bean.class, BeanImpl.class,
      timeout);
      Actors.remote()
      .start("localhost", 9999)
      .registerTypedActor(
      “my-service", actor
      );
      Bean remoteActor =
      (Bean)TypedActor
      .newRemoteInstance(
      Bean.class, BeanImpl.class,
      timeout, "192.68.23.69", 9999
      );
      Untyped
      class MyActor
      extends UntypedActor {}
      Actors.remote()
      .actorOf(MyActor.class,
      "192.68.23.69", 9999
      );
      class MyActor
      extends UntypedActor {}
      Actors.remote()
      .start("localhost", 9999)
      .register(“my-service",
      actorOf(MyActor.class)
      );
    • Software Transactional Memory
      An STM turns the Java heap into a transactional data set with begin/commit/rollback semantics. Very much like a regular database.
      It implements the first three letters in ACID; ACI:
      • Atomic: all or none of the changes made during a transaction get applied
      • Consistent: a transaction gets a consistent of reality
      • Isolated: changes made by concurrent execution transactions are not visible to each other
      Akka’s STM implements the concept already present in Clojure’s STM
      The STM is based on Transactional References (referred to as Refs). Refs:
      • are memory cells, holding an (arbitrary) immutable value
      • implement CAS (Compare-And-Swap) semantics
      • are managed and enforced by the STM for coordinated changes across many Refs
      • are implemented using the excellent Multiverse STM (http://multiverse.codehaus.org)
    • Why we need an STM?
      public class Container {
      private int value;
      private final ReadWriteLock lock = new ReentrantReadWriteLock();
      public intgetValue() {
      lock.readLock().lock();
      try {
      return value;
      } finally {
      lock.readLock.unlock();
      }
      }  
      public void setValue(int value) {
      lock.writeLock().lock();
      try {
      this.value= value;
      } finally {
      lock.writeLock.unlock();
      }
      }
      }
      What’swrong with it?
      • Ad-hoc and hardlyreusablelogic
      • Unnecessarycomplexity
      • Lockslimitconcurrency and thenthroughput
    • How does Akka STM work?
      // Create a Ref with an initial value (also possible without)
      final Ref<Integer> ref = new Ref<Integer>(0);
       
      public int counter() {
      // A transaction is delimited using an Atomic anonymous inner class
      return new Atomic<Integer>() {
      public Integer atomically() {
      intinc = ref.get() + 1; // Accessing the value of the Ref
      ref.set(inc); // Changing the value of the Ref
      return inc;
      }
      }.execute();
      }
      • A transaction is automatically retried when it runs into some read or write conflict
      • In this case an (exponential) delay isused to preventfurthercontentions
      • Thereshouldn’t be side-effects inside the transaction to avoid to repeatthem
    • Transactional Datastructures
      final TransactionalMap<String, User> users =
      new TransactionalMap<String, User>();  
      // fill users map (in a transaction)
      new Atomic() {
      public Object atomically() {
      users.put("bill", new User("bill"));
      users.put("mary", new User("mary"));
      users.put("john", new User("john"));
      return null;
      }
      }.execute();  
      // access users map (in a transaction)
      User user = new Atomic<User>() {
      public User atomically() {
      return users.get("bill").get();
      }
      }.execute();
    • Persistent Datastructures
      Copyright Rich Hickey 2009
    • Coordinating Actors with STM
      public class Counter extends UntypedActor {
      private Ref<Integer> count = new Ref(0);
      private void increment() { count.set(count.get() + 1); }
      public void onReceive(Object incoming) throws Exception {
      if (incoming instanceof Coordinated) {
      Coordinated coordinated = (Coordinated) incoming;
      Object message = coordinated.getMessage();
      if (message instanceof Increment) {
      Increment increment = (Increment) message;
      if (increment.hasFriend()) {
      increment.getFriend()
      .sendOneWay(coordinated.coordinate(new Increment()));
      }
      coordinated.atomic(new Atomically() {
      public void atomically() {
      increment();
      }
      });
      }}}}
    • Actors + STM = Transactors
      public class Counter extends UntypedTransactor {
      Ref<Integer> count = new Ref<Integer>(0);
      @Override public Set<SendTo> coordinate(Object message) {
      if (message instanceof Increment) {
      Increment increment = (Increment) message;
      if (increment.hasFriend())
      return include(increment.getFriend(), new Increment());
      }
      return nobody();
      }
      public void atomically(Object message) {
      if (message instanceof Increment) {
      count.set(count.get() + 1);
      }
      }
      }
    • Other Akka goodies
      • Implements Advanced Message Queuing Protocol (AMPQ) based on the RabbitMQ
      • Integration with Apache Camel
      • PersistencemodulesupportingmanyNoSQL DB like: Cassandra, MongoDB, Riak, Redis, Terrastore, CouchDB, Voldemort, Hbase
      • Integration betweenAkkaSTM and JTA (Java Transaction API)
      • OSGisupport
      • Integration with Spring allowing to defineTyped Actors as Spring managed bean
      • Integration with Lift Web Framework and Play! Framework
    • Thanks a lot!
      Happy Java (or Scala) hing
      by Mario Fusco
      mario.fusco@gmail.com
      twitter: @mariofusco