You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by "fredia (via GitHub)" <gi...@apache.org> on 2024/12/10 06:53:04 UTC

[PR] [FLINK-34973][runtime] Complete timer before emitting watermark [flink]

fredia opened a new pull request, #25773:
URL: https://github.com/apache/flink/pull/25773

   <!--
   *Thank you very much for contributing to Apache Flink - we are happy that you want to help us improve Flink. To help the community review your contribution in the best possible way, please go through the checklist below, which will get the contribution into a shape in which it can be best reviewed.*
   
   *Please understand that we do not do this to make contributions to Flink a hassle. In order to uphold a high standard of quality for code contributions, while at the same time managing a large number of contributions, we need contributors to prepare the contributions well, and give reviewers enough contextual information for the review. Please also understand that contributions that do not follow this guide will take longer to review and thus typically be picked up with lower priority by the community.*
   
   ## Contribution Checklist
   
     - Make sure that the pull request corresponds to a [JIRA issue](https://issues.apache.org/jira/projects/FLINK/issues). Exceptions are made for typos in JavaDoc or documentation files, which need no JIRA issue.
     
     - Name the pull request in the form "[FLINK-XXXX] [component] Title of the pull request", where *FLINK-XXXX* should be replaced by the actual issue number. Skip *component* if you are unsure about which is the best component.
     Typo fixes that have no associated JIRA issue should be named following this pattern: `[hotfix] [docs] Fix typo in event time introduction` or `[hotfix] [javadocs] Expand JavaDoc for PuncuatedWatermarkGenerator`.
   
     - Fill out the template below to describe the changes contributed by the pull request. That will give reviewers the context they need to do the review.
     
     - Make sure that the change passes the automated tests, i.e., `mvn clean verify` passes. You can set up Azure Pipelines CI to do that following [this guide](https://cwiki.apache.org/confluence/display/FLINK/Azure+Pipelines#AzurePipelines-Tutorial:SettingupAzurePipelinesforaforkoftheFlinkrepository).
   
     - Each pull request should address only one issue, not mix up code from multiple issues.
     
     - Each commit in the pull request has a meaningful commit message (including the JIRA id)
   
     - Once all items of the checklist are addressed, remove the above text and this checklist, leaving only the filled out template below.
   
   
   **(The sections below can be removed for hotfixes of typos)**
   -->
   
   ## What is the purpose of the change
   
   This PR makes timers completed before emitting watermark.
   
   
   ## Brief change log
   
   - Let `advanceWatermark` return `CompletableFuture`.
   
   
   ## Verifying this change
   
   This change added tests and can be verified as follows:
   - `InternalTimerServiceAsyncImplTest#testSameKeyEventTimerFireOrder`
   - 
   ## Does this pull request potentially affect one of the following parts:
   
     - Dependencies (does it add or upgrade a dependency): (no)
     - The public API, i.e., is any changed class annotated with `@Public(Evolving)`: (no)
     - The serializers: (no)
     - The runtime per-record code paths (performance sensitive): (no)
     - Anything that affects deployment or recovery: JobManager (and its components), Checkpointing, Kubernetes/Yarn, ZooKeeper: (no)
     - The S3 file system connector: (no)
   
   ## Documentation
   
     - Does this pull request introduce a new feature? (no)
     - If yes, how is the feature documented? (not applicable / docs / JavaDocs / not documented)
   


-- 
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: issues-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-34973][runtime] Complete timer before emitting watermark [flink]

Posted by "flinkbot (via GitHub)" <gi...@apache.org>.
flinkbot commented on PR #25773:
URL: https://github.com/apache/flink/pull/25773#issuecomment-2530598280

   <!--
   Meta data
   {
     "version" : 1,
     "metaDataEntries" : [ {
       "hash" : "e2c3e829a5e0691dc94d5530272aa4787cbbbd69",
       "status" : "UNKNOWN",
       "url" : "TBD",
       "triggerID" : "e2c3e829a5e0691dc94d5530272aa4787cbbbd69",
       "triggerType" : "PUSH"
     } ]
   }-->
   ## CI report:
   
   * e2c3e829a5e0691dc94d5530272aa4787cbbbd69 UNKNOWN
   
   <details>
   <summary>Bot commands</summary>
     The @flinkbot bot supports the following commands:
   
    - `@flinkbot run azure` re-run the last Azure build
   </details>


-- 
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: issues-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-36875][runtime] Complete timer before emitting watermark [flink]

