アクターによる
並行処理アプケーション アーキテクチャ

アクターによる
並行処理アプケーション アーキテクチャ

社内勉強会でアクターとアクターを使ったアプリケーションのアーキテクチャについて紹介した資料です

7e8bb5931a14030dc6a297aa364197bf?s=128

hakobe (Yohei Fushii)

November 27, 2015
Tweet

Transcript

  1. ΞΫλʔʹΑΔ
 ฒߦॲཧΞϓέʔγϣϯ ΞʔΩςΫνϟ hakobe932 1

  2. ࿩͢͜ͱ •ฒߦॲཧʹ͍͓ͭͯ͞Β͍ •ΞΫλʔͷ঺հ •ErlangʹΑΔΞϓϦέʔγϣϯ
 ΞʔΩςΫνϟ 

  3. The Free Lunch Is Over  ϋʔυ΢ΣΞਐԽΛ
 ଴ͯ͹γεςϜੑೳ͕
 ޲্͢Δ࣌୅͸ऴΘͬͨ
 (2005೥ʹ)

    IUUQXXXHPUXDBQVCMJDBUJPOTDPODVSSFODZEEKIUN ϚϧνίΞCPUͷ࣌୅
  4. ϚϧνίΞCPUΛੜ͔ͤΔ
 ฒߦॲཧϓϩάϥϛϯάٕज़Λ ֶ΅͏!

  5. AppΤϯδχΞʹؔ܎ͳ͍? Q:WebΞϓϦέʔγϣϯͷੑೳ͸
 جຊతʹ͸IOό΢ϯυ͡ΌΜ? A:ϨΠςϯγʹؔͯ͠͸ɻ
 େྔͷλεΫΛ͞͹࣌͘ͷεϧʔϓοτ ͕͕ͪ͏ɻ  গͳ͍αʔόͰͨ͘͞Μ࢓ࣄ͕Ͱ͖͓ͯಘ

  6. ϚϧνίΞCPUΛ׆༻ •୯Ґ࣌ؒʹ͞͹͔ͳ͍ͱ͍͚ͳ͍
 σʔλ͸૿͍͑ͯΔ • ϒϩά/ϒοΫϚʔΫ΁ͷϦΫΤετ • Mackerel΁ͷσʔλͷϙετ •ࠓ͋Δ࢓૊ΈͰ͸͞͹͖͖Εͳ͘ͳͬ ͨΒ?? •

    ࣗ෼Ͱ࡞Δͱ͍͏બ୒ࢶΛ࣋ͭ 
  7. ฒߦॲཧ͓͞Β͍

  8. ϓϩηεʹΑΔฒߦॲཧ •ॲཧʹରԠ͢ΔϓϩηεΛىಈ(fork) ͠OSʹεέδϡʔϧͤ͞Δ Good • ҆શ • ϓϩάϥϛϯάύϥμΠϜ͕มΘΒͳ͍ Bad •

    ϝϞϦ/CPUΛ࢖͏ • σʔλڞ༗͕େม(File, Pipe, Socket, DB) 
  9. εϨουʹΑΔฒߦॲཧ •Ұͭͷϓϩηε಺Ͱෳ਺ͷ࣮ߦ୯Ґ Good • ൺֱతϦιʔεΛ࢖Θͳ͍ • ϝϞϦ΍ϑΝΠϧϋϯυϧͳͲΛ
 ڞ༗Ͱ͖Δ Bad •

    ҆શʹڞ༗ϦιʔεΛ࢖͏ͷ͕೉͍͠ 
  10. εϨουΛ҆શʹ࢖͏ʹ͸ •εςʔτϨεʹ͢Δ •ڞ༗ϝϞϦ΁ͷΞΫηεΛಉظԽ͢Δ • Lock/Mutex/Semaphore/synchronized/ volatile …  ͨͩ͠ਖ਼͘͠࢖͏ͷ͸೉͍͠
 σουϩοΫ!

    σʔλڝ߹!!
  11. ྫ: σουϩοΫ  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Λ
 ϩοΫ͍ͨ͠ Ӭԕʹ଴ͭ Ӭԕʹ଴ͭ
  12. ฒߦॲཧͷந৅Խ  •εϨουηʔϑͳΦϒδΣΫτʹ
 ந৅Խ͢Δ •೉͍͠ॲཧΛ҆શʹͱΓ͔͋ͭ͑Δ
 ෦඼ͱ͖ͯ͠ΓΘ͚Δ •ྫ: Future •ࢀߟ: Javaฒߦॲཧϓϩάϥϛϯά

  13.  IUUQTQSBZJPEVTF

  14. ΞΫλʔ

  15. ΞΫλʔ •εϨουͷΑ͏ͳฒߦॲཧͷ୯Ґ •ϝοηʔδͷૹड৴ʹΑͬͯڠௐ •ΞΫλʔؒͰϝϞϦΛڞ༗͠ͳ͍
 ˠ ڝ߹΍σουϩοΫ͕ى͖ʹ͍͘ 

  16. ΞΫλʔ͕Ͱ͖Δ͜ͱ 1.ϝοηʔδΛผͷΞΫλʔʹૹ৴͢Δ 2.ผͷΞΫλʔΛ࡞Δ 3.ϝοηʔδΛड৴ͯࣗ͠෼ͷঢ়ଶΛ
 มߋ͢Δ 

  17. ϝοηʔδͷૹड৴  Actor ϝʔϧϘοΫε ϝοηʔδ ঢ়ଶͷߋ৽ Actor

  18. ৽͍͠ΞΫλʔͷੜ੒  Actor Actor Actor Actor ੜ੒ ੜ੒ ੜ੒

  19. ΞΫλʔʹΑΔγεςϜ  Actor Actor Actor Actor Actor Actor

  20. •ΞΫλʔΛߴ଎ʹେྔʹੜ੒Ͱ͖Δ •ΞΫλʔΛίϯτϩʔϧ͢Δ࢓૊Έ •ΞϓϦέʔγϣϯͱͯ͠ಈ࡞ͤ͞Δ
 ࢓૊Έ ΞΫλʔʹΑΔ
 ࣮༻తͳΞϓϦέʔγϣϯߏங  ErlangͷྫͰ۩ମతʹ঺հ →

  21. Erlangʹ͍ͭͯগ͠ •ؔ਺ܕ, ಈతܕ෇, ม਺΁ͷ࠶୅ೖෆՄ •ΞΫλʔத৺ͷΞʔΩςΫνϟ •༗໊ͳΞϓϦέʔγϣϯྫ: • RabbitMQ, Riak,
 CouchDB,

    ి࿩ͷަ׵ػ 
  22. Erlang༻ޠͰ͸ΞΫλʔͷ͜ ͱΛϓϩηεͱݺͼ·͢!!! ͕ͱ͜͜Ͱ͸ΞΫλʔͱ͍͏͜ͱͰʜ

  23. ErlangͷΞΫλʔ •ErlangϓϩάϥϜ͸ΞΫλʔͷू߹ •ඇৗʹܰྔͰߴ଎ • 1ͭ300ϫʔυ • ୯७ͳͷͰ͢͹΍͘ىಈͰ͖Δ • ࢀߟ: http://d.hatena.ne.jp/sakaia/

    20150315#1426406872 •ωΠςΟϒεϨουͰ͸ͳ͘
 άϦʔϯεϨουతʹಈ࡞ 
  24. spawn !!  Pid = spawn(fun() -> receive hello ->

    io:format("Hello, World!~n") end end). Pid ! hello. Actor Actor Actor Actor Actor Actor hello Hello,
 World!
  25. ΞΫλʔͷίϯτϩʔϧ •Ұ෦ͷΞΫλʔ͕Τϥʔ͕͓͖ͨΒ?  Actor Actor Actor Actor Actor Actor Error!!

  26. Let it crash •Τϥʔ͕͓͖ΔͱΞΫλʔ͕Ϋϥογϡ!  Actor Actor Actor Actor Actor

    Actor γεςϜͷଞͷ෦෼͸Ͳ͏͢Δ?
  27. Link  Actor Actor Actor Actor Actor Actor -JOL Ұํ͕ऴྃ

    ࣗಈతʹ
 ΋͏Ұํ΋ऴྃ γεςϜશମΛ࣮֬ʹऴྃͰ͖Δ
  28. Link + process_trap  Actor Actor Actor Actor Actor Actor

    -JOL Ұํ͕ऴྃ &9*5
 ͕ૹ৴͞ΕΔ &9*5
  29. •ऴྃϋϯυϧͰ͖Δ Link + process_trap  Actor Actor ޙॲཧΛ͢Δ Actor Actor

    ࠶ىಈ͢Δ Actor ͋Δ͍͸ Monitorͱ͍͏࢓૊Έ΋͋Δ
  30. Ͳ͏͍͏ઓུͰΤϥʔॲཧ͢Δ͔? •࣮͸͜Ε͸WorkerManagerͰͨ͠  Server Worker Worker Worker Manager Main શମΛ؅ཧ

    ϦΫΤετ
 ड͚෇͚ ϫʔΧʔΛ
 ؅ཧ
  31. Worker͕Τϥʔ  Server Worker Worker Worker Manager Main →Worker͚ͩ࠶ىಈ͢Ε͹Α͍

  32. Server͕Τϥʔ  Server Worker Worker Worker Manager Main →ܑఋͱͦͷࢠ΋͋Θͤͯ࠶ىಈ

  33. Supervisorʹ·͔ͤΑ͏ •ΞΫλʔऴྃॲཧʹ͸ύλʔϯ͕͋Δ •Link(΍Monitor)Λ࢖ͬͯஸೡʹ࣮૷
 ͢Δͷ͸ୀ۶ •Α͋͘Δ؅ཧॲཧΛݞ୅ΘΓͯ͘͠Ε Δͷ͕Supervisor 

  34. ࠶ىಈͷઓུ •࠶ىಈ͢Δ͔Ͳ͏͔? • permanent/temporary/transient •ԿΛ࠶ىಈ͢Δ͔? • one_for_one/one_for_all/one_for_rest •ͲΕ͘Β͍Ͱ͖͋ΒΊΔ͔? • n෼Ҏ಺ʹmճ࠶ىಈͨ͠Βࢮ͵

    
  35. Managerͷઓུ  Server Worker Worker Worker Manager Main permanent,
 one_for_one

  36. Mainͷઓུ  Server Worker Worker Worker Manager Main permanent one_for_all

  37. •SupervisorπϦʔ͕
 ΞϓϦέʔγϣϯΛߏ੒͢Δ ErlangͷΞϓϦέʔγϣϯ  Application

  38. RabbitMQͷSupervisorπϦʔ IUUQXXXTMJEFTIBSFOFUPME@TPVOEEJTTFDUJOHUIFSBCCJU

  39. SupervisorʹΑΔ଱ো֐
 ΞʔΩςΫνϟ •Τϥʔͷॏཁ౓ʹԠͯ͡ΞΫλʔΛ෼ׂ •Τϥʔ࣌ͷΞΫλʔͷઓུΛSupervisor ʹΑΓઃఆ • ద੾ͳൣғΛඞཁʹԠͯ͡࠶ىಈ • Τϥʔͷ͜ͱΛߟ͑ͳ͍ͱ͍͚ͳ͍ઃܭ 

  40. ·ͱΊ  •ΞΫλʔ͸҆શͰॊೈͳ
 ฒߦॲཧϓϩάϥϛϯάٕज़ͷͻͱͭ •Erlang͸ΞΫλʔʹΑΔγεςϜͷ
 ࣮༻తͳϓϥοτϑΥʔϜ •SupervisorΛ༻͍ͨ଱ো֐ੑ
 ΞʔΩςΫνϟ͕ߏஙͰ͖Δκ

  41. ࢀߟจݙ  •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