-
Notifications
You must be signed in to change notification settings - Fork 13.6k
/
KafkaStreamsTest.java
469 lines (409 loc) · 18.5 KB
/
KafkaStreamsTest.java
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
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.kafka.streams;
import org.apache.kafka.common.Metric;
import org.apache.kafka.common.MetricName;
import org.apache.kafka.common.config.ConfigException;
import org.apache.kafka.common.metrics.Sensor;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.streams.integration.utils.EmbeddedKafkaCluster;
import org.apache.kafka.streams.integration.utils.IntegrationTestUtils;
import org.apache.kafka.streams.kstream.ForeachAction;
import org.apache.kafka.streams.kstream.KStreamBuilder;
import org.apache.kafka.streams.processor.StreamPartitioner;
import org.apache.kafka.streams.processor.internals.GlobalStreamThread;
import org.apache.kafka.streams.processor.internals.StreamThread;
import org.apache.kafka.test.IntegrationTest;
import org.apache.kafka.test.MockMetricsReporter;
import org.apache.kafka.test.TestCondition;
import org.apache.kafka.test.TestUtils;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
import java.io.File;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
@Category({IntegrationTest.class})
public class KafkaStreamsTest {
private static final int NUM_BROKERS = 1;
private static final int NUM_THREADS = 2;
// We need this to avoid the KafkaConsumer hanging on poll (this may occur if the test doesn't complete
// quick enough)
@ClassRule
public static final EmbeddedKafkaCluster CLUSTER = new EmbeddedKafkaCluster(NUM_BROKERS);
private final KStreamBuilder builder = new KStreamBuilder();
private KafkaStreams streams;
private Properties props;
@Before
public void before() {
props = new Properties();
props.setProperty(StreamsConfig.APPLICATION_ID_CONFIG, "appId");
props.setProperty(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, CLUSTER.bootstrapServers());
props.setProperty(StreamsConfig.METRIC_REPORTER_CLASSES_CONFIG, MockMetricsReporter.class.getName());
props.setProperty(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getPath());
props.put(StreamsConfig.NUM_STREAM_THREADS_CONFIG, NUM_THREADS);
streams = new KafkaStreams(builder, props);
}
@Test
public void testStateChanges() throws Exception {
StateListenerStub stateListener = new StateListenerStub();
streams.setStateListener(stateListener);
Assert.assertEquals(streams.state(), KafkaStreams.State.CREATED);
Assert.assertEquals(stateListener.numChanges, 0);
streams.start();
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
return streams.state() == KafkaStreams.State.RUNNING;
}
}, 10 * 1000, "Streams never started.");
streams.close();
Assert.assertEquals(streams.state(), KafkaStreams.State.NOT_RUNNING);
}
@Test
public void testStateCloseAfterCreate() throws Exception {
StateListenerStub stateListener = new StateListenerStub();
streams.setStateListener(stateListener);
streams.close();
Assert.assertEquals(streams.state(), KafkaStreams.State.NOT_RUNNING);
}
private void testStateThreadCloseHelper(final int numThreads) throws Exception {
final java.lang.reflect.Field threadsField = streams.getClass().getDeclaredField("threads");
threadsField.setAccessible(true);
final StreamThread[] threads = (StreamThread[]) threadsField.get(streams);
assertEquals(numThreads, threads.length);
assertEquals(streams.state(), KafkaStreams.State.CREATED);
streams.start();
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
return streams.state() == KafkaStreams.State.RUNNING;
}
}, 10 * 1000, "Streams never started.");
for (int i = 0; i < numThreads; i++) {
final StreamThread tmpThread = threads[i];
tmpThread.close();
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
return tmpThread.state() == StreamThread.State.DEAD;
}
}, 10 * 1000, "Thread never stopped.");
threads[i].join();
}
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
return streams.state() == KafkaStreams.State.ERROR;
}
}, 10 * 1000, "Streams never stopped.");
streams.close();
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
return streams.state() == KafkaStreams.State.NOT_RUNNING;
}
}, 10 * 1000, "Streams never stopped.");
final java.lang.reflect.Field globalThreadField = streams.getClass().getDeclaredField("globalStreamThread");
globalThreadField.setAccessible(true);
GlobalStreamThread globalStreamThread = (GlobalStreamThread) globalThreadField.get(streams);
assertEquals(globalStreamThread, null);
}
@Test
public void testStateThreadClose() throws Exception {
// make sure we have the global state thread running too
builder.globalTable("anyTopic");
streams = new KafkaStreams(new KStreamBuilder(), props);
testStateThreadCloseHelper(NUM_THREADS);
}
@Test
public void testStateGlobalThreadClose() throws Exception {
final KStreamBuilder builder = new KStreamBuilder();
// make sure we have the global state thread running too
builder.globalTable("anyTopic");
streams = new KafkaStreams(builder, props);
streams.start();
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
return streams.state() == KafkaStreams.State.RUNNING;
}
}, 10 * 1000, "Streams never started.");
final java.lang.reflect.Field globalThreadField = streams.getClass().getDeclaredField("globalStreamThread");
globalThreadField.setAccessible(true);
final GlobalStreamThread globalStreamThread = (GlobalStreamThread) globalThreadField.get(streams);
globalStreamThread.close();
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
return globalStreamThread.state() == GlobalStreamThread.State.DEAD;
}
}, 10 * 1000, "Thread never stopped.");
globalStreamThread.join();
assertEquals(streams.state(), KafkaStreams.State.ERROR);
streams.close();
assertEquals(streams.state(), KafkaStreams.State.NOT_RUNNING);
}
@Test
public void testInitializesAndDestroysMetricsReporters() throws Exception {
final int oldInitCount = MockMetricsReporter.INIT_COUNT.get();
final KStreamBuilder builder = new KStreamBuilder();
final KafkaStreams streams = new KafkaStreams(builder, props);
final int newInitCount = MockMetricsReporter.INIT_COUNT.get();
final int initDiff = newInitCount - oldInitCount;
assertTrue("some reporters should be initialized by calling on construction", initDiff > 0);
streams.start();
final int oldCloseCount = MockMetricsReporter.CLOSE_COUNT.get();
streams.close();
assertEquals(oldCloseCount + initDiff, MockMetricsReporter.CLOSE_COUNT.get());
}
@Test
public void testCloseIsIdempotent() throws Exception {
streams.close();
final int closeCount = MockMetricsReporter.CLOSE_COUNT.get();
streams.close();
Assert.assertEquals("subsequent close() calls should do nothing",
closeCount, MockMetricsReporter.CLOSE_COUNT.get());
}
@Test
public void testCannotStartOnceClosed() throws Exception {
streams.start();
streams.close();
try {
streams.start();
fail("Should have thrown IllegalStateException.");
} catch (final IllegalStateException expected) {
// ignore
} finally {
streams.close();
}
}
@Test
public void testCannotStartTwice() throws Exception {
streams.start();
try {
streams.start();
fail("Should have thrown IllegalStateException.");
} catch (final IllegalStateException expected) {
// ignore
} finally {
streams.close();
}
}
@Test
public void testNumberDefaultMetrics() {
props.put(StreamsConfig.NUM_STREAM_THREADS_CONFIG, 1);
streams = new KafkaStreams(builder, props);
final Map<MetricName, ? extends Metric> metrics = streams.metrics();
// all 15 default StreamThread metrics + 1 metric that keeps track of number of metrics
assertEquals(metrics.size(), 16);
}
@Test
public void testIllegalMetricsConfig() {
props.setProperty(StreamsConfig.METRICS_RECORDING_LEVEL_CONFIG, "illegalConfig");
try {
new KafkaStreams(builder, props);
fail("Should have thrown ConfigException.");
} catch (final ConfigException expected) { /* ignore */ }
}
@Test
public void testLegalMetricsConfig() {
props.setProperty(StreamsConfig.METRICS_RECORDING_LEVEL_CONFIG, Sensor.RecordingLevel.INFO.toString());
final KafkaStreams streams1 = new KafkaStreams(builder, props);
streams1.close();
props.setProperty(StreamsConfig.METRICS_RECORDING_LEVEL_CONFIG, Sensor.RecordingLevel.DEBUG.toString());
new KafkaStreams(builder, props);
}
@Test(expected = IllegalStateException.class)
public void shouldNotGetAllTasksWhenNotRunning() throws Exception {
streams.allMetadata();
}
@Test(expected = IllegalStateException.class)
public void shouldNotGetAllTasksWithStoreWhenNotRunning() throws Exception {
streams.allMetadataForStore("store");
}
@Test(expected = IllegalStateException.class)
public void shouldNotGetTaskWithKeyAndSerializerWhenNotRunning() throws Exception {
streams.metadataForKey("store", "key", Serdes.String().serializer());
}
@Test(expected = IllegalStateException.class)
public void shouldNotGetTaskWithKeyAndPartitionerWhenNotRunning() throws Exception {
streams.metadataForKey("store", "key", new StreamPartitioner<String, Object>() {
@Override
public Integer partition(final String key, final Object value, final int numPartitions) {
return 0;
}
});
}
@Test
public void shouldReturnFalseOnCloseWhenThreadsHaventTerminated() throws Exception {
final AtomicBoolean keepRunning = new AtomicBoolean(true);
try {
final KStreamBuilder builder = new KStreamBuilder();
final CountDownLatch latch = new CountDownLatch(1);
final String topic = "input";
CLUSTER.createTopic(topic);
builder.stream(Serdes.String(), Serdes.String(), topic)
.foreach(new ForeachAction<String, String>() {
@Override
public void apply(final String key, final String value) {
try {
latch.countDown();
while (keepRunning.get()) {
Thread.sleep(10);
}
} catch (InterruptedException e) {
// no-op
}
}
});
streams = new KafkaStreams(builder, props);
streams.start();
IntegrationTestUtils.produceKeyValuesSynchronouslyWithTimestamp(topic,
Collections.singletonList(new KeyValue<>("A", "A")),
TestUtils.producerConfig(
CLUSTER.bootstrapServers(),
StringSerializer.class,
StringSerializer.class,
new Properties()),
System.currentTimeMillis());
assertTrue("Timed out waiting to receive single message", latch.await(30, TimeUnit.SECONDS));
assertFalse(streams.close(10, TimeUnit.MILLISECONDS));
} finally {
// stop the thread so we don't interfere with other tests etc
keepRunning.set(false);
if (streams != null) {
streams.close();
}
}
}
@Test
public void testCleanup() throws Exception {
streams.cleanUp();
streams.start();
streams.close();
streams.cleanUp();
}
@Test
public void testCannotCleanupWhileRunning() throws Exception {
streams.start();
try {
streams.cleanUp();
fail("Should have thrown IllegalStateException.");
} catch (final IllegalStateException expected) {
// ignore
} finally {
streams.close();
}
}
@Test
public void testToString() {
streams.start();
String streamString = streams.toString();
streams.close();
String appId = streamString.split("\\n")[1].split(":")[1].trim();
Assert.assertNotEquals("streamString should not be empty", "", streamString);
Assert.assertNotNull("streamString should not be null", streamString);
Assert.assertNotEquals("streamString contains non-empty appId", "", appId);
Assert.assertNotNull("streamString contains non-null appId", appId);
}
@Test
public void shouldCleanupOldStateDirs() throws InterruptedException {
props.setProperty(StreamsConfig.STATE_CLEANUP_DELAY_MS_CONFIG, "1");
final String topic = "topic";
CLUSTER.createTopic(topic);
final KStreamBuilder builder = new KStreamBuilder();
builder.table(Serdes.String(), Serdes.String(), topic, topic);
streams = new KafkaStreams(builder, props);
final CountDownLatch latch = new CountDownLatch(1);
streams.setStateListener(new KafkaStreams.StateListener() {
@Override
public void onChange(final KafkaStreams.State newState, final KafkaStreams.State oldState) {
if (newState == KafkaStreams.State.RUNNING && oldState == KafkaStreams.State.REBALANCING) {
latch.countDown();
}
}
});
final String appDir = props.getProperty(StreamsConfig.STATE_DIR_CONFIG) + File.separator + props.getProperty(StreamsConfig.APPLICATION_ID_CONFIG);
final File oldTaskDir = new File(appDir, "10_1");
assertTrue(oldTaskDir.mkdirs());
try {
streams.start();
latch.await(30, TimeUnit.SECONDS);
verifyCleanupStateDir(appDir, oldTaskDir);
assertTrue(oldTaskDir.mkdirs());
verifyCleanupStateDir(appDir, oldTaskDir);
} finally {
streams.close();
}
}
private void verifyCleanupStateDir(final String appDir, final File oldTaskDir) throws InterruptedException {
final File taskDir = new File(appDir, "0_0");
TestUtils.waitForCondition(new TestCondition() {
@Override
public boolean conditionMet() {
return !oldTaskDir.exists() && taskDir.exists();
}
}, 30000, "cleanup has not successfully run");
assertTrue(taskDir.exists());
}
public static class StateListenerStub implements KafkaStreams.StateListener {
public int numChanges = 0;
public KafkaStreams.State oldState;
public KafkaStreams.State newState;
public Map<KafkaStreams.State, Long> mapStates = new HashMap<>();
private final boolean closeOnChange;
private final KafkaStreams streams;
public StateListenerStub() {
this.closeOnChange = false;
this.streams = null;
}
/**
* For testing only, we might want to test closing streams on a transition change
* @param closeOnChange
* @param streams
*/
public StateListenerStub(final boolean closeOnChange, final KafkaStreams streams) {
this.closeOnChange = closeOnChange;
this.streams = streams;
}
@Override
public void onChange(final KafkaStreams.State newState, final KafkaStreams.State oldState) {
long prevCount = this.mapStates.containsKey(newState) ? this.mapStates.get(newState) : 0;
this.numChanges++;
this.oldState = oldState;
this.newState = newState;
this.mapStates.put(newState, prevCount + 1);
if (this.closeOnChange &&
(newState == KafkaStreams.State.NOT_RUNNING || newState == KafkaStreams.State.ERROR)) {
streams.close();
}
}
}
}