Date post: | 19-May-2015 |
Category: |
Technology |
Upload: | reidar-sollid |
View: | 1,393 times |
Download: | 1 times |
Actor model baksia
Actor based programmingIn Erlang, Scala and F#
Page 1
, 12.04.2023
Actor model baksia
• The reason• The Actor Model• Erlang• Scala• F#• OTP and AKKA
Page 2
, 12.04.2023
Agenda
Actor model baksia
Page 3
, 12.04.2023
The base of the problem
Actor model baksia
Page 4
, 12.04.2023
Another reason
Actor model baksia
Page 5
, 12.04.2023
Concurrency and shared memory model
Actor model baksia
Page 6
, 12.04.2023
Shared memory model
Shared memory
Processthread
Processthread
Processthread
Actor model baksia
• Race conditions• Block contention• Deadlock
Page 7
, 12.04.2023
Problems with shared memory
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
Actor model baksia
Page 9
, 12.04.2023
Pattern Matching
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
Actor model baksia
Page 11
, 12.04.2023
Crash course in Erlang
Variables are immutable
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
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
Actor model baksia
Page 14
, 12.04.2023
Actors in Erlang
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
Actor model baksia
Page 16
, 12.04.2023
Scalatest TestStack
Actor model baksia
Page 17
, 12.04.2023
Scala Actors
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
Actor model baksia
Page 19
, 12.04.2023
Scala Actor on tuples
Actor model baksia
Page 20
, 12.04.2023
Scala Actor with case classes
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#
Actor model baksia
Page 22
, 12.04.2023
Code example F#
Actor model baksia
Page 23
, 12.04.2023
F# Actor MailboxProcessor
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)
Actor model baksia
Page 25
, 12.04.2023
Supervisors in Erlang/OTP and AKKA
Supervisor
ProcessProcess
Process
Actor model baksia
Page 26
, 12.04.2023
Supervising supervisors
Supervisor
Process Process
Process
Supervisor
Process Process
Process
Root Supervisor
Actor model baksia
Page 27
, 12.04.2023