Erlang/OTP - Altenwald - CodeMotion Madrid 2013
-
Upload
manuel-angel-rubio-jimenez -
Category
Technology
-
view
769 -
download
2
description
Transcript of Erlang/OTP - Altenwald - CodeMotion Madrid 2013
![Page 1: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/1.jpg)
Erlang/OTPUn Mundo Concurrente
Manuel Ángel Rubio Jiménez
![Page 2: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/2.jpg)
¿Quién soy?
○ Programador desde los 12 años... unos 20 años programando en: Perl, Python, Ruby, PHP, Java, C/C++, JavaScript, Pascal, Modula-2, Basic y Erlang.
○ Administrador de sistemas desde los 22 años... unos 10 años administrando Windows, GNU/Linux y BSD.
○ En definitiva... DevOps.
○ Fundador de Altenwald y Freelance.
○ Contacto:
○ Blog: http://bosqueviejo.net
○ Twitter: @MRonErlang
![Page 3: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/3.jpg)
¿Qué es Erlang?
![Page 4: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/4.jpg)
¿Qué es Erlang?
○ Nació en 1986 como una extensión de Prolog en los laboratorios Ericsson.
○ Lenguaje○ ¿Funcional o no? ... mejor híbrido.○ Orientado a la Concurrencia... Modelo Actor
○ Máquina Virtual o Plataforma○ Gestión y Planificador de Procesos (soporta más de 1.000.000 procs)○ Gestor de Memoria○ Intérprete de comandos (shell)○ Interfaz transparente para comunicación entre nodos
○ Características○ Distribuido○ Tolerante a fallos○ Escalable○ Cambio de código en caliente
![Page 5: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/5.jpg)
¿Quién usa Erlang?
![Page 6: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/6.jpg)
Caso
○ En 2005, sistemas C++ y MySQL
○ Máx. 80 usuarios concurrentes
○ Crashes son muy frecuentes
○ En 2006, sistemas C++, Python y MySQL
○ Máx. 1.000 usuarios concurrentes
○ Requiere reinicios, el código es difícil de mantener... muchos errores
![Page 7: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/7.jpg)
Caso
○ En 2005, sistemas C++ y MySQL
○ Máx. 80 usuarios concurrentes
○ Crashes son muy frecuentes
○ En 2006, sistemas C++, Python y MySQL
○ Máx. 1.000 usuarios concurrentes
○ Requiere reinicios, el código es difícil de mantener... muchos errores
○ En 2007, sistemas Erlang, Python y MySQL
○ De 20.000 a 1.000.000 de usuarios concurrentes en ○ De 500 a 50.000 peticiones por segundo○ De 50 a 1.850 servidores
![Page 8: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/8.jpg)
Caso
![Page 9: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/9.jpg)
Progresión Informática
○ Carrera de los Hertzios vs Cores
Cuando estás en un atasco de tráfico con un Porsche, todo lo que puedes hacer es consumir más combustible que el resto estando parado. La escalabilidad va de construir
carreteras más anchas, no coches más rápidos.-- Steve Swartz
○ Programación Orientada a Objetos se atribuye a Alan Kay (Smalltalk)
○ Modelo Actor se atribuye a Carl Hewitt por un estudio de 1977.
![Page 10: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/10.jpg)
OOP vs Actor Model
![Page 11: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/11.jpg)
OOP vs Actor Model
![Page 12: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/12.jpg)
Apache vs Yaws
![Page 13: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/13.jpg)
Sintaxis de Erlang
Solo hay dos tipos de lenguajes:aquellos de los que la gente se queja y
aquellos que nadie usa.-- Bjarne Stroustrup
Ejemplo típico del factorial
![Page 14: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/14.jpg)
Sintaxis de Erlang
Solo hay dos tipos de lenguajes:aquellos de los que la gente se queja y
aquellos que nadie usa.-- Bjarne Stroustrup
Ejemplo típico del factorial (C)
factorial(int f) { int i; for (i=f-1; i>1; i--) { f *= i; } return f; }
![Page 15: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/15.jpg)
Sintaxis de Erlang
Solo hay dos tipos de lenguajes:aquellos de los que la gente se queja y
aquellos que nadie usa.-- Bjarne Stroustrup
Ejemplo típico del factorial (C recursivo)
factorial(int f) { if (f <= 1) { return f; } return f * factorial(f-1); }
![Page 16: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/16.jpg)
Sintaxis de Erlang
![Page 17: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/17.jpg)
Sintaxis de Erlang
Solo hay dos tipos de lenguajes:aquellos de los que la gente se queja y
aquellos que nadie usa.-- Bjarne Stroustrup
Ejemplo típico del factorial (Erlang)
factorial(0) -> 0; factorial(1) -> 1; factorial(N) -> N * factorial(N-1).
![Page 18: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/18.jpg)
○ Asignaciones únicas
> A = 1. 1 > A = 2. ** exception error: no match of right hand side value 2
○ Lenguaje simple: case, if, try...catch y receive.
case Value of 12 when is_integer(Value) -> "OK"; _ -> "FAIL"; end.
○ Paso de mensajes
Pid = spawn(fun micode/0), Pid ! "hola mundo!", receive Any -> io:format("OK") end.
Características del Lenguaje
![Page 19: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/19.jpg)
○ Capacidad numérica
1> fact:fact(128).385620482362580421735677065923463640617493109590223590278828403276373402575165543560686168588507361534030051833058916347592172932262498857766114955245039357760034644709279247692495585280000000000000000000000000000000
2> fact:fact(1024).541852879605885728307692194468385473800155396353801344448287027068321061207337660373314098413621458671907918845708980753931994165770187368260454133333721939108367528012764993769768292516937891165755680659663747947314518404886677672556125188694335251213677274521963430770133713205796248433128870088436171654690237518390452944732277808402932158722061853806162806063925435310822186848239287130261690914211362251144684713888587881629252104046295315949943900357882410243934315037444113890806181406210863953275235375885018598451582229599654558541242789130902486944298610923153307579131675745146436304024890820442907734561827369030502252796926553072967370990758747793127635104702469889667961462133026237158973227857814631807156427767644064591085076564783456324457736853810336981776080498707767046394272605341416779125697733374568037475186676265961665615884681450263337042522664141862157046825684773360944326737493676674915098953768112945831626643856479027816385730291542667725665642276826058264393884514911976419675509290208592713156362983290989441052732125187249527501314071676405516936190781821236701912295767363117054126589929916482008515781751955466910902838729232224509906388638147771255227782631322385756948819393658889908993670874516860653098...
Características del Lenguaje
![Page 20: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/20.jpg)
○ Matching
> "Hola " ++ Quien = "Hola mundo!". "Hola mundo!" > Quien. "mundo!"
○ Conjuntos
> A = [1,2,3,4,5], B = [2,4,6], A -- B. [1,3,5] > (A -- B) ++ (B -- A). [1,3,5,6]
○ Binarios
{ok, PNG} = file:open("debian-logo.png", [read,binary]), {ok, <<137,"PNG",13,10,26,10,Length:32,"IHDR">>} = file:read(PNG, 16), {ok, <<Width:32, Height:32, Depth:8, Color:8>>} = file:read(PNG, 10), {ok, <<Compression:8, Filter:8, Interlace:8>>} = file:read(PNG, 3), file:close(PNG).
Características del Lenguaje
![Page 21: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/21.jpg)
○ Listas de Comprensión
> [ X || X <- lists:seq(1,10), X rem 2 =:= 0 ]. [2,4,6,8,10] > [ {X,2,X*2} || X <- lists:seq(1,10) ]. [{1,2,2}, {2,2,4}, {3,2,6}, {4,2,8}, {5,2,10}, {6,2,12}, {7,2,14}, {8,2,16}, {9,2,18}, {10,2,20}]
> [ X || X <- [1,5,9,3,2,6,5,4], X >= 5 ]. [5,9,6,5]
Características del Lenguaje
![Page 22: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/22.jpg)
Recursividad-module(mergesort).
-export([ordena/1]).
ordena([]) -> [];
ordena([H]) -> [H];
ordena(L) ->
{L1,L2} = separa(L),
mezcla(ordena(L1), ordena(L2)).
separa(L) ->
lists:split(length(L) div 2, L).
mezcla([], L) -> L;
mezcla(L, []) -> L;
mezcla([H1|T1], [H2|_]=L2) when H1 =< H2 ->
[H1|mezcla(T1,L2)];
mezcla(L1, [H2|T2]) ->
[H2|mezcla(L1,T2)].
2 5 1 3 4 0
2 5 1 3 4 0
2 5 1 3 4 0
5 1 4 0
1 5 0 4
21 5 0 3 4
0 1 2 3 4 5
![Page 23: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/23.jpg)
Recursividad-module(quicksort).
-export([ordena/1]).
ordena([]) -> [];
ordena([H]) -> [H];
ordena(L) ->
{L1, [Pivote], L2} = separa(L),
mezcla(ordena(L1) ++ [Pivote], ordena(L2)).
separa([]) -> {[], [], []};
separa([H]) -> {[H], [], []};
separa([Pivote|T]) ->
ListaMenores = [ X || X <- T, X =< Pivote ],
ListaMayores = [ X || X <- T, X > Pivote ],
{ListaMenores, [Pivote], ListaMayores}.
mezcla(L1, L2) -> L1 ++ L2.
2 5 1 3 4 0
1 0 2 5 3 4
0 1 2 3 4 5
0 1 3 4 5
4 5
0 1 2 3 4 5
![Page 24: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/24.jpg)
○ Conectando nodos
$ erl -sname bosqueviejo Erlang R15B02 (erts-5.9.2) [64-bit] [smp:8:8] [async-threads:0] [hipe]
Eshell V5.9.2 (abort with ^G) (bosqueviejo@bosque)1>
$ erl -sname bosquenegro Erlang R15B02 (erts-5.9.2) [64-bit] [smp:8:8] [async-threads:0] [hipe]
Eshell V5.9.2 (abort with ^G) (bosquenegro@bosque)1> net_kernel:connect_node(bosqueviejo@bosque). true (bosquenegro@bosque)2> nodes(). [bosqueviejo@bosque]
(bosqueviejo@bosque)1> nodes(). [bosquenegro@bosque]
Distribución
![Page 25: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/25.jpg)
Comportamientos - OTP
○ gen_server: servidores genéricos, actores base.
○ gen_fsm: máquinas de estados finitos.
○ gen_event: manejadores de eventos.
○ supervisor: supervisión de procesos.
○ application: estructura de aplicación.
Sup
Sup Srv FSM
Srv
App
![Page 26: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/26.jpg)
-module(ascensor).-behaviour(gen_fsm).-compile([export_all]). % para simplificar, cambiar por -export(). start_link() -> gen_fsm:start_link({local, ?MODULE}, ?MODULE, [], []). init([]) -> {ok, planta_baja, []}. planta_baja(bajar, State) -> io:format("Beeep!, opcion incorrecta~n", []), {next_state, planta_baja, State};planta_baja(subir, State) -> io:format("Subiendo a la planta primera~n", []), {next_state, planta_primera, State}. planta_primera(bajar, State) -> io:format("Bajando a la planta baja~n", []), {next_state, planta_baja, State};planta_primera(subir, State) -> io:format("Subiendo a la planta segunda~n", []), {next_state, planta_segunda, State}. planta_segunda(bajar, State) -> io:format("Bajando a la planta primera~n", []), {next_state, planta_primera, State};planta_segunda(subir, State) -> io:format("Beeep!, opcion incorrecta~n", []), {next_state, planta_segunda, State}. % agregamos funciones para facilitar las llamadas% estas son opcionales: boton_subir() -> gen_fsm:send_event(?MODULE, subir). boton_bajar() -> gen_fsm:send_event(?MODULE, bajar).
EjemploFiniteStateMachine
![Page 27: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/27.jpg)
Libros en inglés
![Page 28: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/28.jpg)
Libro en castellano
Descarga PDF gratuita
http://erlang.bosqueviejo.net
Compra en Papel
![Page 29: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/29.jpg)
¿Preguntas?
![Page 30: Erlang/OTP - Altenwald - CodeMotion Madrid 2013](https://reader034.fdocumento.com/reader034/viewer/2022042700/559134cc1a28abc4628b47e7/html5/thumbnails/30.jpg)
Agradecimientos
○ Organización de CodeMotion
○ Universidad Politécnica de Madrid
○ ¡A todos vosotros por asistir!