akka streams in action @ scaladays berlin 2016

69
Konrad `@ktosopl` Malawski // ScalaDays Berlin pic: Armored Core for Answer, White Gilt streams and Reactive Streams in action

Upload: konrad-malawski

Post on 16-Apr-2017

1.492 views

Category:

Technology


4 download

TRANSCRIPT

Page 1: Akka Streams in Action @ ScalaDays Berlin 2016

Konrad `@ktosopl` Malawski // ScalaDays Berlin pic: Armored Core for Answer, White Gilt

streamsand Reactive Streams in action

Page 2: Akka Streams in Action @ ScalaDays Berlin 2016

Konrad `ktoso` Malawski

Akka Team, Reactive Streams TCK,

Persistence, HTTP

Page 3: Akka Streams in Action @ ScalaDays Berlin 2016

Konrad `@ktosopl` Malawski

akka.iotypesafe.comgeecon.org

Java.pl / KrakowScala.plsckrk.com

GDGKrakow.pl lambdakrk.pl

Page 4: Akka Streams in Action @ ScalaDays Berlin 2016
Page 5: Akka Streams in Action @ ScalaDays Berlin 2016

… in Action

Yes, we’ll build stuff.

Page 6: Akka Streams in Action @ ScalaDays Berlin 2016

… in Action

Yes, we’ll build stuff.

With pre-release stuff, because ScalaDays.

Page 7: Akka Streams in Action @ ScalaDays Berlin 2016

“Stream”

Page 8: Akka Streams in Action @ ScalaDays Berlin 2016

“Stream” What does it mean?!

Page 9: Akka Streams in Action @ ScalaDays Berlin 2016

Akka Streams

Akka Streams && Reactive Streams

Page 10: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams - story: early FRP

http://blogs.msdn.com/b/rxteam/archive/2009/11/17/announcing-reactive-extensions-rx-for-net-silverlight.aspxhttp://infoscience.epfl.ch/record/176887/files/DeprecatingObservers2012.pdf - Ingo Maier, Martin Odersky

https://github.com/ReactiveX/RxJava/graphs/contributorshttps://github.com/reactor/reactor/graphs/contributors

https://medium.com/@viktorklang/reactive-streams-1-0-0-interview-faaca2c00bec#.69st3rndy

- .NETs’ Reactive Extensions

.NET 3.5

Page 11: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams - story: 2013’s impls

~2013:

Reactive Programming becoming widely adopted on JVM.

- Play introduced “Iteratees”- Akka (2009) had Akka-IO (TCP etc.)- Ben starts work on RxJava

http://blogs.msdn.com/b/rxteam/archive/2009/11/17/announcing-reactive-extensions-rx-for-net-silverlight.aspxhttp://infoscience.epfl.ch/record/176887/files/DeprecatingObservers2012.pdf - Ingo Maier, Martin Odersky

https://github.com/ReactiveX/RxJava/graphs/contributorshttps://github.com/reactor/reactor/graphs/contributors

https://medium.com/@viktorklang/reactive-streams-1-0-0-interview-faaca2c00bec#.69st3rndy

Teams discuss need for back-pressure in simple user API.Play’s Iteratee / Akka’s NACK in IO.

}

Page 12: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams - story: 2013’s impls

Play Iteratees – pull back-pressure, difficult API

http://blogs.msdn.com/b/rxteam/archive/2009/11/17/announcing-reactive-extensions-rx-for-net-silverlight.aspxhttp://infoscience.epfl.ch/record/176887/files/DeprecatingObservers2012.pdf - Ingo Maier, Martin Odersky

https://github.com/ReactiveX/RxJava/graphs/contributorshttps://github.com/reactor/reactor/graphs/contributors

https://medium.com/@viktorklang/reactive-streams-1-0-0-interview-faaca2c00bec#.69st3rndy

Akka-IO – NACK back-pressure; low-level IO (Bytes); messaging API

RxJava – no back-pressure, nice API

