erlang - concurrent language for concurrent world

82
$ whoami Name: Zvi Avraham Title: Founder & CEO Company: ZADATA Ltd E-mail: [email protected]

Upload: nivertech

Post on 13-Jan-2015

2.249 views

Category:

Technology


1 download

DESCRIPTION

Intro to Erlang talk from Erlang Factory Lite Tel-Aviv, October 23 2013

TRANSCRIPT

Page 1: Erlang - Concurrent Language for Concurrent World

$ whoami

Name: Zvi Avraham

Title: Founder & CEO

Company: ZADATA Ltd

E-mail: [email protected]

Page 2: Erlang - Concurrent Language for Concurrent World

Concurrent Language for Concurrent World

Page 3: Erlang - Concurrent Language for Concurrent World
Page 4: Erlang - Concurrent Language for Concurrent World

The world is concurrent… I could not drive the car, if I did not understood concurrency …

-- Joe Armstrong, father of Erlang

Page 5: Erlang - Concurrent Language for Concurrent World

Object-oriented

Programming

Page 6: Erlang - Concurrent Language for Concurrent World

Object-oriented

Programming

object object

object object

object

object

object object

object

object

object

object

object

object

object object

object

object

Page 7: Erlang - Concurrent Language for Concurrent World

Concurrency-oriented

Programming

Page 8: Erlang - Concurrent Language for Concurrent World

Concurrency-oriented

Programming

process process

process process

process

process

process process

process

process

process

process

process

process

process process

process

process

Page 9: Erlang - Concurrent Language for Concurrent World
Page 10: Erlang - Concurrent Language for Concurrent World

Imagine a programming language which allows only

1000 objects -- Joe Armstrong, father of Erlang

Page 11: Erlang - Concurrent Language for Concurrent World

COP vs OOP Concurrency-oriented vs Object-oriented

• 15 years ago OOP became mainstream

• COP became popular only recently

• Process is active object

– i.e. object with it’s own process of control

– Objects in OOP are “passive objects”

• Processes as cheap as hashtables:

– ~ 300 words (1.2KB) per process by default

– max 134M processes per VM (limited by RAM)

Page 12: Erlang - Concurrent Language for Concurrent World

Concurrency-oriented Programming

• Large number of processes (up to millions)

• Complete isolation of processes

• No shared memory

• Copy message data between processes

– instead of just passing pointer to message

• Avoid sequential bottlenecks

• Location transparency

Page 13: Erlang - Concurrent Language for Concurrent World

Share Nothing!

No mutable State!

Page 14: Erlang - Concurrent Language for Concurrent World

Software Isolation

Page 16: Erlang - Concurrent Language for Concurrent World

One Process per

Parallel Activity

• try to do only one thing per process

• If you need to do something

– just spawn a new process

• no thread pools!

Page 17: Erlang - Concurrent Language for Concurrent World
Page 18: Erlang - Concurrent Language for Concurrent World

Let it crash!

• all sequential languages get error handling wrong!

• No defensive programming!

• If you don’t know how to recover from the error – let it crash!

Page 19: Erlang - Concurrent Language for Concurrent World

Lenin – Lived Lenin – is Alive Lenin – will Live!

Page 20: Erlang - Concurrent Language for Concurrent World

Errors – Existed Errors – Exist Errors – will Exist!

Page 21: Erlang - Concurrent Language for Concurrent World

Lenin, read my dissertation: “Making reliable distributed systems in the presence of software errors”

Page 23: Erlang - Concurrent Language for Concurrent World

Erlang Timeline

19841986

1987

1991

1993

OTP

1996

1998

2007

Open Source Distributed Erlang

Early Erlang Prototypes

First fast implementation

CS Lab Experiments

AXD301 switch

Page 25: Erlang - Concurrent Language for Concurrent World
Page 26: Erlang - Concurrent Language for Concurrent World

Functional Sequential Erlang

• Data types: – Integers, floats, atoms, tuples/records, lists,

binaries, funs

• single assignment • pattern matching & guards • closures (anonymous function data type) • list comprehensions • bit-syntax & binary comprehensions • tail recursion & tail call optimization (TCO)

Page 27: Erlang - Concurrent Language for Concurrent World

Single Assignment 1> X.

* 2: variable 'X' is unbound

2> X = 10.

10

3> X = X + 1.

** exception error: no match of right hand side value 11

4> X = X.

10

5> X = 10.

10

