A Tour of Advanced Akka Features in 60

  • Slides: 95
Download presentation
A Tour of (Advanced) Akka Features in 60 Minutes [CON 1706] Johan Janssen, Info

A Tour of (Advanced) Akka Features in 60 Minutes [CON 1706] Johan Janssen, Info Support @johanjanssen 42

Content ▪ Why Akka? ▪ Sharding ▪ Local actor ▪ Persistence ▪ Remote actor

Content ▪ Why Akka? ▪ Sharding ▪ Local actor ▪ Persistence ▪ Remote actor ▪ Akka HTTP ▪ Scheduling ▪ Finite State Machines ▪ Cluster ▪ Conclusion ▪ Routing ▪ Questions ▪ Cluster singleton

Why Akka?

Why Akka?

Why Akka? ▪ Concurrent ▪ Scalable ▪ Fault tolerant ▪ More natural programming experience

Why Akka? ▪ Concurrent ▪ Scalable ▪ Fault tolerant ▪ More natural programming experience when connecting to other systems ▪ Easy to use?

i a l c s i D r e m

i a l c s i D r e m

Local actor

Local actor

Local actor Actor on JVM 1

Local actor Actor on JVM 1

Local actor Coordinator Actor Hello conference

Local actor Coordinator Actor Hello conference

Local actor Coordinator Actor println("Hello conference")

Local actor Coordinator Actor println("Hello conference")

