Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -246,7 +246,14 @@ private Function<Path, List<Pair<Path, Long>>> pathProcessor(Set<Path> emptyDirs
List<FileStatus> files = tryBestListingDirs(path);

if (files.isEmpty()) {
emptyDirs.add(path);
try {
FileStatus dirStatus = fileIO.getFileStatus(path);
if (oldEnough(dirStatus)) {
emptyDirs.add(path);
}
} catch (IOException e) {
LOG.warn("IOException during check dirStatus for {}, ignore it", path, e);
}
return Collections.emptyList();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,14 @@ private List<Path> listFileDirs(Path dir, int level) {

List<Path> result = new ArrayList<>();
for (Path partitionPath : partitionPaths) {
result.addAll(listFileDirs(partitionPath, level - 1));
List<Path> sub = listFileDirs(partitionPath, level - 1);
if (sub.isEmpty()) {
// Empty partition (no bucket subdirs), include for empty-dir cleanup
LOG.info("Found empty partition directory for cleanup: {}", partitionPath);
result.add(partitionPath);
} else {
result.addAll(sub);
}
}
return result;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.attribute.FileTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
Expand Down Expand Up @@ -558,6 +560,13 @@ public void testRemovingEmptyDirectories() throws Exception {
assertThat(fileIO.exists(emptyDirectory1)).isTrue();
assertThat(fileIO.exists(emptyDirectory2)).isTrue();

Files.setLastModifiedTime(
tempDir.resolve("part1=1/part2=2/bucket-0"),
FileTime.fromMillis(System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2)));
Files.setLastModifiedTime(
tempDir.resolve("part1=1/part2=2/bucket-1"),
FileTime.fromMillis(System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2)));

LocalOrphanFilesClean orphanFilesClean = new LocalOrphanFilesClean(table);
List<Path> deleted = orphanFilesClean.clean().getDeletedFilesPath();
assertThat(fileIO.exists(emptyDirectory1)).isFalse();
Expand All @@ -566,6 +575,59 @@ public void testRemovingEmptyDirectories() throws Exception {
validate(deleted, snapshotData, new HashMap<>());
}

@Test
void testEmptyPartitionDirectories() throws Exception {
commit(Collections.singletonList(new TestPojo(1, 0, "a", "v1")));
commit(Collections.singletonList(new TestPojo(2, 0, "b", "v2")));

Path partitionPath1 = new Path(tablePath, "part1=0/part2=a");
Path partitionPath2 = new Path(tablePath, "part1=0/part2=b");
assertThat(fileIO.exists(partitionPath1)).isTrue();
assertThat(fileIO.exists(partitionPath2)).isTrue();

FileStatus[] partition2Files = fileIO.listStatus(partitionPath2);
assertThat(partition2Files).isNotEmpty();
for (FileStatus file : partition2Files) {
if (file.isDir() && file.getPath().getName().startsWith("bucket-")) {
FileStatus[] bucketFiles = fileIO.listStatus(file.getPath());
for (FileStatus bucketFile : bucketFiles) {
fileIO.deleteQuietly(bucketFile.getPath());
}
fileIO.deleteQuietly(file.getPath());
}
}
assertThat(fileIO.listStatus(partitionPath2)).isEmpty();
assertThat(fileIO.exists(partitionPath2)).isTrue();

Path emptyNonLeafPartitionPath = new Path(tablePath, "part1=1");
fileIO.mkdirs(emptyNonLeafPartitionPath);

long oldTime = System.currentTimeMillis() - TimeUnit.DAYS.toMillis(2);
Files.setLastModifiedTime(tempDir.resolve("part1=0/part2=b"), FileTime.fromMillis(oldTime));
Files.setLastModifiedTime(tempDir.resolve("part1=1"), FileTime.fromMillis(oldTime));

LocalOrphanFilesClean orphanFilesClean = new LocalOrphanFilesClean(table);
orphanFilesClean.clean();

assertThat(fileIO.exists(partitionPath2))
.as("Empty partition (no bucket subdirs) is cleaned by orphan files clean.")
.isFalse();
assertThat(fileIO.exists(emptyNonLeafPartitionPath))
.as(
"Empty non-leaf partition dir (e.g. part1=1 with no part2) is cleaned by orphan files clean.")
.isFalse();
assertThat(fileIO.exists(partitionPath1)).isTrue();

Path recentEmptyPath = new Path(tablePath, "part1=2");
fileIO.mkdirs(recentEmptyPath);
LocalOrphanFilesClean cleanRecent =
new LocalOrphanFilesClean(table, System.currentTimeMillis() - 1, false);
cleanRecent.clean();
assertThat(fileIO.exists(recentEmptyPath))
.as("Recent empty partition dir must not be deleted (age safeguard).")
.isTrue();
}

private void writeData(
SnapshotManager snapshotManager,
List<List<TestPojo>> committedData,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,43 @@

package org.apache.paimon.operation;

import org.apache.paimon.CoreOptions;
import org.apache.paimon.data.BinaryString;
import org.apache.paimon.data.GenericRow;
import org.apache.paimon.fs.FileIO;
import org.apache.paimon.fs.Path;
import org.apache.paimon.fs.local.LocalFileIO;
import org.apache.paimon.options.Options;
import org.apache.paimon.schema.Schema;
import org.apache.paimon.schema.SchemaManager;
import org.apache.paimon.schema.SchemaUtils;
import org.apache.paimon.schema.TableSchema;
import org.apache.paimon.table.FileStoreTable;
import org.apache.paimon.table.FileStoreTableFactory;
import org.apache.paimon.table.sink.TableCommitImpl;
import org.apache.paimon.table.sink.TableWriteImpl;
import org.apache.paimon.types.DataType;
import org.apache.paimon.types.DataTypes;
import org.apache.paimon.types.RowKind;
import org.apache.paimon.types.RowType;

import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;

/** Utils for {@link OrphanFilesClean}. */
public class OrphanFilesCleanTest {

@Rule public TemporaryFolder tempDir = new TemporaryFolder();

@Test
public void testOlderThanMillis() {
// normal olderThan
Expand All @@ -37,4 +67,80 @@ public void testOlderThanMillis() {
.hasMessage(
"The arg olderThan must be less than now, because dataFiles that are currently being written and not referenced by snapshots will be mistakenly cleaned up.");
}

@Test
public void testListPaimonFileDirsWithEmptyPartition() throws Exception {
Path tablePath = new Path(tempDir.newFolder().toURI());
FileIO fileIO = LocalFileIO.create();
RowType rowType =
RowType.of(
new DataType[] {
DataTypes.INT(), DataTypes.INT(), DataTypes.STRING(), DataTypes.STRING()
},
new String[] {"pk", "part1", "part2", "value"});
FileStoreTable table = createFileStoreTable(fileIO, tablePath, rowType, new Options());
String commitUser = UUID.randomUUID().toString();
try (TableWriteImpl<?> write = table.newWrite(commitUser);
TableCommitImpl commit = table.newCommit(commitUser)) {
write.write(
GenericRow.ofKind(
RowKind.INSERT,
1,
0,
BinaryString.fromString("a"),
BinaryString.fromString("v1")));
commit.commit(0, write.prepareCommit(true, 0));
}

Path emptyPartitionPath = new Path(tablePath, "part1=0/part2=b");
fileIO.mkdirs(emptyPartitionPath);
Path emptyNonLeafPartitionPath = new Path(tablePath, "part1=1");
fileIO.mkdirs(emptyNonLeafPartitionPath);

java.lang.reflect.Method method =
LocalOrphanFilesClean.class.getSuperclass().getDeclaredMethod("listPaimonFileDirs");
method.setAccessible(true);
@SuppressWarnings("unchecked")
List<Path> dirs = (List<Path>) method.invoke(new LocalOrphanFilesClean(table));

assertThat(dirs)
.as(
"Empty partition (no bucket subdirs) is listed by listPaimonFileDirs for empty-dir cleanup")
.contains(emptyPartitionPath);
assertThat(dirs)
.as(
"Empty non-leaf partition dir (e.g. part1=1 with no part2) is listed by listPaimonFileDirs")
.contains(emptyNonLeafPartitionPath);
}

@Test
public void testDeleteNonEmptyDir() throws Exception {
Path dir = new Path(tempDir.newFolder().toURI().toString(), "part1=0");
FileIO fileIO = LocalFileIO.create();
fileIO.mkdirs(dir);
Path file = new Path(dir, "data.dat");
fileIO.writeFile(file, "x", true);

assertThat(fileIO.exists(dir)).isTrue();
assertThatThrownBy(() -> fileIO.delete(dir, false))
.isInstanceOf(IOException.class)
.hasMessageContaining("not empty");
assertThat(fileIO.exists(dir)).isTrue();
}

private FileStoreTable createFileStoreTable(
FileIO fileIO, Path tablePath, RowType rowType, Options conf) throws Exception {
conf.set(CoreOptions.PATH, tablePath.toString());
conf.set(CoreOptions.BUCKET, 2);
TableSchema tableSchema =
SchemaUtils.forceCommit(
new SchemaManager(fileIO, tablePath),
new Schema(
rowType.getFields(),
Arrays.asList("part1", "part2"),
Arrays.asList("pk", "part1", "part2"),
conf.toMap(),
""));
return FileStoreTableFactory.create(fileIO, tablePath, tableSchema);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -290,7 +290,18 @@ public void processElement(
}
}
if (files.isEmpty()) {
ctx.output(emptyDirOutputTag, dirPath);
try {
FileStatus dirStatus =
fileIO.getFileStatus(dirPath);
if (oldEnough(dirStatus)) {
ctx.output(emptyDirOutputTag, dirPath);
}
} catch (IOException e) {
LOG.warn(
"IOException during check dirStatus for {}, ignore it",
dirPath,
e);
}
}
}
})
Expand Down
Loading
Loading