rkhachatryan commented on code in PR #25167:
URL: https://github.com/apache/flink/pull/25167#discussion_r1713535597


##########
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/util/ProgressBlockingRelativeClock.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.flink.streaming.api.operators.util;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.runtime.metrics.TimerGauge;
+import org.apache.flink.util.clock.Clock;
+import org.apache.flink.util.clock.RelativeClock;
+
+import javax.annotation.concurrent.ThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * A {@link RelativeClock} that can be marked to start and stop blocking 
progress of the relative
+ * time with respect to the wall clock.
+ */
+@Internal
+@ThreadSafe
+public class ProgressBlockingRelativeClock implements RelativeClock, 
TimerGauge.StartStopListener {
+    private final Clock baseClock;
+
+    private long accumulativeBlockedNanoTime;
+    private long currentBlockedNanoTimeStart;
+    private long blockedCounter;
+
+    public ProgressBlockingRelativeClock(Clock baseClock) {
+        this.baseClock = baseClock;
+    }
+
+    @Override
+    public long relativeTimeMillis() {
+        return relativeTimeNanos() / 1_000_000;
+    }
+
+    @Override
+    public long relativeTimeNanos() {
+        return baseClock.relativeTimeNanos() - getBlockedTime();
+    }
+
+    public synchronized void markBlocked() {
+        if (blockedCounter == 0) {
+            currentBlockedNanoTimeStart = baseClock.relativeTimeNanos();
+        }
+        blockedCounter++;
+    }
+
+    public synchronized void markUnblocked() {
+        checkState(blockedCounter >= 1);
+        blockedCounter--;
+        if (blockedCounter == 0) {

Review Comment:
   Do I understand correctly, that the counter is needed to account for 
multiple sources of blocking (source alignment, soft back-pressure, hard 
back-pressure)?
   
   But if I'm reading `SourceOperator` code correctly, the same split can be 
paused multiple times - there is no guard in `checkSplitWatermarkAlignment`. 
But it is unpaused only once - there _**is**_ a guard:
   
   ```
   if (splitWatermark > currentMaxDesiredWatermark) {
       splitsToPause.add(splitId); // <--- add even if is paused already
   } else if (currentlyPausedSplits.contains(splitId)) {
       splitsToResume.add(splitId); // <---- add only if paused
   }
   ...
   pauseOrResumeSplits(splitsToPause, splitsToResume);
   ```



##########
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/source/ProgressiveTimestampsAndWatermarks.java:
##########
@@ -273,6 +322,16 @@ void emitPeriodicWatermark() {
             }
             watermarkMultiplexer.onPeriodicEmit();
         }
+
+        public void pauseOrResumeSplits(
+                Collection<String> splitsToPause, Collection<String> 
splitsToResume) {
+            for (String splitId : splitsToPause) {
+                inputActivityClocks.get(splitId).markBlocked();
+            }
+            for (String splitId : splitsToResume) {
+                inputActivityClocks.get(splitId).markUnblocked();
+            }

Review Comment:
   IIUC, there's no ordering guarantee between `pauseOrResumeSplits` and 
`releaseOutputForSplit`.
   So if `releaseOutputForSplit` comes first then this method might produce NPE.
   Or am I missing something?



##########
flink-table/flink-table-runtime/src/main/java/org/apache/flink/table/runtime/operators/wmassigners/WatermarkAssignerOperator.java:
##########
@@ -162,13 +172,14 @@ public void onProcessingTime(long timestamp) throws 
Exception {
         }
 
         if (processedElements != lastIdleCheckProcessedElements) {
-            timeSinceLastIdleCheck = now;
+            timeSinceLastIdleCheck = inputActivityClock.relativeTimeMillis();
             lastIdleCheckProcessedElements = processedElements;
         }
 
         if (isIdlenessEnabled()
                 && currentStatus.equals(WatermarkStatus.ACTIVE)
-                && timeSinceLastIdleCheck + idleTimeout <= now) {
+                && timeSinceLastIdleCheck + idleTimeout
+                        <= inputActivityClock.relativeTimeMillis()) {

Review Comment:
   Extract `inputActivityClock.relativeTimeMillis()` into a local variable to 
avoid one sys call and `synchronized`?



##########
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/util/ProgressBlockingRelativeClock.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.flink.streaming.api.operators.util;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.runtime.metrics.TimerGauge;
+import org.apache.flink.util.clock.Clock;
+import org.apache.flink.util.clock.RelativeClock;
+
+import javax.annotation.concurrent.ThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * A {@link RelativeClock} that can be marked to start and stop blocking 
progress of the relative
+ * time with respect to the wall clock.
+ */
+@Internal
+@ThreadSafe
+public class ProgressBlockingRelativeClock implements RelativeClock, 
TimerGauge.StartStopListener {

Review Comment:
   Can you document the main paths how this clock is paused and unpaused?
   IIUC, one of the paths goes through metrics - which is not obvious to me, so 
javadoc would be helpful.



##########
flink-streaming-java/src/main/java/org/apache/flink/streaming/api/operators/util/ProgressBlockingRelativeClock.java:
##########
@@ -0,0 +1,92 @@
+/*
+ * 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.flink.streaming.api.operators.util;
+
+import org.apache.flink.annotation.Internal;
+import org.apache.flink.runtime.metrics.TimerGauge;
+import org.apache.flink.util.clock.Clock;
+import org.apache.flink.util.clock.RelativeClock;
+
+import javax.annotation.concurrent.ThreadSafe;
+
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * A {@link RelativeClock} that can be marked to start and stop blocking 
progress of the relative
+ * time with respect to the wall clock.
+ */
+@Internal
+@ThreadSafe
+public class ProgressBlockingRelativeClock implements RelativeClock, 
TimerGauge.StartStopListener {

Review Comment:
   NIT: And probably naming it `PausableClock` would make it more generic and 
more readable.
   - To me, `Progress` doesn't tell much without reading how it's used.
   - The clients on the reader side aren't aware of any blocking



##########
flink-core/src/main/java/org/apache/flink/util/clock/ManualClock.java:
##########
@@ -68,4 +68,9 @@ public void advanceTime(long duration, TimeUnit timeUnit) {
     public void advanceTime(Duration duration) {
         currentTime.addAndGet(duration.toNanos());
     }
+
+    /** Sets the time to the given value. */
+    public void setCurrentTime(long time, TimeUnit timeUnit) {
+        currentTime.set(timeUnit.toNanos(time));
+    }

Review Comment:
   I couldn't find usages of this method



##########
flink-core/src/main/java/org/apache/flink/util/clock/RelativeClock.java:
##########
@@ -0,0 +1,35 @@
+/*
+ * 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.flink.util.clock;
+
+import org.apache.flink.annotation.PublicEvolving;
+
+/**
+ * A clock that gives access to relative time, similar to System#nanoTime(), 
however the progress of
+ * the relative time doesn't have to reflect the progress of a wall clock. 
Concrete classes can
+ * specify a different contract in that regard.
+ */
+@PublicEvolving
+public interface RelativeClock {
+    /** Gets the current relative time, in milliseconds. */
+    long relativeTimeMillis();
+
+    /** Gets the current relative time, in nanoseconds. */
+    long relativeTimeNanos();

Review Comment:
   Should it be added to the contract (javadoc) that the returned value is 
monotonically increasing?



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