Upgrade to Pro
— share decks privately, control downloads, hide ads and more …
Speaker Deck
Features
Speaker Deck
PRO
Sign in
Sign up for free
Search
Search
一体いつから ――― DataLoader が 並列実行されていると錯覚していた?
Search
SMS tech
June 24, 2025
520
0
Share
Embed
Copy iframe code
Copy JS code
Copy link
Start on current slide
一体いつから ――― DataLoader が 並列実行されていると錯覚していた?
SMS tech
June 24, 2025
More Decks by SMS tech
See All by SMS tech
Bits Chat on Development
sms_tech
0
26
オンコールの負荷軽減のためのBits Assistant 活用方法 / How to Use Bits Assistant to Reduce the Workload on On-Call Staff
sms_tech
1
510
AWS DevOps Agent vs SRE俺 / AWS DevOps Agent vs me, the SRE
sms_tech
3
1.3k
カイポケコネクトのフロントエンドコミュニティとGraphQLスタックの更新/advancing-the-graphql-stack-update-in-the-frontend-community
sms_tech
0
180
フロントエンド専任がいないチームのNext.js移行と、その後/Renewal to Next.js for a team without a dedicated front-end developer, and what happened next
sms_tech
0
170
マルチプロダクトのカオスを制す。「プロダクトディシジョンレコード」で実現するチーム横断のアラインメント戦略/Introduction to Product Decision Record
sms_tech
0
3.1k
The Modelの罠を超える! 売上9割 "SLG" のVertical SaaSが挑む PLG × SLG ハイブリッド戦略 #pmconf2025/Defying "The Model": How a Vertical SaaS Integrates PLG into a 90% SLG Motion
sms_tech
0
3.1k
業務の本質理解から始まるリニューアル/ RethinkingBusinessEssenceforRenewal
sms_tech
1
330
プロジェクトを止めない。ライブラリの共存戦略とバージョンアップ/ChakraUI v3 Migration: Keeping Projects Running
sms_tech
0
220
Featured
See All Featured
A brief & incomplete history of UX Design for the World Wide Web: 1989–2019
jct
2
400
How to audit for AI Accessibility on your Front & Back End
davetheseo
0
450
Become a Pro
speakerdeck
PRO
31
6k
How GitHub (no longer) Works
holman
316
150k
AI Search: Implications for SEO and How to Move Forward - #ShenzhenSEOConference
aleyda
1
1.3k
Are puppies a ranking factor?
jonoalderson
1
3.6k
WCS-LA-2024
lcolladotor
0
660
Leveraging Curiosity to Care for An Aging Population
cassininazir
1
270
A Guide to Academic Writing Using Generative AI - A Workshop
ks91
PRO
1
340
Speed Design
sergeychernyshev
33
1.9k
<Decoding/> the Language of Devs - We Love SEO 2024
nikkihalliwell
1
260
Agile Leadership in an Agile Organization
kimpetersen
PRO
0
170
Transcript
0 © SMS Co., Ltd. 2025.06.25 【非公式】 JJUG CCC 2025
Spring 後夜祭 LT 株式会社エス・エム・エス プロダクト推進本部 @hmarui66 一体いつから ――― DataLoader が 並列実行されていると錯覚していた?
1 © SMS Co., Ltd. Spring for GraphQL 使ってます
2 © SMS Co., Ltd. Spring for GraphQL 使ってます アノテーションでスキーマとマッピング
(プラグイン入れたら ✡アイコンで相互にジャンプ可能)
3 © SMS Co., Ltd. Spring for GraphQL 使ってます `@BatchMapping`
は暗黙的に DataLoader を 使用して N+1 問題を回避してくれる(便利) 参考: https://spring.pleiades.io/spring-graphql/reference/controllers.html#controllers.batch-mapping
4 © SMS Co., Ltd. クエリ処理の流れ こんなクエリを実行→
5 © SMS Co., Ltd. クエリ処理の流れ `books` のクエリが呼ばれるので
6 © SMS Co., Ltd. クエリ処理の流れ `@QueryMapping` がつけられた メソッドが呼び出される
7 © SMS Co., Ltd. クエリ処理の流れ `author`, `publisher` も取得対象なので
8 © SMS Co., Ltd. クエリ処理の流れ `@BatchMapping` がつけられた メソッドが呼び出される
9 © SMS Co., Ltd. クエリ処理の流れ `author`, `publisher` は独立したフィールドなので 並列で取得されるはず?
10 © SMS Co., Ltd. クエリ処理の流れ 🙅 デフォルトでは同期的に呼ばれ、 直列実行
11 © SMS Co., Ltd. クエリ処理の流れ (このサンプルでは) `publisher` → `author`
と直列で実行 ※OpenTelemetry の Java Agent を仕込んで可視化
12 © SMS Co., Ltd. 並列実行するには
13 © SMS Co., Ltd. 並列実行するには Java 21+ では、AnnotatedControllerConfigurer が
Executor で構成されている場合、 ブロッキングメソッドシグネチャーを持つコントローラーメソッドが 非同期的に呼び出されます。 ドキュメントによると... 参考: https://spring.pleiades.io/spring-graphql/reference/controllers.html#controllers.schema-mapping.return.values → AnnotatedControllerConfigurer をカスタムすればいけそう
14 © SMS Co., Ltd. 並列実行するには こんな感じで AnnotatedControllerConfigurer を Executor
とともに設定
15 © SMS Co., Ltd. 並列で実行されるようになった 🎉🎉🎉 並列実行するには
16 © SMS Co., Ltd. 実はドキュメントには、こうも書かれている 並列実行するには Spring for GraphQL
の Spring Boot スターターは、 プロパティ spring.threads.virtual.enabled が設定されている場合、仮想スレッドの Executor を使 用して AnnotatedControllerConfigurer を自動的に構成します。 参考: https://spring.pleiades.io/spring-graphql/reference/controllers.html#controllers.schema-mapping.return.values → Virtual Threads を有効化すると自動的に非同期実行!!!
17 © SMS Co., Ltd. ちょっと込み入った話
18 © SMS Co., Ltd. 実はデフォルトでも AnnotatedControllerConfigure の Executor はセットされている
ちょっと込み入った話 参考: https://github.com/spring-projects/spring-boot/blob/740362d3941a0952a7e92183a96c9ef28ef46de0/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/spri ngframework/boot/autoconfigure/task/TaskExecutorConfigurations.java#L59-L65 https://github.com/spring-projects/spring-boot/blob/740362d3941a0952a7e92183a96c9ef28ef46de0/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/spri ngframework/boot/autoconfigure/graphql/GraphQlAutoConfiguration.java#L155-L166
19 © SMS Co., Ltd. AnnotatedControllerConfigure の DataLoader の設定箇所の `shouldInvokeAsync`
に注目 ちょっと込み入った話 参考: https://github.com/spring-projects/spring-graphql/blob/7ee16217f76fc92240e89ee781607acf3ca0981c/spring-graphql/src/main/java/org/springframework/graphql/data/m ethod/annotation/support/AnnotatedControllerConfigurer.java#L346-L393 https://github.com/spring-projects/spring-graphql/blob/7ee16217f76fc92240e89ee781607acf3ca0981c/spring-graphql/src/main/java/org/springframework/graphql/data/m ethod/annotation/support/AnnotatedControllerDetectionSupport.java#L302-L305 「Executor が SchedulingTaskExecutor && prefersShortLivedTasks が true」ではない場合、非 同期で実行される
20 © SMS Co., Ltd. デフォルトの Executor の設定箇所、再掲 ちょっと込み入った話 ここで
build される ThreadPoolTaskExecutor は SchedulingTaskExecutor を実装。 そして、SchedulingTaskExecutor は prefersShortLivedTasks を true 固定。 →前のページの `shouldInvokeAsync` が false となり、非同期実行されず直列処理
21 © SMS Co., Ltd. リポジトリを見ると Virtual Threads 対応はされてい る...?🤔
参考: https://github.com/DataDog/dd-trace-java/pull/6789 Virtual Threads を有効にすると Datadog の javaagent でトレースが途切れる問題がある。 ※dd-java-agent-1.49.0 現在 ちょっと込み入った話 2
22 © SMS Co., Ltd. 参考: https://github.com/spring-projects/spring-boot/blob/740362 d3941a0952a7e92183a96c9ef28ef46de0/spring-boot-proje ct/spring-boot-autoconfigure/src/main/java/org/springframe work/boot/autoconfigure/task/TaskExecutorConfigurations.j
ava#L52-L57 参考: https://github.com/DataDog/dd-trace-java/blob/56844c2a3f b79bb9ece1f6a9078bb36e9e7d3939/dd-java-agent/instru mentation/java-concurrent/src/main/java/datadog/trace/inst rumentation/java/concurrent/TaskRunnerInstrumentation.ja va#L27-L30 Virtual Threads サポートの PR での計装対象は `ThreadPerTaskExecutor` だが... ちょっと込み入った話 2 Spring の AutoConfigure はデフォルトで `SimpleAsyncTaskExecutor` を設定してる →計装漏れにつながってそう? ※AnnotatedControllerConfigurer#setExecutor に Executors.newVirtualThreadPerTaskExecutor() を渡したら 途切れなかったので、そうっぽい
23 © SMS Co., Ltd. おしまい 動作確認に使ったサンプルプロジェクト : https://github.com/hmarui66/sprint-graphql/tree/lt ※main
ブランチはとっ散らかっているので lt ブランチを見てください