SlideShare a Scribd company logo
ERLANG Supervisor
Concepts & Specs
Trung Nguyen 2018
Erlang/OTP high-level coordination
Concurrency built-in:
readily support 10,000 processes per core,
and transparent distribution of processes
across multiple machines,
using message passing for communication.
Erlang distribution model
The robustness of the Erlang is provided
by hierarchies of supervision processes
which manage recovery from software or hardware errors.
The standard Erlang/OTP behaviours are:
● Gen_server For implementing the server of a client-server relation
● Gen_statem For implementing state machines
● Gen_event For implementing event handling functionality
● Supervisor For implementing a supervisor in a supervision tree
Supervisor ->Use for controlling
Let the process die and correct the error
in some other process.
Remote detection and handling of errors.
Child process restarting rate after crash.
Supervisor’s raw structure is a MAP(sincev18)
{ #{ strategy => RestartStrategy,
intensity => MaxRestart ,
timeout => MaxTime},
[#{child_specs}]
}.
Supervisor structure : strategy
To prevent a supervisor from getting into an infinite loop
of child process terminations and restarts,
a maximum restart intensity is defined.
Assuming the values
MaxR for intensity (defaults =1 )
and MaxT for period (defaults =5).
If more than MaxR times of restarts occur within MaxT seconds,
the supervisor terminates all child processes and then itself.
Supervisor structure : strategy
one_for_one -> this is the default restart strategy.
one_for_all ->
rest_for_one ->
simple_one_for_one ->
Child specifications
The child specification can be described in a more abstract form as:
{ ChildId,
StartFunc,
Restart,
Shutdown,
Type,
Modules}.
Child specifications
child_spec() =
#{id => child_id(), % mandatory
start => mfargs(), % mandatory
restart => restart(), % optional
shutdown => shutdown(), % optional
type => worker(), % optional
modules => modules()} % optional
Child specifications : ChildId
The ChildId is just an internal name
used by the supervisor internally.
Any term can be used for the Id.
Child specifications: Restart strategy
restart defines when a terminated child process is to be restarted.
● A permanent child process is always restarted.
● A temporary child process is never restarted
(not even when the supervisor restart strategy is rest_for_one or one_for_all
and a sibling death causes the temporary process to be terminated).
● A transient child process is restarted only if it terminates abnormally
that is, with an exit reason other than normal, shutdown,
or {shutdown,Term}.
Child specifications : Restart order
When the supervisor is started, the child processes are started
in order from left to right according to this list.
When the supervisor terminates, it first terminates its child processes
in reversed start order, from right to left.
-> and then terminates itself.
Child specifications : Start Function
StartFunc is a tuple that tells how to start the child.
standard {M,F,A} format we've used a few times already.
USE ALWAYS gen_*:start_link() wrapped in your own module.
Child specifications : Shutdown
The Shutdown value used to give a deadline on the termination.
When a supervisor gets the shutdown signal,
it will forward it to its own children the same way.
When the top-level supervisor is asked to terminate, it calls
exit(ChildPid, shutdown) on each of the Pids.
If the child is a worker and trapping exits, it'll call its own terminate function.
Otherwise, it's just going to die.
Child specifications : Shutdown
Note is important : for simple_one_for_one strategy
With this behaviour , children are not respecting this rule
with the Shutdown time.
The supervisor will just exit ,
and it will be left to each of the workers to terminate on their own,
after their supervisor is gone.
Child specifications : Type
Type simply lets the supervisor know whether the child
is a worker or a supervisor.
This will be important when upgrading applications
with more advanced OTP features.
Child specifications : Module
Modules is a list of one element,
the name of the callback module used by the child behavior.
Child specifications : Module
The exception to that is when you have callback modules
whose identity you do not know beforehand
(such as event handlers in an event manager).
In this case, the value of Modules should be dynamic,
so that the whole OTP system knows who to contact
when using more advanced features, such as releases.
Behaviours
= formalizations of common patterns.
Divide a process code into
1/ a generic part (a behaviour module)
2/ and a specific part (a callback module).
Workers and Supervisors
hierarchical arrangement of code help
design and program fault-tolerant software
Restart strategies : one_for_one
If one child process terminates and is to be restarted,
only that child process is affected.
Restart strategies : one_for_one
%% Generate a generation of BirdName.
init(oone) ->
init({one_for_one, 5, 60});
init({RestartStrategy, MaxRestart, MaxTime}) ->
{ok, {{RestartStrategy, MaxRestart, MaxTime},
[{ChildSpecs_1},{ChildSpecs_2},..,{ChildSpecs_N}]}}.
Restart strategies : one_for_all ->
If one child process terminates and is to be restarted,
all other child processes are terminated
and then all child processes are restarted.
Restart strategies : one_for_all
%% Generate a generation of BirdName.
init(ofall) ->
init({one_for_all, 5, 60});
init({RestartStrategy, MaxRestart, MaxTime}) ->
{ok, {{RestartStrategy, MaxRestart, MaxTime},
[{ChildSpecs_1},{ChildSpecs_2},..,{ChildSpecs_N}]}}.
Restart strategies : rest_for_one ->
●
If one child process terminates and is to be restarted,
the 'rest' of the child processes are terminated.
(a dependency between processes according to their booting order.
if a child process dies, only those booted after it are killed.
Processes are then restarted as expected.)
Restart strategies : rest_for_all
%% Generate a generation of BirdName.
init(resto) ->
init({rest_for_all, 5, 60});
init({RestartStrategy, MaxRestart, MaxTime}) ->
{ok, {{RestartStrategy, MaxRestart, MaxTime},
[{ChildSpecs_1},{ChildSpecs_2},..,{ChildSpecs_N}]}}.
Restart strategies :simple_one_for_one
A simplified one_for_one supervisor, where all child processes are
dynamically added instances of the same process,
that is, running the same code template .
(same type=WORKER and same frequency of restart & shutdown timeout).
With this strategy, no workers are started during the supervisor initialization.
Instead, a worker is started manually via the
supervisor:start_child/2 function.
Restart strategies :simple_one_for_one
–Robert Virding rvirding explained:
You must define the child spec as it is used for all children
and you do not give it when starting a child.
In all other cases you do not need to give it as you must explicitly give a child
spec when you call supervisor:start_child/2.
The supervisor will automatically start the children with child specs returned in
the init/1 callback.
You can then add children using supervisor:start_child/2.
Restart strategies :simple_one_for_one
%%Supervisor ‘s unique child template
%%A fixedline phone example
init([]) ->
{ok, {{simple_one_for_one, 10, 3600},
[{ms, {phone_fsm, start_link, []},
transient, 2000, worker, [phone_fsm]}]}}.
Example : Supervisor with gen_FSM
A playground with birds
Example : Supervisor with gen_FSM
Example : Supervisor with gen_FSM
Creates a gen_fsm process as part of a supervision tree.
The function should be called, directly or indirectly, by the supervisor.
It will, among other things, ensure that the gen_fsm is linked to the supervisor.
Initialization : Worker descr
start_link(BirdName) ->
gen_fsm:start_link({local, BirdName}, ?MODULE, [BirdName], []).
init(Ms) ->
process_flag(trap_exit, true),
% % default state of worker set to GROWWING
{ok, growing, Ms}.
Example : Supervisor with gen_server
A musicians band

