SlideShare a Scribd company logo
Actor-Based ProgrammingOOP 2011Prof. Dr. Michael StalMichael.Stal@siemens.com
Objectives of this PresentationEmphasize the Multicore Revolution and its impact on ProgrammingIntroduce the Actor ModelPresent Show Case using ScalaConclusions Page 2
Motivation: From Single Core to Hard CoreMulti-Core processor architectures and GPUs offer scaling-up opportunitiesBut how can we harness these new hardware architectures?Obviously, we need to adapt our applicationsFortunately, Multi-Threading respectively Concurrent Programming comes to our rescue Page 3
ConcurrencyConcurrency and PerformanceWhen talking about improving the performance of software concurrency and multi-threadingare often considered as a promising technique for achieving this goal.Multi-threading can improve the“real” performance of a system on multi-processor platforms without incurring the overhead of multi-processing.“perceivable” performance of multi-user systems (even on a single-processor platform)
Concurrency PracticeUnfortunately ...Developing multi-threaded systems that are faster than single-threaded systems is a surprisingly non-trivial tasksome multi-threaded systems “only”run slower than their single-threaded counterpartsmost multi-threaded systems do not run at all, due to deadlocks theyproduce
Architectural PerspectiveConcurrency is not merely a programmer‘s choiceInstead, it covers all abstraction layers:Architecture: every system needs its customized concurrency architectureDesign: we must address issues such as synchronization and coordinationImplementation: we need to use the right features for the right purposeThe more a Concurrency approach supports this, the better!Page 6
Every System needs its Concurrency ArchitectureImage processing systems such as rendering farms or modalities require Pipes & FiltersHome-automation systems work best with Concurrent ReactorsProcess control is supported by Half Sync/Half AsyncLogging servers could use self-organizing Leaders/FollowersMobile agents may use the Blackboard Architecture for Information ExchangePage 7
Let‘s start with Object Oriented Concurrent ProgrammingObjects provide services (via methods)... and data (members)Static members resemble global state – beware of Singletons!In general, objects represent reactive (i.e., passive) entities of controlThey interoperate by method invocations or state manipulationUser-managed threads leverage multiple coresbut handling of threads and shared resources is left to the developerShared StateThreadPage 8datadatamethodsmethodsThread
Challenges of (Object-Oriented) Distributed and Concurrent ProgrammingA lot of pitfalls such as deadlocks and race condidions due toBurden of thread managementAccess to shared stateManaging Lockswhich doesn‘t scale for large distributed systems with high numbers of objectsPage 9Shared StatedatadatamethodsmethodsDanger ZoneThreadThread
Alternative Solution: Actors Actors have been introduced in the early 1970s by Hewitt for AI and Distribution:the Actor model is a mathematical model of concurrent computationthat treats "actors" as the universal primitives of concurrent digital computation: in response to a message that it receives, an actor can make local decisions, create more actors, send more messages, and determine how to respond to the next message received. [Hewitt, 73]Page 10Available almost everywhere: Scala, Erlang, F#, Axum, Io, Clojure, Java, Akka, Smalltalk, Prolog, AAL (Asynchronous Agents Library in Visual Studio 2010)
Properties of ActorsAccording to Gul Agha, an actoris an autonomous, interacting component of a distributed systema concurrency primitive not sharing resources with other actorsIt comprises: an immutable identity (name, virtual address)a mutable local state methods to manipulate local statea thread of control (not necessarily an OS thread)Page 11ActorStatethreadmethodmethodActors
Fundamentals of the Actor ModelAn actor may: process messages send messageschange local statecreate new actorsPage 12ActorStatethreadmethodInterfacemethod<< create >>Actorsmessage
Differences between Objects and ActorsObjects encapsulate state and behaviorActors encapsulate state, behavior and executionPage 13StateStatethreadmethodmethodInterfaceInterfacemethodmethod
Asynchronous And Event-Driven ProcessingHALF SYNC / HALF ASYNC PATTERNActorStateActor Mailbox =  Unique Actor AddressthreadmethodmethodIn general,sending messages to other  actors happens in asynchronous, nonblocking mode – often Actor implementations add synchronous mode
Actors react to events (i.e., message arrival)Principles of MessagesMessages sent to an actorshould only contain values but no referencesotherwise, the immutability would be gone with the windcan be considered event notifications that trigger actions within receiving actorare sent in a sort of „fire‘n forget“ Wild West style Page 15
Request/ReplyRequest/Reply semantics by exchanging messagesActor must know identity of sender to replyCall-by-Value Method invocation semantics:Incoming message contains argumentsOutgoing message contains resultManual solution: use tokens to correlate messagesPage 16Actor 2Actor 1<<send>><<send>>ASYNCHRONOUS COMPLETION TOKEN
Actors may implement the Active Object PatternA client invokes a method on the proxy. The proxy returns a future to the client, and creates a method request which it passes to the scheduler. The scheduler inserts the method request into the activation list (not shown here). When the method request becomes runnable, the scheduler removes it from the activation list (not shown here) and executes it. The method request executes the method on the servant and writes results, if any, to the future. Clientsobtain the method’s results via the future.ClientProxySchedulerServantmethodFutureMethodRequestinsertdispatchcallmethodwritereadDetour: Active Object Pattern
FuturesIf the sender needs to coordinate with result, futures represent a powerful conceptA future is an object that asynchronously waits for (and receives) the reply message of another actormay occur explicitly or implicitlyshould involve configurable timeouts Page 18Akka Example:val future = actor !!! Message// sometimes later:future.await// sometimes later:val result = future.getNote: In Akka you could also use awaitOne() or awaitAll() to wake up until one or all futures contain the awaited results
Possible Strategies in Message HandlingActor implementations can open up the strategy for handling messages BYO (Bring-Your-Own):let developer configure how messages are arranged within message queue  FIFO, priority based, etc.let developer provide dispatching strategy for handling messages such as using thread pools, single threads, etc.let developers add communication protocols for remote actorsPage 19StrategyContextalgorithm()strategy()Strategydispatch()ConcreteStrategyAConcreteStrategyB
Remote ActorsActors do not need to be constrained to one machineInstead, they may be distributed across different machinesIn this case, we need middleware mechanisms such asNetwork-wide identitiesRepositories for actor registrationClient-managed versus server-managed actors (in terms of lifecycle)Proxies for transparently accessing remote actorsMessaging interop protocolsPage 20StateNode ANode BStatethreadthreadmethodInterfacemethodmethodInterfacemethod
Handling Fault-ToleranceErlang provides its own philosophy for fault-tolerance: let it crashAlso used by some Actor libraries such as AkkaDifferent models possible:One for One: If one actor crashes, only restart the crashed actorAll for One: If one actor crashes stop the others too and restart actorsOne for One more suitable if actors are independent: otherwise use All for One Can be applied hierarchically, i.e. one supervisor might be supervised by another supervisorPage 21SupervisorlinkSupervisorlink
Sharing Data betweenActorsAgents are not a good solution for applications that share dataThis holds especially when combining multiple actions In database management systems, transactions come to our rescueSame can be done in memory using STMSTM canbeadded to actors to allowsafesharing of commondataPage 22
STMIdea: Separate object from identityIf object is going to be modifiedby an actor, let reference (identity) refer to new objectObjects themselves remainunchanged (immutability!)If anotheractor has changed the object in the meantimeroll back changeotherwise commit changePage 23RefObjectval xObject‘
AkkaintroducesTransactorsforSimplicityPage 24importakka.transactor.Transactorimportakka.stm.RefcaseobjectIncrementclassFriendlyCounter(friend: ActorRef) extendsTransactor{valcount = Ref(0)overridedefcoordinate = { caseIncrement => include(friend) // youcould also addseveralactorshere  }                                   // coordinate will send theIncrementmsg                                      // to otheractors - overriddenbySendTodefatomically = {  // entertransactioncaseIncrement => count alter (_ + 1)  }}
Example - Actors in Scala: Actor DefinitionActors in Scala defined as library, but with an „internal DSL“ look & feelActors must extend base class and define act() methodPage 25import scala.actors._class Starship (val id : String) extends Actor {	def act() {	  println("Engines of USS " + id + " starting")	  for (i <- 1 to 9) println("Warp " + i)	  println("Test succeeded")  	}}object ActorDemo1 extends Application {	new Starship("Enterprise") start}
Example - Actors in Scala: Simplified DefinitionUsing the actor method of the Actor companion object makes it even easier to define an actor:Page 26import scala.actors._import scala.actors.Actor._object ActorDemo2 extends Application {        val enterprise = actor {	  println("Engines of USS Enterprise starting")	  for (i <- 1 to 9) println("Warp " + i)	  println("Test succeeded")}}
Example - Actors in Scala: Processing MessagesMessages are sent to an actor with the bang operator !Messages are received within a receive block using pattern matchingPage 27case object STOPcase object ACCELERATEobject ActorDemo3 extends Application {val enterprise = actor { var continue = true; var speed = 0while(continue) {      receive {case msg  : String => println("Received: " + msg)	case STOP => println("stopping"); continue = false	case ACCELERATE => speed += 1; println("New speed Warp " + speed)      }     }  }  enterprise ! "Hello, Spock"  for (i <- 1 to 9) enterprise ! ACCELERATE  enterprise ! STOP}[info] Running ActorDemo3Received: Hello, SpockNew speed is Warp 1New speed is Warp 2New speed is Warp 3New speed is Warp 4New speed is Warp 5New speed is Warp 6New speed is Warp 7New speed is Warp 8New speed is Warp 9stopping[info] == run ==[success] Successful.
Example - Actors in Scala: react instead of receivereceive uses one thread and can return results reactmore efficient; does not return so that we need to use loop{while}Page 28[info] Running ActorDemo4Received: Hello, SpockNew speed is Warp 1New speed is Warp 2New speed is Warp 3New speed is Warp 4New speed is Warp 5New speed is Warp 6New speed is Warp 7New speed is Warp 8New speed is Warp 9stopping[info] == run ==[success] Successful.case object STOPcase object ACCELERATEclass Starship (val id : String) extends Actor {  private[this] var speed = 0  def act() {    var continue = true    loopWhile(true == continue) {react {        case msg  : String => println("Received: " + msg)        case STOP => println("stopping"); continue = false        case ACCELERATE => speed += 1; println("New speed is Warp " + speed)      }    }  }}object ActorDemo4 extends Application {  val enterprise = new Starship("Enterprise"); enterprise.start  enterprise ! "Hello, Kirk“ for (i <- 1 to 9) enterprise ! ACCELERATE; enterprise ! STOP}
Example - Actors in Scala: react with replyIn this example the actor returns a result using replyThe sender receives the result synchronously using !?Page 29case class AddItem (price: Double)case object SumUpcase object CartDeleteobject Cart extends Actor {  def act() {    var total = 0.0    loop {      react {        case AddItem(price) => total += price        case CartDelete => total = 0.0case SumUp => reply(total); exit      }    }  }}object CartDemo extends Application {  Cart.start  Cart ! AddItem(19.99); Cart ! AddItem(19.99); Cart ! AddItem(2.02)println("Please pay" + (Cart !? SumUp)) }[info] == run ==[info] Running CartDemoPlease pay 42.0[info] == run ==[success] Successful.
Example - Actors in Scala: react and receive with TIMEOUTIf we use react and receive we might block infinitely without receiving a message (especially after not sending any messages anymore)Using the versions with with a predefined waiting time is more efficient in these circumstancesOn timeout a message TIMEOUT is sent to the actorPage 30    loopWhile(true == continue) {reactWithin(1000) {	case msg  : String => println("Received: " + msg)case STOP => println("stopping"); continue = false	case ACCELERATE => speed += 1; println("New speed is Warp " + speed)      }    }
Example - Actors in Scala: Actors should not block using the ActiveActor patternIf actors block during processing a message, mutual deadlock conditions might occur. We can prevent this by separating message receipt and processingPage 31object ActorDemo6 extends Application {  val service = actor {def doWork() { // here the actual work is done       val service = self           actor {         Thread.sleep(100)         service ! "Serve"       }     }     var served = 0     doWork()loop { // here messages are processed       react {         case "Serve" => println("I am serving"); served += 1                         if (served < 3) doWork ()         case msg => println(msg)       }     }  }  service ! “Serve"}
Example - Actors in Scala: Using SchedulersDevelopers may override the scheduler method from the Actor traitFor example, SingleThreadedScheduler maps the actor always to the same (main) threadPer default the ForkJoinScheduler is appliedPage 32class Starship (val id : String) extends Actor {override def scheduler = scala.actors.scheduler.SingleThreadedScheduler  private[this] var speed = 0  def act() {    var continue = true    loopWhile(true == continue) {      react {        case msg  : String => println("Received: " + msg)        case STOP => println("stopping"); continue = false        case ACCELERATE => speed += 1; println("New speed is Warp " + speed)      }    }  }}
Example - Actors in Scala: Using DaemonsDaemon actors provides the same interface such as actors, butThe application may exit even if daemon actors are still runningOn the other hand, an application will wait for running actors to terminateIn addition we might use lightweight reactors (do not transmit the sender with each message, may only use react) or ReplyReactors (senders are passed)Page 33class Starship (val id : String) extends DaemonActor {  private[this] var speed = 0  def act() {    var continue = true    loopWhile(true == continue) {      react {        case msg  : String => println("Received: " + msg)        case STOP => println("stopping"); continue = false        case ACCELERATE => speed += 1; println("New speed is Warp " + speed)      }    }  }}
Example - Actors in Scala: Back to the FutureWith Futures we can receive a reply from an actor asynchronouslyWe need to use the !! operator which returns an objectUsing isSet on this object tells whether result is already availablePage 34case object STOPclass Echo extends Actor {  def act() {    var continue = true    while(continue) {      receive {        case msg  : String => Thread.sleep(1000); sender ! msg        case STOP => println("stopping"); continue = false      }    }  }}object Future extends Application { val echo = new Echo;   echo.startval replyMsg = echo !! "Hello OOP"  println(replyMsg)  println(replyMsg()) // get the result  echo ! STOP}[info] == run ==[info] Running Future<function0>Hello OOPstopping[info] == run ==[success] Successful..
Example - Actors in Scala: Remote Actors ServerRemote Actors can interact across machine or process boundariesThe server needs to register itselfPage 35import scala.actors.Actor._import scala.actors.remote.RemoteActor._object Server {  def remoteShip(ship : String, name: Symbol) = actor {      alive(8888)      register(name, self)      var continue = true; var speed = 0      loopWhile(continue) {        react {case"Stop" => println("Scotty is stopping"); continue = false	case msg : String => println("Uhura received: " + msg)case i : Int => speed += i; println("Spock reports warp speed " + speed)        }       }      }	   def main(args : Array[String]) {     remoteShip("Enterprise", 'enterprise)   }}
Example - Actors in Scala: Remote Actors Client The client connects to the remote node, ...... selects an actor on this node,and sends messages to the remote actorPage 36import scala.actors.Actor._import scala.actors.remote._import scala.actors.remote.RemoteActor._object Client extends Application {val node = Node("127.0.0.1", 8888)   actor {val ship = select(node, 'enterprise)     ship ! "Hello"     for (i <- 1 to 9) ship ! i     ship ! "Stop"    }	}[info] Running ServerUhura received: HelloSpock reports warp speed 4Spock reports warp speed 5Spock reports warp speed 1Spock reports warp speed 6Spock reports warp speed 2Spock reports warp speed 7Spock reports warp speed 3Spock reports warp speed 8Spock reports warp speed 9Scotty is stopping[info] == run ==[success] Successfull.
Example - Actors in Scala: Concurrency Example (1)Example taken from Programming Scala by Venkat SubramaniamTask: find if number is perfect It is perfect  its factors sum up to be twice the numberInstead of using a single-threaded algorithm to check we are using multiple actorsThe number is partitioned into n ranges each of which an actor analyzesKind of Map-Reduce / Master-Slave approach Page 37..........1    to   10002001 to 30001001 to 2000
Example - Actors in Scala: Concurrency Example (2)Page 38def sumOfFactors(number: Int) = {  (0 /: (1 to number)) { (sum, i) => if (number % i == 0) sum + i else sum }}def isPerfect(candidate: Int) = { 2 * candidate == sumOfFactors(candidate) }def sumOfFactorsInRange(lower: Int, upper: Int, number: Int) = {  (0 /: (lower to upper)) { (sum, i) => if (number % i == 0) sum + i else sum }}def isPerfectConcurrent(candidate: Int) = {  val RANGE = 1000000  val numberOfPartitions = (candidate.toDouble / RANGE).ceil.toInt  val caller = self  for (i <- 0 until numberOfPartitions) {     val lower = i * RANGE + 1;    val upper = candidate min (i + 1) * RANGE    actor {       caller ! sumOfFactorsInRange(lower, upper, candidate)     }}  val sum = (0 /: (0 until numberOfPartitions)) { (partialSum, i) =>    receive {       case sumInRange : Int => partialSum + sumInRange    }}            2 * candidate == sum}
Actors to the Limits - Akka for ScalaAkka provides a much more extensive Actor library developed by Jonas BonérOffers typed and untyped actorsSupports STM and TransactorsAllows searching for actors in its registryProvides Oz-like Dataflow concurrencyAnd moreErlang-like Fault ToleranceSerialization of actors and their referencesAdd-on modules for OSGi, Camel, JTA, ...Page 39
Example - Actors in F#The functional programming language F# (standard language in Visual Studio 2010) also supports Actor-based programmingPage 40open Systemlet mailbox =   MailboxProcessor.Start(fun mb ->    let rec loop x =      async { let! msg = mb.Receive()              printfn "I received: %s"  msg              return! loop x }    loop 0)mailbox.Post("Hallo")mailbox.Post(“OOP“)Console.ReadLine() |> ignore
Example - Actors in ErlangErlang - the first commercially relevant language that introduced actors (language feature)Scala inherited this approachPage 41module_as_actor(E) when is_record(E, event) ->    case lists:keysearch(mfa, 1, E#event.contents) of        {value, {mfa, {M, F, _A}}} ->            case lists:keysearch(pam_result, 1, E#event.contents) of                {value, {pam_result, {M2, _F2, _A2}}} ->                    {true, E#event{label = F, from = M2, to = M}};                _ ->                    {true, E#event{label = F, from = M, to = M}}            end;        _ ->            false    end.
Alternative Approach: Using a DSL such as Microsoft AxumPage 42Available for .NETIncubator projectActor definition in Axum, Code also in other .NET languagesDomainDomainShared StateShared Statechannel ChAddition{     input int Add;     output int Sum;     Start: { Add -> S1; }     S1: { Sum -> End; }}Reader AgentWriterAgentAgent:ReaderWriterAgentChannel w/ protocolAgent:Readerdomain A{intaccu;  int sum(int op) { }     agent Z : ChAddition{ }}Reader AgentsWriterAgentWriterAgentWriterAgentSchemaSchemaMicrosoft C++ developers should look at the AAL (Asynchronous Agents Library)
Different Actor Frameworks for Java availableAkkaActorFoundryActor‘s Guild JetlangKilimPage 43Example from Akka Web site http://akkasource.org/// server codeclass HelloWorldActor extends UntypedActor {  public void onReceive(Object msg) {    getContext().reply(msg + " World");  }}RemoteNode.start("localhost", 9999).register(  "hello-service",    actorOf(HelloWorldActor.class);// client codeActorRef actor = RemoteClient.actorFor(  "hello-service", "localhost", 9999);Object res = actor.sendRequestReply("Hello");
Some Possible Liabilities of Actors*In the pure actor model there is no notion of behavior inheritanceDynamic creation of new actors leads to expansion of statenecessity to decide about where to run and store a new actormaybe new communication channelsNote: in a purely actor-based model entities such as integers would be actorsDifficulty to replace behavior in an actorWhat about stream-based media?Fortunately, most of these liabilities can be avoided in hybrid OO/Actor modelsPage 44*see http://www.doc.ic.ac.uk/~nd/surprise_97/journal/vol2/pjm2/
SummaryMulti-core revolution is pushing developers hard; using self-managed threads and synchronization leads to accidental complexityThe Actor model seems to be a promising alternative for Concurrent ProgrammingNew languages and frameworks mix functional, object-oriented concepts with actorsActors are not the right solution for all problems, e.g., for really stateful tasksIntroduction of actors should follow design of software architecture, especially its concurrency and distribution architecture. Still valid: A fool with a tool is still a foolPage 45

More Related Content

PPTX
Oop2010 Scala Presentation Stal
PPTX
Qcon2011 functions rockpresentation_scala
PPTX
Qcon2011 functions rockpresentation_f_sharp
PPSX
DIWE - Programming with JavaScript
PPSX
DIWE - Advanced PHP Concepts
PPSX
Esoft Metro Campus - Certificate in java basics
PPSX
Esoft Metro Campus - Programming with C++
PDF
Object Oriented Programming using C++ Part I
Oop2010 Scala Presentation Stal
Qcon2011 functions rockpresentation_scala
Qcon2011 functions rockpresentation_f_sharp
DIWE - Programming with JavaScript
DIWE - Advanced PHP Concepts
Esoft Metro Campus - Certificate in java basics
Esoft Metro Campus - Programming with C++
Object Oriented Programming using C++ Part I

What's hot (19)

PPSX
Esoft Metro Campus - Certificate in c / c++ programming
PPTX
C++ Object Oriented Programming
PPTX
Generic Programming in java
PPT
Javascript
PPT
Object-Oriented Programming Using C++
PDF
Object Oriented Programming using C++ Part III
PDF
Java Persistence API
PPTX
pebble - Building apps on pebble
PPTX
A (too) Short Introduction to Scala
PPTX
Oops presentation
PDF
Why Java Sucks and C# Rocks (Final)
PPT
ParaSail
PPTX
Java - Generic programming
PDF
A COMPLETE FILE FOR C++
PPSX
DIWE - Fundamentals of PHP
PPT
C++ oop
PPTX
Modern C++
PPT
Java tutorial for Beginners and Entry Level
PDF
Introduction to C++
Esoft Metro Campus - Certificate in c / c++ programming
C++ Object Oriented Programming
Generic Programming in java
Javascript
Object-Oriented Programming Using C++
Object Oriented Programming using C++ Part III
Java Persistence API
pebble - Building apps on pebble
A (too) Short Introduction to Scala
Oops presentation
Why Java Sucks and C# Rocks (Final)
ParaSail
Java - Generic programming
A COMPLETE FILE FOR C++
DIWE - Fundamentals of PHP
C++ oop
Modern C++
Java tutorial for Beginners and Entry Level
Introduction to C++
Ad

Similar to Oop2011 actor presentation_stal (20)

PDF
Introduction to concurrent programming with akka actors
PDF
Introduction to concurrent programming with Akka actors
PDF
Akka lsug skills matter
PDF
Scaling Web Apps with Akka
KEY
Akka london scala_user_group
PDF
Introducing Akka
PDF
Building Massively Scalable application with Akka 2.0
PDF
Actor, an elegant model for concurrent and distributed computation
PDF
Introduction to Actor Model and Akka
PDF
Actor Model Akka Framework
PDF
Model with actors and implement with Akka
PPTX
Akka Actors
PDF
Akka (1)
PDF
Akka-intro-training-public.pdf
KEY
Introduction to Actor Model and Akka
PDF
Build Cloud Applications with Akka and Heroku
PDF
A gentle introduction into AKKA and the actor model
PPTX
Reactive Programming using Actor Model in Akka
PPTX
Actor-based concurrency and Akka Fundamentals
PDF
Take a Look at Akka+Java (English version)
Introduction to concurrent programming with akka actors
Introduction to concurrent programming with Akka actors
Akka lsug skills matter
Scaling Web Apps with Akka
Akka london scala_user_group
Introducing Akka
Building Massively Scalable application with Akka 2.0
Actor, an elegant model for concurrent and distributed computation
Introduction to Actor Model and Akka
Actor Model Akka Framework
Model with actors and implement with Akka
Akka Actors
Akka (1)
Akka-intro-training-public.pdf
Introduction to Actor Model and Akka
Build Cloud Applications with Akka and Heroku
A gentle introduction into AKKA and the actor model
Reactive Programming using Actor Model in Akka
Actor-based concurrency and Akka Fundamentals
Take a Look at Akka+Java (English version)
Ad

Recently uploaded (20)

PDF
Business Ethics Teaching Materials for college
PDF
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Week 4 Term 3 Study Techniques revisited.pptx
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PPTX
Cell Types and Its function , kingdom of life
PPTX
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PPTX
Pharma ospi slides which help in ospi learning
PDF
RMMM.pdf make it easy to upload and study
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
Complications of Minimal Access Surgery at WLH
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PDF
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
Business Ethics Teaching Materials for college
Mark Klimek Lecture Notes_240423 revision books _173037.pdf
Microbial disease of the cardiovascular and lymphatic systems
Week 4 Term 3 Study Techniques revisited.pptx
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
Supply Chain Operations Speaking Notes -ICLT Program
Cell Types and Its function , kingdom of life
Introduction to Child Health Nursing – Unit I | Child Health Nursing I | B.Sc...
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Pharma ospi slides which help in ospi learning
RMMM.pdf make it easy to upload and study
FourierSeries-QuestionsWithAnswers(Part-A).pdf
Origin of periodic table-Mendeleev’s Periodic-Modern Periodic table
Microbial diseases, their pathogenesis and prophylaxis
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Complications of Minimal Access Surgery at WLH
Renaissance Architecture: A Journey from Faith to Humanism
3rd Neelam Sanjeevareddy Memorial Lecture.pdf
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Abdominal Access Techniques with Prof. Dr. R K Mishra

Oop2011 actor presentation_stal

  • 1. Actor-Based ProgrammingOOP 2011Prof. Dr. Michael StalMichael.Stal@siemens.com
  • 2. Objectives of this PresentationEmphasize the Multicore Revolution and its impact on ProgrammingIntroduce the Actor ModelPresent Show Case using ScalaConclusions Page 2
  • 3. Motivation: From Single Core to Hard CoreMulti-Core processor architectures and GPUs offer scaling-up opportunitiesBut how can we harness these new hardware architectures?Obviously, we need to adapt our applicationsFortunately, Multi-Threading respectively Concurrent Programming comes to our rescue Page 3
  • 4. ConcurrencyConcurrency and PerformanceWhen talking about improving the performance of software concurrency and multi-threadingare often considered as a promising technique for achieving this goal.Multi-threading can improve the“real” performance of a system on multi-processor platforms without incurring the overhead of multi-processing.“perceivable” performance of multi-user systems (even on a single-processor platform)
  • 5. Concurrency PracticeUnfortunately ...Developing multi-threaded systems that are faster than single-threaded systems is a surprisingly non-trivial tasksome multi-threaded systems “only”run slower than their single-threaded counterpartsmost multi-threaded systems do not run at all, due to deadlocks theyproduce
  • 6. Architectural PerspectiveConcurrency is not merely a programmer‘s choiceInstead, it covers all abstraction layers:Architecture: every system needs its customized concurrency architectureDesign: we must address issues such as synchronization and coordinationImplementation: we need to use the right features for the right purposeThe more a Concurrency approach supports this, the better!Page 6
  • 7. Every System needs its Concurrency ArchitectureImage processing systems such as rendering farms or modalities require Pipes & FiltersHome-automation systems work best with Concurrent ReactorsProcess control is supported by Half Sync/Half AsyncLogging servers could use self-organizing Leaders/FollowersMobile agents may use the Blackboard Architecture for Information ExchangePage 7
  • 8. Let‘s start with Object Oriented Concurrent ProgrammingObjects provide services (via methods)... and data (members)Static members resemble global state – beware of Singletons!In general, objects represent reactive (i.e., passive) entities of controlThey interoperate by method invocations or state manipulationUser-managed threads leverage multiple coresbut handling of threads and shared resources is left to the developerShared StateThreadPage 8datadatamethodsmethodsThread
  • 9. Challenges of (Object-Oriented) Distributed and Concurrent ProgrammingA lot of pitfalls such as deadlocks and race condidions due toBurden of thread managementAccess to shared stateManaging Lockswhich doesn‘t scale for large distributed systems with high numbers of objectsPage 9Shared StatedatadatamethodsmethodsDanger ZoneThreadThread
  • 10. Alternative Solution: Actors Actors have been introduced in the early 1970s by Hewitt for AI and Distribution:the Actor model is a mathematical model of concurrent computationthat treats "actors" as the universal primitives of concurrent digital computation: in response to a message that it receives, an actor can make local decisions, create more actors, send more messages, and determine how to respond to the next message received. [Hewitt, 73]Page 10Available almost everywhere: Scala, Erlang, F#, Axum, Io, Clojure, Java, Akka, Smalltalk, Prolog, AAL (Asynchronous Agents Library in Visual Studio 2010)
  • 11. Properties of ActorsAccording to Gul Agha, an actoris an autonomous, interacting component of a distributed systema concurrency primitive not sharing resources with other actorsIt comprises: an immutable identity (name, virtual address)a mutable local state methods to manipulate local statea thread of control (not necessarily an OS thread)Page 11ActorStatethreadmethodmethodActors
  • 12. Fundamentals of the Actor ModelAn actor may: process messages send messageschange local statecreate new actorsPage 12ActorStatethreadmethodInterfacemethod<< create >>Actorsmessage
  • 13. Differences between Objects and ActorsObjects encapsulate state and behaviorActors encapsulate state, behavior and executionPage 13StateStatethreadmethodmethodInterfaceInterfacemethodmethod
  • 14. Asynchronous And Event-Driven ProcessingHALF SYNC / HALF ASYNC PATTERNActorStateActor Mailbox = Unique Actor AddressthreadmethodmethodIn general,sending messages to other actors happens in asynchronous, nonblocking mode – often Actor implementations add synchronous mode
  • 15. Actors react to events (i.e., message arrival)Principles of MessagesMessages sent to an actorshould only contain values but no referencesotherwise, the immutability would be gone with the windcan be considered event notifications that trigger actions within receiving actorare sent in a sort of „fire‘n forget“ Wild West style Page 15
  • 16. Request/ReplyRequest/Reply semantics by exchanging messagesActor must know identity of sender to replyCall-by-Value Method invocation semantics:Incoming message contains argumentsOutgoing message contains resultManual solution: use tokens to correlate messagesPage 16Actor 2Actor 1<<send>><<send>>ASYNCHRONOUS COMPLETION TOKEN
  • 17. Actors may implement the Active Object PatternA client invokes a method on the proxy. The proxy returns a future to the client, and creates a method request which it passes to the scheduler. The scheduler inserts the method request into the activation list (not shown here). When the method request becomes runnable, the scheduler removes it from the activation list (not shown here) and executes it. The method request executes the method on the servant and writes results, if any, to the future. Clientsobtain the method’s results via the future.ClientProxySchedulerServantmethodFutureMethodRequestinsertdispatchcallmethodwritereadDetour: Active Object Pattern
  • 18. FuturesIf the sender needs to coordinate with result, futures represent a powerful conceptA future is an object that asynchronously waits for (and receives) the reply message of another actormay occur explicitly or implicitlyshould involve configurable timeouts Page 18Akka Example:val future = actor !!! Message// sometimes later:future.await// sometimes later:val result = future.getNote: In Akka you could also use awaitOne() or awaitAll() to wake up until one or all futures contain the awaited results
  • 19. Possible Strategies in Message HandlingActor implementations can open up the strategy for handling messages BYO (Bring-Your-Own):let developer configure how messages are arranged within message queue FIFO, priority based, etc.let developer provide dispatching strategy for handling messages such as using thread pools, single threads, etc.let developers add communication protocols for remote actorsPage 19StrategyContextalgorithm()strategy()Strategydispatch()ConcreteStrategyAConcreteStrategyB
  • 20. Remote ActorsActors do not need to be constrained to one machineInstead, they may be distributed across different machinesIn this case, we need middleware mechanisms such asNetwork-wide identitiesRepositories for actor registrationClient-managed versus server-managed actors (in terms of lifecycle)Proxies for transparently accessing remote actorsMessaging interop protocolsPage 20StateNode ANode BStatethreadthreadmethodInterfacemethodmethodInterfacemethod
  • 21. Handling Fault-ToleranceErlang provides its own philosophy for fault-tolerance: let it crashAlso used by some Actor libraries such as AkkaDifferent models possible:One for One: If one actor crashes, only restart the crashed actorAll for One: If one actor crashes stop the others too and restart actorsOne for One more suitable if actors are independent: otherwise use All for One Can be applied hierarchically, i.e. one supervisor might be supervised by another supervisorPage 21SupervisorlinkSupervisorlink
  • 22. Sharing Data betweenActorsAgents are not a good solution for applications that share dataThis holds especially when combining multiple actions In database management systems, transactions come to our rescueSame can be done in memory using STMSTM canbeadded to actors to allowsafesharing of commondataPage 22
  • 23. STMIdea: Separate object from identityIf object is going to be modifiedby an actor, let reference (identity) refer to new objectObjects themselves remainunchanged (immutability!)If anotheractor has changed the object in the meantimeroll back changeotherwise commit changePage 23RefObjectval xObject‘
  • 24. AkkaintroducesTransactorsforSimplicityPage 24importakka.transactor.Transactorimportakka.stm.RefcaseobjectIncrementclassFriendlyCounter(friend: ActorRef) extendsTransactor{valcount = Ref(0)overridedefcoordinate = { caseIncrement => include(friend) // youcould also addseveralactorshere } // coordinate will send theIncrementmsg // to otheractors - overriddenbySendTodefatomically = { // entertransactioncaseIncrement => count alter (_ + 1) }}
  • 25. Example - Actors in Scala: Actor DefinitionActors in Scala defined as library, but with an „internal DSL“ look & feelActors must extend base class and define act() methodPage 25import scala.actors._class Starship (val id : String) extends Actor { def act() { println("Engines of USS " + id + " starting") for (i <- 1 to 9) println("Warp " + i) println("Test succeeded") }}object ActorDemo1 extends Application { new Starship("Enterprise") start}
  • 26. Example - Actors in Scala: Simplified DefinitionUsing the actor method of the Actor companion object makes it even easier to define an actor:Page 26import scala.actors._import scala.actors.Actor._object ActorDemo2 extends Application { val enterprise = actor { println("Engines of USS Enterprise starting") for (i <- 1 to 9) println("Warp " + i) println("Test succeeded")}}
  • 27. Example - Actors in Scala: Processing MessagesMessages are sent to an actor with the bang operator !Messages are received within a receive block using pattern matchingPage 27case object STOPcase object ACCELERATEobject ActorDemo3 extends Application {val enterprise = actor { var continue = true; var speed = 0while(continue) { receive {case msg : String => println("Received: " + msg) case STOP => println("stopping"); continue = false case ACCELERATE => speed += 1; println("New speed Warp " + speed) } } } enterprise ! "Hello, Spock" for (i <- 1 to 9) enterprise ! ACCELERATE enterprise ! STOP}[info] Running ActorDemo3Received: Hello, SpockNew speed is Warp 1New speed is Warp 2New speed is Warp 3New speed is Warp 4New speed is Warp 5New speed is Warp 6New speed is Warp 7New speed is Warp 8New speed is Warp 9stopping[info] == run ==[success] Successful.
  • 28. Example - Actors in Scala: react instead of receivereceive uses one thread and can return results reactmore efficient; does not return so that we need to use loop{while}Page 28[info] Running ActorDemo4Received: Hello, SpockNew speed is Warp 1New speed is Warp 2New speed is Warp 3New speed is Warp 4New speed is Warp 5New speed is Warp 6New speed is Warp 7New speed is Warp 8New speed is Warp 9stopping[info] == run ==[success] Successful.case object STOPcase object ACCELERATEclass Starship (val id : String) extends Actor { private[this] var speed = 0 def act() { var continue = true loopWhile(true == continue) {react { case msg : String => println("Received: " + msg) case STOP => println("stopping"); continue = false case ACCELERATE => speed += 1; println("New speed is Warp " + speed) } } }}object ActorDemo4 extends Application { val enterprise = new Starship("Enterprise"); enterprise.start enterprise ! "Hello, Kirk“ for (i <- 1 to 9) enterprise ! ACCELERATE; enterprise ! STOP}
  • 29. Example - Actors in Scala: react with replyIn this example the actor returns a result using replyThe sender receives the result synchronously using !?Page 29case class AddItem (price: Double)case object SumUpcase object CartDeleteobject Cart extends Actor { def act() { var total = 0.0 loop { react { case AddItem(price) => total += price case CartDelete => total = 0.0case SumUp => reply(total); exit } } }}object CartDemo extends Application { Cart.start Cart ! AddItem(19.99); Cart ! AddItem(19.99); Cart ! AddItem(2.02)println("Please pay" + (Cart !? SumUp)) }[info] == run ==[info] Running CartDemoPlease pay 42.0[info] == run ==[success] Successful.
  • 30. Example - Actors in Scala: react and receive with TIMEOUTIf we use react and receive we might block infinitely without receiving a message (especially after not sending any messages anymore)Using the versions with with a predefined waiting time is more efficient in these circumstancesOn timeout a message TIMEOUT is sent to the actorPage 30 loopWhile(true == continue) {reactWithin(1000) { case msg : String => println("Received: " + msg)case STOP => println("stopping"); continue = false case ACCELERATE => speed += 1; println("New speed is Warp " + speed) } }
  • 31. Example - Actors in Scala: Actors should not block using the ActiveActor patternIf actors block during processing a message, mutual deadlock conditions might occur. We can prevent this by separating message receipt and processingPage 31object ActorDemo6 extends Application { val service = actor {def doWork() { // here the actual work is done val service = self actor { Thread.sleep(100) service ! "Serve" } } var served = 0 doWork()loop { // here messages are processed react { case "Serve" => println("I am serving"); served += 1 if (served < 3) doWork () case msg => println(msg) } } } service ! “Serve"}
  • 32. Example - Actors in Scala: Using SchedulersDevelopers may override the scheduler method from the Actor traitFor example, SingleThreadedScheduler maps the actor always to the same (main) threadPer default the ForkJoinScheduler is appliedPage 32class Starship (val id : String) extends Actor {override def scheduler = scala.actors.scheduler.SingleThreadedScheduler private[this] var speed = 0 def act() { var continue = true loopWhile(true == continue) { react { case msg : String => println("Received: " + msg) case STOP => println("stopping"); continue = false case ACCELERATE => speed += 1; println("New speed is Warp " + speed) } } }}
  • 33. Example - Actors in Scala: Using DaemonsDaemon actors provides the same interface such as actors, butThe application may exit even if daemon actors are still runningOn the other hand, an application will wait for running actors to terminateIn addition we might use lightweight reactors (do not transmit the sender with each message, may only use react) or ReplyReactors (senders are passed)Page 33class Starship (val id : String) extends DaemonActor { private[this] var speed = 0 def act() { var continue = true loopWhile(true == continue) { react { case msg : String => println("Received: " + msg) case STOP => println("stopping"); continue = false case ACCELERATE => speed += 1; println("New speed is Warp " + speed) } } }}
  • 34. Example - Actors in Scala: Back to the FutureWith Futures we can receive a reply from an actor asynchronouslyWe need to use the !! operator which returns an objectUsing isSet on this object tells whether result is already availablePage 34case object STOPclass Echo extends Actor { def act() { var continue = true while(continue) { receive { case msg : String => Thread.sleep(1000); sender ! msg case STOP => println("stopping"); continue = false } } }}object Future extends Application { val echo = new Echo; echo.startval replyMsg = echo !! "Hello OOP" println(replyMsg) println(replyMsg()) // get the result echo ! STOP}[info] == run ==[info] Running Future<function0>Hello OOPstopping[info] == run ==[success] Successful..
  • 35. Example - Actors in Scala: Remote Actors ServerRemote Actors can interact across machine or process boundariesThe server needs to register itselfPage 35import scala.actors.Actor._import scala.actors.remote.RemoteActor._object Server { def remoteShip(ship : String, name: Symbol) = actor { alive(8888) register(name, self) var continue = true; var speed = 0 loopWhile(continue) { react {case"Stop" => println("Scotty is stopping"); continue = false case msg : String => println("Uhura received: " + msg)case i : Int => speed += i; println("Spock reports warp speed " + speed) } } } def main(args : Array[String]) { remoteShip("Enterprise", 'enterprise) }}
  • 36. Example - Actors in Scala: Remote Actors Client The client connects to the remote node, ...... selects an actor on this node,and sends messages to the remote actorPage 36import scala.actors.Actor._import scala.actors.remote._import scala.actors.remote.RemoteActor._object Client extends Application {val node = Node("127.0.0.1", 8888) actor {val ship = select(node, 'enterprise) ship ! "Hello" for (i <- 1 to 9) ship ! i ship ! "Stop" } }[info] Running ServerUhura received: HelloSpock reports warp speed 4Spock reports warp speed 5Spock reports warp speed 1Spock reports warp speed 6Spock reports warp speed 2Spock reports warp speed 7Spock reports warp speed 3Spock reports warp speed 8Spock reports warp speed 9Scotty is stopping[info] == run ==[success] Successfull.
  • 37. Example - Actors in Scala: Concurrency Example (1)Example taken from Programming Scala by Venkat SubramaniamTask: find if number is perfect It is perfect  its factors sum up to be twice the numberInstead of using a single-threaded algorithm to check we are using multiple actorsThe number is partitioned into n ranges each of which an actor analyzesKind of Map-Reduce / Master-Slave approach Page 37..........1 to 10002001 to 30001001 to 2000
  • 38. Example - Actors in Scala: Concurrency Example (2)Page 38def sumOfFactors(number: Int) = { (0 /: (1 to number)) { (sum, i) => if (number % i == 0) sum + i else sum }}def isPerfect(candidate: Int) = { 2 * candidate == sumOfFactors(candidate) }def sumOfFactorsInRange(lower: Int, upper: Int, number: Int) = { (0 /: (lower to upper)) { (sum, i) => if (number % i == 0) sum + i else sum }}def isPerfectConcurrent(candidate: Int) = { val RANGE = 1000000 val numberOfPartitions = (candidate.toDouble / RANGE).ceil.toInt val caller = self for (i <- 0 until numberOfPartitions) { val lower = i * RANGE + 1; val upper = candidate min (i + 1) * RANGE actor { caller ! sumOfFactorsInRange(lower, upper, candidate) }} val sum = (0 /: (0 until numberOfPartitions)) { (partialSum, i) => receive { case sumInRange : Int => partialSum + sumInRange }} 2 * candidate == sum}
  • 39. Actors to the Limits - Akka for ScalaAkka provides a much more extensive Actor library developed by Jonas BonérOffers typed and untyped actorsSupports STM and TransactorsAllows searching for actors in its registryProvides Oz-like Dataflow concurrencyAnd moreErlang-like Fault ToleranceSerialization of actors and their referencesAdd-on modules for OSGi, Camel, JTA, ...Page 39
  • 40. Example - Actors in F#The functional programming language F# (standard language in Visual Studio 2010) also supports Actor-based programmingPage 40open Systemlet mailbox = MailboxProcessor.Start(fun mb -> let rec loop x = async { let! msg = mb.Receive() printfn "I received: %s" msg return! loop x } loop 0)mailbox.Post("Hallo")mailbox.Post(“OOP“)Console.ReadLine() |> ignore
  • 41. Example - Actors in ErlangErlang - the first commercially relevant language that introduced actors (language feature)Scala inherited this approachPage 41module_as_actor(E) when is_record(E, event) -> case lists:keysearch(mfa, 1, E#event.contents) of {value, {mfa, {M, F, _A}}} -> case lists:keysearch(pam_result, 1, E#event.contents) of {value, {pam_result, {M2, _F2, _A2}}} -> {true, E#event{label = F, from = M2, to = M}}; _ -> {true, E#event{label = F, from = M, to = M}} end; _ -> false end.
  • 42. Alternative Approach: Using a DSL such as Microsoft AxumPage 42Available for .NETIncubator projectActor definition in Axum, Code also in other .NET languagesDomainDomainShared StateShared Statechannel ChAddition{ input int Add; output int Sum; Start: { Add -> S1; } S1: { Sum -> End; }}Reader AgentWriterAgentAgent:ReaderWriterAgentChannel w/ protocolAgent:Readerdomain A{intaccu; int sum(int op) { } agent Z : ChAddition{ }}Reader AgentsWriterAgentWriterAgentWriterAgentSchemaSchemaMicrosoft C++ developers should look at the AAL (Asynchronous Agents Library)
  • 43. Different Actor Frameworks for Java availableAkkaActorFoundryActor‘s Guild JetlangKilimPage 43Example from Akka Web site http://akkasource.org/// server codeclass HelloWorldActor extends UntypedActor { public void onReceive(Object msg) { getContext().reply(msg + " World"); }}RemoteNode.start("localhost", 9999).register( "hello-service", actorOf(HelloWorldActor.class);// client codeActorRef actor = RemoteClient.actorFor( "hello-service", "localhost", 9999);Object res = actor.sendRequestReply("Hello");
  • 44. Some Possible Liabilities of Actors*In the pure actor model there is no notion of behavior inheritanceDynamic creation of new actors leads to expansion of statenecessity to decide about where to run and store a new actormaybe new communication channelsNote: in a purely actor-based model entities such as integers would be actorsDifficulty to replace behavior in an actorWhat about stream-based media?Fortunately, most of these liabilities can be avoided in hybrid OO/Actor modelsPage 44*see http://www.doc.ic.ac.uk/~nd/surprise_97/journal/vol2/pjm2/
  • 45. SummaryMulti-core revolution is pushing developers hard; using self-managed threads and synchronization leads to accidental complexityThe Actor model seems to be a promising alternative for Concurrent ProgrammingNew languages and frameworks mix functional, object-oriented concepts with actorsActors are not the right solution for all problems, e.g., for really stateful tasksIntroduction of actors should follow design of software architecture, especially its concurrency and distribution architecture. Still valid: A fool with a tool is still a foolPage 45