SpringOne Platform 2016 報告会「A Lite Rx API for the JVM」/ 井口 貝 @ SmartNews, Inc.

Post on 18-Jan-2017

164 views 1 download

Transcript of SpringOne Platform 2016 報告会「A Lite Rx API for the JVM」/ 井口 貝 @ SmartNews, Inc.

A Lite Rx API for the JVM

井口 貝 @ SmartNews, Inc.

SpringOne Platform 2016 報告会

自己紹介

名前

井口 貝 (いのくち かい)

@kainoque

所属

  サーバサイドエンジニア @ SmartNews, Inc.

ref: SmartNews, Inc. | Teamhttp://about.smartnews.com/ja/team/

自己紹介

自己紹介

SmartNews のニュース配信システム構成

・SpringOne 2016 のセッション報告を通じて、Reactive Streams の実装である Reactor を紹介

 ・Reactor API の紹介

 ・Spring 5 との関連性

 ・注意点など

はじめに

・セッション概要

・”Reactive”

・Reactive Streams

・Reactor

・Reactor and Spring 5

・Reactor API Hands-on

・Notes

もくじ

・セッション概要

・”Reactive”

・Reactive Streams

・Reactor

・Reactor and Spring 5

・Reactor API Hands-on

・Notes

もくじ

タイトル

Reactor Core 3.0: A Lite Rx API for the JVM

内容

・Reactive Streams の概要とその実装の一つである Reactor の紹介

・API ハンズオン

発表者

・Mr. Stéphane Maldini (Project Reactor lead)

・Mr. Sébastien Deleuze (Spring and Project Reactor committer)

セッション概要

・セッション概要

・”Reactive”

・Reactive Streams

・Reactor

・Reactor and Spring 5

・Reactor API Hands-on

・Notes

もくじ

“Reactive”広義には、「non-blocking で event-driven なシステム」を形容する言葉

Reactive Manifesto にてその必要要件が定義されている

・Responsive (即応性)

・Resilent (耐障害性)

・Elastic (弾力性)

・Message-Driven (メッセージ駆動)ref: The Reactive Manifestohttp://www.reactivemanifesto.org/

よりスケールし、安定的に動作し、キャパシティプランニングを容易にすることを標榜す

る場合、システムの Reactive 化は一つの選択肢となる

ユースケース

・BigData processsing・外部サービスに依存する webapp・データ生成側と利用側の処理速度に非対称性があるシステム

・大量のクライアントをさばく必要があるシステム

“Reactive”

もくじ

・セッション概要

・”Reactive”

・Reactive Streams

・Reactor

・Reactor and Spring 5

・Reactor API Hands-on

・Notes

Reactive Streams

”Reactive” なシステムを実現するための非同期ストリーム処理の標準仕様

・non-blocking な backpressure をもつ

・Reactive Streams Commons が策定

・j.u.c.Flow として Java 9 にも組み込まれる予定

・JEP 266: More Concurrency Updates

ref: JEP 266: More Concurrency Updateshttp://openjdk.java.net/jeps/266

ref: reactive-streams-jvm https://github.com/reactive-streams/reactive-streams-jvm

・Publisher と Subscriber の間のコミュニケーション

・Subscriber が受け取るデータの個数を、自身でコントロール可能 (backpressure)・Publisher は Subscriber の要求数より多いデータを送らない

・Publisher は Subscriber から要求されるまでデータを送らない

Reactive Streams

4 interfaces / 7 methods の定義 (+ TCK)

・Publisher

・Subscriber

・Subscription

・Processor

Reactive Streams Subscriber Publisher Subscription

Subscriber Publisher Subscription

subscribe(this)

generate

onSubscribe(s)

s

request(1)

request something

onNext(data)

onComplete()

Reactive StreamsPublisher

public interface Publisher<T> { void subscribe(Subscriber<T> s);}

Subscriber Publisher Subscription

Subscriber Publisher Subscription

subscribe(this)

generate

onSubscribe(s)

s

request(1)

request something

onNext(data)

onComplete()

Reactive StreamsSubscriber

public interface Subscriber<T> { void onSubscribe(Subscription s); void onNext(T t); void onError(Throwable t); void onComplete();}

Subscriber Publisher Subscription

Subscriber Publisher Subscription

subscribe(this)

generate

onSubscribe(s)

s

request(1)

request something

onNext(data)

onComplete()

public interface Subscription { void request(long n); void cancel();}

