-
Notifications
You must be signed in to change notification settings - Fork 588
HDDS-4553. ChunkInputStream should release buffer as soon as last byte in the buffer is read #2062
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
Merged
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
b2305c7
HDDS-4553. ChunkInputStream should release buffer as soon as last byt…
hanishakoneru 42f29eb
HDDS-4552 followup
hanishakoneru 1c1babc
CI fixes
hanishakoneru 175b6ec
CI fixes 2
hanishakoneru 2a06dfe
Review comments
hanishakoneru 9964ae9
Minor NIT in javadoc
hanishakoneru 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -21,7 +21,6 @@ | |
| import com.google.common.annotations.VisibleForTesting; | ||
| import com.google.common.base.Preconditions; | ||
| import java.util.ArrayList; | ||
| import java.util.stream.Collectors; | ||
| import org.apache.commons.lang3.tuple.Pair; | ||
| import org.apache.hadoop.fs.CanUnbuffer; | ||
| import org.apache.hadoop.fs.Seekable; | ||
|
|
@@ -71,8 +70,8 @@ public class ChunkInputStream extends InputStream | |
| private final Supplier<Pipeline> pipelineSupplier; | ||
| private boolean verifyChecksum; | ||
| private boolean allocated = false; | ||
| // Buffer to store the chunk data read from the DN container | ||
| private List<ByteBuffer> buffers; | ||
| // Buffers to store the chunk data read from the DN container | ||
| private ByteBuffer[] buffers; | ||
|
|
||
| // Index of the buffers corresponding to the current position of the buffers | ||
| private int bufferIndex; | ||
|
|
@@ -89,6 +88,9 @@ public class ChunkInputStream extends InputStream | |
| // of chunk data | ||
| private long bufferOffsetWrtChunkData; | ||
|
|
||
| // Index of the first buffer which has not been released | ||
| private int minBufferIndex = 0; | ||
|
|
||
| // The number of bytes of chunk data residing in the buffers currently | ||
| private long buffersSize; | ||
|
|
||
|
|
@@ -133,13 +135,11 @@ public synchronized int read() throws IOException { | |
| // been released by now | ||
| Preconditions.checkState(buffers == null); | ||
| } else { | ||
| dataout = Byte.toUnsignedInt(buffers.get(bufferIndex).get()); | ||
| dataout = Byte.toUnsignedInt(buffers[bufferIndex].get()); | ||
| } | ||
|
|
||
| if (chunkStreamEOF()) { | ||
| // consumer might use getPos to determine EOF, | ||
| // so release buffers when serving the last byte of data | ||
| releaseBuffers(); | ||
| if (bufferEOF()) { | ||
| releaseBuffers(bufferIndex); | ||
| } | ||
|
|
||
| return dataout; | ||
|
|
@@ -179,15 +179,13 @@ public synchronized int read(byte[] b, int off, int len) throws IOException { | |
| Preconditions.checkState(buffers == null); | ||
| return total != 0 ? total : EOF; | ||
| } | ||
| buffers.get(bufferIndex).get(b, off + total, available); | ||
| buffers[bufferIndex].get(b, off + total, available); | ||
| len -= available; | ||
| total += available; | ||
| } | ||
|
|
||
| if (chunkStreamEOF()) { | ||
| // smart consumers determine EOF by calling getPos() | ||
| // so we release buffers when serving the final bytes of data | ||
| releaseBuffers(); | ||
| if (bufferEOF()) { | ||
| releaseBuffers(bufferIndex); | ||
| } | ||
| } | ||
|
|
||
| return total; | ||
|
|
@@ -233,7 +231,7 @@ public synchronized long getPos() { | |
| // BufferOffset w.r.t to ChunkData + BufferOffset w.r.t buffers + | ||
| // Position of current Buffer | ||
| return bufferOffsetWrtChunkData + bufferOffsets[bufferIndex] + | ||
| buffers.get(bufferIndex).position(); | ||
| buffers[bufferIndex].position(); | ||
| } | ||
| if (buffersAllocated()) { | ||
| return bufferOffsetWrtChunkData + buffersSize; | ||
|
|
@@ -289,7 +287,7 @@ private synchronized int prepareRead(int len) throws IOException { | |
| } | ||
| if (buffersHaveData()) { | ||
| // Data is available from buffers | ||
| ByteBuffer bb = buffers.get(bufferIndex); | ||
| ByteBuffer bb = buffers[bufferIndex]; | ||
| return len > bb.remaining() ? bb.remaining() : len; | ||
| } else if (dataRemainingInChunk()) { | ||
| // There is more data in the chunk stream which has not | ||
|
|
@@ -370,22 +368,23 @@ private void readChunkDataIntoBuffers(ChunkInfo readChunkInfo) | |
| buffers = readChunk(readChunkInfo); | ||
| buffersSize = readChunkInfo.getLen(); | ||
|
|
||
| bufferOffsets = new long[buffers.size()]; | ||
| bufferOffsets = new long[buffers.length]; | ||
| int tempOffset = 0; | ||
| for (int i = 0; i < buffers.size(); i++) { | ||
| for (int i = 0; i < buffers.length; i++) { | ||
| bufferOffsets[i] = tempOffset; | ||
| tempOffset += buffers.get(i).limit(); | ||
| tempOffset += buffers[i].limit(); | ||
| } | ||
|
|
||
| bufferIndex = 0; | ||
| minBufferIndex = 0; | ||
| allocated = true; | ||
| } | ||
|
|
||
| /** | ||
| * Send RPC call to get the chunk from the container. | ||
| */ | ||
| @VisibleForTesting | ||
| protected List<ByteBuffer> readChunk(ChunkInfo readChunkInfo) | ||
| protected ByteBuffer[] readChunk(ChunkInfo readChunkInfo) | ||
| throws IOException { | ||
| ReadChunkResponseProto readChunkResponse; | ||
|
|
||
|
|
@@ -405,13 +404,12 @@ protected List<ByteBuffer> readChunk(ChunkInfo readChunkInfo) | |
| } | ||
|
|
||
| if (readChunkResponse.hasData()) { | ||
| return readChunkResponse.getData().asReadOnlyByteBufferList(); | ||
| return readChunkResponse.getData().asReadOnlyByteBufferList() | ||
| .toArray(new ByteBuffer[0]); | ||
| } else if (readChunkResponse.hasDataBuffers()) { | ||
| List<ByteString> buffersList = readChunkResponse.getDataBuffers() | ||
| .getBuffersList(); | ||
| return buffersList.stream() | ||
| .map(ByteString::asReadOnlyByteBuffer) | ||
| .collect(Collectors.toList()); | ||
| return BufferUtils.getReadOnlyByteBuffersArray(buffersList); | ||
| } else { | ||
| throw new IOException("Unexpected error while reading chunk data " + | ||
| "from container. No data returned."); | ||
|
|
@@ -508,21 +506,21 @@ private Pair<Long, Long> computeChecksumBoundaries(long startByteIndex, | |
| private void adjustBufferPosition(long bufferPosition) { | ||
| // The bufferPosition is w.r.t the current buffers. | ||
| // Adjust the bufferIndex and position to the seeked bufferPosition. | ||
| if (bufferIndex >= buffers.size()) { | ||
| if (bufferIndex >= buffers.length) { | ||
| bufferIndex = Arrays.binarySearch(bufferOffsets, bufferPosition); | ||
| } else if (bufferPosition < bufferOffsets[bufferIndex]) { | ||
| bufferIndex = Arrays.binarySearch(bufferOffsets, 0, bufferIndex, | ||
| bufferPosition); | ||
| } else if (bufferPosition >= bufferOffsets[bufferIndex] + | ||
| buffers.get(bufferIndex).capacity()) { | ||
| buffers[bufferIndex].capacity()) { | ||
| bufferIndex = Arrays.binarySearch(bufferOffsets, bufferIndex + 1, | ||
| buffers.size(), bufferPosition); | ||
| buffers.length, bufferPosition); | ||
| } | ||
| if (bufferIndex < 0) { | ||
| bufferIndex = -bufferIndex - 2; | ||
| } | ||
|
|
||
| buffers.get(bufferIndex).position( | ||
| buffers[bufferIndex].position( | ||
| (int) (bufferPosition - bufferOffsets[bufferIndex])); | ||
|
|
||
| // Reset buffers > bufferIndex to position 0. We do this to reset any | ||
|
|
@@ -531,8 +529,8 @@ private void adjustBufferPosition(long bufferPosition) { | |
| // not required for this read. If a seek was done to a position in the | ||
| // previous indices, the buffer position reset would be performed in the | ||
| // seek call. | ||
| for (int i = bufferIndex + 1; i < buffers.size(); i++) { | ||
| buffers.get(i).position(0); | ||
| for (int i = bufferIndex + 1; i < buffers.length; i++) { | ||
| buffers[i].position(0); | ||
| } | ||
|
|
||
| // Reset the chunkPosition as chunk stream has been initialized i.e. the | ||
|
|
@@ -545,7 +543,7 @@ private void adjustBufferPosition(long bufferPosition) { | |
| */ | ||
| @VisibleForTesting | ||
| protected boolean buffersAllocated() { | ||
| return buffers != null && !buffers.isEmpty(); | ||
| return buffers != null && buffers.length > 0; | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -556,16 +554,17 @@ private boolean buffersHaveData() { | |
| boolean hasData = false; | ||
|
|
||
| if (buffersAllocated()) { | ||
| while (bufferIndex < (buffers.size())) { | ||
| if (buffers.get(bufferIndex).hasRemaining()) { | ||
| while (bufferIndex < (buffers.length)) { | ||
| if (buffers[bufferIndex] != null && | ||
| buffers[bufferIndex].hasRemaining()) { | ||
| // current buffer has data | ||
| hasData = true; | ||
| break; | ||
| } else { | ||
| if (buffersRemaining()) { | ||
| // move to next available buffer | ||
| ++bufferIndex; | ||
| Preconditions.checkState(bufferIndex < buffers.size()); | ||
| Preconditions.checkState(bufferIndex < buffers.length); | ||
| } else { | ||
| // no more buffers remaining | ||
| break; | ||
|
|
@@ -578,7 +577,7 @@ private boolean buffersHaveData() { | |
| } | ||
|
|
||
| private boolean buffersRemaining() { | ||
| return (bufferIndex < (buffers.size() - 1)); | ||
| return (bufferIndex < (buffers.length - 1)); | ||
| } | ||
|
|
||
| /** | ||
|
|
@@ -588,7 +587,9 @@ private boolean buffersHavePosition(long pos) { | |
| // Check if buffers have been allocated | ||
| if (buffersAllocated()) { | ||
| // Check if the current buffers cover the input position | ||
| return pos >= bufferOffsetWrtChunkData && | ||
| // Released buffers should not be considered when checking if position | ||
| // is available | ||
| return pos >= bufferOffsetWrtChunkData + bufferOffsets[minBufferIndex] && | ||
| pos < bufferOffsetWrtChunkData + buffersSize; | ||
| } | ||
| return false; | ||
|
|
@@ -609,6 +610,21 @@ private boolean dataRemainingInChunk() { | |
| return bufferPos < length; | ||
| } | ||
|
|
||
| /** | ||
| * Check if current buffer had been read till the end. | ||
| */ | ||
| private boolean bufferEOF() { | ||
| if (!allocated) { | ||
| // Chunk data has not been read yet | ||
| return false; | ||
| } | ||
|
|
||
| if (!buffers[bufferIndex].hasRemaining()) { | ||
| return true; | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| /** | ||
| * Check if end of chunkStream has been reached. | ||
| */ | ||
|
|
@@ -628,12 +644,40 @@ private boolean chunkStreamEOF() { | |
| } | ||
| } | ||
|
|
||
|
|
||
| /** | ||
| * Release a buffer. | ||
|
||
| * A buffer should be released after buffer EOF is reached i.e. the last | ||
| * byte in the buffer has been read. | ||
| * @param releaseUptoBufferIndex bufferIndex (inclusive) upto which the | ||
| * buffers must be released | ||
| */ | ||
| private void releaseBuffers(int releaseUptoBufferIndex) { | ||
| if (releaseUptoBufferIndex == buffers.length - 1) { | ||
| // Before releasing all the buffers, if chunk EOF is not reached, then | ||
| // chunkPosition should be set to point to the last position of the | ||
| // buffers. This should be done so that getPos() can return the current | ||
| // chunk position | ||
| chunkPosition = bufferOffsetWrtChunkData + | ||
| bufferOffsets[releaseUptoBufferIndex] + | ||
| buffers[releaseUptoBufferIndex].capacity(); | ||
| // Release all the buffers | ||
| releaseBuffers(); | ||
| } else { | ||
| for (int i = 0; i <= releaseUptoBufferIndex; i++) { | ||
| buffers[i] = null; | ||
| } | ||
| minBufferIndex = releaseUptoBufferIndex + 1; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * If EOF is reached, release the buffers. | ||
| */ | ||
| private void releaseBuffers() { | ||
| buffers = null; | ||
| bufferIndex = 0; | ||
| minBufferIndex = 0; | ||
| // We should not reset bufferOffsetWrtChunkData and buffersSize here | ||
| // because when getPos() is called in chunkStreamEOF() we use these | ||
| // values and determine whether chunk is read completely or not. | ||
|
|
@@ -671,7 +715,7 @@ public synchronized void unbuffer() { | |
| } | ||
|
|
||
| @VisibleForTesting | ||
| public List<ByteBuffer> getCachedBuffers() { | ||
| return buffers; | ||
| public ByteBuffer[] getCachedBuffers() { | ||
| return BufferUtils.getReadOnlyByteBuffers(buffers); | ||
| } | ||
| } | ||
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
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
Oops, something went wrong.
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.
I am little bit confused with this. Can we think better name? unreleasedFirstBuffIdx or so ?