agile lab_bigdata_meetup_akka

33
Reactive and Parallel Programming Presents... Enrico Pigozzi Vito Ressa

Upload: paolo-platter

Post on 15-Apr-2017

257 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: Agile Lab_BigData_Meetup_AKKA

Reactive and Parallel Programming

Presents...

Enrico PigozziVito Ressa

Page 2: Agile Lab_BigData_Meetup_AKKA

Who is Agile Lab?

GO BIG (data) or GO HOME

Page 3: Agile Lab_BigData_Meetup_AKKA

Summary

• Concurrency problemRecurrent issuesHandle concurrency

• AkkaOverviewHow it worksAkka clusterExpansions and use-casesHow we use Akka

• Demo time

Page 4: Agile Lab_BigData_Meetup_AKKA

Parallel and

concurrent Programming

Ⓒ2016 Agile Lab S.r.l.

“We need to stop coding our concurrency and just let our code run concurrently”

Page 5: Agile Lab_BigData_Meetup_AKKA

Shared/mutable state

• Threads (pool) usage to make code “faster” + synchronization features (primitives, locks, defensive copies, etc.) to make it “safe”.

• We tend to create the problem first, and (don’t) fix it up later (N.B. no real concurrency allowed)

Page 6: Agile Lab_BigData_Meetup_AKKA

Issues

• Threads

• expensive (startup, memory, context switching)

• limited (by OS)

• CPU Vs. Synchronus IO balance required

• optimizated usage requires non-blocking API (cumbersome to write and test)

• Design

• bottlenecks, race conditions, deadlocks

• traditional errors management approches (exceptions and error codes) don’ttranslate well

• maintenance / refactoring costs can increase to a fairy high degree (and thereforeavoided)

Page 7: Agile Lab_BigData_Meetup_AKKA

Software Transactional Memory

• Turns memory heap in a transactional data set (Atomic, Consistent, Isolated)

• Based on Transactional References:

Memory cells, holding an immutable value (separates identity from value)

Implement CAS (Compare-And-Swap) semantic (old value still availabeafter change)

Can only be altered within a transaction

Very fast (near constant time)

Thread and iteration safe

Page 8: Agile Lab_BigData_Meetup_AKKA

Futures

future(a,b)futureB(a)

Asynchronous values

Composable functional abstraction

Error propagation and recover allowed

Callback on termination available

for {

a ← futureA()

b ← futureB(a)

c ← futureC(a,b)

} yield c

futureA()

Page 9: Agile Lab_BigData_Meetup_AKKA

Actors & Message passing• Unit of code organization: “policy” separated from “logic”, incapsulate state,

behaviour, mailbox

• Weakly / Adaptive typed: achitectural flexibility enhanced (decupled design)

• Safe: one thing a time in isolated context (actor accessed by reference, interaction by messages)

• Immutable structures/messages managed by actors in isolated/safe context

Page 10: Agile Lab_BigData_Meetup_AKKA

Finite State Machine

• Applied to actor: must be in one of the defined (in type system) state all the time

• A data object can be attached to each state (M:N relation)

• A starting state (and eventually data) has to be defined

• Event insted of simple messages: provides current state info

• 'When/goto/stay' model: define actor reaction (partial function) to eventfor a state

S1S2

S3S4S5

A

B A

A A

A

B

BB

BA,B = possible messages

S1,…S5 = States

Page 11: Agile Lab_BigData_Meetup_AKKA

Microservices

• Bounded Context: Each element of functionality into a separate service• Fault tolerance• Deployment independency• DataStore independency• Loose coupling• Service Discovery• Sync/async messaging between services• Scalability

Page 12: Agile Lab_BigData_Meetup_AKKA

Akka

Ⓒ2016 Agile Lab S.r.l.

Page 13: Agile Lab_BigData_Meetup_AKKA

Overview

Event-driven systems with

Actors

High Performance

Easy concurrency handling

Simple distribution

High flexibility

ResilienceReactive Manifesto

http://www.reactivemanifesto.org/

Commercial Support

Page 14: Agile Lab_BigData_Meetup_AKKA

Strenghts

Page 15: Agile Lab_BigData_Meetup_AKKA

How it works?

• Actor as an essential code unit• Messages for actors interaction• Hierarchical organization• Supervision Hierarchy

Page 16: Agile Lab_BigData_Meetup_AKKA

Actors Lifecycle

• Actor instance is hidden• Actor restarting

transparent to other actors• Mailbox bounded to the

incarnation

Page 17: Agile Lab_BigData_Meetup_AKKA

Messages

• Any kind of message but immutable!• Hint: In scala use case classes and take advantage of

pattern matching (see next slide)

Tell or Ask

Page 18: Agile Lab_BigData_Meetup_AKKA

