This is an automated email from the ASF dual-hosted git repository.

mxm pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/iceberg.git


The following commit(s) were added to refs/heads/main by this push:
     new 037d8af0eb Flink: Validate concurrent commits in IcebergSink to 
prevent commit duplication (#17901)
037d8af0eb is described below

commit 037d8af0eb085b75ed14c3212e619d6486bb636f
Author: fmorillo7694 <[email protected]>
AuthorDate: Thu Nov 19 17:27:15 2026 +0100

    Flink: Validate concurrent commits in IcebergSink to prevent commit 
duplication (#17901)
---
 .../iceberg/flink/sink/IcebergCommitter.java       | 20 +++++++-
 .../org/apache/iceberg/flink/sink/SinkUtil.java    | 53 ++++++++++++++++++++++
 .../flink/sink/dynamic/DynamicCommitter.java       | 37 ++-------------
 .../iceberg/flink/sink/TestIcebergCommitter.java   | 39 ++++++++++++++++
 4 files changed, 114 insertions(+), 35 deletions(-)

diff --git 
a/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/IcebergCommitter.java
 
b/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/IcebergCommitter.java
index 2b28de7a93..bde568c1dc 100644
--- 
a/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/IcebergCommitter.java
+++ 
b/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/IcebergCommitter.java
@@ -326,8 +326,26 @@ class IcebergCommitter implements 
Committer<IcebergCommittable> {
     operation.set(SinkUtil.OPERATOR_ID, operatorId);
     operation.toBranch(branch);
 
+    // Check again inside the commit, because another commit for this 
checkpoint can land after the
+    // up-front getMaxCommittedCheckpointIdBySink() read.
+    operation.validateWith(
+        new SinkUtil.MaxCommittedCheckpointIdValidator(checkpointId, sinkId, 
operatorId));
+
     long startNano = System.nanoTime();
-    operation.commit(); // abort is automatically called if this fails.
+    try {
+      operation.commit(); // abort is automatically called if this fails.
+    } catch (SinkUtil.MaxCommittedCheckpointMismatchException e) {
+      LOG.info(
+          "Skipping commit operation {} because the {} branch of the {} table 
already contains changes for checkpoint {}."
+              + " This can occur when a failure prevents the committer from 
receiving confirmation of a"
+              + " successful commit, causing the Flink job to retry committing 
the same set of changes.",
+          description,
+          branch,
+          table.name(),
+          checkpointId,
+          e);
+      return;
+    }
     long durationMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - 
startNano);
     LOG.info(
         "Committed {} to table: {}, branch: {}, checkpointId {} in {} ms",
diff --git 
a/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/SinkUtil.java 
b/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/SinkUtil.java
index cd93853d45..d2b2749e8b 100644
--- a/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/SinkUtil.java
+++ b/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/SinkUtil.java
@@ -34,7 +34,9 @@ import javax.annotation.Nullable;
 import org.apache.flink.annotation.Internal;
 import org.apache.iceberg.FileFormat;
 import org.apache.iceberg.Snapshot;
+import org.apache.iceberg.SnapshotAncestryValidator;
 import org.apache.iceberg.Table;
+import org.apache.iceberg.exceptions.ValidationException;
 import org.apache.iceberg.flink.FlinkWriteConf;
 import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
 import org.apache.iceberg.relocated.com.google.common.collect.Maps;
@@ -141,6 +143,57 @@ public class SinkUtil {
     return lastCommittedCheckpointId;
   }
 
+  /**
+   * Rejects a commit when the branch already contains a commit for the staged 
checkpoint ID.
+   *
+   * <p>Throws {@link MaxCommittedCheckpointMismatchException} instead of 
returning false. Callers
+   * must catch it to skip the duplicate commit.
+   */
+  @Internal
+  public static class MaxCommittedCheckpointIdValidator implements 
SnapshotAncestryValidator {
+    private final long stagedCheckpointId;
+    private final String sinkId;
+    private final String operatorId;
+
+    public MaxCommittedCheckpointIdValidator(
+        long stagedCheckpointId, String sinkId, String operatorId) {
+      this.stagedCheckpointId = stagedCheckpointId;
+      this.sinkId = sinkId;
+      this.operatorId = operatorId;
+    }
+
+    @Override
+    public boolean validate(Iterable<Snapshot> baseSnapshots) {
+      long maxCommittedCheckpointId = INITIAL_CHECKPOINT_ID;
+      for (Snapshot ancestor : baseSnapshots) {
+        Map<String, String> summary = ancestor.summary();
+        if (writtenBySameSink(summary, sinkId, operatorId)) {
+          String value = summary.get(MAX_COMMITTED_CHECKPOINT_ID);
+          if (value != null) {
+            maxCommittedCheckpointId = Long.parseLong(value);
+            break;
+          }
+        }
+      }
+
+      if (maxCommittedCheckpointId >= stagedCheckpointId) {
+        throw new MaxCommittedCheckpointMismatchException();
+      }
+
+      return true;
+    }
+  }
+
+  /**
+   * Thrown by {@link MaxCommittedCheckpointIdValidator} when the branch 
already contains a commit
+   * for the staged checkpoint. Committers catch it to skip the duplicate 
commit.
+   */
+  public static class MaxCommittedCheckpointMismatchException extends 
ValidationException {
+    public MaxCommittedCheckpointMismatchException() {
+      super("Table already contains staged changes.");
+    }
+  }
+
   /**
    * Based on the {@link FileFormat} overwrites the table level compression 
properties for the table
    * write.
diff --git 
a/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/dynamic/DynamicCommitter.java
 
b/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/dynamic/DynamicCommitter.java
index aeabcd51e0..611b8005dd 100644
--- 
a/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/dynamic/DynamicCommitter.java
+++ 
b/flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/dynamic/DynamicCommitter.java
@@ -38,14 +38,12 @@ import org.apache.iceberg.ManifestFile;
 import org.apache.iceberg.ReplacePartitions;
 import org.apache.iceberg.RowDelta;
 import org.apache.iceberg.Snapshot;
-import org.apache.iceberg.SnapshotAncestryValidator;
 import org.apache.iceberg.SnapshotSummary;
 import org.apache.iceberg.SnapshotUpdate;
 import org.apache.iceberg.Table;
 import org.apache.iceberg.TableUtil;
 import org.apache.iceberg.catalog.Catalog;
 import org.apache.iceberg.catalog.TableIdentifier;
-import org.apache.iceberg.exceptions.ValidationException;
 import org.apache.iceberg.flink.sink.CommitSummary;
 import org.apache.iceberg.flink.sink.DeltaManifests;
 import org.apache.iceberg.flink.sink.DeltaManifestsSerializer;
@@ -348,36 +346,6 @@ class DynamicCommitter implements 
Committer<DynamicCommittable> {
     }
   }
 
-  private static class MaxCommittedCheckpointMismatchException extends 
ValidationException {
-    private MaxCommittedCheckpointMismatchException() {
-      super("Table already contains staged changes.");
-    }
-  }
-
-  private static class MaxCommittedCheckpointIdValidator implements 
SnapshotAncestryValidator {
-    private final long stagedCheckpointId;
-    private final String sinkId;
-    private final String flinkOperatorId;
-
-    private MaxCommittedCheckpointIdValidator(
-        long stagedCheckpointId, String sinkId, String flinkOperatorId) {
-      this.stagedCheckpointId = stagedCheckpointId;
-      this.sinkId = sinkId;
-      this.flinkOperatorId = flinkOperatorId;
-    }
-
-    @Override
-    public boolean validate(Iterable<Snapshot> baseSnapshots) {
-      long maxCommittedCheckpointId =
-          getMaxCommittedCheckpointId(baseSnapshots, sinkId, flinkOperatorId);
-      if (maxCommittedCheckpointId >= stagedCheckpointId) {
-        throw new MaxCommittedCheckpointMismatchException();
-      }
-
-      return true;
-    }
-  }
-
   @VisibleForTesting
   void commitOperation(
       Table table,
@@ -406,12 +374,13 @@ class DynamicCommitter implements 
Committer<DynamicCommittable> {
 
     operation.set(OPERATOR_ID, operatorId);
     operation.toBranch(branch);
-    operation.validateWith(new MaxCommittedCheckpointIdValidator(checkpointId, 
sinkId, operatorId));
+    operation.validateWith(
+        new SinkUtil.MaxCommittedCheckpointIdValidator(checkpointId, sinkId, 
operatorId));
 
     long startNano = System.nanoTime();
     try {
       operation.commit(); // abort is automatically called if this fails.
-    } catch (MaxCommittedCheckpointMismatchException e) {
+    } catch (SinkUtil.MaxCommittedCheckpointMismatchException e) {
       LOG.info(
           "Skipping commit operation {} because the {} branch of the {} table 
already contains changes for checkpoint {}."
               + " This can occur when a failure prevents the committer from 
receiving confirmation of a"
diff --git 
a/flink/v2.3/flink/src/test/java/org/apache/iceberg/flink/sink/TestIcebergCommitter.java
 
b/flink/v2.3/flink/src/test/java/org/apache/iceberg/flink/sink/TestIcebergCommitter.java
index bfd23331f0..1b8b6870ee 100644
--- 
a/flink/v2.3/flink/src/test/java/org/apache/iceberg/flink/sink/TestIcebergCommitter.java
+++ 
b/flink/v2.3/flink/src/test/java/org/apache/iceberg/flink/sink/TestIcebergCommitter.java
@@ -47,6 +47,8 @@ import org.apache.flink.api.common.TaskInfo;
 import org.apache.flink.api.common.typeutils.TypeSerializer;
 import org.apache.flink.api.common.typeutils.TypeSerializerSnapshot;
 import org.apache.flink.api.connector.sink2.Committer;
+import org.apache.flink.api.connector.sink2.mocks.MockCommitRequest;
+import org.apache.flink.core.io.SimpleVersionedSerialization;
 import org.apache.flink.core.memory.DataInputView;
 import org.apache.flink.core.memory.DataOutputView;
 import org.apache.flink.runtime.checkpoint.OperatorSubtaskState;
@@ -333,6 +335,43 @@ class TestIcebergCommitter extends TestBase {
     SimpleDataUtil.assertTableRows(table, 
ImmutableList.of(rowFromPreviousRun), branch);
   }
 
+  @TestTemplate
+  void staleCommitterDoesNotCommitCheckpointTwice() throws Exception {
+    // Two committers for the same sink read the table at construction. The 
first one commits
+    // checkpoint 1, so the second one holds a stale pre-commit view and would 
otherwise commit
+    // the same checkpoint again (apache/iceberg#14425). The in-commit 
validator must reject the
+    // duplicate so only one snapshot lands.
+    IcebergCommitter stale = getCommitter();
+    IcebergCommitter other = getCommitter();
+
+    other.commit(Lists.newArrayList(duplicateCommitRequest(1, dataFileTest1)));
+    stale.commit(Lists.newArrayList(duplicateCommitRequest(1, dataFileTest2)));
+
+    assertSnapshotSize(1);
+    assertMaxCommittedCheckpointId(jobId, 1);
+  }
+
+  private MockCommitRequest<IcebergCommittable> duplicateCommitRequest(
+      long checkpointId, DataFile dataFile) throws IOException {
+    DeltaManifests manifests =
+        FlinkManifestUtil.writeCompletedFiles(
+            WriteResult.builder().addDataFiles(dataFile).build(),
+            () ->
+                FlinkManifestUtil.createOutputFileFactory(
+                        () -> table, table.properties(), jobId, OPERATOR_ID, 
0, 0)
+                    .create(checkpointId),
+            table.spec(),
+            formatVersion);
+    return new MockCommitRequest<>(
+        new IcebergCommittable(
+            SimpleVersionedSerialization.writeVersionAndSerialize(
+                DeltaManifestsSerializer.INSTANCE, manifests),
+            jobId,
+            SINK_ID,
+            OPERATOR_ID,
+            checkpointId));
+  }
+
   @TestTemplate
   public void testCommitsWithNewSinkIdAfterUpgrade() throws Exception {
     RowData rowFromPreviousRun = SimpleDataUtil.createRowData(1, "hello1");

Reply via email to