Share
Embed
Actor, Thread and me (RubyKaigi2015)
Everybody says. Actor helps to solve the multithreading problem. Actor is awesome. Threads are 💩
Really?
An actor model is "just a model", just like MVC.
Today, Recap what Actor Model is.
seki at druby.org
December 13, 2015
Transcript
86 slides
-
slide sponsors 嫣鹈،ؙة٦ךֿהח鑫׃ֻזؔٝٓ؎ٝ⸇דֹ؝ىُصذ؍״ TBQQPSPCFBNHJUIVCJP ؟ٝٓ؎ؤ⳿ꨡד遤ֻ3VCZ8PSME$POGFSFODFծ嫣䎃㥨鐰㹋倵⚥דׅկ勻䎃 װתׅךדծֶ㉏ְさׇ♴ְׁZBODZB 虠加歕ջ梾楤㼔枾䒍莔ռ׀勻䏄ֶ䖉׃גֶתׅ!PHJKVO սטվ䎃剢ַך➬✲!1613- ջأذ؍٦ـؤռ䋆涪㡰⚥!NB
ջأذ؍٦ـؤռ顠ְת׃!WFTUJHF@ ջأذ؍٦ـؤռ顠ְתׅ!USBDL ֿחכֿחכ!IJCBSJZB -
more slide sponsors ׃ծהן٦ׅה!USJDLOPUFT הגַכ剢דׅկ✮㹀ծ甧גגְֶג♴ְׁי!NBNF 5BLBZVLJ,BNJZBNB!UBLLJJ תֹזְַծ罋ִתׅ![[BL@KQ E3VCZח״ⴓ侔٥8FCفؚٗٓىؚٝ ؔ٦ي爡F#PPL4UPSF IUUQ
FTUPSFPINTIBDPKQUJUMFT1!IJTBTIJN דEFGBVMU@TDPQF⢪ְתַׅ!TVHJOPZ ؔٔآشٕ5ءٍخך5.*9ծ倜㉀ㅷؙٓحثغحؚ㥨鐰涪㡰⚥!JHBJHB זׁתծTFLJ眍ֶ嚂׃ְֻׁկ䊛䃵歲ך倯כծLBXBTBLJSCծZPLPIBNBSC ֶ鑐׃ְֻׁկ,ذٖؽך飑ⰅכծTFLJׁ穗歋ד׀湱锑ְֽה׀⼿⸂׃תׅկ!UTVCPJ -
ॳΓ10प dRuby ʹΑΔ ؔকढ़ஶ ࢄ ɾ Web ϓϩάϥϛϯά 10th anniversary
after the 1st edition -
Actor is better than Thread ϚϧνεϨουͰൃੜ͢Δ͕ղܾ͢Δ εϨουͰਖ਼͘͠ॻ͘ͷΉ͔͍ͣ͠ ΞΫλʔͳΒ؆୯ʹॻ͚Δ εϨου ຊʁ
Everybody says. Actor helps to solve the multithreading problem. Actor is awesome. Threads are . Really? -
Actor and Thread εϨουؒͷσʔλͷަΛ੍ݶ ෳͷεϨου͕σʔλΛॻ͖͑ͳ͍ ΞΫλʔͳΒ؆୯ͩ ͑ʁͦ͜ͳͷʁ - Control passing
data between threads - Properly handle changeing data between threads - So, that's the point? -
Process and Thread εϨουͱϓϩηεͷҧ͍ ϝϞϦۭؒΛڞ༗Ͱ͖Δ ϝϦοτͰ͋ΓσϝϦοτͰ͋Δ ͭ·ΓͦΕ͕ͦ͜ಛͰ͋Δ ϓϩηεͰ͍͍ͷͰʂ Difference between
Thread / Process - Could share memory space - So, that's one of the big characteristics of threads Well, couldn't it be process ...!! -
Model ΞΫλʔϞσϧͨͩͷϞσϧ MVCΈ͍ͨʹͨͩͷϞσϧ ܕ ΑΓྑ͍εϨουͰͳ͍ͱࢥ͏ An actor model is "just
a model;" - Just like MVC An actor model probably isn't a "better thread" -
Actor ࣮ߦओମ ୭ʹअຐ͞Εͳ͍ ࣗͷϖʔεͰࣄ͢Δ - Execution subject - Cannot be
interrupted by anyone - Work at your own pace -
Ether (Æther) ͔ͭͯޫͷΛ͑Δഔ࣭ͱͯ͠Ծ͞Ε͍࣭ͯͨ Ethernetͷޠݯ? ͔͍͍͔ͬ͜ΒࠓΤʔςϧ - Æther used to be
thought as medium that transmit llight waves - Originated from Ethernet ? - "Æther" sounds good to me, so I'll call Messaging System "Ether" today -
Send ࣗͷ߹ͷྑ͍࣌ʹ ϝοηʔδΛૹΔ͜ͱ͕Ͱ͖Δ ฦ৴͕ඞཁͳΒͦΕ ϝοηʔδͰΒ͏ Could send messages whenever you
wish If necessary, you could also receive message for a reply -
Receive ࣗͷ߹ͷྑ͍࣌ʹࣗѼͷ ϝοηʔδΛड͚औΔ ॱෆಉ Could receive messages whenever you wish.
Received in random order -
͜Ε͚ͩͷಓ۩ͰγεςϜΛ ॻ͜͏ ଞʹActorͷੜ͋Δ͚ͲׂѪ ےτϨΈ͍ͨͳײ͡ ࢥߟ࣮ݧʁ Let's try to make system
with these instruments - Skip other topics like Actor Creation - Maybe a thought experiment? -
ۙʹ͋ΔΞΫλʔϞσϧ Win32ΞϓϦͳͲͷEvent Drivenͳͭ ͰεϨου͕ඞཁʹͳͬͨΜͩΑͳ ϞϊεϨουͰselectଐΛͬͨsocketϓϩ άϥϛϯάͱ͔ N̋de.jsʁ ͍ΖΜͳγεςϜͰݟΒΕΔ Common examples
of Actor Model - MFC, - Single threaded socket client/server programming using select - Like these, you can see it in other systems -
client / server 1. Receive 2. Working 3. Send 0.
Send 4. Receive -
Simple Worker def oso_matz_RPC request = ... ether.send(:oso_matz, request) return
ether.receive end while true request = ether.receive result = do_it(request) ether.send(request.from, result) end -
ࣗͷ߹͚ͩͰಈ͘ ୭ʹׯব͞Εͳ͍ ߹ͷྑ͍࣌ʹಡΈɺ߹ͷྑ͍࣌ʹॻ͘ ؆୯ʂ҆৺ʂ εϨουͳΜͯ ຊ͔ʁ Works at your convenience
- No one interrupts you, Read and write whenever you wish - Reliable and easy to use Is this true ? -
Worker͕ڠௐ͢Δέʔε ୭͔ͷ݁ՌΛͬͯ͞Βʹܭࢉ͢Δέʔε Collaborative workers When you work with the results
of your collaborator's calculation -
୭͔ͷ݁ՌͰܭࢉ while true request = ether.receive # jushi_matz RPC ether.send(:choro_matz,
task1) task1_value = ether.receive result = do_it(request, task1_value) ether.send(request.from, result) end SFDFJWF✳א 2種類のメッセージ When you work with the results -
2छྨͷϝοηʔδΛѻ͏ 1. Receive 4. Do it 2. Send task1 3.
Receive task1 5. Send SFDFJWF✳א 2種類のメッセージ 2 types of messages -
͋ͱͰΔ pending = [] while true request = pending.shift ||
ether.receive ether.send(:choro_matz, task1) while true message = ether.receive if message.reply? task1_value = message break end pending << message end result = do_it(request, task1_value) ether.send(request.from, result) end 欲しいものを待つ あとでやるリスト Do it Later - wait for reply message - with list of Do it Later あとでやるリスト -
͋ͱͰΔϦετ 1. Receive 4. Do it 2. Send task1 3.
Receive task1 5. Send 処理できないやつをあとでやる Stock and process do-it-later strategy -
͋ͱͰΔ࡞ઓ... ࣗͷ߹ͷྑ͍࣌ʹΔΑ Γ͔͚ͷ͕ऴΘ͔ͬͯΒ࣍ͷཁٻΛΔ ࣗ͏·͘ߦ͘ - Process whenever you're ready -
Finish working process before moving on to the next request - For myself, it works fine -
૬ޓʹґଘ͢Δ෦͕͋ͬͨΒ... ݏͳ༧ײ͕͢Δ Bad smells... - What if there were mutual
dependency between workers.. -
"͋ͱͰΔ"Λ࣋ͬͨ··͓ޓ͍ʹreplyΛͬͪΌ͏ ݏͳ༧ײ͕͢Δ They wait for their next reply, even though
the have their stocks at hand -
20ੈل ػೳࢄ ͜ͷόάͬͨ͜ͱ͋Δ These bugs sound familiar - 20th century
- Functional distribution -
Ӆ͞Εͨґଘ͕͋ͬͨ ࣌ʑɺΈΜͳ͕replyͪʹͳΔ ୭ࢭ·ͬͯͳ͍͚ͲγεςϜࢭ·ͬͯΔ Hidden dependency...revealed - All of the workers
wait for their replies once in a while - Everyone is trying to work, but the system itself stops -
·͞ʹnon-blocking ฦ৴ʹݶΒͣෳͷϝοηʔδΛͭέʔε Future/PromiseͰͷ݁Ռ͍߹Θͤͱ͔ ෳͷΠϕϯτΛͬͯॲཧ͢Δͱ͔ (ڞ༗ࢿݯͱ͔) This WAS actually "non-blocking" When
multiple messages, including replies, are held by workers - e.g. Query for the results between Future and Promise - e.g. Start processing when some events finish - Not only messages but the other shared resources -
ڞ༗ࢿݯͷૢ࡞ڠௐ͢Δඞཁ͕͋Δ ΞΫλʔͱߟ͑ͯੳ͢Δͱ͍͍Αʂ খωλ/ڞ༗ࢿݯͷૢ࡞ e.g. How to use shared resources between
Actors? I think shared is an actor. -
͋ͱͰΔ࡞ઓ... μϝͩͬͨ ࣗͷ߹ͷྑ͍࣌ʹಡΈɺॲཧ͠ɺॻ͘ ΈΜͳ͕ͦ͏Ͱ͖ΔʹͲ͏͢Δ͔ʁ Do-it -later strategy. It doesn't work.
How do you make it so that everybody can read, process, and write at their own pace? -
ͦ͏͍͏ঢ়گ͕ѱ͍ શͯͷΞΫλʔ͕୭ʹґଘ͠ͳ͍͖ʁ ͔ͯ͠͠αϒϧʔνϯͰ͍͍ʁ ͦΕͳΒΞΫλʔͷҙٛ... The issue is this situation -
Every actor should only depend on themselves -- Does subroutine work for this? --- if then, what's the meaning of Actor? -
ͦ͏͍͏ঢ়گ͕ѱ͍ ਖ਼͘͠ॻ͚͏·͍͘͘ͷʹʂ ͍͍ͨͯͦ͏ ʂ - write it the right way,
and it will work fine! -- This applies to usually everything -
ͦ͏͍͏ঢ়گΛड͚ೖΕΔ ΞΫλʔϞσϧ / ےτϨ / ࢥߟ࣮ݧ Ή͠Ζঢ়گΛड͚ೖΕΑ͏ʂ Accept these situations
- Actor is a model and a thought experiment -- Accept these difficult situations -
͜͏ͨ͠ΒͲ͏ͩΖ͏ ࣗΈΜͳͷ߹ʹ߹ΘͤΔɺͱఘΊΔ ࣄͷ్தͰͰ͖Δ͚͖ͩͭ͋͏ My suggestions: - Each actor waits and
serves others first, even if you have any process left -
event driven while true event = ether.receive case event.kind when
:request do_request(event) when :reply_choro_matz do_reply_choro_matz(event) .... end end -
͋͋callbackͶ while true event = ether.receive case event.kind when :request
do_request(event) when :reply_choro_matz do_reply_choro_matz(event) .... end end callback はこの分岐が 動的になっただけ Ah, callbacks - This branch just turned dynamic for callback -
్தܦաΛϝϞ͢ΔҨݴ࡞ઓ def do_request(event) uid = push_context { :from => event.form
} ether.send(:choro_matz, [args, uid]) end def do_reply_choro_matz(reply) uid = reply.uid context = pop_context(uid) value = do_oso_matz(context, reply) ether.send(context[:from], value) end save context restore context Save-the-progress strategy -
ॲཧͷྲྀΕΛࣗવʹॻ͚ͳ͍ ίϯςΩετΛཅʹѻΘͳ͖ΌͳΒͳ͍ ॲཧΛࣗવͳྲྀΕͱͯ͠ॻ͖ʹ͍͘ while true request = ether.receive # jushi_matz
RPC ether.send(:choro_matz, task1) task1_value = ether.receive result = do_it(request, task1_value) ether.send(request.from, result) end 元は単純だったのに Difficult to write the process naturally - It was simple originally... (but become complex) -
ෳͷίϯςΩετ͕͋ͬͨ ͚͕ࣗͩσʔλΛૢ࡞͢Δͷ͕҆શͷࠜڌ ͍·ෳͷίϯςΩετ͕ڞ༗͢Δ There were contexts - Specifying who handles
data is important for safety - But now they are handled in some contexts -
Fiberͷ͍ํʹࣅͯΔʁ ϞϊεϨουͰෳͷίϯςΩετ ໌ࣔతͳίϯςΩετΓସ͑ ಉ͡ऑ͕͋Δ Similar to Fiber? - Some contexts
in a single thread - Explicit context switch - Same weak point here -
ࢭ·Δ ϒϩοΫ͢Δॲཧ͕͋ͬͨΒશ෦ࢭ·Δ ϒϩοΫͯ͠શମಈ͔͍ͨ͠ ͦ͜ͰϚϧνεϨουͰ͢Αʂ ʢͦͦFiber͍͠͠ʣ Halt - Halt if there
is a process that blocks other one - But want to run a whole system if there is - This is good timing to use multi threads -
͓͢͢Ί γεςϜશମΞΫλʔϞσϧͰߟ͑Δ ΞΫλʔͷதϚϧνεϨου Recomended way - Think about a whole
system using Actor Model - In this context, Actor is multi thread -
github/seki/bartender Fiberͦ͠͏ͳͷͰͬͯΈͨʂ selectΛগ͠நԽ͢ΔϑϨʔϜϫʔΫ BTW - Fiber seems difficult, but i
tried. - This is a framework to abstract a 'select' a little -
Bartender::App App#[]=(event, fd, callback) ίʔϧόοΫͷొɻ Ϣʔβʔσʔλͳ͠ɻ⇦ MethodΦϒδΣΫτΛ͍͍ͤ͡ΌΜ App#delete(event, fd) App#run
ϝΠϯϧʔϓ App#[]= - registers a callback - Pass method object -
ReaderͱWriter ͜ΕͰෆศͳͷͰϢʔςΟϦςΟʔΛ non-block I/OΛblock෩ʹ͢Δ blockͦ͠͏ʹͳͬͨΒ࣮ߦݖΛख์͢ Fiber.yieldͶ - This is not
useful so let's provide a utility - Block-ish non-blocking I/O - If going to block, Fiber yield. -
Bartender::Reader#read(n) def read(n) while @buf.bytesize < n chunk = _read(n)
break if chunk.nil? || chunk.empty? @buf += chunk end @buf.slice!(0, n) end -
readͷଆͷ_read͕ϛι Θ͔Γʹ͍͚͘Ͳ͕͜͜Πέϝϯ The _read insight head is the point -
seems difficult, but hang in there. -
Bartender::Reader#_read(n) def _read(n) @fd.read_nonblock(n) rescue IO::WaitReadable select_readable retry end def
select_readable @bartender[:read, @fd] = Fiber.current.method(:resume) Fiber.yield ensure @bartender.delete(:read, @fd) end -
Bartender::Reader#_read(n) def _read(n) @fd.read_nonblock(n) rescue IO::WaitReadable select_readable retry end def
select_readable @bartender[:read, @fd] = Fiber.current.method(:resume) Fiber.yield ensure @bartender.delete(:read, @fd) end Method object -
Bartender::Reader#_read(n) def _read(n) @fd.read_nonblock(n) rescue IO::WaitReadable select_readable retry end def
select_readable @bartender[:read, @fd] = Fiber.current.method(:resume) Fiber.yield ensure @bartender.delete(:read, @fd) end back to Bartender -
Bartender::Reader#_read(n) def _read(n) @fd.read_nonblock(n) rescue IO::WaitReadable select_readable retry end def
select_readable @bartender[:read, @fd] = Fiber.current.method(:resume) Fiber.yield ensure @bartender.delete(:read, @fd) end Unregister callback -
dRubyϓϩτίϧ ࣮ݧ༻ΫϥΠΞϯτॻ͘ͷ͕໘ dRubyͷϓϩτίϧΛͤΔαʔόΛॻ͜͏ Writing a client for experiment is tough
So let's write a server that understands dRuby protocol -
Bartender::Server class Server def initialize(bartender, port, &blk) @bartender = bartender
@server = TCPServer.new(port) @bartender[:read, @server] = self.method(:on_accept) @blk = blk end def on_accept client = @server.accept reader = Reader.new(@bartender, client) writer = Writer.new(@bartender, client) fiber = Fiber.new do @blk.yield(reader, writer) end fiber.resume end end not important -
DRbEchoServer def initialize(bartender, port) @rdv = Rdv.new Bartender::Server.new(bartender, port) do
|reader, writer| begin while true _, msg, argv = req_drb(reader) case msg when 'push' value = @rdv.push(argv) else value = @rdv.pop end reply_drb(writer, true, value) end rescue p $! end end end not important -
DRbEchoServer def load(reader, marshal=true) sz = reader.read(4) sz = sz.unpack('N')[0]
data = reader.read(sz) return data unless marshal begin Marshal.load(data) rescue DRb::DRbUnknown.new($!, data) end end def req_drb(reader) ref = load(reader, false) msg = load(reader) argc = load(reader) argv = argc.times.collect { load(reader) } block = load(reader, false) [ref, msg, argv] end ブロックを気にせずreadし まくる Don't care about I/O blocking and context switch Just read it sequentially -
BartenderͰͷFiberͷ͍ํ I/OͷϒϩοΫͷࡍʹ࣮ߦݖΛख์͢ ϑϨʔϜϫʔΫ͕ྃΛଊ͑ͯ࠶։͢Δ - Fiber.yield when I/O block happens -
The framework catches its finish and then resumes the fiber -
ղܾ͠ͳ͔ͬͨ͜ͱ ͦͷϝιου͍ॲཧΛ͢Δͷʁ Δํ๏͕ͳ͍ Θ͔ͬͨͱ͜ΖͰͲ͏͍͍͔ͯ͠Θ͔Βͳ͍ εϨουͷํ͕Ϛγ Things not solved yet -
Halt when calling the method that blocks - Thread is relatively better -
method_missing class ActorsOffice def initialize(actor) @queue = Queue.new @thread =
Thread.new(actor) do catch(actor) do while true msg, arg, blk = @queue.pop actor.__send__(msg, *arg, &blk) end end end end def __thread__; @thread; end def method_missing(m, *a, &b) @queue.push([m, a, b]) end end -
ॻ͍͚ͨͲ Θͳ͔ͬͨʂ ͳΜͰྻԽ͍ͨ͠ਓ͚ I wrote, but not used. It's just
for those who like to serialize -
ether = Rinda::TupleSpace _, message = ether.take([:oso_matz, nil]) ether.write([message[:from], {
:reply => "Hi!" }) message = { :from => :choro_matz, :greeting => "Hello, World." } ether.write([:oso_matz, message]) ether.write([:todo_matz, message]) -
·͋ಈ͘ dRubyͱΈ߹ΘͤΕϓϩηεؒಈ͘Α Αͬͯ͘Δ Yes, it works. - You can run
it between processes / machines by using dRuby - I use it, frequently -
ΤʔςϧΛॻ͍ͯؾ͍ͮͨ ΞΫλʔͱΤʔςϧͷؔΞΫλʔʁ Ϟσϧͱͯ͠ͷΞΫλʔͲ͜ʹͰ͋Δͷ Ͱʂ As you may notice when listening
to my talk, the relationship between Actor and Ether may be Actor Model, too -
·ͱΊ Ϟσϧͱͯ͠ͷΞΫλʔΛ͓͞Β͍ ΑΓྑ͍εϨου͡Όͳ͍ؾΛڞ༗ Ͱ͖ͨʁ Summary - Recap what Actor Model
is - Feel like it's not just a better Thread, is it?