szetszwo commented on code in PR #11724:
URL: https://github.com/apache/ozone/pull/11724#discussion_r4516662873
##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockInputStream.java:
##########
@@ -172,27 +158,28 @@ public synchronized void initialize() throws IOException {
}
} while (shouldRetryRead(catchEx, retryPolicy, ++retries));
- if (chunks == null) {
+ if (blockData == null) {
throw catchEx;
} else {
// Reset retry count if we get chunks successfully.
retries = 0;
}
Review Comment:
Let's also add an getBlockDataWithRetries() method
```java
checkOpen();
final BlockData blockData = getBlockDataWithRetries();
final long length = blockInfo.isUnderConstruction() ?
blockData.getSize() : blockInfo.getLength();
// For each chunk in the block, create a ChunkInputStream and compute
its chunkOffset
final List<ChunkInfo> chunks = blockData.getChunksList();
```
```java
private BlockData getBlockDataWithRetries() throws IOException {
for (;;) {
final IOException exception;
try {
final BlockData data = getBlockData();
retries = 0;
return data;
// If we get a StorageContainerException or an IOException due to
datanodes are unreachable,
// refresh pipeline and retry. Otherwise, retry according to the
retry policy.
} catch (SCMSecurityException ex) {
throw ex;
} catch (StorageContainerException e) {
refreshBlockInfo(e);
exception = e;
} catch (IOException e) {
LOG.debug("Retry to get chunk info fail", e);
if (isConnectivityIssue(e)) {
refreshBlockInfo(e);
}
exception = e;
}
if (!shouldRetryRead(exception, retryPolicy, ++retries)) {
throw exception;
}
}
}
```
##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockInputStream.java:
##########
@@ -172,27 +158,28 @@ public synchronized void initialize() throws IOException {
}
} while (shouldRetryRead(catchEx, retryPolicy, ++retries));
- if (chunks == null) {
+ if (blockData == null) {
throw catchEx;
} else {
// Reset retry count if we get chunks successfully.
retries = 0;
}
- if (!chunks.isEmpty()) {
- // For each chunk in the block, create a ChunkInputStream and compute
- // its chunkOffset
- this.chunkOffsets = new long[chunks.size()];
- long tempOffset = 0;
-
- this.chunkStreams = new ArrayList<>(chunks.size());
- for (int i = 0; i < chunks.size(); i++) {
- addStream(chunks.get(i));
- chunkOffsets[i] = tempOffset;
- tempOffset += chunks.get(i).getLen();
- }
+ // For each chunk in the block, create a ChunkInputStream and compute
+ // its chunkOffset
+ final List<ChunkInfo> chunks = blockData.getChunksList();
+ final long[] chunkOffsets = new long[chunks.size()];
+ long tempOffset = 0;
- initialized = true;
+ final List<ChunkInputStream> chunkStreams = new ArrayList<>(chunks.size());
+ for (int i = 0; i < chunks.size(); i++) {
+ chunkStreams.add(createChunkInputStream(chunks.get(i)));
+ chunkOffsets[i] = tempOffset;
+ tempOffset += chunks.get(i).getLen();
+ }
+ blockChunks = new BlockChunks(blockData, length, chunkOffsets,
chunkStreams);
Review Comment:
Let's add a newBlockChunks(..) method:
```java
private BlockChunks newBlockChunks(BlockData blockData, long length) {
final List<ChunkInfo> chunks = blockData.getChunksList();
final long[] chunkOffsets = new long[chunks.size()];
long tempOffset = 0;
// For each chunk, create a ChunkInputStream and compute its chunkOffset
final List<ChunkInputStream> chunkStreams = new
ArrayList<>(chunks.size());
for (int i = 0; i < chunks.size(); i++) {
chunkStreams.add(createChunkInputStream(chunks.get(i)));
chunkOffsets[i] = tempOffset;
tempOffset += chunks.get(i).getLen();
}
return new BlockChunks(blockData, length, chunkOffsets, chunkStreams);
}
```
##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockInputStream.java:
##########
@@ -520,15 +506,18 @@ private int readChunkAt(ChunkInputStream chunk, long
chunkOffset, int readLength
*/
@Override
public synchronized void seek(long pos) throws IOException {
- if (!initialized) {
+ if (!isInitialized()) {
// Stream has not been initialized yet. Save the position so that it
// can be seeked when the stream is initialized.
blockPosition = pos;
return;
}
checkOpen();
- if (pos < 0 || pos > length) {
+ final BlockChunks chunks = blockChunks;
+ final long[] chunkOffsets = chunks.offsets;
+ final List<ChunkInputStream> chunkStreams = chunks.streams;
+ if (pos < 0 || pos > chunks.length) {
Review Comment:
Let's add some methods to BlockChunks:
```java
chunkIndex = chunks.searchChunkIndex(chunkIndex, pos);
chunks.resetPosition(chunkIndex, chunkIndexOfPrevPosition, pos);
```
```java
//BlockChunks
void resetPosition(int chunkIndex, int chunkIndexOfPrevPosition, long
pos) throws IOException {
// Reset the previous chunkStream's position
streams.get(chunkIndexOfPrevPosition).resetPosition();
// Reset all the chunkStreams above the chunkIndex. We do this to reset
// any previous reads which might have updated the chunkPosition.
for (int index = chunkIndex + 1; index < streams.size(); index++) {
streams.get(index).seek(0);
}
// seek to the proper offset in the ChunkInputStream
streams.get(chunkIndex).seek(pos - offsets[chunkIndex]);
}
int searchChunkIndex(int chunkIndex, long pos) {
return searchStreamIndex(chunkIndex, pos, streams, offsets,
ChunkInputStream::getLength);
}
}
static <T extends Seekable> int searchStreamIndex(int index, long pos,
List<T> streams, long[] offsets,
ToLongFunction<T> getLength) {
if (index >= streams.size()) {
index = Arrays.binarySearch(offsets, pos);
} else if (pos < offsets[index]) {
index = Arrays.binarySearch(offsets, 0, index, pos);
} else if (pos >= offsets[index] +
getLength.applyAsLong(streams.get(index))) {
index = Arrays.binarySearch(offsets, index + 1, streams.size(), pos);
}
if (index < 0) {
// Binary search returns -insertionPoint - 1 if element is not
present in the array.
// Adjust the chunkIndex accordingly so that chunkIndex =
insertionPoint - 1
index = -index - 2;
}
return index;
}
```
##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockInputStream.java:
##########
@@ -683,11 +673,34 @@ private synchronized void handleReadError(IOException
cause) throws IOException
}
public synchronized List<ChunkInputStream> getChunkStreams() {
- return chunkStreams;
+ final BlockChunks chunks = blockChunks;
+ return chunks != null ? chunks.streams : null;
}
public BlockData getStreamBlockData() {
- return blockData;
+ final BlockChunks chunks = blockChunks;
+ return chunks != null ? chunks.blockData : null;
}
Review Comment:
getStreamBlockData() is called multiple times in
KeyValueHandler.reconcileChunksPerBlock(..) except for tests.
reconcileChunksPerBlock(..) also calls getChunkStreams(). We should refactor
the code to get BlockChunks only
We should get BlockChunks once and keep using it in
reconcileChunksPerBlock(..).
##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/StreamBlockInputStream.java:
##########
@@ -195,21 +199,19 @@ public int readPositioned(long blockOffset, ByteBuffer
dst) throws IOException {
if (!dst.hasRemaining()) {
return 0;
}
- updateBlockLength();
+ // updateBlockLength() sets blockLength before it clears the volatile
flag, so blockLength is safe to read here.
+ if (needUpdateBlockLength) {
+ updateBlockLength();
+ }
Review Comment:
- Just call updateBlockLength() as before -- The while
(needUpdateBlockLength) loop will take care the if needUpdateBlockLength) here.
- Then, we don't have to add volatile to needUpdateBlockLength.
##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockInputStream.java:
##########
@@ -468,7 +455,6 @@ public int readPositioned(long blockRelativePosition,
ByteBuffer dst) throws IOE
private int readChunkAt(ChunkInputStream chunk, long chunkOffset, int
readLength, ByteBuffer dst) throws IOException {
int preadRetries = 0;
while (true) {
- checkOpen();
Review Comment:
Why removing checkOpen()?
##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/StreamBlockInputStream.java:
##########
@@ -195,21 +199,19 @@ public int readPositioned(long blockOffset, ByteBuffer
dst) throws IOException {
if (!dst.hasRemaining()) {
return 0;
}
- updateBlockLength();
+ // updateBlockLength() sets blockLength before it clears the volatile
flag, so blockLength is safe to read here.
+ if (needUpdateBlockLength) {
+ updateBlockLength();
+ }
if (blockOffset < 0 || blockOffset >= blockLength) {
return EOF;
}
- final XceiverClientFactory factory;
- synchronized (this) {
- checkOpen();
Review Comment:
We probably need call checkOpen() somewhere.
##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/BlockInputStream.java:
##########
@@ -520,15 +506,18 @@ private int readChunkAt(ChunkInputStream chunk, long
chunkOffset, int readLength
*/
@Override
public synchronized void seek(long pos) throws IOException {
- if (!initialized) {
+ if (!isInitialized()) {
// Stream has not been initialized yet. Save the position so that it
// can be seeked when the stream is initialized.
blockPosition = pos;
return;
}
checkOpen();
- if (pos < 0 || pos > length) {
+ final BlockChunks chunks = blockChunks;
+ final long[] chunkOffsets = chunks.offsets;
+ final List<ChunkInputStream> chunkStreams = chunks.streams;
+ if (pos < 0 || pos > chunks.length) {
Review Comment:
MultipartInputStream can also use the new method
```diff
+++
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/MultipartInputStream.java
@@ -145,23 +145,7 @@ public synchronized void seek(long pos) throws
IOException {
}
// 1. Update the partIndex
- if (partIndex >= partStreams.size()) {
- partIndex = Arrays.binarySearch(partOffsets, pos);
- } else if (pos < partOffsets[partIndex]) {
- partIndex =
- Arrays.binarySearch(partOffsets, 0, partIndex, pos);
- } else if (pos >= partOffsets[partIndex] + partStreams
- .get(partIndex).getLength()) {
- partIndex = Arrays.binarySearch(partOffsets, partIndex + 1,
- partStreams.size(), pos);
- }
- if (partIndex < 0) {
- // Binary search returns -insertionPoint - 1 if element is not
present
- // in the array. insertionPoint is the point at which element would be
- // inserted in the sorted array. We need to adjust the blockIndex
- // accordingly so that partIndex = insertionPoint - 1
- partIndex = -partIndex - 2;
- }
+ partIndex = BlockInputStream.searchStreamIndex(partIndex, pos,
partStreams, partOffsets, PartInputStream::getLength);
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]