-
Notifications
You must be signed in to change notification settings - Fork 3k
Core: Handle statistics file clean up from expireSnapshots #6090
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
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0714e82
Core: Handle statistics file clean up from expireSnapshots
ajantha-bhat 4b28215
Addressing the comments
ajantha-bhat 66e7df3
Address new review comments
ajantha-bhat 986f689
Update location as it uses the TestTable
ajantha-bhat 02fdca2
Address test review comments
ajantha-bhat e0f51fd
rename changes
ajantha-bhat 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 |
|---|---|---|
|
|
@@ -18,20 +18,29 @@ | |
| */ | ||
| package org.apache.iceberg; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.util.List; | ||
| import java.util.Set; | ||
| import java.util.UUID; | ||
| import java.util.concurrent.ConcurrentHashMap; | ||
| import java.util.concurrent.Executors; | ||
| import java.util.concurrent.atomic.AtomicInteger; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.iceberg.ManifestEntry.Status; | ||
| import org.apache.iceberg.exceptions.ValidationException; | ||
| import org.apache.iceberg.io.FileIO; | ||
| import org.apache.iceberg.puffin.Blob; | ||
| import org.apache.iceberg.puffin.Puffin; | ||
| import org.apache.iceberg.puffin.PuffinWriter; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableList; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.ImmutableSet; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Iterables; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Lists; | ||
| import org.apache.iceberg.relocated.com.google.common.collect.Sets; | ||
| import org.assertj.core.api.Assertions; | ||
| import org.junit.Assert; | ||
| import org.junit.Assume; | ||
| import org.junit.Test; | ||
|
|
@@ -1234,6 +1243,81 @@ public void testMultipleRefsAndCleanExpiredFilesFailsForIncrementalCleanup() { | |
| .commit()); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExpireWithStatisticsFiles() throws IOException { | ||
| table.newAppend().appendFile(FILE_A).commit(); | ||
| String statsFileLocation1 = statsFileLocation(table.location()); | ||
| StatisticsFile statisticsFile1 = | ||
| writeStatsFile( | ||
| table.currentSnapshot().snapshotId(), | ||
| table.currentSnapshot().sequenceNumber(), | ||
| statsFileLocation1, | ||
| table.io()); | ||
| commitStats(table, statisticsFile1); | ||
|
|
||
| table.newAppend().appendFile(FILE_B).commit(); | ||
| String statsFileLocation2 = statsFileLocation(table.location()); | ||
| StatisticsFile statisticsFile2 = | ||
| writeStatsFile( | ||
| table.currentSnapshot().snapshotId(), | ||
| table.currentSnapshot().sequenceNumber(), | ||
| statsFileLocation2, | ||
| table.io()); | ||
| commitStats(table, statisticsFile2); | ||
| Assert.assertEquals("Should have 2 statistics file", 2, table.statisticsFiles().size()); | ||
|
|
||
| long tAfterCommits = waitUntilAfter(table.currentSnapshot().timestampMillis()); | ||
| removeSnapshots(table).expireOlderThan(tAfterCommits).commit(); | ||
ajantha-bhat marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // only the current snapshot and its stats file should be retained | ||
| Assert.assertEquals("Should keep 1 snapshot", 1, Iterables.size(table.snapshots())); | ||
| Assertions.assertThat(table.statisticsFiles()) | ||
| .hasSize(1) | ||
| .extracting(StatisticsFile::snapshotId) | ||
| .as("Should contain only the statistics file of snapshot2") | ||
| .isEqualTo(Lists.newArrayList(statisticsFile2.snapshotId())); | ||
|
|
||
| Assertions.assertThat(new File(statsFileLocation1).exists()).isFalse(); | ||
| Assertions.assertThat(new File(statsFileLocation2).exists()).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testExpireWithStatisticsFilesWithReuse() throws IOException { | ||
| table.newAppend().appendFile(FILE_A).commit(); | ||
| String statsFileLocation1 = statsFileLocation(table.location()); | ||
| StatisticsFile statisticsFile1 = | ||
| writeStatsFile( | ||
| table.currentSnapshot().snapshotId(), | ||
| table.currentSnapshot().sequenceNumber(), | ||
| statsFileLocation1, | ||
| table.io()); | ||
| commitStats(table, statisticsFile1); | ||
|
|
||
| table.newAppend().appendFile(FILE_B).commit(); | ||
| // If an expired snapshot's stats file is reused for some reason by the live snapshots, | ||
| // that stats file should not get deleted from the file system as the live snapshots still | ||
| // reference it. | ||
| StatisticsFile statisticsFile2 = | ||
| reuseStatsFile(table.currentSnapshot().snapshotId(), statisticsFile1); | ||
| commitStats(table, statisticsFile2); | ||
|
|
||
| Assert.assertEquals("Should have 2 statistics file", 2, table.statisticsFiles().size()); | ||
|
|
||
| long tAfterCommits = waitUntilAfter(table.currentSnapshot().timestampMillis()); | ||
| removeSnapshots(table).expireOlderThan(tAfterCommits).commit(); | ||
|
|
||
| // only the current snapshot and its stats file (reused from previous snapshot) should be | ||
| // retained | ||
| Assert.assertEquals("Should keep 1 snapshot", 1, Iterables.size(table.snapshots())); | ||
| Assertions.assertThat(table.statisticsFiles()) | ||
| .hasSize(1) | ||
| .extracting(StatisticsFile::snapshotId) | ||
| .as("Should contain only the statistics file of snapshot2") | ||
| .isEqualTo(Lists.newArrayList(statisticsFile2.snapshotId())); | ||
| // the reused stats file should exist. | ||
| Assertions.assertThat(new File(statsFileLocation1).exists()).isTrue(); | ||
| } | ||
|
|
||
| @Test | ||
| public void testFailRemovingSnapshotWhenStillReferencedByBranch() { | ||
| table.newAppend().appendFile(FILE_A).commit(); | ||
|
|
@@ -1515,4 +1599,46 @@ private RemoveSnapshots removeSnapshots(Table table) { | |
| RemoveSnapshots removeSnapshots = (RemoveSnapshots) table.expireSnapshots(); | ||
| return (RemoveSnapshots) removeSnapshots.withIncrementalCleanup(incrementalCleanup); | ||
| } | ||
|
|
||
| private StatisticsFile writeStatsFile( | ||
| long snapshotId, long snapshotSequenceNumber, String statsLocation, FileIO fileIO) | ||
| throws IOException { | ||
| try (PuffinWriter puffinWriter = Puffin.write(fileIO.newOutputFile(statsLocation)).build()) { | ||
| puffinWriter.add( | ||
| new Blob( | ||
| "some-blob-type", | ||
| ImmutableList.of(1), | ||
| snapshotId, | ||
| snapshotSequenceNumber, | ||
| ByteBuffer.wrap("blob content".getBytes(StandardCharsets.UTF_8)))); | ||
| puffinWriter.finish(); | ||
|
|
||
| return new GenericStatisticsFile( | ||
| snapshotId, | ||
| statsLocation, | ||
| puffinWriter.fileSize(), | ||
| puffinWriter.footerSize(), | ||
| puffinWriter.writtenBlobsMetadata().stream() | ||
| .map(GenericBlobMetadata::from) | ||
| .collect(ImmutableList.toImmutableList())); | ||
| } | ||
| } | ||
|
|
||
| private StatisticsFile reuseStatsFile(long snapshotId, StatisticsFile statisticsFile) { | ||
| return new GenericStatisticsFile( | ||
| snapshotId, | ||
| statisticsFile.path(), | ||
| statisticsFile.fileSizeInBytes(), | ||
| statisticsFile.fileFooterSizeInBytes(), | ||
| statisticsFile.blobMetadata()); | ||
| } | ||
|
|
||
| private void commitStats(Table table, StatisticsFile statisticsFile) { | ||
| table.updateStatistics().setStatistics(statisticsFile.snapshotId(), statisticsFile).commit(); | ||
|
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. I find it odd that a single line like this is in a separate method. Seems like this could be inlined and would make the tests more readable. |
||
| } | ||
|
|
||
| private String statsFileLocation(String tableLocation) { | ||
| String statsFileName = "stats-file-" + UUID.randomUUID(); | ||
| return tableLocation + "/metadata/" + statsFileName; | ||
| } | ||
| } | ||
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.
Nit: I think we could just have expireStatisticsFilesLocation short circuit before computing in case before expiration is empty. Rather than have the if(!beforeExpiration.statisticsFiles().isEmpty()) in both cleanup implementations