The Actor Model. Towards Better Concurrency. By: Dror Bereznitsky

Similar documents
Concurrency. Unleash your processor(s) Václav Pech

Executive Summary. It is important for a Java Programmer to understand the power and limitations of concurrent programming in Java using threads.

Akka: Simpler Concurrency, Scalability & Fault-tolerance through Actors. Jonas Bonér Viktor Klang

CS Programming Languages: Scala

Scala Actors. Scalable Multithreading on the JVM. Philipp Haller. Ph.D. candidate Programming Methods Lab EPFL, Lausanne, Switzerland

THREADS & CONCURRENCY

Tackling Concurrency With STM. Mark Volkmann 10/22/09

Tackling Concurrency With STM

A Survey of Concurrency Constructs. Ted Leung Sun

Mohamed M. Saad & Binoy Ravindran

Scheduling Transactions in Replicated Distributed Transactional Memory

Advances in Programming Languages

Actors in the Small. Making Actors more Useful. Bill La

Programming Paradigms

Principles of Software Construction: Objects, Design, and Concurrency. The Perils of Concurrency Can't live with it. Cant live without it.

Concurrency: Past and Present

Parallelism Marco Serafini

Concurrency: what, why, how

Concurrency: what, why, how

Introduction to Concurrency Principles of Concurrent System Design

Multithreading and Interactive Programs

CSE Traditional Operating Systems deal with typical system software designed to be:

Parallelism. CS6787 Lecture 8 Fall 2017

General Overview of Mozart/Oz

Message Passing. Advanced Operating Systems Tutorial 7

Concurrency & Parallelism. Threads, Concurrency, and Parallelism. Multicore Processors 11/7/17

IGL S AND MOUNT NS. Taking AKKA to Production

Persistent Data Structures and Managed References

Even coarse architectural trends impact tremendously the design of systems

Synchronization in Concurrent Programming. Amit Gupta

