JingsongLi commented on a change in pull request #17:
URL: https://github.com/apache/flink-table-store/pull/17#discussion_r805460363



##########
File path: 
flink-table-store-core/src/main/java/org/apache/flink/table/store/file/operation/FileStoreExpireImpl.java
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.flink.table.store.file.operation;
+
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.table.data.binary.BinaryRowData;
+import org.apache.flink.table.store.file.Snapshot;
+import org.apache.flink.table.store.file.manifest.ManifestEntry;
+import org.apache.flink.table.store.file.manifest.ManifestFileMeta;
+import org.apache.flink.table.store.file.manifest.ManifestList;
+import org.apache.flink.table.store.file.mergetree.sst.SstPathFactory;
+import org.apache.flink.table.store.file.utils.FileStorePathFactory;
+import org.apache.flink.table.store.file.utils.FileUtils;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Default implementation of {@link FileStoreExpire}. It retains a certain 
number of latest
+ * snapshots.
+ */
+public class FileStoreExpireImpl implements FileStoreExpire {
+
+    private final int numRetained;
+    private final FileStorePathFactory pathFactory;
+    private final ManifestList manifestList;
+    private final FileStoreScan scan;
+
+    public FileStoreExpireImpl(

Review comment:
       I am thinking that we can provide three options:
   - snapshot-expire.retention: retention time for snapshot changes
   - snapshot-expire.num-retained
   - snapshot-expire.check-interval

##########
File path: 
flink-table-store-core/src/main/java/org/apache/flink/table/store/file/operation/FileStoreExpireImpl.java
##########
@@ -0,0 +1,127 @@
+/*
+ * 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.flink.table.store.file.operation;
+
+import org.apache.flink.core.fs.Path;
+import org.apache.flink.table.data.binary.BinaryRowData;
+import org.apache.flink.table.store.file.Snapshot;
+import org.apache.flink.table.store.file.manifest.ManifestEntry;
+import org.apache.flink.table.store.file.manifest.ManifestFileMeta;
+import org.apache.flink.table.store.file.manifest.ManifestList;
+import org.apache.flink.table.store.file.mergetree.sst.SstPathFactory;
+import org.apache.flink.table.store.file.utils.FileStorePathFactory;
+import org.apache.flink.table.store.file.utils.FileUtils;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Map;
+import java.util.Set;
+
+/**
+ * Default implementation of {@link FileStoreExpire}. It retains a certain 
number of latest
+ * snapshots.
+ */
+public class FileStoreExpireImpl implements FileStoreExpire {
+
+    private final int numRetained;
+    private final FileStorePathFactory pathFactory;
+    private final ManifestList manifestList;
+    private final FileStoreScan scan;
+
+    public FileStoreExpireImpl(
+            int numRetained,
+            FileStorePathFactory pathFactory,
+            ManifestList.Factory manifestListFactory,
+            FileStoreScan scan) {
+        this.numRetained = numRetained;
+        this.pathFactory = pathFactory;
+        this.manifestList = manifestListFactory.create();
+        this.scan = scan;
+    }
+
+    @Override
+    public void expire() {
+        if (numRetained <= 0) {
+            // never expire
+            return;
+        }
+        Long latestSnapshotId = pathFactory.latestSnapshotId();
+        if (latestSnapshotId == null) {
+            // no snapshot, nothing to expire
+            return;
+        }
+
+        long lastSnapshotIdToExpire = latestSnapshotId - numRetained;
+        long firstSnapshotIdToExpire = Snapshot.FIRST_SNAPSHOT_ID;
+        for (long id = lastSnapshotIdToExpire; id >= 
Snapshot.FIRST_SNAPSHOT_ID; id--) {
+            Path snapshotPath = pathFactory.toSnapshotPath(id);
+            try {
+                if (!snapshotPath.getFileSystem().exists(snapshotPath)) {
+                    // only latest snapshots are retained, as we cannot find 
this snapshot, we can
+                    // assume that all snapshots preceding it have been removed
+                    firstSnapshotIdToExpire = id + 1;
+                    break;
+                }
+            } catch (IOException e) {
+                throw new RuntimeException(
+                        "Failed to determine if snapshot #" + id + " still 
exists", e);
+            }
+        }
+
+        for (long id = firstSnapshotIdToExpire; id <= lastSnapshotIdToExpire; 
id++) {
+            expire(id);
+        }
+    }
+
+    private void expire(long snapshotId) {
+        // delete all sst files removed in the snapshot to expire
+        Map<BinaryRowData, Map<Integer, SstPathFactory>> sstPathFactories = 
new HashMap<>();
+        for (ManifestEntry entry : 
scan.withSnapshot(snapshotId).plan().deletedFiles()) {

Review comment:
       Consider upgrade files?

##########
File path: 
flink-table-store-core/src/main/java/org/apache/flink/table/store/file/operation/FileStoreScan.java
##########
@@ -56,5 +56,11 @@
 
         /** Result {@link ManifestEntry} files. */
         List<ManifestEntry> files();
+
+        /**
+         * Deleted sst files in current snapshot. They can be safely deleted 
from file system if
+         * this snapshot expires.
+         */
+        List<ManifestEntry> deletedFiles();

Review comment:
       This `deletedFiles` is misunderstanding.
   I prefer to add a new method `planChangeFiles()`, it returns all files. The 
caller can compute physical deleted files.




-- 
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: issues-unsubscr...@flink.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to