More Related Content

What's hot

ZOZOTOWNのCloud Native Journey
ZOZOTOWNのCloud Native JourneyZOZOTOWNのCloud Native Journey
ZOZOTOWNのCloud Native Journey
Toru Makabe
 
アジャイル開発を支える開発環境 公開用
アジャイル開発を支える開発環境 公開用アジャイル開発を支える開発環境 公開用
アジャイル開発を支える開発環境 公開用
ESM SEC
 
並列対決 Elixir × Go × C# x Scala , Node.js
並列対決 Elixir × Go × C# x Scala , Node.js並列対決 Elixir × Go × C# x Scala , Node.js
並列対決 Elixir × Go × C# x Scala , Node.js
Yoshiiro Ueno
 
ASTERIA WARP運用Tips「RDB連携時のトラブルシューティング 」
ASTERIA WARP運用Tips「RDB連携時のトラブルシューティング 」ASTERIA WARP運用Tips「RDB連携時のトラブルシューティング 」
ASTERIA WARP運用Tips「RDB連携時のトラブルシューティング 」
ASTERIA User Group
 
今から始めようMicrosoft PowerApps! (2017年版) - 吉田の備忘録
今から始めようMicrosoft PowerApps! (2017年版) - 吉田の備忘録今から始めようMicrosoft PowerApps! (2017年版) - 吉田の備忘録
今から始めようMicrosoft PowerApps! (2017年版) - 吉田の備忘録
Taiki Yoshida
 
