dawidwys commented on a change in pull request #15771:
URL: https://github.com/apache/flink/pull/15771#discussion_r633267867



##########
File path: 
flink-core/src/main/java/org/apache/flink/api/common/eventtime/CombinedWatermark.java
##########
@@ -0,0 +1,148 @@
+/*
+ * 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.api.common.eventtime;
+
+import org.apache.flink.annotation.Internal;
+
+import java.util.AbstractList;
+import java.util.ArrayList;
+import java.util.List;
+
+import static org.apache.flink.util.Preconditions.checkState;
+
+/**
+ * A {@link CombinedWatermark} combines the watermark (and idleness) updates 
of multiple
+ * partitions/shards/splits into one combined watermark.
+ */
+@Internal
+public final class CombinedWatermark extends 
AbstractList<CombinedWatermark.PartialWatermark> {
+
+    /** List of all watermark outputs, for efficient access. */
+    private final List<PartialWatermark> partialWatermarks = new ArrayList<>();
+
+    /** The combined watermark over the per-output watermarks. */
+    private long combinedWatermark = Long.MIN_VALUE;
+
+    private boolean idle = false;
+
+    public long getCombinedWatermark() {
+        return combinedWatermark;
+    }
+
+    public boolean isIdle() {
+        return idle;
+    }
+
+    @Override
+    public PartialWatermark get(int index) {
+        return partialWatermarks.get(index);
+    }
+
+    @Override
+    public boolean remove(Object o) {
+        return partialWatermarks.remove(o);
+    }
+
+    @Override
+    public PartialWatermark set(int index, PartialWatermark element) {
+        return partialWatermarks.set(index, element);
+    }
+
+    @Override
+    public void add(int index, PartialWatermark element) {
+        partialWatermarks.add(index, element);
+    }
+
+    @Override
+    public int size() {
+        return partialWatermarks.size();
+    }
+
+    /**
+     * Checks whether we need to update the combined watermark.
+     *
+     * <p><b>NOTE:</b>It can update {@link #isIdle()} status.
+     *
+     * @return true, if the combined watermark changed
+     */
+    public boolean updateCombinedWatermark() {
+        long minimumOverAllOutputs = Long.MAX_VALUE;
+
+        boolean hasOutputs = false;
+        boolean allIdle = true;
+        for (PartialWatermark partialWatermark : partialWatermarks) {
+            if (!partialWatermark.isIdle()) {
+                minimumOverAllOutputs =
+                        Math.min(minimumOverAllOutputs, 
partialWatermark.getWatermark());
+                allIdle = false;
+            }
+            hasOutputs = true;
+        }
+
+        // if we don't have any outputs minimumOverAllOutputs is not valid, 
it's still
+        // at its initial Long.MAX_VALUE state and we must not emit that
+        this.idle = allIdle;
+        if (!hasOutputs || allIdle) {
+            return false;
+        }
+
+        if (minimumOverAllOutputs > combinedWatermark) {
+            combinedWatermark = minimumOverAllOutputs;
+            return true;
+        }
+
+        return false;
+    }
+
+    /** Per-output watermark state. */
+    public static class PartialWatermark {

Review comment:
       I agree. If I were writing the class from scratch I'd aim for 
immutability. I don't want to change the logic too much in the crucial 
`WatermarkOutputMultiplexer`.




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


Reply via email to