szetszwo commented on code in PR #11412:
URL: https://github.com/apache/ozone/pull/11412#discussion_r4251458769


##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ClosedContainerReadResolver.java:
##########
@@ -0,0 +1,67 @@
+/*
+ * 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.ozone.container.common.transport.server.ratis;
+
+import java.io.IOException;
+import org.apache.hadoop.hdds.protocol.DatanodeDetails;
+import 
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerCommandRequestProto;
+import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.Type;
+import org.apache.hadoop.hdds.ratis.ContainerCommandRequestMessage;
+import org.apache.hadoop.ozone.container.common.interfaces.Container;
+import org.apache.hadoop.ozone.container.common.interfaces.ContainerDispatcher;
+import org.apache.hadoop.ozone.container.ozoneimpl.ContainerController;
+import org.apache.ratis.protocol.RaftClientRequest;
+import org.apache.ratis.server.api.DataStreamApi;
+
+/**
+ * Serves ReadBlock on a Ratis read-only data stream, without a Raft group, 
for a container that is not open or closing.
+ * SCM gives the read pipeline of such a container a random ID, and no Raft 
group has this ID.
+ * The container no longer changes through Raft, so this datanode can serve 
the read from its replica.
+ */
+final class ClosedContainerReadResolver implements DataStreamApi.Resolver {
+  private final ContainerDispatcher dispatcher;
+  private final ContainerController containerController;
+  private final String datanodeUuid;
+
+  ClosedContainerReadResolver(ContainerDispatcher dispatcher, 
ContainerController containerController,
+      DatanodeDetails datanode) {
+    this.dispatcher = dispatcher;
+    this.containerController = containerController;
+    this.datanodeUuid = datanode.getUuidString();
+  }
+
+  /**
+   * @return the API to serve a ReadBlock of a container on this datanode that 
is not open or closing. Otherwise, null:
+   *     Ratis then serves the request with its Raft group.
+   */
+  @Override
+  public DataStreamApi resolve(RaftClientRequest request) throws IOException {
+    final ContainerCommandRequestProto requestProto = 
ContainerCommandRequestMessage.toProto(
+        request.getMessage().getContent(), request.getRaftGroupId());
+    if (requestProto.getCmdType() != Type.ReadBlock
+        || requestProto.hasDatanodeUuid() && 
!datanodeUuid.equals(requestProto.getDatanodeUuid())) {
+      return null;
+    }
+
+    final Container<?> container = 
containerController.getContainer(requestProto.getContainerID());
+    if (container == null || container.getContainerData().isOpen() || 
container.getContainerData().isClosing()) {
+      return null;
+    }
+    return (message, stream) -> 
ContainerStateMachine.streamReadBlock(dispatcher, requestProto, stream);
+  }
+}

Review Comment:
   We should throw an exception:
   ```java
     @Override
     public DataStreamApi resolve(RaftClientRequest request) throws IOException 
{
       final ContainerCommandRequestProto requestProto = 
ContainerCommandRequestMessage.toProto(
           request.getMessage().getContent(), request.getRaftGroupId());
       if (requestProto.getCmdType() != Type.ReadBlock) {
         throw new IOException("Non-ReadBlock command type: " + 
requestProto.getCmdType());
       }
       if (requestProto.hasDatanodeUuid() && 
!datanodeUuid.equals(requestProto.getDatanodeUuid())) {
         throw new IOException("Datanode uuid mismatch: requestProto has "
             + requestProto.getDatanodeUuid() + " but the local datanode is " + 
datanodeUuid);
       }
   
       final Container<?> container = 
containerController.getContainer(requestProto.getContainerID());
       if (container == null) {
         throw new IOException("Container not found: " + 
requestProto.getContainerID());
       }
       if (container.getContainerData().getState() != 
ContainerProtos.ContainerDataProto.State.CLOSED) {
         throw new IOException("Non-CLOSED container state: " + 
container.getContainerData().getState());
       }
       return (message, stream) -> 
ContainerStateMachine.streamReadBlock(dispatcher, requestProto, stream);
     }
   ```



##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachine.java:
##########
@@ -892,6 +901,97 @@ public CompletableFuture<Message> query(Message request) {
     }
   }
 
+  /** Serves a Ratis read-only data stream. Only ReadBlock is supported, see 
{@link #streamReadBlock}. */
+  @Override
+  public long transferTo(Message request, WritableByteChannel stream) throws 
IOException {
+    try {
+      metrics.incNumQueryStateMachineOps();
+      final ContainerCommandRequestProto requestProto = 
message2ContainerCommandRequestProto(request);
+      if (requestProto.getCmdType() != Type.ReadBlock) {
+        throw new IOException("Unsupported command for a read-only data 
stream: " + requestProto.getCmdType());
+      }
+      return streamReadBlock(dispatcher, requestProto, stream);
+    } catch (IOException e) {
+      metrics.incNumQueryStateMachineFails();
+      throw e;
+    }
+  }
+
+  /**
+   * Reads a block and writes each ReadBlock response to the stream as one 
reply:
+   * {@code [int: metadata length][metadata: the response without its 
data][data]}.
+   * Then closes the stream, which sends the terminal reply.
+   * If the read fails, this method throws without closing the stream. Ratis 
sends the failure as the terminal reply.
+   *
+   * @return the number of bytes written to the stream
+   */
+  static long streamReadBlock(ContainerDispatcher dispatcher, 
ContainerCommandRequestProto request,
+      WritableByteChannel stream) throws IOException {
+    final AtomicLong bytesWritten = new AtomicLong();
+    final AtomicReference<Throwable> error = new AtomicReference<>();
+    final StreamObserver<ContainerCommandResponseProto> observer = new 
StreamObserver<ContainerCommandResponseProto>() {
+      @Override
+      public void onNext(ContainerCommandResponseProto response) {
+        try {
+          bytesWritten.addAndGet(writeReadBlockReply(stream, response));
+        } catch (IOException e) {
+          error.set(e);
+          throw Status.CANCELLED.withDescription("Failed to write a ReadBlock 
reply").withCause(e).asRuntimeException();
+        }
+      }
+
+      @Override
+      public void onError(Throwable t) {
+        error.compareAndSet(null, t);
+      }
+
+      @Override
+      public void onCompleted() {
+      }
+    };
+
+    try (RandomAccessFileChannel blockFile = new RandomAccessFileChannel()) {
+      dispatcher.streamDataReadOnly(request, observer, blockFile, null);
+    } catch (StatusRuntimeException e) {
+      error.compareAndSet(null, e);
+    }
+    if (error.get() != null) {
+      throw new IOException("Failed to read block " + 
BlockID.getFromProtobuf(request.getReadBlock().getBlockID()),
+          error.get());
+    }
+    stream.close();

Review Comment:
   Checked the [ratis 
ReadStreamManagement.transfer(..)](https://github.com/apache/ratis/blob/5396d171f4d1a5513b6713d06362c6ef56ba1beb/ratis-netty/src/main/java/org/apache/ratis/netty/server/ReadStreamManagement.java#L251),
 should it add finally to call ReadStream.close()?



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