バッチを Akka Streams で再実装したら100倍速くなった話 #ScalaMatsuri
バッチを Akka Streams で再実装したら100倍速くなった話 #ScalaMatsuriバッチを Akka Streams で再実装したら100倍速くなった話 #ScalaMatsuri
バッチを Akka Streams で再実装したら100倍速くなった話 #ScalaMatsuri
Kazuki Negoro
 
Elasticsearch勉強会#39 LT 20201217
Elasticsearch勉強会#39 LT 20201217Elasticsearch勉強会#39 LT 20201217
Elasticsearch勉強会#39 LT 20201217
Tetsuya Sodo
 
人生で大事なことはXP白本と参考文献に教わった IN 神山
人生で大事なことはXP白本と参考文献に教わった IN 神山人生で大事なことはXP白本と参考文献に教わった IN 神山
人生で大事なことはXP白本と参考文献に教わった IN 神山
Takeshi Kakeda
 
Spring tools4
Spring tools4Spring tools4
Spring tools4
Takuya Iwatsuka
 
グラフデータベース Neptune 使ってみた
グラフデータベース Neptune 使ってみたグラフデータベース Neptune 使ってみた
グラフデータベース Neptune 使ってみた
Yoshiyasu SAEKI
 
大規模Node.jsを支える ロードバランスとオートスケールの独自実装
大規模Node.jsを支える ロードバランスとオートスケールの独自実装大規模Node.jsを支える ロードバランスとオートスケールの独自実装
大規模Node.jsを支える ロードバランスとオートスケールの独自実装
kidach1
 
AIOpsで実現する効率化 OSC 2022 Online Spring TIS
AIOpsで実現する効率化 OSC 2022 Online Spring TISAIOpsで実現する効率化 OSC 2022 Online Spring TIS
AIOpsで実現する効率化 OSC 2022 Online Spring TIS
Daisuke Ikeda
 
社内勉強会をはじめるにあたって
社内勉強会をはじめるにあたって社内勉強会をはじめるにあたって
社内勉強会をはじめるにあたって
瑛一 西口
 
Yahoo! JAPANのデータパイプラインで起きた障害とチューニング - Apache Kafka Meetup Japan #5 -
Yahoo! JAPANのデータパイプラインで起きた障害とチューニング - Apache Kafka Meetup Japan #5 -Yahoo! JAPANのデータパイプラインで起きた障害とチューニング - Apache Kafka Meetup Japan #5 -
Yahoo! JAPANのデータパイプラインで起きた障害とチューニング - Apache Kafka Meetup Japan #5 -
Yahoo!デベロッパーネットワーク
 
[C16] インメモリ分散KVSの弱点。一貫性が崩れる原因と、それを克服する技術とは? by Taichi Umeda
[C16] インメモリ分散KVSの弱点。一貫性が崩れる原因と、それを克服する技術とは? by Taichi Umeda[C16] インメモリ分散KVSの弱点。一貫性が崩れる原因と、それを克服する技術とは? by Taichi Umeda
[C16] インメモリ分散KVSの弱点。一貫性が崩れる原因と、それを克服する技術とは? by Taichi UmedaInsight Technology, Inc.
 
ラボラトリーオートメーションのためのソフトウェア思想教育(非プログラマ―が知っておくべきプログラミングの本質)
ラボラトリーオートメーションのためのソフトウェア思想教育(非プログラマ―が知っておくべきプログラミングの本質)ラボラトリーオートメーションのためのソフトウェア思想教育(非プログラマ―が知っておくべきプログラミングの本質)
ラボラトリーオートメーションのためのソフトウェア思想教育(非プログラマ―が知っておくべきプログラミングの本質)
Tokoroten Nakayama
 
PostgreSQLモニタリング機能の現状とこれから(Open Developers Conference 2020 Online 発表資料)
PostgreSQLモニタリング機能の現状とこれから(Open Developers Conference 2020 Online 発表資料)PostgreSQLモニタリング機能の現状とこれから(Open Developers Conference 2020 Online 発表資料)
PostgreSQLモニタリング機能の現状とこれから(Open Developers Conference 2020 Online 発表資料)
NTT DATA Technology & Innovation
 
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
Takuto Wada
 
