Actors with akka

Post on 12-Apr-2017

316 views 1 download

Transcript of Actors with akka

Laura DamianValy DiarrassoubaJeremie Charlet16 07 2015

Actors with Akka

IntroductionWhat it’s not

3

Introduction

What it is about

4

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

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

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

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

9

- OneForOneStrategy

- AllForOneStrategy

2 – Akka framework features

Fault Tolerance

Directives:

- Resume

- Restart

- Stop

- Escalate

10

The organisation

analogy

2 – Akka framework features

Location

transparency

Spain

USA

UK

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

• 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

13

3 – Implementation of an actor system in Scala

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

14

3 – Implementation of an actor system in Scala

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

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

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

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

4 – Work Pulling Design Pattern

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

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

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

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