RussellSpitzer commented on a change in pull request #1211: URL: https://github.com/apache/iceberg/pull/1211#discussion_r459761725
########## File path: core/src/main/java/org/apache/iceberg/util/ExpireSnapshotUtil.java ########## @@ -0,0 +1,388 @@ +/* + * 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.util; + +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.List; +import java.util.Set; +import java.util.stream.Collectors; +import org.apache.iceberg.GenericManifestFile; +import org.apache.iceberg.ManifestFile; +import org.apache.iceberg.Schema; +import org.apache.iceberg.Snapshot; +import org.apache.iceberg.SnapshotSummary; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.avro.Avro; +import org.apache.iceberg.io.CloseableIterable; +import org.apache.iceberg.io.FileIO; +import org.apache.iceberg.relocated.com.google.common.collect.Sets; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +public class ExpireSnapshotUtil { + + /** + * Determines the manifest files which need to be inspected because they refer to data files which + * can be removed after a Snapshot Expiration. + * + * Our goal is to determine which manifest files we actually need to read through because they + * may refer to files which are no longer accessible from any valid snapshot and do not effect + * the current table. + * + * For this we need to look through + * 1. Snapshots which have not expired but contain manifests from expired snapshots + * 2. Snapshots which have expired and contain manifests referring to now orphaned files + * + * @param validIds The Ids of the Snapshots which have not been expired + * @param expiredIds The Ids of the Snapshots which have been expired + * @param currentMetadata The table metadata from after the snapshot expiration + * @param originalMetadata The table metadata from before the snapshot expiration + * @param io FileIO for reading manifest info + * @return + */ + public static ManifestExpirationChanges determineManifestChangesFromSnapshotExpiration(Set<Long> validIds, + Set<Long> expiredIds, TableMetadata currentMetadata, TableMetadata originalMetadata, FileIO io) { + + List<Snapshot> currentSnapshots = currentMetadata.snapshots(); + + //Snapshots which are not expired but refer to manifests from expired snapshots + Set<ManifestFile> validManifests = getValidManifests(currentSnapshots, io); + Set<ManifestFile> manifestsToScan = validManifestsInExpiredSnapshots(validManifests, + originalMetadata, validIds); + + //Snapshots which are expired and do not effect the current table + List<Snapshot> snapshotsNotChangingTableState = snapshotsNotInTableState(validIds, originalMetadata); + ManifestExpirationChanges manifestExpirationChanges = + findExpiredManifestsInUnusedSnapshots(snapshotsNotChangingTableState, validManifests, + originalMetadata, expiredIds, io); + + manifestExpirationChanges.manifestsToScan().addAll(manifestsToScan); + return manifestExpirationChanges; + } + + /** + * Compares the Snapshots from the two TableMetadata objects and identifies the snapshots + * still in use and those no longer in use + * @param currentMetadata Metadata from a table after an expiration of snapshots + * @param originalMetadata Metada from the table before expiration of snapshots + * @return + */ + public static SnapshotExpirationChanges getExpiredSnapshots( + TableMetadata currentMetadata, TableMetadata originalMetadata) { + + Set<Long> validIds = Sets.newHashSet(); + for (Snapshot snapshot : currentMetadata.snapshots()) { + validIds.add(snapshot.snapshotId()); + } + + Set<Long> expiredIds = Sets.newHashSet(); + for (Snapshot snapshot : originalMetadata.snapshots()) { + long snapshotId = snapshot.snapshotId(); + if (!validIds.contains(snapshotId)) { + // This snapshot is no longer in the updated metadata + LOG.info("Expired snapshot: {}", snapshot); + expiredIds.add(snapshotId); + } + } + + return new SnapshotExpirationChanges(validIds, expiredIds); + } + + //Utility Class No Instantiation Allowed + private ExpireSnapshotUtil() {} + + private static final Logger LOG = LoggerFactory.getLogger(ExpireSnapshotUtil.class); + + private static Set<Long> getPickedAncestorIds(TableMetadata currentMetadata, Set<Long> ancestorIds) { + // this is the set of ancestors of the current table state. when removing snapshots, this must + // only remove files that were deleted in an ancestor of the current table state to avoid + // physically deleting files that were logically deleted in a commit that was rolled back. + + Set<Long> pickedAncestorSnapshotIds = Sets.newHashSet(); + for (long snapshotId : ancestorIds) { + String sourceSnapshotId = currentMetadata.snapshot(snapshotId).summary() + .get(SnapshotSummary.SOURCE_SNAPSHOT_ID_PROP); + if (sourceSnapshotId != null) { + // protect any snapshot that was cherry-picked into the current table state + pickedAncestorSnapshotIds.add(Long.parseLong(sourceSnapshotId)); + } + } + + return pickedAncestorSnapshotIds; + } + + /** + * Given a list of currently valid snapshots, extract all the manifests from those snapshots. If + * there is an error while reading manifest lists an incomplete list of manifests will be + * produced. + * + * @param currentSnapshots a list of currently valid non-expired snapshots + * @return all of the manifests of those snapshots + */ + private static Set<ManifestFile> getValidManifests(List<Snapshot> currentSnapshots, FileIO io) { + + Set<ManifestFile> validManifests = Sets.newHashSet(); + Tasks.foreach(currentSnapshots).retry(3).suppressFailureWhenFinished() + .onFailure((snapshot, exc) -> + LOG.warn("Failed on snapshot {} while reading manifest list: {}", snapshot.snapshotId(), + snapshot.manifestListLocation(), exc)) + .run( + snapshot -> { + try (CloseableIterable<ManifestFile> manifests = readManifestFiles(snapshot, io)) { + for (ManifestFile manifest : manifests) { + validManifests.add(manifest); + } + } catch (IOException e) { + throw new UncheckedIOException( + String.format("Failed to close manifest list: %s", + snapshot.manifestListLocation()), + e); + } + }); + return validManifests; + } + + /** + * Find manifests to clean up that are still referenced by a valid snapshot, but written by an + * expired snapshot. + * + * @param validSnapshotIds A list of the snapshots which are not expired + * @param originalMeta A reference to the table before expiration + * @return MetadataFiles which must be scanned to look for files to delete + */ + private static Set<ManifestFile> validManifestsInExpiredSnapshots( + Set<ManifestFile> validManfiests, TableMetadata originalMeta, Set<Long> validSnapshotIds) { + + Set<Long> ancestorIds = SnapshotUtil.ancestorIds(originalMeta.currentSnapshot(), originalMeta::snapshot) + .stream().collect(Collectors.toSet()); + Set<Long> pickedAncestorSnapshotIds = getPickedAncestorIds(originalMeta, ancestorIds); + + Set<ManifestFile> manifestsToScan = Sets.newHashSet(); + validManfiests.forEach(manifest -> { + long snapshotId = manifest.snapshotId(); + // whether the manifest was created by a valid snapshot (true) or an expired snapshot (false) + boolean fromValidSnapshots = validSnapshotIds.contains(snapshotId); + // whether the snapshot that created the manifest was an ancestor of the table state + boolean isFromAncestor = ancestorIds.contains(snapshotId); + // whether the changes in this snapshot have been picked into the current table state + boolean isPicked = pickedAncestorSnapshotIds.contains(snapshotId); + // if the snapshot that wrote this manifest is no longer valid (has expired), + // then delete its deleted files. note that this is only for expired snapshots that are in the + // current table state + if (!fromValidSnapshots && (isFromAncestor || isPicked) && manifest.hasDeletedFiles()) { + manifestsToScan.add(manifest.copy()); + } + }); + return manifestsToScan; + } + + /** + * Removes snapshots whose changes impact the current table state leaving only those which may + * have files that could potentially need to be deleted. + * + * @param originalMeta TableMetadata for the table we are expiring from + * @param validSnapshotIds Snapshots which are not expired + * @return A list of those snapshots which may have files that need to be deleted + */ + private static List<Snapshot> snapshotsNotInTableState(Set<Long> validSnapshotIds, TableMetadata originalMeta) { + + Set<Long> ancestorIds = SnapshotUtil.ancestorIds(originalMeta.currentSnapshot(), originalMeta::snapshot) Review comment: I think only by passing it as an arg ... I"ll try that out ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
