-
Notifications
You must be signed in to change notification settings - Fork 3k
Minor MicroBatch Class Cleanup and Return to Spark Structured Streaming Reader #1627
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
Closed
Closed
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
4edcc95
Fix typo in MicroBatchBuilder#skipManifests and add a test to ensure …
kbendick e5b4964
Add todo note about splitting up the test into fewer microbatches to …
kbendick bba0706
Remove isEmpty and add in VisibleForTesting annotation
kbendick 76eca4a
Break into two tests and add in todos about deletes - how would spark…
kbendick a59dbb6
Remove VisibleForTesting
kbendick e2ca146
Clarify comment
kbendick d8592da
Clarify other comments
kbendick 21155ae
Final comment clarififcation
kbendick 455d59f
Uncomment commented out declaration
kbendick 4686272
Get rid of note to self
kbendick 0fb7123
Use uppercase L when declaring long literal
kbendick 9178a7f
Change indent spacing
kbendick 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -50,15 +50,15 @@ public void setupTableProperties() { | |
| public void testGenerateMicroBatch() { | ||
| add(table.newAppend(), files("A", "B", "C", "D", "E")); | ||
|
|
||
| MicroBatch batch = MicroBatches.from(table.snapshot(1L), table.io()) | ||
| MicroBatch batch0 = MicroBatches.from(table.snapshot(1L), table.io()) | ||
| .specsById(table.specs()) | ||
| .generate(0, Long.MAX_VALUE, true); | ||
| Assert.assertEquals(batch.snapshotId(), 1L); | ||
| Assert.assertEquals(batch.startFileIndex(), 0); | ||
| Assert.assertEquals(batch.endFileIndex(), 5); | ||
| Assert.assertEquals(batch.sizeInBytes(), 50); | ||
| Assert.assertTrue(batch.lastIndexOfSnapshot()); | ||
| filesMatch(Lists.newArrayList("A", "B", "C", "D", "E"), filesToScan(batch.tasks())); | ||
| Assert.assertEquals(batch0.snapshotId(), 1L); | ||
| Assert.assertEquals(batch0.startFileIndex(), 0); | ||
| Assert.assertEquals(batch0.endFileIndex(), 5); | ||
| Assert.assertEquals(batch0.sizeInBytes(), 50); | ||
| Assert.assertTrue(batch0.lastIndexOfSnapshot()); | ||
| filesMatch(Lists.newArrayList("A", "B", "C", "D", "E"), filesToScan(batch0.tasks())); | ||
|
|
||
| MicroBatch batch1 = MicroBatches.from(table.snapshot(1L), table.io()) | ||
| .specsById(table.specs()) | ||
|
|
@@ -89,19 +89,20 @@ public void testGenerateMicroBatch() { | |
| public void testGenerateMicroBatchWithSmallTargetSize() { | ||
| add(table.newAppend(), files("A", "B", "C", "D", "E")); | ||
|
|
||
| MicroBatch batch = MicroBatches.from(table.snapshot(1L), table.io()) | ||
| MicroBatch batch0 = MicroBatches.from(table.snapshot(1L), table.io()) | ||
| .specsById(table.specs()) | ||
| .generate(0, 10L, true); | ||
| Assert.assertEquals(batch.snapshotId(), 1L); | ||
| Assert.assertEquals(batch.startFileIndex(), 0); | ||
| Assert.assertEquals(batch.endFileIndex(), 1); | ||
| Assert.assertEquals(batch.sizeInBytes(), 10); | ||
| Assert.assertFalse(batch.lastIndexOfSnapshot()); | ||
| filesMatch(Lists.newArrayList("A"), filesToScan(batch.tasks())); | ||
| Assert.assertEquals(batch0.snapshotId(), 1L); | ||
| Assert.assertEquals(batch0.startFileIndex(), 0); | ||
| Assert.assertEquals(batch0.endFileIndex(), 1); | ||
| Assert.assertEquals(batch0.sizeInBytes(), 10); | ||
| Assert.assertFalse(batch0.lastIndexOfSnapshot()); | ||
| filesMatch(Lists.newArrayList("A"), filesToScan(batch0.tasks())); | ||
|
|
||
| MicroBatch batch1 = MicroBatches.from(table.snapshot(1L), table.io()) | ||
| .specsById(table.specs()) | ||
| .generate(batch.endFileIndex(), 5L, true); | ||
| .generate(batch0.endFileIndex(), 5L, true); | ||
| Assert.assertEquals(batch1.startFileIndex(), 1); | ||
| Assert.assertEquals(batch1.endFileIndex(), 2); | ||
| Assert.assertEquals(batch1.sizeInBytes(), 10); | ||
| filesMatch(Lists.newArrayList("B"), filesToScan(batch1.tasks())); | ||
|
|
@@ -140,15 +141,104 @@ public void testGenerateMicroBatchWithSmallTargetSize() { | |
| Assert.assertTrue(batch5.lastIndexOfSnapshot()); | ||
| } | ||
|
|
||
| private static DataFile file(String name) { | ||
| @Test | ||
| public void testMicroBatchRespectsRequestedMaximumSize() { | ||
| // Add files A-E, all of 10kb, and process in multiple microbatches of varying sizes, | ||
| // emulating perhaps a dynamically growing source (assuming the total batch is being | ||
| // built by one process, as in Flink or on the driver in Spark). | ||
| add(table.newAppend(), files("A", "B", "C", "D", "E")); | ||
|
|
||
| // Request 10kb - Receive file A. | ||
| MicroBatch batch0 = MicroBatches.from(table.snapshot(1L), table.io()) | ||
| .specsById(table.specs()) | ||
| .generate(0, 10L, true); | ||
| Assert.assertEquals(batch0.snapshotId(), 1L); | ||
| Assert.assertEquals(batch0.startFileIndex(), 0); | ||
| Assert.assertEquals(batch0.endFileIndex(), 1); | ||
| Assert.assertEquals(batch0.sizeInBytes(), 10); | ||
| Assert.assertFalse(batch0.lastIndexOfSnapshot()); | ||
| filesMatch(Lists.newArrayList("A"), filesToScan(batch0.tasks())); | ||
|
|
||
| // Request 30kb. Receive B, C, and D. | ||
| MicroBatch batch1 = MicroBatches.from(table.snapshot(1L), table.io()) | ||
| .specsById(table.specs()) | ||
| .generate(batch0.endFileIndex(), 35L, false); | ||
| Assert.assertEquals(batch1.startFileIndex(), 1); | ||
| Assert.assertEquals(batch1.endFileIndex(), 4); | ||
| Assert.assertEquals(batch1.sizeInBytes(), 30); | ||
| filesMatch(Lists.newArrayList("B", "C", "D"), filesToScan(batch1.tasks())); | ||
| Assert.assertFalse(batch1.lastIndexOfSnapshot()); | ||
|
|
||
| // Request 35kb - Receive File E which is the end of the input, only 10kb. | ||
| MicroBatch batch2 = MicroBatches.from(table.snapshot(1L), table.io()) | ||
| .specsById(table.specs()) | ||
| .generate(batch1.endFileIndex(), 35L, false); | ||
| Assert.assertEquals(4, batch2.startFileIndex()); | ||
| Assert.assertEquals(5, batch2.endFileIndex()); | ||
| Assert.assertEquals(10, batch2.sizeInBytes()); | ||
| filesMatch(Lists.newArrayList("E"), filesToScan(batch2.tasks())); | ||
| } | ||
|
|
||
| @Test | ||
| public void testReadingSnapshotIsNotInterruptedByChildSnapshot() { | ||
| // Add files A-E, all of 10kb, and process the single generated snapshot | ||
| // in multiple microbatches. | ||
| add(table.newAppend(), files("A", "B", "C", "D", "E")); | ||
| Assert.assertEquals(1L, table.currentSnapshot().snapshotId()); | ||
|
|
||
| // Request a batch of 40kb - Reads in A, B, C, and D. | ||
| MicroBatch batch0 = MicroBatches.from(table.snapshot(1L), table.io()) | ||
| .specsById(table.specs()) | ||
| .generate(0, 40L, false); | ||
| Assert.assertEquals(0, batch0.startFileIndex()); | ||
| Assert.assertEquals(4, batch0.endFileIndex()); | ||
| Assert.assertEquals(40, batch0.sizeInBytes()); | ||
| filesMatch(Lists.newArrayList("A", "B", "C", "D"), filesToScan(batch0.tasks())); | ||
| Assert.assertFalse(batch0.lastIndexOfSnapshot()); | ||
|
|
||
| // Concurrent write sometime after the start of the last batch and before the next batch. | ||
| final long sizeOfFileF = 25L; | ||
| add(table.newAppend(), | ||
| Collections.singletonList(fileWithSize("F", sizeOfFileF))); | ||
| Assert.assertEquals(2L, table.currentSnapshot().snapshotId()); | ||
|
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 will generate a new snapshot, seems unrelated to the |
||
|
|
||
| // Read the last 10kb of Snapshot 1. | ||
| // Simulates desired stream behavior for example on Trigger.Once(). | ||
| MicroBatch batch1 = MicroBatches.from(table.snapshot(1L), table.io()) | ||
| .specsById(table.specs()) | ||
| .generate(batch0.endFileIndex(), 40L, false); | ||
| Assert.assertEquals(4, batch1.startFileIndex()); | ||
| Assert.assertEquals(5, batch1.endFileIndex()); | ||
| Assert.assertEquals(10, batch1.sizeInBytes()); | ||
| filesMatch(Lists.newArrayList("E"), filesToScan(batch1.tasks())); | ||
| Assert.assertTrue(batch1.lastIndexOfSnapshot()); | ||
|
|
||
| // Show that the next batch / snapshot can be read. | ||
| MicroBatch batch2 = MicroBatches.from(table.currentSnapshot(), table.io()) | ||
| .specsById(table.specs()) | ||
| .generate(0, 40L, true); | ||
| Assert.assertEquals(0, batch2.startFileIndex()); | ||
| Assert.assertEquals(1, batch2.endFileIndex()); | ||
| Assert.assertEquals(sizeOfFileF, batch2.sizeInBytes()); | ||
| filesMatch(Lists.newArrayList("F"), filesToScan(batch2.tasks())); | ||
| Assert.assertTrue(batch2.lastIndexOfSnapshot()); | ||
|
|
||
| } | ||
|
|
||
| private static DataFile fileWithSize(String name, long newFileSizeInBytes) { | ||
| return DataFiles.builder(SPEC) | ||
| .withPath(name + ".parquet") | ||
| .withFileSizeInBytes(10) | ||
| .withFileSizeInBytes(newFileSizeInBytes) | ||
| .withPartitionPath("data_bucket=0") // easy way to set partition data for now | ||
| .withRecordCount(1) | ||
| .build(); | ||
| } | ||
|
|
||
| // Returns default parquet file with size of 10b and 1 record in bucket 0. | ||
| private static DataFile file(String name) { | ||
| return fileWithSize(name, 10L); | ||
| } | ||
|
|
||
| private static void add(AppendFiles appendFiles, List<DataFile> adds) { | ||
| for (DataFile f : adds) { | ||
| appendFiles.appendFile(f); | ||
|
|
||
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.
This update to this java doc comment is what inspired this PR.
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.
The change is not correct. If the
startFileIndexis 4, then "(m2, 3)" should not be skipped, the 4th index is included in "(m2, 3)"