Let It Crash
An introduction to Erlang/OTP supervisors
Tuesday, February 28, 12
Slide 2
Slide 2 text
When it comes to error-handling I have shown how to “abstract
out” the errors, and argued that the program should be divided
into “pure” code and code which “fixes the errors.”
– Joe Armstrong
Tuesday, February 28, 12
def process(message):
result = ""
if isinstance(message, str):
split_message = message.split()
if len(split_message) == 3:
a, operator, b = split_message
if a.isdigit() and b.isdigit():
result = int(a) + int(b)
return result
Tuesday, February 28, 12
Slide 5
Slide 5 text
def process(message):
result = ""
if isinstance(message, str):
split_message = message.split()
if len(split_message) == 3:
a, operator, b = split_message
if a.isdigit() and b.isdigit():
result = int(a) + int(b)
return result
Tuesday, February 28, 12
Can Communicate
A B
Mailbox A
Mailbox B
Tuesday, February 28, 12
Slide 11
Slide 11 text
Can Communicate
A B
Mailbox A
Mailbox B
Tuesday, February 28, 12
Slide 12
Slide 12 text
Can Communicate
A B
Mailbox A
Mailbox B
Tuesday, February 28, 12
Slide 13
Slide 13 text
Can Communicate
A B
Mailbox A
Mailbox B
Tuesday, February 28, 12
Slide 14
Slide 14 text
Distributable
Machine A
Tuesday, February 28, 12
Slide 15
Slide 15 text
Distributable
Machine A Machine B Machine C
Tuesday, February 28, 12
Slide 16
Slide 16 text
Process Links
Tuesday, February 28, 12
Slide 17
Slide 17 text
B C D
A
Tuesday, February 28, 12
Slide 18
Slide 18 text
B C D
A
Tuesday, February 28, 12
Slide 19
Slide 19 text
B C D
A
Tuesday, February 28, 12
Slide 20
Slide 20 text
B C D
A
Tuesday, February 28, 12
Slide 21
Slide 21 text
B D
A
Tuesday, February 28, 12
Slide 22
Slide 22 text
B D
Tuesday, February 28, 12
Slide 23
Slide 23 text
Errors
Tuesday, February 28, 12
Slide 24
Slide 24 text
% all functions will return a value or
% generate an exception
Tuesday, February 28, 12
Slide 25
Slide 25 text
% all functions will return a value or
% generate an exception
% you can cause exceptions manually:
exit(Pid, Reason).
Tuesday, February 28, 12
Slide 26
Slide 26 text
% all functions will return a value or
% generate an exception
% you can cause exceptions manually:
exit(Pid, Reason).
% or they will be raised for you:
add(A,B) -> A + B.
add(foo, 2). % {'EXIT',{badarith,[{intro,add,2}, ...
Tuesday, February 28, 12
Slide 27
Slide 27 text
% if a process exits all linked processes
% will receive an exit signal and exit
Tuesday, February 28, 12
Slide 28
Slide 28 text
% if a process exits all linked processes
% will receive an exit signal and exit
% if the exit message is the atom ‘normal’
% the linked processes will not exit
exit(normal).
Tuesday, February 28, 12
Slide 29
Slide 29 text
% if a process exits all linked processes
% will receive an exit signal and exit
% if the exit message is the atom ‘normal’
% the linked processes will not exit
exit(normal).
% we can stop linked processes from exiting
% by setting the trap_exit process flag
process_flag(trap_exit, true).
Tuesday, February 28, 12
Slide 30
Slide 30 text
% if a process exits all linked processes
% will receive an exit signal and exit
% if the exit message is the atom ‘normal’
% the linked processes will not exit
exit(normal).
% we can stop linked processes from exiting
% by setting the trap_exit process flag
process_flag(trap_exit, true).
% we can override trap_exit by setting the
% exit message to the atom ‘kill’
exit(kill).
Tuesday, February 28, 12
Slide 31
Slide 31 text
OTP
Tuesday, February 28, 12
Slide 32
Slide 32 text
• open telephone platform
• libraries
• behaviors
Tuesday, February 28, 12
Slide 33
Slide 33 text
Supervisors
Tuesday, February 28, 12
Slide 34
Slide 34 text
• monitor child processes
• handle restarts on errors
• trap_exit
Tuesday, February 28, 12
{
Id, % atom identifying the process
StartFunc,
Restart,
Shutdown,
Type,
Modules
}
Tuesday, February 28, 12
Slide 38
Slide 38 text
{
Id, % atom identifying the process
StartFunc, % {Module, Function, Args}
Restart,
Shutdown,
Type,
Modules
}
Tuesday, February 28, 12
Slide 39
Slide 39 text
{
Id, % atom identifying the process
StartFunc, % {Module, Function, Args}
Restart, % atom ‘permanent’, ‘temporary’ or ‘transient’
Shutdown,
Type,
Modules
}
Tuesday, February 28, 12
Slide 40
Slide 40 text
{
Id, % atom identifying the process
StartFunc, % {Module, Function, Args}
Restart, % atom ‘permanent’, ‘transient’ or ‘temporary’
Shutdown, % atom ‘brutal_kill’, ‘infinity’ or an integer
Type,
Modules
}
Tuesday, February 28, 12
Slide 41
Slide 41 text
{
Id, % atom identifying the process
StartFunc, % {Module, Function, Args}
Restart, % atom ‘permanent’, ‘transient’ or ‘temporary’
Shutdown, % atom ‘brutal_kill’, ‘infinity’ or an integer
Type, % atom ‘worker’ or ‘supervisor’
Modules
}
Tuesday, February 28, 12
Slide 42
Slide 42 text
{
Id, % atom identifying the process
StartFunc, % {Module, Function, Args}
Restart, % atom ‘permanent’, ‘transient’ or ‘temporary’
Shutdown, % atom ‘brutal_kill’, ‘infinity’ or an integer
Type, % atom ‘worker’ or ‘supervisor’
Modules % list of used modules
}
Tuesday, February 28, 12
Slide 43
Slide 43 text
{
Id, % atom identifying the process
StartFunc, % {Module, Function, Args}
Restart, % atom ‘permanent’, ‘transient’ or ‘temporary’
Shutdown, % atom ‘brutal_kill’, ‘infinity’ or an integer
Type, % atom ‘worker’ or ‘supervisor’
Modules % list of used modules
}
Tuesday, February 28, 12
• Erlang and OTP in Action
http://www.manning.com/logan/
• Learn you some Erlang for Great Good
http://learnyousomeerlang.com/
• erldocs
http://erldocs.com/
Tuesday, February 28, 12
Slide 71
Slide 71 text
joinbraintree.com
Tuesday, February 28, 12
Slide 72
Slide 72 text
Thanks!
@benemills / github.com/benmills
Tuesday, February 28, 12