Reactive StreamsSubscription

Subscriber Publisher Subscription

Subscriber Publisher Subscription

subscribe(this)

generate

onSubscribe(s)

s

request(1)

request something

onNext(data)

onComplete()

public interface Processor<T, R> extends Subscriber<T>, Publisher<R> {}

Reactive StreamsProcessor

Component A Component CComponent BSubscriber of A / Publisher of C

Processor<T1, T2>

T1 T2

Reactive StreamsSubscriber Publisher Subscription

Subscriber Publisher Subscription

subscribe(this)

generate

onSubscribe(s)

s

request(1)

request something

onNext(data)

onComplete()

・Reactive Streams 自体は直接開発者に触られることを必ずしも想定していない

・通常は Reactive Streams を実装した Reactive API Library を使用する

・実装 Library

・RxJava 2.0 (JVM)

・Akka-Stream (JVM)

・Reactor 3.0 (JVM)

・RxJS (JavaScript) などなど

Reactive Streams

・セッション概要

・”Reactive”

・Reactive Streams

・Reactor

・Reactor and Spring 5

・Reactor API Hands-on

・Notes

もくじ

Reactor

Reactive Streams を実装した JVM 向け API ライブラリの一つ

・buffer, merge, concatenate, filter, map などの API を提供する

・RxJava の project lead である David Karnok や  Spring Framework の commiter が中心となって contribute している

Reactor特徴

・リソースの効率性に強くフォーカスしている

・contributor の多くが共通する Spring 5 とも相性良く設計されている

・Java 8 の各インターフェイスをベースにしている

・RxJava などに顕著な専用の Action1 などのクラスは用いていない

・デバッグ・ロギング・テストを容易に行える

 ・4th Generation Reactive Library

ref: Operator-fusion (Part 1)http://akarnokd.blogspot.jp/2016/03/operator-fusion-part-1.html

Reactor

Reactor・API 紹介

・Mono

・Flux

・Schedulers

・各型の比較

・RxJava との相互運用

・API 紹介

・Mono

・Flux

・Schedulers

・各型の比較

・RxJava との相互運用

Reactor

ReactorMono

・1 個 (または 0 個) の値のみを emit する Publisher 実装

・reactor.core.publisher.Mono

Mono

Reactor

Mono

Reactor

Flux

・複数の値を emit する Publisher 実装

・reactor.core.publisher.Flux

Reactor

Flux

Reactor

Flux

Reactor

Scheduler

・publish / subscribe を行うスレッドのスレッドプールの factory

・Schedulers.single(), parallel(), elastic() 等で生成

・publish / subscribe は、デフォルトでは呼び出し側スレッドで行われる

Reactor

Scheduler

・Slow-Publisher, Fast-Subscriber

・someFlux.subscribeOn(Schedulers.elastic())

Reactor

Scheduler

・Fast-Publisher, Slow-Subscriber

・someFlux.publishOn(Schedulers.elastic())

Reactor

Reactor・API 紹介

・Mono

・Flux

・Schedulers

・各型の比較

・RxJava との相互運用

No value Single value Multiple Values

Blocking void TFuture<T>

Iterable<T>Collection<T>Stream<T>

Non-Blocking CompletableFuture<Void>

CompletableFuture<T>

CompletableFuture<List<T>>

Reactive Streams Publisher<Void> Publisher<T> Publisher<T>

RxJava Completable Single<T> Observable<T>

Reactor Mono<Void> Mono<T> Flux<T>

Reactor

Reactor・API 紹介

・Mono

・Flux

・Schedulers

・各型の比較

・RxJava との相互運用

RxJava1Adapter

RxJava 1 系 の Completable / Single / Observable と Reactor の

Mono / Flux の相互変換アダプタ

Reactor

from RxJava to RxJava

No value completableToMono publisherToCompletable

Single value singleToMono publisherToSingle

Multiple Values observableToFlux publisherToObservable

・セッション概要

・”Reactive”

・Reactive Streams

・Reactor

・Reactor and Spring 5

・Reactor API Hands-on

・Notes

もくじ

Reactor and Spring 5Spring 5 における Reactive サポートの導入に際して、

Reactor Core がその基礎となっている

・例:

・Flux/Mono を受け取り、返却可能な Controller の導入

・Flux/Mono を返却可能な HTTP Client 実装の導入

ref: Reactive Programming with Spring 5.0 M1https://spring.io/blog/2016/07/28/reactive-programming-with-spring-5-0-m1

