Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
Supervisors, Links and Monitors
Search
Sean Cribbs
September 20, 2016
Technology
0
92
Supervisors, Links and Monitors
Using OTP tools in your Elixir application for great justice
Sean Cribbs
September 20, 2016
Tweet
Share
More Decks by Sean Cribbs
See All by Sean Cribbs
Adopting Stream Processing for Instrumentation
seancribbs
1
360
Getting the Word Out: Membership, Dissemination, and Population Protocols
seancribbs
0
340
Reliable High-Performance HTTP Infrastructure with nginx and Lua
seancribbs
0
570
The Refreshingly Rewarding Realm of Research Papers
seancribbs
0
360
Roll-your-own API Management Platform with NGINX and Lua
seancribbs
10
1.1k
Techniques for Metaprogramming in Erlang
seancribbs
2
1.4k
The Final Causal Frontier
seancribbs
6
910
Erlang - Building Blocks for Global Distributed Systems
seancribbs
4
790
A Brief History of Time in Riak
seancribbs
4
1.7k
Other Decks in Technology
See All in Technology
DynamoDB でスロットリングが発生したとき/when_throttling_occurs_in_dynamodb_short
emiki
0
260
Lambdaと地方とコミュニティ
miu_crescent
2
370
インフラとバックエンドとフロントエンドをくまなく調べて遅いアプリを早くした件
tubone24
1
430
FlutterアプリにおけるSLI/SLOを用いたユーザー体験の可視化と計測基盤構築
ostk0069
0
100
TypeScript、上達の瞬間
sadnessojisan
46
13k
マルチプロダクトな開発組織で 「開発生産性」に向き合うために試みたこと / Improving Multi-Product Dev Productivity
sugamasao
1
310
OCI Security サービス 概要
oracle4engineer
PRO
0
6.5k
開発生産性を上げながらビジネスも30倍成長させてきたチームの姿
kamina_zzz
2
1.7k
iOSチームとAndroidチームでブランチ運用が違ったので整理してます
sansantech
PRO
0
150
強いチームと開発生産性
onk
PRO
35
11k
飲食店データの分析事例とそれを支えるデータ基盤
kimujun
0
160
AWS Lambda のトラブルシュートをしていて思うこと
kazzpapa3
2
180
Featured
See All Featured
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Put a Button on it: Removing Barriers to Going Fast.
kastner
59
3.5k
Navigating Team Friction
lara
183
14k
Documentation Writing (for coders)
carmenintech
65
4.4k
Java REST API Framework Comparison - PWX 2021
mraible
PRO
28
8.2k
Responsive Adventures: Dirty Tricks From The Dark Corners of Front-End
smashingmag
250
21k
GitHub's CSS Performance
jonrohan
1030
460k
Building an army of robots
kneath
302
43k
VelocityConf: Rendering Performance Case Studies
addyosmani
325
24k
Statistics for Hackers
jakevdp
796
220k
Evolution of real-time – Irina Nazarova, EuRuKo, 2024
irinanazarova
4
370
Visualization
eitanlees
145
15k
Transcript
SUPERVISORS, LINKS & MONITORS Sean Cribbs
FLASHBACK: 2008 a more innocent time
PLAYSKOOL: MY FIRST ERLANG twitter client mochiweb handler mnesia
PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH twitter
client mochiweb handler mnesia
PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤
Buggy request handler? CRASH twitter client mochiweb handler mnesia
PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤
Buggy request handler? CRASH ➤ Mnesia acting weird? CRASH twitter client mochiweb handler mnesia
PLAYSKOOL: MY FIRST ERLANG ➤ Twitter API changes? CRASH ➤
Buggy request handler? CRASH ➤ Mnesia acting weird? CRASH twitter client mochiweb handler mnesia RESULT: Unpredictable behavior
SUPERVISORS are not for restarting processes
SUPERVISORS are for structuring your application
HINDSIGHT IS 20/20 twitter client mochiweb handler mnesia my_app (supervisor)
HINDSIGHT IS 20/20 ➤ mnesia as application dependency twitter client
mochiweb handler mnesia my_app (supervisor)
HINDSIGHT IS 20/20 ➤ mnesia as application dependency ➤ Client
and mochiweb handler as children twitter client mochiweb handler mnesia my_app (supervisor)
HINDSIGHT IS 20/20 ➤ mnesia as application dependency ➤ Client
and mochiweb handler as children ➤ Supervisor initializes mnesia tables twitter client mochiweb handler mnesia my_app (supervisor)
HINDSIGHT IS 20/20 ➤ mnesia as application dependency ➤ Client
and mochiweb handler as children ➤ Supervisor initializes mnesia tables twitter client mochiweb handler mnesia RESULT: Reliability my_app (supervisor)
:observer.start()
SUPERVISOR STRATEGIES ➤ :one_for_one ➤ :one_for_all ➤ :rest_for_one ➤ :simple_one_for_one
SUPERVISOR STRATEGIES ➤ :one_for_one ➤ :one_for_all ➤ :rest_for_one ➤ :simple_one_for_one
㱺 Task.Supervisor
SUPERVISORS use links and trap exits
LINKS intertwine fates of two processes
A B
A B Process.link(b)
file_server_2 Port (driver)
file_server_2 Port (driver) shutdown!
file_server_2 Port (driver) shutdown! EXIT close()
None
TRAP EXITS if you need to handle exit signals
TRAP EXITS if you need to handle exit signals Process.flag(:trap_exit,
true)
TRAP EXITS if you need to handle exit signals Process.flag(:trap_exit,
true) {:EXIT, pid, reason}
MONITORS notify of process exit without linking
A B
A B Process.monitor(b)
A B Process.monitor(b) {:DOWN, ref, :process, pid, reason}
A B GENSERVER.CALL/3
A B GENSERVER.CALL/3 ➤ Monitor the target {:ok, ref} =
Process.monitor(b)
A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the
message tagged with the monitor reference {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the
message tagged with the monitor reference ➤ Receive {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the
message tagged with the monitor reference ➤ Receive ➤ {^ref, reply} ⇒ {:ok, reply} {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the
message tagged with the monitor reference ➤ Receive ➤ {^ref, reply} ⇒ {:ok, reply} ➤ :DOWN ⇒ exit(reason) {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
A B GENSERVER.CALL/3 ➤ Monitor the target ➤ Send the
message tagged with the monitor reference ➤ Receive ➤ {^ref, reply} ⇒ {:ok, reply} ➤ :DOWN ⇒ exit(reason) ➤ timeout ⇒ exit(:timeout) {:ok, ref} = Process.monitor(b) {:'$gen_call', {a, ref}, :hi}
registry conn sup app sup CUSTOM PROCESS REGISTRY
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry client
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child client
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child conn client
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child ➤ Registry ⇒ monitor connection conn client
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child ➤ Registry ⇒ monitor connection ➤ Client ⇐ connection pid (maybe monitor?) conn client
registry conn sup app sup CUSTOM PROCESS REGISTRY ➤ GOAL:
Lookup or start a connection to another node ➤ Client (GenServer.call) ⇒ Registry ➤ Registry ⇒ Supervisor.start_child ➤ Registry ⇒ monitor connection ➤ Client ⇐ connection pid (maybe monitor?) ➤ DOWN ⇒ Registry cleans up client
TIPS AND TRICKS ➤ Don’t put too much on your
Supervisor ➤ Child order matters ➤ Use links and monitors ➤ Read the Elixir and Erlang source
REFERENCES ➤ Erlang Reference Manual: Processes http://erlang.org/doc/reference_manual/processes.html ➤ Erlang Reference
Manual: Errors http://erlang.org/doc/reference_manual/errors.html ➤ OTP Design Principles: Overview http://erlang.org/doc/design_principles/des_princ.html ➤ OTP Design Principles: Supervisors http://erlang.org/doc/design_principles/sup_princ.html ➤ OTP Design Principles: Applications http://erlang.org/doc/design_principles/applications.html