Upload
inhacking
View
75
Download
3
Embed Size (px)
Citation preview
Erlang benefits
Functional naturePattern matching on steroidsConcurrencyFault ToleranceLet it crashDistributed ComputationDebugging and profilingHot upgrades
Erlang benefits
Functional naturePattern matching on steroidsConcurrencyFault ToleranceLet it crashDistributed ComputationDebugging and profilingHot upgrades
Functions are easy
Dynamic typesPattern matchingNo mutable datastructures
Binary tree search
Factorial function
Erlang benefits
Functional naturePattern matching on steroidsConcurrencyFault ToleranceLet it crashDistributed ComputationDebugging and profilingHot upgrades
Pattern matching and steroids
Structures
Binaries
Complex conditions
1 get_vh_registered_users(Server, [{prefix, Prefix}, {limit, Limit}, {offset, Offset}]) 2 when is_list(Prefix) and is_integer(Limit) and is_integer(Offset) ->3 case [{U,S} || {U, S} <- get_vh_registered_users(Server), lists:prefix(Prefix, U)] of4 [] ->5 [];6 Users ->7 Set = lists:keysort(1, Users),8 L = length(Set),9 Start = if Offset < 1 -> 1;10 Offset > L -> L;11 true -> Offset12 end,13 lists:sublist(Set, Start, Limit)14 end.
* Not your familiar Regex string matching
Erlang benefits
Functional naturePattern matching on steroidsConcurrencyFault ToleranceLet it crashDistributed ComputationDebugging and profilingHot upgrades
• Behavior• State• Parallel• Asynchronous Messages Mailboxes• No Shared State
The Actor ModelCarl Hewitt
1973
• Data + Code + Process • Self-Contained Machines • Stronger Encapsulation • Less Inheritance• Type Inference• Hot Code Upgrades
Actor structure
Behavior way
• Inheritance of Behavior only.• Usually only one level deep.• Usually one of the standard OTP behaviors:
• Generic Server• Event• State Machine• Supervisor.
Actor Model: Benefits
• More true to the real world• Better suited for parallel hardware• Better suited for distributed architectures • Scaling garbage collection (gc/actor)• Less Magic
Actors example-module(pingpong).-export([start/0]).-export([ping/0, pong/0]).
start()->PingPid = spawn(?MODULE, ping(), []),PongPid = spawn(?MODULE, pong(), []),PingPid ! {ping, PongPid},erlang:send_after(60000,PingPid, stop),erlang:send_after(60000,PongPid, stop).
ping()-> receive {Pid, ping} -> io:fwrite("Ping received Pong~n",[]), timer:sleep(3000), Pid ! {self(), pong}, ping(); stop -> true end.
pong() -> receive {Pid, pong} -> io:fwrite("Pong received Ping~n",[]), timer:sleep(3000), Pid ! {self(), ping}, pong(); stop -> true end.
Erlang benefits
Functional naturePattern matching on steroidsConcurrencyFault ToleranceLet it crashDistributed ComputationDebugging and profilingHot upgrades
Erlang benefits
Functional naturePattern matching on steroidsConcurrencyFault ToleranceLet it crashDistributed ComputationDebugging and profilingHot upgrades
Erlang benefits
Functional naturePattern matching on steroidsConcurrencyFault ToleranceDistributed ComputationDebugging and profilingHot upgrades
Setting up an Erlang clustererl -sname ketchup...(ketchup@ferdmbp)1>(ketchup@ferdmbp)1> net_kernel:connect_node(fries@ferdmbp).true
(ketchup@ferdmbp)2> node().ketchup@ferdmbp(ketchup@ferdmbp)3> nodes().[fries@ferdmbp]
(ketchup@ferdmbp)4> register(shell, self()).true
(ketchup@ferdmbp)5> {shell, fries@ferdmbp} ! {hello, from, self()}.{hello,from,<0.52.0>}
(ketchup@ferdmbp)6> flush().Shell got <<"hey there!">>ok
erl -sname fries...(fries@ferdmbp)1>
(fries@ferdmbp)1> register(shell, self()).true
(fries@ferdmbp)2> receive {hello, from, OtherShell} -> OtherShell ! <<"hey there!">> end.
http://learnyousomeerlang.com/distribunomicon
Erlang benefits
Functional naturePattern matching on steroidsConcurrencyFault ToleranceDistributed ComputationDebugging and profilingHot upgrades
Remote shells
(salad@ferdmbp)1>User switch command--> hc [nn] - connect to jobi [nn] - interrupt jobk [nn] - kill jobj - list all jobss [shell] - start local shellr [node [shell]] - start remote shellq - quit erlang? | h - this message--> r mustard@ferdmbp--> j1 {shell,start,[init]}2* {mustard@ferdmbp,shell,start,[]}--> cEshell V5.8.4 (abort with ^G)(mustard@ferdmbp)1> node().mustard@ferdmbp
Debugger and profilers
Tool Results Size of Result
Effects on Program Execution Time
Records Number of Calls
Records Execution Time
Records Called by
Records Garbage Collection
fprofPer process to screen/file Large Significant slowdown Yes Total and
own Yes Yes
eprofPer process/function to screen/file Medium Small slowdown Yes Only total No No
coverPer module to screen/file Small Moderate slowdown Yes, per line No No No
cprof Per module to caller Small Small slowdown Yes No No No
dbg Text based tracer Large Small slowdonw No No Yes No
Community
• recon• redbug• lager• xprof
More introspection
• etop• pman• os_mon• debugger
Erlang benefits
Functional naturePattern matching on steroidsConcurrencyFault ToleranceDistributed ComputationDebugging and profilingHot upgrades
Measurements
Activity
• millions active users• fast processes for handling
Stability
• developers and operations sleeps fine• no emergencies for a couple of years
Performance
• compatible to C, • VM memory management• lightweight concurrency• scalability from a box
Issues
• concurrent bottlenecks• Net splits• Networks security• Hot upgrades