Next.jsでISR
Next.jsでISRNext.jsでISR
Next.jsでISR
NobuhiroKato5
 
40歳過ぎてもエンジニアでいるためにやっていること
40歳過ぎてもエンジニアでいるためにやっていること40歳過ぎてもエンジニアでいるためにやっていること
40歳過ぎてもエンジニアでいるためにやっていること
onozaty
 

What's hot (20)

ZOZOTOWNのCloud Native Journey
ZOZOTOWNのCloud Native JourneyZOZOTOWNのCloud Native Journey
ZOZOTOWNのCloud Native Journey
 
アジャイル開発を支える開発環境 公開用
アジャイル開発を支える開発環境 公開用アジャイル開発を支える開発環境 公開用
アジャイル開発を支える開発環境 公開用
 
並列対決 Elixir × Go × C# x Scala , Node.js
並列対決 Elixir × Go × C# x Scala , Node.js並列対決 Elixir × Go × C# x Scala , Node.js
並列対決 Elixir × Go × C# x Scala , Node.js
 
ASTERIA WARP運用Tips「RDB連携時のトラブルシューティング 」
ASTERIA WARP運用Tips「RDB連携時のトラブルシューティング 」ASTERIA WARP運用Tips「RDB連携時のトラブルシューティング 」
ASTERIA WARP運用Tips「RDB連携時のトラブルシューティング 」
 
今から始めようMicrosoft PowerApps! (2017年版) - 吉田の備忘録
今から始めようMicrosoft PowerApps! (2017年版) - 吉田の備忘録今から始めようMicrosoft PowerApps! (2017年版) - 吉田の備忘録
今から始めようMicrosoft PowerApps! (2017年版) - 吉田の備忘録
 
バッチを Akka Streams で再実装したら100倍速くなった話 #ScalaMatsuri
バッチを Akka Streams で再実装したら100倍速くなった話 #ScalaMatsuriバッチを Akka Streams で再実装したら100倍速くなった話 #ScalaMatsuri
バッチを Akka Streams で再実装したら100倍速くなった話 #ScalaMatsuri
 
Elasticsearch勉強会#39 LT 20201217
Elasticsearch勉強会#39 LT 20201217Elasticsearch勉強会#39 LT 20201217
Elasticsearch勉強会#39 LT 20201217
 
人生で大事なことはXP白本と参考文献に教わった IN 神山
人生で大事なことはXP白本と参考文献に教わった IN 神山人生で大事なことはXP白本と参考文献に教わった IN 神山
人生で大事なことはXP白本と参考文献に教わった IN 神山
 
Spring tools4
Spring tools4Spring tools4
Spring tools4
 
グラフデータベース Neptune 使ってみた
グラフデータベース Neptune 使ってみたグラフデータベース Neptune 使ってみた
グラフデータベース Neptune 使ってみた
 
大規模Node.jsを支える ロードバランスとオートスケールの独自実装
大規模Node.jsを支える ロードバランスとオートスケールの独自実装大規模Node.jsを支える ロードバランスとオートスケールの独自実装
大規模Node.jsを支える ロードバランスとオートスケールの独自実装
 
AIOpsで実現する効率化 OSC 2022 Online Spring TIS
AIOpsで実現する効率化 OSC 2022 Online Spring TISAIOpsで実現する効率化 OSC 2022 Online Spring TIS
AIOpsで実現する効率化 OSC 2022 Online Spring TIS
 
社内勉強会をはじめるにあたって
社内勉強会をはじめるにあたって社内勉強会をはじめるにあたって
社内勉強会をはじめるにあたって
 
Yahoo! JAPANのデータパイプラインで起きた障害とチューニング - Apache Kafka Meetup Japan #5 -
Yahoo! JAPANのデータパイプラインで起きた障害とチューニング - Apache Kafka Meetup Japan #5 -Yahoo! JAPANのデータパイプラインで起きた障害とチューニング - Apache Kafka Meetup Japan #5 -
Yahoo! JAPANのデータパイプラインで起きた障害とチューニング - Apache Kafka Meetup Japan #5 -
 
