-
Notifications
You must be signed in to change notification settings - Fork 3k
Use memory-backed streams in tests #4534
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
Use memory-backed streams in tests #4534
Conversation
| import java.util.UUID; | ||
|
|
||
| import static java.util.Objects.requireNonNull; | ||
| import static org.apache.iceberg.relocated.com.google.common.base.Preconditions.checkState; |
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.
We typically don't use static method imports. I think they are mostly disallowed by the Iceberg style enforcement.
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.
I noticed the test classes are exempt from the checks. I will fix this one nonetheless.Test*
45c90c4 to
201eb2a
Compare
|
AC |
201eb2a to
d9b424f
Compare
kbendick
left a comment
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.
Thanks @findepi! I know this is something that has been considered before, so it's good to see it being added.
Hopefully this will make tests that much faster. Left some style notes and a few comments.
| this.location = Objects.requireNonNull(location, "location is null"); | ||
| this.contents = Objects.requireNonNull(contents, "contents is null").clone(); |
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.
Usually we use Preconditions.checkNotNull, but since this code is in test the style rules are somewhat more lax.
core/src/test/java/org/apache/iceberg/io/InMemoryInputFile.java
Outdated
Show resolved
Hide resolved
| public boolean markSupported() { | ||
| throw new UnsupportedOperationException(); | ||
| } |
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: Is there a benefit to throwing UnsupportedOperationException or should this return false?
core/src/test/java/org/apache/iceberg/io/InMemoryOutputFile.java
Outdated
Show resolved
Hide resolved
| public void seek(long newPos) throws IOException { | ||
| delegate.reset(); | ||
| Preconditions.checkState(delegate.skip(newPos) == newPos, | ||
| "Invalid position %s within stream of length %s", newPos, length); | ||
| } |
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: We should consider adding an explicit check that the new position is non-negative.
The checkState call seems like it will cover that as skip shouldn't return a negative number, but it might be better for tests to add an explicit check. WDYT?
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.
I think that it's just tests, so exception message doesn't matter that much, and adding another check doesn't make the existing check unnecessary. i would leave this as is, if you don't mind
|
|
||
| private final String location; | ||
|
|
||
| private boolean exists; |
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.
Do we explicitly set exists to false anywhere or are we relying on the default value being false?
I'd prefer we explicitly set the value false somewhere for readability purposes.
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.
Added an explicit initializer.
Note that in Trino we have a hard rule that a field must not have an explicit initializer that is same as the default value for given type, so i am afraid I will need to be reminded about this from time to time. Please bear with me
| @Override | ||
| public PositionOutputStream create() { | ||
| if (exists) { | ||
| throw new RuntimeException("Already exists"); |
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.
Can we throw AlreadyExistsException to match the current LocalOutputFile#create() method?
iceberg/api/src/main/java/org/apache/iceberg/Files.java
Lines 57 to 59 in e45c19e
| if (file.exists()) { | |
| throw new AlreadyExistsException("File already exists: %s", file); | |
| } |
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.
Thanks, indeed this is what interface requires me to do
|
|
||
| private InputFile writeManifestList(ManifestFile manifest, int formatVersion) throws IOException { | ||
| OutputFile manifestList = Files.localOutput(temp.newFile()); | ||
| InMemoryOutputFile manifestList = new InMemoryOutputFile(); |
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.
Could just still be OutputFile right?
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.
reverted
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.
yes, fixed (not reverted)
| Assert.assertTrue("Delete should succeed", testFile.delete()); | ||
|
|
||
| try (FileAppender<Record> writer = Avro.write(Files.localOutput(testFile)) | ||
| InMemoryOutputFile outputFile = new InMemoryOutputFile(); |
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.
Could just be output File
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.
fixed
RussellSpitzer
left a comment
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 makes sense to me, although i was wondering when I saw the title whether you were going to implement a full FileIO since we could be using that now. I think this is fine for these tests though
kbendick
left a comment
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.
Outside of the out of date comment related to mark and the question around throwing for markSupported, this looks good for the current testing purposes.
My other comments are more informational or for clarification.
Not until i need that :) |
d9b424f to
9e7a4f5
Compare
|
AC |
| public InMemoryInputFile(String location, byte[] contents) { | ||
| Preconditions.checkNotNull(location, "location is null"); | ||
| Preconditions.checkNotNull(contents, "contents is null"); | ||
| this.location = location; | ||
| this.contents = contents.clone(); | ||
| } |
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.
As a side note: Typically we use ByteBuffers instead of raw byte[], so that we can get views over slices etc for free and help avoid additional allocations in many situations.
ByteBuffers also have the advantage of presenting views over type-specific byte arrays (for char, int, float, etc). And these views are indexed in terms of the type-specific size of the values, which is easier to reason about and also have bulk get and set methods.
This is test code, but if we were to ever move this to be code used in the project, we'd almost certainly want to use ByteBuffer instead of the raw byte[] throughout where possible.
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.
For internal representation byte[] is preferred, since the i can ByteArray{Output,Input}Stream}.
For constructor -- whatever it's easier to provide by the caller. I think we eventually will accept both.
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.
Yeah I'm good with that. If this were ported outside of test, I just wanted to make sure you were aware that request might come up (though it might not depending on the situation) 🙂
Sometimes an actual file is needed, but sometimes it's not. In the case where it isn't, we can use an in-memory `InputFile`, `OutputFile` implementation.
9e7a4f5 to
370672a
Compare
|
AC All comments applied or answered to. @RussellSpitzer @kbendick do you mind to take another look and let me know what else I could change here? |
|
@RussellSpitzer @rdblue can you please give me more advice here? |
kbendick
left a comment
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.
Reconfirming my +1. This looks good to me. Thanks @findepi!
|
thank you for the merge! |
Sometimes an actual file is needed, but sometimes it's not. In the case
where it isn't, we can use an in-memory
InputFile,OutputFileimplementation.
Extracted from #4537