Share
Embed
Project Loom 概要と Scala における有用性 @ ScalaMatsuri2024
ScalaMatsuri2024 の登壇資料です。
th0rikosh1
June 08, 2024
Transcript
37 slides
-
© 2020 Funds, Inc. © 2020 Funds, Inc. Overview Project
Loom Thinking Usefulness in Scala Funds, Inc. Takato Horikoshi Project Loom 概要と Scala における有⽤性 -
© 2020 Funds, Inc. Whoami Hello, I'm Takato Horikoshi. I'm
a Software Developer / Engineering Manager at Funds. -
© 2020 Funds, Inc. n Tested and verified on a
MacBook Pro with an Apple M1 Pro chip. n Scala 3.3.1 n JDK 21 n https://github.com/takat0-h0rikosh1/scala-matsuri-2024 Note Information on the codes listed on this slide. スライドに登場するコードに関する事前情報です。 -
© 2020 Funds, Inc. n Project in OpenJDK n Scalable
Concurrency for the Java Platform Overview Java でスケーラブルな並⾏処理の実現を⽬標とするプロジェクトです。 -
© 2020 Funds, Inc. n Virtual Threads n Structured Concurrency
n etc... Loomʼs features 本⽇は Loom の機能のうち Virtual Threads と Structured Concurrency につ いてお話します。 Todayʼs topics -
© 2020 Funds, Inc. n Lightweight threads(called Fiber) n Create
and manage threads within the JVM n Reduce overhead with creating and managing threads n High throughput. Virtual Threads Virtual Threads は JVM 上で作成・管理される軽量スレッドです。 Fiber と呼ばれていたりします。 -
© 2020 Funds, Inc. How Virtual Threads Work 1 Platform
Threads Virtual Threads Java Virtual Machine Operating System OS Thread OS Thread OS Thread OS Thread OS Thread OS Thread Platform Thread Platform Thread Platform Thread Carrier Thread Carrier Thread Carrier Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread Virtual Thread スレッドの⽣成がOSに依存しないため安価に⼤量に作成できるのが特徴です。 -
© 2020 Funds, Inc. How Virtual Threads Work 2 Platform
Threads Virtual Threads ブロックが発⽣すると即座に別のタスクを開始するためスレッドのリソースを 効率良く活⽤できます。 IO Block IO Block IO Block IO Block IO Block IO Block IO Block IO Block VT VT VT VT VT VT VT VT VT VT VT Platform Threads Platform Threads Platform Threads Carrier Threads Carrier Threads Carrier Threads -
© 2020 Funds, Inc. Comparison with Platform Threads Characteristic Platform
Threads Virtual Threads Threading style Thread-per platform Thread-per-request style Creation & Context Switching High Cost Low Cost Scalability Millions of threads not practical A Millions of threads possible Thread Pool Management Required Do not pool Compute Large Data Fair Poor スレッドをプールせずに100万スレッド⼀気にスケールすることが可能です。 -
© 2020 Funds, Inc. n high-throughput concurrent I/O-heavy tasks. n
ex). pub/sub, large request count Use case Not well-suited for computationally expensive or large- data processing tasks. Ref: https://openjdk.org/jeps/444 I/Oブロックが発⽣しつつも⾼いスループットが要求されるシーンに最適です。 ⼀⽅で⼤規模なデータの計算処理にはあまり向きません。 -
© 2020 Funds, Inc. JDK COMPATIBILITY JDK 21 以上と互換性のある Scala
Version を利⽤します。 -
© 2020 Funds, Inc. Future on Virtual Threads Future を
Virtual Threads で動かすことができます。 -
© 2020 Funds, Inc. Millions of threads can run おおよそ⼗数秒のうちに100万スレッドの⽣成が完了します。
Console Time: 13986ms Count: 1000000 -
© 2020 Funds, Inc. Try to run 1 million threads
in Platform Threads... 同じプログラムを Platform Threads で動かそうとするとどうなるでしょう か...。 -
© 2020 Funds, Inc. Try to run 1 million threads
in Platform Threads... Platform Threads で100万スレッドの⽣成は現実的ではありません。 -
© 2020 Funds, Inc. n Preserve the natural relationship between
tasks and subtasks n Control task lifetime n maintainable, reliable, and observable Structured Concurrency タスク間の関係・⽣存期間を管理する機能です。 メンテナンス性/可⽤性/観測性を向上します。 -
© 2020 Funds, Inc. What is `Structured` mean? Unstructured Structured
sub task task task sub task sub task sub task 構造化することで各タスクの完了時期や結果の⾒通しを⽴てることができます。 -
© 2020 Funds, Inc. Case of `Unstructured` ⾮構造化並⾏処理の例です。即座に例外を投げるタスクと、1秒待機したのち に Goodbye
と標準出⼒するタスクを同時に動かしてみます。 -
© 2020 Funds, Inc. Case of `Unstructured` この例ではそれぞれのタスクは好き勝⼿に結果を返します。 完了のタイミングもバラバラです。 The
process continues regardless of the result of the other execution. -
© 2020 Funds, Inc. Join two tasks as a unit
while running them in their own threads. Case of `Structured` 構造化された並⾏処理の例です。 -
© 2020 Funds, Inc. Case of `Structured` If even one
task fails, the rest are terminated. 複数タスクのユニットを作成します。この例では⼀⽅のタスクが失敗するとも う⼀⽅のタスクはキャンセルされ、唯⼀の結果を返します。 -
© 2020 Funds, Inc. Is the Scala ecosystem introducing or
implementing the equivalent of Loom? Well... さて、Loom の機能の Scala Ecosystem への導⼊進捗は…? -
© 2020 Funds, Inc. ZIO Fiber(v2.1-RC1) ZIO には既にタスクを Virtual Threads
で実⾏する機能が提供されています。 Run task on virtual thread Console Result: 6268ms Count: 1000000 -
© 2020 Funds, Inc. ZIO Fiber(v2.1-RC1) - Fork in Specific
Scope ZIO の Scope を活⽤することでタスクの⽣存期間の ⾒通しが⽴てやすくなります。 Console Still running ... Still running ... Still running ... The innermost scope is about to be closed. Still running ... Still running ... Still running ... Still running ... Still running ... Still running ... The outer scope is about to be closed. -
© 2020 Funds, Inc. Cats Effects ‒ Fiber(v3.5.4) Run on
Fiber Cats Effect は独⾃の軽量スレッドが実装されています。 Runtime に Virtual Threads を指定することも可能です。 Console Time: 4267ms Count: 1000000 -
© 2020 Funds, Inc. Cats Effects - Supervisor(v3.5.4) Supervisor は
ZIO の Scope と同等の機能です。 Console Still running ... Still running ... Still running ... The innermost scope is about to be closed. Still running ... Still running ... Still running ... Still running ... Still running ... Still running ... The outer scope is about to be closed. -
© 2020 Funds, Inc. n Loom-based concurrency API for Scala
n Ox を⽤いた Direct-Style Scala ← 06/09 15:20 Ox SoftwareMill 製の Loom を⼟台に実装したライブラリです。明⽇のセッショ ンを楽しみに待ちましょう。 -
© 2020 Funds, Inc. n What is status now?(Akka, Pekko,
Monix, ...) Etc... その他のライブラリに関しては...。 是⾮⼀緒にお話しましょう(教えて下さい)。 -
© 2020 Funds, Inc. n Loom: Scalable Concurrency for the
Java Platform n Can be used via libraries n Should be useful Summary Loom の機能・相当機能は⼗分に有⽤性があり、わたしたちはそれすぐに利⽤ することができます。 -
© 2020 Funds, Inc. n ZIO は JDK 21 以降の場合に
Project Loom の Virtual Thread を使うようにしてい る? Ø https://github.com/zio/zio/issues/4433 Ø Loom への⾔及? Ø https://zio.dev/overview/performance/ Ø https://github.com/zio/zio/releases/tag/v2.1.0 n Virtual Thread 内での Thread.sleep() はどの thread を sleep している…? Ø => ちゃんと Virtula Thread の thread を sleep してくれてるっぽい n Cats Effect を Virtual Threads に乗せる Ø https://jacum.medium.com/running-cats-effect-on-virtual-threads-of- jdk21-7a68ea97fd65 n ピンされるとスケールしない可能性がある(ファイルI/O, JNIコール) Ø https://davidvlijmincx.com/posts/see-pinned-virtual-threads/ Appendix