-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
439 lines (379 loc) · 16.4 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
<!DOCTYPE html>
<html>
<head>
<title>Akka Streams Supervision</title>
<meta charset="utf-8">
<style>
@import url(https://fonts.googleapis.com/css?family=Ubuntu+Mono:400,700,400italic);
@import url(https://fonts.googleapis.com/css?family=Special+Elite|Coda);
body { font-family: 'Coda'; }
h1, h2, h3 {
font-family: 'Special Elite';
font-weight: normal;
}
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
.inverse {
background: #272822;
color: #777872;
text-shadow: 0 0 20px #333;
}
.inverse a {
color: #5F9EA0;
text-decoration: none;
}
.inverse h1, .inverse h2 {
color: #f3f3f3;
line-height: 0.8em;
}
.footnote {
position: absolute;
bottom: 3em;
}
#slide-dense .remark-code {
font-size: 14px;
}
#slide-terminal-last .remark-code {
font-size: 15px;
}
/* Slide-specific styling */
#slide-inverse .footnote {
bottom: 12px;
left: 20px;
}
</style>
</head>
<body>
<textarea id="source">
name: inverse
layout: true
class: inverse
---
template: inverse
class: center, middle
# Akka Streams
### Supervision
Radzislaw Galler
@gradzislaw
Paycasso Verify Ltd.
---
template: inverse
# Akka Streams
### Quick overview
* Allows to define programs as transforming data flow from a source to a sink.
--
* Consists of general blocks like `Source`, `Flow`, `Graph`, and `Sink`
--
* Defines common operations like `map`, `zip`, `broadcast`, `merge`, `take`, `drop`, `filter`, etc.
--
* Controls back-pressure for you, i.e. slows down producer (upstream) to match consumption speed.
--
* Akka Streams API is completely decoupled from the Reactive Streams interfaces, but is fully interoperable with it.
--
Read more on http://akka.io/docs/
---
# Dropping elements
_From the documentation_
### Akka Streams does not send dropped stream elements to the dead letter office
Elements can be dropped for a number of reasons:
* plain user code can consume one element in a `map(...)` stage and produce an entirely different one as its result
--
* common stream operators drop elements intentionally, e.g. take/drop/filter/conflate/buffer/…
--
* stream failure will tear down the stream without waiting for processing to finish, all elements that are in flight will be discarded
--
* stream cancellation will propagate upstream (e.g. from a `take` operator) leading to upstream processing steps being terminated without having processed all of their inputs
---
<div style="width: 960px; height: 600px; margin: 10px; position: relative;">
<iframe allowfullscreen frameborder="0" style="width:960px; height:600px" src="https://www.lucidchart.com/documents/embeddedchart/5931da0f-197c-4bc4-896b-8655cecd2640" id="gxGZX79uIMbH"></iframe>
</div>
---
```
object Example {
implicit val system = ActorSystem("PSUG")
implicit val materializer = ActorMaterializer()
implicit val log = Logging(system, this.getClass)
val example = Flow.fromGraph(GraphDSL.create() { implicit builder =>
import GraphDSL.Implicits._
val MAX_THREADS = 4
// prepare graph elements
val broadcast = builder.add(Broadcast[Int](2).named("broadcast"))
val flow1 = Flow[Int].mapAsync(MAX_THREADS)(i => Future(i.toString)).log("flow1")
val flow2 = Flow[Int].map(i => if(i % 2 != 0) throw new Exception(s"$i is odd") else i.toString).log("flow2")
val zip = builder.add(ZipWith {(s1: String, s2: String) => s"($s1, $s2)"}.named("zip"))
// define the graph
broadcast ~> flow1 ~> zip.in0
broadcast ~> flow2 ~> zip.in1
// expose ports
FlowShape(broadcast.in, zip.out)
})
val source = Source(1 to 10).log("source")
val sink = Sink.fold[String, String]("")(_ + _)
}
object Main extends App {
import Example._
example.runWith(source, sink)._2.map(println)
}
```
---
### Result:
```terminal
[info] Running io.github.rajish.Main
[DEBUG] [03/03/2016 01:55:25.163] [run-main-0] [EventStream] StandardOutLogger started
2016-03-03 01:55:25,505 |INFO | [] akka.event.slf4j.Slf4jLogger - Slf4jLogger started
[DEBUG] [03/03/2016 01:55:25.512] [run-main-0] [EventStream(akka://PSUG)] logger log1-Slf4jLogger started
[DEBUG] [03/03/2016 01:55:25.513] [run-main-0] [EventStream(akka://PSUG)] Default Loggers started
2016-03-03 01:55:25,515 |DEBUG| [EventStream(akka://PSUG)] akka.event.EventStream - logger log1-Slf4jLogger started
2016-03-03 01:55:25,518 |DEBUG| [EventStream(akka://PSUG)] akka.event.EventStream - Default Loggers started
2016-03-03 01:55:25,696 |DEBUG| [Example$(akka://PSUG)] io.github.rajish.Example$ - [source] Element: 1
2016-03-03 01:55:25,711 |ERROR| [Example$(akka://PSUG)] io.github.rajish.Example$ - [flow2] Upstream failed.
java.lang.Exception: 1 is odd
at io.github.rajish.Example$$anonfun$1$$anonfun$3.apply(Main.scala:22)
at io.github.rajish.Example$$anonfun$1$$anonfun$3.apply(Main.scala:22)
at akka.stream.impl.fusing.Map.onPush(Ops.scala:29)
[...]
2016-03-03 01:55:25,712 |DEBUG| [Example$(akka://PSUG)] io.github.rajish.Example$ - [flow1] Downstream finished.
2016-03-03 01:55:25,712 |DEBUG| [Example$(akka://PSUG)] io.github.rajish.Example$ - [source] Element: 2
2016-03-03 01:55:25,712 |DEBUG| [Example$(akka://PSUG)] io.github.rajish.Example$ - [source] Downstream finished.
^C
```
---
# Supervision
```
val decider: Supervision.Decider = {
case e: Exception =>
log.error(s"Exception was caught, flow was restarted. Error: '${e.getMessage}'")
Supervision.Restart
}
example
.withAttributes(ActorAttributes.supervisionStrategy(decider))
.runWith(source, sink)
._2.map(println)
```
---
### Result
```terminal
2016-03-03 02:16:50,175 |DEBUG| - [source] Element: 1
2016-03-03 02:16:50,181 |ERROR| - Exception was caught, flow was restarted. Error: '1 is odd'
2016-03-03 02:16:50,181 |DEBUG| - [source] Element: 2
2016-03-03 02:16:50,181 |DEBUG| - [flow2] Element: 2
2016-03-03 02:16:50,182 |DEBUG| - [flow1] Element: 1
2016-03-03 02:16:50,182 |DEBUG| - [source] Element: 3
2016-03-03 02:16:50,183 |ERROR| - Exception was caught, flow was restarted. Error: '3 is odd'
2016-03-03 02:16:50,183 |DEBUG| - [source] Element: 4
2016-03-03 02:16:50,183 |DEBUG| - [flow2] Element: 4
2016-03-03 02:16:50,183 |DEBUG| - [flow1] Element: 2
2016-03-03 02:16:50,184 |DEBUG| - [source] Element: 5
2016-03-03 02:16:50,184 |ERROR| - Exception was caught, flow was restarted. Error: '5 is odd'
2016-03-03 02:16:50,184 |DEBUG| - [source] Element: 6
2016-03-03 02:16:50,184 |DEBUG| - [flow2] Element: 6
2016-03-03 02:16:50,184 |DEBUG| - [flow1] Element: 3
2016-03-03 02:16:50,185 |DEBUG| - [source] Element: 7
2016-03-03 02:16:50,185 |ERROR| - Exception was caught, flow was restarted. Error: '7 is odd'
2016-03-03 02:16:50,185 |DEBUG| - [flow1] Element: 4
2016-03-03 02:16:50,185 |DEBUG| - [source] Element: 8
2016-03-03 02:16:50,185 |DEBUG| - [flow2] Element: 8
2016-03-03 02:16:50,185 |DEBUG| - [flow1] Element: 5
2016-03-03 02:16:50,186 |DEBUG| - [source] Element: 9
2016-03-03 02:16:50,186 |ERROR| - Exception was caught, flow was restarted. Error: '9 is odd'
^C
```
???
---
# Supervision, continued
* The supervision decider keeps the stream going but eventually prevents it from completing.
--
* Both Resume and Restart strategies cause the same output.
--
* The stream deadlocks because it's unbalanced.
---
# Balancing buffer
```
val buffer = Flow[Int].buffer(2, OverflowStrategy.backpressure)
...
// define the graph
broadcast ~> buffer ~> flow1 ~> zip.in0
broadcast ~> flow2 ~> zip.in1
```
.footnote[See blog post http://blog.lancearlaus.com/akka/streams/scala/2015/05/27/Akka-Streams-Balancing-Buffer/]
---
### Result
```terminal
2016-03-03 02:45:43,696 |DEBUG| - [source] Element: 1
2016-03-03 02:45:43,698 |ERROR| - Exception was caught, flow was restarted. Error: '1 is odd'
2016-03-03 02:45:43,702 |DEBUG| - [source] Element: 2
2016-03-03 02:45:43,702 |DEBUG| - [flow2] Element: 2
2016-03-03 02:45:43,702 |DEBUG| - [flow1] Element: 1
2016-03-03 02:45:43,703 |DEBUG| - [source] Element: 3
2016-03-03 02:45:43,703 |ERROR| - Exception was caught, flow was restarted. Error: '3 is odd'
2016-03-03 02:45:43,703 |DEBUG| - [source] Element: 4
2016-03-03 02:45:43,703 |DEBUG| - [flow2] Element: 4
2016-03-03 02:45:43,704 |DEBUG| - [flow1] Element: 2
2016-03-03 02:45:43,704 |DEBUG| - [source] Element: 5
2016-03-03 02:45:43,704 |ERROR| - Exception was caught, flow was restarted. Error: '5 is odd'
2016-03-03 02:45:43,704 |DEBUG| - [source] Element: 6
2016-03-03 02:45:43,704 |DEBUG| - [flow2] Element: 6
2016-03-03 02:45:43,705 |DEBUG| - [flow1] Element: 3
2016-03-03 02:45:43,705 |DEBUG| - [source] Element: 7
2016-03-03 02:45:43,705 |ERROR| - Exception was caught, flow was restarted. Error: '7 is odd'
2016-03-03 02:45:43,705 |DEBUG| - [source] Element: 8
2016-03-03 02:45:43,705 |DEBUG| - [flow2] Element: 8
2016-03-03 02:45:43,705 |DEBUG| - [flow1] Element: 4
2016-03-03 02:45:43,706 |DEBUG| - [source] Element: 9
2016-03-03 02:45:43,706 |ERROR| - Exception was caught, flow was restarted. Error: '9 is odd'
2016-03-03 02:45:43,706 |DEBUG| - [source] Element: 10
2016-03-03 02:45:43,706 |DEBUG| - [source] Upstream finished.
2016-03-03 02:45:43,706 |DEBUG| - [flow2] Element: 10
2016-03-03 02:45:43,706 |DEBUG| - [flow2] Upstream finished.
2016-03-03 02:45:43,707 |DEBUG| - [flow1] Element: 5
2016-03-03 02:45:43,707 |DEBUG| - [flow1] Downstream finished.
2016-03-03 02:45:43,708 |INFO | - (1, 2)(2, 4)(3, 6)(4, 8)(5, 10)
```
---
# Going functional
```
import scalaz.Scalaz._
import scalaz.{\/, -\/, \/-}
[...]
val flow2 = Flow[Int].map{ i =>
if(i % 2 != 0)
\/.fromTryCatchThrowable[String, Throwable](throw new Exception(s"$i is odd"))
else
\/.fromTryCatchThrowable[String, Throwable](i.toString)
}.log("flow2")
val zip = builder.add(ZipWith {(s1: String, s2: \/[Throwable, String]) => (s1, s2)}.named("zip"))
[...]
val sink = Sink.foreach[(String, \/[Throwable, String])] {
case (s1, \/-(s2)) => log.info(s"[sink] ($s1, $s2)")
case x => log.error(s"error in input: $x")
}
```
---
name: dense
```terminal
2016-03-03 03:21:21,913 |DEBUG| - [source] Element: 1
2016-03-03 03:21:21,934 |DEBUG| - [flow2] Element: -\/(java.lang.Exception: 1 is odd)
2016-03-03 03:21:21,936 |DEBUG| - [flow1] Element: 1
2016-03-03 03:21:21,938 |ERROR| - error in input: (1,-\/(java.lang.Exception: 1 is odd))
2016-03-03 03:21:21,938 |DEBUG| - [source] Element: 2
2016-03-03 03:21:21,938 |DEBUG| - [flow2] Element: \/-(2)
2016-03-03 03:21:21,938 |DEBUG| - [flow1] Element: 2
2016-03-03 03:21:21,939 |INFO | - [sink] (2, 2)
2016-03-03 03:21:21,940 |DEBUG| - [source] Element: 3
2016-03-03 03:21:21,940 |DEBUG| - [flow2] Element: -\/(java.lang.Exception: 3 is odd)
2016-03-03 03:21:21,940 |DEBUG| - [flow1] Element: 3
2016-03-03 03:21:21,940 |ERROR| - error in input: (3,-\/(java.lang.Exception: 3 is odd))
2016-03-03 03:21:21,940 |DEBUG| - [source] Element: 4
2016-03-03 03:21:21,940 |DEBUG| - [flow2] Element: \/-(4)
2016-03-03 03:21:21,941 |DEBUG| - [flow1] Element: 4
2016-03-03 03:21:21,941 |INFO | - [sink] (4, 4)
2016-03-03 03:21:21,941 |DEBUG| - [source] Element: 5
2016-03-03 03:21:21,941 |DEBUG| - [flow2] Element: -\/(java.lang.Exception: 5 is odd)
2016-03-03 03:21:21,941 |DEBUG| - [flow1] Element: 5
2016-03-03 03:21:21,942 |ERROR| - error in input: (5,-\/(java.lang.Exception: 5 is odd))
2016-03-03 03:21:21,942 |DEBUG| - [source] Element: 6
2016-03-03 03:21:21,942 |DEBUG| - [flow2] Element: \/-(6)
2016-03-03 03:21:21,942 |DEBUG| - [flow1] Element: 6
2016-03-03 03:21:21,942 |INFO | - [sink] (6, 6)
2016-03-03 03:21:21,943 |DEBUG| - [source] Element: 7
2016-03-03 03:21:21,943 |DEBUG| - [flow2] Element: -\/(java.lang.Exception: 7 is odd)
2016-03-03 03:21:21,943 |DEBUG| - [flow1] Element: 7
2016-03-03 03:21:21,944 |ERROR| - error in input: (7,-\/(java.lang.Exception: 7 is odd))
2016-03-03 03:21:21,944 |DEBUG| - [source] Element: 8
2016-03-03 03:21:21,944 |DEBUG| - [flow2] Element: \/-(8)
2016-03-03 03:21:21,944 |DEBUG| - [flow1] Element: 8
2016-03-03 03:21:21,944 |INFO | - [sink] (8, 8)
2016-03-03 03:21:21,944 |DEBUG| - [source] Element: 9
2016-03-03 03:21:21,945 |DEBUG| - [flow2] Element: -\/(java.lang.Exception: 9 is odd)
2016-03-03 03:21:21,945 |DEBUG| - [flow1] Element: 9
2016-03-03 03:21:21,945 |ERROR| - error in input: (9,-\/(java.lang.Exception: 9 is odd))
2016-03-03 03:21:21,945 |DEBUG| - [source] Element: 10
2016-03-03 03:21:21,945 |DEBUG| - [source] Upstream finished.
2016-03-03 03:21:21,945 |DEBUG| - [flow2] Element: \/-(10)
2016-03-03 03:21:21,946 |DEBUG| - [flow2] Upstream finished.
2016-03-03 03:21:21,946 |DEBUG| - [flow1] Element: 10
2016-03-03 03:21:21,946 |INFO | - [sink] (10, 10)
2016-03-03 03:21:21,947 |DEBUG| - [flow1] Downstream finished.
```
---
# Final touch
```
val filter = Flow[(String, \/[Throwable, String])].filter {
case (s1, \/-(s2)) => true
case _ => false
}
[...]
example
.withAttributes(ActorAttributes.supervisionStrategy(decider))
.via(filter)
.runWith(source, sink)
._2.map(p => log.info(p.toString))
```
---
name: terminal-last
```terminal
2016-03-03 03:23:15,181 |DEBUG| - [source] Element: 1
2016-03-03 03:23:15,202 |DEBUG| - [flow2] Element: -\/(java.lang.Exception: 1 is odd)
2016-03-03 03:23:15,204 |DEBUG| - [flow1] Element: 1
2016-03-03 03:23:15,204 |DEBUG| - [source] Element: 2
2016-03-03 03:23:15,205 |DEBUG| - [flow2] Element: \/-(2)
2016-03-03 03:23:15,205 |DEBUG| - [flow1] Element: 2
2016-03-03 03:23:15,206 |INFO | - [sink] (2, 2)
2016-03-03 03:23:15,206 |DEBUG| - [source] Element: 3
2016-03-03 03:23:15,207 |DEBUG| - [flow2] Element: -\/(java.lang.Exception: 3 is odd)
2016-03-03 03:23:15,207 |DEBUG| - [flow1] Element: 3
2016-03-03 03:23:15,207 |DEBUG| - [source] Element: 4
2016-03-03 03:23:15,207 |DEBUG| - [flow2] Element: \/-(4)
2016-03-03 03:23:15,207 |DEBUG| - [flow1] Element: 4
2016-03-03 03:23:15,208 |INFO | - [sink] (4, 4)
2016-03-03 03:23:15,208 |DEBUG| - [source] Element: 5
2016-03-03 03:23:15,208 |DEBUG| - [flow2] Element: -\/(java.lang.Exception: 5 is odd)
2016-03-03 03:23:15,208 |DEBUG| - [flow1] Element: 5
2016-03-03 03:23:15,208 |DEBUG| - [source] Element: 6
2016-03-03 03:23:15,209 |DEBUG| - [flow2] Element: \/-(6)
2016-03-03 03:23:15,209 |DEBUG| - [flow1] Element: 6
2016-03-03 03:23:15,209 |INFO | - [sink] (6, 6)
2016-03-03 03:23:15,209 |DEBUG| - [source] Element: 7
2016-03-03 03:23:15,209 |DEBUG| - [flow2] Element: -\/(java.lang.Exception: 7 is odd)
2016-03-03 03:23:15,210 |DEBUG| - [flow1] Element: 7
2016-03-03 03:23:15,210 |DEBUG| - [source] Element: 8
2016-03-03 03:23:15,210 |DEBUG| - [flow2] Element: \/-(8)
2016-03-03 03:23:15,210 |DEBUG| - [flow1] Element: 8
2016-03-03 03:23:15,210 |INFO | - [sink] (8, 8)
2016-03-03 03:23:15,210 |DEBUG| - [source] Element: 9
2016-03-03 03:23:15,211 |DEBUG| - [flow2] Element: -\/(java.lang.Exception: 9 is odd)
2016-03-03 03:23:15,211 |DEBUG| - [flow1] Element: 9
2016-03-03 03:23:15,211 |DEBUG| - [source] Element: 10
2016-03-03 03:23:15,211 |DEBUG| - [source] Upstream finished.
2016-03-03 03:23:15,211 |DEBUG| - [flow2] Element: \/-(10)
2016-03-03 03:23:15,212 |DEBUG| - [flow2] Upstream finished.
2016-03-03 03:23:15,212 |DEBUG| - [flow1] Element: 10
2016-03-03 03:23:15,212 |DEBUG| - [flow1] Downstream finished.
2016-03-03 03:23:15,212 |INFO | - [sink] (10, 10)
2016-03-03 03:23:15,212 |INFO | - Done
```
---
class: center, middle
# Thank You!
### Questions?
https://github.com/rajish/akka-streams-supervision
</textarea>
<script src="https://gnab.github.io/remark/downloads/remark-latest.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script type="text/javascript">
var hljs = remark.highlighter.engine;
</script>
<script src="terminal.language.js" type="text/javascript"></script>
<script>
var slideshow = remark.create({
ratio: '16:9',
countIncrementalSlides: false,
highlightLanguage: 'scala',
highlightStyle: 'zenburn'
});
// extract the embedded styling from ansi spans
$('code.terminal > span.hljs-ansi').replaceWith(function(i, x) {
return x.replace(/<(\/?(\w+).*?)>/g, '<$1>')
});
</script>
</body>
</html>