Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Erlang OTP


Published on

Published in: Technology, News & Politics
  • Be the first to comment

Erlang OTP

  1. 1. Erlang Open Telecom Platform EAB/UPD/S Ulf Wiger
  2. 2. Contents <ul><li>Background </li></ul><ul><li>The Erlang Language </li></ul><ul><li>OTP </li></ul><ul><li>The Erlang/OTP Test Server </li></ul><ul><li>Experiences </li></ul>
  3. 3. History of Erlang 1984-86: Experiments programming POTS with several languages 1998: Open Source Erlang 1987: Early Erlang Prototype projects 1991: First fast implementation 1993: Distributed Erlang 1995: Several new projects 1996: Open Telecom Platform AXD and GPRS started How to design SW for future telecoms systems?
  4. 4. Erlang “Hello World” program <ul><li>io:format(“Hello, World~n”). </li></ul><ul><li>Hello, World </li></ul><ul><li>ok </li></ul><ul><li>io:format(“Hello, ~s!~n”, [“Zvi”]). </li></ul><ul><li>Hello, Zvi </li></ul><ul><li>ok </li></ul><ul><li>NOTE: “~s” instead of “%s”, like in printf in C. </li></ul><ul><li>and “~n” instead of “ ”. </li></ul>
  5. 5. Erlang Data Types <ul><li>Atoms: </li></ul><ul><ul><li>true, false, inject_slice, zvi@home, ‘EXIT’, ‘+’ </li></ul></ul><ul><li>Integers ( only big integers allocated on the heap ) </li></ul><ul><ul><li>10, -100, 1234567890999999999999999999999 </li></ul></ul><ul><ul><li>2#101 = 5 </li></ul></ul><ul><ul><li>16#FF = 255 </li></ul></ul><ul><ul><li>$A = 65 (i.e. ASCII code of character ‘A’) </li></ul></ul><ul><li>Floats ( floats allocated on the heap – bad for HPC) </li></ul><ul><ul><li>3.1415, 1.0, -1.7e-9 </li></ul></ul><ul><li>Tuples: </li></ul><ul><ul><li>{{1,2,3}, {a,b,c}, {{1,2,{a,b,c},{}} </li></ul></ul><ul><li>Lists: </li></ul><ul><ul><li>[[1,2,3], [a,b,c], [[1,2,[a,b,c],[]] </li></ul></ul>
  6. 6. Erlang Data Types (2) <ul><li>String is a list of ASCII codes: </li></ul><ul><ul><li>“ hello” = [$h,$e,$l,$l,$o] = [104,101,108,108,111] </li></ul></ul><ul><li>Binaries (sequence of bytes): </li></ul><ul><ul><li><<104,101,108,108,111>> = <<“hello”>> </li></ul></ul><ul><li>Binaries with Bit-Syntax: </li></ul><ul><ul><li><<1:1,32787:15>> = 2#1111111111111111 </li></ul></ul><ul><li>Other data types: </li></ul><ul><ul><li>Funs (anonymous functions – closures) </li></ul></ul><ul><ul><li>ProcessID </li></ul></ul><ul><ul><li>Port </li></ul></ul><ul><ul><li>etc. </li></ul></ul>
  7. 7. Pattern Matching and Single Assignment
  8. 8. List Operations
  9. 9. List Operations (2)
  10. 10. List Comprehensions
  11. 11. Anonymous Functions
  12. 12. Functions (cont.)
  13. 13. Erlang Highlights <ul><li>Declarative </li></ul><ul><li>Concurrency </li></ul><ul><li>Soft real-time </li></ul><ul><li>Robustness </li></ul><ul><li>Distribution </li></ul><ul><li>Hot code loading </li></ul><ul><li>External interfaces </li></ul><ul><li>Portability </li></ul>Functional programming language High abstraction level Pattern matching Concise readable programs
  14. 14. Erlang Highlights <ul><li>Declarative </li></ul><ul><li>Concurrency </li></ul><ul><li>Soft real-time </li></ul><ul><li>Robustness </li></ul><ul><li>Distribution </li></ul><ul><li>Hot code loading </li></ul><ul><li>External interfaces </li></ul><ul><li>Portability </li></ul>Solid concurrency model Scales to handle complex concurrency Simple abstractions Examples...
  15. 15. Erlang Example Creating a new process using spawn -module(ex3). -export([activity/3]). activity(Name,Pos,Size) -> ………… Pid = spawn(ex3,activity,[Joe,75,1024]) activity(Joe,75,1024)
  16. 16. Erlang Example Processes communicate by asynchronous message passing Pid ! {data,12,13} receive {start} -> ……… {stop} -> ……… {data,X,Y} -> ……… end receive {start} -> ……… {stop} -> ……… {data,X,Y} -> ……… end
  17. 17. Erlang Examples 4 Concurrency - Finite State Machine ringing_B_side(PidA) -> receive {lim, offhook} -> lim:stop_ringing(), PidA ! {hc, {connect, self()}}, speech(PidA); {hc, {cancel, PidA}} -> cancel(PidA); {lim, {digit, _Digit}} -> ringing_B_side(PidA); {hc, {request_connection, Pid}} -> Pid ! {hc, {reject, self()}}, ringing_B_side(PidA) after 30000 -> cancel(PidA) end. Selective receive True encapsulation of sub-states Optional timeout Asynchronous send
  18. 18. Erlang Highlights <ul><li>Declarative </li></ul><ul><li>Concurrency </li></ul><ul><li>Soft real-time </li></ul><ul><li>Robustness </li></ul><ul><li>Distribution </li></ul><ul><li>Hot code loading </li></ul><ul><li>External interfaces </li></ul><ul><li>Portability </li></ul>Lightweight processes Fast message passing Response times in the order of milliseconds efficient garbage collection Numbers...
  19. 19. Process creation times (LOG/LOG scale) Source: Joe Armstrong SICS > 200,000 processes 10 100 1,000 10,000 100,000 Number of processes 1 10 100 1,000 Microseconds/process erlang java C#
  20. 20. Message passing times (LOG/LOG scale) Source: Joe Armstrong SICS > 200,000 processes 10 100 1,000 10,000 100,000 Number of processes 1 10 1,000 100,000 Microseconds/message erlang java C# 10,000 100 1
  21. 21. Erlang Highlights <ul><li>Declarative </li></ul><ul><li>Concurrency </li></ul><ul><li>Soft real-time </li></ul><ul><li>Robustness </li></ul><ul><li>Distribution </li></ul><ul><li>Hot code loading </li></ul><ul><li>External interfaces </li></ul><ul><li>Portability </li></ul>Simple and consistent error recovery Supervision hierarchies &quot;Program for the correct case&quot; Examples...
  22. 22. Erlang Example Cooperating processes may be linked together using spawn_link(…,…,…) or link(Pid)
  23. 23. Erlang Example When a process terminates, an exit signal is sent to all linked processes … and the termination is propagated
  24. 24. Erlang Example Exit signals can be trapped and received as messages receive {‘EXIT’,Pid,...} -> ... end process_flag(trap_exit,true), ...
  25. 25. Erlang Example Robust systems can be built by layering “ Supervisors” “ Workers”
  26. 26. Error-handling -- Language Safety <ul><li>No global variables -- fewer side-effects </li></ul><ul><li>No direct memory access -- no pointer errors </li></ul><ul><li>No malloc/free bugs </li></ul><ul><li>Solid concurrency model -- reduces synchronization problems, reduces the state space ( simpler programs ) </li></ul><ul><li>Fault isolation -- memory-protected lightweight processes </li></ul><ul><li>Built-in error recovery support -- more consistency </li></ul>Concurrency & Fault Tolerance were designed into the language from the start!
  27. 27. Debugging and Profiling Support <ul><li>Symbolic crash reports </li></ul><ul><ul><li>Usually sufficient info to locate bugs within minutes </li></ul></ul><ul><li>Built-in trace support </li></ul><ul><ul><li>Function calls (ability to filter on module name, function and args) </li></ul></ul><ul><ul><li>Messages (+ sequence trace) </li></ul></ul><ul><ul><li>Process events (context switch, spawn, link, exit) </li></ul></ul><ul><ul><li>Garbage collections </li></ul></ul><ul><ul><li>Optionally with timestamps (can be used for profiling, benchmarks) </li></ul></ul><ul><ul><li>Trace to process, file, or port (network socket) </li></ul></ul><ul><ul><li>Also available on live systems </li></ul></ul>
  28. 28. Erlang Highlights <ul><li>Declarative </li></ul><ul><li>Concurrency </li></ul><ul><li>Soft real-time </li></ul><ul><li>Robustness </li></ul><ul><li>Distribution </li></ul><ul><li>Hot code loading </li></ul><ul><li>External interfaces </li></ul><ul><li>Portability </li></ul>Explicit or transparent distribution Network-aware runtime system Examples...
  29. 29. Transparent Distribution Erlang Run-Time System Erlang Run-Time System B ! Msg network C ! Msg Message passing between processes in different computer is just as easy as between processes in the same computer A B C
  30. 30. Simple RPC loop() -> receive {From, {apply, M, F, A}} -> Answer = (catch apply(M, F, A)), From ! {rex, node(), Answer} loop(); _Other -> loop() end. {rex, Node} ! {self(), {apply, M, F, A}}, receive {rex, Node, What} -> What end {rex, Node} ! {self(), {apply, M, F, A}}, receive {rex, Node, What} -> What end {rex, Node} ! {self(), {apply, M, F, A}}, receive {rex, Node, What} -> What end loop() -> receive {From, {apply, M, F, A}} -> Answer = (catch apply(M, F, A)), From ! {rex, node(), Answer} loop(); _Other -> loop() end. loop() -> receive {From, {apply, M, F, A}} -> Answer = (catch apply(M, F, A)), From ! {rex, node(), Answer} loop(); _Other -> loop() end. loop() -> receive {From, {apply, M, F, A}} -> Answer = (catch apply(M, F, A)), From ! {rex, node(), Answer} loop(); _Other -> loop() end.
  31. 31. Erlang Highlights <ul><li>Declarative </li></ul><ul><li>Concurrency </li></ul><ul><li>Soft real-time </li></ul><ul><li>Robustness </li></ul><ul><li>Distribution </li></ul><ul><li>Hot code loading </li></ul><ul><li>External interfaces </li></ul><ul><li>Portability </li></ul>Easily change code in a running system Enables non-stop operation Simplifies testing Examples...
  32. 32. Erlang Example Version 1 Version 2 change_code
  33. 33. Erlang Highlights <ul><li>Declarative </li></ul><ul><li>Concurrency </li></ul><ul><li>Soft real-time </li></ul><ul><li>Robustness </li></ul><ul><li>Distribution </li></ul><ul><li>Hot code loading </li></ul><ul><li>External interfaces </li></ul><ul><li>Portability </li></ul>&quot;Ports&quot; to the outside world behave as Erlang processes (c.f. UML ports) Examples...
  34. 34. Erlang Example Port External process Port ! {self(), {command, [1,2,3]}}
  35. 35. Erlang Example Port External process receive {Port, {data, Info}} -> end A port can use e.g. a TCP, UDP, SSL socket, UNIX pipe, or custom transport (e.g. SAAL)
  36. 36. Erlang Highlights <ul><li>Declarative </li></ul><ul><li>Concurrency </li></ul><ul><li>Soft real-time </li></ul><ul><li>Robustness </li></ul><ul><li>Distribution </li></ul><ul><li>Hot code loading </li></ul><ul><li>External interfaces </li></ul><ul><li>Portability </li></ul>Erlang runs on any UNIX, Windows, VxWorks, OSE Delta Supports heterogeneous networks Illustration...
  37. 37. Systems Overview OTP Components Standard Libraries Erlang Run-Time System Hardware and Operating System Applications written in Erlang Applications written in C, C++ or Java
  38. 38. Erlang/OTP (Open Telecom Platform) <ul><li>Middleware for Erlang development </li></ul><ul><li>Designed for fault tolerance and portability </li></ul><ul><li>Behaviors: A formalization of design patterns </li></ul><ul><li>Components </li></ul><ul><ul><li>Error handling, reporting and logging </li></ul></ul><ul><ul><li>Mnesia, distributed real-time database management system </li></ul></ul><ul><ul><li>CORBA, IDL Compiler, Java & C Interface Support </li></ul></ul><ul><ul><li>HTTP Server + Client, FTP Client </li></ul></ul><ul><ul><li>SNMP Agent + ASN.1 Compiler </li></ul></ul><ul><ul><li>H.248 </li></ul></ul><ul><ul><li>XML </li></ul></ul><ul><ul><li>... </li></ul></ul>
  39. 39. OTP Behaviors <ul><li>&quot;A formalization of design patterns&quot; </li></ul><ul><ul><li>A framework + generic code to solve a common problem </li></ul></ul><ul><ul><li>Built-in support for debugging and software upgrade </li></ul></ul><ul><ul><li>Makes it easier to reason about the behavior of a program </li></ul></ul><ul><li>Examples of OTP behaviors </li></ul><ul><ul><li>application defines how an application is implemented </li></ul></ul><ul><ul><li>supervisor used to write fault-tolerant supervision trees </li></ul></ul><ul><ul><li>gen_server for writing client-server applications </li></ul></ul><ul><ul><li>gen_event for writing event handlers </li></ul></ul><ul><ul><li>gen_fsm for finite state machine programming </li></ul></ul>
  40. 40. Using Erlang as a Test Tool Language <ul><li>Declarative style </li></ul><ul><li>Program for the correct case </li></ul><ul><li>Pattern matching used for compact &quot;assertions&quot; </li></ul><ul><li>EXIT indicates test case failure </li></ul><ul><li>Easy to spawn worker threads to test concurrency patterns </li></ul><ul><li>Test case cleanup is easy due to local variables, process linking and cascading EXITs </li></ul>
  41. 41. Experiences from Erlang in Large Projects <ul><li>Easy to build a first runnable application </li></ul><ul><li>Easy to debug </li></ul><ul><li>Easy to maintain </li></ul><ul><li>Very strong support for fault tolerance </li></ul><ul><li>Suitable for large systems/projects </li></ul><ul><li>Great for prototyping </li></ul><ul><li>Impressive performance/scalability in real applications </li></ul><ul><li>Outstanding tool for test automation </li></ul><ul><li>High programmer satisfaction </li></ul>
  42. 42. Erlang-based Products as of today <ul><li>Ericsson: AXD 301, GPRS, (NetSim), LCS </li></ul><ul><li>Nortel: SSL Accelerator, SSL VPN gateway + others </li></ul><ul><li>TMobile: IN applications </li></ul><ul><li>Vail Systems: Computer Telephony Apps Service Prov. </li></ul><ul><li>Erlang Financial Systems: Banking & Lottery systems </li></ul><ul><li>Mobile Arts: Presence & Messaging for GSM/UMTS </li></ul><ul><li> Billing & device configuration </li></ul><ul><li>Blue Position: Bluetooth Location Information System </li></ul><ul><li>Motivity: Answer Supervision Generator, Signalling Gateway </li></ul><ul><li>Telia: CTI Platform </li></ul><ul><li>Corelatus: Signalling gateways & cross-connects </li></ul><ul><li>Bluetail/TeleNordia: Robust SMTP Mail Server </li></ul><ul><li>Univ. of Coruña: VoD Cluster </li></ul>
  43. 43. Downloads since Open Source Launch ’98 Grouping: 6 months
  44. 44. Example Products: Outside Ericsson <ul><li>Amazon Web Services - SQS and SimpleDB, iMDB </li></ul><ul><li>Facebook - social networking chat </li></ul><ul><li>Powerset (now Microsoft bing) – Semantic Search & NLP </li></ul><ul><li>Twitter – microbloging </li></ul><ul><li> – presentations </li></ul><ul><li>Mochimedia – ads in Flash games </li></ul><ul><li> / Yahoo! – social bookmarking </li></ul>
  45. 45. Example Products: Open Source <ul><li>EDDIE, Distributed TCP/IP based Clusterware </li></ul><ul><li>Wings 3D, a 3D modeller based on Nendo </li></ul><ul><li>YAWS, Yet Another Web Server </li></ul><ul><li>RabbitMQ, high performance enterprise messaging </li></ul><ul><li>Ejabberd, instant messaging server </li></ul><ul><li>Erlyweb, web development framework </li></ul>
  46. 46. Example Products: Open Source <ul><li>Apache CouchDB – document-based DB with REST I/F </li></ul><ul><li>Disco – Map/Reduce framework for Erlang </li></ul><ul><li>Fuzed – Generic clustering framework (Powerset) </li></ul><ul><li>Mochiweb – Fast lightweight web server framework (like Java Servlets for Erlang) </li></ul><ul><li>Many-many web frameworks </li></ul><ul><li>OpenPoker – scalable Poker server </li></ul><ul><li>DHT – Distributed Key/Value Stores – 90% of all implementations in Erlang – Dunomite, Scalaris, etc. </li></ul>
  47. 47. Erlang good fit for: <ul><li>Irregular concurrency </li></ul><ul><ul><li>Task-level parallelism </li></ul></ul><ul><ul><li>Fine-grained parallelism </li></ul></ul><ul><li>Network servers </li></ul><ul><li>Distributed systems </li></ul><ul><li>Middleware: </li></ul><ul><ul><li>Parallel databases </li></ul></ul><ul><ul><li>Message Queue servers </li></ul></ul><ul><li>Soft Real-Time / Embedded applications </li></ul><ul><li>Monitoring, control and testing tools </li></ul>
  48. 48. Erlang not so good for: <ul><li>Concurrency more appropriate to synchronized parallel execution: </li></ul><ul><ul><li>Data Parallelism </li></ul></ul><ul><li>Floating-point intensive code (HPC) </li></ul><ul><li>Text Processing / Unicode </li></ul><ul><li>Traditional GUI </li></ul><ul><li>Hard Real-Time applications </li></ul><ul><li>Projects using: </li></ul><ul><ul><li>Non-portable instructions </li></ul></ul><ul><ul><li>Running on JVM / CLR </li></ul></ul><ul><ul><li>Extensive use of libraries in other languages </li></ul></ul>
  49. 49. Questions?