You are viewing a plain text version of this content. The canonical link for it is here.
Posted to jira@kafka.apache.org by GitBox <gi...@apache.org> on 2022/03/28 04:18:51 UTC

[GitHub] [kafka] guozhangwang commented on a change in pull request #11896: [Emit final][5/N] emit final for TimeWindowedKStreamImpl

guozhangwang commented on a change in pull request #11896:
URL: https://github.com/apache/kafka/pull/11896#discussion_r836034896



##########
File path: streams/src/main/java/org/apache/kafka/streams/kstream/internals/KStreamWindowAggregate.java
##########
@@ -184,6 +203,52 @@ public void process(final Record<KIn, VIn> record) {
                     droppedRecordsSensor.record();
                 }
             }
+
+            if (emitFinal) {
+                if (!(windows instanceof TimeWindows)) {
+                    throw new IllegalStateException("Emit final is only supported for TimeWindows and SlidingWindows for TimeWindowedKStream");
+                }
+
+                // Close time does not progress
+                if (lastEmitCloseTime >= closeTime) {
+                    return;
+                }
+
+                // Emit final deosn't make sense for some window like unlimitedWindow

Review comment:
       typo

##########
File path: streams/src/main/java/org/apache/kafka/streams/kstream/internals/KStreamWindowAggregate.java
##########
@@ -184,6 +203,52 @@ public void process(final Record<KIn, VIn> record) {
                     droppedRecordsSensor.record();
                 }
             }
+
+            if (emitFinal) {
+                if (!(windows instanceof TimeWindows)) {
+                    throw new IllegalStateException("Emit final is only supported for TimeWindows and SlidingWindows for TimeWindowedKStream");
+                }
+
+                // Close time does not progress
+                if (lastEmitCloseTime >= closeTime) {
+                    return;
+                }
+
+                // Emit final deosn't make sense for some window like unlimitedWindow
+                // This should be checked in upstream before pass emit final in or checked here
+                final long closeWindowStart = closeTime - windows.size();
+                if (closeWindowStart <= 0) {
+                    return;
+                }
+
+                final long lastEmitWindowStart = lastEmitCloseTime == ConsumerRecord.NO_TIMESTAMP ?
+                    0 : lastEmitCloseTime - windows.size();
+
+                final Map<Long, W> matchedCloseWindows = windows.windowsFor(closeWindowStart);
+                final Map<Long, W> matchedEmitWindows = windows.windowsFor(lastEmitWindowStart);
+
+                // Don't fetch store if there's new close time doesn't progress enough to cover next
+                // window
+                if (matchedCloseWindows.equals(matchedEmitWindows)) {
+                    log.debug("no new windows to emit. LastEmitCloseTime={}, newCloseTime={}", lastEmitCloseTime, closeTime);
+                    return;
+                }
+
+                KeyValueIterator<Windowed<KIn>, ValueAndTimestamp<VAgg>> windowToEmit =  windowStore
+                    .fetchAll(lastEmitWindowStart + 1, closeWindowStart);
+
+                while (windowToEmit.hasNext()) {
+                    KeyValue<Windowed<KIn>, ValueAndTimestamp<VAgg>> kv = windowToEmit.next();
+                    tupleForwarder.maybeForward(
+                        record.withKey(kv.key)
+                            .withValue(new Change<>(kv.value.value(), null))

Review comment:
       This is not a suggestion: we are sending `null` with the guarantee that we should have never forward for this key before. I think a good test case coverage would be to have a windowed aggregation emit final, followed by a join. The join results would need both the old/new values to be able to correct, and if emit final we should only emit once, with old value setting to null.

##########
File path: streams/src/main/java/org/apache/kafka/streams/kstream/internals/TimeWindowedKStreamImpl.java
##########
@@ -224,7 +225,15 @@
                         + " retention=[" + retentionPeriod + "]");
             }
 
-            supplier = Stores.persistentTimestampedWindowStore(
+            // TODO: use different store name for emit final case?
+            supplier = emitFinal ?
+                Stores.persistentTimeOrderedWindowStore(

Review comment:
       Should this be updated?

##########
File path: streams/src/main/java/org/apache/kafka/streams/kstream/internals/TimeWindowedKStreamImpl.java
##########
@@ -224,7 +225,15 @@
                         + " retention=[" + retentionPeriod + "]");
             }
 
-            supplier = Stores.persistentTimestampedWindowStore(
+            // TODO: use different store name for emit final case?
+            supplier = emitFinal ?
+                Stores.persistentTimeOrderedWindowStore(
+                materialized.storeName(),
+                Duration.ofMillis(retentionPeriod),
+                Duration.ofMillis(windows.size()),
+                false, false)

Review comment:
       Should `hasIndex` be true here?

##########
File path: streams/src/main/java/org/apache/kafka/streams/state/Stores.java
##########
@@ -160,6 +161,35 @@ public String metricsScope() {
         };
     }
 
+    /**

Review comment:
       This class's changes should be reverted.

##########
File path: streams/src/main/java/org/apache/kafka/streams/kstream/internals/KStreamWindowAggregate.java
##########
@@ -184,6 +203,52 @@ public void process(final Record<KIn, VIn> record) {
                     droppedRecordsSensor.record();
                 }
             }
+
+            if (emitFinal) {
+                if (!(windows instanceof TimeWindows)) {
+                    throw new IllegalStateException("Emit final is only supported for TimeWindows and SlidingWindows for TimeWindowedKStream");
+                }
+
+                // Close time does not progress
+                if (lastEmitCloseTime >= closeTime) {
+                    return;
+                }
+
+                // Emit final deosn't make sense for some window like unlimitedWindow
+                // This should be checked in upstream before pass emit final in or checked here
+                final long closeWindowStart = closeTime - windows.size();

Review comment:
       nit: would it be better named `emitWindowStart`?




-- 
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.

To unsubscribe, e-mail: jira-unsubscribe@kafka.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org