Top Banner
Erlang. Measurements and benefits Valerii Vasylkov, Timecode LLC.
36

SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Jan 22, 2018

Download

Technology

Inhacking
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Erlang.

Measurements and benefits

Valerii Vasylkov, Timecode LLC.

Page 2: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

History notes

Page 3: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Erlang shots

Page 4: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Erlang benefits

Functional naturePattern matching on steroidsConcurrencyFault ToleranceLet it crashDistributed ComputationDebugging and profilingHot upgrades

Page 5: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Erlang benefits

Functional naturePattern matching on steroidsConcurrencyFault ToleranceLet it crashDistributed ComputationDebugging and profilingHot upgrades

Page 6: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Functions are easy

Dynamic typesPattern matchingNo mutable datastructures

Binary tree search

Factorial function

Page 7: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Erlang benefits

Functional naturePattern matching on steroidsConcurrencyFault ToleranceLet it crashDistributed ComputationDebugging and profilingHot upgrades

Page 8: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

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

Page 9: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Erlang benefits

Functional naturePattern matching on steroidsConcurrencyFault ToleranceLet it crashDistributed ComputationDebugging and profilingHot upgrades

Page 10: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Erlang concurrency philosophy

The Actor Model

Thinking Parallel

Thinking Processes

Page 11: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

• Behavior• State• Parallel• Asynchronous Messages Mailboxes• No Shared State

The Actor ModelCarl Hewitt

1973

Page 12: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

• 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.

Page 13: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Actor vs. OO workflowSynchronous Calls

Asynchronous Messages

Page 14: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"
Page 15: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

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

Page 16: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

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.

Page 17: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Erlang benefits

Functional naturePattern matching on steroidsConcurrencyFault ToleranceLet it crashDistributed ComputationDebugging and profilingHot upgrades

Page 18: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Fault-tolerant Erlang

Page 19: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Supervisors

Page 20: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Links and Monitors

Page 21: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Erlang benefits

Functional naturePattern matching on steroidsConcurrencyFault ToleranceLet it crashDistributed ComputationDebugging and profilingHot upgrades

Page 22: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Faulty subsystem are difficult to correct

P.S. Don’t bother, just restart

Page 23: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Erlang benefits

Functional naturePattern matching on steroidsConcurrencyFault ToleranceDistributed ComputationDebugging and profilingHot upgrades

Page 24: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Distribution

Node 1 Node 2

B ! MsgA B C

transparent message passing in cluster

Page 25: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

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

Page 26: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Erlang benefits

Functional naturePattern matching on steroidsConcurrencyFault ToleranceDistributed ComputationDebugging and profilingHot upgrades

Page 27: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Observer

Page 28: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

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

Page 29: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

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

Page 30: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Bottleneck detection

Page 31: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Erlang benefits

Functional naturePattern matching on steroidsConcurrencyFault ToleranceDistributed ComputationDebugging and profilingHot upgrades

Page 32: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Hot code upgrades

Page 33: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Erlang applications pie

Page 34: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

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

Page 35: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Top 5 lines of code

4629211703732827841479

Page 36: SE2016 Exotic Valerii Vasylkov "Erlang. Measurements and benefits"

Questions?