Slide 1

Slide 1 text

"LLBʹΑΔ εϨουͷ࢖͍ํ ϦνϟʔυɹҏਅԬ

Slide 2

Slide 2 text

"LLBͱ͍͑͹

Slide 3

Slide 3 text

https://akka.io/

Slide 4

Slide 4 text

https://akka.io/

Slide 5

Slide 5 text

> Akka is a toolkit for building highly concurrent, distributed, and resilient message-driven applications for Java and Scala

Slide 6

Slide 6 text

•Thread •join •Runnable/Callable •Lock •synchronized •Atomic variables •volatile •ThreadLocal •DynamicVariable •ConcurrentHashMap •ConcurrentLinkedQueue •ForkJoin •ThreadPool •Executor/ExecutorService •ExecutionContext •Future •Thread intereference •Memory Consistency error •Dead lock •Live lock •Starvation •InterruptedException •Scheduling

Slide 7

Slide 7 text

શ෦ؾʹ͠ͳ͕Β ίʔυΛॻ͍͍ͯͩ͘͞

Slide 8

Slide 8 text

ແཧήʔ

Slide 9

Slide 9 text

͕ॿ͚ͯ͘Ε·͢

Slide 10

Slide 10 text

"DUPSʹΑΔ ϝοηʔδύογϯά "LLB

Slide 11

Slide 11 text

