Erlang - Concurrent Language for Concurrent World

3,372 views

Published on

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

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,372
On SlideShare
0
From Embeds
0
Number of Embeds
76
Actions
Shares
0
Downloads
80
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

Erlang - Concurrent Language for Concurrent World

  1. 1. $ whoami Name: Zvi Avraham Title: Founder & CEO Company: ZADATA Ltd E-mail: zvi@zadata.com
  2. 2. Concurrent Language for Concurrent World
  3. 3. The world is concurrent… I could not drive the car, if I did not understood concurrency … -- Joe Armstrong, father of Erlang
  4. 4. Object-oriented Programming
  5. 5. Object-oriented Programming object object object object object object object object object object object object object object object object object object
  6. 6. Concurrency-oriented Programming
  7. 7. Concurrency-oriented Programming process process process process process process process process process process process process process process process process process process
  8. 8. Imagine a programming language which allows only 1000 objects -- Joe Armstrong, father of Erlang
  9. 9. 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)
  10. 10. 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
  11. 11. Share Nothing! No mutable State!
  12. 12. Software Isolation
  13. 13. But, it will not help if the whole VM crashes
  14. 14. 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!
  15. 15. 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!
  16. 16. Lenin – Lived Lenin – is Alive Lenin – will Live!
  17. 17. Errors – Existed Errors – Exist Errors – will Exist!
  18. 18. Lenin, read my dissertation: “Making reliable distributed systems in the presence of software errors”
  19. 19. http://www.erlang.org/download/armstrong_thesis_2003.pdf
  20. 20. Erlang Timeline 1984 1986 1987 1991 1993 OTP 1996 1998 2007 Open SourceDistributed Erlang Early Erlang Prototypes First fast implementation CS Lab Experiments AXD301 switch
  21. 21. Erlang/OTP Stack Erlang VM – BEAM / ERTS Functional Sequential Erlang Concurrent Erlang OTP – Open Telecom Platform Distributed Erlang ErlangProgrammingLanguage
  22. 22. Functional Sequential Erlang • Data types: –Integers (incl. BigNums), floats, atoms –tuples/records, lists/plists, binaries, funs –Maps (added in R17) • single assignment • pattern matching & guards • closures (anonymous function data type) • list comprehensions • bit-syntax & binary comprehensions • tail recursion & tail call optimization (TCO)
  23. 23. Single Assignment1> 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
  24. 24. No Loops???
  25. 25. 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) ].
  26. 26. RECURSION
  27. 27. RECURSION
  28. 28. Regular Recursion
  29. 29. Tail Recursion
  30. 30. 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)
  31. 31. OOP vs FP
  32. 32. 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)
  33. 33. 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)
  34. 34. MFA • Module:Function/Arity • Module:Function(Arguments) • M:F(A1,A2,..,An) = apply(M,F,[A1,A2,..,An])
  35. 35. Concurrent Erlang • Spawn processes • Send messages to processes • Selective receive • Additional data types: – PIDs, references
  36. 36. Spawn a process
  37. 37. Send Message
  38. 38. Receive Message
  39. 39. Actors / Agents / Active Objects
  40. 40. 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
  41. 41. Distributed Erlang Erlang Run-Time System Erlang Run-Time System network A B C Node name: foo@domain.com Node name: bar@domain.com
  42. 42. Distributed Erlang Erlang Run-Time System Erlang Run-Time System B ! Msg network A B C Node name: foo@domain.com Node name: bar@domain.com
  43. 43. Distributed Erlang Erlang Run-Time System Erlang Run-Time System B ! Msg network C ! Msg A B C Node name: foo@domain.com Node name: bar@domain.com
  44. 44. Erlang VM - SMP Schedulers
  45. 45. 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>
  46. 46. 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
  47. 47. 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
  48. 48. ERLANG RESOURCES Docs, Tutorials, Community, Conferences
  49. 49. ErlangCentral.org Erlangbooks
  50. 50. http://www.erlang.org/download.html
  51. 51. http://www.erlang.org/doc
  52. 52. http://erlang.org/mailman/listinfo/erlang-questions
  53. 53. http://LearnYouSomeErlang.com
  54. 54. http://www.TryErlang.org
  55. 55. https://ErlangCentral.org
  56. 56. 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
  57. 57. 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
  58. 58. Thank You! Now Q&A All images are taken from Google Image search and various other places on the Internet © Copyright of corresponding owners
  59. 59. BACKUP SLIDES
  60. 60. 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
  61. 61. 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
  62. 62. OTHER ERLANG VM IMPLEMENTATIONS
  63. 63. Erjang – Erlang for JVM
  64. 64. Erlang-on-Xen / ling VM
  65. 65. LuvvieScript - Erlang to JS compiler
  66. 66. LANGUAGES FOR ERLANG VM
  67. 67. Elixir Books
  68. 68. COMPARISON Concurrent Programming: Scala+Akka, node.js, Go
  69. 69. Go-lang
  70. 70. Erlang / Elixir Clojure Scala Node.js Go-lang Paradigm Functional COP Functional Functional OOP CBoP OOP ImperativeO OP? Type system dynamic dynamic static dynamic static Target BEAM JVM JVM Chrome V8 native Multithreading yes yes yes no Yes Built-in Asynch I/O yes no no yes yes Lightweight processes yes Partial* no no yes Soft-RT GC yes no no no no Pattern Matching yes no yes no no OTP analogue? Yes - OTP Akka? Akka? no no Tools Middle Elixir - better good middle good excellent Problem Domain usually used Messaging, Distributed Systems Unstruct. data Big Data: Kafka, Spark Web apps / Realtime Web DevOps

×