Geovane Fedrecheski
Universidade de São Paulo
Escola Politécnica da USP
geonnave@gmail.com | geovane@lsi.usp.br
In early 80s, Ericsson Telecom engineers found no programming language suited to provide:
A research was then conducted..
..whose outcome was Erlang
Erlang can be divided in three levels:
{:ok, pid_a} = spawn(fn -> IO.puts "Hello World!" end)
Spawns a new process
an anonymous function is the argument
{:error, reason} = spawn(fn -> raise "an exception!" end)
Erlang processes are not OS processes
Instead, they run inside the Erlang VM
No shared memory
message passing
Processes must communicate
{:ok, pid_b} = spawn(fn -> # Spawns a concurrent process.
receive do # Awaits for an incoming message.
msg -> IO.puts msg
end
end)
{:ok, pid_a} = spawn(fn -> # Spawns a concurrent process.
send(pid_b, "Hello") # Sends a message to the process
# identified by `pid_b`.
end)
spawn_link(fn -> raise "I die, so does my father" end)
Process.flag(:trap_exit, true)
spawn_link(fn -> raise "I die, but not my father" end)
We have:
The idea: the father can take action on children's termination
import Supervisor.Spec
children = [
worker(Cache, []),
worker(DatabaseWorker, []),
worker(TCP.Acceptor, [4040])
]
Supervisor.start_link(children, strategy: :one_for_one)
Restart Strategies:
Restart Frequency:
Open Telecom Platform
REPL
(Read-Eval-
Print-Loop)
Observer
Elixir is
-module(sum_server).
-behaviour(gen_server).
-export([
start/0, sum/3,
init/1, handle_call/3, handle_cast/2, handle_info/2, terminate/2,
code_change/3
]).
start() -> gen_server:start(?MODULE, [], []).
sum(Server, A, B) -> gen_server:call(Server, {sum, A, B}).
init(_) -> {ok, undefined}.
handle_call({sum, A, B}, _From, State) -> {reply, A + B, State};
handle_cast(_Msg, State) -> {noreply, State}.
handle_info(_Info, State) -> {noreply, State}.
terminate(_Reason, _State) -> ok.
code_change(_OldVsn, State, _Extra) -> {ok, State}.
defmodule SumServer do
use GenServer
def start do
GenServer.start(__MODULE__, nil)
end
def sum(server, a, b) do
GenServer.call(server, {:sum, a, b})
end
def handle_call({:sum, a, b}, _from, state) do
{:reply, a + b, state}
end
end
Topics selected from Johnson, 1989, to achieve Fault Tolerance:
A qualitative analysis was performed