Your SlideShare is downloading. ×
CEUG: Introduction to OTP Behaviors, Part II - supervisors
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Saving this for later?

Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime - even offline.

Text the download link to your phone

Standard text messaging rates apply

CEUG: Introduction to OTP Behaviors, Part II - supervisors


Published on

Published in: Technology

1 Like
  • Be the first to comment

No Downloads
Total Views
On Slideshare
From Embeds
Number of Embeds
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

No notes for slide


  • 1. Overview Examples Erlang Supervision Erlang/OTP Supervision Tristan Sloughter ( June 2, 2010 Tristan Sloughter ( Erlang/OTP Supervision
  • 2. Overview Examples Erlang Supervision 1 Overview 2 Examples 3 Erlang Supervision Tristan Sloughter ( Erlang/OTP Supervision
  • 3. Overview Examples Erlang Supervision What is Supervision? Watches child processes Starts processes (or links to them) and is notified on their exit Tristan Sloughter ( Erlang/OTP Supervision
  • 4. Overview Examples Erlang Supervision Why Use Supervision? On failure a process can immediately be returned to a base state Keeps the system running even on failures Allows you to not rely on defensive coding to keep a system running for a long time Tristan Sloughter ( Erlang/OTP Supervision
  • 5. Overview Examples Erlang Supervision Real World Examples Chat Server : On failure restart with empty list of connected users (the base state) Streaming video : Video streaming in could be corrupted, if this causes a process to crash it can be quickly restarted and continue accepting data Tristan Sloughter ( Erlang/OTP Supervision
  • 6. Overview Examples Erlang Supervision Most Basic Example Tristan Sloughter ( Erlang/OTP Supervision
  • 7. Overview Examples Erlang Supervision A Supervision Tree Tristan Sloughter ( Erlang/OTP Supervision
  • 8. Overview Examples Erlang Supervision Classic Erlang Supervision Link processes A terminating process sends an exit signal to all linked processes Trap exits Sets the process to not exit on receiving exit signal Process instead receives message of the form: {’EXIT’, From, Reason} Tristan Sloughter ( Erlang/OTP Supervision
  • 9. Overview Examples Erlang Supervision Classic Supervisor supervisor_init() -> process_flag(trap_exit, true), Pid = chatserver:start_link(), supervisor(Pid, chatserver, start_link) supervisor(Pid, Module, Function) -> receive {’EXIT’, From, Reason} -> ... end. Tristan Sloughter ( Erlang/OTP Supervision
  • 10. Overview Examples Erlang Supervision The OTP Supervisor Behaviour Standard process for implementing supervision Provides preconfigured supervision strategies Tristan Sloughter ( Erlang/OTP Supervision
  • 11. Overview Examples Erlang Supervision OTP Supervisor Example init([]) -> RestartStrategy = one_for_one, MaxRestarts = 1000, MaxSecondsBetweenRestarts = 3600, SupFlags = {RestartStrategy, MaxRestarts, MaxSecondsBetweenRestarts}, AChild = {Name, {Module, Function, Args}, Restart, Shutdown, Type, Module}, {ok, {SupFlags, [AChild]}}. Tristan Sloughter ( Erlang/OTP Supervision
  • 12. Overview Examples Erlang Supervision Restart Strategies ... RestartStrategy = one_for_one, MaxRestarts = 1000, MaxSecondsBetweenRestarts = 3600, ... A supervisor has a restart strategy to follow when a child dies, which could effect other children The RestartStrategy variable in our example could be: one for one : Only the one process that dies is restarted one for all : All processes are started and restarted in order if one dies rest for one : Processes started after child that dies are stopped and then all restarted in order Tristan Sloughter ( Erlang/OTP Supervision
  • 13. Overview Examples Erlang Supervision Children Restart Strategies ... AChild = {Name, {Module, Function, Args}, Restart, Shutdown, Type, Module}, ... Each child process (worker) can have 1 of 3 strategies for the supervisor to follow when it exists The Restart variable in our example could be one of these: permanent : On ANY exit, restart transient : On abnormal exit, restart temporary : Never restart Tristan Sloughter ( Erlang/OTP Supervision
  • 14. Overview Examples Erlang Supervision One For All Tristan Sloughter ( Erlang/OTP Supervision
  • 15. Overview Examples Erlang Supervision Simple One For One Dynamic supervision Essentially a process factory Tristan Sloughter ( Erlang/OTP Supervision
  • 16. Overview Examples Erlang Supervision Scenarios Web Server : one for one with temporary children Media Server Encoder : one for one with permanent children Poker Server : one for all with transient children Facebook Session : simple one for one Tristan Sloughter ( Erlang/OTP Supervision
  • 17. Overview Examples Erlang Supervision Resources Simple-one-for-one - erlang-simple-one-for-one-supervisor/ Supervisor Design Principles - doc/design_principles/sup_princ.html Supervisor Behaviour - Tristan Sloughter ( Erlang/OTP Supervision