chungen0126 commented on code in PR #6613:
URL: https://github.com/apache/ozone/pull/6613#discussion_r1792313617


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/keyvalue/KeyValueHandler.java:
##########
@@ -1329,6 +1333,126 @@ public void deleteUnreferenced(Container container, 
long localID)
     }
   }
 
+  @Override
+  public ContainerCommandResponseProto streamDataReadOnly(
+      ContainerCommandRequestProto request, KeyValueContainer kvContainer,
+      DispatcherContext dispatcherContext,
+      StreamObserver<ContainerCommandResponseProto> streamObserver) {
+    ContainerCommandResponseProto responseProto = null;
+    try {
+      if (!request.hasReadBlock()) {
+        if (LOG.isDebugEnabled()) {
+          LOG.debug("Malformed Read Block request. trace ID: {}",
+              request.getTraceID());
+        }
+        return malformedRequest(request);
+      }
+      ReadBlockRequestProto readBlock = request.getReadBlock();
+      ChunkBuffer data;
+
+      BlockID blockID = BlockID.getFromProtobuf(
+          readBlock.getBlockID());
+      if (replicaIndexCheckRequired(request)) {
+        BlockUtils.verifyReplicaIdx(kvContainer, blockID);
+      }
+      BlockData blockData = blockManager.getBlock(kvContainer, blockID);
+      List<ContainerProtos.ChunkInfo> chunkInfos = blockData.getChunks();
+      long blockOffset = 0;
+      int chunkIndex = -1;
+      for (int i = 0; i < chunkInfos.size(); i++) {
+        blockOffset += chunkInfos.get(i).getLen();
+        if (blockOffset > readBlock.getOffset()) {
+          chunkIndex = i;
+          break;
+        }
+      }
+
+      BlockUtils.verifyBCSId(kvContainer, blockID);
+      if (dispatcherContext == null) {
+        dispatcherContext = DispatcherContext.getHandleReadBlock();
+      }
+
+      boolean isReadChunkV0 = readBlock.getVersion()
+          .equals(ContainerProtos.ReadChunkVersion.V0);
+
+      long offset = readBlock.getOffset();
+      long len =  readBlock.getLen();
+      long adjustedChunkOffset, adjustedChunkLen;
+      do {
+        int startIndex = -1;
+        ContainerProtos.ChunkInfo chunk = chunkInfos.get(chunkIndex);
+        if (readBlock.getVerifyChecksum()) {
+          Pair<Long, Long> adjustedOffsetAndLength =
+              computeChecksumBoundaries(chunk, offset, len);
+          adjustedChunkOffset = adjustedOffsetAndLength.getLeft();
+          adjustedChunkLen = adjustedOffsetAndLength.getRight();
+          startIndex = (int) adjustedChunkOffset /
+              chunk.getChecksumData().getBytesPerChecksum();
+          adjustedChunkOffset += chunk.getOffset();
+        } else {
+          adjustedChunkOffset = offset;
+          adjustedChunkLen = Math.min(
+              chunk.getLen() + chunk.getOffset() - offset, len);
+        }
+
+        ChunkInfo chunkInfo = ChunkInfo.getFromProtoBuf(
+            ContainerProtos.ChunkInfo.newBuilder(chunk)
+                .setOffset(adjustedChunkOffset)
+                .setLen(adjustedChunkLen).build());
+        // For older clients, set ReadDataIntoSingleBuffer to true so that
+        // all the data read from chunk file is returned as a single
+        // ByteString. Older clients cannot process data returned as a list
+        // of ByteStrings.
+        if (isReadChunkV0) {
+          chunkInfo.setReadDataIntoSingleBuffer(isReadChunkV0);
+        }
+        data = chunkManager.readChunk(
+            kvContainer, blockID, chunkInfo, dispatcherContext);
+
+        Preconditions.checkNotNull(data, "Chunk data is null");
+        if (DispatcherContext.op(dispatcherContext).readFromTmpFile()) {
+          validateChunkChecksumData(data, chunkInfo);
+          metrics.incBytesReadStateMachine(chunkInfo.getLen());
+          metrics.incNumReadStateMachine();
+        }
+        streamObserver.onNext(
+            getReadBlockResponse(request,
+                blockData.getProtoBufMessage().getBlockID(),
+                chunkInfo.getProtoBufMessage(), isReadChunkV0,
+                data, byteBufferToByteString, startIndex));
+        len -= adjustedChunkLen + adjustedChunkOffset - offset;
+        offset = adjustedChunkOffset + adjustedChunkLen;
+        chunkIndex++;

Review Comment:
   Add a new test in `TestKeyValueHandler`. Do you think it is enough?



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