Download - First glance at Akka 2.0
![Page 1: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/1.jpg)
Akka 2.0: towards new heights @remeniuk
emilypolar@flickr
![Page 2: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/2.jpg)
Location Transparency
![Page 3: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/3.jpg)
You’re doing your regular coding job for a local
environment, and get ability to run your system in a
distributed environment any time for free.
![Page 4: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/4.jpg)
RMI, EJB, CORBA, SOAP,
XML-RPC, Thrift
![Page 5: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/5.jpg)
RMI, EJB, CORBA, SOAP,
XML-RPC, Thrift
are easy, but ultimately
flawed!
Fail under network
partitioning and partial
failure...
![Page 6: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/6.jpg)
RMI, EJB, CORBA, SOAP,
XML-RPC
are easy, but ultimately
flawed!
Fail under network
partitioning and partial
failure...
It’s time for RPC to retire.
Steve Vinovsky
![Page 7: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/7.jpg)
RMI, EJB, CORBA, SOAP,
XML-RPC
are easy, but ultimately
flawed!
Fail under network
partitioning and partial
failure...
Akka*
Simple toolkit for building
distributed, scalable,
fault-tolerant systems
• actors / transactors
• supervisors
• routing and dispatching
• STM for better shared
stated concurrency
• much more…
![Page 8: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/8.jpg)
class HelloWorldActor extends Actor {
def receive = {
case msg => self reply (msg + " World")
}
}
val myActor = actorOf[HelloWorldActor].start()
val result = myActor !! “message”
LocalActorRef
![Page 9: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/9.jpg)
Dispatcher stuffs actor[-s] with messages
class ActorA extends Actor {
self.dispatcher = someDispatcher
...
}
class ActorB extends Actor {
self.dispatcher = someDispatcher
...
}
![Page 10: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/10.jpg)
Actor Registry knows about all the local
actors
![Page 11: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/11.jpg)
registry.actorFor(id)
registry.actorFor(uuid)
registry.actorsFor(classOf[...])
etc.
![Page 12: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/12.jpg)
making actor accessible remotely
![Page 13: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/13.jpg)
remote.start("localhost", 9999).register(
"hello-service", actorOf[HelloWorldActor])
val actor = remote.actorFor(
"hello-service", "localhost", 9999)
val result = actor !! "Hello"
RemoteActorRef
![Page 14: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/14.jpg)
Moreover, there could be a pool of
actors behind the ActorRef
class MyLoadBalancer extends Actor with LoadBalancer {
val pinger = actorOf(new Actor {
def receive = { case x => println("Pinger: " + x) } }).start()
val ponger = actorOf(new Actor {
def receive = { case x => println("Ponger: " + x) } }).start()
val seq = new CyclicIterator[ActorRef](List(pinger,ponger))
}
![Page 15: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/15.jpg)
Fault tolerance
Groups of linked, supervised actors define subsystems:
those actors crash and restart together
![Page 16: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/16.jpg)
Fault tolerance
Groups of linked, supervised actors define subsystems:
those actors crash and restart together
![Page 17: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/17.jpg)
Fault tolerance
Groups of linked, supervised actors define subsystems:
those actors crash and restart together
![Page 18: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/18.jpg)
Fault tolerance
Groups of linked, supervised actors define subsystems:
those actors crash and restart together
![Page 19: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/19.jpg)
Fault tolerance
Groups of linked, supervised actors define subsystems:
those actors crash and restart together
![Page 20: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/20.jpg)
Fault tolerance
Groups of linked, supervised actors define subsystems:
those actors crash and restart together
![Page 21: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/21.jpg)
Fault tolerance
Groups of linked, supervised actors define subsystems:
those actors crash and restart together
![Page 22: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/22.jpg)
Fault tolerance
Groups of linked, supervised actors define subsystems:
those actors crash and restart together
![Page 23: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/23.jpg)
val supervisor = Supervisor(
SupervisorConfig(
AllForOneStrategy(List(classOf[Exception]), 3, 1000),
Supervise(actorOf[MyActor1], Permanent) ::
Supervise(actorOf[MyActor2], Permanent) ::
Nil))
actor1.link(actor2)
supervisor.link(actor2)
![Page 24: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/24.jpg)
Akka 2.0
![Page 25: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/25.jpg)
In Akka 2.0 there’re no local vs.
remote actors anymore. All the
actors are distributed (clustered, routed)
![Page 26: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/26.jpg)
the new level of transparency
![Page 27: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/27.jpg)
brand new akka-cluster
in Soviet Russia by default, all the actors are local
but when you pass a config
cluster magic starts to happen
![Page 28: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/28.jpg)
cluster magic
• actors migration
• actors replication
• cluster-wide routing
• adaptive load balancing
• distributed actor registry
• leader election
• much more…
![Page 29: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/29.jpg)
New important concepts:
• actor address
• deployment config
![Page 30: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/30.jpg)
[behind the scenes]
actor clustering
Distributed Registry
get deployment config
is scope
local?
add to local actor registry
store serilized actor
factory in ZooKeepercreate
actor
create actor on
node
ClusteredActorRef / RoutedActorRef
LocalActorRef
ZooKeeper is used for storing
cluster configs, serialized actor
factories, metrics, etc.
![Page 31: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/31.jpg)
akka.enabled-modules = ["cluster"]
akka.event-handler-level = "WARNING"
akka.actor.deployment.service-hello.router = "round-robin"
akka.actor.deployment.service-hello.clustered.replication-factor = 2
akka.actor.deployment.service-hello.clustered.preferred-nodes =
["node:node1","node:node3"]
node.start()
helloActor = actorOf[HelloWorld]("service-hello“)
Using clustered actor
![Page 32: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/32.jpg)
![Page 33: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/33.jpg)
![Page 34: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/34.jpg)
![Page 35: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/35.jpg)
![Page 36: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/36.jpg)
[Akka 1.x] PROBLEM: synchronization of
registries in a distributed network?
POSSIBLE SOLUTION: https://github.com/remeniuk/akka-easyscale
![Page 37: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/37.jpg)
Distributed Registry
Node A
node.store(“my-actor", classOf[MyActor], serializerFor(classOf[MyActor]))
Node B
node.use(“my-actor")
![Page 38: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/38.jpg)
Actor Props
actorOf(Props[TestActor]
.withDispatcher(new PinnedDispatcher())
.withFaultHandler(
OneForOneStrategy(List(classOf[Exception]), 5, 1000))
actorOf(Props(self ⇒ {
case “hello" ⇒ self tryReply “hello!"
})
![Page 39: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/39.jpg)
Composable Futures
!! *HINT: (actor ? message).get
![Page 40: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/40.jpg)
Composable Futures
val future1 = for {
a <- (actor ? message1).mapTo[String]
b <- (actor ? message2).mapTo[String]
} yield a + b
val future2 = (actor ? Message3).map(_.toString)
val future3 = Futures.find[String](_.isEmpty)(
Seq(future1, future2))
![Page 41: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/41.jpg)
BONUS: multi-jvm testing with
scalatest in specs2
https://github.com/typesafehub/sbt-multi-jvm
https://github.com/jboner/akka/tree/master/akk
a-cluster/src/multi-jvm
https://github.com/remeniuk/sbt-multi-jvm
![Page 42: First glance at Akka 2.0](https://reader034.vdocuments.us/reader034/viewer/2022052310/554a0e2eb4c9058c5d8b47f7/html5/thumbnails/42.jpg)
GO AND TRY IT!
> git clone https://github.com/jboner/akka
> sbt