szetszwo commented on code in PR #11873:
URL: https://github.com/apache/ozone/pull/11873#discussion_r4556156582
##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/io/RandomAccessFileChannel.java:
##########
@@ -25,17 +25,25 @@
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
import java.util.Objects;
+import org.apache.hadoop.hdds.client.BlockID;
+import org.apache.hadoop.ozone.container.common.helpers.BlockData;
import org.apache.ratis.util.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
-/** {@link RandomAccessFile} and its {@link FileChannel}. */
+/**
+ * {@link RandomAccessFile} and its {@link FileChannel}. A ReadBlock stream
serves one block through one instance,
+ * which therefore also caches the block's {@link BlockData} and the read
buffer across the stream's requests.
+ */
public class RandomAccessFileChannel implements Closeable {
private static final Logger LOG =
LoggerFactory.getLogger(RandomAccessFileChannel.class);
private File blockFile;
private RandomAccessFile raf;
private FileChannel channel;
+ private BlockID cachedBlockID;
+ private BlockData cachedBlockData;
Review Comment:
- cachedBlockID is not needed since cachedBlockData has blockId
- Use AtomicReference to avoid synchronized.
```
private final AtomicReference<BlockData> cachedBlockData = new
AtomicReference<>();
```
##########
hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/utils/io/RandomAccessFileChannel.java:
##########
@@ -56,6 +64,27 @@ public synchronized void open(File file) throws
FileNotFoundException {
channel = newChannel;
}
+ /** @return the {@link BlockData} cached for the given block, or null. */
+ public synchronized BlockData getCachedBlockData(BlockID blockID) {
+ return blockID.equals(cachedBlockID) ? cachedBlockData : null;
+ }
+
+ /** Cache the {@link BlockData} of the block this channel serves until
{@link #close()}. */
+ public synchronized void cacheBlockData(BlockID blockID, BlockData
blockData) {
+ cachedBlockID = blockID;
+ cachedBlockData = blockData;
+ }
+
+ /** @return a cleared read buffer of at least the given capacity,
reallocated only when it has to grow. */
+ public synchronized ByteBuffer getReadBuffer(int capacity) {
Review Comment:
synchronized is not needed -- it must be synchronized externally; otherwise,
the buffer is shared between two threads.
##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/KeyValueHandler.java:
##########
@@ -2344,20 +2346,33 @@ private long readBlockImpl(ContainerCommandRequestProto
request, RandomAccessFil
BlockUtils.verifyReplicaIdx(kvContainer, blockID);
BlockUtils.verifyBCSId(kvContainer, blockID);
- final BlockData blockData = getBlockManager().getBlock(kvContainer,
blockID);
+ if (readBlock.getOffset() < 0 || readBlock.getLength() < 0) {
+ return rejectReadBlock(blockFile, streamObserver,
Status.INVALID_ARGUMENT.withDescription(
+ "Negative offset " + readBlock.getOffset() + " or length " +
readBlock.getLength() + " for " + blockID));
+ }
+
+ // Cached per stream; re-resolve when the range reaches the cached size,
since the block may have grown.
+ BlockData blockData = blockFile.getCachedBlockData(blockID);
+ if (blockData == null || readBlock.getLength() >= blockData.getSize() -
readBlock.getOffset()) {
+ blockData = getBlockManager().getBlock(kvContainer, blockID);
+ blockFile.cacheBlockData(blockID, blockData);
+ } else if (!getBlockManager().blockExists(kvContainer, blockID)) {
Review Comment:
blockExists check is not needed since the data was from
getBlockManager().getBlock(..);
--
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]