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

Erlang OTP

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

Editor's Notes

  • #4 2003-12-01 Rev PA1
  • #14 2003-12-01 Rev PA1
  • #15 2003-12-01 Rev PA1
  • #16 2003-12-01 Rev PA1
  • #17 2003-12-01 Rev PA1
  • #18 2003-12-01 Rev PA1
  • #19 2003-12-01 Rev PA1
  • #22 2003-12-01 Rev PA1
  • #23 2003-12-01 Rev PA1
  • #24 2003-12-01 Rev PA1
  • #25 2003-12-01 Rev PA1
  • #26 2003-12-01 Rev PA1
  • #29 2003-12-01 Rev PA1
  • #30 2003-12-01 Rev PA1
  • #31 2003-12-01 Rev PA1 This is the essential loop needed for an RPC server. The server receives requests from clients, uses apply to evaluate the requests and then returns the answer to the originator. This is a simplified solution where no error handling is done. The BIF node/0 returns the node name.
  • #32 2003-12-01 Rev PA1
  • #33 2003-12-01 Rev PA1
  • #34 2003-12-01 Rev PA1
  • #35 2003-12-01 Rev PA1
  • #36 2003-12-01 Rev PA1
  • #37 2003-12-01 Rev PA1
  • #38 2003-12-01 Rev PA1
  • #39 2003-12-01 Rev PA1
  • #40 2003-12-01 Rev PA1