RussellSpitzer commented on a change in pull request #1211: URL: https://github.com/apache/iceberg/pull/1211#discussion_r459759164
########## 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( Review comment: I decided on original ---------------------------------------------------------------- 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]
