Actor based programming
       In Erlang, Scala and F#




                                                         Page 1
                                 Actor model baksia , 29.09.2012
Agenda


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




                                            Page 2
                    Actor model baksia , 29.09.2012
The base of the problem




                                                  Page 3
                          Actor model baksia , 29.09.2012
Another reason




                                         Page 4
                 Actor model baksia , 29.09.2012
Concurrency and shared memory model




                                                       Page 5
                               Actor model baksia , 29.09.2012
Shared memory model



              Shared
              memory

 Process                Process
 thread                 thread


              Process
              thread




                                                      Page 6
                              Actor model baksia , 29.09.2012
Problems with shared memory


• Race conditions
• Block contention
• Deadlock




                                                         Page 7
                                 Actor model baksia , 29.09.2012
What is the Actor Model


• 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
                                                    Actor model baksia , 29.09.2012
Pattern Matching




                                           Page 9
                   Actor model baksia , 29.09.2012
Erlang


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




                                                                Page 10
                                         Actor model baksia , 29.09.2012
Crash course in Erlang
Variables are immutable




                                                   Page 11
                            Actor model baksia , 29.09.2012
Atoms and tuples


• 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
                                                     Actor model baksia , 29.09.2012
Spawn and process ID
• Spawn “spawns” a new Erlang process (light weight)
• Spawn returns a PID (Process identifier)
• Receive waits for a message




                                                                         Page 13
                                                  Actor model baksia , 29.09.2012
Actors in Erlang




                                          Page 14
                   Actor model baksia , 29.09.2012
Crash course in Scala


• 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
                                                          Actor model baksia , 29.09.2012
Scalatest TestStack




                                             Page 16
                      Actor model baksia , 29.09.2012
Scala Actors




                                      Page 17
               Actor model baksia , 29.09.2012
React or receive


• 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
                                                              Actor model baksia , 29.09.2012
Scala Actor on tuples




                                               Page 19
                        Actor model baksia , 29.09.2012
Scala Actor with case classes




                                                       Page 20
                                Actor model baksia , 29.09.2012
Crash course in F#


• 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
                                                    Actor model baksia , 29.09.2012
Code example F#




                                         Page 22
                  Actor model baksia , 29.09.2012
F# Actor MailboxProcessor




                                                   Page 23
                            Actor model baksia , 29.09.2012
Let it crash (supervision)


• 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
                                                     Actor model baksia , 29.09.2012
Supervisors in Erlang/OTP and AKKA



   Supervisor

                            Process




                  Process
     Process




                                                             Page 25
                                      Actor model baksia , 29.09.2012
Supervising supervisors

  Root
Supervisor




                                 Supervisor
Supervisor
                                                         Process
                       Process



                                 Process      Process
Process      Process


                                                                         Page 26
                                                  Actor model baksia , 29.09.2012
Page 27
Actor model baksia , 29.09.2012

Actor three languages

  • 1.
    Actor based programming In Erlang, Scala and F# Page 1 Actor model baksia , 29.09.2012
  • 2.
    Agenda • The reason •The Actor Model • Erlang • Scala • F# • OTP and AKKA Page 2 Actor model baksia , 29.09.2012
  • 3.
    The base ofthe problem Page 3 Actor model baksia , 29.09.2012
  • 4.
    Another reason Page 4 Actor model baksia , 29.09.2012
  • 5.
    Concurrency and sharedmemory model Page 5 Actor model baksia , 29.09.2012
  • 6.
    Shared memory model Shared memory Process Process thread thread Process thread Page 6 Actor model baksia , 29.09.2012
  • 7.
    Problems with sharedmemory • Race conditions • Block contention • Deadlock Page 7 Actor model baksia , 29.09.2012
  • 8.
    What is theActor Model • 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 Actor model baksia , 29.09.2012
  • 9.
    Pattern Matching Page 9 Actor model baksia , 29.09.2012
  • 10.
    Erlang • The worldis concurrent • Things in the world don't share data • Things communicate with messages • Things fail - Joe Armstrong Page 10 Actor model baksia , 29.09.2012
  • 11.
    Crash course inErlang Variables are immutable Page 11 Actor model baksia , 29.09.2012
  • 12.
    Atoms and tuples •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 Actor model baksia , 29.09.2012
  • 13.
    Spawn and processID • Spawn “spawns” a new Erlang process (light weight) • Spawn returns a PID (Process identifier) • Receive waits for a message Page 13 Actor model baksia , 29.09.2012
  • 14.
    Actors in Erlang Page 14 Actor model baksia , 29.09.2012
  • 15.
    Crash course inScala • 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 Actor model baksia , 29.09.2012
  • 16.
    Scalatest TestStack Page 16 Actor model baksia , 29.09.2012
  • 17.
    Scala Actors Page 17 Actor model baksia , 29.09.2012
  • 18.
    React or receive •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 Actor model baksia , 29.09.2012
  • 19.
    Scala Actor ontuples Page 19 Actor model baksia , 29.09.2012
  • 20.
    Scala Actor withcase classes Page 20 Actor model baksia , 29.09.2012
  • 21.
    Crash course inF# • 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 Actor model baksia , 29.09.2012
  • 22.
    Code example F# Page 22 Actor model baksia , 29.09.2012
  • 23.
    F# Actor MailboxProcessor Page 23 Actor model baksia , 29.09.2012
  • 24.
    Let it crash(supervision) • 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 Actor model baksia , 29.09.2012
  • 25.
    Supervisors in Erlang/OTPand AKKA Supervisor Process Process Process Page 25 Actor model baksia , 29.09.2012
  • 26.
    Supervising supervisors Root Supervisor Supervisor Supervisor Process Process Process Process Process Process Page 26 Actor model baksia , 29.09.2012
  • 27.
    Page 27 Actor modelbaksia , 29.09.2012