Zakelly commented on code in PR #24895:
URL: https://github.com/apache/flink/pull/24895#discussion_r1634298332


##########
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/InternalTimerServiceImpl.java:
##########
@@ -307,18 +307,37 @@ void onProcessingTime(long time) throws Exception {
     }
 
     public void advanceWatermark(long time) throws Exception {
-        currentWatermark = time;
+        Preconditions.checkState(
+                tryAdvanceWatermark(
+                        time,
+                        () -> {
+                            // Never stop advancing.
+                            return false;
+                        }));
+    }
 
+    /**
+     * @return true if following watermarks can be processed immediately. 
False if the firing timers
+     *     should be interrupted as soon as possible.
+     */
+    public boolean tryAdvanceWatermark(
+            long time, InternalTimeServiceManager.ShouldStopAdvancingFn 
shouldStopAdvancingFn)
+            throws Exception {
+        currentWatermark = time;
         InternalTimer<K, N> timer;
-
+        boolean stop = cancellationContext.isCancelled();
         while ((timer = eventTimeTimersQueue.peek()) != null
                 && timer.getTimestamp() <= time
-                && !cancellationContext.isCancelled()) {
+                && !stop) {
             keyContext.setCurrentKey(timer.getKey());
             eventTimeTimersQueue.poll();
             triggerTarget.onEventTime(timer);
             taskIOMetricGroup.getNumFiredTimers().inc();
+            // Check if we should stop advancing after at least one iteration 
to guarantee progress
+            // and prevent a potential starvation.
+            stop = cancellationContext.isCancelled() || 
shouldStopAdvancingFn.test();
         }
+        return true;

Review Comment:
   Should be `return stop` ?



##########
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/InternalTimerServiceImpl.java:
##########
@@ -307,18 +307,37 @@ void onProcessingTime(long time) throws Exception {
     }
 
     public void advanceWatermark(long time) throws Exception {
-        currentWatermark = time;
+        Preconditions.checkState(
+                tryAdvanceWatermark(
+                        time,
+                        () -> {
+                            // Never stop advancing.
+                            return false;
+                        }));
+    }
 
+    /**
+     * @return true if following watermarks can be processed immediately. 
False if the firing timers
+     *     should be interrupted as soon as possible.
+     */
+    public boolean tryAdvanceWatermark(
+            long time, InternalTimeServiceManager.ShouldStopAdvancingFn 
shouldStopAdvancingFn)
+            throws Exception {
+        currentWatermark = time;
         InternalTimer<K, N> timer;
-
+        boolean stop = cancellationContext.isCancelled();
         while ((timer = eventTimeTimersQueue.peek()) != null
                 && timer.getTimestamp() <= time
-                && !cancellationContext.isCancelled()) {
+                && !stop) {
             keyContext.setCurrentKey(timer.getKey());
             eventTimeTimersQueue.poll();
             triggerTarget.onEventTime(timer);
             taskIOMetricGroup.getNumFiredTimers().inc();
+            // Check if we should stop advancing after at least one iteration 
to guarantee progress
+            // and prevent a potential starvation.
+            stop = cancellationContext.isCancelled() || 
shouldStopAdvancingFn.test();
         }
+        return true;

Review Comment:
   Should be `return !stop` ?



-- 
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-unsubscr...@flink.apache.org

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

Reply via email to