forked from jaceklaskowski/kafka-workshop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkafka-streams-internals.html
476 lines (415 loc) · 19.9 KB
/
kafka-streams-internals.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
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">
<title>Kafka Streams Workshop | Internals of Kafka Streams</title>
<meta name="description" content="Kafka Streams Workshop | Deep Dive Into / Internals of Kafka Streams">
<meta name="author" content="Jacek Laskowski">
<link rel="stylesheet" href="reveal.js/css/reveal.css">
<link rel="stylesheet" href="reveal.js/css/theme/beige.css" id="theme">
<!-- Theme used for syntax highlighting of code -->
<link rel="stylesheet" href="reveal.js/lib/css/zenburn.css">
<!-- Jacek: custom formatting -->
<link rel="stylesheet" href="revealjs-css/jacek.css">
<!-- Printing and PDF exports -->
<script>
var link = document.createElement('link');
link.rel = 'stylesheet';
link.type = 'text/css';
link.href = window.location.search.match(/print-pdf/gi) ? 'reveal.js/css/print/pdf.css' : 'reveal.js/css/print/paper.css';
document.getElementsByTagName('head')[0].appendChild(link);
</script>
</head>
<body>
<div class="reveal">
<div class="footer">
<footer style="font-size: small;">
© <a href="https://medium.com/@jaceklaskowski">Jacek Laskowski</a> 2019 / <a href="https://twitter.com/jaceklaskowski">@JacekLaskowski</a> / [email protected]
</footer>
</div>
<div class="slides">
<section class="intro" data-transition="zoom" id="home">
<p>
<img width="15%" style="background:none; border:none; box-shadow:none;" data-src="images/kafka-logo.png">
<img width="7%" src="images/jacek_laskowski_20141201_512px.png" style="border: 0">
</p>
<h1 style="font-size: 2.44em;">Deep Dive Into / Internals of</h1>
<h1>Kafka Streams</h1>
<h3>Kafka Streams 2.2.0</h3>
<h4 style="font-size: smaller;">
<a href="https://twitter.com/jaceklaskowski">@jaceklaskowski</a> / <a href="http://stackoverflow.com/users/1305344/jacek-laskowski">StackOverflow</a> / <a href="https://github.com/jaceklaskowski">GitHub</a>
<br>
The "Internals" Books: <a href="https://bit.ly/kafka-streams-internals">Kafka Streams</a> / <a href="https://bit.ly/apache-kafka-internals">Apache Kafka</a>
</h4>
</section>
<section id="kafkasummit" data-markdown>
<textarea data-template>
## My Very First #KafkaSummit!
<a href="https://twitter.com/rmoff/status/1128221572974764032">
<img
width="75%"
style="background:none; border:none; box-shadow:none;"
data-src="images/kafkasummit2019-robin-jakub.jpeg">
</a>
</textarea>
</section>
<section>
<section id="speaker" style="font-size: 90%" data-markdown>
<textarea data-template>
<p><img width="12%" src="images/jacek_laskowski_20141201_512px.png" style="border: 0"></p>
* **Jacek Laskowski** is a freelance IT consultant
* Core competencies in Spark, Kafka, **Kafka Streams**, Scala
* Development | Consulting | Training
* Among contributors to <a href="https://github.com/apache/spark/graphs/contributors">Apache Spark</a>
* Contact me at **[email protected]**
* Follow [@JacekLaskowski](https://twitter.com/jaceklaskowski) on twitter <br>for more #ApacheSpark, #ApacheKafka, #KafkaStreams
</textarea>
</section>
<section id="gitbooks">
<p><img width="12%" src="images/jacek_laskowski_20141201_512px.png" style="border: 0"></p>
<div style="text-align: center">
<p>Jacek is best known by the online "Internals" books:</p>
<p>
<ol>
<li><a href="https://bit.ly/apache-spark-internals">The Internals of Apache Spark</a></li>
<li><a href="https://bit.ly/spark-sql-internals">The Internals of Spark SQL</a></li>
<li><a href="https://bit.ly/spark-structured-streaming">The Internals of Spark Structured Streaming</a></li>
<li><a href="https://bit.ly/kafka-streams-internals">The Internals of Kafka Streams</a></li>
<li><a href="https://bit.ly/apache-kafka-internals">The Internals of Apache Kafka</a></li>
</ol>
</p>
</div>
</section>
<section id="stackoverflow">
<div style="text-align: center">
<p>
Jacek is "active" on <a href="http://stackoverflow.com/users/1305344/jacek-laskowski">StackOverflow</a> 🥳
</p>
<p>
<a href="https://stackoverflow.com/tags/apache-kafka-streams/topusers"><img width="38%" src="images/jaceklaskowski-stackoverflow-apache-kafka-streams.png" style="border: 0"></a>
</p>
</div>
</section>
</section>
<section id="agenda" style="font-size: 90%" data-markdown>
<textarea data-template>
## Agenda
1. [Kafka Streams](#/intro)
1. [Main Development Entities](#/main-development-entities)
1. [Topology](#/topology)
1. [KafkaStreams](#/kafkastreams)
1. [Main Execution Entities](#/main-execution-entities)
1. [StreamThread](#/StreamThread)
1. [TaskManager](#/TaskManager)
1. [StreamTask and StandbyTask](#/kafka-streams-tasks)
1. [StreamsPartitionAssignor](#/StreamsPartitionAssignor)
1. [RebalanceListener](#/RebalanceListener)
</textarea>
</section>
<section>
<section id="intro" data-markdown style="font-size: 90%">
<textarea data-template>
## Kafka Streams <small>(1 of 2)</small>
1. **Kafka Streams** is a client library for **stream processing applications** that process records in Kafka topics
* Low-level **Processor API**
1. Stream processing primitives, e.g. **KStream** and **KTable**
* High-level **Streams DSL**
1. **Topology** to describe the processing flow
* » One record at a time «
1. Wrapper around the Kafka Producer and Consumer APIs
1. Supports **fault-tolerant local state** for stateful operations (e.g. windowed aggregations and joins)
</textarea>
</section>
<section data-markdown style="font-size: 80%">
<textarea data-template>
## Kafka Streams <small>(2 of 2)</small>
1. "Groundbreaking" facts (which changed my life):
1. When started, a topology processes one record at a time only
1. A Kafka Streams application can be run in multiple instances (e.g. as Docker containers)
1. Increasing stream processing power is to increase number of threads and instances
1. Processor itself decides whether to forward a record downstream or not
</textarea>
</section>
</section>
<section id="main-development-entities" data-markdown>
<textarea data-template>
## Main Development Entities
1. As a Kafka Streams developer you work with the two main developer-facing entities:
1. Topology
1. KafkaStreams
</textarea>
</section>
<section>
<section id="topology" data-markdown>
<textarea data-template>
## Topology
1. Represents the **stream processing logic** of a Kafka Streams application
1. Directed Acyclic Graph of **Processors** (Stream Processing Nodes)
1. Logical representation
1. Created directly or indirectly (using Streams DSL)
1. Topology API
* Adding sources, processors, sinks, state stores
1. Can be described (and printed out to stdout)
</textarea>
</section>
<section id="topology-example-creating-topology" data-markdown>
<textarea data-template>
## Example: Creating Topology
```
// Creating directly
import org.apache.kafka.streams.Topology
val topology = new Topology
```
```
// Created using Streams DSL (StreamsBuilder API)
// Scala API for Kafka Streams
import org.apache.kafka.streams.scala._
import ImplicitConversions._
import Serdes._
val builder = new StreamsBuilder
val topology = builder.build
```
</textarea>
</section>
<section id="topology-example-describing-topology" data-markdown>
<textarea data-template>
## Example: Describing Topology
```
scala> println(topology.describe)
Topologies:
Sub-topology: 0 for global store (will not generate tasks)
Source: demo-source-processor (topics: [demo-topic])
--> demo-processor-supplier
Processor: demo-processor-supplier (stores: [in-memory-key-value-store])
--> none
<-- demo-source-processor
```
</textarea>
</section>
</section>
<section>
<section id="kafkastreams" data-markdown style="font-size: 90%">
<textarea data-template>
## KafkaStreams
1. Manages execution of a topology of a Kafka Streams application
* Start, close (shut down), state
1. Consumes messages from and produces processing results to Kafka topics
1. Acceptable to create multiple KafkaStreams instances per Kafka Streams application
1. For better performance, consider creating multiple KafkaStreams instances as separate instances of Kafka Streams application
</textarea>
</section>
<section id="kafkastreams-example" data-markdown>
<textarea data-template>
## Example: Creating KafkaStreams
```scala
import org.apache.kafka.streams.KafkaStreams
val topology: Topology = ...
val config: StreamsConfig = ...
val ks = new KafkaStreams(topology, config)
ks.start // <-- starts execution
```
</textarea>
</section>
</section>
<section id="main-execution-entities" data-markdown>
<textarea data-template>
## Main Execution Entities
1. [StreamThread](#/StreamThread)
1. [TaskManager](#/TaskManager)
1. [StreamTask and StandbyTask](#/kafka-streams-tasks)
1. [StreamsPartitionAssignor](#/StreamsPartitionAssignor)
1. [RebalanceListener](#/RebalanceListener)
</textarea>
</section>
<section>
<section id="StreamThread" data-markdown style="font-size: 85%;">
<textarea data-template>
## StreamThread <small>(1 of 4)</small>
1. Stream Processor Thread
* Runs the **main record processing loop**
* Thread of execution (**java.lang.Thread**)
1. StreamThread uses a Kafka consumer (to poll for records)
* Subscribes to source topics
* Think of Consumer Group
1. **num.stream.threads** configuration property (default: **1**)
1. Uses Kafka Consumer "tools" for operation
* Registers **StreamsPartitionAssignor**
* Uses **RebalanceListener** to intercept changes to the partitions assigned
1. Uses TaskManager to manage processing tasks <small><i>(on next slide)</i></small>
</textarea>
</section>
<section id="StreamThread-2">
<h2>StreamThread <small>(2 of 4)</small></h2>
<img
width="40%"
style="background:none; border:none; box-shadow:none;"
data-src="images/kafka-streams-StreamThread.png">
</section>
<section id="StreamThread-3">
<h2>StreamThread <small>(3 of 4)</small></h2>
<img
style="background:none; border:none; box-shadow:none;"
data-src="images/kafka-streams-StreamThread-runLoop.png">
</section>
<section id="StreamThread-4">
<h2>StreamThread <small>(4 of 4)</small></h2>
<img
style="background:none; border:none; box-shadow:none;"
data-src="images/kafka-streams-StreamThread-runOnce.png">
</section>
</section>
<section>
<section id="TaskManager" data-markdown>
<textarea data-template>
## TaskManager <small>(1 of 2)</small>
1. Task manager of a StreamThread
1. Manages active and standby tasks
* Only active StreamTasks process records
1. Creates processor tasks for assigned partitions
* **RebalanceListener.onPartitionsAssigned**
</textarea>
</section>
<section id="TaskManager-2">
<h2>TaskManager <small>(2 of 2)</small></h2>
<img
width="75%"
style="background:none; border:none; box-shadow:none;"
data-src="images/kafka-streams-TaskManager.png">
</section>
</section>
<section>
<section id="kafka-streams-tasks" data-markdown style="font-size: 90%;">
<textarea data-template>
## Stream Processor Tasks <small>(1 of 3)</small>
1. Managed by **TaskManager** to run a topology of stream processors
1. **StreamTask** - the default processor task
* As many as partitions assigned
* Managed as a group as **AssignedStreamsTasks**
* Only active StreamTasks process records
* Use FIFO RecordQueues (per Kafka TopicPartition)
1. **StandbyTask** - a backup processor task
* "Ghost" tasks for active StreamTasks
* Default: **0** standby tasks
* Managed as a group as **AssignedStandbyTasks**
</textarea>
</section>
<section id="kafka-streams-tasks-2">
<h2>Stream Processor Tasks <small>(2 of 3)</small></h2>
<img
style="background:none; border:none; box-shadow:none;"
data-src="images/kafka-streams-StreamTask-process.png">
</section>
<section id="kafka-streams-tasks-3">
<h2>Stream Processor Tasks <small>(3 of 3)</small></h2>
<img
style="background:none; border:none; box-shadow:none;"
data-src="images/kafka-streams-logs-current-tasks.png">
</section>
</section>
<section>
<section id="StreamsPartitionAssignor" data-markdown style="font-size: 75%;">
<textarea data-template>
## StreamsPartitionAssignor <small>(1 of 2)</small>
1. Custom **PartitionAssignor** from the Kafka Consumer API
* Used for dynamic partition assignment and distributing partition ownership across the members
of a consumer group
* **partition.assignment.strategy** / **ConsumerConfig.PARTITION_ASSIGNMENT_STRATEGY_CONFIG** configuration property
1. Group management protocol
* **group membership**
* **state synchronization**
1. Assigns partitions dynamically across the instances of a Kafka Streams application
* Required **application.id** / **StreamsConfig.APPLICATION_ID_CONFIG** configuration property
</textarea>
</section>
<section id="StreamsPartitionAssignor-2" data-markdown>
<textarea data-template>
## StreamsPartitionAssignor <small>(2 of 2)</small>
<img style="background:none; border:none; box-shadow:none;" data-src="images/kafka-streams-StreamsPartitionAssignor-onAssignment.png">
</textarea>
</section>
</section>
<section>
<section id="RebalanceListener" data-markdown>
<textarea data-template>
## RebalanceListener <small>(1 of 2)</small>
1. Custom **ConsumerRebalanceListener** from the Kafka Consumer API
* Callback interface for custom actions when the set of partitions assigned to the
consumer changes
* partition re-assignment will be triggered any time the members of the consumer group change
1. Intercepts changes to the partitions assigned to a single StreamThread
</textarea>
</section>
<section id="RebalanceListener-2">
<h2>RebalanceListener <small>(2 of 2)</small></h2>
<img
style="background:none; border:none; box-shadow:none;"
data-src="images/kafka-streams-RebalanceListener.png">
</section>
</section>
<section id="recap" data-markdown>
<textarea data-template>
## Recap
1. [Kafka Streams](#/intro)
1. [Main Development Entities](#/main-development-entities)
1. [Topology](#/topology)
1. [KafkaStreams](#/kafkastreams)
1. [Main Execution Entities](#/main-execution-entities)
1. [StreamThread](#/StreamThread)
1. [TaskManager](#/TaskManager)
1. [StreamTask and StandbyTask](#/kafka-streams-tasks)
1. [StreamsPartitionAssignor](#/StreamsPartitionAssignor)
1. [RebalanceListener](#/RebalanceListener)
</textarea>
</section>
<section style="text-align: left" data-markdown id="questions">
<textarea data-template>
# Questions?
* Read [The Internals of Kafka Streams](http://bit.ly/kafka-streams-internals)
* Read [The Internals of Apache Kafka](http://bit.ly/apache-kafka-internals)
* Follow [@jaceklaskowski](https://twitter.com/jaceklaskowski) on twitter (DMs open)
* Upvote [my questions and answers on StackOverflow](http://stackoverflow.com/users/1305344/jacek-laskowski)
* Contact me at **[email protected]**
</textarea>
</section>
</div>
</div>
<script src="reveal.js/lib/js/head.min.js"></script>
<script src="reveal.js/js/reveal.js"></script>
<script>
// More info about config & dependencies:
// - https://github.com/hakimel/reveal.js#configuration
// - https://github.com/hakimel/reveal.js#dependencies
Reveal.initialize({
controls: true,
progress: true,
history: true,
center: true,
slideNumber: true,
transition: 'slide', // none/fade/slide/convex/concave/zoom
menu: {
markers: true,
openSlideNumber: true
},
dependencies: [
{ src: 'reveal.js/lib/js/classList.js', condition: function () { return !document.body.classList; } },
{ src: 'reveal.js/plugin/markdown/marked.js' },
{ src: 'reveal.js/plugin/markdown/markdown.js' },
{ src: 'reveal.js/plugin/zoom-js/zoom.js', async: true },
{ src: 'reveal.js/plugin/notes/notes.js', async: true },
{ src: 'reveal.js/plugin/highlight/highlight.js', async: true, callback: function () { hljs.initHighlightingOnLoad(); } }
]
});
</script>
<script>
(function (i, s, o, g, r, a, m) {
i['GoogleAnalyticsObject'] = r; i[r] = i[r] || function () {
(i[r].q = i[r].q || []).push(arguments)
}, i[r].l = 1 * new Date(); a = s.createElement(o),
m = s.getElementsByTagName(o)[0]; a.async = 1; a.src = g; m.parentNode.insertBefore(a, m)
})(window, document, 'script', '//www.google-analytics.com/analytics.js', 'ga');
ga('create', 'UA-45999426-3', 'auto');
ga('send', 'pageview');
</script>
</body>
</html>