class Worker extends Actor { def receive = { case x => println(x) }

class Worker extends Actor { def receive = { case x => println(x) } } Scala val system = Actor. System("Example. Actor. System") val worker. Actor. Ref = system. actor. Of(Props[Worker]) worker. Actor. Ref ! "Hello conference"

Remote actor

Remote actor

Remote actor Actor on JVM 1 Actor on JVM 2

Remote actor Actor on JVM 1 Actor on JVM 2

val worker. Actor. Ref = system. actor. Of(Props[Worker]) worker. Actor. Ref ! "Hello conference"

val worker. Actor. Ref = system. actor. Of(Props[Worker]) worker. Actor. Ref ! "Hello conference" val worker. Actor. Ref = context. actor. Selection("akka. tcp: // Example. Actor. System@127. 0. 0. 1: 9005 /user/worker. Actor") worker. Actor. Ref ! "Hello conference"

akka { actor { provider = "akka. remote. Remote. Actor. Ref. Provider" } remote

akka { actor { provider = "akka. remote. Remote. Actor. Ref. Provider" } remote { enabled-transports = ["akka. remote. netty. tcp"] netty. tcp { hostname = "127. 0. 0. 1" port = 9002

Remote actor Start. Message Hello conference Coordinator actor Worker actor

Remote actor Start. Message Hello conference Coordinator actor Worker actor

Remote actor Coordinator actor Worker. Message Greetings from the coordinator: Hello Conference Worker actor

Remote actor Coordinator actor Worker. Message Greetings from the coordinator: Hello Conference Worker actor

Remote actor Coordinator actor Worker. Response Message Item processed successfully Worker actor

Remote actor Coordinator actor Worker. Response Message Item processed successfully Worker actor

Scheduling

Scheduling

Scheduling Tick Scheduled once after 1 second system. scheduler. schedule. Once(1 seconds, schedule. Receive.

Scheduling Tick Scheduled once after 1 second system. scheduler. schedule. Once(1 seconds, schedule. Receive. Actor, Tick) Tock Scheduled every 5 seconds system. scheduler. schedule(0 seconds, 5 seconds, schedule. Receive. Actor, Tock) Actor

Scheduling ▪ Does not work for fixed point in time like 17: 00 –

Scheduling ▪ Does not work for fixed point in time like 17: 00 – Use Quartz

Cluster

Cluster

Cluster Actor. System on JVM 2 Actor. System on JVM 1 Actor. System on

Cluster Actor. System on JVM 2 Actor. System on JVM 1 Actor. System on JVM 3 Actor. System on JVM 4

Seed nodes ▪ Contact points for automatically joining a cluster

Seed nodes ▪ Contact points for automatically joining a cluster

akka { cluster { seed-nodes = [ "akka. tcp: //Cluster. Node@127. 0. 0. 1:

akka { cluster { seed-nodes = [ "akka. tcp: //Cluster. Node@127. 0. 0. 1: 2551", "akka. tcp: //Cluster. Node@127. 0. 0. 1: 2552" ] } }

Cluster Worker Node Port 2551 Association failed with [akka. tcp: //Cluster. System@127. 0. 0.

Cluster Worker Node Port 2551 Association failed with [akka. tcp: //Cluster. System@127. 0. 0. 1: 2552

Cluster Member Up with IP: 127. 0. 0. 1 and port: 2551 Member Up

Cluster Member Up with IP: 127. 0. 0. 1 and port: 2551 Member Up with IP: 127. 0. 0. 1 and port: 2552 Worker Node Port 2552

Cluster Member Up with IP: 127. 0. 0. 1 and port: 2550 Coordinator Node

Cluster Member Up with IP: 127. 0. 0. 1 and port: 2550 Coordinator Node Port 2550 Member Up with IP: 127. 0. 0. 1 and port: 2550 Worker Node Port 2551 Worker Node Port 2552

Cluster Coordinator Node Port 2550 Register. Worker Node Port 2551 Register. Worker Node Port

Cluster Coordinator Node Port 2550 Register. Worker Node Port 2551 Register. Worker Node Port 2552

Cluster Coordinator Node Port 2550 Worker registered with IP: 127. 0. 0. 1 and

Cluster 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 Worker Node Port 2551 Worker Node Port 2552

Routing

Routing

Routing Actor on JVM 2 Actor on JVM 1 Actor on JVM 3 Loadbalancer

Routing Actor on JVM 2 Actor on JVM 1 Actor on JVM 3 Loadbalancer Actor on JVM 4

akka { actor { provider = "akka. cluster. Cluster. Actor. Ref. Provider" } }

akka { actor { provider = "akka. cluster. Cluster. Actor. Ref. Provider" } } deployment { /coordinator/router { router = round-robin-pool nr-of-instances = 10 routees. paths = ["/user/emptystringactor"] cluster { enabled = on allow-local-routees = off } } }

Routing "" Coordinator Actor on JVM 1 Empty. String Actor on JVM 2 Empty.

Routing "" Coordinator Actor on JVM 1 Empty. String Actor on JVM 2 Empty. String Actor on JVM 3 Empty. String Actor on JVM 4

Routing "" Coordinator Actor on JVM 1 Hash. Map[hostname, counter] Empty. String Actor on

Routing "" Coordinator Actor on JVM 1 Hash. Map[hostname, counter] Empty. String Actor on JVM 2 Empty. String Actor on JVM 3 Empty. String Actor on JVM 4

Routing Empty. String Actor on JVM 2 Coordinator Actor on JVM 1 "" Empty.

Routing Empty. String Actor on JVM 2 Coordinator Actor on JVM 1 "" Empty. String Actor on JVM 3 Empty. String Actor on JVM 4

Routing Empty. String Actor on JVM 2 Coordinator Actor on JVM 1 Empty. String

Routing Empty. String Actor on JVM 2 Coordinator Actor on JVM 1 Empty. String Actor on JVM 3 "" Empty. String Actor on JVM 4

Cluster singleton

Cluster singleton

Cluster singleton ▪ Only one instance of the actor in the cluster ▪ (Re)created

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

Cluster singleton Actor on JVM 1 Singleton actor and other actors on JVM 2

Cluster singleton Actor on JVM 1 Singleton actor and other actors on JVM 2 Other actors on JVM 3 Other actors on JVM 4

Cluster singleton "" Coordinator Empty. String Actor on JVM 2 Empty. String Actor on

Cluster singleton "" Coordinator Empty. String Actor on JVM 2 Empty. String Actor on JVM 3 Empty. String Actor on JVM 4

Cluster singleton "" Coordinator Hash. Map[hostname, counter] Empty. String Actor on JVM 2 Empty.

Cluster singleton "" Coordinator Hash. Map[hostname, counter] Empty. String Actor on JVM 2 Empty. String Actor on JVM 3 Empty. String Actor on JVM 4

Cluster singleton "" Coordinator Empty. String Actor on JVM 2 Empty. String Actor on

Cluster singleton "" Coordinator Empty. String Actor on JVM 2 Empty. String Actor on JVM 3 Empty. String Actor on JVM 4

Cluster singleton "" Coordinator Hash. Map[hostname, counter] Empty. String Actor on JVM 2 Empty.

Cluster singleton "" Coordinator Hash. Map[hostname, counter] Empty. String Actor on JVM 2 Empty. String Actor on JVM 3 Empty. String Actor on JVM 4

Cluster singleton Coordinator Empty. String Actor Crashon JVM 2 Empty. String Actor on JVM

Cluster singleton Coordinator Empty. String Actor Crashon JVM 2 Empty. String Actor on JVM 3 Empty. String Actor on JVM 4

Cluster singleton Coordinator "" Empty. String Actor on JVM 3 Empty. String Actor on

Cluster singleton Coordinator "" Empty. String Actor on JVM 3 Empty. String Actor on JVM 4

Cluster singleton Coordinator "" Hash. Map[hostname, counter] Empty. String Actor on JVM 3 Empty.

Cluster singleton Coordinator "" Hash. Map[hostname, counter] Empty. String Actor on JVM 3 Empty. String Actor on JVM 4

Cluster singleton Coordinator "" Empty. String Actor on JVM 3 Empty. String Actor on

Cluster singleton Coordinator "" Empty. String Actor on JVM 3 Empty. String Actor on JVM 4

Cluster singleton Coordinator "" Hash. Map[hostname, counter] Empty. String Actor on JVM 3 Empty.

Cluster singleton Coordinator "" Hash. Map[hostname, counter] Empty. String Actor on JVM 3 Empty. String Actor on JVM 4

Sharding

Sharding

Sharding ▪ Dividing a set of actors over a cluster ▪ Actors will be

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

Sharding 0, 2, 4, 6, 8 Actor on JVM 1 1, 3, 5, 7,

Sharding 0, 2, 4, 6, 8 Actor on JVM 1 1, 3, 5, 7, 9 even Shard on JVM 2 odd Shard on JVM 3

Persistence

Persistence

Persistence ▪ Store actor information ▪ Recover after crash ▪ Possibility to take snapshots

Persistence ▪ Store actor information ▪ Recover after crash ▪ Possibility to take snapshots

Example without persistence Actor Cobol

Example without persistence Actor Cobol

Example without persistence Actor Cobol

Example without persistence Actor Cobol

Example without persistence Actor Cobol

Example without persistence Actor Cobol

Example without persistence Actor Java Cobol

Example without persistence Actor Java Cobol

Example without persistence Actor Crash Cobol Java Restart

Example without persistence Actor Crash Cobol Java Restart

Example without persistence Actor

Example without persistence Actor

Example without persistence Actor Scala

Example without persistence Actor Scala

Example without persistence Actor Scala

Example without persistence Actor Scala

Persistence Journal Command Cobol Persistent. Actor

Persistence Journal Command Cobol Persistent. Actor

Persistence Journal Persistent. Actor Cobol Event

Persistence Journal Persistent. Actor Cobol Event

Persistence Journal Persistent. Actor ACK Cobol Event

Persistence Journal Persistent. Actor ACK Cobol Event

Persistence Journal Persistent. Actor Cobol Event

Persistence Journal Persistent. Actor Cobol Event

Persistence Journal Persistent. Actor Cobol Event Java Event

Persistence Journal Persistent. Actor Cobol Event Java Event

Persistence Journal Crash Persistent. Actor Cobol Java Restart Cobol Event Java Event

Persistence Journal Crash Persistent. Actor Cobol Java Restart Cobol Event Java Event

Persistence Journal Persistent. Actor Cobol Event Java Event

Persistence Journal Persistent. Actor Cobol Event Java Event

Persistence Journal Persistent. Actor Scala Cobol Java Cobol Event Java Event

Persistence Journal Persistent. Actor Scala Cobol Java Cobol Event Java Event

Persistence Journal Persistent. Actor Cobol Java Scala Cobol Event Java Event

Persistence Journal Persistent. Actor Cobol Java Scala Cobol Event Java Event

Snapshots Snapshot store Persistent. Actor State until Event C++ Journal Cobol C++

Snapshots Snapshot store Persistent. Actor State until Event C++ Journal Cobol C++

Snapshots Snapshot store Persistent. Actor State until Event C++ Journal Cobol C++

Snapshots Snapshot store Persistent. Actor State until Event C++ Journal Cobol C++

Snapshots Snapshot store State until Event C++ Persistent. Actor Journal Cobol C++

Snapshots Snapshot store State until Event C++ Persistent. Actor Journal Cobol C++

Snapshots Snapshot store Journal Persistent. Actor State until Event C++ Java Cobol C++

Snapshots Snapshot store Journal Persistent. Actor State until Event C++ Java Cobol C++

Snapshots Snapshot store State until Event C++ Persistent. Actor Journal Cobol C++ Java Crash

Snapshots Snapshot store State until Event C++ Persistent. Actor Journal Cobol C++ Java Crash Restart

Snapshots Snapshot store State until Event C++ Persistent. Actor Journal Cobol C++ Java

Snapshots Snapshot store State until Event C++ Persistent. Actor Journal Cobol C++ Java

Snapshots Snapshot store State until Event C++ Persistent. Actor Cobol C++ Journal Cobol C++

Snapshots Snapshot store State until Event C++ Persistent. Actor Cobol C++ Journal Cobol C++ Java

Snapshots Snapshot store State until Event C++ Persistent. Actor Cobol C++ Java Journal Cobol

Snapshots Snapshot store State until Event C++ Persistent. Actor Cobol C++ Java Journal Cobol C++ Java

Snapshots Snapshot store State until Event C++ Persistent. Actor Cobol C++ Java Scala Journal

Snapshots Snapshot store State until Event C++ Persistent. Actor Cobol C++ Java Scala Journal Cobol C++ Java

Akka HTTP

Akka HTTP

Akka HTTP Actor on JVM 1 Actor on JVM 2

Akka HTTP Actor on JVM 1 Actor on JVM 2

Finite State Machine

Finite State Machine

Finite State Machine ▪ State ▪ Event

Finite State Machine ▪ State ▪ Event

Finite State Machines (FSM) Progress, iteration Crappy Project New Project No. Progress, 2 Progress,

Finite State Machines (FSM) Progress, iteration Crappy Project New Project No. Progress, 2 Progress, iteration Inprogress Project

Finite State Machines (FSM) Iteration: 0 New Project Crappy Project Work harder Inprogress Project

Finite State Machines (FSM) Iteration: 0 New Project Crappy Project Work harder Inprogress Project

Finite State Machines (FSM) Iteration: 0 New Project Good job! Crappy Project Inprogress Project

Finite State Machines (FSM) Iteration: 0 New Project Good job! Crappy Project Inprogress Project

Finite State Machines (FSM) Iteration: 0 New Project Wrong direction Crappy Project Inprogress Project

Finite State Machines (FSM) Iteration: 0 New Project Wrong direction Crappy Project Inprogress Project

Finite State Machines (FSM) Iteration: 0 New Project Use Akka Crappy Project Inprogress Project

Finite State Machines (FSM) Iteration: 0 New Project Use Akka Crappy Project Inprogress Project

Finite State Machines (FSM) Iteration: 1 New Project Good job! Crappy Project Inprogress Project

Finite State Machines (FSM) Iteration: 1 New Project Good job! Crappy Project Inprogress Project

Finite State Machines (FSM) Iteration: 1 New Project Wrong direction Crappy Project Inprogress Project

Finite State Machines (FSM) Iteration: 1 New Project Wrong direction Crappy Project Inprogress Project

Finite State Machines (FSM) Iteration: 1 New Project Use Akka Crappy Project Inprogress Project

Finite State Machines (FSM) Iteration: 1 New Project Use Akka Crappy Project Inprogress Project

Finite State Machines (FSM) Iteration: 2 New Project Crappy Project Get another job! Inprogress

Finite State Machines (FSM) Iteration: 2 New Project Crappy Project Get another job! Inprogress Project

Conclusion

Conclusion

Conclusion ▪ Akka can be used with Scala or Java ▪ There is even

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

Questions Johan Janssen @johanjanssen 42 Git. Hub: https: //github. com/johanjanssen/Akka-examples

Questions Johan Janssen @johanjanssen 42 Git. Hub: https: //github. com/johanjanssen/Akka-examples