Package org.apache.hadoop.fs.s3a
Class S3AInputStream
java.lang.Object
java.io.InputStream
org.apache.hadoop.fs.FSInputStream
org.apache.hadoop.fs.s3a.impl.streams.ObjectInputStream
org.apache.hadoop.fs.s3a.S3AInputStream
- All Implemented Interfaces:
Closeable,AutoCloseable,org.apache.hadoop.fs.CanSetReadahead,org.apache.hadoop.fs.CanUnbuffer,org.apache.hadoop.fs.PositionedReadable,org.apache.hadoop.fs.Seekable,org.apache.hadoop.fs.statistics.IOStatisticsSource,org.apache.hadoop.fs.StreamCapabilities
@Private
@Evolving
public class S3AInputStream
extends ObjectInputStream
implements org.apache.hadoop.fs.CanSetReadahead, org.apache.hadoop.fs.CanUnbuffer, org.apache.hadoop.fs.StreamCapabilities, org.apache.hadoop.fs.statistics.IOStatisticsSource
The input stream for an S3A object.
As this stream seeks withing an object, it may close then re-open the stream.
When this happens, any updated stream data may be retrieved, and, given
the consistency model of Amazon S3, outdated data may in fact be picked up.
As a result, the outcome of reading from a stream of an object which is
actively manipulated during the read process is "undefined".
The class is marked as private as code should not be creating instances
themselves. Any extra feature (e.g instrumentation) should be considered
unstable.
Because it prints some of the state of the instrumentation,
the output of
toString() must also be considered unstable.-
Nested Class Summary
Nested classes/interfaces inherited from interface org.apache.hadoop.fs.StreamCapabilities
org.apache.hadoop.fs.StreamCapabilities.StreamCapability -
Field Summary
FieldsFields inherited from interface org.apache.hadoop.fs.StreamCapabilities
ABORTABLE_STREAM, DROPBEHIND, HFLUSH, HSYNC, IOSTATISTICS, IOSTATISTICS_CONTEXT, PREADBYTEBUFFER, READAHEAD, READBYTEBUFFER, UNBUFFER, VECTOREDIO, VECTOREDIO_BUFFERS_SLICED -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionprotected voidBrute force stream close; invoked byLeakReporter.intvoidclose()Close the stream.longlonglonggetPos()longGet the current readahead value.software.amazon.awssdk.core.ResponseInputStream<software.amazon.awssdk.services.s3.model.GetObjectResponse>Get the wrapped stream.booleanhasCapability(String capability) Declare the base capabilities implemented by this class and so by all subclasses.booleanIs the inner object stream open?protected booleanProbe for stream being open.booleanintread()intread(byte[] buf, int off, int len) This updates the statistics on read operations started and whether or not the read operation "completed", that is: returned the exact number of bytes requested.voidreadFully(long position, byte[] buffer, int offset, int length) SubclassreadFully()operation which only seeks at the start of the series of operations; seeking back at the end.voidreadVectored(List<? extends org.apache.hadoop.fs.FileRange> ranges, IntFunction<ByteBuffer> allocate) Pass toreadVectored(List, IntFunction, Consumer)with theVectoredReadUtils.LOG_BYTE_BUFFER_RELEASEDreleaser.voidreadVectored(List<? extends org.apache.hadoop.fs.FileRange> ranges, IntFunction<ByteBuffer> allocate, Consumer<ByteBuffer> release) Vectored read implementation for S3AInputStream.longBytes left in the current request.longBytes left in stream.booleanForcibly reset the stream, by aborting the connection.voidseek(long targetPos) booleanseekToNewSource(long targetPos) voidsetReadahead(Long readahead) toString()String value includes statistics as well as stream state.voidunbuffer()Closes the underlying S3 stream, and merges theObjectInputStream.streamStatisticsinstance associated with the stream.static longvalidateReadahead(Long readahead) from a possibly null Long value, return a valid readahead.Methods inherited from class org.apache.hadoop.fs.s3a.impl.streams.ObjectInputStream
finalize, getBoundedThreadPool, getBucket, getCallbacks, getContentLength, getContext, getInputPolicy, getIOStatistics, getKey, getObjectAttributes, getPathStr, getS3AStreamStatistics, getThreadIOStatistics, getUri, getVectoredIOContext, maxReadSizeForVectorReads, mergeThreadIOStatistics, minSeekForVectorReads, setInputPolicy, streamTypeMethods inherited from class org.apache.hadoop.fs.FSInputStream
read, readFully, validatePositionedReadArgsMethods inherited from class java.io.InputStream
mark, nullInputStream, read, readAllBytes, readNBytes, readNBytes, reset, skip, skipNBytes, transferToMethods inherited from class java.lang.Object
clone, equals, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface org.apache.hadoop.fs.statistics.IOStatisticsSource
getIOStatistics
-
Field Details
-
E_NEGATIVE_READAHEAD_VALUE
- See Also:
-
OPERATION_OPEN
- See Also:
-
OPERATION_REOPEN
- See Also:
-
-
Constructor Details
-
S3AInputStream
Create the stream. This does not attempt to open it; that is only done on the first actual read() operation.- Parameters:
parameters- creation parameters.
-
-
Method Details
-
isStreamOpen
protected boolean isStreamOpen()Probe for stream being open. Not synchronized; the flag is volatile.- Specified by:
isStreamOpenin classObjectInputStream- Returns:
- true if the stream is still open.
-
abortInFinalizer
protected void abortInFinalizer()Brute force stream close; invoked byLeakReporter. All exceptions raised are ignored.- Specified by:
abortInFinalizerin classObjectInputStream
-
getPos
- Specified by:
getPosin interfaceorg.apache.hadoop.fs.Seekable- Specified by:
getPosin classorg.apache.hadoop.fs.FSInputStream- Throws:
IOException
-
seek
- Specified by:
seekin interfaceorg.apache.hadoop.fs.Seekable- Specified by:
seekin classorg.apache.hadoop.fs.FSInputStream- Throws:
IOException
-
seekToNewSource
- Specified by:
seekToNewSourcein interfaceorg.apache.hadoop.fs.Seekable- Specified by:
seekToNewSourcein classorg.apache.hadoop.fs.FSInputStream- Throws:
IOException
-
read
- Specified by:
readin classInputStream- Throws:
IOException
-
read
This updates the statistics on read operations started and whether or not the read operation "completed", that is: returned the exact number of bytes requested.- Overrides:
readin classInputStream- Throws:
IOException- if there are other problems
-
close
Close the stream. This triggers publishing of the stream statistics back to the filesystem statistics. This operation is synchronized, so that only one thread can attempt to close the connection; all later/blocked calls are no-ops.- Specified by:
closein interfaceAutoCloseable- Specified by:
closein interfaceCloseable- Overrides:
closein classObjectInputStream- Throws:
IOException- on any problem
-
resetConnection
Forcibly reset the stream, by aborting the connection. The nextread()operation will trigger the opening of a new HTTPS connection. This is potentially very inefficient, and should only be invoked in extreme circumstances. It logs at info for this reason. Blocks until the abort is completed.- Returns:
- true if the connection was actually reset.
- Throws:
IOException- if invoked on a closed stream.
-
available
- Overrides:
availablein classInputStream- Throws:
IOException
-
remainingInFile
@Private @Unstable public long remainingInFile()Bytes left in stream.- Returns:
- how many bytes are left to read
-
remainingInCurrentRequest
@Private @Unstable public long remainingInCurrentRequest()Bytes left in the current request. Only valid if there is an active request.- Returns:
- how many bytes are left to read in the current GET.
-
getContentRangeFinish
@Private @Unstable public long getContentRangeFinish() -
getContentRangeStart
@Private @Unstable public long getContentRangeStart() -
markSupported
public boolean markSupported()- Overrides:
markSupportedin classInputStream
-
toString
String value includes statistics as well as stream state. Important: there are no guarantees as to the stability of this value.- Overrides:
toStringin classObjectInputStream- Returns:
- a string value for printing in logs/diagnostics
-
readFully
@RetryTranslated public void readFully(long position, byte[] buffer, int offset, int length) throws IOException SubclassreadFully()operation which only seeks at the start of the series of operations; seeking back at the end. This is significantly higher performance if multiple read attempts are needed to fetch the data, as it does not break the HTTP connection. To maintain thread safety requirements, this operation is synchronized for the duration of the sequence.- Specified by:
readFullyin interfaceorg.apache.hadoop.fs.PositionedReadable- Overrides:
readFullyin classorg.apache.hadoop.fs.FSInputStream- Throws:
IOException
-
readVectored
public void readVectored(List<? extends org.apache.hadoop.fs.FileRange> ranges, IntFunction<ByteBuffer> allocate) throws IOException Pass toreadVectored(List, IntFunction, Consumer)with theVectoredReadUtils.LOG_BYTE_BUFFER_RELEASEDreleaser.- Specified by:
readVectoredin interfaceorg.apache.hadoop.fs.PositionedReadable- Parameters:
ranges- the byte ranges to read.allocate- the function to allocate ByteBuffer.- Throws:
IOException- IOE if any.
-
readVectored
public void readVectored(List<? extends org.apache.hadoop.fs.FileRange> ranges, IntFunction<ByteBuffer> allocate, Consumer<ByteBuffer> release) throws IOException Vectored read implementation for S3AInputStream.- Specified by:
readVectoredin interfaceorg.apache.hadoop.fs.PositionedReadable- Parameters:
ranges- the byte ranges to read.allocate- the function to allocate ByteBuffer.release- the function to release a ByteBuffer.- Throws:
IOException- IOE if any.
-
setReadahead
- Specified by:
setReadaheadin interfaceorg.apache.hadoop.fs.CanSetReadahead
-
getReadahead
public long getReadahead()Get the current readahead value.- Returns:
- a non-negative readahead value
-
validateReadahead
from a possibly null Long value, return a valid readahead.- Parameters:
readahead- new readahead- Returns:
- a natural number.
- Throws:
IllegalArgumentException- if the range is invalid.
-
unbuffer
public void unbuffer()Closes the underlying S3 stream, and merges theObjectInputStream.streamStatisticsinstance associated with the stream. Also sets thestopVectoredIOOperationsflag to true such that active vectored read operations are terminated. However termination of old vectored reads are not guaranteed if a new vectored read operation is initiated after unbuffer is called.- Specified by:
unbufferin interfaceorg.apache.hadoop.fs.CanUnbuffer
-
hasCapability
Description copied from class:ObjectInputStreamDeclare the base capabilities implemented by this class and so by all subclasses.Subclasses MUST override this if they add more capabilities, or actually remove any of these.
- Specified by:
hasCapabilityin interfaceorg.apache.hadoop.fs.StreamCapabilities- Overrides:
hasCapabilityin classObjectInputStream- Parameters:
capability- string to query the stream support for.- Returns:
- true if all implementations are known to have the specific capability.
-
isObjectStreamOpen
@VisibleForTesting public boolean isObjectStreamOpen()Is the inner object stream open?- Returns:
- true if there is an active HTTP request to S3.
-
getWrappedStream
@VisibleForTesting public software.amazon.awssdk.core.ResponseInputStream<software.amazon.awssdk.services.s3.model.GetObjectResponse> getWrappedStream()Get the wrapped stream. This is for testing only.- Returns:
- the wrapped stream, or null if there is none.
-