Ce diaporama a bien été signalé.
Nous utilisons votre profil LinkedIn et vos données d’activité pour vous proposer des publicités personnalisées et pertinentes. Vous pouvez changer vos préférences de publicités à tout moment.

Advanced akka features

1 018 vues

Publié le

Advanced akka features

Publié dans : Logiciels
  • Soyez le premier à commenter

Advanced akka features

  1. 1. @johanjanssen42#VoxxedBerlin Advanced Akka features Martin Kanters, Info Support Johan Janssen, Info Support, @johanjanssen42
  2. 2. Content ▪ Introduction ▪ Why Akka? ▪ Local actor ▪ Remote actor ▪ Scheduling ▪ Cluster ▪ Routing ▪ Cluster singleton ▪ Sharding ▪ Persistence ▪ Akka HTTP ▪ Finite State Machines ▪ Conclusion ▪ Questions
  3. 3. Introduction
  4. 4. Why Akka?
  5. 5. Why Akka? ▪ Concurrent ▪ Scalable ▪ Fault tolerant ▪ More natural programming experience when connecting to other systems ▪ Easy to use?
  6. 6. Local actor
  7. 7. Local actor Actor on JVM 1
  8. 8. Local actor Coordinator Actor Hello conference
  9. 9. Local actor Coordinator Actor println("Hello conference")
  10. 10. class Worker extends Actor { def receive = { case x => println(x) } } implicit val system = ActorSystem("ExampleActorSystem") val workerActorRef = system.actorOf(Props[Worker]) workerActorRef ! "Hello conference" Scala
  11. 11. public class AkkaExampleJava8 extends AbstractActor { public AkkaExample() { receive( match( String.class, System.out::println ).build() ); } } ActorSystem actorSystem = ActorSystem.create(); ActorRef actorRef = actorSystem.actorOf(Props.create(AkkaExampleJava8.class)); actorRef.tell("test", ActorRef.noSender()); Java 8
  12. 12. Remote actor
  13. 13. Remote actor Actor on JVM 1 Actor on JVM 2
  14. 14. val workerActorRef = system.actorOf(Props[Worker]) workerActorRef ! "Hello conference" val workerActorRef = context.actorSelection("akka.tcp:// ExampleActorSystem@127.0.0.1:9005 /user/workerActor") workerActorRef ! "Hello conference"
  15. 15. akka { actor { provider = "akka.remote.RemoteActorRefProvider" } remote { enabled-transports = ["akka.remote.netty.tcp"] netty.tcp { hostname = "127.0.0.1" port = 9002
  16. 16. Remote actor Coordinator actor Worker actor StartMessage Hello conference
  17. 17. Remote actor Coordinator actor Worker actor WorkerMessage Greetings from the coordinator: Hello Conference
  18. 18. Remote actor Coordinator actor Worker actor WorkerResponse Message Item processed successfully
  19. 19. Shared protocol
  20. 20. Actor on JVM 1 Actor on JVM 2 Messages
  21. 21. Coordinator Actor on laptop Worker Actor on Raspberry PiWorker Message
  22. 22. Coordinator Actor on laptop Worker Actor on Raspberry Pi MessageProtocol
  23. 23. SharedMessages object SharedMessages { case class WorkerResponseMessage(body: String) case class WorkerMessage(body: String) }
  24. 24. Message used by application val workerActorRef = context.actorSelection("akka.tcp:// ExampleActorSystem@127.0.0.1:9005 /user/workerActor") workerActorRef ! SharedMessages. WorkerMessage("Greetings from the coordinator: " + body)
  25. 25. Scheduling
  26. 26. Scheduling Actor Scheduled once after 1 second Tick Scheduled every 5 seconds Tock
  27. 27. Scheduling ▪ Does not work for fixed point in time like 17:00 – Use Quartz
  28. 28. Cluster
  29. 29. Cluster ActorSystem on JVM 1 ActorSystem on JVM 3 ActorSystem on JVM 2 ActorSystem on JVM 4
  30. 30. Seed nodes ▪ Contact points for automatically joining a cluster
  31. 31. akka { cluster { seed-nodes = [ "akka.tcp://ClusterNode@127.0.0.1:2551", "akka.tcp://ClusterNode@127.0.0.1:2552" ] } }
  32. 32. Cluster Worker Node Port 2551 Association failed with [akka.tcp://ClusterSystem@127.0.0.1:2552
  33. 33. Cluster Worker Node Port 2551 Worker Node Port 2552 Member Up with IP: 127.0.0.1 and port: 2551 Member Up with IP: 127.0.0.1 and port: 2552 Member Up with IP: 127.0.0.1 and port: 2551 Member Up with IP: 127.0.0.1 and port: 2552
  34. 34. Cluster Worker Node Port 2551 Worker Node Port 2552 Member Up with IP: 127.0.0.1 and port: 2550 Member Up with IP: 127.0.0.1 and port: 2550 Coordinator Node Port 2550
  35. 35. Cluster Worker Node Port 2551 Worker Node Port 2552 Coordinator Node Port 2550 RegisterWorker RegisterWorker
  36. 36. Cluster Worker Node Port 2551 Worker Node Port 2552 Coordinator Node Port 2550 Worker registered with IP: 127.0.0.1 and port: 2551 Worker registered with IP: 127.0.0.1 and port: 2552
  37. 37. Routing
  38. 38. Routing Actor on JVM 1 Actor on JVM 3 Actor on JVM 4 Actor on JVM 2 Loadbalancer
  39. 39. akka { actor { provider = "akka.cluster.ClusterActorRefProvider" deployment { /coordinator/router { router = round-robin-pool nr-of-instances = 10 routees.paths = ["/user/emptystringactor"] cluster { enabled = on allow-local-routees = off } } } } }
  40. 40. Routing Coordinator Actor on JVM 1 EmptyString Actor on JVM 3 EmptyString Actor on JVM 4 EmptyString Actor on JVM 2""
  41. 41. Routing Coordinator Actor on JVM 1 EmptyString Actor on JVM 3 EmptyString Actor on JVM 4 EmptyString Actor on JVM 2"" HashMap[hostname, counter]
  42. 42. Routing Coordinator Actor on JVM 1 EmptyString Actor on JVM 3 EmptyString Actor on JVM 4 EmptyString Actor on JVM 2 ""
  43. 43. Routing Coordinator Actor on JVM 1 EmptyString Actor on JVM 3 EmptyString Actor on JVM 4 EmptyString Actor on JVM 2 ""
  44. 44. Cluster singleton
  45. 45. Cluster singleton ▪ Only one instance of the actor in the cluster ▪ (Re)created on the oldest node ▪ Can be used for instance for scheduling/caching
  46. 46. Cluster singleton Actor on JVM 1 Other actors on JVM 3 Other actors on JVM 4 Singleton actor and other actors on JVM 2
  47. 47. Cluster singleton Coordinator EmptyString Actor on JVM 3 EmptyString Actor on JVM 4 EmptyString Actor on JVM 2""
  48. 48. Cluster singleton Coordinator EmptyString Actor on JVM 3 EmptyString Actor on JVM 4 EmptyString Actor on JVM 2"" HashMap[hostname, counter]
  49. 49. Cluster singleton Coordinator EmptyString Actor on JVM 3 EmptyString Actor on JVM 4 EmptyString Actor on JVM 2""
  50. 50. Cluster singleton Coordinator EmptyString Actor on JVM 3 EmptyString Actor on JVM 4 EmptyString Actor on JVM 2"" HashMap[hostname, counter]
  51. 51. Cluster singleton Coordinator EmptyString Actor on JVM 3 EmptyString Actor on JVM 4 EmptyString Actor on JVM 2 Crash
  52. 52. Cluster singleton Coordinator EmptyString Actor on JVM 3 EmptyString Actor on JVM 4 ""
  53. 53. Cluster singleton Coordinator EmptyString Actor on JVM 3 EmptyString Actor on JVM 4 "" HashMap[hostname, counter]
  54. 54. Cluster singleton Coordinator EmptyString Actor on JVM 3 EmptyString Actor on JVM 4 ""
  55. 55. Cluster singleton Coordinator EmptyString Actor on JVM 3 EmptyString Actor on JVM 4 "" HashMap[hostname, counter]
  56. 56. Sharding
  57. 57. Sharding ▪ Dividing a set of actors over a cluster ▪ Actors will be divided into groups called shards ▪ It will divide based on a logical identifier
  58. 58. Sharding Actor on JVM 1 odd Shard on JVM 3 even Shard on JVM 2 0, 2, 4, 6, 8 1, 3, 5, 7, 9
  59. 59. Sharding method: ShardResolver def shardResolver(numberOfShards: Int): ExtractShardId = { case i: Int => (i % numberOfShards).toString }
  60. 60. Sharding method: IdExtractor def idExtractor: ExtractEntityId = { case i: Int => (i.toString, “New message: “ + i) } Actual message
  61. 61. Persistence
  62. 62. Persistence ▪ Store actor information ▪ Recover after crash ▪ Possibility to take snapshots
  63. 63. Example without persistence Actor Cobol
  64. 64. Example without persistence Actor Cobol
  65. 65. Example without persistence Cobol Actor
  66. 66. Example without persistence Actor Java Cobol
  67. 67. Example without persistence Cobol Java Actor Crash Restart
  68. 68. Example without persistence Actor
  69. 69. Example without persistence Actor Scala
  70. 70. Example without persistence Scala Actor
  71. 71. Persistence PersistentActor Cobol Command Journal
  72. 72. Persistence Journal Cobol Event PersistentActor
  73. 73. Persistence Journal ACK PersistentActor Cobol Event
  74. 74. Persistence Cobol Journal Cobol Event PersistentActor
  75. 75. Persistence Cobol Journal Cobol Event PersistentActor Java Event
  76. 76. Persistence Cobol Java Journal Cobol Event PersistentActor Crash Restart Java Event
  77. 77. Persistence Journal Cobol Event PersistentActor Java Event
  78. 78. Persistence Cobol Java Journal Cobol Event PersistentActor Java Event Scala
  79. 79. Persistence Cobol Java Scala Journal Cobol Event PersistentActor Java Event
  80. 80. Snapshots Journal PersistentActor Snapshot store State until Event C++ C++ Cobol
  81. 81. Snapshots Journal PersistentActor Snapshot store State until Event C++ C++ Cobol
  82. 82. Snapshots Journal PersistentActor Snapshot store State until Event C++ C++ Cobol
  83. 83. Snapshots Journal PersistentActor Snapshot store State until Event C++ Java C++ Cobol
  84. 84. Snapshots Journal PersistentActor Snapshot store State until Event C++ Crash Restart C++ Cobol Java
  85. 85. Snapshots Journal PersistentActor Snapshot store State until Event C++ C++ Cobol Java
  86. 86. Snapshots Cobol C++ Journal PersistentActor Snapshot store State until Event C++ C++ Cobol Java
  87. 87. Snapshots Cobol C++ Java Journal PersistentActor Snapshot store State until Event C++ C++ Cobol Java
  88. 88. Snapshots Cobol C++ Java Scala Journal PersistentActor Snapshot store State until Event C++ C++ Cobol Java
  89. 89. Command Query Responsibility Segregation (CQRS)
  90. 90. CQRS in Akka Journal Cobol Event Cobol PersistentActor Persistence Query
  91. 91. Akka HTTP
  92. 92. Akka HTTP Actor on JVM 1 Actor on JVM 2
  93. 93. Finite State Machine
  94. 94. Finite State Machine ▪ State ▪ Event
  95. 95. Finite State Machines (FSM) Inprogress Project Crappy Project New Project Progress, iteration Progress, iteration Progress, iteration NoProgress Progress, 2
  96. 96. Finite State Machines (FSM) Inprogress Project Crappy Project New Project Work harder Iteration: 0
  97. 97. Finite State Machines (FSM) Inprogress Project Crappy Project New Project Good job! Iteration: 0
  98. 98. Finite State Machines (FSM) Inprogress Project Crappy Project New Project Wrong direction Iteration: 0
  99. 99. Finite State Machines (FSM) Inprogress Project Crappy Project New Project Use Akka Iteration: 0
  100. 100. Finite State Machines (FSM) Inprogress Project Crappy Project New Project Good job! Iteration: 1
  101. 101. Finite State Machines (FSM) Inprogress Project Crappy Project New Project Wrong direction Iteration: 1
  102. 102. Finite State Machines (FSM) Inprogress Project Crappy Project New Project Use Akka Iteration: 1
  103. 103. Finite State Machines (FSM) Inprogress Project Crappy Project New Project Get another job! Iteration: 2
  104. 104. Finite State Machines (FSM) ▪ Two types of events – Progress – NoProgress ▪ All events should be handled! ▪ Integer used to count the number of iterations ▪ After two iterations a message is shown
  105. 105. Conclusion
  106. 106. Conclusion ▪ Akka can be used with Scala or Java ▪ There is even a .NET version of Akka ▪ Akka is really powerful ▪ Akka is quite easy to use ▪ Some features are still experimental
  107. 107. Questions Martin Kanters and Johan Janssen @johanjanssen42 GitHub: https://github.com/johanjanssen/Akka-examples

×