[C16] インメモリ分散KVSの弱点。一貫性が崩れる原因と、それを克服する技術とは? by Taichi Umeda
[C16] インメモリ分散KVSの弱点。一貫性が崩れる原因と、それを克服する技術とは? by Taichi Umeda[C16] インメモリ分散KVSの弱点。一貫性が崩れる原因と、それを克服する技術とは? by Taichi Umeda
[C16] インメモリ分散KVSの弱点。一貫性が崩れる原因と、それを克服する技術とは? by Taichi Umeda
 
ラボラトリーオートメーションのためのソフトウェア思想教育(非プログラマ―が知っておくべきプログラミングの本質)
ラボラトリーオートメーションのためのソフトウェア思想教育(非プログラマ―が知っておくべきプログラミングの本質)ラボラトリーオートメーションのためのソフトウェア思想教育(非プログラマ―が知っておくべきプログラミングの本質)
ラボラトリーオートメーションのためのソフトウェア思想教育(非プログラマ―が知っておくべきプログラミングの本質)
 
PostgreSQLモニタリング機能の現状とこれから(Open Developers Conference 2020 Online 発表資料)
PostgreSQLモニタリング機能の現状とこれから(Open Developers Conference 2020 Online 発表資料)PostgreSQLモニタリング機能の現状とこれから(Open Developers Conference 2020 Online 発表資料)
PostgreSQLモニタリング機能の現状とこれから(Open Developers Conference 2020 Online 発表資料)
 
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
SQLアンチパターン 幻の第26章「とりあえず削除フラグ」
 
Next.jsでISR
Next.jsでISRNext.jsでISR
Next.jsでISR
 
40歳過ぎてもエンジニアでいるためにやっていること
40歳過ぎてもエンジニアでいるためにやっていること40歳過ぎてもエンジニアでいるためにやっていること
40歳過ぎてもエンジニアでいるためにやっていること
 

Similar to Erlang supervisor explained

Erlang Supervision Trees
Erlang Supervision TreesErlang Supervision Trees
Erlang Supervision Trees
Digikrit
 
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
pinck2380
 
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
pinck200
 
Ecet 360 Massive Success / snaptutorial.com
Ecet 360 Massive Success / snaptutorial.comEcet 360 Massive Success / snaptutorial.com
Ecet 360 Massive Success / snaptutorial.com
HarrisGeorgx
 
Ecet 360 Enthusiastic Study / snaptutorial.com
Ecet 360 Enthusiastic Study / snaptutorial.comEcet 360 Enthusiastic Study / snaptutorial.com
Ecet 360 Enthusiastic Study / snaptutorial.com
Stephenson34
 
Ecet 360 Success Begins / snaptutorial.com
Ecet 360  Success Begins / snaptutorial.comEcet 360  Success Begins / snaptutorial.com
Ecet 360 Success Begins / snaptutorial.com
WilliamsTaylorzl
 
Operating System
Operating SystemOperating System
Operating System
GowriLatha1
 
Lab 3 of 7 Process Management Simulation L A B  O V E R V I E W.docx
Lab 3 of 7 Process Management Simulation L A B  O V E R V I E W.docxLab 3 of 7 Process Management Simulation L A B  O V E R V I E W.docx
Lab 3 of 7 Process Management Simulation L A B  O V E R V I E W.docx
festockton
 
Monitor(karthika)
Monitor(karthika)Monitor(karthika)
Monitor(karthika)Nagarajan
 
Testing concept definition
Testing concept definitionTesting concept definition
Testing concept definition
Vivek V
 
Availability tactics
Availability tacticsAvailability tactics
Availability tactics
ahsan riaz
 
Unit testing
Unit testingUnit testing
Unit testing
Arthur Purnama
 
Effective Unit Test Style Guide
Effective Unit Test Style GuideEffective Unit Test Style Guide
Effective Unit Test Style Guide
Jacky Lai
 
Optimal Selection of Software Reliability Growth Model-A Study
Optimal Selection of Software Reliability Growth Model-A StudyOptimal Selection of Software Reliability Growth Model-A Study
Optimal Selection of Software Reliability Growth Model-A Study
IJEEE
 
03b loops
03b   loops03b   loops
03b loops
Manzoor ALam
 
Some Commonly Asked Question For Software Testing
Some Commonly Asked Question For Software TestingSome Commonly Asked Question For Software Testing
Some Commonly Asked Question For Software Testing
Kumari Warsha Goel
 
System testing
System testingSystem testing
System testing
Abdullah-Al- Mahmud
 
Introduction to testing.
Introduction to testing.Introduction to testing.
Introduction to testing.
Jithinctzz
 