Advanced MEIC. (Lesson #18)

Monitors; Software Transactional Memory

CScheme in Traditional Concurrency Problems

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 08 09/17/2015

Multithreading and Interactive Programs

JAVA CONCURRENCY FRAMEWORK. Kaushik Kanetkar

Erlang and Concurrency. André Pang Rising Sun Research

CS 31: Introduction to Computer Systems : Threads & Synchronization April 16-18, 2019

Threads & Concurrency

Threads & Concurrency

ECE 587 Hardware/Software Co-Design Lecture 07 Concurrency in Practice Shared Memory I

Introduction to Concurrent Software Systems. CSCI 5828: Foundations of Software Engineering Lecture 12 09/29/2016

Threads, Concurrency, and Parallelism

Concurrency in Object Oriented Programs 1. Object-Oriented Software Development COMP4001 CSE UNSW Sydney Lecturer: John Potter

Principles of Software Construction: Objects, Design, and Concurrency. The Perils of Concurrency Can't live with it. Can't live without it.

CMSC 132: Object-Oriented Programming II. Threads in Java

CSE 451: Operating Systems Winter Module 2 Architectural Support for Operating Systems

HiTune. Dataflow-Based Performance Analysis for Big Data Cloud

Even coarse architectural trends impact tremendously the design of systems

Problems with Concurrency. February 19, 2014

THREADS & CONCURRENCY

Designing for Scalability. Patrick Linskey EJB Team Lead BEA Systems

Even coarse architectural trends impact tremendously the design of systems. Even coarse architectural trends impact tremendously the design of systems

Agenda. Designing Transactional Memory Systems. Why not obstruction-free? Why lock-based?

Reminder from last time

Spot: A Programming Language for Verified Flight Software

Monitors; Software Transactional Memory

Kernel Synchronization I. Changwoo Min

CONCURRENCY AND THE ACTOR MODEL

Akka Java Documentation

OPERATING SYSTEM. Chapter 4: Threads

CSE 230. Concurrency: STM. Slides due to: Kathleen Fisher, Simon Peyton Jones, Satnam Singh, Don Stewart

Multi-core Parallelization in Clojure - a Case Study

CS333 Intro to Operating Systems. Jonathan Walpole

CSE332: Data Abstractions Lecture 23: Programming with Locks and Critical Sections. Tyler Robison Summer 2010

A Sophomoric Introduction to Shared-Memory Parallelism and Concurrency Lecture 5 Programming with Locks and Critical Sections

Thread. Disclaimer: some slides are adopted from the book authors slides with permission 1

Virtual Machine Design

THREADS AND CONCURRENCY

Principles of Software Construction: Objects, Design, and Concurrency. Concurrency: More Design Tradeoffs School of Computer Science

Advanced concurrent programming in Java Shared objects

Threads Questions Important Questions

Unit Testing in Java with an Emphasis on Concurrency Corky Cartwright Rice and Halmstad Universities Summer 2013

Synchronization. CSCI 5103 Operating Systems. Semaphore Usage. Bounded-Buffer Problem With Semaphores. Monitors Other Approaches

Akka: Simpler Concurrency, Scalability & Fault-tolerance through Actors. Jonas Bonér Scalable Solutions

Clojure Lisp for the Real clojure.com

Today. Instance Method Dispatch. Instance Method Dispatch. Instance Method Dispatch 11/29/11. today. last time

Introduction Basics Concurrency Conclusion. Clojure. Marcel Klinzing. December 13, M. Klinzing Clojure 1/18

Stuart

Chapter 4: Threads. Chapter 4: Threads. Overview Multicore Programming Multithreading Models Thread Libraries Implicit Threading Threading Issues

Favoring Isolated Mutability The Actor Model of Concurrency. CSCI 5828: Foundations of Software Engineering Lecture 24 04/11/2012

Dealing with Issues for Interprocess Communication

Seminar on Languages for Scientific Computing Aachen, 6 Feb Navid Abbaszadeh.

Programming Safe Agents in Blueprint. Alex Muscar University of Craiova

Manchester University Transactions for Scala

CPL 2016, week 10. Clojure functional core. Oleg Batrashev. April 11, Institute of Computer Science, Tartu, Estonia

Atelier Java - J2. Marwan Burelle. EPITA Première Année Cycle Ingénieur.

Identity, State and Values

Exercise (could be a quiz) Solution. Concurrent Programming. Roadmap. Tevfik Koşar. CSE 421/521 - Operating Systems Fall Lecture - IV Threads

Parallelization and Synchronization. CS165 Section 8

CS377P Programming for Performance Multicore Performance Multithreading

Concurrent Programming

Concept of a process

Programmazione di sistemi multicore

A Scalable Lock Manager for Multicores

Cloud Programming James Larus Microsoft Research. July 13, 2010

Chapter 4: Threads. Chapter 4: Threads

Parallel design patterns ARCHER course. The actor pattern

Grand Central Dispatch and NSOperation. CSCI 5828: Foundations of Software Engineering Lecture 28 12/03/2015

CS 351 Design of Large Programs Threads and Concurrency

Transcription:

The Actor Model Towards Better Concurrency By: Dror Bereznitsky 1

Warning: Code Examples 2

Agenda Agenda The end of Moore law? Shared state concurrency Message passing concurrency Actors on the JVM More concurrency models Summary 3

The End of Moore Law?

The End of Moore Law? Moore's law The number of transistors on a chip will double approximately every 18 months Gordon E. Moore, 1965 5

The End of Moore Law? Andy Giveth, and Bill Taketh away No matter how fast processors get, software finds new ways to eat up the extra speed 6

The End of Moore Law? The Free Lunch Free and regular performance gains, even without releasing new versions or doing anything special 7

The End of Moore Law? The End of Moore Law? 8

The End of Moore Law? Why You Don t Have 10GHz Today 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 9 9

The End of Moore Law? The Multicore Era 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 10 10

The End of Moore Law? The Concurrency Revolution 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 11 11

The End of Moore Law? Amdahl s Law GParallelizer 12 12

Shared State Concurrency 13

Shared State Concurrency Shared State Concurrency Shared mutable state Locking mechanism 14

Shared State Concurrency Threads Threads concurrently execute code sections Contains resources that must be shared Synchronized in order to guarantee Correct ordering Visibility Data consistency 15

Shared State Concurrency The Popular Choice #C ++C/C 16

Shared State Concurrency Why Threads are Evil /http://www.flickr.com/photos/amagill/235453953 17

Shared State Concurrency Not Convinced Yet? Non-trivial multi-threaded programs are incomprehensible to human Edward A. Lee, The Problem with Threads 18

Shared State Concurrency Alternative Concurrency Models Message Passing Concurrency (Actors) Software Transactional Memory Dataflow Concurrency 19

Message Passing Concurrency

Message Passing concurrency Message Passing concurrency 1973, paper by Carl Hewitt Avoid the problems caused by threading and locking Implemented in Erlang, Oz, Occam 21

Message Passing Concurrency Key Principals Actors instead of objects No shared state between actors Asynchronous message-passing Mailboxes to buffer incoming messages 22

Message Passing Concurrency Actors 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 23

Message Passing Concurrency Messages Actors exchange data by sending immutable messages Messages are sent asynchronously Actors do not block waiting for responses to their messages 24

Message Passing Concurrency Mailbox Messages buffered in an actor's mailbox A mailbox is a queue with multiple producers and a single consumer Also known a channel 25

Message Passing Concurrency Erlang 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 26

Message Passing Concurrency Hello World in Erlang loop() -> receive hello -> io:format("hello, World!~n"), loop(); goodbye -> ok end. 27

Actors on the JVM

Actors on the JVM Actors on the JVM JVM languages actors implementations available for Java Scala Groovy Fantom 29

Java Actors Java Actor Libraries Actor s Guild Akka ActorFoundry Actorom Functional Java Kilim Jetlang 30

Java Actors Actors Guild 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 31

Java Actors Actors Guild Hello World: Actor Implementation 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()));); } } 32