Controller

Reactor and Spring 5

Reactive HTTP Client

in org.springframework.web.client.reactive

Reactor and Spring 5

Spring Initializr で手軽に試すことができる

Dependencies から Reactive Web を選択 (Spring Boot のバージョンは 1.4.1 (SNAPSHOT))

Reactor and Spring 5

ref: SPRING INITIALIZR bootstrap your application nowhttps://start.spring.io/

・セッション概要

・”Reactive”

・Reactive Streams

・Reactor

・Reactor and Spring 5

・Reactor API Hands-on

・Notes

もくじ

Reactor の各 API を理解するためのハンズオン

Part 1 から 9 まで、各 API を利用した JUnit テストが書かれている

それをグリーンにしながら進めていく

Reactor API Hands-on

ref: Lite Rx API Hands-onhttps://github.com/reactor/lite-rx-api-hands-on

Part 1 Flux の作成

Part 2 Mono の作成

Part 3 値の変換

Part 4 Flux のマージ

Part 5 リクエスト

Part 6 その他の操作

Part 7 Reactive -> Blocking 処理の変換

Part 8 RxJava との相互運用

Part 9 Blocking -> Reactive 処理の変換

Reactor API Hands-on

Part 1 Flux の作成

Part 2 Mono の作成

Part 3 値の変換

Part 4 Flux のマージ

Part 5 リクエスト

Reactor API Hands-on

Part 6 その他の操作

Part 7 Reactive -> Blocking 処理の変換

Part 8 RxJava との相互運用

Part 9 Blocking -> Reactive 処理の変換

Mono#just

Reactor API Hands-on

Flux#fromIterable

Reactor API Hands-on

Part 1 Flux の作成

Part 2 Mono の作成

Part 3 値の変換

Part 4 Flux のマージ

Part 5 リクエスト

Reactor API Hands-on

Part 6 その他の操作

Part 7 Reactive -> Blocking 処理の変換

Part 8 RxJava との相互運用

Part 9 Blocking -> Reactive 処理の変換

{Mono,Flux}#map

Reactor API Hands-on

{Mono,Flux}#flatMap

Reactor API Hands-on

Part 1 Flux の作成

Part 2 Mono の作成

Part 3 値の変換

Part 4 Flux のマージ

Part 5 リクエスト

Reactor API Hands-on

Part 6 その他の操作

Part 7 Reactive -> Blocking 処理の変換

Part 8 RxJava との相互運用

Part 9 Blocking -> Reactive 処理の変換

Flux#mergeWith

Reactor API Hands-on

Flux#concatWith

Reactor API Hands-on

Part 1 Flux の作成

Part 2 Mono の作成

Part 3 値の変換

Part 4 Flux のマージ

Part 5 リクエスト

Reactor API Hands-on

Part 6 その他の操作

Part 7 Reactive -> Blocking 処理の変換

Part 8 RxJava との相互運用

Part 9 Blocking -> Reactive 処理の変換

Reactor API Hands-onFlux#zip

Reactor API Hands-onMono#first

Reactor API Hands-onflux#onErrorResumeWith

Part 1 Flux の作成

Part 2 Mono の作成

Part 3 値の変換

Part 4 Flux のマージ

Part 5 リクエスト

Reactor API Hands-on

Part 6 その他の操作

Part 7 Reactive -> Blocking 処理の変換

Part 8 RxJava との相互運用

Part 9 Blocking -> Reactive 処理の変換

Reactor API Hands-onMono#block

Reactor API Hands-onFlux#defer

・セッション概要

・”Reactive”

・Reactive Streams

・Reactor

・Reactor and Spring 5

・Reactor API Hands-on

・Notes

もくじ

・安定的な動作、予測可能なキャパシティと引き換えに、オーバーヘッドはある

・非同期処理特有の複雑さ、デバッグのしづらさはある

Notes

Notes

Notes

おわりに

・”Reactive” なシステムを実現するための標準仕様として Reactive Streams が存在する

・Reactive なシステムを実現するライブラリとして Reactor という選択肢がある

・Non-blocking + backpressure を備えリソース効率性が高い Reactive API Library

・Java 8 / Spring 5 と統合されている

・Mono / Flux により操作を行う

・依然として銀の弾丸ではない

http://about.smartnews.com/ja/careers/

We’re hiring!!

Web Application / iOS / Android / SRE / Ad-tech / Machine-learning / NLP