+ All Categories
Home > Documents > Concurrency and Erlang LCA2007 Andrep

Concurrency and Erlang LCA2007 Andrep

Date post: 31-May-2018
Category:
Upload: mberrow
View: 224 times
Download: 0 times
Share this document with a friend
46
André Pang 1
Transcript
Page 1: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 1/46

André Pang

1

Page 2: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 2/46

2

Concurrency = Threads, for most of you. So, what’s so hard about threads?

Page 3: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 3/46

mutate_variable();

pthread_mutex_lock(mutex);

pthread_mutex_unlock(mutex);

3

Lock, mutate/access, unlock, on every access to the variable. Looks simple, right?

Page 4: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 4/46

4

Well, let’s hear some war stories from our own community that may indicate that concurrencyisn’t quite so easy… (2 minutes)

Page 5: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 5/46

“A computer is astate machine.

Threads are for 

people who can’tprogram state

machines.”

 — Alan Cox

5

Page 6: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 6/46

6

Andrew Tridgell: Software Engineering Keynote at Linux.conf.au 2005. In the context of techniques used in Samba 4 to handle multiple concurrent client connections.

Page 7: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 7/46

7

Page 8: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 8/46

8

Page 9: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 9/46

9

And this is why Alan Cox’s quip about state machines is, well, slightly incorrect. Statemachines really do send you mad once you have to handle a metric boatload of states.

Page 10: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 10/46

10

Similar to Apache: ofoad the choice to the user. Why does a user have to choose betweenapache-mpm-event, -itk, -perchild, -threadpool, and -worker threading models? Mainpoint: Tridge is unhappy with all these models.

Page 11: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 11/46

“… I recall how I took a lock on a data structure that when the system scaled up in size lasted 100

milliseconds too long, which caused backups inqueues throughout the system and deadly cascade

of drops and message retries…”

11

Page 12: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 12/46

Page 13: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 13/46

“… These same issues came up over and over 

again. Deadlock. Corruption. Priority inversion.Performance problems. Impossibility of new people

 to understand how the system worked…”

13

Page 14: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 14/46

“After a long and careful analysis the results are

clear: 11 out of 10 people can't handle threads.”

 — Todd Hoff,The Solution to C++ Threading is Erlang 

14

In light of how hard (shared state) concurrency is to do right, do we need concurrency at all?(6 minutes)

Page 15: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 15/46

15

Reason 1: Performance, scalability. Servers already have 32 cores. Much bigger challenge towrite server code that can scale well to this size. (Apache? Samba?)

Page 16: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 16/46

16

Page 17: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 17/46

0

8

16

24

32

  2  0  0  6

  2  0  0  7

  2  0  0  8

  2  0  0  9

  2  0  1  0

  2  0  1  1

  2  0  1  2

  2  0  1  3

Processor Cores (source: Herb Sutter—”Software and the Concurrency Revolution”)

17

Reason 2: You’ll be required to. Desktops already have 2 cores. Multithreading notimportant for a lot of applications, but some apps really benefit from them (Photoshop &GIMP!)

Page 18: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 18/46

18

Let’s talk about an industry that has had to face these problems in the past few years: games!

Page 19: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 19/46

Page 20: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 20/46

20

3 Xenon CPUs: PowerPC, 3.2GHz.

Page 21: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 21/46

21

Playstation 3: 1 main PowerPC core @ 3GHz, 6 “Synergistic Processing Elements” at 3GHz.

Page 22: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 22/46

22

NVIDIA GeForce 8800GTX: 128 stream processors @ 1.3GHz, ~520GFlops.

Page 23: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 23/46

23

NVIDIA GeForce 8800GTX: 128 stream processors @ 1.3GHz, ~520GFlops.

Page 24: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 24/46

“If you want to utilizeall of that unusedperformance, it’sgoing to become

more of a risk to youand bring pain and

suffering to theprogramming side.”

 — John Carmack 

24

So what do games programmers think about concurrency? Do they find it easy? Apparentlynot… (12 minutes).

Page 25: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 25/46

25

Tim Sweeney: lead programmer & designer, Unreal Tournament (from the original to 2007).Best game engine architect and designer, bar none. Unreal Engine 3 to be sustainable to2010 (16 cores). 50+ games using UE series. Used in FPSs, RTSs, MMORPGs…

Page 26: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 26/46

26

Page 27: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 27/46

27

Arguably the best games architect and designer in the world is calling shared-stateconcurrency intractable. How on Earth are the rest of us puny humans meant to cope?

Page 28: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 28/46

“Surely the most

powerful strokefor softwareproductivity,reliability, and

simplicity has been the progressiveuse of high-level

languages for 

programming.”

 — Fred P. Brooks

28

Perhaps a better programming paradigm can help with this?

Page 29: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 29/46

29

