怒涛のAkka: Dispatchers
Actorを動かしているのがMessageDispatcher.
すべてのMessageDispatcherのインプリがExecutorContext.
actor systemの作成時にExecutorContextを渡すとそれがシステム全体のDispatcher
指定がなければ,defaultはfork-join-executor.
Dispatcherは,mailbox, threadpoolの紐付けとメッセージハンドリングのロジックを行うイメージでいいと思う.
Dispatcherの指定方法
2通りある
共通部分
Confiugrationにてdispatcherを指定
my-dispatcher { # Dispatcher is the name of the event-based dispatcher type = Dispatcher # What kind of ExecutionService to use executor = "fork-join-executor" # Configuration for the fork join pool fork-join-executor { # Min number of threads to cap factor-based parallelism number to parallelism-min = 2 # Parallelism (threads) ... ceil(available processors * factor) parallelism-factor = 2.0 # Max number of threads to cap factor-based parallelism number to parallelism-max = 10 } # Throughput defines the maximum number of messages to be # processed per actor before the thread jumps to the next actor. # Set to 1 for as fair as possible. throughput = 100 }
Configに指定してハードコードで読む場合(1通り目)
- Config
akka.actor.deployment { /myactor { dispatcher = my-dispatcher } }
user gardianの下のmyactorにdispatcherが適用されると思う.
- Code
import akka.actor.Props val myActor = context.actorOf(Props[MyActor], "myactor")
Configに指定しないでハードコードで完結(2通り目)
import akka.actor.Props val myActor = context.actorOf(Props[MyActor].withDispatcher("my-dispatcher"), "myactor1")
Dispatcherタイプ
scalaのtypeではない.
4通りのタイプがある.
- Dispatcher
- PinnedDispatcher
- BalancingDispatcher
- CallingThreadDispatcher
Dispatcher
- default dispatcher.
- actorの集合をthread poolに対応付ける.
- シェアが無制限 (多分dispatcherのシェア)
- use case: default, bulkheading
- 1 actor: 1 mailbox
- java.util.concurrent.ExecutorServiceで動かされる
PinnedDispatcher
- 1 actor: 1 thread pool (1 thread pool = 1 thread)
- 1 actor: 1 mailbox
- シェアなし
- akka.dispatch.ThreadPoolExecutorConfiguratorで動かされる
- use case: bulkheading
BalancingDispatcher
- 忙しいactorからさぽっているactorに仕事を振る
- 1 mailboxで,すべてのactorでシェアされている
- 同じdispatcherのactorでシェア可能
- 1 mailbox
- use case: work sharing
- java.util.concurrent.ExecutorServiceで動かされる
- routerとしては使えないがrouteeとしては使える
CallingThreadDispatcher
- カレントスレッドで呼び出される
- 新しいスレッドは作らない
- 別スレッドからは使える
- シェアが無制限
- 1 mailbox / (1 actor, 1 thread)
- use case: test
- 呼び出しスレッドで動かされる
1actor = 1therad, 1mailboxと思っていたが違うようだ.それはPinnedDispatcherの場合で,基本は1 actor = 1 threadpool + 1 mailboxのよう.