the actor model - towards better concurrency
DESCRIPTION
The actor concurrency model including examples in Erlang, Java, Scala and GroovyTRANSCRIPT
![Page 1: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/1.jpg)
The Actor ModelTowards Better Concurrency
By: Dror Bereznitsky
1
![Page 2: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/2.jpg)
2
Warning: Code Examples
![Page 3: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/3.jpg)
3
» The end of Moore law?» Shared state concurrency» Message passing concurrency» Actors on the JVM» More concurrency models» Summary
Agenda
Agenda
![Page 4: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/4.jpg)
The End of Moore Law ?
![Page 5: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/5.jpg)
5
» The number of transistors on a chip will double approximately every 18 months
» Gordon E. Moore, 1965
Moore's law
The End of Moore Law ?
![Page 6: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/6.jpg)
6
» No matter how fast processors get, software finds new ways to eat up the extra speed
Andy Giveth, and Bill Taketh away
The End of Moore Law ?
![Page 7: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/7.jpg)
7
» Free and regular performance gains, even without releasing new versions or doing anything special
The Free Lunch
The End of Moore Law ?
![Page 8: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/8.jpg)
8
The End of Moore Law ?
The End of Moore Law ?
![Page 9: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/9.jpg)
9
» Hardware crossed a boundary in the early 2000s: chips got big enough, cycle speed got fast enough a signal can no longer reach the whole chip in a clock cycle problems with heat dissipation
Why You Don’t Have 10GHz Today
The End of Moore Law ?
9
![Page 10: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/10.jpg)
10
» Processor manufacturers have turned towards multi-core processors
» Capable of doing multiple calculations in parallel » CPU speeds are likely to stay relatively flat in the near
future
The Multicore Era
The End of Moore Law ?
10
![Page 11: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/11.jpg)
11
» The performance lunch isn’t free any more » Want to benefit from the continued throughput
advances in new processors?» You will need to develop well-written concurrent
applications
The Concurrency Revolution
The End of Moore Law ?
11
![Page 12: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/12.jpg)
12
Amdahl’s Law
The End of Moore Law ?
12
GParallelizer
![Page 13: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/13.jpg)
Shared State Concurrency
13
![Page 14: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/14.jpg)
14
» Shared mutable state » Locking mechanism
Shared State Concurrency
Shared State Concurrency
![Page 15: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/15.jpg)
15
» Threads concurrently execute code sections Contains resources that must be shared Synchronized in order to guarantee • Correct ordering• Visibility• Data consistency
Threads
Shared State Concurrency
![Page 16: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/16.jpg)
16
The Popular Choice
Shared State Concurrency
C/C++
C#
![Page 17: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/17.jpg)
17
Why Threads are Evil
Shared State Concurrency
http://www.flickr.com/photos/amagill/235453953/
![Page 18: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/18.jpg)
18
“Non-trivial multi-threaded programs are incomprehensible to human …”
Edward A. Lee, The Problem with Threads
Not Convinced Yet ?
Shared State Concurrency
![Page 19: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/19.jpg)
19
» Message Passing Concurrency (Actors)» Software Transactional Memory» Dataflow Concurrency
Alternative Concurrency Models
Shared State Concurrency
![Page 20: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/20.jpg)
Message Passing Concurrency
![Page 21: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/21.jpg)
21
» 1973, paper by Carl Hewitt» Avoid the problems caused by threading and locking » Implemented in Erlang, Oz, Occam
Message Passing concurrency
Message Passing concurrency
![Page 22: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/22.jpg)
22
» Actors instead of objects» No shared state between actors» Asynchronous message-passing » Mailboxes to buffer incoming messages
Key Principals
Message Passing Concurrency
![Page 23: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/23.jpg)
23
» React to received messages by executing a behavior function can only change the state of the actor itself can send messages to other actors
» Actors never share state and thus never need to compete for locks for access to shared data
Actors
Message Passing Concurrency
![Page 24: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/24.jpg)
24
» Actors exchange data by sending immutable messages » Messages are sent asynchronously» Actors do not block waiting for responses to their
messages
Messages
Message Passing Concurrency
![Page 25: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/25.jpg)
25
» Messages buffered in an actor's mailbox » A mailbox is a queue with multiple
producers and a single consumer» Also known a channel
Mailbox
Message Passing Concurrency
![Page 26: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/26.jpg)
26
» A pure functional, dynamically typed language invented in 1986 at Ericsson
» Designed for concurrency, distribution and scalability » Actors are part of the language » No Mutable state
Erlang
Message Passing Concurrency
![Page 27: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/27.jpg)
27
loop() ->receive
hello ->io:format("Hello, World!~n"),loop();
goodbye ->ok
end.
Hello World in Erlang
Message Passing Concurrency
![Page 28: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/28.jpg)
Actors on the JVM
![Page 29: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/29.jpg)
29
» JVM languages actors implementations available for Java Scala Groovy Fantom
Actors on the JVM
Actors on the JVM
![Page 30: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/30.jpg)
30
» Actor’s Guild» Akka» ActorFoundry» Actorom» Functional Java» Kilim» Jetlang
Java Actor Libraries
Java Actors
![Page 31: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/31.jpg)
31
» Experimental Java framework make concurrent programming easier
» Annotation based approach» Messages handlers are implemented as Java methods» Uses a request/response pattern for messages
Not a pure Actors implementation» No special pre-processing
Actors Guild
Java Actors
![Page 32: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/32.jpg)
32
public abstract class HelloActor extends Actor { @Prop public abstract String getName();
@Message public AsyncResult<String> sayHello(String name) { return result(
String.format("Hello %s, my name is %s", name, getName())););
}}
Actors Guild Hello World: Actor Implementation
Java Actors
![Page 33: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/33.jpg)
33
Agent agent = new DefaultAgent();
HelloActor hello = agent.create(HelloActor.class,
new Props("name", "Hamlet"));
AsyncResult<String> asyncResult = hello.sayHello("Claudius");
System.out.println(asyncResult.get());
agent.shutdown();
Actors Guild Hello World: Actor Usage
Java Actors
![Page 34: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/34.jpg)
34
@Message@Usage(ThreadUsage.IO)public AsyncResult<Void> writeFile(...) throws Exception {
FileOutputStream fos = new FileOutputStream(name);fos.write(content.getBytes());fos.close();return noResult();
}
@Message@Usage(ThreadUsage.Waiting)public AsyncResult<Void> waitForKeyPrompt() throws Exception {
System.in.read();return noResult();
}
Thread Usage Pattern
Java Actors
![Page 35: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/35.jpg)
35
@Model(ConcurrencyModel.Stateless)class MultiplicatorActor extends Actor { @Message public AsyncResult<Integer> mul(int a, int b) { return result(a * b); } }
Concurrency Model Annotations
Java Actors
![Page 36: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/36.jpg)
36
Scala Actors
Scala Actors
Scala actors combine the powers of functional programming along with the flexible type-system of Scala
![Page 37: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/37.jpg)
37
» Encourages shared-nothing process abstractions mutable state - private shared state - immutable
» Asynchronous message passing» Pattern matching » Fork/Join as the underlying implementation
Share Nothing, Asynch Message Passing
Scala Actors
![Page 38: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/38.jpg)
38
» Thread-based actors - each run in its own JVM thread Overhead in case of large amount of actors full stack frame suspension (receive)
» Event-based actors run on the same thread Use a react block instead of a receive block
Thread-based vs. Event-based Actors
Scala Actors
![Page 39: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/39.jpg)
39
» Lightweight event objects» Executed on an underlying worker thread pool
automatically resized when all threads block on long running operations
» Suspension mechanism based on a continuation closure (react)
Lightweight Event Based Actors
Scala Actors
![Page 40: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/40.jpg)
40
case class Name(name: String) case class Greet();
object HelloWorld extends Actor { def act = { var myName = "Incognito" loop { react { case Name(name) => myName = name case Greet => reply("Hello, my name is " + myName); exit } } } }
The Actor and Messages
Scala Actors
![Page 41: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/41.jpg)
41
object Greetings extends Application { HelloWorld.start
HelloWorld ! Name("foo")
HelloWorld !? Greet match { case result: String => println(result); }}
Interacting with the Actor
Scala Actors
![Page 42: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/42.jpg)
42
Groovy Actors
![Page 43: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/43.jpg)
43
» GPars = Groovy Parallel Systems Formerly known as GParallelizer
» Handle tasks concurrently, asynchronously, and distributed Concurrent collection processing Actor programming model Dataflow concurrency constructs Safe - an mt-safe reference to mutable state
GPars
Groovy Actors
![Page 44: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/44.jpg)
44
» Inspired by the Actors library in Scala» Event driven actors
concurrent actors that share a single pooled thread Using fork/join under the hood
» Supports distributed actors
Gpars Actors
Groovy Actors
![Page 45: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/45.jpg)
45
class HelloWorld extends AbstractPooledActor { String name
void act() { loop { react { switch (it) { case 'hello': println "Hello, my name is $name";
break } } } }}
GPars Actors Hello World: Actor Implementation
Groovy Actors
![Page 46: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/46.jpg)
46
def actor = new HelloWorld(name : "Charlie").start();actor.send('hello')
GPars Actors Hello World: Actor Usage
Groovy Actors
![Page 47: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/47.jpg)
Actors in the Real World
![Page 48: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/48.jpg)
48
» Ericson AXD 301 switch millions of calls per ,99.9999999 percent uptime
» Facebook chat application 70 million concurrent users
» RabbitMQ high-performance AMQP, 400,000 messages per second.
» Apache CouchDB distributed, fault-tolerant document-oriented database
» Ejabberd XMPP server – jabber.org
Erlang Actors in the Real World
Actors in the Real World
![Page 49: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/49.jpg)
49
» Easier to reason about» Higher abstraction level» Easier to avoid
Race conditions Deadlocks Starvation Live locks
» Distributed computing
Actor Benefits
Actors in the Real World
![Page 50: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/50.jpg)
50
» Actors don’t work well when Shared state is needed F.e. bank account Need to achieve global consensus Synchronous behavior is required
» It is not always trivial to break the problem into smaller problems
Problems with Actors
Actors in the Real World
![Page 51: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/51.jpg)
More Concurrency Models
![Page 52: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/52.jpg)
52
» 1995 Nir Shavit and Dan Touitou» Memory as a transactional data set» Alternative to lock based synchronization» Similar to database transactions» Optimistic Approach
a thread completes modifications to shared memory without regard for what other threads might be doing
Software Transactional Memory
More Concurrency Models
![Page 53: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/53.jpg)
53
» Java STM framework by Guy Korland (TAU, GigaSpaces) @Atomic methods Field based access
• More scalable than Object bases.• More efficient than word based.
No reserved words• No need for new compilers (Existing IDEs can be used)
Using bytecode instrumentation (ASM)
Deuce
More Concurrency Models
![Page 54: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/54.jpg)
54
public class Bank{private double commission = 0;
@Atomic(retries = 64)public void transaction( Account ac1, Account ac2, double
amount){ac1.balance -= (amount + commission);ac2.balance += amount;
}
@Atomicpublic void update( double value){ commission += value;}
}
Duece Example
More Concurrency Models
![Page 55: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/55.jpg)
55
public void update( double value){ Context context = ContextDelegetor.getContext(); for( int i = retries ; i > 0 ; --i){ context.init(); try{ update( value, context); if(context.commit()) return; }catch ( TransactionException e ){ context.rollback(); continue; }catch ( Throwable t ){ if( context.commit()) throw t; } } throw new TransactionException();}
Deuce Example Under the Hood
More Concurrency Models
![Page 56: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/56.jpg)
56
» Pros Performance gains on multi processor machines Simple approach to concurrency No deadlocks or livelocks
» Cons Transactions cannot perform any operation that cannot be
undone
STM Pros and Cons
More Concurrency Models
![Page 57: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/57.jpg)
Summary
![Page 58: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/58.jpg)
58
» The free lunch is over» Applications must be built for concurrency» Shared state concurrency is hard» Alternative concurrency models
Message passing concurrency Shared Transactional Memory
Summary
Summary
![Page 59: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/59.jpg)
59
» The free lunch is over» State: You're Doing It Wrong» Edward A. Lee, The Problem with Threads (PDF) » Alex Miller – Actor Concurrency» Deuce STM
Resources
Summary
![Page 60: The Actor Model - Towards Better Concurrency](https://reader033.vdocuments.net/reader033/viewer/2022061218/54b7a95c4a795913288b45d3/html5/thumbnails/60.jpg)
Thank You :-)