[GitHub] [kafka] mjsax commented on a change in pull request #9039: KAFKA-5636: SlidingWindows (KIP-450)
mjsax commented on a change in pull request #9039:
URL: https://github.com/apache/kafka/pull/9039#discussion_r467231847
##
File path:
streams/src/test/java/org/apache/kafka/streams/integration/KStreamAggregationIntegrationTest.java
##
@@ -459,6 +460,95 @@ public void shouldGroupByKey() throws Exception {
)));
}
+
+
+@Test
+public void shouldReduceSlidingWindows() throws Exception {
+streamsConfiguration.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 0);
+final long firstBatchTimestamp = 2000L;
+final long timeDifference = 1000L;
+produceMessages(firstBatchTimestamp);
+final long secondBatchTimestamp = firstBatchTimestamp + timeDifference
/ 2;
+produceMessages(secondBatchTimestamp);
+final long thirdBatchTimestamp = secondBatchTimestamp + timeDifference
- 100L;
+produceMessages(thirdBatchTimestamp);
+
+final Serde> windowedSerde =
WindowedSerdes.timeWindowedSerdeFrom(String.class);
+groupedStream
+
.windowedBy(SlidingWindows.withTimeDifferenceAndGrace(ofMillis(timeDifference),
ofMillis(2000L)))
+.reduce(reducer)
+.toStream()
+.to(outputTopic, Produced.with(windowedSerde,
Serdes.String()));
+
+startStreams();
+
+final List, String>> windowedOutput
= receiveMessages(
+new TimeWindowedDeserializer<>(),
+new StringDeserializer(),
+String.class,
+25);
+
+// read from ConsoleConsumer
+final String resultFromConsoleConsumer =
readWindowedKeyedMessagesViaConsoleConsumer(
+new TimeWindowedDeserializer(),
+new StringDeserializer(),
+String.class,
+25,
+true);
+
+final Comparator, String>>
comparator =
+Comparator.comparing((KeyValueTimestamp,
String> o) -> o.key().key())
+.thenComparing(KeyValueTimestamp::value);
+
+windowedOutput.sort(comparator);
+final long firstBatchLeftWindow = firstBatchTimestamp - timeDifference;
+final long firstBatchRightWindow = firstBatchTimestamp + 1;
+final long secondBatchLeftWindow = secondBatchTimestamp -
timeDifference;
+final long secondBatchRightWindow = secondBatchTimestamp + 1;
+final long thirdBatchLeftWindow = thirdBatchTimestamp - timeDifference;
+
+final List, String>> expectResult =
Arrays.asList(
+new KeyValueTimestamp<>(new Windowed<>("A", new
TimeWindow(firstBatchLeftWindow, Long.MAX_VALUE)), "A", firstBatchTimestamp),
Review comment:
Thanks. I missed the point that this trick to pass in the windowSize
only works for KafkaStreams when we pass in `Serdes` object that are used as
provided...
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
[GitHub] [kafka] mjsax commented on a change in pull request #9039: KAFKA-5636: SlidingWindows (KIP-450)
mjsax commented on a change in pull request #9039: URL: https://github.com/apache/kafka/pull/9039#discussion_r466033381 ## File path: streams/src/main/java/org/apache/kafka/streams/kstream/SlidingWindows.java ## @@ -0,0 +1,141 @@ +/* + * 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.kstream; + +import org.apache.kafka.streams.internals.ApiUtils; +import org.apache.kafka.streams.processor.TimestampExtractor; +import java.time.Duration; +import java.util.Objects; +import static org.apache.kafka.streams.internals.ApiUtils.prepareMillisCheckFailMsgPrefix; + +/** + /** + * A sliding window used for aggregating events. + * + * Sliding Windows are defined based on a record's timestamp, window size based on the given maximum time difference (inclusive) between + * records in the same window and given window grace period. Review comment: I am not a native speaker... Don't ask me... Mr.John should know -- he has the proper education for it. This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
[GitHub] [kafka] mjsax commented on a change in pull request #9039: KAFKA-5636: SlidingWindows (KIP-450)
mjsax commented on a change in pull request #9039:
URL: https://github.com/apache/kafka/pull/9039#discussion_r466032804
##
File path:
streams/src/main/java/org/apache/kafka/streams/kstream/internals/KStreamSlidingWindowAggregate.java
##
@@ -0,0 +1,389 @@
+/*
+ * 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.kstream.internals;
+
+import org.apache.kafka.clients.consumer.ConsumerRecord;
+import org.apache.kafka.common.metrics.Sensor;
+import org.apache.kafka.streams.KeyValue;
+import org.apache.kafka.streams.kstream.Aggregator;
+import org.apache.kafka.streams.kstream.Initializer;
+import org.apache.kafka.streams.kstream.Window;
+import org.apache.kafka.streams.kstream.Windowed;
+import org.apache.kafka.streams.kstream.SlidingWindows;
+import org.apache.kafka.streams.processor.AbstractProcessor;
+import org.apache.kafka.streams.processor.Processor;
+import org.apache.kafka.streams.processor.ProcessorContext;
+import org.apache.kafka.streams.processor.internals.InternalProcessorContext;
+import org.apache.kafka.streams.processor.internals.metrics.StreamsMetricsImpl;
+import org.apache.kafka.streams.state.KeyValueIterator;
+import org.apache.kafka.streams.state.TimestampedWindowStore;
+import org.apache.kafka.streams.state.ValueAndTimestamp;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import java.time.Instant;
+import java.util.HashSet;
+import static
org.apache.kafka.streams.processor.internals.metrics.TaskMetrics.droppedRecordsSensorOrLateRecordDropSensor;
+import static
org.apache.kafka.streams.processor.internals.metrics.TaskMetrics.droppedRecordsSensorOrSkippedRecordsSensor;
+import static org.apache.kafka.streams.state.ValueAndTimestamp.getValueOrNull;
+
+public class KStreamSlidingWindowAggregate implements
KStreamAggProcessorSupplier, V, Agg> {
+private final Logger log = LoggerFactory.getLogger(getClass());
+
+private final String storeName;
+private final SlidingWindows windows;
+private final Initializer initializer;
+private final Aggregator aggregator;
+
+private boolean sendOldValues = false;
+
+public KStreamSlidingWindowAggregate(final SlidingWindows windows,
+ final String storeName,
+ final Initializer initializer,
+ final Aggregator aggregator) {
+this.windows = windows;
+this.storeName = storeName;
+this.initializer = initializer;
+this.aggregator = aggregator;
+}
+
+@Override
+public Processor get() {
+return new KStreamSlidingWindowAggregateProcessor();
+}
+
+public SlidingWindows windows() {
+return windows;
+}
+
+@Override
+public void enableSendingOldValues() {
+sendOldValues = true;
+}
+
+private class KStreamSlidingWindowAggregateProcessor extends
AbstractProcessor {
+private TimestampedWindowStore windowStore;
+private TimestampedTupleForwarder, Agg> tupleForwarder;
+private StreamsMetricsImpl metrics;
+private InternalProcessorContext internalProcessorContext;
+private Sensor lateRecordDropSensor;
+private Sensor droppedRecordsSensor;
+private long observedStreamTime = ConsumerRecord.NO_TIMESTAMP;
+private boolean reverseIteratorImplemented = false;
+
+@SuppressWarnings("unchecked")
+@Override
+public void init(final ProcessorContext context) {
+super.init(context);
+internalProcessorContext = (InternalProcessorContext) context;
+metrics = internalProcessorContext.metrics();
+final String threadId = Thread.currentThread().getName();
+lateRecordDropSensor = droppedRecordsSensorOrLateRecordDropSensor(
+threadId,
+context.taskId().toString(),
+internalProcessorContext.currentNode().name(),
+metrics
+);
+//catch unsupported operation error
+droppedRecordsSensor =
droppedRecordsSensorOrSkippedRecordsSensor(threadId,
context.taskId().toString(), metrics);
+
[GitHub] [kafka] mjsax commented on a change in pull request #9039: KAFKA-5636: SlidingWindows (KIP-450)
mjsax commented on a change in pull request #9039:
URL: https://github.com/apache/kafka/pull/9039#discussion_r466032331
##
File path:
streams/src/main/java/org/apache/kafka/streams/kstream/internals/CogroupedStreamAggregateBuilder.java
##
@@ -132,16 +135,19 @@
final boolean stateCreated,
final StoreBuilder storeBuilder,
final Windows windows,
+
final SlidingWindows slidingWindows,
final SessionWindows sessionWindows,
final Merger sessionMerger) {
final ProcessorSupplier kStreamAggregate;
-if (windows == null && sessionWindows == null) {
+if (windows == null && slidingWindows == null && sessionWindows ==
null) {
kStreamAggregate = new KStreamAggregate<>(storeBuilder.name(),
initializer, aggregator);
-} else if (windows != null && sessionWindows == null) {
+} else if (windows != null && slidingWindows == null && sessionWindows
== null) {
kStreamAggregate = new KStreamWindowAggregate<>(windows,
storeBuilder.name(), initializer, aggregator);
-} else if (windows == null && sessionMerger != null) {
+} else if (windows == null && slidingWindows != null && sessionWindows
== null) {
+kStreamAggregate = new
KStreamSlidingWindowAggregate<>(slidingWindows, storeBuilder.name(),
initializer, aggregator);
+} else if (windows == null && slidingWindows == null && sessionMerger
!= null) {
Review comment:
We pass in all parameter to sharing to code that creates the
`StatefulProcessorNode` -- not sure if it's the best way to structure the code
and I am happy to split it up into multiple methods call (as long as we avoid
code duplication). And yes, you are right, it's internal and the checks are
just for us to avoid programming errors. Users should never be exposed to it. I
personally tend to make a lot of mistakes and the more checks we have in place
the better IMHO :)
If @lct45 want's she can just do a side cleanup PR to fix it, and rebase
this PR after the cleanup PR was merged? Or we do it as follow up. Whatever
works best for you.
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
[GitHub] [kafka] mjsax commented on a change in pull request #9039: KAFKA-5636: SlidingWindows (KIP-450)
mjsax commented on a change in pull request #9039:
URL: https://github.com/apache/kafka/pull/9039#discussion_r466029116
##
File path:
streams/src/main/java/org/apache/kafka/streams/kstream/CogroupedKStream.java
##
@@ -275,6 +275,15 @@
*/
TimeWindowedCogroupedKStream windowedBy(final
Windows windows);
+/**
+ * Create a new {@link TimeWindowedCogroupedKStream} instance that can be
used to perform sliding
+ * windowed aggregations.
+ *
+ * @param windows the specification of the aggregation {@link
SlidingWindows}
+ * @return an instance of {@link TimeWindowedCogroupedKStream}
+ */
+TimeWindowedCogroupedKStream windowedBy(final SlidingWindows
windows);
Review comment:
Ack. Was just a thought.
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
[GitHub] [kafka] mjsax commented on a change in pull request #9039: KAFKA-5636: SlidingWindows (KIP-450)
mjsax commented on a change in pull request #9039:
URL: https://github.com/apache/kafka/pull/9039#discussion_r466028866
##
File path:
streams/src/test/java/org/apache/kafka/streams/integration/KStreamAggregationIntegrationTest.java
##
@@ -459,6 +460,95 @@ public void shouldGroupByKey() throws Exception {
)));
}
+
+
+@Test
+public void shouldReduceSlidingWindows() throws Exception {
+streamsConfiguration.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 0);
+final long firstBatchTimestamp = 2000L;
+final long timeDifference = 1000L;
+produceMessages(firstBatchTimestamp);
+final long secondBatchTimestamp = firstBatchTimestamp + timeDifference
/ 2;
+produceMessages(secondBatchTimestamp);
+final long thirdBatchTimestamp = secondBatchTimestamp + timeDifference
- 100L;
+produceMessages(thirdBatchTimestamp);
Review comment:
I guess testing both cases would be good. Even if testing the former
(fall outside of all existing windows) was my original intent.
And thank for comment Sophie: I tend to forget that we should produce all
(non-empty) right windows already upfront/eagerly (and not delayed/lazily when
stream-time advances beyond window-end time). In any case, it seems to be a
good test case to make sure we don't (re-)emit an (unexpected) window if
stream-time jumps ahead?
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
[GitHub] [kafka] mjsax commented on a change in pull request #9039: KAFKA-5636: SlidingWindows (KIP-450)
mjsax commented on a change in pull request #9039:
URL: https://github.com/apache/kafka/pull/9039#discussion_r466026007
##
File path:
streams/src/test/java/org/apache/kafka/streams/integration/KStreamAggregationIntegrationTest.java
##
@@ -459,6 +460,95 @@ public void shouldGroupByKey() throws Exception {
)));
}
+
+
+@Test
+public void shouldReduceSlidingWindows() throws Exception {
+streamsConfiguration.put(StreamsConfig.COMMIT_INTERVAL_MS_CONFIG, 0);
+final long firstBatchTimestamp = 2000L;
+final long timeDifference = 1000L;
+produceMessages(firstBatchTimestamp);
+final long secondBatchTimestamp = firstBatchTimestamp + timeDifference
/ 2;
+produceMessages(secondBatchTimestamp);
+final long thirdBatchTimestamp = secondBatchTimestamp + timeDifference
- 100L;
+produceMessages(thirdBatchTimestamp);
+
+final Serde> windowedSerde =
WindowedSerdes.timeWindowedSerdeFrom(String.class);
+groupedStream
+
.windowedBy(SlidingWindows.withTimeDifferenceAndGrace(ofMillis(timeDifference),
ofMillis(2000L)))
+.reduce(reducer)
+.toStream()
+.to(outputTopic, Produced.with(windowedSerde,
Serdes.String()));
+
+startStreams();
+
+final List, String>> windowedOutput
= receiveMessages(
+new TimeWindowedDeserializer<>(),
+new StringDeserializer(),
+String.class,
+25);
+
+// read from ConsoleConsumer
+final String resultFromConsoleConsumer =
readWindowedKeyedMessagesViaConsoleConsumer(
+new TimeWindowedDeserializer(),
+new StringDeserializer(),
+String.class,
+25,
+true);
+
+final Comparator, String>>
comparator =
+Comparator.comparing((KeyValueTimestamp,
String> o) -> o.key().key())
+.thenComparing(KeyValueTimestamp::value);
+
+windowedOutput.sort(comparator);
+final long firstBatchLeftWindow = firstBatchTimestamp - timeDifference;
+final long firstBatchRightWindow = firstBatchTimestamp + 1;
+final long secondBatchLeftWindow = secondBatchTimestamp -
timeDifference;
+final long secondBatchRightWindow = secondBatchTimestamp + 1;
+final long thirdBatchLeftWindow = thirdBatchTimestamp - timeDifference;
+
+final List, String>> expectResult =
Arrays.asList(
+new KeyValueTimestamp<>(new Windowed<>("A", new
TimeWindow(firstBatchLeftWindow, Long.MAX_VALUE)), "A", firstBatchTimestamp),
Review comment:
The ticket is already fixed. You need to pass in the `windowSize` into
the the constructor of `TimeWindowDeserializer` to get rid of the problem.
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
[GitHub] [kafka] mjsax commented on a change in pull request #9039: KAFKA-5636: SlidingWindows (KIP-450)
mjsax commented on a change in pull request #9039:
URL: https://github.com/apache/kafka/pull/9039#discussion_r465101059
##
File path:
streams/src/main/java/org/apache/kafka/streams/kstream/internals/SlidingWindowedKStreamImpl.java
##
@@ -0,0 +1,234 @@
+/*
+ * 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.kstream.internals;
+
+import org.apache.kafka.common.serialization.Serde;
+import org.apache.kafka.common.serialization.Serdes;
+import org.apache.kafka.common.utils.Bytes;
+import org.apache.kafka.streams.kstream.Aggregator;
+import org.apache.kafka.streams.kstream.Initializer;
+import org.apache.kafka.streams.kstream.KTable;
+import org.apache.kafka.streams.kstream.Materialized;
+import org.apache.kafka.streams.kstream.Named;
+import org.apache.kafka.streams.kstream.Reducer;
+import org.apache.kafka.streams.kstream.SlidingWindows;
+import org.apache.kafka.streams.kstream.TimeWindowedKStream;
+import org.apache.kafka.streams.kstream.Windowed;
+import org.apache.kafka.streams.kstream.internals.graph.StreamsGraphNode;
+import org.apache.kafka.streams.state.StoreBuilder;
+import org.apache.kafka.streams.state.Stores;
+import org.apache.kafka.streams.state.TimestampedWindowStore;
+import org.apache.kafka.streams.state.WindowBytesStoreSupplier;
+import org.apache.kafka.streams.state.WindowStore;
+import java.time.Duration;
+import java.util.Objects;
+import java.util.Set;
+import static
org.apache.kafka.streams.kstream.internals.KGroupedStreamImpl.AGGREGATE_NAME;
+import static
org.apache.kafka.streams.kstream.internals.KGroupedStreamImpl.REDUCE_NAME;
+
+public class SlidingWindowedKStreamImpl extends AbstractStream
implements TimeWindowedKStream {
+private final SlidingWindows windows;
+private final GroupedStreamAggregateBuilder aggregateBuilder;
+
+SlidingWindowedKStreamImpl(final SlidingWindows windows,
+ final InternalStreamsBuilder builder,
+ final Set subTopologySourceNodes,
+ final String name,
+ final Serde keySerde,
+ final Serde valueSerde,
+ final GroupedStreamAggregateBuilder
aggregateBuilder,
+ final StreamsGraphNode streamsGraphNode) {
+super(name, keySerde, valueSerde, subTopologySourceNodes,
streamsGraphNode, builder);
+this.windows = Objects.requireNonNull(windows, "windows can't be
null");
+this.aggregateBuilder = aggregateBuilder;
+}
+
+@Override
+public KTable, Long> count() {
+return count(NamedInternal.empty());
+}
+
+@Override
+public KTable, Long> count(final Named named) {
+return doCount(named, Materialized.with(keySerde, Serdes.Long()));
+}
+
+@Override
+public KTable, Long> count(final Materialized> materialized) {
+return count(NamedInternal.empty(), materialized);
+}
+
+@Override
+public KTable, Long> count(final Named named, final
Materialized> materialized) {
+Objects.requireNonNull(materialized, "materialized can't be null");
+return doCount(named, materialized);
+}
+
+private KTable, Long> doCount(final Named named,
+ final Materialized> materialized) {
+final MaterializedInternal>
materializedInternal =
+new MaterializedInternal<>(materialized, builder,
AGGREGATE_NAME);
+
+if (materializedInternal.keySerde() == null) {
+materializedInternal.withKeySerde(keySerde);
+}
+if (materializedInternal.valueSerde() == null) {
+materializedInternal.withValueSerde(Serdes.Long());
+}
+
+final String aggregateName = new
NamedInternal(named).orElseGenerateWithPrefix(builder, AGGREGATE_NAME);
+
+return aggregateBuilder.build(
+new NamedInternal(aggregateName),
+materialize(materializedInternal),
+new KStreamSlidingWindowAggregate<>(windows,
materializedInternal.storeName(), aggregateBuilder.countInitializer,
aggregateBuilder.countAggregator),
+
[GitHub] [kafka] mjsax commented on a change in pull request #9039: KAFKA-5636: SlidingWindows (KIP-450)
mjsax commented on a change in pull request #9039:
URL: https://github.com/apache/kafka/pull/9039#discussion_r465099913
##
File path:
streams/src/main/java/org/apache/kafka/streams/kstream/internals/SlidingWindowedKStreamImpl.java
##
@@ -0,0 +1,234 @@
+/*
+ * 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.kstream.internals;
+
+import org.apache.kafka.common.serialization.Serde;
+import org.apache.kafka.common.serialization.Serdes;
+import org.apache.kafka.common.utils.Bytes;
+import org.apache.kafka.streams.kstream.Aggregator;
+import org.apache.kafka.streams.kstream.Initializer;
+import org.apache.kafka.streams.kstream.KTable;
+import org.apache.kafka.streams.kstream.Materialized;
+import org.apache.kafka.streams.kstream.Named;
+import org.apache.kafka.streams.kstream.Reducer;
+import org.apache.kafka.streams.kstream.SlidingWindows;
+import org.apache.kafka.streams.kstream.TimeWindowedKStream;
+import org.apache.kafka.streams.kstream.Windowed;
+import org.apache.kafka.streams.kstream.internals.graph.StreamsGraphNode;
+import org.apache.kafka.streams.state.StoreBuilder;
+import org.apache.kafka.streams.state.Stores;
+import org.apache.kafka.streams.state.TimestampedWindowStore;
+import org.apache.kafka.streams.state.WindowBytesStoreSupplier;
+import org.apache.kafka.streams.state.WindowStore;
+import java.time.Duration;
+import java.util.Objects;
+import java.util.Set;
+import static
org.apache.kafka.streams.kstream.internals.KGroupedStreamImpl.AGGREGATE_NAME;
+import static
org.apache.kafka.streams.kstream.internals.KGroupedStreamImpl.REDUCE_NAME;
+
+public class SlidingWindowedKStreamImpl extends AbstractStream
implements TimeWindowedKStream {
+private final SlidingWindows windows;
+private final GroupedStreamAggregateBuilder aggregateBuilder;
+
+SlidingWindowedKStreamImpl(final SlidingWindows windows,
+ final InternalStreamsBuilder builder,
+ final Set subTopologySourceNodes,
+ final String name,
+ final Serde keySerde,
+ final Serde valueSerde,
+ final GroupedStreamAggregateBuilder
aggregateBuilder,
+ final StreamsGraphNode streamsGraphNode) {
+super(name, keySerde, valueSerde, subTopologySourceNodes,
streamsGraphNode, builder);
+this.windows = Objects.requireNonNull(windows, "windows can't be
null");
+this.aggregateBuilder = aggregateBuilder;
+}
+
+@Override
+public KTable, Long> count() {
+return count(NamedInternal.empty());
+}
+
+@Override
+public KTable, Long> count(final Named named) {
+return doCount(named, Materialized.with(keySerde, Serdes.Long()));
+}
+
+@Override
+public KTable, Long> count(final Materialized> materialized) {
+return count(NamedInternal.empty(), materialized);
+}
+
+@Override
+public KTable, Long> count(final Named named, final
Materialized> materialized) {
+Objects.requireNonNull(materialized, "materialized can't be null");
+return doCount(named, materialized);
+}
+
+private KTable, Long> doCount(final Named named,
+ final Materialized> materialized) {
+final MaterializedInternal>
materializedInternal =
+new MaterializedInternal<>(materialized, builder,
AGGREGATE_NAME);
+
+if (materializedInternal.keySerde() == null) {
+materializedInternal.withKeySerde(keySerde);
+}
+if (materializedInternal.valueSerde() == null) {
+materializedInternal.withValueSerde(Serdes.Long());
+}
+
+final String aggregateName = new
NamedInternal(named).orElseGenerateWithPrefix(builder, AGGREGATE_NAME);
+
+return aggregateBuilder.build(
+new NamedInternal(aggregateName),
+materialize(materializedInternal),
+new KStreamSlidingWindowAggregate<>(windows,
materializedInternal.storeName(), aggregateBuilder.countInitializer,
aggregateBuilder.countAggregator),
+
[GitHub] [kafka] mjsax commented on a change in pull request #9039: KAFKA-5636: SlidingWindows (KIP-450)
mjsax commented on a change in pull request #9039:
URL: https://github.com/apache/kafka/pull/9039#discussion_r465097558
##
File path:
streams/src/main/java/org/apache/kafka/streams/kstream/internals/CogroupedStreamAggregateBuilder.java
##
@@ -132,16 +135,19 @@
final boolean stateCreated,
final StoreBuilder storeBuilder,
final Windows windows,
+
final SlidingWindows slidingWindows,
final SessionWindows sessionWindows,
final Merger sessionMerger) {
final ProcessorSupplier kStreamAggregate;
-if (windows == null && sessionWindows == null) {
+if (windows == null && slidingWindows == null && sessionWindows ==
null) {
kStreamAggregate = new KStreamAggregate<>(storeBuilder.name(),
initializer, aggregator);
-} else if (windows != null && sessionWindows == null) {
+} else if (windows != null && slidingWindows == null && sessionWindows
== null) {
kStreamAggregate = new KStreamWindowAggregate<>(windows,
storeBuilder.name(), initializer, aggregator);
-} else if (windows == null && sessionMerger != null) {
+} else if (windows == null && slidingWindows != null && sessionWindows
== null) {
+kStreamAggregate = new
KStreamSlidingWindowAggregate<>(slidingWindows, storeBuilder.name(),
initializer, aggregator);
+} else if (windows == null && slidingWindows == null && sessionMerger
!= null) {
Review comment:
Well, yes and no. We can follow two strategies: (1) we rely on the user
to only set all `null` (for the non-windowed aggregation case) or either one of
`windows`, `slidingWindow`, or `sessionWindow+sessionMerger` and we don't do
any verification if the method is called correctly or not. However, for this
case, we don't need to do any redundant not-null check and we could just write:
```
if (windows != null) {
...
} else if (slidingWindow != null) {
...
} else if (sessionWindow != null) { // we blindly assume that
`sessionMerger` is not-null, too, for this case
...
} else { // all `null`: non-winowed aggregation
...
}
```
Or, (2) we do not "trust" the caller and do a proper check that the provided
arguments make sense. And the existing code already has such a safe guard and
does checks that not multiple windows are passed in and throws an
`IllegalArgumentException` if the caller makes a mistake. I personally prefer
to have a safe guard (especially on the non-hot code path) as it may prevent
bugs. However, the current check is not complete, as it does not verify that
`sessionMerger` must be not-null when `sessionWindows` is not-null; this may
lead to a potentially cryptic `NullPointerException` later that is harder to
understand. If we do the check and throw a proper
`IllegalArgumentException("sessionMerger cannot be null for sessionWindows")`
and `IllegalArgumentException("Unexpected sessionMerger parameter: should be
null because sessionWindows is null");` help to identify the issue quickly.
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
[GitHub] [kafka] mjsax commented on a change in pull request #9039: KAFKA-5636: SlidingWindows (KIP-450)
mjsax commented on a change in pull request #9039:
URL: https://github.com/apache/kafka/pull/9039#discussion_r464728901
##
File path:
streams/src/main/java/org/apache/kafka/streams/kstream/CogroupedKStream.java
##
@@ -275,6 +275,15 @@
*/
TimeWindowedCogroupedKStream windowedBy(final
Windows windows);
+/**
+ * Create a new {@link TimeWindowedCogroupedKStream} instance that can be
used to perform sliding
+ * windowed aggregations.
+ *
+ * @param windows the specification of the aggregation {@link
SlidingWindows}
+ * @return an instance of {@link TimeWindowedCogroupedKStream}
+ */
+TimeWindowedCogroupedKStream windowedBy(final SlidingWindows
windows);
Review comment:
This PR is rather larger. Would it maybe make sense to split it into 2
and add co-group in it's own PR?
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]
[GitHub] [kafka] mjsax commented on a change in pull request #9039: KAFKA-5636: SlidingWindows (KIP-450)
mjsax commented on a change in pull request #9039: URL: https://github.com/apache/kafka/pull/9039#discussion_r464728426 ## File path: streams/src/main/java/org/apache/kafka/streams/kstream/internals/TimeWindowedKStreamImpl.java ## @@ -19,7 +19,16 @@ import org.apache.kafka.common.serialization.Serde; import org.apache.kafka.common.serialization.Serdes; import org.apache.kafka.common.utils.Bytes; -import org.apache.kafka.streams.kstream.*; Review comment: The build should actually fail on wildcard imports... Do we have some checkstyle gaps? @lct45 can you maybe look into that (if not, also ok). This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
