SlideShare ist ein Scribd-Unternehmen logo
1 von 17
Downloaden Sie, um offline zu lesen
Overview
                                Examples
                       Erlang Supervision




                   Erlang/OTP Supervision

         Tristan Sloughter (kungfooguru@gmail.com)



                                 June 2, 2010




Tristan Sloughter (kungfooguru@gmail.com)   Erlang/OTP Supervision
Overview
                                      Examples
                             Erlang Supervision




1   Overview


2   Examples


3   Erlang Supervision




      Tristan Sloughter (kungfooguru@gmail.com)   Erlang/OTP Supervision
Overview
                                      Examples
                             Erlang Supervision


What is Supervision?




      Watches child processes
      Starts processes (or links to them) and is notified on their exit




      Tristan Sloughter (kungfooguru@gmail.com)   Erlang/OTP Supervision
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 (kungfooguru@gmail.com)   Erlang/OTP Supervision
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 (kungfooguru@gmail.com)   Erlang/OTP Supervision
Overview
                                     Examples
                            Erlang Supervision


Most Basic Example




     Tristan Sloughter (kungfooguru@gmail.com)   Erlang/OTP Supervision
Overview
                                      Examples
                             Erlang Supervision


A Supervision Tree




      Tristan Sloughter (kungfooguru@gmail.com)   Erlang/OTP Supervision
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 (kungfooguru@gmail.com)   Erlang/OTP Supervision
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 (kungfooguru@gmail.com)   Erlang/OTP Supervision
Overview
                                     Examples
                            Erlang Supervision


The OTP Supervisor Behaviour




     Standard process for implementing supervision
     Provides preconfigured supervision strategies




     Tristan Sloughter (kungfooguru@gmail.com)   Erlang/OTP Supervision
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 (kungfooguru@gmail.com)   Erlang/OTP Supervision
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 (kungfooguru@gmail.com)   Erlang/OTP Supervision
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 (kungfooguru@gmail.com)   Erlang/OTP Supervision
Overview
                                      Examples
                             Erlang Supervision


One For All




      Tristan Sloughter (kungfooguru@gmail.com)   Erlang/OTP Supervision
Overview
                                     Examples
                            Erlang Supervision


Simple One For One




     Dynamic supervision
     Essentially a process factory




     Tristan Sloughter (kungfooguru@gmail.com)   Erlang/OTP Supervision
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 (kungfooguru@gmail.com)   Erlang/OTP Supervision
Overview
                                      Examples
                             Erlang Supervision


Resources



     Simple-one-for-one -
     http://kungfooguru.wordpress.com/2009/08/05/
     erlang-simple-one-for-one-supervisor/
     Supervisor Design Principles - http://www.erlang.org/
     doc/design_principles/sup_princ.html
     Supervisor Behaviour -
     http://www.erlang.org/doc/man/supervisor.html




      Tristan Sloughter (kungfooguru@gmail.com)   Erlang/OTP Supervision

Weitere ähnliche Inhalte

Kürzlich hochgeladen

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processorsdebabhi2
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024Rafal Los
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfEnterprise Knowledge
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsJoaquim Jorge
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking MenDelhi Call girls
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Servicegiselly40
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessPixlogix Infotech
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationRadu Cotescu
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?Antenna Manufacturer Coco
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEarley Information Science
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Scriptwesley chun
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CVKhem
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024The Digital Insurer
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 

Kürzlich hochgeladen (20)

Exploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone ProcessorsExploring the Future Potential of AI-Enabled Smartphone Processors
Exploring the Future Potential of AI-Enabled Smartphone Processors
 
The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024The 7 Things I Know About Cyber Security After 25 Years | April 2024
The 7 Things I Know About Cyber Security After 25 Years | April 2024
 
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdfThe Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
The Role of Taxonomy and Ontology in Semantic Layers - Heather Hedden.pdf
 
Artificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and MythsArtificial Intelligence: Facts and Myths
Artificial Intelligence: Facts and Myths
 
08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men08448380779 Call Girls In Friends Colony Women Seeking Men
08448380779 Call Girls In Friends Colony Women Seeking Men
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024Tata AIG General Insurance Company - Insurer Innovation Award 2024
Tata AIG General Insurance Company - Insurer Innovation Award 2024
 
CNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of ServiceCNv6 Instructor Chapter 6 Quality of Service
CNv6 Instructor Chapter 6 Quality of Service
 
Advantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your BusinessAdvantages of Hiring UIUX Design Service Providers for Your Business
Advantages of Hiring UIUX Design Service Providers for Your Business
 
Scaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organizationScaling API-first – The story of a global engineering organization
Scaling API-first – The story of a global engineering organization
 
🐬 The future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?What Are The Drone Anti-jamming Systems Technology?
What Are The Drone Anti-jamming Systems Technology?
 
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptxEIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
EIS-Webinar-Prompt-Knowledge-Eng-2024-04-08.pptx
 
Automating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps ScriptAutomating Google Workspace (GWS) & more with Apps Script
Automating Google Workspace (GWS) & more with Apps Script
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
Real Time Object Detection Using Open CV
Real Time Object Detection Using Open CVReal Time Object Detection Using Open CV
Real Time Object Detection Using Open CV
 
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
Bajaj Allianz Life Insurance Company - Insurer Innovation Award 2024
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 

Empfohlen

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsKurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summarySpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentLily Ray
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best PracticesVit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project managementMindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...RachelPearson36
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Applitools
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at WorkGetSmarter
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...DevGAMM Conference
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationErica Santiago
 

Empfohlen (20)

PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
Unlocking the Power of ChatGPT and AI in Testing - A Real-World Look, present...
 
12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work12 Ways to Increase Your Influence at Work
12 Ways to Increase Your Influence at Work
 
ChatGPT webinar slides
ChatGPT webinar slidesChatGPT webinar slides
ChatGPT webinar slides
 
More than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike RoutesMore than Just Lines on a Map: Best Practices for U.S Bike Routes
More than Just Lines on a Map: Best Practices for U.S Bike Routes
 
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
Ride the Storm: Navigating Through Unstable Periods / Katerina Rudko (Belka G...
 
Barbie - Brand Strategy Presentation
Barbie - Brand Strategy PresentationBarbie - Brand Strategy Presentation
Barbie - Brand Strategy Presentation
 

CEUG: Introduction to OTP Behaviors, Part II - supervisors

  • 1. Overview Examples Erlang Supervision Erlang/OTP Supervision Tristan Sloughter (kungfooguru@gmail.com) June 2, 2010 Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision
  • 2. Overview Examples Erlang Supervision 1 Overview 2 Examples 3 Erlang Supervision Tristan Sloughter (kungfooguru@gmail.com) 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 (kungfooguru@gmail.com) 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 (kungfooguru@gmail.com) 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 (kungfooguru@gmail.com) Erlang/OTP Supervision
  • 6. Overview Examples Erlang Supervision Most Basic Example Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision
  • 7. Overview Examples Erlang Supervision A Supervision Tree Tristan Sloughter (kungfooguru@gmail.com) 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 (kungfooguru@gmail.com) 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 (kungfooguru@gmail.com) Erlang/OTP Supervision
  • 10. Overview Examples Erlang Supervision The OTP Supervisor Behaviour Standard process for implementing supervision Provides preconfigured supervision strategies Tristan Sloughter (kungfooguru@gmail.com) 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 (kungfooguru@gmail.com) 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 (kungfooguru@gmail.com) 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 (kungfooguru@gmail.com) Erlang/OTP Supervision
  • 14. Overview Examples Erlang Supervision One For All Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision
  • 15. Overview Examples Erlang Supervision Simple One For One Dynamic supervision Essentially a process factory Tristan Sloughter (kungfooguru@gmail.com) 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 (kungfooguru@gmail.com) Erlang/OTP Supervision
  • 17. Overview Examples Erlang Supervision Resources Simple-one-for-one - http://kungfooguru.wordpress.com/2009/08/05/ erlang-simple-one-for-one-supervisor/ Supervisor Design Principles - http://www.erlang.org/ doc/design_principles/sup_princ.html Supervisor Behaviour - http://www.erlang.org/doc/man/supervisor.html Tristan Sloughter (kungfooguru@gmail.com) Erlang/OTP Supervision