Copilot commented on code in PR #11302: URL: https://github.com/apache/ozone/pull/11302#discussion_r4074532545
########## hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/BlockReadCursor.java: ########## @@ -0,0 +1,127 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hadoop.ozone.container.keyvalue; + +import java.io.IOException; +import java.util.List; +import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ChecksumType; +import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ChunkInfo; + +/** Tracks chunk-relative checksum boundaries for a streaming block read. */ +class BlockReadCursor { + private final List<ChunkInfo> chunks; + private final int responseDataSize; + private final long start; + private final long end; + private long offset; + private int chunkIndex; + + BlockReadCursor(long requestedOffset, long length, int responseSize, List<ChunkInfo> chunks) throws IOException { + this.chunks = chunks; + long blockEnd = 0; + int bufferSize = responseSize; + for (ChunkInfo chunk : chunks) { + if (chunk.getOffset() != blockEnd || chunk.getLen() <= 0 || chunk.getLen() > Long.MAX_VALUE - blockEnd) { + throw new IOException("Invalid chunk range: " + chunk); + } + blockEnd += chunk.getLen(); + // One checksum interval (or the short chunk containing it) must always fit in the buffer. + bufferSize = Math.max(bufferSize, (int) Math.min(chunk.getLen(), interval(chunk))); + } Review Comment: Buffer sizing can grow to the maximum checksum interval seen in chunk metadata, which may cause large per-request allocations (and potential OOM or GC pressure) if `bytesPerChecksum` is large or metadata is corrupted. Consider enforcing a reasonable upper bound for the computed `bufferSize` (or rejecting chunks with excessively large `bytesPerChecksum`) to protect datanode memory under unexpected inputs. ########## hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/KeyValueHandler.java: ########## @@ -2348,76 +2345,30 @@ private long readBlockImpl(ContainerCommandRequestProto request, RandomAccessFil "Requested offset " + readBlock.getOffset() + " is beyond the end of block " + blockID + " with size " + blockData.getSize())); } - final List<ContainerProtos.ChunkInfo> chunkInfos = blockData.getChunks(); - final ChecksumType checksumType = chunkInfos.get(0).getChecksumData().getType(); - int bytesPerChecksum = STREAMING_BYTES_PER_CHUNK; - if (checksumType != ContainerProtos.ChecksumType.NONE) { - bytesPerChecksum = chunkInfos.get(0).getChecksumData().getBytesPerChecksum(); + if (readBlock.getOffset() < 0 || readBlock.getLength() < 0 || responseDataSize < 0) { + return rejectReadBlock(blockFile, streamObserver, Status.INVALID_ARGUMENT.withDescription( + "Invalid ReadBlock range or response size: " + readBlock)); } - - // TODO: Support client-side flag to toggle checksum verification. - // If checksum is disabled, chunk offset adjustment can be skipped. - int chunkIndex = ReadBlockComputation.searchChunk(readBlock.getOffset(), chunkInfos); - ReadBlockComputation readBlockComputation = - new ReadBlockComputation(responseDataSize, bytesPerChecksum, chunkInfos, chunkIndex); - long adjustedOffset = readBlockComputation.computeAdjustedOffset(readBlock.getOffset()); - - long adjustLength = readBlockComputation.computeAdjustedLength( - readBlock.getOffset(), readBlock.getLength(), adjustedOffset); - - ChecksumData checksumData = new ChecksumData(checksumType, bytesPerChecksum); - final ByteBuffer buffer = ByteBuffer.allocate(responseDataSize); - blockFile.position(adjustedOffset); - long totalDataLength = 0; - int numResponses = 0; - Preconditions.checkState(adjustLength <= blockData.getSize() - adjustedOffset); - LOG.debug("adjustedOffset {}, requiredLength {}, blockSize {}", - adjustedOffset, adjustLength, blockData.getSize()); - for (boolean shouldRead = true; totalDataLength < adjustLength && shouldRead;) { - - int bufferLimit = readBlockComputation.computeBufferLimit(adjustedOffset, adjustLength - totalDataLength); - - buffer.limit(bufferLimit); - - shouldRead = blockFile.read(buffer); + final BlockReadCursor cursor = new BlockReadCursor(readBlock.getOffset(), readBlock.getLength(), + responseDataSize, blockData.getChunks()); + final ByteBuffer buffer = ByteBuffer.allocate(cursor.responseDataSize()); + blockFile.position(cursor.offset()); + while (cursor.hasRemaining()) { + buffer.clear().limit(cursor.nextReadLength()); + blockFile.read(buffer); + if (buffer.hasRemaining()) { + throw new EOFException("Unexpected end of block " + blockID + " at " + cursor.offset()); + } Review Comment: This treats any short read as EOF and fails the request, but file/channel reads are not guaranteed to fill the buffer in a single `read` call (short reads can happen without EOF). To avoid false EOFs, read in a loop until the buffer is filled or an actual EOF is detected (e.g., `read` returns < 0 / false with 0 bytes read). ########## hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/KeyValueHandler.java: ########## @@ -2348,76 +2345,30 @@ private long readBlockImpl(ContainerCommandRequestProto request, RandomAccessFil "Requested offset " + readBlock.getOffset() + " is beyond the end of block " + blockID + " with size " + blockData.getSize())); } - final List<ContainerProtos.ChunkInfo> chunkInfos = blockData.getChunks(); - final ChecksumType checksumType = chunkInfos.get(0).getChecksumData().getType(); - int bytesPerChecksum = STREAMING_BYTES_PER_CHUNK; - if (checksumType != ContainerProtos.ChecksumType.NONE) { - bytesPerChecksum = chunkInfos.get(0).getChecksumData().getBytesPerChecksum(); + if (readBlock.getOffset() < 0 || readBlock.getLength() < 0 || responseDataSize < 0) { + return rejectReadBlock(blockFile, streamObserver, Status.INVALID_ARGUMENT.withDescription( + "Invalid ReadBlock range or response size: " + readBlock)); } - - // TODO: Support client-side flag to toggle checksum verification. - // If checksum is disabled, chunk offset adjustment can be skipped. - int chunkIndex = ReadBlockComputation.searchChunk(readBlock.getOffset(), chunkInfos); - ReadBlockComputation readBlockComputation = - new ReadBlockComputation(responseDataSize, bytesPerChecksum, chunkInfos, chunkIndex); - long adjustedOffset = readBlockComputation.computeAdjustedOffset(readBlock.getOffset()); - - long adjustLength = readBlockComputation.computeAdjustedLength( - readBlock.getOffset(), readBlock.getLength(), adjustedOffset); - - ChecksumData checksumData = new ChecksumData(checksumType, bytesPerChecksum); - final ByteBuffer buffer = ByteBuffer.allocate(responseDataSize); - blockFile.position(adjustedOffset); - long totalDataLength = 0; - int numResponses = 0; - Preconditions.checkState(adjustLength <= blockData.getSize() - adjustedOffset); - LOG.debug("adjustedOffset {}, requiredLength {}, blockSize {}", - adjustedOffset, adjustLength, blockData.getSize()); - for (boolean shouldRead = true; totalDataLength < adjustLength && shouldRead;) { - - int bufferLimit = readBlockComputation.computeBufferLimit(adjustedOffset, adjustLength - totalDataLength); - - buffer.limit(bufferLimit); - - shouldRead = blockFile.read(buffer); + final BlockReadCursor cursor = new BlockReadCursor(readBlock.getOffset(), readBlock.getLength(), + responseDataSize, blockData.getChunks()); Review Comment: A `responseDataSize` of 0 isn't rejected here, but `BlockReadCursor` rejects `responseSize <= 0` by throwing an `IOException`, which is likely mapped to an IO failure rather than an INVALID_ARGUMENT. Consider tightening the check to `responseDataSize <= 0` so client input errors are consistently rejected as INVALID_ARGUMENT. -- 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]
