rodmeneses commented on code in PR #10179: URL: https://github.com/apache/iceberg/pull/10179#discussion_r1626390005
########## flink/v1.19/flink/src/main/java/org/apache/iceberg/flink/sink/committer/IcebergCommitter.java: ########## @@ -0,0 +1,434 @@ +/* + * 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.committer; + +import java.io.IOException; +import java.io.Serializable; +import java.util.Arrays; +import java.util.Collection; +import java.util.List; +import java.util.Map; +import java.util.NavigableMap; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.TimeUnit; +import org.apache.flink.api.connector.sink2.Committer; +import org.apache.flink.core.io.SimpleVersionedSerialization; +import org.apache.iceberg.AppendFiles; +import org.apache.iceberg.ManifestFile; +import org.apache.iceberg.ReplacePartitions; +import org.apache.iceberg.RowDelta; +import org.apache.iceberg.Snapshot; +import org.apache.iceberg.SnapshotUpdate; +import org.apache.iceberg.Table; +import org.apache.iceberg.flink.TableLoader; +import org.apache.iceberg.flink.sink.CommitSummary; +import org.apache.iceberg.flink.sink.DeltaManifests; +import org.apache.iceberg.flink.sink.DeltaManifestsSerializer; +import org.apache.iceberg.flink.sink.FlinkManifestUtil; +import org.apache.iceberg.flink.sink.IcebergFilesCommitterMetrics; +import org.apache.iceberg.io.WriteResult; +import org.apache.iceberg.relocated.com.google.common.annotations.VisibleForTesting; +import org.apache.iceberg.relocated.com.google.common.base.MoreObjects; +import org.apache.iceberg.relocated.com.google.common.base.Preconditions; +import org.apache.iceberg.relocated.com.google.common.collect.Lists; +import org.apache.iceberg.relocated.com.google.common.collect.Maps; +import org.apache.iceberg.util.PropertyUtil; +import org.apache.iceberg.util.ThreadPools; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * This class implements the Flink SinkV2 {@link Committer} interface to implement the Iceberg + * commits. The implementation builds on the following assumptions: + * + * <ul> + * <li>There is a single {@link IcebergCommittable} for every checkpoint + * <li>There is no late checkpoint - if checkpoint 'x' has received in one call, then after a + * successful run only checkpoints > x will arrive + * <li>There is no other writer which would generate another commit to the same branch with the + * same jobId-operatorId-checkpointId triplet + * </ul> + */ +public class IcebergCommitter implements Committer<IcebergCommittable>, Serializable { + private static final long serialVersionUID = 1L; + private static final String MAX_COMMITTED_CHECKPOINT_ID = "flink.max-committed-checkpoint-id"; + private static final Logger LOG = LoggerFactory.getLogger(IcebergCommitter.class); + private static final byte[] EMPTY_MANIFEST_DATA = new byte[0]; + private static final CommitRequest<IcebergCommittable> EMPTY_COMMIT_REQUEST = + new CommitRequest<IcebergCommittable>() { + @Override + public IcebergCommittable getCommittable() { + return new IcebergCommittable(EMPTY_MANIFEST_DATA, "jobId", "operatorId", -1L); + } + + @Override + public int getNumberOfRetries() { + return 0; + } + + @Override + public void signalFailedWithKnownReason(Throwable t) {} + + @Override + public void signalFailedWithUnknownReason(Throwable t) {} + + @Override + public void retryLater() {} + + @Override + public void updateAndRetryLater(IcebergCommittable committable) {} + + @Override + public void signalAlreadyCommitted() {} + }; + + @VisibleForTesting + static final String MAX_CONTINUOUS_EMPTY_COMMITS = "flink.max-continuous-empty-commits"; + + public static final long INITIAL_CHECKPOINT_ID = -1L; + public static final String FLINK_JOB_ID = "flink.job-id"; + public static final String OPERATOR_ID = "flink.operator-id"; + private final String branch; + private final Map<String, String> snapshotProperties; + private final boolean replacePartitions; + private final int workerPoolSize; + private final String prefix; + private transient IcebergFilesCommitterMetrics committerMetrics; + private transient Table table; + private transient int maxContinuousEmptyCommits; + private transient ExecutorService workerPool; + private transient int continuousEmptyCheckpoints = 0; + + public IcebergCommitter( + TableLoader tableLoader, + String branch, + Map<String, String> snapshotProperties, + boolean replacePartitions, + int workerPoolSize, + String prefix, + IcebergFilesCommitterMetrics committerMetrics) { + this.branch = branch; + this.snapshotProperties = snapshotProperties; + this.replacePartitions = replacePartitions; + this.workerPoolSize = workerPoolSize; + this.prefix = prefix; + this.committerMetrics = committerMetrics; + + if (!tableLoader.isOpen()) { + tableLoader.open(); + } + + this.table = tableLoader.loadTable(); + this.maxContinuousEmptyCommits = + PropertyUtil.propertyAsInt(table.properties(), MAX_CONTINUOUS_EMPTY_COMMITS, 10); + Preconditions.checkArgument( + maxContinuousEmptyCommits > 0, MAX_CONTINUOUS_EMPTY_COMMITS + " must be positive"); + this.workerPool = + ThreadPools.newWorkerPool("iceberg-worker-pool-" + table + "-" + prefix, workerPoolSize); + this.continuousEmptyCheckpoints = 0; + } + + @Override + public void commit(Collection<CommitRequest<IcebergCommittable>> commitRequests) + throws IOException, InterruptedException { + NavigableMap<Long, byte[]> manifestMap = Maps.newTreeMap(); + NavigableMap<Long, CommitRequest<IcebergCommittable>> commitRequestMap = Maps.newTreeMap(); + + if (commitRequests.isEmpty()) { + commitRequests.add(EMPTY_COMMIT_REQUEST); + } + + for (CommitRequest<IcebergCommittable> request : commitRequests) { + manifestMap.put(request.getCommittable().checkpointId(), request.getCommittable().manifest()); + commitRequestMap.put(request.getCommittable().checkpointId(), request); + } + + long maxCommittedCheckpointId = getMaxCommittedCheckpointId(commitRequestMap); + + // Mark the already committed FilesCommittable(s) as finished + commitRequestMap + .headMap(maxCommittedCheckpointId, true) + .values() + .forEach(CommitRequest::signalAlreadyCommitted); + + // Commit the remaining + IcebergCommittable last = commitRequestMap.lastEntry().getValue().getCommittable(); + + NavigableMap<Long, byte[]> uncommitted = + Maps.newTreeMap(manifestMap).tailMap(maxCommittedCheckpointId, false); + if (!uncommitted.isEmpty()) { + commitUpToCheckpoint( + commitRequestMap, uncommitted, last.jobId(), last.operatorId(), last.checkpointId()); + } + } + + /** + * Gets the last checkpointId which is committed to this branch of the table. The commits are + * identified by the {@link IcebergCommittable} (jobId/operatorId/checkpointId). Only used by + * {@link IcebergCommittable} (SinkV2). + * + * @param requests The {@link IcebergCommittable}s ordered by checkpointId + * @return The checkpointId for the first commit (historically backward) + */ + public long getMaxCommittedCheckpointId( Review Comment: the new `getMaxCommittedCheckpointId` has a totally different signature from the existing `IcebergFilesCommitter.getMaxCommittedCheckpointId`, so I don't think it is worth refactoring it which will require approches like ---- 1. Create a new `IcebergComitterCommon` class 2. create generic static `getMaxCommittedCheckpointId` 3. Refactor current `IcebergFilesCommitter.getMaxCommittedCheckpointId` to use the new static method 4. Refactor the new `IcebergCommitter.getMaxCommittedCheckpointId` OR ---- 1. Make both `IcebergFilesCommitter.getMaxCommittedCheckpointId` and even `IcebergFilesCommitter` public 2. refactor `IcebergFilesCommitter.getMaxCommittedCheckpointId` 3. Have the new `IcebergComitter` to use `IcebergFilesCommitter.getMaxCommittedCheckpointId` -- 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-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org