This is an automated email from the ASF dual-hosted git repository.

ChenSammi pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ozone.git


The following commit(s) were added to refs/heads/master by this push:
     new 727c15b7df1 HDDS-16419. Avoid nested datanode failover in GetBlock and 
ReadChunk (#11239)
727c15b7df1 is described below

commit 727c15b7df1bf9300b3bcab7c4df05ef6e435661
Author: Symious <[email protected]>
AuthorDate: Mon Nov 2 19:12:19 2026 +0800

    HDDS-16419. Avoid nested datanode failover in GetBlock and ReadChunk 
(#11239)
---
 .../apache/hadoop/hdds/scm/XceiverClientGrpc.java  | 130 +++++++++++++++------
 .../hdds/scm/storage/TestChunkInputStream.java     |   4 +-
 .../scm/storage/TestStreamBlockInputStream.java    |   7 +-
 .../apache/hadoop/hdds/scm/XceiverClientSpi.java   |  31 +++++
 .../hdds/scm/storage/ContainerProtocolCalls.java   |  30 +++--
 .../hadoop/hdds/scm/TestXceiverClientGrpc.java     | 108 +++++++++++++----
 6 files changed, 240 insertions(+), 70 deletions(-)

diff --git 
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientGrpc.java
 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientGrpc.java
index 7c7e465a55f..a4c49db8bea 100644
--- 
a/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientGrpc.java
+++ 
b/hadoop-hdds/client/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientGrpc.java
@@ -408,22 +408,57 @@ public ContainerCommandResponseProto sendCommand(
     }
   }
 
+  @Override
+  public ContainerCommandResponseProto sendCommand(
+      ContainerCommandRequestProto request, List<Validator> validators,
+      DatanodeDetails datanode) throws IOException {
+    return TracingUtil.executeInNewSpan(getSpanName(request), SpanKind.CLIENT,
+        () -> {
+          final ContainerCommandRequestProto requestWithTraceID = 
withTraceIDAndVersion(request);
+          try {
+            return sendCommandToDatanode(requestWithTraceID, validators, 
datanode);
+          } catch (ExecutionException e) {
+            if (LOG.isDebugEnabled()) {
+              LOG.debug("Failed to execute command {} on datanode {}",
+                  processForDebug(requestWithTraceID), datanode, e);
+            }
+            throw toIOException(e);
+          } catch (InterruptedException e) {
+            LOG.error("Command execution was interrupted ", e);
+            Thread.currentThread().interrupt();
+            throw (IOException) new InterruptedIOException(
+                "Command " + processForDebug(requestWithTraceID) + " was 
interrupted.")
+                .initCause(e);
+          }
+        });
+  }
+
+  @Override
+  public List<DatanodeDetails> getDatanodesInOrder(DatanodeBlockID blockID,
+      ContainerProtos.Type cmdType) throws IOException {
+    return sortDatanodes(blockID, cmdType);
+  }
+
   private XceiverClientReply sendCommandWithTraceIDAndRetry(
       ContainerCommandRequestProto request, List<Validator> validators)
       throws IOException {
+    return TracingUtil.executeInNewSpan(getSpanName(request), SpanKind.CLIENT,
+        () -> sendCommandWithRetry(withTraceIDAndVersion(request), 
validators));
+  }
 
-    String spanName = "XceiverClientGrpc." + request.getCmdType().name();
+  private static String getSpanName(ContainerCommandRequestProto request) {
+    return "XceiverClientGrpc." + request.getCmdType().name();
+  }
 
-    return TracingUtil.executeInNewSpan(spanName, SpanKind.CLIENT,
-        () -> {
-          ContainerCommandRequestProto.Builder builder =
-              ContainerCommandRequestProto.newBuilder(request)
-                  .setTraceID(TracingUtil.exportCurrentSpan());
-          if (!request.hasVersion()) {
-            builder.setVersion(ClientVersion.CURRENT.serialize());
-          }
-          return sendCommandWithRetry(builder.build(), validators);
-        });
+  private static ContainerCommandRequestProto withTraceIDAndVersion(
+      ContainerCommandRequestProto request) {
+    ContainerCommandRequestProto.Builder builder =
+        ContainerCommandRequestProto.newBuilder(request)
+            .setTraceID(TracingUtil.exportCurrentSpan());
+    if (!request.hasVersion()) {
+      builder.setVersion(ClientVersion.CURRENT.serialize());
+    }
+    return builder.build();
   }
 
   private List<DatanodeDetails> sortDatanodes(ContainerCommandRequestProto 
request) throws IOException {
@@ -443,7 +478,7 @@ List<DatanodeDetails> sortDatanodes(DatanodeBlockID 
blockID, ContainerProtos.Typ
         }
       }
       // Check if the DN to which the GetBlock command was sent has been 
cached.
-      DatanodeDetails cachedDN = getBlockDNcache.get(blockID);
+      DatanodeDetails cachedDN = getBlockDNcache.get(toCacheKey(blockID));
       if (cachedDN != null && !topologyAwareRead) {
         datanodeList = pipeline.getNodes();
         int getBlockDNCacheIndex = datanodeList.indexOf(cachedDN);
@@ -486,6 +521,53 @@ private static DatanodeBlockID 
getRequestBlockID(ContainerCommandRequestProto re
     return blockID;
   }
 
+  /**
+   * Sends the command to the given datanode only and validates the response.
+   */
+  private ContainerCommandResponseProto sendCommandToDatanode(
+      ContainerCommandRequestProto request, List<Validator> validators,
+      DatanodeDetails dn)
+      throws IOException, ExecutionException, InterruptedException {
+    if (LOG.isDebugEnabled()) {
+      LOG.debug("Executing command {} on datanode {}",
+          processForDebug(request), dn);
+    }
+    final ContainerCommandResponseProto responseProto =
+        sendCommandAsync(request, dn).getResponse().get();
+    if (validators != null && !validators.isEmpty()) {
+      for (Validator validator : validators) {
+        validator.accept(request, responseProto);
+      }
+    }
+    if (request.getCmdType() == ContainerProtos.Type.GetBlock) {
+      DatanodeBlockID getBlockID = request.getGetBlock().getBlockID();
+      getBlockDNcache.put(toCacheKey(getBlockID), dn);
+    }
+    return responseProto;
+  }
+
+  /**
+   * The replicaIndex is unset or 0 depending on how the caller built the 
block ID,
+   * so it is dropped from the key of {@link #getBlockDNcache}.
+   */
+  private static DatanodeBlockID toCacheKey(DatanodeBlockID blockID) {
+    return blockID.hasReplicaIndex() ? 
blockID.toBuilder().clearReplicaIndex().build() : blockID;
+  }
+
+  /**
+   * @return the IOException for a failed command
+   * @throws SCMSecurityException if the datanode rejected the block token
+   */
+  private static IOException toIOException(ExecutionException e)
+      throws SCMSecurityException {
+    if (Status.fromThrowable(e.getCause()).getCode()
+        == Status.UNAUTHENTICATED.getCode()) {
+      throw new SCMSecurityException("Failed to authenticate with "
+          + "GRPC XceiverServer with Ozone block token.");
+    }
+    return new IOException(e);
+  }
+
   private XceiverClientReply sendCommandWithRetry(
       ContainerCommandRequestProto request, List<Validator> validators)
       throws IOException {
@@ -499,28 +581,14 @@ private XceiverClientReply sendCommandWithRetry(
 
     for (DatanodeDetails dn : datanodeList) {
       try {
-        if (LOG.isDebugEnabled()) {
-          LOG.debug("Executing command {} on datanode {}",
-              processForDebug(request), dn);
-        }
         // In case the command gets retried on a 2nd datanode,
         // sendCommandAsyncCall will create a new channel and async stub
         // in case these don't exist for the specific datanode.
         reply.addDatanode(dn);
-        responseProto = sendCommandAsync(request, dn).getResponse().get();
-        if (validators != null && !validators.isEmpty()) {
-          for (Validator validator : validators) {
-            validator.accept(request, responseProto);
-          }
-        }
-        if (request.getCmdType() == ContainerProtos.Type.GetBlock) {
-          DatanodeBlockID getBlockID = request.getGetBlock().getBlockID();
-          getBlockDNcache.put(getBlockID, dn);
-        }
+        responseProto = sendCommandToDatanode(request, validators, dn);
         break;
       } catch (IOException e) {
         ioException = e;
-        responseProto = null;
         if (LOG.isDebugEnabled()) {
           LOG.debug("Failed to execute command {} on datanode {}",
               processForDebug(request), dn, e);
@@ -530,13 +598,7 @@ private XceiverClientReply sendCommandWithRetry(
           LOG.debug("Failed to execute command {} on datanode {}",
               processForDebug(request), dn, e);
         }
-        if (Status.fromThrowable(e.getCause()).getCode()
-            == Status.UNAUTHENTICATED.getCode()) {
-          throw new SCMSecurityException("Failed to authenticate with "
-              + "GRPC XceiverServer with Ozone block token.");
-        }
-
-        ioException = new IOException(e);
+        ioException = toIOException(e);
       } catch (InterruptedException e) {
         LOG.error("Command execution was interrupted ", e);
         Thread.currentThread().interrupt();
diff --git 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestChunkInputStream.java
 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestChunkInputStream.java
index f1d2156b91e..e25f733e7e7 100644
--- 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestChunkInputStream.java
+++ 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestChunkInputStream.java
@@ -474,7 +474,9 @@ public void connectsToNewPipeline() throws Exception {
         ArgumentCaptor.forClass(ContainerCommandRequestProto.class);
     when(client.getPipeline())
         .thenAnswer(invocation -> pipelineRef.get());
-    when(client.sendCommand(requestCaptor.capture(), any()))
+    when(client.getDatanodesInOrder(any(), any()))
+        .thenAnswer(invocation -> pipelineRef.get().getNodes());
+    when(client.sendCommand(requestCaptor.capture(), any(), any()))
         .thenAnswer(invocation ->
             getReadChunkResponse(
                 requestCaptor.getValue(),
diff --git 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestStreamBlockInputStream.java
 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestStreamBlockInputStream.java
index 7cabc661a28..465af2204d2 100644
--- 
a/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestStreamBlockInputStream.java
+++ 
b/hadoop-hdds/client/src/test/java/org/apache/hadoop/hdds/scm/storage/TestStreamBlockInputStream.java
@@ -496,7 +496,7 @@ public void 
testUnderConstructionBlockUsesLengthFromDatanode() throws Exception
       assertEquals(-1, sbis.read(buf));
       sbis.seek(data.length);
     }
-    verify(client, 
times(1)).sendCommand(any(ContainerCommandRequestProto.class), any());
+    verify(client, 
times(1)).sendCommand(any(ContainerCommandRequestProto.class), any(), any());
   }
 
   @Test
@@ -511,7 +511,7 @@ public void testCompletedBlockDoesNotGetBlockFromDatanode() 
throws Exception {
       assertArrayEquals(data, buf.array());
       sbis.seek(0);
     }
-    verify(client, 
never()).sendCommand(any(ContainerCommandRequestProto.class), any());
+    verify(client, 
never()).sendCommand(any(ContainerCommandRequestProto.class), any(), any());
   }
 
   private XceiverClientGrpc mockGetBlockClient(byte[] data, Pipeline pipeline) 
throws Exception {
@@ -519,7 +519,8 @@ private XceiverClientGrpc mockGetBlockClient(byte[] data, 
Pipeline pipeline) thr
     when(client.getPipeline()).thenReturn(pipeline);
     DatanodeDetails datanode = pipeline.getFirstNode();
     when(pipeline.getClosestNode(any())).thenReturn(datanode);
-    when(client.sendCommand(any(ContainerCommandRequestProto.class), 
any())).thenAnswer(inv -> {
+    when(client.getDatanodesInOrder(any(), 
any())).thenReturn(Collections.singletonList(datanode));
+    when(client.sendCommand(any(ContainerCommandRequestProto.class), any(), 
any())).thenAnswer(inv -> {
       ContainerCommandRequestProto request = inv.getArgument(0);
       return ContainerCommandResponseProto.newBuilder()
           .setCmdType(Type.GetBlock)
diff --git 
a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientSpi.java
 
b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientSpi.java
index 0c4b67ac7e9..e04e4edf450 100644
--- 
a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientSpi.java
+++ 
b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/XceiverClientSpi.java
@@ -159,6 +159,37 @@ public ContainerCommandResponseProto sendCommand(
     }
   }
 
+  /**
+   * Sends a given command to the given datanode only, without failing over
+   * to the other datanodes in the pipeline.
+   * Implementations which cannot target a specific datanode fall back to
+   * {@link #sendCommand(ContainerCommandRequestProto, List)}.
+   * @param request Request
+   * @param validators functions to validate the response
+   * @param datanode the datanode to send the command to
+   * @return Response to the command
+   */
+  public ContainerCommandResponseProto sendCommand(
+      ContainerCommandRequestProto request,
+      List<Validator> validators,
+      DatanodeDetails datanode)
+      throws IOException {
+    return sendCommand(request, validators);
+  }
+
+  /**
+   * Returns the datanodes of the pipeline in the order they should be tried
+   * for a command on the given block.
+   * @param blockID the block the command operates on
+   * @param cmdType type of the command
+   * @return datanodes of the pipeline, in the order to try
+   */
+  public List<DatanodeDetails> getDatanodesInOrder(
+      ContainerProtos.DatanodeBlockID blockID, ContainerProtos.Type cmdType)
+      throws IOException {
+    return getPipeline().getNodesInOrder();
+  }
+
   public void initStreamRead(BlockID blockID, StreamingReaderSpi 
streamObserver) throws IOException {
     throw new UnsupportedOperationException("Stream read is not supported");
   }
diff --git 
a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/storage/ContainerProtocolCalls.java
 
b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/storage/ContainerProtocolCalls.java
index e76592d9c9e..c27388a8104 100644
--- 
a/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/storage/ContainerProtocolCalls.java
+++ 
b/hadoop-hdds/common/src/main/java/org/apache/hadoop/hdds/scm/storage/ContainerProtocolCalls.java
@@ -25,10 +25,8 @@
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.HashMap;
-import java.util.HashSet;
 import java.util.List;
 import java.util.Map;
-import java.util.Set;
 import java.util.concurrent.ExecutionException;
 import java.util.function.Function;
 import org.apache.hadoop.hdds.annotation.InterfaceStability;
@@ -143,13 +141,20 @@ public static ListBlockResponseProto 
listBlock(XceiverClientSpi xceiverClient,
     return response.getListBlock();
   }
 
-  static <T> T tryEachDatanode(Pipeline pipeline,
+  /**
+   * Applies {@code op} to each datanode in order until it succeeds.
+   * {@code op} must only contact the given datanode, otherwise each attempt
+   * would fail over to the whole pipeline again.
+   */
+  static <T> T tryEachDatanode(List<DatanodeDetails> datanodes,
       CheckedFunction<DatanodeDetails, T, IOException> op,
       Function<DatanodeDetails, String> toErrorMessage)
       throws IOException {
-    final Set<DatanodeDetails> excluded = new HashSet<>();
-    for (; ;) {
-      final DatanodeDetails d = pipeline.getClosestNode(excluded);
+    if (datanodes.isEmpty()) {
+      throw new IOException("No datanode to try");
+    }
+    for (int i = 0; ; i++) {
+      final DatanodeDetails d = datanodes.get(i);
 
       try {
         return op.apply(d);
@@ -165,8 +170,7 @@ static <T> T tryEachDatanode(Pipeline pipeline,
           }
         }
         span.addEvent("failed to connect to DN " + d);
-        excluded.add(d);
-        if (excluded.size() < pipeline.size()) {
+        if (i < datanodes.size() - 1) {
           LOG.warn(toErrorMessage.apply(d)
               + "; will try another datanode.", e);
         } else {
@@ -197,7 +201,8 @@ public static GetBlockResponseProto 
getBlock(XceiverClientSpi xceiverClient,
       builder.setEncodedToken(token.encodeToUrlString());
     }
 
-    return tryEachDatanode(xceiverClient.getPipeline(),
+    return tryEachDatanode(
+        
xceiverClient.getDatanodesInOrder(blockID.getDatanodeBlockIDProtobuf(), 
Type.GetBlock),
         d -> getBlock(xceiverClient, validators, builder, blockID, d, 
pipeline),
         d -> toErrorMessage(blockID, d));
   }
@@ -256,7 +261,7 @@ private static GetBlockResponseProto 
getBlock(XceiverClientSpi xceiverClient,
         .setDatanodeUuid(datanode.getUuidString())
         .setGetBlock(readBlockRequest).build();
     ContainerCommandResponseProto response =
-        xceiverClient.sendCommand(request, validators);
+        xceiverClient.sendCommand(request, validators, datanode);
     return response.getGetBlock();
   }
 
@@ -434,7 +439,8 @@ public static ContainerProtos.ReadChunkResponseProto 
readChunk(
       span.setAttribute("offset", chunk.getOffset())
           .setAttribute("length", chunk.getLen())
           .setAttribute("block", blockID.toString());
-      return tryEachDatanode(xceiverClient.getPipeline(),
+      return tryEachDatanode(
+          xceiverClient.getDatanodesInOrder(blockID, Type.ReadChunk),
           d -> readChunk(xceiverClient, chunk, blockID,
               validators, builder, d),
           d -> toErrorMessage(chunk, blockID, d));
@@ -453,7 +459,7 @@ private static ContainerProtos.ReadChunkResponseProto 
readChunk(
       requestBuilder = requestBuilder.setTraceID(traceId);
     }
     ContainerCommandResponseProto reply =
-        xceiverClient.sendCommand(requestBuilder.build(), validators);
+        xceiverClient.sendCommand(requestBuilder.build(), validators, d);
     final ReadChunkResponseProto response = reply.getReadChunk();
     final long readLen = getLen(response);
     if (readLen != chunk.getLen()) {
diff --git 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/hdds/scm/TestXceiverClientGrpc.java
 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/hdds/scm/TestXceiverClientGrpc.java
index b2ff5ea70d9..ceb8c10fd4d 100644
--- 
a/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/hdds/scm/TestXceiverClientGrpc.java
+++ 
b/hadoop-ozone/integration-test/src/test/java/org/apache/hadoop/hdds/scm/TestXceiverClientGrpc.java
@@ -51,6 +51,7 @@
 import org.apache.hadoop.ozone.OzoneConfigKeys;
 import org.apache.ozone.test.GenericTestUtils;
 import org.apache.ratis.protocol.exceptions.TimeoutIOException;
+import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
 import org.apache.ratis.thirdparty.io.grpc.stub.ClientCallStreamObserver;
 import org.junit.jupiter.api.BeforeEach;
 import org.junit.jupiter.api.Test;
@@ -121,43 +122,90 @@ public XceiverClientReply sendCommandAsync(
   }
 
   @Test
-  public void testGetBlockRetryAlNodes() {
-    final ArrayList<DatanodeDetails> allDNs = new ArrayList<>(dns);
-    assertThat(allDNs.size()).isGreaterThan(1);
+  public void testGetBlockRetryAlNodes() throws IOException {
+    final List<DatanodeDetails> seenDNs = new ArrayList<>();
+    try (XceiverClientGrpc client = failingClient(seenDNs)) {
+      assertThrows(IOException.class, () -> 
invokeXceiverClientGetBlock(client));
+    }
+    // Each datanode is tried exactly once, not once per datanode tried.
+    assertThat(seenDNs).containsExactlyInAnyOrderElementsOf(dns);
+  }
+
+  @Test
+  public void testGetBlockUsesCachedDatanodeAfterFailover() throws IOException 
{
+    conf.setBoolean(OzoneConfigKeys.OZONE_NETWORK_TOPOLOGY_AWARE_READ_KEY, 
false);
+    final List<DatanodeDetails> seenDNs = new ArrayList<>();
     try (XceiverClientGrpc client = new XceiverClientGrpc(pipeline, conf) {
       @Override
       public XceiverClientReply sendCommandAsync(
           ContainerProtos.ContainerCommandRequestProto request,
           DatanodeDetails dn) throws IOException {
-        allDNs.remove(dn);
-        throw new IOException("Failed " + dn);
+        seenDNs.add(dn);
+        // The first datanode tried, i.e. the leader, always fails.
+        if (dn.equals(seenDNs.get(0))) {
+          throw new IOException("Failed " + dn);
+        }
+        return buildValidResponse();
       }
     }) {
       invokeXceiverClientGetBlock(client);
-    } catch (IOException e) {
-      e.printStackTrace();
+      assertEquals(2, seenDNs.size());
+      final DatanodeDetails cached = seenDNs.get(1);
+
+      // The next GetBlock goes to the cached datanode instead of retrying the 
failed leader.
+      invokeXceiverClientGetBlock(client);
+      assertEquals(3, seenDNs.size());
+      assertEquals(cached, seenDNs.get(2));
     }
-    assertEquals(0, allDNs.size());
   }
 
   @Test
-  public void testReadChunkRetryAllNodes() {
-    final ArrayList<DatanodeDetails> allDNs = new ArrayList<>(dns);
-    assertThat(allDNs.size()).isGreaterThan(1);
+  public void testReadChunkRetryAllNodes() throws IOException {
+    final List<DatanodeDetails> seenDNs = new ArrayList<>();
+    try (XceiverClientGrpc client = failingClient(seenDNs)) {
+      assertThrows(IOException.class, () -> 
invokeXceiverClientReadChunk(client));
+    }
+    // Each datanode is tried exactly once, not once per datanode tried.
+    assertThat(seenDNs).containsExactlyInAnyOrderElementsOf(dns);
+  }
+
+  @Test
+  public void testReadChunkRetryNextNodeOnShortRead() throws IOException {
+    final int chunkLen = 10;
+    final List<DatanodeDetails> seenDNs = new ArrayList<>();
     try (XceiverClientGrpc client = new XceiverClientGrpc(pipeline, conf) {
       @Override
       public XceiverClientReply sendCommandAsync(
           ContainerProtos.ContainerCommandRequestProto request,
-          DatanodeDetails dn) throws IOException {
-        allDNs.remove(dn);
-        throw new IOException("Failed " + dn);
+          DatanodeDetails dn) {
+        seenDNs.add(dn);
+        // The first datanode returns less data than requested.
+        return buildReadChunkResponse(request, seenDNs.size() == 1 ? chunkLen 
- 1 : chunkLen);
       }
     }) {
-      invokeXceiverClientReadChunk(client);
-    } catch (IOException e) {
-      e.printStackTrace();
+      ContainerProtos.ReadChunkResponseProto response =
+          invokeXceiverClientReadChunk(client, chunkLen);
+      assertEquals(chunkLen, response.getData().size());
     }
-    assertEquals(0, allDNs.size());
+    assertEquals(2, seenDNs.size());
+    assertNotEquals(seenDNs.get(0), seenDNs.get(1));
+  }
+
+  /**
+   * @return a client failing all commands, recording the target datanodes
+   */
+  private XceiverClientGrpc failingClient(List<DatanodeDetails> seenDNs) {
+    return new XceiverClientGrpc(pipeline, conf) {
+      @Override
+      public XceiverClientReply sendCommandAsync(
+          ContainerProtos.ContainerCommandRequestProto request,
+          DatanodeDetails dn) throws IOException {
+        seenDNs.add(dn);
+        // The request must be addressed to the datanode it is sent to.
+        assertEquals(dn.getUuidString(), request.getDatanodeUuid());
+        throw new IOException("Failed " + dn);
+      }
+    };
   }
 
   @Test
@@ -281,22 +329,42 @@ private void invokeXceiverClientGetBlock(XceiverClientSpi 
client)
 
   private void invokeXceiverClientReadChunk(XceiverClientSpi client)
       throws IOException {
+    invokeXceiverClientReadChunk(client, -1);
+  }
+
+  private ContainerProtos.ReadChunkResponseProto invokeXceiverClientReadChunk(
+      XceiverClientSpi client, long len) throws IOException {
     BlockID bid = new BlockID(1, 1);
     bid.setBlockCommitSequenceId(1);
-    ContainerProtocolCalls.readChunk(client,
+    return ContainerProtocolCalls.readChunk(client,
         ContainerProtos.ChunkInfo.newBuilder()
             .setChunkName("Anything")
             .setChecksumData(ContainerProtos.ChecksumData.newBuilder()
                 .setBytesPerChecksum(512)
                 .setType(ContainerProtos.ChecksumType.CRC32)
                 .build())
-            .setLen(-1)
+            .setLen(len)
             .setOffset(0)
             .build(),
         bid.getDatanodeBlockIDProtobuf(),
         null, null);
   }
 
+  private XceiverClientReply buildReadChunkResponse(
+      ContainerProtos.ContainerCommandRequestProto request, int len) {
+    ContainerProtos.ReadChunkRequestProto readChunk = request.getReadChunk();
+    ContainerProtos.ContainerCommandResponseProto resp =
+        ContainerProtos.ContainerCommandResponseProto.newBuilder()
+            .setCmdType(ContainerProtos.Type.ReadChunk)
+            .setResult(ContainerProtos.Result.SUCCESS)
+            .setReadChunk(ContainerProtos.ReadChunkResponseProto.newBuilder()
+                .setBlockID(readChunk.getBlockID())
+                .setChunkData(readChunk.getChunkData())
+                .setData(ByteString.copyFrom(new byte[len])))
+            .build();
+    return new XceiverClientReply(CompletableFuture.completedFuture(resp));
+  }
+
   private void invokeXceiverClientReadSmallFile(XceiverClientSpi client)
       throws IOException {
     BlockID bid = new BlockID(1, 1);


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to