jojochuang commented on code in PR #7456:
URL: https://github.com/apache/ozone/pull/7456#discussion_r1915968414


##########
hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/storage/ShortCircuitChunkInputStream.java:
##########
@@ -0,0 +1,123 @@
+/*
+ * 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.hdds.scm.storage;
+
+import com.google.common.annotations.VisibleForTesting;
+import org.apache.hadoop.fs.ByteBufferReadable;
+import org.apache.hadoop.fs.CanUnbuffer;
+import org.apache.hadoop.fs.Seekable;
+import org.apache.hadoop.hdds.client.BlockID;
+import 
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ChunkInfo;
+import org.apache.hadoop.hdds.scm.XceiverClientFactory;
+import org.apache.hadoop.hdds.scm.XceiverClientShortCircuit;
+import org.apache.hadoop.hdds.scm.XceiverClientSpi.ShortCircuitValidator;
+import org.apache.hadoop.hdds.scm.pipeline.Pipeline;
+import org.apache.hadoop.ozone.common.Checksum;
+import org.apache.hadoop.ozone.common.ChecksumData;
+import org.apache.hadoop.ozone.common.OzoneChecksumException;
+import org.apache.hadoop.ozone.common.utils.BufferUtils;
+import org.apache.hadoop.security.token.Token;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.nio.ByteBuffer;
+import java.nio.channels.FileChannel;
+import java.util.Arrays;
+import java.util.List;
+import java.util.function.Supplier;
+
+/**
+ * An {@link InputStream} called from BlockInputStream to read a chunk from the
+ * container. Each chunk may contain multiple underlying {@link ByteBuffer}
+ * instances.
+ */
+public class ShortCircuitChunkInputStream extends ChunkInputStream
+    implements Seekable, CanUnbuffer, ByteBufferReadable {
+
+  private final ChunkInfo chunkInfo;
+  private final FileInputStream blockInputStream;
+  private final FileChannel dataIn;
+  private final ShortCircuitValidator validator;
+  private final XceiverClientShortCircuit xceiverClientShortCircuit;
+  private final boolean verifyChecksum;
+  public static final Logger LOG =
+      LoggerFactory.getLogger(ShortCircuitChunkInputStream.class);
+
+  @SuppressWarnings("checkstyle:parameternumber")
+  ShortCircuitChunkInputStream(ChunkInfo chunkInfo, BlockID blockId, 
XceiverClientFactory xceiverClientFactory,
+      Supplier<Pipeline> pipelineSupplier, boolean verifyChecksum, 
Supplier<Token<?>> tokenSupplier,
+      XceiverClientShortCircuit xceiverClientShortCircuit, FileInputStream 
blockInputStream) {
+    super(chunkInfo, blockId, xceiverClientFactory, pipelineSupplier, 
verifyChecksum, tokenSupplier);
+    this.chunkInfo = chunkInfo;
+    this.blockInputStream = blockInputStream;
+    this.dataIn = blockInputStream.getChannel();
+    this.xceiverClientShortCircuit = xceiverClientShortCircuit;
+    this.validator = this::validateChunk;
+    this.verifyChecksum = verifyChecksum;
+    if (LOG.isDebugEnabled()) {
+      LOG.debug("{} is created for {}", 
ShortCircuitChunkInputStream.class.getSimpleName(), blockId);
+    }
+  }
+
+  /**
+   * Send RPC call to get the chunk from the container.
+   */
+  @VisibleForTesting
+  @Override
+  protected ByteBuffer[] readChunk(ChunkInfo readChunkInfo)
+      throws IOException {
+    int bytesPerChecksum = chunkInfo.getChecksumData().getBytesPerChecksum();
+    final ByteBuffer[] buffers = 
BufferUtils.assignByteBuffers(readChunkInfo.getLen(),
+        bytesPerChecksum);
+    dataIn.position(readChunkInfo.getOffset()).read(buffers);
+    Arrays.stream(buffers).forEach(ByteBuffer::flip);
+    validator.accept(Arrays.asList(buffers), readChunkInfo);
+    return buffers;
+  }
+
+  private void validateChunk(List<ByteBuffer> bufferList, ChunkInfo 
readChunkInfo)

Review Comment:
   ShortCircuitChunkInputStream.validateChunk() is pretty close to part of 
ChunkInputStream.validateChunk(). Please consider refactor the code in the 
future.



-- 
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]

Reply via email to