mxm commented on code in PR #17901:
URL: https://github.com/apache/iceberg/pull/17901#discussion_r4613643784
##########
flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/SinkUtil.java:
##########
@@ -141,6 +144,67 @@ static long getMaxCommittedCheckpointId(
return lastCommittedCheckpointId;
}
+ /**
+ * Validates, inside the commit, that the target branch does not already
contain a commit for the
+ * staged checkpoint.
+ *
+ * <p>The committers read the max committed checkpoint ID before they
commit. Another commit for
+ * the same checkpoint can land after that read, for example when a slow
commit succeeds after the
+ * committer gave up and the request is redelivered on recovery, or when an
old and a new job
+ * commit the same restored checkpoint. The base ancestry is refreshed
before every commit
+ * attempt, so this validator sees such a commit and rejects the duplicate
with a {@link
+ * MaxCommittedCheckpointMismatchException}.
+ *
+ * <p>Shared by {@link IcebergCommitter} and {@code DynamicCommitter}, which
identify their own
+ * snapshots differently, so the caller supplies the {@code
writtenBySameSink} predicate.
+ */
+ @Internal
+ public static class MaxCommittedCheckpointIdValidator implements
SnapshotAncestryValidator {
+ private final long stagedCheckpointId;
+ private final Predicate<Map<String, String>> writtenBySameSink;
+
+ /**
+ * @param stagedCheckpointId the checkpoint ID that is being committed
+ * @param writtenBySameSink returns true for snapshot summaries that the
committing sink wrote
+ */
+ public MaxCommittedCheckpointIdValidator(
+ long stagedCheckpointId, Predicate<Map<String, String>>
writtenBySameSink) {
+ this.stagedCheckpointId = stagedCheckpointId;
+ this.writtenBySameSink = writtenBySameSink;
+ }
Review Comment:
Both callers now pass the same predicate (`writtenBySameSink(summary,
sinkId, operatorId)`). Could the validator take `sinkId` and `operatorId`
instead, as the private class in `DynamicCommitter` did? We can then move the
logic of `writtenBySameSink` into the validator as well.
##########
flink/v2.3/flink/src/main/java/org/apache/iceberg/flink/sink/SinkUtil.java:
##########
@@ -141,6 +144,67 @@ static long getMaxCommittedCheckpointId(
return lastCommittedCheckpointId;
}
+ /**
+ * Validates, inside the commit, that the target branch does not already
contain a commit for the
+ * staged checkpoint.
+ *
+ * <p>The committers read the max committed checkpoint ID before they
commit. Another commit for
+ * the same checkpoint can land after that read, for example when a slow
commit succeeds after the
+ * committer gave up and the request is redelivered on recovery, or when an
old and a new job
+ * commit the same restored checkpoint. The base ancestry is refreshed
before every commit
+ * attempt, so this validator sees such a commit and rejects the duplicate
with a {@link
+ * MaxCommittedCheckpointMismatchException}.
+ *
+ * <p>Shared by {@link IcebergCommitter} and {@code DynamicCommitter}, which
identify their own
+ * snapshots differently, so the caller supplies the {@code
writtenBySameSink} predicate.
+ */
Review Comment:
NIT: Could we trim this?
```suggestion
/**
* 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.
*/
```
##########
flink/v2.3/flink/src/test/java/org/apache/iceberg/flink/sink/TestIcebergCommitterDuplicateCommit.java:
##########
@@ -0,0 +1,129 @@
+/*
+ * 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.sink;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.Collections;
+import java.util.Map;
+import org.apache.flink.api.connector.sink2.mocks.MockCommitRequest;
+import org.apache.flink.core.io.SimpleVersionedSerialization;
+import org.apache.flink.metrics.groups.UnregisteredMetricsGroup;
+import org.apache.iceberg.DataFile;
+import org.apache.iceberg.DataFiles;
+import org.apache.iceberg.Metrics;
+import org.apache.iceberg.PartitionSpec;
+import org.apache.iceberg.SnapshotRef;
+import org.apache.iceberg.Table;
+import org.apache.iceberg.TableProperties;
+import org.apache.iceberg.flink.SimpleDataUtil;
+import org.apache.iceberg.flink.TableLoader;
+import org.apache.iceberg.io.WriteResult;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
+
+/**
+ * Two committers commit the same checkpoint, as when a slow commit lands
after the job restarted
+ * and the restored committable is committed again (apache/iceberg#14425). The
committer that reads
+ * the committed checkpoint id from a table view taken before the other commit
landed must not
+ * commit the checkpoint a second time.
+ */
+class TestIcebergCommitterDuplicateCommit {
Review Comment:
Could we move this test into `TestIcebergCommitter`?
--
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]