amogh-jahagirdar commented on code in PR #14510:
URL: https://github.com/apache/iceberg/pull/14510#discussion_r2502923743


##########
api/src/main/java/org/apache/iceberg/SnapshotAncestryValidator.java:
##########
@@ -0,0 +1,54 @@
+/*
+ * 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;
+
+import java.util.function.Function;
+import javax.annotation.Nonnull;
+
+/**
+ * Interface to support validating snapshot ancestry during the commit process.
+ *
+ * <p>Validation will be called after the table metadata is refreshed to pick 
up any changes to the
+ * table state.
+ */
+@FunctionalInterface
+public interface SnapshotAncestryValidator extends 
Function<Iterable<Snapshot>, Boolean> {
+
+  SnapshotAncestryValidator NON_VALIDATING = baseSnapshots -> true;
+
+  /**
+   * Validate the snapshots based on the refreshed table state.
+   *
+   * @param baseSnapshots ancestry of the base table metadata snapshots
+   * @return boolean for whether the update is valid
+   */
+  @Override
+  Boolean apply(Iterable<Snapshot> baseSnapshots);

Review Comment:
   I'm a bit late to this since I see the interface went in another PR, but any 
reason this can't be primitive? Is this just a byproduct of extending Function



##########
kafka-connect/kafka-connect/src/main/java/org/apache/iceberg/connect/channel/Coordinator.java:
##########
@@ -288,6 +289,32 @@ private void commitToTable(
     }
   }
 
+  private SnapshotAncestryValidator offsetValidator(
+      TableIdentifier tableIdentifier, Map<Integer, Long> expectedOffsets) {
+
+    return new SnapshotAncestryValidator() {
+      private Map<Integer, Long> lastCommittedOffsets;
+
+      @Override
+      public Boolean apply(Iterable<Snapshot> baseSnapshots) {
+        lastCommittedOffsets = lastCommittedOffsets(baseSnapshots);
+
+        if (expectedOffsets.isEmpty() && lastCommittedOffsets.isEmpty()) {
+          return true; // there are no stored offsets, so assume we're 
starting with new offsets
+        }

Review Comment:
   I'm good either way here, one thing when I was re-reading this was that 
while the equals check will take care of it, it's nice to explicitly see the 
behavior when there are no offsets rather than have to more deeply read the 
code.



##########
kafka-connect/kafka-connect/src/main/java/org/apache/iceberg/connect/channel/Coordinator.java:
##########
@@ -302,21 +329,37 @@ private Snapshot latestSnapshot(Table table, String 
branch) {
 
   private Map<Integer, Long> lastCommittedOffsetsForTable(Table table, String 
branch) {
     Snapshot snapshot = latestSnapshot(table, branch);
-    while (snapshot != null) {
-      Map<String, String> summary = snapshot.summary();
-      String value = summary.get(snapshotOffsetsProp);
-      if (value != null) {
-        TypeReference<Map<Integer, Long>> typeRef = new 
TypeReference<Map<Integer, Long>>() {};
-        try {
-          return MAPPER.readValue(value, typeRef);
-        } catch (IOException e) {
-          throw new UncheckedIOException(e);
-        }
-      }
-      Long parentSnapshotId = snapshot.parentId();
-      snapshot = parentSnapshotId != null ? table.snapshot(parentSnapshotId) : 
null;
+
+    if (snapshot == null) {
+      return Map.of();
+    }
+
+    Iterable<Snapshot> branchAncestry =
+        SnapshotUtil.ancestorsOf(snapshot.snapshotId(), table::snapshot);
+    return lastCommittedOffsets(branchAncestry);
+  }
+
+  private Map<Integer, Long> lastCommittedOffsets(Iterable<Snapshot> 
snapshots) {
+    return Streams.stream(snapshots)
+        .filter(Objects::nonNull)
+        .filter(snapshot -> 
snapshot.summary().containsKey(snapshotOffsetsProp))
+        .map(snapshot -> snapshot.summary().get(snapshotOffsetsProp))
+        .map(this::parseOffsets)
+        .findFirst()
+        .orElseGet(Map::of);
+  }
+
+  private Map<Integer, Long> parseOffsets(String value) {
+    if (value == null) {
+      return Map.of();
+    }

Review Comment:
   It's not a valid state but I don't think it'd be possible to reach this 
point if the summary somehow has the offsets key with no values. Summary values 
are interpreted as string maps as per the spec, and I'd think the parser 
[implementation would 
fail](https://github.com/apache/iceberg/blob/main/core/src/main/java/org/apache/iceberg/SnapshotParser.java#L156)
 when trying to build the in-memory representation.
   
    Unless I'm missing something, I'd probably simplify this a bit, writing it 
with the assumption that there is some structured value associated with the 
key, and just keep 357-361 so we handle the failure case where the offsets have 
different types or a different structure than expected. 



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to