Slide 1

Slide 1 text

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

Slide 3

Slide 3 text

process("1 + 1") # 2 process("foo") # "" process("1") # "" process(1) # "" Tuesday, February 28, 12

Slide 4

Slide 4 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

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

Slide 6

Slide 6 text

process(Message) -> [A, _, B] = string:tokens(Message, " "), list_to_integer(A) + list_to_integer(B). Tuesday, February 28, 12

Slide 7

Slide 7 text

Processes Tuesday, February 28, 12

Slide 8

Slide 8 text

Isolated Tuesday, February 28, 12

Slide 9

Slide 9 text

Isolated Tuesday, February 28, 12

Slide 10

Slide 10 text

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

Slide 35

Slide 35 text

Child Specs Tuesday, February 28, 12

Slide 36

Slide 36 text

{ Id, StartFunc, Restart, Shutdown, Type, Modules } Tuesday, February 28, 12

Slide 37

Slide 37 text

{ 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

Slide 44

Slide 44 text

{ simple_server, {simple_server, start_link, []}, permanent, 5000, worker, [...] } Tuesday, February 28, 12

Slide 45

Slide 45 text

Restart Strategies Tuesday, February 28, 12

Slide 46

Slide 46 text

one_f0r_one supervisor worker Tuesday, February 28, 12

Slide 47

Slide 47 text

supervisor worker one_f0r_one Tuesday, February 28, 12

Slide 48

Slide 48 text

supervisor one_f0r_one Tuesday, February 28, 12

Slide 49

Slide 49 text

supervisor new worker one_f0r_one Tuesday, February 28, 12

Slide 50

Slide 50 text

one_for_all supervisor worker worker worker Tuesday, February 28, 12

Slide 51

Slide 51 text

one_for_all supervisor worker worker worker Tuesday, February 28, 12

Slide 52

Slide 52 text

one_for_all supervisor worker worker Tuesday, February 28, 12

Slide 53

Slide 53 text

one_for_all supervisor new worker new worker new worker Tuesday, February 28, 12

Slide 54

Slide 54 text

rest_for_one supervisor 1 2 3 Tuesday, February 28, 12

Slide 55

Slide 55 text

rest_for_one supervisor 1 3 2 Tuesday, February 28, 12

Slide 56

Slide 56 text

rest_for_one supervisor 1 3 3 2 Tuesday, February 28, 12

Slide 57

Slide 57 text

rest_for_one supervisor 2 3 1 Tuesday, February 28, 12

Slide 58

Slide 58 text

simple_one_for_one supervisor Tuesday, February 28, 12

Slide 59

Slide 59 text

simple_one_for_one supervisor 1 Tuesday, February 28, 12

Slide 60

Slide 60 text

simple_one_for_one supervisor 1 2 Tuesday, February 28, 12

Slide 61

Slide 61 text

simple_one_for_one supervisor 1 2 n Tuesday, February 28, 12

Slide 62

Slide 62 text

Full Example Tuesday, February 28, 12

Slide 63

Slide 63 text

{ok, { {one_for_one, 10, 60}, [{ simple_server, {simple_server, start_link, []}, permanent, 5000, worker, [...] }] }} Tuesday, February 28, 12

Slide 64

Slide 64 text

{ok, { {one_for_one, 10, 60}, [{ simple_server, {simple_server, start_link, []}, permanent, 5000, worker, [...] }] }} Tuesday, February 28, 12

Slide 65

Slide 65 text

{ok, { {one_for_one, 10, 60}, [{ simple_server, {simple_server, start_link, []}, permanent, 5000, worker, [...] }] }} Tuesday, February 28, 12

Slide 66

Slide 66 text

{ok, { {RestartStrategy, MaxRestart, MaxTime}, [{ simple_server, {simple_server, start_link, []}, permanent, 5000, worker, [...] }] }} Tuesday, February 28, 12

Slide 67

Slide 67 text

{ok, { {one_for_one, 10, 60}, [{ simple_server, {simple_server, start_link, []}, permanent, 5000, worker, [...] }] }} Tuesday, February 28, 12

Slide 68

Slide 68 text

{ok, { {RestartStrategy, MaxRestart, MaxTime}, ChildSpecs }} Tuesday, February 28, 12

Slide 69

Slide 69 text

Example Tuesday, February 28, 12

Slide 70

Slide 70 text

• 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