If you can't read please download the document
Upload
mirko-bonadei
View
1.843
Download
0
Embed Size (px)
Citation preview
An introduction to Erlang
Code Lovers 30-1-2012
@MirkoBonadei
Our Agenda
- Introduction
- Some bits of history
- The Erlang Way
- Inside the VM
- OTP (Open Telecom Platform)
- Who uses it?
- Conclusion
Erlang is about processes
They are the basic components to model a system.
P2P3P4P5P1
Process isolation
An O.S. Example...
ChromeWord
Process isolation
Your favourite word processor crashes but your browser is still running
ChromeWord
Erlang Processes
Processes are cheap and lightweight.We can have millions of them.Context Switch and IPC are really fast.
Some bits of History
Erlang came to the masses in 2006/07 but....
Some bits of History
Erlang is 25 years old!
So... Why 20 years of silence?
What's happened
Distributed Systems
Technology penetration
Multicore
Crystal Ball?!?
Nope, Industrial Requests
Ericsson was looking for a language to replace PLEX (their development language in the 80s)
Requests:
Time to Market
Distributed
Fault Tolerant
Concurrent
Hot Code Swapping
Declarative
Soft Realtime
Research is the key
At the Ericsson Lab they developed some pilot projects in various programming languages...
MLMirandaChillOthers...AdaSmallTalkProlog
But
There wasn't a language with all those features
Let's create our language!
Good results!
The first version was a language implemented on the top of Prolog.
After some good results, they wrote the JAM Machine in C, boosting performance of 70%.
We are in the early 90s, and the Erlang Era has just began.
But...
After lots of great results, Ericsson banned Erlang from future projects in 1998.They chose to follow competitors...
Ericsson released Erlang as Open Source
Core developers left Ericsson and founded their companies
Ericsson comes back
Understanding the error made, they come back to Erlang.
Ericsson re-hired Joe Armstrong in 2004
The OTP Team is now based in Ericsson.
Let's start....
talking about concurrency
Concurrency we are used to...
Code to solve the problem
Error Handling code
Coordination code
What this means?
Loosing Abstraction
Complexity explodes
Maintenance is hard
The Erlang Way
The best way to understand the Erlang way is to think at the real world. World is concurrent, this is the abstraction we want.
It is simple
To obtain scalability and fault tolerance is quite easy. You took 2 things, you make them share nothing and you get fault tolerance and you can scale.
Joe Armstrong (London Erlang Factory 2011)
Type System
Erlang is dynamically typed
Erlang is strongly typed
&
Type: Number
Type: Atom
Type: Binary and Bit String
Bit Syntax and Pattern Matching:
Type: Pid
Type: Port Identifier
Basic mechanism to communicate with a non Erlang program
The Port Identifier is returned by the call to the BIF open_port/2
Type: Tuple
Type: List
Type: Fun
Ehy... Erlang is a Functional language :)
Type: Reference
A term which is unique in an Erlang Runtime System
No Type: String
A String is simply a list of integer values
No Type: Boolean
true and false are nothing than atoms
No Type: Record
Pattern Matching 1/3
Referential Transparency
Pattern Matching 2/3
Pattern Matching 3/3
Ehy, we've got some recursion here!
Don't Care Variables
Really useful, but use them with care
Processes and IPC
To create a process we use the spawn functions. There are different versions of spawn, for all the possible scenarios.
Every Process has its MailBox where its messages are delivered.
To send a message to a process we use a one character operator, !
An Example
An Example
An Example
How to deal with Errors
Erlang has an Exception system with 3 classes of errors:
- error: It is a runtime error or it could be generated from the call error(Reason)
- exit: generated from the code with the call exit(Reason)
- throw: generated from the code with the call throw(Reason)
Try Catch
But Between Processes...
Exit Signals
When a process terminates it emits (with its last breath) an exit signal with the reason of its termination
The reason could be normal if things are terminated it the right way, or it could be something else, and in this case the termination is abnormal.
We can use processes to isolate errors, make that part of the system fail fast, and eventually restart it.
Links
P1P2
{'EXIT', Pid2, Reason}
We can link processes together with link/1 BIF or directly with spawn_link BIFs.
Linked processes forms a linked set, and if one of them terminates abnormally, the error propagates the the linked set, taking processes down.
Trapping Exits
P2
{'EXIT', Pid2, Reason}
We can trap exits, making a process become a system process with:process_flag(trap_exits, true).
Now it receives the message, and it can choose what to do. Stopping the propagation of the error.
Only the Reason kill is untrappable!
P1
Robustness
WWWW
S SupervisorW Worker
SS
Monitors
While links are bidirectional, monitors are unidirectional.No link set, the monitor process is like a stalker.
Distribution
Distribution is built into the language itself.
Are you kidding?
No shared memory. So, we copy data between processes on the same machine. And... We copy data between processes on different machines.
Nope, think about it...
Ok, and the location?
! operator is location transparent
A Pid also contains information on the location of the process.
Erlang Clusters
It is really simple to create an Erlang cluster.
There are apposite libraries, and when a cluster is set, you work without headache.
Hot Code Swapping
No Downtime allowed for bad guys like us!
So...
Hot Code Swapping is built into the language itself!
How does it woks?
-vsn(1.0)P
Instant 0
How does it woks?
-vsn(1.0)PInstant 1
-vsn(1.1)
How does it woks?
-vsn(1.0)PInstant 2
-vsn(1.1)
How does it woks?
-vsn(1.0)PInstant 3
-vsn(1.1)-vsn(1.2)
Warning!
Code upgrades on intra-module function calls could bite you.The function calls must be fully qualified calls such as:
If you follow OTP Principles you can get Hot Code Swapping the right way without headache
: ()
Warning!
Hot Code Swapping is a difficult task
Even if Erlang helps you...
Test Hard your solutions before taking the system down.
ERTS and the VM
No clear separation between ERTS and VM.
VM runs compiled byte-code (BEAM)
ERTS manages Processes, IPC, Memory, Load Balancing, ecc...
We always call VM both two!
The Scheduler
Fundamental part of the Erlang Runtime System.
It has been deeply changed during the last years to rule multi-core hardware the right way
In the past
The scheduler was there to manage execution of Erlang processes inside an O.S. Process called beam or werl
Scheduler
Run Queue
Then...
Symmetric Multiprocessing was released in May 2006
Scheduler #1
Run Queue
Scheduler #..Scheduler #N
Scheduler #1
Today
Bottleneck removed. One run queue for scheduler.
Migration LogicScheduler #1
RQ
Scheduler #...
RQ
Scheduler #N
Scheduler #...
RQ
Garbage Collection
Per process Garbage Collection
Small processes means quick garbage collection
Garbage Collection doesn't pause the system as happens in other languages. Soft Real-time feature is safe even with million of processes running
OTP: Open Telecom Platform
Set of tools, libraries and design principles to develop distributed applications
Do not reinvent the wheel every time, use OTP behaviours instead!
Supervisor trees, Hot Code Swapping, Packaging, abstracted away
OTP Benefits
Less code to write means, less bug and fast time to market
Easy to test your specific functionality
Common coding style between developers
OTP is battle tested
Who uses Erlang
Who uses Erlang
And many others...
Books
learnyousomeerlang.com
Next Conferences
Thanks