Building resilient applications
Building resilient applicationsBuilding resilient applications
Building resilient applications
Nuno Caneco
 

Similar to Erlang supervisor explained (20)

Erlang Supervision Trees
Erlang Supervision TreesErlang Supervision Trees
Erlang Supervision Trees
 
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 Massive Success / snaptutorial.com
Ecet 360 Massive Success / snaptutorial.comEcet 360 Massive Success / snaptutorial.com
Ecet 360 Massive Success / snaptutorial.com
 
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
 
Operating System
Operating SystemOperating System
Operating System
 
Lab 3 of 7 Process Management Simulation L A B  O V E R V I E W.docx
Lab 3 of 7 Process Management Simulation L A B  O V E R V I E W.docxLab 3 of 7 Process Management Simulation L A B  O V E R V I E W.docx
Lab 3 of 7 Process Management Simulation L A B  O V E R V I E W.docx
 
Monitor(karthika)
Monitor(karthika)Monitor(karthika)
Monitor(karthika)
 
Testing concept definition
Testing concept definitionTesting concept definition
Testing concept definition
 
Availability tactics
Availability tacticsAvailability tactics
Availability tactics
 
Alarm management at DeltaV
Alarm management at DeltaVAlarm management at DeltaV
Alarm management at DeltaV
 
Unit testing
Unit testingUnit testing
Unit testing
 
Effective Unit Test Style Guide
Effective Unit Test Style GuideEffective Unit Test Style Guide
Effective Unit Test Style Guide
 
Optimal Selection of Software Reliability Growth Model-A Study
Optimal Selection of Software Reliability Growth Model-A StudyOptimal Selection of Software Reliability Growth Model-A Study
Optimal Selection of Software Reliability Growth Model-A Study
 
03b loops
03b   loops03b   loops
03b loops
 
Some Commonly Asked Question For Software Testing
Some Commonly Asked Question For Software TestingSome Commonly Asked Question For Software Testing
Some Commonly Asked Question For Software Testing
 
System testing
System testingSystem testing
System testing
 
Introduction to testing.
Introduction to testing.Introduction to testing.
Introduction to testing.
 
Building resilient applications
Building resilient applicationsBuilding resilient applications
Building resilient applications
 

Recently uploaded

Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
XfilesPro
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
Deuglo Infosystem Pvt Ltd
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Globus
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
timtebeek1
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
Łukasz Chruściel
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
Drona Infotech
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
QuickwayInfoSystems3
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Globus
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
lorraineandreiamcidl
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Łukasz Chruściel
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
Google
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
Google
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
Philip Schwarz
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
abdulrafaychaudhry
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
Hornet Dynamics
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
Fermin Galan
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
Max Andersen
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
Globus
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
ShamsuddeenMuhammadA
 

Recently uploaded (20)

Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, BetterWebinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
Webinar: Salesforce Document Management 2.0 - Smarter, Faster, Better
 
Empowering Growth with Best Software Development Company in Noida - Deuglo
Empowering Growth with Best Software  Development Company in Noida - DeugloEmpowering Growth with Best Software  Development Company in Noida - Deuglo
Empowering Growth with Best Software Development Company in Noida - Deuglo
 
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data AnalysisProviding Globus Services to Users of JASMIN for Environmental Data Analysis
Providing Globus Services to Users of JASMIN for Environmental Data Analysis
 
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdfAutomated software refactoring with OpenRewrite and Generative AI.pptx.pdf
Automated software refactoring with OpenRewrite and Generative AI.pptx.pdf
 
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf2024 eCommerceDays Toulouse - Sylius 2.0.pdf
2024 eCommerceDays Toulouse - Sylius 2.0.pdf
 
Mobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona InfotechMobile App Development Company In Noida | Drona Infotech
Mobile App Development Company In Noida | Drona Infotech
 
Enterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptxEnterprise Software Development with No Code Solutions.pptx
Enterprise Software Development with No Code Solutions.pptx
 
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
Innovating Inference - Remote Triggering of Large Language Models on HPC Clus...
 
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOMLORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
LORRAINE ANDREI_LEQUIGAN_HOW TO USE ZOOM
 
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️Need for Speed: Removing speed bumps from your Symfony projects ⚡️
Need for Speed: Removing speed bumps from your Symfony projects ⚡️
 
