Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Java Developer Day 2012 Introduction to Actor M...

yunglin
July 20, 2012

Java Developer Day 2012 Introduction to Actor Model

yunglin

July 20, 2012
Tweet

More Decks by yunglin

Other Decks in Technology

Transcript

  1. The Challenge –The clock speed has stopped growing since 2006

    –The free lunch is over –Moore’s Law still applies but only the number of cores in a single chip is increasing. –The new reality: Amdahl's Law. ref: http://en.wikipedia.org/wiki/Amdahl's_law
  2. Concurrency and Parallelism –Concurrency: A condition that exists when at

    least two threads are making progress. A more generalized form of parallelism that can include time-slicing as a form of virtual parallelism. –Parallelism: A condition that arises when at least two threads are executing simultaneously. –Both of them are hard because of shared mutable state.
  3. Issue: Shared Memory Concurrency –Multithreaded Programs are hard to write

    and test – Non-deterministic – Data Race / Race Condition – Locks are hard to use – too many locks – too few locks – locks in wrong order –Poor Performance. – False sharing: Cache Line Issue.
  4. The solution –A new high level programming model – easier

    to understand – deterministic – no shared/mutable state – fully utilize multi-core processors –Possible Solutions: – Functional Programming - Everything is immutable. scala> List(1, 2, 3).par.map(_ + 2) res: List[Int] = List(3, 4, 5) – Actor Model - Keep mutable state internal and communicate with each other through asynchronous messages.
  5. A Brief of the Actor Model –Formalized in 1973 by

    Carl Hewitt and refined by Gul Agha in mid 80s. –The first major adoption is done by Ericsson in mid 80s. – Invented Erlang and later open-sourced in 90s. – Built a distributed, concurrent, and fault-tolerant telcom system which has 99.9999999% uptime
  6. Actor –Lightweight object. –Keep state internally –Asynchronous and non- blocking

    –Messages are kept in mailbox and processed in order. –Massive scalable and lighting fast because of the small call stack.
  7. Introduce Akka –Founded by Jonas Boner and now part of

    Typesafe stack. –Actor implementation on JVM. –Java API and Scala API –Support Remote Actor –Modules: akka-camel, akka-spring, akka-zeromq
  8. Define Actor 1. import akka.actor.UntypedActor; 2. 3. public class Counter

    extends UntypedActor { 4. 5. private int count = 0; 6. 7. public void onReceive(Object message) throws Exception { 8. if (message.equals("increase") { 9. count += 1; 10. } else if (message.equals("get") { 11. getSender().tell(new Result(count)); 12. } else { 13. unhandled(message); 14. } 15. } 16.}
  9. Create And Send Message 1. // Create an Akka system

    2. ActorSystem system = ActorSystem.create("MySystem"); 3. 4. // create a counter 5. final ActorRef counter = 6. system.actorOf(new Props(Counter.class), "counter"); 7. 8. // send message to the counter 9. counter.tell("increase"); 10.Future<Object> count = ask(counter, "get");
  10. More on the Futures 1. // build a model for

    a EC site. 2. def doSearch(userId: String, keyword: String) { 3. 4. val sessionFuture = ask(sessionManager, GetSession(userId)) 5. val adFuture = ask(advertiser, GetAdvertisement) 6. val resultFuture = ask(searcher, Search(keyword)) 7. 8. val recommFuture = sessionFuture.map { 9. session => ask(recommender, Get(keyword, session)) 10. } 11. 12. val responseFuture = for { 13. ad: Advertisement <- adFuture 14. result: SearchResult <- resultFuture 15. recomm: Recommendation <- recommFuture 16. } yield new Model(ad, result, recomm) 17. return responseFuture.get 18.}
  11. Fault Tolerance in Akka 1. public class MySupervisor extends UntypedActor

    { 2. // Restart the child if it throws ServiceUnavailable 3. private static SupervisorStrategy strategy = 4. new OneForOneStrategy(3, Duration.parse(“5 seconds”), 5. new Function<Throwable, Directive>() { 6. @Override 7. public Directive apply(Throwable t) { 8. if (t instanceof IOException) { 9. return restart(); 10. } else { 11. return escalate(); 12. } 13. } 14. }); 15. 16. @Override 17. public SupervisorStrategy supervisorStrategy() { 18. return strategy; 19. } 20.}
  12. Remote Actor –Actors are location transparent and distributable by design.

    –All Actors can be remote actor through configuration without any code changes. –Sending message to a remote Actor is as simple as sending message to local Actor. –Messages are serialized through java serialization, Protocol Buffer serializer or custom serializer. The desired behavior is configurable in the config file.
  13. Remote Actor 1. // define a remote address 2. Address

    addr = 3. new Address("serializer", "MySystem", "host", 1234); 4. 5. // initialize an actor on remote host programmatically. 6. ActorRef ref = system.actorOf( 7. new Props(Counter.class) 8. .withDeploy( 9. new Deploy(new RemoteScope(addr) 10. ) 11. ) 12.);
  14. Routing & Clustering –Clustering support is still under construction and

    will be available in 2.1 release. –A Router routes incoming messages to outbound actors. – RoundRobinRouter – RandomRouter – SmallestMailboxRouter – BroadcastRouter – ScatterGatherFirstCompletedRouter 1. ActorRef router = system.actorOf( 2. new Props(ExampleActor.class) 3. .withRouter(new RoundRobinRouter(5)) 4. );
  15. Use Cases –Event driven messaging system –Stock trend analysis and

    simulation. –Rule based engine. –Multiplayer online games.
  16. Messaging Service. –Publisher – keeps a list of reference to

    subscribers. – when it receives a message, it will forward the message to subscribers. –Subscribers – stores received messages.
  17. Protocol Classes 1. public class Message implements Serializable { 2.

    public final String sender; 3. public final String message; 4. public final DateTime createDate; 5. //skipped... 6. } 7. public class GetMessages implements Serializable { 8. public final DateTime since; 9. //skipped... 10.} 11.public class Subscribe implements Serializable { 12. public final ActorRef subscriber; 13. //skipped... 14.}
  18. The Actor 1. public class PubSubscriber extends UntypedActor { 2.

    private final String name; 3. private final List<Message> received = Lists.newArrayList(); 4. private final Set<ActorRef> subscribers = Sets.newHashSet(); 5. public PubSubscriber(String name) { 6. this.name = name; 7. } 8. public void onReceive(Object message) { 9. if (message instanceof Subscribe) { 10. subscribers.add(((Subscribe) message).subscriber); 11. } else if (message instanceof Message) { 12. Message msg = (Message) message; 13. // if sender is self, forward the message to subscriber. 14. if (Objects.equal(msg.sender, name)) { 15. for (ActorRef subscriber: subscribers) { 16. subscriber.tell(msg); 17. } 18. } else { 19. received.add((Message) message); 20. }
  19. The Actor 21.} else if (message instanceof GetMessages) { 22.

    final DateTime since = ((GetMessages) message).since; 23. Iterable<Message> ret = Iterables.filter(received, 24. new Predicate<Message>() { 25. @Override 26. public boolean apply(@Nullable Message message) { 27. return message.createDate.isAfter(since); 28. } 29. }); 30. getSender().tell(ret); 31. } else { 32. unhandled(message); 33. } 34. } 35.}
  20. External Interface –Akka-Camel 1. class JettyAdapter extends Consumer with ActorLogging

    { 2. 3. def endpointUri = "jetty:http://localhost:8080/" 4. 5. override def receive = { 6. case CamelMessage(body, headers) => { 7. headers.get("op") match { 8. case Some("msg") => handleMessagingOp(headers) 9. case Some("get") => handleGetOp(headers) 10. case op => handleUnsupportedOp(op) 11. } 12. } 13. }
  21. External Interface 14.private def handleMessagingOp(headers: Map[String, Any]) { 15. val

    tweetOption = for( 16. name <- headers.get("name"); 17. msg <- headers.get("msg") 18. ) yield new Message(name.toString, msg.toString, DateTime.now) 19. 20. tweetOption match { 21. case Some(message) => { 22. findOrCreateActorRef(msg).forward(message) 23. } 24. case None => { 25. sender ! "Unable to perform Action." 26. } 27.}} 28.private def findOrCreateActorRef(name: String): ActorRef = { 29. val pubsub = context.actorFor(name) 30. if (pubsub.isTerminated) { 31. context.actorOf(Props(new PubSubscriber(name)), name = name) 32. } else { pubsub } 33.}
  22. Handle Server Shutdown –When server stops, we need to persist

    state to external storage. – actors’ state – unprocessed messages in mail boxes. –For actor’s state, you can implement preStart and postStop method to persiste state to external storage. –For unprocessed message, Akka provides durable mail box backed by local file system.
  23. Going Remote. –There is no code changes to the PubSubscriber

    or protocol classes. – The protocol classes are serializable and immutable already. – The subscriber reference, the ActorRef, is remote ready too. –The only missing piece is the one connects the actors. We need to rewrite the findOrCreateActor() method. – In Akka 2.1 release, it will provide a new cluster module to solve this issue.