Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Let it crash

725a7e2a994cae4c110ae07de4d0a66d?s=47 benmills
February 28, 2012

Let it crash

Learn about the "Let it crash" philosophy popularized by Erlang and an introduction to OTP supervisors.

725a7e2a994cae4c110ae07de4d0a66d?s=128

benmills

February 28, 2012
Tweet

More Decks by benmills

Other Decks in Programming

Transcript

  1. Let It Crash An introduction to Erlang/OTP supervisors Tuesday, February

    28, 12
  2. 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
  3. process("1 + 1") # 2 process("foo") # "" process("1") #

    "" process(1) # "" Tuesday, February 28, 12
  4. 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
  5. 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
  6. process(Message) -> [A, _, B] = string:tokens(Message, " "), list_to_integer(A)

    + list_to_integer(B). Tuesday, February 28, 12
  7. Processes Tuesday, February 28, 12

  8. Isolated Tuesday, February 28, 12

  9. Isolated Tuesday, February 28, 12

  10. Can Communicate A B Mailbox A Mailbox B Tuesday, February

    28, 12
  11. Can Communicate A B Mailbox A Mailbox B Tuesday, February

    28, 12
  12. Can Communicate A B Mailbox A Mailbox B Tuesday, February

    28, 12
  13. Can Communicate A B Mailbox A Mailbox B Tuesday, February

    28, 12
  14. Distributable Machine A Tuesday, February 28, 12

  15. Distributable Machine A Machine B Machine C Tuesday, February 28,

    12
  16. Process Links Tuesday, February 28, 12

  17. B C D A Tuesday, February 28, 12

  18. B C D A Tuesday, February 28, 12

  19. B C D A Tuesday, February 28, 12

  20. B C D A Tuesday, February 28, 12

  21. B D A Tuesday, February 28, 12

  22. B D Tuesday, February 28, 12

  23. Errors Tuesday, February 28, 12

  24. % all functions will return a value or % generate

    an exception Tuesday, February 28, 12
  25. % all functions will return a value or % generate

    an exception % you can cause exceptions manually: exit(Pid, Reason). Tuesday, February 28, 12
  26. % 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
  27. % if a process exits all linked processes % will

    receive an exit signal and exit Tuesday, February 28, 12
  28. % 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
  29. % 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
  30. % 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
  31. OTP Tuesday, February 28, 12

  32. • open telephone platform • libraries • behaviors Tuesday, February

    28, 12
  33. Supervisors Tuesday, February 28, 12

  34. • monitor child processes • handle restarts on errors •

    trap_exit Tuesday, February 28, 12
  35. Child Specs Tuesday, February 28, 12

  36. { Id, StartFunc, Restart, Shutdown, Type, Modules } Tuesday, February

    28, 12
  37. { Id, % atom identifying the process StartFunc, Restart, Shutdown,

    Type, Modules } Tuesday, February 28, 12
  38. { Id, % atom identifying the process StartFunc, % {Module,

    Function, Args} Restart, Shutdown, Type, Modules } Tuesday, February 28, 12
  39. { Id, % atom identifying the process StartFunc, % {Module,

    Function, Args} Restart, % atom ‘permanent’, ‘temporary’ or ‘transient’ Shutdown, Type, Modules } Tuesday, February 28, 12
  40. { 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
  41. { 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
  42. { 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
  43. { 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
  44. { simple_server, {simple_server, start_link, []}, permanent, 5000, worker, [...] }

    Tuesday, February 28, 12
  45. Restart Strategies Tuesday, February 28, 12

  46. one_f0r_one supervisor worker Tuesday, February 28, 12

  47. supervisor worker one_f0r_one Tuesday, February 28, 12

  48. supervisor one_f0r_one Tuesday, February 28, 12

  49. supervisor new worker one_f0r_one Tuesday, February 28, 12

  50. one_for_all supervisor worker worker worker Tuesday, February 28, 12

  51. one_for_all supervisor worker worker worker Tuesday, February 28, 12

  52. one_for_all supervisor worker worker Tuesday, February 28, 12

  53. one_for_all supervisor new worker new worker new worker Tuesday, February

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

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

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

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

  58. simple_one_for_one supervisor Tuesday, February 28, 12

  59. simple_one_for_one supervisor 1 Tuesday, February 28, 12

  60. simple_one_for_one supervisor 1 2 Tuesday, February 28, 12

  61. simple_one_for_one supervisor 1 2 n Tuesday, February 28, 12

  62. Full Example Tuesday, February 28, 12

  63. {ok, { {one_for_one, 10, 60}, [{ simple_server, {simple_server, start_link, []},

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

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

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

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

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

    12
  69. Example Tuesday, February 28, 12

  70. • 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
  71. joinbraintree.com Tuesday, February 28, 12

  72. Thanks! @benemills / github.com/benmills Tuesday, February 28, 12