rich7420 commented on code in PR #11412:
URL: https://github.com/apache/ozone/pull/11412#discussion_r4204861268
##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ContainerStateMachine.java:
##########
@@ -889,6 +897,102 @@ 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) {
+ if (error.get() != null) {
+ // The read already failed. Drop the error response that the
dispatcher sends after a failed write.
+ return;
+ }
+ try {
+ bytesWritten.addAndGet(writeReadBlockReply(stream, response));
+ } catch (IOException e) {
+ error.set(e);
+ // Throw to stop reading the block
+ throw new UncheckedIOException(e);
Review Comment:
Could we let `UncheckedIOException` propagate through
`KeyValueHandler.readBlock`? It currently becomes `CONTAINER_INTERNAL_ERROR`
and triggers a container scan on client disconnect. Please add a test asserting
no scan is triggered.
##########
hadoop-hdds/container-service/src/main/java/org/apache/hadoop/ozone/container/common/transport/server/ratis/ClosedContainerReadResolver.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * 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 static
org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos.ContainerDataProto.State.CLOSED;
+
+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 for a closed container,
without a Raft group.
+ * SCM gives the read pipeline of a closed container a random ID, and no Raft
group has this ID.
+ * A closed container does not change, 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 closed container on this
datanode. 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.getContainerState() != CLOSED) {
Review Comment:
Should this also accept `QUASI_CLOSED`? SCM returns a read pipeline without
an existing Raft group for that state too.
--
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]