Page 13: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams - Play’s Iteratees

def fold[B]( done: (A, Input[E]) => Promise[B], cont: (Input[E] => Iteratee[E, A]) => Promise[B], error: (String, Input[E]) => Promise[B]): Promise[B]

// an iteratee that consumes chunkes of String and produces an IntIteratee[String,Int]

https://www.playframework.com/documentation/2.0/Iteratees

Feb 2013 Iteratees solved the back-pressure problem,but were hard to use.

Iteratee & Enumeratee – Haskell inspired.

Play / Akka teams looking for common concept.

Page 14: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams - expert group founded

October 2013

Roland Kuhn (Akka) and Erik Meijer (Rx .NET) meet in Lausanne,while recording “Principles of Reactive Programming” Coursera Course.

Viktor Klang (Akka), Erik Meijer, Ben Christensen (RxJava) and Marius Eriksen (Twitter) meet at Twitter HQ.

The term “reactive non-blocking asynchronous back-pressure” gets coined.

Page 15: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams - expert group founded

October 2013

Roland Kuhn (Akka) and Erik Meijer (Rx .NET) meet in Lausanne,while recording “Principles of Reactive Programming” Coursera Course.

Viktor Klang (Akka), Erik Meijer, Ben Christensen (RxJava) and Marius Eriksen (Twitter) meet at Twitter HQ.

The term “reactive non-blocking asynchronous back-pressure” gets coined.

Goals:- asynchronous- never block (waste)- safe (back-threads pressured)- purely local abstraction- allow synchronous impls.

Also, for our examples today:- compatible with TCP

Page 16: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams - expert group founded

October 2013

Roland Kuhn (Akka) and Erik Meijer (Rx .NET) meet in Lausanne,while recording “Principles of Reactive Programming” Coursera Course.

Viktor Klang (Akka), Erik Meijer, Ben Christensen (RxJava) and Marius Eriksen (Twitter) meet at Twitter HQ.

The term “reactive non-blocking asynchronous back-pressure” gets coined.

December 2013Stephane Maldini & Jon Brisbin (Pivotal Reactor) contacted by Viktor.

Page 17: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams - expert group founded

October 2013

Roland Kuhn (Akka) and Erik Meijer (Rx .NET) meet in Lausanne,while recording “Principles of Reactive Programming” Coursera Course.

Viktor Klang (Akka), Erik Meijer, Ben Christensen (RxJava) and Marius Eriksen (Twitter) meet at Twitter HQ.

The term “reactive non-blocking asynchronous back-pressure” gets coined.

December 2013Stephane Maldini & Jon Brisbin (Pivotal Reactor) contacted by Viktor.

Soon after, the “Reactive Streams” expert group is formed.

Also joining the efforts: Doug Lea (Oracle), Endre Varga (Akka), Johannes Rudolph & Mathias Doenitz (Spray), and many others, including myself join the effort soon after.

Page 18: Akka Streams in Action @ ScalaDays Berlin 2016

October 2013

Roland Kuhn (Akka) and Erik Meijer (Rx .NET) meet in Lausanne,while recording “Principles of Reactive Programming” Coursera Course.

Viktor Klang (Akka), Erik Meijer, Ben Christensen (RxJava) and Marius Eriksen (Twitter) meet at Twitter HQ.

The term “reactive non-blocking asynchronous back-pressure” gets coined.

December 2013Stephane Maldini & Jon Brisbin (Pivotal Reactor) contacted by Viktor.

Soon after, the “Reactive Streams” expert group is formed.

Also joining the efforts: Doug Lea (Oracle), Endre Varga (Akka), Johannes Rudolph & Mathias Doenitz (Spray), and many others, including myself join the effort soon after.

Reactive Streams - expert group founded

I ended up implementing much of the TCK.Please use it, let me know if it needs improvements :-)

Page 19: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams - story: 2013’s impls

2014–2015:

Reactive Streams Spec & TCK development, and implementations.

1.0 released on April 28th 2015, with 5+ accompanying implementations.

2015Proposed to be included with JDK9 by Doug Leavia JEP-266 “More Concurrency Updates”

http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/6e50b992bef4/src/java.base/share/classes/java/util/concurrent/Flow.java

Page 20: Akka Streams in Action @ ScalaDays Berlin 2016

2014–2015:

Reactive Streams Spec & TCK development, and implementations.

1.0 released on April 28th 2015, with 5+ accompanying implementations.

2015Proposed to be included with JDK9 by Doug Leavia JEP-266 “More Concurrency Updates”

http://hg.openjdk.java.net/jdk9/jdk9/jdk/file/6e50b992bef4/src/java.base/share/classes/java/util/concurrent/Flow.java

Reactive Streams - story: 2013’s impls

Page 21: Akka Streams in Action @ ScalaDays Berlin 2016

JEP-266 – soon…!public final class Flow { private Flow() {} // uninstantiable

@FunctionalInterface public static interface Publisher<T> { public void subscribe(Subscriber<? super T> subscriber); }

public static interface Subscriber<T> { public void onSubscribe(Subscription subscription); public void onNext(T item); public void onError(Throwable throwable); public void onComplete(); }

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

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

Page 22: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams: goals1) Avoiding unbounded buffering across async boundaries2) Inter-op interfaces between various libraries

Page 23: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams: goals1) Avoiding unbounded buffering across async boundaries

2) Inter-op interfaces between various libraries

Page 24: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams: goals1) Avoiding unbounded buffering across async boundaries

2) Inter-op interfaces between various libraries

Argh, implementing a correct RS Publisher or Subscriber is so hard!

Page 25: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams: goals1) Avoiding unbounded buffering across async boundaries

2) Inter-op interfaces between various libraries

Argh, implementing a correct RS Publisher or Subscriber is so hard!

Page 26: Akka Streams in Action @ ScalaDays Berlin 2016

Reactive Streams: goals1) Avoiding unbounded buffering across async boundaries

2) Inter-op interfaces between various libraries

Argh, implementing a correct RS Publisher or Subscriber is so hard!

Don’t implement RS interfaces directly, use GraphStage instead!

Page 27: Akka Streams in Action @ ScalaDays Berlin 2016

Akka Streams

Streams complement Actors, they do not replace them.

Actors – distribution (location transparency) Streams – back-pressured + more rigid-blueprint

Page 28: Akka Streams in Action @ ScalaDays Berlin 2016

13番: Akka is a Toolkit, pick the right tools for the job.

Runar’s excellent talk @ Scala.World 2015

Asynchronous processing toolbox:

Power

Constraints

Page 29: Akka Streams in Action @ ScalaDays Berlin 2016

13番: Akka is a Toolkit, pick the right tools for the job.

Asynchronous processing toolbox:

Constraints

Power

Page 30: Akka Streams in Action @ ScalaDays Berlin 2016

13番: Akka is a Toolkit, pick the right tools for the job.

Single value, no streaming by definition.Local abstraction. Execution contexts.

Power

Constraints

Page 31: Akka Streams in Action @ ScalaDays Berlin 2016

13番: Akka is a Toolkit, pick the right tools for the job.

Mostly static processing layouts.Well typed and Back-pressured!

Constraints

Power

Page 32: Akka Streams in Action @ ScalaDays Berlin 2016

13番: Akka is a Toolkit, pick the right tools for the job.

Plain Actor’s younger brother, experimental.Location transparent, well typed.Technically unconstrained in actions performed

Constraints

Power

Page 33: Akka Streams in Action @ ScalaDays Berlin 2016

13番: Akka is a Toolkit, pick the right tools for the job.

Runar’s excellent talk @ Scala.World 2015

Location transparent. Various resilience mechanisms.

(watching, persistent recovering, migration, pools)

Untyped and unconstrained in actions performed.