Mailbox• Landing point for messages• Bounded to the incarnation of the actor (not the instance!)• Allows different configurations (FIFO, LIFO, custom priority, ...)• A mailbox can be shared in case of balancing pools

MAILBOX STASH

unstash()

Page 19: Agile Lab_BigData_Meetup_AKKA

Behaviors

Message handling

Behaviors management

Page 20: Agile Lab_BigData_Meetup_AKKA

Akka

Cluster

Ⓒ2016 Agile Lab S.r.l.

Page 21: Agile Lab_BigData_Meetup_AKKA

Akka Cluster

Node A

Node B

Node C

Node D

LeaderCLUSTER

Cluster membership service• Peer to peer• Decentralized• Fault tolerant• No single point of failure• No single point of bottleneck

A Node is identified by a tuple

hostname:port:uid

• Gossip Protocol• Automatic failure detector

Page 22: Agile Lab_BigData_Meetup_AKKA

Failure detection

An implementation of The Phi Accrual Failure Detector

Failure Detector

input

Nodes heartbeats phi-value

Is a node unreachable or not? Depends on a treshold

Page 23: Agile Lab_BigData_Meetup_AKKA

Extensions and

use-cases

Ⓒ2016 Agile Lab S.r.l.

Page 24: Agile Lab_BigData_Meetup_AKKA

Extensions

Community projects available at http://akka.io/community/

• Akka persistence - http://doc.akka.io/docs/akka/current/scala/persistence.html• Akka HTTP - http://doc.akka.io/docs/akka-stream-and-http-

experimental/current/scala/http/• Akka Streams - http://doc.akka.io/docs/akka-stream-and-http-

experimental/current/scala/http/• Akka Camel - http://doc.akka.io/docs/akka/current/scala/camel.html• Akka FSM - http://doc.akka.io/docs/akka/current/scala/fsm.html

Page 25: Agile Lab_BigData_Meetup_AKKA

Use cases: GearPump

Ref: https://www.typesafe.com/resources/case-studies-and-stories/intels-gearpump-real-time-streaming-engine-using-akka

• Real-time streaming engine• Multiple stream datasources• Need of HA, scalability, performance• "Once at a time" transactional message processing

With 4 nodes GearPump can process up to 11 million

messages per seconds with an average latency of 17ms

(100 bytes messages)

Page 26: Agile Lab_BigData_Meetup_AKKA

Use cases: Paycasso

• Three-factor authentication platform• High speed authentication is mandatory• Scala, Akka and Cassandra at the core as a reactive stack• Make use of Akka HTTP, Akka Persistence, Akka Cluster Sharding.

Ref: https://www.typesafe.com/resources/case-studies-and-stories/paycasso-conquers-speed-gap-in-three-factor-authentications-with-reactive-stack

Page 27: Agile Lab_BigData_Meetup_AKKA

Use cases: SAMI• Data broker in the cloud for IoT• Need for reactiveness and resiliency• Microservices-based platform with Akka

Akka

usages

• Actors dedicated to clients for each WebSocket.

• At the transformation layer, where data is

streamed into the system and normalized.

Ref: https://www.typesafe.com/resources/case-studies-and-stories/samsung-strategy-and-innovation-center-executes-iot-vision-at-startup-speed-with-reactive-architecture

Page 28: Agile Lab_BigData_Meetup_AKKA

Other use cases

Interesting Discussion

http://stackoverflow.com/questions/4493001/good-use-case-for-akka

Online Gaming

Finance

StatisticsBetting

Social Media

Telecom

Web Media

Page 29: Agile Lab_BigData_Meetup_AKKA

How we use

Akka

Ⓒ2016 Agile Lab S.r.l.

Page 30: Agile Lab_BigData_Meetup_AKKA

Akka in Action with

Microservices Mosaico RCS

Reverse Proxy

Akka

Srv1

Akka

Srv2

Akka

Srv3

Akka

Srv4Akka

Srv5

Akka

Srv6

Mongo

DB

Mongo

DB

My

SQLS3

Akka Custering

Page 31: Agile Lab_BigData_Meetup_AKKA

Akka in Action with BigData

Wasp AgileLab

Akka Custering

master

Producer

Guardian

Consumer

Gardian

API

Guardian

E

PE

PE

PE

P

K

RK

R

K

R

R

T

S

P

S

PWS TCP

Legend:EP: EndPoint DataInKR:KafkaRouterSP: Spark PipelineRT: Real TimeWS: Web Socket API

DataIn

ControlPlane

Page 32: Agile Lab_BigData_Meetup_AKKA

Demo time...

Ⓒ2016 Agile Lab S.r.l.

Page 33: Agile Lab_BigData_Meetup_AKKA

We are

hiring...

Ⓒ2016 Agile Lab S.r.l.

[email protected]