57
Copyright © 2015 TIS Inc. All rights reserved. Reactive System Meetup in 西新宿 Typesafe Reactive Platformで作る Reactive System入門 2015.8.18 前出 祐吾 根来 和輝

Typesafe Reactive Platformで作るReactive System入門

  • Upload
    tis-inc

  • View
    4.260

  • Download
    2

Embed Size (px)

Citation preview

Page 1: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved.

Reactive  System  Meetup  in  西新宿

Typesafe  Reactive  Platformで作るReactive  System入門2015.8.18

前出 祐吾  根来 和輝

Page 2: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 2

自己紹介■ @yugolf  

!

■ TIS株式会社  

■ 生産革新本部 生産革新部 生産技術R&D室  

■ 社内向けエンジニアリング基盤の整備 ▶▶︎

Page 3: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 3

TIS、リアクティブコンサルはじめました

http://www.tis.jp/service_solution/goreactive/

Page 4: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 4

Agenda

1.  Reactive  Systemとは  

2.  Reactive  Systemのための技術  

3.  Reactive体験

Page 5: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 5

1.    Reactive  Systemとは

Page 6: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 6

本日の話

http://www.reactivemanifesto.org/

Reactive  Programming:  

データの流れ(ストリーム)とデータの値の変化の伝播にフォーカスしたプログラムの設計モデル。  ・・・ の話ではない。

Reactive  System:

Page 7: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 7

Reactive manifesto

絵  !即応性  弾力性  耐障害性  メッセージ駆動

http://www.reactivemanifesto.org/ja

Page 8: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 8

アプリケーションへの高まる要求50台のサーバで動かしていたけど、クラウド上で1000コアの環境を作りたいんだ。

レスポンスを得るのに3秒かかるけど、500ミリ秒くらいにしたいのよ。  

それと、年1回の2時間のメンテナンスなんとかならないかしら。

最初は100ギガくらいだったけど、そろそろペタ行くんじゃないかな。

Page 9: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 9

Reactive Systemとは即応性と、耐障害性と、弾力性と、メッセージ駆動とを備えたシステム

http://www.reactivemanifesto.org/ja

Page 10: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 10

Reactive Systemの価値

届けたい価値

手段

