27
Actor based programming In Erlang, Scala and F# Actor model baksia Page 1 , 10.06.202 2

Actor three languages

Embed Size (px)

Citation preview

Page 1: Actor three languages

Actor model baksia

Actor based programmingIn Erlang, Scala and F#

Page 1

, 12.04.2023

Page 2: Actor three languages

Actor model baksia

• The reason• The Actor Model• Erlang• Scala• F#• OTP and AKKA

Page 2

, 12.04.2023

Agenda

Page 3: Actor three languages

Actor model baksia

Page 3

, 12.04.2023

The base of the problem

Page 4: Actor three languages

Actor model baksia

Page 4

, 12.04.2023

Another reason

Page 5: Actor three languages

Actor model baksia

Page 5

, 12.04.2023

Concurrency and shared memory model

Page 6: Actor three languages

Actor model baksia

Page 6

, 12.04.2023

Shared memory model

Shared memory

Processthread

Processthread

Processthread

Page 7: Actor three languages

Actor model baksia

• Race conditions• Block contention• Deadlock

Page 7

, 12.04.2023

Problems with shared memory

Page 8: Actor three languages

Actor model baksia

• Light weight processes/threads communicating through messaging

• Messages are buffered in a “mailbox”

• No shared state • Isolated from other processes• Normally event systems are inversion of control

• Adding a listener to an object• Hard to maintain in large systems

• Humans do not share memory, we communicate through messages

Page 8

, 12.04.2023

What is the Actor Model

Page 9: Actor three languages

Actor model baksia

Page 9

, 12.04.2023

Pattern Matching

Page 10: Actor three languages

Actor model baksia

• The world is concurrent• Things in the world don't share data• Things communicate with messages• Things fail

- Joe Armstrong

Page 10

, 12.04.2023

Erlang

Page 11: Actor three languages

Actor model baksia

Page 11

, 12.04.2023

Crash course in Erlang

Variables are immutable

Page 12: Actor three languages

Actor model baksia

• An atom is a global constant starting with lower case• A tuple is an ordered set of elements• Your Java or C# class would be a tuple with an atom

identifier

Page 12

, 12.04.2023

Atoms and tuples

Page 13: Actor three languages

Actor model baksia

• Spawn “spawns” a new Erlang process (light weight)• Spawn returns a PID (Process identifier)• Receive waits for a message

Page 13

, 12.04.2023

Spawn and process ID

Page 14: Actor three languages

Actor model baksia

Page 14

, 12.04.2023

Actors in Erlang

Page 15: Actor three languages

Actor model baksia

• Object functional programming language• Everything in Scala is an object, even functions• Running on JVM and interoperable with Java language

• Also runs on Dalvik VM for Android (Java bytecode)

• Concurrent oriented programming language• Actor model programming style from Erlang• Interactive shell• No attachment to Oracle

Page 15

, 12.04.2023

Crash course in Scala

Page 16: Actor three languages

Actor model baksia

Page 16

, 12.04.2023

Scalatest TestStack

Page 17: Actor three languages

Actor model baksia

Page 17

, 12.04.2023

Scala Actors

Page 18: Actor three languages

Actor model baksia

• React is the “normal” actor, lightweight process• Receive spins out a new Java thread • In the paper *

• 5000 threads• 1 200 000 actors

• React uses partial functions to send the process to the heap

• Receive runs on the stack and is heavyweight JVM threads (like Java)

*Actors That Unify Threads and Events P. Haller, M. Odersky

Page 18

, 12.04.2023

React or receive

Page 19: Actor three languages

Actor model baksia

Page 19

, 12.04.2023

Scala Actor on tuples

Page 20: Actor three languages

Actor model baksia

Page 20

, 12.04.2023

Scala Actor with case classes

Page 21: Actor three languages

Actor model baksia

• F# is a object functional programming language for the .Net Framework

• Derived from ML and is largely compatible with OCaml• Interactive shell, script and compiled language• Runs on mono for Posix based systems

Page 21

, 12.04.2023

Crash course in F#

Page 22: Actor three languages

Actor model baksia

Page 22

, 12.04.2023

Code example F#

Page 23: Actor three languages

Actor model baksia

Page 23

, 12.04.2023

F# Actor MailboxProcessor

Page 24: Actor three languages

Actor model baksia

• Erlang/OTP• Scala Akka• Erlang designed for five nines 99.999 uptime, record is

nine nines 99.999999999 • That is 31ms downtime a year

Page 24

, 12.04.2023

Let it crash (supervision)

Page 25: Actor three languages

Actor model baksia

Page 25

, 12.04.2023

Supervisors in Erlang/OTP and AKKA

Supervisor

ProcessProcess

Process

Page 26: Actor three languages

Actor model baksia

Page 26

, 12.04.2023

Supervising supervisors

Supervisor

Process Process

Process

Supervisor

Process Process

Process

Root Supervisor

Page 27: Actor three languages

Actor model baksia

Page 27

, 12.04.2023