-
Notifications
You must be signed in to change notification settings - Fork 3k
Spark 3.2: Add compareWithFileList param to DeleteOrphanFiles action
#4503
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
rdblue
merged 2 commits into
apache:master
from
bijanhoule:delete-orphan-files-actual-files-table
Apr 24, 2022
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,6 +21,7 @@ | |
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.sql.Timestamp; | ||
| import java.util.Arrays; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
@@ -53,6 +54,7 @@ | |
| import org.apache.iceberg.relocated.com.google.common.collect.Maps; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Sets; | ||
| import org.apache.iceberg.spark.SparkTestBase; | ||
| import org.apache.iceberg.spark.source.FilePathLastModifiedRecord; | ||
| import org.apache.iceberg.spark.source.ThreeColumnRecord; | ||
| import org.apache.iceberg.types.Types; | ||
| import org.apache.spark.sql.Dataset; | ||
|
|
@@ -703,4 +705,120 @@ public void testGarbageCollectionDisabled() { | |
| ValidationException.class, "Cannot delete orphan files: GC is disabled", | ||
| () -> SparkActions.get().deleteOrphanFiles(table).execute()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testCompareToFileList() throws IOException, InterruptedException { | ||
| Table table = | ||
| TABLES.create(SCHEMA, PartitionSpec.unpartitioned(), Maps.newHashMap(), tableLocation); | ||
|
|
||
| List<ThreeColumnRecord> records = | ||
| Lists.newArrayList(new ThreeColumnRecord(1, "AAAAAAAAAA", "AAAA")); | ||
|
|
||
| Dataset<Row> df = spark.createDataFrame(records, ThreeColumnRecord.class).coalesce(1); | ||
|
|
||
| df.select("c1", "c2", "c3").write().format("iceberg").mode("append").save(tableLocation); | ||
|
|
||
| df.select("c1", "c2", "c3").write().format("iceberg").mode("append").save(tableLocation); | ||
|
|
||
| Path dataPath = new Path(tableLocation + "/data"); | ||
| FileSystem fs = dataPath.getFileSystem(spark.sessionState().newHadoopConf()); | ||
| List<FilePathLastModifiedRecord> validFiles = | ||
| Arrays.stream(fs.listStatus(dataPath, HiddenPathFilter.get())) | ||
| .filter(FileStatus::isFile) | ||
| .map( | ||
| file -> | ||
| new FilePathLastModifiedRecord( | ||
| file.getPath().toString(), new Timestamp(file.getModificationTime()))) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| Assert.assertEquals("Should be 2 valid files", 2, validFiles.size()); | ||
|
|
||
| df.write().mode("append").parquet(tableLocation + "/data"); | ||
|
|
||
| List<FilePathLastModifiedRecord> allFiles = | ||
| Arrays.stream(fs.listStatus(dataPath, HiddenPathFilter.get())) | ||
| .filter(FileStatus::isFile) | ||
| .map( | ||
| file -> | ||
| new FilePathLastModifiedRecord( | ||
| file.getPath().toString(), new Timestamp(file.getModificationTime()))) | ||
| .collect(Collectors.toList()); | ||
|
|
||
| Assert.assertEquals("Should be 3 files", 3, allFiles.size()); | ||
|
|
||
| List<FilePathLastModifiedRecord> invalidFiles = Lists.newArrayList(allFiles); | ||
| invalidFiles.removeAll(validFiles); | ||
| List<String> invalidFilePaths = | ||
| invalidFiles.stream() | ||
| .map(FilePathLastModifiedRecord::getFilePath) | ||
| .collect(Collectors.toList()); | ||
| Assert.assertEquals("Should be 1 invalid file", 1, invalidFiles.size()); | ||
|
|
||
| // sleep for 1 second to ensure files will be old enough | ||
| Thread.sleep(1000); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should use |
||
|
|
||
| SparkActions actions = SparkActions.get(); | ||
|
|
||
| Dataset<Row> compareToFileList = | ||
| spark | ||
| .createDataFrame(allFiles, FilePathLastModifiedRecord.class) | ||
| .withColumnRenamed("filePath", "file_path") | ||
| .withColumnRenamed("lastModified", "last_modified"); | ||
|
|
||
| DeleteOrphanFiles.Result result1 = | ||
| ((BaseDeleteOrphanFilesSparkAction) actions.deleteOrphanFiles(table)) | ||
| .compareToFileList(compareToFileList) | ||
| .deleteWith(s -> { }) | ||
| .execute(); | ||
| Assert.assertTrue( | ||
| "Default olderThan interval should be safe", | ||
| Iterables.isEmpty(result1.orphanFileLocations())); | ||
|
|
||
| DeleteOrphanFiles.Result result2 = | ||
| ((BaseDeleteOrphanFilesSparkAction) actions.deleteOrphanFiles(table)) | ||
| .compareToFileList(compareToFileList) | ||
| .olderThan(System.currentTimeMillis()) | ||
| .deleteWith(s -> { }) | ||
| .execute(); | ||
| Assert.assertEquals( | ||
| "Action should find 1 file", invalidFilePaths, result2.orphanFileLocations()); | ||
| Assert.assertTrue( | ||
| "Invalid file should be present", fs.exists(new Path(invalidFilePaths.get(0)))); | ||
|
|
||
| DeleteOrphanFiles.Result result3 = | ||
| ((BaseDeleteOrphanFilesSparkAction) actions.deleteOrphanFiles(table)) | ||
| .compareToFileList(compareToFileList) | ||
| .olderThan(System.currentTimeMillis()) | ||
| .execute(); | ||
| Assert.assertEquals( | ||
| "Action should delete 1 file", invalidFilePaths, result3.orphanFileLocations()); | ||
| Assert.assertFalse( | ||
| "Invalid file should not be present", fs.exists(new Path(invalidFilePaths.get(0)))); | ||
|
|
||
| List<ThreeColumnRecord> expectedRecords = Lists.newArrayList(); | ||
| expectedRecords.addAll(records); | ||
| expectedRecords.addAll(records); | ||
|
|
||
| Dataset<Row> resultDF = spark.read().format("iceberg").load(tableLocation); | ||
| List<ThreeColumnRecord> actualRecords = | ||
| resultDF.as(Encoders.bean(ThreeColumnRecord.class)).collectAsList(); | ||
| Assert.assertEquals("Rows must match", expectedRecords, actualRecords); | ||
|
|
||
| List<FilePathLastModifiedRecord> outsideLocationMockFiles = | ||
| Lists.newArrayList(new FilePathLastModifiedRecord("/tmp/mock1", new Timestamp(0L))); | ||
|
|
||
| Dataset<Row> compareToFileListWithOutsideLocation = | ||
| spark | ||
| .createDataFrame(outsideLocationMockFiles, FilePathLastModifiedRecord.class) | ||
| .withColumnRenamed("filePath", "file_path") | ||
| .withColumnRenamed("lastModified", "last_modified"); | ||
|
|
||
| DeleteOrphanFiles.Result result4 = | ||
| ((BaseDeleteOrphanFilesSparkAction) actions.deleteOrphanFiles(table)) | ||
| .compareToFileList(compareToFileListWithOutsideLocation) | ||
| .deleteWith(s -> { }) | ||
| .execute(); | ||
| Assert.assertEquals( | ||
| "Action should find nothing", Lists.newArrayList(), result4.orphanFileLocations()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@aokolnychyi -- re: #4503 (comment) does this
startsWithlocation filtering seem reasonable to you for a short-term solution? I know there's some discussion about making some larger changes here.