Erlang: a programming language developed at Ericsson for use in their bigtelecommunications switches. Named after A. K. Erlang, queue theory mathematician. (16minutes).

Page 30: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 30/46

hello() -> io:format( "hello, world!~n" ).

hello( Name ) -> io:format( "hello, ~s!~n", [ Name ] ).

30

Variable names start with capital letters. Variable names are single-assignment (const).

Page 31: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 31/46

-module( hello_concurrent ).

-export( [ receiver/0, giver/1, start/0 ] ).

receiver() ->receivediediedie -> ok;{ name, Name } -> io:format( "hello, ~s~n", [ Name ] ), receiver()

end.

giver( ReceiverPid ) ->

ReceiverPid ! { name, "Andre" },ReceiverPid ! { name, "Linux.conf.au" },ReceiverPid ! diediedie.

start() ->ReceiverPid = spawn( hello_concurrent, receiver, [] ),spawn( hello_concurrent, giver, [ ReceiverPid ] ),start_finished.

31

Tuples, spawn used to start new threads, ! used to send messages, and receive used toreceive messages. No locking, no mutexes, no shared state at all…

Page 32: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 32/46

  K  B 

  p  e  r  S

  e  c  o  n  d

Number of Concurrent Connections

Apache (Local) Apache (NFS) YAWS (NFS)

32

And how is Erlang’s performancece? Apache dies at 4,000 connections. YAWS? 80000+…(and that’s one Erlang process per client connection!)

Page 33: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 33/46

kernel space

user space

Erlang

VM

33

Userland (green) threads. Cooperative scheduler — but safe, because Erlang VM is in fullcontrol. Erlang R11B uses multiple kernel threads for I/O and SMP eciency. No kernelthreads means no context switching means very very fast threading.

Page 34: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 34/46

34

Flip our problem on its head: what can you do if threads are really easy, instead of beingreally hard?

Page 35: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 35/46

Page 36: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 36/46

36

Erlang has good tools required by industry, since it’s used in industry as well as academia.e.g. An awesome Crashdump Viewer (or as Conrad Parker would say, Crapdump Viewer).

Page 37: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 37/46

erl -rsh /usr/bin/ssh -remsh erlang_node@hostname

1> code:purge(module_name).

2> code:load_file(module_name).

37

How to do hot-code-reloading: two lines of Erlang! Existing modules will keep running untilthey’re no longer used, all managed by the Erlang VM.

Page 38: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 38/46

-record( passwd, { username, password } ).

mnesia:create_schema( [ node() ] ),

mnesia:start(),

mnesia:create_table( passwd, [] ),

NewUser = #passwd{ username=“andrep”, password=”foobar” },

F = fun() -> mnesia:write( passwd, NewUser ) end,mnesia:transaction( F ).

38

Mnesia is Erlang’s insanely great distributed database. Incredibly simple to use! No dataimpedence mismatch. Store tuples, lists, any Erlang object: none of this SQL row/columnnonsense. Query language is just list comprehensions. Transactions are functions!

Page 39: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 39/46

39

Mnesia is replicating. Add new node clusters on-the-fly. Nodes can go down and come backup, and Mnesia will resync the database information to them automatically. Withprogrammer help, it can even recover from network partitioning.

Page 40: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 40/46

40

Erlang gives you a complete framework for writing massive, robust, scalable applications.Callback functions. OO analogy. OTP drives the application: you supply the “business logic”as callbacks. This means that Erlang is a self-healing, self-sustaining system, and is the mainreason why Erlang applications are so robust. (30 minutes)

Page 41: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 41/46

41

AXD301 telephone switch. One to two million lines of Erlang code. Downtime of maybe afew minutes per year, continuous operation over years. On-the-fly upgrades. Mnesia usedfor _soft-real-time_ network routing lookup. Mnesia is just 30,000 lines of code. Impressedyet?

Page 42: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 42/46

42

5,000-10,000 clients + >800 other Jabber servers all connected to one single machine. Loadaverage is rather low. Also doesn’t crash, unlike jabberd2!

Page 43: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 43/46

mutate_variable();

pthread_mutex_lock(mutex);

pthread_mutex_unlock(mutex);

43

Shared state concurrency just doesn’t scale well, is hard to get right (especially if performance is needed: what granularity of locks do you use?). Race conditions, deadlocks,livelocks, no compiler help. Just say no!

Page 44: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 44/46

44

Prefer the messaging (actor) model: use it in your own language! You can do it in C, C++,Python, Java, or whatever your other language is. You may have to write some infrastructurecode, but by God it’ll be easier in the end!

Page 45: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 45/46

45

Page 46: Concurrency and Erlang LCA2007 Andrep

8/14/2019 Concurrency and Erlang LCA2007 Andrep

http://slidepdf.com/reader/full/concurrency-and-erlang-lca2007-andrep 46/46

46


Recommended