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

Akkaによるスレッドの使い方

 Akkaによるスレッドの使い方

リチャード 伊真岡

April 02, 2018
Tweet

Transcript

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

    View Slide

  2. "LLBͱ͍͑͹

    View Slide

  3. https://akka.io/

    View Slide

  4. https://akka.io/

    View Slide

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

    View Slide

  6. •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

    View Slide

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

    View Slide

  8. ແཧήʔ

    View Slide

  9. ͕ॿ͚ͯ͘Ε·͢

    View Slide

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

    View Slide

  11. 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) {
    //࣍ͷϝοηʔδ͔ΒׂΓࠐ·Εͳ͍
    }
    }

    View Slide

  12. 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) {
    //࣍ͷϝοηʔδ͔ΒׂΓࠐ·Εͳ͍
    }
    }

    View Slide

  13. 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) {
    //࣍ͷϝοηʔδ͔ΒׂΓࠐ·Εͳ͍
    }
    }

    View Slide

  14. 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) {
    //࣍ͷϝοηʔδ͔ΒׂΓࠐ·Εͳ͍
    }
    }

    View Slide

  15. •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
    ๨Ε͍͍ͯ

    View Slide

  16. •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ಛ༗ͷ໰୊͸Ͳ͏ͯ͠΋
    ى͜Γ·͢

    View Slide

  17. •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
    ๨Ε͍͍ͯ
    Ұ෦ͷ௿Ϩϕϧͳॲཧ͔Β͸
    ղ์͞ΕΔ

    View Slide

  18. •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͕ड͚࣋ͬͯ͘ΕΔ
    Ұ෦ͷ௿Ϩϕϧͳॲཧ͔Β͸
    ղ์͞ΕΔ

    View Slide

  19. ͋͑ͯ

    View Slide

  20. •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͕ड͚࣋ͬͯ͘ΕΔ

    View Slide

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

    View Slide

  22. ͱ͍͏ͷ͕ࠓ೔ͷτʔΫ

    View Slide

  23. ͭ·Γ

    View Slide

  24. View Slide

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

    View Slide

  26. ·ͣ͸TFOEFSଆͷ࿩

    View Slide

  27. View Slide

  28. ͳΔ΄ͲΘ͔ΒΜ

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  34. Dispatcher

    View Slide

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

    View Slide

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

    View Slide

  37. ͪΐͬͱ͕͍ͪ·͢

    View Slide

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

    View Slide

  39. Dispatcher

    View Slide

  40. ΋͏Ұͭେࣄͳಛ௃

    View Slide

  41. Dispatcher

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

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

    View Slide

  49. Class ConcurrentLinkedQueue

    View Slide

  50. Class ConcurrentLinkedQueue

    View Slide

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

    View Slide

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

    View Slide

  53. /PO#MPDLJOH

    View Slide

  54. View Slide

  55. View Slide

  56. View Slide

  57. -PPQ$"4 $PNQBSFBOE4XBQ

    View Slide

  58. $"4DPNQBSFBOETXBQ
    BUPNJDͳૢ࡞

    View Slide

  59. View Slide

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

    View Slide

  61. ͳΔ΄Ͳ
    /PO#MPDLJOH

    View Slide

  62. View Slide

  63. sender/receiver͔Β
    concurrentʹΞΫηε

    View Slide

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

    View Slide

  65. 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৭ʑ͋Γ·͢

    View Slide

  66. MessageQueue
    ʹೖΕͨ͋ͱͷ࿩

    View Slide

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

    View Slide

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

    View Slide

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

    executorService execute mbox


    }

    View Slide

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

    executorService execute mbox


    }

    View Slide

  71. ͜͜Ͱ;Γ͔͑Γ

    View Slide

  72. Dispatcher

    View Slide

  73. ExecutorService

    View Slide

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

    View Slide

  75. &YFDVUPS4FSWJDF

    View Slide

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

    executorService execute mbox


    }

    View Slide

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

    View Slide

  78. View Slide

  79. ͜͜·Ͱ͸senderଆ

    View Slide

  80. ͔͜͜Β͸receiverଆ

    View Slide

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

    executorService execute mbox


    }

    View Slide

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

    View Slide

  83. ForkJoinTask

    View Slide

  84. ForkJoinTask
    join()
    join()
    fork()
    fork()

    View Slide

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

    View Slide

  86. ForkJoinTask
    join()
    join()
    fork()
    fork()

    View Slide

  87. Fork΋Join΋͠ͳ͍
    ForkJoinTask

    View Slide

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

    View Slide

  89. ౴͑ύϑΥʔϚϯε

    View Slide

  90. Class ForkJoinTask

    View Slide

  91. Class ForkJoinTask

    View Slide

  92. Class ForkJoinPool

    View Slide

  93. Class ForkJoinPool

    View Slide

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

    View Slide

  95. Scalability of Fork Join Pool
    LET IT CRASH blog 2012

    View Slide

  96. LET IT CRASH blog 2012

    View Slide

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

    View Slide

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

    View Slide

  99. LET IT CRASH blog 2012

    View Slide

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

    View Slide

  101. ʮʯ
    LET IT CRASH blog 2012

    View Slide

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

    View Slide

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

    View Slide

  104. LET IT CRASH blog 2012

    View Slide

  105. LET IT CRASH blog 2012
    ʂʁʁʁ

    View Slide

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

    View Slide

  107. LET IT CRASH blog 2012

    View Slide

  108. LET IT CRASH blog 2012

    View Slide

  109. LET IT CRASH blog 2012

    View Slide

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

    View Slide

  111. LET IT CRASH blog 2012

    View Slide

  112. LET IT CRASH blog 2012
    %PVH-FB

    View Slide

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

    View Slide

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

    View Slide

  115. +BWBͷGPSLKPJO
    ࡞ͬͨਓ

    View Slide

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

    View Slide

  117. LET IT CRASH blog 2012

    View Slide

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

    View Slide

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

    View Slide

  120. receiverଆͷ
    ࿩͠ʹ໭Γ·͢

    View Slide

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

    View Slide

  122. 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)
    }
    }

    View Slide

  123. 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)
    }
    }

    View Slide

  124. 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)
    }
    }

    View Slide

  125. 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)
    }
    }

    View Slide

  126. 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 = {

    }
    }

    View Slide

  127. 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)
    }
    }

    View Slide

  128. recursive
    processMailbox()
    ͳͥʁʁ

    View Slide

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

    View Slide


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

    View Slide

  131. View Slide

  132. Ͱ΋

    View Slide

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

    View Slide

  134. ͦ͏͡Όͳ͍

    View Slide

  135. throughput

    View Slide

  136. 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

    View Slide

  137. 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)
    }
    }

    View Slide

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

    View Slide

  139. receiverଆ΋ऴΘͬͨ

    View Slide

  140. શମͷ͓͞Β͍

    View Slide

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

    View Slide

  142. View Slide

  143. ௿Ϩϕϧ͔ΒݟΔͱ

    View Slide

  144. View Slide

  145. View Slide

  146. View Slide

  147. View Slide

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

    View Slide

  149. View Slide

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

    View Slide

  151. ͓͠·͍

    View Slide