amogh-jahagirdar commented on code in PR #5669: URL: https://github.com/apache/iceberg/pull/5669#discussion_r985147394
########## core/src/main/java/org/apache/iceberg/IncrementalFileCleanup.java: ########## @@ -0,0 +1,342 @@ +/* + * 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.io.IOException; +import java.util.List; +import java.util.Set; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutorService; +import java.util.function.Consumer; +import org.apache.iceberg.exceptions.NotFoundException; +import org.apache.iceberg.exceptions.RuntimeIOException; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.relocated.com.google.common.collect.Iterables; +import org.apache.iceberg.relocated.com.google.common.collect.Sets; +import org.apache.iceberg.util.PropertyUtil; +import org.apache.iceberg.util.SnapshotUtil; +import org.apache.iceberg.util.Tasks; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +class IncrementalFileCleanup extends FileCleanupStrategy { + private static final Logger LOG = LoggerFactory.getLogger(IncrementalFileCleanup.class); + + private final TableMetadata base; + private final TableMetadata current; + private final Consumer<String> deleteFunc; + + IncrementalFileCleanup( + TableOperations ops, + TableMetadata base, + ExecutorService deleteExecutorService, + ExecutorService planExecutorService, + Consumer<String> deleteFunc) { + super(ops, deleteExecutorService, planExecutorService, deleteFunc); + this.base = base; + this.current = ops.refresh(); + this.deleteFunc = deleteFunc; + } + + @Override + @SuppressWarnings({"checkstyle:CyclomaticComplexity", "MethodLength"}) + public void cleanFiles() { + if (current.refs().size() > 1) { + throw new UnsupportedOperationException( + "Cannot incrementally clean files for tables with more than 1 ref"); + } + + // clean up the expired snapshots: + // 1. Get a list of the snapshots that were removed + // 2. Delete any data files that were deleted by those snapshots and are not in the table + // 3. Delete any manifests that are no longer used by current snapshots + // 4. Delete the manifest lists + + Set<Long> validIds = Sets.newHashSet(); + for (Snapshot snapshot : current.snapshots()) { + validIds.add(snapshot.snapshotId()); + } + + Set<Long> expiredIds = Sets.newHashSet(); + for (Snapshot snapshot : base.snapshots()) { + long snapshotId = snapshot.snapshotId(); + if (!validIds.contains(snapshotId)) { + // the snapshot was expired + LOG.info("Expired snapshot: {}", snapshot); + expiredIds.add(snapshotId); + } + } + + if (expiredIds.isEmpty()) { + // if no snapshots were expired, skip cleanup + return; + } + + // Reads and deletes are done using Tasks.foreach(...).suppressFailureWhenFinished to complete + // as much of the delete work as possible and avoid orphaned data or manifest files. + SnapshotRef branchToCleanup = Iterables.getFirst(base.refs().values(), null); + if (branchToCleanup == null) { + return; + } Review Comment: Added this logic, we still need https://github.com/apache/iceberg/pull/5666/files the fix if we want to support incremental file cleanups in the case that expiration is called on a table with only non-main commits. -- 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]
