58
You Shall Not Get Excited Ivan Ribeiro Rocha [email protected] @irr

You shall not get excited

  • Upload
    x697272

  • View
    3.073

  • Download
    3

Embed Size (px)

DESCRIPTION

ffffuuuuconf 2010 presentation

Citation preview

Page 1: You shall not get excited

You Shall Not Get

Excited

Ivan Ribeiro [email protected]

@irr

Page 2: You shall not get excited

Agenda● Why not?

● ”History” about getting excited...

● What should I use...?

● My choice: Erlang● Learning from experience...

Page 3: You shall not get excited

Why not?

Page 4: You shall not get excited

”Once upon a time...”

”History” about getting excited...

Page 5: You shall not get excited

(what is a ”technical” manager?)

Page 6: You shall not get excited

…a ”technical”manager meets...

Page 7: You shall not get excited

… a ”smart guy”

(a.k.a salesman)

Page 8: You shall not get excited

Deal! You deserve it!

Page 9: You shall not get excited

”no problem... we can fix it!”

Page 10: You shall not get excited

”relax... the guys will change

it in the upcoming version...”

Page 11: You shall not get excited

sometimes is not so easy to

fix all problems

Page 12: You shall not get excited

”Ignorance more frequently begets

confidence than does

knowledge.”Charles Darwin

Page 13: You shall not get excited

What should I use...?

Page 14: You shall not get excited

Programming Languagesand tools...

● Languages

– Java, Ruby, Python, Perl, ...

– C/C++, Lisp, Haskell, Ocaml, Erlang, ...

● Web Servers

– Apache, nginx, ...

– Jetty/Tomcat, ...

– Yaws, inets, ...

Page 15: You shall not get excited

Anyone using it?

Page 16: You shall not get excited

Is it stable?

Page 17: You shall not get excited

Does it scale?

Page 18: You shall not get excited

(think about...)

You're not:

Page 19: You shall not get excited

what means

scalable?- don’t design to scale infinitely

- consider 5X - 50X growth

- but > 100X requires redesign- break large systems into smaller services

Jeff Dean

http://bit.ly/clIJfL

Page 20: You shall not get excited
Page 21: You shall not get excited
Page 22: You shall not get excited
Page 23: You shall not get excited

no documentation...?● Good APIs?● Available books?● % Comments...?● Source code?

● Please! No ”magazines”...

Page 24: You shall not get excited

still in doubt...?

Page 25: You shall not get excited
Page 26: You shall not get excited

Joe ArmstrongRobert Virding

Mike WilliamsClaes “Klacke” Wikström

and more...

My choice: Erlang

Page 27: You shall not get excited
Page 28: You shall not get excited

Learning from experience...

Page 29: You shall not get excited

● > 3 years and learning...

● back end development

–REST API

– memcached API

– syslog API

– MySQL and Mnesia (data storage)

Page 30: You shall not get excited

● functional + concurrent programming

● fault-tolerant and highly scalable

● very light-weight concurrency (processes)

● running massive systems for 20 years

● bit syntax and binaries (for protocol programming)

● links (encourages “let it crash” programming)

● ”concurrency belongs to the language and not the operating system”

Page 31: You shall not get excited

Erlang(Why you should get excited...)

Concurrent Programming

– lots of processes

● the only way for processes to interact is

through message passing

–eventbased (epoll)

Page 32: You shall not get excited

Erlang(Why you should get excited...)

No shared memory

– ”sharing is the property that preventsfault tolerance”

– destructive shared data modifications

do not occur!

Page 33: You shall not get excited

Erlang(Why you should get excited...)

It's easy to test

and update your code

Page 34: You shall not get excited

Erlang(Why you should get excited...)

HTTP/JSON support

– yaws (embedded)

– mochiweb (json)– misultin

–inets

– httpc

Page 35: You shall not get excited

Erlang(Why you should get excited...)

many Libraries/tools for distributed programs...

– net_[adm|kernel] net_kernel:connect_node(N). net_adm:ping(N).

– rpc {R, E} = rpc:multicall(nodes(), Mod, Fun, [N], T), lists:foldl(fun(L, A) -> merge(A, [X || X <- L, not member(X, A)], N) end, C, R),

– epmd...

Page 36: You shall not get excited

Erlang(Why you should get excited...)

”Mnesia is a distributed DataBase Management System

(DBMS), appropriate for telecommunications applications and other Erlang applications which require continuous operation

and exhibit soft real-time properties”

Mnesia is greatbut you should really know how to use it

and you MUST architecture your applicationto get the best results...

Page 37: You shall not get excited

Erlang(Why you should get excited...)

OTP (Unix <-> C <==> OTP <-> Erlang)

”It’s an application operating system and a set of libraries and procedures used for building

large-scale, fault-tolerant, distributed applications”

– supervisor– applications

– gen_server– gen_fsm

– gen_event

Page 38: You shall not get excited

”We should forget about small efficiencies, say about 97% of the time: premature optimization is the root of all evil.”

Donald Knuth

Page 39: You shall not get excited

Erlang(caveats: You must be careful...)