Posted by "fredia (via GitHub)" <gi...@apache.org>.
fredia commented on code in PR #25773:
URL: https://github.com/apache/flink/pull/25773#discussion_r1877978601


##########
flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/operators/AbstractAsyncStateStreamOperatorV2.java:
##########
@@ -260,6 +262,15 @@ public void processWatermark(Watermark mark) throws Exception {
         asyncExecutionController.processNonRecord(() -> super.processWatermark(mark));
     }
 
+    protected void emitWatermarkDirectly(Watermark mark) throws Exception {

Review Comment:
   As Zakelly's suggestion, I removed `emitWatermark` in `AbstractAsyncStateStreamOperatorV2`.



-- 
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: issues-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-36875][runtime] Complete timer before emitting watermark [flink]

Posted by "fredia (via GitHub)" <gi...@apache.org>.
fredia commented on code in PR #25773:
URL: https://github.com/apache/flink/pull/25773#discussion_r1879258285


##########
flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/operators/AbstractAsyncStateStreamOperator.java:
##########
@@ -330,7 +332,22 @@ public void processWatermark(Watermark mark) throws Exception {
             super.processWatermark(mark);
             return;
         }
-        asyncExecutionController.processNonRecord(() -> super.processWatermark(mark));
+        asyncExecutionController.processNonRecord(
+                () -> {
+                    // todo: make async operator deal with interruptible watermark
+                    if (timeServiceManager != null) {
+                        CompletableFuture<Void> future = timeServiceManager.advanceWatermark(mark);
+                        if (asyncExecutionController.isSerialMode()) {
+                            asyncExecutionController.drainInflightRecords(0);
+                            future.get();
+                            output.emitWatermark(mark);
+                        } else {
+                            future.thenAccept(v -> output.emitWatermark(mark));
+                        }

Review Comment:
   Nice suggestion, moved the `drain` and `assert future.isDone()` into AEC.



-- 
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: issues-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-34973][runtime] Complete timer before emitting watermark [flink]

Posted by "Zakelly (via GitHub)" <gi...@apache.org>.
Zakelly commented on code in PR #25773:
URL: https://github.com/apache/flink/pull/25773#discussion_r1877548425


##########
flink-runtime/src/main/java/org/apache/flink/streaming/api/operators/InternalTimerServiceAsyncImpl.java:
##########
@@ -125,13 +137,14 @@ protected void foreachTimer(
                 "Batch operation is not supported when using async state.");
     }
 
-    private void maintainContextAndProcess(
+    private StateFuture<Void> maintainContextAndProcess(
             InternalTimer<K, N> timer, ThrowingRunnable<Exception> runnable) {
         RecordContext<K> recordCtx = asyncExecutionController.buildContext(null, timer.getKey());

Review Comment:
   Apart from current topic: How about make timer as the record and invoke `buildContext`?



##########
flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/operators/AbstractAsyncStateStreamOperatorV2.java:
##########
@@ -260,6 +262,15 @@ public void processWatermark(Watermark mark) throws Exception {
         asyncExecutionController.processNonRecord(() -> super.processWatermark(mark));
     }
 
+    protected void emitWatermarkDirectly(Watermark mark) throws Exception {
+        if (timeServiceManager != null) {
+            CompletableFuture<Void> future = timeServiceManager.advanceWatermark(mark);
+            future.thenAccept(v -> output.emitWatermark(mark));

Review Comment:
   I think we should also consider `EpochManager.ParallelMode` here. If serial, should wait & drain here.



##########
flink-runtime/src/main/java/org/apache/flink/streaming/api/operators/InternalTimeServiceManagerImpl.java:
##########
@@ -238,10 +239,14 @@ KeyGroupedInternalPriorityQueue<TimerHeapInternalTimer<K, N>> createTimerPriorit
     }
 
     @Override
-    public void advanceWatermark(Watermark watermark) throws Exception {
+    public CompletableFuture<Void> advanceWatermark(Watermark watermark) throws Exception {
+        CompletableFuture<Void>[] futures = new CompletableFuture[timerServices.size()];

Review Comment:
   ```suggestion
           CompletableFuture<?>[] futures = new CompletableFuture[timerServices.size()];
   ```
   to avoid warning



##########
flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/operators/AbstractAsyncStateStreamOperator.java:
##########
@@ -333,6 +335,15 @@ public void processWatermark(Watermark mark) throws Exception {
         asyncExecutionController.processNonRecord(() -> super.processWatermark(mark));
     }
 
+    protected void emitWatermarkDirectly(Watermark mark) throws Exception {

Review Comment:
   How about rewriting `processWatermark` in `AbstractAsyncStateStreamOperator.java` instead of exposing the `emitWatermarkDirectly` ? 



-- 
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: issues-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-36875][runtime] Complete timer before emitting watermark [flink]

Posted by "fredia (via GitHub)" <gi...@apache.org>.
fredia commented on code in PR #25773:
URL: https://github.com/apache/flink/pull/25773#discussion_r1877976956


##########
flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/operators/AbstractAsyncStateStreamOperator.java:
##########
@@ -333,6 +335,15 @@ public void processWatermark(Watermark mark) throws Exception {
         asyncExecutionController.processNonRecord(() -> super.processWatermark(mark));
     }
 
+    protected void emitWatermarkDirectly(Watermark mark) throws Exception {

Review Comment:
   Fixed, recovered `emitWatermarkDirectly ` as origin, and rewrote `processWatermark`. Currently, the async operator won't deal with interruptible watermark.



-- 
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: issues-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-36875][runtime] Complete timer before emitting watermark [flink]

Posted by "Zakelly (via GitHub)" <gi...@apache.org>.
Zakelly commented on code in PR #25773:
URL: https://github.com/apache/flink/pull/25773#discussion_r1879329642


##########
flink-runtime/src/main/java/org/apache/flink/streaming/api/operators/InternalTimerServiceAsyncImpl.java:
##########
@@ -97,20 +102,27 @@ void onProcessingTime(long time) throws Exception {
      * @param time the time in watermark.
      */
     @Override
-    public void advanceWatermark(long time) throws Exception {
+    public CompletableFuture<Void> advanceWatermark(long time) throws Exception {
         currentWatermark = time;
-
+        List<CompletableFuture<Void>> futures = new ArrayList<>();
         InternalTimer<K, N> timer;
-
         while ((timer = eventTimeTimersQueue.peek()) != null
                 && timer.getTimestamp() <= time
                 && !cancellationContext.isCancelled()) {
             eventTimeTimersQueue.poll();
             final InternalTimer<K, N> timerToTrigger = timer;
+            CompletableFuture<Void> future = new CompletableFuture<>();
             maintainContextAndProcess(
-                    timerToTrigger, () -> triggerTarget.onEventTime(timerToTrigger));
+                            timerToTrigger, () -> triggerTarget.onEventTime(timerToTrigger))
+                    .thenAccept(v -> future.complete(null));
+            futures.add(future);
             taskIOMetricGroup.getNumFiredTimers().inc();
         }
+        CompletableFuture<?>[] futuresArray = new CompletableFuture[futures.size()];
+        for (int i = 0; i < futures.size(); i++) {
+            futuresArray[i] = futures.get(i);
+        }
+        return CompletableFuture.allOf(futuresArray);

Review Comment:
   ```suggestion
           return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0]));
   ```



##########
flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/AsyncExecutionController.java:
##########
@@ -387,6 +390,14 @@ public void drainInflightRecords(int targetNum) {
         }
     }
 
+    /** A helper function to drain in-flight requests emitted by timer. */
+    public void drainWithTimerIfNeeded(CompletableFuture<Void> timerFuture) {
+        if (epochParallelMode == ParallelMode.SERIAL_BETWEEN_EPOCH) {
+            drainInflightRecords(0);
+            assert timerFuture.isDone();

Review Comment:
   Hmmmm.... How about using the `Preconditions.checkState()`



-- 
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: issues-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-34973][runtime] Complete timer before emitting watermark [flink]

Posted by "davidradl (via GitHub)" <gi...@apache.org>.
davidradl commented on code in PR #25773:
URL: https://github.com/apache/flink/pull/25773#discussion_r1877705441


##########
flink-runtime/src/main/java/org/apache/flink/streaming/api/operators/InternalTimerServiceAsyncImpl.java:
##########
@@ -125,13 +137,14 @@ protected void foreachTimer(
                 "Batch operation is not supported when using async state.");
     }
 
-    private void maintainContextAndProcess(
+    private StateFuture<Void> maintainContextAndProcess(
             InternalTimer<K, N> timer, ThrowingRunnable<Exception> runnable) {
         RecordContext<K> recordCtx = asyncExecutionController.buildContext(null, timer.getKey());
         recordCtx.retain();
         asyncExecutionController.setCurrentContext(recordCtx);
         keyContext.setCurrentKey(timer.getKey());
-        asyncExecutionController.syncPointRequestWithCallback(runnable);
+        StateFuture<Void> future = asyncExecutionController.syncPointRequestWithCallback(runnable);
         recordCtx.release();
+        return future;

Review Comment:
   NIT: 
   I am curious if there a reason to release the context before returning. If not could we have could we change to `return asyncExecutionController.syncPointRequestWithCallback(runnable);`



-- 
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: issues-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-36875][runtime] Complete timer before emitting watermark [flink]

Posted by "fredia (via GitHub)" <gi...@apache.org>.
fredia merged PR #25773:
URL: https://github.com/apache/flink/pull/25773


-- 
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: issues-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-36875][runtime] Complete timer before emitting watermark [flink]

Posted by "fredia (via GitHub)" <gi...@apache.org>.
fredia commented on code in PR #25773:
URL: https://github.com/apache/flink/pull/25773#discussion_r1877981153


##########
flink-runtime/src/main/java/org/apache/flink/streaming/api/operators/InternalTimerServiceAsyncImpl.java:
##########
@@ -125,13 +137,14 @@ protected void foreachTimer(
                 "Batch operation is not supported when using async state.");
     }
 
-    private void maintainContextAndProcess(
+    private StateFuture<Void> maintainContextAndProcess(
             InternalTimer<K, N> timer, ThrowingRunnable<Exception> runnable) {
         RecordContext<K> recordCtx = asyncExecutionController.buildContext(null, timer.getKey());
         recordCtx.retain();
         asyncExecutionController.setCurrentContext(recordCtx);
         keyContext.setCurrentKey(timer.getKey());
-        asyncExecutionController.syncPointRequestWithCallback(runnable);
+        StateFuture<Void> future = asyncExecutionController.syncPointRequestWithCallback(runnable);
         recordCtx.release();
+        return future;

Review Comment:
   Because `recordCtx.retain()` is called before handling a state request, and the `recordCtx.release()` should appear in pairs.



-- 
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: issues-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-36875][runtime] Complete timer before emitting watermark [flink]

Posted by "fredia (via GitHub)" <gi...@apache.org>.
fredia commented on PR #25773:
URL: https://github.com/apache/flink/pull/25773#issuecomment-2533483990

   @flinkbot run azure


-- 
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: issues-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-36875][runtime] Complete timer before emitting watermark [flink]

Posted by "fredia (via GitHub)" <gi...@apache.org>.
fredia commented on code in PR #25773:
URL: https://github.com/apache/flink/pull/25773#discussion_r1877972095


##########
flink-runtime/src/main/java/org/apache/flink/streaming/api/operators/InternalTimerServiceAsyncImpl.java:
##########
@@ -125,13 +137,14 @@ protected void foreachTimer(
                 "Batch operation is not supported when using async state.");
     }
 
-    private void maintainContextAndProcess(
+    private StateFuture<Void> maintainContextAndProcess(
             InternalTimer<K, N> timer, ThrowingRunnable<Exception> runnable) {
         RecordContext<K> recordCtx = asyncExecutionController.buildContext(null, timer.getKey());

Review Comment:
   Thanks for the suggestion, I have made the timer as the record of timer request.



-- 
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: issues-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-34973][runtime] Complete timer before emitting watermark [flink]

Posted by "davidradl (via GitHub)" <gi...@apache.org>.
davidradl commented on code in PR #25773:
URL: https://github.com/apache/flink/pull/25773#discussion_r1877694520


##########
flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/operators/AbstractAsyncStateStreamOperatorV2.java:
##########
@@ -260,6 +262,15 @@ public void processWatermark(Watermark mark) throws Exception {
         asyncExecutionController.processNonRecord(() -> super.processWatermark(mark));
     }
 
+    protected void emitWatermarkDirectly(Watermark mark) throws Exception {

Review Comment:
   NIT: now that the watermark can be emitted in a future in this method it does not seem directly. I wonder if we could rename to `emitWatermark`.  



-- 
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: issues-unsubscribe@flink.apache.org

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


Re: [PR] [FLINK-36875][runtime] Complete timer before emitting watermark [flink]

Posted by "Zakelly (via GitHub)" <gi...@apache.org>.
Zakelly commented on code in PR #25773:
URL: https://github.com/apache/flink/pull/25773#discussion_r1879241229


##########
flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/operators/AbstractAsyncStateStreamOperator.java:
##########
@@ -330,7 +332,22 @@ public void processWatermark(Watermark mark) throws Exception {
             super.processWatermark(mark);
             return;
         }
-        asyncExecutionController.processNonRecord(() -> super.processWatermark(mark));
+        asyncExecutionController.processNonRecord(
+                () -> {
+                    // todo: make async operator deal with interruptible watermark
+                    if (timeServiceManager != null) {
+                        CompletableFuture<Void> future = timeServiceManager.advanceWatermark(mark);
+                        if (asyncExecutionController.isSerialMode()) {
+                            asyncExecutionController.drainInflightRecords(0);
+                            future.get();
+                            output.emitWatermark(mark);
+                        } else {
+                            future.thenAccept(v -> output.emitWatermark(mark));
+                        }

Review Comment:
   How about package these logic into a method of `AEC`?
   e.g.
   
   ```suggestion
                           CompletableFuture<Void> future = timeServiceManager.advanceWatermark(mark);
                           future.thenAccept(v -> output.emitWatermark(mark));
                           asyncExecutionController.drainWithTimer();
   ```
   
   And we only do `assert future.isDone()` in the `drainWithTimer()` instead of the `future.get()`



##########
flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/operators/AbstractAsyncStateStreamOperator.java:
##########
@@ -330,7 +332,22 @@ public void processWatermark(Watermark mark) throws Exception {
             super.processWatermark(mark);
             return;
         }
-        asyncExecutionController.processNonRecord(() -> super.processWatermark(mark));
+        asyncExecutionController.processNonRecord(
+                () -> {
+                    // todo: make async operator deal with interruptible watermark
+                    if (timeServiceManager != null) {
+                        CompletableFuture<Void> future = timeServiceManager.advanceWatermark(mark);
+                        if (asyncExecutionController.isSerialMode()) {
+                            asyncExecutionController.drainInflightRecords(0);
+                            future.get();
+                            output.emitWatermark(mark);
+                        } else {
+                            future.thenAccept(v -> output.emitWatermark(mark));
+                        }

Review Comment:
   And I think we could handle the interruptible timers in later PR(s).



-- 
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: issues-unsubscribe@flink.apache.org

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