Upgrade to Pro — share decks privately, control downloads, hide ads and more …

アクターモデルの話

tmaeda
October 21, 2017

 アクターモデルの話

tmaeda

October 21, 2017
Tweet

More Decks by tmaeda

Other Decks in Technology

Transcript

  1. ΞΫλʔϞσϧͷ࿩
    2017.10.21 RubySapporo.beam #3
    ·͑ͩͱ΋͖

    View full-size slide

  2. ࣗݾ঺հ
    @tmaeda
    (ג)͑ʹ͠ςοΫ
    ࠷ۙ͸RailsͷཪͰಈ͘SidekiqͷδϣϒΛॻ͘࢓ࣄ͕ଟ͍Ͱ
    ͢
    ElixirΛ·ͬͨΓษڧதͰ͢

    View full-size slide

  3. &-*9*3ͷίʔυྫ
    defmodule Greeter do
    def hello do
    receive do
    name ->
    IO.puts "Hello #{name}"
    end
    hello()
    end
    end
    greeter =
    spawn(Greeter, :hello, [])
    send(greeter, "World")

    View full-size slide

  4. ࠓ೔͓࿩͍ͨ͜͠͠ͱ
    ΞΫλʔϞσϧ͕ੜ·Εͨഎܠ
    ΞΫλʔϞσϧ͸ԿΛղܾ͢Δͷ͔ʁ
    ΞΫλʔϞσϧͱͲ͏෇͖߹͑͹͍͍ͷ͔ʁ

    View full-size slide

  5. ΞΫλʔϞσϧ
    Carl Hewitt
    1973
    https://en.wikipedia.org/wiki/Actor_model

    View full-size slide

  6. πΠολʔ΋͋Δˠhttps://twitter.com/carlhewitt

    View full-size slide


  7. 1977ͷHewittͷ࿦จ
    In recent years, there has been a shift from the
    centralized serial computing system to the distributed
    parallel computing network. (Laws for Communicating
    Parallel Processes)

    https://dspace.mit.edu/bitstream/handle/1721.1/41962/AI_WP_134A.pdf
    ڞײͰ͖·͢ʁ
    ͪΐͬͱ౰࣌ͷ༷ࢠΛௐ΂ͯΈ·͠ΐ͏

    View full-size slide

  8. ʰ·Μ͕͸͡Ίͯ෺ޠʱʢ·Μ͕͸͡Ίͯ΋ͷ͕ͨΓʣͱ͸ɺ1978೥5݄6೔͔Β1984೥3݄31೔·ͰTBSܥྻ
    ہͰ์ૹ͞ΕͨμοΫεΠϯλʔφγϣφϧͱTBSڞಉ੡࡞ͷࢠڙ޲͚ͷڭཆ൪૊Ͱ͋Δɻશ305ճɻ

    View full-size slide

  9. ࠷ॳͷίϯϐϡʔλ
    1946 ENIAC

    View full-size slide

  10. ࠷ॳͷίϯϐϡʔλωοτϫʔΫ
    1950೥୅ޙ൒ Semi-Automatic Ground Environment

    View full-size slide

  11. ֩౥ࡌരܸػͷܴܸͷܭࢉ
    https://www.youtube.com/watch?v=dF75jHfawtM

    View full-size slide

  12. ࠷ॳͷεʔύʔίϯϐϡʔλʔ
    1964 CDC 6600 10MHz

    View full-size slide

  13. ೥ද
    1946 ENIAC 1956 SAGE 1965 CDC6600
    1971 Intel 4004
    740kHz
    1957 Fortran
    1958 LISP
    1959 COBOL 1972 C, Smalltalk
    1972 UNIX
    1973 Ethernet
    1969 ARPANET
    50kbsp
    ͜ͷลͷࠒͷ࿩ˢ
    1967 Thread

    View full-size slide


  14. ݚڀॴ΍܉ࣄࢪઃʹ͸ɺҰൠՈఉͰ͍͏ͱ͜Ζͷ1990೥
    ୅ʙ2000೥୅૬౰ͷίϯϐϡʔλͱωοτϫʔΫ؀ڥ͕
    ͋ͬͨͱ૝૾͞ΕΔɻ
    ͳΔ΄Ͳɺࢲ͕ݟ͍ͯͨ2000೥લޙͷ࿩ͩͱࢥ͑͹ɺ

    ڞײͰ͖Δɻ

    View full-size slide

  15. Կ͕໰୊͔ͩͬͨʁ
    ϨʔείϯσΟγϣϯͷ༗໊ͳ໰୊
    ۜߦޱ࠲ʹ10000ԁ͋Δ
    A͞Μ͕7000ԁҾ͖མͱͦ͏ͱ͢Δ
    B͞Μ͕8000ԁҾ͖མͱͦ͏ͱ͢Δ

    View full-size slide

  16. ͜Μͳίʔυ
    if Ҿ͖མͱֹ͠ <= ࢒ߴ
    ࢒ߴ -= Ҿ͖མͱֹ͠
    return true
    else
    return false
    end

    View full-size slide

  17. ϚϧνεϨουͰಈ͔͢ͱʁ
    "͞Μ #͞Μ
    ޱ࠲
    ࢒ߴ଍ΓΔʁ ࢒ߴ଍ΓΔʁ
    0, 0,
    ԁҾ͖མͱ͠
    ԁҾ͖མͱ͠
    ࢒ߴ10000ԁ
    ࢒ߴ͕ϚΠφεԁʂ

    View full-size slide

  18. ϩοΫͰղܾʁ
    ͜͜ͰϩοΫΛ͔͚Δ(1౓ʹ1ͭͷεϨου͔͠ڐ͞ͳ͍)
    if Ҿ͖མͱֹ͠ <= ࢒ߴ
    ࢒ߴ -= Ҿ͖མͱֹ͠
    return true
    else
    return false
    end
    ͜͜ͰϩοΫΛղ์
    ϩοΫΛ͠๨ΕΔ
    ղ์͠๨ΕΔ
    ΊΜͲ͍͘͞
    ෆ҆

    View full-size slide

  19. ೥ද
    1946 ENIAC 1956 SAGE 1965 CDC6600
    1971 Intel 4004
    740kHz
    1957 Fortran
    1958 LISP
    1959 COBOL 1972 C, Smalltalk
    1972 UNIX
    1973 Ethernet
    1969 ARPANET
    50kbsp
    ͜ͷลͷࠒͷ࿩ˢ
    1967 Thread

    View full-size slide

  20. ΧϓηϧԽͯ͠σʔλΛकΔ
    ͷ͕ΦϒδΣΫτࢦ޲ͷ
    ໨తͷ1ͭͩͬͨͷʹ…

    View full-size slide

  21. ΞΫλʔϞσϧ
    શͯ͸ΞΫλʔͰ͋Δɻ(OOPͰ͍͏ͱ͜Ζͷʮશͯ͸ΦϒδΣΫτͰ͋Δʯͱࣅ͍ͯΔ)
    ΞΫλʔ͸
    ܭࢉͰ͖Δɻ
    ༗ݶͷ৽͍͠ΞΫλʔΛੜ੒Ͱ͖Δɻ
    ଞͷΞΫλʔ΁༗ݶͷϝοηʔδΛૹΕΔɻ
    ࣍ʹड͚Δϝοηʔδ΁ͷ४උ͕Ͱ͖Δ(ঢ়ଶΛอ࣋Ͱ͖Δ)ɻ
    ϝʔϧϘοΫεΛ࣋ͪɺಧ͍ͨϝοηʔδΛ1ͭͣͭॱ൪ʹॲཧ͢Δɻ
    ※ϝοηʔδ͕ಧ͘ॱং͸ෆఆ
    ※ϝοηʔδ͸ಧ͔ͳ͍͔΋͠Εͳ͍

    View full-size slide

  22. ΞΫλʔϞσϧͰղܾ
    "͞Μ #͞Μ ޱ࠲
    SFDFJWF
    0,
    ԁҾ͖མͱ͠
    ԁҾ͖མͱ͠
    ࢒ߴ10000ԁ
    ϝʔϧϘοΫε
    ࢒ߴνΣοΫͯ͠
    ԁҾ͖མͱ࣮͠ߦ
    /(
    SFDFJWF
    ϝοηʔδ͸ඞͣ

    1݅ͣͭॱ൪ʹ

    ॲཧ͞ΕΔ

    View full-size slide

  23. ΞΫλʔϞσϧ

    ʮΦϒδΣΫτʯͱಡΈସ͑ͯΈΔ

    શͯ͸ΞΫλʔͰ͋Δɻ
    ΞΫλʔ͸
    ܭࢉͰ͖Δɻ
    ༗ݶͷ৽͍͠ΞΫλʔΛੜ੒Ͱ͖Δɻ
    ଞͷΞΫλʔ΁༗ݶͷϝοηʔδΛૹΕΔɻ
    ࣍ʹड͚Δϝοηʔδ΁ͷ४උ͕Ͱ͖Δ(ঢ়ଶΛอ࣋Ͱ͖Δ)ɻ
    ϝʔϧϘοΫεΛ࣋ͪɺಧ͍ͨϝοηʔδΛ1ͭͣͭॱ൪ʹॲཧ͢Δɻ
    ΦϒδΣΫτ
    ΦϒδΣΫτ
    ΦϒδΣΫτ
    ΦϒδΣΫτ
    ͚͕ͩ͜͜
    OOPʹͳ͍ಛ௃

    View full-size slide

  24. ΞΫλʔϞσϧ͸
    ΦϒδΣΫτࢦ޲ʹ
    ฒߦ࣮ߦʹର͢ΔΧϓηϧԽ
    Λ௥Ճͨ͠΋ͷͩͱ
    ߟ͑ΒΕΔ

    View full-size slide

  25. https://media.pragprog.com/titles/pb7con/actors.pdf
    Chapter5
    Actors
    More Object-Oriented
    than Objects

    View full-size slide

  26. ΞΫλʔϞσϧͷ࢖͍ํ
    ݪٛతʹ͸ʮશͯΛΞΫλʔͱ͢ΔʯͷͰɺObjectΛnew͢
    ΔΑ͏ͳؾ࣋ͪͰͲΜͲΜΞΫλʔΛ࡞͍͍ͬͯ(Erlang/
    Elixir༻ޠͰݴ͏ͱɺprocessΛspawn͍͍ͯ͠)ɻ
    ΞΫλʔϞσϧ͸ΦϒδΣΫτࢦ޲ͱ΄΅ಉ͡ߟ͑ํͳͷ
    ͰɺΦϒδΣΫτࢦ޲ͷߟ͑ํΛྲྀ༻ͯ͠ϓϩάϥϜΛ૊
    ΜͰ͍͍ɻσβΠϯύλʔϯͱ͔ɻ

    View full-size slide

  27. &-*9*3ͷίʔυྫ վ

    defmodule Greeter do
    def start do
    spawn(__MODULE__, :loop, [])
    end
    def hello(greeter, name) do
    send(greeter, {:hello, name})
    end
    # ͜͜͸privateͬΆ͍ؾ࣋ͪ
    def loop do
    receive do
    {:hello, name} ->
    IO.puts "Hello #{name}"
    end
    loop()
    end
    end
    greeter = Greeter.start
    Greeter.hello(greeter, name)
    ࣮૷ͷৄࡉ ϓϩηεͷىಈͱ͔ɺ
    ϝοηʔδͷ΍ΓऔΓͷੜʑ͍͠
    ͱ͜Ζ
    ͸Ӆ͢

    View full-size slide

  28. ͱݴΘΕ·ͯ͠΋ʜ
    ຊ౰ʹશ෦ΞΫλʔͰ͍͍ͷʁ
    Integer͢Β΋ʁ

    ˠຊ౰ʹඞવੑ͕͋ΔͷͰ͋Ε͹ɺ΍ͬͯ΋͍͍ɻ

    CounterΞΫλʔͱ͔Ͷɻ
    RailsͰΞϓϦ૊ΜͰΔͱThreadΛ࢖͍͍ͨ৔໘ͳΜͯ໓ଟʹͳ͍Μͩ
    ͚Ͳ…

    →Ϛετυϯ͸Sidekiqδϣϒͷմ

    https://github.com/tootsuite/mastodon/tree/master/app/workers

    View full-size slide

  29. ·ͱΊ
    ΞΫλʔϞσϧ͸ΦϒδΣΫτࢦ޲Λฒߦ؀ڥͰ΋ؾܰʹ࢖͑Δ
    Α͏ʹमਖ਼ͨ͠΋ͷͱߟ͑ΒΕΔͷͰɺΞΫλʔ͸༑ୡɺා͘ͳ
    ͍Α
    ιʔγϟϧωοτϫʔΫతͳػೳΛؚΉΞϓϦέʔγϣϯͰ͸
    ΦϒδΣΫτάϥϑ͕ෳࡶʹͳΓ͕ͪͳͷͰ݁ߏ࢖͑Δ৔໘͕
    ͋Δ
    ͦ͏͸ݴͬͯ΋ɺ࢖͍͗͢ΔͱύϑΥʔϚϯεͷ໰୊ͱ͔ಈ͖͕
    ෆఆͰΘ͔Γʹ͘͘ͳͬͨΓ͢ΔͷͰઅ౓Λ࣋ͭඞཁ͕͋Γͦ͏

    View full-size slide