Akka Streamsで
Actorが作成されるのを確認

Akka Streamsで
Actorが作成されるのを確認

LT駆動開発29の発表資料です。
https://github.com/LTDD/Sessions/wiki/LT%E9%A7%86%E5%8B%95%E9%96%8B%E7%99%BA29

Akka Streamsでどんな風にActorが生成されていくのか気になったので、ActorSystemImpl#printTreeで出力して遊んだ話です。

http://qiita.com/eielh/items/fc8dd83989660460061a

1a679952cdf455ecd6a15cbde7ae80d5?s=128

Tomohiko Himura

September 04, 2016
Tweet

Transcript

  1. 14.

    アクターの定義 class HelloActor extends akka.actor.Actor { override def receive: Receive

    = { // receiveϝιουΛ࣮૷͢Δඞཁ͕͋Δ case _ => // Կ͔ϝοηʔδ͕͖ͨΒ println("Hello, World"); } }
  2. 16.

    アクターへのメッセージ送信 • ActorRef ! メッセージ // Ϋϥε͔ΒPropsΛੜ੒ val HelloActorProps =

    akka.actor.Props[HelloActor] // ΞΫλʔγεςϜΛߏங val actorSystem = akka.actor.ActorSystem() // Ϣʔβ༻ͷ਌ΞΫλʔʹPropsΛݩʹΞΫλʔΛੜ੒ɻੜ੒ͨ͠ΞΫλʔͷࢀরΛฦ͢ val helloActorRef = actorSystem.actorOf(HelloActorProps) // ΞΫλʔʹ "world"ͱ͍͏ϝοηʔδΛૹ৴ helloActorRef ! "World" // HelloActorͷΠϯελϯεͰ͸ͳ͍ͷͰɺ receiveϝιου͸௚઀Α΂ͳ͍
  3. 18.

    出力してみる // package akka val actorSystem = akka.actor.ActorSystem() // printeNodeΛ͔ͭ͏ͨΊʹແཧ໼ཧActorSystemImplʹ

    val as = actorSystem.asInstanceOf[ActorSystemImpl] println(as.printTree); // ΞΫλʔγεςϜΛ࡞ͬͨ࣌఺ val helloActorRef = actorSystem.actorOf(HelloActorProps) println(as.printTree); // HelloActorΛҰͭ࡞੒ helloActorRef ! "World"
  4. 19.

    アクターシステムを作った直後 -> / LocalActorRefProvider$$anon$1 class akka.actor.LocalActorRefProvider$Guardian s ⌊-> system LocalActorRef

    class akka.actor.LocalActorRefProvider$SystemGuardian st | ⌊-> deadLetterListener RepointableActorRef class akka.event.DeadLetterListene | ⌊-> eventStreamUnsubscriber-1 RepointableActorRef class akka.event.EventStrea | ⌊-> log1-Logging$DefaultLogger RepointableActorRef class akka.event.Logging$D ⌊-> user LocalActorRef class akka.actor.LocalActorRefProvider$Guardian status=0 n
  5. 20.

    HelloActorを作った直後 -> / LocalActorRefProvider$$anon$1 class akka.actor.LocalActorRefProvider$Guardian s ⌊-> system LocalActorRef

    class akka.actor.LocalActorRefProvider$SystemGuardian st | ⌊-> deadLetterListener RepointableActorRef class akka.event.DeadLetterListene | ⌊-> eventStreamUnsubscriber-1 RepointableActorRef class akka.event.EventStrea | ⌊-> log1-Logging$DefaultLogger RepointableActorRef class akka.event.Logging$D ⌊-> user LocalActorRef class akka.actor.LocalActorRefProvider$Guardian status=0 1 ⌊-> $a RepointableActorRef class akka.HelloActor status=0 no children
  6. 22.

    class HelloActor extends akka.actor.Actor { var child: akka.actor.ActorRef = _

    override def receive: Receive = { case str: String => // HelloActorͷதͰΞΫλʔΛੜ੒ child = context.actorOf(HelloActor.props) println(s"Hello, $str") } } object HelloActor { val props = akka.actor.Props[HelloActor] } object Sample extends App { val actorSystem = akka.actor.ActorSystem() val as = actorSystem.asInstanceOf[ActorSystemImpl] val helloActorRef = actorSystem.actorOf(HelloActor.props) println(as.printTree) helloActorRef ! “World” println(as.printTree) }
  7. 23.

    子が増えてる -> / LocalActorRefProvider$$anon$1 class akka.actor.LocalActorRefProvider$Guardian s ⌊-> system LocalActorRef

    class akka.actor.LocalActorRefProvider$SystemGuardian st | ⌊-> deadLetterListener RepointableActorRef class akka.event.DeadLetterListene | ⌊-> eventStreamUnsubscriber-1 RepointableActorRef class akka.event.EventStrea | ⌊-> log1-Logging$DefaultLogger RepointableActorRef class akka.event.Logging$D ⌊-> user LocalActorRef class akka.actor.LocalActorRefProvider$Guardian status=0 1 ⌊-> $a RepointableActorRef class akka.HelloActor status=0 1 children ⌊-> $a LocalActorRef class akka.HelloActor status=0 no children
  8. 25.

    val actorSystem = akka.actor.ActorSystem() val as = actorSystem.asInstanceOf[ActorSystemImpl] println(as.printTree) //

    StreamSupervisorΞΫλʔ͕ੜ੒͞ΕΔ val materializer = akka.stream.ActorMaterializer()(actorSystem) println(as.printTree) // ·ͩͳʹ΋Ͱ͖ͳ͍ val graph = Source.fromIterator(() => Iterator.from(0)).to(Sink.ignore()) println(as.printTree) // ετϦʔϜΛಈ͔ͨ͢ΊʹΞΫλʔ͕Ͱ͖Δ graph.run()(materializer) println(as.printTree)
  9. 26.

    val actorSystem = akka.actor.ActorSystem() val as = actorSystem.asInstanceOf[ActorSystemImpl] println(as.printTree) //

    StreamSupervisorΞΫλʔ͕ੜ੒͞ΕΔ val materializer = akka.stream.ActorMaterializer()(actorSystem) println(as.printTree) // ·ͩͳʹ΋Ͱ͖ͳ͍ val graph = Source.fromIterator(() => Iterator.from(0)).to(Sink.ignore()) println(as.printTree) // ετϦʔϜΛಈ͔ͨ͢ΊʹΞΫλʔ͕Ͱ͖Δ graph.run()(materializer) println(as.printTree) run されるまではmaterializerが必要ない materializerはActorSystemをもってる materializerはアクターをつくれる
  10. 29.

    mapで処理を加えてみる val graph = Source.fromIterator(() => Iterator.from(0)) .map(_ * 2)

    .to(Sink.ignore()) ⌊VTFS-PDBM"DUPS3FGDMBTTBLLBBDUPS-PDBM"DUPS3FG1SPWJEFS(VBSEJBOTUBUVTDIJMESFO ⌊4USFBN4VQFSWJTPS3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM4USFBN4VQFSWJTPST ⌊qPXVOLOPXOPQFSBUJPO3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQMGVTJOH"D
  11. 31.

    Sink.actorRefを利用 val helloActor = actorSystem.actorOf(HelloActor.props) val graph = Source.fromIterator(() =>

    Iterator.from(0)) .map(_.toString) .to(Sink.actorRef[String](helloActor, ())) ⌊VTFS-PDBM"DUPS3FGDMBTTBLLBBDUPS-PDBM"DUPS3FG1SPWJEFS(VBSEJBOTUBUVTDIJMESFO ⌊B3FQPJOUBCMF"DUPS3FGDMBTTBLLB)FMMP"DUPSTUBUVTOPDIJMESFO ⌊4USFBN4VQFSWJTPS3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM4USFBN4VQFSWJTPSTUBUVT ⌊qPXVOLOPXOPQFSBUJPO3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQMGVTJOH"DUPS(S ⌊qPXBDUPS3FG4JOL3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM"DUPS3FG4JOL"DUPSTU
  12. 33.

    Source.actorRefをつかってみる val helloActor = actorSystem.actorOf(HelloActor.props) val graph = Source.actorRef(1, OverflowStrategy.dropTail)

    .to(Sink.actorRef[String](helloActor, ())) ⌊VTFS-PDBM"DUPS3FGDMBTTBLLBBDUPS-PDBM"DUPS3FG1SPWJEFS(VBSEJBOTUBUVTDIJMESFO ⌊B3FQPJOUBCMF"DUPS3FGDMBTTBLLB)FMMP"DUPSTUBUVTOPDIJMESFO ⌊4USFBN4VQFSWJTPS3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM4USFBN4VQFSWJTPSTUBUVT ⌊qPXBDUPS3FG4PVSDF3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM"DUPS3FG4PVSDF"D ⌊qPXBDUPS3FG4JOL3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM"DUPS3FG4JOL"DUPSTU
  13. 35.

    全部やってみる val graph = Source.actorRef[Int](1, OverflowStrategy.dropTail) .map(_*2) .map(_.toString) .to(Sink.actorRef[String](helloActor, ()))

    ⌊VTFS-PDBM"DUPS3FGDMBTTBLLBBDUPS-PDBM"DUPS3FG1SPWJEFS(VBSEJBOTUBUVTDIJMESFO ⌊B3FQPJOUBCMF"DUPS3FGDMBTTBLLB)FMMP"DUPSTUBUVTOPDIJMESFO ⌊4USFBN4VQFSWJTPS3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM4USFBN4VQFSWJTPSTUBUVT ⌊qPXVOLOPXOPQFSBUJPO3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQMGVTJOH"DUPS(SBQI ⌊qPXBDUPS3FG4PVSDF3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM"DUPS3FG4PVSDF"DUP ⌊qPXBDUPS3FG4JOL3FQPJOUBCMF"DUPS3FGDMBTTBLLBTUSFBNJNQM"DUPS3FG4JOL"DUPSTUBUV
  14. 37.