ahmedabu98 commented on code in PR #41882: URL: https://github.com/apache/beam/pull/41882#discussion_r4776413329
########## sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/maintenance/SetDifference.java: ########## @@ -0,0 +1,191 @@ +/* + * 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.beam.sdk.io.iceberg.maintenance; + +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkState; + +import java.io.Serializable; +import java.util.Collections; +import java.util.List; +import org.apache.beam.sdk.coders.Coder; +import org.apache.beam.sdk.coders.KvCoder; +import org.apache.beam.sdk.coders.StringUtf8Coder; +import org.apache.beam.sdk.metrics.Counter; +import org.apache.beam.sdk.metrics.Metrics; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.GroupByKey; +import org.apache.beam.sdk.transforms.PTransform; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.transforms.SerializableFunction; +import org.apache.beam.sdk.transforms.WithKeys; +import org.apache.beam.sdk.values.KV; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.TypeDescriptors; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * Selects the files that can be deleted: the deletion candidates ({@link FileInfo#getReachable()} + * is false) that no reachable file matches. + * + * <p>The input is every file the operation considered: the files reachable from table state that + * must be preserved, and the deletion candidates. Files are grouped by a key, and each group is + * resolved by a {@link Policy}, which chooses the candidates in the group to delete. + * + * <p>{@link #exactLocations()} matches files the way Iceberg's Spark {@code ExpireSnapshots} action + * does: by exact location, within one table. A candidate is protected only by a reachable file of + * the same table with the same location string. Running several tables through one transform gives + * the same result as running each table alone; files shared between tables are protected by the + * table's {@code gc.enabled} property, not by matching. It emits each location at most once, which + * also removes duplicate candidates such as a Puffin file that holds several deletion vectors. Only + * the location is compared, not the file category, as in Iceberg core's own reachability cleanup; + * this protects at least the files that comparing both would. + * + * <p>Metrics: {@code candidatesEmitted} counts emitted candidates, and {@code candidatesProtected} + * counts keys whose candidates were all protected. Both are attempted values, so retried bundles + * may be counted more than once. + */ +class SetDifference extends PTransform<PCollection<FileInfo>, PCollection<FileInfo>> { + + static final String CANDIDATES_PROTECTED = "candidatesProtected"; + static final String CANDIDATES_EMITTED = "candidatesEmitted"; + + /** Decides how files are matched, and which candidates a group of matching files emits. */ + interface Policy extends Serializable { + + /** Returns the key under which {@code file} is matched against other files. */ + String key(FileInfo file); + + /** + * Returns the candidates to delete from a group of files that share a key, which may be empty. + * Every returned file must be a distinct member of {@code group} that is not reachable. + */ + List<FileInfo> decide(Iterable<FileInfo> group); + } + + private final Policy policy; + + private SetDifference(Policy policy) { + this.policy = policy; + } + + /** Matches files by exact location within one table, like Spark's {@code ExpireSnapshots}. */ + static SetDifference exactLocations() { + return new SetDifference(new ExactLocationPolicy()); + } + + /** Matches files with {@code policy}. */ + static SetDifference withPolicy(Policy policy) { + return new SetDifference(policy); + } + + @Override + public PCollection<FileInfo> expand(PCollection<FileInfo> files) { + Coder<FileInfo> fileInfoCoder = files.getCoder(); + Policy policy = this.policy; + return files + .apply( + "Key", + WithKeys.of((SerializableFunction<FileInfo, String>) policy::key) + .withKeyType(TypeDescriptors.strings())) + .setCoder(KvCoder.of(StringUtf8Coder.of(), fileInfoCoder)) + .apply("Group", GroupByKey.create()) + .apply("Decide", ParDo.of(new DecideFn(policy))) + .setCoder(fileInfoCoder); + } + + private static class DecideFn extends DoFn<KV<String, Iterable<FileInfo>>, FileInfo> { Review Comment: IIUC we're just checking if all files in a group are unreachable and emitting only one if so. I think it may be more fitting to implement a CombineFn and use Combine.perKey. Will let runners pre-combine before the shuffle. Will leave it up to you though, both ways work ########## sdks/java/io/iceberg/src/main/java/org/apache/beam/sdk/io/iceberg/maintenance/SetDifference.java: ########## @@ -0,0 +1,191 @@ +/* + * 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.beam.sdk.io.iceberg.maintenance; + +import static org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.base.Preconditions.checkState; + +import java.io.Serializable; +import java.util.Collections; +import java.util.List; +import org.apache.beam.sdk.coders.Coder; +import org.apache.beam.sdk.coders.KvCoder; +import org.apache.beam.sdk.coders.StringUtf8Coder; +import org.apache.beam.sdk.metrics.Counter; +import org.apache.beam.sdk.metrics.Metrics; +import org.apache.beam.sdk.transforms.DoFn; +import org.apache.beam.sdk.transforms.GroupByKey; +import org.apache.beam.sdk.transforms.PTransform; +import org.apache.beam.sdk.transforms.ParDo; +import org.apache.beam.sdk.transforms.SerializableFunction; +import org.apache.beam.sdk.transforms.WithKeys; +import org.apache.beam.sdk.values.KV; +import org.apache.beam.sdk.values.PCollection; +import org.apache.beam.sdk.values.TypeDescriptors; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * Selects the files that can be deleted: the deletion candidates ({@link FileInfo#getReachable()} + * is false) that no reachable file matches. + * + * <p>The input is every file the operation considered: the files reachable from table state that + * must be preserved, and the deletion candidates. Files are grouped by a key, and each group is + * resolved by a {@link Policy}, which chooses the candidates in the group to delete. + * + * <p>{@link #exactLocations()} matches files the way Iceberg's Spark {@code ExpireSnapshots} action + * does: by exact location, within one table. A candidate is protected only by a reachable file of + * the same table with the same location string. Running several tables through one transform gives + * the same result as running each table alone; files shared between tables are protected by the + * table's {@code gc.enabled} property, not by matching. It emits each location at most once, which + * also removes duplicate candidates such as a Puffin file that holds several deletion vectors. Only + * the location is compared, not the file category, as in Iceberg core's own reachability cleanup; + * this protects at least the files that comparing both would. + * + * <p>Metrics: {@code candidatesEmitted} counts emitted candidates, and {@code candidatesProtected} + * counts keys whose candidates were all protected. Both are attempted values, so retried bundles + * may be counted more than once. + */ +class SetDifference extends PTransform<PCollection<FileInfo>, PCollection<FileInfo>> { + + static final String CANDIDATES_PROTECTED = "candidatesProtected"; + static final String CANDIDATES_EMITTED = "candidatesEmitted"; + + /** Decides how files are matched, and which candidates a group of matching files emits. */ + interface Policy extends Serializable { + + /** Returns the key under which {@code file} is matched against other files. */ + String key(FileInfo file); + + /** + * Returns the candidates to delete from a group of files that share a key, which may be empty. + * Every returned file must be a distinct member of {@code group} that is not reachable. + */ + List<FileInfo> decide(Iterable<FileInfo> group); + } + + private final Policy policy; + + private SetDifference(Policy policy) { + this.policy = policy; + } + + /** Matches files by exact location within one table, like Spark's {@code ExpireSnapshots}. */ + static SetDifference exactLocations() { + return new SetDifference(new ExactLocationPolicy()); + } + + /** Matches files with {@code policy}. */ + static SetDifference withPolicy(Policy policy) { + return new SetDifference(policy); + } + + @Override + public PCollection<FileInfo> expand(PCollection<FileInfo> files) { + Coder<FileInfo> fileInfoCoder = files.getCoder(); + Policy policy = this.policy; + return files + .apply( + "Key", + WithKeys.of((SerializableFunction<FileInfo, String>) policy::key) + .withKeyType(TypeDescriptors.strings())) + .setCoder(KvCoder.of(StringUtf8Coder.of(), fileInfoCoder)) + .apply("Group", GroupByKey.create()) + .apply("Decide", ParDo.of(new DecideFn(policy))) + .setCoder(fileInfoCoder); + } + + private static class DecideFn extends DoFn<KV<String, Iterable<FileInfo>>, FileInfo> { + private final Policy policy; + private final Counter candidatesProtected = + Metrics.counter(SetDifference.class, CANDIDATES_PROTECTED); + private final Counter candidatesEmitted = + Metrics.counter(SetDifference.class, CANDIDATES_EMITTED); + + DecideFn(Policy policy) { + this.policy = policy; + } + + @ProcessElement + public void processElement( + @Element KV<String, Iterable<FileInfo>> group, OutputReceiver<FileInfo> output) { + Iterable<FileInfo> files = group.getValue(); + List<FileInfo> toDelete = policy.decide(files); + for (FileInfo file : toDelete) { + checkState( + !file.getReachable(), + "Policy %s chose reachable file %s for deletion", + policy, + file.getPath()); + output.output(file); + } + candidatesEmitted.inc(toDelete.size()); + // GroupByKey output can be iterated more than once. + if (toDelete.isEmpty() && hasCandidate(files)) { + candidatesProtected.inc(); + } + } + + private static boolean hasCandidate(Iterable<FileInfo> files) { + for (FileInfo file : files) { + if (!file.getReachable()) { + return true; + } + } + return false; + } + } + + /** + * Matches files by table and exact location. A group's candidate is deleted unless the group + * contains a reachable file. + */ + static class ExactLocationPolicy implements Policy { + + /** + * Returns a key combining the table identifier and the location. The identifier is length- + * prefixed, so no pair of different (table, location) values can produce the same key. + */ + @Override + public String key(FileInfo file) { + String table = file.getTableIdentifier(); + return table.length() + ":" + table + file.getPath(); + } Review Comment: why not simplify with a KV<String, String> type? -- 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]
