An introduction to erlang

  • 921 views
Uploaded on

This is an introduction to the Erlang programming language I have given to my local user group on the 30th of January 2012.

This is an introduction to the Erlang programming language I have given to my local user group on the 30th of January 2012.

  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
    Be the first to like this
No Downloads

Views

Total Views
921
On Slideshare
0
From Embeds
0
Number of Embeds
1

Actions

Shares
Downloads
15
Comments
0
Likes
0

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

  • 1. An introduction to Erlang Code Lovers 30-1-2012 @MirkoBonadei
  • 2. Our Agenda- Introduction- Some bits of history- The Erlang Way- Inside the VM- OTP (Open Telecom Platform)- Who uses it?- Conclusion
  • 3. Erlang is about processes P3 P5 P2 P1 P4They are the basic components to model a system.
  • 4. Process isolationChrome Word An O.S. Example...
  • 5. Process isolation Chrome WordYour favourite word processor crashes but your browser is still running
  • 6. Erlang ProcessesProcesses are cheap and lightweight. We can have millions of them.Context Switch and IPC are really fast.
  • 7. Some bits of HistoryErlang came to the masses in 2006/07 but....
  • 8. Some bits of HistoryErlang is 25 years old!So... Why 20 years of silence?
  • 9. Whats happened Distributed Systems MulticoreTechnology “penetration”
  • 10. Crystal Ball?!?
  • 11. Nope, Industrial Requests Ericsson was looking for a language to replace PLEX (their development language in the 80s) Requests: Distributed DeclarativeTime to Market Hot Code Swapping Fault Tolerant Soft Realtime Concurrent
  • 12. Research is the keyAt the Ericsson Lab they developed some pilot projects in variousprogramming languages... Chill ML Miranda Others... SmallT alk Prolog Ada
  • 13. ButThere wasnt a language with all those features Lets create our language!
  • 14. Good results!The first version was a language implemented on the top of Prolog.After some good results, they wrote the JAMMachine in C, boosting performance of 70%.We are in the early 90s, and the “Erlang Era” has just began.
  • 15. 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
  • 16. Ericsson comes backUnderstanding the error made, they come back to Erlang. Ericsson re-hired Joe Armstrong in 2004 The OTP Team is now based in Ericsson.
  • 17. Lets start....talking about concurrency
  • 18. Concurrency we are used to... Error Handling codeCoordination code Code to solve the problem
  • 19. What this means?Loosing AbstractionComplexity explodesMaintenance is hard
  • 20. The Erlang WayThe best way to understand the Erlang way is to think at the real world. World is concurrent, this is the abstraction we want.
  • 21. It is simpleTo obtain scalability and fault tolerance is quite easy. You took 2 things, you make them sharenothing and you get fault tolerance and you can scale. Joe Armstrong (London Erlang Factory 2011)
  • 22. Type SystemErlang is dynamically typed & Erlang is strongly typed
  • 23. Type: Number
  • 24. Type: Atom
  • 25. Type: Binary and Bit StringBit Syntax and Pattern Matching:
  • 26. Type: Pid
  • 27. Type: Port Identifier Basic mechanism to communicate with a non Erlang programThe Port Identifier is returned by the call to the BIF open_port/2
  • 28. Type: Tuple
  • 29. Type: List
  • 30. Type: FunEhy... Erlang is a Functional language :)
  • 31. Type: Reference A term which is uniquein an Erlang Runtime System
  • 32. No Type: StringA String is simply a list of integer values
  • 33. No Type: Booleantrue and false are nothing than atoms
  • 34. No Type: Record
  • 35. Pattern Matching 1/3 Referential Transparency
  • 36. Pattern Matching 2/3
  • 37. Pattern Matching 3/3 Ehy, weve got some recursion here!
  • 38. “Dont Care” VariablesReally useful, but use them with care
  • 39. Processes and IPCTo 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, !
  • 40. An Example
  • 41. An Example
  • 42. An Example
  • 43. How to deal with ErrorsErlang has an Exception system with 3 classes oferrors:- 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)
  • 44. Try … CatchBut Between Processes...
  • 45. 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 thatpart of the system fail fast, and eventually restart it.
  • 46. Links {EXIT, Pid2, Reason} P1 P2We can link processes together with link/1 BIF or directly with spawn_link BIFs. Linked processes forms a linked set, and if one ofthem terminates abnormally, the error propagates the the linked set, taking processes down.
  • 47. Trapping Exits {EXIT, Pid2, Reason} P1 P2 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 choosewhat to do. Stopping the propagation of the error. Only the Reason kill is untrappable!
  • 48. Robustness S S W W S → SupervisorW W W → Worker
  • 49. Monitors While links are bidirectional, monitors are unidirectional.No link set, the monitor process is like a “stalker”.
  • 50. DistributionDistribution is built into the language itself.
  • 51. Are you kidding? Nope, think about it... No shared memory.So, we copy data between processes on the same machine. And... We copy data between processes on different machines.
  • 52. Ok, and the location? ! operator is location transparentA Pid also contains information on the location of the process.
  • 53. Erlang ClustersIt is really simple to create an Erlang cluster.There are apposite libraries, and when a cluster is set,you work without headache.
  • 54. Hot Code Swapping No Downtime allowed for bad guys like us! So...Hot Code Swapping is built into the language itself!
  • 55. How does it woks?Instant 0 P -vsn(1.0)
  • 56. How does it woks?Instant 1 P -vsn(1.0) -vsn(1.1)
  • 57. How does it woks?Instant 2 P -vsn(1.0) -vsn(1.1)
  • 58. How does it woks?Instant 3 P -vsn(1.0) -vsn(1.1) -vsn(1.2)
  • 59. Warning!Code upgrades on intra-module function calls could bite you. The function calls must be fully qualified calls such as:<module_name> : <function_name>(<args,....>)If you follow OTP Principles you can get Hot Code Swapping the right way “without” headache
  • 60. Warning!Hot Code Swapping is a difficult task Even if Erlang helps you... Test Hard your solutions before taking the system down.
  • 61. 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!
  • 62. The Scheduler Fundamental part of the Erlang Runtime System.It has been deeply changed during thelast years to rule multi-core hardware the right way
  • 63. In the past The scheduler was there to manageexecution of Erlang processes inside an O.S. Process called beam or werl Run QueueScheduler
  • 64. Then... Symmetric Multiprocessing was released in May 2006Scheduler #1 Run QueueScheduler #..Scheduler #N
  • 65. Today Bottleneck removed. One run queue for scheduler.Scheduler RQ #1Scheduler RQ #... Migration LogicScheduler RQ #N
  • 66. Garbage Collection Per process Garbage Collection Small processes means quick garbage collectionGarbage Collection doesnt pause the system as happens in other languages. Soft Real-time feature is safe even with million of processes running
  • 67. OTP: Open Telecom PlatformSet 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
  • 68. OTP BenefitsLess code to write means, less bug and fast time to market Easy to test your specific functionalityCommon coding style between developers OTP is battle tested
  • 69. Who uses Erlang
  • 70. Who uses ErlangAnd many others...
  • 71. Bookslearnyousomeerlang.com
  • 72. Next Conferences
  • 73. Thanks