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

adoroszlai 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 0010bd4172f HDDS-17751. Datanode should check that a PutBlock 
committed through a data stream is for the stream's block (#12035)
0010bd4172f is described below

commit 0010bd4172f01cfc79007c6aa85369c0f5e083cc
Author: Rui Wang <[email protected]>
AuthorDate: Mon Nov 16 05:15:21 2026 +0800

    HDDS-17751. Datanode should check that a PutBlock committed through a data 
stream is for the stream's block (#12035)
---
 .../server/ratis/ContainerStateMachine.java        | 40 +++++++++---
 .../server/ratis/ContainerStateMachineTests.java   | 76 +++++++++++++++++++++-
 2 files changed, 105 insertions(+), 11 deletions(-)

diff --git 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachine.java
 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachine.java
index 8ab88c1155f..d35916e9457 100644
--- 
a/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachine.java
+++ 
b/hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachine.java
@@ -735,11 +735,37 @@ private StateMachine.DataChannel getStreamDataChannel(
       throw new StorageContainerException(response.getMessage(), 
response.getResult());
     }
     final CheckedConsumer<ContainerCommandRequestProto, IOException> putBlock
-        = requestProto.getCmdType() == Type.StreamInitWithPutBlock ? 
this::streamPutBlock : null;
+        = requestProto.getCmdType() == Type.StreamInitWithPutBlock ? r -> 
streamPutBlock(requestProto, r) : null;
     return dispatcher.getStreamDataChannel(requestProto, putBlock);
   }
 
