-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
CustomWindowTest.java
311 lines (285 loc) · 14.7 KB
/
CustomWindowTest.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
/*
* Copyright Confluent Inc.
*
* Licensed 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 io.confluent.examples.streams.window;
import org.apache.kafka.common.serialization.IntegerDeserializer;
import org.apache.kafka.common.serialization.IntegerSerializer;
import org.apache.kafka.common.serialization.Serdes;
import org.apache.kafka.common.serialization.StringSerializer;
import org.apache.kafka.common.utils.Bytes;
import org.apache.kafka.streams.KeyValue;
import org.apache.kafka.streams.StreamsBuilder;
import org.apache.kafka.streams.StreamsConfig;
import org.apache.kafka.streams.TestInputTopic;
import org.apache.kafka.streams.TestOutputTopic;
import org.apache.kafka.streams.Topology;
import org.apache.kafka.streams.TopologyTestDriver;
import org.apache.kafka.streams.kstream.KStream;
import org.apache.kafka.streams.kstream.KTable;
import org.apache.kafka.streams.kstream.Materialized;
import org.apache.kafka.streams.kstream.Printed;
import org.apache.kafka.streams.kstream.Produced;
import org.apache.kafka.streams.kstream.Suppressed;
import org.apache.kafka.streams.kstream.TimeWindowedDeserializer;
import org.apache.kafka.streams.kstream.Windowed;
import org.apache.kafka.streams.kstream.WindowedSerdes;
import org.apache.kafka.streams.kstream.internals.TimeWindow;
import org.apache.kafka.streams.state.WindowStore;
import org.apache.kafka.streams.test.TestRecord;
import org.apache.kafka.test.TestUtils;
import org.junit.Test;
import java.time.Duration;
import java.time.ZoneId;
import java.time.ZoneOffset;
import java.time.ZonedDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Properties;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.core.IsEqual.equalTo;
public class CustomWindowTest {
private static final String inputTopic = "inputTopic";
private static final String outputTopic = "outputTopic";
private static final ZoneId zone = ZoneOffset.UTC;
private static final int windowStartHour = 18;
@Test
public void shouldSumNumbersOnSameDay() {
final List<TestRecord<String, Integer>> inputValues = Arrays.asList(
new TestRecord<>(null,
1,
ZonedDateTime.of(2019, 1, 1, 16, 29, 0, 0, zone).toInstant()),
new TestRecord<>(null,
2,
ZonedDateTime.of(2019, 1, 1, 16, 30, 0, 0, zone).toInstant()),
new TestRecord<>(null,
7,
ZonedDateTime.of(2019, 1, 1, 16, 31, 0, 0, zone).toInstant()),
dummyEventToForceSuppression()
);
final List<KeyValue<Windowed<Integer>, Integer>> expectedValues =
Collections.singletonList(KeyValue.pair(toWindowed(1,
ZonedDateTime.of(2018, 12, 31, 18, 0, 0, 0, zone),
ZonedDateTime.of(2019, 1, 1, 18, 0, 0, 0, zone)),
10)
);
verify(inputValues, expectedValues, zone);
}
@Test
public void shouldSumNumbersWithTwoWindows() {
final List<TestRecord<String, Integer>> inputValues = Arrays.asList(
new TestRecord<>(null,
1,
ZonedDateTime.of(2019, 1, 1, 16, 29, 0, 0, zone).toInstant()),
new TestRecord<>(null,
2,
ZonedDateTime.of(2019, 1, 1, 16, 30, 0, 0, zone).toInstant()),
new TestRecord<>(null,
7,
ZonedDateTime.of(2019, 1, 1, 18, 31, 0, 0, zone).toInstant()),
dummyEventToForceSuppression()
);
final List<KeyValue<Windowed<Integer>, Integer>> expectedValues = Arrays.asList(
KeyValue.pair(toWindowed(1,
ZonedDateTime.of(2018, 12, 31, 18, 0, 0, 0, zone),
ZonedDateTime.of(2019, 1, 1, 18, 0, 0, 0, zone)),
3),
KeyValue.pair(toWindowed(1,
ZonedDateTime.of(2019, 1, 1, 18, 0, 0, 0, zone),
ZonedDateTime.of(2019, 1, 2, 18, 0, 0, 0, zone)),
7)
);
verify(inputValues, expectedValues, zone);
}
@Test
public void shouldSumNumbersWithTwoWindowsAndLateArrival() {
final List<TestRecord<String, Integer>> inputValues = Arrays.asList(
new TestRecord<>(null,
1,
ZonedDateTime.of(2019, 1, 1, 16, 29, 0, 0, zone).toInstant()),
new TestRecord<>(null,
2,
ZonedDateTime.of(2019, 1, 1, 16, 30, 0, 0, zone).toInstant()),
new TestRecord<>(null,
2,
ZonedDateTime.of(2019, 1, 1, 18, 1, 0, 0, zone).toInstant()),
//Out-of-order arrival message
new TestRecord<>(null,
7, ZonedDateTime.of(2019, 1, 1, 16, 31, 0, 0, zone).toInstant()),
new TestRecord<>(null,
40,
ZonedDateTime.of(2019, 1, 1, 18, 31, 0, 0, zone).toInstant()),
//this late arrival event should be ignored as it happens after a message that was outside of grace period 18h (end of window) + 30min (grace period)
new TestRecord<>(null,
42,
ZonedDateTime.of(2019, 1, 1, 16, 35, 0, 0, zone).toInstant()),
dummyEventToForceSuppression()
);
final List<KeyValue<Windowed<Integer>, Integer>> expectedValues = Arrays.asList(
KeyValue.pair(toWindowed(1,
ZonedDateTime.of(2018, 12, 31, 18, 0, 0, 0, zone),
ZonedDateTime.of(2019, 1, 1, 18, 0, 0, 0, zone)),
10),
KeyValue.pair(toWindowed(1,
ZonedDateTime.of(2019, 1, 1, 18, 0, 0, 0, zone),
ZonedDateTime.of(2019, 1, 2, 18, 0, 0, 0, zone)),
42)
);
verify(inputValues, expectedValues, zone);
}
// Daylight savings time tests
@Test
public void shouldSumNumbersWithTwoWindowsAndNoDSTTimezone() {
final List<TestRecord<String, Integer>> inputValues = Arrays.asList(
new TestRecord<>(null,
1,
ZonedDateTime.of(2019, 3, 30, 1, 39, 0, 0, zone).toInstant()),
new TestRecord<>(null,
2,
ZonedDateTime.of(2019, 3, 30, 2, 0, 0, 0, zone).toInstant()),
new TestRecord<>(null,
7,
ZonedDateTime.of(2019, 3, 30, 2, 10, 0, 0, zone).toInstant()),
new TestRecord<>(null,
1,
ZonedDateTime.of(2019, 3, 31, 1, 39, 0, 0, zone).toInstant()),
new TestRecord<>(null,
2,
ZonedDateTime.of(2019, 3, 31, 2, 0, 0, 0, zone).toInstant()),
new TestRecord<>(null,
7,
ZonedDateTime.of(2019, 3, 31, 2, 10, 0, 0, zone).toInstant()),
dummyEventToForceSuppression()
);
final List<KeyValue<Windowed<Integer>, Integer>> expectedValues = Arrays.asList(
KeyValue.pair(toWindowed(1,
ZonedDateTime.of(2019, 3, 29, 18, 0, 0, 0, zone),
ZonedDateTime.of(2019, 3, 30, 18, 0, 0, 0, zone)),
10),
KeyValue.pair(toWindowed(1,
ZonedDateTime.of(2019, 3, 30, 18, 0, 0, 0, zone),
ZonedDateTime.of(2019, 3, 31, 18, 0, 0, 0, zone)),
10)
);
verify(inputValues, expectedValues, zone);
}
@Test
public void shouldSumNumbersWithTwoWindowsAndDSTTimezone() {
//This test illustrate problems with daylight savings
//Some timezone have daylight savings time (DST) resulting in two days in year that have either 23 or 25 hours.
//Kafka streams currently support only fixed period for the moment.
final ZoneId zoneWithDST = ZoneId.of("Europe/Paris");
final List<TestRecord<String, Integer>> inputValues = Arrays.asList(
new TestRecord<>(null,
1,
ZonedDateTime.of(2019, 3, 30, 1, 39, 0, 0, zoneWithDST).toInstant()),
new TestRecord<>(null,
2,
ZonedDateTime.of(2019, 3, 30, 2, 0, 0, 0, zoneWithDST).toInstant()),
new TestRecord<>(null,
7,
ZonedDateTime.of(2019, 3, 30, 2, 10, 0, 0, zoneWithDST).toInstant()),
new TestRecord<>(null,
1,
ZonedDateTime.of(2019, 3, 31, 1, 39, 0, 0, zoneWithDST).toInstant()),
new TestRecord<>(null,
2,
ZonedDateTime.of(2019, 3, 31, 2, 0, 0, 0, zoneWithDST).toInstant()),
new TestRecord<>(null,
7,
ZonedDateTime.of(2019, 3, 31, 2, 10, 0, 0, zoneWithDST).toInstant()),
dummyEventToForceSuppression()
);
final List<KeyValue<Windowed<Integer>, Integer>> expectedValues = Arrays.asList(
KeyValue.pair(toWindowed(1,
ZonedDateTime.of(2019, 3, 29, 18, 0, 0, 0, zoneWithDST),
ZonedDateTime.of(2019, 3, 30, 18, 0, 0, 0, zoneWithDST)),
10),
KeyValue.pair(toWindowed(1,
ZonedDateTime.of(2019, 3, 30, 18, 0, 0, 0, zoneWithDST),
//This get one extra hour due to time shift in Daylight saving
//As a user i would expect it to end on 31th at 6pm.
//The limitation seems to come from TimeWindowSerializer /
// TimeWindowDeserializer as we serialize only start date.
// Suggestion: By serializing both start and end of window,
// we could support more complex cases with non fixed time
// window and address daylight savings on daily windows.
ZonedDateTime.of(2019, 3, 31, 19, 0, 0, 0, zoneWithDST)),
10)
);
verify(inputValues, expectedValues, zoneWithDST);
}
private void verify(final List<TestRecord<String, Integer>> inputValues,
final List<KeyValue<Windowed<Integer>, Integer>> expectedValues,
final ZoneId zoneId) {
final Properties streamsConfiguration = new Properties();
streamsConfiguration.put(StreamsConfig.APPLICATION_ID_CONFIG, "sum-lambda-integration-test");
streamsConfiguration.put(StreamsConfig.BOOTSTRAP_SERVERS_CONFIG, "dummy:1234");
streamsConfiguration.put(StreamsConfig.DEFAULT_KEY_SERDE_CLASS_CONFIG, Serdes.Integer().getClass().getName());
streamsConfiguration.put(StreamsConfig.DEFAULT_VALUE_SERDE_CLASS_CONFIG, Serdes.Integer().getClass().getName());
// Use a temporary directory for storing state, which will be automatically removed after the test.
streamsConfiguration.put(StreamsConfig.STATE_DIR_CONFIG, TestUtils.tempDirectory().getAbsolutePath());
final Topology topology = buildKafkaStreamTopology(zoneId);
try (final TopologyTestDriver testDriver = new TopologyTestDriver(topology, streamsConfiguration)) {
final TestInputTopic<String, Integer> input = testDriver
.createInputTopic(inputTopic,
new StringSerializer(),
new IntegerSerializer());
final TestOutputTopic<Windowed<Integer>, Integer> output = testDriver
.createOutputTopic(outputTopic,
new TimeWindowedDeserializer<>(
new IntegerDeserializer(),
Duration.ofDays(1).toMillis()),
new IntegerDeserializer());
input.pipeRecordList(inputValues);
assertThat(output.readKeyValuesToList(), equalTo(expectedValues));
}
}
private Topology buildKafkaStreamTopology(final ZoneId zoneId) {
final StreamsBuilder builder = new StreamsBuilder();
final KStream<String, Integer> input = builder.stream(inputTopic);
final Duration gracePeriod = Duration.ofMinutes(30L);
final KStream<Windowed<Integer>, Integer> sumOfOddNumbers = input
.selectKey((k, v) -> 1)
.groupByKey()
.windowedBy(new DailyTimeWindows(zoneId, windowStartHour, gracePeriod))
// A simple sum of value
.reduce(Integer::sum,
Materialized.<Integer, Integer, WindowStore<Bytes, byte[]>>with(Serdes.Integer(),
Serdes.Integer())
// the default store retention time is 1 day;
// need to explicitly increase the retention time
// to allow for a 1-day window plus configured grace period
.withRetention(Duration.ofDays(1L).plus(gracePeriod)))
// We only care about final result
.suppress(Suppressed.untilWindowCloses(Suppressed.BufferConfig.unbounded()))
.toStream();
sumOfOddNumbers.print(Printed.toSysOut());
sumOfOddNumbers.to(outputTopic, Produced.with(WindowedSerdes.timeWindowedSerdeFrom(Integer.class), Serdes.Integer()));
return builder.build();
}
private Windowed<Integer> toWindowed(final Integer key,
final ZonedDateTime start,
final ZonedDateTime end) {
return new Windowed<>(key, new TimeWindow(start.toInstant().toEpochMilli(),
end.toInstant().toEpochMilli()));
}
/** Generates an event after window end + grace period to trigger flush everything through suppression
@see KTable#suppress(Suppressed)
*/
private TestRecord<String, Integer> dummyEventToForceSuppression() {
return new TestRecord<>(null, 7, ZonedDateTime.now().toInstant());
}
}