Actors with akka

22

Transcript of Actors with akka

Page 1: Actors with akka
Page 2: Actors with akka

Laura DamianValy DiarrassoubaJeremie Charlet16 07 2015

Actors with Akka

Page 3: Actors with akka

IntroductionWhat it’s not

3

Page 4: Actors with akka

Introduction

What it is about

4

Page 5: Actors with akka

PLAN

1. Actor Design Pattern

2. Akka framework features

3. Implementation of an actor system in Scala

4. Presentation of a the work-pulling design pattern

Page 6: Actors with akka

1 - Actor Design Pattern

The organisation

analogy

Create Program Discovery

Build Product Web UI

Build Product Backend

Build Application Module A

Build Application Module B

Developer

Build Application to bring me a

sandwich

Page 7: Actors with akka

1 - Actor Design Pattern

Container for• Behaviour

• What it’s going to do with its request

• Storage• Each actor has its own context/state

> it can affect its behaviour

• Communication• An actor can send messages to other actors

Page 8: Actors with akka

1 - Actor Design Pattern

Axioms:

•An actor can process only 1 message at a time,

the subsequent messages are queued and processed

in order

•When it receives a message it can

•Create new actors

•Send messages to already addressed actors

•Decide what it will do when it receives its next message

Page 9: Actors with akka

9

- OneForOneStrategy

- AllForOneStrategy

2 – Akka framework features

Fault Tolerance

Directives:

- Resume

- Restart

- Stop

- Escalate

Page 10: Actors with akka

10

The organisation

analogy

2 – Akka framework features

Location

transparency

Spain

USA

UK

Page 11: Actors with akka

2 – Akka framework features

• Akka uses the actors design pattern• Is reactive (read http://www.reactivemanifesto.org/ )• Async, non-blocking, message-driven programming model• Very lightweight processes

• Fault tolerance• Supervisor can decide what to do when sub-actors fail

• Location transparencyActors can communicate

• in the same application • on the same host • through remote hosts

• Persistence (production ready?)• Messages can optionally be persisted and replayed

when actor is restarted

Page 12: Actors with akka

• Input file XIP (xml file)

• Operate and validate processes on that XIP

• Send over HTTP files and metadata to Discovery end

12

3 – Implementation of an actor system in Scala

Page 13: Actors with akka

13

3 – Implementation of an actor system in Scala

Page 14: Actors with akka

• Technologies– Spray Can, Akka FSM - Finite State Machine

14

3 – Implementation of an actor system in Scala

Page 15: Actors with akka

Task: categorise 22 millions documents as quickly as possible by using all possible available resources

How? By creating a distributed system

4 – Work Pulling Design Pattern

CategoriserCategoriserCategoriserCategoriserCategoriser

CategoriserCategoriserCategoriserCategoriserCategoriser

Page 16: Actors with akka

Problem: how to make them work together?

By creating a supervisor that will manage the whole collectionand push documents to categorise to its workers

4 – Work Pulling Design Pattern

Categorisation Supervisor

Categorisation Worker

Categorisation Worker

Categorisation Worker

Page 17: Actors with akka

4 – Work Pulling Design Pattern

How to transmit documents to categorise efficiently?

By sending messages to workers

See the problem?

Categorisation Supervisor

Categorisation Worker

Categorisation Worker

Categorisation Worker

C456321;C65465;C654879;C56879

C456321;C65465;C654879;C56879

C456321;C65465;C654879;C56879

C456321;C65465;C654879;C56879C456321;C65465;

C654879;C56879C456321;C65465;C654879;C56879C456321;C65465;

C654879;C56879

C456321;C65465;C654879;C56879C456321;C65465;

C654879;C56879C456321;C65465;C654879;C56879

C456321;C65465;C654879;C56879C456321;C65465;

C654879;C56879C456321;C65465;C654879;C56879C456321;C65465;

C654879;C56879

Page 18: Actors with akka

4 – Work Pulling Design Pattern

Problem of using a simple push solution here:

It’s super quick and easy to browse the index and

send messages with documents

Categorising documents is a complex and

rather slow process (takes 60ms for 1 document)

Quick producer VS slow consumer

> The consumers are going to be overloaded with messages

Page 19: Actors with akka

4 – Work Pulling Design Pattern

Solution: http://www.michaelpollmeier.com/akka-work-pulling-pattern/

Page 20: Actors with akka

4 – Work Pulling Design Pattern

Applied to taxonomy Applications

https://github.com/nationalarchives/taxonomy

There are 2 types batch applications (each runs in its own application server)

•1 instance of Taxonomy-cat-all-supervisor

•N instances of Taxonomy-cat-all-worker

Categorisation supervisor browses the whole index and retrieve 1000 documents at a time

Categorisation worker receives categorisation requests that contains a list of documents to

categorise

Page 21: Actors with akka

4 – Work Pulling Design Pattern

Applied to taxonomy Applications:

Categorisation Supervisor Application

Create Actor Systemhttps://github.com/nationalarchives/taxonomy/blob/master/taxonomy-batch/

src/main/java/uk/gov/nationalarchives/discovery/taxonomy/batch/config/ActorConfiguration.java

Akka configuration file:

https://github.com/nationalarchives/taxonomy/blob/master/taxonomy-batch/src/main/resources/

supervisor.conf

Create Actor and add it to actor system

if supervisor application: create Supervisor actor when application starts

https://github.com/nationalarchives/taxonomy/blob/master/taxonomy-batch/src/main/java/uk/gov/

nationalarchives/discovery/taxonomy/batch/actor/supervisor/CategorisationSupervisorRunner.java

Page 22: Actors with akka

4 – Work Pulling Design Pattern

Applied to taxonomy Applications:

Categorisation Supervisor Application

Supervisor Actorhttps://github.com/nationalarchives/taxonomy/blob/master/taxonomy-common/src/main/java/uk/

gov/nationalarchives/discovery/taxonomy/common/service/async/actor/

CategorisationSupervisorActor.java