6> X = 20.

** exception error: no match of right hand side value 20

Page 28: Erlang - Concurrent Language for Concurrent World

No Loops???

• Use instead:

– recursion and tail recursion

– list comprehensions

–binary comprehensions

–Higher-order Functions (HOFs):

• map/foldl/foldr/zip/filter/etc.

• For example:

[ io:format(“~b”,[I]) || I<-lists:seq(1,10) ].

Page 29: Erlang - Concurrent Language for Concurrent World

Regular Recursion

Page 30: Erlang - Concurrent Language for Concurrent World

Tail Recursion

Page 31: Erlang - Concurrent Language for Concurrent World
Page 32: Erlang - Concurrent Language for Concurrent World

zvi@host:~$ erl

Erlang R16B02 …

1> c(demo).

{ok,demo}

2> demo:fact(0).

1

3> demo:fact(5).

120

4> demo:fact(-10).

** exception error: no function clause matching demo:fact(-10) (demo.erl, line 10)

Page 33: Erlang - Concurrent Language for Concurrent World
Page 34: Erlang - Concurrent Language for Concurrent World

OOP vs FP

Page 35: Erlang - Concurrent Language for Concurrent World

Side-Effects • Erlang is not pure-functional language

• “Controlled Side effects”:

– Messages - Pid ! Msg – send message to process Pid

– Signals - i.e. your linked process is dead

– Exceptions

– I/O

– Process Dictionary - similar to TLS (thread local storage)

– ETS / DETS - fast in-memory / persistent lookup tables

– Mnesia - distributed realtime database (sort of STM)

Page 36: Erlang - Concurrent Language for Concurrent World
Page 37: Erlang - Concurrent Language for Concurrent World

Erlang Source Code Structure

• OTP Release (self-contained tar.gz archive)

– OTP Application (directory structure)

• Module (file sources in *.erl, compiled in *.beam) – Function

» Clause (syntactic sugar for case statement)

Page 38: Erlang - Concurrent Language for Concurrent World

MFA

• Module:Function/Arity

• Module:Function(Arguments)

• M:F(A1,A2,..,An) = apply(M,F,[A1,A2,..,An])

Page 39: Erlang - Concurrent Language for Concurrent World

Concurrent Erlang

• Spawn processes

• Send messages to processes

• Selective receive

• Additional data types:

– PIDs, references

Page 40: Erlang - Concurrent Language for Concurrent World

Spawn a process

Page 41: Erlang - Concurrent Language for Concurrent World

Send Message

Page 42: Erlang - Concurrent Language for Concurrent World

Receive Message

Page 43: Erlang - Concurrent Language for Concurrent World

Actors / Agents / Active Objects

Page 44: Erlang - Concurrent Language for Concurrent World

Process

• Pid – Process ID • optional: registered name • initial call: MFA • message queue (mailbox) • stack • heap • Dictionary (like TLS – thread local storage) • traping exits? (boolean flag) • linked processes • priority (low/normal/high) • Reductions counter

Page 45: Erlang - Concurrent Language for Concurrent World

Distributed Erlang

Erlang Run-Time System Erlang Run-Time System

network

A B C

Node name: [email protected] Node name: [email protected]

Page 46: Erlang - Concurrent Language for Concurrent World

Distributed Erlang

Erlang Run-Time System Erlang Run-Time System

B ! Msg

network

A B C

Node name: [email protected] Node name: [email protected]

Page 47: Erlang - Concurrent Language for Concurrent World

Distributed Erlang

Erlang Run-Time System Erlang Run-Time System

B ! Msg

network

C ! Msg

A B C

Node name: [email protected] Node name: [email protected]

Page 48: Erlang - Concurrent Language for Concurrent World

Erlang VM - SMP Schedulers

Page 49: Erlang - Concurrent Language for Concurrent World

zvi@host:~$ erl

Erlang R16B02 (erts-5.10.3) [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false]

Eshell V5.10.3 (abort with ^G)

1>

Page 50: Erlang - Concurrent Language for Concurrent World
Page 51: Erlang - Concurrent Language for Concurrent World

Hot Code Reloading

• Module may have 2 version loaded in VM: – old version – current version

• When new version of module is loaded: – it become a “current version” – current version become the “old version”

• All new processes will use current version • The old processes running old version

– In places in the code where module name is explicitly specified - the process will switch to current version: • my_function(Args) - no version switch • my_module:my_function(Args) - switching version