Mnesia– can't handle very large data

–2 Gb

● ETS● DETS●MNESIA

Page 40: You shall not get excited

Erlang(caveats: You must be careful...)

Mnesia–2 Gb fragments (mod (2^X))

– avoid rehash: create more fragments...● add● move● del*

Page 41: You shall not get excited

Erlang(caveats: You must be careful...)

Mnesia–CAP theorem

–replica x partitioned networks

Page 42: You shall not get excited

Erlang(caveats: You must be careful...)

Mnesia– need to check replica consistency?

● vector clocks... (avoid dependencies)

● should try timestamps

Page 43: You shall not get excited

Erlang(caveats: You must be careful...)

Mnesia– event "running partitioned network"

● mnesia:set_master_nodes(L).

● must restart other nodes

– if some node will not recover soon...

● mnesia:force_load_table(T).

Page 44: You shall not get excited

Erlang(caveats: You must be careful...)

Mnesia (QLC)● avoid retrieve large data sets● use cursors inside

transactions mnesia:activity(sync_transaction, fun(X) -> QC = qlc:cursor(X), QR = qlc:next_answers(QC, N), qlc:delete_cursor(QC), QR end, [qlc:q([E || E <- mnesia:table(Tab)])], mnesia_frag).

Page 45: You shall not get excited

Erlang(caveats: You must be careful...)

Mnesia– load balance (read)

– avoid ”overload” one instance mnesia_lib:set({T, where_to_read}, Node)

Page 46: You shall not get excited

Erlang(caveats: You must be careful...)

Logging– avoid overhead

● yaws● error_logger

– syslog● udp● tcp*● gen_server

+ handle_cast

Page 47: You shall not get excited

Erlang(caveats: You must be careful...)

Messages– like a mailbox– per process (don't forget to read your messages)

Page 48: You shall not get excited

Erlang(caveats: You must be careful...)

OTP–only API

– poor docs, books, articles...

Page 49: You shall not get excited

Erlang(caveats: You must be careful...)

OTP– avoid ”synchronize” long calls

test(X) -> gen_server:call(?MODULE, {test, X}, 5000).

handle_call({test, X}, From, State) → spawn(fun() → %% do some long task gen_server:reply(From, Reply) end), {noreply, State}.

Page 50: You shall not get excited

Erlang(caveats: You must be careful...)

Security– must be treated externally

● firewall

● private networks

– cookie based

-kernel inet_dist_use_interface {127,0,0,1}-kernel inet_dist_listen_min <min>-kernel inet_dist_listen_max <max>

> erl -kernel inet_dist_listen_min 9001 inet_dist_listen_max 9005(4369 TCP port, as it is used by epmd, ERL_EPMD_PORT environment variable)

Page 51: You shall not get excited

Erlang(caveats: You must be careful...)

Code swapping/replacement– Be careful with spawn inside old (replaced) module... it will die!– The code of a module can exist in two variants in a system: current

and old (fully qualified function calls always refer to current code)

– If a third instance of the module is loaded, the code server will remove (purge) the old code and any processes

lingering in it will be terminated -module(m). -export([loop/0]).

loop() -> receive code_switch -> m:loop(); Msg -> % ...

loop()end.

Page 52: You shall not get excited

Undocumented features...

you must be careful!

Page 53: You shall not get excited

Erlang(caveats: You must be careful...)

Using async acceptors (prim_inet)– gen_tcp:accept(Listen)

Usages: https://github.com/irr/erl-tutorials/blob/master/ts/ts1/src/ts.erl

– prim_inet:async_accept(Socket, -1)Usages:https://github.com/irr/erl-tutorials/blob/master/ts/ts2/src/ts.erl

http://svn.apache.org/viewvc/incubator/thrift/trunk/thrift_server.erlhttp://www.trapexit.org/Building_a_Non-blocking_TCP_server_using_OTP_principles

”It is undocumented because it is an internal module that is not ment to be called from applications. Its interface may

change without warning in even the smallest patch.”

Page 54: You shall not get excited

Erlang(caveats: You must be careful...)

Using socket in http modecase gen_tcp:listen(Port, [binary, {packet, http}, {reuseaddr, true}, {active, false}, {backlog, 30}]) of ...

case gen_tcp:recv(C#c.sock, 0, ?server_idle_timeout) of {ok, {http_header, _, 'Content-Length', _, Val}} ->... {error, {http_error, "\r\n"}} ->... {ok, http_eoh} ->...

Usage:http://www.trapexit.org/A_fast_web_server_demonstrating_some_undocumented_Erlang_features

Page 55: You shall not get excited

Erlang(caveats: You must be careful...)

1> inet:ifget("eth0", [addr]).

{ok,[{addr,{192,168,1,101}}]}

2> inet:getiflist().

{ok,["lo","eth0"]}

3> inet_parse:ntoa({192,168,1,101}).

"192.168.1.101"

4> inet_parse:address("192.168.1.101").

{ok,{192,168,1,101}}

Page 56: You shall not get excited
Page 57: You shall not get excited

there is no

silverbullet!

Page 58: You shall not get excited

Any

doubts?