AI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website CreatorAI Genie Review: World’s First Open AI WordPress Website Creator
AI Genie Review: World’s First Open AI WordPress Website Creator
 
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing SuiteAI Pilot Review: The World’s First Virtual Assistant Marketing Suite
AI Pilot Review: The World’s First Virtual Assistant Marketing Suite
 
A Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of PassageA Sighting of filterA in Typelevel Rite of Passage
A Sighting of filterA in Typelevel Rite of Passage
 
Lecture 1 Introduction to games development
Lecture 1 Introduction to games developmentLecture 1 Introduction to games development
Lecture 1 Introduction to games development
 
E-commerce Application Development Company.pdf
E-commerce Application Development Company.pdfE-commerce Application Development Company.pdf
E-commerce Application Development Company.pdf
 
Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604Orion Context Broker introduction 20240604
Orion Context Broker introduction 20240604
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Quarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden ExtensionsQuarkus Hidden and Forbidden Extensions
Quarkus Hidden and Forbidden Extensions
 
Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024Globus Compute Introduction - GlobusWorld 2024
Globus Compute Introduction - GlobusWorld 2024
 
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptxText-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
Text-Summarization-of-Breaking-News-Using-Fine-tuning-BART-Model.pptx
 

Erlang supervisor explained

  • 1. ERLANG Supervisor Concepts & Specs Trung Nguyen 2018
  • 2. Erlang/OTP high-level coordination Concurrency built-in: readily support 10,000 processes per core, and transparent distribution of processes across multiple machines, using message passing for communication.
  • 3. Erlang distribution model The robustness of the Erlang is provided by hierarchies of supervision processes which manage recovery from software or hardware errors.
  • 4. The standard Erlang/OTP behaviours are: ● Gen_server For implementing the server of a client-server relation ● Gen_statem For implementing state machines ● Gen_event For implementing event handling functionality ● Supervisor For implementing a supervisor in a supervision tree
  • 5. Supervisor ->Use for controlling Let the process die and correct the error in some other process. Remote detection and handling of errors. Child process restarting rate after crash.
  • 6. Supervisor’s raw structure is a MAP(sincev18) { #{ strategy => RestartStrategy, intensity => MaxRestart , timeout => MaxTime}, [#{child_specs}] }.
  • 7. Supervisor structure : strategy To prevent a supervisor from getting into an infinite loop of child process terminations and restarts, a maximum restart intensity is defined. Assuming the values MaxR for intensity (defaults =1 ) and MaxT for period (defaults =5). If more than MaxR times of restarts occur within MaxT seconds, the supervisor terminates all child processes and then itself.
  • 8. Supervisor structure : strategy one_for_one -> this is the default restart strategy. one_for_all -> rest_for_one -> simple_one_for_one ->
  • 9. Child specifications The child specification can be described in a more abstract form as: { ChildId, StartFunc, Restart, Shutdown, Type, Modules}.
  • 10. Child specifications child_spec() = #{id => child_id(), % mandatory start => mfargs(), % mandatory restart => restart(), % optional shutdown => shutdown(), % optional type => worker(), % optional modules => modules()} % optional
  • 11. Child specifications : ChildId The ChildId is just an internal name used by the supervisor internally. Any term can be used for the Id.
  • 12. Child specifications: Restart strategy restart defines when a terminated child process is to be restarted. ● A permanent child process is always restarted. ● A temporary child process is never restarted (not even when the supervisor restart strategy is rest_for_one or one_for_all and a sibling death causes the temporary process to be terminated). ● A transient child process is restarted only if it terminates abnormally that is, with an exit reason other than normal, shutdown, or {shutdown,Term}.
  • 13. Child specifications : Restart order When the supervisor is started, the child processes are started in order from left to right according to this list. When the supervisor terminates, it first terminates its child processes in reversed start order, from right to left. -> and then terminates itself.
  • 14. Child specifications : Start Function StartFunc is a tuple that tells how to start the child. standard {M,F,A} format we've used a few times already. USE ALWAYS gen_*:start_link() wrapped in your own module.
  • 15. Child specifications : Shutdown The Shutdown value used to give a deadline on the termination. When a supervisor gets the shutdown signal, it will forward it to its own children the same way. When the top-level supervisor is asked to terminate, it calls exit(ChildPid, shutdown) on each of the Pids. If the child is a worker and trapping exits, it'll call its own terminate function. Otherwise, it's just going to die.
  • 16. Child specifications : Shutdown Note is important : for simple_one_for_one strategy With this behaviour , children are not respecting this rule with the Shutdown time. The supervisor will just exit , and it will be left to each of the workers to terminate on their own, after their supervisor is gone.
  • 17. Child specifications : Type Type simply lets the supervisor know whether the child is a worker or a supervisor. This will be important when upgrading applications with more advanced OTP features.
  • 18. Child specifications : Module Modules is a list of one element, the name of the callback module used by the child behavior.
  • 19. Child specifications : Module The exception to that is when you have callback modules whose identity you do not know beforehand (such as event handlers in an event manager). In this case, the value of Modules should be dynamic, so that the whole OTP system knows who to contact when using more advanced features, such as releases.
  • 20. Behaviours = formalizations of common patterns. Divide a process code into 1/ a generic part (a behaviour module) 2/ and a specific part (a callback module).
  • 21. Workers and Supervisors hierarchical arrangement of code help design and program fault-tolerant software
  • 22. Restart strategies : one_for_one If one child process terminates and is to be restarted, only that child process is affected.
  • 23. Restart strategies : one_for_one %% Generate a generation of BirdName. init(oone) -> init({one_for_one, 5, 60}); init({RestartStrategy, MaxRestart, MaxTime}) -> {ok, {{RestartStrategy, MaxRestart, MaxTime}, [{ChildSpecs_1},{ChildSpecs_2},..,{ChildSpecs_N}]}}.
  • 24. Restart strategies : one_for_all -> If one child process terminates and is to be restarted, all other child processes are terminated and then all child processes are restarted.
  • 25. Restart strategies : one_for_all %% Generate a generation of BirdName. init(ofall) -> init({one_for_all, 5, 60}); init({RestartStrategy, MaxRestart, MaxTime}) -> {ok, {{RestartStrategy, MaxRestart, MaxTime}, [{ChildSpecs_1},{ChildSpecs_2},..,{ChildSpecs_N}]}}.
  • 26. Restart strategies : rest_for_one -> ● If one child process terminates and is to be restarted, the 'rest' of the child processes are terminated. (a dependency between processes according to their booting order. if a child process dies, only those booted after it are killed. Processes are then restarted as expected.)
  • 27. Restart strategies : rest_for_all %% Generate a generation of BirdName. init(resto) -> init({rest_for_all, 5, 60}); init({RestartStrategy, MaxRestart, MaxTime}) -> {ok, {{RestartStrategy, MaxRestart, MaxTime}, [{ChildSpecs_1},{ChildSpecs_2},..,{ChildSpecs_N}]}}.
  • 28. Restart strategies :simple_one_for_one A simplified one_for_one supervisor, where all child processes are dynamically added instances of the same process, that is, running the same code template . (same type=WORKER and same frequency of restart & shutdown timeout). With this strategy, no workers are started during the supervisor initialization. Instead, a worker is started manually via the supervisor:start_child/2 function.
  • 29. Restart strategies :simple_one_for_one –Robert Virding rvirding explained: You must define the child spec as it is used for all children and you do not give it when starting a child. In all other cases you do not need to give it as you must explicitly give a child spec when you call supervisor:start_child/2. The supervisor will automatically start the children with child specs returned in the init/1 callback. You can then add children using supervisor:start_child/2.
  • 30. Restart strategies :simple_one_for_one %%Supervisor ‘s unique child template %%A fixedline phone example init([]) -> {ok, {{simple_one_for_one, 10, 3600}, [{ms, {phone_fsm, start_link, []}, transient, 2000, worker, [phone_fsm]}]}}.
  • 31. Example : Supervisor with gen_FSM A playground with birds
  • 32. Example : Supervisor with gen_FSM
  • 33. Example : Supervisor with gen_FSM Creates a gen_fsm process as part of a supervision tree. The function should be called, directly or indirectly, by the supervisor. It will, among other things, ensure that the gen_fsm is linked to the supervisor.
  • 34. Initialization : Worker descr start_link(BirdName) -> gen_fsm:start_link({local, BirdName}, ?MODULE, [BirdName], []). init(Ms) -> process_flag(trap_exit, true), % % default state of worker set to GROWWING {ok, growing, Ms}.
  • 35. Example : Supervisor with gen_server A musicians band