You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@paimon.apache.org by yu...@apache.org on 2023/06/20 01:57:14 UTC
[incubator-paimon] branch master updated: [core] Support deleting tag (#1363)
This is an automated email from the ASF dual-hosted git repository.
yuzelin pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-paimon.git
The following commit(s) were added to refs/heads/master by this push:
new 98f381e4c [core] Support deleting tag (#1363)
98f381e4c is described below
commit 98f381e4cfa1cebf1fd7f1dc86d3e7c4a2135c44
Author: yuzelin <33...@users.noreply.github.com>
AuthorDate: Tue Jun 20 09:57:10 2023 +0800
[core] Support deleting tag (#1363)
---
.../java/org/apache/paimon/AbstractFileStore.java | 17 +-
.../src/main/java/org/apache/paimon/FileStore.java | 3 +
.../org/apache/paimon/operation/DeletionUtils.java | 157 +++++++++++++++++
.../paimon/operation/FileStoreExpireImpl.java | 37 ----
.../apache/paimon/operation/SnapshotDeletion.java | 60 +------
.../org/apache/paimon/operation/TagDeletion.java | 187 +++++++++++++++++++++
.../org/apache/paimon/operation/TagFileKeeper.java | 51 +++---
.../paimon/table/AbstractFileStoreTable.java | 14 ++
.../org/apache/paimon/table/ReadonlyTable.java | 8 +
.../main/java/org/apache/paimon/table/Table.java | 4 +
.../paimon/utils/ParallellyExecuteUtils.java | 4 +-
.../test/java/org/apache/paimon/TestFileStore.java | 13 +-
.../apache/paimon/operation/FileDeletionTest.java | 153 +++++++++++++++++
.../paimon/table/FileStoreTableTestBase.java | 19 +++
.../paimon/table/IndexFileExpireTableTest.java | 29 ++++
15 files changed, 622 insertions(+), 134 deletions(-)
diff --git a/paimon-core/src/main/java/org/apache/paimon/AbstractFileStore.java b/paimon-core/src/main/java/org/apache/paimon/AbstractFileStore.java
index 97d23fee6..558daa899 100644
--- a/paimon-core/src/main/java/org/apache/paimon/AbstractFileStore.java
+++ b/paimon-core/src/main/java/org/apache/paimon/AbstractFileStore.java
@@ -30,6 +30,7 @@ import org.apache.paimon.operation.FileStoreCommitImpl;
import org.apache.paimon.operation.FileStoreExpireImpl;
import org.apache.paimon.operation.PartitionExpire;
import org.apache.paimon.operation.SnapshotDeletion;
+import org.apache.paimon.operation.TagDeletion;
import org.apache.paimon.operation.TagFileKeeper;
import org.apache.paimon.options.MemorySize;
import org.apache.paimon.schema.SchemaManager;
@@ -169,12 +170,12 @@ public abstract class AbstractFileStore<T> implements FileStore<T> {
options.snapshotNumRetainMax(),
options.snapshotTimeRetain().toMillis(),
snapshotManager(),
- newIndexFileHandler(),
newSnapshotDeletion(),
new TagFileKeeper(
manifestListFactory().create(),
manifestFileFactory().create(),
- new TagManager(fileIO, options.path())));
+ new TagManager(fileIO, options.path()),
+ options.scanManifestParallelism()));
}
@Override
@@ -187,6 +188,18 @@ public abstract class AbstractFileStore<T> implements FileStore<T> {
newIndexFileHandler());
}
+ @Override
+ public TagDeletion newTagDeletion() {
+ return new TagDeletion(
+ fileIO,
+ options.path(),
+ pathFactory(),
+ manifestListFactory().create(),
+ manifestFileFactory().create(),
+ newIndexFileHandler(),
+ options.scanManifestParallelism());
+ }
+
public abstract Comparator<InternalRow> newKeyComparator();
@Override
diff --git a/paimon-core/src/main/java/org/apache/paimon/FileStore.java b/paimon-core/src/main/java/org/apache/paimon/FileStore.java
index d0d1b94c9..6d91d8485 100644
--- a/paimon-core/src/main/java/org/apache/paimon/FileStore.java
+++ b/paimon-core/src/main/java/org/apache/paimon/FileStore.java
@@ -27,6 +27,7 @@ import org.apache.paimon.operation.FileStoreScan;
import org.apache.paimon.operation.FileStoreWrite;
import org.apache.paimon.operation.PartitionExpire;
import org.apache.paimon.operation.SnapshotDeletion;
+import org.apache.paimon.operation.TagDeletion;
import org.apache.paimon.table.BucketMode;
import org.apache.paimon.types.RowType;
import org.apache.paimon.utils.SnapshotManager;
@@ -66,6 +67,8 @@ public interface FileStore<T> extends Serializable {
SnapshotDeletion newSnapshotDeletion();
+ TagDeletion newTagDeletion();
+
@Nullable
PartitionExpire newPartitionExpire(String commitUser);
}
diff --git a/paimon-core/src/main/java/org/apache/paimon/operation/DeletionUtils.java b/paimon-core/src/main/java/org/apache/paimon/operation/DeletionUtils.java
new file mode 100644
index 000000000..9a9b28465
--- /dev/null
+++ b/paimon-core/src/main/java/org/apache/paimon/operation/DeletionUtils.java
@@ -0,0 +1,157 @@
+/*
+ * 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.operation;
+
+import org.apache.paimon.Snapshot;
+import org.apache.paimon.data.BinaryRow;
+import org.apache.paimon.fs.FileIO;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.index.IndexFileHandler;
+import org.apache.paimon.index.IndexFileMeta;
+import org.apache.paimon.manifest.IndexManifestEntry;
+import org.apache.paimon.manifest.ManifestEntry;
+import org.apache.paimon.manifest.ManifestFile;
+import org.apache.paimon.manifest.ManifestFileMeta;
+import org.apache.paimon.manifest.ManifestList;
+import org.apache.paimon.utils.FileStorePathFactory;
+import org.apache.paimon.utils.ParallellyExecuteUtils;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.annotation.Nullable;
+
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.stream.Collectors;
+
+/** Util methods for snapshot and tag deletion. */
+public class DeletionUtils {
+
+ private static final Logger LOG = LoggerFactory.getLogger(DeletionUtils.class);
+
+ public static Iterable<ManifestEntry> readEntries(
+ List<ManifestFileMeta> manifests,
+ ManifestFile manifestFile,
+ @Nullable Integer scanManifestParallelism) {
+ return ParallellyExecuteUtils.parallelismBatchIterable(
+ files ->
+ files.parallelStream()
+ .flatMap(m -> manifestFile.read(m.fileName()).stream())
+ .collect(Collectors.toList()),
+ manifests,
+ scanManifestParallelism);
+ }
+
+ public static void addMergedDataFiles(
+ Map<BinaryRow, Map<Integer, Set<String>>> dataFiles,
+ Snapshot snapshot,
+ ManifestList manifestList,
+ ManifestFile manifestFile,
+ @Nullable Integer scanManifestParallelism) {
+ Iterable<ManifestEntry> entries =
+ readEntries(
+ snapshot.dataManifests(manifestList),
+ manifestFile,
+ scanManifestParallelism);
+ for (ManifestEntry entry : ManifestEntry.mergeEntries(entries)) {
+ dataFiles
+ .computeIfAbsent(entry.partition(), p -> new HashMap<>())
+ .computeIfAbsent(entry.bucket(), b -> new HashSet<>())
+ .add(entry.file().fileName());
+ }
+ }
+
+ public static boolean containsDataFile(
+ Map<BinaryRow, Map<Integer, Set<String>>> dataFiles, ManifestEntry testee) {
+ Map<Integer, Set<String>> buckets = dataFiles.get(testee.partition());
+ if (buckets != null) {
+ Set<String> fileNames = buckets.get(testee.bucket());
+ if (fileNames != null) {
+ return fileNames.contains(testee.file().fileName());
+ }
+ }
+ return false;
+ }
+
+ /** Try to delete directories that may be empty. */
+ public static void tryDeleteDirectories(
+ Map<BinaryRow, Set<Integer>> deletionBuckets,
+ FileStorePathFactory pathFactory,
+ FileIO fileIO) {
+ // All directory paths are deduplicated and sorted by hierarchy level
+ Map<Integer, Set<Path>> deduplicate = new HashMap<>();
+ for (Map.Entry<BinaryRow, Set<Integer>> entry : deletionBuckets.entrySet()) {
+ // try to delete bucket directories
+ for (Integer bucket : entry.getValue()) {
+ tryDeleteEmptyDirectory(pathFactory.bucketPath(entry.getKey(), bucket), fileIO);
+ }
+
+ List<Path> hierarchicalPaths = pathFactory.getHierarchicalPartitionPath(entry.getKey());
+ int hierarchies = hierarchicalPaths.size();
+ if (hierarchies == 0) {
+ continue;
+ }
+
+ if (tryDeleteEmptyDirectory(hierarchicalPaths.get(hierarchies - 1), fileIO)) {
+ // deduplicate high level partition directories
+ for (int hierarchy = 0; hierarchy < hierarchies - 1; hierarchy++) {
+ Path path = hierarchicalPaths.get(hierarchy);
+ deduplicate.computeIfAbsent(hierarchy, i -> new HashSet<>()).add(path);
+ }
+ }
+ }
+
+ // from deepest to shallowest
+ for (int hierarchy = deduplicate.size() - 1; hierarchy >= 0; hierarchy--) {
+ deduplicate.get(hierarchy).forEach(path -> tryDeleteEmptyDirectory(path, fileIO));
+ }
+ }
+
+ private static boolean tryDeleteEmptyDirectory(Path path, FileIO fileIO) {
+ try {
+ fileIO.delete(path, false);
+ return true;
+ } catch (IOException e) {
+ LOG.debug("Failed to delete directory '{}'. Check whether it is empty.", path);
+ return false;
+ }
+ }
+
+ public static Set<String> collectManifestSkippingSet(
+ Snapshot snapshot, ManifestList manifestList, IndexFileHandler indexFileHandler) {
+ Set<String> skip =
+ snapshot.dataManifests(manifestList).stream()
+ .map(ManifestFileMeta::fileName)
+ .collect(Collectors.toCollection(HashSet::new));
+ String indexManifest = snapshot.indexManifest();
+ if (indexManifest != null) {
+ skip.add(indexManifest);
+ indexFileHandler.readManifest(indexManifest).stream()
+ .map(IndexManifestEntry::indexFile)
+ .map(IndexFileMeta::fileName)
+ .forEach(skip::add);
+ }
+ return skip;
+ }
+}
diff --git a/paimon-core/src/main/java/org/apache/paimon/operation/FileStoreExpireImpl.java b/paimon-core/src/main/java/org/apache/paimon/operation/FileStoreExpireImpl.java
index 6b1dde3b8..46ef06fa5 100644
--- a/paimon-core/src/main/java/org/apache/paimon/operation/FileStoreExpireImpl.java
+++ b/paimon-core/src/main/java/org/apache/paimon/operation/FileStoreExpireImpl.java
@@ -22,13 +22,7 @@ import org.apache.paimon.Snapshot;
import org.apache.paimon.annotation.VisibleForTesting;
import org.apache.paimon.consumer.ConsumerManager;
import org.apache.paimon.data.BinaryRow;
-import org.apache.paimon.fs.FileIO;
-import org.apache.paimon.index.IndexFileHandler;
-import org.apache.paimon.manifest.ManifestFile;
-import org.apache.paimon.manifest.ManifestList;
-import org.apache.paimon.utils.FileStorePathFactory;
import org.apache.paimon.utils.SnapshotManager;
-import org.apache.paimon.utils.TagManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -60,46 +54,16 @@ public class FileStoreExpireImpl implements FileStoreExpire {
private final SnapshotManager snapshotManager;
private final ConsumerManager consumerManager;
private final SnapshotDeletion snapshotDeletion;
- private final IndexFileHandler indexFileHandler;
private final TagFileKeeper tagFileKeeper;
private Lock lock;
public FileStoreExpireImpl(
- FileIO fileIO,
int numRetainedMin,
int numRetainedMax,
long millisRetained,
- FileStorePathFactory pathFactory,
SnapshotManager snapshotManager,
- IndexFileHandler indexFileHandler,
- ManifestFile.Factory manifestFileFactory,
- ManifestList.Factory manifestListFactory) {
- this(
- numRetainedMin,
- numRetainedMax,
- millisRetained,
- snapshotManager,
- indexFileHandler,
- new SnapshotDeletion(
- fileIO,
- pathFactory,
- manifestFileFactory.create(),
- manifestListFactory.create(),
- indexFileHandler),
- new TagFileKeeper(
- manifestListFactory.create(),
- manifestFileFactory.create(),
- new TagManager(fileIO, snapshotManager.tablePath())));
- }
-
- public FileStoreExpireImpl(
- int numRetainedMin,
- int numRetainedMax,
- long millisRetained,
- SnapshotManager snapshotManager,
- IndexFileHandler indexFileHandler,
SnapshotDeletion snapshotDeletion,
TagFileKeeper tagFileKeeper) {
this.numRetainedMin = numRetainedMin;
@@ -108,7 +72,6 @@ public class FileStoreExpireImpl implements FileStoreExpire {
this.snapshotManager = snapshotManager;
this.consumerManager =
new ConsumerManager(snapshotManager.fileIO(), snapshotManager.tablePath());
- this.indexFileHandler = indexFileHandler;
this.snapshotDeletion = snapshotDeletion;
this.tagFileKeeper = tagFileKeeper;
}
diff --git a/paimon-core/src/main/java/org/apache/paimon/operation/SnapshotDeletion.java b/paimon-core/src/main/java/org/apache/paimon/operation/SnapshotDeletion.java
index 281cbd456..ffc975977 100644
--- a/paimon-core/src/main/java/org/apache/paimon/operation/SnapshotDeletion.java
+++ b/paimon-core/src/main/java/org/apache/paimon/operation/SnapshotDeletion.java
@@ -24,7 +24,6 @@ import org.apache.paimon.data.BinaryRow;
import org.apache.paimon.fs.FileIO;
import org.apache.paimon.fs.Path;
import org.apache.paimon.index.IndexFileHandler;
-import org.apache.paimon.index.IndexFileMeta;
import org.apache.paimon.manifest.FileKind;
import org.apache.paimon.manifest.IndexManifestEntry;
import org.apache.paimon.manifest.ManifestEntry;
@@ -39,7 +38,6 @@ import org.apache.paimon.shade.guava30.com.google.common.collect.Iterables;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
@@ -123,37 +121,7 @@ public class SnapshotDeletion {
* #deleteAddedDataFiles}.
*/
public void tryDeleteDirectories(Map<BinaryRow, Set<Integer>> deletionBuckets) {
- // All directory paths are deduplicated and sorted by hierarchy level
- Map<Integer, Set<Path>> deduplicate = new HashMap<>();
- for (Map.Entry<BinaryRow, Set<Integer>> entry : deletionBuckets.entrySet()) {
- // try to delete bucket directories
- for (Integer bucket : entry.getValue()) {
- tryDeleteEmptyDirectory(pathFactory.bucketPath(entry.getKey(), bucket));
- }
-
- List<Path> hierarchicalPaths = pathFactory.getHierarchicalPartitionPath(entry.getKey());
- int hierarchies = hierarchicalPaths.size();
- if (hierarchies == 0) {
- continue;
- }
-
- if (tryDeleteEmptyDirectory(hierarchicalPaths.get(hierarchies - 1))) {
- // deduplicate high level partition directories
- for (int hierarchy = 0; hierarchy < hierarchies - 1; hierarchy++) {
- Path path = hierarchicalPaths.get(hierarchy);
- deduplicate.computeIfAbsent(hierarchy, i -> new HashSet<>()).add(path);
- }
- }
- }
-
- // from deepest to shallowest
- for (int hierarchy = deduplicate.size() - 1; hierarchy >= 0; hierarchy--) {
- deduplicate.get(hierarchy).forEach(this::tryDeleteEmptyDirectory);
- }
- }
-
- public ManifestList manifestList() {
- return manifestList;
+ DeletionUtils.tryDeleteDirectories(deletionBuckets, pathFactory, fileIO);
}
/**
@@ -164,7 +132,7 @@ public class SnapshotDeletion {
* this set too. NOTE: changelog manifests won't be checked.
*/
public void deleteManifestFiles(Set<String> skipped, Snapshot snapshot) {
- // cannot call `toExpire.dataManifests` directly, it is possible that a job is
+ // cannot call `snapshot.dataManifests` directly, it is possible that a job is
// killed during expiration, so some manifest files may have been deleted
List<ManifestFileMeta> toExpireManifests = new ArrayList<>();
toExpireManifests.addAll(tryReadManifestList(snapshot.baseManifestList()));
@@ -295,29 +263,7 @@ public class SnapshotDeletion {
});
}
- private boolean tryDeleteEmptyDirectory(Path path) {
- try {
- fileIO.delete(path, false);
- return true;
- } catch (IOException e) {
- LOG.debug("Failed to delete directory '{}'. Check whether it is empty.", path);
- return false;
- }
- }
-
public Set<String> collectManifestSkippingSet(Snapshot snapshot) {
- Set<String> skip =
- snapshot.dataManifests(manifestList).stream()
- .map(ManifestFileMeta::fileName)
- .collect(Collectors.toCollection(HashSet::new));
- String indexManifest = snapshot.indexManifest();
- if (indexManifest != null) {
- skip.add(indexManifest);
- indexFileHandler.readManifest(indexManifest).stream()
- .map(IndexManifestEntry::indexFile)
- .map(IndexFileMeta::fileName)
- .forEach(skip::add);
- }
- return skip;
+ return DeletionUtils.collectManifestSkippingSet(snapshot, manifestList, indexFileHandler);
}
}
diff --git a/paimon-core/src/main/java/org/apache/paimon/operation/TagDeletion.java b/paimon-core/src/main/java/org/apache/paimon/operation/TagDeletion.java
new file mode 100644
index 000000000..68b467480
--- /dev/null
+++ b/paimon-core/src/main/java/org/apache/paimon/operation/TagDeletion.java
@@ -0,0 +1,187 @@
+/*
+ * 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.operation;
+
+import org.apache.paimon.Snapshot;
+import org.apache.paimon.data.BinaryRow;
+import org.apache.paimon.fs.FileIO;
+import org.apache.paimon.fs.Path;
+import org.apache.paimon.index.IndexFileHandler;
+import org.apache.paimon.manifest.IndexManifestEntry;
+import org.apache.paimon.manifest.ManifestEntry;
+import org.apache.paimon.manifest.ManifestFile;
+import org.apache.paimon.manifest.ManifestFileMeta;
+import org.apache.paimon.manifest.ManifestList;
+import org.apache.paimon.utils.FileStorePathFactory;
+import org.apache.paimon.utils.SnapshotManager;
+import org.apache.paimon.utils.TagManager;
+
+import javax.annotation.Nullable;
+
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import static org.apache.paimon.operation.DeletionUtils.addMergedDataFiles;
+import static org.apache.paimon.operation.DeletionUtils.collectManifestSkippingSet;
+import static org.apache.paimon.operation.DeletionUtils.containsDataFile;
+import static org.apache.paimon.operation.DeletionUtils.readEntries;
+import static org.apache.paimon.operation.DeletionUtils.tryDeleteDirectories;
+
+/**
+ * Delete tag files. This class doesn't check changelog files because they are handled by {@link
+ * SnapshotDeletion}.
+ */
+public class TagDeletion {
+
+ private final FileIO fileIO;
+ private final FileStorePathFactory pathFactory;
+ private final ManifestList manifestList;
+ private final ManifestFile manifestFile;
+ private final IndexFileHandler indexFileHandler;
+ @Nullable private final Integer scanManifestParallelism;
+
+ private final SnapshotManager snapshotManager;
+ private final List<Snapshot> taggedSnapshots;
+
+ private final Map<BinaryRow, Map<Integer, Set<String>>> skippedDataFiles;
+ private final Set<String> skippedManifests;
+
+ public TagDeletion(
+ FileIO fileIO,
+ Path tablePath,
+ FileStorePathFactory pathFactory,
+ ManifestList manifestList,
+ ManifestFile manifestFile,
+ IndexFileHandler indexFileHandler,
+ @Nullable Integer scanManifestParallelism) {
+ this.fileIO = fileIO;
+ this.pathFactory = pathFactory;
+ this.manifestList = manifestList;
+ this.manifestFile = manifestFile;
+ this.indexFileHandler = indexFileHandler;
+ this.scanManifestParallelism = scanManifestParallelism;
+
+ this.snapshotManager = new SnapshotManager(fileIO, tablePath);
+ this.taggedSnapshots = new TagManager(fileIO, tablePath).taggedSnapshots();
+
+ this.skippedDataFiles = new HashMap<>();
+ this.skippedManifests = new HashSet<>();
+ }
+
+ /** Delete unused data files, manifest files and empty data file directories of tag. */
+ public void delete(Snapshot taggedSnapshot) {
+ if (snapshotManager.snapshotExists(taggedSnapshot.id())) {
+ return;
+ }
+
+ // collect from the earliest snapshot
+ Snapshot earliest = snapshotManager.snapshot(snapshotManager.earliestSnapshotId());
+ collectSkip(earliest);
+
+ // collect from the neighbor tags
+ int index = findIndex(taggedSnapshot);
+ if (index - 1 >= 0) {
+ collectSkip(taggedSnapshots.get(index - 1));
+ }
+ if (index + 1 < taggedSnapshots.size()) {
+ collectSkip(taggedSnapshots.get(index + 1));
+ }
+
+ // delete data files and empty directories
+ deleteDataFiles(taggedSnapshot);
+ // delete manifests
+ deleteManifestFiles(taggedSnapshot);
+ }
+
+ private void collectSkip(Snapshot snapshot) {
+ addMergedDataFiles(
+ skippedDataFiles, snapshot, manifestList, manifestFile, scanManifestParallelism);
+ skippedManifests.addAll(
+ collectManifestSkippingSet(snapshot, manifestList, indexFileHandler));
+ }
+
+ private void deleteDataFiles(Snapshot taggedSnapshot) {
+ // delete data files
+ Map<BinaryRow, Set<Integer>> deletionBuckets = new HashMap<>();
+ Iterable<ManifestEntry> entries =
+ readEntries(
+ taggedSnapshot.dataManifests(manifestList),
+ manifestFile,
+ scanManifestParallelism);
+ for (ManifestEntry entry : ManifestEntry.mergeEntries(entries)) {
+ if (!containsDataFile(skippedDataFiles, entry)) {
+ Path bucketPath = pathFactory.bucketPath(entry.partition(), entry.bucket());
+ fileIO.deleteQuietly(new Path(bucketPath, entry.file().fileName()));
+ for (String file : entry.file().extraFiles()) {
+ fileIO.deleteQuietly(new Path(bucketPath, file));
+ }
+
+ deletionBuckets
+ .computeIfAbsent(entry.partition(), p -> new HashSet<>())
+ .add(entry.bucket());
+ }
+ }
+
+ // delete empty data file directories
+ tryDeleteDirectories(deletionBuckets, pathFactory, fileIO);
+ }
+
+ private void deleteManifestFiles(Snapshot taggedSnapshot) {
+ for (ManifestFileMeta manifest : taggedSnapshot.dataManifests(manifestList)) {
+ String fileName = manifest.fileName();
+ if (!skippedManifests.contains(fileName)) {
+ manifestFile.delete(fileName);
+ }
+ }
+
+ // delete manifest lists
+ manifestList.delete(taggedSnapshot.baseManifestList());
+ manifestList.delete(taggedSnapshot.deltaManifestList());
+
+ // delete index files
+ String indexManifest = taggedSnapshot.indexManifest();
+ // check exists, it may have been deleted by other snapshots
+ if (indexManifest != null && indexFileHandler.existsManifest(indexManifest)) {
+ for (IndexManifestEntry entry : indexFileHandler.readManifest(indexManifest)) {
+ if (!skippedManifests.contains(entry.indexFile().fileName())) {
+ indexFileHandler.deleteIndexFile(entry);
+ }
+ }
+
+ if (!skippedManifests.contains(indexManifest)) {
+ indexFileHandler.deleteManifest(indexManifest);
+ }
+ }
+ }
+
+ private int findIndex(Snapshot taggedSnapshot) {
+ for (int i = 0; i < taggedSnapshots.size(); i++) {
+ if (taggedSnapshot.id() == taggedSnapshots.get(i).id()) {
+ return i;
+ }
+ }
+ throw new RuntimeException(
+ String.format(
+ "Didn't find tag with snapshot id '%s'.This is unexpected.",
+ taggedSnapshot.id()));
+ }
+}
diff --git a/paimon-core/src/main/java/org/apache/paimon/operation/TagFileKeeper.java b/paimon-core/src/main/java/org/apache/paimon/operation/TagFileKeeper.java
index 263add359..3f1cc0bef 100644
--- a/paimon-core/src/main/java/org/apache/paimon/operation/TagFileKeeper.java
+++ b/paimon-core/src/main/java/org/apache/paimon/operation/TagFileKeeper.java
@@ -23,17 +23,19 @@ import org.apache.paimon.data.BinaryRow;
import org.apache.paimon.manifest.ManifestEntry;
import org.apache.paimon.manifest.ManifestFile;
import org.apache.paimon.manifest.ManifestList;
-import org.apache.paimon.utils.ParallellyExecuteUtils;
import org.apache.paimon.utils.TagManager;
+import javax.annotation.Nullable;
+
import java.util.ArrayList;
import java.util.HashMap;
-import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Predicate;
-import java.util.stream.Collectors;
+
+import static org.apache.paimon.operation.DeletionUtils.addMergedDataFiles;
+import static org.apache.paimon.operation.DeletionUtils.containsDataFile;
/** Util class to provide methods to prevent tag files to be deleted when expiring snapshots. */
public class TagFileKeeper {
@@ -41,6 +43,7 @@ public class TagFileKeeper {
private final ManifestList manifestList;
private final ManifestFile manifestFile;
private final TagManager tagManager;
+ @Nullable private final Integer scanManifestParallelism;
private long cachedTag = -1;
private final Map<BinaryRow, Map<Integer, Set<String>>> cachedTagDataFiles;
@@ -48,10 +51,15 @@ public class TagFileKeeper {
private List<Snapshot> taggedSnapshots;
public TagFileKeeper(
- ManifestList manifestList, ManifestFile manifestFile, TagManager tagManager) {
+ ManifestList manifestList,
+ ManifestFile manifestFile,
+ TagManager tagManager,
+ @Nullable Integer scanManifestParallelism) {
this.manifestList = manifestList;
this.manifestFile = manifestFile;
this.tagManager = tagManager;
+ this.scanManifestParallelism = scanManifestParallelism;
+
this.cachedTagDataFiles = new HashMap<>();
}
@@ -65,7 +73,7 @@ public class TagFileKeeper {
if (index >= 0) {
tryRefresh(taggedSnapshots.get(index));
}
- return entry -> index >= 0 && contains(entry);
+ return entry -> index >= 0 && containsDataFile(cachedTagDataFiles, entry);
}
public List<Snapshot> findOverlappedSnapshots(long beginInclusive, long endExclusive) {
@@ -89,33 +97,12 @@ public class TagFileKeeper {
private void refresh(Snapshot taggedSnapshot) {
cachedTagDataFiles.clear();
-
- Iterable<ManifestEntry> entries =
- ParallellyExecuteUtils.parallelismBatchIterable(
- files ->
- files.parallelStream()
- .flatMap(m -> manifestFile.read(m.fileName()).stream())
- .collect(Collectors.toList()),
- taggedSnapshot.dataManifests(manifestList),
- null);
-
- for (ManifestEntry entry : ManifestEntry.mergeEntries(entries)) {
- cachedTagDataFiles
- .computeIfAbsent(entry.partition(), p -> new HashMap<>())
- .computeIfAbsent(entry.bucket(), b -> new HashSet<>())
- .add(entry.file().fileName());
- }
- }
-
- private boolean contains(ManifestEntry entry) {
- Map<Integer, Set<String>> buckets = cachedTagDataFiles.get(entry.partition());
- if (buckets != null) {
- Set<String> fileNames = buckets.get(entry.bucket());
- if (fileNames != null) {
- return fileNames.contains(entry.file().fileName());
- }
- }
- return false;
+ addMergedDataFiles(
+ cachedTagDataFiles,
+ taggedSnapshot,
+ manifestList,
+ manifestFile,
+ scanManifestParallelism);
}
private int findPreviousTag(long targetSnapshotId, List<Snapshot> taggedSnapshots) {
diff --git a/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java b/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java
index 44151a0e5..1a982c13d 100644
--- a/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java
+++ b/paimon-core/src/main/java/org/apache/paimon/table/AbstractFileStoreTable.java
@@ -24,6 +24,7 @@ import org.apache.paimon.consumer.ConsumerManager;
import org.apache.paimon.fs.FileIO;
import org.apache.paimon.fs.Path;
import org.apache.paimon.operation.FileStoreScan;
+import org.apache.paimon.operation.TagDeletion;
import org.apache.paimon.options.Options;
import org.apache.paimon.predicate.Predicate;
import org.apache.paimon.schema.SchemaManager;
@@ -43,6 +44,7 @@ import org.apache.paimon.table.source.snapshot.SnapshotReader;
import org.apache.paimon.table.source.snapshot.SnapshotReaderImpl;
import org.apache.paimon.table.source.snapshot.StaticFromTimestampStartingScanner;
import org.apache.paimon.utils.SnapshotManager;
+import org.apache.paimon.utils.StringUtils;
import org.apache.paimon.utils.TagManager;
import java.io.IOException;
@@ -278,4 +280,16 @@ public abstract class AbstractFileStoreTable implements FileStoreTable {
TagManager tagManager = new TagManager(fileIO, path);
tagManager.createTag(snapshot, tagName);
}
+
+ @Override
+ public void deleteTag(String tagName) {
+ checkArgument(!StringUtils.isBlank(tagName), "Tag name '%s' is blank.", tagName);
+
+ TagManager tagManager = new TagManager(fileIO, path);
+ Snapshot taggedSnapshot = tagManager.taggedSnapshot(tagName);
+
+ TagDeletion tagDeletion = store().newTagDeletion();
+ tagDeletion.delete(taggedSnapshot);
+ fileIO.deleteQuietly(tagManager.tagPath(tagName));
+ }
}
diff --git a/paimon-core/src/main/java/org/apache/paimon/table/ReadonlyTable.java b/paimon-core/src/main/java/org/apache/paimon/table/ReadonlyTable.java
index c5458dfc5..1cf25af75 100644
--- a/paimon-core/src/main/java/org/apache/paimon/table/ReadonlyTable.java
+++ b/paimon-core/src/main/java/org/apache/paimon/table/ReadonlyTable.java
@@ -102,4 +102,12 @@ public interface ReadonlyTable extends InnerTable {
"Readonly Table %s does not support createTag.",
this.getClass().getSimpleName()));
}
+
+ @Override
+ default void deleteTag(String tagName) {
+ throw new UnsupportedOperationException(
+ String.format(
+ "Readonly Table %s does not support deleteTag.",
+ this.getClass().getSimpleName()));
+ }
}
diff --git a/paimon-core/src/main/java/org/apache/paimon/table/Table.java b/paimon-core/src/main/java/org/apache/paimon/table/Table.java
index 38b129608..e5b5c26b2 100644
--- a/paimon-core/src/main/java/org/apache/paimon/table/Table.java
+++ b/paimon-core/src/main/java/org/apache/paimon/table/Table.java
@@ -71,6 +71,10 @@ public interface Table extends Serializable {
@Experimental
void createTag(String tagName, long fromSnapshotId);
+ /** Delete a tag by name. */
+ @Experimental
+ void deleteTag(String tagName);
+
// =============== Read & Write Operations ==================
/** Returns a new read builder. */
diff --git a/paimon-core/src/main/java/org/apache/paimon/utils/ParallellyExecuteUtils.java b/paimon-core/src/main/java/org/apache/paimon/utils/ParallellyExecuteUtils.java
index 93f52ad14..3d82f9923 100644
--- a/paimon-core/src/main/java/org/apache/paimon/utils/ParallellyExecuteUtils.java
+++ b/paimon-core/src/main/java/org/apache/paimon/utils/ParallellyExecuteUtils.java
@@ -20,6 +20,8 @@ package org.apache.paimon.utils;
import org.apache.paimon.shade.guava30.com.google.common.collect.Lists;
+import javax.annotation.Nullable;
+
import java.util.ArrayDeque;
import java.util.Iterator;
import java.util.List;
@@ -36,7 +38,7 @@ public class ParallellyExecuteUtils {
// reduce memory usage by batch iterable process, the cached result in memory will be queueSize
public static <T, U> Iterable<T> parallelismBatchIterable(
- Function<List<U>, List<T>> processor, List<U> input, Integer queueSize) {
+ Function<List<U>, List<T>> processor, List<U> input, @Nullable Integer queueSize) {
if (queueSize == null) {
queueSize = FileUtils.COMMON_IO_FORK_JOIN_POOL.getParallelism();
} else if (queueSize <= 0) {
diff --git a/paimon-core/src/test/java/org/apache/paimon/TestFileStore.java b/paimon-core/src/test/java/org/apache/paimon/TestFileStore.java
index 64449e72c..b548827a4 100644
--- a/paimon-core/src/test/java/org/apache/paimon/TestFileStore.java
+++ b/paimon-core/src/test/java/org/apache/paimon/TestFileStore.java
@@ -40,6 +40,7 @@ import org.apache.paimon.operation.FileStoreExpireImpl;
import org.apache.paimon.operation.FileStoreRead;
import org.apache.paimon.operation.FileStoreScan;
import org.apache.paimon.operation.ScanKind;
+import org.apache.paimon.operation.TagFileKeeper;
import org.apache.paimon.options.MemorySize;
import org.apache.paimon.options.Options;
import org.apache.paimon.reader.RecordReaderIterator;
@@ -52,6 +53,7 @@ import org.apache.paimon.utils.CommitIncrement;
import org.apache.paimon.utils.FileStorePathFactory;
import org.apache.paimon.utils.RecordWriter;
import org.apache.paimon.utils.SnapshotManager;
+import org.apache.paimon.utils.TagManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -131,15 +133,16 @@ public class TestFileStore extends KeyValueFileStore {
public FileStoreExpireImpl newExpire(
int numRetainedMin, int numRetainedMax, long millisRetained) {
return new FileStoreExpireImpl(
- fileIO,
numRetainedMin,
numRetainedMax,
millisRetained,
- pathFactory(),
snapshotManager(),
- newIndexFileHandler(),
- manifestFileFactory(),
- manifestListFactory());
+ newSnapshotDeletion(),
+ new TagFileKeeper(
+ manifestListFactory().create(),
+ manifestFileFactory().create(),
+ new TagManager(fileIO, options.path()),
+ options.scanManifestParallelism()));
}
public List<Snapshot> commitData(
diff --git a/paimon-core/src/test/java/org/apache/paimon/operation/FileDeletionTest.java b/paimon-core/src/test/java/org/apache/paimon/operation/FileDeletionTest.java
index d31332be6..be602318f 100644
--- a/paimon-core/src/test/java/org/apache/paimon/operation/FileDeletionTest.java
+++ b/paimon-core/src/test/java/org/apache/paimon/operation/FileDeletionTest.java
@@ -347,6 +347,159 @@ public class FileDeletionTest {
assertPathExists(fileIO, pathFactory.toManifestListPath(tag1.deltaManifestList()));
}
+ @Test
+ public void testDeleteTagWithSnapshot() throws Exception {
+ TestFileStore store = createStore(TestKeyValueGenerator.GeneratorMode.NON_PARTITIONED, 3);
+ TagManager tagManager = new TagManager(fileIO, store.options().path());
+ SnapshotManager snapshotManager = store.snapshotManager();
+ TestKeyValueGenerator gen =
+ new TestKeyValueGenerator(TestKeyValueGenerator.GeneratorMode.NON_PARTITIONED);
+ BinaryRow partition = gen.getPartition(gen.next());
+
+ // snapshot 1: commit A to bucket 0 and B to bucket 1
+ Map<BinaryRow, Map<Integer, RecordWriter<KeyValue>>> writers = new HashMap<>();
+ for (int bucket : Arrays.asList(0, 1)) {
+ List<KeyValue> kvs = partitionedData(5, gen);
+ writeData(store, kvs, partition, bucket, writers);
+ }
+ commitData(store, commitIdentifier++, writers);
+
+ // snapshot 2: commit -A (by cleaning bucket 0)
+ cleanBucket(store, partition, 0);
+
+ // snapshot 3: commit C to bucket 2
+ writers.clear();
+ writeData(store, partitionedData(5, gen), partition, 2, writers);
+ commitData(store, commitIdentifier++, writers);
+
+ ManifestList manifestList = store.manifestListFactory().create();
+ Snapshot snapshot1 = snapshotManager.snapshot(1);
+ List<ManifestFileMeta> snapshot1Data = snapshot1.dataManifests(manifestList);
+ Snapshot snapshot3 = snapshotManager.snapshot(3);
+ List<ManifestFileMeta> snapshot3Data = snapshot3.dataManifests(manifestList);
+
+ List<String> manifestLists =
+ Arrays.asList(snapshot1.baseManifestList(), snapshot1.deltaManifestList());
+
+ // create tag1
+ tagManager.createTag(snapshot1, "tag1");
+
+ // expire snapshot 1, 2
+ store.newExpire(1, 1, Long.MAX_VALUE).expire();
+
+ // check before deleting tag1
+ FileStorePathFactory pathFactory = store.pathFactory();
+ for (int i = 0; i < 3; i++) {
+ assertPathExists(fileIO, pathFactory.bucketPath(partition, i));
+ }
+ for (ManifestFileMeta manifestFileMeta : snapshot1Data) {
+ assertPathExists(fileIO, pathFactory.toManifestFilePath(manifestFileMeta.fileName()));
+ }
+ for (String manifestListName : manifestLists) {
+ assertPathExists(fileIO, pathFactory.toManifestListPath(manifestListName));
+ }
+
+ store.newTagDeletion().delete(tagManager.taggedSnapshot("tag1"));
+
+ // check data files
+ assertPathNotExists(fileIO, pathFactory.bucketPath(partition, 0));
+ assertPathExists(fileIO, pathFactory.bucketPath(partition, 1));
+ assertPathExists(fileIO, pathFactory.bucketPath(partition, 2));
+
+ // check manifests
+ for (ManifestFileMeta manifestFileMeta : snapshot1Data) {
+ Path path = pathFactory.toManifestFilePath(manifestFileMeta.fileName());
+ if (snapshot3Data.contains(manifestFileMeta)) {
+ assertPathExists(fileIO, path);
+ } else {
+ assertPathNotExists(fileIO, path);
+ }
+ }
+ for (String manifestListName : manifestLists) {
+ assertPathNotExists(fileIO, pathFactory.toManifestListPath(manifestListName));
+ }
+ }
+
+ @Test
+ public void testDeleteTagWithOtherTag() throws Exception {
+ TestFileStore store = createStore(TestKeyValueGenerator.GeneratorMode.NON_PARTITIONED, 3);
+ TagManager tagManager = new TagManager(fileIO, store.options().path());
+ SnapshotManager snapshotManager = store.snapshotManager();
+ TestKeyValueGenerator gen =
+ new TestKeyValueGenerator(TestKeyValueGenerator.GeneratorMode.NON_PARTITIONED);
+ BinaryRow partition = gen.getPartition(gen.next());
+
+ // snapshot 1: commit A to bucket 0
+ Map<BinaryRow, Map<Integer, RecordWriter<KeyValue>>> writers = new HashMap<>();
+ writeData(store, partitionedData(5, gen), partition, 0, writers);
+ commitData(store, commitIdentifier++, writers);
+
+ // snapshot 2: commit B to bucket 1
+ writers.clear();
+ writeData(store, partitionedData(5, gen), partition, 1, writers);
+ commitData(store, commitIdentifier++, writers);
+
+ // snapshot 3: commit -A (by cleaning bucket 0)
+ cleanBucket(store, partition, 0);
+
+ // snapshot 4: commit -B (by cleaning bucket 1)
+ cleanBucket(store, partition, 1);
+
+ // snapshot 5: commit C to snapshot 2 (used as the last snapshot)
+ cleanBucket(store, partition, 2);
+
+ ManifestList manifestList = store.manifestListFactory().create();
+ Snapshot snapshot2 = snapshotManager.snapshot(2);
+ List<ManifestFileMeta> snapshot2Data = snapshot2.dataManifests(manifestList);
+
+ List<String> manifestLists =
+ Arrays.asList(snapshot2.baseManifestList(), snapshot2.deltaManifestList());
+
+ // create tags
+ tagManager.createTag(snapshotManager.snapshot(1), "tag1");
+ tagManager.createTag(snapshotManager.snapshot(2), "tag2");
+ tagManager.createTag(snapshotManager.snapshot(4), "tag3");
+
+ // expire snapshot 1, 2, 3, 4
+ store.newExpire(1, 1, Long.MAX_VALUE).expire();
+
+ // check before deleting tag2
+ FileStorePathFactory pathFactory = store.pathFactory();
+ assertPathExists(fileIO, pathFactory.bucketPath(partition, 0));
+ assertPathExists(fileIO, pathFactory.bucketPath(partition, 1));
+
+ for (ManifestFileMeta manifestFileMeta : snapshot2Data) {
+ assertPathExists(fileIO, pathFactory.toManifestFilePath(manifestFileMeta.fileName()));
+ }
+ for (String manifestListName : manifestLists) {
+ assertPathExists(fileIO, pathFactory.toManifestListPath(manifestListName));
+ }
+
+ store.newTagDeletion().delete(tagManager.taggedSnapshot("tag2"));
+
+ // check data files
+ assertPathExists(fileIO, pathFactory.bucketPath(partition, 0));
+ assertPathNotExists(fileIO, pathFactory.bucketPath(partition, 1));
+
+ // check manifests
+ Snapshot tag1 = tagManager.taggedSnapshot("tag1");
+ Snapshot tag3 = tagManager.taggedSnapshot("tag3");
+ List<ManifestFileMeta> existing = tag1.dataManifests(manifestList);
+ existing.addAll(tag3.dataManifests(manifestList));
+ for (ManifestFileMeta manifestFileMeta : snapshot2Data) {
+ Path path = pathFactory.toManifestFilePath(manifestFileMeta.fileName());
+ if (existing.contains(manifestFileMeta)) {
+ assertPathExists(fileIO, path);
+ } else {
+ assertPathNotExists(fileIO, path);
+ }
+ }
+
+ for (String manifestListName : manifestLists) {
+ assertPathNotExists(fileIO, pathFactory.toManifestListPath(manifestListName));
+ }
+ }
+
private TestFileStore createStore(TestKeyValueGenerator.GeneratorMode mode) throws Exception {
return createStore(mode, 2);
}
diff --git a/paimon-core/src/test/java/org/apache/paimon/table/FileStoreTableTestBase.java b/paimon-core/src/test/java/org/apache/paimon/table/FileStoreTableTestBase.java
index 610f8cc6b..aff34ee45 100644
--- a/paimon-core/src/test/java/org/apache/paimon/table/FileStoreTableTestBase.java
+++ b/paimon-core/src/test/java/org/apache/paimon/table/FileStoreTableTestBase.java
@@ -746,6 +746,25 @@ public abstract class FileStoreTableTestBase {
"Tag name cannot be pure numeric string but is '10'."));
}
+ @Test
+ public void testDeleteTag() throws Exception {
+ FileStoreTable table = createFileStoreTable();
+
+ try (StreamTableWrite write = table.newWrite(commitUser);
+ StreamTableCommit commit = table.newCommit(commitUser)) {
+ write.write(rowData(1, 10, 100L));
+ commit.commit(0, write.prepareCommit(false, 1));
+ }
+
+ table.createTag("tag1", 1);
+ table.deleteTag("tag1");
+
+ assertThatThrownBy(() -> table.deleteTag("tag1"))
+ .satisfies(
+ AssertionUtils.anyCauseMatches(
+ IllegalArgumentException.class, "Tag 'tag1' doesn't exist."));
+ }
+
protected List<String> getResult(
TableRead read,
List<Split> splits,
diff --git a/paimon-core/src/test/java/org/apache/paimon/table/IndexFileExpireTableTest.java b/paimon-core/src/test/java/org/apache/paimon/table/IndexFileExpireTableTest.java
index 880bd84ab..63730cbe1 100644
--- a/paimon-core/src/test/java/org/apache/paimon/table/IndexFileExpireTableTest.java
+++ b/paimon-core/src/test/java/org/apache/paimon/table/IndexFileExpireTableTest.java
@@ -112,6 +112,35 @@ public class IndexFileExpireTableTest extends PrimaryKeyTableTestBase {
checkIndexFiles(tagManager.taggedSnapshot("tag5"));
}
+ @Test
+ public void testIndexFileExpirationWhenDeletingTag() throws Exception {
+ prepareExpireTable();
+ FileStoreExpireImpl expire = (FileStoreExpireImpl) table.store().newExpire();
+
+ table.createTag("tag3", 3);
+ table.createTag("tag5", 5);
+
+ long indexFileSize = indexFileSize();
+ long indexManifestSize = indexManifestSize();
+
+ // if snapshot 3 is not expired, deleting tag won't delete index files
+ table.deleteTag("tag3");
+ checkIndexFiles(3);
+ assertThat(indexFileSize()).isEqualTo(indexFileSize);
+ assertThat(indexManifestSize()).isEqualTo(indexManifestSize);
+
+ // test delete tag after expiring snapshots
+ table.createTag("tag3", 3);
+ expire.expireUntil(1, 7);
+ table.deleteTag("tag3");
+
+ TagManager tagManager = new TagManager(LocalFileIO.create(), table.path);
+ checkIndexFiles(7);
+ checkIndexFiles(tagManager.taggedSnapshot("tag5"));
+ assertThat(indexFileSize()).isEqualTo(4);
+ assertThat(indexManifestSize()).isEqualTo(2);
+ }
+
@Test
public void testIndexFileRollback() throws Exception {
prepareExpireTable();