szetszwo commented on code in PR #10764:
URL: https://github.com/apache/ozone/pull/10764#discussion_r3919801309
##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/KeyValueHandler.java:
##########
@@ -2445,6 +2462,64 @@ static List<ByteString> getChecksums(long blockOffset,
int readLength, int bytes
return checksums;
}
+ /**
+ * We have to align the read to checksum boundaries, so whatever offset is
requested, we have to move back to the
+ * previous checksum boundary.
+ * eg if bytesPerChecksum is 512, and the requested offset is 600, we have
to move back to 512.
+ * Returns the checksum boundaries of {@code ChecksumBoundaries} relative to
blockOffset and blockLength.
+ */
+ private static ChecksumBoundaries getChecksumBoundaries(long blockOffset,
long blockLength,
+ List<ContainerProtos.ChunkInfo> chunkInfos, long bytesPerChecksum) {
+ final int offsetChunkIndex = searchChunkByOffset(blockOffset, chunkInfos);
+ final long offsetAlignment = (blockOffset -
chunkInfos.get(offsetChunkIndex).getOffset()) % bytesPerChecksum;
+ final long adjustedOffset = blockOffset - offsetAlignment;
+ final long blockEnd = blockOffset + blockLength - 1;
+ final ContainerProtos.ChunkInfo lastChunk =
chunkInfos.get(searchChunkByOffset(blockEnd, chunkInfos));
+
+ final long chunkOffset = lastChunk.getOffset();
+ final long chunkLength = Math.min(
+ (getEndChecksumIndex(blockEnd, chunkOffset, bytesPerChecksum) + 1) *
bytesPerChecksum, lastChunk.getLen());
+ return new ChecksumBoundaries(offsetChunkIndex, adjustedOffset,
+ chunkOffset + chunkLength - adjustedOffset);
+ }
+
+ private static int getEndChecksumIndex(long blockEnd, long chunkOffset, long
bytesPerChecksum) {
+ return (int) ((blockEnd - chunkOffset) / bytesPerChecksum);
+ }
+
+ private static final class ChecksumBoundaries {
Review Comment:
This class is not really useful since it is only used once. We may inline
the methods:
```java
//readBlockImpl
// Align the read to checksum boundaries.
final int firstChunkIndex = searchChunk(readOffset, chunkInfos);
final int lastChunkIndex = searchChunk(readEnd - 1, chunkInfos);
final long adjustedOffset = adjustOffset(readOffset, bytesPerChecksum,
chunkInfos.get(firstChunkIndex).getOffset());
final long adjustedEnd = adjustEnd(readEnd, bytesPerChecksum,
chunkInfos.get(lastChunkIndex).getOffset());
final long adjustedLength = adjustedEnd - adjustedOffset;
```
```java
/**
* Adjust readOffset to algin with chunkOffset and bytesPerChecksum such
that
* (1) adjustedReadOffset <= readOffset, and
* (2) (adjustedReadOffset - chunkOffset) is a multiple of
bytesPerChecksum.
*
* @param readOffset inclusive
* @param chunkOffset inclusive
* @return the adjusted read offset (inclusive)
*/
static long adjustOffset(long readOffset, int bytesPerChecksum, long
chunkOffset) {
Preconditions.checkState(chunkOffset <= readOffset);
final long alignment = (readOffset - chunkOffset) % bytesPerChecksum;
return readOffset - alignment;
}
/**
* Adjust readEnd to algin with chunkOffset and bytesPerChecksum such that
* (1) adjustedReadEnd >= readEnd, and
* (2) (adjustedReadEnd - chunkOffset) is a multiple of bytesPerChecksum.
*
* @param readEnd exclusive
* @param chunkOffset inclusive
* @return the adjusted read end (exclusive)
*/
static long adjustEnd(long readEnd, int bytesPerChecksum, long
chunkOffset) {
Preconditions.checkState(chunkOffset < readEnd);
final long remainder = (readEnd - chunkOffset) % bytesPerChecksum;
final long alignment = remainder == 0 ? 0 : bytesPerChecksum - remainder;
return readEnd + alignment;
}
```
--
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]