Java Actors Actors Guild Hello World: Actor Usage 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(); 33

Java Actors Thread Usage Pattern @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(); } 34

Java Actors Concurrency Model Annotations @Model(ConcurrencyModel.Stateless) class MultiplicatorActor extends Actor { @Message public AsyncResult<Integer> mul(int a, int b) { return result(a * b); } } 35

Scala Actors Scala Actors Scala actors combine the powers of functional programming along with the flexible type-system of Scala 36

Scala Actors Share Nothing, Asynch Message Passing Encourages shared-nothing process abstractions mutable state - private shared state - immutable Asynchronous message passing Pattern matching Fork/Join as the underlying implementation 37

Scala Actors Thread-based vs. Event-based Actors 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 38

Scala Actors Lightweight Event Based Actors 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) 39

Scala Actors The Actor and Messages 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 } } } } 40

Scala Actors Interacting with the Actor object Greetings extends Application { HelloWorld.start HelloWorld! Name("foo") HelloWorld!? Greet match { case result: String => println(result); } } 41

Groovy Actors 42

Groovy Actors GPars 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 43

Groovy Actors Gpars Actors 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 44

Groovy Actors GPars Actors Hello World: Actor Implementation class HelloWorld extends AbstractPooledActor { String name void act() { loop { react { switch (it) { case 'hello': println "Hello, my name is $name"; break } } } } } 45

Groovy Actors GPars Actors Hello World: Actor Usage def actor = new HelloWorld(name : "Charlie").start(); actor.send('hello') 46

Actors in the Real World

Actors in the Real World Erlang Actors in the Real World 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 48

Actors in the Real World Actor Benefits Easier to reason about Higher abstraction level Easier to avoid Race conditions Deadlocks Starvation Live locks Distributed computing 49

Actors in the Real World Problems with Actors 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 50

More Concurrency Models

More Concurrency Models Software Transactional Memory 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 52

More Concurrency Models Deuce 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) 53

More Concurrency Models Duece Example 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; } @Atomic public void update( double value){ commission += value; } } 54

More Concurrency Models Deuce Example Under the Hood 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(); 55

More Concurrency Models STM Pros and Cons 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 56

Summary

Summary Summary 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 58

Summary Resources 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 59

Thank You :-)