First glance at Akka 2.0

42
Akka 2.0: towards new heights @remeniuk emilypolar@flickr

description

This presentation has been made at the 4th meetup of Belarusian Scala Enthusiasts by @remeniuk

Transcript of First glance at Akka 2.0

Page 1: First glance at Akka 2.0

Akka 2.0: towards new heights @remeniuk

emilypolar@flickr

Page 2: First glance at Akka 2.0

Location Transparency

Page 3: First glance at Akka 2.0

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

RMI, EJB, CORBA, SOAP,

XML-RPC, Thrift

Page 5: First glance at Akka 2.0

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

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

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

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

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

Actor Registry knows about all the local

actors

Page 11: First glance at Akka 2.0

registry.actorFor(id)

registry.actorFor(uuid)

registry.actorsFor(classOf[...])

etc.

Page 12: First glance at Akka 2.0

making actor accessible remotely

Page 13: First glance at Akka 2.0

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

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

Fault tolerance

Groups of linked, supervised actors define subsystems:

those actors crash and restart together

Page 16: First glance at Akka 2.0

Fault tolerance

Groups of linked, supervised actors define subsystems:

those actors crash and restart together

Page 17: First glance at Akka 2.0

Fault tolerance

Groups of linked, supervised actors define subsystems:

those actors crash and restart together

Page 18: First glance at Akka 2.0

Fault tolerance

Groups of linked, supervised actors define subsystems:

those actors crash and restart together

Page 19: First glance at Akka 2.0

Fault tolerance

Groups of linked, supervised actors define subsystems:

those actors crash and restart together

Page 20: First glance at Akka 2.0

Fault tolerance

Groups of linked, supervised actors define subsystems:

those actors crash and restart together

Page 21: First glance at Akka 2.0

Fault tolerance

Groups of linked, supervised actors define subsystems:

those actors crash and restart together

Page 22: First glance at Akka 2.0

Fault tolerance

Groups of linked, supervised actors define subsystems:

those actors crash and restart together

Page 23: First glance at Akka 2.0

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

Akka 2.0

Page 25: First glance at Akka 2.0

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

the new level of transparency

Page 27: First glance at Akka 2.0

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

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

New important concepts:

• actor address

• deployment config

Page 30: First glance at Akka 2.0

[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

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
Page 33: First glance at Akka 2.0
Page 34: First glance at Akka 2.0
Page 35: First glance at Akka 2.0
Page 36: First glance at Akka 2.0

[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

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

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

Composable Futures

!! *HINT: (actor ? message).get

Page 40: First glance at Akka 2.0

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

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

GO AND TRY IT!

> git clone https://github.com/jboner/akka

> sbt