-
Notifications
You must be signed in to change notification settings - Fork 1.5k
PARQUET-2134: Improve binding to ByteBufferReadable #971
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
steveloughran
wants to merge
2
commits into
apache:master
from
steveloughran:feature/PARQUET-2134-byte-buffer
Closed
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,16 +19,15 @@ | |
|
|
||
| package org.apache.parquet.hadoop.util; | ||
|
|
||
| import org.apache.hadoop.fs.ByteBufferReadable; | ||
| import org.apache.hadoop.fs.FSDataInputStream; | ||
| import org.apache.hadoop.fs.FSDataOutputStream; | ||
| import org.apache.parquet.io.ParquetDecodingException; | ||
| import org.apache.parquet.io.SeekableInputStream; | ||
| import org.apache.parquet.io.PositionOutputStream; | ||
| import org.apache.parquet.io.SeekableInputStream; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.lang.reflect.Constructor; | ||
| import java.lang.reflect.InvocationTargetException; | ||
| import java.io.InputStream; | ||
| import java.util.Objects; | ||
|
|
||
| /** | ||
|
|
@@ -38,9 +37,6 @@ public class HadoopStreams { | |
|
|
||
| private static final Logger LOG = LoggerFactory.getLogger(HadoopStreams.class); | ||
|
|
||
| private static final Class<?> byteBufferReadableClass = getReadableClass(); | ||
| static final Constructor<SeekableInputStream> h2SeekableConstructor = getH2SeekableConstructor(); | ||
|
|
||
| /** | ||
| * Wraps a {@link FSDataInputStream} in a {@link SeekableInputStream} | ||
| * implementation for Parquet readers. | ||
|
|
@@ -50,51 +46,45 @@ public class HadoopStreams { | |
| */ | ||
| public static SeekableInputStream wrap(FSDataInputStream stream) { | ||
| Objects.requireNonNull(stream, "Cannot wrap a null input stream"); | ||
| if (byteBufferReadableClass != null && h2SeekableConstructor != null && | ||
| byteBufferReadableClass.isInstance(stream.getWrappedStream())) { | ||
| try { | ||
| return h2SeekableConstructor.newInstance(stream); | ||
| } catch (InstantiationException | IllegalAccessException e) { | ||
| LOG.warn("Could not instantiate H2SeekableInputStream, falling back to byte array reads", e); | ||
| return new H1SeekableInputStream(stream); | ||
| } catch (InvocationTargetException e) { | ||
| throw new ParquetDecodingException( | ||
| "Could not instantiate H2SeekableInputStream", e.getTargetException()); | ||
| } | ||
| if (isWrappedStreamByteBufferReadable(stream)) { | ||
| return new H2SeekableInputStream(stream); | ||
| } else { | ||
| return new H1SeekableInputStream(stream); | ||
| } | ||
| } | ||
|
|
||
| private static Class<?> getReadableClass() { | ||
| try { | ||
| return Class.forName("org.apache.hadoop.fs.ByteBufferReadable"); | ||
| } catch (ClassNotFoundException | NoClassDefFoundError e) { | ||
| return null; | ||
| /** | ||
| * Is the inner stream byte buffer readable? | ||
| * The test is "the stream is not FSDataInputStream | ||
| * and implements ByteBufferReadable' | ||
| * | ||
| * That is: all streams which implement ByteBufferReadable | ||
| * other than FSDataInputStream successfuly support read(ByteBuffer). | ||
| * This is true for all filesytem clients the hadoop codebase. | ||
| * | ||
| * In hadoop 3.3.0+, the StreamCapabilities probe can be used to | ||
| * check this: only those streams which provide the read(ByteBuffer) | ||
| * semantics MAY return true for the probe "in:readbytebuffer"; | ||
| * FSDataInputStream will pass the probe down to the underlying stream. | ||
| * | ||
| * @param stream stream to probe | ||
| * @return true if it is safe to a H2SeekableInputStream to access the data | ||
| */ | ||
| private static boolean isWrappedStreamByteBufferReadable(FSDataInputStream stream) { | ||
| if (stream.hasCapability("in:readbytebuffer")) { | ||
| // stream is issuing the guarantee that it implements the | ||
| // API. Holds for all implementations in hadoop-* | ||
| // since Hadoop 3.3.0 (HDFS-14111). | ||
| return true; | ||
| } | ||
| } | ||
|
|
||
| @SuppressWarnings("unchecked") | ||
|
Member
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 don't understand why Parquet need to use reflection to look up a class defined by itself.
Contributor
Author
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 believe it's because of the transitive dependencies; |
||
| private static Class<SeekableInputStream> getH2SeekableClass() { | ||
| try { | ||
| return (Class<SeekableInputStream>) Class.forName( | ||
| "org.apache.parquet.hadoop.util.H2SeekableInputStream"); | ||
| } catch (ClassNotFoundException | NoClassDefFoundError e) { | ||
| return null; | ||
| InputStream wrapped = stream.getWrappedStream(); | ||
| if (wrapped instanceof FSDataInputStream) { | ||
| LOG.debug("Checking on wrapped stream {} of {} whether is ByteBufferReadable", wrapped, stream); | ||
| return isWrappedStreamByteBufferReadable(((FSDataInputStream) wrapped)); | ||
| } | ||
| return wrapped instanceof ByteBufferReadable; | ||
| } | ||
|
|
||
| private static Constructor<SeekableInputStream> getH2SeekableConstructor() { | ||
| Class<SeekableInputStream> h2SeekableClass = getH2SeekableClass(); | ||
| if (h2SeekableClass != null) { | ||
| try { | ||
| return h2SeekableClass.getConstructor(FSDataInputStream.class); | ||
| } catch (NoSuchMethodException e) { | ||
| return null; | ||
| } | ||
| } | ||
| return null; | ||
| } | ||
|
|
||
| /** | ||
| * Wraps a {@link FSDataOutputStream} in a {@link PositionOutputStream} | ||
|
|
||
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
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.
it appears this is a relatively new method added in https://issues.apache.org/jira/browse/HADOOP-15012 (Hadoop 2.10.0, 2.9.1, 3.1.0 and 3.0.1). Should we care about older provided Hadoop versions?
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.
if you are targeting the older hadoop releases, you'd also need to build java7 artifacts. does anyone want to do that?
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.
Personally I'm in favor of moving on and adopt the new APIs especially if we are going to depend on Hadoop 3 features more. Maybe we can call the next Parquet release 1.13.0 and declare that it's no longer compatible with older Hadoop versions?
cc @shangxinli
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.
that would be nice. do that and the library we are doing to help give 3.2+ apps access to the higher performance cloud storage APIs when available would be great.
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.
Let's be careful about introducing incompatibility & Hadoop is a fundamental dependency for Parquet.