Skip to content
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

findepi
Copy link
Member

@findepi findepi commented Apr 11, 2022

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.

Extracted from #4537

import java.util.UUID;

import static java.util.Objects.requireNonNull;
import static org.apache.iceberg.relocated.com.google.common.base.Preconditions.checkState;
Copy link
Contributor

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.

Copy link
Member Author

@findepi findepi Apr 12, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I noticed the Test* test classes are exempt from the checks. I will fix this one nonetheless.

@findepi findepi force-pushed the findepi/use-memory-backed-streams-in-tests-3b5a04 branch from 45c90c4 to 201eb2a Compare April 12, 2022 07:26
@findepi
Copy link
Member Author

findepi commented Apr 12, 2022

AC

@findepi findepi requested a review from rdblue April 12, 2022 07:26
@findepi findepi force-pushed the findepi/use-memory-backed-streams-in-tests-3b5a04 branch from 201eb2a to d9b424f Compare April 12, 2022 07:41
Copy link
Contributor

@kbendick kbendick left a 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.

Comment on lines 38 to 39
this.location = Objects.requireNonNull(location, "location is null");
this.contents = Objects.requireNonNull(contents, "contents is null").clone();
Copy link
Contributor

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.

Comment on lines 110 to 113
public boolean markSupported() {
throw new UnsupportedOperationException();
}
Copy link
Contributor

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?

Comment on lines 78 to 83
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);
}
Copy link
Contributor

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?

Copy link
Member Author

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;
Copy link
Contributor

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.

Copy link
Member Author

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");
Copy link
Contributor

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?

if (file.exists()) {
throw new AlreadyExistsException("File already exists: %s", file);
}

Copy link
Member Author

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

@@ -235,7 +236,7 @@ void checkRewrittenManifest(ManifestFile manifest, long expectedSequenceNumber,
}

private InputFile writeManifestList(ManifestFile manifest, int formatVersion) throws IOException {
OutputFile manifestList = Files.localOutput(temp.newFile());
InMemoryOutputFile manifestList = new InMemoryOutputFile();
Copy link
Member

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?

Copy link
Member Author

@findepi findepi Apr 15, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reverted

Copy link
Member Author

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();
Copy link
Member

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

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Copy link
Member

@RussellSpitzer RussellSpitzer left a 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

Copy link
Contributor

@kbendick kbendick left a 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.

@findepi
Copy link
Member Author

findepi commented Apr 15, 2022

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

Not until i need that :)

@findepi findepi force-pushed the findepi/use-memory-backed-streams-in-tests-3b5a04 branch from d9b424f to 9e7a4f5 Compare April 15, 2022 12:25
@findepi
Copy link
Member Author

findepi commented Apr 15, 2022

AC

Comment on lines +36 to +41
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();
}
Copy link
Contributor

@kbendick kbendick Apr 15, 2022

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.

Copy link
Member Author

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.

Copy link
Contributor

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.
@findepi findepi force-pushed the findepi/use-memory-backed-streams-in-tests-3b5a04 branch from 9e7a4f5 to 370672a Compare April 19, 2022 10:20
@findepi
Copy link
Member Author

findepi commented Apr 19, 2022

AC

All comments applied or answered to.
Do you want me to mark them as resolved?

@RussellSpitzer @kbendick do you mind to take another look and let me know what else I could change here?

@findepi
Copy link
Member Author

findepi commented Apr 20, 2022

@RussellSpitzer @rdblue can you please give me more advice here?

Copy link
Contributor

@kbendick kbendick left a 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!

@RussellSpitzer RussellSpitzer merged commit ed54952 into apache:master Apr 21, 2022
@RussellSpitzer
Copy link
Member

Thanks @findepi for the contribution and @kbendick and @rdblue for review

@findepi findepi deleted the findepi/use-memory-backed-streams-in-tests-3b5a04 branch April 22, 2022 07:33
@findepi
Copy link
Member Author

findepi commented Apr 22, 2022

thank you for the merge!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants