46
CONCURRENCY AND THE ACTOR MODEL © University of Liverpool COMP 319 slide 1

CONCURRENCY AND THE ACTOR MODEL

  • Upload
    iden

  • View
    58

  • Download
    1

Embed Size (px)

DESCRIPTION

CONCURRENCY AND THE ACTOR MODEL. Concurrency review. Multi-tasking Task Processes with isolated address space Programming examples Unix fork() processes IPC via Pipes, FIFOs Shared memory Fork() returns -1 for error 0 for child >0 for parent ( pid of parent) . Unix fork example. - PowerPoint PPT Presentation

Citation preview

Page 1: CONCURRENCY AND THE ACTOR MODEL

CONCURRENCY AND THE ACTOR MODEL

© University of LiverpoolCOMP 319 slide 1

Page 2: CONCURRENCY AND THE ACTOR MODEL

Concurrency review• Multi-tasking- Task- Processes with isolated address

space- Programming examples- Unix fork() processes- IPC via- Pipes, FIFOs- Shared memory

- Fork() returns- -1 for error- 0 for child->0 for parent (pid of parent)

© University of LiverpoolCOMP319 slide 2

Page 3: CONCURRENCY AND THE ACTOR MODEL

Unix fork examplemain(){ pid_t pID = fork(); if (pID == 0) // child { // Code only executed by child process else if (pID < 0) // fork failed { exit(1); }else // parent {// Code only executed by parent process} © University of LiverpoolCOMP319 slide 3

Page 4: CONCURRENCY AND THE ACTOR MODEL

Threads and processes

© University of LiverpoolCOMP319 slide 4

Process 1 Process 2 Process 3

Page 5: CONCURRENCY AND THE ACTOR MODEL

Standard concurrency in Java review• Threads- Shared memory, flow of execution- Uses Thread or Runnable

• Mutex-Monitors

• Thread synchronization-wait- notify

© University of LiverpoolCOMP319 slide 5

Page 6: CONCURRENCY AND THE ACTOR MODEL

Green Threads and threads• Green threads- Scheduled and switched by the JVM

• Native threads- Supported by the OS- Scheduling depends on OS

• When we look at Actor languages concurrency the difference is important

© University of LiverpoolCOMP319 slide 6

Page 7: CONCURRENCY AND THE ACTOR MODEL

Concurrency and deadlock

© University of LiverpoolCOMP319 slide 7

wait()Thread 1Thread 1wait

Thread 2 notify()

JVMThread 1

Page 8: CONCURRENCY AND THE ACTOR MODEL

Deadlock (circular dependency)

© University of LiverpoolCOMP319 slide 8

Code thread 1object1.wait()

object2.signal()object1

wait()

Code thread 2object2.wait()

object3.signal()object2

wait()

Code thread 3object3.wait()

object1.signal()object1

wait()

Page 9: CONCURRENCY AND THE ACTOR MODEL

Synchronization deadlockpublic void transferMoney(Account fromAccount, Account toAccount, int amountToTransfer) { synchronized (fromAccount) { synchronized (toAccount) { if (fromAccount.hasSufficientBalance(amountToTransfer) { fromAccount.debit(amountToTransfer); toAccount.credit(amountToTransfer); } } } }

© University of LiverpoolCOMP319 slide 9

transferMoney(accountOne, accountTwo, amount);

transferMoney(accountTwo, accountOne, amount);

Page 10: CONCURRENCY AND THE ACTOR MODEL

Double lock issuepublic static Singleton getInstance(){ if (instance == null) { synchronized(Singleton.class) { //1 if (instance == null) //2 instance = new Singleton(); //3 } } return instance;}

© University of LiverpoolCOMP319 slide 10

Page 11: CONCURRENCY AND THE ACTOR MODEL

Deadlock as race condition errors• Very hard to test for…public void transferMoney(Account fromAccount, Account toAccount, int amountToTransfer) { synchronized (fromAccount) { synchronized (toAccount) { if (fromAccount.hasSufficientBalance(amountToTransfer) { fromAccount.debit(amountToTransfer); toAccount.credit(amountToTransfer); } }

© University of LiverpoolCOMP319 slide 11

Pre-emption required here

Page 12: CONCURRENCY AND THE ACTOR MODEL

Fairness• Multiple threads waiting for- Synchronization locks- Notifications

• Java does not guarantee- To wake up threads dependent in waiting time- Not to starve threads out

© University of LiverpoolCOMP319 slide 12

Page 13: CONCURRENCY AND THE ACTOR MODEL

Summary• Thread shared models are prone to-Deadlocks- Unfairness-Data corruption issues- Hard to test code (race conditions)

• Notice this is- Inherent in the model-Must be dealt with by careful design decisions© University of LiverpoolCOMP319 slide 13

Page 14: CONCURRENCY AND THE ACTOR MODEL

Current Trends• Powerful multi-core architecture• Large amounts of memory• High speed networks• Easy to support-Many threads- Fast message

• Hard to design- Complex concurrent systems

• Hardware is powerful/software is complex

© University of LiverpoolCOMP319 slide 14

Page 15: CONCURRENCY AND THE ACTOR MODEL

Actor model• Much simplified approach to

concurrency• Each actor- Receives messages in an inbox- Sends messages to other Actors

• The actor contains state BUT- State is not shared between actors-Messages sent are immutable and owned by the receiver

© University of LiverpoolCOMP319 slide 15

Page 16: CONCURRENCY AND THE ACTOR MODEL

Actor model• Each Actor has an independent

thread• Each Actor’s thread processes

messages out of the inbox• There is no need to share data and

therefore no need for deadlock• The Actor can- Create new Actors- Send message to other Actors

© University of LiverpoolCOMP319 slide 16

Page 17: CONCURRENCY AND THE ACTOR MODEL

Actors and Networks• Actors don’t have to on the same

system• This helps with scaling/redundancy

etc.

© University of LiverpoolCOMP319 slide 17

InternetActo

rActorActo

r

Actor

Actor

Page 18: CONCURRENCY AND THE ACTOR MODEL

Java and state sharing• If objects are not shared then call

by reference is not allowed• Message myMess=new Message(“Hello!”);• myActor.sendMessage(myMess); // shared

state

• // Replace with call by value..• Message myMess=new Message(“Hello!”);myActor.sendMessage(myMess.clone()); // N.B. clone is garbage collected after call terminates

© University of LiverpoolCOMP319 slide 18

Page 19: CONCURRENCY AND THE ACTOR MODEL

Safe Messaging• Some Java Actor frameworks allow- Access to other Actors mailboxes-Memory sharing via messages

• Solutions- Cloning- Immutable objects (e.g. Strings)- Linear types- pointerA=pointerB (pointerB now

invalid)© University of LiverpoolCOMP319 slide 19

Page 20: CONCURRENCY AND THE ACTOR MODEL

Messaging handling issues• Zero copy- Pass by reference- Very fast- Keep object as immutable- Use scalar types- Not useful with remote actors

• Full copy (deep copy)- Can be very slow (often the bottle neck for the application)

© University of LiverpoolCOMP319 slide 20

Page 21: CONCURRENCY AND THE ACTOR MODEL

Message handling• All messages are sent

asynchronously- Non-blocking mode

• Message do not have to be buffered

• Messages do not have to arrive in order they were sent (datagram support)

• Addressing- Actor’s address (mailing address)- Addresses can come from- Message received- Know since this Actor created the

Actor

© University of LiverpoolCOMP319 slide 21

Page 22: CONCURRENCY AND THE ACTOR MODEL

Scheduling and fair-scheduling• The choice of which actor gets to execute next

and for how long is done by a part of the system called the scheduler

• An actor is non-blocked if it is processing a message or if its mailbox is not empty, otherwise the actor is blocked

• A scheduler is fair if it does not starve a non-blocked actor, i.e. all nonblocked actors eventually execute

• Fair scheduling makes it easier to reason about programs and program composition

• Otherwise some correct program (in isolation) may never get processing time when composed with other programs

© University of LiverpoolCOMP319 slide 22

Page 23: CONCURRENCY AND THE ACTOR MODEL

Message fairness• All messages are assumed to be

delivered• All messages are assumed to be

processed (eventually)• This is to help avoid deadlock and

starvation

© University of LiverpoolCOMP319 slide 23

Page 24: CONCURRENCY AND THE ACTOR MODEL

Location Transparency • Addressing is the same wherever - The sender is (local or remote)- The receiver is (local or remote)

• Location Transparency examples-Mobile MISDN-Object Request Broker (CORBA)

• Useful technology- Brokers, Anycasting and multicasting

© University of LiverpoolCOMP319 slide 24

Page 25: CONCURRENCY AND THE ACTOR MODEL

Object Request Broker

© University of LiverpoolCOMP319 slide 25

Page 26: CONCURRENCY AND THE ACTOR MODEL

Locality of reference• Bring Actors closer together- Possibly on the same machine

• Why- Cut down bandwidth, failures and latency (delays)

• How- Process migration- Pick closer Actors first

• Contradicts?- Location transparancy

© University of LiverpoolCOMP319 slide 26

Page 27: CONCURRENCY AND THE ACTOR MODEL

Transparent Migration• Movement of Actors• Features- Capture state- Creation of new Actor- Deletion of old Actor- Restoring state- Handling addressing

• All this should be transparent to- All application code

© University of LiverpoolCOMP319 slide 27

Page 28: CONCURRENCY AND THE ACTOR MODEL

Mobility• Strong or Weak (Fuggetta et al)• Strong-Movement of code + execution state

• Weak-Movement of code + optional initialized state- Can be used to move idle actor

© University of LiverpoolCOMP319 slide 28

Page 29: CONCURRENCY AND THE ACTOR MODEL

Mobility uses• Locality of reference-Moving Actors closer

• Redundancy- Fail over to other hardware

• Load balancing-Moving Actor to less loaded component

• Re-configuration-Moving to new hardware-Moving to mobile client

© University of LiverpoolCOMP319 slide 29

Page 30: CONCURRENCY AND THE ACTOR MODEL

Synchronization and Actors• Actor sends messages, with return

address• Senders-Monitors for mailbox for reply

• Standard service in- Scala- Actor Foundry-many more..

© University of LiverpoolCOMP319 slide 30

Page 31: CONCURRENCY AND THE ACTOR MODEL

Synchronisation constraints• Examples- Buffer that fills up- Service is offline at certain hours

• Solution-Defer the processing of the message- Store new message in saved message queue- Every time new message received, checked saved queue

© University of LiverpoolCOMP319 slide 31

Page 32: CONCURRENCY AND THE ACTOR MODEL

Synchronisation constraints• Scalar Example@Disable(messageName = "put")public Boolean disablePut(Integer x) {

if (bufferReady) { return (tail == bufferSize);

}else {

return true;}

}

© University of LiverpoolCOMP319 slide 32

Page 33: CONCURRENCY AND THE ACTOR MODEL

Actor languages and frameworks• Actor foundry- Safe (by-copy) as well as efficient (zero-copy) messaging -Message ordering using Local Synchronization Constraints - Pattern-matching (Multiple Dispatch) - Fair scheduling -Mobility - Location independence

© University of LiverpoolCOMP319 slide 33

Page 34: CONCURRENCY AND THE ACTOR MODEL

Erlang• Ericsson in house language• Functional approach• Very high performance• Hot swap module handling• Message passing• Location independence • No classes, no OO

© University of LiverpoolCOMP319 slide 34

Page 35: CONCURRENCY AND THE ACTOR MODEL

Erlang and variables and FP• Not really variables- A=5- if A is unbounded 5 assigned to A

- B=A- Ok

- A=10- ERROR A is already bound to 5

- A=5- Just returns 5

© University of LiverpoolCOMP319 slide 35

Page 36: CONCURRENCY AND THE ACTOR MODEL

Referential transparency• Every time you call a function with

the same arguments- You get the same return value

• Feature of Erlang and other FP languages

• Gives higher levels of code stability due to state not being used in functions

© University of LiverpoolCOMP319 slide 36

Page 37: CONCURRENCY AND THE ACTOR MODEL

Tail recursion• Allows you to call a recursively call

a method without pushing anything on to the stack

function a(state) {

a(new_state); // does not let // stack grow, like a

loop}

© University of LiverpoolCOMP319 slide 37

Page 38: CONCURRENCY AND THE ACTOR MODEL

Scala• Function + OOP• Integrated with Java JDK- Call any JDK method from scala

• Full support for Actor programming• Highly scalable• Dynamic Typing• Lots of inference- Example var message=“Hello”- No need for String type definition

© University of LiverpoolCOMP319 slide 38

Page 39: CONCURRENCY AND THE ACTOR MODEL

Scala Hello Worldobject HelloWorld { def main(args: Array[String]): Unit= { var hello="Hello World..."; val myValue="Freddy"; println(hello+myValue); }}No need for static, object defines singleton class instance© University of LiverpoolCOMP319 slide 39

Page 40: CONCURRENCY AND THE ACTOR MODEL

Scala standard class definitionclass Person(val surname: String,val forename: String) { var weight=0f; // weight of person

def fn: String = forename + surnamedef setWeight(w : Float) : Unit = { this.weight=w;}def getWeight(metric: Boolean) : Double = { if (metric) { weight // returns value } else { var imperial=weight*2.2 imperial // returns value }}

} © University of LiverpoolCOMP319 slide 40

Page 41: CONCURRENCY AND THE ACTOR MODEL

Extra constructorsdef this(surname: String ) = {

this(surname,null) this}

© University of LiverpoolCOMP319 slide 41

Page 42: CONCURRENCY AND THE ACTOR MODEL

Actor exampleclass EmailMessage(val message: String) {

}

© University of LiverpoolCOMP319 slide 42

Page 43: CONCURRENCY AND THE ACTOR MODEL

Actor Exampleimport scala.actors.Actorimport scala.actors.Actor._

class EmailClient(val username: String) extends Actor { def act() { while (true) { receive { case incoming: EmailMessage => println("Got message for " + username + "message is " + incoming.message); } }

}}

© University of LiverpoolCOMP319 slide 43

Page 44: CONCURRENCY AND THE ACTOR MODEL

Actor Exampleimport scala.actors.Actorclass Mailbox(userclient: Actor) extends Actor { def act() { // actor thread val a=1; val b=1; var incoming=new EmailMessage("Hello"); while (true) { receive { case incoming : EmailMessage => println("Got message now sending"+incoming.message); userclient ! incoming } } }}

© University of LiverpoolCOMP319 slide 44

Page 45: CONCURRENCY AND THE ACTOR MODEL

Actor Exampleobject Main {

def main(args: Array[String]) : Unit = { val client=new EmailClient("Seb"); val mb=new Mailbox(client); mb.start(); client.start(); mb ! new EmailMessage("Hello how are

you?");

}

} © University of LiverpoolCOMP319 slide 45

Page 46: CONCURRENCY AND THE ACTOR MODEL

Summary• Actors can- Remove dead lock- Improve- Scalability, redundancy

- Allow for full mobility of code- Be local or remote- Create new actors

© University of LiverpoolCOMP319 slide 46