SlideShare a Scribd company logo
1 of 14
Download to read offline
ERLANG
SUPERVISION TREES
Code examples at https://github.com/agrawalakhil/erlang-supervision-trees
HELLO!
I am Akhil Agrawal
Doing Erlang development for last five years
Doing Ejabberd development for last eight months now
Started BIZense in 2008 & Digikrit in 2015
Overview
Erlang supervision trees helps bring fault tolerance, recovery
and robustness to erlang applications
1
ERLANG SUPERVISION TREES
Supervision strategy consists of two steps – first to
form the supervision trees and then providing
restart strategy at each level to follow when child
dies, which could affect other children
Supervision Strategies
Supervisors can supervise workers and other
supervisors forming supervision tree, while workers
should never be used in any position except under
another supervisor
Supervision Trees
Complete examples of supervisors from the
opensource projects like rabbitmq, ejabberd
and others to get some understanding of how
supervisors are used in real world
Complete Examples
A supervisor is responsible for starting, stopping,
and monitoring its child processes. The basic idea
of a supervisor is that it is to keep its child
processes alive by restarting them when necessary
Basics
Basics – Erlang Supervision (Without Supervisor Behavior)
Linked processes - terminating process sends an exit signal to all linked processes but for supervision, supervisor
should also trap exits - process instead of exiting, receives message of the form: {'EXIT', From, Reason}
start_link() ->
SupPid = spawn(classic_sup, supervisor, [basic_worker, start_link, undefined]),
register(classic_sup, SupPid),
SupPid ! {'INIT'},
{ok, SupPid}.
receive ->
{'INIT'} -> process_flag(trap_exit, true),
{ok, NewPid} = apply(Module, Function, []),
link(NewPid),
error_logger:info_msg("Initializing the worker with pid ~p~n", [NewPid]),
supervisor(Module, Function, NewPid);
{'EXIT', Pid, Reason} -> {ok, NewPid} = apply(Module, Function, []),
link(NewPid),
error_logger:info_msg("Process basic_worker pid ~p terminated due to ~p, new pid is
~p~n", [Pid, Reason, NewPid]),
supervisor(Module, Function, NewPid);
end.
Basics – Supervisor Behavior
Supervisor Behavior – Standard process for implementing
supervision and provides preconfigured supervision strategies
Image from Erlang/OTP Supervision Presentation
start_link() ->
supervisor:start_link({local, ?SERVER}, ?MODULE,
[]).
%% Flags :: {Strategy, Intensity, Period}
%% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules}
init(_Args) ->
SupFlags = {one_for_one, 1, 5},
ChildSpecs = [{basic_worker_spec,
{basic_worker, start_link, []},
permanent,
brutal_kill,
worker,
[basic_worker]}],
{ok, {SupFlags, ChildSpecs}}.
Advanced – Multi-level Supervision
Supervisors can form a tree by supervising other
supervisors and workers
SupFlags = {RestartStrategy, Intensity, Period},
ChildSpecs = [ %% advanced_worker
{advanced_worker_spec,
{advanced_worker, start_link, []},
permanent,
brutal_kill,
worker,
[advanced_worker]},
%% basic_sup
{basic_sup_spec,
{basic_sup, start_link, []},
permanent,
brutal_kill,
supervisor,
[basic_sup, basic_worker]}],
{ok, {SupFlags, ChildSpecs}}. Image from Erlang/OTP Supervision Presentation
SUPERVISION STRATEGIES
If a child process terminates, all
other child processes are
terminated, and then all child
processes, including the
terminated one, are restarted
one_for_all
It is a simplified one_for_one
supervisor, where all child
processes are dynamically added
instances of the same process
simple_one_for_one
If a child process terminates,
only that process is restarted
one_for_one
If a child process terminates, the
rest of the child processes (that
is, the child processes after the
terminated process in start order)
are terminated. Then the
terminated child process and rest
of child processes are restarted
rest_for_one Erlang
Supervision
Strategies
Advanced – Supervision Strategy one_for_one
If a child process terminates, only that process is
restarted.
start_link({RestartStrategy, Intensity, Period}) ->
supervisor:start_link({local, ?SERVER}, ?MODULE,
[{RestartStrategy, Intensity, Period}]).
init(Args = [{RestartStrategy, Intensity, Period}]) ->
SupFlags = {RestartStrategy, Intensity, Period},
one_for_one_test_() ->
{foreachx, fun(Args) -> strategy_setup(Args) end,
fun(Args, Pid) -> strategy_cleanup(Args, Pid) end,
[
{{one_for_one, 1, 5},
named_advanced_worker_restarted("one_for_one_advanced_worker
_restarted")},
{{one_for_one, 1, 5},
named_basic_sup_restarted("one_for_one_basic_sup_restarted")
}
]}.
Advanced – Supervision Strategy one_for_all
If a child process terminates, all other
child processes are terminated, and then
all child processes, including the
terminated one, are restarted
one_for_all_test_() ->
{foreachx, fun(Args) ->
strategy_setup(Args) end, fun(Args,
Pid) -> strategy_cleanup(Args, Pid)
end,
[
{{one_for_all, 1, 5},
named_advanced_worker_restarted("one_f
or_all_advanced_worker_restarted")},
{{one_for_all, 1, 5},
named_basic_sup_restarted("one_for_all
_basic_sup_restarted")}
]}. Image from Erlang/OTP Supervision Presentation
Advanced – Supervision Strategy rest_for_one
If a child process terminates, the rest of the
child processes (that is, the child processes
after the terminated process in start order) are
terminated. Then the terminated child process
and rest of child processes are restarted
rest_for_one_test_() ->
{foreachx, fun(Args) ->
strategy_setup(Args) end, fun(Args, Pid) ->
strategy_cleanup(Args, Pid) end,
[
{{rest_for_one, 1, 5},
named_advanced_worker_restarted("rest_for_o
ne_advanced_worker_restarted")},
{{rest_for_one, 1, 5},
named_basic_sup_restarted("rest_for_one_bas
ic_sup_restarted")}
]}.
http://learnyousomeerlang.com/supervisors
http://ferd.ca/the-zen-of-erlang.html
Advanced – Supervision Strategy simple_one_for_one
1. Dynamic supervision
2. Essentially a process factory
%% {simple_one_for_one, 2, 5}
dynamic_worker_restarted(Name, Args) ->
fun(Pid) ->
{Name, fun() ->
?debugFmt("dynamic_worker_restarted for args ~p~n", [Args]),
{ok, ChildPid1} = supervisor:start_child(Pid, []),
{ok, ChildPid2} = supervisor:start_child(Pid, []),
?assertEqual(2, length(supervisor:which_children(Pid))),
?debugFmt("Supervisor children ~p~n", [supervisor:which_children(Pid)]),
exit(ChildPid1, kill),
timer:sleep(1000),
?assertEqual(2, length(supervisor:which_children(Pid))),
?debugFmt("Supervisor children ~p~n", [supervisor:which_children(Pid)]),
exit(ChildPid2, kill),
timer:sleep(1000),
?assertEqual(2, length(supervisor:which_children(Pid))),
?debugFmt("Supervisor children ~p~n", [supervisor:which_children(Pid)])
end}
end.
SOME REFERENCES
◉ http://erlang.org/doc/design_principles/sup_princ.html
◉ http://www.slideshare.net/gamlidek/ceug-introduction-to-otp-
behaviors-part-ii-supervisors
◉ http://learnyousomeerlang.com/building-applications-with-otp
◉ http://learnyousomeerlang.com/supervisors
◉ http://learnyousomeerlang.com/common-test-for-uncommon-tests
◉ https://github.com/Eonblast/Trinity
◉ https://www.erlang.org/course/concurrent-programming
◉ http://ferd.ca/the-zen-of-erlang.html
◉ https://github.com/youngkin/supervisiontest
THANKS!
Any questions?
You can find me at
@digikrit / akhil@digikrit.com
Special thanks to all the people who made and released these awesome resources for free:
 Presentation template by SlidesCarnival
 Presentation models by SlideModel
 Erlang by Ericsson, RabbitMQ from Pivotal & Ejabberd from ProcessOne

More Related Content

Viewers also liked

Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesGanesh Samarthyam
 
Refactoring for Software Design Smells - Tech Talk
Refactoring for Software Design Smells - Tech TalkRefactoring for Software Design Smells - Tech Talk
Refactoring for Software Design Smells - Tech TalkCodeOps Technologies LLP
 
AngularJS Anatomy & Directives
AngularJS Anatomy & DirectivesAngularJS Anatomy & Directives
AngularJS Anatomy & DirectivesDigikrit
 
Solid Principles Of Design (Design Series 01)
Solid Principles Of Design (Design Series 01)Solid Principles Of Design (Design Series 01)
Solid Principles Of Design (Design Series 01)Heartin Jacob
 
Zero downtime release through DevOps Continuous Delivery
Zero downtime release through DevOps Continuous DeliveryZero downtime release through DevOps Continuous Delivery
Zero downtime release through DevOps Continuous DeliveryMurughan Palaniachari
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Ganesh Samarthyam
 
Software Architecture - Principles Patterns and Practices - OSI Days Workshop...
Software Architecture - Principles Patterns and Practices - OSI Days Workshop...Software Architecture - Principles Patterns and Practices - OSI Days Workshop...
Software Architecture - Principles Patterns and Practices - OSI Days Workshop...CodeOps Technologies LLP
 

Viewers also liked (18)

DevOps - A Gentle Introduction
DevOps - A Gentle IntroductionDevOps - A Gentle Introduction
DevOps - A Gentle Introduction
 
Introduction to chef
Introduction to chefIntroduction to chef
Introduction to chef
 
Advanced Debugging Using Java Bytecodes
Advanced Debugging Using Java BytecodesAdvanced Debugging Using Java Bytecodes
Advanced Debugging Using Java Bytecodes
 
Refactoring for Software Design Smells - Tech Talk
Refactoring for Software Design Smells - Tech TalkRefactoring for Software Design Smells - Tech Talk
Refactoring for Software Design Smells - Tech Talk
 
Software Architecture - Quiz Questions
Software Architecture - Quiz QuestionsSoftware Architecture - Quiz Questions
Software Architecture - Quiz Questions
 
Java Concurrency by Example
Java Concurrency by ExampleJava Concurrency by Example
Java Concurrency by Example
 
AngularJS Anatomy & Directives
AngularJS Anatomy & DirectivesAngularJS Anatomy & Directives
AngularJS Anatomy & Directives
 
Solid Principles Of Design (Design Series 01)
Solid Principles Of Design (Design Series 01)Solid Principles Of Design (Design Series 01)
Solid Principles Of Design (Design Series 01)
 
Zero downtime release through DevOps Continuous Delivery
Zero downtime release through DevOps Continuous DeliveryZero downtime release through DevOps Continuous Delivery
Zero downtime release through DevOps Continuous Delivery
 
DevOps Toolchain v1.0
DevOps Toolchain v1.0DevOps Toolchain v1.0
DevOps Toolchain v1.0
 
Java Generics - by Example
Java Generics - by ExampleJava Generics - by Example
Java Generics - by Example
 
DevOps game marshmallow challenge
DevOps game marshmallow challengeDevOps game marshmallow challenge
DevOps game marshmallow challenge
 
Effective DB Interaction
Effective DB Interaction Effective DB Interaction
Effective DB Interaction
 
Java 8 concurrency abstractions
Java 8 concurrency abstractionsJava 8 concurrency abstractions
Java 8 concurrency abstractions
 
Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams Productive Programming in Java 8 - with Lambdas and Streams
Productive Programming in Java 8 - with Lambdas and Streams
 
Sailing with Java 8 Streams
Sailing with Java 8 StreamsSailing with Java 8 Streams
Sailing with Java 8 Streams
 
Java concurrency questions and answers
Java concurrency questions and answers Java concurrency questions and answers
Java concurrency questions and answers
 
Software Architecture - Principles Patterns and Practices - OSI Days Workshop...
Software Architecture - Principles Patterns and Practices - OSI Days Workshop...Software Architecture - Principles Patterns and Practices - OSI Days Workshop...
Software Architecture - Principles Patterns and Practices - OSI Days Workshop...
 

Similar to Erlang Supervision Trees

Elixir/OTP for PHP developers
Elixir/OTP for PHP developersElixir/OTP for PHP developers
Elixir/OTP for PHP developersIgnacio Martín
 
YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
 YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docxgertrudebellgrove
 
Metrics by coda hale : to know your app’ health
Metrics by coda hale : to know your app’ healthMetrics by coda hale : to know your app’ health
Metrics by coda hale : to know your app’ healthIzzet Mustafaiev
 
19012011102_Nayan Oza_Practical-4_AI.pdf
19012011102_Nayan Oza_Practical-4_AI.pdf19012011102_Nayan Oza_Practical-4_AI.pdf
19012011102_Nayan Oza_Practical-4_AI.pdfNayanOza
 
Présentation scrum
Présentation scrumPrésentation scrum
Présentation scrumDexterIT
 
Integrate testing activities in Agile (EuroSTAR webinar)
Integrate testing activities in Agile (EuroSTAR webinar)Integrate testing activities in Agile (EuroSTAR webinar)
Integrate testing activities in Agile (EuroSTAR webinar)Rik Marselis
 
Integrate Test Activities in Agile
Integrate Test Activities in AgileIntegrate Test Activities in Agile
Integrate Test Activities in AgileTEST Huddle
 
More than syntax
More than syntaxMore than syntax
More than syntaxWooga
 
Horovod: Uber’s Open Source Distributed Deep Learning Framework for TensorFlow
Horovod: Uber’s Open Source Distributed Deep Learning Framework for TensorFlowHorovod: Uber’s Open Source Distributed Deep Learning Framework for TensorFlow
Horovod: Uber’s Open Source Distributed Deep Learning Framework for TensorFlowDatabricks
 
ECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/SnaptutorialECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/Snaptutorialpinck2380
 
ECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/SnaptutorialECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/Snaptutorialpinck200
 
Ecet 360 Enthusiastic Study / snaptutorial.com
Ecet 360 Enthusiastic Study / snaptutorial.comEcet 360 Enthusiastic Study / snaptutorial.com
Ecet 360 Enthusiastic Study / snaptutorial.comStephenson34
 
Ecet 360 Success Begins / snaptutorial.com
Ecet 360  Success Begins / snaptutorial.comEcet 360  Success Begins / snaptutorial.com
Ecet 360 Success Begins / snaptutorial.comWilliamsTaylorzl
 
Ecet 360 Massive Success / snaptutorial.com
Ecet 360 Massive Success / snaptutorial.comEcet 360 Massive Success / snaptutorial.com
Ecet 360 Massive Success / snaptutorial.comHarrisGeorgx
 
Productivity improvement at assembly station using work study techniques
Productivity improvement at assembly station using work study techniquesProductivity improvement at assembly station using work study techniques
Productivity improvement at assembly station using work study techniqueseSAT Publishing House
 

Similar to Erlang Supervision Trees (20)

Erlang supervisor explained
Erlang supervisor explainedErlang supervisor explained
Erlang supervisor explained
 
Elixir/OTP for PHP developers
Elixir/OTP for PHP developersElixir/OTP for PHP developers
Elixir/OTP for PHP developers
 
YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
 YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
YOU SHOULD NOT MODIFY ANYTHING IN THIS FILE .docx
 
Core java
Core javaCore java
Core java
 
Unit testing
Unit testingUnit testing
Unit testing
 
Metrics by coda hale : to know your app’ health
Metrics by coda hale : to know your app’ healthMetrics by coda hale : to know your app’ health
Metrics by coda hale : to know your app’ health
 
19012011102_Nayan Oza_Practical-4_AI.pdf
19012011102_Nayan Oza_Practical-4_AI.pdf19012011102_Nayan Oza_Practical-4_AI.pdf
19012011102_Nayan Oza_Practical-4_AI.pdf
 
P&MSP2012 - Unit Testing
P&MSP2012 - Unit TestingP&MSP2012 - Unit Testing
P&MSP2012 - Unit Testing
 
Présentation scrum
Présentation scrumPrésentation scrum
Présentation scrum
 
Présentation scrum
Présentation scrumPrésentation scrum
Présentation scrum
 
Integrate testing activities in Agile (EuroSTAR webinar)
Integrate testing activities in Agile (EuroSTAR webinar)Integrate testing activities in Agile (EuroSTAR webinar)
Integrate testing activities in Agile (EuroSTAR webinar)
 
Integrate Test Activities in Agile
Integrate Test Activities in AgileIntegrate Test Activities in Agile
Integrate Test Activities in Agile
 
More than syntax
More than syntaxMore than syntax
More than syntax
 
Horovod: Uber’s Open Source Distributed Deep Learning Framework for TensorFlow
Horovod: Uber’s Open Source Distributed Deep Learning Framework for TensorFlowHorovod: Uber’s Open Source Distributed Deep Learning Framework for TensorFlow
Horovod: Uber’s Open Source Distributed Deep Learning Framework for TensorFlow
 
ECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/SnaptutorialECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/Snaptutorial
 
ECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/SnaptutorialECET 360 help A Guide to career/Snaptutorial
ECET 360 help A Guide to career/Snaptutorial
 
Ecet 360 Enthusiastic Study / snaptutorial.com
Ecet 360 Enthusiastic Study / snaptutorial.comEcet 360 Enthusiastic Study / snaptutorial.com
Ecet 360 Enthusiastic Study / snaptutorial.com
 
Ecet 360 Success Begins / snaptutorial.com
Ecet 360  Success Begins / snaptutorial.comEcet 360  Success Begins / snaptutorial.com
Ecet 360 Success Begins / snaptutorial.com
 
Ecet 360 Massive Success / snaptutorial.com
Ecet 360 Massive Success / snaptutorial.comEcet 360 Massive Success / snaptutorial.com
Ecet 360 Massive Success / snaptutorial.com
 
Productivity improvement at assembly station using work study techniques
Productivity improvement at assembly station using work study techniquesProductivity improvement at assembly station using work study techniques
Productivity improvement at assembly station using work study techniques
 

Recently uploaded

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024The Digital Insurer
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonetsnaman860154
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Igalia
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking MenDelhi Call girls
 
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
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...Martijn de Jong
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Drew Madelung
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilV3cube
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024The Digital Insurer
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonAnna Loughnan Colquhoun
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slidespraypatel2
 
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 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 future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘RTylerCroy
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Paola De la Torre
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 

Recently uploaded (20)

Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024Finology Group – Insurtech Innovation Award 2024
Finology Group – Insurtech Innovation Award 2024
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
How to convert PDF to text with Nanonets
How to convert PDF to text with NanonetsHow to convert PDF to text with Nanonets
How to convert PDF to text with Nanonets
 
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
Raspberry Pi 5: Challenges and Solutions in Bringing up an OpenGL/Vulkan Driv...
 
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
 
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men08448380779 Call Girls In Greater Kailash - I Women Seeking Men
08448380779 Call Girls In Greater Kailash - I Women Seeking Men
 
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
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...2024: Domino Containers - The Next Step. News from the Domino Container commu...
2024: Domino Containers - The Next Step. News from the Domino Container commu...
 
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
Strategies for Unlocking Knowledge Management in Microsoft 365 in the Copilot...
 
Developing An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of BrazilDeveloping An App To Navigate The Roads of Brazil
Developing An App To Navigate The Roads of Brazil
 
Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024Axa Assurance Maroc - Insurer Innovation Award 2024
Axa Assurance Maroc - Insurer Innovation Award 2024
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Data Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt RobisonData Cloud, More than a CDP by Matt Robison
Data Cloud, More than a CDP by Matt Robison
 
Slack Application Development 101 Slides
Slack Application Development 101 SlidesSlack Application Development 101 Slides
Slack Application Development 101 Slides
 
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 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 future of MySQL is Postgres 🐘
🐬  The future of MySQL is Postgres   🐘🐬  The future of MySQL is Postgres   🐘
🐬 The future of MySQL is Postgres 🐘
 
Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101Salesforce Community Group Quito, Salesforce 101
Salesforce Community Group Quito, Salesforce 101
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 

Erlang Supervision Trees

  • 1. ERLANG SUPERVISION TREES Code examples at https://github.com/agrawalakhil/erlang-supervision-trees
  • 2. HELLO! I am Akhil Agrawal Doing Erlang development for last five years Doing Ejabberd development for last eight months now Started BIZense in 2008 & Digikrit in 2015
  • 3. Overview Erlang supervision trees helps bring fault tolerance, recovery and robustness to erlang applications 1
  • 4. ERLANG SUPERVISION TREES Supervision strategy consists of two steps – first to form the supervision trees and then providing restart strategy at each level to follow when child dies, which could affect other children Supervision Strategies Supervisors can supervise workers and other supervisors forming supervision tree, while workers should never be used in any position except under another supervisor Supervision Trees Complete examples of supervisors from the opensource projects like rabbitmq, ejabberd and others to get some understanding of how supervisors are used in real world Complete Examples A supervisor is responsible for starting, stopping, and monitoring its child processes. The basic idea of a supervisor is that it is to keep its child processes alive by restarting them when necessary Basics
  • 5. Basics – Erlang Supervision (Without Supervisor Behavior) Linked processes - terminating process sends an exit signal to all linked processes but for supervision, supervisor should also trap exits - process instead of exiting, receives message of the form: {'EXIT', From, Reason} start_link() -> SupPid = spawn(classic_sup, supervisor, [basic_worker, start_link, undefined]), register(classic_sup, SupPid), SupPid ! {'INIT'}, {ok, SupPid}. receive -> {'INIT'} -> process_flag(trap_exit, true), {ok, NewPid} = apply(Module, Function, []), link(NewPid), error_logger:info_msg("Initializing the worker with pid ~p~n", [NewPid]), supervisor(Module, Function, NewPid); {'EXIT', Pid, Reason} -> {ok, NewPid} = apply(Module, Function, []), link(NewPid), error_logger:info_msg("Process basic_worker pid ~p terminated due to ~p, new pid is ~p~n", [Pid, Reason, NewPid]), supervisor(Module, Function, NewPid); end.
  • 6. Basics – Supervisor Behavior Supervisor Behavior – Standard process for implementing supervision and provides preconfigured supervision strategies Image from Erlang/OTP Supervision Presentation start_link() -> supervisor:start_link({local, ?SERVER}, ?MODULE, []). %% Flags :: {Strategy, Intensity, Period} %% Child :: {Id,StartFunc,Restart,Shutdown,Type,Modules} init(_Args) -> SupFlags = {one_for_one, 1, 5}, ChildSpecs = [{basic_worker_spec, {basic_worker, start_link, []}, permanent, brutal_kill, worker, [basic_worker]}], {ok, {SupFlags, ChildSpecs}}.
  • 7. Advanced – Multi-level Supervision Supervisors can form a tree by supervising other supervisors and workers SupFlags = {RestartStrategy, Intensity, Period}, ChildSpecs = [ %% advanced_worker {advanced_worker_spec, {advanced_worker, start_link, []}, permanent, brutal_kill, worker, [advanced_worker]}, %% basic_sup {basic_sup_spec, {basic_sup, start_link, []}, permanent, brutal_kill, supervisor, [basic_sup, basic_worker]}], {ok, {SupFlags, ChildSpecs}}. Image from Erlang/OTP Supervision Presentation
  • 8. SUPERVISION STRATEGIES If a child process terminates, all other child processes are terminated, and then all child processes, including the terminated one, are restarted one_for_all It is a simplified one_for_one supervisor, where all child processes are dynamically added instances of the same process simple_one_for_one If a child process terminates, only that process is restarted one_for_one If a child process terminates, the rest of the child processes (that is, the child processes after the terminated process in start order) are terminated. Then the terminated child process and rest of child processes are restarted rest_for_one Erlang Supervision Strategies
  • 9. Advanced – Supervision Strategy one_for_one If a child process terminates, only that process is restarted. start_link({RestartStrategy, Intensity, Period}) -> supervisor:start_link({local, ?SERVER}, ?MODULE, [{RestartStrategy, Intensity, Period}]). init(Args = [{RestartStrategy, Intensity, Period}]) -> SupFlags = {RestartStrategy, Intensity, Period}, one_for_one_test_() -> {foreachx, fun(Args) -> strategy_setup(Args) end, fun(Args, Pid) -> strategy_cleanup(Args, Pid) end, [ {{one_for_one, 1, 5}, named_advanced_worker_restarted("one_for_one_advanced_worker _restarted")}, {{one_for_one, 1, 5}, named_basic_sup_restarted("one_for_one_basic_sup_restarted") } ]}.
  • 10. Advanced – Supervision Strategy one_for_all If a child process terminates, all other child processes are terminated, and then all child processes, including the terminated one, are restarted one_for_all_test_() -> {foreachx, fun(Args) -> strategy_setup(Args) end, fun(Args, Pid) -> strategy_cleanup(Args, Pid) end, [ {{one_for_all, 1, 5}, named_advanced_worker_restarted("one_f or_all_advanced_worker_restarted")}, {{one_for_all, 1, 5}, named_basic_sup_restarted("one_for_all _basic_sup_restarted")} ]}. Image from Erlang/OTP Supervision Presentation
  • 11. Advanced – Supervision Strategy rest_for_one If a child process terminates, the rest of the child processes (that is, the child processes after the terminated process in start order) are terminated. Then the terminated child process and rest of child processes are restarted rest_for_one_test_() -> {foreachx, fun(Args) -> strategy_setup(Args) end, fun(Args, Pid) -> strategy_cleanup(Args, Pid) end, [ {{rest_for_one, 1, 5}, named_advanced_worker_restarted("rest_for_o ne_advanced_worker_restarted")}, {{rest_for_one, 1, 5}, named_basic_sup_restarted("rest_for_one_bas ic_sup_restarted")} ]}. http://learnyousomeerlang.com/supervisors http://ferd.ca/the-zen-of-erlang.html
  • 12. Advanced – Supervision Strategy simple_one_for_one 1. Dynamic supervision 2. Essentially a process factory %% {simple_one_for_one, 2, 5} dynamic_worker_restarted(Name, Args) -> fun(Pid) -> {Name, fun() -> ?debugFmt("dynamic_worker_restarted for args ~p~n", [Args]), {ok, ChildPid1} = supervisor:start_child(Pid, []), {ok, ChildPid2} = supervisor:start_child(Pid, []), ?assertEqual(2, length(supervisor:which_children(Pid))), ?debugFmt("Supervisor children ~p~n", [supervisor:which_children(Pid)]), exit(ChildPid1, kill), timer:sleep(1000), ?assertEqual(2, length(supervisor:which_children(Pid))), ?debugFmt("Supervisor children ~p~n", [supervisor:which_children(Pid)]), exit(ChildPid2, kill), timer:sleep(1000), ?assertEqual(2, length(supervisor:which_children(Pid))), ?debugFmt("Supervisor children ~p~n", [supervisor:which_children(Pid)]) end} end.
  • 13. SOME REFERENCES ◉ http://erlang.org/doc/design_principles/sup_princ.html ◉ http://www.slideshare.net/gamlidek/ceug-introduction-to-otp- behaviors-part-ii-supervisors ◉ http://learnyousomeerlang.com/building-applications-with-otp ◉ http://learnyousomeerlang.com/supervisors ◉ http://learnyousomeerlang.com/common-test-for-uncommon-tests ◉ https://github.com/Eonblast/Trinity ◉ https://www.erlang.org/course/concurrent-programming ◉ http://ferd.ca/the-zen-of-erlang.html ◉ https://github.com/youngkin/supervisiontest
  • 14. THANKS! Any questions? You can find me at @digikrit / akhil@digikrit.com Special thanks to all the people who made and released these awesome resources for free:  Presentation template by SlidesCarnival  Presentation models by SlideModel  Erlang by Ericsson, RabbitMQ from Pivotal & Ejabberd from ProcessOne