-  void streamPutBlock(ContainerCommandRequestProto request) throws IOException 
{
+  /** A PutBlock sent on a data stream must be a PutBlock for the block the 
stream was opened for. */
+  private static void checkStreamPutBlock(ContainerCommandRequestProto 
streamInit,
+      ContainerCommandRequestProto request) throws StorageContainerException {
+    if (request.getCmdType() != Type.PutBlock) {
+      throw new StorageContainerException("Unexpected stream PutBlock request 
type " + request.getCmdType()
+          + ", expected " + Type.PutBlock, 
ContainerProtos.Result.MALFORMED_REQUEST);
+    }
+    final ContainerProtos.DatanodeBlockID putBlock = 
request.getPutBlock().getBlockData().getBlockID();
+    if (request.getContainerID() != putBlock.getContainerID()) {
+      throw new StorageContainerException("Container ID mismatched: request " 
+ request.getContainerID()
+          + " != PutBlock " + putBlock.getContainerID(), 
ContainerProtos.Result.MALFORMED_REQUEST);
+    }
+    if (request.getContainerID() != streamInit.getContainerID()) {
+      throw new StorageContainerException("Container ID mismatched: request " 
+ request.getContainerID()
+          + " != stream " + streamInit.getContainerID(), 
ContainerProtos.Result.MALFORMED_REQUEST);
+    }
+    final ContainerProtos.DatanodeBlockID streamBlock = 
streamInit.getWriteChunk().getBlockID();
+    if (!putBlock.equals(streamBlock)) {
+      throw new StorageContainerException("DatanodeBlockID mismatched: 
PutBlock "
+          + TextFormat.shortDebugString(putBlock) + " != stream " + 
TextFormat.shortDebugString(streamBlock),
+          ContainerProtos.Result.MALFORMED_REQUEST);
+    }
+  }
+
+  void streamPutBlock(ContainerCommandRequestProto streamInit, 
ContainerCommandRequestProto request)
+      throws IOException {
+    checkStreamPutBlock(streamInit, request);
     final DispatcherContext context = 
DispatcherContext.newBuilder(DispatcherContext.Op.STREAM_LINK)
         .setStage(DispatcherContext.WriteChunkStage.COMBINED)
         .setContainer2BCSIDMap(container2BCSIDMap)
@@ -755,14 +781,12 @@ void streamPutBlock(ContainerCommandRequestProto request) 
throws IOException {
    * @return the serialized {@link ContainerCommandResponseProto},
    *         which is identical on all the peers of the pipeline.
    */
-  ByteBuffer streamCommand(ByteBuffer command, DataChannel channel) throws 
IOException {
+  ByteBuffer streamCommand(ContainerCommandRequestProto streamInit, ByteBuffer 
command, DataChannel channel)
+      throws IOException {
     // unsafeWrap is okay since the command buffer can be modified after this 
method has returned.
     final ContainerCommandRequestProto request = 
ContainerCommandRequestMessage.toProto(
         UnsafeByteOperations.unsafeWrap(command), getGroupId());
-    if (request.getCmdType() != Type.PutBlock) {
-      throw new StorageContainerException("Unexpected stream command " + 
request.getCmdType()
-          + ", expected " + Type.PutBlock, 
ContainerProtos.Result.MALFORMED_REQUEST);
-    }
+    checkStreamPutBlock(streamInit, request);
     if (request.getPutBlock().getSync()) {
       channel.force(false);
     }
@@ -793,7 +817,7 @@ public CompletableFuture<DataStream> 
stream(RaftClientRequest request) {
         final DataChannel channel = getStreamDataChannel(requestProto, 
context);
         final ExecutorService chunkExecutor = requestProto.hasWriteChunk() ?
             getChunkExecutor(requestProto.getWriteChunk()) : null;
-        return new LocalStream(channel, chunkExecutor, command -> 
streamCommand(command, channel));
+        return new LocalStream(channel, chunkExecutor, command -> 
streamCommand(requestProto, command, channel));
       } catch (IOException e) {
         throw new CompletionException("Failed to create data stream", e);
       }
diff --git 
a/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachineTests.java
 
b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachineTests.java
index db35c816865..82c158c8572 100644
--- 
a/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachineTests.java
+++ 
b/hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachineTests.java
@@ -38,6 +38,7 @@
 import com.google.common.util.concurrent.ThreadFactoryBuilder;
 import java.io.IOException;
 import java.nio.ByteBuffer;
+import java.util.Arrays;
 import java.util.List;
 import java.util.UUID;
 import java.util.concurrent.CompletableFuture;
@@ -68,6 +69,7 @@
 import org.apache.ratis.statemachine.StateMachine;
 import org.apache.ratis.statemachine.TransactionContext;
 import org.apache.ratis.thirdparty.com.google.protobuf.ByteString;
+import org.apache.ratis.util.function.CheckedConsumer;
 import org.junit.jupiter.api.AfterAll;
 import org.junit.jupiter.api.AfterEach;
 import org.junit.jupiter.api.BeforeEach;
@@ -310,11 +312,56 @@ public void 
testStreamOpenedOnlyIfStreamInitSucceeds(ContainerProtos.Type stream
     verify(dispatcher, never()).getStreamDataChannel(any(), any());
   }
 
+  @Test
+  public void testStreamCommandPutBlockNotMatchingStreamBlockIsRejected() 
throws Exception {
+    final StateMachine.DataStream stream = 
setUpStream(mock(KeyValueStreamDataChannel.class));
+
+    for (ContainerProtos.ContainerCommandRequestProto putBlock : 
newPutBlocksNotMatchingStreamBlock()) {
+      final ByteBuffer command = 
ContainerCommandRequestMessage.toMessage(putBlock, null).getContent()
+          .asReadOnlyByteBuffer();
+      final ExecutionException e = assertThrows(ExecutionException.class, () 
-> stream.onCommand(command, 0).get());
+      final StorageContainerException sce = 
assertInstanceOf(StorageContainerException.class, e.getCause());
+      assertEquals(ContainerProtos.Result.MALFORMED_REQUEST, sce.getResult());
+    }
+    verify(dispatcher, never()).dispatch(argThat(r -> r.getCmdType() == 
ContainerProtos.Type.PutBlock), any());
+
+    stream.onCommand(newPutBlockCommand(), 0).get();
+    verify(dispatcher).dispatch(argThat(r -> r.getCmdType() == 
ContainerProtos.Type.PutBlock),
+        argThat(c -> DispatcherContext.op(c) == 
DispatcherContext.Op.STREAM_COMMAND));
+  }
+
+  @Test
+  public void testStreamClosePutBlockNotMatchingStreamBlockIsRejected() throws 
Exception {
+    final 
AtomicReference<CheckedConsumer<ContainerProtos.ContainerCommandRequestProto, 
IOException>> closePutBlock
+        = new AtomicReference<>();
+    setUpStream(mock(KeyValueStreamDataChannel.class), 
ContainerProtos.Type.StreamInitWithPutBlock, closePutBlock);
+
+    for (ContainerProtos.ContainerCommandRequestProto request : 
newPutBlocksNotMatchingStreamBlock()) {
+      final StorageContainerException e = 
assertThrows(StorageContainerException.class,
+          () -> closePutBlock.get().accept(request));
+      assertEquals(ContainerProtos.Result.MALFORMED_REQUEST, e.getResult());
+    }
+    verify(dispatcher, never()).dispatch(argThat(r -> r.getCmdType() == 
ContainerProtos.Type.PutBlock), any());
+
+    closePutBlock.get().accept(newPutBlock(1, newBlockID()));
+    verify(dispatcher).dispatch(argThat(r -> r.getCmdType() == 
ContainerProtos.Type.PutBlock),
+        argThat(c -> DispatcherContext.op(c) == 
DispatcherContext.Op.STREAM_LINK));
+  }
+
   private StateMachine.DataStream setUpStream(StateMachine.DataChannel 
channel) throws Exception {
+    return setUpStream(channel, ContainerProtos.Type.StreamInit, new 
AtomicReference<>());
+  }
+
+  private StateMachine.DataStream setUpStream(StateMachine.DataChannel 
channel, ContainerProtos.Type streamInitType,
+      
AtomicReference<CheckedConsumer<ContainerProtos.ContainerCommandRequestProto, 
IOException>> closePutBlock)
+      throws Exception {
     when(dispatcher.dispatch(any(), 
any())).thenReturn(ContainerProtos.ContainerCommandResponseProto.newBuilder()
         
.setCmdType(ContainerProtos.Type.PutBlock).setResult(ContainerProtos.Result.SUCCESS).build());
-    when(dispatcher.getStreamDataChannel(any(), any())).thenReturn(channel);
-    return 
stateMachine.stream(newStreamRequest(ContainerProtos.Type.StreamInit)).get();
+    when(dispatcher.getStreamDataChannel(any(), any())).thenAnswer(invocation 
-> {
+      closePutBlock.set(invocation.getArgument(1));
+      return channel;
+    });
+    return stateMachine.stream(newStreamRequest(streamInitType)).get();
   }
 
   private RaftClientRequest newStreamRequest(ContainerProtos.Type 
streamInitType) {
@@ -347,8 +394,31 @@ private static ByteBuffer newPutBlockCommand(boolean sync) 
{
     return ContainerCommandRequestMessage.toMessage(putBlock, 
null).getContent().asReadOnlyByteBuffer();
   }
 
+  private static ContainerProtos.ContainerCommandRequestProto newPutBlock(long 
containerID,
+      ContainerProtos.DatanodeBlockID blockID) {
+    return ContainerProtos.ContainerCommandRequestProto.newBuilder()
+        .setCmdType(ContainerProtos.Type.PutBlock)
+        .setPutBlock(ContainerProtos.PutBlockRequestProto.newBuilder()
+            
.setBlockData(ContainerProtos.BlockData.newBuilder().setBlockID(blockID).build()))
+        .setContainerID(containerID)
+        .setDatanodeUuid(UUID.randomUUID().toString())
+        .build();
+  }
+
+  /** PutBlocks that do not match the stream block (container 1, local ID 1). 
*/
+  private static List<ContainerProtos.ContainerCommandRequestProto> 
newPutBlocksNotMatchingStreamBlock() {
+    return Arrays.asList(
+        newPutBlock(1, newBlockID(1, 2)),
+        newPutBlock(2, newBlockID(2, 1)),
+        newPutBlock(2, newBlockID()));
+  }
+
   private static ContainerProtos.DatanodeBlockID newBlockID() {
-    return 
ContainerProtos.DatanodeBlockID.newBuilder().setContainerID(1).setLocalID(1).build();
+    return newBlockID(1, 1);
+  }
+
+  private static ContainerProtos.DatanodeBlockID newBlockID(long containerID, 
long localID) {
+    return 
ContainerProtos.DatanodeBlockID.newBuilder().setContainerID(containerID).setLocalID(localID).build();
   }
 
   private void setUpMockDispatcherReturn(boolean failWithException) {


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

Reply via email to