introduction to akka - arvutiteaduse instituut · 2013-04-19 · introduction to akka mirko adari...

31
Introduction to Akka Mirko Adari

Upload: others

Post on 22-May-2020

37 views

Category:

Documents


1 download

TRANSCRIPT

Page 1: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Introduction to Akka

Mirko Adari

Page 2: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

• Akka is a toolkit and runtime for building highly concurrent, distributed, and fault tolerant event-driven applications on the JVM.

Page 3: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Why Akka?

• Complexity

– Concurrency

– Scalability

– Fault-tolerance

Page 4: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

CONCURRENCY

Page 5: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Status quo

• Threads

• Shared state

• State visibility

• Locks

• Concurrent collections

• Thread notifications

• …

Page 6: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

What is an actor?

• By Carl Hewitt:

– Unit of computation

• Processing

• Storage

• Communication

Page 7: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

What can actors be used for?

• a thread

• an object instance or component

• a callback or listener

• a singleton or service

• a router load-balancer or pool

• a Finite State Machine

Page 8: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Core operations

• Define

• Create

• Send

• Become

Page 9: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Define

class HelloActor extends Actor {

val log = Logging(context.system, this)

def receive = {

case “hello” = log.info(“received hello”)

case _ = log.info(“received unknown message”)

}

}

Page 10: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Create

class HelloActor extends Actor {

val log = Logging(context.system, this)

def receive = {

val byeActor = context.actorOf(Props[ByeActor],

name = “byeActor”)

}

}

Page 11: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Send

class HelloActor extends Actor {

val log = Logging(context.system, this)

def receive = {

case “hello” = sender ! “hello back”

case _ = log.info(“received unknown message”)

}

}

Page 12: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Become

class HelloActor extends Actor { val log = Logging(context.system, this) def happy: Receive = { case “hello” = sender ! “I feel great.” case “bye” = become(sad) } def sad: Receive = { case “hello” = become(happy) case “sad” = sender ! “I’m thinking of suicide.” } def receive = { case “hello” = become(happy) case “bye” = become(sad) } }

Page 13: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Become

• Transform into a pool or a router

• Finite State Machines

• Adaptive worker processes

Page 14: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Key concepts

• Parallel

• Asynchronous

• Non-blocking

• Immutable

• Mailboxes

Page 15: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

• What happens if actor sends a message to itself?

Page 16: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Futures

class HelloActor extends Actor {

def receive = {

val future = timeActor ? “time”

future onSuccess {

case time = sender ! time

}

}

}

Page 17: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

SCALABILITY

Page 18: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Practices

• Stateless

• Datastore

• Replication

• SOA

Page 19: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Small footprint

• 2.7M actors per GB memory

Page 20: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Routers

akka.actor.deployment {

/hello-pool {

router = round-robin

nr-of-instances = 5

}

}

Page 21: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Remoting

akka {

actor {

provider = "akka.remote.RemoteActorRefProvider"

}

remote {

transport = "akka.remote.netty.NettyRemoteTransport"

netty {

hostname = "127.0.0.1"

port = 2552

}

}

}

Page 22: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

FAULT-TOLERANCE

Page 23: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Inspiration

• Erlang

– Designed by Ericsson in 1986

– Telecommunications

– Modern users

• Facebook

• Amazon

• Yahoo

• T-Mobile

Page 24: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Supervision

• Every actor has a supervisor

Page 25: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

• Who supervises system?

Page 26: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Supervision

• Every actor has a supervision strategy

– Escalate

– Stop

– Restart

– Resume

Page 27: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Default strategy

• ActorInitializationException => Stop

• ActorKilledException => Stop

• Exception => Restart

• Otherwise => Escalate

Page 28: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Custom strategy

class Supervisor extends Actor {

override val supervisorStrategy = OneForOneStrategy(maxNrOfRetries = 10 withinTimeRange = 1 minute) {

case _: ArithmeticException ⇒ Resume

case _: NullPointerException ⇒ Restart

case _: IllegalArgumentException ⇒ Stop

case _: Exception ⇒ Escalate

}

Page 29: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Example

Page 30: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Example

Page 31: Introduction to Akka - Arvutiteaduse instituut · 2013-04-19 · Introduction to Akka Mirko Adari •Akka is a toolkit and runtime for building highly concurrent, distributed, and

Q&A