Repository navigation
HDDS-16722. Serve ReadBlock over Ratis read-only data streams on the datanode #11412
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
c688981
394ce69
6c60bd5
81c2e7d
a4d95fa
47bdca7
7fb1932
95fdf12
f6a6591
a273495
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -29,6 +29,7 @@ | |
| import java.io.InputStream; | ||
| import java.io.OutputStream; | ||
| import java.nio.ByteBuffer; | ||
| import java.nio.channels.WritableByteChannel; | ||
| import java.nio.file.Files; | ||
| import java.util.Arrays; | ||
| import java.util.Collection; | ||
|
|
@@ -53,8 +54,11 @@ | |
| import java.util.concurrent.ThreadPoolExecutor; | ||
| import java.util.concurrent.TimeUnit; | ||
| import java.util.concurrent.atomic.AtomicBoolean; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
| import java.util.concurrent.atomic.AtomicReference; | ||
| import java.util.function.Consumer; | ||
| import org.apache.hadoop.hdds.HddsUtils; | ||
| import org.apache.hadoop.hdds.client.BlockID; | ||
| import org.apache.hadoop.hdds.conf.ConfigurationSource; | ||
| import org.apache.hadoop.hdds.conf.StorageUnit; | ||
| import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos; | ||
|
|
@@ -72,6 +76,7 @@ | |
| import org.apache.hadoop.hdds.scm.utils.ClientCommandsUtils; | ||
| import org.apache.hadoop.hdds.utils.Cache; | ||
| import org.apache.hadoop.hdds.utils.ResourceCache; | ||
| import org.apache.hadoop.hdds.utils.io.RandomAccessFileChannel; | ||
| import org.apache.hadoop.ozone.HddsDatanodeService; | ||
| import org.apache.hadoop.ozone.OzoneConfigKeys; | ||
| import org.apache.hadoop.ozone.common.utils.BufferUtils; | ||
|
|
@@ -109,6 +114,9 @@ | |
| import org.apache.ratis.thirdparty.com.google.protobuf.InvalidProtocolBufferException; | ||
| import org.apache.ratis.thirdparty.com.google.protobuf.TextFormat; | ||
| import org.apache.ratis.thirdparty.com.google.protobuf.UnsafeByteOperations; | ||
| import org.apache.ratis.thirdparty.io.grpc.Status; | ||
| import org.apache.ratis.thirdparty.io.grpc.StatusRuntimeException; | ||
| import org.apache.ratis.thirdparty.io.grpc.stub.StreamObserver; | ||
| import org.apache.ratis.util.FileUtils; | ||
| import org.apache.ratis.util.JavaUtils; | ||
| import org.apache.ratis.util.LifeCycle; | ||
|
|
@@ -124,6 +132,7 @@ | |
| * <p> | ||
| * The container requests can be divided into readonly request, WriteChunk request and other write requests. | ||
| * - Read only requests (see {@link HddsUtils#isReadOnly}) are handled by {@link #query(Message)}. | ||
| * - ReadBlock requests on a Ratis read-only data stream are handled by {@link #transferTo}. | ||
| * - WriteChunk request contains user data | ||
| * - Other write request does not contain user data. | ||
| * <p> | ||
|
|
@@ -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, | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Curious why we make this as static? Is it for easy testing?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just because it doesnt depend on ContainerStateMachine. |
||
| 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(); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Checked the ratis ReadStreamManagement.transfer(..), should it add finally to call ReadStream.close()?
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. +1. If the Ratis handle the stream.close() and stream.fail() (which it does already), the application won't need to worry but just either return normally or throw proper error.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Going back to now without the proper stream.close() on the Ratis side, if we check the existing Ratis implementation https://github.com/apache/ratis/blob/5396d171f4d1a5513b6713d06362c6ef56ba1beb/ratis-netty/src/main/java/org/apache/ratis/netty/server/ReadStreamManagement.java#L252-L265 The minimal is to ensure that the stream only closes when return normally, otherwise Ratis won't be able to send back a proper From this perspective, the current |
||
| return bytesWritten.get(); | ||
| } | ||
|
|
||
| private static int writeReadBlockReply(WritableByteChannel stream, ContainerCommandResponseProto response) | ||
| throws IOException { | ||
| final ByteString data; | ||
| final ContainerCommandResponseProto metadata; | ||
| if (response.hasReadBlock()) { | ||
| data = response.getReadBlock().getData(); | ||
| metadata = response.toBuilder() | ||
| .setReadBlock(response.getReadBlock().toBuilder().setData(ByteString.EMPTY)) | ||
| .build(); | ||
| } else { | ||
| data = ByteString.EMPTY; | ||
| metadata = response; | ||
| } | ||
| final ByteString metadataBytes = metadata.toByteString(); | ||
| final ByteBuffer reply = ByteBuffer.allocate(Integer.BYTES + metadataBytes.size() + data.size()); | ||
| reply.putInt(metadataBytes.size()); | ||
| metadataBytes.copyTo(reply); | ||
| data.copyTo(reply); | ||
| reply.flip(); | ||
|
|
||
| // The client parses each reply on its own, so the reply must be sent in one write. | ||
| final int length = reply.remaining(); | ||
| final int written = stream.write(reply); | ||
| if (written != length) { | ||
| throw new IOException("Failed to write a ReadBlock reply: only " + written + " of " + length + " bytes written"); | ||
| } | ||
| return length; | ||
| } | ||
|
|
||
| private ByteString readStateMachineData( | ||
| ContainerCommandRequestProto requestProto, long term, long index) | ||
| throws IOException { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should throw an exception: