9. Streams
Real Time Stream Processing
When you attach “late” to a Publisher,
you may miss initial elements – it’s a river of data.
http://en.wikiquote.org/wiki/Heraclitus
18. Reactive Streams - Inter-op
http://reactive-streams.org
We want to make different implementations
co-operate with each other.
19. Reactive Streams - Inter-op
http://reactive-streams.org
The different implementations “talk to each other”
using the Reactive Streams protocol.
20. Reactive Streams - Inter-op
http://reactive-streams.org
The Reactive Streams SPI is NOT meant to be user-api.
You should use one of the implementing libraries.
71. Reactive Streams SPI
public interface Publisher<T> {
public void subscribe(Subscriber<? super T> s);
}
gives a public interface Subscription {
public void request(long n);
public void cancel();
}
A
72. Reactive Streams SPI
public interface Subscriber<T> {
public void onSubscribe(Subscription s);
public void onNext(T t);
public void onError(Throwable t);
public void onComplete();
}
public interface Publisher<T> {
public void subscribe(Subscriber<? super T> s);
}
gives a
to a
public interface Subscription {
public void request(long n);
public void cancel();
}
75. Akka
Akka is a high-performance concurrency
library for Scala and Java.
At it’s core it focuses on the Actor Model:
76. An Actor can only:
• Send and receive messages
• Create Actors
• Change it’s behaviour
Akka
Akka is a high-performance concurrency
library for Scala and Java.
At it’s core it focuses on the Actor Model:
77. class Player extends Actor {
def receive = {
case NextTurn => sender() ! decideOnMove()
}
def decideOnMove(): Move = ???
}
Akka
86. Akka Streams – Linear Flow
Flow[Double].map(_.toInt). [...]
No Source attached yet.
“Pipe ready to work with Doubles”.
87. Akka Streams – Linear Flow
implicit val sys = ActorSystem("tokyo-sys")
An ActorSystem is the world in which Actors live in.
AkkaStreams uses Actors, so it needs ActorSystem.
88. Akka Streams – Linear Flow
implicit val sys = ActorSystem("tokyo-sys")
implicit val mat = FlowMaterializer()
Contains logic on HOW to materialise the stream.
89. Akka Streams – Linear Flow
implicit val sys = ActorSystem("tokyo-sys")
implicit val mat = FlowMaterializer()
A materialiser chooses HOW to materialise a Stream.
The Flow’s AST is fully “lifted”.
The Materialiser can choose to materialise the Flow in any way it sees fit.
Our implementation uses Actors.
But you could easily plug in an SparkMaterializer!
90. Akka Streams – Linear Flow
implicit val sys = ActorSystem("tokyo-sys")
implicit val mat = FlowMaterializer()
You can configure it’s buffer sizes etc.
91. Akka Streams – Linear Flow
implicit val sys = ActorSystem("tokyo-sys")
implicit val mat = FlowMaterializer()
val foreachSink = Sink.foreach[Int](println)
val mf = Source(1 to 3).runWith(foreachSink)
92. Akka Streams – Linear Flow
implicit val sys = ActorSystem("tokyo-sys")
implicit val mat = FlowMaterializer()
val foreachSink = Sink.foreach[Int](println)
val mf = Source(1 to 3).runWith(foreachSink)(mat)
Uses the implicit FlowMaterializer
93. Akka Streams – Linear Flow
implicit val sys = ActorSystem("tokyo-sys")
implicit val mat = FlowMaterializer()
// sugar for runWith
Source(1 to 3).foreach(println)
94. Akka Streams – Linear Flow
val mf = Flow[Int].
map(_ * 2).
runWith(Sink.foreach(println))
// is missing a Source,
// can NOT run == won’t compile!
95. Akka Streams – Linear Flow
val f = Flow[Int].
map(_ * 2).
runWith(Sink.foreach(i => println(s"i = $i”))).
// needs Source to run!
96. Akka Streams – Linear Flow
val f = Flow[Int].
map(_ * 2).
runWith(Sink.foreach(i => println(s"i = $i”))).
// needs Source to run!
97. Akka Streams – Linear Flow
val f = Flow[Int].
map(_ * 2).
runWith(Sink.foreach(i => println(s"i = $i”))).
// needs Source to run!
98. Akka Streams – Linear Flow
val f = Flow[Int].
map(_ * 2).
runWith(Sink.foreach(i => println(s"i = $i”))).
// needs Source to run!
f.connect(Source(1 to 10)).run()
99. Akka Streams – Linear Flow
val f = Flow[Int].
map(_ * 2).
runWith(Sink.foreach(i => println(s"i = $i”))).
// needs Source to run!
f.connect(Source(1 to 10)).run()
With a Source attached… it can run()
100. Akka Streams – Linear Flow
Flow[Int].
map(_.toString).
runWith(Source(1 to 10), Sink.ignore)
Connects Source and Sink, then runs
101. Akka Streams – Flows are reusable
f.withSource(IterableSource(1 to 10)).run()
f.withSource(IterableSource(1 to 100)).run()
f.withSource(IterableSource(1 to 1000)).run()
103. Akka Streams <-> Actors – Advanced
Each “group” is a stream too! It’s a “Stream of Streams”.
val subscriber = ActorSubscriber(
system.actorOf(Props[SubStreamParent], ”parent”))
Source(1 to 100).
map(_.toString).
filter(_.length == 2).
drop(2).
groupBy(_.last).
runWith(subscriber)
104. Akka Streams <-> Actors – Advanced
groupBy(_.last).
GroupBy groups “11” to group “1”, “12” to group “2” etc.
105. Akka Streams <-> Actors – Advanced
groupBy(_.last).
It offers (groupKey, subStreamSource) to Subscriber
Source
106. Akka Streams <-> Actors – Advanced
groupBy(_.last).
It can then start children, to handle the sub-flows!
Source
107. Akka Streams <-> Actors – Advanced
groupBy(_.last).
For example, one child for each group.
Source
108. Akka Streams <-> Actors – Advanced
val subscriber = ActorSubscriber(
system.actorOf(Props[SubStreamParent], ”parent”))
Source(1 to 100).
map(_.toString).
filter(_.length == 2).
drop(2).
groupBy(_.last).
runWith(subscriber)
The Actor, will consume SubStream offers.
110. Akka Streams – FlowGraph
Linear Flows
or
non-akka pipelines
Could be another RS implementation!
111. Akka Streams – GraphFlow
Fan-out elements
and
Fan-in elements
112. Akka Streams – GraphFlow
// first define some pipeline pieces
val f1 = Flow[Input].map(_.toIntermediate)
val f2 = Flow[Intermediate].map(_.enrich)
val f3 = Flow[Enriched].filter(_.isImportant)
val f4 = Flow[Intermediate].mapFuture(_.enrichAsync)
// then add input and output placeholders
val in = SubscriberSource[Input]
val out = PublisherSink[Enriched]
118. There’s more to explore!
Topics we did explore today:
• asynchronous non-blocking back-pressure
• complex graph processing pipelines
• streams powered TCP server / client
• a sneak peek into custom elements
119. There’s more to explore!
Topics we didn’t explore today:
• explicit buffering, and overflow strategies
• integrating with Akka Actors
• time-based operators (takeWhile, dropWhile, timer transforms)
• plenty additional combinators and junctions
• implementing custom processing stages and junctions
120. There’s more to explore!
Future plans:
• API stabilisation and documentation (1.0 soon)
• Improve testability & TestKit
• Performance tuning of Streams & HTTP
• Provide more Sinks / Sources and operations
• Visualising flow graphs
• great experiment by Tim Harper
https://github.com/timcharper/reactive-viz
• Distributing computation graphs (?)
121. Links
• http://akka.io
• http://reactive-streams.org
• https://groups.google.com/group/akka-user
• Tim Harper’s awesome complex pipeline example + visualisation
https://github.com/timcharper/reactive-viz
• 1.0-M2 Documentation (not complete)
http://doc.akka.io/docs/akka-stream-and-http-experimental/1.0-M2/scala.html
• Complete JavaDSL for all operations
https://github.com/akka/akka/pulls?q=is%3Apr+javadsl