Constraints

Power

Page 34: Akka Streams in Action @ ScalaDays Berlin 2016

Lightbend monitoring

Measure. Don’t guess.

http://monitoring.lightbend.com/docs/latest/home.html

Page 35: Akka Streams in Action @ ScalaDays Berlin 2016

Lightbend monitoring

Measure. Don’t guess.

Let’s find if & where we have a bottleneck…

http://monitoring.lightbend.com/docs/latest/home.html

DEMO

Page 36: Akka Streams in Action @ ScalaDays Berlin 2016

Materialization

Gears from GeeCON.org, did I mention it’s an awesome conf?

Page 37: Akka Streams in Action @ ScalaDays Berlin 2016

What is “materialization” really?

Page 38: Akka Streams in Action @ ScalaDays Berlin 2016

What is “materialization” really?

Page 39: Akka Streams in Action @ ScalaDays Berlin 2016

What is “materialization” really?

Page 40: Akka Streams in Action @ ScalaDays Berlin 2016

What is “materialization” really?

Page 41: Akka Streams in Action @ ScalaDays Berlin 2016

Custom Stages &

Understanding the Types

Page 42: Akka Streams in Action @ ScalaDays Berlin 2016

Custom stages, made simple (!)

Page 43: Akka Streams in Action @ ScalaDays Berlin 2016

Custom stages, made simple (!)

Page 44: Akka Streams in Action @ ScalaDays Berlin 2016

Custom stages, made simple (!)

Page 45: Akka Streams in Action @ ScalaDays Berlin 2016

Custom stages, made simple (!)

Page 46: Akka Streams in Action @ ScalaDays Berlin 2016

Understanding the Types

Source.fromGraph(Graph[SourceShape[Status], M]) .via(Graph[FlowShape[Status, T], M]) .to(Graph[SinkShape[Status], _])

Page 47: Akka Streams in Action @ ScalaDays Berlin 2016

Understanding the Types

Source.fromGraph(Graph[SourceShape[Status], M]) .via(Graph[FlowShape[Status, T], M]) .to(Graph[SinkShape[Status], _])

Page 48: Akka Streams in Action @ ScalaDays Berlin 2016

Custom != hard; custom == extensible

Your Stages “feel” native.

DEMOLet’s try “Hundred-ify”.

http://doc.akka.io/docs/akka/2.4.7/scala/stream/stream-customize.html#graphstage-scala

See also mapConcat / statefulMapConcat.

Page 49: Akka Streams in Action @ ScalaDays Berlin 2016

Akka HTTP ~ streaming all the way

Streaming all the way with Akka HTTP

Page 50: Akka Streams in Action @ ScalaDays Berlin 2016

Streaming in Akka HTTPDEMO

http://doc.akka.io/docs/akka/2.4.7/scala/stream/stream-customize.html#graphstage-scala “Framed entity streaming” https://github.com/akka/akka/pull/20778

HttpServer as a: Flow[HttpRequest, HttpResponse]

Page 51: Akka Streams in Action @ ScalaDays Berlin 2016

Streaming in Akka HTTPDEMO

http://doc.akka.io/docs/akka/2.4.7/scala/stream/stream-customize.html#graphstage-scala “Framed entity streaming” https://github.com/akka/akka/pull/20778

HttpServer as a: Flow[HttpRequest, HttpResponse]

HTTP Entity as a: Source[ByteString, _]

Page 52: Akka Streams in Action @ ScalaDays Berlin 2016

Streaming in Akka HTTPDEMO

http://doc.akka.io/docs/akka/2.4.7/scala/stream/stream-customize.html#graphstage-scala “Framed entity streaming” https://github.com/akka/akka/pull/20778

HttpServer as a: Flow[HttpRequest, HttpResponse]

HTTP Entity as a: Source[ByteString, _]

Websocket connection as a: Flow[ws.Message, ws.Message]

Page 53: Akka Streams in Action @ ScalaDays Berlin 2016

Persistence Query (experimental)

“The Query Side” of Akka Persistence

Page 54: Akka Streams in Action @ ScalaDays Berlin 2016

Persistence Query (experimental)

Persistence Query Journals

akka/akka-persistence-cassandra 0.16 akka/akka @ leveldb-journal 2.4.8

dnvriend/akka-persistence-jdbc 2.3.3 scullxbones/akka-persistence-mongo 1.2.5

…and more, that I likely forgot about.

DEMO

Implementation of “data-pump” pattern.

Page 55: Akka Streams in Action @ ScalaDays Berlin 2016

Akka + Kafka = BFF

Reactive Kafka

+

Started by Krzysiek Ciesielski & Adam Warski @ SofwareMill.com

Page 56: Akka Streams in Action @ ScalaDays Berlin 2016

“ACKnowladged streams”

happy ACKing!

Page 57: Akka Streams in Action @ ScalaDays Berlin 2016

Kafka + Akka = BFF

Akka is Arbitrary processing.

Kafka is somewhat more than a message queue, but very focused on “the log”.

Spark shines with it’s data-science focus.

Page 58: Akka Streams in Action @ ScalaDays Berlin 2016

Kafka + Akka = BFF

Page 59: Akka Streams in Action @ ScalaDays Berlin 2016

Kafka + Akka = BFF

Page 60: Akka Streams in Action @ ScalaDays Berlin 2016

Streams talking to Actors &&

Actors talking to Streams

Page 61: Akka Streams in Action @ ScalaDays Berlin 2016

Streams <=> Actors inter-op

Source.actorRef (no back-pressure) Source.queue (safe)

Sink.actorRef (no back-pressure) Sink.actorRefWithAck (safe)

Page 62: Akka Streams in Action @ ScalaDays Berlin 2016

Exciting times ahead!

Page 63: Akka Streams in Action @ ScalaDays Berlin 2016

Next steps for Akka

Completely new Akka Remoting (goal: 1M+ msg/s (!)),(it is built using Akka Streams).

More integrations for Akka Streams stages, also dynamic fan-in/out A.K.A. “the Hub”.

Reactive Kafka polishing and stable release with SoftwareMill.

“Confirmed Streams” work from Reactive Kafka generalised.

Akka Typed likely to progress again.

Of course, continued maintenance of Cluster and others.

Page 64: Akka Streams in Action @ ScalaDays Berlin 2016

Happy hAkking!

Page 65: Akka Streams in Action @ ScalaDays Berlin 2016

Happy hAkking!

Announcing that there will be an announcement.

hAkknowledge (pilot name?)

“initiative” / “competition”

Page 66: Akka Streams in Action @ ScalaDays Berlin 2016

Akka <3 contributions• Easy to contribute:

• https://github.com/akka/akka/issues?q=is%3Aissue+is%3Aopen+label%3Aeasy-to-contribute • https://github.com/akka/akka/issues?q=is%3Aissue+is%3Aopen+label%3A%22nice-to-have+%28low-prio%29%22

• Akka Stream Contrib • https://github.com/akka/akka-stream-contrib

• Mailing list:• https://groups.google.com/group/akka-user

• Public chat rooms: • http://gitter.im/akka/dev developing Akka • http://gitter.im/akka/akka using Akka

Page 67: Akka Streams in Action @ ScalaDays Berlin 2016

Thanks!

ktoso @ typesafe.com twitter: ktosopl

github: ktosoteam blog: letitcrash.com

home: akka.io

Thus spake the Master Programmer:

“After three days without programming,life becomes meaningless.”

Page 68: Akka Streams in Action @ ScalaDays Berlin 2016

Q/A (Now’s the time to ask things!)

ktoso @ typesafe.com twitter: ktosopl

github: ktosoteam blog: letitcrash.com

home: akka.io

Page 69: Akka Streams in Action @ ScalaDays Berlin 2016

©Typesafe 2016 – All Rights Reserved