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
アクターによる 並行処理アプケーション アーキテクチャ
Search
hakobe (Yohei Fushii)
November 27, 2015
Programming
4
3.4k
アクターによる 並行処理アプケーション アーキテクチャ
社内勉強会でアクターとアクターを使ったアプリケーションのアーキテクチャについて紹介した資料です
hakobe (Yohei Fushii)
November 27, 2015
Tweet
Share
More Decks by hakobe (Yohei Fushii)
See All by hakobe (Yohei Fushii)
はてなにおけるプログラミング言語の歴史とこれから
hakobe
0
650
ざっとわかるPython
hakobe
4
6.8k
はてなで一人前のエンジニアになる方法
hakobe
3
6.8k
契約による設計の紹介
hakobe
6
20k
Scala In Perl Company
hakobe
11
12k
TDDの練習 Coding Kata の実践
hakobe
0
1.4k
なめらかにGHEに移行する方法
hakobe
19
7.7k
開発フロー@はてなブックマーク
hakobe
0
160
Git on WebApp with Perl
hakobe
1
3.8k
Other Decks in Programming
See All in Programming
선언형 UI에서의 상태관리
l2hyunwoo
0
140
プロダクトの品質に コミットする / Commit to Product Quality
pekepek
2
770
KubeCon + CloudNativeCon NA 2024 Overviewat Kubernetes Meetup Tokyo #68 / amsy810_k8sjp68
masayaaoyama
0
250
Refactor your code - refactor yourself
xosofox
1
260
たのしいparse.y
ydah
3
120
20年もののレガシープロダクトに 0からPHPStanを入れるまで / phpcon2024
hirobe1999
0
110
アクターシステムに頼らずEvent Sourcingする方法について
j5ik2o
4
200
Stackless и stackful? Корутины и асинхронность в Go
lamodatech
0
690
htmxって知っていますか?次世代のHTML
hiro_ghap1
0
330
testcontainers のススメ
sgash708
1
120
命名をリントする
chiroruxx
1
390
Security_for_introducing_eBPF
kentatada
0
110
Featured
See All Featured
Building Applications with DynamoDB
mza
91
6.1k
It's Worth the Effort
3n
183
28k
Making the Leap to Tech Lead
cromwellryan
133
9k
BBQ
matthewcrist
85
9.4k
Code Review Best Practice
trishagee
65
17k
Faster Mobile Websites
deanohume
305
30k
Stop Working from a Prison Cell
hatefulcrawdad
267
20k
Making Projects Easy
brettharned
116
5.9k
Music & Morning Musume
bryan
46
6.2k
Embracing the Ebb and Flow
colly
84
4.5k
Designing Dashboards & Data Visualisations in Web Apps
destraynor
229
52k
[RailsConf 2023] Rails as a piece of cake
palkan
53
5k
Transcript
ΞΫλʔʹΑΔ ฒߦॲཧΞϓέʔγϣϯ ΞʔΩςΫνϟ hakobe932 1
͢͜ͱ •ฒߦॲཧʹ͍͓ͭͯ͞Β͍ •ΞΫλʔͷհ •ErlangʹΑΔΞϓϦέʔγϣϯ ΞʔΩςΫνϟ
The Free Lunch Is Over ϋʔυΣΞਐԽΛ ͯγεςϜੑೳ͕ ্͢Δ࣌ऴΘͬͨ (2005ʹ)
IUUQXXXHPUXDBQVCMJDBUJPOTDPODVSSFODZEEKIUN ϚϧνίΞCPUͷ࣌
ϚϧνίΞCPUΛੜ͔ͤΔ ฒߦॲཧϓϩάϥϛϯάٕज़Λ ֶ΅͏!
AppΤϯδχΞʹؔͳ͍? Q:WebΞϓϦέʔγϣϯͷੑೳ جຊతʹIOόϯυ͡ΌΜ? A:ϨΠςϯγʹؔͯ͠ɻ େྔͷλεΫΛ࣌͘͞ͷεϧʔϓοτ ͕͕ͪ͏ɻ গͳ͍αʔόͰͨ͘͞Μࣄ͕Ͱ͖͓ͯಘ
ϚϧνίΞCPUΛ׆༻ •୯Ґ࣌ؒʹ͔͞ͳ͍ͱ͍͚ͳ͍ σʔλ૿͍͑ͯΔ • ϒϩά/ϒοΫϚʔΫͷϦΫΤετ • Mackerelͷσʔλͷϙετ •ࠓ͋ΔΈͰ͖͖͞Εͳ͘ͳͬ ͨΒ?? •
ࣗͰ࡞Δͱ͍͏બࢶΛ࣋ͭ
ฒߦॲཧ͓͞Β͍
ϓϩηεʹΑΔฒߦॲཧ •ॲཧʹରԠ͢ΔϓϩηεΛىಈ(fork) ͠OSʹεέδϡʔϧͤ͞Δ Good • ҆શ • ϓϩάϥϛϯάύϥμΠϜ͕มΘΒͳ͍ Bad •
ϝϞϦ/CPUΛ͏ • σʔλڞ༗͕େม(File, Pipe, Socket, DB)
εϨουʹΑΔฒߦॲཧ •ҰͭͷϓϩηεͰෳͷ࣮ߦ୯Ґ Good • ൺֱతϦιʔεΛΘͳ͍ • ϝϞϦϑΝΠϧϋϯυϧͳͲΛ ڞ༗Ͱ͖Δ Bad •
҆શʹڞ༗ϦιʔεΛ͏ͷ͕͍͠
εϨουΛ҆શʹ͏ʹ •εςʔτϨεʹ͢Δ •ڞ༗ϝϞϦͷΞΫηεΛಉظԽ͢Δ • Lock/Mutex/Semaphore/synchronized/ volatile … ͨͩ͠ਖ਼͘͠͏ͷ͍͠ σουϩοΫ!
σʔλڝ߹!!
ྫ: σουϩοΫ public class LeftRightDeadlock { private final Object
left = new Object(); private final Object right = new Object(); public void leftRight() { synchronized (left) { synchronized (right) { doSomething(); } } } public void rightLeft() { synchronized (right) { synchronized (left) { doSomething(); } } } } " # leftΛ ϩοΫ rightΛ ϩοΫ rightΛ ϩοΫ͍ͨ͠ leftΛ ϩοΫ͍ͨ͠ Ӭԕʹͭ Ӭԕʹͭ
ฒߦॲཧͷநԽ •εϨουηʔϑͳΦϒδΣΫτʹ நԽ͢Δ •͍͠ॲཧΛ҆શʹͱΓ͔͋ͭ͑Δ ෦ͱ͖ͯ͠ΓΘ͚Δ •ྫ: Future •ࢀߟ: Javaฒߦॲཧϓϩάϥϛϯά
IUUQTQSBZJPEVTF
ΞΫλʔ
ΞΫλʔ •εϨουͷΑ͏ͳฒߦॲཧͷ୯Ґ •ϝοηʔδͷૹड৴ʹΑͬͯڠௐ •ΞΫλʔؒͰϝϞϦΛڞ༗͠ͳ͍ ˠ ڝ߹σουϩοΫ͕ى͖ʹ͍͘
ΞΫλʔ͕Ͱ͖Δ͜ͱ 1.ϝοηʔδΛผͷΞΫλʔʹૹ৴͢Δ 2.ผͷΞΫλʔΛ࡞Δ 3.ϝοηʔδΛड৴ͯࣗ͠ͷঢ়ଶΛ มߋ͢Δ
ϝοηʔδͷૹड৴ Actor ϝʔϧϘοΫε ϝοηʔδ ঢ়ଶͷߋ৽ Actor
৽͍͠ΞΫλʔͷੜ Actor Actor Actor Actor ੜ ੜ ੜ
ΞΫλʔʹΑΔγεςϜ Actor Actor Actor Actor Actor Actor
•ΞΫλʔΛߴʹେྔʹੜͰ͖Δ •ΞΫλʔΛίϯτϩʔϧ͢ΔΈ •ΞϓϦέʔγϣϯͱͯ͠ಈ࡞ͤ͞Δ Έ ΞΫλʔʹΑΔ ࣮༻తͳΞϓϦέʔγϣϯߏங ErlangͷྫͰ۩ମతʹհ →
Erlangʹ͍ͭͯগ͠ •ؔܕ, ಈతܕ, มͷ࠶ೖෆՄ •ΞΫλʔத৺ͷΞʔΩςΫνϟ •༗໊ͳΞϓϦέʔγϣϯྫ: • RabbitMQ, Riak, CouchDB,
ిͷަػ
Erlang༻ޠͰΞΫλʔͷ͜ ͱΛϓϩηεͱݺͼ·͢!!! ͕ͱ͜͜ͰΞΫλʔͱ͍͏͜ͱͰʜ
ErlangͷΞΫλʔ •ErlangϓϩάϥϜΞΫλʔͷू߹ •ඇৗʹܰྔͰߴ • 1ͭ300ϫʔυ • ୯७ͳͷͰ͘͢ىಈͰ͖Δ • ࢀߟ: http://d.hatena.ne.jp/sakaia/
20150315#1426406872 •ωΠςΟϒεϨουͰͳ͘ άϦʔϯεϨουతʹಈ࡞
spawn !! Pid = spawn(fun() -> receive hello ->
io:format("Hello, World!~n") end end). Pid ! hello. Actor Actor Actor Actor Actor Actor hello Hello, World!
ΞΫλʔͷίϯτϩʔϧ •Ұ෦ͷΞΫλʔ͕Τϥʔ͕͓͖ͨΒ? Actor Actor Actor Actor Actor Actor Error!!
Let it crash •Τϥʔ͕͓͖ΔͱΞΫλʔ͕Ϋϥογϡ! Actor Actor Actor Actor Actor
Actor γεςϜͷଞͷ෦Ͳ͏͢Δ?
Link Actor Actor Actor Actor Actor Actor -JOL Ұํ͕ऴྃ
ࣗಈతʹ ͏Ұํऴྃ γεςϜશମΛ࣮֬ʹऴྃͰ͖Δ
Link + process_trap Actor Actor Actor Actor Actor Actor
-JOL Ұํ͕ऴྃ &9*5 ͕ૹ৴͞ΕΔ &9*5
•ऴྃϋϯυϧͰ͖Δ Link + process_trap Actor Actor ޙॲཧΛ͢Δ Actor Actor
࠶ىಈ͢Δ Actor ͋Δ͍ Monitorͱ͍͏Έ͋Δ
Ͳ͏͍͏ઓུͰΤϥʔॲཧ͢Δ͔? •࣮͜ΕWorkerManagerͰͨ͠ Server Worker Worker Worker Manager Main શମΛཧ
ϦΫΤετ ड͚͚ ϫʔΧʔΛ ཧ
Worker͕Τϥʔ Server Worker Worker Worker Manager Main →Worker͚ͩ࠶ىಈ͢ΕΑ͍
Server͕Τϥʔ Server Worker Worker Worker Manager Main →ܑఋͱͦͷࢠ͋Θͤͯ࠶ىಈ
Supervisorʹ·͔ͤΑ͏ •ΞΫλʔऴྃॲཧʹύλʔϯ͕͋Δ •Link(Monitor)Λͬͯஸೡʹ࣮ ͢Δͷୀ۶ •Α͋͘ΔཧॲཧΛݞΘΓͯ͘͠Ε Δͷ͕Supervisor
࠶ىಈͷઓུ •࠶ىಈ͢Δ͔Ͳ͏͔? • permanent/temporary/transient •ԿΛ࠶ىಈ͢Δ͔? • one_for_one/one_for_all/one_for_rest •ͲΕ͘Β͍Ͱ͖͋ΒΊΔ͔? • nҎʹmճ࠶ىಈͨ͠Βࢮ͵
Managerͷઓུ Server Worker Worker Worker Manager Main permanent, one_for_one
Mainͷઓུ Server Worker Worker Worker Manager Main permanent one_for_all
•SupervisorπϦʔ͕ ΞϓϦέʔγϣϯΛߏ͢Δ ErlangͷΞϓϦέʔγϣϯ Application
RabbitMQͷSupervisorπϦʔ IUUQXXXTMJEFTIBSFOFUPME@TPVOEEJTTFDUJOHUIFSBCCJU
SupervisorʹΑΔো ΞʔΩςΫνϟ •ΤϥʔͷॏཁʹԠͯ͡ΞΫλʔΛׂ •Τϥʔ࣌ͷΞΫλʔͷઓུΛSupervisor ʹΑΓઃఆ • దͳൣғΛඞཁʹԠͯ͡࠶ىಈ • Τϥʔͷ͜ͱΛߟ͑ͳ͍ͱ͍͚ͳ͍ઃܭ
·ͱΊ •ΞΫλʔ҆શͰॊೈͳ ฒߦॲཧϓϩάϥϛϯάٕज़ͷͻͱͭ •ErlangΞΫλʔʹΑΔγεςϜͷ ࣮༻తͳϓϥοτϑΥʔϜ •SupervisorΛ༻͍ͨোੑ ΞʔΩςΫνϟ͕ߏஙͰ͖Δκ
ࢀߟจݙ •Javaฒߦॲཧϓϩάϥϛϯά - http://www.amazon.co.jp/dp/4797337206 •͍͢͝ErlangΏ͔͍ʹֶ΅͏ʂ - http://www.amazon.co.jp/dp/B00MLUGZIS/ •ฒߦॲཧॳ৺ऀͷͨΊͷAkkaೖ -
http://www.slideshare.net/sifue/akka-39611889 •ϓϩάϥϛϯάݴޠͷύϥμΠϜγϑτʔScala͔ΒݟΔؔܕͱฒ ྻੑ࣌ͷນ։͚ - http://www.slideshare.net/TanUkkii/functional-and- concurencyinscala •Web API αʔόʔͱͯ͠ͷ Elixir ͷՄೳੑ - https://speakerdeck.com/naoya/web-api-sabatositefalse-elixir- falseke-neng-xing