import scala.collection class MyActor extends Actor { //mutable state val state1: mutable.Map[String, String] = … val state2: mutable.ListBuffer[String] = … def receive = { //immutable message case MyMessage(msg) => procedure(msg) } def procedure(msg: String) { //࣍ͷϝοηʔδ͔ΒׂΓࠐ·Εͳ͍ } }

Slide 12

Slide 12 text

import scala.collection class MyActor extends Actor { //mutable state val state1: mutable.Map[String, String] = … val state2: mutable.ListBuffer[String] = … def receive = { //immutable message case MyMessage(msg) => procedure(msg) } def procedure(msg: String) { //࣍ͷϝοηʔδ͔ΒׂΓࠐ·Εͳ͍ } }

Slide 13

Slide 13 text

import scala.collection class MyActor extends Actor { //mutable state val state1: mutable.Map[String, String] = … val state2: mutable.ListBuffer[String] = … def receive = { //immutable message case MyMessage(msg) => procedure(msg) } def procedure(msg: String) { //࣍ͷϝοηʔδ͔ΒׂΓࠐ·Εͳ͍ } }

Slide 14

Slide 14 text

import scala.collection class MyActor extends Actor { //mutable state val state1: mutable.Map[String, String] = … val state2: mutable.ListBuffer[String] = … def receive = { //immutable message case MyMessage(msg) => procedure(msg) } def procedure(msg: String) { //࣍ͷϝοηʔδ͔ΒׂΓࠐ·Εͳ͍ } }

Slide 15

Slide 15 text

•Thread •join •Runnable/Callable •Lock •synchronized •Atomic variables •volatile •ThreadLocal •DynamicVariable •ConcurrentHashMap •ConcurrentLinkedQueue •ForkJoin •ThreadPool •Executor/ExecutorService •ExecutionContext •Future •Thread intereference •Memory Consistency error •Dead lock •Live lock •Starvation •InterruptedException •Scheduling ๨Ε͍͍ͯ

Slide 16

Slide 16 text

•Thread •join •Runnable/Callable •Lock •synchronized •Atomic variables •volatile •ThreadLocal •DynamicVariable •ConcurrentHashMap •ConcurrentLinkedQueue •ForkJoin •ThreadPool •Executor/ExecutorService •ExecutionContext •Future •Thread intereference •Memory Consistency error •Dead lock •Live lock •Starvation •InterruptedException •Scheduling ๨Ε͍͍ͯ $PODVSSFODZಛ༗ͷ໰୊͸Ͳ͏ͯ͠΋ ى͜Γ·͢

Slide 17

Slide 17 text

•Thread •join •Runnable/Callable •Lock •synchronized •Atomic variables •volatile •ThreadLocal •DynamicVariable •ConcurrentHashMap •ConcurrentLinkedQueue •ForkJoin •ThreadPool •Executor/ExecutorService •ExecutionContext •Future •Thread intereference •Memory Consistency error •Dead lock •Live lock •Starvation •InterruptedException •Scheduling ๨Ε͍͍ͯ Ұ෦ͷ௿Ϩϕϧͳॲཧ͔Β͸ ղ์͞ΕΔ

Slide 18

Slide 18 text

•Thread •join •Runnable/Callable •Lock •synchronized •Atomic variables •volatile •ThreadLocal •DynamicVariable •ConcurrentHashMap •ConcurrentLinkedQueue •ForkJoin •ThreadPool •Executor/ExecutorService •ExecutionContext •Future •Thread intereference •Memory Consistency error •Dead lock •Live lock •Starvation •InterruptedException •Scheduling "LLB͕ड͚࣋ͬͯ͘ΕΔ Ұ෦ͷ௿Ϩϕϧͳॲཧ͔Β͸ ղ์͞ΕΔ

Slide 19

Slide 19 text

͋͑ͯ

Slide 20

Slide 20 text

•Thread •join •Runnable/Callable •Lock •synchronized •Atomic variables •volatile •ThreadLocal •DynamicVariable •ConcurrentHashMap •ConcurrentLinkedQueue •ForkJoin •ThreadPool •Executor/ExecutorService •ExecutionContext •Future •Thread intereference •Memory Consistency error •Dead lock •Live lock •Starvation •InterruptedException •Scheduling ͦͷ෦෼Λݟ͍͖ͯ·͠ΐ͏ "LLB͕ड͚࣋ͬͯ͘ΕΔ

Slide 21

Slide 21 text

Կ͔஌ݟ͕͋Δ͔΋͠Εͳ͍

Slide 22

Slide 22 text

ͱ͍͏ͷ͕ࠓ೔ͷτʔΫ

Slide 23

Slide 23 text

ͭ·Γ

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

! ϝιουΛݺͿͱ ͭ·Γ Կ͕ى͜Δͷ͔

Slide 26

Slide 26 text

·ͣ͸TFOEFSଆͷ࿩

Slide 27

Slide 27 text

No content

Slide 28

Slide 28 text

ͳΔ΄ͲΘ͔ΒΜ

Slide 29

Slide 29 text

ҰาͣͭίʔυΛ௥͍͖ͬͯ·͢

Slide 30

Slide 30 text

class LocalActorRef override def !(message: Any) (implicit sender: ActorRef = Actor.noSender): Unit = actorCell.sendMessage(message, sender)

Slide 31

Slide 31 text

class LocalActorRef override def !(message: Any) (implicit sender: ActorRef = Actor.noSender): Unit = actorCell.sendMessage(message, sender)

Slide 32

Slide 32 text

trait Dispatch def sendMessage(msg: Envelope): Unit = try { val msgToDispatch = if (system.settings.SerializeAllMessages) serializeAndDeserialize(msg) else msg dispatcher.dispatch(this, msgToDispatch) } catch handleException

Slide 33

Slide 33 text

trait Dispatch def sendMessage(msg: Envelope): Unit = try { val msgToDispatch = if (system.settings.SerializeAllMessages) serializeAndDeserialize(msg) else msg dispatcher.dispatch(this, msgToDispatch) } catch handleException

Slide 34

Slide 34 text

Dispatcher

Slide 35

Slide 35 text

EJTQBUDIʤॻྨɾՙ෺ͳͲΛʥૹΔɺ ૹΓग़͢ɺൃૹ͢Δɺٸૹ͢Δ https://eow.alc.co.jp/search?q=dispatch εϖʔεΞϧΫ

Slide 36

Slide 36 text

EJTQBUDIFS㲈TFOEFS https://eow.alc.co.jp/search?q=dispatch εϖʔεΞϧΫ

Slide 37

Slide 37 text

ͪΐͬͱ͕͍ͪ·͢

Slide 38

Slide 38 text

TFOEFS ૹ৴ݩ ͱ͍͏ΑΓ͸ %JTQBUDIFSΛ௨ա

Slide 39

Slide 39 text

Dispatcher

Slide 40

Slide 40 text

΋͏Ұͭେࣄͳಛ௃

Slide 41

Slide 41 text

Dispatcher

Slide 42

Slide 42 text

trait Dispatch def sendMessage(msg: Envelope): Unit = try { val msgToDispatch = if (system.settings.SerializeAllMessages) serializeAndDeserialize(msg) else msg dispatcher.dispatch(this, msgToDispatch) } catch handleException

Slide 43

Slide 43 text

class Dispatcher def dispatch( receiver: ActorCell, invocation: Envelope ): Unit = { val mbox = receiver.mailbox mbox.enqueue(receiver.self, invocation) registerForExecution(mbox, true, false) }

Slide 44

Slide 44 text

class Dispatcher def dispatch( receiver: ActorCell, invocation: Envelope ): Unit = { val mbox = receiver.mailbox mbox.enqueue(receiver.self, invocation) registerForExecution(mbox, true, false) }

Slide 45

Slide 45 text

abstract class Mailbox def enqueue( receiver: ActorRef, msg : Envelope ): Unit = messageQueue.enqueue(receiver, msg)

Slide 46

Slide 46 text

abstract class Mailbox def enqueue( receiver: ActorRef, msg : Envelope ): Unit = messageQueue.enqueue(receiver, msg)

Slide 47

Slide 47 text

MessageQueue object UnboundedMailbox { class MessageQueue extends ConcurrentLinkedQueue[Envelope] with UnboundedQueueBasedMessageQueue { final def queue: Queue[Envelope] = this } }

Slide 48

Slide 48 text

MessageQueue object UnboundedMailbox { class MessageQueue extends ConcurrentLinkedQueue[Envelope] with UnboundedQueueBasedMessageQueue { final def queue: Queue[Envelope] = this } }

Slide 49

Slide 49 text

Class ConcurrentLinkedQueue

Slide 50

Slide 50 text

Class ConcurrentLinkedQueue

Slide 51

Slide 51 text

http://www.cs.rochester.edu/~scott/papers/ 1996_PODC_queues.pdf

Slide 52

Slide 52 text

http://www.cs.rochester.edu/~scott/papers/ 1996_PODC_queues.pdf

Slide 53

Slide 53 text

/PO#MPDLJOH

Slide 54

Slide 54 text

No content

Slide 55

Slide 55 text

No content

Slide 56

Slide 56 text

No content

Slide 57

Slide 57 text

-PPQ$"4 $PNQBSFBOE4XBQ

Slide 58

Slide 58 text

$"4DPNQBSFBOETXBQ BUPNJDͳૢ࡞

Slide 59

Slide 59 text

No content

Slide 60

Slide 60 text

ૢ࡞͕੒ޭ͢Δ·Ͱ$"4ϦτϥΠ

Slide 61

Slide 61 text

ͳΔ΄Ͳ /PO#MPDLJOH

Slide 62

Slide 62 text

No content

Slide 63

Slide 63 text

sender/receiver͔Β concurrentʹΞΫηε

Slide 64

Slide 64 text

ଟ਺ͷsender͔Β concurrentʹΞΫηε

Slide 65

Slide 65 text

https://doc.akka.io/docs/akka/2.5/mailboxes.html SingleConsumerOnlyUnboundedMailbox This queue may or may not be faster than the default one depending on your use-case— be sure to benchmark properly! NonBlockingBoundedMailbox Backed by a very efficient Multiple-Producer Single-Consumer queue …etc ଞʹ΋MessageQueue৭ʑ͋Γ·͢

Slide 66

Slide 66 text

MessageQueue ʹೖΕͨ͋ͱͷ࿩

Slide 67

Slide 67 text

class Dispatcher def dispatch( receiver: ActorCell, invocation: Envelope ): Unit = { val mbox = receiver.mailbox mbox.enqueue(receiver.self, invocation) registerForExecution(mbox, true, false) }

Slide 68

Slide 68 text

class Dispatcher def dispatch( receiver: ActorCell, invocation: Envelope ): Unit = { val mbox = receiver.mailbox mbox.enqueue(receiver.self, invocation) registerForExecution(mbox, true, false) }

Slide 69

Slide 69 text

class Dispatcher override def registerForExecution( mbox: Mailbox, hasMessageHint: Boolean, hasSystemMessageHint: Boolean ): Boolean = { if (mbox.canBeScheduledForExecution( hasMessageHint, hasSystemMessageHint)) { if (mbox.setAsScheduled()) { … executorService execute mbox … … }

Slide 70

Slide 70 text

class Dispatcher override def registerForExecution( mbox: Mailbox, hasMessageHint: Boolean, hasSystemMessageHint: Boolean ): Boolean = { if (mbox.canBeScheduledForExecution( hasMessageHint, hasSystemMessageHint)) { if (mbox.setAsScheduled()) { … executorService execute mbox … … }

Slide 71

Slide 71 text

͜͜Ͱ;Γ͔͑Γ

Slide 72

Slide 72 text

Dispatcher

Slide 73

Slide 73 text

ExecutorService

Slide 74

Slide 74 text

Java͔Β ThreadΑΓந৅౓ͷߴ͍ඇಉظ༻JOUFSGBDF ExecutorService

Slide 75

Slide 75 text

&YFDVUPS4FSWJDF

Slide 76

Slide 76 text

class Dispatcher override def registerForExecution( mbox: Mailbox, hasMessageHint: Boolean, hasSystemMessageHint: Boolean ): Boolean = { if (mbox.canBeScheduledForExecution( hasMessageHint, hasSystemMessageHint)) { if (mbox.setAsScheduled()) { … executorService execute mbox … … }

Slide 77

Slide 77 text

abstract class Mailbox abstract class Mailbox( val messageQueue: MessageQueue ) extends ForkJoinTask[Unit] with SystemMessageQueue with Runnable { ... }

Slide 78

Slide 78 text

No content

Slide 79

Slide 79 text

͜͜·Ͱ͸senderଆ

Slide 80

Slide 80 text

͔͜͜Β͸receiverଆ

Slide 81

Slide 81 text

class Dispatcher override def registerForExecution( mbox: Mailbox, hasMessageHint: Boolean, hasSystemMessageHint: Boolean ): Boolean = { if (mbox.canBeScheduledForExecution( hasMessageHint, hasSystemMessageHint)) { if (mbox.setAsScheduled()) { … executorService execute mbox … … }

Slide 82

Slide 82 text

abstract class Mailbox abstract class Mailbox( val messageQueue: MessageQueue ) extends ForkJoinTask[Unit] with SystemMessageQueue with Runnable { ... }

Slide 83

Slide 83 text

ForkJoinTask

Slide 84

Slide 84 text

ForkJoinTask join() join() fork() fork()

Slide 85

Slide 85 text

ForkJoinTask join() join() fork() fork() Α͘ݟΔਤ

Slide 86

Slide 86 text

ForkJoinTask join() join() fork() fork()

Slide 87

Slide 87 text

Fork΋Join΋͠ͳ͍ ForkJoinTask

Slide 88

Slide 88 text

Fork΋Join΋͠ͳ͍ͳΒ ͳΜͰForkJoinTaskʁ

Slide 89

Slide 89 text

౴͑ύϑΥʔϚϯε

Slide 90

Slide 90 text

Class ForkJoinTask

Slide 91

Slide 91 text

Class ForkJoinTask

Slide 92

Slide 92 text

Class ForkJoinPool

Slide 93

Slide 93 text

Class ForkJoinPool

Slide 94

Slide 94 text

AkkaͷچΦϑΟγϟϧϒϩά LET IT CRASH blog

Slide 95

Slide 95 text

Scalability of Fork Join Pool LET IT CRASH blog 2012

Slide 96

Slide 96 text

LET IT CRASH blog 2012

Slide 97

Slide 97 text

ʮAkkaͷϕϯνϚʔΫऔΖ͏ͥʯ LET IT CRASH blog 2012

Slide 98

Slide 98 text

ʮ΋͏Ϛγϯങͬͨʯ LET IT CRASH blog 2012

Slide 99

Slide 99 text

LET IT CRASH blog 2012

Slide 100

Slide 100 text

LET IT CRASH blog 2012 ʮͰ͔ͬʂ$16ίΞ͍ͭ͋͘Δͷʁʯ

Slide 101

Slide 101 text

ʮʯ LET IT CRASH blog 2012

Slide 102

Slide 102 text

ʮ",#͔ΑXXXʯ LET IT CRASH blog 2012

Slide 103

Slide 103 text

ʮϕϯν૸ΒͤΔΑʔʂʂʯ LET IT CRASH blog 2012 ·Ͱݟࣄʹεέʔϧ͢ΔͰ͠ΐ

Slide 104

Slide 104 text

LET IT CRASH blog 2012

Slide 105

Slide 105 text

LET IT CRASH blog 2012 ʂʁʁʁ

Slide 106

Slide 106 text

LET IT CRASH blog 2012 ·Ͱεέʔϧ͠ͳ͍ͷʂʁʁ

Slide 107

Slide 107 text

LET IT CRASH blog 2012

Slide 108

Slide 108 text

LET IT CRASH blog 2012

Slide 109

Slide 109 text

LET IT CRASH blog 2012

Slide 110

Slide 110 text

LET IT CRASH blog 2012 7JLUPSݱ%FQVUZ ෭ʁ $50BU-JHIUCFOE

Slide 111

Slide 111 text

LET IT CRASH blog 2012

Slide 112

Slide 112 text

LET IT CRASH blog 2012 %PVH-FB

Slide 113

Slide 113 text

https://en.wikipedia.org/wiki/Doug_Lea

Slide 114

Slide 114 text

https://en.wikipedia.org/wiki/Doug_Lea

Slide 115

Slide 115 text

+BWBͷGPSLKPJO ࡞ͬͨਓ

Slide 116

Slide 116 text

LET IT CRASH blog 2012 … resulted in improved implementation of fork join pool … randomized queing and stealing

Slide 117

Slide 117 text

LET IT CRASH blog 2012

Slide 118

Slide 118 text

LET IT CRASH blog 2012 ۙ͘·Ͱ ΄ͲΑ͘εέʔϧ

Slide 119

Slide 119 text

͔ͩΒForkJoinPool͕ "LLBDispatcherͷσϑΥϧτ

Slide 120

Slide 120 text

receiverଆͷ ࿩͠ʹ໭Γ·͢

Slide 121

Slide 121 text

abstract class Mailbox abstract class Mailbox( val messageQueue: MessageQueue ) extends ForkJoinTask[Unit] with SystemMessageQueue with Runnable { ... } ExecutorServiceͷ executeϝιουΛ࢖͏ͱ Runnableͷrunϝιου͕ݺ͹ΕΔ

Slide 122

Slide 122 text

abstract class Mailbox override final def run(): Unit = { try { if (!isClosed) { //Volatile read, needed here processAllSystemMessages() //First, deal with any system messages processMailbox() //Then deal with messages } } finally { setAsIdle() //Volatile write, needed here dispatcher.registerForExecution(this, false, false) } }

Slide 123

Slide 123 text

abstract class Mailbox override final def run(): Unit = { try { if (!isClosed) { //Volatile read, needed here processAllSystemMessages() //First, deal with any system messages processMailbox() //Then deal with messages } } finally { setAsIdle() //Volatile write, needed here dispatcher.registerForExecution(this, false, false) } }

Slide 124

Slide 124 text

abstract class Mailbox @tailrec private final def processMailbox( left: Int = java.lang.Math.max(dispatcher.throughput, 1), deadlineNs: Long = …): Unit = if (shouldProcessMessage) { val next = dequeue() if (next ne null) { … actor invoke next … processMailbox(left - 1, deadlineNs) } }

Slide 125

Slide 125 text

abstract class Mailbox @tailrec private final def processMailbox( left: Int = java.lang.Math.max(dispatcher.throughput, 1), deadlineNs: Long = …): Unit = if (shouldProcessMessage) { val next = dequeue() if (next ne null) { … actor invoke next … processMailbox(left - 1, deadlineNs) } }

Slide 126

Slide 126 text

abstract class Mailbox @tailrec private final def processMailbox( left: Int = java.lang.Math.max(dispatcher.throughput, 1), deadlineNs: Long = …): Unit = if (shouldProcessMessage) { val next = dequeue() if (next ne null) { … actor invoke next … processMailbox(left - 1, deadlineNs) } } //receiveϝιουΛݺͿ class MyActor extends Actor { def receive = { … } }

Slide 127

Slide 127 text

abstract class Mailbox @tailrec private final def processMailbox( left: Int = java.lang.Math.max(dispatcher.throughput, 1), deadlineNs: Long = …): Unit = if (shouldProcessMessage) { val next = dequeue() if (next ne null) { … actor invoke next … processMailbox(left - 1, deadlineNs) } }

Slide 128

Slide 128 text

recursive processMailbox() ͳͥʁʁ

Slide 129

Slide 129 text

ෆཁͳεϨου੾Γସ͑Λ๷͙

Slide 130

Slide 130 text

ྫ ճͷrunͰͭͷϝοηʔδΛϓϩηε

Slide 131

Slide 131 text

No content

Slide 132

Slide 132 text

Ͱ΋

Slide 133

Slide 133 text

MessageQueueʹϝοηʔδ͋ͬͨΒ ಉ͡εϨου͕ԆʑͱprocessMailbox

Slide 134

Slide 134 text

ͦ͏͡Όͳ͍

Slide 135

Slide 135 text

throughput

Slide 136

Slide 136 text

rerence.conf # Throughput defines the number of messages # that are processed in a batch # before the thread is returned to the pool. # Set to 1 for as fair as possible. throughput = 5

Slide 137

Slide 137 text

abstract class Mailbox @tailrec private final def processMailbox( left: Int = java.lang.Math.max(dispatcher.throughput, 1), deadlineNs: Long = …): Unit = if (shouldProcessMessage) { val next = dequeue() if (next ne null) { … actor invoke next … processMailbox(left - 1, deadlineNs) } }

Slide 138

Slide 138 text

σϑΥϧτͰͭ·Ͱ͔͠ ࿈ଓͰϝοηʔδΛॲཧ͠ͳ͍

Slide 139

Slide 139 text

receiverଆ΋ऴΘͬͨ

Slide 140

Slide 140 text

શମͷ͓͞Β͍

Slide 141

Slide 141 text

"DUPSʹΑΔ ϝοηʔδύογϯά "LLB

Slide 142

Slide 142 text

No content

Slide 143

Slide 143 text

௿Ϩϕϧ͔ΒݟΔͱ

Slide 144

Slide 144 text

No content

Slide 145

Slide 145 text

No content

Slide 146

Slide 146 text

No content

Slide 147

Slide 147 text

No content

Slide 148

Slide 148 text

͞Βʹ௿Ϩϕϧ͔ΒݟΔͱ

Slide 149

Slide 149 text

No content

Slide 150

Slide 150 text

+BWBͷ'PSL+PJO5BTLΛ ܁Γฦ͠FYFDVUF͢Δ ΞϓϦέʔγϣϯ "LLB

Slide 151

Slide 151 text

͓͠·͍