支える原理

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 11: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. Reactive  Design  Pattern(http://manning.com/kuhn/) 11

Responsive

ユーザーの要求に迅速にレスポンスするシステム

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

届けたい価値

手段

支える原理

✓高レスポンスを維持するため、リソースプランニングを行い、コンポーネントの分割・分散を考慮すべき

Page 12: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 12

Elastic通常の負荷時だけでなく、一時的な高負荷状況でも、変動するワークロードに応じて割り当てるリソースを増加・減少させることで応答時間を一定水準に保つ  

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

届けたい価値

手段

支える原理

Reactive  Design  Pattern(http://manning.com/kuhn/)

✓伸縮性のため、負荷を監視し、その変化に応じて分散する必要がある  ✓1つの部品がボトルネックを持ってはいけないし、独立して動作すべき

Page 13: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 13

Resilient部分的な障害が発生した時にもシステム全体を危険にさらすことなく回復させ、高レスポンスを維持する

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

届けたい価値

手段

支える原理

Reactive  Design  Pattern(http://manning.com/kuhn/)

✓耐障害性を高めるため、故障したコンポーネントを修復したり、代替となるコンポーネントを再接続するメカニズムが必要

Page 14: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 14

Message Drivenメッセージパッシングにより、コンポーネント間を疎結合にする

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

届けたい価値

手段

支える原理

Reactive  Design  Pattern(http://manning.com/kuhn/)

✓すべてがメッセージ駆動により実現される

Page 15: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 15

2.  Reactive  Systemのための技術

Page 16: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 16

Typesafe Reactive Platform

JavaVM

アプリケーション

 AkkaPlay  Framework

Scala Java

Slick

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 17: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 17

Play Framework

•非同期/ノンブロッキング  •WebSocketサポート  •Railsのような使いやすさ

Page 18: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 18

これまで

def index = Action { implicit request => Ok(views.html.index())}

クライアント サーバブロッキング

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 19: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 19

Blocking

コーヒーください。

コーヒー1つ~

は~い

できた~

・・・

コーヒーどうぞ~  100円です。

コーヒーください。

コーヒー1つ~

Block

Page 20: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 20

Non-blocking

コーヒーください。

コーヒー1つ~

は~い

コーヒーどうぞ~  

コーヒーください。コーヒー1つ~

100円です。

100円です。は~い

できた~

コーヒーください。

Page 21: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 21

Reactive :迅速なレスポンス

def index = Action.async { val customer: Future[Customer] = getCustomer() customer.map(c => Ok("Customer: " + c))}

クライアント サーバ サービスノンブ

ロッキングノンブ

ロッキング

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 22: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 22

Reactive :サーバからのプッシュ

クライアント サーバ

def socket = WebSocket.using[String] { request => // Log events to the console val in = Iteratee.foreach[String](println).map { _ => println("Disconnected") } // Send a single 'Hello!' message val out = Enumerator("Hello!") (in, out)}

ノンブロッキング

WebSocket

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

https://www.playframework.com/documentation/2.4.x/ScalaWebSockets

Page 23: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 23

Akka

•並行処理と分散処理をサポートするActor  System  

•Supervision  Model  •location  transparency

Page 24: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 24

Actor System

sender

Actor

Mailbox

Mailbox

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 25: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 25

Reactive :Actor Model

def socket = WebSocket.acceptWithActor[String, String] { request => out => MyWebSocketActor.props(out)} object MyWebSocketActor { def props(out: ActorRef) = Props(new MyWebSocketActor(out))} class MyWebSocketActor(out: ActorRef) extends Actor { def receive = { case msg: String => out ! ("I received your message: " + msg) }}

クライアントコントローラ

Actor

ノンブロッキング

WebSocket

Actor

https://www.playframework.com/documentation/2.4.x/ScalaWebSockets

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 26: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 26

Supervision Modelで障害に強いシステムに

override val supervisorStrategy: SupervisorStrategy = AllForOneStrategy(maxNrOfRetries = 100, withinTimeRange = 1 minute) { case _ => Restart }

スーパー  バイザー

サブスーパー  バイザー

Actor

Actor Actor

-­‐OneForOne  -­‐AllForOne

障害を外へ波及させない

子アクターの監視役

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 27: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 27

let  it  crash

Page 28: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 28

Distribute

https://conductr.typesafe.com/docs/1.0.x/DeployingApplication

Page 29: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 29

Reactive :Actorを増減させ容易にスケール

クライアントコントローラ

Actor

Actor

Actorルーティングは様々  

•ランダム  •ラウンドロビン  •最小メールボックス  •ブロードキャスト  •最速採用

ノンブロッキング

WebSocket

Actor

location    transparently

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 30: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 30

Reactive Slick (Slick3.0)

•FRM(Functional  Relational  Mapping)ライブラリ  •Scalaのコレクションを扱うかのようにDBアクセスできる  

•DB処理は非同期に実行される

Page 31: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 31

Supported Databases

•MySQL  •PostgreSQL  •H2  •HSQLDB/HyperSQL  •SQLite  •Derby/JavaDB  •Microsoft  Access

•DB2  •Microsoft  SQL  Server  •Oracle

Slick Extensions

Page 32: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 32

Reactive :端から端までReactive

def search = Action.async { implicit request => … val f:Future[Seq[EventRow]] = db.run(Events.filter(form)) f.map { case events if (events.length > 0) => Ok(views.html.sample.search( form.bindFromRequest, events)) case _ => …

クライアントコントローラノンブ

ロッキング

WebSocket

Actor

ノンブロッキング

Actor

データベース

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 33: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 33

def search = Action.async { implicit request => … val f:Future[Seq[EventRow]] = db.run(Events.filter(form)) f.map { case events if (events.length > 0) => Ok(views.html.sample.search( form.bindFromRequest, events)) case _ => … 高レスポンス  

Responsive

クライアントコントローラノンブロッ

キング

WebSocket

Actor

ノンブロッキング

Actor

データベース

Asynchronousが基本、  

どこでWaitするかをデザインする

Reactive :端から端までReactive

Page 34: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 34

Reactive :Reactive Streams

def search = Action.async { implicit request => … val q = for(e <- Event) yield e.eventNm val publisher:DatabasePublisher[String] = db.stream(q.result) publisher.foreach {e => println(e)} …

クライアントコントローラノンブ

ロッキング

WebSocket

Actor

ノンブロッキング

データベース

Actor

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 35: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 35

3.  Reactive体験

Page 36: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 36

自己紹介■ 根来  和輝  (ねごろ  かずき)  

■ @negokaz  

■ TIS株式会社  

■ 生産革新本部 生産革新部 生産技術R&D室  

■ 次世代のアプリケーション開発手法を研究開発

Page 37: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 37

Reactive体験

もし、あなたがソーラーファームのオーナーになったら?

発電効率をできるだけ高めたいですよね。  故障したソーラーパネルがないか監視しましょう。

Page 38: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 38Reactive Solar Farm Monitor Solar Farm

③ アラートを表示ID:  1

② 故障している!パネルを検知

それぞれが毎秒、発電した電力(W)のデータを送信

ID:  1

Reactive Solar Farm Monitor

Icon  made  by  Freepik  from  www.flaticon.com  is  licensed  under  CC  BY  3.0

発電状況を表示① 故障して!発電力が低下

Page 39: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 39

ソーラーパネルの故障検知

全パネルの平均電力からしきい値を決め

それ以下の電力しか発電していないものを  故障したとみなす

‣発電している電力が  0  のものを故障とみなす?  

‣夜になったらどうする?

OK NG

平均値

しきい値

Page 40: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 40

故障検知でやるべきこと

‣全パネルの平均電力を求める  ‣平均値からしきい値を決める  ‣全パネルの電力としきい値を比較する

ただしリアルタイムで

Page 41: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 41

Architecture

Monitor Analyzer

SolarFarmSimulator

HTTP!&!

WebSocket

Page 42: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 42

https://github.com/tech-­‐sketch/reactive-­‐solar-­‐farm-­‐monitor

Reactive体験1 - Responsive

Page 43: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 43

Reactive体験1 - Responsive

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 44: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 44

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

届けたい価値

手段

支える原理

Reactive体験2 - Elastic

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 45: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 45

1つのActorですべての計算処理を行う場合Application  Cluster

Analyzer

AnalyzerMonitor

Mater

1

32

4

Worker

SolarFarmSimulator高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 46: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 46

Application  Cluster

Analyzer

Analyzer

Analyzer

Analyzer

Monitor

Master1

3

2

4

複数のActorに処理を振り分けるWorker

SolarFarmSimulator高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 47: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 47

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

届けたい価値

手段

支える原理

Reactive体験3 - Resilient

Page 48: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 48

Application  Cluster

Analyzer

AnalyzerMonitor

Master13

2

4

WorkerがダウンするとWorker

AnalyzerAnalyzer

SolarFarmSimulator高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 49: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved.

Application  Cluster

Analyzer

Analyzer

Analyzer

Analyzer

Monitor

Master1 3

2

4

Worker

残りのWorkerでやりくりする

SolarFarmSimulator高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 50: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 50

Application  Cluster

Analyzer

Analyzer

Analyzer

Analyzer

Monitor

Master1

3

2

4

MasterがダウンするとWorker

SolarFarmSimulator高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 51: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 51

Application  Cluster

Analyzer

Analyzer

AnalyzerMonitor

1

3

24

Analyzer

Master

WorkerのひとつがMasterになり残りでやりくりする

Worker

SolarFarmSimulator高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 52: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 52

Application  Cluster

Analyzer

AnalyzerMonitor

1

3

2

4

Analyzer

Master

ダウンしたAnalyzerを起動するとWorkerとなって働く

Worker

Analyzer

SolarFarmSimulator高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 53: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 53

支えているのはメッセージ駆動のアーキテクチャ

Reactive体験4 - Message Driven

高レスポンス  Responsive

伸縮性  Elastic

耐障害性  Resilient

メッセージ駆動  Message-driven

Page 54: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 54

4.  Go  Reactive

Page 55: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 55

Go Reactivehttp://www.tis.jp/service_solution/goreactive/

リアクティブに興味のある方はご連絡下さい。go-­‐[email protected]

Page 56: Typesafe Reactive Platformで作るReactive System入門

Copyright © 2015 TIS Inc. All rights reserved. 56

Go Reactive

ソースコードはGitHubで公開しています。

太陽光発電に興味のある方はご連絡下さい。https://github.com/tech-­‐sketch/reactive-­‐solar-­‐farm-­‐monitor

go-­‐[email protected]

Page 57: Typesafe Reactive Platformで作るReactive System入門

THANK YOU