wzhero1 commented on code in PR #7027: URL: https://github.com/apache/paimon/pull/7027#discussion_r3013717929
########## paimon-flink/paimon-flink-common/src/main/java/org/apache/paimon/flink/expire/ExpireSnapshotsPlanner.java: ########## @@ -0,0 +1,326 @@ +/* + * 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.paimon.flink.expire; + +import org.apache.paimon.Snapshot; +import org.apache.paimon.annotation.VisibleForTesting; +import org.apache.paimon.consumer.ConsumerManager; +import org.apache.paimon.operation.SnapshotDeletion; +import org.apache.paimon.options.ExpireConfig; +import org.apache.paimon.table.FileStoreTable; +import org.apache.paimon.utils.Preconditions; +import org.apache.paimon.utils.SnapshotManager; +import org.apache.paimon.utils.TagManager; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.UncheckedIOException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Map; +import java.util.Set; +import java.util.concurrent.CompletableFuture; +import java.util.concurrent.ConcurrentHashMap; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.Executor; + +import static org.apache.paimon.utils.SnapshotManager.findPreviousOrEqualSnapshot; +import static org.apache.paimon.utils.SnapshotManager.findPreviousSnapshot; + +/** + * Planner for snapshot expiration. This class computes the expiration plan including: + * + * <ul> + * <li>The range of snapshots to expire [beginInclusiveId, endExclusiveId) + * <li>Protection set containing manifests that should not be deleted + * <li>Four groups of tasks organized by deletion phase + * </ul> + * + * <p>Tag data files are loaded on-demand by workers using {@link + * SnapshotDeletion#createDataFileSkipperForTags}, which has internal caching to avoid repeated + * reads. + */ +public class ExpireSnapshotsPlanner { + + private static final Logger LOG = LoggerFactory.getLogger(ExpireSnapshotsPlanner.class); + + private final SnapshotManager snapshotManager; + private final ConsumerManager consumerManager; + private final SnapshotDeletion snapshotDeletion; + private final TagManager tagManager; + + public ExpireSnapshotsPlanner( + SnapshotManager snapshotManager, + ConsumerManager consumerManager, + SnapshotDeletion snapshotDeletion, + TagManager tagManager) { + this.snapshotManager = snapshotManager; + this.consumerManager = consumerManager; + this.snapshotDeletion = snapshotDeletion; + this.tagManager = tagManager; + } + + /** Creates an ExpireSnapshotsPlanner from a FileStoreTable. */ + public static ExpireSnapshotsPlanner create(FileStoreTable table) { + SnapshotManager snapshotManager = table.snapshotManager(); + ConsumerManager consumerManager = + new ConsumerManager(table.fileIO(), table.location(), snapshotManager.branch()); + return new ExpireSnapshotsPlanner( + table.snapshotManager(), + consumerManager, + table.store().newSnapshotDeletion(), + table.tagManager()); + } + + /** + * Plan the snapshot expiration. + * + * @param config expiration configuration + * @return the expiration plan with three groups of tasks, or empty plan if nothing to expire + */ + public ExpireSnapshotsPlan plan(ExpireConfig config) { + snapshotDeletion.setChangelogDecoupled(config.isChangelogDecoupled()); + int retainMax = config.getSnapshotRetainMax(); + int retainMin = config.getSnapshotRetainMin(); + int maxDeletes = config.getSnapshotMaxDeletes(); + long olderThanMills = + System.currentTimeMillis() - config.getSnapshotTimeRetain().toMillis(); + + // 1. Get snapshot range + Long latestSnapshotId = snapshotManager.latestSnapshotId(); + if (latestSnapshotId == null) { + // no snapshot, nothing to expire + return ExpireSnapshotsPlan.empty(); + } + + Long earliestId = snapshotManager.earliestSnapshotId(); + if (earliestId == null) { + return ExpireSnapshotsPlan.empty(); + } + + Preconditions.checkArgument( + retainMax >= retainMin, + String.format( + "retainMax (%s) must not be less than retainMin (%s).", + retainMax, retainMin)); + + // the min snapshot to retain from 'snapshot.num-retained.max' + // (the maximum number of snapshots to retain) + long min = Math.max(latestSnapshotId - retainMax + 1, earliestId); + + // the max exclusive snapshot to expire until + // protected by 'snapshot.num-retained.min' + // (the minimum number of completed snapshots to retain) + long endExclusiveId = latestSnapshotId - retainMin + 1; + + // the snapshot being read by the consumer cannot be deleted + long consumerProtection = consumerManager.minNextSnapshot().orElse(Long.MAX_VALUE); + endExclusiveId = Math.min(endExclusiveId, consumerProtection); Review Comment: Done. Extracted `computeSnapshotExpireRange()` as a static method in `ExpireSnapshotsImpl` with an `ExpireRange` return type. Both `ExpireSnapshotsImpl.expire()` and `ExpireSnapshotsPlanner.plan() `now call this shared method instead of duplicating the range computation logic. Also removed the duplicated `findSkippingTags()` from `ExpireSnapshotsPlanner` — it now uses the one in `ExpireSnapshotsImpl`. -- 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]
