You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by "dchristle (via GitHub)" <gi...@apache.org> on 2023/04/19 19:11:17 UTC

[GitHub] [iceberg] dchristle commented on a diff in pull request #7366: Flink: add event time ordered assigner for FLIP-27 source

dchristle commented on code in PR #7366:
URL: https://github.com/apache/iceberg/pull/7366#discussion_r1171650885


##########
flink/v1.16/flink/src/main/java/org/apache/iceberg/flink/source/assigner/EventTimeAlignedAssigner.java:
##########
@@ -0,0 +1,301 @@
+/*
+ *
+ *  * 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.iceberg.flink.source.assigner;
+
+import java.io.Serializable;
+import java.time.Duration;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.NotThreadSafe;
+import org.apache.flink.api.common.eventtime.TimestampAssigner;
+import org.apache.iceberg.flink.source.split.IcebergSourceSplit;
+import org.apache.iceberg.flink.source.split.IcebergSourceSplitState;
+import org.apache.iceberg.flink.source.split.IcebergSourceSplitStatus;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.flink.calcite.shaded.com.google.common.collect.Iterables.getFirst;
+
+public class EventTimeAlignedAssigner implements SplitAssigner, WatermarkTracker.Listener {
+  private static final Logger log = LoggerFactory.getLogger(EventTimeAlignedAssigner.class);
+  private final WatermarkTracker watermarkTracker;
+  private final State state;
+  private final TimestampAssigner<IcebergSourceSplit> timestampAssigner;
+  private final FutureNotifier futureNotifier;
+  private final Options options;
+
+  // flag that informs the state that there are no more splits to be added by the Enumerator
+  private volatile boolean noMoreSplits;
+
+  public EventTimeAlignedAssigner(
+      WatermarkTracker watermarkTracker,
+      TimestampAssigner<IcebergSourceSplit> timestampAssigner,
+      Options options) {
+    this.watermarkTracker = watermarkTracker;
+    this.state = new State(watermarkTracker, timestampAssigner);
+    this.options = options;
+    this.timestampAssigner = timestampAssigner;
+    this.futureNotifier = new FutureNotifier();
+    this.watermarkTracker.onInitialization();
+  }
+
+  @Override
+  public void start() {
+    watermarkTracker.addListener(this);
+  }
+
+  @Override
+  public void close() {
+    watermarkTracker.removeListener(this);
+    notifyListener();
+  }
+
+  @Override
+  public GetSplitResult getNext(@Nullable String hostname) {
+    GetSplitResult result = getNextInternal();
+    if (result.status() == GetSplitResult.Status.AVAILABLE) {
+      state.onSplitAssigned(result.split());
+    }
+    return result;
+  }
+
+  @Override
+  public void onDiscoveredSplits(Collection<IcebergSourceSplit> splits) {
+    state.onDiscoveredSplits(splits);
+  }
+
+  @Override
+  public void onUnassignedSplits(Collection<IcebergSourceSplit> splits) {
+    state.onUnassignedSplits(splits);
+  }
+
+  @Override
+  public void onCompletedSplits(Collection<String> completedSplitIds) {
+    state.onCompletedSplits(completedSplitIds);
+    if (!completedSplitIds.isEmpty() && isTerminal()) {
+      updateListenersOnTerminalCondition();
+    }
+  }
+
+  @Override
+  public void onNoMoreSplits() {
+    noMoreSplits = true;
+    if (isTerminal()) {
+      updateListenersOnTerminalCondition();
+    }
+  }
+
+  private boolean isWithinBounds(IcebergSourceSplit split, Long watermark) {
+    if (watermark == null) {
+      return true;
+    }
+
+    long splitTs = timestampAssigner.extractTimestamp(split, -1);
+    if (splitTs < watermark) {
+      log.warn("splitTs at {} is lower than the watermark {}", splitTs, watermark);
+    }
+
+    return Math.max(splitTs - watermark, 0L) <= options.getThresholdInMs();
+  }
+
+  private GetSplitResult getNextInternal() {
+    if (state.getUnassignedSplits().isEmpty()) {
+      log.info("looks like there are no splits to be assigned");
+      return GetSplitResult.unavailable();
+    }
+
+    try {
+      Long watermark = watermarkTracker.getGlobalWatermark();
+      IcebergSourceSplit pendingSplit = getFirst(state.getUnassignedSplits(), null);
+      if (!isWithinBounds(pendingSplit, watermark)) {
+        log.info(
+            "split {} is not within bounds {} {}",
+            pendingSplit,
+            watermark,
+            timestampAssigner.extractTimestamp(pendingSplit, -1));
+        return GetSplitResult.constrained();
+      }
+
+      return GetSplitResult.forSplit(pendingSplit);
+    } catch (Exception e) {
+      log.error("Couldn't obtain the watermark from the tracker", e);
+      return GetSplitResult.unavailable();
+    }
+  }
+
+  private void updateListenersOnTerminalCondition() {
+    state.onNoMoreStatusChanges();
+  }
+
+  @Override
+  public Collection<IcebergSourceSplitState> state() {
+    return
+        state
+            .getUnassignedSplits()
+            .stream()
+            .map(split -> new IcebergSourceSplitState(split, IcebergSourceSplitStatus.UNASSIGNED))
+            .collect(Collectors.toList());
+  }
+
+  @Override
+  public CompletableFuture<Void> isAvailable() {
+    CompletableFuture<Void> result = futureNotifier.future();
+    checkAndNotifyListener();
+    return result;
+  }
+
+  private boolean hasNext() {
+    return getNextInternal().status().equals(GetSplitResult.Status.AVAILABLE);
+  }
+
+  private void checkAndNotifyListener() {
+    if (hasNext()) {
+      log.info("Looks like the future can be completed with an assignment; completing the future");

Review Comment:
   This looks like more of a debug-level message to me; should we change it to `log.debug` to avoid unnecessary log messages?



##########
flink/v1.16/flink/src/main/java/org/apache/iceberg/flink/source/assigner/EventTimeAlignedAssigner.java:
##########
@@ -0,0 +1,301 @@
+/*
+ *
+ *  * 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.iceberg.flink.source.assigner;
+
+import java.io.Serializable;
+import java.time.Duration;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.Comparator;
+import java.util.Set;
+import java.util.TreeSet;
+import java.util.concurrent.CompletableFuture;
+import java.util.stream.Collectors;
+import javax.annotation.Nullable;
+import javax.annotation.concurrent.NotThreadSafe;
+import org.apache.flink.api.common.eventtime.TimestampAssigner;
+import org.apache.iceberg.flink.source.split.IcebergSourceSplit;
+import org.apache.iceberg.flink.source.split.IcebergSourceSplitState;
+import org.apache.iceberg.flink.source.split.IcebergSourceSplitStatus;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.apache.flink.calcite.shaded.com.google.common.collect.Iterables.getFirst;
+
+public class EventTimeAlignedAssigner implements SplitAssigner, WatermarkTracker.Listener {
+  private static final Logger log = LoggerFactory.getLogger(EventTimeAlignedAssigner.class);
+  private final WatermarkTracker watermarkTracker;
+  private final State state;
+  private final TimestampAssigner<IcebergSourceSplit> timestampAssigner;
+  private final FutureNotifier futureNotifier;
+  private final Options options;
+
+  // flag that informs the state that there are no more splits to be added by the Enumerator
+  private volatile boolean noMoreSplits;
+
+  public EventTimeAlignedAssigner(
+      WatermarkTracker watermarkTracker,
+      TimestampAssigner<IcebergSourceSplit> timestampAssigner,
+      Options options) {
+    this.watermarkTracker = watermarkTracker;
+    this.state = new State(watermarkTracker, timestampAssigner);
+    this.options = options;
+    this.timestampAssigner = timestampAssigner;
+    this.futureNotifier = new FutureNotifier();
+    this.watermarkTracker.onInitialization();
+  }
+
+  @Override
+  public void start() {
+    watermarkTracker.addListener(this);
+  }
+
+  @Override
+  public void close() {
+    watermarkTracker.removeListener(this);
+    notifyListener();
+  }
+
+  @Override
+  public GetSplitResult getNext(@Nullable String hostname) {
+    GetSplitResult result = getNextInternal();
+    if (result.status() == GetSplitResult.Status.AVAILABLE) {
+      state.onSplitAssigned(result.split());
+    }
+    return result;
+  }
+
+  @Override
+  public void onDiscoveredSplits(Collection<IcebergSourceSplit> splits) {
+    state.onDiscoveredSplits(splits);
+  }
+
+  @Override
+  public void onUnassignedSplits(Collection<IcebergSourceSplit> splits) {
+    state.onUnassignedSplits(splits);
+  }
+
+  @Override
+  public void onCompletedSplits(Collection<String> completedSplitIds) {
+    state.onCompletedSplits(completedSplitIds);
+    if (!completedSplitIds.isEmpty() && isTerminal()) {
+      updateListenersOnTerminalCondition();
+    }
+  }
+
+  @Override
+  public void onNoMoreSplits() {
+    noMoreSplits = true;
+    if (isTerminal()) {
+      updateListenersOnTerminalCondition();
+    }
+  }
+
+  private boolean isWithinBounds(IcebergSourceSplit split, Long watermark) {
+    if (watermark == null) {
+      return true;
+    }
+
+    long splitTs = timestampAssigner.extractTimestamp(split, -1);
+    if (splitTs < watermark) {
+      log.warn("splitTs at {} is lower than the watermark {}", splitTs, watermark);
+    }
+
+    return Math.max(splitTs - watermark, 0L) <= options.getThresholdInMs();
+  }
+
+  private GetSplitResult getNextInternal() {
+    if (state.getUnassignedSplits().isEmpty()) {
+      log.info("looks like there are no splits to be assigned");

Review Comment:
   nit: capitalize for consistent style with other log messages.



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

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


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@iceberg.apache.org
For additional commands, e-mail: issues-help@iceberg.apache.org