Page 52: Erlang - Concurrent Language for Concurrent World

Tools

• IDE: – Emacs, Distel, Plugins for Eclipse and Netbeans

• Testing: – e-unit, meck, proper, QuickCheck (paid), Quick Check mini - free

• Static code analyzer: – Dialyzer

• Debug and trace – Built-in in VM – dbg, redbug

• Build: – emake, rebar, relx, erlang.mk

• Package Managers: – rebar, mix

Page 53: Erlang - Concurrent Language for Concurrent World

ERLANG RESOURCES

Docs, Tutorials, Community, Conferences

Page 54: Erlang - Concurrent Language for Concurrent World

ErlangCentral.org

Erlan

g b

oo

ks

Page 56: Erlang - Concurrent Language for Concurrent World

http://www.erlang.org/doc

Page 57: Erlang - Concurrent Language for Concurrent World
Page 58: Erlang - Concurrent Language for Concurrent World
Page 60: Erlang - Concurrent Language for Concurrent World
Page 62: Erlang - Concurrent Language for Concurrent World

http://www.TryErlang.org

Page 63: Erlang - Concurrent Language for Concurrent World

https://ErlangCentral.org

Page 64: Erlang - Concurrent Language for Concurrent World
Page 65: Erlang - Concurrent Language for Concurrent World
Page 66: Erlang - Concurrent Language for Concurrent World

Erlang/OTP is a good fit for:

• Irregular concurrency: – Task-level – Fine-grained parallelism

• Network servers • Distributed systems • Middleware:

– Parallel databases – Message Queue servers

• Soft Realtime / Embedded applications • Monitoring, control and testing tools

Page 67: Erlang - Concurrent Language for Concurrent World

Not so good fit for:

• Concurrency for synchronized parallel execution – Data Parallelism

• Floating-point intensive code (HPC) • Text Processing / Unicode

– Unicode support now much better

• Traditional GUI (supports wxWidgets) • Hard Realtime applications • Extensive interop with other languages/VMs

– improvement here with NIFs and Erjang - Erlang on JVM

Page 68: Erlang - Concurrent Language for Concurrent World

Thank You! Now Q&A

All images are taken from Google Image search and various other places on the Internet © Copyright of corresponding owners

Page 69: Erlang - Concurrent Language for Concurrent World

BACKUP SLIDES

Page 70: Erlang - Concurrent Language for Concurrent World

Communicating with External world:

• Part of the OTP: – CORBA/IDL – ODBC/SQL – ASN.1 – SNMP – HTTP/FTP – SSH

• erlcall – execute any Erlang function on remote Node from the

shell or script

• escript – write scripts in Erlang – no need to compile

Page 71: Erlang - Concurrent Language for Concurrent World

Communicating with External world In-proc Out-of-proc

Custom Protocol Linked-in Drivers • in-proc (.so, DLLs) • Written in C • custom protocols • Can crash VM

Ports • external executable • like pipes • custom protocols via

fd/stdio/sockets • Dir = os:cmd(“ls –al”).

Generic Protocol NIF • Native Implemented

Functions • Written in C • in-proc (like JNI in Java:

.so, DLLs) • .so/DLLs can be hot code

reloaded at runtime • can block a scheduler • Can crash VM

C-Node • out-of-proc (external executable,

interpreter or VM) • Pretend to be a distributed

Erlang node (but implemented in other lang.)

• JInterface (Java), OTP.NET, PyInterface (Pyton), Ruby-electricity, etc.

• Example: erlcall

Page 72: Erlang - Concurrent Language for Concurrent World

LANGUAGES FOR ERLANG VM

ERLANG ON JVM

Page 73: Erlang - Concurrent Language for Concurrent World

Erjang – Erlang for JVM

Page 74: Erlang - Concurrent Language for Concurrent World
Page 75: Erlang - Concurrent Language for Concurrent World
Page 76: Erlang - Concurrent Language for Concurrent World
Page 77: Erlang - Concurrent Language for Concurrent World

Elixir Books

Page 78: Erlang - Concurrent Language for Concurrent World

COMPARISON

Concurrent Programming: Scala+Akka, node.js, Go

Page 79: Erlang - Concurrent Language for Concurrent World
Page 80: Erlang - Concurrent Language for Concurrent World
Page 81: Erlang - Concurrent Language for Concurrent World
Page 82: Erlang - Concurrent Language for Concurrent World

Go-lang